|
68578
|
1 |
(* ************************************************************************** *)
|
|
|
2 |
(* Title: Polynomials.thy *)
|
|
|
3 |
(* Author: Paulo EmÃlio de Vilhena *)
|
|
|
4 |
(* ************************************************************************** *)
|
|
|
5 |
|
|
|
6 |
theory Polynomials
|
|
|
7 |
imports Ring Ring_Divisibility Subrings
|
|
|
8 |
|
|
|
9 |
begin
|
|
|
10 |
|
|
|
11 |
section \<open>Polynomials\<close>
|
|
|
12 |
|
|
|
13 |
subsection \<open>Definitions\<close>
|
|
|
14 |
|
|
|
15 |
abbreviation lead_coeff :: "'a list \<Rightarrow> 'a"
|
|
|
16 |
where "lead_coeff \<equiv> hd"
|
|
|
17 |
|
|
|
18 |
definition degree :: "'a list \<Rightarrow> nat"
|
|
|
19 |
where "degree p = length p - 1"
|
|
|
20 |
|
|
|
21 |
definition polynomial :: "_ \<Rightarrow> 'a list \<Rightarrow> bool"
|
|
|
22 |
where "polynomial R p \<longleftrightarrow> p = [] \<or> (set p \<subseteq> carrier R \<and> lead_coeff p \<noteq> \<zero>\<^bsub>R\<^esub>)"
|
|
|
23 |
|
|
|
24 |
definition (in ring) monon :: "'a \<Rightarrow> nat \<Rightarrow> 'a list"
|
|
|
25 |
where "monon a n = a # (replicate n \<zero>\<^bsub>R\<^esub>)"
|
|
|
26 |
|
|
|
27 |
fun (in ring) eval :: "'a list \<Rightarrow> 'a \<Rightarrow> 'a"
|
|
|
28 |
where
|
|
|
29 |
"eval [] = (\<lambda>_. \<zero>)"
|
|
|
30 |
| "eval p = (\<lambda>x. ((lead_coeff p) \<otimes> (x [^] (degree p))) \<oplus> (eval (tl p) x))"
|
|
|
31 |
|
|
|
32 |
fun (in ring) coeff :: "'a list \<Rightarrow> nat \<Rightarrow> 'a"
|
|
|
33 |
where
|
|
|
34 |
"coeff [] = (\<lambda>_. \<zero>)"
|
|
|
35 |
| "coeff p = (\<lambda>i. if i = degree p then lead_coeff p else (coeff (tl p)) i)"
|
|
|
36 |
|
|
|
37 |
fun (in ring) normalize :: "'a list \<Rightarrow> 'a list"
|
|
|
38 |
where
|
|
|
39 |
"normalize [] = []"
|
|
|
40 |
| "normalize p = (if lead_coeff p \<noteq> \<zero> then p else normalize (tl p))"
|
|
|
41 |
|
|
|
42 |
fun (in ring) poly_add :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
|
|
|
43 |
where "poly_add p1 p2 =
|
|
|
44 |
(if length p1 \<ge> length p2
|
|
|
45 |
then normalize (map2 (\<oplus>) p1 ((replicate (length p1 - length p2) \<zero>) @ p2))
|
|
|
46 |
else poly_add p2 p1)"
|
|
|
47 |
|
|
|
48 |
fun (in ring) poly_mult :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
|
|
|
49 |
where
|
|
|
50 |
"poly_mult [] p2 = []"
|
|
|
51 |
| "poly_mult p1 p2 =
|
|
|
52 |
poly_add ((map (\<lambda>a. lead_coeff p1 \<otimes> a) p2) @ (replicate (degree p1) \<zero>)) (poly_mult (tl p1) p2)"
|
|
|
53 |
|
|
|
54 |
fun (in ring) dense_repr :: "'a list \<Rightarrow> ('a \<times> nat) list"
|
|
|
55 |
where
|
|
|
56 |
"dense_repr [] = []"
|
|
|
57 |
| "dense_repr p = (if lead_coeff p \<noteq> \<zero>
|
|
|
58 |
then (lead_coeff p, degree p) # (dense_repr (tl p))
|
|
|
59 |
else (dense_repr (tl p)))"
|
|
|
60 |
|
|
|
61 |
fun (in ring) of_dense :: "('a \<times> nat) list \<Rightarrow> 'a list"
|
|
|
62 |
where "of_dense dl = foldr (\<lambda>(a, n) l. poly_add (monon a n) l) dl []"
|
|
|
63 |
|
|
|
64 |
|
|
|
65 |
subsection \<open>Basic Properties\<close>
|
|
|
66 |
|
|
|
67 |
context ring
|
|
|
68 |
begin
|
|
|
69 |
|
|
|
70 |
lemma polynomialI [intro]: "\<lbrakk> set p \<subseteq> carrier R; lead_coeff p \<noteq> \<zero> \<rbrakk> \<Longrightarrow> polynomial R p"
|
|
|
71 |
unfolding polynomial_def by auto
|
|
|
72 |
|
|
|
73 |
lemma polynomial_in_carrier [intro]: "polynomial R p \<Longrightarrow> set p \<subseteq> carrier R"
|
|
|
74 |
unfolding polynomial_def by auto
|
|
|
75 |
|
|
|
76 |
lemma lead_coeff_not_zero [intro]: "polynomial R (a # p) \<Longrightarrow> a \<in> carrier R - { \<zero> }"
|
|
|
77 |
unfolding polynomial_def by simp
|
|
|
78 |
|
|
|
79 |
lemma zero_is_polynomial [intro]: "polynomial R []"
|
|
|
80 |
unfolding polynomial_def by simp
|
|
|
81 |
|
|
|
82 |
lemma const_is_polynomial [intro]: "a \<in> carrier R - { \<zero> } \<Longrightarrow> polynomial R [ a ]"
|
|
|
83 |
unfolding polynomial_def by auto
|
|
|
84 |
|
|
|
85 |
lemma monon_is_polynomial [intro]: "a \<in> carrier R - { \<zero> } \<Longrightarrow> polynomial R (monon a n)"
|
|
|
86 |
unfolding polynomial_def monon_def by auto
|
|
|
87 |
|
|
|
88 |
lemma monon_in_carrier [intro]: "a \<in> carrier R \<Longrightarrow> set (monon a n) \<subseteq> carrier R"
|
|
|
89 |
unfolding monon_def by auto
|
|
|
90 |
|
|
|
91 |
lemma normalize_gives_polynomial: "set p \<subseteq> carrier R \<Longrightarrow> polynomial R (normalize p)"
|
|
|
92 |
by (induction p) (auto simp add: polynomial_def)
|
|
|
93 |
|
|
|
94 |
lemma normalize_in_carrier: "set p \<subseteq> carrier R \<Longrightarrow> set (normalize p) \<subseteq> carrier R"
|
|
|
95 |
using normalize_gives_polynomial polynomial_in_carrier by simp
|
|
|
96 |
|
|
|
97 |
lemma normalize_idem: "polynomial R p \<Longrightarrow> normalize p = p"
|
|
|
98 |
unfolding polynomial_def by (cases p) (auto)
|
|
|
99 |
|
|
|
100 |
lemma normalize_length_le: "length (normalize p) \<le> length p"
|
|
|
101 |
by (induction p) (auto)
|
|
|
102 |
|
|
|
103 |
lemma eval_in_carrier: "\<lbrakk> set p \<subseteq> carrier R; x \<in> carrier R \<rbrakk> \<Longrightarrow> (eval p) x \<in> carrier R"
|
|
|
104 |
by (induction p) (auto)
|
|
|
105 |
|
|
|
106 |
lemma eval_poly_in_carrier: "\<lbrakk> polynomial R p; x \<in> carrier R \<rbrakk> \<Longrightarrow> (eval p) x \<in> carrier R"
|
|
|
107 |
using eval_in_carrier unfolding polynomial_def by auto
|
|
|
108 |
|
|
|
109 |
lemma coeff_in_carrier [simp]: "set p \<subseteq> carrier R \<Longrightarrow> (coeff p) i \<in> carrier R"
|
|
|
110 |
by (induction p) (auto)
|
|
|
111 |
|
|
|
112 |
lemma poly_coeff_in_carrier [simp]: "polynomial R p \<Longrightarrow> coeff p i \<in> carrier R"
|
|
|
113 |
using coeff_in_carrier unfolding polynomial_def by auto
|
|
|
114 |
|
|
|
115 |
lemma lead_coeff_simp [simp]: "p \<noteq> [] \<Longrightarrow> (coeff p) (degree p) = lead_coeff p"
|
|
|
116 |
by (metis coeff.simps(2) list.exhaust_sel)
|
|
|
117 |
|
|
|
118 |
lemma coeff_list: "map (coeff p) (rev [0..< length p]) = p"
|
|
|
119 |
proof (induction p)
|
|
|
120 |
case Nil thus ?case by simp
|
|
|
121 |
next
|
|
|
122 |
case (Cons a p)
|
|
|
123 |
have "map (coeff (a # p)) (rev [0..<length (a # p)]) =
|
|
|
124 |
map (coeff (a # p)) ((length p) # (rev [0..<length p]))"
|
|
|
125 |
by simp
|
|
|
126 |
also have " ... = a # (map (coeff p) (rev [0..<length p]))"
|
|
|
127 |
using degree_def[of "a # p"] by auto
|
|
|
128 |
also have " ... = a # p"
|
|
|
129 |
using Cons by simp
|
|
|
130 |
finally show ?case .
|
|
|
131 |
qed
|
|
|
132 |
|
|
|
133 |
lemma coeff_nth: "i < length p \<Longrightarrow> (coeff p) i = p ! (length p - 1 - i)"
|
|
|
134 |
proof -
|
|
|
135 |
assume i_lt: "i < length p"
|
|
|
136 |
hence "(coeff p) i = (map (coeff p) [0..< length p]) ! i"
|
|
|
137 |
by simp
|
|
|
138 |
also have " ... = (rev (map (coeff p) (rev [0..< length p]))) ! i"
|
|
|
139 |
by (simp add: rev_map)
|
|
|
140 |
also have " ... = (map (coeff p) (rev [0..< length p])) ! (length p - 1 - i)"
|
|
|
141 |
using coeff_list i_lt rev_nth by auto
|
|
|
142 |
also have " ... = p ! (length p - 1 - i)"
|
|
|
143 |
using coeff_list[of p] by simp
|
|
|
144 |
finally show "(coeff p) i = p ! (length p - 1 - i)" .
|
|
|
145 |
qed
|
|
|
146 |
|
|
|
147 |
lemma coeff_iff_length_cond:
|
|
|
148 |
assumes "length p1 = length p2"
|
|
|
149 |
shows "p1 = p2 \<longleftrightarrow> coeff p1 = coeff p2"
|
|
|
150 |
proof
|
|
|
151 |
show "p1 = p2 \<Longrightarrow> coeff p1 = coeff p2"
|
|
|
152 |
by simp
|
|
|
153 |
next
|
|
|
154 |
assume A: "coeff p1 = coeff p2"
|
|
|
155 |
have "p1 = map (coeff p1) (rev [0..< length p1])"
|
|
|
156 |
using coeff_list[of p1] by simp
|
|
|
157 |
also have " ... = map (coeff p2) (rev [0..< length p2])"
|
|
|
158 |
using A assms by simp
|
|
|
159 |
also have " ... = p2"
|
|
|
160 |
using coeff_list[of p2] by simp
|
|
|
161 |
finally show "p1 = p2" .
|
|
|
162 |
qed
|
|
|
163 |
|
|
|
164 |
lemma coeff_img_restrict: "(coeff p) ` {..< length p} = set p"
|
|
|
165 |
using coeff_list[of p] by (metis atLeast_upt image_set set_rev)
|
|
|
166 |
|
|
|
167 |
lemma coeff_length: "\<And>i. i \<ge> length p \<Longrightarrow> (coeff p) i = \<zero>"
|
|
|
168 |
by (induction p) (auto simp add: degree_def)
|
|
|
169 |
|
|
|
170 |
lemma coeff_degree: "\<And>i. i > degree p \<Longrightarrow> (coeff p) i = \<zero>"
|
|
|
171 |
using coeff_length by (simp add: degree_def)
|
|
|
172 |
|
|
|
173 |
lemma replicate_zero_coeff [simp]: "coeff (replicate n \<zero>) = (\<lambda>_. \<zero>)"
|
|
|
174 |
by (induction n) (auto)
|
|
|
175 |
|
|
|
176 |
lemma scalar_coeff: "a \<in> carrier R \<Longrightarrow> coeff (map (\<lambda>b. a \<otimes> b) p) = (\<lambda>i. a \<otimes> (coeff p) i)"
|
|
|
177 |
by (induction p) (auto simp add:degree_def)
|
|
|
178 |
|
|
|
179 |
lemma monon_coeff: "coeff (monon a n) = (\<lambda>i. if i = n then a else \<zero>)"
|
|
|
180 |
unfolding monon_def by (induction n) (auto simp add: degree_def)
|
|
|
181 |
|
|
|
182 |
lemma coeff_img:
|
|
|
183 |
"(coeff p) ` {..< length p} = set p"
|
|
|
184 |
"(coeff p) ` { length p ..} = { \<zero> }"
|
|
|
185 |
"(coeff p) ` UNIV = (set p) \<union> { \<zero> }"
|
|
|
186 |
using coeff_img_restrict
|
|
|
187 |
proof (simp)
|
|
|
188 |
show coeff_img_up: "(coeff p) ` { length p ..} = { \<zero> }"
|
|
|
189 |
using coeff_length[of p] unfolding degree_def by force
|
|
|
190 |
from coeff_img_up and coeff_img_restrict[of p]
|
|
|
191 |
show "(coeff p) ` UNIV = (set p) \<union> { \<zero> }"
|
|
|
192 |
by force
|
|
|
193 |
qed
|
|
|
194 |
|
|
|
195 |
lemma degree_def':
|
|
|
196 |
assumes "polynomial R p"
|
|
|
197 |
shows "degree p = (LEAST n. \<forall>i. i > n \<longrightarrow> (coeff p) i = \<zero>)"
|
|
|
198 |
proof (cases p)
|
|
|
199 |
case Nil thus ?thesis
|
|
|
200 |
unfolding degree_def by auto
|
|
|
201 |
next
|
|
|
202 |
define P where "P = (\<lambda>n. \<forall>i. i > n \<longrightarrow> (coeff p) i = \<zero>)"
|
|
|
203 |
|
|
|
204 |
case (Cons a ps)
|
|
|
205 |
hence "(coeff p) (degree p) \<noteq> \<zero>"
|
|
|
206 |
using assms unfolding polynomial_def by auto
|
|
|
207 |
hence "\<And>n. n < degree p \<Longrightarrow> \<not> P n"
|
|
|
208 |
unfolding P_def by auto
|
|
|
209 |
moreover have "P (degree p)"
|
|
|
210 |
unfolding P_def using coeff_degree[of p] by simp
|
|
|
211 |
ultimately have "degree p = (LEAST n. P n)"
|
|
|
212 |
by (meson LeastI nat_neq_iff not_less_Least)
|
|
|
213 |
thus ?thesis unfolding P_def .
|
|
|
214 |
qed
|
|
|
215 |
|
|
|
216 |
lemma coeff_iff_polynomial_cond:
|
|
|
217 |
assumes "polynomial R p1" and "polynomial R p2"
|
|
|
218 |
shows "p1 = p2 \<longleftrightarrow> coeff p1 = coeff p2"
|
|
|
219 |
proof
|
|
|
220 |
show "p1 = p2 \<Longrightarrow> coeff p1 = coeff p2"
|
|
|
221 |
by simp
|
|
|
222 |
next
|
|
|
223 |
assume coeff_eq: "coeff p1 = coeff p2"
|
|
|
224 |
hence deg_eq: "degree p1 = degree p2"
|
|
|
225 |
using degree_def'[OF assms(1)] degree_def'[OF assms(2)] by auto
|
|
|
226 |
thus "p1 = p2"
|
|
|
227 |
proof (cases)
|
|
|
228 |
assume "p1 \<noteq> [] \<and> p2 \<noteq> []"
|
|
|
229 |
hence "length p1 = length p2"
|
|
|
230 |
using deg_eq unfolding degree_def
|
|
|
231 |
by (simp add: Nitpick.size_list_simp(2))
|
|
|
232 |
thus ?thesis
|
|
|
233 |
using coeff_iff_length_cond[of p1 p2] coeff_eq by simp
|
|
|
234 |
next
|
|
|
235 |
{ fix p1 p2 assume A: "p1 = []" "coeff p1 = coeff p2" "polynomial R p2"
|
|
|
236 |
have "p2 = []"
|
|
|
237 |
proof (rule ccontr)
|
|
|
238 |
assume "p2 \<noteq> []"
|
|
|
239 |
hence "(coeff p2) (degree p2) \<noteq> \<zero>"
|
|
|
240 |
using A(3) unfolding polynomial_def
|
|
|
241 |
by (metis coeff.simps(2) list.collapse)
|
|
|
242 |
moreover have "(coeff p1) ` UNIV = { \<zero> }"
|
|
|
243 |
using A(1) by auto
|
|
|
244 |
hence "(coeff p2) ` UNIV = { \<zero> }"
|
|
|
245 |
using A(2) by simp
|
|
|
246 |
ultimately show False
|
|
|
247 |
by blast
|
|
|
248 |
qed } note aux_lemma = this
|
|
|
249 |
assume "\<not> (p1 \<noteq> [] \<and> p2 \<noteq> [])"
|
|
|
250 |
hence "p1 = [] \<or> p2 = []" by simp
|
|
|
251 |
thus ?thesis
|
|
|
252 |
using assms coeff_eq aux_lemma[of p1 p2] aux_lemma[of p2 p1] by auto
|
|
|
253 |
qed
|
|
|
254 |
qed
|
|
|
255 |
|
|
|
256 |
lemma normalize_lead_coeff:
|
|
|
257 |
assumes "length (normalize p) < length p"
|
|
|
258 |
shows "lead_coeff p = \<zero>"
|
|
|
259 |
proof (cases p)
|
|
|
260 |
case Nil thus ?thesis
|
|
|
261 |
using assms by simp
|
|
|
262 |
next
|
|
|
263 |
case (Cons a ps) thus ?thesis
|
|
|
264 |
using assms by (cases "a = \<zero>") (auto)
|
|
|
265 |
qed
|
|
|
266 |
|
|
|
267 |
lemma normalize_length_lt:
|
|
|
268 |
assumes "lead_coeff p = \<zero>" and "length p > 0"
|
|
|
269 |
shows "length (normalize p) < length p"
|
|
|
270 |
proof (cases p)
|
|
|
271 |
case Nil thus ?thesis
|
|
|
272 |
using assms by simp
|
|
|
273 |
next
|
|
|
274 |
case (Cons a ps) thus ?thesis
|
|
|
275 |
using normalize_length_le[of ps] assms by simp
|
|
|
276 |
qed
|
|
|
277 |
|
|
|
278 |
lemma normalize_length_eq:
|
|
|
279 |
assumes "lead_coeff p \<noteq> \<zero>"
|
|
|
280 |
shows "length (normalize p) = length p"
|
|
|
281 |
using normalize_length_le[of p] assms nat_less_le normalize_lead_coeff by auto
|
|
|
282 |
|
|
|
283 |
lemma normalize_replicate_zero: "normalize ((replicate n \<zero>) @ p) = normalize p"
|
|
|
284 |
by (induction n) (auto)
|
|
|
285 |
|
|
|
286 |
lemma normalize_def':
|
|
|
287 |
shows "p = (replicate (length p - length (normalize p)) \<zero>) @
|
|
|
288 |
(drop (length p - length (normalize p)) p)" (is ?statement1)
|
|
|
289 |
and "normalize p = drop (length p - length (normalize p)) p" (is ?statement2)
|
|
|
290 |
proof -
|
|
|
291 |
show ?statement1
|
|
|
292 |
proof (induction p)
|
|
|
293 |
case Nil thus ?case by simp
|
|
|
294 |
next
|
|
|
295 |
case (Cons a p) thus ?case
|
|
|
296 |
proof (cases "a = \<zero>")
|
|
|
297 |
assume "a \<noteq> \<zero>" thus ?case
|
|
|
298 |
using Cons by simp
|
|
|
299 |
next
|
|
|
300 |
assume eq_zero: "a = \<zero>"
|
|
|
301 |
hence len_eq:
|
|
|
302 |
"Suc (length p - length (normalize p)) = length (a # p) - length (normalize (a # p))"
|
|
|
303 |
by (simp add: Suc_diff_le normalize_length_le)
|
|
|
304 |
have "a # p = \<zero> # (replicate (length p - length (normalize p)) \<zero> @
|
|
|
305 |
drop (length p - length (normalize p)) p)"
|
|
|
306 |
using eq_zero Cons by simp
|
|
|
307 |
also have " ... = (replicate (Suc (length p - length (normalize p))) \<zero> @
|
|
|
308 |
drop (Suc (length p - length (normalize p))) (a # p))"
|
|
|
309 |
by simp
|
|
|
310 |
also have " ... = (replicate (length (a # p) - length (normalize (a # p))) \<zero> @
|
|
|
311 |
drop (length (a # p) - length (normalize (a # p))) (a # p))"
|
|
|
312 |
using len_eq by simp
|
|
|
313 |
finally show ?case .
|
|
|
314 |
qed
|
|
|
315 |
qed
|
|
|
316 |
next
|
|
|
317 |
show ?statement2
|
|
|
318 |
proof -
|
|
|
319 |
have "\<exists>m. normalize p = drop m p"
|
|
|
320 |
proof (induction p)
|
|
|
321 |
case Nil thus ?case by simp
|
|
|
322 |
next
|
|
|
323 |
case (Cons a p) thus ?case
|
|
|
324 |
apply (cases "a = \<zero>")
|
|
|
325 |
apply (auto)
|
|
|
326 |
apply (metis drop_Suc_Cons)
|
|
|
327 |
apply (metis drop0)
|
|
|
328 |
done
|
|
|
329 |
qed
|
|
|
330 |
then obtain m where m: "normalize p = drop m p" by auto
|
|
|
331 |
hence "length (normalize p) = length p - m" by simp
|
|
|
332 |
thus ?thesis
|
|
|
333 |
using m by (metis rev_drop rev_rev_ident take_rev)
|
|
|
334 |
qed
|
|
|
335 |
qed
|
|
|
336 |
|
|
|
337 |
lemma normalize_coeff: "coeff p = coeff (normalize p)"
|
|
|
338 |
proof (induction p)
|
|
|
339 |
case Nil thus ?case by simp
|
|
|
340 |
next
|
|
|
341 |
case (Cons a p)
|
|
|
342 |
have "coeff (normalize p) (length p) = \<zero>"
|
|
|
343 |
using normalize_length_le[of p] coeff_degree[of "normalize p"] unfolding degree_def
|
|
|
344 |
by (metis One_nat_def coeff.simps(1) diff_less length_0_conv
|
|
|
345 |
less_imp_diff_less nat_neq_iff neq0_conv not_le zero_less_Suc)
|
|
|
346 |
then show ?case
|
|
|
347 |
using Cons by (cases "a = \<zero>") (auto simp add: degree_def)
|
|
|
348 |
qed
|
|
|
349 |
|
|
|
350 |
lemma append_coeff:
|
|
|
351 |
"coeff (p @ q) = (\<lambda>i. if i < length q then (coeff q) i else (coeff p) (i - length q))"
|
|
|
352 |
proof (induction p)
|
|
|
353 |
case Nil thus ?case
|
|
|
354 |
using coeff_length[of q] by auto
|
|
|
355 |
next
|
|
|
356 |
case (Cons a p)
|
|
|
357 |
have "coeff ((a # p) @ q) = (\<lambda>i. if i = length p + length q then a else (coeff (p @ q)) i)"
|
|
|
358 |
by (auto simp add: degree_def)
|
|
|
359 |
also have " ... = (\<lambda>i. if i = length p + length q then a
|
|
|
360 |
else if i < length q then (coeff q) i
|
|
|
361 |
else (coeff p) (i - length q))"
|
|
|
362 |
using Cons by auto
|
|
|
363 |
also have " ... = (\<lambda>i. if i < length q then (coeff q) i
|
|
|
364 |
else if i = length p + length q then a else (coeff p) (i - length q))"
|
|
|
365 |
by auto
|
|
|
366 |
also have " ... = (\<lambda>i. if i < length q then (coeff q) i
|
|
|
367 |
else if i - length q = length p then a else (coeff p) (i - length q))"
|
|
|
368 |
by fastforce
|
|
|
369 |
also have " ... = (\<lambda>i. if i < length q then (coeff q) i else (coeff (a # p)) (i - length q))"
|
|
|
370 |
by (auto simp add: degree_def)
|
|
|
371 |
finally show ?case .
|
|
|
372 |
qed
|
|
|
373 |
|
|
|
374 |
lemma prefix_replicate_zero_coeff: "coeff p = coeff ((replicate n \<zero>) @ p)"
|
|
|
375 |
using append_coeff[of "replicate n \<zero>" p] replicate_zero_coeff[of n] coeff_length[of p] by auto
|
|
|
376 |
|
|
|
377 |
end
|
|
|
378 |
|
|
|
379 |
|
|
|
380 |
subsection \<open>Poly_Add\<close>
|
|
|
381 |
|
|
|
382 |
context ring
|
|
|
383 |
begin
|
|
|
384 |
|
|
|
385 |
lemma poly_add_is_polynomial:
|
|
|
386 |
assumes "set p1 \<subseteq> carrier R" and "set p2 \<subseteq> carrier R"
|
|
|
387 |
shows "polynomial R (poly_add p1 p2)"
|
|
|
388 |
proof -
|
|
|
389 |
{ fix p1 p2 assume A: "set p1 \<subseteq> carrier R" "set p2 \<subseteq> carrier R" "length p1 \<ge> length p2"
|
|
|
390 |
hence "polynomial R (poly_add p1 p2)"
|
|
|
391 |
proof -
|
|
|
392 |
define p2' where "p2' = (replicate (length p1 - length p2) \<zero>) @ p2"
|
|
|
393 |
hence set_p2': "set p2' \<subseteq> carrier R"
|
|
|
394 |
using A(2) by auto
|
|
|
395 |
have "set (map (\<lambda>(a, b). a \<oplus> b) (zip p1 p2')) \<subseteq> carrier R"
|
|
|
396 |
proof
|
|
|
397 |
fix c assume "c \<in> set (map (\<lambda>(a, b). a \<oplus> b) (zip p1 p2'))"
|
|
|
398 |
then obtain t where "t \<in> set (zip p1 p2')" and c: "c = fst t \<oplus> snd t"
|
|
|
399 |
by auto
|
|
|
400 |
then obtain a b where "a \<in> set p1" "a = fst t"
|
|
|
401 |
and "b \<in> set p2'" "b = snd t"
|
|
|
402 |
by (metis set_zip_leftD set_zip_rightD surjective_pairing)
|
|
|
403 |
thus "c \<in> carrier R"
|
|
|
404 |
using A(1) set_p2' c by auto
|
|
|
405 |
qed
|
|
|
406 |
thus ?thesis
|
|
|
407 |
unfolding p2'_def using normalize_gives_polynomial A(3) by simp
|
|
|
408 |
qed }
|
|
|
409 |
thus ?thesis
|
|
|
410 |
using assms by simp
|
|
|
411 |
qed
|
|
|
412 |
|
|
|
413 |
lemma poly_add_in_carrier:
|
|
|
414 |
"\<lbrakk> set p1 \<subseteq> carrier R; set p2 \<subseteq> carrier R \<rbrakk> \<Longrightarrow> set (poly_add p1 p2) \<subseteq> carrier R"
|
|
|
415 |
using poly_add_is_polynomial polynomial_in_carrier by simp
|
|
|
416 |
|
|
|
417 |
lemma poly_add_closed: "\<lbrakk> polynomial R p1; polynomial R p2 \<rbrakk> \<Longrightarrow> polynomial R (poly_add p1 p2)"
|
|
|
418 |
using poly_add_is_polynomial polynomial_in_carrier by auto
|
|
|
419 |
|
|
|
420 |
lemma poly_add_length_le: "length (poly_add p1 p2) \<le> max (length p1) (length p2)"
|
|
|
421 |
proof -
|
|
|
422 |
{ fix p1 p2 :: "'a list" assume A: "length p1 \<ge> length p2"
|
|
|
423 |
hence "length (poly_add p1 p2) \<le> max (length p1) (length p2)"
|
|
|
424 |
proof -
|
|
|
425 |
let ?p2 = "(replicate (length p1 - length p2) \<zero>) @ p2"
|
|
|
426 |
have "length (map2 (\<oplus>) p1 ?p2) = length p1"
|
|
|
427 |
using A by auto
|
|
|
428 |
thus ?thesis
|
|
|
429 |
using normalize_length_le[of "map2 (\<oplus>) p1 ?p2"] A by auto
|
|
|
430 |
qed }
|
|
|
431 |
thus ?thesis
|
|
|
432 |
by (metis le_cases max.commute poly_add.simps)
|
|
|
433 |
qed
|
|
|
434 |
|
|
|
435 |
lemma poly_add_length_eq:
|
|
|
436 |
assumes "polynomial R p1" "polynomial R p2" and "length p1 \<noteq> length p2"
|
|
|
437 |
shows "length (poly_add p1 p2) = max (length p1) (length p2)"
|
|
|
438 |
proof -
|
|
|
439 |
{ fix p1 p2 assume A: "polynomial R p1" "polynomial R p2" "length p1 > length p2"
|
|
|
440 |
hence "length (poly_add p1 p2) = max (length p1) (length p2)"
|
|
|
441 |
proof -
|
|
|
442 |
let ?p2 = "(replicate (length p1 - length p2) \<zero>) @ p2"
|
|
|
443 |
have p1: "p1 \<noteq> []" and p2: "?p2 \<noteq> []"
|
|
|
444 |
using A(3) by auto
|
|
|
445 |
hence "lead_coeff (map2 (\<oplus>) p1 ?p2) = lead_coeff p1 \<oplus> lead_coeff ?p2"
|
|
|
446 |
by (smt case_prod_conv list.exhaust_sel list.map(2) list.sel(1) zip_Cons_Cons)
|
|
|
447 |
moreover have "lead_coeff p1 \<in> carrier R"
|
|
|
448 |
using p1 A(1) unfolding polynomial_def by auto
|
|
|
449 |
ultimately have "lead_coeff (map2 (\<oplus>) p1 ?p2) = lead_coeff p1"
|
|
|
450 |
using A(3) by auto
|
|
|
451 |
moreover have "lead_coeff p1 \<noteq> \<zero>"
|
|
|
452 |
using p1 A(1) unfolding polynomial_def by simp
|
|
|
453 |
ultimately have "length (normalize (map2 (\<oplus>) p1 ?p2)) = length p1"
|
|
|
454 |
using normalize_length_eq by auto
|
|
|
455 |
thus ?thesis
|
|
|
456 |
using A(3) by auto
|
|
|
457 |
qed }
|
|
|
458 |
thus ?thesis
|
|
|
459 |
using assms by auto
|
|
|
460 |
qed
|
|
|
461 |
|
|
|
462 |
lemma poly_add_degree: "degree (poly_add p1 p2) \<le> max (degree p1) (degree p2)"
|
|
|
463 |
unfolding degree_def using poly_add_length_le
|
|
|
464 |
by (meson diff_le_mono le_max_iff_disj)
|
|
|
465 |
|
|
|
466 |
lemma poly_add_degree_eq:
|
|
|
467 |
assumes "polynomial R p1" "polynomial R p2" and "degree p1 \<noteq> degree p2"
|
|
|
468 |
shows "degree (poly_add p1 p2) = max (degree p1) (degree p2)"
|
|
|
469 |
using poly_add_length_eq[of p1 p2] assms
|
|
|
470 |
by (smt degree_def diff_le_mono le_cases max.absorb1 max_def)
|
|
|
471 |
|
|
|
472 |
lemma poly_add_coeff_aux:
|
|
|
473 |
assumes "length p1 \<ge> length p2"
|
|
|
474 |
shows "coeff (poly_add p1 p2) = (\<lambda>i. ((coeff p1) i) \<oplus> ((coeff p2) i))"
|
|
|
475 |
proof
|
|
|
476 |
fix i
|
|
|
477 |
have "i < length p1 \<Longrightarrow> (coeff (poly_add p1 p2)) i = ((coeff p1) i) \<oplus> ((coeff p2) i)"
|
|
|
478 |
proof -
|
|
|
479 |
let ?p2 = "(replicate (length p1 - length p2) \<zero>) @ p2"
|
|
|
480 |
have len_eqs: "length p1 = length ?p2" "length (map2 (\<oplus>) p1 ?p2) = length p1"
|
|
|
481 |
using assms by auto
|
|
|
482 |
assume i_lt: "i < length p1"
|
|
|
483 |
have "(coeff (poly_add p1 p2)) i = (coeff (map2 (\<oplus>) p1 ?p2)) i"
|
|
|
484 |
using normalize_coeff[of "map2 (\<oplus>) p1 ?p2"] assms by auto
|
|
|
485 |
also have " ... = (map2 (\<oplus>) p1 ?p2) ! (length p1 - 1 - i)"
|
|
|
486 |
using coeff_nth[of i "map2 (\<oplus>) p1 ?p2"] len_eqs(2) i_lt by auto
|
|
|
487 |
also have " ... = (p1 ! (length p1 - 1 - i)) \<oplus> (?p2 ! (length ?p2 - 1 - i))"
|
|
|
488 |
using len_eqs i_lt by auto
|
|
|
489 |
also have " ... = ((coeff p1) i) \<oplus> ((coeff ?p2) i)"
|
|
|
490 |
using coeff_nth[of i p1] coeff_nth[of i ?p2] i_lt len_eqs(1) by auto
|
|
|
491 |
also have " ... = ((coeff p1) i) \<oplus> ((coeff p2) i)"
|
|
|
492 |
using prefix_replicate_zero_coeff by simp
|
|
|
493 |
finally show "(coeff (poly_add p1 p2)) i = ((coeff p1) i) \<oplus> ((coeff p2) i)" .
|
|
|
494 |
qed
|
|
|
495 |
moreover
|
|
|
496 |
have "i \<ge> length p1 \<Longrightarrow> (coeff (poly_add p1 p2)) i = ((coeff p1) i) \<oplus> ((coeff p2) i)"
|
|
|
497 |
using coeff_length[of "poly_add p1 p2"] coeff_length[of p1] coeff_length[of p2]
|
|
|
498 |
poly_add_length_le[of p1 p2] assms by auto
|
|
|
499 |
ultimately show "(coeff (poly_add p1 p2)) i = ((coeff p1) i) \<oplus> ((coeff p2) i)"
|
|
|
500 |
using not_le by blast
|
|
|
501 |
qed
|
|
|
502 |
|
|
|
503 |
lemma poly_add_coeff:
|
|
|
504 |
assumes "set p1 \<subseteq> carrier R" "set p2 \<subseteq> carrier R"
|
|
|
505 |
shows "coeff (poly_add p1 p2) = (\<lambda>i. ((coeff p1) i) \<oplus> ((coeff p2) i))"
|
|
|
506 |
proof -
|
|
|
507 |
have "length p1 \<ge> length p2 \<or> length p2 > length p1"
|
|
|
508 |
by auto
|
|
|
509 |
thus ?thesis
|
|
|
510 |
proof
|
|
|
511 |
assume "length p1 \<ge> length p2" thus ?thesis
|
|
|
512 |
using poly_add_coeff_aux by simp
|
|
|
513 |
next
|
|
|
514 |
assume "length p2 > length p1"
|
|
|
515 |
hence "coeff (poly_add p1 p2) = (\<lambda>i. ((coeff p2) i) \<oplus> ((coeff p1) i))"
|
|
|
516 |
using poly_add_coeff_aux by simp
|
|
|
517 |
thus ?thesis
|
|
|
518 |
using assms by (simp add: add.m_comm)
|
|
|
519 |
qed
|
|
|
520 |
qed
|
|
|
521 |
|
|
|
522 |
lemma poly_add_comm:
|
|
|
523 |
assumes "set p1 \<subseteq> carrier R" "set p2 \<subseteq> carrier R"
|
|
|
524 |
shows "poly_add p1 p2 = poly_add p2 p1"
|
|
|
525 |
proof -
|
|
|
526 |
have "coeff (poly_add p1 p2) = coeff (poly_add p2 p1)"
|
|
|
527 |
using poly_add_coeff[OF assms] poly_add_coeff[OF assms(2) assms(1)]
|
|
|
528 |
coeff_in_carrier[OF assms(1)] coeff_in_carrier[OF assms(2)] add.m_comm by auto
|
|
|
529 |
thus ?thesis
|
|
|
530 |
using coeff_iff_polynomial_cond poly_add_is_polynomial assms by auto
|
|
|
531 |
qed
|
|
|
532 |
|
|
|
533 |
lemma poly_add_monon:
|
|
|
534 |
assumes "set p \<subseteq> carrier R" and "a \<in> carrier R - { \<zero> }"
|
|
|
535 |
shows "poly_add (monon a (length p)) p = a # p"
|
|
|
536 |
unfolding monon_def using assms by (induction p) (auto)
|
|
|
537 |
|
|
|
538 |
lemma poly_add_normalize_aux:
|
|
|
539 |
assumes "set p1 \<subseteq> carrier R" "set p2 \<subseteq> carrier R"
|
|
|
540 |
shows "poly_add p1 p2 = poly_add (normalize p1) p2"
|
|
|
541 |
proof -
|
|
|
542 |
{ fix n p1 p2 assume "set p1 \<subseteq> carrier R" "set p2 \<subseteq> carrier R"
|
|
|
543 |
hence "poly_add p1 p2 = poly_add ((replicate n \<zero>) @ p1) p2"
|
|
|
544 |
proof (induction n)
|
|
|
545 |
case 0 thus ?case by simp
|
|
|
546 |
next
|
|
|
547 |
{ fix p1 p2 :: "'a list"
|
|
|
548 |
assume in_carrier: "set p1 \<subseteq> carrier R" "set p2 \<subseteq> carrier R"
|
|
|
549 |
have "poly_add p1 p2 = poly_add (\<zero> # p1) p2"
|
|
|
550 |
proof -
|
|
|
551 |
have "length p1 \<ge> length p2 \<Longrightarrow> ?thesis"
|
|
|
552 |
proof -
|
|
|
553 |
assume A: "length p1 \<ge> length p2"
|
|
|
554 |
let ?p2 = "\<lambda>n. (replicate n \<zero>) @ p2"
|
|
|
555 |
have "poly_add p1 p2 = normalize (map2 (\<oplus>) (\<zero> # p1) (\<zero> # ?p2 (length p1 - length p2)))"
|
|
|
556 |
using A by simp
|
|
|
557 |
also have " ... = normalize (map2 (\<oplus>) (\<zero> # p1) (?p2 (length (\<zero> # p1) - length p2)))"
|
|
|
558 |
by (simp add: A Suc_diff_le)
|
|
|
559 |
also have " ... = poly_add (\<zero> # p1) p2"
|
|
|
560 |
using A by simp
|
|
|
561 |
finally show ?thesis .
|
|
|
562 |
qed
|
|
|
563 |
|
|
|
564 |
moreover have "length p2 > length p1 \<Longrightarrow> ?thesis"
|
|
|
565 |
proof -
|
|
|
566 |
assume A: "length p2 > length p1"
|
|
|
567 |
let ?f = "\<lambda>n p. (replicate n \<zero>) @ p"
|
|
|
568 |
have "poly_add p1 p2 = poly_add p2 p1"
|
|
|
569 |
using A by simp
|
|
|
570 |
also have " ... = normalize (map2 (\<oplus>) p2 (?f (length p2 - length p1) p1))"
|
|
|
571 |
using A by simp
|
|
|
572 |
also have " ... = normalize (map2 (\<oplus>) p2 (?f (length p2 - Suc (length p1)) (\<zero> # p1)))"
|
|
|
573 |
by (metis A Suc_diff_Suc append_Cons replicate_Suc replicate_app_Cons_same)
|
|
|
574 |
also have " ... = poly_add p2 (\<zero> # p1)"
|
|
|
575 |
using A by simp
|
|
|
576 |
also have " ... = poly_add (\<zero> # p1) p2"
|
|
|
577 |
using poly_add_comm[of p2 "\<zero> # p1"] in_carrier by auto
|
|
|
578 |
finally show ?thesis .
|
|
|
579 |
qed
|
|
|
580 |
|
|
|
581 |
ultimately show ?thesis by auto
|
|
|
582 |
qed } note aux_lemma = this
|
|
|
583 |
|
|
|
584 |
case (Suc n)
|
|
|
585 |
hence in_carrier: "set (replicate n \<zero> @ p1) \<subseteq> carrier R"
|
|
|
586 |
by auto
|
|
|
587 |
have "poly_add p1 p2 = poly_add (replicate n \<zero> @ p1) p2"
|
|
|
588 |
using Suc by simp
|
|
|
589 |
also have " ... = poly_add (replicate (Suc n) \<zero> @ p1) p2"
|
|
|
590 |
using aux_lemma[OF in_carrier Suc(3)] by simp
|
|
|
591 |
finally show ?case .
|
|
|
592 |
qed } note aux_lemma = this
|
|
|
593 |
|
|
|
594 |
have "poly_add p1 p2 =
|
|
|
595 |
poly_add ((replicate (length p1 - length (normalize p1)) \<zero>) @ normalize p1) p2"
|
|
|
596 |
using normalize_def'[of p1] by simp
|
|
|
597 |
also have " ... = poly_add (normalize p1) p2"
|
|
|
598 |
using aux_lemma[OF
|
|
|
599 |
polynomial_in_carrier[OF normalize_gives_polynomial[OF assms(1)]] assms(2)] by simp
|
|
|
600 |
finally show ?thesis .
|
|
|
601 |
qed
|
|
|
602 |
|
|
|
603 |
lemma poly_add_normalize:
|
|
|
604 |
assumes "set p1 \<subseteq> carrier R" "set p2 \<subseteq> carrier R"
|
|
|
605 |
shows "poly_add p1 p2 = poly_add (normalize p1) p2"
|
|
|
606 |
and "poly_add p1 p2 = poly_add p1 (normalize p2)"
|
|
|
607 |
and "poly_add p1 p2 = poly_add (normalize p1) (normalize p2)"
|
|
|
608 |
proof -
|
|
|
609 |
show "poly_add p1 p2 = poly_add p1 (normalize p2)"
|
|
|
610 |
using poly_add_normalize_aux[OF assms(2) assms(1)] poly_add_comm
|
|
|
611 |
polynomial_in_carrier normalize_gives_polynomial assms by auto
|
|
|
612 |
next
|
|
|
613 |
show "poly_add p1 p2 = poly_add (normalize p1) p2"
|
|
|
614 |
using poly_add_normalize_aux[OF assms] by simp
|
|
|
615 |
also have " ... = poly_add p2 (normalize p1)"
|
|
|
616 |
using poly_add_comm polynomial_in_carrier normalize_gives_polynomial assms by auto
|
|
|
617 |
also have " ... = poly_add (normalize p2) (normalize p1)"
|
|
|
618 |
using poly_add_normalize_aux polynomial_in_carrier normalize_gives_polynomial assms by auto
|
|
|
619 |
also have " ... = poly_add (normalize p1) (normalize p2)"
|
|
|
620 |
using poly_add_comm polynomial_in_carrier normalize_gives_polynomial assms by auto
|
|
|
621 |
finally show "poly_add p1 p2 = poly_add (normalize p1) (normalize p2)" .
|
|
|
622 |
qed
|
|
|
623 |
|
|
|
624 |
lemma poly_add_zero':
|
|
|
625 |
assumes "set p \<subseteq> carrier R"
|
|
|
626 |
shows "poly_add p [] = normalize p" and "poly_add [] p = normalize p"
|
|
|
627 |
proof -
|
|
|
628 |
show "poly_add p [] = normalize p" using assms
|
|
|
629 |
proof (induction p)
|
|
|
630 |
case Nil thus ?case by simp
|
|
|
631 |
next
|
|
|
632 |
{ fix p assume A: "set p \<subseteq> carrier R" "lead_coeff p \<noteq> \<zero>"
|
|
|
633 |
hence "polynomial R p"
|
|
|
634 |
unfolding polynomial_def by simp
|
|
|
635 |
moreover have "coeff (poly_add p []) = coeff p"
|
|
|
636 |
using poly_add_coeff[of p "[]"] A(1) by simp
|
|
|
637 |
ultimately have "poly_add p [] = p"
|
|
|
638 |
using coeff_iff_polynomial_cond[OF
|
|
|
639 |
poly_add_is_polynomial[OF A(1), of "[]"], of p] by simp }
|
|
|
640 |
note aux_lemma = this
|
|
|
641 |
case (Cons a p) thus ?case
|
|
|
642 |
using aux_lemma[of "a # p"] by auto
|
|
|
643 |
qed
|
|
|
644 |
thus "poly_add [] p = normalize p"
|
|
|
645 |
using poly_add_comm[OF assms, of "[]"] by simp
|
|
|
646 |
qed
|
|
|
647 |
|
|
|
648 |
lemma poly_add_zero:
|
|
|
649 |
assumes "polynomial R p"
|
|
|
650 |
shows "poly_add p [] = p" and "poly_add [] p = p"
|
|
|
651 |
using poly_add_zero' normalize_idem polynomial_in_carrier assms by auto
|
|
|
652 |
|
|
|
653 |
lemma poly_add_replicate_zero':
|
|
|
654 |
assumes "set p \<subseteq> carrier R"
|
|
|
655 |
shows "poly_add p (replicate n \<zero>) = normalize p" and "poly_add (replicate n \<zero>) p = normalize p"
|
|
|
656 |
proof -
|
|
|
657 |
have "poly_add p (replicate n \<zero>) = poly_add p []"
|
|
|
658 |
using poly_add_normalize(2)[OF assms, of "replicate n \<zero>"]
|
|
|
659 |
normalize_replicate_zero[of n "[]"] by force
|
|
|
660 |
also have " ... = normalize p"
|
|
|
661 |
using poly_add_zero'[OF assms] by simp
|
|
|
662 |
finally show "poly_add p (replicate n \<zero>) = normalize p" .
|
|
|
663 |
thus "poly_add (replicate n \<zero>) p = normalize p"
|
|
|
664 |
using poly_add_comm[OF assms, of "replicate n \<zero>"] by force
|
|
|
665 |
qed
|
|
|
666 |
|
|
|
667 |
lemma poly_add_replicate_zero:
|
|
|
668 |
assumes "polynomial R p"
|
|
|
669 |
shows "poly_add p (replicate n \<zero>) = p" and "poly_add (replicate n \<zero>) p = p"
|
|
|
670 |
using poly_add_replicate_zero' normalize_idem polynomial_in_carrier assms by auto
|
|
|
671 |
|
|
|
672 |
|
|
|
673 |
subsection \<open>Dense Representation\<close>
|
|
|
674 |
|
|
|
675 |
lemma dense_repr_replicate_zero: "dense_repr ((replicate n \<zero>) @ p) = dense_repr p"
|
|
|
676 |
by (induction n) (auto)
|
|
|
677 |
|
|
|
678 |
lemma polynomial_dense_repr:
|
|
|
679 |
assumes "polynomial R p" and "p \<noteq> []"
|
|
|
680 |
shows "dense_repr p = (lead_coeff p, degree p) # dense_repr (normalize (tl p))"
|
|
|
681 |
proof -
|
|
|
682 |
let ?len = length and ?norm = normalize
|
|
|
683 |
obtain a p' where p: "p = a # p'"
|
|
|
684 |
using assms(2) list.exhaust_sel by blast
|
|
|
685 |
hence a: "a \<in> carrier R - { \<zero> }" and p': "set p' \<subseteq> carrier R"
|
|
|
686 |
using assms(1) unfolding p by (auto simp add: polynomial_def)
|
|
|
687 |
hence "dense_repr p = (lead_coeff p, degree p) # dense_repr p'"
|
|
|
688 |
unfolding p by simp
|
|
|
689 |
also have " ... =
|
|
|
690 |
(lead_coeff p, degree p) # dense_repr ((replicate (?len p' - ?len (?norm p')) \<zero>) @ ?norm p')"
|
|
|
691 |
using normalize_def' dense_repr_replicate_zero by simp
|
|
|
692 |
also have " ... = (lead_coeff p, degree p) # dense_repr (?norm p')"
|
|
|
693 |
using dense_repr_replicate_zero by simp
|
|
|
694 |
finally show ?thesis
|
|
|
695 |
unfolding p by simp
|
|
|
696 |
qed
|
|
|
697 |
|
|
|
698 |
lemma monon_decomp:
|
|
|
699 |
assumes "polynomial R p"
|
|
|
700 |
shows "p = of_dense (dense_repr p)"
|
|
|
701 |
using assms
|
|
|
702 |
proof (induct "length p" arbitrary: p rule: less_induct)
|
|
|
703 |
case less thus ?case
|
|
|
704 |
proof (cases p)
|
|
|
705 |
case Nil thus ?thesis by simp
|
|
|
706 |
next
|
|
|
707 |
case (Cons a l)
|
|
|
708 |
hence a: "a \<in> carrier R - { \<zero> }" and l: "set l \<subseteq> carrier R"
|
|
|
709 |
using less(2) by (auto simp add: polynomial_def)
|
|
|
710 |
hence "a # l = poly_add (monon a (degree (a # l))) l"
|
|
|
711 |
using poly_add_monon by (simp add: degree_def)
|
|
|
712 |
also have " ... = poly_add (monon a (degree (a # l))) (normalize l)"
|
|
|
713 |
using poly_add_normalize(2)[of "monon a (degree (a # l))", OF _ l] a
|
|
|
714 |
unfolding monon_def by force
|
|
|
715 |
also have " ... = poly_add (monon a (degree (a # l))) (of_dense (dense_repr (normalize l)))"
|
|
|
716 |
using less(1)[of "normalize l"] normalize_length_le normalize_gives_polynomial[OF l]
|
|
|
717 |
unfolding Cons by (simp add: le_imp_less_Suc)
|
|
|
718 |
also have " ... = of_dense ((a, degree (a # l)) # dense_repr (normalize l))"
|
|
|
719 |
by simp
|
|
|
720 |
also have " ... = of_dense (dense_repr (a # l))"
|
|
|
721 |
using polynomial_dense_repr[OF less(2)] unfolding Cons by simp
|
|
|
722 |
finally show ?thesis
|
|
|
723 |
unfolding Cons by simp
|
|
|
724 |
qed
|
|
|
725 |
qed
|
|
|
726 |
|
|
|
727 |
end
|
|
|
728 |
|
|
|
729 |
|
|
|
730 |
subsection \<open>Poly_Mult\<close>
|
|
|
731 |
|
|
|
732 |
context ring
|
|
|
733 |
begin
|
|
|
734 |
|
|
|
735 |
lemma poly_mult_is_polynomial:
|
|
|
736 |
assumes "set p1 \<subseteq> carrier R" and "set p2 \<subseteq> carrier R"
|
|
|
737 |
shows "polynomial R (poly_mult p1 p2)"
|
|
|
738 |
using assms
|
|
|
739 |
proof (induction p1)
|
|
|
740 |
case Nil thus ?case
|
|
|
741 |
by (simp add: polynomial_def)
|
|
|
742 |
next
|
|
|
743 |
case (Cons a p1)
|
|
|
744 |
let ?a_p2 = "(map (\<lambda>b. a \<otimes> b) p2) @ (replicate (degree (a # p1)) \<zero>)"
|
|
|
745 |
|
|
|
746 |
have "set (poly_mult p1 p2) \<subseteq> carrier R"
|
|
|
747 |
using Cons unfolding polynomial_def by auto
|
|
|
748 |
|
|
|
749 |
moreover have "set ?a_p2 \<subseteq> carrier R"
|
|
|
750 |
proof -
|
|
|
751 |
have "set (map (\<lambda>b. a \<otimes> b) p2) \<subseteq> carrier R"
|
|
|
752 |
proof
|
|
|
753 |
fix c assume "c \<in> set (map (\<lambda>b. a \<otimes> b) p2)"
|
|
|
754 |
then obtain b where "b \<in> set p2" "c = a \<otimes> b"
|
|
|
755 |
by auto
|
|
|
756 |
thus "c \<in> carrier R"
|
|
|
757 |
using Cons(2-3) by auto
|
|
|
758 |
qed
|
|
|
759 |
thus ?thesis
|
|
|
760 |
unfolding degree_def by auto
|
|
|
761 |
qed
|
|
|
762 |
|
|
|
763 |
ultimately have "polynomial R (poly_add ?a_p2 (poly_mult p1 p2))"
|
|
|
764 |
using poly_add_is_polynomial by blast
|
|
|
765 |
thus ?case by simp
|
|
|
766 |
qed
|
|
|
767 |
|
|
|
768 |
lemma poly_mult_in_carrier:
|
|
|
769 |
"\<lbrakk> set p1 \<subseteq> carrier R; set p2 \<subseteq> carrier R \<rbrakk> \<Longrightarrow> set (poly_mult p1 p2) \<subseteq> carrier R"
|
|
|
770 |
using poly_mult_is_polynomial polynomial_in_carrier by simp
|
|
|
771 |
|
|
|
772 |
lemma poly_mult_closed: "\<lbrakk> polynomial R p1; polynomial R p2 \<rbrakk> \<Longrightarrow> polynomial R (poly_mult p1 p2)"
|
|
|
773 |
using poly_mult_is_polynomial polynomial_in_carrier by simp
|
|
|
774 |
|
|
|
775 |
lemma poly_mult_coeff:
|
|
|
776 |
assumes "set p1 \<subseteq> carrier R" "set p2 \<subseteq> carrier R"
|
|
|
777 |
shows "coeff (poly_mult p1 p2) = (\<lambda>i. \<Oplus> k \<in> {..i}. (coeff p1) k \<otimes> (coeff p2) (i - k))"
|
|
|
778 |
using assms(1)
|
|
|
779 |
proof (induction p1)
|
|
|
780 |
case Nil thus ?case using assms(2) by auto
|
|
|
781 |
next
|
|
|
782 |
case (Cons a p1)
|
|
|
783 |
hence in_carrier:
|
|
|
784 |
"a \<in> carrier R" "\<And>i. (coeff p1) i \<in> carrier R" "\<And>i. (coeff p2) i \<in> carrier R"
|
|
|
785 |
using coeff_in_carrier assms(2) by auto
|
|
|
786 |
|
|
|
787 |
let ?a_p2 = "(map (\<lambda>b. a \<otimes> b) p2) @ (replicate (degree (a # p1)) \<zero>)"
|
|
|
788 |
have "coeff (replicate (degree (a # p1)) \<zero>) = (\<lambda>_. \<zero>)"
|
|
|
789 |
and "length (replicate (degree (a # p1)) \<zero>) = length p1"
|
|
|
790 |
using prefix_replicate_zero_coeff[of "[]" "length p1"] unfolding degree_def by auto
|
|
|
791 |
hence "coeff ?a_p2 = (\<lambda>i. if i < length p1 then \<zero> else (coeff (map (\<lambda>b. a \<otimes> b) p2)) (i - length p1))"
|
|
|
792 |
using append_coeff[of "map (\<lambda>b. a \<otimes> b) p2" "replicate (length p1) \<zero>"] unfolding degree_def by auto
|
|
|
793 |
also have " ... = (\<lambda>i. if i < length p1 then \<zero> else a \<otimes> ((coeff p2) (i - length p1)))"
|
|
|
794 |
proof -
|
|
|
795 |
have "\<And>i. i < length p2 \<Longrightarrow> (coeff (map (\<lambda>b. a \<otimes> b) p2)) i = a \<otimes> ((coeff p2) i)"
|
|
|
796 |
proof -
|
|
|
797 |
fix i assume i_lt: "i < length p2"
|
|
|
798 |
hence "(coeff (map (\<lambda>b. a \<otimes> b) p2)) i = (map (\<lambda>b. a \<otimes> b) p2) ! (length p2 - 1 - i)"
|
|
|
799 |
using coeff_nth[of i "map (\<lambda>b. a \<otimes> b) p2"] by auto
|
|
|
800 |
also have " ... = a \<otimes> (p2 ! (length p2 - 1 - i))"
|
|
|
801 |
using i_lt by auto
|
|
|
802 |
also have " ... = a \<otimes> ((coeff p2) i)"
|
|
|
803 |
using coeff_nth[OF i_lt] by simp
|
|
|
804 |
finally show "(coeff (map (\<lambda>b. a \<otimes> b) p2)) i = a \<otimes> ((coeff p2) i)" .
|
|
|
805 |
qed
|
|
|
806 |
moreover have "\<And>i. i \<ge> length p2 \<Longrightarrow> (coeff (map (\<lambda>b. a \<otimes> b) p2)) i = a \<otimes> ((coeff p2) i)"
|
|
|
807 |
using coeff_length[of p2] coeff_length[of "map (\<lambda>b. a \<otimes> b) p2"] in_carrier by auto
|
|
|
808 |
ultimately show ?thesis by (meson not_le)
|
|
|
809 |
qed
|
|
|
810 |
also have " ... = (\<lambda>i. \<Oplus> k \<in> {..i}. (if k = length p1 then a else \<zero>) \<otimes> (coeff p2) (i - k))"
|
|
|
811 |
(is "?f1 = (\<lambda>i. (\<Oplus> k \<in> {..i}. ?f2 k \<otimes> ?f3 (i - k)))")
|
|
|
812 |
proof
|
|
|
813 |
fix i
|
|
|
814 |
have "\<And>k. k \<in> {..i} \<Longrightarrow> ?f2 k \<otimes> ?f3 (i - k) = \<zero>" if "i < length p1"
|
|
|
815 |
using in_carrier that by auto
|
|
|
816 |
hence "(\<Oplus> k \<in> {..i}. ?f2 k \<otimes> ?f3 (i - k)) = \<zero>" if "i < length p1"
|
|
|
817 |
using that in_carrier
|
|
|
818 |
add.finprod_cong'[of "{..i}" "{..i}" "\<lambda>k. ?f2 k \<otimes> ?f3 (i - k)" "\<lambda>i. \<zero>"]
|
|
|
819 |
by auto
|
|
|
820 |
hence eq_lt: "?f1 i = (\<lambda>i. (\<Oplus> k \<in> {..i}. ?f2 k \<otimes> ?f3 (i - k))) i" if "i < length p1"
|
|
|
821 |
using that by auto
|
|
|
822 |
|
|
|
823 |
have "\<And>k. k \<in> {..i} \<Longrightarrow>
|
|
|
824 |
?f2 k \<otimes>\<^bsub>R\<^esub> ?f3 (i - k) = (if length p1 = k then a \<otimes> coeff p2 (i - k) else \<zero>)"
|
|
|
825 |
using in_carrier by auto
|
|
|
826 |
hence "(\<Oplus> k \<in> {..i}. ?f2 k \<otimes> ?f3 (i - k)) =
|
|
|
827 |
(\<Oplus> k \<in> {..i}. (if length p1 = k then a \<otimes> coeff p2 (i - k) else \<zero>))"
|
|
|
828 |
using in_carrier
|
|
|
829 |
add.finprod_cong'[of "{..i}" "{..i}" "\<lambda>k. ?f2 k \<otimes> ?f3 (i - k)"
|
|
|
830 |
"\<lambda>k. (if length p1 = k then a \<otimes> coeff p2 (i - k) else \<zero>)"]
|
|
|
831 |
by fastforce
|
|
|
832 |
also have " ... = a \<otimes> (coeff p2) (i - length p1)" if "i \<ge> length p1"
|
|
|
833 |
using add.finprod_singleton[of "length p1" "{..i}" "\<lambda>j. a \<otimes> (coeff p2) (i - j)"]
|
|
|
834 |
in_carrier that by auto
|
|
|
835 |
finally
|
|
|
836 |
have "(\<Oplus> k \<in> {..i}. ?f2 k \<otimes> ?f3 (i - k)) = a \<otimes> (coeff p2) (i - length p1)" if "i \<ge> length p1"
|
|
|
837 |
using that by simp
|
|
|
838 |
hence eq_ge: "?f1 i = (\<lambda>i. (\<Oplus> k \<in> {..i}. ?f2 k \<otimes> ?f3 (i - k))) i" if "i \<ge> length p1"
|
|
|
839 |
using that by auto
|
|
|
840 |
|
|
|
841 |
from eq_lt eq_ge show "?f1 i = (\<lambda>i. (\<Oplus> k \<in> {..i}. ?f2 k \<otimes> ?f3 (i - k))) i" by auto
|
|
|
842 |
qed
|
|
|
843 |
|
|
|
844 |
finally have coeff_a_p2:
|
|
|
845 |
"coeff ?a_p2 = (\<lambda>i. \<Oplus> k \<in> {..i}. (if k = length p1 then a else \<zero>) \<otimes> (coeff p2) (i - k))" .
|
|
|
846 |
|
|
|
847 |
have "set ?a_p2 \<subseteq> carrier R"
|
|
|
848 |
using in_carrier(1) assms(2) by auto
|
|
|
849 |
|
|
|
850 |
moreover have "set (poly_mult p1 p2) \<subseteq> carrier R"
|
|
|
851 |
using poly_mult_is_polynomial[of p1 p2] polynomial_in_carrier assms(2) Cons(2) by auto
|
|
|
852 |
|
|
|
853 |
ultimately
|
|
|
854 |
have "coeff (poly_mult (a # p1) p2) = (\<lambda>i. ((coeff ?a_p2) i) \<oplus> ((coeff (poly_mult p1 p2)) i))"
|
|
|
855 |
using poly_add_coeff[of ?a_p2 "poly_mult p1 p2"] by simp
|
|
|
856 |
also have " ... = (\<lambda>i. (\<Oplus> k \<in> {..i}. (if k = length p1 then a else \<zero>) \<otimes> (coeff p2) (i - k)) \<oplus>
|
|
|
857 |
(\<Oplus> k \<in> {..i}. (coeff p1) k \<otimes> (coeff p2) (i - k)))"
|
|
|
858 |
using Cons coeff_a_p2 by simp
|
|
|
859 |
also have " ... = (\<lambda>i. (\<Oplus> k \<in> {..i}. ((if k = length p1 then a else \<zero>) \<otimes> (coeff p2) (i - k)) \<oplus>
|
|
|
860 |
((coeff p1) k \<otimes> (coeff p2) (i - k))))"
|
|
|
861 |
using add.finprod_multf in_carrier by auto
|
|
|
862 |
also have " ... = (\<lambda>i. (\<Oplus> k \<in> {..i}. (coeff (a # p1) k) \<otimes> (coeff p2) (i - k)))"
|
|
|
863 |
(is "(\<lambda>i. (\<Oplus> k \<in> {..i}. ?f i k)) = (\<lambda>i. (\<Oplus> k \<in> {..i}. ?g i k))")
|
|
|
864 |
proof
|
|
|
865 |
fix i
|
|
|
866 |
have "\<And>k. ?f i k = ?g i k"
|
|
|
867 |
using in_carrier coeff_length[of p1] by (auto simp add: degree_def)
|
|
|
868 |
thus "(\<Oplus> k \<in> {..i}. ?f i k) = (\<Oplus> k \<in> {..i}. ?g i k)" by simp
|
|
|
869 |
qed
|
|
|
870 |
finally show ?case .
|
|
|
871 |
qed
|
|
|
872 |
|
|
|
873 |
lemma poly_mult_zero:
|
|
|
874 |
assumes "polynomial R p"
|
|
|
875 |
shows "poly_mult [] p = []" and "poly_mult p [] = []"
|
|
|
876 |
proof -
|
|
|
877 |
show "poly_mult [] p = []" by simp
|
|
|
878 |
next
|
|
|
879 |
have "coeff (poly_mult p []) = (\<lambda>_. \<zero>)"
|
|
|
880 |
using poly_mult_coeff[OF polynomial_in_carrier[OF assms], of "[]"]
|
|
|
881 |
poly_coeff_in_carrier[OF assms] by auto
|
|
|
882 |
thus "poly_mult p [] = []"
|
|
|
883 |
using coeff_iff_polynomial_cond[OF poly_mult_closed[OF assms, of "[]"]] zero_is_polynomial by auto
|
|
|
884 |
qed
|
|
|
885 |
|
|
|
886 |
lemma poly_mult_l_distr':
|
|
|
887 |
assumes "set p1 \<subseteq> carrier R" "set p2 \<subseteq> carrier R" "set p3 \<subseteq> carrier R"
|
|
|
888 |
shows "poly_mult (poly_add p1 p2) p3 = poly_add (poly_mult p1 p3) (poly_mult p2 p3)"
|
|
|
889 |
proof -
|
|
|
890 |
let ?c1 = "coeff p1" and ?c2 = "coeff p2" and ?c3 = "coeff p3"
|
|
|
891 |
have in_carrier:
|
|
|
892 |
"\<And>i. ?c1 i \<in> carrier R" "\<And>i. ?c2 i \<in> carrier R" "\<And>i. ?c3 i \<in> carrier R"
|
|
|
893 |
using assms coeff_in_carrier by auto
|
|
|
894 |
|
|
|
895 |
have "coeff (poly_mult (poly_add p1 p2) p3) = (\<lambda>n. \<Oplus>i \<in> {..n}. (?c1 i \<oplus> ?c2 i) \<otimes> ?c3 (n - i))"
|
|
|
896 |
using poly_mult_coeff[of "poly_add p1 p2" p3] poly_add_coeff[OF assms(1-2)]
|
|
|
897 |
poly_add_in_carrier[OF assms(1-2)] assms by auto
|
|
|
898 |
also have " ... = (\<lambda>n. \<Oplus>i \<in> {..n}. (?c1 i \<otimes> ?c3 (n - i)) \<oplus> (?c2 i \<otimes> ?c3 (n - i)))"
|
|
|
899 |
using in_carrier l_distr by auto
|
|
|
900 |
also
|
|
|
901 |
have " ... = (\<lambda>n. (\<Oplus>i \<in> {..n}. (?c1 i \<otimes> ?c3 (n - i))) \<oplus> (\<Oplus>i \<in> {..n}. (?c2 i \<otimes> ?c3 (n - i))))"
|
|
|
902 |
using add.finprod_multf in_carrier by auto
|
|
|
903 |
also have " ... = coeff (poly_add (poly_mult p1 p3) (poly_mult p2 p3))"
|
|
|
904 |
using poly_mult_coeff[OF assms(1) assms(3)] poly_mult_coeff[OF assms(2-3)]
|
|
|
905 |
poly_add_coeff[OF poly_mult_in_carrier[OF assms(1) assms(3)]]
|
|
|
906 |
poly_mult_in_carrier[OF assms(2-3)] by simp
|
|
|
907 |
finally have "coeff (poly_mult (poly_add p1 p2) p3) =
|
|
|
908 |
coeff (poly_add (poly_mult p1 p3) (poly_mult p2 p3))" .
|
|
|
909 |
moreover have "polynomial R (poly_mult (poly_add p1 p2) p3)"
|
|
|
910 |
and "polynomial R (poly_add (poly_mult p1 p3) (poly_mult p2 p3))"
|
|
|
911 |
using assms poly_add_is_polynomial poly_mult_is_polynomial polynomial_in_carrier by auto
|
|
|
912 |
ultimately show ?thesis
|
|
|
913 |
using coeff_iff_polynomial_cond by auto
|
|
|
914 |
qed
|
|
|
915 |
|
|
|
916 |
lemma poly_mult_l_distr:
|
|
|
917 |
assumes "polynomial R p1" "polynomial R p2" "polynomial R p3"
|
|
|
918 |
shows "poly_mult (poly_add p1 p2) p3 = poly_add (poly_mult p1 p3) (poly_mult p2 p3)"
|
|
|
919 |
using poly_mult_l_distr' polynomial_in_carrier assms by auto
|
|
|
920 |
|
|
|
921 |
lemma poly_mult_append_replicate_zero:
|
|
|
922 |
assumes "set p1 \<subseteq> carrier R" "set p2 \<subseteq> carrier R"
|
|
|
923 |
shows "poly_mult p1 p2 = poly_mult ((replicate n \<zero>) @ p1) p2"
|
|
|
924 |
proof -
|
|
|
925 |
{ fix p1 p2 assume A: "set p1 \<subseteq> carrier R" "set p2 \<subseteq> carrier R"
|
|
|
926 |
hence "poly_mult p1 p2 = poly_mult (\<zero> # p1) p2"
|
|
|
927 |
proof -
|
|
|
928 |
let ?a_p2 = "(map ((\<otimes>) \<zero>) p2) @ (replicate (length p1) \<zero>)"
|
|
|
929 |
have "?a_p2 = replicate (length p2 + length p1) \<zero>"
|
|
|
930 |
using A(2) by (induction p2) (auto)
|
|
|
931 |
hence "poly_mult (\<zero> # p1) p2 = poly_add (replicate (length p2 + length p1) \<zero>) (poly_mult p1 p2)"
|
|
|
932 |
by (simp add: degree_def)
|
|
|
933 |
also have " ... = poly_add (normalize (replicate (length p2 + length p1) \<zero>)) (poly_mult p1 p2)"
|
|
|
934 |
using poly_add_normalize(1)[of "replicate (length p2 + length p1) \<zero>" "poly_mult p1 p2"]
|
|
|
935 |
poly_mult_in_carrier[OF A] by force
|
|
|
936 |
also have " ... = poly_mult p1 p2"
|
|
|
937 |
using poly_add_zero(2)[OF poly_mult_is_polynomial[OF A]]
|
|
|
938 |
normalize_replicate_zero[of "length p2 + length p1" "[]"] by auto
|
|
|
939 |
finally show ?thesis by auto
|
|
|
940 |
qed } note aux_lemma = this
|
|
|
941 |
|
|
|
942 |
from assms show ?thesis
|
|
|
943 |
proof (induction n)
|
|
|
944 |
case 0 thus ?case by simp
|
|
|
945 |
next
|
|
|
946 |
case (Suc n) thus ?case
|
|
|
947 |
using aux_lemma[of "replicate n \<zero> @ p1" p2] by force
|
|
|
948 |
qed
|
|
|
949 |
qed
|
|
|
950 |
|
|
|
951 |
lemma poly_mult_normalize:
|
|
|
952 |
assumes "set p1 \<subseteq> carrier R" "set p2 \<subseteq> carrier R"
|
|
|
953 |
shows "poly_mult p1 p2 = poly_mult (normalize p1) p2"
|
|
|
954 |
proof -
|
|
|
955 |
let ?replicate = "replicate (length p1 - length (normalize p1)) \<zero>"
|
|
|
956 |
have "poly_mult p1 p2 = poly_mult (?replicate @ (normalize p1)) p2"
|
|
|
957 |
using normalize_def'[of p1] by simp
|
|
|
958 |
also have " ... = poly_mult (normalize p1) p2"
|
|
|
959 |
using poly_mult_append_replicate_zero polynomial_in_carrier
|
|
|
960 |
normalize_gives_polynomial assms by auto
|
|
|
961 |
finally show ?thesis .
|
|
|
962 |
qed
|
|
|
963 |
|
|
|
964 |
end
|
|
|
965 |
|
|
|
966 |
|
|
|
967 |
subsection \<open>Properties Within a Domain\<close>
|
|
|
968 |
|
|
|
969 |
context domain
|
|
|
970 |
begin
|
|
|
971 |
|
|
|
972 |
lemma one_is_polynomial [intro]: "polynomial R [ \<one> ]"
|
|
|
973 |
unfolding polynomial_def by auto
|
|
|
974 |
|
|
|
975 |
lemma poly_mult_comm:
|
|
|
976 |
assumes "set p1 \<subseteq> carrier R" "set p2 \<subseteq> carrier R"
|
|
|
977 |
shows "poly_mult p1 p2 = poly_mult p2 p1"
|
|
|
978 |
proof -
|
|
|
979 |
let ?c1 = "coeff p1" and ?c2 = "coeff p2"
|
|
|
980 |
have "\<And>i. (\<Oplus>k \<in> {..i}. ?c1 k \<otimes> ?c2 (i - k)) = (\<Oplus>k \<in> {..i}. ?c2 k \<otimes> ?c1 (i - k))"
|
|
|
981 |
proof -
|
|
|
982 |
fix i :: nat
|
|
|
983 |
let ?f = "\<lambda>k. ?c1 k \<otimes> ?c2 (i - k)"
|
|
|
984 |
have in_carrier: "\<And>i. ?c1 i \<in> carrier R" "\<And>i. ?c2 i \<in> carrier R"
|
|
|
985 |
using coeff_in_carrier[OF assms(1)] coeff_in_carrier[OF assms(2)] by auto
|
|
|
986 |
|
|
|
987 |
have reindex_inj: "inj_on (\<lambda>k. i - k) {..i}"
|
|
|
988 |
using inj_on_def by force
|
|
|
989 |
moreover have "(\<lambda>k. i - k) ` {..i} \<subseteq> {..i}" by auto
|
|
|
990 |
hence "(\<lambda>k. i - k) ` {..i} = {..i}"
|
|
|
991 |
using reindex_inj endo_inj_surj[of "{..i}" "\<lambda>k. i - k"] by simp
|
|
|
992 |
ultimately have "(\<Oplus>k \<in> {..i}. ?f k) = (\<Oplus>k \<in> {..i}. ?f (i - k))"
|
|
|
993 |
using add.finprod_reindex[of ?f "\<lambda>k. i - k" "{..i}"] in_carrier by auto
|
|
|
994 |
|
|
|
995 |
moreover have "\<And>k. k \<in> {..i} \<Longrightarrow> ?f (i - k) = ?c2 k \<otimes> ?c1 (i - k)"
|
|
|
996 |
using in_carrier m_comm by auto
|
|
|
997 |
hence "(\<Oplus>k \<in> {..i}. ?f (i - k)) = (\<Oplus>k \<in> {..i}. ?c2 k \<otimes> ?c1 (i - k))"
|
|
|
998 |
using add.finprod_cong'[of "{..i}" "{..i}"] in_carrier by auto
|
|
|
999 |
ultimately show "(\<Oplus>k \<in> {..i}. ?f k) = (\<Oplus>k \<in> {..i}. ?c2 k \<otimes> ?c1 (i - k))"
|
|
|
1000 |
by simp
|
|
|
1001 |
qed
|
|
|
1002 |
hence "coeff (poly_mult p1 p2) = coeff (poly_mult p2 p1)"
|
|
|
1003 |
using poly_mult_coeff[OF assms] poly_mult_coeff[OF assms(2) assms(1)] by simp
|
|
|
1004 |
thus ?thesis
|
|
|
1005 |
using coeff_iff_polynomial_cond[OF poly_mult_is_polynomial[OF assms]
|
|
|
1006 |
poly_mult_is_polynomial[OF assms(2) assms(1)]] by simp
|
|
|
1007 |
qed
|
|
|
1008 |
|
|
|
1009 |
lemma poly_mult_r_distr':
|
|
|
1010 |
assumes "set p1 \<subseteq> carrier R" "set p2 \<subseteq> carrier R" "set p3 \<subseteq> carrier R"
|
|
|
1011 |
shows "poly_mult p1 (poly_add p2 p3) = poly_add (poly_mult p1 p2) (poly_mult p1 p3)"
|
|
|
1012 |
using poly_mult_comm[OF assms(1-2)] poly_mult_l_distr'[OF assms(2-3) assms(1)]
|
|
|
1013 |
poly_mult_comm[OF assms(1) assms(3)] poly_add_is_polynomial[OF assms(2-3)]
|
|
|
1014 |
polynomial_in_carrier poly_mult_comm[OF assms(1), of "poly_add p2 p3"] by simp
|
|
|
1015 |
|
|
|
1016 |
lemma poly_mult_r_distr:
|
|
|
1017 |
assumes "polynomial R p1" "polynomial R p2" "polynomial R p3"
|
|
|
1018 |
shows "poly_mult p1 (poly_add p2 p3) = poly_add (poly_mult p1 p2) (poly_mult p1 p3)"
|
|
|
1019 |
using poly_mult_r_distr' polynomial_in_carrier assms by auto
|
|
|
1020 |
|
|
|
1021 |
lemma poly_mult_replicate_zero:
|
|
|
1022 |
assumes "set p \<subseteq> carrier R"
|
|
|
1023 |
shows "poly_mult (replicate n \<zero>) p = []"
|
|
|
1024 |
and "poly_mult p (replicate n \<zero>) = []"
|
|
|
1025 |
proof -
|
|
|
1026 |
have in_carrier: "\<And>n. set (replicate n \<zero>) \<subseteq> carrier R" by auto
|
|
|
1027 |
show "poly_mult (replicate n \<zero>) p = []" using assms
|
|
|
1028 |
proof (induction n)
|
|
|
1029 |
case 0 thus ?case by simp
|
|
|
1030 |
next
|
|
|
1031 |
case (Suc n)
|
|
|
1032 |
hence "poly_mult (replicate (Suc n) \<zero>) p = poly_mult (\<zero> # (replicate n \<zero>)) p"
|
|
|
1033 |
by simp
|
|
|
1034 |
also have " ... = poly_add ((map (\<lambda>a. \<zero> \<otimes> a) p) @ (replicate n \<zero>)) []"
|
|
|
1035 |
using Suc by (simp add: degree_def)
|
|
|
1036 |
also have " ... = poly_add ((map (\<lambda>a. \<zero>) p) @ (replicate n \<zero>)) []"
|
|
|
1037 |
using Suc(2) by (smt map_eq_conv ring_simprules(24) subset_code(1))
|
|
|
1038 |
also have " ... = poly_add (replicate (length p + n) \<zero>) []"
|
|
|
1039 |
by (simp add: map_replicate_const replicate_add)
|
|
|
1040 |
also have " ... = poly_add [] []"
|
|
|
1041 |
using poly_add_normalize(1)[of "replicate (length p + n) \<zero>" "[]"]
|
|
|
1042 |
normalize_replicate_zero[of "length p + n" "[]"] by auto
|
|
|
1043 |
also have " ... = []" by simp
|
|
|
1044 |
finally show ?case .
|
|
|
1045 |
qed
|
|
|
1046 |
thus "poly_mult p (replicate n \<zero>) = []"
|
|
|
1047 |
using poly_mult_comm[OF assms in_carrier] by simp
|
|
|
1048 |
qed
|
|
|
1049 |
|
|
|
1050 |
lemma poly_mult_const:
|
|
|
1051 |
assumes "polynomial R p" "a \<in> carrier R - { \<zero> }"
|
|
|
1052 |
shows "poly_mult [ a ] p = map (\<lambda>b. a \<otimes> b) p" and "poly_mult p [ a ] = map (\<lambda>b. a \<otimes> b) p"
|
|
|
1053 |
proof -
|
|
|
1054 |
show "poly_mult [ a ] p = map (\<lambda>b. a \<otimes> b) p"
|
|
|
1055 |
proof -
|
|
|
1056 |
have "poly_mult [ a ] p = poly_add (map (\<lambda>b. a \<otimes> b) p) []"
|
|
|
1057 |
by (simp add: degree_def)
|
|
|
1058 |
moreover have "polynomial R (map (\<lambda>b. a \<otimes> b) p)"
|
|
|
1059 |
proof (cases p)
|
|
|
1060 |
case Nil thus ?thesis by (simp add: polynomial_def)
|
|
|
1061 |
next
|
|
|
1062 |
case (Cons b ps)
|
|
|
1063 |
hence "a \<otimes> lead_coeff p \<noteq> \<zero>"
|
|
|
1064 |
using assms integral[of a "lead_coeff p"] unfolding polynomial_def by auto
|
|
|
1065 |
thus ?thesis
|
|
|
1066 |
using Cons polynomial_in_carrier[OF assms(1)] assms(2) unfolding polynomial_def by auto
|
|
|
1067 |
qed
|
|
|
1068 |
ultimately show ?thesis
|
|
|
1069 |
using poly_add_zero(1)[of "map (\<lambda>b. a \<otimes> b) p"] by simp
|
|
|
1070 |
qed
|
|
|
1071 |
thus "poly_mult p [ a ] = map (\<lambda>b. a \<otimes> b) p"
|
|
|
1072 |
using poly_mult_comm[of "[ a ]" p] polynomial_in_carrier[OF assms(1)] assms(2) by auto
|
|
|
1073 |
qed
|
|
|
1074 |
|
|
|
1075 |
lemma poly_mult_monon:
|
|
|
1076 |
assumes "polynomial R p" "a \<in> carrier R - { \<zero> }"
|
|
|
1077 |
shows "poly_mult (monon a n) p =
|
|
|
1078 |
(if p = [] then [] else (map (\<lambda>b. a \<otimes> b) p) @ (replicate n \<zero>))"
|
|
|
1079 |
proof (cases p)
|
|
|
1080 |
case Nil thus ?thesis
|
|
|
1081 |
using poly_mult_zero(2)[OF monon_is_polynomial[OF assms(2)]] by simp
|
|
|
1082 |
next
|
|
|
1083 |
case (Cons b ps)
|
|
|
1084 |
hence "lead_coeff ((map (\<lambda>b. a \<otimes> b) p) @ (replicate n \<zero>)) = a \<otimes> b"
|
|
|
1085 |
by simp
|
|
|
1086 |
hence "lead_coeff ((map (\<lambda>b. a \<otimes> b) p) @ (replicate n \<zero>)) \<noteq> \<zero>"
|
|
|
1087 |
using Cons assms integral[of a b] unfolding polynomial_def by auto
|
|
|
1088 |
moreover have "set ((map (\<lambda>b. a \<otimes> b) p) @ (replicate n \<zero>)) \<subseteq> carrier R"
|
|
|
1089 |
using polynomial_in_carrier[OF assms(1)] assms(2) DiffD1 by auto
|
|
|
1090 |
ultimately have is_polynomial: "polynomial R ((map (\<lambda>b. a \<otimes> b) p) @ (replicate n \<zero>))"
|
|
|
1091 |
using Cons unfolding polynomial_def by auto
|
|
|
1092 |
|
|
|
1093 |
have "poly_mult (a # replicate n \<zero>) p =
|
|
|
1094 |
poly_add ((map (\<lambda>b. a \<otimes> b) p) @ (replicate n \<zero>)) (poly_mult (replicate n \<zero>) p)"
|
|
|
1095 |
by (simp add: degree_def)
|
|
|
1096 |
also have " ... = poly_add ((map (\<lambda>b. a \<otimes> b) p) @ (replicate n \<zero>)) []"
|
|
|
1097 |
using poly_mult_replicate_zero(1)[OF polynomial_in_carrier[OF assms(1)]] by simp
|
|
|
1098 |
also have " ... = (map (\<lambda>b. a \<otimes> b) p) @ (replicate n \<zero>)"
|
|
|
1099 |
using poly_add_zero(1)[OF is_polynomial] .
|
|
|
1100 |
also have " ... = (if p = [] then [] else (map (\<lambda>b. a \<otimes> b) p) @ (replicate n \<zero>))"
|
|
|
1101 |
using Cons by auto
|
|
|
1102 |
finally show ?thesis unfolding monon_def .
|
|
|
1103 |
qed
|
|
|
1104 |
|
|
|
1105 |
lemma poly_mult_one:
|
|
|
1106 |
assumes "polynomial R p"
|
|
|
1107 |
shows "poly_mult [ \<one> ] p = p" and "poly_mult p [ \<one> ] = p"
|
|
|
1108 |
proof -
|
|
|
1109 |
have "map (\<lambda>a. \<one> \<otimes> a) p = p"
|
|
|
1110 |
using polynomial_in_carrier[OF assms] by (meson assms l_one map_idI subsetCE)
|
|
|
1111 |
thus "poly_mult [ \<one> ] p = p" and "poly_mult p [ \<one> ] = p"
|
|
|
1112 |
using poly_mult_const[OF assms, of \<one>] by auto
|
|
|
1113 |
qed
|
|
|
1114 |
|
|
|
1115 |
lemma poly_mult_lead_coeff_aux:
|
|
|
1116 |
assumes "polynomial R p1" "polynomial R p2" and "p1 \<noteq> []" and "p2 \<noteq> []"
|
|
|
1117 |
shows "(coeff (poly_mult p1 p2)) (degree p1 + degree p2) = (lead_coeff p1) \<otimes> (lead_coeff p2)"
|
|
|
1118 |
proof -
|
|
|
1119 |
have p1: "lead_coeff p1 \<in> carrier R - { \<zero> }" and p2: "lead_coeff p2 \<in> carrier R - { \<zero> }"
|
|
|
1120 |
using assms unfolding polynomial_def by auto
|
|
|
1121 |
|
|
|
1122 |
have "(coeff (poly_mult p1 p2)) (degree p1 + degree p2) =
|
|
|
1123 |
(\<Oplus> k \<in> {..((degree p1) + (degree p2))}.
|
|
|
1124 |
(coeff p1) k \<otimes> (coeff p2) ((degree p1) + (degree p2) - k))"
|
|
|
1125 |
using poly_mult_coeff assms(1-2) polynomial_in_carrier by auto
|
|
|
1126 |
also have " ... = (lead_coeff p1) \<otimes> (lead_coeff p2)"
|
|
|
1127 |
proof -
|
|
|
1128 |
let ?f = "\<lambda>i. (coeff p1) i \<otimes> (coeff p2) ((degree p1) + (degree p2) - i)"
|
|
|
1129 |
have in_carrier: "\<And>i. (coeff p1) i \<in> carrier R" "\<And>i. (coeff p2) i \<in> carrier R"
|
|
|
1130 |
using coeff_in_carrier assms by auto
|
|
|
1131 |
have "\<And>i. i < degree p1 \<Longrightarrow> ?f i = \<zero>"
|
|
|
1132 |
using coeff_degree[of p2] in_carrier by auto
|
|
|
1133 |
moreover have "\<And>i. i > degree p1 \<Longrightarrow> ?f i = \<zero>"
|
|
|
1134 |
using coeff_degree[of p1] in_carrier by auto
|
|
|
1135 |
moreover have "?f (degree p1) = (lead_coeff p1) \<otimes> (lead_coeff p2)"
|
|
|
1136 |
using assms(3-4) by simp
|
|
|
1137 |
ultimately have "?f = (\<lambda>i. if degree p1 = i then (lead_coeff p1) \<otimes> (lead_coeff p2) else \<zero>)"
|
|
|
1138 |
using nat_neq_iff by auto
|
|
|
1139 |
thus ?thesis
|
|
|
1140 |
using add.finprod_singleton[of "degree p1" "{..((degree p1) + (degree p2))}"
|
|
|
1141 |
"\<lambda>i. (lead_coeff p1) \<otimes> (lead_coeff p2)"] p1 p2 by auto
|
|
|
1142 |
qed
|
|
|
1143 |
finally show ?thesis .
|
|
|
1144 |
qed
|
|
|
1145 |
|
|
|
1146 |
lemma poly_mult_degree_eq:
|
|
|
1147 |
assumes "polynomial R p1" "polynomial R p2"
|
|
|
1148 |
shows "degree (poly_mult p1 p2) = (if p1 = [] \<or> p2 = [] then 0 else (degree p1) + (degree p2))"
|
|
|
1149 |
proof (cases p1)
|
|
|
1150 |
case Nil thus ?thesis by (simp add: degree_def)
|
|
|
1151 |
next
|
|
|
1152 |
case (Cons a p1') note p1 = Cons
|
|
|
1153 |
show ?thesis
|
|
|
1154 |
proof (cases p2)
|
|
|
1155 |
case Nil thus ?thesis
|
|
|
1156 |
using poly_mult_zero(2)[OF assms(1)] by (simp add: degree_def)
|
|
|
1157 |
next
|
|
|
1158 |
case (Cons b p2') note p2 = Cons
|
|
|
1159 |
have a: "a \<in> carrier R" and b: "b \<in> carrier R"
|
|
|
1160 |
using p1 p2 polynomial_in_carrier[OF assms(1)] polynomial_in_carrier[OF assms(2)] by auto
|
|
|
1161 |
have "(coeff (poly_mult p1 p2)) ((degree p1) + (degree p2)) = a \<otimes> b"
|
|
|
1162 |
using poly_mult_lead_coeff_aux[OF assms] p1 p2 by simp
|
|
|
1163 |
hence "(coeff (poly_mult p1 p2)) ((degree p1) + (degree p2)) \<noteq> \<zero>"
|
|
|
1164 |
using assms p1 p2 integral[of a b] unfolding polynomial_def by auto
|
|
|
1165 |
moreover have "\<And>i. i > (degree p1) + (degree p2) \<Longrightarrow> (coeff (poly_mult p1 p2)) i = \<zero>"
|
|
|
1166 |
proof -
|
|
|
1167 |
have aux_lemma: "degree (poly_mult p1 p2) \<le> (degree p1) + (degree p2)"
|
|
|
1168 |
proof (induct p1)
|
|
|
1169 |
case Nil
|
|
|
1170 |
then show ?case by simp
|
|
|
1171 |
next
|
|
|
1172 |
case (Cons a p1)
|
|
|
1173 |
let ?a_p2 = "(map (\<lambda>b. a \<otimes> b) p2) @ (replicate (degree (a # p1)) \<zero>)"
|
|
|
1174 |
have "poly_mult (a # p1) p2 = poly_add ?a_p2 (poly_mult p1 p2)" by simp
|
|
|
1175 |
hence "degree (poly_mult (a # p1) p2) \<le> max (degree ?a_p2) (degree (poly_mult p1 p2))"
|
|
|
1176 |
using poly_add_degree[of ?a_p2 "poly_mult p1 p2"] by simp
|
|
|
1177 |
also have " ... \<le> max ((degree (a # p1)) + (degree p2)) (degree (poly_mult p1 p2))"
|
|
|
1178 |
unfolding degree_def by auto
|
|
|
1179 |
also have " ... \<le> max ((degree (a # p1)) + (degree p2)) ((degree p1) + (degree p2))"
|
|
|
1180 |
using Cons by simp
|
|
|
1181 |
also have " ... \<le> (degree (a # p1)) + (degree p2)"
|
|
|
1182 |
unfolding degree_def by auto
|
|
|
1183 |
finally show ?case .
|
|
|
1184 |
qed
|
|
|
1185 |
fix i show "i > (degree p1) + (degree p2) \<Longrightarrow> (coeff (poly_mult p1 p2)) i = \<zero>"
|
|
|
1186 |
using coeff_degree aux_lemma by simp
|
|
|
1187 |
qed
|
|
|
1188 |
ultimately have "degree (poly_mult p1 p2) = degree p1 + degree p2"
|
|
|
1189 |
using degree_def'[OF poly_mult_closed[OF assms]]
|
|
|
1190 |
by (smt coeff_degree linorder_cases not_less_Least)
|
|
|
1191 |
thus ?thesis
|
|
|
1192 |
using p1 p2 by auto
|
|
|
1193 |
qed
|
|
|
1194 |
qed
|
|
|
1195 |
|
|
|
1196 |
lemma poly_mult_integral:
|
|
|
1197 |
assumes "polynomial R p1" "polynomial R p2"
|
|
|
1198 |
shows "poly_mult p1 p2 = [] \<Longrightarrow> p1 = [] \<or> p2 = []"
|
|
|
1199 |
proof (rule ccontr)
|
|
|
1200 |
assume A: "poly_mult p1 p2 = []" "\<not> (p1 = [] \<or> p2 = [])"
|
|
|
1201 |
hence "degree (poly_mult p1 p2) = degree p1 + degree p2"
|
|
|
1202 |
using poly_mult_degree_eq[OF assms] by simp
|
|
|
1203 |
hence "length p1 = 1 \<and> length p2 = 1"
|
|
|
1204 |
unfolding degree_def using A Suc_diff_Suc by fastforce
|
|
|
1205 |
then obtain a b where p1: "p1 = [ a ]" and p2: "p2 = [ b ]"
|
|
|
1206 |
by (metis One_nat_def length_0_conv length_Suc_conv)
|
|
|
1207 |
hence "a \<in> carrier R - { \<zero> }" and "b \<in> carrier R - { \<zero> }"
|
|
|
1208 |
using assms unfolding polynomial_def by auto
|
|
|
1209 |
hence "poly_mult [ a ] [ b ] = [ a \<otimes> b ]"
|
|
|
1210 |
using A assms(2) poly_mult_const(1) p1 by fastforce
|
|
|
1211 |
thus False using A(1) p1 p2 by simp
|
|
|
1212 |
qed
|
|
|
1213 |
|
|
|
1214 |
lemma poly_mult_lead_coeff:
|
|
|
1215 |
assumes "polynomial R p1" "polynomial R p2" and "p1 \<noteq> []" and "p2 \<noteq> []"
|
|
|
1216 |
shows "lead_coeff (poly_mult p1 p2) = (lead_coeff p1) \<otimes> (lead_coeff p2)"
|
|
|
1217 |
proof -
|
|
|
1218 |
have "poly_mult p1 p2 \<noteq> []"
|
|
|
1219 |
using poly_mult_integral[OF assms(1-2)] assms(3-4) by auto
|
|
|
1220 |
hence "lead_coeff (poly_mult p1 p2) = (coeff (poly_mult p1 p2)) (degree p1 + degree p2)"
|
|
|
1221 |
using poly_mult_degree_eq[OF assms(1-2)] assms(3-4) by (metis coeff.simps(2) list.collapse)
|
|
|
1222 |
thus ?thesis
|
|
|
1223 |
using poly_mult_lead_coeff_aux[OF assms] by simp
|
|
|
1224 |
qed
|
|
|
1225 |
|
|
|
1226 |
end
|
|
|
1227 |
|
|
|
1228 |
|
|
|
1229 |
subsection \<open>Algebraic Structure of Polynomials\<close>
|
|
|
1230 |
|
|
|
1231 |
definition univ_poly :: "('a, 'b) ring_scheme \<Rightarrow> ('a list) ring"
|
|
|
1232 |
where "univ_poly R =
|
|
|
1233 |
\<lparr> carrier = { p. polynomial R p },
|
|
|
1234 |
monoid.mult = ring.poly_mult R,
|
|
|
1235 |
one = [ \<one>\<^bsub>R\<^esub> ],
|
|
|
1236 |
zero = [],
|
|
|
1237 |
add = ring.poly_add R \<rparr>"
|
|
|
1238 |
|
|
|
1239 |
context domain
|
|
|
1240 |
begin
|
|
|
1241 |
|
|
|
1242 |
lemma poly_mult_assoc_aux:
|
|
|
1243 |
assumes "set p \<subseteq> carrier R" "set q \<subseteq> carrier R" and "a \<in> carrier R"
|
|
|
1244 |
shows "poly_mult ((map (\<lambda>b. a \<otimes> b) p) @ (replicate n \<zero>)) q =
|
|
|
1245 |
poly_mult (monon a n) (poly_mult p q)"
|
|
|
1246 |
proof -
|
|
|
1247 |
let ?len = "n"
|
|
|
1248 |
let ?a_p = "(map (\<lambda>b. a \<otimes> b) p) @ (replicate ?len \<zero>)"
|
|
|
1249 |
let ?c2 = "coeff p" and ?c3 = "coeff q"
|
|
|
1250 |
have coeff_a_p:
|
|
|
1251 |
"coeff ?a_p = (\<lambda>i. if i < ?len then \<zero> else a \<otimes> ?c2 (i - ?len))" (is
|
|
|
1252 |
"coeff ?a_p = (\<lambda>i. ?f i)")
|
|
|
1253 |
using append_coeff[of "map ((\<otimes>) a) p" "replicate ?len \<zero>"]
|
|
|
1254 |
replicate_zero_coeff[of ?len] scalar_coeff[OF assms(3), of p] by auto
|
|
|
1255 |
have in_carrier:
|
|
|
1256 |
"set ?a_p \<subseteq> carrier R" "\<And>i. ?c2 i \<in> carrier R" "\<And>i. ?c3 i \<in> carrier R"
|
|
|
1257 |
"\<And>i. coeff (poly_mult p q) i \<in> carrier R"
|
|
|
1258 |
using assms poly_mult_in_carrier by auto
|
|
|
1259 |
have "coeff (poly_mult ?a_p q) = (\<lambda>n. (\<Oplus>i \<in> {..n}. (coeff ?a_p) i \<otimes> ?c3 (n - i)))"
|
|
|
1260 |
using poly_mult_coeff[OF in_carrier(1) assms(2)] .
|
|
|
1261 |
also have " ... = (\<lambda>n. (\<Oplus>i \<in> {..n}. (?f i) \<otimes> ?c3 (n - i)))"
|
|
|
1262 |
using coeff_a_p by simp
|
|
|
1263 |
also have " ... = (\<lambda>n. (\<Oplus>i \<in> {..n}. (if i = ?len then a else \<zero>) \<otimes> (coeff (poly_mult p q)) (n - i)))"
|
|
|
1264 |
(is "(\<lambda>n. (\<Oplus>i \<in> {..n}. ?side1 n i)) = (\<lambda>n. (\<Oplus>i \<in> {..n}. ?side2 n i))")
|
|
|
1265 |
proof
|
|
|
1266 |
fix n
|
|
|
1267 |
have in_carrier': "\<And>i. ?side1 n i \<in> carrier R" "\<And>i. ?side2 n i \<in> carrier R"
|
|
|
1268 |
using in_carrier assms coeff_in_carrier poly_mult_in_carrier by auto
|
|
|
1269 |
show "(\<Oplus>i \<in> {..n}. ?side1 n i) = (\<Oplus>i \<in> {..n}. ?side2 n i)"
|
|
|
1270 |
proof (cases "n < ?len")
|
|
|
1271 |
assume "n < ?len"
|
|
|
1272 |
hence "\<And>i. i \<le> n \<Longrightarrow> ?side1 n i = ?side2 n i"
|
|
|
1273 |
using in_carrier assms coeff_in_carrier poly_mult_in_carrier by simp
|
|
|
1274 |
thus ?thesis
|
|
|
1275 |
using add.finprod_cong'[of "{..n}" "{..n}" "?side1 n" "?side2 n"] in_carrier'
|
|
|
1276 |
by (metis (no_types, lifting) Pi_I' atMost_iff)
|
|
|
1277 |
next
|
|
|
1278 |
assume "\<not> n < ?len"
|
|
|
1279 |
hence n_ge: "n \<ge> ?len" by simp
|
|
|
1280 |
define h where "h = (\<lambda>i. if i < ?len then \<zero> else (a \<otimes> ?c2 (i - ?len)) \<otimes> ?c3 (n - i))"
|
|
|
1281 |
hence h_in_carrier: "\<And>i. h i \<in> carrier R"
|
|
|
1282 |
using assms(3) in_carrier by auto
|
|
|
1283 |
have "\<And>i. (?f i) \<otimes> ?c3 (n - i) = h i"
|
|
|
1284 |
using in_carrier(2-3) assms(3) h_def by auto
|
|
|
1285 |
hence "(\<Oplus>i \<in> {..n}. ?side1 n i) = (\<Oplus>i \<in> {..n}. h i)"
|
|
|
1286 |
by simp
|
|
|
1287 |
also have " ... = (\<Oplus>i \<in> {..<?len}. h i) \<oplus> (\<Oplus>i \<in> {?len..n}. h i)"
|
|
|
1288 |
using add.finprod_Un_disjoint[of "{..<?len}" "{?len..n}" h] h_in_carrier n_ge
|
|
|
1289 |
by (simp add: ivl_disj_int_one(4) ivl_disj_un_one(4))
|
|
|
1290 |
also have " ... = (\<Oplus>i \<in> {..<?len}. \<zero>) \<oplus> (\<Oplus>i \<in> {?len..n}. h i)"
|
|
|
1291 |
using add.finprod_cong'[of "{..<?len}" "{..<?len}" h "\<lambda>_. \<zero>"] h_in_carrier
|
|
|
1292 |
unfolding h_def by auto
|
|
|
1293 |
also have " ... = (\<Oplus>i \<in> {?len..n}. h i)"
|
|
|
1294 |
using add.finprod_one h_in_carrier by simp
|
|
|
1295 |
also have " ... = (\<Oplus>i \<in> (\<lambda>i. i + ?len) ` {..n - ?len}. h i)"
|
|
|
1296 |
using n_ge atLeast0AtMost image_add_atLeastAtMost'[of ?len 0 "n - ?len"] by auto
|
|
|
1297 |
also have " ... = (\<Oplus>i \<in> {..n - ?len}. h (i + ?len))"
|
|
|
1298 |
using add.finprod_reindex[of h "\<lambda>i. i + ?len" "{..n - ?len}"] h_in_carrier by simp
|
|
|
1299 |
also have " ... = (\<Oplus>i \<in> {..n - ?len}. (a \<otimes> ?c2 i) \<otimes> ?c3 (n - (i + ?len)))"
|
|
|
1300 |
unfolding h_def by simp
|
|
|
1301 |
also have " ... = (\<Oplus>i \<in> {..n - ?len}. a \<otimes> (?c2 i \<otimes> ?c3 (n - (i + ?len))))"
|
|
|
1302 |
using in_carrier assms(3) by (simp add: m_assoc)
|
|
|
1303 |
also have " ... = a \<otimes> (\<Oplus>i \<in> {..n - ?len}. ?c2 i \<otimes> ?c3 (n - (i + ?len)))"
|
|
|
1304 |
using finsum_rdistr[of "{..n - ?len}" a "\<lambda>i. ?c2 i \<otimes> ?c3 (n - (i + ?len))"]
|
|
|
1305 |
in_carrier(2-3) assms(3) by simp
|
|
|
1306 |
also have " ... = a \<otimes> (coeff (poly_mult p q)) (n - ?len)"
|
|
|
1307 |
using poly_mult_coeff[OF assms(1-2)] n_ge by (simp add: add.commute)
|
|
|
1308 |
also have " ... =
|
|
|
1309 |
(\<Oplus>i \<in> {..n}. if ?len = i then a \<otimes> (coeff (poly_mult p q)) (n - i) else \<zero>)"
|
|
|
1310 |
using add.finprod_singleton[of ?len "{..n}" "\<lambda>i. a \<otimes> (coeff (poly_mult p q)) (n - i)"]
|
|
|
1311 |
n_ge in_carrier(2-4) assms by simp
|
|
|
1312 |
also have " ... = (\<Oplus>i \<in> {..n}. (if ?len = i then a else \<zero>) \<otimes> (coeff (poly_mult p q)) (n - i))"
|
|
|
1313 |
using in_carrier(2-4) assms(3) add.finprod_cong'[of "{..n}" "{..n}"] by simp
|
|
|
1314 |
also have " ... = (\<Oplus>i \<in> {..n}. ?side2 n i)"
|
|
|
1315 |
proof -
|
|
|
1316 |
have "(\<lambda>i. (if ?len = i then a else \<zero>) \<otimes> (coeff (poly_mult p q)) (n - i)) = ?side2 n" by auto
|
|
|
1317 |
thus ?thesis by simp
|
|
|
1318 |
qed
|
|
|
1319 |
finally show ?thesis .
|
|
|
1320 |
qed
|
|
|
1321 |
qed
|
|
|
1322 |
also have " ... = coeff (poly_mult (monon a n) (poly_mult p q))"
|
|
|
1323 |
using monon_coeff[of a "n"] poly_mult_coeff[of "monon a n" "poly_mult p q"]
|
|
|
1324 |
poly_mult_in_carrier[OF assms(1-2)] assms(3) unfolding monon_def by force
|
|
|
1325 |
finally
|
|
|
1326 |
have "coeff (poly_mult ?a_p q) = coeff (poly_mult (monon a n) (poly_mult p q))" .
|
|
|
1327 |
moreover have "polynomial R (poly_mult ?a_p q)"
|
|
|
1328 |
using poly_mult_is_polynomial[OF in_carrier(1) assms(2)] by simp
|
|
|
1329 |
moreover have "polynomial R (poly_mult (monon a n) (poly_mult p q))"
|
|
|
1330 |
using poly_mult_is_polynomial[of "monon a n" "poly_mult p q"]
|
|
|
1331 |
poly_mult_in_carrier[OF assms(1-2)] assms(3) unfolding monon_def
|
|
|
1332 |
using in_carrier(1) by auto
|
|
|
1333 |
ultimately show ?thesis
|
|
|
1334 |
using coeff_iff_polynomial_cond by simp
|
|
|
1335 |
qed
|
|
|
1336 |
|
|
|
1337 |
lemma univ_poly_is_monoid: "monoid (univ_poly R)"
|
|
|
1338 |
unfolding univ_poly_def using poly_mult_one
|
|
|
1339 |
proof (auto simp add: poly_add_closed poly_mult_closed one_is_polynomial monoid_def)
|
|
|
1340 |
fix p1 p2 p3
|
|
|
1341 |
let ?P = "poly_mult (poly_mult p1 p2) p3 = poly_mult p1 (poly_mult p2 p3)"
|
|
|
1342 |
|
|
|
1343 |
assume A: "polynomial R p1" "polynomial R p2" "polynomial R p3"
|
|
|
1344 |
show ?P using polynomial_in_carrier[OF A(1)]
|
|
|
1345 |
proof (induction p1)
|
|
|
1346 |
case Nil thus ?case by simp
|
|
|
1347 |
next
|
|
|
1348 |
case (Cons a p1) thus ?case
|
|
|
1349 |
proof (cases "a = \<zero>")
|
|
|
1350 |
assume eq_zero: "a = \<zero>"
|
|
|
1351 |
have p1: "set p1 \<subseteq> carrier R"
|
|
|
1352 |
using Cons(2) by simp
|
|
|
1353 |
have "poly_mult (poly_mult (a # p1) p2) p3 = poly_mult (poly_mult p1 p2) p3"
|
|
|
1354 |
using poly_mult_append_replicate_zero[OF p1 polynomial_in_carrier[OF A(2)], of "Suc 0"]
|
|
|
1355 |
eq_zero by simp
|
|
|
1356 |
also have " ... = poly_mult p1 (poly_mult p2 p3)"
|
|
|
1357 |
using p1[THEN Cons(1)] by simp
|
|
|
1358 |
also have " ... = poly_mult (a # p1) (poly_mult p2 p3)"
|
|
|
1359 |
using poly_mult_append_replicate_zero[OF p1
|
|
|
1360 |
poly_mult_in_carrier[OF A(2-3)[THEN polynomial_in_carrier]], of "Suc 0"] eq_zero by simp
|
|
|
1361 |
finally show ?thesis .
|
|
|
1362 |
next
|
|
|
1363 |
assume "a \<noteq> \<zero>" hence in_carrier:
|
|
|
1364 |
"set p1 \<subseteq> carrier R" "set p2 \<subseteq> carrier R" "set p3 \<subseteq> carrier R" "a \<in> carrier R - { \<zero> }"
|
|
|
1365 |
using A(2-3) polynomial_in_carrier Cons by auto
|
|
|
1366 |
|
|
|
1367 |
let ?a_p2 = "(map (\<lambda>b. a \<otimes> b) p2) @ (replicate (length p1) \<zero>)"
|
|
|
1368 |
have a_p2_in_carrier: "set ?a_p2 \<subseteq> carrier R"
|
|
|
1369 |
using in_carrier by auto
|
|
|
1370 |
|
|
|
1371 |
have "poly_mult (poly_mult (a # p1) p2) p3 = poly_mult (poly_add ?a_p2 (poly_mult p1 p2)) p3"
|
|
|
1372 |
by (simp add: degree_def)
|
|
|
1373 |
also have " ... = poly_add (poly_mult ?a_p2 p3) (poly_mult (poly_mult p1 p2) p3)"
|
|
|
1374 |
using poly_mult_l_distr'[OF a_p2_in_carrier poly_mult_in_carrier[OF in_carrier(1-2)] in_carrier(3)] .
|
|
|
1375 |
also have " ... = poly_add (poly_mult ?a_p2 p3) (poly_mult p1 (poly_mult p2 p3))"
|
|
|
1376 |
using Cons(1)[OF in_carrier(1)] by simp
|
|
|
1377 |
also have " ... = poly_add (poly_mult (a # (replicate (length p1) \<zero>)) (poly_mult p2 p3))
|
|
|
1378 |
(poly_mult p1 (poly_mult p2 p3))"
|
|
|
1379 |
using poly_mult_assoc_aux[of p2 p3 a "length p1"] in_carrier unfolding monon_def by simp
|
|
|
1380 |
also have " ... = poly_mult (poly_add (a # (replicate (length p1) \<zero>)) p1) (poly_mult p2 p3)"
|
|
|
1381 |
using poly_mult_l_distr'[of "a # (replicate (length p1) \<zero>)" p1 "poly_mult p2 p3"]
|
|
|
1382 |
poly_mult_in_carrier[OF in_carrier(2-3)] in_carrier by force
|
|
|
1383 |
also have " ... = poly_mult (a # p1) (poly_mult p2 p3)"
|
|
|
1384 |
using poly_add_monon[OF in_carrier(1) in_carrier(4)] unfolding monon_def by simp
|
|
|
1385 |
finally show ?thesis .
|
|
|
1386 |
qed
|
|
|
1387 |
qed
|
|
|
1388 |
qed
|
|
|
1389 |
|
|
|
1390 |
declare poly_add.simps[simp del]
|
|
|
1391 |
|
|
|
1392 |
lemma univ_poly_is_abelian_monoid: "abelian_monoid (univ_poly R)"
|
|
|
1393 |
unfolding univ_poly_def
|
|
|
1394 |
using poly_add_closed poly_add_zero zero_is_polynomial
|
|
|
1395 |
proof (auto simp add: abelian_monoid_def comm_monoid_def monoid_def comm_monoid_axioms_def)
|
|
|
1396 |
fix p1 p2 p3
|
|
|
1397 |
let ?c = "\<lambda>p. coeff p"
|
|
|
1398 |
assume A: "polynomial R p1" "polynomial R p2" "polynomial R p3"
|
|
|
1399 |
hence
|
|
|
1400 |
p1: "\<And>i. (?c p1) i \<in> carrier R" "set p1 \<subseteq> carrier R" and
|
|
|
1401 |
p2: "\<And>i. (?c p2) i \<in> carrier R" "set p2 \<subseteq> carrier R" and
|
|
|
1402 |
p3: "\<And>i. (?c p3) i \<in> carrier R" "set p3 \<subseteq> carrier R"
|
|
|
1403 |
using polynomial_in_carrier by auto
|
|
|
1404 |
have "?c (poly_add (poly_add p1 p2) p3) = (\<lambda>i. (?c p1 i \<oplus> ?c p2 i) \<oplus> (?c p3 i))"
|
|
|
1405 |
using poly_add_coeff[OF poly_add_in_carrier[OF p1(2) p2(2)] p3(2)]
|
|
|
1406 |
poly_add_coeff[OF p1(2) p2(2)] by simp
|
|
|
1407 |
also have " ... = (\<lambda>i. (?c p1 i) \<oplus> ((?c p2 i) \<oplus> (?c p3 i)))"
|
|
|
1408 |
using p1 p2 p3 add.m_assoc by simp
|
|
|
1409 |
also have " ... = ?c (poly_add p1 (poly_add p2 p3))"
|
|
|
1410 |
using poly_add_coeff[OF p1(2) poly_add_in_carrier[OF p2(2) p3(2)]]
|
|
|
1411 |
poly_add_coeff[OF p2(2) p3(2)] by simp
|
|
|
1412 |
finally have "?c (poly_add (poly_add p1 p2) p3) = ?c (poly_add p1 (poly_add p2 p3))" .
|
|
|
1413 |
thus "poly_add (poly_add p1 p2) p3 = poly_add p1 (poly_add p2 p3)"
|
|
|
1414 |
using coeff_iff_polynomial_cond poly_add_closed A by auto
|
|
|
1415 |
show "poly_add p1 p2 = poly_add p2 p1"
|
|
|
1416 |
using poly_add_comm[OF p1(2) p2(2)] .
|
|
|
1417 |
qed
|
|
|
1418 |
|
|
|
1419 |
lemma univ_poly_is_abelian_group: "abelian_group (univ_poly R)"
|
|
|
1420 |
proof -
|
|
|
1421 |
interpret abelian_monoid "univ_poly R"
|
|
|
1422 |
using univ_poly_is_abelian_monoid .
|
|
|
1423 |
show ?thesis
|
|
|
1424 |
proof (unfold_locales)
|
|
|
1425 |
show "carrier (add_monoid (univ_poly R)) \<subseteq> Units (add_monoid (univ_poly R))"
|
|
|
1426 |
unfolding univ_poly_def Units_def
|
|
|
1427 |
proof (auto)
|
|
|
1428 |
fix p assume p: "polynomial R p"
|
|
|
1429 |
have "polynomial R [ \<ominus> \<one> ]"
|
|
|
1430 |
unfolding polynomial_def using r_neg by fastforce
|
|
|
1431 |
hence cond0: "polynomial R (poly_mult [ \<ominus> \<one> ] p)"
|
|
|
1432 |
using poly_mult_closed[of "[ \<ominus> \<one> ]" p] p by simp
|
|
|
1433 |
|
|
|
1434 |
have "poly_add p (poly_mult [ \<ominus> \<one> ] p) = poly_add (poly_mult [ \<one> ] p) (poly_mult [ \<ominus> \<one> ] p)"
|
|
|
1435 |
using poly_mult_one[OF p] by simp
|
|
|
1436 |
also have " ... = poly_mult (poly_add [ \<one> ] [ \<ominus> \<one> ]) p"
|
|
|
1437 |
using poly_mult_l_distr' polynomial_in_carrier[OF p] by auto
|
|
|
1438 |
also have " ... = poly_mult [] p"
|
|
|
1439 |
using poly_add.simps[of "[ \<one> ]" "[ \<ominus> \<one> ]"]
|
|
|
1440 |
by (simp add: case_prod_unfold r_neg)
|
|
|
1441 |
also have " ... = []" by simp
|
|
|
1442 |
finally have cond1: "poly_add p (poly_mult [ \<ominus> \<one> ] p) = []" .
|
|
|
1443 |
|
|
|
1444 |
have "poly_add (poly_mult [ \<ominus> \<one> ] p) p = poly_add (poly_mult [ \<ominus> \<one> ] p) (poly_mult [ \<one> ] p)"
|
|
|
1445 |
using poly_mult_one[OF p] by simp
|
|
|
1446 |
also have " ... = poly_mult (poly_add [ \<ominus> \<one> ] [ \<one> ]) p"
|
|
|
1447 |
using poly_mult_l_distr' polynomial_in_carrier[OF p] by auto
|
|
|
1448 |
also have " ... = poly_mult [] p"
|
|
|
1449 |
using \<open>poly_mult (poly_add [\<one>] [\<ominus> \<one>]) p = poly_mult [] p\<close> poly_add_comm by auto
|
|
|
1450 |
also have " ... = []" by simp
|
|
|
1451 |
finally have cond2: "poly_add (poly_mult [ \<ominus> \<one> ] p) p = []" .
|
|
|
1452 |
|
|
|
1453 |
from cond0 cond1 cond2 show "\<exists>q. polynomial R q \<and> poly_add q p = [] \<and> poly_add p q = []"
|
|
|
1454 |
by auto
|
|
|
1455 |
qed
|
|
|
1456 |
qed
|
|
|
1457 |
qed
|
|
|
1458 |
|
|
|
1459 |
declare poly_add.simps[simp]
|
|
|
1460 |
|
|
|
1461 |
end
|
|
|
1462 |
|
|
|
1463 |
lemma univ_poly_is_ring:
|
|
|
1464 |
assumes "domain R"
|
|
|
1465 |
shows "ring (univ_poly R)"
|
|
|
1466 |
proof -
|
|
|
1467 |
interpret abelian_group "univ_poly R" + monoid "univ_poly R"
|
|
|
1468 |
using domain.univ_poly_is_abelian_group[OF assms] domain.univ_poly_is_monoid[OF assms] .
|
|
|
1469 |
have R: "ring R"
|
|
|
1470 |
using assms unfolding domain_def cring_def by simp
|
|
|
1471 |
show ?thesis
|
|
|
1472 |
apply unfold_locales
|
|
|
1473 |
apply (auto simp add: univ_poly_def assms domain.poly_mult_r_distr ring.poly_mult_l_distr[OF R])
|
|
|
1474 |
done
|
|
|
1475 |
qed
|
|
|
1476 |
|
|
|
1477 |
lemma univ_poly_is_cring:
|
|
|
1478 |
assumes "domain R"
|
|
|
1479 |
shows "cring (univ_poly R)"
|
|
|
1480 |
proof -
|
|
|
1481 |
interpret ring "univ_poly R"
|
|
|
1482 |
using univ_poly_is_ring[OF assms] by simp
|
|
|
1483 |
have "\<And>p q. \<lbrakk> p \<in> carrier (univ_poly R); q \<in> carrier (univ_poly R) \<rbrakk> \<Longrightarrow>
|
|
|
1484 |
p \<otimes>\<^bsub>univ_poly R\<^esub> q = q \<otimes>\<^bsub>univ_poly R\<^esub> p"
|
|
|
1485 |
unfolding univ_poly_def polynomial_def using domain.poly_mult_comm[OF assms] by auto
|
|
|
1486 |
thus ?thesis
|
|
|
1487 |
by unfold_locales auto
|
|
|
1488 |
qed
|
|
|
1489 |
|
|
|
1490 |
lemma univ_poly_is_domain:
|
|
|
1491 |
assumes "domain R"
|
|
|
1492 |
shows "domain (univ_poly R)"
|
|
|
1493 |
proof -
|
|
|
1494 |
interpret cring "univ_poly R"
|
|
|
1495 |
using univ_poly_is_cring[OF assms] by simp
|
|
|
1496 |
show ?thesis
|
|
|
1497 |
by unfold_locales
|
|
|
1498 |
(auto simp add: univ_poly_def domain.poly_mult_integral[OF assms])
|
|
|
1499 |
qed
|
|
|
1500 |
|
|
|
1501 |
|
|
|
1502 |
subsection \<open>Long Division Theorem\<close>
|
|
|
1503 |
|
|
|
1504 |
lemma (in domain) long_division_theorem:
|
|
|
1505 |
assumes "polynomial R p" "polynomial R b" and "b \<noteq> []" and "lead_coeff b \<in> Units R"
|
|
|
1506 |
shows "\<exists>q r. polynomial R q \<and> polynomial R r \<and>
|
|
|
1507 |
p = poly_add (poly_mult b q) r \<and> (r = [] \<or> degree r < degree b)"
|
|
|
1508 |
(is "\<exists>q r. ?long_division p q r")
|
|
|
1509 |
using assms
|
|
|
1510 |
proof (induct "length p" arbitrary: p rule: less_induct)
|
|
|
1511 |
case less thus ?case
|
|
|
1512 |
proof (cases p)
|
|
|
1513 |
case Nil
|
|
|
1514 |
hence "?long_division p [] []"
|
|
|
1515 |
using zero_is_polynomial poly_mult_zero[OF less(3)] by (simp add: degree_def)
|
|
|
1516 |
thus ?thesis by blast
|
|
|
1517 |
next
|
|
|
1518 |
case (Cons a p') thus ?thesis
|
|
|
1519 |
proof (cases "length b > length p")
|
|
|
1520 |
assume "length b > length p"
|
|
|
1521 |
hence "p = [] \<or> degree p < degree b" unfolding degree_def
|
|
|
1522 |
by (meson diff_less_mono length_0_conv less_one not_le)
|
|
|
1523 |
hence "?long_division p [] p"
|
|
|
1524 |
using poly_add_zero[OF less(2)] less(2) zero_is_polynomial
|
|
|
1525 |
poly_mult_zero[OF less(3)] by simp
|
|
|
1526 |
thus ?thesis by blast
|
|
|
1527 |
next
|
|
|
1528 |
interpret UP: cring "univ_poly R"
|
|
|
1529 |
using univ_poly_is_cring[OF is_domain] .
|
|
|
1530 |
|
|
|
1531 |
assume "\<not> length b > length p"
|
|
|
1532 |
hence len_ge: "length p \<ge> length b" by simp
|
|
|
1533 |
obtain c b' where b: "b = c # b'"
|
|
|
1534 |
using less(4) list.exhaust_sel by blast
|
|
|
1535 |
hence c: "c \<in> Units R" "c \<in> carrier R - { \<zero> }" and a: "a \<in> carrier R - { \<zero> }"
|
|
|
1536 |
using assms(4) less(2-3) Cons unfolding polynomial_def by auto
|
|
|
1537 |
hence "(\<ominus> a) \<in> carrier R - { \<zero> }"
|
|
|
1538 |
using r_neg by force
|
|
|
1539 |
hence in_carrier: "(\<ominus> a) \<otimes> inv c \<in> carrier R - { \<zero> }"
|
|
|
1540 |
using a c(2) Units_inv_closed[OF c(1)] Units_l_inv[OF c(1)]
|
|
|
1541 |
empty_iff insert_iff integral_iff m_closed
|
|
|
1542 |
by (metis Diff_iff zero_not_one)
|
|
|
1543 |
|
|
|
1544 |
let ?len = "length"
|
|
|
1545 |
define s where "s = poly_mult (monon ((\<ominus> a) \<otimes> inv c) (?len p - ?len b)) b"
|
|
|
1546 |
hence s_coeff: "lead_coeff s = (\<ominus> a)"
|
|
|
1547 |
using poly_mult_lead_coeff[OF monon_is_polynomial[OF in_carrier] less(3)] a c
|
|
|
1548 |
unfolding monon_def s_def b using m_assoc by force
|
|
|
1549 |
|
|
|
1550 |
have "degree s = degree (monon ((\<ominus> a) \<otimes> inv c) (?len p - ?len b)) + degree b"
|
|
|
1551 |
using poly_mult_degree_eq[OF monon_is_polynomial[OF in_carrier] less(3)]
|
|
|
1552 |
unfolding s_def b monon_def by auto
|
|
|
1553 |
hence "?len s - 1 = ?len p - 1"
|
|
|
1554 |
using len_ge unfolding b Cons by (simp add: monon_def degree_def)
|
|
|
1555 |
moreover have "s \<noteq> []"
|
|
|
1556 |
using poly_mult_integral[OF monon_is_polynomial[OF in_carrier] less(3)]
|
|
|
1557 |
unfolding s_def monon_def b by blast
|
|
|
1558 |
hence "?len s > 0" by simp
|
|
|
1559 |
ultimately have len_eq: "?len s = ?len p"
|
|
|
1560 |
by (simp add: Nitpick.size_list_simp(2) local.Cons)
|
|
|
1561 |
|
|
|
1562 |
obtain s' where s: "s = (\<ominus> a) # s'"
|
|
|
1563 |
using s_coeff len_eq by (metis \<open>s \<noteq> []\<close> hd_Cons_tl)
|
|
|
1564 |
|
|
|
1565 |
define p_diff where "p_diff = poly_add p s"
|
|
|
1566 |
hence "?len p_diff < ?len p"
|
|
|
1567 |
using len_eq s_coeff in_carrier a c unfolding s Cons apply simp
|
|
|
1568 |
by (metis le_imp_less_Suc length_map map_fst_zip normalize_length_le r_neg)
|
|
|
1569 |
moreover have "polynomial R p_diff" unfolding p_diff_def s_def
|
|
|
1570 |
using poly_mult_closed[OF monon_is_polynomial[OF in_carrier(1)] less(3)]
|
|
|
1571 |
poly_add_closed[OF less(2)] by simp
|
|
|
1572 |
ultimately
|
|
|
1573 |
obtain q' r' where l_div: "?long_division p_diff q' r'"
|
|
|
1574 |
using less(1)[of p_diff] less(3-5) by blast
|
|
|
1575 |
hence r': "polynomial R r'" and q': "polynomial R q'" by auto
|
|
|
1576 |
|
|
|
1577 |
obtain m where m: "polynomial R m" "s = poly_mult m b"
|
|
|
1578 |
using s_def monon_is_polynomial[OF in_carrier(1)] by auto
|
|
|
1579 |
have in_univ_carrier:
|
|
|
1580 |
"p \<in> carrier (univ_poly R)" "m \<in> carrier (univ_poly R)" "b \<in> carrier (univ_poly R)"
|
|
|
1581 |
"r' \<in> carrier (univ_poly R)" "q' \<in> carrier (univ_poly R)"
|
|
|
1582 |
using r' q' less(2-3) m(1) unfolding univ_poly_def by auto
|
|
|
1583 |
|
|
|
1584 |
hence "poly_add p (poly_mult m b) = poly_add (poly_mult b q') r'"
|
|
|
1585 |
using m l_div unfolding p_diff_def by simp
|
|
|
1586 |
hence "p \<oplus>\<^bsub>(univ_poly R)\<^esub> (m \<otimes>\<^bsub>(univ_poly R)\<^esub> b) = (b \<otimes>\<^bsub>(univ_poly R)\<^esub> q') \<oplus>\<^bsub>(univ_poly R)\<^esub> r'"
|
|
|
1587 |
unfolding univ_poly_def by auto
|
|
|
1588 |
hence
|
|
|
1589 |
"(p \<oplus>\<^bsub>(univ_poly R)\<^esub> (m \<otimes>\<^bsub>(univ_poly R)\<^esub> b)) \<ominus>\<^bsub>(univ_poly R)\<^esub> (m \<otimes>\<^bsub>(univ_poly R)\<^esub> b) =
|
|
|
1590 |
((b \<otimes>\<^bsub>(univ_poly R)\<^esub>q') \<oplus>\<^bsub>(univ_poly R)\<^esub> r') \<ominus>\<^bsub>(univ_poly R)\<^esub> (m \<otimes>\<^bsub>(univ_poly R)\<^esub> b)"
|
|
|
1591 |
by simp
|
|
|
1592 |
hence "p = (b \<otimes>\<^bsub>(univ_poly R)\<^esub> (q' \<ominus>\<^bsub>(univ_poly R)\<^esub> m)) \<oplus>\<^bsub>(univ_poly R)\<^esub> r'"
|
|
|
1593 |
using in_univ_carrier by algebra
|
|
|
1594 |
hence "p = poly_add (poly_mult b (q' \<ominus>\<^bsub>(univ_poly R)\<^esub> m)) r'"
|
|
|
1595 |
unfolding univ_poly_def by simp
|
|
|
1596 |
moreover have "q' \<ominus>\<^bsub>(univ_poly R)\<^esub> m \<in> carrier (univ_poly R)"
|
|
|
1597 |
using UP.ring_simprules in_univ_carrier by simp
|
|
|
1598 |
hence "polynomial R (q' \<ominus>\<^bsub>(univ_poly R)\<^esub> m)"
|
|
|
1599 |
unfolding univ_poly_def by simp
|
|
|
1600 |
ultimately have "?long_division p (q' \<ominus>\<^bsub>(univ_poly R)\<^esub> m) r'"
|
|
|
1601 |
using l_div r' by simp
|
|
|
1602 |
thus ?thesis by blast
|
|
|
1603 |
qed
|
|
|
1604 |
qed
|
|
|
1605 |
qed
|
|
|
1606 |
|
|
|
1607 |
lemma (in field) field_long_division_theorem:
|
|
|
1608 |
assumes "polynomial R p" "polynomial R b" and "b \<noteq> []"
|
|
|
1609 |
shows "\<exists>q r. polynomial R q \<and> polynomial R r \<and>
|
|
|
1610 |
p = poly_add (poly_mult b q) r \<and> (r = [] \<or> degree r < degree b)"
|
|
|
1611 |
using long_division_theorem[OF assms] assms lead_coeff_not_zero[of "hd b" "tl b"]
|
|
|
1612 |
by (simp add: field_Units)
|
|
|
1613 |
|
|
|
1614 |
lemma univ_poly_is_euclidean_domain:
|
|
|
1615 |
assumes "field R"
|
|
|
1616 |
shows "euclidean_domain (univ_poly R) degree"
|
|
|
1617 |
proof -
|
|
|
1618 |
interpret domain "univ_poly R"
|
|
|
1619 |
using univ_poly_is_domain assms field_def by blast
|
|
|
1620 |
show ?thesis
|
|
|
1621 |
apply (rule euclidean_domainI)
|
|
|
1622 |
unfolding univ_poly_def
|
|
|
1623 |
using field.field_long_division_theorem[OF assms] by auto
|
|
|
1624 |
qed
|
|
|
1625 |
|
|
|
1626 |
|
|
|
1627 |
subsection \<open>Consistency Rules\<close>
|
|
|
1628 |
|
|
|
1629 |
lemma (in ring) subring_is_ring: (* <- Move to Subrings.thy *)
|
|
|
1630 |
assumes "subring K R" shows "ring (R \<lparr> carrier := K \<rparr>)"
|
|
|
1631 |
using assms unfolding subring_iff[OF subringE(1)[OF assms]] .
|
|
|
1632 |
|
|
|
1633 |
lemma (in ring) eval_consistent [simp]:
|
|
|
1634 |
assumes "subring K R" shows "ring.eval (R \<lparr> carrier := K \<rparr>) = eval"
|
|
|
1635 |
proof
|
|
|
1636 |
fix p show "ring.eval (R \<lparr> carrier := K \<rparr>) p = eval p"
|
|
|
1637 |
using nat_pow_consistent ring.eval.simps[OF subring_is_ring[OF assms]] by (induct p) (auto)
|
|
|
1638 |
qed
|
|
|
1639 |
|
|
|
1640 |
lemma (in ring) coeff_consistent [simp]:
|
|
|
1641 |
assumes "subring K R" shows "ring.coeff (R \<lparr> carrier := K \<rparr>) = coeff"
|
|
|
1642 |
proof
|
|
|
1643 |
fix p show "ring.coeff (R \<lparr> carrier := K \<rparr>) p = coeff p"
|
|
|
1644 |
using ring.coeff.simps[OF subring_is_ring[OF assms]] by (induct p) (auto)
|
|
|
1645 |
qed
|
|
|
1646 |
|
|
|
1647 |
lemma (in ring) normalize_consistent [simp]:
|
|
|
1648 |
assumes "subring K R" shows "ring.normalize (R \<lparr> carrier := K \<rparr>) = normalize"
|
|
|
1649 |
proof
|
|
|
1650 |
fix p show "ring.normalize (R \<lparr> carrier := K \<rparr>) p = normalize p"
|
|
|
1651 |
using ring.normalize.simps[OF subring_is_ring[OF assms]] by (induct p) (auto)
|
|
|
1652 |
qed
|
|
|
1653 |
|
|
|
1654 |
lemma (in ring) poly_add_consistent [simp]:
|
|
|
1655 |
assumes "subring K R" shows "ring.poly_add (R \<lparr> carrier := K \<rparr>) = poly_add"
|
|
|
1656 |
proof -
|
|
|
1657 |
have "\<And>p q. ring.poly_add (R \<lparr> carrier := K \<rparr>) p q = poly_add p q"
|
|
|
1658 |
proof -
|
|
|
1659 |
fix p q show "ring.poly_add (R \<lparr> carrier := K \<rparr>) p q = poly_add p q"
|
|
|
1660 |
using ring.poly_add.simps[OF subring_is_ring[OF assms]] normalize_consistent[OF assms] by auto
|
|
|
1661 |
qed
|
|
|
1662 |
thus ?thesis by (auto simp del: poly_add.simps)
|
|
|
1663 |
qed
|
|
|
1664 |
|
|
|
1665 |
lemma (in ring) poly_mult_consistent [simp]:
|
|
|
1666 |
assumes "subring K R" shows "ring.poly_mult (R \<lparr> carrier := K \<rparr>) = poly_mult"
|
|
|
1667 |
proof -
|
|
|
1668 |
have "\<And>p q. ring.poly_mult (R \<lparr> carrier := K \<rparr>) p q = poly_mult p q"
|
|
|
1669 |
proof -
|
|
|
1670 |
fix p q show "ring.poly_mult (R \<lparr> carrier := K \<rparr>) p q = poly_mult p q"
|
|
|
1671 |
using ring.poly_mult.simps[OF subring_is_ring[OF assms]] poly_add_consistent[OF assms]
|
|
|
1672 |
by (induct p) (auto)
|
|
|
1673 |
qed
|
|
|
1674 |
thus ?thesis by auto
|
|
|
1675 |
qed
|
|
|
1676 |
|
|
|
1677 |
lemma (in ring) univ_poly_carrier_change_def':
|
|
|
1678 |
assumes "subring K R"
|
|
|
1679 |
shows "univ_poly (R \<lparr> carrier := K \<rparr>) = (univ_poly R) \<lparr> carrier := { p. polynomial R p \<and> set p \<subseteq> K } \<rparr>"
|
|
|
1680 |
unfolding univ_poly_def polynomial_def
|
|
|
1681 |
using poly_add_consistent[OF assms]
|
|
|
1682 |
poly_mult_consistent[OF assms]
|
|
|
1683 |
subringE(1)[OF assms]
|
|
|
1684 |
by auto
|
|
|
1685 |
|
|
|
1686 |
|
|
|
1687 |
subsection \<open>The Evaluation Homomorphism\<close>
|
|
|
1688 |
|
|
|
1689 |
lemma (in ring) eval_replicate:
|
|
|
1690 |
assumes "set p \<subseteq> carrier R" "a \<in> carrier R"
|
|
|
1691 |
shows "eval ((replicate n \<zero>) @ p) a = eval p a"
|
|
|
1692 |
using assms eval_in_carrier by (induct n) (auto)
|
|
|
1693 |
|
|
|
1694 |
lemma (in ring) eval_normalize:
|
|
|
1695 |
assumes "set p \<subseteq> carrier R" "a \<in> carrier R"
|
|
|
1696 |
shows "eval (normalize p) a = eval p a"
|
|
|
1697 |
using eval_replicate[OF normalize_in_carrier] normalize_def'[of p] assms by metis
|
|
|
1698 |
|
|
|
1699 |
lemma (in ring) eval_poly_add_aux:
|
|
|
1700 |
assumes "set p \<subseteq> carrier R" "set q \<subseteq> carrier R" and "length p = length q" and "a \<in> carrier R"
|
|
|
1701 |
shows "eval (poly_add p q) a = (eval p a) \<oplus> (eval q a)"
|
|
|
1702 |
proof -
|
|
|
1703 |
have "eval (map2 (\<oplus>) p q) a = (eval p a) \<oplus> (eval q a)"
|
|
|
1704 |
using assms
|
|
|
1705 |
proof (induct p arbitrary: q)
|
|
|
1706 |
case Nil
|
|
|
1707 |
then show ?case by simp
|
|
|
1708 |
next
|
|
|
1709 |
case (Cons b1 p')
|
|
|
1710 |
then obtain b2 q' where q: "q = b2 # q'"
|
|
|
1711 |
by (metis length_Cons list.exhaust list.size(3) nat.simps(3))
|
|
|
1712 |
show ?case
|
|
|
1713 |
using eval_in_carrier[OF _ Cons(5), of q']
|
|
|
1714 |
eval_in_carrier[OF _ Cons(5), of p'] Cons unfolding q
|
|
|
1715 |
by (auto simp add: degree_def ring_simprules(7,13,22))
|
|
|
1716 |
qed
|
|
|
1717 |
moreover have "set (map2 (\<oplus>) p q) \<subseteq> carrier R"
|
|
|
1718 |
using assms(1-2)
|
|
|
1719 |
by (induct p arbitrary: q) (auto, metis add.m_closed in_set_zipE set_ConsD subsetCE)
|
|
|
1720 |
ultimately show ?thesis
|
|
|
1721 |
using assms(3) eval_normalize[OF _ assms(4), of "map2 (\<oplus>) p q"] by auto
|
|
|
1722 |
qed
|
|
|
1723 |
|
|
|
1724 |
lemma (in ring) eval_poly_add:
|
|
|
1725 |
assumes "set p \<subseteq> carrier R" "set q \<subseteq> carrier R" and "a \<in> carrier R"
|
|
|
1726 |
shows "eval (poly_add p q) a = (eval p a) \<oplus> (eval q a)"
|
|
|
1727 |
proof -
|
|
|
1728 |
{ fix p q assume A: "set p \<subseteq> carrier R" "set q \<subseteq> carrier R" "length p \<ge> length q"
|
|
|
1729 |
hence "eval (poly_add p ((replicate (length p - length q) \<zero>) @ q)) a =
|
|
|
1730 |
(eval p a) \<oplus> (eval ((replicate (length p - length q) \<zero>) @ q) a)"
|
|
|
1731 |
using eval_poly_add_aux[OF A(1) _ _ assms(3), of "(replicate (length p - length q) \<zero>) @ q"] by force
|
|
|
1732 |
hence "eval (poly_add p q) a = (eval p a) \<oplus> (eval q a)"
|
|
|
1733 |
using eval_replicate[OF A(2) assms(3)] A(3) by auto }
|
|
|
1734 |
note aux_lemma = this
|
|
|
1735 |
|
|
|
1736 |
have ?thesis if "length q \<ge> length p"
|
|
|
1737 |
using assms(1-2)[THEN eval_in_carrier[OF _ assms(3)]] poly_add_comm[OF assms(1-2)]
|
|
|
1738 |
aux_lemma[OF assms(2,1) that]
|
|
|
1739 |
by (auto simp del: poly_add.simps simp add: add.m_comm)
|
|
|
1740 |
moreover have ?thesis if "length p \<ge> length q"
|
|
|
1741 |
using aux_lemma[OF assms(1-2) that] .
|
|
|
1742 |
ultimately show ?thesis by auto
|
|
|
1743 |
qed
|
|
|
1744 |
|
|
|
1745 |
lemma (in ring) eval_append_aux:
|
|
|
1746 |
assumes "set p \<subseteq> carrier R" and "b \<in> carrier R" and "a \<in> carrier R"
|
|
|
1747 |
shows "eval (p @ [ b ]) a = ((eval p a) \<otimes> a) \<oplus> b"
|
|
|
1748 |
using assms(1)
|
|
|
1749 |
proof (induct p)
|
|
|
1750 |
case Nil thus ?case by (auto simp add: degree_def assms(2-3))
|
|
|
1751 |
next
|
|
|
1752 |
case (Cons l q)
|
|
|
1753 |
have "a [^] length q \<in> carrier R" "eval q a \<in> carrier R"
|
|
|
1754 |
using eval_in_carrier Cons(2) assms(2-3) by auto
|
|
|
1755 |
thus ?case
|
|
|
1756 |
using Cons assms(2-3) by (auto simp add: degree_def, algebra)
|
|
|
1757 |
qed
|
|
|
1758 |
|
|
|
1759 |
lemma (in ring) eval_append:
|
|
|
1760 |
assumes "set p \<subseteq> carrier R" "set q \<subseteq> carrier R" and "a \<in> carrier R"
|
|
|
1761 |
shows "eval (p @ q) a = ((eval p a) \<otimes> (a [^] (length q))) \<oplus> (eval q a)"
|
|
|
1762 |
using assms(2)
|
|
|
1763 |
proof (induct "length q" arbitrary: q)
|
|
|
1764 |
case 0 thus ?case
|
|
|
1765 |
using eval_in_carrier[OF assms(1,3)] by auto
|
|
|
1766 |
next
|
|
|
1767 |
case (Suc n)
|
|
|
1768 |
then obtain b q' where q: "q = q' @ [ b ]"
|
|
|
1769 |
by (metis length_Suc_conv list.simps(3) rev_exhaust)
|
|
|
1770 |
hence in_carrier: "eval p a \<in> carrier R" "eval q' a \<in> carrier R"
|
|
|
1771 |
"a [^] (length q') \<in> carrier R" "b \<in> carrier R"
|
|
|
1772 |
using assms(1,3) Suc(3) eval_in_carrier[OF _ assms(3)] by auto
|
|
|
1773 |
|
|
|
1774 |
have "eval (p @ q) a = ((eval (p @ q') a) \<otimes> a) \<oplus> b"
|
|
|
1775 |
using eval_append_aux[OF _ _ assms(3), of "p @ q'" b] assms(1) Suc(3) unfolding q by auto
|
|
|
1776 |
also have " ... = ((((eval p a) \<otimes> (a [^] (length q'))) \<oplus> (eval q' a)) \<otimes> a) \<oplus> b"
|
|
|
1777 |
using Suc unfolding q by auto
|
|
|
1778 |
also have " ... = (((eval p a) \<otimes> ((a [^] (length q')) \<otimes> a))) \<oplus> (((eval q' a) \<otimes> a) \<oplus> b)"
|
|
|
1779 |
using assms(3) in_carrier by algebra
|
|
|
1780 |
also have " ... = (eval p a) \<otimes> (a [^] (length q)) \<oplus> (eval q a)"
|
|
|
1781 |
using eval_append_aux[OF _ in_carrier(4) assms(3), of q'] Suc(3) unfolding q by auto
|
|
|
1782 |
finally show ?case .
|
|
|
1783 |
qed
|
|
|
1784 |
|
|
|
1785 |
lemma (in ring) eval_monon:
|
|
|
1786 |
assumes "b \<in> carrier R" and "a \<in> carrier R"
|
|
|
1787 |
shows "eval (monon b n) a = b \<otimes> (a [^] n)"
|
|
|
1788 |
proof (induct n)
|
|
|
1789 |
case 0 thus ?case
|
|
|
1790 |
using assms unfolding monon_def by (auto simp add: degree_def)
|
|
|
1791 |
next
|
|
|
1792 |
case (Suc n)
|
|
|
1793 |
have "monon b (Suc n) = (monon b n) @ [ \<zero> ]"
|
|
|
1794 |
unfolding monon_def by (simp add: replicate_append_same)
|
|
|
1795 |
hence "eval (monon b (Suc n)) a = ((eval (monon b n) a) \<otimes> a) \<oplus> \<zero>"
|
|
|
1796 |
using eval_append_aux[OF monon_in_carrier[OF assms(1)] zero_closed assms(2), of n] by simp
|
|
|
1797 |
also have " ... = b \<otimes> (a [^] (Suc n))"
|
|
|
1798 |
using Suc assms m_assoc by auto
|
|
|
1799 |
finally show ?case .
|
|
|
1800 |
qed
|
|
|
1801 |
|
|
|
1802 |
lemma (in cring) eval_poly_mult:
|
|
|
1803 |
assumes "set p \<subseteq> carrier R" "set q \<subseteq> carrier R" and "a \<in> carrier R"
|
|
|
1804 |
shows "eval (poly_mult p q) a = (eval p a) \<otimes> (eval q a)"
|
|
|
1805 |
using assms(1)
|
|
|
1806 |
proof (induct p)
|
|
|
1807 |
case Nil thus ?case
|
|
|
1808 |
using eval_in_carrier[OF assms(2-3)] by simp
|
|
|
1809 |
next
|
|
|
1810 |
{ fix n b assume b: "b \<in> carrier R"
|
|
|
1811 |
hence "set (map ((\<otimes>) b) q) \<subseteq> carrier R" and "set (replicate n \<zero>) \<subseteq> carrier R"
|
|
|
1812 |
using assms(2) by (induct q) (auto)
|
|
|
1813 |
hence "eval ((map ((\<otimes>) b) q) @ (replicate n \<zero>)) a = (eval ((map ((\<otimes>) b) q)) a) \<otimes> (a [^] n) \<oplus> \<zero>"
|
|
|
1814 |
using eval_append[OF _ _ assms(3), of "map ((\<otimes>) b) q" "replicate n \<zero>"]
|
|
|
1815 |
eval_replicate[OF _ assms(3), of "[]"] by auto
|
|
|
1816 |
moreover have "eval (map ((\<otimes>) b) q) a = b \<otimes> eval q a"
|
|
|
1817 |
using assms(2-3) eval_in_carrier b by(induct q) (auto simp add: degree_def m_assoc r_distr)
|
|
|
1818 |
ultimately have "eval ((map ((\<otimes>) b) q) @ (replicate n \<zero>)) a = (b \<otimes> eval q a) \<otimes> (a [^] n) \<oplus> \<zero>"
|
|
|
1819 |
by simp
|
|
|
1820 |
also have " ... = (b \<otimes> (a [^] n)) \<otimes> (eval q a)"
|
|
|
1821 |
using eval_in_carrier[OF assms(2-3)] b assms(3) m_assoc m_comm by auto
|
|
|
1822 |
finally have "eval ((map ((\<otimes>) b) q) @ (replicate n \<zero>)) a = (eval (monon b n) a) \<otimes> (eval q a)"
|
|
|
1823 |
using eval_monon[OF b assms(3)] by simp }
|
|
|
1824 |
note aux_lemma = this
|
|
|
1825 |
|
|
|
1826 |
case (Cons b p)
|
|
|
1827 |
hence in_carrier:
|
|
|
1828 |
"eval (monon b (length p)) a \<in> carrier R" "eval p a \<in> carrier R" "eval q a \<in> carrier R" "b \<in> carrier R"
|
|
|
1829 |
using eval_in_carrier monon_in_carrier assms by auto
|
|
|
1830 |
have set_map: "set ((map ((\<otimes>) b) q) @ (replicate (length p) \<zero>)) \<subseteq> carrier R"
|
|
|
1831 |
using in_carrier(4) assms(2) by (induct q) (auto)
|
|
|
1832 |
have set_poly: "set (poly_mult p q) \<subseteq> carrier R"
|
|
|
1833 |
using poly_mult_in_carrier[OF _ assms(2), of p] Cons(2) by auto
|
|
|
1834 |
have "eval (poly_mult (b # p) q) a =
|
|
|
1835 |
((eval (monon b (length p)) a) \<otimes> (eval q a)) \<oplus> ((eval p a) \<otimes> (eval q a))"
|
|
|
1836 |
using eval_poly_add[OF set_map set_poly assms(3)] aux_lemma[OF in_carrier(4), of "length p"] Cons
|
|
|
1837 |
by (auto simp del: poly_add.simps simp add: degree_def)
|
|
|
1838 |
also have " ... = ((eval (monon b (length p)) a) \<oplus> (eval p a)) \<otimes> (eval q a)"
|
|
|
1839 |
using l_distr[OF in_carrier(1-3)] by simp
|
|
|
1840 |
also have " ... = (eval (b # p) a) \<otimes> (eval q a)"
|
|
|
1841 |
unfolding eval_monon[OF in_carrier(4) assms(3), of "length p"] by (auto simp add: degree_def)
|
|
|
1842 |
finally show ?case .
|
|
|
1843 |
qed
|
|
|
1844 |
|
|
|
1845 |
proposition (in cring) eval_is_hom:
|
|
|
1846 |
assumes "subring K R" and "a \<in> carrier R"
|
|
|
1847 |
shows "(\<lambda>p. (eval p) a) \<in> ring_hom (univ_poly (R \<lparr> carrier := K \<rparr>)) R"
|
|
|
1848 |
unfolding univ_poly_carrier_change_def'[OF assms(1)]
|
|
|
1849 |
using polynomial_in_carrier eval_in_carrier eval_poly_add eval_poly_mult assms(2)
|
|
|
1850 |
by (auto intro!: ring_hom_memI
|
|
|
1851 |
simp add: univ_poly_def degree_def
|
|
|
1852 |
simp del: poly_add.simps poly_mult.simps)
|
|
|
1853 |
|
|
|
1854 |
|
|
|
1855 |
end |