
wenzelm@32960: (*  Title:      HOL/Library/Univ_Poly.thy
wenzelm@32960:     Author:     Amine Chaieb
chaieb@26124: *)
chaieb@26124: 
wenzelm@29292: header {* Univariate Polynomials *}
chaieb@26124: 
chaieb@26124: theory Univ_Poly
haftmann@30738: imports Main
chaieb@26124: begin
chaieb@26124: 
chaieb@26124: text{* Application of polynomial as a function. *}
chaieb@26124: 
haftmann@26194: primrec (in semiring_0) poly :: "'a list => 'a  => 'a" where
chaieb@26124:   poly_Nil:  "poly [] x = 0"
chaieb@26124: | poly_Cons: "poly (h#t) x = h + x * poly t x"
chaieb@26124: 
chaieb@26124: 
chaieb@26124: subsection{*Arithmetic Operations on Polynomials*}
chaieb@26124: 
chaieb@26124: text{*addition*}
chaieb@26124: 
huffman@30488: primrec (in semiring_0) padd :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"  (infixl "+++" 65)
chaieb@26124: where
chaieb@26124:   padd_Nil:  "[] +++ l2 = l2"
chaieb@26124: | padd_Cons: "(h#t) +++ l2 = (if l2 = [] then h#t
chaieb@26124:                             else (h + hd l2)#(t +++ tl l2))"
chaieb@26124: 
chaieb@26124: text{*Multiplication by a constant*}
haftmann@26194: primrec (in semiring_0) cmult :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list"  (infixl "%*" 70) where
chaieb@26124:    cmult_Nil:  "c %* [] = []"
chaieb@26124: |  cmult_Cons: "c %* (h#t) = (c * h)#(c %* t)"
chaieb@26124: 
chaieb@26124: text{*Multiplication by a polynomial*}
haftmann@26194: primrec (in semiring_0) pmult :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"  (infixl "***" 70)
chaieb@26124: where
chaieb@26124:    pmult_Nil:  "[] *** l2 = []"
chaieb@26124: |  pmult_Cons: "(h#t) *** l2 = (if t = [] then h %* l2
chaieb@26124:                               else (h %* l2) +++ ((0) # (t *** l2)))"
chaieb@26124: 
chaieb@26124: text{*Repeated multiplication by a polynomial*}
haftmann@26194: primrec (in semiring_0) mulexp :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a  list \<Rightarrow> 'a list" where
chaieb@26124:    mulexp_zero:  "mulexp 0 p q = q"
chaieb@26124: |  mulexp_Suc:   "mulexp (Suc n) p q = p *** mulexp n p q"
chaieb@26124: 
chaieb@26124: text{*Exponential*}
haftmann@26194: primrec (in semiring_1) pexp :: "'a list \<Rightarrow> nat \<Rightarrow> 'a list"  (infixl "%^" 80) where
chaieb@26124:    pexp_0:   "p %^ 0 = [1]"
chaieb@26124: |  pexp_Suc: "p %^ (Suc n) = p *** (p %^ n)"
chaieb@26124: 
chaieb@26124: text{*Quotient related value of dividing a polynomial by x + a*}
chaieb@26124: (* Useful for divisor properties in inductive proofs *)
haftmann@26194: primrec (in field) "pquot" :: "'a list \<Rightarrow> 'a \<Rightarrow> 'a list" where
chaieb@26124:    pquot_Nil:  "pquot [] a= []"
chaieb@26124: |  pquot_Cons: "pquot (h#t) a = (if t = [] then [h]
chaieb@26124:                    else (inverse(a) * (h - hd( pquot t a)))#(pquot t a))"
chaieb@26124: 
chaieb@26124: text{*normalization of polynomials (remove extra 0 coeff)*}
haftmann@26194: primrec (in semiring_0) pnormalize :: "'a list \<Rightarrow> 'a list" where
chaieb@26124:   pnormalize_Nil:  "pnormalize [] = []"
chaieb@26124: | pnormalize_Cons: "pnormalize (h#p) = (if ( (pnormalize p) = [])
chaieb@26124:                                      then (if (h = 0) then [] else [h])
chaieb@26124:                                      else (h#(pnormalize p)))"
chaieb@26124: 
chaieb@26124: definition (in semiring_0) "pnormal p = ((pnormalize p = p) \<and> p \<noteq> [])"
chaieb@26124: definition (in semiring_0) "nonconstant p = (pnormal p \<and> (\<forall>x. p \<noteq> [x]))"
chaieb@26124: text{*Other definitions*}
chaieb@26124: 
chaieb@26124: definition (in ring_1)
chaieb@26124:   poly_minus :: "'a list => 'a list" ("-- _" [80] 80) where
chaieb@26124:   "-- p = (- 1) %* p"
chaieb@26124: 
chaieb@26124: definition (in semiring_0)
chaieb@26124:   divides :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"  (infixl "divides" 70) where
haftmann@37765:   "p1 divides p2 = (\<exists>q. poly p2 = poly(p1 *** q))"
chaieb@26124: 
chaieb@26124:     --{*order of a polynomial*}
chaieb@26124: definition (in ring_1) order :: "'a => 'a list => nat" where
chaieb@26124:   "order a p = (SOME n. ([-a, 1] %^ n) divides p &
chaieb@26124:                       ~ (([-a, 1] %^ (Suc n)) divides p))"
chaieb@26124: 
chaieb@26124:      --{*degree of a polynomial*}
huffman@30488: definition (in semiring_0) degree :: "'a list => nat" where
chaieb@26124:   "degree p = length (pnormalize p) - 1"
chaieb@26124: 
chaieb@26124:      --{*squarefree polynomials --- NB with respect to real roots only.*}
chaieb@26124: definition (in ring_1)
chaieb@26124:   rsquarefree :: "'a list => bool" where
chaieb@26124:   "rsquarefree p = (poly p \<noteq> poly [] &
chaieb@26124:                      (\<forall>a. (order a p = 0) | (order a p = 1)))"
chaieb@26124: 
chaieb@26124: context semiring_0
chaieb@26124: begin
chaieb@26124: 
chaieb@26124: lemma padd_Nil2[simp]: "p +++ [] = p"
chaieb@26124: by (induct p) auto
chaieb@26124: 
chaieb@26124: lemma padd_Cons_Cons: "(h1 # p1) +++ (h2 # p2) = (h1 + h2) # (p1 +++ p2)"
chaieb@26124: by auto
chaieb@26124: 
chaieb@26124: lemma pminus_Nil[simp]: "-- [] = []"
chaieb@26124: by (simp add: poly_minus_def)
chaieb@26124: 
chaieb@26124: lemma pmult_singleton: "[h1] *** p1 = h1 %* p1" by simp
chaieb@26124: end
chaieb@26124: 
wenzelm@46730: lemma (in semiring_1) poly_ident_mult[simp]: "1 %* t = t" by (induct t) auto
chaieb@26124: 
chaieb@26124: lemma (in semiring_0) poly_simple_add_Cons[simp]: "[a] +++ ((0)#t) = (a#t)"
chaieb@26124: by simp
chaieb@26124: 
chaieb@26124: text{*Handy general properties*}
chaieb@26124: 
chaieb@26124: lemma (in comm_semiring_0) padd_commut: "b +++ a = a +++ b"
chaieb@26124: proof(induct b arbitrary: a)
chaieb@26124:   case Nil thus ?case by auto
chaieb@26124: next
chaieb@26124:   case (Cons b bs a) thus ?case by (cases a, simp_all add: add_commute)
chaieb@26124: qed
chaieb@26124: 
chaieb@26124: lemma (in comm_semiring_0) padd_assoc: "\<forall>b c. (a +++ b) +++ c = a +++ (b +++ c)"
wenzelm@45129: apply (induct a)
chaieb@26124: apply (simp, clarify)
chaieb@26124: apply (case_tac b, simp_all add: add_ac)
chaieb@26124: done
chaieb@26124: 
chaieb@26124: lemma (in semiring_0) poly_cmult_distr: "a %* ( p +++ q) = (a %* p +++ a %* q)"
wenzelm@45129: apply (induct p arbitrary: q, simp)
webertj@49962: apply (case_tac q, simp_all add: distrib_left)
chaieb@26124: done
chaieb@26124: 
chaieb@26124: lemma (in ring_1) pmult_by_x[simp]: "[0, 1] *** t = ((0)#t)"
chaieb@26124: apply (induct "t", simp)
chaieb@26124: apply (auto simp add: mult_zero_left poly_ident_mult padd_commut)
chaieb@26124: apply (case_tac t, auto)
chaieb@26124: done
chaieb@26124: 
chaieb@26124: text{*properties of evaluation of polynomials.*}
chaieb@26124: 
chaieb@26124: lemma (in semiring_0) poly_add: "poly (p1 +++ p2) x = poly p1 x + poly p2 x"
chaieb@26124: proof(induct p1 arbitrary: p2)
chaieb@26124:   case Nil thus ?case by simp
chaieb@26124: next
huffman@30488:   case (Cons a as p2) thus ?case
webertj@49962:     by (cases p2, simp_all  add: add_ac distrib_left)
chaieb@26124: qed
chaieb@26124: 
chaieb@26124: lemma (in comm_semiring_0) poly_cmult: "poly (c %* p) x = c * poly p x"
huffman@30488: apply (induct "p")
chaieb@26124: apply (case_tac [2] "x=zero")
webertj@49962: apply (auto simp add: distrib_left mult_ac)
chaieb@26124: done
chaieb@26124: 
chaieb@26124: lemma (in comm_semiring_0) poly_cmult_map: "poly (map (op * c) p) x = c*poly p x"
webertj@49962:   by (induct p, auto simp add: distrib_left mult_ac)
chaieb@26124: 
chaieb@26124: lemma (in comm_ring_1) poly_minus: "poly (-- p) x = - (poly p x)"
chaieb@26124: apply (simp add: poly_minus_def)
chaieb@26124: apply (auto simp add: poly_cmult minus_mult_left[symmetric])
chaieb@26124: done
chaieb@26124: 
chaieb@26124: lemma (in comm_semiring_0) poly_mult: "poly (p1 *** p2) x = poly p1 x * poly p2 x"
chaieb@26124: proof(induct p1 arbitrary: p2)
chaieb@26124:   case Nil thus ?case by simp
chaieb@26124: next
chaieb@26124:   case (Cons a as p2)
huffman@30488:   thus ?case by (cases as,
webertj@49962:     simp_all add: poly_cmult poly_add distrib_right distrib_left mult_ac)
chaieb@26124: qed
chaieb@26124: 
chaieb@26124: class idom_char_0 = idom + ring_char_0
chaieb@26124: 
haftmann@31021: lemma (in comm_ring_1) poly_exp: "poly (p %^ n) x = (poly p x) ^ n"
chaieb@26124: apply (induct "n")
chaieb@26124: apply (auto simp add: poly_cmult poly_mult power_Suc)
chaieb@26124: done
chaieb@26124: 
chaieb@26124: text{*More Polynomial Evaluation Lemmas*}
chaieb@26124: 
chaieb@26124: lemma  (in semiring_0) poly_add_rzero[simp]: "poly (a +++ []) x = poly a x"
chaieb@26124: by simp
chaieb@26124: 
chaieb@26124: lemma (in comm_semiring_0) poly_mult_assoc: "poly ((a *** b) *** c) x = poly (a *** (b *** c)) x"
chaieb@26124:   by (simp add: poly_mult mult_assoc)
chaieb@26124: 
chaieb@26124: lemma (in semiring_0) poly_mult_Nil2[simp]: "poly (p *** []) x = 0"
chaieb@26124: by (induct "p", auto)
chaieb@26124: 
chaieb@26124: lemma (in comm_semiring_1) poly_exp_add: "poly (p %^ (n + d)) x = poly( p %^ n *** p %^ d) x"
chaieb@26124: apply (induct "n")
chaieb@26124: apply (auto simp add: poly_mult mult_assoc)
chaieb@26124: done
chaieb@26124: 
chaieb@26124: subsection{*Key Property: if @{term "f(a) = 0"} then @{term "(x - a)"} divides
chaieb@26124:  @{term "p(x)"} *}
chaieb@26124: 
chaieb@26124: lemma (in comm_ring_1) lemma_poly_linear_rem: "\<forall>h. \<exists>q r. h#t = [r] +++ [-a, 1] *** q"
chaieb@26124: proof(induct t)
chaieb@26124:   case Nil
chaieb@26124:   {fix h have "[h] = [h] +++ [- a, 1] *** []" by simp}
chaieb@26124:   thus ?case by blast
chaieb@26124: next
chaieb@26124:   case (Cons  x xs)
huffman@30488:   {fix h
huffman@30488:     from Cons.hyps[rule_format, of x]
chaieb@26124:     obtain q r where qr: "x#xs = [r] +++ [- a, 1] *** q" by blast
huffman@30488:     have "h#x#xs = [a*r + h] +++ [-a, 1] *** (r#q)"
haftmann@37887:       using qr by(cases q, simp_all add: algebra_simps diff_minus[symmetric]
wenzelm@32960:         minus_mult_left[symmetric] right_minus)
chaieb@26124:     hence "\<exists>q r. h#x#xs = [r] +++ [-a, 1] *** q" by blast}
chaieb@26124:   thus ?case by blast
chaieb@26124: qed
chaieb@26124: 
chaieb@26124: lemma (in comm_ring_1) poly_linear_rem: "\<exists>q r. h#t = [r] +++ [-a, 1] *** q"
chaieb@26124: by (cut_tac t = t and a = a in lemma_poly_linear_rem, auto)
chaieb@26124: 
chaieb@26124: 
chaieb@26124: lemma (in comm_ring_1) poly_linear_divides: "(poly p a = 0) = ((p = []) | (\<exists>q. p = [-a, 1] *** q))"
chaieb@26124: proof-
chaieb@26124:   {assume p: "p = []" hence ?thesis by simp}
chaieb@26124:   moreover
chaieb@26124:   {fix x xs assume p: "p = x#xs"
huffman@30488:     {fix q assume "p = [-a, 1] *** q" hence "poly p a = 0"
wenzelm@32960:         by (simp add: poly_add poly_cmult minus_mult_left[symmetric])}
chaieb@26124:     moreover
chaieb@26124:     {assume p0: "poly p a = 0"
huffman@30488:       from poly_linear_rem[of x xs a] obtain q r
chaieb@26124:       where qr: "x#xs = [r] +++ [- a, 1] *** q" by blast
chaieb@26124:       have "r = 0" using p0 by (simp only: p qr poly_mult poly_add) simp
chaieb@26124:       hence "\<exists>q. p = [- a, 1] *** q" using p qr  apply - apply (rule exI[where x=q])apply auto apply (cases q) apply auto done}
chaieb@26124:     ultimately have ?thesis using p by blast}
chaieb@26124:   ultimately show ?thesis by (cases p, auto)
chaieb@26124: qed
chaieb@26124: 
chaieb@26124: lemma (in semiring_0) lemma_poly_length_mult[simp]: "\<forall>h k a. length (k %* p +++  (h # (a %* p))) = Suc (length p)"
chaieb@26124: by (induct "p", auto)
chaieb@26124: 
chaieb@26124: lemma (in semiring_0) lemma_poly_length_mult2[simp]: "\<forall>h k. length (k %* p +++  (h # p)) = Suc (length p)"
chaieb@26124: by (induct "p", auto)
chaieb@26124: 
chaieb@26124: lemma (in ring_1) poly_length_mult[simp]: "length([-a,1] *** q) = Suc (length q)"
chaieb@26124: by auto
chaieb@26124: 
chaieb@26124: subsection{*Polynomial length*}
chaieb@26124: 
chaieb@26124: lemma (in semiring_0) poly_cmult_length[simp]: "length (a %* p) = length p"
chaieb@26124: by (induct "p", auto)
chaieb@26124: 
chaieb@26124: lemma (in semiring_0) poly_add_length: "length (p1 +++ p2) = max (length p1) (length p2)"
chaieb@26124: apply (induct p1 arbitrary: p2, simp_all)
chaieb@26124: apply arith
chaieb@26124: done
chaieb@26124: 
chaieb@26124: lemma (in semiring_0) poly_root_mult_length[simp]: "length([a,b] *** p) = Suc (length p)"
chaieb@26124: by (simp add: poly_add_length)
chaieb@26124: 
huffman@30488: lemma (in idom) poly_mult_not_eq_poly_Nil[simp]:
chaieb@26124:  "poly (p *** q) x \<noteq> poly [] x \<longleftrightarrow> poly p x \<noteq> poly [] x \<and> poly q x \<noteq> poly [] x"
chaieb@26124: by (auto simp add: poly_mult)
chaieb@26124: 
chaieb@26124: lemma (in idom) poly_mult_eq_zero_disj: "poly (p *** q) x = 0 \<longleftrightarrow> poly p x = 0 \<or> poly q x = 0"
chaieb@26124: by (auto simp add: poly_mult)
chaieb@26124: 
chaieb@26124: text{*Normalisation Properties*}
chaieb@26124: 
chaieb@26124: lemma (in semiring_0) poly_normalized_nil: "(pnormalize p = []) --> (poly p x = 0)"
chaieb@26124: by (induct "p", auto)
chaieb@26124: 
chaieb@26124: text{*A nontrivial polynomial of degree n has no more than n roots*}
chaieb@26124: lemma (in idom) poly_roots_index_lemma:
huffman@30488:    assumes p: "poly p x \<noteq> poly [] x" and n: "length p = n"
chaieb@26124:   shows "\<exists>i. \<forall>x. poly p x = 0 \<longrightarrow> (\<exists>m\<le>n. x = i m)"
chaieb@26124:   using p n
chaieb@26124: proof(induct n arbitrary: p x)
huffman@30488:   case 0 thus ?case by simp
chaieb@26124: next
chaieb@26124:   case (Suc n p x)
chaieb@26124:   {assume C: "\<And>i. \<exists>x. poly p x = 0 \<and> (\<forall>m\<le>Suc n. x \<noteq> i m)"
chaieb@26124:     from Suc.prems have p0: "poly p x \<noteq> 0" "p\<noteq> []" by auto
huffman@30488:     from p0(1)[unfolded poly_linear_divides[of p x]]
chaieb@26124:     have "\<forall>q. p \<noteq> [- x, 1] *** q" by blast
chaieb@26124:     from C obtain a where a: "poly p a = 0" by blast
huffman@30488:     from a[unfolded poly_linear_divides[of p a]] p0(2)
chaieb@26124:     obtain q where q: "p = [-a, 1] *** q" by blast
chaieb@26124:     have lg: "length q = n" using q Suc.prems(2) by simp
huffman@30488:     from q p0 have qx: "poly q x \<noteq> poly [] x"
chaieb@26124:       by (auto simp add: poly_mult poly_add poly_cmult)
huffman@30488:     from Suc.hyps[OF qx lg] obtain i where
chaieb@26124:       i: "\<forall>x. poly q x = 0 \<longrightarrow> (\<exists>m\<le>n. x = i m)" by blast
chaieb@26124:     let ?i = "\<lambda>m. if m = Suc n then a else i m"
huffman@30488:     from C[of ?i] obtain y where y: "poly p y = 0" "\<forall>m\<le> Suc n. y \<noteq> ?i m"
chaieb@26124:       by blast
huffman@30488:     from y have "y = a \<or> poly q y = 0"
nipkow@29667:       by (simp only: q poly_mult_eq_zero_disj poly_add) (simp add: algebra_simps)
huffman@30488:     with i[rule_format, of y] y(1) y(2) have False apply auto
chaieb@26124:       apply (erule_tac x="m" in allE)
chaieb@26124:       apply auto
chaieb@26124:       done}
chaieb@26124:   thus ?case by blast
chaieb@26124: qed
chaieb@26124: 
chaieb@26124: 
chaieb@26124: lemma (in idom) poly_roots_index_length: "poly p x \<noteq> poly [] x ==>
chaieb@26124:       \<exists>i. \<forall>x. (poly p x = 0) --> (\<exists>n. n \<le> length p & x = i n)"
chaieb@26124: by (blast intro: poly_roots_index_lemma)
chaieb@26124: 
wenzelm@26313: lemma (in idom) poly_roots_finite_lemma1: "poly p x \<noteq> poly [] x ==>
chaieb@26124:       \<exists>N i. \<forall>x. (poly p x = 0) --> (\<exists>n. (n::nat) < N & x = i n)"
chaieb@26124: apply (drule poly_roots_index_length, safe)
chaieb@26124: apply (rule_tac x = "Suc (length p)" in exI)
huffman@30488: apply (rule_tac x = i in exI)
chaieb@26124: apply (simp add: less_Suc_eq_le)
chaieb@26124: done
chaieb@26124: 
chaieb@26124: 
chaieb@26124: lemma (in idom) idom_finite_lemma:
chaieb@26124:   assumes P: "\<forall>x. P x --> (\<exists>n. n < length j & x = j!n)"
chaieb@26124:   shows "finite {x. P x}"
chaieb@26124: proof-
chaieb@26124:   let ?M = "{x. P x}"
chaieb@26124:   let ?N = "set j"
chaieb@26124:   have "?M \<subseteq> ?N" using P by auto
chaieb@26124:   thus ?thesis using finite_subset by auto
chaieb@26124: qed
chaieb@26124: 
chaieb@26124: 
wenzelm@26313: lemma (in idom) poly_roots_finite_lemma2: "poly p x \<noteq> poly [] x ==>
chaieb@26124:       \<exists>i. \<forall>x. (poly p x = 0) --> x \<in> set i"
chaieb@26124: apply (drule poly_roots_index_length, safe)
chaieb@26124: apply (rule_tac x="map (\<lambda>n. i n) [0 ..< Suc (length p)]" in exI)
chaieb@26124: apply (auto simp add: image_iff)
chaieb@26124: apply (erule_tac x="x" in allE, clarsimp)
chaieb@26124: by (case_tac "n=length p", auto simp add: order_le_less)
chaieb@26124: 
huffman@30488: lemma (in ring_char_0) UNIV_ring_char_0_infinte:
huffman@30488:   "\<not> (finite (UNIV:: 'a set))"
chaieb@26124: proof
chaieb@26124:   assume F: "finite (UNIV :: 'a set)"
wenzelm@29292:   have "finite (UNIV :: nat set)"
wenzelm@29292:   proof (rule finite_imageD)
wenzelm@29292:     have "of_nat ` UNIV \<subseteq> UNIV" by simp
wenzelm@29292:     then show "finite (of_nat ` UNIV :: 'a set)" using F by (rule finite_subset)
wenzelm@29292:     show "inj (of_nat :: nat \<Rightarrow> 'a)" by (simp add: inj_on_def)
wenzelm@29292:   qed
nipkow@29879:   with infinite_UNIV_nat show False ..
chaieb@26124: qed
chaieb@26124: 
huffman@30488: lemma (in idom_char_0) poly_roots_finite: "(poly p \<noteq> poly []) =
chaieb@26124:   finite {x. poly p x = 0}"
chaieb@26124: proof
chaieb@26124:   assume H: "poly p \<noteq> poly []"
chaieb@26124:   show "finite {x. poly p x = (0::'a)}"
chaieb@26124:     using H
chaieb@26124:     apply -
chaieb@26124:     apply (erule contrapos_np, rule ext)
chaieb@26124:     apply (rule ccontr)
wenzelm@26313:     apply (clarify dest!: poly_roots_finite_lemma2)
chaieb@26124:     using finite_subset
chaieb@26124:   proof-
chaieb@26124:     fix x i
huffman@30488:     assume F: "\<not> finite {x. poly p x = (0\<Colon>'a)}"
chaieb@26124:       and P: "\<forall>x. poly p x = (0\<Colon>'a) \<longrightarrow> x \<in> set i"
chaieb@26124:     let ?M= "{x. poly p x = (0\<Colon>'a)}"
chaieb@26124:     from P have "?M \<subseteq> set i" by auto
chaieb@26124:     with finite_subset F show False by auto
chaieb@26124:   qed
chaieb@26124: next
chaieb@26124:   assume F: "finite {x. poly p x = (0\<Colon>'a)}"
huffman@30488:   show "poly p \<noteq> poly []" using F UNIV_ring_char_0_infinte by auto
chaieb@26124: qed
chaieb@26124: 
chaieb@26124: text{*Entirety and Cancellation for polynomials*}
chaieb@26124: 
huffman@30488: lemma (in idom_char_0) poly_entire_lemma2:
wenzelm@26313:   assumes p0: "poly p \<noteq> poly []" and q0: "poly q \<noteq> poly []"
wenzelm@26313:   shows "poly (p***q) \<noteq> poly []"
wenzelm@26313: proof-
wenzelm@26313:   let ?S = "\<lambda>p. {x. poly p x = 0}"
wenzelm@26313:   have "?S (p *** q) = ?S p \<union> ?S q" by (auto simp add: poly_mult)
wenzelm@26313:   with p0 q0 show ?thesis  unfolding poly_roots_finite by auto
wenzelm@26313: qed
chaieb@26124: 
huffman@30488: lemma (in idom_char_0) poly_entire:
wenzelm@26313:   "poly (p *** q) = poly [] \<longleftrightarrow> poly p = poly [] \<or> poly q = poly []"
nipkow@29667: using poly_entire_lemma2[of p q]
nipkow@39302: by (auto simp add: fun_eq_iff poly_mult)
chaieb@26124: 
chaieb@26124: lemma (in idom_char_0) poly_entire_neg: "(poly (p *** q) \<noteq> poly []) = ((poly p \<noteq> poly []) & (poly q \<noteq> poly []))"
chaieb@26124: by (simp add: poly_entire)
chaieb@26124: 
chaieb@26124: lemma fun_eq: " (f = g) = (\<forall>x. f x = g x)"
chaieb@26124: by (auto intro!: ext)
chaieb@26124: 
chaieb@26124: lemma (in comm_ring_1) poly_add_minus_zero_iff: "(poly (p +++ -- q) = poly []) = (poly p = poly q)"
nipkow@29667: by (auto simp add: algebra_simps poly_add poly_minus_def fun_eq poly_cmult minus_mult_left[symmetric])
chaieb@26124: 
chaieb@26124: lemma (in comm_ring_1) poly_add_minus_mult_eq: "poly (p *** q +++ --(p *** r)) = poly (p *** (q +++ -- r))"
webertj@49962: by (auto simp add: poly_add poly_minus_def fun_eq poly_mult poly_cmult distrib_left minus_mult_left[symmetric] minus_mult_right[symmetric])
chaieb@26124: 
haftmann@28823: subclass (in idom_char_0) comm_ring_1 ..
chaieb@26124: lemma (in idom_char_0) poly_mult_left_cancel: "(poly (p *** q) = poly (p *** r)) = (poly p = poly [] | poly q = poly r)"
chaieb@26124: proof-
chaieb@26124:   have "poly (p *** q) = poly (p *** r) \<longleftrightarrow> poly (p *** q +++ -- (p *** r)) = poly []" by (simp only: poly_add_minus_zero_iff)
chaieb@26124:   also have "\<dots> \<longleftrightarrow> poly p = poly [] | poly q = poly r"
chaieb@26124:     by (auto intro: ext simp add: poly_add_minus_mult_eq poly_entire poly_add_minus_zero_iff)
chaieb@26124:   finally show ?thesis .
chaieb@26124: qed
chaieb@26124: 
haftmann@31021: lemma (in idom) poly_exp_eq_zero[simp]:
chaieb@26124:      "(poly (p %^ n) = poly []) = (poly p = poly [] & n \<noteq> 0)"
haftmann@37598: apply (simp only: fun_eq add: HOL.all_simps [symmetric])
huffman@30488: apply (rule arg_cong [where f = All])
chaieb@26124: apply (rule ext)
haftmann@26194: apply (induct n)
chaieb@26124: apply (auto simp add: poly_exp poly_mult)
chaieb@26124: done
chaieb@26124: 
chaieb@26124: lemma (in comm_ring_1) poly_prime_eq_zero[simp]: "poly [a,1] \<noteq> poly []"
chaieb@26124: apply (simp add: fun_eq)
chaieb@26124: apply (rule_tac x = "minus one a" in exI)
chaieb@26124: apply (unfold diff_minus)
chaieb@26124: apply (subst add_commute)
chaieb@26124: apply (subst add_assoc)
chaieb@26124: apply simp
huffman@30488: done
chaieb@26124: 
haftmann@31021: lemma (in idom) poly_exp_prime_eq_zero: "(poly ([a, 1] %^ n) \<noteq> poly [])"
chaieb@26124: by auto
chaieb@26124: 
chaieb@26124: text{*A more constructive notion of polynomials being trivial*}
chaieb@26124: 
chaieb@26124: lemma (in idom_char_0) poly_zero_lemma': "poly (h # t) = poly [] ==> h = 0 & poly t = poly []"
chaieb@26124: apply(simp add: fun_eq)
chaieb@26124: apply (case_tac "h = zero")
huffman@30488: apply (drule_tac [2] x = zero in spec, auto)
huffman@30488: apply (cases "poly t = poly []", simp)
chaieb@26124: proof-
chaieb@26124:   fix x
chaieb@26124:   assume H: "\<forall>x. x = (0\<Colon>'a) \<or> poly t x = (0\<Colon>'a)"  and pnz: "poly t \<noteq> poly []"
chaieb@26124:   let ?S = "{x. poly t x = 0}"
chaieb@26124:   from H have "\<forall>x. x \<noteq>0 \<longrightarrow> poly t x = 0" by blast
chaieb@26124:   hence th: "?S \<supseteq> UNIV - {0}" by auto
chaieb@26124:   from poly_roots_finite pnz have th': "finite ?S" by blast
chaieb@26124:   from finite_subset[OF th th'] UNIV_ring_char_0_infinte
chaieb@26124:   show "poly t x = (0\<Colon>'a)" by simp
chaieb@26124:   qed
chaieb@26124: 
chaieb@26124: lemma (in idom_char_0) poly_zero: "(poly p = poly []) = list_all (%c. c = 0) p"
chaieb@26124: apply (induct "p", simp)
chaieb@26124: apply (rule iffI)
chaieb@26124: apply (drule poly_zero_lemma', auto)
chaieb@26124: done
chaieb@26124: 
chaieb@26124: lemma (in idom_char_0) poly_0: "list_all (\<lambda>c. c = 0) p \<Longrightarrow> poly p x = 0"
chaieb@26124:   unfolding poly_zero[symmetric] by simp
chaieb@26124: 
chaieb@26124: 
chaieb@26124: 
chaieb@26124: text{*Basics of divisibility.*}
chaieb@26124: 
chaieb@26124: lemma (in idom) poly_primes: "([a, 1] divides (p *** q)) = ([a, 1] divides p | [a, 1] divides q)"
webertj@49962: apply (auto simp add: divides_def fun_eq poly_mult poly_add poly_cmult distrib_right [symmetric])
chaieb@26124: apply (drule_tac x = "uminus a" in spec)
webertj@49962: apply (simp add: poly_linear_divides poly_add poly_cmult distrib_right [symmetric])
chaieb@26124: apply (cases "p = []")
chaieb@26124: apply (rule exI[where x="[]"])
chaieb@26124: apply simp
chaieb@26124: apply (cases "q = []")
chaieb@26124: apply (erule allE[where x="[]"], simp)
chaieb@26124: 
chaieb@26124: apply clarsimp
chaieb@26124: apply (cases "\<exists>q\<Colon>'a list. p = a %* q +++ ((0\<Colon>'a) # q)")
chaieb@26124: apply (clarsimp simp add: poly_add poly_cmult)
chaieb@26124: apply (rule_tac x="qa" in exI)
webertj@49962: apply (simp add: distrib_right [symmetric])
chaieb@26124: apply clarsimp
chaieb@26124: 
webertj@49962: apply (auto simp add: right_minus poly_linear_divides poly_add poly_cmult distrib_right [symmetric])
chaieb@26124: apply (rule_tac x = "pmult qa q" in exI)
chaieb@26124: apply (rule_tac [2] x = "pmult p qa" in exI)
chaieb@26124: apply (auto simp add: poly_add poly_mult poly_cmult mult_ac)
chaieb@26124: done
chaieb@26124: 
chaieb@26124: lemma (in comm_semiring_1) poly_divides_refl[simp]: "p divides p"
chaieb@26124: apply (simp add: divides_def)
chaieb@26124: apply (rule_tac x = "[one]" in exI)
chaieb@26124: apply (auto simp add: poly_mult fun_eq)
chaieb@26124: done
chaieb@26124: 
chaieb@26124: lemma (in comm_semiring_1) poly_divides_trans: "[| p divides q; q divides r |] ==> p divides r"
chaieb@26124: apply (simp add: divides_def, safe)
chaieb@26124: apply (rule_tac x = "pmult qa qaa" in exI)
chaieb@26124: apply (auto simp add: poly_mult fun_eq mult_assoc)
chaieb@26124: done
chaieb@26124: 
chaieb@26124: 
haftmann@31021: lemma (in comm_semiring_1) poly_divides_exp: "m \<le> n ==> (p %^ m) divides (p %^ n)"
chaieb@26124: apply (auto simp add: le_iff_add)
chaieb@26124: apply (induct_tac k)
chaieb@26124: apply (rule_tac [2] poly_divides_trans)
chaieb@26124: apply (auto simp add: divides_def)
chaieb@26124: apply (rule_tac x = p in exI)
chaieb@26124: apply (auto simp add: poly_mult fun_eq mult_ac)
chaieb@26124: done
chaieb@26124: 
haftmann@31021: lemma (in comm_semiring_1) poly_exp_divides: "[| (p %^ n) divides q;  m\<le>n |] ==> (p %^ m) divides q"
chaieb@26124: by (blast intro: poly_divides_exp poly_divides_trans)
chaieb@26124: 
chaieb@26124: lemma (in comm_semiring_0) poly_divides_add:
chaieb@26124:    "[| p divides q; p divides r |] ==> p divides (q +++ r)"
chaieb@26124: apply (simp add: divides_def, auto)
chaieb@26124: apply (rule_tac x = "padd qa qaa" in exI)
webertj@49962: apply (auto simp add: poly_add fun_eq poly_mult distrib_left)
chaieb@26124: done
chaieb@26124: 
chaieb@26124: lemma (in comm_ring_1) poly_divides_diff:
chaieb@26124:    "[| p divides q; p divides (q +++ r) |] ==> p divides r"
chaieb@26124: apply (simp add: divides_def, auto)
chaieb@26124: apply (rule_tac x = "padd qaa (poly_minus qa)" in exI)
nipkow@29667: apply (auto simp add: poly_add fun_eq poly_mult poly_minus algebra_simps)
chaieb@26124: done
chaieb@26124: 
chaieb@26124: lemma (in comm_ring_1) poly_divides_diff2: "[| p divides r; p divides (q +++ r) |] ==> p divides q"
chaieb@26124: apply (erule poly_divides_diff)
chaieb@26124: apply (auto simp add: poly_add fun_eq poly_mult divides_def add_ac)
chaieb@26124: done
chaieb@26124: 
chaieb@26124: lemma (in semiring_0) poly_divides_zero: "poly p = poly [] ==> q divides p"
chaieb@26124: apply (simp add: divides_def)
chaieb@26124: apply (rule exI[where x="[]"])
chaieb@26124: apply (auto simp add: fun_eq poly_mult)
chaieb@26124: done
chaieb@26124: 
chaieb@26124: lemma (in semiring_0) poly_divides_zero2[simp]: "q divides []"
chaieb@26124: apply (simp add: divides_def)
chaieb@26124: apply (rule_tac x = "[]" in exI)
chaieb@26124: apply (auto simp add: fun_eq)
chaieb@26124: done
chaieb@26124: 
chaieb@26124: text{*At last, we can consider the order of a root.*}
chaieb@26124: 
chaieb@26124: lemma (in idom_char_0)  poly_order_exists_lemma:
chaieb@26124:   assumes lp: "length p = d" and p: "poly p \<noteq> poly []"
chaieb@26124:   shows "\<exists>n q. p = mulexp n [-a, 1] q \<and> poly q a \<noteq> 0"
chaieb@26124: using lp p
chaieb@26124: proof(induct d arbitrary: p)
chaieb@26124:   case 0 thus ?case by simp
chaieb@26124: next
chaieb@26124:   case (Suc n p)
chaieb@26124:   {assume p0: "poly p a = 0"
wenzelm@29292:     from Suc.prems have h: "length p = Suc n" "poly p \<noteq> poly []" by auto
wenzelm@29292:     hence pN: "p \<noteq> []" by auto
huffman@30488:     from p0[unfolded poly_linear_divides] pN  obtain q where
chaieb@26124:       q: "p = [-a, 1] *** q" by blast
huffman@30488:     from q h p0 have qh: "length q = n" "poly q \<noteq> poly []"
chaieb@26124:       apply -
chaieb@26124:       apply simp
chaieb@26124:       apply (simp only: fun_eq)
chaieb@26124:       apply (rule ccontr)
chaieb@26124:       apply (simp add: fun_eq poly_add poly_cmult minus_mult_left[symmetric])
chaieb@26124:       done
huffman@30488:     from Suc.hyps[OF qh] obtain m r where
huffman@30488:       mr: "q = mulexp m [-a,1] r" "poly r a \<noteq> 0" by blast
chaieb@26124:     from mr q have "p = mulexp (Suc m) [-a,1] r \<and> poly r a \<noteq> 0" by simp
chaieb@26124:     hence ?case by blast}
chaieb@26124:   moreover
chaieb@26124:   {assume p0: "poly p a \<noteq> 0"
chaieb@26124:     hence ?case using Suc.prems apply simp by (rule exI[where x="0::nat"], simp)}
chaieb@26124:   ultimately show ?case by blast
chaieb@26124: qed
chaieb@26124: 
chaieb@26124: 
haftmann@31021: lemma (in comm_semiring_1) poly_mulexp: "poly (mulexp n p q) x = (poly p x) ^ n * poly q x"
chaieb@26124: by(induct n, auto simp add: poly_mult power_Suc mult_ac)
chaieb@26124: 
chaieb@26124: lemma (in comm_semiring_1) divides_left_mult:
chaieb@26124:   assumes d:"(p***q) divides r" shows "p divides r \<and> q divides r"
chaieb@26124: proof-
chaieb@26124:   from d obtain t where r:"poly r = poly (p***q *** t)"
chaieb@26124:     unfolding divides_def by blast
chaieb@26124:   hence "poly r = poly (p *** (q *** t))"
chaieb@26124:     "poly r = poly (q *** (p***t))" by(auto simp add: fun_eq poly_mult mult_ac)
chaieb@26124:   thus ?thesis unfolding divides_def by blast
chaieb@26124: qed
chaieb@26124: 
chaieb@26124: 
chaieb@26124: 
chaieb@26124: (* FIXME: Tidy up *)
chaieb@26124: 
haftmann@31021: lemma (in semiring_1)
chaieb@26124:   zero_power_iff: "0 ^ n = (if n = 0 then 1 else 0)"
chaieb@26124:   by (induct n, simp_all add: power_Suc)
chaieb@26124: 
haftmann@31021: lemma (in idom_char_0) poly_order_exists:
chaieb@26124:   assumes lp: "length p = d" and p0: "poly p \<noteq> poly []"
chaieb@26124:   shows "\<exists>n. ([-a, 1] %^ n) divides p & ~(([-a, 1] %^ (Suc n)) divides p)"
chaieb@26124: proof-
chaieb@26124: let ?poly = poly
chaieb@26124: let ?mulexp = mulexp
chaieb@26124: let ?pexp = pexp
chaieb@26124: from lp p0
chaieb@26124: show ?thesis
chaieb@26124: apply -
huffman@30488: apply (drule poly_order_exists_lemma [where a=a], assumption, clarify)
chaieb@26124: apply (rule_tac x = n in exI, safe)
chaieb@26124: apply (unfold divides_def)
chaieb@26124: apply (rule_tac x = q in exI)
chaieb@26124: apply (induct_tac "n", simp)
webertj@49962: apply (simp (no_asm_simp) add: poly_add poly_cmult poly_mult distrib_left mult_ac)
chaieb@26124: apply safe
huffman@30488: apply (subgoal_tac "?poly (?mulexp n [uminus a, one] q) \<noteq> ?poly (pmult (?pexp [uminus a, one] (Suc n)) qa)")
huffman@30488: apply simp
chaieb@26124: apply (induct_tac "n")
chaieb@26124: apply (simp del: pmult_Cons pexp_Suc)
chaieb@26124: apply (erule_tac Q = "?poly q a = zero" in contrapos_np)
chaieb@26124: apply (simp add: poly_add poly_cmult minus_mult_left[symmetric])
chaieb@26124: apply (rule pexp_Suc [THEN ssubst])
chaieb@26124: apply (rule ccontr)
chaieb@26124: apply (simp add: poly_mult_left_cancel poly_mult_assoc del: pmult_Cons pexp_Suc)
chaieb@26124: done
chaieb@26124: qed
chaieb@26124: 
chaieb@26124: 
chaieb@26124: lemma (in semiring_1) poly_one_divides[simp]: "[1] divides p"
chaieb@26124: by (simp add: divides_def, auto)
chaieb@26124: 
haftmann@31021: lemma (in idom_char_0) poly_order: "poly p \<noteq> poly []
chaieb@26124:       ==> EX! n. ([-a, 1] %^ n) divides p &
chaieb@26124:                  ~(([-a, 1] %^ (Suc n)) divides p)"
chaieb@26124: apply (auto intro: poly_order_exists simp add: less_linear simp del: pmult_Cons pexp_Suc)
chaieb@26124: apply (cut_tac x = y and y = n in less_linear)
chaieb@26124: apply (drule_tac m = n in poly_exp_divides)
chaieb@26124: apply (auto dest: Suc_le_eq [THEN iffD2, THEN [2] poly_exp_divides]
chaieb@26124:             simp del: pmult_Cons pexp_Suc)
chaieb@26124: done
chaieb@26124: 
chaieb@26124: text{*Order*}
chaieb@26124: 
chaieb@26124: lemma some1_equalityD: "[| n = (@n. P n); EX! n. P n |] ==> P n"
chaieb@26124: by (blast intro: someI2)
chaieb@26124: 
haftmann@31021: lemma (in idom_char_0) order:
chaieb@26124:       "(([-a, 1] %^ n) divides p &
chaieb@26124:         ~(([-a, 1] %^ (Suc n)) divides p)) =
chaieb@26124:         ((n = order a p) & ~(poly p = poly []))"
chaieb@26124: apply (unfold order_def)
chaieb@26124: apply (rule iffI)
chaieb@26124: apply (blast dest: poly_divides_zero intro!: some1_equality [symmetric] poly_order)
chaieb@26124: apply (blast intro!: poly_order [THEN [2] some1_equalityD])
chaieb@26124: done
chaieb@26124: 
haftmann@31021: lemma (in idom_char_0) order2: "[| poly p \<noteq> poly [] |]
chaieb@26124:       ==> ([-a, 1] %^ (order a p)) divides p &
chaieb@26124:               ~(([-a, 1] %^ (Suc(order a p))) divides p)"
chaieb@26124: by (simp add: order del: pexp_Suc)
chaieb@26124: 
haftmann@31021: lemma (in idom_char_0) order_unique: "[| poly p \<noteq> poly []; ([-a, 1] %^ n) divides p;
chaieb@26124:          ~(([-a, 1] %^ (Suc n)) divides p)
chaieb@26124:       |] ==> (n = order a p)"
huffman@30488: by (insert order [of a n p], auto)
chaieb@26124: 
haftmann@31021: lemma (in idom_char_0) order_unique_lemma: "(poly p \<noteq> poly [] & ([-a, 1] %^ n) divides p &
chaieb@26124:          ~(([-a, 1] %^ (Suc n)) divides p))
chaieb@26124:       ==> (n = order a p)"
chaieb@26124: by (blast intro: order_unique)
chaieb@26124: 
chaieb@26124: lemma (in ring_1) order_poly: "poly p = poly q ==> order a p = order a q"
chaieb@26124: by (auto simp add: fun_eq divides_def poly_mult order_def)
chaieb@26124: 
chaieb@26124: lemma (in semiring_1) pexp_one[simp]: "p %^ (Suc 0) = p"
chaieb@26124: apply (induct "p")
chaieb@26124: apply (auto simp add: numeral_1_eq_1)
chaieb@26124: done
chaieb@26124: 
chaieb@26124: lemma (in comm_ring_1) lemma_order_root:
chaieb@26124:      " 0 < n & [- a, 1] %^ n divides p & ~ [- a, 1] %^ (Suc n) divides p
chaieb@26124:              \<Longrightarrow> poly p a = 0"
chaieb@26124: apply (induct n arbitrary: a p, blast)
chaieb@26124: apply (auto simp add: divides_def poly_mult simp del: pmult_Cons)
chaieb@26124: done
chaieb@26124: 
haftmann@31021: lemma (in idom_char_0) order_root: "(poly p a = 0) = ((poly p = poly []) | order a p \<noteq> 0)"
chaieb@26124: proof-
chaieb@26124:   let ?poly = poly
huffman@30488:   show ?thesis
chaieb@26124: apply (case_tac "?poly p = ?poly []", auto)
chaieb@26124: apply (simp add: poly_linear_divides del: pmult_Cons, safe)
chaieb@26124: apply (drule_tac [!] a = a in order2)
chaieb@26124: apply (rule ccontr)
chaieb@26124: apply (simp add: divides_def poly_mult fun_eq del: pmult_Cons, blast)
chaieb@26124: using neq0_conv
chaieb@26124: apply (blast intro: lemma_order_root)
chaieb@26124: done
chaieb@26124: qed
chaieb@26124: 
haftmann@31021: lemma (in idom_char_0) order_divides: "(([-a, 1] %^ n) divides p) = ((poly p = poly []) | n \<le> order a p)"
chaieb@26124: proof-
chaieb@26124:   let ?poly = poly
huffman@30488:   show ?thesis
chaieb@26124: apply (case_tac "?poly p = ?poly []", auto)
chaieb@26124: apply (simp add: divides_def fun_eq poly_mult)
chaieb@26124: apply (rule_tac x = "[]" in exI)
chaieb@26124: apply (auto dest!: order2 [where a=a]
wenzelm@32960:             intro: poly_exp_divides simp del: pexp_Suc)
chaieb@26124: done
chaieb@26124: qed
chaieb@26124: 
haftmann@31021: lemma (in idom_char_0) order_decomp:
chaieb@26124:      "poly p \<noteq> poly []
chaieb@26124:       ==> \<exists>q. (poly p = poly (([-a, 1] %^ (order a p)) *** q)) &
chaieb@26124:                 ~([-a, 1] divides q)"
chaieb@26124: apply (unfold divides_def)
chaieb@26124: apply (drule order2 [where a = a])
chaieb@26124: apply (simp add: divides_def del: pexp_Suc pmult_Cons, safe)
chaieb@26124: apply (rule_tac x = q in exI, safe)
chaieb@26124: apply (drule_tac x = qa in spec)
chaieb@26124: apply (auto simp add: poly_mult fun_eq poly_exp mult_ac simp del: pmult_Cons)
chaieb@26124: done
chaieb@26124: 
chaieb@26124: text{*Important composition properties of orders.*}
chaieb@26124: lemma order_mult: "poly (p *** q) \<noteq> poly []
haftmann@31021:       ==> order a (p *** q) = order a p + order (a::'a::{idom_char_0}) q"
chaieb@26124: apply (cut_tac a = a and p = "p *** q" and n = "order a p + order a q" in order)
chaieb@26124: apply (auto simp add: poly_entire simp del: pmult_Cons)
chaieb@26124: apply (drule_tac a = a in order2)+
chaieb@26124: apply safe
chaieb@26124: apply (simp add: divides_def fun_eq poly_exp_add poly_mult del: pmult_Cons, safe)
chaieb@26124: apply (rule_tac x = "qa *** qaa" in exI)
chaieb@26124: apply (simp add: poly_mult mult_ac del: pmult_Cons)
chaieb@26124: apply (drule_tac a = a in order_decomp)+
chaieb@26124: apply safe
chaieb@26124: apply (subgoal_tac "[-a,1] divides (qa *** qaa) ")
chaieb@26124: apply (simp add: poly_primes del: pmult_Cons)
chaieb@26124: apply (auto simp add: divides_def simp del: pmult_Cons)
chaieb@26124: apply (rule_tac x = qb in exI)
chaieb@26124: apply (subgoal_tac "poly ([-a, 1] %^ (order a p) *** (qa *** qaa)) = poly ([-a, 1] %^ (order a p) *** ([-a, 1] *** qb))")
chaieb@26124: apply (drule poly_mult_left_cancel [THEN iffD1], force)
chaieb@26124: 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))) ")
chaieb@26124: apply (drule poly_mult_left_cancel [THEN iffD1], force)
chaieb@26124: apply (simp add: fun_eq poly_exp_add poly_mult mult_ac del: pmult_Cons)
chaieb@26124: done
chaieb@26124: 
haftmann@31021: lemma (in idom_char_0) order_mult:
chaieb@26124:   assumes pq0: "poly (p *** q) \<noteq> poly []"
chaieb@26124:   shows "order a (p *** q) = order a p + order a q"
chaieb@26124: proof-
chaieb@26124:   let ?order = order
chaieb@26124:   let ?divides = "op divides"
chaieb@26124:   let ?poly = poly
huffman@30488: from pq0
chaieb@26124: show ?thesis
chaieb@26124: apply (cut_tac a = a and p = "pmult p q" and n = "?order a p + ?order a q" in order)
chaieb@26124: apply (auto simp add: poly_entire simp del: pmult_Cons)
chaieb@26124: apply (drule_tac a = a in order2)+
chaieb@26124: apply safe
chaieb@26124: apply (simp add: divides_def fun_eq poly_exp_add poly_mult del: pmult_Cons, safe)
chaieb@26124: apply (rule_tac x = "pmult qa qaa" in exI)
chaieb@26124: apply (simp add: poly_mult mult_ac del: pmult_Cons)
chaieb@26124: apply (drule_tac a = a in order_decomp)+
chaieb@26124: apply safe
chaieb@26124: apply (subgoal_tac "?divides [uminus a,one ] (pmult qa qaa) ")
chaieb@26124: apply (simp add: poly_primes del: pmult_Cons)
chaieb@26124: apply (auto simp add: divides_def simp del: pmult_Cons)
chaieb@26124: apply (rule_tac x = qb in exI)
chaieb@26124: apply (subgoal_tac "?poly (pmult (pexp [uminus a, one] (?order a p)) (pmult qa qaa)) = ?poly (pmult (pexp [uminus a, one] (?order a p)) (pmult [uminus a, one] qb))")
chaieb@26124: apply (drule poly_mult_left_cancel [THEN iffD1], force)
chaieb@26124: apply (subgoal_tac "?poly (pmult (pexp [uminus a, one ] (order a q)) (pmult (pexp [uminus a, one] (order a p)) (pmult qa qaa))) = ?poly (pmult (pexp [uminus a, one] (order a q)) (pmult (pexp [uminus a, one] (order a p)) (pmult [uminus a, one] qb))) ")
chaieb@26124: apply (drule poly_mult_left_cancel [THEN iffD1], force)
chaieb@26124: apply (simp add: fun_eq poly_exp_add poly_mult mult_ac del: pmult_Cons)
chaieb@26124: done
chaieb@26124: qed
chaieb@26124: 
haftmann@31021: lemma (in idom_char_0) order_root2: "poly p \<noteq> poly [] ==> (poly p a = 0) = (order a p \<noteq> 0)"
chaieb@26124: by (rule order_root [THEN ssubst], auto)
chaieb@26124: 
chaieb@26124: lemma (in semiring_1) pmult_one[simp]: "[1] *** p = p" by auto
chaieb@26124: 
chaieb@26124: lemma (in semiring_0) poly_Nil_zero: "poly [] = poly [0]"
chaieb@26124: by (simp add: fun_eq)
chaieb@26124: 
haftmann@31021: lemma (in idom_char_0) rsquarefree_decomp:
chaieb@26124:      "[| rsquarefree p; poly p a = 0 |]
chaieb@26124:       ==> \<exists>q. (poly p = poly ([-a, 1] *** q)) & poly q a \<noteq> 0"
chaieb@26124: apply (simp add: rsquarefree_def, safe)
chaieb@26124: apply (frule_tac a = a in order_decomp)
chaieb@26124: apply (drule_tac x = a in spec)
chaieb@26124: apply (drule_tac a = a in order_root2 [symmetric])
chaieb@26124: apply (auto simp del: pmult_Cons)
chaieb@26124: apply (rule_tac x = q in exI, safe)
chaieb@26124: apply (simp add: poly_mult fun_eq)
chaieb@26124: apply (drule_tac p1 = q in poly_linear_divides [THEN iffD1])
chaieb@26124: apply (simp add: divides_def del: pmult_Cons, safe)
chaieb@26124: apply (drule_tac x = "[]" in spec)
chaieb@26124: apply (auto simp add: fun_eq)
chaieb@26124: done
chaieb@26124: 
chaieb@26124: 
chaieb@26124: text{*Normalization of a polynomial.*}
chaieb@26124: 
chaieb@26124: lemma (in semiring_0) poly_normalize[simp]: "poly (pnormalize p) = poly p"
chaieb@26124: apply (induct "p")
chaieb@26124: apply (auto simp add: fun_eq)
chaieb@26124: done
chaieb@26124: 
chaieb@26124: text{*The degree of a polynomial.*}
chaieb@26124: 
chaieb@26124: lemma (in semiring_0) lemma_degree_zero:
chaieb@26124:      "list_all (%c. c = 0) p \<longleftrightarrow>  pnormalize p = []"
chaieb@26124: by (induct "p", auto)
chaieb@26124: 
huffman@30488: lemma (in idom_char_0) degree_zero:
chaieb@26124:   assumes pN: "poly p = poly []" shows"degree p = 0"
chaieb@26124: proof-
chaieb@26124:   let ?pn = pnormalize
chaieb@26124:   from pN
huffman@30488:   show ?thesis
chaieb@26124:     apply (simp add: degree_def)
chaieb@26124:     apply (case_tac "?pn p = []")
chaieb@26124:     apply (auto simp add: poly_zero lemma_degree_zero )
chaieb@26124:     done
chaieb@26124: qed
chaieb@26124: 
nipkow@32456: lemma (in semiring_0) pnormalize_sing: "(pnormalize [x] = [x]) \<longleftrightarrow> x \<noteq> 0"
nipkow@32456: by simp
chaieb@26124: lemma (in semiring_0) pnormalize_pair: "y \<noteq> 0 \<longleftrightarrow> (pnormalize [x, y] = [x, y])" by simp
huffman@30488: lemma (in semiring_0) pnormal_cons: "pnormal p \<Longrightarrow> pnormal (c#p)"
chaieb@26124:   unfolding pnormal_def by simp
chaieb@26124: lemma (in semiring_0) pnormal_tail: "p\<noteq>[] \<Longrightarrow> pnormal (c#p) \<Longrightarrow> pnormal p"
nipkow@32456:   unfolding pnormal_def by(auto split: split_if_asm)
chaieb@26124: 
chaieb@26124: 
chaieb@26124: lemma (in semiring_0) pnormal_last_nonzero: "pnormal p ==> last p \<noteq> 0"
nipkow@32456: by(induct p) (simp_all add: pnormal_def split: split_if_asm)
chaieb@26124: 
chaieb@26124: lemma (in semiring_0) pnormal_length: "pnormal p \<Longrightarrow> 0 < length p"
chaieb@26124:   unfolding pnormal_def length_greater_0_conv by blast
chaieb@26124: 
chaieb@26124: lemma (in semiring_0) pnormal_last_length: "\<lbrakk>0 < length p ; last p \<noteq> 0\<rbrakk> \<Longrightarrow> pnormal p"
nipkow@32456: by (induct p) (auto simp: pnormal_def  split: split_if_asm)
nipkow@32456: 
chaieb@26124: 
chaieb@26124: lemma (in semiring_0) pnormal_id: "pnormal p \<longleftrightarrow> (0 < length p \<and> last p \<noteq> 0)"
chaieb@26124:   using pnormal_last_length pnormal_length pnormal_last_nonzero by blast
chaieb@26124: 
chaieb@26124: lemma (in idom_char_0) poly_Cons_eq: "poly (c#cs) = poly (d#ds) \<longleftrightarrow> c=d \<and> poly cs = poly ds" (is "?lhs \<longleftrightarrow> ?rhs")
chaieb@26124: proof
chaieb@26124:   assume eq: ?lhs
chaieb@26124:   hence "\<And>x. poly ((c#cs) +++ -- (d#ds)) x = 0"
nipkow@29667:     by (simp only: poly_minus poly_add algebra_simps) simp
nipkow@39302:   hence "poly ((c#cs) +++ -- (d#ds)) = poly []" by(simp add: fun_eq_iff)
chaieb@26124:   hence "c = d \<and> list_all (\<lambda>x. x=0) ((cs +++ -- ds))"
nipkow@29667:     unfolding poly_zero by (simp add: poly_minus_def algebra_simps)
chaieb@26124:   hence "c = d \<and> (\<forall>x. poly (cs +++ -- ds) x = 0)"
huffman@30488:     unfolding poly_zero[symmetric] by simp
nipkow@39302:   thus ?rhs  by (simp add: poly_minus poly_add algebra_simps fun_eq_iff)
chaieb@26124: next
nipkow@39302:   assume ?rhs then show ?lhs by(simp add:fun_eq_iff)
chaieb@26124: qed
huffman@30488: 
chaieb@26124: lemma (in idom_char_0) pnormalize_unique: "poly p = poly q \<Longrightarrow> pnormalize p = pnormalize q"
chaieb@26124: proof(induct q arbitrary: p)
chaieb@26124:   case Nil thus ?case by (simp only: poly_zero lemma_degree_zero) simp
chaieb@26124: next
chaieb@26124:   case (Cons c cs p)
chaieb@26124:   thus ?case
chaieb@26124:   proof(induct p)
chaieb@26124:     case Nil
chaieb@26124:     hence "poly [] = poly (c#cs)" by blast
huffman@30488:     then have "poly (c#cs) = poly [] " by simp
chaieb@26124:     thus ?case by (simp only: poly_zero lemma_degree_zero) simp
chaieb@26124:   next
chaieb@26124:     case (Cons d ds)
chaieb@26124:     hence eq: "poly (d # ds) = poly (c # cs)" by blast
chaieb@26124:     hence eq': "\<And>x. poly (d # ds) x = poly (c # cs) x" by simp
chaieb@26124:     hence "poly (d # ds) 0 = poly (c # cs) 0" by blast
chaieb@26124:     hence dc: "d = c" by auto
chaieb@26124:     with eq have "poly ds = poly cs"
chaieb@26124:       unfolding  poly_Cons_eq by simp
chaieb@26124:     with Cons.prems have "pnormalize ds = pnormalize cs" by blast
chaieb@26124:     with dc show ?case by simp
chaieb@26124:   qed
chaieb@26124: qed
chaieb@26124: 
chaieb@26124: lemma (in idom_char_0) degree_unique: assumes pq: "poly p = poly q"
chaieb@26124:   shows "degree p = degree q"
chaieb@26124: using pnormalize_unique[OF pq] unfolding degree_def by simp
chaieb@26124: 
chaieb@26124: lemma (in semiring_0) pnormalize_length: "length (pnormalize p) \<le> length p" by (induct p, auto)
chaieb@26124: 
huffman@30488: lemma (in semiring_0) last_linear_mul_lemma:
chaieb@26124:   "last ((a %* p) +++ (x#(b %* p))) = (if p=[] then x else b*last p)"
chaieb@26124: 
chaieb@26124: apply (induct p arbitrary: a x b, auto)
chaieb@26124: apply (subgoal_tac "padd (cmult aa p) (times b a # cmult b p) \<noteq> []", simp)
chaieb@26124: apply (induct_tac p, auto)
chaieb@26124: done
chaieb@26124: 
chaieb@26124: lemma (in semiring_1) last_linear_mul: assumes p:"p\<noteq>[]" shows "last ([a,1] *** p) = last p"
chaieb@26124: proof-
chaieb@26124:   from p obtain c cs where cs: "p = c#cs" by (cases p, auto)
chaieb@26124:   from cs have eq:"[a,1] *** p = (a %* (c#cs)) +++ (0#(1 %* (c#cs)))"
chaieb@26124:     by (simp add: poly_cmult_distr)
chaieb@26124:   show ?thesis using cs
chaieb@26124:     unfolding eq last_linear_mul_lemma by simp
chaieb@26124: qed
chaieb@26124: 
chaieb@26124: lemma (in semiring_0) pnormalize_eq: "last p \<noteq> 0 \<Longrightarrow> pnormalize p = p"
nipkow@32456: by (induct p) (auto split: split_if_asm)
chaieb@26124: 
chaieb@26124: lemma (in semiring_0) last_pnormalize: "pnormalize p \<noteq> [] \<Longrightarrow> last (pnormalize p) \<noteq> 0"
chaieb@26124:   by (induct p, auto)
chaieb@26124: 
chaieb@26124: lemma (in semiring_0) pnormal_degree: "last p \<noteq> 0 \<Longrightarrow> degree p = length p - 1"
chaieb@26124:   using pnormalize_eq[of p] unfolding degree_def by simp
chaieb@26124: 
wenzelm@26313: lemma (in semiring_0) poly_Nil_ext: "poly [] = (\<lambda>x. 0)" by (rule ext) simp
chaieb@26124: 
chaieb@26124: lemma (in idom_char_0) linear_mul_degree: assumes p: "poly p \<noteq> poly []"
chaieb@26124:   shows "degree ([a,1] *** p) = degree p + 1"
chaieb@26124: proof-
chaieb@26124:   from p have pnz: "pnormalize p \<noteq> []"
chaieb@26124:     unfolding poly_zero lemma_degree_zero .
huffman@30488: 
chaieb@26124:   from last_linear_mul[OF pnz, of a] last_pnormalize[OF pnz]
chaieb@26124:   have l0: "last ([a, 1] *** pnormalize p) \<noteq> 0" by simp
chaieb@26124:   from last_pnormalize[OF pnz] last_linear_mul[OF pnz, of a]
chaieb@26124:     pnormal_degree[OF l0] pnormal_degree[OF last_pnormalize[OF pnz]] pnz
huffman@30488: 
chaieb@26124: 
huffman@30488:   have th: "degree ([a,1] *** pnormalize p) = degree (pnormalize p) + 1"
chaieb@26124:     by (auto simp add: poly_length_mult)
chaieb@26124: 
chaieb@26124:   have eqs: "poly ([a,1] *** pnormalize p) = poly ([a,1] *** p)"
chaieb@26124:     by (rule ext) (simp add: poly_mult poly_add poly_cmult)
chaieb@26124:   from degree_unique[OF eqs] th
chaieb@26124:   show ?thesis by (simp add: degree_unique[OF poly_normalize])
chaieb@26124: qed
chaieb@26124: 
chaieb@26124: lemma (in idom_char_0) linear_pow_mul_degree:
chaieb@26124:   "degree([a,1] %^n *** p) = (if poly p = poly [] then 0 else degree p + n)"
chaieb@26124: proof(induct n arbitrary: a p)
chaieb@26124:   case (0 a p)
chaieb@26124:   {assume p: "poly p = poly []"
chaieb@26124:     hence ?case using degree_unique[OF p] by (simp add: degree_def)}
chaieb@26124:   moreover
wenzelm@26313:   {assume p: "poly p \<noteq> poly []" hence ?case by (auto simp add: poly_Nil_ext) }
chaieb@26124:   ultimately show ?case by blast
chaieb@26124: next
chaieb@26124:   case (Suc n a p)
chaieb@26124:   have eq: "poly ([a,1] %^(Suc n) *** p) = poly ([a,1] %^ n *** ([a,1] *** p))"
chaieb@26124:     apply (rule ext, simp add: poly_mult poly_add poly_cmult)
webertj@49962:     by (simp add: mult_ac add_ac distrib_left)
chaieb@26124:   note deq = degree_unique[OF eq]
chaieb@26124:   {assume p: "poly p = poly []"
huffman@30488:     with eq have eq': "poly ([a,1] %^(Suc n) *** p) = poly []"
chaieb@26124:       by - (rule ext,simp add: poly_mult poly_cmult poly_add)
chaieb@26124:     from degree_unique[OF eq'] p have ?case by (simp add: degree_def)}
chaieb@26124:   moreover
chaieb@26124:   {assume p: "poly p \<noteq> poly []"
chaieb@26124:     from p have ap: "poly ([a,1] *** p) \<noteq> poly []"
huffman@30488:       using poly_mult_not_eq_poly_Nil unfolding poly_entire by auto
chaieb@26124:     have eq: "poly ([a,1] %^(Suc n) *** p) = poly ([a,1]%^n *** ([a,1] *** p))"
nipkow@29667:      by (rule ext, simp add: poly_mult poly_add poly_exp poly_cmult algebra_simps)
chaieb@26124:    from ap have ap': "(poly ([a,1] *** p) = poly []) = False" by blast
chaieb@26124:    have  th0: "degree ([a,1]%^n *** ([a,1] *** p)) = degree ([a,1] *** p) + n"
chaieb@26124:      apply (simp only: Suc.hyps[of a "pmult [a,one] p"] ap')
chaieb@26124:      by simp
huffman@30488: 
chaieb@26124:    from degree_unique[OF eq] ap p th0 linear_mul_degree[OF p, of a]
chaieb@26124:    have ?case by (auto simp del: poly.simps)}
chaieb@26124:   ultimately show ?case by blast
chaieb@26124: qed
chaieb@26124: 
haftmann@31021: lemma (in idom_char_0) order_degree:
chaieb@26124:   assumes p0: "poly p \<noteq> poly []"
chaieb@26124:   shows "order a p \<le> degree p"
chaieb@26124: proof-
chaieb@26124:   from order2[OF p0, unfolded divides_def]
chaieb@26124:   obtain q where q: "poly p = poly ([- a, 1]%^ (order a p) *** q)" by blast
chaieb@26124:   {assume "poly q = poly []"
chaieb@26124:     with q p0 have False by (simp add: poly_mult poly_entire)}
huffman@30488:   with degree_unique[OF q, unfolded linear_pow_mul_degree]
chaieb@26124:   show ?thesis by auto
chaieb@26124: qed
chaieb@26124: 
chaieb@26124: text{*Tidier versions of finiteness of roots.*}
chaieb@26124: 
chaieb@26124: lemma (in idom_char_0) poly_roots_finite_set: "poly p \<noteq> poly [] ==> finite {x. poly p x = 0}"
chaieb@26124: unfolding poly_roots_finite .
chaieb@26124: 
chaieb@26124: text{*bound for polynomial.*}
chaieb@26124: 
haftmann@35028: lemma poly_mono: "abs(x) \<le> k ==> abs(poly p (x::'a::{linordered_idom})) \<le> poly (map abs p) k"
chaieb@26124: apply (induct "p", auto)
chaieb@26124: apply (rule_tac y = "abs a + abs (x * poly p x)" in order_trans)
chaieb@26124: apply (rule abs_triangle_ineq)
chaieb@26124: apply (auto intro!: mult_mono simp add: abs_mult)
chaieb@26124: done
chaieb@26124: 
chaieb@26124: lemma (in semiring_0) poly_Sing: "poly [c] x = c" by simp
chaieb@26124: 
chaieb@26124: end




