src/HOL/Library/Poly_Deriv.thy
author wenzelm
Tue Jan 05 21:57:21 2016 +0100 (2016-01-05)
changeset 62072 bf3d9f113474
parent 62065 1546a042e87b
child 62128 3201ddb00097
permissions -rw-r--r--
isabelle update_cartouches -c -t;
wenzelm@41959
     1
(*  Title:      HOL/Library/Poly_Deriv.thy
huffman@29985
     2
    Author:     Amine Chaieb
wenzelm@41959
     3
    Author:     Brian Huffman
huffman@29985
     4
*)
huffman@29985
     5
wenzelm@60500
     6
section\<open>Polynomials and Differentiation\<close>
huffman@29985
     7
huffman@29985
     8
theory Poly_Deriv
huffman@29985
     9
imports Deriv Polynomial
huffman@29985
    10
begin
huffman@29985
    11
wenzelm@60500
    12
subsection \<open>Derivatives of univariate polynomials\<close>
huffman@29985
    13
haftmann@52380
    14
function pderiv :: "'a::real_normed_field poly \<Rightarrow> 'a poly"
haftmann@52380
    15
where
haftmann@52380
    16
  [simp del]: "pderiv (pCons a p) = (if p = 0 then 0 else p + pCons 0 (pderiv p))"
haftmann@52380
    17
  by (auto intro: pCons_cases)
haftmann@52380
    18
haftmann@52380
    19
termination pderiv
haftmann@52380
    20
  by (relation "measure degree") simp_all
huffman@29985
    21
haftmann@52380
    22
lemma pderiv_0 [simp]:
haftmann@52380
    23
  "pderiv 0 = 0"
haftmann@52380
    24
  using pderiv.simps [of 0 0] by simp
huffman@29985
    25
haftmann@52380
    26
lemma pderiv_pCons:
haftmann@52380
    27
  "pderiv (pCons a p) = p + pCons 0 (pderiv p)"
haftmann@52380
    28
  by (simp add: pderiv.simps)
huffman@29985
    29
huffman@29985
    30
lemma coeff_pderiv: "coeff (pderiv p) n = of_nat (Suc n) * coeff p (Suc n)"
lp15@56383
    31
  by (induct p arbitrary: n) 
lp15@56383
    32
     (auto simp add: pderiv_pCons coeff_pCons algebra_simps split: nat.split)
huffman@29985
    33
haftmann@52380
    34
primrec pderiv_coeffs :: "'a::comm_monoid_add list \<Rightarrow> 'a list"
haftmann@52380
    35
where
haftmann@52380
    36
  "pderiv_coeffs [] = []"
haftmann@52380
    37
| "pderiv_coeffs (x # xs) = plus_coeffs xs (cCons 0 (pderiv_coeffs xs))"
haftmann@52380
    38
haftmann@52380
    39
lemma coeffs_pderiv [code abstract]:
haftmann@52380
    40
  "coeffs (pderiv p) = pderiv_coeffs (coeffs p)"
haftmann@52380
    41
  by (rule sym, induct p) (simp_all add: pderiv_pCons coeffs_plus_eq_plus_coeffs cCons_def)
haftmann@52380
    42
huffman@29985
    43
lemma pderiv_eq_0_iff: "pderiv p = 0 \<longleftrightarrow> degree p = 0"
huffman@29985
    44
  apply (rule iffI)
huffman@29985
    45
  apply (cases p, simp)
haftmann@52380
    46
  apply (simp add: poly_eq_iff coeff_pderiv del: of_nat_Suc)
haftmann@52380
    47
  apply (simp add: poly_eq_iff coeff_pderiv coeff_eq_0)
huffman@29985
    48
  done
huffman@29985
    49
huffman@29985
    50
lemma degree_pderiv: "degree (pderiv p) = degree p - 1"
huffman@29985
    51
  apply (rule order_antisym [OF degree_le])
huffman@29985
    52
  apply (simp add: coeff_pderiv coeff_eq_0)
huffman@29985
    53
  apply (cases "degree p", simp)
huffman@29985
    54
  apply (rule le_degree)
huffman@29985
    55
  apply (simp add: coeff_pderiv del: of_nat_Suc)
lp15@56383
    56
  apply (metis degree_0 leading_coeff_0_iff nat.distinct(1))
huffman@29985
    57
  done
huffman@29985
    58
huffman@29985
    59
lemma pderiv_singleton [simp]: "pderiv [:a:] = 0"
huffman@29985
    60
by (simp add: pderiv_pCons)
huffman@29985
    61
huffman@29985
    62
lemma pderiv_add: "pderiv (p + q) = pderiv p + pderiv q"
haftmann@52380
    63
by (rule poly_eqI, simp add: coeff_pderiv algebra_simps)
huffman@29985
    64
huffman@29985
    65
lemma pderiv_minus: "pderiv (- p) = - pderiv p"
haftmann@52380
    66
by (rule poly_eqI, simp add: coeff_pderiv)
huffman@29985
    67
huffman@29985
    68
lemma pderiv_diff: "pderiv (p - q) = pderiv p - pderiv q"
haftmann@52380
    69
by (rule poly_eqI, simp add: coeff_pderiv algebra_simps)
huffman@29985
    70
huffman@29985
    71
lemma pderiv_smult: "pderiv (smult a p) = smult a (pderiv p)"
haftmann@52380
    72
by (rule poly_eqI, simp add: coeff_pderiv algebra_simps)
huffman@29985
    73
huffman@29985
    74
lemma pderiv_mult: "pderiv (p * q) = p * pderiv q + q * pderiv p"
lp15@56383
    75
by (induct p) (auto simp: pderiv_add pderiv_smult pderiv_pCons algebra_simps)
huffman@29985
    76
huffman@29985
    77
lemma pderiv_power_Suc:
huffman@29985
    78
  "pderiv (p ^ Suc n) = smult (of_nat (Suc n)) (p ^ n) * pderiv p"
huffman@29985
    79
apply (induct n)
huffman@29985
    80
apply simp
huffman@29985
    81
apply (subst power_Suc)
huffman@29985
    82
apply (subst pderiv_mult)
huffman@29985
    83
apply (erule ssubst)
huffman@47108
    84
apply (simp only: of_nat_Suc smult_add_left smult_1_left)
lp15@56383
    85
apply (simp add: algebra_simps)
huffman@29985
    86
done
huffman@29985
    87
huffman@29985
    88
lemma DERIV_pow2: "DERIV (%x. x ^ Suc n) x :> real (Suc n) * (x ^ n)"
huffman@44317
    89
by (rule DERIV_cong, rule DERIV_pow, simp)
huffman@29985
    90
declare DERIV_pow2 [simp] DERIV_pow [simp]
huffman@29985
    91
huffman@29985
    92
lemma DERIV_add_const: "DERIV f x :> D ==>  DERIV (%x. a + f x :: 'a::real_normed_field) x :> D"
huffman@44317
    93
by (rule DERIV_cong, rule DERIV_add, auto)
huffman@29985
    94
huffman@29985
    95
lemma poly_DERIV[simp]: "DERIV (%x. poly p x) x :> poly (pderiv p) x"
hoelzl@56381
    96
  by (induct p, auto intro!: derivative_eq_intros simp add: pderiv_pCons)
huffman@29985
    97
eberlm@62065
    98
lemma continuous_on_poly [continuous_intros]: 
eberlm@62065
    99
  fixes p :: "'a :: {real_normed_field} poly"
eberlm@62065
   100
  assumes "continuous_on A f"
eberlm@62065
   101
  shows   "continuous_on A (\<lambda>x. poly p (f x))"
eberlm@62065
   102
proof -
eberlm@62065
   103
  have "continuous_on A (\<lambda>x. (\<Sum>i\<le>degree p. (f x) ^ i * coeff p i))" 
eberlm@62065
   104
    by (intro continuous_intros assms)
eberlm@62065
   105
  also have "\<dots> = (\<lambda>x. poly p (f x))" by (intro ext) (simp add: poly_altdef mult_ac)
eberlm@62065
   106
  finally show ?thesis .
eberlm@62065
   107
qed
eberlm@62065
   108
wenzelm@60500
   109
text\<open>Consequences of the derivative theorem above\<close>
huffman@29985
   110
hoelzl@56181
   111
lemma poly_differentiable[simp]: "(%x. poly p x) differentiable (at x::real filter)"
hoelzl@56181
   112
apply (simp add: real_differentiable_def)
huffman@29985
   113
apply (blast intro: poly_DERIV)
huffman@29985
   114
done
huffman@29985
   115
huffman@29985
   116
lemma poly_isCont[simp]: "isCont (%x. poly p x) (x::real)"
huffman@29985
   117
by (rule poly_DERIV [THEN DERIV_isCont])
huffman@29985
   118
huffman@29985
   119
lemma poly_IVT_pos: "[| a < b; poly p (a::real) < 0; 0 < poly p b |]
huffman@29985
   120
      ==> \<exists>x. a < x & x < b & (poly p x = 0)"
lp15@56383
   121
using IVT_objl [of "poly p" a 0 b]
lp15@56383
   122
by (auto simp add: order_le_less)
huffman@29985
   123
huffman@29985
   124
lemma poly_IVT_neg: "[| (a::real) < b; 0 < poly p a; poly p b < 0 |]
huffman@29985
   125
      ==> \<exists>x. a < x & x < b & (poly p x = 0)"
huffman@29985
   126
by (insert poly_IVT_pos [where p = "- p" ]) simp
huffman@29985
   127
eberlm@62065
   128
lemma poly_IVT:
eberlm@62065
   129
  fixes p::"real poly"
eberlm@62065
   130
  assumes "a<b" and "poly p a * poly p b < 0"
eberlm@62065
   131
  shows "\<exists>x>a. x < b \<and> poly p x = 0"
eberlm@62065
   132
by (metis assms(1) assms(2) less_not_sym mult_less_0_iff poly_IVT_neg poly_IVT_pos)
eberlm@62065
   133
huffman@29985
   134
lemma poly_MVT: "(a::real) < b ==>
huffman@29985
   135
     \<exists>x. a < x & x < b & (poly p b - poly p a = (b - a) * poly (pderiv p) x)"
lp15@56383
   136
using MVT [of a b "poly p"]
lp15@56383
   137
apply auto
huffman@29985
   138
apply (rule_tac x = z in exI)
lp15@56217
   139
apply (auto simp add: mult_left_cancel poly_DERIV [THEN DERIV_unique])
huffman@29985
   140
done
huffman@29985
   141
eberlm@62065
   142
lemma poly_MVT':
eberlm@62065
   143
  assumes "{min a b..max a b} \<subseteq> A"
eberlm@62065
   144
  shows   "\<exists>x\<in>A. poly p b - poly p a = (b - a) * poly (pderiv p) (x::real)"
eberlm@62065
   145
proof (cases a b rule: linorder_cases)
eberlm@62065
   146
  case less
eberlm@62065
   147
  from poly_MVT[OF less, of p] guess x by (elim exE conjE)
eberlm@62065
   148
  thus ?thesis by (intro bexI[of _ x]) (auto intro!: subsetD[OF assms])
eberlm@62065
   149
eberlm@62065
   150
next
eberlm@62065
   151
  case greater
eberlm@62065
   152
  from poly_MVT[OF greater, of p] guess x by (elim exE conjE)
eberlm@62065
   153
  thus ?thesis by (intro bexI[of _ x]) (auto simp: algebra_simps intro!: subsetD[OF assms])
eberlm@62065
   154
qed (insert assms, auto)
eberlm@62065
   155
eberlm@62065
   156
lemma poly_pinfty_gt_lc:
eberlm@62065
   157
  fixes p:: "real poly"
eberlm@62065
   158
  assumes  "lead_coeff p > 0" 
eberlm@62065
   159
  shows "\<exists> n. \<forall> x \<ge> n. poly p x \<ge> lead_coeff p" using assms
eberlm@62065
   160
proof (induct p)
eberlm@62065
   161
  case 0
eberlm@62065
   162
  thus ?case by auto
eberlm@62065
   163
next
eberlm@62065
   164
  case (pCons a p)
eberlm@62065
   165
  have "\<lbrakk>a\<noteq>0;p=0\<rbrakk> \<Longrightarrow> ?case" by auto
eberlm@62065
   166
  moreover have "p\<noteq>0 \<Longrightarrow> ?case"
eberlm@62065
   167
    proof -
eberlm@62065
   168
      assume "p\<noteq>0"
eberlm@62065
   169
      then obtain n1 where gte_lcoeff:"\<forall>x\<ge>n1. lead_coeff p \<le> poly p x" using that pCons by auto
wenzelm@62072
   170
      have gt_0:"lead_coeff p >0" using pCons(3) \<open>p\<noteq>0\<close> by auto
eberlm@62065
   171
      def n\<equiv>"max n1 (1+ \<bar>a\<bar>/(lead_coeff p))"
eberlm@62065
   172
      show ?thesis 
eberlm@62065
   173
        proof (rule_tac x=n in exI,rule,rule) 
eberlm@62065
   174
          fix x assume "n \<le> x"
eberlm@62065
   175
          hence "lead_coeff p \<le> poly p x" 
eberlm@62065
   176
            using gte_lcoeff unfolding n_def by auto
eberlm@62065
   177
          hence " \<bar>a\<bar>/(lead_coeff p) \<ge> \<bar>a\<bar>/(poly p x)" and "poly p x>0" using gt_0
eberlm@62065
   178
            by (intro frac_le,auto)
wenzelm@62072
   179
          hence "x\<ge>1+ \<bar>a\<bar>/(poly p x)" using \<open>n\<le>x\<close>[unfolded n_def] by auto
eberlm@62065
   180
          thus "lead_coeff (pCons a p) \<le> poly (pCons a p) x"
wenzelm@62072
   181
            using \<open>lead_coeff p \<le> poly p x\<close> \<open>poly p x>0\<close> \<open>p\<noteq>0\<close>
eberlm@62065
   182
            by (auto simp add:field_simps)
eberlm@62065
   183
        qed
eberlm@62065
   184
    qed
eberlm@62065
   185
  ultimately show ?case by fastforce
eberlm@62065
   186
qed
eberlm@62065
   187
eberlm@62065
   188
wenzelm@60500
   189
text\<open>Lemmas for Derivatives\<close>
huffman@29985
   190
huffman@29985
   191
lemma order_unique_lemma:
huffman@29985
   192
  fixes p :: "'a::idom poly"
lp15@56383
   193
  assumes "[:-a, 1:] ^ n dvd p" "\<not> [:-a, 1:] ^ Suc n dvd p"
huffman@29985
   194
  shows "n = order a p"
huffman@29985
   195
unfolding Polynomial.order_def
huffman@29985
   196
apply (rule Least_equality [symmetric])
haftmann@58199
   197
apply (fact assms)
haftmann@58199
   198
apply (rule classical)
haftmann@58199
   199
apply (erule notE)
haftmann@58199
   200
unfolding not_less_eq_eq
haftmann@58199
   201
using assms(1) apply (rule power_le_dvd)
haftmann@58199
   202
apply assumption
haftmann@58199
   203
done
huffman@29985
   204
huffman@29985
   205
lemma lemma_order_pderiv1:
huffman@29985
   206
  "pderiv ([:- a, 1:] ^ Suc n * q) = [:- a, 1:] ^ Suc n * pderiv q +
huffman@29985
   207
    smult (of_nat (Suc n)) (q * [:- a, 1:] ^ n)"
huffman@29985
   208
apply (simp only: pderiv_mult pderiv_power_Suc)
huffman@30273
   209
apply (simp del: power_Suc of_nat_Suc add: pderiv_pCons)
huffman@29985
   210
done
huffman@29985
   211
huffman@29985
   212
lemma dvd_add_cancel1:
huffman@29985
   213
  fixes a b c :: "'a::comm_ring_1"
huffman@29985
   214
  shows "a dvd b + c \<Longrightarrow> a dvd b \<Longrightarrow> a dvd c"
haftmann@35050
   215
  by (drule (1) Rings.dvd_diff, simp)
huffman@29985
   216
lp15@56383
   217
lemma lemma_order_pderiv:
lp15@56383
   218
  assumes n: "0 < n" 
lp15@56383
   219
      and pd: "pderiv p \<noteq> 0" 
lp15@56383
   220
      and pe: "p = [:- a, 1:] ^ n * q" 
lp15@56383
   221
      and nd: "~ [:- a, 1:] dvd q"
lp15@56383
   222
    shows "n = Suc (order a (pderiv p))"
lp15@56383
   223
using n 
lp15@56383
   224
proof -
lp15@56383
   225
  have "pderiv ([:- a, 1:] ^ n * q) \<noteq> 0"
lp15@56383
   226
    using assms by auto
lp15@56383
   227
  obtain n' where "n = Suc n'" "0 < Suc n'" "pderiv ([:- a, 1:] ^ Suc n' * q) \<noteq> 0"
lp15@56383
   228
    using assms by (cases n) auto
lp15@56383
   229
  then have *: "!!k l. k dvd k * pderiv q + smult (of_nat (Suc n')) l \<Longrightarrow> k dvd l"
lp15@56383
   230
    by (metis dvd_add_cancel1 dvd_smult_iff dvd_triv_left of_nat_eq_0_iff old.nat.distinct(2))
lp15@56383
   231
  have "n' = order a (pderiv ([:- a, 1:] ^ Suc n' * q))" 
lp15@56383
   232
  proof (rule order_unique_lemma)
lp15@56383
   233
    show "[:- a, 1:] ^ n' dvd pderiv ([:- a, 1:] ^ Suc n' * q)"
lp15@56383
   234
      apply (subst lemma_order_pderiv1)
lp15@56383
   235
      apply (rule dvd_add)
lp15@56383
   236
      apply (metis dvdI dvd_mult2 power_Suc2)
lp15@56383
   237
      apply (metis dvd_smult dvd_triv_right)
lp15@56383
   238
      done
lp15@56383
   239
  next
lp15@56383
   240
    show "\<not> [:- a, 1:] ^ Suc n' dvd pderiv ([:- a, 1:] ^ Suc n' * q)"
lp15@56383
   241
     apply (subst lemma_order_pderiv1)
haftmann@60867
   242
     by (metis * nd dvd_mult_cancel_right power_not_zero pCons_eq_0_iff power_Suc zero_neq_one)
lp15@56383
   243
  qed
lp15@56383
   244
  then show ?thesis
wenzelm@60500
   245
    by (metis \<open>n = Suc n'\<close> pe)
lp15@56383
   246
qed
huffman@29985
   247
huffman@29985
   248
lemma order_decomp:
haftmann@60688
   249
  assumes "p \<noteq> 0"
haftmann@60688
   250
  shows "\<exists>q. p = [:- a, 1:] ^ order a p * q \<and> \<not> [:- a, 1:] dvd q"
haftmann@60688
   251
proof -
haftmann@60688
   252
  from assms have A: "[:- a, 1:] ^ order a p dvd p"
haftmann@60688
   253
    and B: "\<not> [:- a, 1:] ^ Suc (order a p) dvd p" by (auto dest: order)
haftmann@60688
   254
  from A obtain q where C: "p = [:- a, 1:] ^ order a p * q" ..
haftmann@60688
   255
  with B have "\<not> [:- a, 1:] ^ Suc (order a p) dvd [:- a, 1:] ^ order a p * q"
haftmann@60688
   256
    by simp
haftmann@60688
   257
  then have "\<not> [:- a, 1:] ^ order a p * [:- a, 1:] dvd [:- a, 1:] ^ order a p * q"
haftmann@60688
   258
    by simp
haftmann@60688
   259
  then have D: "\<not> [:- a, 1:] dvd q"
haftmann@60688
   260
    using idom_class.dvd_mult_cancel_left [of "[:- a, 1:] ^ order a p" "[:- a, 1:]" q]
haftmann@60688
   261
    by auto
haftmann@60688
   262
  from C D show ?thesis by blast
haftmann@60688
   263
qed
huffman@29985
   264
huffman@29985
   265
lemma order_pderiv: "[| pderiv p \<noteq> 0; order a p \<noteq> 0 |]
huffman@29985
   266
      ==> (order a p = Suc (order a (pderiv p)))"
huffman@29985
   267
apply (case_tac "p = 0", simp)
huffman@29985
   268
apply (drule_tac a = a and p = p in order_decomp)
huffman@29985
   269
using neq0_conv
huffman@29985
   270
apply (blast intro: lemma_order_pderiv)
huffman@29985
   271
done
huffman@29985
   272
huffman@29985
   273
lemma order_mult: "p * q \<noteq> 0 \<Longrightarrow> order a (p * q) = order a p + order a q"
huffman@29985
   274
proof -
huffman@29985
   275
  def i \<equiv> "order a p"
huffman@29985
   276
  def j \<equiv> "order a q"
huffman@29985
   277
  def t \<equiv> "[:-a, 1:]"
huffman@29985
   278
  have t_dvd_iff: "\<And>u. t dvd u \<longleftrightarrow> poly u a = 0"
huffman@29985
   279
    unfolding t_def by (simp add: dvd_iff_poly_eq_0)
huffman@29985
   280
  assume "p * q \<noteq> 0"
huffman@29985
   281
  then show "order a (p * q) = i + j"
huffman@29985
   282
    apply clarsimp
huffman@29985
   283
    apply (drule order [where a=a and p=p, folded i_def t_def])
huffman@29985
   284
    apply (drule order [where a=a and p=q, folded j_def t_def])
huffman@29985
   285
    apply clarify
lp15@56383
   286
    apply (erule dvdE)+
huffman@29985
   287
    apply (rule order_unique_lemma [symmetric], fold t_def)
lp15@56383
   288
    apply (simp_all add: power_add t_dvd_iff)
huffman@29985
   289
    done
huffman@29985
   290
qed
huffman@29985
   291
eberlm@62065
   292
lemma order_smult:
eberlm@62065
   293
  assumes "c \<noteq> 0" 
eberlm@62065
   294
  shows "order x (smult c p) = order x p"
eberlm@62065
   295
proof (cases "p = 0")
eberlm@62065
   296
  case False
eberlm@62065
   297
  have "smult c p = [:c:] * p" by simp
eberlm@62065
   298
  also from assms False have "order x \<dots> = order x [:c:] + order x p" 
eberlm@62065
   299
    by (subst order_mult) simp_all
eberlm@62065
   300
  also from assms have "order x [:c:] = 0" by (intro order_0I) auto
eberlm@62065
   301
  finally show ?thesis by simp
eberlm@62065
   302
qed simp
eberlm@62065
   303
eberlm@62065
   304
(* Next two lemmas contributed by Wenda Li *)
eberlm@62065
   305
lemma order_1_eq_0 [simp]:"order x 1 = 0" 
eberlm@62065
   306
  by (metis order_root poly_1 zero_neq_one)
eberlm@62065
   307
eberlm@62065
   308
lemma order_power_n_n: "order a ([:-a,1:]^n)=n" 
eberlm@62065
   309
proof (induct n) (*might be proved more concisely using nat_less_induct*)
eberlm@62065
   310
  case 0
eberlm@62065
   311
  thus ?case by (metis order_root poly_1 power_0 zero_neq_one)
eberlm@62065
   312
next 
eberlm@62065
   313
  case (Suc n)
eberlm@62065
   314
  have "order a ([:- a, 1:] ^ Suc n)=order a ([:- a, 1:] ^ n) + order a [:-a,1:]" 
eberlm@62065
   315
    by (metis (no_types, hide_lams) One_nat_def add_Suc_right monoid_add_class.add.right_neutral 
eberlm@62065
   316
      one_neq_zero order_mult pCons_eq_0_iff power_add power_eq_0_iff power_one_right)
eberlm@62065
   317
  moreover have "order a [:-a,1:]=1" unfolding order_def
eberlm@62065
   318
    proof (rule Least_equality,rule ccontr)
eberlm@62065
   319
      assume  "\<not> \<not> [:- a, 1:] ^ Suc 1 dvd [:- a, 1:]"
eberlm@62065
   320
      hence "[:- a, 1:] ^ Suc 1 dvd [:- a, 1:]" by simp
eberlm@62065
   321
      hence "degree ([:- a, 1:] ^ Suc 1) \<le> degree ([:- a, 1:] )" 
eberlm@62065
   322
        by (rule dvd_imp_degree_le,auto) 
eberlm@62065
   323
      thus False by auto
eberlm@62065
   324
    next
eberlm@62065
   325
      fix y assume asm:"\<not> [:- a, 1:] ^ Suc y dvd [:- a, 1:]"
eberlm@62065
   326
      show "1 \<le> y" 
eberlm@62065
   327
        proof (rule ccontr)
eberlm@62065
   328
          assume "\<not> 1 \<le> y"
eberlm@62065
   329
          hence "y=0" by auto
eberlm@62065
   330
          hence "[:- a, 1:] ^ Suc y dvd [:- a, 1:]" by auto
eberlm@62065
   331
          thus False using asm by auto
eberlm@62065
   332
        qed
eberlm@62065
   333
    qed
eberlm@62065
   334
  ultimately show ?case using Suc by auto
eberlm@62065
   335
qed
eberlm@62065
   336
wenzelm@60500
   337
text\<open>Now justify the standard squarefree decomposition, i.e. f / gcd(f,f').\<close>
huffman@29985
   338
huffman@29985
   339
lemma order_divides: "[:-a, 1:] ^ n dvd p \<longleftrightarrow> p = 0 \<or> n \<le> order a p"
huffman@29985
   340
apply (cases "p = 0", auto)
huffman@29985
   341
apply (drule order_2 [where a=a and p=p])
lp15@56383
   342
apply (metis not_less_eq_eq power_le_dvd)
huffman@29985
   343
apply (erule power_le_dvd [OF order_1])
huffman@29985
   344
done
huffman@29985
   345
huffman@29985
   346
lemma poly_squarefree_decomp_order:
huffman@29985
   347
  assumes "pderiv p \<noteq> 0"
huffman@29985
   348
  and p: "p = q * d"
huffman@29985
   349
  and p': "pderiv p = e * d"
huffman@29985
   350
  and d: "d = r * p + s * pderiv p"
huffman@29985
   351
  shows "order a q = (if order a p = 0 then 0 else 1)"
huffman@29985
   352
proof (rule classical)
huffman@29985
   353
  assume 1: "order a q \<noteq> (if order a p = 0 then 0 else 1)"
wenzelm@60500
   354
  from \<open>pderiv p \<noteq> 0\<close> have "p \<noteq> 0" by auto
huffman@29985
   355
  with p have "order a p = order a q + order a d"
huffman@29985
   356
    by (simp add: order_mult)
huffman@29985
   357
  with 1 have "order a p \<noteq> 0" by (auto split: if_splits)
huffman@29985
   358
  have "order a (pderiv p) = order a e + order a d"
wenzelm@60500
   359
    using \<open>pderiv p \<noteq> 0\<close> \<open>pderiv p = e * d\<close> by (simp add: order_mult)
huffman@29985
   360
  have "order a p = Suc (order a (pderiv p))"
wenzelm@60500
   361
    using \<open>pderiv p \<noteq> 0\<close> \<open>order a p \<noteq> 0\<close> by (rule order_pderiv)
wenzelm@60500
   362
  have "d \<noteq> 0" using \<open>p \<noteq> 0\<close> \<open>p = q * d\<close> by simp
huffman@29985
   363
  have "([:-a, 1:] ^ (order a (pderiv p))) dvd d"
huffman@29985
   364
    apply (simp add: d)
huffman@29985
   365
    apply (rule dvd_add)
huffman@29985
   366
    apply (rule dvd_mult)
wenzelm@60500
   367
    apply (simp add: order_divides \<open>p \<noteq> 0\<close>
wenzelm@60500
   368
           \<open>order a p = Suc (order a (pderiv p))\<close>)
huffman@29985
   369
    apply (rule dvd_mult)
huffman@29985
   370
    apply (simp add: order_divides)
huffman@29985
   371
    done
huffman@29985
   372
  then have "order a (pderiv p) \<le> order a d"
wenzelm@60500
   373
    using \<open>d \<noteq> 0\<close> by (simp add: order_divides)
huffman@29985
   374
  show ?thesis
wenzelm@60500
   375
    using \<open>order a p = order a q + order a d\<close>
wenzelm@60500
   376
    using \<open>order a (pderiv p) = order a e + order a d\<close>
wenzelm@60500
   377
    using \<open>order a p = Suc (order a (pderiv p))\<close>
wenzelm@60500
   378
    using \<open>order a (pderiv p) \<le> order a d\<close>
huffman@29985
   379
    by auto
huffman@29985
   380
qed
huffman@29985
   381
huffman@29985
   382
lemma poly_squarefree_decomp_order2: "[| pderiv p \<noteq> 0;
huffman@29985
   383
         p = q * d;
huffman@29985
   384
         pderiv p = e * d;
huffman@29985
   385
         d = r * p + s * pderiv p
huffman@29985
   386
      |] ==> \<forall>a. order a q = (if order a p = 0 then 0 else 1)"
lp15@56383
   387
by (blast intro: poly_squarefree_decomp_order)
huffman@29985
   388
huffman@29985
   389
lemma order_pderiv2: "[| pderiv p \<noteq> 0; order a p \<noteq> 0 |]
huffman@29985
   390
      ==> (order a (pderiv p) = n) = (order a p = Suc n)"
lp15@56383
   391
by (auto dest: order_pderiv)
huffman@29985
   392
huffman@29985
   393
definition
huffman@29985
   394
  rsquarefree :: "'a::idom poly => bool" where
huffman@29985
   395
  "rsquarefree p = (p \<noteq> 0 & (\<forall>a. (order a p = 0) | (order a p = 1)))"
huffman@29985
   396
huffman@29985
   397
lemma pderiv_iszero: "pderiv p = 0 \<Longrightarrow> \<exists>h. p = [:h:]"
huffman@29985
   398
apply (simp add: pderiv_eq_0_iff)
huffman@29985
   399
apply (case_tac p, auto split: if_splits)
huffman@29985
   400
done
huffman@29985
   401
huffman@29985
   402
lemma rsquarefree_roots:
huffman@29985
   403
  "rsquarefree p = (\<forall>a. ~(poly p a = 0 & poly (pderiv p) a = 0))"
huffman@29985
   404
apply (simp add: rsquarefree_def)
huffman@29985
   405
apply (case_tac "p = 0", simp, simp)
huffman@29985
   406
apply (case_tac "pderiv p = 0")
huffman@29985
   407
apply simp
lp15@56383
   408
apply (drule pderiv_iszero, clarsimp)
lp15@56383
   409
apply (metis coeff_0 coeff_pCons_0 degree_pCons_0 le0 le_antisym order_degree)
lp15@56383
   410
apply (force simp add: order_root order_pderiv2)
huffman@29985
   411
done
huffman@29985
   412
huffman@29985
   413
lemma poly_squarefree_decomp:
huffman@29985
   414
  assumes "pderiv p \<noteq> 0"
huffman@29985
   415
    and "p = q * d"
huffman@29985
   416
    and "pderiv p = e * d"
huffman@29985
   417
    and "d = r * p + s * pderiv p"
huffman@29985
   418
  shows "rsquarefree q & (\<forall>a. (poly q a = 0) = (poly p a = 0))"
huffman@29985
   419
proof -
wenzelm@60500
   420
  from \<open>pderiv p \<noteq> 0\<close> have "p \<noteq> 0" by auto
wenzelm@60500
   421
  with \<open>p = q * d\<close> have "q \<noteq> 0" by simp
huffman@29985
   422
  have "\<forall>a. order a q = (if order a p = 0 then 0 else 1)"
huffman@29985
   423
    using assms by (rule poly_squarefree_decomp_order2)
wenzelm@60500
   424
  with \<open>p \<noteq> 0\<close> \<open>q \<noteq> 0\<close> show ?thesis
huffman@29985
   425
    by (simp add: rsquarefree_def order_root)
huffman@29985
   426
qed
huffman@29985
   427
huffman@29985
   428
end