| author | paulson <lp15@cam.ac.uk> | 
| Fri, 13 Sep 2019 12:46:36 +0100 | |
| changeset 70694 | ae37b8fbf023 | 
| parent 70113 | c8deb8ba6d05 | 
| child 71398 | e0237f2eb49d | 
| permissions | -rw-r--r-- | 
| 65435 | 1 | (* Title: HOL/Computational_Algebra/Polynomial.thy | 
| 29451 | 2 | Author: Brian Huffman | 
| 41959 | 3 | Author: Clemens Ballarin | 
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 4 | Author: Amine Chaieb | 
| 52380 | 5 | Author: Florian Haftmann | 
| 29451 | 6 | *) | 
| 7 | ||
| 60500 | 8 | section \<open>Polynomials as type over a ring structure\<close> | 
| 29451 | 9 | |
| 10 | theory Polynomial | |
| 65417 | 11 | imports | 
| 68534 
914e1bc7369a
Now based on Complex_Main, not HOL.Deriv
 paulson <lp15@cam.ac.uk> parents: 
68532diff
changeset | 12 | Complex_Main | 
| 66453 
cc19f7ca2ed6
session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
 wenzelm parents: 
65811diff
changeset | 13 | "HOL-Library.More_List" | 
| 
cc19f7ca2ed6
session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
 wenzelm parents: 
65811diff
changeset | 14 | "HOL-Library.Infinite_Set" | 
| 66805 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 15 | Factorial_Ring | 
| 29451 | 16 | begin | 
| 17 | ||
| 60500 | 18 | subsection \<open>Auxiliary: operations for lists (later) representing coefficients\<close> | 
| 52380 | 19 | |
| 20 | definition cCons :: "'a::zero \<Rightarrow> 'a list \<Rightarrow> 'a list" (infixr "##" 65) | |
| 65346 | 21 | where "x ## xs = (if xs = [] \<and> x = 0 then [] else x # xs)" | 
| 22 | ||
| 23 | lemma cCons_0_Nil_eq [simp]: "0 ## [] = []" | |
| 52380 | 24 | by (simp add: cCons_def) | 
| 25 | ||
| 65346 | 26 | lemma cCons_Cons_eq [simp]: "x ## y # ys = x # y # ys" | 
| 52380 | 27 | by (simp add: cCons_def) | 
| 28 | ||
| 65346 | 29 | lemma cCons_append_Cons_eq [simp]: "x ## xs @ y # ys = x # xs @ y # ys" | 
| 52380 | 30 | by (simp add: cCons_def) | 
| 31 | ||
| 65346 | 32 | lemma cCons_not_0_eq [simp]: "x \<noteq> 0 \<Longrightarrow> x ## xs = x # xs" | 
| 52380 | 33 | by (simp add: cCons_def) | 
| 34 | ||
| 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" | |
| 37 | proof (cases "x = 0") | |
| 65346 | 38 | case False | 
| 39 | then show ?thesis by simp | |
| 52380 | 40 | next | 
| 65346 | 41 | case True | 
| 42 | show ?thesis | |
| 52380 | 43 | proof (induct xs rule: rev_induct) | 
| 65346 | 44 | case Nil | 
| 45 | with True show ?case by simp | |
| 52380 | 46 | next | 
| 65346 | 47 | case (snoc y ys) | 
| 48 | then show ?case | |
| 52380 | 49 | by (cases "y = 0") (simp_all add: append_Cons [symmetric] del: append_Cons) | 
| 50 | qed | |
| 51 | qed | |
| 52 | ||
| 65346 | 53 | lemma tl_cCons [simp]: "tl (x ## xs) = xs" | 
| 52380 | 54 | by (simp add: cCons_def) | 
| 55 | ||
| 65346 | 56 | |
| 61585 | 57 | subsection \<open>Definition of type \<open>poly\<close>\<close> | 
| 29451 | 58 | |
| 61260 | 59 | typedef (overloaded) 'a poly = "{f :: nat \<Rightarrow> 'a::zero. \<forall>\<^sub>\<infinity> n. f n = 0}"
 | 
| 63433 | 60 | morphisms coeff Abs_poly | 
| 61 | by (auto intro!: ALL_MOST) | |
| 29451 | 62 | |
| 59487 
adaa430fc0f7
default abstypes and default abstract equations make technical (no_code) annotation superfluous
 haftmann parents: 
58881diff
changeset | 63 | setup_lifting type_definition_poly | 
| 52380 | 64 | |
| 65 | lemma poly_eq_iff: "p = q \<longleftrightarrow> (\<forall>n. coeff p n = coeff q n)" | |
| 45694 
4a8743618257
prefer typedef without extra definition and alternative name;
 wenzelm parents: 
45605diff
changeset | 66 | by (simp add: coeff_inject [symmetric] fun_eq_iff) | 
| 29451 | 67 | |
| 52380 | 68 | lemma poly_eqI: "(\<And>n. coeff p n = coeff q n) \<Longrightarrow> p = q" | 
| 69 | by (simp add: poly_eq_iff) | |
| 70 | ||
| 59983 
cd2efd7d06bd
replace almost_everywhere_zero by Infinite_Set.MOST
 hoelzl parents: 
59815diff
changeset | 71 | lemma MOST_coeff_eq_0: "\<forall>\<^sub>\<infinity> n. coeff p n = 0" | 
| 52380 | 72 | using coeff [of p] by simp | 
| 29451 | 73 | |
| 74 | ||
| 60500 | 75 | subsection \<open>Degree of a polynomial\<close> | 
| 29451 | 76 | |
| 52380 | 77 | definition degree :: "'a::zero poly \<Rightarrow> nat" | 
| 65346 | 78 | where "degree p = (LEAST n. \<forall>i>n. coeff p i = 0)" | 
| 29451 | 79 | |
| 52380 | 80 | lemma coeff_eq_0: | 
| 81 | assumes "degree p < n" | |
| 82 | shows "coeff p n = 0" | |
| 29451 | 83 | proof - | 
| 59983 
cd2efd7d06bd
replace almost_everywhere_zero by Infinite_Set.MOST
 hoelzl parents: 
59815diff
changeset | 84 | have "\<exists>n. \<forall>i>n. coeff p i = 0" | 
| 
cd2efd7d06bd
replace almost_everywhere_zero by Infinite_Set.MOST
 hoelzl parents: 
59815diff
changeset | 85 | using MOST_coeff_eq_0 by (simp add: MOST_nat) | 
| 52380 | 86 | then have "\<forall>i>degree p. coeff p i = 0" | 
| 29451 | 87 | unfolding degree_def by (rule LeastI_ex) | 
| 52380 | 88 | with assms show ?thesis by simp | 
| 29451 | 89 | qed | 
| 90 | ||
| 91 | lemma le_degree: "coeff p n \<noteq> 0 \<Longrightarrow> n \<le> degree p" | |
| 92 | by (erule contrapos_np, rule coeff_eq_0, simp) | |
| 93 | ||
| 94 | lemma degree_le: "\<forall>i>n. coeff p i = 0 \<Longrightarrow> degree p \<le> n" | |
| 95 | unfolding degree_def by (erule Least_le) | |
| 96 | ||
| 97 | lemma less_degree_imp: "n < degree p \<Longrightarrow> \<exists>i>n. coeff p i \<noteq> 0" | |
| 98 | unfolding degree_def by (drule not_less_Least, simp) | |
| 99 | ||
| 100 | ||
| 60500 | 101 | subsection \<open>The zero polynomial\<close> | 
| 29451 | 102 | |
| 103 | instantiation poly :: (zero) zero | |
| 104 | begin | |
| 105 | ||
| 52380 | 106 | lift_definition zero_poly :: "'a poly" | 
| 65390 | 107 | is "\<lambda>_. 0" | 
| 108 | by (rule MOST_I) simp | |
| 29451 | 109 | |
| 110 | instance .. | |
| 52380 | 111 | |
| 29451 | 112 | end | 
| 113 | ||
| 65346 | 114 | lemma coeff_0 [simp]: "coeff 0 n = 0" | 
| 52380 | 115 | by transfer rule | 
| 29451 | 116 | |
| 65346 | 117 | lemma degree_0 [simp]: "degree 0 = 0" | 
| 29451 | 118 | by (rule order_antisym [OF degree_le le0]) simp | 
| 119 | ||
| 120 | lemma leading_coeff_neq_0: | |
| 52380 | 121 | assumes "p \<noteq> 0" | 
| 122 | shows "coeff p (degree p) \<noteq> 0" | |
| 29451 | 123 | proof (cases "degree p") | 
| 124 | case 0 | |
| 65346 | 125 | from \<open>p \<noteq> 0\<close> obtain n where "coeff p n \<noteq> 0" | 
| 126 | by (auto simp add: poly_eq_iff) | |
| 127 | then have "n \<le> degree p" | |
| 128 | by (rule le_degree) | |
| 129 | with \<open>coeff p n \<noteq> 0\<close> and \<open>degree p = 0\<close> show "coeff p (degree p) \<noteq> 0" | |
| 130 | by simp | |
| 29451 | 131 | next | 
| 132 | case (Suc n) | |
| 65346 | 133 | from \<open>degree p = Suc n\<close> have "n < degree p" | 
| 134 | by simp | |
| 135 | then have "\<exists>i>n. coeff p i \<noteq> 0" | |
| 136 | by (rule less_degree_imp) | |
| 137 | then obtain i where "n < i" and "coeff p i \<noteq> 0" | |
| 138 | by blast | |
| 139 | from \<open>degree p = Suc n\<close> and \<open>n < i\<close> have "degree p \<le> i" | |
| 140 | by simp | |
| 141 | also from \<open>coeff p i \<noteq> 0\<close> have "i \<le> degree p" | |
| 142 | by (rule le_degree) | |
| 29451 | 143 | finally have "degree p = i" . | 
| 60500 | 144 | with \<open>coeff p i \<noteq> 0\<close> show "coeff p (degree p) \<noteq> 0" by simp | 
| 29451 | 145 | qed | 
| 146 | ||
| 65346 | 147 | lemma leading_coeff_0_iff [simp]: "coeff p (degree p) = 0 \<longleftrightarrow> p = 0" | 
| 148 | by (cases "p = 0") (simp_all add: leading_coeff_neq_0) | |
| 29451 | 149 | |
| 64795 | 150 | lemma eq_zero_or_degree_less: | 
| 151 | assumes "degree p \<le> n" and "coeff p n = 0" | |
| 152 | shows "p = 0 \<or> degree p < n" | |
| 153 | proof (cases n) | |
| 154 | case 0 | |
| 65346 | 155 | with \<open>degree p \<le> n\<close> and \<open>coeff p n = 0\<close> have "coeff p (degree p) = 0" | 
| 156 | by simp | |
| 64795 | 157 | then have "p = 0" by simp | 
| 158 | then show ?thesis .. | |
| 159 | next | |
| 160 | case (Suc m) | |
| 65346 | 161 | from \<open>degree p \<le> n\<close> have "\<forall>i>n. coeff p i = 0" | 
| 162 | by (simp add: coeff_eq_0) | |
| 163 | with \<open>coeff p n = 0\<close> have "\<forall>i\<ge>n. coeff p i = 0" | |
| 164 | by (simp add: le_less) | |
| 165 | with \<open>n = Suc m\<close> have "\<forall>i>m. coeff p i = 0" | |
| 166 | by (simp add: less_eq_Suc_le) | |
| 64795 | 167 | then have "degree p \<le> m" | 
| 168 | by (rule degree_le) | |
| 65346 | 169 | with \<open>n = Suc m\<close> have "degree p < n" | 
| 170 | by (simp add: less_Suc_eq_le) | |
| 64795 | 171 | then show ?thesis .. | 
| 172 | qed | |
| 173 | ||
| 174 | lemma coeff_0_degree_minus_1: "coeff rrr dr = 0 \<Longrightarrow> degree rrr \<le> dr \<Longrightarrow> degree rrr \<le> dr - 1" | |
| 175 | using eq_zero_or_degree_less by fastforce | |
| 176 | ||
| 29451 | 177 | |
| 60500 | 178 | subsection \<open>List-style constructor for polynomials\<close> | 
| 29451 | 179 | |
| 52380 | 180 | lift_definition pCons :: "'a::zero \<Rightarrow> 'a poly \<Rightarrow> 'a poly" | 
| 55415 | 181 | is "\<lambda>a p. case_nat a (coeff p)" | 
| 59983 
cd2efd7d06bd
replace almost_everywhere_zero by Infinite_Set.MOST
 hoelzl parents: 
59815diff
changeset | 182 | by (rule MOST_SucD) (simp add: MOST_coeff_eq_0) | 
| 29451 | 183 | |
| 52380 | 184 | lemmas coeff_pCons = pCons.rep_eq | 
| 29455 | 185 | |
| 65346 | 186 | lemma coeff_pCons_0 [simp]: "coeff (pCons a p) 0 = a" | 
| 52380 | 187 | by transfer simp | 
| 29455 | 188 | |
| 65346 | 189 | lemma coeff_pCons_Suc [simp]: "coeff (pCons a p) (Suc n) = coeff p n" | 
| 29451 | 190 | by (simp add: coeff_pCons) | 
| 191 | ||
| 65346 | 192 | lemma degree_pCons_le: "degree (pCons a p) \<le> Suc (degree p)" | 
| 52380 | 193 | by (rule degree_le) (simp add: coeff_eq_0 coeff_pCons split: nat.split) | 
| 29451 | 194 | |
| 65346 | 195 | lemma degree_pCons_eq: "p \<noteq> 0 \<Longrightarrow> degree (pCons a p) = Suc (degree p)" | 
| 52380 | 196 | apply (rule order_antisym [OF degree_pCons_le]) | 
| 197 | apply (rule le_degree, simp) | |
| 198 | done | |
| 29451 | 199 | |
| 65346 | 200 | lemma degree_pCons_0: "degree (pCons a 0) = 0" | 
| 52380 | 201 | apply (rule order_antisym [OF _ le0]) | 
| 202 | apply (rule degree_le, simp add: coeff_pCons split: nat.split) | |
| 203 | done | |
| 29451 | 204 | |
| 65346 | 205 | lemma degree_pCons_eq_if [simp]: "degree (pCons a p) = (if p = 0 then 0 else Suc (degree p))" | 
| 52380 | 206 | apply (cases "p = 0", simp_all) | 
| 207 | apply (rule order_antisym [OF _ le0]) | |
| 208 | apply (rule degree_le, simp add: coeff_pCons split: nat.split) | |
| 209 | apply (rule order_antisym [OF degree_pCons_le]) | |
| 210 | apply (rule le_degree, simp) | |
| 211 | done | |
| 29451 | 212 | |
| 65346 | 213 | lemma pCons_0_0 [simp]: "pCons 0 0 = 0" | 
| 52380 | 214 | by (rule poly_eqI) (simp add: coeff_pCons split: nat.split) | 
| 29451 | 215 | |
| 65346 | 216 | lemma pCons_eq_iff [simp]: "pCons a p = pCons b q \<longleftrightarrow> a = b \<and> p = q" | 
| 52380 | 217 | proof safe | 
| 29451 | 218 | assume "pCons a p = pCons b q" | 
| 65346 | 219 | then have "coeff (pCons a p) 0 = coeff (pCons b q) 0" | 
| 220 | by simp | |
| 221 | then show "a = b" | |
| 222 | by simp | |
| 29451 | 223 | next | 
| 224 | assume "pCons a p = pCons b q" | |
| 65346 | 225 | then have "coeff (pCons a p) (Suc n) = coeff (pCons b q) (Suc n)" for n | 
| 226 | by simp | |
| 227 | then show "p = q" | |
| 228 | by (simp add: poly_eq_iff) | |
| 29451 | 229 | qed | 
| 230 | ||
| 65346 | 231 | lemma pCons_eq_0_iff [simp]: "pCons a p = 0 \<longleftrightarrow> a = 0 \<and> p = 0" | 
| 29451 | 232 | using pCons_eq_iff [of a p 0 0] by simp | 
| 233 | ||
| 234 | lemma pCons_cases [cases type: poly]: | |
| 235 | obtains (pCons) a q where "p = pCons a q" | |
| 236 | proof | |
| 237 | show "p = pCons (coeff p 0) (Abs_poly (\<lambda>n. coeff p (Suc n)))" | |
| 52380 | 238 | by transfer | 
| 65346 | 239 | (simp_all add: MOST_inj[where f=Suc and P="\<lambda>n. p n = 0" for p] fun_eq_iff Abs_poly_inverse | 
| 240 | split: nat.split) | |
| 29451 | 241 | qed | 
| 242 | ||
| 243 | lemma pCons_induct [case_names 0 pCons, induct type: poly]: | |
| 244 | assumes zero: "P 0" | |
| 54856 | 245 | assumes pCons: "\<And>a p. a \<noteq> 0 \<or> p \<noteq> 0 \<Longrightarrow> P p \<Longrightarrow> P (pCons a p)" | 
| 29451 | 246 | shows "P p" | 
| 247 | proof (induct p rule: measure_induct_rule [where f=degree]) | |
| 248 | case (less p) | |
| 249 | obtain a q where "p = pCons a q" by (rule pCons_cases) | |
| 250 | have "P q" | |
| 251 | proof (cases "q = 0") | |
| 252 | case True | |
| 253 | then show "P q" by (simp add: zero) | |
| 254 | next | |
| 255 | case False | |
| 256 | then have "degree (pCons a q) = Suc (degree q)" | |
| 257 | by (rule degree_pCons_eq) | |
| 65346 | 258 | with \<open>p = pCons a q\<close> have "degree q < degree p" | 
| 259 | by simp | |
| 29451 | 260 | then show "P q" | 
| 261 | by (rule less.hyps) | |
| 262 | qed | |
| 54856 | 263 | have "P (pCons a q)" | 
| 264 | proof (cases "a \<noteq> 0 \<or> q \<noteq> 0") | |
| 265 | case True | |
| 60500 | 266 | with \<open>P q\<close> show ?thesis by (auto intro: pCons) | 
| 54856 | 267 | next | 
| 268 | case False | |
| 269 | with zero show ?thesis by simp | |
| 270 | qed | |
| 65346 | 271 | with \<open>p = pCons a q\<close> show ?case | 
| 272 | by simp | |
| 29451 | 273 | qed | 
| 274 | ||
| 60570 | 275 | lemma degree_eq_zeroE: | 
| 276 | fixes p :: "'a::zero poly" | |
| 277 | assumes "degree p = 0" | |
| 278 | obtains a where "p = pCons a 0" | |
| 279 | proof - | |
| 65346 | 280 | obtain a q where p: "p = pCons a q" | 
| 281 | by (cases p) | |
| 282 | with assms have "q = 0" | |
| 283 | by (cases "q = 0") simp_all | |
| 284 | with p have "p = pCons a 0" | |
| 285 | by simp | |
| 286 | then show thesis .. | |
| 60570 | 287 | qed | 
| 288 | ||
| 29451 | 289 | |
| 62422 | 290 | subsection \<open>Quickcheck generator for polynomials\<close> | 
| 291 | ||
| 292 | quickcheck_generator poly constructors: "0 :: _ poly", pCons | |
| 293 | ||
| 294 | ||
| 60500 | 295 | subsection \<open>List-style syntax for polynomials\<close> | 
| 52380 | 296 | |
| 65346 | 297 | syntax "_poly" :: "args \<Rightarrow> 'a poly"  ("[:(_):]")
 | 
| 52380 | 298 | translations | 
| 65346 | 299 | "[:x, xs:]" \<rightleftharpoons> "CONST pCons x [:xs:]" | 
| 300 | "[:x:]" \<rightleftharpoons> "CONST pCons x 0" | |
| 301 | "[:x:]" \<leftharpoondown> "CONST pCons x (_constrain 0 t)" | |
| 52380 | 302 | |
| 303 | ||
| 60500 | 304 | subsection \<open>Representation of polynomials by lists of coefficients\<close> | 
| 52380 | 305 | |
| 306 | primrec Poly :: "'a::zero list \<Rightarrow> 'a poly" | |
| 65346 | 307 | where | 
| 308 | [code_post]: "Poly [] = 0" | |
| 309 | | [code_post]: "Poly (a # as) = pCons a (Poly as)" | |
| 310 | ||
| 311 | lemma Poly_replicate_0 [simp]: "Poly (replicate n 0) = 0" | |
| 52380 | 312 | by (induct n) simp_all | 
| 313 | ||
| 65346 | 314 | lemma Poly_eq_0: "Poly as = 0 \<longleftrightarrow> (\<exists>n. as = replicate n 0)" | 
| 52380 | 315 | by (induct as) (auto simp add: Cons_replicate_eq) | 
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 316 | |
| 65346 | 317 | lemma Poly_append_replicate_zero [simp]: "Poly (as @ replicate n 0) = Poly as" | 
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 318 | by (induct as) simp_all | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 319 | |
| 65346 | 320 | lemma Poly_snoc_zero [simp]: "Poly (as @ [0]) = Poly as" | 
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 321 | using Poly_append_replicate_zero [of as 1] by simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 322 | |
| 65346 | 323 | lemma Poly_cCons_eq_pCons_Poly [simp]: "Poly (a ## p) = pCons a (Poly p)" | 
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 324 | by (simp add: cCons_def) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 325 | |
| 65346 | 326 | lemma Poly_on_rev_starting_with_0 [simp]: "hd as = 0 \<Longrightarrow> Poly (rev (tl as)) = Poly (rev as)" | 
| 327 | by (cases as) simp_all | |
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 328 | |
| 62065 | 329 | lemma degree_Poly: "degree (Poly xs) \<le> length xs" | 
| 65346 | 330 | by (induct xs) simp_all | 
| 331 | ||
| 332 | lemma coeff_Poly_eq [simp]: "coeff (Poly xs) = nth_default 0 xs" | |
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 333 | by (induct xs) (simp_all add: fun_eq_iff coeff_pCons split: nat.splits) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 334 | |
| 52380 | 335 | definition coeffs :: "'a poly \<Rightarrow> 'a::zero list" | 
| 65346 | 336 | where "coeffs p = (if p = 0 then [] else map (\<lambda>i. coeff p i) [0 ..< Suc (degree p)])" | 
| 337 | ||
| 338 | lemma coeffs_eq_Nil [simp]: "coeffs p = [] \<longleftrightarrow> p = 0" | |
| 52380 | 339 | by (simp add: coeffs_def) | 
| 340 | ||
| 65346 | 341 | lemma not_0_coeffs_not_Nil: "p \<noteq> 0 \<Longrightarrow> coeffs p \<noteq> []" | 
| 52380 | 342 | by simp | 
| 343 | ||
| 65346 | 344 | lemma coeffs_0_eq_Nil [simp]: "coeffs 0 = []" | 
| 52380 | 345 | by simp | 
| 29454 
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
 huffman parents: 
29453diff
changeset | 346 | |
| 65346 | 347 | lemma coeffs_pCons_eq_cCons [simp]: "coeffs (pCons a p) = a ## coeffs p" | 
| 52380 | 348 | proof - | 
| 65346 | 349 | have *: "\<forall>m\<in>set ms. m > 0 \<Longrightarrow> map (case_nat x f) ms = map f (map (\<lambda>n. n - 1) ms)" | 
| 350 | for ms :: "nat list" and f :: "nat \<Rightarrow> 'a" and x :: "'a" | |
| 351 | by (induct ms) (auto split: nat.split) | |
| 52380 | 352 | show ?thesis | 
| 65346 | 353 | by (simp add: * coeffs_def upt_conv_Cons coeff_pCons map_decr_upt del: upt_Suc) | 
| 52380 | 354 | qed | 
| 355 | ||
| 62065 | 356 | lemma length_coeffs: "p \<noteq> 0 \<Longrightarrow> length (coeffs p) = degree p + 1" | 
| 357 | by (simp add: coeffs_def) | |
| 64860 | 358 | |
| 65346 | 359 | lemma coeffs_nth: "p \<noteq> 0 \<Longrightarrow> n \<le> degree p \<Longrightarrow> coeffs p ! n = coeff p n" | 
| 360 | by (auto simp: coeffs_def simp del: upt_Suc) | |
| 361 | ||
| 362 | lemma coeff_in_coeffs: "p \<noteq> 0 \<Longrightarrow> n \<le> degree p \<Longrightarrow> coeff p n \<in> set (coeffs p)" | |
| 363 | using coeffs_nth [of p n, symmetric] by (simp add: length_coeffs) | |
| 364 | ||
| 365 | lemma not_0_cCons_eq [simp]: "p \<noteq> 0 \<Longrightarrow> a ## coeffs p = a # coeffs p" | |
| 52380 | 366 | by (simp add: cCons_def) | 
| 367 | ||
| 65346 | 368 | lemma Poly_coeffs [simp, code abstype]: "Poly (coeffs p) = p" | 
| 54856 | 369 | by (induct p) auto | 
| 52380 | 370 | |
| 65346 | 371 | lemma coeffs_Poly [simp]: "coeffs (Poly as) = strip_while (HOL.eq 0) as" | 
| 52380 | 372 | proof (induct as) | 
| 65346 | 373 | case Nil | 
| 374 | then show ?case by simp | |
| 52380 | 375 | next | 
| 376 | case (Cons a as) | |
| 65346 | 377 | from replicate_length_same [of as 0] have "(\<forall>n. as \<noteq> replicate n 0) \<longleftrightarrow> (\<exists>a\<in>set as. a \<noteq> 0)" | 
| 378 | by (auto dest: sym [of _ as]) | |
| 52380 | 379 | with Cons show ?case by auto | 
| 380 | qed | |
| 381 | ||
| 65390 | 382 | lemma no_trailing_coeffs [simp]: | 
| 383 | "no_trailing (HOL.eq 0) (coeffs p)" | |
| 384 | by (induct p) auto | |
| 385 | ||
| 386 | lemma strip_while_coeffs [simp]: | |
| 387 | "strip_while (HOL.eq 0) (coeffs p) = coeffs p" | |
| 388 | by simp | |
| 52380 | 389 | |
| 65346 | 390 | lemma coeffs_eq_iff: "p = q \<longleftrightarrow> coeffs p = coeffs q" | 
| 391 | (is "?P \<longleftrightarrow> ?Q") | |
| 52380 | 392 | proof | 
| 65346 | 393 | assume ?P | 
| 394 | then show ?Q by simp | |
| 52380 | 395 | next | 
| 396 | assume ?Q | |
| 397 | then have "Poly (coeffs p) = Poly (coeffs q)" by simp | |
| 398 | then show ?P by simp | |
| 399 | qed | |
| 400 | ||
| 65346 | 401 | lemma nth_default_coeffs_eq: "nth_default 0 (coeffs p) = coeff p" | 
| 52380 | 402 | by (simp add: fun_eq_iff coeff_Poly_eq [symmetric]) | 
| 403 | ||
| 65346 | 404 | lemma [code]: "coeff p = nth_default 0 (coeffs p)" | 
| 52380 | 405 | by (simp add: nth_default_coeffs_eq) | 
| 406 | ||
| 407 | lemma coeffs_eqI: | |
| 408 | assumes coeff: "\<And>n. coeff p n = nth_default 0 xs n" | |
| 65390 | 409 | assumes zero: "no_trailing (HOL.eq 0) xs" | 
| 52380 | 410 | shows "coeffs p = xs" | 
| 411 | proof - | |
| 65390 | 412 | from coeff have "p = Poly xs" | 
| 413 | by (simp add: poly_eq_iff) | |
| 414 | with zero show ?thesis by simp | |
| 52380 | 415 | qed | 
| 416 | ||
| 65346 | 417 | lemma degree_eq_length_coeffs [code]: "degree p = length (coeffs p) - 1" | 
| 52380 | 418 | by (simp add: coeffs_def) | 
| 419 | ||
| 65346 | 420 | lemma length_coeffs_degree: "p \<noteq> 0 \<Longrightarrow> length (coeffs p) = Suc (degree p)" | 
| 421 | by (induct p) (auto simp: cCons_def) | |
| 422 | ||
| 423 | lemma [code abstract]: "coeffs 0 = []" | |
| 52380 | 424 | by (fact coeffs_0_eq_Nil) | 
| 425 | ||
| 65346 | 426 | lemma [code abstract]: "coeffs (pCons a p) = a ## coeffs p" | 
| 52380 | 427 | by (fact coeffs_pCons_eq_cCons) | 
| 428 | ||
| 65811 | 429 | lemma set_coeffs_subset_singleton_0_iff [simp]: | 
| 430 |   "set (coeffs p) \<subseteq> {0} \<longleftrightarrow> p = 0"
 | |
| 431 | by (auto simp add: coeffs_def intro: classical) | |
| 432 | ||
| 433 | lemma set_coeffs_not_only_0 [simp]: | |
| 434 |   "set (coeffs p) \<noteq> {0}"
 | |
| 435 | by (auto simp add: set_eq_subset) | |
| 436 | ||
| 437 | lemma forall_coeffs_conv: | |
| 438 | "(\<forall>n. P (coeff p n)) \<longleftrightarrow> (\<forall>c \<in> set (coeffs p). P c)" if "P 0" | |
| 439 | using that by (auto simp add: coeffs_def) | |
| 440 | (metis atLeastLessThan_iff coeff_eq_0 not_less_iff_gr_or_eq zero_le) | |
| 441 | ||
| 52380 | 442 | instantiation poly :: ("{zero, equal}") equal
 | 
| 443 | begin | |
| 444 | ||
| 65346 | 445 | definition [code]: "HOL.equal (p::'a poly) q \<longleftrightarrow> HOL.equal (coeffs p) (coeffs q)" | 
| 52380 | 446 | |
| 60679 | 447 | instance | 
| 448 | by standard (simp add: equal equal_poly_def coeffs_eq_iff) | |
| 52380 | 449 | |
| 450 | end | |
| 451 | ||
| 60679 | 452 | lemma [code nbe]: "HOL.equal (p :: _ poly) p \<longleftrightarrow> True" | 
| 52380 | 453 | by (fact equal_refl) | 
| 29454 
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
 huffman parents: 
29453diff
changeset | 454 | |
| 52380 | 455 | definition is_zero :: "'a::zero poly \<Rightarrow> bool" | 
| 65346 | 456 | where [code]: "is_zero p \<longleftrightarrow> List.null (coeffs p)" | 
| 457 | ||
| 458 | lemma is_zero_null [code_abbrev]: "is_zero p \<longleftrightarrow> p = 0" | |
| 52380 | 459 | by (simp add: is_zero_def null_def) | 
| 460 | ||
| 65346 | 461 | |
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 462 | subsubsection \<open>Reconstructing the polynomial from the list\<close> | 
| 63145 | 463 | \<comment> \<open>contributed by Sebastiaan J.C. Joosten and René Thiemann\<close> | 
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 464 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 465 | definition poly_of_list :: "'a::comm_monoid_add list \<Rightarrow> 'a poly" | 
| 65346 | 466 | where [simp]: "poly_of_list = Poly" | 
| 467 | ||
| 468 | lemma poly_of_list_impl [code abstract]: "coeffs (poly_of_list as) = strip_while (HOL.eq 0) as" | |
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 469 | by simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 470 | |
| 52380 | 471 | |
| 60500 | 472 | subsection \<open>Fold combinator for polynomials\<close> | 
| 52380 | 473 | |
| 474 | definition fold_coeffs :: "('a::zero \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a poly \<Rightarrow> 'b \<Rightarrow> 'b"
 | |
| 65346 | 475 | where "fold_coeffs f p = foldr f (coeffs p)" | 
| 476 | ||
| 477 | lemma fold_coeffs_0_eq [simp]: "fold_coeffs f 0 = id" | |
| 52380 | 478 | by (simp add: fold_coeffs_def) | 
| 479 | ||
| 65346 | 480 | lemma fold_coeffs_pCons_eq [simp]: "f 0 = id \<Longrightarrow> fold_coeffs f (pCons a p) = f a \<circ> fold_coeffs f p" | 
| 52380 | 481 | by (simp add: fold_coeffs_def cCons_def fun_eq_iff) | 
| 29454 
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
 huffman parents: 
29453diff
changeset | 482 | |
| 65346 | 483 | lemma fold_coeffs_pCons_0_0_eq [simp]: "fold_coeffs f (pCons 0 0) = id" | 
| 52380 | 484 | by (simp add: fold_coeffs_def) | 
| 485 | ||
| 486 | lemma fold_coeffs_pCons_coeff_not_0_eq [simp]: | |
| 487 | "a \<noteq> 0 \<Longrightarrow> fold_coeffs f (pCons a p) = f a \<circ> fold_coeffs f p" | |
| 488 | by (simp add: fold_coeffs_def) | |
| 489 | ||
| 490 | lemma fold_coeffs_pCons_not_0_0_eq [simp]: | |
| 491 | "p \<noteq> 0 \<Longrightarrow> fold_coeffs f (pCons a p) = f a \<circ> fold_coeffs f p" | |
| 492 | by (simp add: fold_coeffs_def) | |
| 493 | ||
| 64795 | 494 | |
| 60500 | 495 | subsection \<open>Canonical morphism on polynomials -- evaluation\<close> | 
| 52380 | 496 | |
| 497 | definition poly :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a" | |
| 65346 | 498 | where "poly p = fold_coeffs (\<lambda>a f x. a + x * f x) p (\<lambda>x. 0)" \<comment> \<open>The Horner Schema\<close> | 
| 499 | ||
| 500 | lemma poly_0 [simp]: "poly 0 x = 0" | |
| 52380 | 501 | by (simp add: poly_def) | 
| 65346 | 502 | |
| 503 | lemma poly_pCons [simp]: "poly (pCons a p) x = a + x * poly p x" | |
| 52380 | 504 | by (cases "p = 0 \<and> a = 0") (auto simp add: poly_def) | 
| 29454 
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
 huffman parents: 
29453diff
changeset | 505 | |
| 65346 | 506 | lemma poly_altdef: "poly p x = (\<Sum>i\<le>degree p. coeff p i * x ^ i)" | 
| 507 |   for x :: "'a::{comm_semiring_0,semiring_1}"
 | |
| 62065 | 508 | proof (induction p rule: pCons_induct) | 
| 65346 | 509 | case 0 | 
| 510 | then show ?case | |
| 511 | by simp | |
| 512 | next | |
| 62065 | 513 | case (pCons a p) | 
| 65346 | 514 | show ?case | 
| 515 | proof (cases "p = 0") | |
| 516 | case True | |
| 517 | then show ?thesis by simp | |
| 518 | next | |
| 519 | case False | |
| 520 | let ?p' = "pCons a p" | |
| 521 | note poly_pCons[of a p x] | |
| 522 | also note pCons.IH | |
| 523 | also have "a + x * (\<Sum>i\<le>degree p. coeff p i * x ^ i) = | |
| 524 | coeff ?p' 0 * x^0 + (\<Sum>i\<le>degree p. coeff ?p' (Suc i) * x^Suc i)" | |
| 525 | by (simp add: field_simps sum_distrib_left coeff_pCons) | |
| 70113 
c8deb8ba6d05
Fixing the main Homology theory; also moving a lot of sum/prod lemmas into their generic context
 paulson <lp15@cam.ac.uk> parents: 
70097diff
changeset | 526 | also note sum.atMost_Suc_shift[symmetric] | 
| 65346 | 527 | also note degree_pCons_eq[OF \<open>p \<noteq> 0\<close>, of a, symmetric] | 
| 528 | finally show ?thesis . | |
| 529 | qed | |
| 530 | qed | |
| 62065 | 531 | |
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 532 | lemma poly_0_coeff_0: "poly p 0 = coeff p 0" | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 533 | by (cases p) (auto simp: poly_altdef) | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 534 | |
| 29454 
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
 huffman parents: 
29453diff
changeset | 535 | |
| 60500 | 536 | subsection \<open>Monomials\<close> | 
| 29451 | 537 | |
| 52380 | 538 | lift_definition monom :: "'a \<Rightarrow> nat \<Rightarrow> 'a::zero poly" | 
| 539 | is "\<lambda>a m n. if m = n then a else 0" | |
| 59983 
cd2efd7d06bd
replace almost_everywhere_zero by Infinite_Set.MOST
 hoelzl parents: 
59815diff
changeset | 540 | by (simp add: MOST_iff_cofinite) | 
| 52380 | 541 | |
| 65346 | 542 | lemma coeff_monom [simp]: "coeff (monom a m) n = (if m = n then a else 0)" | 
| 52380 | 543 | by transfer rule | 
| 29451 | 544 | |
| 65346 | 545 | lemma monom_0: "monom a 0 = pCons a 0" | 
| 52380 | 546 | by (rule poly_eqI) (simp add: coeff_pCons split: nat.split) | 
| 29451 | 547 | |
| 65346 | 548 | lemma monom_Suc: "monom a (Suc n) = pCons 0 (monom a n)" | 
| 52380 | 549 | by (rule poly_eqI) (simp add: coeff_pCons split: nat.split) | 
| 29451 | 550 | |
| 551 | lemma monom_eq_0 [simp]: "monom 0 n = 0" | |
| 52380 | 552 | by (rule poly_eqI) simp | 
| 29451 | 553 | |
| 554 | lemma monom_eq_0_iff [simp]: "monom a n = 0 \<longleftrightarrow> a = 0" | |
| 52380 | 555 | by (simp add: poly_eq_iff) | 
| 29451 | 556 | |
| 557 | lemma monom_eq_iff [simp]: "monom a n = monom b n \<longleftrightarrow> a = b" | |
| 52380 | 558 | by (simp add: poly_eq_iff) | 
| 29451 | 559 | |
| 560 | lemma degree_monom_le: "degree (monom a n) \<le> n" | |
| 561 | by (rule degree_le, simp) | |
| 562 | ||
| 563 | lemma degree_monom_eq: "a \<noteq> 0 \<Longrightarrow> degree (monom a n) = n" | |
| 564 | apply (rule order_antisym [OF degree_monom_le]) | |
| 65346 | 565 | apply (rule le_degree) | 
| 566 | apply simp | |
| 29451 | 567 | done | 
| 568 | ||
| 52380 | 569 | lemma coeffs_monom [code abstract]: | 
| 570 | "coeffs (monom a n) = (if a = 0 then [] else replicate n 0 @ [a])" | |
| 571 | by (induct n) (simp_all add: monom_0 monom_Suc) | |
| 572 | ||
| 65346 | 573 | lemma fold_coeffs_monom [simp]: "a \<noteq> 0 \<Longrightarrow> fold_coeffs f (monom a n) = f 0 ^^ n \<circ> f a" | 
| 52380 | 574 | by (simp add: fold_coeffs_def coeffs_monom fun_eq_iff) | 
| 575 | ||
| 65346 | 576 | lemma poly_monom: "poly (monom a n) x = a * x ^ n" | 
| 577 | for a x :: "'a::comm_semiring_1" | |
| 578 | by (cases "a = 0", simp_all) (induct n, simp_all add: mult.left_commute poly_def) | |
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 579 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 580 | lemma monom_eq_iff': "monom c n = monom d m \<longleftrightarrow> c = d \<and> (c = 0 \<or> n = m)" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 581 | by (auto simp: poly_eq_iff) | 
| 65346 | 582 | |
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 583 | lemma monom_eq_const_iff: "monom c n = [:d:] \<longleftrightarrow> c = d \<and> (c = 0 \<or> n = 0)" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 584 | using monom_eq_iff'[of c n d 0] by (simp add: monom_0) | 
| 64795 | 585 | |
| 586 | ||
| 587 | subsection \<open>Leading coefficient\<close> | |
| 588 | ||
| 589 | abbreviation lead_coeff:: "'a::zero poly \<Rightarrow> 'a" | |
| 590 | where "lead_coeff p \<equiv> coeff p (degree p)" | |
| 591 | ||
| 592 | lemma lead_coeff_pCons[simp]: | |
| 593 | "p \<noteq> 0 \<Longrightarrow> lead_coeff (pCons a p) = lead_coeff p" | |
| 594 | "p = 0 \<Longrightarrow> lead_coeff (pCons a p) = a" | |
| 595 | by auto | |
| 596 | ||
| 597 | lemma lead_coeff_monom [simp]: "lead_coeff (monom c n) = c" | |
| 598 | by (cases "c = 0") (simp_all add: degree_monom_eq) | |
| 599 | ||
| 66799 | 600 | lemma last_coeffs_eq_coeff_degree: | 
| 601 | "last (coeffs p) = lead_coeff p" if "p \<noteq> 0" | |
| 602 | using that by (simp add: coeffs_def) | |
| 603 | ||
| 64795 | 604 | |
| 60500 | 605 | subsection \<open>Addition and subtraction\<close> | 
| 29451 | 606 | |
| 607 | instantiation poly :: (comm_monoid_add) comm_monoid_add | |
| 608 | begin | |
| 609 | ||
| 52380 | 610 | lift_definition plus_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" | 
| 611 | is "\<lambda>p q n. coeff p n + coeff q n" | |
| 60040 
1fa1023b13b9
move MOST and INFM in Infinite_Set to Filter; change them to abbreviations over the cofinite filter
 hoelzl parents: 
59983diff
changeset | 612 | proof - | 
| 60679 | 613 | fix q p :: "'a poly" | 
| 614 | show "\<forall>\<^sub>\<infinity>n. coeff p n + coeff q n = 0" | |
| 60040 
1fa1023b13b9
move MOST and INFM in Infinite_Set to Filter; change them to abbreviations over the cofinite filter
 hoelzl parents: 
59983diff
changeset | 615 | using MOST_coeff_eq_0[of p] MOST_coeff_eq_0[of q] by eventually_elim simp | 
| 52380 | 616 | qed | 
| 29451 | 617 | |
| 60679 | 618 | lemma coeff_add [simp]: "coeff (p + q) n = coeff p n + coeff q n" | 
| 52380 | 619 | by (simp add: plus_poly.rep_eq) | 
| 29451 | 620 | |
| 60679 | 621 | instance | 
| 622 | proof | |
| 29451 | 623 | fix p q r :: "'a poly" | 
| 624 | show "(p + q) + r = p + (q + r)" | |
| 57512 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 haftmann parents: 
57482diff
changeset | 625 | by (simp add: poly_eq_iff add.assoc) | 
| 29451 | 626 | show "p + q = q + p" | 
| 57512 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 haftmann parents: 
57482diff
changeset | 627 | by (simp add: poly_eq_iff add.commute) | 
| 29451 | 628 | show "0 + p = p" | 
| 52380 | 629 | by (simp add: poly_eq_iff) | 
| 29451 | 630 | qed | 
| 631 | ||
| 632 | end | |
| 633 | ||
| 59815 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 634 | instantiation poly :: (cancel_comm_monoid_add) cancel_comm_monoid_add | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 635 | begin | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 636 | |
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 637 | lift_definition minus_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 638 | is "\<lambda>p q n. coeff p n - coeff q n" | 
| 60040 
1fa1023b13b9
move MOST and INFM in Infinite_Set to Filter; change them to abbreviations over the cofinite filter
 hoelzl parents: 
59983diff
changeset | 639 | proof - | 
| 60679 | 640 | fix q p :: "'a poly" | 
| 641 | show "\<forall>\<^sub>\<infinity>n. coeff p n - coeff q n = 0" | |
| 60040 
1fa1023b13b9
move MOST and INFM in Infinite_Set to Filter; change them to abbreviations over the cofinite filter
 hoelzl parents: 
59983diff
changeset | 642 | using MOST_coeff_eq_0[of p] MOST_coeff_eq_0[of q] by eventually_elim simp | 
| 59815 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 643 | qed | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 644 | |
| 60679 | 645 | lemma coeff_diff [simp]: "coeff (p - q) n = coeff p n - coeff q n" | 
| 59815 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 646 | by (simp add: minus_poly.rep_eq) | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 647 | |
| 60679 | 648 | instance | 
| 649 | proof | |
| 29540 | 650 | fix p q r :: "'a poly" | 
| 59815 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 651 | show "p + q - p = q" | 
| 52380 | 652 | by (simp add: poly_eq_iff) | 
| 59815 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 653 | show "p - q - r = p - (q + r)" | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 654 | by (simp add: poly_eq_iff diff_diff_eq) | 
| 29540 | 655 | qed | 
| 656 | ||
| 59815 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 657 | end | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 658 | |
| 29451 | 659 | instantiation poly :: (ab_group_add) ab_group_add | 
| 660 | begin | |
| 661 | ||
| 52380 | 662 | lift_definition uminus_poly :: "'a poly \<Rightarrow> 'a poly" | 
| 663 | is "\<lambda>p n. - coeff p n" | |
| 60040 
1fa1023b13b9
move MOST and INFM in Infinite_Set to Filter; change them to abbreviations over the cofinite filter
 hoelzl parents: 
59983diff
changeset | 664 | proof - | 
| 60679 | 665 | fix p :: "'a poly" | 
| 666 | show "\<forall>\<^sub>\<infinity>n. - coeff p n = 0" | |
| 60040 
1fa1023b13b9
move MOST and INFM in Infinite_Set to Filter; change them to abbreviations over the cofinite filter
 hoelzl parents: 
59983diff
changeset | 667 | using MOST_coeff_eq_0 by simp | 
| 52380 | 668 | qed | 
| 29451 | 669 | |
| 670 | lemma coeff_minus [simp]: "coeff (- p) n = - coeff p n" | |
| 52380 | 671 | by (simp add: uminus_poly.rep_eq) | 
| 29451 | 672 | |
| 60679 | 673 | instance | 
| 674 | proof | |
| 29451 | 675 | fix p q :: "'a poly" | 
| 676 | show "- p + p = 0" | |
| 52380 | 677 | by (simp add: poly_eq_iff) | 
| 29451 | 678 | show "p - q = p + - q" | 
| 54230 
b1d955791529
more simplification rules on unary and binary minus
 haftmann parents: 
52380diff
changeset | 679 | by (simp add: poly_eq_iff) | 
| 29451 | 680 | qed | 
| 681 | ||
| 682 | end | |
| 683 | ||
| 65346 | 684 | lemma add_pCons [simp]: "pCons a p + pCons b q = pCons (a + b) (p + q)" | 
| 685 | by (rule poly_eqI) (simp add: coeff_pCons split: nat.split) | |
| 686 | ||
| 687 | lemma minus_pCons [simp]: "- pCons a p = pCons (- a) (- p)" | |
| 688 | by (rule poly_eqI) (simp add: coeff_pCons split: nat.split) | |
| 689 | ||
| 690 | lemma diff_pCons [simp]: "pCons a p - pCons b q = pCons (a - b) (p - q)" | |
| 691 | by (rule poly_eqI) (simp add: coeff_pCons split: nat.split) | |
| 29451 | 692 | |
| 29539 | 693 | lemma degree_add_le_max: "degree (p + q) \<le> max (degree p) (degree q)" | 
| 65346 | 694 | by (rule degree_le) (auto simp add: coeff_eq_0) | 
| 695 | ||
| 696 | lemma degree_add_le: "degree p \<le> n \<Longrightarrow> degree q \<le> n \<Longrightarrow> degree (p + q) \<le> n" | |
| 29539 | 697 | by (auto intro: order_trans degree_add_le_max) | 
| 698 | ||
| 65346 | 699 | lemma degree_add_less: "degree p < n \<Longrightarrow> degree q < n \<Longrightarrow> degree (p + q) < n" | 
| 29539 | 700 | by (auto intro: le_less_trans degree_add_le_max) | 
| 29453 | 701 | |
| 65346 | 702 | lemma degree_add_eq_right: "degree p < degree q \<Longrightarrow> degree (p + q) = degree q" | 
| 703 | apply (cases "q = 0") | |
| 704 | apply simp | |
| 29451 | 705 | apply (rule order_antisym) | 
| 65346 | 706 | apply (simp add: degree_add_le) | 
| 29451 | 707 | apply (rule le_degree) | 
| 708 | apply (simp add: coeff_eq_0) | |
| 709 | done | |
| 710 | ||
| 65346 | 711 | lemma degree_add_eq_left: "degree q < degree p \<Longrightarrow> degree (p + q) = degree p" | 
| 712 | using degree_add_eq_right [of q p] by (simp add: add.commute) | |
| 713 | ||
| 714 | lemma degree_minus [simp]: "degree (- p) = degree p" | |
| 715 | by (simp add: degree_def) | |
| 716 | ||
| 717 | lemma lead_coeff_add_le: "degree p < degree q \<Longrightarrow> lead_coeff (p + q) = lead_coeff q" | |
| 64795 | 718 | by (metis coeff_add coeff_eq_0 monoid_add_class.add.left_neutral degree_add_eq_right) | 
| 719 | ||
| 65346 | 720 | lemma lead_coeff_minus: "lead_coeff (- p) = - lead_coeff p" | 
| 64795 | 721 | by (metis coeff_minus degree_minus) | 
| 722 | ||
| 65346 | 723 | lemma degree_diff_le_max: "degree (p - q) \<le> max (degree p) (degree q)" | 
| 724 | for p q :: "'a::ab_group_add poly" | |
| 725 | using degree_add_le [where p=p and q="-q"] by simp | |
| 726 | ||
| 727 | lemma degree_diff_le: "degree p \<le> n \<Longrightarrow> degree q \<le> n \<Longrightarrow> degree (p - q) \<le> n" | |
| 728 | for p q :: "'a::ab_group_add poly" | |
| 729 | using degree_add_le [of p n "- q"] by simp | |
| 730 | ||
| 731 | lemma degree_diff_less: "degree p < n \<Longrightarrow> degree q < n \<Longrightarrow> degree (p - q) < n" | |
| 732 | for p q :: "'a::ab_group_add poly" | |
| 733 | using degree_add_less [of p n "- q"] by simp | |
| 29453 | 734 | |
| 29451 | 735 | lemma add_monom: "monom a n + monom b n = monom (a + b) n" | 
| 52380 | 736 | by (rule poly_eqI) simp | 
| 29451 | 737 | |
| 738 | lemma diff_monom: "monom a n - monom b n = monom (a - b) n" | |
| 52380 | 739 | by (rule poly_eqI) simp | 
| 29451 | 740 | |
| 65346 | 741 | lemma minus_monom: "- monom a n = monom (- a) n" | 
| 52380 | 742 | by (rule poly_eqI) simp | 
| 29451 | 743 | |
| 64267 | 744 | lemma coeff_sum: "coeff (\<Sum>x\<in>A. p x) i = (\<Sum>x\<in>A. coeff (p x) i)" | 
| 65346 | 745 | by (induct A rule: infinite_finite_induct) simp_all | 
| 29451 | 746 | |
| 64267 | 747 | lemma monom_sum: "monom (\<Sum>x\<in>A. a x) n = (\<Sum>x\<in>A. monom (a x) n)" | 
| 748 | by (rule poly_eqI) (simp add: coeff_sum) | |
| 52380 | 749 | |
| 750 | fun plus_coeffs :: "'a::comm_monoid_add list \<Rightarrow> 'a list \<Rightarrow> 'a list" | |
| 65346 | 751 | where | 
| 752 | "plus_coeffs xs [] = xs" | |
| 753 | | "plus_coeffs [] ys = ys" | |
| 754 | | "plus_coeffs (x # xs) (y # ys) = (x + y) ## plus_coeffs xs ys" | |
| 52380 | 755 | |
| 756 | lemma coeffs_plus_eq_plus_coeffs [code abstract]: | |
| 757 | "coeffs (p + q) = plus_coeffs (coeffs p) (coeffs q)" | |
| 758 | proof - | |
| 65346 | 759 | have *: "nth_default 0 (plus_coeffs xs ys) n = nth_default 0 xs n + nth_default 0 ys n" | 
| 760 | for xs ys :: "'a list" and n | |
| 761 | proof (induct xs ys arbitrary: n rule: plus_coeffs.induct) | |
| 65390 | 762 | case (3 x xs y ys n) | 
| 763 | then show ?case | |
| 764 | by (cases n) (auto simp add: cCons_def) | |
| 65346 | 765 | qed simp_all | 
| 65390 | 766 | have **: "no_trailing (HOL.eq 0) (plus_coeffs xs ys)" | 
| 767 | if "no_trailing (HOL.eq 0) xs" and "no_trailing (HOL.eq 0) ys" | |
| 768 | for xs ys :: "'a list" | |
| 769 | using that by (induct xs ys rule: plus_coeffs.induct) (simp_all add: cCons_def) | |
| 52380 | 770 | show ?thesis | 
| 65390 | 771 | by (rule coeffs_eqI) (auto simp add: * nth_default_coeffs_eq intro: **) | 
| 52380 | 772 | qed | 
| 773 | ||
| 65390 | 774 | lemma coeffs_uminus [code abstract]: | 
| 775 | "coeffs (- p) = map uminus (coeffs p)" | |
| 776 | proof - | |
| 777 | have eq_0: "HOL.eq 0 \<circ> uminus = HOL.eq (0::'a)" | |
| 778 | by (simp add: fun_eq_iff) | |
| 779 | show ?thesis | |
| 780 | by (rule coeffs_eqI) (simp_all add: nth_default_map_eq nth_default_coeffs_eq no_trailing_map eq_0) | |
| 781 | qed | |
| 52380 | 782 | |
| 65346 | 783 | lemma [code]: "p - q = p + - q" | 
| 784 | for p q :: "'a::ab_group_add poly" | |
| 59557 | 785 | by (fact diff_conv_add_uminus) | 
| 52380 | 786 | |
| 787 | lemma poly_add [simp]: "poly (p + q) x = poly p x + poly q x" | |
| 65346 | 788 | apply (induct p arbitrary: q) | 
| 789 | apply simp | |
| 52380 | 790 | apply (case_tac q, simp, simp add: algebra_simps) | 
| 791 | done | |
| 792 | ||
| 65346 | 793 | lemma poly_minus [simp]: "poly (- p) x = - poly p x" | 
| 794 | for x :: "'a::comm_ring" | |
| 52380 | 795 | by (induct p) simp_all | 
| 796 | ||
| 65346 | 797 | lemma poly_diff [simp]: "poly (p - q) x = poly p x - poly q x" | 
| 798 | for x :: "'a::comm_ring" | |
| 54230 
b1d955791529
more simplification rules on unary and binary minus
 haftmann parents: 
52380diff
changeset | 799 | using poly_add [of p "- q" x] by simp | 
| 52380 | 800 | |
| 64267 | 801 | lemma poly_sum: "poly (\<Sum>k\<in>A. p k) x = (\<Sum>k\<in>A. poly (p k) x)" | 
| 52380 | 802 | by (induct A rule: infinite_finite_induct) simp_all | 
| 29451 | 803 | |
| 65346 | 804 | lemma degree_sum_le: "finite S \<Longrightarrow> (\<And>p. p \<in> S \<Longrightarrow> degree (f p) \<le> n) \<Longrightarrow> degree (sum f S) \<le> n" | 
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 805 | proof (induct S rule: finite_induct) | 
| 65346 | 806 | case empty | 
| 807 | then show ?case by simp | |
| 808 | next | |
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 809 | case (insert p S) | 
| 65346 | 810 | then have "degree (sum f S) \<le> n" "degree (f p) \<le> n" | 
| 811 | by auto | |
| 812 | then show ?case | |
| 813 | unfolding sum.insert[OF insert(1-2)] by (metis degree_add_le) | |
| 814 | qed | |
| 815 | ||
| 816 | lemma poly_as_sum_of_monoms': | |
| 817 | assumes "degree p \<le> n" | |
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 818 | shows "(\<Sum>i\<le>n. monom (coeff p i) i) = p" | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 819 | proof - | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 820 |   have eq: "\<And>i. {..n} \<inter> {i} = (if i \<le> n then {i} else {})"
 | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 821 | by auto | 
| 65346 | 822 | from assms show ?thesis | 
| 823 | by (simp add: poly_eq_iff coeff_sum coeff_eq_0 sum.If_cases eq | |
| 824 | if_distrib[where f="\<lambda>x. x * a" for a]) | |
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 825 | qed | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 826 | |
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 827 | lemma poly_as_sum_of_monoms: "(\<Sum>i\<le>degree p. monom (coeff p i) i) = p" | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 828 | by (intro poly_as_sum_of_monoms' order_refl) | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 829 | |
| 62065 | 830 | lemma Poly_snoc: "Poly (xs @ [x]) = Poly xs + monom x (length xs)" | 
| 65346 | 831 | by (induct xs) (simp_all add: monom_0 monom_Suc) | 
| 62065 | 832 | |
| 29451 | 833 | |
| 60500 | 834 | subsection \<open>Multiplication by a constant, polynomial multiplication and the unit polynomial\<close> | 
| 29451 | 835 | |
| 52380 | 836 | lift_definition smult :: "'a::comm_semiring_0 \<Rightarrow> 'a poly \<Rightarrow> 'a poly" | 
| 837 | is "\<lambda>a p n. a * coeff p n" | |
| 60040 
1fa1023b13b9
move MOST and INFM in Infinite_Set to Filter; change them to abbreviations over the cofinite filter
 hoelzl parents: 
59983diff
changeset | 838 | proof - | 
| 65346 | 839 | fix a :: 'a and p :: "'a poly" | 
| 840 | show "\<forall>\<^sub>\<infinity> i. a * coeff p i = 0" | |
| 60040 
1fa1023b13b9
move MOST and INFM in Infinite_Set to Filter; change them to abbreviations over the cofinite filter
 hoelzl parents: 
59983diff
changeset | 841 | using MOST_coeff_eq_0[of p] by eventually_elim simp | 
| 52380 | 842 | qed | 
| 29451 | 843 | |
| 65346 | 844 | lemma coeff_smult [simp]: "coeff (smult a p) n = a * coeff p n" | 
| 52380 | 845 | by (simp add: smult.rep_eq) | 
| 29451 | 846 | |
| 847 | lemma degree_smult_le: "degree (smult a p) \<le> degree p" | |
| 65346 | 848 | by (rule degree_le) (simp add: coeff_eq_0) | 
| 29451 | 849 | |
| 29472 | 850 | lemma smult_smult [simp]: "smult a (smult b p) = smult (a * b) p" | 
| 65346 | 851 | by (rule poly_eqI) (simp add: mult.assoc) | 
| 29451 | 852 | |
| 853 | lemma smult_0_right [simp]: "smult a 0 = 0" | |
| 65346 | 854 | by (rule poly_eqI) simp | 
| 29451 | 855 | |
| 856 | lemma smult_0_left [simp]: "smult 0 p = 0" | |
| 65346 | 857 | by (rule poly_eqI) simp | 
| 29451 | 858 | |
| 859 | lemma smult_1_left [simp]: "smult (1::'a::comm_semiring_1) p = p" | |
| 65346 | 860 | by (rule poly_eqI) simp | 
| 861 | ||
| 862 | lemma smult_add_right: "smult a (p + q) = smult a p + smult a q" | |
| 863 | by (rule poly_eqI) (simp add: algebra_simps) | |
| 864 | ||
| 865 | lemma smult_add_left: "smult (a + b) p = smult a p + smult b p" | |
| 866 | by (rule poly_eqI) (simp add: algebra_simps) | |
| 867 | ||
| 868 | lemma smult_minus_right [simp]: "smult a (- p) = - smult a p" | |
| 869 | for a :: "'a::comm_ring" | |
| 870 | by (rule poly_eqI) simp | |
| 871 | ||
| 872 | lemma smult_minus_left [simp]: "smult (- a) p = - smult a p" | |
| 873 | for a :: "'a::comm_ring" | |
| 874 | by (rule poly_eqI) simp | |
| 875 | ||
| 876 | lemma smult_diff_right: "smult a (p - q) = smult a p - smult a q" | |
| 877 | for a :: "'a::comm_ring" | |
| 878 | by (rule poly_eqI) (simp add: algebra_simps) | |
| 879 | ||
| 880 | lemma smult_diff_left: "smult (a - b) p = smult a p - smult b p" | |
| 881 | for a b :: "'a::comm_ring" | |
| 882 | by (rule poly_eqI) (simp add: algebra_simps) | |
| 29451 | 883 | |
| 29472 | 884 | lemmas smult_distribs = | 
| 885 | smult_add_left smult_add_right | |
| 886 | smult_diff_left smult_diff_right | |
| 887 | ||
| 65346 | 888 | lemma smult_pCons [simp]: "smult a (pCons b p) = pCons (a * b) (smult a p)" | 
| 889 | by (rule poly_eqI) (simp add: coeff_pCons split: nat.split) | |
| 29451 | 890 | |
| 891 | lemma smult_monom: "smult a (monom b n) = monom (a * b) n" | |
| 65346 | 892 | by (induct n) (simp_all add: monom_0 monom_Suc) | 
| 29451 | 893 | |
| 69064 
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
 nipkow parents: 
69022diff
changeset | 894 | lemma smult_Poly: "smult c (Poly xs) = Poly (map ((*) c) xs)" | 
| 65346 | 895 | by (auto simp: poly_eq_iff nth_default_def) | 
| 896 | ||
| 897 | lemma degree_smult_eq [simp]: "degree (smult a p) = (if a = 0 then 0 else degree p)" | |
| 898 |   for a :: "'a::{comm_semiring_0,semiring_no_zero_divisors}"
 | |
| 899 | by (cases "a = 0") (simp_all add: degree_def) | |
| 900 | ||
| 901 | lemma smult_eq_0_iff [simp]: "smult a p = 0 \<longleftrightarrow> a = 0 \<or> p = 0" | |
| 902 |   for a :: "'a::{comm_semiring_0,semiring_no_zero_divisors}"
 | |
| 52380 | 903 | by (simp add: poly_eq_iff) | 
| 65346 | 904 | |
| 52380 | 905 | lemma coeffs_smult [code abstract]: | 
| 65346 | 906 | "coeffs (smult a p) = (if a = 0 then [] else map (Groups.times a) (coeffs p))" | 
| 907 |   for p :: "'a::{comm_semiring_0,semiring_no_zero_divisors} poly"
 | |
| 65390 | 908 | proof - | 
| 909 | have eq_0: "HOL.eq 0 \<circ> times a = HOL.eq (0::'a)" if "a \<noteq> 0" | |
| 910 | using that by (simp add: fun_eq_iff) | |
| 911 | show ?thesis | |
| 912 | by (rule coeffs_eqI) (auto simp add: no_trailing_map nth_default_map_eq nth_default_coeffs_eq eq_0) | |
| 913 | qed | |
| 64795 | 914 | |
| 915 | lemma smult_eq_iff: | |
| 65346 | 916 | fixes b :: "'a :: field" | 
| 917 | assumes "b \<noteq> 0" | |
| 918 | shows "smult a p = smult b q \<longleftrightarrow> smult (a / b) p = q" | |
| 919 | (is "?lhs \<longleftrightarrow> ?rhs") | |
| 64795 | 920 | proof | 
| 65346 | 921 | assume ?lhs | 
| 922 | also from assms have "smult (inverse b) \<dots> = q" | |
| 923 | by simp | |
| 924 | finally show ?rhs | |
| 925 | by (simp add: field_simps) | |
| 926 | next | |
| 927 | assume ?rhs | |
| 928 | with assms show ?lhs by auto | |
| 929 | qed | |
| 64795 | 930 | |
| 29451 | 931 | instantiation poly :: (comm_semiring_0) comm_semiring_0 | 
| 932 | begin | |
| 933 | ||
| 65346 | 934 | definition "p * q = fold_coeffs (\<lambda>a p. smult a q + pCons 0 p) p 0" | 
| 29474 | 935 | |
| 936 | lemma mult_poly_0_left: "(0::'a poly) * q = 0" | |
| 52380 | 937 | by (simp add: times_poly_def) | 
| 29474 | 938 | |
| 65346 | 939 | lemma mult_pCons_left [simp]: "pCons a p * q = smult a q + pCons 0 (p * q)" | 
| 52380 | 940 | by (cases "p = 0 \<and> a = 0") (auto simp add: times_poly_def) | 
| 29474 | 941 | |
| 942 | lemma mult_poly_0_right: "p * (0::'a poly) = 0" | |
| 65346 | 943 | by (induct p) (simp_all add: mult_poly_0_left) | 
| 944 | ||
| 945 | lemma mult_pCons_right [simp]: "p * pCons a q = smult a p + pCons 0 (p * q)" | |
| 946 | by (induct p) (simp_all add: mult_poly_0_left algebra_simps) | |
| 29474 | 947 | |
| 948 | lemmas mult_poly_0 = mult_poly_0_left mult_poly_0_right | |
| 949 | ||
| 65346 | 950 | lemma mult_smult_left [simp]: "smult a p * q = smult a (p * q)" | 
| 951 | by (induct p) (simp_all add: mult_poly_0 smult_add_right) | |
| 952 | ||
| 953 | lemma mult_smult_right [simp]: "p * smult a q = smult a (p * q)" | |
| 954 | by (induct q) (simp_all add: mult_poly_0 smult_add_right) | |
| 955 | ||
| 956 | lemma mult_poly_add_left: "(p + q) * r = p * r + q * r" | |
| 957 | for p q r :: "'a poly" | |
| 958 | by (induct r) (simp_all add: mult_poly_0 smult_distribs algebra_simps) | |
| 29451 | 959 | |
| 60679 | 960 | instance | 
| 961 | proof | |
| 29451 | 962 | fix p q r :: "'a poly" | 
| 963 | show 0: "0 * p = 0" | |
| 29474 | 964 | by (rule mult_poly_0_left) | 
| 29451 | 965 | show "p * 0 = 0" | 
| 29474 | 966 | by (rule mult_poly_0_right) | 
| 29451 | 967 | show "(p + q) * r = p * r + q * r" | 
| 29474 | 968 | by (rule mult_poly_add_left) | 
| 29451 | 969 | show "(p * q) * r = p * (q * r)" | 
| 65346 | 970 | by (induct p) (simp_all add: mult_poly_0 mult_poly_add_left) | 
| 29451 | 971 | show "p * q = q * p" | 
| 65346 | 972 | by (induct p) (simp_all add: mult_poly_0) | 
| 29451 | 973 | qed | 
| 974 | ||
| 975 | end | |
| 976 | ||
| 63498 | 977 | lemma coeff_mult_degree_sum: | 
| 65346 | 978 | "coeff (p * q) (degree p + degree q) = coeff p (degree p) * coeff q (degree q)" | 
| 979 | by (induct p) (simp_all add: coeff_eq_0) | |
| 63498 | 980 | |
| 981 | instance poly :: ("{comm_semiring_0,semiring_no_zero_divisors}") semiring_no_zero_divisors
 | |
| 982 | proof | |
| 983 | fix p q :: "'a poly" | |
| 984 | assume "p \<noteq> 0" and "q \<noteq> 0" | |
| 65346 | 985 | have "coeff (p * q) (degree p + degree q) = coeff p (degree p) * coeff q (degree q)" | 
| 63498 | 986 | by (rule coeff_mult_degree_sum) | 
| 65346 | 987 | also from \<open>p \<noteq> 0\<close> \<open>q \<noteq> 0\<close> have "coeff p (degree p) * coeff q (degree q) \<noteq> 0" | 
| 988 | by simp | |
| 63498 | 989 | finally have "\<exists>n. coeff (p * q) n \<noteq> 0" .. | 
| 65346 | 990 | then show "p * q \<noteq> 0" | 
| 991 | by (simp add: poly_eq_iff) | |
| 63498 | 992 | qed | 
| 993 | ||
| 29540 | 994 | instance poly :: (comm_semiring_0_cancel) comm_semiring_0_cancel .. | 
| 995 | ||
| 65346 | 996 | lemma coeff_mult: "coeff (p * q) n = (\<Sum>i\<le>n. coeff p i * coeff q (n-i))" | 
| 29474 | 997 | proof (induct p arbitrary: n) | 
| 65346 | 998 | case 0 | 
| 999 | show ?case by simp | |
| 29474 | 1000 | next | 
| 65346 | 1001 | case (pCons a p n) | 
| 1002 | then show ?case | |
| 70113 
c8deb8ba6d05
Fixing the main Homology theory; also moving a lot of sum/prod lemmas into their generic context
 paulson <lp15@cam.ac.uk> parents: 
70097diff
changeset | 1003 | by (cases n) (simp_all add: sum.atMost_Suc_shift del: sum.atMost_Suc) | 
| 29474 | 1004 | qed | 
| 29451 | 1005 | |
| 29474 | 1006 | lemma degree_mult_le: "degree (p * q) \<le> degree p + degree q" | 
| 65346 | 1007 | apply (rule degree_le) | 
| 1008 | apply (induct p) | |
| 1009 | apply simp | |
| 1010 | apply (simp add: coeff_eq_0 coeff_pCons split: nat.split) | |
| 1011 | done | |
| 29451 | 1012 | |
| 1013 | lemma mult_monom: "monom a m * monom b n = monom (a * b) (m + n)" | |
| 60679 | 1014 | by (induct m) (simp add: monom_0 smult_monom, simp add: monom_Suc) | 
| 29451 | 1015 | |
| 1016 | instantiation poly :: (comm_semiring_1) comm_semiring_1 | |
| 1017 | begin | |
| 1018 | ||
| 65486 | 1019 | lift_definition one_poly :: "'a poly" | 
| 1020 | is "\<lambda>n. of_bool (n = 0)" | |
| 1021 | by (rule MOST_SucD) simp | |
| 1022 | ||
| 1023 | lemma coeff_1 [simp]: | |
| 1024 | "coeff 1 n = of_bool (n = 0)" | |
| 1025 | by (simp add: one_poly.rep_eq) | |
| 1026 | ||
| 1027 | lemma one_pCons: | |
| 1028 | "1 = [:1:]" | |
| 1029 | by (simp add: poly_eq_iff coeff_pCons split: nat.splits) | |
| 1030 | ||
| 1031 | lemma pCons_one: | |
| 1032 | "[:1:] = 1" | |
| 1033 | by (simp add: one_pCons) | |
| 29451 | 1034 | |
| 60679 | 1035 | instance | 
| 65486 | 1036 | by standard (simp_all add: one_pCons) | 
| 29451 | 1037 | |
| 1038 | end | |
| 1039 | ||
| 65486 | 1040 | lemma poly_1 [simp]: | 
| 1041 | "poly 1 x = 1" | |
| 1042 | by (simp add: one_pCons) | |
| 1043 | ||
| 1044 | lemma one_poly_eq_simps [simp]: | |
| 1045 | "1 = [:1:] \<longleftrightarrow> True" | |
| 1046 | "[:1:] = 1 \<longleftrightarrow> True" | |
| 1047 | by (simp_all add: one_pCons) | |
| 1048 | ||
| 1049 | lemma degree_1 [simp]: | |
| 1050 | "degree 1 = 0" | |
| 1051 | by (simp add: one_pCons) | |
| 1052 | ||
| 1053 | lemma coeffs_1_eq [simp, code abstract]: | |
| 1054 | "coeffs 1 = [1]" | |
| 1055 | by (simp add: one_pCons) | |
| 1056 | ||
| 1057 | lemma smult_one [simp]: | |
| 1058 | "smult c 1 = [:c:]" | |
| 1059 | by (simp add: one_pCons) | |
| 1060 | ||
| 1061 | lemma monom_eq_1 [simp]: | |
| 1062 | "monom 1 0 = 1" | |
| 1063 | by (simp add: monom_0 one_pCons) | |
| 1064 | ||
| 1065 | lemma monom_eq_1_iff: | |
| 1066 | "monom c n = 1 \<longleftrightarrow> c = 1 \<and> n = 0" | |
| 1067 | using monom_eq_const_iff [of c n 1] by auto | |
| 1068 | ||
| 1069 | lemma monom_altdef: | |
| 1070 | "monom c n = smult c ([:0, 1:] ^ n)" | |
| 1071 | by (induct n) (simp_all add: monom_0 monom_Suc) | |
| 1072 | ||
| 63498 | 1073 | instance poly :: ("{comm_semiring_1,semiring_1_no_zero_divisors}") semiring_1_no_zero_divisors ..
 | 
| 52380 | 1074 | instance poly :: (comm_ring) comm_ring .. | 
| 1075 | instance poly :: (comm_ring_1) comm_ring_1 .. | |
| 63498 | 1076 | instance poly :: (comm_ring_1) comm_semiring_1_cancel .. | 
| 1077 | ||
| 65346 | 1078 | lemma degree_power_le: "degree (p ^ n) \<le> degree p * n" | 
| 52380 | 1079 | by (induct n) (auto intro: order_trans degree_mult_le) | 
| 1080 | ||
| 65346 | 1081 | lemma coeff_0_power: "coeff (p ^ n) 0 = coeff p 0 ^ n" | 
| 1082 | by (induct n) (simp_all add: coeff_mult) | |
| 1083 | ||
| 1084 | lemma poly_smult [simp]: "poly (smult a p) x = a * poly p x" | |
| 1085 | by (induct p) (simp_all add: algebra_simps) | |
| 1086 | ||
| 1087 | lemma poly_mult [simp]: "poly (p * q) x = poly p x * poly q x" | |
| 1088 | by (induct p) (simp_all add: algebra_simps) | |
| 1089 | ||
| 1090 | lemma poly_power [simp]: "poly (p ^ n) x = poly p x ^ n" | |
| 1091 | for p :: "'a::comm_semiring_1 poly" | |
| 52380 | 1092 | by (induct n) simp_all | 
| 1093 | ||
| 64272 | 1094 | lemma poly_prod: "poly (\<Prod>k\<in>A. p k) x = (\<Prod>k\<in>A. poly (p k) x)" | 
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1095 | by (induct A rule: infinite_finite_induct) simp_all | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1096 | |
| 67091 | 1097 | lemma degree_prod_sum_le: "finite S \<Longrightarrow> degree (prod f S) \<le> sum (degree \<circ> f) S" | 
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1098 | proof (induct S rule: finite_induct) | 
| 65346 | 1099 | case empty | 
| 1100 | then show ?case by simp | |
| 1101 | next | |
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1102 | case (insert a S) | 
| 65346 | 1103 | show ?case | 
| 1104 | unfolding prod.insert[OF insert(1-2)] sum.insert[OF insert(1-2)] | |
| 1105 | by (rule le_trans[OF degree_mult_le]) (use insert in auto) | |
| 1106 | qed | |
| 1107 | ||
| 1108 | lemma coeff_0_prod_list: "coeff (prod_list xs) 0 = prod_list (map (\<lambda>p. coeff p 0) xs)" | |
| 1109 | by (induct xs) (simp_all add: coeff_mult) | |
| 1110 | ||
| 1111 | lemma coeff_monom_mult: "coeff (monom c n * p) k = (if k < n then 0 else c * coeff p (k - n))" | |
| 64795 | 1112 | proof - | 
| 1113 | have "coeff (monom c n * p) k = (\<Sum>i\<le>k. (if n = i then c else 0) * coeff p (k - i))" | |
| 1114 | by (simp add: coeff_mult) | |
| 1115 | also have "\<dots> = (\<Sum>i\<le>k. (if n = i then c * coeff p (k - i) else 0))" | |
| 1116 | by (intro sum.cong) simp_all | |
| 65346 | 1117 | also have "\<dots> = (if k < n then 0 else c * coeff p (k - n))" | 
| 66799 | 1118 | by simp | 
| 64795 | 1119 | finally show ?thesis . | 
| 1120 | qed | |
| 1121 | ||
| 65346 | 1122 | lemma monom_1_dvd_iff': "monom 1 n dvd p \<longleftrightarrow> (\<forall>k<n. coeff p k = 0)" | 
| 64795 | 1123 | proof | 
| 1124 | assume "monom 1 n dvd p" | |
| 65346 | 1125 | then obtain r where "p = monom 1 n * r" | 
| 1126 | by (rule dvdE) | |
| 1127 | then show "\<forall>k<n. coeff p k = 0" | |
| 1128 | by (simp add: coeff_mult) | |
| 64795 | 1129 | next | 
| 1130 | assume zero: "(\<forall>k<n. coeff p k = 0)" | |
| 1131 | define r where "r = Abs_poly (\<lambda>k. coeff p (k + n))" | |
| 1132 | have "\<forall>\<^sub>\<infinity>k. coeff p (k + n) = 0" | |
| 65346 | 1133 | by (subst cofinite_eq_sequentially, subst eventually_sequentially_seg, | 
| 64795 | 1134 | subst cofinite_eq_sequentially [symmetric]) transfer | 
| 65346 | 1135 | then have coeff_r [simp]: "coeff r k = coeff p (k + n)" for k | 
| 1136 | unfolding r_def by (subst poly.Abs_poly_inverse) simp_all | |
| 64795 | 1137 | have "p = monom 1 n * r" | 
| 65346 | 1138 | by (rule poly_eqI, subst coeff_monom_mult) (simp_all add: zero) | 
| 1139 | then show "monom 1 n dvd p" by simp | |
| 64795 | 1140 | qed | 
| 1141 | ||
| 64591 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1142 | |
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1143 | subsection \<open>Mapping polynomials\<close> | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1144 | |
| 65346 | 1145 | definition map_poly :: "('a :: zero \<Rightarrow> 'b :: zero) \<Rightarrow> 'a poly \<Rightarrow> 'b poly"
 | 
| 1146 | where "map_poly f p = Poly (map f (coeffs p))" | |
| 64591 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1147 | |
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1148 | lemma map_poly_0 [simp]: "map_poly f 0 = 0" | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1149 | by (simp add: map_poly_def) | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1150 | |
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1151 | lemma map_poly_1: "map_poly f 1 = [:f 1:]" | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1152 | by (simp add: map_poly_def) | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1153 | |
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1154 | lemma map_poly_1' [simp]: "f 1 = 1 \<Longrightarrow> map_poly f 1 = 1" | 
| 65486 | 1155 | by (simp add: map_poly_def one_pCons) | 
| 64591 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1156 | |
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1157 | lemma coeff_map_poly: | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1158 | assumes "f 0 = 0" | 
| 65346 | 1159 | shows "coeff (map_poly f p) n = f (coeff p n)" | 
| 1160 | by (auto simp: assms map_poly_def nth_default_def coeffs_def not_less Suc_le_eq coeff_eq_0 | |
| 1161 | simp del: upt_Suc) | |
| 1162 | ||
| 1163 | lemma coeffs_map_poly [code abstract]: | |
| 67399 | 1164 | "coeffs (map_poly f p) = strip_while ((=) 0) (map f (coeffs p))" | 
| 64591 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1165 | by (simp add: map_poly_def) | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1166 | |
| 65346 | 1167 | lemma coeffs_map_poly': | 
| 1168 | assumes "\<And>x. x \<noteq> 0 \<Longrightarrow> f x \<noteq> 0" | |
| 1169 | shows "coeffs (map_poly f p) = map f (coeffs p)" | |
| 66799 | 1170 | using assms | 
| 1171 | by (auto simp add: coeffs_map_poly strip_while_idem_iff | |
| 1172 | last_coeffs_eq_coeff_degree no_trailing_unfold last_map) | |
| 65390 | 1173 | |
| 1174 | lemma set_coeffs_map_poly: | |
| 1175 | "(\<And>x. f x = 0 \<longleftrightarrow> x = 0) \<Longrightarrow> set (coeffs (map_poly f p)) = f ` set (coeffs p)" | |
| 1176 | by (simp add: coeffs_map_poly') | |
| 64591 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1177 | |
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1178 | lemma degree_map_poly: | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1179 | assumes "\<And>x. x \<noteq> 0 \<Longrightarrow> f x \<noteq> 0" | 
| 65346 | 1180 | shows "degree (map_poly f p) = degree p" | 
| 64591 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1181 | by (simp add: degree_eq_length_coeffs coeffs_map_poly' assms) | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1182 | |
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1183 | lemma map_poly_eq_0_iff: | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1184 | assumes "f 0 = 0" "\<And>x. x \<in> set (coeffs p) \<Longrightarrow> x \<noteq> 0 \<Longrightarrow> f x \<noteq> 0" | 
| 65346 | 1185 | shows "map_poly f p = 0 \<longleftrightarrow> p = 0" | 
| 64591 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1186 | proof - | 
| 65346 | 1187 | have "(coeff (map_poly f p) n = 0) = (coeff p n = 0)" for n | 
| 1188 | proof - | |
| 1189 | have "coeff (map_poly f p) n = f (coeff p n)" | |
| 1190 | by (simp add: coeff_map_poly assms) | |
| 64591 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1191 | also have "\<dots> = 0 \<longleftrightarrow> coeff p n = 0" | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1192 | proof (cases "n < length (coeffs p)") | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1193 | case True | 
| 65346 | 1194 | then have "coeff p n \<in> set (coeffs p)" | 
| 1195 | by (auto simp: coeffs_def simp del: upt_Suc) | |
| 1196 | with assms show "f (coeff p n) = 0 \<longleftrightarrow> coeff p n = 0" | |
| 1197 | by auto | |
| 1198 | next | |
| 1199 | case False | |
| 1200 | then show ?thesis | |
| 1201 | by (auto simp: assms length_coeffs nth_default_coeffs_eq [symmetric] nth_default_def) | |
| 1202 | qed | |
| 1203 | finally show ?thesis . | |
| 1204 | qed | |
| 1205 | then show ?thesis by (auto simp: poly_eq_iff) | |
| 64591 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1206 | qed | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1207 | |
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1208 | lemma map_poly_smult: | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1209 | assumes "f 0 = 0""\<And>c x. f (c * x) = f c * f x" | 
| 65346 | 1210 | shows "map_poly f (smult c p) = smult (f c) (map_poly f p)" | 
| 64591 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1211 | by (intro poly_eqI) (simp_all add: assms coeff_map_poly) | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1212 | |
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1213 | lemma map_poly_pCons: | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1214 | assumes "f 0 = 0" | 
| 65346 | 1215 | shows "map_poly f (pCons c p) = pCons (f c) (map_poly f p)" | 
| 64591 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1216 | by (intro poly_eqI) (simp_all add: assms coeff_map_poly coeff_pCons split: nat.splits) | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1217 | |
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1218 | lemma map_poly_map_poly: | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1219 | assumes "f 0 = 0" "g 0 = 0" | 
| 65346 | 1220 | shows "map_poly f (map_poly g p) = map_poly (f \<circ> g) p" | 
| 64591 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1221 | by (intro poly_eqI) (simp add: coeff_map_poly assms) | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1222 | |
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1223 | lemma map_poly_id [simp]: "map_poly id p = p" | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1224 | by (simp add: map_poly_def) | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1225 | |
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1226 | lemma map_poly_id' [simp]: "map_poly (\<lambda>x. x) p = p" | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1227 | by (simp add: map_poly_def) | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1228 | |
| 65346 | 1229 | lemma map_poly_cong: | 
| 64591 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1230 | assumes "(\<And>x. x \<in> set (coeffs p) \<Longrightarrow> f x = g x)" | 
| 65346 | 1231 | shows "map_poly f p = map_poly g p" | 
| 64591 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1232 | proof - | 
| 65346 | 1233 | from assms have "map f (coeffs p) = map g (coeffs p)" | 
| 1234 | by (intro map_cong) simp_all | |
| 1235 | then show ?thesis | |
| 1236 | by (simp only: coeffs_eq_iff coeffs_map_poly) | |
| 64591 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1237 | qed | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1238 | |
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1239 | lemma map_poly_monom: "f 0 = 0 \<Longrightarrow> map_poly f (monom c n) = monom (f c) n" | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1240 | by (intro poly_eqI) (simp_all add: coeff_map_poly) | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1241 | |
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1242 | lemma map_poly_idI: | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1243 | assumes "\<And>x. x \<in> set (coeffs p) \<Longrightarrow> f x = x" | 
| 65346 | 1244 | shows "map_poly f p = p" | 
| 64591 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1245 | using map_poly_cong[OF assms, of _ id] by simp | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1246 | |
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1247 | lemma map_poly_idI': | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1248 | assumes "\<And>x. x \<in> set (coeffs p) \<Longrightarrow> f x = x" | 
| 65346 | 1249 | shows "p = map_poly f p" | 
| 64591 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1250 | using map_poly_cong[OF assms, of _ id] by simp | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1251 | |
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1252 | lemma smult_conv_map_poly: "smult c p = map_poly (\<lambda>x. c * x) p" | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1253 | by (intro poly_eqI) (simp_all add: coeff_map_poly) | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1254 | |
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1255 | |
| 65484 | 1256 | subsection \<open>Conversions\<close> | 
| 1257 | ||
| 1258 | lemma of_nat_poly: | |
| 1259 | "of_nat n = [:of_nat n:]" | |
| 65486 | 1260 | by (induct n) (simp_all add: one_pCons) | 
| 65484 | 1261 | |
| 1262 | lemma of_nat_monom: | |
| 1263 | "of_nat n = monom (of_nat n) 0" | |
| 1264 | by (simp add: of_nat_poly monom_0) | |
| 1265 | ||
| 1266 | lemma degree_of_nat [simp]: | |
| 1267 | "degree (of_nat n) = 0" | |
| 62065 | 1268 | by (simp add: of_nat_poly) | 
| 1269 | ||
| 64795 | 1270 | lemma lead_coeff_of_nat [simp]: | 
| 65484 | 1271 | "lead_coeff (of_nat n) = of_nat n" | 
| 64795 | 1272 | by (simp add: of_nat_poly) | 
| 1273 | ||
| 65484 | 1274 | lemma of_int_poly: | 
| 1275 | "of_int k = [:of_int k:]" | |
| 64793 | 1276 | by (simp only: of_int_of_nat of_nat_poly) simp | 
| 1277 | ||
| 65484 | 1278 | lemma of_int_monom: | 
| 1279 | "of_int k = monom (of_int k) 0" | |
| 1280 | by (simp add: of_int_poly monom_0) | |
| 1281 | ||
| 1282 | lemma degree_of_int [simp]: | |
| 1283 | "degree (of_int k) = 0" | |
| 64795 | 1284 | by (simp add: of_int_poly) | 
| 1285 | ||
| 1286 | lemma lead_coeff_of_int [simp]: | |
| 65484 | 1287 | "lead_coeff (of_int k) = of_int k" | 
| 64793 | 1288 | by (simp add: of_int_poly) | 
| 62065 | 1289 | |
| 1290 | lemma numeral_poly: "numeral n = [:numeral n:]" | |
| 65484 | 1291 | proof - | 
| 1292 | have "numeral n = of_nat (numeral n)" | |
| 1293 | by simp | |
| 1294 | also have "\<dots> = [:of_nat (numeral n):]" | |
| 1295 | by (simp add: of_nat_poly) | |
| 1296 | finally show ?thesis | |
| 1297 | by simp | |
| 1298 | qed | |
| 1299 | ||
| 1300 | lemma numeral_monom: | |
| 1301 | "numeral n = monom (numeral n) 0" | |
| 1302 | by (simp add: numeral_poly monom_0) | |
| 1303 | ||
| 1304 | lemma degree_numeral [simp]: | |
| 1305 | "degree (numeral n) = 0" | |
| 1306 | by (simp add: numeral_poly) | |
| 52380 | 1307 | |
| 65346 | 1308 | lemma lead_coeff_numeral [simp]: | 
| 64795 | 1309 | "lead_coeff (numeral n) = numeral n" | 
| 1310 | by (simp add: numeral_poly) | |
| 1311 | ||
| 64591 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1312 | |
| 60500 | 1313 | subsection \<open>Lemmas about divisibility\<close> | 
| 29979 | 1314 | |
| 65346 | 1315 | lemma dvd_smult: | 
| 1316 | assumes "p dvd q" | |
| 1317 | shows "p dvd smult a q" | |
| 29979 | 1318 | proof - | 
| 65346 | 1319 | from assms obtain k where "q = p * k" .. | 
| 29979 | 1320 | then have "smult a q = p * smult a k" by simp | 
| 1321 | then show "p dvd smult a q" .. | |
| 1322 | qed | |
| 1323 | ||
| 65346 | 1324 | lemma dvd_smult_cancel: "p dvd smult a q \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> p dvd q" | 
| 1325 | for a :: "'a::field" | |
| 29979 | 1326 | by (drule dvd_smult [where a="inverse a"]) simp | 
| 1327 | ||
| 65346 | 1328 | lemma dvd_smult_iff: "a \<noteq> 0 \<Longrightarrow> p dvd smult a q \<longleftrightarrow> p dvd q" | 
| 1329 | for a :: "'a::field" | |
| 29979 | 1330 | by (safe elim!: dvd_smult dvd_smult_cancel) | 
| 1331 | ||
| 31663 | 1332 | lemma smult_dvd_cancel: | 
| 65346 | 1333 | assumes "smult a p dvd q" | 
| 1334 | shows "p dvd q" | |
| 31663 | 1335 | proof - | 
| 65346 | 1336 | from assms obtain k where "q = smult a p * k" .. | 
| 31663 | 1337 | then have "q = p * smult a k" by simp | 
| 1338 | then show "p dvd q" .. | |
| 1339 | qed | |
| 1340 | ||
| 65346 | 1341 | lemma smult_dvd: "p dvd q \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> smult a p dvd q" | 
| 1342 | for a :: "'a::field" | |
| 31663 | 1343 | by (rule smult_dvd_cancel [where a="inverse a"]) simp | 
| 1344 | ||
| 65346 | 1345 | lemma smult_dvd_iff: "smult a p dvd q \<longleftrightarrow> (if a = 0 then q = 0 else p dvd q)" | 
| 1346 | for a :: "'a::field" | |
| 31663 | 1347 | by (auto elim: smult_dvd smult_dvd_cancel) | 
| 1348 | ||
| 64795 | 1349 | lemma is_unit_smult_iff: "smult c p dvd 1 \<longleftrightarrow> c dvd 1 \<and> p dvd 1" | 
| 1350 | proof - | |
| 1351 | have "smult c p = [:c:] * p" by simp | |
| 1352 | also have "\<dots> dvd 1 \<longleftrightarrow> c dvd 1 \<and> p dvd 1" | |
| 1353 | proof safe | |
| 65346 | 1354 | assume *: "[:c:] * p dvd 1" | 
| 1355 | then show "p dvd 1" | |
| 1356 | by (rule dvd_mult_right) | |
| 1357 | from * obtain q where q: "1 = [:c:] * p * q" | |
| 1358 | by (rule dvdE) | |
| 1359 | have "c dvd c * (coeff p 0 * coeff q 0)" | |
| 1360 | by simp | |
| 1361 | also have "\<dots> = coeff ([:c:] * p * q) 0" | |
| 1362 | by (simp add: mult.assoc coeff_mult) | |
| 1363 | also note q [symmetric] | |
| 1364 | finally have "c dvd coeff 1 0" . | |
| 1365 | then show "c dvd 1" by simp | |
| 64795 | 1366 | next | 
| 1367 | assume "c dvd 1" "p dvd 1" | |
| 65346 | 1368 | from this(1) obtain d where "1 = c * d" | 
| 1369 | by (rule dvdE) | |
| 1370 | then have "1 = [:c:] * [:d:]" | |
| 65486 | 1371 | by (simp add: one_pCons ac_simps) | 
| 65346 | 1372 | then have "[:c:] dvd 1" | 
| 1373 | by (rule dvdI) | |
| 1374 | from mult_dvd_mono[OF this \<open>p dvd 1\<close>] show "[:c:] * p dvd 1" | |
| 1375 | by simp | |
| 64795 | 1376 | qed | 
| 1377 | finally show ?thesis . | |
| 1378 | qed | |
| 1379 | ||
| 29451 | 1380 | |
| 60500 | 1381 | subsection \<open>Polynomials form an integral domain\<close> | 
| 29451 | 1382 | |
| 63498 | 1383 | instance poly :: (idom) idom .. | 
| 29451 | 1384 | |
| 65577 
32d4117ad6e8
instance for polynomial rings with characteristic zero
 haftmann parents: 
65486diff
changeset | 1385 | instance poly :: ("{ring_char_0, comm_ring_1}") ring_char_0
 | 
| 
32d4117ad6e8
instance for polynomial rings with characteristic zero
 haftmann parents: 
65486diff
changeset | 1386 | by standard (auto simp add: of_nat_poly intro: injI) | 
| 
32d4117ad6e8
instance for polynomial rings with characteristic zero
 haftmann parents: 
65486diff
changeset | 1387 | |
| 65346 | 1388 | lemma degree_mult_eq: "p \<noteq> 0 \<Longrightarrow> q \<noteq> 0 \<Longrightarrow> degree (p * q) = degree p + degree q" | 
| 1389 |   for p q :: "'a::{comm_semiring_0,semiring_no_zero_divisors} poly"
 | |
| 1390 | by (rule order_antisym [OF degree_mult_le le_degree]) (simp add: coeff_mult_degree_sum) | |
| 29451 | 1391 | |
| 64591 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1392 | lemma degree_mult_eq_0: | 
| 65346 | 1393 | "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)" | 
| 1394 |   for p q :: "'a::{comm_semiring_0,semiring_no_zero_divisors} poly"
 | |
| 1395 | by (auto simp: degree_mult_eq) | |
| 64591 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 1396 | |
| 66550 
e5d82cf3c387
Some small lemmas about polynomials and FPSs
 eberlm <eberlm@in.tum.de> parents: 
66453diff
changeset | 1397 | lemma degree_power_eq: "p \<noteq> 0 \<Longrightarrow> degree ((p :: 'a :: idom poly) ^ n) = n * degree p" | 
| 
e5d82cf3c387
Some small lemmas about polynomials and FPSs
 eberlm <eberlm@in.tum.de> parents: 
66453diff
changeset | 1398 | by (induction n) (simp_all add: degree_mult_eq) | 
| 
e5d82cf3c387
Some small lemmas about polynomials and FPSs
 eberlm <eberlm@in.tum.de> parents: 
66453diff
changeset | 1399 | |
| 60570 | 1400 | lemma degree_mult_right_le: | 
| 63498 | 1401 |   fixes p q :: "'a::{comm_semiring_0,semiring_no_zero_divisors} poly"
 | 
| 60570 | 1402 | assumes "q \<noteq> 0" | 
| 1403 | shows "degree p \<le> degree (p * q)" | |
| 1404 | using assms by (cases "p = 0") (simp_all add: degree_mult_eq) | |
| 1405 | ||
| 65346 | 1406 | lemma coeff_degree_mult: "coeff (p * q) (degree (p * q)) = coeff q (degree q) * coeff p (degree p)" | 
| 1407 |   for p q :: "'a::{comm_semiring_0,semiring_no_zero_divisors} poly"
 | |
| 1408 | by (cases "p = 0 \<or> q = 0") (auto simp: degree_mult_eq coeff_mult_degree_sum mult_ac) | |
| 1409 | ||
| 1410 | lemma dvd_imp_degree_le: "p dvd q \<Longrightarrow> q \<noteq> 0 \<Longrightarrow> degree p \<le> degree q" | |
| 1411 |   for p q :: "'a::{comm_semiring_1,semiring_no_zero_divisors} poly"
 | |
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1412 | by (erule dvdE, hypsubst, subst degree_mult_eq) auto | 
| 29451 | 1413 | |
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1414 | lemma divides_degree: | 
| 65346 | 1415 |   fixes p q :: "'a ::{comm_semiring_1,semiring_no_zero_divisors} poly"
 | 
| 1416 | assumes "p dvd q" | |
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1417 | shows "degree p \<le> degree q \<or> q = 0" | 
| 65346 | 1418 | by (metis dvd_imp_degree_le assms) | 
| 1419 | ||
| 63498 | 1420 | lemma const_poly_dvd_iff: | 
| 65346 | 1421 |   fixes c :: "'a::{comm_semiring_1,semiring_no_zero_divisors}"
 | 
| 63498 | 1422 | shows "[:c:] dvd p \<longleftrightarrow> (\<forall>n. c dvd coeff p n)" | 
| 1423 | proof (cases "c = 0 \<or> p = 0") | |
| 65346 | 1424 | case True | 
| 1425 | then show ?thesis | |
| 1426 | by (auto intro!: poly_eqI) | |
| 1427 | next | |
| 63498 | 1428 | case False | 
| 1429 | show ?thesis | |
| 1430 | proof | |
| 1431 | assume "[:c:] dvd p" | |
| 65346 | 1432 | then show "\<forall>n. c dvd coeff p n" | 
| 1433 | by (auto elim!: dvdE simp: coeffs_def) | |
| 63498 | 1434 | next | 
| 1435 | assume *: "\<forall>n. c dvd coeff p n" | |
| 65346 | 1436 | define mydiv where "mydiv x y = (SOME z. x = y * z)" for x y :: 'a | 
| 63498 | 1437 | have mydiv: "x = y * mydiv x y" if "y dvd x" for x y | 
| 1438 | using that unfolding mydiv_def dvd_def by (rule someI_ex) | |
| 1439 | define q where "q = Poly (map (\<lambda>a. mydiv a c) (coeffs p))" | |
| 1440 | from False * have "p = q * [:c:]" | |
| 65346 | 1441 | by (intro poly_eqI) | 
| 1442 | (auto simp: q_def nth_default_def not_less length_coeffs_degree coeffs_nth | |
| 1443 | intro!: coeff_eq_0 mydiv) | |
| 1444 | then show "[:c:] dvd p" | |
| 1445 | by (simp only: dvd_triv_right) | |
| 63498 | 1446 | qed | 
| 65346 | 1447 | qed | 
| 1448 | ||
| 1449 | lemma const_poly_dvd_const_poly_iff [simp]: "[:a:] dvd [:b:] \<longleftrightarrow> a dvd b" | |
| 1450 |   for a b :: "'a::{comm_semiring_1,semiring_no_zero_divisors}"
 | |
| 63498 | 1451 | by (subst const_poly_dvd_iff) (auto simp: coeff_pCons split: nat.splits) | 
| 1452 | ||
| 65346 | 1453 | lemma lead_coeff_mult: "lead_coeff (p * q) = lead_coeff p * lead_coeff q" | 
| 1454 |   for p q :: "'a::{comm_semiring_0, semiring_no_zero_divisors} poly"
 | |
| 1455 | by (cases "p = 0 \<or> q = 0") (auto simp: coeff_mult_degree_sum degree_mult_eq) | |
| 1456 | ||
| 1457 | lemma lead_coeff_smult: "lead_coeff (smult c p) = c * lead_coeff p" | |
| 1458 |   for p :: "'a::{comm_semiring_0,semiring_no_zero_divisors} poly"
 | |
| 64795 | 1459 | proof - | 
| 1460 | have "smult c p = [:c:] * p" by simp | |
| 1461 | also have "lead_coeff \<dots> = c * lead_coeff p" | |
| 1462 | by (subst lead_coeff_mult) simp_all | |
| 1463 | finally show ?thesis . | |
| 1464 | qed | |
| 1465 | ||
| 1466 | lemma lead_coeff_1 [simp]: "lead_coeff 1 = 1" | |
| 1467 | by simp | |
| 1468 | ||
| 65346 | 1469 | lemma lead_coeff_power: "lead_coeff (p ^ n) = lead_coeff p ^ n" | 
| 1470 |   for p :: "'a::{comm_semiring_1,semiring_no_zero_divisors} poly"
 | |
| 1471 | by (induct n) (simp_all add: lead_coeff_mult) | |
| 64795 | 1472 | |
| 29451 | 1473 | |
| 60500 | 1474 | subsection \<open>Polynomials form an ordered integral domain\<close> | 
| 29878 | 1475 | |
| 63498 | 1476 | definition pos_poly :: "'a::linordered_semidom poly \<Rightarrow> bool" | 
| 65346 | 1477 | where "pos_poly p \<longleftrightarrow> 0 < coeff p (degree p)" | 
| 1478 | ||
| 1479 | lemma pos_poly_pCons: "pos_poly (pCons a p) \<longleftrightarrow> pos_poly p \<or> (p = 0 \<and> 0 < a)" | |
| 1480 | by (simp add: pos_poly_def) | |
| 29878 | 1481 | |
| 1482 | lemma not_pos_poly_0 [simp]: "\<not> pos_poly 0" | |
| 65346 | 1483 | by (simp add: pos_poly_def) | 
| 1484 | ||
| 1485 | lemma pos_poly_add: "pos_poly p \<Longrightarrow> pos_poly q \<Longrightarrow> pos_poly (p + q)" | |
| 1486 | apply (induct p arbitrary: q) | |
| 1487 | apply simp | |
| 1488 | apply (case_tac q) | |
| 1489 | apply (force simp add: pos_poly_pCons add_pos_pos) | |
| 29878 | 1490 | done | 
| 1491 | ||
| 65346 | 1492 | lemma pos_poly_mult: "pos_poly p \<Longrightarrow> pos_poly q \<Longrightarrow> pos_poly (p * q)" | 
| 29878 | 1493 | unfolding pos_poly_def | 
| 1494 | apply (subgoal_tac "p \<noteq> 0 \<and> q \<noteq> 0") | |
| 65346 | 1495 | apply (simp add: degree_mult_eq coeff_mult_degree_sum) | 
| 29878 | 1496 | apply auto | 
| 1497 | done | |
| 1498 | ||
| 65346 | 1499 | lemma pos_poly_total: "p = 0 \<or> pos_poly p \<or> pos_poly (- p)" | 
| 1500 | for p :: "'a::linordered_idom poly" | |
| 1501 | by (induct p) (auto simp: pos_poly_pCons) | |
| 1502 | ||
| 1503 | lemma pos_poly_coeffs [code]: "pos_poly p \<longleftrightarrow> (let as = coeffs p in as \<noteq> [] \<and> last as > 0)" | |
| 1504 | (is "?lhs \<longleftrightarrow> ?rhs") | |
| 52380 | 1505 | proof | 
| 65346 | 1506 | assume ?rhs | 
| 1507 | then show ?lhs | |
| 1508 | by (auto simp add: pos_poly_def last_coeffs_eq_coeff_degree) | |
| 52380 | 1509 | next | 
| 65346 | 1510 | assume ?lhs | 
| 1511 | then have *: "0 < coeff p (degree p)" | |
| 1512 | by (simp add: pos_poly_def) | |
| 1513 | then have "p \<noteq> 0" | |
| 1514 | by auto | |
| 1515 | with * show ?rhs | |
| 1516 | by (simp add: last_coeffs_eq_coeff_degree) | |
| 52380 | 1517 | qed | 
| 1518 | ||
| 35028 
108662d50512
more consistent naming of type classes involving orderings (and lattices) -- c.f. NEWS
 haftmann parents: 
34973diff
changeset | 1519 | instantiation poly :: (linordered_idom) linordered_idom | 
| 29878 | 1520 | begin | 
| 1521 | ||
| 65346 | 1522 | definition "x < y \<longleftrightarrow> pos_poly (y - x)" | 
| 1523 | ||
| 1524 | definition "x \<le> y \<longleftrightarrow> x = y \<or> pos_poly (y - x)" | |
| 1525 | ||
| 1526 | definition "\<bar>x::'a poly\<bar> = (if x < 0 then - x else x)" | |
| 1527 | ||
| 1528 | definition "sgn (x::'a poly) = (if x = 0 then 0 else if 0 < x then 1 else - 1)" | |
| 29878 | 1529 | |
| 60679 | 1530 | instance | 
| 1531 | proof | |
| 1532 | fix x y z :: "'a poly" | |
| 29878 | 1533 | show "x < y \<longleftrightarrow> x \<le> y \<and> \<not> y \<le> x" | 
| 1534 | unfolding less_eq_poly_def less_poly_def | |
| 1535 | apply safe | |
| 65346 | 1536 | apply simp | 
| 29878 | 1537 | apply (drule (1) pos_poly_add) | 
| 1538 | apply simp | |
| 1539 | done | |
| 60679 | 1540 | show "x \<le> x" | 
| 65346 | 1541 | by (simp add: less_eq_poly_def) | 
| 60679 | 1542 | show "x \<le> y \<Longrightarrow> y \<le> z \<Longrightarrow> x \<le> z" | 
| 29878 | 1543 | unfolding less_eq_poly_def | 
| 1544 | apply safe | |
| 1545 | apply (drule (1) pos_poly_add) | |
| 1546 | apply (simp add: algebra_simps) | |
| 1547 | done | |
| 60679 | 1548 | show "x \<le> y \<Longrightarrow> y \<le> x \<Longrightarrow> x = y" | 
| 29878 | 1549 | unfolding less_eq_poly_def | 
| 1550 | apply safe | |
| 1551 | apply (drule (1) pos_poly_add) | |
| 1552 | apply simp | |
| 1553 | done | |
| 60679 | 1554 | show "x \<le> y \<Longrightarrow> z + x \<le> z + y" | 
| 29878 | 1555 | unfolding less_eq_poly_def | 
| 1556 | apply safe | |
| 1557 | apply (simp add: algebra_simps) | |
| 1558 | done | |
| 1559 | show "x \<le> y \<or> y \<le> x" | |
| 1560 | unfolding less_eq_poly_def | |
| 1561 | using pos_poly_total [of "x - y"] | |
| 1562 | by auto | |
| 60679 | 1563 | show "x < y \<Longrightarrow> 0 < z \<Longrightarrow> z * x < z * y" | 
| 65346 | 1564 | by (simp add: less_poly_def right_diff_distrib [symmetric] pos_poly_mult) | 
| 29878 | 1565 | show "\<bar>x\<bar> = (if x < 0 then - x else x)" | 
| 1566 | by (rule abs_poly_def) | |
| 1567 | show "sgn x = (if x = 0 then 0 else if 0 < x then 1 else - 1)" | |
| 1568 | by (rule sgn_poly_def) | |
| 1569 | qed | |
| 1570 | ||
| 1571 | end | |
| 1572 | ||
| 60500 | 1573 | text \<open>TODO: Simplification rules for comparisons\<close> | 
| 29878 | 1574 | |
| 1575 | ||
| 60500 | 1576 | subsection \<open>Synthetic division and polynomial roots\<close> | 
| 52380 | 1577 | |
| 65346 | 1578 | subsubsection \<open>Synthetic division\<close> | 
| 1579 | ||
| 69597 | 1580 | text \<open>Synthetic division is simply division by the linear polynomial \<^term>\<open>x - c\<close>.\<close> | 
| 52380 | 1581 | |
| 1582 | definition synthetic_divmod :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly \<times> 'a" | |
| 65346 | 1583 | where "synthetic_divmod p c = fold_coeffs (\<lambda>a (q, r). (pCons r q, a + c * r)) p (0, 0)" | 
| 52380 | 1584 | |
| 1585 | definition synthetic_div :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly" | |
| 65346 | 1586 | where "synthetic_div p c = fst (synthetic_divmod p c)" | 
| 1587 | ||
| 1588 | lemma synthetic_divmod_0 [simp]: "synthetic_divmod 0 c = (0, 0)" | |
| 52380 | 1589 | by (simp add: synthetic_divmod_def) | 
| 1590 | ||
| 1591 | lemma synthetic_divmod_pCons [simp]: | |
| 1592 | "synthetic_divmod (pCons a p) c = (\<lambda>(q, r). (pCons r q, a + c * r)) (synthetic_divmod p c)" | |
| 1593 | by (cases "p = 0 \<and> a = 0") (auto simp add: synthetic_divmod_def) | |
| 1594 | ||
| 65346 | 1595 | lemma synthetic_div_0 [simp]: "synthetic_div 0 c = 0" | 
| 1596 | by (simp add: synthetic_div_def) | |
| 52380 | 1597 | |
| 1598 | lemma synthetic_div_unique_lemma: "smult c p = pCons a p \<Longrightarrow> p = 0" | |
| 65346 | 1599 | by (induct p arbitrary: a) simp_all | 
| 1600 | ||
| 1601 | lemma snd_synthetic_divmod: "snd (synthetic_divmod p c) = poly p c" | |
| 1602 | by (induct p) (simp_all add: split_def) | |
| 52380 | 1603 | |
| 1604 | lemma synthetic_div_pCons [simp]: | |
| 1605 | "synthetic_div (pCons a p) c = pCons (poly p c) (synthetic_div p c)" | |
| 65346 | 1606 | by (simp add: synthetic_div_def split_def snd_synthetic_divmod) | 
| 1607 | ||
| 1608 | lemma synthetic_div_eq_0_iff: "synthetic_div p c = 0 \<longleftrightarrow> degree p = 0" | |
| 63649 | 1609 | proof (induct p) | 
| 1610 | case 0 | |
| 1611 | then show ?case by simp | |
| 1612 | next | |
| 1613 | case (pCons a p) | |
| 1614 | then show ?case by (cases p) simp | |
| 1615 | qed | |
| 52380 | 1616 | |
| 65346 | 1617 | lemma degree_synthetic_div: "degree (synthetic_div p c) = degree p - 1" | 
| 63649 | 1618 | by (induct p) (simp_all add: synthetic_div_eq_0_iff) | 
| 52380 | 1619 | |
| 1620 | lemma synthetic_div_correct: | |
| 1621 | "p + smult c (synthetic_div p c) = pCons (poly p c) (synthetic_div p c)" | |
| 1622 | by (induct p) simp_all | |
| 1623 | ||
| 65346 | 1624 | lemma synthetic_div_unique: "p + smult c q = pCons r q \<Longrightarrow> r = poly p c \<and> q = synthetic_div p c" | 
| 1625 | apply (induct p arbitrary: q r) | |
| 1626 | apply simp | |
| 1627 | apply (frule synthetic_div_unique_lemma) | |
| 1628 | apply simp | |
| 1629 | apply (case_tac q, force) | |
| 1630 | done | |
| 1631 | ||
| 1632 | lemma synthetic_div_correct': "[:-c, 1:] * synthetic_div p c + [:poly p c:] = p" | |
| 1633 | for c :: "'a::comm_ring_1" | |
| 1634 | using synthetic_div_correct [of p c] by (simp add: algebra_simps) | |
| 1635 | ||
| 1636 | ||
| 64795 | 1637 | subsubsection \<open>Polynomial roots\<close> | 
| 65346 | 1638 | |
| 1639 | lemma poly_eq_0_iff_dvd: "poly p c = 0 \<longleftrightarrow> [:- c, 1:] dvd p" | |
| 1640 | (is "?lhs \<longleftrightarrow> ?rhs") | |
| 1641 | for c :: "'a::comm_ring_1" | |
| 52380 | 1642 | proof | 
| 65346 | 1643 | assume ?lhs | 
| 1644 | with synthetic_div_correct' [of c p] have "p = [:-c, 1:] * synthetic_div p c" by simp | |
| 1645 | then show ?rhs .. | |
| 52380 | 1646 | next | 
| 65346 | 1647 | assume ?rhs | 
| 52380 | 1648 | then obtain k where "p = [:-c, 1:] * k" by (rule dvdE) | 
| 65346 | 1649 | then show ?lhs by simp | 
| 52380 | 1650 | qed | 
| 1651 | ||
| 65346 | 1652 | lemma dvd_iff_poly_eq_0: "[:c, 1:] dvd p \<longleftrightarrow> poly p (- c) = 0" | 
| 1653 | for c :: "'a::comm_ring_1" | |
| 52380 | 1654 | by (simp add: poly_eq_0_iff_dvd) | 
| 1655 | ||
| 65346 | 1656 | lemma poly_roots_finite: "p \<noteq> 0 \<Longrightarrow> finite {x. poly p x = 0}"
 | 
| 1657 |   for p :: "'a::{comm_ring_1,ring_no_zero_divisors} poly"
 | |
| 52380 | 1658 | proof (induct n \<equiv> "degree p" arbitrary: p) | 
| 65346 | 1659 | case 0 | 
| 52380 | 1660 | then obtain a where "a \<noteq> 0" and "p = [:a:]" | 
| 65346 | 1661 | by (cases p) (simp split: if_splits) | 
| 1662 |   then show "finite {x. poly p x = 0}"
 | |
| 1663 | by simp | |
| 52380 | 1664 | next | 
| 65346 | 1665 | case (Suc n) | 
| 52380 | 1666 |   show "finite {x. poly p x = 0}"
 | 
| 1667 | proof (cases "\<exists>x. poly p x = 0") | |
| 1668 | case False | |
| 1669 |     then show "finite {x. poly p x = 0}" by simp
 | |
| 1670 | next | |
| 1671 | case True | |
| 1672 | then obtain a where "poly p a = 0" .. | |
| 65346 | 1673 | then have "[:-a, 1:] dvd p" | 
| 1674 | by (simp only: poly_eq_0_iff_dvd) | |
| 52380 | 1675 | then obtain k where k: "p = [:-a, 1:] * k" .. | 
| 65346 | 1676 | with \<open>p \<noteq> 0\<close> have "k \<noteq> 0" | 
| 1677 | by auto | |
| 52380 | 1678 | with k have "degree p = Suc (degree k)" | 
| 1679 | by (simp add: degree_mult_eq del: mult_pCons_left) | |
| 65346 | 1680 | with \<open>Suc n = degree p\<close> have "n = degree k" | 
| 1681 | by simp | |
| 1682 |     from this \<open>k \<noteq> 0\<close> have "finite {x. poly k x = 0}"
 | |
| 1683 | by (rule Suc.hyps) | |
| 1684 |     then have "finite (insert a {x. poly k x = 0})"
 | |
| 1685 | by simp | |
| 52380 | 1686 |     then show "finite {x. poly p x = 0}"
 | 
| 57862 | 1687 | by (simp add: k Collect_disj_eq del: mult_pCons_left) | 
| 52380 | 1688 | qed | 
| 1689 | qed | |
| 1690 | ||
| 65346 | 1691 | lemma poly_eq_poly_eq_iff: "poly p = poly q \<longleftrightarrow> p = q" | 
| 1692 | (is "?lhs \<longleftrightarrow> ?rhs") | |
| 1693 |   for p q :: "'a::{comm_ring_1,ring_no_zero_divisors,ring_char_0} poly"
 | |
| 52380 | 1694 | proof | 
| 65346 | 1695 | assume ?rhs | 
| 1696 | then show ?lhs by simp | |
| 52380 | 1697 | next | 
| 65346 | 1698 | assume ?lhs | 
| 1699 | have "poly p = poly 0 \<longleftrightarrow> p = 0" for p :: "'a poly" | |
| 1700 | apply (cases "p = 0") | |
| 1701 | apply simp_all | |
| 1702 | apply (drule poly_roots_finite) | |
| 1703 | apply (auto simp add: infinite_UNIV_char_0) | |
| 1704 | done | |
| 1705 | from \<open>?lhs\<close> and this [of "p - q"] show ?rhs | |
| 1706 | by auto | |
| 52380 | 1707 | qed | 
| 1708 | ||
| 65346 | 1709 | lemma poly_all_0_iff_0: "(\<forall>x. poly p x = 0) \<longleftrightarrow> p = 0" | 
| 1710 |   for p :: "'a::{ring_char_0,comm_ring_1,ring_no_zero_divisors} poly"
 | |
| 52380 | 1711 | by (auto simp add: poly_eq_poly_eq_iff [symmetric]) | 
| 1712 | ||
| 65346 | 1713 | |
| 64795 | 1714 | subsubsection \<open>Order of polynomial roots\<close> | 
| 29977 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 1715 | |
| 52380 | 1716 | definition order :: "'a::idom \<Rightarrow> 'a poly \<Rightarrow> nat" | 
| 65346 | 1717 | where "order a p = (LEAST n. \<not> [:-a, 1:] ^ Suc n dvd p)" | 
| 1718 | ||
| 1719 | lemma coeff_linear_power: "coeff ([:a, 1:] ^ n) n = 1" | |
| 1720 | for a :: "'a::comm_semiring_1" | |
| 1721 | apply (induct n) | |
| 1722 | apply simp_all | |
| 1723 | apply (subst coeff_eq_0) | |
| 1724 | apply (auto intro: le_less_trans degree_power_le) | |
| 1725 | done | |
| 1726 | ||
| 1727 | lemma degree_linear_power: "degree ([:a, 1:] ^ n) = n" | |
| 1728 | for a :: "'a::comm_semiring_1" | |
| 1729 | apply (rule order_antisym) | |
| 1730 | apply (rule ord_le_eq_trans [OF degree_power_le]) | |
| 1731 | apply simp | |
| 1732 | apply (rule le_degree) | |
| 1733 | apply (simp add: coeff_linear_power) | |
| 1734 | done | |
| 29977 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 1735 | |
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 1736 | lemma order_1: "[:-a, 1:] ^ order a p dvd p" | 
| 65346 | 1737 | apply (cases "p = 0") | 
| 1738 | apply simp | |
| 1739 | apply (cases "order a p") | |
| 1740 | apply simp | |
| 1741 | apply (subgoal_tac "nat < (LEAST n. \<not> [:-a, 1:] ^ Suc n dvd p)") | |
| 1742 | apply (drule not_less_Least) | |
| 1743 | apply simp | |
| 1744 | apply (fold order_def) | |
| 1745 | apply simp | |
| 1746 | done | |
| 29977 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 1747 | |
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 1748 | lemma order_2: "p \<noteq> 0 \<Longrightarrow> \<not> [:-a, 1:] ^ Suc (order a p) dvd p" | 
| 65346 | 1749 | unfolding order_def | 
| 1750 | apply (rule LeastI_ex) | |
| 1751 | apply (rule_tac x="degree p" in exI) | |
| 1752 | apply (rule notI) | |
| 1753 | apply (drule (1) dvd_imp_degree_le) | |
| 1754 | apply (simp only: degree_linear_power) | |
| 1755 | done | |
| 1756 | ||
| 1757 | lemma order: "p \<noteq> 0 \<Longrightarrow> [:-a, 1:] ^ order a p dvd p \<and> \<not> [:-a, 1:] ^ Suc (order a p) dvd p" | |
| 1758 | by (rule conjI [OF order_1 order_2]) | |
| 29977 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 1759 | |
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 1760 | lemma order_degree: | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 1761 | assumes p: "p \<noteq> 0" | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 1762 | shows "order a p \<le> degree p" | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 1763 | proof - | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 1764 | have "order a p = degree ([:-a, 1:] ^ order a p)" | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 1765 | by (simp only: degree_linear_power) | 
| 65346 | 1766 | also from order_1 p have "\<dots> \<le> degree p" | 
| 1767 | by (rule dvd_imp_degree_le) | |
| 29977 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 1768 | finally show ?thesis . | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 1769 | qed | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 1770 | |
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 1771 | lemma order_root: "poly p a = 0 \<longleftrightarrow> p = 0 \<or> order a p \<noteq> 0" | 
| 65346 | 1772 | apply (cases "p = 0") | 
| 1773 | apply simp_all | |
| 1774 | apply (rule iffI) | |
| 1775 | apply (metis order_2 not_gr0 poly_eq_0_iff_dvd power_0 power_Suc_0 power_one_right) | |
| 1776 | unfolding poly_eq_0_iff_dvd | |
| 1777 | apply (metis dvd_power dvd_trans order_1) | |
| 1778 | done | |
| 29977 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 1779 | |
| 62065 | 1780 | lemma order_0I: "poly p a \<noteq> 0 \<Longrightarrow> order a p = 0" | 
| 1781 | by (subst (asm) order_root) auto | |
| 1782 | ||
| 64795 | 1783 | lemma order_unique_lemma: | 
| 1784 | fixes p :: "'a::idom poly" | |
| 1785 | assumes "[:-a, 1:] ^ n dvd p" "\<not> [:-a, 1:] ^ Suc n dvd p" | |
| 1786 | shows "n = order a p" | |
| 65346 | 1787 | unfolding Polynomial.order_def | 
| 1788 | apply (rule Least_equality [symmetric]) | |
| 1789 | apply (fact assms) | |
| 1790 | apply (rule classical) | |
| 1791 | apply (erule notE) | |
| 1792 | unfolding not_less_eq_eq | |
| 1793 | using assms(1) | |
| 1794 | apply (rule power_le_dvd) | |
| 1795 | apply assumption | |
| 64795 | 1796 | done | 
| 65346 | 1797 | |
| 64795 | 1798 | lemma order_mult: "p * q \<noteq> 0 \<Longrightarrow> order a (p * q) = order a p + order a q" | 
| 1799 | proof - | |
| 1800 | define i where "i = order a p" | |
| 1801 | define j where "j = order a q" | |
| 1802 | define t where "t = [:-a, 1:]" | |
| 1803 | have t_dvd_iff: "\<And>u. t dvd u \<longleftrightarrow> poly u a = 0" | |
| 65346 | 1804 | by (simp add: t_def dvd_iff_poly_eq_0) | 
| 64795 | 1805 | assume "p * q \<noteq> 0" | 
| 1806 | then show "order a (p * q) = i + j" | |
| 1807 | apply clarsimp | |
| 1808 | apply (drule order [where a=a and p=p, folded i_def t_def]) | |
| 1809 | apply (drule order [where a=a and p=q, folded j_def t_def]) | |
| 1810 | apply clarify | |
| 1811 | apply (erule dvdE)+ | |
| 1812 | apply (rule order_unique_lemma [symmetric], fold t_def) | |
| 65346 | 1813 | apply (simp_all add: power_add t_dvd_iff) | 
| 64795 | 1814 | done | 
| 1815 | qed | |
| 1816 | ||
| 1817 | lemma order_smult: | |
| 65346 | 1818 | assumes "c \<noteq> 0" | 
| 64795 | 1819 | shows "order x (smult c p) = order x p" | 
| 1820 | proof (cases "p = 0") | |
| 65346 | 1821 | case True | 
| 1822 | then show ?thesis | |
| 1823 | by simp | |
| 1824 | next | |
| 64795 | 1825 | case False | 
| 1826 | have "smult c p = [:c:] * p" by simp | |
| 65346 | 1827 | also from assms False have "order x \<dots> = order x [:c:] + order x p" | 
| 64795 | 1828 | by (subst order_mult) simp_all | 
| 65346 | 1829 | also have "order x [:c:] = 0" | 
| 1830 | by (rule order_0I) (use assms in auto) | |
| 1831 | finally show ?thesis | |
| 1832 | by simp | |
| 1833 | qed | |
| 64795 | 1834 | |
| 68532 
f8b98d31ad45
Incorporating new/strengthened proofs from Library and AFP entries
 paulson <lp15@cam.ac.uk> parents: 
67399diff
changeset | 1835 | (* Next three lemmas contributed by Wenda Li *) | 
| 65346 | 1836 | lemma order_1_eq_0 [simp]:"order x 1 = 0" | 
| 64795 | 1837 | by (metis order_root poly_1 zero_neq_one) | 
| 1838 | ||
| 68532 
f8b98d31ad45
Incorporating new/strengthened proofs from Library and AFP entries
 paulson <lp15@cam.ac.uk> parents: 
67399diff
changeset | 1839 | lemma order_uminus[simp]: "order x (-p) = order x p" | 
| 
f8b98d31ad45
Incorporating new/strengthened proofs from Library and AFP entries
 paulson <lp15@cam.ac.uk> parents: 
67399diff
changeset | 1840 | by (metis neg_equal_0_iff_equal order_smult smult_1_left smult_minus_left) | 
| 
f8b98d31ad45
Incorporating new/strengthened proofs from Library and AFP entries
 paulson <lp15@cam.ac.uk> parents: 
67399diff
changeset | 1841 | |
| 65346 | 1842 | lemma order_power_n_n: "order a ([:-a,1:]^n)=n" | 
| 64795 | 1843 | proof (induct n) (*might be proved more concisely using nat_less_induct*) | 
| 1844 | case 0 | |
| 65346 | 1845 | then show ?case | 
| 1846 | by (metis order_root poly_1 power_0 zero_neq_one) | |
| 1847 | next | |
| 64795 | 1848 | case (Suc n) | 
| 65346 | 1849 | have "order a ([:- a, 1:] ^ Suc n) = order a ([:- a, 1:] ^ n) + order a [:-a,1:]" | 
| 1850 | by (metis (no_types, hide_lams) One_nat_def add_Suc_right monoid_add_class.add.right_neutral | |
| 64795 | 1851 | one_neq_zero order_mult pCons_eq_0_iff power_add power_eq_0_iff power_one_right) | 
| 65346 | 1852 | moreover have "order a [:-a,1:] = 1" | 
| 1853 | unfolding order_def | |
| 1854 | proof (rule Least_equality, rule notI) | |
| 1855 | assume "[:- a, 1:] ^ Suc 1 dvd [:- a, 1:]" | |
| 1856 | then have "degree ([:- a, 1:] ^ Suc 1) \<le> degree ([:- a, 1:])" | |
| 1857 | by (rule dvd_imp_degree_le) auto | |
| 1858 | then show False | |
| 1859 | by auto | |
| 1860 | next | |
| 1861 | fix y | |
| 1862 | assume *: "\<not> [:- a, 1:] ^ Suc y dvd [:- a, 1:]" | |
| 1863 | show "1 \<le> y" | |
| 1864 | proof (rule ccontr) | |
| 1865 | assume "\<not> 1 \<le> y" | |
| 1866 | then have "y = 0" by auto | |
| 1867 | then have "[:- a, 1:] ^ Suc y dvd [:- a, 1:]" by auto | |
| 1868 | with * show False by auto | |
| 64795 | 1869 | qed | 
| 65346 | 1870 | qed | 
| 1871 | ultimately show ?case | |
| 1872 | using Suc by auto | |
| 64795 | 1873 | qed | 
| 1874 | ||
| 65346 | 1875 | lemma order_0_monom [simp]: "c \<noteq> 0 \<Longrightarrow> order 0 (monom c n) = n" | 
| 1876 | using order_power_n_n[of 0 n] by (simp add: monom_altdef order_smult) | |
| 1877 | ||
| 1878 | lemma dvd_imp_order_le: "q \<noteq> 0 \<Longrightarrow> p dvd q \<Longrightarrow> Polynomial.order a p \<le> Polynomial.order a q" | |
| 64795 | 1879 | by (auto simp: order_mult elim: dvdE) | 
| 1880 | ||
| 65346 | 1881 | text \<open>Now justify the standard squarefree decomposition, i.e. \<open>f / gcd f f'\<close>.\<close> | 
| 64795 | 1882 | |
| 1883 | lemma order_divides: "[:-a, 1:] ^ n dvd p \<longleftrightarrow> p = 0 \<or> n \<le> order a p" | |
| 65346 | 1884 | apply (cases "p = 0") | 
| 1885 | apply auto | |
| 1886 | apply (drule order_2 [where a=a and p=p]) | |
| 1887 | apply (metis not_less_eq_eq power_le_dvd) | |
| 1888 | apply (erule power_le_dvd [OF order_1]) | |
| 1889 | done | |
| 64795 | 1890 | |
| 1891 | lemma order_decomp: | |
| 1892 | assumes "p \<noteq> 0" | |
| 1893 | shows "\<exists>q. p = [:- a, 1:] ^ order a p * q \<and> \<not> [:- a, 1:] dvd q" | |
| 1894 | proof - | |
| 65346 | 1895 | from assms have *: "[:- a, 1:] ^ order a p dvd p" | 
| 1896 | and **: "\<not> [:- a, 1:] ^ Suc (order a p) dvd p" | |
| 1897 | by (auto dest: order) | |
| 1898 | from * obtain q where q: "p = [:- a, 1:] ^ order a p * q" .. | |
| 1899 | with ** have "\<not> [:- a, 1:] ^ Suc (order a p) dvd [:- a, 1:] ^ order a p * q" | |
| 64795 | 1900 | by simp | 
| 1901 | then have "\<not> [:- a, 1:] ^ order a p * [:- a, 1:] dvd [:- a, 1:] ^ order a p * q" | |
| 1902 | by simp | |
| 65346 | 1903 | with idom_class.dvd_mult_cancel_left [of "[:- a, 1:] ^ order a p" "[:- a, 1:]" q] | 
| 1904 | have "\<not> [:- a, 1:] dvd q" by auto | |
| 1905 | with q show ?thesis by blast | |
| 64795 | 1906 | qed | 
| 1907 | ||
| 65346 | 1908 | lemma monom_1_dvd_iff: "p \<noteq> 0 \<Longrightarrow> monom 1 n dvd p \<longleftrightarrow> n \<le> order 0 p" | 
| 1909 | using order_divides[of 0 n p] by (simp add: monom_altdef) | |
| 64795 | 1910 | |
| 29977 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 1911 | |
| 62065 | 1912 | subsection \<open>Additional induction rules on polynomials\<close> | 
| 1913 | ||
| 1914 | text \<open> | |
| 65346 | 1915 | An induction rule for induction over the roots of a polynomial with a certain property. | 
| 62065 | 1916 | (e.g. all positive roots) | 
| 1917 | \<close> | |
| 1918 | lemma poly_root_induct [case_names 0 no_roots root]: | |
| 1919 | fixes p :: "'a :: idom poly" | |
| 1920 | assumes "Q 0" | |
| 65346 | 1921 | and "\<And>p. (\<And>a. P a \<Longrightarrow> poly p a \<noteq> 0) \<Longrightarrow> Q p" | 
| 1922 | and "\<And>a p. P a \<Longrightarrow> Q p \<Longrightarrow> Q ([:a, -1:] * p)" | |
| 1923 | shows "Q p" | |
| 62065 | 1924 | proof (induction "degree p" arbitrary: p rule: less_induct) | 
| 1925 | case (less p) | |
| 1926 | show ?case | |
| 1927 | proof (cases "p = 0") | |
| 65346 | 1928 | case True | 
| 1929 | with assms(1) show ?thesis by simp | |
| 1930 | next | |
| 1931 | case False | |
| 1932 | show ?thesis | |
| 62065 | 1933 | proof (cases "\<exists>a. P a \<and> poly p a = 0") | 
| 1934 | case False | |
| 65346 | 1935 | then show ?thesis by (intro assms(2)) blast | 
| 62065 | 1936 | next | 
| 1937 | case True | |
| 65346 | 1938 | then obtain a where a: "P a" "poly p a = 0" | 
| 62065 | 1939 | by blast | 
| 65346 | 1940 | then have "-[:-a, 1:] dvd p" | 
| 62065 | 1941 | by (subst minus_dvd_iff) (simp add: poly_eq_0_iff_dvd) | 
| 1942 | then obtain q where q: "p = [:a, -1:] * q" by (elim dvdE) simp | |
| 65346 | 1943 | with False have "q \<noteq> 0" by auto | 
| 62065 | 1944 | have "degree p = Suc (degree q)" | 
| 65346 | 1945 | by (subst q, subst degree_mult_eq) (simp_all add: \<open>q \<noteq> 0\<close>) | 
| 1946 | then have "Q q" by (intro less) simp | |
| 1947 | with a(1) have "Q ([:a, -1:] * q)" | |
| 62065 | 1948 | by (rule assms(3)) | 
| 1949 | with q show ?thesis by simp | |
| 1950 | qed | |
| 65346 | 1951 | qed | 
| 62065 | 1952 | qed | 
| 1953 | ||
| 65346 | 1954 | lemma dropWhile_replicate_append: | 
| 67399 | 1955 | "dropWhile ((=) a) (replicate n a @ ys) = dropWhile ((=) a) ys" | 
| 65346 | 1956 | by (induct n) simp_all | 
| 62065 | 1957 | |
| 1958 | lemma Poly_append_replicate_0: "Poly (xs @ replicate n 0) = Poly xs" | |
| 1959 | by (subst coeffs_eq_iff) (simp_all add: strip_while_def dropWhile_replicate_append) | |
| 1960 | ||
| 1961 | text \<open> | |
| 65346 | 1962 | An induction rule for simultaneous induction over two polynomials, | 
| 62065 | 1963 | prepending one coefficient in each step. | 
| 1964 | \<close> | |
| 1965 | lemma poly_induct2 [case_names 0 pCons]: | |
| 1966 | assumes "P 0 0" "\<And>a p b q. P p q \<Longrightarrow> P (pCons a p) (pCons b q)" | |
| 65346 | 1967 | shows "P p q" | 
| 62065 | 1968 | proof - | 
| 63040 | 1969 | define n where "n = max (length (coeffs p)) (length (coeffs q))" | 
| 1970 | define xs where "xs = coeffs p @ (replicate (n - length (coeffs p)) 0)" | |
| 1971 | define ys where "ys = coeffs q @ (replicate (n - length (coeffs q)) 0)" | |
| 65346 | 1972 | have "length xs = length ys" | 
| 62065 | 1973 | by (simp add: xs_def ys_def n_def) | 
| 65346 | 1974 | then have "P (Poly xs) (Poly ys)" | 
| 1975 | by (induct rule: list_induct2) (simp_all add: assms) | |
| 1976 | also have "Poly xs = p" | |
| 62065 | 1977 | by (simp add: xs_def Poly_append_replicate_0) | 
| 65346 | 1978 | also have "Poly ys = q" | 
| 62065 | 1979 | by (simp add: ys_def Poly_append_replicate_0) | 
| 1980 | finally show ?thesis . | |
| 1981 | qed | |
| 1982 | ||
| 65346 | 1983 | |
| 60500 | 1984 | subsection \<open>Composition of polynomials\<close> | 
| 29478 | 1985 | |
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1986 | (* Several lemmas contributed by René Thiemann and Akihisa Yamada *) | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1987 | |
| 52380 | 1988 | definition pcompose :: "'a::comm_semiring_0 poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" | 
| 65346 | 1989 | where "pcompose p q = fold_coeffs (\<lambda>a c. [:a:] + q * c) p 0" | 
| 52380 | 1990 | |
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1991 | notation pcompose (infixl "\<circ>\<^sub>p" 71) | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1992 | |
| 65346 | 1993 | lemma pcompose_0 [simp]: "pcompose 0 q = 0" | 
| 52380 | 1994 | by (simp add: pcompose_def) | 
| 65346 | 1995 | |
| 1996 | lemma pcompose_pCons: "pcompose (pCons a p) q = [:a:] + q * pcompose p q" | |
| 52380 | 1997 | by (cases "p = 0 \<and> a = 0") (auto simp add: pcompose_def) | 
| 1998 | ||
| 65346 | 1999 | lemma pcompose_1: "pcompose 1 p = 1" | 
| 2000 | for p :: "'a::comm_semiring_1 poly" | |
| 65486 | 2001 | by (auto simp: one_pCons pcompose_pCons) | 
| 65346 | 2002 | |
| 2003 | lemma poly_pcompose: "poly (pcompose p q) x = poly p (poly q x)" | |
| 52380 | 2004 | by (induct p) (simp_all add: pcompose_pCons) | 
| 2005 | ||
| 65346 | 2006 | lemma degree_pcompose_le: "degree (pcompose p q) \<le> degree p * degree q" | 
| 2007 | apply (induct p) | |
| 2008 | apply simp | |
| 2009 | apply (simp add: pcompose_pCons) | |
| 2010 | apply clarify | |
| 2011 | apply (rule degree_add_le) | |
| 2012 | apply simp | |
| 2013 | apply (rule order_trans [OF degree_mult_le]) | |
| 2014 | apply simp | |
| 2015 | done | |
| 2016 | ||
| 2017 | lemma pcompose_add: "pcompose (p + q) r = pcompose p r + pcompose q r" | |
| 2018 |   for p q r :: "'a::{comm_semiring_0, ab_semigroup_add} poly"
 | |
| 62065 | 2019 | proof (induction p q rule: poly_induct2) | 
| 65346 | 2020 | case 0 | 
| 2021 | then show ?case by simp | |
| 2022 | next | |
| 62065 | 2023 | case (pCons a p b q) | 
| 65346 | 2024 | have "pcompose (pCons a p + pCons b q) r = [:a + b:] + r * pcompose p r + r * pcompose q r" | 
| 62065 | 2025 | by (simp_all add: pcompose_pCons pCons.IH algebra_simps) | 
| 2026 | also have "[:a + b:] = [:a:] + [:b:]" by simp | |
| 65346 | 2027 | also have "\<dots> + r * pcompose p r + r * pcompose q r = | 
| 2028 | pcompose (pCons a p) r + pcompose (pCons b q) r" | |
| 62065 | 2029 | by (simp only: pcompose_pCons add_ac) | 
| 2030 | finally show ?case . | |
| 65346 | 2031 | qed | 
| 2032 | ||
| 2033 | lemma pcompose_uminus: "pcompose (-p) r = -pcompose p r" | |
| 2034 | for p r :: "'a::comm_ring poly" | |
| 2035 | by (induct p) (simp_all add: pcompose_pCons) | |
| 2036 | ||
| 2037 | lemma pcompose_diff: "pcompose (p - q) r = pcompose p r - pcompose q r" | |
| 2038 | for p q r :: "'a::comm_ring poly" | |
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2039 | using pcompose_add[of p "-q"] by (simp add: pcompose_uminus) | 
| 62065 | 2040 | |
| 65346 | 2041 | lemma pcompose_smult: "pcompose (smult a p) r = smult a (pcompose p r)" | 
| 2042 | for p r :: "'a::comm_semiring_0 poly" | |
| 2043 | by (induct p) (simp_all add: pcompose_pCons pcompose_add smult_add_right) | |
| 2044 | ||
| 2045 | lemma pcompose_mult: "pcompose (p * q) r = pcompose p r * pcompose q r" | |
| 2046 | for p q r :: "'a::comm_semiring_0 poly" | |
| 2047 | by (induct p arbitrary: q) (simp_all add: pcompose_add pcompose_smult pcompose_pCons algebra_simps) | |
| 2048 | ||
| 2049 | lemma pcompose_assoc: "pcompose p (pcompose q r) = pcompose (pcompose p q) r" | |
| 2050 | for p q r :: "'a::comm_semiring_0 poly" | |
| 2051 | by (induct p arbitrary: q) (simp_all add: pcompose_pCons pcompose_add pcompose_mult) | |
| 2052 | ||
| 2053 | lemma pcompose_idR[simp]: "pcompose p [: 0, 1 :] = p" | |
| 2054 | for p :: "'a::comm_semiring_1 poly" | |
| 2055 | by (induct p) (simp_all add: pcompose_pCons) | |
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2056 | |
| 64267 | 2057 | lemma pcompose_sum: "pcompose (sum f A) p = sum (\<lambda>i. pcompose (f i) p) A" | 
| 65346 | 2058 | by (induct A rule: infinite_finite_induct) (simp_all add: pcompose_1 pcompose_add) | 
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2059 | |
| 64272 | 2060 | lemma pcompose_prod: "pcompose (prod f A) p = prod (\<lambda>i. pcompose (f i) p) A" | 
| 65346 | 2061 | by (induct A rule: infinite_finite_induct) (simp_all add: pcompose_1 pcompose_mult) | 
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2062 | |
| 64591 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 2063 | lemma pcompose_const [simp]: "pcompose [:a:] q = [:a:]" | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 2064 | by (subst pcompose_pCons) simp | 
| 62065 | 2065 | |
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2066 | lemma pcompose_0': "pcompose p 0 = [:coeff p 0:]" | 
| 64591 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64272diff
changeset | 2067 | by (induct p) (auto simp add: pcompose_pCons) | 
| 62065 | 2068 | |
| 65346 | 2069 | lemma degree_pcompose: "degree (pcompose p q) = degree p * degree q" | 
| 2070 |   for p q :: "'a::{comm_semiring_0,semiring_no_zero_divisors} poly"
 | |
| 62065 | 2071 | proof (induct p) | 
| 2072 | case 0 | |
| 65346 | 2073 | then show ?case by auto | 
| 62065 | 2074 | next | 
| 2075 | case (pCons a p) | |
| 65346 | 2076 | consider "degree (q * pcompose p q) = 0" | "degree (q * pcompose p q) > 0" | 
| 2077 | by blast | |
| 2078 | then show ?case | |
| 2079 | proof cases | |
| 2080 | case prems: 1 | |
| 2081 | show ?thesis | |
| 2082 | proof (cases "p = 0") | |
| 62065 | 2083 | case True | 
| 65346 | 2084 | then show ?thesis by auto | 
| 62065 | 2085 | next | 
| 65346 | 2086 | case False | 
| 2087 | from prems have "degree q = 0 \<or> pcompose p q = 0" | |
| 2088 | by (auto simp add: degree_mult_eq_0) | |
| 2089 | moreover have False if "pcompose p q = 0" "degree q \<noteq> 0" | |
| 2090 | proof - | |
| 2091 | from pCons.hyps(2) that have "degree p = 0" | |
| 2092 | by auto | |
| 2093 | then obtain a1 where "p = [:a1:]" | |
| 2094 | by (metis degree_pCons_eq_if old.nat.distinct(2) pCons_cases) | |
| 2095 | with \<open>pcompose p q = 0\<close> \<open>p \<noteq> 0\<close> show False | |
| 2096 | by auto | |
| 2097 | qed | |
| 2098 | ultimately have "degree (pCons a p) * degree q = 0" | |
| 2099 | by auto | |
| 2100 | moreover have "degree (pcompose (pCons a p) q) = 0" | |
| 2101 | proof - | |
| 2102 | from prems have "0 = max (degree [:a:]) (degree (q * pcompose p q))" | |
| 2103 | by simp | |
| 2104 | also have "\<dots> \<ge> degree ([:a:] + q * pcompose p q)" | |
| 2105 | by (rule degree_add_le_max) | |
| 2106 | finally show ?thesis | |
| 2107 | by (auto simp add: pcompose_pCons) | |
| 2108 | qed | |
| 62065 | 2109 | ultimately show ?thesis by simp | 
| 2110 | qed | |
| 65346 | 2111 | next | 
| 2112 | case prems: 2 | |
| 2113 | then have "p \<noteq> 0" "q \<noteq> 0" "pcompose p q \<noteq> 0" | |
| 2114 | by auto | |
| 2115 | from prems degree_add_eq_right [of "[:a:]"] | |
| 2116 | have "degree (pcompose (pCons a p) q) = degree (q * pcompose p q)" | |
| 2117 | by (auto simp: pcompose_pCons) | |
| 2118 | with pCons.hyps(2) degree_mult_eq[OF \<open>q\<noteq>0\<close> \<open>pcompose p q\<noteq>0\<close>] show ?thesis | |
| 2119 | by auto | |
| 2120 | qed | |
| 62065 | 2121 | qed | 
| 2122 | ||
| 2123 | lemma pcompose_eq_0: | |
| 65346 | 2124 |   fixes p q :: "'a::{comm_semiring_0,semiring_no_zero_divisors} poly"
 | 
| 2125 | assumes "pcompose p q = 0" "degree q > 0" | |
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2126 | shows "p = 0" | 
| 62065 | 2127 | proof - | 
| 65346 | 2128 | from assms degree_pcompose [of p q] have "degree p = 0" | 
| 2129 | by auto | |
| 2130 | then obtain a where "p = [:a:]" | |
| 62065 | 2131 | by (metis degree_pCons_eq_if gr0_conv_Suc neq0_conv pCons_cases) | 
| 65346 | 2132 | with assms(1) have "a = 0" | 
| 2133 | by auto | |
| 2134 | with \<open>p = [:a:]\<close> show ?thesis | |
| 2135 | by simp | |
| 62065 | 2136 | qed | 
| 2137 | ||
| 2138 | lemma lead_coeff_comp: | |
| 65346 | 2139 |   fixes p q :: "'a::{comm_semiring_1,semiring_no_zero_divisors} poly"
 | 
| 2140 | assumes "degree q > 0" | |
| 62065 | 2141 | shows "lead_coeff (pcompose p q) = lead_coeff p * lead_coeff q ^ (degree p)" | 
| 2142 | proof (induct p) | |
| 2143 | case 0 | |
| 65346 | 2144 | then show ?case by auto | 
| 62065 | 2145 | next | 
| 2146 | case (pCons a p) | |
| 65346 | 2147 | consider "degree (q * pcompose p q) = 0" | "degree (q * pcompose p q) > 0" | 
| 2148 | by blast | |
| 2149 | then show ?case | |
| 2150 | proof cases | |
| 2151 | case prems: 1 | |
| 2152 | then have "pcompose p q = 0" | |
| 2153 | by (metis assms degree_0 degree_mult_eq_0 neq0_conv) | |
| 2154 | with pcompose_eq_0[OF _ \<open>degree q > 0\<close>] have "p = 0" | |
| 2155 | by simp | |
| 2156 | then show ?thesis | |
| 2157 | by auto | |
| 2158 | next | |
| 2159 | case prems: 2 | |
| 2160 | then have "degree [:a:] < degree (q * pcompose p q)" | |
| 2161 | by simp | |
| 2162 | then have "lead_coeff ([:a:] + q * p \<circ>\<^sub>p q) = lead_coeff (q * p \<circ>\<^sub>p q)" | |
| 2163 | by (rule lead_coeff_add_le) | |
| 2164 | then have "lead_coeff (pcompose (pCons a p) q) = lead_coeff (q * pcompose p q)" | |
| 2165 | by (simp add: pcompose_pCons) | |
| 2166 | also have "\<dots> = lead_coeff q * (lead_coeff p * lead_coeff q ^ degree p)" | |
| 2167 | using pCons.hyps(2) lead_coeff_mult[of q "pcompose p q"] by simp | |
| 2168 | also have "\<dots> = lead_coeff p * lead_coeff q ^ (degree p + 1)" | |
| 2169 | by (auto simp: mult_ac) | |
| 2170 | finally show ?thesis by auto | |
| 2171 | qed | |
| 62065 | 2172 | qed | 
| 2173 | ||
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2174 | |
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2175 | subsection \<open>Shifting polynomials\<close> | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2176 | |
| 65346 | 2177 | definition poly_shift :: "nat \<Rightarrow> 'a::zero poly \<Rightarrow> 'a poly" | 
| 2178 | where "poly_shift n p = Abs_poly (\<lambda>i. coeff p (i + n))" | |
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2179 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2180 | lemma nth_default_drop: "nth_default x (drop n xs) m = nth_default x xs (m + n)" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2181 | by (auto simp add: nth_default_def add_ac) | 
| 65346 | 2182 | |
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2183 | lemma nth_default_take: "nth_default x (take n xs) m = (if m < n then nth_default x xs m else x)" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2184 | by (auto simp add: nth_default_def add_ac) | 
| 65346 | 2185 | |
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2186 | lemma coeff_poly_shift: "coeff (poly_shift n p) i = coeff p (i + n)" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2187 | proof - | 
| 65346 | 2188 | from MOST_coeff_eq_0[of p] obtain m where "\<forall>k>m. coeff p k = 0" | 
| 2189 | by (auto simp: MOST_nat) | |
| 2190 | then have "\<forall>k>m. coeff p (k + n) = 0" | |
| 2191 | by auto | |
| 2192 | then have "\<forall>\<^sub>\<infinity>k. coeff p (k + n) = 0" | |
| 2193 | by (auto simp: MOST_nat) | |
| 2194 | then show ?thesis | |
| 2195 | by (simp add: poly_shift_def poly.Abs_poly_inverse) | |
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2196 | qed | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2197 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2198 | lemma poly_shift_id [simp]: "poly_shift 0 = (\<lambda>x. x)" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2199 | by (simp add: poly_eq_iff fun_eq_iff coeff_poly_shift) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2200 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2201 | lemma poly_shift_0 [simp]: "poly_shift n 0 = 0" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2202 | by (simp add: poly_eq_iff coeff_poly_shift) | 
| 65346 | 2203 | |
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2204 | lemma poly_shift_1: "poly_shift n 1 = (if n = 0 then 1 else 0)" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2205 | by (simp add: poly_eq_iff coeff_poly_shift) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2206 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2207 | lemma poly_shift_monom: "poly_shift n (monom c m) = (if m \<ge> n then monom c (m - n) else 0)" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2208 | by (auto simp add: poly_eq_iff coeff_poly_shift) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2209 | |
| 65390 | 2210 | lemma coeffs_shift_poly [code abstract]: | 
| 2211 | "coeffs (poly_shift n p) = drop n (coeffs p)" | |
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2212 | proof (cases "p = 0") | 
| 65346 | 2213 | case True | 
| 2214 | then show ?thesis by simp | |
| 2215 | next | |
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2216 | case False | 
| 65346 | 2217 | then show ?thesis | 
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2218 | by (intro coeffs_eqI) | 
| 65390 | 2219 | (simp_all add: coeff_poly_shift nth_default_drop nth_default_coeffs_eq) | 
| 65346 | 2220 | qed | 
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2221 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2222 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2223 | subsection \<open>Truncating polynomials\<close> | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2224 | |
| 65346 | 2225 | definition poly_cutoff | 
| 2226 | where "poly_cutoff n p = Abs_poly (\<lambda>k. if k < n then coeff p k else 0)" | |
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2227 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2228 | lemma coeff_poly_cutoff: "coeff (poly_cutoff n p) k = (if k < n then coeff p k else 0)" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2229 | unfolding poly_cutoff_def | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2230 | by (subst poly.Abs_poly_inverse) (auto simp: MOST_nat intro: exI[of _ n]) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2231 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2232 | lemma poly_cutoff_0 [simp]: "poly_cutoff n 0 = 0" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2233 | by (simp add: poly_eq_iff coeff_poly_cutoff) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2234 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2235 | lemma poly_cutoff_1 [simp]: "poly_cutoff n 1 = (if n = 0 then 0 else 1)" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2236 | by (simp add: poly_eq_iff coeff_poly_cutoff) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2237 | |
| 65346 | 2238 | lemma coeffs_poly_cutoff [code abstract]: | 
| 67399 | 2239 | "coeffs (poly_cutoff n p) = strip_while ((=) 0) (take n (coeffs p))" | 
| 2240 | proof (cases "strip_while ((=) 0) (take n (coeffs p)) = []") | |
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2241 | case True | 
| 65346 | 2242 | then have "coeff (poly_cutoff n p) k = 0" for k | 
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2243 | unfolding coeff_poly_cutoff | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2244 | by (auto simp: nth_default_coeffs_eq [symmetric] nth_default_def set_conv_nth) | 
| 65346 | 2245 | then have "poly_cutoff n p = 0" | 
| 2246 | by (simp add: poly_eq_iff) | |
| 2247 | then show ?thesis | |
| 2248 | by (subst True) simp_all | |
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2249 | next | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2250 | case False | 
| 67399 | 2251 | have "no_trailing ((=) 0) (strip_while ((=) 0) (take n (coeffs p)))" | 
| 65346 | 2252 | by simp | 
| 67399 | 2253 | with False have "last (strip_while ((=) 0) (take n (coeffs p))) \<noteq> 0" | 
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2254 | unfolding no_trailing_unfold by auto | 
| 65346 | 2255 | then show ?thesis | 
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2256 | by (intro coeffs_eqI) | 
| 65390 | 2257 | (simp_all add: coeff_poly_cutoff nth_default_take nth_default_coeffs_eq) | 
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2258 | qed | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2259 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2260 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2261 | subsection \<open>Reflecting polynomials\<close> | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2262 | |
| 65346 | 2263 | definition reflect_poly :: "'a::zero poly \<Rightarrow> 'a poly" | 
| 2264 | where "reflect_poly p = Poly (rev (coeffs p))" | |
| 2265 | ||
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2266 | lemma coeffs_reflect_poly [code abstract]: | 
| 67399 | 2267 | "coeffs (reflect_poly p) = rev (dropWhile ((=) 0) (coeffs p))" | 
| 65346 | 2268 | by (simp add: reflect_poly_def) | 
| 2269 | ||
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2270 | lemma reflect_poly_0 [simp]: "reflect_poly 0 = 0" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2271 | by (simp add: reflect_poly_def) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2272 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2273 | lemma reflect_poly_1 [simp]: "reflect_poly 1 = 1" | 
| 65486 | 2274 | by (simp add: reflect_poly_def one_pCons) | 
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2275 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2276 | lemma coeff_reflect_poly: | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2277 | "coeff (reflect_poly p) n = (if n > degree p then 0 else coeff p (degree p - n))" | 
| 65346 | 2278 | by (cases "p = 0") | 
| 2279 | (auto simp add: reflect_poly_def nth_default_def | |
| 2280 | rev_nth degree_eq_length_coeffs coeffs_nth not_less | |
| 2281 | dest: le_imp_less_Suc) | |
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2282 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2283 | lemma coeff_0_reflect_poly_0_iff [simp]: "coeff (reflect_poly p) 0 = 0 \<longleftrightarrow> p = 0" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2284 | by (simp add: coeff_reflect_poly) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2285 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2286 | lemma reflect_poly_at_0_eq_0_iff [simp]: "poly (reflect_poly p) 0 = 0 \<longleftrightarrow> p = 0" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2287 | by (simp add: coeff_reflect_poly poly_0_coeff_0) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2288 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2289 | lemma reflect_poly_pCons': | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2290 | "p \<noteq> 0 \<Longrightarrow> reflect_poly (pCons c p) = reflect_poly p + monom c (Suc (degree p))" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2291 | by (intro poly_eqI) | 
| 65346 | 2292 | (auto simp: coeff_reflect_poly coeff_pCons not_less Suc_diff_le split: nat.split) | 
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2293 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2294 | lemma reflect_poly_const [simp]: "reflect_poly [:a:] = [:a:]" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2295 | by (cases "a = 0") (simp_all add: reflect_poly_def) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2296 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2297 | lemma poly_reflect_poly_nz: | 
| 65346 | 2298 | "x \<noteq> 0 \<Longrightarrow> poly (reflect_poly p) x = x ^ degree p * poly p (inverse x)" | 
| 2299 | for x :: "'a::field" | |
| 2300 | by (induct rule: pCons_induct) (simp_all add: field_simps reflect_poly_pCons' poly_monom) | |
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2301 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2302 | lemma coeff_0_reflect_poly [simp]: "coeff (reflect_poly p) 0 = lead_coeff p" | 
| 64794 | 2303 | by (simp add: coeff_reflect_poly) | 
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2304 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2305 | lemma poly_reflect_poly_0 [simp]: "poly (reflect_poly p) 0 = lead_coeff p" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2306 | by (simp add: poly_0_coeff_0) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2307 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2308 | lemma reflect_poly_reflect_poly [simp]: "coeff p 0 \<noteq> 0 \<Longrightarrow> reflect_poly (reflect_poly p) = p" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2309 | by (cases p rule: pCons_cases) (simp add: reflect_poly_def ) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2310 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2311 | lemma degree_reflect_poly_le: "degree (reflect_poly p) \<le> degree p" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2312 | by (simp add: degree_eq_length_coeffs coeffs_reflect_poly length_dropWhile_le diff_le_mono) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2313 | |
| 65346 | 2314 | lemma reflect_poly_pCons: "a \<noteq> 0 \<Longrightarrow> reflect_poly (pCons a p) = Poly (rev (a # coeffs p))" | 
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2315 | by (subst coeffs_eq_iff) (simp add: coeffs_reflect_poly) | 
| 65346 | 2316 | |
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2317 | lemma degree_reflect_poly_eq [simp]: "coeff p 0 \<noteq> 0 \<Longrightarrow> degree (reflect_poly p) = degree p" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2318 | by (cases p rule: pCons_cases) (simp add: reflect_poly_pCons degree_eq_length_coeffs) | 
| 65346 | 2319 | |
| 63498 | 2320 | (* TODO: does this work with zero divisors as well? Probably not. *) | 
| 65346 | 2321 | lemma reflect_poly_mult: "reflect_poly (p * q) = reflect_poly p * reflect_poly q" | 
| 2322 |   for p q :: "'a::{comm_semiring_0,semiring_no_zero_divisors} poly"
 | |
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2323 | proof (cases "p = 0 \<or> q = 0") | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2324 | case False | 
| 65346 | 2325 | then have [simp]: "p \<noteq> 0" "q \<noteq> 0" by auto | 
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2326 | show ?thesis | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2327 | proof (rule poly_eqI) | 
| 65346 | 2328 | show "coeff (reflect_poly (p * q)) i = coeff (reflect_poly p * reflect_poly q) i" for i | 
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2329 | proof (cases "i \<le> degree (p * q)") | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2330 | case True | 
| 64811 | 2331 |       define A where "A = {..i} \<inter> {i - degree q..degree p}"
 | 
| 2332 |       define B where "B = {..degree p} \<inter> {degree p - i..degree (p*q) - i}"
 | |
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2333 | let ?f = "\<lambda>j. degree p - j" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2334 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2335 | from True have "coeff (reflect_poly (p * q)) i = coeff (p * q) (degree (p * q) - i)" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2336 | by (simp add: coeff_reflect_poly) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2337 | also have "\<dots> = (\<Sum>j\<le>degree (p * q) - i. coeff p j * coeff q (degree (p * q) - i - j))" | 
| 65346 | 2338 | by (simp add: coeff_mult) | 
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2339 | also have "\<dots> = (\<Sum>j\<in>B. coeff p j * coeff q (degree (p * q) - i - j))" | 
| 64267 | 2340 | by (intro sum.mono_neutral_right) (auto simp: B_def degree_mult_eq not_le coeff_eq_0) | 
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2341 | also from True have "\<dots> = (\<Sum>j\<in>A. coeff p (degree p - j) * coeff q (degree q - (i - j)))" | 
| 64267 | 2342 | by (intro sum.reindex_bij_witness[of _ ?f ?f]) | 
| 65346 | 2343 | (auto simp: A_def B_def degree_mult_eq add_ac) | 
| 2344 | also have "\<dots> = | |
| 2345 | (\<Sum>j\<le>i. | |
| 2346 |           if j \<in> {i - degree q..degree p}
 | |
| 2347 | then coeff p (degree p - j) * coeff q (degree q - (i - j)) | |
| 2348 | else 0)" | |
| 64267 | 2349 | by (subst sum.inter_restrict [symmetric]) (simp_all add: A_def) | 
| 65346 | 2350 | also have "\<dots> = coeff (reflect_poly p * reflect_poly q) i" | 
| 2351 | by (fastforce simp: coeff_mult coeff_reflect_poly intro!: sum.cong) | |
| 2352 | finally show ?thesis . | |
| 64267 | 2353 | qed (auto simp: coeff_mult coeff_reflect_poly coeff_eq_0 degree_mult_eq intro!: sum.neutral) | 
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2354 | qed | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2355 | qed auto | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2356 | |
| 65346 | 2357 | lemma reflect_poly_smult: "reflect_poly (smult c p) = smult c (reflect_poly p)" | 
| 2358 |   for p :: "'a::{comm_semiring_0,semiring_no_zero_divisors} poly"
 | |
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2359 | using reflect_poly_mult[of "[:c:]" p] by simp | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2360 | |
| 65346 | 2361 | lemma reflect_poly_power: "reflect_poly (p ^ n) = reflect_poly p ^ n" | 
| 2362 |   for p :: "'a::{comm_semiring_1,semiring_no_zero_divisors} poly"
 | |
| 2363 | by (induct n) (simp_all add: reflect_poly_mult) | |
| 2364 | ||
| 2365 | lemma reflect_poly_prod: "reflect_poly (prod f A) = prod (\<lambda>x. reflect_poly (f x)) A" | |
| 2366 |   for f :: "_ \<Rightarrow> _::{comm_semiring_0,semiring_no_zero_divisors} poly"
 | |
| 2367 | by (induct A rule: infinite_finite_induct) (simp_all add: reflect_poly_mult) | |
| 2368 | ||
| 2369 | lemma reflect_poly_prod_list: "reflect_poly (prod_list xs) = prod_list (map reflect_poly xs)" | |
| 2370 |   for xs :: "_::{comm_semiring_0,semiring_no_zero_divisors} poly list"
 | |
| 2371 | by (induct xs) (simp_all add: reflect_poly_mult) | |
| 2372 | ||
| 65390 | 2373 | lemma reflect_poly_Poly_nz: | 
| 2374 | "no_trailing (HOL.eq 0) xs \<Longrightarrow> reflect_poly (Poly xs) = Poly (rev xs)" | |
| 65346 | 2375 | by (simp add: reflect_poly_def) | 
| 2376 | ||
| 2377 | lemmas reflect_poly_simps = | |
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2378 | reflect_poly_0 reflect_poly_1 reflect_poly_const reflect_poly_smult reflect_poly_mult | 
| 64272 | 2379 | reflect_poly_power reflect_poly_prod reflect_poly_prod_list | 
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2380 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2381 | |
| 64795 | 2382 | subsection \<open>Derivatives\<close> | 
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2383 | |
| 63498 | 2384 | function pderiv :: "('a :: {comm_semiring_1,semiring_no_zero_divisors}) poly \<Rightarrow> 'a poly"
 | 
| 65346 | 2385 | where "pderiv (pCons a p) = (if p = 0 then 0 else p + pCons 0 (pderiv p))" | 
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2386 | by (auto intro: pCons_cases) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2387 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2388 | termination pderiv | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2389 | by (relation "measure degree") simp_all | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2390 | |
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2391 | declare pderiv.simps[simp del] | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2392 | |
| 65346 | 2393 | lemma pderiv_0 [simp]: "pderiv 0 = 0" | 
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2394 | using pderiv.simps [of 0 0] by simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2395 | |
| 65346 | 2396 | lemma pderiv_pCons: "pderiv (pCons a p) = p + pCons 0 (pderiv p)" | 
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2397 | by (simp add: pderiv.simps) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2398 | |
| 65346 | 2399 | lemma pderiv_1 [simp]: "pderiv 1 = 0" | 
| 65486 | 2400 | by (simp add: one_pCons pderiv_pCons) | 
| 65346 | 2401 | |
| 2402 | lemma pderiv_of_nat [simp]: "pderiv (of_nat n) = 0" | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2403 | and pderiv_numeral [simp]: "pderiv (numeral m) = 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2404 | by (simp_all add: of_nat_poly numeral_poly pderiv_pCons) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2405 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2406 | lemma coeff_pderiv: "coeff (pderiv p) n = of_nat (Suc n) * coeff p (Suc n)" | 
| 65346 | 2407 | by (induct p arbitrary: n) | 
| 2408 | (auto simp add: pderiv_pCons coeff_pCons algebra_simps split: nat.split) | |
| 2409 | ||
| 2410 | fun pderiv_coeffs_code :: "'a::{comm_semiring_1,semiring_no_zero_divisors} \<Rightarrow> 'a list \<Rightarrow> 'a list"
 | |
| 2411 | where | |
| 2412 | "pderiv_coeffs_code f (x # xs) = cCons (f * x) (pderiv_coeffs_code (f+1) xs)" | |
| 2413 | | "pderiv_coeffs_code f [] = []" | |
| 2414 | ||
| 2415 | definition pderiv_coeffs :: "'a::{comm_semiring_1,semiring_no_zero_divisors} list \<Rightarrow> 'a list"
 | |
| 2416 | where "pderiv_coeffs xs = pderiv_coeffs_code 1 (tl xs)" | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2417 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2418 | (* Efficient code for pderiv contributed by René Thiemann and Akihisa Yamada *) | 
| 65346 | 2419 | lemma pderiv_coeffs_code: | 
| 2420 | "nth_default 0 (pderiv_coeffs_code f xs) n = (f + of_nat n) * nth_default 0 xs n" | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2421 | proof (induct xs arbitrary: f n) | 
| 65346 | 2422 | case Nil | 
| 2423 | then show ?case by simp | |
| 2424 | next | |
| 2425 | case (Cons x xs) | |
| 2426 | show ?case | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2427 | proof (cases n) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2428 | case 0 | 
| 65346 | 2429 | then show ?thesis | 
| 2430 | by (cases "pderiv_coeffs_code (f + 1) xs = [] \<and> f * x = 0") (auto simp: cCons_def) | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2431 | next | 
| 65346 | 2432 | case n: (Suc m) | 
| 2433 | show ?thesis | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2434 | proof (cases "pderiv_coeffs_code (f + 1) xs = [] \<and> f * x = 0") | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2435 | case False | 
| 65346 | 2436 | then have "nth_default 0 (pderiv_coeffs_code f (x # xs)) n = | 
| 2437 | nth_default 0 (pderiv_coeffs_code (f + 1) xs) m" | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2438 | by (auto simp: cCons_def n) | 
| 65346 | 2439 | also have "\<dots> = (f + of_nat n) * nth_default 0 xs m" | 
| 2440 | by (simp add: Cons n add_ac) | |
| 2441 | finally show ?thesis | |
| 2442 | by (simp add: n) | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2443 | next | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2444 | case True | 
| 65346 | 2445 | have empty: "pderiv_coeffs_code g xs = [] \<Longrightarrow> g + of_nat m = 0 \<or> nth_default 0 xs m = 0" for g | 
| 2446 | proof (induct xs arbitrary: g m) | |
| 2447 | case Nil | |
| 2448 | then show ?case by simp | |
| 2449 | next | |
| 2450 | case (Cons x xs) | |
| 2451 | from Cons(2) have empty: "pderiv_coeffs_code (g + 1) xs = []" and g: "g = 0 \<or> x = 0" | |
| 2452 | by (auto simp: cCons_def split: if_splits) | |
| 2453 | note IH = Cons(1)[OF empty] | |
| 2454 | from IH[of m] IH[of "m - 1"] g show ?case | |
| 2455 | by (cases m) (auto simp: field_simps) | |
| 2456 | qed | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2457 | from True have "nth_default 0 (pderiv_coeffs_code f (x # xs)) n = 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2458 | by (auto simp: cCons_def n) | 
| 65346 | 2459 | moreover from True have "(f + of_nat n) * nth_default 0 (x # xs) n = 0" | 
| 2460 | by (simp add: n) (use empty[of "f+1"] in \<open>auto simp: field_simps\<close>) | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2461 | ultimately show ?thesis by simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2462 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2463 | qed | 
| 65346 | 2464 | qed | 
| 2465 | ||
| 2466 | lemma coeffs_pderiv_code [code abstract]: "coeffs (pderiv p) = pderiv_coeffs (coeffs p)" | |
| 2467 | unfolding pderiv_coeffs_def | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2468 | proof (rule coeffs_eqI, unfold pderiv_coeffs_code coeff_pderiv, goal_cases) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2469 | case (1 n) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2470 | have id: "coeff p (Suc n) = nth_default 0 (map (\<lambda>i. coeff p (Suc i)) [0..<degree p]) n" | 
| 65346 | 2471 | by (cases "n < degree p") (auto simp: nth_default_def coeff_eq_0) | 
| 2472 | show ?case | |
| 2473 | unfolding coeffs_def map_upt_Suc by (auto simp: id) | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2474 | next | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2475 | case 2 | 
| 65346 | 2476 | obtain n :: 'a and xs where defs: "tl (coeffs p) = xs" "1 = n" | 
| 2477 | by simp | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2478 | from 2 show ?case | 
| 65346 | 2479 | unfolding defs by (induct xs arbitrary: n) (auto simp: cCons_def) | 
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2480 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2481 | |
| 65346 | 2482 | lemma pderiv_eq_0_iff: "pderiv p = 0 \<longleftrightarrow> degree p = 0" | 
| 2483 |   for p :: "'a::{comm_semiring_1,semiring_no_zero_divisors,semiring_char_0} poly"
 | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2484 | apply (rule iffI) | 
| 65346 | 2485 | apply (cases p) | 
| 2486 | apply simp | |
| 2487 | apply (simp add: poly_eq_iff coeff_pderiv del: of_nat_Suc) | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2488 | apply (simp add: poly_eq_iff coeff_pderiv coeff_eq_0) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2489 | done | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2490 | |
| 65346 | 2491 | lemma degree_pderiv: "degree (pderiv p) = degree p - 1" | 
| 2492 |   for p :: "'a::{comm_semiring_1,semiring_no_zero_divisors,semiring_char_0} poly"
 | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2493 | apply (rule order_antisym [OF degree_le]) | 
| 65346 | 2494 | apply (simp add: coeff_pderiv coeff_eq_0) | 
| 2495 | apply (cases "degree p") | |
| 2496 | apply simp | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2497 | apply (rule le_degree) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2498 | apply (simp add: coeff_pderiv del: of_nat_Suc) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2499 | apply (metis degree_0 leading_coeff_0_iff nat.distinct(1)) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2500 | done | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2501 | |
| 65346 | 2502 | lemma not_dvd_pderiv: | 
| 2503 |   fixes p :: "'a::{comm_semiring_1,semiring_no_zero_divisors,semiring_char_0} poly"
 | |
| 2504 | assumes "degree p \<noteq> 0" | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2505 | shows "\<not> p dvd pderiv p" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2506 | proof | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2507 | assume dvd: "p dvd pderiv p" | 
| 65346 | 2508 | then obtain q where p: "pderiv p = p * q" | 
| 2509 | unfolding dvd_def by auto | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2510 | from dvd have le: "degree p \<le> degree (pderiv p)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2511 | by (simp add: assms dvd_imp_degree_le pderiv_eq_0_iff) | 
| 65346 | 2512 | from assms and this [unfolded degree_pderiv] | 
| 2513 | show False by auto | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2514 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2515 | |
| 65346 | 2516 | lemma dvd_pderiv_iff [simp]: "p dvd pderiv p \<longleftrightarrow> degree p = 0" | 
| 2517 |   for p :: "'a::{comm_semiring_1,semiring_no_zero_divisors,semiring_char_0} poly"
 | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2518 | using not_dvd_pderiv[of p] by (auto simp: pderiv_eq_0_iff [symmetric]) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2519 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2520 | lemma pderiv_singleton [simp]: "pderiv [:a:] = 0" | 
| 65346 | 2521 | by (simp add: pderiv_pCons) | 
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2522 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2523 | lemma pderiv_add: "pderiv (p + q) = pderiv p + pderiv q" | 
| 65346 | 2524 | by (rule poly_eqI) (simp add: coeff_pderiv algebra_simps) | 
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2525 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2526 | lemma pderiv_minus: "pderiv (- p :: 'a :: idom poly) = - pderiv p" | 
| 65346 | 2527 | by (rule poly_eqI) (simp add: coeff_pderiv algebra_simps) | 
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2528 | |
| 63498 | 2529 | lemma pderiv_diff: "pderiv ((p :: _ :: idom poly) - q) = pderiv p - pderiv q" | 
| 65346 | 2530 | by (rule poly_eqI) (simp add: coeff_pderiv algebra_simps) | 
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2531 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2532 | lemma pderiv_smult: "pderiv (smult a p) = smult a (pderiv p)" | 
| 65346 | 2533 | by (rule poly_eqI) (simp add: coeff_pderiv algebra_simps) | 
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2534 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2535 | lemma pderiv_mult: "pderiv (p * q) = p * pderiv q + q * pderiv p" | 
| 65346 | 2536 | by (induct p) (auto simp: pderiv_add pderiv_smult pderiv_pCons algebra_simps) | 
| 2537 | ||
| 2538 | lemma pderiv_power_Suc: "pderiv (p ^ Suc n) = smult (of_nat (Suc n)) (p ^ n) * pderiv p" | |
| 2539 | apply (induct n) | |
| 2540 | apply simp | |
| 2541 | apply (subst power_Suc) | |
| 2542 | apply (subst pderiv_mult) | |
| 2543 | apply (erule ssubst) | |
| 2544 | apply (simp only: of_nat_Suc smult_add_left smult_1_left) | |
| 2545 | apply (simp add: algebra_simps) | |
| 2546 | done | |
| 2547 | ||
| 66550 
e5d82cf3c387
Some small lemmas about polynomials and FPSs
 eberlm <eberlm@in.tum.de> parents: 
66453diff
changeset | 2548 | lemma pderiv_pcompose: "pderiv (pcompose p q) = pcompose (pderiv p) q * pderiv q" | 
| 
e5d82cf3c387
Some small lemmas about polynomials and FPSs
 eberlm <eberlm@in.tum.de> parents: 
66453diff
changeset | 2549 | by (induction p rule: pCons_induct) | 
| 
e5d82cf3c387
Some small lemmas about polynomials and FPSs
 eberlm <eberlm@in.tum.de> parents: 
66453diff
changeset | 2550 | (auto simp: pcompose_pCons pderiv_add pderiv_mult pderiv_pCons pcompose_add algebra_simps) | 
| 
e5d82cf3c387
Some small lemmas about polynomials and FPSs
 eberlm <eberlm@in.tum.de> parents: 
66453diff
changeset | 2551 | |
| 65346 | 2552 | lemma pderiv_prod: "pderiv (prod f (as)) = (\<Sum>a\<in>as. prod f (as - {a}) * pderiv (f a))"
 | 
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2553 | proof (induct as rule: infinite_finite_induct) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2554 | case (insert a as) | 
| 65346 | 2555 | then have id: "prod f (insert a as) = f a * prod f as" | 
| 2556 | "\<And>g. sum g (insert a as) = g a + sum g as" | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2557 |     "insert a as - {a} = as"
 | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2558 | by auto | 
| 65346 | 2559 |   have "prod f (insert a as - {b}) = f a * prod f (as - {b})" if "b \<in> as" for b
 | 
| 2560 | proof - | |
| 2561 |     from \<open>a \<notin> as\<close> that have *: "insert a as - {b} = insert a (as - {b})"
 | |
| 2562 | by auto | |
| 2563 | show ?thesis | |
| 2564 | unfolding * by (subst prod.insert) (use insert in auto) | |
| 2565 | qed | |
| 2566 | then show ?case | |
| 64267 | 2567 | unfolding id pderiv_mult insert(3) sum_distrib_left | 
| 65346 | 2568 | by (auto simp add: ac_simps intro!: sum.cong) | 
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2569 | qed auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2570 | |
| 65346 | 2571 | lemma DERIV_pow2: "DERIV (\<lambda>x. x ^ Suc n) x :> real (Suc n) * (x ^ n)" | 
| 2572 | by (rule DERIV_cong, rule DERIV_pow) simp | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2573 | declare DERIV_pow2 [simp] DERIV_pow [simp] | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2574 | |
| 65346 | 2575 | lemma DERIV_add_const: "DERIV f x :> D \<Longrightarrow> DERIV (\<lambda>x. a + f x :: 'a::real_normed_field) x :> D" | 
| 2576 | by (rule DERIV_cong, rule DERIV_add) auto | |
| 2577 | ||
| 2578 | lemma poly_DERIV [simp]: "DERIV (\<lambda>x. poly p x) x :> poly (pderiv p) x" | |
| 2579 | by (induct p) (auto intro!: derivative_eq_intros simp add: pderiv_pCons) | |
| 2580 | ||
| 68532 
f8b98d31ad45
Incorporating new/strengthened proofs from Library and AFP entries
 paulson <lp15@cam.ac.uk> parents: 
67399diff
changeset | 2581 | lemma poly_isCont[simp]: | 
| 
f8b98d31ad45
Incorporating new/strengthened proofs from Library and AFP entries
 paulson <lp15@cam.ac.uk> parents: 
67399diff
changeset | 2582 | fixes x::"'a::real_normed_field" | 
| 
f8b98d31ad45
Incorporating new/strengthened proofs from Library and AFP entries
 paulson <lp15@cam.ac.uk> parents: 
67399diff
changeset | 2583 | shows "isCont (\<lambda>x. poly p x) x" | 
| 
f8b98d31ad45
Incorporating new/strengthened proofs from Library and AFP entries
 paulson <lp15@cam.ac.uk> parents: 
67399diff
changeset | 2584 | by (rule poly_DERIV [THEN DERIV_isCont]) | 
| 
f8b98d31ad45
Incorporating new/strengthened proofs from Library and AFP entries
 paulson <lp15@cam.ac.uk> parents: 
67399diff
changeset | 2585 | |
| 
f8b98d31ad45
Incorporating new/strengthened proofs from Library and AFP entries
 paulson <lp15@cam.ac.uk> parents: 
67399diff
changeset | 2586 | lemma tendsto_poly [tendsto_intros]: "(f \<longlongrightarrow> a) F \<Longrightarrow> ((\<lambda>x. poly p (f x)) \<longlongrightarrow> poly p a) F" | 
| 
f8b98d31ad45
Incorporating new/strengthened proofs from Library and AFP entries
 paulson <lp15@cam.ac.uk> parents: 
67399diff
changeset | 2587 | for f :: "_ \<Rightarrow> 'a::real_normed_field" | 
| 
f8b98d31ad45
Incorporating new/strengthened proofs from Library and AFP entries
 paulson <lp15@cam.ac.uk> parents: 
67399diff
changeset | 2588 | by (rule isCont_tendsto_compose [OF poly_isCont]) | 
| 
f8b98d31ad45
Incorporating new/strengthened proofs from Library and AFP entries
 paulson <lp15@cam.ac.uk> parents: 
67399diff
changeset | 2589 | |
| 
f8b98d31ad45
Incorporating new/strengthened proofs from Library and AFP entries
 paulson <lp15@cam.ac.uk> parents: 
67399diff
changeset | 2590 | lemma continuous_within_poly: "continuous (at z within s) (poly p)" | 
| 
f8b98d31ad45
Incorporating new/strengthened proofs from Library and AFP entries
 paulson <lp15@cam.ac.uk> parents: 
67399diff
changeset | 2591 |   for z :: "'a::{real_normed_field}"
 | 
| 
f8b98d31ad45
Incorporating new/strengthened proofs from Library and AFP entries
 paulson <lp15@cam.ac.uk> parents: 
67399diff
changeset | 2592 | by (simp add: continuous_within tendsto_poly) | 
| 
f8b98d31ad45
Incorporating new/strengthened proofs from Library and AFP entries
 paulson <lp15@cam.ac.uk> parents: 
67399diff
changeset | 2593 | |
| 
f8b98d31ad45
Incorporating new/strengthened proofs from Library and AFP entries
 paulson <lp15@cam.ac.uk> parents: 
67399diff
changeset | 2594 | lemma continuous_poly [continuous_intros]: "continuous F f \<Longrightarrow> continuous F (\<lambda>x. poly p (f x))" | 
| 
f8b98d31ad45
Incorporating new/strengthened proofs from Library and AFP entries
 paulson <lp15@cam.ac.uk> parents: 
67399diff
changeset | 2595 | for f :: "_ \<Rightarrow> 'a::real_normed_field" | 
| 
f8b98d31ad45
Incorporating new/strengthened proofs from Library and AFP entries
 paulson <lp15@cam.ac.uk> parents: 
67399diff
changeset | 2596 | unfolding continuous_def by (rule tendsto_poly) | 
| 
f8b98d31ad45
Incorporating new/strengthened proofs from Library and AFP entries
 paulson <lp15@cam.ac.uk> parents: 
67399diff
changeset | 2597 | |
| 65346 | 2598 | lemma continuous_on_poly [continuous_intros]: | 
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2599 |   fixes p :: "'a :: {real_normed_field} poly"
 | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2600 | assumes "continuous_on A f" | 
| 65346 | 2601 | shows "continuous_on A (\<lambda>x. poly p (f x))" | 
| 68532 
f8b98d31ad45
Incorporating new/strengthened proofs from Library and AFP entries
 paulson <lp15@cam.ac.uk> parents: 
67399diff
changeset | 2602 | by (metis DERIV_continuous_on assms continuous_on_compose2 poly_DERIV subset_UNIV) | 
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2603 | |
| 65346 | 2604 | text \<open>Consequences of the derivative theorem above.\<close> | 
| 2605 | ||
| 2606 | lemma poly_differentiable[simp]: "(\<lambda>x. poly p x) differentiable (at x)" | |
| 2607 | for x :: real | |
| 2608 | by (simp add: real_differentiable_def) (blast intro: poly_DERIV) | |
| 2609 | ||
| 2610 | lemma poly_IVT_pos: "a < b \<Longrightarrow> poly p a < 0 \<Longrightarrow> 0 < poly p b \<Longrightarrow> \<exists>x. a < x \<and> x < b \<and> poly p x = 0" | |
| 2611 | for a b :: real | |
| 2612 | using IVT_objl [of "poly p" a 0 b] by (auto simp add: order_le_less) | |
| 2613 | ||
| 2614 | lemma poly_IVT_neg: "a < b \<Longrightarrow> 0 < poly p a \<Longrightarrow> poly p b < 0 \<Longrightarrow> \<exists>x. a < x \<and> x < b \<and> poly p x = 0" | |
| 2615 | for a b :: real | |
| 2616 | using poly_IVT_pos [where p = "- p"] by simp | |
| 2617 | ||
| 2618 | lemma poly_IVT: "a < b \<Longrightarrow> poly p a * poly p b < 0 \<Longrightarrow> \<exists>x>a. x < b \<and> poly p x = 0" | |
| 2619 | for p :: "real poly" | |
| 2620 | by (metis less_not_sym mult_less_0_iff poly_IVT_neg poly_IVT_pos) | |
| 2621 | ||
| 2622 | lemma poly_MVT: "a < b \<Longrightarrow> \<exists>x. a < x \<and> x < b \<and> poly p b - poly p a = (b - a) * poly (pderiv p) x" | |
| 2623 | for a b :: real | |
| 2624 | using MVT [of a b "poly p"] | |
| 69022 
e2858770997a
removal of more redundancies, and fixes
 paulson <lp15@cam.ac.uk> parents: 
68790diff
changeset | 2625 | apply simp | 
| 
e2858770997a
removal of more redundancies, and fixes
 paulson <lp15@cam.ac.uk> parents: 
68790diff
changeset | 2626 | by (metis (full_types) DERIV_continuous_on DERIV_unique has_field_derivative_at_within poly_DERIV) | 
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2627 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2628 | lemma poly_MVT': | 
| 65346 | 2629 | fixes a b :: real | 
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2630 |   assumes "{min a b..max a b} \<subseteq> A"
 | 
| 65346 | 2631 | shows "\<exists>x\<in>A. poly p b - poly p a = (b - a) * poly (pderiv p) x" | 
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2632 | proof (cases a b rule: linorder_cases) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2633 | case less | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2634 | from poly_MVT[OF less, of p] guess x by (elim exE conjE) | 
| 65346 | 2635 | then show ?thesis by (intro bexI[of _ x]) (auto intro!: subsetD[OF assms]) | 
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2636 | next | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2637 | case greater | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2638 | from poly_MVT[OF greater, of p] guess x by (elim exE conjE) | 
| 65346 | 2639 | then show ?thesis by (intro bexI[of _ x]) (auto simp: algebra_simps intro!: subsetD[OF assms]) | 
| 2640 | qed (use assms in auto) | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2641 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2642 | lemma poly_pinfty_gt_lc: | 
| 63649 | 2643 | fixes p :: "real poly" | 
| 65346 | 2644 | assumes "lead_coeff p > 0" | 
| 65347 | 2645 | shows "\<exists>n. \<forall> x \<ge> n. poly p x \<ge> lead_coeff p" | 
| 63649 | 2646 | using assms | 
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2647 | proof (induct p) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2648 | case 0 | 
| 63649 | 2649 | then show ?case by auto | 
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2650 | next | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2651 | case (pCons a p) | 
| 63649 | 2652 | from this(1) consider "a \<noteq> 0" "p = 0" | "p \<noteq> 0" by auto | 
| 2653 | then show ?case | |
| 2654 | proof cases | |
| 2655 | case 1 | |
| 2656 | then show ?thesis by auto | |
| 2657 | next | |
| 2658 | case 2 | |
| 2659 | with pCons obtain n1 where gte_lcoeff: "\<forall>x\<ge>n1. lead_coeff p \<le> poly p x" | |
| 2660 | by auto | |
| 2661 | from pCons(3) \<open>p \<noteq> 0\<close> have gt_0: "lead_coeff p > 0" by auto | |
| 2662 | define n where "n = max n1 (1 + \<bar>a\<bar> / lead_coeff p)" | |
| 2663 | have "lead_coeff (pCons a p) \<le> poly (pCons a p) x" if "n \<le> x" for x | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2664 | proof - | 
| 63649 | 2665 | from gte_lcoeff that have "lead_coeff p \<le> poly p x" | 
| 2666 | by (auto simp: n_def) | |
| 2667 | with gt_0 have "\<bar>a\<bar> / lead_coeff p \<ge> \<bar>a\<bar> / poly p x" and "poly p x > 0" | |
| 2668 | by (auto intro: frac_le) | |
| 65346 | 2669 | with \<open>n \<le> x\<close>[unfolded n_def] have "x \<ge> 1 + \<bar>a\<bar> / poly p x" | 
| 63649 | 2670 | by auto | 
| 2671 | with \<open>lead_coeff p \<le> poly p x\<close> \<open>poly p x > 0\<close> \<open>p \<noteq> 0\<close> | |
| 2672 | show "lead_coeff (pCons a p) \<le> poly (pCons a p) x" | |
| 2673 | by (auto simp: field_simps) | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2674 | qed | 
| 63649 | 2675 | then show ?thesis by blast | 
| 2676 | qed | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2677 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2678 | |
| 64795 | 2679 | lemma lemma_order_pderiv1: | 
| 2680 | "pderiv ([:- a, 1:] ^ Suc n * q) = [:- a, 1:] ^ Suc n * pderiv q + | |
| 2681 | smult (of_nat (Suc n)) (q * [:- a, 1:] ^ n)" | |
| 65346 | 2682 | by (simp only: pderiv_mult pderiv_power_Suc) (simp del: power_Suc of_nat_Suc add: pderiv_pCons) | 
| 64795 | 2683 | |
| 2684 | lemma lemma_order_pderiv: | |
| 2685 | fixes p :: "'a :: field_char_0 poly" | |
| 65346 | 2686 | assumes n: "0 < n" | 
| 2687 | and pd: "pderiv p \<noteq> 0" | |
| 2688 | and pe: "p = [:- a, 1:] ^ n * q" | |
| 2689 | and nd: "\<not> [:- a, 1:] dvd q" | |
| 2690 | shows "n = Suc (order a (pderiv p))" | |
| 64795 | 2691 | proof - | 
| 65346 | 2692 | from assms have "pderiv ([:- a, 1:] ^ n * q) \<noteq> 0" | 
| 2693 | by auto | |
| 2694 | from assms obtain n' where "n = Suc n'" "0 < Suc n'" "pderiv ([:- a, 1:] ^ Suc n' * q) \<noteq> 0" | |
| 2695 | by (cases n) auto | |
| 2696 | have *: "k dvd k * pderiv q + smult (of_nat (Suc n')) l \<Longrightarrow> k dvd l" for k l | |
| 64795 | 2697 | by (auto simp del: of_nat_Suc simp: dvd_add_right_iff dvd_smult_iff) | 
| 65346 | 2698 | have "n' = order a (pderiv ([:- a, 1:] ^ Suc n' * q))" | 
| 64795 | 2699 | proof (rule order_unique_lemma) | 
| 2700 | show "[:- a, 1:] ^ n' dvd pderiv ([:- a, 1:] ^ Suc n' * q)" | |
| 2701 | apply (subst lemma_order_pderiv1) | |
| 2702 | apply (rule dvd_add) | |
| 65346 | 2703 | apply (metis dvdI dvd_mult2 power_Suc2) | 
| 64795 | 2704 | apply (metis dvd_smult dvd_triv_right) | 
| 2705 | done | |
| 2706 | show "\<not> [:- a, 1:] ^ Suc n' dvd pderiv ([:- a, 1:] ^ Suc n' * q)" | |
| 65346 | 2707 | apply (subst lemma_order_pderiv1) | 
| 2708 | apply (metis * nd dvd_mult_cancel_right power_not_zero pCons_eq_0_iff power_Suc zero_neq_one) | |
| 2709 | done | |
| 64795 | 2710 | qed | 
| 2711 | then show ?thesis | |
| 2712 | by (metis \<open>n = Suc n'\<close> pe) | |
| 2713 | qed | |
| 2714 | ||
| 65346 | 2715 | lemma order_pderiv: "pderiv p \<noteq> 0 \<Longrightarrow> order a p \<noteq> 0 \<Longrightarrow> order a p = Suc (order a (pderiv p))" | 
| 2716 | for p :: "'a::field_char_0 poly" | |
| 2717 | apply (cases "p = 0") | |
| 2718 | apply simp | |
| 2719 | apply (drule_tac a = a and p = p in order_decomp) | |
| 2720 | using neq0_conv | |
| 2721 | apply (blast intro: lemma_order_pderiv) | |
| 2722 | done | |
| 64795 | 2723 | |
| 2724 | lemma poly_squarefree_decomp_order: | |
| 65346 | 2725 | fixes p :: "'a::field_char_0 poly" | 
| 2726 | assumes "pderiv p \<noteq> 0" | |
| 2727 | and p: "p = q * d" | |
| 2728 | and p': "pderiv p = e * d" | |
| 2729 | and d: "d = r * p + s * pderiv p" | |
| 64795 | 2730 | shows "order a q = (if order a p = 0 then 0 else 1)" | 
| 2731 | proof (rule classical) | |
| 65346 | 2732 | assume 1: "\<not> ?thesis" | 
| 64795 | 2733 | from \<open>pderiv p \<noteq> 0\<close> have "p \<noteq> 0" by auto | 
| 2734 | with p have "order a p = order a q + order a d" | |
| 2735 | by (simp add: order_mult) | |
| 65346 | 2736 | with 1 have "order a p \<noteq> 0" | 
| 2737 | by (auto split: if_splits) | |
| 2738 | from \<open>pderiv p \<noteq> 0\<close> \<open>pderiv p = e * d\<close> have "order a (pderiv p) = order a e + order a d" | |
| 2739 | by (simp add: order_mult) | |
| 2740 | from \<open>pderiv p \<noteq> 0\<close> \<open>order a p \<noteq> 0\<close> have "order a p = Suc (order a (pderiv p))" | |
| 2741 | by (rule order_pderiv) | |
| 2742 | from \<open>p \<noteq> 0\<close> \<open>p = q * d\<close> have "d \<noteq> 0" | |
| 2743 | by simp | |
| 64795 | 2744 | have "([:-a, 1:] ^ (order a (pderiv p))) dvd d" | 
| 2745 | apply (simp add: d) | |
| 2746 | apply (rule dvd_add) | |
| 65346 | 2747 | apply (rule dvd_mult) | 
| 2748 | apply (simp add: order_divides \<open>p \<noteq> 0\<close> \<open>order a p = Suc (order a (pderiv p))\<close>) | |
| 64795 | 2749 | apply (rule dvd_mult) | 
| 2750 | apply (simp add: order_divides) | |
| 2751 | done | |
| 65346 | 2752 | with \<open>d \<noteq> 0\<close> have "order a (pderiv p) \<le> order a d" | 
| 2753 | by (simp add: order_divides) | |
| 64795 | 2754 | show ?thesis | 
| 2755 | using \<open>order a p = order a q + order a d\<close> | |
| 65346 | 2756 | and \<open>order a (pderiv p) = order a e + order a d\<close> | 
| 2757 | and \<open>order a p = Suc (order a (pderiv p))\<close> | |
| 2758 | and \<open>order a (pderiv p) \<le> order a d\<close> | |
| 64795 | 2759 | by auto | 
| 2760 | qed | |
| 2761 | ||
| 65346 | 2762 | lemma poly_squarefree_decomp_order2: | 
| 65347 | 2763 | "pderiv p \<noteq> 0 \<Longrightarrow> p = q * d \<Longrightarrow> pderiv p = e * d \<Longrightarrow> | 
| 2764 | d = r * p + s * pderiv p \<Longrightarrow> \<forall>a. order a q = (if order a p = 0 then 0 else 1)" | |
| 2765 | for p :: "'a::field_char_0 poly" | |
| 2766 | by (blast intro: poly_squarefree_decomp_order) | |
| 64795 | 2767 | |
| 65346 | 2768 | lemma order_pderiv2: | 
| 65347 | 2769 | "pderiv p \<noteq> 0 \<Longrightarrow> order a p \<noteq> 0 \<Longrightarrow> order a (pderiv p) = n \<longleftrightarrow> order a p = Suc n" | 
| 2770 | for p :: "'a::field_char_0 poly" | |
| 2771 | by (auto dest: order_pderiv) | |
| 64795 | 2772 | |
| 2773 | definition rsquarefree :: "'a::idom poly \<Rightarrow> bool" | |
| 2774 | where "rsquarefree p \<longleftrightarrow> p \<noteq> 0 \<and> (\<forall>a. order a p = 0 \<or> order a p = 1)" | |
| 2775 | ||
| 65347 | 2776 | lemma pderiv_iszero: "pderiv p = 0 \<Longrightarrow> \<exists>h. p = [:h:]" | 
| 2777 |   for p :: "'a::{semidom,semiring_char_0} poly"
 | |
| 64795 | 2778 | by (cases p) (auto simp: pderiv_eq_0_iff split: if_splits) | 
| 2779 | ||
| 65347 | 2780 | lemma rsquarefree_roots: "rsquarefree p \<longleftrightarrow> (\<forall>a. \<not> (poly p a = 0 \<and> poly (pderiv p) a = 0))" | 
| 2781 | for p :: "'a::field_char_0 poly" | |
| 2782 | apply (simp add: rsquarefree_def) | |
| 2783 | apply (case_tac "p = 0") | |
| 2784 | apply simp | |
| 2785 | apply simp | |
| 2786 | apply (case_tac "pderiv p = 0") | |
| 2787 | apply simp | |
| 2788 | apply (drule pderiv_iszero, clarsimp) | |
| 2789 | apply (metis coeff_0 coeff_pCons_0 degree_pCons_0 le0 le_antisym order_degree) | |
| 2790 | apply (force simp add: order_root order_pderiv2) | |
| 64795 | 2791 | done | 
| 2792 | ||
| 2793 | lemma poly_squarefree_decomp: | |
| 65347 | 2794 | fixes p :: "'a::field_char_0 poly" | 
| 2795 | assumes "pderiv p \<noteq> 0" | |
| 64795 | 2796 | and "p = q * d" | 
| 2797 | and "pderiv p = e * d" | |
| 2798 | and "d = r * p + s * pderiv p" | |
| 65347 | 2799 | shows "rsquarefree q \<and> (\<forall>a. poly q a = 0 \<longleftrightarrow> poly p a = 0)" | 
| 64795 | 2800 | proof - | 
| 2801 | from \<open>pderiv p \<noteq> 0\<close> have "p \<noteq> 0" by auto | |
| 2802 | with \<open>p = q * d\<close> have "q \<noteq> 0" by simp | |
| 65347 | 2803 | from assms have "\<forall>a. order a q = (if order a p = 0 then 0 else 1)" | 
| 2804 | by (rule poly_squarefree_decomp_order2) | |
| 64795 | 2805 | with \<open>p \<noteq> 0\<close> \<open>q \<noteq> 0\<close> show ?thesis | 
| 2806 | by (simp add: rsquarefree_def order_root) | |
| 2807 | qed | |
| 2808 | ||
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2809 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2810 | subsection \<open>Algebraic numbers\<close> | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2811 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2812 | text \<open> | 
| 65346 | 2813 | Algebraic numbers can be defined in two equivalent ways: all real numbers that are | 
| 2814 | roots of rational polynomials or of integer polynomials. The Algebraic-Numbers AFP entry | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2815 | uses the rational definition, but we need the integer definition. | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2816 | |
| 65346 | 2817 | The equivalence is obvious since any rational polynomial can be multiplied with the | 
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2818 | LCM of its coefficients, yielding an integer polynomial with the same roots. | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2819 | \<close> | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2820 | |
| 65347 | 2821 | definition algebraic :: "'a :: field_char_0 \<Rightarrow> bool" | 
| 2822 | where "algebraic x \<longleftrightarrow> (\<exists>p. (\<forall>i. coeff p i \<in> \<int>) \<and> p \<noteq> 0 \<and> poly p x = 0)" | |
| 2823 | ||
| 2824 | lemma algebraicI: "(\<And>i. coeff p i \<in> \<int>) \<Longrightarrow> p \<noteq> 0 \<Longrightarrow> poly p x = 0 \<Longrightarrow> algebraic x" | |
| 2825 | unfolding algebraic_def by blast | |
| 65346 | 2826 | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2827 | lemma algebraicE: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2828 | assumes "algebraic x" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2829 | obtains p where "\<And>i. coeff p i \<in> \<int>" "p \<noteq> 0" "poly p x = 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2830 | using assms unfolding algebraic_def by blast | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2831 | |
| 65347 | 2832 | lemma algebraic_altdef: "algebraic x \<longleftrightarrow> (\<exists>p. (\<forall>i. coeff p i \<in> \<rat>) \<and> p \<noteq> 0 \<and> poly p x = 0)" | 
| 2833 | for p :: "'a::field_char_0 poly" | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2834 | proof safe | 
| 65347 | 2835 | fix p | 
| 2836 | assume rat: "\<forall>i. coeff p i \<in> \<rat>" and root: "poly p x = 0" and nz: "p \<noteq> 0" | |
| 63040 | 2837 | define cs where "cs = coeffs p" | 
| 65347 | 2838 | from rat have "\<forall>c\<in>range (coeff p). \<exists>c'. c = of_rat c'" | 
| 2839 | unfolding Rats_def by blast | |
| 63060 | 2840 | then obtain f where f: "coeff p i = of_rat (f (coeff p i))" for i | 
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2841 | by (subst (asm) bchoice_iff) blast | 
| 63040 | 2842 | define cs' where "cs' = map (quotient_of \<circ> f) (coeffs p)" | 
| 2843 | define d where "d = Lcm (set (map snd cs'))" | |
| 2844 | define p' where "p' = smult (of_int d) p" | |
| 65346 | 2845 | |
| 65347 | 2846 | have "coeff p' n \<in> \<int>" for n | 
| 2847 | proof (cases "n \<le> degree p") | |
| 2848 | case True | |
| 2849 | define c where "c = coeff p n" | |
| 2850 | define a where "a = fst (quotient_of (f (coeff p n)))" | |
| 2851 | define b where "b = snd (quotient_of (f (coeff p n)))" | |
| 2852 | have b_pos: "b > 0" | |
| 2853 | unfolding b_def using quotient_of_denom_pos' by simp | |
| 2854 | have "coeff p' n = of_int d * coeff p n" | |
| 2855 | by (simp add: p'_def) | |
| 2856 | also have "coeff p n = of_rat (of_int a / of_int b)" | |
| 2857 | unfolding a_def b_def | |
| 2858 | by (subst quotient_of_div [of "f (coeff p n)", symmetric]) (simp_all add: f [symmetric]) | |
| 2859 | also have "of_int d * \<dots> = of_rat (of_int (a*d) / of_int b)" | |
| 2860 | by (simp add: of_rat_mult of_rat_divide) | |
| 2861 | also from nz True have "b \<in> snd ` set cs'" | |
| 2862 | by (force simp: cs'_def o_def b_def coeffs_def simp del: upt_Suc) | |
| 2863 | then have "b dvd (a * d)" | |
| 2864 | by (simp add: d_def) | |
| 2865 | then have "of_int (a * d) / of_int b \<in> (\<int> :: rat set)" | |
| 2866 | by (rule of_int_divide_in_Ints) | |
| 2867 | then have "of_rat (of_int (a * d) / of_int b) \<in> \<int>" by (elim Ints_cases) auto | |
| 2868 | finally show ?thesis . | |
| 2869 | next | |
| 2870 | case False | |
| 2871 | then show ?thesis | |
| 2872 | by (auto simp: p'_def not_le coeff_eq_0) | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2873 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2874 |   moreover have "set (map snd cs') \<subseteq> {0<..}"
 | 
| 65346 | 2875 | unfolding cs'_def using quotient_of_denom_pos' by (auto simp: coeffs_def simp del: upt_Suc) | 
| 65347 | 2876 | then have "d \<noteq> 0" | 
| 2877 | unfolding d_def by (induct cs') simp_all | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2878 | with nz have "p' \<noteq> 0" by (simp add: p'_def) | 
| 65347 | 2879 | moreover from root have "poly p' x = 0" | 
| 2880 | by (simp add: p'_def) | |
| 2881 | ultimately show "algebraic x" | |
| 2882 | unfolding algebraic_def by blast | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2883 | next | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2884 | assume "algebraic x" | 
| 63060 | 2885 | then obtain p where p: "coeff p i \<in> \<int>" "poly p x = 0" "p \<noteq> 0" for i | 
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2886 | by (force simp: algebraic_def) | 
| 65347 | 2887 | moreover have "coeff p i \<in> \<int> \<Longrightarrow> coeff p i \<in> \<rat>" for i | 
| 2888 | by (elim Ints_cases) simp | |
| 2889 | ultimately show "\<exists>p. (\<forall>i. coeff p i \<in> \<rat>) \<and> p \<noteq> 0 \<and> poly p x = 0" by auto | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2890 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2891 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2892 | |
| 64795 | 2893 | subsection \<open>Division of polynomials\<close> | 
| 2894 | ||
| 2895 | subsubsection \<open>Division in general\<close> | |
| 65346 | 2896 | |
| 64795 | 2897 | instantiation poly :: (idom_divide) idom_divide | 
| 2898 | begin | |
| 2899 | ||
| 65347 | 2900 | fun divide_poly_main :: "'a \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> 'a poly" | 
| 2901 | where | |
| 2902 | "divide_poly_main lc q r d dr (Suc n) = | |
| 2903 | (let cr = coeff r dr; a = cr div lc; mon = monom a n in | |
| 67369 | 2904 | if False \<or> a * lc = cr then \<comment> \<open>\<open>False \<or>\<close> is only because of problem in function-package\<close> | 
| 65347 | 2905 | divide_poly_main | 
| 2906 | lc | |
| 2907 | (q + mon) | |
| 2908 | (r - mon * d) | |
| 2909 | d (dr - 1) n else 0)" | |
| 2910 | | "divide_poly_main lc q r d dr 0 = q" | |
| 2911 | ||
| 2912 | definition divide_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" | |
| 2913 | where "divide_poly f g = | |
| 2914 | (if g = 0 then 0 | |
| 2915 | else | |
| 2916 | divide_poly_main (coeff g (degree g)) 0 f g (degree f) | |
| 2917 | (1 + length (coeffs f) - length (coeffs g)))" | |
| 64795 | 2918 | |
| 2919 | lemma divide_poly_main: | |
| 2920 | assumes d: "d \<noteq> 0" "lc = coeff d (degree d)" | |
| 65347 | 2921 | and "degree (d * r) \<le> dr" "divide_poly_main lc q (d * r) d dr n = q'" | 
| 2922 | and "n = 1 + dr - degree d \<or> dr = 0 \<and> n = 0 \<and> d * r = 0" | |
| 64795 | 2923 | shows "q' = q + r" | 
| 65347 | 2924 | using assms(3-) | 
| 64795 | 2925 | proof (induct n arbitrary: q r dr) | 
| 65347 | 2926 | case (Suc n) | 
| 64795 | 2927 | let ?rr = "d * r" | 
| 2928 | let ?a = "coeff ?rr dr" | |
| 2929 | let ?qq = "?a div lc" | |
| 2930 | define b where [simp]: "b = monom ?qq n" | |
| 2931 | let ?rrr = "d * (r - b)" | |
| 2932 | let ?qqq = "q + b" | |
| 2933 | note res = Suc(3) | |
| 65347 | 2934 | from Suc(4) have dr: "dr = n + degree d" by auto | 
| 2935 | from d have lc: "lc \<noteq> 0" by auto | |
| 64795 | 2936 | have "coeff (b * d) dr = coeff b n * coeff d (degree d)" | 
| 2937 | proof (cases "?qq = 0") | |
| 65347 | 2938 | case True | 
| 2939 | then show ?thesis by simp | |
| 2940 | next | |
| 64795 | 2941 | case False | 
| 65347 | 2942 | then have n: "n = degree b" | 
| 2943 | by (simp add: degree_monom_eq) | |
| 2944 | show ?thesis | |
| 2945 | unfolding n dr by (simp add: coeff_mult_degree_sum) | |
| 2946 | qed | |
| 2947 | also have "\<dots> = lc * coeff b n" | |
| 2948 | by (simp add: d) | |
| 64795 | 2949 | finally have c2: "coeff (b * d) dr = lc * coeff b n" . | 
| 65347 | 2950 | have rrr: "?rrr = ?rr - b * d" | 
| 2951 | by (simp add: field_simps) | |
| 64795 | 2952 | have c1: "coeff (d * r) dr = lc * coeff r n" | 
| 2953 | proof (cases "degree r = n") | |
| 2954 | case True | |
| 65347 | 2955 | with Suc(2) show ?thesis | 
| 2956 | unfolding dr using coeff_mult_degree_sum[of d r] d by (auto simp: ac_simps) | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 2957 | next | 
| 64795 | 2958 | case False | 
| 65347 | 2959 | from dr Suc(2) have "degree r \<le> n" | 
| 2960 | by auto | |
| 2961 | (metis add.commute add_le_cancel_left d(1) degree_0 degree_mult_eq | |
| 2962 | diff_is_0_eq diff_zero le_cases) | |
| 2963 | with False have r_n: "degree r < n" | |
| 2964 | by auto | |
| 2965 | then have right: "lc * coeff r n = 0" | |
| 2966 | by (simp add: coeff_eq_0) | |
| 2967 | have "coeff (d * r) dr = coeff (d * r) (degree d + n)" | |
| 2968 | by (simp add: dr ac_simps) | |
| 2969 | also from r_n have "\<dots> = 0" | |
| 65346 | 2970 | by (metis False Suc.prems(1) add.commute add_left_imp_eq coeff_degree_mult coeff_eq_0 | 
| 64795 | 2971 | coeff_mult_degree_sum degree_mult_le dr le_eq_less_or_eq) | 
| 65347 | 2972 | finally show ?thesis | 
| 2973 | by (simp only: right) | |
| 64795 | 2974 | qed | 
| 65346 | 2975 | have c0: "coeff ?rrr dr = 0" | 
| 65347 | 2976 | and id: "lc * (coeff (d * r) dr div lc) = coeff (d * r) dr" | 
| 2977 | unfolding rrr coeff_diff c2 | |
| 64795 | 2978 | unfolding b_def coeff_monom coeff_smult c1 using lc by auto | 
| 2979 | from res[unfolded divide_poly_main.simps[of lc q] Let_def] id | |
| 65346 | 2980 | have res: "divide_poly_main lc ?qqq ?rrr d (dr - 1) n = q'" | 
| 64795 | 2981 | by (simp del: divide_poly_main.simps add: field_simps) | 
| 65346 | 2982 | note IH = Suc(1)[OF _ res] | 
| 65347 | 2983 | from Suc(4) have dr: "dr = n + degree d" by auto | 
| 2984 | from Suc(2) have deg_rr: "degree ?rr \<le> dr" by auto | |
| 64795 | 2985 | have deg_bd: "degree (b * d) \<le> dr" | 
| 65347 | 2986 | unfolding dr b_def by (rule order.trans[OF degree_mult_le]) (auto simp: degree_monom_le) | 
| 2987 | have "degree ?rrr \<le> dr" | |
| 2988 | unfolding rrr by (rule degree_diff_le[OF deg_rr deg_bd]) | |
| 64795 | 2989 | with c0 have deg_rrr: "degree ?rrr \<le> (dr - 1)" | 
| 2990 | by (rule coeff_0_degree_minus_1) | |
| 65346 | 2991 | have "n = 1 + (dr - 1) - degree d \<or> dr - 1 = 0 \<and> n = 0 \<and> ?rrr = 0" | 
| 64795 | 2992 | proof (cases dr) | 
| 2993 | case 0 | |
| 65347 | 2994 | with Suc(4) have 0: "dr = 0" "n = 0" "degree d = 0" | 
| 2995 | by auto | |
| 2996 | with deg_rrr have "degree ?rrr = 0" | |
| 2997 | by simp | |
| 2998 | from degree_eq_zeroE[OF this] obtain a where rrr: "?rrr = [:a:]" | |
| 2999 | by metis | |
| 3000 | show ?thesis | |
| 3001 | unfolding 0 using c0 unfolding rrr 0 by simp | |
| 3002 | next | |
| 3003 | case _: Suc | |
| 3004 | with Suc(4) show ?thesis by auto | |
| 3005 | qed | |
| 3006 | from IH[OF deg_rrr this] show ?case | |
| 3007 | by simp | |
| 64795 | 3008 | next | 
| 65347 | 3009 | case 0 | 
| 65346 | 3010 | show ?case | 
| 64795 | 3011 | proof (cases "r = 0") | 
| 3012 | case True | |
| 65347 | 3013 | with 0 show ?thesis by auto | 
| 64795 | 3014 | next | 
| 3015 | case False | |
| 65347 | 3016 | from d False have "degree (d * r) = degree d + degree r" | 
| 3017 | by (subst degree_mult_eq) auto | |
| 3018 | with 0 d show ?thesis by auto | |
| 64795 | 3019 | qed | 
| 65346 | 3020 | qed | 
| 64795 | 3021 | |
| 3022 | lemma divide_poly_main_0: "divide_poly_main 0 0 r d dr n = 0" | |
| 3023 | proof (induct n arbitrary: r d dr) | |
| 65347 | 3024 | case 0 | 
| 3025 | then show ?case by simp | |
| 3026 | next | |
| 3027 | case Suc | |
| 3028 | show ?case | |
| 3029 | unfolding divide_poly_main.simps[of _ _ r] Let_def | |
| 64795 | 3030 | by (simp add: Suc del: divide_poly_main.simps) | 
| 65347 | 3031 | qed | 
| 64795 | 3032 | |
| 3033 | lemma divide_poly: | |
| 3034 | assumes g: "g \<noteq> 0" | |
| 65346 | 3035 | shows "(f * g) div g = (f :: 'a poly)" | 
| 3036 | proof - | |
| 65347 | 3037 | have len: "length (coeffs f) = Suc (degree f)" if "f \<noteq> 0" for f :: "'a poly" | 
| 3038 | using that unfolding degree_eq_length_coeffs by auto | |
| 3039 | have "divide_poly_main (coeff g (degree g)) 0 (g * f) g (degree (g * f)) | |
| 3040 | (1 + length (coeffs (g * f)) - length (coeffs g)) = (f * g) div g" | |
| 3041 | by (simp add: divide_poly_def Let_def ac_simps) | |
| 64795 | 3042 | note main = divide_poly_main[OF g refl le_refl this] | 
| 3043 | have "(f * g) div g = 0 + f" | |
| 3044 | proof (rule main, goal_cases) | |
| 3045 | case 1 | |
| 3046 | show ?case | |
| 3047 | proof (cases "f = 0") | |
| 3048 | case True | |
| 65347 | 3049 | with g show ?thesis | 
| 3050 | by (auto simp: degree_eq_length_coeffs) | |
| 64795 | 3051 | next | 
| 3052 | case False | |
| 3053 | with g have fg: "g * f \<noteq> 0" by auto | |
| 65347 | 3054 | show ?thesis | 
| 3055 | unfolding len[OF fg] len[OF g] by auto | |
| 64795 | 3056 | qed | 
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3057 | qed | 
| 65346 | 3058 | then show ?thesis by simp | 
| 64795 | 3059 | qed | 
| 3060 | ||
| 65347 | 3061 | lemma divide_poly_0: "f div 0 = 0" | 
| 3062 | for f :: "'a poly" | |
| 64795 | 3063 | by (simp add: divide_poly_def Let_def divide_poly_main_0) | 
| 3064 | ||
| 3065 | instance | |
| 3066 | by standard (auto simp: divide_poly divide_poly_0) | |
| 3067 | ||
| 3068 | end | |
| 3069 | ||
| 3070 | instance poly :: (idom_divide) algebraic_semidom .. | |
| 3071 | ||
| 65346 | 3072 | lemma div_const_poly_conv_map_poly: | 
| 64795 | 3073 | assumes "[:c:] dvd p" | 
| 65347 | 3074 | shows "p div [:c:] = map_poly (\<lambda>x. x div c) p" | 
| 64795 | 3075 | proof (cases "c = 0") | 
| 65347 | 3076 | case True | 
| 3077 | then show ?thesis | |
| 3078 | by (auto intro!: poly_eqI simp: coeff_map_poly) | |
| 3079 | next | |
| 64795 | 3080 | case False | 
| 65347 | 3081 | from assms obtain q where p: "p = [:c:] * q" by (rule dvdE) | 
| 64795 | 3082 |   moreover {
 | 
| 65347 | 3083 | have "smult c q = [:c:] * q" | 
| 3084 | by simp | |
| 3085 | also have "\<dots> div [:c:] = q" | |
| 3086 | by (rule nonzero_mult_div_cancel_left) (use False in auto) | |
| 64795 | 3087 | finally have "smult c q div [:c:] = q" . | 
| 3088 | } | |
| 3089 | ultimately show ?thesis by (intro poly_eqI) (auto simp: coeff_map_poly False) | |
| 65347 | 3090 | qed | 
| 64795 | 3091 | |
| 3092 | lemma is_unit_monom_0: | |
| 3093 | fixes a :: "'a::field" | |
| 3094 | assumes "a \<noteq> 0" | |
| 3095 | shows "is_unit (monom a 0)" | |
| 3096 | proof | |
| 3097 | from assms show "1 = monom a 0 * monom (inverse a) 0" | |
| 3098 | by (simp add: mult_monom) | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3099 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3100 | |
| 65347 | 3101 | lemma is_unit_triv: "a \<noteq> 0 \<Longrightarrow> is_unit [:a:]" | 
| 3102 | for a :: "'a::field" | |
| 3103 | by (simp add: is_unit_monom_0 monom_0 [symmetric]) | |
| 64795 | 3104 | |
| 3105 | lemma is_unit_iff_degree: | |
| 65347 | 3106 | fixes p :: "'a::field poly" | 
| 3107 | assumes "p \<noteq> 0" | |
| 3108 | shows "is_unit p \<longleftrightarrow> degree p = 0" | |
| 3109 | (is "?lhs \<longleftrightarrow> ?rhs") | |
| 64795 | 3110 | proof | 
| 65347 | 3111 | assume ?rhs | 
| 3112 | then obtain a where "p = [:a:]" | |
| 3113 | by (rule degree_eq_zeroE) | |
| 3114 | with assms show ?lhs | |
| 3115 | by (simp add: is_unit_triv) | |
| 64795 | 3116 | next | 
| 65347 | 3117 | assume ?lhs | 
| 64795 | 3118 | then obtain q where "q \<noteq> 0" "p * q = 1" .. | 
| 3119 | then have "degree (p * q) = degree 1" | |
| 3120 | by simp | |
| 3121 | with \<open>p \<noteq> 0\<close> \<open>q \<noteq> 0\<close> have "degree p + degree q = 0" | |
| 3122 | by (simp add: degree_mult_eq) | |
| 65347 | 3123 | then show ?rhs by simp | 
| 64795 | 3124 | qed | 
| 3125 | ||
| 65347 | 3126 | lemma is_unit_pCons_iff: "is_unit (pCons a p) \<longleftrightarrow> p = 0 \<and> a \<noteq> 0" | 
| 3127 | for p :: "'a::field poly" | |
| 3128 | by (cases "p = 0") (auto simp: is_unit_triv is_unit_iff_degree) | |
| 3129 | ||
| 3130 | lemma is_unit_monom_trival: "is_unit p \<Longrightarrow> monom (coeff p (degree p)) 0 = p" | |
| 3131 | for p :: "'a::field poly" | |
| 3132 | by (cases p) (simp_all add: monom_0 is_unit_pCons_iff) | |
| 3133 | ||
| 3134 | lemma is_unit_const_poly_iff: "[:c:] dvd 1 \<longleftrightarrow> c dvd 1" | |
| 3135 |   for c :: "'a::{comm_semiring_1,semiring_no_zero_divisors}"
 | |
| 65486 | 3136 | by (auto simp: one_pCons) | 
| 64795 | 3137 | |
| 3138 | lemma is_unit_polyE: | |
| 3139 |   fixes p :: "'a :: {comm_semiring_1,semiring_no_zero_divisors} poly"
 | |
| 65347 | 3140 | assumes "p dvd 1" | 
| 3141 | obtains c where "p = [:c:]" "c dvd 1" | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3142 | proof - | 
| 64795 | 3143 | from assms obtain q where "1 = p * q" | 
| 3144 | by (rule dvdE) | |
| 3145 | then have "p \<noteq> 0" and "q \<noteq> 0" | |
| 3146 | by auto | |
| 3147 | from \<open>1 = p * q\<close> have "degree 1 = degree (p * q)" | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3148 | by simp | 
| 64795 | 3149 | also from \<open>p \<noteq> 0\<close> and \<open>q \<noteq> 0\<close> have "\<dots> = degree p + degree q" | 
| 3150 | by (simp add: degree_mult_eq) | |
| 3151 | finally have "degree p = 0" by simp | |
| 3152 | with degree_eq_zeroE obtain c where c: "p = [:c:]" . | |
| 65347 | 3153 | with \<open>p dvd 1\<close> have "c dvd 1" | 
| 64795 | 3154 | by (simp add: is_unit_const_poly_iff) | 
| 65347 | 3155 | with c show thesis .. | 
| 64795 | 3156 | qed | 
| 3157 | ||
| 3158 | lemma is_unit_polyE': | |
| 65347 | 3159 | fixes p :: "'a::field poly" | 
| 3160 | assumes "is_unit p" | |
| 64795 | 3161 | obtains a where "p = monom a 0" and "a \<noteq> 0" | 
| 3162 | proof - | |
| 65347 | 3163 | obtain a q where "p = pCons a q" | 
| 3164 | by (cases p) | |
| 64795 | 3165 | with assms have "p = [:a:]" and "a \<noteq> 0" | 
| 3166 | by (simp_all add: is_unit_pCons_iff) | |
| 3167 | with that show thesis by (simp add: monom_0) | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3168 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3169 | |
| 65347 | 3170 | lemma is_unit_poly_iff: "p dvd 1 \<longleftrightarrow> (\<exists>c. p = [:c:] \<and> c dvd 1)" | 
| 3171 |   for p :: "'a::{comm_semiring_1,semiring_no_zero_divisors} poly"
 | |
| 64795 | 3172 | by (auto elim: is_unit_polyE simp add: is_unit_const_poly_iff) | 
| 3173 | ||
| 65346 | 3174 | |
| 64795 | 3175 | subsubsection \<open>Pseudo-Division\<close> | 
| 3176 | ||
| 65347 | 3177 | text \<open>This part is by René Thiemann and Akihisa Yamada.\<close> | 
| 3178 | ||
| 3179 | fun pseudo_divmod_main :: | |
| 3180 | "'a :: comm_ring_1 \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> 'a poly \<times> 'a poly" | |
| 3181 | where | |
| 3182 | "pseudo_divmod_main lc q r d dr (Suc n) = | |
| 3183 | (let | |
| 3184 | rr = smult lc r; | |
| 3185 | qq = coeff r dr; | |
| 3186 | rrr = rr - monom qq n * d; | |
| 3187 | qqq = smult lc q + monom qq n | |
| 3188 | in pseudo_divmod_main lc qqq rrr d (dr - 1) n)" | |
| 3189 | | "pseudo_divmod_main lc q r d dr 0 = (q,r)" | |
| 3190 | ||
| 3191 | definition pseudo_divmod :: "'a :: comm_ring_1 poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<times> 'a poly" | |
| 3192 | where "pseudo_divmod p q \<equiv> | |
| 3193 | if q = 0 then (0, p) | |
| 3194 | else | |
| 3195 | pseudo_divmod_main (coeff q (degree q)) 0 p q (degree p) | |
| 3196 | (1 + length (coeffs p) - length (coeffs q))" | |
| 3197 | ||
| 3198 | lemma pseudo_divmod_main: | |
| 3199 | assumes d: "d \<noteq> 0" "lc = coeff d (degree d)" | |
| 3200 | and "degree r \<le> dr" "pseudo_divmod_main lc q r d dr n = (q',r')" | |
| 3201 | and "n = 1 + dr - degree d \<or> dr = 0 \<and> n = 0 \<and> r = 0" | |
| 64795 | 3202 | shows "(r' = 0 \<or> degree r' < degree d) \<and> smult (lc^n) (d * q + r) = d * q' + r'" | 
| 65347 | 3203 | using assms(3-) | 
| 64795 | 3204 | proof (induct n arbitrary: q r dr) | 
| 65347 | 3205 | case 0 | 
| 3206 | then show ?case by auto | |
| 3207 | next | |
| 3208 | case (Suc n) | |
| 64795 | 3209 | let ?rr = "smult lc r" | 
| 3210 | let ?qq = "coeff r dr" | |
| 3211 | define b where [simp]: "b = monom ?qq n" | |
| 3212 | let ?rrr = "?rr - b * d" | |
| 3213 | let ?qqq = "smult lc q + b" | |
| 3214 | note res = Suc(3) | |
| 65346 | 3215 | from res[unfolded pseudo_divmod_main.simps[of lc q] Let_def] | 
| 3216 | have res: "pseudo_divmod_main lc ?qqq ?rrr d (dr - 1) n = (q',r')" | |
| 64795 | 3217 | by (simp del: pseudo_divmod_main.simps) | 
| 65347 | 3218 | from Suc(4) have dr: "dr = n + degree d" by auto | 
| 64795 | 3219 | have "coeff (b * d) dr = coeff b n * coeff d (degree d)" | 
| 3220 | proof (cases "?qq = 0") | |
| 65347 | 3221 | case True | 
| 3222 | then show ?thesis by auto | |
| 3223 | next | |
| 64795 | 3224 | case False | 
| 65347 | 3225 | then have n: "n = degree b" | 
| 3226 | by (simp add: degree_monom_eq) | |
| 3227 | show ?thesis | |
| 3228 | unfolding n dr by (simp add: coeff_mult_degree_sum) | |
| 3229 | qed | |
| 3230 | also have "\<dots> = lc * coeff b n" by (simp add: d) | |
| 64795 | 3231 | finally have "coeff (b * d) dr = lc * coeff b n" . | 
| 65347 | 3232 | moreover have "coeff ?rr dr = lc * coeff r dr" | 
| 3233 | by simp | |
| 3234 | ultimately have c0: "coeff ?rrr dr = 0" | |
| 3235 | by auto | |
| 3236 | from Suc(4) have dr: "dr = n + degree d" by auto | |
| 3237 | have deg_rr: "degree ?rr \<le> dr" | |
| 3238 | using Suc(2) degree_smult_le dual_order.trans by blast | |
| 64795 | 3239 | have deg_bd: "degree (b * d) \<le> dr" | 
| 65347 | 3240 | unfolding dr by (rule order.trans[OF degree_mult_le]) (auto simp: degree_monom_le) | 
| 64795 | 3241 | have "degree ?rrr \<le> dr" | 
| 3242 | using degree_diff_le[OF deg_rr deg_bd] by auto | |
| 65347 | 3243 | with c0 have deg_rrr: "degree ?rrr \<le> (dr - 1)" | 
| 3244 | by (rule coeff_0_degree_minus_1) | |
| 64795 | 3245 | have "n = 1 + (dr - 1) - degree d \<or> dr - 1 = 0 \<and> n = 0 \<and> ?rrr = 0" | 
| 3246 | proof (cases dr) | |
| 3247 | case 0 | |
| 3248 | with Suc(4) have 0: "dr = 0" "n = 0" "degree d = 0" by auto | |
| 3249 | with deg_rrr have "degree ?rrr = 0" by simp | |
| 65347 | 3250 | then have "\<exists>a. ?rrr = [:a:]" | 
| 3251 | by (metis degree_pCons_eq_if old.nat.distinct(2) pCons_cases) | |
| 3252 | from this obtain a where rrr: "?rrr = [:a:]" | |
| 3253 | by auto | |
| 3254 | show ?thesis | |
| 3255 | unfolding 0 using c0 unfolding rrr 0 by simp | |
| 3256 | next | |
| 3257 | case _: Suc | |
| 3258 | with Suc(4) show ?thesis by auto | |
| 3259 | qed | |
| 64795 | 3260 | note IH = Suc(1)[OF deg_rrr res this] | 
| 3261 | show ?case | |
| 3262 | proof (intro conjI) | |
| 65347 | 3263 | from IH show "r' = 0 \<or> degree r' < degree d" | 
| 3264 | by blast | |
| 64795 | 3265 | show "smult (lc ^ Suc n) (d * q + r) = d * q' + r'" | 
| 3266 | unfolding IH[THEN conjunct2,symmetric] | |
| 3267 | by (simp add: field_simps smult_add_right) | |
| 3268 | qed | |
| 65347 | 3269 | qed | 
| 64795 | 3270 | |
| 3271 | lemma pseudo_divmod: | |
| 65347 | 3272 | assumes g: "g \<noteq> 0" | 
| 3273 | and *: "pseudo_divmod f g = (q,r)" | |
| 3274 | shows "smult (coeff g (degree g) ^ (Suc (degree f) - degree g)) f = g * q + r" (is ?A) | |
| 3275 | and "r = 0 \<or> degree r < degree g" (is ?B) | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3276 | proof - | 
| 64795 | 3277 | from *[unfolded pseudo_divmod_def Let_def] | 
| 65347 | 3278 | have "pseudo_divmod_main (coeff g (degree g)) 0 f g (degree f) | 
| 3279 | (1 + length (coeffs f) - length (coeffs g)) = (q, r)" | |
| 3280 | by (auto simp: g) | |
| 64795 | 3281 | note main = pseudo_divmod_main[OF _ _ _ this, OF g refl le_refl] | 
| 65347 | 3282 | from g have "1 + length (coeffs f) - length (coeffs g) = 1 + degree f - degree g \<or> | 
| 3283 | degree f = 0 \<and> 1 + length (coeffs f) - length (coeffs g) = 0 \<and> f = 0" | |
| 3284 | by (cases "f = 0"; cases "coeffs g") (auto simp: degree_eq_length_coeffs) | |
| 3285 | note main' = main[OF this] | |
| 3286 | then show "r = 0 \<or> degree r < degree g" by auto | |
| 65346 | 3287 | show "smult (coeff g (degree g) ^ (Suc (degree f) - degree g)) f = g * q + r" | 
| 65347 | 3288 | by (subst main'[THEN conjunct2, symmetric], simp add: degree_eq_length_coeffs, | 
| 3289 | cases "f = 0"; cases "coeffs g", use g in auto) | |
| 64795 | 3290 | qed | 
| 65346 | 3291 | |
| 64795 | 3292 | definition "pseudo_mod_main lc r d dr n = snd (pseudo_divmod_main lc 0 r d dr n)" | 
| 3293 | ||
| 3294 | lemma snd_pseudo_divmod_main: | |
| 3295 | "snd (pseudo_divmod_main lc q r d dr n) = snd (pseudo_divmod_main lc q' r d dr n)" | |
| 65347 | 3296 | by (induct n arbitrary: q q' lc r d dr) (simp_all add: Let_def) | 
| 3297 | ||
| 3298 | definition pseudo_mod :: "'a::{comm_ring_1,semiring_1_no_zero_divisors} poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
 | |
| 3299 | where "pseudo_mod f g = snd (pseudo_divmod f g)" | |
| 65346 | 3300 | |
| 64795 | 3301 | lemma pseudo_mod: | 
| 65347 | 3302 |   fixes f g :: "'a::{comm_ring_1,semiring_1_no_zero_divisors} poly"
 | 
| 64795 | 3303 | defines "r \<equiv> pseudo_mod f g" | 
| 3304 | assumes g: "g \<noteq> 0" | |
| 65347 | 3305 | shows "\<exists>a q. a \<noteq> 0 \<and> smult a f = g * q + r" "r = 0 \<or> degree r < degree g" | 
| 65346 | 3306 | proof - | 
| 64795 | 3307 | let ?cg = "coeff g (degree g)" | 
| 3308 | let ?cge = "?cg ^ (Suc (degree f) - degree g)" | |
| 3309 | define a where "a = ?cge" | |
| 65347 | 3310 | from r_def[unfolded pseudo_mod_def] obtain q where pdm: "pseudo_divmod f g = (q, r)" | 
| 3311 | by (cases "pseudo_divmod f g") auto | |
| 65346 | 3312 | from pseudo_divmod[OF g pdm] have id: "smult a f = g * q + r" and "r = 0 \<or> degree r < degree g" | 
| 65347 | 3313 | by (auto simp: a_def) | 
| 64795 | 3314 | show "r = 0 \<or> degree r < degree g" by fact | 
| 65347 | 3315 | from g have "a \<noteq> 0" | 
| 3316 | by (auto simp: a_def) | |
| 3317 | with id show "\<exists>a q. a \<noteq> 0 \<and> smult a f = g * q + r" | |
| 3318 | by auto | |
| 64795 | 3319 | qed | 
| 65346 | 3320 | |
| 64795 | 3321 | lemma fst_pseudo_divmod_main_as_divide_poly_main: | 
| 3322 | assumes d: "d \<noteq> 0" | |
| 3323 | defines lc: "lc \<equiv> coeff d (degree d)" | |
| 65347 | 3324 | shows "fst (pseudo_divmod_main lc q r d dr n) = | 
| 3325 | divide_poly_main lc (smult (lc^n) q) (smult (lc^n) r) d dr n" | |
| 3326 | proof (induct n arbitrary: q r dr) | |
| 3327 | case 0 | |
| 3328 | then show ?case by simp | |
| 64795 | 3329 | next | 
| 3330 | case (Suc n) | |
| 65347 | 3331 | note lc0 = leading_coeff_neq_0[OF d, folded lc] | 
| 3332 | then have "pseudo_divmod_main lc q r d dr (Suc n) = | |
| 64795 | 3333 | pseudo_divmod_main lc (smult lc q + monom (coeff r dr) n) | 
| 3334 | (smult lc r - monom (coeff r dr) n * d) d (dr - 1) n" | |
| 3335 | by (simp add: Let_def ac_simps) | |
| 65347 | 3336 | also have "fst \<dots> = divide_poly_main lc | 
| 64795 | 3337 | (smult (lc^n) (smult lc q + monom (coeff r dr) n)) | 
| 3338 | (smult (lc^n) (smult lc r - monom (coeff r dr) n * d)) | |
| 3339 | d (dr - 1) n" | |
| 65347 | 3340 | by (simp only: Suc[unfolded divide_poly_main.simps Let_def]) | 
| 3341 | also have "\<dots> = divide_poly_main lc (smult (lc ^ Suc n) q) (smult (lc ^ Suc n) r) d dr (Suc n)" | |
| 3342 | unfolding smult_monom smult_distribs mult_smult_left[symmetric] | |
| 3343 | using lc0 by (simp add: Let_def ac_simps) | |
| 3344 | finally show ?case . | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3345 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3346 | |
| 64795 | 3347 | |
| 3348 | subsubsection \<open>Division in polynomials over fields\<close> | |
| 3349 | ||
| 3350 | lemma pseudo_divmod_field: | |
| 65347 | 3351 | fixes g :: "'a::field poly" | 
| 3352 | assumes g: "g \<noteq> 0" | |
| 3353 | and *: "pseudo_divmod f g = (q,r)" | |
| 64795 | 3354 | defines "c \<equiv> coeff g (degree g) ^ (Suc (degree f) - degree g)" | 
| 3355 | shows "f = g * smult (1/c) q + smult (1/c) r" | |
| 3356 | proof - | |
| 65347 | 3357 | from leading_coeff_neq_0[OF g] have c0: "c \<noteq> 0" | 
| 3358 | by (auto simp: c_def) | |
| 3359 | from pseudo_divmod(1)[OF g *, folded c_def] have "smult c f = g * q + r" | |
| 3360 | by auto | |
| 3361 | also have "smult (1 / c) \<dots> = g * smult (1 / c) q + smult (1 / c) r" | |
| 3362 | by (simp add: smult_add_right) | |
| 3363 | finally show ?thesis | |
| 3364 | using c0 by auto | |
| 64795 | 3365 | qed | 
| 3366 | ||
| 3367 | lemma divide_poly_main_field: | |
| 65347 | 3368 | fixes d :: "'a::field poly" | 
| 3369 | assumes d: "d \<noteq> 0" | |
| 64795 | 3370 | defines lc: "lc \<equiv> coeff d (degree d)" | 
| 65347 | 3371 | shows "divide_poly_main lc q r d dr n = | 
| 3372 | fst (pseudo_divmod_main lc (smult ((1 / lc)^n) q) (smult ((1 / lc)^n) r) d dr n)" | |
| 3373 | unfolding lc by (subst fst_pseudo_divmod_main_as_divide_poly_main) (auto simp: d power_one_over) | |
| 64795 | 3374 | |
| 3375 | lemma divide_poly_field: | |
| 65347 | 3376 | fixes f g :: "'a::field poly" | 
| 64795 | 3377 | defines "f' \<equiv> smult ((1 / coeff g (degree g)) ^ (Suc (degree f) - degree g)) f" | 
| 65347 | 3378 | shows "f div g = fst (pseudo_divmod f' g)" | 
| 64795 | 3379 | proof (cases "g = 0") | 
| 65347 | 3380 | case True | 
| 3381 | show ?thesis | |
| 3382 | unfolding divide_poly_def pseudo_divmod_def Let_def f'_def True | |
| 3383 | by (simp add: divide_poly_main_0) | |
| 64795 | 3384 | next | 
| 3385 | case False | |
| 65347 | 3386 | from leading_coeff_neq_0[OF False] have "degree f' = degree f" | 
| 3387 | by (auto simp: f'_def) | |
| 3388 | then show ?thesis | |
| 3389 | using length_coeffs_degree[of f'] length_coeffs_degree[of f] | |
| 3390 | unfolding divide_poly_def pseudo_divmod_def Let_def | |
| 3391 | divide_poly_main_field[OF False] | |
| 3392 | length_coeffs_degree[OF False] | |
| 3393 | f'_def | |
| 3394 | by force | |
| 64795 | 3395 | qed | 
| 3396 | ||
| 65347 | 3397 | instantiation poly :: ("{semidom_divide_unit_factor,idom_divide}") normalization_semidom
 | 
| 64795 | 3398 | begin | 
| 3399 | ||
| 3400 | definition unit_factor_poly :: "'a poly \<Rightarrow> 'a poly" | |
| 64848 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3401 | where "unit_factor_poly p = [:unit_factor (lead_coeff p):]" | 
| 64795 | 3402 | |
| 3403 | definition normalize_poly :: "'a poly \<Rightarrow> 'a poly" | |
| 64848 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3404 | where "normalize p = p div [:unit_factor (lead_coeff p):]" | 
| 64795 | 3405 | |
| 65347 | 3406 | instance | 
| 3407 | proof | |
| 64795 | 3408 | fix p :: "'a poly" | 
| 3409 | show "unit_factor p * normalize p = p" | |
| 64848 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3410 | proof (cases "p = 0") | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3411 | case True | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3412 | then show ?thesis | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3413 | by (simp add: unit_factor_poly_def normalize_poly_def) | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3414 | next | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3415 | case False | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3416 | then have "lead_coeff p \<noteq> 0" | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3417 | by simp | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3418 | then have *: "unit_factor (lead_coeff p) \<noteq> 0" | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3419 | using unit_factor_is_unit [of "lead_coeff p"] by auto | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3420 | then have "unit_factor (lead_coeff p) dvd 1" | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3421 | by (auto intro: unit_factor_is_unit) | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3422 | then have **: "unit_factor (lead_coeff p) dvd c" for c | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3423 | by (rule dvd_trans) simp | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3424 | have ***: "unit_factor (lead_coeff p) * (c div unit_factor (lead_coeff p)) = c" for c | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3425 | proof - | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3426 | from ** obtain b where "c = unit_factor (lead_coeff p) * b" .. | 
| 65347 | 3427 | with False * show ?thesis by simp | 
| 64848 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3428 | qed | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3429 | have "p div [:unit_factor (lead_coeff p):] = | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3430 | map_poly (\<lambda>c. c div unit_factor (lead_coeff p)) p" | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3431 | by (simp add: const_poly_dvd_iff div_const_poly_conv_map_poly **) | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3432 | then show ?thesis | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3433 | by (simp add: normalize_poly_def unit_factor_poly_def | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3434 | smult_conv_map_poly map_poly_map_poly o_def ***) | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3435 | qed | 
| 64795 | 3436 | next | 
| 3437 | fix p :: "'a poly" | |
| 3438 | assume "is_unit p" | |
| 64848 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3439 | then obtain c where p: "p = [:c:]" "c dvd 1" | 
| 64795 | 3440 | by (auto simp: is_unit_poly_iff) | 
| 64848 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3441 | then show "unit_factor p = p" | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3442 | by (simp add: unit_factor_poly_def monom_0 is_unit_unit_factor) | 
| 64795 | 3443 | next | 
| 65347 | 3444 | fix p :: "'a poly" | 
| 3445 | assume "p \<noteq> 0" | |
| 64848 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3446 | then show "is_unit (unit_factor p)" | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3447 | by (simp add: unit_factor_poly_def monom_0 is_unit_poly_iff unit_factor_is_unit) | 
| 64795 | 3448 | qed (simp_all add: normalize_poly_def unit_factor_poly_def monom_0 lead_coeff_mult unit_factor_mult) | 
| 3449 | ||
| 3450 | end | |
| 3451 | ||
| 65347 | 3452 | lemma normalize_poly_eq_map_poly: "normalize p = map_poly (\<lambda>x. x div unit_factor (lead_coeff p)) p" | 
| 64848 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3453 | proof - | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3454 | have "[:unit_factor (lead_coeff p):] dvd p" | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3455 | by (metis unit_factor_poly_def unit_factor_self) | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3456 | then show ?thesis | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3457 | by (simp add: normalize_poly_def div_const_poly_conv_map_poly) | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3458 | qed | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3459 | |
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3460 | lemma coeff_normalize [simp]: | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3461 | "coeff (normalize p) n = coeff p n div unit_factor (lead_coeff p)" | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3462 | by (simp add: normalize_poly_eq_map_poly coeff_map_poly) | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3463 | |
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3464 | class field_unit_factor = field + unit_factor + | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3465 | assumes unit_factor_field [simp]: "unit_factor = id" | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3466 | begin | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3467 | |
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3468 | subclass semidom_divide_unit_factor | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3469 | proof | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3470 | fix a | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3471 | assume "a \<noteq> 0" | 
| 65347 | 3472 | then have "1 = a * inverse a" by simp | 
| 64848 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3473 | then have "a dvd 1" .. | 
| 65347 | 3474 | then show "unit_factor a dvd 1" by simp | 
| 64848 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3475 | qed simp_all | 
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3476 | |
| 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3477 | end | 
| 64795 | 3478 | |
| 3479 | lemma unit_factor_pCons: | |
| 64848 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3480 | "unit_factor (pCons a p) = (if p = 0 then [:unit_factor a:] else unit_factor p)" | 
| 64795 | 3481 | by (simp add: unit_factor_poly_def) | 
| 3482 | ||
| 65347 | 3483 | lemma normalize_monom [simp]: "normalize (monom a n) = monom (normalize a) n" | 
| 64848 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3484 | by (cases "a = 0") (simp_all add: map_poly_monom normalize_poly_eq_map_poly degree_monom_eq) | 
| 64795 | 3485 | |
| 65347 | 3486 | lemma unit_factor_monom [simp]: "unit_factor (monom a n) = [:unit_factor a:]" | 
| 64795 | 3487 | by (cases "a = 0") (simp_all add: unit_factor_poly_def degree_monom_eq) | 
| 3488 | ||
| 3489 | lemma normalize_const_poly: "normalize [:c:] = [:normalize c:]" | |
| 64848 
c50db2128048
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
 haftmann parents: 
64811diff
changeset | 3490 | by (simp add: normalize_poly_eq_map_poly map_poly_pCons) | 
| 64795 | 3491 | |
| 3492 | lemma normalize_smult: "normalize (smult c p) = smult (normalize c) (normalize p)" | |
| 3493 | proof - | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3494 | have "smult c p = [:c:] * p" by simp | 
| 64795 | 3495 | also have "normalize \<dots> = smult (normalize c) (normalize p)" | 
| 3496 | by (subst normalize_mult) (simp add: normalize_const_poly) | |
| 3497 | finally show ?thesis . | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3498 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3499 | |
| 64795 | 3500 | inductive eucl_rel_poly :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<times> 'a poly \<Rightarrow> bool" | 
| 65347 | 3501 | where | 
| 3502 | eucl_rel_poly_by0: "eucl_rel_poly x 0 (0, x)" | |
| 64795 | 3503 | | eucl_rel_poly_dividesI: "y \<noteq> 0 \<Longrightarrow> x = q * y \<Longrightarrow> eucl_rel_poly x y (q, 0)" | 
| 65347 | 3504 | | eucl_rel_poly_remainderI: | 
| 3505 | "y \<noteq> 0 \<Longrightarrow> degree r < degree y \<Longrightarrow> x = q * y + r \<Longrightarrow> eucl_rel_poly x y (q, r)" | |
| 65346 | 3506 | |
| 64795 | 3507 | lemma eucl_rel_poly_iff: | 
| 3508 | "eucl_rel_poly x y (q, r) \<longleftrightarrow> | |
| 65347 | 3509 | x = q * y + r \<and> (if y = 0 then q = 0 else r = 0 \<or> degree r < degree y)" | 
| 64795 | 3510 | by (auto elim: eucl_rel_poly.cases | 
| 65347 | 3511 | intro: eucl_rel_poly_by0 eucl_rel_poly_dividesI eucl_rel_poly_remainderI) | 
| 3512 | ||
| 3513 | lemma eucl_rel_poly_0: "eucl_rel_poly 0 y (0, 0)" | |
| 3514 | by (simp add: eucl_rel_poly_iff) | |
| 3515 | ||
| 3516 | lemma eucl_rel_poly_by_0: "eucl_rel_poly x 0 (0, x)" | |
| 3517 | by (simp add: eucl_rel_poly_iff) | |
| 64795 | 3518 | |
| 3519 | lemma eucl_rel_poly_pCons: | |
| 3520 | assumes rel: "eucl_rel_poly x y (q, r)" | |
| 3521 | assumes y: "y \<noteq> 0" | |
| 3522 | assumes b: "b = coeff (pCons a r) (degree y) / coeff y (degree y)" | |
| 3523 | shows "eucl_rel_poly (pCons a x) y (pCons b q, pCons a r - smult b y)" | |
| 3524 | (is "eucl_rel_poly ?x y (?q, ?r)") | |
| 3525 | proof - | |
| 65347 | 3526 | from assms have x: "x = q * y + r" and r: "r = 0 \<or> degree r < degree y" | 
| 3527 | by (simp_all add: eucl_rel_poly_iff) | |
| 3528 | from b x have "?x = ?q * y + ?r" by simp | |
| 3529 | moreover | |
| 3530 | have "?r = 0 \<or> degree ?r < degree y" | |
| 64795 | 3531 | proof (rule eq_zero_or_degree_less) | 
| 3532 | show "degree ?r \<le> degree y" | |
| 3533 | proof (rule degree_diff_le) | |
| 65347 | 3534 | from r show "degree (pCons a r) \<le> degree y" | 
| 3535 | by auto | |
| 64795 | 3536 | show "degree (smult b y) \<le> degree y" | 
| 3537 | by (rule degree_smult_le) | |
| 3538 | qed | |
| 65347 | 3539 | from \<open>y \<noteq> 0\<close> show "coeff ?r (degree y) = 0" | 
| 3540 | by (simp add: b) | |
| 64795 | 3541 | qed | 
| 65347 | 3542 | ultimately show ?thesis | 
| 3543 | unfolding eucl_rel_poly_iff using \<open>y \<noteq> 0\<close> by simp | |
| 64795 | 3544 | qed | 
| 3545 | ||
| 3546 | lemma eucl_rel_poly_exists: "\<exists>q r. eucl_rel_poly x y (q, r)" | |
| 65347 | 3547 | apply (cases "y = 0") | 
| 3548 | apply (fast intro!: eucl_rel_poly_by_0) | |
| 3549 | apply (induct x) | |
| 3550 | apply (fast intro!: eucl_rel_poly_0) | |
| 3551 | apply (fast intro!: eucl_rel_poly_pCons) | |
| 3552 | done | |
| 64795 | 3553 | |
| 3554 | lemma eucl_rel_poly_unique: | |
| 3555 | assumes 1: "eucl_rel_poly x y (q1, r1)" | |
| 3556 | assumes 2: "eucl_rel_poly x y (q2, r2)" | |
| 3557 | shows "q1 = q2 \<and> r1 = r2" | |
| 3558 | proof (cases "y = 0") | |
| 65347 | 3559 | assume "y = 0" | 
| 3560 | with assms show ?thesis | |
| 64795 | 3561 | by (simp add: eucl_rel_poly_iff) | 
| 3562 | next | |
| 3563 | assume [simp]: "y \<noteq> 0" | |
| 3564 | from 1 have q1: "x = q1 * y + r1" and r1: "r1 = 0 \<or> degree r1 < degree y" | |
| 3565 | unfolding eucl_rel_poly_iff by simp_all | |
| 3566 | from 2 have q2: "x = q2 * y + r2" and r2: "r2 = 0 \<or> degree r2 < degree y" | |
| 3567 | unfolding eucl_rel_poly_iff by simp_all | |
| 3568 | from q1 q2 have q3: "(q1 - q2) * y = r2 - r1" | |
| 3569 | by (simp add: algebra_simps) | |
| 3570 | from r1 r2 have r3: "(r2 - r1) = 0 \<or> degree (r2 - r1) < degree y" | |
| 3571 | by (auto intro: degree_diff_less) | |
| 3572 | show "q1 = q2 \<and> r1 = r2" | |
| 65347 | 3573 | proof (rule classical) | 
| 3574 | assume "\<not> ?thesis" | |
| 64795 | 3575 | with q3 have "q1 \<noteq> q2" and "r1 \<noteq> r2" by auto | 
| 3576 | with r3 have "degree (r2 - r1) < degree y" by simp | |
| 3577 | also have "degree y \<le> degree (q1 - q2) + degree y" by simp | |
| 65347 | 3578 | also from \<open>q1 \<noteq> q2\<close> have "\<dots> = degree ((q1 - q2) * y)" | 
| 3579 | by (simp add: degree_mult_eq) | |
| 3580 | also from q3 have "\<dots> = degree (r2 - r1)" | |
| 3581 | by simp | |
| 64795 | 3582 | finally have "degree (r2 - r1) < degree (r2 - r1)" . | 
| 65347 | 3583 | then show ?thesis by simp | 
| 64795 | 3584 | qed | 
| 3585 | qed | |
| 3586 | ||
| 3587 | lemma eucl_rel_poly_0_iff: "eucl_rel_poly 0 y (q, r) \<longleftrightarrow> q = 0 \<and> r = 0" | |
| 65347 | 3588 | by (auto dest: eucl_rel_poly_unique intro: eucl_rel_poly_0) | 
| 64795 | 3589 | |
| 3590 | lemma eucl_rel_poly_by_0_iff: "eucl_rel_poly x 0 (q, r) \<longleftrightarrow> q = 0 \<and> r = x" | |
| 65347 | 3591 | by (auto dest: eucl_rel_poly_unique intro: eucl_rel_poly_by_0) | 
| 64795 | 3592 | |
| 3593 | lemmas eucl_rel_poly_unique_div = eucl_rel_poly_unique [THEN conjunct1] | |
| 3594 | ||
| 3595 | lemmas eucl_rel_poly_unique_mod = eucl_rel_poly_unique [THEN conjunct2] | |
| 3596 | ||
| 64861 | 3597 | instantiation poly :: (field) semidom_modulo | 
| 64795 | 3598 | begin | 
| 65346 | 3599 | |
| 64861 | 3600 | definition modulo_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" | 
| 65347 | 3601 | where mod_poly_def: "f mod g = | 
| 3602 | (if g = 0 then f else pseudo_mod (smult ((1 / lead_coeff g) ^ (Suc (degree f) - degree g)) f) g)" | |
| 3603 | ||
| 3604 | instance | |
| 3605 | proof | |
| 64861 | 3606 | fix x y :: "'a poly" | 
| 3607 | show "x div y * y + x mod y = x" | |
| 3608 | proof (cases "y = 0") | |
| 65347 | 3609 | case True | 
| 3610 | then show ?thesis | |
| 64861 | 3611 | by (simp add: divide_poly_0 mod_poly_def) | 
| 64795 | 3612 | next | 
| 3613 | case False | |
| 64861 | 3614 | then have "pseudo_divmod (smult ((1 / lead_coeff y) ^ (Suc (degree x) - degree y)) x) y = | 
| 65347 | 3615 | (x div y, x mod y)" | 
| 64861 | 3616 | by (simp add: divide_poly_field mod_poly_def pseudo_mod_def) | 
| 65347 | 3617 | with False pseudo_divmod [OF False this] show ?thesis | 
| 64861 | 3618 | by (simp add: power_mult_distrib [symmetric] ac_simps) | 
| 64795 | 3619 | qed | 
| 64861 | 3620 | qed | 
| 65346 | 3621 | |
| 64861 | 3622 | end | 
| 65346 | 3623 | |
| 64861 | 3624 | lemma eucl_rel_poly: "eucl_rel_poly x y (x div y, x mod y)" | 
| 65347 | 3625 | unfolding eucl_rel_poly_iff | 
| 3626 | proof | |
| 64861 | 3627 | show "x = x div y * y + x mod y" | 
| 3628 | by (simp add: div_mult_mod_eq) | |
| 64795 | 3629 | show "if y = 0 then x div y = 0 else x mod y = 0 \<or> degree (x mod y) < degree y" | 
| 3630 | proof (cases "y = 0") | |
| 65347 | 3631 | case True | 
| 3632 | then show ?thesis by auto | |
| 64795 | 3633 | next | 
| 3634 | case False | |
| 65347 | 3635 | with pseudo_mod[OF this] show ?thesis | 
| 3636 | by (simp add: mod_poly_def) | |
| 64795 | 3637 | qed | 
| 3638 | qed | |
| 3639 | ||
| 65347 | 3640 | lemma div_poly_eq: "eucl_rel_poly x y (q, r) \<Longrightarrow> x div y = q" | 
| 3641 | for x :: "'a::field poly" | |
| 3642 | by (rule eucl_rel_poly_unique_div [OF eucl_rel_poly]) | |
| 3643 | ||
| 3644 | lemma mod_poly_eq: "eucl_rel_poly x y (q, r) \<Longrightarrow> x mod y = r" | |
| 3645 | for x :: "'a::field poly" | |
| 64861 | 3646 | by (rule eucl_rel_poly_unique_mod [OF eucl_rel_poly]) | 
| 3647 | ||
| 66806 
a4e82b58d833
abolished (semi)ring_div in favour of euclidean_(semi)ring_cancel
 haftmann parents: 
66805diff
changeset | 3648 | instance poly :: (field) idom_modulo .. | 
| 64795 | 3649 | |
| 64811 | 3650 | lemma div_pCons_eq: | 
| 65347 | 3651 | "pCons a p div q = | 
| 3652 | (if q = 0 then 0 | |
| 3653 | else pCons (coeff (pCons a (p mod q)) (degree q) / lead_coeff q) (p div q))" | |
| 64811 | 3654 | using eucl_rel_poly_pCons [OF eucl_rel_poly _ refl, of q a p] | 
| 3655 | by (auto intro: div_poly_eq) | |
| 3656 | ||
| 3657 | lemma mod_pCons_eq: | |
| 65347 | 3658 | "pCons a p mod q = | 
| 3659 | (if q = 0 then pCons a p | |
| 3660 | else pCons a (p mod q) - smult (coeff (pCons a (p mod q)) (degree q) / lead_coeff q) q)" | |
| 64811 | 3661 | using eucl_rel_poly_pCons [OF eucl_rel_poly _ refl, of q a p] | 
| 3662 | by (auto intro: mod_poly_eq) | |
| 3663 | ||
| 3664 | lemma div_mod_fold_coeffs: | |
| 65347 | 3665 | "(p div q, p mod q) = | 
| 3666 | (if q = 0 then (0, p) | |
| 3667 | else | |
| 3668 | fold_coeffs | |
| 3669 | (\<lambda>a (s, r). | |
| 3670 | let b = coeff (pCons a r) (degree q) / coeff q (degree q) | |
| 3671 | in (pCons b s, pCons a r - smult b q)) p (0, 0))" | |
| 3672 | by (rule sym, induct p) (auto simp: div_pCons_eq mod_pCons_eq Let_def) | |
| 3673 | ||
| 3674 | lemma degree_mod_less: "y \<noteq> 0 \<Longrightarrow> x mod y = 0 \<or> degree (x mod y) < degree y" | |
| 3675 | using eucl_rel_poly [of x y] unfolding eucl_rel_poly_iff by simp | |
| 64795 | 3676 | |
| 3677 | lemma degree_mod_less': "b \<noteq> 0 \<Longrightarrow> a mod b \<noteq> 0 \<Longrightarrow> degree (a mod b) < degree b" | |
| 3678 | using degree_mod_less[of b a] by auto | |
| 3679 | ||
| 65347 | 3680 | lemma div_poly_less: | 
| 3681 | fixes x :: "'a::field poly" | |
| 3682 | assumes "degree x < degree y" | |
| 3683 | shows "x div y = 0" | |
| 64795 | 3684 | proof - | 
| 65347 | 3685 | from assms have "eucl_rel_poly x y (0, x)" | 
| 64795 | 3686 | by (simp add: eucl_rel_poly_iff) | 
| 65347 | 3687 | then show "x div y = 0" | 
| 3688 | by (rule div_poly_eq) | |
| 64795 | 3689 | qed | 
| 3690 | ||
| 65347 | 3691 | lemma mod_poly_less: | 
| 3692 | assumes "degree x < degree y" | |
| 3693 | shows "x mod y = x" | |
| 64795 | 3694 | proof - | 
| 65347 | 3695 | from assms have "eucl_rel_poly x y (0, x)" | 
| 64795 | 3696 | by (simp add: eucl_rel_poly_iff) | 
| 65347 | 3697 | then show "x mod y = x" | 
| 3698 | by (rule mod_poly_eq) | |
| 64795 | 3699 | qed | 
| 3700 | ||
| 3701 | lemma eucl_rel_poly_smult_left: | |
| 65347 | 3702 | "eucl_rel_poly x y (q, r) \<Longrightarrow> eucl_rel_poly (smult a x) y (smult a q, smult a r)" | 
| 3703 | by (simp add: eucl_rel_poly_iff smult_add_right) | |
| 3704 | ||
| 3705 | lemma div_smult_left: "(smult a x) div y = smult a (x div y)" | |
| 3706 | for x y :: "'a::field poly" | |
| 64795 | 3707 | by (rule div_poly_eq, rule eucl_rel_poly_smult_left, rule eucl_rel_poly) | 
| 3708 | ||
| 3709 | lemma mod_smult_left: "(smult a x) mod y = smult a (x mod y)" | |
| 3710 | by (rule mod_poly_eq, rule eucl_rel_poly_smult_left, rule eucl_rel_poly) | |
| 3711 | ||
| 65347 | 3712 | lemma poly_div_minus_left [simp]: "(- x) div y = - (x div y)" | 
| 3713 | for x y :: "'a::field poly" | |
| 64795 | 3714 | using div_smult_left [of "- 1::'a"] by simp | 
| 3715 | ||
| 65347 | 3716 | lemma poly_mod_minus_left [simp]: "(- x) mod y = - (x mod y)" | 
| 3717 | for x y :: "'a::field poly" | |
| 64795 | 3718 | using mod_smult_left [of "- 1::'a"] by simp | 
| 3719 | ||
| 3720 | lemma eucl_rel_poly_add_left: | |
| 3721 | assumes "eucl_rel_poly x y (q, r)" | |
| 3722 | assumes "eucl_rel_poly x' y (q', r')" | |
| 3723 | shows "eucl_rel_poly (x + x') y (q + q', r + r')" | |
| 3724 | using assms unfolding eucl_rel_poly_iff | |
| 65347 | 3725 | by (auto simp: algebra_simps degree_add_less) | 
| 3726 | ||
| 3727 | lemma poly_div_add_left: "(x + y) div z = x div z + y div z" | |
| 3728 | for x y z :: "'a::field poly" | |
| 64795 | 3729 | using eucl_rel_poly_add_left [OF eucl_rel_poly eucl_rel_poly] | 
| 3730 | by (rule div_poly_eq) | |
| 3731 | ||
| 65347 | 3732 | lemma poly_mod_add_left: "(x + y) mod z = x mod z + y mod z" | 
| 3733 | for x y z :: "'a::field poly" | |
| 64795 | 3734 | using eucl_rel_poly_add_left [OF eucl_rel_poly eucl_rel_poly] | 
| 3735 | by (rule mod_poly_eq) | |
| 3736 | ||
| 65347 | 3737 | lemma poly_div_diff_left: "(x - y) div z = x div z - y div z" | 
| 3738 | for x y z :: "'a::field poly" | |
| 64795 | 3739 | by (simp only: diff_conv_add_uminus poly_div_add_left poly_div_minus_left) | 
| 3740 | ||
| 65347 | 3741 | lemma poly_mod_diff_left: "(x - y) mod z = x mod z - y mod z" | 
| 3742 | for x y z :: "'a::field poly" | |
| 64795 | 3743 | by (simp only: diff_conv_add_uminus poly_mod_add_left poly_mod_minus_left) | 
| 3744 | ||
| 3745 | lemma eucl_rel_poly_smult_right: | |
| 65347 | 3746 | "a \<noteq> 0 \<Longrightarrow> eucl_rel_poly x y (q, r) \<Longrightarrow> eucl_rel_poly x (smult a y) (smult (inverse a) q, r)" | 
| 3747 | by (simp add: eucl_rel_poly_iff) | |
| 3748 | ||
| 3749 | lemma div_smult_right: "a \<noteq> 0 \<Longrightarrow> x div (smult a y) = smult (inverse a) (x div y)" | |
| 3750 | for x y :: "'a::field poly" | |
| 64795 | 3751 | by (rule div_poly_eq, erule eucl_rel_poly_smult_right, rule eucl_rel_poly) | 
| 3752 | ||
| 3753 | lemma mod_smult_right: "a \<noteq> 0 \<Longrightarrow> x mod (smult a y) = x mod y" | |
| 3754 | by (rule mod_poly_eq, erule eucl_rel_poly_smult_right, rule eucl_rel_poly) | |
| 3755 | ||
| 65347 | 3756 | lemma poly_div_minus_right [simp]: "x div (- y) = - (x div y)" | 
| 3757 | for x y :: "'a::field poly" | |
| 64795 | 3758 | using div_smult_right [of "- 1::'a"] by (simp add: nonzero_inverse_minus_eq) | 
| 3759 | ||
| 65347 | 3760 | lemma poly_mod_minus_right [simp]: "x mod (- y) = x mod y" | 
| 3761 | for x y :: "'a::field poly" | |
| 64795 | 3762 | using mod_smult_right [of "- 1::'a"] by simp | 
| 3763 | ||
| 3764 | lemma eucl_rel_poly_mult: | |
| 65347 | 3765 | "eucl_rel_poly x y (q, r) \<Longrightarrow> eucl_rel_poly q z (q', r') \<Longrightarrow> | 
| 3766 | eucl_rel_poly x (y * z) (q', y * r' + r)" | |
| 3767 | apply (cases "z = 0", simp add: eucl_rel_poly_iff) | |
| 3768 | apply (cases "y = 0", simp add: eucl_rel_poly_by_0_iff eucl_rel_poly_0_iff) | |
| 3769 | apply (cases "r = 0") | |
| 3770 | apply (cases "r' = 0") | |
| 3771 | apply (simp add: eucl_rel_poly_iff) | |
| 3772 | apply (simp add: eucl_rel_poly_iff field_simps degree_mult_eq) | |
| 3773 | apply (cases "r' = 0") | |
| 3774 | apply (simp add: eucl_rel_poly_iff degree_mult_eq) | |
| 3775 | apply (simp add: eucl_rel_poly_iff field_simps) | |
| 3776 | apply (simp add: degree_mult_eq degree_add_less) | |
| 3777 | done | |
| 3778 | ||
| 3779 | lemma poly_div_mult_right: "x div (y * z) = (x div y) div z" | |
| 3780 | for x y z :: "'a::field poly" | |
| 64795 | 3781 | by (rule div_poly_eq, rule eucl_rel_poly_mult, (rule eucl_rel_poly)+) | 
| 3782 | ||
| 65347 | 3783 | lemma poly_mod_mult_right: "x mod (y * z) = y * (x div y mod z) + x mod y" | 
| 3784 | for x y z :: "'a::field poly" | |
| 64795 | 3785 | by (rule mod_poly_eq, rule eucl_rel_poly_mult, (rule eucl_rel_poly)+) | 
| 3786 | ||
| 3787 | lemma mod_pCons: | |
| 65347 | 3788 | fixes a :: "'a::field" | 
| 3789 | and x y :: "'a::field poly" | |
| 64795 | 3790 | assumes y: "y \<noteq> 0" | 
| 65347 | 3791 | defines "b \<equiv> coeff (pCons a (x mod y)) (degree y) / coeff y (degree y)" | 
| 3792 | shows "(pCons a x) mod y = pCons a (x mod y) - smult b y" | |
| 3793 | unfolding b_def | |
| 3794 | by (rule mod_poly_eq, rule eucl_rel_poly_pCons [OF eucl_rel_poly y refl]) | |
| 64795 | 3795 | |
| 65346 | 3796 | |
| 64795 | 3797 | subsubsection \<open>List-based versions for fast implementation\<close> | 
| 3798 | (* Subsection by: | |
| 3799 | Sebastiaan Joosten | |
| 3800 | René Thiemann | |
| 3801 | Akihisa Yamada | |
| 3802 | *) | |
| 65347 | 3803 | fun minus_poly_rev_list :: "'a :: group_add list \<Rightarrow> 'a list \<Rightarrow> 'a list" | 
| 3804 | where | |
| 3805 | "minus_poly_rev_list (x # xs) (y # ys) = (x - y) # (minus_poly_rev_list xs ys)" | |
| 3806 | | "minus_poly_rev_list xs [] = xs" | |
| 3807 | | "minus_poly_rev_list [] (y # ys) = []" | |
| 3808 | ||
| 3809 | fun pseudo_divmod_main_list :: | |
| 3810 | "'a::comm_ring_1 \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> nat \<Rightarrow> 'a list \<times> 'a list" | |
| 3811 | where | |
| 3812 | "pseudo_divmod_main_list lc q r d (Suc n) = | |
| 3813 | (let | |
| 69064 
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
 nipkow parents: 
69022diff
changeset | 3814 | rr = map ((*) lc) r; | 
| 65347 | 3815 | a = hd r; | 
| 69064 
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
 nipkow parents: 
69022diff
changeset | 3816 | qqq = cCons a (map ((*) lc) q); | 
| 
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
 nipkow parents: 
69022diff
changeset | 3817 | rrr = tl (if a = 0 then rr else minus_poly_rev_list rr (map ((*) a) d)) | 
| 65347 | 3818 | in pseudo_divmod_main_list lc qqq rrr d n)" | 
| 3819 | | "pseudo_divmod_main_list lc q r d 0 = (q, r)" | |
| 3820 | ||
| 3821 | fun pseudo_mod_main_list :: "'a::comm_ring_1 \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> nat \<Rightarrow> 'a list" | |
| 3822 | where | |
| 3823 | "pseudo_mod_main_list lc r d (Suc n) = | |
| 3824 | (let | |
| 69064 
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
 nipkow parents: 
69022diff
changeset | 3825 | rr = map ((*) lc) r; | 
| 65347 | 3826 | a = hd r; | 
| 69064 
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
 nipkow parents: 
69022diff
changeset | 3827 | rrr = tl (if a = 0 then rr else minus_poly_rev_list rr (map ((*) a) d)) | 
| 65347 | 3828 | in pseudo_mod_main_list lc rrr d n)" | 
| 3829 | | "pseudo_mod_main_list lc r d 0 = r" | |
| 3830 | ||
| 3831 | ||
| 3832 | fun divmod_poly_one_main_list :: | |
| 3833 | "'a::comm_ring_1 list \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> nat \<Rightarrow> 'a list \<times> 'a list" | |
| 3834 | where | |
| 3835 | "divmod_poly_one_main_list q r d (Suc n) = | |
| 3836 | (let | |
| 3837 | a = hd r; | |
| 3838 | qqq = cCons a q; | |
| 69064 
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
 nipkow parents: 
69022diff
changeset | 3839 | rr = tl (if a = 0 then r else minus_poly_rev_list r (map ((*) a) d)) | 
| 65347 | 3840 | in divmod_poly_one_main_list qqq rr d n)" | 
| 3841 | | "divmod_poly_one_main_list q r d 0 = (q, r)" | |
| 3842 | ||
| 3843 | fun mod_poly_one_main_list :: "'a::comm_ring_1 list \<Rightarrow> 'a list \<Rightarrow> nat \<Rightarrow> 'a list" | |
| 3844 | where | |
| 3845 | "mod_poly_one_main_list r d (Suc n) = | |
| 3846 | (let | |
| 3847 | a = hd r; | |
| 69064 
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
 nipkow parents: 
69022diff
changeset | 3848 | rr = tl (if a = 0 then r else minus_poly_rev_list r (map ((*) a) d)) | 
| 65347 | 3849 | in mod_poly_one_main_list rr d n)" | 
| 3850 | | "mod_poly_one_main_list r d 0 = r" | |
| 3851 | ||
| 3852 | definition pseudo_divmod_list :: "'a::comm_ring_1 list \<Rightarrow> 'a list \<Rightarrow> 'a list \<times> 'a list" | |
| 3853 | where "pseudo_divmod_list p q = | |
| 3854 | (if q = [] then ([], p) | |
| 3855 | else | |
| 3856 | (let rq = rev q; | |
| 3857 | (qu,re) = pseudo_divmod_main_list (hd rq) [] (rev p) rq (1 + length p - length q) | |
| 3858 | in (qu, rev re)))" | |
| 3859 | ||
| 3860 | definition pseudo_mod_list :: "'a::comm_ring_1 list \<Rightarrow> 'a list \<Rightarrow> 'a list" | |
| 3861 | where "pseudo_mod_list p q = | |
| 3862 | (if q = [] then p | |
| 3863 | else | |
| 3864 | (let | |
| 3865 | rq = rev q; | |
| 3866 | re = pseudo_mod_main_list (hd rq) (rev p) rq (1 + length p - length q) | |
| 3867 | in rev re))" | |
| 3868 | ||
| 69064 
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
 nipkow parents: 
69022diff
changeset | 3869 | lemma minus_zero_does_nothing: "minus_poly_rev_list x (map ((*) 0) y) = x" | 
| 65347 | 3870 | for x :: "'a::ring list" | 
| 3871 | by (induct x y rule: minus_poly_rev_list.induct) auto | |
| 3872 | ||
| 3873 | lemma length_minus_poly_rev_list [simp]: "length (minus_poly_rev_list xs ys) = length xs" | |
| 3874 | by (induct xs ys rule: minus_poly_rev_list.induct) auto | |
| 64795 | 3875 | |
| 3876 | lemma if_0_minus_poly_rev_list: | |
| 69064 
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
 nipkow parents: 
69022diff
changeset | 3877 | "(if a = 0 then x else minus_poly_rev_list x (map ((*) a) y)) = | 
| 
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
 nipkow parents: 
69022diff
changeset | 3878 | minus_poly_rev_list x (map ((*) a) y)" | 
| 65347 | 3879 | for a :: "'a::ring" | 
| 3880 | by(cases "a = 0") (simp_all add: minus_zero_does_nothing) | |
| 3881 | ||
| 3882 | lemma Poly_append: "Poly (a @ b) = Poly a + monom 1 (length a) * Poly b" | |
| 3883 | for a :: "'a::comm_semiring_1 list" | |
| 3884 | by (induct a) (auto simp: monom_0 monom_Suc) | |
| 3885 | ||
| 3886 | lemma minus_poly_rev_list: "length p \<ge> length q \<Longrightarrow> | |
| 3887 | Poly (rev (minus_poly_rev_list (rev p) (rev q))) = | |
| 3888 | Poly p - monom 1 (length p - length q) * Poly q" | |
| 3889 | for p q :: "'a :: comm_ring_1 list" | |
| 64795 | 3890 | proof (induct "rev p" "rev q" arbitrary: p q rule: minus_poly_rev_list.induct) | 
| 65346 | 3891 | case (1 x xs y ys) | 
| 65347 | 3892 | then have "length (rev q) \<le> length (rev p)" | 
| 3893 | by simp | |
| 3894 | from this[folded 1(2,3)] have ys_xs: "length ys \<le> length xs" | |
| 3895 | by simp | |
| 3896 | then have *: "Poly (rev (minus_poly_rev_list xs ys)) = | |
| 3897 | Poly (rev xs) - monom 1 (length xs - length ys) * Poly (rev ys)" | |
| 3898 | by (subst "1.hyps"(1)[of "rev xs" "rev ys", unfolded rev_rev_ident length_rev]) auto | |
| 3899 | have "Poly p - monom 1 (length p - length q) * Poly q = | |
| 3900 | Poly (rev (rev p)) - monom 1 (length (rev (rev p)) - length (rev (rev q))) * Poly (rev (rev q))" | |
| 64795 | 3901 | by simp | 
| 65347 | 3902 | also have "\<dots> = | 
| 3903 | Poly (rev (x # xs)) - monom 1 (length (x # xs) - length (y # ys)) * Poly (rev (y # ys))" | |
| 64795 | 3904 | unfolding 1(2,3) by simp | 
| 65347 | 3905 | also from ys_xs have "\<dots> = | 
| 3906 | Poly (rev xs) + monom x (length xs) - | |
| 3907 | (monom 1 (length xs - length ys) * Poly (rev ys) + monom y (length xs))" | |
| 3908 | by (simp add: Poly_append distrib_left mult_monom smult_monom) | |
| 64795 | 3909 | also have "\<dots> = Poly (rev (minus_poly_rev_list xs ys)) + monom (x - y) (length xs)" | 
| 65347 | 3910 | unfolding * diff_monom[symmetric] by simp | 
| 64795 | 3911 | finally show ?case | 
| 65347 | 3912 | by (simp add: 1(2,3)[symmetric] smult_monom Poly_append) | 
| 64795 | 3913 | qed auto | 
| 3914 | ||
| 3915 | lemma smult_monom_mult: "smult a (monom b n * f) = monom (a * b) n * f" | |
| 3916 | using smult_monom [of a _ n] by (metis mult_smult_left) | |
| 3917 | ||
| 3918 | lemma head_minus_poly_rev_list: | |
| 65347 | 3919 | "length d \<le> length r \<Longrightarrow> d \<noteq> [] \<Longrightarrow> | 
| 69064 
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
 nipkow parents: 
69022diff
changeset | 3920 | hd (minus_poly_rev_list (map ((*) (last d)) r) (map ((*) (hd r)) (rev d))) = 0" | 
| 65347 | 3921 | for d r :: "'a::comm_ring list" | 
| 3922 | proof (induct r) | |
| 3923 | case Nil | |
| 3924 | then show ?case by simp | |
| 3925 | next | |
| 64795 | 3926 | case (Cons a rs) | 
| 65347 | 3927 | then show ?case by (cases "rev d") (simp_all add: ac_simps) | 
| 3928 | qed | |
| 64795 | 3929 | |
| 69064 
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
 nipkow parents: 
69022diff
changeset | 3930 | lemma Poly_map: "Poly (map ((*) a) p) = smult a (Poly p)" | 
| 64795 | 3931 | proof (induct p) | 
| 65347 | 3932 | case Nil | 
| 3933 | then show ?case by simp | |
| 3934 | next | |
| 3935 | case (Cons x xs) | |
| 3936 | then show ?case by (cases "Poly xs = 0") auto | |
| 3937 | qed | |
| 64795 | 3938 | |
| 3939 | lemma last_coeff_is_hd: "xs \<noteq> [] \<Longrightarrow> coeff (Poly xs) (length xs - 1) = hd (rev xs)" | |
| 3940 | by (simp_all add: hd_conv_nth rev_nth nth_default_nth nth_append) | |
| 3941 | ||
| 65347 | 3942 | lemma pseudo_divmod_main_list_invar: | 
| 3943 | assumes leading_nonzero: "last d \<noteq> 0" | |
| 3944 | and lc: "last d = lc" | |
| 3945 | and "d \<noteq> []" | |
| 3946 | and "pseudo_divmod_main_list lc q (rev r) (rev d) n = (q', rev r')" | |
| 3947 | and "n = 1 + length r - length d" | |
| 3948 | shows "pseudo_divmod_main lc (monom 1 n * Poly q) (Poly r) (Poly d) (length r - 1) n = | |
| 3949 | (Poly q', Poly r')" | |
| 3950 | using assms(4-) | |
| 3951 | proof (induct n arbitrary: r q) | |
| 3952 | case (Suc n) | |
| 3953 | from Suc.prems have *: "\<not> Suc (length r) \<le> length d" | |
| 3954 | by simp | |
| 3955 | with \<open>d \<noteq> []\<close> have "r \<noteq> []" | |
| 3956 | using Suc_leI length_greater_0_conv list.size(3) by fastforce | |
| 64795 | 3957 | let ?a = "(hd (rev r))" | 
| 69064 
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
 nipkow parents: 
69022diff
changeset | 3958 | let ?rr = "map ((*) lc) (rev r)" | 
| 
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
 nipkow parents: 
69022diff
changeset | 3959 | let ?rrr = "rev (tl (minus_poly_rev_list ?rr (map ((*) ?a) (rev d))))" | 
| 
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
 nipkow parents: 
69022diff
changeset | 3960 | let ?qq = "cCons ?a (map ((*) lc) q)" | 
| 65347 | 3961 | from * Suc(3) have n: "n = (1 + length r - length d - 1)" | 
| 3962 | by simp | |
| 3963 | from * have rr_val:"(length ?rrr) = (length r - 1)" | |
| 3964 | by auto | |
| 3965 | with \<open>r \<noteq> []\<close> * have rr_smaller: "(1 + length r - length d - 1) = (1 + length ?rrr - length d)" | |
| 3966 | by auto | |
| 3967 | from * have id: "Suc (length r) - length d = Suc (length r - length d)" | |
| 3968 | by auto | |
| 3969 | from Suc.prems * | |
| 64795 | 3970 | have "pseudo_divmod_main_list lc ?qq (rev ?rrr) (rev d) (1 + length r - length d - 1) = (q', rev r')" | 
| 65347 | 3971 | by (simp add: Let_def if_0_minus_poly_rev_list id) | 
| 3972 | with n have v: "pseudo_divmod_main_list lc ?qq (rev ?rrr) (rev d) n = (q', rev r')" | |
| 3973 | by auto | |
| 3974 | from * have sucrr:"Suc (length r) - length d = Suc (length r - length d)" | |
| 3975 | using Suc_diff_le not_less_eq_eq by blast | |
| 3976 | from Suc(3) \<open>r \<noteq> []\<close> have n_ok : "n = 1 + (length ?rrr) - length d" | |
| 3977 | by simp | |
| 65346 | 3978 | have cong: "\<And>x1 x2 x3 x4 y1 y2 y3 y4. x1 = y1 \<Longrightarrow> x2 = y2 \<Longrightarrow> x3 = y3 \<Longrightarrow> x4 = y4 \<Longrightarrow> | 
| 65347 | 3979 | pseudo_divmod_main lc x1 x2 x3 x4 n = pseudo_divmod_main lc y1 y2 y3 y4 n" | 
| 3980 | by simp | |
| 3981 | have hd_rev: "coeff (Poly r) (length r - Suc 0) = hd (rev r)" | |
| 3982 | using last_coeff_is_hd[OF \<open>r \<noteq> []\<close>] by simp | |
| 3983 | show ?case | |
| 3984 | unfolding Suc.hyps(1)[OF v n_ok, symmetric] pseudo_divmod_main.simps Let_def | |
| 64795 | 3985 | proof (rule cong[OF _ _ refl], goal_cases) | 
| 65346 | 3986 | case 1 | 
| 65347 | 3987 | show ?case | 
| 3988 | by (simp add: monom_Suc hd_rev[symmetric] smult_monom Poly_map) | |
| 64795 | 3989 | next | 
| 65346 | 3990 | case 2 | 
| 3991 | show ?case | |
| 64795 | 3992 | proof (subst Poly_on_rev_starting_with_0, goal_cases) | 
| 69064 
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
 nipkow parents: 
69022diff
changeset | 3993 | show "hd (minus_poly_rev_list (map ((*) lc) (rev r)) (map ((*) (hd (rev r))) (rev d))) = 0" | 
| 65347 | 3994 | by (fold lc, subst head_minus_poly_rev_list, insert * \<open>d \<noteq> []\<close>, auto) | 
| 3995 | from * have "length d \<le> length r" | |
| 3996 | by simp | |
| 64795 | 3997 | then show "smult lc (Poly r) - monom (coeff (Poly r) (length r - 1)) n * Poly d = | 
| 69064 
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
 nipkow parents: 
69022diff
changeset | 3998 | Poly (rev (minus_poly_rev_list (map ((*) lc) (rev r)) (map ((*) (hd (rev r))) (rev d))))" | 
| 64795 | 3999 | by (fold rev_map) (auto simp add: n smult_monom_mult Poly_map hd_rev [symmetric] | 
| 65347 | 4000 | minus_poly_rev_list) | 
| 64795 | 4001 | qed | 
| 4002 | qed simp | |
| 4003 | qed simp | |
| 4004 | ||
| 65390 | 4005 | lemma pseudo_divmod_impl [code]: | 
| 4006 | "pseudo_divmod f g = map_prod poly_of_list poly_of_list (pseudo_divmod_list (coeffs f) (coeffs g))" | |
| 4007 | for f g :: "'a::comm_ring_1 poly" | |
| 65347 | 4008 | proof (cases "g = 0") | 
| 4009 | case False | |
| 65390 | 4010 | then have "last (coeffs g) \<noteq> 0" | 
| 4011 | and "last (coeffs g) = lead_coeff g" | |
| 4012 | and "coeffs g \<noteq> []" | |
| 4013 | by (simp_all add: last_coeffs_eq_coeff_degree) | |
| 4014 | moreover obtain q r where qr: "pseudo_divmod_main_list | |
| 4015 | (last (coeffs g)) (rev []) | |
| 4016 | (rev (coeffs f)) (rev (coeffs g)) | |
| 4017 | (1 + length (coeffs f) - | |
| 4018 | length (coeffs g)) = (q, rev (rev r))" | |
| 65347 | 4019 | by force | 
| 65390 | 4020 | ultimately have "(Poly q, Poly (rev r)) = pseudo_divmod_main (lead_coeff g) 0 f g | 
| 4021 | (length (coeffs f) - Suc 0) (Suc (length (coeffs f)) - length (coeffs g))" | |
| 4022 | by (subst pseudo_divmod_main_list_invar [symmetric]) auto | |
| 4023 | moreover have "pseudo_divmod_main_list | |
| 4024 | (hd (rev (coeffs g))) [] | |
| 4025 | (rev (coeffs f)) (rev (coeffs g)) | |
| 4026 | (1 + length (coeffs f) - | |
| 4027 | length (coeffs g)) = (q, r)" | |
| 4028 | using qr hd_rev [OF \<open>coeffs g \<noteq> []\<close>] by simp | |
| 4029 | ultimately show ?thesis | |
| 4030 | by (auto simp: degree_eq_length_coeffs pseudo_divmod_def pseudo_divmod_list_def Let_def) | |
| 64795 | 4031 | next | 
| 4032 | case True | |
| 65347 | 4033 | then show ?thesis | 
| 65390 | 4034 | by (auto simp add: pseudo_divmod_def pseudo_divmod_list_def) | 
| 64795 | 4035 | qed | 
| 4036 | ||
| 65347 | 4037 | lemma pseudo_mod_main_list: | 
| 4038 | "snd (pseudo_divmod_main_list l q xs ys n) = pseudo_mod_main_list l xs ys n" | |
| 4039 | by (induct n arbitrary: l q xs ys) (auto simp: Let_def) | |
| 4040 | ||
| 4041 | lemma pseudo_mod_impl[code]: "pseudo_mod f g = poly_of_list (pseudo_mod_list (coeffs f) (coeffs g))" | |
| 64795 | 4042 | proof - | 
| 65346 | 4043 | have snd_case: "\<And>f g p. snd ((\<lambda>(x,y). (f x, g y)) p) = g (snd p)" | 
| 64795 | 4044 | by auto | 
| 4045 | show ?thesis | |
| 65347 | 4046 | unfolding pseudo_mod_def pseudo_divmod_impl pseudo_divmod_list_def | 
| 4047 | pseudo_mod_list_def Let_def | |
| 4048 | by (simp add: snd_case pseudo_mod_main_list) | |
| 64795 | 4049 | qed | 
| 4050 | ||
| 4051 | ||
| 4052 | subsubsection \<open>Improved Code-Equations for Polynomial (Pseudo) Division\<close> | |
| 4053 | ||
| 64811 | 4054 | lemma pdivmod_pdivmodrel: "eucl_rel_poly p q (r, s) \<longleftrightarrow> (p div q, p mod q) = (r, s)" | 
| 4055 | by (metis eucl_rel_poly eucl_rel_poly_unique) | |
| 4056 | ||
| 65347 | 4057 | lemma pdivmod_via_pseudo_divmod: | 
| 4058 | "(f div g, f mod g) = | |
| 4059 | (if g = 0 then (0, f) | |
| 4060 | else | |
| 4061 | let | |
| 4062 | ilc = inverse (coeff g (degree g)); | |
| 4063 | h = smult ilc g; | |
| 4064 | (q,r) = pseudo_divmod f h | |
| 4065 | in (smult ilc q, r))" | |
| 4066 | (is "?l = ?r") | |
| 64795 | 4067 | proof (cases "g = 0") | 
| 65347 | 4068 | case True | 
| 4069 | then show ?thesis by simp | |
| 4070 | next | |
| 64795 | 4071 | case False | 
| 4072 | define lc where "lc = inverse (coeff g (degree g))" | |
| 4073 | define h where "h = smult lc g" | |
| 65347 | 4074 | from False have h1: "coeff h (degree h) = 1" and lc: "lc \<noteq> 0" | 
| 4075 | by (auto simp: h_def lc_def) | |
| 4076 | then have h0: "h \<noteq> 0" | |
| 4077 | by auto | |
| 4078 | obtain q r where p: "pseudo_divmod f h = (q, r)" | |
| 4079 | by force | |
| 65346 | 4080 | from False have id: "?r = (smult lc q, r)" | 
| 65347 | 4081 | by (auto simp: Let_def h_def[symmetric] lc_def[symmetric] p) | 
| 65346 | 4082 | from pseudo_divmod[OF h0 p, unfolded h1] | 
| 65347 | 4083 | have f: "f = h * q + r" and r: "r = 0 \<or> degree r < degree h" | 
| 4084 | by auto | |
| 4085 | from f r h0 have "eucl_rel_poly f h (q, r)" | |
| 4086 | by (auto simp: eucl_rel_poly_iff) | |
| 4087 | then have "(f div h, f mod h) = (q, r)" | |
| 4088 | by (simp add: pdivmod_pdivmodrel) | |
| 4089 | with lc have "(f div g, f mod g) = (smult lc q, r)" | |
| 4090 | by (auto simp: h_def div_smult_right[OF lc] mod_smult_right[OF lc]) | |
| 4091 | with id show ?thesis | |
| 4092 | by auto | |
| 4093 | qed | |
| 4094 | ||
| 4095 | lemma pdivmod_via_pseudo_divmod_list: | |
| 4096 | "(f div g, f mod g) = | |
| 4097 | (let cg = coeffs g in | |
| 4098 | if cg = [] then (0, f) | |
| 4099 | else | |
| 4100 | let | |
| 4101 | cf = coeffs f; | |
| 4102 | ilc = inverse (last cg); | |
| 69064 
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
 nipkow parents: 
69022diff
changeset | 4103 | ch = map ((*) ilc) cg; | 
| 65347 | 4104 | (q, r) = pseudo_divmod_main_list 1 [] (rev cf) (rev ch) (1 + length cf - length cg) | 
| 69064 
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
 nipkow parents: 
69022diff
changeset | 4105 | in (poly_of_list (map ((*) ilc) q), poly_of_list (rev r)))" | 
| 64795 | 4106 | proof - | 
| 65347 | 4107 | note d = pdivmod_via_pseudo_divmod pseudo_divmod_impl pseudo_divmod_list_def | 
| 64795 | 4108 | show ?thesis | 
| 4109 | proof (cases "g = 0") | |
| 65347 | 4110 | case True | 
| 4111 | with d show ?thesis by auto | |
| 64795 | 4112 | next | 
| 4113 | case False | |
| 4114 | define ilc where "ilc = inverse (coeff g (degree g))" | |
| 65347 | 4115 | from False have ilc: "ilc \<noteq> 0" | 
| 4116 | by (auto simp: ilc_def) | |
| 4117 | with False have id: "g = 0 \<longleftrightarrow> False" "coeffs g = [] \<longleftrightarrow> False" | |
| 65346 | 4118 | "last (coeffs g) = coeff g (degree g)" | 
| 65347 | 4119 | "coeffs (smult ilc g) = [] \<longleftrightarrow> False" | 
| 65346 | 4120 | by (auto simp: last_coeffs_eq_coeff_degree) | 
| 4121 | have id2: "hd (rev (coeffs (smult ilc g))) = 1" | |
| 64795 | 4122 | by (subst hd_rev, insert id ilc, auto simp: coeffs_smult, subst last_map, auto simp: id ilc_def) | 
| 65346 | 4123 | have id3: "length (coeffs (smult ilc g)) = length (coeffs g)" | 
| 69064 
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
 nipkow parents: 
69022diff
changeset | 4124 | "rev (coeffs (smult ilc g)) = rev (map ((*) ilc) (coeffs g))" | 
| 65347 | 4125 | unfolding coeffs_smult using ilc by auto | 
| 4126 | obtain q r where pair: | |
| 69064 
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
 nipkow parents: 
69022diff
changeset | 4127 | "pseudo_divmod_main_list 1 [] (rev (coeffs f)) (rev (map ((*) ilc) (coeffs g))) | 
| 65347 | 4128 | (1 + length (coeffs f) - length (coeffs g)) = (q, r)" | 
| 4129 | by force | |
| 4130 | show ?thesis | |
| 4131 | unfolding d Let_def id if_False ilc_def[symmetric] map_prod_def[symmetric] id2 | |
| 4132 | unfolding id3 pair map_prod_def split | |
| 4133 | by (auto simp: Poly_map) | |
| 64795 | 4134 | qed | 
| 4135 | qed | |
| 4136 | ||
| 4137 | lemma pseudo_divmod_main_list_1: "pseudo_divmod_main_list 1 = divmod_poly_one_main_list" | |
| 4138 | proof (intro ext, goal_cases) | |
| 4139 | case (1 q r d n) | |
| 69064 
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
 nipkow parents: 
69022diff
changeset | 4140 | have *: "map ((*) 1) xs = xs" for xs :: "'a list" | 
| 65347 | 4141 | by (induct xs) auto | 
| 4142 | show ?case | |
| 4143 | by (induct n arbitrary: q r d) (auto simp: * Let_def) | |
| 64795 | 4144 | qed | 
| 4145 | ||
| 65347 | 4146 | fun divide_poly_main_list :: "'a::idom_divide \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> nat \<Rightarrow> 'a list" | 
| 4147 | where | |
| 4148 | "divide_poly_main_list lc q r d (Suc n) = | |
| 4149 | (let | |
| 4150 | cr = hd r | |
| 4151 | in if cr = 0 then divide_poly_main_list lc (cCons cr q) (tl r) d n else let | |
| 4152 | a = cr div lc; | |
| 4153 | qq = cCons a q; | |
| 69064 
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
 nipkow parents: 
69022diff
changeset | 4154 | rr = minus_poly_rev_list r (map ((*) a) d) | 
| 65347 | 4155 | in if hd rr = 0 then divide_poly_main_list lc qq (tl rr) d n else [])" | 
| 4156 | | "divide_poly_main_list lc q r d 0 = q" | |
| 4157 | ||
| 4158 | lemma divide_poly_main_list_simp [simp]: | |
| 4159 | "divide_poly_main_list lc q r d (Suc n) = | |
| 4160 | (let | |
| 4161 | cr = hd r; | |
| 4162 | a = cr div lc; | |
| 4163 | qq = cCons a q; | |
| 69064 
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
 nipkow parents: 
69022diff
changeset | 4164 | rr = minus_poly_rev_list r (map ((*) a) d) | 
| 64795 | 4165 | in if hd rr = 0 then divide_poly_main_list lc qq (tl rr) d n else [])" | 
| 4166 | by (simp add: Let_def minus_zero_does_nothing) | |
| 4167 | ||
| 4168 | declare divide_poly_main_list.simps(1)[simp del] | |
| 4169 | ||
| 65347 | 4170 | definition divide_poly_list :: "'a::idom_divide poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" | 
| 4171 | where "divide_poly_list f g = | |
| 4172 | (let cg = coeffs g in | |
| 4173 | if cg = [] then g | |
| 4174 | else | |
| 4175 | let | |
| 4176 | cf = coeffs f; | |
| 4177 | cgr = rev cg | |
| 4178 | in poly_of_list (divide_poly_main_list (hd cgr) [] (rev cf) cgr (1 + length cf - length cg)))" | |
| 64795 | 4179 | |
| 64811 | 4180 | lemmas pdivmod_via_divmod_list = pdivmod_via_pseudo_divmod_list[unfolded pseudo_divmod_main_list_1] | 
| 64795 | 4181 | |
| 4182 | lemma mod_poly_one_main_list: "snd (divmod_poly_one_main_list q r d n) = mod_poly_one_main_list r d n" | |
| 65347 | 4183 | by (induct n arbitrary: q r d) (auto simp: Let_def) | 
| 4184 | ||
| 4185 | lemma mod_poly_code [code]: | |
| 4186 | "f mod g = | |
| 4187 | (let cg = coeffs g in | |
| 4188 | if cg = [] then f | |
| 4189 | else | |
| 4190 | let | |
| 4191 | cf = coeffs f; | |
| 4192 | ilc = inverse (last cg); | |
| 69064 
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
 nipkow parents: 
69022diff
changeset | 4193 | ch = map ((*) ilc) cg; | 
| 65347 | 4194 | r = mod_poly_one_main_list (rev cf) (rev ch) (1 + length cf - length cg) | 
| 4195 | in poly_of_list (rev r))" | |
| 4196 | (is "_ = ?rhs") | |
| 64795 | 4197 | proof - | 
| 65347 | 4198 | have "snd (f div g, f mod g) = ?rhs" | 
| 4199 | unfolding pdivmod_via_divmod_list Let_def mod_poly_one_main_list [symmetric, of _ _ _ Nil] | |
| 4200 | by (auto split: prod.splits) | |
| 4201 | then show ?thesis by simp | |
| 64795 | 4202 | qed | 
| 4203 | ||
| 65347 | 4204 | definition div_field_poly_impl :: "'a :: field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" | 
| 4205 | where "div_field_poly_impl f g = | |
| 4206 | (let cg = coeffs g in | |
| 4207 | if cg = [] then 0 | |
| 4208 | else | |
| 4209 | let | |
| 4210 | cf = coeffs f; | |
| 4211 | ilc = inverse (last cg); | |
| 69064 
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
 nipkow parents: 
69022diff
changeset | 4212 | ch = map ((*) ilc) cg; | 
| 65347 | 4213 | q = fst (divmod_poly_one_main_list [] (rev cf) (rev ch) (1 + length cf - length cg)) | 
| 69064 
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
 nipkow parents: 
69022diff
changeset | 4214 | in poly_of_list ((map ((*) ilc) q)))" | 
| 64795 | 4215 | |
| 65346 | 4216 | text \<open>We do not declare the following lemma as code equation, since then polynomial division | 
| 4217 | on non-fields will no longer be executable. However, a code-unfold is possible, since | |
| 64795 | 4218 | \<open>div_field_poly_impl\<close> is a bit more efficient than the generic polynomial division.\<close> | 
| 67399 | 4219 | lemma div_field_poly_impl[code_unfold]: "(div) = div_field_poly_impl" | 
| 64795 | 4220 | proof (intro ext) | 
| 4221 | fix f g :: "'a poly" | |
| 65347 | 4222 | have "fst (f div g, f mod g) = div_field_poly_impl f g" | 
| 4223 | unfolding div_field_poly_impl_def pdivmod_via_divmod_list Let_def | |
| 4224 | by (auto split: prod.splits) | |
| 64811 | 4225 | then show "f div g = div_field_poly_impl f g" | 
| 4226 | by simp | |
| 64795 | 4227 | qed | 
| 4228 | ||
| 4229 | lemma divide_poly_main_list: | |
| 4230 | assumes lc0: "lc \<noteq> 0" | |
| 65347 | 4231 | and lc: "last d = lc" | 
| 4232 | and d: "d \<noteq> []" | |
| 4233 | and "n = (1 + length r - length d)" | |
| 4234 | shows "Poly (divide_poly_main_list lc q (rev r) (rev d) n) = | |
| 4235 | divide_poly_main lc (monom 1 n * Poly q) (Poly r) (Poly d) (length r - 1) n" | |
| 4236 | using assms(4-) | |
| 4237 | proof (induct "n" arbitrary: r q) | |
| 4238 | case (Suc n) | |
| 4239 | from Suc.prems have ifCond: "\<not> Suc (length r) \<le> length d" | |
| 4240 | by simp | |
| 4241 | with d have r: "r \<noteq> []" | |
| 4242 | using Suc_leI length_greater_0_conv list.size(3) by fastforce | |
| 4243 | then obtain rr lcr where r: "r = rr @ [lcr]" | |
| 4244 | by (cases r rule: rev_cases) auto | |
| 65346 | 4245 | from d lc obtain dd where d: "d = dd @ [lc]" | 
| 65347 | 4246 | by (cases d rule: rev_cases) auto | 
| 4247 | from Suc(2) ifCond have n: "n = 1 + length rr - length d" | |
| 4248 | by (auto simp: r) | |
| 4249 | from ifCond have len: "length dd \<le> length rr" | |
| 4250 | by (simp add: r d) | |
| 64795 | 4251 | show ?case | 
| 4252 | proof (cases "lcr div lc * lc = lcr") | |
| 4253 | case False | |
| 65347 | 4254 | with r d show ?thesis | 
| 4255 | unfolding Suc(2)[symmetric] | |
| 64795 | 4256 | by (auto simp add: Let_def nth_default_append) | 
| 4257 | next | |
| 4258 | case True | |
| 65347 | 4259 | with r d have id: | 
| 4260 | "?thesis \<longleftrightarrow> | |
| 4261 | Poly (divide_poly_main_list lc (cCons (lcr div lc) q) | |
| 69064 
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
 nipkow parents: 
69022diff
changeset | 4262 | (rev (rev (minus_poly_rev_list (rev rr) (rev (map ((*) (lcr div lc)) dd))))) (rev d) n) = | 
| 65347 | 4263 | divide_poly_main lc | 
| 4264 | (monom 1 (Suc n) * Poly q + monom (lcr div lc) n) | |
| 4265 | (Poly r - monom (lcr div lc) n * Poly d) | |
| 4266 | (Poly d) (length rr - 1) n" | |
| 4267 | by (cases r rule: rev_cases; cases "d" rule: rev_cases) | |
| 4268 | (auto simp add: Let_def rev_map nth_default_append) | |
| 65346 | 4269 | have cong: "\<And>x1 x2 x3 x4 y1 y2 y3 y4. x1 = y1 \<Longrightarrow> x2 = y2 \<Longrightarrow> x3 = y3 \<Longrightarrow> x4 = y4 \<Longrightarrow> | 
| 65347 | 4270 | divide_poly_main lc x1 x2 x3 x4 n = divide_poly_main lc y1 y2 y3 y4 n" | 
| 4271 | by simp | |
| 4272 | show ?thesis | |
| 4273 | unfolding id | |
| 64795 | 4274 | proof (subst Suc(1), simp add: n, | 
| 65347 | 4275 | subst minus_poly_rev_list, force simp: len, rule cong[OF _ _ refl], goal_cases) | 
| 65346 | 4276 | case 2 | 
| 64795 | 4277 | have "monom lcr (length rr) = monom (lcr div lc) (length rr - length dd) * monom lc (length dd)" | 
| 4278 | by (simp add: mult_monom len True) | |
| 65346 | 4279 | then show ?case unfolding r d Poly_append n ring_distribs | 
| 64795 | 4280 | by (auto simp: Poly_map smult_monom smult_monom_mult) | 
| 4281 | qed (auto simp: len monom_Suc smult_monom) | |
| 4282 | qed | |
| 4283 | qed simp | |
| 4284 | ||
| 65346 | 4285 | lemma divide_poly_list[code]: "f div g = divide_poly_list f g" | 
| 64795 | 4286 | proof - | 
| 4287 | note d = divide_poly_def divide_poly_list_def | |
| 4288 | show ?thesis | |
| 4289 | proof (cases "g = 0") | |
| 4290 | case True | |
| 65347 | 4291 | show ?thesis by (auto simp: d True) | 
| 64795 | 4292 | next | 
| 4293 | case False | |
| 65347 | 4294 | then obtain cg lcg where cg: "coeffs g = cg @ [lcg]" | 
| 4295 | by (cases "coeffs g" rule: rev_cases) auto | |
| 4296 | with False have id: "(g = 0) = False" "(cg @ [lcg] = []) = False" | |
| 4297 | by auto | |
| 65346 | 4298 | from cg False have lcg: "coeff g (degree g) = lcg" | 
| 64795 | 4299 | using last_coeffs_eq_coeff_degree last_snoc by force | 
| 65347 | 4300 | with False have "lcg \<noteq> 0" by auto | 
| 4301 | from cg Poly_coeffs [of g] have ltp: "Poly (cg @ [lcg]) = g" | |
| 4302 | by auto | |
| 4303 | show ?thesis | |
| 4304 | unfolding d cg Let_def id if_False poly_of_list_def | |
| 4305 | by (subst divide_poly_main_list, insert False cg \<open>lcg \<noteq> 0\<close>) | |
| 4306 | (auto simp: lcg ltp, simp add: degree_eq_length_coeffs) | |
| 64795 | 4307 | qed | 
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 4308 | qed | 
| 52380 | 4309 | |
| 66805 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4310 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4311 | subsection \<open>Primality and irreducibility in polynomial rings\<close> | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4312 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4313 | lemma prod_mset_const_poly: "(\<Prod>x\<in>#A. [:f x:]) = [:prod_mset (image_mset f A):]" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4314 | by (induct A) (simp_all add: ac_simps) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4315 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4316 | lemma irreducible_const_poly_iff: | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4317 |   fixes c :: "'a :: {comm_semiring_1,semiring_no_zero_divisors}"
 | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4318 | shows "irreducible [:c:] \<longleftrightarrow> irreducible c" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4319 | proof | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4320 | assume A: "irreducible c" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4321 | show "irreducible [:c:]" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4322 | proof (rule irreducibleI) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4323 | fix a b assume ab: "[:c:] = a * b" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4324 | hence "degree [:c:] = degree (a * b)" by (simp only: ) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4325 | also from A ab have "a \<noteq> 0" "b \<noteq> 0" by auto | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4326 | hence "degree (a * b) = degree a + degree b" by (simp add: degree_mult_eq) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4327 | finally have "degree a = 0" "degree b = 0" by auto | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4328 | then obtain a' b' where ab': "a = [:a':]" "b = [:b':]" by (auto elim!: degree_eq_zeroE) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4329 | from ab have "coeff [:c:] 0 = coeff (a * b) 0" by (simp only: ) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4330 | hence "c = a' * b'" by (simp add: ab' mult_ac) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4331 | from A and this have "a' dvd 1 \<or> b' dvd 1" by (rule irreducibleD) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4332 | with ab' show "a dvd 1 \<or> b dvd 1" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4333 | by (auto simp add: is_unit_const_poly_iff) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4334 | qed (insert A, auto simp: irreducible_def is_unit_poly_iff) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4335 | next | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4336 | assume A: "irreducible [:c:]" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4337 | then have "c \<noteq> 0" and "\<not> c dvd 1" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4338 | by (auto simp add: irreducible_def is_unit_const_poly_iff) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4339 | then show "irreducible c" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4340 | proof (rule irreducibleI) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4341 | fix a b assume ab: "c = a * b" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4342 | hence "[:c:] = [:a:] * [:b:]" by (simp add: mult_ac) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4343 | from A and this have "[:a:] dvd 1 \<or> [:b:] dvd 1" by (rule irreducibleD) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4344 | then show "a dvd 1 \<or> b dvd 1" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4345 | by (auto simp add: is_unit_const_poly_iff) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4346 | qed | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4347 | qed | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4348 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4349 | lemma lift_prime_elem_poly: | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4350 | assumes "prime_elem (c :: 'a :: semidom)" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4351 | shows "prime_elem [:c:]" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4352 | proof (rule prime_elemI) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4353 | fix a b assume *: "[:c:] dvd a * b" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4354 | from * have dvd: "c dvd coeff (a * b) n" for n | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4355 | by (subst (asm) const_poly_dvd_iff) blast | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4356 |   {
 | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4357 | define m where "m = (GREATEST m. \<not>c dvd coeff b m)" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4358 | assume "\<not>[:c:] dvd b" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4359 | hence A: "\<exists>i. \<not>c dvd coeff b i" by (subst (asm) const_poly_dvd_iff) blast | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4360 | have B: "\<forall>i. \<not>c dvd coeff b i \<longrightarrow> i \<le> degree b" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4361 | by (auto intro: le_degree) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4362 | have coeff_m: "\<not>c dvd coeff b m" unfolding m_def by (rule GreatestI_ex_nat[OF A B]) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4363 | have "i \<le> m" if "\<not>c dvd coeff b i" for i | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4364 | unfolding m_def by (rule Greatest_le_nat[OF that B]) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4365 | hence dvd_b: "c dvd coeff b i" if "i > m" for i using that by force | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4366 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4367 | have "c dvd coeff a i" for i | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4368 | proof (induction i rule: nat_descend_induct[of "degree a"]) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4369 | case (base i) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4370 | thus ?case by (simp add: coeff_eq_0) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4371 | next | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4372 | case (descend i) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4373 |       let ?A = "{..i+m} - {i}"
 | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4374 | have "c dvd coeff (a * b) (i + m)" by (rule dvd) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4375 | also have "coeff (a * b) (i + m) = (\<Sum>k\<le>i + m. coeff a k * coeff b (i + m - k))" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4376 | by (simp add: coeff_mult) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4377 |       also have "{..i+m} = insert i ?A" by auto
 | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4378 | also have "(\<Sum>k\<in>\<dots>. coeff a k * coeff b (i + m - k)) = | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4379 | coeff a i * coeff b m + (\<Sum>k\<in>?A. coeff a k * coeff b (i + m - k))" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4380 | (is "_ = _ + ?S") | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4381 | by (subst sum.insert) simp_all | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4382 | finally have eq: "c dvd coeff a i * coeff b m + ?S" . | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4383 | moreover have "c dvd ?S" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4384 | proof (rule dvd_sum) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4385 |         fix k assume k: "k \<in> {..i+m} - {i}"
 | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4386 | show "c dvd coeff a k * coeff b (i + m - k)" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4387 | proof (cases "k < i") | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4388 | case False | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4389 | with k have "c dvd coeff a k" by (intro descend.IH) simp | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4390 | thus ?thesis by simp | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4391 | next | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4392 | case True | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4393 | hence "c dvd coeff b (i + m - k)" by (intro dvd_b) simp | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4394 | thus ?thesis by simp | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4395 | qed | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4396 | qed | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4397 | ultimately have "c dvd coeff a i * coeff b m" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4398 | by (simp add: dvd_add_left_iff) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4399 | with assms coeff_m show "c dvd coeff a i" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4400 | by (simp add: prime_elem_dvd_mult_iff) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4401 | qed | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4402 | hence "[:c:] dvd a" by (subst const_poly_dvd_iff) blast | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4403 | } | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4404 | then show "[:c:] dvd a \<or> [:c:] dvd b" by blast | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4405 | next | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4406 | from assms show "[:c:] \<noteq> 0" and "\<not> [:c:] dvd 1" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4407 | by (simp_all add: prime_elem_def is_unit_const_poly_iff) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4408 | qed | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4409 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4410 | lemma prime_elem_const_poly_iff: | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4411 | fixes c :: "'a :: semidom" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4412 | shows "prime_elem [:c:] \<longleftrightarrow> prime_elem c" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4413 | proof | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4414 | assume A: "prime_elem [:c:]" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4415 | show "prime_elem c" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4416 | proof (rule prime_elemI) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4417 | fix a b assume "c dvd a * b" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4418 | hence "[:c:] dvd [:a:] * [:b:]" by (simp add: mult_ac) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4419 | from A and this have "[:c:] dvd [:a:] \<or> [:c:] dvd [:b:]" by (rule prime_elem_dvd_multD) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4420 | thus "c dvd a \<or> c dvd b" by simp | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4421 | qed (insert A, auto simp: prime_elem_def is_unit_poly_iff) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4422 | qed (auto intro: lift_prime_elem_poly) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4423 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4424 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4425 | subsection \<open>Content and primitive part of a polynomial\<close> | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4426 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4427 | definition content :: "'a::semiring_gcd poly \<Rightarrow> 'a" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4428 | where "content p = gcd_list (coeffs p)" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4429 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4430 | lemma content_eq_fold_coeffs [code]: "content p = fold_coeffs gcd p 0" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4431 | by (simp add: content_def Gcd_fin.set_eq_fold fold_coeffs_def foldr_fold fun_eq_iff ac_simps) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4432 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4433 | lemma content_0 [simp]: "content 0 = 0" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4434 | by (simp add: content_def) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4435 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4436 | lemma content_1 [simp]: "content 1 = 1" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4437 | by (simp add: content_def) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4438 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4439 | lemma content_const [simp]: "content [:c:] = normalize c" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4440 | by (simp add: content_def cCons_def) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4441 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4442 | lemma const_poly_dvd_iff_dvd_content: "[:c:] dvd p \<longleftrightarrow> c dvd content p" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4443 | for c :: "'a::semiring_gcd" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4444 | proof (cases "p = 0") | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4445 | case True | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4446 | then show ?thesis by simp | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4447 | next | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4448 | case False | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4449 | have "[:c:] dvd p \<longleftrightarrow> (\<forall>n. c dvd coeff p n)" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4450 | by (rule const_poly_dvd_iff) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4451 | also have "\<dots> \<longleftrightarrow> (\<forall>a\<in>set (coeffs p). c dvd a)" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4452 | proof safe | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4453 | fix n :: nat | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4454 | assume "\<forall>a\<in>set (coeffs p). c dvd a" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4455 | then show "c dvd coeff p n" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4456 | by (cases "n \<le> degree p") (auto simp: coeff_eq_0 coeffs_def split: if_splits) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4457 | qed (auto simp: coeffs_def simp del: upt_Suc split: if_splits) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4458 | also have "\<dots> \<longleftrightarrow> c dvd content p" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4459 | by (simp add: content_def dvd_Gcd_fin_iff dvd_mult_unit_iff) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4460 | finally show ?thesis . | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4461 | qed | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4462 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4463 | lemma content_dvd [simp]: "[:content p:] dvd p" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4464 | by (subst const_poly_dvd_iff_dvd_content) simp_all | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4465 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4466 | lemma content_dvd_coeff [simp]: "content p dvd coeff p n" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4467 | proof (cases "p = 0") | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4468 | case True | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4469 | then show ?thesis | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4470 | by simp | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4471 | next | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4472 | case False | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4473 | then show ?thesis | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4474 | by (cases "n \<le> degree p") | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4475 | (auto simp add: content_def not_le coeff_eq_0 coeff_in_coeffs intro: Gcd_fin_dvd) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4476 | qed | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4477 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4478 | lemma content_dvd_coeffs: "c \<in> set (coeffs p) \<Longrightarrow> content p dvd c" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4479 | by (simp add: content_def Gcd_fin_dvd) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4480 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4481 | lemma normalize_content [simp]: "normalize (content p) = content p" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4482 | by (simp add: content_def) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4483 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4484 | lemma is_unit_content_iff [simp]: "is_unit (content p) \<longleftrightarrow> content p = 1" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4485 | proof | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4486 | assume "is_unit (content p)" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4487 | then have "normalize (content p) = 1" by (simp add: is_unit_normalize del: normalize_content) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4488 | then show "content p = 1" by simp | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4489 | qed auto | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4490 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4491 | lemma content_smult [simp]: "content (smult c p) = normalize c * content p" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4492 | by (simp add: content_def coeffs_smult Gcd_fin_mult) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4493 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4494 | lemma content_eq_zero_iff [simp]: "content p = 0 \<longleftrightarrow> p = 0" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4495 | by (auto simp: content_def simp: poly_eq_iff coeffs_def) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4496 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4497 | definition primitive_part :: "'a :: semiring_gcd poly \<Rightarrow> 'a poly" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4498 | where "primitive_part p = map_poly (\<lambda>x. x div content p) p" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4499 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4500 | lemma primitive_part_0 [simp]: "primitive_part 0 = 0" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4501 | by (simp add: primitive_part_def) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4502 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4503 | lemma content_times_primitive_part [simp]: "smult (content p) (primitive_part p) = p" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4504 | for p :: "'a :: semiring_gcd poly" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4505 | proof (cases "p = 0") | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4506 | case True | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4507 | then show ?thesis by simp | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4508 | next | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4509 | case False | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4510 | then show ?thesis | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4511 | unfolding primitive_part_def | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4512 | by (auto simp: smult_conv_map_poly map_poly_map_poly o_def content_dvd_coeffs | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4513 | intro: map_poly_idI) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4514 | qed | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4515 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4516 | lemma primitive_part_eq_0_iff [simp]: "primitive_part p = 0 \<longleftrightarrow> p = 0" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4517 | proof (cases "p = 0") | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4518 | case True | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4519 | then show ?thesis by simp | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4520 | next | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4521 | case False | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4522 | then have "primitive_part p = map_poly (\<lambda>x. x div content p) p" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4523 | by (simp add: primitive_part_def) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4524 | also from False have "\<dots> = 0 \<longleftrightarrow> p = 0" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4525 | by (intro map_poly_eq_0_iff) (auto simp: dvd_div_eq_0_iff content_dvd_coeffs) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4526 | finally show ?thesis | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4527 | using False by simp | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4528 | qed | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4529 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4530 | lemma content_primitive_part [simp]: | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4531 | assumes "p \<noteq> 0" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4532 | shows "content (primitive_part p) = 1" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4533 | proof - | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4534 | have "p = smult (content p) (primitive_part p)" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4535 | by simp | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4536 | also have "content \<dots> = content (primitive_part p) * content p" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4537 | by (simp del: content_times_primitive_part add: ac_simps) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4538 | finally have "1 * content p = content (primitive_part p) * content p" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4539 | by simp | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4540 | then have "1 * content p div content p = content (primitive_part p) * content p div content p" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4541 | by simp | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4542 | with assms show ?thesis | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4543 | by simp | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4544 | qed | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4545 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4546 | lemma content_decompose: | 
| 68790 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4547 | obtains p' :: "'a::semiring_gcd poly" | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4548 | where "p = smult (content p) p'" "content p' = 1" | 
| 66805 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4549 | proof (cases "p = 0") | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4550 | case True | 
| 68790 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4551 | then have "p = smult (content p) 1" "content 1 = 1" | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4552 | by simp_all | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4553 | then show ?thesis .. | 
| 66805 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4554 | next | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4555 | case False | 
| 68790 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4556 | then have "p = smult (content p) (primitive_part p)" "content (primitive_part p) = 1" | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4557 | by simp_all | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4558 | then show ?thesis .. | 
| 66805 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4559 | qed | 
| 68790 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4560 | |
| 66805 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4561 | lemma content_dvd_contentI [intro]: "p dvd q \<Longrightarrow> content p dvd content q" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4562 | using const_poly_dvd_iff_dvd_content content_dvd dvd_trans by blast | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4563 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4564 | lemma primitive_part_const_poly [simp]: "primitive_part [:x:] = [:unit_factor x:]" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4565 | by (simp add: primitive_part_def map_poly_pCons) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4566 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4567 | lemma primitive_part_prim: "content p = 1 \<Longrightarrow> primitive_part p = p" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4568 | by (auto simp: primitive_part_def) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4569 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4570 | lemma degree_primitive_part [simp]: "degree (primitive_part p) = degree p" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4571 | proof (cases "p = 0") | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4572 | case True | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4573 | then show ?thesis by simp | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4574 | next | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4575 | case False | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4576 | have "p = smult (content p) (primitive_part p)" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4577 | by simp | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4578 | also from False have "degree \<dots> = degree (primitive_part p)" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4579 | by (subst degree_smult_eq) simp_all | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4580 | finally show ?thesis .. | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4581 | qed | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4582 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4583 | lemma smult_content_normalize_primitive_part [simp]: | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4584 | "smult (content p) (normalize (primitive_part p)) = normalize p" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4585 | proof - | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4586 | have "smult (content p) (normalize (primitive_part p)) = | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4587 | normalize ([:content p:] * primitive_part p)" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4588 | by (subst normalize_mult) (simp_all add: normalize_const_poly) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4589 | also have "[:content p:] * primitive_part p = p" by simp | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4590 | finally show ?thesis . | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4591 | qed | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4592 | |
| 68790 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4593 | context | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4594 | begin | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4595 | |
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4596 | private | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4597 | |
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4598 | lemma content_1_mult: | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4599 |   fixes f g :: "'a :: {semiring_gcd, factorial_semiring} poly"
 | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4600 | assumes "content f = 1" "content g = 1" | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4601 | shows "content (f * g) = 1" | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4602 | proof (cases "f * g = 0") | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4603 | case False | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4604 | from assms have "f \<noteq> 0" "g \<noteq> 0" by auto | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4605 | |
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4606 | hence "f * g \<noteq> 0" by auto | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4607 |   {
 | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4608 | assume "\<not>is_unit (content (f * g))" | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4609 | with False have "\<exists>p. p dvd content (f * g) \<and> prime p" | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4610 | by (intro prime_divisor_exists) simp_all | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4611 | then obtain p where "p dvd content (f * g)" "prime p" by blast | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4612 | from \<open>p dvd content (f * g)\<close> have "[:p:] dvd f * g" | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4613 | by (simp add: const_poly_dvd_iff_dvd_content) | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4614 | moreover from \<open>prime p\<close> have "prime_elem [:p:]" by (simp add: lift_prime_elem_poly) | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4615 | ultimately have "[:p:] dvd f \<or> [:p:] dvd g" | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4616 | by (simp add: prime_elem_dvd_mult_iff) | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4617 | with assms have "is_unit p" by (simp add: const_poly_dvd_iff_dvd_content) | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4618 | with \<open>prime p\<close> have False by simp | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4619 | } | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4620 | hence "is_unit (content (f * g))" by blast | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4621 | hence "normalize (content (f * g)) = 1" by (simp add: is_unit_normalize del: normalize_content) | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4622 | thus ?thesis by simp | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4623 | qed (insert assms, auto) | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4624 | |
| 66805 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4625 | lemma content_mult: | 
| 68790 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4626 |   fixes p q :: "'a :: {factorial_semiring, semiring_gcd} poly"
 | 
| 66805 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4627 | shows "content (p * q) = content p * content q" | 
| 68790 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4628 | proof (cases "p * q = 0") | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4629 | case False | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4630 | then have "p \<noteq> 0" and "q \<noteq> 0" | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4631 | by simp_all | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4632 | then have *: "content (primitive_part p * primitive_part q) = 1" | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4633 | by (auto intro: content_1_mult) | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4634 | have "p * q = smult (content p) (primitive_part p) * smult (content q) (primitive_part q)" | 
| 66805 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4635 | by simp | 
| 68790 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4636 | also have "\<dots> = smult (content p * content q) (primitive_part p * primitive_part q)" | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4637 | by (metis mult.commute mult_smult_right smult_smult) | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4638 | with * show ?thesis | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4639 | by (simp add: normalize_mult) | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4640 | next | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4641 | case True | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4642 | then show ?thesis | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4643 | by auto | 
| 66805 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4644 | qed | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4645 | |
| 68790 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4646 | end | 
| 
851a9d9746c6
prefer constructive primitive_part over implicit content_decompose
 haftmann parents: 
68534diff
changeset | 4647 | |
| 66805 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4648 | lemma primitive_part_mult: | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4649 |   fixes p q :: "'a :: {factorial_semiring, semiring_Gcd, ring_gcd, idom_divide} poly"
 | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4650 | shows "primitive_part (p * q) = primitive_part p * primitive_part q" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4651 | proof - | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4652 | have "primitive_part (p * q) = p * q div [:content (p * q):]" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4653 | by (simp add: primitive_part_def div_const_poly_conv_map_poly) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4654 | also have "\<dots> = (p div [:content p:]) * (q div [:content q:])" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4655 | by (subst div_mult_div_if_dvd) (simp_all add: content_mult mult_ac) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4656 | also have "\<dots> = primitive_part p * primitive_part q" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4657 | by (simp add: primitive_part_def div_const_poly_conv_map_poly) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4658 | finally show ?thesis . | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4659 | qed | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4660 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4661 | lemma primitive_part_smult: | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4662 |   fixes p :: "'a :: {factorial_semiring, semiring_Gcd, ring_gcd, idom_divide} poly"
 | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4663 | shows "primitive_part (smult a p) = smult (unit_factor a) (primitive_part p)" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4664 | proof - | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4665 | have "smult a p = [:a:] * p" by simp | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4666 | also have "primitive_part \<dots> = smult (unit_factor a) (primitive_part p)" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4667 | by (subst primitive_part_mult) simp_all | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4668 | finally show ?thesis . | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4669 | qed | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4670 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4671 | lemma primitive_part_dvd_primitive_partI [intro]: | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4672 |   fixes p q :: "'a :: {factorial_semiring, semiring_Gcd, ring_gcd, idom_divide} poly"
 | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4673 | shows "p dvd q \<Longrightarrow> primitive_part p dvd primitive_part q" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4674 | by (auto elim!: dvdE simp: primitive_part_mult) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4675 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4676 | lemma content_prod_mset: | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4677 |   fixes A :: "'a :: {factorial_semiring, semiring_Gcd} poly multiset"
 | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4678 | shows "content (prod_mset A) = prod_mset (image_mset content A)" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4679 | by (induction A) (simp_all add: content_mult mult_ac) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4680 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4681 | lemma content_prod_eq_1_iff: | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4682 |   fixes p q :: "'a :: {factorial_semiring, semiring_Gcd} poly"
 | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4683 | shows "content (p * q) = 1 \<longleftrightarrow> content p = 1 \<and> content q = 1" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4684 | proof safe | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4685 | assume A: "content (p * q) = 1" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4686 |   {
 | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4687 | fix p q :: "'a poly" assume "content p * content q = 1" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4688 | hence "1 = content p * content q" by simp | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4689 | hence "content p dvd 1" by (rule dvdI) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4690 | hence "content p = 1" by simp | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4691 | } note B = this | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4692 | from A B[of p q] B [of q p] show "content p = 1" "content q = 1" | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4693 | by (simp_all add: content_mult mult_ac) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4694 | qed (auto simp: content_mult) | 
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4695 | |
| 
274b4edca859
Polynomial_Factorial does not depend on Field_as_Ring as such
 haftmann parents: 
66799diff
changeset | 4696 | |
| 52380 | 4697 | no_notation cCons (infixr "##" 65) | 
| 31663 | 4698 | |
| 29478 | 4699 | end |