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