src/HOL/Library/Formal_Power_Series.thy
changeset 31021 53642251a04f
parent 30994 ba5bce0c26de
child 31073 4b44c4d08aa6
equal deleted inserted replaced
31020:9999a77590c3 31021:53642251a04f
   678 lemma fps_deriv_maclauren_0: "(fps_nth_deriv k (f:: ('a::comm_semiring_1) fps)) $ 0 = of_nat (fact k) * f$(k)"
   678 lemma fps_deriv_maclauren_0: "(fps_nth_deriv k (f:: ('a::comm_semiring_1) fps)) $ 0 = of_nat (fact k) * f$(k)"
   679   by (induct k arbitrary: f) (auto simp add: ring_simps of_nat_mult)
   679   by (induct k arbitrary: f) (auto simp add: ring_simps of_nat_mult)
   680 
   680 
   681 subsection {* Powers*}
   681 subsection {* Powers*}
   682 
   682 
   683 instance fps :: (semiring_1) recpower ..
       
   684 
       
   685 lemma fps_power_zeroth_eq_one: "a$0 =1 \<Longrightarrow> a^n $ 0 = (1::'a::semiring_1)"
   683 lemma fps_power_zeroth_eq_one: "a$0 =1 \<Longrightarrow> a^n $ 0 = (1::'a::semiring_1)"
   686   by (induct n, auto simp add: expand_fps_eq fps_mult_nth)
   684   by (induct n, auto simp add: expand_fps_eq fps_mult_nth)
   687 
   685 
   688 lemma fps_power_first_eq: "(a:: 'a::comm_ring_1 fps)$0 =1 \<Longrightarrow> a^n $ 1 = of_nat n * a$1"
   686 lemma fps_power_first_eq: "(a:: 'a::comm_ring_1 fps)$0 =1 \<Longrightarrow> a^n $ 1 = of_nat n * a$1"
   689 proof(induct n)
   687 proof(induct n)
   699   by (induct n, auto simp add: fps_mult_nth)
   697   by (induct n, auto simp add: fps_mult_nth)
   700 
   698 
   701 lemma startsby_zero_power:"a $0 = (0::'a::comm_ring_1) \<Longrightarrow> n > 0 \<Longrightarrow> a^n $0 = 0"
   699 lemma startsby_zero_power:"a $0 = (0::'a::comm_ring_1) \<Longrightarrow> n > 0 \<Longrightarrow> a^n $0 = 0"
   702   by (induct n, auto simp add: fps_mult_nth)
   700   by (induct n, auto simp add: fps_mult_nth)
   703 
   701 
   704 lemma startsby_power:"a $0 = (v::'a::{comm_ring_1, recpower}) \<Longrightarrow> a^n $0 = v^n"
   702 lemma startsby_power:"a $0 = (v::'a::{comm_ring_1}) \<Longrightarrow> a^n $0 = v^n"
   705   by (induct n, auto simp add: fps_mult_nth power_Suc)
   703   by (induct n, auto simp add: fps_mult_nth power_Suc)
   706 
   704 
   707 lemma startsby_zero_power_iff[simp]:
   705 lemma startsby_zero_power_iff[simp]:
   708   "a^n $0 = (0::'a::{idom, recpower}) \<longleftrightarrow> (n \<noteq> 0 \<and> a$0 = 0)"
   706   "a^n $0 = (0::'a::{idom}) \<longleftrightarrow> (n \<noteq> 0 \<and> a$0 = 0)"
   709 apply (rule iffI)
   707 apply (rule iffI)
   710 apply (induct n, auto simp add: power_Suc fps_mult_nth)
   708 apply (induct n, auto simp add: power_Suc fps_mult_nth)
   711 by (rule startsby_zero_power, simp_all)
   709 by (rule startsby_zero_power, simp_all)
   712 
   710 
   713 lemma startsby_zero_power_prefix:
   711 lemma startsby_zero_power_prefix:
   746   apply (rule setsum_mono_zero_right)
   744   apply (rule setsum_mono_zero_right)
   747   using kn apply auto
   745   using kn apply auto
   748   apply (rule startsby_zero_power_prefix[rule_format, OF a0])
   746   apply (rule startsby_zero_power_prefix[rule_format, OF a0])
   749   by arith
   747   by arith
   750 
   748 
   751 lemma startsby_zero_power_nth_same: assumes a0: "a$0 = (0::'a::{recpower, idom})"
   749 lemma startsby_zero_power_nth_same: assumes a0: "a$0 = (0::'a::{idom})"
   752   shows "a^n $ n = (a$1) ^ n"
   750   shows "a^n $ n = (a$1) ^ n"
   753 proof(induct n)
   751 proof(induct n)
   754   case 0 thus ?case by (simp add: power_0)
   752   case 0 thus ?case by (simp add: power_0)
   755 next
   753 next
   756   case (Suc n)
   754   case (Suc n)
   767   also have "\<dots> = a^n $ n * a$1" using a0 by simp
   765   also have "\<dots> = a^n $ n * a$1" using a0 by simp
   768   finally show ?case using Suc.hyps by (simp add: power_Suc)
   766   finally show ?case using Suc.hyps by (simp add: power_Suc)
   769 qed
   767 qed
   770 
   768 
   771 lemma fps_inverse_power:
   769 lemma fps_inverse_power:
   772   fixes a :: "('a::{field, recpower}) fps"
   770   fixes a :: "('a::{field}) fps"
   773   shows "inverse (a^n) = inverse a ^ n"
   771   shows "inverse (a^n) = inverse a ^ n"
   774 proof-
   772 proof-
   775   {assume a0: "a$0 = 0"
   773   {assume a0: "a$0 = 0"
   776     hence eq: "inverse a = 0" by (simp add: fps_inverse_def)
   774     hence eq: "inverse a = 0" by (simp add: fps_inverse_def)
   777     {assume "n = 0" hence ?thesis by simp}
   775     {assume "n = 0" hence ?thesis by simp}
   856   using fps_inverse_deriv[OF a0]
   854   using fps_inverse_deriv[OF a0]
   857   by (simp add: fps_divide_def ring_simps power2_eq_square fps_inverse_mult inverse_mult_eq_1'[OF a0])
   855   by (simp add: fps_divide_def ring_simps power2_eq_square fps_inverse_mult inverse_mult_eq_1'[OF a0])
   858 
   856 
   859 subsection{* The eXtractor series X*}
   857 subsection{* The eXtractor series X*}
   860 
   858 
   861 lemma minus_one_power_iff: "(- (1::'a :: {recpower, comm_ring_1})) ^ n = (if even n then 1 else - 1)"
   859 lemma minus_one_power_iff: "(- (1::'a :: {comm_ring_1})) ^ n = (if even n then 1 else - 1)"
   862   by (induct n, auto)
   860   by (induct n, auto)
   863 
   861 
   864 definition "X = Abs_fps (\<lambda>n. if n = 1 then 1 else 0)"
   862 definition "X = Abs_fps (\<lambda>n. if n = 1 then 1 else 0)"
   865 
   863 
   866 lemma fps_inverse_gp': "inverse (Abs_fps(\<lambda>n. (1::'a::field)))
   864 lemma fps_inverse_gp': "inverse (Abs_fps(\<lambda>n. (1::'a::field)))
   913 lemma X_nth[simp]: "X$n = (if n = 1 then 1 else 0)" by (simp add: X_def)
   911 lemma X_nth[simp]: "X$n = (if n = 1 then 1 else 0)" by (simp add: X_def)
   914 lemma X_power_nth[simp]: "(X^k) $n = (if n = k then 1 else (0::'a::comm_ring_1))"
   912 lemma X_power_nth[simp]: "(X^k) $n = (if n = k then 1 else (0::'a::comm_ring_1))"
   915   by (simp add: X_power_iff)
   913   by (simp add: X_power_iff)
   916 
   914 
   917 lemma fps_inverse_X_plus1:
   915 lemma fps_inverse_X_plus1:
   918   "inverse (1 + X) = Abs_fps (\<lambda>n. (- (1::'a::{recpower, field})) ^ n)" (is "_ = ?r")
   916   "inverse (1 + X) = Abs_fps (\<lambda>n. (- (1::'a::{field})) ^ n)" (is "_ = ?r")
   919 proof-
   917 proof-
   920   have eq: "(1 + X) * ?r = 1"
   918   have eq: "(1 + X) * ?r = 1"
   921     unfolding minus_one_power_iff
   919     unfolding minus_one_power_iff
   922     apply (auto simp add: ring_simps fps_eq_iff)
   920     apply (auto simp add: ring_simps fps_eq_iff)
   923     by presburger+
   921     by presburger+
  1012 
  1010 
  1013 lemma fps_mult_X_deriv_shift: "X* fps_deriv a = Abs_fps (\<lambda>n. of_nat n* a$n)" by (simp add: fps_eq_iff)
  1011 lemma fps_mult_X_deriv_shift: "X* fps_deriv a = Abs_fps (\<lambda>n. of_nat n* a$n)" by (simp add: fps_eq_iff)
  1014 
  1012 
  1015 
  1013 
  1016 lemma fps_mult_XD_shift:
  1014 lemma fps_mult_XD_shift:
  1017   "(XD ^^ k) (a:: ('a::{comm_ring_1, recpower}) fps) = Abs_fps (\<lambda>n. (of_nat n ^ k) * a$n)"
  1015   "(XD ^^ k) (a:: ('a::{comm_ring_1}) fps) = Abs_fps (\<lambda>n. (of_nat n ^ k) * a$n)"
  1018   by (induct k arbitrary: a) (simp_all add: power_Suc XD_def fps_eq_iff ring_simps del: One_nat_def)
  1016   by (induct k arbitrary: a) (simp_all add: power_Suc XD_def fps_eq_iff ring_simps del: One_nat_def)
  1019 
  1017 
  1020 subsubsection{* Rule 3 is trivial and is given by @{text fps_times_def}*}
  1018 subsubsection{* Rule 3 is trivial and is given by @{text fps_times_def}*}
  1021 subsubsection{* Rule 5 --- summation and "division" by (1 - X)*}
  1019 subsubsection{* Rule 5 --- summation and "division" by (1 - X)*}
  1022 
  1020 
  1294   fixes m :: nat and a :: "('a::comm_ring_1) fps"
  1292   fixes m :: nat and a :: "('a::comm_ring_1) fps"
  1295   shows "(a ^m)$n = (if m=0 then 1$n else setsum (\<lambda>v. setprod (\<lambda>j. a $ (v!j)) {0..m - 1}) (natpermute n m))"
  1293   shows "(a ^m)$n = (if m=0 then 1$n else setsum (\<lambda>v. setprod (\<lambda>j. a $ (v!j)) {0..m - 1}) (natpermute n m))"
  1296   by (cases m, simp_all add: fps_power_nth_Suc del: power_Suc)
  1294   by (cases m, simp_all add: fps_power_nth_Suc del: power_Suc)
  1297 
  1295 
  1298 lemma fps_nth_power_0:
  1296 lemma fps_nth_power_0:
  1299   fixes m :: nat and a :: "('a::{comm_ring_1, recpower}) fps"
  1297   fixes m :: nat and a :: "('a::{comm_ring_1}) fps"
  1300   shows "(a ^m)$0 = (a$0) ^ m"
  1298   shows "(a ^m)$0 = (a$0) ^ m"
  1301 proof-
  1299 proof-
  1302   {assume "m=0" hence ?thesis by simp}
  1300   {assume "m=0" hence ?thesis by simp}
  1303   moreover
  1301   moreover
  1304   {fix n assume m: "m = Suc n"
  1302   {fix n assume m: "m = Suc n"
  1310    finally have ?thesis .}
  1308    finally have ?thesis .}
  1311  ultimately show ?thesis by (cases m, auto)
  1309  ultimately show ?thesis by (cases m, auto)
  1312 qed
  1310 qed
  1313 
  1311 
  1314 lemma fps_compose_inj_right:
  1312 lemma fps_compose_inj_right:
  1315   assumes a0: "a$0 = (0::'a::{recpower,idom})"
  1313   assumes a0: "a$0 = (0::'a::{idom})"
  1316   and a1: "a$1 \<noteq> 0"
  1314   and a1: "a$1 \<noteq> 0"
  1317   shows "(b oo a = c oo a) \<longleftrightarrow> b = c" (is "?lhs \<longleftrightarrow>?rhs")
  1315   shows "(b oo a = c oo a) \<longleftrightarrow> b = c" (is "?lhs \<longleftrightarrow>?rhs")
  1318 proof-
  1316 proof-
  1319   {assume ?rhs then have "?lhs" by simp}
  1317   {assume ?rhs then have "?lhs" by simp}
  1320   moreover
  1318   moreover
  1351 
  1349 
  1352 
  1350 
  1353 subsection {* Radicals *}
  1351 subsection {* Radicals *}
  1354 
  1352 
  1355 declare setprod_cong[fundef_cong]
  1353 declare setprod_cong[fundef_cong]
  1356 function radical :: "(nat \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> nat \<Rightarrow> ('a::{field, recpower}) fps \<Rightarrow> nat \<Rightarrow> 'a" where
  1354 function radical :: "(nat \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> nat \<Rightarrow> ('a::{field}) fps \<Rightarrow> nat \<Rightarrow> 'a" where
  1357   "radical r 0 a 0 = 1"
  1355   "radical r 0 a 0 = 1"
  1358 | "radical r 0 a (Suc n) = 0"
  1356 | "radical r 0 a (Suc n) = 0"
  1359 | "radical r (Suc k) a 0 = r (Suc k) (a$0)"
  1357 | "radical r (Suc k) a 0 = r (Suc k) (a$0)"
  1360 | "radical r (Suc k) a (Suc n) = (a$ Suc n - setsum (\<lambda>xs. setprod (\<lambda>j. radical r (Suc k) a (xs ! j)) {0..k}) {xs. xs \<in> natpermute (Suc n) (Suc k) \<and> Suc n \<notin> set xs}) / (of_nat (Suc k) * (radical r (Suc k) a 0)^k)"
  1358 | "radical r (Suc k) a (Suc n) = (a$ Suc n - setsum (\<lambda>xs. setprod (\<lambda>j. radical r (Suc k) a (xs ! j)) {0..k}) {xs. xs \<in> natpermute (Suc n) (Suc k) \<and> Suc n \<notin> set xs}) / (of_nat (Suc k) * (radical r (Suc k) a 0)^k)"
  1361 by pat_completeness auto
  1359 by pat_completeness auto
  1439   from card_UN_disjoint[OF fK fAK d]
  1437   from card_UN_disjoint[OF fK fAK d]
  1440   show "card (\<Union>i\<in>{0..k}. {replicate (k + 1) 0[i := n]}) = k+1" by simp
  1438   show "card (\<Union>i\<in>{0..k}. {replicate (k + 1) 0[i := n]}) = k+1" by simp
  1441 qed
  1439 qed
  1442 
  1440 
  1443 lemma power_radical:
  1441 lemma power_radical:
  1444   fixes a:: "'a ::{field, ring_char_0, recpower} fps"
  1442   fixes a:: "'a ::{field, ring_char_0} fps"
  1445   assumes r0: "(r (Suc k) (a$0)) ^ Suc k = a$0" and a0: "a$0 \<noteq> 0"
  1443   assumes r0: "(r (Suc k) (a$0)) ^ Suc k = a$0" and a0: "a$0 \<noteq> 0"
  1446   shows "(fps_radical r (Suc k) a) ^ (Suc k) = a"
  1444   shows "(fps_radical r (Suc k) a) ^ (Suc k) = a"
  1447 proof-
  1445 proof-
  1448   let ?r = "fps_radical r (Suc k) a"
  1446   let ?r = "fps_radical r (Suc k) a"
  1449   from a0 r0 have r00: "r (Suc k) (a$0) \<noteq> 0" by auto
  1447   from a0 r0 have r00: "r (Suc k) (a$0) \<noteq> 0" by auto
  1500   then show "a = b/c" unfolding  field_inverse[OF c0] by simp
  1498   then show "a = b/c" unfolding  field_inverse[OF c0] by simp
  1501 qed
  1499 qed
  1502 
  1500 
  1503 lemma radical_unique:
  1501 lemma radical_unique:
  1504   assumes r0: "(r (Suc k) (b$0)) ^ Suc k = b$0"
  1502   assumes r0: "(r (Suc k) (b$0)) ^ Suc k = b$0"
  1505   and a0: "r (Suc k) (b$0 ::'a::{field, ring_char_0, recpower}) = a$0" and b0: "b$0 \<noteq> 0"
  1503   and a0: "r (Suc k) (b$0 ::'a::{field, ring_char_0}) = a$0" and b0: "b$0 \<noteq> 0"
  1506   shows "a^(Suc k) = b \<longleftrightarrow> a = fps_radical r (Suc k) b"
  1504   shows "a^(Suc k) = b \<longleftrightarrow> a = fps_radical r (Suc k) b"
  1507 proof-
  1505 proof-
  1508   let ?r = "fps_radical r (Suc k) b"
  1506   let ?r = "fps_radical r (Suc k) b"
  1509   have r00: "r (Suc k) (b$0) \<noteq> 0" using b0 r0 by auto
  1507   have r00: "r (Suc k) (b$0) \<noteq> 0" using b0 r0 by auto
  1510   {assume H: "a = ?r"
  1508   {assume H: "a = ?r"
  1595 qed
  1593 qed
  1596 
  1594 
  1597 
  1595 
  1598 lemma radical_power:
  1596 lemma radical_power:
  1599   assumes r0: "r (Suc k) ((a$0) ^ Suc k) = a$0"
  1597   assumes r0: "r (Suc k) ((a$0) ^ Suc k) = a$0"
  1600   and a0: "(a$0 ::'a::{field, ring_char_0, recpower}) \<noteq> 0"
  1598   and a0: "(a$0 ::'a::{field, ring_char_0}) \<noteq> 0"
  1601   shows "(fps_radical r (Suc k) (a ^ Suc k)) = a"
  1599   shows "(fps_radical r (Suc k) (a ^ Suc k)) = a"
  1602 proof-
  1600 proof-
  1603   let ?ak = "a^ Suc k"
  1601   let ?ak = "a^ Suc k"
  1604   have ak0: "?ak $ 0 = (a$0) ^ Suc k" by (simp add: fps_nth_power_0 del: power_Suc)
  1602   have ak0: "?ak $ 0 = (a$0) ^ Suc k" by (simp add: fps_nth_power_0 del: power_Suc)
  1605   from r0 have th0: "r (Suc k) (a ^ Suc k $ 0) ^ Suc k = a ^ Suc k $ 0" using ak0 by auto
  1603   from r0 have th0: "r (Suc k) (a ^ Suc k $ 0) ^ Suc k = a ^ Suc k $ 0" using ak0 by auto
  1607   from ak0 a0 have ak00: "?ak $ 0 \<noteq>0 " by auto
  1605   from ak0 a0 have ak00: "?ak $ 0 \<noteq>0 " by auto
  1608   from radical_unique[of r k ?ak a, OF th0 th1 ak00] show ?thesis by metis
  1606   from radical_unique[of r k ?ak a, OF th0 th1 ak00] show ?thesis by metis
  1609 qed
  1607 qed
  1610 
  1608 
  1611 lemma fps_deriv_radical:
  1609 lemma fps_deriv_radical:
  1612   fixes a:: "'a ::{field, ring_char_0, recpower} fps"
  1610   fixes a:: "'a ::{field, ring_char_0} fps"
  1613   assumes r0: "(r (Suc k) (a$0)) ^ Suc k = a$0" and a0: "a$0 \<noteq> 0"
  1611   assumes r0: "(r (Suc k) (a$0)) ^ Suc k = a$0" and a0: "a$0 \<noteq> 0"
  1614   shows "fps_deriv (fps_radical r (Suc k) a) = fps_deriv a / (fps_const (of_nat (Suc k)) * (fps_radical r (Suc k) a) ^ k)"
  1612   shows "fps_deriv (fps_radical r (Suc k) a) = fps_deriv a / (fps_const (of_nat (Suc k)) * (fps_radical r (Suc k) a) ^ k)"
  1615 proof-
  1613 proof-
  1616   let ?r= "fps_radical r (Suc k) a"
  1614   let ?r= "fps_radical r (Suc k) a"
  1617   let ?w = "(fps_const (of_nat (Suc k)) * ?r ^ k)"
  1615   let ?w = "(fps_const (of_nat (Suc k)) * ?r ^ k)"
  1628     by (simp add: fps_divide_def)
  1626     by (simp add: fps_divide_def)
  1629   then show ?thesis unfolding th0 by simp
  1627   then show ?thesis unfolding th0 by simp
  1630 qed
  1628 qed
  1631 
  1629 
  1632 lemma radical_mult_distrib:
  1630 lemma radical_mult_distrib:
  1633   fixes a:: "'a ::{field, ring_char_0, recpower} fps"
  1631   fixes a:: "'a ::{field, ring_char_0} fps"
  1634   assumes
  1632   assumes
  1635   ra0: "r (k) (a $ 0) ^ k = a $ 0"
  1633   ra0: "r (k) (a $ 0) ^ k = a $ 0"
  1636   and rb0: "r (k) (b $ 0) ^ k = b $ 0"
  1634   and rb0: "r (k) (b $ 0) ^ k = b $ 0"
  1637   and r0': "r (k) ((a * b) $ 0) = r (k) (a $ 0) * r (k) (b $ 0)"
  1635   and r0': "r (k) ((a * b) $ 0) = r (k) (a $ 0) * r (k) (b $ 0)"
  1638   and a0: "a$0 \<noteq> 0"
  1636   and a0: "a$0 \<noteq> 0"
  1654   have ?thesis by (auto simp add: power_mult_distrib simp del: power_Suc)}
  1652   have ?thesis by (auto simp add: power_mult_distrib simp del: power_Suc)}
  1655 ultimately show ?thesis by (cases k, auto)
  1653 ultimately show ?thesis by (cases k, auto)
  1656 qed
  1654 qed
  1657 
  1655 
  1658 lemma radical_inverse:
  1656 lemma radical_inverse:
  1659   fixes a:: "'a ::{field, ring_char_0, recpower} fps"
  1657   fixes a:: "'a ::{field, ring_char_0} fps"
  1660   assumes
  1658   assumes
  1661   ra0: "r (k) (a $ 0) ^ k = a $ 0"
  1659   ra0: "r (k) (a $ 0) ^ k = a $ 0"
  1662   and ria0: "r (k) (inverse (a $ 0)) = inverse (r (k) (a $ 0))"
  1660   and ria0: "r (k) (inverse (a $ 0)) = inverse (r (k) (a $ 0))"
  1663   and r1: "(r (k) 1) = 1"
  1661   and r1: "(r (k) 1) = 1"
  1664   and a0: "a$0 \<noteq> 0"
  1662   and a0: "a$0 \<noteq> 0"
  1696 
  1694 
  1697 lemma fps_divide_inverse: "(a::('a::field) fps) / b = a * inverse b"
  1695 lemma fps_divide_inverse: "(a::('a::field) fps) / b = a * inverse b"
  1698   by (simp add: fps_divide_def)
  1696   by (simp add: fps_divide_def)
  1699 
  1697 
  1700 lemma radical_divide:
  1698 lemma radical_divide:
  1701   fixes a:: "'a ::{field, ring_char_0, recpower} fps"
  1699   fixes a:: "'a ::{field, ring_char_0} fps"
  1702   assumes
  1700   assumes
  1703       ra0: "r k (a $ 0) ^ k = a $ 0"
  1701       ra0: "r k (a $ 0) ^ k = a $ 0"
  1704   and rb0: "r k (b $ 0) ^ k = b $ 0"
  1702   and rb0: "r k (b $ 0) ^ k = b $ 0"
  1705   and r1: "r k 1 = 1"
  1703   and r1: "r k 1 = 1"
  1706   and rb0': "r k (inverse (b $ 0)) = inverse (r k (b $ 0))"
  1704   and rb0': "r k (inverse (b $ 0)) = inverse (r k (b $ 0))"
  1816 qed
  1814 qed
  1817 
  1815 
  1818 subsection{* Compositional inverses *}
  1816 subsection{* Compositional inverses *}
  1819 
  1817 
  1820 
  1818 
  1821 fun compinv :: "'a fps \<Rightarrow> nat \<Rightarrow> 'a::{recpower,field}" where
  1819 fun compinv :: "'a fps \<Rightarrow> nat \<Rightarrow> 'a::{field}" where
  1822   "compinv a 0 = X$0"
  1820   "compinv a 0 = X$0"
  1823 | "compinv a (Suc n) = (X$ Suc n - setsum (\<lambda>i. (compinv a i) * (a^i)$Suc n) {0 .. n}) / (a$1) ^ Suc n"
  1821 | "compinv a (Suc n) = (X$ Suc n - setsum (\<lambda>i. (compinv a i) * (a^i)$Suc n) {0 .. n}) / (a$1) ^ Suc n"
  1824 
  1822 
  1825 definition "fps_inv a = Abs_fps (compinv a)"
  1823 definition "fps_inv a = Abs_fps (compinv a)"
  1826 
  1824 
  1847     qed}
  1845     qed}
  1848   then show ?thesis by (simp add: fps_eq_iff)
  1846   then show ?thesis by (simp add: fps_eq_iff)
  1849 qed
  1847 qed
  1850 
  1848 
  1851 
  1849 
  1852 fun gcompinv :: "'a fps \<Rightarrow> 'a fps \<Rightarrow> nat \<Rightarrow> 'a::{recpower,field}" where
  1850 fun gcompinv :: "'a fps \<Rightarrow> 'a fps \<Rightarrow> nat \<Rightarrow> 'a::{field}" where
  1853   "gcompinv b a 0 = b$0"
  1851   "gcompinv b a 0 = b$0"
  1854 | "gcompinv b a (Suc n) = (b$ Suc n - setsum (\<lambda>i. (gcompinv b a i) * (a^i)$Suc n) {0 .. n}) / (a$1) ^ Suc n"
  1852 | "gcompinv b a (Suc n) = (b$ Suc n - setsum (\<lambda>i. (gcompinv b a i) * (a^i)$Suc n) {0 .. n}) / (a$1) ^ Suc n"
  1855 
  1853 
  1856 definition "fps_ginv b a = Abs_fps (gcompinv b a)"
  1854 definition "fps_ginv b a = Abs_fps (gcompinv b a)"
  1857 
  1855 
  2100   with fps_compose_inj_right[OF a0 a1]
  2098   with fps_compose_inj_right[OF a0 a1]
  2101   show ?thesis by simp
  2099   show ?thesis by simp
  2102 qed
  2100 qed
  2103 
  2101 
  2104 lemma fps_inv_deriv:
  2102 lemma fps_inv_deriv:
  2105   assumes a0:"a$0 = (0::'a::{recpower,field})" and a1: "a$1 \<noteq> 0"
  2103   assumes a0:"a$0 = (0::'a::{field})" and a1: "a$1 \<noteq> 0"
  2106   shows "fps_deriv (fps_inv a) = inverse (fps_deriv a oo fps_inv a)"
  2104   shows "fps_deriv (fps_inv a) = inverse (fps_deriv a oo fps_inv a)"
  2107 proof-
  2105 proof-
  2108   let ?ia = "fps_inv a"
  2106   let ?ia = "fps_inv a"
  2109   let ?d = "fps_deriv a oo ?ia"
  2107   let ?d = "fps_deriv a oo ?ia"
  2110   let ?dia = "fps_deriv ?ia"
  2108   let ?dia = "fps_deriv ?ia"
  2120 subsection{* Elementary series *}
  2118 subsection{* Elementary series *}
  2121 
  2119 
  2122 subsubsection{* Exponential series *}
  2120 subsubsection{* Exponential series *}
  2123 definition "E x = Abs_fps (\<lambda>n. x^n / of_nat (fact n))"
  2121 definition "E x = Abs_fps (\<lambda>n. x^n / of_nat (fact n))"
  2124 
  2122 
  2125 lemma E_deriv[simp]: "fps_deriv (E a) = fps_const (a::'a::{field, recpower, ring_char_0}) * E a" (is "?l = ?r")
  2123 lemma E_deriv[simp]: "fps_deriv (E a) = fps_const (a::'a::{field, ring_char_0}) * E a" (is "?l = ?r")
  2126 proof-
  2124 proof-
  2127   {fix n
  2125   {fix n
  2128     have "?l$n = ?r $ n"
  2126     have "?l$n = ?r $ n"
  2129   apply (auto simp add: E_def field_simps power_Suc[symmetric]simp del: fact_Suc of_nat_Suc power_Suc)
  2127   apply (auto simp add: E_def field_simps power_Suc[symmetric]simp del: fact_Suc of_nat_Suc power_Suc)
  2130   by (simp add: of_nat_mult ring_simps)}
  2128   by (simp add: of_nat_mult ring_simps)}
  2131 then show ?thesis by (simp add: fps_eq_iff)
  2129 then show ?thesis by (simp add: fps_eq_iff)
  2132 qed
  2130 qed
  2133 
  2131 
  2134 lemma E_unique_ODE:
  2132 lemma E_unique_ODE:
  2135   "fps_deriv a = fps_const c * a \<longleftrightarrow> a = fps_const (a$0) * E (c :: 'a::{field, ring_char_0, recpower})"
  2133   "fps_deriv a = fps_const c * a \<longleftrightarrow> a = fps_const (a$0) * E (c :: 'a::{field, ring_char_0})"
  2136   (is "?lhs \<longleftrightarrow> ?rhs")
  2134   (is "?lhs \<longleftrightarrow> ?rhs")
  2137 proof-
  2135 proof-
  2138   {assume d: ?lhs
  2136   {assume d: ?lhs
  2139   from d have th: "\<And>n. a $ Suc n = c * a$n / of_nat (Suc n)"
  2137   from d have th: "\<And>n. a $ Suc n = c * a$n / of_nat (Suc n)"
  2140     by (simp add: fps_deriv_def fps_eq_iff field_simps del: of_nat_Suc)
  2138     by (simp add: fps_deriv_def fps_eq_iff field_simps del: of_nat_Suc)
  2157     apply (simp only: h[symmetric])
  2155     apply (simp only: h[symmetric])
  2158   by simp}
  2156   by simp}
  2159 ultimately show ?thesis by blast
  2157 ultimately show ?thesis by blast
  2160 qed
  2158 qed
  2161 
  2159 
  2162 lemma E_add_mult: "E (a + b) = E (a::'a::{ring_char_0, field, recpower}) * E b" (is "?l = ?r")
  2160 lemma E_add_mult: "E (a + b) = E (a::'a::{ring_char_0, field}) * E b" (is "?l = ?r")
  2163 proof-
  2161 proof-
  2164   have "fps_deriv (?r) = fps_const (a+b) * ?r"
  2162   have "fps_deriv (?r) = fps_const (a+b) * ?r"
  2165     by (simp add: fps_const_add[symmetric] ring_simps del: fps_const_add)
  2163     by (simp add: fps_const_add[symmetric] ring_simps del: fps_const_add)
  2166   then have "?r = ?l" apply (simp only: E_unique_ODE)
  2164   then have "?r = ?l" apply (simp only: E_unique_ODE)
  2167     by (simp add: fps_mult_nth E_def)
  2165     by (simp add: fps_mult_nth E_def)
  2169 qed
  2167 qed
  2170 
  2168 
  2171 lemma E_nth[simp]: "E a $ n = a^n / of_nat (fact n)"
  2169 lemma E_nth[simp]: "E a $ n = a^n / of_nat (fact n)"
  2172   by (simp add: E_def)
  2170   by (simp add: E_def)
  2173 
  2171 
  2174 lemma E0[simp]: "E (0::'a::{field, recpower}) = 1"
  2172 lemma E0[simp]: "E (0::'a::{field}) = 1"
  2175   by (simp add: fps_eq_iff power_0_left)
  2173   by (simp add: fps_eq_iff power_0_left)
  2176 
  2174 
  2177 lemma E_neg: "E (- a) = inverse (E (a::'a::{ring_char_0, field, recpower}))"
  2175 lemma E_neg: "E (- a) = inverse (E (a::'a::{ring_char_0, field}))"
  2178 proof-
  2176 proof-
  2179   from E_add_mult[of a "- a"] have th0: "E a * E (- a) = 1"
  2177   from E_add_mult[of a "- a"] have th0: "E a * E (- a) = 1"
  2180     by (simp )
  2178     by (simp )
  2181   have th1: "E a $ 0 \<noteq> 0" by simp
  2179   have th1: "E a $ 0 \<noteq> 0" by simp
  2182   from fps_inverse_unique[OF th1 th0] show ?thesis by simp
  2180   from fps_inverse_unique[OF th1 th0] show ?thesis by simp
  2183 qed
  2181 qed
  2184 
  2182 
  2185 lemma E_nth_deriv[simp]: "fps_nth_deriv n (E (a::'a::{field, recpower, ring_char_0})) = (fps_const a)^n * (E a)"
  2183 lemma E_nth_deriv[simp]: "fps_nth_deriv n (E (a::'a::{field, ring_char_0})) = (fps_const a)^n * (E a)"
  2186   by (induct n, auto simp add: power_Suc)
  2184   by (induct n, auto simp add: power_Suc)
  2187 
  2185 
  2188 lemma fps_compose_uminus: "- (a::'a::ring_1 fps) oo c = - (a oo c)"
  2186 lemma fps_compose_uminus: "- (a::'a::ring_1 fps) oo c = - (a oo c)"
  2189   by (simp add: fps_eq_iff fps_compose_nth ring_simps setsum_negf[symmetric])
  2187   by (simp add: fps_eq_iff fps_compose_nth ring_simps setsum_negf[symmetric])
  2190 
  2188 
  2193   unfolding diff_minus fps_compose_uminus fps_compose_add_distrib ..
  2191   unfolding diff_minus fps_compose_uminus fps_compose_add_distrib ..
  2194 
  2192 
  2195 lemma X_fps_compose:"X oo a = Abs_fps (\<lambda>n. if n = 0 then (0::'a::comm_ring_1) else a$n)"
  2193 lemma X_fps_compose:"X oo a = Abs_fps (\<lambda>n. if n = 0 then (0::'a::comm_ring_1) else a$n)"
  2196   by (simp add: fps_eq_iff fps_compose_nth mult_delta_left setsum_delta power_Suc)
  2194   by (simp add: fps_eq_iff fps_compose_nth mult_delta_left setsum_delta power_Suc)
  2197 
  2195 
  2198 lemma X_compose_E[simp]: "X oo E (a::'a::{field, recpower}) = E a - 1"
  2196 lemma X_compose_E[simp]: "X oo E (a::'a::{field}) = E a - 1"
  2199   by (simp add: fps_eq_iff X_fps_compose)
  2197   by (simp add: fps_eq_iff X_fps_compose)
  2200 
  2198 
  2201 lemma LE_compose:
  2199 lemma LE_compose:
  2202   assumes a: "a\<noteq>0"
  2200   assumes a: "a\<noteq>0"
  2203   shows "fps_inv (E a - 1) oo (E a - 1) = X"
  2201   shows "fps_inv (E a - 1) oo (E a - 1) = X"
  2215   "inverse (fps_const (a::'a::{field, division_by_zero})) = fps_const (inverse a)"
  2213   "inverse (fps_const (a::'a::{field, division_by_zero})) = fps_const (inverse a)"
  2216   apply (auto simp add: fps_eq_iff fps_inverse_def) by (case_tac "n", auto)
  2214   apply (auto simp add: fps_eq_iff fps_inverse_def) by (case_tac "n", auto)
  2217 
  2215 
  2218 
  2216 
  2219 lemma inverse_one_plus_X:
  2217 lemma inverse_one_plus_X:
  2220   "inverse (1 + X) = Abs_fps (\<lambda>n. (- 1 ::'a::{field, recpower})^n)"
  2218   "inverse (1 + X) = Abs_fps (\<lambda>n. (- 1 ::'a::{field})^n)"
  2221   (is "inverse ?l = ?r")
  2219   (is "inverse ?l = ?r")
  2222 proof-
  2220 proof-
  2223   have th: "?l * ?r = 1"
  2221   have th: "?l * ?r = 1"
  2224     apply (auto simp add: ring_simps fps_eq_iff X_mult_nth  minus_one_power_iff)
  2222     apply (auto simp add: ring_simps fps_eq_iff X_mult_nth  minus_one_power_iff)
  2225     apply presburger+
  2223     apply presburger+
  2226     done
  2224     done
  2227   have th': "?l $ 0 \<noteq> 0" by (simp add: )
  2225   have th': "?l $ 0 \<noteq> 0" by (simp add: )
  2228   from fps_inverse_unique[OF th' th] show ?thesis .
  2226   from fps_inverse_unique[OF th' th] show ?thesis .
  2229 qed
  2227 qed
  2230 
  2228 
  2231 lemma E_power_mult: "(E (c::'a::{field,recpower,ring_char_0}))^n = E (of_nat n * c)"
  2229 lemma E_power_mult: "(E (c::'a::{field,ring_char_0}))^n = E (of_nat n * c)"
  2232   by (induct n, auto simp add: ring_simps E_add_mult power_Suc)
  2230   by (induct n, auto simp add: ring_simps E_add_mult power_Suc)
  2233 
  2231 
  2234 subsubsection{* Logarithmic series *}
  2232 subsubsection{* Logarithmic series *}
  2235 definition "(L::'a::{field, ring_char_0,recpower} fps)
  2233 definition "(L::'a::{field, ring_char_0} fps)
  2236   = Abs_fps (\<lambda>n. (- 1) ^ Suc n / of_nat n)"
  2234   = Abs_fps (\<lambda>n. (- 1) ^ Suc n / of_nat n)"
  2237 
  2235 
  2238 lemma fps_deriv_L: "fps_deriv L = inverse (1 + X)"
  2236 lemma fps_deriv_L: "fps_deriv L = inverse (1 + X)"
  2239   unfolding inverse_one_plus_X
  2237   unfolding inverse_one_plus_X
  2240   by (simp add: L_def fps_eq_iff power_Suc del: of_nat_Suc)
  2238   by (simp add: L_def fps_eq_iff power_Suc del: of_nat_Suc)
  2241 
  2239 
  2242 lemma L_nth: "L $ n = (- 1) ^ Suc n / of_nat n"
  2240 lemma L_nth: "L $ n = (- 1) ^ Suc n / of_nat n"
  2243   by (simp add: L_def)
  2241   by (simp add: L_def)
  2244 
  2242 
  2245 lemma L_E_inv:
  2243 lemma L_E_inv:
  2246   assumes a: "a\<noteq> (0::'a::{field,division_by_zero,ring_char_0,recpower})"
  2244   assumes a: "a\<noteq> (0::'a::{field,division_by_zero,ring_char_0})"
  2247   shows "L = fps_const a * fps_inv (E a - 1)" (is "?l = ?r")
  2245   shows "L = fps_const a * fps_inv (E a - 1)" (is "?l = ?r")
  2248 proof-
  2246 proof-
  2249   let ?b = "E a - 1"
  2247   let ?b = "E a - 1"
  2250   have b0: "?b $ 0 = 0" by simp
  2248   have b0: "?b $ 0 = 0" by simp
  2251   have b1: "?b $ 1 \<noteq> 0" by (simp add: a)
  2249   have b1: "?b $ 1 \<noteq> 0" by (simp add: a)
  2265     by (simp add: L_nth fps_inv_def)
  2263     by (simp add: L_nth fps_inv_def)
  2266 qed
  2264 qed
  2267 
  2265 
  2268 subsubsection{* Formal trigonometric functions  *}
  2266 subsubsection{* Formal trigonometric functions  *}
  2269 
  2267 
  2270 definition "fps_sin (c::'a::{field, recpower, ring_char_0}) =
  2268 definition "fps_sin (c::'a::{field, ring_char_0}) =
  2271   Abs_fps (\<lambda>n. if even n then 0 else (- 1) ^((n - 1) div 2) * c^n /(of_nat (fact n)))"
  2269   Abs_fps (\<lambda>n. if even n then 0 else (- 1) ^((n - 1) div 2) * c^n /(of_nat (fact n)))"
  2272 
  2270 
  2273 definition "fps_cos (c::'a::{field, recpower, ring_char_0}) = Abs_fps (\<lambda>n. if even n then (- 1) ^ (n div 2) * c^n / (of_nat (fact n)) else 0)"
  2271 definition "fps_cos (c::'a::{field, ring_char_0}) = Abs_fps (\<lambda>n. if even n then (- 1) ^ (n div 2) * c^n / (of_nat (fact n)) else 0)"
  2274 
  2272 
  2275 lemma fps_sin_deriv:
  2273 lemma fps_sin_deriv:
  2276   "fps_deriv (fps_sin c) = fps_const c * fps_cos c"
  2274   "fps_deriv (fps_sin c) = fps_const c * fps_cos c"
  2277   (is "?lhs = ?rhs")
  2275   (is "?lhs = ?rhs")
  2278 proof-
  2276 proof-