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