src/HOL/MacLaurin.thy
changeset 61609 77b453bd616f
parent 61284 2314c2f62eb1
child 61799 4cf66f21b764
equal deleted inserted replaced
61553:933eb9e6a1cc 61609:77b453bd616f
    22 by (rule exI[where x = "(f h - (\<Sum>m<n. (j m / (fact m)) * h^m)) * (fact n) / (h^n)"]) simp
    22 by (rule exI[where x = "(f h - (\<Sum>m<n. (j m / (fact m)) * h^m)) * (fact n) / (h^n)"]) simp
    23 
    23 
    24 lemma eq_diff_eq': "(x = y - z) = (y = x + (z::real))"
    24 lemma eq_diff_eq': "(x = y - z) = (y = x + (z::real))"
    25 by arith
    25 by arith
    26 
    26 
    27 lemma fact_diff_Suc [rule_format]:
    27 lemma fact_diff_Suc:
    28   "n < Suc m ==> fact (Suc m - n) = (Suc m - n) * fact (m - n)"
    28   "n < Suc m \<Longrightarrow> fact (Suc m - n) = (Suc m - n) * fact (m - n)"
    29   by (subst fact_reduce, auto)
    29   by (subst fact_reduce, auto)
    30 
    30 
    31 lemma Maclaurin_lemma2:
    31 lemma Maclaurin_lemma2:
    32   fixes B
    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"
    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"
    34       and INIT : "n = Suc k"
    35   defines "difg \<equiv> 
    35   defines "difg \<equiv>
    36       (\<lambda>m t::real. diff m t - 
    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)))))" 
    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)")
    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"
    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)+
    40 proof (rule allI impI)+
    41   fix m and t::real
    41   fix m and t::real
    42   assume INIT2: "m < n & 0 \<le> t & t \<le> h"
    42   assume INIT2: "m < n & 0 \<le> t & t \<le> h"
    43   have "DERIV (difg m) t :> diff (Suc m) t -
    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)) +
    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)))" 
    45      real (n - m) * t ^ (n - Suc m) * B / (fact (n - m)))"
    46     unfolding difg_def
    46     unfolding difg_def
    47     by (auto intro!: derivative_eq_intros DERIV[rule_format, OF INIT2]
    47     by (auto intro!: derivative_eq_intros DERIV[rule_format, OF INIT2])
    48              simp: real_of_nat_def[symmetric])
       
    49   moreover
    48   moreover
    50   from INIT2 have intvl: "{..<n - m} = insert 0 (Suc ` {..<n - Suc m})" and "0 < n - m"
    49   from INIT2 have intvl: "{..<n - m} = insert 0 (Suc ` {..<n - Suc m})" and "0 < n - m"
    51     unfolding atLeast0LessThan[symmetric] by auto
    50     unfolding atLeast0LessThan[symmetric] by auto
    52   have "(\<Sum>x<n - m. real x * t ^ (x - Suc 0) * diff (m + x) 0 / (fact x)) =
    51   have "(\<Sum>x<n - m. real x * t ^ (x - Suc 0) * diff (m + x) 0 / (fact x)) =
    53       (\<Sum>x<n - Suc m. real (Suc x) * t ^ x * diff (Suc m + x) 0 / (fact (Suc x)))"
    52       (\<Sum>x<n - Suc m. real (Suc x) * t ^ x * diff (Suc m + x) 0 / (fact (Suc x)))"
    54     unfolding intvl atLeast0LessThan by (subst setsum.insert) (auto simp: setsum.reindex)
    53     unfolding intvl atLeast0LessThan by (subst setsum.insert) (auto simp: setsum.reindex)
    55   moreover
    54   moreover
    56   have fact_neq_0: "\<And>x. (fact x) + real x * (fact x) \<noteq> 0"
    55   have fact_neq_0: "\<And>x. (fact x) + real x * (fact x) \<noteq> 0"
    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)
    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)
    58   have "\<And>x. (Suc x) * t ^ x * diff (Suc m + x) 0 / (fact (Suc x)) =
    57   have "\<And>x. (Suc x) * t ^ x * diff (Suc m + x) 0 / (fact (Suc x)) =
    59             diff (Suc m + x) 0 * t^x / (fact x)"
    58             diff (Suc m + x) 0 * t^x / (fact x)"
    60     by (rule nonzero_divide_eq_eq[THEN iffD2]) auto
    59     by (rule nonzero_divide_eq_eq[THEN iffD2]) auto
    61   moreover
    60   moreover
    62   have "(n - m) * t ^ (n - Suc m) * B / (fact (n - m)) =
    61   have "(n - m) * t ^ (n - Suc m) * B / (fact (n - m)) =
    63         B * (t ^ (n - Suc m) / (fact (n - Suc m)))"
    62         B * (t ^ (n - Suc m) / (fact (n - Suc m)))"
    64     using \<open>0 < n - m\<close>
    63     using \<open>0 < n - m\<close>
    65     by (simp add: divide_simps fact_reduce)
    64     by (simp add: divide_simps fact_reduce)
    66   ultimately show "DERIV (difg m) t :> difg (Suc m) t"
    65   ultimately show "DERIV (difg m) t :> difg (Suc m) t"
    67     unfolding difg_def by simp
    66     unfolding difg_def  by (simp add: mult.commute)
    68 qed
    67 qed
    69 
    68 
    70 lemma Maclaurin:
    69 lemma Maclaurin:
    71   assumes h: "0 < h"
    70   assumes h: "0 < h"
    72   assumes n: "0 < n"
    71   assumes n: "0 < n"
   418 text\<open>It is unclear why so many variant results are needed.\<close>
   417 text\<open>It is unclear why so many variant results are needed.\<close>
   419 
   418 
   420 lemma sin_expansion_lemma:
   419 lemma sin_expansion_lemma:
   421      "sin (x + real (Suc m) * pi / 2) =
   420      "sin (x + real (Suc m) * pi / 2) =
   422       cos (x + real (m) * pi / 2)"
   421       cos (x + real (m) * pi / 2)"
   423 by (simp only: cos_add sin_add real_of_nat_Suc add_divide_distrib distrib_right, auto)
   422 by (simp only: cos_add sin_add of_nat_Suc add_divide_distrib distrib_right, auto)
   424 
   423 
   425 lemma Maclaurin_sin_expansion2:
   424 lemma Maclaurin_sin_expansion2:
   426      "\<exists>t. abs t \<le> abs x &
   425      "\<exists>t. abs t \<le> abs x &
   427        sin x =
   426        sin x =
   428        (\<Sum>m<n. sin_coeff m * x ^ m)
   427        (\<Sum>m<n. sin_coeff m * x ^ m)
   429       + ((sin(t + 1/2 * real (n) *pi) / (fact n)) * x ^ n)"
   428       + ((sin(t + 1/2 * real (n) *pi) / (fact n)) * x ^ n)"
   430 apply (cut_tac f = sin and n = n and x = x
   429 apply (cut_tac f = sin and n = n and x = x
   431         and diff = "%n x. sin (x + 1/2*real n * pi)" in Maclaurin_all_lt_objl)
   430         and diff = "%n x. sin (x + 1/2*real n * pi)" in Maclaurin_all_lt_objl)
   432 apply safe
   431 apply safe
   433     apply (simp)
   432     apply (simp)
   434    apply (simp add: sin_expansion_lemma del: real_of_nat_Suc)
   433    apply (simp add: sin_expansion_lemma del: of_nat_Suc)
   435    apply (force intro!: derivative_eq_intros)
   434    apply (force intro!: derivative_eq_intros)
   436   apply (subst (asm) setsum.neutral, auto)[1]
   435   apply (subst (asm) setsum.neutral, auto)[1]
   437  apply (rule ccontr, simp)
   436  apply (rule ccontr, simp)
   438  apply (drule_tac x = x in spec, simp)
   437  apply (drule_tac x = x in spec, simp)
   439 apply (erule ssubst)
   438 apply (erule ssubst)
   440 apply (rule_tac x = t in exI, simp)
   439 apply (rule_tac x = t in exI, simp)
   441 apply (rule setsum.cong[OF refl])
   440 apply (rule setsum.cong[OF refl])
   442 apply (auto simp add: sin_coeff_def sin_zero_iff elim: oddE simp del: real_of_nat_Suc)
   441 apply (auto simp add: sin_coeff_def sin_zero_iff elim: oddE simp del: of_nat_Suc)
   443 done
   442 done
   444 
   443 
   445 lemma Maclaurin_sin_expansion:
   444 lemma Maclaurin_sin_expansion:
   446      "\<exists>t. sin x =
   445      "\<exists>t. sin x =
   447        (\<Sum>m<n. sin_coeff m * x ^ m)
   446        (\<Sum>m<n. sin_coeff m * x ^ m)
   457        (\<Sum>m<n. sin_coeff m * x ^ m)
   456        (\<Sum>m<n. sin_coeff m * x ^ m)
   458       + ((sin(t + 1/2 * real(n) *pi) / (fact n)) * x ^ n)"
   457       + ((sin(t + 1/2 * real(n) *pi) / (fact n)) * x ^ n)"
   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)
   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)
   460 apply safe
   459 apply safe
   461     apply simp
   460     apply simp
   462    apply (simp (no_asm) add: sin_expansion_lemma del: real_of_nat_Suc)
   461    apply (simp (no_asm) add: sin_expansion_lemma del: of_nat_Suc)
   463    apply (force intro!: derivative_eq_intros)
   462    apply (force intro!: derivative_eq_intros)
   464   apply (erule ssubst)
   463   apply (erule ssubst)
   465   apply (rule_tac x = t in exI, simp)
   464   apply (rule_tac x = t in exI, simp)
   466  apply (rule setsum.cong[OF refl])
   465  apply (rule setsum.cong[OF refl])
   467  apply (auto simp add: sin_coeff_def sin_zero_iff elim: oddE simp del: real_of_nat_Suc)
   466  apply (auto simp add: sin_coeff_def sin_zero_iff elim: oddE simp del: of_nat_Suc)
   468 done
   467 done
   469 
   468 
   470 lemma Maclaurin_sin_expansion4:
   469 lemma Maclaurin_sin_expansion4:
   471      "0 < x ==>
   470      "0 < x ==>
   472        \<exists>t. 0 < t & t \<le> x &
   471        \<exists>t. 0 < t & t \<le> x &
   474        (\<Sum>m<n. sin_coeff m * x ^ m)
   473        (\<Sum>m<n. sin_coeff m * x ^ m)
   475       + ((sin(t + 1/2 * real (n) *pi) / (fact n)) * x ^ n)"
   474       + ((sin(t + 1/2 * real (n) *pi) / (fact n)) * x ^ n)"
   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)
   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)
   477 apply safe
   476 apply safe
   478     apply simp
   477     apply simp
   479    apply (simp (no_asm) add: sin_expansion_lemma del: real_of_nat_Suc)
   478    apply (simp (no_asm) add: sin_expansion_lemma del: of_nat_Suc)
   480    apply (force intro!: derivative_eq_intros)
   479    apply (force intro!: derivative_eq_intros)
   481   apply (erule ssubst)
   480   apply (erule ssubst)
   482   apply (rule_tac x = t in exI, simp)
   481   apply (rule_tac x = t in exI, simp)
   483  apply (rule setsum.cong[OF refl])
   482  apply (rule setsum.cong[OF refl])
   484  apply (auto simp add: sin_coeff_def sin_zero_iff elim: oddE simp del: real_of_nat_Suc)
   483  apply (auto simp add: sin_coeff_def sin_zero_iff elim: oddE simp del: of_nat_Suc)
   485 done
   484 done
   486 
   485 
   487 
   486 
   488 subsection\<open>Maclaurin Expansion for Cosine Function\<close>
   487 subsection\<open>Maclaurin Expansion for Cosine Function\<close>
   489 
   488 
   491   "(\<Sum>m<(Suc n). cos_coeff m * 0 ^ m) = 1"
   490   "(\<Sum>m<(Suc n). cos_coeff m * 0 ^ m) = 1"
   492 by (induct "n", auto)
   491 by (induct "n", auto)
   493 
   492 
   494 lemma cos_expansion_lemma:
   493 lemma cos_expansion_lemma:
   495   "cos (x + real(Suc m) * pi / 2) = -sin (x + real m * pi / 2)"
   494   "cos (x + real(Suc m) * pi / 2) = -sin (x + real m * pi / 2)"
   496 by (simp only: cos_add sin_add real_of_nat_Suc distrib_right add_divide_distrib, auto)
   495 by (simp only: cos_add sin_add of_nat_Suc distrib_right add_divide_distrib, auto)
   497 
   496 
   498 lemma Maclaurin_cos_expansion:
   497 lemma Maclaurin_cos_expansion:
   499      "\<exists>t::real. abs t \<le> abs x &
   498      "\<exists>t::real. abs t \<le> abs x &
   500        cos x =
   499        cos x =
   501        (\<Sum>m<n. cos_coeff m * x ^ m)
   500        (\<Sum>m<n. cos_coeff m * x ^ m)
   502       + ((cos(t + 1/2 * real (n) *pi) / (fact n)) * x ^ n)"
   501       + ((cos(t + 1/2 * real (n) *pi) / (fact n)) * x ^ n)"
   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)
   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)
   504 apply safe
   503 apply safe
   505     apply (simp (no_asm))
   504     apply (simp (no_asm))
   506    apply (simp (no_asm) add: cos_expansion_lemma del: real_of_nat_Suc)
   505    apply (simp (no_asm) add: cos_expansion_lemma del: of_nat_Suc)
   507   apply (case_tac "n", simp)
   506   apply (case_tac "n", simp)
   508   apply (simp del: setsum_lessThan_Suc)
   507   apply (simp del: setsum_lessThan_Suc)
   509 apply (rule ccontr, simp)
   508 apply (rule ccontr, simp)
   510 apply (drule_tac x = x in spec, simp)
   509 apply (drule_tac x = x in spec, simp)
   511 apply (erule ssubst)
   510 apply (erule ssubst)
   521        (\<Sum>m<n. cos_coeff m * x ^ m)
   520        (\<Sum>m<n. cos_coeff m * x ^ m)
   522       + ((cos(t + 1/2 * real (n) *pi) / (fact n)) * x ^ n)"
   521       + ((cos(t + 1/2 * real (n) *pi) / (fact n)) * x ^ n)"
   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)
   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)
   524 apply safe
   523 apply safe
   525   apply simp
   524   apply simp
   526   apply (simp (no_asm) add: cos_expansion_lemma del: real_of_nat_Suc)
   525   apply (simp (no_asm) add: cos_expansion_lemma del: of_nat_Suc)
   527  apply (erule ssubst)
   526  apply (erule ssubst)
   528  apply (rule_tac x = t in exI, simp)
   527  apply (rule_tac x = t in exI, simp)
   529 apply (rule setsum.cong[OF refl])
   528 apply (rule setsum.cong[OF refl])
   530 apply (auto simp add: cos_coeff_def cos_zero_iff elim: evenE)
   529 apply (auto simp add: cos_coeff_def cos_zero_iff elim: evenE)
   531 done
   530 done
   537        (\<Sum>m<n. cos_coeff m * x ^ m)
   536        (\<Sum>m<n. cos_coeff m * x ^ m)
   538       + ((cos(t + 1/2 * real (n) *pi) / (fact n)) * x ^ n)"
   537       + ((cos(t + 1/2 * real (n) *pi) / (fact n)) * x ^ n)"
   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)
   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)
   540 apply safe
   539 apply safe
   541   apply simp
   540   apply simp
   542  apply (simp (no_asm) add: cos_expansion_lemma del: real_of_nat_Suc)
   541  apply (simp (no_asm) add: cos_expansion_lemma del: of_nat_Suc)
   543 apply (erule ssubst)
   542 apply (erule ssubst)
   544 apply (rule_tac x = t in exI, simp)
   543 apply (rule_tac x = t in exI, simp)
   545 apply (rule setsum.cong[OF refl])
   544 apply (rule setsum.cong[OF refl])
   546 apply (auto simp add: cos_coeff_def cos_zero_iff elim: evenE)
   545 apply (auto simp add: cos_coeff_def cos_zero_iff elim: evenE)
   547 done
   546 done