huffman@29451: (* Title: HOL/Polynomial.thy
huffman@29451: Author: Brian Huffman
huffman@29451: Based on an earlier development by Clemens Ballarin
huffman@29451: *)
huffman@29451:
huffman@29451: header {* Univariate Polynomials *}
huffman@29451:
huffman@29451: theory Polynomial
huffman@29451: imports Plain SetInterval
huffman@29451: begin
huffman@29451:
huffman@29451: subsection {* Definition of type @{text poly} *}
huffman@29451:
huffman@29451: typedef (Poly) 'a poly = "{f::nat \ 'a::zero. \n. \i>n. f i = 0}"
huffman@29451: morphisms coeff Abs_poly
huffman@29451: by auto
huffman@29451:
huffman@29451: lemma expand_poly_eq: "p = q \ (\n. coeff p n = coeff q n)"
huffman@29451: by (simp add: coeff_inject [symmetric] expand_fun_eq)
huffman@29451:
huffman@29451: lemma poly_ext: "(\n. coeff p n = coeff q n) \ p = q"
huffman@29451: by (simp add: expand_poly_eq)
huffman@29451:
huffman@29451:
huffman@29451: subsection {* Degree of a polynomial *}
huffman@29451:
huffman@29451: definition
huffman@29451: degree :: "'a::zero poly \ nat" where
huffman@29451: "degree p = (LEAST n. \i>n. coeff p i = 0)"
huffman@29451:
huffman@29451: lemma coeff_eq_0: "degree p < n \ coeff p n = 0"
huffman@29451: proof -
huffman@29451: have "coeff p \ Poly"
huffman@29451: by (rule coeff)
huffman@29451: hence "\n. \i>n. coeff p i = 0"
huffman@29451: unfolding Poly_def by simp
huffman@29451: hence "\i>degree p. coeff p i = 0"
huffman@29451: unfolding degree_def by (rule LeastI_ex)
huffman@29451: moreover assume "degree p < n"
huffman@29451: ultimately show ?thesis by simp
huffman@29451: qed
huffman@29451:
huffman@29451: lemma le_degree: "coeff p n \ 0 \ n \ degree p"
huffman@29451: by (erule contrapos_np, rule coeff_eq_0, simp)
huffman@29451:
huffman@29451: lemma degree_le: "\i>n. coeff p i = 0 \ degree p \ n"
huffman@29451: unfolding degree_def by (erule Least_le)
huffman@29451:
huffman@29451: lemma less_degree_imp: "n < degree p \ \i>n. coeff p i \ 0"
huffman@29451: unfolding degree_def by (drule not_less_Least, simp)
huffman@29451:
huffman@29451:
huffman@29451: subsection {* The zero polynomial *}
huffman@29451:
huffman@29451: instantiation poly :: (zero) zero
huffman@29451: begin
huffman@29451:
huffman@29451: definition
huffman@29451: zero_poly_def: "0 = Abs_poly (\n. 0)"
huffman@29451:
huffman@29451: instance ..
huffman@29451: end
huffman@29451:
huffman@29451: lemma coeff_0 [simp]: "coeff 0 n = 0"
huffman@29451: unfolding zero_poly_def
huffman@29451: by (simp add: Abs_poly_inverse Poly_def)
huffman@29451:
huffman@29451: lemma degree_0 [simp]: "degree 0 = 0"
huffman@29451: by (rule order_antisym [OF degree_le le0]) simp
huffman@29451:
huffman@29451: lemma leading_coeff_neq_0:
huffman@29451: assumes "p \ 0" shows "coeff p (degree p) \ 0"
huffman@29451: proof (cases "degree p")
huffman@29451: case 0
huffman@29451: from `p \ 0` have "\n. coeff p n \ 0"
huffman@29451: by (simp add: expand_poly_eq)
huffman@29451: then obtain n where "coeff p n \ 0" ..
huffman@29451: hence "n \ degree p" by (rule le_degree)
huffman@29451: with `coeff p n \ 0` and `degree p = 0`
huffman@29451: show "coeff p (degree p) \ 0" by simp
huffman@29451: next
huffman@29451: case (Suc n)
huffman@29451: from `degree p = Suc n` have "n < degree p" by simp
huffman@29451: hence "\i>n. coeff p i \ 0" by (rule less_degree_imp)
huffman@29451: then obtain i where "n < i" and "coeff p i \ 0" by fast
huffman@29451: from `degree p = Suc n` and `n < i` have "degree p \ i" by simp
huffman@29451: also from `coeff p i \ 0` have "i \ degree p" by (rule le_degree)
huffman@29451: finally have "degree p = i" .
huffman@29451: with `coeff p i \ 0` show "coeff p (degree p) \ 0" by simp
huffman@29451: qed
huffman@29451:
huffman@29451: lemma leading_coeff_0_iff [simp]: "coeff p (degree p) = 0 \ p = 0"
huffman@29451: by (cases "p = 0", simp, simp add: leading_coeff_neq_0)
huffman@29451:
huffman@29451:
huffman@29451: subsection {* List-style constructor for polynomials *}
huffman@29451:
huffman@29451: definition
huffman@29451: pCons :: "'a::zero \ 'a poly \ 'a poly"
huffman@29451: where
huffman@29451: [code del]: "pCons a p = Abs_poly (nat_case a (coeff p))"
huffman@29451:
huffman@29451: lemma Poly_nat_case: "f \ Poly \ nat_case a f \ Poly"
huffman@29451: unfolding Poly_def by (auto split: nat.split)
huffman@29451:
huffman@29451: lemma coeff_pCons:
huffman@29451: "coeff (pCons a p) = nat_case a (coeff p)"
huffman@29451: unfolding pCons_def
huffman@29451: by (simp add: Abs_poly_inverse Poly_nat_case coeff)
huffman@29451:
huffman@29451: lemma coeff_pCons_0 [simp]: "coeff (pCons a p) 0 = a"
huffman@29451: by (simp add: coeff_pCons)
huffman@29451:
huffman@29451: lemma coeff_pCons_Suc [simp]: "coeff (pCons a p) (Suc n) = coeff p n"
huffman@29451: by (simp add: coeff_pCons)
huffman@29451:
huffman@29451: lemma degree_pCons_le: "degree (pCons a p) \ Suc (degree p)"
huffman@29451: by (rule degree_le, simp add: coeff_eq_0 coeff_pCons split: nat.split)
huffman@29451:
huffman@29451: lemma degree_pCons_eq:
huffman@29451: "p \ 0 \ degree (pCons a p) = Suc (degree p)"
huffman@29451: apply (rule order_antisym [OF degree_pCons_le])
huffman@29451: apply (rule le_degree, simp)
huffman@29451: done
huffman@29451:
huffman@29451: lemma degree_pCons_0: "degree (pCons a 0) = 0"
huffman@29451: apply (rule order_antisym [OF _ le0])
huffman@29451: apply (rule degree_le, simp add: coeff_pCons split: nat.split)
huffman@29451: done
huffman@29451:
huffman@29451: lemma degree_pCons_eq_if:
huffman@29451: "degree (pCons a p) = (if p = 0 then 0 else Suc (degree p))"
huffman@29451: apply (cases "p = 0", simp_all)
huffman@29451: apply (rule order_antisym [OF _ le0])
huffman@29451: apply (rule degree_le, simp add: coeff_pCons split: nat.split)
huffman@29451: apply (rule order_antisym [OF degree_pCons_le])
huffman@29451: apply (rule le_degree, simp)
huffman@29451: done
huffman@29451:
huffman@29451: lemma pCons_0_0 [simp]: "pCons 0 0 = 0"
huffman@29451: by (rule poly_ext, simp add: coeff_pCons split: nat.split)
huffman@29451:
huffman@29451: lemma pCons_eq_iff [simp]:
huffman@29451: "pCons a p = pCons b q \ a = b \ p = q"
huffman@29451: proof (safe)
huffman@29451: assume "pCons a p = pCons b q"
huffman@29451: then have "coeff (pCons a p) 0 = coeff (pCons b q) 0" by simp
huffman@29451: then show "a = b" by simp
huffman@29451: next
huffman@29451: assume "pCons a p = pCons b q"
huffman@29451: then have "\n. coeff (pCons a p) (Suc n) =
huffman@29451: coeff (pCons b q) (Suc n)" by simp
huffman@29451: then show "p = q" by (simp add: expand_poly_eq)
huffman@29451: qed
huffman@29451:
huffman@29451: lemma pCons_eq_0_iff [simp]: "pCons a p = 0 \ a = 0 \ p = 0"
huffman@29451: using pCons_eq_iff [of a p 0 0] by simp
huffman@29451:
huffman@29451: lemma Poly_Suc: "f \ Poly \ (\n. f (Suc n)) \ Poly"
huffman@29451: unfolding Poly_def
huffman@29451: by (clarify, rule_tac x=n in exI, simp)
huffman@29451:
huffman@29451: lemma pCons_cases [cases type: poly]:
huffman@29451: obtains (pCons) a q where "p = pCons a q"
huffman@29451: proof
huffman@29451: show "p = pCons (coeff p 0) (Abs_poly (\n. coeff p (Suc n)))"
huffman@29451: by (rule poly_ext)
huffman@29451: (simp add: Abs_poly_inverse Poly_Suc coeff coeff_pCons
huffman@29451: split: nat.split)
huffman@29451: qed
huffman@29451:
huffman@29451: lemma pCons_induct [case_names 0 pCons, induct type: poly]:
huffman@29451: assumes zero: "P 0"
huffman@29451: assumes pCons: "\a p. P p \ P (pCons a p)"
huffman@29451: shows "P p"
huffman@29451: proof (induct p rule: measure_induct_rule [where f=degree])
huffman@29451: case (less p)
huffman@29451: obtain a q where "p = pCons a q" by (rule pCons_cases)
huffman@29451: have "P q"
huffman@29451: proof (cases "q = 0")
huffman@29451: case True
huffman@29451: then show "P q" by (simp add: zero)
huffman@29451: next
huffman@29451: case False
huffman@29451: then have "degree (pCons a q) = Suc (degree q)"
huffman@29451: by (rule degree_pCons_eq)
huffman@29451: then have "degree q < degree p"
huffman@29451: using `p = pCons a q` by simp
huffman@29451: then show "P q"
huffman@29451: by (rule less.hyps)
huffman@29451: qed
huffman@29451: then have "P (pCons a q)"
huffman@29451: by (rule pCons)
huffman@29451: then show ?case
huffman@29451: using `p = pCons a q` by simp
huffman@29451: qed
huffman@29451:
huffman@29451:
huffman@29451: subsection {* Monomials *}
huffman@29451:
huffman@29451: definition
huffman@29451: monom :: "'a \ nat \ 'a::zero poly" where
huffman@29451: "monom a m = Abs_poly (\n. if m = n then a else 0)"
huffman@29451:
huffman@29451: lemma coeff_monom [simp]: "coeff (monom a m) n = (if m=n then a else 0)"
huffman@29451: unfolding monom_def
huffman@29451: by (subst Abs_poly_inverse, auto simp add: Poly_def)
huffman@29451:
huffman@29451: lemma monom_0: "monom a 0 = pCons a 0"
huffman@29451: by (rule poly_ext, simp add: coeff_pCons split: nat.split)
huffman@29451:
huffman@29451: lemma monom_Suc: "monom a (Suc n) = pCons 0 (monom a n)"
huffman@29451: by (rule poly_ext, simp add: coeff_pCons split: nat.split)
huffman@29451:
huffman@29451: lemma monom_eq_0 [simp]: "monom 0 n = 0"
huffman@29451: by (rule poly_ext) simp
huffman@29451:
huffman@29451: lemma monom_eq_0_iff [simp]: "monom a n = 0 \ a = 0"
huffman@29451: by (simp add: expand_poly_eq)
huffman@29451:
huffman@29451: lemma monom_eq_iff [simp]: "monom a n = monom b n \ a = b"
huffman@29451: by (simp add: expand_poly_eq)
huffman@29451:
huffman@29451: lemma degree_monom_le: "degree (monom a n) \ n"
huffman@29451: by (rule degree_le, simp)
huffman@29451:
huffman@29451: lemma degree_monom_eq: "a \ 0 \ degree (monom a n) = n"
huffman@29451: apply (rule order_antisym [OF degree_monom_le])
huffman@29451: apply (rule le_degree, simp)
huffman@29451: done
huffman@29451:
huffman@29451:
huffman@29451: subsection {* Addition and subtraction *}
huffman@29451:
huffman@29451: instantiation poly :: (comm_monoid_add) comm_monoid_add
huffman@29451: begin
huffman@29451:
huffman@29451: definition
huffman@29451: plus_poly_def [code del]:
huffman@29451: "p + q = Abs_poly (\n. coeff p n + coeff q n)"
huffman@29451:
huffman@29451: lemma Poly_add:
huffman@29451: fixes f g :: "nat \ 'a"
huffman@29451: shows "\f \ Poly; g \ Poly\ \ (\n. f n + g n) \ Poly"
huffman@29451: unfolding Poly_def
huffman@29451: apply (clarify, rename_tac m n)
huffman@29451: apply (rule_tac x="max m n" in exI, simp)
huffman@29451: done
huffman@29451:
huffman@29451: lemma coeff_add [simp]:
huffman@29451: "coeff (p + q) n = coeff p n + coeff q n"
huffman@29451: unfolding plus_poly_def
huffman@29451: by (simp add: Abs_poly_inverse coeff Poly_add)
huffman@29451:
huffman@29451: instance proof
huffman@29451: fix p q r :: "'a poly"
huffman@29451: show "(p + q) + r = p + (q + r)"
huffman@29451: by (simp add: expand_poly_eq add_assoc)
huffman@29451: show "p + q = q + p"
huffman@29451: by (simp add: expand_poly_eq add_commute)
huffman@29451: show "0 + p = p"
huffman@29451: by (simp add: expand_poly_eq)
huffman@29451: qed
huffman@29451:
huffman@29451: end
huffman@29451:
huffman@29451: instantiation poly :: (ab_group_add) ab_group_add
huffman@29451: begin
huffman@29451:
huffman@29451: definition
huffman@29451: uminus_poly_def [code del]:
huffman@29451: "- p = Abs_poly (\n. - coeff p n)"
huffman@29451:
huffman@29451: definition
huffman@29451: minus_poly_def [code del]:
huffman@29451: "p - q = Abs_poly (\n. coeff p n - coeff q n)"
huffman@29451:
huffman@29451: lemma Poly_minus:
huffman@29451: fixes f :: "nat \ 'a"
huffman@29451: shows "f \ Poly \ (\n. - f n) \ Poly"
huffman@29451: unfolding Poly_def by simp
huffman@29451:
huffman@29451: lemma Poly_diff:
huffman@29451: fixes f g :: "nat \ 'a"
huffman@29451: shows "\f \ Poly; g \ Poly\ \ (\n. f n - g n) \ Poly"
huffman@29451: unfolding diff_minus by (simp add: Poly_add Poly_minus)
huffman@29451:
huffman@29451: lemma coeff_minus [simp]: "coeff (- p) n = - coeff p n"
huffman@29451: unfolding uminus_poly_def
huffman@29451: by (simp add: Abs_poly_inverse coeff Poly_minus)
huffman@29451:
huffman@29451: lemma coeff_diff [simp]:
huffman@29451: "coeff (p - q) n = coeff p n - coeff q n"
huffman@29451: unfolding minus_poly_def
huffman@29451: by (simp add: Abs_poly_inverse coeff Poly_diff)
huffman@29451:
huffman@29451: instance proof
huffman@29451: fix p q :: "'a poly"
huffman@29451: show "- p + p = 0"
huffman@29451: by (simp add: expand_poly_eq)
huffman@29451: show "p - q = p + - q"
huffman@29451: by (simp add: expand_poly_eq diff_minus)
huffman@29451: qed
huffman@29451:
huffman@29451: end
huffman@29451:
huffman@29451: lemma add_pCons [simp]:
huffman@29451: "pCons a p + pCons b q = pCons (a + b) (p + q)"
huffman@29451: by (rule poly_ext, simp add: coeff_pCons split: nat.split)
huffman@29451:
huffman@29451: lemma minus_pCons [simp]:
huffman@29451: "- pCons a p = pCons (- a) (- p)"
huffman@29451: by (rule poly_ext, simp add: coeff_pCons split: nat.split)
huffman@29451:
huffman@29451: lemma diff_pCons [simp]:
huffman@29451: "pCons a p - pCons b q = pCons (a - b) (p - q)"
huffman@29451: by (rule poly_ext, simp add: coeff_pCons split: nat.split)
huffman@29451:
huffman@29451: lemma degree_add_le: "degree (p + q) \ max (degree p) (degree q)"
huffman@29451: by (rule degree_le, auto simp add: coeff_eq_0)
huffman@29451:
huffman@29451: lemma degree_add_eq_right:
huffman@29451: "degree p < degree q \ degree (p + q) = degree q"
huffman@29451: apply (cases "q = 0", simp)
huffman@29451: apply (rule order_antisym)
huffman@29451: apply (rule ord_le_eq_trans [OF degree_add_le])
huffman@29451: apply simp
huffman@29451: apply (rule le_degree)
huffman@29451: apply (simp add: coeff_eq_0)
huffman@29451: done
huffman@29451:
huffman@29451: lemma degree_add_eq_left:
huffman@29451: "degree q < degree p \ degree (p + q) = degree p"
huffman@29451: using degree_add_eq_right [of q p]
huffman@29451: by (simp add: add_commute)
huffman@29451:
huffman@29451: lemma degree_minus [simp]: "degree (- p) = degree p"
huffman@29451: unfolding degree_def by simp
huffman@29451:
huffman@29451: lemma degree_diff_le: "degree (p - q) \ max (degree p) (degree q)"
huffman@29451: using degree_add_le [where p=p and q="-q"]
huffman@29451: by (simp add: diff_minus)
huffman@29451:
huffman@29451: lemma add_monom: "monom a n + monom b n = monom (a + b) n"
huffman@29451: by (rule poly_ext) simp
huffman@29451:
huffman@29451: lemma diff_monom: "monom a n - monom b n = monom (a - b) n"
huffman@29451: by (rule poly_ext) simp
huffman@29451:
huffman@29451: lemma minus_monom: "- monom a n = monom (-a) n"
huffman@29451: by (rule poly_ext) simp
huffman@29451:
huffman@29451: lemma coeff_setsum: "coeff (\x\A. p x) i = (\x\A. coeff (p x) i)"
huffman@29451: by (cases "finite A", induct set: finite, simp_all)
huffman@29451:
huffman@29451: lemma monom_setsum: "monom (\x\A. a x) n = (\x\A. monom (a x) n)"
huffman@29451: by (rule poly_ext) (simp add: coeff_setsum)
huffman@29451:
huffman@29451:
huffman@29451: subsection {* Multiplication by a constant *}
huffman@29451:
huffman@29451: definition
huffman@29451: smult :: "'a::comm_semiring_0 \ 'a poly \ 'a poly" where
huffman@29451: "smult a p = Abs_poly (\n. a * coeff p n)"
huffman@29451:
huffman@29451: lemma Poly_smult:
huffman@29451: fixes f :: "nat \ 'a::comm_semiring_0"
huffman@29451: shows "f \ Poly \ (\n. a * f n) \ Poly"
huffman@29451: unfolding Poly_def
huffman@29451: by (clarify, rule_tac x=n in exI, simp)
huffman@29451:
huffman@29451: lemma coeff_smult [simp]: "coeff (smult a p) n = a * coeff p n"
huffman@29451: unfolding smult_def
huffman@29451: by (simp add: Abs_poly_inverse Poly_smult coeff)
huffman@29451:
huffman@29451: lemma degree_smult_le: "degree (smult a p) \ degree p"
huffman@29451: by (rule degree_le, simp add: coeff_eq_0)
huffman@29451:
huffman@29451: lemma smult_smult: "smult a (smult b p) = smult (a * b) p"
huffman@29451: by (rule poly_ext, simp add: mult_assoc)
huffman@29451:
huffman@29451: lemma smult_0_right [simp]: "smult a 0 = 0"
huffman@29451: by (rule poly_ext, simp)
huffman@29451:
huffman@29451: lemma smult_0_left [simp]: "smult 0 p = 0"
huffman@29451: by (rule poly_ext, simp)
huffman@29451:
huffman@29451: lemma smult_1_left [simp]: "smult (1::'a::comm_semiring_1) p = p"
huffman@29451: by (rule poly_ext, simp)
huffman@29451:
huffman@29451: lemma smult_add_right:
huffman@29451: "smult a (p + q) = smult a p + smult a q"
huffman@29451: by (rule poly_ext, simp add: ring_simps)
huffman@29451:
huffman@29451: lemma smult_add_left:
huffman@29451: "smult (a + b) p = smult a p + smult b p"
huffman@29451: by (rule poly_ext, simp add: ring_simps)
huffman@29451:
huffman@29451: lemma smult_minus_right:
huffman@29451: "smult (a::'a::comm_ring) (- p) = - smult a p"
huffman@29451: by (rule poly_ext, simp)
huffman@29451:
huffman@29451: lemma smult_minus_left:
huffman@29451: "smult (- a::'a::comm_ring) p = - smult a p"
huffman@29451: by (rule poly_ext, simp)
huffman@29451:
huffman@29451: lemma smult_diff_right:
huffman@29451: "smult (a::'a::comm_ring) (p - q) = smult a p - smult a q"
huffman@29451: by (rule poly_ext, simp add: ring_simps)
huffman@29451:
huffman@29451: lemma smult_diff_left:
huffman@29451: "smult (a - b::'a::comm_ring) p = smult a p - smult b p"
huffman@29451: by (rule poly_ext, simp add: ring_simps)
huffman@29451:
huffman@29451: lemma smult_pCons [simp]:
huffman@29451: "smult a (pCons b p) = pCons (a * b) (smult a p)"
huffman@29451: by (rule poly_ext, simp add: coeff_pCons split: nat.split)
huffman@29451:
huffman@29451: lemma smult_monom: "smult a (monom b n) = monom (a * b) n"
huffman@29451: by (induct n, simp add: monom_0, simp add: monom_Suc)
huffman@29451:
huffman@29451:
huffman@29451: subsection {* Multiplication of polynomials *}
huffman@29451:
huffman@29451: lemma Poly_mult_lemma:
huffman@29451: fixes f g :: "nat \ 'a::comm_semiring_0" and m n :: nat
huffman@29451: assumes "\i>m. f i = 0"
huffman@29451: assumes "\j>n. g j = 0"
huffman@29451: shows "\k>m+n. (\i\k. f i * g (k-i)) = 0"
huffman@29451: proof (clarify)
huffman@29451: fix k :: nat
huffman@29451: assume "m + n < k"
huffman@29451: show "(\i\k. f i * g (k - i)) = 0"
huffman@29451: proof (rule setsum_0' [rule_format])
huffman@29451: fix i :: nat
huffman@29451: assume "i \ {..k}" hence "i \ k" by simp
huffman@29451: with `m + n < k` have "m < i \ n < k - i" by arith
huffman@29451: thus "f i * g (k - i) = 0"
huffman@29451: using prems by auto
huffman@29451: qed
huffman@29451: qed
huffman@29451:
huffman@29451: lemma Poly_mult:
huffman@29451: fixes f g :: "nat \ 'a::comm_semiring_0"
huffman@29451: shows "\f \ Poly; g \ Poly\ \ (\n. \i\n. f i * g (n-i)) \ Poly"
huffman@29451: unfolding Poly_def
huffman@29451: by (safe, rule exI, rule Poly_mult_lemma)
huffman@29451:
huffman@29451: lemma poly_mult_assoc_lemma:
huffman@29451: fixes k :: nat and f :: "nat \ nat \ nat \ 'a::comm_monoid_add"
huffman@29451: shows "(\j\k. \i\j. f i (j - i) (n - j)) =
huffman@29451: (\j\k. \i\k - j. f j i (n - j - i))"
huffman@29451: proof (induct k)
huffman@29451: case 0 show ?case by simp
huffman@29451: next
huffman@29451: case (Suc k) thus ?case
huffman@29451: by (simp add: Suc_diff_le setsum_addf add_assoc
huffman@29451: cong: strong_setsum_cong)
huffman@29451: qed
huffman@29451:
huffman@29451: lemma poly_mult_commute_lemma:
huffman@29451: fixes n :: nat and f :: "nat \ nat \ 'a::comm_monoid_add"
huffman@29451: shows "(\i\n. f i (n - i)) = (\i\n. f (n - i) i)"
huffman@29451: proof (rule setsum_reindex_cong)
huffman@29451: show "inj_on (\i. n - i) {..n}"
huffman@29451: by (rule inj_onI) simp
huffman@29451: show "{..n} = (\i. n - i) ` {..n}"
huffman@29451: by (auto, rule_tac x="n - x" in image_eqI, simp_all)
huffman@29451: next
huffman@29451: fix i assume "i \ {..n}"
huffman@29451: hence "n - (n - i) = i" by simp
huffman@29451: thus "f (n - i) i = f (n - i) (n - (n - i))" by simp
huffman@29451: qed
huffman@29451:
huffman@29451: text {* TODO: move to appropriate theory *}
huffman@29451: lemma setsum_atMost_Suc_shift:
huffman@29451: fixes f :: "nat \ 'a::comm_monoid_add"
huffman@29451: shows "(\i\Suc n. f i) = f 0 + (\i\n. f (Suc i))"
huffman@29451: proof (induct n)
huffman@29451: case 0 show ?case by simp
huffman@29451: next
huffman@29451: case (Suc n) note IH = this
huffman@29451: have "(\i\Suc (Suc n). f i) = (\i\Suc n. f i) + f (Suc (Suc n))"
huffman@29451: by (rule setsum_atMost_Suc)
huffman@29451: also have "(\i\Suc n. f i) = f 0 + (\i\n. f (Suc i))"
huffman@29451: by (rule IH)
huffman@29451: also have "f 0 + (\i\n. f (Suc i)) + f (Suc (Suc n)) =
huffman@29451: f 0 + ((\i\n. f (Suc i)) + f (Suc (Suc n)))"
huffman@29451: by (rule add_assoc)
huffman@29451: also have "(\i\n. f (Suc i)) + f (Suc (Suc n)) = (\i\Suc n. f (Suc i))"
huffman@29451: by (rule setsum_atMost_Suc [symmetric])
huffman@29451: finally show ?case .
huffman@29451: qed
huffman@29451:
huffman@29451: instantiation poly :: (comm_semiring_0) comm_semiring_0
huffman@29451: begin
huffman@29451:
huffman@29451: definition
huffman@29451: times_poly_def:
huffman@29451: "p * q = Abs_poly (\n. \i\n. coeff p i * coeff q (n-i))"
huffman@29451:
huffman@29451: lemma coeff_mult:
huffman@29451: "coeff (p * q) n = (\i\n. coeff p i * coeff q (n-i))"
huffman@29451: unfolding times_poly_def
huffman@29451: by (simp add: Abs_poly_inverse coeff Poly_mult)
huffman@29451:
huffman@29451: instance proof
huffman@29451: fix p q r :: "'a poly"
huffman@29451: show 0: "0 * p = 0"
huffman@29451: by (simp add: expand_poly_eq coeff_mult)
huffman@29451: show "p * 0 = 0"
huffman@29451: by (simp add: expand_poly_eq coeff_mult)
huffman@29451: show "(p + q) * r = p * r + q * r"
huffman@29451: by (simp add: expand_poly_eq coeff_mult left_distrib setsum_addf)
huffman@29451: show "(p * q) * r = p * (q * r)"
huffman@29451: proof (rule poly_ext)
huffman@29451: fix n :: nat
huffman@29451: have "(\j\n. \i\j. coeff p i * coeff q (j - i) * coeff r (n - j)) =
huffman@29451: (\j\n. \i\n - j. coeff p j * coeff q i * coeff r (n - j - i))"
huffman@29451: by (rule poly_mult_assoc_lemma)
huffman@29451: thus "coeff ((p * q) * r) n = coeff (p * (q * r)) n"
huffman@29451: by (simp add: coeff_mult setsum_right_distrib
huffman@29451: setsum_left_distrib mult_assoc)
huffman@29451: qed
huffman@29451: show "p * q = q * p"
huffman@29451: proof (rule poly_ext)
huffman@29451: fix n :: nat
huffman@29451: have "(\i\n. coeff p i * coeff q (n - i)) =
huffman@29451: (\i\n. coeff p (n - i) * coeff q i)"
huffman@29451: by (rule poly_mult_commute_lemma)
huffman@29451: thus "coeff (p * q) n = coeff (q * p) n"
huffman@29451: by (simp add: coeff_mult mult_commute)
huffman@29451: qed
huffman@29451: qed
huffman@29451:
huffman@29451: end
huffman@29451:
huffman@29451: lemma degree_mult_le: "degree (p * q) \ degree p + degree q"
huffman@29451: apply (rule degree_le, simp add: coeff_mult)
huffman@29451: apply (rule Poly_mult_lemma)
huffman@29451: apply (simp_all add: coeff_eq_0)
huffman@29451: done
huffman@29451:
huffman@29451: lemma mult_pCons_left [simp]:
huffman@29451: "pCons a p * q = smult a q + pCons 0 (p * q)"
huffman@29451: apply (rule poly_ext)
huffman@29451: apply (case_tac n)
huffman@29451: apply (simp add: coeff_mult)
huffman@29451: apply (simp add: coeff_mult setsum_atMost_Suc_shift
huffman@29451: del: setsum_atMost_Suc)
huffman@29451: done
huffman@29451:
huffman@29451: lemma mult_pCons_right [simp]:
huffman@29451: "p * pCons a q = smult a p + pCons 0 (p * q)"
huffman@29451: using mult_pCons_left [of a q p] by (simp add: mult_commute)
huffman@29451:
huffman@29451: lemma mult_smult_left: "smult a p * q = smult a (p * q)"
huffman@29451: by (induct p, simp, simp add: smult_add_right smult_smult)
huffman@29451:
huffman@29451: lemma mult_smult_right: "p * smult a q = smult a (p * q)"
huffman@29451: using mult_smult_left [of a q p] by (simp add: mult_commute)
huffman@29451:
huffman@29451: lemma mult_monom: "monom a m * monom b n = monom (a * b) (m + n)"
huffman@29451: by (induct m, simp add: monom_0 smult_monom, simp add: monom_Suc)
huffman@29451:
huffman@29451:
huffman@29451: subsection {* The unit polynomial and exponentiation *}
huffman@29451:
huffman@29451: instantiation poly :: (comm_semiring_1) comm_semiring_1
huffman@29451: begin
huffman@29451:
huffman@29451: definition
huffman@29451: one_poly_def:
huffman@29451: "1 = pCons 1 0"
huffman@29451:
huffman@29451: instance proof
huffman@29451: fix p :: "'a poly" show "1 * p = p"
huffman@29451: unfolding one_poly_def
huffman@29451: by simp
huffman@29451: next
huffman@29451: show "0 \ (1::'a poly)"
huffman@29451: unfolding one_poly_def by simp
huffman@29451: qed
huffman@29451:
huffman@29451: end
huffman@29451:
huffman@29451: lemma coeff_1 [simp]: "coeff 1 n = (if n = 0 then 1 else 0)"
huffman@29451: unfolding one_poly_def
huffman@29451: by (simp add: coeff_pCons split: nat.split)
huffman@29451:
huffman@29451: lemma degree_1 [simp]: "degree 1 = 0"
huffman@29451: unfolding one_poly_def
huffman@29451: by (rule degree_pCons_0)
huffman@29451:
huffman@29451: instantiation poly :: (comm_semiring_1) recpower
huffman@29451: begin
huffman@29451:
huffman@29451: primrec power_poly where
huffman@29451: power_poly_0: "(p::'a poly) ^ 0 = 1"
huffman@29451: | power_poly_Suc: "(p::'a poly) ^ (Suc n) = p * p ^ n"
huffman@29451:
huffman@29451: instance
huffman@29451: by default simp_all
huffman@29451:
huffman@29451: end
huffman@29451:
huffman@29451: instance poly :: (comm_ring) comm_ring ..
huffman@29451:
huffman@29451: instance poly :: (comm_ring_1) comm_ring_1 ..
huffman@29451:
huffman@29451: instantiation poly :: (comm_ring_1) number_ring
huffman@29451: begin
huffman@29451:
huffman@29451: definition
huffman@29451: "number_of k = (of_int k :: 'a poly)"
huffman@29451:
huffman@29451: instance
huffman@29451: by default (rule number_of_poly_def)
huffman@29451:
huffman@29451: end
huffman@29451:
huffman@29451:
huffman@29451: subsection {* Polynomials form an integral domain *}
huffman@29451:
huffman@29451: lemma coeff_mult_degree_sum:
huffman@29451: "coeff (p * q) (degree p + degree q) =
huffman@29451: coeff p (degree p) * coeff q (degree q)"
huffman@29451: apply (simp add: coeff_mult)
huffman@29451: apply (subst setsum_diff1' [where a="degree p"])
huffman@29451: apply simp
huffman@29451: apply simp
huffman@29451: apply (subst setsum_0' [rule_format])
huffman@29451: apply clarsimp
huffman@29451: apply (subgoal_tac "degree p < a \ degree q < degree p + degree q - a")
huffman@29451: apply (force simp add: coeff_eq_0)
huffman@29451: apply arith
huffman@29451: apply simp
huffman@29451: done
huffman@29451:
huffman@29451: instance poly :: (idom) idom
huffman@29451: proof
huffman@29451: fix p q :: "'a poly"
huffman@29451: assume "p \ 0" and "q \ 0"
huffman@29451: have "coeff (p * q) (degree p + degree q) =
huffman@29451: coeff p (degree p) * coeff q (degree q)"
huffman@29451: by (rule coeff_mult_degree_sum)
huffman@29451: also have "coeff p (degree p) * coeff q (degree q) \ 0"
huffman@29451: using `p \ 0` and `q \ 0` by simp
huffman@29451: finally have "\n. coeff (p * q) n \ 0" ..
huffman@29451: thus "p * q \ 0" by (simp add: expand_poly_eq)
huffman@29451: qed
huffman@29451:
huffman@29451: lemma degree_mult_eq:
huffman@29451: fixes p q :: "'a::idom poly"
huffman@29451: shows "\p \ 0; q \ 0\ \ degree (p * q) = degree p + degree q"
huffman@29451: apply (rule order_antisym [OF degree_mult_le le_degree])
huffman@29451: apply (simp add: coeff_mult_degree_sum)
huffman@29451: done
huffman@29451:
huffman@29451: lemma dvd_imp_degree_le:
huffman@29451: fixes p q :: "'a::idom poly"
huffman@29451: shows "\p dvd q; q \ 0\ \ degree p \ degree q"
huffman@29451: by (erule dvdE, simp add: degree_mult_eq)
huffman@29451:
huffman@29451:
huffman@29451: subsection {* Long division of polynomials *}
huffman@29451:
huffman@29451: definition
huffman@29451: divmod_poly_rel :: "'a::field poly \ 'a poly \ 'a poly \ 'a poly \ bool"
huffman@29451: where
huffman@29451: "divmod_poly_rel x y q r \
huffman@29451: x = q * y + r \ (if y = 0 then q = 0 else r = 0 \ degree r < degree y)"
huffman@29451:
huffman@29451: lemma divmod_poly_rel_0:
huffman@29451: "divmod_poly_rel 0 y 0 0"
huffman@29451: unfolding divmod_poly_rel_def by simp
huffman@29451:
huffman@29451: lemma divmod_poly_rel_by_0:
huffman@29451: "divmod_poly_rel x 0 0 x"
huffman@29451: unfolding divmod_poly_rel_def by simp
huffman@29451:
huffman@29451: lemma eq_zero_or_degree_less:
huffman@29451: assumes "degree p \ n" and "coeff p n = 0"
huffman@29451: shows "p = 0 \ degree p < n"
huffman@29451: proof (cases n)
huffman@29451: case 0
huffman@29451: with `degree p \ n` and `coeff p n = 0`
huffman@29451: have "coeff p (degree p) = 0" by simp
huffman@29451: then have "p = 0" by simp
huffman@29451: then show ?thesis ..
huffman@29451: next
huffman@29451: case (Suc m)
huffman@29451: have "\i>n. coeff p i = 0"
huffman@29451: using `degree p \ n` by (simp add: coeff_eq_0)
huffman@29451: then have "\i\n. coeff p i = 0"
huffman@29451: using `coeff p n = 0` by (simp add: le_less)
huffman@29451: then have "\i>m. coeff p i = 0"
huffman@29451: using `n = Suc m` by (simp add: less_eq_Suc_le)
huffman@29451: then have "degree p \ m"
huffman@29451: by (rule degree_le)
huffman@29451: then have "degree p < n"
huffman@29451: using `n = Suc m` by (simp add: less_Suc_eq_le)
huffman@29451: then show ?thesis ..
huffman@29451: qed
huffman@29451:
huffman@29451: lemma divmod_poly_rel_pCons:
huffman@29451: assumes rel: "divmod_poly_rel x y q r"
huffman@29451: assumes y: "y \ 0"
huffman@29451: assumes b: "b = coeff (pCons a r) (degree y) / coeff y (degree y)"
huffman@29451: shows "divmod_poly_rel (pCons a x) y (pCons b q) (pCons a r - smult b y)"
huffman@29451: (is "divmod_poly_rel ?x y ?q ?r")
huffman@29451: proof -
huffman@29451: have x: "x = q * y + r" and r: "r = 0 \ degree r < degree y"
huffman@29451: using assms unfolding divmod_poly_rel_def by simp_all
huffman@29451:
huffman@29451: have 1: "?x = ?q * y + ?r"
huffman@29451: using b x by simp
huffman@29451:
huffman@29451: have 2: "?r = 0 \ degree ?r < degree y"
huffman@29451: proof (rule eq_zero_or_degree_less)
huffman@29451: have "degree ?r \ max (degree (pCons a r)) (degree (smult b y))"
huffman@29451: by (rule degree_diff_le)
huffman@29451: also have "\ \ degree y"
huffman@29451: proof (rule min_max.le_supI)
huffman@29451: show "degree (pCons a r) \ degree y"
huffman@29451: using r by (auto simp add: degree_pCons_eq_if)
huffman@29451: show "degree (smult b y) \ degree y"
huffman@29451: by (rule degree_smult_le)
huffman@29451: qed
huffman@29451: finally show "degree ?r \ degree y" .
huffman@29451: next
huffman@29451: show "coeff ?r (degree y) = 0"
huffman@29451: using `y \ 0` unfolding b by simp
huffman@29451: qed
huffman@29451:
huffman@29451: from 1 2 show ?thesis
huffman@29451: unfolding divmod_poly_rel_def
huffman@29451: using `y \ 0` by simp
huffman@29451: qed
huffman@29451:
huffman@29451: lemma divmod_poly_rel_exists: "\q r. divmod_poly_rel x y q r"
huffman@29451: apply (cases "y = 0")
huffman@29451: apply (fast intro!: divmod_poly_rel_by_0)
huffman@29451: apply (induct x)
huffman@29451: apply (fast intro!: divmod_poly_rel_0)
huffman@29451: apply (fast intro!: divmod_poly_rel_pCons)
huffman@29451: done
huffman@29451:
huffman@29451: lemma divmod_poly_rel_unique:
huffman@29451: assumes 1: "divmod_poly_rel x y q1 r1"
huffman@29451: assumes 2: "divmod_poly_rel x y q2 r2"
huffman@29451: shows "q1 = q2 \ r1 = r2"
huffman@29451: proof (cases "y = 0")
huffman@29451: assume "y = 0" with assms show ?thesis
huffman@29451: by (simp add: divmod_poly_rel_def)
huffman@29451: next
huffman@29451: assume [simp]: "y \ 0"
huffman@29451: from 1 have q1: "x = q1 * y + r1" and r1: "r1 = 0 \ degree r1 < degree y"
huffman@29451: unfolding divmod_poly_rel_def by simp_all
huffman@29451: from 2 have q2: "x = q2 * y + r2" and r2: "r2 = 0 \ degree r2 < degree y"
huffman@29451: unfolding divmod_poly_rel_def by simp_all
huffman@29451: from q1 q2 have q3: "(q1 - q2) * y = r2 - r1"
huffman@29451: by (simp add: ring_simps)
huffman@29451: from r1 r2 have r3: "(r2 - r1) = 0 \ degree (r2 - r1) < degree y"
huffman@29451: by (auto intro: le_less_trans [OF degree_diff_le])
huffman@29451:
huffman@29451: show "q1 = q2 \ r1 = r2"
huffman@29451: proof (rule ccontr)
huffman@29451: assume "\ (q1 = q2 \ r1 = r2)"
huffman@29451: with q3 have "q1 \ q2" and "r1 \ r2" by auto
huffman@29451: with r3 have "degree (r2 - r1) < degree y" by simp
huffman@29451: also have "degree y \ degree (q1 - q2) + degree y" by simp
huffman@29451: also have "\ = degree ((q1 - q2) * y)"
huffman@29451: using `q1 \