src/HOL/MacLaurin.thy
 author wenzelm Sat Jul 18 22:58:50 2015 +0200 (2015-07-18) changeset 60758 d8d85a8172b5 parent 60017 b785d6d06430 child 61076 bdc1e2f0a86a permissions -rw-r--r--
isabelle update_cartouches;
```     1 (*  Author      : Jacques D. Fleuriot
```
```     2     Copyright   : 2001 University of Edinburgh
```
```     3     Conversion to Isar and new proofs by Lawrence C Paulson, 2004
```
```     4     Conversion of Mac Laurin to Isar by Lukas Bulwahn and Bernhard Häupler, 2005
```
```     5 *)
```
```     6
```
```     7 section\<open>MacLaurin Series\<close>
```
```     8
```
```     9 theory MacLaurin
```
```    10 imports Transcendental
```
```    11 begin
```
```    12
```
```    13 subsection\<open>Maclaurin's Theorem with Lagrange Form of Remainder\<close>
```
```    14
```
```    15 text\<open>This is a very long, messy proof even now that it's been broken down
```
```    16 into lemmas.\<close>
```
```    17
```
```    18 lemma Maclaurin_lemma:
```
```    19     "0 < h ==>
```
```    20      \<exists>B::real. f h = (\<Sum>m<n. (j m / (fact m)) * (h^m)) +
```
```    21                (B * ((h^n) /(fact n)))"
```
```    22 by (rule exI[where x = "(f h - (\<Sum>m<n. (j m / (fact m)) * h^m)) * (fact n) / (h^n)"]) simp
```
```    23
```
```    24 lemma eq_diff_eq': "(x = y - z) = (y = x + (z::real))"
```
```    25 by arith
```
```    26
```
```    27 lemma fact_diff_Suc [rule_format]:
```
```    28   "n < Suc m ==> fact (Suc m - n) = (Suc m - n) * fact (m - n)"
```
```    29   by (subst fact_reduce, auto)
```
```    30
```
```    31 lemma Maclaurin_lemma2:
```
```    32   fixes B
```
```    33   assumes DERIV : "\<forall>m t. m < n \<and> 0\<le>t \<and> t\<le>h \<longrightarrow> DERIV (diff m) t :> diff (Suc m) t"
```
```    34       and INIT : "n = Suc k"
```
```    35   defines "difg \<equiv>
```
```    36       (\<lambda>m t::real. diff m t -
```
```    37          ((\<Sum>p<n - m. diff (m + p) 0 / (fact p) * t ^ p) + B * (t ^ (n - m) / (fact (n - m)))))"
```
```    38         (is "difg \<equiv> (\<lambda>m t. diff m t - ?difg m t)")
```
```    39   shows "\<forall>m t. m < n & 0 \<le> t & t \<le> h --> DERIV (difg m) t :> difg (Suc m) t"
```
```    40 proof (rule allI impI)+
```
```    41   fix m and t::real
```
```    42   assume INIT2: "m < n & 0 \<le> t & t \<le> h"
```
```    43   have "DERIV (difg m) t :> diff (Suc m) t -
```
```    44     ((\<Sum>x<n - m. real x * t ^ (x - Suc 0) * diff (m + x) 0 / (fact x)) +
```
```    45      real (n - m) * t ^ (n - Suc m) * B / (fact (n - m)))"
```
```    46     unfolding difg_def
```
```    47     by (auto intro!: derivative_eq_intros DERIV[rule_format, OF INIT2]
```
```    48              simp: real_of_nat_def[symmetric])
```
```    49   moreover
```
```    50   from INIT2 have intvl: "{..<n - m} = insert 0 (Suc ` {..<n - Suc m})" and "0 < n - m"
```
```    51     unfolding atLeast0LessThan[symmetric] by auto
```
```    52   have "(\<Sum>x<n - m. real x * t ^ (x - Suc 0) * diff (m + x) 0 / (fact x)) =
```
```    53       (\<Sum>x<n - Suc m. real (Suc x) * t ^ x * diff (Suc m + x) 0 / (fact (Suc x)))"
```
```    54     unfolding intvl atLeast0LessThan by (subst setsum.insert) (auto simp: setsum.reindex)
```
```    55   moreover
```
```    56   have fact_neq_0: "\<And>x. (fact x) + real x * (fact x) \<noteq> 0"
```
```    57     by (metis add_pos_pos fact_gt_zero less_add_same_cancel1 less_add_same_cancel2 less_numeral_extra(3) mult_less_0_iff not_real_of_nat_less_zero)
```
```    58   have "\<And>x. (Suc x) * t ^ x * diff (Suc m + x) 0 / (fact (Suc x)) =
```
```    59             diff (Suc m + x) 0 * t^x / (fact x)"
```
```    60     by (rule nonzero_divide_eq_eq[THEN iffD2]) auto
```
```    61   moreover
```
```    62   have "(n - m) * t ^ (n - Suc m) * B / (fact (n - m)) =
```
```    63         B * (t ^ (n - Suc m) / (fact (n - Suc m)))"
```
```    64     using \<open>0 < n - m\<close>
```
```    65     by (simp add: divide_simps fact_reduce)
```
```    66   ultimately show "DERIV (difg m) t :> difg (Suc m) t"
```
```    67     unfolding difg_def by simp
```
```    68 qed
```
```    69
```
```    70 lemma Maclaurin:
```
```    71   assumes h: "0 < h"
```
```    72   assumes n: "0 < n"
```
```    73   assumes diff_0: "diff 0 = f"
```
```    74   assumes diff_Suc:
```
```    75     "\<forall>m t. m < n & 0 \<le> t & t \<le> h --> DERIV (diff m) t :> diff (Suc m) t"
```
```    76   shows
```
```    77     "\<exists>t::real. 0 < t & t < h &
```
```    78               f h =
```
```    79               setsum (%m. (diff m 0 / (fact m)) * h ^ m) {..<n} +
```
```    80               (diff n t / (fact n)) * h ^ n"
```
```    81 proof -
```
```    82   from n obtain m where m: "n = Suc m"
```
```    83     by (cases n) (simp add: n)
```
```    84
```
```    85   obtain B where f_h: "f h =
```
```    86         (\<Sum>m<n. diff m (0\<Colon>real) / (fact m) * h ^ m) + B * (h ^ n / (fact n))"
```
```    87     using Maclaurin_lemma [OF h] ..
```
```    88
```
```    89   def g \<equiv> "(\<lambda>t. f t -
```
```    90     (setsum (\<lambda>m. (diff m 0 / (fact m)) * t^m) {..<n} + (B * (t^n / (fact n)))))"
```
```    91
```
```    92   have g2: "g 0 = 0 & g h = 0"
```
```    93     by (simp add: m f_h g_def lessThan_Suc_eq_insert_0 image_iff diff_0 setsum.reindex)
```
```    94
```
```    95   def difg \<equiv> "(%m t. diff m t -
```
```    96     (setsum (%p. (diff (m + p) 0 / (fact p)) * (t ^ p)) {..<n-m}
```
```    97       + (B * ((t ^ (n - m)) / (fact (n - m))))))"
```
```    98
```
```    99   have difg_0: "difg 0 = g"
```
```   100     unfolding difg_def g_def by (simp add: diff_0)
```
```   101
```
```   102   have difg_Suc: "\<forall>(m\<Colon>nat) t\<Colon>real.
```
```   103         m < n \<and> (0\<Colon>real) \<le> t \<and> t \<le> h \<longrightarrow> DERIV (difg m) t :> difg (Suc m) t"
```
```   104     using diff_Suc m unfolding difg_def by (rule Maclaurin_lemma2)
```
```   105
```
```   106   have difg_eq_0: "\<forall>m<n. difg m 0 = 0"
```
```   107     by (auto simp: difg_def m Suc_diff_le lessThan_Suc_eq_insert_0 image_iff setsum.reindex)
```
```   108
```
```   109   have isCont_difg: "\<And>m x. \<lbrakk>m < n; 0 \<le> x; x \<le> h\<rbrakk> \<Longrightarrow> isCont (difg m) x"
```
```   110     by (rule DERIV_isCont [OF difg_Suc [rule_format]]) simp
```
```   111
```
```   112   have differentiable_difg:
```
```   113     "\<And>m x. \<lbrakk>m < n; 0 \<le> x; x \<le> h\<rbrakk> \<Longrightarrow> difg m differentiable (at x)"
```
```   114     by (rule differentiableI [OF difg_Suc [rule_format]]) simp
```
```   115
```
```   116   have difg_Suc_eq_0: "\<And>m t. \<lbrakk>m < n; 0 \<le> t; t \<le> h; DERIV (difg m) t :> 0\<rbrakk>
```
```   117         \<Longrightarrow> difg (Suc m) t = 0"
```
```   118     by (rule DERIV_unique [OF difg_Suc [rule_format]]) simp
```
```   119
```
```   120   have "m < n" using m by simp
```
```   121
```
```   122   have "\<exists>t. 0 < t \<and> t < h \<and> DERIV (difg m) t :> 0"
```
```   123   using \<open>m < n\<close>
```
```   124   proof (induct m)
```
```   125     case 0
```
```   126     show ?case
```
```   127     proof (rule Rolle)
```
```   128       show "0 < h" by fact
```
```   129       show "difg 0 0 = difg 0 h" by (simp add: difg_0 g2)
```
```   130       show "\<forall>x. 0 \<le> x \<and> x \<le> h \<longrightarrow> isCont (difg (0\<Colon>nat)) x"
```
```   131         by (simp add: isCont_difg n)
```
```   132       show "\<forall>x. 0 < x \<and> x < h \<longrightarrow> difg (0\<Colon>nat) differentiable (at x)"
```
```   133         by (simp add: differentiable_difg n)
```
```   134     qed
```
```   135   next
```
```   136     case (Suc m')
```
```   137     hence "\<exists>t. 0 < t \<and> t < h \<and> DERIV (difg m') t :> 0" by simp
```
```   138     then obtain t where t: "0 < t" "t < h" "DERIV (difg m') t :> 0" by fast
```
```   139     have "\<exists>t'. 0 < t' \<and> t' < t \<and> DERIV (difg (Suc m')) t' :> 0"
```
```   140     proof (rule Rolle)
```
```   141       show "0 < t" by fact
```
```   142       show "difg (Suc m') 0 = difg (Suc m') t"
```
```   143         using t \<open>Suc m' < n\<close> by (simp add: difg_Suc_eq_0 difg_eq_0)
```
```   144       show "\<forall>x. 0 \<le> x \<and> x \<le> t \<longrightarrow> isCont (difg (Suc m')) x"
```
```   145         using \<open>t < h\<close> \<open>Suc m' < n\<close> by (simp add: isCont_difg)
```
```   146       show "\<forall>x. 0 < x \<and> x < t \<longrightarrow> difg (Suc m') differentiable (at x)"
```
```   147         using \<open>t < h\<close> \<open>Suc m' < n\<close> by (simp add: differentiable_difg)
```
```   148     qed
```
```   149     thus ?case
```
```   150       using \<open>t < h\<close> by auto
```
```   151   qed
```
```   152   then obtain t where "0 < t" "t < h" "DERIV (difg m) t :> 0" by fast
```
```   153
```
```   154   hence "difg (Suc m) t = 0"
```
```   155     using \<open>m < n\<close> by (simp add: difg_Suc_eq_0)
```
```   156
```
```   157   show ?thesis
```
```   158   proof (intro exI conjI)
```
```   159     show "0 < t" by fact
```
```   160     show "t < h" by fact
```
```   161     show "f h = (\<Sum>m<n. diff m 0 / (fact m) * h ^ m) + diff n t / (fact n) * h ^ n"
```
```   162       using \<open>difg (Suc m) t = 0\<close>
```
```   163       by (simp add: m f_h difg_def)
```
```   164   qed
```
```   165 qed
```
```   166
```
```   167 lemma Maclaurin_objl:
```
```   168   "0 < h & n>0 & diff 0 = f &
```
```   169   (\<forall>m t. m < n & 0 \<le> t & t \<le> h --> DERIV (diff m) t :> diff (Suc m) t)
```
```   170    --> (\<exists>t::real. 0 < t & t < h &
```
```   171             f h = (\<Sum>m<n. diff m 0 / (fact m) * h ^ m) +
```
```   172                   diff n t / (fact n) * h ^ n)"
```
```   173 by (blast intro: Maclaurin)
```
```   174
```
```   175
```
```   176 lemma Maclaurin2:
```
```   177   assumes INIT1: "0 < h " and INIT2: "diff 0 = f"
```
```   178   and DERIV: "\<forall>m t::real.
```
```   179   m < n & 0 \<le> t & t \<le> h --> DERIV (diff m) t :> diff (Suc m) t"
```
```   180   shows "\<exists>t. 0 < t \<and> t \<le> h \<and> f h =
```
```   181   (\<Sum>m<n. diff m 0 / (fact m) * h ^ m) +
```
```   182   diff n t / (fact n) * h ^ n"
```
```   183 proof (cases "n")
```
```   184   case 0 with INIT1 INIT2 show ?thesis by fastforce
```
```   185 next
```
```   186   case Suc
```
```   187   hence "n > 0" by simp
```
```   188   from INIT1 this INIT2 DERIV have "\<exists>t>0. t < h \<and>
```
```   189     f h =
```
```   190     (\<Sum>m<n. diff m 0 / (fact m) * h ^ m) + diff n t / (fact n) * h ^ n"
```
```   191     by (rule Maclaurin)
```
```   192   thus ?thesis by fastforce
```
```   193 qed
```
```   194
```
```   195 lemma Maclaurin2_objl:
```
```   196      "0 < h & diff 0 = f &
```
```   197        (\<forall>m t. m < n & 0 \<le> t & t \<le> h --> DERIV (diff m) t :> diff (Suc m) t)
```
```   198     --> (\<exists>t::real. 0 < t &
```
```   199               t \<le> h &
```
```   200               f h =
```
```   201               (\<Sum>m<n. diff m 0 / (fact m) * h ^ m) +
```
```   202               diff n t / (fact n) * h ^ n)"
```
```   203 by (blast intro: Maclaurin2)
```
```   204
```
```   205 lemma Maclaurin_minus:
```
```   206   fixes h::real
```
```   207   assumes "h < 0" "0 < n" "diff 0 = f"
```
```   208   and DERIV: "\<forall>m t. m < n & h \<le> t & t \<le> 0 --> DERIV (diff m) t :> diff (Suc m) t"
```
```   209   shows "\<exists>t. h < t & t < 0 &
```
```   210          f h = (\<Sum>m<n. diff m 0 / (fact m) * h ^ m) +
```
```   211          diff n t / (fact n) * h ^ n"
```
```   212 proof -
```
```   213   txt "Transform @{text ABL'} into @{text derivative_intros} format."
```
```   214   note DERIV' = DERIV_chain'[OF _ DERIV[rule_format], THEN DERIV_cong]
```
```   215   from assms
```
```   216   have "\<exists>t>0. t < - h \<and>
```
```   217     f (- (- h)) =
```
```   218     (\<Sum>m<n.
```
```   219     (- 1) ^ m * diff m (- 0) / (fact m) * (- h) ^ m) +
```
```   220     (- 1) ^ n * diff n (- t) / (fact n) * (- h) ^ n"
```
```   221     by (intro Maclaurin) (auto intro!: derivative_eq_intros DERIV')
```
```   222   then guess t ..
```
```   223   moreover
```
```   224   have "(- 1) ^ n * diff n (- t) * (- h) ^ n / (fact n) = diff n (- t) * h ^ n / (fact n)"
```
```   225     by (auto simp add: power_mult_distrib[symmetric])
```
```   226   moreover
```
```   227   have "(SUM m<n. (- 1) ^ m * diff m 0 * (- h) ^ m / (fact m)) = (SUM m<n. diff m 0 * h ^ m / (fact m))"
```
```   228     by (auto intro: setsum.cong simp add: power_mult_distrib[symmetric])
```
```   229   ultimately have " h < - t \<and>
```
```   230     - t < 0 \<and>
```
```   231     f h =
```
```   232     (\<Sum>m<n. diff m 0 / (fact m) * h ^ m) + diff n (- t) / (fact n) * h ^ n"
```
```   233     by auto
```
```   234   thus ?thesis ..
```
```   235 qed
```
```   236
```
```   237 lemma Maclaurin_minus_objl:
```
```   238   fixes h::real
```
```   239   shows
```
```   240      "(h < 0 & n > 0 & diff 0 = f &
```
```   241        (\<forall>m t.
```
```   242           m < n & h \<le> t & t \<le> 0 --> DERIV (diff m) t :> diff (Suc m) t))
```
```   243     --> (\<exists>t. h < t &
```
```   244               t < 0 &
```
```   245               f h =
```
```   246               (\<Sum>m<n. diff m 0 / (fact m) * h ^ m) +
```
```   247               diff n t / (fact n) * h ^ n)"
```
```   248 by (blast intro: Maclaurin_minus)
```
```   249
```
```   250
```
```   251 subsection\<open>More Convenient "Bidirectional" Version.\<close>
```
```   252
```
```   253 (* not good for PVS sin_approx, cos_approx *)
```
```   254
```
```   255 lemma Maclaurin_bi_le_lemma:
```
```   256   "n>0 \<Longrightarrow>
```
```   257    diff 0 0 =
```
```   258    (\<Sum>m<n. diff m 0 * 0 ^ m / (fact m)) + diff n 0 * 0 ^ n / (fact n :: real)"
```
```   259 by (induct "n") auto
```
```   260
```
```   261 lemma Maclaurin_bi_le:
```
```   262    assumes "diff 0 = f"
```
```   263    and DERIV : "\<forall>m t::real. m < n & abs t \<le> abs x --> DERIV (diff m) t :> diff (Suc m) t"
```
```   264    shows "\<exists>t. abs t \<le> abs x &
```
```   265               f x =
```
```   266               (\<Sum>m<n. diff m 0 / (fact m) * x ^ m) +
```
```   267      diff n t / (fact n) * x ^ n" (is "\<exists>t. _ \<and> f x = ?f x t")
```
```   268 proof cases
```
```   269   assume "n = 0" with \<open>diff 0 = f\<close> show ?thesis by force
```
```   270 next
```
```   271   assume "n \<noteq> 0"
```
```   272   show ?thesis
```
```   273   proof (cases rule: linorder_cases)
```
```   274     assume "x = 0" with \<open>n \<noteq> 0\<close> \<open>diff 0 = f\<close> DERIV
```
```   275     have "\<bar>0\<bar> \<le> \<bar>x\<bar> \<and> f x = ?f x 0" by (auto simp add: Maclaurin_bi_le_lemma)
```
```   276     thus ?thesis ..
```
```   277   next
```
```   278     assume "x < 0"
```
```   279     with \<open>n \<noteq> 0\<close> DERIV
```
```   280     have "\<exists>t>x. t < 0 \<and> diff 0 x = ?f x t" by (intro Maclaurin_minus) auto
```
```   281     then guess t ..
```
```   282     with \<open>x < 0\<close> \<open>diff 0 = f\<close> have "\<bar>t\<bar> \<le> \<bar>x\<bar> \<and> f x = ?f x t" by simp
```
```   283     thus ?thesis ..
```
```   284   next
```
```   285     assume "x > 0"
```
```   286     with \<open>n \<noteq> 0\<close> \<open>diff 0 = f\<close> DERIV
```
```   287     have "\<exists>t>0. t < x \<and> diff 0 x = ?f x t" by (intro Maclaurin) auto
```
```   288     then guess t ..
```
```   289     with \<open>x > 0\<close> \<open>diff 0 = f\<close> have "\<bar>t\<bar> \<le> \<bar>x\<bar> \<and> f x = ?f x t" by simp
```
```   290     thus ?thesis ..
```
```   291   qed
```
```   292 qed
```
```   293
```
```   294 lemma Maclaurin_all_lt:
```
```   295   fixes x::real
```
```   296   assumes INIT1: "diff 0 = f" and INIT2: "0 < n" and INIT3: "x \<noteq> 0"
```
```   297   and DERIV: "\<forall>m x. DERIV (diff m) x :> diff(Suc m) x"
```
```   298   shows "\<exists>t. 0 < abs t & abs t < abs x & f x =
```
```   299     (\<Sum>m<n. (diff m 0 / (fact m)) * x ^ m) +
```
```   300                 (diff n t / (fact n)) * x ^ n" (is "\<exists>t. _ \<and> _ \<and> f x = ?f x t")
```
```   301 proof (cases rule: linorder_cases)
```
```   302   assume "x = 0" with INIT3 show "?thesis"..
```
```   303 next
```
```   304   assume "x < 0"
```
```   305   with assms have "\<exists>t>x. t < 0 \<and> f x = ?f x t" by (intro Maclaurin_minus) auto
```
```   306   then guess t ..
```
```   307   with \<open>x < 0\<close> have "0 < \<bar>t\<bar> \<and> \<bar>t\<bar> < \<bar>x\<bar> \<and> f x = ?f x t" by simp
```
```   308   thus ?thesis ..
```
```   309 next
```
```   310   assume "x > 0"
```
```   311   with assms have "\<exists>t>0. t < x \<and> f x = ?f x t " by (intro Maclaurin) auto
```
```   312   then guess t ..
```
```   313   with \<open>x > 0\<close> have "0 < \<bar>t\<bar> \<and> \<bar>t\<bar> < \<bar>x\<bar> \<and> f x = ?f x t" by simp
```
```   314   thus ?thesis ..
```
```   315 qed
```
```   316
```
```   317
```
```   318 lemma Maclaurin_all_lt_objl:
```
```   319   fixes x::real
```
```   320   shows
```
```   321      "diff 0 = f &
```
```   322       (\<forall>m x. DERIV (diff m) x :> diff(Suc m) x) &
```
```   323       x ~= 0 & n > 0
```
```   324       --> (\<exists>t. 0 < abs t & abs t < abs x &
```
```   325                f x = (\<Sum>m<n. (diff m 0 / (fact m)) * x ^ m) +
```
```   326                      (diff n t / (fact n)) * x ^ n)"
```
```   327 by (blast intro: Maclaurin_all_lt)
```
```   328
```
```   329 lemma Maclaurin_zero [rule_format]:
```
```   330      "x = (0::real)
```
```   331       ==> n \<noteq> 0 -->
```
```   332           (\<Sum>m<n. (diff m (0::real) / (fact m)) * x ^ m) =
```
```   333           diff 0 0"
```
```   334 by (induct n, auto)
```
```   335
```
```   336
```
```   337 lemma Maclaurin_all_le:
```
```   338   assumes INIT: "diff 0 = f"
```
```   339   and DERIV: "\<forall>m x::real. DERIV (diff m) x :> diff (Suc m) x"
```
```   340   shows "\<exists>t. abs t \<le> abs x & f x =
```
```   341     (\<Sum>m<n. (diff m 0 / (fact m)) * x ^ m) +
```
```   342     (diff n t / (fact n)) * x ^ n" (is "\<exists>t. _ \<and> f x = ?f x t")
```
```   343 proof cases
```
```   344   assume "n = 0" with INIT show ?thesis by force
```
```   345   next
```
```   346   assume "n \<noteq> 0"
```
```   347   show ?thesis
```
```   348   proof cases
```
```   349     assume "x = 0"
```
```   350     with \<open>n \<noteq> 0\<close> have "(\<Sum>m<n. diff m 0 / (fact m) * x ^ m) = diff 0 0"
```
```   351       by (intro Maclaurin_zero) auto
```
```   352     with INIT \<open>x = 0\<close> \<open>n \<noteq> 0\<close> have " \<bar>0\<bar> \<le> \<bar>x\<bar> \<and> f x = ?f x 0" by force
```
```   353     thus ?thesis ..
```
```   354   next
```
```   355     assume "x \<noteq> 0"
```
```   356     with INIT \<open>n \<noteq> 0\<close> DERIV have "\<exists>t. 0 < \<bar>t\<bar> \<and> \<bar>t\<bar> < \<bar>x\<bar> \<and> f x = ?f x t"
```
```   357       by (intro Maclaurin_all_lt) auto
```
```   358     then guess t ..
```
```   359     hence "\<bar>t\<bar> \<le> \<bar>x\<bar> \<and> f x = ?f x t" by simp
```
```   360     thus ?thesis ..
```
```   361   qed
```
```   362 qed
```
```   363
```
```   364 lemma Maclaurin_all_le_objl:
```
```   365   "diff 0 = f &
```
```   366       (\<forall>m x. DERIV (diff m) x :> diff (Suc m) x)
```
```   367       --> (\<exists>t::real. abs t \<le> abs x &
```
```   368               f x = (\<Sum>m<n. (diff m 0 / (fact m)) * x ^ m) +
```
```   369                     (diff n t / (fact n)) * x ^ n)"
```
```   370 by (blast intro: Maclaurin_all_le)
```
```   371
```
```   372
```
```   373 subsection\<open>Version for Exponential Function\<close>
```
```   374
```
```   375 lemma Maclaurin_exp_lt:
```
```   376   fixes x::real
```
```   377   shows
```
```   378   "[| x ~= 0; n > 0 |]
```
```   379       ==> (\<exists>t. 0 < abs t &
```
```   380                 abs t < abs x &
```
```   381                 exp x = (\<Sum>m<n. (x ^ m) / (fact m)) +
```
```   382                         (exp t / (fact n)) * x ^ n)"
```
```   383 by (cut_tac diff = "%n. exp" and f = exp and x = x and n = n in Maclaurin_all_lt_objl, auto)
```
```   384
```
```   385
```
```   386 lemma Maclaurin_exp_le:
```
```   387      "\<exists>t::real. abs t \<le> abs x &
```
```   388             exp x = (\<Sum>m<n. (x ^ m) / (fact m)) +
```
```   389                        (exp t / (fact n)) * x ^ n"
```
```   390 by (cut_tac diff = "%n. exp" and f = exp and x = x and n = n in Maclaurin_all_le_objl, auto)
```
```   391
```
```   392 lemma exp_lower_taylor_quadratic:
```
```   393   fixes x::real
```
```   394   shows "0 \<le> x \<Longrightarrow> 1 + x + x\<^sup>2 / 2 \<le> exp x"
```
```   395   using Maclaurin_exp_le [of x 3]
```
```   396   by (auto simp: numeral_3_eq_3 power2_eq_square power_Suc)
```
```   397
```
```   398
```
```   399 subsection\<open>Version for Sine Function\<close>
```
```   400
```
```   401 lemma mod_exhaust_less_4:
```
```   402   "m mod 4 = 0 | m mod 4 = 1 | m mod 4 = 2 | m mod 4 = (3::nat)"
```
```   403 by auto
```
```   404
```
```   405 lemma Suc_Suc_mult_two_diff_two [rule_format, simp]:
```
```   406   "n\<noteq>0 --> Suc (Suc (2 * n - 2)) = 2*n"
```
```   407 by (induct "n", auto)
```
```   408
```
```   409 lemma lemma_Suc_Suc_4n_diff_2 [rule_format, simp]:
```
```   410   "n\<noteq>0 --> Suc (Suc (4*n - 2)) = 4*n"
```
```   411 by (induct "n", auto)
```
```   412
```
```   413 lemma Suc_mult_two_diff_one [rule_format, simp]:
```
```   414   "n\<noteq>0 --> Suc (2 * n - 1) = 2*n"
```
```   415 by (induct "n", auto)
```
```   416
```
```   417
```
```   418 text\<open>It is unclear why so many variant results are needed.\<close>
```
```   419
```
```   420 lemma sin_expansion_lemma:
```
```   421      "sin (x + real (Suc m) * pi / 2) =
```
```   422       cos (x + real (m) * pi / 2)"
```
```   423 by (simp only: cos_add sin_add real_of_nat_Suc add_divide_distrib distrib_right, auto)
```
```   424
```
```   425 lemma Maclaurin_sin_expansion2:
```
```   426      "\<exists>t. abs t \<le> abs x &
```
```   427        sin x =
```
```   428        (\<Sum>m<n. sin_coeff m * x ^ m)
```
```   429       + ((sin(t + 1/2 * real (n) *pi) / (fact n)) * x ^ n)"
```
```   430 apply (cut_tac f = sin and n = n and x = x
```
```   431         and diff = "%n x. sin (x + 1/2*real n * pi)" in Maclaurin_all_lt_objl)
```
```   432 apply safe
```
```   433 apply (simp (no_asm))
```
```   434 apply (simp (no_asm) add: sin_expansion_lemma)
```
```   435 apply (force intro!: derivative_eq_intros)
```
```   436 apply (subst (asm) setsum.neutral, auto)
```
```   437 apply (rule ccontr, simp)
```
```   438 apply (drule_tac x = x in spec, simp)
```
```   439 apply (erule ssubst)
```
```   440 apply (rule_tac x = t in exI, simp)
```
```   441 apply (rule setsum.cong[OF refl])
```
```   442 apply (auto simp add: sin_coeff_def sin_zero_iff elim: oddE)
```
```   443 done
```
```   444
```
```   445 lemma Maclaurin_sin_expansion:
```
```   446      "\<exists>t. sin x =
```
```   447        (\<Sum>m<n. sin_coeff m * x ^ m)
```
```   448       + ((sin(t + 1/2 * real (n) *pi) / (fact n)) * x ^ n)"
```
```   449 apply (insert Maclaurin_sin_expansion2 [of x n])
```
```   450 apply (blast intro: elim:)
```
```   451 done
```
```   452
```
```   453 lemma Maclaurin_sin_expansion3:
```
```   454      "[| n > 0; 0 < x |] ==>
```
```   455        \<exists>t. 0 < t & t < x &
```
```   456        sin x =
```
```   457        (\<Sum>m<n. sin_coeff m * x ^ m)
```
```   458       + ((sin(t + 1/2 * real(n) *pi) / (fact n)) * x ^ n)"
```
```   459 apply (cut_tac f = sin and n = n and h = x and diff = "%n x. sin (x + 1/2*real (n) *pi)" in Maclaurin_objl)
```
```   460 apply safe
```
```   461 apply simp
```
```   462 apply (simp (no_asm) add: sin_expansion_lemma)
```
```   463 apply (force intro!: derivative_eq_intros)
```
```   464 apply (erule ssubst)
```
```   465 apply (rule_tac x = t in exI, simp)
```
```   466 apply (rule setsum.cong[OF refl])
```
```   467 apply (auto simp add: sin_coeff_def sin_zero_iff elim: oddE)
```
```   468 done
```
```   469
```
```   470 lemma Maclaurin_sin_expansion4:
```
```   471      "0 < x ==>
```
```   472        \<exists>t. 0 < t & t \<le> x &
```
```   473        sin x =
```
```   474        (\<Sum>m<n. sin_coeff m * x ^ m)
```
```   475       + ((sin(t + 1/2 * real (n) *pi) / (fact n)) * x ^ n)"
```
```   476 apply (cut_tac f = sin and n = n and h = x and diff = "%n x. sin (x + 1/2*real (n) *pi)" in Maclaurin2_objl)
```
```   477 apply safe
```
```   478 apply simp
```
```   479 apply (simp (no_asm) add: sin_expansion_lemma)
```
```   480 apply (force intro!: derivative_eq_intros)
```
```   481 apply (erule ssubst)
```
```   482 apply (rule_tac x = t in exI, simp)
```
```   483 apply (rule setsum.cong[OF refl])
```
```   484 apply (auto simp add: sin_coeff_def sin_zero_iff elim: oddE)
```
```   485 done
```
```   486
```
```   487
```
```   488 subsection\<open>Maclaurin Expansion for Cosine Function\<close>
```
```   489
```
```   490 lemma sumr_cos_zero_one [simp]:
```
```   491   "(\<Sum>m<(Suc n). cos_coeff m * 0 ^ m) = 1"
```
```   492 by (induct "n", auto)
```
```   493
```
```   494 lemma cos_expansion_lemma:
```
```   495   "cos (x + real(Suc m) * pi / 2) = -sin (x + real m * pi / 2)"
```
```   496 by (simp only: cos_add sin_add real_of_nat_Suc distrib_right add_divide_distrib, auto)
```
```   497
```
```   498 lemma Maclaurin_cos_expansion:
```
```   499      "\<exists>t::real. abs t \<le> abs x &
```
```   500        cos x =
```
```   501        (\<Sum>m<n. cos_coeff m * x ^ m)
```
```   502       + ((cos(t + 1/2 * real (n) *pi) / (fact n)) * x ^ n)"
```
```   503 apply (cut_tac f = cos and n = n and x = x and diff = "%n x. cos (x + 1/2*real (n) *pi)" in Maclaurin_all_lt_objl)
```
```   504 apply safe
```
```   505 apply (simp (no_asm))
```
```   506 apply (simp (no_asm) add: cos_expansion_lemma)
```
```   507 apply (case_tac "n", simp)
```
```   508 apply (simp del: setsum_lessThan_Suc)
```
```   509 apply (rule ccontr, simp)
```
```   510 apply (drule_tac x = x in spec, simp)
```
```   511 apply (erule ssubst)
```
```   512 apply (rule_tac x = t in exI, simp)
```
```   513 apply (rule setsum.cong[OF refl])
```
```   514 apply (auto simp add: cos_coeff_def cos_zero_iff elim: evenE)
```
```   515 done
```
```   516
```
```   517 lemma Maclaurin_cos_expansion2:
```
```   518      "[| 0 < x; n > 0 |] ==>
```
```   519        \<exists>t. 0 < t & t < x &
```
```   520        cos x =
```
```   521        (\<Sum>m<n. cos_coeff m * x ^ m)
```
```   522       + ((cos(t + 1/2 * real (n) *pi) / (fact n)) * x ^ n)"
```
```   523 apply (cut_tac f = cos and n = n and h = x and diff = "%n x. cos (x + 1/2*real (n) *pi)" in Maclaurin_objl)
```
```   524 apply safe
```
```   525 apply simp
```
```   526 apply (simp (no_asm) add: cos_expansion_lemma)
```
```   527 apply (erule ssubst)
```
```   528 apply (rule_tac x = t in exI, simp)
```
```   529 apply (rule setsum.cong[OF refl])
```
```   530 apply (auto simp add: cos_coeff_def cos_zero_iff elim: evenE)
```
```   531 done
```
```   532
```
```   533 lemma Maclaurin_minus_cos_expansion:
```
```   534      "[| x < 0; n > 0 |] ==>
```
```   535        \<exists>t. x < t & t < 0 &
```
```   536        cos x =
```
```   537        (\<Sum>m<n. cos_coeff m * x ^ m)
```
```   538       + ((cos(t + 1/2 * real (n) *pi) / (fact n)) * x ^ n)"
```
```   539 apply (cut_tac f = cos and n = n and h = x and diff = "%n x. cos (x + 1/2*real (n) *pi)" in Maclaurin_minus_objl)
```
```   540 apply safe
```
```   541 apply simp
```
```   542 apply (simp (no_asm) add: cos_expansion_lemma)
```
```   543 apply (erule ssubst)
```
```   544 apply (rule_tac x = t in exI, simp)
```
```   545 apply (rule setsum.cong[OF refl])
```
```   546 apply (auto simp add: cos_coeff_def cos_zero_iff elim: evenE)
```
```   547 done
```
```   548
```
```   549 (* ------------------------------------------------------------------------- *)
```
```   550 (* Version for ln(1 +/- x). Where is it??                                    *)
```
```   551 (* ------------------------------------------------------------------------- *)
```
```   552
```
```   553 lemma sin_bound_lemma:
```
```   554     "[|x = y; abs u \<le> (v::real) |] ==> \<bar>(x + u) - y\<bar> \<le> v"
```
```   555 by auto
```
```   556
```
```   557 lemma Maclaurin_sin_bound:
```
```   558   "abs(sin x - (\<Sum>m<n. sin_coeff m * x ^ m))
```
```   559   \<le> inverse((fact n)) * \<bar>x\<bar> ^ n"
```
```   560 proof -
```
```   561   have "!! x (y::real). x \<le> 1 \<Longrightarrow> 0 \<le> y \<Longrightarrow> x * y \<le> 1 * y"
```
```   562     by (rule_tac mult_right_mono,simp_all)
```
```   563   note est = this[simplified]
```
```   564   let ?diff = "\<lambda>(n::nat) x. if n mod 4 = 0 then sin(x) else if n mod 4 = 1 then cos(x) else if n mod 4 = 2 then -sin(x) else -cos(x)"
```
```   565   have diff_0: "?diff 0 = sin" by simp
```
```   566   have DERIV_diff: "\<forall>m x. DERIV (?diff m) x :> ?diff (Suc m) x"
```
```   567     apply (clarify)
```
```   568     apply (subst (1 2 3) mod_Suc_eq_Suc_mod)
```
```   569     apply (cut_tac m=m in mod_exhaust_less_4)
```
```   570     apply (safe, auto intro!: derivative_eq_intros)
```
```   571     done
```
```   572   from Maclaurin_all_le [OF diff_0 DERIV_diff]
```
```   573   obtain t where t1: "\<bar>t\<bar> \<le> \<bar>x\<bar>" and
```
```   574     t2: "sin x = (\<Sum>m<n. ?diff m 0 / (fact m) * x ^ m) +
```
```   575       ?diff n t / (fact n) * x ^ n" by fast
```
```   576   have diff_m_0:
```
```   577     "\<And>m. ?diff m 0 = (if even m then 0
```
```   578          else (- 1) ^ ((m - Suc 0) div 2))"
```
```   579     apply (subst even_even_mod_4_iff)
```
```   580     apply (cut_tac m=m in mod_exhaust_less_4)
```
```   581     apply (elim disjE, simp_all)
```
```   582     apply (safe dest!: mod_eqD, simp_all)
```
```   583     done
```
```   584   show ?thesis
```
```   585     unfolding sin_coeff_def
```
```   586     apply (subst t2)
```
```   587     apply (rule sin_bound_lemma)
```
```   588     apply (rule setsum.cong[OF refl])
```
```   589     apply (subst diff_m_0, simp)
```
```   590     apply (auto intro: mult_right_mono [where b=1, simplified] mult_right_mono
```
```   591                 simp add: est ac_simps divide_inverse power_abs [symmetric] abs_mult)
```
```   592     done
```
```   593 qed
```
```   594
```
```   595 end
```