author | eberlm |
Mon, 11 Jan 2016 16:38:39 +0100 | |
changeset 62128 | 3201ddb00097 |
parent 62072 | bf3d9f113474 |
child 62351 | fd049b54ad68 |
permissions | -rw-r--r-- |
41959 | 1 |
(* Title: HOL/Library/Polynomial.thy |
29451 | 2 |
Author: Brian Huffman |
41959 | 3 |
Author: Clemens Ballarin |
52380 | 4 |
Author: Florian Haftmann |
29451 | 5 |
*) |
6 |
||
60500 | 7 |
section \<open>Polynomials as type over a ring structure\<close> |
29451 | 8 |
|
9 |
theory Polynomial |
|
60685
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
10 |
imports Main "~~/src/HOL/GCD" "~~/src/HOL/Library/More_List" "~~/src/HOL/Library/Infinite_Set" |
29451 | 11 |
begin |
12 |
||
60500 | 13 |
subsection \<open>Auxiliary: operations for lists (later) representing coefficients\<close> |
52380 | 14 |
|
15 |
definition cCons :: "'a::zero \<Rightarrow> 'a list \<Rightarrow> 'a list" (infixr "##" 65) |
|
16 |
where |
|
17 |
"x ## xs = (if xs = [] \<and> x = 0 then [] else x # xs)" |
|
18 |
||
19 |
lemma cCons_0_Nil_eq [simp]: |
|
20 |
"0 ## [] = []" |
|
21 |
by (simp add: cCons_def) |
|
22 |
||
23 |
lemma cCons_Cons_eq [simp]: |
|
24 |
"x ## y # ys = x # y # ys" |
|
25 |
by (simp add: cCons_def) |
|
26 |
||
27 |
lemma cCons_append_Cons_eq [simp]: |
|
28 |
"x ## xs @ y # ys = x # xs @ y # ys" |
|
29 |
by (simp add: cCons_def) |
|
30 |
||
31 |
lemma cCons_not_0_eq [simp]: |
|
32 |
"x \<noteq> 0 \<Longrightarrow> x ## xs = x # xs" |
|
33 |
by (simp add: cCons_def) |
|
34 |
||
35 |
lemma strip_while_not_0_Cons_eq [simp]: |
|
36 |
"strip_while (\<lambda>x. x = 0) (x # xs) = x ## strip_while (\<lambda>x. x = 0) xs" |
|
37 |
proof (cases "x = 0") |
|
38 |
case False then show ?thesis by simp |
|
39 |
next |
|
40 |
case True show ?thesis |
|
41 |
proof (induct xs rule: rev_induct) |
|
42 |
case Nil with True show ?case by simp |
|
43 |
next |
|
44 |
case (snoc y ys) then show ?case |
|
45 |
by (cases "y = 0") (simp_all add: append_Cons [symmetric] del: append_Cons) |
|
46 |
qed |
|
47 |
qed |
|
48 |
||
49 |
lemma tl_cCons [simp]: |
|
50 |
"tl (x ## xs) = xs" |
|
51 |
by (simp add: cCons_def) |
|
52 |
||
61585 | 53 |
subsection \<open>Definition of type \<open>poly\<close>\<close> |
29451 | 54 |
|
61260 | 55 |
typedef (overloaded) 'a poly = "{f :: nat \<Rightarrow> 'a::zero. \<forall>\<^sub>\<infinity> n. f n = 0}" |
59983
cd2efd7d06bd
replace almost_everywhere_zero by Infinite_Set.MOST
hoelzl
parents:
59815
diff
changeset
|
56 |
morphisms coeff Abs_poly by (auto intro!: ALL_MOST) |
29451 | 57 |
|
59487
adaa430fc0f7
default abstypes and default abstract equations make technical (no_code) annotation superfluous
haftmann
parents:
58881
diff
changeset
|
58 |
setup_lifting type_definition_poly |
52380 | 59 |
|
60 |
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
|
61 |
by (simp add: coeff_inject [symmetric] fun_eq_iff) |
29451 | 62 |
|
52380 | 63 |
lemma poly_eqI: "(\<And>n. coeff p n = coeff q n) \<Longrightarrow> p = q" |
64 |
by (simp add: poly_eq_iff) |
|
65 |
||
59983
cd2efd7d06bd
replace almost_everywhere_zero by Infinite_Set.MOST
hoelzl
parents:
59815
diff
changeset
|
66 |
lemma MOST_coeff_eq_0: "\<forall>\<^sub>\<infinity> n. coeff p n = 0" |
52380 | 67 |
using coeff [of p] by simp |
29451 | 68 |
|
69 |
||
60500 | 70 |
subsection \<open>Degree of a polynomial\<close> |
29451 | 71 |
|
52380 | 72 |
definition degree :: "'a::zero poly \<Rightarrow> nat" |
73 |
where |
|
29451 | 74 |
"degree p = (LEAST n. \<forall>i>n. coeff p i = 0)" |
75 |
||
52380 | 76 |
lemma coeff_eq_0: |
77 |
assumes "degree p < n" |
|
78 |
shows "coeff p n = 0" |
|
29451 | 79 |
proof - |
59983
cd2efd7d06bd
replace almost_everywhere_zero by Infinite_Set.MOST
hoelzl
parents:
59815
diff
changeset
|
80 |
have "\<exists>n. \<forall>i>n. coeff p i = 0" |
cd2efd7d06bd
replace almost_everywhere_zero by Infinite_Set.MOST
hoelzl
parents:
59815
diff
changeset
|
81 |
using MOST_coeff_eq_0 by (simp add: MOST_nat) |
52380 | 82 |
then have "\<forall>i>degree p. coeff p i = 0" |
29451 | 83 |
unfolding degree_def by (rule LeastI_ex) |
52380 | 84 |
with assms show ?thesis by simp |
29451 | 85 |
qed |
86 |
||
87 |
lemma le_degree: "coeff p n \<noteq> 0 \<Longrightarrow> n \<le> degree p" |
|
88 |
by (erule contrapos_np, rule coeff_eq_0, simp) |
|
89 |
||
90 |
lemma degree_le: "\<forall>i>n. coeff p i = 0 \<Longrightarrow> degree p \<le> n" |
|
91 |
unfolding degree_def by (erule Least_le) |
|
92 |
||
93 |
lemma less_degree_imp: "n < degree p \<Longrightarrow> \<exists>i>n. coeff p i \<noteq> 0" |
|
94 |
unfolding degree_def by (drule not_less_Least, simp) |
|
95 |
||
96 |
||
60500 | 97 |
subsection \<open>The zero polynomial\<close> |
29451 | 98 |
|
99 |
instantiation poly :: (zero) zero |
|
100 |
begin |
|
101 |
||
52380 | 102 |
lift_definition zero_poly :: "'a poly" |
59983
cd2efd7d06bd
replace almost_everywhere_zero by Infinite_Set.MOST
hoelzl
parents:
59815
diff
changeset
|
103 |
is "\<lambda>_. 0" by (rule MOST_I) simp |
29451 | 104 |
|
105 |
instance .. |
|
52380 | 106 |
|
29451 | 107 |
end |
108 |
||
52380 | 109 |
lemma coeff_0 [simp]: |
110 |
"coeff 0 n = 0" |
|
111 |
by transfer rule |
|
29451 | 112 |
|
52380 | 113 |
lemma degree_0 [simp]: |
114 |
"degree 0 = 0" |
|
29451 | 115 |
by (rule order_antisym [OF degree_le le0]) simp |
116 |
||
117 |
lemma leading_coeff_neq_0: |
|
52380 | 118 |
assumes "p \<noteq> 0" |
119 |
shows "coeff p (degree p) \<noteq> 0" |
|
29451 | 120 |
proof (cases "degree p") |
121 |
case 0 |
|
60500 | 122 |
from \<open>p \<noteq> 0\<close> have "\<exists>n. coeff p n \<noteq> 0" |
52380 | 123 |
by (simp add: poly_eq_iff) |
29451 | 124 |
then obtain n where "coeff p n \<noteq> 0" .. |
125 |
hence "n \<le> degree p" by (rule le_degree) |
|
60500 | 126 |
with \<open>coeff p n \<noteq> 0\<close> and \<open>degree p = 0\<close> |
29451 | 127 |
show "coeff p (degree p) \<noteq> 0" by simp |
128 |
next |
|
129 |
case (Suc n) |
|
60500 | 130 |
from \<open>degree p = Suc n\<close> have "n < degree p" by simp |
29451 | 131 |
hence "\<exists>i>n. coeff p i \<noteq> 0" by (rule less_degree_imp) |
132 |
then obtain i where "n < i" and "coeff p i \<noteq> 0" by fast |
|
60500 | 133 |
from \<open>degree p = Suc n\<close> and \<open>n < i\<close> have "degree p \<le> i" by simp |
134 |
also from \<open>coeff p i \<noteq> 0\<close> have "i \<le> degree p" by (rule le_degree) |
|
29451 | 135 |
finally have "degree p = i" . |
60500 | 136 |
with \<open>coeff p i \<noteq> 0\<close> show "coeff p (degree p) \<noteq> 0" by simp |
29451 | 137 |
qed |
138 |
||
52380 | 139 |
lemma leading_coeff_0_iff [simp]: |
140 |
"coeff p (degree p) = 0 \<longleftrightarrow> p = 0" |
|
29451 | 141 |
by (cases "p = 0", simp, simp add: leading_coeff_neq_0) |
142 |
||
143 |
||
60500 | 144 |
subsection \<open>List-style constructor for polynomials\<close> |
29451 | 145 |
|
52380 | 146 |
lift_definition pCons :: "'a::zero \<Rightarrow> 'a poly \<Rightarrow> 'a poly" |
55415 | 147 |
is "\<lambda>a p. case_nat a (coeff p)" |
59983
cd2efd7d06bd
replace almost_everywhere_zero by Infinite_Set.MOST
hoelzl
parents:
59815
diff
changeset
|
148 |
by (rule MOST_SucD) (simp add: MOST_coeff_eq_0) |
29451 | 149 |
|
52380 | 150 |
lemmas coeff_pCons = pCons.rep_eq |
29455 | 151 |
|
52380 | 152 |
lemma coeff_pCons_0 [simp]: |
153 |
"coeff (pCons a p) 0 = a" |
|
154 |
by transfer simp |
|
29455 | 155 |
|
52380 | 156 |
lemma coeff_pCons_Suc [simp]: |
157 |
"coeff (pCons a p) (Suc n) = coeff p n" |
|
29451 | 158 |
by (simp add: coeff_pCons) |
159 |
||
52380 | 160 |
lemma degree_pCons_le: |
161 |
"degree (pCons a p) \<le> Suc (degree p)" |
|
162 |
by (rule degree_le) (simp add: coeff_eq_0 coeff_pCons split: nat.split) |
|
29451 | 163 |
|
164 |
lemma degree_pCons_eq: |
|
165 |
"p \<noteq> 0 \<Longrightarrow> degree (pCons a p) = Suc (degree p)" |
|
52380 | 166 |
apply (rule order_antisym [OF degree_pCons_le]) |
167 |
apply (rule le_degree, simp) |
|
168 |
done |
|
29451 | 169 |
|
52380 | 170 |
lemma degree_pCons_0: |
171 |
"degree (pCons a 0) = 0" |
|
172 |
apply (rule order_antisym [OF _ le0]) |
|
173 |
apply (rule degree_le, simp add: coeff_pCons split: nat.split) |
|
174 |
done |
|
29451 | 175 |
|
29460
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
176 |
lemma degree_pCons_eq_if [simp]: |
29451 | 177 |
"degree (pCons a p) = (if p = 0 then 0 else Suc (degree p))" |
52380 | 178 |
apply (cases "p = 0", simp_all) |
179 |
apply (rule order_antisym [OF _ le0]) |
|
180 |
apply (rule degree_le, simp add: coeff_pCons split: nat.split) |
|
181 |
apply (rule order_antisym [OF degree_pCons_le]) |
|
182 |
apply (rule le_degree, simp) |
|
183 |
done |
|
29451 | 184 |
|
52380 | 185 |
lemma pCons_0_0 [simp]: |
186 |
"pCons 0 0 = 0" |
|
187 |
by (rule poly_eqI) (simp add: coeff_pCons split: nat.split) |
|
29451 | 188 |
|
189 |
lemma pCons_eq_iff [simp]: |
|
190 |
"pCons a p = pCons b q \<longleftrightarrow> a = b \<and> p = q" |
|
52380 | 191 |
proof safe |
29451 | 192 |
assume "pCons a p = pCons b q" |
193 |
then have "coeff (pCons a p) 0 = coeff (pCons b q) 0" by simp |
|
194 |
then show "a = b" by simp |
|
195 |
next |
|
196 |
assume "pCons a p = pCons b q" |
|
197 |
then have "\<forall>n. coeff (pCons a p) (Suc n) = |
|
198 |
coeff (pCons b q) (Suc n)" by simp |
|
52380 | 199 |
then show "p = q" by (simp add: poly_eq_iff) |
29451 | 200 |
qed |
201 |
||
52380 | 202 |
lemma pCons_eq_0_iff [simp]: |
203 |
"pCons a p = 0 \<longleftrightarrow> a = 0 \<and> p = 0" |
|
29451 | 204 |
using pCons_eq_iff [of a p 0 0] by simp |
205 |
||
206 |
lemma pCons_cases [cases type: poly]: |
|
207 |
obtains (pCons) a q where "p = pCons a q" |
|
208 |
proof |
|
209 |
show "p = pCons (coeff p 0) (Abs_poly (\<lambda>n. coeff p (Suc n)))" |
|
52380 | 210 |
by transfer |
59983
cd2efd7d06bd
replace almost_everywhere_zero by Infinite_Set.MOST
hoelzl
parents:
59815
diff
changeset
|
211 |
(simp_all add: MOST_inj[where f=Suc and P="\<lambda>n. p n = 0" for p] fun_eq_iff Abs_poly_inverse |
cd2efd7d06bd
replace almost_everywhere_zero by Infinite_Set.MOST
hoelzl
parents:
59815
diff
changeset
|
212 |
split: nat.split) |
29451 | 213 |
qed |
214 |
||
215 |
lemma pCons_induct [case_names 0 pCons, induct type: poly]: |
|
216 |
assumes zero: "P 0" |
|
54856 | 217 |
assumes pCons: "\<And>a p. a \<noteq> 0 \<or> p \<noteq> 0 \<Longrightarrow> P p \<Longrightarrow> P (pCons a p)" |
29451 | 218 |
shows "P p" |
219 |
proof (induct p rule: measure_induct_rule [where f=degree]) |
|
220 |
case (less p) |
|
221 |
obtain a q where "p = pCons a q" by (rule pCons_cases) |
|
222 |
have "P q" |
|
223 |
proof (cases "q = 0") |
|
224 |
case True |
|
225 |
then show "P q" by (simp add: zero) |
|
226 |
next |
|
227 |
case False |
|
228 |
then have "degree (pCons a q) = Suc (degree q)" |
|
229 |
by (rule degree_pCons_eq) |
|
230 |
then have "degree q < degree p" |
|
60500 | 231 |
using \<open>p = pCons a q\<close> by simp |
29451 | 232 |
then show "P q" |
233 |
by (rule less.hyps) |
|
234 |
qed |
|
54856 | 235 |
have "P (pCons a q)" |
236 |
proof (cases "a \<noteq> 0 \<or> q \<noteq> 0") |
|
237 |
case True |
|
60500 | 238 |
with \<open>P q\<close> show ?thesis by (auto intro: pCons) |
54856 | 239 |
next |
240 |
case False |
|
241 |
with zero show ?thesis by simp |
|
242 |
qed |
|
29451 | 243 |
then show ?case |
60500 | 244 |
using \<open>p = pCons a q\<close> by simp |
29451 | 245 |
qed |
246 |
||
60570 | 247 |
lemma degree_eq_zeroE: |
248 |
fixes p :: "'a::zero poly" |
|
249 |
assumes "degree p = 0" |
|
250 |
obtains a where "p = pCons a 0" |
|
251 |
proof - |
|
252 |
obtain a q where p: "p = pCons a q" by (cases p) |
|
253 |
with assms have "q = 0" by (cases "q = 0") simp_all |
|
254 |
with p have "p = pCons a 0" by simp |
|
255 |
with that show thesis . |
|
256 |
qed |
|
257 |
||
29451 | 258 |
|
60500 | 259 |
subsection \<open>List-style syntax for polynomials\<close> |
52380 | 260 |
|
261 |
syntax |
|
262 |
"_poly" :: "args \<Rightarrow> 'a poly" ("[:(_):]") |
|
263 |
||
264 |
translations |
|
265 |
"[:x, xs:]" == "CONST pCons x [:xs:]" |
|
266 |
"[:x:]" == "CONST pCons x 0" |
|
267 |
"[:x:]" <= "CONST pCons x (_constrain 0 t)" |
|
268 |
||
269 |
||
60500 | 270 |
subsection \<open>Representation of polynomials by lists of coefficients\<close> |
52380 | 271 |
|
272 |
primrec Poly :: "'a::zero list \<Rightarrow> 'a poly" |
|
273 |
where |
|
54855 | 274 |
[code_post]: "Poly [] = 0" |
275 |
| [code_post]: "Poly (a # as) = pCons a (Poly as)" |
|
52380 | 276 |
|
277 |
lemma Poly_replicate_0 [simp]: |
|
278 |
"Poly (replicate n 0) = 0" |
|
279 |
by (induct n) simp_all |
|
280 |
||
281 |
lemma Poly_eq_0: |
|
282 |
"Poly as = 0 \<longleftrightarrow> (\<exists>n. as = replicate n 0)" |
|
283 |
by (induct as) (auto simp add: Cons_replicate_eq) |
|
62065 | 284 |
|
285 |
lemma degree_Poly: "degree (Poly xs) \<le> length xs" |
|
286 |
by (induction xs) simp_all |
|
287 |
||
52380 | 288 |
definition coeffs :: "'a poly \<Rightarrow> 'a::zero list" |
289 |
where |
|
290 |
"coeffs p = (if p = 0 then [] else map (\<lambda>i. coeff p i) [0 ..< Suc (degree p)])" |
|
291 |
||
292 |
lemma coeffs_eq_Nil [simp]: |
|
293 |
"coeffs p = [] \<longleftrightarrow> p = 0" |
|
294 |
by (simp add: coeffs_def) |
|
295 |
||
296 |
lemma not_0_coeffs_not_Nil: |
|
297 |
"p \<noteq> 0 \<Longrightarrow> coeffs p \<noteq> []" |
|
298 |
by simp |
|
299 |
||
300 |
lemma coeffs_0_eq_Nil [simp]: |
|
301 |
"coeffs 0 = []" |
|
302 |
by simp |
|
29454
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
303 |
|
52380 | 304 |
lemma coeffs_pCons_eq_cCons [simp]: |
305 |
"coeffs (pCons a p) = a ## coeffs p" |
|
306 |
proof - |
|
307 |
{ fix ms :: "nat list" and f :: "nat \<Rightarrow> 'a" and x :: "'a" |
|
308 |
assume "\<forall>m\<in>set ms. m > 0" |
|
55415 | 309 |
then have "map (case_nat x f) ms = map f (map (\<lambda>n. n - 1) ms)" |
58199
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
57862
diff
changeset
|
310 |
by (induct ms) (auto split: nat.split) |
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
57862
diff
changeset
|
311 |
} |
52380 | 312 |
note * = this |
313 |
show ?thesis |
|
60570 | 314 |
by (simp add: coeffs_def * upt_conv_Cons coeff_pCons map_decr_upt del: upt_Suc) |
52380 | 315 |
qed |
316 |
||
62065 | 317 |
lemma length_coeffs: "p \<noteq> 0 \<Longrightarrow> length (coeffs p) = degree p + 1" |
318 |
by (simp add: coeffs_def) |
|
319 |
||
320 |
lemma coeffs_nth: |
|
321 |
assumes "p \<noteq> 0" "n \<le> degree p" |
|
322 |
shows "coeffs p ! n = coeff p n" |
|
323 |
using assms unfolding coeffs_def by (auto simp del: upt_Suc) |
|
324 |
||
52380 | 325 |
lemma not_0_cCons_eq [simp]: |
326 |
"p \<noteq> 0 \<Longrightarrow> a ## coeffs p = a # coeffs p" |
|
327 |
by (simp add: cCons_def) |
|
328 |
||
329 |
lemma Poly_coeffs [simp, code abstype]: |
|
330 |
"Poly (coeffs p) = p" |
|
54856 | 331 |
by (induct p) auto |
52380 | 332 |
|
333 |
lemma coeffs_Poly [simp]: |
|
334 |
"coeffs (Poly as) = strip_while (HOL.eq 0) as" |
|
335 |
proof (induct as) |
|
336 |
case Nil then show ?case by simp |
|
337 |
next |
|
338 |
case (Cons a as) |
|
339 |
have "(\<forall>n. as \<noteq> replicate n 0) \<longleftrightarrow> (\<exists>a\<in>set as. a \<noteq> 0)" |
|
340 |
using replicate_length_same [of as 0] by (auto dest: sym [of _ as]) |
|
341 |
with Cons show ?case by auto |
|
342 |
qed |
|
343 |
||
344 |
lemma last_coeffs_not_0: |
|
345 |
"p \<noteq> 0 \<Longrightarrow> last (coeffs p) \<noteq> 0" |
|
346 |
by (induct p) (auto simp add: cCons_def) |
|
347 |
||
348 |
lemma strip_while_coeffs [simp]: |
|
349 |
"strip_while (HOL.eq 0) (coeffs p) = coeffs p" |
|
350 |
by (cases "p = 0") (auto dest: last_coeffs_not_0 intro: strip_while_not_last) |
|
351 |
||
352 |
lemma coeffs_eq_iff: |
|
353 |
"p = q \<longleftrightarrow> coeffs p = coeffs q" (is "?P \<longleftrightarrow> ?Q") |
|
354 |
proof |
|
355 |
assume ?P then show ?Q by simp |
|
356 |
next |
|
357 |
assume ?Q |
|
358 |
then have "Poly (coeffs p) = Poly (coeffs q)" by simp |
|
359 |
then show ?P by simp |
|
360 |
qed |
|
361 |
||
362 |
lemma coeff_Poly_eq: |
|
363 |
"coeff (Poly xs) n = nth_default 0 xs n" |
|
364 |
apply (induct xs arbitrary: n) apply simp_all |
|
55642
63beb38e9258
adapted to renaming of datatype 'cases' and 'recs' to 'case' and 'rec'
blanchet
parents:
55417
diff
changeset
|
365 |
by (metis nat.case not0_implies_Suc nth_default_Cons_0 nth_default_Cons_Suc pCons.rep_eq) |
29454
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
366 |
|
52380 | 367 |
lemma nth_default_coeffs_eq: |
368 |
"nth_default 0 (coeffs p) = coeff p" |
|
369 |
by (simp add: fun_eq_iff coeff_Poly_eq [symmetric]) |
|
370 |
||
371 |
lemma [code]: |
|
372 |
"coeff p = nth_default 0 (coeffs p)" |
|
373 |
by (simp add: nth_default_coeffs_eq) |
|
374 |
||
375 |
lemma coeffs_eqI: |
|
376 |
assumes coeff: "\<And>n. coeff p n = nth_default 0 xs n" |
|
377 |
assumes zero: "xs \<noteq> [] \<Longrightarrow> last xs \<noteq> 0" |
|
378 |
shows "coeffs p = xs" |
|
379 |
proof - |
|
380 |
from coeff have "p = Poly xs" by (simp add: poly_eq_iff coeff_Poly_eq) |
|
381 |
with zero show ?thesis by simp (cases xs, simp_all) |
|
382 |
qed |
|
383 |
||
384 |
lemma degree_eq_length_coeffs [code]: |
|
385 |
"degree p = length (coeffs p) - 1" |
|
386 |
by (simp add: coeffs_def) |
|
387 |
||
388 |
lemma length_coeffs_degree: |
|
389 |
"p \<noteq> 0 \<Longrightarrow> length (coeffs p) = Suc (degree p)" |
|
390 |
by (induct p) (auto simp add: cCons_def) |
|
391 |
||
392 |
lemma [code abstract]: |
|
393 |
"coeffs 0 = []" |
|
394 |
by (fact coeffs_0_eq_Nil) |
|
395 |
||
396 |
lemma [code abstract]: |
|
397 |
"coeffs (pCons a p) = a ## coeffs p" |
|
398 |
by (fact coeffs_pCons_eq_cCons) |
|
399 |
||
400 |
instantiation poly :: ("{zero, equal}") equal |
|
401 |
begin |
|
402 |
||
403 |
definition |
|
404 |
[code]: "HOL.equal (p::'a poly) q \<longleftrightarrow> HOL.equal (coeffs p) (coeffs q)" |
|
405 |
||
60679 | 406 |
instance |
407 |
by standard (simp add: equal equal_poly_def coeffs_eq_iff) |
|
52380 | 408 |
|
409 |
end |
|
410 |
||
60679 | 411 |
lemma [code nbe]: "HOL.equal (p :: _ poly) p \<longleftrightarrow> True" |
52380 | 412 |
by (fact equal_refl) |
29454
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
413 |
|
52380 | 414 |
definition is_zero :: "'a::zero poly \<Rightarrow> bool" |
415 |
where |
|
416 |
[code]: "is_zero p \<longleftrightarrow> List.null (coeffs p)" |
|
417 |
||
418 |
lemma is_zero_null [code_abbrev]: |
|
419 |
"is_zero p \<longleftrightarrow> p = 0" |
|
420 |
by (simp add: is_zero_def null_def) |
|
421 |
||
422 |
||
60500 | 423 |
subsection \<open>Fold combinator for polynomials\<close> |
52380 | 424 |
|
425 |
definition fold_coeffs :: "('a::zero \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a poly \<Rightarrow> 'b \<Rightarrow> 'b" |
|
426 |
where |
|
427 |
"fold_coeffs f p = foldr f (coeffs p)" |
|
428 |
||
429 |
lemma fold_coeffs_0_eq [simp]: |
|
430 |
"fold_coeffs f 0 = id" |
|
431 |
by (simp add: fold_coeffs_def) |
|
432 |
||
433 |
lemma fold_coeffs_pCons_eq [simp]: |
|
434 |
"f 0 = id \<Longrightarrow> fold_coeffs f (pCons a p) = f a \<circ> fold_coeffs f p" |
|
435 |
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
|
436 |
|
52380 | 437 |
lemma fold_coeffs_pCons_0_0_eq [simp]: |
438 |
"fold_coeffs f (pCons 0 0) = id" |
|
439 |
by (simp add: fold_coeffs_def) |
|
440 |
||
441 |
lemma fold_coeffs_pCons_coeff_not_0_eq [simp]: |
|
442 |
"a \<noteq> 0 \<Longrightarrow> fold_coeffs f (pCons a p) = f a \<circ> fold_coeffs f p" |
|
443 |
by (simp add: fold_coeffs_def) |
|
444 |
||
445 |
lemma fold_coeffs_pCons_not_0_0_eq [simp]: |
|
446 |
"p \<noteq> 0 \<Longrightarrow> fold_coeffs f (pCons a p) = f a \<circ> fold_coeffs f p" |
|
447 |
by (simp add: fold_coeffs_def) |
|
448 |
||
449 |
||
60500 | 450 |
subsection \<open>Canonical morphism on polynomials -- evaluation\<close> |
52380 | 451 |
|
452 |
definition poly :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a" |
|
453 |
where |
|
61585 | 454 |
"poly p = fold_coeffs (\<lambda>a f x. a + x * f x) p (\<lambda>x. 0)" \<comment> \<open>The Horner Schema\<close> |
52380 | 455 |
|
456 |
lemma poly_0 [simp]: |
|
457 |
"poly 0 x = 0" |
|
458 |
by (simp add: poly_def) |
|
62128
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
459 |
|
52380 | 460 |
lemma poly_pCons [simp]: |
461 |
"poly (pCons a p) x = a + x * poly p x" |
|
462 |
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
|
463 |
|
62065 | 464 |
lemma poly_altdef: |
465 |
"poly p (x :: 'a :: {comm_semiring_0, semiring_1}) = (\<Sum>i\<le>degree p. coeff p i * x ^ i)" |
|
466 |
proof (induction p rule: pCons_induct) |
|
467 |
case (pCons a p) |
|
468 |
show ?case |
|
469 |
proof (cases "p = 0") |
|
470 |
case False |
|
471 |
let ?p' = "pCons a p" |
|
472 |
note poly_pCons[of a p x] |
|
473 |
also note pCons.IH |
|
474 |
also have "a + x * (\<Sum>i\<le>degree p. coeff p i * x ^ i) = |
|
475 |
coeff ?p' 0 * x^0 + (\<Sum>i\<le>degree p. coeff ?p' (Suc i) * x^Suc i)" |
|
476 |
by (simp add: field_simps setsum_right_distrib coeff_pCons) |
|
477 |
also note setsum_atMost_Suc_shift[symmetric] |
|
62072 | 478 |
also note degree_pCons_eq[OF \<open>p \<noteq> 0\<close>, of a, symmetric] |
62065 | 479 |
finally show ?thesis . |
480 |
qed simp |
|
481 |
qed simp |
|
482 |
||
62128
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
483 |
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
|
484 |
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
|
485 |
|
29454
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
486 |
|
60500 | 487 |
subsection \<open>Monomials\<close> |
29451 | 488 |
|
52380 | 489 |
lift_definition monom :: "'a \<Rightarrow> nat \<Rightarrow> 'a::zero poly" |
490 |
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
|
491 |
by (simp add: MOST_iff_cofinite) |
52380 | 492 |
|
493 |
lemma coeff_monom [simp]: |
|
494 |
"coeff (monom a m) n = (if m = n then a else 0)" |
|
495 |
by transfer rule |
|
29451 | 496 |
|
52380 | 497 |
lemma monom_0: |
498 |
"monom a 0 = pCons a 0" |
|
499 |
by (rule poly_eqI) (simp add: coeff_pCons split: nat.split) |
|
29451 | 500 |
|
52380 | 501 |
lemma monom_Suc: |
502 |
"monom a (Suc n) = pCons 0 (monom a n)" |
|
503 |
by (rule poly_eqI) (simp add: coeff_pCons split: nat.split) |
|
29451 | 504 |
|
505 |
lemma monom_eq_0 [simp]: "monom 0 n = 0" |
|
52380 | 506 |
by (rule poly_eqI) simp |
29451 | 507 |
|
508 |
lemma monom_eq_0_iff [simp]: "monom a n = 0 \<longleftrightarrow> a = 0" |
|
52380 | 509 |
by (simp add: poly_eq_iff) |
29451 | 510 |
|
511 |
lemma monom_eq_iff [simp]: "monom a n = monom b n \<longleftrightarrow> a = b" |
|
52380 | 512 |
by (simp add: poly_eq_iff) |
29451 | 513 |
|
514 |
lemma degree_monom_le: "degree (monom a n) \<le> n" |
|
515 |
by (rule degree_le, simp) |
|
516 |
||
517 |
lemma degree_monom_eq: "a \<noteq> 0 \<Longrightarrow> degree (monom a n) = n" |
|
518 |
apply (rule order_antisym [OF degree_monom_le]) |
|
519 |
apply (rule le_degree, simp) |
|
520 |
done |
|
521 |
||
52380 | 522 |
lemma coeffs_monom [code abstract]: |
523 |
"coeffs (monom a n) = (if a = 0 then [] else replicate n 0 @ [a])" |
|
524 |
by (induct n) (simp_all add: monom_0 monom_Suc) |
|
525 |
||
526 |
lemma fold_coeffs_monom [simp]: |
|
527 |
"a \<noteq> 0 \<Longrightarrow> fold_coeffs f (monom a n) = f 0 ^^ n \<circ> f a" |
|
528 |
by (simp add: fold_coeffs_def coeffs_monom fun_eq_iff) |
|
529 |
||
530 |
lemma poly_monom: |
|
531 |
fixes a x :: "'a::{comm_semiring_1}" |
|
532 |
shows "poly (monom a n) x = a * x ^ n" |
|
533 |
by (cases "a = 0", simp_all) |
|
534 |
(induct n, simp_all add: mult.left_commute poly_def) |
|
535 |
||
62065 | 536 |
|
60500 | 537 |
subsection \<open>Addition and subtraction\<close> |
29451 | 538 |
|
539 |
instantiation poly :: (comm_monoid_add) comm_monoid_add |
|
540 |
begin |
|
541 |
||
52380 | 542 |
lift_definition plus_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" |
543 |
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
|
544 |
proof - |
60679 | 545 |
fix q p :: "'a poly" |
546 |
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
|
547 |
using MOST_coeff_eq_0[of p] MOST_coeff_eq_0[of q] by eventually_elim simp |
52380 | 548 |
qed |
29451 | 549 |
|
60679 | 550 |
lemma coeff_add [simp]: "coeff (p + q) n = coeff p n + coeff q n" |
52380 | 551 |
by (simp add: plus_poly.rep_eq) |
29451 | 552 |
|
60679 | 553 |
instance |
554 |
proof |
|
29451 | 555 |
fix p q r :: "'a poly" |
556 |
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
|
557 |
by (simp add: poly_eq_iff add.assoc) |
29451 | 558 |
show "p + q = q + p" |
57512
cc97b347b301
reduced name variants for assoc and commute on plus and mult
haftmann
parents:
57482
diff
changeset
|
559 |
by (simp add: poly_eq_iff add.commute) |
29451 | 560 |
show "0 + p = p" |
52380 | 561 |
by (simp add: poly_eq_iff) |
29451 | 562 |
qed |
563 |
||
564 |
end |
|
565 |
||
59815
cce82e360c2f
explicit commutative additive inverse operation;
haftmann
parents:
59557
diff
changeset
|
566 |
instantiation poly :: (cancel_comm_monoid_add) cancel_comm_monoid_add |
cce82e360c2f
explicit commutative additive inverse operation;
haftmann
parents:
59557
diff
changeset
|
567 |
begin |
cce82e360c2f
explicit commutative additive inverse operation;
haftmann
parents:
59557
diff
changeset
|
568 |
|
cce82e360c2f
explicit commutative additive inverse operation;
haftmann
parents:
59557
diff
changeset
|
569 |
lift_definition minus_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" |
cce82e360c2f
explicit commutative additive inverse operation;
haftmann
parents:
59557
diff
changeset
|
570 |
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
|
571 |
proof - |
60679 | 572 |
fix q p :: "'a poly" |
573 |
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
|
574 |
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
|
575 |
qed |
cce82e360c2f
explicit commutative additive inverse operation;
haftmann
parents:
59557
diff
changeset
|
576 |
|
60679 | 577 |
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
|
578 |
by (simp add: minus_poly.rep_eq) |
cce82e360c2f
explicit commutative additive inverse operation;
haftmann
parents:
59557
diff
changeset
|
579 |
|
60679 | 580 |
instance |
581 |
proof |
|
29540 | 582 |
fix p q r :: "'a poly" |
59815
cce82e360c2f
explicit commutative additive inverse operation;
haftmann
parents:
59557
diff
changeset
|
583 |
show "p + q - p = q" |
52380 | 584 |
by (simp add: poly_eq_iff) |
59815
cce82e360c2f
explicit commutative additive inverse operation;
haftmann
parents:
59557
diff
changeset
|
585 |
show "p - q - r = p - (q + r)" |
cce82e360c2f
explicit commutative additive inverse operation;
haftmann
parents:
59557
diff
changeset
|
586 |
by (simp add: poly_eq_iff diff_diff_eq) |
29540 | 587 |
qed |
588 |
||
59815
cce82e360c2f
explicit commutative additive inverse operation;
haftmann
parents:
59557
diff
changeset
|
589 |
end |
cce82e360c2f
explicit commutative additive inverse operation;
haftmann
parents:
59557
diff
changeset
|
590 |
|
29451 | 591 |
instantiation poly :: (ab_group_add) ab_group_add |
592 |
begin |
|
593 |
||
52380 | 594 |
lift_definition uminus_poly :: "'a poly \<Rightarrow> 'a poly" |
595 |
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
|
596 |
proof - |
60679 | 597 |
fix p :: "'a poly" |
598 |
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
|
599 |
using MOST_coeff_eq_0 by simp |
52380 | 600 |
qed |
29451 | 601 |
|
602 |
lemma coeff_minus [simp]: "coeff (- p) n = - coeff p n" |
|
52380 | 603 |
by (simp add: uminus_poly.rep_eq) |
29451 | 604 |
|
60679 | 605 |
instance |
606 |
proof |
|
29451 | 607 |
fix p q :: "'a poly" |
608 |
show "- p + p = 0" |
|
52380 | 609 |
by (simp add: poly_eq_iff) |
29451 | 610 |
show "p - q = p + - q" |
54230
b1d955791529
more simplification rules on unary and binary minus
haftmann
parents:
52380
diff
changeset
|
611 |
by (simp add: poly_eq_iff) |
29451 | 612 |
qed |
613 |
||
614 |
end |
|
615 |
||
616 |
lemma add_pCons [simp]: |
|
617 |
"pCons a p + pCons b q = pCons (a + b) (p + q)" |
|
52380 | 618 |
by (rule poly_eqI, simp add: coeff_pCons split: nat.split) |
29451 | 619 |
|
620 |
lemma minus_pCons [simp]: |
|
621 |
"- pCons a p = pCons (- a) (- p)" |
|
52380 | 622 |
by (rule poly_eqI, simp add: coeff_pCons split: nat.split) |
29451 | 623 |
|
624 |
lemma diff_pCons [simp]: |
|
625 |
"pCons a p - pCons b q = pCons (a - b) (p - q)" |
|
52380 | 626 |
by (rule poly_eqI, simp add: coeff_pCons split: nat.split) |
29451 | 627 |
|
29539 | 628 |
lemma degree_add_le_max: "degree (p + q) \<le> max (degree p) (degree q)" |
29451 | 629 |
by (rule degree_le, auto simp add: coeff_eq_0) |
630 |
||
29539 | 631 |
lemma degree_add_le: |
632 |
"\<lbrakk>degree p \<le> n; degree q \<le> n\<rbrakk> \<Longrightarrow> degree (p + q) \<le> n" |
|
633 |
by (auto intro: order_trans degree_add_le_max) |
|
634 |
||
29453 | 635 |
lemma degree_add_less: |
636 |
"\<lbrakk>degree p < n; degree q < n\<rbrakk> \<Longrightarrow> degree (p + q) < n" |
|
29539 | 637 |
by (auto intro: le_less_trans degree_add_le_max) |
29453 | 638 |
|
29451 | 639 |
lemma degree_add_eq_right: |
640 |
"degree p < degree q \<Longrightarrow> degree (p + q) = degree q" |
|
641 |
apply (cases "q = 0", simp) |
|
642 |
apply (rule order_antisym) |
|
29539 | 643 |
apply (simp add: degree_add_le) |
29451 | 644 |
apply (rule le_degree) |
645 |
apply (simp add: coeff_eq_0) |
|
646 |
done |
|
647 |
||
648 |
lemma degree_add_eq_left: |
|
649 |
"degree q < degree p \<Longrightarrow> degree (p + q) = degree p" |
|
650 |
using degree_add_eq_right [of q p] |
|
57512
cc97b347b301
reduced name variants for assoc and commute on plus and mult
haftmann
parents:
57482
diff
changeset
|
651 |
by (simp add: add.commute) |
29451 | 652 |
|
59815
cce82e360c2f
explicit commutative additive inverse operation;
haftmann
parents:
59557
diff
changeset
|
653 |
lemma degree_minus [simp]: |
cce82e360c2f
explicit commutative additive inverse operation;
haftmann
parents:
59557
diff
changeset
|
654 |
"degree (- p) = degree p" |
29451 | 655 |
unfolding degree_def by simp |
656 |
||
59815
cce82e360c2f
explicit commutative additive inverse operation;
haftmann
parents:
59557
diff
changeset
|
657 |
lemma degree_diff_le_max: |
cce82e360c2f
explicit commutative additive inverse operation;
haftmann
parents:
59557
diff
changeset
|
658 |
fixes p q :: "'a :: ab_group_add poly" |
cce82e360c2f
explicit commutative additive inverse operation;
haftmann
parents:
59557
diff
changeset
|
659 |
shows "degree (p - q) \<le> max (degree p) (degree q)" |
29451 | 660 |
using degree_add_le [where p=p and q="-q"] |
54230
b1d955791529
more simplification rules on unary and binary minus
haftmann
parents:
52380
diff
changeset
|
661 |
by simp |
29451 | 662 |
|
29539 | 663 |
lemma degree_diff_le: |
59815
cce82e360c2f
explicit commutative additive inverse operation;
haftmann
parents:
59557
diff
changeset
|
664 |
fixes p q :: "'a :: ab_group_add poly" |
cce82e360c2f
explicit commutative additive inverse operation;
haftmann
parents:
59557
diff
changeset
|
665 |
assumes "degree p \<le> n" and "degree q \<le> n" |
cce82e360c2f
explicit commutative additive inverse operation;
haftmann
parents:
59557
diff
changeset
|
666 |
shows "degree (p - q) \<le> n" |
cce82e360c2f
explicit commutative additive inverse operation;
haftmann
parents:
59557
diff
changeset
|
667 |
using assms degree_add_le [of p n "- q"] by simp |
29539 | 668 |
|
29453 | 669 |
lemma degree_diff_less: |
59815
cce82e360c2f
explicit commutative additive inverse operation;
haftmann
parents:
59557
diff
changeset
|
670 |
fixes p q :: "'a :: ab_group_add poly" |
cce82e360c2f
explicit commutative additive inverse operation;
haftmann
parents:
59557
diff
changeset
|
671 |
assumes "degree p < n" and "degree q < n" |
cce82e360c2f
explicit commutative additive inverse operation;
haftmann
parents:
59557
diff
changeset
|
672 |
shows "degree (p - q) < n" |
cce82e360c2f
explicit commutative additive inverse operation;
haftmann
parents:
59557
diff
changeset
|
673 |
using assms degree_add_less [of p n "- q"] by simp |
29453 | 674 |
|
29451 | 675 |
lemma add_monom: "monom a n + monom b n = monom (a + b) n" |
52380 | 676 |
by (rule poly_eqI) simp |
29451 | 677 |
|
678 |
lemma diff_monom: "monom a n - monom b n = monom (a - b) n" |
|
52380 | 679 |
by (rule poly_eqI) simp |
29451 | 680 |
|
681 |
lemma minus_monom: "- monom a n = monom (-a) n" |
|
52380 | 682 |
by (rule poly_eqI) simp |
29451 | 683 |
|
684 |
lemma coeff_setsum: "coeff (\<Sum>x\<in>A. p x) i = (\<Sum>x\<in>A. coeff (p x) i)" |
|
685 |
by (cases "finite A", induct set: finite, simp_all) |
|
686 |
||
687 |
lemma monom_setsum: "monom (\<Sum>x\<in>A. a x) n = (\<Sum>x\<in>A. monom (a x) n)" |
|
52380 | 688 |
by (rule poly_eqI) (simp add: coeff_setsum) |
689 |
||
690 |
fun plus_coeffs :: "'a::comm_monoid_add list \<Rightarrow> 'a list \<Rightarrow> 'a list" |
|
691 |
where |
|
692 |
"plus_coeffs xs [] = xs" |
|
693 |
| "plus_coeffs [] ys = ys" |
|
694 |
| "plus_coeffs (x # xs) (y # ys) = (x + y) ## plus_coeffs xs ys" |
|
695 |
||
696 |
lemma coeffs_plus_eq_plus_coeffs [code abstract]: |
|
697 |
"coeffs (p + q) = plus_coeffs (coeffs p) (coeffs q)" |
|
698 |
proof - |
|
699 |
{ fix xs ys :: "'a list" and n |
|
700 |
have "nth_default 0 (plus_coeffs xs ys) n = nth_default 0 xs n + nth_default 0 ys n" |
|
701 |
proof (induct xs ys arbitrary: n rule: plus_coeffs.induct) |
|
60679 | 702 |
case (3 x xs y ys n) |
703 |
then show ?case by (cases n) (auto simp add: cCons_def) |
|
52380 | 704 |
qed simp_all } |
705 |
note * = this |
|
706 |
{ fix xs ys :: "'a list" |
|
707 |
assume "xs \<noteq> [] \<Longrightarrow> last xs \<noteq> 0" and "ys \<noteq> [] \<Longrightarrow> last ys \<noteq> 0" |
|
708 |
moreover assume "plus_coeffs xs ys \<noteq> []" |
|
709 |
ultimately have "last (plus_coeffs xs ys) \<noteq> 0" |
|
710 |
proof (induct xs ys rule: plus_coeffs.induct) |
|
711 |
case (3 x xs y ys) then show ?case by (auto simp add: cCons_def) metis |
|
712 |
qed simp_all } |
|
713 |
note ** = this |
|
714 |
show ?thesis |
|
715 |
apply (rule coeffs_eqI) |
|
716 |
apply (simp add: * nth_default_coeffs_eq) |
|
717 |
apply (rule **) |
|
718 |
apply (auto dest: last_coeffs_not_0) |
|
719 |
done |
|
720 |
qed |
|
721 |
||
722 |
lemma coeffs_uminus [code abstract]: |
|
723 |
"coeffs (- p) = map (\<lambda>a. - a) (coeffs p)" |
|
724 |
by (rule coeffs_eqI) |
|
725 |
(simp_all add: not_0_coeffs_not_Nil last_map last_coeffs_not_0 nth_default_map_eq nth_default_coeffs_eq) |
|
726 |
||
727 |
lemma [code]: |
|
728 |
fixes p q :: "'a::ab_group_add poly" |
|
729 |
shows "p - q = p + - q" |
|
59557 | 730 |
by (fact diff_conv_add_uminus) |
52380 | 731 |
|
732 |
lemma poly_add [simp]: "poly (p + q) x = poly p x + poly q x" |
|
733 |
apply (induct p arbitrary: q, simp) |
|
734 |
apply (case_tac q, simp, simp add: algebra_simps) |
|
735 |
done |
|
736 |
||
737 |
lemma poly_minus [simp]: |
|
738 |
fixes x :: "'a::comm_ring" |
|
739 |
shows "poly (- p) x = - poly p x" |
|
740 |
by (induct p) simp_all |
|
741 |
||
742 |
lemma poly_diff [simp]: |
|
743 |
fixes x :: "'a::comm_ring" |
|
744 |
shows "poly (p - q) x = poly p x - poly q x" |
|
54230
b1d955791529
more simplification rules on unary and binary minus
haftmann
parents:
52380
diff
changeset
|
745 |
using poly_add [of p "- q" x] by simp |
52380 | 746 |
|
747 |
lemma poly_setsum: "poly (\<Sum>k\<in>A. p k) x = (\<Sum>k\<in>A. poly (p k) x)" |
|
748 |
by (induct A rule: infinite_finite_induct) simp_all |
|
29451 | 749 |
|
62128
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
750 |
lemma degree_setsum_le: "finite S \<Longrightarrow> (\<And> p . p \<in> S \<Longrightarrow> degree (f p) \<le> n) |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
751 |
\<Longrightarrow> degree (setsum f S) \<le> n" |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
752 |
proof (induct S rule: finite_induct) |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
753 |
case (insert p S) |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
754 |
hence "degree (setsum f S) \<le> n" "degree (f p) \<le> n" by auto |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
755 |
thus ?case unfolding setsum.insert[OF insert(1-2)] by (metis degree_add_le) |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
756 |
qed simp |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
757 |
|
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
758 |
lemma poly_as_sum_of_monoms': |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
759 |
assumes n: "degree p \<le> n" |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
760 |
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
|
761 |
proof - |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
762 |
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
|
763 |
by auto |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
764 |
show ?thesis |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
765 |
using n by (simp add: poly_eq_iff coeff_setsum coeff_eq_0 setsum.If_cases eq |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
766 |
if_distrib[where f="\<lambda>x. x * a" for a]) |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
767 |
qed |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
768 |
|
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
769 |
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
|
770 |
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
|
771 |
|
62065 | 772 |
lemma Poly_snoc: "Poly (xs @ [x]) = Poly xs + monom x (length xs)" |
773 |
by (induction xs) (simp_all add: monom_0 monom_Suc) |
|
774 |
||
29451 | 775 |
|
60500 | 776 |
subsection \<open>Multiplication by a constant, polynomial multiplication and the unit polynomial\<close> |
29451 | 777 |
|
52380 | 778 |
lift_definition smult :: "'a::comm_semiring_0 \<Rightarrow> 'a poly \<Rightarrow> 'a poly" |
779 |
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
|
780 |
proof - |
1fa1023b13b9
move MOST and INFM in Infinite_Set to Filter; change them to abbreviations over the cofinite filter
hoelzl
parents:
59983
diff
changeset
|
781 |
fix a :: 'a and p :: "'a poly" show "\<forall>\<^sub>\<infinity> i. a * coeff p i = 0" |
1fa1023b13b9
move MOST and INFM in Infinite_Set to Filter; change them to abbreviations over the cofinite filter
hoelzl
parents:
59983
diff
changeset
|
782 |
using MOST_coeff_eq_0[of p] by eventually_elim simp |
52380 | 783 |
qed |
29451 | 784 |
|
52380 | 785 |
lemma coeff_smult [simp]: |
786 |
"coeff (smult a p) n = a * coeff p n" |
|
787 |
by (simp add: smult.rep_eq) |
|
29451 | 788 |
|
789 |
lemma degree_smult_le: "degree (smult a p) \<le> degree p" |
|
790 |
by (rule degree_le, simp add: coeff_eq_0) |
|
791 |
||
29472 | 792 |
lemma smult_smult [simp]: "smult a (smult b p) = smult (a * b) p" |
57512
cc97b347b301
reduced name variants for assoc and commute on plus and mult
haftmann
parents:
57482
diff
changeset
|
793 |
by (rule poly_eqI, simp add: mult.assoc) |
29451 | 794 |
|
795 |
lemma smult_0_right [simp]: "smult a 0 = 0" |
|
52380 | 796 |
by (rule poly_eqI, simp) |
29451 | 797 |
|
798 |
lemma smult_0_left [simp]: "smult 0 p = 0" |
|
52380 | 799 |
by (rule poly_eqI, simp) |
29451 | 800 |
|
801 |
lemma smult_1_left [simp]: "smult (1::'a::comm_semiring_1) p = p" |
|
52380 | 802 |
by (rule poly_eqI, simp) |
29451 | 803 |
|
804 |
lemma smult_add_right: |
|
805 |
"smult a (p + q) = smult a p + smult a q" |
|
52380 | 806 |
by (rule poly_eqI, simp add: algebra_simps) |
29451 | 807 |
|
808 |
lemma smult_add_left: |
|
809 |
"smult (a + b) p = smult a p + smult b p" |
|
52380 | 810 |
by (rule poly_eqI, simp add: algebra_simps) |
29451 | 811 |
|
29457 | 812 |
lemma smult_minus_right [simp]: |
29451 | 813 |
"smult (a::'a::comm_ring) (- p) = - smult a p" |
52380 | 814 |
by (rule poly_eqI, simp) |
29451 | 815 |
|
29457 | 816 |
lemma smult_minus_left [simp]: |
29451 | 817 |
"smult (- a::'a::comm_ring) p = - smult a p" |
52380 | 818 |
by (rule poly_eqI, simp) |
29451 | 819 |
|
820 |
lemma smult_diff_right: |
|
821 |
"smult (a::'a::comm_ring) (p - q) = smult a p - smult a q" |
|
52380 | 822 |
by (rule poly_eqI, simp add: algebra_simps) |
29451 | 823 |
|
824 |
lemma smult_diff_left: |
|
825 |
"smult (a - b::'a::comm_ring) p = smult a p - smult b p" |
|
52380 | 826 |
by (rule poly_eqI, simp add: algebra_simps) |
29451 | 827 |
|
29472 | 828 |
lemmas smult_distribs = |
829 |
smult_add_left smult_add_right |
|
830 |
smult_diff_left smult_diff_right |
|
831 |
||
29451 | 832 |
lemma smult_pCons [simp]: |
833 |
"smult a (pCons b p) = pCons (a * b) (smult a p)" |
|
52380 | 834 |
by (rule poly_eqI, simp add: coeff_pCons split: nat.split) |
29451 | 835 |
|
836 |
lemma smult_monom: "smult a (monom b n) = monom (a * b) n" |
|
837 |
by (induct n, simp add: monom_0, simp add: monom_Suc) |
|
838 |
||
29659 | 839 |
lemma degree_smult_eq [simp]: |
840 |
fixes a :: "'a::idom" |
|
841 |
shows "degree (smult a p) = (if a = 0 then 0 else degree p)" |
|
842 |
by (cases "a = 0", simp, simp add: degree_def) |
|
843 |
||
844 |
lemma smult_eq_0_iff [simp]: |
|
845 |
fixes a :: "'a::idom" |
|
846 |
shows "smult a p = 0 \<longleftrightarrow> a = 0 \<or> p = 0" |
|
52380 | 847 |
by (simp add: poly_eq_iff) |
29451 | 848 |
|
52380 | 849 |
lemma coeffs_smult [code abstract]: |
850 |
fixes p :: "'a::idom poly" |
|
851 |
shows "coeffs (smult a p) = (if a = 0 then [] else map (Groups.times a) (coeffs p))" |
|
852 |
by (rule coeffs_eqI) |
|
853 |
(auto simp add: not_0_coeffs_not_Nil last_map last_coeffs_not_0 nth_default_map_eq nth_default_coeffs_eq) |
|
29451 | 854 |
|
855 |
instantiation poly :: (comm_semiring_0) comm_semiring_0 |
|
856 |
begin |
|
857 |
||
858 |
definition |
|
52380 | 859 |
"p * q = fold_coeffs (\<lambda>a p. smult a q + pCons 0 p) p 0" |
29474 | 860 |
|
861 |
lemma mult_poly_0_left: "(0::'a poly) * q = 0" |
|
52380 | 862 |
by (simp add: times_poly_def) |
29474 | 863 |
|
864 |
lemma mult_pCons_left [simp]: |
|
865 |
"pCons a p * q = smult a q + pCons 0 (p * q)" |
|
52380 | 866 |
by (cases "p = 0 \<and> a = 0") (auto simp add: times_poly_def) |
29474 | 867 |
|
868 |
lemma mult_poly_0_right: "p * (0::'a poly) = 0" |
|
52380 | 869 |
by (induct p) (simp add: mult_poly_0_left, simp) |
29451 | 870 |
|
29474 | 871 |
lemma mult_pCons_right [simp]: |
872 |
"p * pCons a q = smult a p + pCons 0 (p * q)" |
|
52380 | 873 |
by (induct p) (simp add: mult_poly_0_left, simp add: algebra_simps) |
29474 | 874 |
|
875 |
lemmas mult_poly_0 = mult_poly_0_left mult_poly_0_right |
|
876 |
||
52380 | 877 |
lemma mult_smult_left [simp]: |
878 |
"smult a p * q = smult a (p * q)" |
|
879 |
by (induct p) (simp add: mult_poly_0, simp add: smult_add_right) |
|
29474 | 880 |
|
52380 | 881 |
lemma mult_smult_right [simp]: |
882 |
"p * smult a q = smult a (p * q)" |
|
883 |
by (induct q) (simp add: mult_poly_0, simp add: smult_add_right) |
|
29474 | 884 |
|
885 |
lemma mult_poly_add_left: |
|
886 |
fixes p q r :: "'a poly" |
|
887 |
shows "(p + q) * r = p * r + q * r" |
|
52380 | 888 |
by (induct r) (simp add: mult_poly_0, simp add: smult_distribs algebra_simps) |
29451 | 889 |
|
60679 | 890 |
instance |
891 |
proof |
|
29451 | 892 |
fix p q r :: "'a poly" |
893 |
show 0: "0 * p = 0" |
|
29474 | 894 |
by (rule mult_poly_0_left) |
29451 | 895 |
show "p * 0 = 0" |
29474 | 896 |
by (rule mult_poly_0_right) |
29451 | 897 |
show "(p + q) * r = p * r + q * r" |
29474 | 898 |
by (rule mult_poly_add_left) |
29451 | 899 |
show "(p * q) * r = p * (q * r)" |
29474 | 900 |
by (induct p, simp add: mult_poly_0, simp add: mult_poly_add_left) |
29451 | 901 |
show "p * q = q * p" |
29474 | 902 |
by (induct p, simp add: mult_poly_0, simp) |
29451 | 903 |
qed |
904 |
||
905 |
end |
|
906 |
||
29540 | 907 |
instance poly :: (comm_semiring_0_cancel) comm_semiring_0_cancel .. |
908 |
||
29474 | 909 |
lemma coeff_mult: |
910 |
"coeff (p * q) n = (\<Sum>i\<le>n. coeff p i * coeff q (n-i))" |
|
911 |
proof (induct p arbitrary: n) |
|
912 |
case 0 show ?case by simp |
|
913 |
next |
|
914 |
case (pCons a p n) thus ?case |
|
915 |
by (cases n, simp, simp add: setsum_atMost_Suc_shift |
|
916 |
del: setsum_atMost_Suc) |
|
917 |
qed |
|
29451 | 918 |
|
29474 | 919 |
lemma degree_mult_le: "degree (p * q) \<le> degree p + degree q" |
920 |
apply (rule degree_le) |
|
921 |
apply (induct p) |
|
922 |
apply simp |
|
923 |
apply (simp add: coeff_eq_0 coeff_pCons split: nat.split) |
|
29451 | 924 |
done |
925 |
||
926 |
lemma mult_monom: "monom a m * monom b n = monom (a * b) (m + n)" |
|
60679 | 927 |
by (induct m) (simp add: monom_0 smult_monom, simp add: monom_Suc) |
29451 | 928 |
|
929 |
instantiation poly :: (comm_semiring_1) comm_semiring_1 |
|
930 |
begin |
|
931 |
||
60679 | 932 |
definition one_poly_def: "1 = pCons 1 0" |
29451 | 933 |
|
60679 | 934 |
instance |
935 |
proof |
|
936 |
show "1 * p = p" for p :: "'a poly" |
|
52380 | 937 |
unfolding one_poly_def by simp |
29451 | 938 |
show "0 \<noteq> (1::'a poly)" |
939 |
unfolding one_poly_def by simp |
|
940 |
qed |
|
941 |
||
942 |
end |
|
943 |
||
52380 | 944 |
instance poly :: (comm_ring) comm_ring .. |
945 |
||
946 |
instance poly :: (comm_ring_1) comm_ring_1 .. |
|
947 |
||
29451 | 948 |
lemma coeff_1 [simp]: "coeff 1 n = (if n = 0 then 1 else 0)" |
949 |
unfolding one_poly_def |
|
950 |
by (simp add: coeff_pCons split: nat.split) |
|
951 |
||
60570 | 952 |
lemma monom_eq_1 [simp]: |
953 |
"monom 1 0 = 1" |
|
954 |
by (simp add: monom_0 one_poly_def) |
|
955 |
||
29451 | 956 |
lemma degree_1 [simp]: "degree 1 = 0" |
957 |
unfolding one_poly_def |
|
958 |
by (rule degree_pCons_0) |
|
959 |
||
52380 | 960 |
lemma coeffs_1_eq [simp, code abstract]: |
961 |
"coeffs 1 = [1]" |
|
962 |
by (simp add: one_poly_def) |
|
963 |
||
964 |
lemma degree_power_le: |
|
965 |
"degree (p ^ n) \<le> degree p * n" |
|
966 |
by (induct n) (auto intro: order_trans degree_mult_le) |
|
967 |
||
968 |
lemma poly_smult [simp]: |
|
969 |
"poly (smult a p) x = a * poly p x" |
|
970 |
by (induct p, simp, simp add: algebra_simps) |
|
971 |
||
972 |
lemma poly_mult [simp]: |
|
973 |
"poly (p * q) x = poly p x * poly q x" |
|
974 |
by (induct p, simp_all, simp add: algebra_simps) |
|
975 |
||
976 |
lemma poly_1 [simp]: |
|
977 |
"poly 1 x = 1" |
|
978 |
by (simp add: one_poly_def) |
|
979 |
||
980 |
lemma poly_power [simp]: |
|
981 |
fixes p :: "'a::{comm_semiring_1} poly" |
|
982 |
shows "poly (p ^ n) x = poly p x ^ n" |
|
983 |
by (induct n) simp_all |
|
984 |
||
62128
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
985 |
lemma poly_setprod: "poly (\<Prod>k\<in>A. p k) x = (\<Prod>k\<in>A. poly (p k) x)" |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
986 |
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
|
987 |
|
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
988 |
lemma degree_setprod_setsum_le: "finite S \<Longrightarrow> degree (setprod f S) \<le> setsum (degree o f) S" |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
989 |
proof (induct S rule: finite_induct) |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
990 |
case (insert a S) |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
991 |
show ?case unfolding setprod.insert[OF insert(1-2)] setsum.insert[OF insert(1-2)] |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
992 |
by (rule le_trans[OF degree_mult_le], insert insert, auto) |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
993 |
qed simp |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
994 |
|
62065 | 995 |
subsection \<open>Conversions from natural numbers\<close> |
996 |
||
997 |
lemma of_nat_poly: "of_nat n = [:of_nat n :: 'a :: comm_semiring_1:]" |
|
998 |
proof (induction n) |
|
999 |
case (Suc n) |
|
1000 |
hence "of_nat (Suc n) = 1 + (of_nat n :: 'a poly)" |
|
1001 |
by simp |
|
1002 |
also have "(of_nat n :: 'a poly) = [: of_nat n :]" |
|
1003 |
by (subst Suc) (rule refl) |
|
1004 |
also have "1 = [:1:]" by (simp add: one_poly_def) |
|
1005 |
finally show ?case by (subst (asm) add_pCons) simp |
|
1006 |
qed simp |
|
1007 |
||
1008 |
lemma degree_of_nat [simp]: "degree (of_nat n) = 0" |
|
1009 |
by (simp add: of_nat_poly) |
|
1010 |
||
1011 |
lemma degree_numeral [simp]: "degree (numeral n) = 0" |
|
1012 |
by (subst of_nat_numeral [symmetric], subst of_nat_poly) simp |
|
1013 |
||
1014 |
lemma numeral_poly: "numeral n = [:numeral n:]" |
|
1015 |
by (subst of_nat_numeral [symmetric], subst of_nat_poly) simp |
|
52380 | 1016 |
|
60500 | 1017 |
subsection \<open>Lemmas about divisibility\<close> |
29979 | 1018 |
|
1019 |
lemma dvd_smult: "p dvd q \<Longrightarrow> p dvd smult a q" |
|
1020 |
proof - |
|
1021 |
assume "p dvd q" |
|
1022 |
then obtain k where "q = p * k" .. |
|
1023 |
then have "smult a q = p * smult a k" by simp |
|
1024 |
then show "p dvd smult a q" .. |
|
1025 |
qed |
|
1026 |
||
1027 |
lemma dvd_smult_cancel: |
|
62128
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
1028 |
fixes a :: "'a :: field" |
29979 | 1029 |
shows "p dvd smult a q \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> p dvd q" |
1030 |
by (drule dvd_smult [where a="inverse a"]) simp |
|
1031 |
||
1032 |
lemma dvd_smult_iff: |
|
1033 |
fixes a :: "'a::field" |
|
1034 |
shows "a \<noteq> 0 \<Longrightarrow> p dvd smult a q \<longleftrightarrow> p dvd q" |
|
1035 |
by (safe elim!: dvd_smult dvd_smult_cancel) |
|
1036 |
||
31663 | 1037 |
lemma smult_dvd_cancel: |
1038 |
"smult a p dvd q \<Longrightarrow> p dvd q" |
|
1039 |
proof - |
|
1040 |
assume "smult a p dvd q" |
|
1041 |
then obtain k where "q = smult a p * k" .. |
|
1042 |
then have "q = p * smult a k" by simp |
|
1043 |
then show "p dvd q" .. |
|
1044 |
qed |
|
1045 |
||
1046 |
lemma smult_dvd: |
|
1047 |
fixes a :: "'a::field" |
|
1048 |
shows "p dvd q \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> smult a p dvd q" |
|
1049 |
by (rule smult_dvd_cancel [where a="inverse a"]) simp |
|
1050 |
||
1051 |
lemma smult_dvd_iff: |
|
1052 |
fixes a :: "'a::field" |
|
1053 |
shows "smult a p dvd q \<longleftrightarrow> (if a = 0 then q = 0 else p dvd q)" |
|
1054 |
by (auto elim: smult_dvd smult_dvd_cancel) |
|
1055 |
||
29451 | 1056 |
|
60500 | 1057 |
subsection \<open>Polynomials form an integral domain\<close> |
29451 | 1058 |
|
1059 |
lemma coeff_mult_degree_sum: |
|
1060 |
"coeff (p * q) (degree p + degree q) = |
|
1061 |
coeff p (degree p) * coeff q (degree q)" |
|
29471 | 1062 |
by (induct p, simp, simp add: coeff_eq_0) |
29451 | 1063 |
|
1064 |
instance poly :: (idom) idom |
|
1065 |
proof |
|
1066 |
fix p q :: "'a poly" |
|
1067 |
assume "p \<noteq> 0" and "q \<noteq> 0" |
|
1068 |
have "coeff (p * q) (degree p + degree q) = |
|
1069 |
coeff p (degree p) * coeff q (degree q)" |
|
1070 |
by (rule coeff_mult_degree_sum) |
|
1071 |
also have "coeff p (degree p) * coeff q (degree q) \<noteq> 0" |
|
60500 | 1072 |
using \<open>p \<noteq> 0\<close> and \<open>q \<noteq> 0\<close> by simp |
29451 | 1073 |
finally have "\<exists>n. coeff (p * q) n \<noteq> 0" .. |
52380 | 1074 |
thus "p * q \<noteq> 0" by (simp add: poly_eq_iff) |
29451 | 1075 |
qed |
1076 |
||
1077 |
lemma degree_mult_eq: |
|
62128
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
1078 |
fixes p q :: "'a::semidom poly" |
29451 | 1079 |
shows "\<lbrakk>p \<noteq> 0; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree (p * q) = degree p + degree q" |
1080 |
apply (rule order_antisym [OF degree_mult_le le_degree]) |
|
1081 |
apply (simp add: coeff_mult_degree_sum) |
|
1082 |
done |
|
1083 |
||
60570 | 1084 |
lemma degree_mult_right_le: |
62128
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
1085 |
fixes p q :: "'a::semidom poly" |
60570 | 1086 |
assumes "q \<noteq> 0" |
1087 |
shows "degree p \<le> degree (p * q)" |
|
1088 |
using assms by (cases "p = 0") (simp_all add: degree_mult_eq) |
|
1089 |
||
1090 |
lemma coeff_degree_mult: |
|
62128
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
1091 |
fixes p q :: "'a::semidom poly" |
60570 | 1092 |
shows "coeff (p * q) (degree (p * q)) = |
1093 |
coeff q (degree q) * coeff p (degree p)" |
|
62128
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
1094 |
by (cases "p = 0 \<or> q = 0") (auto simp add: degree_mult_eq coeff_mult_degree_sum mult_ac) |
60570 | 1095 |
|
29451 | 1096 |
lemma dvd_imp_degree_le: |
62128
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
1097 |
fixes p q :: "'a::semidom poly" |
29451 | 1098 |
shows "\<lbrakk>p dvd q; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree p \<le> degree q" |
62128
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
1099 |
by (erule dvdE, hypsubst, subst degree_mult_eq) auto |
29451 | 1100 |
|
62128
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
1101 |
lemma divides_degree: |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
1102 |
assumes pq: "p dvd (q :: 'a :: semidom poly)" |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
1103 |
shows "degree p \<le> degree q \<or> q = 0" |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
1104 |
by (metis dvd_imp_degree_le pq) |
29451 | 1105 |
|
60500 | 1106 |
subsection \<open>Polynomials form an ordered integral domain\<close> |
29878 | 1107 |
|
52380 | 1108 |
definition pos_poly :: "'a::linordered_idom poly \<Rightarrow> bool" |
29878 | 1109 |
where |
1110 |
"pos_poly p \<longleftrightarrow> 0 < coeff p (degree p)" |
|
1111 |
||
1112 |
lemma pos_poly_pCons: |
|
1113 |
"pos_poly (pCons a p) \<longleftrightarrow> pos_poly p \<or> (p = 0 \<and> 0 < a)" |
|
1114 |
unfolding pos_poly_def by simp |
|
1115 |
||
1116 |
lemma not_pos_poly_0 [simp]: "\<not> pos_poly 0" |
|
1117 |
unfolding pos_poly_def by simp |
|
1118 |
||
1119 |
lemma pos_poly_add: "\<lbrakk>pos_poly p; pos_poly q\<rbrakk> \<Longrightarrow> pos_poly (p + q)" |
|
1120 |
apply (induct p arbitrary: q, simp) |
|
1121 |
apply (case_tac q, force simp add: pos_poly_pCons add_pos_pos) |
|
1122 |
done |
|
1123 |
||
1124 |
lemma pos_poly_mult: "\<lbrakk>pos_poly p; pos_poly q\<rbrakk> \<Longrightarrow> pos_poly (p * q)" |
|
1125 |
unfolding pos_poly_def |
|
1126 |
apply (subgoal_tac "p \<noteq> 0 \<and> q \<noteq> 0") |
|
56544 | 1127 |
apply (simp add: degree_mult_eq coeff_mult_degree_sum) |
29878 | 1128 |
apply auto |
1129 |
done |
|
1130 |
||
1131 |
lemma pos_poly_total: "p = 0 \<or> pos_poly p \<or> pos_poly (- p)" |
|
1132 |
by (induct p) (auto simp add: pos_poly_pCons) |
|
1133 |
||
52380 | 1134 |
lemma last_coeffs_eq_coeff_degree: |
1135 |
"p \<noteq> 0 \<Longrightarrow> last (coeffs p) = coeff p (degree p)" |
|
1136 |
by (simp add: coeffs_def) |
|
1137 |
||
1138 |
lemma pos_poly_coeffs [code]: |
|
1139 |
"pos_poly p \<longleftrightarrow> (let as = coeffs p in as \<noteq> [] \<and> last as > 0)" (is "?P \<longleftrightarrow> ?Q") |
|
1140 |
proof |
|
1141 |
assume ?Q then show ?P by (auto simp add: pos_poly_def last_coeffs_eq_coeff_degree) |
|
1142 |
next |
|
1143 |
assume ?P then have *: "0 < coeff p (degree p)" by (simp add: pos_poly_def) |
|
1144 |
then have "p \<noteq> 0" by auto |
|
1145 |
with * show ?Q by (simp add: last_coeffs_eq_coeff_degree) |
|
1146 |
qed |
|
1147 |
||
35028
108662d50512
more consistent naming of type classes involving orderings (and lattices) -- c.f. NEWS
haftmann
parents:
34973
diff
changeset
|
1148 |
instantiation poly :: (linordered_idom) linordered_idom |
29878 | 1149 |
begin |
1150 |
||
1151 |
definition |
|
37765 | 1152 |
"x < y \<longleftrightarrow> pos_poly (y - x)" |
29878 | 1153 |
|
1154 |
definition |
|
37765 | 1155 |
"x \<le> y \<longleftrightarrow> x = y \<or> pos_poly (y - x)" |
29878 | 1156 |
|
1157 |
definition |
|
61945 | 1158 |
"\<bar>x::'a poly\<bar> = (if x < 0 then - x else x)" |
29878 | 1159 |
|
1160 |
definition |
|
37765 | 1161 |
"sgn (x::'a poly) = (if x = 0 then 0 else if 0 < x then 1 else - 1)" |
29878 | 1162 |
|
60679 | 1163 |
instance |
1164 |
proof |
|
1165 |
fix x y z :: "'a poly" |
|
29878 | 1166 |
show "x < y \<longleftrightarrow> x \<le> y \<and> \<not> y \<le> x" |
1167 |
unfolding less_eq_poly_def less_poly_def |
|
1168 |
apply safe |
|
1169 |
apply simp |
|
1170 |
apply (drule (1) pos_poly_add) |
|
1171 |
apply simp |
|
1172 |
done |
|
60679 | 1173 |
show "x \<le> x" |
29878 | 1174 |
unfolding less_eq_poly_def by simp |
60679 | 1175 |
show "x \<le> y \<Longrightarrow> y \<le> z \<Longrightarrow> x \<le> z" |
29878 | 1176 |
unfolding less_eq_poly_def |
1177 |
apply safe |
|
1178 |
apply (drule (1) pos_poly_add) |
|
1179 |
apply (simp add: algebra_simps) |
|
1180 |
done |
|
60679 | 1181 |
show "x \<le> y \<Longrightarrow> y \<le> x \<Longrightarrow> x = y" |
29878 | 1182 |
unfolding less_eq_poly_def |
1183 |
apply safe |
|
1184 |
apply (drule (1) pos_poly_add) |
|
1185 |
apply simp |
|
1186 |
done |
|
60679 | 1187 |
show "x \<le> y \<Longrightarrow> z + x \<le> z + y" |
29878 | 1188 |
unfolding less_eq_poly_def |
1189 |
apply safe |
|
1190 |
apply (simp add: algebra_simps) |
|
1191 |
done |
|
1192 |
show "x \<le> y \<or> y \<le> x" |
|
1193 |
unfolding less_eq_poly_def |
|
1194 |
using pos_poly_total [of "x - y"] |
|
1195 |
by auto |
|
60679 | 1196 |
show "x < y \<Longrightarrow> 0 < z \<Longrightarrow> z * x < z * y" |
29878 | 1197 |
unfolding less_poly_def |
1198 |
by (simp add: right_diff_distrib [symmetric] pos_poly_mult) |
|
1199 |
show "\<bar>x\<bar> = (if x < 0 then - x else x)" |
|
1200 |
by (rule abs_poly_def) |
|
1201 |
show "sgn x = (if x = 0 then 0 else if 0 < x then 1 else - 1)" |
|
1202 |
by (rule sgn_poly_def) |
|
1203 |
qed |
|
1204 |
||
1205 |
end |
|
1206 |
||
60500 | 1207 |
text \<open>TODO: Simplification rules for comparisons\<close> |
29878 | 1208 |
|
1209 |
||
60500 | 1210 |
subsection \<open>Synthetic division and polynomial roots\<close> |
52380 | 1211 |
|
60500 | 1212 |
text \<open> |
52380 | 1213 |
Synthetic division is simply division by the linear polynomial @{term "x - c"}. |
60500 | 1214 |
\<close> |
52380 | 1215 |
|
1216 |
definition synthetic_divmod :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly \<times> 'a" |
|
1217 |
where |
|
1218 |
"synthetic_divmod p c = fold_coeffs (\<lambda>a (q, r). (pCons r q, a + c * r)) p (0, 0)" |
|
1219 |
||
1220 |
definition synthetic_div :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly" |
|
1221 |
where |
|
1222 |
"synthetic_div p c = fst (synthetic_divmod p c)" |
|
1223 |
||
1224 |
lemma synthetic_divmod_0 [simp]: |
|
1225 |
"synthetic_divmod 0 c = (0, 0)" |
|
1226 |
by (simp add: synthetic_divmod_def) |
|
1227 |
||
1228 |
lemma synthetic_divmod_pCons [simp]: |
|
1229 |
"synthetic_divmod (pCons a p) c = (\<lambda>(q, r). (pCons r q, a + c * r)) (synthetic_divmod p c)" |
|
1230 |
by (cases "p = 0 \<and> a = 0") (auto simp add: synthetic_divmod_def) |
|
1231 |
||
1232 |
lemma synthetic_div_0 [simp]: |
|
1233 |
"synthetic_div 0 c = 0" |
|
1234 |
unfolding synthetic_div_def by simp |
|
1235 |
||
1236 |
lemma synthetic_div_unique_lemma: "smult c p = pCons a p \<Longrightarrow> p = 0" |
|
1237 |
by (induct p arbitrary: a) simp_all |
|
1238 |
||
1239 |
lemma snd_synthetic_divmod: |
|
1240 |
"snd (synthetic_divmod p c) = poly p c" |
|
1241 |
by (induct p, simp, simp add: split_def) |
|
1242 |
||
1243 |
lemma synthetic_div_pCons [simp]: |
|
1244 |
"synthetic_div (pCons a p) c = pCons (poly p c) (synthetic_div p c)" |
|
1245 |
unfolding synthetic_div_def |
|
1246 |
by (simp add: split_def snd_synthetic_divmod) |
|
1247 |
||
1248 |
lemma synthetic_div_eq_0_iff: |
|
1249 |
"synthetic_div p c = 0 \<longleftrightarrow> degree p = 0" |
|
1250 |
by (induct p, simp, case_tac p, simp) |
|
1251 |
||
1252 |
lemma degree_synthetic_div: |
|
1253 |
"degree (synthetic_div p c) = degree p - 1" |
|
1254 |
by (induct p, simp, simp add: synthetic_div_eq_0_iff) |
|
1255 |
||
1256 |
lemma synthetic_div_correct: |
|
1257 |
"p + smult c (synthetic_div p c) = pCons (poly p c) (synthetic_div p c)" |
|
1258 |
by (induct p) simp_all |
|
1259 |
||
1260 |
lemma synthetic_div_unique: |
|
1261 |
"p + smult c q = pCons r q \<Longrightarrow> r = poly p c \<and> q = synthetic_div p c" |
|
1262 |
apply (induct p arbitrary: q r) |
|
1263 |
apply (simp, frule synthetic_div_unique_lemma, simp) |
|
1264 |
apply (case_tac q, force) |
|
1265 |
done |
|
1266 |
||
1267 |
lemma synthetic_div_correct': |
|
1268 |
fixes c :: "'a::comm_ring_1" |
|
1269 |
shows "[:-c, 1:] * synthetic_div p c + [:poly p c:] = p" |
|
1270 |
using synthetic_div_correct [of p c] |
|
1271 |
by (simp add: algebra_simps) |
|
1272 |
||
1273 |
lemma poly_eq_0_iff_dvd: |
|
1274 |
fixes c :: "'a::idom" |
|
1275 |
shows "poly p c = 0 \<longleftrightarrow> [:-c, 1:] dvd p" |
|
1276 |
proof |
|
1277 |
assume "poly p c = 0" |
|
1278 |
with synthetic_div_correct' [of c p] |
|
1279 |
have "p = [:-c, 1:] * synthetic_div p c" by simp |
|
1280 |
then show "[:-c, 1:] dvd p" .. |
|
1281 |
next |
|
1282 |
assume "[:-c, 1:] dvd p" |
|
1283 |
then obtain k where "p = [:-c, 1:] * k" by (rule dvdE) |
|
1284 |
then show "poly p c = 0" by simp |
|
1285 |
qed |
|
1286 |
||
1287 |
lemma dvd_iff_poly_eq_0: |
|
1288 |
fixes c :: "'a::idom" |
|
1289 |
shows "[:c, 1:] dvd p \<longleftrightarrow> poly p (-c) = 0" |
|
1290 |
by (simp add: poly_eq_0_iff_dvd) |
|
1291 |
||
1292 |
lemma poly_roots_finite: |
|
1293 |
fixes p :: "'a::idom poly" |
|
1294 |
shows "p \<noteq> 0 \<Longrightarrow> finite {x. poly p x = 0}" |
|
1295 |
proof (induct n \<equiv> "degree p" arbitrary: p) |
|
1296 |
case (0 p) |
|
1297 |
then obtain a where "a \<noteq> 0" and "p = [:a:]" |
|
1298 |
by (cases p, simp split: if_splits) |
|
1299 |
then show "finite {x. poly p x = 0}" by simp |
|
1300 |
next |
|
1301 |
case (Suc n p) |
|
1302 |
show "finite {x. poly p x = 0}" |
|
1303 |
proof (cases "\<exists>x. poly p x = 0") |
|
1304 |
case False |
|
1305 |
then show "finite {x. poly p x = 0}" by simp |
|
1306 |
next |
|
1307 |
case True |
|
1308 |
then obtain a where "poly p a = 0" .. |
|
1309 |
then have "[:-a, 1:] dvd p" by (simp only: poly_eq_0_iff_dvd) |
|
1310 |
then obtain k where k: "p = [:-a, 1:] * k" .. |
|
60500 | 1311 |
with \<open>p \<noteq> 0\<close> have "k \<noteq> 0" by auto |
52380 | 1312 |
with k have "degree p = Suc (degree k)" |
1313 |
by (simp add: degree_mult_eq del: mult_pCons_left) |
|
60500 | 1314 |
with \<open>Suc n = degree p\<close> have "n = degree k" by simp |
1315 |
then have "finite {x. poly k x = 0}" using \<open>k \<noteq> 0\<close> by (rule Suc.hyps) |
|
52380 | 1316 |
then have "finite (insert a {x. poly k x = 0})" by simp |
1317 |
then show "finite {x. poly p x = 0}" |
|
57862 | 1318 |
by (simp add: k Collect_disj_eq del: mult_pCons_left) |
52380 | 1319 |
qed |
1320 |
qed |
|
1321 |
||
1322 |
lemma poly_eq_poly_eq_iff: |
|
1323 |
fixes p q :: "'a::{idom,ring_char_0} poly" |
|
1324 |
shows "poly p = poly q \<longleftrightarrow> p = q" (is "?P \<longleftrightarrow> ?Q") |
|
1325 |
proof |
|
1326 |
assume ?Q then show ?P by simp |
|
1327 |
next |
|
1328 |
{ fix p :: "'a::{idom,ring_char_0} poly" |
|
1329 |
have "poly p = poly 0 \<longleftrightarrow> p = 0" |
|
1330 |
apply (cases "p = 0", simp_all) |
|
1331 |
apply (drule poly_roots_finite) |
|
1332 |
apply (auto simp add: infinite_UNIV_char_0) |
|
1333 |
done |
|
1334 |
} note this [of "p - q"] |
|
1335 |
moreover assume ?P |
|
1336 |
ultimately show ?Q by auto |
|
1337 |
qed |
|
1338 |
||
1339 |
lemma poly_all_0_iff_0: |
|
1340 |
fixes p :: "'a::{ring_char_0, idom} poly" |
|
1341 |
shows "(\<forall>x. poly p x = 0) \<longleftrightarrow> p = 0" |
|
1342 |
by (auto simp add: poly_eq_poly_eq_iff [symmetric]) |
|
1343 |
||
1344 |
||
60500 | 1345 |
subsection \<open>Long division of polynomials\<close> |
29451 | 1346 |
|
52380 | 1347 |
definition pdivmod_rel :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> bool" |
29451 | 1348 |
where |
29537 | 1349 |
"pdivmod_rel x y q r \<longleftrightarrow> |
29451 | 1350 |
x = q * y + r \<and> (if y = 0 then q = 0 else r = 0 \<or> degree r < degree y)" |
1351 |
||
29537 | 1352 |
lemma pdivmod_rel_0: |
1353 |
"pdivmod_rel 0 y 0 0" |
|
1354 |
unfolding pdivmod_rel_def by simp |
|
29451 | 1355 |
|
29537 | 1356 |
lemma pdivmod_rel_by_0: |
1357 |
"pdivmod_rel x 0 0 x" |
|
1358 |
unfolding pdivmod_rel_def by simp |
|
29451 | 1359 |
|
1360 |
lemma eq_zero_or_degree_less: |
|
1361 |
assumes "degree p \<le> n" and "coeff p n = 0" |
|
1362 |
shows "p = 0 \<or> degree p < n" |
|
1363 |
proof (cases n) |
|
1364 |
case 0 |
|
60500 | 1365 |
with \<open>degree p \<le> n\<close> and \<open>coeff p n = 0\<close> |
29451 | 1366 |
have "coeff p (degree p) = 0" by simp |
1367 |
then have "p = 0" by simp |
|
1368 |
then show ?thesis .. |
|
1369 |
next |
|
1370 |
case (Suc m) |
|
1371 |
have "\<forall>i>n. coeff p i = 0" |
|
60500 | 1372 |
using \<open>degree p \<le> n\<close> by (simp add: coeff_eq_0) |
29451 | 1373 |
then have "\<forall>i\<ge>n. coeff p i = 0" |
60500 | 1374 |
using \<open>coeff p n = 0\<close> by (simp add: le_less) |
29451 | 1375 |
then have "\<forall>i>m. coeff p i = 0" |
60500 | 1376 |
using \<open>n = Suc m\<close> by (simp add: less_eq_Suc_le) |
29451 | 1377 |
then have "degree p \<le> m" |
1378 |
by (rule degree_le) |
|
1379 |
then have "degree p < n" |
|
60500 | 1380 |
using \<open>n = Suc m\<close> by (simp add: less_Suc_eq_le) |
29451 | 1381 |
then show ?thesis .. |
1382 |
qed |
|
1383 |
||
29537 | 1384 |
lemma pdivmod_rel_pCons: |
1385 |
assumes rel: "pdivmod_rel x y q r" |
|
29451 | 1386 |
assumes y: "y \<noteq> 0" |
1387 |
assumes b: "b = coeff (pCons a r) (degree y) / coeff y (degree y)" |
|
29537 | 1388 |
shows "pdivmod_rel (pCons a x) y (pCons b q) (pCons a r - smult b y)" |
1389 |
(is "pdivmod_rel ?x y ?q ?r") |
|
29451 | 1390 |
proof - |
1391 |
have x: "x = q * y + r" and r: "r = 0 \<or> degree r < degree y" |
|
29537 | 1392 |
using assms unfolding pdivmod_rel_def by simp_all |
29451 | 1393 |
|
1394 |
have 1: "?x = ?q * y + ?r" |
|
1395 |
using b x by simp |
|
1396 |
||
1397 |
have 2: "?r = 0 \<or> degree ?r < degree y" |
|
1398 |
proof (rule eq_zero_or_degree_less) |
|
29539 | 1399 |
show "degree ?r \<le> degree y" |
1400 |
proof (rule degree_diff_le) |
|
29451 | 1401 |
show "degree (pCons a r) \<le> degree y" |
29460
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
1402 |
using r by auto |
29451 | 1403 |
show "degree (smult b y) \<le> degree y" |
1404 |
by (rule degree_smult_le) |
|
1405 |
qed |
|
1406 |
next |
|
1407 |
show "coeff ?r (degree y) = 0" |
|
60500 | 1408 |
using \<open>y \<noteq> 0\<close> unfolding b by simp |
29451 | 1409 |
qed |
1410 |
||
1411 |
from 1 2 show ?thesis |
|
29537 | 1412 |
unfolding pdivmod_rel_def |
60500 | 1413 |
using \<open>y \<noteq> 0\<close> by simp |
29451 | 1414 |
qed |
1415 |
||
29537 | 1416 |
lemma pdivmod_rel_exists: "\<exists>q r. pdivmod_rel x y q r" |
29451 | 1417 |
apply (cases "y = 0") |
29537 | 1418 |
apply (fast intro!: pdivmod_rel_by_0) |
29451 | 1419 |
apply (induct x) |
29537 | 1420 |
apply (fast intro!: pdivmod_rel_0) |
1421 |
apply (fast intro!: pdivmod_rel_pCons) |
|
29451 | 1422 |
done |
1423 |
||
29537 | 1424 |
lemma pdivmod_rel_unique: |
1425 |
assumes 1: "pdivmod_rel x y q1 r1" |
|
1426 |
assumes 2: "pdivmod_rel x y q2 r2" |
|
29451 | 1427 |
shows "q1 = q2 \<and> r1 = r2" |
1428 |
proof (cases "y = 0") |
|
1429 |
assume "y = 0" with assms show ?thesis |
|
29537 | 1430 |
by (simp add: pdivmod_rel_def) |
29451 | 1431 |
next |
1432 |
assume [simp]: "y \<noteq> 0" |
|
1433 |
from 1 have q1: "x = q1 * y + r1" and r1: "r1 = 0 \<or> degree r1 < degree y" |
|
29537 | 1434 |
unfolding pdivmod_rel_def by simp_all |
29451 | 1435 |
from 2 have q2: "x = q2 * y + r2" and r2: "r2 = 0 \<or> degree r2 < degree y" |
29537 | 1436 |
unfolding pdivmod_rel_def by simp_all |
29451 | 1437 |
from q1 q2 have q3: "(q1 - q2) * y = r2 - r1" |
29667 | 1438 |
by (simp add: algebra_simps) |
29451 | 1439 |
from r1 r2 have r3: "(r2 - r1) = 0 \<or> degree (r2 - r1) < degree y" |
29453 | 1440 |
by (auto intro: degree_diff_less) |
29451 | 1441 |
|
1442 |
show "q1 = q2 \<and> r1 = r2" |
|
1443 |
proof (rule ccontr) |
|
1444 |
assume "\<not> (q1 = q2 \<and> r1 = r2)" |
|
1445 |
with q3 have "q1 \<noteq> q2" and "r1 \<noteq> r2" by auto |
|
1446 |
with r3 have "degree (r2 - r1) < degree y" by simp |
|
1447 |
also have "degree y \<le> degree (q1 - q2) + degree y" by simp |
|
1448 |
also have "\<dots> = degree ((q1 - q2) * y)" |
|
60500 | 1449 |
using \<open>q1 \<noteq> q2\<close> by (simp add: degree_mult_eq) |
29451 | 1450 |
also have "\<dots> = degree (r2 - r1)" |
1451 |
using q3 by simp |
|
1452 |
finally have "degree (r2 - r1) < degree (r2 - r1)" . |
|
1453 |
then show "False" by simp |
|
1454 |
qed |
|
1455 |
qed |
|
1456 |
||
29660 | 1457 |
lemma pdivmod_rel_0_iff: "pdivmod_rel 0 y q r \<longleftrightarrow> q = 0 \<and> r = 0" |
1458 |
by (auto dest: pdivmod_rel_unique intro: pdivmod_rel_0) |
|
1459 |
||
1460 |
lemma pdivmod_rel_by_0_iff: "pdivmod_rel x 0 q r \<longleftrightarrow> q = 0 \<and> r = x" |
|
1461 |
by (auto dest: pdivmod_rel_unique intro: pdivmod_rel_by_0) |
|
1462 |
||
45605 | 1463 |
lemmas pdivmod_rel_unique_div = pdivmod_rel_unique [THEN conjunct1] |
29451 | 1464 |
|
45605 | 1465 |
lemmas pdivmod_rel_unique_mod = pdivmod_rel_unique [THEN conjunct2] |
29451 | 1466 |
|
1467 |
instantiation poly :: (field) ring_div |
|
1468 |
begin |
|
1469 |
||
60352
d46de31a50c4
separate class for division operator, with particular syntax added in more specific classes
haftmann
parents:
60040
diff
changeset
|
1470 |
definition divide_poly where |
60429
d3d1e185cd63
uniform _ div _ as infix syntax for ring division
haftmann
parents:
60352
diff
changeset
|
1471 |
div_poly_def: "x div y = (THE q. \<exists>r. pdivmod_rel x y q r)" |
29451 | 1472 |
|
1473 |
definition mod_poly where |
|
37765 | 1474 |
"x mod y = (THE r. \<exists>q. pdivmod_rel x y q r)" |
29451 | 1475 |
|
1476 |
lemma div_poly_eq: |
|
60429
d3d1e185cd63
uniform _ div _ as infix syntax for ring division
haftmann
parents:
60352
diff
changeset
|
1477 |
"pdivmod_rel x y q r \<Longrightarrow> x div y = q" |
29451 | 1478 |
unfolding div_poly_def |
29537 | 1479 |
by (fast elim: pdivmod_rel_unique_div) |
29451 | 1480 |
|
1481 |
lemma mod_poly_eq: |
|
29537 | 1482 |
"pdivmod_rel x y q r \<Longrightarrow> x mod y = r" |
29451 | 1483 |
unfolding mod_poly_def |
29537 | 1484 |
by (fast elim: pdivmod_rel_unique_mod) |
29451 | 1485 |
|
29537 | 1486 |
lemma pdivmod_rel: |
60429
d3d1e185cd63
uniform _ div _ as infix syntax for ring division
haftmann
parents:
60352
diff
changeset
|
1487 |
"pdivmod_rel x y (x div y) (x mod y)" |
29451 | 1488 |
proof - |
29537 | 1489 |
from pdivmod_rel_exists |
1490 |
obtain q r where "pdivmod_rel x y q r" by fast |
|
29451 | 1491 |
thus ?thesis |
1492 |
by (simp add: div_poly_eq mod_poly_eq) |
|
1493 |
qed |
|
1494 |
||
60679 | 1495 |
instance |
1496 |
proof |
|
29451 | 1497 |
fix x y :: "'a poly" |
60429
d3d1e185cd63
uniform _ div _ as infix syntax for ring division
haftmann
parents:
60352
diff
changeset
|
1498 |
show "x div y * y + x mod y = x" |
29537 | 1499 |
using pdivmod_rel [of x y] |
1500 |
by (simp add: pdivmod_rel_def) |
|
29451 | 1501 |
next |
1502 |
fix x :: "'a poly" |
|
29537 | 1503 |
have "pdivmod_rel x 0 0 x" |
1504 |
by (rule pdivmod_rel_by_0) |
|
60429
d3d1e185cd63
uniform _ div _ as infix syntax for ring division
haftmann
parents:
60352
diff
changeset
|
1505 |
thus "x div 0 = 0" |
29451 | 1506 |
by (rule div_poly_eq) |
1507 |
next |
|
1508 |
fix y :: "'a poly" |
|
29537 | 1509 |
have "pdivmod_rel 0 y 0 0" |
1510 |
by (rule pdivmod_rel_0) |
|
60429
d3d1e185cd63
uniform _ div _ as infix syntax for ring division
haftmann
parents:
60352
diff
changeset
|
1511 |
thus "0 div y = 0" |
29451 | 1512 |
by (rule div_poly_eq) |
1513 |
next |
|
1514 |
fix x y z :: "'a poly" |
|
1515 |
assume "y \<noteq> 0" |
|
60429
d3d1e185cd63
uniform _ div _ as infix syntax for ring division
haftmann
parents:
60352
diff
changeset
|
1516 |
hence "pdivmod_rel (x + z * y) y (z + x div y) (x mod y)" |
29537 | 1517 |
using pdivmod_rel [of x y] |
49962
a8cc904a6820
Renamed {left,right}_distrib to distrib_{right,left}.
webertj
parents:
49834
diff
changeset
|
1518 |
by (simp add: pdivmod_rel_def distrib_right) |
60429
d3d1e185cd63
uniform _ div _ as infix syntax for ring division
haftmann
parents:
60352
diff
changeset
|
1519 |
thus "(x + z * y) div y = z + x div y" |
29451 | 1520 |
by (rule div_poly_eq) |
30930 | 1521 |
next |
1522 |
fix x y z :: "'a poly" |
|
1523 |
assume "x \<noteq> 0" |
|
60429
d3d1e185cd63
uniform _ div _ as infix syntax for ring division
haftmann
parents:
60352
diff
changeset
|
1524 |
show "(x * y) div (x * z) = y div z" |
30930 | 1525 |
proof (cases "y \<noteq> 0 \<and> z \<noteq> 0") |
1526 |
have "\<And>x::'a poly. pdivmod_rel x 0 0 x" |
|
1527 |
by (rule pdivmod_rel_by_0) |
|
60429
d3d1e185cd63
uniform _ div _ as infix syntax for ring division
haftmann
parents:
60352
diff
changeset
|
1528 |
then have [simp]: "\<And>x::'a poly. x div 0 = 0" |
30930 | 1529 |
by (rule div_poly_eq) |
1530 |
have "\<And>x::'a poly. pdivmod_rel 0 x 0 0" |
|
1531 |
by (rule pdivmod_rel_0) |
|
60429
d3d1e185cd63
uniform _ div _ as infix syntax for ring division
haftmann
parents:
60352
diff
changeset
|
1532 |
then have [simp]: "\<And>x::'a poly. 0 div x = 0" |
30930 | 1533 |
by (rule div_poly_eq) |
1534 |
case False then show ?thesis by auto |
|
1535 |
next |
|
1536 |
case True then have "y \<noteq> 0" and "z \<noteq> 0" by auto |
|
60500 | 1537 |
with \<open>x \<noteq> 0\<close> |
30930 | 1538 |
have "\<And>q r. pdivmod_rel y z q r \<Longrightarrow> pdivmod_rel (x * y) (x * z) q (x * r)" |
1539 |
by (auto simp add: pdivmod_rel_def algebra_simps) |
|
1540 |
(rule classical, simp add: degree_mult_eq) |
|
60429
d3d1e185cd63
uniform _ div _ as infix syntax for ring division
haftmann
parents:
60352
diff
changeset
|
1541 |
moreover from pdivmod_rel have "pdivmod_rel y z (y div z) (y mod z)" . |
d3d1e185cd63
uniform _ div _ as infix syntax for ring division
haftmann
parents:
60352
diff
changeset
|
1542 |
ultimately have "pdivmod_rel (x * y) (x * z) (y div z) (x * (y mod z))" . |
30930 | 1543 |
then show ?thesis by (simp add: div_poly_eq) |
1544 |
qed |
|
29451 | 1545 |
qed |
1546 |
||
1547 |
end |
|
1548 |
||
60570 | 1549 |
lemma is_unit_monom_0: |
1550 |
fixes a :: "'a::field" |
|
1551 |
assumes "a \<noteq> 0" |
|
1552 |
shows "is_unit (monom a 0)" |
|
1553 |
proof |
|
1554 |
from assms show "1 = monom a 0 * monom (1 / a) 0" |
|
1555 |
by (simp add: mult_monom) |
|
1556 |
qed |
|
1557 |
||
1558 |
lemma is_unit_triv: |
|
1559 |
fixes a :: "'a::field" |
|
1560 |
assumes "a \<noteq> 0" |
|
1561 |
shows "is_unit [:a:]" |
|
1562 |
using assms by (simp add: is_unit_monom_0 monom_0 [symmetric]) |
|
1563 |
||
1564 |
lemma is_unit_iff_degree: |
|
1565 |
assumes "p \<noteq> 0" |
|
1566 |
shows "is_unit p \<longleftrightarrow> degree p = 0" (is "?P \<longleftrightarrow> ?Q") |
|
1567 |
proof |
|
1568 |
assume ?Q |
|
1569 |
then obtain a where "p = [:a:]" by (rule degree_eq_zeroE) |
|
1570 |
with assms show ?P by (simp add: is_unit_triv) |
|
1571 |
next |
|
1572 |
assume ?P |
|
1573 |
then obtain q where "q \<noteq> 0" "p * q = 1" .. |
|
1574 |
then have "degree (p * q) = degree 1" |
|
1575 |
by simp |
|
1576 |
with \<open>p \<noteq> 0\<close> \<open>q \<noteq> 0\<close> have "degree p + degree q = 0" |
|
1577 |
by (simp add: degree_mult_eq) |
|
1578 |
then show ?Q by simp |
|
1579 |
qed |
|
1580 |
||
1581 |
lemma is_unit_pCons_iff: |
|
1582 |
"is_unit (pCons a p) \<longleftrightarrow> p = 0 \<and> a \<noteq> 0" (is "?P \<longleftrightarrow> ?Q") |
|
1583 |
by (cases "p = 0") (auto simp add: is_unit_triv is_unit_iff_degree) |
|
1584 |
||
1585 |
lemma is_unit_monom_trival: |
|
1586 |
fixes p :: "'a::field poly" |
|
1587 |
assumes "is_unit p" |
|
1588 |
shows "monom (coeff p (degree p)) 0 = p" |
|
1589 |
using assms by (cases p) (simp_all add: monom_0 is_unit_pCons_iff) |
|
1590 |
||
60685
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1591 |
lemma is_unit_polyE: |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1592 |
assumes "is_unit p" |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1593 |
obtains a where "p = monom a 0" and "a \<noteq> 0" |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1594 |
proof - |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1595 |
obtain a q where "p = pCons a q" by (cases p) |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1596 |
with assms have "p = [:a:]" and "a \<noteq> 0" |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1597 |
by (simp_all add: is_unit_pCons_iff) |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1598 |
with that show thesis by (simp add: monom_0) |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1599 |
qed |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1600 |
|
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1601 |
instantiation poly :: (field) normalization_semidom |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1602 |
begin |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1603 |
|
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1604 |
definition normalize_poly :: "'a poly \<Rightarrow> 'a poly" |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1605 |
where "normalize_poly p = smult (1 / coeff p (degree p)) p" |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1606 |
|
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1607 |
definition unit_factor_poly :: "'a poly \<Rightarrow> 'a poly" |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1608 |
where "unit_factor_poly p = monom (coeff p (degree p)) 0" |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1609 |
|
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1610 |
instance |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1611 |
proof |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1612 |
fix p :: "'a poly" |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1613 |
show "unit_factor p * normalize p = p" |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1614 |
by (simp add: normalize_poly_def unit_factor_poly_def) |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1615 |
(simp only: mult_smult_left [symmetric] smult_monom, simp) |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1616 |
next |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1617 |
show "normalize 0 = (0::'a poly)" |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1618 |
by (simp add: normalize_poly_def) |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1619 |
next |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1620 |
show "unit_factor 0 = (0::'a poly)" |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1621 |
by (simp add: unit_factor_poly_def) |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1622 |
next |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1623 |
fix p :: "'a poly" |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1624 |
assume "is_unit p" |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1625 |
then obtain a where "p = monom a 0" and "a \<noteq> 0" |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1626 |
by (rule is_unit_polyE) |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1627 |
then show "normalize p = 1" |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1628 |
by (auto simp add: normalize_poly_def smult_monom degree_monom_eq) |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1629 |
next |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1630 |
fix p q :: "'a poly" |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1631 |
assume "q \<noteq> 0" |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1632 |
from \<open>q \<noteq> 0\<close> have "is_unit (monom (coeff q (degree q)) 0)" |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1633 |
by (auto intro: is_unit_monom_0) |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1634 |
then show "is_unit (unit_factor q)" |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1635 |
by (simp add: unit_factor_poly_def) |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1636 |
next |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1637 |
fix p q :: "'a poly" |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1638 |
have "monom (coeff (p * q) (degree (p * q))) 0 = |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1639 |
monom (coeff p (degree p)) 0 * monom (coeff q (degree q)) 0" |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1640 |
by (simp add: monom_0 coeff_degree_mult) |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1641 |
then show "unit_factor (p * q) = |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1642 |
unit_factor p * unit_factor q" |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1643 |
by (simp add: unit_factor_poly_def) |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1644 |
qed |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1645 |
|
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1646 |
end |
cb21b7022b00
moved normalization and unit_factor into Main HOL corpus
haftmann
parents:
60679
diff
changeset
|
1647 |
|
29451 | 1648 |
lemma degree_mod_less: |
1649 |
"y \<noteq> 0 \<Longrightarrow> x mod y = 0 \<or> degree (x mod y) < degree y" |
|
29537 | 1650 |
using pdivmod_rel [of x y] |
1651 |
unfolding pdivmod_rel_def by simp |
|
29451 | 1652 |
|
1653 |
lemma div_poly_less: "degree x < degree y \<Longrightarrow> x div y = 0" |
|
1654 |
proof - |
|
1655 |
assume "degree x < degree y" |
|
29537 | 1656 |
hence "pdivmod_rel x y 0 x" |
1657 |
by (simp add: pdivmod_rel_def) |
|
29451 | 1658 |
thus "x div y = 0" by (rule div_poly_eq) |
1659 |
qed |
|
1660 |
||
1661 |
lemma mod_poly_less: "degree x < degree y \<Longrightarrow> x mod y = x" |
|
1662 |
proof - |
|
1663 |
assume "degree x < degree y" |
|
29537 | 1664 |
hence "pdivmod_rel x y 0 x" |
1665 |
by (simp add: pdivmod_rel_def) |
|
29451 | 1666 |
thus "x mod y = x" by (rule mod_poly_eq) |
1667 |
qed |
|
1668 |
||
29659 | 1669 |
lemma pdivmod_rel_smult_left: |
1670 |
"pdivmod_rel x y q r |
|
1671 |
\<Longrightarrow> pdivmod_rel (smult a x) y (smult a q) (smult a r)" |
|
1672 |
unfolding pdivmod_rel_def by (simp add: smult_add_right) |
|
1673 |
||
1674 |
lemma div_smult_left: "(smult a x) div y = smult a (x div y)" |
|
1675 |
by (rule div_poly_eq, rule pdivmod_rel_smult_left, rule pdivmod_rel) |
|
1676 |
||
1677 |
lemma mod_smult_left: "(smult a x) mod y = smult a (x mod y)" |
|
1678 |
by (rule mod_poly_eq, rule pdivmod_rel_smult_left, rule pdivmod_rel) |
|
1679 |
||
30072 | 1680 |
lemma poly_div_minus_left [simp]: |
1681 |
fixes x y :: "'a::field poly" |
|
1682 |
shows "(- x) div y = - (x div y)" |
|
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
1683 |
using div_smult_left [of "- 1::'a"] by simp |
30072 | 1684 |
|
1685 |
lemma poly_mod_minus_left [simp]: |
|
1686 |
fixes x y :: "'a::field poly" |
|
1687 |
shows "(- x) mod y = - (x mod y)" |
|
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
1688 |
using mod_smult_left [of "- 1::'a"] by simp |
30072 | 1689 |
|
57482
60459c3853af
add lemmas: polynomial div/mod distribute over addition
huffman
parents:
56544
diff
changeset
|
1690 |
lemma pdivmod_rel_add_left: |
60459c3853af
add lemmas: polynomial div/mod distribute over addition
huffman
parents:
56544
diff
changeset
|
1691 |
assumes "pdivmod_rel x y q r" |
60459c3853af
add lemmas: polynomial div/mod distribute over addition
huffman
parents:
56544
diff
changeset
|
1692 |
assumes "pdivmod_rel x' y q' r'" |
60459c3853af
add lemmas: polynomial div/mod distribute over addition
huffman
parents:
56544
diff
changeset
|
1693 |
shows "pdivmod_rel (x + x') y (q + q') (r + r')" |
60459c3853af
add lemmas: polynomial div/mod distribute over addition
huffman
parents:
56544
diff
changeset
|
1694 |
using assms unfolding pdivmod_rel_def |
59557 | 1695 |
by (auto simp add: algebra_simps degree_add_less) |
57482
60459c3853af
add lemmas: polynomial div/mod distribute over addition
huffman
parents:
56544
diff
changeset
|
1696 |
|
60459c3853af
add lemmas: polynomial div/mod distribute over addition
huffman
parents:
56544
diff
changeset
|
1697 |
lemma poly_div_add_left: |
60459c3853af
add lemmas: polynomial div/mod distribute over addition
huffman
parents:
56544
diff
changeset
|
1698 |
fixes x y z :: "'a::field poly" |
60459c3853af
add lemmas: polynomial div/mod distribute over addition
huffman
parents:
56544
diff
changeset
|
1699 |
shows "(x + y) div z = x div z + y div z" |
60459c3853af
add lemmas: polynomial div/mod distribute over addition
huffman
parents:
56544
diff
changeset
|
1700 |
using pdivmod_rel_add_left [OF pdivmod_rel pdivmod_rel] |
60459c3853af
add lemmas: polynomial div/mod distribute over addition
huffman
parents:
56544
diff
changeset
|
1701 |
by (rule div_poly_eq) |
60459c3853af
add lemmas: polynomial div/mod distribute over addition
huffman
parents:
56544
diff
changeset
|
1702 |
|
60459c3853af
add lemmas: polynomial div/mod distribute over addition
huffman
parents:
56544
diff
changeset
|
1703 |
lemma poly_mod_add_left: |
60459c3853af
add lemmas: polynomial div/mod distribute over addition
huffman
parents:
56544
diff
changeset
|
1704 |
fixes x y z :: "'a::field poly" |
60459c3853af
add lemmas: polynomial div/mod distribute over addition
huffman
parents:
56544
diff
changeset
|
1705 |
shows "(x + y) mod z = x mod z + y mod z" |
60459c3853af
add lemmas: polynomial div/mod distribute over addition
huffman
parents:
56544
diff
changeset
|
1706 |
using pdivmod_rel_add_left [OF pdivmod_rel pdivmod_rel] |
60459c3853af
add lemmas: polynomial div/mod distribute over addition
huffman
parents:
56544
diff
changeset
|
1707 |
by (rule mod_poly_eq) |
60459c3853af
add lemmas: polynomial div/mod distribute over addition
huffman
parents:
56544
diff
changeset
|
1708 |
|
60459c3853af
add lemmas: polynomial div/mod distribute over addition
huffman
parents:
56544
diff
changeset
|
1709 |
lemma poly_div_diff_left: |
60459c3853af
add lemmas: polynomial div/mod distribute over addition
huffman
parents:
56544
diff
changeset
|
1710 |
fixes x y z :: "'a::field poly" |
60459c3853af
add lemmas: polynomial div/mod distribute over addition
huffman
parents:
56544
diff
changeset
|
1711 |
shows "(x - y) div z = x div z - y div z" |
60459c3853af
add lemmas: polynomial div/mod distribute over addition
huffman
parents:
56544
diff
changeset
|
1712 |
by (simp only: diff_conv_add_uminus poly_div_add_left poly_div_minus_left) |
60459c3853af
add lemmas: polynomial div/mod distribute over addition
huffman
parents:
56544
diff
changeset
|
1713 |
|
60459c3853af
add lemmas: polynomial div/mod distribute over addition
huffman
parents:
56544
diff
changeset
|
1714 |
lemma poly_mod_diff_left: |
60459c3853af
add lemmas: polynomial div/mod distribute over addition
huffman
parents:
56544
diff
changeset
|
1715 |
fixes x y z :: "'a::field poly" |
60459c3853af
add lemmas: polynomial div/mod distribute over addition
huffman
parents:
56544
diff
changeset
|
1716 |
shows "(x - y) mod z = x mod z - y mod z" |
60459c3853af
add lemmas: polynomial div/mod distribute over addition
huffman
parents:
56544
diff
changeset
|
1717 |
by (simp only: diff_conv_add_uminus poly_mod_add_left poly_mod_minus_left) |
60459c3853af
add lemmas: polynomial div/mod distribute over addition
huffman
parents:
56544
diff
changeset
|
1718 |
|
29659 | 1719 |
lemma pdivmod_rel_smult_right: |
1720 |
"\<lbrakk>a \<noteq> 0; pdivmod_rel x y q r\<rbrakk> |
|
1721 |
\<Longrightarrow> pdivmod_rel x (smult a y) (smult (inverse a) q) r" |
|
1722 |
unfolding pdivmod_rel_def by simp |
|
1723 |
||
1724 |
lemma div_smult_right: |
|
1725 |
"a \<noteq> 0 \<Longrightarrow> x div (smult a y) = smult (inverse a) (x div y)" |
|
1726 |
by (rule div_poly_eq, erule pdivmod_rel_smult_right, rule pdivmod_rel) |
|
1727 |
||
1728 |
lemma mod_smult_right: "a \<noteq> 0 \<Longrightarrow> x mod (smult a y) = x mod y" |
|
1729 |
by (rule mod_poly_eq, erule pdivmod_rel_smult_right, rule pdivmod_rel) |
|
1730 |
||
30072 | 1731 |
lemma poly_div_minus_right [simp]: |
1732 |
fixes x y :: "'a::field poly" |
|
1733 |
shows "x div (- y) = - (x div y)" |
|
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
1734 |
using div_smult_right [of "- 1::'a"] by (simp add: nonzero_inverse_minus_eq) |
30072 | 1735 |
|
1736 |
lemma poly_mod_minus_right [simp]: |
|
1737 |
fixes x y :: "'a::field poly" |
|
1738 |
shows "x mod (- y) = x mod y" |
|
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54230
diff
changeset
|
1739 |
using mod_smult_right [of "- 1::'a"] by simp |
30072 | 1740 |
|
29660 | 1741 |
lemma pdivmod_rel_mult: |
1742 |
"\<lbrakk>pdivmod_rel x y q r; pdivmod_rel q z q' r'\<rbrakk> |
|
1743 |
\<Longrightarrow> pdivmod_rel x (y * z) q' (y * r' + r)" |
|
1744 |
apply (cases "z = 0", simp add: pdivmod_rel_def) |
|
1745 |
apply (cases "y = 0", simp add: pdivmod_rel_by_0_iff pdivmod_rel_0_iff) |
|
1746 |
apply (cases "r = 0") |
|
1747 |
apply (cases "r' = 0") |
|
1748 |
apply (simp add: pdivmod_rel_def) |
|
36350 | 1749 |
apply (simp add: pdivmod_rel_def field_simps degree_mult_eq) |
29660 | 1750 |
apply (cases "r' = 0") |
1751 |
apply (simp add: pdivmod_rel_def degree_mult_eq) |
|
36350 | 1752 |
apply (simp add: pdivmod_rel_def field_simps) |
29660 | 1753 |
apply (simp add: degree_mult_eq degree_add_less) |
1754 |
done |
|
1755 |
||
1756 |
lemma poly_div_mult_right: |
|
1757 |
fixes x y z :: "'a::field poly" |
|
1758 |
shows "x div (y * z) = (x div y) div z" |
|
1759 |
by (rule div_poly_eq, rule pdivmod_rel_mult, (rule pdivmod_rel)+) |
|
1760 |
||
1761 |
lemma poly_mod_mult_right: |
|
1762 |
fixes x y z :: "'a::field poly" |
|
1763 |
shows "x mod (y * z) = y * (x div y mod z) + x mod y" |
|
1764 |
by (rule mod_poly_eq, rule pdivmod_rel_mult, (rule pdivmod_rel)+) |
|
1765 |
||
29451 | 1766 |
lemma mod_pCons: |
1767 |
fixes a and x |
|
1768 |
assumes y: "y \<noteq> 0" |
|
1769 |
defines b: "b \<equiv> coeff (pCons a (x mod y)) (degree y) / coeff y (degree y)" |
|
1770 |
shows "(pCons a x) mod y = (pCons a (x mod y) - smult b y)" |
|
1771 |
unfolding b |
|
1772 |
apply (rule mod_poly_eq) |
|
29537 | 1773 |
apply (rule pdivmod_rel_pCons [OF pdivmod_rel y refl]) |
29451 | 1774 |
done |
1775 |
||
52380 | 1776 |
definition pdivmod :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<times> 'a poly" |
1777 |
where |
|
1778 |
"pdivmod p q = (p div q, p mod q)" |
|
31663 | 1779 |
|
52380 | 1780 |
lemma div_poly_code [code]: |
1781 |
"p div q = fst (pdivmod p q)" |
|
1782 |
by (simp add: pdivmod_def) |
|
31663 | 1783 |
|
52380 | 1784 |
lemma mod_poly_code [code]: |
1785 |
"p mod q = snd (pdivmod p q)" |
|
1786 |
by (simp add: pdivmod_def) |
|
31663 | 1787 |
|
52380 | 1788 |
lemma pdivmod_0: |
1789 |
"pdivmod 0 q = (0, 0)" |
|
1790 |
by (simp add: pdivmod_def) |
|
31663 | 1791 |
|
52380 | 1792 |
lemma pdivmod_pCons: |
1793 |
"pdivmod (pCons a p) q = |
|
1794 |
(if q = 0 then (0, pCons a p) else |
|
1795 |
(let (s, r) = pdivmod p q; |
|
1796 |
b = coeff (pCons a r) (degree q) / coeff q (degree q) |
|
1797 |
in (pCons b s, pCons a r - smult b q)))" |
|
1798 |
apply (simp add: pdivmod_def Let_def, safe) |
|
1799 |
apply (rule div_poly_eq) |
|
1800 |
apply (erule pdivmod_rel_pCons [OF pdivmod_rel _ refl]) |
|
1801 |
apply (rule mod_poly_eq) |
|
1802 |
apply (erule pdivmod_rel_pCons [OF pdivmod_rel _ refl]) |
|
29451 | 1803 |
done |
1804 |
||
52380 | 1805 |
lemma pdivmod_fold_coeffs [code]: |
1806 |
"pdivmod p q = (if q = 0 then (0, p) |
|
1807 |
else fold_coeffs (\<lambda>a (s, r). |
|
1808 |
let b = coeff (pCons a r) (degree q) / coeff q (degree q) |
|
1809 |
in (pCons b s, pCons a r - smult b q) |
|
1810 |
) p (0, 0))" |
|
1811 |
apply (cases "q = 0") |
|
1812 |
apply (simp add: pdivmod_def) |
|
1813 |
apply (rule sym) |
|
1814 |
apply (induct p) |
|
1815 |
apply (simp_all add: pdivmod_0 pdivmod_pCons) |
|
1816 |
apply (case_tac "a = 0 \<and> p = 0") |
|
1817 |
apply (auto simp add: pdivmod_def) |
|
1818 |
done |
|
29980 | 1819 |
|
1820 |
||
60500 | 1821 |
subsection \<open>Order of polynomial roots\<close> |
29977
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1822 |
|
52380 | 1823 |
definition order :: "'a::idom \<Rightarrow> 'a poly \<Rightarrow> nat" |
29977
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1824 |
where |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1825 |
"order a p = (LEAST n. \<not> [:-a, 1:] ^ Suc n dvd p)" |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1826 |
|
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1827 |
lemma coeff_linear_power: |
29979 | 1828 |
fixes a :: "'a::comm_semiring_1" |
29977
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1829 |
shows "coeff ([:a, 1:] ^ n) n = 1" |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1830 |
apply (induct n, simp_all) |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1831 |
apply (subst coeff_eq_0) |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1832 |
apply (auto intro: le_less_trans degree_power_le) |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1833 |
done |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1834 |
|
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1835 |
lemma degree_linear_power: |
29979 | 1836 |
fixes a :: "'a::comm_semiring_1" |
29977
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1837 |
shows "degree ([:a, 1:] ^ n) = n" |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1838 |
apply (rule order_antisym) |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1839 |
apply (rule ord_le_eq_trans [OF degree_power_le], simp) |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1840 |
apply (rule le_degree, simp add: coeff_linear_power) |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1841 |
done |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1842 |
|
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1843 |
lemma order_1: "[:-a, 1:] ^ order a p dvd p" |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1844 |
apply (cases "p = 0", simp) |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1845 |
apply (cases "order a p", simp) |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1846 |
apply (subgoal_tac "nat < (LEAST n. \<not> [:-a, 1:] ^ Suc n dvd p)") |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1847 |
apply (drule not_less_Least, simp) |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1848 |
apply (fold order_def, simp) |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1849 |
done |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1850 |
|
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1851 |
lemma order_2: "p \<noteq> 0 \<Longrightarrow> \<not> [:-a, 1:] ^ Suc (order a p) dvd p" |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1852 |
unfolding order_def |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1853 |
apply (rule LeastI_ex) |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1854 |
apply (rule_tac x="degree p" in exI) |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1855 |
apply (rule notI) |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1856 |
apply (drule (1) dvd_imp_degree_le) |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1857 |
apply (simp only: degree_linear_power) |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1858 |
done |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1859 |
|
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1860 |
lemma order: |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1861 |
"p \<noteq> 0 \<Longrightarrow> [:-a, 1:] ^ order a p dvd p \<and> \<not> [:-a, 1:] ^ Suc (order a p) dvd p" |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1862 |
by (rule conjI [OF order_1 order_2]) |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1863 |
|
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1864 |
lemma order_degree: |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1865 |
assumes p: "p \<noteq> 0" |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1866 |
shows "order a p \<le> degree p" |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1867 |
proof - |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1868 |
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
|
1869 |
by (simp only: degree_linear_power) |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1870 |
also have "\<dots> \<le> degree p" |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1871 |
using order_1 p by (rule dvd_imp_degree_le) |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1872 |
finally show ?thesis . |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1873 |
qed |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1874 |
|
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1875 |
lemma order_root: "poly p a = 0 \<longleftrightarrow> p = 0 \<or> order a p \<noteq> 0" |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1876 |
apply (cases "p = 0", simp_all) |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1877 |
apply (rule iffI) |
56383 | 1878 |
apply (metis order_2 not_gr0 poly_eq_0_iff_dvd power_0 power_Suc_0 power_one_right) |
1879 |
unfolding poly_eq_0_iff_dvd |
|
1880 |
apply (metis dvd_power dvd_trans order_1) |
|
29977
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1881 |
done |
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1882 |
|
62065 | 1883 |
lemma order_0I: "poly p a \<noteq> 0 \<Longrightarrow> order a p = 0" |
1884 |
by (subst (asm) order_root) auto |
|
1885 |
||
29977
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents:
29904
diff
changeset
|
1886 |
|
60500 | 1887 |
subsection \<open>GCD of polynomials\<close> |
29478 | 1888 |
|
52380 | 1889 |
instantiation poly :: (field) gcd |
29478 | 1890 |
begin |
1891 |
||
52380 | 1892 |
function gcd_poly :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" |
1893 |
where |
|
1894 |
"gcd (x::'a poly) 0 = smult (inverse (coeff x (degree x))) x" |
|
1895 |
| "y \<noteq> 0 \<Longrightarrow> gcd (x::'a poly) y = gcd y (x mod y)" |
|
1896 |
by auto |
|
29478 | 1897 |
|
52380 | 1898 |
termination "gcd :: _ poly \<Rightarrow> _" |
1899 |
by (relation "measure (\<lambda>(x, y). if y = 0 then 0 else Suc (degree y))") |
|
1900 |
(auto dest: degree_mod_less) |
|
1901 |
||
1902 |
declare gcd_poly.simps [simp del] |
|
1903 |
||
58513 | 1904 |
definition lcm_poly :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" |
1905 |
where |
|
1906 |
"lcm_poly a b = a * b div smult (coeff a (degree a) * coeff b (degree b)) (gcd a b)" |
|
1907 |
||
52380 | 1908 |
instance .. |
29478 | 1909 |
|
29451 | 1910 |
end |
29478 | 1911 |
|
52380 | 1912 |
lemma |
1913 |
fixes x y :: "_ poly" |
|
1914 |
shows poly_gcd_dvd1 [iff]: "gcd x y dvd x" |
|
1915 |
and poly_gcd_dvd2 [iff]: "gcd x y dvd y" |
|
1916 |
apply (induct x y rule: gcd_poly.induct) |
|
1917 |
apply (simp_all add: gcd_poly.simps) |
|
1918 |
apply (fastforce simp add: smult_dvd_iff dest: inverse_zero_imp_zero) |
|
1919 |
apply (blast dest: dvd_mod_imp_dvd) |
|
1920 |
done |
|
38857
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
haftmann
parents:
37770
diff
changeset
|
1921 |
|
52380 | 1922 |
lemma poly_gcd_greatest: |
1923 |
fixes k x y :: "_ poly" |
|
1924 |
shows "k dvd x \<Longrightarrow> k dvd y \<Longrightarrow> k dvd gcd x y" |
|
1925 |
by (induct x y rule: gcd_poly.induct) |
|
1926 |
(simp_all add: gcd_poly.simps dvd_mod dvd_smult) |
|
29478 | 1927 |
|
52380 | 1928 |
lemma dvd_poly_gcd_iff [iff]: |
1929 |
fixes k x y :: "_ poly" |
|
1930 |
shows "k dvd gcd x y \<longleftrightarrow> k dvd x \<and> k dvd y" |
|
60686 | 1931 |
by (auto intro!: poly_gcd_greatest intro: dvd_trans [of _ "gcd x y"]) |
29478 | 1932 |
|
52380 | 1933 |
lemma poly_gcd_monic: |
1934 |
fixes x y :: "_ poly" |
|
1935 |
shows "coeff (gcd x y) (degree (gcd x y)) = |
|
1936 |
(if x = 0 \<and> y = 0 then 0 else 1)" |
|
1937 |
by (induct x y rule: gcd_poly.induct) |
|
1938 |
(simp_all add: gcd_poly.simps nonzero_imp_inverse_nonzero) |
|
29478 | 1939 |
|
52380 | 1940 |
lemma poly_gcd_zero_iff [simp]: |
1941 |
fixes x y :: "_ poly" |
|
1942 |
shows "gcd x y = 0 \<longleftrightarrow> x = 0 \<and> y = 0" |
|
1943 |
by (simp only: dvd_0_left_iff [symmetric] dvd_poly_gcd_iff) |
|
29478 | 1944 |
|
52380 | 1945 |
lemma poly_gcd_0_0 [simp]: |
1946 |
"gcd (0::_ poly) 0 = 0" |
|
1947 |
by simp |
|
29478 | 1948 |
|
52380 | 1949 |
lemma poly_dvd_antisym: |
1950 |
fixes p q :: "'a::idom poly" |
|
1951 |
assumes coeff: "coeff p (degree p) = coeff q (degree q)" |
|
1952 |
assumes dvd1: "p dvd q" and dvd2: "q dvd p" shows "p = q" |
|
1953 |
proof (cases "p = 0") |
|
1954 |
case True with coeff show "p = q" by simp |
|
1955 |
next |
|
1956 |
case False with coeff have "q \<noteq> 0" by auto |
|
1957 |
have degree: "degree p = degree q" |
|
60500 | 1958 |
using \<open>p dvd q\<close> \<open>q dvd p\<close> \<open>p \<noteq> 0\<close> \<open>q \<noteq> 0\<close> |
52380 | 1959 |
by (intro order_antisym dvd_imp_degree_le) |
29478 | 1960 |
|
60500 | 1961 |
from \<open>p dvd q\<close> obtain a where a: "q = p * a" .. |
1962 |
with \<open>q \<noteq> 0\<close> have "a \<noteq> 0" by auto |
|
1963 |
with degree a \<open>p \<noteq> 0\<close> have "degree a = 0" |
|
52380 | 1964 |
by (simp add: degree_mult_eq) |
1965 |
with coeff a show "p = q" |
|
1966 |
by (cases a, auto split: if_splits) |
|
1967 |
qed |
|
29478 | 1968 |
|
52380 | 1969 |
lemma poly_gcd_unique: |
1970 |
fixes d x y :: "_ poly" |
|
1971 |
assumes dvd1: "d dvd x" and dvd2: "d dvd y" |
|
1972 |
and greatest: "\<And>k. k dvd x \<Longrightarrow> k dvd y \<Longrightarrow> k dvd d" |
|
1973 |
and monic: "coeff d (degree d) = (if x = 0 \<and> y = 0 then 0 else 1)" |
|
1974 |
shows "gcd x y = d" |
|
1975 |
proof - |
|
1976 |
have "coeff (gcd x y) (degree (gcd x y)) = coeff d (degree d)" |
|
1977 |
by (simp_all add: poly_gcd_monic monic) |
|
1978 |
moreover have "gcd x y dvd d" |
|
1979 |
using poly_gcd_dvd1 poly_gcd_dvd2 by (rule greatest) |
|
1980 |
moreover have "d dvd gcd x y" |
|
1981 |
using dvd1 dvd2 by (rule poly_gcd_greatest) |
|
1982 |
ultimately show ?thesis |
|
1983 |
by (rule poly_dvd_antisym) |
|
1984 |
qed |
|
29478 | 1985 |
|
61605 | 1986 |
interpretation gcd_poly: abel_semigroup "gcd :: _ poly \<Rightarrow> _" |
52380 | 1987 |
proof |
1988 |
fix x y z :: "'a poly" |
|
1989 |
show "gcd (gcd x y) z = gcd x (gcd y z)" |
|
1990 |
by (rule poly_gcd_unique) (auto intro: dvd_trans simp add: poly_gcd_monic) |
|
1991 |
show "gcd x y = gcd y x" |
|
1992 |
by (rule poly_gcd_unique) (simp_all add: poly_gcd_monic) |
|
1993 |
qed |
|
29478 | 1994 |
|
52380 | 1995 |
lemmas poly_gcd_assoc = gcd_poly.assoc |
1996 |
lemmas poly_gcd_commute = gcd_poly.commute |
|
1997 |
lemmas poly_gcd_left_commute = gcd_poly.left_commute |
|
29478 | 1998 |
|
52380 | 1999 |
lemmas poly_gcd_ac = poly_gcd_assoc poly_gcd_commute poly_gcd_left_commute |
2000 |
||
2001 |
lemma poly_gcd_1_left [simp]: "gcd 1 y = (1 :: _ poly)" |
|
2002 |
by (rule poly_gcd_unique) simp_all |
|
29478 | 2003 |
|
52380 | 2004 |
lemma poly_gcd_1_right [simp]: "gcd x 1 = (1 :: _ poly)" |
2005 |
by (rule poly_gcd_unique) simp_all |
|
2006 |
||
2007 |
lemma poly_gcd_minus_left [simp]: "gcd (- x) y = gcd x (y :: _ poly)" |
|
2008 |
by (rule poly_gcd_unique) (simp_all add: poly_gcd_monic) |
|
29478 | 2009 |
|
52380 | 2010 |
lemma poly_gcd_minus_right [simp]: "gcd x (- y) = gcd x (y :: _ poly)" |
2011 |
by (rule poly_gcd_unique) (simp_all add: poly_gcd_monic) |
|
29478 | 2012 |
|
52380 | 2013 |
lemma poly_gcd_code [code]: |
2014 |
"gcd x y = (if y = 0 then smult (inverse (coeff x (degree x))) x else gcd y (x mod (y :: _ poly)))" |
|
2015 |
by (simp add: gcd_poly.simps) |
|
2016 |
||
2017 |
||
62065 | 2018 |
subsection \<open>Additional induction rules on polynomials\<close> |
2019 |
||
2020 |
text \<open> |
|
2021 |
An induction rule for induction over the roots of a polynomial with a certain property. |
|
2022 |
(e.g. all positive roots) |
|
2023 |
\<close> |
|
2024 |
lemma poly_root_induct [case_names 0 no_roots root]: |
|
2025 |
fixes p :: "'a :: idom poly" |
|
2026 |
assumes "Q 0" |
|
2027 |
assumes "\<And>p. (\<And>a. P a \<Longrightarrow> poly p a \<noteq> 0) \<Longrightarrow> Q p" |
|
2028 |
assumes "\<And>a p. P a \<Longrightarrow> Q p \<Longrightarrow> Q ([:a, -1:] * p)" |
|
2029 |
shows "Q p" |
|
2030 |
proof (induction "degree p" arbitrary: p rule: less_induct) |
|
2031 |
case (less p) |
|
2032 |
show ?case |
|
2033 |
proof (cases "p = 0") |
|
2034 |
assume nz: "p \<noteq> 0" |
|
2035 |
show ?case |
|
2036 |
proof (cases "\<exists>a. P a \<and> poly p a = 0") |
|
2037 |
case False |
|
2038 |
thus ?thesis by (intro assms(2)) blast |
|
2039 |
next |
|
2040 |
case True |
|
2041 |
then obtain a where a: "P a" "poly p a = 0" |
|
2042 |
by blast |
|
2043 |
hence "-[:-a, 1:] dvd p" |
|
2044 |
by (subst minus_dvd_iff) (simp add: poly_eq_0_iff_dvd) |
|
2045 |
then obtain q where q: "p = [:a, -1:] * q" by (elim dvdE) simp |
|
2046 |
with nz have q_nz: "q \<noteq> 0" by auto |
|
2047 |
have "degree p = Suc (degree q)" |
|
2048 |
by (subst q, subst degree_mult_eq) (simp_all add: q_nz) |
|
2049 |
hence "Q q" by (intro less) simp |
|
2050 |
from a(1) and this have "Q ([:a, -1:] * q)" |
|
2051 |
by (rule assms(3)) |
|
2052 |
with q show ?thesis by simp |
|
2053 |
qed |
|
2054 |
qed (simp add: assms(1)) |
|
2055 |
qed |
|
2056 |
||
2057 |
lemma dropWhile_replicate_append: |
|
2058 |
"dropWhile (op= a) (replicate n a @ ys) = dropWhile (op= a) ys" |
|
2059 |
by (induction n) simp_all |
|
2060 |
||
2061 |
lemma Poly_append_replicate_0: "Poly (xs @ replicate n 0) = Poly xs" |
|
2062 |
by (subst coeffs_eq_iff) (simp_all add: strip_while_def dropWhile_replicate_append) |
|
2063 |
||
2064 |
text \<open> |
|
2065 |
An induction rule for simultaneous induction over two polynomials, |
|
2066 |
prepending one coefficient in each step. |
|
2067 |
\<close> |
|
2068 |
lemma poly_induct2 [case_names 0 pCons]: |
|
2069 |
assumes "P 0 0" "\<And>a p b q. P p q \<Longrightarrow> P (pCons a p) (pCons b q)" |
|
2070 |
shows "P p q" |
|
2071 |
proof - |
|
2072 |
def n \<equiv> "max (length (coeffs p)) (length (coeffs q))" |
|
2073 |
def xs \<equiv> "coeffs p @ (replicate (n - length (coeffs p)) 0)" |
|
2074 |
def ys \<equiv> "coeffs q @ (replicate (n - length (coeffs q)) 0)" |
|
2075 |
have "length xs = length ys" |
|
2076 |
by (simp add: xs_def ys_def n_def) |
|
2077 |
hence "P (Poly xs) (Poly ys)" |
|
2078 |
by (induction rule: list_induct2) (simp_all add: assms) |
|
2079 |
also have "Poly xs = p" |
|
2080 |
by (simp add: xs_def Poly_append_replicate_0) |
|
2081 |
also have "Poly ys = q" |
|
2082 |
by (simp add: ys_def Poly_append_replicate_0) |
|
2083 |
finally show ?thesis . |
|
2084 |
qed |
|
2085 |
||
2086 |
||
60500 | 2087 |
subsection \<open>Composition of polynomials\<close> |
29478 | 2088 |
|
62128
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
2089 |
(* 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
|
2090 |
|
52380 | 2091 |
definition pcompose :: "'a::comm_semiring_0 poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" |
2092 |
where |
|
2093 |
"pcompose p q = fold_coeffs (\<lambda>a c. [:a:] + q * c) p 0" |
|
2094 |
||
62128
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
2095 |
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
|
2096 |
|
52380 | 2097 |
lemma pcompose_0 [simp]: |
2098 |
"pcompose 0 q = 0" |
|
2099 |
by (simp add: pcompose_def) |
|
62128
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
2100 |
|
52380 | 2101 |
lemma pcompose_pCons: |
2102 |
"pcompose (pCons a p) q = [:a:] + q * pcompose p q" |
|
2103 |
by (cases "p = 0 \<and> a = 0") (auto simp add: pcompose_def) |
|
2104 |
||
62128
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
2105 |
lemma pcompose_1: |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
2106 |
fixes p :: "'a :: comm_semiring_1 poly" |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
2107 |
shows "pcompose 1 p = 1" |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
2108 |
unfolding one_poly_def by (auto simp: pcompose_pCons) |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
2109 |
|
52380 | 2110 |
lemma poly_pcompose: |
2111 |
"poly (pcompose p q) x = poly p (poly q x)" |
|
2112 |
by (induct p) (simp_all add: pcompose_pCons) |
|
2113 |
||
2114 |
lemma degree_pcompose_le: |
|
2115 |
"degree (pcompose p q) \<le> degree p * degree q" |
|
2116 |
apply (induct p, simp) |
|
2117 |
apply (simp add: pcompose_pCons, clarify) |
|
2118 |
apply (rule degree_add_le, simp) |
|
2119 |
apply (rule order_trans [OF degree_mult_le], simp) |
|
29478 | 2120 |
done |
2121 |
||
62065 | 2122 |
lemma pcompose_add: |
2123 |
fixes p q r :: "'a :: {comm_semiring_0, ab_semigroup_add} poly" |
|
2124 |
shows "pcompose (p + q) r = pcompose p r + pcompose q r" |
|
2125 |
proof (induction p q rule: poly_induct2) |
|
2126 |
case (pCons a p b q) |
|
2127 |
have "pcompose (pCons a p + pCons b q) r = |
|
2128 |
[:a + b:] + r * pcompose p r + r * pcompose q r" |
|
2129 |
by (simp_all add: pcompose_pCons pCons.IH algebra_simps) |
|
2130 |
also have "[:a + b:] = [:a:] + [:b:]" by simp |
|
2131 |
also have "\<dots> + r * pcompose p r + r * pcompose q r = |
|
2132 |
pcompose (pCons a p) r + pcompose (pCons b q) r" |
|
2133 |
by (simp only: pcompose_pCons add_ac) |
|
2134 |
finally show ?case . |
|
2135 |
qed simp |
|
2136 |
||
62128
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
2137 |
lemma pcompose_uminus: |
62065 | 2138 |
fixes p r :: "'a :: comm_ring poly" |
2139 |
shows "pcompose (-p) r = -pcompose p r" |
|
2140 |
by (induction p) (simp_all add: pcompose_pCons) |
|
2141 |
||
2142 |
lemma pcompose_diff: |
|
2143 |
fixes p q r :: "'a :: comm_ring poly" |
|
2144 |
shows "pcompose (p - q) r = pcompose p r - pcompose q r" |
|
62128
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
2145 |
using pcompose_add[of p "-q"] by (simp add: pcompose_uminus) |
62065 | 2146 |
|
2147 |
lemma pcompose_smult: |
|
2148 |
fixes p r :: "'a :: comm_semiring_0 poly" |
|
2149 |
shows "pcompose (smult a p) r = smult a (pcompose p r)" |
|
2150 |
by (induction p) |
|
2151 |
(simp_all add: pcompose_pCons pcompose_add smult_add_right) |
|
2152 |
||
2153 |
lemma pcompose_mult: |
|
2154 |
fixes p q r :: "'a :: comm_semiring_0 poly" |
|
2155 |
shows "pcompose (p * q) r = pcompose p r * pcompose q r" |
|
2156 |
by (induction p arbitrary: q) |
|
2157 |
(simp_all add: pcompose_add pcompose_smult pcompose_pCons algebra_simps) |
|
2158 |
||
2159 |
lemma pcompose_assoc: |
|
2160 |
"pcompose p (pcompose q r :: 'a :: comm_semiring_0 poly ) = |
|
2161 |
pcompose (pcompose p q) r" |
|
2162 |
by (induction p arbitrary: q) |
|
2163 |
(simp_all add: pcompose_pCons pcompose_add pcompose_mult) |
|
2164 |
||
62128
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
2165 |
lemma pcompose_idR[simp]: |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
2166 |
fixes p :: "'a :: comm_semiring_1 poly" |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
2167 |
shows "pcompose p [: 0, 1 :] = p" |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
2168 |
by (induct p; simp add: pcompose_pCons) |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
2169 |
|
62065 | 2170 |
|
2171 |
(* The remainder of this section and the next were contributed by Wenda Li *) |
|
2172 |
||
2173 |
lemma degree_mult_eq_0: |
|
62128
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
2174 |
fixes p q:: "'a :: semidom poly" |
62065 | 2175 |
shows "degree (p*q) = 0 \<longleftrightarrow> p=0 \<or> q=0 \<or> (p\<noteq>0 \<and> q\<noteq>0 \<and> degree p =0 \<and> degree q =0)" |
2176 |
by (auto simp add:degree_mult_eq) |
|
2177 |
||
2178 |
lemma pcompose_const[simp]:"pcompose [:a:] q = [:a:]" by (subst pcompose_pCons,simp) |
|
2179 |
||
62128
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
2180 |
lemma pcompose_0': "pcompose p 0 = [:coeff p 0:]" |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
2181 |
by (induct p) (auto simp add:pcompose_pCons) |
62065 | 2182 |
|
2183 |
lemma degree_pcompose: |
|
62128
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
2184 |
fixes p q:: "'a::semidom poly" |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
2185 |
shows "degree (pcompose p q) = degree p * degree q" |
62065 | 2186 |
proof (induct p) |
2187 |
case 0 |
|
2188 |
thus ?case by auto |
|
2189 |
next |
|
2190 |
case (pCons a p) |
|
2191 |
have "degree (q * pcompose p q) = 0 \<Longrightarrow> ?case" |
|
2192 |
proof (cases "p=0") |
|
2193 |
case True |
|
2194 |
thus ?thesis by auto |
|
2195 |
next |
|
2196 |
case False assume "degree (q * pcompose p q) = 0" |
|
62128
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
2197 |
hence "degree q=0 \<or> pcompose p q=0" by (auto simp add: degree_mult_eq_0) |
62072 | 2198 |
moreover have "\<lbrakk>pcompose p q=0;degree q\<noteq>0\<rbrakk> \<Longrightarrow> False" using pCons.hyps(2) \<open>p\<noteq>0\<close> |
62065 | 2199 |
proof - |
2200 |
assume "pcompose p q=0" "degree q\<noteq>0" |
|
2201 |
hence "degree p=0" using pCons.hyps(2) by auto |
|
2202 |
then obtain a1 where "p=[:a1:]" |
|
2203 |
by (metis degree_pCons_eq_if old.nat.distinct(2) pCons_cases) |
|
62072 | 2204 |
thus False using \<open>pcompose p q=0\<close> \<open>p\<noteq>0\<close> by auto |
62065 | 2205 |
qed |
2206 |
ultimately have "degree (pCons a p) * degree q=0" by auto |
|
2207 |
moreover have "degree (pcompose (pCons a p) q) = 0" |
|
2208 |
proof - |
|
2209 |
have" 0 = max (degree [:a:]) (degree (q*pcompose p q))" |
|
62072 | 2210 |
using \<open>degree (q * pcompose p q) = 0\<close> by simp |
62065 | 2211 |
also have "... \<ge> degree ([:a:] + q * pcompose p q)" |
2212 |
by (rule degree_add_le_max) |
|
2213 |
finally show ?thesis by (auto simp add:pcompose_pCons) |
|
2214 |
qed |
|
2215 |
ultimately show ?thesis by simp |
|
2216 |
qed |
|
2217 |
moreover have "degree (q * pcompose p q)>0 \<Longrightarrow> ?case" |
|
2218 |
proof - |
|
2219 |
assume asm:"0 < degree (q * pcompose p q)" |
|
2220 |
hence "p\<noteq>0" "q\<noteq>0" "pcompose p q\<noteq>0" by auto |
|
2221 |
have "degree (pcompose (pCons a p) q) = degree ( q * pcompose p q)" |
|
2222 |
unfolding pcompose_pCons |
|
2223 |
using degree_add_eq_right[of "[:a:]" ] asm by auto |
|
2224 |
thus ?thesis |
|
62072 | 2225 |
using pCons.hyps(2) degree_mult_eq[OF \<open>q\<noteq>0\<close> \<open>pcompose p q\<noteq>0\<close>] by auto |
62065 | 2226 |
qed |
2227 |
ultimately show ?case by blast |
|
2228 |
qed |
|
2229 |
||
2230 |
lemma pcompose_eq_0: |
|
62128
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
2231 |
fixes p q:: "'a :: semidom poly" |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
2232 |
assumes "pcompose p q = 0" "degree q > 0" |
3201ddb00097
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents:
62072
diff
changeset
|
2233 |
shows "p = 0" |
62065 | 2234 |
proof - |
2235 |
have "degree p=0" using assms degree_pcompose[of p q] by auto |
|
2236 |
then obtain a where "p=[:a:]" |
|
2237 |
by (metis degree_pCons_eq_if gr0_conv_Suc neq0_conv pCons_cases) |
|
2238 |
hence "a=0" using assms(1) by auto |
|
62072 | 2239 |
thus ?thesis using \<open>p=[:a:]\<close> by simp |
62065 | 2240 |
qed |
2241 |
||
2242 |
||
62072 | 2243 |
subsection \<open>Leading coefficient\<close> |
62065 | 2244 |
|
2245 |
definition lead_coeff:: "'a::zero poly \<Rightarrow> 'a" where |
|
2246 |
"lead_coeff p= coeff p (degree p)" |
|
2247 |
||
2248 |
lemma lead_coeff_pCons[simp]: |
|
2249 |
"p\<noteq>0 \<Longrightarrow>lead_coeff (pCons a p) = lead_coeff p" |
|
2250 |
"p=0 \<Longrightarrow> lead_coeff (pCons a p) = a" |
|
2251 |
unfolding lead_coeff_def by auto |
|
2252 |
||
2253 |
lemma lead_coeff_0[simp]:"lead_coeff 0 =0" |
|
2254 |
unfolding lead_coeff_def by auto |
|
2255 |
||
2256 |
lemma lead_coeff_mult: |
|
2257 |
fixes p q::"'a ::idom poly" |
|
2258 |
shows "lead_coeff (p * q) = lead_coeff p * lead_coeff q" |
|
2259 |
by (unfold lead_coeff_def,cases "p=0 \<or> q=0",auto simp add:coeff_mult_degree_sum degree_mult_eq) |
|
2260 |
||
2261 |
lemma lead_coeff_add_le: |
|
2262 |
assumes "degree p < degree q" |
|
2263 |
shows "lead_coeff (p+q) = lead_coeff q" |
|
2264 |
using assms unfolding lead_coeff_def |
|
2265 |
by (metis coeff_add coeff_eq_0 monoid_add_class.add.left_neutral degree_add_eq_right) |
|
2266 |
||
2267 |
lemma lead_coeff_minus: |
|
2268 |
"lead_coeff (-p) = - lead_coeff p" |
|
2269 |
by (metis coeff_minus degree_minus lead_coeff_def) |
|
2270 |
||
2271 |
||
2272 |
lemma lead_coeff_comp: |
|
2273 |
fixes p q:: "'a::idom poly" |
|
2274 |
assumes "degree q > 0" |
|
2275 |
shows "lead_coeff (pcompose p q) = lead_coeff p * lead_coeff q ^ (degree p)" |
|
2276 |
proof (induct p) |
|
2277 |
case 0 |
|
2278 |
thus ?case unfolding lead_coeff_def by auto |
|
2279 |
next |
|
2280 |
case (pCons a p) |
|
2281 |
have "degree ( q * pcompose p q) = 0 \<Longrightarrow> ?case" |
|
2282 |
proof - |
|
2283 |
assume "degree ( q * pcompose p q) = 0" |
|
2284 |
hence "pcompose p q = 0" by (metis assms degree_0 degree_mult_eq_0 neq0_conv) |
|
62072 | 2285 |
hence "p=0" using pcompose_eq_0[OF _ \<open>degree q > 0\<close>] by simp |
62065 | 2286 |
thus ?thesis by auto |
2287 |
qed |
|
2288 |
moreover have "degree ( q * pcompose p q) > 0 \<Longrightarrow> ?case" |
|
2289 |
proof - |
|
2290 |
assume "degree ( q * pcompose p q) > 0" |
|
2291 |
hence "lead_coeff (pcompose (pCons a p) q) =lead_coeff ( q * pcompose p q)" |
|
2292 |
by (auto simp add:pcompose_pCons lead_coeff_add_le) |
|
2293 |
also have "... = lead_coeff q * (lead_coeff p * lead_coeff q ^ degree p)" |
|
2294 |
using pCons.hyps(2) lead_coeff_mult[of q "pcompose p q"] by simp |
|
2295 |
also have "... = lead_coeff p * lead_coeff q ^ (degree p + 1)" |
|
2296 |
by auto |
|
2297 |
finally show ?thesis by auto |
|
2298 |
qed |
|
2299 |
ultimately show ?case by blast |
|
2300 |
qed |
|
2301 |
||
2302 |
lemma lead_coeff_smult: |
|
2303 |
"lead_coeff (smult c p :: 'a :: idom poly) = c * lead_coeff p" |
|
2304 |
proof - |
|
2305 |
have "smult c p = [:c:] * p" by simp |
|
2306 |
also have "lead_coeff \<dots> = c * lead_coeff p" |
|
2307 |
by (subst lead_coeff_mult) simp_all |
|
2308 |
finally show ?thesis . |
|
2309 |
qed |
|
2310 |
||
2311 |
lemma lead_coeff_1 [simp]: "lead_coeff 1 = 1" |
|
2312 |
by (simp add: lead_coeff_def) |
|
2313 |
||
2314 |
lemma lead_coeff_of_nat [simp]: |
|
2315 |
"lead_coeff (of_nat n) = (of_nat n :: 'a :: {comm_semiring_1,semiring_char_0})" |
|
2316 |
by (induction n) (simp_all add: lead_coeff_def of_nat_poly) |
|
2317 |
||
2318 |
lemma lead_coeff_numeral [simp]: |
|
2319 |
"lead_coeff (numeral n) = numeral n" |
|
2320 |
unfolding lead_coeff_def |
|
2321 |
by (subst of_nat_numeral [symmetric], subst of_nat_poly) simp |
|
2322 |
||
2323 |
lemma lead_coeff_power: |
|
2324 |
"lead_coeff (p ^ n :: 'a :: idom poly) = lead_coeff p ^ n" |
|
2325 |
by (induction n) (simp_all add: lead_coeff_mult) |
|
2326 |
||
2327 |
lemma lead_coeff_nonzero: "p \<noteq> 0 \<Longrightarrow> lead_coeff p \<noteq> 0" |
|
2328 |
by (simp add: lead_coeff_def) |
|
2329 |
||
2330 |
||
52380 | 2331 |
|
2332 |
no_notation cCons (infixr "##" 65) |
|
31663 | 2333 |
|
29478 | 2334 |
end |