src/HOL/MacLaurin.thy
author huffman
Sat Jun 06 09:11:12 2009 -0700 (2009-06-06)
changeset 31488 5691ccb8d6b5
parent 31148 7ba7c1f8bc22
child 31881 eba74a5790d2
permissions -rw-r--r--
generalize tendsto to class topological_space
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@29811
     9
imports 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:
huffman@29187
    61
  assumes diff: "\<forall>m t. m < n \<and> 0\<le>t \<and> t\<le>h \<longrightarrow> DERIV (diff m) t :> diff (Suc m) t"
huffman@29187
    62
  assumes n: "n = Suc k"
huffman@29187
    63
  assumes difg: "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) +
huffman@29187
    66
                B * (t ^ (n - m) / real (fact (n - m)))))"
huffman@29187
    67
  shows
huffman@29187
    68
      "\<forall>m t. m < n & 0 \<le> t & t \<le> h --> DERIV (difg m) t :> difg (Suc m) t"
huffman@29187
    69
unfolding difg
huffman@29187
    70
 apply clarify
huffman@29187
    71
 apply (rule DERIV_diff)
huffman@29187
    72
  apply (simp add: diff)
huffman@29187
    73
 apply (simp only: n)
huffman@29187
    74
 apply (rule DERIV_add)
huffman@29187
    75
  apply (rule_tac [2] DERIV_cmult)
huffman@29187
    76
  apply (rule_tac [2] lemma_DERIV_subst)
huffman@29187
    77
   apply (rule_tac [2] DERIV_quotient)
huffman@29187
    78
     apply (rule_tac [3] DERIV_const)
huffman@29187
    79
    apply (rule_tac [2] DERIV_pow)
huffman@29187
    80
   prefer 3 apply (simp add: fact_diff_Suc)
huffman@29187
    81
  prefer 2 apply simp
huffman@29187
    82
 apply (frule less_iff_Suc_add [THEN iffD1], clarify)
huffman@29187
    83
 apply (simp del: setsum_op_ivl_Suc)
huffman@30082
    84
 apply (insert sumr_offset4 [of "Suc 0"])
huffman@30273
    85
 apply (simp del: setsum_op_ivl_Suc fact_Suc power_Suc)
huffman@29187
    86
 apply (rule lemma_DERIV_subst)
huffman@29187
    87
  apply (rule DERIV_add)
huffman@29187
    88
   apply (rule_tac [2] DERIV_const)
huffman@29187
    89
  apply (rule DERIV_sumr, clarify)
huffman@29187
    90
  prefer 2 apply simp
huffman@30273
    91
 apply (simp (no_asm) add: divide_inverse mult_assoc del: fact_Suc power_Suc)
huffman@29187
    92
 apply (rule DERIV_cmult)
huffman@29187
    93
 apply (rule lemma_DERIV_subst)
huffman@29187
    94
  apply (best intro: DERIV_chain2 intro!: DERIV_intros)
huffman@29187
    95
 apply (subst fact_Suc)
huffman@29187
    96
 apply (subst real_of_nat_mult)
huffman@29187
    97
 apply (simp add: mult_ac)
paulson@15079
    98
done
paulson@15079
    99
paulson@15079
   100
paulson@15079
   101
lemma Maclaurin:
huffman@29187
   102
  assumes h: "0 < h"
huffman@29187
   103
  assumes n: "0 < n"
huffman@29187
   104
  assumes diff_0: "diff 0 = f"
huffman@29187
   105
  assumes diff_Suc:
huffman@29187
   106
    "\<forall>m t. m < n & 0 \<le> t & t \<le> h --> DERIV (diff m) t :> diff (Suc m) t"
huffman@29187
   107
  shows
huffman@29187
   108
    "\<exists>t. 0 < t & t < h &
paulson@15079
   109
              f h =
nipkow@15539
   110
              setsum (%m. (diff m 0 / real (fact m)) * h ^ m) {0..<n} +
paulson@15079
   111
              (diff n t / real (fact n)) * h ^ n"
huffman@29187
   112
proof -
huffman@29187
   113
  from n obtain m where m: "n = Suc m"
huffman@29187
   114
    by (cases n, simp add: n)
huffman@29187
   115
huffman@29187
   116
  obtain B where f_h: "f h =
huffman@29187
   117
        (\<Sum>m = 0..<n. diff m (0\<Colon>real) / real (fact m) * h ^ m) +
huffman@29187
   118
        B * (h ^ n / real (fact n))"
huffman@29187
   119
    using Maclaurin_lemma [OF h] ..
huffman@29187
   120
huffman@29187
   121
  obtain g where g_def: "g = (%t. f t -
huffman@29187
   122
    (setsum (%m. (diff m 0 / real(fact m)) * t^m) {0..<n}
huffman@29187
   123
      + (B * (t^n / real(fact n)))))" by blast
huffman@29187
   124
huffman@29187
   125
  have g2: "g 0 = 0 & g h = 0"
huffman@29187
   126
    apply (simp add: m f_h g_def del: setsum_op_ivl_Suc)
huffman@30082
   127
    apply (cut_tac n = m and k = "Suc 0" in sumr_offset2)
huffman@29187
   128
    apply (simp add: eq_diff_eq' diff_0 del: setsum_op_ivl_Suc)
huffman@29187
   129
    done
huffman@29187
   130
huffman@29187
   131
  obtain difg where difg_def: "difg = (%m t. diff m t -
huffman@29187
   132
    (setsum (%p. (diff (m + p) 0 / real (fact p)) * (t ^ p)) {0..<n-m}
huffman@29187
   133
      + (B * ((t ^ (n - m)) / real (fact (n - m))))))" by blast
huffman@29187
   134
huffman@29187
   135
  have difg_0: "difg 0 = g"
huffman@29187
   136
    unfolding difg_def g_def by (simp add: diff_0)
huffman@29187
   137
huffman@29187
   138
  have difg_Suc: "\<forall>(m\<Colon>nat) t\<Colon>real.
huffman@29187
   139
        m < n \<and> (0\<Colon>real) \<le> t \<and> t \<le> h \<longrightarrow> DERIV (difg m) t :> difg (Suc m) t"
huffman@29187
   140
    using diff_Suc m difg_def by (rule Maclaurin_lemma2)
huffman@29187
   141
huffman@29187
   142
  have difg_eq_0: "\<forall>m. m < n --> difg m 0 = 0"
huffman@29187
   143
    apply clarify
huffman@29187
   144
    apply (simp add: m difg_def)
huffman@29187
   145
    apply (frule less_iff_Suc_add [THEN iffD1], clarify)
huffman@29187
   146
    apply (simp del: setsum_op_ivl_Suc)
huffman@30082
   147
    apply (insert sumr_offset4 [of "Suc 0"])
huffman@30273
   148
    apply (simp del: setsum_op_ivl_Suc fact_Suc)
huffman@29187
   149
    done
huffman@29187
   150
huffman@29187
   151
  have isCont_difg: "\<And>m x. \<lbrakk>m < n; 0 \<le> x; x \<le> h\<rbrakk> \<Longrightarrow> isCont (difg m) x"
huffman@29187
   152
    by (rule DERIV_isCont [OF difg_Suc [rule_format]]) simp
huffman@29187
   153
huffman@29187
   154
  have differentiable_difg:
huffman@29187
   155
    "\<And>m x. \<lbrakk>m < n; 0 \<le> x; x \<le> h\<rbrakk> \<Longrightarrow> difg m differentiable x"
huffman@29187
   156
    by (rule differentiableI [OF difg_Suc [rule_format]]) simp
huffman@29187
   157
huffman@29187
   158
  have difg_Suc_eq_0: "\<And>m t. \<lbrakk>m < n; 0 \<le> t; t \<le> h; DERIV (difg m) t :> 0\<rbrakk>
huffman@29187
   159
        \<Longrightarrow> difg (Suc m) t = 0"
huffman@29187
   160
    by (rule DERIV_unique [OF difg_Suc [rule_format]]) simp
huffman@29187
   161
huffman@29187
   162
  have "m < n" using m by simp
huffman@29187
   163
huffman@29187
   164
  have "\<exists>t. 0 < t \<and> t < h \<and> DERIV (difg m) t :> 0"
huffman@29187
   165
  using `m < n`
huffman@29187
   166
  proof (induct m)
huffman@29187
   167
  case 0
huffman@29187
   168
    show ?case
huffman@29187
   169
    proof (rule Rolle)
huffman@29187
   170
      show "0 < h" by fact
huffman@29187
   171
      show "difg 0 0 = difg 0 h" by (simp add: difg_0 g2)
huffman@29187
   172
      show "\<forall>x. 0 \<le> x \<and> x \<le> h \<longrightarrow> isCont (difg (0\<Colon>nat)) x"
huffman@29187
   173
        by (simp add: isCont_difg n)
huffman@29187
   174
      show "\<forall>x. 0 < x \<and> x < h \<longrightarrow> difg (0\<Colon>nat) differentiable x"
huffman@29187
   175
        by (simp add: differentiable_difg n)
huffman@29187
   176
    qed
huffman@29187
   177
  next
huffman@29187
   178
  case (Suc m')
huffman@29187
   179
    hence "\<exists>t. 0 < t \<and> t < h \<and> DERIV (difg m') t :> 0" by simp
huffman@29187
   180
    then obtain t where t: "0 < t" "t < h" "DERIV (difg m') t :> 0" by fast
huffman@29187
   181
    have "\<exists>t'. 0 < t' \<and> t' < t \<and> DERIV (difg (Suc m')) t' :> 0"
huffman@29187
   182
    proof (rule Rolle)
huffman@29187
   183
      show "0 < t" by fact
huffman@29187
   184
      show "difg (Suc m') 0 = difg (Suc m') t"
huffman@29187
   185
        using t `Suc m' < n` by (simp add: difg_Suc_eq_0 difg_eq_0)
huffman@29187
   186
      show "\<forall>x. 0 \<le> x \<and> x \<le> t \<longrightarrow> isCont (difg (Suc m')) x"
huffman@29187
   187
        using `t < h` `Suc m' < n` by (simp add: isCont_difg)
huffman@29187
   188
      show "\<forall>x. 0 < x \<and> x < t \<longrightarrow> difg (Suc m') differentiable x"
huffman@29187
   189
        using `t < h` `Suc m' < n` by (simp add: differentiable_difg)
huffman@29187
   190
    qed
huffman@29187
   191
    thus ?case
huffman@29187
   192
      using `t < h` by auto
huffman@29187
   193
  qed
huffman@29187
   194
huffman@29187
   195
  then obtain t where "0 < t" "t < h" "DERIV (difg m) t :> 0" by fast
huffman@29187
   196
huffman@29187
   197
  hence "difg (Suc m) t = 0"
huffman@29187
   198
    using `m < n` by (simp add: difg_Suc_eq_0)
huffman@29187
   199
huffman@29187
   200
  show ?thesis
huffman@29187
   201
  proof (intro exI conjI)
huffman@29187
   202
    show "0 < t" by fact
huffman@29187
   203
    show "t < h" by fact
huffman@29187
   204
    show "f h =
huffman@29187
   205
      (\<Sum>m = 0..<n. diff m 0 / real (fact m) * h ^ m) +
huffman@29187
   206
      diff n t / real (fact n) * h ^ n"
huffman@29187
   207
      using `difg (Suc m) t = 0`
huffman@30273
   208
      by (simp add: m f_h difg_def del: fact_Suc)
huffman@29187
   209
  qed
huffman@29187
   210
huffman@29187
   211
qed
paulson@15079
   212
paulson@15079
   213
lemma Maclaurin_objl:
nipkow@25162
   214
  "0 < h & n>0 & diff 0 = f &
nipkow@25134
   215
  (\<forall>m t. m < n & 0 \<le> t & t \<le> h --> DERIV (diff m) t :> diff (Suc m) t)
nipkow@25134
   216
   --> (\<exists>t. 0 < t & t < h &
nipkow@25134
   217
            f h = (\<Sum>m=0..<n. diff m 0 / real (fact m) * h ^ m) +
nipkow@25134
   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 =
nipkow@15539
   229
              (\<Sum>m=0..<n. 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 =
nipkow@15539
   242
              (\<Sum>m=0..<n. 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:
nipkow@25162
   247
   "[| h < 0; n > 0; 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 =
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
apply (cut_tac f = "%x. f (-x)"
huffman@23177
   255
        and diff = "%n x. (-1 ^ n) * diff n (-x)"
paulson@15079
   256
        and h = "-h" and n = n in Maclaurin_objl)
nipkow@15539
   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])
huffman@23069
   263
apply (rule_tac [2] DERIV_minus, rule_tac [2] DERIV_ident)
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))")
nipkow@15536
   269
apply (rule_tac [2] setsum_cong[OF refl])
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:
nipkow@25162
   274
     "(h < 0 & n > 0 & 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 =
nipkow@15539
   280
              (\<Sum>m=0..<n. 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]:
nipkow@25162
   290
  "n>0 \<longrightarrow>
nipkow@25134
   291
   diff 0 0 =
nipkow@25134
   292
   (\<Sum>m = 0..<n. diff m 0 * 0 ^ m / real (fact m)) +
nipkow@25134
   293
   diff n 0 * 0 ^ n / real (fact n)"
paulson@15251
   294
by (induct "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 =
nipkow@15539
   301
              (\<Sum>m=0..<n. 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")
nipkow@25134
   305
 apply (rule_tac x = 0 in exI)
nipkow@25134
   306
 apply (force simp add: Maclaurin_bi_le_lemma)
nipkow@25134
   307
apply (cut_tac x = x and y = 0 in linorder_less_linear, auto)
nipkow@25134
   308
 txt{*Case 1, where @{term "x < 0"}*}
nipkow@25134
   309
 apply (cut_tac f = "diff 0" and diff = diff and h = x and n = n in Maclaurin_minus_objl, safe)
nipkow@25134
   310
  apply (simp add: abs_if)
nipkow@25134
   311
 apply (rule_tac x = t in exI)
nipkow@25134
   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)
nipkow@25134
   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;
nipkow@25162
   323
        x ~= 0; n > 0
paulson@15079
   324
      |] ==> \<exists>t. 0 < abs t & abs t < 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"
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) &
nipkow@25162
   337
      x ~= 0 & n > 0
paulson@15079
   338
      --> (\<exists>t. 0 < abs t & abs t < abs x &
nipkow@15539
   339
               f x = (\<Sum>m=0..<n. (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)
nipkow@25134
   345
      ==> n \<noteq> 0 -->
nipkow@15539
   346
          (\<Sum>m=0..<n. (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 &
nipkow@15539
   353
              f x = (\<Sum>m=0..<n. (diff m 0 / real (fact m)) * x ^ m) +
paulson@15079
   354
                    (diff n t / real (fact n)) * x ^ n"
nipkow@25134
   355
apply(cases "n=0")
nipkow@25134
   356
apply (force)
paulson@15079
   357
apply (case_tac "x = 0")
paulson@15079
   358
apply (frule_tac diff = diff and n = n in Maclaurin_zero, assumption)
nipkow@25134
   359
apply (drule 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 &
nipkow@15539
   368
              f x = (\<Sum>m=0..<n. (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
nipkow@25162
   375
lemma Maclaurin_exp_lt: "[| x ~= 0; n > 0 |]
paulson@15079
   376
      ==> (\<exists>t. 0 < abs t &
paulson@15079
   377
                abs t < abs x &
nipkow@15539
   378
                exp x = (\<Sum>m=0..<n. (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 &
nipkow@15539
   385
            exp x = (\<Sum>m=0..<n. (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 mod_exhaust_less_4:
nipkow@25134
   393
  "m mod 4 = 0 | m mod 4 = 1 | m mod 4 = 2 | m mod 4 = (3::nat)"
webertj@20217
   394
by auto
paulson@15079
   395
paulson@15079
   396
lemma Suc_Suc_mult_two_diff_two [rule_format, simp]:
nipkow@25134
   397
  "n\<noteq>0 --> Suc (Suc (2 * n - 2)) = 2*n"
paulson@15251
   398
by (induct "n", auto)
paulson@15079
   399
paulson@15079
   400
lemma lemma_Suc_Suc_4n_diff_2 [rule_format, simp]:
nipkow@25134
   401
  "n\<noteq>0 --> Suc (Suc (4*n - 2)) = 4*n"
paulson@15251
   402
by (induct "n", auto)
paulson@15079
   403
paulson@15079
   404
lemma Suc_mult_two_diff_one [rule_format, simp]:
nipkow@25134
   405
  "n\<noteq>0 --> Suc (2 * n - 1) = 2*n"
paulson@15251
   406
by (induct "n", auto)
paulson@15079
   407
paulson@15234
   408
paulson@15234
   409
text{*It is unclear why so many variant results are needed.*}
paulson@15079
   410
paulson@15079
   411
lemma Maclaurin_sin_expansion2:
paulson@15079
   412
     "\<exists>t. abs t \<le> abs x &
paulson@15079
   413
       sin x =
nipkow@15539
   414
       (\<Sum>m=0..<n. (if even m then 0
huffman@23177
   415
                       else (-1 ^ ((m - Suc 0) div 2)) / real (fact m)) *
nipkow@15539
   416
                       x ^ m)
paulson@15079
   417
      + ((sin(t + 1/2 * real (n) *pi) / real (fact n)) * x ^ n)"
paulson@15079
   418
apply (cut_tac f = sin and n = n and x = x
paulson@15079
   419
        and diff = "%n x. sin (x + 1/2*real n * pi)" in Maclaurin_all_lt_objl)
paulson@15079
   420
apply safe
paulson@15079
   421
apply (simp (no_asm))
nipkow@15539
   422
apply (simp (no_asm))
huffman@23242
   423
apply (case_tac "n", clarify, simp, simp add: lemma_STAR_sin)
paulson@15079
   424
apply (rule ccontr, simp)
paulson@15079
   425
apply (drule_tac x = x in spec, simp)
paulson@15079
   426
apply (erule ssubst)
paulson@15079
   427
apply (rule_tac x = t in exI, simp)
nipkow@15536
   428
apply (rule setsum_cong[OF refl])
nipkow@15539
   429
apply (auto simp add: sin_zero_iff odd_Suc_mult_two_ex)
paulson@15079
   430
done
paulson@15079
   431
paulson@15234
   432
lemma Maclaurin_sin_expansion:
paulson@15234
   433
     "\<exists>t. sin x =
nipkow@15539
   434
       (\<Sum>m=0..<n. (if even m then 0
huffman@23177
   435
                       else (-1 ^ ((m - Suc 0) div 2)) / real (fact m)) *
nipkow@15539
   436
                       x ^ m)
paulson@15234
   437
      + ((sin(t + 1/2 * real (n) *pi) / real (fact n)) * x ^ n)"
paulson@15234
   438
apply (insert Maclaurin_sin_expansion2 [of x n]) 
paulson@15234
   439
apply (blast intro: elim:); 
paulson@15234
   440
done
paulson@15234
   441
paulson@15234
   442
paulson@15079
   443
lemma Maclaurin_sin_expansion3:
nipkow@25162
   444
     "[| n > 0; 0 < x |] ==>
paulson@15079
   445
       \<exists>t. 0 < t & t < x &
paulson@15079
   446
       sin x =
nipkow@15539
   447
       (\<Sum>m=0..<n. (if even m then 0
huffman@23177
   448
                       else (-1 ^ ((m - Suc 0) div 2)) / real (fact m)) *
nipkow@15539
   449
                       x ^ m)
paulson@15079
   450
      + ((sin(t + 1/2 * real(n) *pi) / real (fact n)) * x ^ n)"
paulson@15079
   451
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
   452
apply safe
paulson@15079
   453
apply simp
nipkow@15539
   454
apply (simp (no_asm))
paulson@15079
   455
apply (erule ssubst)
paulson@15079
   456
apply (rule_tac x = t in exI, simp)
nipkow@15536
   457
apply (rule setsum_cong[OF refl])
nipkow@15539
   458
apply (auto simp add: sin_zero_iff odd_Suc_mult_two_ex)
paulson@15079
   459
done
paulson@15079
   460
paulson@15079
   461
lemma Maclaurin_sin_expansion4:
paulson@15079
   462
     "0 < x ==>
paulson@15079
   463
       \<exists>t. 0 < t & t \<le> x &
paulson@15079
   464
       sin x =
nipkow@15539
   465
       (\<Sum>m=0..<n. (if even m then 0
huffman@23177
   466
                       else (-1 ^ ((m - Suc 0) div 2)) / real (fact m)) *
nipkow@15539
   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 Maclaurin2_objl)
paulson@15079
   470
apply safe
paulson@15079
   471
apply simp
nipkow@15539
   472
apply (simp (no_asm))
paulson@15079
   473
apply (erule ssubst)
paulson@15079
   474
apply (rule_tac x = t in exI, simp)
nipkow@15536
   475
apply (rule setsum_cong[OF refl])
nipkow@15539
   476
apply (auto simp add: sin_zero_iff odd_Suc_mult_two_ex)
paulson@15079
   477
done
paulson@15079
   478
paulson@15079
   479
paulson@15079
   480
subsection{*Maclaurin Expansion for Cosine Function*}
paulson@15079
   481
paulson@15079
   482
lemma sumr_cos_zero_one [simp]:
nipkow@15539
   483
 "(\<Sum>m=0..<(Suc n).
huffman@23177
   484
     (if even m then -1 ^ (m div 2)/(real  (fact m)) else 0) * 0 ^ m) = 1"
paulson@15251
   485
by (induct "n", auto)
paulson@15079
   486
paulson@15079
   487
lemma Maclaurin_cos_expansion:
paulson@15079
   488
     "\<exists>t. abs t \<le> abs x &
paulson@15079
   489
       cos x =
nipkow@15539
   490
       (\<Sum>m=0..<n. (if even m
huffman@23177
   491
                       then -1 ^ (m div 2)/(real (fact m))
paulson@15079
   492
                       else 0) *
nipkow@15539
   493
                       x ^ m)
paulson@15079
   494
      + ((cos(t + 1/2 * real (n) *pi) / real (fact n)) * x ^ n)"
paulson@15079
   495
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
   496
apply safe
paulson@15079
   497
apply (simp (no_asm))
nipkow@15539
   498
apply (simp (no_asm))
paulson@15079
   499
apply (case_tac "n", simp)
nipkow@15561
   500
apply (simp del: setsum_op_ivl_Suc)
paulson@15079
   501
apply (rule ccontr, simp)
paulson@15079
   502
apply (drule_tac x = x in spec, simp)
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@15079
   509
lemma Maclaurin_cos_expansion2:
nipkow@25162
   510
     "[| 0 < x; n > 0 |] ==>
paulson@15079
   511
       \<exists>t. 0 < t & t < x &
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_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@15234
   528
lemma Maclaurin_minus_cos_expansion:
nipkow@25162
   529
     "[| x < 0; n > 0 |] ==>
paulson@15079
   530
       \<exists>t. x < t & t < 0 &
paulson@15079
   531
       cos x =
nipkow@15539
   532
       (\<Sum>m=0..<n. (if even m
huffman@23177
   533
                       then -1 ^ (m div 2)/(real (fact m))
paulson@15079
   534
                       else 0) *
nipkow@15539
   535
                       x ^ m)
paulson@15079
   536
      + ((cos(t + 1/2 * real (n) *pi) / real (fact n)) * x ^ n)"
paulson@15079
   537
apply (cut_tac f = cos and n = n and h = x and diff = "%n x. cos (x + 1/2*real (n) *pi)" in Maclaurin_minus_objl)
paulson@15079
   538
apply safe
paulson@15079
   539
apply simp
nipkow@15539
   540
apply (simp (no_asm))
paulson@15079
   541
apply (erule ssubst)
paulson@15079
   542
apply (rule_tac x = t in exI, simp)
nipkow@15536
   543
apply (rule setsum_cong[OF refl])
paulson@15234
   544
apply (auto simp add: cos_zero_iff even_mult_two_ex)
paulson@15079
   545
done
paulson@15079
   546
paulson@15079
   547
(* ------------------------------------------------------------------------- *)
paulson@15079
   548
(* Version for ln(1 +/- x). Where is it??                                    *)
paulson@15079
   549
(* ------------------------------------------------------------------------- *)
paulson@15079
   550
paulson@15079
   551
lemma sin_bound_lemma:
paulson@15081
   552
    "[|x = y; abs u \<le> (v::real) |] ==> \<bar>(x + u) - y\<bar> \<le> v"
paulson@15079
   553
by auto
paulson@15079
   554
paulson@15079
   555
lemma Maclaurin_sin_bound:
huffman@23177
   556
  "abs(sin x - (\<Sum>m=0..<n. (if even m then 0 else (-1 ^ ((m - Suc 0) div 2)) / real (fact m)) *
paulson@15081
   557
  x ^ m))  \<le> inverse(real (fact n)) * \<bar>x\<bar> ^ n"
obua@14738
   558
proof -
paulson@15079
   559
  have "!! x (y::real). x \<le> 1 \<Longrightarrow> 0 \<le> y \<Longrightarrow> x * y \<le> 1 * y"
obua@14738
   560
    by (rule_tac mult_right_mono,simp_all)
obua@14738
   561
  note est = this[simplified]
huffman@22985
   562
  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
   563
  have diff_0: "?diff 0 = sin" by simp
huffman@22985
   564
  have DERIV_diff: "\<forall>m x. DERIV (?diff m) x :> ?diff (Suc m) x"
huffman@22985
   565
    apply (clarify)
huffman@22985
   566
    apply (subst (1 2 3) mod_Suc_eq_Suc_mod)
huffman@22985
   567
    apply (cut_tac m=m in mod_exhaust_less_4)
huffman@22985
   568
    apply (safe, simp_all)
huffman@22985
   569
    apply (rule DERIV_minus, simp)
huffman@22985
   570
    apply (rule lemma_DERIV_subst, rule DERIV_minus, rule DERIV_cos, simp)
huffman@22985
   571
    done
huffman@22985
   572
  from Maclaurin_all_le [OF diff_0 DERIV_diff]
huffman@22985
   573
  obtain t where t1: "\<bar>t\<bar> \<le> \<bar>x\<bar>" and
huffman@22985
   574
    t2: "sin x = (\<Sum>m = 0..<n. ?diff m 0 / real (fact m) * x ^ m) +
huffman@22985
   575
      ?diff n t / real (fact n) * x ^ n" by fast
huffman@22985
   576
  have diff_m_0:
huffman@22985
   577
    "\<And>m. ?diff m 0 = (if even m then 0
huffman@23177
   578
         else -1 ^ ((m - Suc 0) div 2))"
huffman@22985
   579
    apply (subst even_even_mod_4_iff)
huffman@22985
   580
    apply (cut_tac m=m in mod_exhaust_less_4)
huffman@22985
   581
    apply (elim disjE, simp_all)
huffman@22985
   582
    apply (safe dest!: mod_eqD, simp_all)
huffman@22985
   583
    done
obua@14738
   584
  show ?thesis
huffman@22985
   585
    apply (subst t2)
paulson@15079
   586
    apply (rule sin_bound_lemma)
nipkow@15536
   587
    apply (rule setsum_cong[OF refl])
huffman@22985
   588
    apply (subst diff_m_0, simp)
paulson@15079
   589
    apply (auto intro: mult_right_mono [where b=1, simplified] mult_right_mono
avigad@16775
   590
                   simp add: est mult_nonneg_nonneg mult_ac divide_inverse
paulson@16924
   591
                          power_abs [symmetric] abs_mult)
obua@14738
   592
    done
obua@14738
   593
qed
obua@14738
   594
paulson@15079
   595
end