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