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