| author | wenzelm | 
| Sat, 14 May 2016 13:52:01 +0200 | |
| changeset 63093 | 3081f7719df7 | 
| parent 63060 | 293ede07b775 | 
| child 63145 | 703edebd1d92 | 
| 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}"
 | 
| 59983 
cd2efd7d06bd
replace almost_everywhere_zero by Infinite_Set.MOST
 hoelzl parents: 
59815diff
changeset | 58 | morphisms coeff Abs_poly by (auto intro!: ALL_MOST) | 
| 29451 | 59 | |
| 59487 
adaa430fc0f7
default abstypes and default abstract equations make technical (no_code) annotation superfluous
 haftmann parents: 
58881diff
changeset | 60 | setup_lifting type_definition_poly | 
| 52380 | 61 | |
| 62 | 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 | 63 | by (simp add: coeff_inject [symmetric] fun_eq_iff) | 
| 29451 | 64 | |
| 52380 | 65 | lemma poly_eqI: "(\<And>n. coeff p n = coeff q n) \<Longrightarrow> p = q" | 
| 66 | by (simp add: poly_eq_iff) | |
| 67 | ||
| 59983 
cd2efd7d06bd
replace almost_everywhere_zero by Infinite_Set.MOST
 hoelzl parents: 
59815diff
changeset | 68 | lemma MOST_coeff_eq_0: "\<forall>\<^sub>\<infinity> n. coeff p n = 0" | 
| 52380 | 69 | using coeff [of p] by simp | 
| 29451 | 70 | |
| 71 | ||
| 60500 | 72 | subsection \<open>Degree of a polynomial\<close> | 
| 29451 | 73 | |
| 52380 | 74 | definition degree :: "'a::zero poly \<Rightarrow> nat" | 
| 75 | where | |
| 29451 | 76 | "degree p = (LEAST n. \<forall>i>n. coeff p i = 0)" | 
| 77 | ||
| 52380 | 78 | lemma coeff_eq_0: | 
| 79 | assumes "degree p < n" | |
| 80 | shows "coeff p n = 0" | |
| 29451 | 81 | proof - | 
| 59983 
cd2efd7d06bd
replace almost_everywhere_zero by Infinite_Set.MOST
 hoelzl parents: 
59815diff
changeset | 82 | have "\<exists>n. \<forall>i>n. coeff p i = 0" | 
| 
cd2efd7d06bd
replace almost_everywhere_zero by Infinite_Set.MOST
 hoelzl parents: 
59815diff
changeset | 83 | using MOST_coeff_eq_0 by (simp add: MOST_nat) | 
| 52380 | 84 | then have "\<forall>i>degree p. coeff p i = 0" | 
| 29451 | 85 | unfolding degree_def by (rule LeastI_ex) | 
| 52380 | 86 | with assms show ?thesis by simp | 
| 29451 | 87 | qed | 
| 88 | ||
| 89 | lemma le_degree: "coeff p n \<noteq> 0 \<Longrightarrow> n \<le> degree p" | |
| 90 | by (erule contrapos_np, rule coeff_eq_0, simp) | |
| 91 | ||
| 92 | lemma degree_le: "\<forall>i>n. coeff p i = 0 \<Longrightarrow> degree p \<le> n" | |
| 93 | unfolding degree_def by (erule Least_le) | |
| 94 | ||
| 95 | lemma less_degree_imp: "n < degree p \<Longrightarrow> \<exists>i>n. coeff p i \<noteq> 0" | |
| 96 | unfolding degree_def by (drule not_less_Least, simp) | |
| 97 | ||
| 98 | ||
| 60500 | 99 | subsection \<open>The zero polynomial\<close> | 
| 29451 | 100 | |
| 101 | instantiation poly :: (zero) zero | |
| 102 | begin | |
| 103 | ||
| 52380 | 104 | lift_definition zero_poly :: "'a poly" | 
| 59983 
cd2efd7d06bd
replace almost_everywhere_zero by Infinite_Set.MOST
 hoelzl parents: 
59815diff
changeset | 105 | is "\<lambda>_. 0" by (rule MOST_I) simp | 
| 29451 | 106 | |
| 107 | instance .. | |
| 52380 | 108 | |
| 29451 | 109 | end | 
| 110 | ||
| 52380 | 111 | lemma coeff_0 [simp]: | 
| 112 | "coeff 0 n = 0" | |
| 113 | by transfer rule | |
| 29451 | 114 | |
| 52380 | 115 | lemma degree_0 [simp]: | 
| 116 | "degree 0 = 0" | |
| 29451 | 117 | by (rule order_antisym [OF degree_le le0]) simp | 
| 118 | ||
| 119 | lemma leading_coeff_neq_0: | |
| 52380 | 120 | assumes "p \<noteq> 0" | 
| 121 | shows "coeff p (degree p) \<noteq> 0" | |
| 29451 | 122 | proof (cases "degree p") | 
| 123 | case 0 | |
| 60500 | 124 | from \<open>p \<noteq> 0\<close> have "\<exists>n. coeff p n \<noteq> 0" | 
| 52380 | 125 | by (simp add: poly_eq_iff) | 
| 29451 | 126 | then obtain n where "coeff p n \<noteq> 0" .. | 
| 127 | hence "n \<le> degree p" by (rule le_degree) | |
| 60500 | 128 | with \<open>coeff p n \<noteq> 0\<close> and \<open>degree p = 0\<close> | 
| 29451 | 129 | show "coeff p (degree p) \<noteq> 0" by simp | 
| 130 | next | |
| 131 | case (Suc n) | |
| 60500 | 132 | from \<open>degree p = Suc n\<close> have "n < degree p" by simp | 
| 29451 | 133 | hence "\<exists>i>n. coeff p i \<noteq> 0" by (rule less_degree_imp) | 
| 134 | then obtain i where "n < i" and "coeff p i \<noteq> 0" by fast | |
| 60500 | 135 | from \<open>degree p = Suc n\<close> and \<open>n < i\<close> have "degree p \<le> i" by simp | 
| 136 | also from \<open>coeff p i \<noteq> 0\<close> have "i \<le> degree p" by (rule le_degree) | |
| 29451 | 137 | finally have "degree p = i" . | 
| 60500 | 138 | with \<open>coeff p i \<noteq> 0\<close> show "coeff p (degree p) \<noteq> 0" by simp | 
| 29451 | 139 | qed | 
| 140 | ||
| 52380 | 141 | lemma leading_coeff_0_iff [simp]: | 
| 142 | "coeff p (degree p) = 0 \<longleftrightarrow> p = 0" | |
| 29451 | 143 | by (cases "p = 0", simp, simp add: leading_coeff_neq_0) | 
| 144 | ||
| 145 | ||
| 60500 | 146 | subsection \<open>List-style constructor for polynomials\<close> | 
| 29451 | 147 | |
| 52380 | 148 | lift_definition pCons :: "'a::zero \<Rightarrow> 'a poly \<Rightarrow> 'a poly" | 
| 55415 | 149 | is "\<lambda>a p. case_nat a (coeff p)" | 
| 59983 
cd2efd7d06bd
replace almost_everywhere_zero by Infinite_Set.MOST
 hoelzl parents: 
59815diff
changeset | 150 | by (rule MOST_SucD) (simp add: MOST_coeff_eq_0) | 
| 29451 | 151 | |
| 52380 | 152 | lemmas coeff_pCons = pCons.rep_eq | 
| 29455 | 153 | |
| 52380 | 154 | lemma coeff_pCons_0 [simp]: | 
| 155 | "coeff (pCons a p) 0 = a" | |
| 156 | by transfer simp | |
| 29455 | 157 | |
| 52380 | 158 | lemma coeff_pCons_Suc [simp]: | 
| 159 | "coeff (pCons a p) (Suc n) = coeff p n" | |
| 29451 | 160 | by (simp add: coeff_pCons) | 
| 161 | ||
| 52380 | 162 | lemma degree_pCons_le: | 
| 163 | "degree (pCons a p) \<le> Suc (degree p)" | |
| 164 | by (rule degree_le) (simp add: coeff_eq_0 coeff_pCons split: nat.split) | |
| 29451 | 165 | |
| 166 | lemma degree_pCons_eq: | |
| 167 | "p \<noteq> 0 \<Longrightarrow> degree (pCons a p) = Suc (degree p)" | |
| 52380 | 168 | apply (rule order_antisym [OF degree_pCons_le]) | 
| 169 | apply (rule le_degree, simp) | |
| 170 | done | |
| 29451 | 171 | |
| 52380 | 172 | lemma degree_pCons_0: | 
| 173 | "degree (pCons a 0) = 0" | |
| 174 | apply (rule order_antisym [OF _ le0]) | |
| 175 | apply (rule degree_le, simp add: coeff_pCons split: nat.split) | |
| 176 | done | |
| 29451 | 177 | |
| 29460 
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
 huffman parents: 
29457diff
changeset | 178 | lemma degree_pCons_eq_if [simp]: | 
| 29451 | 179 | "degree (pCons a p) = (if p = 0 then 0 else Suc (degree p))" | 
| 52380 | 180 | apply (cases "p = 0", simp_all) | 
| 181 | apply (rule order_antisym [OF _ le0]) | |
| 182 | apply (rule degree_le, simp add: coeff_pCons split: nat.split) | |
| 183 | apply (rule order_antisym [OF degree_pCons_le]) | |
| 184 | apply (rule le_degree, simp) | |
| 185 | done | |
| 29451 | 186 | |
| 52380 | 187 | lemma pCons_0_0 [simp]: | 
| 188 | "pCons 0 0 = 0" | |
| 189 | by (rule poly_eqI) (simp add: coeff_pCons split: nat.split) | |
| 29451 | 190 | |
| 191 | lemma pCons_eq_iff [simp]: | |
| 192 | "pCons a p = pCons b q \<longleftrightarrow> a = b \<and> p = q" | |
| 52380 | 193 | proof safe | 
| 29451 | 194 | assume "pCons a p = pCons b q" | 
| 195 | then have "coeff (pCons a p) 0 = coeff (pCons b q) 0" by simp | |
| 196 | then show "a = b" by simp | |
| 197 | next | |
| 198 | assume "pCons a p = pCons b q" | |
| 199 | then have "\<forall>n. coeff (pCons a p) (Suc n) = | |
| 200 | coeff (pCons b q) (Suc n)" by simp | |
| 52380 | 201 | then show "p = q" by (simp add: poly_eq_iff) | 
| 29451 | 202 | qed | 
| 203 | ||
| 52380 | 204 | lemma pCons_eq_0_iff [simp]: | 
| 205 | "pCons a p = 0 \<longleftrightarrow> a = 0 \<and> p = 0" | |
| 29451 | 206 | using pCons_eq_iff [of a p 0 0] by simp | 
| 207 | ||
| 208 | lemma pCons_cases [cases type: poly]: | |
| 209 | obtains (pCons) a q where "p = pCons a q" | |
| 210 | proof | |
| 211 | show "p = pCons (coeff p 0) (Abs_poly (\<lambda>n. coeff p (Suc n)))" | |
| 52380 | 212 | by transfer | 
| 59983 
cd2efd7d06bd
replace almost_everywhere_zero by Infinite_Set.MOST
 hoelzl parents: 
59815diff
changeset | 213 | (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 | 214 | split: nat.split) | 
| 29451 | 215 | qed | 
| 216 | ||
| 217 | lemma pCons_induct [case_names 0 pCons, induct type: poly]: | |
| 218 | assumes zero: "P 0" | |
| 54856 | 219 | assumes pCons: "\<And>a p. a \<noteq> 0 \<or> p \<noteq> 0 \<Longrightarrow> P p \<Longrightarrow> P (pCons a p)" | 
| 29451 | 220 | shows "P p" | 
| 221 | proof (induct p rule: measure_induct_rule [where f=degree]) | |
| 222 | case (less p) | |
| 223 | obtain a q where "p = pCons a q" by (rule pCons_cases) | |
| 224 | have "P q" | |
| 225 | proof (cases "q = 0") | |
| 226 | case True | |
| 227 | then show "P q" by (simp add: zero) | |
| 228 | next | |
| 229 | case False | |
| 230 | then have "degree (pCons a q) = Suc (degree q)" | |
| 231 | by (rule degree_pCons_eq) | |
| 232 | then have "degree q < degree p" | |
| 60500 | 233 | using \<open>p = pCons a q\<close> by simp | 
| 29451 | 234 | then show "P q" | 
| 235 | by (rule less.hyps) | |
| 236 | qed | |
| 54856 | 237 | have "P (pCons a q)" | 
| 238 | proof (cases "a \<noteq> 0 \<or> q \<noteq> 0") | |
| 239 | case True | |
| 60500 | 240 | with \<open>P q\<close> show ?thesis by (auto intro: pCons) | 
| 54856 | 241 | next | 
| 242 | case False | |
| 243 | with zero show ?thesis by simp | |
| 244 | qed | |
| 29451 | 245 | then show ?case | 
| 60500 | 246 | using \<open>p = pCons a q\<close> by simp | 
| 29451 | 247 | qed | 
| 248 | ||
| 60570 | 249 | lemma degree_eq_zeroE: | 
| 250 | fixes p :: "'a::zero poly" | |
| 251 | assumes "degree p = 0" | |
| 252 | obtains a where "p = pCons a 0" | |
| 253 | proof - | |
| 254 | obtain a q where p: "p = pCons a q" by (cases p) | |
| 255 | with assms have "q = 0" by (cases "q = 0") simp_all | |
| 256 | with p have "p = pCons a 0" by simp | |
| 257 | with that show thesis . | |
| 258 | qed | |
| 259 | ||
| 29451 | 260 | |
| 62422 | 261 | subsection \<open>Quickcheck generator for polynomials\<close> | 
| 262 | ||
| 263 | quickcheck_generator poly constructors: "0 :: _ poly", pCons | |
| 264 | ||
| 265 | ||
| 60500 | 266 | subsection \<open>List-style syntax for polynomials\<close> | 
| 52380 | 267 | |
| 268 | syntax | |
| 269 |   "_poly" :: "args \<Rightarrow> 'a poly"  ("[:(_):]")
 | |
| 270 | ||
| 271 | translations | |
| 272 | "[:x, xs:]" == "CONST pCons x [:xs:]" | |
| 273 | "[:x:]" == "CONST pCons x 0" | |
| 274 | "[:x:]" <= "CONST pCons x (_constrain 0 t)" | |
| 275 | ||
| 276 | ||
| 60500 | 277 | subsection \<open>Representation of polynomials by lists of coefficients\<close> | 
| 52380 | 278 | |
| 279 | primrec Poly :: "'a::zero list \<Rightarrow> 'a poly" | |
| 280 | where | |
| 54855 | 281 | [code_post]: "Poly [] = 0" | 
| 282 | | [code_post]: "Poly (a # as) = pCons a (Poly as)" | |
| 52380 | 283 | |
| 284 | lemma Poly_replicate_0 [simp]: | |
| 285 | "Poly (replicate n 0) = 0" | |
| 286 | by (induct n) simp_all | |
| 287 | ||
| 288 | lemma Poly_eq_0: | |
| 289 | "Poly as = 0 \<longleftrightarrow> (\<exists>n. as = replicate n 0)" | |
| 290 | 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 | 291 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 292 | 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 | 293 | "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 | 294 | 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 | 295 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 296 | 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 | 297 | "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 | 298 | 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 | 299 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 300 | 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 | 301 | "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 | 302 | 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 | 303 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 304 | 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 | 305 | assumes "hd as = 0" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 306 | 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 | 307 | 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 | 308 | |
| 62065 | 309 | lemma degree_Poly: "degree (Poly xs) \<le> length xs" | 
| 310 | 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 | 311 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 312 | 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 | 313 | "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 | 314 | 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 | 315 | |
| 52380 | 316 | definition coeffs :: "'a poly \<Rightarrow> 'a::zero list" | 
| 317 | where | |
| 318 | "coeffs p = (if p = 0 then [] else map (\<lambda>i. coeff p i) [0 ..< Suc (degree p)])" | |
| 319 | ||
| 320 | lemma coeffs_eq_Nil [simp]: | |
| 321 | "coeffs p = [] \<longleftrightarrow> p = 0" | |
| 322 | by (simp add: coeffs_def) | |
| 323 | ||
| 324 | lemma not_0_coeffs_not_Nil: | |
| 325 | "p \<noteq> 0 \<Longrightarrow> coeffs p \<noteq> []" | |
| 326 | by simp | |
| 327 | ||
| 328 | lemma coeffs_0_eq_Nil [simp]: | |
| 329 | "coeffs 0 = []" | |
| 330 | by simp | |
| 29454 
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
 huffman parents: 
29453diff
changeset | 331 | |
| 52380 | 332 | lemma coeffs_pCons_eq_cCons [simp]: | 
| 333 | "coeffs (pCons a p) = a ## coeffs p" | |
| 334 | proof - | |
| 335 |   { fix ms :: "nat list" and f :: "nat \<Rightarrow> 'a" and x :: "'a"
 | |
| 336 | assume "\<forall>m\<in>set ms. m > 0" | |
| 55415 | 337 | 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 | 338 | by (induct ms) (auto split: nat.split) | 
| 
5fbe474b5da8
explicit theory with additional, less commonly used list operations
 haftmann parents: 
57862diff
changeset | 339 | } | 
| 52380 | 340 | note * = this | 
| 341 | show ?thesis | |
| 60570 | 342 | by (simp add: coeffs_def * upt_conv_Cons coeff_pCons map_decr_upt del: upt_Suc) | 
| 52380 | 343 | qed | 
| 344 | ||
| 62065 | 345 | lemma length_coeffs: "p \<noteq> 0 \<Longrightarrow> length (coeffs p) = degree p + 1" | 
| 346 | by (simp add: coeffs_def) | |
| 347 | ||
| 348 | lemma coeffs_nth: | |
| 349 | assumes "p \<noteq> 0" "n \<le> degree p" | |
| 350 | shows "coeffs p ! n = coeff p n" | |
| 351 | using assms unfolding coeffs_def by (auto simp del: upt_Suc) | |
| 352 | ||
| 52380 | 353 | lemma not_0_cCons_eq [simp]: | 
| 354 | "p \<noteq> 0 \<Longrightarrow> a ## coeffs p = a # coeffs p" | |
| 355 | by (simp add: cCons_def) | |
| 356 | ||
| 357 | lemma Poly_coeffs [simp, code abstype]: | |
| 358 | "Poly (coeffs p) = p" | |
| 54856 | 359 | by (induct p) auto | 
| 52380 | 360 | |
| 361 | lemma coeffs_Poly [simp]: | |
| 362 | "coeffs (Poly as) = strip_while (HOL.eq 0) as" | |
| 363 | proof (induct as) | |
| 364 | case Nil then show ?case by simp | |
| 365 | next | |
| 366 | case (Cons a as) | |
| 367 | have "(\<forall>n. as \<noteq> replicate n 0) \<longleftrightarrow> (\<exists>a\<in>set as. a \<noteq> 0)" | |
| 368 | using replicate_length_same [of as 0] by (auto dest: sym [of _ as]) | |
| 369 | with Cons show ?case by auto | |
| 370 | qed | |
| 371 | ||
| 372 | lemma last_coeffs_not_0: | |
| 373 | "p \<noteq> 0 \<Longrightarrow> last (coeffs p) \<noteq> 0" | |
| 374 | by (induct p) (auto simp add: cCons_def) | |
| 375 | ||
| 376 | lemma strip_while_coeffs [simp]: | |
| 377 | "strip_while (HOL.eq 0) (coeffs p) = coeffs p" | |
| 378 | by (cases "p = 0") (auto dest: last_coeffs_not_0 intro: strip_while_not_last) | |
| 379 | ||
| 380 | lemma coeffs_eq_iff: | |
| 381 | "p = q \<longleftrightarrow> coeffs p = coeffs q" (is "?P \<longleftrightarrow> ?Q") | |
| 382 | proof | |
| 383 | assume ?P then show ?Q by simp | |
| 384 | next | |
| 385 | assume ?Q | |
| 386 | then have "Poly (coeffs p) = Poly (coeffs q)" by simp | |
| 387 | then show ?P by simp | |
| 388 | qed | |
| 389 | ||
| 390 | lemma nth_default_coeffs_eq: | |
| 391 | "nth_default 0 (coeffs p) = coeff p" | |
| 392 | by (simp add: fun_eq_iff coeff_Poly_eq [symmetric]) | |
| 393 | ||
| 394 | lemma [code]: | |
| 395 | "coeff p = nth_default 0 (coeffs p)" | |
| 396 | by (simp add: nth_default_coeffs_eq) | |
| 397 | ||
| 398 | lemma coeffs_eqI: | |
| 399 | assumes coeff: "\<And>n. coeff p n = nth_default 0 xs n" | |
| 400 | assumes zero: "xs \<noteq> [] \<Longrightarrow> last xs \<noteq> 0" | |
| 401 | shows "coeffs p = xs" | |
| 402 | proof - | |
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 403 | from coeff have "p = Poly xs" by (simp add: poly_eq_iff) | 
| 52380 | 404 | with zero show ?thesis by simp (cases xs, simp_all) | 
| 405 | qed | |
| 406 | ||
| 407 | lemma degree_eq_length_coeffs [code]: | |
| 408 | "degree p = length (coeffs p) - 1" | |
| 409 | by (simp add: coeffs_def) | |
| 410 | ||
| 411 | lemma length_coeffs_degree: | |
| 412 | "p \<noteq> 0 \<Longrightarrow> length (coeffs p) = Suc (degree p)" | |
| 413 | by (induct p) (auto simp add: cCons_def) | |
| 414 | ||
| 415 | lemma [code abstract]: | |
| 416 | "coeffs 0 = []" | |
| 417 | by (fact coeffs_0_eq_Nil) | |
| 418 | ||
| 419 | lemma [code abstract]: | |
| 420 | "coeffs (pCons a p) = a ## coeffs p" | |
| 421 | by (fact coeffs_pCons_eq_cCons) | |
| 422 | ||
| 423 | instantiation poly :: ("{zero, equal}") equal
 | |
| 424 | begin | |
| 425 | ||
| 426 | definition | |
| 427 | [code]: "HOL.equal (p::'a poly) q \<longleftrightarrow> HOL.equal (coeffs p) (coeffs q)" | |
| 428 | ||
| 60679 | 429 | instance | 
| 430 | by standard (simp add: equal equal_poly_def coeffs_eq_iff) | |
| 52380 | 431 | |
| 432 | end | |
| 433 | ||
| 60679 | 434 | lemma [code nbe]: "HOL.equal (p :: _ poly) p \<longleftrightarrow> True" | 
| 52380 | 435 | by (fact equal_refl) | 
| 29454 
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
 huffman parents: 
29453diff
changeset | 436 | |
| 52380 | 437 | definition is_zero :: "'a::zero poly \<Rightarrow> bool" | 
| 438 | where | |
| 439 | [code]: "is_zero p \<longleftrightarrow> List.null (coeffs p)" | |
| 440 | ||
| 441 | lemma is_zero_null [code_abbrev]: | |
| 442 | "is_zero p \<longleftrightarrow> p = 0" | |
| 443 | by (simp add: is_zero_def null_def) | |
| 444 | ||
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 445 | subsubsection \<open>Reconstructing the polynomial from the list\<close> | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 446 | -- \<open>contributed by Sebastiaan J.C. Joosten and René Thiemann\<close> | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 447 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 448 | 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 | 449 | where | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 450 | [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 | 451 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 452 | 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 | 453 | "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 | 454 | by simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 455 | |
| 52380 | 456 | |
| 60500 | 457 | subsection \<open>Fold combinator for polynomials\<close> | 
| 52380 | 458 | |
| 459 | definition fold_coeffs :: "('a::zero \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a poly \<Rightarrow> 'b \<Rightarrow> 'b"
 | |
| 460 | where | |
| 461 | "fold_coeffs f p = foldr f (coeffs p)" | |
| 462 | ||
| 463 | lemma fold_coeffs_0_eq [simp]: | |
| 464 | "fold_coeffs f 0 = id" | |
| 465 | by (simp add: fold_coeffs_def) | |
| 466 | ||
| 467 | lemma fold_coeffs_pCons_eq [simp]: | |
| 468 | "f 0 = id \<Longrightarrow> fold_coeffs f (pCons a p) = f a \<circ> fold_coeffs f p" | |
| 469 | 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 | 470 | |
| 52380 | 471 | lemma fold_coeffs_pCons_0_0_eq [simp]: | 
| 472 | "fold_coeffs f (pCons 0 0) = id" | |
| 473 | by (simp add: fold_coeffs_def) | |
| 474 | ||
| 475 | lemma fold_coeffs_pCons_coeff_not_0_eq [simp]: | |
| 476 | "a \<noteq> 0 \<Longrightarrow> fold_coeffs f (pCons a p) = f a \<circ> fold_coeffs f p" | |
| 477 | by (simp add: fold_coeffs_def) | |
| 478 | ||
| 479 | lemma fold_coeffs_pCons_not_0_0_eq [simp]: | |
| 480 | "p \<noteq> 0 \<Longrightarrow> fold_coeffs f (pCons a p) = f a \<circ> fold_coeffs f p" | |
| 481 | by (simp add: fold_coeffs_def) | |
| 482 | ||
| 60500 | 483 | subsection \<open>Canonical morphism on polynomials -- evaluation\<close> | 
| 52380 | 484 | |
| 485 | definition poly :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a" | |
| 486 | where | |
| 61585 | 487 | "poly p = fold_coeffs (\<lambda>a f x. a + x * f x) p (\<lambda>x. 0)" \<comment> \<open>The Horner Schema\<close> | 
| 52380 | 488 | |
| 489 | lemma poly_0 [simp]: | |
| 490 | "poly 0 x = 0" | |
| 491 | 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 | 492 | |
| 52380 | 493 | lemma poly_pCons [simp]: | 
| 494 | "poly (pCons a p) x = a + x * poly p x" | |
| 495 | 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 | 496 | |
| 62065 | 497 | lemma poly_altdef: | 
| 498 |   "poly p (x :: 'a :: {comm_semiring_0, semiring_1}) = (\<Sum>i\<le>degree p. coeff p i * x ^ i)"
 | |
| 499 | proof (induction p rule: pCons_induct) | |
| 500 | case (pCons a p) | |
| 501 | show ?case | |
| 502 | proof (cases "p = 0") | |
| 503 | case False | |
| 504 | let ?p' = "pCons a p" | |
| 505 | note poly_pCons[of a p x] | |
| 506 | also note pCons.IH | |
| 507 | also have "a + x * (\<Sum>i\<le>degree p. coeff p i * x ^ i) = | |
| 508 | coeff ?p' 0 * x^0 + (\<Sum>i\<le>degree p. coeff ?p' (Suc i) * x^Suc i)" | |
| 509 | by (simp add: field_simps setsum_right_distrib coeff_pCons) | |
| 510 | also note setsum_atMost_Suc_shift[symmetric] | |
| 62072 | 511 | also note degree_pCons_eq[OF \<open>p \<noteq> 0\<close>, of a, symmetric] | 
| 62065 | 512 | finally show ?thesis . | 
| 513 | qed simp | |
| 514 | qed simp | |
| 515 | ||
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 516 | 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 | 517 | 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 | 518 | |
| 29454 
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
 huffman parents: 
29453diff
changeset | 519 | |
| 60500 | 520 | subsection \<open>Monomials\<close> | 
| 29451 | 521 | |
| 52380 | 522 | lift_definition monom :: "'a \<Rightarrow> nat \<Rightarrow> 'a::zero poly" | 
| 523 | 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 | 524 | by (simp add: MOST_iff_cofinite) | 
| 52380 | 525 | |
| 526 | lemma coeff_monom [simp]: | |
| 527 | "coeff (monom a m) n = (if m = n then a else 0)" | |
| 528 | by transfer rule | |
| 29451 | 529 | |
| 52380 | 530 | lemma monom_0: | 
| 531 | "monom a 0 = pCons a 0" | |
| 532 | by (rule poly_eqI) (simp add: coeff_pCons split: nat.split) | |
| 29451 | 533 | |
| 52380 | 534 | lemma monom_Suc: | 
| 535 | "monom a (Suc n) = pCons 0 (monom a n)" | |
| 536 | by (rule poly_eqI) (simp add: coeff_pCons split: nat.split) | |
| 29451 | 537 | |
| 538 | lemma monom_eq_0 [simp]: "monom 0 n = 0" | |
| 52380 | 539 | by (rule poly_eqI) simp | 
| 29451 | 540 | |
| 541 | lemma monom_eq_0_iff [simp]: "monom a n = 0 \<longleftrightarrow> a = 0" | |
| 52380 | 542 | by (simp add: poly_eq_iff) | 
| 29451 | 543 | |
| 544 | lemma monom_eq_iff [simp]: "monom a n = monom b n \<longleftrightarrow> a = b" | |
| 52380 | 545 | by (simp add: poly_eq_iff) | 
| 29451 | 546 | |
| 547 | lemma degree_monom_le: "degree (monom a n) \<le> n" | |
| 548 | by (rule degree_le, simp) | |
| 549 | ||
| 550 | lemma degree_monom_eq: "a \<noteq> 0 \<Longrightarrow> degree (monom a n) = n" | |
| 551 | apply (rule order_antisym [OF degree_monom_le]) | |
| 552 | apply (rule le_degree, simp) | |
| 553 | done | |
| 554 | ||
| 52380 | 555 | lemma coeffs_monom [code abstract]: | 
| 556 | "coeffs (monom a n) = (if a = 0 then [] else replicate n 0 @ [a])" | |
| 557 | by (induct n) (simp_all add: monom_0 monom_Suc) | |
| 558 | ||
| 559 | lemma fold_coeffs_monom [simp]: | |
| 560 | "a \<noteq> 0 \<Longrightarrow> fold_coeffs f (monom a n) = f 0 ^^ n \<circ> f a" | |
| 561 | by (simp add: fold_coeffs_def coeffs_monom fun_eq_iff) | |
| 562 | ||
| 563 | lemma poly_monom: | |
| 564 |   fixes a x :: "'a::{comm_semiring_1}"
 | |
| 565 | shows "poly (monom a n) x = a * x ^ n" | |
| 566 | by (cases "a = 0", simp_all) | |
| 567 | (induct n, simp_all add: mult.left_commute poly_def) | |
| 568 | ||
| 62065 | 569 | |
| 60500 | 570 | subsection \<open>Addition and subtraction\<close> | 
| 29451 | 571 | |
| 572 | instantiation poly :: (comm_monoid_add) comm_monoid_add | |
| 573 | begin | |
| 574 | ||
| 52380 | 575 | lift_definition plus_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" | 
| 576 | 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 | 577 | proof - | 
| 60679 | 578 | fix q p :: "'a poly" | 
| 579 | 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 | 580 | using MOST_coeff_eq_0[of p] MOST_coeff_eq_0[of q] by eventually_elim simp | 
| 52380 | 581 | qed | 
| 29451 | 582 | |
| 60679 | 583 | lemma coeff_add [simp]: "coeff (p + q) n = coeff p n + coeff q n" | 
| 52380 | 584 | by (simp add: plus_poly.rep_eq) | 
| 29451 | 585 | |
| 60679 | 586 | instance | 
| 587 | proof | |
| 29451 | 588 | fix p q r :: "'a poly" | 
| 589 | show "(p + q) + r = p + (q + r)" | |
| 57512 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 haftmann parents: 
57482diff
changeset | 590 | by (simp add: poly_eq_iff add.assoc) | 
| 29451 | 591 | show "p + q = q + p" | 
| 57512 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 haftmann parents: 
57482diff
changeset | 592 | by (simp add: poly_eq_iff add.commute) | 
| 29451 | 593 | show "0 + p = p" | 
| 52380 | 594 | by (simp add: poly_eq_iff) | 
| 29451 | 595 | qed | 
| 596 | ||
| 597 | end | |
| 598 | ||
| 59815 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 599 | instantiation poly :: (cancel_comm_monoid_add) cancel_comm_monoid_add | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 600 | begin | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 601 | |
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 602 | lift_definition minus_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 603 | 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 | 604 | proof - | 
| 60679 | 605 | fix q p :: "'a poly" | 
| 606 | 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 | 607 | 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 | 608 | qed | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 609 | |
| 60679 | 610 | 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 | 611 | by (simp add: minus_poly.rep_eq) | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 612 | |
| 60679 | 613 | instance | 
| 614 | proof | |
| 29540 | 615 | fix p q r :: "'a poly" | 
| 59815 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 616 | show "p + q - p = q" | 
| 52380 | 617 | by (simp add: poly_eq_iff) | 
| 59815 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 618 | show "p - q - r = p - (q + r)" | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 619 | by (simp add: poly_eq_iff diff_diff_eq) | 
| 29540 | 620 | qed | 
| 621 | ||
| 59815 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 622 | end | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 623 | |
| 29451 | 624 | instantiation poly :: (ab_group_add) ab_group_add | 
| 625 | begin | |
| 626 | ||
| 52380 | 627 | lift_definition uminus_poly :: "'a poly \<Rightarrow> 'a poly" | 
| 628 | 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 | 629 | proof - | 
| 60679 | 630 | fix p :: "'a poly" | 
| 631 | 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 | 632 | using MOST_coeff_eq_0 by simp | 
| 52380 | 633 | qed | 
| 29451 | 634 | |
| 635 | lemma coeff_minus [simp]: "coeff (- p) n = - coeff p n" | |
| 52380 | 636 | by (simp add: uminus_poly.rep_eq) | 
| 29451 | 637 | |
| 60679 | 638 | instance | 
| 639 | proof | |
| 29451 | 640 | fix p q :: "'a poly" | 
| 641 | show "- p + p = 0" | |
| 52380 | 642 | by (simp add: poly_eq_iff) | 
| 29451 | 643 | show "p - q = p + - q" | 
| 54230 
b1d955791529
more simplification rules on unary and binary minus
 haftmann parents: 
52380diff
changeset | 644 | by (simp add: poly_eq_iff) | 
| 29451 | 645 | qed | 
| 646 | ||
| 647 | end | |
| 648 | ||
| 649 | lemma add_pCons [simp]: | |
| 650 | "pCons a p + pCons b q = pCons (a + b) (p + q)" | |
| 52380 | 651 | by (rule poly_eqI, simp add: coeff_pCons split: nat.split) | 
| 29451 | 652 | |
| 653 | lemma minus_pCons [simp]: | |
| 654 | "- pCons a p = pCons (- a) (- p)" | |
| 52380 | 655 | by (rule poly_eqI, simp add: coeff_pCons split: nat.split) | 
| 29451 | 656 | |
| 657 | lemma diff_pCons [simp]: | |
| 658 | "pCons a p - pCons b q = pCons (a - b) (p - q)" | |
| 52380 | 659 | by (rule poly_eqI, simp add: coeff_pCons split: nat.split) | 
| 29451 | 660 | |
| 29539 | 661 | lemma degree_add_le_max: "degree (p + q) \<le> max (degree p) (degree q)" | 
| 29451 | 662 | by (rule degree_le, auto simp add: coeff_eq_0) | 
| 663 | ||
| 29539 | 664 | lemma degree_add_le: | 
| 665 | "\<lbrakk>degree p \<le> n; degree q \<le> n\<rbrakk> \<Longrightarrow> degree (p + q) \<le> n" | |
| 666 | by (auto intro: order_trans degree_add_le_max) | |
| 667 | ||
| 29453 | 668 | lemma degree_add_less: | 
| 669 | "\<lbrakk>degree p < n; degree q < n\<rbrakk> \<Longrightarrow> degree (p + q) < n" | |
| 29539 | 670 | by (auto intro: le_less_trans degree_add_le_max) | 
| 29453 | 671 | |
| 29451 | 672 | lemma degree_add_eq_right: | 
| 673 | "degree p < degree q \<Longrightarrow> degree (p + q) = degree q" | |
| 674 | apply (cases "q = 0", simp) | |
| 675 | apply (rule order_antisym) | |
| 29539 | 676 | apply (simp add: degree_add_le) | 
| 29451 | 677 | apply (rule le_degree) | 
| 678 | apply (simp add: coeff_eq_0) | |
| 679 | done | |
| 680 | ||
| 681 | lemma degree_add_eq_left: | |
| 682 | "degree q < degree p \<Longrightarrow> degree (p + q) = degree p" | |
| 683 | 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 | 684 | by (simp add: add.commute) | 
| 29451 | 685 | |
| 59815 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 686 | lemma degree_minus [simp]: | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 687 | "degree (- p) = degree p" | 
| 29451 | 688 | unfolding degree_def by simp | 
| 689 | ||
| 59815 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 690 | lemma degree_diff_le_max: | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 691 | fixes p q :: "'a :: ab_group_add poly" | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 692 | shows "degree (p - q) \<le> max (degree p) (degree q)" | 
| 29451 | 693 | using degree_add_le [where p=p and q="-q"] | 
| 54230 
b1d955791529
more simplification rules on unary and binary minus
 haftmann parents: 
52380diff
changeset | 694 | by simp | 
| 29451 | 695 | |
| 29539 | 696 | lemma degree_diff_le: | 
| 59815 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 697 | fixes p q :: "'a :: ab_group_add poly" | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 698 | assumes "degree p \<le> n" and "degree q \<le> n" | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 699 | shows "degree (p - q) \<le> n" | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 700 | using assms degree_add_le [of p n "- q"] by simp | 
| 29539 | 701 | |
| 29453 | 702 | lemma degree_diff_less: | 
| 59815 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 703 | fixes p q :: "'a :: ab_group_add poly" | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 704 | assumes "degree p < n" and "degree q < n" | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 705 | shows "degree (p - q) < n" | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59557diff
changeset | 706 | using assms degree_add_less [of p n "- q"] by simp | 
| 29453 | 707 | |
| 29451 | 708 | lemma add_monom: "monom a n + monom b n = monom (a + b) n" | 
| 52380 | 709 | by (rule poly_eqI) simp | 
| 29451 | 710 | |
| 711 | lemma diff_monom: "monom a n - monom b n = monom (a - b) n" | |
| 52380 | 712 | by (rule poly_eqI) simp | 
| 29451 | 713 | |
| 714 | lemma minus_monom: "- monom a n = monom (-a) n" | |
| 52380 | 715 | by (rule poly_eqI) simp | 
| 29451 | 716 | |
| 717 | lemma coeff_setsum: "coeff (\<Sum>x\<in>A. p x) i = (\<Sum>x\<in>A. coeff (p x) i)" | |
| 718 | by (cases "finite A", induct set: finite, simp_all) | |
| 719 | ||
| 720 | lemma monom_setsum: "monom (\<Sum>x\<in>A. a x) n = (\<Sum>x\<in>A. monom (a x) n)" | |
| 52380 | 721 | by (rule poly_eqI) (simp add: coeff_setsum) | 
| 722 | ||
| 723 | fun plus_coeffs :: "'a::comm_monoid_add list \<Rightarrow> 'a list \<Rightarrow> 'a list" | |
| 724 | where | |
| 725 | "plus_coeffs xs [] = xs" | |
| 726 | | "plus_coeffs [] ys = ys" | |
| 727 | | "plus_coeffs (x # xs) (y # ys) = (x + y) ## plus_coeffs xs ys" | |
| 728 | ||
| 729 | lemma coeffs_plus_eq_plus_coeffs [code abstract]: | |
| 730 | "coeffs (p + q) = plus_coeffs (coeffs p) (coeffs q)" | |
| 731 | proof - | |
| 732 |   { fix xs ys :: "'a list" and n
 | |
| 733 | have "nth_default 0 (plus_coeffs xs ys) n = nth_default 0 xs n + nth_default 0 ys n" | |
| 734 | proof (induct xs ys arbitrary: n rule: plus_coeffs.induct) | |
| 60679 | 735 | case (3 x xs y ys n) | 
| 736 | then show ?case by (cases n) (auto simp add: cCons_def) | |
| 52380 | 737 | qed simp_all } | 
| 738 | note * = this | |
| 739 |   { fix xs ys :: "'a list"
 | |
| 740 | assume "xs \<noteq> [] \<Longrightarrow> last xs \<noteq> 0" and "ys \<noteq> [] \<Longrightarrow> last ys \<noteq> 0" | |
| 741 | moreover assume "plus_coeffs xs ys \<noteq> []" | |
| 742 | ultimately have "last (plus_coeffs xs ys) \<noteq> 0" | |
| 743 | proof (induct xs ys rule: plus_coeffs.induct) | |
| 744 | case (3 x xs y ys) then show ?case by (auto simp add: cCons_def) metis | |
| 745 | qed simp_all } | |
| 746 | note ** = this | |
| 747 | show ?thesis | |
| 748 | apply (rule coeffs_eqI) | |
| 749 | apply (simp add: * nth_default_coeffs_eq) | |
| 750 | apply (rule **) | |
| 751 | apply (auto dest: last_coeffs_not_0) | |
| 752 | done | |
| 753 | qed | |
| 754 | ||
| 755 | lemma coeffs_uminus [code abstract]: | |
| 756 | "coeffs (- p) = map (\<lambda>a. - a) (coeffs p)" | |
| 757 | by (rule coeffs_eqI) | |
| 758 | (simp_all add: not_0_coeffs_not_Nil last_map last_coeffs_not_0 nth_default_map_eq nth_default_coeffs_eq) | |
| 759 | ||
| 760 | lemma [code]: | |
| 761 | fixes p q :: "'a::ab_group_add poly" | |
| 762 | shows "p - q = p + - q" | |
| 59557 | 763 | by (fact diff_conv_add_uminus) | 
| 52380 | 764 | |
| 765 | lemma poly_add [simp]: "poly (p + q) x = poly p x + poly q x" | |
| 766 | apply (induct p arbitrary: q, simp) | |
| 767 | apply (case_tac q, simp, simp add: algebra_simps) | |
| 768 | done | |
| 769 | ||
| 770 | lemma poly_minus [simp]: | |
| 771 | fixes x :: "'a::comm_ring" | |
| 772 | shows "poly (- p) x = - poly p x" | |
| 773 | by (induct p) simp_all | |
| 774 | ||
| 775 | lemma poly_diff [simp]: | |
| 776 | fixes x :: "'a::comm_ring" | |
| 777 | 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 | 778 | using poly_add [of p "- q" x] by simp | 
| 52380 | 779 | |
| 780 | lemma poly_setsum: "poly (\<Sum>k\<in>A. p k) x = (\<Sum>k\<in>A. poly (p k) x)" | |
| 781 | by (induct A rule: infinite_finite_induct) simp_all | |
| 29451 | 782 | |
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 783 | lemma degree_setsum_le: "finite S \<Longrightarrow> (\<And> p . p \<in> S \<Longrightarrow> degree (f p) \<le> n) | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 784 | \<Longrightarrow> degree (setsum f S) \<le> n" | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 785 | 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 | 786 | case (insert p S) | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 787 | hence "degree (setsum f S) \<le> n" "degree (f p) \<le> n" by auto | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 788 | thus ?case unfolding setsum.insert[OF insert(1-2)] by (metis degree_add_le) | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 789 | qed simp | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 790 | |
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 791 | 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 | 792 | 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 | 793 | 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 | 794 | proof - | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 795 |   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 | 796 | by auto | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 797 | show ?thesis | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 798 | using n by (simp add: poly_eq_iff coeff_setsum coeff_eq_0 setsum.If_cases eq | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 799 | 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 | 800 | qed | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 801 | |
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 802 | 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 | 803 | 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 | 804 | |
| 62065 | 805 | lemma Poly_snoc: "Poly (xs @ [x]) = Poly xs + monom x (length xs)" | 
| 806 | by (induction xs) (simp_all add: monom_0 monom_Suc) | |
| 807 | ||
| 29451 | 808 | |
| 60500 | 809 | subsection \<open>Multiplication by a constant, polynomial multiplication and the unit polynomial\<close> | 
| 29451 | 810 | |
| 52380 | 811 | lift_definition smult :: "'a::comm_semiring_0 \<Rightarrow> 'a poly \<Rightarrow> 'a poly" | 
| 812 | 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 | 813 | proof - | 
| 
1fa1023b13b9
move MOST and INFM in Infinite_Set to Filter; change them to abbreviations over the cofinite filter
 hoelzl parents: 
59983diff
changeset | 814 | 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 | 815 | using MOST_coeff_eq_0[of p] by eventually_elim simp | 
| 52380 | 816 | qed | 
| 29451 | 817 | |
| 52380 | 818 | lemma coeff_smult [simp]: | 
| 819 | "coeff (smult a p) n = a * coeff p n" | |
| 820 | by (simp add: smult.rep_eq) | |
| 29451 | 821 | |
| 822 | lemma degree_smult_le: "degree (smult a p) \<le> degree p" | |
| 823 | by (rule degree_le, simp add: coeff_eq_0) | |
| 824 | ||
| 29472 | 825 | 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 | 826 | by (rule poly_eqI, simp add: mult.assoc) | 
| 29451 | 827 | |
| 828 | lemma smult_0_right [simp]: "smult a 0 = 0" | |
| 52380 | 829 | by (rule poly_eqI, simp) | 
| 29451 | 830 | |
| 831 | lemma smult_0_left [simp]: "smult 0 p = 0" | |
| 52380 | 832 | by (rule poly_eqI, simp) | 
| 29451 | 833 | |
| 834 | lemma smult_1_left [simp]: "smult (1::'a::comm_semiring_1) p = p" | |
| 52380 | 835 | by (rule poly_eqI, simp) | 
| 29451 | 836 | |
| 837 | lemma smult_add_right: | |
| 838 | "smult a (p + q) = smult a p + smult a q" | |
| 52380 | 839 | by (rule poly_eqI, simp add: algebra_simps) | 
| 29451 | 840 | |
| 841 | lemma smult_add_left: | |
| 842 | "smult (a + b) p = smult a p + smult b p" | |
| 52380 | 843 | by (rule poly_eqI, simp add: algebra_simps) | 
| 29451 | 844 | |
| 29457 | 845 | lemma smult_minus_right [simp]: | 
| 29451 | 846 | "smult (a::'a::comm_ring) (- p) = - smult a p" | 
| 52380 | 847 | by (rule poly_eqI, simp) | 
| 29451 | 848 | |
| 29457 | 849 | lemma smult_minus_left [simp]: | 
| 29451 | 850 | "smult (- a::'a::comm_ring) p = - smult a p" | 
| 52380 | 851 | by (rule poly_eqI, simp) | 
| 29451 | 852 | |
| 853 | lemma smult_diff_right: | |
| 854 | "smult (a::'a::comm_ring) (p - q) = smult a p - smult a q" | |
| 52380 | 855 | by (rule poly_eqI, simp add: algebra_simps) | 
| 29451 | 856 | |
| 857 | lemma smult_diff_left: | |
| 858 | "smult (a - b::'a::comm_ring) p = smult a p - smult b p" | |
| 52380 | 859 | by (rule poly_eqI, simp add: algebra_simps) | 
| 29451 | 860 | |
| 29472 | 861 | lemmas smult_distribs = | 
| 862 | smult_add_left smult_add_right | |
| 863 | smult_diff_left smult_diff_right | |
| 864 | ||
| 29451 | 865 | lemma smult_pCons [simp]: | 
| 866 | "smult a (pCons b p) = pCons (a * b) (smult a p)" | |
| 52380 | 867 | by (rule poly_eqI, simp add: coeff_pCons split: nat.split) | 
| 29451 | 868 | |
| 869 | lemma smult_monom: "smult a (monom b n) = monom (a * b) n" | |
| 870 | by (induct n, simp add: monom_0, simp add: monom_Suc) | |
| 871 | ||
| 29659 | 872 | lemma degree_smult_eq [simp]: | 
| 873 | fixes a :: "'a::idom" | |
| 874 | shows "degree (smult a p) = (if a = 0 then 0 else degree p)" | |
| 875 | by (cases "a = 0", simp, simp add: degree_def) | |
| 876 | ||
| 877 | lemma smult_eq_0_iff [simp]: | |
| 878 | fixes a :: "'a::idom" | |
| 879 | shows "smult a p = 0 \<longleftrightarrow> a = 0 \<or> p = 0" | |
| 52380 | 880 | by (simp add: poly_eq_iff) | 
| 29451 | 881 | |
| 52380 | 882 | lemma coeffs_smult [code abstract]: | 
| 883 | fixes p :: "'a::idom poly" | |
| 884 | shows "coeffs (smult a p) = (if a = 0 then [] else map (Groups.times a) (coeffs p))" | |
| 885 | by (rule coeffs_eqI) | |
| 886 | (auto simp add: not_0_coeffs_not_Nil last_map last_coeffs_not_0 nth_default_map_eq nth_default_coeffs_eq) | |
| 29451 | 887 | |
| 888 | instantiation poly :: (comm_semiring_0) comm_semiring_0 | |
| 889 | begin | |
| 890 | ||
| 891 | definition | |
| 52380 | 892 | "p * q = fold_coeffs (\<lambda>a p. smult a q + pCons 0 p) p 0" | 
| 29474 | 893 | |
| 894 | lemma mult_poly_0_left: "(0::'a poly) * q = 0" | |
| 52380 | 895 | by (simp add: times_poly_def) | 
| 29474 | 896 | |
| 897 | lemma mult_pCons_left [simp]: | |
| 898 | "pCons a p * q = smult a q + pCons 0 (p * q)" | |
| 52380 | 899 | by (cases "p = 0 \<and> a = 0") (auto simp add: times_poly_def) | 
| 29474 | 900 | |
| 901 | lemma mult_poly_0_right: "p * (0::'a poly) = 0" | |
| 52380 | 902 | by (induct p) (simp add: mult_poly_0_left, simp) | 
| 29451 | 903 | |
| 29474 | 904 | lemma mult_pCons_right [simp]: | 
| 905 | "p * pCons a q = smult a p + pCons 0 (p * q)" | |
| 52380 | 906 | by (induct p) (simp add: mult_poly_0_left, simp add: algebra_simps) | 
| 29474 | 907 | |
| 908 | lemmas mult_poly_0 = mult_poly_0_left mult_poly_0_right | |
| 909 | ||
| 52380 | 910 | lemma mult_smult_left [simp]: | 
| 911 | "smult a p * q = smult a (p * q)" | |
| 912 | by (induct p) (simp add: mult_poly_0, simp add: smult_add_right) | |
| 29474 | 913 | |
| 52380 | 914 | lemma mult_smult_right [simp]: | 
| 915 | "p * smult a q = smult a (p * q)" | |
| 916 | by (induct q) (simp add: mult_poly_0, simp add: smult_add_right) | |
| 29474 | 917 | |
| 918 | lemma mult_poly_add_left: | |
| 919 | fixes p q r :: "'a poly" | |
| 920 | shows "(p + q) * r = p * r + q * r" | |
| 52380 | 921 | by (induct r) (simp add: mult_poly_0, simp add: smult_distribs algebra_simps) | 
| 29451 | 922 | |
| 60679 | 923 | instance | 
| 924 | proof | |
| 29451 | 925 | fix p q r :: "'a poly" | 
| 926 | show 0: "0 * p = 0" | |
| 29474 | 927 | by (rule mult_poly_0_left) | 
| 29451 | 928 | show "p * 0 = 0" | 
| 29474 | 929 | by (rule mult_poly_0_right) | 
| 29451 | 930 | show "(p + q) * r = p * r + q * r" | 
| 29474 | 931 | by (rule mult_poly_add_left) | 
| 29451 | 932 | show "(p * q) * r = p * (q * r)" | 
| 29474 | 933 | by (induct p, simp add: mult_poly_0, simp add: mult_poly_add_left) | 
| 29451 | 934 | show "p * q = q * p" | 
| 29474 | 935 | by (induct p, simp add: mult_poly_0, simp) | 
| 29451 | 936 | qed | 
| 937 | ||
| 938 | end | |
| 939 | ||
| 29540 | 940 | instance poly :: (comm_semiring_0_cancel) comm_semiring_0_cancel .. | 
| 941 | ||
| 29474 | 942 | lemma coeff_mult: | 
| 943 | "coeff (p * q) n = (\<Sum>i\<le>n. coeff p i * coeff q (n-i))" | |
| 944 | proof (induct p arbitrary: n) | |
| 945 | case 0 show ?case by simp | |
| 946 | next | |
| 947 | case (pCons a p n) thus ?case | |
| 948 | by (cases n, simp, simp add: setsum_atMost_Suc_shift | |
| 949 | del: setsum_atMost_Suc) | |
| 950 | qed | |
| 29451 | 951 | |
| 29474 | 952 | lemma degree_mult_le: "degree (p * q) \<le> degree p + degree q" | 
| 953 | apply (rule degree_le) | |
| 954 | apply (induct p) | |
| 955 | apply simp | |
| 956 | apply (simp add: coeff_eq_0 coeff_pCons split: nat.split) | |
| 29451 | 957 | done | 
| 958 | ||
| 959 | lemma mult_monom: "monom a m * monom b n = monom (a * b) (m + n)" | |
| 60679 | 960 | by (induct m) (simp add: monom_0 smult_monom, simp add: monom_Suc) | 
| 29451 | 961 | |
| 962 | instantiation poly :: (comm_semiring_1) comm_semiring_1 | |
| 963 | begin | |
| 964 | ||
| 60679 | 965 | definition one_poly_def: "1 = pCons 1 0" | 
| 29451 | 966 | |
| 60679 | 967 | instance | 
| 968 | proof | |
| 969 | show "1 * p = p" for p :: "'a poly" | |
| 52380 | 970 | unfolding one_poly_def by simp | 
| 29451 | 971 | show "0 \<noteq> (1::'a poly)" | 
| 972 | unfolding one_poly_def by simp | |
| 973 | qed | |
| 974 | ||
| 975 | end | |
| 976 | ||
| 52380 | 977 | instance poly :: (comm_ring) comm_ring .. | 
| 978 | ||
| 979 | instance poly :: (comm_ring_1) comm_ring_1 .. | |
| 980 | ||
| 29451 | 981 | lemma coeff_1 [simp]: "coeff 1 n = (if n = 0 then 1 else 0)" | 
| 982 | unfolding one_poly_def | |
| 983 | by (simp add: coeff_pCons split: nat.split) | |
| 984 | ||
| 60570 | 985 | lemma monom_eq_1 [simp]: | 
| 986 | "monom 1 0 = 1" | |
| 987 | by (simp add: monom_0 one_poly_def) | |
| 988 | ||
| 29451 | 989 | lemma degree_1 [simp]: "degree 1 = 0" | 
| 990 | unfolding one_poly_def | |
| 991 | by (rule degree_pCons_0) | |
| 992 | ||
| 52380 | 993 | lemma coeffs_1_eq [simp, code abstract]: | 
| 994 | "coeffs 1 = [1]" | |
| 995 | by (simp add: one_poly_def) | |
| 996 | ||
| 997 | lemma degree_power_le: | |
| 998 | "degree (p ^ n) \<le> degree p * n" | |
| 999 | by (induct n) (auto intro: order_trans degree_mult_le) | |
| 1000 | ||
| 1001 | lemma poly_smult [simp]: | |
| 1002 | "poly (smult a p) x = a * poly p x" | |
| 1003 | by (induct p, simp, simp add: algebra_simps) | |
| 1004 | ||
| 1005 | lemma poly_mult [simp]: | |
| 1006 | "poly (p * q) x = poly p x * poly q x" | |
| 1007 | by (induct p, simp_all, simp add: algebra_simps) | |
| 1008 | ||
| 1009 | lemma poly_1 [simp]: | |
| 1010 | "poly 1 x = 1" | |
| 1011 | by (simp add: one_poly_def) | |
| 1012 | ||
| 1013 | lemma poly_power [simp]: | |
| 1014 |   fixes p :: "'a::{comm_semiring_1} poly"
 | |
| 1015 | shows "poly (p ^ n) x = poly p x ^ n" | |
| 1016 | by (induct n) simp_all | |
| 1017 | ||
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1018 | lemma poly_setprod: "poly (\<Prod>k\<in>A. p k) x = (\<Prod>k\<in>A. poly (p k) x)" | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1019 | 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 | 1020 | |
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1021 | lemma degree_setprod_setsum_le: "finite S \<Longrightarrow> degree (setprod f S) \<le> setsum (degree o f) S" | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1022 | 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 | 1023 | case (insert a S) | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1024 | show ?case unfolding setprod.insert[OF insert(1-2)] setsum.insert[OF insert(1-2)] | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1025 | 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 | 1026 | qed simp | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1027 | |
| 62065 | 1028 | subsection \<open>Conversions from natural numbers\<close> | 
| 1029 | ||
| 1030 | lemma of_nat_poly: "of_nat n = [:of_nat n :: 'a :: comm_semiring_1:]" | |
| 1031 | proof (induction n) | |
| 1032 | case (Suc n) | |
| 1033 | hence "of_nat (Suc n) = 1 + (of_nat n :: 'a poly)" | |
| 1034 | by simp | |
| 1035 | also have "(of_nat n :: 'a poly) = [: of_nat n :]" | |
| 1036 | by (subst Suc) (rule refl) | |
| 1037 | also have "1 = [:1:]" by (simp add: one_poly_def) | |
| 1038 | finally show ?case by (subst (asm) add_pCons) simp | |
| 1039 | qed simp | |
| 1040 | ||
| 1041 | lemma degree_of_nat [simp]: "degree (of_nat n) = 0" | |
| 1042 | by (simp add: of_nat_poly) | |
| 1043 | ||
| 1044 | lemma degree_numeral [simp]: "degree (numeral n) = 0" | |
| 1045 | by (subst of_nat_numeral [symmetric], subst of_nat_poly) simp | |
| 1046 | ||
| 1047 | lemma numeral_poly: "numeral n = [:numeral n:]" | |
| 1048 | by (subst of_nat_numeral [symmetric], subst of_nat_poly) simp | |
| 52380 | 1049 | |
| 60500 | 1050 | subsection \<open>Lemmas about divisibility\<close> | 
| 29979 | 1051 | |
| 1052 | lemma dvd_smult: "p dvd q \<Longrightarrow> p dvd smult a q" | |
| 1053 | proof - | |
| 1054 | assume "p dvd q" | |
| 1055 | then obtain k where "q = p * k" .. | |
| 1056 | then have "smult a q = p * smult a k" by simp | |
| 1057 | then show "p dvd smult a q" .. | |
| 1058 | qed | |
| 1059 | ||
| 1060 | lemma dvd_smult_cancel: | |
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1061 | fixes a :: "'a :: field" | 
| 29979 | 1062 | shows "p dvd smult a q \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> p dvd q" | 
| 1063 | by (drule dvd_smult [where a="inverse a"]) simp | |
| 1064 | ||
| 1065 | lemma dvd_smult_iff: | |
| 1066 | fixes a :: "'a::field" | |
| 1067 | shows "a \<noteq> 0 \<Longrightarrow> p dvd smult a q \<longleftrightarrow> p dvd q" | |
| 1068 | by (safe elim!: dvd_smult dvd_smult_cancel) | |
| 1069 | ||
| 31663 | 1070 | lemma smult_dvd_cancel: | 
| 1071 | "smult a p dvd q \<Longrightarrow> p dvd q" | |
| 1072 | proof - | |
| 1073 | assume "smult a p dvd q" | |
| 1074 | then obtain k where "q = smult a p * k" .. | |
| 1075 | then have "q = p * smult a k" by simp | |
| 1076 | then show "p dvd q" .. | |
| 1077 | qed | |
| 1078 | ||
| 1079 | lemma smult_dvd: | |
| 1080 | fixes a :: "'a::field" | |
| 1081 | shows "p dvd q \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> smult a p dvd q" | |
| 1082 | by (rule smult_dvd_cancel [where a="inverse a"]) simp | |
| 1083 | ||
| 1084 | lemma smult_dvd_iff: | |
| 1085 | fixes a :: "'a::field" | |
| 1086 | shows "smult a p dvd q \<longleftrightarrow> (if a = 0 then q = 0 else p dvd q)" | |
| 1087 | by (auto elim: smult_dvd smult_dvd_cancel) | |
| 1088 | ||
| 29451 | 1089 | |
| 60500 | 1090 | subsection \<open>Polynomials form an integral domain\<close> | 
| 29451 | 1091 | |
| 1092 | lemma coeff_mult_degree_sum: | |
| 1093 | "coeff (p * q) (degree p + degree q) = | |
| 1094 | coeff p (degree p) * coeff q (degree q)" | |
| 29471 | 1095 | by (induct p, simp, simp add: coeff_eq_0) | 
| 29451 | 1096 | |
| 1097 | instance poly :: (idom) idom | |
| 1098 | proof | |
| 1099 | fix p q :: "'a poly" | |
| 1100 | assume "p \<noteq> 0" and "q \<noteq> 0" | |
| 1101 | have "coeff (p * q) (degree p + degree q) = | |
| 1102 | coeff p (degree p) * coeff q (degree q)" | |
| 1103 | by (rule coeff_mult_degree_sum) | |
| 1104 | also have "coeff p (degree p) * coeff q (degree q) \<noteq> 0" | |
| 60500 | 1105 | using \<open>p \<noteq> 0\<close> and \<open>q \<noteq> 0\<close> by simp | 
| 29451 | 1106 | finally have "\<exists>n. coeff (p * q) n \<noteq> 0" .. | 
| 52380 | 1107 | thus "p * q \<noteq> 0" by (simp add: poly_eq_iff) | 
| 29451 | 1108 | qed | 
| 1109 | ||
| 1110 | lemma degree_mult_eq: | |
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1111 | fixes p q :: "'a::semidom poly" | 
| 29451 | 1112 | shows "\<lbrakk>p \<noteq> 0; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree (p * q) = degree p + degree q" | 
| 1113 | apply (rule order_antisym [OF degree_mult_le le_degree]) | |
| 1114 | apply (simp add: coeff_mult_degree_sum) | |
| 1115 | done | |
| 1116 | ||
| 60570 | 1117 | lemma degree_mult_right_le: | 
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1118 | fixes p q :: "'a::semidom poly" | 
| 60570 | 1119 | assumes "q \<noteq> 0" | 
| 1120 | shows "degree p \<le> degree (p * q)" | |
| 1121 | using assms by (cases "p = 0") (simp_all add: degree_mult_eq) | |
| 1122 | ||
| 1123 | lemma coeff_degree_mult: | |
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1124 | fixes p q :: "'a::semidom poly" | 
| 60570 | 1125 | shows "coeff (p * q) (degree (p * q)) = | 
| 1126 | 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 | 1127 | by (cases "p = 0 \<or> q = 0") (auto simp add: degree_mult_eq coeff_mult_degree_sum mult_ac) | 
| 60570 | 1128 | |
| 29451 | 1129 | lemma dvd_imp_degree_le: | 
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1130 | fixes p q :: "'a::semidom poly" | 
| 29451 | 1131 | 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 | 1132 | by (erule dvdE, hypsubst, subst degree_mult_eq) auto | 
| 29451 | 1133 | |
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1134 | lemma divides_degree: | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1135 | assumes pq: "p dvd (q :: 'a :: semidom poly)" | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 1136 | 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 | 1137 | by (metis dvd_imp_degree_le pq) | 
| 29451 | 1138 | |
| 60500 | 1139 | subsection \<open>Polynomials form an ordered integral domain\<close> | 
| 29878 | 1140 | |
| 52380 | 1141 | definition pos_poly :: "'a::linordered_idom poly \<Rightarrow> bool" | 
| 29878 | 1142 | where | 
| 1143 | "pos_poly p \<longleftrightarrow> 0 < coeff p (degree p)" | |
| 1144 | ||
| 1145 | lemma pos_poly_pCons: | |
| 1146 | "pos_poly (pCons a p) \<longleftrightarrow> pos_poly p \<or> (p = 0 \<and> 0 < a)" | |
| 1147 | unfolding pos_poly_def by simp | |
| 1148 | ||
| 1149 | lemma not_pos_poly_0 [simp]: "\<not> pos_poly 0" | |
| 1150 | unfolding pos_poly_def by simp | |
| 1151 | ||
| 1152 | lemma pos_poly_add: "\<lbrakk>pos_poly p; pos_poly q\<rbrakk> \<Longrightarrow> pos_poly (p + q)" | |
| 1153 | apply (induct p arbitrary: q, simp) | |
| 1154 | apply (case_tac q, force simp add: pos_poly_pCons add_pos_pos) | |
| 1155 | done | |
| 1156 | ||
| 1157 | lemma pos_poly_mult: "\<lbrakk>pos_poly p; pos_poly q\<rbrakk> \<Longrightarrow> pos_poly (p * q)" | |
| 1158 | unfolding pos_poly_def | |
| 1159 | apply (subgoal_tac "p \<noteq> 0 \<and> q \<noteq> 0") | |
| 56544 | 1160 | apply (simp add: degree_mult_eq coeff_mult_degree_sum) | 
| 29878 | 1161 | apply auto | 
| 1162 | done | |
| 1163 | ||
| 1164 | lemma pos_poly_total: "p = 0 \<or> pos_poly p \<or> pos_poly (- p)" | |
| 1165 | by (induct p) (auto simp add: pos_poly_pCons) | |
| 1166 | ||
| 52380 | 1167 | lemma last_coeffs_eq_coeff_degree: | 
| 1168 | "p \<noteq> 0 \<Longrightarrow> last (coeffs p) = coeff p (degree p)" | |
| 1169 | by (simp add: coeffs_def) | |
| 1170 | ||
| 1171 | lemma pos_poly_coeffs [code]: | |
| 1172 | "pos_poly p \<longleftrightarrow> (let as = coeffs p in as \<noteq> [] \<and> last as > 0)" (is "?P \<longleftrightarrow> ?Q") | |
| 1173 | proof | |
| 1174 | assume ?Q then show ?P by (auto simp add: pos_poly_def last_coeffs_eq_coeff_degree) | |
| 1175 | next | |
| 1176 | assume ?P then have *: "0 < coeff p (degree p)" by (simp add: pos_poly_def) | |
| 1177 | then have "p \<noteq> 0" by auto | |
| 1178 | with * show ?Q by (simp add: last_coeffs_eq_coeff_degree) | |
| 1179 | qed | |
| 1180 | ||
| 35028 
108662d50512
more consistent naming of type classes involving orderings (and lattices) -- c.f. NEWS
 haftmann parents: 
34973diff
changeset | 1181 | instantiation poly :: (linordered_idom) linordered_idom | 
| 29878 | 1182 | begin | 
| 1183 | ||
| 1184 | definition | |
| 37765 | 1185 | "x < y \<longleftrightarrow> pos_poly (y - x)" | 
| 29878 | 1186 | |
| 1187 | definition | |
| 37765 | 1188 | "x \<le> y \<longleftrightarrow> x = y \<or> pos_poly (y - x)" | 
| 29878 | 1189 | |
| 1190 | definition | |
| 61945 | 1191 | "\<bar>x::'a poly\<bar> = (if x < 0 then - x else x)" | 
| 29878 | 1192 | |
| 1193 | definition | |
| 37765 | 1194 | "sgn (x::'a poly) = (if x = 0 then 0 else if 0 < x then 1 else - 1)" | 
| 29878 | 1195 | |
| 60679 | 1196 | instance | 
| 1197 | proof | |
| 1198 | fix x y z :: "'a poly" | |
| 29878 | 1199 | show "x < y \<longleftrightarrow> x \<le> y \<and> \<not> y \<le> x" | 
| 1200 | unfolding less_eq_poly_def less_poly_def | |
| 1201 | apply safe | |
| 1202 | apply simp | |
| 1203 | apply (drule (1) pos_poly_add) | |
| 1204 | apply simp | |
| 1205 | done | |
| 60679 | 1206 | show "x \<le> x" | 
| 29878 | 1207 | unfolding less_eq_poly_def by simp | 
| 60679 | 1208 | show "x \<le> y \<Longrightarrow> y \<le> z \<Longrightarrow> x \<le> z" | 
| 29878 | 1209 | unfolding less_eq_poly_def | 
| 1210 | apply safe | |
| 1211 | apply (drule (1) pos_poly_add) | |
| 1212 | apply (simp add: algebra_simps) | |
| 1213 | done | |
| 60679 | 1214 | show "x \<le> y \<Longrightarrow> y \<le> x \<Longrightarrow> x = y" | 
| 29878 | 1215 | unfolding less_eq_poly_def | 
| 1216 | apply safe | |
| 1217 | apply (drule (1) pos_poly_add) | |
| 1218 | apply simp | |
| 1219 | done | |
| 60679 | 1220 | show "x \<le> y \<Longrightarrow> z + x \<le> z + y" | 
| 29878 | 1221 | unfolding less_eq_poly_def | 
| 1222 | apply safe | |
| 1223 | apply (simp add: algebra_simps) | |
| 1224 | done | |
| 1225 | show "x \<le> y \<or> y \<le> x" | |
| 1226 | unfolding less_eq_poly_def | |
| 1227 | using pos_poly_total [of "x - y"] | |
| 1228 | by auto | |
| 60679 | 1229 | show "x < y \<Longrightarrow> 0 < z \<Longrightarrow> z * x < z * y" | 
| 29878 | 1230 | unfolding less_poly_def | 
| 1231 | by (simp add: right_diff_distrib [symmetric] pos_poly_mult) | |
| 1232 | show "\<bar>x\<bar> = (if x < 0 then - x else x)" | |
| 1233 | by (rule abs_poly_def) | |
| 1234 | show "sgn x = (if x = 0 then 0 else if 0 < x then 1 else - 1)" | |
| 1235 | by (rule sgn_poly_def) | |
| 1236 | qed | |
| 1237 | ||
| 1238 | end | |
| 1239 | ||
| 60500 | 1240 | text \<open>TODO: Simplification rules for comparisons\<close> | 
| 29878 | 1241 | |
| 1242 | ||
| 60500 | 1243 | subsection \<open>Synthetic division and polynomial roots\<close> | 
| 52380 | 1244 | |
| 60500 | 1245 | text \<open> | 
| 52380 | 1246 |   Synthetic division is simply division by the linear polynomial @{term "x - c"}.
 | 
| 60500 | 1247 | \<close> | 
| 52380 | 1248 | |
| 1249 | definition synthetic_divmod :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly \<times> 'a" | |
| 1250 | where | |
| 1251 | "synthetic_divmod p c = fold_coeffs (\<lambda>a (q, r). (pCons r q, a + c * r)) p (0, 0)" | |
| 1252 | ||
| 1253 | definition synthetic_div :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly" | |
| 1254 | where | |
| 1255 | "synthetic_div p c = fst (synthetic_divmod p c)" | |
| 1256 | ||
| 1257 | lemma synthetic_divmod_0 [simp]: | |
| 1258 | "synthetic_divmod 0 c = (0, 0)" | |
| 1259 | by (simp add: synthetic_divmod_def) | |
| 1260 | ||
| 1261 | lemma synthetic_divmod_pCons [simp]: | |
| 1262 | "synthetic_divmod (pCons a p) c = (\<lambda>(q, r). (pCons r q, a + c * r)) (synthetic_divmod p c)" | |
| 1263 | by (cases "p = 0 \<and> a = 0") (auto simp add: synthetic_divmod_def) | |
| 1264 | ||
| 1265 | lemma synthetic_div_0 [simp]: | |
| 1266 | "synthetic_div 0 c = 0" | |
| 1267 | unfolding synthetic_div_def by simp | |
| 1268 | ||
| 1269 | lemma synthetic_div_unique_lemma: "smult c p = pCons a p \<Longrightarrow> p = 0" | |
| 1270 | by (induct p arbitrary: a) simp_all | |
| 1271 | ||
| 1272 | lemma snd_synthetic_divmod: | |
| 1273 | "snd (synthetic_divmod p c) = poly p c" | |
| 1274 | by (induct p, simp, simp add: split_def) | |
| 1275 | ||
| 1276 | lemma synthetic_div_pCons [simp]: | |
| 1277 | "synthetic_div (pCons a p) c = pCons (poly p c) (synthetic_div p c)" | |
| 1278 | unfolding synthetic_div_def | |
| 1279 | by (simp add: split_def snd_synthetic_divmod) | |
| 1280 | ||
| 1281 | lemma synthetic_div_eq_0_iff: | |
| 1282 | "synthetic_div p c = 0 \<longleftrightarrow> degree p = 0" | |
| 1283 | by (induct p, simp, case_tac p, simp) | |
| 1284 | ||
| 1285 | lemma degree_synthetic_div: | |
| 1286 | "degree (synthetic_div p c) = degree p - 1" | |
| 1287 | by (induct p, simp, simp add: synthetic_div_eq_0_iff) | |
| 1288 | ||
| 1289 | lemma synthetic_div_correct: | |
| 1290 | "p + smult c (synthetic_div p c) = pCons (poly p c) (synthetic_div p c)" | |
| 1291 | by (induct p) simp_all | |
| 1292 | ||
| 1293 | lemma synthetic_div_unique: | |
| 1294 | "p + smult c q = pCons r q \<Longrightarrow> r = poly p c \<and> q = synthetic_div p c" | |
| 1295 | apply (induct p arbitrary: q r) | |
| 1296 | apply (simp, frule synthetic_div_unique_lemma, simp) | |
| 1297 | apply (case_tac q, force) | |
| 1298 | done | |
| 1299 | ||
| 1300 | lemma synthetic_div_correct': | |
| 1301 | fixes c :: "'a::comm_ring_1" | |
| 1302 | shows "[:-c, 1:] * synthetic_div p c + [:poly p c:] = p" | |
| 1303 | using synthetic_div_correct [of p c] | |
| 1304 | by (simp add: algebra_simps) | |
| 1305 | ||
| 1306 | lemma poly_eq_0_iff_dvd: | |
| 1307 | fixes c :: "'a::idom" | |
| 1308 | shows "poly p c = 0 \<longleftrightarrow> [:-c, 1:] dvd p" | |
| 1309 | proof | |
| 1310 | assume "poly p c = 0" | |
| 1311 | with synthetic_div_correct' [of c p] | |
| 1312 | have "p = [:-c, 1:] * synthetic_div p c" by simp | |
| 1313 | then show "[:-c, 1:] dvd p" .. | |
| 1314 | next | |
| 1315 | assume "[:-c, 1:] dvd p" | |
| 1316 | then obtain k where "p = [:-c, 1:] * k" by (rule dvdE) | |
| 1317 | then show "poly p c = 0" by simp | |
| 1318 | qed | |
| 1319 | ||
| 1320 | lemma dvd_iff_poly_eq_0: | |
| 1321 | fixes c :: "'a::idom" | |
| 1322 | shows "[:c, 1:] dvd p \<longleftrightarrow> poly p (-c) = 0" | |
| 1323 | by (simp add: poly_eq_0_iff_dvd) | |
| 1324 | ||
| 1325 | lemma poly_roots_finite: | |
| 1326 | fixes p :: "'a::idom poly" | |
| 1327 |   shows "p \<noteq> 0 \<Longrightarrow> finite {x. poly p x = 0}"
 | |
| 1328 | proof (induct n \<equiv> "degree p" arbitrary: p) | |
| 1329 | case (0 p) | |
| 1330 | then obtain a where "a \<noteq> 0" and "p = [:a:]" | |
| 1331 | by (cases p, simp split: if_splits) | |
| 1332 |   then show "finite {x. poly p x = 0}" by simp
 | |
| 1333 | next | |
| 1334 | case (Suc n p) | |
| 1335 |   show "finite {x. poly p x = 0}"
 | |
| 1336 | proof (cases "\<exists>x. poly p x = 0") | |
| 1337 | case False | |
| 1338 |     then show "finite {x. poly p x = 0}" by simp
 | |
| 1339 | next | |
| 1340 | case True | |
| 1341 | then obtain a where "poly p a = 0" .. | |
| 1342 | then have "[:-a, 1:] dvd p" by (simp only: poly_eq_0_iff_dvd) | |
| 1343 | then obtain k where k: "p = [:-a, 1:] * k" .. | |
| 60500 | 1344 | with \<open>p \<noteq> 0\<close> have "k \<noteq> 0" by auto | 
| 52380 | 1345 | with k have "degree p = Suc (degree k)" | 
| 1346 | by (simp add: degree_mult_eq del: mult_pCons_left) | |
| 60500 | 1347 | with \<open>Suc n = degree p\<close> have "n = degree k" by simp | 
| 1348 |     then have "finite {x. poly k x = 0}" using \<open>k \<noteq> 0\<close> by (rule Suc.hyps)
 | |
| 52380 | 1349 |     then have "finite (insert a {x. poly k x = 0})" by simp
 | 
| 1350 |     then show "finite {x. poly p x = 0}"
 | |
| 57862 | 1351 | by (simp add: k Collect_disj_eq del: mult_pCons_left) | 
| 52380 | 1352 | qed | 
| 1353 | qed | |
| 1354 | ||
| 1355 | lemma poly_eq_poly_eq_iff: | |
| 1356 |   fixes p q :: "'a::{idom,ring_char_0} poly"
 | |
| 1357 | shows "poly p = poly q \<longleftrightarrow> p = q" (is "?P \<longleftrightarrow> ?Q") | |
| 1358 | proof | |
| 1359 | assume ?Q then show ?P by simp | |
| 1360 | next | |
| 1361 |   { fix p :: "'a::{idom,ring_char_0} poly"
 | |
| 1362 | have "poly p = poly 0 \<longleftrightarrow> p = 0" | |
| 1363 | apply (cases "p = 0", simp_all) | |
| 1364 | apply (drule poly_roots_finite) | |
| 1365 | apply (auto simp add: infinite_UNIV_char_0) | |
| 1366 | done | |
| 1367 | } note this [of "p - q"] | |
| 1368 | moreover assume ?P | |
| 1369 | ultimately show ?Q by auto | |
| 1370 | qed | |
| 1371 | ||
| 1372 | lemma poly_all_0_iff_0: | |
| 1373 |   fixes p :: "'a::{ring_char_0, idom} poly"
 | |
| 1374 | shows "(\<forall>x. poly p x = 0) \<longleftrightarrow> p = 0" | |
| 1375 | by (auto simp add: poly_eq_poly_eq_iff [symmetric]) | |
| 1376 | ||
| 1377 | ||
| 60500 | 1378 | subsection \<open>Long division of polynomials\<close> | 
| 29451 | 1379 | |
| 52380 | 1380 | definition pdivmod_rel :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> bool" | 
| 29451 | 1381 | where | 
| 29537 | 1382 | "pdivmod_rel x y q r \<longleftrightarrow> | 
| 29451 | 1383 | x = q * y + r \<and> (if y = 0 then q = 0 else r = 0 \<or> degree r < degree y)" | 
| 1384 | ||
| 29537 | 1385 | lemma pdivmod_rel_0: | 
| 1386 | "pdivmod_rel 0 y 0 0" | |
| 1387 | unfolding pdivmod_rel_def by simp | |
| 29451 | 1388 | |
| 29537 | 1389 | lemma pdivmod_rel_by_0: | 
| 1390 | "pdivmod_rel x 0 0 x" | |
| 1391 | unfolding pdivmod_rel_def by simp | |
| 29451 | 1392 | |
| 1393 | lemma eq_zero_or_degree_less: | |
| 1394 | assumes "degree p \<le> n" and "coeff p n = 0" | |
| 1395 | shows "p = 0 \<or> degree p < n" | |
| 1396 | proof (cases n) | |
| 1397 | case 0 | |
| 60500 | 1398 | with \<open>degree p \<le> n\<close> and \<open>coeff p n = 0\<close> | 
| 29451 | 1399 | have "coeff p (degree p) = 0" by simp | 
| 1400 | then have "p = 0" by simp | |
| 1401 | then show ?thesis .. | |
| 1402 | next | |
| 1403 | case (Suc m) | |
| 1404 | have "\<forall>i>n. coeff p i = 0" | |
| 60500 | 1405 | using \<open>degree p \<le> n\<close> by (simp add: coeff_eq_0) | 
| 29451 | 1406 | then have "\<forall>i\<ge>n. coeff p i = 0" | 
| 60500 | 1407 | using \<open>coeff p n = 0\<close> by (simp add: le_less) | 
| 29451 | 1408 | then have "\<forall>i>m. coeff p i = 0" | 
| 60500 | 1409 | using \<open>n = Suc m\<close> by (simp add: less_eq_Suc_le) | 
| 29451 | 1410 | then have "degree p \<le> m" | 
| 1411 | by (rule degree_le) | |
| 1412 | then have "degree p < n" | |
| 60500 | 1413 | using \<open>n = Suc m\<close> by (simp add: less_Suc_eq_le) | 
| 29451 | 1414 | then show ?thesis .. | 
| 1415 | qed | |
| 1416 | ||
| 29537 | 1417 | lemma pdivmod_rel_pCons: | 
| 1418 | assumes rel: "pdivmod_rel x y q r" | |
| 29451 | 1419 | assumes y: "y \<noteq> 0" | 
| 1420 | assumes b: "b = coeff (pCons a r) (degree y) / coeff y (degree y)" | |
| 29537 | 1421 | shows "pdivmod_rel (pCons a x) y (pCons b q) (pCons a r - smult b y)" | 
| 1422 | (is "pdivmod_rel ?x y ?q ?r") | |
| 29451 | 1423 | proof - | 
| 1424 | have x: "x = q * y + r" and r: "r = 0 \<or> degree r < degree y" | |
| 29537 | 1425 | using assms unfolding pdivmod_rel_def by simp_all | 
| 29451 | 1426 | |
| 1427 | have 1: "?x = ?q * y + ?r" | |
| 1428 | using b x by simp | |
| 1429 | ||
| 1430 | have 2: "?r = 0 \<or> degree ?r < degree y" | |
| 1431 | proof (rule eq_zero_or_degree_less) | |
| 29539 | 1432 | show "degree ?r \<le> degree y" | 
| 1433 | proof (rule degree_diff_le) | |
| 29451 | 1434 | 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 | 1435 | using r by auto | 
| 29451 | 1436 | show "degree (smult b y) \<le> degree y" | 
| 1437 | by (rule degree_smult_le) | |
| 1438 | qed | |
| 1439 | next | |
| 1440 | show "coeff ?r (degree y) = 0" | |
| 60500 | 1441 | using \<open>y \<noteq> 0\<close> unfolding b by simp | 
| 29451 | 1442 | qed | 
| 1443 | ||
| 1444 | from 1 2 show ?thesis | |
| 29537 | 1445 | unfolding pdivmod_rel_def | 
| 60500 | 1446 | using \<open>y \<noteq> 0\<close> by simp | 
| 29451 | 1447 | qed | 
| 1448 | ||
| 29537 | 1449 | lemma pdivmod_rel_exists: "\<exists>q r. pdivmod_rel x y q r" | 
| 29451 | 1450 | apply (cases "y = 0") | 
| 29537 | 1451 | apply (fast intro!: pdivmod_rel_by_0) | 
| 29451 | 1452 | apply (induct x) | 
| 29537 | 1453 | apply (fast intro!: pdivmod_rel_0) | 
| 1454 | apply (fast intro!: pdivmod_rel_pCons) | |
| 29451 | 1455 | done | 
| 1456 | ||
| 29537 | 1457 | lemma pdivmod_rel_unique: | 
| 1458 | assumes 1: "pdivmod_rel x y q1 r1" | |
| 1459 | assumes 2: "pdivmod_rel x y q2 r2" | |
| 29451 | 1460 | shows "q1 = q2 \<and> r1 = r2" | 
| 1461 | proof (cases "y = 0") | |
| 1462 | assume "y = 0" with assms show ?thesis | |
| 29537 | 1463 | by (simp add: pdivmod_rel_def) | 
| 29451 | 1464 | next | 
| 1465 | assume [simp]: "y \<noteq> 0" | |
| 1466 | from 1 have q1: "x = q1 * y + r1" and r1: "r1 = 0 \<or> degree r1 < degree y" | |
| 29537 | 1467 | unfolding pdivmod_rel_def by simp_all | 
| 29451 | 1468 | from 2 have q2: "x = q2 * y + r2" and r2: "r2 = 0 \<or> degree r2 < degree y" | 
| 29537 | 1469 | unfolding pdivmod_rel_def by simp_all | 
| 29451 | 1470 | from q1 q2 have q3: "(q1 - q2) * y = r2 - r1" | 
| 29667 | 1471 | by (simp add: algebra_simps) | 
| 29451 | 1472 | from r1 r2 have r3: "(r2 - r1) = 0 \<or> degree (r2 - r1) < degree y" | 
| 29453 | 1473 | by (auto intro: degree_diff_less) | 
| 29451 | 1474 | |
| 1475 | show "q1 = q2 \<and> r1 = r2" | |
| 1476 | proof (rule ccontr) | |
| 1477 | assume "\<not> (q1 = q2 \<and> r1 = r2)" | |
| 1478 | with q3 have "q1 \<noteq> q2" and "r1 \<noteq> r2" by auto | |
| 1479 | with r3 have "degree (r2 - r1) < degree y" by simp | |
| 1480 | also have "degree y \<le> degree (q1 - q2) + degree y" by simp | |
| 1481 | also have "\<dots> = degree ((q1 - q2) * y)" | |
| 60500 | 1482 | using \<open>q1 \<noteq> q2\<close> by (simp add: degree_mult_eq) | 
| 29451 | 1483 | also have "\<dots> = degree (r2 - r1)" | 
| 1484 | using q3 by simp | |
| 1485 | finally have "degree (r2 - r1) < degree (r2 - r1)" . | |
| 1486 | then show "False" by simp | |
| 1487 | qed | |
| 1488 | qed | |
| 1489 | ||
| 29660 | 1490 | lemma pdivmod_rel_0_iff: "pdivmod_rel 0 y q r \<longleftrightarrow> q = 0 \<and> r = 0" | 
| 1491 | by (auto dest: pdivmod_rel_unique intro: pdivmod_rel_0) | |
| 1492 | ||
| 1493 | lemma pdivmod_rel_by_0_iff: "pdivmod_rel x 0 q r \<longleftrightarrow> q = 0 \<and> r = x" | |
| 1494 | by (auto dest: pdivmod_rel_unique intro: pdivmod_rel_by_0) | |
| 1495 | ||
| 45605 | 1496 | lemmas pdivmod_rel_unique_div = pdivmod_rel_unique [THEN conjunct1] | 
| 29451 | 1497 | |
| 45605 | 1498 | lemmas pdivmod_rel_unique_mod = pdivmod_rel_unique [THEN conjunct2] | 
| 29451 | 1499 | |
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1500 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1501 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1502 | 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 | 1503 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1504 | 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 | 1505 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1506 | 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 | 1507 | \<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 | 1508 | "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 | 1509 | rr = smult lc r; | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1510 | qq = coeff r dr; | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1511 | 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 | 1512 | 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 | 1513 | 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 | 1514 | | "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 | 1515 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1516 | 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 | 1517 | "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 | 1518 | 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 | 1519 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1520 | 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 | 1521 | 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 | 1522 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1523 | 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 | 1524 | 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 | 1525 | "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 | 1526 | 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 | 1527 | using * | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1528 | 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 | 1529 | 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 | 1530 | 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 | 1531 | let ?qq = "coeff r dr" | 
| 63040 | 1532 | 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 | 1533 | 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 | 1534 | 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 | 1535 | note res = Suc(3) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1536 | 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 | 1537 | 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 | 1538 | 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 | 1539 | 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 | 1540 | 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 | 1541 | proof (cases "?qq = 0") | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1542 | case False | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1543 | 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 | 1544 | 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 | 1545 | qed auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1546 | 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 | 1547 | 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 | 1548 | 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 | 1549 | 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 | 1550 | 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 | 1551 | 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 | 1552 | 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 | 1553 | 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 | 1554 | unfolding dr | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1555 | 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 | 1556 | 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 | 1557 | 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 | 1558 | 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 | 1559 | 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 | 1560 | proof (cases dr) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1561 | case 0 | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1562 | 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 | 1563 | 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 | 1564 | 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 | 1565 | 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 | 1566 | 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 | 1567 | 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 | 1568 | 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 | 1569 | show ?case | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1570 | proof (intro conjI) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1571 | 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 | 1572 | 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 | 1573 | 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 | 1574 | 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 | 1575 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1576 | qed auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1577 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1578 | lemma pseudo_divmod: | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1579 | 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 | 1580 | 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 | 1581 | 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 | 1582 | proof - | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1583 | 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 | 1584 | 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 | 1585 | 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 | 1586 | 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 | 1587 | 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 | 1588 | 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 | 1589 | 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 | 1590 | 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 | 1591 | 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 | 1592 | 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 | 1593 | 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 | 1594 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1595 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1596 | 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 | 1597 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1598 | 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 | 1599 | "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 | 1600 | 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 | 1601 | |
| 63035 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 1602 | definition pseudo_mod :: "'a :: idom 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 | 1603 | "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 | 1604 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1605 | lemma pseudo_mod: | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1606 | fixes f g | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1607 | 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 | 1608 | 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 | 1609 | 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 | 1610 | proof - | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1611 | 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 | 1612 | let ?cge = "?cg ^ (Suc (degree f) - degree g)" | 
| 63040 | 1613 | 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 | 1614 | 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 | 1615 | 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 | 1616 | 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 | 1617 | 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 | 1618 | 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 | 1619 | 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 | 1620 | 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 | 1621 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1622 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1623 | 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 | 1624 | begin | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1625 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1626 | 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 | 1627 | \<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 | 1628 | "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 | 1629 | 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 | 1630 | divide_poly_main | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1631 | lc | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1632 | (q + mon) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1633 | (r - mon * d) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1634 | 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 | 1635 | | "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 | 1636 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1637 | 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 | 1638 | "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 | 1639 | 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 | 1640 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1641 | lemma divide_poly_main: | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1642 | 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 | 1643 | 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 | 1644 | "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 | 1645 | shows "q' = q + r" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1646 | using * | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1647 | 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 | 1648 | 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 | 1649 | let ?rr = "d * r" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1650 | 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 | 1651 | let ?qq = "?a div lc" | 
| 63040 | 1652 | 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 | 1653 | 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 | 1654 | let ?qqq = "q + b" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1655 | note res = Suc(3) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1656 | 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 | 1657 | 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 | 1658 | 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 | 1659 | proof (cases "?qq = 0") | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1660 | case False | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1661 | 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 | 1662 | 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 | 1663 | qed simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1664 | 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 | 1665 | 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 | 1666 | 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 | 1667 | 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 | 1668 | 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 | 1669 | case True | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1670 | 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 | 1671 | next | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1672 | case False | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1673 | 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 | 1674 | (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 | 1675 | 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 | 1676 | 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 | 1677 | 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 | 1678 | 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 | 1679 | 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 | 1680 | 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 | 1681 | 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 | 1682 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1683 | 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 | 1684 | 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 | 1685 | 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 | 1686 | 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 | 1687 | 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 | 1688 | 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 | 1689 | 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 | 1690 | 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 | 1691 | 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 | 1692 | 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 | 1693 | 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 | 1694 | 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 | 1695 | 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 | 1696 | 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 | 1697 | proof (cases dr) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1698 | case 0 | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1699 | 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 | 1700 | 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 | 1701 | 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 | 1702 | 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 | 1703 | 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 | 1704 | 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 | 1705 | 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 | 1706 | next | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1707 | 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 | 1708 | show ?case | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1709 | proof (cases "r = 0") | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1710 | case True | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1711 | 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 | 1712 | next | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1713 | case False | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1714 | 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 | 1715 | 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 | 1716 | 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 | 1717 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1718 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1719 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1720 | 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 | 1721 | 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 | 1722 | 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 | 1723 | 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 | 1724 | 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 | 1725 | 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 | 1726 | next | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1727 | case (Suc n) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1728 | 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 | 1729 | 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 | 1730 | 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 | 1731 | (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 | 1732 | 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 | 1733 | 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 | 1734 | (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 | 1735 | (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 | 1736 | d (dr - 1) n" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1737 | 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 | 1738 | 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 | 1739 | (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 | 1740 | 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 | 1741 | 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 | 1742 | finally show ?case. | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1743 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1744 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1745 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1746 | 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 | 1747 | 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 | 1748 | 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 | 1749 | 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 | 1750 | 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 | 1751 | qed simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1752 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1753 | lemma divide_poly: | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1754 | 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 | 1755 | 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 | 1756 | proof - | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1757 | 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 | 1758 | = (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 | 1759 | 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 | 1760 |   {
 | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1761 | fix f :: "'a poly" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1762 | assume "f \<noteq> 0" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1763 | 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 | 1764 | } note len = this | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1765 | 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 | 1766 | 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 | 1767 | case 1 | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1768 | show ?case | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1769 | proof (cases "f = 0") | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1770 | case True | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1771 | 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 | 1772 | next | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1773 | case False | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1774 | 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 | 1775 | 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 | 1776 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1777 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1778 | thus ?thesis by simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1779 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1780 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1781 | 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 | 1782 | 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 | 1783 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1784 | 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 | 1785 | end | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1786 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1787 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1788 | 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 | 1789 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1790 | text\<open> | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1791 | 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 | 1792 | 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 | 1793 | \<close> | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1794 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1795 | lemma pseudo_divmod_field: | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1796 | 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 | 1797 | 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 | 1798 | 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 | 1799 | proof - | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1800 | 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 | 1801 | 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 | 1802 | 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 | 1803 | 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 | 1804 | 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 | 1805 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1806 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1807 | 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 | 1808 | 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 | 1809 | 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 | 1810 | 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 | 1811 | unfolding lc | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1812 | 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 | 1813 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1814 | lemma divide_poly_field: | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1815 | 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 | 1816 | 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 | 1817 | 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 | 1818 | proof (cases "g = 0") | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1819 | case True show ?thesis | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1820 | 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 | 1821 | next | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1822 | case False | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1823 | 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 | 1824 | then show ?thesis | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1825 | 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 | 1826 | 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 | 1827 | 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 | 1828 | 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 | 1829 | f'_def | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1830 | by force | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1831 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1832 | |
| 29451 | 1833 | instantiation poly :: (field) ring_div | 
| 1834 | begin | |
| 1835 | ||
| 1836 | definition mod_poly where | |
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1837 | "f mod g \<equiv> | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1838 | 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 | 1839 | 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 | 1840 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1841 | 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 | 1842 | unfolding pdivmod_rel_def | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1843 | proof (intro conjI) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1844 | 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 | 1845 | proof(cases "y = 0") | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1846 | 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 | 1847 | next | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1848 | case False | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1849 | 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 | 1850 | (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 | 1851 | 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 | 1852 | 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 | 1853 | show ?thesis using False | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1854 | 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 | 1855 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1856 | 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 | 1857 | proof (cases "y = 0") | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1858 | 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 | 1859 | next | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1860 | case False | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1861 | 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 | 1862 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 1863 | qed | 
| 29451 | 1864 | |
| 1865 | 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 | 1866 | "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 | 1867 | by(rule pdivmod_rel_unique_div[OF pdivmod_rel]) | 
| 29451 | 1868 | |
| 1869 | 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 | 1870 | "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 | 1871 | by (rule pdivmod_rel_unique_mod[OF pdivmod_rel]) | 
| 29451 | 1872 | |
| 60679 | 1873 | instance | 
| 1874 | proof | |
| 29451 | 1875 | 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 | 1876 | 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 | 1877 | show "x div y * y + x mod y = x" by auto | 
| 29451 | 1878 | next | 
| 1879 | 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 | 1880 | show "x div 0 = 0" by simp | 
| 29451 | 1881 | next | 
| 1882 | 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 | 1883 | show "0 div y = 0" by simp | 
| 29451 | 1884 | next | 
| 1885 | fix x y z :: "'a poly" | |
| 1886 | assume "y \<noteq> 0" | |
| 60429 
d3d1e185cd63
uniform _ div _ as infix syntax for ring division
 haftmann parents: 
60352diff
changeset | 1887 | hence "pdivmod_rel (x + z * y) y (z + x div y) (x mod y)" | 
| 29537 | 1888 | using pdivmod_rel [of x y] | 
| 49962 
a8cc904a6820
Renamed {left,right}_distrib to distrib_{right,left}.
 webertj parents: 
49834diff
changeset | 1889 | by (simp add: pdivmod_rel_def distrib_right) | 
| 60429 
d3d1e185cd63
uniform _ div _ as infix syntax for ring division
 haftmann parents: 
60352diff
changeset | 1890 | thus "(x + z * y) div y = z + x div y" | 
| 29451 | 1891 | by (rule div_poly_eq) | 
| 30930 | 1892 | next | 
| 1893 | fix x y z :: "'a poly" | |
| 1894 | assume "x \<noteq> 0" | |
| 60429 
d3d1e185cd63
uniform _ div _ as infix syntax for ring division
 haftmann parents: 
60352diff
changeset | 1895 | show "(x * y) div (x * z) = y div z" | 
| 30930 | 1896 | proof (cases "y \<noteq> 0 \<and> z \<noteq> 0") | 
| 1897 | have "\<And>x::'a poly. pdivmod_rel x 0 0 x" | |
| 1898 | by (rule pdivmod_rel_by_0) | |
| 60429 
d3d1e185cd63
uniform _ div _ as infix syntax for ring division
 haftmann parents: 
60352diff
changeset | 1899 | then have [simp]: "\<And>x::'a poly. x div 0 = 0" | 
| 30930 | 1900 | by (rule div_poly_eq) | 
| 1901 | have "\<And>x::'a poly. pdivmod_rel 0 x 0 0" | |
| 1902 | by (rule pdivmod_rel_0) | |
| 60429 
d3d1e185cd63
uniform _ div _ as infix syntax for ring division
 haftmann parents: 
60352diff
changeset | 1903 | then have [simp]: "\<And>x::'a poly. 0 div x = 0" | 
| 30930 | 1904 | by (rule div_poly_eq) | 
| 1905 | case False then show ?thesis by auto | |
| 1906 | next | |
| 1907 | case True then have "y \<noteq> 0" and "z \<noteq> 0" by auto | |
| 60500 | 1908 | with \<open>x \<noteq> 0\<close> | 
| 30930 | 1909 | have "\<And>q r. pdivmod_rel y z q r \<Longrightarrow> pdivmod_rel (x * y) (x * z) q (x * r)" | 
| 1910 | by (auto simp add: pdivmod_rel_def algebra_simps) | |
| 1911 | (rule classical, simp add: degree_mult_eq) | |
| 60429 
d3d1e185cd63
uniform _ div _ as infix syntax for ring division
 haftmann parents: 
60352diff
changeset | 1912 | 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 | 1913 | ultimately have "pdivmod_rel (x * y) (x * z) (y div z) (x * (y mod z))" . | 
| 30930 | 1914 | then show ?thesis by (simp add: div_poly_eq) | 
| 1915 | qed | |
| 29451 | 1916 | qed | 
| 1917 | ||
| 1918 | end | |
| 1919 | ||
| 60570 | 1920 | lemma is_unit_monom_0: | 
| 1921 | fixes a :: "'a::field" | |
| 1922 | assumes "a \<noteq> 0" | |
| 1923 | shows "is_unit (monom a 0)" | |
| 1924 | proof | |
| 62351 | 1925 | from assms show "1 = monom a 0 * monom (inverse a) 0" | 
| 60570 | 1926 | by (simp add: mult_monom) | 
| 1927 | qed | |
| 1928 | ||
| 1929 | lemma is_unit_triv: | |
| 1930 | fixes a :: "'a::field" | |
| 1931 | assumes "a \<noteq> 0" | |
| 1932 | shows "is_unit [:a:]" | |
| 1933 | using assms by (simp add: is_unit_monom_0 monom_0 [symmetric]) | |
| 1934 | ||
| 1935 | lemma is_unit_iff_degree: | |
| 1936 | assumes "p \<noteq> 0" | |
| 1937 | shows "is_unit p \<longleftrightarrow> degree p = 0" (is "?P \<longleftrightarrow> ?Q") | |
| 1938 | proof | |
| 1939 | assume ?Q | |
| 1940 | then obtain a where "p = [:a:]" by (rule degree_eq_zeroE) | |
| 1941 | with assms show ?P by (simp add: is_unit_triv) | |
| 1942 | next | |
| 1943 | assume ?P | |
| 1944 | then obtain q where "q \<noteq> 0" "p * q = 1" .. | |
| 1945 | then have "degree (p * q) = degree 1" | |
| 1946 | by simp | |
| 1947 | with \<open>p \<noteq> 0\<close> \<open>q \<noteq> 0\<close> have "degree p + degree q = 0" | |
| 1948 | by (simp add: degree_mult_eq) | |
| 1949 | then show ?Q by simp | |
| 1950 | qed | |
| 1951 | ||
| 1952 | lemma is_unit_pCons_iff: | |
| 1953 | "is_unit (pCons a p) \<longleftrightarrow> p = 0 \<and> a \<noteq> 0" (is "?P \<longleftrightarrow> ?Q") | |
| 1954 | by (cases "p = 0") (auto simp add: is_unit_triv is_unit_iff_degree) | |
| 1955 | ||
| 1956 | lemma is_unit_monom_trival: | |
| 1957 | fixes p :: "'a::field poly" | |
| 1958 | assumes "is_unit p" | |
| 1959 | shows "monom (coeff p (degree p)) 0 = p" | |
| 1960 | using assms by (cases p) (simp_all add: monom_0 is_unit_pCons_iff) | |
| 1961 | ||
| 60685 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 1962 | lemma is_unit_polyE: | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 1963 | assumes "is_unit p" | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 1964 | 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 | 1965 | proof - | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 1966 | 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 | 1967 | with assms have "p = [:a:]" and "a \<noteq> 0" | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 1968 | by (simp_all add: is_unit_pCons_iff) | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 1969 | with that show thesis by (simp add: monom_0) | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 1970 | qed | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 1971 | |
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 1972 | instantiation poly :: (field) normalization_semidom | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 1973 | begin | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 1974 | |
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 1975 | definition normalize_poly :: "'a poly \<Rightarrow> 'a poly" | 
| 62351 | 1976 | where "normalize_poly p = smult (inverse (coeff p (degree p))) p" | 
| 60685 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 1977 | |
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 1978 | definition unit_factor_poly :: "'a poly \<Rightarrow> 'a poly" | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 1979 | where "unit_factor_poly p = monom (coeff p (degree p)) 0" | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 1980 | |
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 1981 | instance | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 1982 | proof | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 1983 | fix p :: "'a poly" | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 1984 | show "unit_factor p * normalize p = p" | 
| 62351 | 1985 | by (cases "p = 0") | 
| 1986 | (simp_all add: normalize_poly_def unit_factor_poly_def, | |
| 1987 | simp only: mult_smult_left [symmetric] smult_monom, simp) | |
| 60685 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 1988 | next | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 1989 | show "normalize 0 = (0::'a poly)" | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 1990 | by (simp add: normalize_poly_def) | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 1991 | next | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 1992 | show "unit_factor 0 = (0::'a poly)" | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 1993 | by (simp add: unit_factor_poly_def) | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 1994 | next | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 1995 | fix p :: "'a poly" | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 1996 | assume "is_unit p" | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 1997 | then obtain a where "p = monom a 0" and "a \<noteq> 0" | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 1998 | by (rule is_unit_polyE) | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 1999 | then show "normalize p = 1" | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 2000 | by (auto simp add: normalize_poly_def smult_monom degree_monom_eq) | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 2001 | next | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 2002 | fix p q :: "'a poly" | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 2003 | assume "q \<noteq> 0" | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 2004 | from \<open>q \<noteq> 0\<close> have "is_unit (monom (coeff q (degree q)) 0)" | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 2005 | by (auto intro: is_unit_monom_0) | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 2006 | then show "is_unit (unit_factor q)" | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 2007 | by (simp add: unit_factor_poly_def) | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 2008 | next | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 2009 | fix p q :: "'a poly" | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 2010 | have "monom (coeff (p * q) (degree (p * q))) 0 = | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 2011 | monom (coeff p (degree p)) 0 * monom (coeff q (degree q)) 0" | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 2012 | by (simp add: monom_0 coeff_degree_mult) | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 2013 | then show "unit_factor (p * q) = | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 2014 | unit_factor p * unit_factor q" | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 2015 | by (simp add: unit_factor_poly_def) | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 2016 | qed | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 2017 | |
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 2018 | end | 
| 
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
 haftmann parents: 
60679diff
changeset | 2019 | |
| 62351 | 2020 | lemma unit_factor_monom [simp]: | 
| 2021 | "unit_factor (monom a n) = | |
| 2022 | (if a = 0 then 0 else monom a 0)" | |
| 2023 | by (simp add: unit_factor_poly_def degree_monom_eq) | |
| 2024 | ||
| 2025 | lemma unit_factor_pCons [simp]: | |
| 2026 | "unit_factor (pCons a p) = | |
| 2027 | (if p = 0 then monom a 0 else unit_factor p)" | |
| 2028 | by (simp add: unit_factor_poly_def) | |
| 2029 | ||
| 2030 | lemma normalize_monom [simp]: | |
| 2031 | "normalize (monom a n) = | |
| 2032 | (if a = 0 then 0 else monom 1 n)" | |
| 2033 | by (simp add: normalize_poly_def degree_monom_eq smult_monom) | |
| 2034 | ||
| 29451 | 2035 | lemma degree_mod_less: | 
| 2036 | "y \<noteq> 0 \<Longrightarrow> x mod y = 0 \<or> degree (x mod y) < degree y" | |
| 29537 | 2037 | using pdivmod_rel [of x y] | 
| 2038 | unfolding pdivmod_rel_def by simp | |
| 29451 | 2039 | |
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2040 | lemma div_poly_less: "degree (x::'a::field poly) < degree y \<Longrightarrow> x div y = 0" | 
| 29451 | 2041 | proof - | 
| 2042 | assume "degree x < degree y" | |
| 29537 | 2043 | hence "pdivmod_rel x y 0 x" | 
| 2044 | by (simp add: pdivmod_rel_def) | |
| 29451 | 2045 | thus "x div y = 0" by (rule div_poly_eq) | 
| 2046 | qed | |
| 2047 | ||
| 2048 | lemma mod_poly_less: "degree x < degree y \<Longrightarrow> x mod y = x" | |
| 2049 | proof - | |
| 2050 | assume "degree x < degree y" | |
| 29537 | 2051 | hence "pdivmod_rel x y 0 x" | 
| 2052 | by (simp add: pdivmod_rel_def) | |
| 29451 | 2053 | thus "x mod y = x" by (rule mod_poly_eq) | 
| 2054 | qed | |
| 2055 | ||
| 29659 | 2056 | lemma pdivmod_rel_smult_left: | 
| 2057 | "pdivmod_rel x y q r | |
| 2058 | \<Longrightarrow> pdivmod_rel (smult a x) y (smult a q) (smult a r)" | |
| 2059 | unfolding pdivmod_rel_def by (simp add: smult_add_right) | |
| 2060 | ||
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2061 | lemma div_smult_left: "(smult (a::'a::field) x) div y = smult a (x div y)" | 
| 29659 | 2062 | by (rule div_poly_eq, rule pdivmod_rel_smult_left, rule pdivmod_rel) | 
| 2063 | ||
| 2064 | lemma mod_smult_left: "(smult a x) mod y = smult a (x mod y)" | |
| 2065 | by (rule mod_poly_eq, rule pdivmod_rel_smult_left, rule pdivmod_rel) | |
| 2066 | ||
| 30072 | 2067 | lemma poly_div_minus_left [simp]: | 
| 2068 | fixes x y :: "'a::field poly" | |
| 2069 | shows "(- x) div y = - (x div y)" | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54230diff
changeset | 2070 | using div_smult_left [of "- 1::'a"] by simp | 
| 30072 | 2071 | |
| 2072 | lemma poly_mod_minus_left [simp]: | |
| 2073 | fixes x y :: "'a::field poly" | |
| 2074 | shows "(- x) mod y = - (x mod y)" | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54230diff
changeset | 2075 | using mod_smult_left [of "- 1::'a"] by simp | 
| 30072 | 2076 | |
| 57482 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2077 | lemma pdivmod_rel_add_left: | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2078 | assumes "pdivmod_rel x y q r" | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2079 | assumes "pdivmod_rel x' y q' r'" | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2080 | shows "pdivmod_rel (x + x') y (q + q') (r + r')" | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2081 | using assms unfolding pdivmod_rel_def | 
| 59557 | 2082 | by (auto simp add: algebra_simps degree_add_less) | 
| 57482 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2083 | |
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2084 | lemma poly_div_add_left: | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2085 | fixes x y z :: "'a::field poly" | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2086 | shows "(x + y) div z = x div z + y div z" | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2087 | using pdivmod_rel_add_left [OF pdivmod_rel pdivmod_rel] | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2088 | by (rule div_poly_eq) | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2089 | |
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2090 | lemma poly_mod_add_left: | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2091 | fixes x y z :: "'a::field poly" | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2092 | shows "(x + y) mod z = x mod z + y mod z" | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2093 | using pdivmod_rel_add_left [OF pdivmod_rel pdivmod_rel] | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2094 | by (rule mod_poly_eq) | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2095 | |
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2096 | lemma poly_div_diff_left: | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2097 | fixes x y z :: "'a::field poly" | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2098 | shows "(x - y) div z = x div z - y div z" | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2099 | 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 | 2100 | |
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2101 | lemma poly_mod_diff_left: | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2102 | fixes x y z :: "'a::field poly" | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2103 | shows "(x - y) mod z = x mod z - y mod z" | 
| 
60459c3853af
add lemmas: polynomial div/mod distribute over addition
 huffman parents: 
56544diff
changeset | 2104 | 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 | 2105 | |
| 29659 | 2106 | lemma pdivmod_rel_smult_right: | 
| 2107 | "\<lbrakk>a \<noteq> 0; pdivmod_rel x y q r\<rbrakk> | |
| 2108 | \<Longrightarrow> pdivmod_rel x (smult a y) (smult (inverse a) q) r" | |
| 2109 | unfolding pdivmod_rel_def by simp | |
| 2110 | ||
| 2111 | 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 | 2112 | "(a::'a::field) \<noteq> 0 \<Longrightarrow> x div (smult a y) = smult (inverse a) (x div y)" | 
| 29659 | 2113 | by (rule div_poly_eq, erule pdivmod_rel_smult_right, rule pdivmod_rel) | 
| 2114 | ||
| 2115 | lemma mod_smult_right: "a \<noteq> 0 \<Longrightarrow> x mod (smult a y) = x mod y" | |
| 2116 | by (rule mod_poly_eq, erule pdivmod_rel_smult_right, rule pdivmod_rel) | |
| 2117 | ||
| 30072 | 2118 | lemma poly_div_minus_right [simp]: | 
| 2119 | fixes x y :: "'a::field poly" | |
| 2120 | shows "x div (- y) = - (x div y)" | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54230diff
changeset | 2121 | using div_smult_right [of "- 1::'a"] by (simp add: nonzero_inverse_minus_eq) | 
| 30072 | 2122 | |
| 2123 | lemma poly_mod_minus_right [simp]: | |
| 2124 | fixes x y :: "'a::field poly" | |
| 2125 | shows "x mod (- y) = x mod y" | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54230diff
changeset | 2126 | using mod_smult_right [of "- 1::'a"] by simp | 
| 30072 | 2127 | |
| 29660 | 2128 | lemma pdivmod_rel_mult: | 
| 2129 | "\<lbrakk>pdivmod_rel x y q r; pdivmod_rel q z q' r'\<rbrakk> | |
| 2130 | \<Longrightarrow> pdivmod_rel x (y * z) q' (y * r' + r)" | |
| 2131 | apply (cases "z = 0", simp add: pdivmod_rel_def) | |
| 2132 | apply (cases "y = 0", simp add: pdivmod_rel_by_0_iff pdivmod_rel_0_iff) | |
| 2133 | apply (cases "r = 0") | |
| 2134 | apply (cases "r' = 0") | |
| 2135 | apply (simp add: pdivmod_rel_def) | |
| 36350 | 2136 | apply (simp add: pdivmod_rel_def field_simps degree_mult_eq) | 
| 29660 | 2137 | apply (cases "r' = 0") | 
| 2138 | apply (simp add: pdivmod_rel_def degree_mult_eq) | |
| 36350 | 2139 | apply (simp add: pdivmod_rel_def field_simps) | 
| 29660 | 2140 | apply (simp add: degree_mult_eq degree_add_less) | 
| 2141 | done | |
| 2142 | ||
| 2143 | lemma poly_div_mult_right: | |
| 2144 | fixes x y z :: "'a::field poly" | |
| 2145 | shows "x div (y * z) = (x div y) div z" | |
| 2146 | by (rule div_poly_eq, rule pdivmod_rel_mult, (rule pdivmod_rel)+) | |
| 2147 | ||
| 2148 | lemma poly_mod_mult_right: | |
| 2149 | fixes x y z :: "'a::field poly" | |
| 2150 | shows "x mod (y * z) = y * (x div y mod z) + x mod y" | |
| 2151 | by (rule mod_poly_eq, rule pdivmod_rel_mult, (rule pdivmod_rel)+) | |
| 2152 | ||
| 29451 | 2153 | lemma mod_pCons: | 
| 2154 | fixes a and x | |
| 2155 | assumes y: "y \<noteq> 0" | |
| 2156 | defines b: "b \<equiv> coeff (pCons a (x mod y)) (degree y) / coeff y (degree y)" | |
| 2157 | shows "(pCons a x) mod y = (pCons a (x mod y) - smult b y)" | |
| 2158 | unfolding b | |
| 2159 | apply (rule mod_poly_eq) | |
| 29537 | 2160 | apply (rule pdivmod_rel_pCons [OF pdivmod_rel y refl]) | 
| 29451 | 2161 | done | 
| 2162 | ||
| 52380 | 2163 | definition pdivmod :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<times> 'a poly" | 
| 2164 | where | |
| 2165 | "pdivmod p q = (p div q, p mod q)" | |
| 31663 | 2166 | |
| 52380 | 2167 | lemma pdivmod_0: | 
| 2168 | "pdivmod 0 q = (0, 0)" | |
| 2169 | by (simp add: pdivmod_def) | |
| 31663 | 2170 | |
| 52380 | 2171 | lemma pdivmod_pCons: | 
| 2172 | "pdivmod (pCons a p) q = | |
| 2173 | (if q = 0 then (0, pCons a p) else | |
| 2174 | (let (s, r) = pdivmod p q; | |
| 2175 | b = coeff (pCons a r) (degree q) / coeff q (degree q) | |
| 2176 | in (pCons b s, pCons a r - smult b q)))" | |
| 2177 | apply (simp add: pdivmod_def Let_def, safe) | |
| 2178 | apply (rule div_poly_eq) | |
| 2179 | apply (erule pdivmod_rel_pCons [OF pdivmod_rel _ refl]) | |
| 2180 | apply (rule mod_poly_eq) | |
| 2181 | apply (erule pdivmod_rel_pCons [OF pdivmod_rel _ refl]) | |
| 29451 | 2182 | done | 
| 2183 | ||
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2184 | lemma pdivmod_fold_coeffs: | 
| 52380 | 2185 | "pdivmod p q = (if q = 0 then (0, p) | 
| 2186 | else fold_coeffs (\<lambda>a (s, r). | |
| 2187 | let b = coeff (pCons a r) (degree q) / coeff q (degree q) | |
| 2188 | in (pCons b s, pCons a r - smult b q) | |
| 2189 | ) p (0, 0))" | |
| 2190 | apply (cases "q = 0") | |
| 2191 | apply (simp add: pdivmod_def) | |
| 2192 | apply (rule sym) | |
| 2193 | apply (induct p) | |
| 2194 | apply (simp_all add: pdivmod_0 pdivmod_pCons) | |
| 2195 | apply (case_tac "a = 0 \<and> p = 0") | |
| 2196 | apply (auto simp add: pdivmod_def) | |
| 2197 | done | |
| 29980 | 2198 | |
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2199 | 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 | 2200 | (* Subsection by: | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2201 | Sebastiaan Joosten | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2202 | René Thiemann | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2203 | Akihisa Yamada | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2204 | *) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2205 | 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 | 2206 | "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 | 2207 | | "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 | 2208 | | "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 | 2209 | |
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2210 | 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 | 2211 | \<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 | 2212 | "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 | 2213 | 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 | 2214 | a = hd r; | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2215 | 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 | 2216 | 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 | 2217 | 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 | 2218 | | "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 | 2219 | |
| 63035 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2220 | 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 | 2221 | \<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 | 2222 | "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 | 2223 | 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 | 2224 | 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 | 2225 | 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 | 2226 | 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 | 2227 | | "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 | 2228 | |
| 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2229 | |
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2230 | 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 | 2231 | \<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 | 2232 | "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 | 2233 | a = hd r; | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2234 | qqq = cCons a q; | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2235 | 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 | 2236 | 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 | 2237 | | "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 | 2238 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2239 | 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 | 2240 | \<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 | 2241 | "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 | 2242 | a = hd r; | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2243 | 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 | 2244 | 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 | 2245 | | "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 | 2246 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2247 | 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 | 2248 | "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 | 2249 | (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 | 2250 | (let rq = rev q; | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2251 | (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 | 2252 | (qu,rev re)))" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2253 | |
| 63035 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2254 | 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 | 2255 | "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 | 2256 | (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 | 2257 | (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 | 2258 | 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 | 2259 | (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 | 2260 | |
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2261 | 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 | 2262 | "(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 | 2263 | 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 | 2264 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2265 | 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 | 2266 | "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 | 2267 | 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 | 2268 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2269 | 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 | 2270 | "(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 | 2271 | = 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 | 2272 | 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 | 2273 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2274 | lemma Poly_append: | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2275 | "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 | 2276 | 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 | 2277 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2278 | 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 | 2279 | 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 | 2280 | = 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 | 2281 | 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 | 2282 | 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 | 2283 | 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 | 2284 | 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 | 2285 | 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 | 2286 | 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 | 2287 | 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 | 2288 | 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 | 2289 | = 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 | 2290 | 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 (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 | 2292 | 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 | 2293 | 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 | 2294 | (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 | 2295 | 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 | 2296 | 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 | 2297 | 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 | 2298 | finally show ?case | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2299 | 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 | 2300 | qed auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2301 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2302 | 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 | 2303 | 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 | 2304 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2305 | 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 | 2306 | "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 | 2307 | 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 | 2308 | proof(induct r) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2309 | case (Cons a rs) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2310 | 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 | 2311 | qed simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2312 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2313 | 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 | 2314 | proof (induct p) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2315 | 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 | 2316 | qed simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2317 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2318 | 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 | 2319 | 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 | 2320 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2321 | 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 | 2322 | 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 | 2323 | 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 | 2324 | and dNonempty:"d \<noteq> []" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2325 | 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 | 2326 | 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 | 2327 | shows | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2328 | "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 | 2329 | (Poly q', Poly r')" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2330 | using assms(4-) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2331 | 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 | 2332 | 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 | 2333 | 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 | 2334 | have rNonempty:"r \<noteq> []" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2335 | 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 | 2336 | 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 | 2337 | 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 | 2338 | 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 | 2339 | 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 | 2340 | 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 | 2341 | 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 | 2342 | 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 | 2343 | 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 | 2344 | 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 | 2345 | 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 | 2346 | 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 | 2347 | 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 | 2348 | 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 | 2349 | using n by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2350 | 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 | 2351 | 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 | 2352 | 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 | 2353 | 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 | 2354 | 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 | 2355 | 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 | 2356 | 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 | 2357 | 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 | 2358 | 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 | 2359 | case 1 | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2360 | 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 | 2361 | 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 | 2362 | next | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2363 | case 2 | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2364 | show ?case | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2365 | 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 | 2366 | 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 | 2367 | 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 | 2368 | 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 | 2369 | 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 | 2370 | 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 | 2371 | 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 | 2372 | minus_poly_rev_list) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2373 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2374 | qed simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2375 | qed simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2376 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2377 | 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 | 2378 | 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 | 2379 | proof (cases "g=0") | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2380 | case False | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2381 | 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 | 2382 | 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 | 2383 | 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 | 2384 | 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 | 2385 | (last (coeffs g)) (rev []) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2386 | (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 | 2387 | (1 + length (coeffs f) - | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2388 | 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 | 2389 | 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 | 2390 | (hd (rev (coeffs g))) [] | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2391 | (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 | 2392 | (1 + length (coeffs f) - | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2393 | 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 | 2394 | 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 | 2395 | 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 | 2396 | show ?thesis | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2397 | 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 | 2398 | 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 | 2399 | poly_of_list_def | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2400 | 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 | 2401 | next | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2402 | case True | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2403 | 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 | 2404 | by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2405 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2406 | |
| 63035 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2407 | 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 | 2408 | 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 | 2409 | 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 | 2410 | |
| 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2411 | 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 | 2412 | 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 | 2413 | proof - | 
| 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2414 | 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 | 2415 | 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 | 2416 | 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 | 2417 | 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 | 2418 | 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 | 2419 | 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 | 2420 | qed | 
| 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2421 | |
| 
6c018eb1e177
fixed code equation for pdivmod, added improved code equation for pseudo_mod
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
63034diff
changeset | 2422 | |
| 63027 
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 | 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 | 2425 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2426 | 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 | 2427 | 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 | 2428 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2429 | 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 | 2430 | else let | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2431 | 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 | 2432 | h = smult ilc g; | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2433 | (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 | 2434 | 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 | 2435 | proof (cases "g = 0") | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2436 | case False | 
| 63040 | 2437 | define lc where "lc = inverse (coeff g (degree g))" | 
| 2438 | 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 | 2439 | 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 | 2440 | 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 | 2441 | 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 | 2442 | 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 | 2443 | 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 | 2444 | 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 | 2445 | 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 | 2446 | 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 | 2447 | 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 | 2448 | 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 | 2449 | 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 | 2450 | using lc by auto | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2451 | 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 | 2452 | 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 | 2453 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2454 | 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 | 2455 | cg = coeffs g | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2456 | 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 | 2457 | else let | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2458 | cf = coeffs f; | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2459 | ilc = inverse (last cg); | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2460 | 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 | 2461 | (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 | 2462 | 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 | 2463 | proof - | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2464 | 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 | 2465 | 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 | 2466 | show ?thesis | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2467 | proof (cases "g = 0") | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2468 | 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 | 2469 | next | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2470 | case False | 
| 63040 | 2471 | 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 | 2472 | 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 | 2473 | 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 | 2474 | "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 | 2475 | "(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 | 2476 | 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 | 2477 | 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 | 2478 | 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 | 2479 | 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 | 2480 | "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 | 2481 | 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 | 2482 | (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 | 2483 | 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 | 2484 | 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 | 2485 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2486 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2487 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2488 | 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 | 2489 | 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 | 2490 | 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 | 2491 |   {
 | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2492 | fix xs :: "'a list" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2493 | 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 | 2494 | } note [simp] = this | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2495 | 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 | 2496 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2497 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2498 | 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 | 2499 | \<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 | 2500 | "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 | 2501 | cr = hd r | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2502 | 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 | 2503 | a = cr div lc; | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2504 | qq = cCons a q; | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2505 | 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 | 2506 | 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 | 2507 | | "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 | 2508 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2509 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2510 | 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 | 2511 | cr = hd r; | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2512 | a = cr div lc; | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2513 | qq = cCons a q; | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2514 | 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 | 2515 | 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 | 2516 | 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 | 2517 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2518 | 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 | 2519 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2520 | 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 | 2521 | "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 | 2522 | (let cg = coeffs g | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2523 | 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 | 2524 | 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 | 2525 | 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 | 2526 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2527 | 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 | 2528 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2529 | 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 | 2530 | 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 | 2531 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2532 | 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 | 2533 | (let cg = coeffs g | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2534 | 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 | 2535 | 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 | 2536 | 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 | 2537 | 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 | 2538 | proof - | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2539 | 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 | 2540 | 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 | 2541 | 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 | 2542 | finally show ?thesis . | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2543 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2544 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2545 | 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 | 2546 | "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 | 2547 | let cg = coeffs g | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2548 | 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 | 2549 | 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 | 2550 | 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 | 2551 | 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 | 2552 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2553 | 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 | 2554 | on non-fields will no longer be executable. However, a code-unfold is possible, since | 
| 63034 | 2555 | \<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 | 2556 | 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 | 2557 | proof (intro ext) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2558 | 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 | 2559 | 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 | 2560 | 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 | 2561 | 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 | 2562 | 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 | 2563 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2564 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2565 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2566 | 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 | 2567 | 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 | 2568 | 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 | 2569 | and d:"d \<noteq> []" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2570 | 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 | 2571 | shows | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2572 | "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 | 2573 | 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 | 2574 | using assms(4-) | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2575 | 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 | 2576 | 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 | 2577 | 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 | 2578 | have r: "r \<noteq> []" | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2579 | 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 | 2580 | 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 | 2581 | 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 | 2582 | 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 | 2583 | 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 | 2584 | 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 | 2585 | show ?case | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2586 | 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 | 2587 | case False | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2588 | 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 | 2589 | 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 | 2590 | next | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2591 | case True | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2592 | hence id: | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2593 | "?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 | 2594 | (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 | 2595 | divide_poly_main lc | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2596 | (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 | 2597 | (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 | 2598 | (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 | 2599 | using r d | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2600 | 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 | 2601 | 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 | 2602 | 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 | 2603 | 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 | 2604 | show ?thesis unfolding id | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2605 | 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 | 2606 | 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 | 2607 | case 2 | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2608 | 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 | 2609 | 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 | 2610 | 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 | 2611 | 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 | 2612 | 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 | 2613 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2614 | qed simp | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2615 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2616 | |
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2617 | 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 | 2618 | proof - | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2619 | 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 | 2620 | show ?thesis | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2621 | proof (cases "g = 0") | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2622 | case True | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2623 | 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 | 2624 | next | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2625 | case False | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2626 | 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 | 2627 | 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 | 2628 | 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 | 2629 | 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 | 2630 | 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 | 2631 | 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 | 2632 | 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 | 2633 | 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 | 2634 | 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 | 2635 | 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 | 2636 | qed | 
| 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 2637 | qed | 
| 29980 | 2638 | |
| 60500 | 2639 | subsection \<open>Order of polynomial roots\<close> | 
| 29977 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2640 | |
| 52380 | 2641 | 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 | 2642 | where | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2643 | "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 | 2644 | |
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2645 | lemma coeff_linear_power: | 
| 29979 | 2646 | fixes a :: "'a::comm_semiring_1" | 
| 29977 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2647 | shows "coeff ([:a, 1:] ^ n) n = 1" | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2648 | apply (induct n, simp_all) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2649 | apply (subst coeff_eq_0) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2650 | apply (auto intro: le_less_trans degree_power_le) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2651 | done | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2652 | |
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2653 | lemma degree_linear_power: | 
| 29979 | 2654 | fixes a :: "'a::comm_semiring_1" | 
| 29977 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2655 | shows "degree ([:a, 1:] ^ n) = n" | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2656 | apply (rule order_antisym) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2657 | 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 | 2658 | apply (rule le_degree, simp add: coeff_linear_power) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2659 | done | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2660 | |
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2661 | 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 | 2662 | apply (cases "p = 0", simp) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2663 | apply (cases "order a p", simp) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2664 | 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 | 2665 | apply (drule not_less_Least, simp) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2666 | apply (fold order_def, simp) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2667 | done | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2668 | |
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2669 | 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 | 2670 | unfolding order_def | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2671 | apply (rule LeastI_ex) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2672 | apply (rule_tac x="degree p" in exI) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2673 | apply (rule notI) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2674 | apply (drule (1) dvd_imp_degree_le) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2675 | apply (simp only: degree_linear_power) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2676 | done | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2677 | |
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2678 | lemma order: | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2679 | "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 | 2680 | by (rule conjI [OF order_1 order_2]) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2681 | |
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2682 | lemma order_degree: | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2683 | assumes p: "p \<noteq> 0" | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2684 | shows "order a p \<le> degree p" | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2685 | proof - | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2686 | 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 | 2687 | by (simp only: degree_linear_power) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2688 | also have "\<dots> \<le> degree p" | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2689 | 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 | 2690 | finally show ?thesis . | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2691 | qed | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2692 | |
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2693 | 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 | 2694 | apply (cases "p = 0", simp_all) | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2695 | apply (rule iffI) | 
| 56383 | 2696 | apply (metis order_2 not_gr0 poly_eq_0_iff_dvd power_0 power_Suc_0 power_one_right) | 
| 2697 | unfolding poly_eq_0_iff_dvd | |
| 2698 | apply (metis dvd_power dvd_trans order_1) | |
| 29977 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2699 | done | 
| 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2700 | |
| 62065 | 2701 | lemma order_0I: "poly p a \<noteq> 0 \<Longrightarrow> order a p = 0" | 
| 2702 | by (subst (asm) order_root) auto | |
| 2703 | ||
| 29977 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 huffman parents: 
29904diff
changeset | 2704 | |
| 62065 | 2705 | subsection \<open>Additional induction rules on polynomials\<close> | 
| 2706 | ||
| 2707 | text \<open> | |
| 2708 | An induction rule for induction over the roots of a polynomial with a certain property. | |
| 2709 | (e.g. all positive roots) | |
| 2710 | \<close> | |
| 2711 | lemma poly_root_induct [case_names 0 no_roots root]: | |
| 2712 | fixes p :: "'a :: idom poly" | |
| 2713 | assumes "Q 0" | |
| 2714 | assumes "\<And>p. (\<And>a. P a \<Longrightarrow> poly p a \<noteq> 0) \<Longrightarrow> Q p" | |
| 2715 | assumes "\<And>a p. P a \<Longrightarrow> Q p \<Longrightarrow> Q ([:a, -1:] * p)" | |
| 2716 | shows "Q p" | |
| 2717 | proof (induction "degree p" arbitrary: p rule: less_induct) | |
| 2718 | case (less p) | |
| 2719 | show ?case | |
| 2720 | proof (cases "p = 0") | |
| 2721 | assume nz: "p \<noteq> 0" | |
| 2722 | show ?case | |
| 2723 | proof (cases "\<exists>a. P a \<and> poly p a = 0") | |
| 2724 | case False | |
| 2725 | thus ?thesis by (intro assms(2)) blast | |
| 2726 | next | |
| 2727 | case True | |
| 2728 | then obtain a where a: "P a" "poly p a = 0" | |
| 2729 | by blast | |
| 2730 | hence "-[:-a, 1:] dvd p" | |
| 2731 | by (subst minus_dvd_iff) (simp add: poly_eq_0_iff_dvd) | |
| 2732 | then obtain q where q: "p = [:a, -1:] * q" by (elim dvdE) simp | |
| 2733 | with nz have q_nz: "q \<noteq> 0" by auto | |
| 2734 | have "degree p = Suc (degree q)" | |
| 2735 | by (subst q, subst degree_mult_eq) (simp_all add: q_nz) | |
| 2736 | hence "Q q" by (intro less) simp | |
| 2737 | from a(1) and this have "Q ([:a, -1:] * q)" | |
| 2738 | by (rule assms(3)) | |
| 2739 | with q show ?thesis by simp | |
| 2740 | qed | |
| 2741 | qed (simp add: assms(1)) | |
| 2742 | qed | |
| 2743 | ||
| 2744 | lemma dropWhile_replicate_append: | |
| 2745 | "dropWhile (op= a) (replicate n a @ ys) = dropWhile (op= a) ys" | |
| 2746 | by (induction n) simp_all | |
| 2747 | ||
| 2748 | lemma Poly_append_replicate_0: "Poly (xs @ replicate n 0) = Poly xs" | |
| 2749 | by (subst coeffs_eq_iff) (simp_all add: strip_while_def dropWhile_replicate_append) | |
| 2750 | ||
| 2751 | text \<open> | |
| 2752 | An induction rule for simultaneous induction over two polynomials, | |
| 2753 | prepending one coefficient in each step. | |
| 2754 | \<close> | |
| 2755 | lemma poly_induct2 [case_names 0 pCons]: | |
| 2756 | assumes "P 0 0" "\<And>a p b q. P p q \<Longrightarrow> P (pCons a p) (pCons b q)" | |
| 2757 | shows "P p q" | |
| 2758 | proof - | |
| 63040 | 2759 | define n where "n = max (length (coeffs p)) (length (coeffs q))" | 
| 2760 | define xs where "xs = coeffs p @ (replicate (n - length (coeffs p)) 0)" | |
| 2761 | define ys where "ys = coeffs q @ (replicate (n - length (coeffs q)) 0)" | |
| 62065 | 2762 | have "length xs = length ys" | 
| 2763 | by (simp add: xs_def ys_def n_def) | |
| 2764 | hence "P (Poly xs) (Poly ys)" | |
| 2765 | by (induction rule: list_induct2) (simp_all add: assms) | |
| 2766 | also have "Poly xs = p" | |
| 2767 | by (simp add: xs_def Poly_append_replicate_0) | |
| 2768 | also have "Poly ys = q" | |
| 2769 | by (simp add: ys_def Poly_append_replicate_0) | |
| 2770 | finally show ?thesis . | |
| 2771 | qed | |
| 2772 | ||
| 2773 | ||
| 60500 | 2774 | subsection \<open>Composition of polynomials\<close> | 
| 29478 | 2775 | |
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2776 | (* 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 | 2777 | |
| 52380 | 2778 | definition pcompose :: "'a::comm_semiring_0 poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" | 
| 2779 | where | |
| 2780 | "pcompose p q = fold_coeffs (\<lambda>a c. [:a:] + q * c) p 0" | |
| 2781 | ||
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2782 | 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 | 2783 | |
| 52380 | 2784 | lemma pcompose_0 [simp]: | 
| 2785 | "pcompose 0 q = 0" | |
| 2786 | 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 | 2787 | |
| 52380 | 2788 | lemma pcompose_pCons: | 
| 2789 | "pcompose (pCons a p) q = [:a:] + q * pcompose p q" | |
| 2790 | by (cases "p = 0 \<and> a = 0") (auto simp add: pcompose_def) | |
| 2791 | ||
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2792 | lemma pcompose_1: | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2793 | 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 | 2794 | shows "pcompose 1 p = 1" | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2795 | 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 | 2796 | |
| 52380 | 2797 | lemma poly_pcompose: | 
| 2798 | "poly (pcompose p q) x = poly p (poly q x)" | |
| 2799 | by (induct p) (simp_all add: pcompose_pCons) | |
| 2800 | ||
| 2801 | lemma degree_pcompose_le: | |
| 2802 | "degree (pcompose p q) \<le> degree p * degree q" | |
| 2803 | apply (induct p, simp) | |
| 2804 | apply (simp add: pcompose_pCons, clarify) | |
| 2805 | apply (rule degree_add_le, simp) | |
| 2806 | apply (rule order_trans [OF degree_mult_le], simp) | |
| 29478 | 2807 | done | 
| 2808 | ||
| 62065 | 2809 | lemma pcompose_add: | 
| 2810 |   fixes p q r :: "'a :: {comm_semiring_0, ab_semigroup_add} poly"
 | |
| 2811 | shows "pcompose (p + q) r = pcompose p r + pcompose q r" | |
| 2812 | proof (induction p q rule: poly_induct2) | |
| 2813 | case (pCons a p b q) | |
| 2814 | have "pcompose (pCons a p + pCons b q) r = | |
| 2815 | [:a + b:] + r * pcompose p r + r * pcompose q r" | |
| 2816 | by (simp_all add: pcompose_pCons pCons.IH algebra_simps) | |
| 2817 | also have "[:a + b:] = [:a:] + [:b:]" by simp | |
| 2818 | also have "\<dots> + r * pcompose p r + r * pcompose q r = | |
| 2819 | pcompose (pCons a p) r + pcompose (pCons b q) r" | |
| 2820 | by (simp only: pcompose_pCons add_ac) | |
| 2821 | finally show ?case . | |
| 2822 | qed simp | |
| 2823 | ||
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2824 | lemma pcompose_uminus: | 
| 62065 | 2825 | fixes p r :: "'a :: comm_ring poly" | 
| 2826 | shows "pcompose (-p) r = -pcompose p r" | |
| 2827 | by (induction p) (simp_all add: pcompose_pCons) | |
| 2828 | ||
| 2829 | lemma pcompose_diff: | |
| 2830 | fixes p q r :: "'a :: comm_ring poly" | |
| 2831 | 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 | 2832 | using pcompose_add[of p "-q"] by (simp add: pcompose_uminus) | 
| 62065 | 2833 | |
| 2834 | lemma pcompose_smult: | |
| 2835 | fixes p r :: "'a :: comm_semiring_0 poly" | |
| 2836 | shows "pcompose (smult a p) r = smult a (pcompose p r)" | |
| 2837 | by (induction p) | |
| 2838 | (simp_all add: pcompose_pCons pcompose_add smult_add_right) | |
| 2839 | ||
| 2840 | lemma pcompose_mult: | |
| 2841 | fixes p q r :: "'a :: comm_semiring_0 poly" | |
| 2842 | shows "pcompose (p * q) r = pcompose p r * pcompose q r" | |
| 2843 | by (induction p arbitrary: q) | |
| 2844 | (simp_all add: pcompose_add pcompose_smult pcompose_pCons algebra_simps) | |
| 2845 | ||
| 2846 | lemma pcompose_assoc: | |
| 2847 | "pcompose p (pcompose q r :: 'a :: comm_semiring_0 poly ) = | |
| 2848 | pcompose (pcompose p q) r" | |
| 2849 | by (induction p arbitrary: q) | |
| 2850 | (simp_all add: pcompose_pCons pcompose_add pcompose_mult) | |
| 2851 | ||
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2852 | lemma pcompose_idR[simp]: | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2853 | 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 | 2854 | 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 | 2855 | 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 | 2856 | |
| 62065 | 2857 | |
| 2858 | (* The remainder of this section and the next were contributed by Wenda Li *) | |
| 2859 | ||
| 2860 | lemma degree_mult_eq_0: | |
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2861 | fixes p q:: "'a :: semidom poly" | 
| 62065 | 2862 | 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)" | 
| 2863 | by (auto simp add:degree_mult_eq) | |
| 2864 | ||
| 2865 | lemma pcompose_const[simp]:"pcompose [:a:] q = [:a:]" by (subst pcompose_pCons,simp) | |
| 2866 | ||
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2867 | 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 | 2868 | by (induct p) (auto simp add:pcompose_pCons) | 
| 62065 | 2869 | |
| 2870 | lemma degree_pcompose: | |
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2871 | fixes p q:: "'a::semidom poly" | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2872 | shows "degree (pcompose p q) = degree p * degree q" | 
| 62065 | 2873 | proof (induct p) | 
| 2874 | case 0 | |
| 2875 | thus ?case by auto | |
| 2876 | next | |
| 2877 | case (pCons a p) | |
| 2878 | have "degree (q * pcompose p q) = 0 \<Longrightarrow> ?case" | |
| 2879 | proof (cases "p=0") | |
| 2880 | case True | |
| 2881 | thus ?thesis by auto | |
| 2882 | next | |
| 2883 | 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 | 2884 | hence "degree q=0 \<or> pcompose p q=0" by (auto simp add: degree_mult_eq_0) | 
| 62072 | 2885 | moreover have "\<lbrakk>pcompose p q=0;degree q\<noteq>0\<rbrakk> \<Longrightarrow> False" using pCons.hyps(2) \<open>p\<noteq>0\<close> | 
| 62065 | 2886 | proof - | 
| 2887 | assume "pcompose p q=0" "degree q\<noteq>0" | |
| 2888 | hence "degree p=0" using pCons.hyps(2) by auto | |
| 2889 | then obtain a1 where "p=[:a1:]" | |
| 2890 | by (metis degree_pCons_eq_if old.nat.distinct(2) pCons_cases) | |
| 62072 | 2891 | thus False using \<open>pcompose p q=0\<close> \<open>p\<noteq>0\<close> by auto | 
| 62065 | 2892 | qed | 
| 2893 | ultimately have "degree (pCons a p) * degree q=0" by auto | |
| 2894 | moreover have "degree (pcompose (pCons a p) q) = 0" | |
| 2895 | proof - | |
| 2896 | have" 0 = max (degree [:a:]) (degree (q*pcompose p q))" | |
| 62072 | 2897 | using \<open>degree (q * pcompose p q) = 0\<close> by simp | 
| 62065 | 2898 | also have "... \<ge> degree ([:a:] + q * pcompose p q)" | 
| 2899 | by (rule degree_add_le_max) | |
| 2900 | finally show ?thesis by (auto simp add:pcompose_pCons) | |
| 2901 | qed | |
| 2902 | ultimately show ?thesis by simp | |
| 2903 | qed | |
| 2904 | moreover have "degree (q * pcompose p q)>0 \<Longrightarrow> ?case" | |
| 2905 | proof - | |
| 2906 | assume asm:"0 < degree (q * pcompose p q)" | |
| 2907 | hence "p\<noteq>0" "q\<noteq>0" "pcompose p q\<noteq>0" by auto | |
| 2908 | have "degree (pcompose (pCons a p) q) = degree ( q * pcompose p q)" | |
| 2909 | unfolding pcompose_pCons | |
| 2910 | using degree_add_eq_right[of "[:a:]" ] asm by auto | |
| 2911 | thus ?thesis | |
| 62072 | 2912 | using pCons.hyps(2) degree_mult_eq[OF \<open>q\<noteq>0\<close> \<open>pcompose p q\<noteq>0\<close>] by auto | 
| 62065 | 2913 | qed | 
| 2914 | ultimately show ?case by blast | |
| 2915 | qed | |
| 2916 | ||
| 2917 | lemma pcompose_eq_0: | |
| 62128 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2918 | fixes p q:: "'a :: semidom poly" | 
| 
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
 eberlm parents: 
62072diff
changeset | 2919 | 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 | 2920 | shows "p = 0" | 
| 62065 | 2921 | proof - | 
| 2922 | have "degree p=0" using assms degree_pcompose[of p q] by auto | |
| 2923 | then obtain a where "p=[:a:]" | |
| 2924 | by (metis degree_pCons_eq_if gr0_conv_Suc neq0_conv pCons_cases) | |
| 2925 | hence "a=0" using assms(1) by auto | |
| 62072 | 2926 | thus ?thesis using \<open>p=[:a:]\<close> by simp | 
| 62065 | 2927 | qed | 
| 2928 | ||
| 2929 | ||
| 62072 | 2930 | subsection \<open>Leading coefficient\<close> | 
| 62065 | 2931 | |
| 2932 | definition lead_coeff:: "'a::zero poly \<Rightarrow> 'a" where | |
| 2933 | "lead_coeff p= coeff p (degree p)" | |
| 2934 | ||
| 2935 | lemma lead_coeff_pCons[simp]: | |
| 2936 | "p\<noteq>0 \<Longrightarrow>lead_coeff (pCons a p) = lead_coeff p" | |
| 2937 | "p=0 \<Longrightarrow> lead_coeff (pCons a p) = a" | |
| 2938 | unfolding lead_coeff_def by auto | |
| 2939 | ||
| 2940 | lemma lead_coeff_0[simp]:"lead_coeff 0 =0" | |
| 2941 | unfolding lead_coeff_def by auto | |
| 2942 | ||
| 2943 | lemma lead_coeff_mult: | |
| 2944 | fixes p q::"'a ::idom poly" | |
| 2945 | shows "lead_coeff (p * q) = lead_coeff p * lead_coeff q" | |
| 2946 | by (unfold lead_coeff_def,cases "p=0 \<or> q=0",auto simp add:coeff_mult_degree_sum degree_mult_eq) | |
| 2947 | ||
| 2948 | lemma lead_coeff_add_le: | |
| 2949 | assumes "degree p < degree q" | |
| 2950 | shows "lead_coeff (p+q) = lead_coeff q" | |
| 2951 | using assms unfolding lead_coeff_def | |
| 2952 | by (metis coeff_add coeff_eq_0 monoid_add_class.add.left_neutral degree_add_eq_right) | |
| 2953 | ||
| 2954 | lemma lead_coeff_minus: | |
| 2955 | "lead_coeff (-p) = - lead_coeff p" | |
| 2956 | by (metis coeff_minus degree_minus lead_coeff_def) | |
| 2957 | ||
| 2958 | ||
| 2959 | lemma lead_coeff_comp: | |
| 2960 | fixes p q:: "'a::idom poly" | |
| 2961 | assumes "degree q > 0" | |
| 2962 | shows "lead_coeff (pcompose p q) = lead_coeff p * lead_coeff q ^ (degree p)" | |
| 2963 | proof (induct p) | |
| 2964 | case 0 | |
| 2965 | thus ?case unfolding lead_coeff_def by auto | |
| 2966 | next | |
| 2967 | case (pCons a p) | |
| 2968 | have "degree ( q * pcompose p q) = 0 \<Longrightarrow> ?case" | |
| 2969 | proof - | |
| 2970 | assume "degree ( q * pcompose p q) = 0" | |
| 2971 | hence "pcompose p q = 0" by (metis assms degree_0 degree_mult_eq_0 neq0_conv) | |
| 62072 | 2972 | hence "p=0" using pcompose_eq_0[OF _ \<open>degree q > 0\<close>] by simp | 
| 62065 | 2973 | thus ?thesis by auto | 
| 2974 | qed | |
| 2975 | moreover have "degree ( q * pcompose p q) > 0 \<Longrightarrow> ?case" | |
| 2976 | proof - | |
| 2977 | assume "degree ( q * pcompose p q) > 0" | |
| 2978 | hence "lead_coeff (pcompose (pCons a p) q) =lead_coeff ( q * pcompose p q)" | |
| 2979 | by (auto simp add:pcompose_pCons lead_coeff_add_le) | |
| 2980 | also have "... = lead_coeff q * (lead_coeff p * lead_coeff q ^ degree p)" | |
| 2981 | using pCons.hyps(2) lead_coeff_mult[of q "pcompose p q"] by simp | |
| 2982 | also have "... = lead_coeff p * lead_coeff q ^ (degree p + 1)" | |
| 2983 | by auto | |
| 2984 | finally show ?thesis by auto | |
| 2985 | qed | |
| 2986 | ultimately show ?case by blast | |
| 2987 | qed | |
| 2988 | ||
| 2989 | lemma lead_coeff_smult: | |
| 2990 | "lead_coeff (smult c p :: 'a :: idom poly) = c * lead_coeff p" | |
| 2991 | proof - | |
| 2992 | have "smult c p = [:c:] * p" by simp | |
| 2993 | also have "lead_coeff \<dots> = c * lead_coeff p" | |
| 2994 | by (subst lead_coeff_mult) simp_all | |
| 2995 | finally show ?thesis . | |
| 2996 | qed | |
| 2997 | ||
| 2998 | lemma lead_coeff_1 [simp]: "lead_coeff 1 = 1" | |
| 2999 | by (simp add: lead_coeff_def) | |
| 3000 | ||
| 3001 | lemma lead_coeff_of_nat [simp]: | |
| 3002 |   "lead_coeff (of_nat n) = (of_nat n :: 'a :: {comm_semiring_1,semiring_char_0})"
 | |
| 3003 | by (induction n) (simp_all add: lead_coeff_def of_nat_poly) | |
| 3004 | ||
| 3005 | lemma lead_coeff_numeral [simp]: | |
| 3006 | "lead_coeff (numeral n) = numeral n" | |
| 3007 | unfolding lead_coeff_def | |
| 3008 | by (subst of_nat_numeral [symmetric], subst of_nat_poly) simp | |
| 3009 | ||
| 3010 | lemma lead_coeff_power: | |
| 3011 | "lead_coeff (p ^ n :: 'a :: idom poly) = lead_coeff p ^ n" | |
| 3012 | by (induction n) (simp_all add: lead_coeff_mult) | |
| 3013 | ||
| 3014 | lemma lead_coeff_nonzero: "p \<noteq> 0 \<Longrightarrow> lead_coeff p \<noteq> 0" | |
| 3015 | by (simp add: lead_coeff_def) | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3016 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3017 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3018 | subsection \<open>Derivatives of univariate polynomials\<close> | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3019 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3020 | function pderiv :: "('a :: semidom) poly \<Rightarrow> 'a poly"
 | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3021 | where | 
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 3022 | "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 | 3023 | by (auto intro: pCons_cases) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3024 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3025 | termination pderiv | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3026 | by (relation "measure degree") simp_all | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3027 | |
| 63027 
8de0ebee3f1c
several updates on polynomial long division and pseudo division
 Rene Thiemann <rene.thiemann@uibk.ac.at> parents: 
62422diff
changeset | 3028 | 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 | 3029 | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3030 | lemma pderiv_0 [simp]: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3031 | "pderiv 0 = 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3032 | using pderiv.simps [of 0 0] by simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3033 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3034 | lemma pderiv_pCons: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3035 | "pderiv (pCons a p) = p + pCons 0 (pderiv p)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3036 | by (simp add: pderiv.simps) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3037 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3038 | lemma pderiv_1 [simp]: "pderiv 1 = 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3039 | unfolding one_poly_def by (simp add: pderiv_pCons) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3040 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3041 | lemma pderiv_of_nat [simp]: "pderiv (of_nat n) = 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3042 | and pderiv_numeral [simp]: "pderiv (numeral m) = 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3043 | 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 | 3044 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3045 | 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 | 3046 | by (induct p arbitrary: n) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3047 | (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 | 3048 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3049 | fun pderiv_coeffs_code :: "('a :: semidom) \<Rightarrow> 'a list \<Rightarrow> 'a list" where
 | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3050 | "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 | 3051 | | "pderiv_coeffs_code f [] = []" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3052 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3053 | definition pderiv_coeffs :: "('a :: semidom) list \<Rightarrow> 'a list" where
 | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3054 | "pderiv_coeffs xs = pderiv_coeffs_code 1 (tl xs)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3055 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3056 | (* 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 | 3057 | lemma pderiv_coeffs_code: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3058 | "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 | 3059 | proof (induct xs arbitrary: f n) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3060 | case (Cons x xs f n) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3061 | show ?case | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3062 | proof (cases n) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3063 | case 0 | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3064 | 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 | 3065 | next | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3066 | case (Suc m) note n = this | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3067 | show ?thesis | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3068 | 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 | 3069 | case False | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3070 | 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 | 3071 | nth_default 0 (pderiv_coeffs_code (f + 1) xs) m" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3072 | by (auto simp: cCons_def n) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3073 | 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 | 3074 | unfolding Cons by (simp add: n add_ac) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3075 | finally show ?thesis by (simp add: n) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3076 | next | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3077 | case True | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3078 |       {
 | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3079 | fix g | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3080 | 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 | 3081 | proof (induct xs arbitrary: g m) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3082 | case (Cons x xs g) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3083 | 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 | 3084 | and g: "(g = 0 \<or> x = 0)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3085 | by (auto simp: cCons_def split: if_splits) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3086 | note IH = Cons(1)[OF empty] | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3087 | from IH[of m] IH[of "m - 1"] g | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3088 | show ?case by (cases m, auto simp: field_simps) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3089 | qed simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3090 | } note empty = this | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3091 | 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 | 3092 | by (auto simp: cCons_def n) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3093 | 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 | 3094 | 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 | 3095 | ultimately show ?thesis by simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3096 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3097 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3098 | qed simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3099 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3100 | 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 | 3101 | by (induct n arbitrary: f, auto) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3102 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3103 | lemma coeffs_pderiv_code [code abstract]: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3104 | "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 | 3105 | 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 | 3106 | case (1 n) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3107 | 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 | 3108 | 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 | 3109 | 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 | 3110 | next | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3111 | case 2 | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3112 | 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 | 3113 | from 2 show ?case | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3114 | 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 | 3115 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3116 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3117 | context | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3118 |   assumes "SORT_CONSTRAINT('a::{semidom, semiring_char_0})"
 | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3119 | begin | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3120 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3121 | lemma pderiv_eq_0_iff: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3122 | "pderiv (p :: 'a poly) = 0 \<longleftrightarrow> degree p = 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3123 | apply (rule iffI) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3124 | apply (cases p, simp) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3125 | 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 | 3126 | 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 | 3127 | done | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3128 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3129 | 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 | 3130 | apply (rule order_antisym [OF degree_le]) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3131 | apply (simp add: coeff_pderiv coeff_eq_0) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3132 | apply (cases "degree p", simp) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3133 | apply (rule le_degree) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3134 | apply (simp add: coeff_pderiv del: of_nat_Suc) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3135 | 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 | 3136 | done | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3137 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3138 | lemma not_dvd_pderiv: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3139 | assumes "degree (p :: 'a poly) \<noteq> 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3140 | shows "\<not> p dvd pderiv p" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3141 | proof | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3142 | assume dvd: "p dvd pderiv p" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3143 | 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 | 3144 | from dvd have le: "degree p \<le> degree (pderiv p)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3145 | 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 | 3146 | from this[unfolded degree_pderiv] assms show False by auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3147 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3148 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3149 | 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 | 3150 | 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 | 3151 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3152 | end | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3153 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3154 | lemma pderiv_singleton [simp]: "pderiv [:a:] = 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3155 | by (simp add: pderiv_pCons) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3156 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3157 | lemma pderiv_add: "pderiv (p + q) = pderiv p + pderiv q" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3158 | by (rule poly_eqI, simp add: coeff_pderiv algebra_simps) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3159 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3160 | lemma pderiv_minus: "pderiv (- p :: 'a :: idom poly) = - pderiv p" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3161 | by (rule poly_eqI, simp add: coeff_pderiv algebra_simps) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3162 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3163 | lemma pderiv_diff: "pderiv (p - q) = pderiv p - pderiv q" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3164 | by (rule poly_eqI, simp add: coeff_pderiv algebra_simps) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3165 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3166 | 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 | 3167 | by (rule poly_eqI, simp add: coeff_pderiv algebra_simps) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3168 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3169 | 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 | 3170 | 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 | 3171 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3172 | lemma pderiv_power_Suc: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3173 | "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 | 3174 | apply (induct n) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3175 | apply simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3176 | apply (subst power_Suc) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3177 | apply (subst pderiv_mult) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3178 | apply (erule ssubst) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3179 | 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 | 3180 | apply (simp add: algebra_simps) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3181 | done | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3182 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3183 | lemma pderiv_setprod: "pderiv (setprod f (as)) = | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3184 |   (\<Sum>a \<in> as. setprod f (as - {a}) * pderiv (f a))"
 | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3185 | proof (induct as rule: infinite_finite_induct) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3186 | case (insert a as) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3187 | hence id: "setprod f (insert a as) = f a * setprod f as" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3188 | "\<And> g. setsum g (insert a as) = g a + setsum g as" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3189 |     "insert a as - {a} = as"
 | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3190 | by auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3191 |   {
 | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3192 | fix b | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3193 | assume "b \<in> as" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3194 |     hence id2: "insert a as - {b} = insert a (as - {b})" using \<open>a \<notin> as\<close> by auto
 | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3195 |     have "setprod f (insert a as - {b}) = f a * setprod f (as - {b})"
 | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3196 | unfolding id2 | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3197 | by (subst setprod.insert, insert insert, auto) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3198 | } note id2 = this | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3199 | show ?case | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3200 | unfolding id pderiv_mult insert(3) setsum_right_distrib | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3201 | by (auto simp add: ac_simps id2 intro!: setsum.cong) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3202 | qed auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3203 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3204 | 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 | 3205 | by (rule DERIV_cong, rule DERIV_pow, simp) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3206 | declare DERIV_pow2 [simp] DERIV_pow [simp] | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3207 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3208 | 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 | 3209 | by (rule DERIV_cong, rule DERIV_add, auto) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3210 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3211 | 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 | 3212 | 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 | 3213 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3214 | lemma continuous_on_poly [continuous_intros]: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3215 |   fixes p :: "'a :: {real_normed_field} poly"
 | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3216 | assumes "continuous_on A f" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3217 | shows "continuous_on A (\<lambda>x. poly p (f x))" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3218 | proof - | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3219 | 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 | 3220 | by (intro continuous_intros assms) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3221 | 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 | 3222 | finally show ?thesis . | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3223 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3224 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3225 | text\<open>Consequences of the derivative theorem above\<close> | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3226 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3227 | 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 | 3228 | apply (simp add: real_differentiable_def) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3229 | apply (blast intro: poly_DERIV) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3230 | done | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3231 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3232 | 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 | 3233 | by (rule poly_DERIV [THEN DERIV_isCont]) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3234 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3235 | 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 | 3236 | ==> \<exists>x. a < x & x < b & (poly p x = 0)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3237 | using IVT_objl [of "poly p" a 0 b] | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3238 | by (auto simp add: order_le_less) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3239 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3240 | 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 | 3241 | ==> \<exists>x. a < x & x < b & (poly p x = 0)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3242 | by (insert poly_IVT_pos [where p = "- p" ]) simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3243 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3244 | lemma poly_IVT: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3245 | fixes p::"real poly" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3246 | 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 | 3247 | 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 | 3248 | 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 | 3249 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3250 | lemma poly_MVT: "(a::real) < b ==> | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3251 | \<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 | 3252 | using MVT [of a b "poly p"] | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3253 | apply auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3254 | apply (rule_tac x = z in exI) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3255 | 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 | 3256 | done | 
| 
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 poly_MVT': | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3259 |   assumes "{min a b..max a b} \<subseteq> A"
 | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3260 | 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 | 3261 | proof (cases a b rule: linorder_cases) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3262 | case less | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3263 | 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 | 3264 | 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 | 3265 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3266 | next | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3267 | case greater | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3268 | 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 | 3269 | 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 | 3270 | qed (insert assms, auto) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3271 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3272 | lemma poly_pinfty_gt_lc: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3273 | fixes p:: "real poly" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3274 | assumes "lead_coeff p > 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3275 | shows "\<exists> n. \<forall> x \<ge> n. poly p x \<ge> lead_coeff p" using assms | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3276 | proof (induct p) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3277 | case 0 | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3278 | thus ?case by auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3279 | next | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3280 | case (pCons a p) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3281 | have "\<lbrakk>a\<noteq>0;p=0\<rbrakk> \<Longrightarrow> ?case" by auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3282 | moreover have "p\<noteq>0 \<Longrightarrow> ?case" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3283 | proof - | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3284 | assume "p\<noteq>0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3285 | then obtain n1 where gte_lcoeff:"\<forall>x\<ge>n1. lead_coeff p \<le> poly p x" using that pCons by auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3286 | have gt_0:"lead_coeff p >0" using pCons(3) \<open>p\<noteq>0\<close> by auto | 
| 63040 | 3287 | define n where "n = max n1 (1+ \<bar>a\<bar>/(lead_coeff p))" | 
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3288 | show ?thesis | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3289 | proof (rule_tac x=n in exI,rule,rule) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3290 | fix x assume "n \<le> x" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3291 | hence "lead_coeff p \<le> poly p x" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3292 | using gte_lcoeff unfolding n_def by auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3293 | hence " \<bar>a\<bar>/(lead_coeff p) \<ge> \<bar>a\<bar>/(poly p x)" and "poly p x>0" using gt_0 | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3294 | by (intro frac_le,auto) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3295 | hence "x\<ge>1+ \<bar>a\<bar>/(poly p x)" using \<open>n\<le>x\<close>[unfolded n_def] by auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3296 | thus "lead_coeff (pCons a p) \<le> poly (pCons a p) x" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3297 | using \<open>lead_coeff p \<le> poly p x\<close> \<open>poly p x>0\<close> \<open>p\<noteq>0\<close> | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3298 | by (auto simp add:field_simps) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3299 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3300 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3301 | ultimately show ?case by fastforce | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3302 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3303 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3304 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3305 | subsection \<open>Algebraic numbers\<close> | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3306 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3307 | text \<open> | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3308 | 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 | 3309 | 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 | 3310 | uses the rational definition, but we need the integer definition. | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3311 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3312 | 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 | 3313 | 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 | 3314 | \<close> | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3315 | subsection \<open>Algebraic numbers\<close> | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3316 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3317 | definition algebraic :: "'a :: field_char_0 \<Rightarrow> bool" where | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3318 | "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 | 3319 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3320 | lemma algebraicI: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3321 | 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 | 3322 | shows "algebraic x" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3323 | using assms unfolding algebraic_def by blast | 
| 62065 | 3324 | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3325 | lemma algebraicE: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3326 | assumes "algebraic x" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3327 | 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 | 3328 | using assms unfolding algebraic_def by blast | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3329 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3330 | lemma quotient_of_denom_pos': "snd (quotient_of x) > 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3331 | using quotient_of_denom_pos[OF surjective_pairing] . | 
| 62065 | 3332 | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3333 | lemma of_int_div_in_Ints: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3334 | "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 | 3335 | proof (cases "of_int b = (0 :: 'a)") | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3336 | assume "b dvd a" "of_int b \<noteq> (0::'a)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3337 | then obtain c where "a = b * c" by (elim dvdE) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3338 | 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 | 3339 | qed auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3340 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3341 | lemma of_int_divide_in_Ints: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3342 | "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 | 3343 | proof (cases "of_int b = (0 :: 'a)") | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3344 | assume "b dvd a" "of_int b \<noteq> (0::'a)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3345 | then obtain c where "a = b * c" by (elim dvdE) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3346 | 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 | 3347 | qed auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3348 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3349 | lemma algebraic_altdef: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3350 | fixes p :: "'a :: field_char_0 poly" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3351 | 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 | 3352 | proof safe | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3353 | fix p assume rat: "\<forall>i. coeff p i \<in> \<rat>" and root: "poly p x = 0" and nz: "p \<noteq> 0" | 
| 63040 | 3354 | define cs where "cs = coeffs p" | 
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3355 | from rat have "\<forall>c\<in>range (coeff p). \<exists>c'. c = of_rat c'" unfolding Rats_def by blast | 
| 63060 | 3356 | 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 | 3357 | by (subst (asm) bchoice_iff) blast | 
| 63040 | 3358 | define cs' where "cs' = map (quotient_of \<circ> f) (coeffs p)" | 
| 3359 | define d where "d = Lcm (set (map snd cs'))" | |
| 3360 | define p' where "p' = smult (of_int d) p" | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3361 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3362 | have "\<forall>n. coeff p' n \<in> \<int>" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3363 | proof | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3364 | fix n :: nat | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3365 | show "coeff p' n \<in> \<int>" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3366 | proof (cases "n \<le> degree p") | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3367 | case True | 
| 63040 | 3368 | define c where "c = coeff p n" | 
| 3369 | define a where "a = fst (quotient_of (f (coeff p n)))" | |
| 3370 | 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 | 3371 | 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 | 3372 | 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 | 3373 | 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 | 3374 | 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 | 3375 | (simp_all add: f [symmetric]) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3376 | 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 | 3377 | by (simp add: of_rat_mult of_rat_divide) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3378 | 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 | 3379 | 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 | 3380 | hence "b dvd (a * d)" unfolding d_def by simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3381 | 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 | 3382 | by (rule of_int_divide_in_Ints) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3383 | 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 | 3384 | finally show ?thesis . | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3385 | qed (auto simp: p'_def not_le coeff_eq_0) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3386 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3387 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3388 |   moreover have "set (map snd cs') \<subseteq> {0<..}"
 | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3389 | 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 | 3390 | 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 | 3391 | 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 | 3392 | 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 | 3393 | ultimately show "algebraic x" unfolding algebraic_def by blast | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3394 | next | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3395 | assume "algebraic x" | 
| 63060 | 3396 | 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 | 3397 | by (force simp: algebraic_def) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3398 | 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 | 3399 | 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 | 3400 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3401 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3402 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3403 | text\<open>Lemmas for Derivatives\<close> | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3404 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3405 | lemma order_unique_lemma: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3406 | fixes p :: "'a::idom poly" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3407 | 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 | 3408 | shows "n = order a p" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3409 | unfolding Polynomial.order_def | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3410 | apply (rule Least_equality [symmetric]) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3411 | apply (fact assms) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3412 | apply (rule classical) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3413 | apply (erule notE) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3414 | unfolding not_less_eq_eq | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3415 | using assms(1) apply (rule power_le_dvd) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3416 | apply assumption | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3417 | done | 
| 
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 lemma_order_pderiv1: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3420 | "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 | 3421 | smult (of_nat (Suc n)) (q * [:- a, 1:] ^ n)" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3422 | apply (simp only: pderiv_mult pderiv_power_Suc) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3423 | 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 | 3424 | done | 
| 
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 lemma_order_pderiv: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3427 | fixes p :: "'a :: field_char_0 poly" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3428 | assumes n: "0 < n" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3429 | and pd: "pderiv p \<noteq> 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3430 | and pe: "p = [:- a, 1:] ^ n * q" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3431 | and nd: "~ [:- a, 1:] dvd q" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3432 | shows "n = Suc (order a (pderiv p))" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3433 | using n | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3434 | proof - | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3435 | have "pderiv ([:- a, 1:] ^ n * q) \<noteq> 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3436 | using assms by auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3437 | 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 | 3438 | using assms by (cases n) auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3439 | 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 | 3440 | 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 | 3441 | have "n' = order a (pderiv ([:- a, 1:] ^ Suc n' * q))" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3442 | proof (rule order_unique_lemma) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3443 | 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 | 3444 | apply (subst lemma_order_pderiv1) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3445 | apply (rule dvd_add) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3446 | apply (metis dvdI dvd_mult2 power_Suc2) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3447 | apply (metis dvd_smult dvd_triv_right) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3448 | done | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3449 | next | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3450 | 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 | 3451 | apply (subst lemma_order_pderiv1) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3452 | 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 | 3453 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3454 | then show ?thesis | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3455 | by (metis \<open>n = Suc n'\<close> pe) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3456 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3457 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3458 | lemma order_decomp: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3459 | assumes "p \<noteq> 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3460 | 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 | 3461 | proof - | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3462 | 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 | 3463 | 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 | 3464 | 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 | 3465 | 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 | 3466 | by simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3467 | 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 | 3468 | by simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3469 | then have D: "\<not> [:- a, 1:] dvd q" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3470 | 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 | 3471 | by auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3472 | from C D show ?thesis by blast | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3473 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3474 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3475 | lemma order_pderiv: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3476 | "\<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 | 3477 | (order a p = Suc (order a (pderiv p)))" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3478 | apply (case_tac "p = 0", simp) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3479 | 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 | 3480 | using neq0_conv | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3481 | apply (blast intro: lemma_order_pderiv) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3482 | done | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3483 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3484 | 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 | 3485 | proof - | 
| 63040 | 3486 | define i where "i = order a p" | 
| 3487 | define j where "j = order a q" | |
| 3488 | define t where "t = [:-a, 1:]" | |
| 62352 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3489 | 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 | 3490 | 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 | 3491 | assume "p * q \<noteq> 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3492 | then show "order a (p * q) = i + j" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3493 | apply clarsimp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3494 | 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 | 3495 | 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 | 3496 | apply clarify | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3497 | apply (erule dvdE)+ | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3498 | apply (rule order_unique_lemma [symmetric], fold t_def) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3499 | apply (simp_all add: power_add t_dvd_iff) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3500 | done | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3501 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3502 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3503 | lemma order_smult: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3504 | assumes "c \<noteq> 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3505 | shows "order x (smult c p) = order x p" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3506 | proof (cases "p = 0") | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3507 | case False | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3508 | have "smult c p = [:c:] * p" by simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3509 | 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 | 3510 | by (subst order_mult) simp_all | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3511 | 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 | 3512 | finally show ?thesis by simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3513 | qed simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3514 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3515 | (* Next two lemmas contributed by Wenda Li *) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3516 | lemma order_1_eq_0 [simp]:"order x 1 = 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3517 | by (metis order_root poly_1 zero_neq_one) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3518 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3519 | 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 | 3520 | 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 | 3521 | case 0 | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3522 | 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 | 3523 | next | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3524 | case (Suc n) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3525 | 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 | 3526 | 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 | 3527 | 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 | 3528 | moreover have "order a [:-a,1:]=1" unfolding order_def | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3529 | proof (rule Least_equality,rule ccontr) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3530 | assume "\<not> \<not> [:- a, 1:] ^ Suc 1 dvd [:- a, 1:]" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3531 | hence "[:- a, 1:] ^ Suc 1 dvd [:- a, 1:]" by simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3532 | hence "degree ([:- a, 1:] ^ Suc 1) \<le> degree ([:- a, 1:] )" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3533 | by (rule dvd_imp_degree_le,auto) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3534 | thus False by auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3535 | next | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3536 | 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 | 3537 | show "1 \<le> y" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3538 | proof (rule ccontr) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3539 | assume "\<not> 1 \<le> y" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3540 | hence "y=0" by auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3541 | hence "[:- a, 1:] ^ Suc y dvd [:- a, 1:]" by auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3542 | thus False using asm by auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3543 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3544 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3545 | ultimately show ?case using Suc by auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3546 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3547 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3548 | 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 | 3549 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3550 | 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 | 3551 | apply (cases "p = 0", auto) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3552 | apply (drule order_2 [where a=a and p=p]) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3553 | apply (metis not_less_eq_eq power_le_dvd) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3554 | apply (erule power_le_dvd [OF order_1]) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3555 | done | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3556 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3557 | lemma poly_squarefree_decomp_order: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3558 | assumes "pderiv (p :: 'a :: field_char_0 poly) \<noteq> 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3559 | and p: "p = q * d" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3560 | and p': "pderiv p = e * d" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3561 | and d: "d = r * p + s * pderiv p" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3562 | 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 | 3563 | proof (rule classical) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3564 | 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 | 3565 | 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 | 3566 | 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 | 3567 | by (simp add: order_mult) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3568 | 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 | 3569 | 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 | 3570 | 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 | 3571 | have "order a p = Suc (order a (pderiv p))" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3572 | 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 | 3573 | 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 | 3574 | have "([:-a, 1:] ^ (order a (pderiv p))) dvd d" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3575 | apply (simp add: d) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3576 | apply (rule dvd_add) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3577 | apply (rule dvd_mult) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3578 | apply (simp add: order_divides \<open>p \<noteq> 0\<close> | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3579 | \<open>order a p = Suc (order a (pderiv p))\<close>) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3580 | apply (rule dvd_mult) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3581 | apply (simp add: order_divides) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3582 | done | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3583 | then have "order a (pderiv p) \<le> order a d" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3584 | 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 | 3585 | show ?thesis | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3586 | 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 | 3587 | 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 | 3588 | 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 | 3589 | 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 | 3590 | by auto | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3591 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3592 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3593 | lemma poly_squarefree_decomp_order2: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3594 | "\<lbrakk>pderiv p \<noteq> (0 :: 'a :: field_char_0 poly); | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3595 | p = q * d; | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3596 | pderiv p = e * d; | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3597 | d = r * p + s * pderiv p | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3598 | \<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 | 3599 | by (blast intro: poly_squarefree_decomp_order) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3600 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3601 | lemma order_pderiv2: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3602 | "\<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 | 3603 | \<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 | 3604 | by (auto dest: order_pderiv) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3605 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3606 | definition | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3607 | rsquarefree :: "'a::idom poly => bool" where | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3608 | "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 | 3609 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3610 | lemma pderiv_iszero: "pderiv p = 0 \<Longrightarrow> \<exists>h. p = [:h :: 'a :: {semidom,semiring_char_0}:]"
 | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3611 | apply (simp add: pderiv_eq_0_iff) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3612 | apply (case_tac p, auto split: if_splits) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3613 | done | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3614 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3615 | lemma rsquarefree_roots: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3616 | fixes p :: "'a :: field_char_0 poly" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3617 | 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 | 3618 | apply (simp add: rsquarefree_def) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3619 | apply (case_tac "p = 0", simp, simp) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3620 | apply (case_tac "pderiv p = 0") | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3621 | apply simp | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3622 | apply (drule pderiv_iszero, clarsimp) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3623 | 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 | 3624 | apply (force simp add: order_root order_pderiv2) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3625 | done | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3626 | |
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3627 | lemma poly_squarefree_decomp: | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3628 | assumes "pderiv (p :: 'a :: field_char_0 poly) \<noteq> 0" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3629 | and "p = q * d" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3630 | and "pderiv p = e * d" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3631 | and "d = r * p + s * pderiv p" | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3632 | 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 | 3633 | proof - | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3634 | 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 | 3635 | 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 | 3636 | 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 | 3637 | using assms by (rule poly_squarefree_decomp_order2) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3638 | 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 | 3639 | by (simp add: rsquarefree_def order_root) | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3640 | qed | 
| 
35a9e1cbb5b3
separated potentially conflicting type class instance into separate theory
 haftmann parents: 
62351diff
changeset | 3641 | |
| 52380 | 3642 | |
| 3643 | no_notation cCons (infixr "##" 65) | |
| 31663 | 3644 | |
| 29478 | 3645 | end |