src/HOL/MacLaurin.thy
author hoelzl
Tue Jun 30 18:24:00 2009 +0200 (2009-06-30)
changeset 31882 3578434d645d
parent 31881 eba74a5790d2
child 32038 4127b89f48ab
permissions -rw-r--r--
remove DERIV_tac and deriv_tac, neither is used in Isabelle/HOL or the AFP
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
lemma Maclaurin_lemma2:
huffman@29187
    31
  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
    32
  assumes n: "n = Suc k"
huffman@29187
    33
  assumes difg: "difg =
paulson@15079
    34
        (\<lambda>m t. diff m t -
paulson@15079
    35
               ((\<Sum>p = 0..<n - m. diff (m + p) 0 / real (fact p) * t ^ p) +
huffman@29187
    36
                B * (t ^ (n - m) / real (fact (n - m)))))"
huffman@29187
    37
  shows
huffman@29187
    38
      "\<forall>m t. m < n & 0 \<le> t & t \<le> h --> DERIV (difg m) t :> difg (Suc m) t"
huffman@29187
    39
unfolding difg
huffman@29187
    40
 apply clarify
huffman@29187
    41
 apply (rule DERIV_diff)
huffman@29187
    42
  apply (simp add: diff)
huffman@29187
    43
 apply (simp only: n)
huffman@29187
    44
 apply (rule DERIV_add)
huffman@29187
    45
  apply (rule_tac [2] DERIV_cmult)
huffman@29187
    46
  apply (rule_tac [2] lemma_DERIV_subst)
huffman@29187
    47
   apply (rule_tac [2] DERIV_quotient)
huffman@29187
    48
     apply (rule_tac [3] DERIV_const)
huffman@29187
    49
    apply (rule_tac [2] DERIV_pow)
huffman@29187
    50
   prefer 3 apply (simp add: fact_diff_Suc)
huffman@29187
    51
  prefer 2 apply simp
huffman@29187
    52
 apply (frule less_iff_Suc_add [THEN iffD1], clarify)
huffman@29187
    53
 apply (simp del: setsum_op_ivl_Suc)
huffman@30082
    54
 apply (insert sumr_offset4 [of "Suc 0"])
huffman@30273
    55
 apply (simp del: setsum_op_ivl_Suc fact_Suc power_Suc)
huffman@29187
    56
 apply (rule lemma_DERIV_subst)
huffman@29187
    57
  apply (rule DERIV_add)
huffman@29187
    58
   apply (rule_tac [2] DERIV_const)
huffman@29187
    59
  apply (rule DERIV_sumr, clarify)
huffman@29187
    60
  prefer 2 apply simp
huffman@30273
    61
 apply (simp (no_asm) add: divide_inverse mult_assoc del: fact_Suc power_Suc)
huffman@29187
    62
 apply (rule DERIV_cmult)
huffman@29187
    63
 apply (rule lemma_DERIV_subst)
hoelzl@31881
    64
  apply (best intro!: DERIV_intros)
huffman@29187
    65
 apply (subst fact_Suc)
huffman@29187
    66
 apply (subst real_of_nat_mult)
huffman@29187
    67
 apply (simp add: mult_ac)
paulson@15079
    68
done
paulson@15079
    69
paulson@15079
    70
lemma Maclaurin:
huffman@29187
    71
  assumes h: "0 < h"
huffman@29187
    72
  assumes n: "0 < n"
huffman@29187
    73
  assumes diff_0: "diff 0 = f"
huffman@29187
    74
  assumes diff_Suc:
huffman@29187
    75
    "\<forall>m t. m < n & 0 \<le> t & t \<le> h --> DERIV (diff m) t :> diff (Suc m) t"
huffman@29187
    76
  shows
huffman@29187
    77
    "\<exists>t. 0 < t & t < h &
paulson@15079
    78
              f h =
nipkow@15539
    79
              setsum (%m. (diff m 0 / real (fact m)) * h ^ m) {0..<n} +
paulson@15079
    80
              (diff n t / real (fact n)) * h ^ n"
huffman@29187
    81
proof -
huffman@29187
    82
  from n obtain m where m: "n = Suc m"
huffman@29187
    83
    by (cases n, simp add: n)
huffman@29187
    84
huffman@29187
    85
  obtain B where f_h: "f h =
huffman@29187
    86
        (\<Sum>m = 0..<n. diff m (0\<Colon>real) / real (fact m) * h ^ m) +
huffman@29187
    87
        B * (h ^ n / real (fact n))"
huffman@29187
    88
    using Maclaurin_lemma [OF h] ..
huffman@29187
    89
huffman@29187
    90
  obtain g where g_def: "g = (%t. f t -
huffman@29187
    91
    (setsum (%m. (diff m 0 / real(fact m)) * t^m) {0..<n}
huffman@29187
    92
      + (B * (t^n / real(fact n)))))" by blast
huffman@29187
    93
huffman@29187
    94
  have g2: "g 0 = 0 & g h = 0"
huffman@29187
    95
    apply (simp add: m f_h g_def del: setsum_op_ivl_Suc)
huffman@30082
    96
    apply (cut_tac n = m and k = "Suc 0" in sumr_offset2)
huffman@29187
    97
    apply (simp add: eq_diff_eq' diff_0 del: setsum_op_ivl_Suc)
huffman@29187
    98
    done
huffman@29187
    99
huffman@29187
   100
  obtain difg where difg_def: "difg = (%m t. diff m t -
huffman@29187
   101
    (setsum (%p. (diff (m + p) 0 / real (fact p)) * (t ^ p)) {0..<n-m}
huffman@29187
   102
      + (B * ((t ^ (n - m)) / real (fact (n - m))))))" by blast
huffman@29187
   103
huffman@29187
   104
  have difg_0: "difg 0 = g"
huffman@29187
   105
    unfolding difg_def g_def by (simp add: diff_0)
huffman@29187
   106
huffman@29187
   107
  have difg_Suc: "\<forall>(m\<Colon>nat) t\<Colon>real.
huffman@29187
   108
        m < n \<and> (0\<Colon>real) \<le> t \<and> t \<le> h \<longrightarrow> DERIV (difg m) t :> difg (Suc m) t"
huffman@29187
   109
    using diff_Suc m difg_def by (rule Maclaurin_lemma2)
huffman@29187
   110
huffman@29187
   111
  have difg_eq_0: "\<forall>m. m < n --> difg m 0 = 0"
huffman@29187
   112
    apply clarify
huffman@29187
   113
    apply (simp add: m difg_def)
huffman@29187
   114
    apply (frule less_iff_Suc_add [THEN iffD1], clarify)
huffman@29187
   115
    apply (simp del: setsum_op_ivl_Suc)
huffman@30082
   116
    apply (insert sumr_offset4 [of "Suc 0"])
huffman@30273
   117
    apply (simp del: setsum_op_ivl_Suc fact_Suc)
huffman@29187
   118
    done
huffman@29187
   119
huffman@29187
   120
  have isCont_difg: "\<And>m x. \<lbrakk>m < n; 0 \<le> x; x \<le> h\<rbrakk> \<Longrightarrow> isCont (difg m) x"
huffman@29187
   121
    by (rule DERIV_isCont [OF difg_Suc [rule_format]]) simp
huffman@29187
   122
huffman@29187
   123
  have differentiable_difg:
huffman@29187
   124
    "\<And>m x. \<lbrakk>m < n; 0 \<le> x; x \<le> h\<rbrakk> \<Longrightarrow> difg m differentiable x"
huffman@29187
   125
    by (rule differentiableI [OF difg_Suc [rule_format]]) simp
huffman@29187
   126
huffman@29187
   127
  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
   128
        \<Longrightarrow> difg (Suc m) t = 0"
huffman@29187
   129
    by (rule DERIV_unique [OF difg_Suc [rule_format]]) simp
huffman@29187
   130
huffman@29187
   131
  have "m < n" using m by simp
huffman@29187
   132
huffman@29187
   133
  have "\<exists>t. 0 < t \<and> t < h \<and> DERIV (difg m) t :> 0"
huffman@29187
   134
  using `m < n`
huffman@29187
   135
  proof (induct m)
huffman@29187
   136
  case 0
huffman@29187
   137
    show ?case
huffman@29187
   138
    proof (rule Rolle)
huffman@29187
   139
      show "0 < h" by fact
huffman@29187
   140
      show "difg 0 0 = difg 0 h" by (simp add: difg_0 g2)
huffman@29187
   141
      show "\<forall>x. 0 \<le> x \<and> x \<le> h \<longrightarrow> isCont (difg (0\<Colon>nat)) x"
huffman@29187
   142
        by (simp add: isCont_difg n)
huffman@29187
   143
      show "\<forall>x. 0 < x \<and> x < h \<longrightarrow> difg (0\<Colon>nat) differentiable x"
huffman@29187
   144
        by (simp add: differentiable_difg n)
huffman@29187
   145
    qed
huffman@29187
   146
  next
huffman@29187
   147
  case (Suc m')
huffman@29187
   148
    hence "\<exists>t. 0 < t \<and> t < h \<and> DERIV (difg m') t :> 0" by simp
huffman@29187
   149
    then obtain t where t: "0 < t" "t < h" "DERIV (difg m') t :> 0" by fast
huffman@29187
   150
    have "\<exists>t'. 0 < t' \<and> t' < t \<and> DERIV (difg (Suc m')) t' :> 0"
huffman@29187
   151
    proof (rule Rolle)
huffman@29187
   152
      show "0 < t" by fact
huffman@29187
   153
      show "difg (Suc m') 0 = difg (Suc m') t"
huffman@29187
   154
        using t `Suc m' < n` by (simp add: difg_Suc_eq_0 difg_eq_0)
huffman@29187
   155
      show "\<forall>x. 0 \<le> x \<and> x \<le> t \<longrightarrow> isCont (difg (Suc m')) x"
huffman@29187
   156
        using `t < h` `Suc m' < n` by (simp add: isCont_difg)
huffman@29187
   157
      show "\<forall>x. 0 < x \<and> x < t \<longrightarrow> difg (Suc m') differentiable x"
huffman@29187
   158
        using `t < h` `Suc m' < n` by (simp add: differentiable_difg)
huffman@29187
   159
    qed
huffman@29187
   160
    thus ?case
huffman@29187
   161
      using `t < h` by auto
huffman@29187
   162
  qed
huffman@29187
   163
huffman@29187
   164
  then obtain t where "0 < t" "t < h" "DERIV (difg m) t :> 0" by fast
huffman@29187
   165
huffman@29187
   166
  hence "difg (Suc m) t = 0"
huffman@29187
   167
    using `m < n` by (simp add: difg_Suc_eq_0)
huffman@29187
   168
huffman@29187
   169
  show ?thesis
huffman@29187
   170
  proof (intro exI conjI)
huffman@29187
   171
    show "0 < t" by fact
huffman@29187
   172
    show "t < h" by fact
huffman@29187
   173
    show "f h =
huffman@29187
   174
      (\<Sum>m = 0..<n. diff m 0 / real (fact m) * h ^ m) +
huffman@29187
   175
      diff n t / real (fact n) * h ^ n"
huffman@29187
   176
      using `difg (Suc m) t = 0`
huffman@30273
   177
      by (simp add: m f_h difg_def del: fact_Suc)
huffman@29187
   178
  qed
huffman@29187
   179
huffman@29187
   180
qed
paulson@15079
   181
paulson@15079
   182
lemma Maclaurin_objl:
nipkow@25162
   183
  "0 < h & n>0 & diff 0 = f &
nipkow@25134
   184
  (\<forall>m t. m < n & 0 \<le> t & t \<le> h --> DERIV (diff m) t :> diff (Suc m) t)
nipkow@25134
   185
   --> (\<exists>t. 0 < t & t < h &
nipkow@25134
   186
            f h = (\<Sum>m=0..<n. diff m 0 / real (fact m) * h ^ m) +
nipkow@25134
   187
                  diff n t / real (fact n) * h ^ n)"
paulson@15079
   188
by (blast intro: Maclaurin)
paulson@15079
   189
paulson@15079
   190
paulson@15079
   191
lemma Maclaurin2:
paulson@15079
   192
   "[| 0 < h; diff 0 = f;
paulson@15079
   193
       \<forall>m t.
paulson@15079
   194
          m < n & 0 \<le> t & t \<le> h --> DERIV (diff m) t :> diff (Suc m) t |]
paulson@15079
   195
    ==> \<exists>t. 0 < t &
paulson@15079
   196
              t \<le> h &
paulson@15079
   197
              f h =
nipkow@15539
   198
              (\<Sum>m=0..<n. diff m 0 / real (fact m) * h ^ m) +
paulson@15079
   199
              diff n t / real (fact n) * h ^ n"
paulson@15079
   200
apply (case_tac "n", auto)
paulson@15079
   201
apply (drule Maclaurin, auto)
paulson@15079
   202
done
paulson@15079
   203
paulson@15079
   204
lemma Maclaurin2_objl:
paulson@15079
   205
     "0 < h & diff 0 = f &
paulson@15079
   206
       (\<forall>m t.
paulson@15079
   207
          m < n & 0 \<le> t & t \<le> h --> DERIV (diff m) t :> diff (Suc m) t)
paulson@15079
   208
    --> (\<exists>t. 0 < t &
paulson@15079
   209
              t \<le> h &
paulson@15079
   210
              f h =
nipkow@15539
   211
              (\<Sum>m=0..<n. diff m 0 / real (fact m) * h ^ m) +
paulson@15079
   212
              diff n t / real (fact n) * h ^ n)"
paulson@15079
   213
by (blast intro: Maclaurin2)
paulson@15079
   214
paulson@15079
   215
lemma Maclaurin_minus:
nipkow@25162
   216
   "[| h < 0; n > 0; diff 0 = f;
paulson@15079
   217
       \<forall>m t. m < n & h \<le> t & t \<le> 0 --> DERIV (diff m) t :> diff (Suc m) t |]
paulson@15079
   218
    ==> \<exists>t. h < t &
paulson@15079
   219
              t < 0 &
paulson@15079
   220
              f h =
nipkow@15539
   221
              (\<Sum>m=0..<n. diff m 0 / real (fact m) * h ^ m) +
paulson@15079
   222
              diff n t / real (fact n) * h ^ n"
paulson@15079
   223
apply (cut_tac f = "%x. f (-x)"
huffman@23177
   224
        and diff = "%n x. (-1 ^ n) * diff n (-x)"
paulson@15079
   225
        and h = "-h" and n = n in Maclaurin_objl)
nipkow@15539
   226
apply (simp)
paulson@15079
   227
apply safe
paulson@15079
   228
apply (subst minus_mult_right)
paulson@15079
   229
apply (rule DERIV_cmult)
paulson@15079
   230
apply (rule lemma_DERIV_subst)
paulson@15079
   231
apply (rule DERIV_chain2 [where g=uminus])
huffman@23069
   232
apply (rule_tac [2] DERIV_minus, rule_tac [2] DERIV_ident)
paulson@15079
   233
prefer 2 apply force
paulson@15079
   234
apply force
paulson@15079
   235
apply (rule_tac x = "-t" in exI, auto)
paulson@15079
   236
apply (subgoal_tac "(\<Sum>m = 0..<n. -1 ^ m * diff m 0 * (-h)^m / real(fact m)) =
paulson@15079
   237
                    (\<Sum>m = 0..<n. diff m 0 * h ^ m / real(fact m))")
nipkow@15536
   238
apply (rule_tac [2] setsum_cong[OF refl])
paulson@15079
   239
apply (auto simp add: divide_inverse power_mult_distrib [symmetric])
paulson@15079
   240
done
paulson@15079
   241
paulson@15079
   242
lemma Maclaurin_minus_objl:
nipkow@25162
   243
     "(h < 0 & n > 0 & diff 0 = f &
paulson@15079
   244
       (\<forall>m t.
paulson@15079
   245
          m < n & h \<le> t & t \<le> 0 --> DERIV (diff m) t :> diff (Suc m) t))
paulson@15079
   246
    --> (\<exists>t. h < t &
paulson@15079
   247
              t < 0 &
paulson@15079
   248
              f h =
nipkow@15539
   249
              (\<Sum>m=0..<n. diff m 0 / real (fact m) * h ^ m) +
paulson@15079
   250
              diff n t / real (fact n) * h ^ n)"
paulson@15079
   251
by (blast intro: Maclaurin_minus)
paulson@15079
   252
paulson@15079
   253
paulson@15079
   254
subsection{*More Convenient "Bidirectional" Version.*}
paulson@15079
   255
paulson@15079
   256
(* not good for PVS sin_approx, cos_approx *)
paulson@15079
   257
paulson@15079
   258
lemma Maclaurin_bi_le_lemma [rule_format]:
nipkow@25162
   259
  "n>0 \<longrightarrow>
nipkow@25134
   260
   diff 0 0 =
nipkow@25134
   261
   (\<Sum>m = 0..<n. diff m 0 * 0 ^ m / real (fact m)) +
nipkow@25134
   262
   diff n 0 * 0 ^ n / real (fact n)"
paulson@15251
   263
by (induct "n", auto)
obua@14738
   264
paulson@15079
   265
lemma Maclaurin_bi_le:
paulson@15079
   266
   "[| diff 0 = f;
paulson@15079
   267
       \<forall>m t. m < n & abs t \<le> abs x --> DERIV (diff m) t :> diff (Suc m) t |]
paulson@15079
   268
    ==> \<exists>t. abs t \<le> abs x &
paulson@15079
   269
              f x =
nipkow@15539
   270
              (\<Sum>m=0..<n. diff m 0 / real (fact m) * x ^ m) +
paulson@15079
   271
              diff n t / real (fact n) * x ^ n"
paulson@15079
   272
apply (case_tac "n = 0", force)
paulson@15079
   273
apply (case_tac "x = 0")
nipkow@25134
   274
 apply (rule_tac x = 0 in exI)
nipkow@25134
   275
 apply (force simp add: Maclaurin_bi_le_lemma)
nipkow@25134
   276
apply (cut_tac x = x and y = 0 in linorder_less_linear, auto)
nipkow@25134
   277
 txt{*Case 1, where @{term "x < 0"}*}
nipkow@25134
   278
 apply (cut_tac f = "diff 0" and diff = diff and h = x and n = n in Maclaurin_minus_objl, safe)
nipkow@25134
   279
  apply (simp add: abs_if)
nipkow@25134
   280
 apply (rule_tac x = t in exI)
nipkow@25134
   281
 apply (simp add: abs_if)
paulson@15079
   282
txt{*Case 2, where @{term "0 < x"}*}
paulson@15079
   283
apply (cut_tac f = "diff 0" and diff = diff and h = x and n = n in Maclaurin_objl, safe)
nipkow@25134
   284
 apply (simp add: abs_if)
paulson@15079
   285
apply (rule_tac x = t in exI)
paulson@15079
   286
apply (simp add: abs_if)
paulson@15079
   287
done
paulson@15079
   288
paulson@15079
   289
lemma Maclaurin_all_lt:
paulson@15079
   290
     "[| diff 0 = f;
paulson@15079
   291
         \<forall>m x. DERIV (diff m) x :> diff(Suc m) x;
nipkow@25162
   292
        x ~= 0; n > 0
paulson@15079
   293
      |] ==> \<exists>t. 0 < abs t & abs t < abs x &
nipkow@15539
   294
               f x = (\<Sum>m=0..<n. (diff m 0 / real (fact m)) * x ^ m) +
paulson@15079
   295
                     (diff n t / real (fact n)) * x ^ n"
paulson@15079
   296
apply (rule_tac x = x and y = 0 in linorder_cases)
paulson@15079
   297
prefer 2 apply blast
paulson@15079
   298
apply (drule_tac [2] diff=diff in Maclaurin)
paulson@15079
   299
apply (drule_tac diff=diff in Maclaurin_minus, simp_all, safe)
paulson@15229
   300
apply (rule_tac [!] x = t in exI, auto)
paulson@15079
   301
done
paulson@15079
   302
paulson@15079
   303
lemma Maclaurin_all_lt_objl:
paulson@15079
   304
     "diff 0 = f &
paulson@15079
   305
      (\<forall>m x. DERIV (diff m) x :> diff(Suc m) x) &
nipkow@25162
   306
      x ~= 0 & n > 0
paulson@15079
   307
      --> (\<exists>t. 0 < abs t & abs t < abs x &
nipkow@15539
   308
               f x = (\<Sum>m=0..<n. (diff m 0 / real (fact m)) * x ^ m) +
paulson@15079
   309
                     (diff n t / real (fact n)) * x ^ n)"
paulson@15079
   310
by (blast intro: Maclaurin_all_lt)
paulson@15079
   311
paulson@15079
   312
lemma Maclaurin_zero [rule_format]:
paulson@15079
   313
     "x = (0::real)
nipkow@25134
   314
      ==> n \<noteq> 0 -->
nipkow@15539
   315
          (\<Sum>m=0..<n. (diff m (0::real) / real (fact m)) * x ^ m) =
paulson@15079
   316
          diff 0 0"
paulson@15079
   317
by (induct n, auto)
paulson@15079
   318
paulson@15079
   319
lemma Maclaurin_all_le: "[| diff 0 = f;
paulson@15079
   320
        \<forall>m x. DERIV (diff m) x :> diff (Suc m) x
paulson@15079
   321
      |] ==> \<exists>t. abs t \<le> abs x &
nipkow@15539
   322
              f x = (\<Sum>m=0..<n. (diff m 0 / real (fact m)) * x ^ m) +
paulson@15079
   323
                    (diff n t / real (fact n)) * x ^ n"
nipkow@25134
   324
apply(cases "n=0")
nipkow@25134
   325
apply (force)
paulson@15079
   326
apply (case_tac "x = 0")
paulson@15079
   327
apply (frule_tac diff = diff and n = n in Maclaurin_zero, assumption)
nipkow@25134
   328
apply (drule not0_implies_Suc)
paulson@15079
   329
apply (rule_tac x = 0 in exI, force)
paulson@15079
   330
apply (frule_tac diff = diff and n = n in Maclaurin_all_lt, auto)
paulson@15079
   331
apply (rule_tac x = t in exI, auto)
paulson@15079
   332
done
paulson@15079
   333
paulson@15079
   334
lemma Maclaurin_all_le_objl: "diff 0 = f &
paulson@15079
   335
      (\<forall>m x. DERIV (diff m) x :> diff (Suc m) x)
paulson@15079
   336
      --> (\<exists>t. abs t \<le> abs x &
nipkow@15539
   337
              f x = (\<Sum>m=0..<n. (diff m 0 / real (fact m)) * x ^ m) +
paulson@15079
   338
                    (diff n t / real (fact n)) * x ^ n)"
paulson@15079
   339
by (blast intro: Maclaurin_all_le)
paulson@15079
   340
paulson@15079
   341
paulson@15079
   342
subsection{*Version for Exponential Function*}
paulson@15079
   343
nipkow@25162
   344
lemma Maclaurin_exp_lt: "[| x ~= 0; n > 0 |]
paulson@15079
   345
      ==> (\<exists>t. 0 < abs t &
paulson@15079
   346
                abs t < abs x &
nipkow@15539
   347
                exp x = (\<Sum>m=0..<n. (x ^ m) / real (fact m)) +
paulson@15079
   348
                        (exp t / real (fact n)) * x ^ n)"
paulson@15079
   349
by (cut_tac diff = "%n. exp" and f = exp and x = x and n = n in Maclaurin_all_lt_objl, auto)
paulson@15079
   350
paulson@15079
   351
paulson@15079
   352
lemma Maclaurin_exp_le:
paulson@15079
   353
     "\<exists>t. abs t \<le> abs x &
nipkow@15539
   354
            exp x = (\<Sum>m=0..<n. (x ^ m) / real (fact m)) +
paulson@15079
   355
                       (exp t / real (fact n)) * x ^ n"
paulson@15079
   356
by (cut_tac diff = "%n. exp" and f = exp and x = x and n = n in Maclaurin_all_le_objl, auto)
paulson@15079
   357
paulson@15079
   358
paulson@15079
   359
subsection{*Version for Sine Function*}
paulson@15079
   360
paulson@15079
   361
lemma mod_exhaust_less_4:
nipkow@25134
   362
  "m mod 4 = 0 | m mod 4 = 1 | m mod 4 = 2 | m mod 4 = (3::nat)"
webertj@20217
   363
by auto
paulson@15079
   364
paulson@15079
   365
lemma Suc_Suc_mult_two_diff_two [rule_format, simp]:
nipkow@25134
   366
  "n\<noteq>0 --> Suc (Suc (2 * n - 2)) = 2*n"
paulson@15251
   367
by (induct "n", auto)
paulson@15079
   368
paulson@15079
   369
lemma lemma_Suc_Suc_4n_diff_2 [rule_format, simp]:
nipkow@25134
   370
  "n\<noteq>0 --> Suc (Suc (4*n - 2)) = 4*n"
paulson@15251
   371
by (induct "n", auto)
paulson@15079
   372
paulson@15079
   373
lemma Suc_mult_two_diff_one [rule_format, simp]:
nipkow@25134
   374
  "n\<noteq>0 --> Suc (2 * n - 1) = 2*n"
paulson@15251
   375
by (induct "n", auto)
paulson@15079
   376
paulson@15234
   377
paulson@15234
   378
text{*It is unclear why so many variant results are needed.*}
paulson@15079
   379
paulson@15079
   380
lemma Maclaurin_sin_expansion2:
paulson@15079
   381
     "\<exists>t. abs t \<le> abs x &
paulson@15079
   382
       sin x =
nipkow@15539
   383
       (\<Sum>m=0..<n. (if even m then 0
huffman@23177
   384
                       else (-1 ^ ((m - Suc 0) div 2)) / real (fact m)) *
nipkow@15539
   385
                       x ^ m)
paulson@15079
   386
      + ((sin(t + 1/2 * real (n) *pi) / real (fact n)) * x ^ n)"
paulson@15079
   387
apply (cut_tac f = sin and n = n and x = x
paulson@15079
   388
        and diff = "%n x. sin (x + 1/2*real n * pi)" in Maclaurin_all_lt_objl)
paulson@15079
   389
apply safe
paulson@15079
   390
apply (simp (no_asm))
nipkow@15539
   391
apply (simp (no_asm))
huffman@23242
   392
apply (case_tac "n", clarify, simp, simp add: lemma_STAR_sin)
paulson@15079
   393
apply (rule ccontr, simp)
paulson@15079
   394
apply (drule_tac x = x in spec, simp)
paulson@15079
   395
apply (erule ssubst)
paulson@15079
   396
apply (rule_tac x = t in exI, simp)
nipkow@15536
   397
apply (rule setsum_cong[OF refl])
nipkow@15539
   398
apply (auto simp add: sin_zero_iff odd_Suc_mult_two_ex)
paulson@15079
   399
done
paulson@15079
   400
paulson@15234
   401
lemma Maclaurin_sin_expansion:
paulson@15234
   402
     "\<exists>t. sin x =
nipkow@15539
   403
       (\<Sum>m=0..<n. (if even m then 0
huffman@23177
   404
                       else (-1 ^ ((m - Suc 0) div 2)) / real (fact m)) *
nipkow@15539
   405
                       x ^ m)
paulson@15234
   406
      + ((sin(t + 1/2 * real (n) *pi) / real (fact n)) * x ^ n)"
paulson@15234
   407
apply (insert Maclaurin_sin_expansion2 [of x n]) 
paulson@15234
   408
apply (blast intro: elim:); 
paulson@15234
   409
done
paulson@15234
   410
paulson@15234
   411
paulson@15079
   412
lemma Maclaurin_sin_expansion3:
nipkow@25162
   413
     "[| n > 0; 0 < x |] ==>
paulson@15079
   414
       \<exists>t. 0 < t & t < x &
paulson@15079
   415
       sin x =
nipkow@15539
   416
       (\<Sum>m=0..<n. (if even m then 0
huffman@23177
   417
                       else (-1 ^ ((m - Suc 0) div 2)) / real (fact m)) *
nipkow@15539
   418
                       x ^ m)
paulson@15079
   419
      + ((sin(t + 1/2 * real(n) *pi) / real (fact n)) * x ^ n)"
paulson@15079
   420
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
   421
apply safe
paulson@15079
   422
apply simp
nipkow@15539
   423
apply (simp (no_asm))
paulson@15079
   424
apply (erule ssubst)
paulson@15079
   425
apply (rule_tac x = t in exI, simp)
nipkow@15536
   426
apply (rule setsum_cong[OF refl])
nipkow@15539
   427
apply (auto simp add: sin_zero_iff odd_Suc_mult_two_ex)
paulson@15079
   428
done
paulson@15079
   429
paulson@15079
   430
lemma Maclaurin_sin_expansion4:
paulson@15079
   431
     "0 < x ==>
paulson@15079
   432
       \<exists>t. 0 < t & t \<le> x &
paulson@15079
   433
       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@15079
   437
      + ((sin(t + 1/2 * real (n) *pi) / real (fact n)) * x ^ n)"
paulson@15079
   438
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
   439
apply safe
paulson@15079
   440
apply simp
nipkow@15539
   441
apply (simp (no_asm))
paulson@15079
   442
apply (erule ssubst)
paulson@15079
   443
apply (rule_tac x = t in exI, simp)
nipkow@15536
   444
apply (rule setsum_cong[OF refl])
nipkow@15539
   445
apply (auto simp add: sin_zero_iff odd_Suc_mult_two_ex)
paulson@15079
   446
done
paulson@15079
   447
paulson@15079
   448
paulson@15079
   449
subsection{*Maclaurin Expansion for Cosine Function*}
paulson@15079
   450
paulson@15079
   451
lemma sumr_cos_zero_one [simp]:
nipkow@15539
   452
 "(\<Sum>m=0..<(Suc n).
huffman@23177
   453
     (if even m then -1 ^ (m div 2)/(real  (fact m)) else 0) * 0 ^ m) = 1"
paulson@15251
   454
by (induct "n", auto)
paulson@15079
   455
paulson@15079
   456
lemma Maclaurin_cos_expansion:
paulson@15079
   457
     "\<exists>t. abs t \<le> abs x &
paulson@15079
   458
       cos x =
nipkow@15539
   459
       (\<Sum>m=0..<n. (if even m
huffman@23177
   460
                       then -1 ^ (m div 2)/(real (fact m))
paulson@15079
   461
                       else 0) *
nipkow@15539
   462
                       x ^ m)
paulson@15079
   463
      + ((cos(t + 1/2 * real (n) *pi) / real (fact n)) * x ^ n)"
paulson@15079
   464
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
   465
apply safe
paulson@15079
   466
apply (simp (no_asm))
nipkow@15539
   467
apply (simp (no_asm))
paulson@15079
   468
apply (case_tac "n", simp)
nipkow@15561
   469
apply (simp del: setsum_op_ivl_Suc)
paulson@15079
   470
apply (rule ccontr, simp)
paulson@15079
   471
apply (drule_tac x = x in spec, simp)
paulson@15079
   472
apply (erule ssubst)
paulson@15079
   473
apply (rule_tac x = t in exI, simp)
nipkow@15536
   474
apply (rule setsum_cong[OF refl])
paulson@15234
   475
apply (auto simp add: cos_zero_iff even_mult_two_ex)
paulson@15079
   476
done
paulson@15079
   477
paulson@15079
   478
lemma Maclaurin_cos_expansion2:
nipkow@25162
   479
     "[| 0 < x; n > 0 |] ==>
paulson@15079
   480
       \<exists>t. 0 < t & t < x &
paulson@15079
   481
       cos x =
nipkow@15539
   482
       (\<Sum>m=0..<n. (if even m
huffman@23177
   483
                       then -1 ^ (m div 2)/(real (fact m))
paulson@15079
   484
                       else 0) *
nipkow@15539
   485
                       x ^ m)
paulson@15079
   486
      + ((cos(t + 1/2 * real (n) *pi) / real (fact n)) * x ^ n)"
paulson@15079
   487
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
   488
apply safe
paulson@15079
   489
apply simp
nipkow@15539
   490
apply (simp (no_asm))
paulson@15079
   491
apply (erule ssubst)
paulson@15079
   492
apply (rule_tac x = t in exI, simp)
nipkow@15536
   493
apply (rule setsum_cong[OF refl])
paulson@15234
   494
apply (auto simp add: cos_zero_iff even_mult_two_ex)
paulson@15079
   495
done
paulson@15079
   496
paulson@15234
   497
lemma Maclaurin_minus_cos_expansion:
nipkow@25162
   498
     "[| x < 0; n > 0 |] ==>
paulson@15079
   499
       \<exists>t. x < t & t < 0 &
paulson@15079
   500
       cos x =
nipkow@15539
   501
       (\<Sum>m=0..<n. (if even m
huffman@23177
   502
                       then -1 ^ (m div 2)/(real (fact m))
paulson@15079
   503
                       else 0) *
nipkow@15539
   504
                       x ^ m)
paulson@15079
   505
      + ((cos(t + 1/2 * real (n) *pi) / real (fact n)) * x ^ n)"
paulson@15079
   506
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
   507
apply safe
paulson@15079
   508
apply simp
nipkow@15539
   509
apply (simp (no_asm))
paulson@15079
   510
apply (erule ssubst)
paulson@15079
   511
apply (rule_tac x = t in exI, simp)
nipkow@15536
   512
apply (rule setsum_cong[OF refl])
paulson@15234
   513
apply (auto simp add: cos_zero_iff even_mult_two_ex)
paulson@15079
   514
done
paulson@15079
   515
paulson@15079
   516
(* ------------------------------------------------------------------------- *)
paulson@15079
   517
(* Version for ln(1 +/- x). Where is it??                                    *)
paulson@15079
   518
(* ------------------------------------------------------------------------- *)
paulson@15079
   519
paulson@15079
   520
lemma sin_bound_lemma:
paulson@15081
   521
    "[|x = y; abs u \<le> (v::real) |] ==> \<bar>(x + u) - y\<bar> \<le> v"
paulson@15079
   522
by auto
paulson@15079
   523
paulson@15079
   524
lemma Maclaurin_sin_bound:
huffman@23177
   525
  "abs(sin x - (\<Sum>m=0..<n. (if even m then 0 else (-1 ^ ((m - Suc 0) div 2)) / real (fact m)) *
paulson@15081
   526
  x ^ m))  \<le> inverse(real (fact n)) * \<bar>x\<bar> ^ n"
obua@14738
   527
proof -
paulson@15079
   528
  have "!! x (y::real). x \<le> 1 \<Longrightarrow> 0 \<le> y \<Longrightarrow> x * y \<le> 1 * y"
obua@14738
   529
    by (rule_tac mult_right_mono,simp_all)
obua@14738
   530
  note est = this[simplified]
huffman@22985
   531
  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
   532
  have diff_0: "?diff 0 = sin" by simp
huffman@22985
   533
  have DERIV_diff: "\<forall>m x. DERIV (?diff m) x :> ?diff (Suc m) x"
huffman@22985
   534
    apply (clarify)
huffman@22985
   535
    apply (subst (1 2 3) mod_Suc_eq_Suc_mod)
huffman@22985
   536
    apply (cut_tac m=m in mod_exhaust_less_4)
hoelzl@31881
   537
    apply (safe, auto intro!: DERIV_intros)
huffman@22985
   538
    done
huffman@22985
   539
  from Maclaurin_all_le [OF diff_0 DERIV_diff]
huffman@22985
   540
  obtain t where t1: "\<bar>t\<bar> \<le> \<bar>x\<bar>" and
huffman@22985
   541
    t2: "sin x = (\<Sum>m = 0..<n. ?diff m 0 / real (fact m) * x ^ m) +
huffman@22985
   542
      ?diff n t / real (fact n) * x ^ n" by fast
huffman@22985
   543
  have diff_m_0:
huffman@22985
   544
    "\<And>m. ?diff m 0 = (if even m then 0
huffman@23177
   545
         else -1 ^ ((m - Suc 0) div 2))"
huffman@22985
   546
    apply (subst even_even_mod_4_iff)
huffman@22985
   547
    apply (cut_tac m=m in mod_exhaust_less_4)
huffman@22985
   548
    apply (elim disjE, simp_all)
huffman@22985
   549
    apply (safe dest!: mod_eqD, simp_all)
huffman@22985
   550
    done
obua@14738
   551
  show ?thesis
huffman@22985
   552
    apply (subst t2)
paulson@15079
   553
    apply (rule sin_bound_lemma)
nipkow@15536
   554
    apply (rule setsum_cong[OF refl])
huffman@22985
   555
    apply (subst diff_m_0, simp)
paulson@15079
   556
    apply (auto intro: mult_right_mono [where b=1, simplified] mult_right_mono
avigad@16775
   557
                   simp add: est mult_nonneg_nonneg mult_ac divide_inverse
paulson@16924
   558
                          power_abs [symmetric] abs_mult)
obua@14738
   559
    done
obua@14738
   560
qed
obua@14738
   561
paulson@15079
   562
end