author | haftmann |
Mon, 17 Apr 2017 16:39:01 +0200 | |
changeset 65486 | d801126a14cb |
parent 65435 | 378175f44328 |
child 66480 | 4b8d1df8933b |
permissions | -rw-r--r-- |
65435 | 1 |
(* Title: HOL/Computational_Algebra/Polynomial_FPS.thy |
65366 | 2 |
Author: Manuel Eberl, TU München |
63317 | 3 |
*) |
4 |
||
5 |
section \<open>Converting polynomials to formal power series\<close> |
|
65366 | 6 |
|
63317 | 7 |
theory Polynomial_FPS |
65366 | 8 |
imports Polynomial Formal_Power_Series |
63317 | 9 |
begin |
10 |
||
63319 | 11 |
definition fps_of_poly where |
12 |
"fps_of_poly p = Abs_fps (coeff p)" |
|
63317 | 13 |
|
63319 | 14 |
lemma fps_of_poly_eq_iff: "fps_of_poly p = fps_of_poly q \<longleftrightarrow> p = q" |
15 |
by (simp add: fps_of_poly_def poly_eq_iff fps_eq_iff) |
|
63317 | 16 |
|
63319 | 17 |
lemma fps_of_poly_nth [simp]: "fps_of_poly p $ n = coeff p n" |
18 |
by (simp add: fps_of_poly_def) |
|
63317 | 19 |
|
63319 | 20 |
lemma fps_of_poly_const: "fps_of_poly [:c:] = fps_const c" |
63317 | 21 |
proof (subst fps_eq_iff, clarify) |
63319 | 22 |
fix n :: nat show "fps_of_poly [:c:] $ n = fps_const c $ n" |
23 |
by (cases n) (auto simp: fps_of_poly_def) |
|
63317 | 24 |
qed |
25 |
||
63319 | 26 |
lemma fps_of_poly_0 [simp]: "fps_of_poly 0 = 0" |
27 |
by (subst fps_const_0_eq_0 [symmetric], subst fps_of_poly_const [symmetric]) simp |
|
63317 | 28 |
|
63319 | 29 |
lemma fps_of_poly_1 [simp]: "fps_of_poly 1 = 1" |
65486 | 30 |
by (simp add: fps_eq_iff) |
63317 | 31 |
|
63319 | 32 |
lemma fps_of_poly_1' [simp]: "fps_of_poly [:1:] = 1" |
33 |
by (subst fps_const_1_eq_1 [symmetric], subst fps_of_poly_const [symmetric]) |
|
63317 | 34 |
(simp add: one_poly_def) |
35 |
||
63319 | 36 |
lemma fps_of_poly_numeral [simp]: "fps_of_poly (numeral n) = numeral n" |
37 |
by (simp add: numeral_fps_const fps_of_poly_const [symmetric] numeral_poly) |
|
63317 | 38 |
|
63319 | 39 |
lemma fps_of_poly_numeral' [simp]: "fps_of_poly [:numeral n:] = numeral n" |
40 |
by (simp add: numeral_fps_const fps_of_poly_const [symmetric] numeral_poly) |
|
63317 | 41 |
|
63319 | 42 |
lemma fps_of_poly_X [simp]: "fps_of_poly [:0, 1:] = X" |
43 |
by (auto simp add: fps_of_poly_def fps_eq_iff coeff_pCons split: nat.split) |
|
63317 | 44 |
|
63319 | 45 |
lemma fps_of_poly_add: "fps_of_poly (p + q) = fps_of_poly p + fps_of_poly q" |
46 |
by (simp add: fps_of_poly_def plus_poly.rep_eq fps_plus_def) |
|
63317 | 47 |
|
63319 | 48 |
lemma fps_of_poly_diff: "fps_of_poly (p - q) = fps_of_poly p - fps_of_poly q" |
49 |
by (simp add: fps_of_poly_def minus_poly.rep_eq fps_minus_def) |
|
63317 | 50 |
|
63319 | 51 |
lemma fps_of_poly_uminus: "fps_of_poly (-p) = -fps_of_poly p" |
52 |
by (simp add: fps_of_poly_def uminus_poly.rep_eq fps_uminus_def) |
|
63317 | 53 |
|
63319 | 54 |
lemma fps_of_poly_mult: "fps_of_poly (p * q) = fps_of_poly p * fps_of_poly q" |
55 |
by (simp add: fps_of_poly_def fps_times_def fps_eq_iff coeff_mult atLeast0AtMost) |
|
63317 | 56 |
|
63319 | 57 |
lemma fps_of_poly_smult: |
58 |
"fps_of_poly (smult c p) = fps_const c * fps_of_poly p" |
|
59 |
using fps_of_poly_mult[of "[:c:]" p] by (simp add: fps_of_poly_mult fps_of_poly_const) |
|
63317 | 60 |
|
64267 | 61 |
lemma fps_of_poly_sum: "fps_of_poly (sum f A) = sum (\<lambda>x. fps_of_poly (f x)) A" |
63319 | 62 |
by (cases "finite A", induction rule: finite_induct) (simp_all add: fps_of_poly_add) |
63317 | 63 |
|
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63539
diff
changeset
|
64 |
lemma fps_of_poly_sum_list: "fps_of_poly (sum_list xs) = sum_list (map fps_of_poly xs)" |
63319 | 65 |
by (induction xs) (simp_all add: fps_of_poly_add) |
63317 | 66 |
|
64272 | 67 |
lemma fps_of_poly_prod: "fps_of_poly (prod f A) = prod (\<lambda>x. fps_of_poly (f x)) A" |
63319 | 68 |
by (cases "finite A", induction rule: finite_induct) (simp_all add: fps_of_poly_mult) |
63317 | 69 |
|
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63539
diff
changeset
|
70 |
lemma fps_of_poly_prod_list: "fps_of_poly (prod_list xs) = prod_list (map fps_of_poly xs)" |
63319 | 71 |
by (induction xs) (simp_all add: fps_of_poly_mult) |
63317 | 72 |
|
63319 | 73 |
lemma fps_of_poly_pCons: |
74 |
"fps_of_poly (pCons (c :: 'a :: semiring_1) p) = fps_const c + fps_of_poly p * X" |
|
63317 | 75 |
by (subst fps_mult_X_commute [symmetric], intro fps_ext) |
63319 | 76 |
(auto simp: fps_of_poly_def coeff_pCons split: nat.split) |
63317 | 77 |
|
63319 | 78 |
lemma fps_of_poly_pderiv: "fps_of_poly (pderiv p) = fps_deriv (fps_of_poly p)" |
79 |
by (intro fps_ext) (simp add: fps_of_poly_nth coeff_pderiv) |
|
63317 | 80 |
|
63319 | 81 |
lemma fps_of_poly_power: "fps_of_poly (p ^ n) = fps_of_poly p ^ n" |
82 |
by (induction n) (simp_all add: fps_of_poly_mult) |
|
63317 | 83 |
|
63319 | 84 |
lemma fps_of_poly_monom: "fps_of_poly (monom (c :: 'a :: comm_ring_1) n) = fps_const c * X ^ n" |
63317 | 85 |
by (intro fps_ext) simp_all |
86 |
||
63319 | 87 |
lemma fps_of_poly_monom': "fps_of_poly (monom (1 :: 'a :: comm_ring_1) n) = X ^ n" |
88 |
by (simp add: fps_of_poly_monom) |
|
63317 | 89 |
|
63319 | 90 |
lemma fps_of_poly_div: |
63317 | 91 |
assumes "(q :: 'a :: field poly) dvd p" |
63319 | 92 |
shows "fps_of_poly (p div q) = fps_of_poly p / fps_of_poly q" |
63317 | 93 |
proof (cases "q = 0") |
94 |
case False |
|
63319 | 95 |
from False fps_of_poly_eq_iff[of q 0] have nz: "fps_of_poly q \<noteq> 0" by simp |
63317 | 96 |
from assms have "p = (p div q) * q" by simp |
63319 | 97 |
also have "fps_of_poly \<dots> = fps_of_poly (p div q) * fps_of_poly q" |
98 |
by (simp add: fps_of_poly_mult) |
|
99 |
also from nz have "\<dots> / fps_of_poly q = fps_of_poly (p div q)" |
|
64240 | 100 |
by (intro nonzero_mult_div_cancel_right) (auto simp: fps_of_poly_0) |
63317 | 101 |
finally show ?thesis .. |
102 |
qed simp |
|
103 |
||
63319 | 104 |
lemma fps_of_poly_divide_numeral: |
105 |
"fps_of_poly (smult (inverse (numeral c :: 'a :: field)) p) = fps_of_poly p / numeral c" |
|
63317 | 106 |
proof - |
107 |
have "smult (inverse (numeral c)) p = [:inverse (numeral c):] * p" by simp |
|
63319 | 108 |
also have "fps_of_poly \<dots> = fps_of_poly p / numeral c" |
109 |
by (subst fps_of_poly_mult) (simp add: numeral_fps_const fps_of_poly_pCons) |
|
63317 | 110 |
finally show ?thesis by simp |
111 |
qed |
|
112 |
||
113 |
||
63319 | 114 |
lemma subdegree_fps_of_poly: |
63317 | 115 |
assumes "p \<noteq> 0" |
116 |
defines "n \<equiv> Polynomial.order 0 p" |
|
63319 | 117 |
shows "subdegree (fps_of_poly p) = n" |
63317 | 118 |
proof (rule subdegreeI) |
119 |
from assms have "monom 1 n dvd p" by (simp add: monom_1_dvd_iff) |
|
63319 | 120 |
thus zero: "fps_of_poly p $ i = 0" if "i < n" for i |
63317 | 121 |
using that by (simp add: monom_1_dvd_iff') |
122 |
||
123 |
from assms have "\<not>monom 1 (Suc n) dvd p" |
|
124 |
by (auto simp: monom_1_dvd_iff simp del: power_Suc) |
|
63539 | 125 |
then obtain k where k: "k \<le> n" "fps_of_poly p $ k \<noteq> 0" |
63317 | 126 |
by (auto simp: monom_1_dvd_iff' less_Suc_eq_le) |
63539 | 127 |
with zero[of k] have "k = n" by linarith |
128 |
with k show "fps_of_poly p $ n \<noteq> 0" by simp |
|
63317 | 129 |
qed |
130 |
||
63319 | 131 |
lemma fps_of_poly_dvd: |
63317 | 132 |
assumes "p dvd q" |
63319 | 133 |
shows "fps_of_poly (p :: 'a :: field poly) dvd fps_of_poly q" |
63317 | 134 |
proof (cases "p = 0 \<or> q = 0") |
135 |
case False |
|
63319 | 136 |
with assms fps_of_poly_eq_iff[of p 0] fps_of_poly_eq_iff[of q 0] show ?thesis |
137 |
by (auto simp: fps_dvd_iff subdegree_fps_of_poly dvd_imp_order_le) |
|
63317 | 138 |
qed (insert assms, auto) |
139 |
||
140 |
||
63319 | 141 |
lemmas fps_of_poly_simps = |
142 |
fps_of_poly_0 fps_of_poly_1 fps_of_poly_numeral fps_of_poly_const fps_of_poly_X |
|
143 |
fps_of_poly_add fps_of_poly_diff fps_of_poly_uminus fps_of_poly_mult fps_of_poly_smult |
|
64272 | 144 |
fps_of_poly_sum fps_of_poly_sum_list fps_of_poly_prod fps_of_poly_prod_list |
63319 | 145 |
fps_of_poly_pCons fps_of_poly_pderiv fps_of_poly_power fps_of_poly_monom |
146 |
fps_of_poly_divide_numeral |
|
63317 | 147 |
|
63319 | 148 |
lemma fps_of_poly_pcompose: |
63317 | 149 |
assumes "coeff q 0 = (0 :: 'a :: idom)" |
63319 | 150 |
shows "fps_of_poly (pcompose p q) = fps_compose (fps_of_poly p) (fps_of_poly q)" |
63317 | 151 |
using assms by (induction p rule: pCons_induct) |
63319 | 152 |
(auto simp: pcompose_pCons fps_of_poly_simps fps_of_poly_pCons |
63317 | 153 |
fps_compose_add_distrib fps_compose_mult_distrib) |
154 |
||
155 |
lemmas reify_fps_atom = |
|
63319 | 156 |
fps_of_poly_0 fps_of_poly_1' fps_of_poly_numeral' fps_of_poly_const fps_of_poly_X |
63317 | 157 |
|
158 |
||
159 |
text \<open> |
|
160 |
The following simproc can reduce the equality of two polynomial FPSs two equality of the |
|
161 |
respective polynomials. A polynomial FPS is one that only has finitely many non-zero |
|
63319 | 162 |
coefficients and can therefore be written as @{term "fps_of_poly p"} for some |
64911 | 163 |
polynomial \<open>p\<close>. |
63317 | 164 |
|
165 |
This may sound trivial, but it covers a number of annoying side conditions like |
|
166 |
@{term "1 + X \<noteq> 0"} that would otherwise not be solved automatically. |
|
167 |
\<close> |
|
168 |
||
169 |
ML \<open> |
|
170 |
||
171 |
(* TODO: Support for division *) |
|
172 |
signature POLY_FPS = sig |
|
173 |
||
174 |
val reify_conv : conv |
|
175 |
val eq_conv : conv |
|
176 |
val eq_simproc : cterm -> thm option |
|
177 |
||
178 |
end |
|
179 |
||
180 |
||
181 |
structure Poly_Fps = struct |
|
182 |
||
183 |
fun const_binop_conv s conv ct = |
|
184 |
case Thm.term_of ct of |
|
185 |
(Const (s', _) $ _ $ _) => |
|
186 |
if s = s' then |
|
187 |
Conv.binop_conv conv ct |
|
188 |
else |
|
189 |
raise CTERM ("const_binop_conv", [ct]) |
|
190 |
| _ => raise CTERM ("const_binop_conv", [ct]) |
|
191 |
||
192 |
fun reify_conv ct = |
|
193 |
let |
|
194 |
val rewr = Conv.rewrs_conv o map (fn thm => thm RS @{thm eq_reflection}) |
|
195 |
val un = Conv.arg_conv reify_conv |
|
196 |
val bin = Conv.binop_conv reify_conv |
|
197 |
in |
|
198 |
case Thm.term_of ct of |
|
63319 | 199 |
(Const (@{const_name "fps_of_poly"}, _) $ _) => ct |> Conv.all_conv |
63317 | 200 |
| (Const (@{const_name "Groups.plus"}, _) $ _ $ _) => ct |> ( |
63319 | 201 |
bin then_conv rewr @{thms fps_of_poly_add [symmetric]}) |
63317 | 202 |
| (Const (@{const_name "Groups.uminus"}, _) $ _) => ct |> ( |
63319 | 203 |
un then_conv rewr @{thms fps_of_poly_uminus [symmetric]}) |
63317 | 204 |
| (Const (@{const_name "Groups.minus"}, _) $ _ $ _) => ct |> ( |
63319 | 205 |
bin then_conv rewr @{thms fps_of_poly_diff [symmetric]}) |
63317 | 206 |
| (Const (@{const_name "Groups.times"}, _) $ _ $ _) => ct |> ( |
63319 | 207 |
bin then_conv rewr @{thms fps_of_poly_mult [symmetric]}) |
63317 | 208 |
| (Const (@{const_name "Rings.divide"}, _) $ _ $ (Const (@{const_name "Num.numeral"}, _) $ _)) |
209 |
=> ct |> (Conv.fun_conv (Conv.arg_conv reify_conv) |
|
63319 | 210 |
then_conv rewr @{thms fps_of_poly_divide_numeral [symmetric]}) |
63317 | 211 |
| (Const (@{const_name "Power.power"}, _) $ Const (@{const_name "X"},_) $ _) => ct |> ( |
63319 | 212 |
rewr @{thms fps_of_poly_monom' [symmetric]}) |
63317 | 213 |
| (Const (@{const_name "Power.power"}, _) $ _ $ _) => ct |> ( |
214 |
Conv.fun_conv (Conv.arg_conv reify_conv) |
|
63319 | 215 |
then_conv rewr @{thms fps_of_poly_power [symmetric]}) |
63317 | 216 |
| _ => ct |> ( |
217 |
rewr @{thms reify_fps_atom [symmetric]}) |
|
218 |
end |
|
219 |
||
220 |
||
221 |
fun eq_conv ct = |
|
222 |
case Thm.term_of ct of |
|
223 |
(Const (@{const_name "HOL.eq"}, _) $ _ $ _) => ct |> ( |
|
224 |
Conv.binop_conv reify_conv |
|
63319 | 225 |
then_conv Conv.rewr_conv @{thm fps_of_poly_eq_iff[THEN eq_reflection]}) |
63317 | 226 |
| _ => raise CTERM ("poly_fps_eq_conv", [ct]) |
227 |
||
228 |
val eq_simproc = try eq_conv |
|
229 |
||
230 |
end |
|
231 |
\<close> |
|
232 |
||
233 |
simproc_setup poly_fps_eq ("(f :: 'a fps) = g") = \<open>K (K Poly_Fps.eq_simproc)\<close> |
|
234 |
||
63319 | 235 |
lemma fps_of_poly_linear: "fps_of_poly [:a,1 :: 'a :: field:] = X + fps_const a" |
63317 | 236 |
by simp |
237 |
||
63319 | 238 |
lemma fps_of_poly_linear': "fps_of_poly [:1,a :: 'a :: field:] = 1 + fps_const a * X" |
63317 | 239 |
by simp |
240 |
||
63319 | 241 |
lemma fps_of_poly_cutoff [simp]: |
242 |
"fps_of_poly (poly_cutoff n p) = fps_cutoff n (fps_of_poly p)" |
|
63317 | 243 |
by (simp add: fps_eq_iff coeff_poly_cutoff) |
244 |
||
63319 | 245 |
lemma fps_of_poly_shift [simp]: "fps_of_poly (poly_shift n p) = fps_shift n (fps_of_poly p)" |
63317 | 246 |
by (simp add: fps_eq_iff coeff_poly_shift) |
247 |
||
248 |
||
249 |
definition poly_subdegree :: "'a::zero poly \<Rightarrow> nat" where |
|
63319 | 250 |
"poly_subdegree p = subdegree (fps_of_poly p)" |
63317 | 251 |
|
252 |
lemma coeff_less_poly_subdegree: |
|
253 |
"k < poly_subdegree p \<Longrightarrow> coeff p k = 0" |
|
63319 | 254 |
unfolding poly_subdegree_def using nth_less_subdegree_zero[of k "fps_of_poly p"] by simp |
63317 | 255 |
|
256 |
(* TODO: Move ? *) |
|
257 |
definition prefix_length :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> nat" where |
|
258 |
"prefix_length P xs = length (takeWhile P xs)" |
|
259 |
||
260 |
primrec prefix_length_aux :: "('a \<Rightarrow> bool) \<Rightarrow> nat \<Rightarrow> 'a list \<Rightarrow> nat" where |
|
261 |
"prefix_length_aux P acc [] = acc" |
|
262 |
| "prefix_length_aux P acc (x#xs) = (if P x then prefix_length_aux P (Suc acc) xs else acc)" |
|
263 |
||
264 |
lemma prefix_length_aux_correct: "prefix_length_aux P acc xs = prefix_length P xs + acc" |
|
265 |
by (induction xs arbitrary: acc) (simp_all add: prefix_length_def) |
|
266 |
||
267 |
lemma prefix_length_code [code]: "prefix_length P xs = prefix_length_aux P 0 xs" |
|
268 |
by (simp add: prefix_length_aux_correct) |
|
269 |
||
270 |
lemma prefix_length_le_length: "prefix_length P xs \<le> length xs" |
|
271 |
by (induction xs) (simp_all add: prefix_length_def) |
|
272 |
||
273 |
lemma prefix_length_less_length: "(\<exists>x\<in>set xs. \<not>P x) \<Longrightarrow> prefix_length P xs < length xs" |
|
274 |
by (induction xs) (simp_all add: prefix_length_def) |
|
275 |
||
276 |
lemma nth_prefix_length: |
|
277 |
"(\<exists>x\<in>set xs. \<not>P x) \<Longrightarrow> \<not>P (xs ! prefix_length P xs)" |
|
278 |
by (induction xs) (simp_all add: prefix_length_def) |
|
279 |
||
280 |
lemma nth_less_prefix_length: |
|
281 |
"n < prefix_length P xs \<Longrightarrow> P (xs ! n)" |
|
282 |
by (induction xs arbitrary: n) |
|
283 |
(auto simp: prefix_length_def nth_Cons split: if_splits nat.splits) |
|
284 |
(* END TODO *) |
|
285 |
||
286 |
lemma poly_subdegree_code [code]: "poly_subdegree p = prefix_length (op = 0) (coeffs p)" |
|
287 |
proof (cases "p = 0") |
|
288 |
case False |
|
289 |
note [simp] = this |
|
290 |
define n where "n = prefix_length (op = 0) (coeffs p)" |
|
291 |
from False have "\<exists>k. coeff p k \<noteq> 0" by (auto simp: poly_eq_iff) |
|
292 |
hence ex: "\<exists>x\<in>set (coeffs p). x \<noteq> 0" by (auto simp: coeffs_def) |
|
293 |
hence n_less: "n < length (coeffs p)" and nonzero: "coeffs p ! n \<noteq> 0" |
|
294 |
unfolding n_def by (auto intro!: prefix_length_less_length nth_prefix_length) |
|
295 |
show ?thesis unfolding poly_subdegree_def |
|
296 |
proof (intro subdegreeI) |
|
63319 | 297 |
from n_less have "fps_of_poly p $ n = coeffs p ! n" |
63317 | 298 |
by (subst coeffs_nth) (simp_all add: degree_eq_length_coeffs) |
63319 | 299 |
with nonzero show "fps_of_poly p $ prefix_length (op = 0) (coeffs p) \<noteq> 0" |
63317 | 300 |
unfolding n_def by simp |
301 |
next |
|
302 |
fix k assume A: "k < prefix_length (op = 0) (coeffs p)" |
|
303 |
also have "\<dots> \<le> length (coeffs p)" by (rule prefix_length_le_length) |
|
63319 | 304 |
finally show "fps_of_poly p $ k = 0" |
63317 | 305 |
using nth_less_prefix_length[OF A] |
306 |
by (simp add: coeffs_nth degree_eq_length_coeffs) |
|
307 |
qed |
|
308 |
qed (simp_all add: poly_subdegree_def prefix_length_def) |
|
309 |
||
310 |
end |