| author | wenzelm | 
| Thu, 10 Nov 2016 21:54:58 +0100 | |
| changeset 64482 | 43f6c28ff496 | 
| parent 64272 | f76b6dda2e56 | 
| child 64591 | 240a39af9ec4 | 
| permissions | -rw-r--r-- | 
| 41959 | 1 | (* Title: HOL/Library/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 | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 11 | imports Main "~~/src/HOL/Deriv" "~~/src/HOL/Library/More_List" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 12 | "~~/src/HOL/Library/Infinite_Set" | 
| 29451 | 13 | begin | 
| 14 | ||
| 60500 | 15 | subsection \<open>Auxiliary: operations for lists (later) representing coefficients\<close> | 
| 52380 | 16 | |
| 17 | definition cCons :: "'a::zero \<Rightarrow> 'a list \<Rightarrow> 'a list" (infixr "##" 65) | |
| 18 | where | |
| 19 | "x ## xs = (if xs = [] \<and> x = 0 then [] else x # xs)" | |
| 20 | ||
| 21 | lemma cCons_0_Nil_eq [simp]: | |
| 22 | "0 ## [] = []" | |
| 23 | by (simp add: cCons_def) | |
| 24 | ||
| 25 | lemma cCons_Cons_eq [simp]: | |
| 26 | "x ## y # ys = x # y # ys" | |
| 27 | by (simp add: cCons_def) | |
| 28 | ||
| 29 | lemma cCons_append_Cons_eq [simp]: | |
| 30 | "x ## xs @ y # ys = x # xs @ y # ys" | |
| 31 | by (simp add: cCons_def) | |
| 32 | ||
| 33 | lemma cCons_not_0_eq [simp]: | |
| 34 | "x \<noteq> 0 \<Longrightarrow> x ## xs = x # xs" | |
| 35 | by (simp add: cCons_def) | |
| 36 | ||
| 37 | lemma strip_while_not_0_Cons_eq [simp]: | |
| 38 | "strip_while (\<lambda>x. x = 0) (x # xs) = x ## strip_while (\<lambda>x. x = 0) xs" | |
| 39 | proof (cases "x = 0") | |
| 40 | case False then show ?thesis by simp | |
| 41 | next | |
| 42 | case True show ?thesis | |
| 43 | proof (induct xs rule: rev_induct) | |
| 44 | case Nil with True show ?case by simp | |
| 45 | next | |
| 46 | case (snoc y ys) then show ?case | |
| 47 | by (cases "y = 0") (simp_all add: append_Cons [symmetric] del: append_Cons) | |
| 48 | qed | |
| 49 | qed | |
| 50 | ||
| 51 | lemma tl_cCons [simp]: | |
| 52 | "tl (x ## xs) = xs" | |
| 53 | by (simp add: cCons_def) | |
| 54 | ||
| 61585 | 55 | subsection \<open>Definition of type \<open>poly\<close>\<close> | 
| 29451 | 56 | |
| 61260 | 57 | typedef (overloaded) 'a poly = "{f :: nat \<Rightarrow> 'a::zero. \<forall>\<^sub>\<infinity> n. f n = 0}"
 | 
| 63433 | 58 | morphisms coeff Abs_poly | 
| 59 | by (auto intro!: ALL_MOST) | |
| 29451 | 60 | |
| 59487 
adaa430fc0f7
default abstypes and default abstract equations make technical (no_code) annotation superfluous
 haftmann parents: 
58881diff
changeset | 61 | setup_lifting type_definition_poly | 
| 52380 | 62 | |
| 63 | 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 | 64 | by (simp add: coeff_inject [symmetric] fun_eq_iff) | 
| 29451 | 65 | |
| 52380 | 66 | lemma poly_eqI: "(\<And>n. coeff p n = coeff q n) \<Longrightarrow> p = q" | 
| 67 | by (simp add: poly_eq_iff) | |
| 68 | ||
| 59983 
cd2efd7d06bd
replace almost_everywhere_zero by Infinite_Set.MOST
 hoelzl parents: 
59815diff
changeset | 69 | lemma MOST_coeff_eq_0: "\<forall>\<^sub>\<infinity> n. coeff p n = 0" | 
| 52380 | 70 | using coeff [of p] by simp | 
| 29451 | 71 | |
| 72 | ||
| 60500 | 73 | subsection \<open>Degree of a polynomial\<close> | 
| 29451 | 74 | |
| 52380 | 75 | definition degree :: "'a::zero poly \<Rightarrow> nat" | 
| 76 | where | |
| 29451 | 77 | "degree p = (LEAST n. \<forall>i>n. coeff p i = 0)" | 
| 78 | ||
| 52380 | 79 | lemma coeff_eq_0: | 
| 80 | assumes "degree p < n" | |
| 81 | shows "coeff p n = 0" | |
| 29451 | 82 | proof - | 
| 59983 
cd2efd7d06bd
replace almost_everywhere_zero by Infinite_Set.MOST
 hoelzl parents: 
59815diff
changeset | 83 | have "\<exists>n. \<forall>i>n. coeff p i = 0" | 
| 
cd2efd7d06bd
replace almost_everywhere_zero by Infinite_Set.MOST
 hoelzl parents: 
59815diff
changeset | 84 | using MOST_coeff_eq_0 by (simp add: MOST_nat) | 
| 52380 | 85 | then have "\<forall>i>degree p. coeff p i = 0" | 
| 29451 | 86 | unfolding degree_def by (rule LeastI_ex) | 
| 52380 | 87 | with assms show ?thesis by simp | 
| 29451 | 88 | qed | 
| 89 | ||
| 90 | lemma le_degree: "coeff p n \<noteq> 0 \<Longrightarrow> n \<le> degree p" | |
| 91 | by (erule contrapos_np, rule coeff_eq_0, simp) | |
| 92 | ||
| 93 | lemma degree_le: "\<forall>i>n. coeff p i = 0 \<Longrightarrow> degree p \<le> n" | |
| 94 | unfolding degree_def by (erule Least_le) | |
| 95 | ||
| 96 | lemma less_degree_imp: "n < degree p \<Longrightarrow> \<exists>i>n. coeff p i \<noteq> 0" | |
| 97 | unfolding degree_def by (drule not_less_Least, simp) | |
| 98 | ||
| 99 | ||
| 60500 | 100 | subsection \<open>The zero polynomial\<close> | 
| 29451 | 101 | |
| 102 | instantiation poly :: (zero) zero | |
| 103 | begin | |
| 104 | ||
| 52380 | 105 | lift_definition zero_poly :: "'a poly" | 
| 59983 
cd2efd7d06bd
replace almost_everywhere_zero by Infinite_Set.MOST
 hoelzl parents: 
59815diff
changeset | 106 | is "\<lambda>_. 0" by (rule MOST_I) simp | 
| 29451 | 107 | |
| 108 | instance .. | |
| 52380 | 109 | |
| 29451 | 110 | end | 
| 111 | ||
| 52380 | 112 | lemma coeff_0 [simp]: | 
| 113 | "coeff 0 n = 0" | |
| 114 | by transfer rule | |
| 29451 | 115 | |
| 52380 | 116 | lemma degree_0 [simp]: | 
| 117 | "degree 0 = 0" | |
| 29451 | 118 | by (rule order_antisym [OF degree_le le0]) simp | 
| 119 | ||
| 120 | lemma leading_coeff_neq_0: | |
| 52380 | 121 | assumes "p \<noteq> 0" | 
| 122 | shows "coeff p (degree p) \<noteq> 0" | |
| 29451 | 123 | proof (cases "degree p") | 
| 124 | case 0 | |
| 60500 | 125 | from \<open>p \<noteq> 0\<close> have "\<exists>n. coeff p n \<noteq> 0" | 
| 52380 | 126 | by (simp add: poly_eq_iff) | 
| 29451 | 127 | then obtain n where "coeff p n \<noteq> 0" .. | 
| 128 | hence "n \<le> degree p" by (rule le_degree) | |
| 60500 | 129 | with \<open>coeff p n \<noteq> 0\<close> and \<open>degree p = 0\<close> | 
| 29451 | 130 | show "coeff p (degree p) \<noteq> 0" by simp | 
| 131 | next | |
| 132 | case (Suc n) | |
| 60500 | 133 | from \<open>degree p = Suc n\<close> have "n < degree p" by simp | 
| 29451 | 134 | hence "\<exists>i>n. coeff p i \<noteq> 0" by (rule less_degree_imp) | 
| 135 | then obtain i where "n < i" and "coeff p i \<noteq> 0" by fast | |
| 60500 | 136 | from \<open>degree p = Suc n\<close> and \<open>n < i\<close> have "degree p \<le> i" by simp | 
| 137 | also from \<open>coeff p i \<noteq> 0\<close> have "i \<le> degree p" by (rule le_degree) | |
| 29451 | 138 | finally have "degree p = i" . | 
| 60500 | 139 | with \<open>coeff p i \<noteq> 0\<close> show "coeff p (degree p) \<noteq> 0" by simp | 
| 29451 | 140 | qed | 
| 141 | ||
| 52380 | 142 | lemma leading_coeff_0_iff [simp]: | 
| 143 | "coeff p (degree p) = 0 \<longleftrightarrow> p = 0" | |
| 29451 | 144 | by (cases "p = 0", simp, simp add: leading_coeff_neq_0) | 
| 145 | ||
| 146 | ||
| 60500 | 147 | subsection \<open>List-style constructor for polynomials\<close> | 
| 29451 | 148 | |
| 52380 | 149 | lift_definition pCons :: "'a::zero \<Rightarrow> 'a poly \<Rightarrow> 'a poly" | 
| 55415 | 150 | is "\<lambda>a p. case_nat a (coeff p)" | 
| 59983 
cd2efd7d06bd
replace almost_everywhere_zero by Infinite_Set.MOST
 hoelzl parents: 
59815diff
changeset | 151 | by (rule MOST_SucD) (simp add: MOST_coeff_eq_0) | 
| 29451 | 152 | |
| 52380 | 153 | lemmas coeff_pCons = pCons.rep_eq | 
| 29455 | 154 | |
| 52380 | 155 | lemma coeff_pCons_0 [simp]: | 
| 156 | "coeff (pCons a p) 0 = a" | |
| 157 | by transfer simp | |
| 29455 | 158 | |
| 52380 | 159 | lemma coeff_pCons_Suc [simp]: | 
| 160 | "coeff (pCons a p) (Suc n) = coeff p n" | |
| 29451 | 161 | by (simp add: coeff_pCons) | 
| 162 | ||
| 52380 | 163 | lemma degree_pCons_le: | 
| 164 | "degree (pCons a p) \<le> Suc (degree p)" | |
| 165 | by (rule degree_le) (simp add: coeff_eq_0 coeff_pCons split: nat.split) | |
| 29451 | 166 | |
| 167 | lemma degree_pCons_eq: | |
| 168 | "p \<noteq> 0 \<Longrightarrow> degree (pCons a p) = Suc (degree p)" | |
| 52380 | 169 | apply (rule order_antisym [OF degree_pCons_le]) | 
| 170 | apply (rule le_degree, simp) | |
| 171 | done | |
| 29451 | 172 | |
| 52380 | 173 | lemma degree_pCons_0: | 
| 174 | "degree (pCons a 0) = 0" | |
| 175 | apply (rule order_antisym [OF _ le0]) | |
| 176 | apply (rule degree_le, simp add: coeff_pCons split: nat.split) | |
| 177 | done | |
| 29451 | 178 | |
| 29460 
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
 huffman parents: 
29457diff
changeset | 179 | lemma degree_pCons_eq_if [simp]: | 
| 29451 | 180 | "degree (pCons a p) = (if p = 0 then 0 else Suc (degree p))" | 
| 52380 | 181 | apply (cases "p = 0", simp_all) | 
| 182 | apply (rule order_antisym [OF _ le0]) | |
| 183 | apply (rule degree_le, simp add: coeff_pCons split: nat.split) | |
| 184 | apply (rule order_antisym [OF degree_pCons_le]) | |
| 185 | apply (rule le_degree, simp) | |
| 186 | done | |
| 29451 | 187 | |
| 52380 | 188 | lemma pCons_0_0 [simp]: | 
| 189 | "pCons 0 0 = 0" | |
| 190 | by (rule poly_eqI) (simp add: coeff_pCons split: nat.split) | |
| 29451 | 191 | |
| 192 | lemma pCons_eq_iff [simp]: | |
| 193 | "pCons a p = pCons b q \<longleftrightarrow> a = b \<and> p = q" | |
| 52380 | 194 | proof safe | 
| 29451 | 195 | assume "pCons a p = pCons b q" | 
| 196 | then have "coeff (pCons a p) 0 = coeff (pCons b q) 0" by simp | |
| 197 | then show "a = b" by simp | |
| 198 | next | |
| 199 | assume "pCons a p = pCons b q" | |
| 200 | then have "\<forall>n. coeff (pCons a p) (Suc n) = | |
| 201 | coeff (pCons b q) (Suc n)" by simp | |
| 52380 | 202 | then show "p = q" by (simp add: poly_eq_iff) | 
| 29451 | 203 | qed | 
| 204 | ||
| 52380 | 205 | lemma pCons_eq_0_iff [simp]: | 
| 206 | "pCons a p = 0 \<longleftrightarrow> a = 0 \<and> p = 0" | |
| 29451 | 207 | using pCons_eq_iff [of a p 0 0] by simp | 
| 208 | ||
| 209 | lemma pCons_cases [cases type: poly]: | |
| 210 | obtains (pCons) a q where "p = pCons a q" | |
| 211 | proof | |
| 212 | show "p = pCons (coeff p 0) (Abs_poly (\<lambda>n. coeff p (Suc n)))" | |
| 52380 | 213 | by transfer | 
| 59983 
cd2efd7d06bd
replace almost_everywhere_zero by Infinite_Set.MOST
 hoelzl parents: 
59815diff
changeset | 214 | (simp_all add: MOST_inj[where f=Suc and P="\<lambda>n. p n = 0" for p] fun_eq_iff Abs_poly_inverse | 
| 
cd2efd7d06bd
replace almost_everywhere_zero by Infinite_Set.MOST
 hoelzl parents: 
59815diff
changeset | 215 | split: nat.split) | 
| 29451 | 216 | qed | 
| 217 | ||
| 218 | lemma pCons_induct [case_names 0 pCons, induct type: poly]: | |
| 219 | assumes zero: "P 0" | |
| 54856 | 220 | assumes pCons: "\<And>a p. a \<noteq> 0 \<or> p \<noteq> 0 \<Longrightarrow> P p \<Longrightarrow> P (pCons a p)" | 
| 29451 | 221 | shows "P p" | 
| 222 | proof (induct p rule: measure_induct_rule [where f=degree]) | |
| 223 | case (less p) | |
| 224 | obtain a q where "p = pCons a q" by (rule pCons_cases) | |
| 225 | have "P q" | |
| 226 | proof (cases "q = 0") | |
| 227 | case True | |
| 228 | then show "P q" by (simp add: zero) | |
| 229 | next | |
| 230 | case False | |
| 231 | then have "degree (pCons a q) = Suc (degree q)" | |
| 232 | by (rule degree_pCons_eq) | |
| 233 | then have "degree q < degree p" | |
| 60500 | 234 | using \<open>p = pCons a q\<close> by simp | 
| 29451 | 235 | then show "P q" | 
| 236 | by (rule less.hyps) | |
| 237 | qed | |
| 54856 | 238 | have "P (pCons a q)" | 
| 239 | proof (cases "a \<noteq> 0 \<or> q \<noteq> 0") | |
| 240 | case True | |
| 60500 | 241 | with \<open>P q\<close> show ?thesis by (auto intro: pCons) | 
| 54856 | 242 | next | 
| 243 | case False | |
| 244 | with zero show ?thesis by simp | |
| 245 | qed | |
| 29451 | 246 | then show ?case | 
| 60500 | 247 | using \<open>p = pCons a q\<close> by simp | 
| 29451 | 248 | qed | 
| 249 | ||
| 60570 | 250 | lemma degree_eq_zeroE: | 
| 251 | fixes p :: "'a::zero poly" | |
| 252 | assumes "degree p = 0" | |
| 253 | obtains a where "p = pCons a 0" | |
| 254 | proof - | |
| 255 | obtain a q where p: "p = pCons a q" by (cases p) | |
| 256 | with assms have "q = 0" by (cases "q = 0") simp_all | |
| 257 | with p have "p = pCons a 0" by simp | |
| 258 | with that show thesis . | |
| 259 | qed | |
| 260 | ||
| 29451 | 261 | |
| 62422 | 262 | subsection \<open>Quickcheck generator for polynomials\<close> | 
| 263 | ||
| 264 | quickcheck_generator poly constructors: "0 :: _ poly", pCons | |
| 265 | ||
| 266 | ||
| 60500 | 267 | subsection \<open>List-style syntax for polynomials\<close> | 
| 52380 | 268 | |
| 269 | syntax | |
| 270 |   "_poly" :: "args \<Rightarrow> 'a poly"  ("[:(_):]")
 | |
| 271 | ||
| 272 | translations | |
| 273 | "[:x, xs:]" == "CONST pCons x [:xs:]" | |
| 274 | "[:x:]" == "CONST pCons x 0" | |
| 275 | "[:x:]" <= "CONST pCons x (_constrain 0 t)" | |
| 276 | ||
| 277 | ||
| 60500 | 278 | subsection \<open>Representation of polynomials by lists of coefficients\<close> | 
| 52380 | 279 | |
| 280 | primrec Poly :: "'a::zero list \<Rightarrow> 'a poly" | |
| 281 | where | |
| 54855 | 282 | [code_post]: "Poly [] = 0" | 
| 283 | | [code_post]: "Poly (a # as) = pCons a (Poly as)" | |
| 52380 | 284 | |
| 285 | lemma Poly_replicate_0 [simp]: | |
| 286 | "Poly (replicate n 0) = 0" | |
| 287 | by (induct n) simp_all | |
| 288 | ||
| 289 | lemma Poly_eq_0: | |
| 290 | "Poly as = 0 \<longleftrightarrow> (\<exists>n. as = replicate n 0)" | |
| 291 | 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 | 292 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 293 | lemma Poly_append_replicate_zero [simp]: | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 294 | "Poly (as @ replicate n 0) = Poly as" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 295 | 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 | 296 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 297 | lemma Poly_snoc_zero [simp]: | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 298 | "Poly (as @ [0]) = Poly as" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 299 | 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 | 300 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 301 | lemma Poly_cCons_eq_pCons_Poly [simp]: | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 302 | "Poly (a ## p) = pCons a (Poly p)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 303 | 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 | 304 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 305 | lemma Poly_on_rev_starting_with_0 [simp]: | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 306 | assumes "hd as = 0" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 307 | shows "Poly (rev (tl as)) = Poly (rev as)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 308 | using assms by (cases as) simp_all | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 309 | |
| 62065 | 310 | lemma degree_Poly: "degree (Poly xs) \<le> length xs" | 
| 311 | by (induction xs) simp_all | |
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 312 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 313 | lemma coeff_Poly_eq [simp]: | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 314 | "coeff (Poly xs) = nth_default 0 xs" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 315 | 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 | 316 | |
| 52380 | 317 | definition coeffs :: "'a poly \<Rightarrow> 'a::zero list" | 
| 318 | where | |
| 319 | "coeffs p = (if p = 0 then [] else map (\<lambda>i. coeff p i) [0 ..< Suc (degree p)])" | |
| 320 | ||
| 321 | lemma coeffs_eq_Nil [simp]: | |
| 322 | "coeffs p = [] \<longleftrightarrow> p = 0" | |
| 323 | by (simp add: coeffs_def) | |
| 324 | ||
| 325 | lemma not_0_coeffs_not_Nil: | |
| 326 | "p \<noteq> 0 \<Longrightarrow> coeffs p \<noteq> []" | |
| 327 | by simp | |
| 328 | ||
| 329 | lemma coeffs_0_eq_Nil [simp]: | |
| 330 | "coeffs 0 = []" | |
| 331 | by simp | |
| 29454 
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
 huffman parents: 
29453diff
changeset | 332 | |
| 52380 | 333 | lemma coeffs_pCons_eq_cCons [simp]: | 
| 334 | "coeffs (pCons a p) = a ## coeffs p" | |
| 335 | proof - | |
| 336 |   { fix ms :: "nat list" and f :: "nat \<Rightarrow> 'a" and x :: "'a"
 | |
| 337 | assume "\<forall>m\<in>set ms. m > 0" | |
| 55415 | 338 | then have "map (case_nat x f) ms = map f (map (\<lambda>n. n - 1) ms)" | 
| 58199 
5fbe474b5da8
explicit theory with additional, less commonly used list operations
 haftmann parents: 
57862diff
changeset | 339 | by (induct ms) (auto split: nat.split) | 
| 
5fbe474b5da8
explicit theory with additional, less commonly used list operations
 haftmann parents: 
57862diff
changeset | 340 | } | 
| 52380 | 341 | note * = this | 
| 342 | show ?thesis | |
| 60570 | 343 | by (simp add: coeffs_def * upt_conv_Cons coeff_pCons map_decr_upt del: upt_Suc) | 
| 52380 | 344 | qed | 
| 345 | ||
| 62065 | 346 | lemma length_coeffs: "p \<noteq> 0 \<Longrightarrow> length (coeffs p) = degree p + 1" | 
| 347 | by (simp add: coeffs_def) | |
| 348 | ||
| 349 | lemma coeffs_nth: | |
| 350 | assumes "p \<noteq> 0" "n \<le> degree p" | |
| 351 | shows "coeffs p ! n = coeff p n" | |
| 352 | using assms unfolding coeffs_def by (auto simp del: upt_Suc) | |
| 353 | ||
| 52380 | 354 | lemma not_0_cCons_eq [simp]: | 
| 355 | "p \<noteq> 0 \<Longrightarrow> a ## coeffs p = a # coeffs p" | |
| 356 | by (simp add: cCons_def) | |
| 357 | ||
| 358 | lemma Poly_coeffs [simp, code abstype]: | |
| 359 | "Poly (coeffs p) = p" | |
| 54856 | 360 | by (induct p) auto | 
| 52380 | 361 | |
| 362 | lemma coeffs_Poly [simp]: | |
| 363 | "coeffs (Poly as) = strip_while (HOL.eq 0) as" | |
| 364 | proof (induct as) | |
| 365 | case Nil then show ?case by simp | |
| 366 | next | |
| 367 | case (Cons a as) | |
| 368 | have "(\<forall>n. as \<noteq> replicate n 0) \<longleftrightarrow> (\<exists>a\<in>set as. a \<noteq> 0)" | |
| 369 | using replicate_length_same [of as 0] by (auto dest: sym [of _ as]) | |
| 370 | with Cons show ?case by auto | |
| 371 | qed | |
| 372 | ||
| 373 | lemma last_coeffs_not_0: | |
| 374 | "p \<noteq> 0 \<Longrightarrow> last (coeffs p) \<noteq> 0" | |
| 375 | by (induct p) (auto simp add: cCons_def) | |
| 376 | ||
| 377 | lemma strip_while_coeffs [simp]: | |
| 378 | "strip_while (HOL.eq 0) (coeffs p) = coeffs p" | |
| 379 | by (cases "p = 0") (auto dest: last_coeffs_not_0 intro: strip_while_not_last) | |
| 380 | ||
| 381 | lemma coeffs_eq_iff: | |
| 382 | "p = q \<longleftrightarrow> coeffs p = coeffs q" (is "?P \<longleftrightarrow> ?Q") | |
| 383 | proof | |
| 384 | assume ?P then show ?Q by simp | |
| 385 | next | |
| 386 | assume ?Q | |
| 387 | then have "Poly (coeffs p) = Poly (coeffs q)" by simp | |
| 388 | then show ?P by simp | |
| 389 | qed | |
| 390 | ||
| 391 | lemma nth_default_coeffs_eq: | |
| 392 | "nth_default 0 (coeffs p) = coeff p" | |
| 393 | by (simp add: fun_eq_iff coeff_Poly_eq [symmetric]) | |
| 394 | ||
| 395 | lemma [code]: | |
| 396 | "coeff p = nth_default 0 (coeffs p)" | |
| 397 | by (simp add: nth_default_coeffs_eq) | |
| 398 | ||
| 399 | lemma coeffs_eqI: | |
| 400 | assumes coeff: "\<And>n. coeff p n = nth_default 0 xs n" | |
| 401 | assumes zero: "xs \<noteq> [] \<Longrightarrow> last xs \<noteq> 0" | |
| 402 | shows "coeffs p = xs" | |
| 403 | proof - | |
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 404 | from coeff have "p = Poly xs" by (simp add: poly_eq_iff) | 
| 52380 | 405 | with zero show ?thesis by simp (cases xs, simp_all) | 
| 406 | qed | |
| 407 | ||
| 408 | lemma degree_eq_length_coeffs [code]: | |
| 409 | "degree p = length (coeffs p) - 1" | |
| 410 | by (simp add: coeffs_def) | |
| 411 | ||
| 412 | lemma length_coeffs_degree: | |
| 413 | "p \<noteq> 0 \<Longrightarrow> length (coeffs p) = Suc (degree p)" | |
| 414 | by (induct p) (auto simp add: cCons_def) | |
| 415 | ||
| 416 | lemma [code abstract]: | |
| 417 | "coeffs 0 = []" | |
| 418 | by (fact coeffs_0_eq_Nil) | |
| 419 | ||
| 420 | lemma [code abstract]: | |
| 421 | "coeffs (pCons a p) = a ## coeffs p" | |
| 422 | by (fact coeffs_pCons_eq_cCons) | |
| 423 | ||
| 424 | instantiation poly :: ("{zero, equal}") equal
 | |
| 425 | begin | |
| 426 | ||
| 427 | definition | |
| 428 | [code]: "HOL.equal (p::'a poly) q \<longleftrightarrow> HOL.equal (coeffs p) (coeffs q)" | |
| 429 | ||
| 60679 | 430 | instance | 
| 431 | by standard (simp add: equal equal_poly_def coeffs_eq_iff) | |
| 52380 | 432 | |
| 433 | end | |
| 434 | ||
| 60679 | 435 | lemma [code nbe]: "HOL.equal (p :: _ poly) p \<longleftrightarrow> True" | 
| 52380 | 436 | by (fact equal_refl) | 
| 29454 
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
 huffman parents: 
29453diff
changeset | 437 | |
| 52380 | 438 | definition is_zero :: "'a::zero poly \<Rightarrow> bool" | 
| 439 | where | |
| 440 | [code]: "is_zero p \<longleftrightarrow> List.null (coeffs p)" | |
| 441 | ||
| 442 | lemma is_zero_null [code_abbrev]: | |
| 443 | "is_zero p \<longleftrightarrow> p = 0" | |
| 444 | by (simp add: is_zero_def null_def) | |
| 445 | ||
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 446 | subsubsection \<open>Reconstructing the polynomial from the list\<close> | 
| 63145 | 447 | \<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 | 448 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 449 | definition poly_of_list :: "'a::comm_monoid_add list \<Rightarrow> 'a poly" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 450 | where | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 451 | [simp]: "poly_of_list = Poly" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 452 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 453 | lemma poly_of_list_impl [code abstract]: | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 454 | "coeffs (poly_of_list as) = strip_while (HOL.eq 0) as" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 455 | by simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 456 | |
| 52380 | 457 | |
| 60500 | 458 | subsection \<open>Fold combinator for polynomials\<close> | 
| 52380 | 459 | |
| 460 | definition fold_coeffs :: "('a::zero \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a poly \<Rightarrow> 'b \<Rightarrow> 'b"
 | |
| 461 | where | |
| 462 | "fold_coeffs f p = foldr f (coeffs p)" | |
| 463 | ||
| 464 | lemma fold_coeffs_0_eq [simp]: | |
| 465 | "fold_coeffs f 0 = id" | |
| 466 | by (simp add: fold_coeffs_def) | |
| 467 | ||
| 468 | lemma fold_coeffs_pCons_eq [simp]: | |
| 469 | "f 0 = id \<Longrightarrow> fold_coeffs f (pCons a p) = f a \<circ> fold_coeffs f p" | |
| 470 | 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 | 471 | |
| 52380 | 472 | lemma fold_coeffs_pCons_0_0_eq [simp]: | 
| 473 | "fold_coeffs f (pCons 0 0) = id" | |
| 474 | by (simp add: fold_coeffs_def) | |
| 475 | ||
| 476 | lemma fold_coeffs_pCons_coeff_not_0_eq [simp]: | |
| 477 | "a \<noteq> 0 \<Longrightarrow> fold_coeffs f (pCons a p) = f a \<circ> fold_coeffs f p" | |
| 478 | by (simp add: fold_coeffs_def) | |
| 479 | ||
| 480 | lemma fold_coeffs_pCons_not_0_0_eq [simp]: | |
| 481 | "p \<noteq> 0 \<Longrightarrow> fold_coeffs f (pCons a p) = f a \<circ> fold_coeffs f p" | |
| 482 | by (simp add: fold_coeffs_def) | |
| 483 | ||
| 60500 | 484 | subsection \<open>Canonical morphism on polynomials -- evaluation\<close> | 
| 52380 | 485 | |
| 486 | definition poly :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a" | |
| 487 | where | |
| 61585 | 488 | "poly p = fold_coeffs (\<lambda>a f x. a + x * f x) p (\<lambda>x. 0)" \<comment> \<open>The Horner Schema\<close> | 
| 52380 | 489 | |
| 490 | lemma poly_0 [simp]: | |
| 491 | "poly 0 x = 0" | |
| 492 | by (simp add: poly_def) | |
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 493 | |
| 52380 | 494 | lemma poly_pCons [simp]: | 
| 495 | "poly (pCons a p) x = a + x * poly p x" | |
| 496 | 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 | 497 | |
| 62065 | 498 | lemma poly_altdef: | 
| 499 |   "poly p (x :: 'a :: {comm_semiring_0, semiring_1}) = (\<Sum>i\<le>degree p. coeff p i * x ^ i)"
 | |
| 500 | proof (induction p rule: pCons_induct) | |
| 501 | case (pCons a p) | |
| 502 | show ?case | |
| 503 | proof (cases "p = 0") | |
| 504 | case False | |
| 505 | let ?p' = "pCons a p" | |
| 506 | note poly_pCons[of a p x] | |
| 507 | also note pCons.IH | |
| 508 | also have "a + x * (\<Sum>i\<le>degree p. coeff p i * x ^ i) = | |
| 509 | coeff ?p' 0 * x^0 + (\<Sum>i\<le>degree p. coeff ?p' (Suc i) * x^Suc i)" | |
| 64267 | 510 | by (simp add: field_simps sum_distrib_left coeff_pCons) | 
| 511 | also note sum_atMost_Suc_shift[symmetric] | |
| 62072 | 512 | also note degree_pCons_eq[OF \<open>p \<noteq> 0\<close>, of a, symmetric] | 
| 62065 | 513 | finally show ?thesis . | 
| 514 | qed simp | |
| 515 | qed simp | |
| 516 | ||
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 517 | 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 | 518 | 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 | 519 | |
| 29454 
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
 huffman parents: 
29453diff
changeset | 520 | |
| 60500 | 521 | subsection \<open>Monomials\<close> | 
| 29451 | 522 | |
| 52380 | 523 | lift_definition monom :: "'a \<Rightarrow> nat \<Rightarrow> 'a::zero poly" | 
| 524 | 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 | 525 | by (simp add: MOST_iff_cofinite) | 
| 52380 | 526 | |
| 527 | lemma coeff_monom [simp]: | |
| 528 | "coeff (monom a m) n = (if m = n then a else 0)" | |
| 529 | by transfer rule | |
| 29451 | 530 | |
| 52380 | 531 | lemma monom_0: | 
| 532 | "monom a 0 = pCons a 0" | |
| 533 | by (rule poly_eqI) (simp add: coeff_pCons split: nat.split) | |
| 29451 | 534 | |
| 52380 | 535 | lemma monom_Suc: | 
| 536 | "monom a (Suc n) = pCons 0 (monom a n)" | |
| 537 | by (rule poly_eqI) (simp add: coeff_pCons split: nat.split) | |
| 29451 | 538 | |
| 539 | lemma monom_eq_0 [simp]: "monom 0 n = 0" | |
| 52380 | 540 | by (rule poly_eqI) simp | 
| 29451 | 541 | |
| 542 | lemma monom_eq_0_iff [simp]: "monom a n = 0 \<longleftrightarrow> a = 0" | |
| 52380 | 543 | by (simp add: poly_eq_iff) | 
| 29451 | 544 | |
| 545 | lemma monom_eq_iff [simp]: "monom a n = monom b n \<longleftrightarrow> a = b" | |
| 52380 | 546 | by (simp add: poly_eq_iff) | 
| 29451 | 547 | |
| 548 | lemma degree_monom_le: "degree (monom a n) \<le> n" | |
| 549 | by (rule degree_le, simp) | |
| 550 | ||
| 551 | lemma degree_monom_eq: "a \<noteq> 0 \<Longrightarrow> degree (monom a n) = n" | |
| 552 | apply (rule order_antisym [OF degree_monom_le]) | |
| 553 | apply (rule le_degree, simp) | |
| 554 | done | |
| 555 | ||
| 52380 | 556 | lemma coeffs_monom [code abstract]: | 
| 557 | "coeffs (monom a n) = (if a = 0 then [] else replicate n 0 @ [a])" | |
| 558 | by (induct n) (simp_all add: monom_0 monom_Suc) | |
| 559 | ||
| 560 | lemma fold_coeffs_monom [simp]: | |
| 561 | "a \<noteq> 0 \<Longrightarrow> fold_coeffs f (monom a n) = f 0 ^^ n \<circ> f a" | |
| 562 | by (simp add: fold_coeffs_def coeffs_monom fun_eq_iff) | |
| 563 | ||
| 564 | lemma poly_monom: | |
| 565 |   fixes a x :: "'a::{comm_semiring_1}"
 | |
| 566 | shows "poly (monom a n) x = a * x ^ n" | |
| 567 | by (cases "a = 0", simp_all) | |
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 568 | (induct n, simp_all add: mult.left_commute poly_def) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 569 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 570 | 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 | 571 | by (auto simp: poly_eq_iff) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 572 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 573 | 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 | 574 | using monom_eq_iff'[of c n d 0] by (simp add: monom_0) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 575 | |
| 62065 | 576 | |
| 60500 | 577 | subsection \<open>Addition and subtraction\<close> | 
| 29451 | 578 | |
| 579 | instantiation poly :: (comm_monoid_add) comm_monoid_add | |
| 580 | begin | |
| 581 | ||
| 52380 | 582 | lift_definition plus_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" | 
| 583 | 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 | 584 | proof - | 
| 60679 | 585 | fix q p :: "'a poly" | 
| 586 | 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 | 587 | using MOST_coeff_eq_0[of p] MOST_coeff_eq_0[of q] by eventually_elim simp | 
| 52380 | 588 | qed | 
| 29451 | 589 | |
| 60679 | 590 | lemma coeff_add [simp]: "coeff (p + q) n = coeff p n + coeff q n" | 
| 52380 | 591 | by (simp add: plus_poly.rep_eq) | 
| 29451 | 592 | |
| 60679 | 593 | instance | 
| 594 | proof | |
| 29451 | 595 | fix p q r :: "'a poly" | 
| 596 | show "(p + q) + r = p + (q + r)" | |
| 57512 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 haftmann parents: 
57482diff
changeset | 597 | by (simp add: poly_eq_iff add.assoc) | 
| 29451 | 598 | show "p + q = q + p" | 
| 57512 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 haftmann parents: 
57482diff
changeset | 599 | by (simp add: poly_eq_iff add.commute) | 
| 29451 | 600 | show "0 + p = p" | 
| 52380 | 601 | by (simp add: poly_eq_iff) | 
| 29451 | 602 | qed | 
| 603 | ||
| 604 | end | |
| 605 | ||
| 59815 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 606 | instantiation poly :: (cancel_comm_monoid_add) cancel_comm_monoid_add | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 607 | begin | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 608 | |
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 609 | lift_definition minus_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 610 | 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 | 611 | proof - | 
| 60679 | 612 | fix q p :: "'a poly" | 
| 613 | 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 | 614 | 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 | 615 | qed | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 616 | |
| 60679 | 617 | 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 | 618 | by (simp add: minus_poly.rep_eq) | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 619 | |
| 60679 | 620 | instance | 
| 621 | proof | |
| 29540 | 622 | fix p q r :: "'a poly" | 
| 59815 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 623 | show "p + q - p = q" | 
| 52380 | 624 | by (simp add: poly_eq_iff) | 
| 59815 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 625 | show "p - q - r = p - (q + r)" | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 626 | by (simp add: poly_eq_iff diff_diff_eq) | 
| 29540 | 627 | qed | 
| 628 | ||
| 59815 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 629 | end | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 630 | |
| 29451 | 631 | instantiation poly :: (ab_group_add) ab_group_add | 
| 632 | begin | |
| 633 | ||
| 52380 | 634 | lift_definition uminus_poly :: "'a poly \<Rightarrow> 'a poly" | 
| 635 | 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 | 636 | proof - | 
| 60679 | 637 | fix p :: "'a poly" | 
| 638 | 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 | 639 | using MOST_coeff_eq_0 by simp | 
| 52380 | 640 | qed | 
| 29451 | 641 | |
| 642 | lemma coeff_minus [simp]: "coeff (- p) n = - coeff p n" | |
| 52380 | 643 | by (simp add: uminus_poly.rep_eq) | 
| 29451 | 644 | |
| 60679 | 645 | instance | 
| 646 | proof | |
| 29451 | 647 | fix p q :: "'a poly" | 
| 648 | show "- p + p = 0" | |
| 52380 | 649 | by (simp add: poly_eq_iff) | 
| 29451 | 650 | show "p - q = p + - q" | 
| 54230 
b1d955791529
more simplification rules on unary and binary minus
 haftmann parents: 
52380diff
changeset | 651 | by (simp add: poly_eq_iff) | 
| 29451 | 652 | qed | 
| 653 | ||
| 654 | end | |
| 655 | ||
| 656 | lemma add_pCons [simp]: | |
| 657 | "pCons a p + pCons b q = pCons (a + b) (p + q)" | |
| 52380 | 658 | by (rule poly_eqI, simp add: coeff_pCons split: nat.split) | 
| 29451 | 659 | |
| 660 | lemma minus_pCons [simp]: | |
| 661 | "- pCons a p = pCons (- a) (- p)" | |
| 52380 | 662 | by (rule poly_eqI, simp add: coeff_pCons split: nat.split) | 
| 29451 | 663 | |
| 664 | lemma diff_pCons [simp]: | |
| 665 | "pCons a p - pCons b q = pCons (a - b) (p - q)" | |
| 52380 | 666 | by (rule poly_eqI, simp add: coeff_pCons split: nat.split) | 
| 29451 | 667 | |
| 29539 | 668 | lemma degree_add_le_max: "degree (p + q) \<le> max (degree p) (degree q)" | 
| 29451 | 669 | by (rule degree_le, auto simp add: coeff_eq_0) | 
| 670 | ||
| 29539 | 671 | lemma degree_add_le: | 
| 672 | "\<lbrakk>degree p \<le> n; degree q \<le> n\<rbrakk> \<Longrightarrow> degree (p + q) \<le> n" | |
| 673 | by (auto intro: order_trans degree_add_le_max) | |
| 674 | ||
| 29453 | 675 | lemma degree_add_less: | 
| 676 | "\<lbrakk>degree p < n; degree q < n\<rbrakk> \<Longrightarrow> degree (p + q) < n" | |
| 29539 | 677 | by (auto intro: le_less_trans degree_add_le_max) | 
| 29453 | 678 | |
| 29451 | 679 | lemma degree_add_eq_right: | 
| 680 | "degree p < degree q \<Longrightarrow> degree (p + q) = degree q" | |
| 681 | apply (cases "q = 0", simp) | |
| 682 | apply (rule order_antisym) | |
| 29539 | 683 | apply (simp add: degree_add_le) | 
| 29451 | 684 | apply (rule le_degree) | 
| 685 | apply (simp add: coeff_eq_0) | |
| 686 | done | |
| 687 | ||
| 688 | lemma degree_add_eq_left: | |
| 689 | "degree q < degree p \<Longrightarrow> degree (p + q) = degree p" | |
| 690 | using degree_add_eq_right [of q p] | |
| 57512 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 haftmann parents: 
57482diff
changeset | 691 | by (simp add: add.commute) | 
| 29451 | 692 | |
| 59815 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 693 | lemma degree_minus [simp]: | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 694 | "degree (- p) = degree p" | 
| 29451 | 695 | unfolding degree_def by simp | 
| 696 | ||
| 59815 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 697 | lemma degree_diff_le_max: | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 698 | fixes p q :: "'a :: ab_group_add poly" | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 699 | shows "degree (p - q) \<le> max (degree p) (degree q)" | 
| 29451 | 700 | using degree_add_le [where p=p and q="-q"] | 
| 54230 
b1d955791529
more simplification rules on unary and binary minus
 haftmann parents: 
52380diff
changeset | 701 | by simp | 
| 29451 | 702 | |
| 29539 | 703 | lemma degree_diff_le: | 
| 59815 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 704 | fixes p q :: "'a :: ab_group_add poly" | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 705 | assumes "degree p \<le> n" and "degree q \<le> n" | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 706 | shows "degree (p - q) \<le> n" | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 707 | using assms degree_add_le [of p n "- q"] by simp | 
| 29539 | 708 | |
| 29453 | 709 | lemma degree_diff_less: | 
| 59815 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 710 | fixes p q :: "'a :: ab_group_add poly" | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 711 | assumes "degree p < n" and "degree q < n" | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 712 | shows "degree (p - q) < n" | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 713 | using assms degree_add_less [of p n "- q"] by simp | 
| 29453 | 714 | |
| 29451 | 715 | lemma add_monom: "monom a n + monom b n = monom (a + b) n" | 
| 52380 | 716 | by (rule poly_eqI) simp | 
| 29451 | 717 | |
| 718 | lemma diff_monom: "monom a n - monom b n = monom (a - b) n" | |
| 52380 | 719 | by (rule poly_eqI) simp | 
| 29451 | 720 | |
| 721 | lemma minus_monom: "- monom a n = monom (-a) n" | |
| 52380 | 722 | by (rule poly_eqI) simp | 
| 29451 | 723 | |
| 64267 | 724 | lemma coeff_sum: "coeff (\<Sum>x\<in>A. p x) i = (\<Sum>x\<in>A. coeff (p x) i)" | 
| 29451 | 725 | by (cases "finite A", induct set: finite, simp_all) | 
| 726 | ||
| 64267 | 727 | lemma monom_sum: "monom (\<Sum>x\<in>A. a x) n = (\<Sum>x\<in>A. monom (a x) n)" | 
| 728 | by (rule poly_eqI) (simp add: coeff_sum) | |
| 52380 | 729 | |
| 730 | fun plus_coeffs :: "'a::comm_monoid_add list \<Rightarrow> 'a list \<Rightarrow> 'a list" | |
| 731 | where | |
| 732 | "plus_coeffs xs [] = xs" | |
| 733 | | "plus_coeffs [] ys = ys" | |
| 734 | | "plus_coeffs (x # xs) (y # ys) = (x + y) ## plus_coeffs xs ys" | |
| 735 | ||
| 736 | lemma coeffs_plus_eq_plus_coeffs [code abstract]: | |
| 737 | "coeffs (p + q) = plus_coeffs (coeffs p) (coeffs q)" | |
| 738 | proof - | |
| 739 |   { fix xs ys :: "'a list" and n
 | |
| 740 | have "nth_default 0 (plus_coeffs xs ys) n = nth_default 0 xs n + nth_default 0 ys n" | |
| 741 | proof (induct xs ys arbitrary: n rule: plus_coeffs.induct) | |
| 60679 | 742 | case (3 x xs y ys n) | 
| 743 | then show ?case by (cases n) (auto simp add: cCons_def) | |
| 52380 | 744 | qed simp_all } | 
| 745 | note * = this | |
| 746 |   { fix xs ys :: "'a list"
 | |
| 747 | assume "xs \<noteq> [] \<Longrightarrow> last xs \<noteq> 0" and "ys \<noteq> [] \<Longrightarrow> last ys \<noteq> 0" | |
| 748 | moreover assume "plus_coeffs xs ys \<noteq> []" | |
| 749 | ultimately have "last (plus_coeffs xs ys) \<noteq> 0" | |
| 750 | proof (induct xs ys rule: plus_coeffs.induct) | |
| 751 | case (3 x xs y ys) then show ?case by (auto simp add: cCons_def) metis | |
| 752 | qed simp_all } | |
| 753 | note ** = this | |
| 754 | show ?thesis | |
| 755 | apply (rule coeffs_eqI) | |
| 756 | apply (simp add: * nth_default_coeffs_eq) | |
| 757 | apply (rule **) | |
| 758 | apply (auto dest: last_coeffs_not_0) | |
| 759 | done | |
| 760 | qed | |
| 761 | ||
| 762 | lemma coeffs_uminus [code abstract]: | |
| 763 | "coeffs (- p) = map (\<lambda>a. - a) (coeffs p)" | |
| 764 | by (rule coeffs_eqI) | |
| 765 | (simp_all add: not_0_coeffs_not_Nil last_map last_coeffs_not_0 nth_default_map_eq nth_default_coeffs_eq) | |
| 766 | ||
| 767 | lemma [code]: | |
| 768 | fixes p q :: "'a::ab_group_add poly" | |
| 769 | shows "p - q = p + - q" | |
| 59557 | 770 | by (fact diff_conv_add_uminus) | 
| 52380 | 771 | |
| 772 | lemma poly_add [simp]: "poly (p + q) x = poly p x + poly q x" | |
| 773 | apply (induct p arbitrary: q, simp) | |
| 774 | apply (case_tac q, simp, simp add: algebra_simps) | |
| 775 | done | |
| 776 | ||
| 777 | lemma poly_minus [simp]: | |
| 778 | fixes x :: "'a::comm_ring" | |
| 779 | shows "poly (- p) x = - poly p x" | |
| 780 | by (induct p) simp_all | |
| 781 | ||
| 782 | lemma poly_diff [simp]: | |
| 783 | fixes x :: "'a::comm_ring" | |
| 784 | shows "poly (p - q) x = poly p x - poly q x" | |
| 54230 
b1d955791529
more simplification rules on unary and binary minus
 haftmann parents: 
52380diff
changeset | 785 | using poly_add [of p "- q" x] by simp | 
| 52380 | 786 | |
| 64267 | 787 | lemma poly_sum: "poly (\<Sum>k\<in>A. p k) x = (\<Sum>k\<in>A. poly (p k) x)" | 
| 52380 | 788 | by (induct A rule: infinite_finite_induct) simp_all | 
| 29451 | 789 | |
| 64267 | 790 | lemma degree_sum_le: "finite S \<Longrightarrow> (\<And> p . p \<in> S \<Longrightarrow> degree (f p) \<le> n) | 
| 791 | \<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 | 792 | proof (induct S rule: finite_induct) | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 793 | case (insert p S) | 
| 64267 | 794 | hence "degree (sum f S) \<le> n" "degree (f p) \<le> n" by auto | 
| 795 | thus ?case unfolding sum.insert[OF insert(1-2)] by (metis degree_add_le) | |
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 796 | qed simp | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 797 | |
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 798 | lemma poly_as_sum_of_monoms': | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 799 | assumes n: "degree p \<le> n" | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 800 | 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 | 801 | proof - | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 802 |   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 | 803 | by auto | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 804 | show ?thesis | 
| 64267 | 805 | using n by (simp add: poly_eq_iff coeff_sum coeff_eq_0 sum.If_cases eq | 
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 806 | if_distrib[where f="\<lambda>x. x * a" for a]) | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 807 | qed | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 808 | |
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 809 | 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 | 810 | 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 | 811 | |
| 62065 | 812 | lemma Poly_snoc: "Poly (xs @ [x]) = Poly xs + monom x (length xs)" | 
| 813 | by (induction xs) (simp_all add: monom_0 monom_Suc) | |
| 814 | ||
| 29451 | 815 | |
| 60500 | 816 | subsection \<open>Multiplication by a constant, polynomial multiplication and the unit polynomial\<close> | 
| 29451 | 817 | |
| 52380 | 818 | lift_definition smult :: "'a::comm_semiring_0 \<Rightarrow> 'a poly \<Rightarrow> 'a poly" | 
| 819 | 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 | 820 | proof - | 
| 
1fa1023b13b9
move MOST and INFM in Infinite_Set to Filter; change them to abbreviations over the cofinite filter
 hoelzl parents: 
59983diff
changeset | 821 | fix a :: 'a and p :: "'a poly" show "\<forall>\<^sub>\<infinity> i. a * coeff p i = 0" | 
| 
1fa1023b13b9
move MOST and INFM in Infinite_Set to Filter; change them to abbreviations over the cofinite filter
 hoelzl parents: 
59983diff
changeset | 822 | using MOST_coeff_eq_0[of p] by eventually_elim simp | 
| 52380 | 823 | qed | 
| 29451 | 824 | |
| 52380 | 825 | lemma coeff_smult [simp]: | 
| 826 | "coeff (smult a p) n = a * coeff p n" | |
| 827 | by (simp add: smult.rep_eq) | |
| 29451 | 828 | |
| 829 | lemma degree_smult_le: "degree (smult a p) \<le> degree p" | |
| 830 | by (rule degree_le, simp add: coeff_eq_0) | |
| 831 | ||
| 29472 | 832 | lemma smult_smult [simp]: "smult a (smult b p) = smult (a * b) p" | 
| 57512 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 haftmann parents: 
57482diff
changeset | 833 | by (rule poly_eqI, simp add: mult.assoc) | 
| 29451 | 834 | |
| 835 | lemma smult_0_right [simp]: "smult a 0 = 0" | |
| 52380 | 836 | by (rule poly_eqI, simp) | 
| 29451 | 837 | |
| 838 | lemma smult_0_left [simp]: "smult 0 p = 0" | |
| 52380 | 839 | by (rule poly_eqI, simp) | 
| 29451 | 840 | |
| 841 | lemma smult_1_left [simp]: "smult (1::'a::comm_semiring_1) p = p" | |
| 52380 | 842 | by (rule poly_eqI, simp) | 
| 29451 | 843 | |
| 844 | lemma smult_add_right: | |
| 845 | "smult a (p + q) = smult a p + smult a q" | |
| 52380 | 846 | by (rule poly_eqI, simp add: algebra_simps) | 
| 29451 | 847 | |
| 848 | lemma smult_add_left: | |
| 849 | "smult (a + b) p = smult a p + smult b p" | |
| 52380 | 850 | by (rule poly_eqI, simp add: algebra_simps) | 
| 29451 | 851 | |
| 29457 | 852 | lemma smult_minus_right [simp]: | 
| 29451 | 853 | "smult (a::'a::comm_ring) (- p) = - smult a p" | 
| 52380 | 854 | by (rule poly_eqI, simp) | 
| 29451 | 855 | |
| 29457 | 856 | lemma smult_minus_left [simp]: | 
| 29451 | 857 | "smult (- a::'a::comm_ring) p = - smult a p" | 
| 52380 | 858 | by (rule poly_eqI, simp) | 
| 29451 | 859 | |
| 860 | lemma smult_diff_right: | |
| 861 | "smult (a::'a::comm_ring) (p - q) = smult a p - smult a q" | |
| 52380 | 862 | by (rule poly_eqI, simp add: algebra_simps) | 
| 29451 | 863 | |
| 864 | lemma smult_diff_left: | |
| 865 | "smult (a - b::'a::comm_ring) p = smult a p - smult b p" | |
| 52380 | 866 | by (rule poly_eqI, simp add: algebra_simps) | 
| 29451 | 867 | |
| 29472 | 868 | lemmas smult_distribs = | 
| 869 | smult_add_left smult_add_right | |
| 870 | smult_diff_left smult_diff_right | |
| 871 | ||
| 29451 | 872 | lemma smult_pCons [simp]: | 
| 873 | "smult a (pCons b p) = pCons (a * b) (smult a p)" | |
| 52380 | 874 | by (rule poly_eqI, simp add: coeff_pCons split: nat.split) | 
| 29451 | 875 | |
| 876 | lemma smult_monom: "smult a (monom b n) = monom (a * b) n" | |
| 877 | by (induct n, simp add: monom_0, simp add: monom_Suc) | |
| 878 | ||
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 879 | lemma smult_Poly: "smult c (Poly xs) = Poly (map (op * c) xs)" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 880 | by (auto simp add: poly_eq_iff coeff_Poly_eq nth_default_def) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 881 | |
| 29659 | 882 | lemma degree_smult_eq [simp]: | 
| 63498 | 883 |   fixes a :: "'a::{comm_semiring_0,semiring_no_zero_divisors}"
 | 
| 29659 | 884 | shows "degree (smult a p) = (if a = 0 then 0 else degree p)" | 
| 885 | by (cases "a = 0", simp, simp add: degree_def) | |
| 886 | ||
| 887 | lemma smult_eq_0_iff [simp]: | |
| 63498 | 888 |   fixes a :: "'a::{comm_semiring_0,semiring_no_zero_divisors}"
 | 
| 29659 | 889 | shows "smult a p = 0 \<longleftrightarrow> a = 0 \<or> p = 0" | 
| 52380 | 890 | by (simp add: poly_eq_iff) | 
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 891 | |
| 52380 | 892 | lemma coeffs_smult [code abstract]: | 
| 63498 | 893 |   fixes p :: "'a::{comm_semiring_0,semiring_no_zero_divisors} poly"
 | 
| 52380 | 894 | shows "coeffs (smult a p) = (if a = 0 then [] else map (Groups.times a) (coeffs p))" | 
| 895 | by (rule coeffs_eqI) | |
| 896 | (auto simp add: not_0_coeffs_not_Nil last_map last_coeffs_not_0 nth_default_map_eq nth_default_coeffs_eq) | |
| 63498 | 897 | |
| 29451 | 898 | instantiation poly :: (comm_semiring_0) comm_semiring_0 | 
| 899 | begin | |
| 900 | ||
| 901 | definition | |
| 52380 | 902 | "p * q = fold_coeffs (\<lambda>a p. smult a q + pCons 0 p) p 0" | 
| 29474 | 903 | |
| 904 | lemma mult_poly_0_left: "(0::'a poly) * q = 0" | |
| 52380 | 905 | by (simp add: times_poly_def) | 
| 29474 | 906 | |
| 907 | lemma mult_pCons_left [simp]: | |
| 908 | "pCons a p * q = smult a q + pCons 0 (p * q)" | |
| 52380 | 909 | by (cases "p = 0 \<and> a = 0") (auto simp add: times_poly_def) | 
| 29474 | 910 | |
| 911 | lemma mult_poly_0_right: "p * (0::'a poly) = 0" | |
| 52380 | 912 | by (induct p) (simp add: mult_poly_0_left, simp) | 
| 29451 | 913 | |
| 29474 | 914 | lemma mult_pCons_right [simp]: | 
| 915 | "p * pCons a q = smult a p + pCons 0 (p * q)" | |
| 52380 | 916 | by (induct p) (simp add: mult_poly_0_left, simp add: algebra_simps) | 
| 29474 | 917 | |
| 918 | lemmas mult_poly_0 = mult_poly_0_left mult_poly_0_right | |
| 919 | ||
| 52380 | 920 | lemma mult_smult_left [simp]: | 
| 921 | "smult a p * q = smult a (p * q)" | |
| 922 | by (induct p) (simp add: mult_poly_0, simp add: smult_add_right) | |
| 29474 | 923 | |
| 52380 | 924 | lemma mult_smult_right [simp]: | 
| 925 | "p * smult a q = smult a (p * q)" | |
| 926 | by (induct q) (simp add: mult_poly_0, simp add: smult_add_right) | |
| 29474 | 927 | |
| 928 | lemma mult_poly_add_left: | |
| 929 | fixes p q r :: "'a poly" | |
| 930 | shows "(p + q) * r = p * r + q * r" | |
| 52380 | 931 | by (induct r) (simp add: mult_poly_0, simp add: smult_distribs algebra_simps) | 
| 29451 | 932 | |
| 60679 | 933 | instance | 
| 934 | proof | |
| 29451 | 935 | fix p q r :: "'a poly" | 
| 936 | show 0: "0 * p = 0" | |
| 29474 | 937 | by (rule mult_poly_0_left) | 
| 29451 | 938 | show "p * 0 = 0" | 
| 29474 | 939 | by (rule mult_poly_0_right) | 
| 29451 | 940 | show "(p + q) * r = p * r + q * r" | 
| 29474 | 941 | by (rule mult_poly_add_left) | 
| 29451 | 942 | show "(p * q) * r = p * (q * r)" | 
| 29474 | 943 | by (induct p, simp add: mult_poly_0, simp add: mult_poly_add_left) | 
| 29451 | 944 | show "p * q = q * p" | 
| 29474 | 945 | by (induct p, simp add: mult_poly_0, simp) | 
| 29451 | 946 | qed | 
| 947 | ||
| 948 | end | |
| 949 | ||
| 63498 | 950 | lemma coeff_mult_degree_sum: | 
| 951 | "coeff (p * q) (degree p + degree q) = | |
| 952 | coeff p (degree p) * coeff q (degree q)" | |
| 953 | by (induct p, simp, simp add: coeff_eq_0) | |
| 954 | ||
| 955 | instance poly :: ("{comm_semiring_0,semiring_no_zero_divisors}") semiring_no_zero_divisors
 | |
| 956 | proof | |
| 957 | fix p q :: "'a poly" | |
| 958 | assume "p \<noteq> 0" and "q \<noteq> 0" | |
| 959 | have "coeff (p * q) (degree p + degree q) = | |
| 960 | coeff p (degree p) * coeff q (degree q)" | |
| 961 | by (rule coeff_mult_degree_sum) | |
| 962 | also have "coeff p (degree p) * coeff q (degree q) \<noteq> 0" | |
| 963 | using \<open>p \<noteq> 0\<close> and \<open>q \<noteq> 0\<close> by simp | |
| 964 | finally have "\<exists>n. coeff (p * q) n \<noteq> 0" .. | |
| 965 | thus "p * q \<noteq> 0" by (simp add: poly_eq_iff) | |
| 966 | qed | |
| 967 | ||
| 29540 | 968 | instance poly :: (comm_semiring_0_cancel) comm_semiring_0_cancel .. | 
| 969 | ||
| 29474 | 970 | lemma coeff_mult: | 
| 971 | "coeff (p * q) n = (\<Sum>i\<le>n. coeff p i * coeff q (n-i))" | |
| 972 | proof (induct p arbitrary: n) | |
| 973 | case 0 show ?case by simp | |
| 974 | next | |
| 975 | case (pCons a p n) thus ?case | |
| 64267 | 976 | by (cases n, simp, simp add: sum_atMost_Suc_shift | 
| 977 | del: sum_atMost_Suc) | |
| 29474 | 978 | qed | 
| 29451 | 979 | |
| 29474 | 980 | lemma degree_mult_le: "degree (p * q) \<le> degree p + degree q" | 
| 981 | apply (rule degree_le) | |
| 982 | apply (induct p) | |
| 983 | apply simp | |
| 984 | apply (simp add: coeff_eq_0 coeff_pCons split: nat.split) | |
| 29451 | 985 | done | 
| 986 | ||
| 987 | lemma mult_monom: "monom a m * monom b n = monom (a * b) (m + n)" | |
| 60679 | 988 | by (induct m) (simp add: monom_0 smult_monom, simp add: monom_Suc) | 
| 29451 | 989 | |
| 990 | instantiation poly :: (comm_semiring_1) comm_semiring_1 | |
| 991 | begin | |
| 992 | ||
| 60679 | 993 | definition one_poly_def: "1 = pCons 1 0" | 
| 29451 | 994 | |
| 60679 | 995 | instance | 
| 996 | proof | |
| 997 | show "1 * p = p" for p :: "'a poly" | |
| 52380 | 998 | unfolding one_poly_def by simp | 
| 29451 | 999 | show "0 \<noteq> (1::'a poly)" | 
| 1000 | unfolding one_poly_def by simp | |
| 1001 | qed | |
| 1002 | ||
| 1003 | end | |
| 1004 | ||
| 63498 | 1005 | instance poly :: ("{comm_semiring_1,semiring_1_no_zero_divisors}") semiring_1_no_zero_divisors ..
 | 
| 1006 | ||
| 52380 | 1007 | instance poly :: (comm_ring) comm_ring .. | 
| 1008 | ||
| 1009 | instance poly :: (comm_ring_1) comm_ring_1 .. | |
| 1010 | ||
| 63498 | 1011 | instance poly :: (comm_ring_1) comm_semiring_1_cancel .. | 
| 1012 | ||
| 29451 | 1013 | lemma coeff_1 [simp]: "coeff 1 n = (if n = 0 then 1 else 0)" | 
| 1014 | unfolding one_poly_def | |
| 1015 | by (simp add: coeff_pCons split: nat.split) | |
| 1016 | ||
| 60570 | 1017 | lemma monom_eq_1 [simp]: | 
| 1018 | "monom 1 0 = 1" | |
| 1019 | by (simp add: monom_0 one_poly_def) | |
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 1020 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 1021 | lemma monom_eq_1_iff: "monom c n = 1 \<longleftrightarrow> c = 1 \<and> n = 0" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 1022 | using monom_eq_const_iff[of c n 1] by (auto simp: one_poly_def) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 1023 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 1024 | lemma monom_altdef: | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 1025 | "monom c n = smult c ([:0, 1:]^n)" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 1026 | by (induction n) (simp_all add: monom_0 monom_Suc one_poly_def) | 
| 60570 | 1027 | |
| 29451 | 1028 | lemma degree_1 [simp]: "degree 1 = 0" | 
| 1029 | unfolding one_poly_def | |
| 1030 | by (rule degree_pCons_0) | |
| 1031 | ||
| 52380 | 1032 | lemma coeffs_1_eq [simp, code abstract]: | 
| 1033 | "coeffs 1 = [1]" | |
| 1034 | by (simp add: one_poly_def) | |
| 1035 | ||
| 1036 | lemma degree_power_le: | |
| 1037 | "degree (p ^ n) \<le> degree p * n" | |
| 1038 | by (induct n) (auto intro: order_trans degree_mult_le) | |
| 1039 | ||
| 1040 | lemma poly_smult [simp]: | |
| 1041 | "poly (smult a p) x = a * poly p x" | |
| 1042 | by (induct p, simp, simp add: algebra_simps) | |
| 1043 | ||
| 1044 | lemma poly_mult [simp]: | |
| 1045 | "poly (p * q) x = poly p x * poly q x" | |
| 1046 | by (induct p, simp_all, simp add: algebra_simps) | |
| 1047 | ||
| 1048 | lemma poly_1 [simp]: | |
| 1049 | "poly 1 x = 1" | |
| 1050 | by (simp add: one_poly_def) | |
| 1051 | ||
| 1052 | lemma poly_power [simp]: | |
| 1053 |   fixes p :: "'a::{comm_semiring_1} poly"
 | |
| 1054 | shows "poly (p ^ n) x = poly p x ^ n" | |
| 1055 | by (induct n) simp_all | |
| 1056 | ||
| 64272 | 1057 | 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 | 1058 | 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 | 1059 | |
| 64272 | 1060 | lemma degree_prod_sum_le: "finite S \<Longrightarrow> degree (prod f S) \<le> sum (degree o f) S" | 
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1061 | proof (induct S rule: finite_induct) | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1062 | case (insert a S) | 
| 64272 | 1063 | show ?case unfolding prod.insert[OF insert(1-2)] sum.insert[OF insert(1-2)] | 
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1064 | by (rule le_trans[OF degree_mult_le], insert insert, auto) | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1065 | qed simp | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1066 | |
| 62065 | 1067 | subsection \<open>Conversions from natural numbers\<close> | 
| 1068 | ||
| 1069 | lemma of_nat_poly: "of_nat n = [:of_nat n :: 'a :: comm_semiring_1:]" | |
| 1070 | proof (induction n) | |
| 1071 | case (Suc n) | |
| 1072 | hence "of_nat (Suc n) = 1 + (of_nat n :: 'a poly)" | |
| 1073 | by simp | |
| 1074 | also have "(of_nat n :: 'a poly) = [: of_nat n :]" | |
| 1075 | by (subst Suc) (rule refl) | |
| 1076 | also have "1 = [:1:]" by (simp add: one_poly_def) | |
| 1077 | finally show ?case by (subst (asm) add_pCons) simp | |
| 1078 | qed simp | |
| 1079 | ||
| 1080 | lemma degree_of_nat [simp]: "degree (of_nat n) = 0" | |
| 1081 | by (simp add: of_nat_poly) | |
| 1082 | ||
| 1083 | lemma degree_numeral [simp]: "degree (numeral n) = 0" | |
| 1084 | by (subst of_nat_numeral [symmetric], subst of_nat_poly) simp | |
| 1085 | ||
| 1086 | lemma numeral_poly: "numeral n = [:numeral n:]" | |
| 1087 | by (subst of_nat_numeral [symmetric], subst of_nat_poly) simp | |
| 52380 | 1088 | |
| 60500 | 1089 | subsection \<open>Lemmas about divisibility\<close> | 
| 29979 | 1090 | |
| 1091 | lemma dvd_smult: "p dvd q \<Longrightarrow> p dvd smult a q" | |
| 1092 | proof - | |
| 1093 | assume "p dvd q" | |
| 1094 | then obtain k where "q = p * k" .. | |
| 1095 | then have "smult a q = p * smult a k" by simp | |
| 1096 | then show "p dvd smult a q" .. | |
| 1097 | qed | |
| 1098 | ||
| 1099 | lemma dvd_smult_cancel: | |
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1100 | fixes a :: "'a :: field" | 
| 29979 | 1101 | shows "p dvd smult a q \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> p dvd q" | 
| 1102 | by (drule dvd_smult [where a="inverse a"]) simp | |
| 1103 | ||
| 1104 | lemma dvd_smult_iff: | |
| 1105 | fixes a :: "'a::field" | |
| 1106 | shows "a \<noteq> 0 \<Longrightarrow> p dvd smult a q \<longleftrightarrow> p dvd q" | |
| 1107 | by (safe elim!: dvd_smult dvd_smult_cancel) | |
| 1108 | ||
| 31663 | 1109 | lemma smult_dvd_cancel: | 
| 1110 | "smult a p dvd q \<Longrightarrow> p dvd q" | |
| 1111 | proof - | |
| 1112 | assume "smult a p dvd q" | |
| 1113 | then obtain k where "q = smult a p * k" .. | |
| 1114 | then have "q = p * smult a k" by simp | |
| 1115 | then show "p dvd q" .. | |
| 1116 | qed | |
| 1117 | ||
| 1118 | lemma smult_dvd: | |
| 1119 | fixes a :: "'a::field" | |
| 1120 | shows "p dvd q \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> smult a p dvd q" | |
| 1121 | by (rule smult_dvd_cancel [where a="inverse a"]) simp | |
| 1122 | ||
| 1123 | lemma smult_dvd_iff: | |
| 1124 | fixes a :: "'a::field" | |
| 1125 | shows "smult a p dvd q \<longleftrightarrow> (if a = 0 then q = 0 else p dvd q)" | |
| 1126 | by (auto elim: smult_dvd smult_dvd_cancel) | |
| 1127 | ||
| 29451 | 1128 | |
| 60500 | 1129 | subsection \<open>Polynomials form an integral domain\<close> | 
| 29451 | 1130 | |
| 63498 | 1131 | instance poly :: (idom) idom .. | 
| 29451 | 1132 | |
| 1133 | lemma degree_mult_eq: | |
| 63498 | 1134 |   fixes p q :: "'a::{comm_semiring_0,semiring_no_zero_divisors} poly"
 | 
| 29451 | 1135 | shows "\<lbrakk>p \<noteq> 0; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree (p * q) = degree p + degree q" | 
| 1136 | apply (rule order_antisym [OF degree_mult_le le_degree]) | |
| 1137 | apply (simp add: coeff_mult_degree_sum) | |
| 1138 | done | |
| 1139 | ||
| 60570 | 1140 | lemma degree_mult_right_le: | 
| 63498 | 1141 |   fixes p q :: "'a::{comm_semiring_0,semiring_no_zero_divisors} poly"
 | 
| 60570 | 1142 | assumes "q \<noteq> 0" | 
| 1143 | shows "degree p \<le> degree (p * q)" | |
| 1144 | using assms by (cases "p = 0") (simp_all add: degree_mult_eq) | |
| 1145 | ||
| 1146 | lemma coeff_degree_mult: | |
| 63498 | 1147 |   fixes p q :: "'a::{comm_semiring_0,semiring_no_zero_divisors} poly"
 | 
| 60570 | 1148 | shows "coeff (p * q) (degree (p * q)) = | 
| 1149 | coeff q (degree q) * coeff p (degree p)" | |
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1150 | by (cases "p = 0 \<or> q = 0") (auto simp add: degree_mult_eq coeff_mult_degree_sum mult_ac) | 
| 60570 | 1151 | |
| 29451 | 1152 | lemma dvd_imp_degree_le: | 
| 63498 | 1153 |   fixes p q :: "'a::{comm_semiring_1,semiring_no_zero_divisors} poly"
 | 
| 29451 | 1154 | shows "\<lbrakk>p dvd q; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree p \<le> degree q" | 
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1155 | by (erule dvdE, hypsubst, subst degree_mult_eq) auto | 
| 29451 | 1156 | |
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1157 | lemma divides_degree: | 
| 63498 | 1158 |   assumes pq: "p dvd (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 | 1159 | shows "degree p \<le> degree q \<or> q = 0" | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1160 | by (metis dvd_imp_degree_le pq) | 
| 63498 | 1161 | |
| 1162 | lemma const_poly_dvd_iff: | |
| 1163 |   fixes c :: "'a :: {comm_semiring_1,semiring_no_zero_divisors}"
 | |
| 1164 | shows "[:c:] dvd p \<longleftrightarrow> (\<forall>n. c dvd coeff p n)" | |
| 1165 | proof (cases "c = 0 \<or> p = 0") | |
| 1166 | case False | |
| 1167 | show ?thesis | |
| 1168 | proof | |
| 1169 | assume "[:c:] dvd p" | |
| 1170 | thus "\<forall>n. c dvd coeff p n" by (auto elim!: dvdE simp: coeffs_def) | |
| 1171 | next | |
| 1172 | assume *: "\<forall>n. c dvd coeff p n" | |
| 1173 | define mydiv where "mydiv = (\<lambda>x y :: 'a. SOME z. x = y * z)" | |
| 1174 | have mydiv: "x = y * mydiv x y" if "y dvd x" for x y | |
| 1175 | using that unfolding mydiv_def dvd_def by (rule someI_ex) | |
| 1176 | define q where "q = Poly (map (\<lambda>a. mydiv a c) (coeffs p))" | |
| 1177 | from False * have "p = q * [:c:]" | |
| 1178 | by (intro poly_eqI) (auto simp: q_def nth_default_def not_less length_coeffs_degree | |
| 1179 | coeffs_nth intro!: coeff_eq_0 mydiv) | |
| 1180 | thus "[:c:] dvd p" by (simp only: dvd_triv_right) | |
| 1181 | qed | |
| 1182 | qed (auto intro!: poly_eqI) | |
| 1183 | ||
| 1184 | lemma const_poly_dvd_const_poly_iff [simp]: | |
| 1185 |   "[:a::'a::{comm_semiring_1,semiring_no_zero_divisors}:] dvd [:b:] \<longleftrightarrow> a dvd b"
 | |
| 1186 | by (subst const_poly_dvd_iff) (auto simp: coeff_pCons split: nat.splits) | |
| 1187 | ||
| 29451 | 1188 | |
| 60500 | 1189 | subsection \<open>Polynomials form an ordered integral domain\<close> | 
| 29878 | 1190 | |
| 63498 | 1191 | definition pos_poly :: "'a::linordered_semidom poly \<Rightarrow> bool" | 
| 29878 | 1192 | where | 
| 1193 | "pos_poly p \<longleftrightarrow> 0 < coeff p (degree p)" | |
| 1194 | ||
| 1195 | lemma pos_poly_pCons: | |
| 1196 | "pos_poly (pCons a p) \<longleftrightarrow> pos_poly p \<or> (p = 0 \<and> 0 < a)" | |
| 1197 | unfolding pos_poly_def by simp | |
| 1198 | ||
| 1199 | lemma not_pos_poly_0 [simp]: "\<not> pos_poly 0" | |
| 1200 | unfolding pos_poly_def by simp | |
| 1201 | ||
| 1202 | lemma pos_poly_add: "\<lbrakk>pos_poly p; pos_poly q\<rbrakk> \<Longrightarrow> pos_poly (p + q)" | |
| 1203 | apply (induct p arbitrary: q, simp) | |
| 1204 | apply (case_tac q, force simp add: pos_poly_pCons add_pos_pos) | |
| 1205 | done | |
| 1206 | ||
| 1207 | lemma pos_poly_mult: "\<lbrakk>pos_poly p; pos_poly q\<rbrakk> \<Longrightarrow> pos_poly (p * q)" | |
| 1208 | unfolding pos_poly_def | |
| 1209 | apply (subgoal_tac "p \<noteq> 0 \<and> q \<noteq> 0") | |
| 56544 | 1210 | apply (simp add: degree_mult_eq coeff_mult_degree_sum) | 
| 29878 | 1211 | apply auto | 
| 1212 | done | |
| 1213 | ||
| 63498 | 1214 | lemma pos_poly_total: "(p :: 'a :: linordered_idom poly) = 0 \<or> pos_poly p \<or> pos_poly (- p)" | 
| 29878 | 1215 | by (induct p) (auto simp add: pos_poly_pCons) | 
| 1216 | ||
| 52380 | 1217 | lemma last_coeffs_eq_coeff_degree: | 
| 1218 | "p \<noteq> 0 \<Longrightarrow> last (coeffs p) = coeff p (degree p)" | |
| 1219 | by (simp add: coeffs_def) | |
| 1220 | ||
| 1221 | lemma pos_poly_coeffs [code]: | |
| 1222 | "pos_poly p \<longleftrightarrow> (let as = coeffs p in as \<noteq> [] \<and> last as > 0)" (is "?P \<longleftrightarrow> ?Q") | |
| 1223 | proof | |
| 1224 | assume ?Q then show ?P by (auto simp add: pos_poly_def last_coeffs_eq_coeff_degree) | |
| 1225 | next | |
| 1226 | assume ?P then have *: "0 < coeff p (degree p)" by (simp add: pos_poly_def) | |
| 1227 | then have "p \<noteq> 0" by auto | |
| 1228 | with * show ?Q by (simp add: last_coeffs_eq_coeff_degree) | |
| 1229 | qed | |
| 1230 | ||
| 35028 
108662d50512
more consistent naming of type classes involving orderings (and lattices) -- c.f. NEWS
 haftmann parents: 
34973diff
changeset | 1231 | instantiation poly :: (linordered_idom) linordered_idom | 
| 29878 | 1232 | begin | 
| 1233 | ||
| 1234 | definition | |
| 37765 | 1235 | "x < y \<longleftrightarrow> pos_poly (y - x)" | 
| 29878 | 1236 | |
| 1237 | definition | |
| 37765 | 1238 | "x \<le> y \<longleftrightarrow> x = y \<or> pos_poly (y - x)" | 
| 29878 | 1239 | |
| 1240 | definition | |
| 61945 | 1241 | "\<bar>x::'a poly\<bar> = (if x < 0 then - x else x)" | 
| 29878 | 1242 | |
| 1243 | definition | |
| 37765 | 1244 | "sgn (x::'a poly) = (if x = 0 then 0 else if 0 < x then 1 else - 1)" | 
| 29878 | 1245 | |
| 60679 | 1246 | instance | 
| 1247 | proof | |
| 1248 | fix x y z :: "'a poly" | |
| 29878 | 1249 | show "x < y \<longleftrightarrow> x \<le> y \<and> \<not> y \<le> x" | 
| 1250 | unfolding less_eq_poly_def less_poly_def | |
| 1251 | apply safe | |
| 1252 | apply simp | |
| 1253 | apply (drule (1) pos_poly_add) | |
| 1254 | apply simp | |
| 1255 | done | |
| 60679 | 1256 | show "x \<le> x" | 
| 29878 | 1257 | unfolding less_eq_poly_def by simp | 
| 60679 | 1258 | show "x \<le> y \<Longrightarrow> y \<le> z \<Longrightarrow> x \<le> z" | 
| 29878 | 1259 | unfolding less_eq_poly_def | 
| 1260 | apply safe | |
| 1261 | apply (drule (1) pos_poly_add) | |
| 1262 | apply (simp add: algebra_simps) | |
| 1263 | done | |
| 60679 | 1264 | show "x \<le> y \<Longrightarrow> y \<le> x \<Longrightarrow> x = y" | 
| 29878 | 1265 | unfolding less_eq_poly_def | 
| 1266 | apply safe | |
| 1267 | apply (drule (1) pos_poly_add) | |
| 1268 | apply simp | |
| 1269 | done | |
| 60679 | 1270 | show "x \<le> y \<Longrightarrow> z + x \<le> z + y" | 
| 29878 | 1271 | unfolding less_eq_poly_def | 
| 1272 | apply safe | |
| 1273 | apply (simp add: algebra_simps) | |
| 1274 | done | |
| 1275 | show "x \<le> y \<or> y \<le> x" | |
| 1276 | unfolding less_eq_poly_def | |
| 1277 | using pos_poly_total [of "x - y"] | |
| 1278 | by auto | |
| 60679 | 1279 | show "x < y \<Longrightarrow> 0 < z \<Longrightarrow> z * x < z * y" | 
| 29878 | 1280 | unfolding less_poly_def | 
| 1281 | by (simp add: right_diff_distrib [symmetric] pos_poly_mult) | |
| 1282 | show "\<bar>x\<bar> = (if x < 0 then - x else x)" | |
| 1283 | by (rule abs_poly_def) | |
| 1284 | show "sgn x = (if x = 0 then 0 else if 0 < x then 1 else - 1)" | |
| 1285 | by (rule sgn_poly_def) | |
| 1286 | qed | |
| 1287 | ||
| 1288 | end | |
| 1289 | ||
| 60500 | 1290 | text \<open>TODO: Simplification rules for comparisons\<close> | 
| 29878 | 1291 | |
| 1292 | ||
| 60500 | 1293 | subsection \<open>Synthetic division and polynomial roots\<close> | 
| 52380 | 1294 | |
| 60500 | 1295 | text \<open> | 
| 52380 | 1296 |   Synthetic division is simply division by the linear polynomial @{term "x - c"}.
 | 
| 60500 | 1297 | \<close> | 
| 52380 | 1298 | |
| 1299 | definition synthetic_divmod :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly \<times> 'a" | |
| 1300 | where | |
| 1301 | "synthetic_divmod p c = fold_coeffs (\<lambda>a (q, r). (pCons r q, a + c * r)) p (0, 0)" | |
| 1302 | ||
| 1303 | definition synthetic_div :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly" | |
| 1304 | where | |
| 1305 | "synthetic_div p c = fst (synthetic_divmod p c)" | |
| 1306 | ||
| 1307 | lemma synthetic_divmod_0 [simp]: | |
| 1308 | "synthetic_divmod 0 c = (0, 0)" | |
| 1309 | by (simp add: synthetic_divmod_def) | |
| 1310 | ||
| 1311 | lemma synthetic_divmod_pCons [simp]: | |
| 1312 | "synthetic_divmod (pCons a p) c = (\<lambda>(q, r). (pCons r q, a + c * r)) (synthetic_divmod p c)" | |
| 1313 | by (cases "p = 0 \<and> a = 0") (auto simp add: synthetic_divmod_def) | |
| 1314 | ||
| 1315 | lemma synthetic_div_0 [simp]: | |
| 1316 | "synthetic_div 0 c = 0" | |
| 1317 | unfolding synthetic_div_def by simp | |
| 1318 | ||
| 1319 | lemma synthetic_div_unique_lemma: "smult c p = pCons a p \<Longrightarrow> p = 0" | |
| 1320 | by (induct p arbitrary: a) simp_all | |
| 1321 | ||
| 1322 | lemma snd_synthetic_divmod: | |
| 1323 | "snd (synthetic_divmod p c) = poly p c" | |
| 1324 | by (induct p, simp, simp add: split_def) | |
| 1325 | ||
| 1326 | lemma synthetic_div_pCons [simp]: | |
| 1327 | "synthetic_div (pCons a p) c = pCons (poly p c) (synthetic_div p c)" | |
| 1328 | unfolding synthetic_div_def | |
| 1329 | by (simp add: split_def snd_synthetic_divmod) | |
| 1330 | ||
| 1331 | lemma synthetic_div_eq_0_iff: | |
| 1332 | "synthetic_div p c = 0 \<longleftrightarrow> degree p = 0" | |
| 63649 | 1333 | proof (induct p) | 
| 1334 | case 0 | |
| 1335 | then show ?case by simp | |
| 1336 | next | |
| 1337 | case (pCons a p) | |
| 1338 | then show ?case by (cases p) simp | |
| 1339 | qed | |
| 52380 | 1340 | |
| 1341 | lemma degree_synthetic_div: | |
| 1342 | "degree (synthetic_div p c) = degree p - 1" | |
| 63649 | 1343 | by (induct p) (simp_all add: synthetic_div_eq_0_iff) | 
| 52380 | 1344 | |
| 1345 | lemma synthetic_div_correct: | |
| 1346 | "p + smult c (synthetic_div p c) = pCons (poly p c) (synthetic_div p c)" | |
| 1347 | by (induct p) simp_all | |
| 1348 | ||
| 1349 | lemma synthetic_div_unique: | |
| 1350 | "p + smult c q = pCons r q \<Longrightarrow> r = poly p c \<and> q = synthetic_div p c" | |
| 1351 | apply (induct p arbitrary: q r) | |
| 1352 | apply (simp, frule synthetic_div_unique_lemma, simp) | |
| 1353 | apply (case_tac q, force) | |
| 1354 | done | |
| 1355 | ||
| 1356 | lemma synthetic_div_correct': | |
| 1357 | fixes c :: "'a::comm_ring_1" | |
| 1358 | shows "[:-c, 1:] * synthetic_div p c + [:poly p c:] = p" | |
| 1359 | using synthetic_div_correct [of p c] | |
| 1360 | by (simp add: algebra_simps) | |
| 1361 | ||
| 1362 | lemma poly_eq_0_iff_dvd: | |
| 63498 | 1363 |   fixes c :: "'a::{comm_ring_1}"
 | 
| 52380 | 1364 | shows "poly p c = 0 \<longleftrightarrow> [:-c, 1:] dvd p" | 
| 1365 | proof | |
| 1366 | assume "poly p c = 0" | |
| 1367 | with synthetic_div_correct' [of c p] | |
| 1368 | have "p = [:-c, 1:] * synthetic_div p c" by simp | |
| 1369 | then show "[:-c, 1:] dvd p" .. | |
| 1370 | next | |
| 1371 | assume "[:-c, 1:] dvd p" | |
| 1372 | then obtain k where "p = [:-c, 1:] * k" by (rule dvdE) | |
| 1373 | then show "poly p c = 0" by simp | |
| 1374 | qed | |
| 1375 | ||
| 1376 | lemma dvd_iff_poly_eq_0: | |
| 63498 | 1377 |   fixes c :: "'a::{comm_ring_1}"
 | 
| 52380 | 1378 | shows "[:c, 1:] dvd p \<longleftrightarrow> poly p (-c) = 0" | 
| 1379 | by (simp add: poly_eq_0_iff_dvd) | |
| 1380 | ||
| 1381 | lemma poly_roots_finite: | |
| 63498 | 1382 |   fixes p :: "'a::{comm_ring_1,ring_no_zero_divisors} poly"
 | 
| 52380 | 1383 |   shows "p \<noteq> 0 \<Longrightarrow> finite {x. poly p x = 0}"
 | 
| 1384 | proof (induct n \<equiv> "degree p" arbitrary: p) | |
| 1385 | case (0 p) | |
| 1386 | then obtain a where "a \<noteq> 0" and "p = [:a:]" | |
| 1387 | by (cases p, simp split: if_splits) | |
| 1388 |   then show "finite {x. poly p x = 0}" by simp
 | |
| 1389 | next | |
| 1390 | case (Suc n p) | |
| 1391 |   show "finite {x. poly p x = 0}"
 | |
| 1392 | proof (cases "\<exists>x. poly p x = 0") | |
| 1393 | case False | |
| 1394 |     then show "finite {x. poly p x = 0}" by simp
 | |
| 1395 | next | |
| 1396 | case True | |
| 1397 | then obtain a where "poly p a = 0" .. | |
| 1398 | then have "[:-a, 1:] dvd p" by (simp only: poly_eq_0_iff_dvd) | |
| 1399 | then obtain k where k: "p = [:-a, 1:] * k" .. | |
| 60500 | 1400 | with \<open>p \<noteq> 0\<close> have "k \<noteq> 0" by auto | 
| 52380 | 1401 | with k have "degree p = Suc (degree k)" | 
| 1402 | by (simp add: degree_mult_eq del: mult_pCons_left) | |
| 60500 | 1403 | with \<open>Suc n = degree p\<close> have "n = degree k" by simp | 
| 1404 |     then have "finite {x. poly k x = 0}" using \<open>k \<noteq> 0\<close> by (rule Suc.hyps)
 | |
| 52380 | 1405 |     then have "finite (insert a {x. poly k x = 0})" by simp
 | 
| 1406 |     then show "finite {x. poly p x = 0}"
 | |
| 57862 | 1407 | by (simp add: k Collect_disj_eq del: mult_pCons_left) | 
| 52380 | 1408 | qed | 
| 1409 | qed | |
| 1410 | ||
| 1411 | lemma poly_eq_poly_eq_iff: | |
| 63498 | 1412 |   fixes p q :: "'a::{comm_ring_1,ring_no_zero_divisors,ring_char_0} poly"
 | 
| 52380 | 1413 | shows "poly p = poly q \<longleftrightarrow> p = q" (is "?P \<longleftrightarrow> ?Q") | 
| 1414 | proof | |
| 1415 | assume ?Q then show ?P by simp | |
| 1416 | next | |
| 63498 | 1417 |   { fix p :: "'a poly"
 | 
| 52380 | 1418 | have "poly p = poly 0 \<longleftrightarrow> p = 0" | 
| 1419 | apply (cases "p = 0", simp_all) | |
| 1420 | apply (drule poly_roots_finite) | |
| 1421 | apply (auto simp add: infinite_UNIV_char_0) | |
| 1422 | done | |
| 1423 | } note this [of "p - q"] | |
| 1424 | moreover assume ?P | |
| 1425 | ultimately show ?Q by auto | |
| 1426 | qed | |
| 1427 | ||
| 1428 | lemma poly_all_0_iff_0: | |
| 63498 | 1429 |   fixes p :: "'a::{ring_char_0, comm_ring_1,ring_no_zero_divisors} poly"
 | 
| 52380 | 1430 | shows "(\<forall>x. poly p x = 0) \<longleftrightarrow> p = 0" | 
| 1431 | by (auto simp add: poly_eq_poly_eq_iff [symmetric]) | |
| 1432 | ||
| 1433 | ||
| 60500 | 1434 | subsection \<open>Long division of polynomials\<close> | 
| 29451 | 1435 | |
| 52380 | 1436 | definition pdivmod_rel :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> bool" | 
| 29451 | 1437 | where | 
| 29537 | 1438 | "pdivmod_rel x y q r \<longleftrightarrow> | 
| 29451 | 1439 | x = q * y + r \<and> (if y = 0 then q = 0 else r = 0 \<or> degree r < degree y)" | 
| 1440 | ||
| 29537 | 1441 | lemma pdivmod_rel_0: | 
| 1442 | "pdivmod_rel 0 y 0 0" | |
| 1443 | unfolding pdivmod_rel_def by simp | |
| 29451 | 1444 | |
| 29537 | 1445 | lemma pdivmod_rel_by_0: | 
| 1446 | "pdivmod_rel x 0 0 x" | |
| 1447 | unfolding pdivmod_rel_def by simp | |
| 29451 | 1448 | |
| 1449 | lemma eq_zero_or_degree_less: | |
| 1450 | assumes "degree p \<le> n" and "coeff p n = 0" | |
| 1451 | shows "p = 0 \<or> degree p < n" | |
| 1452 | proof (cases n) | |
| 1453 | case 0 | |
| 60500 | 1454 | with \<open>degree p \<le> n\<close> and \<open>coeff p n = 0\<close> | 
| 29451 | 1455 | have "coeff p (degree p) = 0" by simp | 
| 1456 | then have "p = 0" by simp | |
| 1457 | then show ?thesis .. | |
| 1458 | next | |
| 1459 | case (Suc m) | |
| 1460 | have "\<forall>i>n. coeff p i = 0" | |
| 60500 | 1461 | using \<open>degree p \<le> n\<close> by (simp add: coeff_eq_0) | 
| 29451 | 1462 | then have "\<forall>i\<ge>n. coeff p i = 0" | 
| 60500 | 1463 | using \<open>coeff p n = 0\<close> by (simp add: le_less) | 
| 29451 | 1464 | then have "\<forall>i>m. coeff p i = 0" | 
| 60500 | 1465 | using \<open>n = Suc m\<close> by (simp add: less_eq_Suc_le) | 
| 29451 | 1466 | then have "degree p \<le> m" | 
| 1467 | by (rule degree_le) | |
| 1468 | then have "degree p < n" | |
| 60500 | 1469 | using \<open>n = Suc m\<close> by (simp add: less_Suc_eq_le) | 
| 29451 | 1470 | then show ?thesis .. | 
| 1471 | qed | |
| 1472 | ||
| 29537 | 1473 | lemma pdivmod_rel_pCons: | 
| 1474 | assumes rel: "pdivmod_rel x y q r" | |
| 29451 | 1475 | assumes y: "y \<noteq> 0" | 
| 1476 | assumes b: "b = coeff (pCons a r) (degree y) / coeff y (degree y)" | |
| 29537 | 1477 | shows "pdivmod_rel (pCons a x) y (pCons b q) (pCons a r - smult b y)" | 
| 1478 | (is "pdivmod_rel ?x y ?q ?r") | |
| 29451 | 1479 | proof - | 
| 1480 | have x: "x = q * y + r" and r: "r = 0 \<or> degree r < degree y" | |
| 29537 | 1481 | using assms unfolding pdivmod_rel_def by simp_all | 
| 29451 | 1482 | |
| 1483 | have 1: "?x = ?q * y + ?r" | |
| 1484 | using b x by simp | |
| 1485 | ||
| 1486 | have 2: "?r = 0 \<or> degree ?r < degree y" | |
| 1487 | proof (rule eq_zero_or_degree_less) | |
| 29539 | 1488 | show "degree ?r \<le> degree y" | 
| 1489 | proof (rule degree_diff_le) | |
| 29451 | 1490 | show "degree (pCons a r) \<le> degree y" | 
| 29460 
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
 huffman parents: 
29457diff
changeset | 1491 | using r by auto | 
| 29451 | 1492 | show "degree (smult b y) \<le> degree y" | 
| 1493 | by (rule degree_smult_le) | |
| 1494 | qed | |
| 1495 | next | |
| 1496 | show "coeff ?r (degree y) = 0" | |
| 60500 | 1497 | using \<open>y \<noteq> 0\<close> unfolding b by simp | 
| 29451 | 1498 | qed | 
| 1499 | ||
| 1500 | from 1 2 show ?thesis | |
| 29537 | 1501 | unfolding pdivmod_rel_def | 
| 60500 | 1502 | using \<open>y \<noteq> 0\<close> by simp | 
| 29451 | 1503 | qed | 
| 1504 | ||
| 29537 | 1505 | lemma pdivmod_rel_exists: "\<exists>q r. pdivmod_rel x y q r" | 
| 29451 | 1506 | apply (cases "y = 0") | 
| 29537 | 1507 | apply (fast intro!: pdivmod_rel_by_0) | 
| 29451 | 1508 | apply (induct x) | 
| 29537 | 1509 | apply (fast intro!: pdivmod_rel_0) | 
| 1510 | apply (fast intro!: pdivmod_rel_pCons) | |
| 29451 | 1511 | done | 
| 1512 | ||
| 29537 | 1513 | lemma pdivmod_rel_unique: | 
| 1514 | assumes 1: "pdivmod_rel x y q1 r1" | |
| 1515 | assumes 2: "pdivmod_rel x y q2 r2" | |
| 29451 | 1516 | shows "q1 = q2 \<and> r1 = r2" | 
| 1517 | proof (cases "y = 0") | |
| 1518 | assume "y = 0" with assms show ?thesis | |
| 29537 | 1519 | by (simp add: pdivmod_rel_def) | 
| 29451 | 1520 | next | 
| 1521 | assume [simp]: "y \<noteq> 0" | |
| 1522 | from 1 have q1: "x = q1 * y + r1" and r1: "r1 = 0 \<or> degree r1 < degree y" | |
| 29537 | 1523 | unfolding pdivmod_rel_def by simp_all | 
| 29451 | 1524 | from 2 have q2: "x = q2 * y + r2" and r2: "r2 = 0 \<or> degree r2 < degree y" | 
| 29537 | 1525 | unfolding pdivmod_rel_def by simp_all | 
| 29451 | 1526 | from q1 q2 have q3: "(q1 - q2) * y = r2 - r1" | 
| 29667 | 1527 | by (simp add: algebra_simps) | 
| 29451 | 1528 | from r1 r2 have r3: "(r2 - r1) = 0 \<or> degree (r2 - r1) < degree y" | 
| 29453 | 1529 | by (auto intro: degree_diff_less) | 
| 29451 | 1530 | |
| 1531 | show "q1 = q2 \<and> r1 = r2" | |
| 1532 | proof (rule ccontr) | |
| 1533 | assume "\<not> (q1 = q2 \<and> r1 = r2)" | |
| 1534 | with q3 have "q1 \<noteq> q2" and "r1 \<noteq> r2" by auto | |
| 1535 | with r3 have "degree (r2 - r1) < degree y" by simp | |
| 1536 | also have "degree y \<le> degree (q1 - q2) + degree y" by simp | |
| 1537 | also have "\<dots> = degree ((q1 - q2) * y)" | |
| 60500 | 1538 | using \<open>q1 \<noteq> q2\<close> by (simp add: degree_mult_eq) | 
| 29451 | 1539 | also have "\<dots> = degree (r2 - r1)" | 
| 1540 | using q3 by simp | |
| 1541 | finally have "degree (r2 - r1) < degree (r2 - r1)" . | |
| 1542 | then show "False" by simp | |
| 1543 | qed | |
| 1544 | qed | |
| 1545 | ||
| 29660 | 1546 | lemma pdivmod_rel_0_iff: "pdivmod_rel 0 y q r \<longleftrightarrow> q = 0 \<and> r = 0" | 
| 1547 | by (auto dest: pdivmod_rel_unique intro: pdivmod_rel_0) | |
| 1548 | ||
| 1549 | lemma pdivmod_rel_by_0_iff: "pdivmod_rel x 0 q r \<longleftrightarrow> q = 0 \<and> r = x" | |
| 1550 | by (auto dest: pdivmod_rel_unique intro: pdivmod_rel_by_0) | |
| 1551 | ||
| 45605 | 1552 | lemmas pdivmod_rel_unique_div = pdivmod_rel_unique [THEN conjunct1] | 
| 29451 | 1553 | |
| 45605 | 1554 | lemmas pdivmod_rel_unique_mod = pdivmod_rel_unique [THEN conjunct2] | 
| 29451 | 1555 | |
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1556 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1557 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1558 | subsection\<open>Pseudo-Division and Division of Polynomials\<close> | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1559 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1560 | text\<open>This part is by René Thiemann and Akihisa Yamada.\<close> | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1561 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1562 | fun pseudo_divmod_main :: "'a :: comm_ring_1 \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1563 | \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> 'a poly \<times> 'a poly" where | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1564 | "pseudo_divmod_main lc q r d dr (Suc n) = (let | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1565 | rr = smult lc r; | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1566 | qq = coeff r dr; | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1567 | rrr = rr - monom qq n * d; | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1568 | qqq = smult lc q + monom qq n | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1569 | in pseudo_divmod_main lc qqq rrr d (dr - 1) n)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1570 | | "pseudo_divmod_main lc q r d dr 0 = (q,r)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1571 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1572 | definition pseudo_divmod :: "'a :: comm_ring_1 poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<times> 'a poly" where | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1573 | "pseudo_divmod p q \<equiv> if q = 0 then (0,p) else | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1574 | pseudo_divmod_main (coeff q (degree q)) 0 p q (degree p) (1 + length (coeffs p) - length (coeffs q))" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1575 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1576 | lemma coeff_0_degree_minus_1: "coeff rrr dr = 0 \<Longrightarrow> degree rrr \<le> dr \<Longrightarrow> degree rrr \<le> dr - 1" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1577 | using eq_zero_or_degree_less by fastforce | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1578 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1579 | lemma pseudo_divmod_main: assumes d: "d \<noteq> 0" "lc = coeff d (degree d)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1580 | and *: "degree r \<le> dr" "pseudo_divmod_main lc q r d dr n = (q',r')" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1581 | "n = 1 + dr - degree d \<or> dr = 0 \<and> n = 0 \<and> r = 0" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1582 | shows "(r' = 0 \<or> degree r' < degree d) \<and> smult (lc^n) (d * q + r) = d * q' + r'" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1583 | using * | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1584 | proof (induct n arbitrary: q r dr) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1585 | case (Suc n q r dr) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1586 | let ?rr = "smult lc r" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1587 | let ?qq = "coeff r dr" | 
| 63040 | 1588 | define b where [simp]: "b = monom ?qq n" | 
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1589 | let ?rrr = "?rr - b * d" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1590 | let ?qqq = "smult lc q + b" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1591 | note res = Suc(3) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1592 | from res[unfolded pseudo_divmod_main.simps[of lc q] Let_def] | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1593 | have res: "pseudo_divmod_main lc ?qqq ?rrr d (dr - 1) n = (q',r')" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1594 | by (simp del: pseudo_divmod_main.simps) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1595 | have dr: "dr = n + degree d" using Suc(4) by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1596 | have "coeff (b * d) dr = coeff b n * coeff d (degree d)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1597 | proof (cases "?qq = 0") | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1598 | case False | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1599 | hence n: "n = degree b" by (simp add: degree_monom_eq) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1600 | show ?thesis unfolding n dr by (simp add: coeff_mult_degree_sum) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1601 | qed auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1602 | also have "\<dots> = lc * coeff b n" unfolding d by simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1603 | finally have "coeff (b * d) dr = lc * coeff b n" . | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1604 | moreover have "coeff ?rr dr = lc * coeff r dr" by simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1605 | ultimately have c0: "coeff ?rrr dr = 0" by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1606 | have dr: "dr = n + degree d" using Suc(4) by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1607 | have deg_rr: "degree ?rr \<le> dr" using Suc(2) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1608 | using degree_smult_le dual_order.trans by blast | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1609 | have deg_bd: "degree (b * d) \<le> dr" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1610 | unfolding dr | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1611 | by(rule order.trans[OF degree_mult_le], auto simp: degree_monom_le) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1612 | have "degree ?rrr \<le> dr" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1613 | using degree_diff_le[OF deg_rr deg_bd] by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1614 | with c0 have deg_rrr: "degree ?rrr \<le> (dr - 1)" by (rule coeff_0_degree_minus_1) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1615 | have "n = 1 + (dr - 1) - degree d \<or> dr - 1 = 0 \<and> n = 0 \<and> ?rrr = 0" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1616 | proof (cases dr) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1617 | case 0 | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1618 | with Suc(4) have 0: "dr = 0" "n = 0" "degree d = 0" by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1619 | with deg_rrr have "degree ?rrr = 0" by simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1620 | hence "\<exists> a. ?rrr = [: a :]" by (metis degree_pCons_eq_if old.nat.distinct(2) pCons_cases) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1621 | from this obtain a where rrr: "?rrr = [:a:]" by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1622 | show ?thesis unfolding 0 using c0 unfolding rrr 0 by simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1623 | qed (insert Suc(4), auto) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1624 | note IH = Suc(1)[OF deg_rrr res this] | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1625 | show ?case | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1626 | proof (intro conjI) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1627 | show "r' = 0 \<or> degree r' < degree d" using IH by blast | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1628 | show "smult (lc ^ Suc n) (d * q + r) = d * q' + r'" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1629 | unfolding IH[THEN conjunct2,symmetric] | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1630 | by (simp add: field_simps smult_add_right) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1631 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1632 | qed auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1633 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1634 | lemma pseudo_divmod: | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1635 | assumes g: "g \<noteq> 0" and *: "pseudo_divmod f g = (q,r)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1636 | shows "smult (coeff g (degree g) ^ (Suc (degree f) - degree g)) f = g * q + r" (is ?A) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1637 | and "r = 0 \<or> degree r < degree g" (is ?B) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1638 | proof - | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1639 | from *[unfolded pseudo_divmod_def Let_def] | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1640 | have "pseudo_divmod_main (coeff g (degree g)) 0 f g (degree f) (1 + length (coeffs f) - length (coeffs g)) = (q, r)" by (auto simp: g) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1641 | note main = pseudo_divmod_main[OF _ _ _ this, OF g refl le_refl] | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1642 | have "1 + length (coeffs f) - length (coeffs g) = 1 + degree f - degree g \<or> | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1643 | degree f = 0 \<and> 1 + length (coeffs f) - length (coeffs g) = 0 \<and> f = 0" using g | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1644 | by (cases "f = 0"; cases "coeffs g", auto simp: degree_eq_length_coeffs) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1645 | note main = main[OF this] | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1646 | from main show "r = 0 \<or> degree r < degree g" by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1647 | show "smult (coeff g (degree g) ^ (Suc (degree f) - degree g)) f = g * q + r" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1648 | by (subst main[THEN conjunct2, symmetric], simp add: degree_eq_length_coeffs, | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1649 | insert g, cases "f = 0"; cases "coeffs g", auto) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1650 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1651 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1652 | definition "pseudo_mod_main lc r d dr n = snd (pseudo_divmod_main lc 0 r d dr n)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1653 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1654 | lemma snd_pseudo_divmod_main: | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1655 | "snd (pseudo_divmod_main lc q r d dr n) = snd (pseudo_divmod_main lc q' r d dr n)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1656 | by (induct n arbitrary: q q' lc r d dr; simp add: Let_def) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1657 | |
| 63498 | 1658 | definition pseudo_mod | 
| 1659 |     :: "'a :: {comm_ring_1,semiring_1_no_zero_divisors} poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where
 | |
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1660 | "pseudo_mod f g = snd (pseudo_divmod f g)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1661 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1662 | lemma pseudo_mod: | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1663 | fixes f g | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1664 | defines "r \<equiv> pseudo_mod f g" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1665 | assumes g: "g \<noteq> 0" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1666 | shows "\<exists> a q. a \<noteq> 0 \<and> smult a f = g * q + r" "r = 0 \<or> degree r < degree g" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1667 | proof - | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1668 | let ?cg = "coeff g (degree g)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1669 | let ?cge = "?cg ^ (Suc (degree f) - degree g)" | 
| 63040 | 1670 | define a where "a = ?cge" | 
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1671 | obtain q where pdm: "pseudo_divmod f g = (q,r)" using r_def[unfolded pseudo_mod_def] | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1672 | by (cases "pseudo_divmod f g", auto) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1673 | from pseudo_divmod[OF g pdm] have id: "smult a f = g * q + r" and "r = 0 \<or> degree r < degree g" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1674 | unfolding a_def by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1675 | show "r = 0 \<or> degree r < degree g" by fact | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1676 | from g have "a \<noteq> 0" unfolding a_def by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1677 | thus "\<exists> a q. a \<noteq> 0 \<and> smult a f = g * q + r" using id by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1678 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1679 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1680 | instantiation poly :: (idom_divide) idom_divide | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1681 | begin | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1682 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1683 | fun divide_poly_main :: "'a \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1684 | \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> 'a poly" where | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1685 | "divide_poly_main lc q r d dr (Suc n) = (let cr = coeff r dr; a = cr div lc; mon = monom a n in | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1686 | if False \<or> a * lc = cr then (* False \<or> is only because of problem in function-package *) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1687 | divide_poly_main | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1688 | lc | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1689 | (q + mon) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1690 | (r - mon * d) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1691 | d (dr - 1) n else 0)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1692 | | "divide_poly_main lc q r d dr 0 = q" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1693 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1694 | definition divide_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1695 | "divide_poly f g = (if g = 0 then 0 else | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1696 | divide_poly_main (coeff g (degree g)) 0 f g (degree f) (1 + length (coeffs f) - length (coeffs g)))" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1697 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1698 | lemma divide_poly_main: | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1699 | assumes d: "d \<noteq> 0" "lc = coeff d (degree d)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1700 | and *: "degree (d * r) \<le> dr" "divide_poly_main lc q (d * r) d dr n = q'" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1701 | "n = 1 + dr - degree d \<or> dr = 0 \<and> n = 0 \<and> d * r = 0" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1702 | shows "q' = q + r" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1703 | using * | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1704 | proof (induct n arbitrary: q r dr) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1705 | case (Suc n q r dr) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1706 | let ?rr = "d * r" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1707 | let ?a = "coeff ?rr dr" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1708 | let ?qq = "?a div lc" | 
| 63040 | 1709 | define b where [simp]: "b = monom ?qq n" | 
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1710 | let ?rrr = "d * (r - b)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1711 | let ?qqq = "q + b" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1712 | note res = Suc(3) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1713 | have dr: "dr = n + degree d" using Suc(4) by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1714 | have lc: "lc \<noteq> 0" using d by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1715 | have "coeff (b * d) dr = coeff b n * coeff d (degree d)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1716 | proof (cases "?qq = 0") | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1717 | case False | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1718 | hence n: "n = degree b" by (simp add: degree_monom_eq) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1719 | show ?thesis unfolding n dr by (simp add: coeff_mult_degree_sum) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1720 | qed simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1721 | also have "\<dots> = lc * coeff b n" unfolding d by simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1722 | finally have c2: "coeff (b * d) dr = lc * coeff b n" . | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1723 | have rrr: "?rrr = ?rr - b * d" by (simp add: field_simps) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1724 | have c1: "coeff (d * r) dr = lc * coeff r n" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1725 | proof (cases "degree r = n") | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1726 | case True | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1727 | thus ?thesis using Suc(2) unfolding dr using coeff_mult_degree_sum[of d r] d by (auto simp: ac_simps) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1728 | next | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1729 | case False | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1730 | have "degree r \<le> n" using dr Suc(2) by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1731 | (metis add.commute add_le_cancel_left d(1) degree_0 degree_mult_eq diff_is_0_eq diff_zero le_cases) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1732 | with False have r_n: "degree r < n" by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1733 | hence right: "lc * coeff r n = 0" by (simp add: coeff_eq_0) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1734 | have "coeff (d * r) dr = coeff (d * r) (degree d + n)" unfolding dr by (simp add: ac_simps) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1735 | also have "\<dots> = 0" using r_n | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1736 | by (metis False Suc.prems(1) add.commute add_left_imp_eq coeff_degree_mult coeff_eq_0 | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1737 | coeff_mult_degree_sum degree_mult_le dr le_eq_less_or_eq) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1738 | finally show ?thesis unfolding right . | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1739 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1740 | have c0: "coeff ?rrr dr = 0" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1741 | and id: "lc * (coeff (d * r) dr div lc) = coeff (d * r) dr" unfolding rrr coeff_diff c2 | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1742 | unfolding b_def coeff_monom coeff_smult c1 using lc by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1743 | from res[unfolded divide_poly_main.simps[of lc q] Let_def] id | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1744 | have res: "divide_poly_main lc ?qqq ?rrr d (dr - 1) n = q'" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1745 | by (simp del: divide_poly_main.simps add: field_simps) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1746 | note IH = Suc(1)[OF _ res] | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1747 | have dr: "dr = n + degree d" using Suc(4) by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1748 | have deg_rr: "degree ?rr \<le> dr" using Suc(2) by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1749 | have deg_bd: "degree (b * d) \<le> dr" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1750 | unfolding dr b_def by (rule order.trans[OF degree_mult_le], auto simp: degree_monom_le) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1751 | have "degree ?rrr \<le> dr" unfolding rrr by (rule degree_diff_le[OF deg_rr deg_bd]) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1752 | with c0 have deg_rrr: "degree ?rrr \<le> (dr - 1)" by (rule coeff_0_degree_minus_1) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1753 | have "n = 1 + (dr - 1) - degree d \<or> dr - 1 = 0 \<and> n = 0 \<and> ?rrr = 0" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1754 | proof (cases dr) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1755 | case 0 | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1756 | with Suc(4) have 0: "dr = 0" "n = 0" "degree d = 0" by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1757 | with deg_rrr have "degree ?rrr = 0" by simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1758 | from degree_eq_zeroE[OF this] obtain a where rrr: "?rrr = [:a:]" by metis | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1759 | show ?thesis unfolding 0 using c0 unfolding rrr 0 by simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1760 | qed (insert Suc(4), auto) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1761 | note IH = IH[OF deg_rrr this] | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1762 | show ?case using IH by simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1763 | next | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1764 | case (0 q r dr) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1765 | show ?case | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1766 | proof (cases "r = 0") | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1767 | case True | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1768 | thus ?thesis using 0 by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1769 | next | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1770 | case False | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1771 | have "degree (d * r) = degree d + degree r" using d False | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1772 | by (subst degree_mult_eq, auto) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1773 | thus ?thesis using 0 d by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1774 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1775 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1776 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1777 | lemma fst_pseudo_divmod_main_as_divide_poly_main: | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1778 | assumes d: "d \<noteq> 0" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1779 | defines lc: "lc \<equiv> coeff d (degree d)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1780 | shows "fst (pseudo_divmod_main lc q r d dr n) = divide_poly_main lc (smult (lc^n) q) (smult (lc^n) r) d dr n" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1781 | proof(induct n arbitrary: q r dr) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1782 | case 0 then show ?case by simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1783 | next | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1784 | case (Suc n) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1785 | note lc0 = leading_coeff_neq_0[OF d, folded lc] | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1786 | then have "pseudo_divmod_main lc q r d dr (Suc n) = | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1787 | pseudo_divmod_main lc (smult lc q + monom (coeff r dr) n) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1788 | (smult lc r - monom (coeff r dr) n * d) d (dr - 1) n" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1789 | by (simp add: Let_def ac_simps) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1790 | also have "fst ... = divide_poly_main lc | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1791 | (smult (lc^n) (smult lc q + monom (coeff r dr) n)) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1792 | (smult (lc^n) (smult lc r - monom (coeff r dr) n * d)) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1793 | d (dr - 1) n" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1794 | unfolding Suc[unfolded divide_poly_main.simps Let_def].. | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1795 | also have "... = divide_poly_main lc (smult (lc ^ Suc n) q) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1796 | (smult (lc ^ Suc n) r) d dr (Suc n)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1797 | unfolding smult_monom smult_distribs mult_smult_left[symmetric] | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1798 | using lc0 by (simp add: Let_def ac_simps) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1799 | finally show ?case. | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1800 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1801 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1802 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1803 | lemma divide_poly_main_0: "divide_poly_main 0 0 r d dr n = 0" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1804 | proof (induct n arbitrary: r d dr) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1805 | case (Suc n r d dr) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1806 | show ?case unfolding divide_poly_main.simps[of _ _ r] Let_def | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1807 | by (simp add: Suc del: divide_poly_main.simps) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1808 | qed simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1809 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1810 | lemma divide_poly: | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1811 | assumes g: "g \<noteq> 0" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1812 | shows "(f * g) div g = (f :: 'a poly)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1813 | proof - | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1814 | have "divide_poly_main (coeff g (degree g)) 0 (g * f) g (degree (g * f)) (1 + length (coeffs (g * f)) - length (coeffs g)) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1815 | = (f * g) div g" unfolding divide_poly_def Let_def by (simp add: ac_simps) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1816 | note main = divide_poly_main[OF g refl le_refl this] | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1817 |   {
 | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1818 | fix f :: "'a poly" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1819 | assume "f \<noteq> 0" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1820 | hence "length (coeffs f) = Suc (degree f)" unfolding degree_eq_length_coeffs by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1821 | } note len = this | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1822 | have "(f * g) div g = 0 + f" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1823 | proof (rule main, goal_cases) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1824 | case 1 | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1825 | show ?case | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1826 | proof (cases "f = 0") | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1827 | case True | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1828 | with g show ?thesis by (auto simp: degree_eq_length_coeffs) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1829 | next | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1830 | case False | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1831 | with g have fg: "g * f \<noteq> 0" by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1832 | show ?thesis unfolding len[OF fg] len[OF g] by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1833 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1834 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1835 | thus ?thesis by simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1836 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1837 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1838 | lemma divide_poly_0: "f div 0 = (0 :: 'a poly)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1839 | by (simp add: divide_poly_def Let_def divide_poly_main_0) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1840 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1841 | instance by (standard, auto simp: divide_poly divide_poly_0) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1842 | end | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1843 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1844 | |
| 63498 | 1845 | instance poly :: (idom_divide) algebraic_semidom .. | 
| 1846 | ||
| 1847 | ||
| 1848 | ||
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1849 | subsubsection\<open>Division in Field Polynomials\<close> | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1850 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1851 | text\<open> | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1852 | This part connects the above result to the division of field polynomials. | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1853 | Mainly imported from Isabelle 2016. | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1854 | \<close> | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1855 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1856 | lemma pseudo_divmod_field: | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1857 | assumes g: "(g::'a::field poly) \<noteq> 0" and *: "pseudo_divmod f g = (q,r)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1858 | defines "c \<equiv> coeff g (degree g) ^ (Suc (degree f) - degree g)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1859 | shows "f = g * smult (1/c) q + smult (1/c) r" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1860 | proof - | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1861 | from leading_coeff_neq_0[OF g] have c0: "c \<noteq> 0" unfolding c_def by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1862 | from pseudo_divmod(1)[OF g *, folded c_def] | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1863 | have "smult c f = g * q + r" by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1864 | also have "smult (1/c) ... = g * smult (1/c) q + smult (1/c) r" by (simp add: smult_add_right) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1865 | finally show ?thesis using c0 by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1866 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1867 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1868 | lemma divide_poly_main_field: | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1869 | assumes d: "(d::'a::field poly) \<noteq> 0" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1870 | defines lc: "lc \<equiv> coeff d (degree d)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1871 | shows "divide_poly_main lc q r d dr n = fst (pseudo_divmod_main lc (smult ((1/lc)^n) q) (smult ((1/lc)^n) r) d dr n)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1872 | unfolding lc | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1873 | by(subst fst_pseudo_divmod_main_as_divide_poly_main, auto simp: d power_one_over) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1874 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1875 | lemma divide_poly_field: | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1876 | fixes f g :: "'a :: field poly" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1877 | defines "f' \<equiv> smult ((1 / coeff g (degree g)) ^ (Suc (degree f) - degree g)) f" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1878 | shows "(f::'a::field poly) div g = fst (pseudo_divmod f' g)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1879 | proof (cases "g = 0") | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1880 | case True show ?thesis | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1881 | unfolding divide_poly_def pseudo_divmod_def Let_def f'_def True by (simp add: divide_poly_main_0) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1882 | next | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1883 | case False | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1884 | from leading_coeff_neq_0[OF False] have "degree f' = degree f" unfolding f'_def by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1885 | then show ?thesis | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1886 | using length_coeffs_degree[of f'] length_coeffs_degree[of f] | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1887 | unfolding divide_poly_def pseudo_divmod_def Let_def | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1888 | divide_poly_main_field[OF False] | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1889 | length_coeffs_degree[OF False] | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1890 | f'_def | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1891 | by force | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1892 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1893 | |
| 29451 | 1894 | instantiation poly :: (field) ring_div | 
| 1895 | begin | |
| 1896 | ||
| 63950 
cdc1e59aa513
syntactic type class for operation mod named after mod;
 haftmann parents: 
63918diff
changeset | 1897 | definition modulo_poly where | 
| 
cdc1e59aa513
syntactic type class for operation mod named after mod;
 haftmann parents: 
63918diff
changeset | 1898 | mod_poly_def: "f mod g \<equiv> | 
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1899 | if g = 0 then f | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1900 | else pseudo_mod (smult ((1/coeff g (degree g)) ^ (Suc (degree f) - degree g)) f) g" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1901 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1902 | lemma pdivmod_rel: "pdivmod_rel (x::'a::field poly) y (x div y) (x mod y)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1903 | unfolding pdivmod_rel_def | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1904 | proof (intro conjI) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1905 | show "x = x div y * y + x mod y" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1906 | proof(cases "y = 0") | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1907 | case True show ?thesis by(simp add: True divide_poly_def divide_poly_0 mod_poly_def) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1908 | next | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1909 | case False | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1910 | then have "pseudo_divmod (smult ((1 / coeff y (degree y)) ^ (Suc (degree x) - degree y)) x) y = | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1911 | (x div y, x mod y)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1912 | unfolding divide_poly_field mod_poly_def pseudo_mod_def by simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1913 | from pseudo_divmod[OF False this] | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1914 | show ?thesis using False | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1915 | by (simp add: power_mult_distrib[symmetric] mult.commute) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1916 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1917 | show "if y = 0 then x div y = 0 else x mod y = 0 \<or> degree (x mod y) < degree y" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1918 | proof (cases "y = 0") | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1919 | case True then show ?thesis by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1920 | next | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1921 | case False | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1922 | with pseudo_mod[OF this] show ?thesis unfolding mod_poly_def by simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1923 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1924 | qed | 
| 29451 | 1925 | |
| 1926 | lemma div_poly_eq: | |
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1927 | "pdivmod_rel (x::'a::field poly) y q r \<Longrightarrow> x div y = q" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1928 | by(rule pdivmod_rel_unique_div[OF pdivmod_rel]) | 
| 29451 | 1929 | |
| 1930 | lemma mod_poly_eq: | |
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1931 | "pdivmod_rel (x::'a::field poly) y q r \<Longrightarrow> x mod y = r" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1932 | by (rule pdivmod_rel_unique_mod[OF pdivmod_rel]) | 
| 29451 | 1933 | |
| 60679 | 1934 | instance | 
| 1935 | proof | |
| 29451 | 1936 | fix x y :: "'a poly" | 
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1937 | from pdivmod_rel[of x y,unfolded pdivmod_rel_def] | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1938 | show "x div y * y + x mod y = x" by auto | 
| 29451 | 1939 | next | 
| 1940 | fix x :: "'a poly" | |
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1941 | show "x div 0 = 0" by simp | 
| 29451 | 1942 | next | 
| 1943 | fix y :: "'a poly" | |
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1944 | show "0 div y = 0" by simp | 
| 29451 | 1945 | next | 
| 1946 | fix x y z :: "'a poly" | |
| 1947 | assume "y \<noteq> 0" | |
| 60429 
d3d1e185cd63
uniform _ div _ as infix syntax for ring division
 haftmann parents: 
60352diff
changeset | 1948 | hence "pdivmod_rel (x + z * y) y (z + x div y) (x mod y)" | 
| 29537 | 1949 | using pdivmod_rel [of x y] | 
| 49962 
a8cc904a6820
Renamed {left,right}_distrib to distrib_{right,left}.
 webertj parents: 
49834diff
changeset | 1950 | by (simp add: pdivmod_rel_def distrib_right) | 
| 60429 
d3d1e185cd63
uniform _ div _ as infix syntax for ring division
 haftmann parents: 
60352diff
changeset | 1951 | thus "(x + z * y) div y = z + x div y" | 
| 29451 | 1952 | by (rule div_poly_eq) | 
| 30930 | 1953 | next | 
| 1954 | fix x y z :: "'a poly" | |
| 1955 | assume "x \<noteq> 0" | |
| 60429 
d3d1e185cd63
uniform _ div _ as infix syntax for ring division
 haftmann parents: 
60352diff
changeset | 1956 | show "(x * y) div (x * z) = y div z" | 
| 30930 | 1957 | proof (cases "y \<noteq> 0 \<and> z \<noteq> 0") | 
| 1958 | have "\<And>x::'a poly. pdivmod_rel x 0 0 x" | |
| 1959 | by (rule pdivmod_rel_by_0) | |
| 60429 
d3d1e185cd63
uniform _ div _ as infix syntax for ring division
 haftmann parents: 
60352diff
changeset | 1960 | then have [simp]: "\<And>x::'a poly. x div 0 = 0" | 
| 30930 | 1961 | by (rule div_poly_eq) | 
| 1962 | have "\<And>x::'a poly. pdivmod_rel 0 x 0 0" | |
| 1963 | by (rule pdivmod_rel_0) | |
| 60429 
d3d1e185cd63
uniform _ div _ as infix syntax for ring division
 haftmann parents: 
60352diff
changeset | 1964 | then have [simp]: "\<And>x::'a poly. 0 div x = 0" | 
| 30930 | 1965 | by (rule div_poly_eq) | 
| 1966 | case False then show ?thesis by auto | |
| 1967 | next | |
| 1968 | case True then have "y \<noteq> 0" and "z \<noteq> 0" by auto | |
| 60500 | 1969 | with \<open>x \<noteq> 0\<close> | 
| 30930 | 1970 | have "\<And>q r. pdivmod_rel y z q r \<Longrightarrow> pdivmod_rel (x * y) (x * z) q (x * r)" | 
| 1971 | by (auto simp add: pdivmod_rel_def algebra_simps) | |
| 1972 | (rule classical, simp add: degree_mult_eq) | |
| 60429 
d3d1e185cd63
uniform _ div _ as infix syntax for ring division
 haftmann parents: 
60352diff
changeset | 1973 | moreover from pdivmod_rel have "pdivmod_rel y z (y div z) (y mod z)" . | 
| 
d3d1e185cd63
uniform _ div _ as infix syntax for ring division
 haftmann parents: 
60352diff
changeset | 1974 | ultimately have "pdivmod_rel (x * y) (x * z) (y div z) (x * (y mod z))" . | 
| 30930 | 1975 | then show ?thesis by (simp add: div_poly_eq) | 
| 1976 | qed | |
| 29451 | 1977 | qed | 
| 1978 | ||
| 1979 | end | |
| 1980 | ||
| 60570 | 1981 | lemma is_unit_monom_0: | 
| 1982 | fixes a :: "'a::field" | |
| 1983 | assumes "a \<noteq> 0" | |
| 1984 | shows "is_unit (monom a 0)" | |
| 1985 | proof | |
| 62351 | 1986 | from assms show "1 = monom a 0 * monom (inverse a) 0" | 
| 60570 | 1987 | by (simp add: mult_monom) | 
| 1988 | qed | |
| 1989 | ||
| 1990 | lemma is_unit_triv: | |
| 1991 | fixes a :: "'a::field" | |
| 1992 | assumes "a \<noteq> 0" | |
| 1993 | shows "is_unit [:a:]" | |
| 1994 | using assms by (simp add: is_unit_monom_0 monom_0 [symmetric]) | |
| 1995 | ||
| 1996 | lemma is_unit_iff_degree: | |
| 63498 | 1997 | assumes "p \<noteq> (0 :: _ :: field poly)" | 
| 60570 | 1998 | shows "is_unit p \<longleftrightarrow> degree p = 0" (is "?P \<longleftrightarrow> ?Q") | 
| 1999 | proof | |
| 2000 | assume ?Q | |
| 2001 | then obtain a where "p = [:a:]" by (rule degree_eq_zeroE) | |
| 2002 | with assms show ?P by (simp add: is_unit_triv) | |
| 2003 | next | |
| 2004 | assume ?P | |
| 2005 | then obtain q where "q \<noteq> 0" "p * q = 1" .. | |
| 2006 | then have "degree (p * q) = degree 1" | |
| 2007 | by simp | |
| 2008 | with \<open>p \<noteq> 0\<close> \<open>q \<noteq> 0\<close> have "degree p + degree q = 0" | |
| 2009 | by (simp add: degree_mult_eq) | |
| 2010 | then show ?Q by simp | |
| 2011 | qed | |
| 2012 | ||
| 2013 | lemma is_unit_pCons_iff: | |
| 63498 | 2014 | "is_unit (pCons (a::_::field) p) \<longleftrightarrow> p = 0 \<and> a \<noteq> 0" | 
| 60570 | 2015 | by (cases "p = 0") (auto simp add: is_unit_triv is_unit_iff_degree) | 
| 2016 | ||
| 2017 | lemma is_unit_monom_trival: | |
| 2018 | fixes p :: "'a::field poly" | |
| 2019 | assumes "is_unit p" | |
| 2020 | shows "monom (coeff p (degree p)) 0 = p" | |
| 2021 | using assms by (cases p) (simp_all add: monom_0 is_unit_pCons_iff) | |
| 2022 | ||
| 60685 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 2023 | lemma is_unit_polyE: | 
| 63498 | 2024 | assumes "is_unit (p::_::field poly)" | 
| 60685 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 2025 | obtains a where "p = monom a 0" and "a \<noteq> 0" | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 2026 | proof - | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 2027 | obtain a q where "p = pCons a q" by (cases p) | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 2028 | with assms have "p = [:a:]" and "a \<noteq> 0" | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 2029 | by (simp_all add: is_unit_pCons_iff) | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 2030 | with that show thesis by (simp add: monom_0) | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 2031 | qed | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 2032 | |
| 29451 | 2033 | lemma degree_mod_less: | 
| 2034 | "y \<noteq> 0 \<Longrightarrow> x mod y = 0 \<or> degree (x mod y) < degree y" | |
| 29537 | 2035 | using pdivmod_rel [of x y] | 
| 2036 | unfolding pdivmod_rel_def by simp | |
| 29451 | 2037 | |
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2038 | lemma div_poly_less: "degree (x::'a::field poly) < degree y \<Longrightarrow> x div y = 0" | 
| 29451 | 2039 | proof - | 
| 2040 | assume "degree x < degree y" | |
| 29537 | 2041 | hence "pdivmod_rel x y 0 x" | 
| 2042 | by (simp add: pdivmod_rel_def) | |
| 29451 | 2043 | thus "x div y = 0" by (rule div_poly_eq) | 
| 2044 | qed | |
| 2045 | ||
| 2046 | lemma mod_poly_less: "degree x < degree y \<Longrightarrow> x mod y = x" | |
| 2047 | proof - | |
| 2048 | assume "degree x < degree y" | |
| 29537 | 2049 | hence "pdivmod_rel x y 0 x" | 
| 2050 | by (simp add: pdivmod_rel_def) | |
| 29451 | 2051 | thus "x mod y = x" by (rule mod_poly_eq) | 
| 2052 | qed | |
| 2053 | ||
| 29659 | 2054 | lemma pdivmod_rel_smult_left: | 
| 2055 | "pdivmod_rel x y q r | |
| 2056 | \<Longrightarrow> pdivmod_rel (smult a x) y (smult a q) (smult a r)" | |
| 2057 | unfolding pdivmod_rel_def by (simp add: smult_add_right) | |
| 2058 | ||
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2059 | lemma div_smult_left: "(smult (a::'a::field) x) div y = smult a (x div y)" | 
| 29659 | 2060 | by (rule div_poly_eq, rule pdivmod_rel_smult_left, rule pdivmod_rel) | 
| 2061 | ||
| 2062 | lemma mod_smult_left: "(smult a x) mod y = smult a (x mod y)" | |
| 2063 | by (rule mod_poly_eq, rule pdivmod_rel_smult_left, rule pdivmod_rel) | |
| 2064 | ||
| 30072 | 2065 | lemma poly_div_minus_left [simp]: | 
| 2066 | fixes x y :: "'a::field poly" | |
| 2067 | shows "(- x) div y = - (x div y)" | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54230diff
changeset | 2068 | using div_smult_left [of "- 1::'a"] by simp | 
| 30072 | 2069 | |
| 2070 | lemma poly_mod_minus_left [simp]: | |
| 2071 | fixes x y :: "'a::field poly" | |
| 2072 | shows "(- x) mod y = - (x mod y)" | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54230diff
changeset | 2073 | using mod_smult_left [of "- 1::'a"] by simp | 
| 30072 | 2074 | |
| 57482 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2075 | lemma pdivmod_rel_add_left: | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2076 | assumes "pdivmod_rel x y q r" | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2077 | assumes "pdivmod_rel x' y q' r'" | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2078 | shows "pdivmod_rel (x + x') y (q + q') (r + r')" | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2079 | using assms unfolding pdivmod_rel_def | 
| 59557 | 2080 | by (auto simp add: algebra_simps degree_add_less) | 
| 57482 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2081 | |
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2082 | lemma poly_div_add_left: | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2083 | fixes x y z :: "'a::field poly" | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2084 | shows "(x + y) div z = x div z + y div z" | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2085 | using pdivmod_rel_add_left [OF pdivmod_rel pdivmod_rel] | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2086 | by (rule div_poly_eq) | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2087 | |
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2088 | lemma poly_mod_add_left: | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2089 | fixes x y z :: "'a::field poly" | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2090 | shows "(x + y) mod z = x mod z + y mod z" | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2091 | using pdivmod_rel_add_left [OF pdivmod_rel pdivmod_rel] | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2092 | by (rule mod_poly_eq) | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2093 | |
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2094 | lemma poly_div_diff_left: | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2095 | fixes x y z :: "'a::field poly" | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2096 | shows "(x - y) div z = x div z - y div z" | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2097 | by (simp only: diff_conv_add_uminus poly_div_add_left poly_div_minus_left) | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2098 | |
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2099 | lemma poly_mod_diff_left: | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2100 | fixes x y z :: "'a::field poly" | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2101 | shows "(x - y) mod z = x mod z - y mod z" | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2102 | by (simp only: diff_conv_add_uminus poly_mod_add_left poly_mod_minus_left) | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2103 | |
| 29659 | 2104 | lemma pdivmod_rel_smult_right: | 
| 2105 | "\<lbrakk>a \<noteq> 0; pdivmod_rel x y q r\<rbrakk> | |
| 2106 | \<Longrightarrow> pdivmod_rel x (smult a y) (smult (inverse a) q) r" | |
| 2107 | unfolding pdivmod_rel_def by simp | |
| 2108 | ||
| 2109 | lemma div_smult_right: | |
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2110 | "(a::'a::field) \<noteq> 0 \<Longrightarrow> x div (smult a y) = smult (inverse a) (x div y)" | 
| 29659 | 2111 | by (rule div_poly_eq, erule pdivmod_rel_smult_right, rule pdivmod_rel) | 
| 2112 | ||
| 2113 | lemma mod_smult_right: "a \<noteq> 0 \<Longrightarrow> x mod (smult a y) = x mod y" | |
| 2114 | by (rule mod_poly_eq, erule pdivmod_rel_smult_right, rule pdivmod_rel) | |
| 2115 | ||
| 30072 | 2116 | lemma poly_div_minus_right [simp]: | 
| 2117 | fixes x y :: "'a::field poly" | |
| 2118 | shows "x div (- y) = - (x div y)" | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54230diff
changeset | 2119 | using div_smult_right [of "- 1::'a"] by (simp add: nonzero_inverse_minus_eq) | 
| 30072 | 2120 | |
| 2121 | lemma poly_mod_minus_right [simp]: | |
| 2122 | fixes x y :: "'a::field poly" | |
| 2123 | shows "x mod (- y) = x mod y" | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54230diff
changeset | 2124 | using mod_smult_right [of "- 1::'a"] by simp | 
| 30072 | 2125 | |
| 29660 | 2126 | lemma pdivmod_rel_mult: | 
| 2127 | "\<lbrakk>pdivmod_rel x y q r; pdivmod_rel q z q' r'\<rbrakk> | |
| 2128 | \<Longrightarrow> pdivmod_rel x (y * z) q' (y * r' + r)" | |
| 2129 | apply (cases "z = 0", simp add: pdivmod_rel_def) | |
| 2130 | apply (cases "y = 0", simp add: pdivmod_rel_by_0_iff pdivmod_rel_0_iff) | |
| 2131 | apply (cases "r = 0") | |
| 2132 | apply (cases "r' = 0") | |
| 2133 | apply (simp add: pdivmod_rel_def) | |
| 36350 | 2134 | apply (simp add: pdivmod_rel_def field_simps degree_mult_eq) | 
| 29660 | 2135 | apply (cases "r' = 0") | 
| 2136 | apply (simp add: pdivmod_rel_def degree_mult_eq) | |
| 36350 | 2137 | apply (simp add: pdivmod_rel_def field_simps) | 
| 29660 | 2138 | apply (simp add: degree_mult_eq degree_add_less) | 
| 2139 | done | |
| 2140 | ||
| 2141 | lemma poly_div_mult_right: | |
| 2142 | fixes x y z :: "'a::field poly" | |
| 2143 | shows "x div (y * z) = (x div y) div z" | |
| 2144 | by (rule div_poly_eq, rule pdivmod_rel_mult, (rule pdivmod_rel)+) | |
| 2145 | ||
| 2146 | lemma poly_mod_mult_right: | |
| 2147 | fixes x y z :: "'a::field poly" | |
| 2148 | shows "x mod (y * z) = y * (x div y mod z) + x mod y" | |
| 2149 | by (rule mod_poly_eq, rule pdivmod_rel_mult, (rule pdivmod_rel)+) | |
| 2150 | ||
| 29451 | 2151 | lemma mod_pCons: | 
| 2152 | fixes a and x | |
| 2153 | assumes y: "y \<noteq> 0" | |
| 2154 | defines b: "b \<equiv> coeff (pCons a (x mod y)) (degree y) / coeff y (degree y)" | |
| 2155 | shows "(pCons a x) mod y = (pCons a (x mod y) - smult b y)" | |
| 2156 | unfolding b | |
| 2157 | apply (rule mod_poly_eq) | |
| 29537 | 2158 | apply (rule pdivmod_rel_pCons [OF pdivmod_rel y refl]) | 
| 29451 | 2159 | done | 
| 2160 | ||
| 52380 | 2161 | definition pdivmod :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<times> 'a poly" | 
| 2162 | where | |
| 2163 | "pdivmod p q = (p div q, p mod q)" | |
| 31663 | 2164 | |
| 52380 | 2165 | lemma pdivmod_0: | 
| 2166 | "pdivmod 0 q = (0, 0)" | |
| 2167 | by (simp add: pdivmod_def) | |
| 31663 | 2168 | |
| 52380 | 2169 | lemma pdivmod_pCons: | 
| 2170 | "pdivmod (pCons a p) q = | |
| 2171 | (if q = 0 then (0, pCons a p) else | |
| 2172 | (let (s, r) = pdivmod p q; | |
| 2173 | b = coeff (pCons a r) (degree q) / coeff q (degree q) | |
| 2174 | in (pCons b s, pCons a r - smult b q)))" | |
| 2175 | apply (simp add: pdivmod_def Let_def, safe) | |
| 2176 | apply (rule div_poly_eq) | |
| 2177 | apply (erule pdivmod_rel_pCons [OF pdivmod_rel _ refl]) | |
| 2178 | apply (rule mod_poly_eq) | |
| 2179 | apply (erule pdivmod_rel_pCons [OF pdivmod_rel _ refl]) | |
| 29451 | 2180 | done | 
| 2181 | ||
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2182 | lemma pdivmod_fold_coeffs: | 
| 52380 | 2183 | "pdivmod p q = (if q = 0 then (0, p) | 
| 2184 | else fold_coeffs (\<lambda>a (s, r). | |
| 2185 | let b = coeff (pCons a r) (degree q) / coeff q (degree q) | |
| 2186 | in (pCons b s, pCons a r - smult b q) | |
| 2187 | ) p (0, 0))" | |
| 2188 | apply (cases "q = 0") | |
| 2189 | apply (simp add: pdivmod_def) | |
| 2190 | apply (rule sym) | |
| 2191 | apply (induct p) | |
| 2192 | apply (simp_all add: pdivmod_0 pdivmod_pCons) | |
| 2193 | apply (case_tac "a = 0 \<and> p = 0") | |
| 2194 | apply (auto simp add: pdivmod_def) | |
| 2195 | done | |
| 29980 | 2196 | |
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2197 | subsection \<open>List-based versions for fast implementation\<close> | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2198 | (* Subsection by: | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2199 | Sebastiaan Joosten | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2200 | René Thiemann | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2201 | Akihisa Yamada | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2202 | *) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2203 | fun minus_poly_rev_list :: "'a :: group_add list \<Rightarrow> 'a list \<Rightarrow> 'a list" where | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2204 | "minus_poly_rev_list (x # xs) (y # ys) = (x - y) # (minus_poly_rev_list xs ys)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2205 | | "minus_poly_rev_list xs [] = xs" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2206 | | "minus_poly_rev_list [] (y # ys) = []" | 
| 63035 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2207 | |
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2208 | fun pseudo_divmod_main_list :: "'a::comm_ring_1 \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> 'a list | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2209 | \<Rightarrow> nat \<Rightarrow> 'a list \<times> 'a list" where | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2210 | "pseudo_divmod_main_list lc q r d (Suc n) = (let | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2211 | rr = map (op * lc) r; | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2212 | a = hd r; | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2213 | qqq = cCons a (map (op * lc) q); | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2214 | rrr = tl (if a = 0 then rr else minus_poly_rev_list rr (map (op * a) d)) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2215 | in pseudo_divmod_main_list lc qqq rrr d n)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2216 | | "pseudo_divmod_main_list lc q r d 0 = (q,r)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2217 | |
| 63035 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2218 | fun pseudo_mod_main_list :: "'a::comm_ring_1 \<Rightarrow> 'a list \<Rightarrow> 'a list | 
| 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2219 | \<Rightarrow> nat \<Rightarrow> 'a list" where | 
| 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2220 | "pseudo_mod_main_list lc r d (Suc n) = (let | 
| 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2221 | rr = map (op * lc) r; | 
| 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2222 | a = hd r; | 
| 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2223 | rrr = tl (if a = 0 then rr else minus_poly_rev_list rr (map (op * a) d)) | 
| 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2224 | in pseudo_mod_main_list lc rrr d n)" | 
| 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2225 | | "pseudo_mod_main_list lc r d 0 = r" | 
| 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2226 | |
| 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2227 | |
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2228 | fun divmod_poly_one_main_list :: "'a::comm_ring_1 list \<Rightarrow> 'a list \<Rightarrow> 'a list | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2229 | \<Rightarrow> nat \<Rightarrow> 'a list \<times> 'a list" where | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2230 | "divmod_poly_one_main_list q r d (Suc n) = (let | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2231 | a = hd r; | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2232 | qqq = cCons a q; | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2233 | rr = tl (if a = 0 then r else minus_poly_rev_list r (map (op * a) d)) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2234 | in divmod_poly_one_main_list qqq rr d n)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2235 | | "divmod_poly_one_main_list q r d 0 = (q,r)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2236 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2237 | fun mod_poly_one_main_list :: "'a::comm_ring_1 list \<Rightarrow> 'a list | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2238 | \<Rightarrow> nat \<Rightarrow> 'a list" where | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2239 | "mod_poly_one_main_list r d (Suc n) = (let | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2240 | a = hd r; | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2241 | rr = tl (if a = 0 then r else minus_poly_rev_list r (map (op * a) d)) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2242 | in mod_poly_one_main_list rr d n)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2243 | | "mod_poly_one_main_list r d 0 = r" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2244 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2245 | definition pseudo_divmod_list :: "'a::comm_ring_1 list \<Rightarrow> 'a list \<Rightarrow> 'a list \<times> 'a list" where | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2246 | "pseudo_divmod_list p q = | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2247 | (if q = [] then ([],p) else | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2248 | (let rq = rev q; | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2249 | (qu,re) = pseudo_divmod_main_list (hd rq) [] (rev p) rq (1 + length p - length q) in | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2250 | (qu,rev re)))" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2251 | |
| 63035 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2252 | definition pseudo_mod_list :: "'a::comm_ring_1 list \<Rightarrow> 'a list \<Rightarrow> 'a list" where | 
| 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2253 | "pseudo_mod_list p q = | 
| 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2254 | (if q = [] then p else | 
| 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2255 | (let rq = rev q; | 
| 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2256 | re = pseudo_mod_main_list (hd rq) (rev p) rq (1 + length p - length q) in | 
| 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2257 | (rev re)))" | 
| 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2258 | |
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2259 | lemma minus_zero_does_nothing: | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2260 | "(minus_poly_rev_list x (map (op * 0) y)) = (x :: 'a :: ring list)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2261 | by(induct x y rule: minus_poly_rev_list.induct, auto) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2262 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2263 | lemma length_minus_poly_rev_list[simp]: | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2264 | "length (minus_poly_rev_list xs ys) = length xs" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2265 | by(induct xs ys rule: minus_poly_rev_list.induct, auto) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2266 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2267 | lemma if_0_minus_poly_rev_list: | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2268 | "(if a = 0 then x else minus_poly_rev_list x (map (op * a) y)) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2269 | = minus_poly_rev_list x (map (op * (a :: 'a :: ring)) y)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2270 | by(cases "a=0",simp_all add:minus_zero_does_nothing) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2271 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2272 | lemma Poly_append: | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2273 | "Poly ((a::'a::comm_semiring_1 list) @ b) = Poly a + monom 1 (length a) * Poly b" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2274 | by (induct a,auto simp: monom_0 monom_Suc) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2275 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2276 | lemma minus_poly_rev_list: "length p \<ge> length (q :: 'a :: comm_ring_1 list) \<Longrightarrow> | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2277 | Poly (rev (minus_poly_rev_list (rev p) (rev q))) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2278 | = Poly p - monom 1 (length p - length q) * Poly q" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2279 | proof (induct "rev p" "rev q" arbitrary: p q rule: minus_poly_rev_list.induct) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2280 | case (1 x xs y ys) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2281 | have "length (rev q) \<le> length (rev p)" using 1 by simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2282 | from this[folded 1(2,3)] have ys_xs:"length ys \<le> length xs" by simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2283 | hence a:"Poly (rev (minus_poly_rev_list xs ys)) = | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2284 | Poly (rev xs) - monom 1 (length xs - length ys) * Poly (rev ys)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2285 | by(subst "1.hyps"(1)[of "rev xs" "rev ys", unfolded rev_rev_ident length_rev],auto) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2286 | have "Poly p - monom 1 (length p - length q) * Poly q | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2287 | = Poly (rev (rev p)) - monom 1 (length (rev (rev p)) - length (rev (rev q))) * Poly (rev (rev q))" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2288 | by simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2289 | also have "\<dots> = Poly (rev (x # xs)) - monom 1 (length (x # xs) - length (y # ys)) * Poly (rev (y # ys))" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2290 | unfolding 1(2,3) by simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2291 | also have "\<dots> = Poly (rev xs) + monom x (length xs) - | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2292 | (monom 1 (length xs - length ys) * Poly (rev ys) + monom y (length xs))" using ys_xs | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2293 | by (simp add:Poly_append distrib_left mult_monom smult_monom) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2294 | also have "\<dots> = Poly (rev (minus_poly_rev_list xs ys)) + monom (x - y) (length xs)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2295 | unfolding a diff_monom[symmetric] by(simp) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2296 | finally show ?case | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2297 | unfolding 1(2,3)[symmetric] by (simp add: smult_monom Poly_append) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2298 | qed auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2299 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2300 | lemma smult_monom_mult: "smult a (monom b n * f) = monom (a * b) n * f" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2301 | using smult_monom [of a _ n] by (metis mult_smult_left) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2302 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2303 | lemma head_minus_poly_rev_list: | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2304 | "length d \<le> length r \<Longrightarrow> d\<noteq>[] \<Longrightarrow> | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2305 | hd (minus_poly_rev_list (map (op * (last d :: 'a :: comm_ring)) r) (map (op * (hd r)) (rev d))) = 0" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2306 | proof(induct r) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2307 | case (Cons a rs) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2308 | thus ?case by(cases "rev d", simp_all add:ac_simps) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2309 | qed simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2310 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2311 | lemma Poly_map: "Poly (map (op * a) p) = smult a (Poly p)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2312 | proof (induct p) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2313 | case(Cons x xs) thus ?case by (cases "Poly xs = 0",auto) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2314 | qed simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2315 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2316 | lemma last_coeff_is_hd: "xs \<noteq> [] \<Longrightarrow> coeff (Poly xs) (length xs - 1) = hd (rev xs)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2317 | by (simp_all add: hd_conv_nth rev_nth nth_default_nth nth_append) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2318 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2319 | lemma pseudo_divmod_main_list_invar : | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2320 | assumes leading_nonzero:"last d \<noteq> 0" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2321 | and lc:"last d = lc" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2322 | and dNonempty:"d \<noteq> []" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2323 | and "pseudo_divmod_main_list lc q (rev r) (rev d) n = (q',rev r')" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2324 | and "n = (1 + length r - length d)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2325 | shows | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2326 | "pseudo_divmod_main lc (monom 1 n * Poly q) (Poly r) (Poly d) (length r - 1) n = | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2327 | (Poly q', Poly r')" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2328 | using assms(4-) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2329 | proof(induct "n" arbitrary: r q) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2330 | case (Suc n r q) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2331 | have ifCond: "\<not> Suc (length r) \<le> length d" using Suc.prems by simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2332 | have rNonempty:"r \<noteq> []" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2333 | using ifCond dNonempty using Suc_leI length_greater_0_conv list.size(3) by fastforce | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2334 | let ?a = "(hd (rev r))" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2335 | let ?rr = "map (op * lc) (rev r)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2336 | let ?rrr = "rev (tl (minus_poly_rev_list ?rr (map (op * ?a) (rev d))))" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2337 | let ?qq = "cCons ?a (map (op * lc) q)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2338 | have n: "n = (1 + length r - length d - 1)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2339 | using ifCond Suc(3) by simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2340 | have rr_val:"(length ?rrr) = (length r - 1)" using ifCond by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2341 | hence rr_smaller: "(1 + length r - length d - 1) = (1 + length ?rrr - length d)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2342 | using rNonempty ifCond unfolding One_nat_def by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2343 | from ifCond have id: "Suc (length r) - length d = Suc (length r - length d)" by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2344 | have "pseudo_divmod_main_list lc ?qq (rev ?rrr) (rev d) (1 + length r - length d - 1) = (q', rev r')" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2345 | using Suc.prems ifCond by (simp add:Let_def if_0_minus_poly_rev_list id) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2346 | hence v:"pseudo_divmod_main_list lc ?qq (rev ?rrr) (rev d) n = (q', rev r')" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2347 | using n by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2348 | have sucrr:"Suc (length r) - length d = Suc (length r - length d)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2349 | using Suc_diff_le ifCond not_less_eq_eq by blast | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2350 | have n_ok : "n = 1 + (length ?rrr) - length d" using Suc(3) rNonempty by simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2351 | have cong: "\<And> x1 x2 x3 x4 y1 y2 y3 y4. x1 = y1 \<Longrightarrow> x2 = y2 \<Longrightarrow> x3 = y3 \<Longrightarrow> x4 = y4 \<Longrightarrow> | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2352 | pseudo_divmod_main lc x1 x2 x3 x4 n = pseudo_divmod_main lc y1 y2 y3 y4 n" by simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2353 | have hd_rev:"coeff (Poly r) (length r - Suc 0) = hd (rev r)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2354 | using last_coeff_is_hd[OF rNonempty] by simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2355 | show ?case unfolding Suc.hyps(1)[OF v n_ok, symmetric] pseudo_divmod_main.simps Let_def | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2356 | proof (rule cong[OF _ _ refl], goal_cases) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2357 | case 1 | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2358 | show ?case unfolding monom_Suc hd_rev[symmetric] | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2359 | by (simp add: smult_monom Poly_map) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2360 | next | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2361 | case 2 | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2362 | show ?case | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2363 | proof (subst Poly_on_rev_starting_with_0, goal_cases) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2364 | show "hd (minus_poly_rev_list (map (op * lc) (rev r)) (map (op * (hd (rev r))) (rev d))) = 0" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2365 | by (fold lc, subst head_minus_poly_rev_list, insert ifCond dNonempty,auto) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2366 | from ifCond have "length d \<le> length r" by simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2367 | then show "smult lc (Poly r) - monom (coeff (Poly r) (length r - 1)) n * Poly d = | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2368 | Poly (rev (minus_poly_rev_list (map (op * lc) (rev r)) (map (op * (hd (rev r))) (rev d))))" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2369 | by (fold rev_map) (auto simp add: n smult_monom_mult Poly_map hd_rev [symmetric] | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2370 | minus_poly_rev_list) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2371 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2372 | qed simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2373 | qed simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2374 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2375 | lemma pseudo_divmod_impl[code]: "pseudo_divmod (f::'a::comm_ring_1 poly) g = | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2376 | map_prod poly_of_list poly_of_list (pseudo_divmod_list (coeffs f) (coeffs g))" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2377 | proof (cases "g=0") | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2378 | case False | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2379 | hence coeffs_g_nonempty:"(coeffs g) \<noteq> []" by simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2380 | hence lastcoeffs:"last (coeffs g) = coeff g (degree g)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2381 | by (simp add: hd_rev last_coeffs_eq_coeff_degree not_0_coeffs_not_Nil) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2382 | obtain q r where qr: "pseudo_divmod_main_list | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2383 | (last (coeffs g)) (rev []) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2384 | (rev (coeffs f)) (rev (coeffs g)) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2385 | (1 + length (coeffs f) - | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2386 | length (coeffs g)) = (q,rev (rev r))" by force | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2387 | then have qr': "pseudo_divmod_main_list | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2388 | (hd (rev (coeffs g))) [] | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2389 | (rev (coeffs f)) (rev (coeffs g)) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2390 | (1 + length (coeffs f) - | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2391 | length (coeffs g)) = (q,r)" using hd_rev[OF coeffs_g_nonempty] by(auto) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2392 | from False have cg: "(coeffs g = []) = False" by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2393 | have last_non0:"last (coeffs g) \<noteq> 0" using False by (simp add:last_coeffs_not_0) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2394 | show ?thesis | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2395 | unfolding pseudo_divmod_def pseudo_divmod_list_def Let_def qr' map_prod_def split cg if_False | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2396 | pseudo_divmod_main_list_invar[OF last_non0 _ _ qr,unfolded lastcoeffs,simplified,symmetric,OF False] | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2397 | poly_of_list_def | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2398 | using False by (auto simp: degree_eq_length_coeffs) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2399 | next | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2400 | case True | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2401 | show ?thesis unfolding True unfolding pseudo_divmod_def pseudo_divmod_list_def | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2402 | by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2403 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2404 | |
| 63035 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2405 | lemma pseudo_mod_main_list: "snd (pseudo_divmod_main_list l q | 
| 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2406 | xs ys n) = pseudo_mod_main_list l xs ys n" | 
| 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2407 | by (induct n arbitrary: l q xs ys, auto simp: Let_def) | 
| 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2408 | |
| 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2409 | lemma pseudo_mod_impl[code]: "pseudo_mod f g = | 
| 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2410 | poly_of_list (pseudo_mod_list (coeffs f) (coeffs g))" | 
| 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2411 | proof - | 
| 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2412 | have snd_case: "\<And> f g p. snd ((\<lambda> (x,y). (f x, g y)) p) = g (snd p)" | 
| 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2413 | by auto | 
| 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2414 | show ?thesis | 
| 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2415 | unfolding pseudo_mod_def pseudo_divmod_impl pseudo_divmod_list_def | 
| 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2416 | pseudo_mod_list_def Let_def | 
| 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2417 | by (simp add: snd_case pseudo_mod_main_list) | 
| 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2418 | qed | 
| 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2419 | |
| 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2420 | |
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2421 | (* *************** *) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2422 | subsubsection \<open>Improved Code-Equations for Polynomial (Pseudo) Division\<close> | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2423 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2424 | lemma pdivmod_pdivmodrel: "pdivmod_rel p q r s = (pdivmod p q = (r, s))" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2425 | by (metis pdivmod_def pdivmod_rel pdivmod_rel_unique prod.sel) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2426 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2427 | lemma pdivmod_via_pseudo_divmod: "pdivmod f g = (if g = 0 then (0,f) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2428 | else let | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2429 | ilc = inverse (coeff g (degree g)); | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2430 | h = smult ilc g; | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2431 | (q,r) = pseudo_divmod f h | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2432 | in (smult ilc q, r))" (is "?l = ?r") | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2433 | proof (cases "g = 0") | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2434 | case False | 
| 63040 | 2435 | define lc where "lc = inverse (coeff g (degree g))" | 
| 2436 | define h where "h = smult lc g" | |
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2437 | from False have h1: "coeff h (degree h) = 1" and lc: "lc \<noteq> 0" unfolding h_def lc_def by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2438 | hence h0: "h \<noteq> 0" by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2439 | obtain q r where p: "pseudo_divmod f h = (q,r)" by force | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2440 | from False have id: "?r = (smult lc q, r)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2441 | unfolding Let_def h_def[symmetric] lc_def[symmetric] p by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2442 | from pseudo_divmod[OF h0 p, unfolded h1] | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2443 | have f: "f = h * q + r" and r: "r = 0 \<or> degree r < degree h" by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2444 | have "pdivmod_rel f h q r" unfolding pdivmod_rel_def using f r h0 by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2445 | hence "pdivmod f h = (q,r)" by (simp add: pdivmod_pdivmodrel) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2446 | hence "pdivmod f g = (smult lc q, r)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2447 | unfolding pdivmod_def h_def div_smult_right[OF lc] mod_smult_right[OF lc] | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2448 | using lc by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2449 | with id show ?thesis by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2450 | qed (auto simp: pdivmod_def) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2451 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2452 | lemma pdivmod_via_pseudo_divmod_list: "pdivmod f g = (let | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2453 | cg = coeffs g | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2454 | in if cg = [] then (0,f) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2455 | else let | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2456 | cf = coeffs f; | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2457 | ilc = inverse (last cg); | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2458 | ch = map (op * ilc) cg; | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2459 | (q,r) = pseudo_divmod_main_list 1 [] (rev cf) (rev ch) (1 + length cf - length cg) | 
| 63035 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2460 | in (poly_of_list (map (op * ilc) q), poly_of_list (rev r)))" | 
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2461 | proof - | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2462 | note d = pdivmod_via_pseudo_divmod | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2463 | pseudo_divmod_impl pseudo_divmod_list_def | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2464 | show ?thesis | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2465 | proof (cases "g = 0") | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2466 | case True thus ?thesis unfolding d by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2467 | next | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2468 | case False | 
| 63040 | 2469 | define ilc where "ilc = inverse (coeff g (degree g))" | 
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2470 | from False have ilc: "ilc \<noteq> 0" unfolding ilc_def by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2471 | with False have id: "(g = 0) = False" "(coeffs g = []) = False" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2472 | "last (coeffs g) = coeff g (degree g)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2473 | "(coeffs (smult ilc g) = []) = False" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2474 | by (auto simp: last_coeffs_eq_coeff_degree) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2475 | have id2: "hd (rev (coeffs (smult ilc g))) = 1" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2476 | by (subst hd_rev, insert id ilc, auto simp: coeffs_smult, subst last_map, auto simp: id ilc_def) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2477 | have id3: "length (coeffs (smult ilc g)) = length (coeffs g)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2478 | "rev (coeffs (smult ilc g)) = rev (map (op * ilc) (coeffs g))" unfolding coeffs_smult using ilc by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2479 | obtain q r where pair: "pseudo_divmod_main_list 1 [] (rev (coeffs f)) (rev (map (op * ilc) (coeffs g))) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2480 | (1 + length (coeffs f) - length (coeffs g)) = (q,r)" by force | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2481 | show ?thesis unfolding d Let_def id if_False ilc_def[symmetric] map_prod_def[symmetric] id2 | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2482 | unfolding id3 pair map_prod_def split by (auto simp: Poly_map) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2483 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2484 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2485 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2486 | lemma pseudo_divmod_main_list_1: "pseudo_divmod_main_list 1 = divmod_poly_one_main_list" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2487 | proof (intro ext, goal_cases) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2488 | case (1 q r d n) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2489 |   {
 | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2490 | fix xs :: "'a list" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2491 | have "map (op * 1) xs = xs" by (induct xs, auto) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2492 | } note [simp] = this | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2493 | show ?case by (induct n arbitrary: q r d, auto simp: Let_def) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2494 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2495 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2496 | fun divide_poly_main_list :: "'a::idom_divide \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> 'a list | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2497 | \<Rightarrow> nat \<Rightarrow> 'a list" where | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2498 | "divide_poly_main_list lc q r d (Suc n) = (let | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2499 | cr = hd r | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2500 | in if cr = 0 then divide_poly_main_list lc (cCons cr q) (tl r) d n else let | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2501 | a = cr div lc; | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2502 | qq = cCons a q; | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2503 | rr = minus_poly_rev_list r (map (op * a) d) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2504 | in if hd rr = 0 then divide_poly_main_list lc qq (tl rr) d n else [])" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2505 | | "divide_poly_main_list lc q r d 0 = q" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2506 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2507 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2508 | lemma divide_poly_main_list_simp[simp]: "divide_poly_main_list lc q r d (Suc n) = (let | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2509 | cr = hd r; | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2510 | a = cr div lc; | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2511 | qq = cCons a q; | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2512 | rr = minus_poly_rev_list r (map (op * a) d) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2513 | in if hd rr = 0 then divide_poly_main_list lc qq (tl rr) d n else [])" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2514 | by (simp add: Let_def minus_zero_does_nothing) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2515 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2516 | declare divide_poly_main_list.simps(1)[simp del] | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2517 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2518 | definition divide_poly_list :: "'a::idom_divide poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2519 | "divide_poly_list f g = | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2520 | (let cg = coeffs g | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2521 | in if cg = [] then g | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2522 | else let cf = coeffs f; cgr = rev cg | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2523 | in poly_of_list (divide_poly_main_list (hd cgr) [] (rev cf) cgr (1 + length cf - length cg)))" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2524 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2525 | lemmas pdivmod_via_divmod_list[code] = pdivmod_via_pseudo_divmod_list[unfolded pseudo_divmod_main_list_1] | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2526 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2527 | lemma mod_poly_one_main_list: "snd (divmod_poly_one_main_list q r d n) = mod_poly_one_main_list r d n" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2528 | by (induct n arbitrary: q r d, auto simp: Let_def) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2529 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2530 | lemma mod_poly_code[code]: "f mod g = | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2531 | (let cg = coeffs g | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2532 | in if cg = [] then f | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2533 | else let cf = coeffs f; ilc = inverse (last cg); ch = map (op * ilc) cg; | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2534 | r = mod_poly_one_main_list (rev cf) (rev ch) (1 + length cf - length cg) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2535 | in poly_of_list (rev r))" (is "?l = ?r") | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2536 | proof - | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2537 | have "?l = snd (pdivmod f g)" unfolding pdivmod_def by simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2538 | also have "\<dots> = ?r" unfolding pdivmod_via_divmod_list Let_def | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2539 | mod_poly_one_main_list[symmetric, of _ _ _ Nil] by (auto split: prod.splits) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2540 | finally show ?thesis . | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2541 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2542 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2543 | definition div_field_poly_impl :: "'a :: field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2544 | "div_field_poly_impl f g = ( | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2545 | let cg = coeffs g | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2546 | in if cg = [] then 0 | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2547 | else let cf = coeffs f; ilc = inverse (last cg); ch = map (op * ilc) cg; | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2548 | q = fst (divmod_poly_one_main_list [] (rev cf) (rev ch) (1 + length cf - length cg)) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2549 | in poly_of_list ((map (op * ilc) q)))" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2550 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2551 | text \<open>We do not declare the following lemma as code equation, since then polynomial division | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2552 | on non-fields will no longer be executable. However, a code-unfold is possible, since | 
| 63034 | 2553 | \<open>div_field_poly_impl\<close> is a bit more efficient than the generic polynomial division.\<close> | 
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2554 | lemma div_field_poly_impl[code_unfold]: "op div = div_field_poly_impl" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2555 | proof (intro ext) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2556 | fix f g :: "'a poly" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2557 | have "f div g = fst (pdivmod f g)" unfolding pdivmod_def by simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2558 | also have "\<dots> = div_field_poly_impl f g" unfolding | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2559 | div_field_poly_impl_def pdivmod_via_divmod_list Let_def by (auto split: prod.splits) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2560 | finally show "f div g = div_field_poly_impl f g" . | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2561 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2562 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2563 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2564 | lemma divide_poly_main_list: | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2565 | assumes lc0: "lc \<noteq> 0" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2566 | and lc:"last d = lc" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2567 | and d:"d \<noteq> []" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2568 | and "n = (1 + length r - length d)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2569 | shows | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2570 | "Poly (divide_poly_main_list lc q (rev r) (rev d) n) = | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2571 | divide_poly_main lc (monom 1 n * Poly q) (Poly r) (Poly d) (length r - 1) n" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2572 | using assms(4-) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2573 | proof(induct "n" arbitrary: r q) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2574 | case (Suc n r q) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2575 | have ifCond: "\<not> Suc (length r) \<le> length d" using Suc.prems by simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2576 | have r: "r \<noteq> []" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2577 | using ifCond d using Suc_leI length_greater_0_conv list.size(3) by fastforce | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2578 | then obtain rr lcr where r: "r = rr @ [lcr]" by (cases r rule: rev_cases, auto) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2579 | from d lc obtain dd where d: "d = dd @ [lc]" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2580 | by (cases d rule: rev_cases, auto) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2581 | from Suc(2) ifCond have n: "n = 1 + length rr - length d" by (auto simp: r) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2582 | from ifCond have len: "length dd \<le> length rr" by (simp add: r d) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2583 | show ?case | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2584 | proof (cases "lcr div lc * lc = lcr") | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2585 | case False | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2586 | thus ?thesis unfolding Suc(2)[symmetric] using r d | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2587 | by (auto simp add: Let_def nth_default_append) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2588 | next | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2589 | case True | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2590 | hence id: | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2591 | "?thesis = (Poly (divide_poly_main_list lc (cCons (lcr div lc) q) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2592 | (rev (rev (minus_poly_rev_list (rev rr) (rev (map (op * (lcr div lc)) dd))))) (rev d) n) = | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2593 | divide_poly_main lc | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2594 | (monom 1 (Suc n) * Poly q + monom (lcr div lc) n) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2595 | (Poly r - monom (lcr div lc) n * Poly d) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2596 | (Poly d) (length rr - 1) n)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2597 | using r d | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2598 | by (cases r rule: rev_cases; cases "d" rule: rev_cases; | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2599 | auto simp add: Let_def rev_map nth_default_append) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2600 | have cong: "\<And> x1 x2 x3 x4 y1 y2 y3 y4. x1 = y1 \<Longrightarrow> x2 = y2 \<Longrightarrow> x3 = y3 \<Longrightarrow> x4 = y4 \<Longrightarrow> | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2601 | divide_poly_main lc x1 x2 x3 x4 n = divide_poly_main lc y1 y2 y3 y4 n" by simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2602 | show ?thesis unfolding id | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2603 | proof (subst Suc(1), simp add: n, | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2604 | subst minus_poly_rev_list, force simp: len, rule cong[OF _ _ refl], goal_cases) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2605 | case 2 | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2606 | have "monom lcr (length rr) = monom (lcr div lc) (length rr - length dd) * monom lc (length dd)" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2607 | by (simp add: mult_monom len True) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2608 | thus ?case unfolding r d Poly_append n ring_distribs | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2609 | by (auto simp: Poly_map smult_monom smult_monom_mult) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2610 | qed (auto simp: len monom_Suc smult_monom) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2611 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2612 | qed simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2613 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2614 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2615 | lemma divide_poly_list[code]: "f div g = divide_poly_list f g" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2616 | proof - | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2617 | note d = divide_poly_def divide_poly_list_def | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2618 | show ?thesis | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2619 | proof (cases "g = 0") | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2620 | case True | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2621 | show ?thesis unfolding d True by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2622 | next | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2623 | case False | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2624 | then obtain cg lcg where cg: "coeffs g = cg @ [lcg]" by (cases "coeffs g" rule: rev_cases, auto) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2625 | with False have id: "(g = 0) = False" "(cg @ [lcg] = []) = False" by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2626 | from cg False have lcg: "coeff g (degree g) = lcg" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2627 | using last_coeffs_eq_coeff_degree last_snoc by force | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2628 | with False have lcg0: "lcg \<noteq> 0" by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2629 | from cg have ltp: "Poly (cg @ [lcg]) = g" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2630 | using Poly_coeffs [of g] by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2631 | show ?thesis unfolding d cg Let_def id if_False poly_of_list_def | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2632 | by (subst divide_poly_main_list, insert False cg lcg0, auto simp: lcg ltp, | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2633 | simp add: degree_eq_length_coeffs) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2634 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2635 | qed | 
| 29980 | 2636 | |
| 60500 | 2637 | subsection \<open>Order of polynomial roots\<close> | 
| 29977 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2638 | |
| 52380 | 2639 | definition order :: "'a::idom \<Rightarrow> 'a poly \<Rightarrow> nat" | 
| 29977 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2640 | where | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2641 | "order a p = (LEAST n. \<not> [:-a, 1:] ^ Suc n dvd p)" | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2642 | |
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2643 | lemma coeff_linear_power: | 
| 29979 | 2644 | fixes a :: "'a::comm_semiring_1" | 
| 29977 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2645 | shows "coeff ([:a, 1:] ^ n) n = 1" | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2646 | apply (induct n, simp_all) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2647 | apply (subst coeff_eq_0) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2648 | apply (auto intro: le_less_trans degree_power_le) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2649 | done | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2650 | |
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2651 | lemma degree_linear_power: | 
| 29979 | 2652 | fixes a :: "'a::comm_semiring_1" | 
| 29977 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2653 | shows "degree ([:a, 1:] ^ n) = n" | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2654 | apply (rule order_antisym) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2655 | apply (rule ord_le_eq_trans [OF degree_power_le], simp) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2656 | apply (rule le_degree, simp add: coeff_linear_power) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2657 | done | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2658 | |
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2659 | lemma order_1: "[:-a, 1:] ^ order a p dvd p" | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2660 | apply (cases "p = 0", simp) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2661 | apply (cases "order a p", simp) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2662 | apply (subgoal_tac "nat < (LEAST n. \<not> [:-a, 1:] ^ Suc n dvd p)") | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2663 | apply (drule not_less_Least, simp) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2664 | apply (fold order_def, simp) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2665 | done | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2666 | |
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2667 | lemma order_2: "p \<noteq> 0 \<Longrightarrow> \<not> [:-a, 1:] ^ Suc (order a p) dvd p" | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2668 | unfolding order_def | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2669 | apply (rule LeastI_ex) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2670 | apply (rule_tac x="degree p" in exI) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2671 | apply (rule notI) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2672 | apply (drule (1) dvd_imp_degree_le) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2673 | apply (simp only: degree_linear_power) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2674 | done | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2675 | |
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2676 | lemma order: | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2677 | "p \<noteq> 0 \<Longrightarrow> [:-a, 1:] ^ order a p dvd p \<and> \<not> [:-a, 1:] ^ Suc (order a p) dvd p" | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2678 | by (rule conjI [OF order_1 order_2]) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2679 | |
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2680 | lemma order_degree: | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2681 | assumes p: "p \<noteq> 0" | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2682 | shows "order a p \<le> degree p" | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2683 | proof - | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2684 | 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 | 2685 | by (simp only: degree_linear_power) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2686 | also have "\<dots> \<le> degree p" | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2687 | using order_1 p by (rule dvd_imp_degree_le) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2688 | finally show ?thesis . | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2689 | qed | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2690 | |
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2691 | lemma order_root: "poly p a = 0 \<longleftrightarrow> p = 0 \<or> order a p \<noteq> 0" | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2692 | apply (cases "p = 0", simp_all) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2693 | apply (rule iffI) | 
| 56383 | 2694 | apply (metis order_2 not_gr0 poly_eq_0_iff_dvd power_0 power_Suc_0 power_one_right) | 
| 2695 | unfolding poly_eq_0_iff_dvd | |
| 2696 | apply (metis dvd_power dvd_trans order_1) | |
| 29977 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2697 | done | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2698 | |
| 62065 | 2699 | lemma order_0I: "poly p a \<noteq> 0 \<Longrightarrow> order a p = 0" | 
| 2700 | by (subst (asm) order_root) auto | |
| 2701 | ||
| 29977 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2702 | |
| 62065 | 2703 | subsection \<open>Additional induction rules on polynomials\<close> | 
| 2704 | ||
| 2705 | text \<open> | |
| 2706 | An induction rule for induction over the roots of a polynomial with a certain property. | |
| 2707 | (e.g. all positive roots) | |
| 2708 | \<close> | |
| 2709 | lemma poly_root_induct [case_names 0 no_roots root]: | |
| 2710 | fixes p :: "'a :: idom poly" | |
| 2711 | assumes "Q 0" | |
| 2712 | assumes "\<And>p. (\<And>a. P a \<Longrightarrow> poly p a \<noteq> 0) \<Longrightarrow> Q p" | |
| 2713 | assumes "\<And>a p. P a \<Longrightarrow> Q p \<Longrightarrow> Q ([:a, -1:] * p)" | |
| 2714 | shows "Q p" | |
| 2715 | proof (induction "degree p" arbitrary: p rule: less_induct) | |
| 2716 | case (less p) | |
| 2717 | show ?case | |
| 2718 | proof (cases "p = 0") | |
| 2719 | assume nz: "p \<noteq> 0" | |
| 2720 | show ?case | |
| 2721 | proof (cases "\<exists>a. P a \<and> poly p a = 0") | |
| 2722 | case False | |
| 2723 | thus ?thesis by (intro assms(2)) blast | |
| 2724 | next | |
| 2725 | case True | |
| 2726 | then obtain a where a: "P a" "poly p a = 0" | |
| 2727 | by blast | |
| 2728 | hence "-[:-a, 1:] dvd p" | |
| 2729 | by (subst minus_dvd_iff) (simp add: poly_eq_0_iff_dvd) | |
| 2730 | then obtain q where q: "p = [:a, -1:] * q" by (elim dvdE) simp | |
| 2731 | with nz have q_nz: "q \<noteq> 0" by auto | |
| 2732 | have "degree p = Suc (degree q)" | |
| 2733 | by (subst q, subst degree_mult_eq) (simp_all add: q_nz) | |
| 2734 | hence "Q q" by (intro less) simp | |
| 2735 | from a(1) and this have "Q ([:a, -1:] * q)" | |
| 2736 | by (rule assms(3)) | |
| 2737 | with q show ?thesis by simp | |
| 2738 | qed | |
| 2739 | qed (simp add: assms(1)) | |
| 2740 | qed | |
| 2741 | ||
| 2742 | lemma dropWhile_replicate_append: | |
| 2743 | "dropWhile (op= a) (replicate n a @ ys) = dropWhile (op= a) ys" | |
| 2744 | by (induction n) simp_all | |
| 2745 | ||
| 2746 | lemma Poly_append_replicate_0: "Poly (xs @ replicate n 0) = Poly xs" | |
| 2747 | by (subst coeffs_eq_iff) (simp_all add: strip_while_def dropWhile_replicate_append) | |
| 2748 | ||
| 2749 | text \<open> | |
| 2750 | An induction rule for simultaneous induction over two polynomials, | |
| 2751 | prepending one coefficient in each step. | |
| 2752 | \<close> | |
| 2753 | lemma poly_induct2 [case_names 0 pCons]: | |
| 2754 | assumes "P 0 0" "\<And>a p b q. P p q \<Longrightarrow> P (pCons a p) (pCons b q)" | |
| 2755 | shows "P p q" | |
| 2756 | proof - | |
| 63040 | 2757 | define n where "n = max (length (coeffs p)) (length (coeffs q))" | 
| 2758 | define xs where "xs = coeffs p @ (replicate (n - length (coeffs p)) 0)" | |
| 2759 | define ys where "ys = coeffs q @ (replicate (n - length (coeffs q)) 0)" | |
| 62065 | 2760 | have "length xs = length ys" | 
| 2761 | by (simp add: xs_def ys_def n_def) | |
| 2762 | hence "P (Poly xs) (Poly ys)" | |
| 2763 | by (induction rule: list_induct2) (simp_all add: assms) | |
| 2764 | also have "Poly xs = p" | |
| 2765 | by (simp add: xs_def Poly_append_replicate_0) | |
| 2766 | also have "Poly ys = q" | |
| 2767 | by (simp add: ys_def Poly_append_replicate_0) | |
| 2768 | finally show ?thesis . | |
| 2769 | qed | |
| 2770 | ||
| 2771 | ||
| 60500 | 2772 | subsection \<open>Composition of polynomials\<close> | 
| 29478 | 2773 | |
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2774 | (* 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 | 2775 | |
| 52380 | 2776 | definition pcompose :: "'a::comm_semiring_0 poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" | 
| 2777 | where | |
| 2778 | "pcompose p q = fold_coeffs (\<lambda>a c. [:a:] + q * c) p 0" | |
| 2779 | ||
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2780 | 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 | 2781 | |
| 52380 | 2782 | lemma pcompose_0 [simp]: | 
| 2783 | "pcompose 0 q = 0" | |
| 2784 | by (simp add: pcompose_def) | |
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2785 | |
| 52380 | 2786 | lemma pcompose_pCons: | 
| 2787 | "pcompose (pCons a p) q = [:a:] + q * pcompose p q" | |
| 2788 | by (cases "p = 0 \<and> a = 0") (auto simp add: pcompose_def) | |
| 2789 | ||
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2790 | lemma pcompose_1: | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2791 | fixes p :: "'a :: comm_semiring_1 poly" | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2792 | shows "pcompose 1 p = 1" | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2793 | unfolding one_poly_def by (auto simp: pcompose_pCons) | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2794 | |
| 52380 | 2795 | lemma poly_pcompose: | 
| 2796 | "poly (pcompose p q) x = poly p (poly q x)" | |
| 2797 | by (induct p) (simp_all add: pcompose_pCons) | |
| 2798 | ||
| 2799 | lemma degree_pcompose_le: | |
| 2800 | "degree (pcompose p q) \<le> degree p * degree q" | |
| 2801 | apply (induct p, simp) | |
| 2802 | apply (simp add: pcompose_pCons, clarify) | |
| 2803 | apply (rule degree_add_le, simp) | |
| 2804 | apply (rule order_trans [OF degree_mult_le], simp) | |
| 29478 | 2805 | done | 
| 2806 | ||
| 62065 | 2807 | lemma pcompose_add: | 
| 2808 |   fixes p q r :: "'a :: {comm_semiring_0, ab_semigroup_add} poly"
 | |
| 2809 | shows "pcompose (p + q) r = pcompose p r + pcompose q r" | |
| 2810 | proof (induction p q rule: poly_induct2) | |
| 2811 | case (pCons a p b q) | |
| 2812 | have "pcompose (pCons a p + pCons b q) r = | |
| 2813 | [:a + b:] + r * pcompose p r + r * pcompose q r" | |
| 2814 | by (simp_all add: pcompose_pCons pCons.IH algebra_simps) | |
| 2815 | also have "[:a + b:] = [:a:] + [:b:]" by simp | |
| 2816 | also have "\<dots> + r * pcompose p r + r * pcompose q r = | |
| 2817 | pcompose (pCons a p) r + pcompose (pCons b q) r" | |
| 2818 | by (simp only: pcompose_pCons add_ac) | |
| 2819 | finally show ?case . | |
| 2820 | qed simp | |
| 2821 | ||
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2822 | lemma pcompose_uminus: | 
| 62065 | 2823 | fixes p r :: "'a :: comm_ring poly" | 
| 2824 | shows "pcompose (-p) r = -pcompose p r" | |
| 2825 | by (induction p) (simp_all add: pcompose_pCons) | |
| 2826 | ||
| 2827 | lemma pcompose_diff: | |
| 2828 | fixes p q r :: "'a :: comm_ring poly" | |
| 2829 | shows "pcompose (p - q) r = pcompose p r - pcompose q r" | |
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2830 | using pcompose_add[of p "-q"] by (simp add: pcompose_uminus) | 
| 62065 | 2831 | |
| 2832 | lemma pcompose_smult: | |
| 2833 | fixes p r :: "'a :: comm_semiring_0 poly" | |
| 2834 | shows "pcompose (smult a p) r = smult a (pcompose p r)" | |
| 2835 | by (induction p) | |
| 2836 | (simp_all add: pcompose_pCons pcompose_add smult_add_right) | |
| 2837 | ||
| 2838 | lemma pcompose_mult: | |
| 2839 | fixes p q r :: "'a :: comm_semiring_0 poly" | |
| 2840 | shows "pcompose (p * q) r = pcompose p r * pcompose q r" | |
| 2841 | by (induction p arbitrary: q) | |
| 2842 | (simp_all add: pcompose_add pcompose_smult pcompose_pCons algebra_simps) | |
| 2843 | ||
| 2844 | lemma pcompose_assoc: | |
| 2845 | "pcompose p (pcompose q r :: 'a :: comm_semiring_0 poly ) = | |
| 2846 | pcompose (pcompose p q) r" | |
| 2847 | by (induction p arbitrary: q) | |
| 2848 | (simp_all add: pcompose_pCons pcompose_add pcompose_mult) | |
| 2849 | ||
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2850 | lemma pcompose_idR[simp]: | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2851 | fixes p :: "'a :: comm_semiring_1 poly" | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2852 | shows "pcompose p [: 0, 1 :] = p" | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2853 | by (induct p; simp add: pcompose_pCons) | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2854 | |
| 64267 | 2855 | lemma pcompose_sum: "pcompose (sum f A) p = sum (\<lambda>i. pcompose (f i) p) A" | 
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2856 | by (cases "finite A", induction rule: finite_induct) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2857 | (simp_all add: pcompose_1 pcompose_add) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2858 | |
| 64272 | 2859 | lemma pcompose_prod: "pcompose (prod f A) p = prod (\<lambda>i. pcompose (f i) p) A" | 
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2860 | by (cases "finite A", induction rule: finite_induct) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2861 | (simp_all add: pcompose_1 pcompose_mult) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2862 | |
| 62065 | 2863 | |
| 2864 | (* The remainder of this section and the next were contributed by Wenda Li *) | |
| 2865 | ||
| 2866 | lemma degree_mult_eq_0: | |
| 63498 | 2867 |   fixes p q:: "'a :: {comm_semiring_0,semiring_no_zero_divisors} poly"
 | 
| 62065 | 2868 | shows "degree (p*q) = 0 \<longleftrightarrow> p=0 \<or> q=0 \<or> (p\<noteq>0 \<and> q\<noteq>0 \<and> degree p =0 \<and> degree q =0)" | 
| 2869 | by (auto simp add:degree_mult_eq) | |
| 2870 | ||
| 2871 | lemma pcompose_const[simp]:"pcompose [:a:] q = [:a:]" by (subst pcompose_pCons,simp) | |
| 2872 | ||
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2873 | lemma pcompose_0': "pcompose p 0 = [:coeff p 0:]" | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2874 | by (induct p) (auto simp add:pcompose_pCons) | 
| 62065 | 2875 | |
| 2876 | lemma degree_pcompose: | |
| 63498 | 2877 |   fixes p q:: "'a::{comm_semiring_0,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 | 2878 | shows "degree (pcompose p q) = degree p * degree q" | 
| 62065 | 2879 | proof (induct p) | 
| 2880 | case 0 | |
| 2881 | thus ?case by auto | |
| 2882 | next | |
| 2883 | case (pCons a p) | |
| 2884 | have "degree (q * pcompose p q) = 0 \<Longrightarrow> ?case" | |
| 2885 | proof (cases "p=0") | |
| 2886 | case True | |
| 2887 | thus ?thesis by auto | |
| 2888 | next | |
| 2889 | case False assume "degree (q * pcompose p q) = 0" | |
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2890 | hence "degree q=0 \<or> pcompose p q=0" by (auto simp add: degree_mult_eq_0) | 
| 62072 | 2891 | moreover have "\<lbrakk>pcompose p q=0;degree q\<noteq>0\<rbrakk> \<Longrightarrow> False" using pCons.hyps(2) \<open>p\<noteq>0\<close> | 
| 62065 | 2892 | proof - | 
| 2893 | assume "pcompose p q=0" "degree q\<noteq>0" | |
| 2894 | hence "degree p=0" using pCons.hyps(2) by auto | |
| 2895 | then obtain a1 where "p=[:a1:]" | |
| 2896 | by (metis degree_pCons_eq_if old.nat.distinct(2) pCons_cases) | |
| 62072 | 2897 | thus False using \<open>pcompose p q=0\<close> \<open>p\<noteq>0\<close> by auto | 
| 62065 | 2898 | qed | 
| 2899 | ultimately have "degree (pCons a p) * degree q=0" by auto | |
| 2900 | moreover have "degree (pcompose (pCons a p) q) = 0" | |
| 2901 | proof - | |
| 2902 | have" 0 = max (degree [:a:]) (degree (q*pcompose p q))" | |
| 62072 | 2903 | using \<open>degree (q * pcompose p q) = 0\<close> by simp | 
| 62065 | 2904 | also have "... \<ge> degree ([:a:] + q * pcompose p q)" | 
| 2905 | by (rule degree_add_le_max) | |
| 2906 | finally show ?thesis by (auto simp add:pcompose_pCons) | |
| 2907 | qed | |
| 2908 | ultimately show ?thesis by simp | |
| 2909 | qed | |
| 2910 | moreover have "degree (q * pcompose p q)>0 \<Longrightarrow> ?case" | |
| 2911 | proof - | |
| 2912 | assume asm:"0 < degree (q * pcompose p q)" | |
| 2913 | hence "p\<noteq>0" "q\<noteq>0" "pcompose p q\<noteq>0" by auto | |
| 2914 | have "degree (pcompose (pCons a p) q) = degree ( q * pcompose p q)" | |
| 2915 | unfolding pcompose_pCons | |
| 2916 | using degree_add_eq_right[of "[:a:]" ] asm by auto | |
| 2917 | thus ?thesis | |
| 62072 | 2918 | using pCons.hyps(2) degree_mult_eq[OF \<open>q\<noteq>0\<close> \<open>pcompose p q\<noteq>0\<close>] by auto | 
| 62065 | 2919 | qed | 
| 2920 | ultimately show ?case by blast | |
| 2921 | qed | |
| 2922 | ||
| 2923 | lemma pcompose_eq_0: | |
| 63498 | 2924 |   fixes p q:: "'a :: {comm_semiring_0,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 | 2925 | assumes "pcompose p q = 0" "degree q > 0" | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2926 | shows "p = 0" | 
| 62065 | 2927 | proof - | 
| 2928 | have "degree p=0" using assms degree_pcompose[of p q] by auto | |
| 2929 | then obtain a where "p=[:a:]" | |
| 2930 | by (metis degree_pCons_eq_if gr0_conv_Suc neq0_conv pCons_cases) | |
| 2931 | hence "a=0" using assms(1) by auto | |
| 62072 | 2932 | thus ?thesis using \<open>p=[:a:]\<close> by simp | 
| 62065 | 2933 | qed | 
| 2934 | ||
| 2935 | ||
| 62072 | 2936 | subsection \<open>Leading coefficient\<close> | 
| 62065 | 2937 | |
| 2938 | definition lead_coeff:: "'a::zero poly \<Rightarrow> 'a" where | |
| 2939 | "lead_coeff p= coeff p (degree p)" | |
| 2940 | ||
| 2941 | lemma lead_coeff_pCons[simp]: | |
| 2942 | "p\<noteq>0 \<Longrightarrow>lead_coeff (pCons a p) = lead_coeff p" | |
| 2943 | "p=0 \<Longrightarrow> lead_coeff (pCons a p) = a" | |
| 2944 | unfolding lead_coeff_def by auto | |
| 2945 | ||
| 2946 | lemma lead_coeff_0[simp]:"lead_coeff 0 =0" | |
| 2947 | unfolding lead_coeff_def by auto | |
| 2948 | ||
| 63882 
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
 nipkow parents: 
63649diff
changeset | 2949 | lemma coeff_0_prod_list: "coeff (prod_list xs) 0 = prod_list (map (\<lambda>p. coeff p 0) xs)" | 
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2950 | by (induction xs) (simp_all add: coeff_mult) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2951 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2952 | lemma coeff_0_power: "coeff (p ^ n) 0 = coeff p 0 ^ n" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2953 | by (induction n) (simp_all add: coeff_mult) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 2954 | |
| 62065 | 2955 | lemma lead_coeff_mult: | 
| 63498 | 2956 |    fixes p q::"'a :: {comm_semiring_0,semiring_no_zero_divisors} poly"
 | 
| 62065 | 2957 | shows "lead_coeff (p * q) = lead_coeff p * lead_coeff q" | 
| 2958 | by (unfold lead_coeff_def,cases "p=0 \<or> q=0",auto simp add:coeff_mult_degree_sum degree_mult_eq) | |
| 2959 | ||
| 2960 | lemma lead_coeff_add_le: | |
| 2961 | assumes "degree p < degree q" | |
| 2962 | shows "lead_coeff (p+q) = lead_coeff q" | |
| 2963 | using assms unfolding lead_coeff_def | |
| 2964 | by (metis coeff_add coeff_eq_0 monoid_add_class.add.left_neutral degree_add_eq_right) | |
| 2965 | ||
| 2966 | lemma lead_coeff_minus: | |
| 2967 | "lead_coeff (-p) = - lead_coeff p" | |
| 2968 | by (metis coeff_minus degree_minus lead_coeff_def) | |
| 2969 | ||
| 63498 | 2970 | lemma lead_coeff_smult: | 
| 2971 |   "lead_coeff (smult c p :: 'a :: {comm_semiring_0,semiring_no_zero_divisors} poly) = c * lead_coeff p"
 | |
| 2972 | proof - | |
| 2973 | have "smult c p = [:c:] * p" by simp | |
| 2974 | also have "lead_coeff \<dots> = c * lead_coeff p" | |
| 2975 | by (subst lead_coeff_mult) simp_all | |
| 2976 | finally show ?thesis . | |
| 2977 | qed | |
| 2978 | ||
| 2979 | lemma lead_coeff_eq_zero_iff [simp]: "lead_coeff p = 0 \<longleftrightarrow> p = 0" | |
| 2980 | by (simp add: lead_coeff_def) | |
| 62065 | 2981 | |
| 2982 | lemma lead_coeff_comp: | |
| 63498 | 2983 |   fixes p q:: "'a::{comm_semiring_1,semiring_no_zero_divisors} poly"
 | 
| 62065 | 2984 | assumes "degree q > 0" | 
| 2985 | shows "lead_coeff (pcompose p q) = lead_coeff p * lead_coeff q ^ (degree p)" | |
| 2986 | proof (induct p) | |
| 2987 | case 0 | |
| 2988 | thus ?case unfolding lead_coeff_def by auto | |
| 2989 | next | |
| 2990 | case (pCons a p) | |
| 2991 | have "degree ( q * pcompose p q) = 0 \<Longrightarrow> ?case" | |
| 2992 | proof - | |
| 2993 | assume "degree ( q * pcompose p q) = 0" | |
| 2994 | hence "pcompose p q = 0" by (metis assms degree_0 degree_mult_eq_0 neq0_conv) | |
| 62072 | 2995 | hence "p=0" using pcompose_eq_0[OF _ \<open>degree q > 0\<close>] by simp | 
| 62065 | 2996 | thus ?thesis by auto | 
| 2997 | qed | |
| 2998 | moreover have "degree ( q * pcompose p q) > 0 \<Longrightarrow> ?case" | |
| 2999 | proof - | |
| 3000 | assume "degree ( q * pcompose p q) > 0" | |
| 3001 | hence "lead_coeff (pcompose (pCons a p) q) =lead_coeff ( q * pcompose p q)" | |
| 3002 | by (auto simp add:pcompose_pCons lead_coeff_add_le) | |
| 3003 | also have "... = lead_coeff q * (lead_coeff p * lead_coeff q ^ degree p)" | |
| 3004 | using pCons.hyps(2) lead_coeff_mult[of q "pcompose p q"] by simp | |
| 3005 | also have "... = lead_coeff p * lead_coeff q ^ (degree p + 1)" | |
| 63498 | 3006 | by (auto simp: mult_ac) | 
| 62065 | 3007 | finally show ?thesis by auto | 
| 3008 | qed | |
| 3009 | ultimately show ?case by blast | |
| 3010 | qed | |
| 3011 | ||
| 3012 | lemma lead_coeff_1 [simp]: "lead_coeff 1 = 1" | |
| 3013 | by (simp add: lead_coeff_def) | |
| 3014 | ||
| 3015 | lemma lead_coeff_of_nat [simp]: | |
| 3016 |   "lead_coeff (of_nat n) = (of_nat n :: 'a :: {comm_semiring_1,semiring_char_0})"
 | |
| 3017 | by (induction n) (simp_all add: lead_coeff_def of_nat_poly) | |
| 3018 | ||
| 3019 | lemma lead_coeff_numeral [simp]: | |
| 3020 | "lead_coeff (numeral n) = numeral n" | |
| 3021 | unfolding lead_coeff_def | |
| 3022 | by (subst of_nat_numeral [symmetric], subst of_nat_poly) simp | |
| 3023 | ||
| 3024 | lemma lead_coeff_power: | |
| 63498 | 3025 |   "lead_coeff (p ^ n :: 'a :: {comm_semiring_1,semiring_no_zero_divisors} poly) = lead_coeff p ^ n"
 | 
| 62065 | 3026 | by (induction n) (simp_all add: lead_coeff_mult) | 
| 3027 | ||
| 3028 | lemma lead_coeff_nonzero: "p \<noteq> 0 \<Longrightarrow> lead_coeff p \<noteq> 0" | |
| 3029 | by (simp add: lead_coeff_def) | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3030 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3031 | |
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3032 | subsection \<open>Shifting polynomials\<close> | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3033 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3034 | definition poly_shift :: "nat \<Rightarrow> ('a::zero) poly \<Rightarrow> 'a poly" where
 | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3035 | "poly_shift n p = Abs_poly (\<lambda>i. coeff p (i + n))" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3036 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3037 | 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 | 3038 | by (auto simp add: nth_default_def add_ac) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3039 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3040 | 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 | 3041 | by (auto simp add: nth_default_def add_ac) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3042 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3043 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3044 | 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 | 3045 | proof - | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3046 | from MOST_coeff_eq_0[of p] obtain m where "\<forall>k>m. coeff p k = 0" by (auto simp: MOST_nat) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3047 | hence "\<forall>k>m. coeff p (k + n) = 0" by auto | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3048 | hence "\<forall>\<^sub>\<infinity>k. coeff p (k + n) = 0" by (auto simp: MOST_nat) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3049 | thus ?thesis by (simp add: poly_shift_def poly.Abs_poly_inverse) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3050 | qed | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3051 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3052 | lemma poly_shift_id [simp]: "poly_shift 0 = (\<lambda>x. x)" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3053 | by (simp add: poly_eq_iff fun_eq_iff coeff_poly_shift) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3054 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3055 | lemma poly_shift_0 [simp]: "poly_shift n 0 = 0" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3056 | by (simp add: poly_eq_iff coeff_poly_shift) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3057 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3058 | 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 | 3059 | by (simp add: poly_eq_iff coeff_poly_shift) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3060 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3061 | 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 | 3062 | by (auto simp add: poly_eq_iff coeff_poly_shift) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3063 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3064 | lemma coeffs_shift_poly [code abstract]: "coeffs (poly_shift n p) = drop n (coeffs p)" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3065 | proof (cases "p = 0") | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3066 | case False | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3067 | thus ?thesis | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3068 | by (intro coeffs_eqI) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3069 | (simp_all add: coeff_poly_shift nth_default_drop last_coeffs_not_0 nth_default_coeffs_eq) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3070 | qed simp_all | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3071 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3072 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3073 | subsection \<open>Truncating polynomials\<close> | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3074 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3075 | definition poly_cutoff where | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3076 | "poly_cutoff n p = Abs_poly (\<lambda>k. if k < n then coeff p k else 0)" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3077 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3078 | 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 | 3079 | unfolding poly_cutoff_def | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3080 | 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 | 3081 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3082 | lemma poly_cutoff_0 [simp]: "poly_cutoff n 0 = 0" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3083 | by (simp add: poly_eq_iff coeff_poly_cutoff) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3084 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3085 | 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 | 3086 | by (simp add: poly_eq_iff coeff_poly_cutoff) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3087 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3088 | lemma coeffs_poly_cutoff [code abstract]: | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3089 | "coeffs (poly_cutoff n p) = strip_while (op = 0) (take n (coeffs p))" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3090 | proof (cases "strip_while (op = 0) (take n (coeffs p)) = []") | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3091 | case True | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3092 | hence "coeff (poly_cutoff n p) k = 0" for k | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3093 | unfolding coeff_poly_cutoff | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3094 | by (auto simp: nth_default_coeffs_eq [symmetric] nth_default_def set_conv_nth) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3095 | hence "poly_cutoff n p = 0" by (simp add: poly_eq_iff) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3096 | thus ?thesis by (subst True) simp_all | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3097 | next | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3098 | case False | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3099 | have "no_trailing (op = 0) (strip_while (op = 0) (take n (coeffs p)))" by simp | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3100 | with False have "last (strip_while (op = 0) (take n (coeffs p))) \<noteq> 0" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3101 | unfolding no_trailing_unfold by auto | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3102 | thus ?thesis | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3103 | by (intro coeffs_eqI) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3104 | (simp_all add: coeff_poly_cutoff last_coeffs_not_0 nth_default_take nth_default_coeffs_eq) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3105 | qed | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3106 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3107 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3108 | subsection \<open>Reflecting polynomials\<close> | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3109 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3110 | definition reflect_poly where | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3111 | "reflect_poly p = Poly (rev (coeffs p))" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3112 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3113 | lemma coeffs_reflect_poly [code abstract]: | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3114 | "coeffs (reflect_poly p) = rev (dropWhile (op = 0) (coeffs p))" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3115 | unfolding reflect_poly_def by simp | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3116 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3117 | lemma reflect_poly_0 [simp]: "reflect_poly 0 = 0" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3118 | by (simp add: reflect_poly_def) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3119 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3120 | lemma reflect_poly_1 [simp]: "reflect_poly 1 = 1" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3121 | by (simp add: reflect_poly_def one_poly_def) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3122 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3123 | lemma coeff_reflect_poly: | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3124 | "coeff (reflect_poly p) n = (if n > degree p then 0 else coeff p (degree p - n))" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3125 | by (cases "p = 0") (auto simp add: reflect_poly_def coeff_Poly_eq nth_default_def | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3126 | rev_nth degree_eq_length_coeffs coeffs_nth not_less | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3127 | dest: le_imp_less_Suc) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3128 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3129 | 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 | 3130 | by (simp add: coeff_reflect_poly) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3131 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3132 | 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 | 3133 | by (simp add: coeff_reflect_poly poly_0_coeff_0) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3134 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3135 | lemma reflect_poly_pCons': | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3136 | "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 | 3137 | by (intro poly_eqI) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3138 | (auto simp: coeff_reflect_poly coeff_pCons not_less Suc_diff_le split: nat.split) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3139 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3140 | lemma reflect_poly_const [simp]: "reflect_poly [:a:] = [:a:]" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3141 | by (cases "a = 0") (simp_all add: reflect_poly_def) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3142 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3143 | lemma poly_reflect_poly_nz: | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3144 | "(x :: 'a :: field) \<noteq> 0 \<Longrightarrow> poly (reflect_poly p) x = x ^ degree p * poly p (inverse x)" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3145 | by (induction rule: pCons_induct) (simp_all add: field_simps reflect_poly_pCons' poly_monom) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3146 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3147 | lemma coeff_0_reflect_poly [simp]: "coeff (reflect_poly p) 0 = lead_coeff p" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3148 | by (simp add: coeff_reflect_poly lead_coeff_def) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3149 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3150 | 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 | 3151 | by (simp add: poly_0_coeff_0) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3152 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3153 | 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 | 3154 | by (cases p rule: pCons_cases) (simp add: reflect_poly_def ) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3155 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3156 | lemma degree_reflect_poly_le: "degree (reflect_poly p) \<le> degree p" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3157 | 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 | 3158 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3159 | lemma reflect_poly_pCons: | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3160 | "a \<noteq> 0 \<Longrightarrow> reflect_poly (pCons a p) = Poly (rev (a # coeffs p))" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3161 | by (subst coeffs_eq_iff) (simp add: coeffs_reflect_poly) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3162 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3163 | 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 | 3164 | by (cases p rule: pCons_cases) (simp add: reflect_poly_pCons degree_eq_length_coeffs) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3165 | |
| 63498 | 3166 | (* TODO: does this work with zero divisors as well? Probably not. *) | 
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3167 | lemma reflect_poly_mult: | 
| 63498 | 3168 | "reflect_poly (p * q) = | 
| 3169 |      reflect_poly p * reflect_poly (q :: _ :: {comm_semiring_0,semiring_no_zero_divisors} poly)"
 | |
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3170 | proof (cases "p = 0 \<or> q = 0") | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3171 | case False | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3172 | hence [simp]: "p \<noteq> 0" "q \<noteq> 0" by auto | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3173 | show ?thesis | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3174 | proof (rule poly_eqI) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3175 | fix i :: nat | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3176 | show "coeff (reflect_poly (p * q)) i = coeff (reflect_poly p * reflect_poly q) i" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3177 | proof (cases "i \<le> degree (p * q)") | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3178 | case True | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3179 |       def A \<equiv> "{..i} \<inter> {i - degree q..degree p}"
 | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3180 |       def B \<equiv> "{..degree p} \<inter> {degree p - i..degree (p*q) - i}"
 | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3181 | let ?f = "\<lambda>j. degree p - j" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3182 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3183 | 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 | 3184 | by (simp add: coeff_reflect_poly) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3185 | also have "\<dots> = (\<Sum>j\<le>degree (p * q) - i. coeff p j * coeff q (degree (p * q) - i - j))" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3186 | unfolding coeff_mult by simp | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3187 | also have "\<dots> = (\<Sum>j\<in>B. coeff p j * coeff q (degree (p * q) - i - j))" | 
| 64267 | 3188 | 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 | 3189 | also from True have "\<dots> = (\<Sum>j\<in>A. coeff p (degree p - j) * coeff q (degree q - (i - j)))" | 
| 64267 | 3190 | by (intro sum.reindex_bij_witness[of _ ?f ?f]) | 
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3191 | (auto simp: A_def B_def degree_mult_eq add_ac) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3192 |       also have "\<dots> = (\<Sum>j\<le>i. if j \<in> {i - degree q..degree p} then
 | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3193 | coeff p (degree p - j) * coeff q (degree q - (i - j)) else 0)" | 
| 64267 | 3194 | by (subst sum.inter_restrict [symmetric]) (simp_all add: A_def) | 
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3195 | also have "\<dots> = coeff (reflect_poly p * reflect_poly q) i" | 
| 64267 | 3196 | by (fastforce simp: coeff_mult coeff_reflect_poly intro!: sum.cong) | 
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3197 | finally show ?thesis . | 
| 64267 | 3198 | 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 | 3199 | qed | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3200 | qed auto | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3201 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3202 | lemma reflect_poly_smult: | 
| 63498 | 3203 |   "reflect_poly (Polynomial.smult (c::'a::{comm_semiring_0,semiring_no_zero_divisors}) p) = 
 | 
| 3204 | Polynomial.smult c (reflect_poly p)" | |
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3205 | using reflect_poly_mult[of "[:c:]" p] by simp | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3206 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3207 | lemma reflect_poly_power: | 
| 63498 | 3208 |     "reflect_poly (p ^ n :: 'a :: {comm_semiring_1,semiring_no_zero_divisors} poly) = 
 | 
| 3209 | reflect_poly p ^ n" | |
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3210 | by (induction n) (simp_all add: reflect_poly_mult) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3211 | |
| 64272 | 3212 | lemma reflect_poly_prod: | 
| 3213 |   "reflect_poly (prod (f :: _ \<Rightarrow> _ :: {comm_semiring_0,semiring_no_zero_divisors} poly) A) = 
 | |
| 3214 | prod (\<lambda>x. reflect_poly (f x)) A" | |
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3215 | by (cases "finite A", induction rule: finite_induct) (simp_all add: reflect_poly_mult) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3216 | |
| 63882 
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
 nipkow parents: 
63649diff
changeset | 3217 | lemma reflect_poly_prod_list: | 
| 
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
 nipkow parents: 
63649diff
changeset | 3218 |   "reflect_poly (prod_list (xs :: _ :: {comm_semiring_0,semiring_no_zero_divisors} poly list)) = 
 | 
| 
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
 nipkow parents: 
63649diff
changeset | 3219 | prod_list (map reflect_poly xs)" | 
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3220 | by (induction xs) (simp_all add: reflect_poly_mult) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3221 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3222 | lemma reflect_poly_Poly_nz: | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3223 | "xs \<noteq> [] \<Longrightarrow> last xs \<noteq> 0 \<Longrightarrow> reflect_poly (Poly xs) = Poly (rev xs)" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3224 | unfolding reflect_poly_def coeffs_Poly by simp | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3225 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3226 | lemmas reflect_poly_simps = | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3227 | reflect_poly_0 reflect_poly_1 reflect_poly_const reflect_poly_smult reflect_poly_mult | 
| 64272 | 3228 | reflect_poly_power reflect_poly_prod reflect_poly_prod_list | 
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3229 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3230 | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3231 | subsection \<open>Derivatives of univariate polynomials\<close> | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3232 | |
| 63498 | 3233 | function pderiv :: "('a :: {comm_semiring_1,semiring_no_zero_divisors}) poly \<Rightarrow> 'a poly"
 | 
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3234 | where | 
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 3235 | "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 | 3236 | by (auto intro: pCons_cases) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3237 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3238 | termination pderiv | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3239 | by (relation "measure degree") simp_all | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3240 | |
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 3241 | 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 | 3242 | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3243 | lemma pderiv_0 [simp]: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3244 | "pderiv 0 = 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3245 | using pderiv.simps [of 0 0] by simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3246 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3247 | lemma pderiv_pCons: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3248 | "pderiv (pCons a p) = p + pCons 0 (pderiv p)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3249 | by (simp add: pderiv.simps) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3250 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3251 | lemma pderiv_1 [simp]: "pderiv 1 = 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3252 | unfolding one_poly_def by (simp add: pderiv_pCons) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3253 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3254 | lemma pderiv_of_nat [simp]: "pderiv (of_nat n) = 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3255 | and pderiv_numeral [simp]: "pderiv (numeral m) = 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3256 | 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 | 3257 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3258 | lemma coeff_pderiv: "coeff (pderiv p) n = of_nat (Suc n) * coeff p (Suc n)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3259 | by (induct p arbitrary: n) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3260 | (auto simp add: pderiv_pCons coeff_pCons algebra_simps split: nat.split) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3261 | |
| 63498 | 3262 | fun pderiv_coeffs_code | 
| 3263 |       :: "('a :: {comm_semiring_1,semiring_no_zero_divisors}) \<Rightarrow> 'a list \<Rightarrow> 'a list" where
 | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3264 | "pderiv_coeffs_code f (x # xs) = cCons (f * x) (pderiv_coeffs_code (f+1) xs)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3265 | | "pderiv_coeffs_code f [] = []" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3266 | |
| 63498 | 3267 | definition pderiv_coeffs :: | 
| 3268 |     "'a :: {comm_semiring_1,semiring_no_zero_divisors} list \<Rightarrow> 'a list" where
 | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3269 | "pderiv_coeffs xs = pderiv_coeffs_code 1 (tl xs)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3270 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3271 | (* Efficient code for pderiv contributed by René Thiemann and Akihisa Yamada *) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3272 | lemma pderiv_coeffs_code: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3273 | "nth_default 0 (pderiv_coeffs_code f xs) n = (f + of_nat n) * (nth_default 0 xs n)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3274 | proof (induct xs arbitrary: f n) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3275 | case (Cons x xs f n) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3276 | show ?case | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3277 | proof (cases n) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3278 | case 0 | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3279 | thus ?thesis by (cases "pderiv_coeffs_code (f + 1) xs = [] \<and> f * x = 0", auto simp: cCons_def) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3280 | next | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3281 | case (Suc m) note n = this | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3282 | show ?thesis | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3283 | 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 | 3284 | case False | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3285 | hence "nth_default 0 (pderiv_coeffs_code f (x # xs)) n = | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3286 | nth_default 0 (pderiv_coeffs_code (f + 1) xs) m" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3287 | by (auto simp: cCons_def n) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3288 | also have "\<dots> = (f + of_nat n) * (nth_default 0 xs m)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3289 | unfolding Cons by (simp add: n add_ac) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3290 | finally show ?thesis by (simp add: n) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3291 | next | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3292 | case True | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3293 |       {
 | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3294 | fix g | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3295 | have "pderiv_coeffs_code g xs = [] \<Longrightarrow> g + of_nat m = 0 \<or> nth_default 0 xs m = 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3296 | proof (induct xs arbitrary: g m) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3297 | case (Cons x xs g) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3298 | from Cons(2) have empty: "pderiv_coeffs_code (g + 1) xs = []" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3299 | and g: "(g = 0 \<or> x = 0)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3300 | by (auto simp: cCons_def split: if_splits) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3301 | note IH = Cons(1)[OF empty] | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3302 | from IH[of m] IH[of "m - 1"] g | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3303 | show ?case by (cases m, auto simp: field_simps) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3304 | qed simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3305 | } note empty = this | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3306 | 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 | 3307 | by (auto simp: cCons_def n) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3308 | moreover have "(f + of_nat n) * nth_default 0 (x # xs) n = 0" using True | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3309 | by (simp add: n, insert empty[of "f+1"], auto simp: field_simps) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3310 | ultimately show ?thesis by simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3311 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3312 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3313 | qed simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3314 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3315 | lemma map_upt_Suc: "map f [0 ..< Suc n] = f 0 # map (\<lambda> i. f (Suc i)) [0 ..< n]" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3316 | by (induct n arbitrary: f, auto) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3317 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3318 | lemma coeffs_pderiv_code [code abstract]: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3319 | "coeffs (pderiv p) = pderiv_coeffs (coeffs p)" unfolding pderiv_coeffs_def | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3320 | 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 | 3321 | case (1 n) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3322 | have id: "coeff p (Suc n) = nth_default 0 (map (\<lambda>i. coeff p (Suc i)) [0..<degree p]) n" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3323 | by (cases "n < degree p", auto simp: nth_default_def coeff_eq_0) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3324 | show ?case unfolding coeffs_def map_upt_Suc by (auto simp: id) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3325 | next | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3326 | case 2 | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3327 | obtain n xs where id: "tl (coeffs p) = xs" "(1 :: 'a) = n" by auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3328 | from 2 show ?case | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3329 | unfolding id by (induct xs arbitrary: n, auto simp: cCons_def) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3330 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3331 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3332 | context | 
| 63498 | 3333 |   assumes "SORT_CONSTRAINT('a::{comm_semiring_1,semiring_no_zero_divisors, semiring_char_0})"
 | 
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3334 | begin | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3335 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3336 | lemma pderiv_eq_0_iff: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3337 | "pderiv (p :: 'a poly) = 0 \<longleftrightarrow> degree p = 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3338 | apply (rule iffI) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3339 | apply (cases p, simp) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3340 | apply (simp add: poly_eq_iff coeff_pderiv del: of_nat_Suc) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3341 | apply (simp add: poly_eq_iff coeff_pderiv coeff_eq_0) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3342 | done | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3343 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3344 | lemma degree_pderiv: "degree (pderiv (p :: 'a poly)) = degree p - 1" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3345 | apply (rule order_antisym [OF degree_le]) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3346 | apply (simp add: coeff_pderiv coeff_eq_0) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3347 | apply (cases "degree p", simp) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3348 | apply (rule le_degree) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3349 | apply (simp add: coeff_pderiv del: of_nat_Suc) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3350 | apply (metis degree_0 leading_coeff_0_iff nat.distinct(1)) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3351 | done | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3352 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3353 | lemma not_dvd_pderiv: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3354 | assumes "degree (p :: 'a poly) \<noteq> 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3355 | shows "\<not> p dvd pderiv p" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3356 | proof | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3357 | assume dvd: "p dvd pderiv p" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3358 | then obtain q where p: "pderiv p = p * q" unfolding dvd_def by auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3359 | from dvd have le: "degree p \<le> degree (pderiv p)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3360 | by (simp add: assms dvd_imp_degree_le pderiv_eq_0_iff) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3361 | from this[unfolded degree_pderiv] assms show False by auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3362 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3363 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3364 | lemma dvd_pderiv_iff [simp]: "(p :: 'a poly) dvd pderiv p \<longleftrightarrow> degree p = 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3365 | 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 | 3366 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3367 | end | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3368 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3369 | lemma pderiv_singleton [simp]: "pderiv [:a:] = 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3370 | by (simp add: pderiv_pCons) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3371 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3372 | lemma pderiv_add: "pderiv (p + q) = pderiv p + pderiv q" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3373 | by (rule poly_eqI, simp add: coeff_pderiv algebra_simps) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3374 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3375 | lemma pderiv_minus: "pderiv (- p :: 'a :: idom poly) = - pderiv p" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3376 | by (rule poly_eqI, simp add: coeff_pderiv algebra_simps) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3377 | |
| 63498 | 3378 | lemma pderiv_diff: "pderiv ((p :: _ :: idom poly) - q) = pderiv p - pderiv q" | 
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3379 | by (rule poly_eqI, simp add: coeff_pderiv algebra_simps) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3380 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3381 | lemma pderiv_smult: "pderiv (smult a p) = smult a (pderiv p)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3382 | by (rule poly_eqI, simp add: coeff_pderiv algebra_simps) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3383 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3384 | lemma pderiv_mult: "pderiv (p * q) = p * pderiv q + q * pderiv p" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3385 | by (induct p) (auto simp: pderiv_add pderiv_smult pderiv_pCons algebra_simps) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3386 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3387 | lemma pderiv_power_Suc: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3388 | "pderiv (p ^ Suc n) = smult (of_nat (Suc n)) (p ^ n) * pderiv p" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3389 | apply (induct n) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3390 | apply simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3391 | apply (subst power_Suc) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3392 | apply (subst pderiv_mult) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3393 | apply (erule ssubst) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3394 | apply (simp only: of_nat_Suc smult_add_left smult_1_left) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3395 | apply (simp add: algebra_simps) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3396 | done | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3397 | |
| 64272 | 3398 | lemma pderiv_prod: "pderiv (prod f (as)) = | 
| 3399 |   (\<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 | 3400 | proof (induct as rule: infinite_finite_induct) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3401 | case (insert a as) | 
| 64272 | 3402 | hence id: "prod f (insert a as) = f a * prod f as" | 
| 64267 | 3403 | "\<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 | 3404 |     "insert a as - {a} = as"
 | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3405 | by auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3406 |   {
 | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3407 | fix b | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3408 | assume "b \<in> as" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3409 |     hence id2: "insert a as - {b} = insert a (as - {b})" using \<open>a \<notin> as\<close> by auto
 | 
| 64272 | 3410 |     have "prod f (insert a as - {b}) = f a * prod f (as - {b})"
 | 
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3411 | unfolding id2 | 
| 64272 | 3412 | by (subst prod.insert, insert insert, auto) | 
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3413 | } note id2 = this | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3414 | show ?case | 
| 64267 | 3415 | unfolding id pderiv_mult insert(3) sum_distrib_left | 
| 3416 | by (auto simp add: ac_simps id2 intro!: sum.cong) | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3417 | qed auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3418 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3419 | lemma DERIV_pow2: "DERIV (%x. x ^ Suc n) x :> real (Suc n) * (x ^ n)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3420 | by (rule DERIV_cong, rule DERIV_pow, simp) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3421 | declare DERIV_pow2 [simp] DERIV_pow [simp] | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3422 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3423 | lemma DERIV_add_const: "DERIV f x :> D ==> DERIV (%x. a + f x :: 'a::real_normed_field) x :> D" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3424 | by (rule DERIV_cong, rule DERIV_add, auto) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3425 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3426 | lemma poly_DERIV [simp]: "DERIV (%x. poly p x) x :> poly (pderiv p) x" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3427 | by (induct p, auto intro!: derivative_eq_intros simp add: pderiv_pCons) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3428 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3429 | lemma continuous_on_poly [continuous_intros]: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3430 |   fixes p :: "'a :: {real_normed_field} poly"
 | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3431 | assumes "continuous_on A f" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3432 | shows "continuous_on A (\<lambda>x. poly p (f x))" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3433 | proof - | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3434 | have "continuous_on A (\<lambda>x. (\<Sum>i\<le>degree p. (f x) ^ i * coeff p i))" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3435 | by (intro continuous_intros assms) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3436 | also have "\<dots> = (\<lambda>x. poly p (f x))" by (intro ext) (simp add: poly_altdef mult_ac) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3437 | finally show ?thesis . | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3438 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3439 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3440 | text\<open>Consequences of the derivative theorem above\<close> | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3441 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3442 | lemma poly_differentiable[simp]: "(%x. poly p x) differentiable (at x::real filter)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3443 | apply (simp add: real_differentiable_def) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3444 | apply (blast intro: poly_DERIV) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3445 | done | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3446 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3447 | lemma poly_isCont[simp]: "isCont (%x. poly p x) (x::real)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3448 | by (rule poly_DERIV [THEN DERIV_isCont]) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3449 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3450 | lemma poly_IVT_pos: "[| a < b; poly p (a::real) < 0; 0 < poly p b |] | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3451 | ==> \<exists>x. a < x & x < b & (poly p x = 0)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3452 | using IVT_objl [of "poly p" a 0 b] | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3453 | by (auto simp add: order_le_less) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3454 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3455 | lemma poly_IVT_neg: "[| (a::real) < b; 0 < poly p a; poly p b < 0 |] | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3456 | ==> \<exists>x. a < x & x < b & (poly p x = 0)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3457 | by (insert poly_IVT_pos [where p = "- p" ]) simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3458 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3459 | lemma poly_IVT: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3460 | fixes p::"real poly" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3461 | assumes "a<b" and "poly p a * poly p b < 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3462 | shows "\<exists>x>a. x < b \<and> poly p x = 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3463 | by (metis assms(1) assms(2) less_not_sym mult_less_0_iff poly_IVT_neg poly_IVT_pos) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3464 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3465 | lemma poly_MVT: "(a::real) < b ==> | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3466 | \<exists>x. a < x & x < b & (poly p b - poly p a = (b - a) * poly (pderiv p) x)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3467 | using MVT [of a b "poly p"] | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3468 | apply auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3469 | apply (rule_tac x = z in exI) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3470 | apply (auto simp add: mult_left_cancel poly_DERIV [THEN DERIV_unique]) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3471 | done | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3472 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3473 | lemma poly_MVT': | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3474 |   assumes "{min a b..max a b} \<subseteq> A"
 | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3475 | shows "\<exists>x\<in>A. poly p b - poly p a = (b - a) * poly (pderiv p) (x::real)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3476 | proof (cases a b rule: linorder_cases) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3477 | case less | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3478 | from poly_MVT[OF less, of p] guess x by (elim exE conjE) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3479 | thus ?thesis by (intro bexI[of _ x]) (auto intro!: subsetD[OF assms]) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3480 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3481 | next | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3482 | case greater | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3483 | from poly_MVT[OF greater, of p] guess x by (elim exE conjE) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3484 | thus ?thesis by (intro bexI[of _ x]) (auto simp: algebra_simps intro!: subsetD[OF assms]) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3485 | qed (insert assms, auto) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3486 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3487 | lemma poly_pinfty_gt_lc: | 
| 63649 | 3488 | fixes p :: "real poly" | 
| 3489 | assumes "lead_coeff p > 0" | |
| 3490 | shows "\<exists> n. \<forall> x \<ge> n. poly p x \<ge> lead_coeff p" | |
| 3491 | using assms | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3492 | proof (induct p) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3493 | case 0 | 
| 63649 | 3494 | then show ?case by auto | 
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3495 | next | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3496 | case (pCons a p) | 
| 63649 | 3497 | from this(1) consider "a \<noteq> 0" "p = 0" | "p \<noteq> 0" by auto | 
| 3498 | then show ?case | |
| 3499 | proof cases | |
| 3500 | case 1 | |
| 3501 | then show ?thesis by auto | |
| 3502 | next | |
| 3503 | case 2 | |
| 3504 | with pCons obtain n1 where gte_lcoeff: "\<forall>x\<ge>n1. lead_coeff p \<le> poly p x" | |
| 3505 | by auto | |
| 3506 | from pCons(3) \<open>p \<noteq> 0\<close> have gt_0: "lead_coeff p > 0" by auto | |
| 3507 | define n where "n = max n1 (1 + \<bar>a\<bar> / lead_coeff p)" | |
| 3508 | 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 | 3509 | proof - | 
| 63649 | 3510 | from gte_lcoeff that have "lead_coeff p \<le> poly p x" | 
| 3511 | by (auto simp: n_def) | |
| 3512 | with gt_0 have "\<bar>a\<bar> / lead_coeff p \<ge> \<bar>a\<bar> / poly p x" and "poly p x > 0" | |
| 3513 | by (auto intro: frac_le) | |
| 3514 | with \<open>n\<le>x\<close>[unfolded n_def] have "x \<ge> 1 + \<bar>a\<bar> / poly p x" | |
| 3515 | by auto | |
| 3516 | with \<open>lead_coeff p \<le> poly p x\<close> \<open>poly p x > 0\<close> \<open>p \<noteq> 0\<close> | |
| 3517 | show "lead_coeff (pCons a p) \<le> poly (pCons a p) x" | |
| 3518 | by (auto simp: field_simps) | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3519 | qed | 
| 63649 | 3520 | then show ?thesis by blast | 
| 3521 | qed | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3522 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3523 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3524 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3525 | subsection \<open>Algebraic numbers\<close> | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3526 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3527 | text \<open> | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3528 | Algebraic numbers can be defined in two equivalent ways: all real numbers that are | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3529 | roots of rational polynomials or of integer polynomials. The Algebraic-Numbers AFP entry | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3530 | uses the rational definition, but we need the integer definition. | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3531 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3532 | The equivalence is obvious since any rational polynomial can be multiplied with the | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3533 | 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 | 3534 | \<close> | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3535 | subsection \<open>Algebraic numbers\<close> | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3536 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3537 | definition algebraic :: "'a :: field_char_0 \<Rightarrow> bool" where | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3538 | "algebraic x \<longleftrightarrow> (\<exists>p. (\<forall>i. coeff p i \<in> \<int>) \<and> p \<noteq> 0 \<and> poly p x = 0)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3539 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3540 | lemma algebraicI: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3541 | assumes "\<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 | 3542 | shows "algebraic x" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3543 | using assms unfolding algebraic_def by blast | 
| 62065 | 3544 | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3545 | lemma algebraicE: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3546 | assumes "algebraic x" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3547 | 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 | 3548 | using assms unfolding algebraic_def by blast | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3549 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3550 | lemma quotient_of_denom_pos': "snd (quotient_of x) > 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3551 | using quotient_of_denom_pos[OF surjective_pairing] . | 
| 62065 | 3552 | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3553 | lemma of_int_div_in_Ints: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3554 | "b dvd a \<Longrightarrow> of_int a div of_int b \<in> (\<int> :: 'a :: ring_div set)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3555 | proof (cases "of_int b = (0 :: 'a)") | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3556 | assume "b dvd a" "of_int b \<noteq> (0::'a)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3557 | then obtain c where "a = b * c" by (elim dvdE) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3558 | with \<open>of_int b \<noteq> (0::'a)\<close> show ?thesis by simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3559 | qed auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3560 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3561 | lemma of_int_divide_in_Ints: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3562 | "b dvd a \<Longrightarrow> of_int a / of_int b \<in> (\<int> :: 'a :: field set)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3563 | proof (cases "of_int b = (0 :: 'a)") | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3564 | assume "b dvd a" "of_int b \<noteq> (0::'a)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3565 | then obtain c where "a = b * c" by (elim dvdE) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3566 | with \<open>of_int b \<noteq> (0::'a)\<close> show ?thesis by simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3567 | qed auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3568 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3569 | lemma algebraic_altdef: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3570 | fixes p :: "'a :: field_char_0 poly" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3571 | shows "algebraic x \<longleftrightarrow> (\<exists>p. (\<forall>i. coeff p i \<in> \<rat>) \<and> p \<noteq> 0 \<and> poly p x = 0)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3572 | proof safe | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3573 | fix p assume rat: "\<forall>i. coeff p i \<in> \<rat>" and root: "poly p x = 0" and nz: "p \<noteq> 0" | 
| 63040 | 3574 | define cs where "cs = coeffs p" | 
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3575 | from rat have "\<forall>c\<in>range (coeff p). \<exists>c'. c = of_rat c'" unfolding Rats_def by blast | 
| 63060 | 3576 | 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 | 3577 | by (subst (asm) bchoice_iff) blast | 
| 63040 | 3578 | define cs' where "cs' = map (quotient_of \<circ> f) (coeffs p)" | 
| 3579 | define d where "d = Lcm (set (map snd cs'))" | |
| 3580 | define p' where "p' = smult (of_int d) p" | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3581 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3582 | have "\<forall>n. coeff p' n \<in> \<int>" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3583 | proof | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3584 | fix n :: nat | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3585 | show "coeff p' n \<in> \<int>" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3586 | proof (cases "n \<le> degree p") | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3587 | case True | 
| 63040 | 3588 | define c where "c = coeff p n" | 
| 3589 | define a where "a = fst (quotient_of (f (coeff p n)))" | |
| 3590 | define b where "b = snd (quotient_of (f (coeff p n)))" | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3591 | have b_pos: "b > 0" unfolding b_def using quotient_of_denom_pos' by simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3592 | have "coeff p' n = of_int d * coeff p n" by (simp add: p'_def) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3593 | also have "coeff p n = of_rat (of_int a / of_int b)" unfolding a_def b_def | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3594 | by (subst quotient_of_div [of "f (coeff p n)", symmetric]) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3595 | (simp_all add: f [symmetric]) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3596 | also have "of_int d * \<dots> = of_rat (of_int (a*d) / of_int b)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3597 | by (simp add: of_rat_mult of_rat_divide) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3598 | also from nz True have "b \<in> snd ` set cs'" unfolding cs'_def | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3599 | by (force simp: o_def b_def coeffs_def simp del: upt_Suc) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3600 | hence "b dvd (a * d)" unfolding d_def by simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3601 | hence "of_int (a * d) / of_int b \<in> (\<int> :: rat set)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3602 | by (rule of_int_divide_in_Ints) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3603 | hence "of_rat (of_int (a * d) / of_int b) \<in> \<int>" by (elim Ints_cases) auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3604 | finally show ?thesis . | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3605 | qed (auto simp: p'_def not_le coeff_eq_0) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3606 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3607 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3608 |   moreover have "set (map snd cs') \<subseteq> {0<..}"
 | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3609 | unfolding cs'_def using quotient_of_denom_pos' by (auto simp: coeffs_def simp del: upt_Suc) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3610 | hence "d \<noteq> 0" unfolding d_def by (induction cs') simp_all | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3611 | with nz have "p' \<noteq> 0" by (simp add: p'_def) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3612 | moreover from root have "poly p' x = 0" by (simp add: p'_def) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3613 | ultimately show "algebraic x" unfolding algebraic_def by blast | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3614 | next | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3615 | assume "algebraic x" | 
| 63060 | 3616 | 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 | 3617 | by (force simp: algebraic_def) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3618 | moreover have "coeff p i \<in> \<int> \<Longrightarrow> coeff p i \<in> \<rat>" for i by (elim Ints_cases) simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3619 | ultimately show "(\<exists>p. (\<forall>i. coeff p i \<in> \<rat>) \<and> p \<noteq> 0 \<and> poly p x = 0)" by auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3620 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3621 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3622 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3623 | text\<open>Lemmas for Derivatives\<close> | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3624 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3625 | lemma order_unique_lemma: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3626 | fixes p :: "'a::idom poly" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3627 | assumes "[:-a, 1:] ^ n dvd p" "\<not> [:-a, 1:] ^ Suc n dvd p" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3628 | shows "n = order a p" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3629 | unfolding Polynomial.order_def | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3630 | apply (rule Least_equality [symmetric]) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3631 | apply (fact assms) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3632 | apply (rule classical) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3633 | apply (erule notE) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3634 | unfolding not_less_eq_eq | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3635 | using assms(1) apply (rule power_le_dvd) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3636 | apply assumption | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3637 | done | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3638 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3639 | lemma lemma_order_pderiv1: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3640 | "pderiv ([:- a, 1:] ^ Suc n * q) = [:- a, 1:] ^ Suc n * pderiv q + | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3641 | smult (of_nat (Suc n)) (q * [:- a, 1:] ^ n)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3642 | apply (simp only: pderiv_mult pderiv_power_Suc) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3643 | apply (simp del: power_Suc of_nat_Suc add: pderiv_pCons) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3644 | done | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3645 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3646 | lemma lemma_order_pderiv: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3647 | fixes p :: "'a :: field_char_0 poly" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3648 | assumes n: "0 < n" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3649 | and pd: "pderiv p \<noteq> 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3650 | and pe: "p = [:- a, 1:] ^ n * q" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3651 | and nd: "~ [:- a, 1:] dvd q" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3652 | shows "n = Suc (order a (pderiv p))" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3653 | using n | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3654 | proof - | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3655 | have "pderiv ([:- a, 1:] ^ n * q) \<noteq> 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3656 | using assms by auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3657 | obtain n' where "n = Suc n'" "0 < Suc n'" "pderiv ([:- a, 1:] ^ Suc n' * q) \<noteq> 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3658 | using assms by (cases n) auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3659 | have *: "!!k l. k dvd k * pderiv q + smult (of_nat (Suc n')) l \<Longrightarrow> k dvd l" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3660 | by (auto simp del: of_nat_Suc simp: dvd_add_right_iff dvd_smult_iff) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3661 | have "n' = order a (pderiv ([:- a, 1:] ^ Suc n' * q))" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3662 | proof (rule order_unique_lemma) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3663 | show "[:- a, 1:] ^ n' dvd pderiv ([:- a, 1:] ^ Suc n' * q)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3664 | apply (subst lemma_order_pderiv1) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3665 | apply (rule dvd_add) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3666 | apply (metis dvdI dvd_mult2 power_Suc2) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3667 | apply (metis dvd_smult dvd_triv_right) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3668 | done | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3669 | next | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3670 | show "\<not> [:- a, 1:] ^ Suc n' dvd pderiv ([:- a, 1:] ^ Suc n' * q)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3671 | apply (subst lemma_order_pderiv1) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3672 | by (metis * nd dvd_mult_cancel_right power_not_zero pCons_eq_0_iff power_Suc zero_neq_one) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3673 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3674 | then show ?thesis | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3675 | by (metis \<open>n = Suc n'\<close> pe) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3676 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3677 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3678 | lemma order_decomp: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3679 | assumes "p \<noteq> 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3680 | shows "\<exists>q. p = [:- a, 1:] ^ order a p * q \<and> \<not> [:- a, 1:] dvd q" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3681 | proof - | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3682 | from assms have A: "[:- a, 1:] ^ order a p dvd p" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3683 | and B: "\<not> [:- a, 1:] ^ Suc (order a p) dvd p" by (auto dest: order) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3684 | from A obtain q where C: "p = [:- a, 1:] ^ order a p * q" .. | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3685 | with B have "\<not> [:- a, 1:] ^ Suc (order a p) dvd [:- a, 1:] ^ order a p * q" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3686 | by simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3687 | then have "\<not> [:- a, 1:] ^ order a p * [:- a, 1:] dvd [:- a, 1:] ^ order a p * q" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3688 | by simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3689 | then have D: "\<not> [:- a, 1:] dvd q" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3690 | using idom_class.dvd_mult_cancel_left [of "[:- a, 1:] ^ order a p" "[:- a, 1:]" q] | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3691 | by auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3692 | from C D show ?thesis by blast | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3693 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3694 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3695 | lemma order_pderiv: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3696 | "\<lbrakk>pderiv p \<noteq> 0; order a (p :: 'a :: field_char_0 poly) \<noteq> 0\<rbrakk> \<Longrightarrow> | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3697 | (order a p = Suc (order a (pderiv p)))" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3698 | apply (case_tac "p = 0", simp) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3699 | apply (drule_tac a = a and p = p in order_decomp) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3700 | using neq0_conv | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3701 | apply (blast intro: lemma_order_pderiv) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3702 | done | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3703 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3704 | lemma order_mult: "p * q \<noteq> 0 \<Longrightarrow> order a (p * q) = order a p + order a q" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3705 | proof - | 
| 63040 | 3706 | define i where "i = order a p" | 
| 3707 | define j where "j = order a q" | |
| 3708 | define t where "t = [:-a, 1:]" | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3709 | have t_dvd_iff: "\<And>u. t dvd u \<longleftrightarrow> poly u a = 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3710 | unfolding t_def by (simp add: dvd_iff_poly_eq_0) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3711 | assume "p * q \<noteq> 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3712 | then show "order a (p * q) = i + j" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3713 | apply clarsimp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3714 | apply (drule order [where a=a and p=p, folded i_def t_def]) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3715 | apply (drule order [where a=a and p=q, folded j_def t_def]) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3716 | apply clarify | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3717 | apply (erule dvdE)+ | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3718 | apply (rule order_unique_lemma [symmetric], fold t_def) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3719 | apply (simp_all add: power_add t_dvd_iff) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3720 | done | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3721 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3722 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3723 | lemma order_smult: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3724 | assumes "c \<noteq> 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3725 | shows "order x (smult c p) = order x p" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3726 | proof (cases "p = 0") | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3727 | case False | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3728 | have "smult c p = [:c:] * p" by simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3729 | also from assms False have "order x \<dots> = order x [:c:] + order x p" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3730 | by (subst order_mult) simp_all | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3731 | also from assms have "order x [:c:] = 0" by (intro order_0I) auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3732 | finally show ?thesis by simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3733 | qed simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3734 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3735 | (* Next two lemmas contributed by Wenda Li *) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3736 | lemma order_1_eq_0 [simp]:"order x 1 = 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3737 | by (metis order_root poly_1 zero_neq_one) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3738 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3739 | lemma order_power_n_n: "order a ([:-a,1:]^n)=n" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3740 | proof (induct n) (*might be proved more concisely using nat_less_induct*) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3741 | case 0 | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3742 | thus ?case by (metis order_root poly_1 power_0 zero_neq_one) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3743 | next | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3744 | case (Suc n) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3745 | have "order a ([:- a, 1:] ^ Suc n)=order a ([:- a, 1:] ^ n) + order a [:-a,1:]" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3746 | by (metis (no_types, hide_lams) One_nat_def add_Suc_right monoid_add_class.add.right_neutral | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3747 | one_neq_zero order_mult pCons_eq_0_iff power_add power_eq_0_iff power_one_right) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3748 | moreover have "order a [:-a,1:]=1" unfolding order_def | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3749 | proof (rule Least_equality,rule ccontr) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3750 | assume "\<not> \<not> [:- a, 1:] ^ Suc 1 dvd [:- a, 1:]" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3751 | hence "[:- a, 1:] ^ Suc 1 dvd [:- a, 1:]" by simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3752 | hence "degree ([:- a, 1:] ^ Suc 1) \<le> degree ([:- a, 1:] )" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3753 | by (rule dvd_imp_degree_le,auto) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3754 | thus False by auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3755 | next | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3756 | fix y assume asm:"\<not> [:- a, 1:] ^ Suc y dvd [:- a, 1:]" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3757 | show "1 \<le> y" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3758 | proof (rule ccontr) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3759 | assume "\<not> 1 \<le> y" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3760 | hence "y=0" by auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3761 | hence "[:- a, 1:] ^ Suc y dvd [:- a, 1:]" by auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3762 | thus False using asm by auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3763 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3764 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3765 | ultimately show ?case using Suc by auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3766 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3767 | |
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3768 | lemma order_0_monom [simp]: | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3769 | assumes "c \<noteq> 0" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3770 | shows "order 0 (monom c n) = n" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3771 | using assms order_power_n_n[of 0 n] by (simp add: monom_altdef order_smult) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3772 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3773 | lemma dvd_imp_order_le: | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3774 | "q \<noteq> 0 \<Longrightarrow> p dvd q \<Longrightarrow> Polynomial.order a p \<le> Polynomial.order a q" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3775 | by (auto simp: order_mult elim: dvdE) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3776 | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3777 | text\<open>Now justify the standard squarefree decomposition, i.e. f / gcd(f,f').\<close> | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3778 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3779 | lemma order_divides: "[:-a, 1:] ^ n dvd p \<longleftrightarrow> p = 0 \<or> n \<le> order a p" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3780 | apply (cases "p = 0", auto) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3781 | apply (drule order_2 [where a=a and p=p]) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3782 | apply (metis not_less_eq_eq power_le_dvd) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3783 | apply (erule power_le_dvd [OF order_1]) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3784 | done | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3785 | |
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3786 | lemma monom_1_dvd_iff: | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3787 | assumes "p \<noteq> 0" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3788 | shows "monom 1 n dvd p \<longleftrightarrow> n \<le> Polynomial.order 0 p" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3789 | using assms order_divides[of 0 n p] by (simp add: monom_altdef) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3790 | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3791 | lemma poly_squarefree_decomp_order: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3792 | assumes "pderiv (p :: 'a :: field_char_0 poly) \<noteq> 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3793 | and p: "p = q * d" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3794 | and p': "pderiv p = e * d" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3795 | and d: "d = r * p + s * pderiv p" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3796 | shows "order a q = (if order a p = 0 then 0 else 1)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3797 | proof (rule classical) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3798 | assume 1: "order a q \<noteq> (if order a p = 0 then 0 else 1)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3799 | from \<open>pderiv p \<noteq> 0\<close> have "p \<noteq> 0" by auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3800 | with p have "order a p = order a q + order a d" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3801 | by (simp add: order_mult) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3802 | with 1 have "order a p \<noteq> 0" by (auto split: if_splits) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3803 | have "order a (pderiv p) = order a e + order a d" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3804 | using \<open>pderiv p \<noteq> 0\<close> \<open>pderiv p = e * d\<close> by (simp add: order_mult) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3805 | have "order a p = Suc (order a (pderiv p))" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3806 | using \<open>pderiv p \<noteq> 0\<close> \<open>order a p \<noteq> 0\<close> by (rule order_pderiv) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3807 | have "d \<noteq> 0" using \<open>p \<noteq> 0\<close> \<open>p = q * d\<close> by simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3808 | have "([:-a, 1:] ^ (order a (pderiv p))) dvd d" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3809 | apply (simp add: d) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3810 | apply (rule dvd_add) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3811 | apply (rule dvd_mult) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3812 | apply (simp add: order_divides \<open>p \<noteq> 0\<close> | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3813 | \<open>order a p = Suc (order a (pderiv p))\<close>) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3814 | apply (rule dvd_mult) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3815 | apply (simp add: order_divides) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3816 | done | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3817 | then have "order a (pderiv p) \<le> order a d" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3818 | using \<open>d \<noteq> 0\<close> by (simp add: order_divides) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3819 | show ?thesis | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3820 | using \<open>order a p = order a q + order a d\<close> | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3821 | using \<open>order a (pderiv p) = order a e + order a d\<close> | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3822 | using \<open>order a p = Suc (order a (pderiv p))\<close> | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3823 | using \<open>order a (pderiv p) \<le> order a d\<close> | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3824 | by auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3825 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3826 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3827 | lemma poly_squarefree_decomp_order2: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3828 | "\<lbrakk>pderiv p \<noteq> (0 :: 'a :: field_char_0 poly); | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3829 | p = q * d; | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3830 | pderiv p = e * d; | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3831 | d = r * p + s * pderiv p | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3832 | \<rbrakk> \<Longrightarrow> \<forall>a. order a q = (if order a p = 0 then 0 else 1)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3833 | by (blast intro: poly_squarefree_decomp_order) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3834 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3835 | lemma order_pderiv2: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3836 | "\<lbrakk>pderiv p \<noteq> 0; order a (p :: 'a :: field_char_0 poly) \<noteq> 0\<rbrakk> | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3837 | \<Longrightarrow> (order a (pderiv p) = n) = (order a p = Suc n)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3838 | by (auto dest: order_pderiv) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3839 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3840 | definition | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3841 | rsquarefree :: "'a::idom poly => bool" where | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3842 | "rsquarefree p = (p \<noteq> 0 & (\<forall>a. (order a p = 0) | (order a p = 1)))" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3843 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3844 | lemma pderiv_iszero: "pderiv p = 0 \<Longrightarrow> \<exists>h. p = [:h :: 'a :: {semidom,semiring_char_0}:]"
 | 
| 63649 | 3845 | by (cases p) (auto simp: pderiv_eq_0_iff split: if_splits) | 
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3846 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3847 | lemma rsquarefree_roots: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3848 | fixes p :: "'a :: field_char_0 poly" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3849 | shows "rsquarefree p = (\<forall>a. \<not>(poly p a = 0 \<and> poly (pderiv p) a = 0))" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3850 | apply (simp add: rsquarefree_def) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3851 | apply (case_tac "p = 0", simp, simp) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3852 | apply (case_tac "pderiv p = 0") | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3853 | apply simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3854 | apply (drule pderiv_iszero, clarsimp) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3855 | apply (metis coeff_0 coeff_pCons_0 degree_pCons_0 le0 le_antisym order_degree) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3856 | apply (force simp add: order_root order_pderiv2) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3857 | done | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3858 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3859 | lemma poly_squarefree_decomp: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3860 | assumes "pderiv (p :: 'a :: field_char_0 poly) \<noteq> 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3861 | and "p = q * d" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3862 | and "pderiv p = e * d" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3863 | and "d = r * p + s * pderiv p" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3864 | shows "rsquarefree q & (\<forall>a. (poly q a = 0) = (poly p a = 0))" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3865 | proof - | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3866 | from \<open>pderiv p \<noteq> 0\<close> have "p \<noteq> 0" by auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3867 | with \<open>p = q * d\<close> have "q \<noteq> 0" by simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3868 | have "\<forall>a. order a q = (if order a p = 0 then 0 else 1)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3869 | using assms by (rule poly_squarefree_decomp_order2) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3870 | with \<open>p \<noteq> 0\<close> \<open>q \<noteq> 0\<close> show ?thesis | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3871 | by (simp add: rsquarefree_def order_root) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3872 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3873 | |
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3874 | lemma coeff_monom_mult: | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3875 | "coeff (monom c n * p) k = (if k < n then 0 else c * coeff p (k - n))" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3876 | proof - | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3877 | have "coeff (monom c n * p) k = (\<Sum>i\<le>k. (if n = i then c else 0) * coeff p (k - i))" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3878 | by (simp add: coeff_mult) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3879 | also have "\<dots> = (\<Sum>i\<le>k. (if n = i then c * coeff p (k - i) else 0))" | 
| 64267 | 3880 | by (intro sum.cong) simp_all | 
| 3881 | also have "\<dots> = (if k < n then 0 else c * coeff p (k - n))" by (simp add: sum.delta') | |
| 63317 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3882 | finally show ?thesis . | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3883 | qed | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3884 | |
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3885 | lemma monom_1_dvd_iff': | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3886 | "monom 1 n dvd p \<longleftrightarrow> (\<forall>k<n. coeff p k = 0)" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3887 | proof | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3888 | assume "monom 1 n dvd p" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3889 | then obtain r where r: "p = monom 1 n * r" by (elim dvdE) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3890 | thus "\<forall>k<n. coeff p k = 0" by (simp add: coeff_mult) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3891 | next | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3892 | assume zero: "(\<forall>k<n. coeff p k = 0)" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3893 | define r where "r = Abs_poly (\<lambda>k. coeff p (k + n))" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3894 | have "\<forall>\<^sub>\<infinity>k. coeff p (k + n) = 0" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3895 | by (subst cofinite_eq_sequentially, subst eventually_sequentially_seg, | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3896 | subst cofinite_eq_sequentially [symmetric]) transfer | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3897 | hence coeff_r [simp]: "coeff r k = coeff p (k + n)" for k unfolding r_def | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3898 | by (subst poly.Abs_poly_inverse) simp_all | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3899 | have "p = monom 1 n * r" | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3900 | by (intro poly_eqI, subst coeff_monom_mult) (insert zero, simp_all) | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3901 | thus "monom 1 n dvd p" by simp | 
| 
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
 eberlm parents: 
63145diff
changeset | 3902 | qed | 
| 52380 | 3903 | |
| 3904 | no_notation cCons (infixr "##" 65) | |
| 31663 | 3905 | |
| 29478 | 3906 | end |