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