1 (* Title: HOL/Library/Univ_Poly.thy
5 header {* Univariate Polynomials *}
11 text{* Application of polynomial as a function. *}
13 primrec (in semiring_0) poly :: "'a list \<Rightarrow> 'a \<Rightarrow> 'a"
15 poly_Nil: "poly [] x = 0"
16 | poly_Cons: "poly (h#t) x = h + x * poly t x"
19 subsection{*Arithmetic Operations on Polynomials*}
23 primrec (in semiring_0) padd :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" (infixl "+++" 65)
25 padd_Nil: "[] +++ l2 = l2"
26 | padd_Cons: "(h#t) +++ l2 = (if l2 = [] then h#t else (h + hd l2)#(t +++ tl l2))"
28 text{*Multiplication by a constant*}
29 primrec (in semiring_0) cmult :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" (infixl "%*" 70) where
30 cmult_Nil: "c %* [] = []"
31 | cmult_Cons: "c %* (h#t) = (c * h)#(c %* t)"
33 text{*Multiplication by a polynomial*}
34 primrec (in semiring_0) pmult :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" (infixl "***" 70)
36 pmult_Nil: "[] *** l2 = []"
37 | pmult_Cons: "(h#t) *** l2 = (if t = [] then h %* l2
38 else (h %* l2) +++ ((0) # (t *** l2)))"
40 text{*Repeated multiplication by a polynomial*}
41 primrec (in semiring_0) mulexp :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" where
42 mulexp_zero: "mulexp 0 p q = q"
43 | mulexp_Suc: "mulexp (Suc n) p q = p *** mulexp n p q"
46 primrec (in semiring_1) pexp :: "'a list \<Rightarrow> nat \<Rightarrow> 'a list" (infixl "%^" 80) where
47 pexp_0: "p %^ 0 = [1]"
48 | pexp_Suc: "p %^ (Suc n) = p *** (p %^ n)"
50 text{*Quotient related value of dividing a polynomial by x + a*}
51 (* Useful for divisor properties in inductive proofs *)
52 primrec (in field) "pquot" :: "'a list \<Rightarrow> 'a \<Rightarrow> 'a list"
54 pquot_Nil: "pquot [] a= []"
55 | pquot_Cons: "pquot (h#t) a =
56 (if t = [] then [h] else (inverse(a) * (h - hd( pquot t a)))#(pquot t a))"
58 text{*normalization of polynomials (remove extra 0 coeff)*}
59 primrec (in semiring_0) pnormalize :: "'a list \<Rightarrow> 'a list" where
60 pnormalize_Nil: "pnormalize [] = []"
61 | pnormalize_Cons: "pnormalize (h#p) =
62 (if pnormalize p = [] then (if h = 0 then [] else [h]) else h # pnormalize p)"
64 definition (in semiring_0) "pnormal p = ((pnormalize p = p) \<and> p \<noteq> [])"
65 definition (in semiring_0) "nonconstant p = (pnormal p \<and> (\<forall>x. p \<noteq> [x]))"
66 text{*Other definitions*}
68 definition (in ring_1) poly_minus :: "'a list \<Rightarrow> 'a list" ("-- _" [80] 80)
69 where "-- p = (- 1) %* p"
71 definition (in semiring_0) divides :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" (infixl "divides" 70)
72 where "p1 divides p2 = (\<exists>q. poly p2 = poly(p1 *** q))"
74 --{*order of a polynomial*}
75 definition (in ring_1) order :: "'a \<Rightarrow> 'a list \<Rightarrow> nat" where
76 "order a p = (SOME n. ([-a, 1] %^ n) divides p \<and> ~ (([-a, 1] %^ (Suc n)) divides p))"
78 --{*degree of a polynomial*}
79 definition (in semiring_0) degree :: "'a list \<Rightarrow> nat"
80 where "degree p = length (pnormalize p) - 1"
82 --{*squarefree polynomials --- NB with respect to real roots only.*}
83 definition (in ring_1) rsquarefree :: "'a list \<Rightarrow> bool"
84 where "rsquarefree p \<longleftrightarrow> poly p \<noteq> poly [] \<and> (\<forall>a. order a p = 0 \<or> order a p = 1)"
89 lemma padd_Nil2[simp]: "p +++ [] = p"
92 lemma padd_Cons_Cons: "(h1 # p1) +++ (h2 # p2) = (h1 + h2) # (p1 +++ p2)"
95 lemma pminus_Nil: "-- [] = []"
96 by (simp add: poly_minus_def)
98 lemma pmult_singleton: "[h1] *** p1 = h1 %* p1" by simp
102 lemma (in semiring_1) poly_ident_mult[simp]: "1 %* t = t" by (induct t) auto
104 lemma (in semiring_0) poly_simple_add_Cons[simp]: "[a] +++ ((0)#t) = (a#t)"
107 text{*Handy general properties*}
109 lemma (in comm_semiring_0) padd_commut: "b +++ a = a +++ b"
110 proof (induct b arbitrary: a)
115 thus ?case by (cases a) (simp_all add: add_commute)
118 lemma (in comm_semiring_0) padd_assoc: "\<forall>b c. (a +++ b) +++ c = a +++ (b +++ c)"
120 apply (simp, clarify)
121 apply (case_tac b, simp_all add: add_ac)
124 lemma (in semiring_0) poly_cmult_distr: "a %* ( p +++ q) = (a %* p +++ a %* q)"
125 apply (induct p arbitrary: q)
127 apply (case_tac q, simp_all add: distrib_left)
130 lemma (in ring_1) pmult_by_x[simp]: "[0, 1] *** t = ((0)#t)"
133 apply (auto simp add: padd_commut)
134 apply (case_tac t, auto)
137 text{*properties of evaluation of polynomials.*}
139 lemma (in semiring_0) poly_add: "poly (p1 +++ p2) x = poly p1 x + poly p2 x"
140 proof(induct p1 arbitrary: p2)
146 by (cases p2) (simp_all add: add_ac distrib_left)
149 lemma (in comm_semiring_0) poly_cmult: "poly (c %* p) x = c * poly p x"
151 apply (case_tac [2] "x = zero")
152 apply (auto simp add: distrib_left mult_ac)
155 lemma (in comm_semiring_0) poly_cmult_map: "poly (map (op * c) p) x = c*poly p x"
156 by (induct p) (auto simp add: distrib_left mult_ac)
158 lemma (in comm_ring_1) poly_minus: "poly (-- p) x = - (poly p x)"
159 apply (simp add: poly_minus_def)
160 apply (auto simp add: poly_cmult)
163 lemma (in comm_semiring_0) poly_mult: "poly (p1 *** p2) x = poly p1 x * poly p2 x"
164 proof (induct p1 arbitrary: p2)
169 thus ?case by (cases as)
170 (simp_all add: poly_cmult poly_add distrib_right distrib_left mult_ac)
173 class idom_char_0 = idom + ring_char_0
175 lemma (in comm_ring_1) poly_exp: "poly (p %^ n) x = (poly p x) ^ n"
176 by (induct n) (auto simp add: poly_cmult poly_mult)
178 text{*More Polynomial Evaluation Lemmas*}
180 lemma (in semiring_0) poly_add_rzero[simp]: "poly (a +++ []) x = poly a x"
183 lemma (in comm_semiring_0) poly_mult_assoc: "poly ((a *** b) *** c) x = poly (a *** (b *** c)) x"
184 by (simp add: poly_mult mult_assoc)
186 lemma (in semiring_0) poly_mult_Nil2[simp]: "poly (p *** []) x = 0"
189 lemma (in comm_semiring_1) poly_exp_add: "poly (p %^ (n + d)) x = poly( p %^ n *** p %^ d) x"
190 by (induct n) (auto simp add: poly_mult mult_assoc)
192 subsection{*Key Property: if @{term "f(a) = 0"} then @{term "(x - a)"} divides
195 lemma (in comm_ring_1) lemma_poly_linear_rem: "\<forall>h. \<exists>q r. h#t = [r] +++ [-a, 1] *** q"
198 { fix h have "[h] = [h] +++ [- a, 1] *** []" by simp }
203 from Cons.hyps[rule_format, of x]
204 obtain q r where qr: "x#xs = [r] +++ [- a, 1] *** q" by blast
205 have "h#x#xs = [a*r + h] +++ [-a, 1] *** (r#q)"
206 using qr by (cases q) (simp_all add: algebra_simps)
207 hence "\<exists>q r. h#x#xs = [r] +++ [-a, 1] *** q" by blast}
211 lemma (in comm_ring_1) poly_linear_rem: "\<exists>q r. h#t = [r] +++ [-a, 1] *** q"
212 using lemma_poly_linear_rem [where t = t and a = a] by auto
215 lemma (in comm_ring_1) poly_linear_divides: "(poly p a = 0) = ((p = []) | (\<exists>q. p = [-a, 1] *** q))"
217 { assume p: "p = []" hence ?thesis by simp }
220 fix x xs assume p: "p = x#xs"
222 fix q assume "p = [-a, 1] *** q"
223 hence "poly p a = 0" by (simp add: poly_add poly_cmult)
226 { assume p0: "poly p a = 0"
227 from poly_linear_rem[of x xs a] obtain q r
228 where qr: "x#xs = [r] +++ [- a, 1] *** q" by blast
229 have "r = 0" using p0 by (simp only: p qr poly_mult poly_add) simp
230 hence "\<exists>q. p = [- a, 1] *** q"
233 apply (rule exI[where x=q])
239 ultimately have ?thesis using p by blast
241 ultimately show ?thesis by (cases p) auto
244 lemma (in semiring_0) lemma_poly_length_mult[simp]: "\<forall>h k a. length (k %* p +++ (h # (a %* p))) = Suc (length p)"
247 lemma (in semiring_0) lemma_poly_length_mult2[simp]: "\<forall>h k. length (k %* p +++ (h # p)) = Suc (length p)"
250 lemma (in ring_1) poly_length_mult[simp]: "length([-a,1] *** q) = Suc (length q)"
253 subsection{*Polynomial length*}
255 lemma (in semiring_0) poly_cmult_length[simp]: "length (a %* p) = length p"
258 lemma (in semiring_0) poly_add_length: "length (p1 +++ p2) = max (length p1) (length p2)"
259 by (induct p1 arbitrary: p2) (simp_all, arith)
261 lemma (in semiring_0) poly_root_mult_length[simp]: "length([a,b] *** p) = Suc (length p)"
262 by (simp add: poly_add_length)
264 lemma (in idom) poly_mult_not_eq_poly_Nil[simp]:
265 "poly (p *** q) x \<noteq> poly [] x \<longleftrightarrow> poly p x \<noteq> poly [] x \<and> poly q x \<noteq> poly [] x"
266 by (auto simp add: poly_mult)
268 lemma (in idom) poly_mult_eq_zero_disj: "poly (p *** q) x = 0 \<longleftrightarrow> poly p x = 0 \<or> poly q x = 0"
269 by (auto simp add: poly_mult)
271 text{*Normalisation Properties*}
273 lemma (in semiring_0) poly_normalized_nil: "(pnormalize p = []) --> (poly p x = 0)"
276 text{*A nontrivial polynomial of degree n has no more than n roots*}
277 lemma (in idom) poly_roots_index_lemma:
278 assumes p: "poly p x \<noteq> poly [] x" and n: "length p = n"
279 shows "\<exists>i. \<forall>x. poly p x = 0 \<longrightarrow> (\<exists>m\<le>n. x = i m)"
281 proof (induct n arbitrary: p x)
287 assume C: "\<And>i. \<exists>x. poly p x = 0 \<and> (\<forall>m\<le>Suc n. x \<noteq> i m)"
288 from Suc.prems have p0: "poly p x \<noteq> 0" "p\<noteq> []" by auto
289 from p0(1)[unfolded poly_linear_divides[of p x]]
290 have "\<forall>q. p \<noteq> [- x, 1] *** q" by blast
291 from C obtain a where a: "poly p a = 0" by blast
292 from a[unfolded poly_linear_divides[of p a]] p0(2)
293 obtain q where q: "p = [-a, 1] *** q" by blast
294 have lg: "length q = n" using q Suc.prems(2) by simp
295 from q p0 have qx: "poly q x \<noteq> poly [] x"
296 by (auto simp add: poly_mult poly_add poly_cmult)
297 from Suc.hyps[OF qx lg] obtain i where
298 i: "\<forall>x. poly q x = 0 \<longrightarrow> (\<exists>m\<le>n. x = i m)" by blast
299 let ?i = "\<lambda>m. if m = Suc n then a else i m"
300 from C[of ?i] obtain y where y: "poly p y = 0" "\<forall>m\<le> Suc n. y \<noteq> ?i m"
302 from y have "y = a \<or> poly q y = 0"
303 by (simp only: q poly_mult_eq_zero_disj poly_add) (simp add: algebra_simps)
304 with i[rule_format, of y] y(1) y(2) have False
306 apply (erule_tac x = "m" in allE)
314 lemma (in idom) poly_roots_index_length:
315 "poly p x \<noteq> poly [] x \<Longrightarrow> \<exists>i. \<forall>x. (poly p x = 0) \<longrightarrow> (\<exists>n. n \<le> length p \<and> x = i n)"
316 by (blast intro: poly_roots_index_lemma)
318 lemma (in idom) poly_roots_finite_lemma1:
319 "poly p x \<noteq> poly [] x \<Longrightarrow> \<exists>N i. \<forall>x. (poly p x = 0) \<longrightarrow> (\<exists>n. (n::nat) < N \<and> x = i n)"
320 apply (drule poly_roots_index_length, safe)
321 apply (rule_tac x = "Suc (length p)" in exI)
322 apply (rule_tac x = i in exI)
323 apply (simp add: less_Suc_eq_le)
326 lemma (in idom) idom_finite_lemma:
327 assumes P: "\<forall>x. P x --> (\<exists>n. n < length j \<and> x = j!n)"
328 shows "finite {x. P x}"
332 have "?M \<subseteq> ?N" using P by auto
333 thus ?thesis using finite_subset by auto
336 lemma (in idom) poly_roots_finite_lemma2:
337 "poly p x \<noteq> poly [] x \<Longrightarrow> \<exists>i. \<forall>x. poly p x = 0 \<longrightarrow> x \<in> set i"
338 apply (drule poly_roots_index_length, safe)
339 apply (rule_tac x="map (\<lambda>n. i n) [0 ..< Suc (length p)]" in exI)
340 apply (auto simp add: image_iff)
341 apply (erule_tac x="x" in allE, clarsimp)
342 apply (case_tac "n = length p")
343 apply (auto simp add: order_le_less)
346 lemma (in ring_char_0) UNIV_ring_char_0_infinte: "\<not> (finite (UNIV:: 'a set))"
348 assume F: "finite (UNIV :: 'a set)"
349 have "finite (UNIV :: nat set)"
350 proof (rule finite_imageD)
351 have "of_nat ` UNIV \<subseteq> UNIV" by simp
352 then show "finite (of_nat ` UNIV :: 'a set)" using F by (rule finite_subset)
353 show "inj (of_nat :: nat \<Rightarrow> 'a)" by (simp add: inj_on_def)
355 with infinite_UNIV_nat show False ..
358 lemma (in idom_char_0) poly_roots_finite: "poly p \<noteq> poly [] \<longleftrightarrow> finite {x. poly p x = 0}"
360 assume H: "poly p \<noteq> poly []"
361 show "finite {x. poly p x = (0::'a)}"
364 apply (erule contrapos_np, rule ext)
366 apply (clarify dest!: poly_roots_finite_lemma2)
370 assume F: "\<not> finite {x. poly p x = (0\<Colon>'a)}"
371 and P: "\<forall>x. poly p x = (0\<Colon>'a) \<longrightarrow> x \<in> set i"
372 let ?M= "{x. poly p x = (0\<Colon>'a)}"
373 from P have "?M \<subseteq> set i" by auto
374 with finite_subset F show False by auto
377 assume F: "finite {x. poly p x = (0\<Colon>'a)}"
378 show "poly p \<noteq> poly []" using F UNIV_ring_char_0_infinte by auto
381 text{*Entirety and Cancellation for polynomials*}
383 lemma (in idom_char_0) poly_entire_lemma2:
384 assumes p0: "poly p \<noteq> poly []"
385 and q0: "poly q \<noteq> poly []"
386 shows "poly (p***q) \<noteq> poly []"
388 let ?S = "\<lambda>p. {x. poly p x = 0}"
389 have "?S (p *** q) = ?S p \<union> ?S q" by (auto simp add: poly_mult)
390 with p0 q0 show ?thesis unfolding poly_roots_finite by auto
393 lemma (in idom_char_0) poly_entire:
394 "poly (p *** q) = poly [] \<longleftrightarrow> poly p = poly [] \<or> poly q = poly []"
395 using poly_entire_lemma2[of p q]
396 by (auto simp add: fun_eq_iff poly_mult)
398 lemma (in idom_char_0) poly_entire_neg:
399 "poly (p *** q) \<noteq> poly [] \<longleftrightarrow> poly p \<noteq> poly [] \<and> poly q \<noteq> poly []"
400 by (simp add: poly_entire)
402 lemma fun_eq: "f = g \<longleftrightarrow> (\<forall>x. f x = g x)"
405 lemma (in comm_ring_1) poly_add_minus_zero_iff:
406 "poly (p +++ -- q) = poly [] \<longleftrightarrow> poly p = poly q"
407 by (auto simp add: algebra_simps poly_add poly_minus_def fun_eq poly_cmult)
409 lemma (in comm_ring_1) poly_add_minus_mult_eq:
410 "poly (p *** q +++ --(p *** r)) = poly (p *** (q +++ -- r))"
411 by (auto simp add: poly_add poly_minus_def fun_eq poly_mult poly_cmult distrib_left)
413 subclass (in idom_char_0) comm_ring_1 ..
415 lemma (in idom_char_0) poly_mult_left_cancel:
416 "poly (p *** q) = poly (p *** r) \<longleftrightarrow> poly p = poly [] \<or> poly q = poly r"
418 have "poly (p *** q) = poly (p *** r) \<longleftrightarrow> poly (p *** q +++ -- (p *** r)) = poly []"
419 by (simp only: poly_add_minus_zero_iff)
420 also have "\<dots> \<longleftrightarrow> poly p = poly [] \<or> poly q = poly r"
421 by (auto intro: simp add: poly_add_minus_mult_eq poly_entire poly_add_minus_zero_iff)
422 finally show ?thesis .
425 lemma (in idom) poly_exp_eq_zero[simp]:
426 "poly (p %^ n) = poly [] \<longleftrightarrow> poly p = poly [] \<and> n \<noteq> 0"
427 apply (simp only: fun_eq add: HOL.all_simps [symmetric])
428 apply (rule arg_cong [where f = All])
431 apply (auto simp add: poly_exp poly_mult)
434 lemma (in comm_ring_1) poly_prime_eq_zero[simp]: "poly [a,1] \<noteq> poly []"
435 apply (simp add: fun_eq)
436 apply (rule_tac x = "minus one a" in exI)
437 apply (unfold diff_minus)
438 apply (subst add_commute)
439 apply (subst add_assoc)
443 lemma (in idom) poly_exp_prime_eq_zero: "poly ([a, 1] %^ n) \<noteq> poly []"
446 text{*A more constructive notion of polynomials being trivial*}
448 lemma (in idom_char_0) poly_zero_lemma': "poly (h # t) = poly [] \<Longrightarrow> h = 0 \<and> poly t = poly []"
449 apply (simp add: fun_eq)
450 apply (case_tac "h = zero")
451 apply (drule_tac [2] x = zero in spec, auto)
452 apply (cases "poly t = poly []", simp)
455 assume H: "\<forall>x. x = (0\<Colon>'a) \<or> poly t x = (0\<Colon>'a)"
456 and pnz: "poly t \<noteq> poly []"
457 let ?S = "{x. poly t x = 0}"
458 from H have "\<forall>x. x \<noteq>0 \<longrightarrow> poly t x = 0" by blast
459 hence th: "?S \<supseteq> UNIV - {0}" by auto
460 from poly_roots_finite pnz have th': "finite ?S" by blast
461 from finite_subset[OF th th'] UNIV_ring_char_0_infinte show "poly t x = (0\<Colon>'a)"
465 lemma (in idom_char_0) poly_zero: "(poly p = poly []) = list_all (%c. c = 0) p"
469 apply (drule poly_zero_lemma', auto)
472 lemma (in idom_char_0) poly_0: "list_all (\<lambda>c. c = 0) p \<Longrightarrow> poly p x = 0"
473 unfolding poly_zero[symmetric] by simp
477 text{*Basics of divisibility.*}
479 lemma (in idom) poly_primes:
480 "[a, 1] divides (p *** q) \<longleftrightarrow> [a, 1] divides p \<or> [a, 1] divides q"
481 apply (auto simp add: divides_def fun_eq poly_mult poly_add poly_cmult distrib_right [symmetric])
482 apply (drule_tac x = "uminus a" in spec)
483 apply (simp add: poly_linear_divides poly_add poly_cmult distrib_right [symmetric])
484 apply (cases "p = []")
485 apply (rule exI[where x="[]"])
487 apply (cases "q = []")
488 apply (erule allE[where x="[]"], simp)
491 apply (cases "\<exists>q\<Colon>'a list. p = a %* q +++ ((0\<Colon>'a) # q)")
492 apply (clarsimp simp add: poly_add poly_cmult)
493 apply (rule_tac x="qa" in exI)
494 apply (simp add: distrib_right [symmetric])
497 apply (auto simp add: poly_linear_divides poly_add poly_cmult distrib_right [symmetric])
498 apply (rule_tac x = "pmult qa q" in exI)
499 apply (rule_tac [2] x = "pmult p qa" in exI)
500 apply (auto simp add: poly_add poly_mult poly_cmult mult_ac)
503 lemma (in comm_semiring_1) poly_divides_refl[simp]: "p divides p"
504 apply (simp add: divides_def)
505 apply (rule_tac x = "[one]" in exI)
506 apply (auto simp add: poly_mult fun_eq)
509 lemma (in comm_semiring_1) poly_divides_trans: "p divides q \<Longrightarrow> q divides r \<Longrightarrow> p divides r"
510 apply (simp add: divides_def, safe)
511 apply (rule_tac x = "pmult qa qaa" in exI)
512 apply (auto simp add: poly_mult fun_eq mult_assoc)
515 lemma (in comm_semiring_1) poly_divides_exp: "m \<le> n \<Longrightarrow> (p %^ m) divides (p %^ n)"
516 apply (auto simp add: le_iff_add)
518 apply (rule_tac [2] poly_divides_trans)
519 apply (auto simp add: divides_def)
520 apply (rule_tac x = p in exI)
521 apply (auto simp add: poly_mult fun_eq mult_ac)
524 lemma (in comm_semiring_1) poly_exp_divides:
525 "(p %^ n) divides q \<Longrightarrow> m \<le> n \<Longrightarrow> (p %^ m) divides q"
526 by (blast intro: poly_divides_exp poly_divides_trans)
528 lemma (in comm_semiring_0) poly_divides_add:
529 "p divides q \<Longrightarrow> p divides r \<Longrightarrow> p divides (q +++ r)"
530 apply (simp add: divides_def, auto)
531 apply (rule_tac x = "padd qa qaa" in exI)
532 apply (auto simp add: poly_add fun_eq poly_mult distrib_left)
535 lemma (in comm_ring_1) poly_divides_diff:
536 "p divides q \<Longrightarrow> p divides (q +++ r) \<Longrightarrow> p divides r"
537 apply (simp add: divides_def, auto)
538 apply (rule_tac x = "padd qaa (poly_minus qa)" in exI)
539 apply (auto simp add: poly_add fun_eq poly_mult poly_minus algebra_simps)
542 lemma (in comm_ring_1) poly_divides_diff2:
543 "p divides r \<Longrightarrow> p divides (q +++ r) \<Longrightarrow> p divides q"
544 apply (erule poly_divides_diff)
545 apply (auto simp add: poly_add fun_eq poly_mult divides_def add_ac)
548 lemma (in semiring_0) poly_divides_zero: "poly p = poly [] \<Longrightarrow> q divides p"
549 apply (simp add: divides_def)
550 apply (rule exI[where x="[]"])
551 apply (auto simp add: fun_eq poly_mult)
554 lemma (in semiring_0) poly_divides_zero2 [simp]: "q divides []"
555 apply (simp add: divides_def)
556 apply (rule_tac x = "[]" in exI)
557 apply (auto simp add: fun_eq)
560 text{*At last, we can consider the order of a root.*}
562 lemma (in idom_char_0) poly_order_exists_lemma:
563 assumes lp: "length p = d"
564 and p: "poly p \<noteq> poly []"
565 shows "\<exists>n q. p = mulexp n [-a, 1] q \<and> poly q a \<noteq> 0"
567 proof (induct d arbitrary: p)
573 proof (cases "poly p a = 0")
575 from Suc.prems have h: "length p = Suc n" "poly p \<noteq> poly []" by auto
576 hence pN: "p \<noteq> []" by auto
577 from True[unfolded poly_linear_divides] pN obtain q where q: "p = [-a, 1] *** q"
579 from q h True have qh: "length q = n" "poly q \<noteq> poly []"
582 apply (simp only: fun_eq)
584 apply (simp add: fun_eq poly_add poly_cmult)
586 from Suc.hyps[OF qh] obtain m r where mr: "q = mulexp m [-a,1] r" "poly r a \<noteq> 0"
588 from mr q have "p = mulexp (Suc m) [-a,1] r \<and> poly r a \<noteq> 0" by simp
589 then show ?thesis by blast
595 apply (rule exI[where x="0::nat"])
602 lemma (in comm_semiring_1) poly_mulexp: "poly (mulexp n p q) x = (poly p x) ^ n * poly q x"
603 by (induct n) (auto simp add: poly_mult mult_ac)
605 lemma (in comm_semiring_1) divides_left_mult:
606 assumes d:"(p***q) divides r" shows "p divides r \<and> q divides r"
608 from d obtain t where r:"poly r = poly (p***q *** t)"
609 unfolding divides_def by blast
610 hence "poly r = poly (p *** (q *** t))"
611 "poly r = poly (q *** (p***t))" by(auto simp add: fun_eq poly_mult mult_ac)
612 thus ?thesis unfolding divides_def by blast
618 lemma (in semiring_1) zero_power_iff: "0 ^ n = (if n = 0 then 1 else 0)"
619 by (induct n) simp_all
621 lemma (in idom_char_0) poly_order_exists:
622 assumes "length p = d" and "poly p \<noteq> poly []"
623 shows "\<exists>n. ([-a, 1] %^ n) divides p \<and> ~(([-a, 1] %^ (Suc n)) divides p)"
626 apply (drule poly_order_exists_lemma [where a=a], assumption, clarify)
627 apply (rule_tac x = n in exI, safe)
628 apply (unfold divides_def)
629 apply (rule_tac x = q in exI)
630 apply (induct_tac n, simp)
631 apply (simp (no_asm_simp) add: poly_add poly_cmult poly_mult distrib_left mult_ac)
633 apply (subgoal_tac "poly (mulexp n [uminus a, one] q) \<noteq>
634 poly (pmult (pexp [uminus a, one] (Suc n)) qa)")
637 apply (simp del: pmult_Cons pexp_Suc)
638 apply (erule_tac Q = "poly q a = zero" in contrapos_np)
639 apply (simp add: poly_add poly_cmult)
640 apply (rule pexp_Suc [THEN ssubst])
642 apply (simp add: poly_mult_left_cancel poly_mult_assoc del: pmult_Cons pexp_Suc)
645 lemma (in semiring_1) poly_one_divides[simp]: "[1] divides p"
646 by (auto simp add: divides_def)
648 lemma (in idom_char_0) poly_order:
649 "poly p \<noteq> poly [] \<Longrightarrow> \<exists>!n. ([-a, 1] %^ n) divides p \<and> \<not> (([-a, 1] %^ Suc n) divides p)"
650 apply (auto intro: poly_order_exists simp add: less_linear simp del: pmult_Cons pexp_Suc)
651 apply (cut_tac x = y and y = n in less_linear)
652 apply (drule_tac m = n in poly_exp_divides)
653 apply (auto dest: Suc_le_eq [THEN iffD2, THEN [2] poly_exp_divides]
654 simp del: pmult_Cons pexp_Suc)
659 lemma some1_equalityD: "n = (SOME n. P n) \<Longrightarrow> \<exists>!n. P n \<Longrightarrow> P n"
660 by (blast intro: someI2)
662 lemma (in idom_char_0) order:
663 "(([-a, 1] %^ n) divides p \<and>
664 ~(([-a, 1] %^ (Suc n)) divides p)) =
665 ((n = order a p) \<and> ~(poly p = poly []))"
666 apply (unfold order_def)
668 apply (blast dest: poly_divides_zero intro!: some1_equality [symmetric] poly_order)
669 apply (blast intro!: poly_order [THEN [2] some1_equalityD])
672 lemma (in idom_char_0) order2:
673 "poly p \<noteq> poly [] \<Longrightarrow>
674 ([-a, 1] %^ (order a p)) divides p \<and> \<not> (([-a, 1] %^ (Suc (order a p))) divides p)"
675 by (simp add: order del: pexp_Suc)
677 lemma (in idom_char_0) order_unique:
678 "poly p \<noteq> poly [] \<Longrightarrow> ([-a, 1] %^ n) divides p \<Longrightarrow> ~(([-a, 1] %^ (Suc n)) divides p) \<Longrightarrow>
680 using order [of a n p] by auto
682 lemma (in idom_char_0) order_unique_lemma:
683 "poly p \<noteq> poly [] \<and> ([-a, 1] %^ n) divides p \<and> ~(([-a, 1] %^ (Suc n)) divides p) \<Longrightarrow>
685 by (blast intro: order_unique)
687 lemma (in ring_1) order_poly: "poly p = poly q \<Longrightarrow> order a p = order a q"
688 by (auto simp add: fun_eq divides_def poly_mult order_def)
690 lemma (in semiring_1) pexp_one[simp]: "p %^ (Suc 0) = p"
693 lemma (in comm_ring_1) lemma_order_root:
694 "0 < n \<and> [- a, 1] %^ n divides p \<and> ~ [- a, 1] %^ (Suc n) divides p \<Longrightarrow> poly p a = 0"
695 by (induct n arbitrary: a p) (auto simp add: divides_def poly_mult simp del: pmult_Cons)
697 lemma (in idom_char_0) order_root:
698 "poly p a = 0 \<longleftrightarrow> poly p = poly [] \<or> order a p \<noteq> 0"
699 apply (cases "poly p = poly []")
701 apply (simp add: poly_linear_divides del: pmult_Cons, safe)
702 apply (drule_tac [!] a = a in order2)
704 apply (simp add: divides_def poly_mult fun_eq del: pmult_Cons, blast)
706 apply (blast intro: lemma_order_root)
709 lemma (in idom_char_0) order_divides:
710 "([-a, 1] %^ n) divides p \<longleftrightarrow> poly p = poly [] \<or> n \<le> order a p"
711 apply (cases "poly p = poly []")
713 apply (simp add: divides_def fun_eq poly_mult)
714 apply (rule_tac x = "[]" in exI)
715 apply (auto dest!: order2 [where a=a] intro: poly_exp_divides simp del: pexp_Suc)
718 lemma (in idom_char_0) order_decomp:
719 "poly p \<noteq> poly [] \<Longrightarrow> \<exists>q. poly p = poly (([-a, 1] %^ (order a p)) *** q) \<and> ~([-a, 1] divides q)"
720 apply (unfold divides_def)
721 apply (drule order2 [where a = a])
722 apply (simp add: divides_def del: pexp_Suc pmult_Cons, safe)
723 apply (rule_tac x = q in exI, safe)
724 apply (drule_tac x = qa in spec)
725 apply (auto simp add: poly_mult fun_eq poly_exp mult_ac simp del: pmult_Cons)
728 text{*Important composition properties of orders.*}
730 "poly (p *** q) \<noteq> poly [] \<Longrightarrow>
731 order a (p *** q) = order a p + order (a::'a::{idom_char_0}) q"
732 apply (cut_tac a = a and p = "p *** q" and n = "order a p + order a q" in order)
733 apply (auto simp add: poly_entire simp del: pmult_Cons)
734 apply (drule_tac a = a in order2)+
736 apply (simp add: divides_def fun_eq poly_exp_add poly_mult del: pmult_Cons, safe)
737 apply (rule_tac x = "qa *** qaa" in exI)
738 apply (simp add: poly_mult mult_ac del: pmult_Cons)
739 apply (drule_tac a = a in order_decomp)+
741 apply (subgoal_tac "[-a,1] divides (qa *** qaa) ")
742 apply (simp add: poly_primes del: pmult_Cons)
743 apply (auto simp add: divides_def simp del: pmult_Cons)
744 apply (rule_tac x = qb in exI)
745 apply (subgoal_tac "poly ([-a, 1] %^ (order a p) *** (qa *** qaa)) = poly ([-a, 1] %^ (order a p) *** ([-a, 1] *** qb))")
746 apply (drule poly_mult_left_cancel [THEN iffD1], force)
747 apply (subgoal_tac "poly ([-a, 1] %^ (order a q) *** ([-a, 1] %^ (order a p) *** (qa *** qaa))) = poly ([-a, 1] %^ (order a q) *** ([-a, 1] %^ (order a p) *** ([-a, 1] *** qb))) ")
748 apply (drule poly_mult_left_cancel [THEN iffD1], force)
749 apply (simp add: fun_eq poly_exp_add poly_mult mult_ac del: pmult_Cons)
752 lemma (in idom_char_0) order_mult:
753 assumes "poly (p *** q) \<noteq> poly []"
754 shows "order a (p *** q) = order a p + order a q"
756 apply (cut_tac a = a and p = "pmult p q" and n = "order a p + order a q" in order)
757 apply (auto simp add: poly_entire simp del: pmult_Cons)
758 apply (drule_tac a = a in order2)+
760 apply (simp add: divides_def fun_eq poly_exp_add poly_mult del: pmult_Cons, safe)
761 apply (rule_tac x = "pmult qa qaa" in exI)
762 apply (simp add: poly_mult mult_ac del: pmult_Cons)
763 apply (drule_tac a = a in order_decomp)+
765 apply (subgoal_tac "[uminus a, one] divides pmult qa qaa")
766 apply (simp add: poly_primes del: pmult_Cons)
767 apply (auto simp add: divides_def simp del: pmult_Cons)
768 apply (rule_tac x = qb in exI)
769 apply (subgoal_tac "poly (pmult (pexp [uminus a, one] (order a p)) (pmult qa qaa)) =
770 poly (pmult (pexp [uminus a, one] (?order a p)) (pmult [uminus a, one] qb))")
771 apply (drule poly_mult_left_cancel [THEN iffD1], force)
772 apply (subgoal_tac "poly (pmult (pexp [uminus a, one] (order a q))
773 (pmult (pexp [uminus a, one] (order a p)) (pmult qa qaa))) =
774 poly (pmult (pexp [uminus a, one] (order a q))
775 (pmult (pexp [uminus a, one] (order a p)) (pmult [uminus a, one] qb)))")
776 apply (drule poly_mult_left_cancel [THEN iffD1], force)
777 apply (simp add: fun_eq poly_exp_add poly_mult mult_ac del: pmult_Cons)
780 lemma (in idom_char_0) order_root2: "poly p \<noteq> poly [] \<Longrightarrow> poly p a = 0 \<longleftrightarrow> order a p \<noteq> 0"
781 by (rule order_root [THEN ssubst]) auto
783 lemma (in semiring_1) pmult_one[simp]: "[1] *** p = p" by auto
785 lemma (in semiring_0) poly_Nil_zero: "poly [] = poly [0]"
786 by (simp add: fun_eq)
788 lemma (in idom_char_0) rsquarefree_decomp:
789 "rsquarefree p \<Longrightarrow> poly p a = 0 \<Longrightarrow>
790 \<exists>q. poly p = poly ([-a, 1] *** q) \<and> poly q a \<noteq> 0"
791 apply (simp add: rsquarefree_def, safe)
792 apply (frule_tac a = a in order_decomp)
793 apply (drule_tac x = a in spec)
794 apply (drule_tac a = a in order_root2 [symmetric])
795 apply (auto simp del: pmult_Cons)
796 apply (rule_tac x = q in exI, safe)
797 apply (simp add: poly_mult fun_eq)
798 apply (drule_tac p1 = q in poly_linear_divides [THEN iffD1])
799 apply (simp add: divides_def del: pmult_Cons, safe)
800 apply (drule_tac x = "[]" in spec)
801 apply (auto simp add: fun_eq)
805 text{*Normalization of a polynomial.*}
807 lemma (in semiring_0) poly_normalize[simp]: "poly (pnormalize p) = poly p"
808 by (induct p) (auto simp add: fun_eq)
810 text{*The degree of a polynomial.*}
812 lemma (in semiring_0) lemma_degree_zero: "list_all (%c. c = 0) p \<longleftrightarrow> pnormalize p = []"
815 lemma (in idom_char_0) degree_zero:
816 assumes "poly p = poly []"
819 by (cases "pnormalize p = []") (auto simp add: degree_def poly_zero lemma_degree_zero)
821 lemma (in semiring_0) pnormalize_sing: "(pnormalize [x] = [x]) \<longleftrightarrow> x \<noteq> 0"
824 lemma (in semiring_0) pnormalize_pair: "y \<noteq> 0 \<longleftrightarrow> (pnormalize [x, y] = [x, y])"
827 lemma (in semiring_0) pnormal_cons: "pnormal p \<Longrightarrow> pnormal (c#p)"
828 unfolding pnormal_def by simp
830 lemma (in semiring_0) pnormal_tail: "p\<noteq>[] \<Longrightarrow> pnormal (c#p) \<Longrightarrow> pnormal p"
831 unfolding pnormal_def by(auto split: split_if_asm)
834 lemma (in semiring_0) pnormal_last_nonzero: "pnormal p \<Longrightarrow> last p \<noteq> 0"
835 by (induct p) (simp_all add: pnormal_def split: split_if_asm)
837 lemma (in semiring_0) pnormal_length: "pnormal p \<Longrightarrow> 0 < length p"
838 unfolding pnormal_def length_greater_0_conv by blast
840 lemma (in semiring_0) pnormal_last_length: "0 < length p \<Longrightarrow> last p \<noteq> 0 \<Longrightarrow> pnormal p"
841 by (induct p) (auto simp: pnormal_def split: split_if_asm)
844 lemma (in semiring_0) pnormal_id: "pnormal p \<longleftrightarrow> 0 < length p \<and> last p \<noteq> 0"
845 using pnormal_last_length pnormal_length pnormal_last_nonzero by blast
847 lemma (in idom_char_0) poly_Cons_eq:
848 "poly (c # cs) = poly (d # ds) \<longleftrightarrow> c = d \<and> poly cs = poly ds"
849 (is "?lhs \<longleftrightarrow> ?rhs")
852 hence "\<And>x. poly ((c#cs) +++ -- (d#ds)) x = 0"
853 by (simp only: poly_minus poly_add algebra_simps) simp
854 hence "poly ((c#cs) +++ -- (d#ds)) = poly []" by(simp add: fun_eq_iff)
855 hence "c = d \<and> list_all (\<lambda>x. x=0) ((cs +++ -- ds))"
856 unfolding poly_zero by (simp add: poly_minus_def algebra_simps)
857 hence "c = d \<and> (\<forall>x. poly (cs +++ -- ds) x = 0)"
858 unfolding poly_zero[symmetric] by simp
859 then show ?rhs by (simp add: poly_minus poly_add algebra_simps fun_eq_iff)
862 then show ?lhs by(simp add:fun_eq_iff)
865 lemma (in idom_char_0) pnormalize_unique: "poly p = poly q \<Longrightarrow> pnormalize p = pnormalize q"
866 proof (induct q arbitrary: p)
868 thus ?case by (simp only: poly_zero lemma_degree_zero) simp
874 hence "poly [] = poly (c#cs)" by blast
875 then have "poly (c#cs) = poly [] " by simp
876 thus ?case by (simp only: poly_zero lemma_degree_zero) simp
879 hence eq: "poly (d # ds) = poly (c # cs)" by blast
880 hence eq': "\<And>x. poly (d # ds) x = poly (c # cs) x" by simp
881 hence "poly (d # ds) 0 = poly (c # cs) 0" by blast
882 hence dc: "d = c" by auto
883 with eq have "poly ds = poly cs"
884 unfolding poly_Cons_eq by simp
885 with Cons.prems have "pnormalize ds = pnormalize cs" by blast
886 with dc show ?case by simp
890 lemma (in idom_char_0) degree_unique:
891 assumes pq: "poly p = poly q"
892 shows "degree p = degree q"
893 using pnormalize_unique[OF pq] unfolding degree_def by simp
895 lemma (in semiring_0) pnormalize_length:
896 "length (pnormalize p) \<le> length p" by (induct p) auto
898 lemma (in semiring_0) last_linear_mul_lemma:
899 "last ((a %* p) +++ (x#(b %* p))) = (if p = [] then x else b * last p)"
900 apply (induct p arbitrary: a x b)
902 apply (subgoal_tac "padd (cmult aa p) (times b a # cmult b p) \<noteq> []")
908 lemma (in semiring_1) last_linear_mul:
909 assumes p: "p \<noteq> []"
910 shows "last ([a,1] *** p) = last p"
912 from p obtain c cs where cs: "p = c#cs" by (cases p) auto
913 from cs have eq: "[a,1] *** p = (a %* (c#cs)) +++ (0#(1 %* (c#cs)))"
914 by (simp add: poly_cmult_distr)
915 show ?thesis using cs
916 unfolding eq last_linear_mul_lemma by simp
919 lemma (in semiring_0) pnormalize_eq: "last p \<noteq> 0 \<Longrightarrow> pnormalize p = p"
920 by (induct p) (auto split: split_if_asm)
922 lemma (in semiring_0) last_pnormalize: "pnormalize p \<noteq> [] \<Longrightarrow> last (pnormalize p) \<noteq> 0"
925 lemma (in semiring_0) pnormal_degree: "last p \<noteq> 0 \<Longrightarrow> degree p = length p - 1"
926 using pnormalize_eq[of p] unfolding degree_def by simp
928 lemma (in semiring_0) poly_Nil_ext: "poly [] = (\<lambda>x. 0)"
931 lemma (in idom_char_0) linear_mul_degree:
932 assumes p: "poly p \<noteq> poly []"
933 shows "degree ([a,1] *** p) = degree p + 1"
935 from p have pnz: "pnormalize p \<noteq> []"
936 unfolding poly_zero lemma_degree_zero .
938 from last_linear_mul[OF pnz, of a] last_pnormalize[OF pnz]
939 have l0: "last ([a, 1] *** pnormalize p) \<noteq> 0" by simp
940 from last_pnormalize[OF pnz] last_linear_mul[OF pnz, of a]
941 pnormal_degree[OF l0] pnormal_degree[OF last_pnormalize[OF pnz]] pnz
943 have th: "degree ([a,1] *** pnormalize p) = degree (pnormalize p) + 1"
946 have eqs: "poly ([a,1] *** pnormalize p) = poly ([a,1] *** p)"
947 by (rule ext) (simp add: poly_mult poly_add poly_cmult)
948 from degree_unique[OF eqs] th
949 show ?thesis by (simp add: degree_unique[OF poly_normalize])
952 lemma (in idom_char_0) linear_pow_mul_degree:
953 "degree([a,1] %^n *** p) = (if poly p = poly [] then 0 else degree p + n)"
954 proof (induct n arbitrary: a p)
957 proof (cases "poly p = poly []")
960 using degree_unique[OF True] by (simp add: degree_def)
963 then show ?thesis by (auto simp add: poly_Nil_ext)
967 have eq: "poly ([a,1] %^(Suc n) *** p) = poly ([a,1] %^ n *** ([a,1] *** p))"
969 apply (simp add: poly_mult poly_add poly_cmult)
970 apply (simp add: mult_ac add_ac distrib_left)
972 note deq = degree_unique[OF eq]
974 proof (cases "poly p = poly []")
976 with eq have eq': "poly ([a,1] %^(Suc n) *** p) = poly []"
979 apply (simp add: poly_mult poly_cmult poly_add)
981 from degree_unique[OF eq'] True show ?thesis
982 by (simp add: degree_def)
985 then have ap: "poly ([a,1] *** p) \<noteq> poly []"
986 using poly_mult_not_eq_poly_Nil unfolding poly_entire by auto
987 have eq: "poly ([a,1] %^(Suc n) *** p) = poly ([a,1]%^n *** ([a,1] *** p))"
988 by (rule ext, simp add: poly_mult poly_add poly_exp poly_cmult algebra_simps)
989 from ap have ap': "(poly ([a,1] *** p) = poly []) = False"
991 have th0: "degree ([a,1]%^n *** ([a,1] *** p)) = degree ([a,1] *** p) + n"
992 apply (simp only: Suc.hyps[of a "pmult [a,one] p"] ap')
995 from degree_unique[OF eq] ap False th0 linear_mul_degree[OF False, of a]
996 show ?thesis by (auto simp del: poly.simps)
1000 lemma (in idom_char_0) order_degree:
1001 assumes p0: "poly p \<noteq> poly []"
1002 shows "order a p \<le> degree p"
1004 from order2[OF p0, unfolded divides_def]
1005 obtain q where q: "poly p = poly ([- a, 1]%^ (order a p) *** q)" by blast
1007 assume "poly q = poly []"
1008 with q p0 have False by (simp add: poly_mult poly_entire)
1010 with degree_unique[OF q, unfolded linear_pow_mul_degree] show ?thesis
1014 text{*Tidier versions of finiteness of roots.*}
1016 lemma (in idom_char_0) poly_roots_finite_set:
1017 "poly p \<noteq> poly [] \<Longrightarrow> finite {x. poly p x = 0}"
1018 unfolding poly_roots_finite .
1020 text{*bound for polynomial.*}
1022 lemma poly_mono: "abs(x) \<le> k \<Longrightarrow> abs(poly p (x::'a::{linordered_idom})) \<le> poly (map abs p) k"
1025 apply (rule_tac y = "abs a + abs (x * poly p x)" in order_trans)
1026 apply (rule abs_triangle_ineq)
1027 apply (auto intro!: mult_mono simp add: abs_mult)
1030 lemma (in semiring_0) poly_Sing: "poly [c] x = c" by simp