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