author | haftmann |
Wed, 21 Jan 2009 16:47:31 +0100 | |
changeset 29580 | 117b88da143c |
parent 29540 | 8858d197a9b6 |
child 29654 | 24e73987bfe2 |
child 29659 | f8d2c03ecfd8 |
child 29667 | 53103fc8ffa3 |
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 |
29475 | 211 |
poly_rec_pCons_eq_if [simp del, code del]: |
29454
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 |
||
29540 | 296 |
instance poly :: |
297 |
("{cancel_ab_semigroup_add,comm_monoid_add}") cancel_ab_semigroup_add |
|
298 |
proof |
|
299 |
fix p q r :: "'a poly" |
|
300 |
assume "p + q = p + r" thus "q = r" |
|
301 |
by (simp add: expand_poly_eq) |
|
302 |
qed |
|
303 |
||
29451 | 304 |
instantiation poly :: (ab_group_add) ab_group_add |
305 |
begin |
|
306 |
||
307 |
definition |
|
308 |
uminus_poly_def [code del]: |
|
309 |
"- p = Abs_poly (\<lambda>n. - coeff p n)" |
|
310 |
||
311 |
definition |
|
312 |
minus_poly_def [code del]: |
|
313 |
"p - q = Abs_poly (\<lambda>n. coeff p n - coeff q n)" |
|
314 |
||
315 |
lemma Poly_minus: |
|
316 |
fixes f :: "nat \<Rightarrow> 'a" |
|
317 |
shows "f \<in> Poly \<Longrightarrow> (\<lambda>n. - f n) \<in> Poly" |
|
318 |
unfolding Poly_def by simp |
|
319 |
||
320 |
lemma Poly_diff: |
|
321 |
fixes f g :: "nat \<Rightarrow> 'a" |
|
322 |
shows "\<lbrakk>f \<in> Poly; g \<in> Poly\<rbrakk> \<Longrightarrow> (\<lambda>n. f n - g n) \<in> Poly" |
|
323 |
unfolding diff_minus by (simp add: Poly_add Poly_minus) |
|
324 |
||
325 |
lemma coeff_minus [simp]: "coeff (- p) n = - coeff p n" |
|
326 |
unfolding uminus_poly_def |
|
327 |
by (simp add: Abs_poly_inverse coeff Poly_minus) |
|
328 |
||
329 |
lemma coeff_diff [simp]: |
|
330 |
"coeff (p - q) n = coeff p n - coeff q n" |
|
331 |
unfolding minus_poly_def |
|
332 |
by (simp add: Abs_poly_inverse coeff Poly_diff) |
|
333 |
||
334 |
instance proof |
|
335 |
fix p q :: "'a poly" |
|
336 |
show "- p + p = 0" |
|
337 |
by (simp add: expand_poly_eq) |
|
338 |
show "p - q = p + - q" |
|
339 |
by (simp add: expand_poly_eq diff_minus) |
|
340 |
qed |
|
341 |
||
342 |
end |
|
343 |
||
344 |
lemma add_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 minus_pCons [simp]: |
|
349 |
"- pCons a p = pCons (- a) (- p)" |
|
350 |
by (rule poly_ext, simp add: coeff_pCons split: nat.split) |
|
351 |
||
352 |
lemma diff_pCons [simp]: |
|
353 |
"pCons a p - pCons b q = pCons (a - b) (p - q)" |
|
354 |
by (rule poly_ext, simp add: coeff_pCons split: nat.split) |
|
355 |
||
29539 | 356 |
lemma degree_add_le_max: "degree (p + q) \<le> max (degree p) (degree q)" |
29451 | 357 |
by (rule degree_le, auto simp add: coeff_eq_0) |
358 |
||
29539 | 359 |
lemma degree_add_le: |
360 |
"\<lbrakk>degree p \<le> n; degree q \<le> n\<rbrakk> \<Longrightarrow> degree (p + q) \<le> n" |
|
361 |
by (auto intro: order_trans degree_add_le_max) |
|
362 |
||
29453 | 363 |
lemma degree_add_less: |
364 |
"\<lbrakk>degree p < n; degree q < n\<rbrakk> \<Longrightarrow> degree (p + q) < n" |
|
29539 | 365 |
by (auto intro: le_less_trans degree_add_le_max) |
29453 | 366 |
|
29451 | 367 |
lemma degree_add_eq_right: |
368 |
"degree p < degree q \<Longrightarrow> degree (p + q) = degree q" |
|
369 |
apply (cases "q = 0", simp) |
|
370 |
apply (rule order_antisym) |
|
29539 | 371 |
apply (simp add: degree_add_le) |
29451 | 372 |
apply (rule le_degree) |
373 |
apply (simp add: coeff_eq_0) |
|
374 |
done |
|
375 |
||
376 |
lemma degree_add_eq_left: |
|
377 |
"degree q < degree p \<Longrightarrow> degree (p + q) = degree p" |
|
378 |
using degree_add_eq_right [of q p] |
|
379 |
by (simp add: add_commute) |
|
380 |
||
381 |
lemma degree_minus [simp]: "degree (- p) = degree p" |
|
382 |
unfolding degree_def by simp |
|
383 |
||
29539 | 384 |
lemma degree_diff_le_max: "degree (p - q) \<le> max (degree p) (degree q)" |
29451 | 385 |
using degree_add_le [where p=p and q="-q"] |
386 |
by (simp add: diff_minus) |
|
387 |
||
29539 | 388 |
lemma degree_diff_le: |
389 |
"\<lbrakk>degree p \<le> n; degree q \<le> n\<rbrakk> \<Longrightarrow> degree (p - q) \<le> n" |
|
390 |
by (simp add: diff_minus degree_add_le) |
|
391 |
||
29453 | 392 |
lemma degree_diff_less: |
393 |
"\<lbrakk>degree p < n; degree q < n\<rbrakk> \<Longrightarrow> degree (p - q) < n" |
|
29539 | 394 |
by (simp add: diff_minus degree_add_less) |
29453 | 395 |
|
29451 | 396 |
lemma add_monom: "monom a n + monom b n = monom (a + b) n" |
397 |
by (rule poly_ext) simp |
|
398 |
||
399 |
lemma diff_monom: "monom a n - monom b n = monom (a - b) n" |
|
400 |
by (rule poly_ext) simp |
|
401 |
||
402 |
lemma minus_monom: "- monom a n = monom (-a) n" |
|
403 |
by (rule poly_ext) simp |
|
404 |
||
405 |
lemma coeff_setsum: "coeff (\<Sum>x\<in>A. p x) i = (\<Sum>x\<in>A. coeff (p x) i)" |
|
406 |
by (cases "finite A", induct set: finite, simp_all) |
|
407 |
||
408 |
lemma monom_setsum: "monom (\<Sum>x\<in>A. a x) n = (\<Sum>x\<in>A. monom (a x) n)" |
|
409 |
by (rule poly_ext) (simp add: coeff_setsum) |
|
410 |
||
411 |
||
412 |
subsection {* Multiplication by a constant *} |
|
413 |
||
414 |
definition |
|
415 |
smult :: "'a::comm_semiring_0 \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where |
|
416 |
"smult a p = Abs_poly (\<lambda>n. a * coeff p n)" |
|
417 |
||
418 |
lemma Poly_smult: |
|
419 |
fixes f :: "nat \<Rightarrow> 'a::comm_semiring_0" |
|
420 |
shows "f \<in> Poly \<Longrightarrow> (\<lambda>n. a * f n) \<in> Poly" |
|
421 |
unfolding Poly_def |
|
422 |
by (clarify, rule_tac x=n in exI, simp) |
|
423 |
||
424 |
lemma coeff_smult [simp]: "coeff (smult a p) n = a * coeff p n" |
|
425 |
unfolding smult_def |
|
426 |
by (simp add: Abs_poly_inverse Poly_smult coeff) |
|
427 |
||
428 |
lemma degree_smult_le: "degree (smult a p) \<le> degree p" |
|
429 |
by (rule degree_le, simp add: coeff_eq_0) |
|
430 |
||
29472 | 431 |
lemma smult_smult [simp]: "smult a (smult b p) = smult (a * b) p" |
29451 | 432 |
by (rule poly_ext, simp add: mult_assoc) |
433 |
||
434 |
lemma smult_0_right [simp]: "smult a 0 = 0" |
|
435 |
by (rule poly_ext, simp) |
|
436 |
||
437 |
lemma smult_0_left [simp]: "smult 0 p = 0" |
|
438 |
by (rule poly_ext, simp) |
|
439 |
||
440 |
lemma smult_1_left [simp]: "smult (1::'a::comm_semiring_1) p = p" |
|
441 |
by (rule poly_ext, simp) |
|
442 |
||
443 |
lemma smult_add_right: |
|
444 |
"smult a (p + q) = smult a p + smult a q" |
|
445 |
by (rule poly_ext, simp add: ring_simps) |
|
446 |
||
447 |
lemma smult_add_left: |
|
448 |
"smult (a + b) p = smult a p + smult b p" |
|
449 |
by (rule poly_ext, simp add: ring_simps) |
|
450 |
||
29457 | 451 |
lemma smult_minus_right [simp]: |
29451 | 452 |
"smult (a::'a::comm_ring) (- p) = - smult a p" |
453 |
by (rule poly_ext, simp) |
|
454 |
||
29457 | 455 |
lemma smult_minus_left [simp]: |
29451 | 456 |
"smult (- a::'a::comm_ring) p = - smult a p" |
457 |
by (rule poly_ext, simp) |
|
458 |
||
459 |
lemma smult_diff_right: |
|
460 |
"smult (a::'a::comm_ring) (p - q) = smult a p - smult a q" |
|
461 |
by (rule poly_ext, simp add: ring_simps) |
|
462 |
||
463 |
lemma smult_diff_left: |
|
464 |
"smult (a - b::'a::comm_ring) p = smult a p - smult b p" |
|
465 |
by (rule poly_ext, simp add: ring_simps) |
|
466 |
||
29472 | 467 |
lemmas smult_distribs = |
468 |
smult_add_left smult_add_right |
|
469 |
smult_diff_left smult_diff_right |
|
470 |
||
29451 | 471 |
lemma smult_pCons [simp]: |
472 |
"smult a (pCons b p) = pCons (a * b) (smult a p)" |
|
473 |
by (rule poly_ext, simp add: coeff_pCons split: nat.split) |
|
474 |
||
475 |
lemma smult_monom: "smult a (monom b n) = monom (a * b) n" |
|
476 |
by (induct n, simp add: monom_0, simp add: monom_Suc) |
|
477 |
||
478 |
||
479 |
subsection {* Multiplication of polynomials *} |
|
480 |
||
29474 | 481 |
text {* TODO: move to SetInterval.thy *} |
29451 | 482 |
lemma setsum_atMost_Suc_shift: |
483 |
fixes f :: "nat \<Rightarrow> 'a::comm_monoid_add" |
|
484 |
shows "(\<Sum>i\<le>Suc n. f i) = f 0 + (\<Sum>i\<le>n. f (Suc i))" |
|
485 |
proof (induct n) |
|
486 |
case 0 show ?case by simp |
|
487 |
next |
|
488 |
case (Suc n) note IH = this |
|
489 |
have "(\<Sum>i\<le>Suc (Suc n). f i) = (\<Sum>i\<le>Suc n. f i) + f (Suc (Suc n))" |
|
490 |
by (rule setsum_atMost_Suc) |
|
491 |
also have "(\<Sum>i\<le>Suc n. f i) = f 0 + (\<Sum>i\<le>n. f (Suc i))" |
|
492 |
by (rule IH) |
|
493 |
also have "f 0 + (\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)) = |
|
494 |
f 0 + ((\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)))" |
|
495 |
by (rule add_assoc) |
|
496 |
also have "(\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)) = (\<Sum>i\<le>Suc n. f (Suc i))" |
|
497 |
by (rule setsum_atMost_Suc [symmetric]) |
|
498 |
finally show ?case . |
|
499 |
qed |
|
500 |
||
501 |
instantiation poly :: (comm_semiring_0) comm_semiring_0 |
|
502 |
begin |
|
503 |
||
504 |
definition |
|
29475 | 505 |
times_poly_def [code del]: |
29474 | 506 |
"p * q = poly_rec 0 (\<lambda>a p pq. smult a q + pCons 0 pq) p" |
507 |
||
508 |
lemma mult_poly_0_left: "(0::'a poly) * q = 0" |
|
509 |
unfolding times_poly_def by (simp add: poly_rec_0) |
|
510 |
||
511 |
lemma mult_pCons_left [simp]: |
|
512 |
"pCons a p * q = smult a q + pCons 0 (p * q)" |
|
513 |
unfolding times_poly_def by (simp add: poly_rec_pCons) |
|
514 |
||
515 |
lemma mult_poly_0_right: "p * (0::'a poly) = 0" |
|
516 |
by (induct p, simp add: mult_poly_0_left, simp) |
|
29451 | 517 |
|
29474 | 518 |
lemma mult_pCons_right [simp]: |
519 |
"p * pCons a q = smult a p + pCons 0 (p * q)" |
|
520 |
by (induct p, simp add: mult_poly_0_left, simp add: ring_simps) |
|
521 |
||
522 |
lemmas mult_poly_0 = mult_poly_0_left mult_poly_0_right |
|
523 |
||
524 |
lemma mult_smult_left [simp]: "smult a p * q = smult a (p * q)" |
|
525 |
by (induct p, simp add: mult_poly_0, simp add: smult_add_right) |
|
526 |
||
527 |
lemma mult_smult_right [simp]: "p * smult a q = smult a (p * q)" |
|
528 |
by (induct q, simp add: mult_poly_0, simp add: smult_add_right) |
|
529 |
||
530 |
lemma mult_poly_add_left: |
|
531 |
fixes p q r :: "'a poly" |
|
532 |
shows "(p + q) * r = p * r + q * r" |
|
533 |
by (induct r, simp add: mult_poly_0, |
|
534 |
simp add: smult_distribs group_simps) |
|
29451 | 535 |
|
536 |
instance proof |
|
537 |
fix p q r :: "'a poly" |
|
538 |
show 0: "0 * p = 0" |
|
29474 | 539 |
by (rule mult_poly_0_left) |
29451 | 540 |
show "p * 0 = 0" |
29474 | 541 |
by (rule mult_poly_0_right) |
29451 | 542 |
show "(p + q) * r = p * r + q * r" |
29474 | 543 |
by (rule mult_poly_add_left) |
29451 | 544 |
show "(p * q) * r = p * (q * r)" |
29474 | 545 |
by (induct p, simp add: mult_poly_0, simp add: mult_poly_add_left) |
29451 | 546 |
show "p * q = q * p" |
29474 | 547 |
by (induct p, simp add: mult_poly_0, simp) |
29451 | 548 |
qed |
549 |
||
550 |
end |
|
551 |
||
29540 | 552 |
instance poly :: (comm_semiring_0_cancel) comm_semiring_0_cancel .. |
553 |
||
29474 | 554 |
lemma coeff_mult: |
555 |
"coeff (p * q) n = (\<Sum>i\<le>n. coeff p i * coeff q (n-i))" |
|
556 |
proof (induct p arbitrary: n) |
|
557 |
case 0 show ?case by simp |
|
558 |
next |
|
559 |
case (pCons a p n) thus ?case |
|
560 |
by (cases n, simp, simp add: setsum_atMost_Suc_shift |
|
561 |
del: setsum_atMost_Suc) |
|
562 |
qed |
|
29451 | 563 |
|
29474 | 564 |
lemma degree_mult_le: "degree (p * q) \<le> degree p + degree q" |
565 |
apply (rule degree_le) |
|
566 |
apply (induct p) |
|
567 |
apply simp |
|
568 |
apply (simp add: coeff_eq_0 coeff_pCons split: nat.split) |
|
29451 | 569 |
done |
570 |
||
571 |
lemma mult_monom: "monom a m * monom b n = monom (a * b) (m + n)" |
|
572 |
by (induct m, simp add: monom_0 smult_monom, simp add: monom_Suc) |
|
573 |
||
574 |
||
575 |
subsection {* The unit polynomial and exponentiation *} |
|
576 |
||
577 |
instantiation poly :: (comm_semiring_1) comm_semiring_1 |
|
578 |
begin |
|
579 |
||
580 |
definition |
|
581 |
one_poly_def: |
|
582 |
"1 = pCons 1 0" |
|
583 |
||
584 |
instance proof |
|
585 |
fix p :: "'a poly" show "1 * p = p" |
|
586 |
unfolding one_poly_def |
|
587 |
by simp |
|
588 |
next |
|
589 |
show "0 \<noteq> (1::'a poly)" |
|
590 |
unfolding one_poly_def by simp |
|
591 |
qed |
|
592 |
||
593 |
end |
|
594 |
||
29540 | 595 |
instance poly :: (comm_semiring_1_cancel) comm_semiring_1_cancel .. |
596 |
||
29451 | 597 |
lemma coeff_1 [simp]: "coeff 1 n = (if n = 0 then 1 else 0)" |
598 |
unfolding one_poly_def |
|
599 |
by (simp add: coeff_pCons split: nat.split) |
|
600 |
||
601 |
lemma degree_1 [simp]: "degree 1 = 0" |
|
602 |
unfolding one_poly_def |
|
603 |
by (rule degree_pCons_0) |
|
604 |
||
605 |
instantiation poly :: (comm_semiring_1) recpower |
|
606 |
begin |
|
607 |
||
608 |
primrec power_poly where |
|
609 |
power_poly_0: "(p::'a poly) ^ 0 = 1" |
|
610 |
| power_poly_Suc: "(p::'a poly) ^ (Suc n) = p * p ^ n" |
|
611 |
||
612 |
instance |
|
613 |
by default simp_all |
|
614 |
||
615 |
end |
|
616 |
||
617 |
instance poly :: (comm_ring) comm_ring .. |
|
618 |
||
619 |
instance poly :: (comm_ring_1) comm_ring_1 .. |
|
620 |
||
621 |
instantiation poly :: (comm_ring_1) number_ring |
|
622 |
begin |
|
623 |
||
624 |
definition |
|
625 |
"number_of k = (of_int k :: 'a poly)" |
|
626 |
||
627 |
instance |
|
628 |
by default (rule number_of_poly_def) |
|
629 |
||
630 |
end |
|
631 |
||
632 |
||
633 |
subsection {* Polynomials form an integral domain *} |
|
634 |
||
635 |
lemma coeff_mult_degree_sum: |
|
636 |
"coeff (p * q) (degree p + degree q) = |
|
637 |
coeff p (degree p) * coeff q (degree q)" |
|
29471 | 638 |
by (induct p, simp, simp add: coeff_eq_0) |
29451 | 639 |
|
640 |
instance poly :: (idom) idom |
|
641 |
proof |
|
642 |
fix p q :: "'a poly" |
|
643 |
assume "p \<noteq> 0" and "q \<noteq> 0" |
|
644 |
have "coeff (p * q) (degree p + degree q) = |
|
645 |
coeff p (degree p) * coeff q (degree q)" |
|
646 |
by (rule coeff_mult_degree_sum) |
|
647 |
also have "coeff p (degree p) * coeff q (degree q) \<noteq> 0" |
|
648 |
using `p \<noteq> 0` and `q \<noteq> 0` by simp |
|
649 |
finally have "\<exists>n. coeff (p * q) n \<noteq> 0" .. |
|
650 |
thus "p * q \<noteq> 0" by (simp add: expand_poly_eq) |
|
651 |
qed |
|
652 |
||
653 |
lemma degree_mult_eq: |
|
654 |
fixes p q :: "'a::idom poly" |
|
655 |
shows "\<lbrakk>p \<noteq> 0; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree (p * q) = degree p + degree q" |
|
656 |
apply (rule order_antisym [OF degree_mult_le le_degree]) |
|
657 |
apply (simp add: coeff_mult_degree_sum) |
|
658 |
done |
|
659 |
||
660 |
lemma dvd_imp_degree_le: |
|
661 |
fixes p q :: "'a::idom poly" |
|
662 |
shows "\<lbrakk>p dvd q; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree p \<le> degree q" |
|
663 |
by (erule dvdE, simp add: degree_mult_eq) |
|
664 |
||
665 |
||
666 |
subsection {* Long division of polynomials *} |
|
667 |
||
668 |
definition |
|
29537 | 669 |
pdivmod_rel :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> bool" |
29451 | 670 |
where |
29475 | 671 |
[code del]: |
29537 | 672 |
"pdivmod_rel x y q r \<longleftrightarrow> |
29451 | 673 |
x = q * y + r \<and> (if y = 0 then q = 0 else r = 0 \<or> degree r < degree y)" |
674 |
||
29537 | 675 |
lemma pdivmod_rel_0: |
676 |
"pdivmod_rel 0 y 0 0" |
|
677 |
unfolding pdivmod_rel_def by simp |
|
29451 | 678 |
|
29537 | 679 |
lemma pdivmod_rel_by_0: |
680 |
"pdivmod_rel x 0 0 x" |
|
681 |
unfolding pdivmod_rel_def by simp |
|
29451 | 682 |
|
683 |
lemma eq_zero_or_degree_less: |
|
684 |
assumes "degree p \<le> n" and "coeff p n = 0" |
|
685 |
shows "p = 0 \<or> degree p < n" |
|
686 |
proof (cases n) |
|
687 |
case 0 |
|
688 |
with `degree p \<le> n` and `coeff p n = 0` |
|
689 |
have "coeff p (degree p) = 0" by simp |
|
690 |
then have "p = 0" by simp |
|
691 |
then show ?thesis .. |
|
692 |
next |
|
693 |
case (Suc m) |
|
694 |
have "\<forall>i>n. coeff p i = 0" |
|
695 |
using `degree p \<le> n` by (simp add: coeff_eq_0) |
|
696 |
then have "\<forall>i\<ge>n. coeff p i = 0" |
|
697 |
using `coeff p n = 0` by (simp add: le_less) |
|
698 |
then have "\<forall>i>m. coeff p i = 0" |
|
699 |
using `n = Suc m` by (simp add: less_eq_Suc_le) |
|
700 |
then have "degree p \<le> m" |
|
701 |
by (rule degree_le) |
|
702 |
then have "degree p < n" |
|
703 |
using `n = Suc m` by (simp add: less_Suc_eq_le) |
|
704 |
then show ?thesis .. |
|
705 |
qed |
|
706 |
||
29537 | 707 |
lemma pdivmod_rel_pCons: |
708 |
assumes rel: "pdivmod_rel x y q r" |
|
29451 | 709 |
assumes y: "y \<noteq> 0" |
710 |
assumes b: "b = coeff (pCons a r) (degree y) / coeff y (degree y)" |
|
29537 | 711 |
shows "pdivmod_rel (pCons a x) y (pCons b q) (pCons a r - smult b y)" |
712 |
(is "pdivmod_rel ?x y ?q ?r") |
|
29451 | 713 |
proof - |
714 |
have x: "x = q * y + r" and r: "r = 0 \<or> degree r < degree y" |
|
29537 | 715 |
using assms unfolding pdivmod_rel_def by simp_all |
29451 | 716 |
|
717 |
have 1: "?x = ?q * y + ?r" |
|
718 |
using b x by simp |
|
719 |
||
720 |
have 2: "?r = 0 \<or> degree ?r < degree y" |
|
721 |
proof (rule eq_zero_or_degree_less) |
|
29539 | 722 |
show "degree ?r \<le> degree y" |
723 |
proof (rule degree_diff_le) |
|
29451 | 724 |
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
|
725 |
using r by auto |
29451 | 726 |
show "degree (smult b y) \<le> degree y" |
727 |
by (rule degree_smult_le) |
|
728 |
qed |
|
729 |
next |
|
730 |
show "coeff ?r (degree y) = 0" |
|
731 |
using `y \<noteq> 0` unfolding b by simp |
|
732 |
qed |
|
733 |
||
734 |
from 1 2 show ?thesis |
|
29537 | 735 |
unfolding pdivmod_rel_def |
29451 | 736 |
using `y \<noteq> 0` by simp |
737 |
qed |
|
738 |
||
29537 | 739 |
lemma pdivmod_rel_exists: "\<exists>q r. pdivmod_rel x y q r" |
29451 | 740 |
apply (cases "y = 0") |
29537 | 741 |
apply (fast intro!: pdivmod_rel_by_0) |
29451 | 742 |
apply (induct x) |
29537 | 743 |
apply (fast intro!: pdivmod_rel_0) |
744 |
apply (fast intro!: pdivmod_rel_pCons) |
|
29451 | 745 |
done |
746 |
||
29537 | 747 |
lemma pdivmod_rel_unique: |
748 |
assumes 1: "pdivmod_rel x y q1 r1" |
|
749 |
assumes 2: "pdivmod_rel x y q2 r2" |
|
29451 | 750 |
shows "q1 = q2 \<and> r1 = r2" |
751 |
proof (cases "y = 0") |
|
752 |
assume "y = 0" with assms show ?thesis |
|
29537 | 753 |
by (simp add: pdivmod_rel_def) |
29451 | 754 |
next |
755 |
assume [simp]: "y \<noteq> 0" |
|
756 |
from 1 have q1: "x = q1 * y + r1" and r1: "r1 = 0 \<or> degree r1 < degree y" |
|
29537 | 757 |
unfolding pdivmod_rel_def by simp_all |
29451 | 758 |
from 2 have q2: "x = q2 * y + r2" and r2: "r2 = 0 \<or> degree r2 < degree y" |
29537 | 759 |
unfolding pdivmod_rel_def by simp_all |
29451 | 760 |
from q1 q2 have q3: "(q1 - q2) * y = r2 - r1" |
761 |
by (simp add: ring_simps) |
|
762 |
from r1 r2 have r3: "(r2 - r1) = 0 \<or> degree (r2 - r1) < degree y" |
|
29453 | 763 |
by (auto intro: degree_diff_less) |
29451 | 764 |
|
765 |
show "q1 = q2 \<and> r1 = r2" |
|
766 |
proof (rule ccontr) |
|
767 |
assume "\<not> (q1 = q2 \<and> r1 = r2)" |
|
768 |
with q3 have "q1 \<noteq> q2" and "r1 \<noteq> r2" by auto |
|
769 |
with r3 have "degree (r2 - r1) < degree y" by simp |
|
770 |
also have "degree y \<le> degree (q1 - q2) + degree y" by simp |
|
771 |
also have "\<dots> = degree ((q1 - q2) * y)" |
|
772 |
using `q1 \<noteq> q2` by (simp add: degree_mult_eq) |
|
773 |
also have "\<dots> = degree (r2 - r1)" |
|
774 |
using q3 by simp |
|
775 |
finally have "degree (r2 - r1) < degree (r2 - r1)" . |
|
776 |
then show "False" by simp |
|
777 |
qed |
|
778 |
qed |
|
779 |
||
29537 | 780 |
lemmas pdivmod_rel_unique_div = |
781 |
pdivmod_rel_unique [THEN conjunct1, standard] |
|
29451 | 782 |
|
29537 | 783 |
lemmas pdivmod_rel_unique_mod = |
784 |
pdivmod_rel_unique [THEN conjunct2, standard] |
|
29451 | 785 |
|
786 |
instantiation poly :: (field) ring_div |
|
787 |
begin |
|
788 |
||
789 |
definition div_poly where |
|
29537 | 790 |
[code del]: "x div y = (THE q. \<exists>r. pdivmod_rel x y q r)" |
29451 | 791 |
|
792 |
definition mod_poly where |
|
29537 | 793 |
[code del]: "x mod y = (THE r. \<exists>q. pdivmod_rel x y q r)" |
29451 | 794 |
|
795 |
lemma div_poly_eq: |
|
29537 | 796 |
"pdivmod_rel x y q r \<Longrightarrow> x div y = q" |
29451 | 797 |
unfolding div_poly_def |
29537 | 798 |
by (fast elim: pdivmod_rel_unique_div) |
29451 | 799 |
|
800 |
lemma mod_poly_eq: |
|
29537 | 801 |
"pdivmod_rel x y q r \<Longrightarrow> x mod y = r" |
29451 | 802 |
unfolding mod_poly_def |
29537 | 803 |
by (fast elim: pdivmod_rel_unique_mod) |
29451 | 804 |
|
29537 | 805 |
lemma pdivmod_rel: |
806 |
"pdivmod_rel x y (x div y) (x mod y)" |
|
29451 | 807 |
proof - |
29537 | 808 |
from pdivmod_rel_exists |
809 |
obtain q r where "pdivmod_rel x y q r" by fast |
|
29451 | 810 |
thus ?thesis |
811 |
by (simp add: div_poly_eq mod_poly_eq) |
|
812 |
qed |
|
813 |
||
814 |
instance proof |
|
815 |
fix x y :: "'a poly" |
|
816 |
show "x div y * y + x mod y = x" |
|
29537 | 817 |
using pdivmod_rel [of x y] |
818 |
by (simp add: pdivmod_rel_def) |
|
29451 | 819 |
next |
820 |
fix x :: "'a poly" |
|
29537 | 821 |
have "pdivmod_rel x 0 0 x" |
822 |
by (rule pdivmod_rel_by_0) |
|
29451 | 823 |
thus "x div 0 = 0" |
824 |
by (rule div_poly_eq) |
|
825 |
next |
|
826 |
fix y :: "'a poly" |
|
29537 | 827 |
have "pdivmod_rel 0 y 0 0" |
828 |
by (rule pdivmod_rel_0) |
|
29451 | 829 |
thus "0 div y = 0" |
830 |
by (rule div_poly_eq) |
|
831 |
next |
|
832 |
fix x y z :: "'a poly" |
|
833 |
assume "y \<noteq> 0" |
|
29537 | 834 |
hence "pdivmod_rel (x + z * y) y (z + x div y) (x mod y)" |
835 |
using pdivmod_rel [of x y] |
|
836 |
by (simp add: pdivmod_rel_def left_distrib) |
|
29451 | 837 |
thus "(x + z * y) div y = z + x div y" |
838 |
by (rule div_poly_eq) |
|
839 |
qed |
|
840 |
||
841 |
end |
|
842 |
||
843 |
lemma degree_mod_less: |
|
844 |
"y \<noteq> 0 \<Longrightarrow> x mod y = 0 \<or> degree (x mod y) < degree y" |
|
29537 | 845 |
using pdivmod_rel [of x y] |
846 |
unfolding pdivmod_rel_def by simp |
|
29451 | 847 |
|
848 |
lemma div_poly_less: "degree x < degree y \<Longrightarrow> x div y = 0" |
|
849 |
proof - |
|
850 |
assume "degree x < degree y" |
|
29537 | 851 |
hence "pdivmod_rel x y 0 x" |
852 |
by (simp add: pdivmod_rel_def) |
|
29451 | 853 |
thus "x div y = 0" by (rule div_poly_eq) |
854 |
qed |
|
855 |
||
856 |
lemma mod_poly_less: "degree x < degree y \<Longrightarrow> x mod y = x" |
|
857 |
proof - |
|
858 |
assume "degree x < degree y" |
|
29537 | 859 |
hence "pdivmod_rel x y 0 x" |
860 |
by (simp add: pdivmod_rel_def) |
|
29451 | 861 |
thus "x mod y = x" by (rule mod_poly_eq) |
862 |
qed |
|
863 |
||
864 |
lemma mod_pCons: |
|
865 |
fixes a and x |
|
866 |
assumes y: "y \<noteq> 0" |
|
867 |
defines b: "b \<equiv> coeff (pCons a (x mod y)) (degree y) / coeff y (degree y)" |
|
868 |
shows "(pCons a x) mod y = (pCons a (x mod y) - smult b y)" |
|
869 |
unfolding b |
|
870 |
apply (rule mod_poly_eq) |
|
29537 | 871 |
apply (rule pdivmod_rel_pCons [OF pdivmod_rel y refl]) |
29451 | 872 |
done |
873 |
||
874 |
||
875 |
subsection {* Evaluation of polynomials *} |
|
876 |
||
877 |
definition |
|
29454
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
878 |
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
|
879 |
"poly = poly_rec (\<lambda>x. 0) (\<lambda>a p f x. a + x * f x)" |
29451 | 880 |
|
881 |
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
|
882 |
unfolding poly_def by (simp add: poly_rec_0) |
29451 | 883 |
|
884 |
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
|
885 |
unfolding poly_def by (simp add: poly_rec_pCons) |
29451 | 886 |
|
887 |
lemma poly_1 [simp]: "poly 1 x = 1" |
|
888 |
unfolding one_poly_def by simp |
|
889 |
||
29454
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
890 |
lemma poly_monom: |
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
891 |
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
|
892 |
shows "poly (monom a n) x = a * x ^ n" |
29451 | 893 |
by (induct n, simp add: monom_0, simp add: monom_Suc power_Suc mult_ac) |
894 |
||
895 |
lemma poly_add [simp]: "poly (p + q) x = poly p x + poly q x" |
|
896 |
apply (induct p arbitrary: q, simp) |
|
897 |
apply (case_tac q, simp, simp add: ring_simps) |
|
898 |
done |
|
899 |
||
900 |
lemma poly_minus [simp]: |
|
29454
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
901 |
fixes x :: "'a::comm_ring" |
29451 | 902 |
shows "poly (- p) x = - poly p x" |
903 |
by (induct p, simp_all) |
|
904 |
||
905 |
lemma poly_diff [simp]: |
|
29454
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents:
29453
diff
changeset
|
906 |
fixes x :: "'a::comm_ring" |
29451 | 907 |
shows "poly (p - q) x = poly p x - poly q x" |
908 |
by (simp add: diff_minus) |
|
909 |
||
910 |
lemma poly_setsum: "poly (\<Sum>k\<in>A. p k) x = (\<Sum>k\<in>A. poly (p k) x)" |
|
911 |
by (cases "finite A", induct set: finite, simp_all) |
|
912 |
||
913 |
lemma poly_smult [simp]: "poly (smult a p) x = a * poly p x" |
|
914 |
by (induct p, simp, simp add: ring_simps) |
|
915 |
||
916 |
lemma poly_mult [simp]: "poly (p * q) x = poly p x * poly q x" |
|
917 |
by (induct p, simp_all, simp add: ring_simps) |
|
918 |
||
29462 | 919 |
lemma poly_power [simp]: |
920 |
fixes p :: "'a::{comm_semiring_1,recpower} poly" |
|
921 |
shows "poly (p ^ n) x = poly p x ^ n" |
|
922 |
by (induct n, simp, simp add: power_Suc) |
|
923 |
||
29456 | 924 |
|
925 |
subsection {* Synthetic division *} |
|
926 |
||
927 |
definition |
|
928 |
synthetic_divmod :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly \<times> 'a" |
|
29478 | 929 |
where [code del]: |
29456 | 930 |
"synthetic_divmod p c = |
931 |
poly_rec (0, 0) (\<lambda>a p (q, r). (pCons r q, a + c * r)) p" |
|
932 |
||
933 |
definition |
|
934 |
synthetic_div :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly" |
|
935 |
where |
|
936 |
"synthetic_div p c = fst (synthetic_divmod p c)" |
|
937 |
||
938 |
lemma synthetic_divmod_0 [simp]: |
|
939 |
"synthetic_divmod 0 c = (0, 0)" |
|
940 |
unfolding synthetic_divmod_def |
|
941 |
by (simp add: poly_rec_0) |
|
942 |
||
943 |
lemma synthetic_divmod_pCons [simp]: |
|
944 |
"synthetic_divmod (pCons a p) c = |
|
945 |
(\<lambda>(q, r). (pCons r q, a + c * r)) (synthetic_divmod p c)" |
|
946 |
unfolding synthetic_divmod_def |
|
947 |
by (simp add: poly_rec_pCons) |
|
948 |
||
949 |
lemma snd_synthetic_divmod: "snd (synthetic_divmod p c) = poly p c" |
|
950 |
by (induct p, simp, simp add: split_def) |
|
951 |
||
952 |
lemma synthetic_div_0 [simp]: "synthetic_div 0 c = 0" |
|
953 |
unfolding synthetic_div_def by simp |
|
954 |
||
955 |
lemma synthetic_div_pCons [simp]: |
|
956 |
"synthetic_div (pCons a p) c = pCons (poly p c) (synthetic_div p c)" |
|
957 |
unfolding synthetic_div_def |
|
958 |
by (simp add: split_def snd_synthetic_divmod) |
|
959 |
||
29460
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
960 |
lemma synthetic_div_eq_0_iff: |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
961 |
"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
|
962 |
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
|
963 |
|
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
964 |
lemma degree_synthetic_div: |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
965 |
"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
|
966 |
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
|
967 |
|
29457 | 968 |
lemma synthetic_div_correct: |
29456 | 969 |
"p + smult c (synthetic_div p c) = pCons (poly p c) (synthetic_div p c)" |
970 |
by (induct p) simp_all |
|
971 |
||
29457 | 972 |
lemma synthetic_div_unique_lemma: "smult c p = pCons a p \<Longrightarrow> p = 0" |
973 |
by (induct p arbitrary: a) simp_all |
|
974 |
||
975 |
lemma synthetic_div_unique: |
|
976 |
"p + smult c q = pCons r q \<Longrightarrow> r = poly p c \<and> q = synthetic_div p c" |
|
977 |
apply (induct p arbitrary: q r) |
|
978 |
apply (simp, frule synthetic_div_unique_lemma, simp) |
|
979 |
apply (case_tac q, force) |
|
980 |
done |
|
981 |
||
982 |
lemma synthetic_div_correct': |
|
983 |
fixes c :: "'a::comm_ring_1" |
|
984 |
shows "[:-c, 1:] * synthetic_div p c + [:poly p c:] = p" |
|
985 |
using synthetic_div_correct [of p c] |
|
986 |
by (simp add: group_simps) |
|
987 |
||
29460
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
988 |
lemma poly_eq_0_iff_dvd: |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
989 |
fixes c :: "'a::idom" |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
990 |
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
|
991 |
proof |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
992 |
assume "poly p c = 0" |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
993 |
with synthetic_div_correct' [of c p] |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
994 |
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
|
995 |
then show "[:-c, 1:] dvd p" .. |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
996 |
next |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
997 |
assume "[:-c, 1:] dvd p" |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
998 |
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
|
999 |
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
|
1000 |
qed |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
1001 |
|
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
1002 |
lemma dvd_iff_poly_eq_0: |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
1003 |
fixes c :: "'a::idom" |
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents:
29457
diff
changeset
|
1004 |
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
|
1005 |
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
|
1006 |
|
29462 | 1007 |
lemma poly_roots_finite: |
1008 |
fixes p :: "'a::idom poly" |
|
1009 |
shows "p \<noteq> 0 \<Longrightarrow> finite {x. poly p x = 0}" |
|
1010 |
proof (induct n \<equiv> "degree p" arbitrary: p) |
|
1011 |
case (0 p) |
|
1012 |
then obtain a where "a \<noteq> 0" and "p = [:a:]" |
|
1013 |
by (cases p, simp split: if_splits) |
|
1014 |
then show "finite {x. poly p x = 0}" by simp |
|
1015 |
next |
|
1016 |
case (Suc n p) |
|
1017 |
show "finite {x. poly p x = 0}" |
|
1018 |
proof (cases "\<exists>x. poly p x = 0") |
|
1019 |
case False |
|
1020 |
then show "finite {x. poly p x = 0}" by simp |
|
1021 |
next |
|
1022 |
case True |
|
1023 |
then obtain a where "poly p a = 0" .. |
|
1024 |
then have "[:-a, 1:] dvd p" by (simp only: poly_eq_0_iff_dvd) |
|
1025 |
then obtain k where k: "p = [:-a, 1:] * k" .. |
|
1026 |
with `p \<noteq> 0` have "k \<noteq> 0" by auto |
|
1027 |
with k have "degree p = Suc (degree k)" |
|
1028 |
by (simp add: degree_mult_eq del: mult_pCons_left) |
|
1029 |
with `Suc n = degree p` have "n = degree k" by simp |
|
1030 |
with `k \<noteq> 0` have "finite {x. poly k x = 0}" by (rule Suc.hyps) |
|
1031 |
then have "finite (insert a {x. poly k x = 0})" by simp |
|
1032 |
then show "finite {x. poly p x = 0}" |
|
1033 |
by (simp add: k uminus_add_conv_diff Collect_disj_eq |
|
1034 |
del: mult_pCons_left) |
|
1035 |
qed |
|
1036 |
qed |
|
1037 |
||
29478 | 1038 |
|
1039 |
subsection {* Configuration of the code generator *} |
|
1040 |
||
1041 |
code_datatype "0::'a::zero poly" pCons |
|
1042 |
||
29480 | 1043 |
declare pCons_0_0 [code post] |
1044 |
||
29478 | 1045 |
instantiation poly :: ("{zero,eq}") eq |
1046 |
begin |
|
1047 |
||
1048 |
definition [code del]: |
|
1049 |
"eq_class.eq (p::'a poly) q \<longleftrightarrow> p = q" |
|
1050 |
||
1051 |
instance |
|
1052 |
by default (rule eq_poly_def) |
|
1053 |
||
29451 | 1054 |
end |
29478 | 1055 |
|
1056 |
lemma eq_poly_code [code]: |
|
1057 |
"eq_class.eq (0::_ poly) (0::_ poly) \<longleftrightarrow> True" |
|
1058 |
"eq_class.eq (0::_ poly) (pCons b q) \<longleftrightarrow> eq_class.eq 0 b \<and> eq_class.eq 0 q" |
|
1059 |
"eq_class.eq (pCons a p) (0::_ poly) \<longleftrightarrow> eq_class.eq a 0 \<and> eq_class.eq p 0" |
|
1060 |
"eq_class.eq (pCons a p) (pCons b q) \<longleftrightarrow> eq_class.eq a b \<and> eq_class.eq p q" |
|
1061 |
unfolding eq by simp_all |
|
1062 |
||
1063 |
lemmas coeff_code [code] = |
|
1064 |
coeff_0 coeff_pCons_0 coeff_pCons_Suc |
|
1065 |
||
1066 |
lemmas degree_code [code] = |
|
1067 |
degree_0 degree_pCons_eq_if |
|
1068 |
||
1069 |
lemmas monom_poly_code [code] = |
|
1070 |
monom_0 monom_Suc |
|
1071 |
||
1072 |
lemma add_poly_code [code]: |
|
1073 |
"0 + q = (q :: _ poly)" |
|
1074 |
"p + 0 = (p :: _ poly)" |
|
1075 |
"pCons a p + pCons b q = pCons (a + b) (p + q)" |
|
1076 |
by simp_all |
|
1077 |
||
1078 |
lemma minus_poly_code [code]: |
|
1079 |
"- 0 = (0 :: _ poly)" |
|
1080 |
"- pCons a p = pCons (- a) (- p)" |
|
1081 |
by simp_all |
|
1082 |
||
1083 |
lemma diff_poly_code [code]: |
|
1084 |
"0 - q = (- q :: _ poly)" |
|
1085 |
"p - 0 = (p :: _ poly)" |
|
1086 |
"pCons a p - pCons b q = pCons (a - b) (p - q)" |
|
1087 |
by simp_all |
|
1088 |
||
1089 |
lemmas smult_poly_code [code] = |
|
1090 |
smult_0_right smult_pCons |
|
1091 |
||
1092 |
lemma mult_poly_code [code]: |
|
1093 |
"0 * q = (0 :: _ poly)" |
|
1094 |
"pCons a p * q = smult a q + pCons 0 (p * q)" |
|
1095 |
by simp_all |
|
1096 |
||
1097 |
lemmas poly_code [code] = |
|
1098 |
poly_0 poly_pCons |
|
1099 |
||
1100 |
lemmas synthetic_divmod_code [code] = |
|
1101 |
synthetic_divmod_0 synthetic_divmod_pCons |
|
1102 |
||
1103 |
text {* code generator setup for div and mod *} |
|
1104 |
||
1105 |
definition |
|
29537 | 1106 |
pdivmod :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<times> 'a poly" |
29478 | 1107 |
where |
29537 | 1108 |
[code del]: "pdivmod x y = (x div y, x mod y)" |
29478 | 1109 |
|
29537 | 1110 |
lemma div_poly_code [code]: "x div y = fst (pdivmod x y)" |
1111 |
unfolding pdivmod_def by simp |
|
29478 | 1112 |
|
29537 | 1113 |
lemma mod_poly_code [code]: "x mod y = snd (pdivmod x y)" |
1114 |
unfolding pdivmod_def by simp |
|
29478 | 1115 |
|
29537 | 1116 |
lemma pdivmod_0 [code]: "pdivmod 0 y = (0, 0)" |
1117 |
unfolding pdivmod_def by simp |
|
29478 | 1118 |
|
29537 | 1119 |
lemma pdivmod_pCons [code]: |
1120 |
"pdivmod (pCons a x) y = |
|
29478 | 1121 |
(if y = 0 then (0, pCons a x) else |
29537 | 1122 |
(let (q, r) = pdivmod x y; |
29478 | 1123 |
b = coeff (pCons a r) (degree y) / coeff y (degree y) |
1124 |
in (pCons b q, pCons a r - smult b y)))" |
|
29537 | 1125 |
apply (simp add: pdivmod_def Let_def, safe) |
29478 | 1126 |
apply (rule div_poly_eq) |
29537 | 1127 |
apply (erule pdivmod_rel_pCons [OF pdivmod_rel _ refl]) |
29478 | 1128 |
apply (rule mod_poly_eq) |
29537 | 1129 |
apply (erule pdivmod_rel_pCons [OF pdivmod_rel _ refl]) |
29478 | 1130 |
done |
1131 |
||
1132 |
end |