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