Theory MacLaurin

theory MacLaurin
imports Transcendental
(*  Title:      HOL/MacLaurin.thy
    Author:     Jacques D. Fleuriot, 2001 University of Edinburgh
    Author:     Lawrence C Paulson, 2004
    Author:     Lukas Bulwahn and Bernhard Häupler, 2005
*)

section ‹MacLaurin and Taylor Series›

theory MacLaurin
imports Transcendental
begin

subsection ‹Maclaurin's Theorem with Lagrange Form of Remainder›

text ‹This is a very long, messy proof even now that it's been broken down
  into lemmas.›

lemma Maclaurin_lemma:
  "0 < h ⟹
    ∃B::real. f h = (∑m<n. (j m / (fact m)) * (h^m)) + (B * ((h^n) /(fact n)))"
  by (rule exI[where x = "(f h - (∑m<n. (j m / (fact m)) * h^m)) * (fact n) / (h^n)"]) simp

lemma eq_diff_eq': "x = y - z ⟷ y = x + z"
  for x y z :: real
  by arith

lemma fact_diff_Suc: "n < Suc m ⟹ fact (Suc m - n) = (Suc m - n) * fact (m - n)"
  by (subst fact_reduce) auto

lemma Maclaurin_lemma2:
  fixes B
  assumes DERIV: "∀m t. m < n ∧ 0≤t ∧ t≤h ⟶ DERIV (diff m) t :> diff (Suc m) t"
    and INIT: "n = Suc k"
  defines "difg ≡
    (λm t::real. diff m t -
      ((∑p<n - m. diff (m + p) 0 / fact p * t ^ p) + B * (t ^ (n - m) / fact (n - m))))"
    (is "difg ≡ (λm t. diff m t - ?difg m t)")
  shows "∀m t. m < n ∧ 0 ≤ t ∧ t ≤ h ⟶ DERIV (difg m) t :> difg (Suc m) t"
proof (rule allI impI)+
  fix m t
  assume INIT2: "m < n ∧ 0 ≤ t ∧ t ≤ h"
  have "DERIV (difg m) t :> diff (Suc m) t -
    ((∑x<n - m. real x * t ^ (x - Suc 0) * diff (m + x) 0 / fact x) +
     real (n - m) * t ^ (n - Suc m) * B / fact (n - m))"
    by (auto simp: difg_def intro!: derivative_eq_intros DERIV[rule_format, OF INIT2])
  moreover
  from INIT2 have intvl: "{..<n - m} = insert 0 (Suc ` {..<n - Suc m})" and "0 < n - m"
    unfolding atLeast0LessThan[symmetric] by auto
  have "(∑x<n - m. real x * t ^ (x - Suc 0) * diff (m + x) 0 / fact x) =
      (∑x<n - Suc m. real (Suc x) * t ^ x * diff (Suc m + x) 0 / fact (Suc x))"
    unfolding intvl by (subst sum.insert) (auto simp add: sum.reindex)
  moreover
  have fact_neq_0: "⋀x. (fact x) + real x * (fact x) ≠ 0"
    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)
  have "⋀x. (Suc x) * t ^ x * diff (Suc m + x) 0 / fact (Suc x) = diff (Suc m + x) 0 * t^x / fact x"
    by (rule nonzero_divide_eq_eq[THEN iffD2]) auto
  moreover
  have "(n - m) * t ^ (n - Suc m) * B / fact (n - m) = B * (t ^ (n - Suc m) / fact (n - Suc m))"
    using ‹0 < n - m› by (simp add: divide_simps fact_reduce)
  ultimately show "DERIV (difg m) t :> difg (Suc m) t"
    unfolding difg_def  by (simp add: mult.commute)
qed

lemma Maclaurin:
  assumes h: "0 < h"
    and n: "0 < n"
    and diff_0: "diff 0 = f"
    and diff_Suc: "∀m t. m < n ∧ 0 ≤ t ∧ t ≤ h ⟶ DERIV (diff m) t :> diff (Suc m) t"
  shows
    "∃t::real. 0 < t ∧ t < h ∧
      f h = sum (λm. (diff m 0 / fact m) * h ^ m) {..<n} + (diff n t / fact n) * h ^ n"
proof -
  from n obtain m where m: "n = Suc m"
    by (cases n) (simp add: n)
  from m have "m < n" by simp

  obtain B where f_h: "f h = (∑m<n. diff m 0 / fact m * h ^ m) + B * (h ^ n / fact n)"
    using Maclaurin_lemma [OF h] ..

  define g where [abs_def]: "g t =
    f t - (sum (λm. (diff m 0 / fact m) * t^m) {..<n} + B * (t^n / fact n))" for t
  have g2: "g 0 = 0" "g h = 0"
    by (simp_all add: m f_h g_def lessThan_Suc_eq_insert_0 image_iff diff_0 sum.reindex)

  define difg where [abs_def]: "difg m t =
    diff m t - (sum (λp. (diff (m + p) 0 / fact p) * (t ^ p)) {..<n-m} +
      B * ((t ^ (n - m)) / fact (n - m)))" for m t
  have difg_0: "difg 0 = g"
    by (simp add: difg_def g_def diff_0)
  have difg_Suc: "∀m t. m < n ∧ 0 ≤ t ∧ t ≤ h ⟶ DERIV (difg m) t :> difg (Suc m) t"
    using diff_Suc m unfolding difg_def [abs_def] by (rule Maclaurin_lemma2)
  have difg_eq_0: "∀m<n. difg m 0 = 0"
    by (auto simp: difg_def m Suc_diff_le lessThan_Suc_eq_insert_0 image_iff sum.reindex)
  have isCont_difg: "⋀m x. m < n ⟹ 0 ≤ x ⟹ x ≤ h ⟹ isCont (difg m) x"
    by (rule DERIV_isCont [OF difg_Suc [rule_format]]) simp
  have differentiable_difg: "⋀m x. m < n ⟹ 0 ≤ x ⟹ x ≤ h ⟹ difg m differentiable (at x)"
    by (rule differentiableI [OF difg_Suc [rule_format]]) simp
  have difg_Suc_eq_0:
    "⋀m t. m < n ⟹ 0 ≤ t ⟹ t ≤ h ⟹ DERIV (difg m) t :> 0 ⟹ difg (Suc m) t = 0"
    by (rule DERIV_unique [OF difg_Suc [rule_format]]) simp

  have "∃t. 0 < t ∧ t < h ∧ DERIV (difg m) t :> 0"
  using ‹m < n›
  proof (induct m)
    case 0
    show ?case
    proof (rule Rolle)
      show "0 < h" by fact
      show "difg 0 0 = difg 0 h"
        by (simp add: difg_0 g2)
      show "∀x. 0 ≤ x ∧ x ≤ h ⟶ isCont (difg (0::nat)) x"
        by (simp add: isCont_difg n)
      show "∀x. 0 < x ∧ x < h ⟶ difg (0::nat) differentiable (at x)"
        by (simp add: differentiable_difg n)
    qed
  next
    case (Suc m')
    then have "∃t. 0 < t ∧ t < h ∧ DERIV (difg m') t :> 0"
      by simp
    then obtain t where t: "0 < t" "t < h" "DERIV (difg m') t :> 0"
      by fast
    have "∃t'. 0 < t' ∧ t' < t ∧ DERIV (difg (Suc m')) t' :> 0"
    proof (rule Rolle)
      show "0 < t" by fact
      show "difg (Suc m') 0 = difg (Suc m') t"
        using t ‹Suc m' < n› by (simp add: difg_Suc_eq_0 difg_eq_0)
      show "∀x. 0 ≤ x ∧ x ≤ t ⟶ isCont (difg (Suc m')) x"
        using ‹t < h› ‹Suc m' < n› by (simp add: isCont_difg)
      show "∀x. 0 < x ∧ x < t ⟶ difg (Suc m') differentiable (at x)"
        using ‹t < h› ‹Suc m' < n› by (simp add: differentiable_difg)
    qed
    with ‹t < h› show ?case
      by auto
  qed
  then obtain t where "0 < t" "t < h" "DERIV (difg m) t :> 0"
    by fast
  with ‹m < n› have "difg (Suc m) t = 0"
    by (simp add: difg_Suc_eq_0)
  show ?thesis
  proof (intro exI conjI)
    show "0 < t" by fact
    show "t < h" by fact
    show "f h = (∑m<n. diff m 0 / (fact m) * h ^ m) + diff n t / (fact n) * h ^ n"
      using ‹difg (Suc m) t = 0› by (simp add: m f_h difg_def)
  qed
qed

lemma Maclaurin_objl:
  "0 < h ∧ n > 0 ∧ diff 0 = f ∧
    (∀m t. m < n ∧ 0 ≤ t ∧ t ≤ h ⟶ DERIV (diff m) t :> diff (Suc m) t) ⟶
    (∃t. 0 < t ∧ t < h ∧ f h = (∑m<n. diff m 0 / (fact m) * h ^ m) + diff n t / fact n * h ^ n)"
  for n :: nat and h :: real
  by (blast intro: Maclaurin)

lemma Maclaurin2:
  fixes n :: nat
    and h :: real
  assumes INIT1: "0 < h"
    and INIT2: "diff 0 = f"
    and DERIV: "∀m t. m < n ∧ 0 ≤ t ∧ t ≤ h ⟶ DERIV (diff m) t :> diff (Suc m) t"
  shows "∃t. 0 < t ∧ t ≤ h ∧ f h = (∑m<n. diff m 0 / (fact m) * h ^ m) + diff n t / fact n * h ^ n"
proof (cases n)
  case 0
  with INIT1 INIT2 show ?thesis by fastforce
next
  case Suc
  then have "n > 0" by simp
  from INIT1 this INIT2 DERIV
  have "∃t>0. t < h ∧ f h = (∑m<n. diff m 0 / fact m * h ^ m) + diff n t / fact n * h ^ n"
    by (rule Maclaurin)
  then show ?thesis by fastforce
qed

lemma Maclaurin2_objl:
  "0 < h ∧ diff 0 = f ∧
    (∀m t. m < n ∧ 0 ≤ t ∧ t ≤ h ⟶ DERIV (diff m) t :> diff (Suc m) t) ⟶
    (∃t. 0 < t ∧ t ≤ h ∧ f h = (∑m<n. diff m 0 / fact m * h ^ m) + diff n t / fact n * h ^ n)"
  for n :: nat and h :: real
  by (blast intro: Maclaurin2)

lemma Maclaurin_minus:
  fixes n :: nat and h :: real
  assumes "h < 0" "0 < n" "diff 0 = f"
    and DERIV: "∀m t. m < n ∧ h ≤ t ∧ t ≤ 0 ⟶ DERIV (diff m) t :> diff (Suc m) t"
  shows "∃t. h < t ∧ t < 0 ∧ f h = (∑m<n. diff m 0 / fact m * h ^ m) + diff n t / fact n * h ^ n"
proof -
  txt ‹Transform ‹ABL'› into ‹derivative_intros› format.›
  note DERIV' = DERIV_chain'[OF _ DERIV[rule_format], THEN DERIV_cong]
  let ?sum = "λt.
    (∑m<n. (- 1) ^ m * diff m (- 0) / (fact m) * (- h) ^ m) +
    (- 1) ^ n * diff n (- t) / (fact n) * (- h) ^ n"
  from assms have "∃t>0. t < - h ∧ f (- (- h)) = ?sum t"
    by (intro Maclaurin) (auto intro!: derivative_eq_intros DERIV')
  then obtain t where "0 < t" "t < - h" "f (- (- h)) = ?sum t"
    by blast
  moreover have "(- 1) ^ n * diff n (- t) * (- h) ^ n / fact n = diff n (- t) * h ^ n / fact n"
    by (auto simp: power_mult_distrib[symmetric])
  moreover
    have "(∑m<n. (- 1) ^ m * diff m 0 * (- h) ^ m / fact m) = (∑m<n. diff m 0 * h ^ m / fact m)"
    by (auto intro: sum.cong simp add: power_mult_distrib[symmetric])
  ultimately have "h < - t ∧ - t < 0 ∧
    f h = (∑m<n. diff m 0 / (fact m) * h ^ m) + diff n (- t) / (fact n) * h ^ n"
    by auto
  then show ?thesis ..
qed

lemma Maclaurin_minus_objl:
  fixes n :: nat and h :: real
  shows
    "h < 0 ∧ n > 0 ∧ diff 0 = f ∧
      (∀m t. m < n & h ≤ t & t ≤ 0 --> DERIV (diff m) t :> diff (Suc m) t) ⟶
    (∃t. h < t ∧ t < 0 ∧ f h = (∑m<n. diff m 0 / fact m * h ^ m) + diff n t / fact n * h ^ n)"
  by (blast intro: Maclaurin_minus)


subsection ‹More Convenient "Bidirectional" Version.›

(* not good for PVS sin_approx, cos_approx *)

lemma Maclaurin_bi_le_lemma:
  "n > 0 ⟹
    diff 0 0 = (∑m<n. diff m 0 * 0 ^ m / (fact m)) + diff n 0 * 0 ^ n / (fact n :: real)"
  by (induct n) auto

lemma Maclaurin_bi_le:
  fixes n :: nat and x :: real
  assumes "diff 0 = f"
    and DERIV : "∀m t. m < n ∧ ¦t¦ ≤ ¦x¦ ⟶ DERIV (diff m) t :> diff (Suc m) t"
  shows "∃t. ¦t¦ ≤ ¦x¦ ∧ f x = (∑m<n. diff m 0 / (fact m) * x ^ m) + diff n t / (fact n) * x ^ n"
    (is "∃t. _ ∧ f x = ?f x t")
proof (cases "n = 0")
  case True
  with ‹diff 0 = f› show ?thesis by force
next
  case False
  show ?thesis
  proof (cases rule: linorder_cases)
    assume "x = 0"
    with ‹n ≠ 0› ‹diff 0 = f› DERIV have "¦0¦ ≤ ¦x¦ ∧ f x = ?f x 0"
      by (auto simp add: Maclaurin_bi_le_lemma)
    then show ?thesis ..
  next
    assume "x < 0"
    with ‹n ≠ 0› DERIV have "∃t>x. t < 0 ∧ diff 0 x = ?f x t"
      by (intro Maclaurin_minus) auto
    then obtain t where "x < t" "t < 0"
      "diff 0 x = (∑m<n. diff m 0 / fact m * x ^ m) + diff n t / fact n * x ^ n"
      by blast
    with ‹x < 0› ‹diff 0 = f› have "¦t¦ ≤ ¦x¦ ∧ f x = ?f x t"
      by simp
    then show ?thesis ..
  next
    assume "x > 0"
    with ‹n ≠ 0› ‹diff 0 = f› DERIV have "∃t>0. t < x ∧ diff 0 x = ?f x t"
      by (intro Maclaurin) auto
    then obtain t where "0 < t" "t < x"
      "diff 0 x = (∑m<n. diff m 0 / fact m * x ^ m) + diff n t / fact n * x ^ n"
      by blast
    with ‹x > 0› ‹diff 0 = f› have "¦t¦ ≤ ¦x¦ ∧ f x = ?f x t" by simp
    then show ?thesis ..
  qed
qed

lemma Maclaurin_all_lt:
  fixes x :: real
  assumes INIT1: "diff 0 = f"
    and INIT2: "0 < n"
    and INIT3: "x ≠ 0"
    and DERIV: "∀m x. DERIV (diff m) x :> diff(Suc m) x"
  shows "∃t. 0 < ¦t¦ ∧ ¦t¦ < ¦x¦ ∧ f x =
      (∑m<n. (diff m 0 / fact m) * x ^ m) + (diff n t / fact n) * x ^ n"
    (is "∃t. _ ∧ _ ∧ f x = ?f x t")
proof (cases rule: linorder_cases)
  assume "x = 0"
  with INIT3 show ?thesis ..
next
  assume "x < 0"
  with assms have "∃t>x. t < 0 ∧ f x = ?f x t"
    by (intro Maclaurin_minus) auto
  then obtain t where "t > x" "t < 0" "f x = ?f x t"
    by blast
  with ‹x < 0› have "0 < ¦t¦ ∧ ¦t¦ < ¦x¦ ∧ f x = ?f x t"
    by simp
  then show ?thesis ..
next
  assume "x > 0"
  with assms have "∃t>0. t < x ∧ f x = ?f x t"
    by (intro Maclaurin) auto
  then obtain t where "t > 0" "t < x" "f x = ?f x t"
    by blast
  with ‹x > 0› have "0 < ¦t¦ ∧ ¦t¦ < ¦x¦ ∧ f x = ?f x t"
    by simp
  then show ?thesis ..
qed


lemma Maclaurin_all_lt_objl:
  fixes x :: real
  shows
    "diff 0 = f ∧ (∀m x. DERIV (diff m) x :> diff (Suc m) x) ∧ x ≠ 0 ∧ n > 0 ⟶
    (∃t. 0 < ¦t¦ ∧ ¦t¦ < ¦x¦ ∧
      f x = (∑m<n. (diff m 0 / fact m) * x ^ m) + (diff n t / fact n) * x ^ n)"
  by (blast intro: Maclaurin_all_lt)

lemma Maclaurin_zero: "x = 0 ⟹ n ≠ 0 ⟹ (∑m<n. (diff m 0 / fact m) * x ^ m) = diff 0 0"
  for x :: real and n :: nat
  by (induct n) auto


lemma Maclaurin_all_le:
  fixes x :: real and n :: nat
  assumes INIT: "diff 0 = f"
    and DERIV: "∀m x. DERIV (diff m) x :> diff (Suc m) x"
  shows "∃t. ¦t¦ ≤ ¦x¦ ∧ f x = (∑m<n. (diff m 0 / fact m) * x ^ m) + (diff n t / fact n) * x ^ n"
    (is "∃t. _ ∧ f x = ?f x t")
proof (cases "n = 0")
  case True
  with INIT show ?thesis by force
next
  case False
  show ?thesis
  proof (cases "x = 0")
    case True
    with ‹n ≠ 0› have "(∑m<n. diff m 0 / (fact m) * x ^ m) = diff 0 0"
      by (intro Maclaurin_zero) auto
    with INIT ‹x = 0› ‹n ≠ 0› have " ¦0¦ ≤ ¦x¦ ∧ f x = ?f x 0"
      by force
    then show ?thesis ..
  next
    case False
    with INIT ‹n ≠ 0› DERIV have "∃t. 0 < ¦t¦ ∧ ¦t¦ < ¦x¦ ∧ f x = ?f x t"
      by (intro Maclaurin_all_lt) auto
    then obtain t where "0 < ¦t¦ ∧ ¦t¦ < ¦x¦ ∧ f x = ?f x t" ..
    then have "¦t¦ ≤ ¦x¦ ∧ f x = ?f x t"
      by simp
    then show ?thesis ..
  qed
qed

lemma Maclaurin_all_le_objl:
  "diff 0 = f ∧ (∀m x. DERIV (diff m) x :> diff (Suc m) x) ⟶
    (∃t::real. ¦t¦ ≤ ¦x¦ ∧ f x = (∑m<n. (diff m 0 / fact m) * x ^ m) + (diff n t / fact n) * x ^ n)"
  for x :: real and n :: nat
  by (blast intro: Maclaurin_all_le)


subsection ‹Version for Exponential Function›

lemma Maclaurin_exp_lt:
  fixes x :: real and n :: nat
  shows
    "x ≠ 0 ⟹ n > 0 ⟹
      (∃t. 0 < ¦t¦ ∧ ¦t¦ < ¦x¦ ∧ exp x = (∑m<n. (x ^ m) / fact m) + (exp t / fact n) * x ^ n)"
 using Maclaurin_all_lt_objl [where diff = "λn. exp" and f = exp and x = x and n = n] by auto

lemma Maclaurin_exp_le:
  fixes x :: real and n :: nat
  shows "∃t. ¦t¦ ≤ ¦x¦ ∧ exp x = (∑m<n. (x ^ m) / fact m) + (exp t / fact n) * x ^ n"
  using Maclaurin_all_le_objl [where diff = "λn. exp" and f = exp and x = x and n = n] by auto

corollary exp_lower_taylor_quadratic: "0 ≤ x ⟹ 1 + x + x2 / 2 ≤ exp x"
  for x :: real
  using Maclaurin_exp_le [of x 3] by (auto simp: numeral_3_eq_3 power2_eq_square)

corollary ln_2_less_1: "ln 2 < (1::real)"
proof -
  have "2 < 5/(2::real)" by simp
  also have "5/2 ≤ exp (1::real)" using exp_lower_taylor_quadratic[of 1, simplified] by simp
  finally have "exp (ln 2) < exp (1::real)" by simp
  thus "ln 2 < (1::real)" by (subst (asm) exp_less_cancel_iff) simp
qed

subsection ‹Version for Sine Function›

lemma mod_exhaust_less_4: "m mod 4 = 0 | m mod 4 = 1 | m mod 4 = 2 | m mod 4 = 3"
  for m :: nat
  by auto

lemma Suc_Suc_mult_two_diff_two [simp]: "n ≠ 0 ⟹ Suc (Suc (2 * n - 2)) = 2 * n"
  by (induct n) auto

lemma lemma_Suc_Suc_4n_diff_2 [simp]: "n ≠ 0 ⟹ Suc (Suc (4 * n - 2)) = 4 * n"
  by (induct n) auto

lemma Suc_mult_two_diff_one [simp]: "n ≠ 0 ⟹ Suc (2 * n - 1) = 2 * n"
  by (induct n) auto


text ‹It is unclear why so many variant results are needed.›

lemma sin_expansion_lemma: "sin (x + real (Suc m) * pi / 2) = cos (x + real m * pi / 2)"
  by (auto simp: cos_add sin_add add_divide_distrib distrib_right)

lemma Maclaurin_sin_expansion2:
  "∃t. ¦t¦ ≤ ¦x¦ ∧
    sin x = (∑m<n. sin_coeff m * x ^ m) + (sin (t + 1/2 * real n * pi) / fact n) * x ^ n"
  using Maclaurin_all_lt_objl
    [where f = sin and n = n and x = x and diff = "λn x. sin (x + 1/2 * real n * pi)"]
  apply safe
      apply simp
     apply (simp add: sin_expansion_lemma del: of_nat_Suc)
     apply (force intro!: derivative_eq_intros)
    apply (subst (asm) sum.neutral; auto)
   apply (rule ccontr)
   apply simp
   apply (drule_tac x = x in spec)
   apply simp
  apply (erule ssubst)
  apply (rule_tac x = t in exI)
  apply simp
  apply (rule sum.cong[OF refl])
  apply (auto simp add: sin_coeff_def sin_zero_iff elim: oddE simp del: of_nat_Suc)
  done

lemma Maclaurin_sin_expansion:
  "∃t. sin x = (∑m<n. sin_coeff m * x ^ m) + (sin (t + 1/2 * real n * pi) / fact n) * x ^ n"
  using Maclaurin_sin_expansion2 [of x n] by blast

lemma Maclaurin_sin_expansion3:
  "n > 0 ⟹ 0 < x ⟹
    ∃t. 0 < t ∧ t < x ∧
       sin x = (∑m<n. sin_coeff m * x ^ m) + (sin (t + 1/2 * real n * pi) / fact n) * x ^ n"
  using Maclaurin_objl
    [where f = sin and n = n and h = x and diff = "λn x. sin (x + 1/2 * real n * pi)"]
  apply safe
    apply simp
   apply (simp (no_asm) add: sin_expansion_lemma del: of_nat_Suc)
   apply (force intro!: derivative_eq_intros)
  apply (erule ssubst)
  apply (rule_tac x = t in exI)
  apply simp
  apply (rule sum.cong[OF refl])
  apply (auto simp add: sin_coeff_def sin_zero_iff elim: oddE simp del: of_nat_Suc)
  done

lemma Maclaurin_sin_expansion4:
  "0 < x ⟹
    ∃t. 0 < t ∧ t ≤ x ∧
      sin x = (∑m<n. sin_coeff m * x ^ m) + (sin (t + 1/2 * real n * pi) / fact n) * x ^ n"
  using Maclaurin2_objl
    [where f = sin and n = n and h = x and diff = "λn x. sin (x + 1/2 * real n * pi)"]
  apply safe
    apply simp
   apply (simp (no_asm) add: sin_expansion_lemma del: of_nat_Suc)
   apply (force intro!: derivative_eq_intros)
  apply (erule ssubst)
  apply (rule_tac x = t in exI)
  apply simp
  apply (rule sum.cong[OF refl])
  apply (auto simp add: sin_coeff_def sin_zero_iff elim: oddE simp del: of_nat_Suc)
  done


subsection ‹Maclaurin Expansion for Cosine Function›

lemma sumr_cos_zero_one [simp]: "(∑m<Suc n. cos_coeff m * 0 ^ m) = 1"
  by (induct n) auto

lemma cos_expansion_lemma: "cos (x + real (Suc m) * pi / 2) = - sin (x + real m * pi / 2)"
  by (auto simp: cos_add sin_add distrib_right add_divide_distrib)

lemma Maclaurin_cos_expansion:
  "∃t::real. ¦t¦ ≤ ¦x¦ ∧
    cos x = (∑m<n. cos_coeff m * x ^ m) + (cos(t + 1/2 * real n * pi) / fact n) * x ^ n"
  using Maclaurin_all_lt_objl
    [where f = cos and n = n and x = x and diff = "λn x. cos (x + 1/2 * real n * pi)"]
  apply safe
      apply simp
     apply (simp add: cos_expansion_lemma del: of_nat_Suc)
    apply (cases n)
     apply simp
    apply (simp del: sum_lessThan_Suc)
   apply (rule ccontr)
   apply simp
   apply (drule_tac x = x in spec)
   apply simp
  apply (erule ssubst)
  apply (rule_tac x = t in exI)
  apply simp
  apply (rule sum.cong[OF refl])
  apply (auto simp add: cos_coeff_def cos_zero_iff elim: evenE)
  done

lemma Maclaurin_cos_expansion2:
  "0 < x ⟹ n > 0 ⟹
    ∃t. 0 < t ∧ t < x ∧
      cos x = (∑m<n. cos_coeff m * x ^ m) + (cos (t + 1/2 * real n * pi) / fact n) * x ^ n"
  using Maclaurin_objl
    [where f = cos and n = n and h = x and diff = "λn x. cos (x + 1/2 * real n * pi)"]
  apply safe
    apply simp
   apply (simp add: cos_expansion_lemma del: of_nat_Suc)
  apply (erule ssubst)
  apply (rule_tac x = t in exI)
  apply simp
  apply (rule sum.cong[OF refl])
  apply (auto simp: cos_coeff_def cos_zero_iff elim: evenE)
  done

lemma Maclaurin_minus_cos_expansion:
  "x < 0 ⟹ n > 0 ⟹
    ∃t. x < t ∧ t < 0 ∧
      cos x = (∑m<n. cos_coeff m * x ^ m) + ((cos (t + 1/2 * real n * pi) / fact n) * x ^ n)"
  using Maclaurin_minus_objl
    [where f = cos and n = n and h = x and diff = "λn x. cos (x + 1/2 * real n *pi)"]
  apply safe
    apply simp
   apply (simp add: cos_expansion_lemma del: of_nat_Suc)
  apply (erule ssubst)
  apply (rule_tac x = t in exI)
  apply simp
  apply (rule sum.cong[OF refl])
  apply (auto simp: cos_coeff_def cos_zero_iff elim: evenE)
  done


(* Version for ln(1 +/- x). Where is it?? *)


lemma sin_bound_lemma: "x = y ⟹ ¦u¦ ≤ v ⟹ ¦(x + u) - y¦ ≤ v"
  for x y u v :: real
  by auto

lemma Maclaurin_sin_bound: "¦sin x - (∑m<n. sin_coeff m * x ^ m)¦ ≤ inverse (fact n) * ¦x¦ ^ n"
proof -
  have est: "x ≤ 1 ⟹ 0 ≤ y ⟹ x * y ≤ 1 * y" for x y :: real
    by (rule mult_right_mono) simp_all
  let ?diff = "λ(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"
  have diff_0: "?diff 0 = sin" by simp
  have DERIV_diff: "∀m x. DERIV (?diff m) x :> ?diff (Suc m) x"
    apply clarify
    apply (subst (1 2 3) mod_Suc_eq_Suc_mod)
    apply (cut_tac m=m in mod_exhaust_less_4)
    apply safe
       apply (auto intro!: derivative_eq_intros)
    done
  from Maclaurin_all_le [OF diff_0 DERIV_diff]
  obtain t where t1: "¦t¦ ≤ ¦x¦"
    and t2: "sin x = (∑m<n. ?diff m 0 / (fact m) * x ^ m) + ?diff n t / (fact n) * x ^ n"
    by fast
  have diff_m_0: "⋀m. ?diff m 0 = (if even m then 0 else (- 1) ^ ((m - Suc 0) div 2))"
    apply (subst even_even_mod_4_iff)
    apply (cut_tac m=m in mod_exhaust_less_4)
    apply (elim disjE)
       apply simp_all
     apply (safe dest!: mod_eqD)
     apply simp_all
    done
  show ?thesis
    unfolding sin_coeff_def
    apply (subst t2)
    apply (rule sin_bound_lemma)
     apply (rule sum.cong[OF refl])
     apply (subst diff_m_0, simp)
    using est
    apply (auto intro: mult_right_mono [where b=1, simplified] mult_right_mono
        simp: ac_simps divide_inverse power_abs [symmetric] abs_mult)
    done
qed


section ‹Taylor series›

text ‹
  We use MacLaurin and the translation of the expansion point ‹c› to ‹0›
  to prove Taylor's theorem.
›

lemma taylor_up:
  assumes INIT: "n > 0" "diff 0 = f"
    and DERIV: "∀m t. m < n ∧ a ≤ t ∧ t ≤ b ⟶ DERIV (diff m) t :> (diff (Suc m) t)"
    and INTERV: "a ≤ c" "c < b"
  shows "∃t::real. c < t ∧ t < b ∧
    f b = (∑m<n. (diff m c / fact m) * (b - c)^m) + (diff n t / fact n) * (b - c)^n"
proof -
  from INTERV have "0 < b - c" by arith
  moreover from INIT have "n > 0" "(λm x. diff m (x + c)) 0 = (λx. f (x + c))"
    by auto
  moreover
  have "∀m t. m < n ∧ 0 ≤ t ∧ t ≤ b - c ⟶ DERIV (λx. diff m (x + c)) t :> diff (Suc m) (t + c)"
  proof (intro strip)
    fix m t
    assume "m < n ∧ 0 ≤ t ∧ t ≤ b - c"
    with DERIV and INTERV have "DERIV (diff m) (t + c) :> diff (Suc m) (t + c)"
      by auto
    moreover from DERIV_ident and DERIV_const have "DERIV (λx. x + c) t :> 1 + 0"
      by (rule DERIV_add)
    ultimately have "DERIV (λx. diff m (x + c)) t :> diff (Suc m) (t + c) * (1 + 0)"
      by (rule DERIV_chain2)
    then show "DERIV (λx. diff m (x + c)) t :> diff (Suc m) (t + c)"
      by simp
  qed
  ultimately obtain x where
    "0 < x ∧ x < b - c ∧
      f (b - c + c) =
        (∑m<n. diff m (0 + c) / fact m * (b - c) ^ m) + diff n (x + c) / fact n * (b - c) ^ n"
     by (rule Maclaurin [THEN exE])
   then have "c < x + c ∧ x + c < b ∧ f b =
     (∑m<n. diff m c / fact m * (b - c) ^ m) + diff n (x + c) / fact n * (b - c) ^ n"
    by fastforce
  then show ?thesis by fastforce
qed

lemma taylor_down:
  fixes a :: real and n :: nat
  assumes INIT: "n > 0" "diff 0 = f"
    and DERIV: "(∀m t. m < n ∧ a ≤ t ∧ t ≤ b ⟶ DERIV (diff m) t :> diff (Suc m) t)"
    and INTERV: "a < c" "c ≤ b"
  shows "∃t. a < t ∧ t < c ∧
    f a = (∑m<n. (diff m c / fact m) * (a - c)^m) + (diff n t / fact n) * (a - c)^n"
proof -
  from INTERV have "a-c < 0" by arith
  moreover from INIT have "n > 0" "(λm x. diff m (x + c)) 0 = (λx. f (x + c))"
    by auto
  moreover
  have "∀m t. m < n ∧ a - c ≤ t ∧ t ≤ 0 ⟶ DERIV (λx. diff m (x + c)) t :> diff (Suc m) (t + c)"
  proof (rule allI impI)+
    fix m t
    assume "m < n ∧ a - c ≤ t ∧ t ≤ 0"
    with DERIV and INTERV have "DERIV (diff m) (t + c) :> diff (Suc m) (t + c)"
      by auto
    moreover from DERIV_ident and DERIV_const have "DERIV (λx. x + c) t :> 1 + 0"
      by (rule DERIV_add)
    ultimately have "DERIV (λx. diff m (x + c)) t :> diff (Suc m) (t + c) * (1 + 0)"
      by (rule DERIV_chain2)
    then show "DERIV (λx. diff m (x + c)) t :> diff (Suc m) (t + c)"
      by simp
  qed
  ultimately obtain x where
    "a - c < x ∧ x < 0 ∧
      f (a - c + c) =
        (∑m<n. diff m (0 + c) / fact m * (a - c) ^ m) + diff n (x + c) / fact n * (a - c) ^ n"
    by (rule Maclaurin_minus [THEN exE])
  then have "a < x + c ∧ x + c < c ∧
    f a = (∑m<n. diff m c / fact m * (a - c) ^ m) + diff n (x + c) / fact n * (a - c) ^ n"
    by fastforce
  then show ?thesis by fastforce
qed

theorem taylor:
  fixes a :: real and n :: nat
  assumes INIT: "n > 0" "diff 0 = f"
    and DERIV: "∀m t. m < n ∧ a ≤ t ∧ t ≤ b ⟶ DERIV (diff m) t :> diff (Suc m) t"
    and INTERV: "a ≤ c " "c ≤ b" "a ≤ x" "x ≤ b" "x ≠ c"
  shows "∃t.
    (if x < c then x < t ∧ t < c else c < t ∧ t < x) ∧
    f x = (∑m<n. (diff m c / fact m) * (x - c)^m) + (diff n t / fact n) * (x - c)^n"
proof (cases "x < c")
  case True
  note INIT
  moreover have "∀m t. m < n ∧ x ≤ t ∧ t ≤ b ⟶ DERIV (diff m) t :> diff (Suc m) t"
    using DERIV and INTERV by fastforce
  moreover note True
  moreover from INTERV have "c ≤ b"
    by simp
  ultimately have "∃t>x. t < c ∧ f x =
    (∑m<n. diff m c / (fact m) * (x - c) ^ m) + diff n t / (fact n) * (x - c) ^ n"
    by (rule taylor_down)
  with True show ?thesis by simp
next
  case False
  note INIT
  moreover have "∀m t. m < n ∧ a ≤ t ∧ t ≤ x ⟶ DERIV (diff m) t :> diff (Suc m) t"
    using DERIV and INTERV by fastforce
  moreover from INTERV have "a ≤ c"
    by arith
  moreover from False and INTERV have "c < x"
    by arith
  ultimately have "∃t>c. t < x ∧ f x =
    (∑m<n. diff m c / (fact m) * (x - c) ^ m) + diff n t / (fact n) * (x - c) ^ n"
    by (rule taylor_up)
  with False show ?thesis by simp
qed

end