| author | chaieb | 
| Fri, 20 Feb 2009 13:14:57 +0000 | |
| changeset 30005 | 7d97e20728d4 | 
| parent 29987 | 391dcbd7e4dd | 
| child 30072 | 4eecd8b9b6cf | 
| 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  | 
|
| 
29654
 
24e73987bfe2
Plain, Main form meeting points in import hierarchy
 
haftmann 
parents: 
29540 
diff
changeset
 | 
9  | 
imports Plain SetInterval Main  | 
| 29451 | 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  | 
||
| 29904 | 296  | 
instance poly :: (cancel_comm_monoid_add) cancel_comm_monoid_add  | 
| 29540 | 297  | 
proof  | 
298  | 
fix p q r :: "'a poly"  | 
|
299  | 
assume "p + q = p + r" thus "q = r"  | 
|
300  | 
by (simp add: expand_poly_eq)  | 
|
301  | 
qed  | 
|
302  | 
||
| 29451 | 303  | 
instantiation poly :: (ab_group_add) ab_group_add  | 
304  | 
begin  | 
|
305  | 
||
306  | 
definition  | 
|
307  | 
uminus_poly_def [code del]:  | 
|
308  | 
"- p = Abs_poly (\<lambda>n. - coeff p n)"  | 
|
309  | 
||
310  | 
definition  | 
|
311  | 
minus_poly_def [code del]:  | 
|
312  | 
"p - q = Abs_poly (\<lambda>n. coeff p n - coeff q n)"  | 
|
313  | 
||
314  | 
lemma Poly_minus:  | 
|
315  | 
fixes f :: "nat \<Rightarrow> 'a"  | 
|
316  | 
shows "f \<in> Poly \<Longrightarrow> (\<lambda>n. - f n) \<in> Poly"  | 
|
317  | 
unfolding Poly_def by simp  | 
|
318  | 
||
319  | 
lemma Poly_diff:  | 
|
320  | 
fixes f g :: "nat \<Rightarrow> 'a"  | 
|
321  | 
shows "\<lbrakk>f \<in> Poly; g \<in> Poly\<rbrakk> \<Longrightarrow> (\<lambda>n. f n - g n) \<in> Poly"  | 
|
322  | 
unfolding diff_minus by (simp add: Poly_add Poly_minus)  | 
|
323  | 
||
324  | 
lemma coeff_minus [simp]: "coeff (- p) n = - coeff p n"  | 
|
325  | 
unfolding uminus_poly_def  | 
|
326  | 
by (simp add: Abs_poly_inverse coeff Poly_minus)  | 
|
327  | 
||
328  | 
lemma coeff_diff [simp]:  | 
|
329  | 
"coeff (p - q) n = coeff p n - coeff q n"  | 
|
330  | 
unfolding minus_poly_def  | 
|
331  | 
by (simp add: Abs_poly_inverse coeff Poly_diff)  | 
|
332  | 
||
333  | 
instance proof  | 
|
334  | 
fix p q :: "'a poly"  | 
|
335  | 
show "- p + p = 0"  | 
|
336  | 
by (simp add: expand_poly_eq)  | 
|
337  | 
show "p - q = p + - q"  | 
|
338  | 
by (simp add: expand_poly_eq diff_minus)  | 
|
339  | 
qed  | 
|
340  | 
||
341  | 
end  | 
|
342  | 
||
343  | 
lemma add_pCons [simp]:  | 
|
344  | 
"pCons a p + pCons b q = pCons (a + b) (p + q)"  | 
|
345  | 
by (rule poly_ext, simp add: coeff_pCons split: nat.split)  | 
|
346  | 
||
347  | 
lemma minus_pCons [simp]:  | 
|
348  | 
"- pCons a p = pCons (- a) (- p)"  | 
|
349  | 
by (rule poly_ext, simp add: coeff_pCons split: nat.split)  | 
|
350  | 
||
351  | 
lemma diff_pCons [simp]:  | 
|
352  | 
"pCons a p - pCons b q = pCons (a - b) (p - q)"  | 
|
353  | 
by (rule poly_ext, simp add: coeff_pCons split: nat.split)  | 
|
354  | 
||
| 29539 | 355  | 
lemma degree_add_le_max: "degree (p + q) \<le> max (degree p) (degree q)"  | 
| 29451 | 356  | 
by (rule degree_le, auto simp add: coeff_eq_0)  | 
357  | 
||
| 29539 | 358  | 
lemma degree_add_le:  | 
359  | 
"\<lbrakk>degree p \<le> n; degree q \<le> n\<rbrakk> \<Longrightarrow> degree (p + q) \<le> n"  | 
|
360  | 
by (auto intro: order_trans degree_add_le_max)  | 
|
361  | 
||
| 29453 | 362  | 
lemma degree_add_less:  | 
363  | 
"\<lbrakk>degree p < n; degree q < n\<rbrakk> \<Longrightarrow> degree (p + q) < n"  | 
|
| 29539 | 364  | 
by (auto intro: le_less_trans degree_add_le_max)  | 
| 29453 | 365  | 
|
| 29451 | 366  | 
lemma degree_add_eq_right:  | 
367  | 
"degree p < degree q \<Longrightarrow> degree (p + q) = degree q"  | 
|
368  | 
apply (cases "q = 0", simp)  | 
|
369  | 
apply (rule order_antisym)  | 
|
| 29539 | 370  | 
apply (simp add: degree_add_le)  | 
| 29451 | 371  | 
apply (rule le_degree)  | 
372  | 
apply (simp add: coeff_eq_0)  | 
|
373  | 
done  | 
|
374  | 
||
375  | 
lemma degree_add_eq_left:  | 
|
376  | 
"degree q < degree p \<Longrightarrow> degree (p + q) = degree p"  | 
|
377  | 
using degree_add_eq_right [of q p]  | 
|
378  | 
by (simp add: add_commute)  | 
|
379  | 
||
380  | 
lemma degree_minus [simp]: "degree (- p) = degree p"  | 
|
381  | 
unfolding degree_def by simp  | 
|
382  | 
||
| 29539 | 383  | 
lemma degree_diff_le_max: "degree (p - q) \<le> max (degree p) (degree q)"  | 
| 29451 | 384  | 
using degree_add_le [where p=p and q="-q"]  | 
385  | 
by (simp add: diff_minus)  | 
|
386  | 
||
| 29539 | 387  | 
lemma degree_diff_le:  | 
388  | 
"\<lbrakk>degree p \<le> n; degree q \<le> n\<rbrakk> \<Longrightarrow> degree (p - q) \<le> n"  | 
|
389  | 
by (simp add: diff_minus degree_add_le)  | 
|
390  | 
||
| 29453 | 391  | 
lemma degree_diff_less:  | 
392  | 
"\<lbrakk>degree p < n; degree q < n\<rbrakk> \<Longrightarrow> degree (p - q) < n"  | 
|
| 29539 | 393  | 
by (simp add: diff_minus degree_add_less)  | 
| 29453 | 394  | 
|
| 29451 | 395  | 
lemma add_monom: "monom a n + monom b n = monom (a + b) n"  | 
396  | 
by (rule poly_ext) simp  | 
|
397  | 
||
398  | 
lemma diff_monom: "monom a n - monom b n = monom (a - b) n"  | 
|
399  | 
by (rule poly_ext) simp  | 
|
400  | 
||
401  | 
lemma minus_monom: "- monom a n = monom (-a) n"  | 
|
402  | 
by (rule poly_ext) simp  | 
|
403  | 
||
404  | 
lemma coeff_setsum: "coeff (\<Sum>x\<in>A. p x) i = (\<Sum>x\<in>A. coeff (p x) i)"  | 
|
405  | 
by (cases "finite A", induct set: finite, simp_all)  | 
|
406  | 
||
407  | 
lemma monom_setsum: "monom (\<Sum>x\<in>A. a x) n = (\<Sum>x\<in>A. monom (a x) n)"  | 
|
408  | 
by (rule poly_ext) (simp add: coeff_setsum)  | 
|
409  | 
||
410  | 
||
411  | 
subsection {* Multiplication by a constant *}
 | 
|
412  | 
||
413  | 
definition  | 
|
414  | 
smult :: "'a::comm_semiring_0 \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where  | 
|
415  | 
"smult a p = Abs_poly (\<lambda>n. a * coeff p n)"  | 
|
416  | 
||
417  | 
lemma Poly_smult:  | 
|
418  | 
fixes f :: "nat \<Rightarrow> 'a::comm_semiring_0"  | 
|
419  | 
shows "f \<in> Poly \<Longrightarrow> (\<lambda>n. a * f n) \<in> Poly"  | 
|
420  | 
unfolding Poly_def  | 
|
421  | 
by (clarify, rule_tac x=n in exI, simp)  | 
|
422  | 
||
423  | 
lemma coeff_smult [simp]: "coeff (smult a p) n = a * coeff p n"  | 
|
424  | 
unfolding smult_def  | 
|
425  | 
by (simp add: Abs_poly_inverse Poly_smult coeff)  | 
|
426  | 
||
427  | 
lemma degree_smult_le: "degree (smult a p) \<le> degree p"  | 
|
428  | 
by (rule degree_le, simp add: coeff_eq_0)  | 
|
429  | 
||
| 29472 | 430  | 
lemma smult_smult [simp]: "smult a (smult b p) = smult (a * b) p"  | 
| 29451 | 431  | 
by (rule poly_ext, simp add: mult_assoc)  | 
432  | 
||
433  | 
lemma smult_0_right [simp]: "smult a 0 = 0"  | 
|
434  | 
by (rule poly_ext, simp)  | 
|
435  | 
||
436  | 
lemma smult_0_left [simp]: "smult 0 p = 0"  | 
|
437  | 
by (rule poly_ext, simp)  | 
|
438  | 
||
439  | 
lemma smult_1_left [simp]: "smult (1::'a::comm_semiring_1) p = p"  | 
|
440  | 
by (rule poly_ext, simp)  | 
|
441  | 
||
442  | 
lemma smult_add_right:  | 
|
443  | 
"smult a (p + q) = smult a p + smult a q"  | 
|
| 29667 | 444  | 
by (rule poly_ext, simp add: algebra_simps)  | 
| 29451 | 445  | 
|
446  | 
lemma smult_add_left:  | 
|
447  | 
"smult (a + b) p = smult a p + smult b p"  | 
|
| 29667 | 448  | 
by (rule poly_ext, simp add: algebra_simps)  | 
| 29451 | 449  | 
|
| 29457 | 450  | 
lemma smult_minus_right [simp]:  | 
| 29451 | 451  | 
"smult (a::'a::comm_ring) (- p) = - smult a p"  | 
452  | 
by (rule poly_ext, simp)  | 
|
453  | 
||
| 29457 | 454  | 
lemma smult_minus_left [simp]:  | 
| 29451 | 455  | 
"smult (- a::'a::comm_ring) p = - smult a p"  | 
456  | 
by (rule poly_ext, simp)  | 
|
457  | 
||
458  | 
lemma smult_diff_right:  | 
|
459  | 
"smult (a::'a::comm_ring) (p - q) = smult a p - smult a q"  | 
|
| 29667 | 460  | 
by (rule poly_ext, simp add: algebra_simps)  | 
| 29451 | 461  | 
|
462  | 
lemma smult_diff_left:  | 
|
463  | 
"smult (a - b::'a::comm_ring) p = smult a p - smult b p"  | 
|
| 29667 | 464  | 
by (rule poly_ext, simp add: algebra_simps)  | 
| 29451 | 465  | 
|
| 29472 | 466  | 
lemmas smult_distribs =  | 
467  | 
smult_add_left smult_add_right  | 
|
468  | 
smult_diff_left smult_diff_right  | 
|
469  | 
||
| 29451 | 470  | 
lemma smult_pCons [simp]:  | 
471  | 
"smult a (pCons b p) = pCons (a * b) (smult a p)"  | 
|
472  | 
by (rule poly_ext, simp add: coeff_pCons split: nat.split)  | 
|
473  | 
||
474  | 
lemma smult_monom: "smult a (monom b n) = monom (a * b) n"  | 
|
475  | 
by (induct n, simp add: monom_0, simp add: monom_Suc)  | 
|
476  | 
||
| 29659 | 477  | 
lemma degree_smult_eq [simp]:  | 
478  | 
fixes a :: "'a::idom"  | 
|
479  | 
shows "degree (smult a p) = (if a = 0 then 0 else degree p)"  | 
|
480  | 
by (cases "a = 0", simp, simp add: degree_def)  | 
|
481  | 
||
482  | 
lemma smult_eq_0_iff [simp]:  | 
|
483  | 
fixes a :: "'a::idom"  | 
|
484  | 
shows "smult a p = 0 \<longleftrightarrow> a = 0 \<or> p = 0"  | 
|
485  | 
by (simp add: expand_poly_eq)  | 
|
486  | 
||
| 29451 | 487  | 
|
488  | 
subsection {* Multiplication of polynomials *}
 | 
|
489  | 
||
| 29474 | 490  | 
text {* TODO: move to SetInterval.thy *}
 | 
| 29451 | 491  | 
lemma setsum_atMost_Suc_shift:  | 
492  | 
fixes f :: "nat \<Rightarrow> 'a::comm_monoid_add"  | 
|
493  | 
shows "(\<Sum>i\<le>Suc n. f i) = f 0 + (\<Sum>i\<le>n. f (Suc i))"  | 
|
494  | 
proof (induct n)  | 
|
495  | 
case 0 show ?case by simp  | 
|
496  | 
next  | 
|
497  | 
case (Suc n) note IH = this  | 
|
498  | 
have "(\<Sum>i\<le>Suc (Suc n). f i) = (\<Sum>i\<le>Suc n. f i) + f (Suc (Suc n))"  | 
|
499  | 
by (rule setsum_atMost_Suc)  | 
|
500  | 
also have "(\<Sum>i\<le>Suc n. f i) = f 0 + (\<Sum>i\<le>n. f (Suc i))"  | 
|
501  | 
by (rule IH)  | 
|
502  | 
also have "f 0 + (\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)) =  | 
|
503  | 
f 0 + ((\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)))"  | 
|
504  | 
by (rule add_assoc)  | 
|
505  | 
also have "(\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)) = (\<Sum>i\<le>Suc n. f (Suc i))"  | 
|
506  | 
by (rule setsum_atMost_Suc [symmetric])  | 
|
507  | 
finally show ?case .  | 
|
508  | 
qed  | 
|
509  | 
||
510  | 
instantiation poly :: (comm_semiring_0) comm_semiring_0  | 
|
511  | 
begin  | 
|
512  | 
||
513  | 
definition  | 
|
| 29475 | 514  | 
times_poly_def [code del]:  | 
| 29474 | 515  | 
"p * q = poly_rec 0 (\<lambda>a p pq. smult a q + pCons 0 pq) p"  | 
516  | 
||
517  | 
lemma mult_poly_0_left: "(0::'a poly) * q = 0"  | 
|
518  | 
unfolding times_poly_def by (simp add: poly_rec_0)  | 
|
519  | 
||
520  | 
lemma mult_pCons_left [simp]:  | 
|
521  | 
"pCons a p * q = smult a q + pCons 0 (p * q)"  | 
|
522  | 
unfolding times_poly_def by (simp add: poly_rec_pCons)  | 
|
523  | 
||
524  | 
lemma mult_poly_0_right: "p * (0::'a poly) = 0"  | 
|
525  | 
by (induct p, simp add: mult_poly_0_left, simp)  | 
|
| 29451 | 526  | 
|
| 29474 | 527  | 
lemma mult_pCons_right [simp]:  | 
528  | 
"p * pCons a q = smult a p + pCons 0 (p * q)"  | 
|
| 29667 | 529  | 
by (induct p, simp add: mult_poly_0_left, simp add: algebra_simps)  | 
| 29474 | 530  | 
|
531  | 
lemmas mult_poly_0 = mult_poly_0_left mult_poly_0_right  | 
|
532  | 
||
533  | 
lemma mult_smult_left [simp]: "smult a p * q = smult a (p * q)"  | 
|
534  | 
by (induct p, simp add: mult_poly_0, simp add: smult_add_right)  | 
|
535  | 
||
536  | 
lemma mult_smult_right [simp]: "p * smult a q = smult a (p * q)"  | 
|
537  | 
by (induct q, simp add: mult_poly_0, simp add: smult_add_right)  | 
|
538  | 
||
539  | 
lemma mult_poly_add_left:  | 
|
540  | 
fixes p q r :: "'a poly"  | 
|
541  | 
shows "(p + q) * r = p * r + q * r"  | 
|
542  | 
by (induct r, simp add: mult_poly_0,  | 
|
| 29667 | 543  | 
simp add: smult_distribs algebra_simps)  | 
| 29451 | 544  | 
|
545  | 
instance proof  | 
|
546  | 
fix p q r :: "'a poly"  | 
|
547  | 
show 0: "0 * p = 0"  | 
|
| 29474 | 548  | 
by (rule mult_poly_0_left)  | 
| 29451 | 549  | 
show "p * 0 = 0"  | 
| 29474 | 550  | 
by (rule mult_poly_0_right)  | 
| 29451 | 551  | 
show "(p + q) * r = p * r + q * r"  | 
| 29474 | 552  | 
by (rule mult_poly_add_left)  | 
| 29451 | 553  | 
show "(p * q) * r = p * (q * r)"  | 
| 29474 | 554  | 
by (induct p, simp add: mult_poly_0, simp add: mult_poly_add_left)  | 
| 29451 | 555  | 
show "p * q = q * p"  | 
| 29474 | 556  | 
by (induct p, simp add: mult_poly_0, simp)  | 
| 29451 | 557  | 
qed  | 
558  | 
||
559  | 
end  | 
|
560  | 
||
| 29540 | 561  | 
instance poly :: (comm_semiring_0_cancel) comm_semiring_0_cancel ..  | 
562  | 
||
| 29474 | 563  | 
lemma coeff_mult:  | 
564  | 
"coeff (p * q) n = (\<Sum>i\<le>n. coeff p i * coeff q (n-i))"  | 
|
565  | 
proof (induct p arbitrary: n)  | 
|
566  | 
case 0 show ?case by simp  | 
|
567  | 
next  | 
|
568  | 
case (pCons a p n) thus ?case  | 
|
569  | 
by (cases n, simp, simp add: setsum_atMost_Suc_shift  | 
|
570  | 
del: setsum_atMost_Suc)  | 
|
571  | 
qed  | 
|
| 29451 | 572  | 
|
| 29474 | 573  | 
lemma degree_mult_le: "degree (p * q) \<le> degree p + degree q"  | 
574  | 
apply (rule degree_le)  | 
|
575  | 
apply (induct p)  | 
|
576  | 
apply simp  | 
|
577  | 
apply (simp add: coeff_eq_0 coeff_pCons split: nat.split)  | 
|
| 29451 | 578  | 
done  | 
579  | 
||
580  | 
lemma mult_monom: "monom a m * monom b n = monom (a * b) (m + n)"  | 
|
581  | 
by (induct m, simp add: monom_0 smult_monom, simp add: monom_Suc)  | 
|
582  | 
||
583  | 
||
584  | 
subsection {* The unit polynomial and exponentiation *}
 | 
|
585  | 
||
586  | 
instantiation poly :: (comm_semiring_1) comm_semiring_1  | 
|
587  | 
begin  | 
|
588  | 
||
589  | 
definition  | 
|
590  | 
one_poly_def:  | 
|
591  | 
"1 = pCons 1 0"  | 
|
592  | 
||
593  | 
instance proof  | 
|
594  | 
fix p :: "'a poly" show "1 * p = p"  | 
|
595  | 
unfolding one_poly_def  | 
|
596  | 
by simp  | 
|
597  | 
next  | 
|
598  | 
show "0 \<noteq> (1::'a poly)"  | 
|
599  | 
unfolding one_poly_def by simp  | 
|
600  | 
qed  | 
|
601  | 
||
602  | 
end  | 
|
603  | 
||
| 29540 | 604  | 
instance poly :: (comm_semiring_1_cancel) comm_semiring_1_cancel ..  | 
605  | 
||
| 29451 | 606  | 
lemma coeff_1 [simp]: "coeff 1 n = (if n = 0 then 1 else 0)"  | 
607  | 
unfolding one_poly_def  | 
|
608  | 
by (simp add: coeff_pCons split: nat.split)  | 
|
609  | 
||
610  | 
lemma degree_1 [simp]: "degree 1 = 0"  | 
|
611  | 
unfolding one_poly_def  | 
|
612  | 
by (rule degree_pCons_0)  | 
|
613  | 
||
| 29979 | 614  | 
text {* Lemmas about divisibility *}
 | 
615  | 
||
616  | 
lemma dvd_smult: "p dvd q \<Longrightarrow> p dvd smult a q"  | 
|
617  | 
proof -  | 
|
618  | 
assume "p dvd q"  | 
|
619  | 
then obtain k where "q = p * k" ..  | 
|
620  | 
then have "smult a q = p * smult a k" by simp  | 
|
621  | 
then show "p dvd smult a q" ..  | 
|
622  | 
qed  | 
|
623  | 
||
624  | 
lemma dvd_smult_cancel:  | 
|
625  | 
fixes a :: "'a::field"  | 
|
626  | 
shows "p dvd smult a q \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> p dvd q"  | 
|
627  | 
by (drule dvd_smult [where a="inverse a"]) simp  | 
|
628  | 
||
629  | 
lemma dvd_smult_iff:  | 
|
630  | 
fixes a :: "'a::field"  | 
|
631  | 
shows "a \<noteq> 0 \<Longrightarrow> p dvd smult a q \<longleftrightarrow> p dvd q"  | 
|
632  | 
by (safe elim!: dvd_smult dvd_smult_cancel)  | 
|
633  | 
||
| 29451 | 634  | 
instantiation poly :: (comm_semiring_1) recpower  | 
635  | 
begin  | 
|
636  | 
||
637  | 
primrec power_poly where  | 
|
638  | 
power_poly_0: "(p::'a poly) ^ 0 = 1"  | 
|
639  | 
| power_poly_Suc: "(p::'a poly) ^ (Suc n) = p * p ^ n"  | 
|
640  | 
||
641  | 
instance  | 
|
642  | 
by default simp_all  | 
|
643  | 
||
644  | 
end  | 
|
645  | 
||
| 29979 | 646  | 
lemma degree_power_le: "degree (p ^ n) \<le> degree p * n"  | 
647  | 
by (induct n, simp, auto intro: order_trans degree_mult_le)  | 
|
648  | 
||
| 29451 | 649  | 
instance poly :: (comm_ring) comm_ring ..  | 
650  | 
||
651  | 
instance poly :: (comm_ring_1) comm_ring_1 ..  | 
|
652  | 
||
653  | 
instantiation poly :: (comm_ring_1) number_ring  | 
|
654  | 
begin  | 
|
655  | 
||
656  | 
definition  | 
|
657  | 
"number_of k = (of_int k :: 'a poly)"  | 
|
658  | 
||
659  | 
instance  | 
|
660  | 
by default (rule number_of_poly_def)  | 
|
661  | 
||
662  | 
end  | 
|
663  | 
||
664  | 
||
665  | 
subsection {* Polynomials form an integral domain *}
 | 
|
666  | 
||
667  | 
lemma coeff_mult_degree_sum:  | 
|
668  | 
"coeff (p * q) (degree p + degree q) =  | 
|
669  | 
coeff p (degree p) * coeff q (degree q)"  | 
|
| 29471 | 670  | 
by (induct p, simp, simp add: coeff_eq_0)  | 
| 29451 | 671  | 
|
672  | 
instance poly :: (idom) idom  | 
|
673  | 
proof  | 
|
674  | 
fix p q :: "'a poly"  | 
|
675  | 
assume "p \<noteq> 0" and "q \<noteq> 0"  | 
|
676  | 
have "coeff (p * q) (degree p + degree q) =  | 
|
677  | 
coeff p (degree p) * coeff q (degree q)"  | 
|
678  | 
by (rule coeff_mult_degree_sum)  | 
|
679  | 
also have "coeff p (degree p) * coeff q (degree q) \<noteq> 0"  | 
|
680  | 
using `p \<noteq> 0` and `q \<noteq> 0` by simp  | 
|
681  | 
finally have "\<exists>n. coeff (p * q) n \<noteq> 0" ..  | 
|
682  | 
thus "p * q \<noteq> 0" by (simp add: expand_poly_eq)  | 
|
683  | 
qed  | 
|
684  | 
||
685  | 
lemma degree_mult_eq:  | 
|
686  | 
fixes p q :: "'a::idom poly"  | 
|
687  | 
shows "\<lbrakk>p \<noteq> 0; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree (p * q) = degree p + degree q"  | 
|
688  | 
apply (rule order_antisym [OF degree_mult_le le_degree])  | 
|
689  | 
apply (simp add: coeff_mult_degree_sum)  | 
|
690  | 
done  | 
|
691  | 
||
692  | 
lemma dvd_imp_degree_le:  | 
|
693  | 
fixes p q :: "'a::idom poly"  | 
|
694  | 
shows "\<lbrakk>p dvd q; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree p \<le> degree q"  | 
|
695  | 
by (erule dvdE, simp add: degree_mult_eq)  | 
|
696  | 
||
697  | 
||
| 29878 | 698  | 
subsection {* Polynomials form an ordered integral domain *}
 | 
699  | 
||
700  | 
definition  | 
|
701  | 
pos_poly :: "'a::ordered_idom poly \<Rightarrow> bool"  | 
|
702  | 
where  | 
|
703  | 
"pos_poly p \<longleftrightarrow> 0 < coeff p (degree p)"  | 
|
704  | 
||
705  | 
lemma pos_poly_pCons:  | 
|
706  | 
"pos_poly (pCons a p) \<longleftrightarrow> pos_poly p \<or> (p = 0 \<and> 0 < a)"  | 
|
707  | 
unfolding pos_poly_def by simp  | 
|
708  | 
||
709  | 
lemma not_pos_poly_0 [simp]: "\<not> pos_poly 0"  | 
|
710  | 
unfolding pos_poly_def by simp  | 
|
711  | 
||
712  | 
lemma pos_poly_add: "\<lbrakk>pos_poly p; pos_poly q\<rbrakk> \<Longrightarrow> pos_poly (p + q)"  | 
|
713  | 
apply (induct p arbitrary: q, simp)  | 
|
714  | 
apply (case_tac q, force simp add: pos_poly_pCons add_pos_pos)  | 
|
715  | 
done  | 
|
716  | 
||
717  | 
lemma pos_poly_mult: "\<lbrakk>pos_poly p; pos_poly q\<rbrakk> \<Longrightarrow> pos_poly (p * q)"  | 
|
718  | 
unfolding pos_poly_def  | 
|
719  | 
apply (subgoal_tac "p \<noteq> 0 \<and> q \<noteq> 0")  | 
|
720  | 
apply (simp add: degree_mult_eq coeff_mult_degree_sum mult_pos_pos)  | 
|
721  | 
apply auto  | 
|
722  | 
done  | 
|
723  | 
||
724  | 
lemma pos_poly_total: "p = 0 \<or> pos_poly p \<or> pos_poly (- p)"  | 
|
725  | 
by (induct p) (auto simp add: pos_poly_pCons)  | 
|
726  | 
||
727  | 
instantiation poly :: (ordered_idom) ordered_idom  | 
|
728  | 
begin  | 
|
729  | 
||
730  | 
definition  | 
|
731  | 
[code del]:  | 
|
732  | 
"x < y \<longleftrightarrow> pos_poly (y - x)"  | 
|
733  | 
||
734  | 
definition  | 
|
735  | 
[code del]:  | 
|
736  | 
"x \<le> y \<longleftrightarrow> x = y \<or> pos_poly (y - x)"  | 
|
737  | 
||
738  | 
definition  | 
|
739  | 
[code del]:  | 
|
740  | 
"abs (x::'a poly) = (if x < 0 then - x else x)"  | 
|
741  | 
||
742  | 
definition  | 
|
743  | 
[code del]:  | 
|
744  | 
"sgn (x::'a poly) = (if x = 0 then 0 else if 0 < x then 1 else - 1)"  | 
|
745  | 
||
746  | 
instance proof  | 
|
747  | 
fix x y :: "'a poly"  | 
|
748  | 
show "x < y \<longleftrightarrow> x \<le> y \<and> \<not> y \<le> x"  | 
|
749  | 
unfolding less_eq_poly_def less_poly_def  | 
|
750  | 
apply safe  | 
|
751  | 
apply simp  | 
|
752  | 
apply (drule (1) pos_poly_add)  | 
|
753  | 
apply simp  | 
|
754  | 
done  | 
|
755  | 
next  | 
|
756  | 
fix x :: "'a poly" show "x \<le> x"  | 
|
757  | 
unfolding less_eq_poly_def by simp  | 
|
758  | 
next  | 
|
759  | 
fix x y z :: "'a poly"  | 
|
760  | 
assume "x \<le> y" and "y \<le> z" thus "x \<le> z"  | 
|
761  | 
unfolding less_eq_poly_def  | 
|
762  | 
apply safe  | 
|
763  | 
apply (drule (1) pos_poly_add)  | 
|
764  | 
apply (simp add: algebra_simps)  | 
|
765  | 
done  | 
|
766  | 
next  | 
|
767  | 
fix x y :: "'a poly"  | 
|
768  | 
assume "x \<le> y" and "y \<le> x" thus "x = y"  | 
|
769  | 
unfolding less_eq_poly_def  | 
|
770  | 
apply safe  | 
|
771  | 
apply (drule (1) pos_poly_add)  | 
|
772  | 
apply simp  | 
|
773  | 
done  | 
|
774  | 
next  | 
|
775  | 
fix x y z :: "'a poly"  | 
|
776  | 
assume "x \<le> y" thus "z + x \<le> z + y"  | 
|
777  | 
unfolding less_eq_poly_def  | 
|
778  | 
apply safe  | 
|
779  | 
apply (simp add: algebra_simps)  | 
|
780  | 
done  | 
|
781  | 
next  | 
|
782  | 
fix x y :: "'a poly"  | 
|
783  | 
show "x \<le> y \<or> y \<le> x"  | 
|
784  | 
unfolding less_eq_poly_def  | 
|
785  | 
using pos_poly_total [of "x - y"]  | 
|
786  | 
by auto  | 
|
787  | 
next  | 
|
788  | 
fix x y z :: "'a poly"  | 
|
789  | 
assume "x < y" and "0 < z"  | 
|
790  | 
thus "z * x < z * y"  | 
|
791  | 
unfolding less_poly_def  | 
|
792  | 
by (simp add: right_diff_distrib [symmetric] pos_poly_mult)  | 
|
793  | 
next  | 
|
794  | 
fix x :: "'a poly"  | 
|
795  | 
show "\<bar>x\<bar> = (if x < 0 then - x else x)"  | 
|
796  | 
by (rule abs_poly_def)  | 
|
797  | 
next  | 
|
798  | 
fix x :: "'a poly"  | 
|
799  | 
show "sgn x = (if x = 0 then 0 else if 0 < x then 1 else - 1)"  | 
|
800  | 
by (rule sgn_poly_def)  | 
|
801  | 
qed  | 
|
802  | 
||
803  | 
end  | 
|
804  | 
||
805  | 
text {* TODO: Simplification rules for comparisons *}
 | 
|
806  | 
||
807  | 
||
| 29451 | 808  | 
subsection {* Long division of polynomials *}
 | 
809  | 
||
810  | 
definition  | 
|
| 29537 | 811  | 
pdivmod_rel :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> bool"  | 
| 29451 | 812  | 
where  | 
| 29475 | 813  | 
[code del]:  | 
| 29537 | 814  | 
"pdivmod_rel x y q r \<longleftrightarrow>  | 
| 29451 | 815  | 
x = q * y + r \<and> (if y = 0 then q = 0 else r = 0 \<or> degree r < degree y)"  | 
816  | 
||
| 29537 | 817  | 
lemma pdivmod_rel_0:  | 
818  | 
"pdivmod_rel 0 y 0 0"  | 
|
819  | 
unfolding pdivmod_rel_def by simp  | 
|
| 29451 | 820  | 
|
| 29537 | 821  | 
lemma pdivmod_rel_by_0:  | 
822  | 
"pdivmod_rel x 0 0 x"  | 
|
823  | 
unfolding pdivmod_rel_def by simp  | 
|
| 29451 | 824  | 
|
825  | 
lemma eq_zero_or_degree_less:  | 
|
826  | 
assumes "degree p \<le> n" and "coeff p n = 0"  | 
|
827  | 
shows "p = 0 \<or> degree p < n"  | 
|
828  | 
proof (cases n)  | 
|
829  | 
case 0  | 
|
830  | 
with `degree p \<le> n` and `coeff p n = 0`  | 
|
831  | 
have "coeff p (degree p) = 0" by simp  | 
|
832  | 
then have "p = 0" by simp  | 
|
833  | 
then show ?thesis ..  | 
|
834  | 
next  | 
|
835  | 
case (Suc m)  | 
|
836  | 
have "\<forall>i>n. coeff p i = 0"  | 
|
837  | 
using `degree p \<le> n` by (simp add: coeff_eq_0)  | 
|
838  | 
then have "\<forall>i\<ge>n. coeff p i = 0"  | 
|
839  | 
using `coeff p n = 0` by (simp add: le_less)  | 
|
840  | 
then have "\<forall>i>m. coeff p i = 0"  | 
|
841  | 
using `n = Suc m` by (simp add: less_eq_Suc_le)  | 
|
842  | 
then have "degree p \<le> m"  | 
|
843  | 
by (rule degree_le)  | 
|
844  | 
then have "degree p < n"  | 
|
845  | 
using `n = Suc m` by (simp add: less_Suc_eq_le)  | 
|
846  | 
then show ?thesis ..  | 
|
847  | 
qed  | 
|
848  | 
||
| 29537 | 849  | 
lemma pdivmod_rel_pCons:  | 
850  | 
assumes rel: "pdivmod_rel x y q r"  | 
|
| 29451 | 851  | 
assumes y: "y \<noteq> 0"  | 
852  | 
assumes b: "b = coeff (pCons a r) (degree y) / coeff y (degree y)"  | 
|
| 29537 | 853  | 
shows "pdivmod_rel (pCons a x) y (pCons b q) (pCons a r - smult b y)"  | 
854  | 
(is "pdivmod_rel ?x y ?q ?r")  | 
|
| 29451 | 855  | 
proof -  | 
856  | 
have x: "x = q * y + r" and r: "r = 0 \<or> degree r < degree y"  | 
|
| 29537 | 857  | 
using assms unfolding pdivmod_rel_def by simp_all  | 
| 29451 | 858  | 
|
859  | 
have 1: "?x = ?q * y + ?r"  | 
|
860  | 
using b x by simp  | 
|
861  | 
||
862  | 
have 2: "?r = 0 \<or> degree ?r < degree y"  | 
|
863  | 
proof (rule eq_zero_or_degree_less)  | 
|
| 29539 | 864  | 
show "degree ?r \<le> degree y"  | 
865  | 
proof (rule degree_diff_le)  | 
|
| 29451 | 866  | 
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
 | 
867  | 
using r by auto  | 
| 29451 | 868  | 
show "degree (smult b y) \<le> degree y"  | 
869  | 
by (rule degree_smult_le)  | 
|
870  | 
qed  | 
|
871  | 
next  | 
|
872  | 
show "coeff ?r (degree y) = 0"  | 
|
873  | 
using `y \<noteq> 0` unfolding b by simp  | 
|
874  | 
qed  | 
|
875  | 
||
876  | 
from 1 2 show ?thesis  | 
|
| 29537 | 877  | 
unfolding pdivmod_rel_def  | 
| 29451 | 878  | 
using `y \<noteq> 0` by simp  | 
879  | 
qed  | 
|
880  | 
||
| 29537 | 881  | 
lemma pdivmod_rel_exists: "\<exists>q r. pdivmod_rel x y q r"  | 
| 29451 | 882  | 
apply (cases "y = 0")  | 
| 29537 | 883  | 
apply (fast intro!: pdivmod_rel_by_0)  | 
| 29451 | 884  | 
apply (induct x)  | 
| 29537 | 885  | 
apply (fast intro!: pdivmod_rel_0)  | 
886  | 
apply (fast intro!: pdivmod_rel_pCons)  | 
|
| 29451 | 887  | 
done  | 
888  | 
||
| 29537 | 889  | 
lemma pdivmod_rel_unique:  | 
890  | 
assumes 1: "pdivmod_rel x y q1 r1"  | 
|
891  | 
assumes 2: "pdivmod_rel x y q2 r2"  | 
|
| 29451 | 892  | 
shows "q1 = q2 \<and> r1 = r2"  | 
893  | 
proof (cases "y = 0")  | 
|
894  | 
assume "y = 0" with assms show ?thesis  | 
|
| 29537 | 895  | 
by (simp add: pdivmod_rel_def)  | 
| 29451 | 896  | 
next  | 
897  | 
assume [simp]: "y \<noteq> 0"  | 
|
898  | 
from 1 have q1: "x = q1 * y + r1" and r1: "r1 = 0 \<or> degree r1 < degree y"  | 
|
| 29537 | 899  | 
unfolding pdivmod_rel_def by simp_all  | 
| 29451 | 900  | 
from 2 have q2: "x = q2 * y + r2" and r2: "r2 = 0 \<or> degree r2 < degree y"  | 
| 29537 | 901  | 
unfolding pdivmod_rel_def by simp_all  | 
| 29451 | 902  | 
from q1 q2 have q3: "(q1 - q2) * y = r2 - r1"  | 
| 29667 | 903  | 
by (simp add: algebra_simps)  | 
| 29451 | 904  | 
from r1 r2 have r3: "(r2 - r1) = 0 \<or> degree (r2 - r1) < degree y"  | 
| 29453 | 905  | 
by (auto intro: degree_diff_less)  | 
| 29451 | 906  | 
|
907  | 
show "q1 = q2 \<and> r1 = r2"  | 
|
908  | 
proof (rule ccontr)  | 
|
909  | 
assume "\<not> (q1 = q2 \<and> r1 = r2)"  | 
|
910  | 
with q3 have "q1 \<noteq> q2" and "r1 \<noteq> r2" by auto  | 
|
911  | 
with r3 have "degree (r2 - r1) < degree y" by simp  | 
|
912  | 
also have "degree y \<le> degree (q1 - q2) + degree y" by simp  | 
|
913  | 
also have "\<dots> = degree ((q1 - q2) * y)"  | 
|
914  | 
using `q1 \<noteq> q2` by (simp add: degree_mult_eq)  | 
|
915  | 
also have "\<dots> = degree (r2 - r1)"  | 
|
916  | 
using q3 by simp  | 
|
917  | 
finally have "degree (r2 - r1) < degree (r2 - r1)" .  | 
|
918  | 
then show "False" by simp  | 
|
919  | 
qed  | 
|
920  | 
qed  | 
|
921  | 
||
| 29660 | 922  | 
lemma pdivmod_rel_0_iff: "pdivmod_rel 0 y q r \<longleftrightarrow> q = 0 \<and> r = 0"  | 
923  | 
by (auto dest: pdivmod_rel_unique intro: pdivmod_rel_0)  | 
|
924  | 
||
925  | 
lemma pdivmod_rel_by_0_iff: "pdivmod_rel x 0 q r \<longleftrightarrow> q = 0 \<and> r = x"  | 
|
926  | 
by (auto dest: pdivmod_rel_unique intro: pdivmod_rel_by_0)  | 
|
927  | 
||
| 29537 | 928  | 
lemmas pdivmod_rel_unique_div =  | 
929  | 
pdivmod_rel_unique [THEN conjunct1, standard]  | 
|
| 29451 | 930  | 
|
| 29537 | 931  | 
lemmas pdivmod_rel_unique_mod =  | 
932  | 
pdivmod_rel_unique [THEN conjunct2, standard]  | 
|
| 29451 | 933  | 
|
934  | 
instantiation poly :: (field) ring_div  | 
|
935  | 
begin  | 
|
936  | 
||
937  | 
definition div_poly where  | 
|
| 29537 | 938  | 
[code del]: "x div y = (THE q. \<exists>r. pdivmod_rel x y q r)"  | 
| 29451 | 939  | 
|
940  | 
definition mod_poly where  | 
|
| 29537 | 941  | 
[code del]: "x mod y = (THE r. \<exists>q. pdivmod_rel x y q r)"  | 
| 29451 | 942  | 
|
943  | 
lemma div_poly_eq:  | 
|
| 29537 | 944  | 
"pdivmod_rel x y q r \<Longrightarrow> x div y = q"  | 
| 29451 | 945  | 
unfolding div_poly_def  | 
| 29537 | 946  | 
by (fast elim: pdivmod_rel_unique_div)  | 
| 29451 | 947  | 
|
948  | 
lemma mod_poly_eq:  | 
|
| 29537 | 949  | 
"pdivmod_rel x y q r \<Longrightarrow> x mod y = r"  | 
| 29451 | 950  | 
unfolding mod_poly_def  | 
| 29537 | 951  | 
by (fast elim: pdivmod_rel_unique_mod)  | 
| 29451 | 952  | 
|
| 29537 | 953  | 
lemma pdivmod_rel:  | 
954  | 
"pdivmod_rel x y (x div y) (x mod y)"  | 
|
| 29451 | 955  | 
proof -  | 
| 29537 | 956  | 
from pdivmod_rel_exists  | 
957  | 
obtain q r where "pdivmod_rel x y q r" by fast  | 
|
| 29451 | 958  | 
thus ?thesis  | 
959  | 
by (simp add: div_poly_eq mod_poly_eq)  | 
|
960  | 
qed  | 
|
961  | 
||
962  | 
instance proof  | 
|
963  | 
fix x y :: "'a poly"  | 
|
964  | 
show "x div y * y + x mod y = x"  | 
|
| 29537 | 965  | 
using pdivmod_rel [of x y]  | 
966  | 
by (simp add: pdivmod_rel_def)  | 
|
| 29451 | 967  | 
next  | 
968  | 
fix x :: "'a poly"  | 
|
| 29537 | 969  | 
have "pdivmod_rel x 0 0 x"  | 
970  | 
by (rule pdivmod_rel_by_0)  | 
|
| 29451 | 971  | 
thus "x div 0 = 0"  | 
972  | 
by (rule div_poly_eq)  | 
|
973  | 
next  | 
|
974  | 
fix y :: "'a poly"  | 
|
| 29537 | 975  | 
have "pdivmod_rel 0 y 0 0"  | 
976  | 
by (rule pdivmod_rel_0)  | 
|
| 29451 | 977  | 
thus "0 div y = 0"  | 
978  | 
by (rule div_poly_eq)  | 
|
979  | 
next  | 
|
980  | 
fix x y z :: "'a poly"  | 
|
981  | 
assume "y \<noteq> 0"  | 
|
| 29537 | 982  | 
hence "pdivmod_rel (x + z * y) y (z + x div y) (x mod y)"  | 
983  | 
using pdivmod_rel [of x y]  | 
|
984  | 
by (simp add: pdivmod_rel_def left_distrib)  | 
|
| 29451 | 985  | 
thus "(x + z * y) div y = z + x div y"  | 
986  | 
by (rule div_poly_eq)  | 
|
987  | 
qed  | 
|
988  | 
||
989  | 
end  | 
|
990  | 
||
991  | 
lemma degree_mod_less:  | 
|
992  | 
"y \<noteq> 0 \<Longrightarrow> x mod y = 0 \<or> degree (x mod y) < degree y"  | 
|
| 29537 | 993  | 
using pdivmod_rel [of x y]  | 
994  | 
unfolding pdivmod_rel_def by simp  | 
|
| 29451 | 995  | 
|
996  | 
lemma div_poly_less: "degree x < degree y \<Longrightarrow> x div y = 0"  | 
|
997  | 
proof -  | 
|
998  | 
assume "degree x < degree y"  | 
|
| 29537 | 999  | 
hence "pdivmod_rel x y 0 x"  | 
1000  | 
by (simp add: pdivmod_rel_def)  | 
|
| 29451 | 1001  | 
thus "x div y = 0" by (rule div_poly_eq)  | 
1002  | 
qed  | 
|
1003  | 
||
1004  | 
lemma mod_poly_less: "degree x < degree y \<Longrightarrow> x mod y = x"  | 
|
1005  | 
proof -  | 
|
1006  | 
assume "degree x < degree y"  | 
|
| 29537 | 1007  | 
hence "pdivmod_rel x y 0 x"  | 
1008  | 
by (simp add: pdivmod_rel_def)  | 
|
| 29451 | 1009  | 
thus "x mod y = x" by (rule mod_poly_eq)  | 
1010  | 
qed  | 
|
1011  | 
||
| 29659 | 1012  | 
lemma pdivmod_rel_smult_left:  | 
1013  | 
"pdivmod_rel x y q r  | 
|
1014  | 
\<Longrightarrow> pdivmod_rel (smult a x) y (smult a q) (smult a r)"  | 
|
1015  | 
unfolding pdivmod_rel_def by (simp add: smult_add_right)  | 
|
1016  | 
||
1017  | 
lemma div_smult_left: "(smult a x) div y = smult a (x div y)"  | 
|
1018  | 
by (rule div_poly_eq, rule pdivmod_rel_smult_left, rule pdivmod_rel)  | 
|
1019  | 
||
1020  | 
lemma mod_smult_left: "(smult a x) mod y = smult a (x mod y)"  | 
|
1021  | 
by (rule mod_poly_eq, rule pdivmod_rel_smult_left, rule pdivmod_rel)  | 
|
1022  | 
||
1023  | 
lemma pdivmod_rel_smult_right:  | 
|
1024  | 
"\<lbrakk>a \<noteq> 0; pdivmod_rel x y q r\<rbrakk>  | 
|
1025  | 
\<Longrightarrow> pdivmod_rel x (smult a y) (smult (inverse a) q) r"  | 
|
1026  | 
unfolding pdivmod_rel_def by simp  | 
|
1027  | 
||
1028  | 
lemma div_smult_right:  | 
|
1029  | 
"a \<noteq> 0 \<Longrightarrow> x div (smult a y) = smult (inverse a) (x div y)"  | 
|
1030  | 
by (rule div_poly_eq, erule pdivmod_rel_smult_right, rule pdivmod_rel)  | 
|
1031  | 
||
1032  | 
lemma mod_smult_right: "a \<noteq> 0 \<Longrightarrow> x mod (smult a y) = x mod y"  | 
|
1033  | 
by (rule mod_poly_eq, erule pdivmod_rel_smult_right, rule pdivmod_rel)  | 
|
1034  | 
||
| 29660 | 1035  | 
lemma pdivmod_rel_mult:  | 
1036  | 
"\<lbrakk>pdivmod_rel x y q r; pdivmod_rel q z q' r'\<rbrakk>  | 
|
1037  | 
\<Longrightarrow> pdivmod_rel x (y * z) q' (y * r' + r)"  | 
|
1038  | 
apply (cases "z = 0", simp add: pdivmod_rel_def)  | 
|
1039  | 
apply (cases "y = 0", simp add: pdivmod_rel_by_0_iff pdivmod_rel_0_iff)  | 
|
1040  | 
apply (cases "r = 0")  | 
|
1041  | 
apply (cases "r' = 0")  | 
|
1042  | 
apply (simp add: pdivmod_rel_def)  | 
|
1043  | 
apply (simp add: pdivmod_rel_def ring_simps degree_mult_eq)  | 
|
1044  | 
apply (cases "r' = 0")  | 
|
1045  | 
apply (simp add: pdivmod_rel_def degree_mult_eq)  | 
|
1046  | 
apply (simp add: pdivmod_rel_def ring_simps)  | 
|
1047  | 
apply (simp add: degree_mult_eq degree_add_less)  | 
|
1048  | 
done  | 
|
1049  | 
||
1050  | 
lemma poly_div_mult_right:  | 
|
1051  | 
fixes x y z :: "'a::field poly"  | 
|
1052  | 
shows "x div (y * z) = (x div y) div z"  | 
|
1053  | 
by (rule div_poly_eq, rule pdivmod_rel_mult, (rule pdivmod_rel)+)  | 
|
1054  | 
||
1055  | 
lemma poly_mod_mult_right:  | 
|
1056  | 
fixes x y z :: "'a::field poly"  | 
|
1057  | 
shows "x mod (y * z) = y * (x div y mod z) + x mod y"  | 
|
1058  | 
by (rule mod_poly_eq, rule pdivmod_rel_mult, (rule pdivmod_rel)+)  | 
|
1059  | 
||
| 29451 | 1060  | 
lemma mod_pCons:  | 
1061  | 
fixes a and x  | 
|
1062  | 
assumes y: "y \<noteq> 0"  | 
|
1063  | 
defines b: "b \<equiv> coeff (pCons a (x mod y)) (degree y) / coeff y (degree y)"  | 
|
1064  | 
shows "(pCons a x) mod y = (pCons a (x mod y) - smult b y)"  | 
|
1065  | 
unfolding b  | 
|
1066  | 
apply (rule mod_poly_eq)  | 
|
| 29537 | 1067  | 
apply (rule pdivmod_rel_pCons [OF pdivmod_rel y refl])  | 
| 29451 | 1068  | 
done  | 
1069  | 
||
1070  | 
||
1071  | 
subsection {* Evaluation of polynomials *}
 | 
|
1072  | 
||
1073  | 
definition  | 
|
| 
29454
 
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
 
huffman 
parents: 
29453 
diff
changeset
 | 
1074  | 
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
 | 
1075  | 
"poly = poly_rec (\<lambda>x. 0) (\<lambda>a p f x. a + x * f x)"  | 
| 29451 | 1076  | 
|
1077  | 
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
 | 
1078  | 
unfolding poly_def by (simp add: poly_rec_0)  | 
| 29451 | 1079  | 
|
1080  | 
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
 | 
1081  | 
unfolding poly_def by (simp add: poly_rec_pCons)  | 
| 29451 | 1082  | 
|
1083  | 
lemma poly_1 [simp]: "poly 1 x = 1"  | 
|
1084  | 
unfolding one_poly_def by simp  | 
|
1085  | 
||
| 
29454
 
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
 
huffman 
parents: 
29453 
diff
changeset
 | 
1086  | 
lemma poly_monom:  | 
| 
 
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
 
huffman 
parents: 
29453 
diff
changeset
 | 
1087  | 
  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
 | 
1088  | 
shows "poly (monom a n) x = a * x ^ n"  | 
| 29451 | 1089  | 
by (induct n, simp add: monom_0, simp add: monom_Suc power_Suc mult_ac)  | 
1090  | 
||
1091  | 
lemma poly_add [simp]: "poly (p + q) x = poly p x + poly q x"  | 
|
1092  | 
apply (induct p arbitrary: q, simp)  | 
|
| 29667 | 1093  | 
apply (case_tac q, simp, simp add: algebra_simps)  | 
| 29451 | 1094  | 
done  | 
1095  | 
||
1096  | 
lemma poly_minus [simp]:  | 
|
| 
29454
 
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
 
huffman 
parents: 
29453 
diff
changeset
 | 
1097  | 
fixes x :: "'a::comm_ring"  | 
| 29451 | 1098  | 
shows "poly (- p) x = - poly p x"  | 
1099  | 
by (induct p, simp_all)  | 
|
1100  | 
||
1101  | 
lemma poly_diff [simp]:  | 
|
| 
29454
 
b0f586f38dd7
add recursion combinator poly_rec; define poly function using poly_rec
 
huffman 
parents: 
29453 
diff
changeset
 | 
1102  | 
fixes x :: "'a::comm_ring"  | 
| 29451 | 1103  | 
shows "poly (p - q) x = poly p x - poly q x"  | 
1104  | 
by (simp add: diff_minus)  | 
|
1105  | 
||
1106  | 
lemma poly_setsum: "poly (\<Sum>k\<in>A. p k) x = (\<Sum>k\<in>A. poly (p k) x)"  | 
|
1107  | 
by (cases "finite A", induct set: finite, simp_all)  | 
|
1108  | 
||
1109  | 
lemma poly_smult [simp]: "poly (smult a p) x = a * poly p x"  | 
|
| 29667 | 1110  | 
by (induct p, simp, simp add: algebra_simps)  | 
| 29451 | 1111  | 
|
1112  | 
lemma poly_mult [simp]: "poly (p * q) x = poly p x * poly q x"  | 
|
| 29667 | 1113  | 
by (induct p, simp_all, simp add: algebra_simps)  | 
| 29451 | 1114  | 
|
| 29462 | 1115  | 
lemma poly_power [simp]:  | 
1116  | 
  fixes p :: "'a::{comm_semiring_1,recpower} poly"
 | 
|
1117  | 
shows "poly (p ^ n) x = poly p x ^ n"  | 
|
1118  | 
by (induct n, simp, simp add: power_Suc)  | 
|
1119  | 
||
| 29456 | 1120  | 
|
1121  | 
subsection {* Synthetic division *}
 | 
|
1122  | 
||
| 29980 | 1123  | 
text {*
 | 
1124  | 
Synthetic division is simply division by the  | 
|
1125  | 
  linear polynomial @{term "x - c"}.
 | 
|
1126  | 
*}  | 
|
1127  | 
||
| 29456 | 1128  | 
definition  | 
1129  | 
synthetic_divmod :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly \<times> 'a"  | 
|
| 29478 | 1130  | 
where [code del]:  | 
| 29456 | 1131  | 
"synthetic_divmod p c =  | 
1132  | 
poly_rec (0, 0) (\<lambda>a p (q, r). (pCons r q, a + c * r)) p"  | 
|
1133  | 
||
1134  | 
definition  | 
|
1135  | 
synthetic_div :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly"  | 
|
1136  | 
where  | 
|
1137  | 
"synthetic_div p c = fst (synthetic_divmod p c)"  | 
|
1138  | 
||
1139  | 
lemma synthetic_divmod_0 [simp]:  | 
|
1140  | 
"synthetic_divmod 0 c = (0, 0)"  | 
|
1141  | 
unfolding synthetic_divmod_def  | 
|
1142  | 
by (simp add: poly_rec_0)  | 
|
1143  | 
||
1144  | 
lemma synthetic_divmod_pCons [simp]:  | 
|
1145  | 
"synthetic_divmod (pCons a p) c =  | 
|
1146  | 
(\<lambda>(q, r). (pCons r q, a + c * r)) (synthetic_divmod p c)"  | 
|
1147  | 
unfolding synthetic_divmod_def  | 
|
1148  | 
by (simp add: poly_rec_pCons)  | 
|
1149  | 
||
1150  | 
lemma snd_synthetic_divmod: "snd (synthetic_divmod p c) = poly p c"  | 
|
1151  | 
by (induct p, simp, simp add: split_def)  | 
|
1152  | 
||
1153  | 
lemma synthetic_div_0 [simp]: "synthetic_div 0 c = 0"  | 
|
1154  | 
unfolding synthetic_div_def by simp  | 
|
1155  | 
||
1156  | 
lemma synthetic_div_pCons [simp]:  | 
|
1157  | 
"synthetic_div (pCons a p) c = pCons (poly p c) (synthetic_div p c)"  | 
|
1158  | 
unfolding synthetic_div_def  | 
|
1159  | 
by (simp add: split_def snd_synthetic_divmod)  | 
|
1160  | 
||
| 
29460
 
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
 
huffman 
parents: 
29457 
diff
changeset
 | 
1161  | 
lemma synthetic_div_eq_0_iff:  | 
| 
 
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
 
huffman 
parents: 
29457 
diff
changeset
 | 
1162  | 
"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
 | 
1163  | 
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
 | 
1164  | 
|
| 
 
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
 
huffman 
parents: 
29457 
diff
changeset
 | 
1165  | 
lemma degree_synthetic_div:  | 
| 
 
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
 
huffman 
parents: 
29457 
diff
changeset
 | 
1166  | 
"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
 | 
1167  | 
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
 | 
1168  | 
|
| 29457 | 1169  | 
lemma synthetic_div_correct:  | 
| 29456 | 1170  | 
"p + smult c (synthetic_div p c) = pCons (poly p c) (synthetic_div p c)"  | 
1171  | 
by (induct p) simp_all  | 
|
1172  | 
||
| 29457 | 1173  | 
lemma synthetic_div_unique_lemma: "smult c p = pCons a p \<Longrightarrow> p = 0"  | 
1174  | 
by (induct p arbitrary: a) simp_all  | 
|
1175  | 
||
1176  | 
lemma synthetic_div_unique:  | 
|
1177  | 
"p + smult c q = pCons r q \<Longrightarrow> r = poly p c \<and> q = synthetic_div p c"  | 
|
1178  | 
apply (induct p arbitrary: q r)  | 
|
1179  | 
apply (simp, frule synthetic_div_unique_lemma, simp)  | 
|
1180  | 
apply (case_tac q, force)  | 
|
1181  | 
done  | 
|
1182  | 
||
1183  | 
lemma synthetic_div_correct':  | 
|
1184  | 
fixes c :: "'a::comm_ring_1"  | 
|
1185  | 
shows "[:-c, 1:] * synthetic_div p c + [:poly p c:] = p"  | 
|
1186  | 
using synthetic_div_correct [of p c]  | 
|
| 29667 | 1187  | 
by (simp add: algebra_simps)  | 
| 29457 | 1188  | 
|
| 
29460
 
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
 
huffman 
parents: 
29457 
diff
changeset
 | 
1189  | 
lemma poly_eq_0_iff_dvd:  | 
| 
 
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
 
huffman 
parents: 
29457 
diff
changeset
 | 
1190  | 
fixes c :: "'a::idom"  | 
| 
 
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
 
huffman 
parents: 
29457 
diff
changeset
 | 
1191  | 
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
 | 
1192  | 
proof  | 
| 
 
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
 
huffman 
parents: 
29457 
diff
changeset
 | 
1193  | 
assume "poly p c = 0"  | 
| 
 
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
 
huffman 
parents: 
29457 
diff
changeset
 | 
1194  | 
with synthetic_div_correct' [of c p]  | 
| 
 
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
 
huffman 
parents: 
29457 
diff
changeset
 | 
1195  | 
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
 | 
1196  | 
then show "[:-c, 1:] dvd p" ..  | 
| 
 
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
 
huffman 
parents: 
29457 
diff
changeset
 | 
1197  | 
next  | 
| 
 
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
 
huffman 
parents: 
29457 
diff
changeset
 | 
1198  | 
assume "[:-c, 1:] dvd p"  | 
| 
 
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
 
huffman 
parents: 
29457 
diff
changeset
 | 
1199  | 
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
 | 
1200  | 
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
 | 
1201  | 
qed  | 
| 
 
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
 
huffman 
parents: 
29457 
diff
changeset
 | 
1202  | 
|
| 
 
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
 
huffman 
parents: 
29457 
diff
changeset
 | 
1203  | 
lemma dvd_iff_poly_eq_0:  | 
| 
 
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
 
huffman 
parents: 
29457 
diff
changeset
 | 
1204  | 
fixes c :: "'a::idom"  | 
| 
 
ad87e5d1488b
new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
 
huffman 
parents: 
29457 
diff
changeset
 | 
1205  | 
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
 | 
1206  | 
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
 | 
1207  | 
|
| 29462 | 1208  | 
lemma poly_roots_finite:  | 
1209  | 
fixes p :: "'a::idom poly"  | 
|
1210  | 
  shows "p \<noteq> 0 \<Longrightarrow> finite {x. poly p x = 0}"
 | 
|
1211  | 
proof (induct n \<equiv> "degree p" arbitrary: p)  | 
|
1212  | 
case (0 p)  | 
|
1213  | 
then obtain a where "a \<noteq> 0" and "p = [:a:]"  | 
|
1214  | 
by (cases p, simp split: if_splits)  | 
|
1215  | 
  then show "finite {x. poly p x = 0}" by simp
 | 
|
1216  | 
next  | 
|
1217  | 
case (Suc n p)  | 
|
1218  | 
  show "finite {x. poly p x = 0}"
 | 
|
1219  | 
proof (cases "\<exists>x. poly p x = 0")  | 
|
1220  | 
case False  | 
|
1221  | 
    then show "finite {x. poly p x = 0}" by simp
 | 
|
1222  | 
next  | 
|
1223  | 
case True  | 
|
1224  | 
then obtain a where "poly p a = 0" ..  | 
|
1225  | 
then have "[:-a, 1:] dvd p" by (simp only: poly_eq_0_iff_dvd)  | 
|
1226  | 
then obtain k where k: "p = [:-a, 1:] * k" ..  | 
|
1227  | 
with `p \<noteq> 0` have "k \<noteq> 0" by auto  | 
|
1228  | 
with k have "degree p = Suc (degree k)"  | 
|
1229  | 
by (simp add: degree_mult_eq del: mult_pCons_left)  | 
|
1230  | 
with `Suc n = degree p` have "n = degree k" by simp  | 
|
1231  | 
    with `k \<noteq> 0` have "finite {x. poly k x = 0}" by (rule Suc.hyps)
 | 
|
1232  | 
    then have "finite (insert a {x. poly k x = 0})" by simp
 | 
|
1233  | 
    then show "finite {x. poly p x = 0}"
 | 
|
1234  | 
by (simp add: k uminus_add_conv_diff Collect_disj_eq  | 
|
1235  | 
del: mult_pCons_left)  | 
|
1236  | 
qed  | 
|
1237  | 
qed  | 
|
1238  | 
||
| 29979 | 1239  | 
lemma poly_zero:  | 
1240  | 
  fixes p :: "'a::{idom,ring_char_0} poly"
 | 
|
1241  | 
shows "poly p = poly 0 \<longleftrightarrow> p = 0"  | 
|
1242  | 
apply (cases "p = 0", simp_all)  | 
|
1243  | 
apply (drule poly_roots_finite)  | 
|
1244  | 
apply (auto simp add: infinite_UNIV_char_0)  | 
|
1245  | 
done  | 
|
1246  | 
||
1247  | 
lemma poly_eq_iff:  | 
|
1248  | 
  fixes p q :: "'a::{idom,ring_char_0} poly"
 | 
|
1249  | 
shows "poly p = poly q \<longleftrightarrow> p = q"  | 
|
1250  | 
using poly_zero [of "p - q"]  | 
|
1251  | 
by (simp add: expand_fun_eq)  | 
|
1252  | 
||
| 29478 | 1253  | 
|
| 29980 | 1254  | 
subsection {* Composition of polynomials *}
 | 
1255  | 
||
1256  | 
definition  | 
|
1257  | 
pcompose :: "'a::comm_semiring_0 poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"  | 
|
1258  | 
where  | 
|
1259  | 
"pcompose p q = poly_rec 0 (\<lambda>a _ c. [:a:] + q * c) p"  | 
|
1260  | 
||
1261  | 
lemma pcompose_0 [simp]: "pcompose 0 q = 0"  | 
|
1262  | 
unfolding pcompose_def by (simp add: poly_rec_0)  | 
|
1263  | 
||
1264  | 
lemma pcompose_pCons:  | 
|
1265  | 
"pcompose (pCons a p) q = [:a:] + q * pcompose p q"  | 
|
1266  | 
unfolding pcompose_def by (simp add: poly_rec_pCons)  | 
|
1267  | 
||
1268  | 
lemma poly_pcompose: "poly (pcompose p q) x = poly p (poly q x)"  | 
|
1269  | 
by (induct p) (simp_all add: pcompose_pCons)  | 
|
1270  | 
||
1271  | 
lemma degree_pcompose_le:  | 
|
1272  | 
"degree (pcompose p q) \<le> degree p * degree q"  | 
|
1273  | 
apply (induct p, simp)  | 
|
1274  | 
apply (simp add: pcompose_pCons, clarify)  | 
|
1275  | 
apply (rule degree_add_le, simp)  | 
|
1276  | 
apply (rule order_trans [OF degree_mult_le], simp)  | 
|
1277  | 
done  | 
|
1278  | 
||
1279  | 
||
| 
29977
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1280  | 
subsection {* Order of polynomial roots *}
 | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1281  | 
|
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1282  | 
definition  | 
| 29979 | 1283  | 
order :: "'a::idom \<Rightarrow> 'a poly \<Rightarrow> nat"  | 
| 
29977
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1284  | 
where  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1285  | 
[code del]:  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1286  | 
"order a p = (LEAST n. \<not> [:-a, 1:] ^ Suc n dvd p)"  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1287  | 
|
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1288  | 
lemma coeff_linear_power:  | 
| 29979 | 1289  | 
fixes a :: "'a::comm_semiring_1"  | 
| 
29977
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1290  | 
shows "coeff ([:a, 1:] ^ n) n = 1"  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1291  | 
apply (induct n, simp_all)  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1292  | 
apply (subst coeff_eq_0)  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1293  | 
apply (auto intro: le_less_trans degree_power_le)  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1294  | 
done  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1295  | 
|
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1296  | 
lemma degree_linear_power:  | 
| 29979 | 1297  | 
fixes a :: "'a::comm_semiring_1"  | 
| 
29977
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1298  | 
shows "degree ([:a, 1:] ^ n) = n"  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1299  | 
apply (rule order_antisym)  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1300  | 
apply (rule ord_le_eq_trans [OF degree_power_le], simp)  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1301  | 
apply (rule le_degree, simp add: coeff_linear_power)  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1302  | 
done  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1303  | 
|
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1304  | 
lemma order_1: "[:-a, 1:] ^ order a p dvd p"  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1305  | 
apply (cases "p = 0", simp)  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1306  | 
apply (cases "order a p", simp)  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1307  | 
apply (subgoal_tac "nat < (LEAST n. \<not> [:-a, 1:] ^ Suc n dvd p)")  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1308  | 
apply (drule not_less_Least, simp)  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1309  | 
apply (fold order_def, simp)  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1310  | 
done  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1311  | 
|
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1312  | 
lemma order_2: "p \<noteq> 0 \<Longrightarrow> \<not> [:-a, 1:] ^ Suc (order a p) dvd p"  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1313  | 
unfolding order_def  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1314  | 
apply (rule LeastI_ex)  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1315  | 
apply (rule_tac x="degree p" in exI)  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1316  | 
apply (rule notI)  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1317  | 
apply (drule (1) dvd_imp_degree_le)  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1318  | 
apply (simp only: degree_linear_power)  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1319  | 
done  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1320  | 
|
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1321  | 
lemma order:  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1322  | 
"p \<noteq> 0 \<Longrightarrow> [:-a, 1:] ^ order a p dvd p \<and> \<not> [:-a, 1:] ^ Suc (order a p) dvd p"  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1323  | 
by (rule conjI [OF order_1 order_2])  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1324  | 
|
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1325  | 
lemma order_degree:  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1326  | 
assumes p: "p \<noteq> 0"  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1327  | 
shows "order a p \<le> degree p"  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1328  | 
proof -  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1329  | 
have "order a p = degree ([:-a, 1:] ^ order a p)"  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1330  | 
by (simp only: degree_linear_power)  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1331  | 
also have "\<dots> \<le> degree p"  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1332  | 
using order_1 p by (rule dvd_imp_degree_le)  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1333  | 
finally show ?thesis .  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1334  | 
qed  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1335  | 
|
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1336  | 
lemma order_root: "poly p a = 0 \<longleftrightarrow> p = 0 \<or> order a p \<noteq> 0"  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1337  | 
apply (cases "p = 0", simp_all)  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1338  | 
apply (rule iffI)  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1339  | 
apply (rule ccontr, simp)  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1340  | 
apply (frule order_2 [where a=a], simp)  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1341  | 
apply (simp add: poly_eq_0_iff_dvd)  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1342  | 
apply (simp add: poly_eq_0_iff_dvd)  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1343  | 
apply (simp only: order_def)  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1344  | 
apply (drule not_less_Least, simp)  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1345  | 
done  | 
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1346  | 
|
| 
 
d76b830366bc
move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
 
huffman 
parents: 
29904 
diff
changeset
 | 
1347  | 
|
| 29478 | 1348  | 
subsection {* Configuration of the code generator *}
 | 
1349  | 
||
1350  | 
code_datatype "0::'a::zero poly" pCons  | 
|
1351  | 
||
| 29480 | 1352  | 
declare pCons_0_0 [code post]  | 
1353  | 
||
| 29478 | 1354  | 
instantiation poly :: ("{zero,eq}") eq
 | 
1355  | 
begin  | 
|
1356  | 
||
1357  | 
definition [code del]:  | 
|
1358  | 
"eq_class.eq (p::'a poly) q \<longleftrightarrow> p = q"  | 
|
1359  | 
||
1360  | 
instance  | 
|
1361  | 
by default (rule eq_poly_def)  | 
|
1362  | 
||
| 29451 | 1363  | 
end  | 
| 29478 | 1364  | 
|
1365  | 
lemma eq_poly_code [code]:  | 
|
1366  | 
"eq_class.eq (0::_ poly) (0::_ poly) \<longleftrightarrow> True"  | 
|
1367  | 
"eq_class.eq (0::_ poly) (pCons b q) \<longleftrightarrow> eq_class.eq 0 b \<and> eq_class.eq 0 q"  | 
|
1368  | 
"eq_class.eq (pCons a p) (0::_ poly) \<longleftrightarrow> eq_class.eq a 0 \<and> eq_class.eq p 0"  | 
|
1369  | 
"eq_class.eq (pCons a p) (pCons b q) \<longleftrightarrow> eq_class.eq a b \<and> eq_class.eq p q"  | 
|
1370  | 
unfolding eq by simp_all  | 
|
1371  | 
||
1372  | 
lemmas coeff_code [code] =  | 
|
1373  | 
coeff_0 coeff_pCons_0 coeff_pCons_Suc  | 
|
1374  | 
||
1375  | 
lemmas degree_code [code] =  | 
|
1376  | 
degree_0 degree_pCons_eq_if  | 
|
1377  | 
||
1378  | 
lemmas monom_poly_code [code] =  | 
|
1379  | 
monom_0 monom_Suc  | 
|
1380  | 
||
1381  | 
lemma add_poly_code [code]:  | 
|
1382  | 
"0 + q = (q :: _ poly)"  | 
|
1383  | 
"p + 0 = (p :: _ poly)"  | 
|
1384  | 
"pCons a p + pCons b q = pCons (a + b) (p + q)"  | 
|
1385  | 
by simp_all  | 
|
1386  | 
||
1387  | 
lemma minus_poly_code [code]:  | 
|
1388  | 
"- 0 = (0 :: _ poly)"  | 
|
1389  | 
"- pCons a p = pCons (- a) (- p)"  | 
|
1390  | 
by simp_all  | 
|
1391  | 
||
1392  | 
lemma diff_poly_code [code]:  | 
|
1393  | 
"0 - q = (- q :: _ poly)"  | 
|
1394  | 
"p - 0 = (p :: _ poly)"  | 
|
1395  | 
"pCons a p - pCons b q = pCons (a - b) (p - q)"  | 
|
1396  | 
by simp_all  | 
|
1397  | 
||
1398  | 
lemmas smult_poly_code [code] =  | 
|
1399  | 
smult_0_right smult_pCons  | 
|
1400  | 
||
1401  | 
lemma mult_poly_code [code]:  | 
|
1402  | 
"0 * q = (0 :: _ poly)"  | 
|
1403  | 
"pCons a p * q = smult a q + pCons 0 (p * q)"  | 
|
1404  | 
by simp_all  | 
|
1405  | 
||
1406  | 
lemmas poly_code [code] =  | 
|
1407  | 
poly_0 poly_pCons  | 
|
1408  | 
||
1409  | 
lemmas synthetic_divmod_code [code] =  | 
|
1410  | 
synthetic_divmod_0 synthetic_divmod_pCons  | 
|
1411  | 
||
1412  | 
text {* code generator setup for div and mod *}
 | 
|
1413  | 
||
1414  | 
definition  | 
|
| 29537 | 1415  | 
pdivmod :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<times> 'a poly"  | 
| 29478 | 1416  | 
where  | 
| 29537 | 1417  | 
[code del]: "pdivmod x y = (x div y, x mod y)"  | 
| 29478 | 1418  | 
|
| 29537 | 1419  | 
lemma div_poly_code [code]: "x div y = fst (pdivmod x y)"  | 
1420  | 
unfolding pdivmod_def by simp  | 
|
| 29478 | 1421  | 
|
| 29537 | 1422  | 
lemma mod_poly_code [code]: "x mod y = snd (pdivmod x y)"  | 
1423  | 
unfolding pdivmod_def by simp  | 
|
| 29478 | 1424  | 
|
| 29537 | 1425  | 
lemma pdivmod_0 [code]: "pdivmod 0 y = (0, 0)"  | 
1426  | 
unfolding pdivmod_def by simp  | 
|
| 29478 | 1427  | 
|
| 29537 | 1428  | 
lemma pdivmod_pCons [code]:  | 
1429  | 
"pdivmod (pCons a x) y =  | 
|
| 29478 | 1430  | 
(if y = 0 then (0, pCons a x) else  | 
| 29537 | 1431  | 
(let (q, r) = pdivmod x y;  | 
| 29478 | 1432  | 
b = coeff (pCons a r) (degree y) / coeff y (degree y)  | 
1433  | 
in (pCons b q, pCons a r - smult b y)))"  | 
|
| 29537 | 1434  | 
apply (simp add: pdivmod_def Let_def, safe)  | 
| 29478 | 1435  | 
apply (rule div_poly_eq)  | 
| 29537 | 1436  | 
apply (erule pdivmod_rel_pCons [OF pdivmod_rel _ refl])  | 
| 29478 | 1437  | 
apply (rule mod_poly_eq)  | 
| 29537 | 1438  | 
apply (erule pdivmod_rel_pCons [OF pdivmod_rel _ refl])  | 
| 29478 | 1439  | 
done  | 
1440  | 
||
1441  | 
end  |