move Lubs from HOL to HOL-Library (replaced by conditionally complete lattices)
1 (* Title: HOL/Library/Formal_Power_Series.thy
2 Author: Amine Chaieb, University of Cambridge
5 header{* A formalization of formal power series *}
7 theory Formal_Power_Series
12 subsection {* The type of formal power series*}
14 typedef 'a fps = "{f :: nat \<Rightarrow> 'a. True}"
15 morphisms fps_nth Abs_fps
18 notation fps_nth (infixl "$" 75)
20 lemma expand_fps_eq: "p = q \<longleftrightarrow> (\<forall>n. p $ n = q $ n)"
21 by (simp add: fps_nth_inject [symmetric] fun_eq_iff)
23 lemma fps_ext: "(\<And>n. p $ n = q $ n) \<Longrightarrow> p = q"
24 by (simp add: expand_fps_eq)
26 lemma fps_nth_Abs_fps [simp]: "Abs_fps f $ n = f n"
27 by (simp add: Abs_fps_inverse)
29 text{* Definition of the basic elements 0 and 1 and the basic operations of addition,
30 negation and multiplication *}
32 instantiation fps :: (zero) zero
35 definition fps_zero_def:
36 "0 = Abs_fps (\<lambda>n. 0)"
41 lemma fps_zero_nth [simp]: "0 $ n = 0"
42 unfolding fps_zero_def by simp
44 instantiation fps :: ("{one, zero}") one
47 definition fps_one_def:
48 "1 = Abs_fps (\<lambda>n. if n = 0 then 1 else 0)"
53 lemma fps_one_nth [simp]: "1 $ n = (if n = 0 then 1 else 0)"
54 unfolding fps_one_def by simp
56 instantiation fps :: (plus) plus
59 definition fps_plus_def:
60 "op + = (\<lambda>f g. Abs_fps (\<lambda>n. f $ n + g $ n))"
65 lemma fps_add_nth [simp]: "(f + g) $ n = f $ n + g $ n"
66 unfolding fps_plus_def by simp
68 instantiation fps :: (minus) minus
71 definition fps_minus_def:
72 "op - = (\<lambda>f g. Abs_fps (\<lambda>n. f $ n - g $ n))"
77 lemma fps_sub_nth [simp]: "(f - g) $ n = f $ n - g $ n"
78 unfolding fps_minus_def by simp
80 instantiation fps :: (uminus) uminus
83 definition fps_uminus_def:
84 "uminus = (\<lambda>f. Abs_fps (\<lambda>n. - (f $ n)))"
89 lemma fps_neg_nth [simp]: "(- f) $ n = - (f $ n)"
90 unfolding fps_uminus_def by simp
92 instantiation fps :: ("{comm_monoid_add, times}") times
95 definition fps_times_def:
96 "op * = (\<lambda>f g. Abs_fps (\<lambda>n. \<Sum>i=0..n. f $ i * g $ (n - i)))"
101 lemma fps_mult_nth: "(f * g) $ n = (\<Sum>i=0..n. f$i * g$(n - i))"
102 unfolding fps_times_def by simp
104 declare atLeastAtMost_iff [presburger]
105 declare Bex_def [presburger]
106 declare Ball_def [presburger]
108 lemma mult_delta_left:
109 fixes x y :: "'a::mult_zero"
110 shows "(if b then x else 0) * y = (if b then x * y else 0)"
113 lemma mult_delta_right:
114 fixes x y :: "'a::mult_zero"
115 shows "x * (if b then y else 0) = (if b then x * y else 0)"
118 lemma cond_value_iff: "f (if b then x else y) = (if b then f x else f y)"
121 lemma cond_application_beta: "(if b then f else g) x = (if b then f x else g x)"
124 subsection{* Formal power series form a commutative ring with unity, if the range of sequences
125 they represent is a commutative ring with unity*}
127 instance fps :: (semigroup_add) semigroup_add
129 fix a b c :: "'a fps"
130 show "a + b + c = a + (b + c)"
131 by (simp add: fps_ext add_assoc)
134 instance fps :: (ab_semigroup_add) ab_semigroup_add
138 by (simp add: fps_ext add_commute)
141 lemma fps_mult_assoc_lemma:
143 and f :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> 'a::comm_monoid_add"
144 shows "(\<Sum>j=0..k. \<Sum>i=0..j. f i (j - i) (n - j)) =
145 (\<Sum>j=0..k. \<Sum>i=0..k - j. f j i (n - j - i))"
146 by (induct k) (simp_all add: Suc_diff_le setsum_addf add_assoc)
148 instance fps :: (semiring_0) semigroup_mult
150 fix a b c :: "'a fps"
151 show "(a * b) * c = a * (b * c)"
154 have "(\<Sum>j=0..n. \<Sum>i=0..j. a$i * b$(j - i) * c$(n - j)) =
155 (\<Sum>j=0..n. \<Sum>i=0..n - j. a$j * b$i * c$(n - j - i))"
156 by (rule fps_mult_assoc_lemma)
157 then show "((a * b) * c) $ n = (a * (b * c)) $ n"
158 by (simp add: fps_mult_nth setsum_right_distrib setsum_left_distrib mult_assoc)
162 lemma fps_mult_commute_lemma:
164 and f :: "nat \<Rightarrow> nat \<Rightarrow> 'a::comm_monoid_add"
165 shows "(\<Sum>i=0..n. f i (n - i)) = (\<Sum>i=0..n. f (n - i) i)"
166 proof (rule setsum_reindex_cong)
167 show "inj_on (\<lambda>i. n - i) {0..n}"
168 by (rule inj_onI) simp
169 show "{0..n} = (\<lambda>i. n - i) ` {0..n}"
171 apply (rule_tac x = "n - x" in image_eqI)
176 assume "i \<in> {0..n}"
177 then have "n - (n - i) = i" by simp
178 then show "f (n - i) i = f (n - i) (n - (n - i))" by simp
181 instance fps :: (comm_semiring_0) ab_semigroup_mult
187 have "(\<Sum>i=0..n. a$i * b$(n - i)) = (\<Sum>i=0..n. a$(n - i) * b$i)"
188 by (rule fps_mult_commute_lemma)
189 then show "(a * b) $ n = (b * a) $ n"
190 by (simp add: fps_mult_nth mult_commute)
194 instance fps :: (monoid_add) monoid_add
197 show "0 + a = a" by (simp add: fps_ext)
198 show "a + 0 = a" by (simp add: fps_ext)
201 instance fps :: (comm_monoid_add) comm_monoid_add
204 show "0 + a = a" by (simp add: fps_ext)
207 instance fps :: (semiring_1) monoid_mult
210 show "1 * a = a" by (simp add: fps_ext fps_mult_nth mult_delta_left setsum_delta)
211 show "a * 1 = a" by (simp add: fps_ext fps_mult_nth mult_delta_right setsum_delta')
214 instance fps :: (cancel_semigroup_add) cancel_semigroup_add
216 fix a b c :: "'a fps"
217 { assume "a + b = a + c" then show "b = c" by (simp add: expand_fps_eq) }
218 { assume "b + a = c + a" then show "b = c" by (simp add: expand_fps_eq) }
221 instance fps :: (cancel_ab_semigroup_add) cancel_ab_semigroup_add
223 fix a b c :: "'a fps"
224 assume "a + b = a + c"
225 then show "b = c" by (simp add: expand_fps_eq)
228 instance fps :: (cancel_comm_monoid_add) cancel_comm_monoid_add ..
230 instance fps :: (group_add) group_add
233 show "- a + a = 0" by (simp add: fps_ext)
234 show "a + - b = a - b" by (simp add: fps_ext)
237 instance fps :: (ab_group_add) ab_group_add
240 show "- a + a = 0" by (simp add: fps_ext)
241 show "a - b = a + - b" by (simp add: fps_ext)
244 instance fps :: (zero_neq_one) zero_neq_one
245 by default (simp add: expand_fps_eq)
247 instance fps :: (semiring_0) semiring
249 fix a b c :: "'a fps"
250 show "(a + b) * c = a * c + b * c"
251 by (simp add: expand_fps_eq fps_mult_nth distrib_right setsum_addf)
252 show "a * (b + c) = a * b + a * c"
253 by (simp add: expand_fps_eq fps_mult_nth distrib_left setsum_addf)
256 instance fps :: (semiring_0) semiring_0
259 show "0 * a = 0" by (simp add: fps_ext fps_mult_nth)
260 show "a * 0 = 0" by (simp add: fps_ext fps_mult_nth)
263 instance fps :: (semiring_0_cancel) semiring_0_cancel ..
265 subsection {* Selection of the nth power of the implicit variable in the infinite sum*}
267 lemma fps_nonzero_nth: "f \<noteq> 0 \<longleftrightarrow> (\<exists> n. f $n \<noteq> 0)"
268 by (simp add: expand_fps_eq)
270 lemma fps_nonzero_nth_minimal: "f \<noteq> 0 \<longleftrightarrow> (\<exists>n. f $ n \<noteq> 0 \<and> (\<forall>m < n. f $ m = 0))"
272 let ?n = "LEAST n. f $ n \<noteq> 0"
273 assume "f \<noteq> 0"
274 then have "\<exists>n. f $ n \<noteq> 0"
275 by (simp add: fps_nonzero_nth)
276 then have "f $ ?n \<noteq> 0"
278 moreover have "\<forall>m<?n. f $ m = 0"
279 by (auto dest: not_less_Least)
280 ultimately have "f $ ?n \<noteq> 0 \<and> (\<forall>m<?n. f $ m = 0)" ..
281 then show "\<exists>n. f $ n \<noteq> 0 \<and> (\<forall>m<n. f $ m = 0)" ..
283 assume "\<exists>n. f $ n \<noteq> 0 \<and> (\<forall>m<n. f $ m = 0)"
284 then show "f \<noteq> 0" by (auto simp add: expand_fps_eq)
287 lemma fps_eq_iff: "f = g \<longleftrightarrow> (\<forall>n. f $ n = g $n)"
288 by (rule expand_fps_eq)
290 lemma fps_setsum_nth: "setsum f S $ n = setsum (\<lambda>k. (f k) $ n) S"
291 proof (cases "finite S")
293 then show ?thesis by (induct set: finite) auto
296 then show ?thesis by simp
299 subsection{* Injection of the basic ring elements and multiplication by scalars *}
301 definition "fps_const c = Abs_fps (\<lambda>n. if n = 0 then c else 0)"
303 lemma fps_nth_fps_const [simp]: "fps_const c $ n = (if n = 0 then c else 0)"
304 unfolding fps_const_def by simp
306 lemma fps_const_0_eq_0 [simp]: "fps_const 0 = 0"
307 by (simp add: fps_ext)
309 lemma fps_const_1_eq_1 [simp]: "fps_const 1 = 1"
310 by (simp add: fps_ext)
312 lemma fps_const_neg [simp]: "- (fps_const (c::'a::ring)) = fps_const (- c)"
313 by (simp add: fps_ext)
315 lemma fps_const_add [simp]: "fps_const (c::'a\<Colon>monoid_add) + fps_const d = fps_const (c + d)"
316 by (simp add: fps_ext)
318 lemma fps_const_sub [simp]: "fps_const (c::'a\<Colon>group_add) - fps_const d = fps_const (c - d)"
319 by (simp add: fps_ext)
321 lemma fps_const_mult[simp]: "fps_const (c::'a\<Colon>ring) * fps_const d = fps_const (c * d)"
322 by (simp add: fps_eq_iff fps_mult_nth setsum_0')
324 lemma fps_const_add_left: "fps_const (c::'a\<Colon>monoid_add) + f =
325 Abs_fps (\<lambda>n. if n = 0 then c + f$0 else f$n)"
326 by (simp add: fps_ext)
328 lemma fps_const_add_right: "f + fps_const (c::'a\<Colon>monoid_add) =
329 Abs_fps (\<lambda>n. if n = 0 then f$0 + c else f$n)"
330 by (simp add: fps_ext)
332 lemma fps_const_mult_left: "fps_const (c::'a\<Colon>semiring_0) * f = Abs_fps (\<lambda>n. c * f$n)"
333 unfolding fps_eq_iff fps_mult_nth
334 by (simp add: fps_const_def mult_delta_left setsum_delta)
336 lemma fps_const_mult_right: "f * fps_const (c::'a\<Colon>semiring_0) = Abs_fps (\<lambda>n. f$n * c)"
337 unfolding fps_eq_iff fps_mult_nth
338 by (simp add: fps_const_def mult_delta_right setsum_delta')
340 lemma fps_mult_left_const_nth [simp]: "(fps_const (c::'a::semiring_1) * f)$n = c* f$n"
341 by (simp add: fps_mult_nth mult_delta_left setsum_delta)
343 lemma fps_mult_right_const_nth [simp]: "(f * fps_const (c::'a::semiring_1))$n = f$n * c"
344 by (simp add: fps_mult_nth mult_delta_right setsum_delta')
346 subsection {* Formal power series form an integral domain*}
348 instance fps :: (ring) ring ..
350 instance fps :: (ring_1) ring_1
351 by (intro_classes, auto simp add: distrib_right)
353 instance fps :: (comm_ring_1) comm_ring_1
354 by (intro_classes, auto simp add: distrib_right)
356 instance fps :: (ring_no_zero_divisors) ring_no_zero_divisors
359 assume a0: "a \<noteq> 0" and b0: "b \<noteq> 0"
360 then obtain i j where i: "a$i\<noteq>0" "\<forall>k<i. a$k=0"
361 and j: "b$j \<noteq>0" "\<forall>k<j. b$k =0" unfolding fps_nonzero_nth_minimal
363 have "(a * b) $ (i+j) = (\<Sum>k=0..i+j. a$k * b$(i+j-k))"
364 by (rule fps_mult_nth)
365 also have "\<dots> = (a$i * b$(i+j-i)) + (\<Sum>k\<in>{0..i+j}-{i}. a$k * b$(i+j-k))"
366 by (rule setsum_diff1') simp_all
367 also have "(\<Sum>k\<in>{0..i+j}-{i}. a$k * b$(i+j-k)) = 0"
368 proof (rule setsum_0' [rule_format])
369 fix k assume "k \<in> {0..i+j} - {i}"
370 then have "k < i \<or> i+j-k < j" by auto
371 then show "a$k * b$(i+j-k) = 0" using i j by auto
373 also have "a$i * b$(i+j-i) + 0 = a$i * b$j" by simp
374 also have "a$i * b$j \<noteq> 0" using i j by simp
375 finally have "(a*b) $ (i+j) \<noteq> 0" .
376 then show "a*b \<noteq> 0" unfolding fps_nonzero_nth by blast
379 instance fps :: (ring_1_no_zero_divisors) ring_1_no_zero_divisors ..
381 instance fps :: (idom) idom ..
383 lemma numeral_fps_const: "numeral k = fps_const (numeral k)"
384 by (induct k) (simp_all only: numeral.simps fps_const_1_eq_1
385 fps_const_add [symmetric])
387 lemma neg_numeral_fps_const: "neg_numeral k = fps_const (neg_numeral k)"
388 by (simp only: neg_numeral_def numeral_fps_const fps_const_neg)
390 subsection{* The eXtractor series X*}
392 lemma minus_one_power_iff: "(- (1::'a :: {comm_ring_1})) ^ n = (if even n then 1 else - 1)"
395 definition "X = Abs_fps (\<lambda>n. if n = 1 then 1 else 0)"
397 lemma X_mult_nth [simp]:
398 "(X * (f :: ('a::semiring_1) fps)) $n = (if n = 0 then 0 else f $ (n - 1))"
399 proof (cases "n = 0")
401 have "(X * f) $n = (\<Sum>i = 0..n. X $ i * f $ (n - i))"
402 by (simp add: fps_mult_nth)
403 also have "\<dots> = f $ (n - 1)"
404 using False by (simp add: X_def mult_delta_left setsum_delta)
405 finally show ?thesis using False by simp
408 then show ?thesis by (simp add: fps_mult_nth X_def)
411 lemma X_mult_right_nth[simp]:
412 "((f :: ('a::comm_semiring_1) fps) * X) $n = (if n = 0 then 0 else f $ (n - 1))"
413 by (metis X_mult_nth mult_commute)
415 lemma X_power_iff: "X^k = Abs_fps (\<lambda>n. if n = k then (1::'a::comm_ring_1) else 0)"
418 thus ?case by (simp add: X_def fps_eq_iff)
423 have "(X^Suc k) $ m = (if m = 0 then (0::'a) else (X^k) $ (m - 1))"
424 by (simp del: One_nat_def)
425 then have "(X^Suc k) $ m = (if m = Suc k then (1::'a) else 0)"
426 using Suc.hyps by (auto cong del: if_weak_cong)
428 then show ?case by (simp add: fps_eq_iff)
431 lemma X_power_mult_nth:
432 "(X^k * (f :: ('a::comm_ring_1) fps)) $n = (if n < k then 0 else f $ (n - k))"
433 apply (induct k arbitrary: n)
435 unfolding power_Suc mult_assoc
440 lemma X_power_mult_right_nth:
441 "((f :: ('a::comm_ring_1) fps) * X^k) $n = (if n < k then 0 else f $ (n - k))"
442 by (metis X_power_mult_nth mult_commute)
445 subsection{* Formal Power series form a metric space *}
447 definition (in dist) "ball x r = {y. dist y x < r}"
449 instantiation fps :: (comm_ring_1) dist
453 dist_fps_def: "dist (a::'a fps) b =
454 (if (\<exists>n. a$n \<noteq> b$n) then inverse (2 ^ (LEAST n. a$n \<noteq> b$n)) else 0)"
456 lemma dist_fps_ge0: "dist (a::'a fps) b \<ge> 0"
457 by (simp add: dist_fps_def)
459 lemma dist_fps_sym: "dist (a::'a fps) b = dist b a"
460 apply (auto simp add: dist_fps_def)
461 apply (rule cong[OF refl, where x="(\<lambda>n\<Colon>nat. a $ n \<noteq> b $ n)"])
470 instantiation fps :: (comm_ring_1) metric_space
473 definition open_fps_def: "open (S :: 'a fps set) = (\<forall>a \<in> S. \<exists>r. r >0 \<and> ball a r \<subseteq> S)"
477 fix S :: "'a fps set"
478 show "open S = (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
479 by (auto simp add: open_fps_def ball_def subset_eq)
485 then have "\<not> (\<exists>n. a $ n \<noteq> b $ n)" by simp
486 then have "dist a b = 0" by (simp add: dist_fps_def)
490 assume d: "dist a b = 0"
491 then have "\<forall>n. a$n = b$n"
492 by - (rule ccontr, simp add: dist_fps_def)
493 then have "a = b" by (simp add: fps_eq_iff)
495 ultimately show "dist a b =0 \<longleftrightarrow> a = b" by blast
498 from th have th'[simp]: "\<And>a::'a fps. dist a a = 0" by simp
499 fix a b c :: "'a fps"
502 then have "dist a b = 0" unfolding th .
503 then have "dist a b \<le> dist a c + dist b c"
504 using dist_fps_ge0 [of a c] dist_fps_ge0 [of b c] by simp
508 assume "c = a \<or> c = b"
509 then have "dist a b \<le> dist a c + dist b c"
510 by (cases "c = a") (simp_all add: th dist_fps_sym)
514 assume ab: "a \<noteq> b" and ac: "a \<noteq> c" and bc: "b \<noteq> c"
515 def n \<equiv> "\<lambda>a b::'a fps. LEAST n. a$n \<noteq> b$n"
516 then have n': "\<And>m a b. m < n a b \<Longrightarrow> a$m = b$m"
517 by (auto dest: not_less_Least)
520 have dab: "dist a b = inverse (2 ^ n a b)"
521 and dac: "dist a c = inverse (2 ^ n a c)"
522 and dbc: "dist b c = inverse (2 ^ n b c)"
523 by (simp_all add: dist_fps_def n_def fps_eq_iff)
524 from ab ac bc have nz: "dist a b \<noteq> 0" "dist a c \<noteq> 0" "dist b c \<noteq> 0"
525 unfolding th by simp_all
526 from nz have pos: "dist a b > 0" "dist a c > 0" "dist b c > 0"
527 using dist_fps_ge0[of a b] dist_fps_ge0[of a c] dist_fps_ge0[of b c]
529 have th1: "\<And>n. (2::real)^n >0" by auto
531 assume h: "dist a b > dist a c + dist b c"
532 then have gt: "dist a b > dist a c" "dist a b > dist b c"
534 from gt have gtn: "n a b < n b c" "n a b < n a c"
535 unfolding dab dbc dac by (auto simp add: th1)
536 from n'[OF gtn(2)] n'(1)[OF gtn(1)]
537 have "a $ n a b = b $ n a b" by simp
538 moreover have "a $ n a b \<noteq> b $ n a b"
539 unfolding n_def by (rule LeastI_ex) (insert ab, simp add: fps_eq_iff)
540 ultimately have False by contradiction
542 then have "dist a b \<le> dist a c + dist b c"
543 by (auto simp add: not_le[symmetric])
545 ultimately show "dist a b \<le> dist a c + dist b c" by blast
550 text{* The infinite sums and justification of the notation in textbooks*}
552 lemma reals_power_lt_ex:
553 assumes xp: "x > 0" and y1: "(y::real) > 1"
554 shows "\<exists>k>0. (1/y)^k < x"
556 have yp: "y > 0" using y1 by simp
557 from reals_Archimedean2[of "max 0 (- log y x) + 1"]
558 obtain k::nat where k: "real k > max 0 (- log y x) + 1" by blast
559 from k have kp: "k > 0" by simp
560 from k have "real k > - log y x" by simp
561 then have "ln y * real k > - ln x" unfolding log_def
562 using ln_gt_zero_iff[OF yp] y1
563 by (simp add: minus_divide_left field_simps del:minus_divide_left[symmetric])
564 then have "ln y * real k + ln x > 0" by simp
565 then have "exp (real k * ln y + ln x) > exp 0"
566 by (simp add: mult_ac)
567 then have "y ^ k * x > 1"
568 unfolding exp_zero exp_add exp_real_of_nat_mult exp_ln [OF xp] exp_ln [OF yp]
570 then have "x > (1 / y)^k" using yp
571 by (simp add: field_simps nonzero_power_divide)
572 then show ?thesis using kp by blast
575 lemma X_nth[simp]: "X$n = (if n = 1 then 1 else 0)" by (simp add: X_def)
577 lemma X_power_nth[simp]: "(X^k) $n = (if n = k then 1 else (0::'a::comm_ring_1))"
578 by (simp add: X_power_iff)
581 lemma fps_sum_rep_nth: "(setsum (%i. fps_const(a$i)*X^i) {0..m})$n =
582 (if n \<le> m then a$n else (0::'a::comm_ring_1))"
583 apply (auto simp add: fps_setsum_nth cond_value_iff cong del: if_weak_cong)
584 apply (simp add: setsum_delta')
587 lemma fps_notation: "(%n. setsum (%i. fps_const(a$i) * X^i) {0..n}) ----> a"
593 have th0: "(2::real) > 1" by simp
594 from reals_power_lt_ex[OF rp th0]
595 obtain n0 where n0: "(1/2)^n0 < r" "n0 > 0" by blast
598 assume nn0: "n \<ge> n0"
599 then have thnn0: "(1/2)^n <= (1/2 :: real)^n0"
600 by (auto intro: power_decreasing)
603 then have "dist (?s n) a < r"
604 unfolding dist_eq_0_iff[of "?s n" a, symmetric]
605 using rp by (simp del: dist_eq_0_iff)
609 assume neq: "?s n \<noteq> a"
610 def k \<equiv> "LEAST i. ?s n $ i \<noteq> a $ i"
611 from neq have dth: "dist (?s n) a = (1/2)^k"
612 by (auto simp add: dist_fps_def inverse_eq_divide power_divide k_def fps_eq_iff)
614 from neq have kn: "k > n"
615 by (auto simp: fps_sum_rep_nth not_le k_def fps_eq_iff split: split_if_asm intro: LeastI2_ex)
616 then have "dist (?s n) a < (1/2)^n" unfolding dth
617 by (auto intro: power_strict_decreasing)
618 also have "\<dots> <= (1/2)^n0" using nn0
619 by (auto intro: power_decreasing)
620 also have "\<dots> < r" using n0 by simp
621 finally have "dist (?s n) a < r" .
623 ultimately have "dist (?s n) a < r" by blast
625 then have "\<exists>n0. \<forall> n \<ge> n0. dist (?s n) a < r " by blast
627 then show ?thesis unfolding LIMSEQ_def by blast
630 subsection{* Inverses of formal power series *}
632 declare setsum_cong[fundef_cong]
634 instantiation fps :: ("{comm_monoid_add, inverse, times, uminus}") inverse
637 fun natfun_inverse:: "'a fps \<Rightarrow> nat \<Rightarrow> 'a"
639 "natfun_inverse f 0 = inverse (f$0)"
640 | "natfun_inverse f n = - inverse (f$0) * setsum (\<lambda>i. f$i * natfun_inverse f (n - i)) {1..n}"
643 fps_inverse_def: "inverse f = (if f $ 0 = 0 then 0 else Abs_fps (natfun_inverse f))"
646 fps_divide_def: "divide = (\<lambda>(f::'a fps) g. f * inverse g)"
652 lemma fps_inverse_zero [simp]:
653 "inverse (0 :: 'a::{comm_monoid_add,inverse, times, uminus} fps) = 0"
654 by (simp add: fps_ext fps_inverse_def)
656 lemma fps_inverse_one [simp]: "inverse (1 :: 'a::{division_ring,zero_neq_one} fps) = 1"
657 apply (auto simp add: expand_fps_eq fps_inverse_def)
662 lemma inverse_mult_eq_1 [intro]:
663 assumes f0: "f$0 \<noteq> (0::'a::field)"
664 shows "inverse f * f = 1"
666 have c: "inverse f * f = f * inverse f" by (simp add: mult_commute)
667 from f0 have ifn: "\<And>n. inverse f $ n = natfun_inverse f n"
668 by (simp add: fps_inverse_def)
669 from f0 have th0: "(inverse f * f) $ 0 = 1"
670 by (simp add: fps_mult_nth fps_inverse_def)
674 from np have eq: "{0..n} = {0} \<union> {1 .. n}" by auto
675 have d: "{0} \<inter> {1 .. n} = {}" by auto
676 from f0 np have th0: "- (inverse f $ n) =
677 (setsum (\<lambda>i. f$i * natfun_inverse f (n - i)) {1..n}) / (f$0)"
678 by (cases n) (simp_all add: divide_inverse fps_inverse_def)
679 from th0[symmetric, unfolded nonzero_divide_eq_eq[OF f0]]
680 have th1: "setsum (\<lambda>i. f$i * natfun_inverse f (n - i)) {1..n} = - (f$0) * (inverse f)$n"
681 by (simp add: field_simps)
682 have "(f * inverse f) $ n = (\<Sum>i = 0..n. f $i * natfun_inverse f (n - i))"
683 unfolding fps_mult_nth ifn ..
684 also have "\<dots> = f$0 * natfun_inverse f n + (\<Sum>i = 1..n. f$i * natfun_inverse f (n-i))"
686 also have "\<dots> = 0" unfolding th1 ifn by simp
687 finally have "(inverse f * f)$n = 0" unfolding c .
689 with th0 show ?thesis by (simp add: fps_eq_iff)
692 lemma fps_inverse_0_iff[simp]: "(inverse f)$0 = (0::'a::division_ring) \<longleftrightarrow> f$0 = 0"
693 by (simp add: fps_inverse_def nonzero_imp_inverse_nonzero)
695 lemma fps_inverse_eq_0_iff[simp]: "inverse f = (0:: ('a::field) fps) \<longleftrightarrow> f $0 = 0"
699 then have "inverse f = 0" by (simp add: fps_inverse_def)
703 assume h: "inverse f = 0" and c: "f $0 \<noteq> 0"
704 from inverse_mult_eq_1[OF c] h have False by simp
706 ultimately show ?thesis by blast
709 lemma fps_inverse_idempotent[intro]:
710 assumes f0: "f$0 \<noteq> (0::'a::field)"
711 shows "inverse (inverse f) = f"
713 from f0 have if0: "inverse f $ 0 \<noteq> 0" by simp
714 from inverse_mult_eq_1[OF f0] inverse_mult_eq_1[OF if0]
715 have "inverse f * f = inverse f * inverse (inverse f)"
716 by (simp add: mult_ac)
717 then show ?thesis using f0 unfolding mult_cancel_left by simp
720 lemma fps_inverse_unique:
721 assumes f0: "f$0 \<noteq> (0::'a::field)"
723 shows "inverse f = g"
725 from inverse_mult_eq_1[OF f0] fg
726 have th0: "inverse f * f = g * f" by (simp add: mult_ac)
727 then show ?thesis using f0 unfolding mult_cancel_right
728 by (auto simp add: expand_fps_eq)
731 lemma fps_inverse_gp: "inverse (Abs_fps(\<lambda>n. (1::'a::field)))
732 = Abs_fps (\<lambda>n. if n= 0 then 1 else if n=1 then - 1 else 0)"
733 apply (rule fps_inverse_unique)
735 apply (simp add: fps_eq_iff fps_mult_nth)
739 let ?f = "\<lambda>i. if n = i then (1\<Colon>'a) else if n - i = 1 then - 1 else 0"
740 let ?g = "\<lambda>i. if i = n then 1 else if i=n - 1 then - 1 else 0"
741 let ?h = "\<lambda>i. if i=n - 1 then - 1 else 0"
742 have th1: "setsum ?f {0..n} = setsum ?g {0..n}"
743 by (rule setsum_cong2) auto
744 have th2: "setsum ?g {0..n - 1} = setsum ?h {0..n - 1}"
745 using n apply - by (rule setsum_cong2) auto
746 have eq: "{0 .. n} = {0.. n - 1} \<union> {n}" by auto
747 from n have d: "{0.. n - 1} \<inter> {n} = {}" by auto
748 have f: "finite {0.. n - 1}" "finite {n}" by auto
749 show "setsum ?f {0..n} = 0"
751 apply (simp add: setsum_Un_disjoint[OF f d, unfolded eq[symmetric]] del: One_nat_def)
753 apply (simp add: setsum_delta)
757 subsection{* Formal Derivatives, and the MacLaurin theorem around 0*}
759 definition "fps_deriv f = Abs_fps (\<lambda>n. of_nat (n + 1) * f $ (n + 1))"
761 lemma fps_deriv_nth[simp]: "fps_deriv f $ n = of_nat (n +1) * f $ (n+1)"
762 by (simp add: fps_deriv_def)
764 lemma fps_deriv_linear[simp]:
765 "fps_deriv (fps_const (a::'a::comm_semiring_1) * f + fps_const b * g) =
766 fps_const a * fps_deriv f + fps_const b * fps_deriv g"
767 unfolding fps_eq_iff fps_add_nth fps_const_mult_left fps_deriv_nth by (simp add: field_simps)
769 lemma fps_deriv_mult[simp]:
770 fixes f :: "('a :: comm_ring_1) fps"
771 shows "fps_deriv (f * g) = f * fps_deriv g + fps_deriv f * g"
776 let ?Zn1 = "{0 .. n + 1}"
777 let ?f = "\<lambda>i. i + 1"
778 have fi: "inj_on ?f {0..n}" by (simp add: inj_on_def)
779 have eq: "{1.. n+1} = ?f ` {0..n}" by auto
780 let ?g = "\<lambda>i. of_nat (i+1) * g $ (i+1) * f $ (n - i) +
781 of_nat (i+1)* f $ (i+1) * g $ (n - i)"
782 let ?h = "\<lambda>i. of_nat i * g $ i * f $ ((n+1) - i) +
783 of_nat i* f $ i * g $ ((n + 1) - i)"
786 assume k: "k \<in> {0..n}"
787 have "?h (k + 1) = ?g k" using k by auto
790 have eq': "{0..n +1}- {1 .. n+1} = {0}" by auto
791 have s0: "setsum (\<lambda>i. of_nat i * f $ i * g $ (n + 1 - i)) ?Zn1 =
792 setsum (\<lambda>i. of_nat (n + 1 - i) * f $ (n + 1 - i) * g $ i) ?Zn1"
793 apply (rule setsum_reindex_cong[where f="\<lambda>i. n + 1 - i"])
794 apply (simp add: inj_on_def Ball_def)
797 apply (presburger add: image_iff)
800 have s1: "setsum (\<lambda>i. f $ i * g $ (n + 1 - i)) ?Zn1 =
801 setsum (\<lambda>i. f $ (n + 1 - i) * g $ i) ?Zn1"
802 apply (rule setsum_reindex_cong[where f="\<lambda>i. n + 1 - i"])
803 apply (simp add: inj_on_def Ball_def)
806 apply (presburger add: image_iff)
809 have "(f * ?D g + ?D f * g)$n = (?D g * f + ?D f * g)$n"
810 by (simp only: mult_commute)
811 also have "\<dots> = (\<Sum>i = 0..n. ?g i)"
812 by (simp add: fps_mult_nth setsum_addf[symmetric])
813 also have "\<dots> = setsum ?h {1..n+1}"
814 using th0 setsum_reindex_cong[OF fi eq, of "?g" "?h"] by auto
815 also have "\<dots> = setsum ?h {0..n+1}"
816 apply (rule setsum_mono_zero_left)
818 apply (simp add: subset_eq)
822 also have "\<dots> = (fps_deriv (f * g)) $ n"
823 apply (simp only: fps_deriv_nth fps_mult_nth setsum_addf)
825 unfolding setsum_addf[symmetric] setsum_right_distrib
826 apply (rule setsum_cong2)
827 apply (auto simp add: of_nat_diff field_simps)
829 finally have "(f * ?D g + ?D f * g) $ n = ?D (f*g) $ n" .
831 then show ?thesis unfolding fps_eq_iff by auto
834 lemma fps_deriv_X[simp]: "fps_deriv X = 1"
835 by (simp add: fps_deriv_def X_def fps_eq_iff)
837 lemma fps_deriv_neg[simp]: "fps_deriv (- (f:: ('a:: comm_ring_1) fps)) = - (fps_deriv f)"
838 by (simp add: fps_eq_iff fps_deriv_def)
840 lemma fps_deriv_add[simp]: "fps_deriv ((f:: ('a::comm_ring_1) fps) + g) = fps_deriv f + fps_deriv g"
841 using fps_deriv_linear[of 1 f 1 g] by simp
843 lemma fps_deriv_sub[simp]: "fps_deriv ((f:: ('a::comm_ring_1) fps) - g) = fps_deriv f - fps_deriv g"
844 using fps_deriv_add [of f "- g"] by simp
846 lemma fps_deriv_const[simp]: "fps_deriv (fps_const c) = 0"
847 by (simp add: fps_ext fps_deriv_def fps_const_def)
849 lemma fps_deriv_mult_const_left[simp]:
850 "fps_deriv (fps_const (c::'a::comm_ring_1) * f) = fps_const c * fps_deriv f"
853 lemma fps_deriv_0[simp]: "fps_deriv 0 = 0"
854 by (simp add: fps_deriv_def fps_eq_iff)
856 lemma fps_deriv_1[simp]: "fps_deriv 1 = 0"
857 by (simp add: fps_deriv_def fps_eq_iff )
859 lemma fps_deriv_mult_const_right[simp]:
860 "fps_deriv (f * fps_const (c::'a::comm_ring_1)) = fps_deriv f * fps_const c"
863 lemma fps_deriv_setsum:
864 "fps_deriv (setsum f S) = setsum (\<lambda>i. fps_deriv (f i :: ('a::comm_ring_1) fps)) S"
865 proof (cases "finite S")
867 then show ?thesis by simp
870 show ?thesis by (induct rule: finite_induct [OF True]) simp_all
873 lemma fps_deriv_eq_0_iff [simp]:
874 "fps_deriv f = 0 \<longleftrightarrow> (f = fps_const (f$0 :: 'a::{idom,semiring_char_0}))"
877 assume "f = fps_const (f$0)"
878 then have "fps_deriv f = fps_deriv (fps_const (f$0))" by simp
879 then have "fps_deriv f = 0" by simp
883 assume z: "fps_deriv f = 0"
884 then have "\<forall>n. (fps_deriv f)$n = 0" by simp
885 then have "\<forall>n. f$(n+1) = 0" by (simp del: of_nat_Suc of_nat_add One_nat_def)
886 then have "f = fps_const (f$0)"
887 apply (clarsimp simp add: fps_eq_iff fps_const_def)
888 apply (erule_tac x="n - 1" in allE)
892 ultimately show ?thesis by blast
895 lemma fps_deriv_eq_iff:
896 fixes f:: "('a::{idom,semiring_char_0}) fps"
897 shows "fps_deriv f = fps_deriv g \<longleftrightarrow> (f = fps_const(f$0 - g$0) + g)"
899 have "fps_deriv f = fps_deriv g \<longleftrightarrow> fps_deriv (f - g) = 0"
901 also have "\<dots> \<longleftrightarrow> f - g = fps_const ((f-g)$0)"
902 unfolding fps_deriv_eq_0_iff ..
903 finally show ?thesis by (simp add: field_simps)
906 lemma fps_deriv_eq_iff_ex:
907 "(fps_deriv f = fps_deriv g) \<longleftrightarrow> (\<exists>(c::'a::{idom,semiring_char_0}). f = fps_const c + g)"
908 by (auto simp: fps_deriv_eq_iff)
911 fun fps_nth_deriv :: "nat \<Rightarrow> ('a::semiring_1) fps \<Rightarrow> 'a fps"
913 "fps_nth_deriv 0 f = f"
914 | "fps_nth_deriv (Suc n) f = fps_nth_deriv n (fps_deriv f)"
916 lemma fps_nth_deriv_commute: "fps_nth_deriv (Suc n) f = fps_deriv (fps_nth_deriv n f)"
917 by (induct n arbitrary: f) auto
919 lemma fps_nth_deriv_linear[simp]:
920 "fps_nth_deriv n (fps_const (a::'a::comm_semiring_1) * f + fps_const b * g) =
921 fps_const a * fps_nth_deriv n f + fps_const b * fps_nth_deriv n g"
922 by (induct n arbitrary: f g) (auto simp add: fps_nth_deriv_commute)
924 lemma fps_nth_deriv_neg[simp]:
925 "fps_nth_deriv n (- (f:: ('a:: comm_ring_1) fps)) = - (fps_nth_deriv n f)"
926 by (induct n arbitrary: f) simp_all
928 lemma fps_nth_deriv_add[simp]:
929 "fps_nth_deriv n ((f:: ('a::comm_ring_1) fps) + g) = fps_nth_deriv n f + fps_nth_deriv n g"
930 using fps_nth_deriv_linear[of n 1 f 1 g] by simp
932 lemma fps_nth_deriv_sub[simp]:
933 "fps_nth_deriv n ((f:: ('a::comm_ring_1) fps) - g) = fps_nth_deriv n f - fps_nth_deriv n g"
934 using fps_nth_deriv_add [of n f "- g"] by simp
936 lemma fps_nth_deriv_0[simp]: "fps_nth_deriv n 0 = 0"
937 by (induct n) simp_all
939 lemma fps_nth_deriv_1[simp]: "fps_nth_deriv n 1 = (if n = 0 then 1 else 0)"
940 by (induct n) simp_all
942 lemma fps_nth_deriv_const[simp]:
943 "fps_nth_deriv n (fps_const c) = (if n = 0 then fps_const c else 0)"
944 by (cases n) simp_all
946 lemma fps_nth_deriv_mult_const_left[simp]:
947 "fps_nth_deriv n (fps_const (c::'a::comm_ring_1) * f) = fps_const c * fps_nth_deriv n f"
948 using fps_nth_deriv_linear[of n "c" f 0 0 ] by simp
950 lemma fps_nth_deriv_mult_const_right[simp]:
951 "fps_nth_deriv n (f * fps_const (c::'a::comm_ring_1)) = fps_nth_deriv n f * fps_const c"
952 using fps_nth_deriv_linear[of n "c" f 0 0] by (simp add: mult_commute)
954 lemma fps_nth_deriv_setsum:
955 "fps_nth_deriv n (setsum f S) = setsum (\<lambda>i. fps_nth_deriv n (f i :: ('a::comm_ring_1) fps)) S"
956 proof (cases "finite S")
958 show ?thesis by (induct rule: finite_induct [OF True]) simp_all
961 then show ?thesis by simp
964 lemma fps_deriv_maclauren_0:
965 "(fps_nth_deriv k (f:: ('a::comm_semiring_1) fps)) $ 0 = of_nat (fact k) * f$(k)"
966 by (induct k arbitrary: f) (auto simp add: field_simps of_nat_mult)
968 subsection {* Powers*}
970 lemma fps_power_zeroth_eq_one: "a$0 =1 \<Longrightarrow> a^n $ 0 = (1::'a::semiring_1)"
971 by (induct n) (auto simp add: expand_fps_eq fps_mult_nth)
973 lemma fps_power_first_eq: "(a:: 'a::comm_ring_1 fps)$0 =1 \<Longrightarrow> a^n $ 1 = of_nat n * a$1"
976 then show ?case by simp
979 note h = Suc.hyps[OF `a$0 = 1`]
980 show ?case unfolding power_Suc fps_mult_nth
981 using h `a$0 = 1` fps_power_zeroth_eq_one[OF `a$0=1`]
982 by (simp add: field_simps)
985 lemma startsby_one_power:"a $ 0 = (1::'a::comm_ring_1) \<Longrightarrow> a^n $ 0 = 1"
986 by (induct n) (auto simp add: fps_mult_nth)
988 lemma startsby_zero_power:"a $0 = (0::'a::comm_ring_1) \<Longrightarrow> n > 0 \<Longrightarrow> a^n $0 = 0"
989 by (induct n) (auto simp add: fps_mult_nth)
991 lemma startsby_power:"a $0 = (v::'a::{comm_ring_1}) \<Longrightarrow> a^n $0 = v^n"
992 by (induct n) (auto simp add: fps_mult_nth)
994 lemma startsby_zero_power_iff[simp]: "a^n $0 = (0::'a::{idom}) \<longleftrightarrow> (n \<noteq> 0 \<and> a$0 = 0)"
997 apply (auto simp add: fps_mult_nth)
998 apply (rule startsby_zero_power, simp_all)
1001 lemma startsby_zero_power_prefix:
1002 assumes a0: "a $0 = (0::'a::idom)"
1003 shows "\<forall>n < k. a ^ k $ n = 0"
1005 proof(induct k rule: nat_less_induct)
1007 assume H: "\<forall>m<k. a $0 = 0 \<longrightarrow> (\<forall>n<m. a ^ m $ n = 0)" and a0: "a $0 = (0\<Colon>'a)"
1008 let ?ths = "\<forall>m<k. a ^ k $ m = 0"
1009 { assume "k = 0" then have ?ths by simp }
1013 assume k: "k = Suc l"
1019 then have "a^k $ m = 0"
1020 using startsby_zero_power[of a k] k a0 by simp
1024 assume m0: "m \<noteq> 0"
1025 have "a ^k $ m = (a^l * a) $m" by (simp add: k mult_commute)
1026 also have "\<dots> = (\<Sum>i = 0..m. a ^ l $ i * a $ (m - i))" by (simp add: fps_mult_nth)
1027 also have "\<dots> = 0"
1028 apply (rule setsum_0')
1030 apply (case_tac "x = m")
1032 apply (rule H[rule_format])
1033 using a0 k mk apply auto
1035 finally have "a^k $ m = 0" .
1037 ultimately have "a^k $ m = 0" by blast
1039 then have ?ths by blast
1041 ultimately show ?ths by (cases k) auto
1044 lemma startsby_zero_setsum_depends:
1045 assumes a0: "a $0 = (0::'a::idom)" and kn: "n \<ge> k"
1046 shows "setsum (\<lambda>i. (a ^ i)$k) {0 .. n} = setsum (\<lambda>i. (a ^ i)$k) {0 .. k}"
1047 apply (rule setsum_mono_zero_right)
1049 apply (rule startsby_zero_power_prefix[rule_format, OF a0])
1053 lemma startsby_zero_power_nth_same:
1054 assumes a0: "a$0 = (0::'a::{idom})"
1055 shows "a^n $ n = (a$1) ^ n"
1058 then show ?case by simp
1061 have "a ^ Suc n $ (Suc n) = (a^n * a)$(Suc n)" by (simp add: field_simps)
1062 also have "\<dots> = setsum (\<lambda>i. a^n$i * a $ (Suc n - i)) {0.. Suc n}"
1063 by (simp add: fps_mult_nth)
1064 also have "\<dots> = setsum (\<lambda>i. a^n$i * a $ (Suc n - i)) {n .. Suc n}"
1065 apply (rule setsum_mono_zero_right)
1069 apply (rule startsby_zero_power_prefix[rule_format, OF a0])
1072 also have "\<dots> = a^n $ n * a$1" using a0 by simp
1073 finally show ?case using Suc.hyps by simp
1076 lemma fps_inverse_power:
1077 fixes a :: "('a::{field}) fps"
1078 shows "inverse (a^n) = inverse a ^ n"
1081 assume a0: "a$0 = 0"
1082 then have eq: "inverse a = 0" by (simp add: fps_inverse_def)
1083 { assume "n = 0" hence ?thesis by simp }
1087 from startsby_zero_power[OF a0 n] eq a0 n have ?thesis
1088 by (simp add: fps_inverse_def)
1090 ultimately have ?thesis by blast
1094 assume a0: "a$0 \<noteq> 0"
1096 apply (rule fps_inverse_unique)
1097 apply (simp add: a0)
1098 unfolding power_mult_distrib[symmetric]
1099 apply (rule ssubst[where t = "a * inverse a" and s= 1])
1101 apply (subst mult_commute)
1102 apply (rule inverse_mult_eq_1[OF a0])
1105 ultimately show ?thesis by blast
1108 lemma fps_deriv_power:
1109 "fps_deriv (a ^ n) = fps_const (of_nat n :: 'a:: comm_ring_1) * fps_deriv a * a ^ (n - 1)"
1111 apply (auto simp add: field_simps fps_const_add[symmetric] simp del: fps_const_add)
1113 apply (auto simp add: field_simps)
1116 lemma fps_inverse_deriv:
1117 fixes a:: "('a :: field) fps"
1118 assumes a0: "a$0 \<noteq> 0"
1119 shows "fps_deriv (inverse a) = - fps_deriv a * (inverse a)\<^sup>2"
1121 from inverse_mult_eq_1[OF a0]
1122 have "fps_deriv (inverse a * a) = 0" by simp
1123 hence "inverse a * fps_deriv a + fps_deriv (inverse a) * a = 0" by simp
1124 hence "inverse a * (inverse a * fps_deriv a + fps_deriv (inverse a) * a) = 0" by simp
1125 with inverse_mult_eq_1[OF a0]
1126 have "(inverse a)\<^sup>2 * fps_deriv a + fps_deriv (inverse a) = 0"
1127 unfolding power2_eq_square
1128 apply (simp add: field_simps)
1129 apply (simp add: mult_assoc[symmetric])
1131 then have "(inverse a)\<^sup>2 * fps_deriv a + fps_deriv (inverse a) - fps_deriv a * (inverse a)\<^sup>2 =
1132 0 - fps_deriv a * (inverse a)\<^sup>2"
1134 then show "fps_deriv (inverse a) = - fps_deriv a * (inverse a)\<^sup>2"
1135 by (simp add: field_simps)
1138 lemma fps_inverse_mult:
1139 fixes a::"('a :: field) fps"
1140 shows "inverse (a * b) = inverse a * inverse b"
1143 assume a0: "a$0 = 0" hence ab0: "(a*b)$0 = 0" by (simp add: fps_mult_nth)
1144 from a0 ab0 have th: "inverse a = 0" "inverse (a*b) = 0" by simp_all
1145 have ?thesis unfolding th by simp
1149 assume b0: "b$0 = 0" hence ab0: "(a*b)$0 = 0" by (simp add: fps_mult_nth)
1150 from b0 ab0 have th: "inverse b = 0" "inverse (a*b) = 0" by simp_all
1151 have ?thesis unfolding th by simp
1155 assume a0: "a$0 \<noteq> 0" and b0: "b$0 \<noteq> 0"
1156 from a0 b0 have ab0:"(a*b) $ 0 \<noteq> 0" by (simp add: fps_mult_nth)
1157 from inverse_mult_eq_1[OF ab0]
1158 have "inverse (a*b) * (a*b) * inverse a * inverse b = 1 * inverse a * inverse b" by simp
1159 then have "inverse (a*b) * (inverse a * a) * (inverse b * b) = inverse a * inverse b"
1160 by (simp add: field_simps)
1161 then have ?thesis using inverse_mult_eq_1[OF a0] inverse_mult_eq_1[OF b0] by simp
1163 ultimately show ?thesis by blast
1166 lemma fps_inverse_deriv':
1167 fixes a:: "('a :: field) fps"
1168 assumes a0: "a$0 \<noteq> 0"
1169 shows "fps_deriv (inverse a) = - fps_deriv a / a\<^sup>2"
1170 using fps_inverse_deriv[OF a0]
1171 unfolding power2_eq_square fps_divide_def fps_inverse_mult
1174 lemma inverse_mult_eq_1':
1175 assumes f0: "f$0 \<noteq> (0::'a::field)"
1176 shows "f * inverse f= 1"
1177 by (metis mult_commute inverse_mult_eq_1 f0)
1179 lemma fps_divide_deriv:
1180 fixes a:: "('a :: field) fps"
1181 assumes a0: "b$0 \<noteq> 0"
1182 shows "fps_deriv (a / b) = (fps_deriv a * b - a * fps_deriv b) / b\<^sup>2"
1183 using fps_inverse_deriv[OF a0]
1184 by (simp add: fps_divide_def field_simps
1185 power2_eq_square fps_inverse_mult inverse_mult_eq_1'[OF a0])
1188 lemma fps_inverse_gp': "inverse (Abs_fps(\<lambda>n. (1::'a::field))) = 1 - X"
1189 by (simp add: fps_inverse_gp fps_eq_iff X_def)
1191 lemma fps_nth_deriv_X[simp]: "fps_nth_deriv n X = (if n = 0 then X else if n=1 then 1 else 0)"
1192 by (cases n) simp_all
1195 lemma fps_inverse_X_plus1:
1196 "inverse (1 + X) = Abs_fps (\<lambda>n. (- (1::'a::{field})) ^ n)" (is "_ = ?r")
1198 have eq: "(1 + X) * ?r = 1"
1199 unfolding minus_one_power_iff
1200 by (auto simp add: field_simps fps_eq_iff)
1201 show ?thesis by (auto simp add: eq intro: fps_inverse_unique simp del: minus_one)
1205 subsection{* Integration *}
1207 definition fps_integral :: "'a::field_char_0 fps \<Rightarrow> 'a \<Rightarrow> 'a fps"
1208 where "fps_integral a a0 = Abs_fps (\<lambda>n. if n = 0 then a0 else (a$(n - 1) / of_nat n))"
1210 lemma fps_deriv_fps_integral: "fps_deriv (fps_integral a a0) = a"
1211 unfolding fps_integral_def fps_deriv_def
1212 by (simp add: fps_eq_iff del: of_nat_Suc)
1214 lemma fps_integral_linear:
1215 "fps_integral (fps_const a * f + fps_const b * g) (a*a0 + b*b0) =
1216 fps_const a * fps_integral f a0 + fps_const b * fps_integral g b0"
1219 have "fps_deriv ?l = fps_deriv ?r" by (simp add: fps_deriv_fps_integral)
1220 moreover have "?l$0 = ?r$0" by (simp add: fps_integral_def)
1221 ultimately show ?thesis
1222 unfolding fps_deriv_eq_iff by auto
1226 subsection {* Composition of FPSs *}
1228 definition fps_compose :: "('a::semiring_1) fps \<Rightarrow> 'a fps \<Rightarrow> 'a fps" (infixl "oo" 55) where
1229 fps_compose_def: "a oo b = Abs_fps (\<lambda>n. setsum (\<lambda>i. a$i * (b^i$n)) {0..n})"
1231 lemma fps_compose_nth: "(a oo b)$n = setsum (\<lambda>i. a$i * (b^i$n)) {0..n}"
1232 by (simp add: fps_compose_def)
1234 lemma fps_compose_X[simp]: "a oo X = (a :: ('a :: comm_ring_1) fps)"
1235 by (simp add: fps_ext fps_compose_def mult_delta_right setsum_delta')
1237 lemma fps_const_compose[simp]:
1238 "fps_const (a::'a::{comm_ring_1}) oo b = fps_const (a)"
1239 by (simp add: fps_eq_iff fps_compose_nth mult_delta_left setsum_delta)
1241 lemma numeral_compose[simp]: "(numeral k::('a::{comm_ring_1}) fps) oo b = numeral k"
1242 unfolding numeral_fps_const by simp
1244 lemma neg_numeral_compose[simp]: "(neg_numeral k::('a::{comm_ring_1}) fps) oo b = neg_numeral k"
1245 unfolding neg_numeral_fps_const by simp
1247 lemma X_fps_compose_startby0[simp]: "a$0 = 0 \<Longrightarrow> X oo a = (a :: ('a :: comm_ring_1) fps)"
1248 by (simp add: fps_eq_iff fps_compose_def mult_delta_left setsum_delta not_le)
1251 subsection {* Rules from Herbert Wilf's Generatingfunctionology*}
1253 subsubsection {* Rule 1 *}
1254 (* {a_{n+k}}_0^infty Corresponds to (f - setsum (\<lambda>i. a_i * x^i))/x^h, for h>0*)
1256 lemma fps_power_mult_eq_shift:
1257 "X^Suc k * Abs_fps (\<lambda>n. a (n + Suc k)) =
1258 Abs_fps a - setsum (\<lambda>i. fps_const (a i :: 'a:: comm_ring_1) * X^i) {0 .. k}"
1262 have "?lhs $ n = (if n < Suc k then 0 else a n)"
1263 unfolding X_power_mult_nth by auto
1264 also have "\<dots> = ?rhs $ n"
1267 thus ?case by (simp add: fps_setsum_nth)
1270 note th = Suc.hyps[symmetric]
1271 have "(Abs_fps a - setsum (\<lambda>i. fps_const (a i :: 'a) * X^i) {0 .. Suc k})$n =
1272 (Abs_fps a - setsum (\<lambda>i. fps_const (a i :: 'a) * X^i) {0 .. k} -
1273 fps_const (a (Suc k)) * X^ Suc k) $ n"
1274 by (simp add: field_simps)
1275 also have "\<dots> = (if n < Suc k then 0 else a n) - (fps_const (a (Suc k)) * X^ Suc k)$n"
1276 using th unfolding fps_sub_nth by simp
1277 also have "\<dots> = (if n < Suc (Suc k) then 0 else a n)"
1278 unfolding X_power_mult_right_nth
1279 apply (auto simp add: not_less fps_const_def)
1280 apply (rule cong[of a a, OF refl])
1283 finally show ?case by simp
1285 finally have "?lhs $ n = ?rhs $ n" .
1287 then show ?thesis by (simp add: fps_eq_iff)
1291 subsubsection {* Rule 2*}
1293 (* We can not reach the form of Wilf, but still near to it using rewrite rules*)
1294 (* If f reprents {a_n} and P is a polynomial, then
1295 P(xD) f represents {P(n) a_n}*)
1297 definition "XD = op * X o fps_deriv"
1299 lemma XD_add[simp]:"XD (a + b) = XD a + XD (b :: ('a::comm_ring_1) fps)"
1300 by (simp add: XD_def field_simps)
1302 lemma XD_mult_const[simp]:"XD (fps_const (c::'a::comm_ring_1) * a) = fps_const c * XD a"
1303 by (simp add: XD_def field_simps)
1305 lemma XD_linear[simp]: "XD (fps_const c * a + fps_const d * b) =
1306 fps_const c * XD a + fps_const d * XD (b :: ('a::comm_ring_1) fps)"
1310 "(XD ^^ n) (fps_const c * a + fps_const d * b) =
1311 fps_const c * (XD ^^ n) a + fps_const d * (XD ^^ n) (b :: ('a::comm_ring_1) fps)"
1312 by (induct n) simp_all
1314 lemma fps_mult_X_deriv_shift: "X* fps_deriv a = Abs_fps (\<lambda>n. of_nat n* a$n)"
1315 by (simp add: fps_eq_iff)
1318 lemma fps_mult_XD_shift:
1319 "(XD ^^ k) (a:: ('a::{comm_ring_1}) fps) = Abs_fps (\<lambda>n. (of_nat n ^ k) * a$n)"
1320 by (induct k arbitrary: a) (simp_all add: XD_def fps_eq_iff field_simps del: One_nat_def)
1323 subsubsection{* Rule 3 is trivial and is given by @{text fps_times_def}*}
1325 subsubsection{* Rule 5 --- summation and "division" by (1 - X)*}
1327 lemma fps_divide_X_minus1_setsum_lemma:
1328 "a = ((1::('a::comm_ring_1) fps) - X) * Abs_fps (\<lambda>n. setsum (\<lambda>i. a $ i) {0..n})"
1330 let ?X = "X::('a::comm_ring_1) fps"
1331 let ?sa = "Abs_fps (\<lambda>n. setsum (\<lambda>i. a $ i) {0..n})"
1332 have th0: "\<And>i. (1 - (X::'a fps)) $ i = (if i = 0 then 1 else if i = 1 then - 1 else 0)"
1338 hence "a$n = ((1 - ?X) * ?sa) $ n"
1339 by (simp add: fps_mult_nth)
1343 assume n0: "n \<noteq> 0"
1344 then have u: "{0} \<union> ({1} \<union> {2..n}) = {0..n}" "{1}\<union>{2..n} = {1..n}"
1345 "{0..n - 1}\<union>{n} = {0..n}"
1346 by (auto simp: set_eq_iff)
1347 have d: "{0} \<inter> ({1} \<union> {2..n}) = {}" "{1} \<inter> {2..n} = {}"
1348 "{0..n - 1}\<inter>{n} ={}" using n0 by simp_all
1349 have f: "finite {0}" "finite {1}" "finite {2 .. n}"
1350 "finite {0 .. n - 1}" "finite {n}" by simp_all
1351 have "((1 - ?X) * ?sa) $ n = setsum (\<lambda>i. (1 - ?X)$ i * ?sa $ (n - i)) {0 .. n}"
1352 by (simp add: fps_mult_nth)
1353 also have "\<dots> = a$n"
1355 unfolding setsum_Un_disjoint[OF f(1) finite_UnI[OF f(2,3)] d(1), unfolded u(1)]
1356 unfolding setsum_Un_disjoint[OF f(2) f(3) d(2)]
1358 unfolding setsum_Un_disjoint[OF f(4,5) d(3), unfolded u(3)]
1361 finally have "a$n = ((1 - ?X) * ?sa) $ n" by simp
1363 ultimately have "a$n = ((1 - ?X) * ?sa) $ n" by blast
1365 then show ?thesis unfolding fps_eq_iff by blast
1368 lemma fps_divide_X_minus1_setsum:
1369 "a /((1::('a::field) fps) - X) = Abs_fps (\<lambda>n. setsum (\<lambda>i. a $ i) {0..n})"
1371 let ?X = "1 - (X::('a::field) fps)"
1372 have th0: "?X $ 0 \<noteq> 0" by simp
1373 have "a /?X = ?X * Abs_fps (\<lambda>n\<Colon>nat. setsum (op $ a) {0..n}) * inverse ?X"
1374 using fps_divide_X_minus1_setsum_lemma[of a, symmetric] th0
1375 by (simp add: fps_divide_def mult_assoc)
1376 also have "\<dots> = (inverse ?X * ?X) * Abs_fps (\<lambda>n\<Colon>nat. setsum (op $ a) {0..n}) "
1377 by (simp add: mult_ac)
1378 finally show ?thesis by (simp add: inverse_mult_eq_1[OF th0])
1382 subsubsection{* Rule 4 in its more general form: generalizes Rule 3 for an arbitrary
1383 finite product of FPS, also the relvant instance of powers of a FPS*}
1385 definition "natpermute n k = {l :: nat list. length l = k \<and> listsum l = n}"
1387 lemma natlist_trivial_1: "natpermute n 1 = {[n]}"
1388 apply (auto simp add: natpermute_def)
1393 lemma append_natpermute_less_eq:
1394 assumes h: "xs@ys \<in> natpermute n k"
1395 shows "listsum xs \<le> n" and "listsum ys \<le> n"
1397 from h have "listsum (xs @ ys) = n" by (simp add: natpermute_def)
1398 hence "listsum xs + listsum ys = n" by simp
1399 then show "listsum xs \<le> n" and "listsum ys \<le> n" by simp_all
1402 lemma natpermute_split:
1403 assumes mn: "h \<le> k"
1404 shows "natpermute n k =
1405 (\<Union>m \<in>{0..n}. {l1 @ l2 |l1 l2. l1 \<in> natpermute m h \<and> l2 \<in> natpermute (n - m) (k - h)})"
1406 (is "?L = ?R" is "?L = (\<Union>m \<in>{0..n}. ?S m)")
1410 assume l: "l \<in> ?R"
1411 from l obtain m xs ys where h: "m \<in> {0..n}"
1412 and xs: "xs \<in> natpermute m h"
1413 and ys: "ys \<in> natpermute (n - m) (k - h)"
1414 and leq: "l = xs@ys" by blast
1415 from xs have xs': "listsum xs = m"
1416 by (simp add: natpermute_def)
1417 from ys have ys': "listsum ys = n - m"
1418 by (simp add: natpermute_def)
1419 have "l \<in> ?L" using leq xs ys h
1420 apply (clarsimp simp add: natpermute_def)
1423 unfolding natpermute_def
1430 assume l: "l \<in> natpermute n k"
1431 let ?xs = "take h l"
1432 let ?ys = "drop h l"
1433 let ?m = "listsum ?xs"
1434 from l have ls: "listsum (?xs @ ?ys) = n"
1435 by (simp add: natpermute_def)
1436 have xs: "?xs \<in> natpermute ?m h" using l mn
1437 by (simp add: natpermute_def)
1438 have l_take_drop: "listsum l = listsum (take h l @ drop h l)"
1440 then have ys: "?ys \<in> natpermute (n - ?m) (k - h)"
1441 using l mn ls by (auto simp add: natpermute_def simp del: append_take_drop_id)
1442 from ls have m: "?m \<in> {0..n}"
1443 by (simp add: l_take_drop del: append_take_drop_id)
1444 from xs ys ls have "l \<in> ?R"
1446 apply (rule bexI [where x = "?m"])
1447 apply (rule exI [where x = "?xs"])
1448 apply (rule exI [where x = "?ys"])
1450 apply (auto simp add: natpermute_def l_take_drop simp del: append_take_drop_id)
1454 ultimately show ?thesis by blast
1457 lemma natpermute_0: "natpermute n 0 = (if n = 0 then {[]} else {})"
1458 by (auto simp add: natpermute_def)
1460 lemma natpermute_0'[simp]: "natpermute 0 k = (if k = 0 then {[]} else {replicate k 0})"
1461 apply (auto simp add: set_replicate_conv_if natpermute_def)
1462 apply (rule nth_equalityI)
1466 lemma natpermute_finite: "finite (natpermute n k)"
1467 proof (induct k arbitrary: n)
1470 apply (subst natpermute_split[of 0 0, simplified])
1471 apply (simp add: natpermute_0)
1475 then show ?case unfolding natpermute_split [of k "Suc k", simplified]
1477 apply (rule finite_UN_I)
1479 unfolding One_nat_def[symmetric] natlist_trivial_1
1484 lemma natpermute_contain_maximal:
1485 "{xs \<in> natpermute n (k+1). n \<in> set xs} = UNION {0 .. k} (\<lambda>i. {(replicate (k+1) 0) [i:=n]})"
1490 assume H: "xs \<in> natpermute n (k+1)" and n: "n \<in> set xs"
1491 from n obtain i where i: "i \<in> {0.. k}" "xs!i = n" using H
1492 unfolding in_set_conv_nth by (auto simp add: less_Suc_eq_le natpermute_def)
1493 have eqs: "({0..k} - {i}) \<union> {i} = {0..k}"
1495 have f: "finite({0..k} - {i})" "finite {i}"
1497 have d: "({0..k} - {i}) \<inter> {i} = {}"
1499 from H have "n = setsum (nth xs) {0..k}"
1500 apply (simp add: natpermute_def)
1501 apply (auto simp add: atLeastLessThanSuc_atLeastAtMost listsum_setsum_nth)
1503 also have "\<dots> = n + setsum (nth xs) ({0..k} - {i})"
1504 unfolding setsum_Un_disjoint[OF f d, unfolded eqs] using i by simp
1505 finally have zxs: "\<forall> j\<in> {0..k} - {i}. xs!j = 0"
1507 from H have xsl: "length xs = k+1"
1508 by (simp add: natpermute_def)
1509 from i have i': "i < length (replicate (k+1) 0)" "i < k+1"
1510 unfolding length_replicate by presburger+
1511 have "xs = replicate (k+1) 0 [i := n]"
1512 apply (rule nth_equalityI)
1513 unfolding xsl length_list_update length_replicate
1516 unfolding nth_list_update[OF i'(1)]
1518 apply (case_tac "ia = i")
1519 apply (auto simp del: replicate.simps)
1521 then have "xs \<in> ?B" using i by blast
1526 assume i: "i \<in> {0..k}"
1527 let ?xs = "replicate (k+1) 0 [i:=n]"
1528 have nxs: "n \<in> set ?xs"
1529 apply (rule set_update_memI)
1532 have xsl: "length ?xs = k+1"
1533 by (simp only: length_replicate length_list_update)
1534 have "listsum ?xs = setsum (nth ?xs) {0..<k+1}"
1535 unfolding listsum_setsum_nth xsl ..
1536 also have "\<dots> = setsum (\<lambda>j. if j = i then n else 0) {0..< k+1}"
1537 by (rule setsum_cong2) (simp del: replicate.simps)
1538 also have "\<dots> = n" using i by (simp add: setsum_delta)
1539 finally have "?xs \<in> natpermute n (k+1)"
1540 using xsl unfolding natpermute_def mem_Collect_eq by blast
1541 then have "?xs \<in> ?A"
1544 ultimately show ?thesis by auto
1547 (* The general form *)
1548 lemma fps_setprod_nth:
1550 and a :: "nat \<Rightarrow> ('a::comm_ring_1) fps"
1551 shows "(setprod a {0 .. m})$n =
1552 setsum (\<lambda>v. setprod (\<lambda>j. (a j) $ (v!j)) {0..m}) (natpermute n (m+1))"
1554 proof (induct m arbitrary: n rule: nat_less_induct)
1555 fix m n assume H: "\<forall>m' < m. \<forall>n. ?P m' n"
1561 unfolding natlist_trivial_1[where n = n, unfolded One_nat_def]
1566 then have km: "k < m" by arith
1567 have u0: "{0 .. k} \<union> {m} = {0..m}"
1568 using Suc apply (simp add: set_eq_iff)
1571 have f0: "finite {0 .. k}" "finite {m}" by auto
1572 have d0: "{0 .. k} \<inter> {m} = {}" using Suc by auto
1573 have "(setprod a {0 .. m}) $ n = (setprod a {0 .. k} * a m) $ n"
1574 unfolding setprod_Un_disjoint[OF f0 d0, unfolded u0] by simp
1575 also have "\<dots> = (\<Sum>i = 0..n. (\<Sum>v\<in>natpermute i (k + 1). \<Prod>j\<in>{0..k}. a j $ v ! j) * a m $ (n - i))"
1576 unfolding fps_mult_nth H[rule_format, OF km] ..
1577 also have "\<dots> = (\<Sum>v\<in>natpermute n (m + 1). \<Prod>j\<in>{0..m}. a j $ v ! j)"
1578 apply (simp add: Suc)
1579 unfolding natpermute_split[of m "m + 1", simplified, of n,
1580 unfolded natlist_trivial_1[unfolded One_nat_def] Suc]
1581 apply (subst setsum_UN_disjoint)
1584 unfolding image_Collect[symmetric]
1586 apply (rule finite_imageI)
1587 apply (rule natpermute_finite)
1588 apply (clarsimp simp add: set_eq_iff)
1590 apply (rule setsum_cong2)
1591 unfolding setsum_left_distrib
1593 apply (rule_tac f="\<lambda>xs. xs @[n - x]" in setsum_reindex_cong)
1594 apply (simp add: inj_on_def)
1596 unfolding setprod_Un_disjoint[OF f0 d0, unfolded u0, unfolded Suc]
1597 apply (clarsimp simp add: natpermute_def nth_append)
1599 finally show ?thesis .
1603 text{* The special form for powers *}
1604 lemma fps_power_nth_Suc:
1606 and a :: "('a::comm_ring_1) fps"
1607 shows "(a ^ Suc m)$n = setsum (\<lambda>v. setprod (\<lambda>j. a $ (v!j)) {0..m}) (natpermute n (m+1))"
1609 have th0: "a^Suc m = setprod (\<lambda>i. a) {0..m}" by (simp add: setprod_constant)
1610 show ?thesis unfolding th0 fps_setprod_nth ..
1613 lemma fps_power_nth:
1614 fixes m :: nat and a :: "('a::comm_ring_1) fps"
1616 (if m=0 then 1$n else setsum (\<lambda>v. setprod (\<lambda>j. a $ (v!j)) {0..m - 1}) (natpermute n m))"
1617 by (cases m) (simp_all add: fps_power_nth_Suc del: power_Suc)
1619 lemma fps_nth_power_0:
1620 fixes m :: nat and a :: "('a::{comm_ring_1}) fps"
1621 shows "(a ^m)$0 = (a$0) ^ m"
1624 then show ?thesis by simp
1627 then have c: "m = card {0..n}" by simp
1628 have "(a ^m)$0 = setprod (\<lambda>i. a$0) {0..n}"
1629 by (simp add: Suc fps_power_nth del: replicate.simps power_Suc)
1630 also have "\<dots> = (a$0) ^ m"
1631 unfolding c by (rule setprod_constant) simp
1632 finally show ?thesis .
1635 lemma fps_compose_inj_right:
1636 assumes a0: "a$0 = (0::'a::{idom})"
1637 and a1: "a$1 \<noteq> 0"
1638 shows "(b oo a = c oo a) \<longleftrightarrow> b = c" (is "?lhs \<longleftrightarrow>?rhs")
1641 then show "?lhs" by simp
1647 proof (induct n rule: nat_less_induct)
1649 assume H: "\<forall>m<n. b$m = c$m"
1652 from h have "(b oo a)$n = (c oo a)$n" by simp
1653 hence "b$n = c$n" using n0 by (simp add: fps_compose_nth)
1657 fix n1 assume n1: "n = Suc n1"
1658 have f: "finite {0 .. n1}" "finite {n}" by simp_all
1659 have eq: "{0 .. n1} \<union> {n} = {0 .. n}" using n1 by auto
1660 have d: "{0 .. n1} \<inter> {n} = {}" using n1 by auto
1661 have seq: "(\<Sum>i = 0..n1. b $ i * a ^ i $ n) = (\<Sum>i = 0..n1. c $ i * a ^ i $ n)"
1662 apply (rule setsum_cong2)
1666 have th0: "(b oo a) $n = (\<Sum>i = 0..n1. c $ i * a ^ i $ n) + b$n * (a$1)^n"
1667 unfolding fps_compose_nth setsum_Un_disjoint[OF f d, unfolded eq] seq
1668 using startsby_zero_power_nth_same[OF a0]
1670 have th1: "(c oo a) $n = (\<Sum>i = 0..n1. c $ i * a ^ i $ n) + c$n * (a$1)^n"
1671 unfolding fps_compose_nth setsum_Un_disjoint[OF f d, unfolded eq]
1672 using startsby_zero_power_nth_same[OF a0]
1674 from h[unfolded fps_eq_iff, rule_format, of n] th0 th1 a1
1675 have "b$n = c$n" by auto
1677 ultimately show "b$n = c$n" by (cases n) auto
1679 then show ?rhs by (simp add: fps_eq_iff)
1683 subsection {* Radicals *}
1685 declare setprod_cong [fundef_cong]
1687 function radical :: "(nat \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> nat \<Rightarrow> ('a::{field}) fps \<Rightarrow> nat \<Rightarrow> 'a"
1689 "radical r 0 a 0 = 1"
1690 | "radical r 0 a (Suc n) = 0"
1691 | "radical r (Suc k) a 0 = r (Suc k) (a$0)"
1692 | "radical r (Suc k) a (Suc n) =
1693 (a$ Suc n - setsum (\<lambda>xs. setprod (\<lambda>j. radical r (Suc k) a (xs ! j)) {0..k})
1694 {xs. xs \<in> natpermute (Suc n) (Suc k) \<and> Suc n \<notin> set xs}) /
1695 (of_nat (Suc k) * (radical r (Suc k) a 0)^k)"
1696 by pat_completeness auto
1700 let ?R = "measure (\<lambda>(r, k, a, n). n)"
1702 show "wf ?R" by auto
1705 assume xs: "xs \<in> {xs \<in> natpermute (Suc n) (Suc k). Suc n \<notin> set xs}" and i: "i \<in> {0..k}"
1707 assume c: "Suc n \<le> xs ! i"
1708 from xs i have "xs !i \<noteq> Suc n"
1709 by (auto simp add: in_set_conv_nth natpermute_def)
1710 with c have c': "Suc n < xs!i" by arith
1711 have fths: "finite {0 ..< i}" "finite {i}" "finite {i+1..<Suc k}"
1713 have d: "{0 ..< i} \<inter> ({i} \<union> {i+1 ..< Suc k}) = {}" "{i} \<inter> {i+1..< Suc k} = {}"
1715 have eqs: "{0..<Suc k} = {0 ..< i} \<union> ({i} \<union> {i+1 ..< Suc k})"
1717 from xs have "Suc n = listsum xs"
1718 by (simp add: natpermute_def)
1719 also have "\<dots> = setsum (nth xs) {0..<Suc k}" using xs
1720 by (simp add: natpermute_def listsum_setsum_nth)
1721 also have "\<dots> = xs!i + setsum (nth xs) {0..<i} + setsum (nth xs) {i+1..<Suc k}"
1722 unfolding eqs setsum_Un_disjoint[OF fths(1) finite_UnI[OF fths(2,3)] d(1)]
1723 unfolding setsum_Un_disjoint[OF fths(2) fths(3) d(2)]
1725 finally have False using c' by simp
1727 then show "((r, Suc k, a, xs!i), r, Suc k, a, Suc n) \<in> ?R"
1729 apply (metis not_less)
1733 show "((r, Suc k, a, 0), r, Suc k, a, Suc n) \<in> ?R" by simp
1737 definition "fps_radical r n a = Abs_fps (radical r n a)"
1739 lemma fps_radical0[simp]: "fps_radical r 0 a = 1"
1740 apply (auto simp add: fps_eq_iff fps_radical_def)
1745 lemma fps_radical_nth_0[simp]: "fps_radical r n a $ 0 = (if n=0 then 1 else r n (a$0))"
1746 by (cases n) (simp_all add: fps_radical_def)
1748 lemma fps_radical_power_nth[simp]:
1749 assumes r: "(r k (a$0)) ^ k = a$0"
1750 shows "fps_radical r k a ^ k $ 0 = (if k = 0 then 1 else a$0)"
1753 then show ?thesis by simp
1756 have eq1: "fps_radical r k a ^ k $ 0 = (\<Prod>j\<in>{0..h}. fps_radical r k a $ (replicate k 0) ! j)"
1757 unfolding fps_power_nth Suc by simp
1758 also have "\<dots> = (\<Prod>j\<in>{0..h}. r k (a$0))"
1759 apply (rule setprod_cong)
1762 apply (subgoal_tac "replicate k (0::nat) ! x = 0")
1763 apply (auto intro: nth_replicate simp del: replicate.simps)
1765 also have "\<dots> = a$0" using r Suc by (simp add: setprod_constant)
1766 finally show ?thesis using Suc by simp
1769 lemma natpermute_max_card:
1770 assumes n0: "n\<noteq>0"
1771 shows "card {xs \<in> natpermute n (k+1). n \<in> set xs} = k + 1"
1772 unfolding natpermute_contain_maximal
1774 let ?A= "\<lambda>i. {replicate (k + 1) 0[i := n]}"
1776 have fK: "finite ?K" by simp
1777 have fAK: "\<forall>i\<in>?K. finite (?A i)" by auto
1778 have d: "\<forall>i\<in> ?K. \<forall>j\<in> ?K. i \<noteq> j \<longrightarrow>
1779 {replicate (k + 1) 0[i := n]} \<inter> {replicate (k + 1) 0[j := n]} = {}"
1782 assume i: "i \<in> ?K" and j: "j\<in> ?K" and ij: "i\<noteq>j"
1784 assume eq: "replicate (k+1) 0 [i:=n] = replicate (k+1) 0 [j:= n]"
1785 have "(replicate (k+1) 0 [i:=n] ! i) = n"
1786 using i by (simp del: replicate.simps)
1788 have "(replicate (k+1) 0 [j:=n] ! i) = 0"
1789 using i ij by (simp del: replicate.simps)
1790 ultimately have False
1791 using eq n0 by (simp del: replicate.simps)
1793 then show "{replicate (k + 1) 0[i := n]} \<inter> {replicate (k + 1) 0[j := n]} = {}"
1796 from card_UN_disjoint[OF fK fAK d] show "card (\<Union>i\<in>{0..k}. {replicate (k + 1) 0[i := n]}) = k+1"
1800 lemma power_radical:
1801 fixes a:: "'a::field_char_0 fps"
1802 assumes a0: "a$0 \<noteq> 0"
1803 shows "(r (Suc k) (a$0)) ^ Suc k = a$0 \<longleftrightarrow> (fps_radical r (Suc k) a) ^ (Suc k) = a"
1805 let ?r = "fps_radical r (Suc k) a"
1807 assume r0: "(r (Suc k) (a$0)) ^ Suc k = a$0"
1808 from a0 r0 have r00: "r (Suc k) (a$0) \<noteq> 0" by auto
1811 have "?r ^ Suc k $ z = a$z"
1812 proof (induct z rule: nat_less_induct)
1814 assume H: "\<forall>m<n. ?r ^ Suc k $ m = a$m"
1817 hence "?r ^ Suc k $ n = a $n"
1818 using fps_radical_power_nth[of r "Suc k" a, OF r0] by simp
1822 fix n1 assume n1: "n = Suc n1"
1823 have nz: "n \<noteq> 0" using n1 by arith
1824 let ?Pnk = "natpermute n (k + 1)"
1825 let ?Pnkn = "{xs \<in> ?Pnk. n \<in> set xs}"
1826 let ?Pnknn = "{xs \<in> ?Pnk. n \<notin> set xs}"
1827 have eq: "?Pnkn \<union> ?Pnknn = ?Pnk" by blast
1828 have d: "?Pnkn \<inter> ?Pnknn = {}" by blast
1829 have f: "finite ?Pnkn" "finite ?Pnknn"
1830 using finite_Un[of ?Pnkn ?Pnknn, unfolded eq]
1831 by (metis natpermute_finite)+
1832 let ?f = "\<lambda>v. \<Prod>j\<in>{0..k}. ?r $ v ! j"
1833 have "setsum ?f ?Pnkn = setsum (\<lambda>v. ?r $ n * r (Suc k) (a $ 0) ^ k) ?Pnkn"
1834 proof (rule setsum_cong2)
1835 fix v assume v: "v \<in> {xs \<in> natpermute n (k + 1). n \<in> set xs}"
1836 let ?ths = "(\<Prod>j\<in>{0..k}. fps_radical r (Suc k) a $ v ! j) =
1837 fps_radical r (Suc k) a $ n * r (Suc k) (a $ 0) ^ k"
1838 from v obtain i where i: "i \<in> {0..k}" "v = replicate (k+1) 0 [i:= n]"
1839 unfolding natpermute_contain_maximal by auto
1840 have "(\<Prod>j\<in>{0..k}. fps_radical r (Suc k) a $ v ! j) =
1841 (\<Prod>j\<in>{0..k}. if j = i then fps_radical r (Suc k) a $ n else r (Suc k) (a$0))"
1842 apply (rule setprod_cong, simp)
1844 apply (simp del: replicate.simps)
1846 also have "\<dots> = (fps_radical r (Suc k) a $ n) * r (Suc k) (a$0) ^ k"
1847 using i r0 by (simp add: setprod_gen_delta)
1850 then have "setsum ?f ?Pnkn = of_nat (k+1) * ?r $ n * r (Suc k) (a $ 0) ^ k"
1851 by (simp add: natpermute_max_card[OF nz, simplified])
1852 also have "\<dots> = a$n - setsum ?f ?Pnknn"
1853 unfolding n1 using r00 a0 by (simp add: field_simps fps_radical_def del: of_nat_Suc)
1854 finally have fn: "setsum ?f ?Pnkn = a$n - setsum ?f ?Pnknn" .
1855 have "(?r ^ Suc k)$n = setsum ?f ?Pnkn + setsum ?f ?Pnknn"
1856 unfolding fps_power_nth_Suc setsum_Un_disjoint[OF f d, unfolded eq] ..
1857 also have "\<dots> = a$n" unfolding fn by simp
1858 finally have "?r ^ Suc k $ n = a $n" .
1860 ultimately show "?r ^ Suc k $ n = a $n" by (cases n) auto
1863 then have ?thesis using r0 by (simp add: fps_eq_iff)
1867 assume h: "(fps_radical r (Suc k) a) ^ (Suc k) = a"
1868 hence "((fps_radical r (Suc k) a) ^ (Suc k))$0 = a$0" by simp
1869 then have "(r (Suc k) (a$0)) ^ Suc k = a$0"
1870 unfolding fps_power_nth_Suc
1871 by (simp add: setprod_constant del: replicate.simps)
1873 ultimately show ?thesis by blast
1877 lemma power_radical:
1878 fixes a:: "'a::field_char_0 fps"
1879 assumes r0: "(r (Suc k) (a$0)) ^ Suc k = a$0" and a0: "a$0 \<noteq> 0"
1880 shows "(fps_radical r (Suc k) a) ^ (Suc k) = a"
1882 let ?r = "fps_radical r (Suc k) a"
1883 from a0 r0 have r00: "r (Suc k) (a$0) \<noteq> 0" by auto
1884 {fix z have "?r ^ Suc k $ z = a$z"
1885 proof(induct z rule: nat_less_induct)
1886 fix n assume H: "\<forall>m<n. ?r ^ Suc k $ m = a$m"
1887 {assume "n = 0" hence "?r ^ Suc k $ n = a $n"
1888 using fps_radical_power_nth[of r "Suc k" a, OF r0] by simp}
1890 {fix n1 assume n1: "n = Suc n1"
1891 have fK: "finite {0..k}" by simp
1892 have nz: "n \<noteq> 0" using n1 by arith
1893 let ?Pnk = "natpermute n (k + 1)"
1894 let ?Pnkn = "{xs \<in> ?Pnk. n \<in> set xs}"
1895 let ?Pnknn = "{xs \<in> ?Pnk. n \<notin> set xs}"
1896 have eq: "?Pnkn \<union> ?Pnknn = ?Pnk" by blast
1897 have d: "?Pnkn \<inter> ?Pnknn = {}" by blast
1898 have f: "finite ?Pnkn" "finite ?Pnknn"
1899 using finite_Un[of ?Pnkn ?Pnknn, unfolded eq]
1900 by (metis natpermute_finite)+
1901 let ?f = "\<lambda>v. \<Prod>j\<in>{0..k}. ?r $ v ! j"
1902 have "setsum ?f ?Pnkn = setsum (\<lambda>v. ?r $ n * r (Suc k) (a $ 0) ^ k) ?Pnkn"
1903 proof(rule setsum_cong2)
1904 fix v assume v: "v \<in> {xs \<in> natpermute n (k + 1). n \<in> set xs}"
1905 let ?ths = "(\<Prod>j\<in>{0..k}. fps_radical r (Suc k) a $ v ! j) = fps_radical r (Suc k) a $ n * r (Suc k) (a $ 0) ^ k"
1906 from v obtain i where i: "i \<in> {0..k}" "v = replicate (k+1) 0 [i:= n]"
1907 unfolding natpermute_contain_maximal by auto
1908 have "(\<Prod>j\<in>{0..k}. fps_radical r (Suc k) a $ v ! j) = (\<Prod>j\<in>{0..k}. if j = i then fps_radical r (Suc k) a $ n else r (Suc k) (a$0))"
1909 apply (rule setprod_cong, simp)
1910 using i r0 by (simp del: replicate.simps)
1911 also have "\<dots> = (fps_radical r (Suc k) a $ n) * r (Suc k) (a$0) ^ k"
1912 unfolding setprod_gen_delta[OF fK] using i r0 by simp
1915 then have "setsum ?f ?Pnkn = of_nat (k+1) * ?r $ n * r (Suc k) (a $ 0) ^ k"
1916 by (simp add: natpermute_max_card[OF nz, simplified])
1917 also have "\<dots> = a$n - setsum ?f ?Pnknn"
1918 unfolding n1 using r00 a0 by (simp add: field_simps fps_radical_def del: of_nat_Suc )
1919 finally have fn: "setsum ?f ?Pnkn = a$n - setsum ?f ?Pnknn" .
1920 have "(?r ^ Suc k)$n = setsum ?f ?Pnkn + setsum ?f ?Pnknn"
1921 unfolding fps_power_nth_Suc setsum_Un_disjoint[OF f d, unfolded eq] ..
1922 also have "\<dots> = a$n" unfolding fn by simp
1923 finally have "?r ^ Suc k $ n = a $n" .}
1924 ultimately show "?r ^ Suc k $ n = a $n" by (cases n, auto)
1926 then show ?thesis by (simp add: fps_eq_iff)
1930 lemma eq_divide_imp':
1931 assumes c0: "(c::'a::field) ~= 0"
1935 from eq have "a * c * inverse c = b * inverse c"
1937 hence "a * (inverse c * c) = b/c"
1938 by (simp only: field_simps divide_inverse)
1940 unfolding field_inverse[OF c0] by simp
1943 lemma radical_unique:
1944 assumes r0: "(r (Suc k) (b$0)) ^ Suc k = b$0"
1945 and a0: "r (Suc k) (b$0 ::'a::field_char_0) = a$0"
1946 and b0: "b$0 \<noteq> 0"
1947 shows "a^(Suc k) = b \<longleftrightarrow> a = fps_radical r (Suc k) b"
1949 let ?r = "fps_radical r (Suc k) b"
1950 have r00: "r (Suc k) (b$0) \<noteq> 0" using b0 r0 by auto
1953 from H have "a^Suc k = b"
1954 using power_radical[OF b0, of r k, unfolded r0] by simp
1958 assume H: "a^Suc k = b"
1959 have ceq: "card {0..k} = Suc k" by simp
1960 from a0 have a0r0: "a$0 = ?r$0" by simp
1963 have "a $ n = ?r $ n"
1964 proof (induct n rule: nat_less_induct)
1966 assume h: "\<forall>m<n. a$m = ?r $m"
1969 hence "a$n = ?r $n" using a0 by simp
1974 assume n1: "n = Suc n1"
1975 have fK: "finite {0..k}" by simp
1976 have nz: "n \<noteq> 0" using n1 by arith
1977 let ?Pnk = "natpermute n (Suc k)"
1978 let ?Pnkn = "{xs \<in> ?Pnk. n \<in> set xs}"
1979 let ?Pnknn = "{xs \<in> ?Pnk. n \<notin> set xs}"
1980 have eq: "?Pnkn \<union> ?Pnknn = ?Pnk" by blast
1981 have d: "?Pnkn \<inter> ?Pnknn = {}" by blast
1982 have f: "finite ?Pnkn" "finite ?Pnknn"
1983 using finite_Un[of ?Pnkn ?Pnknn, unfolded eq]
1984 by (metis natpermute_finite)+
1985 let ?f = "\<lambda>v. \<Prod>j\<in>{0..k}. ?r $ v ! j"
1986 let ?g = "\<lambda>v. \<Prod>j\<in>{0..k}. a $ v ! j"
1987 have "setsum ?g ?Pnkn = setsum (\<lambda>v. a $ n * (?r$0)^k) ?Pnkn"
1988 proof (rule setsum_cong2)
1990 assume v: "v \<in> {xs \<in> natpermute n (Suc k). n \<in> set xs}"
1991 let ?ths = "(\<Prod>j\<in>{0..k}. a $ v ! j) = a $ n * (?r$0)^k"
1992 from v obtain i where i: "i \<in> {0..k}" "v = replicate (k+1) 0 [i:= n]"
1993 unfolding Suc_eq_plus1 natpermute_contain_maximal
1994 by (auto simp del: replicate.simps)
1995 have "(\<Prod>j\<in>{0..k}. a $ v ! j) = (\<Prod>j\<in>{0..k}. if j = i then a $ n else r (Suc k) (b$0))"
1996 apply (rule setprod_cong, simp)
1997 using i a0 apply (simp del: replicate.simps)
1999 also have "\<dots> = a $ n * (?r $ 0)^k"
2000 using i by (simp add: setprod_gen_delta)
2003 then have th0: "setsum ?g ?Pnkn = of_nat (k+1) * a $ n * (?r $ 0)^k"
2004 by (simp add: natpermute_max_card[OF nz, simplified])
2005 have th1: "setsum ?g ?Pnknn = setsum ?f ?Pnknn"
2006 proof (rule setsum_cong2, rule setprod_cong, simp)
2008 assume xs: "xs \<in> ?Pnknn" and i: "i \<in> {0..k}"
2010 assume c: "n \<le> xs ! i"
2011 from xs i have "xs !i \<noteq> n"
2012 by (auto simp add: in_set_conv_nth natpermute_def)
2013 with c have c': "n < xs!i" by arith
2014 have fths: "finite {0 ..< i}" "finite {i}" "finite {i+1..<Suc k}"
2016 have d: "{0 ..< i} \<inter> ({i} \<union> {i+1 ..< Suc k}) = {}" "{i} \<inter> {i+1..< Suc k} = {}"
2018 have eqs: "{0..<Suc k} = {0 ..< i} \<union> ({i} \<union> {i+1 ..< Suc k})"
2020 from xs have "n = listsum xs"
2021 by (simp add: natpermute_def)
2022 also have "\<dots> = setsum (nth xs) {0..<Suc k}"
2023 using xs by (simp add: natpermute_def listsum_setsum_nth)
2024 also have "\<dots> = xs!i + setsum (nth xs) {0..<i} + setsum (nth xs) {i+1..<Suc k}"
2025 unfolding eqs setsum_Un_disjoint[OF fths(1) finite_UnI[OF fths(2,3)] d(1)]
2026 unfolding setsum_Un_disjoint[OF fths(2) fths(3) d(2)]
2028 finally have False using c' by simp
2030 then have thn: "xs!i < n" by presburger
2031 from h[rule_format, OF thn] show "a$(xs !i) = ?r$(xs!i)" .
2033 have th00: "\<And>(x::'a). of_nat (Suc k) * (x * inverse (of_nat (Suc k))) = x"
2034 by (simp add: field_simps del: of_nat_Suc)
2035 from H have "b$n = a^Suc k $ n"
2036 by (simp add: fps_eq_iff)
2037 also have "a ^ Suc k$n = setsum ?g ?Pnkn + setsum ?g ?Pnknn"
2038 unfolding fps_power_nth_Suc
2039 using setsum_Un_disjoint[OF f d, unfolded Suc_eq_plus1[symmetric],
2040 unfolded eq, of ?g] by simp
2041 also have "\<dots> = of_nat (k+1) * a $ n * (?r $ 0)^k + setsum ?f ?Pnknn"
2042 unfolding th0 th1 ..
2043 finally have "of_nat (k+1) * a $ n * (?r $ 0)^k = b$n - setsum ?f ?Pnknn"
2045 then have "a$n = (b$n - setsum ?f ?Pnknn) / (of_nat (k+1) * (?r $ 0)^k)"
2047 apply (rule eq_divide_imp')
2049 apply (simp del: of_nat_Suc)
2050 apply (simp add: mult_ac)
2052 then have "a$n = ?r $n"
2053 apply (simp del: of_nat_Suc)
2054 unfolding fps_radical_def n1
2055 apply (simp add: field_simps n1 th00 del: of_nat_Suc)
2058 ultimately show "a$n = ?r $ n" by (cases n) auto
2061 then have "a = ?r" by (simp add: fps_eq_iff)
2063 ultimately show ?thesis by blast
2067 lemma radical_power:
2068 assumes r0: "r (Suc k) ((a$0) ^ Suc k) = a$0"
2069 and a0: "(a$0 ::'a::field_char_0) \<noteq> 0"
2070 shows "(fps_radical r (Suc k) (a ^ Suc k)) = a"
2072 let ?ak = "a^ Suc k"
2073 have ak0: "?ak $ 0 = (a$0) ^ Suc k"
2074 by (simp add: fps_nth_power_0 del: power_Suc)
2075 from r0 have th0: "r (Suc k) (a ^ Suc k $ 0) ^ Suc k = a ^ Suc k $ 0"
2077 from r0 ak0 have th1: "r (Suc k) (a ^ Suc k $ 0) = a $ 0"
2079 from ak0 a0 have ak00: "?ak $ 0 \<noteq>0 "
2081 from radical_unique[of r k ?ak a, OF th0 th1 ak00] show ?thesis
2085 lemma fps_deriv_radical:
2086 fixes a:: "'a::field_char_0 fps"
2087 assumes r0: "(r (Suc k) (a$0)) ^ Suc k = a$0"
2088 and a0: "a$0 \<noteq> 0"
2089 shows "fps_deriv (fps_radical r (Suc k) a) =
2090 fps_deriv a / (fps_const (of_nat (Suc k)) * (fps_radical r (Suc k) a) ^ k)"
2092 let ?r = "fps_radical r (Suc k) a"
2093 let ?w = "(fps_const (of_nat (Suc k)) * ?r ^ k)"
2094 from a0 r0 have r0': "r (Suc k) (a$0) \<noteq> 0"
2096 from r0' have w0: "?w $ 0 \<noteq> 0"
2097 by (simp del: of_nat_Suc)
2098 note th0 = inverse_mult_eq_1[OF w0]
2099 let ?iw = "inverse ?w"
2100 from iffD1[OF power_radical[of a r], OF a0 r0]
2101 have "fps_deriv (?r ^ Suc k) = fps_deriv a"
2103 hence "fps_deriv ?r * ?w = fps_deriv a"
2104 by (simp add: fps_deriv_power mult_ac del: power_Suc)
2105 hence "?iw * fps_deriv ?r * ?w = ?iw * fps_deriv a"
2107 hence "fps_deriv ?r * (?iw * ?w) = fps_deriv a / ?w"
2108 by (simp add: fps_divide_def)
2109 then show ?thesis unfolding th0 by simp
2112 lemma radical_mult_distrib:
2113 fixes a:: "'a::field_char_0 fps"
2115 and ra0: "r k (a $ 0) ^ k = a $ 0"
2116 and rb0: "r k (b $ 0) ^ k = b $ 0"
2117 and a0: "a$0 \<noteq> 0"
2118 and b0: "b$0 \<noteq> 0"
2119 shows "r k ((a * b) $ 0) = r k (a $ 0) * r k (b $ 0) \<longleftrightarrow>
2120 fps_radical r (k) (a*b) = fps_radical r (k) a * fps_radical r (k) (b)"
2123 assume r0': "r k ((a * b) $ 0) = r k (a $ 0) * r k (b $ 0)"
2124 from r0' have r0: "(r (k) ((a*b)$0)) ^ k = (a*b)$0"
2125 by (simp add: fps_mult_nth ra0 rb0 power_mult_distrib)
2128 hence ?thesis using r0' by simp
2132 fix h assume k: "k = Suc h"
2133 let ?ra = "fps_radical r (Suc h) a"
2134 let ?rb = "fps_radical r (Suc h) b"
2135 have th0: "r (Suc h) ((a * b) $ 0) = (fps_radical r (Suc h) a * fps_radical r (Suc h) b) $ 0"
2136 using r0' k by (simp add: fps_mult_nth)
2137 have ab0: "(a*b) $ 0 \<noteq> 0"
2138 using a0 b0 by (simp add: fps_mult_nth)
2139 from radical_unique[of r h "a*b" "fps_radical r (Suc h) a * fps_radical r (Suc h) b", OF r0[unfolded k] th0 ab0, symmetric]
2140 iffD1[OF power_radical[of _ r], OF a0 ra0[unfolded k]] iffD1[OF power_radical[of _ r], OF b0 rb0[unfolded k]] k r0'
2141 have ?thesis by (auto simp add: power_mult_distrib simp del: power_Suc)
2143 ultimately have ?thesis by (cases k) auto
2147 assume h: "fps_radical r k (a*b) = fps_radical r k a * fps_radical r k b"
2148 hence "(fps_radical r k (a*b))$0 = (fps_radical r k a * fps_radical r k b)$0"
2150 then have "r k ((a * b) $ 0) = r k (a $ 0) * r k (b $ 0)"
2151 using k by (simp add: fps_mult_nth)
2153 ultimately show ?thesis by blast
2157 lemma radical_mult_distrib:
2158 fixes a:: "'a::field_char_0 fps"
2160 ra0: "r k (a $ 0) ^ k = a $ 0"
2161 and rb0: "r k (b $ 0) ^ k = b $ 0"
2162 and r0': "r k ((a * b) $ 0) = r k (a $ 0) * r k (b $ 0)"
2163 and a0: "a$0 \<noteq> 0"
2164 and b0: "b$0 \<noteq> 0"
2165 shows "fps_radical r (k) (a*b) = fps_radical r (k) a * fps_radical r (k) (b)"
2167 from r0' have r0: "(r (k) ((a*b)$0)) ^ k = (a*b)$0"
2168 by (simp add: fps_mult_nth ra0 rb0 power_mult_distrib)
2169 {assume "k=0" hence ?thesis by simp}
2171 {fix h assume k: "k = Suc h"
2172 let ?ra = "fps_radical r (Suc h) a"
2173 let ?rb = "fps_radical r (Suc h) b"
2174 have th0: "r (Suc h) ((a * b) $ 0) = (fps_radical r (Suc h) a * fps_radical r (Suc h) b) $ 0"
2175 using r0' k by (simp add: fps_mult_nth)
2176 have ab0: "(a*b) $ 0 \<noteq> 0" using a0 b0 by (simp add: fps_mult_nth)
2177 from radical_unique[of r h "a*b" "fps_radical r (Suc h) a * fps_radical r (Suc h) b", OF r0[unfolded k] th0 ab0, symmetric]
2178 power_radical[of r, OF ra0[unfolded k] a0] power_radical[of r, OF rb0[unfolded k] b0] k
2179 have ?thesis by (auto simp add: power_mult_distrib simp del: power_Suc)}
2180 ultimately show ?thesis by (cases k, auto)
2184 lemma fps_divide_1[simp]: "(a:: ('a::field) fps) / 1 = a"
2185 by (simp add: fps_divide_def)
2187 lemma radical_divide:
2188 fixes a :: "'a::field_char_0 fps"
2190 and ra0: "(r k (a $ 0)) ^ k = a $ 0"
2191 and rb0: "(r k (b $ 0)) ^ k = b $ 0"
2192 and a0: "a$0 \<noteq> 0"
2193 and b0: "b$0 \<noteq> 0"
2194 shows "r k ((a $ 0) / (b$0)) = r k (a$0) / r k (b $ 0) \<longleftrightarrow>
2195 fps_radical r k (a/b) = fps_radical r k a / fps_radical r k b"
2198 let ?r = "fps_radical r k"
2199 from kp obtain h where k: "k = Suc h" by (cases k) auto
2200 have ra0': "r k (a$0) \<noteq> 0" using a0 ra0 k by auto
2201 have rb0': "r k (b$0) \<noteq> 0" using b0 rb0 k by auto
2205 then have "?r (a/b) $ 0 = (?r a / ?r b)$0" by simp
2206 then have ?lhs using k a0 b0 rb0'
2207 by (simp add: fps_divide_def fps_mult_nth fps_inverse_def divide_inverse)
2212 from a0 b0 have ab0[simp]: "(a/b)$0 = a$0 / b$0"
2213 by (simp add: fps_divide_def fps_mult_nth divide_inverse fps_inverse_def)
2214 have th0: "r k ((a/b)$0) ^ k = (a/b)$0"
2215 by (simp add: h nonzero_power_divide[OF rb0'] ra0 rb0)
2216 from a0 b0 ra0' rb0' kp h
2217 have th1: "r k ((a / b) $ 0) = (fps_radical r k a / fps_radical r k b) $ 0"
2218 by (simp add: fps_divide_def fps_mult_nth fps_inverse_def divide_inverse)
2219 from a0 b0 ra0' rb0' kp have ab0': "(a / b) $ 0 \<noteq> 0"
2220 by (simp add: fps_divide_def fps_mult_nth fps_inverse_def nonzero_imp_inverse_nonzero)
2221 note tha[simp] = iffD1[OF power_radical[where r=r and k=h], OF a0 ra0[unfolded k], unfolded k[symmetric]]
2222 note thb[simp] = iffD1[OF power_radical[where r=r and k=h], OF b0 rb0[unfolded k], unfolded k[symmetric]]
2223 have th2: "(?r a / ?r b)^k = a/b"
2224 by (simp add: fps_divide_def power_mult_distrib fps_inverse_power[symmetric])
2225 from iffD1[OF radical_unique[where r=r and a="?r a / ?r b" and b="a/b" and k=h], symmetric, unfolded k[symmetric], OF th0 th1 ab0' th2]
2228 ultimately show ?thesis by blast
2231 lemma radical_inverse:
2232 fixes a :: "'a::field_char_0 fps"
2234 and ra0: "r k (a $ 0) ^ k = a $ 0"
2235 and r1: "(r k 1)^k = 1"
2236 and a0: "a$0 \<noteq> 0"
2237 shows "r k (inverse (a $ 0)) = r k 1 / (r k (a $ 0)) \<longleftrightarrow>
2238 fps_radical r k (inverse a) = fps_radical r k 1 / fps_radical r k a"
2239 using radical_divide[where k=k and r=r and a=1 and b=a, OF k ] ra0 r1 a0
2240 by (simp add: divide_inverse fps_divide_def)
2242 subsection{* Derivative of composition *}
2244 lemma fps_compose_deriv:
2245 fixes a:: "('a::idom) fps"
2246 assumes b0: "b$0 = 0"
2247 shows "fps_deriv (a oo b) = ((fps_deriv a) oo b) * (fps_deriv b)"
2251 have "(fps_deriv (a oo b))$n = setsum (\<lambda>i. a $ i * (fps_deriv (b^i))$n) {0.. Suc n}"
2252 by (simp add: fps_compose_def field_simps setsum_right_distrib del: of_nat_Suc)
2253 also have "\<dots> = setsum (\<lambda>i. a$i * ((fps_const (of_nat i)) * (fps_deriv b * (b^(i - 1))))$n) {0.. Suc n}"
2254 by (simp add: field_simps fps_deriv_power del: fps_mult_left_const_nth of_nat_Suc)
2255 also have "\<dots> = setsum (\<lambda>i. of_nat i * a$i * (((b^(i - 1)) * fps_deriv b))$n) {0.. Suc n}"
2256 unfolding fps_mult_left_const_nth by (simp add: field_simps)
2257 also have "\<dots> = setsum (\<lambda>i. of_nat i * a$i * (setsum (\<lambda>j. (b^ (i - 1))$j * (fps_deriv b)$(n - j)) {0..n})) {0.. Suc n}"
2258 unfolding fps_mult_nth ..
2259 also have "\<dots> = setsum (\<lambda>i. of_nat i * a$i * (setsum (\<lambda>j. (b^ (i - 1))$j * (fps_deriv b)$(n - j)) {0..n})) {1.. Suc n}"
2260 apply (rule setsum_mono_zero_right)
2261 apply (auto simp add: mult_delta_left setsum_delta not_le)
2263 also have "\<dots> = setsum (\<lambda>i. of_nat (i + 1) * a$(i+1) * (setsum (\<lambda>j. (b^ i)$j * of_nat (n - j + 1) * b$(n - j + 1)) {0..n})) {0.. n}"
2264 unfolding fps_deriv_nth
2265 by (rule setsum_reindex_cong [where f = Suc]) (auto simp add: mult_assoc)
2266 finally have th0: "(fps_deriv (a oo b))$n =
2267 setsum (\<lambda>i. of_nat (i + 1) * a$(i+1) * (setsum (\<lambda>j. (b^ i)$j * of_nat (n - j + 1) * b$(n - j + 1)) {0..n})) {0.. n}" .
2269 have "(((fps_deriv a) oo b) * (fps_deriv b))$n = setsum (\<lambda>i. (fps_deriv b)$ (n - i) * ((fps_deriv a) oo b)$i) {0..n}"
2270 unfolding fps_mult_nth by (simp add: mult_ac)
2271 also have "\<dots> = setsum (\<lambda>i. setsum (\<lambda>j. of_nat (n - i +1) * b$(n - i + 1) * of_nat (j + 1) * a$(j+1) * (b^j)$i) {0..n}) {0..n}"
2272 unfolding fps_deriv_nth fps_compose_nth setsum_right_distrib mult_assoc
2273 apply (rule setsum_cong2)
2274 apply (rule setsum_mono_zero_left)
2275 apply (simp_all add: subset_eq)
2277 apply (subgoal_tac "b^i$x = 0")
2279 apply (rule startsby_zero_power_prefix[OF b0, rule_format])
2282 also have "\<dots> = setsum (\<lambda>i. of_nat (i + 1) * a$(i+1) * (setsum (\<lambda>j. (b^ i)$j * of_nat (n - j + 1) * b$(n - j + 1)) {0..n})) {0.. n}"
2283 unfolding setsum_right_distrib
2284 apply (subst setsum_commute)
2285 apply (rule setsum_cong2)+
2288 finally have "(fps_deriv (a oo b))$n = (((fps_deriv a) oo b) * (fps_deriv b)) $n"
2289 unfolding th0 by simp
2291 then show ?thesis by (simp add: fps_eq_iff)
2294 lemma fps_mult_X_plus_1_nth:
2295 "((1+X)*a) $n = (if n = 0 then (a$n :: 'a::comm_ring_1) else a$n + a$(n - 1))"
2298 then show ?thesis by (simp add: fps_mult_nth )
2301 have "((1+X)*a) $n = setsum (\<lambda>i. (1+X)$i * a$(n-i)) {0..n}"
2302 by (simp add: fps_mult_nth)
2303 also have "\<dots> = setsum (\<lambda>i. (1+X)$i * a$(n-i)) {0.. 1}"
2304 unfolding Suc by (rule setsum_mono_zero_right) auto
2305 also have "\<dots> = (if n = 0 then (a$n :: 'a::comm_ring_1) else a$n + a$(n - 1))"
2307 finally show ?thesis .
2310 subsection{* Finite FPS (i.e. polynomials) and X *}
2312 lemma fps_poly_sum_X:
2313 assumes z: "\<forall>i > n. a$i = (0::'a::comm_ring_1)"
2314 shows "a = setsum (\<lambda>i. fps_const (a$i) * X^i) {0..n}" (is "a = ?r")
2319 unfolding fps_setsum_nth fps_mult_left_const_nth X_power_nth
2320 by (simp add: mult_delta_right setsum_delta' z)
2322 then show ?thesis unfolding fps_eq_iff by blast
2326 subsection{* Compositional inverses *}
2328 fun compinv :: "'a fps \<Rightarrow> nat \<Rightarrow> 'a::{field}"
2331 | "compinv a (Suc n) =
2332 (X$ Suc n - setsum (\<lambda>i. (compinv a i) * (a^i)$Suc n) {0 .. n}) / (a$1) ^ Suc n"
2334 definition "fps_inv a = Abs_fps (compinv a)"
2337 assumes a0: "a$0 = 0"
2338 and a1: "a$1 \<noteq> 0"
2339 shows "fps_inv a oo a = X"
2341 let ?i = "fps_inv a oo a"
2345 proof (induct n rule: nat_less_induct)
2347 assume h: "\<forall>m<n. ?i$m = X$m"
2351 then show ?thesis using a0
2352 by (simp add: fps_compose_nth fps_inv_def)
2355 have "?i $ n = setsum (\<lambda>i. (fps_inv a $ i) * (a^i)$n) {0 .. n1} + fps_inv a $ Suc n1 * (a $ 1)^ Suc n1"
2356 by (simp add: fps_compose_nth Suc startsby_zero_power_nth_same[OF a0] del: power_Suc)
2357 also have "\<dots> = setsum (\<lambda>i. (fps_inv a $ i) * (a^i)$n) {0 .. n1} +
2358 (X$ Suc n1 - setsum (\<lambda>i. (fps_inv a $ i) * (a^i)$n) {0 .. n1})"
2359 using a0 a1 Suc by (simp add: fps_inv_def)
2360 also have "\<dots> = X$n" using Suc by simp
2361 finally show ?thesis .
2365 then show ?thesis by (simp add: fps_eq_iff)
2369 fun gcompinv :: "'a fps \<Rightarrow> 'a fps \<Rightarrow> nat \<Rightarrow> 'a::{field}"
2371 "gcompinv b a 0 = b$0"
2372 | "gcompinv b a (Suc n) =
2373 (b$ Suc n - setsum (\<lambda>i. (gcompinv b a i) * (a^i)$Suc n) {0 .. n}) / (a$1) ^ Suc n"
2375 definition "fps_ginv b a = Abs_fps (gcompinv b a)"
2378 assumes a0: "a$0 = 0"
2379 and a1: "a$1 \<noteq> 0"
2380 shows "fps_ginv b a oo a = b"
2382 let ?i = "fps_ginv b a oo a"
2386 proof (induct n rule: nat_less_induct)
2388 assume h: "\<forall>m<n. ?i$m = b$m"
2392 then show ?thesis using a0
2393 by (simp add: fps_compose_nth fps_ginv_def)
2396 have "?i $ n = setsum (\<lambda>i. (fps_ginv b a $ i) * (a^i)$n) {0 .. n1} + fps_ginv b a $ Suc n1 * (a $ 1)^ Suc n1"
2397 by (simp add: fps_compose_nth Suc startsby_zero_power_nth_same[OF a0] del: power_Suc)
2398 also have "\<dots> = setsum (\<lambda>i. (fps_ginv b a $ i) * (a^i)$n) {0 .. n1} +
2399 (b$ Suc n1 - setsum (\<lambda>i. (fps_ginv b a $ i) * (a^i)$n) {0 .. n1})"
2400 using a0 a1 Suc by (simp add: fps_ginv_def)
2401 also have "\<dots> = b$n" using Suc by simp
2402 finally show ?thesis .
2406 then show ?thesis by (simp add: fps_eq_iff)
2409 lemma fps_inv_ginv: "fps_inv = fps_ginv X"
2410 apply (auto simp add: fun_eq_iff fps_eq_iff fps_inv_def fps_ginv_def)
2411 apply (induct_tac n rule: nat_less_induct)
2418 lemma fps_compose_1[simp]: "1 oo a = 1"
2419 by (simp add: fps_eq_iff fps_compose_nth mult_delta_left setsum_delta)
2421 lemma fps_compose_0[simp]: "0 oo a = 0"
2422 by (simp add: fps_eq_iff fps_compose_nth)
2424 lemma fps_compose_0_right[simp]: "a oo 0 = fps_const (a$0)"
2425 by (auto simp add: fps_eq_iff fps_compose_nth power_0_left setsum_0')
2427 lemma fps_compose_add_distrib: "(a + b) oo c = (a oo c) + (b oo c)"
2428 by (simp add: fps_eq_iff fps_compose_nth field_simps setsum_addf)
2430 lemma fps_compose_setsum_distrib: "(setsum f S) oo a = setsum (\<lambda>i. f i oo a) S"
2431 proof (cases "finite S")
2434 proof (rule finite_induct[OF True])
2435 show "setsum f {} oo a = (\<Sum>i\<in>{}. f i oo a)" by simp
2438 assume fF: "finite F"
2439 and xF: "x \<notin> F"
2440 and h: "setsum f F oo a = setsum (\<lambda>i. f i oo a) F"
2441 show "setsum f (insert x F) oo a = setsum (\<lambda>i. f i oo a) (insert x F)"
2442 using fF xF h by (simp add: fps_compose_add_distrib)
2446 then show ?thesis by simp
2449 lemma convolution_eq:
2450 "setsum (%i. a (i :: nat) * b (n - i)) {0 .. n} = setsum (%(i,j). a i * b j) {(i,j). i <= n \<and> j \<le> n \<and> i + j = n}"
2451 apply (rule setsum_reindex_cong[where f=fst])
2452 apply (clarsimp simp add: inj_on_def)
2453 apply (auto simp add: set_eq_iff image_iff)
2454 apply (rule_tac x= "x" in exI)
2456 apply (rule_tac x="n - x" in exI)
2460 lemma product_composition_lemma:
2461 assumes c0: "c$0 = (0::'a::idom)"
2463 shows "((a oo c) * (b oo d))$n =
2464 setsum (%(k,m). a$k * b$m * (c^k * d^m) $ n) {(k,m). k + m \<le> n}" (is "?l = ?r")
2466 let ?S = "{(k\<Colon>nat, m\<Colon>nat). k + m \<le> n}"
2467 have s: "?S \<subseteq> {0..n} <*> {0..n}" by (auto simp add: subset_eq)
2468 have f: "finite {(k\<Colon>nat, m\<Colon>nat). k + m \<le> n}"
2469 apply (rule finite_subset[OF s])
2472 have "?r = setsum (%i. setsum (%(k,m). a$k * (c^k)$i * b$m * (d^m) $ (n - i)) {(k,m). k + m \<le> n}) {0..n}"
2473 apply (simp add: fps_mult_nth setsum_right_distrib)
2474 apply (subst setsum_commute)
2475 apply (rule setsum_cong2)
2476 apply (auto simp add: field_simps)
2478 also have "\<dots> = ?l"
2479 apply (simp add: fps_mult_nth fps_compose_nth setsum_product)
2480 apply (rule setsum_cong2)
2481 apply (simp add: setsum_cartesian_product mult_assoc)
2482 apply (rule setsum_mono_zero_right[OF f])
2483 apply (simp add: subset_eq) apply presburger
2486 apply (clarsimp simp add: not_le)
2487 apply (case_tac "x < aa")
2489 apply (frule_tac startsby_zero_power_prefix[rule_format, OF c0])
2492 apply (frule_tac startsby_zero_power_prefix[rule_format, OF d0])
2495 finally show ?thesis by simp
2498 lemma product_composition_lemma':
2499 assumes c0: "c$0 = (0::'a::idom)"
2501 shows "((a oo c) * (b oo d))$n =
2502 setsum (%k. setsum (%m. a$k * b$m * (c^k * d^m) $ n) {0..n}) {0..n}" (is "?l = ?r")
2503 unfolding product_composition_lemma[OF c0 d0]
2504 unfolding setsum_cartesian_product
2505 apply (rule setsum_mono_zero_left)
2507 apply (clarsimp simp add: subset_eq)
2510 apply (subgoal_tac "(c^aa * d^ba) $ n = 0")
2512 unfolding fps_mult_nth
2513 apply (rule setsum_0')
2514 apply (clarsimp simp add: not_le)
2515 apply (case_tac "x < aa")
2516 apply (rule startsby_zero_power_prefix[OF c0, rule_format])
2518 apply (subgoal_tac "n - x < ba")
2519 apply (frule_tac k = "ba" in startsby_zero_power_prefix[OF d0, rule_format])
2525 lemma setsum_pair_less_iff:
2526 "setsum (%((k::nat),m). a k * b m * c (k + m)) {(k,m). k + m \<le> n} =
2527 setsum (%s. setsum (%i. a i * b (s - i) * c s) {0..s}) {0..n}"
2530 let ?KM = "{(k,m). k + m \<le> n}"
2531 let ?f = "%s. UNION {(0::nat)..s} (%i. {(i,s - i)})"
2532 have th0: "?KM = UNION {0..n} ?f"
2533 apply (simp add: set_eq_iff)
2534 apply presburger (* FIXME: slow! *)
2538 apply (subst setsum_UN_disjoint)
2540 apply (subst setsum_UN_disjoint)
2545 lemma fps_compose_mult_distrib_lemma:
2546 assumes c0: "c$0 = (0::'a::idom)"
2547 shows "((a oo c) * (b oo c))$n =
2548 setsum (%s. setsum (%i. a$i * b$(s - i) * (c^s) $ n) {0..s}) {0..n}"
2550 unfolding product_composition_lemma[OF c0 c0] power_add[symmetric]
2551 unfolding setsum_pair_less_iff[where a = "%k. a$k" and b="%m. b$m" and c="%s. (c ^ s)$n" and n = n] ..
2554 lemma fps_compose_mult_distrib:
2555 assumes c0: "c$0 = (0::'a::idom)"
2556 shows "(a * b) oo c = (a oo c) * (b oo c)" (is "?l = ?r")
2557 apply (simp add: fps_eq_iff fps_compose_mult_distrib_lemma[OF c0])
2558 apply (simp add: fps_compose_nth fps_mult_nth setsum_left_distrib)
2561 lemma fps_compose_setprod_distrib:
2562 assumes c0: "c$0 = (0::'a::idom)"
2563 shows "(setprod a S) oo c = setprod (%k. a k oo c) S" (is "?l = ?r")
2564 apply (cases "finite S")
2566 apply (induct S rule: finite_induct)
2568 apply (simp add: fps_compose_mult_distrib[OF c0])
2571 lemma fps_compose_power:
2572 assumes c0: "c$0 = (0::'a::idom)"
2573 shows "(a oo c)^n = a^n oo c"
2577 then show ?thesis by simp
2580 have th0: "a^n = setprod (%k. a) {0..m}" "(a oo c) ^ n = setprod (%k. a oo c) {0..m}"
2581 by (simp_all add: setprod_constant Suc)
2583 by (simp add: fps_compose_setprod_distrib[OF c0])
2586 lemma fps_compose_uminus: "- (a::'a::ring_1 fps) oo c = - (a oo c)"
2587 by (simp add: fps_eq_iff fps_compose_nth field_simps setsum_negf[symmetric])
2589 lemma fps_compose_sub_distrib: "(a - b) oo (c::'a::ring_1 fps) = (a oo c) - (b oo c)"
2590 using fps_compose_add_distrib [of a "- b" c] by (simp add: fps_compose_uminus)
2592 lemma X_fps_compose: "X oo a = Abs_fps (\<lambda>n. if n = 0 then (0::'a::comm_ring_1) else a$n)"
2593 by (simp add: fps_eq_iff fps_compose_nth mult_delta_left setsum_delta)
2595 lemma fps_inverse_compose:
2596 assumes b0: "(b$0 :: 'a::field) = 0"
2597 and a0: "a$0 \<noteq> 0"
2598 shows "inverse a oo b = inverse (a oo b)"
2600 let ?ia = "inverse a"
2602 let ?iab = "inverse ?ab"
2604 from a0 have ia0: "?ia $ 0 \<noteq> 0" by simp
2605 from a0 have ab0: "?ab $ 0 \<noteq> 0" by (simp add: fps_compose_def)
2606 have "(?ia oo b) * (a oo b) = 1"
2607 unfolding fps_compose_mult_distrib[OF b0, symmetric]
2608 unfolding inverse_mult_eq_1[OF a0]
2611 then have "(?ia oo b) * (a oo b) * ?iab = 1 * ?iab" by simp
2612 then have "(?ia oo b) * (?iab * (a oo b)) = ?iab" by simp
2613 then show ?thesis unfolding inverse_mult_eq_1[OF ab0] by simp
2616 lemma fps_divide_compose:
2617 assumes c0: "(c$0 :: 'a::field) = 0"
2618 and b0: "b$0 \<noteq> 0"
2619 shows "(a/b) oo c = (a oo c) / (b oo c)"
2620 unfolding fps_divide_def fps_compose_mult_distrib[OF c0]
2621 fps_inverse_compose[OF c0 b0] ..
2624 assumes a0: "a$0 = (0::'a::field)"
2625 shows "(Abs_fps (\<lambda>n. 1)) oo a = 1/(1 - a)"
2626 (is "?one oo a = _")
2628 have o0: "?one $ 0 \<noteq> 0" by simp
2629 have th0: "(1 - X) $ 0 \<noteq> (0::'a)" by simp
2630 from fps_inverse_gp[where ?'a = 'a]
2631 have "inverse ?one = 1 - X" by (simp add: fps_eq_iff)
2632 hence "inverse (inverse ?one) = inverse (1 - X)" by simp
2633 hence th: "?one = 1/(1 - X)" unfolding fps_inverse_idempotent[OF o0]
2634 by (simp add: fps_divide_def)
2637 unfolding fps_divide_compose[OF a0 th0]
2638 fps_compose_1 fps_compose_sub_distrib X_fps_compose_startby0[OF a0] ..
2641 lemma fps_const_power [simp]: "fps_const (c::'a::ring_1) ^ n = fps_const (c^n)"
2644 lemma fps_compose_radical:
2645 assumes b0: "b$0 = (0::'a::field_char_0)"
2646 and ra0: "r (Suc k) (a$0) ^ Suc k = a$0"
2647 and a0: "a$0 \<noteq> 0"
2648 shows "fps_radical r (Suc k) a oo b = fps_radical r (Suc k) (a oo b)"
2650 let ?r = "fps_radical r (Suc k)"
2652 have ab0: "?ab $ 0 = a$0"
2653 by (simp add: fps_compose_def)
2654 from ab0 a0 ra0 have rab0: "?ab $ 0 \<noteq> 0" "r (Suc k) (?ab $ 0) ^ Suc k = ?ab $ 0"
2656 have th00: "r (Suc k) ((a oo b) $ 0) = (fps_radical r (Suc k) a oo b) $ 0"
2657 by (simp add: ab0 fps_compose_def)
2658 have th0: "(?r a oo b) ^ (Suc k) = a oo b"
2659 unfolding fps_compose_power[OF b0]
2660 unfolding iffD1[OF power_radical[of a r k], OF a0 ra0] ..
2661 from iffD1[OF radical_unique[where r=r and k=k and b= ?ab and a = "?r a oo b", OF rab0(2) th00 rab0(1)], OF th0]
2665 lemma fps_const_mult_apply_left: "fps_const c * (a oo b) = (fps_const c * a) oo b"
2666 by (simp add: fps_eq_iff fps_compose_nth setsum_right_distrib mult_assoc)
2668 lemma fps_const_mult_apply_right:
2669 "(a oo b) * fps_const (c::'a::comm_semiring_1) = (fps_const c * a) oo b"
2670 by (auto simp add: fps_const_mult_apply_left mult_commute)
2672 lemma fps_compose_assoc:
2673 assumes c0: "c$0 = (0::'a::idom)"
2675 shows "a oo (b oo c) = a oo b oo c" (is "?l = ?r")
2679 have "?l$n = (setsum (\<lambda>i. (fps_const (a$i) * b^i) oo c) {0..n})$n"
2680 by (simp add: fps_compose_nth fps_compose_power[OF c0] fps_const_mult_apply_left
2681 setsum_right_distrib mult_assoc fps_setsum_nth)
2682 also have "\<dots> = ((setsum (\<lambda>i. fps_const (a$i) * b^i) {0..n}) oo c)$n"
2683 by (simp add: fps_compose_setsum_distrib)
2684 also have "\<dots> = ?r$n"
2685 apply (simp add: fps_compose_nth fps_setsum_nth setsum_left_distrib mult_assoc)
2686 apply (rule setsum_cong2)
2687 apply (rule setsum_mono_zero_right)
2688 apply (auto simp add: not_le)
2689 apply (erule startsby_zero_power_prefix[OF b0, rule_format])
2691 finally have "?l$n = ?r$n" .
2693 then show ?thesis by (simp add: fps_eq_iff)
2697 lemma fps_X_power_compose:
2699 shows "X^k oo a = (a::('a::idom fps))^k" (is "?l = ?r")
2702 then show ?thesis by simp
2709 hence "?l $ n = ?r $n" using a0 startsby_zero_power_prefix[OF a0] Suc
2710 by (simp add: fps_compose_nth del: power_Suc)
2714 assume kn: "k \<le> n"
2716 by (simp add: fps_compose_nth mult_delta_left setsum_delta)
2718 moreover have "k >n \<or> k\<le> n" by arith
2719 ultimately have "?l$n = ?r$n" by blast
2721 then show ?thesis unfolding fps_eq_iff by blast
2724 lemma fps_inv_right:
2725 assumes a0: "a$0 = 0"
2726 and a1: "a$1 \<noteq> 0"
2727 shows "a oo fps_inv a = X"
2729 let ?ia = "fps_inv a"
2730 let ?iaa = "a oo fps_inv a"
2731 have th0: "?ia $ 0 = 0" by (simp add: fps_inv_def)
2732 have th1: "?iaa $ 0 = 0" using a0 a1
2733 by (simp add: fps_inv_def fps_compose_nth)
2734 have th2: "X$0 = 0" by simp
2735 from fps_inv[OF a0 a1] have "a oo (fps_inv a oo a) = a oo X" by simp
2736 then have "(a oo fps_inv a) oo a = X oo a"
2737 by (simp add: fps_compose_assoc[OF a0 th0] X_fps_compose_startby0[OF a0])
2738 with fps_compose_inj_right[OF a0 a1]
2739 show ?thesis by simp
2742 lemma fps_inv_deriv:
2743 assumes a0:"a$0 = (0::'a::{field})"
2744 and a1: "a$1 \<noteq> 0"
2745 shows "fps_deriv (fps_inv a) = inverse (fps_deriv a oo fps_inv a)"
2747 let ?ia = "fps_inv a"
2748 let ?d = "fps_deriv a oo ?ia"
2749 let ?dia = "fps_deriv ?ia"
2750 have ia0: "?ia$0 = 0" by (simp add: fps_inv_def)
2751 have th0: "?d$0 \<noteq> 0" using a1 by (simp add: fps_compose_nth)
2752 from fps_inv_right[OF a0 a1] have "?d * ?dia = 1"
2753 by (simp add: fps_compose_deriv[OF ia0, of a, symmetric] )
2754 hence "inverse ?d * ?d * ?dia = inverse ?d * 1" by simp
2755 with inverse_mult_eq_1 [OF th0]
2756 show "?dia = inverse ?d" by simp
2759 lemma fps_inv_idempotent:
2760 assumes a0: "a$0 = 0"
2761 and a1: "a$1 \<noteq> 0"
2762 shows "fps_inv (fps_inv a) = a"
2765 have ra0: "?r a $ 0 = 0" by (simp add: fps_inv_def)
2766 from a1 have ra1: "?r a $ 1 \<noteq> 0" by (simp add: fps_inv_def field_simps)
2767 have X0: "X$0 = 0" by simp
2768 from fps_inv[OF ra0 ra1] have "?r (?r a) oo ?r a = X" .
2769 then have "?r (?r a) oo ?r a oo a = X oo a" by simp
2770 then have "?r (?r a) oo (?r a oo a) = a"
2771 unfolding X_fps_compose_startby0[OF a0]
2772 unfolding fps_compose_assoc[OF a0 ra0, symmetric] .
2773 then show ?thesis unfolding fps_inv[OF a0 a1] by simp
2776 lemma fps_ginv_ginv:
2777 assumes a0: "a$0 = 0"
2778 and a1: "a$1 \<noteq> 0"
2780 and c1: "c$1 \<noteq> 0"
2781 shows "fps_ginv b (fps_ginv c a) = b oo a oo fps_inv c"
2784 from c0 have rca0: "?r c a $0 = 0" by (simp add: fps_ginv_def)
2785 from a1 c1 have rca1: "?r c a $ 1 \<noteq> 0" by (simp add: fps_ginv_def field_simps)
2786 from fps_ginv[OF rca0 rca1]
2787 have "?r b (?r c a) oo ?r c a = b" .
2788 then have "?r b (?r c a) oo ?r c a oo a = b oo a" by simp
2789 then have "?r b (?r c a) oo (?r c a oo a) = b oo a"
2790 apply (subst fps_compose_assoc)
2792 apply (auto simp add: fps_ginv_def)
2794 then have "?r b (?r c a) oo c = b oo a"
2795 unfolding fps_ginv[OF a0 a1] .
2796 then have "?r b (?r c a) oo c oo fps_inv c= b oo a oo fps_inv c" by simp
2797 then have "?r b (?r c a) oo (c oo fps_inv c) = b oo a oo fps_inv c"
2798 apply (subst fps_compose_assoc)
2800 apply (auto simp add: fps_inv_def)
2802 then show ?thesis unfolding fps_inv_right[OF c0 c1] by simp
2805 lemma fps_ginv_deriv:
2806 assumes a0:"a$0 = (0::'a::{field})"
2807 and a1: "a$1 \<noteq> 0"
2808 shows "fps_deriv (fps_ginv b a) = (fps_deriv b / fps_deriv a) oo fps_ginv X a"
2810 let ?ia = "fps_ginv b a"
2811 let ?iXa = "fps_ginv X a"
2812 let ?d = "fps_deriv"
2814 have iXa0: "?iXa $ 0 = 0" by (simp add: fps_ginv_def)
2815 have da0: "?d a $ 0 \<noteq> 0" using a1 by simp
2816 from fps_ginv[OF a0 a1, of b] have "?d (?ia oo a) = fps_deriv b" by simp
2817 then have "(?d ?ia oo a) * ?d a = ?d b" unfolding fps_compose_deriv[OF a0] .
2818 then have "(?d ?ia oo a) * ?d a * inverse (?d a) = ?d b * inverse (?d a)" by simp
2819 then have "(?d ?ia oo a) * (inverse (?d a) * ?d a) = ?d b / ?d a"
2820 by (simp add: fps_divide_def)
2821 then have "(?d ?ia oo a) oo ?iXa = (?d b / ?d a) oo ?iXa "
2822 unfolding inverse_mult_eq_1[OF da0] by simp
2823 then have "?d ?ia oo (a oo ?iXa) = (?d b / ?d a) oo ?iXa"
2824 unfolding fps_compose_assoc[OF iXa0 a0] .
2825 then show ?thesis unfolding fps_inv_ginv[symmetric]
2826 unfolding fps_inv_right[OF a0 a1] by simp
2829 subsection{* Elementary series *}
2831 subsubsection{* Exponential series *}
2833 definition "E x = Abs_fps (\<lambda>n. x^n / of_nat (fact n))"
2835 lemma E_deriv[simp]: "fps_deriv (E a) = fps_const (a::'a::field_char_0) * E a" (is "?l = ?r")
2839 have "?l$n = ?r $ n"
2840 apply (auto simp add: E_def field_simps power_Suc[symmetric]
2841 simp del: fact_Suc of_nat_Suc power_Suc)
2842 apply (simp add: of_nat_mult field_simps)
2845 then show ?thesis by (simp add: fps_eq_iff)
2849 "fps_deriv a = fps_const c * a \<longleftrightarrow> a = fps_const (a$0) * E (c :: 'a::field_char_0)"
2850 (is "?lhs \<longleftrightarrow> ?rhs")
2853 from d have th: "\<And>n. a $ Suc n = c * a$n / of_nat (Suc n)"
2854 by (simp add: fps_deriv_def fps_eq_iff field_simps del: of_nat_Suc)
2857 have "a$n = a$0 * c ^ n/ (of_nat (fact n))"
2861 using fact_gt_zero_nat
2862 apply (simp add: field_simps del: of_nat_Suc fact_Suc)
2864 apply (simp add: field_simps of_nat_mult)
2868 show ?rhs by (auto simp add: fps_eq_iff fps_const_mult_left E_def intro: th')
2874 apply (simp only: h[symmetric])
2879 lemma E_add_mult: "E (a + b) = E (a::'a::field_char_0) * E b" (is "?l = ?r")
2881 have "fps_deriv (?r) = fps_const (a+b) * ?r"
2882 by (simp add: fps_const_add[symmetric] field_simps del: fps_const_add)
2883 then have "?r = ?l" apply (simp only: E_unique_ODE)
2884 by (simp add: fps_mult_nth E_def)
2885 then show ?thesis ..
2888 lemma E_nth[simp]: "E a $ n = a^n / of_nat (fact n)"
2889 by (simp add: E_def)
2891 lemma E0[simp]: "E (0::'a::{field}) = 1"
2892 by (simp add: fps_eq_iff power_0_left)
2894 lemma E_neg: "E (- a) = inverse (E (a::'a::field_char_0))"
2896 from E_add_mult[of a "- a"] have th0: "E a * E (- a) = 1"
2898 have th1: "E a $ 0 \<noteq> 0" by simp
2899 from fps_inverse_unique[OF th1 th0] show ?thesis by simp
2902 lemma E_nth_deriv[simp]: "fps_nth_deriv n (E (a::'a::field_char_0)) = (fps_const a)^n * (E a)"
2905 lemma X_compose_E[simp]: "X oo E (a::'a::{field}) = E a - 1"
2906 by (simp add: fps_eq_iff X_fps_compose)
2909 assumes a: "a\<noteq>0"
2910 shows "fps_inv (E a - 1) oo (E a - 1) = X"
2911 and "(E a - 1) oo fps_inv (E a - 1) = X"
2914 have b0: "?b $ 0 = 0" by simp
2915 have b1: "?b $ 1 \<noteq> 0" by (simp add: a)
2916 from fps_inv[OF b0 b1] show "fps_inv (E a - 1) oo (E a - 1) = X" .
2917 from fps_inv_right[OF b0 b1] show "(E a - 1) oo fps_inv (E a - 1) = X" .
2920 lemma fps_const_inverse:
2921 "a \<noteq> 0 \<Longrightarrow> inverse (fps_const (a::'a::field)) = fps_const (inverse a)"
2922 apply (auto simp add: fps_eq_iff fps_inverse_def)
2927 lemma inverse_one_plus_X:
2928 "inverse (1 + X) = Abs_fps (\<lambda>n. (- 1 ::'a::{field})^n)"
2929 (is "inverse ?l = ?r")
2931 have th: "?l * ?r = 1"
2932 by (auto simp add: field_simps fps_eq_iff minus_one_power_iff simp del: minus_one)
2933 have th': "?l $ 0 \<noteq> 0" by (simp add: )
2934 from fps_inverse_unique[OF th' th] show ?thesis .
2937 lemma E_power_mult: "(E (c::'a::field_char_0))^n = E (of_nat n * c)"
2938 by (induct n) (auto simp add: field_simps E_add_mult)
2941 assumes r: "r (Suc k) 1 = 1"
2942 shows "fps_radical r (Suc k) (E (c::'a::{field_char_0})) = E (c / of_nat (Suc k))"
2944 let ?ck = "(c / of_nat (Suc k))"
2945 let ?r = "fps_radical r (Suc k)"
2946 have eq0[simp]: "?ck * of_nat (Suc k) = c" "of_nat (Suc k) * ?ck = c"
2947 by (simp_all del: of_nat_Suc)
2948 have th0: "E ?ck ^ (Suc k) = E c" unfolding E_power_mult eq0 ..
2949 have th: "r (Suc k) (E c $0) ^ Suc k = E c $ 0"
2950 "r (Suc k) (E c $ 0) = E ?ck $ 0" "E c $ 0 \<noteq> 0" using r by simp_all
2951 from th0 radical_unique[where r=r and k=k, OF th]
2952 show ?thesis by auto
2955 lemma Ec_E1_eq: "E (1::'a::{field_char_0}) oo (fps_const c * X) = E c"
2956 apply (auto simp add: fps_eq_iff E_def fps_compose_def power_mult_distrib)
2957 apply (simp add: cond_value_iff cond_application_beta setsum_delta' cong del: if_weak_cong)
2960 text{* The generalized binomial theorem as a consequence of @{thm E_add_mult} *}
2962 lemma gbinomial_theorem:
2963 "((a::'a::{field_char_0, field_inverse_zero})+b) ^ n =
2964 (\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n-k))"
2966 from E_add_mult[of a b]
2967 have "(E (a + b)) $ n = (E a * E b)$n" by simp
2968 then have "(a + b) ^ n =
2969 (\<Sum>i\<Colon>nat = 0\<Colon>nat..n. a ^ i * b ^ (n - i) * (of_nat (fact n) / of_nat (fact i * fact (n - i))))"
2970 by (simp add: field_simps fps_mult_nth of_nat_mult[symmetric] setsum_right_distrib)
2973 apply (rule setsum_cong2)
2975 apply (frule binomial_fact[where ?'a = 'a, symmetric])
2976 apply (simp add: field_simps of_nat_mult)
2980 text{* And the nat-form -- also available from Binomial.thy *}
2981 lemma binomial_theorem: "(a+b) ^ n = (\<Sum>k=0..n. (n choose k) * a^k * b^(n-k))"
2982 using gbinomial_theorem[of "of_nat a" "of_nat b" n]
2983 unfolding of_nat_add[symmetric] of_nat_power[symmetric] of_nat_mult[symmetric]
2984 of_nat_setsum[symmetric]
2988 subsubsection{* Logarithmic series *}
2991 "Abs_fps(%n. if n=0 then (v::'a::ring_1) else f n) = fps_const v + X * Abs_fps (%n. f (Suc n))"
2992 by (auto simp add: fps_eq_iff)
2994 definition L :: "'a::field_char_0 \<Rightarrow> 'a fps"
2995 where "L c = fps_const (1/c) * Abs_fps (\<lambda>n. if n = 0 then 0 else (- 1) ^ (n - 1) / of_nat n)"
2997 lemma fps_deriv_L: "fps_deriv (L c) = fps_const (1/c) * inverse (1 + X)"
2998 unfolding inverse_one_plus_X
2999 by (simp add: L_def fps_eq_iff del: of_nat_Suc)
3001 lemma L_nth: "L c $ n = (if n=0 then 0 else 1/c * ((- 1) ^ (n - 1) / of_nat n))"
3002 by (simp add: L_def field_simps)
3004 lemma L_0[simp]: "L c $ 0 = 0" by (simp add: L_def)
3007 assumes a: "a\<noteq> (0::'a::{field_char_0})"
3008 shows "L a = fps_inv (E a - 1)" (is "?l = ?r")
3011 have b0: "?b $ 0 = 0" by simp
3012 have b1: "?b $ 1 \<noteq> 0" by (simp add: a)
3013 have "fps_deriv (E a - 1) oo fps_inv (E a - 1) =
3014 (fps_const a * (E a - 1) + fps_const a) oo fps_inv (E a - 1)"
3015 by (simp add: field_simps)
3016 also have "\<dots> = fps_const a * (X + 1)"
3017 apply (simp add: fps_compose_add_distrib fps_const_mult_apply_left[symmetric] fps_inv_right[OF b0 b1])
3018 apply (simp add: field_simps)
3020 finally have eq: "fps_deriv (E a - 1) oo fps_inv (E a - 1) = fps_const a * (X + 1)" .
3021 from fps_inv_deriv[OF b0 b1, unfolded eq]
3022 have "fps_deriv (fps_inv ?b) = fps_const (inverse a) / (X + 1)"
3024 by (simp add: fps_const_inverse eq fps_divide_def fps_inverse_mult)
3025 hence "fps_deriv ?l = fps_deriv ?r"
3026 by (simp add: fps_deriv_L add_commute fps_divide_def divide_inverse)
3027 then show ?thesis unfolding fps_deriv_eq_iff
3028 by (simp add: L_nth fps_inv_def)
3032 assumes c0: "c\<noteq>0"
3033 and d0: "d\<noteq>0"
3034 shows "L c + L d = fps_const (c+d) * L (c*d)"
3037 from c0 d0 have eq: "1/c + 1/d = (c+d)/(c*d)" by (simp add: field_simps)
3038 have "fps_deriv ?r = fps_const (1/c + 1/d) * inverse (1 + X)"
3039 by (simp add: fps_deriv_L fps_const_add[symmetric] algebra_simps del: fps_const_add)
3040 also have "\<dots> = fps_deriv ?l"
3041 apply (simp add: fps_deriv_L)
3042 apply (simp add: fps_eq_iff eq)
3044 finally show ?thesis
3045 unfolding fps_deriv_eq_iff by simp
3049 subsubsection{* Binomial series *}
3051 definition "fps_binomial a = Abs_fps (\<lambda>n. a gchoose n)"
3053 lemma fps_binomial_nth[simp]: "fps_binomial a $ n = a gchoose n"
3054 by (simp add: fps_binomial_def)
3056 lemma fps_binomial_ODE_unique:
3057 fixes c :: "'a::field_char_0"
3058 shows "fps_deriv a = (fps_const c * a) / (1 + X) \<longleftrightarrow> a = fps_const (a$0) * fps_binomial c"
3059 (is "?lhs \<longleftrightarrow> ?rhs")
3061 let ?da = "fps_deriv a"
3062 let ?x1 = "(1 + X):: 'a fps"
3063 let ?l = "?x1 * ?da"
3064 let ?r = "fps_const c * a"
3065 have x10: "?x1 $ 0 \<noteq> 0" by simp
3066 have "?l = ?r \<longleftrightarrow> inverse ?x1 * ?l = inverse ?x1 * ?r" by simp
3067 also have "\<dots> \<longleftrightarrow> ?da = (fps_const c * a) / ?x1"
3068 apply (simp only: fps_divide_def mult_assoc[symmetric] inverse_mult_eq_1[OF x10])
3069 apply (simp add: field_simps)
3071 finally have eq: "?l = ?r \<longleftrightarrow> ?lhs" by simp
3073 {assume h: "?l = ?r"
3075 from h have lrn: "?l $ n = ?r$n" by simp
3078 have "a$ Suc n = ((c - of_nat n) / of_nat (Suc n)) * a $n"
3079 apply (simp add: field_simps del: of_nat_Suc)
3080 by (cases n, simp_all add: field_simps del: of_nat_Suc)
3085 have "a$n = (c gchoose n) * a$0"
3091 thus ?case unfolding th0
3092 apply (simp add: field_simps del: of_nat_Suc)
3093 unfolding mult_assoc[symmetric] gbinomial_mult_1
3094 apply (simp add: field_simps)
3100 apply (simp add: fps_eq_iff)
3102 apply (simp add: field_simps)
3108 have th00: "\<And>x y. x * (a$0 * y) = a$0 * (x*y)"
3109 by (simp add: mult_commute)
3113 apply (clarsimp simp add: fps_eq_iff field_simps)
3114 unfolding mult_assoc[symmetric] th00 gbinomial_mult_1
3115 apply (simp add: field_simps gbinomial_mult_1)
3118 ultimately show ?thesis by blast
3121 lemma fps_binomial_deriv: "fps_deriv (fps_binomial c) = fps_const c * fps_binomial c / (1 + X)"
3123 let ?a = "fps_binomial c"
3124 have th0: "?a = fps_const (?a$0) * ?a" by (simp)
3125 from iffD2[OF fps_binomial_ODE_unique, OF th0] show ?thesis .
3128 lemma fps_binomial_add_mult: "fps_binomial (c+d) = fps_binomial c * fps_binomial d" (is "?l = ?r")
3131 let ?b = "fps_binomial"
3132 let ?db = "\<lambda>x. fps_deriv (?b x)"
3133 have "fps_deriv ?P = ?db c * ?b d + ?b c * ?db d - ?db (c + d)" by simp
3134 also have "\<dots> = inverse (1 + X) *
3135 (fps_const c * ?b c * ?b d + fps_const d * ?b c * ?b d - fps_const (c+d) * ?b (c + d))"
3136 unfolding fps_binomial_deriv
3137 by (simp add: fps_divide_def field_simps)
3138 also have "\<dots> = (fps_const (c + d)/ (1 + X)) * ?P"
3139 by (simp add: field_simps fps_divide_def fps_const_add[symmetric] del: fps_const_add)
3140 finally have th0: "fps_deriv ?P = fps_const (c+d) * ?P / (1 + X)"
3141 by (simp add: fps_divide_def)
3142 have "?P = fps_const (?P$0) * ?b (c + d)"
3143 unfolding fps_binomial_ODE_unique[symmetric]
3145 hence "?P = 0" by (simp add: fps_mult_nth)
3146 then show ?thesis by simp
3149 lemma fps_minomial_minus_one: "fps_binomial (- 1) = inverse (1 + X)"
3150 (is "?l = inverse ?r")
3152 have th: "?r$0 \<noteq> 0" by simp
3153 have th': "fps_deriv (inverse ?r) = fps_const (- 1) * inverse ?r / (1 + X)"
3154 by (simp add: fps_inverse_deriv[OF th] fps_divide_def
3155 power2_eq_square mult_commute fps_const_neg[symmetric] del: fps_const_neg minus_one)
3156 have eq: "inverse ?r $ 0 = 1"
3157 by (simp add: fps_inverse_def)
3158 from iffD1[OF fps_binomial_ODE_unique[of "inverse (1 + X)" "- 1"] th'] eq
3159 show ?thesis by (simp add: fps_inverse_def)
3162 text{* Vandermonde's Identity as a consequence *}
3163 lemma gbinomial_Vandermonde:
3164 "setsum (\<lambda>k. (a gchoose k) * (b gchoose (n - k))) {0..n} = (a + b) gchoose n"
3166 let ?ba = "fps_binomial a"
3167 let ?bb = "fps_binomial b"
3168 let ?bab = "fps_binomial (a + b)"
3169 from fps_binomial_add_mult[of a b] have "?bab $ n = (?ba * ?bb)$n" by simp
3170 then show ?thesis by (simp add: fps_mult_nth)
3173 lemma binomial_Vandermonde:
3174 "setsum (\<lambda>k. (a choose k) * (b choose (n - k))) {0..n} = (a + b) choose n"
3175 using gbinomial_Vandermonde[of "(of_nat a)" "of_nat b" n]
3176 apply (simp only: binomial_gbinomial[symmetric] of_nat_mult[symmetric]
3177 of_nat_setsum[symmetric] of_nat_add[symmetric])
3181 lemma binomial_Vandermonde_same: "setsum (\<lambda>k. (n choose k)\<^sup>2) {0..n} = (2*n) choose n"
3182 using binomial_Vandermonde[of n n n,symmetric]
3184 apply (simp add: power2_eq_square)
3185 apply (rule setsum_cong2)
3186 apply (auto intro: binomial_symmetric)
3189 lemma Vandermonde_pochhammer_lemma:
3190 fixes a :: "'a::field_char_0"
3191 assumes b: "\<forall> j\<in>{0 ..<n}. b \<noteq> of_nat j"
3192 shows "setsum (%k. (pochhammer (- a) k * pochhammer (- (of_nat n)) k) /
3193 (of_nat (fact k) * pochhammer (b - of_nat n + 1) k)) {0..n} =
3194 pochhammer (- (a+ b)) n / pochhammer (- b) n"
3197 let ?m1 = "%m. (- 1 :: 'a) ^ m"
3198 let ?f = "%m. of_nat (fact m)"
3199 let ?p = "%(x::'a). pochhammer (- x)"
3200 from b have bn0: "?p b n \<noteq> 0" unfolding pochhammer_eq_0_iff by simp
3203 assume kn: "k \<in> {0..n}"
3205 assume c:"pochhammer (b - of_nat n + 1) n = 0"
3206 then obtain j where j: "j < n" "b - of_nat n + 1 = - of_nat j"
3207 unfolding pochhammer_eq_0_iff by blast
3208 from j have "b = of_nat n - of_nat j - of_nat 1"
3209 by (simp add: algebra_simps)
3210 then have "b = of_nat (n - j - 1)"
3211 using j kn by (simp add: of_nat_diff)
3212 with b have False using j by auto
3214 then have nz: "pochhammer (1 + b - of_nat n) n \<noteq> 0"
3215 by (auto simp add: algebra_simps)
3217 from nz kn [simplified] have nz': "pochhammer (1 + b - of_nat n) k \<noteq> 0"
3218 by (rule pochhammer_neq_0_mono)
3220 assume k0: "k = 0 \<or> n =0"
3221 then have "b gchoose (n - k) =
3222 (?m1 n * ?p b n * ?m1 k * ?p (of_nat n) k) / (?f n * pochhammer (b - of_nat n + 1) k)"
3224 by (cases "k = 0") (simp_all add: gbinomial_pochhammer)
3228 assume n0: "n \<noteq> 0" and k0: "k \<noteq> 0"
3229 then obtain m where m: "n = Suc m" by (cases n) auto
3230 from k0 obtain h where h: "k = Suc h" by (cases k) auto
3233 then have "b gchoose (n - k) =
3234 (?m1 n * ?p b n * ?m1 k * ?p (of_nat n) k) / (?f n * pochhammer (b - of_nat n + 1) k)"
3235 using kn pochhammer_minus'[where k=k and n=n and b=b]
3236 apply (simp add: pochhammer_same)
3238 apply (simp add: field_simps power_add[symmetric])
3243 assume nk: "k \<noteq> n"
3244 have m1nk: "?m1 n = setprod (%i. - 1) {0..m}" "?m1 k = setprod (%i. - 1) {0..h}"
3245 by (simp_all add: setprod_constant m h)
3246 from kn nk have kn': "k < n" by simp
3247 have bnz0: "pochhammer (b - of_nat n + 1) k \<noteq> 0"
3249 unfolding pochhammer_eq_0_iff
3251 apply (erule_tac x= "n - ka - 1" in allE)
3252 apply (auto simp add: algebra_simps of_nat_diff)
3254 have eq1: "setprod (%k. (1::'a) + of_nat m - of_nat k) {0 .. h} =
3255 setprod of_nat {Suc (m - h) .. Suc m}"
3256 apply (rule strong_setprod_reindex_cong[where f="%k. Suc m - k "])
3258 apply (auto simp add: inj_on_def image_def)
3259 apply (rule_tac x="Suc m - x" in bexI)
3260 apply (simp_all add: of_nat_diff)
3263 have th1: "(?m1 k * ?p (of_nat n) k) / ?f n = 1 / of_nat(fact (n - k))"
3265 unfolding m h pochhammer_Suc_setprod
3266 apply (simp add: field_simps del: fact_Suc minus_one)
3267 unfolding fact_altdef_nat id_def
3268 unfolding of_nat_setprod
3269 unfolding setprod_timesf[symmetric]
3272 apply (subst setprod_Un_disjoint[symmetric])
3274 apply (rule setprod_cong)
3277 have th20: "?m1 n * ?p b n = setprod (%i. b - of_nat i) {0..m}"
3279 unfolding m h pochhammer_Suc_setprod
3280 unfolding setprod_timesf[symmetric]
3281 apply (rule setprod_cong)
3284 have th21:"pochhammer (b - of_nat n + 1) k = setprod (%i. b - of_nat i) {n - k .. n - 1}"
3286 unfolding pochhammer_Suc_setprod
3287 apply (rule strong_setprod_reindex_cong[where f="%k. n - 1 - k"])
3289 apply (auto simp add: inj_on_def m h image_def)
3290 apply (rule_tac x= "m - x" in bexI)
3291 apply (auto simp add: of_nat_diff)
3294 have "?m1 n * ?p b n =
3295 pochhammer (b - of_nat n + 1) k * setprod (%i. b - of_nat i) {0.. n - k - 1}"
3298 apply (subst setprod_Un_disjoint[symmetric])
3301 apply (rule setprod_cong)
3304 then have th2: "(?m1 n * ?p b n)/pochhammer (b - of_nat n + 1) k =
3305 setprod (%i. b - of_nat i) {0.. n - k - 1}"
3306 using nz' by (simp add: field_simps)
3307 have "(?m1 n * ?p b n * ?m1 k * ?p (of_nat n) k) / (?f n * pochhammer (b - of_nat n + 1) k) =
3308 ((?m1 k * ?p (of_nat n) k) / ?f n) * ((?m1 n * ?p b n)/pochhammer (b - of_nat n + 1) k)"
3310 by (simp add: field_simps)
3311 also have "\<dots> = b gchoose (n - k)"
3313 using kn' by (simp add: gbinomial_def)
3314 finally have "b gchoose (n - k) =
3315 (?m1 n * ?p b n * ?m1 k * ?p (of_nat n) k) / (?f n * pochhammer (b - of_nat n + 1) k)"
3319 have "b gchoose (n - k) =
3320 (?m1 n * ?p b n * ?m1 k * ?p (of_nat n) k) / (?f n * pochhammer (b - of_nat n + 1) k)"
3321 by (cases "k = n") auto
3323 ultimately have "b gchoose (n - k) =
3324 (?m1 n * ?p b n * ?m1 k * ?p (of_nat n) k) / (?f n * pochhammer (b - of_nat n + 1) k)"
3325 "pochhammer (1 + b - of_nat n) k \<noteq> 0 "
3326 apply (cases "n = 0")
3334 have "?r = ((a + b) gchoose n) * (of_nat (fact n)/ (?m1 n * pochhammer (- b) n))"
3335 unfolding gbinomial_pochhammer
3336 using bn0 by (auto simp add: field_simps)
3337 also have "\<dots> = ?l"
3338 unfolding gbinomial_Vandermonde[symmetric]
3339 apply (simp add: th00)
3340 unfolding gbinomial_pochhammer
3342 apply (simp add: setsum_left_distrib setsum_right_distrib field_simps)
3343 apply (rule setsum_cong2)
3344 apply (drule th00(2))
3345 apply (simp add: field_simps power_add[symmetric])
3347 finally show ?thesis by simp
3350 lemma Vandermonde_pochhammer:
3351 fixes a :: "'a::field_char_0"
3352 assumes c: "ALL i : {0..< n}. c \<noteq> - of_nat i"
3353 shows "setsum (%k. (pochhammer a k * pochhammer (- (of_nat n)) k) /
3354 (of_nat (fact k) * pochhammer c k)) {0..n} = pochhammer (c - a) n / pochhammer c n"
3357 let ?b = "c + of_nat n - 1"
3358 have h: "\<forall> j \<in>{0..< n}. ?b \<noteq> of_nat j" using c
3359 apply (auto simp add: algebra_simps of_nat_diff)
3360 apply (erule_tac x= "n - j - 1" in ballE)
3361 apply (auto simp add: of_nat_diff algebra_simps)
3363 have th0: "pochhammer (- (?a + ?b)) n = (- 1)^n * pochhammer (c - a) n"
3364 unfolding pochhammer_minus[OF le_refl]
3365 by (simp add: algebra_simps)
3366 have th1: "pochhammer (- ?b) n = (- 1)^n * pochhammer c n"
3367 unfolding pochhammer_minus[OF le_refl]
3369 have nz: "pochhammer c n \<noteq> 0" using c
3370 by (simp add: pochhammer_eq_0_iff)
3371 from Vandermonde_pochhammer_lemma[where a = "?a" and b="?b" and n=n, OF h, unfolded th0 th1]
3372 show ?thesis using nz by (simp add: field_simps setsum_right_distrib)
3376 subsubsection{* Formal trigonometric functions *}
3378 definition "fps_sin (c::'a::field_char_0) =
3379 Abs_fps (\<lambda>n. if even n then 0 else (- 1) ^((n - 1) div 2) * c^n /(of_nat (fact n)))"
3381 definition "fps_cos (c::'a::field_char_0) =
3382 Abs_fps (\<lambda>n. if even n then (- 1) ^ (n div 2) * c^n / (of_nat (fact n)) else 0)"
3384 lemma fps_sin_deriv:
3385 "fps_deriv (fps_sin c) = fps_const c * fps_cos c"
3387 proof (rule fps_ext)
3391 have "?lhs$n = of_nat (n+1) * (fps_sin c $ (n+1))" by simp
3392 also have "\<dots> = of_nat (n+1) * ((- 1)^(n div 2) * c^Suc n / of_nat (fact (Suc n)))"
3393 using en by (simp add: fps_sin_def)
3394 also have "\<dots> = (- 1)^(n div 2) * c^Suc n * (of_nat (n+1) / (of_nat (Suc n) * of_nat (fact n)))"
3395 unfolding fact_Suc of_nat_mult
3396 by (simp add: field_simps del: of_nat_add of_nat_Suc)
3397 also have "\<dots> = (- 1)^(n div 2) *c^Suc n / of_nat (fact n)"
3398 by (simp add: field_simps del: of_nat_add of_nat_Suc)
3399 finally have "?lhs $n = ?rhs$n" using en
3400 by (simp add: fps_cos_def field_simps)
3402 then show "?lhs $ n = ?rhs $ n"
3403 by (cases "even n") (simp_all add: fps_deriv_def fps_sin_def fps_cos_def)
3406 lemma fps_cos_deriv: "fps_deriv (fps_cos c) = fps_const (- c)* (fps_sin c)"
3408 proof (rule fps_ext)
3409 have th0: "\<And>n. - ((- 1::'a) ^ n) = (- 1)^Suc n" by simp
3410 have th1: "\<And>n. odd n \<Longrightarrow> Suc ((n - 1) div 2) = Suc n div 2"
3411 by (case_tac n, simp_all)
3415 from en have n0: "n \<noteq>0 " by presburger
3416 have "?lhs$n = of_nat (n+1) * (fps_cos c $ (n+1))" by simp
3417 also have "\<dots> = of_nat (n+1) * ((- 1)^((n + 1) div 2) * c^Suc n / of_nat (fact (Suc n)))"
3418 using en by (simp add: fps_cos_def)
3419 also have "\<dots> = (- 1)^((n + 1) div 2)*c^Suc n * (of_nat (n+1) / (of_nat (Suc n) * of_nat (fact n)))"
3420 unfolding fact_Suc of_nat_mult
3421 by (simp add: field_simps del: of_nat_add of_nat_Suc)
3422 also have "\<dots> = (- 1)^((n + 1) div 2) * c^Suc n / of_nat (fact n)"
3423 by (simp add: field_simps del: of_nat_add of_nat_Suc)
3424 also have "\<dots> = (- ((- 1)^((n - 1) div 2))) * c^Suc n / of_nat (fact n)"
3425 unfolding th0 unfolding th1[OF en] by simp
3426 finally have "?lhs $n = ?rhs$n" using en
3427 by (simp add: fps_sin_def field_simps)
3429 then show "?lhs $ n = ?rhs $ n"
3430 by (cases "even n") (simp_all add: fps_deriv_def fps_sin_def fps_cos_def)
3433 lemma fps_sin_cos_sum_of_squares:
3434 "(fps_cos c)\<^sup>2 + (fps_sin c)\<^sup>2 = 1" (is "?lhs = 1")
3436 have "fps_deriv ?lhs = 0"
3437 apply (simp add: fps_deriv_power fps_sin_deriv fps_cos_deriv)
3438 apply (simp add: field_simps fps_const_neg[symmetric] del: fps_const_neg)
3440 then have "?lhs = fps_const (?lhs $ 0)"
3441 unfolding fps_deriv_eq_0_iff .
3442 also have "\<dots> = 1"
3443 by (auto simp add: fps_eq_iff numeral_2_eq_2 fps_mult_nth fps_cos_def fps_sin_def)
3444 finally show ?thesis .
3447 lemma divide_eq_iff: "a \<noteq> (0::'a::field) \<Longrightarrow> x / a = y \<longleftrightarrow> x = y * a"
3450 lemma eq_divide_iff: "a \<noteq> (0::'a::field) \<Longrightarrow> x = y / a \<longleftrightarrow> x * a = y"
3453 lemma fps_sin_nth_0 [simp]: "fps_sin c $ 0 = 0"
3454 unfolding fps_sin_def by simp
3456 lemma fps_sin_nth_1 [simp]: "fps_sin c $ 1 = c"
3457 unfolding fps_sin_def by simp
3459 lemma fps_sin_nth_add_2:
3460 "fps_sin c $ (n + 2) = - (c * c * fps_sin c $ n / (of_nat(n+1) * of_nat(n+2)))"
3461 unfolding fps_sin_def
3462 apply (cases n, simp)
3463 apply (simp add: divide_eq_iff eq_divide_iff del: of_nat_Suc fact_Suc)
3464 apply (simp add: of_nat_mult del: of_nat_Suc mult_Suc)
3467 lemma fps_cos_nth_0 [simp]: "fps_cos c $ 0 = 1"
3468 unfolding fps_cos_def by simp
3470 lemma fps_cos_nth_1 [simp]: "fps_cos c $ 1 = 0"
3471 unfolding fps_cos_def by simp
3473 lemma fps_cos_nth_add_2:
3474 "fps_cos c $ (n + 2) = - (c * c * fps_cos c $ n / (of_nat(n+1) * of_nat(n+2)))"
3475 unfolding fps_cos_def
3476 apply (simp add: divide_eq_iff eq_divide_iff del: of_nat_Suc fact_Suc)
3477 apply (simp add: of_nat_mult del: of_nat_Suc mult_Suc)
3480 lemma nat_induct2: "P 0 \<Longrightarrow> P 1 \<Longrightarrow> (\<And>n. P n \<Longrightarrow> P (n + 2)) \<Longrightarrow> P (n::nat)"
3481 unfolding One_nat_def numeral_2_eq_2
3482 apply (induct n rule: nat_less_induct)
3485 apply (rename_tac m)
3488 apply (rename_tac k)
3493 lemma nat_add_1_add_1: "(n::nat) + 1 + 1 = n + 2"
3497 assumes 0: "a $ 0 = 0"
3499 and 2: "fps_deriv (fps_deriv a) = - (fps_const c * fps_const c * a)"
3500 shows "a = fps_sin c"
3501 apply (rule fps_ext)
3502 apply (induct_tac n rule: nat_induct2)
3504 apply (simp add: 1 del: One_nat_def)
3505 apply (rename_tac m, cut_tac f="\<lambda>a. a $ m" in arg_cong [OF 2])
3506 apply (simp add: nat_add_1_add_1 fps_sin_nth_add_2
3507 del: One_nat_def of_nat_Suc of_nat_add add_2_eq_Suc')
3508 apply (subst minus_divide_left)
3509 apply (subst eq_divide_iff)
3510 apply (simp del: of_nat_add of_nat_Suc)
3511 apply (simp only: mult_ac)
3515 assumes 0: "a $ 0 = 1"
3517 and 2: "fps_deriv (fps_deriv a) = - (fps_const c * fps_const c * a)"
3518 shows "a = fps_cos c"
3519 apply (rule fps_ext)
3520 apply (induct_tac n rule: nat_induct2)
3522 apply (simp add: 1 del: One_nat_def)
3523 apply (rename_tac m, cut_tac f="\<lambda>a. a $ m" in arg_cong [OF 2])
3524 apply (simp add: nat_add_1_add_1 fps_cos_nth_add_2
3525 del: One_nat_def of_nat_Suc of_nat_add add_2_eq_Suc')
3526 apply (subst minus_divide_left)
3527 apply (subst eq_divide_iff)
3528 apply (simp del: of_nat_add of_nat_Suc)
3529 apply (simp only: mult_ac)
3532 lemma mult_nth_0 [simp]: "(a * b) $ 0 = a $ 0 * b $ 0"
3533 by (simp add: fps_mult_nth)
3535 lemma mult_nth_1 [simp]: "(a * b) $ 1 = a $ 0 * b $ 1 + a $ 1 * b $ 0"
3536 by (simp add: fps_mult_nth)
3538 lemma fps_sin_add: "fps_sin (a + b) = fps_sin a * fps_cos b + fps_cos a * fps_sin b"
3539 apply (rule eq_fps_sin [symmetric], simp, simp del: One_nat_def)
3540 apply (simp del: fps_const_neg fps_const_add fps_const_mult
3541 add: fps_const_add [symmetric] fps_const_neg [symmetric]
3542 fps_sin_deriv fps_cos_deriv algebra_simps)
3545 lemma fps_cos_add: "fps_cos (a + b) = fps_cos a * fps_cos b - fps_sin a * fps_sin b"
3546 apply (rule eq_fps_cos [symmetric], simp, simp del: One_nat_def)
3547 apply (simp del: fps_const_neg fps_const_add fps_const_mult
3548 add: fps_const_add [symmetric] fps_const_neg [symmetric]
3549 fps_sin_deriv fps_cos_deriv algebra_simps)
3552 lemma fps_sin_even: "fps_sin (- c) = - fps_sin c"
3553 by (auto simp add: fps_eq_iff fps_sin_def)
3555 lemma fps_cos_odd: "fps_cos (- c) = fps_cos c"
3556 by (auto simp add: fps_eq_iff fps_cos_def)
3558 definition "fps_tan c = fps_sin c / fps_cos c"
3560 lemma fps_tan_deriv: "fps_deriv (fps_tan c) = fps_const c / (fps_cos c)\<^sup>2"
3562 have th0: "fps_cos c $ 0 \<noteq> 0" by (simp add: fps_cos_def)
3564 using fps_sin_cos_sum_of_squares[of c]
3565 apply (simp add: fps_tan_def fps_divide_deriv[OF th0] fps_sin_deriv fps_cos_deriv
3566 fps_const_neg[symmetric] field_simps power2_eq_square del: fps_const_neg)
3567 unfolding distrib_left[symmetric]
3572 text {* Connection to E c over the complex numbers --- Euler and De Moivre*}
3573 lemma Eii_sin_cos: "E (ii * c) = fps_cos c + fps_const ii * fps_sin c "
3579 from en obtain m where m: "n = 2 * m"
3580 unfolding even_mult_two_ex by blast
3583 by (simp add: m fps_sin_def fps_cos_def power_mult_distrib power_mult power_minus)
3588 from on obtain m where m: "n = 2*m + 1"
3589 unfolding odd_nat_equiv_def2 by (auto simp add: mult_2)
3591 by (simp add: m fps_sin_def fps_cos_def power_mult_distrib
3592 power_mult power_minus)
3594 ultimately have "?l $n = ?r$n" by blast
3595 } then show ?thesis by (simp add: fps_eq_iff)
3598 lemma E_minus_ii_sin_cos: "E (- (ii * c)) = fps_cos c - fps_const ii * fps_sin c"
3599 unfolding minus_mult_right Eii_sin_cos by (simp add: fps_sin_even fps_cos_odd)
3601 lemma fps_const_minus: "fps_const (c::'a::group_add) - fps_const d = fps_const (c - d)"
3602 by (simp add: fps_eq_iff fps_const_def)
3604 lemma fps_numeral_fps_const: "numeral i = fps_const (numeral i :: 'a:: {comm_ring_1})"
3605 by (fact numeral_fps_const) (* FIXME: duplicate *)
3607 lemma fps_cos_Eii: "fps_cos c = (E (ii * c) + E (- ii * c)) / fps_const 2"
3609 have th: "fps_cos c + fps_cos c = fps_cos c * fps_const 2"
3610 by (simp add: numeral_fps_const)
3612 unfolding Eii_sin_cos minus_mult_commute
3613 by (simp add: fps_sin_even fps_cos_odd numeral_fps_const fps_divide_def fps_const_inverse th)
3616 lemma fps_sin_Eii: "fps_sin c = (E (ii * c) - E (- ii * c)) / fps_const (2*ii)"
3618 have th: "fps_const \<i> * fps_sin c + fps_const \<i> * fps_sin c = fps_sin c * fps_const (2 * ii)"
3619 by (simp add: fps_eq_iff numeral_fps_const)
3621 unfolding Eii_sin_cos minus_mult_commute
3622 by (simp add: fps_sin_even fps_cos_odd fps_divide_def fps_const_inverse th)
3626 "fps_tan c = (E (ii * c) - E (- ii * c)) / (fps_const ii * (E (ii * c) + E (- ii * c)))"
3627 unfolding fps_tan_def fps_sin_Eii fps_cos_Eii mult_minus_left E_neg
3628 apply (simp add: fps_divide_def fps_inverse_mult fps_const_mult[symmetric] fps_const_inverse del: fps_const_mult)
3632 lemma fps_demoivre: "(fps_cos a + fps_const ii * fps_sin a)^n = fps_cos (of_nat n * a) + fps_const ii * fps_sin (of_nat n * a)"
3633 unfolding Eii_sin_cos[symmetric] E_power_mult
3634 by (simp add: mult_ac)
3637 subsection {* Hypergeometric series *}
3639 definition "F as bs (c::'a::{field_char_0, field_inverse_zero}) =
3640 Abs_fps (%n. (foldl (%r a. r* pochhammer a n) 1 as * c^n) /
3641 (foldl (%r b. r * pochhammer b n) 1 bs * of_nat (fact n)))"
3643 lemma F_nth[simp]: "F as bs c $ n =
3644 (foldl (\<lambda>r a. r* pochhammer a n) 1 as * c^n) /
3645 (foldl (\<lambda>r b. r * pochhammer b n) 1 bs * of_nat (fact n))"
3646 by (simp add: F_def)
3648 lemma foldl_mult_start:
3649 "foldl (%r x. r * f x) (v::'a::comm_ring_1) as * x = foldl (%r x. r * f x) (v * x) as "
3650 by (induct as arbitrary: x v) (auto simp add: algebra_simps)
3652 lemma foldr_mult_foldl:
3653 "foldr (%x r. r * f x) as v = foldl (%r x. r * f x) (v :: 'a::comm_ring_1) as"
3654 by (induct as arbitrary: v) (auto simp add: foldl_mult_start)
3657 "F as bs c $ n = foldr (\<lambda>a r. r * pochhammer a n) as (c ^ n) /
3658 foldr (\<lambda>b r. r * pochhammer b n) bs (of_nat (fact n))"
3659 by (simp add: foldl_mult_start foldr_mult_foldl)
3661 lemma F_E[simp]: "F [] [] c = E c"
3662 by (simp add: fps_eq_iff)
3664 lemma F_1_0[simp]: "F [1] [] c = 1/(1 - fps_const c * X)"
3666 let ?a = "(Abs_fps (\<lambda>n. 1)) oo (fps_const c * X)"
3667 have th0: "(fps_const c * X) $ 0 = 0" by simp
3668 show ?thesis unfolding gp[OF th0, symmetric]
3669 by (auto simp add: fps_eq_iff pochhammer_fact[symmetric]
3670 fps_compose_nth power_mult_distrib cond_value_iff setsum_delta' cong del: if_weak_cong)
3673 lemma F_B[simp]: "F [-a] [] (- 1) = fps_binomial a"
3674 by (simp add: fps_eq_iff gbinomial_pochhammer algebra_simps)
3676 lemma F_0[simp]: "F as bs c $0 = 1"
3678 apply (subgoal_tac "ALL as. foldl (%(r::'a) (a::'a). r) 1 as = 1")
3680 apply (induct_tac as)
3684 lemma foldl_prod_prod:
3685 "foldl (%(r::'b::comm_ring_1) (x::'a::comm_ring_1). r * f x) v as * foldl (%r x. r * g x) w as =
3686 foldl (%r x. r * f x * g x) (v*w) as"
3687 by (induct as arbitrary: v w) (auto simp add: algebra_simps)
3691 "F as bs c $ Suc n = ((foldl (%r a. r* (a + of_nat n)) c as) /
3692 (foldl (%r b. r * (b + of_nat n)) (of_nat (Suc n)) bs )) * F as bs c $ n"
3693 apply (simp del: of_nat_Suc of_nat_add fact_Suc)
3694 apply (simp add: foldl_mult_start del: fact_Suc of_nat_Suc)
3695 unfolding foldl_prod_prod[unfolded foldl_mult_start] pochhammer_Suc
3696 apply (simp add: algebra_simps of_nat_mult)
3699 lemma XD_nth[simp]: "XD a $ n = (if n=0 then 0 else of_nat n * a$n)"
3700 by (simp add: XD_def)
3702 lemma XD_0th[simp]: "XD a $ 0 = 0" by simp
3703 lemma XD_Suc[simp]:" XD a $ Suc n = of_nat (Suc n) * a $ Suc n" by simp
3705 definition "XDp c a = XD a + fps_const c * a"
3707 lemma XDp_nth[simp]: "XDp c a $ n = (c + of_nat n) * a$n"
3708 by (simp add: XDp_def algebra_simps)
3710 lemma XDp_commute: "XDp b o XDp (c::'a::comm_ring_1) = XDp c o XDp b"
3711 by (auto simp add: XDp_def fun_eq_iff fps_eq_iff algebra_simps)
3713 lemma XDp0 [simp]: "XDp 0 = XD"
3714 by (simp add: fun_eq_iff fps_eq_iff)
3716 lemma XDp_fps_integral [simp]: "XDp 0 (fps_integral a c) = X * a"
3717 by (simp add: fps_eq_iff fps_integral_def)
3720 "F [- of_nat n] [- of_nat (n + m)] (c::'a::{field_char_0, field_inverse_zero}) $ k =
3722 pochhammer (- of_nat n) k * c ^ k / (pochhammer (- of_nat (n + m)) k * of_nat (fact k))
3724 "F [- of_nat m] [- of_nat (m + n)] (c::'a::{field_char_0, field_inverse_zero}) $ k =
3726 pochhammer (- of_nat m) k * c ^ k / (pochhammer (- of_nat (m + n)) k * of_nat (fact k))
3728 by (auto simp add: pochhammer_eq_0_iff)
3730 lemma setsum_eq_if: "setsum f {(n::nat) .. m} = (if m < n then 0 else f n + setsum f {n+1 .. m})"
3732 apply (subst setsum_insert[symmetric])
3733 apply (auto simp add: not_less setsum_head_Suc)
3736 lemma pochhammer_rec_if: "pochhammer a n = (if n = 0 then 1 else a * pochhammer (a + 1) (n - 1))"
3737 by (cases n) (simp_all add: pochhammer_rec)
3739 lemma XDp_foldr_nth [simp]: "foldr (%c r. XDp c o r) cs (%c. XDp c a) c0 $ n =
3740 foldr (%c r. (c + of_nat n) * r) cs (c0 + of_nat n) * a$n"
3741 by (induct cs arbitrary: c0) (auto simp add: algebra_simps)
3743 lemma genric_XDp_foldr_nth:
3744 assumes f: "ALL n c a. f c a $ n = (of_nat n + k c) * a$n"
3745 shows "foldr (%c r. f c o r) cs (%c. g c a) c0 $ n =
3746 foldr (%c r. (k c + of_nat n) * r) cs (g c0 a $ n)"
3747 by (induct cs arbitrary: c0) (auto simp add: algebra_simps f)
3749 lemma dist_less_imp_nth_equal:
3750 assumes "dist f g < inverse (2 ^ i)"
3752 shows "f $ j = g $ j"
3754 assume "f $ j \<noteq> g $ j"
3755 then have "\<exists>n. f $ n \<noteq> g $ n" by auto
3756 with assms have "i < (LEAST n. f $ n \<noteq> g $ n)"
3757 by (simp add: split_if_asm dist_fps_def)
3758 also have "\<dots> \<le> j"
3759 using `f $ j \<noteq> g $ j` by (auto intro: Least_le)
3760 finally show False using `j \<le> i` by simp
3763 lemma nth_equal_imp_dist_less:
3764 assumes "\<And>j. j \<le> i \<Longrightarrow> f $ j = g $ j"
3765 shows "dist f g < inverse (2 ^ i)"
3766 proof (cases "f = g")
3768 hence "\<exists>n. f $ n \<noteq> g $ n" by (simp add: fps_eq_iff)
3769 with assms have "dist f g = inverse (2 ^ (LEAST n. f $ n \<noteq> g $ n))"
3770 by (simp add: split_if_asm dist_fps_def)
3772 from assms `\<exists>n. f $ n \<noteq> g $ n` have "i < (LEAST n. f $ n \<noteq> g $ n)"
3773 by (metis (mono_tags) LeastI not_less)
3774 ultimately show ?thesis by simp
3777 lemma dist_less_eq_nth_equal: "dist f g < inverse (2 ^ i) \<longleftrightarrow> (\<forall>j \<le> i. f $ j = g $ j)"
3778 using dist_less_imp_nth_equal nth_equal_imp_dist_less by blast
3780 instance fps :: (comm_ring_1) complete_space
3782 fix X::"nat \<Rightarrow> 'a fps"
3786 have "0 < inverse ((2::real)^i)" by simp
3787 from metric_CauchyD[OF `Cauchy X` this] dist_less_imp_nth_equal
3788 have "\<exists>M. \<forall>m \<ge> M. \<forall>j\<le>i. X M $ j = X m $ j" by blast
3790 then obtain M where M: "\<forall>i. \<forall>m \<ge> M i. \<forall>j \<le> i. X (M i) $ j = X m $ j" by metis
3791 hence "\<forall>i. \<forall>m \<ge> M i. \<forall>j \<le> i. X (M i) $ j = X m $ j" by metis
3793 proof (rule convergentI)
3794 show "X ----> Abs_fps (\<lambda>i. X (M i) $ i)"
3795 unfolding tendsto_iff
3797 fix e::real assume "0 < e"
3798 with LIMSEQ_inverse_realpow_zero[of 2, simplified, simplified filterlim_iff,
3799 THEN spec, of "\<lambda>x. x < e"]
3800 have "eventually (\<lambda>i. inverse (2 ^ i) < e) sequentially"
3802 apply (auto simp: eventually_nhds)
3804 then obtain i where "inverse (2 ^ i) < e" by (auto simp: eventually_sequentially)
3805 have "eventually (\<lambda>x. M i \<le> x) sequentially" by (auto simp: eventually_sequentially)
3806 thus "eventually (\<lambda>x. dist (X x) (Abs_fps (\<lambda>i. X (M i) $ i)) < e) sequentially"
3807 proof eventually_elim
3809 assume "M i \<le> x"
3811 have "\<And>j. j \<le> i \<Longrightarrow> X (M i) $ j = X (M j) $ j"
3812 using M by (metis nat_le_linear)
3813 ultimately have "dist (X x) (Abs_fps (\<lambda>j. X (M j) $ j)) < inverse (2 ^ i)"
3814 using M by (force simp: dist_less_eq_nth_equal)
3815 also note `inverse (2 ^ i) < e`
3816 finally show "dist (X x) (Abs_fps (\<lambda>j. X (M j) $ j)) < e" .