src/HOL/MacLaurin.thy
changeset 64267 b9a1486e79be
parent 63570 1826a90b9cbc
child 65273 917ae0ba03a2
equal deleted inserted replaced
64265:8eb6365f5916 64267:b9a1486e79be
    46   moreover
    46   moreover
    47   from INIT2 have intvl: "{..<n - m} = insert 0 (Suc ` {..<n - Suc m})" and "0 < n - m"
    47   from INIT2 have intvl: "{..<n - m} = insert 0 (Suc ` {..<n - Suc m})" and "0 < n - m"
    48     unfolding atLeast0LessThan[symmetric] by auto
    48     unfolding atLeast0LessThan[symmetric] by auto
    49   have "(\<Sum>x<n - m. real x * t ^ (x - Suc 0) * diff (m + x) 0 / fact x) =
    49   have "(\<Sum>x<n - m. real x * t ^ (x - Suc 0) * diff (m + x) 0 / fact x) =
    50       (\<Sum>x<n - Suc m. real (Suc x) * t ^ x * diff (Suc m + x) 0 / fact (Suc x))"
    50       (\<Sum>x<n - Suc m. real (Suc x) * t ^ x * diff (Suc m + x) 0 / fact (Suc x))"
    51     unfolding intvl by (subst setsum.insert) (auto simp add: setsum.reindex)
    51     unfolding intvl by (subst sum.insert) (auto simp add: sum.reindex)
    52   moreover
    52   moreover
    53   have fact_neq_0: "\<And>x. (fact x) + real x * (fact x) \<noteq> 0"
    53   have fact_neq_0: "\<And>x. (fact x) + real x * (fact x) \<noteq> 0"
    54     by (metis add_pos_pos fact_gt_zero less_add_same_cancel1 less_add_same_cancel2
    54     by (metis add_pos_pos fact_gt_zero less_add_same_cancel1 less_add_same_cancel2
    55         less_numeral_extra(3) mult_less_0_iff of_nat_less_0_iff)
    55         less_numeral_extra(3) mult_less_0_iff of_nat_less_0_iff)
    56   have "\<And>x. (Suc x) * t ^ x * diff (Suc m + x) 0 / fact (Suc x) = diff (Suc m + x) 0 * t^x / fact x"
    56   have "\<And>x. (Suc x) * t ^ x * diff (Suc m + x) 0 / fact (Suc x) = diff (Suc m + x) 0 * t^x / fact x"
    67     and n: "0 < n"
    67     and n: "0 < n"
    68     and diff_0: "diff 0 = f"
    68     and diff_0: "diff 0 = f"
    69     and diff_Suc: "\<forall>m t. m < n \<and> 0 \<le> t \<and> t \<le> h \<longrightarrow> DERIV (diff m) t :> diff (Suc m) t"
    69     and diff_Suc: "\<forall>m t. m < n \<and> 0 \<le> t \<and> t \<le> h \<longrightarrow> DERIV (diff m) t :> diff (Suc m) t"
    70   shows
    70   shows
    71     "\<exists>t::real. 0 < t \<and> t < h \<and>
    71     "\<exists>t::real. 0 < t \<and> t < h \<and>
    72       f h = setsum (\<lambda>m. (diff m 0 / fact m) * h ^ m) {..<n} + (diff n t / fact n) * h ^ n"
    72       f h = sum (\<lambda>m. (diff m 0 / fact m) * h ^ m) {..<n} + (diff n t / fact n) * h ^ n"
    73 proof -
    73 proof -
    74   from n obtain m where m: "n = Suc m"
    74   from n obtain m where m: "n = Suc m"
    75     by (cases n) (simp add: n)
    75     by (cases n) (simp add: n)
    76   from m have "m < n" by simp
    76   from m have "m < n" by simp
    77 
    77 
    78   obtain B where f_h: "f h = (\<Sum>m<n. diff m 0 / fact m * h ^ m) + B * (h ^ n / fact n)"
    78   obtain B where f_h: "f h = (\<Sum>m<n. diff m 0 / fact m * h ^ m) + B * (h ^ n / fact n)"
    79     using Maclaurin_lemma [OF h] ..
    79     using Maclaurin_lemma [OF h] ..
    80 
    80 
    81   define g where [abs_def]: "g t =
    81   define g where [abs_def]: "g t =
    82     f t - (setsum (\<lambda>m. (diff m 0 / fact m) * t^m) {..<n} + B * (t^n / fact n))" for t
    82     f t - (sum (\<lambda>m. (diff m 0 / fact m) * t^m) {..<n} + B * (t^n / fact n))" for t
    83   have g2: "g 0 = 0" "g h = 0"
    83   have g2: "g 0 = 0" "g h = 0"
    84     by (simp_all add: m f_h g_def lessThan_Suc_eq_insert_0 image_iff diff_0 setsum.reindex)
    84     by (simp_all add: m f_h g_def lessThan_Suc_eq_insert_0 image_iff diff_0 sum.reindex)
    85 
    85 
    86   define difg where [abs_def]: "difg m t =
    86   define difg where [abs_def]: "difg m t =
    87     diff m t - (setsum (\<lambda>p. (diff (m + p) 0 / fact p) * (t ^ p)) {..<n-m} +
    87     diff m t - (sum (\<lambda>p. (diff (m + p) 0 / fact p) * (t ^ p)) {..<n-m} +
    88       B * ((t ^ (n - m)) / fact (n - m)))" for m t
    88       B * ((t ^ (n - m)) / fact (n - m)))" for m t
    89   have difg_0: "difg 0 = g"
    89   have difg_0: "difg 0 = g"
    90     by (simp add: difg_def g_def diff_0)
    90     by (simp add: difg_def g_def diff_0)
    91   have difg_Suc: "\<forall>m t. m < n \<and> 0 \<le> t \<and> t \<le> h \<longrightarrow> DERIV (difg m) t :> difg (Suc m) t"
    91   have difg_Suc: "\<forall>m t. m < n \<and> 0 \<le> t \<and> t \<le> h \<longrightarrow> DERIV (difg m) t :> difg (Suc m) t"
    92     using diff_Suc m unfolding difg_def [abs_def] by (rule Maclaurin_lemma2)
    92     using diff_Suc m unfolding difg_def [abs_def] by (rule Maclaurin_lemma2)
    93   have difg_eq_0: "\<forall>m<n. difg m 0 = 0"
    93   have difg_eq_0: "\<forall>m<n. difg m 0 = 0"
    94     by (auto simp: difg_def m Suc_diff_le lessThan_Suc_eq_insert_0 image_iff setsum.reindex)
    94     by (auto simp: difg_def m Suc_diff_le lessThan_Suc_eq_insert_0 image_iff sum.reindex)
    95   have isCont_difg: "\<And>m x. m < n \<Longrightarrow> 0 \<le> x \<Longrightarrow> x \<le> h \<Longrightarrow> isCont (difg m) x"
    95   have isCont_difg: "\<And>m x. m < n \<Longrightarrow> 0 \<le> x \<Longrightarrow> x \<le> h \<Longrightarrow> isCont (difg m) x"
    96     by (rule DERIV_isCont [OF difg_Suc [rule_format]]) simp
    96     by (rule DERIV_isCont [OF difg_Suc [rule_format]]) simp
    97   have differentiable_difg: "\<And>m x. m < n \<Longrightarrow> 0 \<le> x \<Longrightarrow> x \<le> h \<Longrightarrow> difg m differentiable (at x)"
    97   have differentiable_difg: "\<And>m x. m < n \<Longrightarrow> 0 \<le> x \<Longrightarrow> x \<le> h \<Longrightarrow> difg m differentiable (at x)"
    98     by (rule differentiableI [OF difg_Suc [rule_format]]) simp
    98     by (rule differentiableI [OF difg_Suc [rule_format]]) simp
    99   have difg_Suc_eq_0:
    99   have difg_Suc_eq_0:
   196     by blast
   196     by blast
   197   moreover have "(- 1) ^ n * diff n (- t) * (- h) ^ n / fact n = diff n (- t) * h ^ n / fact n"
   197   moreover have "(- 1) ^ n * diff n (- t) * (- h) ^ n / fact n = diff n (- t) * h ^ n / fact n"
   198     by (auto simp: power_mult_distrib[symmetric])
   198     by (auto simp: power_mult_distrib[symmetric])
   199   moreover
   199   moreover
   200     have "(\<Sum>m<n. (- 1) ^ m * diff m 0 * (- h) ^ m / fact m) = (\<Sum>m<n. diff m 0 * h ^ m / fact m)"
   200     have "(\<Sum>m<n. (- 1) ^ m * diff m 0 * (- h) ^ m / fact m) = (\<Sum>m<n. diff m 0 * h ^ m / fact m)"
   201     by (auto intro: setsum.cong simp add: power_mult_distrib[symmetric])
   201     by (auto intro: sum.cong simp add: power_mult_distrib[symmetric])
   202   ultimately have "h < - t \<and> - t < 0 \<and>
   202   ultimately have "h < - t \<and> - t < 0 \<and>
   203     f h = (\<Sum>m<n. diff m 0 / (fact m) * h ^ m) + diff n (- t) / (fact n) * h ^ n"
   203     f h = (\<Sum>m<n. diff m 0 / (fact m) * h ^ m) + diff n (- t) / (fact n) * h ^ n"
   204     by auto
   204     by auto
   205   then show ?thesis ..
   205   then show ?thesis ..
   206 qed
   206 qed
   392     [where f = sin and n = n and x = x and diff = "\<lambda>n x. sin (x + 1/2 * real n * pi)"]
   392     [where f = sin and n = n and x = x and diff = "\<lambda>n x. sin (x + 1/2 * real n * pi)"]
   393   apply safe
   393   apply safe
   394       apply simp
   394       apply simp
   395      apply (simp add: sin_expansion_lemma del: of_nat_Suc)
   395      apply (simp add: sin_expansion_lemma del: of_nat_Suc)
   396      apply (force intro!: derivative_eq_intros)
   396      apply (force intro!: derivative_eq_intros)
   397     apply (subst (asm) setsum.neutral; auto)
   397     apply (subst (asm) sum.neutral; auto)
   398    apply (rule ccontr)
   398    apply (rule ccontr)
   399    apply simp
   399    apply simp
   400    apply (drule_tac x = x in spec)
   400    apply (drule_tac x = x in spec)
   401    apply simp
   401    apply simp
   402   apply (erule ssubst)
   402   apply (erule ssubst)
   403   apply (rule_tac x = t in exI)
   403   apply (rule_tac x = t in exI)
   404   apply simp
   404   apply simp
   405   apply (rule setsum.cong[OF refl])
   405   apply (rule sum.cong[OF refl])
   406   apply (auto simp add: sin_coeff_def sin_zero_iff elim: oddE simp del: of_nat_Suc)
   406   apply (auto simp add: sin_coeff_def sin_zero_iff elim: oddE simp del: of_nat_Suc)
   407   done
   407   done
   408 
   408 
   409 lemma Maclaurin_sin_expansion:
   409 lemma Maclaurin_sin_expansion:
   410   "\<exists>t. sin x = (\<Sum>m<n. sin_coeff m * x ^ m) + (sin (t + 1/2 * real n * pi) / fact n) * x ^ n"
   410   "\<exists>t. sin x = (\<Sum>m<n. sin_coeff m * x ^ m) + (sin (t + 1/2 * real n * pi) / fact n) * x ^ n"
   421    apply (simp (no_asm) add: sin_expansion_lemma del: of_nat_Suc)
   421    apply (simp (no_asm) add: sin_expansion_lemma del: of_nat_Suc)
   422    apply (force intro!: derivative_eq_intros)
   422    apply (force intro!: derivative_eq_intros)
   423   apply (erule ssubst)
   423   apply (erule ssubst)
   424   apply (rule_tac x = t in exI)
   424   apply (rule_tac x = t in exI)
   425   apply simp
   425   apply simp
   426   apply (rule setsum.cong[OF refl])
   426   apply (rule sum.cong[OF refl])
   427   apply (auto simp add: sin_coeff_def sin_zero_iff elim: oddE simp del: of_nat_Suc)
   427   apply (auto simp add: sin_coeff_def sin_zero_iff elim: oddE simp del: of_nat_Suc)
   428   done
   428   done
   429 
   429 
   430 lemma Maclaurin_sin_expansion4:
   430 lemma Maclaurin_sin_expansion4:
   431   "0 < x \<Longrightarrow>
   431   "0 < x \<Longrightarrow>
   438    apply (simp (no_asm) add: sin_expansion_lemma del: of_nat_Suc)
   438    apply (simp (no_asm) add: sin_expansion_lemma del: of_nat_Suc)
   439    apply (force intro!: derivative_eq_intros)
   439    apply (force intro!: derivative_eq_intros)
   440   apply (erule ssubst)
   440   apply (erule ssubst)
   441   apply (rule_tac x = t in exI)
   441   apply (rule_tac x = t in exI)
   442   apply simp
   442   apply simp
   443   apply (rule setsum.cong[OF refl])
   443   apply (rule sum.cong[OF refl])
   444   apply (auto simp add: sin_coeff_def sin_zero_iff elim: oddE simp del: of_nat_Suc)
   444   apply (auto simp add: sin_coeff_def sin_zero_iff elim: oddE simp del: of_nat_Suc)
   445   done
   445   done
   446 
   446 
   447 
   447 
   448 subsection \<open>Maclaurin Expansion for Cosine Function\<close>
   448 subsection \<open>Maclaurin Expansion for Cosine Function\<close>
   461   apply safe
   461   apply safe
   462       apply simp
   462       apply simp
   463      apply (simp add: cos_expansion_lemma del: of_nat_Suc)
   463      apply (simp add: cos_expansion_lemma del: of_nat_Suc)
   464     apply (cases n)
   464     apply (cases n)
   465      apply simp
   465      apply simp
   466     apply (simp del: setsum_lessThan_Suc)
   466     apply (simp del: sum_lessThan_Suc)
   467    apply (rule ccontr)
   467    apply (rule ccontr)
   468    apply simp
   468    apply simp
   469    apply (drule_tac x = x in spec)
   469    apply (drule_tac x = x in spec)
   470    apply simp
   470    apply simp
   471   apply (erule ssubst)
   471   apply (erule ssubst)
   472   apply (rule_tac x = t in exI)
   472   apply (rule_tac x = t in exI)
   473   apply simp
   473   apply simp
   474   apply (rule setsum.cong[OF refl])
   474   apply (rule sum.cong[OF refl])
   475   apply (auto simp add: cos_coeff_def cos_zero_iff elim: evenE)
   475   apply (auto simp add: cos_coeff_def cos_zero_iff elim: evenE)
   476   done
   476   done
   477 
   477 
   478 lemma Maclaurin_cos_expansion2:
   478 lemma Maclaurin_cos_expansion2:
   479   "0 < x \<Longrightarrow> n > 0 \<Longrightarrow>
   479   "0 < x \<Longrightarrow> n > 0 \<Longrightarrow>
   485     apply simp
   485     apply simp
   486    apply (simp add: cos_expansion_lemma del: of_nat_Suc)
   486    apply (simp add: cos_expansion_lemma del: of_nat_Suc)
   487   apply (erule ssubst)
   487   apply (erule ssubst)
   488   apply (rule_tac x = t in exI)
   488   apply (rule_tac x = t in exI)
   489   apply simp
   489   apply simp
   490   apply (rule setsum.cong[OF refl])
   490   apply (rule sum.cong[OF refl])
   491   apply (auto simp: cos_coeff_def cos_zero_iff elim: evenE)
   491   apply (auto simp: cos_coeff_def cos_zero_iff elim: evenE)
   492   done
   492   done
   493 
   493 
   494 lemma Maclaurin_minus_cos_expansion:
   494 lemma Maclaurin_minus_cos_expansion:
   495   "x < 0 \<Longrightarrow> n > 0 \<Longrightarrow>
   495   "x < 0 \<Longrightarrow> n > 0 \<Longrightarrow>
   501     apply simp
   501     apply simp
   502    apply (simp add: cos_expansion_lemma del: of_nat_Suc)
   502    apply (simp add: cos_expansion_lemma del: of_nat_Suc)
   503   apply (erule ssubst)
   503   apply (erule ssubst)
   504   apply (rule_tac x = t in exI)
   504   apply (rule_tac x = t in exI)
   505   apply simp
   505   apply simp
   506   apply (rule setsum.cong[OF refl])
   506   apply (rule sum.cong[OF refl])
   507   apply (auto simp: cos_coeff_def cos_zero_iff elim: evenE)
   507   apply (auto simp: cos_coeff_def cos_zero_iff elim: evenE)
   508   done
   508   done
   509 
   509 
   510 
   510 
   511 (* Version for ln(1 +/- x). Where is it?? *)
   511 (* Version for ln(1 +/- x). Where is it?? *)
   546     done
   546     done
   547   show ?thesis
   547   show ?thesis
   548     unfolding sin_coeff_def
   548     unfolding sin_coeff_def
   549     apply (subst t2)
   549     apply (subst t2)
   550     apply (rule sin_bound_lemma)
   550     apply (rule sin_bound_lemma)
   551      apply (rule setsum.cong[OF refl])
   551      apply (rule sum.cong[OF refl])
   552      apply (subst diff_m_0, simp)
   552      apply (subst diff_m_0, simp)
   553     using est
   553     using est
   554     apply (auto intro: mult_right_mono [where b=1, simplified] mult_right_mono
   554     apply (auto intro: mult_right_mono [where b=1, simplified] mult_right_mono
   555         simp: ac_simps divide_inverse power_abs [symmetric] abs_mult)
   555         simp: ac_simps divide_inverse power_abs [symmetric] abs_mult)
   556     done
   556     done