Theory Transcendental

theory Transcendental
imports Series NthRoot
(*  Title:      HOL/Transcendental.thy
    Author:     Jacques D. Fleuriot, University of Cambridge, University of Edinburgh
    Author:     Lawrence C Paulson
    Author:     Jeremy Avigad
*)

section ‹Power Series, Transcendental Functions etc.›

theory Transcendental
imports Series Deriv NthRoot
begin

text ‹A theorem about the factcorial function on the reals.›

lemma square_fact_le_2_fact: "fact n * fact n ≤ (fact (2 * n) :: real)"
proof (induct n)
  case 0
  then show ?case by simp
next
  case (Suc n)
  have "(fact (Suc n)) * (fact (Suc n)) = of_nat (Suc n) * of_nat (Suc n) * (fact n * fact n :: real)"
    by (simp add: field_simps)
  also have "… ≤ of_nat (Suc n) * of_nat (Suc n) * fact (2 * n)"
    by (rule mult_left_mono [OF Suc]) simp
  also have "… ≤ of_nat (Suc (Suc (2 * n))) * of_nat (Suc (2 * n)) * fact (2 * n)"
    by (rule mult_right_mono)+ (auto simp: field_simps)
  also have "… = fact (2 * Suc n)" by (simp add: field_simps)
  finally show ?case .
qed

lemma fact_in_Reals: "fact n ∈ ℝ"
  by (induction n) auto

lemma of_real_fact [simp]: "of_real (fact n) = fact n"
  by (metis of_nat_fact of_real_of_nat_eq)

lemma pochhammer_of_real: "pochhammer (of_real x) n = of_real (pochhammer x n)"
  by (simp add: pochhammer_prod)

lemma norm_fact [simp]: "norm (fact n :: 'a::real_normed_algebra_1) = fact n"
proof -
  have "(fact n :: 'a) = of_real (fact n)"
    by simp
  also have "norm … = fact n"
    by (subst norm_of_real) simp
  finally show ?thesis .
qed

lemma root_test_convergence:
  fixes f :: "nat ⇒ 'a::banach"
  assumes f: "(λn. root n (norm (f n))) ⇢ x" ― ‹could be weakened to lim sup›
    and "x < 1"
  shows "summable f"
proof -
  have "0 ≤ x"
    by (rule LIMSEQ_le[OF tendsto_const f]) (auto intro!: exI[of _ 1])
  from ‹x < 1› obtain z where z: "x < z" "z < 1"
    by (metis dense)
  from f ‹x < z› have "eventually (λn. root n (norm (f n)) < z) sequentially"
    by (rule order_tendstoD)
  then have "eventually (λn. norm (f n) ≤ z^n) sequentially"
    using eventually_ge_at_top
  proof eventually_elim
    fix n
    assume less: "root n (norm (f n)) < z" and n: "1 ≤ n"
    from power_strict_mono[OF less, of n] n show "norm (f n) ≤ z ^ n"
      by simp
  qed
  then show "summable f"
    unfolding eventually_sequentially
    using z ‹0 ≤ x› by (auto intro!: summable_comparison_test[OF _  summable_geometric])
qed

subsection ‹More facts about binomial coefficients›

text ‹
  These facts could have been proven before, but having real numbers
  makes the proofs a lot easier.
›

lemma central_binomial_odd:
  "odd n ⟹ n choose (Suc (n div 2)) = n choose (n div 2)"
proof -
  assume "odd n"
  hence "Suc (n div 2) ≤ n" by presburger
  hence "n choose (Suc (n div 2)) = n choose (n - Suc (n div 2))"
    by (rule binomial_symmetric)
  also from ‹odd n› have "n - Suc (n div 2) = n div 2" by presburger
  finally show ?thesis .
qed

lemma binomial_less_binomial_Suc:
  assumes k: "k < n div 2"
  shows   "n choose k < n choose (Suc k)"
proof -
  from k have k': "k ≤ n" "Suc k ≤ n" by simp_all
  from k' have "real (n choose k) = fact n / (fact k * fact (n - k))"
    by (simp add: binomial_fact)
  also from k' have "n - k = Suc (n - Suc k)" by simp
  also from k' have "fact … = (real n - real k) * fact (n - Suc k)"
    by (subst fact_Suc) (simp_all add: of_nat_diff)
  also from k have "fact k = fact (Suc k) / (real k + 1)" by (simp add: field_simps)
  also have "fact n / (fact (Suc k) / (real k + 1) * ((real n - real k) * fact (n - Suc k))) =
               (n choose (Suc k)) * ((real k + 1) / (real n - real k))"
    using k by (simp add: divide_simps binomial_fact)
  also from assms have "(real k + 1) / (real n - real k) < 1" by simp
  finally show ?thesis using k by (simp add: mult_less_cancel_left)
qed

lemma binomial_strict_mono:
  assumes "k < k'" "2*k' ≤ n"
  shows   "n choose k < n choose k'"
proof -
  from assms have "k ≤ k' - 1" by simp
  thus ?thesis
  proof (induction rule: inc_induct)
    case base
    with assms binomial_less_binomial_Suc[of "k' - 1" n]
      show ?case by simp
  next
    case (step k)
    from step.prems step.hyps assms have "n choose k < n choose (Suc k)"
      by (intro binomial_less_binomial_Suc) simp_all
    also have "… < n choose k'" by (rule step.IH)
    finally show ?case .
  qed
qed

lemma binomial_mono:
  assumes "k ≤ k'" "2*k' ≤ n"
  shows   "n choose k ≤ n choose k'"
  using assms binomial_strict_mono[of k k' n] by (cases "k = k'") simp_all

lemma binomial_strict_antimono:
  assumes "k < k'" "2 * k ≥ n" "k' ≤ n"
  shows   "n choose k > n choose k'"
proof -
  from assms have "n choose (n - k) > n choose (n - k')"
    by (intro binomial_strict_mono) (simp_all add: algebra_simps)
  with assms show ?thesis by (simp add: binomial_symmetric [symmetric])
qed

lemma binomial_antimono:
  assumes "k ≤ k'" "k ≥ n div 2" "k' ≤ n"
  shows   "n choose k ≥ n choose k'"
proof (cases "k = k'")
  case False
  note not_eq = False
  show ?thesis
  proof (cases "k = n div 2 ∧ odd n")
    case False
    with assms(2) have "2*k ≥ n" by presburger
    with not_eq assms binomial_strict_antimono[of k k' n]
      show ?thesis by simp
  next
    case True
    have "n choose k' ≤ n choose (Suc (n div 2))"
    proof (cases "k' = Suc (n div 2)")
      case False
      with assms True not_eq have "Suc (n div 2) < k'" by simp
      with assms binomial_strict_antimono[of "Suc (n div 2)" k' n] True
        show ?thesis by auto
    qed simp_all
    also from True have "… = n choose k" by (simp add: central_binomial_odd)
    finally show ?thesis .
  qed
qed simp_all

lemma binomial_maximum: "n choose k ≤ n choose (n div 2)"
proof -
  have "k ≤ n div 2 ⟷ 2*k ≤ n" by linarith
  consider "2*k ≤ n" | "2*k ≥ n" "k ≤ n" | "k > n" by linarith
  thus ?thesis
  proof cases
    case 1
    thus ?thesis by (intro binomial_mono) linarith+
  next
    case 2
    thus ?thesis by (intro binomial_antimono) simp_all
  qed (simp_all add: binomial_eq_0)
qed

lemma binomial_maximum': "(2*n) choose k ≤ (2*n) choose n"
  using binomial_maximum[of "2*n"] by simp

lemma central_binomial_lower_bound:
  assumes "n > 0"
  shows   "4^n / (2*real n) ≤ real ((2*n) choose n)"
proof -
  from binomial[of 1 1 "2*n"]
    have "4 ^ n = (∑k≤2*n. (2*n) choose k)"
    by (simp add: power_mult power2_eq_square One_nat_def [symmetric] del: One_nat_def)
  also have "{..2*n} = {0<..<2*n} ∪ {0,2*n}" by auto
  also have "(∑k∈…. (2*n) choose k) =
             (∑k∈{0<..<2*n}. (2*n) choose k) + (∑k∈{0,2*n}. (2*n) choose k)"
    by (subst sum.union_disjoint) auto
  also have "(∑k∈{0,2*n}. (2*n) choose k) ≤ (∑k≤1. (n choose k)2)"
    by (cases n) simp_all
  also from assms have "… ≤ (∑k≤n. (n choose k)2)"
    by (intro sum_mono2) auto
  also have "… = (2*n) choose n" by (rule choose_square_sum)
  also have "(∑k∈{0<..<2*n}. (2*n) choose k) ≤ (∑k∈{0<..<2*n}. (2*n) choose n)"
    by (intro sum_mono binomial_maximum')
  also have "… = card {0<..<2*n} * ((2*n) choose n)" by simp
  also have "card {0<..<2*n} ≤ 2*n - 1" by (cases n) simp_all
  also have "(2 * n - 1) * (2 * n choose n) + (2 * n choose n) = ((2*n) choose n) * (2*n)"
    using assms by (simp add: algebra_simps)
  finally have "4 ^ n ≤ (2 * n choose n) * (2 * n)" by simp_all
  hence "real (4 ^ n) ≤ real ((2 * n choose n) * (2 * n))"
    by (subst of_nat_le_iff)
  with assms show ?thesis by (simp add: field_simps)
qed


subsection ‹Properties of Power Series›

lemma powser_zero [simp]: "(∑n. f n * 0 ^ n) = f 0"
  for f :: "nat ⇒ 'a::real_normed_algebra_1"
proof -
  have "(∑n<1. f n * 0 ^ n) = (∑n. f n * 0 ^ n)"
    by (subst suminf_finite[where N="{0}"]) (auto simp: power_0_left)
  then show ?thesis by simp
qed

lemma powser_sums_zero: "(λn. a n * 0^n) sums a 0"
  for a :: "nat ⇒ 'a::real_normed_div_algebra"
  using sums_finite [of "{0}" "λn. a n * 0 ^ n"]
  by simp

lemma powser_sums_zero_iff [simp]: "(λn. a n * 0^n) sums x ⟷ a 0 = x"
  for a :: "nat ⇒ 'a::real_normed_div_algebra"
  using powser_sums_zero sums_unique2 by blast

text ‹
  Power series has a circle or radius of convergence: if it sums for ‹x›,
  then it sums absolutely for ‹z› with @{term "¦z¦ < ¦x¦"}.›

lemma powser_insidea:
  fixes x z :: "'a::real_normed_div_algebra"
  assumes 1: "summable (λn. f n * x^n)"
    and 2: "norm z < norm x"
  shows "summable (λn. norm (f n * z ^ n))"
proof -
  from 2 have x_neq_0: "x ≠ 0" by clarsimp
  from 1 have "(λn. f n * x^n) ⇢ 0"
    by (rule summable_LIMSEQ_zero)
  then have "convergent (λn. f n * x^n)"
    by (rule convergentI)
  then have "Cauchy (λn. f n * x^n)"
    by (rule convergent_Cauchy)
  then have "Bseq (λn. f n * x^n)"
    by (rule Cauchy_Bseq)
  then obtain K where 3: "0 < K" and 4: "∀n. norm (f n * x^n) ≤ K"
    by (auto simp: Bseq_def)
  have "∃N. ∀n≥N. norm (norm (f n * z ^ n)) ≤ K * norm (z ^ n) * inverse (norm (x^n))"
  proof (intro exI allI impI)
    fix n :: nat
    assume "0 ≤ n"
    have "norm (norm (f n * z ^ n)) * norm (x^n) =
          norm (f n * x^n) * norm (z ^ n)"
      by (simp add: norm_mult abs_mult)
    also have "… ≤ K * norm (z ^ n)"
      by (simp only: mult_right_mono 4 norm_ge_zero)
    also have "… = K * norm (z ^ n) * (inverse (norm (x^n)) * norm (x^n))"
      by (simp add: x_neq_0)
    also have "… = K * norm (z ^ n) * inverse (norm (x^n)) * norm (x^n)"
      by (simp only: mult.assoc)
    finally show "norm (norm (f n * z ^ n)) ≤ K * norm (z ^ n) * inverse (norm (x^n))"
      by (simp add: mult_le_cancel_right x_neq_0)
  qed
  moreover have "summable (λn. K * norm (z ^ n) * inverse (norm (x^n)))"
  proof -
    from 2 have "norm (norm (z * inverse x)) < 1"
      using x_neq_0
      by (simp add: norm_mult nonzero_norm_inverse divide_inverse [where 'a=real, symmetric])
    then have "summable (λn. norm (z * inverse x) ^ n)"
      by (rule summable_geometric)
    then have "summable (λn. K * norm (z * inverse x) ^ n)"
      by (rule summable_mult)
    then show "summable (λn. K * norm (z ^ n) * inverse (norm (x^n)))"
      using x_neq_0
      by (simp add: norm_mult nonzero_norm_inverse power_mult_distrib
          power_inverse norm_power mult.assoc)
  qed
  ultimately show "summable (λn. norm (f n * z ^ n))"
    by (rule summable_comparison_test)
qed

lemma powser_inside:
  fixes f :: "nat ⇒ 'a::{real_normed_div_algebra,banach}"
  shows
    "summable (λn. f n * (x^n)) ⟹ norm z < norm x ⟹
      summable (λn. f n * (z ^ n))"
  by (rule powser_insidea [THEN summable_norm_cancel])

lemma powser_times_n_limit_0:
  fixes x :: "'a::{real_normed_div_algebra,banach}"
  assumes "norm x < 1"
    shows "(λn. of_nat n * x ^ n) ⇢ 0"
proof -
  have "norm x / (1 - norm x) ≥ 0"
    using assms by (auto simp: divide_simps)
  moreover obtain N where N: "norm x / (1 - norm x) < of_int N"
    using ex_le_of_int by (meson ex_less_of_int)
  ultimately have N0: "N>0"
    by auto
  then have *: "real_of_int (N + 1) * norm x / real_of_int N < 1"
    using N assms by (auto simp: field_simps)
  have **: "real_of_int N * (norm x * (real_of_nat (Suc n) * norm (x ^ n))) ≤
      real_of_nat n * (norm x * ((1 + N) * norm (x ^ n)))" if "N ≤ int n" for n :: nat
  proof -
    from that have "real_of_int N * real_of_nat (Suc n) ≤ real_of_nat n * real_of_int (1 + N)"
      by (simp add: algebra_simps)
    then have "(real_of_int N * real_of_nat (Suc n)) * (norm x * norm (x ^ n)) ≤
        (real_of_nat n *  (1 + N)) * (norm x * norm (x ^ n))"
      using N0 mult_mono by fastforce
    then show ?thesis
      by (simp add: algebra_simps)
  qed
  show ?thesis using *
    by (rule summable_LIMSEQ_zero [OF summable_ratio_test, where N1="nat N"])
      (simp add: N0 norm_mult field_simps ** del: of_nat_Suc of_int_add)
qed

corollary lim_n_over_pown:
  fixes x :: "'a::{real_normed_field,banach}"
  shows "1 < norm x ⟹ ((λn. of_nat n / x^n) ⤏ 0) sequentially"
  using powser_times_n_limit_0 [of "inverse x"]
  by (simp add: norm_divide divide_simps)

lemma sum_split_even_odd:
  fixes f :: "nat ⇒ real"
  shows "(∑i<2 * n. if even i then f i else g i) = (∑i<n. f (2 * i)) + (∑i<n. g (2 * i + 1))"
proof (induct n)
  case 0
  then show ?case by simp
next
  case (Suc n)
  have "(∑i<2 * Suc n. if even i then f i else g i) =
    (∑i<n. f (2 * i)) + (∑i<n. g (2 * i + 1)) + (f (2 * n) + g (2 * n + 1))"
    using Suc.hyps unfolding One_nat_def by auto
  also have "… = (∑i<Suc n. f (2 * i)) + (∑i<Suc n. g (2 * i + 1))"
    by auto
  finally show ?case .
qed

lemma sums_if':
  fixes g :: "nat ⇒ real"
  assumes "g sums x"
  shows "(λ n. if even n then 0 else g ((n - 1) div 2)) sums x"
  unfolding sums_def
proof (rule LIMSEQ_I)
  fix r :: real
  assume "0 < r"
  from ‹g sums x›[unfolded sums_def, THEN LIMSEQ_D, OF this]
  obtain no where no_eq: "⋀n. n ≥ no ⟹ (norm (sum g {..<n} - x) < r)"
    by blast

  let ?SUM = "λ m. ∑i<m. if even i then 0 else g ((i - 1) div 2)"
  have "(norm (?SUM m - x) < r)" if "m ≥ 2 * no" for m
  proof -
    from that have "m div 2 ≥ no" by auto
    have sum_eq: "?SUM (2 * (m div 2)) = sum g {..< m div 2}"
      using sum_split_even_odd by auto
    then have "(norm (?SUM (2 * (m div 2)) - x) < r)"
      using no_eq unfolding sum_eq using ‹m div 2 ≥ no› by auto
    moreover
    have "?SUM (2 * (m div 2)) = ?SUM m"
    proof (cases "even m")
      case True
      then show ?thesis
        by (auto simp: even_two_times_div_two)
    next
      case False
      then have eq: "Suc (2 * (m div 2)) = m" by simp
      then have "even (2 * (m div 2))" using ‹odd m› by auto
      have "?SUM m = ?SUM (Suc (2 * (m div 2)))" unfolding eq ..
      also have "… = ?SUM (2 * (m div 2))" using ‹even (2 * (m div 2))› by auto
      finally show ?thesis by auto
    qed
    ultimately show ?thesis by auto
  qed
  then show "∃no. ∀ m ≥ no. norm (?SUM m - x) < r"
    by blast
qed

lemma sums_if:
  fixes g :: "nat ⇒ real"
  assumes "g sums x" and "f sums y"
  shows "(λ n. if even n then f (n div 2) else g ((n - 1) div 2)) sums (x + y)"
proof -
  let ?s = "λ n. if even n then 0 else f ((n - 1) div 2)"
  have if_sum: "(if B then (0 :: real) else E) + (if B then T else 0) = (if B then T else E)"
    for B T E
    by (cases B) auto
  have g_sums: "(λ n. if even n then 0 else g ((n - 1) div 2)) sums x"
    using sums_if'[OF ‹g sums x›] .
  have if_eq: "⋀B T E. (if ¬ B then T else E) = (if B then E else T)"
    by auto
  have "?s sums y" using sums_if'[OF ‹f sums y›] .
  from this[unfolded sums_def, THEN LIMSEQ_Suc]
  have "(λn. if even n then f (n div 2) else 0) sums y"
    by (simp add: lessThan_Suc_eq_insert_0 sum_atLeast1_atMost_eq image_Suc_lessThan
        if_eq sums_def cong del: if_weak_cong)
  from sums_add[OF g_sums this] show ?thesis
    by (simp only: if_sum)
qed

subsection ‹Alternating series test / Leibniz formula›
(* FIXME: generalise these results from the reals via type classes? *)

lemma sums_alternating_upper_lower:
  fixes a :: "nat ⇒ real"
  assumes mono: "⋀n. a (Suc n) ≤ a n"
    and a_pos: "⋀n. 0 ≤ a n"
    and "a ⇢ 0"
  shows "∃l. ((∀n. (∑i<2*n. (- 1)^i*a i) ≤ l) ∧ (λ n. ∑i<2*n. (- 1)^i*a i) ⇢ l) ∧
             ((∀n. l ≤ (∑i<2*n + 1. (- 1)^i*a i)) ∧ (λ n. ∑i<2*n + 1. (- 1)^i*a i) ⇢ l)"
  (is "∃l. ((∀n. ?f n ≤ l) ∧ _) ∧ ((∀n. l ≤ ?g n) ∧ _)")
proof (rule nested_sequence_unique)
  have fg_diff: "⋀n. ?f n - ?g n = - a (2 * n)" by auto

  show "∀n. ?f n ≤ ?f (Suc n)"
  proof
    show "?f n ≤ ?f (Suc n)" for n
      using mono[of "2*n"] by auto
  qed
  show "∀n. ?g (Suc n) ≤ ?g n"
  proof
    show "?g (Suc n) ≤ ?g n" for n
      using mono[of "Suc (2*n)"] by auto
  qed
  show "∀n. ?f n ≤ ?g n"
  proof
    show "?f n ≤ ?g n" for n
      using fg_diff a_pos by auto
  qed
  show "(λn. ?f n - ?g n) ⇢ 0"
    unfolding fg_diff
  proof (rule LIMSEQ_I)
    fix r :: real
    assume "0 < r"
    with ‹a ⇢ 0›[THEN LIMSEQ_D] obtain N where "⋀ n. n ≥ N ⟹ norm (a n - 0) < r"
      by auto
    then have "∀n ≥ N. norm (- a (2 * n) - 0) < r"
      by auto
    then show "∃N. ∀n ≥ N. norm (- a (2 * n) - 0) < r"
      by auto
  qed
qed

lemma summable_Leibniz':
  fixes a :: "nat ⇒ real"
  assumes a_zero: "a ⇢ 0"
    and a_pos: "⋀n. 0 ≤ a n"
    and a_monotone: "⋀n. a (Suc n) ≤ a n"
  shows summable: "summable (λ n. (-1)^n * a n)"
    and "⋀n. (∑i<2*n. (-1)^i*a i) ≤ (∑i. (-1)^i*a i)"
    and "(λn. ∑i<2*n. (-1)^i*a i) ⇢ (∑i. (-1)^i*a i)"
    and "⋀n. (∑i. (-1)^i*a i) ≤ (∑i<2*n+1. (-1)^i*a i)"
    and "(λn. ∑i<2*n+1. (-1)^i*a i) ⇢ (∑i. (-1)^i*a i)"
proof -
  let ?S = "λn. (-1)^n * a n"
  let ?P = "λn. ∑i<n. ?S i"
  let ?f = "λn. ?P (2 * n)"
  let ?g = "λn. ?P (2 * n + 1)"
  obtain l :: real
    where below_l: "∀ n. ?f n ≤ l"
      and "?f ⇢ l"
      and above_l: "∀ n. l ≤ ?g n"
      and "?g ⇢ l"
    using sums_alternating_upper_lower[OF a_monotone a_pos a_zero] by blast

  let ?Sa = "λm. ∑n<m. ?S n"
  have "?Sa ⇢ l"
  proof (rule LIMSEQ_I)
    fix r :: real
    assume "0 < r"
    with ‹?f ⇢ l›[THEN LIMSEQ_D]
    obtain f_no where f: "⋀n. n ≥ f_no ⟹ norm (?f n - l) < r"
      by auto
    from ‹0 < r› ‹?g ⇢ l›[THEN LIMSEQ_D]
    obtain g_no where g: "⋀n. n ≥ g_no ⟹ norm (?g n - l) < r"
      by auto
    have "norm (?Sa n - l) < r" if "n ≥ (max (2 * f_no) (2 * g_no))" for n
    proof -
      from that have "n ≥ 2 * f_no" and "n ≥ 2 * g_no" by auto
      show ?thesis
      proof (cases "even n")
        case True
        then have n_eq: "2 * (n div 2) = n"
          by (simp add: even_two_times_div_two)
        with ‹n ≥ 2 * f_no› have "n div 2 ≥ f_no"
          by auto
        from f[OF this] show ?thesis
          unfolding n_eq atLeastLessThanSuc_atLeastAtMost .
      next
        case False
        then have "even (n - 1)" by simp
        then have n_eq: "2 * ((n - 1) div 2) = n - 1"
          by (simp add: even_two_times_div_two)
        then have range_eq: "n - 1 + 1 = n"
          using odd_pos[OF False] by auto
        from n_eq ‹n ≥ 2 * g_no› have "(n - 1) div 2 ≥ g_no"
          by auto
        from g[OF this] show ?thesis
          by (simp only: n_eq range_eq)
      qed
    qed
    then show "∃no. ∀n ≥ no. norm (?Sa n - l) < r" by blast
  qed
  then have sums_l: "(λi. (-1)^i * a i) sums l"
    by (simp only: sums_def)
  then show "summable ?S"
    by (auto simp: summable_def)

  have "l = suminf ?S" by (rule sums_unique[OF sums_l])

  fix n
  show "suminf ?S ≤ ?g n"
    unfolding sums_unique[OF sums_l, symmetric] using above_l by auto
  show "?f n ≤ suminf ?S"
    unfolding sums_unique[OF sums_l, symmetric] using below_l by auto
  show "?g ⇢ suminf ?S"
    using ‹?g ⇢ l› ‹l = suminf ?S› by auto
  show "?f ⇢ suminf ?S"
    using ‹?f ⇢ l› ‹l = suminf ?S› by auto
qed

theorem summable_Leibniz:
  fixes a :: "nat ⇒ real"
  assumes a_zero: "a ⇢ 0"
    and "monoseq a"
  shows "summable (λ n. (-1)^n * a n)" (is "?summable")
    and "0 < a 0 ⟶
      (∀n. (∑i. (- 1)^i*a i) ∈ { ∑i<2*n. (- 1)^i * a i .. ∑i<2*n+1. (- 1)^i * a i})" (is "?pos")
    and "a 0 < 0 ⟶
      (∀n. (∑i. (- 1)^i*a i) ∈ { ∑i<2*n+1. (- 1)^i * a i .. ∑i<2*n. (- 1)^i * a i})" (is "?neg")
    and "(λn. ∑i<2*n. (- 1)^i*a i) ⇢ (∑i. (- 1)^i*a i)" (is "?f")
    and "(λn. ∑i<2*n+1. (- 1)^i*a i) ⇢ (∑i. (- 1)^i*a i)" (is "?g")
proof -
  have "?summable ∧ ?pos ∧ ?neg ∧ ?f ∧ ?g"
  proof (cases "(∀n. 0 ≤ a n) ∧ (∀m. ∀n≥m. a n ≤ a m)")
    case True
    then have ord: "⋀n m. m ≤ n ⟹ a n ≤ a m"
      and ge0: "⋀n. 0 ≤ a n"
      by auto
    have mono: "a (Suc n) ≤ a n" for n
      using ord[where n="Suc n" and m=n] by auto
    note leibniz = summable_Leibniz'[OF ‹a ⇢ 0› ge0]
    from leibniz[OF mono]
    show ?thesis using ‹0 ≤ a 0› by auto
  next
    let ?a = "λn. - a n"
    case False
    with monoseq_le[OF ‹monoseq a› ‹a ⇢ 0›]
    have "(∀ n. a n ≤ 0) ∧ (∀m. ∀n≥m. a m ≤ a n)" by auto
    then have ord: "⋀n m. m ≤ n ⟹ ?a n ≤ ?a m" and ge0: "⋀ n. 0 ≤ ?a n"
      by auto
    have monotone: "?a (Suc n) ≤ ?a n" for n
      using ord[where n="Suc n" and m=n] by auto
    note leibniz =
      summable_Leibniz'[OF _ ge0, of "λx. x",
        OF tendsto_minus[OF ‹a ⇢ 0›, unfolded minus_zero] monotone]
    have "summable (λ n. (-1)^n * ?a n)"
      using leibniz(1) by auto
    then obtain l where "(λ n. (-1)^n * ?a n) sums l"
      unfolding summable_def by auto
    from this[THEN sums_minus] have "(λ n. (-1)^n * a n) sums -l"
      by auto
    then have ?summable by (auto simp: summable_def)
    moreover
    have "¦- a - - b¦ = ¦a - b¦" for a b :: real
      unfolding minus_diff_minus by auto

    from suminf_minus[OF leibniz(1), unfolded mult_minus_right minus_minus]
    have move_minus: "(∑n. - ((- 1) ^ n * a n)) = - (∑n. (- 1) ^ n * a n)"
      by auto

    have ?pos using ‹0 ≤ ?a 0› by auto
    moreover have ?neg
      using leibniz(2,4)
      unfolding mult_minus_right sum_negf move_minus neg_le_iff_le
      by auto
    moreover have ?f and ?g
      using leibniz(3,5)[unfolded mult_minus_right sum_negf move_minus, THEN tendsto_minus_cancel]
      by auto
    ultimately show ?thesis by auto
  qed
  then show ?summable and ?pos and ?neg and ?f and ?g
    by safe
qed


subsection ‹Term-by-Term Differentiability of Power Series›

definition diffs :: "(nat ⇒ 'a::ring_1) ⇒ nat ⇒ 'a"
  where "diffs c = (λn. of_nat (Suc n) * c (Suc n))"

text ‹Lemma about distributing negation over it.›
lemma diffs_minus: "diffs (λn. - c n) = (λn. - diffs c n)"
  by (simp add: diffs_def)

lemma diffs_equiv:
  fixes x :: "'a::{real_normed_vector,ring_1}"
  shows "summable (λn. diffs c n * x^n) ⟹
    (λn. of_nat n * c n * x^(n - Suc 0)) sums (∑n. diffs c n * x^n)"
  unfolding diffs_def
  by (simp add: summable_sums sums_Suc_imp)

lemma lemma_termdiff1:
  fixes z :: "'a :: {monoid_mult,comm_ring}"
  shows "(∑p<m. (((z + h) ^ (m - p)) * (z ^ p)) - (z ^ m)) =
    (∑p<m. (z ^ p) * (((z + h) ^ (m - p)) - (z ^ (m - p))))"
  by (auto simp: algebra_simps power_add [symmetric])

lemma sumr_diff_mult_const2: "sum f {..<n} - of_nat n * r = (∑i<n. f i - r)"
  for r :: "'a::ring_1"
  by (simp add: sum_subtractf)

lemma lemma_realpow_rev_sumr:
  "(∑p<Suc n. (x ^ p) * (y ^ (n - p))) = (∑p<Suc n. (x ^ (n - p)) * (y ^ p))"
  by (subst nat_diff_sum_reindex[symmetric]) simp

lemma lemma_termdiff2:
  fixes h :: "'a::field"
  assumes h: "h ≠ 0"
  shows "((z + h) ^ n - z ^ n) / h - of_nat n * z ^ (n - Suc 0) =
         h * (∑p< n - Suc 0. ∑q< n - Suc 0 - p. (z + h) ^ q * z ^ (n - 2 - q))"
    (is "?lhs = ?rhs")
proof (cases n)
  case (Suc n)
  have 0: "⋀x k. (∑n<Suc k. h * (z ^ x * (z ^ (k - n) * (h + z) ^ n))) =
                 (∑j<Suc k.  h * ((h + z) ^ j * z ^ (x + k - j)))"
    apply (rule sum.cong [OF refl])
    by (simp add: power_add [symmetric] mult.commute)
  have *: "(∑i<n. z ^ i * ((z + h) ^ (n - i) - z ^ (n - i))) =
           (∑i<n. ∑j<n - i. h * ((z + h) ^ j * z ^ (n - Suc j)))"
    apply (rule sum.cong [OF refl])
    apply (clarsimp simp add: less_iff_Suc_add sum_distrib_left diff_power_eq_sum ac_simps 0
        simp del: sum_lessThan_Suc power_Suc)
    done
  have "h * ?lhs = h * ?rhs"
    apply (simp add: right_diff_distrib diff_divide_distrib h mult.assoc [symmetric])
    using Suc
    apply (simp add: diff_power_eq_sum h right_diff_distrib [symmetric] mult.assoc
        del: power_Suc sum_lessThan_Suc of_nat_Suc)
    apply (subst lemma_realpow_rev_sumr)
    apply (subst sumr_diff_mult_const2)
    apply (simp add: lemma_termdiff1 sum_distrib_left *)
    done
  then show ?thesis
    by (simp add: h)
qed auto


lemma real_sum_nat_ivl_bounded2:
  fixes K :: "'a::linordered_semidom"
  assumes f: "⋀p::nat. p < n ⟹ f p ≤ K"
    and K: "0 ≤ K"
  shows "sum f {..<n-k} ≤ of_nat n * K"
  apply (rule order_trans [OF sum_mono [OF f]])
  apply (auto simp: mult_right_mono K)
  done

lemma lemma_termdiff3:
  fixes h z :: "'a::real_normed_field"
  assumes 1: "h ≠ 0"
    and 2: "norm z ≤ K"
    and 3: "norm (z + h) ≤ K"
  shows "norm (((z + h) ^ n - z ^ n) / h - of_nat n * z ^ (n - Suc 0)) ≤
    of_nat n * of_nat (n - Suc 0) * K ^ (n - 2) * norm h"
proof -
  have "norm (((z + h) ^ n - z ^ n) / h - of_nat n * z ^ (n - Suc 0)) =
    norm (∑p<n - Suc 0. ∑q<n - Suc 0 - p. (z + h) ^ q * z ^ (n - 2 - q)) * norm h"
    by (metis (lifting, no_types) lemma_termdiff2 [OF 1] mult.commute norm_mult)
  also have "… ≤ of_nat n * (of_nat (n - Suc 0) * K ^ (n - 2)) * norm h"
  proof (rule mult_right_mono [OF _ norm_ge_zero])
    from norm_ge_zero 2 have K: "0 ≤ K"
      by (rule order_trans)
    have le_Kn: "⋀i j n. i + j = n ⟹ norm ((z + h) ^ i * z ^ j) ≤ K ^ n"
      apply (erule subst)
      apply (simp only: norm_mult norm_power power_add)
      apply (intro mult_mono power_mono 2 3 norm_ge_zero zero_le_power K)
      done
    show "norm (∑p<n - Suc 0. ∑q<n - Suc 0 - p. (z + h) ^ q * z ^ (n - 2 - q)) ≤
        of_nat n * (of_nat (n - Suc 0) * K ^ (n - 2))"
      apply (intro
          order_trans [OF norm_sum]
          real_sum_nat_ivl_bounded2
          mult_nonneg_nonneg
          of_nat_0_le_iff
          zero_le_power K)
      apply (rule le_Kn, simp)
      done
  qed
  also have "… = of_nat n * of_nat (n - Suc 0) * K ^ (n - 2) * norm h"
    by (simp only: mult.assoc)
  finally show ?thesis .
qed

lemma lemma_termdiff4:
  fixes f :: "'a::real_normed_vector ⇒ 'b::real_normed_vector"
    and k :: real
  assumes k: "0 < k"
    and le: "⋀h. h ≠ 0 ⟹ norm h < k ⟹ norm (f h) ≤ K * norm h"
  shows "f ─0→ 0"
proof (rule tendsto_norm_zero_cancel)
  show "(λh. norm (f h)) ─0→ 0"
  proof (rule real_tendsto_sandwich)
    show "eventually (λh. 0 ≤ norm (f h)) (at 0)"
      by simp
    show "eventually (λh. norm (f h) ≤ K * norm h) (at 0)"
      using k by (auto simp: eventually_at dist_norm le)
    show "(λh. 0) ─(0::'a)→ (0::real)"
      by (rule tendsto_const)
    have "(λh. K * norm h) ─(0::'a)→ K * norm (0::'a)"
      by (intro tendsto_intros)
    then show "(λh. K * norm h) ─(0::'a)→ 0"
      by simp
  qed
qed

lemma lemma_termdiff5:
  fixes g :: "'a::real_normed_vector ⇒ nat ⇒ 'b::banach"
    and k :: real
  assumes k: "0 < k"
    and f: "summable f"
    and le: "⋀h n. h ≠ 0 ⟹ norm h < k ⟹ norm (g h n) ≤ f n * norm h"
  shows "(λh. suminf (g h)) ─0→ 0"
proof (rule lemma_termdiff4 [OF k])
  fix h :: 'a
  assume "h ≠ 0" and "norm h < k"
  then have 1: "∀n. norm (g h n) ≤ f n * norm h"
    by (simp add: le)
  then have "∃N. ∀n≥N. norm (norm (g h n)) ≤ f n * norm h"
    by simp
  moreover from f have 2: "summable (λn. f n * norm h)"
    by (rule summable_mult2)
  ultimately have 3: "summable (λn. norm (g h n))"
    by (rule summable_comparison_test)
  then have "norm (suminf (g h)) ≤ (∑n. norm (g h n))"
    by (rule summable_norm)
  also from 1 3 2 have "(∑n. norm (g h n)) ≤ (∑n. f n * norm h)"
    by (rule suminf_le)
  also from f have "(∑n. f n * norm h) = suminf f * norm h"
    by (rule suminf_mult2 [symmetric])
  finally show "norm (suminf (g h)) ≤ suminf f * norm h" .
qed


(* FIXME: Long proofs *)

lemma termdiffs_aux:
  fixes x :: "'a::{real_normed_field,banach}"
  assumes 1: "summable (λn. diffs (diffs c) n * K ^ n)"
    and 2: "norm x < norm K"
  shows "(λh. ∑n. c n * (((x + h) ^ n - x^n) / h - of_nat n * x ^ (n - Suc 0))) ─0→ 0"
proof -
  from dense [OF 2] obtain r where r1: "norm x < r" and r2: "r < norm K"
    by fast
  from norm_ge_zero r1 have r: "0 < r"
    by (rule order_le_less_trans)
  then have r_neq_0: "r ≠ 0" by simp
  show ?thesis
  proof (rule lemma_termdiff5)
    show "0 < r - norm x"
      using r1 by simp
    from r r2 have "norm (of_real r::'a) < norm K"
      by simp
    with 1 have "summable (λn. norm (diffs (diffs c) n * (of_real r ^ n)))"
      by (rule powser_insidea)
    then have "summable (λn. diffs (diffs (λn. norm (c n))) n * r ^ n)"
      using r by (simp add: diffs_def norm_mult norm_power del: of_nat_Suc)
    then have "summable (λn. of_nat n * diffs (λn. norm (c n)) n * r ^ (n - Suc 0))"
      by (rule diffs_equiv [THEN sums_summable])
    also have "(λn. of_nat n * diffs (λn. norm (c n)) n * r ^ (n - Suc 0)) =
      (λn. diffs (λm. of_nat (m - Suc 0) * norm (c m) * inverse r) n * (r ^ n))"
      apply (rule ext)
      apply (case_tac n)
      apply (simp_all add: diffs_def r_neq_0)
      done
    finally have "summable
      (λn. of_nat n * (of_nat (n - Suc 0) * norm (c n) * inverse r) * r ^ (n - Suc 0))"
      by (rule diffs_equiv [THEN sums_summable])
    also have
      "(λn. of_nat n * (of_nat (n - Suc 0) * norm (c n) * inverse r) * r ^ (n - Suc 0)) =
       (λn. norm (c n) * of_nat n * of_nat (n - Suc 0) * r ^ (n - 2))"
      apply (rule ext)
      apply (case_tac n, simp)
      apply (rename_tac nat)
      apply (case_tac nat, simp)
      apply (simp add: r_neq_0)
      done
    finally show "summable (λn. norm (c n) * of_nat n * of_nat (n - Suc 0) * r ^ (n - 2))" .
  next
    fix h :: 'a
    fix n :: nat
    assume h: "h ≠ 0"
    assume "norm h < r - norm x"
    then have "norm x + norm h < r" by simp
    with norm_triangle_ineq have xh: "norm (x + h) < r"
      by (rule order_le_less_trans)
    show "norm (c n * (((x + h) ^ n - x^n) / h - of_nat n * x ^ (n - Suc 0))) ≤
      norm (c n) * of_nat n * of_nat (n - Suc 0) * r ^ (n - 2) * norm h"
      apply (simp only: norm_mult mult.assoc)
      apply (rule mult_left_mono [OF _ norm_ge_zero])
      apply (simp add: mult.assoc [symmetric])
      apply (metis h lemma_termdiff3 less_eq_real_def r1 xh)
      done
  qed
qed

lemma termdiffs:
  fixes K x :: "'a::{real_normed_field,banach}"
  assumes 1: "summable (λn. c n * K ^ n)"
    and 2: "summable (λn. (diffs c) n * K ^ n)"
    and 3: "summable (λn. (diffs (diffs c)) n * K ^ n)"
    and 4: "norm x < norm K"
  shows "DERIV (λx. ∑n. c n * x^n) x :> (∑n. (diffs c) n * x^n)"
  unfolding DERIV_def
proof (rule LIM_zero_cancel)
  show "(λh. (suminf (λn. c n * (x + h) ^ n) - suminf (λn. c n * x^n)) / h
            - suminf (λn. diffs c n * x^n)) ─0→ 0"
  proof (rule LIM_equal2)
    show "0 < norm K - norm x"
      using 4 by (simp add: less_diff_eq)
  next
    fix h :: 'a
    assume "norm (h - 0) < norm K - norm x"
    then have "norm x + norm h < norm K" by simp
    then have 5: "norm (x + h) < norm K"
      by (rule norm_triangle_ineq [THEN order_le_less_trans])
    have "summable (λn. c n * x^n)"
      and "summable (λn. c n * (x + h) ^ n)"
      and "summable (λn. diffs c n * x^n)"
      using 1 2 4 5 by (auto elim: powser_inside)
    then have "((∑n. c n * (x + h) ^ n) - (∑n. c n * x^n)) / h - (∑n. diffs c n * x^n) =
          (∑n. (c n * (x + h) ^ n - c n * x^n) / h - of_nat n * c n * x ^ (n - Suc 0))"
      by (intro sums_unique sums_diff sums_divide diffs_equiv summable_sums)
    then show "((∑n. c n * (x + h) ^ n) - (∑n. c n * x^n)) / h - (∑n. diffs c n * x^n) =
          (∑n. c n * (((x + h) ^ n - x^n) / h - of_nat n * x ^ (n - Suc 0)))"
      by (simp add: algebra_simps)
  next
    show "(λh. ∑n. c n * (((x + h) ^ n - x^n) / h - of_nat n * x ^ (n - Suc 0))) ─0→ 0"
      by (rule termdiffs_aux [OF 3 4])
  qed
qed

subsection ‹The Derivative of a Power Series Has the Same Radius of Convergence›

lemma termdiff_converges:
  fixes x :: "'a::{real_normed_field,banach}"
  assumes K: "norm x < K"
    and sm: "⋀x. norm x < K ⟹ summable(λn. c n * x ^ n)"
  shows "summable (λn. diffs c n * x ^ n)"
proof (cases "x = 0")
  case True
  then show ?thesis
    using powser_sums_zero sums_summable by auto
next
  case False
  then have "K > 0"
    using K less_trans zero_less_norm_iff by blast
  then obtain r :: real where r: "norm x < norm r" "norm r < K" "r > 0"
    using K False
    by (auto simp: field_simps abs_less_iff add_pos_pos intro: that [of "(norm x + K) / 2"])
  have to0: "(λn. of_nat n * (x / of_real r) ^ n) ⇢ 0"
    using r by (simp add: norm_divide powser_times_n_limit_0 [of "x / of_real r"])
  obtain N where N: "⋀n. n≥N ⟹ real_of_nat n * norm x ^ n < r ^ n"
    using r LIMSEQ_D [OF to0, of 1]
    by (auto simp: norm_divide norm_mult norm_power field_simps)
  have "summable (λn. (of_nat n * c n) * x ^ n)"
  proof (rule summable_comparison_test')
    show "summable (λn. norm (c n * of_real r ^ n))"
      apply (rule powser_insidea [OF sm [of "of_real ((r+K)/2)"]])
      using N r norm_of_real [of "r + K", where 'a = 'a] by auto
    show "⋀n. N ≤ n ⟹ norm (of_nat n * c n * x ^ n) ≤ norm (c n * of_real r ^ n)"
      using N r by (fastforce simp add: norm_mult norm_power less_eq_real_def)
  qed
  then have "summable (λn. (of_nat (Suc n) * c(Suc n)) * x ^ Suc n)"
    using summable_iff_shift [of "λn. of_nat n * c n * x ^ n" 1]
    by simp
  then have "summable (λn. (of_nat (Suc n) * c(Suc n)) * x ^ n)"
    using False summable_mult2 [of "λn. (of_nat (Suc n) * c(Suc n) * x ^ n) * x" "inverse x"]
    by (simp add: mult.assoc) (auto simp: ac_simps)
  then show ?thesis
    by (simp add: diffs_def)
qed

lemma termdiff_converges_all:
  fixes x :: "'a::{real_normed_field,banach}"
  assumes "⋀x. summable (λn. c n * x^n)"
  shows "summable (λn. diffs c n * x^n)"
  by (rule termdiff_converges [where K = "1 + norm x"]) (use assms in auto)

lemma termdiffs_strong:
  fixes K x :: "'a::{real_normed_field,banach}"
  assumes sm: "summable (λn. c n * K ^ n)"
    and K: "norm x < norm K"
  shows "DERIV (λx. ∑n. c n * x^n) x :> (∑n. diffs c n * x^n)"
proof -
  have K2: "norm ((of_real (norm K) + of_real (norm x)) / 2 :: 'a) < norm K"
    using K
    apply (auto simp: norm_divide field_simps)
    apply (rule le_less_trans [of _ "of_real (norm K) + of_real (norm x)"])
     apply (auto simp: mult_2_right norm_triangle_mono)
    done
  then have [simp]: "norm ((of_real (norm K) + of_real (norm x)) :: 'a) < norm K * 2"
    by simp
  have "summable (λn. c n * (of_real (norm x + norm K) / 2) ^ n)"
    by (metis K2 summable_norm_cancel [OF powser_insidea [OF sm]] add.commute of_real_add)
  moreover have "⋀x. norm x < norm K ⟹ summable (λn. diffs c n * x ^ n)"
    by (blast intro: sm termdiff_converges powser_inside)
  moreover have "⋀x. norm x < norm K ⟹ summable (λn. diffs(diffs c) n * x ^ n)"
    by (blast intro: sm termdiff_converges powser_inside)
  ultimately show ?thesis
    apply (rule termdiffs [where K = "of_real (norm x + norm K) / 2"])
    using K
      apply (auto simp: field_simps)
    apply (simp flip: of_real_add)
    done
qed

lemma termdiffs_strong_converges_everywhere:
  fixes K x :: "'a::{real_normed_field,banach}"
  assumes "⋀y. summable (λn. c n * y ^ n)"
  shows "((λx. ∑n. c n * x^n) has_field_derivative (∑n. diffs c n * x^n)) (at x)"
  using termdiffs_strong[OF assms[of "of_real (norm x + 1)"], of x]
  by (force simp del: of_real_add)

lemma termdiffs_strong':
  fixes z :: "'a :: {real_normed_field,banach}"
  assumes "⋀z. norm z < K ⟹ summable (λn. c n * z ^ n)"
  assumes "norm z < K"
  shows   "((λz. ∑n. c n * z^n) has_field_derivative (∑n. diffs c n * z^n)) (at z)"
proof (rule termdiffs_strong)
  define L :: real where "L =  (norm z + K) / 2"
  have "0 ≤ norm z" by simp
  also note ‹norm z < K›
  finally have K: "K ≥ 0" by simp
  from assms K have L: "L ≥ 0" "norm z < L" "L < K" by (simp_all add: L_def)
  from L show "norm z < norm (of_real L :: 'a)" by simp
  from L show "summable (λn. c n * of_real L ^ n)" by (intro assms(1)) simp_all
qed

lemma termdiffs_sums_strong:
  fixes z :: "'a :: {banach,real_normed_field}"
  assumes sums: "⋀z. norm z < K ⟹ (λn. c n * z ^ n) sums f z"
  assumes deriv: "(f has_field_derivative f') (at z)"
  assumes norm: "norm z < K"
  shows   "(λn. diffs c n * z ^ n) sums f'"
proof -
  have summable: "summable (λn. diffs c n * z^n)"
    by (intro termdiff_converges[OF norm] sums_summable[OF sums])
  from norm have "eventually (λz. z ∈ norm -` {..<K}) (nhds z)"
    by (intro eventually_nhds_in_open open_vimage)
       (simp_all add: continuous_on_norm continuous_on_id)
  hence eq: "eventually (λz. (∑n. c n * z^n) = f z) (nhds z)"
    by eventually_elim (insert sums, simp add: sums_iff)

  have "((λz. ∑n. c n * z^n) has_field_derivative (∑n. diffs c n * z^n)) (at z)"
    by (intro termdiffs_strong'[OF _ norm] sums_summable[OF sums])
  hence "(f has_field_derivative (∑n. diffs c n * z^n)) (at z)"
    by (subst (asm) DERIV_cong_ev[OF refl eq refl])
  from this and deriv have "(∑n. diffs c n * z^n) = f'" by (rule DERIV_unique)
  with summable show ?thesis by (simp add: sums_iff)
qed

lemma isCont_powser:
  fixes K x :: "'a::{real_normed_field,banach}"
  assumes "summable (λn. c n * K ^ n)"
  assumes "norm x < norm K"
  shows "isCont (λx. ∑n. c n * x^n) x"
  using termdiffs_strong[OF assms] by (blast intro!: DERIV_isCont)

lemmas isCont_powser' = isCont_o2[OF _ isCont_powser]

lemma isCont_powser_converges_everywhere:
  fixes K x :: "'a::{real_normed_field,banach}"
  assumes "⋀y. summable (λn. c n * y ^ n)"
  shows "isCont (λx. ∑n. c n * x^n) x"
  using termdiffs_strong[OF assms[of "of_real (norm x + 1)"], of x]
  by (force intro!: DERIV_isCont simp del: of_real_add)

lemma powser_limit_0:
  fixes a :: "nat ⇒ 'a::{real_normed_field,banach}"
  assumes s: "0 < s"
    and sm: "⋀x. norm x < s ⟹ (λn. a n * x ^ n) sums (f x)"
  shows "(f ⤏ a 0) (at 0)"
proof -
  have "norm (of_real s / 2 :: 'a) < s"
    using s  by (auto simp: norm_divide)
  then have "summable (λn. a n * (of_real s / 2) ^ n)"
    by (rule sums_summable [OF sm])
  then have "((λx. ∑n. a n * x ^ n) has_field_derivative (∑n. diffs a n * 0 ^ n)) (at 0)"
    by (rule termdiffs_strong) (use s in ‹auto simp: norm_divide›)
  then have "isCont (λx. ∑n. a n * x ^ n) 0"
    by (blast intro: DERIV_continuous)
  then have "((λx. ∑n. a n * x ^ n) ⤏ a 0) (at 0)"
    by (simp add: continuous_within)
  then show ?thesis
    apply (rule Lim_transform)
    apply (clarsimp simp: LIM_eq)
    apply (rule_tac x=s in exI)
    using s sm sums_unique by fastforce
qed

lemma powser_limit_0_strong:
  fixes a :: "nat ⇒ 'a::{real_normed_field,banach}"
  assumes s: "0 < s"
    and sm: "⋀x. x ≠ 0 ⟹ norm x < s ⟹ (λn. a n * x ^ n) sums (f x)"
  shows "(f ⤏ a 0) (at 0)"
proof -
  have *: "((λx. if x = 0 then a 0 else f x) ⤏ a 0) (at 0)"
    by (rule powser_limit_0 [OF s]) (auto simp: powser_sums_zero sm)
  show ?thesis
    apply (subst LIM_equal [where g = "(λx. if x = 0 then a 0 else f x)"])
     apply (simp_all add: *)
    done
qed


subsection ‹Derivability of power series›

lemma DERIV_series':
  fixes f :: "real ⇒ nat ⇒ real"
  assumes DERIV_f: "⋀ n. DERIV (λ x. f x n) x0 :> (f' x0 n)"
    and allf_summable: "⋀ x. x ∈ {a <..< b} ⟹ summable (f x)"
    and x0_in_I: "x0 ∈ {a <..< b}"
    and "summable (f' x0)"
    and "summable L"
    and L_def: "⋀n x y. x ∈ {a <..< b} ⟹ y ∈ {a <..< b} ⟹ ¦f x n - f y n¦ ≤ L n * ¦x - y¦"
  shows "DERIV (λ x. suminf (f x)) x0 :> (suminf (f' x0))"
  unfolding DERIV_def
proof (rule LIM_I)
  fix r :: real
  assume "0 < r" then have "0 < r/3" by auto

  obtain N_L where N_L: "⋀ n. N_L ≤ n ⟹ ¦ ∑ i. L (i + n) ¦ < r/3"
    using suminf_exist_split[OF ‹0 < r/3› ‹summable L›] by auto

  obtain N_f' where N_f': "⋀ n. N_f' ≤ n ⟹ ¦ ∑ i. f' x0 (i + n) ¦ < r/3"
    using suminf_exist_split[OF ‹0 < r/3› ‹summable (f' x0)›] by auto

  let ?N = "Suc (max N_L N_f')"
  have "¦ ∑ i. f' x0 (i + ?N) ¦ < r/3" (is "?f'_part < r/3")
    and L_estimate: "¦ ∑ i. L (i + ?N) ¦ < r/3"
    using N_L[of "?N"] and N_f' [of "?N"] by auto

  let ?diff = "λi x. (f (x0 + x) i - f x0 i) / x"

  let ?r = "r / (3 * real ?N)"
  from ‹0 < r› have "0 < ?r" by simp

  let ?s = "λn. SOME s. 0 < s ∧ (∀ x. x ≠ 0 ∧ ¦ x ¦ < s ⟶ ¦ ?diff n x - f' x0 n ¦ < ?r)"
  define S' where "S' = Min (?s ` {..< ?N })"

  have "0 < S'"
    unfolding S'_def
  proof (rule iffD2[OF Min_gr_iff])
    show "∀x ∈ (?s ` {..< ?N }). 0 < x"
    proof
      fix x
      assume "x ∈ ?s ` {..<?N}"
      then obtain n where "x = ?s n" and "n ∈ {..<?N}"
        using image_iff[THEN iffD1] by blast
      from DERIV_D[OF DERIV_f[where n=n], THEN LIM_D, OF ‹0 < ?r›, unfolded real_norm_def]
      obtain s where s_bound: "0 < s ∧ (∀x. x ≠ 0 ∧ ¦x¦ < s ⟶ ¦?diff n x - f' x0 n¦ < ?r)"
        by auto
      have "0 < ?s n"
        by (rule someI2[where a=s]) (auto simp: s_bound simp del: of_nat_Suc)
      then show "0 < x" by (simp only: ‹x = ?s n›)
    qed
  qed auto

  define S where "S = min (min (x0 - a) (b - x0)) S'"
  then have "0 < S" and S_a: "S ≤ x0 - a" and S_b: "S ≤ b - x0"
    and "S ≤ S'" using x0_in_I and ‹0 < S'›
    by auto

  have "¦(suminf (f (x0 + x)) - suminf (f x0)) / x - suminf (f' x0)¦ < r"
    if "x ≠ 0" and "¦x¦ < S" for x
  proof -
    from that have x_in_I: "x0 + x ∈ {a <..< b}"
      using S_a S_b by auto

    note diff_smbl = summable_diff[OF allf_summable[OF x_in_I] allf_summable[OF x0_in_I]]
    note div_smbl = summable_divide[OF diff_smbl]
    note all_smbl = summable_diff[OF div_smbl ‹summable (f' x0)›]
    note ign = summable_ignore_initial_segment[where k="?N"]
    note diff_shft_smbl = summable_diff[OF ign[OF allf_summable[OF x_in_I]] ign[OF allf_summable[OF x0_in_I]]]
    note div_shft_smbl = summable_divide[OF diff_shft_smbl]
    note all_shft_smbl = summable_diff[OF div_smbl ign[OF ‹summable (f' x0)›]]

    have 1: "¦(¦?diff (n + ?N) x¦)¦ ≤ L (n + ?N)" for n
    proof -
      have "¦?diff (n + ?N) x¦ ≤ L (n + ?N) * ¦(x0 + x) - x0¦ / ¦x¦"
        using divide_right_mono[OF L_def[OF x_in_I x0_in_I] abs_ge_zero]
        by (simp only: abs_divide)
      with ‹x ≠ 0› show ?thesis by auto
    qed
    note 2 = summable_rabs_comparison_test[OF _ ign[OF ‹summable L›]]
    from 1 have "¦ ∑ i. ?diff (i + ?N) x ¦ ≤ (∑ i. L (i + ?N))"
      by (metis (lifting) abs_idempotent
          order_trans[OF summable_rabs[OF 2] suminf_le[OF _ 2 ign[OF ‹summable L›]]])
    then have "¦∑i. ?diff (i + ?N) x¦ ≤ r / 3" (is "?L_part ≤ r/3")
      using L_estimate by auto

    have "¦∑n<?N. ?diff n x - f' x0 n¦ ≤ (∑n<?N. ¦?diff n x - f' x0 n¦)" ..
    also have "… < (∑n<?N. ?r)"
    proof (rule sum_strict_mono)
      fix n
      assume "n ∈ {..< ?N}"
      have "¦x¦ < S" using ‹¦x¦ < S› .
      also have "S ≤ S'" using ‹S ≤ S'› .
      also have "S' ≤ ?s n"
        unfolding S'_def
      proof (rule Min_le_iff[THEN iffD2])
        have "?s n ∈ (?s ` {..<?N}) ∧ ?s n ≤ ?s n"
          using ‹n ∈ {..< ?N}› by auto
        then show "∃ a ∈ (?s ` {..<?N}). a ≤ ?s n"
          by blast
      qed auto
      finally have "¦x¦ < ?s n" .

      from DERIV_D[OF DERIV_f[where n=n], THEN LIM_D, OF ‹0 < ?r›,
          unfolded real_norm_def diff_0_right, unfolded some_eq_ex[symmetric], THEN conjunct2]
      have "∀x. x ≠ 0 ∧ ¦x¦ < ?s n ⟶ ¦?diff n x - f' x0 n¦ < ?r" .
      with ‹x ≠ 0› and ‹¦x¦ < ?s n› show "¦?diff n x - f' x0 n¦ < ?r"
        by blast
    qed auto
    also have "… = of_nat (card {..<?N}) * ?r"
      by (rule sum_constant)
    also have "… = real ?N * ?r"
      by simp
    also have "… = r/3"
      by (auto simp del: of_nat_Suc)
    finally have "¦∑n<?N. ?diff n x - f' x0 n ¦ < r / 3" (is "?diff_part < r / 3") .

    from suminf_diff[OF allf_summable[OF x_in_I] allf_summable[OF x0_in_I]]
    have "¦(suminf (f (x0 + x)) - (suminf (f x0))) / x - suminf (f' x0)¦ =
        ¦∑n. ?diff n x - f' x0 n¦"
      unfolding suminf_diff[OF div_smbl ‹summable (f' x0)›, symmetric]
      using suminf_divide[OF diff_smbl, symmetric] by auto
    also have "… ≤ ?diff_part + ¦(∑n. ?diff (n + ?N) x) - (∑ n. f' x0 (n + ?N))¦"
      unfolding suminf_split_initial_segment[OF all_smbl, where k="?N"]
      unfolding suminf_diff[OF div_shft_smbl ign[OF ‹summable (f' x0)›]]
      apply (simp only: add.commute)
      using abs_triangle_ineq by blast
    also have "… ≤ ?diff_part + ?L_part + ?f'_part"
      using abs_triangle_ineq4 by auto
    also have "… < r /3 + r/3 + r/3"
      using ‹?diff_part < r/3› ‹?L_part ≤ r/3› and ‹?f'_part < r/3›
      by (rule add_strict_mono [OF add_less_le_mono])
    finally show ?thesis
      by auto
  qed
  then show "∃s > 0. ∀ x. x ≠ 0 ∧ norm (x - 0) < s ⟶
      norm (((∑n. f (x0 + x) n) - (∑n. f x0 n)) / x - (∑n. f' x0 n)) < r"
    using ‹0 < S› by auto
qed

lemma DERIV_power_series':
  fixes f :: "nat ⇒ real"
  assumes converges: "⋀x. x ∈ {-R <..< R} ⟹ summable (λn. f n * real (Suc n) * x^n)"
    and x0_in_I: "x0 ∈ {-R <..< R}"
    and "0 < R"
  shows "DERIV (λx. (∑n. f n * x^(Suc n))) x0 :> (∑n. f n * real (Suc n) * x0^n)"
    (is "DERIV (λx. suminf (?f x)) x0 :> suminf (?f' x0)")
proof -
  have for_subinterval: "DERIV (λx. suminf (?f x)) x0 :> suminf (?f' x0)"
    if "0 < R'" and "R' < R" and "-R' < x0" and "x0 < R'" for R'
  proof -
    from that have "x0 ∈ {-R' <..< R'}" and "R' ∈ {-R <..< R}" and "x0 ∈ {-R <..< R}"
      by auto
    show ?thesis
    proof (rule DERIV_series')
      show "summable (λ n. ¦f n * real (Suc n) * R'^n¦)"
      proof -
        have "(R' + R) / 2 < R" and "0 < (R' + R) / 2"
          using ‹0 < R'› ‹0 < R› ‹R' < R› by (auto simp: field_simps)
        then have in_Rball: "(R' + R) / 2 ∈ {-R <..< R}"
          using ‹R' < R› by auto
        have "norm R' < norm ((R' + R) / 2)"
          using ‹0 < R'› ‹0 < R› ‹R' < R› by (auto simp: field_simps)
        from powser_insidea[OF converges[OF in_Rball] this] show ?thesis
          by auto
      qed
    next
      fix n x y
      assume "x ∈ {-R' <..< R'}" and "y ∈ {-R' <..< R'}"
      show "¦?f x n - ?f y n¦ ≤ ¦f n * real (Suc n) * R'^n¦ * ¦x-y¦"
      proof -
        have "¦f n * x ^ (Suc n) - f n * y ^ (Suc n)¦ =
          (¦f n¦ * ¦x-y¦) * ¦∑p<Suc n. x ^ p * y ^ (n - p)¦"
          unfolding right_diff_distrib[symmetric] diff_power_eq_sum abs_mult
          by auto
        also have "… ≤ (¦f n¦ * ¦x-y¦) * (¦real (Suc n)¦ * ¦R' ^ n¦)"
        proof (rule mult_left_mono)
          have "¦∑p<Suc n. x ^ p * y ^ (n - p)¦ ≤ (∑p<Suc n. ¦x ^ p * y ^ (n - p)¦)"
            by (rule sum_abs)
          also have "… ≤ (∑p<Suc n. R' ^ n)"
          proof (rule sum_mono)
            fix p
            assume "p ∈ {..<Suc n}"
            then have "p ≤ n" by auto
            have "¦x^n¦ ≤ R'^n" if  "x ∈ {-R'<..<R'}" for n and x :: real
            proof -
              from that have "¦x¦ ≤ R'" by auto
              then show ?thesis
                unfolding power_abs by (rule power_mono) auto
            qed
            from mult_mono[OF this[OF ‹x ∈ {-R'<..<R'}›, of p] this[OF ‹y ∈ {-R'<..<R'}›, of "n-p"]]
              and ‹0 < R'›
            have "¦x^p * y^(n - p)¦ ≤ R'^p * R'^(n - p)"
              unfolding abs_mult by auto
            then show "¦x^p * y^(n - p)¦ ≤ R'^n"
              unfolding power_add[symmetric] using ‹p ≤ n› by auto
          qed
          also have "… = real (Suc n) * R' ^ n"
            unfolding sum_constant card_atLeastLessThan by auto
          finally show "¦∑p<Suc n. x ^ p * y ^ (n - p)¦ ≤ ¦real (Suc n)¦ * ¦R' ^ n¦"
            unfolding abs_of_nonneg[OF zero_le_power[OF less_imp_le[OF ‹0 < R'›]]]
            by linarith
          show "0 ≤ ¦f n¦ * ¦x - y¦"
            unfolding abs_mult[symmetric] by auto
        qed
        also have "… = ¦f n * real (Suc n) * R' ^ n¦ * ¦x - y¦"
          unfolding abs_mult mult.assoc[symmetric] by algebra
        finally show ?thesis .
      qed
    next
      show "DERIV (λx. ?f x n) x0 :> ?f' x0 n" for n
        by (auto intro!: derivative_eq_intros simp del: power_Suc)
    next
      fix x
      assume "x ∈ {-R' <..< R'}"
      then have "R' ∈ {-R <..< R}" and "norm x < norm R'"
        using assms ‹R' < R› by auto
      have "summable (λn. f n * x^n)"
      proof (rule summable_comparison_test, intro exI allI impI)
        fix n
        have le: "¦f n¦ * 1 ≤ ¦f n¦ * real (Suc n)"
          by (rule mult_left_mono) auto
        show "norm (f n * x^n) ≤ norm (f n * real (Suc n) * x^n)"
          unfolding real_norm_def abs_mult
          using le mult_right_mono by fastforce
      qed (rule powser_insidea[OF converges[OF ‹R' ∈ {-R <..< R}›] ‹norm x < norm R'›])
      from this[THEN summable_mult2[where c=x], simplified mult.assoc, simplified mult.commute]
      show "summable (?f x)" by auto
    next
      show "summable (?f' x0)"
        using converges[OF ‹x0 ∈ {-R <..< R}›] .
      show "x0 ∈ {-R' <..< R'}"
        using ‹x0 ∈ {-R' <..< R'}› .
    qed
  qed
  let ?R = "(R + ¦x0¦) / 2"
  have "¦x0¦ < ?R"
    using assms by (auto simp: field_simps)
  then have "- ?R < x0"
  proof (cases "x0 < 0")
    case True
    then have "- x0 < ?R"
      using ‹¦x0¦ < ?R› by auto
    then show ?thesis
      unfolding neg_less_iff_less[symmetric, of "- x0"] by auto
  next
    case False
    have "- ?R < 0" using assms by auto
    also have "… ≤ x0" using False by auto
    finally show ?thesis .
  qed
  then have "0 < ?R" "?R < R" "- ?R < x0" and "x0 < ?R"
    using assms by (auto simp: field_simps)
  from for_subinterval[OF this] show ?thesis .
qed

lemma geometric_deriv_sums:
  fixes z :: "'a :: {real_normed_field,banach}"
  assumes "norm z < 1"
  shows   "(λn. of_nat (Suc n) * z ^ n) sums (1 / (1 - z)^2)"
proof -
  have "(λn. diffs (λn. 1) n * z^n) sums (1 / (1 - z)^2)"
  proof (rule termdiffs_sums_strong)
    fix z :: 'a assume "norm z < 1"
    thus "(λn. 1 * z^n) sums (1 / (1 - z))" by (simp add: geometric_sums)
  qed (insert assms, auto intro!: derivative_eq_intros simp: power2_eq_square)
  thus ?thesis unfolding diffs_def by simp
qed

lemma isCont_pochhammer [continuous_intros]: "isCont (λz. pochhammer z n) z"
  for z :: "'a::real_normed_field"
  by (induct n) (auto simp: pochhammer_rec')

lemma continuous_on_pochhammer [continuous_intros]: "continuous_on A (λz. pochhammer z n)"
  for A :: "'a::real_normed_field set"
  by (intro continuous_at_imp_continuous_on ballI isCont_pochhammer)

lemmas continuous_on_pochhammer' [continuous_intros] =
  continuous_on_compose2[OF continuous_on_pochhammer _ subset_UNIV]


subsection ‹Exponential Function›

definition exp :: "'a ⇒ 'a::{real_normed_algebra_1,banach}"
  where "exp = (λx. ∑n. x^n /R fact n)"

lemma summable_exp_generic:
  fixes x :: "'a::{real_normed_algebra_1,banach}"
  defines S_def: "S ≡ λn. x^n /R fact n"
  shows "summable S"
proof -
  have S_Suc: "⋀n. S (Suc n) = (x * S n) /R (Suc n)"
    unfolding S_def by (simp del: mult_Suc)
  obtain r :: real where r0: "0 < r" and r1: "r < 1"
    using dense [OF zero_less_one] by fast
  obtain N :: nat where N: "norm x < real N * r"
    using ex_less_of_nat_mult r0 by auto
  from r1 show ?thesis
  proof (rule summable_ratio_test [rule_format])
    fix n :: nat
    assume n: "N ≤ n"
    have "norm x ≤ real N * r"
      using N by (rule order_less_imp_le)
    also have "real N * r ≤ real (Suc n) * r"
      using r0 n by (simp add: mult_right_mono)
    finally have "norm x * norm (S n) ≤ real (Suc n) * r * norm (S n)"
      using norm_ge_zero by (rule mult_right_mono)
    then have "norm (x * S n) ≤ real (Suc n) * r * norm (S n)"
      by (rule order_trans [OF norm_mult_ineq])
    then have "norm (x * S n) / real (Suc n) ≤ r * norm (S n)"
      by (simp add: pos_divide_le_eq ac_simps)
    then show "norm (S (Suc n)) ≤ r * norm (S n)"
      by (simp add: S_Suc inverse_eq_divide)
  qed
qed

lemma summable_norm_exp: "summable (λn. norm (x^n /R fact n))"
  for x :: "'a::{real_normed_algebra_1,banach}"
proof (rule summable_norm_comparison_test [OF exI, rule_format])
  show "summable (λn. norm x^n /R fact n)"
    by (rule summable_exp_generic)
  show "norm (x^n /R fact n) ≤ norm x^n /R fact n" for n
    by (simp add: norm_power_ineq)
qed

lemma summable_exp: "summable (λn. inverse (fact n) * x^n)"
  for x :: "'a::{real_normed_field,banach}"
  using summable_exp_generic [where x=x]
  by (simp add: scaleR_conv_of_real nonzero_of_real_inverse)

lemma exp_converges: "(λn. x^n /R fact n) sums exp x"
  unfolding exp_def by (rule summable_exp_generic [THEN summable_sums])

lemma exp_fdiffs:
  "diffs (λn. inverse (fact n)) = (λn. inverse (fact n :: 'a::{real_normed_field,banach}))"
  by (simp add: diffs_def mult_ac nonzero_inverse_mult_distrib nonzero_of_real_inverse
      del: mult_Suc of_nat_Suc)

lemma diffs_of_real: "diffs (λn. of_real (f n)) = (λn. of_real (diffs f n))"
  by (simp add: diffs_def)

lemma DERIV_exp [simp]: "DERIV exp x :> exp x"
  unfolding exp_def scaleR_conv_of_real
proof (rule DERIV_cong)
  have sinv: "summable (λn. of_real (inverse (fact n)) * x ^ n)" for x::'a
    by (rule exp_converges [THEN sums_summable, unfolded scaleR_conv_of_real])
  note xx = exp_converges [THEN sums_summable, unfolded scaleR_conv_of_real]
  show "((λx. ∑n. of_real (inverse (fact n)) * x ^ n) has_field_derivative
        (∑n. diffs (λn. of_real (inverse (fact n))) n * x ^ n))  (at x)"
    by (rule termdiffs [where K="of_real (1 + norm x)"]) (simp_all only: diffs_of_real exp_fdiffs sinv norm_of_real)
  show "(∑n. diffs (λn. of_real (inverse (fact n))) n * x ^ n) = (∑n. of_real (inverse (fact n)) * x ^ n)"
    by (simp add: diffs_of_real exp_fdiffs)
qed

declare DERIV_exp[THEN DERIV_chain2, derivative_intros]
  and DERIV_exp[THEN DERIV_chain2, unfolded has_field_derivative_def, derivative_intros]

lemmas has_derivative_exp[derivative_intros] = DERIV_exp[THEN DERIV_compose_FDERIV]

lemma norm_exp: "norm (exp x) ≤ exp (norm x)"
proof -
  from summable_norm[OF summable_norm_exp, of x]
  have "norm (exp x) ≤ (∑n. inverse (fact n) * norm (x^n))"
    by (simp add: exp_def)
  also have "… ≤ exp (norm x)"
    using summable_exp_generic[of "norm x"] summable_norm_exp[of x]
    by (auto simp: exp_def intro!: suminf_le norm_power_ineq)
  finally show ?thesis .
qed

lemma isCont_exp: "isCont exp x"
  for x :: "'a::{real_normed_field,banach}"
  by (rule DERIV_exp [THEN DERIV_isCont])

lemma isCont_exp' [simp]: "isCont f a ⟹ isCont (λx. exp (f x)) a"
  for f :: "_ ⇒'a::{real_normed_field,banach}"
  by (rule isCont_o2 [OF _ isCont_exp])

lemma tendsto_exp [tendsto_intros]: "(f ⤏ a) F ⟹ ((λx. exp (f x)) ⤏ exp a) F"
  for f:: "_ ⇒'a::{real_normed_field,banach}"
  by (rule isCont_tendsto_compose [OF isCont_exp])

lemma continuous_exp [continuous_intros]: "continuous F f ⟹ continuous F (λx. exp (f x))"
  for f :: "_ ⇒'a::{real_normed_field,banach}"
  unfolding continuous_def by (rule tendsto_exp)

lemma continuous_on_exp [continuous_intros]: "continuous_on s f ⟹ continuous_on s (λx. exp (f x))"
  for f :: "_ ⇒'a::{real_normed_field,banach}"
  unfolding continuous_on_def by (auto intro: tendsto_exp)


subsubsection ‹Properties of the Exponential Function›

lemma exp_zero [simp]: "exp 0 = 1"
  unfolding exp_def by (simp add: scaleR_conv_of_real)

lemma exp_series_add_commuting:
  fixes x y :: "'a::{real_normed_algebra_1,banach}"
  defines S_def: "S ≡ λx n. x^n /R fact n"
  assumes comm: "x * y = y * x"
  shows "S (x + y) n = (∑i≤n. S x i * S y (n - i))"
proof (induct n)
  case 0
  show ?case
    unfolding S_def by simp
next
  case (Suc n)
  have S_Suc: "⋀x n. S x (Suc n) = (x * S x n) /R real (Suc n)"
    unfolding S_def by (simp del: mult_Suc)
  then have times_S: "⋀x n. x * S x n = real (Suc n) *R S x (Suc n)"
    by simp
  have S_comm: "⋀n. S x n * y = y * S x n"
    by (simp add: power_commuting_commutes comm S_def)

  have "real (Suc n) *R S (x + y) (Suc n) = (x + y) * S (x + y) n"
    by (simp only: times_S)
  also have "… = (x + y) * (∑i≤n. S x i * S y (n - i))"
    by (simp only: Suc)
  also have "… = x * (∑i≤n. S x i * S y (n - i)) + y * (∑i≤n. S x i * S y (n - i))"
    by (rule distrib_right)
  also have "… = (∑i≤n. x * S x i * S y (n - i)) + (∑i≤n. S x i * y * S y (n - i))"
    by (simp add: sum_distrib_left ac_simps S_comm)
  also have "… = (∑i≤n. x * S x i * S y (n - i)) + (∑i≤n. S x i * (y * S y (n - i)))"
    by (simp add: ac_simps)
  also have "… = (∑i≤n. real (Suc i) *R (S x (Suc i) * S y (n - i))) +
      (∑i≤n. real (Suc n - i) *R (S x i * S y (Suc n - i)))"
    by (simp add: times_S Suc_diff_le)
  also have "(∑i≤n. real (Suc i) *R (S x (Suc i) * S y (n - i))) =
      (∑i≤Suc n. real i *R (S x i * S y (Suc n - i)))"
    by (subst sum_atMost_Suc_shift) simp
  also have "(∑i≤n. real (Suc n - i) *R (S x i * S y (Suc n - i))) =
      (∑i≤Suc n. real (Suc n - i) *R (S x i * S y (Suc n - i)))"
    by simp
  also have "(∑i≤Suc n. real i *R (S x i * S y (Suc n - i))) +
        (∑i≤Suc n. real (Suc n - i) *R (S x i * S y (Suc n - i))) =
      (∑i≤Suc n. real (Suc n) *R (S x i * S y (Suc n - i)))"
    by (simp only: sum.distrib [symmetric] scaleR_left_distrib [symmetric]
        of_nat_add [symmetric]) simp
  also have "… = real (Suc n) *R (∑i≤Suc n. S x i * S y (Suc n - i))"
    by (simp only: scaleR_right.sum)
  finally show "S (x + y) (Suc n) = (∑i≤Suc n. S x i * S y (Suc n - i))"
    by (simp del: sum_cl_ivl_Suc)
qed

lemma exp_add_commuting: "x * y = y * x ⟹ exp (x + y) = exp x * exp y"
  by (simp only: exp_def Cauchy_product summable_norm_exp exp_series_add_commuting)

lemma exp_times_arg_commute: "exp A * A = A * exp A"
  by (simp add: exp_def suminf_mult[symmetric] summable_exp_generic power_commutes suminf_mult2)

lemma exp_add: "exp (x + y) = exp x * exp y"
  for x y :: "'a::{real_normed_field,banach}"
  by (rule exp_add_commuting) (simp add: ac_simps)

lemma exp_double: "exp(2 * z) = exp z ^ 2"
  by (simp add: exp_add_commuting mult_2 power2_eq_square)

lemmas mult_exp_exp = exp_add [symmetric]

lemma exp_of_real: "exp (of_real x) = of_real (exp x)"
  unfolding exp_def
  apply (subst suminf_of_real [OF summable_exp_generic])
  apply (simp add: scaleR_conv_of_real)
  done

lemmas of_real_exp = exp_of_real[symmetric]

corollary exp_in_Reals [simp]: "z ∈ ℝ ⟹ exp z ∈ ℝ"
  by (metis Reals_cases Reals_of_real exp_of_real)

lemma exp_not_eq_zero [simp]: "exp x ≠ 0"
proof
  have "exp x * exp (- x) = 1"
    by (simp add: exp_add_commuting[symmetric])
  also assume "exp x = 0"
  finally show False by simp
qed

lemma exp_minus_inverse: "exp x * exp (- x) = 1"
  by (simp add: exp_add_commuting[symmetric])

lemma exp_minus: "exp (- x) = inverse (exp x)"
  for x :: "'a::{real_normed_field,banach}"
  by (intro inverse_unique [symmetric] exp_minus_inverse)

lemma exp_diff: "exp (x - y) = exp x / exp y"
  for x :: "'a::{real_normed_field,banach}"
  using exp_add [of x "- y"] by (simp add: exp_minus divide_inverse)

lemma exp_of_nat_mult: "exp (of_nat n * x) = exp x ^ n"
  for x :: "'a::{real_normed_field,banach}"
  by (induct n) (auto simp: distrib_left exp_add mult.commute)

corollary exp_of_nat2_mult: "exp (x * of_nat n) = exp x ^ n"
  for x :: "'a::{real_normed_field,banach}"
  by (metis exp_of_nat_mult mult_of_nat_commute)

lemma exp_sum: "finite I ⟹ exp (sum f I) = prod (λx. exp (f x)) I"
  by (induct I rule: finite_induct) (auto simp: exp_add_commuting mult.commute)

lemma exp_divide_power_eq:
  fixes x :: "'a::{real_normed_field,banach}"
  assumes "n > 0"
  shows "exp (x / of_nat n) ^ n = exp x"
  using assms
proof (induction n arbitrary: x)
  case (Suc n)
  show ?case
  proof (cases "n = 0")
    case True
    then show ?thesis by simp
  next
    case False
    then have [simp]: "x * of_nat n / (1 + of_nat n) / of_nat n = x / (1 + of_nat n)"
      by simp
    have [simp]: "x / (1 + of_nat n) + x * of_nat n / (1 + of_nat n) = x"
      apply (simp add: divide_simps)
      using of_nat_eq_0_iff apply (fastforce simp: distrib_left)
      done
    show ?thesis
      using Suc.IH [of "x * of_nat n / (1 + of_nat n)"] False
      by (simp add: exp_add [symmetric])
  qed
qed simp


subsubsection ‹Properties of the Exponential Function on Reals›

text ‹Comparisons of @{term "exp x"} with zero.›

text ‹Proof: because every exponential can be seen as a square.›
lemma exp_ge_zero [simp]: "0 ≤ exp x"
  for x :: real
proof -
  have "0 ≤ exp (x/2) * exp (x/2)"
    by simp
  then show ?thesis
    by (simp add: exp_add [symmetric])
qed

lemma exp_gt_zero [simp]: "0 < exp x"
  for x :: real
  by (simp add: order_less_le)

lemma not_exp_less_zero [simp]: "¬ exp x < 0"
  for x :: real
  by (simp add: not_less)

lemma not_exp_le_zero [simp]: "¬ exp x ≤ 0"
  for x :: real
  by (simp add: not_le)

lemma abs_exp_cancel [simp]: "¦exp x¦ = exp x"
  for x :: real
  by simp

text ‹Strict monotonicity of exponential.›

lemma exp_ge_add_one_self_aux:
  fixes x :: real
  assumes "0 ≤ x"
  shows "1 + x ≤ exp x"
  using order_le_imp_less_or_eq [OF assms]
proof
  assume "0 < x"
  have "1 + x ≤ (∑n<2. inverse (fact n) * x^n)"
    by (auto simp: numeral_2_eq_2)
  also have "… ≤ (∑n. inverse (fact n) * x^n)"
    apply (rule sum_le_suminf [OF summable_exp])
    using ‹0 < x›
    apply (auto  simp add: zero_le_mult_iff)
    done
  finally show "1 + x ≤ exp x"
    by (simp add: exp_def)
qed auto

lemma exp_gt_one: "0 < x ⟹ 1 < exp x"
  for x :: real
proof -
  assume x: "0 < x"
  then have "1 < 1 + x" by simp
  also from x have "1 + x ≤ exp x"
    by (simp add: exp_ge_add_one_self_aux)
  finally show ?thesis .
qed

lemma exp_less_mono:
  fixes x y :: real
  assumes "x < y"
  shows "exp x < exp y"
proof -
  from ‹x < y› have "0 < y - x" by simp
  then have "1 < exp (y - x)" by (rule exp_gt_one)
  then have "1 < exp y / exp x" by (simp only: exp_diff)
  then show "exp x < exp y" by simp
qed

lemma exp_less_cancel: "exp x < exp y ⟹ x < y"
  for x y :: real
  unfolding linorder_not_le [symmetric]
  by (auto simp: order_le_less exp_less_mono)

lemma exp_less_cancel_iff [iff]: "exp x < exp y ⟷ x < y"
  for x y :: real
  by (auto intro: exp_less_mono exp_less_cancel)

lemma exp_le_cancel_iff [iff]: "exp x ≤ exp y ⟷ x ≤ y"
  for x y :: real
  by (auto simp: linorder_not_less [symmetric])

lemma exp_inj_iff [iff]: "exp x = exp y ⟷ x = y"
  for x y :: real
  by (simp add: order_eq_iff)

text ‹Comparisons of @{term "exp x"} with one.›

lemma one_less_exp_iff [simp]: "1 < exp x ⟷ 0 < x"
  for x :: real
  using exp_less_cancel_iff [where x = 0 and y = x] by simp

lemma exp_less_one_iff [simp]: "exp x < 1 ⟷ x < 0"
  for x :: real
  using exp_less_cancel_iff [where x = x and y = 0] by simp

lemma one_le_exp_iff [simp]: "1 ≤ exp x ⟷ 0 ≤ x"
  for x :: real
  using exp_le_cancel_iff [where x = 0 and y = x] by simp

lemma exp_le_one_iff [simp]: "exp x ≤ 1 ⟷ x ≤ 0"
  for x :: real
  using exp_le_cancel_iff [where x = x and y = 0] by simp

lemma exp_eq_one_iff [simp]: "exp x = 1 ⟷ x = 0"
  for x :: real
  using exp_inj_iff [where x = x and y = 0] by simp

lemma lemma_exp_total: "1 ≤ y ⟹ ∃x. 0 ≤ x ∧ x ≤ y - 1 ∧ exp x = y"
  for y :: real
proof (rule IVT)
  assume "1 ≤ y"
  then have "0 ≤ y - 1" by simp
  then have "1 + (y - 1) ≤ exp (y - 1)"
    by (rule exp_ge_add_one_self_aux)
  then show "y ≤ exp (y - 1)" by simp
qed (simp_all add: le_diff_eq)

lemma exp_total: "0 < y ⟹ ∃x. exp x = y"
  for y :: real
proof (rule linorder_le_cases [of 1 y])
  assume "1 ≤ y"
  then show "∃x. exp x = y"
    by (fast dest: lemma_exp_total)
next
  assume "0 < y" and "y ≤ 1"
  then have "1 ≤ inverse y"
    by (simp add: one_le_inverse_iff)
  then obtain x where "exp x = inverse y"
    by (fast dest: lemma_exp_total)
  then have "exp (- x) = y"
    by (simp add: exp_minus)
  then show "∃x. exp x = y" ..
qed


subsection ‹Natural Logarithm›

class ln = real_normed_algebra_1 + banach +
  fixes ln :: "'a ⇒ 'a"
  assumes ln_one [simp]: "ln 1 = 0"

definition powr :: "'a ⇒ 'a ⇒ 'a::ln"  (infixr "powr" 80)
  ― ‹exponentation via ln and exp›
  where  [code del]: "x powr a ≡ if x = 0 then 0 else exp (a * ln x)"

lemma powr_0 [simp]: "0 powr z = 0"
  by (simp add: powr_def)


instantiation real :: ln
begin

definition ln_real :: "real ⇒ real"
  where "ln_real x = (THE u. exp u = x)"

instance
  by intro_classes (simp add: ln_real_def)

end

lemma powr_eq_0_iff [simp]: "w powr z = 0 ⟷ w = 0"
  by (simp add: powr_def)

lemma ln_exp [simp]: "ln (exp x) = x"
  for x :: real
  by (simp add: ln_real_def)

lemma exp_ln [simp]: "0 < x ⟹ exp (ln x) = x"
  for x :: real
  by (auto dest: exp_total)

lemma exp_ln_iff [simp]: "exp (ln x) = x ⟷ 0 < x"
  for x :: real
  by (metis exp_gt_zero exp_ln)

lemma ln_unique: "exp y = x ⟹ ln x = y"
  for x :: real
  by (erule subst) (rule ln_exp)

lemma ln_mult: "0 < x ⟹ 0 < y ⟹ ln (x * y) = ln x + ln y"
  for x :: real
  by (rule ln_unique) (simp add: exp_add)

lemma ln_prod: "finite I ⟹ (⋀i. i ∈ I ⟹ f i > 0) ⟹ ln (prod f I) = sum (λx. ln(f x)) I"
  for f :: "'a ⇒ real"
  by (induct I rule: finite_induct) (auto simp: ln_mult prod_pos)

lemma ln_inverse: "0 < x ⟹ ln (inverse x) = - ln x"
  for x :: real
  by (rule ln_unique) (simp add: exp_minus)

lemma ln_div: "0 < x ⟹ 0 < y ⟹ ln (x / y) = ln x - ln y"
  for x :: real
  by (rule ln_unique) (simp add: exp_diff)

lemma ln_realpow: "0 < x ⟹ ln (x^n) = real n * ln x"
  by (rule ln_unique) (simp add: exp_of_nat_mult)

lemma ln_less_cancel_iff [simp]: "0 < x ⟹ 0 < y ⟹ ln x < ln y ⟷ x < y"
  for x :: real
  by (subst exp_less_cancel_iff [symmetric]) simp

lemma ln_le_cancel_iff [simp]: "0 < x ⟹ 0 < y ⟹ ln x ≤ ln y ⟷ x ≤ y"
  for x :: real
  by (simp add: linorder_not_less [symmetric])

lemma ln_inj_iff [simp]: "0 < x ⟹ 0 < y ⟹ ln x = ln y ⟷ x = y"
  for x :: real
  by (simp add: order_eq_iff)

lemma ln_add_one_self_le_self: "0 ≤ x ⟹ ln (1 + x) ≤ x"
  for x :: real
  by (rule exp_le_cancel_iff [THEN iffD1]) (simp add: exp_ge_add_one_self_aux)

lemma ln_less_self [simp]: "0 < x ⟹ ln x < x"
  for x :: real
  by (rule order_less_le_trans [where y = "ln (1 + x)"]) (simp_all add: ln_add_one_self_le_self)

lemma ln_ge_iff: "⋀x::real. 0 < x ⟹ y ≤ ln x ⟷ exp y ≤ x"
  using exp_le_cancel_iff exp_total by force

lemma ln_ge_zero [simp]: "1 ≤ x ⟹ 0 ≤ ln x"
  for x :: real
  using ln_le_cancel_iff [of 1 x] by simp

lemma ln_ge_zero_imp_ge_one: "0 ≤ ln x ⟹ 0 < x ⟹ 1 ≤ x"
  for x :: real
  using ln_le_cancel_iff [of 1 x] by simp

lemma ln_ge_zero_iff [simp]: "0 < x ⟹ 0 ≤ ln x ⟷ 1 ≤ x"
  for x :: real
  using ln_le_cancel_iff [of 1 x] by simp

lemma ln_less_zero_iff [simp]: "0 < x ⟹ ln x < 0 ⟷ x < 1"
  for x :: real
  using ln_less_cancel_iff [of x 1] by simp

lemma ln_le_zero_iff [simp]: "0 < x ⟹ ln x ≤ 0 ⟷ x ≤ 1"
  for x :: real
  by (metis less_numeral_extra(1) ln_le_cancel_iff ln_one)

lemma ln_gt_zero: "1 < x ⟹ 0 < ln x"
  for x :: real
  using ln_less_cancel_iff [of 1 x] by simp

lemma ln_gt_zero_imp_gt_one: "0 < ln x ⟹ 0 < x ⟹ 1 < x"
  for x :: real
  using ln_less_cancel_iff [of 1 x] by simp

lemma ln_gt_zero_iff [simp]: "0 < x ⟹ 0 < ln x ⟷ 1 < x"
  for x :: real
  using ln_less_cancel_iff [of 1 x] by simp

lemma ln_eq_zero_iff [simp]: "0 < x ⟹ ln x = 0 ⟷ x = 1"
  for x :: real
  using ln_inj_iff [of x 1] by simp

lemma ln_less_zero: "0 < x ⟹ x < 1 ⟹ ln x < 0"
  for x :: real
  by simp

lemma ln_neg_is_const: "x ≤ 0 ⟹ ln x = (THE x. False)"
  for x :: real
  by (auto simp: ln_real_def intro!: arg_cong[where f = The])

lemma isCont_ln:
  fixes x :: real
  assumes "x ≠ 0"
  shows "isCont ln x"
proof (cases "0 < x")
  case True
  then have "isCont ln (exp (ln x))"
    by (intro isCont_inverse_function[where d = "¦x¦" and f = exp]) auto
  with True show ?thesis
    by simp
next
  case False
  with ‹x ≠ 0› show "isCont ln x"
    unfolding isCont_def
    by (subst filterlim_cong[OF _ refl, of _ "nhds (ln 0)" _ "λ_. ln 0"])
       (auto simp: ln_neg_is_const not_less eventually_at dist_real_def
         intro!: exI[of _ "¦x¦"])
qed

lemma tendsto_ln [tendsto_intros]: "(f ⤏ a) F ⟹ a ≠ 0 ⟹ ((λx. ln (f x)) ⤏ ln a) F"
  for a :: real
  by (rule isCont_tendsto_compose [OF isCont_ln])

lemma continuous_ln:
  "continuous F f ⟹ f (Lim F (λx. x)) ≠ 0 ⟹ continuous F (λx. ln (f x :: real))"
  unfolding continuous_def by (rule tendsto_ln)

lemma isCont_ln' [continuous_intros]:
  "continuous (at x) f ⟹ f x ≠ 0 ⟹ continuous (at x) (λx. ln (f x :: real))"
  unfolding continuous_at by (rule tendsto_ln)

lemma continuous_within_ln [continuous_intros]:
  "continuous (at x within s) f ⟹ f x ≠ 0 ⟹ continuous (at x within s) (λx. ln (f x :: real))"
  unfolding continuous_within by (rule tendsto_ln)

lemma continuous_on_ln [continuous_intros]:
  "continuous_on s f ⟹ (∀x∈s. f x ≠ 0) ⟹ continuous_on s (λx. ln (f x :: real))"
  unfolding continuous_on_def by (auto intro: tendsto_ln)

lemma DERIV_ln: "0 < x ⟹ DERIV ln x :> inverse x"
  for x :: real
  by (rule DERIV_inverse_function [where f=exp and a=0 and b="x+1"])
    (auto intro: DERIV_cong [OF DERIV_exp exp_ln] isCont_ln)

lemma DERIV_ln_divide: "0 < x ⟹ DERIV ln x :> 1 / x"
  for x :: real
  by (rule DERIV_ln[THEN DERIV_cong]) (simp_all add: divide_inverse)

declare DERIV_ln_divide[THEN DERIV_chain2, derivative_intros]
  and DERIV_ln_divide[THEN DERIV_chain2, unfolded has_field_derivative_def, derivative_intros]

lemmas has_derivative_ln[derivative_intros] = DERIV_ln[THEN DERIV_compose_FDERIV]

lemma ln_series:
  assumes "0 < x" and "x < 2"
  shows "ln x = (∑ n. (-1)^n * (1 / real (n + 1)) * (x - 1)^(Suc n))"
    (is "ln x = suminf (?f (x - 1))")
proof -
  let ?f' = "λx n. (-1)^n * (x - 1)^n"

  have "ln x - suminf (?f (x - 1)) = ln 1 - suminf (?f (1 - 1))"
  proof (rule DERIV_isconst3 [where x = x])
    fix x :: real
    assume "x ∈ {0 <..< 2}"
    then have "0 < x" and "x < 2" by auto
    have "norm (1 - x) < 1"
      using ‹0 < x› and ‹x < 2› by auto
    have "1 / x = 1 / (1 - (1 - x))" by auto
    also have "… = (∑ n. (1 - x)^n)"
      using geometric_sums[OF ‹norm (1 - x) < 1›] by (rule sums_unique)
    also have "… = suminf (?f' x)"
      unfolding power_mult_distrib[symmetric]
      by (rule arg_cong[where f=suminf], rule arg_cong[where f="(^)"], auto)
    finally have "DERIV ln x :> suminf (?f' x)"
      using DERIV_ln[OF ‹0 < x›] unfolding divide_inverse by auto
    moreover
    have repos: "⋀ h x :: real. h - 1 + x = h + x - 1" by auto
    have "DERIV (λx. suminf (?f x)) (x - 1) :>
      (∑n. (-1)^n * (1 / real (n + 1)) * real (Suc n) * (x - 1) ^ n)"
    proof (rule DERIV_power_series')
      show "x - 1 ∈ {- 1<..<1}" and "(0 :: real) < 1"
        using ‹0 < x› ‹x < 2› by auto
    next
      fix x :: real
      assume "x ∈ {- 1<..<1}"
      then have "norm (-x) < 1" by auto
      show "summable (λn. (- 1) ^ n * (1 / real (n + 1)) * real (Suc n) * x^n)"
        unfolding One_nat_def
        by (auto simp: power_mult_distrib[symmetric] summable_geometric[OF ‹norm (-x) < 1›])
    qed
    then have "DERIV (λx. suminf (?f x)) (x - 1) :> suminf (?f' x)"
      unfolding One_nat_def by auto
    then have "DERIV (λx. suminf (?f (x - 1))) x :> suminf (?f' x)"
      unfolding DERIV_def repos .
    ultimately have "DERIV (λx. ln x - suminf (?f (x - 1))) x :> suminf (?f' x) - suminf (?f' x)"
      by (rule DERIV_diff)
    then show "DERIV (λx. ln x - suminf (?f (x - 1))) x :> 0" by auto
  qed (auto simp: assms)
  then show ?thesis by auto
qed

lemma exp_first_terms:
  fixes x :: "'a::{real_normed_algebra_1,banach}"
  shows "exp x = (∑n<k. inverse(fact n) *R (x ^ n)) + (∑n. inverse(fact (n + k)) *R (x ^ (n + k)))"
proof -
  have "exp x = suminf (λn. inverse(fact n) *R (x^n))"
    by (simp add: exp_def)
  also from summable_exp_generic have "… = (∑ n. inverse(fact(n+k)) *R (x ^ (n + k))) +
    (∑ n::nat<k. inverse(fact n) *R (x^n))" (is "_ = _ + ?a")
    by (rule suminf_split_initial_segment)
  finally show ?thesis by simp
qed

lemma exp_first_term: "exp x = 1 + (∑n. inverse (fact (Suc n)) *R (x ^ Suc n))"
  for x :: "'a::{real_normed_algebra_1,banach}"
  using exp_first_terms[of x 1] by simp

lemma exp_first_two_terms: "exp x = 1 + x + (∑n. inverse (fact (n + 2)) *R (x ^ (n + 2)))"
  for x :: "'a::{real_normed_algebra_1,banach}"
  using exp_first_terms[of x 2] by (simp add: eval_nat_numeral)

lemma exp_bound:
  fixes x :: real
  assumes a: "0 ≤ x"
    and b: "x ≤ 1"
  shows "exp x ≤ 1 + x + x2"
proof -
  have "suminf (λn. inverse(fact (n+2)) * (x ^ (n + 2))) ≤ x2"
  proof -
    have "(λn. x2 / 2 * (1 / 2) ^ n) sums (x2 / 2 * (1 / (1 - 1 / 2)))"
      by (intro sums_mult geometric_sums) simp
    then have sumsx: "(λn. x2 / 2 * (1 / 2) ^ n) sums x2"
      by simp
    have "suminf (λn. inverse(fact (n+2)) * (x ^ (n + 2))) ≤ suminf (λn. (x2/2) * ((1/2)^n))"
    proof (intro suminf_le allI)
      show "inverse (fact (n + 2)) * x ^ (n + 2) ≤ (x2/2) * ((1/2)^n)" for n :: nat
      proof -
        have "(2::nat) * 2 ^ n ≤ fact (n + 2)"
          by (induct n) simp_all
        then have "real ((2::nat) * 2 ^ n) ≤ real_of_nat (fact (n + 2))"
          by (simp only: of_nat_le_iff)
        then have "((2::real) * 2 ^ n) ≤ fact (n + 2)"
          unfolding of_nat_fact by simp
        then have "inverse (fact (n + 2)) ≤ inverse ((2::real) * 2 ^ n)"
          by (rule le_imp_inverse_le) simp
        then have "inverse (fact (n + 2)) ≤ 1/(2::real) * (1/2)^n"
          by (simp add: power_inverse [symmetric])
        then have "inverse (fact (n + 2)) * (x^n * x2) ≤ 1/2 * (1/2)^n * (1 * x2)"
          by (rule mult_mono) (rule mult_mono, simp_all add: power_le_one a b)
        then show ?thesis
          unfolding power_add by (simp add: ac_simps del: fact_Suc)
      qed
      show "summable (λn. inverse (fact (n + 2)) * x ^ (n + 2))"
        by (rule summable_exp [THEN summable_ignore_initial_segment])
      show "summable (λn. x2 / 2 * (1 / 2) ^ n)"
        by (rule sums_summable [OF sumsx])
    qed
    also have "… = x2"
      by (rule sums_unique [THEN sym]) (rule sumsx)
    finally show ?thesis .
  qed
  then show ?thesis
    unfolding exp_first_two_terms by auto
qed

corollary exp_half_le2: "exp(1/2) ≤ (2::real)"
  using exp_bound [of "1/2"]
  by (simp add: field_simps)

corollary exp_le: "exp 1 ≤ (3::real)"
  using exp_bound [of 1]
  by (simp add: field_simps)

lemma exp_bound_half: "norm z ≤ 1/2 ⟹ norm (exp z) ≤ 2"
  by (blast intro: order_trans intro!: exp_half_le2 norm_exp)

lemma exp_bound_lemma:
  assumes "norm z ≤ 1/2"
  shows "norm (exp z) ≤ 1 + 2 * norm z"
proof -
  have *: "(norm z)2 ≤ norm z * 1"
    unfolding power2_eq_square
    by (rule mult_left_mono) (use assms in auto)
  have "norm (exp z) ≤ exp (norm z)"
    by (rule norm_exp)
  also have "… ≤ 1 + (norm z) + (norm z)2"
    using assms exp_bound by auto
  also have "… ≤ 1 + 2 * norm z"
    using * by auto
  finally show ?thesis .
qed

lemma real_exp_bound_lemma: "0 ≤ x ⟹ x ≤ 1/2 ⟹ exp x ≤ 1 + 2 * x"
  for x :: real
  using exp_bound_lemma [of x] by simp

lemma ln_one_minus_pos_upper_bound:
  fixes x :: real
  assumes a: "0 ≤ x" and b: "x < 1"
  shows "ln (1 - x) ≤ - x"
proof -
  have "(1 - x) * (1 + x + x2) = 1 - x^3"
    by (simp add: algebra_simps power2_eq_square power3_eq_cube)
  also have "… ≤ 1"
    by (auto simp: a)
  finally have "(1 - x) * (1 + x + x2) ≤ 1" .
  moreover have c: "0 < 1 + x + x2"
    by (simp add: add_pos_nonneg a)
  ultimately have "1 - x ≤ 1 / (1 + x + x2)"
    by (elim mult_imp_le_div_pos)
  also have "… ≤ 1 / exp x"
    by (metis a abs_one b exp_bound exp_gt_zero frac_le less_eq_real_def real_sqrt_abs
        real_sqrt_pow2_iff real_sqrt_power)
  also have "… = exp (- x)"
    by (auto simp: exp_minus divide_inverse)
  finally have "1 - x ≤ exp (- x)" .
  also have "1 - x = exp (ln (1 - x))"
    by (metis b diff_0 exp_ln_iff less_iff_diff_less_0 minus_diff_eq)
  finally have "exp (ln (1 - x)) ≤ exp (- x)" .
  then show ?thesis
    by (auto simp only: exp_le_cancel_iff)
qed

lemma exp_ge_add_one_self [simp]: "1 + x ≤ exp x"
  for x :: real
proof (cases "0 ≤ x ∨ x ≤ -1")
  case True
  then show ?thesis
    apply (rule disjE)
     apply (simp add: exp_ge_add_one_self_aux)
    using exp_ge_zero order_trans real_add_le_0_iff by blast
next
  case False
  then have ln1: "ln (1 + x) ≤ x"
    using ln_one_minus_pos_upper_bound [of "-x"] by simp
  have "1 + x = exp (ln (1 + x))"
    using False by auto
  also have "… ≤ exp x"
    by (simp add: ln1)
  finally show ?thesis .
qed

lemma ln_one_plus_pos_lower_bound:
  fixes x :: real
  assumes a: "0 ≤ x" and b: "x ≤ 1"
  shows "x - x2 ≤ ln (1 + x)"
proof -
  have "exp (x - x2) = exp x / exp (x2)"
    by (rule exp_diff)
  also have "… ≤ (1 + x + x2) / exp (x 2)"
    by (metis a b divide_right_mono exp_bound exp_ge_zero)
  also have "… ≤ (1 + x + x2) / (1 + x2)"
    by (simp add: a divide_left_mono add_pos_nonneg)
  also from a have "… ≤ 1 + x"
    by (simp add: field_simps add_strict_increasing zero_le_mult_iff)
  finally have "exp (x - x2) ≤ 1 + x" .
  also have "… = exp (ln (1 + x))"
  proof -
    from a have "0 < 1 + x" by auto
    then show ?thesis
      by (auto simp only: exp_ln_iff [THEN sym])
  qed
  finally have "exp (x - x2) ≤ exp (ln (1 + x))" .
  then show ?thesis
    by (metis exp_le_cancel_iff)
qed

lemma ln_one_minus_pos_lower_bound:
  fixes x :: real
  assumes a: "0 ≤ x" and b: "x ≤ 1 / 2"
  shows "- x - 2 * x2 ≤ ln (1 - x)"
proof -
  from b have c: "x < 1" by auto
  then have "ln (1 - x) = - ln (1 + x / (1 - x))"
    by (auto simp: ln_inverse [symmetric] field_simps intro: arg_cong [where f=ln])
  also have "- (x / (1 - x)) ≤ …"
  proof -
    have "ln (1 + x / (1 - x)) ≤ x / (1 - x)"
      using a c by (intro ln_add_one_self_le_self) auto
    then show ?thesis
      by auto
  qed
  also have "- (x / (1 - x)) = - x / (1 - x)"
    by auto
  finally have d: "- x / (1 - x) ≤ ln (1 - x)" .
  have "0 < 1 - x" using a b by simp
  then have e: "- x - 2 * x2 ≤ - x / (1 - x)"
    using mult_right_le_one_le[of "x * x" "2 * x"] a b
    by (simp add: field_simps power2_eq_square)
  from e d show "- x - 2 * x2 ≤ ln (1 - x)"
    by (rule order_trans)
qed

lemma ln_add_one_self_le_self2:
  fixes x :: real
  shows "-1 < x ⟹ ln (1 + x) ≤ x"
  by (metis diff_gt_0_iff_gt diff_minus_eq_add exp_ge_add_one_self exp_le_cancel_iff exp_ln minus_less_iff)

lemma abs_ln_one_plus_x_minus_x_bound_nonneg:
  fixes x :: real
  assumes x: "0 ≤ x" and x1: "x ≤ 1"
  shows "¦ln (1 + x) - x¦ ≤ x2"
proof -
  from x have "ln (1 + x) ≤ x"
    by (rule ln_add_one_self_le_self)
  then have "ln (1 + x) - x ≤ 0"
    by simp
  then have "¦ln(1 + x) - x¦ = - (ln(1 + x) - x)"
    by (rule abs_of_nonpos)
  also have "… = x - ln (1 + x)"
    by simp
  also have "… ≤ x2"
  proof -
    from x x1 have "x - x2 ≤ ln (1 + x)"
      by (intro ln_one_plus_pos_lower_bound)
    then show ?thesis
      by simp
  qed
  finally show ?thesis .
qed

lemma abs_ln_one_plus_x_minus_x_bound_nonpos:
  fixes x :: real
  assumes a: "-(1 / 2) ≤ x" and b: "x ≤ 0"
  shows "¦ln (1 + x) - x¦ ≤ 2 * x2"
proof -
  have *: "- (-x) - 2 * (-x)2 ≤ ln (1 - (- x))"
    by (metis a b diff_zero ln_one_minus_pos_lower_bound minus_diff_eq neg_le_iff_le) 
  have "¦ln (1 + x) - x¦ = x - ln (1 - (- x))"
    using a ln_add_one_self_le_self2 [of x] by (simp add: abs_if)
  also have "… ≤ 2 * x2"
    using * by (simp add: algebra_simps)
  finally show ?thesis .
qed

lemma abs_ln_one_plus_x_minus_x_bound:
  fixes x :: real
  assumes "¦x¦ ≤ 1 / 2"
  shows "¦ln (1 + x) - x¦ ≤ 2 * x2"
proof (cases "0 ≤ x")
  case True
  then show ?thesis
    using abs_ln_one_plus_x_minus_x_bound_nonneg assms by fastforce
next
  case False
  then show ?thesis
    using abs_ln_one_plus_x_minus_x_bound_nonpos assms by auto
qed

lemma ln_x_over_x_mono:
  fixes x :: real
  assumes x: "exp 1 ≤ x" "x ≤ y"
  shows "ln y / y ≤ ln x / x"
proof -
  note x
  moreover have "0 < exp (1::real)" by simp
  ultimately have a: "0 < x" and b: "0 < y"
    by (fast intro: less_le_trans order_trans)+
  have "x * ln y - x * ln x = x * (ln y - ln x)"
    by (simp add: algebra_simps)
  also have "… = x * ln (y / x)"
    by (simp only: ln_div a b)
  also have "y / x = (x + (y - x)) / x"
    by simp
  also have "… = 1 + (y - x) / x"
    using x a by (simp add: field_simps)
  also have "x * ln (1 + (y - x) / x) ≤ x * ((y - x) / x)"
    using x a
    by (intro mult_left_mono ln_add_one_self_le_self) simp_all
  also have "… = y - x"
    using a by simp
  also have "… = (y - x) * ln (exp 1)" by simp
  also have "… ≤ (y - x) * ln x"
    using a x exp_total of_nat_1 x(1)  by (fastforce intro: mult_left_mono)
  also have "… = y * ln x - x * ln x"
    by (rule left_diff_distrib)
  finally have "x * ln y ≤ y * ln x"
    by arith
  then have "ln y ≤ (y * ln x) / x"
    using a by (simp add: field_simps)
  also have "… = y * (ln x / x)" by simp
  finally show ?thesis
    using b by (simp add: field_simps)
qed

lemma ln_le_minus_one: "0 < x ⟹ ln x ≤ x - 1"
  for x :: real
  using exp_ge_add_one_self[of "ln x"] by simp

corollary ln_diff_le: "0 < x ⟹ 0 < y ⟹ ln x - ln y ≤ (x - y) / y"
  for x :: real
  by (simp add: ln_div [symmetric] diff_divide_distrib ln_le_minus_one)

lemma ln_eq_minus_one:
  fixes x :: real
  assumes "0 < x" "ln x = x - 1"
  shows "x = 1"
proof -
  let ?l = "λy. ln y - y + 1"
  have D: "⋀x::real. 0 < x ⟹ DERIV ?l x :> (1 / x - 1)"
    by (auto intro!: derivative_eq_intros)

  show ?thesis
  proof (cases rule: linorder_cases)
    assume "x < 1"
    from dense[OF ‹x < 1›] obtain a where "x < a" "a < 1" by blast
    from ‹x < a› have "?l x < ?l a"
    proof (rule DERIV_pos_imp_increasing, safe)
      fix y
      assume "x ≤ y" "y ≤ a"
      with ‹0 < x› ‹a < 1› have "0 < 1 / y - 1" "0 < y"
        by (auto simp: field_simps)
      with D show "∃z. DERIV ?l y :> z ∧ 0 < z" by blast
    qed
    also have "… ≤ 0"
      using ln_le_minus_one ‹0 < x› ‹x < a› by (auto simp: field_simps)
    finally show "x = 1" using assms by auto
  next
    assume "1 < x"
    from dense[OF this] obtain a where "1 < a" "a < x" by blast
    from ‹a < x› have "?l x < ?l a"
    proof (rule DERIV_neg_imp_decreasing)
      fix y
      assume "a ≤ y" "y ≤ x"
      with ‹1 < a› have "1 / y - 1 < 0" "0 < y"
        by (auto simp: field_simps)
      with D show "∃z. DERIV ?l y :> z ∧ z < 0"
        by blast
    qed
    also have "… ≤ 0"
      using ln_le_minus_one ‹1 < a› by (auto simp: field_simps)
    finally show "x = 1" using assms by auto
  next
    assume "x = 1"
    then show ?thesis by simp
  qed
qed

lemma ln_x_over_x_tendsto_0: "((λx::real. ln x / x) ⤏ 0) at_top"
proof (rule lhospital_at_top_at_top[where f' = inverse and g' = "λ_. 1"])
  from eventually_gt_at_top[of "0::real"]
  show "∀F x in at_top. (ln has_real_derivative inverse x) (at x)"
    by eventually_elim (auto intro!: derivative_eq_intros simp: field_simps)
qed (use tendsto_inverse_0 in
      ‹auto simp: filterlim_ident dest!: tendsto_mono[OF at_top_le_at_infinity]›)

lemma exp_ge_one_plus_x_over_n_power_n:
  assumes "x ≥ - real n" "n > 0"
  shows "(1 + x / of_nat n) ^ n ≤ exp x"
proof (cases "x = - of_nat n")
  case False
  from assms False have "(1 + x / of_nat n) ^ n = exp (of_nat n * ln (1 + x / of_nat n))"
    by (subst exp_of_nat_mult, subst exp_ln) (simp_all add: field_simps)
  also from assms False have "ln (1 + x / real n) ≤ x / real n"
    by (intro ln_add_one_self_le_self2) (simp_all add: field_simps)
  with assms have "exp (of_nat n * ln (1 + x / of_nat n)) ≤ exp x"
    by (simp add: field_simps)
  finally show ?thesis .
next
  case True
  then show ?thesis by (simp add: zero_power)
qed

lemma exp_ge_one_minus_x_over_n_power_n:
  assumes "x ≤ real n" "n > 0"
  shows "(1 - x / of_nat n) ^ n ≤ exp (-x)"
  using exp_ge_one_plus_x_over_n_power_n[of n "-x"] assms by simp

lemma exp_at_bot: "(exp ⤏ (0::real)) at_bot"
  unfolding tendsto_Zfun_iff
proof (rule ZfunI, simp add: eventually_at_bot_dense)
  fix r :: real
  assume "0 < r"
  have "exp x < r" if "x < ln r" for x
    by (metis ‹0 < r› exp_less_mono exp_ln that)
  then show "∃k. ∀n<k. exp n < r" by auto
qed

lemma exp_at_top: "LIM x at_top. exp x :: real :> at_top"
  by (rule filterlim_at_top_at_top[where Q="λx. True" and P="λx. 0 < x" and g=ln])
    (auto intro: eventually_gt_at_top)

lemma lim_exp_minus_1: "((λz::'a. (exp(z) - 1) / z) ⤏ 1) (at 0)"
  for x :: "'a::{real_normed_field,banach}"
proof -
  have "((λz::'a. exp(z) - 1) has_field_derivative 1) (at 0)"
    by (intro derivative_eq_intros | simp)+
  then show ?thesis
    by (simp add: Deriv.has_field_derivative_iff)
qed

lemma ln_at_0: "LIM x at_right 0. ln (x::real) :> at_bot"
  by (rule filterlim_at_bot_at_right[where Q="λx. 0 < x" and P="λx. True" and g=exp])
     (auto simp: eventually_at_filter)

lemma ln_at_top: "LIM x at_top. ln (x::real) :> at_top"
  by (rule filterlim_at_top_at_top[where Q="λx. 0 < x" and P="λx. True" and g=exp])
     (auto intro: eventually_gt_at_top)

lemma filtermap_ln_at_top: "filtermap (ln::real ⇒ real) at_top = at_top"
  by (intro filtermap_fun_inverse[of exp] exp_at_top ln_at_top) auto

lemma filtermap_exp_at_top: "filtermap (exp::real ⇒ real) at_top = at_top"
  by (intro filtermap_fun_inverse[of ln] exp_at_top ln_at_top)
     (auto simp: eventually_at_top_dense)

lemma filtermap_ln_at_right: "filtermap ln (at_right (0::real)) = at_bot"
  by (auto intro!: filtermap_fun_inverse[where g="λx. exp x"] ln_at_0
      simp: filterlim_at exp_at_bot)

lemma tendsto_power_div_exp_0: "((λx. x ^ k / exp x) ⤏ (0::real)) at_top"
proof (induct k)
  case 0
  show "((λx. x ^ 0 / exp x) ⤏ (0::real)) at_top"
    by (simp add: inverse_eq_divide[symmetric])
       (metis filterlim_compose[OF tendsto_inverse_0] exp_at_top filterlim_mono
         at_top_le_at_infinity order_refl)
next
  case (Suc k)
  show ?case
  proof (rule lhospital_at_top_at_top)
    show "eventually (λx. DERIV (λx. x ^ Suc k) x :> (real (Suc k) * x^k)) at_top"
      by eventually_elim (intro derivative_eq_intros, auto)
    show "eventually (λx. DERIV exp x :> exp x) at_top"
      by eventually_elim auto
    show "eventually (λx. exp x ≠ 0) at_top"
      by auto
    from tendsto_mult[OF tendsto_const Suc, of "real (Suc k)"]
    show "((λx. real (Suc k) * x ^ k / exp x) ⤏ 0) at_top"
      by simp
  qed (rule exp_at_top)
qed

subsubsection‹ A couple of simple bounds›

lemma exp_plus_inverse_exp:
  fixes x::real
  shows "2 ≤ exp x + inverse (exp x)"
proof -
  have "2 ≤ exp x + exp (-x)"
    using exp_ge_add_one_self [of x] exp_ge_add_one_self [of "-x"]
    by linarith
  then show ?thesis
    by (simp add: exp_minus)
qed

lemma real_le_x_sinh:
  fixes x::real
  assumes "0 ≤ x"
  shows "x ≤ (exp x - inverse(exp x)) / 2"
proof -
  have *: "exp a - inverse(exp a) - 2*a ≤ exp b - inverse(exp b) - 2*b" if "a ≤ b" for a b::real
    using exp_plus_inverse_exp
    by (fastforce intro: derivative_eq_intros DERIV_nonneg_imp_nondecreasing [OF that])
  show ?thesis
    using*[OF assms] by simp
qed

lemma real_le_abs_sinh:
  fixes x::real
  shows "abs x ≤ abs((exp x - inverse(exp x)) / 2)"
proof (cases "0 ≤ x")
  case True
  show ?thesis
    using real_le_x_sinh [OF True] True by (simp add: abs_if)
next
  case False
  have "-x ≤ (exp(-x) - inverse(exp(-x))) / 2"
    by (meson False linear neg_le_0_iff_le real_le_x_sinh)
  also have "… ≤ ¦(exp x - inverse (exp x)) / 2¦"
    by (metis (no_types, hide_lams) abs_divide abs_le_iff abs_minus_cancel
       add.inverse_inverse exp_minus minus_diff_eq order_refl)
  finally show ?thesis
    using False by linarith
qed

subsection‹The general logarithm›

definition log :: "real ⇒ real ⇒ real"
  ― ‹logarithm of @{term x} to base @{term a}›
  where "log a x = ln x / ln a"

lemma tendsto_log [tendsto_intros]:
  "(f ⤏ a) F ⟹ (g ⤏ b) F ⟹ 0 < a ⟹ a ≠ 1 ⟹ 0 < b ⟹
    ((λx. log (f x) (g x)) ⤏ log a b) F"
  unfolding log_def by (intro tendsto_intros) auto

lemma continuous_log:
  assumes "continuous F f"
    and "continuous F g"
    and "0 < f (Lim F (λx. x))"
    and "f (Lim F (λx. x)) ≠ 1"
    and "0 < g (Lim F (λx. x))"
  shows "continuous F (λx. log (f x) (g x))"
  using assms unfolding continuous_def by (rule tendsto_log)

lemma continuous_at_within_log[continuous_intros]:
  assumes "continuous (at a within s) f"
    and "continuous (at a within s) g"
    and "0 < f a"
    and "f a ≠ 1"
    and "0 < g a"
  shows "continuous (at a within s) (λx. log (f x) (g x))"
  using assms unfolding continuous_within by (rule tendsto_log)

lemma isCont_log[continuous_intros, simp]:
  assumes "isCont f a" "isCont g a" "0 < f a" "f a ≠ 1" "0 < g a"
  shows "isCont (λx. log (f x) (g x)) a"
  using assms unfolding continuous_at by (rule tendsto_log)

lemma continuous_on_log[continuous_intros]:
  assumes "continuous_on s f" "continuous_on s g"
    and "∀x∈s. 0 < f x" "∀x∈s. f x ≠ 1" "∀x∈s. 0 < g x"
  shows "continuous_on s (λx. log (f x) (g x))"
  using assms unfolding continuous_on_def by (fast intro: tendsto_log)

lemma powr_one_eq_one [simp]: "1 powr a = 1"
  by (simp add: powr_def)

lemma powr_zero_eq_one [simp]: "x powr 0 = (if x = 0 then 0 else 1)"
  by (simp add: powr_def)

lemma powr_one_gt_zero_iff [simp]: "x powr 1 = x ⟷ 0 ≤ x"
  for x :: real
  by (auto simp: powr_def)
declare powr_one_gt_zero_iff [THEN iffD2, simp]

lemma powr_diff:
  fixes w:: "'a::{ln,real_normed_field}" shows  "w powr (z1 - z2) = w powr z1 / w powr z2"
  by (simp add: powr_def algebra_simps exp_diff)

lemma powr_mult: "0 ≤ x ⟹ 0 ≤ y ⟹ (x * y) powr a = (x powr a) * (y powr a)"
  for a x y :: real
  by (simp add: powr_def exp_add [symmetric] ln_mult distrib_left)

lemma powr_ge_pzero [simp]: "0 ≤ x powr y"
  for x y :: real
  by (simp add: powr_def)

lemma powr_non_neg[simp]: "¬a powr x < 0" for a x::real
  using powr_ge_pzero[of a x] by arith

lemma powr_divide: "0 < x ⟹ 0 < y ⟹ (x / y) powr a = (x powr a) / (y powr a)"
  for a b x :: real
  apply (simp add: divide_inverse positive_imp_inverse_positive powr_mult)
  apply (simp add: powr_def exp_minus [symmetric] exp_add [symmetric] ln_inverse)
  done

lemma powr_add: "x powr (a + b) = (x powr a) * (x powr b)"
  for a b x :: "'a::{ln,real_normed_field}"
  by (simp add: powr_def exp_add [symmetric] distrib_right)

lemma powr_mult_base: "0 < x ⟹x * x powr y = x powr (1 + y)"
  for x :: real
  by (auto simp: powr_add)

lemma powr_powr: "(x powr a) powr b = x powr (a * b)"
  for a b x :: real
  by (simp add: powr_def)

lemma powr_powr_swap: "(x powr a) powr b = (x powr b) powr a"
  for a b x :: real
  by (simp add: powr_powr mult.commute)

lemma powr_minus: "x powr (- a) = inverse (x powr a)"
      for a x :: "'a::{ln,real_normed_field}"
  by (simp add: powr_def exp_minus [symmetric])

lemma powr_minus_divide: "x powr (- a) = 1/(x powr a)"
      for a x :: "'a::{ln,real_normed_field}"
  by (simp add: divide_inverse powr_minus)

lemma divide_powr_uminus: "a / b powr c = a * b powr (- c)"
  for a b c :: real
  by (simp add: powr_minus_divide)

lemma powr_less_mono: "a < b ⟹ 1 < x ⟹ x powr a < x powr b"
  for a b x :: real
  by (simp add: powr_def)

lemma powr_less_cancel: "x powr a < x powr b ⟹ 1 < x ⟹ a < b"
  for a b x :: real
  by (simp add: powr_def)

lemma powr_less_cancel_iff [simp]: "1 < x ⟹ x powr a < x powr b ⟷ a < b"
  for a b x :: real
  by (blast intro: powr_less_cancel powr_less_mono)

lemma powr_le_cancel_iff [simp]: "1 < x ⟹ x powr a ≤ x powr b ⟷ a ≤ b"
  for a b x :: real
  by (simp add: linorder_not_less [symmetric])

lemma powr_realpow: "0 < x ⟹ x powr (real n) = x^n"
by (induction n) (simp_all add: ac_simps powr_add)

lemma log_ln: "ln x = log (exp(1)) x"
  by (simp add: log_def)

lemma DERIV_log:
  assumes "x > 0"
  shows "DERIV (λy. log b y) x :> 1 / (ln b * x)"
proof -
  define lb where "lb = 1 / ln b"
  moreover have "DERIV (λy. lb * ln y) x :> lb / x"
    using ‹x > 0› by (auto intro!: derivative_eq_intros)
  ultimately show ?thesis
    by (simp add: log_def)
qed

lemmas DERIV_log[THEN DERIV_chain2, derivative_intros]
  and DERIV_log[THEN DERIV_chain2, unfolded has_field_derivative_def, derivative_intros]

lemma powr_log_cancel [simp]: "0 < a ⟹ a ≠ 1 ⟹ 0 < x ⟹ a powr (log a x) = x"
  by (simp add: powr_def log_def)

lemma log_powr_cancel [simp]: "0 < a ⟹ a ≠ 1 ⟹ log a (a powr y) = y"
  by (simp add: log_def powr_def)

lemma log_mult:
  "0 < a ⟹ a ≠ 1 ⟹ 0 < x ⟹ 0 < y ⟹
    log a (x * y) = log a x + log a y"
  by (simp add: log_def ln_mult divide_inverse distrib_right)

lemma log_eq_div_ln_mult_log:
  "0 < a ⟹ a ≠ 1 ⟹ 0 < b ⟹ b ≠ 1 ⟹ 0 < x ⟹
    log a x = (ln b/ln a) * log b x"
  by (simp add: log_def divide_inverse)

text‹Base 10 logarithms›
lemma log_base_10_eq1: "0 < x ⟹ log 10 x = (ln (exp 1) / ln 10) * ln x"
  by (simp add: log_def)

lemma log_base_10_eq2: "0 < x ⟹ log 10 x = (log 10 (exp 1)) * ln x"
  by (simp add: log_def)

lemma log_one [simp]: "log a 1 = 0"
  by (simp add: log_def)

lemma log_eq_one [simp]: "0 < a ⟹ a ≠ 1 ⟹ log a a = 1"
  by (simp add: log_def)

lemma log_inverse: "0 < a ⟹ a ≠ 1 ⟹ 0 < x ⟹ log a (inverse x) = - log a x"
  using ln_inverse log_def by auto

lemma log_divide: "0 < a ⟹ a ≠ 1 ⟹ 0 < x ⟹ 0 < y ⟹ log a (x/y) = log a x - log a y"
  by (simp add: log_mult divide_inverse log_inverse)

lemma powr_gt_zero [simp]: "0 < x powr a ⟷ x ≠ 0"
  for a x :: real
  by (simp add: powr_def)

lemma powr_nonneg_iff[simp]: "a powr x ≤ 0 ⟷ a = 0"
  for a x::real
  by (meson not_less powr_gt_zero)

lemma log_add_eq_powr: "0 < b ⟹ b ≠ 1 ⟹ 0 < x ⟹ log b x + y = log b (x * b powr y)"
  and add_log_eq_powr: "0 < b ⟹ b ≠ 1 ⟹ 0 < x ⟹ y + log b x = log b (b powr y * x)"
  and log_minus_eq_powr: "0 < b ⟹ b ≠ 1 ⟹ 0 < x ⟹ log b x - y = log b (x * b powr -y)"
  and minus_log_eq_powr: "0 < b ⟹ b ≠ 1 ⟹ 0 < x ⟹ y - log b x = log b (b powr y / x)"
  by (simp_all add: log_mult log_divide)

lemma log_less_cancel_iff [simp]: "1 < a ⟹ 0 < x ⟹ 0 < y ⟹ log a x < log a y ⟷ x < y"
  using powr_less_cancel_iff [of a] powr_log_cancel [of a x] powr_log_cancel [of a y]
  by (metis less_eq_real_def less_trans not_le zero_less_one)

lemma log_inj:
  assumes "1 < b"
  shows "inj_on (log b) {0 <..}"
proof (rule inj_onI, simp)
  fix x y
  assume pos: "0 < x" "0 < y" and *: "log b x = log b y"
  show "x = y"
  proof (cases rule: linorder_cases)
    assume "x = y"
    then show ?thesis by simp
  next
    assume "x < y"
    then have "log b x < log b y"
      using log_less_cancel_iff[OF ‹1 < b›] pos by simp
    then show ?thesis using * by simp
  next
    assume "y < x"
    then have "log b y < log b x"
      using log_less_cancel_iff[OF ‹1 < b›] pos by simp
    then show ?thesis using * by simp
  qed
qed

lemma log_le_cancel_iff [simp]: "1 < a ⟹ 0 < x ⟹ 0 < y ⟹ log a x ≤ log a y ⟷ x ≤ y"
  by (simp add: linorder_not_less [symmetric])

lemma zero_less_log_cancel_iff[simp]: "1 < a ⟹ 0 < x ⟹ 0 < log a x ⟷ 1 < x"
  using log_less_cancel_iff[of a 1 x] by simp

lemma zero_le_log_cancel_iff[simp]: "1 < a ⟹ 0 < x ⟹ 0 ≤ log a x ⟷ 1 ≤ x"
  using log_le_cancel_iff[of a 1 x] by simp

lemma log_less_zero_cancel_iff[simp]: "1 < a ⟹ 0 < x ⟹ log a x < 0 ⟷ x < 1"
  using log_less_cancel_iff[of a x 1] by simp

lemma log_le_zero_cancel_iff[simp]: "1 < a ⟹ 0 < x ⟹ log a x ≤ 0 ⟷ x ≤ 1"
  using log_le_cancel_iff[of a x 1] by simp

lemma one_less_log_cancel_iff[simp]: "1 < a ⟹ 0 < x ⟹ 1 < log a x ⟷ a < x"
  using log_less_cancel_iff[of a a x] by simp

lemma one_le_log_cancel_iff[simp]: "1 < a ⟹ 0 < x ⟹ 1 ≤ log a x ⟷ a ≤ x"
  using log_le_cancel_iff[of a a x] by simp

lemma log_less_one_cancel_iff[simp]: "1 < a ⟹ 0 < x ⟹ log a x < 1 ⟷ x < a"
  using log_less_cancel_iff[of a x a] by simp

lemma log_le_one_cancel_iff[simp]: "1 < a ⟹ 0 < x ⟹ log a x ≤ 1 ⟷ x ≤ a"
  using log_le_cancel_iff[of a x a] by simp

lemma le_log_iff:
  fixes b x y :: real
  assumes "1 < b" "x > 0"
  shows "y ≤ log b x ⟷ b powr y ≤ x"
  using assms
  by (metis less_irrefl less_trans powr_le_cancel_iff powr_log_cancel zero_less_one)

lemma less_log_iff:
  assumes "1 < b" "x > 0"
  shows "y < log b x ⟷ b powr y < x"
  by (metis assms dual_order.strict_trans less_irrefl powr_less_cancel_iff
    powr_log_cancel zero_less_one)

lemma
  assumes "1 < b" "x > 0"
  shows log_less_iff: "log b x < y ⟷ x < b powr y"
    and log_le_iff: "log b x ≤ y ⟷ x ≤ b powr y"
  using le_log_iff[OF assms, of y] less_log_iff[OF assms, of y]
  by auto

lemmas powr_le_iff = le_log_iff[symmetric]
  and powr_less_iff = less_log_iff[symmetric]
  and less_powr_iff = log_less_iff[symmetric]
  and le_powr_iff = log_le_iff[symmetric]

lemma le_log_of_power:
  assumes "b ^ n ≤ m" "1 < b"
  shows "n ≤ log b m"
proof -
  from assms have "0 < m" by (metis less_trans zero_less_power less_le_trans zero_less_one)
  thus ?thesis using assms by (simp add: le_log_iff powr_realpow)
qed

lemma le_log2_of_power: "2 ^ n ≤ m ⟹ n ≤ log 2 m" for m n :: nat
using le_log_of_power[of 2] by simp

lemma log_of_power_le: "⟦ m ≤ b ^ n; b > 1; m > 0 ⟧ ⟹ log b (real m) ≤ n"
by (simp add: log_le_iff powr_realpow)

lemma log2_of_power_le: "⟦ m ≤ 2 ^ n; m > 0 ⟧ ⟹ log 2 m ≤ n" for m n :: nat
using log_of_power_le[of _ 2] by simp

lemma log_of_power_less: "⟦ m < b ^ n; b > 1; m > 0 ⟧ ⟹ log b (real m) < n"
by (simp add: log_less_iff powr_realpow)

lemma log2_of_power_less: "⟦ m < 2 ^ n; m > 0 ⟧ ⟹ log 2 m < n" for m n :: nat
using log_of_power_less[of _ 2] by simp

lemma less_log_of_power:
  assumes "b ^ n < m" "1 < b"
  shows "n < log b m"
proof -
  have "0 < m" by (metis assms less_trans zero_less_power zero_less_one)
  thus ?thesis using assms by (simp add: less_log_iff powr_realpow)
qed

lemma less_log2_of_power: "2 ^ n < m ⟹ n < log 2 m" for m n :: nat
using less_log_of_power[of 2] by simp

lemma gr_one_powr[simp]:
  fixes x y :: real shows "⟦ x > 1; y > 0 ⟧ ⟹ 1 < x powr y"
by(simp add: less_powr_iff)

lemma floor_log_eq_powr_iff: "x > 0 ⟹ b > 1 ⟹ ⌊log b x⌋ = k ⟷ b powr k ≤ x ∧ x < b powr (k + 1)"
  by (auto simp: floor_eq_iff powr_le_iff less_powr_iff)

lemma floor_log_nat_eq_powr_iff: fixes b n k :: nat
  shows "⟦ b ≥ 2; k > 0 ⟧ ⟹
  floor (log b (real k)) = n ⟷ b^n ≤ k ∧ k < b^(n+1)"
by (auto simp: floor_log_eq_powr_iff powr_add powr_realpow
               of_nat_power[symmetric] of_nat_mult[symmetric] ac_simps
         simp del: of_nat_power of_nat_mult)

lemma floor_log_nat_eq_if: fixes b n k :: nat
  assumes "b^n ≤ k" "k < b^(n+1)" "b ≥ 2"
  shows "floor (log b (real k)) = n"
proof -
  have "k ≥ 1" using assms(1,3) one_le_power[of b n] by linarith
  with assms show ?thesis by(simp add: floor_log_nat_eq_powr_iff)
qed

lemma ceiling_log_eq_powr_iff: "⟦ x > 0; b > 1 ⟧
  ⟹ ⌈log b x⌉ = int k + 1 ⟷ b powr k < x ∧ x ≤ b powr (k + 1)"
by (auto simp: ceiling_eq_iff powr_less_iff le_powr_iff)

lemma ceiling_log_nat_eq_powr_iff: fixes b n k :: nat
  shows "⟦ b ≥ 2; k > 0 ⟧ ⟹
  ceiling (log b (real k)) = int n + 1 ⟷ (b^n < k ∧ k ≤ b^(n+1))"
using ceiling_log_eq_powr_iff
by (auto simp: powr_add powr_realpow of_nat_power[symmetric] of_nat_mult[symmetric] ac_simps
         simp del: of_nat_power of_nat_mult)

lemma ceiling_log_nat_eq_if: fixes b n k :: nat
  assumes "b^n < k" "k ≤ b^(n+1)" "b ≥ 2"
  shows "ceiling (log b (real k)) = int n + 1"
proof -
  have "k ≥ 1" using assms(1,3) one_le_power[of b n] by linarith
  with assms show ?thesis by(simp add: ceiling_log_nat_eq_powr_iff)
qed

lemma floor_log2_div2: fixes n :: nat assumes "n ≥ 2"
shows "floor(log 2 n) = floor(log 2 (n div 2)) + 1"
proof cases
  assume "n=2" thus ?thesis by simp
next
  let ?m = "n div 2"
  assume "n≠2"
  hence "1 ≤ ?m" using assms by arith
  then obtain i where i: "2 ^ i ≤ ?m" "?m < 2 ^ (i + 1)"
    using ex_power_ivl1[of 2 ?m] by auto
  have "2^(i+1) ≤ 2*?m" using i(1) by simp
  also have "2*?m ≤ n" by arith
  finally have *: "2^(i+1) ≤ …" .
  have "n < 2^(i+1+1)" using i(2) by simp
  from floor_log_nat_eq_if[OF * this] floor_log_nat_eq_if[OF i]
  show ?thesis by simp
qed

lemma ceiling_log2_div2: assumes "n ≥ 2"
shows "ceiling(log 2 (real n)) = ceiling(log 2 ((n-1) div 2 + 1)) + 1"
proof cases
  assume "n=2" thus ?thesis by simp
next
  let ?m = "(n-1) div 2 + 1"
  assume "n≠2"
  hence "2 ≤ ?m" using assms by arith
  then obtain i where i: "2 ^ i < ?m" "?m ≤ 2 ^ (i + 1)"
    using ex_power_ivl2[of 2 ?m] by auto
  have "n ≤ 2*?m" by arith
  also have "2*?m ≤ 2 ^ ((i+1)+1)" using i(2) by simp
  finally have *: "n ≤ …" .
  have "2^(i+1) < n" using i(1) by (auto simp: less_Suc_eq_0_disj)
  from ceiling_log_nat_eq_if[OF this *] ceiling_log_nat_eq_if[OF i]
  show ?thesis by simp
qed

lemma powr_real_of_int:
  "x > 0 ⟹ x powr real_of_int n = (if n ≥ 0 then x ^ nat n else inverse (x ^ nat (- n)))"
  using powr_realpow[of x "nat n"] powr_realpow[of x "nat (-n)"]
  by (auto simp: field_simps powr_minus)

lemma powr_numeral [simp]: "0 < x ⟹ x powr (numeral n :: real) = x ^ (numeral n)"
  by (metis of_nat_numeral powr_realpow)

lemma powr_int:
  assumes "x > 0"
  shows "x powr i = (if i ≥ 0 then x ^ nat i else 1 / x ^ nat (-i))"
proof (cases "i < 0")
  case True
  have r: "x powr i = 1 / x powr (- i)"
    by (simp add: powr_minus field_simps)
  show ?thesis using ‹i < 0› ‹x > 0›
    by (simp add: r field_simps powr_realpow[symmetric])
next
  case False
  then show ?thesis
    by (simp add: assms powr_realpow[symmetric])
qed

lemma compute_powr[code]:
  fixes i :: real
  shows "b powr i =
    (if b ≤ 0 then Code.abort (STR ''op powr with nonpositive base'') (λ_. b powr i)
     else if ⌊i⌋ = i then (if 0 ≤ i then b ^ nat ⌊i⌋ else 1 / b ^ nat ⌊- i⌋)
     else Code.abort (STR ''op powr with non-integer exponent'') (λ_. b powr i))"
  by (auto simp: powr_int)

lemma powr_one: "0 ≤ x ⟹ x powr 1 = x"
  for x :: real
  using powr_realpow [of x 1] by simp

lemma powr_neg_one: "0 < x ⟹ x powr - 1 = 1 / x"
  for x :: real
  using powr_int [of x "- 1"] by simp

lemma powr_neg_numeral: "0 < x ⟹ x powr - numeral n = 1 / x ^ numeral n"
  for x :: real
  using powr_int [of x "- numeral n"] by simp

lemma root_powr_inverse: "0 < n ⟹ 0 < x ⟹ root n x = x powr (1/n)"
  by (rule real_root_pos_unique) (auto simp: powr_realpow[symmetric] powr_powr)

lemma ln_powr: "x ≠ 0 ⟹ ln (x powr y) = y * ln x"
  for x :: real
  by (simp add: powr_def)

lemma ln_root: "n > 0 ⟹ b > 0 ⟹ ln (root n b) =  ln b / n"
  by (simp add: root_powr_inverse ln_powr)

lemma ln_sqrt: "0 < x ⟹ ln (sqrt x) = ln x / 2"
  by (simp add: ln_powr ln_powr[symmetric] mult.commute)

lemma log_root: "n > 0 ⟹ a > 0 ⟹ log b (root n a) =  log b a / n"
  by (simp add: log_def ln_root)

lemma log_powr: "x ≠ 0 ⟹ log b (x powr y) = y * log b x"
  by (simp add: log_def ln_powr)

(* [simp] is not worth it, interferes with some proofs *)
lemma log_nat_power: "0 < x ⟹ log b (x^n) = real n * log b x"
  by (simp add: log_powr powr_realpow [symmetric])

lemma log_of_power_eq:
  assumes "m = b ^ n" "b > 1"
  shows "n = log b (real m)"
proof -
  have "n = log b (b ^ n)" using assms(2) by (simp add: log_nat_power)
  also have "… = log b m" using assms by simp
  finally show ?thesis .
qed

lemma log2_of_power_eq: "m = 2 ^ n ⟹ n = log 2 m" for m n :: nat
using log_of_power_eq[of _ 2] by simp

lemma log_base_change: "0 < a ⟹ a ≠ 1 ⟹ log b x = log a x / log a b"
  by (simp add: log_def)

lemma log_base_pow: "0 < a ⟹ log (a ^ n) x = log a x / n"
  by (simp add: log_def ln_realpow)

lemma log_base_powr: "a ≠ 0 ⟹ log (a powr b) x = log a x / b"
  by (simp add: log_def ln_powr)

lemma log_base_root: "n > 0 ⟹ b > 0 ⟹ log (root n b) x = n * (log b x)"
  by (simp add: log_def ln_root)

lemma ln_bound: "0 < x ⟹ ln x ≤ x" for x :: real
  using ln_le_minus_one by force

lemma powr_mono:
  fixes x :: real
  assumes "a ≤ b" and "1 ≤ x" shows "x powr a ≤ x powr b"
  using assms less_eq_real_def by auto

lemma ge_one_powr_ge_zero: "1 ≤ x ⟹ 0 ≤ a ⟹ 1 ≤ x powr a"
  for x :: real
  using powr_mono by fastforce

lemma powr_less_mono2: "0 < a ⟹ 0 ≤ x ⟹ x < y ⟹ x powr a < y powr a"
  for x :: real
  by (simp add: powr_def)

lemma powr_less_mono2_neg: "a < 0 ⟹ 0 < x ⟹ x < y ⟹ y powr a < x powr a"
  for x :: real
  by (simp add: powr_def)

lemma powr_mono2: "x powr a ≤ y powr a" if "0 ≤ a" "0 ≤ x" "x ≤ y"
  for x :: real
  using less_eq_real_def powr_less_mono2 that by auto

lemma powr_le1: "0 ≤ a ⟹ 0 ≤ x ⟹ x ≤ 1 ⟹ x powr a ≤ 1"
  for x :: real
  using powr_mono2 by fastforce

lemma powr_mono2':
  fixes a x y :: real
  assumes "a ≤ 0" "x > 0" "x ≤ y"
  shows "x powr a ≥ y powr a"
proof -
  from assms have "x powr - a ≤ y powr - a"
    by (intro powr_mono2) simp_all
  with assms show ?thesis
    by (auto simp: powr_minus field_simps)
qed

lemma powr_mono_both:
  fixes x :: real
  assumes "0 ≤ a" "a ≤ b" "1 ≤ x" "x ≤ y"
    shows "x powr a ≤ y powr b"
  by (meson assms order.trans powr_mono powr_mono2 zero_le_one)

lemma powr_inj: "0 < a ⟹ a ≠ 1 ⟹ a powr x = a powr y ⟷ x = y"
  for x :: real
  unfolding powr_def exp_inj_iff by simp

lemma powr_half_sqrt: "0 ≤ x ⟹ x powr (1/2) = sqrt x"
  by (simp add: powr_def root_powr_inverse sqrt_def)

lemma ln_powr_bound: "1 ≤ x ⟹ 0 < a ⟹ ln x ≤ (x powr a) / a"
  for x :: real
  by (metis exp_gt_zero linear ln_eq_zero_iff ln_exp ln_less_self ln_powr mult.commute
      mult_imp_le_div_pos not_less powr_gt_zero)

lemma ln_powr_bound2:
  fixes x :: real
  assumes "1 < x" and "0 < a"
  shows "(ln x) powr a ≤ (a powr a) * x"
proof -
  from assms have "ln x ≤ (x powr (1 / a)) / (1 / a)"
    by (metis less_eq_real_def ln_powr_bound zero_less_divide_1_iff)
  also have "… = a * (x powr (1 / a))"
    by simp
  finally have "(ln x) powr a ≤ (a * (x powr (1 / a))) powr a"
    by (metis assms less_imp_le ln_gt_zero powr_mono2)
  also have "… = (a powr a) * ((x powr (1 / a)) powr a)"
    using assms powr_mult by auto
  also have "(x powr (1 / a)) powr a = x powr ((1 / a) * a)"
    by (rule powr_powr)
  also have "… = x" using assms
    by auto
  finally show ?thesis .
qed

lemma tendsto_powr:
  fixes a b :: real
  assumes f: "(f ⤏ a) F"
    and g: "(g ⤏ b) F"
    and a: "a ≠ 0"
  shows "((λx. f x powr g x) ⤏ a powr b) F"
  unfolding powr_def
proof (rule filterlim_If)
  from f show "((λx. 0) ⤏ (if a = 0 then 0 else exp (b * ln a))) (inf F (principal {x. f x = 0}))"
    by simp (auto simp: filterlim_iff eventually_inf_principal elim: eventually_mono dest: t1_space_nhds)
  from f g a show "((λx. exp (g x * ln (f x))) ⤏ (if a = 0 then 0 else exp (b * ln a)))
      (inf F (principal {x. f x ≠ 0}))"
    by (auto intro!: tendsto_intros intro: tendsto_mono inf_le1)
qed

lemma tendsto_powr'[tendsto_intros]:
  fixes a :: real
  assumes f: "(f ⤏ a) F"
    and g: "(g ⤏ b) F"
    and a: "a ≠ 0 ∨ (b > 0 ∧ eventually (λx. f x ≥ 0) F)"
  shows "((λx. f x powr g x) ⤏ a powr b) F"
proof -
  from a consider "a ≠ 0" | "a = 0" "b > 0" "eventually (λx. f x ≥ 0) F"
    by auto
  then show ?thesis
  proof cases
    case 1
    with f g show ?thesis by (rule tendsto_powr)
  next
    case 2
    have "((λx. if f x = 0 then 0 else exp (g x * ln (f x))) ⤏ 0) F"
    proof (intro filterlim_If)
      have "filterlim f (principal {0<..}) (inf F (principal {z. f z ≠ 0}))"
        using ‹eventually (λx. f x ≥ 0) F›
        by (auto simp: filterlim_iff eventually_inf_principal
            eventually_principal elim: eventually_mono)
      moreover have "filterlim f (nhds a) (inf F (principal {z. f z ≠ 0}))"
        by (rule tendsto_mono[OF _ f]) simp_all
      ultimately have f: "filterlim f (at_right 0) (inf F (principal {x. f x ≠ 0}))"
        by (simp add: at_within_def filterlim_inf ‹a = 0›)
      have g: "(g ⤏ b) (inf F (principal {z. f z ≠ 0}))"
        by (rule tendsto_mono[OF _ g]) simp_all
      show "((λx. exp (g x * ln (f x))) ⤏ 0) (inf F (principal {x. f x ≠ 0}))"
        by (rule filterlim_compose[OF exp_at_bot] filterlim_tendsto_pos_mult_at_bot
                 filterlim_compose[OF ln_at_0] f g ‹b > 0›)+
    qed simp_all
    with ‹a = 0› show ?thesis
      by (simp add: powr_def)
  qed
qed

lemma continuous_powr:
  assumes "continuous F f"
    and "continuous F g"
    and "f (Lim F (λx. x)) ≠ 0"
  shows "continuous F (λx. (f x) powr (g x :: real))"
  using assms unfolding continuous_def by (rule tendsto_powr)

lemma continuous_at_within_powr[continuous_intros]:
  fixes f g :: "_ ⇒ real"
  assumes "continuous (at a within s) f"
    and "continuous (at a within s) g"
    and "f a ≠ 0"
  shows "continuous (at a within s) (λx. (f x) powr (g x))"
  using assms unfolding continuous_within by (rule tendsto_powr)

lemma isCont_powr[continuous_intros, simp]:
  fixes f g :: "_ ⇒ real"
  assumes "isCont f a" "isCont g a" "f a ≠ 0"
  shows "isCont (λx. (f x) powr g x) a"
  using assms unfolding continuous_at by (rule tendsto_powr)

lemma continuous_on_powr[continuous_intros]:
  fixes f g :: "_ ⇒ real"
  assumes "continuous_on s f" "continuous_on s g" and "∀x∈s. f x ≠ 0"
  shows "continuous_on s (λx. (f x) powr (g x))"
  using assms unfolding continuous_on_def by (fast intro: tendsto_powr)

lemma tendsto_powr2:
  fixes a :: real
  assumes f: "(f ⤏ a) F"
    and g: "(g ⤏ b) F"
    and "∀F x in F. 0 ≤ f x"
    and b: "0 < b"
  shows "((λx. f x powr g x) ⤏ a powr b) F"
  using tendsto_powr'[of f a F g b] assms by auto

lemma has_derivative_powr[derivative_intros]:
  assumes g[derivative_intros]: "(g has_derivative g') (at x within X)"
    and f[derivative_intros]:"(f has_derivative f') (at x within X)"
  assumes pos: "0 < g x" and "x ∈ X"
  shows "((λx. g x powr f x::real) has_derivative (λh. (g x powr f x) * (f' h * ln (g x) + g' h * f x / g x))) (at x within X)"
proof -
  have "∀F x in at x within X. g x > 0"
    by (rule order_tendstoD[OF _ pos])
      (rule has_derivative_continuous[OF g, unfolded continuous_within])
  then obtain d where "d > 0" and pos': "⋀x'. x' ∈ X ⟹ dist x' x < d ⟹ 0 < g x'"
    using pos unfolding eventually_at by force
  have "((λx. exp (f x * ln (g x))) has_derivative
    (λh. (g x powr f x) * (f' h * ln (g x) + g' h * f x / g x))) (at x within X)"
    using pos
    by (auto intro!: derivative_eq_intros simp: divide_simps powr_def)
  then show ?thesis
    by (rule has_derivative_transform_within[OF _ ‹d > 0› ‹x ∈ X›]) (auto simp: powr_def dest: pos')
qed

lemma DERIV_powr:
  fixes r :: real
  assumes g: "DERIV g x :> m"
    and pos: "g x > 0"
    and f: "DERIV f x :> r"
  shows "DERIV (λx. g x powr f x) x :> (g x powr f x) * (r * ln (g x) + m * f x / g x)"
  using assms
  by (auto intro!: derivative_eq_intros ext simp: has_field_derivative_def algebra_simps)

lemma DERIV_fun_powr:
  fixes r :: real
  assumes g: "DERIV g x :> m"
    and pos: "g x > 0"
  shows "DERIV (λx. (g x) powr r) x :> r * (g x) powr (r - of_nat 1) * m"
  using DERIV_powr[OF g pos DERIV_const, of r] pos
  by (simp add: powr_diff field_simps)

lemma has_real_derivative_powr:
  assumes "z > 0"
  shows "((λz. z powr r) has_real_derivative r * z powr (r - 1)) (at z)"
proof (subst DERIV_cong_ev[OF refl _ refl])
  from assms have "eventually (λz. z ≠ 0) (nhds z)"
    by (intro t1_space_nhds) auto
  then show "eventually (λz. z powr r = exp (r * ln z)) (nhds z)"
    unfolding powr_def by eventually_elim simp
  from assms show "((λz. exp (r * ln z)) has_real_derivative r * z powr (r - 1)) (at z)"
    by (auto intro!: derivative_eq_intros simp: powr_def field_simps exp_diff)
qed

declare has_real_derivative_powr[THEN DERIV_chain2, derivative_intros]

lemma tendsto_zero_powrI:
  assumes "(f ⤏ (0::real)) F" "(g ⤏ b) F" "∀F x in F. 0 ≤ f x" "0 < b"
  shows "((λx. f x powr g x) ⤏ 0) F"
  using tendsto_powr2[OF assms] by simp

lemma continuous_on_powr':
  fixes f g :: "_ ⇒ real"
  assumes "continuous_on s f" "continuous_on s g"
    and "∀x∈s. f x ≥ 0 ∧ (f x = 0 ⟶ g x > 0)"
  shows "continuous_on s (λx. (f x) powr (g x))"
  unfolding continuous_on_def
proof
  fix x
  assume x: "x ∈ s"
  from assms x show "((λx. f x powr g x) ⤏ f x powr g x) (at x within s)"
  proof (cases "f x = 0")
    case True
    from assms(3) have "eventually (λx. f x ≥ 0) (at x within s)"
      by (auto simp: at_within_def eventually_inf_principal)
    with True x assms show ?thesis
      by (auto intro!: tendsto_zero_powrI[of f _ g "g x"] simp: continuous_on_def)
  next
    case False
    with assms x show ?thesis
      by (auto intro!: tendsto_powr' simp: continuous_on_def)
  qed
qed

lemma tendsto_neg_powr:
  assumes "s < 0"
    and f: "LIM x F. f x :> at_top"
  shows "((λx. f x powr s) ⤏ (0::real)) F"
proof -
  have "((λx. exp (s * ln (f x))) ⤏ (0::real)) F" (is "?X")
    by (auto intro!: filterlim_compose[OF exp_at_bot] filterlim_compose[OF ln_at_top]
        filterlim_tendsto_neg_mult_at_bot assms)
  also have "?X ⟷ ((λx. f x powr s) ⤏ (0::real)) F"
    using f filterlim_at_top_dense[of f F]
    by (intro filterlim_cong[OF refl refl]) (auto simp: neq_iff powr_def elim: eventually_mono)
  finally show ?thesis .
qed

lemma tendsto_exp_limit_at_right: "((λy. (1 + x * y) powr (1 / y)) ⤏ exp x) (at_right 0)"
  for x :: real
proof (cases "x = 0")
  case True
  then show ?thesis by simp
next
  case False
  have "((λy. ln (1 + x * y)::real) has_real_derivative 1 * x) (at 0)"
    by (auto intro!: derivative_eq_intros)
  then have "((λy. ln (1 + x * y) / y) ⤏ x) (at 0)"
    by (auto simp: has_field_derivative_def field_has_derivative_at)
  then have *: "((λy. exp (ln (1 + x * y) / y)) ⤏ exp x) (at 0)"
    by (rule tendsto_intros)
  then show ?thesis
  proof (rule filterlim_mono_eventually)
    show "eventually (λxa. exp (ln (1 + x * xa) / xa) = (1 + x * xa) powr (1 / xa)) (at_right 0)"
      unfolding eventually_at_right[OF zero_less_one]
      using False
      by (intro exI[of _ "1 / ¦x¦"]) (auto simp: field_simps powr_def abs_if add_nonneg_eq_0_iff)
  qed (simp_all add: at_eq_sup_left_right)
qed

lemma tendsto_exp_limit_at_top: "((λy. (1 + x / y) powr y) ⤏ exp x) at_top"
  for x :: real
  by (simp add: filterlim_at_top_to_right inverse_eq_divide tendsto_exp_limit_at_right)

lemma tendsto_exp_limit_sequentially: "(λn. (1 + x / n) ^ n) ⇢ exp x"
  for x :: real
proof (rule filterlim_mono_eventually)
  from reals_Archimedean2 [of "¦x¦"] obtain n :: nat where *: "real n > ¦x¦" ..
  then have "eventually (λn :: nat. 0 < 1 + x / real n) at_top"
    by (intro eventually_sequentiallyI [of n]) (auto simp: divide_simps)
  then show "eventually (λn. (1 + x / n) powr n = (1 + x / n) ^ n) at_top"
    by (rule eventually_mono) (erule powr_realpow)
  show "(λn. (1 + x / real n) powr real n) ⇢ exp x"
    by (rule filterlim_compose [OF tendsto_exp_limit_at_top filterlim_real_sequentially])
qed auto


subsection ‹Sine and Cosine›

definition sin_coeff :: "nat ⇒ real"
  where "sin_coeff = (λn. if even n then 0 else (- 1) ^ ((n - Suc 0) div 2) / (fact n))"

definition cos_coeff :: "nat ⇒ real"
  where "cos_coeff = (λn. if even n then ((- 1) ^ (n div 2)) / (fact n) else 0)"

definition sin :: "'a ⇒ 'a::{real_normed_algebra_1,banach}"
  where "sin = (λx. ∑n. sin_coeff n *R x^n)"

definition cos :: "'a ⇒ 'a::{real_normed_algebra_1,banach}"
  where "cos = (λx. ∑n. cos_coeff n *R x^n)"

lemma sin_coeff_0 [simp]: "sin_coeff 0 = 0"
  unfolding sin_coeff_def by simp

lemma cos_coeff_0 [simp]: "cos_coeff 0 = 1"
  unfolding cos_coeff_def by simp

lemma sin_coeff_Suc: "sin_coeff (Suc n) = cos_coeff n / real (Suc n)"
  unfolding cos_coeff_def sin_coeff_def
  by (simp del: mult_Suc)

lemma cos_coeff_Suc: "cos_coeff (Suc n) = - sin_coeff n / real (Suc n)"
  unfolding cos_coeff_def sin_coeff_def
  by (simp del: mult_Suc) (auto elim: oddE)

lemma summable_norm_sin: "summable (λn. norm (sin_coeff n *R x^n))"
  for x :: "'a::{real_normed_algebra_1,banach}"
  unfolding sin_coeff_def
  apply (rule summable_comparison_test [OF _ summable_norm_exp [where x=x]])
  apply (auto simp: divide_inverse abs_mult power_abs [symmetric] zero_le_mult_iff)
  done

lemma summable_norm_cos: "summable (λn. norm (cos_coeff n *R x^n))"
  for x :: "'a::{real_normed_algebra_1,banach}"
  unfolding cos_coeff_def
  apply (rule summable_comparison_test [OF _ summable_norm_exp [where x=x]])
  apply (auto simp: divide_inverse abs_mult power_abs [symmetric] zero_le_mult_iff)
  done

lemma sin_converges: "(λn. sin_coeff n *R x^n) sums sin x"
  unfolding sin_def
  by (metis (full_types) summable_norm_cancel summable_norm_sin summable_sums)

lemma cos_converges: "(λn. cos_coeff n *R x^n) sums cos x"
  unfolding cos_def
  by (metis (full_types) summable_norm_cancel summable_norm_cos summable_sums)

lemma sin_of_real: "sin (of_real x) = of_real (sin x)"
  for x :: real
proof -
  have "(λn. of_real (sin_coeff n *R  x^n)) = (λn. sin_coeff n *R  (of_real x)^n)"
  proof
    show "of_real (sin_coeff n *R  x^n) = sin_coeff n *R of_real x^n" for n
      by (simp add: scaleR_conv_of_real)
  qed
  also have "… sums (sin (of_real x))"
    by (rule sin_converges)
  finally have "(λn. of_real (sin_coeff n *R x^n)) sums (sin (of_real x))" .
  then show ?thesis
    using sums_unique2 sums_of_real [OF sin_converges]
    by blast
qed

corollary sin_in_Reals [simp]: "z ∈ ℝ ⟹ sin z ∈ ℝ"
  by (metis Reals_cases Reals_of_real sin_of_real)

lemma cos_of_real: "cos (of_real x) = of_real (cos x)"
  for x :: real
proof -
  have "(λn. of_real (cos_coeff n *R  x^n)) = (λn. cos_coeff n *R  (of_real x)^n)"
  proof
    show "of_real (cos_coeff n *R  x^n) = cos_coeff n *R of_real x^n" for n
      by (simp add: scaleR_conv_of_real)
  qed
  also have "… sums (cos (of_real x))"
    by (rule cos_converges)
  finally have "(λn. of_real (cos_coeff n *R x^n)) sums (cos (of_real x))" .
  then show ?thesis
    using sums_unique2 sums_of_real [OF cos_converges]
    by blast
qed

corollary cos_in_Reals [simp]: "z ∈ ℝ ⟹ cos z ∈ ℝ"
  by (metis Reals_cases Reals_of_real cos_of_real)

lemma diffs_sin_coeff: "diffs sin_coeff = cos_coeff"
  by (simp add: diffs_def sin_coeff_Suc del: of_nat_Suc)

lemma diffs_cos_coeff: "diffs cos_coeff = (λn. - sin_coeff n)"
  by (simp add: diffs_def cos_coeff_Suc del: of_nat_Suc)

lemma sin_int_times_real: "sin (of_int m * of_real x) = of_real (sin (of_int m * x))"
  by (metis sin_of_real of_real_mult of_real_of_int_eq)

lemma cos_int_times_real: "cos (of_int m * of_real x) = of_real (cos (of_int m * x))"
  by (metis cos_of_real of_real_mult of_real_of_int_eq)

text ‹Now at last we can get the derivatives of exp, sin and cos.›

lemma DERIV_sin [simp]: "DERIV sin x :> cos x"
  for x :: "'a::{real_normed_field,banach}"
  unfolding sin_def cos_def scaleR_conv_of_real
  apply (rule DERIV_cong)
   apply (rule termdiffs [where K="of_real (norm x) + 1 :: 'a"])
      apply (simp_all add: norm_less_p1 diffs_of_real diffs_sin_coeff diffs_cos_coeff
              summable_minus_iff scaleR_conv_of_real [symmetric]
              summable_norm_sin [THEN summable_norm_cancel]
              summable_norm_cos [THEN summable_norm_cancel])
  done

declare DERIV_sin[THEN DERIV_chain2, derivative_intros]
  and DERIV_sin[THEN DERIV_chain2, unfolded has_field_derivative_def, derivative_intros]

lemmas has_derivative_sin[derivative_intros] = DERIV_sin[THEN DERIV_compose_FDERIV]

lemma DERIV_cos [simp]: "DERIV cos x :> - sin x"
  for x :: "'a::{real_normed_field,banach}"
  unfolding sin_def cos_def scaleR_conv_of_real
  apply (rule DERIV_cong)
   apply (rule termdiffs [where K="of_real (norm x) + 1 :: 'a"])
      apply (simp_all add: norm_less_p1 diffs_of_real diffs_minus suminf_minus
              diffs_sin_coeff diffs_cos_coeff
              summable_minus_iff scaleR_conv_of_real [symmetric]
              summable_norm_sin [THEN summable_norm_cancel]
              summable_norm_cos [THEN summable_norm_cancel])
  done

declare DERIV_cos[THEN DERIV_chain2, derivative_intros]
  and DERIV_cos[THEN DERIV_chain2, unfolded has_field_derivative_def, derivative_intros]

lemmas has_derivative_cos[derivative_intros] = DERIV_cos[THEN DERIV_compose_FDERIV]

lemma isCont_sin: "isCont sin x"
  for x :: "'a::{real_normed_field,banach}"
  by (rule DERIV_sin [THEN DERIV_isCont])

lemma isCont_cos: "isCont cos x"
  for x :: "'a::{real_normed_field,banach}"
  by (rule DERIV_cos [THEN DERIV_isCont])

lemma isCont_sin' [simp]: "isCont f a ⟹ isCont (λx. sin (f x)) a"
  for f :: "_ ⇒ 'a::{real_normed_field,banach}"
  by (rule isCont_o2 [OF _ isCont_sin])

(* FIXME a context for f would be better *)

lemma isCont_cos' [simp]: "isCont f a ⟹ isCont (λx. cos (f x)) a"
  for f :: "_ ⇒ 'a::{real_normed_field,banach}"
  by (rule isCont_o2 [OF _ isCont_cos])

lemma tendsto_sin [tendsto_intros]: "(f ⤏ a) F ⟹ ((λx. sin (f x)) ⤏ sin a) F"
  for f :: "_ ⇒ 'a::{real_normed_field,banach}"
  by (rule isCont_tendsto_compose [OF isCont_sin])

lemma tendsto_cos [tendsto_intros]: "(f ⤏ a) F ⟹ ((λx. cos (f x)) ⤏ cos a) F"
  for f :: "_ ⇒ 'a::{real_normed_field,banach}"
  by (rule isCont_tendsto_compose [OF isCont_cos])

lemma continuous_sin [continuous_intros]: "continuous F f ⟹ continuous F (λx. sin (f x))"
  for f :: "_ ⇒ 'a::{real_normed_field,banach}"
  unfolding continuous_def by (rule tendsto_sin)

lemma continuous_on_sin [continuous_intros]: "continuous_on s f ⟹ continuous_on s (λx. sin (f x))"
  for f :: "_ ⇒ 'a::{real_normed_field,banach}"
  unfolding continuous_on_def by (auto intro: tendsto_sin)

lemma continuous_within_sin: "continuous (at z within s) sin"
  for z :: "'a::{real_normed_field,banach}"
  by (simp add: continuous_within tendsto_sin)

lemma continuous_cos [continuous_intros]: "continuous F f ⟹ continuous F (λx. cos (f x))"
  for f :: "_ ⇒ 'a::{real_normed_field,banach}"
  unfolding continuous_def by (rule tendsto_cos)

lemma continuous_on_cos [continuous_intros]: "continuous_on s f ⟹ continuous_on s (λx. cos (f x))"
  for f :: "_ ⇒ 'a::{real_normed_field,banach}"
  unfolding continuous_on_def by (auto intro: tendsto_cos)

lemma continuous_within_cos: "continuous (at z within s) cos"
  for z :: "'a::{real_normed_field,banach}"
  by (simp add: continuous_within tendsto_cos)


subsection ‹Properties of Sine and Cosine›

lemma sin_zero [simp]: "sin 0 = 0"
  by (simp add: sin_def sin_coeff_def scaleR_conv_of_real)

lemma cos_zero [simp]: "cos 0 = 1"
  by (simp add: cos_def cos_coeff_def scaleR_conv_of_real)

lemma DERIV_fun_sin: "DERIV g x :> m ⟹ DERIV (λx. sin (g x)) x :> cos (g x) * m"
  by (auto intro!: derivative_intros)

lemma DERIV_fun_cos: "DERIV g x :> m ⟹ DERIV (λx. cos(g x)) x :> - sin (g x) * m"
  by (auto intro!: derivative_eq_intros)


subsection ‹Deriving the Addition Formulas›

text ‹The product of two cosine series.›
lemma cos_x_cos_y:
  fixes x :: "'a::{real_normed_field,banach}"
  shows
    "(λp. ∑n≤p.
        if even p ∧ even n
        then ((-1) ^ (p div 2) * (p choose n) / (fact p)) *R (x^n) * y^(p-n) else 0)
      sums (cos x * cos y)"
proof -
  have "(cos_coeff n * cos_coeff (p - n)) *R (x^n * y^(p - n)) =
    (if even p ∧ even n then ((-1) ^ (p div 2) * (p choose n) / (fact p)) *R (x^n) * y^(p - n)
     else 0)"
    if "n ≤ p" for n p :: nat
  proof -
    from that have *: "even n ⟹ even p ⟹
        (-1) ^ (n div 2) * (-1) ^ ((p - n) div 2) = (-1 :: real) ^ (p div 2)"
      by (metis div_add power_add le_add_diff_inverse odd_add)
    with that show ?thesis
      by (auto simp: algebra_simps cos_coeff_def binomial_fact)
  qed
  then have "(λp. ∑n≤p. if even p ∧ even n
                  then ((-1) ^ (p div 2) * (p choose n) / (fact p)) *R (x^n) * y^(p-n) else 0) =
             (λp. ∑n≤p. (cos_coeff n * cos_coeff (p - n)) *R (x^n * y^(p-n)))"
    by simp
  also have "… = (λp. ∑n≤p. (cos_coeff n *R x^n) * (cos_coeff (p - n) *R y^(p-n)))"
    by (simp add: algebra_simps)
  also have "… sums (cos x * cos y)"
    using summable_norm_cos
    by (auto simp: cos_def scaleR_conv_of_real intro!: Cauchy_product_sums)
  finally show ?thesis .
qed

text ‹The product of two sine series.›
lemma sin_x_sin_y:
  fixes x :: "'a::{real_normed_field,banach}"
  shows
    "(λp. ∑n≤p.
        if even p ∧ odd n
        then - ((-1) ^ (p div 2) * (p choose n) / (fact p)) *R (x^n) * y^(p-n)
        else 0)
      sums (sin x * sin y)"
proof -
  have "(sin_coeff n * sin_coeff (p - n)) *R (x^n * y^(p-n)) =
    (if even p ∧ odd n
     then -((-1) ^ (p div 2) * (p choose n) / (fact p)) *R (x^n) * y^(p-n)
     else 0)"
    if "n ≤ p" for n p :: nat
  proof -
    have "(-1) ^ ((n - Suc 0) div 2) * (-1) ^ ((p - Suc n) div 2) = - ((-1 :: real) ^ (p div 2))"
      if np: "odd n" "even p"
    proof -
      from ‹n ≤ p› np have *: "n - Suc 0 + (p - Suc n) = p - Suc (Suc 0)" "Suc (Suc 0) ≤ p"
        by arith+
      have "(p - Suc (Suc 0)) div 2 = p div 2 - Suc 0"
        by simp
      with ‹n ≤ p› np * show ?thesis
        apply (simp add: power_add [symmetric] div_add [symmetric] del: div_add)
        apply (metis (no_types) One_nat_def Suc_1 le_div_geq minus_minus
            mult.left_neutral mult_minus_left power.simps(2) zero_less_Suc)
        done
    qed
    then show ?thesis
      using ‹n≤p› by (auto simp: algebra_simps sin_coeff_def binomial_fact)
  qed
  then have "(λp. ∑n≤p. if even p ∧ odd n
               then - ((-1) ^ (p div 2) * (p choose n) / (fact p)) *R (x^n) * y^(p-n) else 0) =
             (λp. ∑n≤p. (sin_coeff n * sin_coeff (p - n)) *R (x^n * y^(p-n)))"
    by simp
  also have "… = (λp. ∑n≤p. (sin_coeff n *R x^n) * (sin_coeff (p - n) *R y^(p-n)))"
    by (simp add: algebra_simps)
  also have "… sums (sin x * sin y)"
    using summable_norm_sin
    by (auto simp: sin_def scaleR_conv_of_real intro!: Cauchy_product_sums)
  finally show ?thesis .
qed

lemma sums_cos_x_plus_y:
  fixes x :: "'a::{real_normed_field,banach}"
  shows
    "(λp. ∑n≤p.
        if even p
        then ((-1) ^ (p div 2) * (p choose n) / (fact p)) *R (x^n) * y^(p-n)
        else 0)
      sums cos (x + y)"
proof -
  have
    "(∑n≤p.
      if even p then ((-1) ^ (p div 2) * (p choose n) / (fact p)) *R (x^n) * y^(p-n)
      else 0) = cos_coeff p *R ((x + y) ^ p)"
    for p :: nat
  proof -
    have
      "(∑n≤p. if even p then ((-1) ^ (p div 2) * (p choose n) / (fact p)) *R (x^n) * y^(p-n) else 0) =
       (if even p then ∑n≤p. ((-1) ^ (p div 2) * (p choose n) / (fact p)) *R (x^n) * y^(p-n) else 0)"
      by simp
    also have "… =
       (if even p
        then of_real ((-1) ^ (p div 2) / (fact p)) * (∑n≤p. (p choose n) *R (x^n) * y^(p-n))
        else 0)"
      by (auto simp: sum_distrib_left field_simps scaleR_conv_of_real nonzero_of_real_divide)
    also have "… = cos_coeff p *R ((x + y) ^ p)"
      by (simp add: cos_coeff_def binomial_ring [of x y]  scaleR_conv_of_real atLeast0AtMost)
    finally show ?thesis .
  qed
  then have
    "(λp. ∑n≤p.
        if even p
        then ((-1) ^ (p div 2) * (p choose n) / (fact p)) *R (x^n) * y^(p-n)
        else 0) = (λp. cos_coeff p *R ((x+y)^p))"
    by simp
   also have "… sums cos (x + y)"
    by (rule cos_converges)
   finally show ?thesis .
qed

theorem cos_add:
  fixes x :: "'a::{real_normed_field,banach}"
  shows "cos (x + y) = cos x * cos y - sin x * sin y"
proof -
  have
    "(if even p ∧ even n
      then ((- 1) ^ (p div 2) * int (p choose n) / (fact p)) *R (x^n) * y^(p-n) else 0) -
     (if even p ∧ odd n
      then - ((- 1) ^ (p div 2) * int (p choose n) / (fact p)) *R (x^n) * y^(p-n) else 0) =
     (if even p then ((-1) ^ (p div 2) * (p choose n) / (fact p)) *R (x^n) * y^(p-n) else 0)"
    if "n ≤ p" for n p :: nat
    by simp
  then have
    "(λp. ∑n≤p. (if even p then ((-1) ^ (p div 2) * (p choose n) / (fact p)) *R (x^n) * y^(p-n) else 0))
      sums (cos x * cos y - sin x * sin y)"
    using sums_diff [OF cos_x_cos_y [of x y] sin_x_sin_y [of x y]]
    by (simp add: sum_subtractf [symmetric])
  then show ?thesis
    by (blast intro: sums_cos_x_plus_y sums_unique2)
qed

lemma sin_minus_converges: "(λn. - (sin_coeff n *R (-x)^n)) sums sin x"
proof -
  have [simp]: "⋀n. - (sin_coeff n *R (-x)^n) = (sin_coeff n *R x^n)"
    by (auto simp: sin_coeff_def elim!: oddE)
  show ?thesis
    by (simp add: sin_def summable_norm_sin [THEN summable_norm_cancel, THEN summable_sums])
qed

lemma sin_minus [simp]: "sin (- x) = - sin x"
  for x :: "'a::{real_normed_algebra_1,banach}"
  using sin_minus_converges [of x]
  by (auto simp: sin_def summable_norm_sin [THEN summable_norm_cancel]
      suminf_minus sums_iff equation_minus_iff)

lemma cos_minus_converges: "(λn. (cos_coeff n *R (-x)^n)) sums cos x"
proof -
  have [simp]: "⋀n. (cos_coeff n *R (-x)^n) = (cos_coeff n *R x^n)"
    by (auto simp: Transcendental.cos_coeff_def elim!: evenE)
  show ?thesis
    by (simp add: cos_def summable_norm_cos [THEN summable_norm_cancel, THEN summable_sums])
qed

lemma cos_minus [simp]: "cos (-x) = cos x"
  for x :: "'a::{real_normed_algebra_1,banach}"
  using cos_minus_converges [of x]
  by (simp add: cos_def summable_norm_cos [THEN summable_norm_cancel]
      suminf_minus sums_iff equation_minus_iff)

lemma sin_cos_squared_add [simp]: "(sin x)2 + (cos x)2 = 1"
  for x :: "'a::{real_normed_field,banach}"
  using cos_add [of x "-x"]
  by (simp add: power2_eq_square algebra_simps)

lemma sin_cos_squared_add2 [simp]: "(cos x)2 + (sin x)2 = 1"
  for x :: "'a::{real_normed_field,banach}"
  by (subst add.commute, rule sin_cos_squared_add)

lemma sin_cos_squared_add3 [simp]: "cos x * cos x + sin x * sin x = 1"
  for x :: "'a::{real_normed_field,banach}"
  using sin_cos_squared_add2 [unfolded power2_eq_square] .

lemma sin_squared_eq: "(sin x)2 = 1 - (cos x)2"
  for x :: "'a::{real_normed_field,banach}"
  unfolding eq_diff_eq by (rule sin_cos_squared_add)

lemma cos_squared_eq: "(cos x)2 = 1 - (sin x)2"
  for x :: "'a::{real_normed_field,banach}"
  unfolding eq_diff_eq by (rule sin_cos_squared_add2)

lemma abs_sin_le_one [simp]: "¦sin x¦ ≤ 1"
  for x :: real
  by (rule power2_le_imp_le) (simp_all add: sin_squared_eq)

lemma sin_ge_minus_one [simp]: "- 1 ≤ sin x"
  for x :: real
  using abs_sin_le_one [of x] by (simp add: abs_le_iff)

lemma sin_le_one [simp]: "sin x ≤ 1"
  for x :: real
  using abs_sin_le_one [of x] by (simp add: abs_le_iff)

lemma abs_cos_le_one [simp]: "¦cos x¦ ≤ 1"
  for x :: real
  by (rule power2_le_imp_le) (simp_all add: cos_squared_eq)

lemma cos_ge_minus_one [simp]: "- 1 ≤ cos x"
  for x :: real
  using abs_cos_le_one [of x] by (simp add: abs_le_iff)

lemma cos_le_one [simp]: "cos x ≤ 1"
  for x :: real
  using abs_cos_le_one [of x] by (simp add: abs_le_iff)

lemma cos_diff: "cos (x - y) = cos x * cos y + sin x * sin y"
  for x :: "'a::{real_normed_field,banach}"
  using cos_add [of x "- y"] by simp

lemma cos_double: "cos(2*x) = (cos x)2 - (sin x)2"
  for x :: "'a::{real_normed_field,banach}"
  using cos_add [where x=x and y=x] by (simp add: power2_eq_square)

lemma sin_cos_le1: "¦sin x * sin y + cos x * cos y¦ ≤ 1"
  for x :: real
  using cos_diff [of x y] by (metis abs_cos_le_one add.commute)

lemma DERIV_fun_pow: "DERIV g x :> m ⟹ DERIV (λx. (g x) ^ n) x :> real n * (g x) ^ (n - 1) * m"
  by (auto intro!: derivative_eq_intros simp:)

lemma DERIV_fun_exp: "DERIV g x :> m ⟹ DERIV (λx. exp (g x)) x :> exp (g x) * m"
  by (auto intro!: derivative_intros)


subsection ‹The Constant Pi›

definition pi :: real
  where "pi = 2 * (THE x. 0 ≤ x ∧ x ≤ 2 ∧ cos x = 0)"

text ‹Show that there's a least positive @{term x} with @{term "cos x = 0"};
   hence define pi.›

lemma sin_paired: "(λn. (- 1) ^ n / (fact (2 * n + 1)) * x ^ (2 * n + 1)) sums  sin x"
  for x :: real
proof -
  have "(λn. ∑k = n*2..<n * 2 + 2. sin_coeff k * x ^ k) sums sin x"
    by (rule sums_group) (use sin_converges [of x, unfolded scaleR_conv_of_real] in auto)
  then show ?thesis
    by (simp add: sin_coeff_def ac_simps)
qed

lemma sin_gt_zero_02:
  fixes x :: real
  assumes "0 < x" and "x < 2"
  shows "0 < sin x"
proof -
  let ?f = "λn::nat. ∑k = n*2..<n*2+2. (- 1) ^ k / (fact (2*k+1)) * x^(2*k+1)"
  have pos: "∀n. 0 < ?f n"
  proof
    fix n :: nat
    let ?k2 = "real (Suc (Suc (4 * n)))"
    let ?k3 = "real (Suc (Suc (Suc (4 * n))))"
    have "x * x < ?k2 * ?k3"
      using assms by (intro mult_strict_mono', simp_all)
    then have "x * x * x * x ^ (n * 4) < ?k2 * ?k3 * x * x ^ (n * 4)"
      by (intro mult_strict_right_mono zero_less_power ‹0 < x›)
    then show "0 < ?f n"
      by (simp add: divide_simps mult_ac del: mult_Suc)
qed
  have sums: "?f sums sin x"
    by (rule sin_paired [THEN sums_group]) simp
  show "0 < sin x"
    unfolding sums_unique [OF sums]
    using sums_summable [OF sums] pos
    by (rule suminf_pos)
qed

lemma cos_double_less_one: "0 < x ⟹ x < 2 ⟹ cos (2 * x) < 1"
  for x :: real
  using sin_gt_zero_02 [where x = x] by (auto simp: cos_squared_eq cos_double)

lemma cos_paired: "(λn. (- 1) ^ n / (fact (2 * n)) * x ^ (2 * n)) sums cos x"
  for x :: real
proof -
  have "(λn. ∑k = n * 2..<n * 2 + 2. cos_coeff k * x ^ k) sums cos x"
    by (rule sums_group) (use cos_converges [of x, unfolded scaleR_conv_of_real] in auto)
  then show ?thesis
    by (simp add: cos_coeff_def ac_simps)
qed

lemma sum_pos_lt_pair:
  fixes f :: "nat ⇒ real"
  assumes f: "summable f" and fplus: "⋀d. 0 < f (k + (Suc(Suc 0) * d)) + f (k + ((Suc (Suc 0) * d) + 1))"
  shows "sum f {..<k} < suminf f"
proof -
  have "(λn. ∑n = n * Suc (Suc 0)..<n * Suc (Suc 0) +  Suc (Suc 0). f (n + k)) 
             sums (∑n. f (n + k))"
  proof (rule sums_group)
    show "(λn. f (n + k)) sums (∑n. f (n + k))"
      by (simp add: f summable_iff_shift summable_sums)
  qed auto
  with fplus have "0 < (∑n. f (n + k))"
    apply (simp add: add.commute)
    apply (metis (no_types, lifting) suminf_pos summable_def sums_unique)
    done
  then show ?thesis
    by (simp add: f suminf_minus_initial_segment)
qed

lemma cos_two_less_zero [simp]: "cos 2 < (0::real)"
proof -
  note fact_Suc [simp del]
  from sums_minus [OF cos_paired]
  have *: "(λn. - ((- 1) ^ n * 2 ^ (2 * n) / fact (2 * n))) sums - cos (2::real)"
    by simp
  then have sm: "summable (λn. - ((- 1::real) ^ n * 2 ^ (2 * n) / (fact (2 * n))))"
    by (rule sums_summable)
  have "0 < (∑n<Suc (Suc (Suc 0)). - ((- 1::real) ^ n * 2 ^ (2 * n) / (fact (2 * n))))"
    by (simp add: fact_num_eq_if power_eq_if)
  moreover have "(∑n<Suc (Suc (Suc 0)). - ((- 1::real) ^ n  * 2 ^ (2 * n) / (fact (2 * n)))) <
    (∑n. - ((- 1) ^ n * 2 ^ (2 * n) / (fact (2 * n))))"
  proof -
    {
      fix d
      let ?six4d = "Suc (Suc (Suc (Suc (Suc (Suc (4 * d))))))"
      have "(4::real) * (fact (?six4d)) < (Suc (Suc (?six4d)) * fact (Suc (?six4d)))"
        unfolding of_nat_mult by (rule mult_strict_mono) (simp_all add: fact_less_mono)
      then have "(4::real) * (fact (?six4d)) < (fact (Suc (Suc (?six4d))))"
        by (simp only: fact_Suc [of "Suc (?six4d)"] of_nat_mult of_nat_fact)
      then have "(4::real) * inverse (fact (Suc (Suc (?six4d)))) < inverse (fact (?six4d))"
        by (simp add: inverse_eq_divide less_divide_eq)
    }
    then show ?thesis
      by (force intro!: sum_pos_lt_pair [OF sm] simp add: divide_inverse algebra_simps)
  qed
  ultimately have "0 < (∑n. - ((- 1::real) ^ n * 2 ^ (2 * n) / (fact (2 * n))))"
    by (rule order_less_trans)
  moreover from * have "- cos 2 = (∑n. - ((- 1::real) ^ n * 2 ^ (2 * n) / (fact (2 * n))))"
    by (rule sums_unique)
  ultimately have "(0::real) < - cos 2" by simp
  then show ?thesis by simp
qed

lemmas cos_two_neq_zero [simp] = cos_two_less_zero [THEN less_imp_neq]
lemmas cos_two_le_zero [simp] = cos_two_less_zero [THEN order_less_imp_le]

lemma cos_is_zero: "∃!x::real. 0 ≤ x ∧ x ≤ 2 ∧ cos x = 0"
proof (rule ex_ex1I)
  show "∃x::real. 0 ≤ x ∧ x ≤ 2 ∧ cos x = 0"
    by (rule IVT2) simp_all
next
  fix a b :: real
  assume ab: "0 ≤ a ∧ a ≤ 2 ∧ cos a = 0" "0 ≤ b ∧ b ≤ 2 ∧ cos b = 0"
  have cosd: "⋀x::real. cos differentiable (at x)"
    unfolding real_differentiable_def by (auto intro: DERIV_cos)
  show "a = b"
  proof (cases a b rule: linorder_cases)
    case less
    then obtain z where "a < z" "z < b" "(cos has_real_derivative 0) (at z)"
      using Rolle by (metis cosd isCont_cos ab)
    then have "sin z = 0"
      using DERIV_cos DERIV_unique neg_equal_0_iff_equal by blast
    then show ?thesis
      by (metis ‹a < z› ‹z < b› ab order_less_le_trans less_le sin_gt_zero_02)
  next
    case greater
    then obtain z where "b < z" "z < a" "(cos has_real_derivative 0) (at z)"
      using Rolle by (metis cosd isCont_cos ab)
    then have "sin z = 0"
      using DERIV_cos DERIV_unique neg_equal_0_iff_equal by blast
    then show ?thesis
      by (metis ‹b < z› ‹z < a› ab order_less_le_trans less_le sin_gt_zero_02)
  qed auto
qed

lemma pi_half: "pi/2 = (THE x. 0 ≤ x ∧ x ≤ 2 ∧ cos x = 0)"
  by (simp add: pi_def)

lemma cos_pi_half [simp]: "cos (pi/2) = 0"
  by (simp add: pi_half cos_is_zero [THEN theI'])

lemma cos_of_real_pi_half [simp]: "cos ((of_real pi/2) :: 'a) = 0"
  if "SORT_CONSTRAINT('a::{real_field,banach,real_normed_algebra_1})"
  by (metis cos_pi_half cos_of_real eq_numeral_simps(4)
      nonzero_of_real_divide of_real_0 of_real_numeral)

lemma pi_half_gt_zero [simp]: "0 < pi/2"
proof -
  have "0 ≤ pi/2"
    by (simp add: pi_half cos_is_zero [THEN theI'])
  then show ?thesis
    by (metis cos_pi_half cos_zero less_eq_real_def one_neq_zero)
qed

lemmas pi_half_neq_zero [simp] = pi_half_gt_zero [THEN less_imp_neq, symmetric]
lemmas pi_half_ge_zero [simp] = pi_half_gt_zero [THEN order_less_imp_le]

lemma pi_half_less_two [simp]: "pi/2 < 2"
proof -
  have "pi/2 ≤ 2"
    by (simp add: pi_half cos_is_zero [THEN theI'])
  then show ?thesis
    by (metis cos_pi_half cos_two_neq_zero le_less)
qed

lemmas pi_half_neq_two [simp] = pi_half_less_two [THEN less_imp_neq]
lemmas pi_half_le_two [simp] =  pi_half_less_two [THEN order_less_imp_le]

lemma pi_gt_zero [simp]: "0 < pi"
  using pi_half_gt_zero by simp

lemma pi_ge_zero [simp]: "0 ≤ pi"
  by (rule pi_gt_zero [THEN order_less_imp_le])

lemma pi_neq_zero [simp]: "pi ≠ 0"
  by (rule pi_gt_zero [THEN less_imp_neq, symmetric])

lemma pi_not_less_zero [simp]: "¬ pi < 0"
  by (simp add: linorder_not_less)

lemma minus_pi_half_less_zero: "-(pi/2) < 0"
  by simp

lemma m2pi_less_pi: "- (2*pi) < pi"
  by simp

lemma sin_pi_half [simp]: "sin(pi/2) = 1"
  using sin_cos_squared_add2 [where x = "pi/2"]
  using sin_gt_zero_02 [OF pi_half_gt_zero pi_half_less_two]
  by (simp add: power2_eq_1_iff)

lemma sin_of_real_pi_half [simp]: "sin ((of_real pi/2) :: 'a) = 1"
  if "SORT_CONSTRAINT('a::{real_field,banach,real_normed_algebra_1})"
  using sin_pi_half
  by (metis sin_pi_half eq_numeral_simps(4) nonzero_of_real_divide of_real_1 of_real_numeral sin_of_real)

lemma sin_cos_eq: "sin x = cos (of_real pi/2 - x)"
  for x :: "'a::{real_normed_field,banach}"
  by (simp add: cos_diff)

lemma minus_sin_cos_eq: "- sin x = cos (x + of_real pi/2)"
  for x :: "'a::{real_normed_field,banach}"
  by (simp add: cos_add nonzero_of_real_divide)

lemma cos_sin_eq: "cos x = sin (of_real pi/2 - x)"
  for x :: "'a::{real_normed_field,banach}"
  using sin_cos_eq [of "of_real pi/2 - x"] by simp

lemma sin_add: "sin (x + y) = sin x * cos y + cos x * sin y"
  for x :: "'a::{real_normed_field,banach}"
  using cos_add [of "of_real pi/2 - x" "-y"]
  by (simp add: cos_sin_eq) (simp add: sin_cos_eq)

lemma sin_diff: "sin (x - y) = sin x * cos y - cos x * sin y"
  for x :: "'a::{real_normed_field,banach}"
  using sin_add [of x "- y"] by simp

lemma sin_double: "sin(2 * x) = 2 * sin x * cos x"
  for x :: "'a::{real_normed_field,banach}"
  using sin_add [where x=x and y=x] by simp

lemma cos_of_real_pi [simp]: "cos (of_real pi) = -1"
  using cos_add [where x = "pi/2" and y = "pi/2"]
  by (simp add: cos_of_real)

lemma sin_of_real_pi [simp]: "sin (of_real pi) = 0"
  using sin_add [where x = "pi/2" and y = "pi/2"]
  by (simp add: sin_of_real)

lemma cos_pi [simp]: "cos pi = -1"
  using cos_add [where x = "pi/2" and y = "pi/2"] by simp

lemma sin_pi [simp]: "sin pi = 0"
  using sin_add [where x = "pi/2" and y = "pi/2"] by simp

lemma sin_periodic_pi [simp]: "sin (x + pi) = - sin x"
  by (simp add: sin_add)

lemma sin_periodic_pi2 [simp]: "sin (pi + x) = - sin x"
  by (simp add: sin_add)

lemma cos_periodic_pi [simp]: "cos (x + pi) = - cos x"
  by (simp add: cos_add)

lemma cos_periodic_pi2 [simp]: "cos (pi + x) = - cos x"
  by (simp add: cos_add)

lemma sin_periodic [simp]: "sin (x + 2 * pi) = sin x"
  by (simp add: sin_add sin_double cos_double)

lemma cos_periodic [simp]: "cos (x + 2 * pi) = cos x"
  by (simp add: cos_add sin_double cos_double)

lemma cos_npi [simp]: "cos (real n * pi) = (- 1) ^ n"
  by (induct n) (auto simp: distrib_right)

lemma cos_npi2 [simp]: "cos (pi * real n) = (- 1) ^ n"
  by (metis cos_npi mult.commute)

lemma sin_npi [simp]: "sin (real n * pi) = 0"
  for n :: nat
  by (induct n) (auto simp: distrib_right)

lemma sin_npi2 [simp]: "sin (pi * real n) = 0"
  for n :: nat
  by (simp add: mult.commute [of pi])

lemma cos_two_pi [simp]: "cos (2 * pi) = 1"
  by (simp add: cos_double)

lemma sin_two_pi [simp]: "sin (2 * pi) = 0"
  by (simp add: sin_double)

lemma sin_times_sin: "sin w * sin z = (cos (w - z) - cos (w + z)) / 2"
  for w :: "'a::{real_normed_field,banach}"
  by (simp add: cos_diff cos_add)

lemma sin_times_cos: "sin w * cos z = (sin (w + z) + sin (w - z)) / 2"
  for w :: "'a::{real_normed_field,banach}"
  by (simp add: sin_diff sin_add)

lemma cos_times_sin: "cos w * sin z = (sin (w + z) - sin (w - z)) / 2"
  for w :: "'a::{real_normed_field,banach}"
  by (simp add: sin_diff sin_add)

lemma cos_times_cos: "cos w * cos z = (cos (w - z) + cos (w + z)) / 2"
  for w :: "'a::{real_normed_field,banach}"
  by (simp add: cos_diff cos_add)

lemma sin_plus_sin: "sin w + sin z = 2 * sin ((w + z) / 2) * cos ((w - z) / 2)"
  for w :: "'a::{real_normed_field,banach}" 
  apply (simp add: mult.assoc sin_times_cos)
  apply (simp add: field_simps)
  done

lemma sin_diff_sin: "sin w - sin z = 2 * sin ((w - z) / 2) * cos ((w + z) / 2)"
  for w :: "'a::{real_normed_field,banach}"
  apply (simp add: mult.assoc sin_times_cos)
  apply (simp add: field_simps)
  done

lemma cos_plus_cos: "cos w + cos z = 2 * cos ((w + z) / 2) * cos ((w - z) / 2)"
  for w :: "'a::{real_normed_field,banach,field}"
  apply (simp add: mult.assoc cos_times_cos)
  apply (simp add: field_simps)
  done

lemma cos_diff_cos: "cos w - cos z = 2 * sin ((w + z) / 2) * sin ((z - w) / 2)"
  for w :: "'a::{real_normed_field,banach,field}"
  apply (simp add: mult.assoc sin_times_sin)
  apply (simp add: field_simps)
  done

lemma cos_double_cos: "cos (2 * z) = 2 * cos z ^ 2 - 1"
  for z :: "'a::{real_normed_field,banach}"
  by (simp add: cos_double sin_squared_eq)

lemma cos_double_sin: "cos (2 * z) = 1 - 2 * sin z ^ 2"
  for z :: "'a::{real_normed_field,banach}"
  by (simp add: cos_double sin_squared_eq)

lemma sin_pi_minus [simp]: "sin (pi - x) = sin x"
  by (metis sin_minus sin_periodic_pi minus_minus uminus_add_conv_diff)

lemma cos_pi_minus [simp]: "cos (pi - x) = - (cos x)"
  by (metis cos_minus cos_periodic_pi uminus_add_conv_diff)

lemma sin_minus_pi [simp]: "sin (x - pi) = - (sin x)"
  by (simp add: sin_diff)

lemma cos_minus_pi [simp]: "cos (x - pi) = - (cos x)"
  by (simp add: cos_diff)

lemma sin_2pi_minus [simp]: "sin (2 * pi - x) = - (sin x)"
  by (metis sin_periodic_pi2 add_diff_eq mult_2 sin_pi_minus)

lemma cos_2pi_minus [simp]: "cos (2 * pi - x) = cos x"
  by (metis (no_types, hide_lams) cos_add cos_minus cos_two_pi sin_minus sin_two_pi
      diff_0_right minus_diff_eq mult_1 mult_zero_left uminus_add_conv_diff)

lemma sin_gt_zero2: "0 < x ⟹ x < pi/2 ⟹ 0 < sin x"
  by (metis sin_gt_zero_02 order_less_trans pi_half_less_two)

lemma sin_less_zero:
  assumes "- pi/2 < x" and "x < 0"
  shows "sin x < 0"
proof -
  have "0 < sin (- x)"
    using assms by (simp only: sin_gt_zero2)
  then show ?thesis by simp
qed

lemma pi_less_4: "pi < 4"
  using pi_half_less_two by auto

lemma cos_gt_zero: "0 < x ⟹ x < pi/2 ⟹ 0 < cos x"
  by (simp add: cos_sin_eq sin_gt_zero2)

lemma cos_gt_zero_pi: "-(pi/2) < x ⟹ x < pi/2 ⟹ 0 < cos x"
  using cos_gt_zero [of x] cos_gt_zero [of "-x"]
  by (cases rule: linorder_cases [of x 0]) auto

lemma cos_ge_zero: "-(pi/2) ≤ x ⟹ x ≤ pi/2 ⟹ 0 ≤ cos x"
  by (auto simp: order_le_less cos_gt_zero_pi)
    (metis cos_pi_half eq_divide_eq eq_numeral_simps(4))

lemma sin_gt_zero: "0 < x ⟹ x < pi ⟹ 0 < sin x"
  by (simp add: sin_cos_eq cos_gt_zero_pi)

lemma sin_lt_zero: "pi < x ⟹ x < 2 * pi ⟹ sin x < 0"
  using sin_gt_zero [of "x - pi"]
  by (simp add: sin_diff)

lemma pi_ge_two: "2 ≤ pi"
proof (rule ccontr)
  assume "¬ ?thesis"
  then have "pi < 2" by auto
  have "∃y > pi. y < 2 ∧ y < 2 * pi"
  proof (cases "2 < 2 * pi")
    case True
    with dense[OF ‹pi < 2›] show ?thesis by auto
  next
    case False
    have "pi < 2 * pi" by auto
    from dense[OF this] and False show ?thesis by auto
  qed
  then obtain y where "pi < y" and "y < 2" and "y < 2 * pi"
    by blast
  then have "0 < sin y"
    using sin_gt_zero_02 by auto
  moreover have "sin y < 0"
    using sin_gt_zero[of "y - pi"] ‹pi < y› and ‹y < 2 * pi› sin_periodic_pi[of "y - pi"]
    by auto
  ultimately show False by auto
qed

lemma sin_ge_zero: "0 ≤ x ⟹ x ≤ pi ⟹ 0 ≤ sin x"
  by (auto simp: order_le_less sin_gt_zero)

lemma sin_le_zero: "pi ≤ x ⟹ x < 2 * pi ⟹ sin x ≤ 0"
  using sin_ge_zero [of "x - pi"] by (simp add: sin_diff)

lemma sin_pi_divide_n_ge_0 [simp]:
  assumes "n ≠ 0"
  shows "0 ≤ sin (pi / real n)"
  by (rule sin_ge_zero) (use assms in ‹simp_all add: divide_simps›)

lemma sin_pi_divide_n_gt_0:
  assumes "2 ≤ n"
  shows "0 < sin (pi / real n)"
  by (rule sin_gt_zero) (use assms in ‹simp_all add: divide_simps›)

text‹Proof resembles that of @{text cos_is_zero} but with @{term pi} for the upper bound›
lemma cos_total:
  assumes y: "-1 ≤ y" "y ≤ 1"
  shows "∃!x. 0 ≤ x ∧ x ≤ pi ∧ cos x = y"
proof (rule ex_ex1I)
  show "∃x::real. 0 ≤ x ∧ x ≤ pi ∧ cos x = y"
    by (rule IVT2) (simp_all add: y)
next
  fix a b :: real
  assume ab: "0 ≤ a ∧ a ≤ pi ∧ cos a = y" "0 ≤ b ∧ b ≤ pi ∧ cos b = y"
  have cosd: "⋀x::real. cos differentiable (at x)"
    unfolding real_differentiable_def by (auto intro: DERIV_cos)
  show "a = b"
  proof (cases a b rule: linorder_cases)
    case less
    then obtain z where "a < z" "z < b" "(cos has_real_derivative 0) (at z)"
      using Rolle by (metis cosd isCont_cos ab)
    then have "sin z = 0"
      using DERIV_cos DERIV_unique neg_equal_0_iff_equal by blast
    then show ?thesis
      by (metis ‹a < z› ‹z < b› ab order_less_le_trans less_le sin_gt_zero)
  next
    case greater
    then obtain z where "b < z" "z < a" "(cos has_real_derivative 0) (at z)"
      using Rolle by (metis cosd isCont_cos ab)
    then have "sin z = 0"
      using DERIV_cos DERIV_unique neg_equal_0_iff_equal by blast
    then show ?thesis
      by (metis ‹b < z› ‹z < a› ab order_less_le_trans less_le sin_gt_zero)
  qed auto
qed

lemma sin_total:
  assumes y: "-1 ≤ y" "y ≤ 1"
  shows "∃!x. - (pi/2) ≤ x ∧ x ≤ pi/2 ∧ sin x = y"
proof -
  from cos_total [OF y]
  obtain x where x: "0 ≤ x" "x ≤ pi" "cos x = y"
    and uniq: "⋀x'. 0 ≤ x' ⟹ x' ≤ pi ⟹ cos x' = y ⟹ x' = x "
    by blast
  show ?thesis
    unfolding sin_cos_eq
  proof (rule ex1I [where a="pi/2 - x"])
    show "- (pi/2) ≤ z ∧ z ≤ pi/2 ∧ cos (of_real pi/2 - z) = y ⟹
          z = pi/2 - x" for z
      using uniq [of "pi/2 -z"] by auto
  qed (use x in auto)
qed