src/HOL/MacLaurin.thy
author paulson <lp15@cam.ac.uk>
Fri Mar 21 15:36:00 2014 +0000 (2014-03-21)
changeset 56238 5d147e1e18d1
parent 56193 c726ecfb22b6
child 56381 0556204bc230
permissions -rw-r--r--
a few new lemmas and generalisations of old ones
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@41166
    43
    by (auto intro!: DERIV_intros DERIV[rule_format, OF INIT2])
hoelzl@56193
    44
  moreover
hoelzl@41166
    45
  from INIT2 have intvl: "{..<n - m} = insert 0 (Suc ` {..<n - Suc m})" and "0 < n - m"
hoelzl@41166
    46
    unfolding atLeast0LessThan[symmetric] by auto
hoelzl@56193
    47
  have "(\<Sum>x<n - m. real x * t ^ (x - Suc 0) * diff (m + x) 0 / real (fact x)) =
hoelzl@56193
    48
      (\<Sum>x<n - Suc m. real (Suc x) * t ^ x * diff (Suc m + x) 0 / real (fact (Suc x)))"
hoelzl@41166
    49
    unfolding intvl atLeast0LessThan by (subst setsum.insert) (auto simp: setsum.reindex)
hoelzl@41166
    50
  moreover
hoelzl@41166
    51
  have fact_neq_0: "\<And>x::nat. real (fact x) + real x * real (fact x) \<noteq> 0"
hoelzl@41166
    52
    by (metis fact_gt_zero_nat not_add_less1 real_of_nat_add real_of_nat_mult real_of_nat_zero_iff)
hoelzl@41166
    53
  have "\<And>x. real (Suc x) * t ^ x * diff (Suc m + x) 0 / real (fact (Suc x)) =
hoelzl@41166
    54
      diff (Suc m + x) 0 * t^x / real (fact x)"
hoelzl@41166
    55
    by (auto simp: field_simps real_of_nat_Suc fact_neq_0 intro!: nonzero_divide_eq_eq[THEN iffD2])
hoelzl@41166
    56
  moreover
hoelzl@41166
    57
  have "real (n - m) * t ^ (n - Suc m) * B / real (fact (n - m)) =
hoelzl@41166
    58
      B * (t ^ (n - Suc m) / real (fact (n - Suc m)))"
hoelzl@41166
    59
    using `0 < n - m` by (simp add: fact_reduce_nat)
hoelzl@41166
    60
  ultimately show "DERIV (difg m) t :> difg (Suc m) t"
hoelzl@41166
    61
    unfolding difg_def by simp
bulwahn@41120
    62
qed
avigad@32038
    63
paulson@15079
    64
lemma Maclaurin:
huffman@29187
    65
  assumes h: "0 < h"
huffman@29187
    66
  assumes n: "0 < n"
huffman@29187
    67
  assumes diff_0: "diff 0 = f"
huffman@29187
    68
  assumes diff_Suc:
huffman@29187
    69
    "\<forall>m t. m < n & 0 \<le> t & t \<le> h --> DERIV (diff m) t :> diff (Suc m) t"
huffman@29187
    70
  shows
huffman@29187
    71
    "\<exists>t. 0 < t & t < h &
paulson@15079
    72
              f h =
hoelzl@56193
    73
              setsum (%m. (diff m 0 / real (fact m)) * h ^ m) {..<n} +
hoelzl@41166
    74
              (diff n t / real (fact n)) * h ^ n"
huffman@29187
    75
proof -
huffman@29187
    76
  from n obtain m where m: "n = Suc m"
hoelzl@41166
    77
    by (cases n) (simp add: n)
huffman@29187
    78
huffman@29187
    79
  obtain B where f_h: "f h =
hoelzl@56193
    80
        (\<Sum>m<n. diff m (0\<Colon>real) / real (fact m) * h ^ m) +
huffman@29187
    81
        B * (h ^ n / real (fact n))"
huffman@29187
    82
    using Maclaurin_lemma [OF h] ..
huffman@29187
    83
hoelzl@41166
    84
  def g \<equiv> "(\<lambda>t. f t -
hoelzl@56193
    85
    (setsum (\<lambda>m. (diff m 0 / real(fact m)) * t^m) {..<n}
hoelzl@41166
    86
      + (B * (t^n / real(fact n)))))"
huffman@29187
    87
huffman@29187
    88
  have g2: "g 0 = 0 & g h = 0"
hoelzl@56193
    89
    by (simp add: m f_h g_def lessThan_Suc_eq_insert_0 image_iff diff_0 setsum_reindex)
huffman@29187
    90
hoelzl@41166
    91
  def difg \<equiv> "(%m t. diff m t -
hoelzl@56193
    92
    (setsum (%p. (diff (m + p) 0 / real (fact p)) * (t ^ p)) {..<n-m}
hoelzl@41166
    93
      + (B * ((t ^ (n - m)) / real (fact (n - m))))))"
huffman@29187
    94
huffman@29187
    95
  have difg_0: "difg 0 = g"
huffman@29187
    96
    unfolding difg_def g_def by (simp add: diff_0)
huffman@29187
    97
huffman@29187
    98
  have difg_Suc: "\<forall>(m\<Colon>nat) t\<Colon>real.
huffman@29187
    99
        m < n \<and> (0\<Colon>real) \<le> t \<and> t \<le> h \<longrightarrow> DERIV (difg m) t :> difg (Suc m) t"
hoelzl@41166
   100
    using diff_Suc m unfolding difg_def by (rule Maclaurin_lemma2)
huffman@29187
   101
hoelzl@56193
   102
  have difg_eq_0: "\<forall>m<n. difg m 0 = 0"
hoelzl@56193
   103
    by (auto simp: difg_def m Suc_diff_le lessThan_Suc_eq_insert_0 image_iff setsum_reindex)
huffman@29187
   104
huffman@29187
   105
  have isCont_difg: "\<And>m x. \<lbrakk>m < n; 0 \<le> x; x \<le> h\<rbrakk> \<Longrightarrow> isCont (difg m) x"
huffman@29187
   106
    by (rule DERIV_isCont [OF difg_Suc [rule_format]]) simp
huffman@29187
   107
huffman@29187
   108
  have differentiable_difg:
hoelzl@56181
   109
    "\<And>m x. \<lbrakk>m < n; 0 \<le> x; x \<le> h\<rbrakk> \<Longrightarrow> difg m differentiable (at x)"
huffman@29187
   110
    by (rule differentiableI [OF difg_Suc [rule_format]]) simp
huffman@29187
   111
huffman@29187
   112
  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
   113
        \<Longrightarrow> difg (Suc m) t = 0"
huffman@29187
   114
    by (rule DERIV_unique [OF difg_Suc [rule_format]]) simp
huffman@29187
   115
huffman@29187
   116
  have "m < n" using m by simp
huffman@29187
   117
huffman@29187
   118
  have "\<exists>t. 0 < t \<and> t < h \<and> DERIV (difg m) t :> 0"
huffman@29187
   119
  using `m < n`
huffman@29187
   120
  proof (induct m)
hoelzl@41166
   121
    case 0
huffman@29187
   122
    show ?case
huffman@29187
   123
    proof (rule Rolle)
huffman@29187
   124
      show "0 < h" by fact
huffman@29187
   125
      show "difg 0 0 = difg 0 h" by (simp add: difg_0 g2)
huffman@29187
   126
      show "\<forall>x. 0 \<le> x \<and> x \<le> h \<longrightarrow> isCont (difg (0\<Colon>nat)) x"
huffman@29187
   127
        by (simp add: isCont_difg n)
hoelzl@56181
   128
      show "\<forall>x. 0 < x \<and> x < h \<longrightarrow> difg (0\<Colon>nat) differentiable (at x)"
huffman@29187
   129
        by (simp add: differentiable_difg n)
huffman@29187
   130
    qed
huffman@29187
   131
  next
hoelzl@41166
   132
    case (Suc m')
huffman@29187
   133
    hence "\<exists>t. 0 < t \<and> t < h \<and> DERIV (difg m') t :> 0" by simp
huffman@29187
   134
    then obtain t where t: "0 < t" "t < h" "DERIV (difg m') t :> 0" by fast
huffman@29187
   135
    have "\<exists>t'. 0 < t' \<and> t' < t \<and> DERIV (difg (Suc m')) t' :> 0"
huffman@29187
   136
    proof (rule Rolle)
huffman@29187
   137
      show "0 < t" by fact
huffman@29187
   138
      show "difg (Suc m') 0 = difg (Suc m') t"
huffman@29187
   139
        using t `Suc m' < n` by (simp add: difg_Suc_eq_0 difg_eq_0)
huffman@29187
   140
      show "\<forall>x. 0 \<le> x \<and> x \<le> t \<longrightarrow> isCont (difg (Suc m')) x"
huffman@29187
   141
        using `t < h` `Suc m' < n` by (simp add: isCont_difg)
hoelzl@56181
   142
      show "\<forall>x. 0 < x \<and> x < t \<longrightarrow> difg (Suc m') differentiable (at x)"
huffman@29187
   143
        using `t < h` `Suc m' < n` by (simp add: differentiable_difg)
huffman@29187
   144
    qed
huffman@29187
   145
    thus ?case
huffman@29187
   146
      using `t < h` by auto
huffman@29187
   147
  qed
huffman@29187
   148
huffman@29187
   149
  then obtain t where "0 < t" "t < h" "DERIV (difg m) t :> 0" by fast
huffman@29187
   150
huffman@29187
   151
  hence "difg (Suc m) t = 0"
huffman@29187
   152
    using `m < n` by (simp add: difg_Suc_eq_0)
huffman@29187
   153
huffman@29187
   154
  show ?thesis
huffman@29187
   155
  proof (intro exI conjI)
huffman@29187
   156
    show "0 < t" by fact
huffman@29187
   157
    show "t < h" by fact
huffman@29187
   158
    show "f h =
hoelzl@56193
   159
      (\<Sum>m<n. diff m 0 / real (fact m) * h ^ m) +
huffman@29187
   160
      diff n t / real (fact n) * h ^ n"
huffman@29187
   161
      using `difg (Suc m) t = 0`
avigad@32047
   162
      by (simp add: m f_h difg_def del: fact_Suc)
huffman@29187
   163
  qed
huffman@29187
   164
qed
paulson@15079
   165
paulson@15079
   166
lemma Maclaurin_objl:
nipkow@25162
   167
  "0 < h & n>0 & diff 0 = f &
nipkow@25134
   168
  (\<forall>m t. m < n & 0 \<le> t & t \<le> h --> DERIV (diff m) t :> diff (Suc m) t)
nipkow@25134
   169
   --> (\<exists>t. 0 < t & t < h &
hoelzl@56193
   170
            f h = (\<Sum>m<n. diff m 0 / real (fact m) * h ^ m) +
nipkow@25134
   171
                  diff n t / real (fact n) * h ^ n)"
paulson@15079
   172
by (blast intro: Maclaurin)
paulson@15079
   173
paulson@15079
   174
paulson@15079
   175
lemma Maclaurin2:
bulwahn@41120
   176
  assumes INIT1: "0 < h " and INIT2: "diff 0 = f"
bulwahn@41120
   177
  and DERIV: "\<forall>m t.
bulwahn@41120
   178
  m < n & 0 \<le> t & t \<le> h --> DERIV (diff m) t :> diff (Suc m) t"
bulwahn@41120
   179
  shows "\<exists>t. 0 < t \<and> t \<le> h \<and> f h =
hoelzl@56193
   180
  (\<Sum>m<n. diff m 0 / real (fact m) * h ^ m) +
bulwahn@41120
   181
  diff n t / real (fact n) * h ^ n"
bulwahn@41120
   182
proof (cases "n")
nipkow@44890
   183
  case 0 with INIT1 INIT2 show ?thesis by fastforce
bulwahn@41120
   184
next
hoelzl@41166
   185
  case Suc
bulwahn@41120
   186
  hence "n > 0" by simp
bulwahn@41120
   187
  from INIT1 this INIT2 DERIV have "\<exists>t>0. t < h \<and>
bulwahn@41120
   188
    f h =
hoelzl@56193
   189
    (\<Sum>m<n. diff m 0 / real (fact m) * h ^ m) + diff n t / real (fact n) * h ^ n"
bulwahn@41120
   190
    by (rule Maclaurin)
nipkow@44890
   191
  thus ?thesis by fastforce
bulwahn@41120
   192
qed
paulson@15079
   193
paulson@15079
   194
lemma Maclaurin2_objl:
paulson@15079
   195
     "0 < h & diff 0 = f &
paulson@15079
   196
       (\<forall>m t.
paulson@15079
   197
          m < n & 0 \<le> t & t \<le> h --> DERIV (diff m) t :> diff (Suc m) t)
paulson@15079
   198
    --> (\<exists>t. 0 < t &
paulson@15079
   199
              t \<le> h &
paulson@15079
   200
              f h =
hoelzl@56193
   201
              (\<Sum>m<n. diff m 0 / real (fact m) * h ^ m) +
paulson@15079
   202
              diff n t / real (fact n) * h ^ n)"
paulson@15079
   203
by (blast intro: Maclaurin2)
paulson@15079
   204
paulson@15079
   205
lemma Maclaurin_minus:
hoelzl@41166
   206
  assumes "h < 0" "0 < n" "diff 0 = f"
hoelzl@41166
   207
  and DERIV: "\<forall>m t. m < n & h \<le> t & t \<le> 0 --> DERIV (diff m) t :> diff (Suc m) t"
bulwahn@41120
   208
  shows "\<exists>t. h < t & t < 0 &
hoelzl@56193
   209
         f h = (\<Sum>m<n. diff m 0 / real (fact m) * h ^ m) +
bulwahn@41120
   210
         diff n t / real (fact n) * h ^ n"
bulwahn@41120
   211
proof -
hoelzl@41166
   212
  txt "Transform @{text ABL'} into @{text DERIV_intros} format."
hoelzl@41166
   213
  note DERIV' = DERIV_chain'[OF _ DERIV[rule_format], THEN DERIV_cong]
hoelzl@41166
   214
  from assms
hoelzl@41166
   215
  have "\<exists>t>0. t < - h \<and>
bulwahn@41120
   216
    f (- (- h)) =
hoelzl@56193
   217
    (\<Sum>m<n.
bulwahn@41120
   218
    (- 1) ^ m * diff m (- 0) / real (fact m) * (- h) ^ m) +
hoelzl@41166
   219
    (- 1) ^ n * diff n (- t) / real (fact n) * (- h) ^ n"
hoelzl@41166
   220
    by (intro Maclaurin) (auto intro!: DERIV_intros DERIV')
hoelzl@41166
   221
  then guess t ..
bulwahn@41120
   222
  moreover
bulwahn@41120
   223
  have "-1 ^ n * diff n (- t) * (- h) ^ n / real (fact n) = diff n (- t) * h ^ n / real (fact n)"
bulwahn@41120
   224
    by (auto simp add: power_mult_distrib[symmetric])
bulwahn@41120
   225
  moreover
hoelzl@56193
   226
  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))"
bulwahn@41120
   227
    by (auto intro: setsum_cong simp add: power_mult_distrib[symmetric])
bulwahn@41120
   228
  ultimately have " h < - t \<and>
bulwahn@41120
   229
    - t < 0 \<and>
bulwahn@41120
   230
    f h =
hoelzl@56193
   231
    (\<Sum>m<n. diff m 0 / real (fact m) * h ^ m) + diff n (- t) / real (fact n) * h ^ n"
bulwahn@41120
   232
    by auto
bulwahn@41120
   233
  thus ?thesis ..
bulwahn@41120
   234
qed
paulson@15079
   235
paulson@15079
   236
lemma Maclaurin_minus_objl:
nipkow@25162
   237
     "(h < 0 & n > 0 & diff 0 = f &
paulson@15079
   238
       (\<forall>m t.
paulson@15079
   239
          m < n & h \<le> t & t \<le> 0 --> DERIV (diff m) t :> diff (Suc m) t))
paulson@15079
   240
    --> (\<exists>t. h < t &
paulson@15079
   241
              t < 0 &
paulson@15079
   242
              f h =
hoelzl@56193
   243
              (\<Sum>m<n. diff m 0 / real (fact m) * h ^ m) +
paulson@15079
   244
              diff n t / real (fact n) * h ^ n)"
paulson@15079
   245
by (blast intro: Maclaurin_minus)
paulson@15079
   246
paulson@15079
   247
paulson@15079
   248
subsection{*More Convenient "Bidirectional" Version.*}
paulson@15079
   249
paulson@15079
   250
(* not good for PVS sin_approx, cos_approx *)
paulson@15079
   251
paulson@15079
   252
lemma Maclaurin_bi_le_lemma [rule_format]:
nipkow@25162
   253
  "n>0 \<longrightarrow>
nipkow@25134
   254
   diff 0 0 =
hoelzl@56193
   255
   (\<Sum>m<n. diff m 0 * 0 ^ m / real (fact m)) +
nipkow@25134
   256
   diff n 0 * 0 ^ n / real (fact n)"
hoelzl@41166
   257
by (induct "n") auto
obua@14738
   258
paulson@15079
   259
lemma Maclaurin_bi_le:
hoelzl@41166
   260
   assumes "diff 0 = f"
bulwahn@41120
   261
   and DERIV : "\<forall>m t. m < n & abs t \<le> abs x --> DERIV (diff m) t :> diff (Suc m) t"
bulwahn@41120
   262
   shows "\<exists>t. abs t \<le> abs x &
paulson@15079
   263
              f x =
hoelzl@56193
   264
              (\<Sum>m<n. diff m 0 / real (fact m) * x ^ m) +
hoelzl@41166
   265
     diff n t / real (fact n) * x ^ n" (is "\<exists>t. _ \<and> f x = ?f x t")
hoelzl@41166
   266
proof cases
hoelzl@41166
   267
  assume "n = 0" with `diff 0 = f` show ?thesis by force
bulwahn@41120
   268
next
hoelzl@41166
   269
  assume "n \<noteq> 0"
hoelzl@41166
   270
  show ?thesis
hoelzl@41166
   271
  proof (cases rule: linorder_cases)
hoelzl@41166
   272
    assume "x = 0" with `n \<noteq> 0` `diff 0 = f` DERIV
lp15@56238
   273
    have "\<bar>0\<bar> \<le> \<bar>x\<bar> \<and> f x = ?f x 0" by (auto simp add: Maclaurin_bi_le_lemma)
hoelzl@41166
   274
    thus ?thesis ..
bulwahn@41120
   275
  next
hoelzl@41166
   276
    assume "x < 0"
hoelzl@41166
   277
    with `n \<noteq> 0` DERIV
hoelzl@41166
   278
    have "\<exists>t>x. t < 0 \<and> diff 0 x = ?f x t" by (intro Maclaurin_minus) auto
hoelzl@41166
   279
    then guess t ..
hoelzl@41166
   280
    with `x < 0` `diff 0 = f` have "\<bar>t\<bar> \<le> \<bar>x\<bar> \<and> f x = ?f x t" by simp
hoelzl@41166
   281
    thus ?thesis ..
hoelzl@41166
   282
  next
hoelzl@41166
   283
    assume "x > 0"
hoelzl@41166
   284
    with `n \<noteq> 0` `diff 0 = f` DERIV
hoelzl@41166
   285
    have "\<exists>t>0. t < x \<and> diff 0 x = ?f x t" by (intro Maclaurin) auto
hoelzl@41166
   286
    then guess t ..
hoelzl@41166
   287
    with `x > 0` `diff 0 = f` have "\<bar>t\<bar> \<le> \<bar>x\<bar> \<and> f x = ?f x t" by simp
hoelzl@41166
   288
    thus ?thesis ..
bulwahn@41120
   289
  qed
bulwahn@41120
   290
qed
bulwahn@41120
   291
paulson@15079
   292
lemma Maclaurin_all_lt:
bulwahn@41120
   293
  assumes INIT1: "diff 0 = f" and INIT2: "0 < n" and INIT3: "x \<noteq> 0"
bulwahn@41120
   294
  and DERIV: "\<forall>m x. DERIV (diff m) x :> diff(Suc m) x"
hoelzl@41166
   295
  shows "\<exists>t. 0 < abs t & abs t < abs x & f x =
hoelzl@56193
   296
    (\<Sum>m<n. (diff m 0 / real (fact m)) * x ^ m) +
hoelzl@41166
   297
                (diff n t / real (fact n)) * x ^ n" (is "\<exists>t. _ \<and> _ \<and> f x = ?f x t")
hoelzl@41166
   298
proof (cases rule: linorder_cases)
hoelzl@41166
   299
  assume "x = 0" with INIT3 show "?thesis"..
hoelzl@41166
   300
next
hoelzl@41166
   301
  assume "x < 0"
hoelzl@41166
   302
  with assms have "\<exists>t>x. t < 0 \<and> f x = ?f x t" by (intro Maclaurin_minus) auto
hoelzl@41166
   303
  then guess t ..
hoelzl@41166
   304
  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
   305
  thus ?thesis ..
hoelzl@41166
   306
next
hoelzl@41166
   307
  assume "x > 0"
hoelzl@41166
   308
  with assms have "\<exists>t>0. t < x \<and> f x = ?f x t " by (intro Maclaurin) auto
hoelzl@41166
   309
  then guess t ..
hoelzl@41166
   310
  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
   311
  thus ?thesis ..
bulwahn@41120
   312
qed
bulwahn@41120
   313
paulson@15079
   314
paulson@15079
   315
lemma Maclaurin_all_lt_objl:
paulson@15079
   316
     "diff 0 = f &
paulson@15079
   317
      (\<forall>m x. DERIV (diff m) x :> diff(Suc m) x) &
nipkow@25162
   318
      x ~= 0 & n > 0
paulson@15079
   319
      --> (\<exists>t. 0 < abs t & abs t < abs x &
hoelzl@56193
   320
               f x = (\<Sum>m<n. (diff m 0 / real (fact m)) * x ^ m) +
paulson@15079
   321
                     (diff n t / real (fact n)) * x ^ n)"
paulson@15079
   322
by (blast intro: Maclaurin_all_lt)
paulson@15079
   323
paulson@15079
   324
lemma Maclaurin_zero [rule_format]:
paulson@15079
   325
     "x = (0::real)
nipkow@25134
   326
      ==> n \<noteq> 0 -->
hoelzl@56193
   327
          (\<Sum>m<n. (diff m (0::real) / real (fact m)) * x ^ m) =
paulson@15079
   328
          diff 0 0"
paulson@15079
   329
by (induct n, auto)
paulson@15079
   330
bulwahn@41120
   331
bulwahn@41120
   332
lemma Maclaurin_all_le:
bulwahn@41120
   333
  assumes INIT: "diff 0 = f"
bulwahn@41120
   334
  and DERIV: "\<forall>m x. DERIV (diff m) x :> diff (Suc m) x"
hoelzl@41166
   335
  shows "\<exists>t. abs t \<le> abs x & f x =
hoelzl@56193
   336
    (\<Sum>m<n. (diff m 0 / real (fact m)) * x ^ m) +
hoelzl@41166
   337
    (diff n t / real (fact n)) * x ^ n" (is "\<exists>t. _ \<and> f x = ?f x t")
hoelzl@41166
   338
proof cases
hoelzl@41166
   339
  assume "n = 0" with INIT show ?thesis by force
bulwahn@41120
   340
  next
hoelzl@41166
   341
  assume "n \<noteq> 0"
hoelzl@41166
   342
  show ?thesis
hoelzl@41166
   343
  proof cases
hoelzl@41166
   344
    assume "x = 0"
hoelzl@56193
   345
    with `n \<noteq> 0` have "(\<Sum>m<n. diff m 0 / real (fact m) * x ^ m) = diff 0 0"
hoelzl@41166
   346
      by (intro Maclaurin_zero) auto
hoelzl@41166
   347
    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
   348
    thus ?thesis ..
hoelzl@41166
   349
  next
hoelzl@41166
   350
    assume "x \<noteq> 0"
hoelzl@41166
   351
    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
   352
      by (intro Maclaurin_all_lt) auto
hoelzl@41166
   353
    then guess t ..
hoelzl@41166
   354
    hence "\<bar>t\<bar> \<le> \<bar>x\<bar> \<and> f x = ?f x t" by simp
hoelzl@41166
   355
    thus ?thesis ..
bulwahn@41120
   356
  qed
bulwahn@41120
   357
qed
bulwahn@41120
   358
paulson@15079
   359
lemma Maclaurin_all_le_objl: "diff 0 = f &
paulson@15079
   360
      (\<forall>m x. DERIV (diff m) x :> diff (Suc m) x)
paulson@15079
   361
      --> (\<exists>t. abs t \<le> abs x &
hoelzl@56193
   362
              f x = (\<Sum>m<n. (diff m 0 / real (fact m)) * x ^ m) +
paulson@15079
   363
                    (diff n t / real (fact n)) * x ^ n)"
paulson@15079
   364
by (blast intro: Maclaurin_all_le)
paulson@15079
   365
paulson@15079
   366
paulson@15079
   367
subsection{*Version for Exponential Function*}
paulson@15079
   368
nipkow@25162
   369
lemma Maclaurin_exp_lt: "[| x ~= 0; n > 0 |]
paulson@15079
   370
      ==> (\<exists>t. 0 < abs t &
paulson@15079
   371
                abs t < abs x &
hoelzl@56193
   372
                exp x = (\<Sum>m<n. (x ^ m) / real (fact m)) +
paulson@15079
   373
                        (exp t / real (fact n)) * x ^ n)"
paulson@15079
   374
by (cut_tac diff = "%n. exp" and f = exp and x = x and n = n in Maclaurin_all_lt_objl, auto)
paulson@15079
   375
paulson@15079
   376
paulson@15079
   377
lemma Maclaurin_exp_le:
paulson@15079
   378
     "\<exists>t. abs t \<le> abs x &
hoelzl@56193
   379
            exp x = (\<Sum>m<n. (x ^ m) / real (fact m)) +
paulson@15079
   380
                       (exp t / real (fact n)) * x ^ n"
paulson@15079
   381
by (cut_tac diff = "%n. exp" and f = exp and x = x and n = n in Maclaurin_all_le_objl, auto)
paulson@15079
   382
paulson@15079
   383
paulson@15079
   384
subsection{*Version for Sine Function*}
paulson@15079
   385
paulson@15079
   386
lemma mod_exhaust_less_4:
nipkow@25134
   387
  "m mod 4 = 0 | m mod 4 = 1 | m mod 4 = 2 | m mod 4 = (3::nat)"
webertj@20217
   388
by auto
paulson@15079
   389
paulson@15079
   390
lemma Suc_Suc_mult_two_diff_two [rule_format, simp]:
nipkow@25134
   391
  "n\<noteq>0 --> Suc (Suc (2 * n - 2)) = 2*n"
paulson@15251
   392
by (induct "n", auto)
paulson@15079
   393
paulson@15079
   394
lemma lemma_Suc_Suc_4n_diff_2 [rule_format, simp]:
nipkow@25134
   395
  "n\<noteq>0 --> Suc (Suc (4*n - 2)) = 4*n"
paulson@15251
   396
by (induct "n", auto)
paulson@15079
   397
paulson@15079
   398
lemma Suc_mult_two_diff_one [rule_format, simp]:
nipkow@25134
   399
  "n\<noteq>0 --> Suc (2 * n - 1) = 2*n"
paulson@15251
   400
by (induct "n", auto)
paulson@15079
   401
paulson@15234
   402
paulson@15234
   403
text{*It is unclear why so many variant results are needed.*}
paulson@15079
   404
huffman@36974
   405
lemma sin_expansion_lemma:
hoelzl@41166
   406
     "sin (x + real (Suc m) * pi / 2) =
huffman@36974
   407
      cos (x + real (m) * pi / 2)"
webertj@49962
   408
by (simp only: cos_add sin_add real_of_nat_Suc add_divide_distrib distrib_right, auto)
huffman@36974
   409
paulson@15079
   410
lemma Maclaurin_sin_expansion2:
paulson@15079
   411
     "\<exists>t. abs t \<le> abs x &
paulson@15079
   412
       sin x =
hoelzl@56193
   413
       (\<Sum>m<n. sin_coeff m * x ^ m)
paulson@15079
   414
      + ((sin(t + 1/2 * real (n) *pi) / real (fact n)) * x ^ n)"
paulson@15079
   415
apply (cut_tac f = sin and n = n and x = x
paulson@15079
   416
        and diff = "%n x. sin (x + 1/2*real n * pi)" in Maclaurin_all_lt_objl)
paulson@15079
   417
apply safe
paulson@15079
   418
apply (simp (no_asm))
huffman@36974
   419
apply (simp (no_asm) add: sin_expansion_lemma)
huffman@44308
   420
apply (force intro!: DERIV_intros)
haftmann@51489
   421
apply (subst (asm) setsum_0', clarify, case_tac "x", simp, simp)
huffman@44306
   422
apply (cases n, simp, simp)
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)
nipkow@15536
   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)
huffman@44308
   449
apply (force intro!: DERIV_intros)
paulson@15079
   450
apply (erule ssubst)
paulson@15079
   451
apply (rule_tac x = t in exI, simp)
nipkow@15536
   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)
huffman@44308
   466
apply (force intro!: DERIV_intros)
paulson@15079
   467
apply (erule ssubst)
paulson@15079
   468
apply (rule_tac x = t in exI, simp)
nipkow@15536
   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)
nipkow@15536
   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)
nipkow@15536
   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)
nipkow@15536
   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@31881
   556
    apply (safe, auto intro!: DERIV_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
huffman@23177
   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)
nipkow@15536
   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
hoelzl@41166
   577
                simp add: est mult_nonneg_nonneg mult_ac divide_inverse
paulson@16924
   578
                          power_abs [symmetric] abs_mult)
obua@14738
   579
    done
obua@14738
   580
qed
obua@14738
   581
paulson@15079
   582
end