1 (* Title: HOL/Library/Polynomial.thy
3 Author: Clemens Ballarin
4 Author: Florian Haftmann
7 section \<open>Polynomials as type over a ring structure\<close>
10 imports Main "~~/src/HOL/GCD" "~~/src/HOL/Library/More_List" "~~/src/HOL/Library/Infinite_Set"
13 subsection \<open>Auxiliary: operations for lists (later) representing coefficients\<close>
15 definition cCons :: "'a::zero \<Rightarrow> 'a list \<Rightarrow> 'a list" (infixr "##" 65)
17 "x ## xs = (if xs = [] \<and> x = 0 then [] else x # xs)"
19 lemma cCons_0_Nil_eq [simp]:
21 by (simp add: cCons_def)
23 lemma cCons_Cons_eq [simp]:
24 "x ## y # ys = x # y # ys"
25 by (simp add: cCons_def)
27 lemma cCons_append_Cons_eq [simp]:
28 "x ## xs @ y # ys = x # xs @ y # ys"
29 by (simp add: cCons_def)
31 lemma cCons_not_0_eq [simp]:
32 "x \<noteq> 0 \<Longrightarrow> x ## xs = x # xs"
33 by (simp add: cCons_def)
35 lemma strip_while_not_0_Cons_eq [simp]:
36 "strip_while (\<lambda>x. x = 0) (x # xs) = x ## strip_while (\<lambda>x. x = 0) xs"
38 case False then show ?thesis by simp
40 case True show ?thesis
41 proof (induct xs rule: rev_induct)
42 case Nil with True show ?case by simp
44 case (snoc y ys) then show ?case
45 by (cases "y = 0") (simp_all add: append_Cons [symmetric] del: append_Cons)
49 lemma tl_cCons [simp]:
51 by (simp add: cCons_def)
53 subsection \<open>Definition of type \<open>poly\<close>\<close>
55 typedef (overloaded) 'a poly = "{f :: nat \<Rightarrow> 'a::zero. \<forall>\<^sub>\<infinity> n. f n = 0}"
56 morphisms coeff Abs_poly by (auto intro!: ALL_MOST)
58 setup_lifting type_definition_poly
60 lemma poly_eq_iff: "p = q \<longleftrightarrow> (\<forall>n. coeff p n = coeff q n)"
61 by (simp add: coeff_inject [symmetric] fun_eq_iff)
63 lemma poly_eqI: "(\<And>n. coeff p n = coeff q n) \<Longrightarrow> p = q"
64 by (simp add: poly_eq_iff)
66 lemma MOST_coeff_eq_0: "\<forall>\<^sub>\<infinity> n. coeff p n = 0"
67 using coeff [of p] by simp
70 subsection \<open>Degree of a polynomial\<close>
72 definition degree :: "'a::zero poly \<Rightarrow> nat"
74 "degree p = (LEAST n. \<forall>i>n. coeff p i = 0)"
77 assumes "degree p < n"
80 have "\<exists>n. \<forall>i>n. coeff p i = 0"
81 using MOST_coeff_eq_0 by (simp add: MOST_nat)
82 then have "\<forall>i>degree p. coeff p i = 0"
83 unfolding degree_def by (rule LeastI_ex)
84 with assms show ?thesis by simp
87 lemma le_degree: "coeff p n \<noteq> 0 \<Longrightarrow> n \<le> degree p"
88 by (erule contrapos_np, rule coeff_eq_0, simp)
90 lemma degree_le: "\<forall>i>n. coeff p i = 0 \<Longrightarrow> degree p \<le> n"
91 unfolding degree_def by (erule Least_le)
93 lemma less_degree_imp: "n < degree p \<Longrightarrow> \<exists>i>n. coeff p i \<noteq> 0"
94 unfolding degree_def by (drule not_less_Least, simp)
97 subsection \<open>The zero polynomial\<close>
99 instantiation poly :: (zero) zero
102 lift_definition zero_poly :: "'a poly"
103 is "\<lambda>_. 0" by (rule MOST_I) simp
109 lemma coeff_0 [simp]:
113 lemma degree_0 [simp]:
115 by (rule order_antisym [OF degree_le le0]) simp
117 lemma leading_coeff_neq_0:
118 assumes "p \<noteq> 0"
119 shows "coeff p (degree p) \<noteq> 0"
120 proof (cases "degree p")
122 from \<open>p \<noteq> 0\<close> have "\<exists>n. coeff p n \<noteq> 0"
123 by (simp add: poly_eq_iff)
124 then obtain n where "coeff p n \<noteq> 0" ..
125 hence "n \<le> degree p" by (rule le_degree)
126 with \<open>coeff p n \<noteq> 0\<close> and \<open>degree p = 0\<close>
127 show "coeff p (degree p) \<noteq> 0" by simp
130 from \<open>degree p = Suc n\<close> have "n < degree p" by simp
131 hence "\<exists>i>n. coeff p i \<noteq> 0" by (rule less_degree_imp)
132 then obtain i where "n < i" and "coeff p i \<noteq> 0" by fast
133 from \<open>degree p = Suc n\<close> and \<open>n < i\<close> have "degree p \<le> i" by simp
134 also from \<open>coeff p i \<noteq> 0\<close> have "i \<le> degree p" by (rule le_degree)
135 finally have "degree p = i" .
136 with \<open>coeff p i \<noteq> 0\<close> show "coeff p (degree p) \<noteq> 0" by simp
139 lemma leading_coeff_0_iff [simp]:
140 "coeff p (degree p) = 0 \<longleftrightarrow> p = 0"
141 by (cases "p = 0", simp, simp add: leading_coeff_neq_0)
144 subsection \<open>List-style constructor for polynomials\<close>
146 lift_definition pCons :: "'a::zero \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
147 is "\<lambda>a p. case_nat a (coeff p)"
148 by (rule MOST_SucD) (simp add: MOST_coeff_eq_0)
150 lemmas coeff_pCons = pCons.rep_eq
152 lemma coeff_pCons_0 [simp]:
153 "coeff (pCons a p) 0 = a"
156 lemma coeff_pCons_Suc [simp]:
157 "coeff (pCons a p) (Suc n) = coeff p n"
158 by (simp add: coeff_pCons)
160 lemma degree_pCons_le:
161 "degree (pCons a p) \<le> Suc (degree p)"
162 by (rule degree_le) (simp add: coeff_eq_0 coeff_pCons split: nat.split)
164 lemma degree_pCons_eq:
165 "p \<noteq> 0 \<Longrightarrow> degree (pCons a p) = Suc (degree p)"
166 apply (rule order_antisym [OF degree_pCons_le])
167 apply (rule le_degree, simp)
170 lemma degree_pCons_0:
171 "degree (pCons a 0) = 0"
172 apply (rule order_antisym [OF _ le0])
173 apply (rule degree_le, simp add: coeff_pCons split: nat.split)
176 lemma degree_pCons_eq_if [simp]:
177 "degree (pCons a p) = (if p = 0 then 0 else Suc (degree p))"
178 apply (cases "p = 0", simp_all)
179 apply (rule order_antisym [OF _ le0])
180 apply (rule degree_le, simp add: coeff_pCons split: nat.split)
181 apply (rule order_antisym [OF degree_pCons_le])
182 apply (rule le_degree, simp)
185 lemma pCons_0_0 [simp]:
187 by (rule poly_eqI) (simp add: coeff_pCons split: nat.split)
189 lemma pCons_eq_iff [simp]:
190 "pCons a p = pCons b q \<longleftrightarrow> a = b \<and> p = q"
192 assume "pCons a p = pCons b q"
193 then have "coeff (pCons a p) 0 = coeff (pCons b q) 0" by simp
194 then show "a = b" by simp
196 assume "pCons a p = pCons b q"
197 then have "\<forall>n. coeff (pCons a p) (Suc n) =
198 coeff (pCons b q) (Suc n)" by simp
199 then show "p = q" by (simp add: poly_eq_iff)
202 lemma pCons_eq_0_iff [simp]:
203 "pCons a p = 0 \<longleftrightarrow> a = 0 \<and> p = 0"
204 using pCons_eq_iff [of a p 0 0] by simp
206 lemma pCons_cases [cases type: poly]:
207 obtains (pCons) a q where "p = pCons a q"
209 show "p = pCons (coeff p 0) (Abs_poly (\<lambda>n. coeff p (Suc n)))"
211 (simp_all add: MOST_inj[where f=Suc and P="\<lambda>n. p n = 0" for p] fun_eq_iff Abs_poly_inverse
215 lemma pCons_induct [case_names 0 pCons, induct type: poly]:
217 assumes pCons: "\<And>a p. a \<noteq> 0 \<or> p \<noteq> 0 \<Longrightarrow> P p \<Longrightarrow> P (pCons a p)"
219 proof (induct p rule: measure_induct_rule [where f=degree])
221 obtain a q where "p = pCons a q" by (rule pCons_cases)
223 proof (cases "q = 0")
225 then show "P q" by (simp add: zero)
228 then have "degree (pCons a q) = Suc (degree q)"
229 by (rule degree_pCons_eq)
230 then have "degree q < degree p"
231 using \<open>p = pCons a q\<close> by simp
236 proof (cases "a \<noteq> 0 \<or> q \<noteq> 0")
238 with \<open>P q\<close> show ?thesis by (auto intro: pCons)
241 with zero show ?thesis by simp
244 using \<open>p = pCons a q\<close> by simp
247 lemma degree_eq_zeroE:
248 fixes p :: "'a::zero poly"
249 assumes "degree p = 0"
250 obtains a where "p = pCons a 0"
252 obtain a q where p: "p = pCons a q" by (cases p)
253 with assms have "q = 0" by (cases "q = 0") simp_all
254 with p have "p = pCons a 0" by simp
255 with that show thesis .
259 subsection \<open>List-style syntax for polynomials\<close>
262 "_poly" :: "args \<Rightarrow> 'a poly" ("[:(_):]")
265 "[:x, xs:]" == "CONST pCons x [:xs:]"
266 "[:x:]" == "CONST pCons x 0"
267 "[:x:]" <= "CONST pCons x (_constrain 0 t)"
270 subsection \<open>Representation of polynomials by lists of coefficients\<close>
272 primrec Poly :: "'a::zero list \<Rightarrow> 'a poly"
274 [code_post]: "Poly [] = 0"
275 | [code_post]: "Poly (a # as) = pCons a (Poly as)"
277 lemma Poly_replicate_0 [simp]:
278 "Poly (replicate n 0) = 0"
279 by (induct n) simp_all
282 "Poly as = 0 \<longleftrightarrow> (\<exists>n. as = replicate n 0)"
283 by (induct as) (auto simp add: Cons_replicate_eq)
285 lemma degree_Poly: "degree (Poly xs) \<le> length xs"
286 by (induction xs) simp_all
288 definition coeffs :: "'a poly \<Rightarrow> 'a::zero list"
290 "coeffs p = (if p = 0 then [] else map (\<lambda>i. coeff p i) [0 ..< Suc (degree p)])"
292 lemma coeffs_eq_Nil [simp]:
293 "coeffs p = [] \<longleftrightarrow> p = 0"
294 by (simp add: coeffs_def)
296 lemma not_0_coeffs_not_Nil:
297 "p \<noteq> 0 \<Longrightarrow> coeffs p \<noteq> []"
300 lemma coeffs_0_eq_Nil [simp]:
304 lemma coeffs_pCons_eq_cCons [simp]:
305 "coeffs (pCons a p) = a ## coeffs p"
307 { fix ms :: "nat list" and f :: "nat \<Rightarrow> 'a" and x :: "'a"
308 assume "\<forall>m\<in>set ms. m > 0"
309 then have "map (case_nat x f) ms = map f (map (\<lambda>n. n - 1) ms)"
310 by (induct ms) (auto split: nat.split)
314 by (simp add: coeffs_def * upt_conv_Cons coeff_pCons map_decr_upt del: upt_Suc)
317 lemma length_coeffs: "p \<noteq> 0 \<Longrightarrow> length (coeffs p) = degree p + 1"
318 by (simp add: coeffs_def)
321 assumes "p \<noteq> 0" "n \<le> degree p"
322 shows "coeffs p ! n = coeff p n"
323 using assms unfolding coeffs_def by (auto simp del: upt_Suc)
325 lemma not_0_cCons_eq [simp]:
326 "p \<noteq> 0 \<Longrightarrow> a ## coeffs p = a # coeffs p"
327 by (simp add: cCons_def)
329 lemma Poly_coeffs [simp, code abstype]:
330 "Poly (coeffs p) = p"
333 lemma coeffs_Poly [simp]:
334 "coeffs (Poly as) = strip_while (HOL.eq 0) as"
336 case Nil then show ?case by simp
339 have "(\<forall>n. as \<noteq> replicate n 0) \<longleftrightarrow> (\<exists>a\<in>set as. a \<noteq> 0)"
340 using replicate_length_same [of as 0] by (auto dest: sym [of _ as])
341 with Cons show ?case by auto
344 lemma last_coeffs_not_0:
345 "p \<noteq> 0 \<Longrightarrow> last (coeffs p) \<noteq> 0"
346 by (induct p) (auto simp add: cCons_def)
348 lemma strip_while_coeffs [simp]:
349 "strip_while (HOL.eq 0) (coeffs p) = coeffs p"
350 by (cases "p = 0") (auto dest: last_coeffs_not_0 intro: strip_while_not_last)
353 "p = q \<longleftrightarrow> coeffs p = coeffs q" (is "?P \<longleftrightarrow> ?Q")
355 assume ?P then show ?Q by simp
358 then have "Poly (coeffs p) = Poly (coeffs q)" by simp
363 "coeff (Poly xs) n = nth_default 0 xs n"
364 apply (induct xs arbitrary: n) apply simp_all
365 by (metis nat.case not0_implies_Suc nth_default_Cons_0 nth_default_Cons_Suc pCons.rep_eq)
367 lemma nth_default_coeffs_eq:
368 "nth_default 0 (coeffs p) = coeff p"
369 by (simp add: fun_eq_iff coeff_Poly_eq [symmetric])
372 "coeff p = nth_default 0 (coeffs p)"
373 by (simp add: nth_default_coeffs_eq)
376 assumes coeff: "\<And>n. coeff p n = nth_default 0 xs n"
377 assumes zero: "xs \<noteq> [] \<Longrightarrow> last xs \<noteq> 0"
378 shows "coeffs p = xs"
380 from coeff have "p = Poly xs" by (simp add: poly_eq_iff coeff_Poly_eq)
381 with zero show ?thesis by simp (cases xs, simp_all)
384 lemma degree_eq_length_coeffs [code]:
385 "degree p = length (coeffs p) - 1"
386 by (simp add: coeffs_def)
388 lemma length_coeffs_degree:
389 "p \<noteq> 0 \<Longrightarrow> length (coeffs p) = Suc (degree p)"
390 by (induct p) (auto simp add: cCons_def)
392 lemma [code abstract]:
394 by (fact coeffs_0_eq_Nil)
396 lemma [code abstract]:
397 "coeffs (pCons a p) = a ## coeffs p"
398 by (fact coeffs_pCons_eq_cCons)
400 instantiation poly :: ("{zero, equal}") equal
404 [code]: "HOL.equal (p::'a poly) q \<longleftrightarrow> HOL.equal (coeffs p) (coeffs q)"
407 by standard (simp add: equal equal_poly_def coeffs_eq_iff)
411 lemma [code nbe]: "HOL.equal (p :: _ poly) p \<longleftrightarrow> True"
414 definition is_zero :: "'a::zero poly \<Rightarrow> bool"
416 [code]: "is_zero p \<longleftrightarrow> List.null (coeffs p)"
418 lemma is_zero_null [code_abbrev]:
419 "is_zero p \<longleftrightarrow> p = 0"
420 by (simp add: is_zero_def null_def)
423 subsection \<open>Fold combinator for polynomials\<close>
425 definition fold_coeffs :: "('a::zero \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a poly \<Rightarrow> 'b \<Rightarrow> 'b"
427 "fold_coeffs f p = foldr f (coeffs p)"
429 lemma fold_coeffs_0_eq [simp]:
430 "fold_coeffs f 0 = id"
431 by (simp add: fold_coeffs_def)
433 lemma fold_coeffs_pCons_eq [simp]:
434 "f 0 = id \<Longrightarrow> fold_coeffs f (pCons a p) = f a \<circ> fold_coeffs f p"
435 by (simp add: fold_coeffs_def cCons_def fun_eq_iff)
437 lemma fold_coeffs_pCons_0_0_eq [simp]:
438 "fold_coeffs f (pCons 0 0) = id"
439 by (simp add: fold_coeffs_def)
441 lemma fold_coeffs_pCons_coeff_not_0_eq [simp]:
442 "a \<noteq> 0 \<Longrightarrow> fold_coeffs f (pCons a p) = f a \<circ> fold_coeffs f p"
443 by (simp add: fold_coeffs_def)
445 lemma fold_coeffs_pCons_not_0_0_eq [simp]:
446 "p \<noteq> 0 \<Longrightarrow> fold_coeffs f (pCons a p) = f a \<circ> fold_coeffs f p"
447 by (simp add: fold_coeffs_def)
450 subsection \<open>Canonical morphism on polynomials -- evaluation\<close>
452 definition poly :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a"
454 "poly p = fold_coeffs (\<lambda>a f x. a + x * f x) p (\<lambda>x. 0)" \<comment> \<open>The Horner Schema\<close>
458 by (simp add: poly_def)
460 lemma poly_pCons [simp]:
461 "poly (pCons a p) x = a + x * poly p x"
462 by (cases "p = 0 \<and> a = 0") (auto simp add: poly_def)
465 "poly p (x :: 'a :: {comm_semiring_0, semiring_1}) = (\<Sum>i\<le>degree p. coeff p i * x ^ i)"
466 proof (induction p rule: pCons_induct)
469 proof (cases "p = 0")
471 let ?p' = "pCons a p"
472 note poly_pCons[of a p x]
474 also have "a + x * (\<Sum>i\<le>degree p. coeff p i * x ^ i) =
475 coeff ?p' 0 * x^0 + (\<Sum>i\<le>degree p. coeff ?p' (Suc i) * x^Suc i)"
476 by (simp add: field_simps setsum_right_distrib coeff_pCons)
477 also note setsum_atMost_Suc_shift[symmetric]
478 also note degree_pCons_eq[OF \<open>p \<noteq> 0\<close>, of a, symmetric]
479 finally show ?thesis .
484 subsection \<open>Monomials\<close>
486 lift_definition monom :: "'a \<Rightarrow> nat \<Rightarrow> 'a::zero poly"
487 is "\<lambda>a m n. if m = n then a else 0"
488 by (simp add: MOST_iff_cofinite)
490 lemma coeff_monom [simp]:
491 "coeff (monom a m) n = (if m = n then a else 0)"
495 "monom a 0 = pCons a 0"
496 by (rule poly_eqI) (simp add: coeff_pCons split: nat.split)
499 "monom a (Suc n) = pCons 0 (monom a n)"
500 by (rule poly_eqI) (simp add: coeff_pCons split: nat.split)
502 lemma monom_eq_0 [simp]: "monom 0 n = 0"
503 by (rule poly_eqI) simp
505 lemma monom_eq_0_iff [simp]: "monom a n = 0 \<longleftrightarrow> a = 0"
506 by (simp add: poly_eq_iff)
508 lemma monom_eq_iff [simp]: "monom a n = monom b n \<longleftrightarrow> a = b"
509 by (simp add: poly_eq_iff)
511 lemma degree_monom_le: "degree (monom a n) \<le> n"
512 by (rule degree_le, simp)
514 lemma degree_monom_eq: "a \<noteq> 0 \<Longrightarrow> degree (monom a n) = n"
515 apply (rule order_antisym [OF degree_monom_le])
516 apply (rule le_degree, simp)
519 lemma coeffs_monom [code abstract]:
520 "coeffs (monom a n) = (if a = 0 then [] else replicate n 0 @ [a])"
521 by (induct n) (simp_all add: monom_0 monom_Suc)
523 lemma fold_coeffs_monom [simp]:
524 "a \<noteq> 0 \<Longrightarrow> fold_coeffs f (monom a n) = f 0 ^^ n \<circ> f a"
525 by (simp add: fold_coeffs_def coeffs_monom fun_eq_iff)
528 fixes a x :: "'a::{comm_semiring_1}"
529 shows "poly (monom a n) x = a * x ^ n"
530 by (cases "a = 0", simp_all)
531 (induct n, simp_all add: mult.left_commute poly_def)
534 subsection \<open>Addition and subtraction\<close>
536 instantiation poly :: (comm_monoid_add) comm_monoid_add
539 lift_definition plus_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
540 is "\<lambda>p q n. coeff p n + coeff q n"
543 show "\<forall>\<^sub>\<infinity>n. coeff p n + coeff q n = 0"
544 using MOST_coeff_eq_0[of p] MOST_coeff_eq_0[of q] by eventually_elim simp
547 lemma coeff_add [simp]: "coeff (p + q) n = coeff p n + coeff q n"
548 by (simp add: plus_poly.rep_eq)
552 fix p q r :: "'a poly"
553 show "(p + q) + r = p + (q + r)"
554 by (simp add: poly_eq_iff add.assoc)
556 by (simp add: poly_eq_iff add.commute)
558 by (simp add: poly_eq_iff)
563 instantiation poly :: (cancel_comm_monoid_add) cancel_comm_monoid_add
566 lift_definition minus_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
567 is "\<lambda>p q n. coeff p n - coeff q n"
570 show "\<forall>\<^sub>\<infinity>n. coeff p n - coeff q n = 0"
571 using MOST_coeff_eq_0[of p] MOST_coeff_eq_0[of q] by eventually_elim simp
574 lemma coeff_diff [simp]: "coeff (p - q) n = coeff p n - coeff q n"
575 by (simp add: minus_poly.rep_eq)
579 fix p q r :: "'a poly"
581 by (simp add: poly_eq_iff)
582 show "p - q - r = p - (q + r)"
583 by (simp add: poly_eq_iff diff_diff_eq)
588 instantiation poly :: (ab_group_add) ab_group_add
591 lift_definition uminus_poly :: "'a poly \<Rightarrow> 'a poly"
592 is "\<lambda>p n. - coeff p n"
595 show "\<forall>\<^sub>\<infinity>n. - coeff p n = 0"
596 using MOST_coeff_eq_0 by simp
599 lemma coeff_minus [simp]: "coeff (- p) n = - coeff p n"
600 by (simp add: uminus_poly.rep_eq)
606 by (simp add: poly_eq_iff)
607 show "p - q = p + - q"
608 by (simp add: poly_eq_iff)
613 lemma add_pCons [simp]:
614 "pCons a p + pCons b q = pCons (a + b) (p + q)"
615 by (rule poly_eqI, simp add: coeff_pCons split: nat.split)
617 lemma minus_pCons [simp]:
618 "- pCons a p = pCons (- a) (- p)"
619 by (rule poly_eqI, simp add: coeff_pCons split: nat.split)
621 lemma diff_pCons [simp]:
622 "pCons a p - pCons b q = pCons (a - b) (p - q)"
623 by (rule poly_eqI, simp add: coeff_pCons split: nat.split)
625 lemma degree_add_le_max: "degree (p + q) \<le> max (degree p) (degree q)"
626 by (rule degree_le, auto simp add: coeff_eq_0)
629 "\<lbrakk>degree p \<le> n; degree q \<le> n\<rbrakk> \<Longrightarrow> degree (p + q) \<le> n"
630 by (auto intro: order_trans degree_add_le_max)
632 lemma degree_add_less:
633 "\<lbrakk>degree p < n; degree q < n\<rbrakk> \<Longrightarrow> degree (p + q) < n"
634 by (auto intro: le_less_trans degree_add_le_max)
636 lemma degree_add_eq_right:
637 "degree p < degree q \<Longrightarrow> degree (p + q) = degree q"
638 apply (cases "q = 0", simp)
639 apply (rule order_antisym)
640 apply (simp add: degree_add_le)
641 apply (rule le_degree)
642 apply (simp add: coeff_eq_0)
645 lemma degree_add_eq_left:
646 "degree q < degree p \<Longrightarrow> degree (p + q) = degree p"
647 using degree_add_eq_right [of q p]
648 by (simp add: add.commute)
650 lemma degree_minus [simp]:
651 "degree (- p) = degree p"
652 unfolding degree_def by simp
654 lemma degree_diff_le_max:
655 fixes p q :: "'a :: ab_group_add poly"
656 shows "degree (p - q) \<le> max (degree p) (degree q)"
657 using degree_add_le [where p=p and q="-q"]
660 lemma degree_diff_le:
661 fixes p q :: "'a :: ab_group_add poly"
662 assumes "degree p \<le> n" and "degree q \<le> n"
663 shows "degree (p - q) \<le> n"
664 using assms degree_add_le [of p n "- q"] by simp
666 lemma degree_diff_less:
667 fixes p q :: "'a :: ab_group_add poly"
668 assumes "degree p < n" and "degree q < n"
669 shows "degree (p - q) < n"
670 using assms degree_add_less [of p n "- q"] by simp
672 lemma add_monom: "monom a n + monom b n = monom (a + b) n"
673 by (rule poly_eqI) simp
675 lemma diff_monom: "monom a n - monom b n = monom (a - b) n"
676 by (rule poly_eqI) simp
678 lemma minus_monom: "- monom a n = monom (-a) n"
679 by (rule poly_eqI) simp
681 lemma coeff_setsum: "coeff (\<Sum>x\<in>A. p x) i = (\<Sum>x\<in>A. coeff (p x) i)"
682 by (cases "finite A", induct set: finite, simp_all)
684 lemma monom_setsum: "monom (\<Sum>x\<in>A. a x) n = (\<Sum>x\<in>A. monom (a x) n)"
685 by (rule poly_eqI) (simp add: coeff_setsum)
687 fun plus_coeffs :: "'a::comm_monoid_add list \<Rightarrow> 'a list \<Rightarrow> 'a list"
689 "plus_coeffs xs [] = xs"
690 | "plus_coeffs [] ys = ys"
691 | "plus_coeffs (x # xs) (y # ys) = (x + y) ## plus_coeffs xs ys"
693 lemma coeffs_plus_eq_plus_coeffs [code abstract]:
694 "coeffs (p + q) = plus_coeffs (coeffs p) (coeffs q)"
696 { fix xs ys :: "'a list" and n
697 have "nth_default 0 (plus_coeffs xs ys) n = nth_default 0 xs n + nth_default 0 ys n"
698 proof (induct xs ys arbitrary: n rule: plus_coeffs.induct)
700 then show ?case by (cases n) (auto simp add: cCons_def)
703 { fix xs ys :: "'a list"
704 assume "xs \<noteq> [] \<Longrightarrow> last xs \<noteq> 0" and "ys \<noteq> [] \<Longrightarrow> last ys \<noteq> 0"
705 moreover assume "plus_coeffs xs ys \<noteq> []"
706 ultimately have "last (plus_coeffs xs ys) \<noteq> 0"
707 proof (induct xs ys rule: plus_coeffs.induct)
708 case (3 x xs y ys) then show ?case by (auto simp add: cCons_def) metis
712 apply (rule coeffs_eqI)
713 apply (simp add: * nth_default_coeffs_eq)
715 apply (auto dest: last_coeffs_not_0)
719 lemma coeffs_uminus [code abstract]:
720 "coeffs (- p) = map (\<lambda>a. - a) (coeffs p)"
722 (simp_all add: not_0_coeffs_not_Nil last_map last_coeffs_not_0 nth_default_map_eq nth_default_coeffs_eq)
725 fixes p q :: "'a::ab_group_add poly"
726 shows "p - q = p + - q"
727 by (fact diff_conv_add_uminus)
729 lemma poly_add [simp]: "poly (p + q) x = poly p x + poly q x"
730 apply (induct p arbitrary: q, simp)
731 apply (case_tac q, simp, simp add: algebra_simps)
734 lemma poly_minus [simp]:
735 fixes x :: "'a::comm_ring"
736 shows "poly (- p) x = - poly p x"
737 by (induct p) simp_all
739 lemma poly_diff [simp]:
740 fixes x :: "'a::comm_ring"
741 shows "poly (p - q) x = poly p x - poly q x"
742 using poly_add [of p "- q" x] by simp
744 lemma poly_setsum: "poly (\<Sum>k\<in>A. p k) x = (\<Sum>k\<in>A. poly (p k) x)"
745 by (induct A rule: infinite_finite_induct) simp_all
747 lemma Poly_snoc: "Poly (xs @ [x]) = Poly xs + monom x (length xs)"
748 by (induction xs) (simp_all add: monom_0 monom_Suc)
751 subsection \<open>Multiplication by a constant, polynomial multiplication and the unit polynomial\<close>
753 lift_definition smult :: "'a::comm_semiring_0 \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
754 is "\<lambda>a p n. a * coeff p n"
756 fix a :: 'a and p :: "'a poly" show "\<forall>\<^sub>\<infinity> i. a * coeff p i = 0"
757 using MOST_coeff_eq_0[of p] by eventually_elim simp
760 lemma coeff_smult [simp]:
761 "coeff (smult a p) n = a * coeff p n"
762 by (simp add: smult.rep_eq)
764 lemma degree_smult_le: "degree (smult a p) \<le> degree p"
765 by (rule degree_le, simp add: coeff_eq_0)
767 lemma smult_smult [simp]: "smult a (smult b p) = smult (a * b) p"
768 by (rule poly_eqI, simp add: mult.assoc)
770 lemma smult_0_right [simp]: "smult a 0 = 0"
771 by (rule poly_eqI, simp)
773 lemma smult_0_left [simp]: "smult 0 p = 0"
774 by (rule poly_eqI, simp)
776 lemma smult_1_left [simp]: "smult (1::'a::comm_semiring_1) p = p"
777 by (rule poly_eqI, simp)
779 lemma smult_add_right:
780 "smult a (p + q) = smult a p + smult a q"
781 by (rule poly_eqI, simp add: algebra_simps)
783 lemma smult_add_left:
784 "smult (a + b) p = smult a p + smult b p"
785 by (rule poly_eqI, simp add: algebra_simps)
787 lemma smult_minus_right [simp]:
788 "smult (a::'a::comm_ring) (- p) = - smult a p"
789 by (rule poly_eqI, simp)
791 lemma smult_minus_left [simp]:
792 "smult (- a::'a::comm_ring) p = - smult a p"
793 by (rule poly_eqI, simp)
795 lemma smult_diff_right:
796 "smult (a::'a::comm_ring) (p - q) = smult a p - smult a q"
797 by (rule poly_eqI, simp add: algebra_simps)
799 lemma smult_diff_left:
800 "smult (a - b::'a::comm_ring) p = smult a p - smult b p"
801 by (rule poly_eqI, simp add: algebra_simps)
803 lemmas smult_distribs =
804 smult_add_left smult_add_right
805 smult_diff_left smult_diff_right
807 lemma smult_pCons [simp]:
808 "smult a (pCons b p) = pCons (a * b) (smult a p)"
809 by (rule poly_eqI, simp add: coeff_pCons split: nat.split)
811 lemma smult_monom: "smult a (monom b n) = monom (a * b) n"
812 by (induct n, simp add: monom_0, simp add: monom_Suc)
814 lemma degree_smult_eq [simp]:
815 fixes a :: "'a::idom"
816 shows "degree (smult a p) = (if a = 0 then 0 else degree p)"
817 by (cases "a = 0", simp, simp add: degree_def)
819 lemma smult_eq_0_iff [simp]:
820 fixes a :: "'a::idom"
821 shows "smult a p = 0 \<longleftrightarrow> a = 0 \<or> p = 0"
822 by (simp add: poly_eq_iff)
824 lemma coeffs_smult [code abstract]:
825 fixes p :: "'a::idom poly"
826 shows "coeffs (smult a p) = (if a = 0 then [] else map (Groups.times a) (coeffs p))"
828 (auto simp add: not_0_coeffs_not_Nil last_map last_coeffs_not_0 nth_default_map_eq nth_default_coeffs_eq)
830 instantiation poly :: (comm_semiring_0) comm_semiring_0
834 "p * q = fold_coeffs (\<lambda>a p. smult a q + pCons 0 p) p 0"
836 lemma mult_poly_0_left: "(0::'a poly) * q = 0"
837 by (simp add: times_poly_def)
839 lemma mult_pCons_left [simp]:
840 "pCons a p * q = smult a q + pCons 0 (p * q)"
841 by (cases "p = 0 \<and> a = 0") (auto simp add: times_poly_def)
843 lemma mult_poly_0_right: "p * (0::'a poly) = 0"
844 by (induct p) (simp add: mult_poly_0_left, simp)
846 lemma mult_pCons_right [simp]:
847 "p * pCons a q = smult a p + pCons 0 (p * q)"
848 by (induct p) (simp add: mult_poly_0_left, simp add: algebra_simps)
850 lemmas mult_poly_0 = mult_poly_0_left mult_poly_0_right
852 lemma mult_smult_left [simp]:
853 "smult a p * q = smult a (p * q)"
854 by (induct p) (simp add: mult_poly_0, simp add: smult_add_right)
856 lemma mult_smult_right [simp]:
857 "p * smult a q = smult a (p * q)"
858 by (induct q) (simp add: mult_poly_0, simp add: smult_add_right)
860 lemma mult_poly_add_left:
861 fixes p q r :: "'a poly"
862 shows "(p + q) * r = p * r + q * r"
863 by (induct r) (simp add: mult_poly_0, simp add: smult_distribs algebra_simps)
867 fix p q r :: "'a poly"
869 by (rule mult_poly_0_left)
871 by (rule mult_poly_0_right)
872 show "(p + q) * r = p * r + q * r"
873 by (rule mult_poly_add_left)
874 show "(p * q) * r = p * (q * r)"
875 by (induct p, simp add: mult_poly_0, simp add: mult_poly_add_left)
877 by (induct p, simp add: mult_poly_0, simp)
882 instance poly :: (comm_semiring_0_cancel) comm_semiring_0_cancel ..
885 "coeff (p * q) n = (\<Sum>i\<le>n. coeff p i * coeff q (n-i))"
886 proof (induct p arbitrary: n)
887 case 0 show ?case by simp
889 case (pCons a p n) thus ?case
890 by (cases n, simp, simp add: setsum_atMost_Suc_shift
891 del: setsum_atMost_Suc)
894 lemma degree_mult_le: "degree (p * q) \<le> degree p + degree q"
895 apply (rule degree_le)
898 apply (simp add: coeff_eq_0 coeff_pCons split: nat.split)
901 lemma mult_monom: "monom a m * monom b n = monom (a * b) (m + n)"
902 by (induct m) (simp add: monom_0 smult_monom, simp add: monom_Suc)
904 instantiation poly :: (comm_semiring_1) comm_semiring_1
907 definition one_poly_def: "1 = pCons 1 0"
911 show "1 * p = p" for p :: "'a poly"
912 unfolding one_poly_def by simp
913 show "0 \<noteq> (1::'a poly)"
914 unfolding one_poly_def by simp
919 instance poly :: (comm_ring) comm_ring ..
921 instance poly :: (comm_ring_1) comm_ring_1 ..
923 lemma coeff_1 [simp]: "coeff 1 n = (if n = 0 then 1 else 0)"
924 unfolding one_poly_def
925 by (simp add: coeff_pCons split: nat.split)
927 lemma monom_eq_1 [simp]:
929 by (simp add: monom_0 one_poly_def)
931 lemma degree_1 [simp]: "degree 1 = 0"
932 unfolding one_poly_def
933 by (rule degree_pCons_0)
935 lemma coeffs_1_eq [simp, code abstract]:
937 by (simp add: one_poly_def)
939 lemma degree_power_le:
940 "degree (p ^ n) \<le> degree p * n"
941 by (induct n) (auto intro: order_trans degree_mult_le)
943 lemma poly_smult [simp]:
944 "poly (smult a p) x = a * poly p x"
945 by (induct p, simp, simp add: algebra_simps)
947 lemma poly_mult [simp]:
948 "poly (p * q) x = poly p x * poly q x"
949 by (induct p, simp_all, simp add: algebra_simps)
953 by (simp add: one_poly_def)
955 lemma poly_power [simp]:
956 fixes p :: "'a::{comm_semiring_1} poly"
957 shows "poly (p ^ n) x = poly p x ^ n"
958 by (induct n) simp_all
961 subsection \<open>Conversions from natural numbers\<close>
963 lemma of_nat_poly: "of_nat n = [:of_nat n :: 'a :: comm_semiring_1:]"
966 hence "of_nat (Suc n) = 1 + (of_nat n :: 'a poly)"
968 also have "(of_nat n :: 'a poly) = [: of_nat n :]"
969 by (subst Suc) (rule refl)
970 also have "1 = [:1:]" by (simp add: one_poly_def)
971 finally show ?case by (subst (asm) add_pCons) simp
974 lemma degree_of_nat [simp]: "degree (of_nat n) = 0"
975 by (simp add: of_nat_poly)
977 lemma degree_numeral [simp]: "degree (numeral n) = 0"
978 by (subst of_nat_numeral [symmetric], subst of_nat_poly) simp
980 lemma numeral_poly: "numeral n = [:numeral n:]"
981 by (subst of_nat_numeral [symmetric], subst of_nat_poly) simp
983 subsection \<open>Lemmas about divisibility\<close>
985 lemma dvd_smult: "p dvd q \<Longrightarrow> p dvd smult a q"
988 then obtain k where "q = p * k" ..
989 then have "smult a q = p * smult a k" by simp
990 then show "p dvd smult a q" ..
993 lemma dvd_smult_cancel:
994 fixes a :: "'a::field"
995 shows "p dvd smult a q \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> p dvd q"
996 by (drule dvd_smult [where a="inverse a"]) simp
999 fixes a :: "'a::field"
1000 shows "a \<noteq> 0 \<Longrightarrow> p dvd smult a q \<longleftrightarrow> p dvd q"
1001 by (safe elim!: dvd_smult dvd_smult_cancel)
1003 lemma smult_dvd_cancel:
1004 "smult a p dvd q \<Longrightarrow> p dvd q"
1006 assume "smult a p dvd q"
1007 then obtain k where "q = smult a p * k" ..
1008 then have "q = p * smult a k" by simp
1009 then show "p dvd q" ..
1013 fixes a :: "'a::field"
1014 shows "p dvd q \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> smult a p dvd q"
1015 by (rule smult_dvd_cancel [where a="inverse a"]) simp
1017 lemma smult_dvd_iff:
1018 fixes a :: "'a::field"
1019 shows "smult a p dvd q \<longleftrightarrow> (if a = 0 then q = 0 else p dvd q)"
1020 by (auto elim: smult_dvd smult_dvd_cancel)
1023 subsection \<open>Polynomials form an integral domain\<close>
1025 lemma coeff_mult_degree_sum:
1026 "coeff (p * q) (degree p + degree q) =
1027 coeff p (degree p) * coeff q (degree q)"
1028 by (induct p, simp, simp add: coeff_eq_0)
1030 instance poly :: (idom) idom
1032 fix p q :: "'a poly"
1033 assume "p \<noteq> 0" and "q \<noteq> 0"
1034 have "coeff (p * q) (degree p + degree q) =
1035 coeff p (degree p) * coeff q (degree q)"
1036 by (rule coeff_mult_degree_sum)
1037 also have "coeff p (degree p) * coeff q (degree q) \<noteq> 0"
1038 using \<open>p \<noteq> 0\<close> and \<open>q \<noteq> 0\<close> by simp
1039 finally have "\<exists>n. coeff (p * q) n \<noteq> 0" ..
1040 thus "p * q \<noteq> 0" by (simp add: poly_eq_iff)
1043 lemma degree_mult_eq:
1044 fixes p q :: "'a::idom poly"
1045 shows "\<lbrakk>p \<noteq> 0; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree (p * q) = degree p + degree q"
1046 apply (rule order_antisym [OF degree_mult_le le_degree])
1047 apply (simp add: coeff_mult_degree_sum)
1050 lemma degree_mult_right_le:
1051 fixes p q :: "'a::idom poly"
1052 assumes "q \<noteq> 0"
1053 shows "degree p \<le> degree (p * q)"
1054 using assms by (cases "p = 0") (simp_all add: degree_mult_eq)
1056 lemma coeff_degree_mult:
1057 fixes p q :: "'a::idom poly"
1058 shows "coeff (p * q) (degree (p * q)) =
1059 coeff q (degree q) * coeff p (degree p)"
1060 by (cases "p = 0 \<or> q = 0") (auto simp add: degree_mult_eq coeff_mult_degree_sum)
1062 lemma dvd_imp_degree_le:
1063 fixes p q :: "'a::idom poly"
1064 shows "\<lbrakk>p dvd q; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree p \<le> degree q"
1065 by (erule dvdE, simp add: degree_mult_eq)
1068 subsection \<open>Polynomials form an ordered integral domain\<close>
1070 definition pos_poly :: "'a::linordered_idom poly \<Rightarrow> bool"
1072 "pos_poly p \<longleftrightarrow> 0 < coeff p (degree p)"
1074 lemma pos_poly_pCons:
1075 "pos_poly (pCons a p) \<longleftrightarrow> pos_poly p \<or> (p = 0 \<and> 0 < a)"
1076 unfolding pos_poly_def by simp
1078 lemma not_pos_poly_0 [simp]: "\<not> pos_poly 0"
1079 unfolding pos_poly_def by simp
1081 lemma pos_poly_add: "\<lbrakk>pos_poly p; pos_poly q\<rbrakk> \<Longrightarrow> pos_poly (p + q)"
1082 apply (induct p arbitrary: q, simp)
1083 apply (case_tac q, force simp add: pos_poly_pCons add_pos_pos)
1086 lemma pos_poly_mult: "\<lbrakk>pos_poly p; pos_poly q\<rbrakk> \<Longrightarrow> pos_poly (p * q)"
1087 unfolding pos_poly_def
1088 apply (subgoal_tac "p \<noteq> 0 \<and> q \<noteq> 0")
1089 apply (simp add: degree_mult_eq coeff_mult_degree_sum)
1093 lemma pos_poly_total: "p = 0 \<or> pos_poly p \<or> pos_poly (- p)"
1094 by (induct p) (auto simp add: pos_poly_pCons)
1096 lemma last_coeffs_eq_coeff_degree:
1097 "p \<noteq> 0 \<Longrightarrow> last (coeffs p) = coeff p (degree p)"
1098 by (simp add: coeffs_def)
1100 lemma pos_poly_coeffs [code]:
1101 "pos_poly p \<longleftrightarrow> (let as = coeffs p in as \<noteq> [] \<and> last as > 0)" (is "?P \<longleftrightarrow> ?Q")
1103 assume ?Q then show ?P by (auto simp add: pos_poly_def last_coeffs_eq_coeff_degree)
1105 assume ?P then have *: "0 < coeff p (degree p)" by (simp add: pos_poly_def)
1106 then have "p \<noteq> 0" by auto
1107 with * show ?Q by (simp add: last_coeffs_eq_coeff_degree)
1110 instantiation poly :: (linordered_idom) linordered_idom
1114 "x < y \<longleftrightarrow> pos_poly (y - x)"
1117 "x \<le> y \<longleftrightarrow> x = y \<or> pos_poly (y - x)"
1120 "\<bar>x::'a poly\<bar> = (if x < 0 then - x else x)"
1123 "sgn (x::'a poly) = (if x = 0 then 0 else if 0 < x then 1 else - 1)"
1127 fix x y z :: "'a poly"
1128 show "x < y \<longleftrightarrow> x \<le> y \<and> \<not> y \<le> x"
1129 unfolding less_eq_poly_def less_poly_def
1132 apply (drule (1) pos_poly_add)
1136 unfolding less_eq_poly_def by simp
1137 show "x \<le> y \<Longrightarrow> y \<le> z \<Longrightarrow> x \<le> z"
1138 unfolding less_eq_poly_def
1140 apply (drule (1) pos_poly_add)
1141 apply (simp add: algebra_simps)
1143 show "x \<le> y \<Longrightarrow> y \<le> x \<Longrightarrow> x = y"
1144 unfolding less_eq_poly_def
1146 apply (drule (1) pos_poly_add)
1149 show "x \<le> y \<Longrightarrow> z + x \<le> z + y"
1150 unfolding less_eq_poly_def
1152 apply (simp add: algebra_simps)
1154 show "x \<le> y \<or> y \<le> x"
1155 unfolding less_eq_poly_def
1156 using pos_poly_total [of "x - y"]
1158 show "x < y \<Longrightarrow> 0 < z \<Longrightarrow> z * x < z * y"
1159 unfolding less_poly_def
1160 by (simp add: right_diff_distrib [symmetric] pos_poly_mult)
1161 show "\<bar>x\<bar> = (if x < 0 then - x else x)"
1162 by (rule abs_poly_def)
1163 show "sgn x = (if x = 0 then 0 else if 0 < x then 1 else - 1)"
1164 by (rule sgn_poly_def)
1169 text \<open>TODO: Simplification rules for comparisons\<close>
1172 subsection \<open>Synthetic division and polynomial roots\<close>
1175 Synthetic division is simply division by the linear polynomial @{term "x - c"}.
1178 definition synthetic_divmod :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly \<times> 'a"
1180 "synthetic_divmod p c = fold_coeffs (\<lambda>a (q, r). (pCons r q, a + c * r)) p (0, 0)"
1182 definition synthetic_div :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly"
1184 "synthetic_div p c = fst (synthetic_divmod p c)"
1186 lemma synthetic_divmod_0 [simp]:
1187 "synthetic_divmod 0 c = (0, 0)"
1188 by (simp add: synthetic_divmod_def)
1190 lemma synthetic_divmod_pCons [simp]:
1191 "synthetic_divmod (pCons a p) c = (\<lambda>(q, r). (pCons r q, a + c * r)) (synthetic_divmod p c)"
1192 by (cases "p = 0 \<and> a = 0") (auto simp add: synthetic_divmod_def)
1194 lemma synthetic_div_0 [simp]:
1195 "synthetic_div 0 c = 0"
1196 unfolding synthetic_div_def by simp
1198 lemma synthetic_div_unique_lemma: "smult c p = pCons a p \<Longrightarrow> p = 0"
1199 by (induct p arbitrary: a) simp_all
1201 lemma snd_synthetic_divmod:
1202 "snd (synthetic_divmod p c) = poly p c"
1203 by (induct p, simp, simp add: split_def)
1205 lemma synthetic_div_pCons [simp]:
1206 "synthetic_div (pCons a p) c = pCons (poly p c) (synthetic_div p c)"
1207 unfolding synthetic_div_def
1208 by (simp add: split_def snd_synthetic_divmod)
1210 lemma synthetic_div_eq_0_iff:
1211 "synthetic_div p c = 0 \<longleftrightarrow> degree p = 0"
1212 by (induct p, simp, case_tac p, simp)
1214 lemma degree_synthetic_div:
1215 "degree (synthetic_div p c) = degree p - 1"
1216 by (induct p, simp, simp add: synthetic_div_eq_0_iff)
1218 lemma synthetic_div_correct:
1219 "p + smult c (synthetic_div p c) = pCons (poly p c) (synthetic_div p c)"
1220 by (induct p) simp_all
1222 lemma synthetic_div_unique:
1223 "p + smult c q = pCons r q \<Longrightarrow> r = poly p c \<and> q = synthetic_div p c"
1224 apply (induct p arbitrary: q r)
1225 apply (simp, frule synthetic_div_unique_lemma, simp)
1226 apply (case_tac q, force)
1229 lemma synthetic_div_correct':
1230 fixes c :: "'a::comm_ring_1"
1231 shows "[:-c, 1:] * synthetic_div p c + [:poly p c:] = p"
1232 using synthetic_div_correct [of p c]
1233 by (simp add: algebra_simps)
1235 lemma poly_eq_0_iff_dvd:
1236 fixes c :: "'a::idom"
1237 shows "poly p c = 0 \<longleftrightarrow> [:-c, 1:] dvd p"
1239 assume "poly p c = 0"
1240 with synthetic_div_correct' [of c p]
1241 have "p = [:-c, 1:] * synthetic_div p c" by simp
1242 then show "[:-c, 1:] dvd p" ..
1244 assume "[:-c, 1:] dvd p"
1245 then obtain k where "p = [:-c, 1:] * k" by (rule dvdE)
1246 then show "poly p c = 0" by simp
1249 lemma dvd_iff_poly_eq_0:
1250 fixes c :: "'a::idom"
1251 shows "[:c, 1:] dvd p \<longleftrightarrow> poly p (-c) = 0"
1252 by (simp add: poly_eq_0_iff_dvd)
1254 lemma poly_roots_finite:
1255 fixes p :: "'a::idom poly"
1256 shows "p \<noteq> 0 \<Longrightarrow> finite {x. poly p x = 0}"
1257 proof (induct n \<equiv> "degree p" arbitrary: p)
1259 then obtain a where "a \<noteq> 0" and "p = [:a:]"
1260 by (cases p, simp split: if_splits)
1261 then show "finite {x. poly p x = 0}" by simp
1264 show "finite {x. poly p x = 0}"
1265 proof (cases "\<exists>x. poly p x = 0")
1267 then show "finite {x. poly p x = 0}" by simp
1270 then obtain a where "poly p a = 0" ..
1271 then have "[:-a, 1:] dvd p" by (simp only: poly_eq_0_iff_dvd)
1272 then obtain k where k: "p = [:-a, 1:] * k" ..
1273 with \<open>p \<noteq> 0\<close> have "k \<noteq> 0" by auto
1274 with k have "degree p = Suc (degree k)"
1275 by (simp add: degree_mult_eq del: mult_pCons_left)
1276 with \<open>Suc n = degree p\<close> have "n = degree k" by simp
1277 then have "finite {x. poly k x = 0}" using \<open>k \<noteq> 0\<close> by (rule Suc.hyps)
1278 then have "finite (insert a {x. poly k x = 0})" by simp
1279 then show "finite {x. poly p x = 0}"
1280 by (simp add: k Collect_disj_eq del: mult_pCons_left)
1284 lemma poly_eq_poly_eq_iff:
1285 fixes p q :: "'a::{idom,ring_char_0} poly"
1286 shows "poly p = poly q \<longleftrightarrow> p = q" (is "?P \<longleftrightarrow> ?Q")
1288 assume ?Q then show ?P by simp
1290 { fix p :: "'a::{idom,ring_char_0} poly"
1291 have "poly p = poly 0 \<longleftrightarrow> p = 0"
1292 apply (cases "p = 0", simp_all)
1293 apply (drule poly_roots_finite)
1294 apply (auto simp add: infinite_UNIV_char_0)
1296 } note this [of "p - q"]
1298 ultimately show ?Q by auto
1301 lemma poly_all_0_iff_0:
1302 fixes p :: "'a::{ring_char_0, idom} poly"
1303 shows "(\<forall>x. poly p x = 0) \<longleftrightarrow> p = 0"
1304 by (auto simp add: poly_eq_poly_eq_iff [symmetric])
1307 subsection \<open>Long division of polynomials\<close>
1309 definition pdivmod_rel :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> bool"
1311 "pdivmod_rel x y q r \<longleftrightarrow>
1312 x = q * y + r \<and> (if y = 0 then q = 0 else r = 0 \<or> degree r < degree y)"
1314 lemma pdivmod_rel_0:
1315 "pdivmod_rel 0 y 0 0"
1316 unfolding pdivmod_rel_def by simp
1318 lemma pdivmod_rel_by_0:
1319 "pdivmod_rel x 0 0 x"
1320 unfolding pdivmod_rel_def by simp
1322 lemma eq_zero_or_degree_less:
1323 assumes "degree p \<le> n" and "coeff p n = 0"
1324 shows "p = 0 \<or> degree p < n"
1327 with \<open>degree p \<le> n\<close> and \<open>coeff p n = 0\<close>
1328 have "coeff p (degree p) = 0" by simp
1329 then have "p = 0" by simp
1330 then show ?thesis ..
1333 have "\<forall>i>n. coeff p i = 0"
1334 using \<open>degree p \<le> n\<close> by (simp add: coeff_eq_0)
1335 then have "\<forall>i\<ge>n. coeff p i = 0"
1336 using \<open>coeff p n = 0\<close> by (simp add: le_less)
1337 then have "\<forall>i>m. coeff p i = 0"
1338 using \<open>n = Suc m\<close> by (simp add: less_eq_Suc_le)
1339 then have "degree p \<le> m"
1341 then have "degree p < n"
1342 using \<open>n = Suc m\<close> by (simp add: less_Suc_eq_le)
1343 then show ?thesis ..
1346 lemma pdivmod_rel_pCons:
1347 assumes rel: "pdivmod_rel x y q r"
1348 assumes y: "y \<noteq> 0"
1349 assumes b: "b = coeff (pCons a r) (degree y) / coeff y (degree y)"
1350 shows "pdivmod_rel (pCons a x) y (pCons b q) (pCons a r - smult b y)"
1351 (is "pdivmod_rel ?x y ?q ?r")
1353 have x: "x = q * y + r" and r: "r = 0 \<or> degree r < degree y"
1354 using assms unfolding pdivmod_rel_def by simp_all
1356 have 1: "?x = ?q * y + ?r"
1359 have 2: "?r = 0 \<or> degree ?r < degree y"
1360 proof (rule eq_zero_or_degree_less)
1361 show "degree ?r \<le> degree y"
1362 proof (rule degree_diff_le)
1363 show "degree (pCons a r) \<le> degree y"
1365 show "degree (smult b y) \<le> degree y"
1366 by (rule degree_smult_le)
1369 show "coeff ?r (degree y) = 0"
1370 using \<open>y \<noteq> 0\<close> unfolding b by simp
1373 from 1 2 show ?thesis
1374 unfolding pdivmod_rel_def
1375 using \<open>y \<noteq> 0\<close> by simp
1378 lemma pdivmod_rel_exists: "\<exists>q r. pdivmod_rel x y q r"
1379 apply (cases "y = 0")
1380 apply (fast intro!: pdivmod_rel_by_0)
1382 apply (fast intro!: pdivmod_rel_0)
1383 apply (fast intro!: pdivmod_rel_pCons)
1386 lemma pdivmod_rel_unique:
1387 assumes 1: "pdivmod_rel x y q1 r1"
1388 assumes 2: "pdivmod_rel x y q2 r2"
1389 shows "q1 = q2 \<and> r1 = r2"
1390 proof (cases "y = 0")
1391 assume "y = 0" with assms show ?thesis
1392 by (simp add: pdivmod_rel_def)
1394 assume [simp]: "y \<noteq> 0"
1395 from 1 have q1: "x = q1 * y + r1" and r1: "r1 = 0 \<or> degree r1 < degree y"
1396 unfolding pdivmod_rel_def by simp_all
1397 from 2 have q2: "x = q2 * y + r2" and r2: "r2 = 0 \<or> degree r2 < degree y"
1398 unfolding pdivmod_rel_def by simp_all
1399 from q1 q2 have q3: "(q1 - q2) * y = r2 - r1"
1400 by (simp add: algebra_simps)
1401 from r1 r2 have r3: "(r2 - r1) = 0 \<or> degree (r2 - r1) < degree y"
1402 by (auto intro: degree_diff_less)
1404 show "q1 = q2 \<and> r1 = r2"
1406 assume "\<not> (q1 = q2 \<and> r1 = r2)"
1407 with q3 have "q1 \<noteq> q2" and "r1 \<noteq> r2" by auto
1408 with r3 have "degree (r2 - r1) < degree y" by simp
1409 also have "degree y \<le> degree (q1 - q2) + degree y" by simp
1410 also have "\<dots> = degree ((q1 - q2) * y)"
1411 using \<open>q1 \<noteq> q2\<close> by (simp add: degree_mult_eq)
1412 also have "\<dots> = degree (r2 - r1)"
1414 finally have "degree (r2 - r1) < degree (r2 - r1)" .
1415 then show "False" by simp
1419 lemma pdivmod_rel_0_iff: "pdivmod_rel 0 y q r \<longleftrightarrow> q = 0 \<and> r = 0"
1420 by (auto dest: pdivmod_rel_unique intro: pdivmod_rel_0)
1422 lemma pdivmod_rel_by_0_iff: "pdivmod_rel x 0 q r \<longleftrightarrow> q = 0 \<and> r = x"
1423 by (auto dest: pdivmod_rel_unique intro: pdivmod_rel_by_0)
1425 lemmas pdivmod_rel_unique_div = pdivmod_rel_unique [THEN conjunct1]
1427 lemmas pdivmod_rel_unique_mod = pdivmod_rel_unique [THEN conjunct2]
1429 instantiation poly :: (field) ring_div
1432 definition divide_poly where
1433 div_poly_def: "x div y = (THE q. \<exists>r. pdivmod_rel x y q r)"
1435 definition mod_poly where
1436 "x mod y = (THE r. \<exists>q. pdivmod_rel x y q r)"
1439 "pdivmod_rel x y q r \<Longrightarrow> x div y = q"
1440 unfolding div_poly_def
1441 by (fast elim: pdivmod_rel_unique_div)
1444 "pdivmod_rel x y q r \<Longrightarrow> x mod y = r"
1445 unfolding mod_poly_def
1446 by (fast elim: pdivmod_rel_unique_mod)
1449 "pdivmod_rel x y (x div y) (x mod y)"
1451 from pdivmod_rel_exists
1452 obtain q r where "pdivmod_rel x y q r" by fast
1454 by (simp add: div_poly_eq mod_poly_eq)
1459 fix x y :: "'a poly"
1460 show "x div y * y + x mod y = x"
1461 using pdivmod_rel [of x y]
1462 by (simp add: pdivmod_rel_def)
1465 have "pdivmod_rel x 0 0 x"
1466 by (rule pdivmod_rel_by_0)
1468 by (rule div_poly_eq)
1471 have "pdivmod_rel 0 y 0 0"
1472 by (rule pdivmod_rel_0)
1474 by (rule div_poly_eq)
1476 fix x y z :: "'a poly"
1477 assume "y \<noteq> 0"
1478 hence "pdivmod_rel (x + z * y) y (z + x div y) (x mod y)"
1479 using pdivmod_rel [of x y]
1480 by (simp add: pdivmod_rel_def distrib_right)
1481 thus "(x + z * y) div y = z + x div y"
1482 by (rule div_poly_eq)
1484 fix x y z :: "'a poly"
1485 assume "x \<noteq> 0"
1486 show "(x * y) div (x * z) = y div z"
1487 proof (cases "y \<noteq> 0 \<and> z \<noteq> 0")
1488 have "\<And>x::'a poly. pdivmod_rel x 0 0 x"
1489 by (rule pdivmod_rel_by_0)
1490 then have [simp]: "\<And>x::'a poly. x div 0 = 0"
1491 by (rule div_poly_eq)
1492 have "\<And>x::'a poly. pdivmod_rel 0 x 0 0"
1493 by (rule pdivmod_rel_0)
1494 then have [simp]: "\<And>x::'a poly. 0 div x = 0"
1495 by (rule div_poly_eq)
1496 case False then show ?thesis by auto
1498 case True then have "y \<noteq> 0" and "z \<noteq> 0" by auto
1499 with \<open>x \<noteq> 0\<close>
1500 have "\<And>q r. pdivmod_rel y z q r \<Longrightarrow> pdivmod_rel (x * y) (x * z) q (x * r)"
1501 by (auto simp add: pdivmod_rel_def algebra_simps)
1502 (rule classical, simp add: degree_mult_eq)
1503 moreover from pdivmod_rel have "pdivmod_rel y z (y div z) (y mod z)" .
1504 ultimately have "pdivmod_rel (x * y) (x * z) (y div z) (x * (y mod z))" .
1505 then show ?thesis by (simp add: div_poly_eq)
1511 lemma is_unit_monom_0:
1512 fixes a :: "'a::field"
1513 assumes "a \<noteq> 0"
1514 shows "is_unit (monom a 0)"
1516 from assms show "1 = monom a 0 * monom (1 / a) 0"
1517 by (simp add: mult_monom)
1521 fixes a :: "'a::field"
1522 assumes "a \<noteq> 0"
1523 shows "is_unit [:a:]"
1524 using assms by (simp add: is_unit_monom_0 monom_0 [symmetric])
1526 lemma is_unit_iff_degree:
1527 assumes "p \<noteq> 0"
1528 shows "is_unit p \<longleftrightarrow> degree p = 0" (is "?P \<longleftrightarrow> ?Q")
1531 then obtain a where "p = [:a:]" by (rule degree_eq_zeroE)
1532 with assms show ?P by (simp add: is_unit_triv)
1535 then obtain q where "q \<noteq> 0" "p * q = 1" ..
1536 then have "degree (p * q) = degree 1"
1538 with \<open>p \<noteq> 0\<close> \<open>q \<noteq> 0\<close> have "degree p + degree q = 0"
1539 by (simp add: degree_mult_eq)
1540 then show ?Q by simp
1543 lemma is_unit_pCons_iff:
1544 "is_unit (pCons a p) \<longleftrightarrow> p = 0 \<and> a \<noteq> 0" (is "?P \<longleftrightarrow> ?Q")
1545 by (cases "p = 0") (auto simp add: is_unit_triv is_unit_iff_degree)
1547 lemma is_unit_monom_trival:
1548 fixes p :: "'a::field poly"
1550 shows "monom (coeff p (degree p)) 0 = p"
1551 using assms by (cases p) (simp_all add: monom_0 is_unit_pCons_iff)
1553 lemma is_unit_polyE:
1555 obtains a where "p = monom a 0" and "a \<noteq> 0"
1557 obtain a q where "p = pCons a q" by (cases p)
1558 with assms have "p = [:a:]" and "a \<noteq> 0"
1559 by (simp_all add: is_unit_pCons_iff)
1560 with that show thesis by (simp add: monom_0)
1563 instantiation poly :: (field) normalization_semidom
1566 definition normalize_poly :: "'a poly \<Rightarrow> 'a poly"
1567 where "normalize_poly p = smult (1 / coeff p (degree p)) p"
1569 definition unit_factor_poly :: "'a poly \<Rightarrow> 'a poly"
1570 where "unit_factor_poly p = monom (coeff p (degree p)) 0"
1575 show "unit_factor p * normalize p = p"
1576 by (simp add: normalize_poly_def unit_factor_poly_def)
1577 (simp only: mult_smult_left [symmetric] smult_monom, simp)
1579 show "normalize 0 = (0::'a poly)"
1580 by (simp add: normalize_poly_def)
1582 show "unit_factor 0 = (0::'a poly)"
1583 by (simp add: unit_factor_poly_def)
1587 then obtain a where "p = monom a 0" and "a \<noteq> 0"
1588 by (rule is_unit_polyE)
1589 then show "normalize p = 1"
1590 by (auto simp add: normalize_poly_def smult_monom degree_monom_eq)
1592 fix p q :: "'a poly"
1593 assume "q \<noteq> 0"
1594 from \<open>q \<noteq> 0\<close> have "is_unit (monom (coeff q (degree q)) 0)"
1595 by (auto intro: is_unit_monom_0)
1596 then show "is_unit (unit_factor q)"
1597 by (simp add: unit_factor_poly_def)
1599 fix p q :: "'a poly"
1600 have "monom (coeff (p * q) (degree (p * q))) 0 =
1601 monom (coeff p (degree p)) 0 * monom (coeff q (degree q)) 0"
1602 by (simp add: monom_0 coeff_degree_mult)
1603 then show "unit_factor (p * q) =
1604 unit_factor p * unit_factor q"
1605 by (simp add: unit_factor_poly_def)
1610 lemma degree_mod_less:
1611 "y \<noteq> 0 \<Longrightarrow> x mod y = 0 \<or> degree (x mod y) < degree y"
1612 using pdivmod_rel [of x y]
1613 unfolding pdivmod_rel_def by simp
1615 lemma div_poly_less: "degree x < degree y \<Longrightarrow> x div y = 0"
1617 assume "degree x < degree y"
1618 hence "pdivmod_rel x y 0 x"
1619 by (simp add: pdivmod_rel_def)
1620 thus "x div y = 0" by (rule div_poly_eq)
1623 lemma mod_poly_less: "degree x < degree y \<Longrightarrow> x mod y = x"
1625 assume "degree x < degree y"
1626 hence "pdivmod_rel x y 0 x"
1627 by (simp add: pdivmod_rel_def)
1628 thus "x mod y = x" by (rule mod_poly_eq)
1631 lemma pdivmod_rel_smult_left:
1632 "pdivmod_rel x y q r
1633 \<Longrightarrow> pdivmod_rel (smult a x) y (smult a q) (smult a r)"
1634 unfolding pdivmod_rel_def by (simp add: smult_add_right)
1636 lemma div_smult_left: "(smult a x) div y = smult a (x div y)"
1637 by (rule div_poly_eq, rule pdivmod_rel_smult_left, rule pdivmod_rel)
1639 lemma mod_smult_left: "(smult a x) mod y = smult a (x mod y)"
1640 by (rule mod_poly_eq, rule pdivmod_rel_smult_left, rule pdivmod_rel)
1642 lemma poly_div_minus_left [simp]:
1643 fixes x y :: "'a::field poly"
1644 shows "(- x) div y = - (x div y)"
1645 using div_smult_left [of "- 1::'a"] by simp
1647 lemma poly_mod_minus_left [simp]:
1648 fixes x y :: "'a::field poly"
1649 shows "(- x) mod y = - (x mod y)"
1650 using mod_smult_left [of "- 1::'a"] by simp
1652 lemma pdivmod_rel_add_left:
1653 assumes "pdivmod_rel x y q r"
1654 assumes "pdivmod_rel x' y q' r'"
1655 shows "pdivmod_rel (x + x') y (q + q') (r + r')"
1656 using assms unfolding pdivmod_rel_def
1657 by (auto simp add: algebra_simps degree_add_less)
1659 lemma poly_div_add_left:
1660 fixes x y z :: "'a::field poly"
1661 shows "(x + y) div z = x div z + y div z"
1662 using pdivmod_rel_add_left [OF pdivmod_rel pdivmod_rel]
1663 by (rule div_poly_eq)
1665 lemma poly_mod_add_left:
1666 fixes x y z :: "'a::field poly"
1667 shows "(x + y) mod z = x mod z + y mod z"
1668 using pdivmod_rel_add_left [OF pdivmod_rel pdivmod_rel]
1669 by (rule mod_poly_eq)
1671 lemma poly_div_diff_left:
1672 fixes x y z :: "'a::field poly"
1673 shows "(x - y) div z = x div z - y div z"
1674 by (simp only: diff_conv_add_uminus poly_div_add_left poly_div_minus_left)
1676 lemma poly_mod_diff_left:
1677 fixes x y z :: "'a::field poly"
1678 shows "(x - y) mod z = x mod z - y mod z"
1679 by (simp only: diff_conv_add_uminus poly_mod_add_left poly_mod_minus_left)
1681 lemma pdivmod_rel_smult_right:
1682 "\<lbrakk>a \<noteq> 0; pdivmod_rel x y q r\<rbrakk>
1683 \<Longrightarrow> pdivmod_rel x (smult a y) (smult (inverse a) q) r"
1684 unfolding pdivmod_rel_def by simp
1686 lemma div_smult_right:
1687 "a \<noteq> 0 \<Longrightarrow> x div (smult a y) = smult (inverse a) (x div y)"
1688 by (rule div_poly_eq, erule pdivmod_rel_smult_right, rule pdivmod_rel)
1690 lemma mod_smult_right: "a \<noteq> 0 \<Longrightarrow> x mod (smult a y) = x mod y"
1691 by (rule mod_poly_eq, erule pdivmod_rel_smult_right, rule pdivmod_rel)
1693 lemma poly_div_minus_right [simp]:
1694 fixes x y :: "'a::field poly"
1695 shows "x div (- y) = - (x div y)"
1696 using div_smult_right [of "- 1::'a"] by (simp add: nonzero_inverse_minus_eq)
1698 lemma poly_mod_minus_right [simp]:
1699 fixes x y :: "'a::field poly"
1700 shows "x mod (- y) = x mod y"
1701 using mod_smult_right [of "- 1::'a"] by simp
1703 lemma pdivmod_rel_mult:
1704 "\<lbrakk>pdivmod_rel x y q r; pdivmod_rel q z q' r'\<rbrakk>
1705 \<Longrightarrow> pdivmod_rel x (y * z) q' (y * r' + r)"
1706 apply (cases "z = 0", simp add: pdivmod_rel_def)
1707 apply (cases "y = 0", simp add: pdivmod_rel_by_0_iff pdivmod_rel_0_iff)
1708 apply (cases "r = 0")
1709 apply (cases "r' = 0")
1710 apply (simp add: pdivmod_rel_def)
1711 apply (simp add: pdivmod_rel_def field_simps degree_mult_eq)
1712 apply (cases "r' = 0")
1713 apply (simp add: pdivmod_rel_def degree_mult_eq)
1714 apply (simp add: pdivmod_rel_def field_simps)
1715 apply (simp add: degree_mult_eq degree_add_less)
1718 lemma poly_div_mult_right:
1719 fixes x y z :: "'a::field poly"
1720 shows "x div (y * z) = (x div y) div z"
1721 by (rule div_poly_eq, rule pdivmod_rel_mult, (rule pdivmod_rel)+)
1723 lemma poly_mod_mult_right:
1724 fixes x y z :: "'a::field poly"
1725 shows "x mod (y * z) = y * (x div y mod z) + x mod y"
1726 by (rule mod_poly_eq, rule pdivmod_rel_mult, (rule pdivmod_rel)+)
1730 assumes y: "y \<noteq> 0"
1731 defines b: "b \<equiv> coeff (pCons a (x mod y)) (degree y) / coeff y (degree y)"
1732 shows "(pCons a x) mod y = (pCons a (x mod y) - smult b y)"
1734 apply (rule mod_poly_eq)
1735 apply (rule pdivmod_rel_pCons [OF pdivmod_rel y refl])
1738 definition pdivmod :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<times> 'a poly"
1740 "pdivmod p q = (p div q, p mod q)"
1742 lemma div_poly_code [code]:
1743 "p div q = fst (pdivmod p q)"
1744 by (simp add: pdivmod_def)
1746 lemma mod_poly_code [code]:
1747 "p mod q = snd (pdivmod p q)"
1748 by (simp add: pdivmod_def)
1751 "pdivmod 0 q = (0, 0)"
1752 by (simp add: pdivmod_def)
1754 lemma pdivmod_pCons:
1755 "pdivmod (pCons a p) q =
1756 (if q = 0 then (0, pCons a p) else
1757 (let (s, r) = pdivmod p q;
1758 b = coeff (pCons a r) (degree q) / coeff q (degree q)
1759 in (pCons b s, pCons a r - smult b q)))"
1760 apply (simp add: pdivmod_def Let_def, safe)
1761 apply (rule div_poly_eq)
1762 apply (erule pdivmod_rel_pCons [OF pdivmod_rel _ refl])
1763 apply (rule mod_poly_eq)
1764 apply (erule pdivmod_rel_pCons [OF pdivmod_rel _ refl])
1767 lemma pdivmod_fold_coeffs [code]:
1768 "pdivmod p q = (if q = 0 then (0, p)
1769 else fold_coeffs (\<lambda>a (s, r).
1770 let b = coeff (pCons a r) (degree q) / coeff q (degree q)
1771 in (pCons b s, pCons a r - smult b q)
1773 apply (cases "q = 0")
1774 apply (simp add: pdivmod_def)
1777 apply (simp_all add: pdivmod_0 pdivmod_pCons)
1778 apply (case_tac "a = 0 \<and> p = 0")
1779 apply (auto simp add: pdivmod_def)
1783 subsection \<open>Order of polynomial roots\<close>
1785 definition order :: "'a::idom \<Rightarrow> 'a poly \<Rightarrow> nat"
1787 "order a p = (LEAST n. \<not> [:-a, 1:] ^ Suc n dvd p)"
1789 lemma coeff_linear_power:
1790 fixes a :: "'a::comm_semiring_1"
1791 shows "coeff ([:a, 1:] ^ n) n = 1"
1792 apply (induct n, simp_all)
1793 apply (subst coeff_eq_0)
1794 apply (auto intro: le_less_trans degree_power_le)
1797 lemma degree_linear_power:
1798 fixes a :: "'a::comm_semiring_1"
1799 shows "degree ([:a, 1:] ^ n) = n"
1800 apply (rule order_antisym)
1801 apply (rule ord_le_eq_trans [OF degree_power_le], simp)
1802 apply (rule le_degree, simp add: coeff_linear_power)
1805 lemma order_1: "[:-a, 1:] ^ order a p dvd p"
1806 apply (cases "p = 0", simp)
1807 apply (cases "order a p", simp)
1808 apply (subgoal_tac "nat < (LEAST n. \<not> [:-a, 1:] ^ Suc n dvd p)")
1809 apply (drule not_less_Least, simp)
1810 apply (fold order_def, simp)
1813 lemma order_2: "p \<noteq> 0 \<Longrightarrow> \<not> [:-a, 1:] ^ Suc (order a p) dvd p"
1815 apply (rule LeastI_ex)
1816 apply (rule_tac x="degree p" in exI)
1818 apply (drule (1) dvd_imp_degree_le)
1819 apply (simp only: degree_linear_power)
1823 "p \<noteq> 0 \<Longrightarrow> [:-a, 1:] ^ order a p dvd p \<and> \<not> [:-a, 1:] ^ Suc (order a p) dvd p"
1824 by (rule conjI [OF order_1 order_2])
1827 assumes p: "p \<noteq> 0"
1828 shows "order a p \<le> degree p"
1830 have "order a p = degree ([:-a, 1:] ^ order a p)"
1831 by (simp only: degree_linear_power)
1832 also have "\<dots> \<le> degree p"
1833 using order_1 p by (rule dvd_imp_degree_le)
1834 finally show ?thesis .
1837 lemma order_root: "poly p a = 0 \<longleftrightarrow> p = 0 \<or> order a p \<noteq> 0"
1838 apply (cases "p = 0", simp_all)
1840 apply (metis order_2 not_gr0 poly_eq_0_iff_dvd power_0 power_Suc_0 power_one_right)
1841 unfolding poly_eq_0_iff_dvd
1842 apply (metis dvd_power dvd_trans order_1)
1845 lemma order_0I: "poly p a \<noteq> 0 \<Longrightarrow> order a p = 0"
1846 by (subst (asm) order_root) auto
1849 subsection \<open>GCD of polynomials\<close>
1851 instantiation poly :: (field) gcd
1854 function gcd_poly :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
1856 "gcd (x::'a poly) 0 = smult (inverse (coeff x (degree x))) x"
1857 | "y \<noteq> 0 \<Longrightarrow> gcd (x::'a poly) y = gcd y (x mod y)"
1860 termination "gcd :: _ poly \<Rightarrow> _"
1861 by (relation "measure (\<lambda>(x, y). if y = 0 then 0 else Suc (degree y))")
1862 (auto dest: degree_mod_less)
1864 declare gcd_poly.simps [simp del]
1866 definition lcm_poly :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
1868 "lcm_poly a b = a * b div smult (coeff a (degree a) * coeff b (degree b)) (gcd a b)"
1875 fixes x y :: "_ poly"
1876 shows poly_gcd_dvd1 [iff]: "gcd x y dvd x"
1877 and poly_gcd_dvd2 [iff]: "gcd x y dvd y"
1878 apply (induct x y rule: gcd_poly.induct)
1879 apply (simp_all add: gcd_poly.simps)
1880 apply (fastforce simp add: smult_dvd_iff dest: inverse_zero_imp_zero)
1881 apply (blast dest: dvd_mod_imp_dvd)
1884 lemma poly_gcd_greatest:
1885 fixes k x y :: "_ poly"
1886 shows "k dvd x \<Longrightarrow> k dvd y \<Longrightarrow> k dvd gcd x y"
1887 by (induct x y rule: gcd_poly.induct)
1888 (simp_all add: gcd_poly.simps dvd_mod dvd_smult)
1890 lemma dvd_poly_gcd_iff [iff]:
1891 fixes k x y :: "_ poly"
1892 shows "k dvd gcd x y \<longleftrightarrow> k dvd x \<and> k dvd y"
1893 by (auto intro!: poly_gcd_greatest intro: dvd_trans [of _ "gcd x y"])
1895 lemma poly_gcd_monic:
1896 fixes x y :: "_ poly"
1897 shows "coeff (gcd x y) (degree (gcd x y)) =
1898 (if x = 0 \<and> y = 0 then 0 else 1)"
1899 by (induct x y rule: gcd_poly.induct)
1900 (simp_all add: gcd_poly.simps nonzero_imp_inverse_nonzero)
1902 lemma poly_gcd_zero_iff [simp]:
1903 fixes x y :: "_ poly"
1904 shows "gcd x y = 0 \<longleftrightarrow> x = 0 \<and> y = 0"
1905 by (simp only: dvd_0_left_iff [symmetric] dvd_poly_gcd_iff)
1907 lemma poly_gcd_0_0 [simp]:
1908 "gcd (0::_ poly) 0 = 0"
1911 lemma poly_dvd_antisym:
1912 fixes p q :: "'a::idom poly"
1913 assumes coeff: "coeff p (degree p) = coeff q (degree q)"
1914 assumes dvd1: "p dvd q" and dvd2: "q dvd p" shows "p = q"
1915 proof (cases "p = 0")
1916 case True with coeff show "p = q" by simp
1918 case False with coeff have "q \<noteq> 0" by auto
1919 have degree: "degree p = degree q"
1920 using \<open>p dvd q\<close> \<open>q dvd p\<close> \<open>p \<noteq> 0\<close> \<open>q \<noteq> 0\<close>
1921 by (intro order_antisym dvd_imp_degree_le)
1923 from \<open>p dvd q\<close> obtain a where a: "q = p * a" ..
1924 with \<open>q \<noteq> 0\<close> have "a \<noteq> 0" by auto
1925 with degree a \<open>p \<noteq> 0\<close> have "degree a = 0"
1926 by (simp add: degree_mult_eq)
1927 with coeff a show "p = q"
1928 by (cases a, auto split: if_splits)
1931 lemma poly_gcd_unique:
1932 fixes d x y :: "_ poly"
1933 assumes dvd1: "d dvd x" and dvd2: "d dvd y"
1934 and greatest: "\<And>k. k dvd x \<Longrightarrow> k dvd y \<Longrightarrow> k dvd d"
1935 and monic: "coeff d (degree d) = (if x = 0 \<and> y = 0 then 0 else 1)"
1938 have "coeff (gcd x y) (degree (gcd x y)) = coeff d (degree d)"
1939 by (simp_all add: poly_gcd_monic monic)
1940 moreover have "gcd x y dvd d"
1941 using poly_gcd_dvd1 poly_gcd_dvd2 by (rule greatest)
1942 moreover have "d dvd gcd x y"
1943 using dvd1 dvd2 by (rule poly_gcd_greatest)
1944 ultimately show ?thesis
1945 by (rule poly_dvd_antisym)
1948 interpretation gcd_poly: abel_semigroup "gcd :: _ poly \<Rightarrow> _"
1950 fix x y z :: "'a poly"
1951 show "gcd (gcd x y) z = gcd x (gcd y z)"
1952 by (rule poly_gcd_unique) (auto intro: dvd_trans simp add: poly_gcd_monic)
1953 show "gcd x y = gcd y x"
1954 by (rule poly_gcd_unique) (simp_all add: poly_gcd_monic)
1957 lemmas poly_gcd_assoc = gcd_poly.assoc
1958 lemmas poly_gcd_commute = gcd_poly.commute
1959 lemmas poly_gcd_left_commute = gcd_poly.left_commute
1961 lemmas poly_gcd_ac = poly_gcd_assoc poly_gcd_commute poly_gcd_left_commute
1963 lemma poly_gcd_1_left [simp]: "gcd 1 y = (1 :: _ poly)"
1964 by (rule poly_gcd_unique) simp_all
1966 lemma poly_gcd_1_right [simp]: "gcd x 1 = (1 :: _ poly)"
1967 by (rule poly_gcd_unique) simp_all
1969 lemma poly_gcd_minus_left [simp]: "gcd (- x) y = gcd x (y :: _ poly)"
1970 by (rule poly_gcd_unique) (simp_all add: poly_gcd_monic)
1972 lemma poly_gcd_minus_right [simp]: "gcd x (- y) = gcd x (y :: _ poly)"
1973 by (rule poly_gcd_unique) (simp_all add: poly_gcd_monic)
1975 lemma poly_gcd_code [code]:
1976 "gcd x y = (if y = 0 then smult (inverse (coeff x (degree x))) x else gcd y (x mod (y :: _ poly)))"
1977 by (simp add: gcd_poly.simps)
1980 subsection \<open>Additional induction rules on polynomials\<close>
1983 An induction rule for induction over the roots of a polynomial with a certain property.
1984 (e.g. all positive roots)
1986 lemma poly_root_induct [case_names 0 no_roots root]:
1987 fixes p :: "'a :: idom poly"
1989 assumes "\<And>p. (\<And>a. P a \<Longrightarrow> poly p a \<noteq> 0) \<Longrightarrow> Q p"
1990 assumes "\<And>a p. P a \<Longrightarrow> Q p \<Longrightarrow> Q ([:a, -1:] * p)"
1992 proof (induction "degree p" arbitrary: p rule: less_induct)
1995 proof (cases "p = 0")
1996 assume nz: "p \<noteq> 0"
1998 proof (cases "\<exists>a. P a \<and> poly p a = 0")
2000 thus ?thesis by (intro assms(2)) blast
2003 then obtain a where a: "P a" "poly p a = 0"
2005 hence "-[:-a, 1:] dvd p"
2006 by (subst minus_dvd_iff) (simp add: poly_eq_0_iff_dvd)
2007 then obtain q where q: "p = [:a, -1:] * q" by (elim dvdE) simp
2008 with nz have q_nz: "q \<noteq> 0" by auto
2009 have "degree p = Suc (degree q)"
2010 by (subst q, subst degree_mult_eq) (simp_all add: q_nz)
2011 hence "Q q" by (intro less) simp
2012 from a(1) and this have "Q ([:a, -1:] * q)"
2014 with q show ?thesis by simp
2016 qed (simp add: assms(1))
2019 lemma dropWhile_replicate_append:
2020 "dropWhile (op= a) (replicate n a @ ys) = dropWhile (op= a) ys"
2021 by (induction n) simp_all
2023 lemma Poly_append_replicate_0: "Poly (xs @ replicate n 0) = Poly xs"
2024 by (subst coeffs_eq_iff) (simp_all add: strip_while_def dropWhile_replicate_append)
2027 An induction rule for simultaneous induction over two polynomials,
2028 prepending one coefficient in each step.
2030 lemma poly_induct2 [case_names 0 pCons]:
2031 assumes "P 0 0" "\<And>a p b q. P p q \<Longrightarrow> P (pCons a p) (pCons b q)"
2034 def n \<equiv> "max (length (coeffs p)) (length (coeffs q))"
2035 def xs \<equiv> "coeffs p @ (replicate (n - length (coeffs p)) 0)"
2036 def ys \<equiv> "coeffs q @ (replicate (n - length (coeffs q)) 0)"
2037 have "length xs = length ys"
2038 by (simp add: xs_def ys_def n_def)
2039 hence "P (Poly xs) (Poly ys)"
2040 by (induction rule: list_induct2) (simp_all add: assms)
2041 also have "Poly xs = p"
2042 by (simp add: xs_def Poly_append_replicate_0)
2043 also have "Poly ys = q"
2044 by (simp add: ys_def Poly_append_replicate_0)
2045 finally show ?thesis .
2049 subsection \<open>Composition of polynomials\<close>
2051 definition pcompose :: "'a::comm_semiring_0 poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
2053 "pcompose p q = fold_coeffs (\<lambda>a c. [:a:] + q * c) p 0"
2055 lemma pcompose_0 [simp]:
2057 by (simp add: pcompose_def)
2059 lemma pcompose_pCons:
2060 "pcompose (pCons a p) q = [:a:] + q * pcompose p q"
2061 by (cases "p = 0 \<and> a = 0") (auto simp add: pcompose_def)
2063 lemma poly_pcompose:
2064 "poly (pcompose p q) x = poly p (poly q x)"
2065 by (induct p) (simp_all add: pcompose_pCons)
2067 lemma degree_pcompose_le:
2068 "degree (pcompose p q) \<le> degree p * degree q"
2069 apply (induct p, simp)
2070 apply (simp add: pcompose_pCons, clarify)
2071 apply (rule degree_add_le, simp)
2072 apply (rule order_trans [OF degree_mult_le], simp)
2076 fixes p q r :: "'a :: {comm_semiring_0, ab_semigroup_add} poly"
2077 shows "pcompose (p + q) r = pcompose p r + pcompose q r"
2078 proof (induction p q rule: poly_induct2)
2079 case (pCons a p b q)
2080 have "pcompose (pCons a p + pCons b q) r =
2081 [:a + b:] + r * pcompose p r + r * pcompose q r"
2082 by (simp_all add: pcompose_pCons pCons.IH algebra_simps)
2083 also have "[:a + b:] = [:a:] + [:b:]" by simp
2084 also have "\<dots> + r * pcompose p r + r * pcompose q r =
2085 pcompose (pCons a p) r + pcompose (pCons b q) r"
2086 by (simp only: pcompose_pCons add_ac)
2087 finally show ?case .
2090 lemma pcompose_minus:
2091 fixes p r :: "'a :: comm_ring poly"
2092 shows "pcompose (-p) r = -pcompose p r"
2093 by (induction p) (simp_all add: pcompose_pCons)
2095 lemma pcompose_diff:
2096 fixes p q r :: "'a :: comm_ring poly"
2097 shows "pcompose (p - q) r = pcompose p r - pcompose q r"
2098 using pcompose_add[of p "-q"] by (simp add: pcompose_minus)
2100 lemma pcompose_smult:
2101 fixes p r :: "'a :: comm_semiring_0 poly"
2102 shows "pcompose (smult a p) r = smult a (pcompose p r)"
2104 (simp_all add: pcompose_pCons pcompose_add smult_add_right)
2106 lemma pcompose_mult:
2107 fixes p q r :: "'a :: comm_semiring_0 poly"
2108 shows "pcompose (p * q) r = pcompose p r * pcompose q r"
2109 by (induction p arbitrary: q)
2110 (simp_all add: pcompose_add pcompose_smult pcompose_pCons algebra_simps)
2112 lemma pcompose_assoc:
2113 "pcompose p (pcompose q r :: 'a :: comm_semiring_0 poly ) =
2114 pcompose (pcompose p q) r"
2115 by (induction p arbitrary: q)
2116 (simp_all add: pcompose_pCons pcompose_add pcompose_mult)
2119 (* The remainder of this section and the next were contributed by Wenda Li *)
2121 lemma degree_mult_eq_0:
2122 fixes p q:: "'a :: idom poly"
2123 shows "degree (p*q) = 0 \<longleftrightarrow> p=0 \<or> q=0 \<or> (p\<noteq>0 \<and> q\<noteq>0 \<and> degree p =0 \<and> degree q =0)"
2124 by (auto simp add:degree_mult_eq)
2126 lemma pcompose_const[simp]:"pcompose [:a:] q = [:a:]" by (subst pcompose_pCons,simp)
2128 lemma pcompose_0':"pcompose p 0=[:coeff p 0:]"
2130 apply (auto simp add:pcompose_pCons)
2133 lemma degree_pcompose:
2134 fixes p q:: "'a::idom poly"
2135 shows "degree(pcompose p q) = degree p * degree q"
2141 have "degree (q * pcompose p q) = 0 \<Longrightarrow> ?case"
2144 thus ?thesis by auto
2146 case False assume "degree (q * pcompose p q) = 0"
2147 hence "degree q=0 \<or> pcompose p q=0" by (auto simp add:degree_mult_eq_0)
2148 moreover have "\<lbrakk>pcompose p q=0;degree q\<noteq>0\<rbrakk> \<Longrightarrow> False" using pCons.hyps(2) \<open>p\<noteq>0\<close>
2150 assume "pcompose p q=0" "degree q\<noteq>0"
2151 hence "degree p=0" using pCons.hyps(2) by auto
2152 then obtain a1 where "p=[:a1:]"
2153 by (metis degree_pCons_eq_if old.nat.distinct(2) pCons_cases)
2154 thus False using \<open>pcompose p q=0\<close> \<open>p\<noteq>0\<close> by auto
2156 ultimately have "degree (pCons a p) * degree q=0" by auto
2157 moreover have "degree (pcompose (pCons a p) q) = 0"
2159 have" 0 = max (degree [:a:]) (degree (q*pcompose p q))"
2160 using \<open>degree (q * pcompose p q) = 0\<close> by simp
2161 also have "... \<ge> degree ([:a:] + q * pcompose p q)"
2162 by (rule degree_add_le_max)
2163 finally show ?thesis by (auto simp add:pcompose_pCons)
2165 ultimately show ?thesis by simp
2167 moreover have "degree (q * pcompose p q)>0 \<Longrightarrow> ?case"
2169 assume asm:"0 < degree (q * pcompose p q)"
2170 hence "p\<noteq>0" "q\<noteq>0" "pcompose p q\<noteq>0" by auto
2171 have "degree (pcompose (pCons a p) q) = degree ( q * pcompose p q)"
2172 unfolding pcompose_pCons
2173 using degree_add_eq_right[of "[:a:]" ] asm by auto
2175 using pCons.hyps(2) degree_mult_eq[OF \<open>q\<noteq>0\<close> \<open>pcompose p q\<noteq>0\<close>] by auto
2177 ultimately show ?case by blast
2180 lemma pcompose_eq_0:
2181 fixes p q:: "'a::idom poly"
2182 assumes "pcompose p q=0" "degree q>0"
2185 have "degree p=0" using assms degree_pcompose[of p q] by auto
2186 then obtain a where "p=[:a:]"
2187 by (metis degree_pCons_eq_if gr0_conv_Suc neq0_conv pCons_cases)
2188 hence "a=0" using assms(1) by auto
2189 thus ?thesis using \<open>p=[:a:]\<close> by simp
2193 subsection \<open>Leading coefficient\<close>
2195 definition lead_coeff:: "'a::zero poly \<Rightarrow> 'a" where
2196 "lead_coeff p= coeff p (degree p)"
2198 lemma lead_coeff_pCons[simp]:
2199 "p\<noteq>0 \<Longrightarrow>lead_coeff (pCons a p) = lead_coeff p"
2200 "p=0 \<Longrightarrow> lead_coeff (pCons a p) = a"
2201 unfolding lead_coeff_def by auto
2203 lemma lead_coeff_0[simp]:"lead_coeff 0 =0"
2204 unfolding lead_coeff_def by auto
2206 lemma lead_coeff_mult:
2207 fixes p q::"'a ::idom poly"
2208 shows "lead_coeff (p * q) = lead_coeff p * lead_coeff q"
2209 by (unfold lead_coeff_def,cases "p=0 \<or> q=0",auto simp add:coeff_mult_degree_sum degree_mult_eq)
2211 lemma lead_coeff_add_le:
2212 assumes "degree p < degree q"
2213 shows "lead_coeff (p+q) = lead_coeff q"
2214 using assms unfolding lead_coeff_def
2215 by (metis coeff_add coeff_eq_0 monoid_add_class.add.left_neutral degree_add_eq_right)
2217 lemma lead_coeff_minus:
2218 "lead_coeff (-p) = - lead_coeff p"
2219 by (metis coeff_minus degree_minus lead_coeff_def)
2222 lemma lead_coeff_comp:
2223 fixes p q:: "'a::idom poly"
2224 assumes "degree q > 0"
2225 shows "lead_coeff (pcompose p q) = lead_coeff p * lead_coeff q ^ (degree p)"
2228 thus ?case unfolding lead_coeff_def by auto
2231 have "degree ( q * pcompose p q) = 0 \<Longrightarrow> ?case"
2233 assume "degree ( q * pcompose p q) = 0"
2234 hence "pcompose p q = 0" by (metis assms degree_0 degree_mult_eq_0 neq0_conv)
2235 hence "p=0" using pcompose_eq_0[OF _ \<open>degree q > 0\<close>] by simp
2236 thus ?thesis by auto
2238 moreover have "degree ( q * pcompose p q) > 0 \<Longrightarrow> ?case"
2240 assume "degree ( q * pcompose p q) > 0"
2241 hence "lead_coeff (pcompose (pCons a p) q) =lead_coeff ( q * pcompose p q)"
2242 by (auto simp add:pcompose_pCons lead_coeff_add_le)
2243 also have "... = lead_coeff q * (lead_coeff p * lead_coeff q ^ degree p)"
2244 using pCons.hyps(2) lead_coeff_mult[of q "pcompose p q"] by simp
2245 also have "... = lead_coeff p * lead_coeff q ^ (degree p + 1)"
2247 finally show ?thesis by auto
2249 ultimately show ?case by blast
2252 lemma lead_coeff_smult:
2253 "lead_coeff (smult c p :: 'a :: idom poly) = c * lead_coeff p"
2255 have "smult c p = [:c:] * p" by simp
2256 also have "lead_coeff \<dots> = c * lead_coeff p"
2257 by (subst lead_coeff_mult) simp_all
2258 finally show ?thesis .
2261 lemma lead_coeff_1 [simp]: "lead_coeff 1 = 1"
2262 by (simp add: lead_coeff_def)
2264 lemma lead_coeff_of_nat [simp]:
2265 "lead_coeff (of_nat n) = (of_nat n :: 'a :: {comm_semiring_1,semiring_char_0})"
2266 by (induction n) (simp_all add: lead_coeff_def of_nat_poly)
2268 lemma lead_coeff_numeral [simp]:
2269 "lead_coeff (numeral n) = numeral n"
2270 unfolding lead_coeff_def
2271 by (subst of_nat_numeral [symmetric], subst of_nat_poly) simp
2273 lemma lead_coeff_power:
2274 "lead_coeff (p ^ n :: 'a :: idom poly) = lead_coeff p ^ n"
2275 by (induction n) (simp_all add: lead_coeff_mult)
2277 lemma lead_coeff_nonzero: "p \<noteq> 0 \<Longrightarrow> lead_coeff p \<noteq> 0"
2278 by (simp add: lead_coeff_def)
2282 no_notation cCons (infixr "##" 65)