wenzelm@42830
|
1 |
(* Title: HOL/Library/Polynomial.thy
|
huffman@29449
|
2 |
Author: Brian Huffman
|
wenzelm@42830
|
3 |
Author: Clemens Ballarin
|
huffman@29449
|
4 |
*)
|
huffman@29449
|
5 |
|
huffman@29449
|
6 |
header {* Univariate Polynomials *}
|
huffman@29449
|
7 |
|
huffman@29449
|
8 |
theory Polynomial
|
haftmann@30738
|
9 |
imports Main
|
huffman@29449
|
10 |
begin
|
huffman@29449
|
11 |
|
huffman@29449
|
12 |
subsection {* Definition of type @{text poly} *}
|
huffman@29449
|
13 |
|
wenzelm@46567
|
14 |
definition "Poly = {f::nat \<Rightarrow> 'a::zero. \<exists>n. \<forall>i>n. f i = 0}"
|
wenzelm@46567
|
15 |
|
wenzelm@46567
|
16 |
typedef (open) 'a poly = "Poly :: (nat => 'a::zero) set"
|
huffman@29449
|
17 |
morphisms coeff Abs_poly
|
wenzelm@46567
|
18 |
unfolding Poly_def by auto
|
huffman@29449
|
19 |
|
haftmann@47873
|
20 |
(* FIXME should be named poly_eq_iff *)
|
huffman@29449
|
21 |
lemma expand_poly_eq: "p = q \<longleftrightarrow> (\<forall>n. coeff p n = coeff q n)"
|
wenzelm@46567
|
22 |
by (simp add: coeff_inject [symmetric] fun_eq_iff)
|
huffman@29449
|
23 |
|
haftmann@47873
|
24 |
(* FIXME should be named poly_eqI *)
|
huffman@29449
|
25 |
lemma poly_ext: "(\<And>n. coeff p n = coeff q n) \<Longrightarrow> p = q"
|
wenzelm@46567
|
26 |
by (simp add: expand_poly_eq)
|
huffman@29449
|
27 |
|
huffman@29449
|
28 |
|
huffman@29449
|
29 |
subsection {* Degree of a polynomial *}
|
huffman@29449
|
30 |
|
huffman@29449
|
31 |
definition
|
huffman@29449
|
32 |
degree :: "'a::zero poly \<Rightarrow> nat" where
|
huffman@29449
|
33 |
"degree p = (LEAST n. \<forall>i>n. coeff p i = 0)"
|
huffman@29449
|
34 |
|
huffman@29449
|
35 |
lemma coeff_eq_0: "degree p < n \<Longrightarrow> coeff p n = 0"
|
huffman@29449
|
36 |
proof -
|
huffman@29449
|
37 |
have "coeff p \<in> Poly"
|
huffman@29449
|
38 |
by (rule coeff)
|
huffman@29449
|
39 |
hence "\<exists>n. \<forall>i>n. coeff p i = 0"
|
huffman@29449
|
40 |
unfolding Poly_def by simp
|
huffman@29449
|
41 |
hence "\<forall>i>degree p. coeff p i = 0"
|
huffman@29449
|
42 |
unfolding degree_def by (rule LeastI_ex)
|
huffman@29449
|
43 |
moreover assume "degree p < n"
|
huffman@29449
|
44 |
ultimately show ?thesis by simp
|
huffman@29449
|
45 |
qed
|
huffman@29449
|
46 |
|
huffman@29449
|
47 |
lemma le_degree: "coeff p n \<noteq> 0 \<Longrightarrow> n \<le> degree p"
|
huffman@29449
|
48 |
by (erule contrapos_np, rule coeff_eq_0, simp)
|
huffman@29449
|
49 |
|
huffman@29449
|
50 |
lemma degree_le: "\<forall>i>n. coeff p i = 0 \<Longrightarrow> degree p \<le> n"
|
huffman@29449
|
51 |
unfolding degree_def by (erule Least_le)
|
huffman@29449
|
52 |
|
huffman@29449
|
53 |
lemma less_degree_imp: "n < degree p \<Longrightarrow> \<exists>i>n. coeff p i \<noteq> 0"
|
huffman@29449
|
54 |
unfolding degree_def by (drule not_less_Least, simp)
|
huffman@29449
|
55 |
|
huffman@29449
|
56 |
|
huffman@29449
|
57 |
subsection {* The zero polynomial *}
|
huffman@29449
|
58 |
|
huffman@29449
|
59 |
instantiation poly :: (zero) zero
|
huffman@29449
|
60 |
begin
|
huffman@29449
|
61 |
|
huffman@29449
|
62 |
definition
|
huffman@29449
|
63 |
zero_poly_def: "0 = Abs_poly (\<lambda>n. 0)"
|
huffman@29449
|
64 |
|
huffman@29449
|
65 |
instance ..
|
huffman@29449
|
66 |
end
|
huffman@29449
|
67 |
|
huffman@29449
|
68 |
lemma coeff_0 [simp]: "coeff 0 n = 0"
|
huffman@29449
|
69 |
unfolding zero_poly_def
|
huffman@29449
|
70 |
by (simp add: Abs_poly_inverse Poly_def)
|
huffman@29449
|
71 |
|
huffman@29449
|
72 |
lemma degree_0 [simp]: "degree 0 = 0"
|
huffman@29449
|
73 |
by (rule order_antisym [OF degree_le le0]) simp
|
huffman@29449
|
74 |
|
huffman@29449
|
75 |
lemma leading_coeff_neq_0:
|
huffman@29449
|
76 |
assumes "p \<noteq> 0" shows "coeff p (degree p) \<noteq> 0"
|
huffman@29449
|
77 |
proof (cases "degree p")
|
huffman@29449
|
78 |
case 0
|
huffman@29449
|
79 |
from `p \<noteq> 0` have "\<exists>n. coeff p n \<noteq> 0"
|
huffman@29449
|
80 |
by (simp add: expand_poly_eq)
|
huffman@29449
|
81 |
then obtain n where "coeff p n \<noteq> 0" ..
|
huffman@29449
|
82 |
hence "n \<le> degree p" by (rule le_degree)
|
huffman@29449
|
83 |
with `coeff p n \<noteq> 0` and `degree p = 0`
|
huffman@29449
|
84 |
show "coeff p (degree p) \<noteq> 0" by simp
|
huffman@29449
|
85 |
next
|
huffman@29449
|
86 |
case (Suc n)
|
huffman@29449
|
87 |
from `degree p = Suc n` have "n < degree p" by simp
|
huffman@29449
|
88 |
hence "\<exists>i>n. coeff p i \<noteq> 0" by (rule less_degree_imp)
|
huffman@29449
|
89 |
then obtain i where "n < i" and "coeff p i \<noteq> 0" by fast
|
huffman@29449
|
90 |
from `degree p = Suc n` and `n < i` have "degree p \<le> i" by simp
|
huffman@29449
|
91 |
also from `coeff p i \<noteq> 0` have "i \<le> degree p" by (rule le_degree)
|
huffman@29449
|
92 |
finally have "degree p = i" .
|
huffman@29449
|
93 |
with `coeff p i \<noteq> 0` show "coeff p (degree p) \<noteq> 0" by simp
|
huffman@29449
|
94 |
qed
|
huffman@29449
|
95 |
|
huffman@29449
|
96 |
lemma leading_coeff_0_iff [simp]: "coeff p (degree p) = 0 \<longleftrightarrow> p = 0"
|
huffman@29449
|
97 |
by (cases "p = 0", simp, simp add: leading_coeff_neq_0)
|
huffman@29449
|
98 |
|
huffman@29449
|
99 |
|
huffman@29449
|
100 |
subsection {* List-style constructor for polynomials *}
|
huffman@29449
|
101 |
|
huffman@29449
|
102 |
definition
|
huffman@29449
|
103 |
pCons :: "'a::zero \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
|
huffman@29449
|
104 |
where
|
haftmann@37765
|
105 |
"pCons a p = Abs_poly (nat_case a (coeff p))"
|
huffman@29449
|
106 |
|
huffman@29455
|
107 |
syntax
|
huffman@29455
|
108 |
"_poly" :: "args \<Rightarrow> 'a poly" ("[:(_):]")
|
huffman@29455
|
109 |
|
huffman@29455
|
110 |
translations
|
huffman@29455
|
111 |
"[:x, xs:]" == "CONST pCons x [:xs:]"
|
huffman@29455
|
112 |
"[:x:]" == "CONST pCons x 0"
|
huffman@30155
|
113 |
"[:x:]" <= "CONST pCons x (_constrain 0 t)"
|
huffman@29455
|
114 |
|
huffman@29449
|
115 |
lemma Poly_nat_case: "f \<in> Poly \<Longrightarrow> nat_case a f \<in> Poly"
|
huffman@29449
|
116 |
unfolding Poly_def by (auto split: nat.split)
|
huffman@29449
|
117 |
|
huffman@29449
|
118 |
lemma coeff_pCons:
|
huffman@29449
|
119 |
"coeff (pCons a p) = nat_case a (coeff p)"
|
huffman@29449
|
120 |
unfolding pCons_def
|
huffman@29449
|
121 |
by (simp add: Abs_poly_inverse Poly_nat_case coeff)
|
huffman@29449
|
122 |
|
huffman@29449
|
123 |
lemma coeff_pCons_0 [simp]: "coeff (pCons a p) 0 = a"
|
huffman@29449
|
124 |
by (simp add: coeff_pCons)
|
huffman@29449
|
125 |
|
huffman@29449
|
126 |
lemma coeff_pCons_Suc [simp]: "coeff (pCons a p) (Suc n) = coeff p n"
|
huffman@29449
|
127 |
by (simp add: coeff_pCons)
|
huffman@29449
|
128 |
|
huffman@29449
|
129 |
lemma degree_pCons_le: "degree (pCons a p) \<le> Suc (degree p)"
|
huffman@29449
|
130 |
by (rule degree_le, simp add: coeff_eq_0 coeff_pCons split: nat.split)
|
huffman@29449
|
131 |
|
huffman@29449
|
132 |
lemma degree_pCons_eq:
|
huffman@29449
|
133 |
"p \<noteq> 0 \<Longrightarrow> degree (pCons a p) = Suc (degree p)"
|
huffman@29449
|
134 |
apply (rule order_antisym [OF degree_pCons_le])
|
huffman@29449
|
135 |
apply (rule le_degree, simp)
|
huffman@29449
|
136 |
done
|
huffman@29449
|
137 |
|
huffman@29449
|
138 |
lemma degree_pCons_0: "degree (pCons a 0) = 0"
|
huffman@29449
|
139 |
apply (rule order_antisym [OF _ le0])
|
huffman@29449
|
140 |
apply (rule degree_le, simp add: coeff_pCons split: nat.split)
|
huffman@29449
|
141 |
done
|
huffman@29449
|
142 |
|
huffman@29458
|
143 |
lemma degree_pCons_eq_if [simp]:
|
huffman@29449
|
144 |
"degree (pCons a p) = (if p = 0 then 0 else Suc (degree p))"
|
huffman@29449
|
145 |
apply (cases "p = 0", simp_all)
|
huffman@29449
|
146 |
apply (rule order_antisym [OF _ le0])
|
huffman@29449
|
147 |
apply (rule degree_le, simp add: coeff_pCons split: nat.split)
|
huffman@29449
|
148 |
apply (rule order_antisym [OF degree_pCons_le])
|
huffman@29449
|
149 |
apply (rule le_degree, simp)
|
huffman@29449
|
150 |
done
|
huffman@29449
|
151 |
|
haftmann@46902
|
152 |
lemma pCons_0_0 [simp, code_post]: "pCons 0 0 = 0"
|
huffman@29449
|
153 |
by (rule poly_ext, simp add: coeff_pCons split: nat.split)
|
huffman@29449
|
154 |
|
huffman@29449
|
155 |
lemma pCons_eq_iff [simp]:
|
huffman@29449
|
156 |
"pCons a p = pCons b q \<longleftrightarrow> a = b \<and> p = q"
|
huffman@29449
|
157 |
proof (safe)
|
huffman@29449
|
158 |
assume "pCons a p = pCons b q"
|
huffman@29449
|
159 |
then have "coeff (pCons a p) 0 = coeff (pCons b q) 0" by simp
|
huffman@29449
|
160 |
then show "a = b" by simp
|
huffman@29449
|
161 |
next
|
huffman@29449
|
162 |
assume "pCons a p = pCons b q"
|
huffman@29449
|
163 |
then have "\<forall>n. coeff (pCons a p) (Suc n) =
|
huffman@29449
|
164 |
coeff (pCons b q) (Suc n)" by simp
|
huffman@29449
|
165 |
then show "p = q" by (simp add: expand_poly_eq)
|
huffman@29449
|
166 |
qed
|
huffman@29449
|
167 |
|
huffman@29449
|
168 |
lemma pCons_eq_0_iff [simp]: "pCons a p = 0 \<longleftrightarrow> a = 0 \<and> p = 0"
|
huffman@29449
|
169 |
using pCons_eq_iff [of a p 0 0] by simp
|
huffman@29449
|
170 |
|
huffman@29449
|
171 |
lemma Poly_Suc: "f \<in> Poly \<Longrightarrow> (\<lambda>n. f (Suc n)) \<in> Poly"
|
huffman@29449
|
172 |
unfolding Poly_def
|
huffman@29449
|
173 |
by (clarify, rule_tac x=n in exI, simp)
|
huffman@29449
|
174 |
|
huffman@29449
|
175 |
lemma pCons_cases [cases type: poly]:
|
huffman@29449
|
176 |
obtains (pCons) a q where "p = pCons a q"
|
huffman@29449
|
177 |
proof
|
huffman@29449
|
178 |
show "p = pCons (coeff p 0) (Abs_poly (\<lambda>n. coeff p (Suc n)))"
|
huffman@29449
|
179 |
by (rule poly_ext)
|
huffman@29449
|
180 |
(simp add: Abs_poly_inverse Poly_Suc coeff coeff_pCons
|
huffman@29449
|
181 |
split: nat.split)
|
huffman@29449
|
182 |
qed
|
huffman@29449
|
183 |
|
huffman@29449
|
184 |
lemma pCons_induct [case_names 0 pCons, induct type: poly]:
|
huffman@29449
|
185 |
assumes zero: "P 0"
|
huffman@29449
|
186 |
assumes pCons: "\<And>a p. P p \<Longrightarrow> P (pCons a p)"
|
huffman@29449
|
187 |
shows "P p"
|
huffman@29449
|
188 |
proof (induct p rule: measure_induct_rule [where f=degree])
|
huffman@29449
|
189 |
case (less p)
|
huffman@29449
|
190 |
obtain a q where "p = pCons a q" by (rule pCons_cases)
|
huffman@29449
|
191 |
have "P q"
|
huffman@29449
|
192 |
proof (cases "q = 0")
|
huffman@29449
|
193 |
case True
|
huffman@29449
|
194 |
then show "P q" by (simp add: zero)
|
huffman@29449
|
195 |
next
|
huffman@29449
|
196 |
case False
|
huffman@29449
|
197 |
then have "degree (pCons a q) = Suc (degree q)"
|
huffman@29449
|
198 |
by (rule degree_pCons_eq)
|
huffman@29449
|
199 |
then have "degree q < degree p"
|
huffman@29449
|
200 |
using `p = pCons a q` by simp
|
huffman@29449
|
201 |
then show "P q"
|
huffman@29449
|
202 |
by (rule less.hyps)
|
huffman@29449
|
203 |
qed
|
huffman@29449
|
204 |
then have "P (pCons a q)"
|
huffman@29449
|
205 |
by (rule pCons)
|
huffman@29449
|
206 |
then show ?case
|
huffman@29449
|
207 |
using `p = pCons a q` by simp
|
huffman@29449
|
208 |
qed
|
huffman@29449
|
209 |
|
huffman@29449
|
210 |
|
huffman@29454
|
211 |
subsection {* Recursion combinator for polynomials *}
|
huffman@29454
|
212 |
|
huffman@29454
|
213 |
function
|
huffman@29454
|
214 |
poly_rec :: "'b \<Rightarrow> ('a::zero \<Rightarrow> 'a poly \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a poly \<Rightarrow> 'b"
|
huffman@29454
|
215 |
where
|
haftmann@37765
|
216 |
poly_rec_pCons_eq_if [simp del]:
|
huffman@29454
|
217 |
"poly_rec z f (pCons a p) = f a p (if p = 0 then z else poly_rec z f p)"
|
huffman@29454
|
218 |
by (case_tac x, rename_tac q, case_tac q, auto)
|
huffman@29454
|
219 |
|
huffman@29454
|
220 |
termination poly_rec
|
huffman@29454
|
221 |
by (relation "measure (degree \<circ> snd \<circ> snd)", simp)
|
huffman@29454
|
222 |
(simp add: degree_pCons_eq)
|
huffman@29454
|
223 |
|
huffman@29454
|
224 |
lemma poly_rec_0:
|
huffman@29454
|
225 |
"f 0 0 z = z \<Longrightarrow> poly_rec z f 0 = z"
|
huffman@29454
|
226 |
using poly_rec_pCons_eq_if [of z f 0 0] by simp
|
huffman@29454
|
227 |
|
huffman@29454
|
228 |
lemma poly_rec_pCons:
|
huffman@29454
|
229 |
"f 0 0 z = z \<Longrightarrow> poly_rec z f (pCons a p) = f a p (poly_rec z f p)"
|
huffman@29454
|
230 |
by (simp add: poly_rec_pCons_eq_if poly_rec_0)
|
huffman@29454
|
231 |
|
huffman@29454
|
232 |
|
huffman@29449
|
233 |
subsection {* Monomials *}
|
huffman@29449
|
234 |
|
huffman@29449
|
235 |
definition
|
huffman@29449
|
236 |
monom :: "'a \<Rightarrow> nat \<Rightarrow> 'a::zero poly" where
|
huffman@29449
|
237 |
"monom a m = Abs_poly (\<lambda>n. if m = n then a else 0)"
|
huffman@29449
|
238 |
|
huffman@29449
|
239 |
lemma coeff_monom [simp]: "coeff (monom a m) n = (if m=n then a else 0)"
|
huffman@29449
|
240 |
unfolding monom_def
|
huffman@29449
|
241 |
by (subst Abs_poly_inverse, auto simp add: Poly_def)
|
huffman@29449
|
242 |
|
huffman@29449
|
243 |
lemma monom_0: "monom a 0 = pCons a 0"
|
huffman@29449
|
244 |
by (rule poly_ext, simp add: coeff_pCons split: nat.split)
|
huffman@29449
|
245 |
|
huffman@29449
|
246 |
lemma monom_Suc: "monom a (Suc n) = pCons 0 (monom a n)"
|
huffman@29449
|
247 |
by (rule poly_ext, simp add: coeff_pCons split: nat.split)
|
huffman@29449
|
248 |
|
huffman@29449
|
249 |
lemma monom_eq_0 [simp]: "monom 0 n = 0"
|
huffman@29449
|
250 |
by (rule poly_ext) simp
|
huffman@29449
|
251 |
|
huffman@29449
|
252 |
lemma monom_eq_0_iff [simp]: "monom a n = 0 \<longleftrightarrow> a = 0"
|
huffman@29449
|
253 |
by (simp add: expand_poly_eq)
|
huffman@29449
|
254 |
|
huffman@29449
|
255 |
lemma monom_eq_iff [simp]: "monom a n = monom b n \<longleftrightarrow> a = b"
|
huffman@29449
|
256 |
by (simp add: expand_poly_eq)
|
huffman@29449
|
257 |
|
huffman@29449
|
258 |
lemma degree_monom_le: "degree (monom a n) \<le> n"
|
huffman@29449
|
259 |
by (rule degree_le, simp)
|
huffman@29449
|
260 |
|
huffman@29449
|
261 |
lemma degree_monom_eq: "a \<noteq> 0 \<Longrightarrow> degree (monom a n) = n"
|
huffman@29449
|
262 |
apply (rule order_antisym [OF degree_monom_le])
|
huffman@29449
|
263 |
apply (rule le_degree, simp)
|
huffman@29449
|
264 |
done
|
huffman@29449
|
265 |
|
huffman@29449
|
266 |
|
huffman@29449
|
267 |
subsection {* Addition and subtraction *}
|
huffman@29449
|
268 |
|
huffman@29449
|
269 |
instantiation poly :: (comm_monoid_add) comm_monoid_add
|
huffman@29449
|
270 |
begin
|
huffman@29449
|
271 |
|
huffman@29449
|
272 |
definition
|
haftmann@37765
|
273 |
plus_poly_def:
|
huffman@29449
|
274 |
"p + q = Abs_poly (\<lambda>n. coeff p n + coeff q n)"
|
huffman@29449
|
275 |
|
huffman@29449
|
276 |
lemma Poly_add:
|
huffman@29449
|
277 |
fixes f g :: "nat \<Rightarrow> 'a"
|
huffman@29449
|
278 |
shows "\<lbrakk>f \<in> Poly; g \<in> Poly\<rbrakk> \<Longrightarrow> (\<lambda>n. f n + g n) \<in> Poly"
|
huffman@29449
|
279 |
unfolding Poly_def
|
huffman@29449
|
280 |
apply (clarify, rename_tac m n)
|
huffman@29449
|
281 |
apply (rule_tac x="max m n" in exI, simp)
|
huffman@29449
|
282 |
done
|
huffman@29449
|
283 |
|
huffman@29449
|
284 |
lemma coeff_add [simp]:
|
huffman@29449
|
285 |
"coeff (p + q) n = coeff p n + coeff q n"
|
huffman@29449
|
286 |
unfolding plus_poly_def
|
huffman@29449
|
287 |
by (simp add: Abs_poly_inverse coeff Poly_add)
|
huffman@29449
|
288 |
|
huffman@29449
|
289 |
instance proof
|
huffman@29449
|
290 |
fix p q r :: "'a poly"
|
huffman@29449
|
291 |
show "(p + q) + r = p + (q + r)"
|
huffman@29449
|
292 |
by (simp add: expand_poly_eq add_assoc)
|
huffman@29449
|
293 |
show "p + q = q + p"
|
huffman@29449
|
294 |
by (simp add: expand_poly_eq add_commute)
|
huffman@29449
|
295 |
show "0 + p = p"
|
huffman@29449
|
296 |
by (simp add: expand_poly_eq)
|
huffman@29449
|
297 |
qed
|
huffman@29449
|
298 |
|
huffman@29449
|
299 |
end
|
huffman@29449
|
300 |
|
huffman@29841
|
301 |
instance poly :: (cancel_comm_monoid_add) cancel_comm_monoid_add
|
huffman@29540
|
302 |
proof
|
huffman@29540
|
303 |
fix p q r :: "'a poly"
|
huffman@29540
|
304 |
assume "p + q = p + r" thus "q = r"
|
huffman@29540
|
305 |
by (simp add: expand_poly_eq)
|
huffman@29540
|
306 |
qed
|
huffman@29540
|
307 |
|
huffman@29449
|
308 |
instantiation poly :: (ab_group_add) ab_group_add
|
huffman@29449
|
309 |
begin
|
huffman@29449
|
310 |
|
huffman@29449
|
311 |
definition
|
haftmann@37765
|
312 |
uminus_poly_def:
|
huffman@29449
|
313 |
"- p = Abs_poly (\<lambda>n. - coeff p n)"
|
huffman@29449
|
314 |
|
huffman@29449
|
315 |
definition
|
haftmann@37765
|
316 |
minus_poly_def:
|
huffman@29449
|
317 |
"p - q = Abs_poly (\<lambda>n. coeff p n - coeff q n)"
|
huffman@29449
|
318 |
|
huffman@29449
|
319 |
lemma Poly_minus:
|
huffman@29449
|
320 |
fixes f :: "nat \<Rightarrow> 'a"
|
huffman@29449
|
321 |
shows "f \<in> Poly \<Longrightarrow> (\<lambda>n. - f n) \<in> Poly"
|
huffman@29449
|
322 |
unfolding Poly_def by simp
|
huffman@29449
|
323 |
|
huffman@29449
|
324 |
lemma Poly_diff:
|
huffman@29449
|
325 |
fixes f g :: "nat \<Rightarrow> 'a"
|
huffman@29449
|
326 |
shows "\<lbrakk>f \<in> Poly; g \<in> Poly\<rbrakk> \<Longrightarrow> (\<lambda>n. f n - g n) \<in> Poly"
|
huffman@29449
|
327 |
unfolding diff_minus by (simp add: Poly_add Poly_minus)
|
huffman@29449
|
328 |
|
huffman@29449
|
329 |
lemma coeff_minus [simp]: "coeff (- p) n = - coeff p n"
|
huffman@29449
|
330 |
unfolding uminus_poly_def
|
huffman@29449
|
331 |
by (simp add: Abs_poly_inverse coeff Poly_minus)
|
huffman@29449
|
332 |
|
huffman@29449
|
333 |
lemma coeff_diff [simp]:
|
huffman@29449
|
334 |
"coeff (p - q) n = coeff p n - coeff q n"
|
huffman@29449
|
335 |
unfolding minus_poly_def
|
huffman@29449
|
336 |
by (simp add: Abs_poly_inverse coeff Poly_diff)
|
huffman@29449
|
337 |
|
huffman@29449
|
338 |
instance proof
|
huffman@29449
|
339 |
fix p q :: "'a poly"
|
huffman@29449
|
340 |
show "- p + p = 0"
|
huffman@29449
|
341 |
by (simp add: expand_poly_eq)
|
huffman@29449
|
342 |
show "p - q = p + - q"
|
huffman@29449
|
343 |
by (simp add: expand_poly_eq diff_minus)
|
huffman@29449
|
344 |
qed
|
huffman@29449
|
345 |
|
huffman@29449
|
346 |
end
|
huffman@29449
|
347 |
|
huffman@29449
|
348 |
lemma add_pCons [simp]:
|
huffman@29449
|
349 |
"pCons a p + pCons b q = pCons (a + b) (p + q)"
|
huffman@29449
|
350 |
by (rule poly_ext, simp add: coeff_pCons split: nat.split)
|
huffman@29449
|
351 |
|
huffman@29449
|
352 |
lemma minus_pCons [simp]:
|
huffman@29449
|
353 |
"- pCons a p = pCons (- a) (- p)"
|
huffman@29449
|
354 |
by (rule poly_ext, simp add: coeff_pCons split: nat.split)
|
huffman@29449
|
355 |
|
huffman@29449
|
356 |
lemma diff_pCons [simp]:
|
huffman@29449
|
357 |
"pCons a p - pCons b q = pCons (a - b) (p - q)"
|
huffman@29449
|
358 |
by (rule poly_ext, simp add: coeff_pCons split: nat.split)
|
huffman@29449
|
359 |
|
huffman@29539
|
360 |
lemma degree_add_le_max: "degree (p + q) \<le> max (degree p) (degree q)"
|
huffman@29449
|
361 |
by (rule degree_le, auto simp add: coeff_eq_0)
|
huffman@29449
|
362 |
|
huffman@29539
|
363 |
lemma degree_add_le:
|
huffman@29539
|
364 |
"\<lbrakk>degree p \<le> n; degree q \<le> n\<rbrakk> \<Longrightarrow> degree (p + q) \<le> n"
|
huffman@29539
|
365 |
by (auto intro: order_trans degree_add_le_max)
|
huffman@29539
|
366 |
|
huffman@29453
|
367 |
lemma degree_add_less:
|
huffman@29453
|
368 |
"\<lbrakk>degree p < n; degree q < n\<rbrakk> \<Longrightarrow> degree (p + q) < n"
|
huffman@29539
|
369 |
by (auto intro: le_less_trans degree_add_le_max)
|
huffman@29453
|
370 |
|
huffman@29449
|
371 |
lemma degree_add_eq_right:
|
huffman@29449
|
372 |
"degree p < degree q \<Longrightarrow> degree (p + q) = degree q"
|
huffman@29449
|
373 |
apply (cases "q = 0", simp)
|
huffman@29449
|
374 |
apply (rule order_antisym)
|
huffman@29539
|
375 |
apply (simp add: degree_add_le)
|
huffman@29449
|
376 |
apply (rule le_degree)
|
huffman@29449
|
377 |
apply (simp add: coeff_eq_0)
|
huffman@29449
|
378 |
done
|
huffman@29449
|
379 |
|
huffman@29449
|
380 |
lemma degree_add_eq_left:
|
huffman@29449
|
381 |
"degree q < degree p \<Longrightarrow> degree (p + q) = degree p"
|
huffman@29449
|
382 |
using degree_add_eq_right [of q p]
|
huffman@29449
|
383 |
by (simp add: add_commute)
|
huffman@29449
|
384 |
|
huffman@29449
|
385 |
lemma degree_minus [simp]: "degree (- p) = degree p"
|
huffman@29449
|
386 |
unfolding degree_def by simp
|
huffman@29449
|
387 |
|
huffman@29539
|
388 |
lemma degree_diff_le_max: "degree (p - q) \<le> max (degree p) (degree q)"
|
huffman@29449
|
389 |
using degree_add_le [where p=p and q="-q"]
|
huffman@29449
|
390 |
by (simp add: diff_minus)
|
huffman@29449
|
391 |
|
huffman@29539
|
392 |
lemma degree_diff_le:
|
huffman@29539
|
393 |
"\<lbrakk>degree p \<le> n; degree q \<le> n\<rbrakk> \<Longrightarrow> degree (p - q) \<le> n"
|
huffman@29539
|
394 |
by (simp add: diff_minus degree_add_le)
|
huffman@29539
|
395 |
|
huffman@29453
|
396 |
lemma degree_diff_less:
|
huffman@29453
|
397 |
"\<lbrakk>degree p < n; degree q < n\<rbrakk> \<Longrightarrow> degree (p - q) < n"
|
huffman@29539
|
398 |
by (simp add: diff_minus degree_add_less)
|
huffman@29453
|
399 |
|
huffman@29449
|
400 |
lemma add_monom: "monom a n + monom b n = monom (a + b) n"
|
huffman@29449
|
401 |
by (rule poly_ext) simp
|
huffman@29449
|
402 |
|
huffman@29449
|
403 |
lemma diff_monom: "monom a n - monom b n = monom (a - b) n"
|
huffman@29449
|
404 |
by (rule poly_ext) simp
|
huffman@29449
|
405 |
|
huffman@29449
|
406 |
lemma minus_monom: "- monom a n = monom (-a) n"
|
huffman@29449
|
407 |
by (rule poly_ext) simp
|
huffman@29449
|
408 |
|
huffman@29449
|
409 |
lemma coeff_setsum: "coeff (\<Sum>x\<in>A. p x) i = (\<Sum>x\<in>A. coeff (p x) i)"
|
huffman@29449
|
410 |
by (cases "finite A", induct set: finite, simp_all)
|
huffman@29449
|
411 |
|
huffman@29449
|
412 |
lemma monom_setsum: "monom (\<Sum>x\<in>A. a x) n = (\<Sum>x\<in>A. monom (a x) n)"
|
huffman@29449
|
413 |
by (rule poly_ext) (simp add: coeff_setsum)
|
huffman@29449
|
414 |
|
huffman@29449
|
415 |
|
huffman@29449
|
416 |
subsection {* Multiplication by a constant *}
|
huffman@29449
|
417 |
|
huffman@29449
|
418 |
definition
|
huffman@29449
|
419 |
smult :: "'a::comm_semiring_0 \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where
|
huffman@29449
|
420 |
"smult a p = Abs_poly (\<lambda>n. a * coeff p n)"
|
huffman@29449
|
421 |
|
huffman@29449
|
422 |
lemma Poly_smult:
|
huffman@29449
|
423 |
fixes f :: "nat \<Rightarrow> 'a::comm_semiring_0"
|
huffman@29449
|
424 |
shows "f \<in> Poly \<Longrightarrow> (\<lambda>n. a * f n) \<in> Poly"
|
huffman@29449
|
425 |
unfolding Poly_def
|
huffman@29449
|
426 |
by (clarify, rule_tac x=n in exI, simp)
|
huffman@29449
|
427 |
|
huffman@29449
|
428 |
lemma coeff_smult [simp]: "coeff (smult a p) n = a * coeff p n"
|
huffman@29449
|
429 |
unfolding smult_def
|
huffman@29449
|
430 |
by (simp add: Abs_poly_inverse Poly_smult coeff)
|
huffman@29449
|
431 |
|
huffman@29449
|
432 |
lemma degree_smult_le: "degree (smult a p) \<le> degree p"
|
huffman@29449
|
433 |
by (rule degree_le, simp add: coeff_eq_0)
|
huffman@29449
|
434 |
|
huffman@29470
|
435 |
lemma smult_smult [simp]: "smult a (smult b p) = smult (a * b) p"
|
huffman@29449
|
436 |
by (rule poly_ext, simp add: mult_assoc)
|
huffman@29449
|
437 |
|
huffman@29449
|
438 |
lemma smult_0_right [simp]: "smult a 0 = 0"
|
huffman@29449
|
439 |
by (rule poly_ext, simp)
|
huffman@29449
|
440 |
|
huffman@29449
|
441 |
lemma smult_0_left [simp]: "smult 0 p = 0"
|
huffman@29449
|
442 |
by (rule poly_ext, simp)
|
huffman@29449
|
443 |
|
huffman@29449
|
444 |
lemma smult_1_left [simp]: "smult (1::'a::comm_semiring_1) p = p"
|
huffman@29449
|
445 |
by (rule poly_ext, simp)
|
huffman@29449
|
446 |
|
huffman@29449
|
447 |
lemma smult_add_right:
|
huffman@29449
|
448 |
"smult a (p + q) = smult a p + smult a q"
|
nipkow@29667
|
449 |
by (rule poly_ext, simp add: algebra_simps)
|
huffman@29449
|
450 |
|
huffman@29449
|
451 |
lemma smult_add_left:
|
huffman@29449
|
452 |
"smult (a + b) p = smult a p + smult b p"
|
nipkow@29667
|
453 |
by (rule poly_ext, simp add: algebra_simps)
|
huffman@29449
|
454 |
|
huffman@29457
|
455 |
lemma smult_minus_right [simp]:
|
huffman@29449
|
456 |
"smult (a::'a::comm_ring) (- p) = - smult a p"
|
huffman@29449
|
457 |
by (rule poly_ext, simp)
|
huffman@29449
|
458 |
|
huffman@29457
|
459 |
lemma smult_minus_left [simp]:
|
huffman@29449
|
460 |
"smult (- a::'a::comm_ring) p = - smult a p"
|
huffman@29449
|
461 |
by (rule poly_ext, simp)
|
huffman@29449
|
462 |
|
huffman@29449
|
463 |
lemma smult_diff_right:
|
huffman@29449
|
464 |
"smult (a::'a::comm_ring) (p - q) = smult a p - smult a q"
|
nipkow@29667
|
465 |
by (rule poly_ext, simp add: algebra_simps)
|
huffman@29449
|
466 |
|
huffman@29449
|
467 |
lemma smult_diff_left:
|
huffman@29449
|
468 |
"smult (a - b::'a::comm_ring) p = smult a p - smult b p"
|
nipkow@29667
|
469 |
by (rule poly_ext, simp add: algebra_simps)
|
huffman@29449
|
470 |
|
huffman@29470
|
471 |
lemmas smult_distribs =
|
huffman@29470
|
472 |
smult_add_left smult_add_right
|
huffman@29470
|
473 |
smult_diff_left smult_diff_right
|
huffman@29470
|
474 |
|
huffman@29449
|
475 |
lemma smult_pCons [simp]:
|
huffman@29449
|
476 |
"smult a (pCons b p) = pCons (a * b) (smult a p)"
|
huffman@29449
|
477 |
by (rule poly_ext, simp add: coeff_pCons split: nat.split)
|
huffman@29449
|
478 |
|
huffman@29449
|
479 |
lemma smult_monom: "smult a (monom b n) = monom (a * b) n"
|
huffman@29449
|
480 |
by (induct n, simp add: monom_0, simp add: monom_Suc)
|
huffman@29449
|
481 |
|
huffman@29659
|
482 |
lemma degree_smult_eq [simp]:
|
huffman@29659
|
483 |
fixes a :: "'a::idom"
|
huffman@29659
|
484 |
shows "degree (smult a p) = (if a = 0 then 0 else degree p)"
|
huffman@29659
|
485 |
by (cases "a = 0", simp, simp add: degree_def)
|
huffman@29659
|
486 |
|
huffman@29659
|
487 |
lemma smult_eq_0_iff [simp]:
|
huffman@29659
|
488 |
fixes a :: "'a::idom"
|
huffman@29659
|
489 |
shows "smult a p = 0 \<longleftrightarrow> a = 0 \<or> p = 0"
|
huffman@29659
|
490 |
by (simp add: expand_poly_eq)
|
huffman@29659
|
491 |
|
huffman@29449
|
492 |
|
huffman@29449
|
493 |
subsection {* Multiplication of polynomials *}
|
huffman@29449
|
494 |
|
huffman@29472
|
495 |
text {* TODO: move to SetInterval.thy *}
|
huffman@29449
|
496 |
lemma setsum_atMost_Suc_shift:
|
huffman@29449
|
497 |
fixes f :: "nat \<Rightarrow> 'a::comm_monoid_add"
|
huffman@29449
|
498 |
shows "(\<Sum>i\<le>Suc n. f i) = f 0 + (\<Sum>i\<le>n. f (Suc i))"
|
huffman@29449
|
499 |
proof (induct n)
|
huffman@29449
|
500 |
case 0 show ?case by simp
|
huffman@29449
|
501 |
next
|
huffman@29449
|
502 |
case (Suc n) note IH = this
|
huffman@29449
|
503 |
have "(\<Sum>i\<le>Suc (Suc n). f i) = (\<Sum>i\<le>Suc n. f i) + f (Suc (Suc n))"
|
huffman@29449
|
504 |
by (rule setsum_atMost_Suc)
|
huffman@29449
|
505 |
also have "(\<Sum>i\<le>Suc n. f i) = f 0 + (\<Sum>i\<le>n. f (Suc i))"
|
huffman@29449
|
506 |
by (rule IH)
|
huffman@29449
|
507 |
also have "f 0 + (\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)) =
|
huffman@29449
|
508 |
f 0 + ((\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)))"
|
huffman@29449
|
509 |
by (rule add_assoc)
|
huffman@29449
|
510 |
also have "(\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)) = (\<Sum>i\<le>Suc n. f (Suc i))"
|
huffman@29449
|
511 |
by (rule setsum_atMost_Suc [symmetric])
|
huffman@29449
|
512 |
finally show ?case .
|
huffman@29449
|
513 |
qed
|
huffman@29449
|
514 |
|
huffman@29449
|
515 |
instantiation poly :: (comm_semiring_0) comm_semiring_0
|
huffman@29449
|
516 |
begin
|
huffman@29449
|
517 |
|
huffman@29449
|
518 |
definition
|
haftmann@37765
|
519 |
times_poly_def:
|
huffman@29472
|
520 |
"p * q = poly_rec 0 (\<lambda>a p pq. smult a q + pCons 0 pq) p"
|
huffman@29449
|
521 |
|
huffman@29472
|
522 |
lemma mult_poly_0_left: "(0::'a poly) * q = 0"
|
huffman@29472
|
523 |
unfolding times_poly_def by (simp add: poly_rec_0)
|
huffman@29472
|
524 |
|
huffman@29472
|
525 |
lemma mult_pCons_left [simp]:
|
huffman@29472
|
526 |
"pCons a p * q = smult a q + pCons 0 (p * q)"
|
huffman@29472
|
527 |
unfolding times_poly_def by (simp add: poly_rec_pCons)
|
huffman@29472
|
528 |
|
huffman@29472
|
529 |
lemma mult_poly_0_right: "p * (0::'a poly) = 0"
|
huffman@29472
|
530 |
by (induct p, simp add: mult_poly_0_left, simp)
|
huffman@29472
|
531 |
|
huffman@29472
|
532 |
lemma mult_pCons_right [simp]:
|
huffman@29472
|
533 |
"p * pCons a q = smult a p + pCons 0 (p * q)"
|
nipkow@29667
|
534 |
by (induct p, simp add: mult_poly_0_left, simp add: algebra_simps)
|
huffman@29472
|
535 |
|
huffman@29472
|
536 |
lemmas mult_poly_0 = mult_poly_0_left mult_poly_0_right
|
huffman@29472
|
537 |
|
huffman@29472
|
538 |
lemma mult_smult_left [simp]: "smult a p * q = smult a (p * q)"
|
huffman@29472
|
539 |
by (induct p, simp add: mult_poly_0, simp add: smult_add_right)
|
huffman@29472
|
540 |
|
huffman@29472
|
541 |
lemma mult_smult_right [simp]: "p * smult a q = smult a (p * q)"
|
huffman@29472
|
542 |
by (induct q, simp add: mult_poly_0, simp add: smult_add_right)
|
huffman@29472
|
543 |
|
huffman@29472
|
544 |
lemma mult_poly_add_left:
|
huffman@29472
|
545 |
fixes p q r :: "'a poly"
|
huffman@29472
|
546 |
shows "(p + q) * r = p * r + q * r"
|
huffman@29472
|
547 |
by (induct r, simp add: mult_poly_0,
|
nipkow@29667
|
548 |
simp add: smult_distribs algebra_simps)
|
huffman@29449
|
549 |
|
huffman@29449
|
550 |
instance proof
|
huffman@29449
|
551 |
fix p q r :: "'a poly"
|
huffman@29449
|
552 |
show 0: "0 * p = 0"
|
huffman@29472
|
553 |
by (rule mult_poly_0_left)
|
huffman@29449
|
554 |
show "p * 0 = 0"
|
huffman@29472
|
555 |
by (rule mult_poly_0_right)
|
huffman@29449
|
556 |
show "(p + q) * r = p * r + q * r"
|
huffman@29472
|
557 |
by (rule mult_poly_add_left)
|
huffman@29449
|
558 |
show "(p * q) * r = p * (q * r)"
|
huffman@29472
|
559 |
by (induct p, simp add: mult_poly_0, simp add: mult_poly_add_left)
|
huffman@29449
|
560 |
show "p * q = q * p"
|
huffman@29472
|
561 |
by (induct p, simp add: mult_poly_0, simp)
|
huffman@29449
|
562 |
qed
|
huffman@29449
|
563 |
|
huffman@29449
|
564 |
end
|
huffman@29449
|
565 |
|
huffman@29540
|
566 |
instance poly :: (comm_semiring_0_cancel) comm_semiring_0_cancel ..
|
huffman@29540
|
567 |
|
huffman@29472
|
568 |
lemma coeff_mult:
|
huffman@29472
|
569 |
"coeff (p * q) n = (\<Sum>i\<le>n. coeff p i * coeff q (n-i))"
|
huffman@29472
|
570 |
proof (induct p arbitrary: n)
|
huffman@29472
|
571 |
case 0 show ?case by simp
|
huffman@29472
|
572 |
next
|
huffman@29472
|
573 |
case (pCons a p n) thus ?case
|
huffman@29472
|
574 |
by (cases n, simp, simp add: setsum_atMost_Suc_shift
|
huffman@29472
|
575 |
del: setsum_atMost_Suc)
|
huffman@29472
|
576 |
qed
|
huffman@29472
|
577 |
|
huffman@29449
|
578 |
lemma degree_mult_le: "degree (p * q) \<le> degree p + degree q"
|
huffman@29472
|
579 |
apply (rule degree_le)
|
huffman@29472
|
580 |
apply (induct p)
|
huffman@29472
|
581 |
apply simp
|
huffman@29472
|
582 |
apply (simp add: coeff_eq_0 coeff_pCons split: nat.split)
|
huffman@29449
|
583 |
done
|
huffman@29449
|
584 |
|
huffman@29449
|
585 |
lemma mult_monom: "monom a m * monom b n = monom (a * b) (m + n)"
|
huffman@29449
|
586 |
by (induct m, simp add: monom_0 smult_monom, simp add: monom_Suc)
|
huffman@29449
|
587 |
|
huffman@29449
|
588 |
|
huffman@29449
|
589 |
subsection {* The unit polynomial and exponentiation *}
|
huffman@29449
|
590 |
|
huffman@29449
|
591 |
instantiation poly :: (comm_semiring_1) comm_semiring_1
|
huffman@29449
|
592 |
begin
|
huffman@29449
|
593 |
|
huffman@29449
|
594 |
definition
|
huffman@29449
|
595 |
one_poly_def:
|
huffman@29449
|
596 |
"1 = pCons 1 0"
|
huffman@29449
|
597 |
|
huffman@29449
|
598 |
instance proof
|
huffman@29449
|
599 |
fix p :: "'a poly" show "1 * p = p"
|
huffman@29449
|
600 |
unfolding one_poly_def
|
huffman@29449
|
601 |
by simp
|
huffman@29449
|
602 |
next
|
huffman@29449
|
603 |
show "0 \<noteq> (1::'a poly)"
|
huffman@29449
|
604 |
unfolding one_poly_def by simp
|
huffman@29449
|
605 |
qed
|
huffman@29449
|
606 |
|
huffman@29449
|
607 |
end
|
huffman@29449
|
608 |
|
huffman@29540
|
609 |
instance poly :: (comm_semiring_1_cancel) comm_semiring_1_cancel ..
|
huffman@29540
|
610 |
|
huffman@29449
|
611 |
lemma coeff_1 [simp]: "coeff 1 n = (if n = 0 then 1 else 0)"
|
huffman@29449
|
612 |
unfolding one_poly_def
|
huffman@29449
|
613 |
by (simp add: coeff_pCons split: nat.split)
|
huffman@29449
|
614 |
|
huffman@29449
|
615 |
lemma degree_1 [simp]: "degree 1 = 0"
|
huffman@29449
|
616 |
unfolding one_poly_def
|
huffman@29449
|
617 |
by (rule degree_pCons_0)
|
huffman@29449
|
618 |
|
huffman@29916
|
619 |
text {* Lemmas about divisibility *}
|
huffman@29916
|
620 |
|
huffman@29916
|
621 |
lemma dvd_smult: "p dvd q \<Longrightarrow> p dvd smult a q"
|
huffman@29916
|
622 |
proof -
|
huffman@29916
|
623 |
assume "p dvd q"
|
huffman@29916
|
624 |
then obtain k where "q = p * k" ..
|
huffman@29916
|
625 |
then have "smult a q = p * smult a k" by simp
|
huffman@29916
|
626 |
then show "p dvd smult a q" ..
|
huffman@29916
|
627 |
qed
|
huffman@29916
|
628 |
|
huffman@29916
|
629 |
lemma dvd_smult_cancel:
|
huffman@29916
|
630 |
fixes a :: "'a::field"
|
huffman@29916
|
631 |
shows "p dvd smult a q \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> p dvd q"
|
huffman@29916
|
632 |
by (drule dvd_smult [where a="inverse a"]) simp
|
huffman@29916
|
633 |
|
huffman@29916
|
634 |
lemma dvd_smult_iff:
|
huffman@29916
|
635 |
fixes a :: "'a::field"
|
huffman@29916
|
636 |
shows "a \<noteq> 0 \<Longrightarrow> p dvd smult a q \<longleftrightarrow> p dvd q"
|
huffman@29916
|
637 |
by (safe elim!: dvd_smult dvd_smult_cancel)
|
huffman@29916
|
638 |
|
huffman@31663
|
639 |
lemma smult_dvd_cancel:
|
huffman@31663
|
640 |
"smult a p dvd q \<Longrightarrow> p dvd q"
|
huffman@31663
|
641 |
proof -
|
huffman@31663
|
642 |
assume "smult a p dvd q"
|
huffman@31663
|
643 |
then obtain k where "q = smult a p * k" ..
|
huffman@31663
|
644 |
then have "q = p * smult a k" by simp
|
huffman@31663
|
645 |
then show "p dvd q" ..
|
huffman@31663
|
646 |
qed
|
huffman@31663
|
647 |
|
huffman@31663
|
648 |
lemma smult_dvd:
|
huffman@31663
|
649 |
fixes a :: "'a::field"
|
huffman@31663
|
650 |
shows "p dvd q \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> smult a p dvd q"
|
huffman@31663
|
651 |
by (rule smult_dvd_cancel [where a="inverse a"]) simp
|
huffman@31663
|
652 |
|
huffman@31663
|
653 |
lemma smult_dvd_iff:
|
huffman@31663
|
654 |
fixes a :: "'a::field"
|
huffman@31663
|
655 |
shows "smult a p dvd q \<longleftrightarrow> (if a = 0 then q = 0 else p dvd q)"
|
huffman@31663
|
656 |
by (auto elim: smult_dvd smult_dvd_cancel)
|
huffman@31663
|
657 |
|
huffman@29916
|
658 |
lemma degree_power_le: "degree (p ^ n) \<le> degree p * n"
|
huffman@29916
|
659 |
by (induct n, simp, auto intro: order_trans degree_mult_le)
|
huffman@29916
|
660 |
|
huffman@29449
|
661 |
instance poly :: (comm_ring) comm_ring ..
|
huffman@29449
|
662 |
|
huffman@29449
|
663 |
instance poly :: (comm_ring_1) comm_ring_1 ..
|
huffman@29449
|
664 |
|
huffman@29449
|
665 |
instantiation poly :: (comm_ring_1) number_ring
|
huffman@29449
|
666 |
begin
|
huffman@29449
|
667 |
|
huffman@29449
|
668 |
definition
|
huffman@29449
|
669 |
"number_of k = (of_int k :: 'a poly)"
|
huffman@29449
|
670 |
|
huffman@29449
|
671 |
instance
|
huffman@29449
|
672 |
by default (rule number_of_poly_def)
|
huffman@29449
|
673 |
|
huffman@29449
|
674 |
end
|
huffman@29449
|
675 |
|
huffman@29449
|
676 |
|
huffman@29449
|
677 |
subsection {* Polynomials form an integral domain *}
|
huffman@29449
|
678 |
|
huffman@29449
|
679 |
lemma coeff_mult_degree_sum:
|
huffman@29449
|
680 |
"coeff (p * q) (degree p + degree q) =
|
huffman@29449
|
681 |
coeff p (degree p) * coeff q (degree q)"
|
huffman@29469
|
682 |
by (induct p, simp, simp add: coeff_eq_0)
|
huffman@29449
|
683 |
|
huffman@29449
|
684 |
instance poly :: (idom) idom
|
huffman@29449
|
685 |
proof
|
huffman@29449
|
686 |
fix p q :: "'a poly"
|
huffman@29449
|
687 |
assume "p \<noteq> 0" and "q \<noteq> 0"
|
huffman@29449
|
688 |
have "coeff (p * q) (degree p + degree q) =
|
huffman@29449
|
689 |
coeff p (degree p) * coeff q (degree q)"
|
huffman@29449
|
690 |
by (rule coeff_mult_degree_sum)
|
huffman@29449
|
691 |
also have "coeff p (degree p) * coeff q (degree q) \<noteq> 0"
|
huffman@29449
|
692 |
using `p \<noteq> 0` and `q \<noteq> 0` by simp
|
huffman@29449
|
693 |
finally have "\<exists>n. coeff (p * q) n \<noteq> 0" ..
|
huffman@29449
|
694 |
thus "p * q \<noteq> 0" by (simp add: expand_poly_eq)
|
huffman@29449
|
695 |
qed
|
huffman@29449
|
696 |
|
huffman@29449
|
697 |
lemma degree_mult_eq:
|
huffman@29449
|
698 |
fixes p q :: "'a::idom poly"
|
huffman@29449
|
699 |
shows "\<lbrakk>p \<noteq> 0; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree (p * q) = degree p + degree q"
|
huffman@29449
|
700 |
apply (rule order_antisym [OF degree_mult_le le_degree])
|
huffman@29449
|
701 |
apply (simp add: coeff_mult_degree_sum)
|
huffman@29449
|
702 |
done
|
huffman@29449
|
703 |
|
huffman@29449
|
704 |
lemma dvd_imp_degree_le:
|
huffman@29449
|
705 |
fixes p q :: "'a::idom poly"
|
huffman@29449
|
706 |
shows "\<lbrakk>p dvd q; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree p \<le> degree q"
|
huffman@29449
|
707 |
by (erule dvdE, simp add: degree_mult_eq)
|
huffman@29449
|
708 |
|
huffman@29449
|
709 |
|
huffman@29815
|
710 |
subsection {* Polynomials form an ordered integral domain *}
|
huffman@29815
|
711 |
|
huffman@29815
|
712 |
definition
|
haftmann@35028
|
713 |
pos_poly :: "'a::linordered_idom poly \<Rightarrow> bool"
|
huffman@29815
|
714 |
where
|
huffman@29815
|
715 |
"pos_poly p \<longleftrightarrow> 0 < coeff p (degree p)"
|
huffman@29815
|
716 |
|
huffman@29815
|
717 |
lemma pos_poly_pCons:
|
huffman@29815
|
718 |
"pos_poly (pCons a p) \<longleftrightarrow> pos_poly p \<or> (p = 0 \<and> 0 < a)"
|
huffman@29815
|
719 |
unfolding pos_poly_def by simp
|
huffman@29815
|
720 |
|
huffman@29815
|
721 |
lemma not_pos_poly_0 [simp]: "\<not> pos_poly 0"
|
huffman@29815
|
722 |
unfolding pos_poly_def by simp
|
huffman@29815
|
723 |
|
huffman@29815
|
724 |
lemma pos_poly_add: "\<lbrakk>pos_poly p; pos_poly q\<rbrakk> \<Longrightarrow> pos_poly (p + q)"
|
huffman@29815
|
725 |
apply (induct p arbitrary: q, simp)
|
huffman@29815
|
726 |
apply (case_tac q, force simp add: pos_poly_pCons add_pos_pos)
|
huffman@29815
|
727 |
done
|
huffman@29815
|
728 |
|
huffman@29815
|
729 |
lemma pos_poly_mult: "\<lbrakk>pos_poly p; pos_poly q\<rbrakk> \<Longrightarrow> pos_poly (p * q)"
|
huffman@29815
|
730 |
unfolding pos_poly_def
|
huffman@29815
|
731 |
apply (subgoal_tac "p \<noteq> 0 \<and> q \<noteq> 0")
|
huffman@29815
|
732 |
apply (simp add: degree_mult_eq coeff_mult_degree_sum mult_pos_pos)
|
huffman@29815
|
733 |
apply auto
|
huffman@29815
|
734 |
done
|
huffman@29815
|
735 |
|
huffman@29815
|
736 |
lemma pos_poly_total: "p = 0 \<or> pos_poly p \<or> pos_poly (- p)"
|
huffman@29815
|
737 |
by (induct p) (auto simp add: pos_poly_pCons)
|
huffman@29815
|
738 |
|
haftmann@35028
|
739 |
instantiation poly :: (linordered_idom) linordered_idom
|
huffman@29815
|
740 |
begin
|
huffman@29815
|
741 |
|
huffman@29815
|
742 |
definition
|
haftmann@37765
|
743 |
"x < y \<longleftrightarrow> pos_poly (y - x)"
|
huffman@29815
|
744 |
|
huffman@29815
|
745 |
definition
|
haftmann@37765
|
746 |
"x \<le> y \<longleftrightarrow> x = y \<or> pos_poly (y - x)"
|
huffman@29815
|
747 |
|
huffman@29815
|
748 |
definition
|
haftmann@37765
|
749 |
"abs (x::'a poly) = (if x < 0 then - x else x)"
|
huffman@29815
|
750 |
|
huffman@29815
|
751 |
definition
|
haftmann@37765
|
752 |
"sgn (x::'a poly) = (if x = 0 then 0 else if 0 < x then 1 else - 1)"
|
huffman@29815
|
753 |
|
huffman@29815
|
754 |
instance proof
|
huffman@29815
|
755 |
fix x y :: "'a poly"
|
huffman@29815
|
756 |
show "x < y \<longleftrightarrow> x \<le> y \<and> \<not> y \<le> x"
|
huffman@29815
|
757 |
unfolding less_eq_poly_def less_poly_def
|
huffman@29815
|
758 |
apply safe
|
huffman@29815
|
759 |
apply simp
|
huffman@29815
|
760 |
apply (drule (1) pos_poly_add)
|
huffman@29815
|
761 |
apply simp
|
huffman@29815
|
762 |
done
|
huffman@29815
|
763 |
next
|
huffman@29815
|
764 |
fix x :: "'a poly" show "x \<le> x"
|
huffman@29815
|
765 |
unfolding less_eq_poly_def by simp
|
huffman@29815
|
766 |
next
|
huffman@29815
|
767 |
fix x y z :: "'a poly"
|
huffman@29815
|
768 |
assume "x \<le> y" and "y \<le> z" thus "x \<le> z"
|
huffman@29815
|
769 |
unfolding less_eq_poly_def
|
huffman@29815
|
770 |
apply safe
|
huffman@29815
|
771 |
apply (drule (1) pos_poly_add)
|
huffman@29815
|
772 |
apply (simp add: algebra_simps)
|
huffman@29815
|
773 |
done
|
huffman@29815
|
774 |
next
|
huffman@29815
|
775 |
fix x y :: "'a poly"
|
huffman@29815
|
776 |
assume "x \<le> y" and "y \<le> x" thus "x = y"
|
huffman@29815
|
777 |
unfolding less_eq_poly_def
|
huffman@29815
|
778 |
apply safe
|
huffman@29815
|
779 |
apply (drule (1) pos_poly_add)
|
huffman@29815
|
780 |
apply simp
|
huffman@29815
|
781 |
done
|
huffman@29815
|
782 |
next
|
huffman@29815
|
783 |
fix x y z :: "'a poly"
|
huffman@29815
|
784 |
assume "x \<le> y" thus "z + x \<le> z + y"
|
huffman@29815
|
785 |
unfolding less_eq_poly_def
|
huffman@29815
|
786 |
apply safe
|
huffman@29815
|
787 |
apply (simp add: algebra_simps)
|
huffman@29815
|
788 |
done
|
huffman@29815
|
789 |
next
|
huffman@29815
|
790 |
fix x y :: "'a poly"
|
huffman@29815
|
791 |
show "x \<le> y \<or> y \<le> x"
|
huffman@29815
|
792 |
unfolding less_eq_poly_def
|
huffman@29815
|
793 |
using pos_poly_total [of "x - y"]
|
huffman@29815
|
794 |
by auto
|
huffman@29815
|
795 |
next
|
huffman@29815
|
796 |
fix x y z :: "'a poly"
|
huffman@29815
|
797 |
assume "x < y" and "0 < z"
|
huffman@29815
|
798 |
thus "z * x < z * y"
|
huffman@29815
|
799 |
unfolding less_poly_def
|
huffman@29815
|
800 |
by (simp add: right_diff_distrib [symmetric] pos_poly_mult)
|
huffman@29815
|
801 |
next
|
huffman@29815
|
802 |
fix x :: "'a poly"
|
huffman@29815
|
803 |
show "\<bar>x\<bar> = (if x < 0 then - x else x)"
|
huffman@29815
|
804 |
by (rule abs_poly_def)
|
huffman@29815
|
805 |
next
|
huffman@29815
|
806 |
fix x :: "'a poly"
|
huffman@29815
|
807 |
show "sgn x = (if x = 0 then 0 else if 0 < x then 1 else - 1)"
|
huffman@29815
|
808 |
by (rule sgn_poly_def)
|
huffman@29815
|
809 |
qed
|
huffman@29815
|
810 |
|
huffman@29815
|
811 |
end
|
huffman@29815
|
812 |
|
huffman@29815
|
813 |
text {* TODO: Simplification rules for comparisons *}
|
huffman@29815
|
814 |
|
huffman@29815
|
815 |
|
huffman@29449
|
816 |
subsection {* Long division of polynomials *}
|
huffman@29449
|
817 |
|
huffman@29449
|
818 |
definition
|
huffman@29537
|
819 |
pdivmod_rel :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> bool"
|
huffman@29449
|
820 |
where
|
huffman@29537
|
821 |
"pdivmod_rel x y q r \<longleftrightarrow>
|
huffman@29449
|
822 |
x = q * y + r \<and> (if y = 0 then q = 0 else r = 0 \<or> degree r < degree y)"
|
huffman@29449
|
823 |
|
huffman@29537
|
824 |
lemma pdivmod_rel_0:
|
huffman@29537
|
825 |
"pdivmod_rel 0 y 0 0"
|
huffman@29537
|
826 |
unfolding pdivmod_rel_def by simp
|
huffman@29449
|
827 |
|
huffman@29537
|
828 |
lemma pdivmod_rel_by_0:
|
huffman@29537
|
829 |
"pdivmod_rel x 0 0 x"
|
huffman@29537
|
830 |
unfolding pdivmod_rel_def by simp
|
huffman@29449
|
831 |
|
huffman@29449
|
832 |
lemma eq_zero_or_degree_less:
|
huffman@29449
|
833 |
assumes "degree p \<le> n" and "coeff p n = 0"
|
huffman@29449
|
834 |
shows "p = 0 \<or> degree p < n"
|
huffman@29449
|
835 |
proof (cases n)
|
huffman@29449
|
836 |
case 0
|
huffman@29449
|
837 |
with `degree p \<le> n` and `coeff p n = 0`
|
huffman@29449
|
838 |
have "coeff p (degree p) = 0" by simp
|
huffman@29449
|
839 |
then have "p = 0" by simp
|
huffman@29449
|
840 |
then show ?thesis ..
|
huffman@29449
|
841 |
next
|
huffman@29449
|
842 |
case (Suc m)
|
huffman@29449
|
843 |
have "\<forall>i>n. coeff p i = 0"
|
huffman@29449
|
844 |
using `degree p \<le> n` by (simp add: coeff_eq_0)
|
huffman@29449
|
845 |
then have "\<forall>i\<ge>n. coeff p i = 0"
|
huffman@29449
|
846 |
using `coeff p n = 0` by (simp add: le_less)
|
huffman@29449
|
847 |
then have "\<forall>i>m. coeff p i = 0"
|
huffman@29449
|
848 |
using `n = Suc m` by (simp add: less_eq_Suc_le)
|
huffman@29449
|
849 |
then have "degree p \<le> m"
|
huffman@29449
|
850 |
by (rule degree_le)
|
huffman@29449
|
851 |
then have "degree p < n"
|
huffman@29449
|
852 |
using `n = Suc m` by (simp add: less_Suc_eq_le)
|
huffman@29449
|
853 |
then show ?thesis ..
|
huffman@29449
|
854 |
qed
|
huffman@29449
|
855 |
|
huffman@29537
|
856 |
lemma pdivmod_rel_pCons:
|
huffman@29537
|
857 |
assumes rel: "pdivmod_rel x y q r"
|
huffman@29449
|
858 |
assumes y: "y \<noteq> 0"
|
huffman@29449
|
859 |
assumes b: "b = coeff (pCons a r) (degree y) / coeff y (degree y)"
|
huffman@29537
|
860 |
shows "pdivmod_rel (pCons a x) y (pCons b q) (pCons a r - smult b y)"
|
huffman@29537
|
861 |
(is "pdivmod_rel ?x y ?q ?r")
|
huffman@29449
|
862 |
proof -
|
huffman@29449
|
863 |
have x: "x = q * y + r" and r: "r = 0 \<or> degree r < degree y"
|
huffman@29537
|
864 |
using assms unfolding pdivmod_rel_def by simp_all
|
huffman@29449
|
865 |
|
huffman@29449
|
866 |
have 1: "?x = ?q * y + ?r"
|
huffman@29449
|
867 |
using b x by simp
|
huffman@29449
|
868 |
|
huffman@29449
|
869 |
have 2: "?r = 0 \<or> degree ?r < degree y"
|
huffman@29449
|
870 |
proof (rule eq_zero_or_degree_less)
|
huffman@29539
|
871 |
show "degree ?r \<le> degree y"
|
huffman@29539
|
872 |
proof (rule degree_diff_le)
|
huffman@29449
|
873 |
show "degree (pCons a r) \<le> degree y"
|
huffman@29458
|
874 |
using r by auto
|
huffman@29449
|
875 |
show "degree (smult b y) \<le> degree y"
|
huffman@29449
|
876 |
by (rule degree_smult_le)
|
huffman@29449
|
877 |
qed
|
huffman@29449
|
878 |
next
|
huffman@29449
|
879 |
show "coeff ?r (degree y) = 0"
|
huffman@29449
|
880 |
using `y \<noteq> 0` unfolding b by simp
|
huffman@29449
|
881 |
qed
|
huffman@29449
|
882 |
|
huffman@29449
|
883 |
from 1 2 show ?thesis
|
huffman@29537
|
884 |
unfolding pdivmod_rel_def
|
huffman@29449
|
885 |
using `y \<noteq> 0` by simp
|
huffman@29449
|
886 |
qed
|
huffman@29449
|
887 |
|
huffman@29537
|
888 |
lemma pdivmod_rel_exists: "\<exists>q r. pdivmod_rel x y q r"
|
huffman@29449
|
889 |
apply (cases "y = 0")
|
huffman@29537
|
890 |
apply (fast intro!: pdivmod_rel_by_0)
|
huffman@29449
|
891 |
apply (induct x)
|
huffman@29537
|
892 |
apply (fast intro!: pdivmod_rel_0)
|
huffman@29537
|
893 |
apply (fast intro!: pdivmod_rel_pCons)
|
huffman@29449
|
894 |
done
|
huffman@29449
|
895 |
|
huffman@29537
|
896 |
lemma pdivmod_rel_unique:
|
huffman@29537
|
897 |
assumes 1: "pdivmod_rel x y q1 r1"
|
huffman@29537
|
898 |
assumes 2: "pdivmod_rel x y q2 r2"
|
huffman@29449
|
899 |
shows "q1 = q2 \<and> r1 = r2"
|
huffman@29449
|
900 |
proof (cases "y = 0")
|
huffman@29449
|
901 |
assume "y = 0" with assms show ?thesis
|
huffman@29537
|
902 |
by (simp add: pdivmod_rel_def)
|
huffman@29449
|
903 |
next
|
huffman@29449
|
904 |
assume [simp]: "y \<noteq> 0"
|
huffman@29449
|
905 |
from 1 have q1: "x = q1 * y + r1" and r1: "r1 = 0 \<or> degree r1 < degree y"
|
huffman@29537
|
906 |
unfolding pdivmod_rel_def by simp_all
|
huffman@29449
|
907 |
from 2 have q2: "x = q2 * y + r2" and r2: "r2 = 0 \<or> degree r2 < degree y"
|
huffman@29537
|
908 |
unfolding pdivmod_rel_def by simp_all
|
huffman@29449
|
909 |
from q1 q2 have q3: "(q1 - q2) * y = r2 - r1"
|
nipkow@29667
|
910 |
by (simp add: algebra_simps)
|
huffman@29449
|
911 |
from r1 r2 have r3: "(r2 - r1) = 0 \<or> degree (r2 - r1) < degree y"
|
huffman@29453
|
912 |
by (auto intro: degree_diff_less)
|
huffman@29449
|
913 |
|
huffman@29449
|
914 |
show "q1 = q2 \<and> r1 = r2"
|
huffman@29449
|
915 |
proof (rule ccontr)
|
huffman@29449
|
916 |
assume "\<not> (q1 = q2 \<and> r1 = r2)"
|
huffman@29449
|
917 |
with q3 have "q1 \<noteq> q2" and "r1 \<noteq> r2" by auto
|
huffman@29449
|
918 |
with r3 have "degree (r2 - r1) < degree y" by simp
|
huffman@29449
|
919 |
also have "degree y \<le> degree (q1 - q2) + degree y" by simp
|
huffman@29449
|
920 |
also have "\<dots> = degree ((q1 - q2) * y)"
|
huffman@29449
|
921 |
using `q1 \<noteq> q2` by (simp add: degree_mult_eq)
|
huffman@29449
|
922 |
also have "\<dots> = degree (r2 - r1)"
|
huffman@29449
|
923 |
using q3 by simp
|
huffman@29449
|
924 |
finally have "degree (r2 - r1) < degree (r2 - r1)" .
|
huffman@29449
|
925 |
then show "False" by simp
|
huffman@29449
|
926 |
qed
|
huffman@29449
|
927 |
qed
|
huffman@29449
|
928 |
|
huffman@29660
|
929 |
lemma pdivmod_rel_0_iff: "pdivmod_rel 0 y q r \<longleftrightarrow> q = 0 \<and> r = 0"
|
huffman@29660
|
930 |
by (auto dest: pdivmod_rel_unique intro: pdivmod_rel_0)
|
huffman@29660
|
931 |
|
huffman@29660
|
932 |
lemma pdivmod_rel_by_0_iff: "pdivmod_rel x 0 q r \<longleftrightarrow> q = 0 \<and> r = x"
|
huffman@29660
|
933 |
by (auto dest: pdivmod_rel_unique intro: pdivmod_rel_by_0)
|
huffman@29660
|
934 |
|
wenzelm@46476
|
935 |
lemmas pdivmod_rel_unique_div = pdivmod_rel_unique [THEN conjunct1]
|
huffman@29449
|
936 |
|
wenzelm@46476
|
937 |
lemmas pdivmod_rel_unique_mod = pdivmod_rel_unique [THEN conjunct2]
|
huffman@29449
|
938 |
|
huffman@29449
|
939 |
instantiation poly :: (field) ring_div
|
huffman@29449
|
940 |
begin
|
huffman@29449
|
941 |
|
huffman@29449
|
942 |
definition div_poly where
|
haftmann@37765
|
943 |
"x div y = (THE q. \<exists>r. pdivmod_rel x y q r)"
|
huffman@29449
|
944 |
|
huffman@29449
|
945 |
definition mod_poly where
|
haftmann@37765
|
946 |
"x mod y = (THE r. \<exists>q. pdivmod_rel x y q r)"
|
huffman@29449
|
947 |
|
huffman@29449
|
948 |
lemma div_poly_eq:
|
huffman@29537
|
949 |
"pdivmod_rel x y q r \<Longrightarrow> x div y = q"
|
huffman@29449
|
950 |
unfolding div_poly_def
|
huffman@29537
|
951 |
by (fast elim: pdivmod_rel_unique_div)
|
huffman@29449
|
952 |
|
huffman@29449
|
953 |
lemma mod_poly_eq:
|
huffman@29537
|
954 |
"pdivmod_rel x y q r \<Longrightarrow> x mod y = r"
|
huffman@29449
|
955 |
unfolding mod_poly_def
|
huffman@29537
|
956 |
by (fast elim: pdivmod_rel_unique_mod)
|
huffman@29449
|
957 |
|
huffman@29537
|
958 |
lemma pdivmod_rel:
|
huffman@29537
|
959 |
"pdivmod_rel x y (x div y) (x mod y)"
|
huffman@29449
|
960 |
proof -
|
huffman@29537
|
961 |
from pdivmod_rel_exists
|
huffman@29537
|
962 |
obtain q r where "pdivmod_rel x y q r" by fast
|
huffman@29449
|
963 |
thus ?thesis
|
huffman@29449
|
964 |
by (simp add: div_poly_eq mod_poly_eq)
|
huffman@29449
|
965 |
qed
|
huffman@29449
|
966 |
|
huffman@29449
|
967 |
instance proof
|
huffman@29449
|
968 |
fix x y :: "'a poly"
|
huffman@29449
|
969 |
show "x div y * y + x mod y = x"
|
huffman@29537
|
970 |
using pdivmod_rel [of x y]
|
huffman@29537
|
971 |
by (simp add: pdivmod_rel_def)
|
huffman@29449
|
972 |
next
|
huffman@29449
|
973 |
fix x :: "'a poly"
|
huffman@29537
|
974 |
have "pdivmod_rel x 0 0 x"
|
huffman@29537
|
975 |
by (rule pdivmod_rel_by_0)
|
huffman@29449
|
976 |
thus "x div 0 = 0"
|
huffman@29449
|
977 |
by (rule div_poly_eq)
|
huffman@29449
|
978 |
next
|
huffman@29449
|
979 |
fix y :: "'a poly"
|
huffman@29537
|
980 |
have "pdivmod_rel 0 y 0 0"
|
huffman@29537
|
981 |
by (rule pdivmod_rel_0)
|
huffman@29449
|
982 |
thus "0 div y = 0"
|
huffman@29449
|
983 |
by (rule div_poly_eq)
|
huffman@29449
|
984 |
next
|
huffman@29449
|
985 |
fix x y z :: "'a poly"
|
huffman@29449
|
986 |
assume "y \<noteq> 0"
|
huffman@29537
|
987 |
hence "pdivmod_rel (x + z * y) y (z + x div y) (x mod y)"
|
huffman@29537
|
988 |
using pdivmod_rel [of x y]
|
huffman@29537
|
989 |
by (simp add: pdivmod_rel_def left_distrib)
|
huffman@29449
|
990 |
thus "(x + z * y) div y = z + x div y"
|
huffman@29449
|
991 |
by (rule div_poly_eq)
|
haftmann@30930
|
992 |
next
|
haftmann@30930
|
993 |
fix x y z :: "'a poly"
|
haftmann@30930
|
994 |
assume "x \<noteq> 0"
|
haftmann@30930
|
995 |
show "(x * y) div (x * z) = y div z"
|
haftmann@30930
|
996 |
proof (cases "y \<noteq> 0 \<and> z \<noteq> 0")
|
haftmann@30930
|
997 |
have "\<And>x::'a poly. pdivmod_rel x 0 0 x"
|
haftmann@30930
|
998 |
by (rule pdivmod_rel_by_0)
|
haftmann@30930
|
999 |
then have [simp]: "\<And>x::'a poly. x div 0 = 0"
|
haftmann@30930
|
1000 |
by (rule div_poly_eq)
|
haftmann@30930
|
1001 |
have "\<And>x::'a poly. pdivmod_rel 0 x 0 0"
|
haftmann@30930
|
1002 |
by (rule pdivmod_rel_0)
|
haftmann@30930
|
1003 |
then have [simp]: "\<And>x::'a poly. 0 div x = 0"
|
haftmann@30930
|
1004 |
by (rule div_poly_eq)
|
haftmann@30930
|
1005 |
case False then show ?thesis by auto
|
haftmann@30930
|
1006 |
next
|
haftmann@30930
|
1007 |
case True then have "y \<noteq> 0" and "z \<noteq> 0" by auto
|
haftmann@30930
|
1008 |
with `x \<noteq> 0`
|
haftmann@30930
|
1009 |
have "\<And>q r. pdivmod_rel y z q r \<Longrightarrow> pdivmod_rel (x * y) (x * z) q (x * r)"
|
haftmann@30930
|
1010 |
by (auto simp add: pdivmod_rel_def algebra_simps)
|
haftmann@30930
|
1011 |
(rule classical, simp add: degree_mult_eq)
|
haftmann@30930
|
1012 |
moreover from pdivmod_rel have "pdivmod_rel y z (y div z) (y mod z)" .
|
haftmann@30930
|
1013 |
ultimately have "pdivmod_rel (x * y) (x * z) (y div z) (x * (y mod z))" .
|
haftmann@30930
|
1014 |
then show ?thesis by (simp add: div_poly_eq)
|
haftmann@30930
|
1015 |
qed
|
huffman@29449
|
1016 |
qed
|
huffman@29449
|
1017 |
|
huffman@29449
|
1018 |
end
|
huffman@29449
|
1019 |
|
huffman@29449
|
1020 |
lemma degree_mod_less:
|
huffman@29449
|
1021 |
"y \<noteq> 0 \<Longrightarrow> x mod y = 0 \<or> degree (x mod y) < degree y"
|
huffman@29537
|
1022 |
using pdivmod_rel [of x y]
|
huffman@29537
|
1023 |
unfolding pdivmod_rel_def by simp
|
huffman@29449
|
1024 |
|
huffman@29449
|
1025 |
lemma div_poly_less: "degree x < degree y \<Longrightarrow> x div y = 0"
|
huffman@29449
|
1026 |
proof -
|
huffman@29449
|
1027 |
assume "degree x < degree y"
|
huffman@29537
|
1028 |
hence "pdivmod_rel x y 0 x"
|
huffman@29537
|
1029 |
by (simp add: pdivmod_rel_def)
|
huffman@29449
|
1030 |
thus "x div y = 0" by (rule div_poly_eq)
|
huffman@29449
|
1031 |
qed
|
huffman@29449
|
1032 |
|
huffman@29449
|
1033 |
lemma mod_poly_less: "degree x < degree y \<Longrightarrow> x mod y = x"
|
huffman@29449
|
1034 |
proof -
|
huffman@29449
|
1035 |
assume "degree x < degree y"
|
huffman@29537
|
1036 |
hence "pdivmod_rel x y 0 x"
|
huffman@29537
|
1037 |
by (simp add: pdivmod_rel_def)
|
huffman@29449
|
1038 |
thus "x mod y = x" by (rule mod_poly_eq)
|
huffman@29449
|
1039 |
qed
|
huffman@29449
|
1040 |
|
huffman@29659
|
1041 |
lemma pdivmod_rel_smult_left:
|
huffman@29659
|
1042 |
"pdivmod_rel x y q r
|
huffman@29659
|
1043 |
\<Longrightarrow> pdivmod_rel (smult a x) y (smult a q) (smult a r)"
|
huffman@29659
|
1044 |
unfolding pdivmod_rel_def by (simp add: smult_add_right)
|
huffman@29659
|
1045 |
|
huffman@29659
|
1046 |
lemma div_smult_left: "(smult a x) div y = smult a (x div y)"
|
huffman@29659
|
1047 |
by (rule div_poly_eq, rule pdivmod_rel_smult_left, rule pdivmod_rel)
|
huffman@29659
|
1048 |
|
huffman@29659
|
1049 |
lemma mod_smult_left: "(smult a x) mod y = smult a (x mod y)"
|
huffman@29659
|
1050 |
by (rule mod_poly_eq, rule pdivmod_rel_smult_left, rule pdivmod_rel)
|
huffman@29659
|
1051 |
|
huffman@30009
|
1052 |
lemma poly_div_minus_left [simp]:
|
huffman@30009
|
1053 |
fixes x y :: "'a::field poly"
|
huffman@30009
|
1054 |
shows "(- x) div y = - (x div y)"
|
huffman@30009
|
1055 |
using div_smult_left [of "- 1::'a"] by simp
|
huffman@30009
|
1056 |
|
huffman@30009
|
1057 |
lemma poly_mod_minus_left [simp]:
|
huffman@30009
|
1058 |
fixes x y :: "'a::field poly"
|
huffman@30009
|
1059 |
shows "(- x) mod y = - (x mod y)"
|
huffman@30009
|
1060 |
using mod_smult_left [of "- 1::'a"] by simp
|
huffman@30009
|
1061 |
|
huffman@29659
|
1062 |
lemma pdivmod_rel_smult_right:
|
huffman@29659
|
1063 |
"\<lbrakk>a \<noteq> 0; pdivmod_rel x y q r\<rbrakk>
|
huffman@29659
|
1064 |
\<Longrightarrow> pdivmod_rel x (smult a y) (smult (inverse a) q) r"
|
huffman@29659
|
1065 |
unfolding pdivmod_rel_def by simp
|
huffman@29659
|
1066 |
|
huffman@29659
|
1067 |
lemma div_smult_right:
|
huffman@29659
|
1068 |
"a \<noteq> 0 \<Longrightarrow> x div (smult a y) = smult (inverse a) (x div y)"
|
huffman@29659
|
1069 |
by (rule div_poly_eq, erule pdivmod_rel_smult_right, rule pdivmod_rel)
|
huffman@29659
|
1070 |
|
huffman@29659
|
1071 |
lemma mod_smult_right: "a \<noteq> 0 \<Longrightarrow> x mod (smult a y) = x mod y"
|
huffman@29659
|
1072 |
by (rule mod_poly_eq, erule pdivmod_rel_smult_right, rule pdivmod_rel)
|
huffman@29659
|
1073 |
|
huffman@30009
|
1074 |
lemma poly_div_minus_right [simp]:
|
huffman@30009
|
1075 |
fixes x y :: "'a::field poly"
|
huffman@30009
|
1076 |
shows "x div (- y) = - (x div y)"
|
huffman@30009
|
1077 |
using div_smult_right [of "- 1::'a"]
|
huffman@30009
|
1078 |
by (simp add: nonzero_inverse_minus_eq)
|
huffman@30009
|
1079 |
|
huffman@30009
|
1080 |
lemma poly_mod_minus_right [simp]:
|
huffman@30009
|
1081 |
fixes x y :: "'a::field poly"
|
huffman@30009
|
1082 |
shows "x mod (- y) = x mod y"
|
huffman@30009
|
1083 |
using mod_smult_right [of "- 1::'a"] by simp
|
huffman@30009
|
1084 |
|
huffman@29660
|
1085 |
lemma pdivmod_rel_mult:
|
huffman@29660
|
1086 |
"\<lbrakk>pdivmod_rel x y q r; pdivmod_rel q z q' r'\<rbrakk>
|
huffman@29660
|
1087 |
\<Longrightarrow> pdivmod_rel x (y * z) q' (y * r' + r)"
|
huffman@29660
|
1088 |
apply (cases "z = 0", simp add: pdivmod_rel_def)
|
huffman@29660
|
1089 |
apply (cases "y = 0", simp add: pdivmod_rel_by_0_iff pdivmod_rel_0_iff)
|
huffman@29660
|
1090 |
apply (cases "r = 0")
|
huffman@29660
|
1091 |
apply (cases "r' = 0")
|
huffman@29660
|
1092 |
apply (simp add: pdivmod_rel_def)
|
haftmann@36349
|
1093 |
apply (simp add: pdivmod_rel_def field_simps degree_mult_eq)
|
huffman@29660
|
1094 |
apply (cases "r' = 0")
|
huffman@29660
|
1095 |
apply (simp add: pdivmod_rel_def degree_mult_eq)
|
haftmann@36349
|
1096 |
apply (simp add: pdivmod_rel_def field_simps)
|
huffman@29660
|
1097 |
apply (simp add: degree_mult_eq degree_add_less)
|
huffman@29660
|
1098 |
done
|
huffman@29660
|
1099 |
|
huffman@29660
|
1100 |
lemma poly_div_mult_right:
|
huffman@29660
|
1101 |
fixes x y z :: "'a::field poly"
|
huffman@29660
|
1102 |
shows "x div (y * z) = (x div y) div z"
|
huffman@29660
|
1103 |
by (rule div_poly_eq, rule pdivmod_rel_mult, (rule pdivmod_rel)+)
|
huffman@29660
|
1104 |
|
huffman@29660
|
1105 |
lemma poly_mod_mult_right:
|
huffman@29660
|
1106 |
fixes x y z :: "'a::field poly"
|
huffman@29660
|
1107 |
shows "x mod (y * z) = y * (x div y mod z) + x mod y"
|
huffman@29660
|
1108 |
by (rule mod_poly_eq, rule pdivmod_rel_mult, (rule pdivmod_rel)+)
|
huffman@29660
|
1109 |
|
huffman@29449
|
1110 |
lemma mod_pCons:
|
huffman@29449
|
1111 |
fixes a and x
|
huffman@29449
|
1112 |
assumes y: "y \<noteq> 0"
|
huffman@29449
|
1113 |
defines b: "b \<equiv> coeff (pCons a (x mod y)) (degree y) / coeff y (degree y)"
|
huffman@29449
|
1114 |
shows "(pCons a x) mod y = (pCons a (x mod y) - smult b y)"
|
huffman@29449
|
1115 |
unfolding b
|
huffman@29449
|
1116 |
apply (rule mod_poly_eq)
|
huffman@29537
|
1117 |
apply (rule pdivmod_rel_pCons [OF pdivmod_rel y refl])
|
huffman@29449
|
1118 |
done
|
huffman@29449
|
1119 |
|
huffman@29449
|
1120 |
|
huffman@31663
|
1121 |
subsection {* GCD of polynomials *}
|
huffman@31663
|
1122 |
|
huffman@31663
|
1123 |
function
|
huffman@31663
|
1124 |
poly_gcd :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where
|
huffman@31663
|
1125 |
"poly_gcd x 0 = smult (inverse (coeff x (degree x))) x"
|
huffman@31663
|
1126 |
| "y \<noteq> 0 \<Longrightarrow> poly_gcd x y = poly_gcd y (x mod y)"
|
huffman@31663
|
1127 |
by auto
|
huffman@31663
|
1128 |
|
huffman@31663
|
1129 |
termination poly_gcd
|
huffman@31663
|
1130 |
by (relation "measure (\<lambda>(x, y). if y = 0 then 0 else Suc (degree y))")
|
huffman@31663
|
1131 |
(auto dest: degree_mod_less)
|
huffman@31663
|
1132 |
|
haftmann@37765
|
1133 |
declare poly_gcd.simps [simp del]
|
huffman@31663
|
1134 |
|
huffman@31663
|
1135 |
lemma poly_gcd_dvd1 [iff]: "poly_gcd x y dvd x"
|
huffman@31663
|
1136 |
and poly_gcd_dvd2 [iff]: "poly_gcd x y dvd y"
|
huffman@31663
|
1137 |
apply (induct x y rule: poly_gcd.induct)
|
huffman@31663
|
1138 |
apply (simp_all add: poly_gcd.simps)
|
nipkow@45761
|
1139 |
apply (fastforce simp add: smult_dvd_iff dest: inverse_zero_imp_zero)
|
huffman@31663
|
1140 |
apply (blast dest: dvd_mod_imp_dvd)
|
huffman@31663
|
1141 |
done
|
huffman@31663
|
1142 |
|
huffman@31663
|
1143 |
lemma poly_gcd_greatest: "k dvd x \<Longrightarrow> k dvd y \<Longrightarrow> k dvd poly_gcd x y"
|
huffman@31663
|
1144 |
by (induct x y rule: poly_gcd.induct)
|
huffman@31663
|
1145 |
(simp_all add: poly_gcd.simps dvd_mod dvd_smult)
|
huffman@31663
|
1146 |
|
huffman@31663
|
1147 |
lemma dvd_poly_gcd_iff [iff]:
|
huffman@31663
|
1148 |
"k dvd poly_gcd x y \<longleftrightarrow> k dvd x \<and> k dvd y"
|
huffman@31663
|
1149 |
by (blast intro!: poly_gcd_greatest intro: dvd_trans)
|
huffman@31663
|
1150 |
|
huffman@31663
|
1151 |
lemma poly_gcd_monic:
|
huffman@31663
|
1152 |
"coeff (poly_gcd x y) (degree (poly_gcd x y)) =
|
huffman@31663
|
1153 |
(if x = 0 \<and> y = 0 then 0 else 1)"
|
huffman@31663
|
1154 |
by (induct x y rule: poly_gcd.induct)
|
huffman@31663
|
1155 |
(simp_all add: poly_gcd.simps nonzero_imp_inverse_nonzero)
|
huffman@31663
|
1156 |
|
huffman@31663
|
1157 |
lemma poly_gcd_zero_iff [simp]:
|
huffman@31663
|
1158 |
"poly_gcd x y = 0 \<longleftrightarrow> x = 0 \<and> y = 0"
|
huffman@31663
|
1159 |
by (simp only: dvd_0_left_iff [symmetric] dvd_poly_gcd_iff)
|
huffman@31663
|
1160 |
|
huffman@31663
|
1161 |
lemma poly_gcd_0_0 [simp]: "poly_gcd 0 0 = 0"
|
huffman@31663
|
1162 |
by simp
|
huffman@31663
|
1163 |
|
huffman@31663
|
1164 |
lemma poly_dvd_antisym:
|
huffman@31663
|
1165 |
fixes p q :: "'a::idom poly"
|
huffman@31663
|
1166 |
assumes coeff: "coeff p (degree p) = coeff q (degree q)"
|
huffman@31663
|
1167 |
assumes dvd1: "p dvd q" and dvd2: "q dvd p" shows "p = q"
|
huffman@31663
|
1168 |
proof (cases "p = 0")
|
huffman@31663
|
1169 |
case True with coeff show "p = q" by simp
|
huffman@31663
|
1170 |
next
|
huffman@31663
|
1171 |
case False with coeff have "q \<noteq> 0" by auto
|
huffman@31663
|
1172 |
have degree: "degree p = degree q"
|
huffman@31663
|
1173 |
using `p dvd q` `q dvd p` `p \<noteq> 0` `q \<noteq> 0`
|
huffman@31663
|
1174 |
by (intro order_antisym dvd_imp_degree_le)
|
huffman@31663
|
1175 |
|
huffman@31663
|
1176 |
from `p dvd q` obtain a where a: "q = p * a" ..
|
huffman@31663
|
1177 |
with `q \<noteq> 0` have "a \<noteq> 0" by auto
|
huffman@31663
|
1178 |
with degree a `p \<noteq> 0` have "degree a = 0"
|
huffman@31663
|
1179 |
by (simp add: degree_mult_eq)
|
huffman@31663
|
1180 |
with coeff a show "p = q"
|
huffman@31663
|
1181 |
by (cases a, auto split: if_splits)
|
huffman@31663
|
1182 |
qed
|
huffman@31663
|
1183 |
|
huffman@31663
|
1184 |
lemma poly_gcd_unique:
|
huffman@31663
|
1185 |
assumes dvd1: "d dvd x" and dvd2: "d dvd y"
|
huffman@31663
|
1186 |
and greatest: "\<And>k. k dvd x \<Longrightarrow> k dvd y \<Longrightarrow> k dvd d"
|
huffman@31663
|
1187 |
and monic: "coeff d (degree d) = (if x = 0 \<and> y = 0 then 0 else 1)"
|
huffman@31663
|
1188 |
shows "poly_gcd x y = d"
|
huffman@31663
|
1189 |
proof -
|
huffman@31663
|
1190 |
have "coeff (poly_gcd x y) (degree (poly_gcd x y)) = coeff d (degree d)"
|
huffman@31663
|
1191 |
by (simp_all add: poly_gcd_monic monic)
|
huffman@31663
|
1192 |
moreover have "poly_gcd x y dvd d"
|
huffman@31663
|
1193 |
using poly_gcd_dvd1 poly_gcd_dvd2 by (rule greatest)
|
huffman@31663
|
1194 |
moreover have "d dvd poly_gcd x y"
|
huffman@31663
|
1195 |
using dvd1 dvd2 by (rule poly_gcd_greatest)
|
huffman@31663
|
1196 |
ultimately show ?thesis
|
huffman@31663
|
1197 |
by (rule poly_dvd_antisym)
|
huffman@31663
|
1198 |
qed
|
huffman@31663
|
1199 |
|
haftmann@37770
|
1200 |
interpretation poly_gcd: abel_semigroup poly_gcd
|
haftmann@34960
|
1201 |
proof
|
haftmann@34960
|
1202 |
fix x y z :: "'a poly"
|
haftmann@34960
|
1203 |
show "poly_gcd (poly_gcd x y) z = poly_gcd x (poly_gcd y z)"
|
haftmann@34960
|
1204 |
by (rule poly_gcd_unique) (auto intro: dvd_trans simp add: poly_gcd_monic)
|
haftmann@34960
|
1205 |
show "poly_gcd x y = poly_gcd y x"
|
haftmann@34960
|
1206 |
by (rule poly_gcd_unique) (simp_all add: poly_gcd_monic)
|
haftmann@34960
|
1207 |
qed
|
huffman@31663
|
1208 |
|
haftmann@34960
|
1209 |
lemmas poly_gcd_assoc = poly_gcd.assoc
|
haftmann@34960
|
1210 |
lemmas poly_gcd_commute = poly_gcd.commute
|
haftmann@34960
|
1211 |
lemmas poly_gcd_left_commute = poly_gcd.left_commute
|
huffman@31663
|
1212 |
|
huffman@31663
|
1213 |
lemmas poly_gcd_ac = poly_gcd_assoc poly_gcd_commute poly_gcd_left_commute
|
huffman@31663
|
1214 |
|
huffman@31663
|
1215 |
lemma poly_gcd_1_left [simp]: "poly_gcd 1 y = 1"
|
huffman@31663
|
1216 |
by (rule poly_gcd_unique) simp_all
|
huffman@31663
|
1217 |
|
huffman@31663
|
1218 |
lemma poly_gcd_1_right [simp]: "poly_gcd x 1 = 1"
|
huffman@31663
|
1219 |
by (rule poly_gcd_unique) simp_all
|
huffman@31663
|
1220 |
|
huffman@31663
|
1221 |
lemma poly_gcd_minus_left [simp]: "poly_gcd (- x) y = poly_gcd x y"
|
huffman@31663
|
1222 |
by (rule poly_gcd_unique) (simp_all add: poly_gcd_monic)
|
huffman@31663
|
1223 |
|
huffman@31663
|
1224 |
lemma poly_gcd_minus_right [simp]: "poly_gcd x (- y) = poly_gcd x y"
|
huffman@31663
|
1225 |
by (rule poly_gcd_unique) (simp_all add: poly_gcd_monic)
|
huffman@31663
|
1226 |
|
huffman@31663
|
1227 |
|
huffman@29449
|
1228 |
subsection {* Evaluation of polynomials *}
|
huffman@29449
|
1229 |
|
huffman@29449
|
1230 |
definition
|
huffman@29454
|
1231 |
poly :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a" where
|
huffman@29454
|
1232 |
"poly = poly_rec (\<lambda>x. 0) (\<lambda>a p f x. a + x * f x)"
|
huffman@29449
|
1233 |
|
huffman@29449
|
1234 |
lemma poly_0 [simp]: "poly 0 x = 0"
|
huffman@29454
|
1235 |
unfolding poly_def by (simp add: poly_rec_0)
|
huffman@29449
|
1236 |
|
huffman@29449
|
1237 |
lemma poly_pCons [simp]: "poly (pCons a p) x = a + x * poly p x"
|
huffman@29454
|
1238 |
unfolding poly_def by (simp add: poly_rec_pCons)
|
huffman@29449
|
1239 |
|
huffman@29449
|
1240 |
lemma poly_1 [simp]: "poly 1 x = 1"
|
huffman@29449
|
1241 |
unfolding one_poly_def by simp
|
huffman@29449
|
1242 |
|
huffman@29454
|
1243 |
lemma poly_monom:
|
haftmann@31021
|
1244 |
fixes a x :: "'a::{comm_semiring_1}"
|
huffman@29454
|
1245 |
shows "poly (monom a n) x = a * x ^ n"
|
huffman@29449
|
1246 |
by (induct n, simp add: monom_0, simp add: monom_Suc power_Suc mult_ac)
|
huffman@29449
|
1247 |
|
huffman@29449
|
1248 |
lemma poly_add [simp]: "poly (p + q) x = poly p x + poly q x"
|
huffman@29449
|
1249 |
apply (induct p arbitrary: q, simp)
|
nipkow@29667
|
1250 |
apply (case_tac q, simp, simp add: algebra_simps)
|
huffman@29449
|
1251 |
done
|
huffman@29449
|
1252 |
|
huffman@29449
|
1253 |
lemma poly_minus [simp]:
|
huffman@29454
|
1254 |
fixes x :: "'a::comm_ring"
|
huffman@29449
|
1255 |
shows "poly (- p) x = - poly p x"
|
huffman@29449
|
1256 |
by (induct p, simp_all)
|
huffman@29449
|
1257 |
|
huffman@29449
|
1258 |
lemma poly_diff [simp]:
|
huffman@29454
|
1259 |
fixes x :: "'a::comm_ring"
|
huffman@29449
|
1260 |
shows "poly (p - q) x = poly p x - poly q x"
|
huffman@29449
|
1261 |
by (simp add: diff_minus)
|
huffman@29449
|
1262 |
|
huffman@29449
|
1263 |
lemma poly_setsum: "poly (\<Sum>k\<in>A. p k) x = (\<Sum>k\<in>A. poly (p k) x)"
|
huffman@29449
|
1264 |
by (cases "finite A", induct set: finite, simp_all)
|
huffman@29449
|
1265 |
|
huffman@29449
|
1266 |
lemma poly_smult [simp]: "poly (smult a p) x = a * poly p x"
|
nipkow@29667
|
1267 |
by (induct p, simp, simp add: algebra_simps)
|
huffman@29449
|
1268 |
|
huffman@29449
|
1269 |
lemma poly_mult [simp]: "poly (p * q) x = poly p x * poly q x"
|
nipkow@29667
|
1270 |
by (induct p, simp_all, simp add: algebra_simps)
|
huffman@29449
|
1271 |
|
huffman@29460
|
1272 |
lemma poly_power [simp]:
|
haftmann@31021
|
1273 |
fixes p :: "'a::{comm_semiring_1} poly"
|
huffman@29460
|
1274 |
shows "poly (p ^ n) x = poly p x ^ n"
|
huffman@29460
|
1275 |
by (induct n, simp, simp add: power_Suc)
|
huffman@29460
|
1276 |
|
huffman@29456
|
1277 |
|
huffman@29456
|
1278 |
subsection {* Synthetic division *}
|
huffman@29456
|
1279 |
|
huffman@29917
|
1280 |
text {*
|
huffman@29917
|
1281 |
Synthetic division is simply division by the
|
huffman@29917
|
1282 |
linear polynomial @{term "x - c"}.
|
huffman@29917
|
1283 |
*}
|
huffman@29917
|
1284 |
|
huffman@29456
|
1285 |
definition
|
huffman@29456
|
1286 |
synthetic_divmod :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly \<times> 'a"
|
haftmann@37765
|
1287 |
where
|
huffman@29456
|
1288 |
"synthetic_divmod p c =
|
huffman@29456
|
1289 |
poly_rec (0, 0) (\<lambda>a p (q, r). (pCons r q, a + c * r)) p"
|
huffman@29456
|
1290 |
|
huffman@29456
|
1291 |
definition
|
huffman@29456
|
1292 |
synthetic_div :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly"
|
huffman@29456
|
1293 |
where
|
huffman@29456
|
1294 |
"synthetic_div p c = fst (synthetic_divmod p c)"
|
huffman@29456
|
1295 |
|
huffman@29456
|
1296 |
lemma synthetic_divmod_0 [simp]:
|
huffman@29456
|
1297 |
"synthetic_divmod 0 c = (0, 0)"
|
huffman@29456
|
1298 |
unfolding synthetic_divmod_def
|
huffman@29456
|
1299 |
by (simp add: poly_rec_0)
|
huffman@29456
|
1300 |
|
huffman@29456
|
1301 |
lemma synthetic_divmod_pCons [simp]:
|
huffman@29456
|
1302 |
"synthetic_divmod (pCons a p) c =
|
huffman@29456
|
1303 |
(\<lambda>(q, r). (pCons r q, a + c * r)) (synthetic_divmod p c)"
|
huffman@29456
|
1304 |
unfolding synthetic_divmod_def
|
huffman@29456
|
1305 |
by (simp add: poly_rec_pCons)
|
huffman@29456
|
1306 |
|
huffman@29456
|
1307 |
lemma snd_synthetic_divmod: "snd (synthetic_divmod p c) = poly p c"
|
huffman@29456
|
1308 |
by (induct p, simp, simp add: split_def)
|
huffman@29456
|
1309 |
|
huffman@29456
|
1310 |
lemma synthetic_div_0 [simp]: "synthetic_div 0 c = 0"
|
huffman@29456
|
1311 |
unfolding synthetic_div_def by simp
|
huffman@29456
|
1312 |
|
huffman@29456
|
1313 |
lemma synthetic_div_pCons [simp]:
|
huffman@29456
|
1314 |
"synthetic_div (pCons a p) c = pCons (poly p c) (synthetic_div p c)"
|
huffman@29456
|
1315 |
unfolding synthetic_div_def
|
huffman@29456
|
1316 |
by (simp add: split_def snd_synthetic_divmod)
|
huffman@29456
|
1317 |
|
huffman@29458
|
1318 |
lemma synthetic_div_eq_0_iff:
|
huffman@29458
|
1319 |
"synthetic_div p c = 0 \<longleftrightarrow> degree p = 0"
|
huffman@29458
|
1320 |
by (induct p, simp, case_tac p, simp)
|
huffman@29458
|
1321 |
|
huffman@29458
|
1322 |
lemma degree_synthetic_div:
|
huffman@29458
|
1323 |
"degree (synthetic_div p c) = degree p - 1"
|
huffman@29458
|
1324 |
by (induct p, simp, simp add: synthetic_div_eq_0_iff)
|
huffman@29458
|
1325 |
|
huffman@29457
|
1326 |
lemma synthetic_div_correct:
|
huffman@29456
|
1327 |
"p + smult c (synthetic_div p c) = pCons (poly p c) (synthetic_div p c)"
|
huffman@29456
|
1328 |
by (induct p) simp_all
|
huffman@29456
|
1329 |
|
huffman@29457
|
1330 |
lemma synthetic_div_unique_lemma: "smult c p = pCons a p \<Longrightarrow> p = 0"
|
huffman@29457
|
1331 |
by (induct p arbitrary: a) simp_all
|
huffman@29457
|
1332 |
|
huffman@29457
|
1333 |
lemma synthetic_div_unique:
|
huffman@29457
|
1334 |
"p + smult c q = pCons r q \<Longrightarrow> r = poly p c \<and> q = synthetic_div p c"
|
huffman@29457
|
1335 |
apply (induct p arbitrary: q r)
|
huffman@29457
|
1336 |
apply (simp, frule synthetic_div_unique_lemma, simp)
|
huffman@29457
|
1337 |
apply (case_tac q, force)
|
huffman@29457
|
1338 |
done
|
huffman@29457
|
1339 |
|
huffman@29457
|
1340 |
lemma synthetic_div_correct':
|
huffman@29457
|
1341 |
fixes c :: "'a::comm_ring_1"
|
huffman@29457
|
1342 |
shows "[:-c, 1:] * synthetic_div p c + [:poly p c:] = p"
|
huffman@29457
|
1343 |
using synthetic_div_correct [of p c]
|
nipkow@29667
|
1344 |
by (simp add: algebra_simps)
|
huffman@29457
|
1345 |
|
huffman@29458
|
1346 |
lemma poly_eq_0_iff_dvd:
|
huffman@29458
|
1347 |
fixes c :: "'a::idom"
|
huffman@29458
|
1348 |
shows "poly p c = 0 \<longleftrightarrow> [:-c, 1:] dvd p"
|
huffman@29458
|
1349 |
proof
|
huffman@29458
|
1350 |
assume "poly p c = 0"
|
huffman@29458
|
1351 |
with synthetic_div_correct' [of c p]
|
huffman@29458
|
1352 |
have "p = [:-c, 1:] * synthetic_div p c" by simp
|
huffman@29458
|
1353 |
then show "[:-c, 1:] dvd p" ..
|
huffman@29458
|
1354 |
next
|
huffman@29458
|
1355 |
assume "[:-c, 1:] dvd p"
|
huffman@29458
|
1356 |
then obtain k where "p = [:-c, 1:] * k" by (rule dvdE)
|
huffman@29458
|
1357 |
then show "poly p c = 0" by simp
|
huffman@29458
|
1358 |
qed
|
huffman@29458
|
1359 |
|
huffman@29458
|
1360 |
lemma dvd_iff_poly_eq_0:
|
huffman@29458
|
1361 |
fixes c :: "'a::idom"
|
huffman@29458
|
1362 |
shows "[:c, 1:] dvd p \<longleftrightarrow> poly p (-c) = 0"
|
huffman@29458
|
1363 |
by (simp add: poly_eq_0_iff_dvd)
|
huffman@29458
|
1364 |
|
huffman@29460
|
1365 |
lemma poly_roots_finite:
|
huffman@29460
|
1366 |
fixes p :: "'a::idom poly"
|
huffman@29460
|
1367 |
shows "p \<noteq> 0 \<Longrightarrow> finite {x. poly p x = 0}"
|
huffman@29460
|
1368 |
proof (induct n \<equiv> "degree p" arbitrary: p)
|
huffman@29460
|
1369 |
case (0 p)
|
huffman@29460
|
1370 |
then obtain a where "a \<noteq> 0" and "p = [:a:]"
|
huffman@29460
|
1371 |
by (cases p, simp split: if_splits)
|
huffman@29460
|
1372 |
then show "finite {x. poly p x = 0}" by simp
|
huffman@29460
|
1373 |
next
|
huffman@29460
|
1374 |
case (Suc n p)
|
huffman@29460
|
1375 |
show "finite {x. poly p x = 0}"
|
huffman@29460
|
1376 |
proof (cases "\<exists>x. poly p x = 0")
|
huffman@29460
|
1377 |
case False
|
huffman@29460
|
1378 |
then show "finite {x. poly p x = 0}" by simp
|
huffman@29460
|
1379 |
next
|
huffman@29460
|
1380 |
case True
|
huffman@29460
|
1381 |
then obtain a where "poly p a = 0" ..
|
huffman@29460
|
1382 |
then have "[:-a, 1:] dvd p" by (simp only: poly_eq_0_iff_dvd)
|
huffman@29460
|
1383 |
then obtain k where k: "p = [:-a, 1:] * k" ..
|
huffman@29460
|
1384 |
with `p \<noteq> 0` have "k \<noteq> 0" by auto
|
huffman@29460
|
1385 |
with k have "degree p = Suc (degree k)"
|
huffman@29460
|
1386 |
by (simp add: degree_mult_eq del: mult_pCons_left)
|
huffman@29460
|
1387 |
with `Suc n = degree p` have "n = degree k" by simp
|
berghofe@34915
|
1388 |
then have "finite {x. poly k x = 0}" using `k \<noteq> 0` by (rule Suc.hyps)
|
huffman@29460
|
1389 |
then have "finite (insert a {x. poly k x = 0})" by simp
|
huffman@29460
|
1390 |
then show "finite {x. poly p x = 0}"
|
huffman@29460
|
1391 |
by (simp add: k uminus_add_conv_diff Collect_disj_eq
|
huffman@29460
|
1392 |
del: mult_pCons_left)
|
huffman@29460
|
1393 |
qed
|
huffman@29460
|
1394 |
qed
|
huffman@29460
|
1395 |
|
huffman@29916
|
1396 |
lemma poly_zero:
|
huffman@29916
|
1397 |
fixes p :: "'a::{idom,ring_char_0} poly"
|
huffman@29916
|
1398 |
shows "poly p = poly 0 \<longleftrightarrow> p = 0"
|
huffman@29916
|
1399 |
apply (cases "p = 0", simp_all)
|
huffman@29916
|
1400 |
apply (drule poly_roots_finite)
|
huffman@29916
|
1401 |
apply (auto simp add: infinite_UNIV_char_0)
|
huffman@29916
|
1402 |
done
|
huffman@29916
|
1403 |
|
huffman@29916
|
1404 |
lemma poly_eq_iff:
|
huffman@29916
|
1405 |
fixes p q :: "'a::{idom,ring_char_0} poly"
|
huffman@29916
|
1406 |
shows "poly p = poly q \<longleftrightarrow> p = q"
|
huffman@29916
|
1407 |
using poly_zero [of "p - q"]
|
nipkow@39535
|
1408 |
by (simp add: fun_eq_iff)
|
huffman@29916
|
1409 |
|
huffman@29478
|
1410 |
|
huffman@29917
|
1411 |
subsection {* Composition of polynomials *}
|
huffman@29917
|
1412 |
|
huffman@29917
|
1413 |
definition
|
huffman@29917
|
1414 |
pcompose :: "'a::comm_semiring_0 poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
|
huffman@29917
|
1415 |
where
|
huffman@29917
|
1416 |
"pcompose p q = poly_rec 0 (\<lambda>a _ c. [:a:] + q * c) p"
|
huffman@29917
|
1417 |
|
huffman@29917
|
1418 |
lemma pcompose_0 [simp]: "pcompose 0 q = 0"
|
huffman@29917
|
1419 |
unfolding pcompose_def by (simp add: poly_rec_0)
|
huffman@29917
|
1420 |
|
huffman@29917
|
1421 |
lemma pcompose_pCons:
|
huffman@29917
|
1422 |
"pcompose (pCons a p) q = [:a:] + q * pcompose p q"
|
huffman@29917
|
1423 |
unfolding pcompose_def by (simp add: poly_rec_pCons)
|
huffman@29917
|
1424 |
|
huffman@29917
|
1425 |
lemma poly_pcompose: "poly (pcompose p q) x = poly p (poly q x)"
|
huffman@29917
|
1426 |
by (induct p) (simp_all add: pcompose_pCons)
|
huffman@29917
|
1427 |
|
huffman@29917
|
1428 |
lemma degree_pcompose_le:
|
huffman@29917
|
1429 |
"degree (pcompose p q) \<le> degree p * degree q"
|
huffman@29917
|
1430 |
apply (induct p, simp)
|
huffman@29917
|
1431 |
apply (simp add: pcompose_pCons, clarify)
|
huffman@29917
|
1432 |
apply (rule degree_add_le, simp)
|
huffman@29917
|
1433 |
apply (rule order_trans [OF degree_mult_le], simp)
|
huffman@29917
|
1434 |
done
|
huffman@29917
|
1435 |
|
huffman@29917
|
1436 |
|
huffman@29914
|
1437 |
subsection {* Order of polynomial roots *}
|
huffman@29914
|
1438 |
|
huffman@29914
|
1439 |
definition
|
huffman@29916
|
1440 |
order :: "'a::idom \<Rightarrow> 'a poly \<Rightarrow> nat"
|
huffman@29914
|
1441 |
where
|
huffman@29914
|
1442 |
"order a p = (LEAST n. \<not> [:-a, 1:] ^ Suc n dvd p)"
|
huffman@29914
|
1443 |
|
huffman@29914
|
1444 |
lemma coeff_linear_power:
|
huffman@29916
|
1445 |
fixes a :: "'a::comm_semiring_1"
|
huffman@29914
|
1446 |
shows "coeff ([:a, 1:] ^ n) n = 1"
|
huffman@29914
|
1447 |
apply (induct n, simp_all)
|
huffman@29914
|
1448 |
apply (subst coeff_eq_0)
|
huffman@29914
|
1449 |
apply (auto intro: le_less_trans degree_power_le)
|
huffman@29914
|
1450 |
done
|
huffman@29914
|
1451 |
|
huffman@29914
|
1452 |
lemma degree_linear_power:
|
huffman@29916
|
1453 |
fixes a :: "'a::comm_semiring_1"
|
huffman@29914
|
1454 |
shows "degree ([:a, 1:] ^ n) = n"
|
huffman@29914
|
1455 |
apply (rule order_antisym)
|
huffman@29914
|
1456 |
apply (rule ord_le_eq_trans [OF degree_power_le], simp)
|
huffman@29914
|
1457 |
apply (rule le_degree, simp add: coeff_linear_power)
|
huffman@29914
|
1458 |
done
|
huffman@29914
|
1459 |
|
huffman@29914
|
1460 |
lemma order_1: "[:-a, 1:] ^ order a p dvd p"
|
huffman@29914
|
1461 |
apply (cases "p = 0", simp)
|
huffman@29914
|
1462 |
apply (cases "order a p", simp)
|
huffman@29914
|
1463 |
apply (subgoal_tac "nat < (LEAST n. \<not> [:-a, 1:] ^ Suc n dvd p)")
|
huffman@29914
|
1464 |
apply (drule not_less_Least, simp)
|
huffman@29914
|
1465 |
apply (fold order_def, simp)
|
huffman@29914
|
1466 |
done
|
huffman@29914
|
1467 |
|
huffman@29914
|
1468 |
lemma order_2: "p \<noteq> 0 \<Longrightarrow> \<not> [:-a, 1:] ^ Suc (order a p) dvd p"
|
huffman@29914
|
1469 |
unfolding order_def
|
huffman@29914
|
1470 |
apply (rule LeastI_ex)
|
huffman@29914
|
1471 |
apply (rule_tac x="degree p" in exI)
|
huffman@29914
|
1472 |
apply (rule notI)
|
huffman@29914
|
1473 |
apply (drule (1) dvd_imp_degree_le)
|
huffman@29914
|
1474 |
apply (simp only: degree_linear_power)
|
huffman@29914
|
1475 |
done
|
huffman@29914
|
1476 |
|
huffman@29914
|
1477 |
lemma order:
|
huffman@29914
|
1478 |
"p \<noteq> 0 \<Longrightarrow> [:-a, 1:] ^ order a p dvd p \<and> \<not> [:-a, 1:] ^ Suc (order a p) dvd p"
|
huffman@29914
|
1479 |
by (rule conjI [OF order_1 order_2])
|
huffman@29914
|
1480 |
|
huffman@29914
|
1481 |
lemma order_degree:
|
huffman@29914
|
1482 |
assumes p: "p \<noteq> 0"
|
huffman@29914
|
1483 |
shows "order a p \<le> degree p"
|
huffman@29914
|
1484 |
proof -
|
huffman@29914
|
1485 |
have "order a p = degree ([:-a, 1:] ^ order a p)"
|
huffman@29914
|
1486 |
by (simp only: degree_linear_power)
|
huffman@29914
|
1487 |
also have "\<dots> \<le> degree p"
|
huffman@29914
|
1488 |
using order_1 p by (rule dvd_imp_degree_le)
|
huffman@29914
|
1489 |
finally show ?thesis .
|
huffman@29914
|
1490 |
qed
|
huffman@29914
|
1491 |
|
huffman@29914
|
1492 |
lemma order_root: "poly p a = 0 \<longleftrightarrow> p = 0 \<or> order a p \<noteq> 0"
|
huffman@29914
|
1493 |
apply (cases "p = 0", simp_all)
|
huffman@29914
|
1494 |
apply (rule iffI)
|
huffman@29914
|
1495 |
apply (rule ccontr, simp)
|
huffman@29914
|
1496 |
apply (frule order_2 [where a=a], simp)
|
huffman@29914
|
1497 |
apply (simp add: poly_eq_0_iff_dvd)
|
huffman@29914
|
1498 |
apply (simp add: poly_eq_0_iff_dvd)
|
huffman@29914
|
1499 |
apply (simp only: order_def)
|
huffman@29914
|
1500 |
apply (drule not_less_Least, simp)
|
huffman@29914
|
1501 |
done
|
huffman@29914
|
1502 |
|
huffman@29914
|
1503 |
|
huffman@29478
|
1504 |
subsection {* Configuration of the code generator *}
|
huffman@29478
|
1505 |
|
huffman@29478
|
1506 |
code_datatype "0::'a::zero poly" pCons
|
huffman@29478
|
1507 |
|
bulwahn@46799
|
1508 |
quickcheck_generator poly constructors: "0::'a::zero poly", pCons
|
bulwahn@46799
|
1509 |
|
haftmann@39086
|
1510 |
instantiation poly :: ("{zero, equal}") equal
|
huffman@29478
|
1511 |
begin
|
huffman@29478
|
1512 |
|
haftmann@37765
|
1513 |
definition
|
haftmann@39086
|
1514 |
"HOL.equal (p::'a poly) q \<longleftrightarrow> p = q"
|
huffman@29478
|
1515 |
|
haftmann@39086
|
1516 |
instance proof
|
haftmann@39086
|
1517 |
qed (rule equal_poly_def)
|
huffman@29478
|
1518 |
|
huffman@29449
|
1519 |
end
|
huffman@29478
|
1520 |
|
huffman@29478
|
1521 |
lemma eq_poly_code [code]:
|
haftmann@39086
|
1522 |
"HOL.equal (0::_ poly) (0::_ poly) \<longleftrightarrow> True"
|
haftmann@39086
|
1523 |
"HOL.equal (0::_ poly) (pCons b q) \<longleftrightarrow> HOL.equal 0 b \<and> HOL.equal 0 q"
|
haftmann@39086
|
1524 |
"HOL.equal (pCons a p) (0::_ poly) \<longleftrightarrow> HOL.equal a 0 \<and> HOL.equal p 0"
|
haftmann@39086
|
1525 |
"HOL.equal (pCons a p) (pCons b q) \<longleftrightarrow> HOL.equal a b \<and> HOL.equal p q"
|
haftmann@39086
|
1526 |
by (simp_all add: equal)
|
haftmann@39086
|
1527 |
|
haftmann@39086
|
1528 |
lemma [code nbe]:
|
haftmann@39086
|
1529 |
"HOL.equal (p :: _ poly) p \<longleftrightarrow> True"
|
haftmann@39086
|
1530 |
by (fact equal_refl)
|
huffman@29478
|
1531 |
|
huffman@29478
|
1532 |
lemmas coeff_code [code] =
|
huffman@29478
|
1533 |
coeff_0 coeff_pCons_0 coeff_pCons_Suc
|
huffman@29478
|
1534 |
|
huffman@29478
|
1535 |
lemmas degree_code [code] =
|
huffman@29478
|
1536 |
degree_0 degree_pCons_eq_if
|
huffman@29478
|
1537 |
|
huffman@29478
|
1538 |
lemmas monom_poly_code [code] =
|
huffman@29478
|
1539 |
monom_0 monom_Suc
|
huffman@29478
|
1540 |
|
huffman@29478
|
1541 |
lemma add_poly_code [code]:
|
huffman@29478
|
1542 |
"0 + q = (q :: _ poly)"
|
huffman@29478
|
1543 |
"p + 0 = (p :: _ poly)"
|
huffman@29478
|
1544 |
"pCons a p + pCons b q = pCons (a + b) (p + q)"
|
huffman@29478
|
1545 |
by simp_all
|
huffman@29478
|
1546 |
|
huffman@29478
|
1547 |
lemma minus_poly_code [code]:
|
huffman@29478
|
1548 |
"- 0 = (0 :: _ poly)"
|
huffman@29478
|
1549 |
"- pCons a p = pCons (- a) (- p)"
|
huffman@29478
|
1550 |
by simp_all
|
huffman@29478
|
1551 |
|
huffman@29478
|
1552 |
lemma diff_poly_code [code]:
|
huffman@29478
|
1553 |
"0 - q = (- q :: _ poly)"
|
huffman@29478
|
1554 |
"p - 0 = (p :: _ poly)"
|
huffman@29478
|
1555 |
"pCons a p - pCons b q = pCons (a - b) (p - q)"
|
huffman@29478
|
1556 |
by simp_all
|
huffman@29478
|
1557 |
|
huffman@29478
|
1558 |
lemmas smult_poly_code [code] =
|
huffman@29478
|
1559 |
smult_0_right smult_pCons
|
huffman@29478
|
1560 |
|
huffman@29478
|
1561 |
lemma mult_poly_code [code]:
|
huffman@29478
|
1562 |
"0 * q = (0 :: _ poly)"
|
huffman@29478
|
1563 |
"pCons a p * q = smult a q + pCons 0 (p * q)"
|
huffman@29478
|
1564 |
by simp_all
|
huffman@29478
|
1565 |
|
huffman@29478
|
1566 |
lemmas poly_code [code] =
|
huffman@29478
|
1567 |
poly_0 poly_pCons
|
huffman@29478
|
1568 |
|
huffman@29478
|
1569 |
lemmas synthetic_divmod_code [code] =
|
huffman@29478
|
1570 |
synthetic_divmod_0 synthetic_divmod_pCons
|
huffman@29478
|
1571 |
|
huffman@29478
|
1572 |
text {* code generator setup for div and mod *}
|
huffman@29478
|
1573 |
|
huffman@29478
|
1574 |
definition
|
huffman@29537
|
1575 |
pdivmod :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<times> 'a poly"
|
huffman@29478
|
1576 |
where
|
haftmann@37765
|
1577 |
"pdivmod x y = (x div y, x mod y)"
|
huffman@29478
|
1578 |
|
huffman@29537
|
1579 |
lemma div_poly_code [code]: "x div y = fst (pdivmod x y)"
|
huffman@29537
|
1580 |
unfolding pdivmod_def by simp
|
huffman@29478
|
1581 |
|
huffman@29537
|
1582 |
lemma mod_poly_code [code]: "x mod y = snd (pdivmod x y)"
|
huffman@29537
|
1583 |
unfolding pdivmod_def by simp
|
huffman@29478
|
1584 |
|
huffman@29537
|
1585 |
lemma pdivmod_0 [code]: "pdivmod 0 y = (0, 0)"
|
huffman@29537
|
1586 |
unfolding pdivmod_def by simp
|
huffman@29478
|
1587 |
|
huffman@29537
|
1588 |
lemma pdivmod_pCons [code]:
|
huffman@29537
|
1589 |
"pdivmod (pCons a x) y =
|
huffman@29478
|
1590 |
(if y = 0 then (0, pCons a x) else
|
huffman@29537
|
1591 |
(let (q, r) = pdivmod x y;
|
huffman@29478
|
1592 |
b = coeff (pCons a r) (degree y) / coeff y (degree y)
|
huffman@29478
|
1593 |
in (pCons b q, pCons a r - smult b y)))"
|
huffman@29537
|
1594 |
apply (simp add: pdivmod_def Let_def, safe)
|
huffman@29478
|
1595 |
apply (rule div_poly_eq)
|
huffman@29537
|
1596 |
apply (erule pdivmod_rel_pCons [OF pdivmod_rel _ refl])
|
huffman@29478
|
1597 |
apply (rule mod_poly_eq)
|
huffman@29537
|
1598 |
apply (erule pdivmod_rel_pCons [OF pdivmod_rel _ refl])
|
huffman@29478
|
1599 |
done
|
huffman@29478
|
1600 |
|
huffman@31663
|
1601 |
lemma poly_gcd_code [code]:
|
huffman@31663
|
1602 |
"poly_gcd x y =
|
huffman@31663
|
1603 |
(if y = 0 then smult (inverse (coeff x (degree x))) x
|
huffman@31663
|
1604 |
else poly_gcd y (x mod y))"
|
huffman@31663
|
1605 |
by (simp add: poly_gcd.simps)
|
huffman@31663
|
1606 |
|
huffman@29478
|
1607 |
end
|