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