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