author | huffman |
Tue, 13 Jan 2009 07:40:05 -0800 | |
changeset 29471 | 6a46a13ce1f9 |
parent 29462 | dc97c6188a7a |
child 29472 | a63a2e46cec9 |
permissions | -rw-r--r-- |
29451 | 1 |
(* Title: HOL/Polynomial.thy |
2 |
Author: Brian Huffman |
|
3 |
Based on an earlier development by Clemens Ballarin |
|
4 |
*) |
|
5 |
||
6 |
header {* Univariate Polynomials *} |
|
7 |
||
8 |
theory Polynomial |
|
9 |
imports Plain SetInterval |
|
10 |
begin |
|
11 |
||
12 |
subsection {* Definition of type @{text poly} *} |
|
13 |
||
14 |
typedef (Poly) 'a poly = "{f::nat \<Rightarrow> 'a::zero. \<exists>n. \<forall>i>n. f i = 0}" |
|
15 |
morphisms coeff Abs_poly |
|
16 |
by auto |
|
17 |
||
18 |
lemma expand_poly_eq: "p = q \<longleftrightarrow> (\<forall>n. coeff p n = coeff q n)" |
|
19 |
by (simp add: coeff_inject [symmetric] expand_fun_eq) |
|
20 |
||
21 |
lemma poly_ext: "(\<And>n. coeff p n = coeff q n) \<Longrightarrow> p = q" |
|
22 |
by (simp add: expand_poly_eq) |
|
23 |
||
24 |
||
25 |
subsection {* Degree of a polynomial *} |
|
26 |
||
27 |
definition |
|
28 |
degree :: "'a::zero poly \<Rightarrow> nat" where |
|
29 |
"degree p = (LEAST n. \<forall>i>n. coeff p i = 0)" |
|
30 |
||
31 |
lemma coeff_eq_0: "degree p < n \<Longrightarrow> coeff p n = 0" |
|
32 |
proof - |
|
33 |
have "coeff p \<in> Poly" |
|
34 |
by (rule coeff) |
|
35 |
hence "\<exists>n. \<forall>i>n. coeff p i = 0" |
|
36 |
unfolding Poly_def by simp |
|
37 |
hence "\<forall>i>degree p. coeff p i = 0" |
|
38 |
unfolding degree_def by (rule LeastI_ex) |
|
39 |
moreover assume "degree p < n" |
|
40 |
ultimately show ?thesis by simp |
|
41 |
qed |
|
42 |
||
43 |
lemma le_degree: "coeff p n \<noteq> 0 \<Longrightarrow> n \<le> degree p" |
|
44 |
by (erule contrapos_np, rule coeff_eq_0, simp) |
|
45 |
||
46 |
lemma degree_le: "\<forall>i>n. coeff p i = 0 \<Longrightarrow> degree p \<le> n" |
|
47 |
unfolding degree_def by (erule Least_le) |
|
48 |
||
49 |
lemma less_degree_imp: "n < degree p \<Longrightarrow> \<exists>i>n. coeff p i \<noteq> 0" |
|
50 |
unfolding degree_def by (drule not_less_Least, simp) |
|
51 |
||
52 |
||
53 |
subsection {* The zero polynomial *} |
|
54 |
||
55 |
instantiation poly :: (zero) zero |
|
56 |
begin |
|
57 |
||
58 |
definition |
|
59 |
zero_poly_def: "0 = Abs_poly (\<lambda>n. 0)" |
|
60 |
||
61 |
instance .. |
|
62 |
end |
|
63 |
||
64 |
lemma coeff_0 [simp]: "coeff 0 n = 0" |
|
65 |
unfolding zero_poly_def |
|
66 |
by (simp add: Abs_poly_inverse Poly_def) |
|
67 |
||
68 |
lemma degree_0 [simp]: "degree 0 = 0" |
|
69 |
by (rule order_antisym [OF degree_le le0]) simp |
|
70 |
||
71 |
lemma leading_coeff_neq_0: |
|
72 |
assumes "p \<noteq> 0" shows "coeff p (degree p) \<noteq> 0" |
|
73 |
proof (cases "degree p") |
|
74 |
case 0 |
|
75 |
from `p \<noteq> 0` have "\<exists>n. coeff p n \<noteq> 0" |
|
76 |
by (simp add: expand_poly_eq) |
|
77 |
then obtain n where "coeff p n \<noteq> 0" .. |
|
78 |
hence "n \<le> degree p" by (rule le_degree) |
|
79 |
with `coeff p n \<noteq> 0` and `degree p = 0` |
|
80 |
show "coeff p (degree p) \<noteq> 0" by simp |
|
81 |
next |
|
82 |
case (Suc n) |
|
83 |
from `degree p = Suc n` have "n < degree p" by simp |
|
84 |
hence "\<exists>i>n. coeff p i \<noteq> 0" by (rule less_degree_imp) |
|
85 |
then obtain i where "n < i" and "coeff p i \<noteq> 0" by fast |
|
86 |
from `degree p = Suc n` and `n < i` have "degree p \<le> i" by simp |
|
87 |
also from `coeff p i \<noteq> 0` have "i \<le> degree p" by (rule le_degree) |
|
88 |
finally have "degree p = i" . |
|
89 |
with `coeff p i \<noteq> 0` show "coeff p (degree p) \<noteq> 0" by simp |
|
90 |
qed |
|
91 |
||
92 |
lemma leading_coeff_0_iff [simp]: "coeff p (degree p) = 0 \<longleftrightarrow> p = 0" |
|
93 |
by (cases "p = 0", simp, simp add: leading_coeff_neq_0) |
|
94 |
||
95 |
||
96 |
subsection {* List-style constructor for polynomials *} |
|
97 |
||
98 |
definition |
|
99 |
pCons :: "'a::zero \<Rightarrow> 'a poly \<Rightarrow> 'a poly" |
|
100 |
where |
|
101 |
[code del]: "pCons a p = Abs_poly (nat_case a (coeff p))" |
|
102 |
||
29455 | 103 |
syntax |
104 |
"_poly" :: "args \<Rightarrow> 'a poly" ("[:(_):]") |
|
105 |
||
106 |
translations |
|
107 |
"[:x, xs:]" == "CONST pCons x [:xs:]" |
|
108 |
"[:x:]" == "CONST pCons x 0" |
|
109 |
||
29451 | 110 |
lemma Poly_nat_case: "f \<in> Poly \<Longrightarrow> nat_case a f \<in> Poly" |
111 |
unfolding Poly_def by (auto split: nat.split) |
|
112 |
||
113 |
lemma coeff_pCons: |
|
114 |
"coeff (pCons a p) = nat_case a (coeff p)" |
|
115 |
unfolding pCons_def |
|
116 |
by (simp add: Abs_poly_inverse Poly_nat_case coeff) |
|
117 |
||
118 |
lemma coeff_pCons_0 [simp]: "coeff (pCons a p) 0 = a" |
|
119 |
by (simp add: coeff_pCons) |
|
120 |
||
121 |
lemma coeff_pCons_Suc [simp]: "coeff (pCons a p) (Suc n) = coeff p n" |
|
122 |
by (simp add: coeff_pCons) |
|
123 |
||
124 |
lemma degree_pCons_le: "degree (pCons a p) \<le> Suc (degree p)" |
|
125 |
by (rule degree_le, simp add: coeff_eq_0 coeff_pCons split: nat.split) |
|
126 |
||
127 |
lemma degree_pCons_eq: |
|
128 |
"p \<noteq> 0 \<Longrightarrow> degree (pCons a p) = Suc (degree p)" |
|
129 |
apply (rule order_antisym [OF degree_pCons_le]) |
|
130 |
apply (rule le_degree, simp) |
|
131 |
done |
|
132 |
||
133 |
lemma degree_pCons_0: "degree (pCons a 0) = 0" |
|
134 |
apply (rule order_antisym [OF _ le0]) |
|
135 |
apply (rule degree_le, simp add: coeff_pCons split: nat.split) |
|
136 |
done |
|
137 |
||
29460
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
138 |
lemma degree_pCons_eq_if [simp]: |
29451 | 139 |
"degree (pCons a p) = (if p = 0 then 0 else Suc (degree p))" |
140 |
apply (cases "p = 0", simp_all) |
|
141 |
apply (rule order_antisym [OF _ le0]) |
|
142 |
apply (rule degree_le, simp add: coeff_pCons split: nat.split) |
|
143 |
apply (rule order_antisym [OF degree_pCons_le]) |
|
144 |
apply (rule le_degree, simp) |
|
145 |
done |
|
146 |
||
147 |
lemma pCons_0_0 [simp]: "pCons 0 0 = 0" |
|
148 |
by (rule poly_ext, simp add: coeff_pCons split: nat.split) |
|
149 |
||
150 |
lemma pCons_eq_iff [simp]: |
|
151 |
"pCons a p = pCons b q \<longleftrightarrow> a = b \<and> p = q" |
|
152 |
proof (safe) |
|
153 |
assume "pCons a p = pCons b q" |
|
154 |
then have "coeff (pCons a p) 0 = coeff (pCons b q) 0" by simp |
|
155 |
then show "a = b" by simp |
|
156 |
next |
|
157 |
assume "pCons a p = pCons b q" |
|
158 |
then have "\<forall>n. coeff (pCons a p) (Suc n) = |
|
159 |
coeff (pCons b q) (Suc n)" by simp |
|
160 |
then show "p = q" by (simp add: expand_poly_eq) |
|
161 |
qed |
|
162 |
||
163 |
lemma pCons_eq_0_iff [simp]: "pCons a p = 0 \<longleftrightarrow> a = 0 \<and> p = 0" |
|
164 |
using pCons_eq_iff [of a p 0 0] by simp |
|
165 |
||
166 |
lemma Poly_Suc: "f \<in> Poly \<Longrightarrow> (\<lambda>n. f (Suc n)) \<in> Poly" |
|
167 |
unfolding Poly_def |
|
168 |
by (clarify, rule_tac x=n in exI, simp) |
|
169 |
||
170 |
lemma pCons_cases [cases type: poly]: |
|
171 |
obtains (pCons) a q where "p = pCons a q" |
|
172 |
proof |
|
173 |
show "p = pCons (coeff p 0) (Abs_poly (\<lambda>n. coeff p (Suc n)))" |
|
174 |
by (rule poly_ext) |
|
175 |
(simp add: Abs_poly_inverse Poly_Suc coeff coeff_pCons |
|
176 |
split: nat.split) |
|
177 |
qed |
|
178 |
||
179 |
lemma pCons_induct [case_names 0 pCons, induct type: poly]: |
|
180 |
assumes zero: "P 0" |
|
181 |
assumes pCons: "\<And>a p. P p \<Longrightarrow> P (pCons a p)" |
|
182 |
shows "P p" |
|
183 |
proof (induct p rule: measure_induct_rule [where f=degree]) |
|
184 |
case (less p) |
|
185 |
obtain a q where "p = pCons a q" by (rule pCons_cases) |
|
186 |
have "P q" |
|
187 |
proof (cases "q = 0") |
|
188 |
case True |
|
189 |
then show "P q" by (simp add: zero) |
|
190 |
next |
|
191 |
case False |
|
192 |
then have "degree (pCons a q) = Suc (degree q)" |
|
193 |
by (rule degree_pCons_eq) |
|
194 |
then have "degree q < degree p" |
|
195 |
using `p = pCons a q` by simp |
|
196 |
then show "P q" |
|
197 |
by (rule less.hyps) |
|
198 |
qed |
|
199 |
then have "P (pCons a q)" |
|
200 |
by (rule pCons) |
|
201 |
then show ?case |
|
202 |
using `p = pCons a q` by simp |
|
203 |
qed |
|
204 |
||
205 |
||
29454
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
206 |
subsection {* Recursion combinator for polynomials *} |
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
207 |
|
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
208 |
function |
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
209 |
poly_rec :: "'b \<Rightarrow> ('a::zero \<Rightarrow> 'a poly \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a poly \<Rightarrow> 'b" |
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
210 |
where |
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
211 |
poly_rec_pCons_eq_if [simp del]: |
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
212 |
"poly_rec z f (pCons a p) = f a p (if p = 0 then z else poly_rec z f p)" |
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
213 |
by (case_tac x, rename_tac q, case_tac q, auto) |
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
214 |
|
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
215 |
termination poly_rec |
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
216 |
by (relation "measure (degree \<circ> snd \<circ> snd)", simp) |
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
217 |
(simp add: degree_pCons_eq) |
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
218 |
|
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
219 |
lemma poly_rec_0: |
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
220 |
"f 0 0 z = z \<Longrightarrow> poly_rec z f 0 = z" |
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
221 |
using poly_rec_pCons_eq_if [of z f 0 0] by simp |
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
222 |
|
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
223 |
lemma poly_rec_pCons: |
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
224 |
"f 0 0 z = z \<Longrightarrow> poly_rec z f (pCons a p) = f a p (poly_rec z f p)" |
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
225 |
by (simp add: poly_rec_pCons_eq_if poly_rec_0) |
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
226 |
|
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
227 |
|
29451 | 228 |
subsection {* Monomials *} |
229 |
||
230 |
definition |
|
231 |
monom :: "'a \<Rightarrow> nat \<Rightarrow> 'a::zero poly" where |
|
232 |
"monom a m = Abs_poly (\<lambda>n. if m = n then a else 0)" |
|
233 |
||
234 |
lemma coeff_monom [simp]: "coeff (monom a m) n = (if m=n then a else 0)" |
|
235 |
unfolding monom_def |
|
236 |
by (subst Abs_poly_inverse, auto simp add: Poly_def) |
|
237 |
||
238 |
lemma monom_0: "monom a 0 = pCons a 0" |
|
239 |
by (rule poly_ext, simp add: coeff_pCons split: nat.split) |
|
240 |
||
241 |
lemma monom_Suc: "monom a (Suc n) = pCons 0 (monom a n)" |
|
242 |
by (rule poly_ext, simp add: coeff_pCons split: nat.split) |
|
243 |
||
244 |
lemma monom_eq_0 [simp]: "monom 0 n = 0" |
|
245 |
by (rule poly_ext) simp |
|
246 |
||
247 |
lemma monom_eq_0_iff [simp]: "monom a n = 0 \<longleftrightarrow> a = 0" |
|
248 |
by (simp add: expand_poly_eq) |
|
249 |
||
250 |
lemma monom_eq_iff [simp]: "monom a n = monom b n \<longleftrightarrow> a = b" |
|
251 |
by (simp add: expand_poly_eq) |
|
252 |
||
253 |
lemma degree_monom_le: "degree (monom a n) \<le> n" |
|
254 |
by (rule degree_le, simp) |
|
255 |
||
256 |
lemma degree_monom_eq: "a \<noteq> 0 \<Longrightarrow> degree (monom a n) = n" |
|
257 |
apply (rule order_antisym [OF degree_monom_le]) |
|
258 |
apply (rule le_degree, simp) |
|
259 |
done |
|
260 |
||
261 |
||
262 |
subsection {* Addition and subtraction *} |
|
263 |
||
264 |
instantiation poly :: (comm_monoid_add) comm_monoid_add |
|
265 |
begin |
|
266 |
||
267 |
definition |
|
268 |
plus_poly_def [code del]: |
|
269 |
"p + q = Abs_poly (\<lambda>n. coeff p n + coeff q n)" |
|
270 |
||
271 |
lemma Poly_add: |
|
272 |
fixes f g :: "nat \<Rightarrow> 'a" |
|
273 |
shows "\<lbrakk>f \<in> Poly; g \<in> Poly\<rbrakk> \<Longrightarrow> (\<lambda>n. f n + g n) \<in> Poly" |
|
274 |
unfolding Poly_def |
|
275 |
apply (clarify, rename_tac m n) |
|
276 |
apply (rule_tac x="max m n" in exI, simp) |
|
277 |
done |
|
278 |
||
279 |
lemma coeff_add [simp]: |
|
280 |
"coeff (p + q) n = coeff p n + coeff q n" |
|
281 |
unfolding plus_poly_def |
|
282 |
by (simp add: Abs_poly_inverse coeff Poly_add) |
|
283 |
||
284 |
instance proof |
|
285 |
fix p q r :: "'a poly" |
|
286 |
show "(p + q) + r = p + (q + r)" |
|
287 |
by (simp add: expand_poly_eq add_assoc) |
|
288 |
show "p + q = q + p" |
|
289 |
by (simp add: expand_poly_eq add_commute) |
|
290 |
show "0 + p = p" |
|
291 |
by (simp add: expand_poly_eq) |
|
292 |
qed |
|
293 |
||
294 |
end |
|
295 |
||
296 |
instantiation poly :: (ab_group_add) ab_group_add |
|
297 |
begin |
|
298 |
||
299 |
definition |
|
300 |
uminus_poly_def [code del]: |
|
301 |
"- p = Abs_poly (\<lambda>n. - coeff p n)" |
|
302 |
||
303 |
definition |
|
304 |
minus_poly_def [code del]: |
|
305 |
"p - q = Abs_poly (\<lambda>n. coeff p n - coeff q n)" |
|
306 |
||
307 |
lemma Poly_minus: |
|
308 |
fixes f :: "nat \<Rightarrow> 'a" |
|
309 |
shows "f \<in> Poly \<Longrightarrow> (\<lambda>n. - f n) \<in> Poly" |
|
310 |
unfolding Poly_def by simp |
|
311 |
||
312 |
lemma Poly_diff: |
|
313 |
fixes f g :: "nat \<Rightarrow> 'a" |
|
314 |
shows "\<lbrakk>f \<in> Poly; g \<in> Poly\<rbrakk> \<Longrightarrow> (\<lambda>n. f n - g n) \<in> Poly" |
|
315 |
unfolding diff_minus by (simp add: Poly_add Poly_minus) |
|
316 |
||
317 |
lemma coeff_minus [simp]: "coeff (- p) n = - coeff p n" |
|
318 |
unfolding uminus_poly_def |
|
319 |
by (simp add: Abs_poly_inverse coeff Poly_minus) |
|
320 |
||
321 |
lemma coeff_diff [simp]: |
|
322 |
"coeff (p - q) n = coeff p n - coeff q n" |
|
323 |
unfolding minus_poly_def |
|
324 |
by (simp add: Abs_poly_inverse coeff Poly_diff) |
|
325 |
||
326 |
instance proof |
|
327 |
fix p q :: "'a poly" |
|
328 |
show "- p + p = 0" |
|
329 |
by (simp add: expand_poly_eq) |
|
330 |
show "p - q = p + - q" |
|
331 |
by (simp add: expand_poly_eq diff_minus) |
|
332 |
qed |
|
333 |
||
334 |
end |
|
335 |
||
336 |
lemma add_pCons [simp]: |
|
337 |
"pCons a p + pCons b q = pCons (a + b) (p + q)" |
|
338 |
by (rule poly_ext, simp add: coeff_pCons split: nat.split) |
|
339 |
||
340 |
lemma minus_pCons [simp]: |
|
341 |
"- pCons a p = pCons (- a) (- p)" |
|
342 |
by (rule poly_ext, simp add: coeff_pCons split: nat.split) |
|
343 |
||
344 |
lemma diff_pCons [simp]: |
|
345 |
"pCons a p - pCons b q = pCons (a - b) (p - q)" |
|
346 |
by (rule poly_ext, simp add: coeff_pCons split: nat.split) |
|
347 |
||
348 |
lemma degree_add_le: "degree (p + q) \<le> max (degree p) (degree q)" |
|
349 |
by (rule degree_le, auto simp add: coeff_eq_0) |
|
350 |
||
29453 | 351 |
lemma degree_add_less: |
352 |
"\<lbrakk>degree p < n; degree q < n\<rbrakk> \<Longrightarrow> degree (p + q) < n" |
|
353 |
by (auto intro: le_less_trans degree_add_le) |
|
354 |
||
29451 | 355 |
lemma degree_add_eq_right: |
356 |
"degree p < degree q \<Longrightarrow> degree (p + q) = degree q" |
|
357 |
apply (cases "q = 0", simp) |
|
358 |
apply (rule order_antisym) |
|
359 |
apply (rule ord_le_eq_trans [OF degree_add_le]) |
|
360 |
apply simp |
|
361 |
apply (rule le_degree) |
|
362 |
apply (simp add: coeff_eq_0) |
|
363 |
done |
|
364 |
||
365 |
lemma degree_add_eq_left: |
|
366 |
"degree q < degree p \<Longrightarrow> degree (p + q) = degree p" |
|
367 |
using degree_add_eq_right [of q p] |
|
368 |
by (simp add: add_commute) |
|
369 |
||
370 |
lemma degree_minus [simp]: "degree (- p) = degree p" |
|
371 |
unfolding degree_def by simp |
|
372 |
||
373 |
lemma degree_diff_le: "degree (p - q) \<le> max (degree p) (degree q)" |
|
374 |
using degree_add_le [where p=p and q="-q"] |
|
375 |
by (simp add: diff_minus) |
|
376 |
||
29453 | 377 |
lemma degree_diff_less: |
378 |
"\<lbrakk>degree p < n; degree q < n\<rbrakk> \<Longrightarrow> degree (p - q) < n" |
|
379 |
by (auto intro: le_less_trans degree_diff_le) |
|
380 |
||
29451 | 381 |
lemma add_monom: "monom a n + monom b n = monom (a + b) n" |
382 |
by (rule poly_ext) simp |
|
383 |
||
384 |
lemma diff_monom: "monom a n - monom b n = monom (a - b) n" |
|
385 |
by (rule poly_ext) simp |
|
386 |
||
387 |
lemma minus_monom: "- monom a n = monom (-a) n" |
|
388 |
by (rule poly_ext) simp |
|
389 |
||
390 |
lemma coeff_setsum: "coeff (\<Sum>x\<in>A. p x) i = (\<Sum>x\<in>A. coeff (p x) i)" |
|
391 |
by (cases "finite A", induct set: finite, simp_all) |
|
392 |
||
393 |
lemma monom_setsum: "monom (\<Sum>x\<in>A. a x) n = (\<Sum>x\<in>A. monom (a x) n)" |
|
394 |
by (rule poly_ext) (simp add: coeff_setsum) |
|
395 |
||
396 |
||
397 |
subsection {* Multiplication by a constant *} |
|
398 |
||
399 |
definition |
|
400 |
smult :: "'a::comm_semiring_0 \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where |
|
401 |
"smult a p = Abs_poly (\<lambda>n. a * coeff p n)" |
|
402 |
||
403 |
lemma Poly_smult: |
|
404 |
fixes f :: "nat \<Rightarrow> 'a::comm_semiring_0" |
|
405 |
shows "f \<in> Poly \<Longrightarrow> (\<lambda>n. a * f n) \<in> Poly" |
|
406 |
unfolding Poly_def |
|
407 |
by (clarify, rule_tac x=n in exI, simp) |
|
408 |
||
409 |
lemma coeff_smult [simp]: "coeff (smult a p) n = a * coeff p n" |
|
410 |
unfolding smult_def |
|
411 |
by (simp add: Abs_poly_inverse Poly_smult coeff) |
|
412 |
||
413 |
lemma degree_smult_le: "degree (smult a p) \<le> degree p" |
|
414 |
by (rule degree_le, simp add: coeff_eq_0) |
|
415 |
||
416 |
lemma smult_smult: "smult a (smult b p) = smult (a * b) p" |
|
417 |
by (rule poly_ext, simp add: mult_assoc) |
|
418 |
||
419 |
lemma smult_0_right [simp]: "smult a 0 = 0" |
|
420 |
by (rule poly_ext, simp) |
|
421 |
||
422 |
lemma smult_0_left [simp]: "smult 0 p = 0" |
|
423 |
by (rule poly_ext, simp) |
|
424 |
||
425 |
lemma smult_1_left [simp]: "smult (1::'a::comm_semiring_1) p = p" |
|
426 |
by (rule poly_ext, simp) |
|
427 |
||
428 |
lemma smult_add_right: |
|
429 |
"smult a (p + q) = smult a p + smult a q" |
|
430 |
by (rule poly_ext, simp add: ring_simps) |
|
431 |
||
432 |
lemma smult_add_left: |
|
433 |
"smult (a + b) p = smult a p + smult b p" |
|
434 |
by (rule poly_ext, simp add: ring_simps) |
|
435 |
||
29457 | 436 |
lemma smult_minus_right [simp]: |
29451 | 437 |
"smult (a::'a::comm_ring) (- p) = - smult a p" |
438 |
by (rule poly_ext, simp) |
|
439 |
||
29457 | 440 |
lemma smult_minus_left [simp]: |
29451 | 441 |
"smult (- a::'a::comm_ring) p = - smult a p" |
442 |
by (rule poly_ext, simp) |
|
443 |
||
444 |
lemma smult_diff_right: |
|
445 |
"smult (a::'a::comm_ring) (p - q) = smult a p - smult a q" |
|
446 |
by (rule poly_ext, simp add: ring_simps) |
|
447 |
||
448 |
lemma smult_diff_left: |
|
449 |
"smult (a - b::'a::comm_ring) p = smult a p - smult b p" |
|
450 |
by (rule poly_ext, simp add: ring_simps) |
|
451 |
||
452 |
lemma smult_pCons [simp]: |
|
453 |
"smult a (pCons b p) = pCons (a * b) (smult a p)" |
|
454 |
by (rule poly_ext, simp add: coeff_pCons split: nat.split) |
|
455 |
||
456 |
lemma smult_monom: "smult a (monom b n) = monom (a * b) n" |
|
457 |
by (induct n, simp add: monom_0, simp add: monom_Suc) |
|
458 |
||
459 |
||
460 |
subsection {* Multiplication of polynomials *} |
|
461 |
||
462 |
lemma Poly_mult_lemma: |
|
463 |
fixes f g :: "nat \<Rightarrow> 'a::comm_semiring_0" and m n :: nat |
|
464 |
assumes "\<forall>i>m. f i = 0" |
|
465 |
assumes "\<forall>j>n. g j = 0" |
|
466 |
shows "\<forall>k>m+n. (\<Sum>i\<le>k. f i * g (k-i)) = 0" |
|
467 |
proof (clarify) |
|
468 |
fix k :: nat |
|
469 |
assume "m + n < k" |
|
470 |
show "(\<Sum>i\<le>k. f i * g (k - i)) = 0" |
|
471 |
proof (rule setsum_0' [rule_format]) |
|
472 |
fix i :: nat |
|
473 |
assume "i \<in> {..k}" hence "i \<le> k" by simp |
|
474 |
with `m + n < k` have "m < i \<or> n < k - i" by arith |
|
475 |
thus "f i * g (k - i) = 0" |
|
476 |
using prems by auto |
|
477 |
qed |
|
478 |
qed |
|
479 |
||
480 |
lemma Poly_mult: |
|
481 |
fixes f g :: "nat \<Rightarrow> 'a::comm_semiring_0" |
|
482 |
shows "\<lbrakk>f \<in> Poly; g \<in> Poly\<rbrakk> \<Longrightarrow> (\<lambda>n. \<Sum>i\<le>n. f i * g (n-i)) \<in> Poly" |
|
483 |
unfolding Poly_def |
|
484 |
by (safe, rule exI, rule Poly_mult_lemma) |
|
485 |
||
486 |
lemma poly_mult_assoc_lemma: |
|
487 |
fixes k :: nat and f :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> 'a::comm_monoid_add" |
|
488 |
shows "(\<Sum>j\<le>k. \<Sum>i\<le>j. f i (j - i) (n - j)) = |
|
489 |
(\<Sum>j\<le>k. \<Sum>i\<le>k - j. f j i (n - j - i))" |
|
490 |
proof (induct k) |
|
491 |
case 0 show ?case by simp |
|
492 |
next |
|
493 |
case (Suc k) thus ?case |
|
494 |
by (simp add: Suc_diff_le setsum_addf add_assoc |
|
495 |
cong: strong_setsum_cong) |
|
496 |
qed |
|
497 |
||
498 |
lemma poly_mult_commute_lemma: |
|
499 |
fixes n :: nat and f :: "nat \<Rightarrow> nat \<Rightarrow> 'a::comm_monoid_add" |
|
500 |
shows "(\<Sum>i\<le>n. f i (n - i)) = (\<Sum>i\<le>n. f (n - i) i)" |
|
501 |
proof (rule setsum_reindex_cong) |
|
502 |
show "inj_on (\<lambda>i. n - i) {..n}" |
|
503 |
by (rule inj_onI) simp |
|
504 |
show "{..n} = (\<lambda>i. n - i) ` {..n}" |
|
505 |
by (auto, rule_tac x="n - x" in image_eqI, simp_all) |
|
506 |
next |
|
507 |
fix i assume "i \<in> {..n}" |
|
508 |
hence "n - (n - i) = i" by simp |
|
509 |
thus "f (n - i) i = f (n - i) (n - (n - i))" by simp |
|
510 |
qed |
|
511 |
||
512 |
text {* TODO: move to appropriate theory *} |
|
513 |
lemma setsum_atMost_Suc_shift: |
|
514 |
fixes f :: "nat \<Rightarrow> 'a::comm_monoid_add" |
|
515 |
shows "(\<Sum>i\<le>Suc n. f i) = f 0 + (\<Sum>i\<le>n. f (Suc i))" |
|
516 |
proof (induct n) |
|
517 |
case 0 show ?case by simp |
|
518 |
next |
|
519 |
case (Suc n) note IH = this |
|
520 |
have "(\<Sum>i\<le>Suc (Suc n). f i) = (\<Sum>i\<le>Suc n. f i) + f (Suc (Suc n))" |
|
521 |
by (rule setsum_atMost_Suc) |
|
522 |
also have "(\<Sum>i\<le>Suc n. f i) = f 0 + (\<Sum>i\<le>n. f (Suc i))" |
|
523 |
by (rule IH) |
|
524 |
also have "f 0 + (\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)) = |
|
525 |
f 0 + ((\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)))" |
|
526 |
by (rule add_assoc) |
|
527 |
also have "(\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)) = (\<Sum>i\<le>Suc n. f (Suc i))" |
|
528 |
by (rule setsum_atMost_Suc [symmetric]) |
|
529 |
finally show ?case . |
|
530 |
qed |
|
531 |
||
532 |
instantiation poly :: (comm_semiring_0) comm_semiring_0 |
|
533 |
begin |
|
534 |
||
535 |
definition |
|
536 |
times_poly_def: |
|
537 |
"p * q = Abs_poly (\<lambda>n. \<Sum>i\<le>n. coeff p i * coeff q (n-i))" |
|
538 |
||
539 |
lemma coeff_mult: |
|
540 |
"coeff (p * q) n = (\<Sum>i\<le>n. coeff p i * coeff q (n-i))" |
|
541 |
unfolding times_poly_def |
|
542 |
by (simp add: Abs_poly_inverse coeff Poly_mult) |
|
543 |
||
544 |
instance proof |
|
545 |
fix p q r :: "'a poly" |
|
546 |
show 0: "0 * p = 0" |
|
547 |
by (simp add: expand_poly_eq coeff_mult) |
|
548 |
show "p * 0 = 0" |
|
549 |
by (simp add: expand_poly_eq coeff_mult) |
|
550 |
show "(p + q) * r = p * r + q * r" |
|
551 |
by (simp add: expand_poly_eq coeff_mult left_distrib setsum_addf) |
|
552 |
show "(p * q) * r = p * (q * r)" |
|
553 |
proof (rule poly_ext) |
|
554 |
fix n :: nat |
|
555 |
have "(\<Sum>j\<le>n. \<Sum>i\<le>j. coeff p i * coeff q (j - i) * coeff r (n - j)) = |
|
556 |
(\<Sum>j\<le>n. \<Sum>i\<le>n - j. coeff p j * coeff q i * coeff r (n - j - i))" |
|
557 |
by (rule poly_mult_assoc_lemma) |
|
558 |
thus "coeff ((p * q) * r) n = coeff (p * (q * r)) n" |
|
559 |
by (simp add: coeff_mult setsum_right_distrib |
|
560 |
setsum_left_distrib mult_assoc) |
|
561 |
qed |
|
562 |
show "p * q = q * p" |
|
563 |
proof (rule poly_ext) |
|
564 |
fix n :: nat |
|
565 |
have "(\<Sum>i\<le>n. coeff p i * coeff q (n - i)) = |
|
566 |
(\<Sum>i\<le>n. coeff p (n - i) * coeff q i)" |
|
567 |
by (rule poly_mult_commute_lemma) |
|
568 |
thus "coeff (p * q) n = coeff (q * p) n" |
|
569 |
by (simp add: coeff_mult mult_commute) |
|
570 |
qed |
|
571 |
qed |
|
572 |
||
573 |
end |
|
574 |
||
575 |
lemma degree_mult_le: "degree (p * q) \<le> degree p + degree q" |
|
576 |
apply (rule degree_le, simp add: coeff_mult) |
|
577 |
apply (rule Poly_mult_lemma) |
|
578 |
apply (simp_all add: coeff_eq_0) |
|
579 |
done |
|
580 |
||
581 |
lemma mult_pCons_left [simp]: |
|
582 |
"pCons a p * q = smult a q + pCons 0 (p * q)" |
|
583 |
apply (rule poly_ext) |
|
584 |
apply (case_tac n) |
|
585 |
apply (simp add: coeff_mult) |
|
586 |
apply (simp add: coeff_mult setsum_atMost_Suc_shift |
|
587 |
del: setsum_atMost_Suc) |
|
588 |
done |
|
589 |
||
590 |
lemma mult_pCons_right [simp]: |
|
591 |
"p * pCons a q = smult a p + pCons 0 (p * q)" |
|
592 |
using mult_pCons_left [of a q p] by (simp add: mult_commute) |
|
593 |
||
594 |
lemma mult_smult_left: "smult a p * q = smult a (p * q)" |
|
595 |
by (induct p, simp, simp add: smult_add_right smult_smult) |
|
596 |
||
597 |
lemma mult_smult_right: "p * smult a q = smult a (p * q)" |
|
598 |
using mult_smult_left [of a q p] by (simp add: mult_commute) |
|
599 |
||
600 |
lemma mult_monom: "monom a m * monom b n = monom (a * b) (m + n)" |
|
601 |
by (induct m, simp add: monom_0 smult_monom, simp add: monom_Suc) |
|
602 |
||
603 |
||
604 |
subsection {* The unit polynomial and exponentiation *} |
|
605 |
||
606 |
instantiation poly :: (comm_semiring_1) comm_semiring_1 |
|
607 |
begin |
|
608 |
||
609 |
definition |
|
610 |
one_poly_def: |
|
611 |
"1 = pCons 1 0" |
|
612 |
||
613 |
instance proof |
|
614 |
fix p :: "'a poly" show "1 * p = p" |
|
615 |
unfolding one_poly_def |
|
616 |
by simp |
|
617 |
next |
|
618 |
show "0 \<noteq> (1::'a poly)" |
|
619 |
unfolding one_poly_def by simp |
|
620 |
qed |
|
621 |
||
622 |
end |
|
623 |
||
624 |
lemma coeff_1 [simp]: "coeff 1 n = (if n = 0 then 1 else 0)" |
|
625 |
unfolding one_poly_def |
|
626 |
by (simp add: coeff_pCons split: nat.split) |
|
627 |
||
628 |
lemma degree_1 [simp]: "degree 1 = 0" |
|
629 |
unfolding one_poly_def |
|
630 |
by (rule degree_pCons_0) |
|
631 |
||
632 |
instantiation poly :: (comm_semiring_1) recpower |
|
633 |
begin |
|
634 |
||
635 |
primrec power_poly where |
|
636 |
power_poly_0: "(p::'a poly) ^ 0 = 1" |
|
637 |
| power_poly_Suc: "(p::'a poly) ^ (Suc n) = p * p ^ n" |
|
638 |
||
639 |
instance |
|
640 |
by default simp_all |
|
641 |
||
642 |
end |
|
643 |
||
644 |
instance poly :: (comm_ring) comm_ring .. |
|
645 |
||
646 |
instance poly :: (comm_ring_1) comm_ring_1 .. |
|
647 |
||
648 |
instantiation poly :: (comm_ring_1) number_ring |
|
649 |
begin |
|
650 |
||
651 |
definition |
|
652 |
"number_of k = (of_int k :: 'a poly)" |
|
653 |
||
654 |
instance |
|
655 |
by default (rule number_of_poly_def) |
|
656 |
||
657 |
end |
|
658 |
||
659 |
||
660 |
subsection {* Polynomials form an integral domain *} |
|
661 |
||
662 |
lemma coeff_mult_degree_sum: |
|
663 |
"coeff (p * q) (degree p + degree q) = |
|
664 |
coeff p (degree p) * coeff q (degree q)" |
|
29471 | 665 |
by (induct p, simp, simp add: coeff_eq_0) |
29451 | 666 |
|
667 |
instance poly :: (idom) idom |
|
668 |
proof |
|
669 |
fix p q :: "'a poly" |
|
670 |
assume "p \<noteq> 0" and "q \<noteq> 0" |
|
671 |
have "coeff (p * q) (degree p + degree q) = |
|
672 |
coeff p (degree p) * coeff q (degree q)" |
|
673 |
by (rule coeff_mult_degree_sum) |
|
674 |
also have "coeff p (degree p) * coeff q (degree q) \<noteq> 0" |
|
675 |
using `p \<noteq> 0` and `q \<noteq> 0` by simp |
|
676 |
finally have "\<exists>n. coeff (p * q) n \<noteq> 0" .. |
|
677 |
thus "p * q \<noteq> 0" by (simp add: expand_poly_eq) |
|
678 |
qed |
|
679 |
||
680 |
lemma degree_mult_eq: |
|
681 |
fixes p q :: "'a::idom poly" |
|
682 |
shows "\<lbrakk>p \<noteq> 0; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree (p * q) = degree p + degree q" |
|
683 |
apply (rule order_antisym [OF degree_mult_le le_degree]) |
|
684 |
apply (simp add: coeff_mult_degree_sum) |
|
685 |
done |
|
686 |
||
687 |
lemma dvd_imp_degree_le: |
|
688 |
fixes p q :: "'a::idom poly" |
|
689 |
shows "\<lbrakk>p dvd q; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree p \<le> degree q" |
|
690 |
by (erule dvdE, simp add: degree_mult_eq) |
|
691 |
||
692 |
||
693 |
subsection {* Long division of polynomials *} |
|
694 |
||
695 |
definition |
|
696 |
divmod_poly_rel :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> bool" |
|
697 |
where |
|
698 |
"divmod_poly_rel x y q r \<longleftrightarrow> |
|
699 |
x = q * y + r \<and> (if y = 0 then q = 0 else r = 0 \<or> degree r < degree y)" |
|
700 |
||
701 |
lemma divmod_poly_rel_0: |
|
702 |
"divmod_poly_rel 0 y 0 0" |
|
703 |
unfolding divmod_poly_rel_def by simp |
|
704 |
||
705 |
lemma divmod_poly_rel_by_0: |
|
706 |
"divmod_poly_rel x 0 0 x" |
|
707 |
unfolding divmod_poly_rel_def by simp |
|
708 |
||
709 |
lemma eq_zero_or_degree_less: |
|
710 |
assumes "degree p \<le> n" and "coeff p n = 0" |
|
711 |
shows "p = 0 \<or> degree p < n" |
|
712 |
proof (cases n) |
|
713 |
case 0 |
|
714 |
with `degree p \<le> n` and `coeff p n = 0` |
|
715 |
have "coeff p (degree p) = 0" by simp |
|
716 |
then have "p = 0" by simp |
|
717 |
then show ?thesis .. |
|
718 |
next |
|
719 |
case (Suc m) |
|
720 |
have "\<forall>i>n. coeff p i = 0" |
|
721 |
using `degree p \<le> n` by (simp add: coeff_eq_0) |
|
722 |
then have "\<forall>i\<ge>n. coeff p i = 0" |
|
723 |
using `coeff p n = 0` by (simp add: le_less) |
|
724 |
then have "\<forall>i>m. coeff p i = 0" |
|
725 |
using `n = Suc m` by (simp add: less_eq_Suc_le) |
|
726 |
then have "degree p \<le> m" |
|
727 |
by (rule degree_le) |
|
728 |
then have "degree p < n" |
|
729 |
using `n = Suc m` by (simp add: less_Suc_eq_le) |
|
730 |
then show ?thesis .. |
|
731 |
qed |
|
732 |
||
733 |
lemma divmod_poly_rel_pCons: |
|
734 |
assumes rel: "divmod_poly_rel x y q r" |
|
735 |
assumes y: "y \<noteq> 0" |
|
736 |
assumes b: "b = coeff (pCons a r) (degree y) / coeff y (degree y)" |
|
737 |
shows "divmod_poly_rel (pCons a x) y (pCons b q) (pCons a r - smult b y)" |
|
738 |
(is "divmod_poly_rel ?x y ?q ?r") |
|
739 |
proof - |
|
740 |
have x: "x = q * y + r" and r: "r = 0 \<or> degree r < degree y" |
|
741 |
using assms unfolding divmod_poly_rel_def by simp_all |
|
742 |
||
743 |
have 1: "?x = ?q * y + ?r" |
|
744 |
using b x by simp |
|
745 |
||
746 |
have 2: "?r = 0 \<or> degree ?r < degree y" |
|
747 |
proof (rule eq_zero_or_degree_less) |
|
748 |
have "degree ?r \<le> max (degree (pCons a r)) (degree (smult b y))" |
|
749 |
by (rule degree_diff_le) |
|
750 |
also have "\<dots> \<le> degree y" |
|
751 |
proof (rule min_max.le_supI) |
|
752 |
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
|
753 |
using r by auto |
29451 | 754 |
show "degree (smult b y) \<le> degree y" |
755 |
by (rule degree_smult_le) |
|
756 |
qed |
|
757 |
finally show "degree ?r \<le> degree y" . |
|
758 |
next |
|
759 |
show "coeff ?r (degree y) = 0" |
|
760 |
using `y \<noteq> 0` unfolding b by simp |
|
761 |
qed |
|
762 |
||
763 |
from 1 2 show ?thesis |
|
764 |
unfolding divmod_poly_rel_def |
|
765 |
using `y \<noteq> 0` by simp |
|
766 |
qed |
|
767 |
||
768 |
lemma divmod_poly_rel_exists: "\<exists>q r. divmod_poly_rel x y q r" |
|
769 |
apply (cases "y = 0") |
|
770 |
apply (fast intro!: divmod_poly_rel_by_0) |
|
771 |
apply (induct x) |
|
772 |
apply (fast intro!: divmod_poly_rel_0) |
|
773 |
apply (fast intro!: divmod_poly_rel_pCons) |
|
774 |
done |
|
775 |
||
776 |
lemma divmod_poly_rel_unique: |
|
777 |
assumes 1: "divmod_poly_rel x y q1 r1" |
|
778 |
assumes 2: "divmod_poly_rel x y q2 r2" |
|
779 |
shows "q1 = q2 \<and> r1 = r2" |
|
780 |
proof (cases "y = 0") |
|
781 |
assume "y = 0" with assms show ?thesis |
|
782 |
by (simp add: divmod_poly_rel_def) |
|
783 |
next |
|
784 |
assume [simp]: "y \<noteq> 0" |
|
785 |
from 1 have q1: "x = q1 * y + r1" and r1: "r1 = 0 \<or> degree r1 < degree y" |
|
786 |
unfolding divmod_poly_rel_def by simp_all |
|
787 |
from 2 have q2: "x = q2 * y + r2" and r2: "r2 = 0 \<or> degree r2 < degree y" |
|
788 |
unfolding divmod_poly_rel_def by simp_all |
|
789 |
from q1 q2 have q3: "(q1 - q2) * y = r2 - r1" |
|
790 |
by (simp add: ring_simps) |
|
791 |
from r1 r2 have r3: "(r2 - r1) = 0 \<or> degree (r2 - r1) < degree y" |
|
29453 | 792 |
by (auto intro: degree_diff_less) |
29451 | 793 |
|
794 |
show "q1 = q2 \<and> r1 = r2" |
|
795 |
proof (rule ccontr) |
|
796 |
assume "\<not> (q1 = q2 \<and> r1 = r2)" |
|
797 |
with q3 have "q1 \<noteq> q2" and "r1 \<noteq> r2" by auto |
|
798 |
with r3 have "degree (r2 - r1) < degree y" by simp |
|
799 |
also have "degree y \<le> degree (q1 - q2) + degree y" by simp |
|
800 |
also have "\<dots> = degree ((q1 - q2) * y)" |
|
801 |
using `q1 \<noteq> q2` by (simp add: degree_mult_eq) |
|
802 |
also have "\<dots> = degree (r2 - r1)" |
|
803 |
using q3 by simp |
|
804 |
finally have "degree (r2 - r1) < degree (r2 - r1)" . |
|
805 |
then show "False" by simp |
|
806 |
qed |
|
807 |
qed |
|
808 |
||
809 |
lemmas divmod_poly_rel_unique_div = |
|
810 |
divmod_poly_rel_unique [THEN conjunct1, standard] |
|
811 |
||
812 |
lemmas divmod_poly_rel_unique_mod = |
|
813 |
divmod_poly_rel_unique [THEN conjunct2, standard] |
|
814 |
||
815 |
instantiation poly :: (field) ring_div |
|
816 |
begin |
|
817 |
||
818 |
definition div_poly where |
|
819 |
[code del]: "x div y = (THE q. \<exists>r. divmod_poly_rel x y q r)" |
|
820 |
||
821 |
definition mod_poly where |
|
822 |
[code del]: "x mod y = (THE r. \<exists>q. divmod_poly_rel x y q r)" |
|
823 |
||
824 |
lemma div_poly_eq: |
|
825 |
"divmod_poly_rel x y q r \<Longrightarrow> x div y = q" |
|
826 |
unfolding div_poly_def |
|
827 |
by (fast elim: divmod_poly_rel_unique_div) |
|
828 |
||
829 |
lemma mod_poly_eq: |
|
830 |
"divmod_poly_rel x y q r \<Longrightarrow> x mod y = r" |
|
831 |
unfolding mod_poly_def |
|
832 |
by (fast elim: divmod_poly_rel_unique_mod) |
|
833 |
||
834 |
lemma divmod_poly_rel: |
|
835 |
"divmod_poly_rel x y (x div y) (x mod y)" |
|
836 |
proof - |
|
837 |
from divmod_poly_rel_exists |
|
838 |
obtain q r where "divmod_poly_rel x y q r" by fast |
|
839 |
thus ?thesis |
|
840 |
by (simp add: div_poly_eq mod_poly_eq) |
|
841 |
qed |
|
842 |
||
843 |
instance proof |
|
844 |
fix x y :: "'a poly" |
|
845 |
show "x div y * y + x mod y = x" |
|
846 |
using divmod_poly_rel [of x y] |
|
847 |
by (simp add: divmod_poly_rel_def) |
|
848 |
next |
|
849 |
fix x :: "'a poly" |
|
850 |
have "divmod_poly_rel x 0 0 x" |
|
851 |
by (rule divmod_poly_rel_by_0) |
|
852 |
thus "x div 0 = 0" |
|
853 |
by (rule div_poly_eq) |
|
854 |
next |
|
855 |
fix y :: "'a poly" |
|
856 |
have "divmod_poly_rel 0 y 0 0" |
|
857 |
by (rule divmod_poly_rel_0) |
|
858 |
thus "0 div y = 0" |
|
859 |
by (rule div_poly_eq) |
|
860 |
next |
|
861 |
fix x y z :: "'a poly" |
|
862 |
assume "y \<noteq> 0" |
|
863 |
hence "divmod_poly_rel (x + z * y) y (z + x div y) (x mod y)" |
|
864 |
using divmod_poly_rel [of x y] |
|
865 |
by (simp add: divmod_poly_rel_def left_distrib) |
|
866 |
thus "(x + z * y) div y = z + x div y" |
|
867 |
by (rule div_poly_eq) |
|
868 |
qed |
|
869 |
||
870 |
end |
|
871 |
||
872 |
lemma degree_mod_less: |
|
873 |
"y \<noteq> 0 \<Longrightarrow> x mod y = 0 \<or> degree (x mod y) < degree y" |
|
874 |
using divmod_poly_rel [of x y] |
|
875 |
unfolding divmod_poly_rel_def by simp |
|
876 |
||
877 |
lemma div_poly_less: "degree x < degree y \<Longrightarrow> x div y = 0" |
|
878 |
proof - |
|
879 |
assume "degree x < degree y" |
|
880 |
hence "divmod_poly_rel x y 0 x" |
|
881 |
by (simp add: divmod_poly_rel_def) |
|
882 |
thus "x div y = 0" by (rule div_poly_eq) |
|
883 |
qed |
|
884 |
||
885 |
lemma mod_poly_less: "degree x < degree y \<Longrightarrow> x mod y = x" |
|
886 |
proof - |
|
887 |
assume "degree x < degree y" |
|
888 |
hence "divmod_poly_rel x y 0 x" |
|
889 |
by (simp add: divmod_poly_rel_def) |
|
890 |
thus "x mod y = x" by (rule mod_poly_eq) |
|
891 |
qed |
|
892 |
||
893 |
lemma mod_pCons: |
|
894 |
fixes a and x |
|
895 |
assumes y: "y \<noteq> 0" |
|
896 |
defines b: "b \<equiv> coeff (pCons a (x mod y)) (degree y) / coeff y (degree y)" |
|
897 |
shows "(pCons a x) mod y = (pCons a (x mod y) - smult b y)" |
|
898 |
unfolding b |
|
899 |
apply (rule mod_poly_eq) |
|
900 |
apply (rule divmod_poly_rel_pCons [OF divmod_poly_rel y refl]) |
|
901 |
done |
|
902 |
||
903 |
||
904 |
subsection {* Evaluation of polynomials *} |
|
905 |
||
906 |
definition |
|
29454
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
907 |
poly :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a" where |
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
908 |
"poly = poly_rec (\<lambda>x. 0) (\<lambda>a p f x. a + x * f x)" |
29451 | 909 |
|
910 |
lemma poly_0 [simp]: "poly 0 x = 0" |
|
29454
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
911 |
unfolding poly_def by (simp add: poly_rec_0) |
29451 | 912 |
|
913 |
lemma poly_pCons [simp]: "poly (pCons a p) x = a + x * poly p x" |
|
29454
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
914 |
unfolding poly_def by (simp add: poly_rec_pCons) |
29451 | 915 |
|
916 |
lemma poly_1 [simp]: "poly 1 x = 1" |
|
917 |
unfolding one_poly_def by simp |
|
918 |
||
29454
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
919 |
lemma poly_monom: |
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
920 |
fixes a x :: "'a::{comm_semiring_1,recpower}" |
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
921 |
shows "poly (monom a n) x = a * x ^ n" |
29451 | 922 |
by (induct n, simp add: monom_0, simp add: monom_Suc power_Suc mult_ac) |
923 |
||
924 |
lemma poly_add [simp]: "poly (p + q) x = poly p x + poly q x" |
|
925 |
apply (induct p arbitrary: q, simp) |
|
926 |
apply (case_tac q, simp, simp add: ring_simps) |
|
927 |
done |
|
928 |
||
929 |
lemma poly_minus [simp]: |
|
29454
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
930 |
fixes x :: "'a::comm_ring" |
29451 | 931 |
shows "poly (- p) x = - poly p x" |
932 |
by (induct p, simp_all) |
|
933 |
||
934 |
lemma poly_diff [simp]: |
|
29454
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
935 |
fixes x :: "'a::comm_ring" |
29451 | 936 |
shows "poly (p - q) x = poly p x - poly q x" |
937 |
by (simp add: diff_minus) |
|
938 |
||
939 |
lemma poly_setsum: "poly (\<Sum>k\<in>A. p k) x = (\<Sum>k\<in>A. poly (p k) x)" |
|
940 |
by (cases "finite A", induct set: finite, simp_all) |
|
941 |
||
942 |
lemma poly_smult [simp]: "poly (smult a p) x = a * poly p x" |
|
943 |
by (induct p, simp, simp add: ring_simps) |
|
944 |
||
945 |
lemma poly_mult [simp]: "poly (p * q) x = poly p x * poly q x" |
|
946 |
by (induct p, simp_all, simp add: ring_simps) |
|
947 |
||
29462 | 948 |
lemma poly_power [simp]: |
949 |
fixes p :: "'a::{comm_semiring_1,recpower} poly" |
|
950 |
shows "poly (p ^ n) x = poly p x ^ n" |
|
951 |
by (induct n, simp, simp add: power_Suc) |
|
952 |
||
29456 | 953 |
|
954 |
subsection {* Synthetic division *} |
|
955 |
||
956 |
definition |
|
957 |
synthetic_divmod :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly \<times> 'a" |
|
958 |
where |
|
959 |
"synthetic_divmod p c = |
|
960 |
poly_rec (0, 0) (\<lambda>a p (q, r). (pCons r q, a + c * r)) p" |
|
961 |
||
962 |
definition |
|
963 |
synthetic_div :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly" |
|
964 |
where |
|
965 |
"synthetic_div p c = fst (synthetic_divmod p c)" |
|
966 |
||
967 |
lemma synthetic_divmod_0 [simp]: |
|
968 |
"synthetic_divmod 0 c = (0, 0)" |
|
969 |
unfolding synthetic_divmod_def |
|
970 |
by (simp add: poly_rec_0) |
|
971 |
||
972 |
lemma synthetic_divmod_pCons [simp]: |
|
973 |
"synthetic_divmod (pCons a p) c = |
|
974 |
(\<lambda>(q, r). (pCons r q, a + c * r)) (synthetic_divmod p c)" |
|
975 |
unfolding synthetic_divmod_def |
|
976 |
by (simp add: poly_rec_pCons) |
|
977 |
||
978 |
lemma snd_synthetic_divmod: "snd (synthetic_divmod p c) = poly p c" |
|
979 |
by (induct p, simp, simp add: split_def) |
|
980 |
||
981 |
lemma synthetic_div_0 [simp]: "synthetic_div 0 c = 0" |
|
982 |
unfolding synthetic_div_def by simp |
|
983 |
||
984 |
lemma synthetic_div_pCons [simp]: |
|
985 |
"synthetic_div (pCons a p) c = pCons (poly p c) (synthetic_div p c)" |
|
986 |
unfolding synthetic_div_def |
|
987 |
by (simp add: split_def snd_synthetic_divmod) |
|
988 |
||
29460
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
989 |
lemma synthetic_div_eq_0_iff: |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
990 |
"synthetic_div p c = 0 \<longleftrightarrow> degree p = 0" |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
991 |
by (induct p, simp, case_tac p, simp) |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
992 |
|
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
993 |
lemma degree_synthetic_div: |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
994 |
"degree (synthetic_div p c) = degree p - 1" |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
995 |
by (induct p, simp, simp add: synthetic_div_eq_0_iff) |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
996 |
|
29457 | 997 |
lemma synthetic_div_correct: |
29456 | 998 |
"p + smult c (synthetic_div p c) = pCons (poly p c) (synthetic_div p c)" |
999 |
by (induct p) simp_all |
|
1000 |
||
29457 | 1001 |
lemma synthetic_div_unique_lemma: "smult c p = pCons a p \<Longrightarrow> p = 0" |
1002 |
by (induct p arbitrary: a) simp_all |
|
1003 |
||
1004 |
lemma synthetic_div_unique: |
|
1005 |
"p + smult c q = pCons r q \<Longrightarrow> r = poly p c \<and> q = synthetic_div p c" |
|
1006 |
apply (induct p arbitrary: q r) |
|
1007 |
apply (simp, frule synthetic_div_unique_lemma, simp) |
|
1008 |
apply (case_tac q, force) |
|
1009 |
done |
|
1010 |
||
1011 |
lemma synthetic_div_correct': |
|
1012 |
fixes c :: "'a::comm_ring_1" |
|
1013 |
shows "[:-c, 1:] * synthetic_div p c + [:poly p c:] = p" |
|
1014 |
using synthetic_div_correct [of p c] |
|
1015 |
by (simp add: group_simps) |
|
1016 |
||
29460
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
1017 |
lemma poly_eq_0_iff_dvd: |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
1018 |
fixes c :: "'a::idom" |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
1019 |
shows "poly p c = 0 \<longleftrightarrow> [:-c, 1:] dvd p" |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
1020 |
proof |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
1021 |
assume "poly p c = 0" |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
1022 |
with synthetic_div_correct' [of c p] |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
1023 |
have "p = [:-c, 1:] * synthetic_div p c" by simp |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
1024 |
then show "[:-c, 1:] dvd p" .. |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
1025 |
next |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
1026 |
assume "[:-c, 1:] dvd p" |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
1027 |
then obtain k where "p = [:-c, 1:] * k" by (rule dvdE) |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
1028 |
then show "poly p c = 0" by simp |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
1029 |
qed |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
1030 |
|
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
1031 |
lemma dvd_iff_poly_eq_0: |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
1032 |
fixes c :: "'a::idom" |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
1033 |
shows "[:c, 1:] dvd p \<longleftrightarrow> poly p (-c) = 0" |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
1034 |
by (simp add: poly_eq_0_iff_dvd) |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
1035 |
|
29462 | 1036 |
lemma poly_roots_finite: |
1037 |
fixes p :: "'a::idom poly" |
|
1038 |
shows "p \<noteq> 0 \<Longrightarrow> finite {x. poly p x = 0}" |
|
1039 |
proof (induct n \<equiv> "degree p" arbitrary: p) |
|
1040 |
case (0 p) |
|
1041 |
then obtain a where "a \<noteq> 0" and "p = [:a:]" |
|
1042 |
by (cases p, simp split: if_splits) |
|
1043 |
then show "finite {x. poly p x = 0}" by simp |
|
1044 |
next |
|
1045 |
case (Suc n p) |
|
1046 |
show "finite {x. poly p x = 0}" |
|
1047 |
proof (cases "\<exists>x. poly p x = 0") |
|
1048 |
case False |
|
1049 |
then show "finite {x. poly p x = 0}" by simp |
|
1050 |
next |
|
1051 |
case True |
|
1052 |
then obtain a where "poly p a = 0" .. |
|
1053 |
then have "[:-a, 1:] dvd p" by (simp only: poly_eq_0_iff_dvd) |
|
1054 |
then obtain k where k: "p = [:-a, 1:] * k" .. |
|
1055 |
with `p \<noteq> 0` have "k \<noteq> 0" by auto |
|
1056 |
with k have "degree p = Suc (degree k)" |
|
1057 |
by (simp add: degree_mult_eq del: mult_pCons_left) |
|
1058 |
with `Suc n = degree p` have "n = degree k" by simp |
|
1059 |
with `k \<noteq> 0` have "finite {x. poly k x = 0}" by (rule Suc.hyps) |
|
1060 |
then have "finite (insert a {x. poly k x = 0})" by simp |
|
1061 |
then show "finite {x. poly p x = 0}" |
|
1062 |
by (simp add: k uminus_add_conv_diff Collect_disj_eq |
|
1063 |
del: mult_pCons_left) |
|
1064 |
qed |
|
1065 |
qed |
|
1066 |
||
29451 | 1067 |
end |