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