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