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