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