src/HOL/MacLaurin.thy
author haftmann
Sun Sep 21 16:56:11 2014 +0200 (2014-09-21)
changeset 58410 6d46ad54a2ab
parent 57514 bdc2c6b40bf2
child 58709 efdc6c533bd3
permissions -rw-r--r--
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
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
bulwahn@41120
     4
    Conversion of Mac Laurin to Isar by Lukas Bulwahn and Bernhard Häupler, 2005
paulson@12224
     5
*)
paulson@12224
     6
paulson@15944
     7
header{*MacLaurin Series*}
paulson@15944
     8
nipkow@15131
     9
theory MacLaurin
chaieb@29811
    10
imports Transcendental
nipkow@15131
    11
begin
paulson@15079
    12
paulson@15079
    13
subsection{*Maclaurin's Theorem with Lagrange Form of Remainder*}
paulson@15079
    14
paulson@15079
    15
text{*This is a very long, messy proof even now that it's been broken down
paulson@15079
    16
into lemmas.*}
paulson@15079
    17
paulson@15079
    18
lemma Maclaurin_lemma:
paulson@15079
    19
    "0 < h ==>
hoelzl@56193
    20
     \<exists>B. f h = (\<Sum>m<n. (j m / real (fact m)) * (h^m)) +
paulson@15079
    21
               (B * ((h^n) / real(fact n)))"
hoelzl@56193
    22
by (rule exI[where x = "(f h - (\<Sum>m<n. (j m / real (fact m)) * h^m)) * real(fact n) / (h^n)"]) simp
paulson@15079
    23
paulson@15079
    24
lemma eq_diff_eq': "(x = y - z) = (y = x + (z::real))"
paulson@15079
    25
by arith
paulson@15079
    26
avigad@32038
    27
lemma fact_diff_Suc [rule_format]:
avigad@32038
    28
  "n < Suc m ==> fact (Suc m - n) = (Suc m - n) * fact (m - n)"
avigad@32038
    29
  by (subst fact_reduce_nat, auto)
avigad@32038
    30
paulson@15079
    31
lemma Maclaurin_lemma2:
hoelzl@41166
    32
  fixes B
bulwahn@41120
    33
  assumes DERIV : "\<forall>m t. m < n \<and> 0\<le>t \<and> t\<le>h \<longrightarrow> DERIV (diff m) t :> diff (Suc m) t"
hoelzl@41166
    34
    and INIT : "n = Suc k"
hoelzl@56193
    35
  defines "difg \<equiv> (\<lambda>m t. diff m t - ((\<Sum>p<n - m. diff (m + p) 0 / real (fact p) * t ^ p) +
hoelzl@41166
    36
    B * (t ^ (n - m) / real (fact (n - m)))))" (is "difg \<equiv> (\<lambda>m t. diff m t - ?difg m t)")
bulwahn@41120
    37
  shows "\<forall>m t. m < n & 0 \<le> t & t \<le> h --> DERIV (difg m) t :> difg (Suc m) t"
hoelzl@41166
    38
proof (rule allI impI)+
hoelzl@41166
    39
  fix m t assume INIT2: "m < n & 0 \<le> t & t \<le> h"
hoelzl@41166
    40
  have "DERIV (difg m) t :> diff (Suc m) t -
hoelzl@56193
    41
    ((\<Sum>x<n - m. real x * t ^ (x - Suc 0) * diff (m + x) 0 / real (fact x)) +
hoelzl@41166
    42
     real (n - m) * t ^ (n - Suc m) * B / real (fact (n - m)))" unfolding difg_def
hoelzl@56381
    43
    by (auto intro!: derivative_eq_intros DERIV[rule_format, OF INIT2]
hoelzl@56381
    44
             simp: real_of_nat_def[symmetric])
hoelzl@56193
    45
  moreover
hoelzl@41166
    46
  from INIT2 have intvl: "{..<n - m} = insert 0 (Suc ` {..<n - Suc m})" and "0 < n - m"
hoelzl@41166
    47
    unfolding atLeast0LessThan[symmetric] by auto
hoelzl@56193
    48
  have "(\<Sum>x<n - m. real x * t ^ (x - Suc 0) * diff (m + x) 0 / real (fact x)) =
hoelzl@56193
    49
      (\<Sum>x<n - Suc m. real (Suc x) * t ^ x * diff (Suc m + x) 0 / real (fact (Suc x)))"
hoelzl@41166
    50
    unfolding intvl atLeast0LessThan by (subst setsum.insert) (auto simp: setsum.reindex)
hoelzl@41166
    51
  moreover
hoelzl@41166
    52
  have fact_neq_0: "\<And>x::nat. real (fact x) + real x * real (fact x) \<noteq> 0"
hoelzl@41166
    53
    by (metis fact_gt_zero_nat not_add_less1 real_of_nat_add real_of_nat_mult real_of_nat_zero_iff)
hoelzl@41166
    54
  have "\<And>x. real (Suc x) * t ^ x * diff (Suc m + x) 0 / real (fact (Suc x)) =
hoelzl@41166
    55
      diff (Suc m + x) 0 * t^x / real (fact x)"
hoelzl@41166
    56
    by (auto simp: field_simps real_of_nat_Suc fact_neq_0 intro!: nonzero_divide_eq_eq[THEN iffD2])
hoelzl@41166
    57
  moreover
hoelzl@41166
    58
  have "real (n - m) * t ^ (n - Suc m) * B / real (fact (n - m)) =
hoelzl@41166
    59
      B * (t ^ (n - Suc m) / real (fact (n - Suc m)))"
hoelzl@41166
    60
    using `0 < n - m` by (simp add: fact_reduce_nat)
hoelzl@41166
    61
  ultimately show "DERIV (difg m) t :> difg (Suc m) t"
hoelzl@41166
    62
    unfolding difg_def by simp
bulwahn@41120
    63
qed
avigad@32038
    64
paulson@15079
    65
lemma Maclaurin:
huffman@29187
    66
  assumes h: "0 < h"
huffman@29187
    67
  assumes n: "0 < n"
huffman@29187
    68
  assumes diff_0: "diff 0 = f"
huffman@29187
    69
  assumes diff_Suc:
huffman@29187
    70
    "\<forall>m t. m < n & 0 \<le> t & t \<le> h --> DERIV (diff m) t :> diff (Suc m) t"
huffman@29187
    71
  shows
huffman@29187
    72
    "\<exists>t. 0 < t & t < h &
paulson@15079
    73
              f h =
hoelzl@56193
    74
              setsum (%m. (diff m 0 / real (fact m)) * h ^ m) {..<n} +
hoelzl@41166
    75
              (diff n t / real (fact n)) * h ^ n"
huffman@29187
    76
proof -
huffman@29187
    77
  from n obtain m where m: "n = Suc m"
hoelzl@41166
    78
    by (cases n) (simp add: n)
huffman@29187
    79
huffman@29187
    80
  obtain B where f_h: "f h =
hoelzl@56193
    81
        (\<Sum>m<n. diff m (0\<Colon>real) / real (fact m) * h ^ m) +
huffman@29187
    82
        B * (h ^ n / real (fact n))"
huffman@29187
    83
    using Maclaurin_lemma [OF h] ..
huffman@29187
    84
hoelzl@41166
    85
  def g \<equiv> "(\<lambda>t. f t -
hoelzl@56193
    86
    (setsum (\<lambda>m. (diff m 0 / real(fact m)) * t^m) {..<n}
hoelzl@41166
    87
      + (B * (t^n / real(fact n)))))"
huffman@29187
    88
huffman@29187
    89
  have g2: "g 0 = 0 & g h = 0"
haftmann@57418
    90
    by (simp add: m f_h g_def lessThan_Suc_eq_insert_0 image_iff diff_0 setsum.reindex)
huffman@29187
    91
hoelzl@41166
    92
  def difg \<equiv> "(%m t. diff m t -
hoelzl@56193
    93
    (setsum (%p. (diff (m + p) 0 / real (fact p)) * (t ^ p)) {..<n-m}
hoelzl@41166
    94
      + (B * ((t ^ (n - m)) / real (fact (n - m))))))"
huffman@29187
    95
huffman@29187
    96
  have difg_0: "difg 0 = g"
huffman@29187
    97
    unfolding difg_def g_def by (simp add: diff_0)
huffman@29187
    98
huffman@29187
    99
  have difg_Suc: "\<forall>(m\<Colon>nat) t\<Colon>real.
huffman@29187
   100
        m < n \<and> (0\<Colon>real) \<le> t \<and> t \<le> h \<longrightarrow> DERIV (difg m) t :> difg (Suc m) t"
hoelzl@41166
   101
    using diff_Suc m unfolding difg_def by (rule Maclaurin_lemma2)
huffman@29187
   102
hoelzl@56193
   103
  have difg_eq_0: "\<forall>m<n. difg m 0 = 0"
haftmann@57418
   104
    by (auto simp: difg_def m Suc_diff_le lessThan_Suc_eq_insert_0 image_iff setsum.reindex)
huffman@29187
   105
huffman@29187
   106
  have isCont_difg: "\<And>m x. \<lbrakk>m < n; 0 \<le> x; x \<le> h\<rbrakk> \<Longrightarrow> isCont (difg m) x"
huffman@29187
   107
    by (rule DERIV_isCont [OF difg_Suc [rule_format]]) simp
huffman@29187
   108
huffman@29187
   109
  have differentiable_difg:
hoelzl@56181
   110
    "\<And>m x. \<lbrakk>m < n; 0 \<le> x; x \<le> h\<rbrakk> \<Longrightarrow> difg m differentiable (at x)"
huffman@29187
   111
    by (rule differentiableI [OF difg_Suc [rule_format]]) simp
huffman@29187
   112
huffman@29187
   113
  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
   114
        \<Longrightarrow> difg (Suc m) t = 0"
huffman@29187
   115
    by (rule DERIV_unique [OF difg_Suc [rule_format]]) simp
huffman@29187
   116
huffman@29187
   117
  have "m < n" using m by simp
huffman@29187
   118
huffman@29187
   119
  have "\<exists>t. 0 < t \<and> t < h \<and> DERIV (difg m) t :> 0"
huffman@29187
   120
  using `m < n`
huffman@29187
   121
  proof (induct m)
hoelzl@41166
   122
    case 0
huffman@29187
   123
    show ?case
huffman@29187
   124
    proof (rule Rolle)
huffman@29187
   125
      show "0 < h" by fact
huffman@29187
   126
      show "difg 0 0 = difg 0 h" by (simp add: difg_0 g2)
huffman@29187
   127
      show "\<forall>x. 0 \<le> x \<and> x \<le> h \<longrightarrow> isCont (difg (0\<Colon>nat)) x"
huffman@29187
   128
        by (simp add: isCont_difg n)
hoelzl@56181
   129
      show "\<forall>x. 0 < x \<and> x < h \<longrightarrow> difg (0\<Colon>nat) differentiable (at x)"
huffman@29187
   130
        by (simp add: differentiable_difg n)
huffman@29187
   131
    qed
huffman@29187
   132
  next
hoelzl@41166
   133
    case (Suc m')
huffman@29187
   134
    hence "\<exists>t. 0 < t \<and> t < h \<and> DERIV (difg m') t :> 0" by simp
huffman@29187
   135
    then obtain t where t: "0 < t" "t < h" "DERIV (difg m') t :> 0" by fast
huffman@29187
   136
    have "\<exists>t'. 0 < t' \<and> t' < t \<and> DERIV (difg (Suc m')) t' :> 0"
huffman@29187
   137
    proof (rule Rolle)
huffman@29187
   138
      show "0 < t" by fact
huffman@29187
   139
      show "difg (Suc m') 0 = difg (Suc m') t"
huffman@29187
   140
        using t `Suc m' < n` by (simp add: difg_Suc_eq_0 difg_eq_0)
huffman@29187
   141
      show "\<forall>x. 0 \<le> x \<and> x \<le> t \<longrightarrow> isCont (difg (Suc m')) x"
huffman@29187
   142
        using `t < h` `Suc m' < n` by (simp add: isCont_difg)
hoelzl@56181
   143
      show "\<forall>x. 0 < x \<and> x < t \<longrightarrow> difg (Suc m') differentiable (at x)"
huffman@29187
   144
        using `t < h` `Suc m' < n` by (simp add: differentiable_difg)
huffman@29187
   145
    qed
huffman@29187
   146
    thus ?case
huffman@29187
   147
      using `t < h` by auto
huffman@29187
   148
  qed
huffman@29187
   149
huffman@29187
   150
  then obtain t where "0 < t" "t < h" "DERIV (difg m) t :> 0" by fast
huffman@29187
   151
huffman@29187
   152
  hence "difg (Suc m) t = 0"
huffman@29187
   153
    using `m < n` by (simp add: difg_Suc_eq_0)
huffman@29187
   154
huffman@29187
   155
  show ?thesis
huffman@29187
   156
  proof (intro exI conjI)
huffman@29187
   157
    show "0 < t" by fact
huffman@29187
   158
    show "t < h" by fact
huffman@29187
   159
    show "f h =
hoelzl@56193
   160
      (\<Sum>m<n. diff m 0 / real (fact m) * h ^ m) +
huffman@29187
   161
      diff n t / real (fact n) * h ^ n"
huffman@29187
   162
      using `difg (Suc m) t = 0`
avigad@32047
   163
      by (simp add: m f_h difg_def del: fact_Suc)
huffman@29187
   164
  qed
huffman@29187
   165
qed
paulson@15079
   166
paulson@15079
   167
lemma Maclaurin_objl:
nipkow@25162
   168
  "0 < h & n>0 & diff 0 = f &
nipkow@25134
   169
  (\<forall>m t. m < n & 0 \<le> t & t \<le> h --> DERIV (diff m) t :> diff (Suc m) t)
nipkow@25134
   170
   --> (\<exists>t. 0 < t & t < h &
hoelzl@56193
   171
            f h = (\<Sum>m<n. diff m 0 / real (fact m) * h ^ m) +
nipkow@25134
   172
                  diff n t / real (fact n) * h ^ n)"
paulson@15079
   173
by (blast intro: Maclaurin)
paulson@15079
   174
paulson@15079
   175
paulson@15079
   176
lemma Maclaurin2:
bulwahn@41120
   177
  assumes INIT1: "0 < h " and INIT2: "diff 0 = f"
bulwahn@41120
   178
  and DERIV: "\<forall>m t.
bulwahn@41120
   179
  m < n & 0 \<le> t & t \<le> h --> DERIV (diff m) t :> diff (Suc m) t"
bulwahn@41120
   180
  shows "\<exists>t. 0 < t \<and> t \<le> h \<and> f h =
hoelzl@56193
   181
  (\<Sum>m<n. diff m 0 / real (fact m) * h ^ m) +
bulwahn@41120
   182
  diff n t / real (fact n) * h ^ n"
bulwahn@41120
   183
proof (cases "n")
nipkow@44890
   184
  case 0 with INIT1 INIT2 show ?thesis by fastforce
bulwahn@41120
   185
next
hoelzl@41166
   186
  case Suc
bulwahn@41120
   187
  hence "n > 0" by simp
bulwahn@41120
   188
  from INIT1 this INIT2 DERIV have "\<exists>t>0. t < h \<and>
bulwahn@41120
   189
    f h =
hoelzl@56193
   190
    (\<Sum>m<n. diff m 0 / real (fact m) * h ^ m) + diff n t / real (fact n) * h ^ n"
bulwahn@41120
   191
    by (rule Maclaurin)
nipkow@44890
   192
  thus ?thesis by fastforce
bulwahn@41120
   193
qed
paulson@15079
   194
paulson@15079
   195
lemma Maclaurin2_objl:
paulson@15079
   196
     "0 < h & diff 0 = f &
paulson@15079
   197
       (\<forall>m t.
paulson@15079
   198
          m < n & 0 \<le> t & t \<le> h --> DERIV (diff m) t :> diff (Suc m) t)
paulson@15079
   199
    --> (\<exists>t. 0 < t &
paulson@15079
   200
              t \<le> h &
paulson@15079
   201
              f h =
hoelzl@56193
   202
              (\<Sum>m<n. diff m 0 / real (fact m) * h ^ m) +
paulson@15079
   203
              diff n t / real (fact n) * h ^ n)"
paulson@15079
   204
by (blast intro: Maclaurin2)
paulson@15079
   205
paulson@15079
   206
lemma Maclaurin_minus:
hoelzl@41166
   207
  assumes "h < 0" "0 < n" "diff 0 = f"
hoelzl@41166
   208
  and DERIV: "\<forall>m t. m < n & h \<le> t & t \<le> 0 --> DERIV (diff m) t :> diff (Suc m) t"
bulwahn@41120
   209
  shows "\<exists>t. h < t & t < 0 &
hoelzl@56193
   210
         f h = (\<Sum>m<n. diff m 0 / real (fact m) * h ^ m) +
bulwahn@41120
   211
         diff n t / real (fact n) * h ^ n"
bulwahn@41120
   212
proof -
hoelzl@56381
   213
  txt "Transform @{text ABL'} into @{text derivative_intros} format."
hoelzl@41166
   214
  note DERIV' = DERIV_chain'[OF _ DERIV[rule_format], THEN DERIV_cong]
hoelzl@41166
   215
  from assms
hoelzl@41166
   216
  have "\<exists>t>0. t < - h \<and>
bulwahn@41120
   217
    f (- (- h)) =
hoelzl@56193
   218
    (\<Sum>m<n.
bulwahn@41120
   219
    (- 1) ^ m * diff m (- 0) / real (fact m) * (- h) ^ m) +
hoelzl@41166
   220
    (- 1) ^ n * diff n (- t) / real (fact n) * (- h) ^ n"
hoelzl@56381
   221
    by (intro Maclaurin) (auto intro!: derivative_eq_intros DERIV')
hoelzl@41166
   222
  then guess t ..
bulwahn@41120
   223
  moreover
haftmann@58410
   224
  have "(- 1) ^ n * diff n (- t) * (- h) ^ n / real (fact n) = diff n (- t) * h ^ n / real (fact n)"
bulwahn@41120
   225
    by (auto simp add: power_mult_distrib[symmetric])
bulwahn@41120
   226
  moreover
haftmann@58410
   227
  have "(SUM m<n. (- 1) ^ m * diff m 0 * (- h) ^ m / real (fact m)) = (SUM m<n. diff m 0 * h ^ m / real (fact m))"
haftmann@57418
   228
    by (auto intro: setsum.cong simp add: power_mult_distrib[symmetric])
bulwahn@41120
   229
  ultimately have " h < - t \<and>
bulwahn@41120
   230
    - t < 0 \<and>
bulwahn@41120
   231
    f h =
hoelzl@56193
   232
    (\<Sum>m<n. diff m 0 / real (fact m) * h ^ m) + diff n (- t) / real (fact n) * h ^ n"
bulwahn@41120
   233
    by auto
bulwahn@41120
   234
  thus ?thesis ..
bulwahn@41120
   235
qed
paulson@15079
   236
paulson@15079
   237
lemma Maclaurin_minus_objl:
nipkow@25162
   238
     "(h < 0 & n > 0 & diff 0 = f &
paulson@15079
   239
       (\<forall>m t.
paulson@15079
   240
          m < n & h \<le> t & t \<le> 0 --> DERIV (diff m) t :> diff (Suc m) t))
paulson@15079
   241
    --> (\<exists>t. h < t &
paulson@15079
   242
              t < 0 &
paulson@15079
   243
              f h =
hoelzl@56193
   244
              (\<Sum>m<n. diff m 0 / real (fact m) * h ^ m) +
paulson@15079
   245
              diff n t / real (fact n) * h ^ n)"
paulson@15079
   246
by (blast intro: Maclaurin_minus)
paulson@15079
   247
paulson@15079
   248
paulson@15079
   249
subsection{*More Convenient "Bidirectional" Version.*}
paulson@15079
   250
paulson@15079
   251
(* not good for PVS sin_approx, cos_approx *)
paulson@15079
   252
paulson@15079
   253
lemma Maclaurin_bi_le_lemma [rule_format]:
nipkow@25162
   254
  "n>0 \<longrightarrow>
nipkow@25134
   255
   diff 0 0 =
hoelzl@56193
   256
   (\<Sum>m<n. diff m 0 * 0 ^ m / real (fact m)) +
nipkow@25134
   257
   diff n 0 * 0 ^ n / real (fact n)"
hoelzl@41166
   258
by (induct "n") auto
obua@14738
   259
paulson@15079
   260
lemma Maclaurin_bi_le:
hoelzl@41166
   261
   assumes "diff 0 = f"
bulwahn@41120
   262
   and DERIV : "\<forall>m t. m < n & abs t \<le> abs x --> DERIV (diff m) t :> diff (Suc m) t"
bulwahn@41120
   263
   shows "\<exists>t. abs t \<le> abs x &
paulson@15079
   264
              f x =
hoelzl@56193
   265
              (\<Sum>m<n. diff m 0 / real (fact m) * x ^ m) +
hoelzl@41166
   266
     diff n t / real (fact n) * x ^ n" (is "\<exists>t. _ \<and> f x = ?f x t")
hoelzl@41166
   267
proof cases
hoelzl@41166
   268
  assume "n = 0" with `diff 0 = f` show ?thesis by force
bulwahn@41120
   269
next
hoelzl@41166
   270
  assume "n \<noteq> 0"
hoelzl@41166
   271
  show ?thesis
hoelzl@41166
   272
  proof (cases rule: linorder_cases)
hoelzl@41166
   273
    assume "x = 0" with `n \<noteq> 0` `diff 0 = f` DERIV
lp15@56238
   274
    have "\<bar>0\<bar> \<le> \<bar>x\<bar> \<and> f x = ?f x 0" by (auto simp add: Maclaurin_bi_le_lemma)
hoelzl@41166
   275
    thus ?thesis ..
bulwahn@41120
   276
  next
hoelzl@41166
   277
    assume "x < 0"
hoelzl@41166
   278
    with `n \<noteq> 0` DERIV
hoelzl@41166
   279
    have "\<exists>t>x. t < 0 \<and> diff 0 x = ?f x t" by (intro Maclaurin_minus) auto
hoelzl@41166
   280
    then guess t ..
hoelzl@41166
   281
    with `x < 0` `diff 0 = f` have "\<bar>t\<bar> \<le> \<bar>x\<bar> \<and> f x = ?f x t" by simp
hoelzl@41166
   282
    thus ?thesis ..
hoelzl@41166
   283
  next
hoelzl@41166
   284
    assume "x > 0"
hoelzl@41166
   285
    with `n \<noteq> 0` `diff 0 = f` DERIV
hoelzl@41166
   286
    have "\<exists>t>0. t < x \<and> diff 0 x = ?f x t" by (intro Maclaurin) auto
hoelzl@41166
   287
    then guess t ..
hoelzl@41166
   288
    with `x > 0` `diff 0 = f` have "\<bar>t\<bar> \<le> \<bar>x\<bar> \<and> f x = ?f x t" by simp
hoelzl@41166
   289
    thus ?thesis ..
bulwahn@41120
   290
  qed
bulwahn@41120
   291
qed
bulwahn@41120
   292
paulson@15079
   293
lemma Maclaurin_all_lt:
bulwahn@41120
   294
  assumes INIT1: "diff 0 = f" and INIT2: "0 < n" and INIT3: "x \<noteq> 0"
bulwahn@41120
   295
  and DERIV: "\<forall>m x. DERIV (diff m) x :> diff(Suc m) x"
hoelzl@41166
   296
  shows "\<exists>t. 0 < abs t & abs t < abs x & f x =
hoelzl@56193
   297
    (\<Sum>m<n. (diff m 0 / real (fact m)) * x ^ m) +
hoelzl@41166
   298
                (diff n t / real (fact n)) * x ^ n" (is "\<exists>t. _ \<and> _ \<and> f x = ?f x t")
hoelzl@41166
   299
proof (cases rule: linorder_cases)
hoelzl@41166
   300
  assume "x = 0" with INIT3 show "?thesis"..
hoelzl@41166
   301
next
hoelzl@41166
   302
  assume "x < 0"
hoelzl@41166
   303
  with assms have "\<exists>t>x. t < 0 \<and> f x = ?f x t" by (intro Maclaurin_minus) auto
hoelzl@41166
   304
  then guess t ..
hoelzl@41166
   305
  with `x < 0` have "0 < \<bar>t\<bar> \<and> \<bar>t\<bar> < \<bar>x\<bar> \<and> f x = ?f x t" by simp
hoelzl@41166
   306
  thus ?thesis ..
hoelzl@41166
   307
next
hoelzl@41166
   308
  assume "x > 0"
hoelzl@41166
   309
  with assms have "\<exists>t>0. t < x \<and> f x = ?f x t " by (intro Maclaurin) auto
hoelzl@41166
   310
  then guess t ..
hoelzl@41166
   311
  with `x > 0` have "0 < \<bar>t\<bar> \<and> \<bar>t\<bar> < \<bar>x\<bar> \<and> f x = ?f x t" by simp
hoelzl@41166
   312
  thus ?thesis ..
bulwahn@41120
   313
qed
bulwahn@41120
   314
paulson@15079
   315
paulson@15079
   316
lemma Maclaurin_all_lt_objl:
paulson@15079
   317
     "diff 0 = f &
paulson@15079
   318
      (\<forall>m x. DERIV (diff m) x :> diff(Suc m) x) &
nipkow@25162
   319
      x ~= 0 & n > 0
paulson@15079
   320
      --> (\<exists>t. 0 < abs t & abs t < abs x &
hoelzl@56193
   321
               f x = (\<Sum>m<n. (diff m 0 / real (fact m)) * x ^ m) +
paulson@15079
   322
                     (diff n t / real (fact n)) * x ^ n)"
paulson@15079
   323
by (blast intro: Maclaurin_all_lt)
paulson@15079
   324
paulson@15079
   325
lemma Maclaurin_zero [rule_format]:
paulson@15079
   326
     "x = (0::real)
nipkow@25134
   327
      ==> n \<noteq> 0 -->
hoelzl@56193
   328
          (\<Sum>m<n. (diff m (0::real) / real (fact m)) * x ^ m) =
paulson@15079
   329
          diff 0 0"
paulson@15079
   330
by (induct n, auto)
paulson@15079
   331
bulwahn@41120
   332
bulwahn@41120
   333
lemma Maclaurin_all_le:
bulwahn@41120
   334
  assumes INIT: "diff 0 = f"
bulwahn@41120
   335
  and DERIV: "\<forall>m x. DERIV (diff m) x :> diff (Suc m) x"
hoelzl@41166
   336
  shows "\<exists>t. abs t \<le> abs x & f x =
hoelzl@56193
   337
    (\<Sum>m<n. (diff m 0 / real (fact m)) * x ^ m) +
hoelzl@41166
   338
    (diff n t / real (fact n)) * x ^ n" (is "\<exists>t. _ \<and> f x = ?f x t")
hoelzl@41166
   339
proof cases
hoelzl@41166
   340
  assume "n = 0" with INIT show ?thesis by force
bulwahn@41120
   341
  next
hoelzl@41166
   342
  assume "n \<noteq> 0"
hoelzl@41166
   343
  show ?thesis
hoelzl@41166
   344
  proof cases
hoelzl@41166
   345
    assume "x = 0"
hoelzl@56193
   346
    with `n \<noteq> 0` have "(\<Sum>m<n. diff m 0 / real (fact m) * x ^ m) = diff 0 0"
hoelzl@41166
   347
      by (intro Maclaurin_zero) auto
hoelzl@41166
   348
    with INIT `x = 0` `n \<noteq> 0` have " \<bar>0\<bar> \<le> \<bar>x\<bar> \<and> f x = ?f x 0" by force
hoelzl@41166
   349
    thus ?thesis ..
hoelzl@41166
   350
  next
hoelzl@41166
   351
    assume "x \<noteq> 0"
hoelzl@41166
   352
    with INIT `n \<noteq> 0` DERIV have "\<exists>t. 0 < \<bar>t\<bar> \<and> \<bar>t\<bar> < \<bar>x\<bar> \<and> f x = ?f x t"
hoelzl@41166
   353
      by (intro Maclaurin_all_lt) auto
hoelzl@41166
   354
    then guess t ..
hoelzl@41166
   355
    hence "\<bar>t\<bar> \<le> \<bar>x\<bar> \<and> f x = ?f x t" by simp
hoelzl@41166
   356
    thus ?thesis ..
bulwahn@41120
   357
  qed
bulwahn@41120
   358
qed
bulwahn@41120
   359
paulson@15079
   360
lemma Maclaurin_all_le_objl: "diff 0 = f &
paulson@15079
   361
      (\<forall>m x. DERIV (diff m) x :> diff (Suc m) x)
paulson@15079
   362
      --> (\<exists>t. abs t \<le> abs x &
hoelzl@56193
   363
              f x = (\<Sum>m<n. (diff m 0 / real (fact m)) * x ^ m) +
paulson@15079
   364
                    (diff n t / real (fact n)) * x ^ n)"
paulson@15079
   365
by (blast intro: Maclaurin_all_le)
paulson@15079
   366
paulson@15079
   367
paulson@15079
   368
subsection{*Version for Exponential Function*}
paulson@15079
   369
nipkow@25162
   370
lemma Maclaurin_exp_lt: "[| x ~= 0; n > 0 |]
paulson@15079
   371
      ==> (\<exists>t. 0 < abs t &
paulson@15079
   372
                abs t < abs x &
hoelzl@56193
   373
                exp x = (\<Sum>m<n. (x ^ m) / real (fact m)) +
paulson@15079
   374
                        (exp t / real (fact n)) * x ^ n)"
paulson@15079
   375
by (cut_tac diff = "%n. exp" and f = exp and x = x and n = n in Maclaurin_all_lt_objl, auto)
paulson@15079
   376
paulson@15079
   377
paulson@15079
   378
lemma Maclaurin_exp_le:
paulson@15079
   379
     "\<exists>t. abs t \<le> abs x &
hoelzl@56193
   380
            exp x = (\<Sum>m<n. (x ^ m) / real (fact m)) +
paulson@15079
   381
                       (exp t / real (fact n)) * x ^ n"
paulson@15079
   382
by (cut_tac diff = "%n. exp" and f = exp and x = x and n = n in Maclaurin_all_le_objl, auto)
paulson@15079
   383
paulson@15079
   384
paulson@15079
   385
subsection{*Version for Sine Function*}
paulson@15079
   386
paulson@15079
   387
lemma mod_exhaust_less_4:
nipkow@25134
   388
  "m mod 4 = 0 | m mod 4 = 1 | m mod 4 = 2 | m mod 4 = (3::nat)"
webertj@20217
   389
by auto
paulson@15079
   390
paulson@15079
   391
lemma Suc_Suc_mult_two_diff_two [rule_format, simp]:
nipkow@25134
   392
  "n\<noteq>0 --> Suc (Suc (2 * n - 2)) = 2*n"
paulson@15251
   393
by (induct "n", auto)
paulson@15079
   394
paulson@15079
   395
lemma lemma_Suc_Suc_4n_diff_2 [rule_format, simp]:
nipkow@25134
   396
  "n\<noteq>0 --> Suc (Suc (4*n - 2)) = 4*n"
paulson@15251
   397
by (induct "n", auto)
paulson@15079
   398
paulson@15079
   399
lemma Suc_mult_two_diff_one [rule_format, simp]:
nipkow@25134
   400
  "n\<noteq>0 --> Suc (2 * n - 1) = 2*n"
paulson@15251
   401
by (induct "n", auto)
paulson@15079
   402
paulson@15234
   403
paulson@15234
   404
text{*It is unclear why so many variant results are needed.*}
paulson@15079
   405
huffman@36974
   406
lemma sin_expansion_lemma:
hoelzl@41166
   407
     "sin (x + real (Suc m) * pi / 2) =
huffman@36974
   408
      cos (x + real (m) * pi / 2)"
webertj@49962
   409
by (simp only: cos_add sin_add real_of_nat_Suc add_divide_distrib distrib_right, auto)
huffman@36974
   410
paulson@15079
   411
lemma Maclaurin_sin_expansion2:
paulson@15079
   412
     "\<exists>t. abs t \<le> abs x &
paulson@15079
   413
       sin x =
hoelzl@56193
   414
       (\<Sum>m<n. sin_coeff m * x ^ m)
paulson@15079
   415
      + ((sin(t + 1/2 * real (n) *pi) / real (fact n)) * x ^ n)"
paulson@15079
   416
apply (cut_tac f = sin and n = n and x = x
paulson@15079
   417
        and diff = "%n x. sin (x + 1/2*real n * pi)" in Maclaurin_all_lt_objl)
paulson@15079
   418
apply safe
paulson@15079
   419
apply (simp (no_asm))
huffman@36974
   420
apply (simp (no_asm) add: sin_expansion_lemma)
hoelzl@56381
   421
apply (force intro!: derivative_eq_intros)
thomas@57492
   422
apply (subst (asm) setsum.neutral, auto)[1]
paulson@15079
   423
apply (rule ccontr, simp)
paulson@15079
   424
apply (drule_tac x = x in spec, simp)
paulson@15079
   425
apply (erule ssubst)
paulson@15079
   426
apply (rule_tac x = t in exI, simp)
haftmann@57418
   427
apply (rule setsum.cong[OF refl])
huffman@44306
   428
apply (auto simp add: sin_coeff_def sin_zero_iff odd_Suc_mult_two_ex)
paulson@15079
   429
done
paulson@15079
   430
paulson@15234
   431
lemma Maclaurin_sin_expansion:
paulson@15234
   432
     "\<exists>t. sin x =
hoelzl@56193
   433
       (\<Sum>m<n. sin_coeff m * x ^ m)
paulson@15234
   434
      + ((sin(t + 1/2 * real (n) *pi) / real (fact n)) * x ^ n)"
hoelzl@41166
   435
apply (insert Maclaurin_sin_expansion2 [of x n])
hoelzl@41166
   436
apply (blast intro: elim:)
paulson@15234
   437
done
paulson@15234
   438
paulson@15079
   439
lemma Maclaurin_sin_expansion3:
nipkow@25162
   440
     "[| n > 0; 0 < x |] ==>
paulson@15079
   441
       \<exists>t. 0 < t & t < x &
paulson@15079
   442
       sin x =
hoelzl@56193
   443
       (\<Sum>m<n. sin_coeff m * x ^ m)
paulson@15079
   444
      + ((sin(t + 1/2 * real(n) *pi) / real (fact n)) * x ^ n)"
paulson@15079
   445
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
   446
apply safe
paulson@15079
   447
apply simp
huffman@36974
   448
apply (simp (no_asm) add: sin_expansion_lemma)
hoelzl@56381
   449
apply (force intro!: derivative_eq_intros)
paulson@15079
   450
apply (erule ssubst)
paulson@15079
   451
apply (rule_tac x = t in exI, simp)
haftmann@57418
   452
apply (rule setsum.cong[OF refl])
huffman@44306
   453
apply (auto simp add: sin_coeff_def sin_zero_iff odd_Suc_mult_two_ex)
paulson@15079
   454
done
paulson@15079
   455
paulson@15079
   456
lemma Maclaurin_sin_expansion4:
paulson@15079
   457
     "0 < x ==>
paulson@15079
   458
       \<exists>t. 0 < t & t \<le> x &
paulson@15079
   459
       sin x =
hoelzl@56193
   460
       (\<Sum>m<n. sin_coeff m * x ^ m)
paulson@15079
   461
      + ((sin(t + 1/2 * real (n) *pi) / real (fact n)) * x ^ n)"
paulson@15079
   462
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
   463
apply safe
paulson@15079
   464
apply simp
huffman@36974
   465
apply (simp (no_asm) add: sin_expansion_lemma)
hoelzl@56381
   466
apply (force intro!: derivative_eq_intros)
paulson@15079
   467
apply (erule ssubst)
paulson@15079
   468
apply (rule_tac x = t in exI, simp)
haftmann@57418
   469
apply (rule setsum.cong[OF refl])
huffman@44306
   470
apply (auto simp add: sin_coeff_def sin_zero_iff odd_Suc_mult_two_ex)
paulson@15079
   471
done
paulson@15079
   472
paulson@15079
   473
paulson@15079
   474
subsection{*Maclaurin Expansion for Cosine Function*}
paulson@15079
   475
paulson@15079
   476
lemma sumr_cos_zero_one [simp]:
hoelzl@56193
   477
  "(\<Sum>m<(Suc n). cos_coeff m * 0 ^ m) = 1"
paulson@15251
   478
by (induct "n", auto)
paulson@15079
   479
huffman@36974
   480
lemma cos_expansion_lemma:
huffman@36974
   481
  "cos (x + real(Suc m) * pi / 2) = -sin (x + real m * pi / 2)"
webertj@49962
   482
by (simp only: cos_add sin_add real_of_nat_Suc distrib_right add_divide_distrib, auto)
huffman@36974
   483
paulson@15079
   484
lemma Maclaurin_cos_expansion:
paulson@15079
   485
     "\<exists>t. abs t \<le> abs x &
paulson@15079
   486
       cos x =
hoelzl@56193
   487
       (\<Sum>m<n. cos_coeff m * x ^ m)
paulson@15079
   488
      + ((cos(t + 1/2 * real (n) *pi) / real (fact n)) * x ^ n)"
paulson@15079
   489
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
   490
apply safe
paulson@15079
   491
apply (simp (no_asm))
huffman@36974
   492
apply (simp (no_asm) add: cos_expansion_lemma)
paulson@15079
   493
apply (case_tac "n", simp)
hoelzl@56193
   494
apply (simp del: setsum_lessThan_Suc)
paulson@15079
   495
apply (rule ccontr, simp)
paulson@15079
   496
apply (drule_tac x = x in spec, simp)
paulson@15079
   497
apply (erule ssubst)
paulson@15079
   498
apply (rule_tac x = t in exI, simp)
haftmann@57418
   499
apply (rule setsum.cong[OF refl])
huffman@44306
   500
apply (auto simp add: cos_coeff_def cos_zero_iff even_mult_two_ex)
paulson@15079
   501
done
paulson@15079
   502
paulson@15079
   503
lemma Maclaurin_cos_expansion2:
nipkow@25162
   504
     "[| 0 < x; n > 0 |] ==>
paulson@15079
   505
       \<exists>t. 0 < t & t < x &
paulson@15079
   506
       cos x =
hoelzl@56193
   507
       (\<Sum>m<n. cos_coeff m * x ^ m)
paulson@15079
   508
      + ((cos(t + 1/2 * real (n) *pi) / real (fact n)) * x ^ n)"
paulson@15079
   509
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
   510
apply safe
paulson@15079
   511
apply simp
huffman@36974
   512
apply (simp (no_asm) add: cos_expansion_lemma)
paulson@15079
   513
apply (erule ssubst)
paulson@15079
   514
apply (rule_tac x = t in exI, simp)
haftmann@57418
   515
apply (rule setsum.cong[OF refl])
huffman@44306
   516
apply (auto simp add: cos_coeff_def cos_zero_iff even_mult_two_ex)
paulson@15079
   517
done
paulson@15079
   518
paulson@15234
   519
lemma Maclaurin_minus_cos_expansion:
nipkow@25162
   520
     "[| x < 0; n > 0 |] ==>
paulson@15079
   521
       \<exists>t. x < t & t < 0 &
paulson@15079
   522
       cos x =
hoelzl@56193
   523
       (\<Sum>m<n. cos_coeff m * x ^ m)
paulson@15079
   524
      + ((cos(t + 1/2 * real (n) *pi) / real (fact n)) * x ^ n)"
paulson@15079
   525
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
   526
apply safe
paulson@15079
   527
apply simp
huffman@36974
   528
apply (simp (no_asm) add: cos_expansion_lemma)
paulson@15079
   529
apply (erule ssubst)
paulson@15079
   530
apply (rule_tac x = t in exI, simp)
haftmann@57418
   531
apply (rule setsum.cong[OF refl])
huffman@44306
   532
apply (auto simp add: cos_coeff_def cos_zero_iff even_mult_two_ex)
paulson@15079
   533
done
paulson@15079
   534
paulson@15079
   535
(* ------------------------------------------------------------------------- *)
paulson@15079
   536
(* Version for ln(1 +/- x). Where is it??                                    *)
paulson@15079
   537
(* ------------------------------------------------------------------------- *)
paulson@15079
   538
paulson@15079
   539
lemma sin_bound_lemma:
paulson@15081
   540
    "[|x = y; abs u \<le> (v::real) |] ==> \<bar>(x + u) - y\<bar> \<le> v"
paulson@15079
   541
by auto
paulson@15079
   542
paulson@15079
   543
lemma Maclaurin_sin_bound:
hoelzl@56193
   544
  "abs(sin x - (\<Sum>m<n. sin_coeff m * x ^ m))
huffman@44306
   545
  \<le> inverse(real (fact n)) * \<bar>x\<bar> ^ n"
obua@14738
   546
proof -
paulson@15079
   547
  have "!! x (y::real). x \<le> 1 \<Longrightarrow> 0 \<le> y \<Longrightarrow> x * y \<le> 1 * y"
obua@14738
   548
    by (rule_tac mult_right_mono,simp_all)
obua@14738
   549
  note est = this[simplified]
huffman@22985
   550
  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
   551
  have diff_0: "?diff 0 = sin" by simp
huffman@22985
   552
  have DERIV_diff: "\<forall>m x. DERIV (?diff m) x :> ?diff (Suc m) x"
huffman@22985
   553
    apply (clarify)
huffman@22985
   554
    apply (subst (1 2 3) mod_Suc_eq_Suc_mod)
huffman@22985
   555
    apply (cut_tac m=m in mod_exhaust_less_4)
hoelzl@56381
   556
    apply (safe, auto intro!: derivative_eq_intros)
huffman@22985
   557
    done
huffman@22985
   558
  from Maclaurin_all_le [OF diff_0 DERIV_diff]
huffman@22985
   559
  obtain t where t1: "\<bar>t\<bar> \<le> \<bar>x\<bar>" and
hoelzl@56193
   560
    t2: "sin x = (\<Sum>m<n. ?diff m 0 / real (fact m) * x ^ m) +
huffman@22985
   561
      ?diff n t / real (fact n) * x ^ n" by fast
huffman@22985
   562
  have diff_m_0:
huffman@22985
   563
    "\<And>m. ?diff m 0 = (if even m then 0
haftmann@58410
   564
         else (- 1) ^ ((m - Suc 0) div 2))"
huffman@22985
   565
    apply (subst even_even_mod_4_iff)
huffman@22985
   566
    apply (cut_tac m=m in mod_exhaust_less_4)
huffman@22985
   567
    apply (elim disjE, simp_all)
huffman@22985
   568
    apply (safe dest!: mod_eqD, simp_all)
huffman@22985
   569
    done
obua@14738
   570
  show ?thesis
huffman@44306
   571
    unfolding sin_coeff_def
huffman@22985
   572
    apply (subst t2)
paulson@15079
   573
    apply (rule sin_bound_lemma)
haftmann@57418
   574
    apply (rule setsum.cong[OF refl])
huffman@22985
   575
    apply (subst diff_m_0, simp)
paulson@15079
   576
    apply (auto intro: mult_right_mono [where b=1, simplified] mult_right_mono
haftmann@57514
   577
                simp add: est ac_simps divide_inverse power_abs [symmetric] abs_mult)
obua@14738
   578
    done
obua@14738
   579
qed
obua@14738
   580
paulson@15079
   581
end