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