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