src/HOL/Library/Poly_Deriv.thy
author haftmann
Sat Jun 15 17:19:23 2013 +0200 (2013-06-15)
changeset 52380 3cc46b8cca5e
parent 47108 2a1953f0d20d
child 56181 2aa0b19e74f3
permissions -rw-r--r--
lifting for primitive definitions;
explicit conversions from and to lists of coefficients, used for generated code;
replaced recursion operator poly_rec by fold_coeffs, preferring function definitions for non-trivial recursions;
prefer pre-existing gcd operation for gcd
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
huffman@29985
     6
header{* Polynomials and Differentiation *}
huffman@29985
     7
huffman@29985
     8
theory Poly_Deriv
huffman@29985
     9
imports Deriv Polynomial
huffman@29985
    10
begin
huffman@29985
    11
huffman@29985
    12
subsection {* Derivatives of univariate polynomials *}
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)"
huffman@29985
    31
  apply (induct p arbitrary: n, simp)
huffman@29985
    32
  apply (simp add: pderiv_pCons coeff_pCons algebra_simps split: nat.split)
huffman@29985
    33
  done
huffman@29985
    34
haftmann@52380
    35
primrec pderiv_coeffs :: "'a::comm_monoid_add list \<Rightarrow> 'a list"
haftmann@52380
    36
where
haftmann@52380
    37
  "pderiv_coeffs [] = []"
haftmann@52380
    38
| "pderiv_coeffs (x # xs) = plus_coeffs xs (cCons 0 (pderiv_coeffs xs))"
haftmann@52380
    39
haftmann@52380
    40
lemma coeffs_pderiv [code abstract]:
haftmann@52380
    41
  "coeffs (pderiv p) = pderiv_coeffs (coeffs p)"
haftmann@52380
    42
  by (rule sym, induct p) (simp_all add: pderiv_pCons coeffs_plus_eq_plus_coeffs cCons_def)
haftmann@52380
    43
huffman@29985
    44
lemma pderiv_eq_0_iff: "pderiv p = 0 \<longleftrightarrow> degree p = 0"
huffman@29985
    45
  apply (rule iffI)
huffman@29985
    46
  apply (cases p, simp)
haftmann@52380
    47
  apply (simp add: poly_eq_iff coeff_pderiv del: of_nat_Suc)
haftmann@52380
    48
  apply (simp add: poly_eq_iff coeff_pderiv coeff_eq_0)
huffman@29985
    49
  done
huffman@29985
    50
huffman@29985
    51
lemma degree_pderiv: "degree (pderiv p) = degree p - 1"
huffman@29985
    52
  apply (rule order_antisym [OF degree_le])
huffman@29985
    53
  apply (simp add: coeff_pderiv coeff_eq_0)
huffman@29985
    54
  apply (cases "degree p", simp)
huffman@29985
    55
  apply (rule le_degree)
huffman@29985
    56
  apply (simp add: coeff_pderiv del: of_nat_Suc)
huffman@29985
    57
  apply (rule subst, assumption)
huffman@29985
    58
  apply (rule leading_coeff_neq_0, clarsimp)
huffman@29985
    59
  done
huffman@29985
    60
huffman@29985
    61
lemma pderiv_singleton [simp]: "pderiv [:a:] = 0"
huffman@29985
    62
by (simp add: pderiv_pCons)
huffman@29985
    63
huffman@29985
    64
lemma pderiv_add: "pderiv (p + q) = pderiv p + pderiv q"
haftmann@52380
    65
by (rule poly_eqI, simp add: coeff_pderiv algebra_simps)
huffman@29985
    66
huffman@29985
    67
lemma pderiv_minus: "pderiv (- p) = - pderiv p"
haftmann@52380
    68
by (rule poly_eqI, simp add: coeff_pderiv)
huffman@29985
    69
huffman@29985
    70
lemma pderiv_diff: "pderiv (p - q) = pderiv p - pderiv q"
haftmann@52380
    71
by (rule poly_eqI, simp add: coeff_pderiv algebra_simps)
huffman@29985
    72
huffman@29985
    73
lemma pderiv_smult: "pderiv (smult a p) = smult a (pderiv p)"
haftmann@52380
    74
by (rule poly_eqI, simp add: coeff_pderiv algebra_simps)
huffman@29985
    75
huffman@29985
    76
lemma pderiv_mult: "pderiv (p * q) = p * pderiv q + q * pderiv p"
huffman@29985
    77
apply (induct p)
huffman@29985
    78
apply simp
huffman@29985
    79
apply (simp add: pderiv_add pderiv_smult pderiv_pCons algebra_simps)
huffman@29985
    80
done
huffman@29985
    81
huffman@29985
    82
lemma pderiv_power_Suc:
huffman@29985
    83
  "pderiv (p ^ Suc n) = smult (of_nat (Suc n)) (p ^ n) * pderiv p"
huffman@29985
    84
apply (induct n)
huffman@29985
    85
apply simp
huffman@29985
    86
apply (subst power_Suc)
huffman@29985
    87
apply (subst pderiv_mult)
huffman@29985
    88
apply (erule ssubst)
huffman@47108
    89
apply (simp only: of_nat_Suc smult_add_left smult_1_left)
huffman@47108
    90
apply (simp add: algebra_simps) (* FIXME *)
huffman@29985
    91
done
huffman@29985
    92
haftmann@52380
    93
huffman@29985
    94
lemma DERIV_cmult2: "DERIV f x :> D ==> DERIV (%x. (f x) * c :: real) x :> D * c"
huffman@29985
    95
by (simp add: DERIV_cmult mult_commute [of _ c])
huffman@29985
    96
huffman@29985
    97
lemma DERIV_pow2: "DERIV (%x. x ^ Suc n) x :> real (Suc n) * (x ^ n)"
huffman@44317
    98
by (rule DERIV_cong, rule DERIV_pow, simp)
huffman@29985
    99
declare DERIV_pow2 [simp] DERIV_pow [simp]
huffman@29985
   100
huffman@29985
   101
lemma DERIV_add_const: "DERIV f x :> D ==>  DERIV (%x. a + f x :: 'a::real_normed_field) x :> D"
huffman@44317
   102
by (rule DERIV_cong, rule DERIV_add, auto)
huffman@29985
   103
huffman@29985
   104
lemma poly_DERIV[simp]: "DERIV (%x. poly p x) x :> poly (pderiv p) x"
hoelzl@31881
   105
  by (induct p, auto intro!: DERIV_intros simp add: pderiv_pCons)
huffman@29985
   106
huffman@29985
   107
text{* Consequences of the derivative theorem above*}
huffman@29985
   108
huffman@29985
   109
lemma poly_differentiable[simp]: "(%x. poly p x) differentiable (x::real)"
huffman@29985
   110
apply (simp add: differentiable_def)
huffman@29985
   111
apply (blast intro: poly_DERIV)
huffman@29985
   112
done
huffman@29985
   113
huffman@29985
   114
lemma poly_isCont[simp]: "isCont (%x. poly p x) (x::real)"
huffman@29985
   115
by (rule poly_DERIV [THEN DERIV_isCont])
huffman@29985
   116
huffman@29985
   117
lemma poly_IVT_pos: "[| a < b; poly p (a::real) < 0; 0 < poly p b |]
huffman@29985
   118
      ==> \<exists>x. a < x & x < b & (poly p x = 0)"
huffman@29985
   119
apply (cut_tac f = "%x. poly p x" and a = a and b = b and y = 0 in IVT_objl)
huffman@29985
   120
apply (auto simp add: order_le_less)
huffman@29985
   121
done
huffman@29985
   122
huffman@29985
   123
lemma poly_IVT_neg: "[| (a::real) < b; 0 < poly p a; poly p b < 0 |]
huffman@29985
   124
      ==> \<exists>x. a < x & x < b & (poly p x = 0)"
huffman@29985
   125
by (insert poly_IVT_pos [where p = "- p" ]) simp
huffman@29985
   126
huffman@29985
   127
lemma poly_MVT: "(a::real) < b ==>
huffman@29985
   128
     \<exists>x. a < x & x < b & (poly p b - poly p a = (b - a) * poly (pderiv p) x)"
huffman@29985
   129
apply (drule_tac f = "poly p" in MVT, auto)
huffman@29985
   130
apply (rule_tac x = z in exI)
huffman@29985
   131
apply (auto simp add: real_mult_left_cancel poly_DERIV [THEN DERIV_unique])
huffman@29985
   132
done
huffman@29985
   133
huffman@29985
   134
text{*Lemmas for Derivatives*}
huffman@29985
   135
huffman@29985
   136
lemma order_unique_lemma:
huffman@29985
   137
  fixes p :: "'a::idom poly"
huffman@29985
   138
  assumes "[:-a, 1:] ^ n dvd p \<and> \<not> [:-a, 1:] ^ Suc n dvd p"
huffman@29985
   139
  shows "n = order a p"
huffman@29985
   140
unfolding Polynomial.order_def
huffman@29985
   141
apply (rule Least_equality [symmetric])
huffman@29985
   142
apply (rule assms [THEN conjunct2])
huffman@29985
   143
apply (erule contrapos_np)
huffman@29985
   144
apply (rule power_le_dvd)
huffman@29985
   145
apply (rule assms [THEN conjunct1])
huffman@29985
   146
apply simp
huffman@29985
   147
done
huffman@29985
   148
huffman@29985
   149
lemma lemma_order_pderiv1:
huffman@29985
   150
  "pderiv ([:- a, 1:] ^ Suc n * q) = [:- a, 1:] ^ Suc n * pderiv q +
huffman@29985
   151
    smult (of_nat (Suc n)) (q * [:- a, 1:] ^ n)"
huffman@29985
   152
apply (simp only: pderiv_mult pderiv_power_Suc)
huffman@30273
   153
apply (simp del: power_Suc of_nat_Suc add: pderiv_pCons)
huffman@29985
   154
done
huffman@29985
   155
huffman@29985
   156
lemma dvd_add_cancel1:
huffman@29985
   157
  fixes a b c :: "'a::comm_ring_1"
huffman@29985
   158
  shows "a dvd b + c \<Longrightarrow> a dvd b \<Longrightarrow> a dvd c"
haftmann@35050
   159
  by (drule (1) Rings.dvd_diff, simp)
huffman@29985
   160
huffman@29985
   161
lemma lemma_order_pderiv [rule_format]:
huffman@29985
   162
     "\<forall>p q a. 0 < n &
huffman@29985
   163
       pderiv p \<noteq> 0 &
huffman@29985
   164
       p = [:- a, 1:] ^ n * q & ~ [:- a, 1:] dvd q
huffman@29985
   165
       --> n = Suc (order a (pderiv p))"
huffman@29985
   166
 apply (cases "n", safe, rename_tac n p q a)
huffman@29985
   167
 apply (rule order_unique_lemma)
huffman@29985
   168
 apply (rule conjI)
huffman@29985
   169
  apply (subst lemma_order_pderiv1)
huffman@29985
   170
  apply (rule dvd_add)
huffman@29985
   171
   apply (rule dvd_mult2)
huffman@29985
   172
   apply (rule le_imp_power_dvd, simp)
huffman@29985
   173
  apply (rule dvd_smult)
huffman@29985
   174
  apply (rule dvd_mult)
huffman@29985
   175
  apply (rule dvd_refl)
huffman@29985
   176
 apply (subst lemma_order_pderiv1)
huffman@29985
   177
 apply (erule contrapos_nn) back
huffman@29985
   178
 apply (subgoal_tac "[:- a, 1:] ^ Suc n dvd q * [:- a, 1:] ^ n")
huffman@29985
   179
  apply (simp del: mult_pCons_left)
huffman@29985
   180
 apply (drule dvd_add_cancel1)
huffman@29985
   181
  apply (simp del: mult_pCons_left)
huffman@29985
   182
 apply (drule dvd_smult_cancel, simp del: of_nat_Suc)
huffman@29985
   183
 apply assumption
huffman@29985
   184
done
huffman@29985
   185
huffman@29985
   186
lemma order_decomp:
huffman@29985
   187
     "p \<noteq> 0
huffman@29985
   188
      ==> \<exists>q. p = [:-a, 1:] ^ (order a p) * q &
huffman@29985
   189
                ~([:-a, 1:] dvd q)"
huffman@29985
   190
apply (drule order [where a=a])
huffman@29985
   191
apply (erule conjE)
huffman@29985
   192
apply (erule dvdE)
huffman@29985
   193
apply (rule exI)
huffman@29985
   194
apply (rule conjI, assumption)
huffman@29985
   195
apply (erule contrapos_nn)
huffman@29985
   196
apply (erule ssubst) back
huffman@29985
   197
apply (subst power_Suc2)
huffman@29985
   198
apply (erule mult_dvd_mono [OF dvd_refl])
huffman@29985
   199
done
huffman@29985
   200
huffman@29985
   201
lemma order_pderiv: "[| pderiv p \<noteq> 0; order a p \<noteq> 0 |]
huffman@29985
   202
      ==> (order a p = Suc (order a (pderiv p)))"
huffman@29985
   203
apply (case_tac "p = 0", simp)
huffman@29985
   204
apply (drule_tac a = a and p = p in order_decomp)
huffman@29985
   205
using neq0_conv
huffman@29985
   206
apply (blast intro: lemma_order_pderiv)
huffman@29985
   207
done
huffman@29985
   208
huffman@29985
   209
lemma order_mult: "p * q \<noteq> 0 \<Longrightarrow> order a (p * q) = order a p + order a q"
huffman@29985
   210
proof -
huffman@29985
   211
  def i \<equiv> "order a p"
huffman@29985
   212
  def j \<equiv> "order a q"
huffman@29985
   213
  def t \<equiv> "[:-a, 1:]"
huffman@29985
   214
  have t_dvd_iff: "\<And>u. t dvd u \<longleftrightarrow> poly u a = 0"
huffman@29985
   215
    unfolding t_def by (simp add: dvd_iff_poly_eq_0)
huffman@29985
   216
  assume "p * q \<noteq> 0"
huffman@29985
   217
  then show "order a (p * q) = i + j"
huffman@29985
   218
    apply clarsimp
huffman@29985
   219
    apply (drule order [where a=a and p=p, folded i_def t_def])
huffman@29985
   220
    apply (drule order [where a=a and p=q, folded j_def t_def])
huffman@29985
   221
    apply clarify
huffman@29985
   222
    apply (rule order_unique_lemma [symmetric], fold t_def)
huffman@29985
   223
    apply (erule dvdE)+
huffman@29985
   224
    apply (simp add: power_add t_dvd_iff)
huffman@29985
   225
    done
huffman@29985
   226
qed
huffman@29985
   227
huffman@29985
   228
text{*Now justify the standard squarefree decomposition, i.e. f / gcd(f,f'). *}
huffman@29985
   229
huffman@29985
   230
lemma order_divides: "[:-a, 1:] ^ n dvd p \<longleftrightarrow> p = 0 \<or> n \<le> order a p"
huffman@29985
   231
apply (cases "p = 0", auto)
huffman@29985
   232
apply (drule order_2 [where a=a and p=p])
huffman@29985
   233
apply (erule contrapos_np)
huffman@29985
   234
apply (erule power_le_dvd)
huffman@29985
   235
apply simp
huffman@29985
   236
apply (erule power_le_dvd [OF order_1])
huffman@29985
   237
done
huffman@29985
   238
huffman@29985
   239
lemma poly_squarefree_decomp_order:
huffman@29985
   240
  assumes "pderiv p \<noteq> 0"
huffman@29985
   241
  and p: "p = q * d"
huffman@29985
   242
  and p': "pderiv p = e * d"
huffman@29985
   243
  and d: "d = r * p + s * pderiv p"
huffman@29985
   244
  shows "order a q = (if order a p = 0 then 0 else 1)"
huffman@29985
   245
proof (rule classical)
huffman@29985
   246
  assume 1: "order a q \<noteq> (if order a p = 0 then 0 else 1)"
huffman@29985
   247
  from `pderiv p \<noteq> 0` have "p \<noteq> 0" by auto
huffman@29985
   248
  with p have "order a p = order a q + order a d"
huffman@29985
   249
    by (simp add: order_mult)
huffman@29985
   250
  with 1 have "order a p \<noteq> 0" by (auto split: if_splits)
huffman@29985
   251
  have "order a (pderiv p) = order a e + order a d"
huffman@29985
   252
    using `pderiv p \<noteq> 0` `pderiv p = e * d` by (simp add: order_mult)
huffman@29985
   253
  have "order a p = Suc (order a (pderiv p))"
huffman@29985
   254
    using `pderiv p \<noteq> 0` `order a p \<noteq> 0` by (rule order_pderiv)
huffman@29985
   255
  have "d \<noteq> 0" using `p \<noteq> 0` `p = q * d` by simp
huffman@29985
   256
  have "([:-a, 1:] ^ (order a (pderiv p))) dvd d"
huffman@29985
   257
    apply (simp add: d)
huffman@29985
   258
    apply (rule dvd_add)
huffman@29985
   259
    apply (rule dvd_mult)
huffman@29985
   260
    apply (simp add: order_divides `p \<noteq> 0`
huffman@29985
   261
           `order a p = Suc (order a (pderiv p))`)
huffman@29985
   262
    apply (rule dvd_mult)
huffman@29985
   263
    apply (simp add: order_divides)
huffman@29985
   264
    done
huffman@29985
   265
  then have "order a (pderiv p) \<le> order a d"
huffman@29985
   266
    using `d \<noteq> 0` by (simp add: order_divides)
huffman@29985
   267
  show ?thesis
huffman@29985
   268
    using `order a p = order a q + order a d`
huffman@29985
   269
    using `order a (pderiv p) = order a e + order a d`
huffman@29985
   270
    using `order a p = Suc (order a (pderiv p))`
huffman@29985
   271
    using `order a (pderiv p) \<le> order a d`
huffman@29985
   272
    by auto
huffman@29985
   273
qed
huffman@29985
   274
huffman@29985
   275
lemma poly_squarefree_decomp_order2: "[| pderiv p \<noteq> 0;
huffman@29985
   276
         p = q * d;
huffman@29985
   277
         pderiv p = e * d;
huffman@29985
   278
         d = r * p + s * pderiv p
huffman@29985
   279
      |] ==> \<forall>a. order a q = (if order a p = 0 then 0 else 1)"
huffman@29985
   280
apply (blast intro: poly_squarefree_decomp_order)
huffman@29985
   281
done
huffman@29985
   282
huffman@29985
   283
lemma order_pderiv2: "[| pderiv p \<noteq> 0; order a p \<noteq> 0 |]
huffman@29985
   284
      ==> (order a (pderiv p) = n) = (order a p = Suc n)"
huffman@29985
   285
apply (auto dest: order_pderiv)
huffman@29985
   286
done
huffman@29985
   287
huffman@29985
   288
definition
huffman@29985
   289
  rsquarefree :: "'a::idom poly => bool" where
huffman@29985
   290
  "rsquarefree p = (p \<noteq> 0 & (\<forall>a. (order a p = 0) | (order a p = 1)))"
huffman@29985
   291
huffman@29985
   292
lemma pderiv_iszero: "pderiv p = 0 \<Longrightarrow> \<exists>h. p = [:h:]"
huffman@29985
   293
apply (simp add: pderiv_eq_0_iff)
huffman@29985
   294
apply (case_tac p, auto split: if_splits)
huffman@29985
   295
done
huffman@29985
   296
huffman@29985
   297
lemma rsquarefree_roots:
huffman@29985
   298
  "rsquarefree p = (\<forall>a. ~(poly p a = 0 & poly (pderiv p) a = 0))"
huffman@29985
   299
apply (simp add: rsquarefree_def)
huffman@29985
   300
apply (case_tac "p = 0", simp, simp)
huffman@29985
   301
apply (case_tac "pderiv p = 0")
huffman@29985
   302
apply simp
huffman@29985
   303
apply (drule pderiv_iszero, clarify)
huffman@29985
   304
apply simp
huffman@29985
   305
apply (rule allI)
huffman@29985
   306
apply (cut_tac p = "[:h:]" and a = a in order_root)
huffman@29985
   307
apply simp
huffman@29985
   308
apply (auto simp add: order_root order_pderiv2)
huffman@29985
   309
apply (erule_tac x="a" in allE, simp)
huffman@29985
   310
done
huffman@29985
   311
huffman@29985
   312
lemma poly_squarefree_decomp:
huffman@29985
   313
  assumes "pderiv p \<noteq> 0"
huffman@29985
   314
    and "p = q * d"
huffman@29985
   315
    and "pderiv p = e * d"
huffman@29985
   316
    and "d = r * p + s * pderiv p"
huffman@29985
   317
  shows "rsquarefree q & (\<forall>a. (poly q a = 0) = (poly p a = 0))"
huffman@29985
   318
proof -
huffman@29985
   319
  from `pderiv p \<noteq> 0` have "p \<noteq> 0" by auto
huffman@29985
   320
  with `p = q * d` have "q \<noteq> 0" by simp
huffman@29985
   321
  have "\<forall>a. order a q = (if order a p = 0 then 0 else 1)"
huffman@29985
   322
    using assms by (rule poly_squarefree_decomp_order2)
huffman@29985
   323
  with `p \<noteq> 0` `q \<noteq> 0` show ?thesis
huffman@29985
   324
    by (simp add: rsquarefree_def order_root)
huffman@29985
   325
qed
huffman@29985
   326
huffman@29985
   327
end