src/HOL/Library/Poly_Deriv.thy
author wenzelm
Wed Jun 17 11:03:05 2015 +0200 (2015-06-17)
changeset 60500 903bb1495239
parent 58881 b9556a055632
child 60688 01488b559910
permissions -rw-r--r--
isabelle update_cartouches;
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
wenzelm@60500
    98
text\<open>Consequences of the derivative theorem above\<close>
huffman@29985
    99
hoelzl@56181
   100
lemma poly_differentiable[simp]: "(%x. poly p x) differentiable (at x::real filter)"
hoelzl@56181
   101
apply (simp add: real_differentiable_def)
huffman@29985
   102
apply (blast intro: poly_DERIV)
huffman@29985
   103
done
huffman@29985
   104
huffman@29985
   105
lemma poly_isCont[simp]: "isCont (%x. poly p x) (x::real)"
huffman@29985
   106
by (rule poly_DERIV [THEN DERIV_isCont])
huffman@29985
   107
huffman@29985
   108
lemma poly_IVT_pos: "[| a < b; poly p (a::real) < 0; 0 < poly p b |]
huffman@29985
   109
      ==> \<exists>x. a < x & x < b & (poly p x = 0)"
lp15@56383
   110
using IVT_objl [of "poly p" a 0 b]
lp15@56383
   111
by (auto simp add: order_le_less)
huffman@29985
   112
huffman@29985
   113
lemma poly_IVT_neg: "[| (a::real) < b; 0 < poly p a; poly p b < 0 |]
huffman@29985
   114
      ==> \<exists>x. a < x & x < b & (poly p x = 0)"
huffman@29985
   115
by (insert poly_IVT_pos [where p = "- p" ]) simp
huffman@29985
   116
huffman@29985
   117
lemma poly_MVT: "(a::real) < b ==>
huffman@29985
   118
     \<exists>x. a < x & x < b & (poly p b - poly p a = (b - a) * poly (pderiv p) x)"
lp15@56383
   119
using MVT [of a b "poly p"]
lp15@56383
   120
apply auto
huffman@29985
   121
apply (rule_tac x = z in exI)
lp15@56217
   122
apply (auto simp add: mult_left_cancel poly_DERIV [THEN DERIV_unique])
huffman@29985
   123
done
huffman@29985
   124
wenzelm@60500
   125
text\<open>Lemmas for Derivatives\<close>
huffman@29985
   126
huffman@29985
   127
lemma order_unique_lemma:
huffman@29985
   128
  fixes p :: "'a::idom poly"
lp15@56383
   129
  assumes "[:-a, 1:] ^ n dvd p" "\<not> [:-a, 1:] ^ Suc n dvd p"
huffman@29985
   130
  shows "n = order a p"
huffman@29985
   131
unfolding Polynomial.order_def
huffman@29985
   132
apply (rule Least_equality [symmetric])
haftmann@58199
   133
apply (fact assms)
haftmann@58199
   134
apply (rule classical)
haftmann@58199
   135
apply (erule notE)
haftmann@58199
   136
unfolding not_less_eq_eq
haftmann@58199
   137
using assms(1) apply (rule power_le_dvd)
haftmann@58199
   138
apply assumption
haftmann@58199
   139
done
huffman@29985
   140
huffman@29985
   141
lemma lemma_order_pderiv1:
huffman@29985
   142
  "pderiv ([:- a, 1:] ^ Suc n * q) = [:- a, 1:] ^ Suc n * pderiv q +
huffman@29985
   143
    smult (of_nat (Suc n)) (q * [:- a, 1:] ^ n)"
huffman@29985
   144
apply (simp only: pderiv_mult pderiv_power_Suc)
huffman@30273
   145
apply (simp del: power_Suc of_nat_Suc add: pderiv_pCons)
huffman@29985
   146
done
huffman@29985
   147
huffman@29985
   148
lemma dvd_add_cancel1:
huffman@29985
   149
  fixes a b c :: "'a::comm_ring_1"
huffman@29985
   150
  shows "a dvd b + c \<Longrightarrow> a dvd b \<Longrightarrow> a dvd c"
haftmann@35050
   151
  by (drule (1) Rings.dvd_diff, simp)
huffman@29985
   152
lp15@56383
   153
lemma lemma_order_pderiv:
lp15@56383
   154
  assumes n: "0 < n" 
lp15@56383
   155
      and pd: "pderiv p \<noteq> 0" 
lp15@56383
   156
      and pe: "p = [:- a, 1:] ^ n * q" 
lp15@56383
   157
      and nd: "~ [:- a, 1:] dvd q"
lp15@56383
   158
    shows "n = Suc (order a (pderiv p))"
lp15@56383
   159
using n 
lp15@56383
   160
proof -
lp15@56383
   161
  have "pderiv ([:- a, 1:] ^ n * q) \<noteq> 0"
lp15@56383
   162
    using assms by auto
lp15@56383
   163
  obtain n' where "n = Suc n'" "0 < Suc n'" "pderiv ([:- a, 1:] ^ Suc n' * q) \<noteq> 0"
lp15@56383
   164
    using assms by (cases n) auto
lp15@56383
   165
  then have *: "!!k l. k dvd k * pderiv q + smult (of_nat (Suc n')) l \<Longrightarrow> k dvd l"
lp15@56383
   166
    by (metis dvd_add_cancel1 dvd_smult_iff dvd_triv_left of_nat_eq_0_iff old.nat.distinct(2))
lp15@56383
   167
  have "n' = order a (pderiv ([:- a, 1:] ^ Suc n' * q))" 
lp15@56383
   168
  proof (rule order_unique_lemma)
lp15@56383
   169
    show "[:- a, 1:] ^ n' dvd pderiv ([:- a, 1:] ^ Suc n' * q)"
lp15@56383
   170
      apply (subst lemma_order_pderiv1)
lp15@56383
   171
      apply (rule dvd_add)
lp15@56383
   172
      apply (metis dvdI dvd_mult2 power_Suc2)
lp15@56383
   173
      apply (metis dvd_smult dvd_triv_right)
lp15@56383
   174
      done
lp15@56383
   175
  next
lp15@56383
   176
    show "\<not> [:- a, 1:] ^ Suc n' dvd pderiv ([:- a, 1:] ^ Suc n' * q)"
lp15@56383
   177
     apply (subst lemma_order_pderiv1)
lp15@56383
   178
     by (metis * nd dvd_mult_cancel_right field_power_not_zero pCons_eq_0_iff power_Suc zero_neq_one)
lp15@56383
   179
  qed
lp15@56383
   180
  then show ?thesis
wenzelm@60500
   181
    by (metis \<open>n = Suc n'\<close> pe)
lp15@56383
   182
qed
huffman@29985
   183
huffman@29985
   184
lemma order_decomp:
huffman@29985
   185
     "p \<noteq> 0
huffman@29985
   186
      ==> \<exists>q. p = [:-a, 1:] ^ (order a p) * q &
huffman@29985
   187
                ~([:-a, 1:] dvd q)"
huffman@29985
   188
apply (drule order [where a=a])
lp15@56383
   189
by (metis dvdE dvd_mult_cancel_left power_Suc2)
huffman@29985
   190
huffman@29985
   191
lemma order_pderiv: "[| pderiv p \<noteq> 0; order a p \<noteq> 0 |]
huffman@29985
   192
      ==> (order a p = Suc (order a (pderiv p)))"
huffman@29985
   193
apply (case_tac "p = 0", simp)
huffman@29985
   194
apply (drule_tac a = a and p = p in order_decomp)
huffman@29985
   195
using neq0_conv
huffman@29985
   196
apply (blast intro: lemma_order_pderiv)
huffman@29985
   197
done
huffman@29985
   198
huffman@29985
   199
lemma order_mult: "p * q \<noteq> 0 \<Longrightarrow> order a (p * q) = order a p + order a q"
huffman@29985
   200
proof -
huffman@29985
   201
  def i \<equiv> "order a p"
huffman@29985
   202
  def j \<equiv> "order a q"
huffman@29985
   203
  def t \<equiv> "[:-a, 1:]"
huffman@29985
   204
  have t_dvd_iff: "\<And>u. t dvd u \<longleftrightarrow> poly u a = 0"
huffman@29985
   205
    unfolding t_def by (simp add: dvd_iff_poly_eq_0)
huffman@29985
   206
  assume "p * q \<noteq> 0"
huffman@29985
   207
  then show "order a (p * q) = i + j"
huffman@29985
   208
    apply clarsimp
huffman@29985
   209
    apply (drule order [where a=a and p=p, folded i_def t_def])
huffman@29985
   210
    apply (drule order [where a=a and p=q, folded j_def t_def])
huffman@29985
   211
    apply clarify
lp15@56383
   212
    apply (erule dvdE)+
huffman@29985
   213
    apply (rule order_unique_lemma [symmetric], fold t_def)
lp15@56383
   214
    apply (simp_all add: power_add t_dvd_iff)
huffman@29985
   215
    done
huffman@29985
   216
qed
huffman@29985
   217
wenzelm@60500
   218
text\<open>Now justify the standard squarefree decomposition, i.e. f / gcd(f,f').\<close>
huffman@29985
   219
huffman@29985
   220
lemma order_divides: "[:-a, 1:] ^ n dvd p \<longleftrightarrow> p = 0 \<or> n \<le> order a p"
huffman@29985
   221
apply (cases "p = 0", auto)
huffman@29985
   222
apply (drule order_2 [where a=a and p=p])
lp15@56383
   223
apply (metis not_less_eq_eq power_le_dvd)
huffman@29985
   224
apply (erule power_le_dvd [OF order_1])
huffman@29985
   225
done
huffman@29985
   226
huffman@29985
   227
lemma poly_squarefree_decomp_order:
huffman@29985
   228
  assumes "pderiv p \<noteq> 0"
huffman@29985
   229
  and p: "p = q * d"
huffman@29985
   230
  and p': "pderiv p = e * d"
huffman@29985
   231
  and d: "d = r * p + s * pderiv p"
huffman@29985
   232
  shows "order a q = (if order a p = 0 then 0 else 1)"
huffman@29985
   233
proof (rule classical)
huffman@29985
   234
  assume 1: "order a q \<noteq> (if order a p = 0 then 0 else 1)"
wenzelm@60500
   235
  from \<open>pderiv p \<noteq> 0\<close> have "p \<noteq> 0" by auto
huffman@29985
   236
  with p have "order a p = order a q + order a d"
huffman@29985
   237
    by (simp add: order_mult)
huffman@29985
   238
  with 1 have "order a p \<noteq> 0" by (auto split: if_splits)
huffman@29985
   239
  have "order a (pderiv p) = order a e + order a d"
wenzelm@60500
   240
    using \<open>pderiv p \<noteq> 0\<close> \<open>pderiv p = e * d\<close> by (simp add: order_mult)
huffman@29985
   241
  have "order a p = Suc (order a (pderiv p))"
wenzelm@60500
   242
    using \<open>pderiv p \<noteq> 0\<close> \<open>order a p \<noteq> 0\<close> by (rule order_pderiv)
wenzelm@60500
   243
  have "d \<noteq> 0" using \<open>p \<noteq> 0\<close> \<open>p = q * d\<close> by simp
huffman@29985
   244
  have "([:-a, 1:] ^ (order a (pderiv p))) dvd d"
huffman@29985
   245
    apply (simp add: d)
huffman@29985
   246
    apply (rule dvd_add)
huffman@29985
   247
    apply (rule dvd_mult)
wenzelm@60500
   248
    apply (simp add: order_divides \<open>p \<noteq> 0\<close>
wenzelm@60500
   249
           \<open>order a p = Suc (order a (pderiv p))\<close>)
huffman@29985
   250
    apply (rule dvd_mult)
huffman@29985
   251
    apply (simp add: order_divides)
huffman@29985
   252
    done
huffman@29985
   253
  then have "order a (pderiv p) \<le> order a d"
wenzelm@60500
   254
    using \<open>d \<noteq> 0\<close> by (simp add: order_divides)
huffman@29985
   255
  show ?thesis
wenzelm@60500
   256
    using \<open>order a p = order a q + order a d\<close>
wenzelm@60500
   257
    using \<open>order a (pderiv p) = order a e + order a d\<close>
wenzelm@60500
   258
    using \<open>order a p = Suc (order a (pderiv p))\<close>
wenzelm@60500
   259
    using \<open>order a (pderiv p) \<le> order a d\<close>
huffman@29985
   260
    by auto
huffman@29985
   261
qed
huffman@29985
   262
huffman@29985
   263
lemma poly_squarefree_decomp_order2: "[| pderiv p \<noteq> 0;
huffman@29985
   264
         p = q * d;
huffman@29985
   265
         pderiv p = e * d;
huffman@29985
   266
         d = r * p + s * pderiv p
huffman@29985
   267
      |] ==> \<forall>a. order a q = (if order a p = 0 then 0 else 1)"
lp15@56383
   268
by (blast intro: poly_squarefree_decomp_order)
huffman@29985
   269
huffman@29985
   270
lemma order_pderiv2: "[| pderiv p \<noteq> 0; order a p \<noteq> 0 |]
huffman@29985
   271
      ==> (order a (pderiv p) = n) = (order a p = Suc n)"
lp15@56383
   272
by (auto dest: order_pderiv)
huffman@29985
   273
huffman@29985
   274
definition
huffman@29985
   275
  rsquarefree :: "'a::idom poly => bool" where
huffman@29985
   276
  "rsquarefree p = (p \<noteq> 0 & (\<forall>a. (order a p = 0) | (order a p = 1)))"
huffman@29985
   277
huffman@29985
   278
lemma pderiv_iszero: "pderiv p = 0 \<Longrightarrow> \<exists>h. p = [:h:]"
huffman@29985
   279
apply (simp add: pderiv_eq_0_iff)
huffman@29985
   280
apply (case_tac p, auto split: if_splits)
huffman@29985
   281
done
huffman@29985
   282
huffman@29985
   283
lemma rsquarefree_roots:
huffman@29985
   284
  "rsquarefree p = (\<forall>a. ~(poly p a = 0 & poly (pderiv p) a = 0))"
huffman@29985
   285
apply (simp add: rsquarefree_def)
huffman@29985
   286
apply (case_tac "p = 0", simp, simp)
huffman@29985
   287
apply (case_tac "pderiv p = 0")
huffman@29985
   288
apply simp
lp15@56383
   289
apply (drule pderiv_iszero, clarsimp)
lp15@56383
   290
apply (metis coeff_0 coeff_pCons_0 degree_pCons_0 le0 le_antisym order_degree)
lp15@56383
   291
apply (force simp add: order_root order_pderiv2)
huffman@29985
   292
done
huffman@29985
   293
huffman@29985
   294
lemma poly_squarefree_decomp:
huffman@29985
   295
  assumes "pderiv p \<noteq> 0"
huffman@29985
   296
    and "p = q * d"
huffman@29985
   297
    and "pderiv p = e * d"
huffman@29985
   298
    and "d = r * p + s * pderiv p"
huffman@29985
   299
  shows "rsquarefree q & (\<forall>a. (poly q a = 0) = (poly p a = 0))"
huffman@29985
   300
proof -
wenzelm@60500
   301
  from \<open>pderiv p \<noteq> 0\<close> have "p \<noteq> 0" by auto
wenzelm@60500
   302
  with \<open>p = q * d\<close> have "q \<noteq> 0" by simp
huffman@29985
   303
  have "\<forall>a. order a q = (if order a p = 0 then 0 else 1)"
huffman@29985
   304
    using assms by (rule poly_squarefree_decomp_order2)
wenzelm@60500
   305
  with \<open>p \<noteq> 0\<close> \<open>q \<noteq> 0\<close> show ?thesis
huffman@29985
   306
    by (simp add: rsquarefree_def order_root)
huffman@29985
   307
qed
huffman@29985
   308
huffman@29985
   309
end