Theory HOL-Analysis.Complex_Transcendental

section ‹Complex Transcendental Functions›

text‹By John Harrison et al.  Ported from HOL Light by L C Paulson (2015)›

theory Complex_Transcendental
imports
  Complex_Analysis_Basics Summation_Tests "HOL-Library.Periodic_Fun"
begin

subsection‹Möbius transformations›

(* TODO: Figure out what to do with Möbius transformations *)
definitiontag important› "moebius a b c d  (λz. (a*z+b) / (c*z+d :: 'a :: field))"

theorem moebius_inverse:
  assumes "a * d  b * c" "c * z + d  0"
  shows   "moebius d (-b) (-c) a (moebius a b c d z) = z"
proof -
  from assms have "(-c) * moebius a b c d z + a  0" unfolding moebius_def
    by (simp add: field_simps)
  with assms show ?thesis
    unfolding moebius_def by (simp add: moebius_def divide_simps) (simp add: algebra_simps)?
qed

lemma moebius_inverse':
  assumes "a * d  b * c" "c * z - a  0"
  shows   "moebius a b c d (moebius d (-b) (-c) a z) = z"
  using assms moebius_inverse[of d a "-b" "-c" z]
  by (auto simp: algebra_simps)

lemma cmod_add_real_less:
  assumes "Im z  0" "r0"
    shows "cmod (z + r) < cmod z + ¦r¦"
proof (cases z)
  case (Complex x y)
  then have "0 < y * y"
    using assms mult_neg_neg by force
  with assms have "r * x / ¦r¦ < sqrt (x*x + y*y)"
    by (simp add: real_less_rsqrt power2_eq_square)
  then show ?thesis using assms Complex
    apply (simp add: cmod_def)
    apply (rule power2_less_imp_less, auto)
    apply (simp add: power2_eq_square field_simps)
    done
qed

lemma cmod_diff_real_less: "Im z  0  x0  cmod (z - x) < cmod z + ¦x¦"
  using cmod_add_real_less [of z "-x"]
  by simp

lemma cmod_square_less_1_plus:
  assumes "Im z = 0  ¦Re z¦ < 1"
    shows "(cmod z)2 < 1 + cmod (1 - z2)"
proof (cases "Im z = 0  Re z = 0")
  case True
  with assms abs_square_less_1 show ?thesis
    by (force simp add: Re_power2 Im_power2 cmod_def)
next
  case False
  with cmod_diff_real_less [of "1 - z2" "1"] show ?thesis
    by (simp add: norm_power Im_power2)
qed

subsectiontag unimportant›‹The Exponential Function›

lemma norm_exp_i_times [simp]: "norm (exp(𝗂 * of_real y)) = 1"
  by simp

lemma norm_exp_imaginary: "norm(exp z) = 1  Re z = 0"
  by simp

lemma field_differentiable_within_exp: "exp field_differentiable (at z within s)"
  using DERIV_exp field_differentiable_at_within field_differentiable_def by blast

lemma continuous_within_exp:
  fixes z::"'a::{real_normed_field,banach}"
  shows "continuous (at z within s) exp"
  by (simp add: continuous_at_imp_continuous_within)

lemma holomorphic_on_exp [holomorphic_intros]: "exp holomorphic_on s"
  by (simp add: field_differentiable_within_exp holomorphic_on_def)

lemma holomorphic_on_exp' [holomorphic_intros]:
  "f holomorphic_on s  (λx. exp (f x)) holomorphic_on s"
  using holomorphic_on_compose[OF _ holomorphic_on_exp] by (simp add: o_def)

lemma exp_analytic_on [analytic_intros]:
  assumes "f analytic_on A"
  shows   "(λz. exp (f z)) analytic_on A"
  by (metis analytic_on_holomorphic assms holomorphic_on_exp')

lemma
  assumes "w. w  A  exp (f w) = w"
  assumes "f holomorphic_on A" "z  A" "open A"
  shows   deriv_complex_logarithm: "deriv f z = 1 / z"
    and   has_field_derivative_complex_logarithm: "(f has_field_derivative 1 / z) (at z)"
proof -
  have [simp]: "z  0"
    using assms(1)[of z] assms(3) by auto
  have deriv [derivative_intros]: "(f has_field_derivative deriv f z) (at z)"
    using assms holomorphic_derivI by blast
  have "((λw. w) has_field_derivative 1) (at z)"
    by (intro derivative_intros)
  also have "?this  ((λw. exp (f w)) has_field_derivative 1) (at z)"
    by (smt (verit, best) assms has_field_derivative_transform_within_open)
  finally have "((λw. exp (f w)) has_field_derivative 1) (at z)" .
  moreover have "((λw. exp (f w)) has_field_derivative exp (f z) * deriv f z) (at z)"
    by (rule derivative_eq_intros refl)+
  ultimately have "exp (f z) * deriv f z = 1"
    using DERIV_unique by blast
  with assms show "deriv f z = 1 / z"
    by (simp add: field_simps)
  with deriv show "(f has_field_derivative 1 / z) (at z)"
    by simp
qed
  
subsection‹Euler and de Moivre formulas›

text‹The sine series times termi
lemma sin_i_eq: "(λn. (𝗂 * sin_coeff n) * z^n) sums (𝗂 * sin z)"
proof -
  have "(λn. 𝗂 * sin_coeff n *R z^n) sums (𝗂 * sin z)"
    using sin_converges sums_mult by blast
  then show ?thesis
    by (simp add: scaleR_conv_of_real field_simps)
qed

theorem exp_Euler: "exp(𝗂 * z) = cos(z) + 𝗂 * sin(z)"
proof -
  have "(λn. (cos_coeff n + 𝗂 * sin_coeff n) * z^n) = (λn. (𝗂 * z) ^ n /R (fact n))"
    by (force simp: cos_coeff_def sin_coeff_def scaleR_conv_of_real field_simps elim!: evenE oddE)
  also have " sums (exp (𝗂 * z))"
    by (rule exp_converges)
  finally have "(λn. (cos_coeff n + 𝗂 * sin_coeff n) * z^n) sums (exp (𝗂 * z))" .
  moreover have "(λn. (cos_coeff n + 𝗂 * sin_coeff n) * z^n) sums (cos z + 𝗂 * sin z)"
    using sums_add [OF cos_converges [of z] sin_i_eq [of z]]
    by (simp add: field_simps scaleR_conv_of_real)
  ultimately show ?thesis
    using sums_unique2 by blast
qed

corollarytag unimportant› exp_minus_Euler: "exp(-(𝗂 * z)) = cos(z) - 𝗂 * sin(z)"
  using exp_Euler [of "-z"] by simp

lemma sin_exp_eq: "sin z = (exp(𝗂 * z) - exp(-(𝗂 * z))) / (2*𝗂)"
  by (simp add: exp_Euler exp_minus_Euler)

lemma sin_exp_eq': "sin z = 𝗂 * (exp(-(𝗂 * z)) - exp(𝗂 * z)) / 2"
  by (simp add: exp_Euler exp_minus_Euler)

lemma cos_exp_eq:  "cos z = (exp(𝗂 * z) + exp(-(𝗂 * z))) / 2"
  by (simp add: exp_Euler exp_minus_Euler)

theorem Euler: "exp(z) = of_real(exp(Re z)) *
              (of_real(cos(Im z)) + 𝗂 * of_real(sin(Im z)))"
  by (simp add: Complex_eq cis.code exp_eq_polar)

lemma Re_sin: "Re(sin z) = sin(Re z) * (exp(Im z) + exp(-(Im z))) / 2"
  by (simp add: sin_exp_eq field_simps Re_divide Im_exp)

lemma Im_sin: "Im(sin z) = cos(Re z) * (exp(Im z) - exp(-(Im z))) / 2"
  by (simp add: sin_exp_eq field_simps Im_divide Re_exp)

lemma Re_cos: "Re(cos z) = cos(Re z) * (exp(Im z) + exp(-(Im z))) / 2"
  by (simp add: cos_exp_eq field_simps Re_divide Re_exp)

lemma Im_cos: "Im(cos z) = sin(Re z) * (exp(-(Im z)) - exp(Im z)) / 2"
  by (simp add: cos_exp_eq field_simps Im_divide Im_exp)

lemma Re_sin_pos: "0 < Re z  Re z < pi  Re (sin z) > 0"
  by (auto simp: Re_sin Im_sin add_pos_pos sin_gt_zero)

lemma Im_sin_nonneg: "Re z = 0  0  Im z  0  Im (sin z)"
  by (simp add: Re_sin Im_sin algebra_simps)

lemma Im_sin_nonneg2: "Re z = pi  Im z  0  0  Im (sin z)"
  by (simp add: Re_sin Im_sin algebra_simps)

subsectiontag unimportant›‹Relationships between real and complex trigonometric and hyperbolic functions›

lemma real_sin_eq [simp]: "Re(sin(of_real x)) = sin x"
  by (simp add: sin_of_real)

lemma real_cos_eq [simp]: "Re(cos(of_real x)) = cos x"
  by (simp add: cos_of_real)

lemma DeMoivre: "(cos z + 𝗂 * sin z) ^ n = cos(n * z) + 𝗂 * sin(n * z)"
  by (metis exp_Euler [symmetric] exp_of_nat_mult mult.left_commute)

lemma exp_cnj: "cnj (exp z) = exp (cnj z)"
  by (simp add: cis_cnj exp_eq_polar)

lemma cnj_sin: "cnj(sin z) = sin(cnj z)"
  by (simp add: sin_exp_eq exp_cnj field_simps)

lemma cnj_cos: "cnj(cos z) = cos(cnj z)"
  by (simp add: cos_exp_eq exp_cnj field_simps)

lemma field_differentiable_at_sin: "sin field_differentiable at z"
  using DERIV_sin field_differentiable_def by blast

lemma field_differentiable_within_sin: "sin field_differentiable (at z within S)"
  by (simp add: field_differentiable_at_sin field_differentiable_at_within)

lemma field_differentiable_at_cos: "cos field_differentiable at z"
  using DERIV_cos field_differentiable_def by blast

lemma field_differentiable_within_cos: "cos field_differentiable (at z within S)"
  by (simp add: field_differentiable_at_cos field_differentiable_at_within)

lemma holomorphic_on_sin: "sin holomorphic_on S"
  by (simp add: field_differentiable_within_sin holomorphic_on_def)

lemma holomorphic_on_cos: "cos holomorphic_on S"
  by (simp add: field_differentiable_within_cos holomorphic_on_def)

lemma holomorphic_on_sin' [holomorphic_intros]:
  assumes "f holomorphic_on A"
  shows   "(λx. sin (f x)) holomorphic_on A"
  using holomorphic_on_compose[OF assms holomorphic_on_sin] by (simp add: o_def)

lemma holomorphic_on_cos' [holomorphic_intros]:
  assumes "f holomorphic_on A"
  shows   "(λx. cos (f x)) holomorphic_on A"
  using holomorphic_on_compose[OF assms holomorphic_on_cos] by (simp add: o_def)

lemma analytic_on_sin [analytic_intros]: "f analytic_on A  (λw. sin (f w)) analytic_on A"
  and analytic_on_cos [analytic_intros]: "f analytic_on A  (λw. cos (f w)) analytic_on A"
  and analytic_on_sinh [analytic_intros]: "f analytic_on A  (λw. sinh (f w)) analytic_on A"
  and analytic_on_cosh [analytic_intros]: "f analytic_on A  (λw. cosh (f w)) analytic_on A"
  unfolding sin_exp_eq cos_exp_eq sinh_def cosh_def
  by (auto intro!: analytic_intros)

lemma analytic_on_tan [analytic_intros]:
        "f analytic_on A  (z. z  A  cos (f z)  0)  (λw. tan (f w)) analytic_on A"
  and analytic_on_cot [analytic_intros]:
        "f analytic_on A  (z. z  A  sin (f z)  0)  (λw. cot (f w)) analytic_on A"
  and analytic_on_tanh [analytic_intros]:
        "f analytic_on A  (z. z  A  cosh (f z)  0)  (λw. tanh (f w)) analytic_on A"
  unfolding tan_def cot_def tanh_def by (auto intro!: analytic_intros)

subsectiontag unimportant›‹More on the Polar Representation of Complex Numbers›

lemma exp_Complex: "exp(Complex r t) = of_real(exp r) * Complex (cos t) (sin t)"
  using Complex_eq Euler complex.sel by presburger

lemma exp_eq_1: "exp z = 1  Re(z) = 0  (n::int. Im(z) = of_int (2 * n) * pi)"
                 (is "?lhs = ?rhs")
proof
  assume "exp z = 1"
  then have "Re z = 0"
    by (metis exp_eq_one_iff norm_exp_eq_Re norm_one)
  with ?lhs show ?rhs
    by (metis Re_exp cos_one_2pi_int exp_zero mult.commute mult_1 of_int_mult of_int_numeral one_complex.simps(1))
next
  assume ?rhs then show ?lhs
    using Im_exp Re_exp complex_eq_iff
    by (simp add: cos_one_2pi_int cos_one_sin_zero mult.commute)
qed

lemma exp_eq: "exp w = exp z  (n::int. w = z + (of_int (2 * n) * pi) * 𝗂)"
                (is "?lhs = ?rhs")
proof -
  have "exp w = exp z  exp (w-z) = 1"
    by (simp add: exp_diff)
  also have "  (Re w = Re z  (n::int. Im w - Im z = of_int (2 * n) * pi))"
    by (simp add: exp_eq_1)
  also have "  ?rhs"
    by (auto simp: algebra_simps intro!: complex_eqI)
  finally show ?thesis .
qed

lemma exp_complex_eqI: "¦Im w - Im z¦ < 2*pi  exp w = exp z  w = z"
  by (auto simp: exp_eq abs_mult)

lemma exp_integer_2pi:
  assumes "n  "
  shows "exp((2 * n * pi) * 𝗂) = 1"
  by (metis assms cis_conv_exp cis_multiple_2pi mult.assoc mult.commute)

lemma exp_plus_2pin [simp]: "exp (z + 𝗂 * (of_int n * (of_real pi * 2))) = exp z"
  by (simp add: exp_eq)

lemma exp_integer_2pi_plus1:
  assumes "n  "
  shows "exp(((2 * n + 1) * pi) * 𝗂) = - 1"
  using exp_integer_2pi [OF assms]
  by (metis cis_conv_exp cis_mult cis_pi distrib_left mult.commute mult.right_neutral)

lemma inj_on_exp_pi:
  fixes z::complex shows "inj_on exp (ball z pi)"
proof (clarsimp simp: inj_on_def exp_eq)
  fix y n
  assume "dist z (y + 2 * of_int n * of_real pi * 𝗂) < pi"
         "dist z y < pi"
  then have "dist y (y + 2 * of_int n * of_real pi * 𝗂) < pi+pi"
    using dist_commute_lessI dist_triangle_less_add by blast
  then have "norm (2 * of_int n * of_real pi * 𝗂) < 2*pi"
    by (simp add: dist_norm)
  then show "n = 0"
    by (auto simp: norm_mult)
qed

lemma cmod_add_squared:
  fixes r1 r2::real
  shows "(cmod (r1 * exp (𝗂 * θ1) + r2 * exp (𝗂 * θ2)))2 = r12 + r22 + 2 * r1 * r2 * cos (θ1 - θ2)" (is "(cmod (?z1 + ?z2))2 = ?rhs")
proof -
  have "(cmod (?z1 + ?z2))2 = (?z1 + ?z2) * cnj (?z1 + ?z2)"
    by (rule complex_norm_square)
  also have " = (?z1 * cnj ?z1 + ?z2 * cnj ?z2) + (?z1 * cnj ?z2 + cnj ?z1 * ?z2)"
    by (simp add: algebra_simps)
  also have " = (norm ?z1)2 + (norm ?z2)2 + 2 * Re (?z1 * cnj ?z2)"
    unfolding complex_norm_square [symmetric] cnj_add_mult_eq_Re by simp
  also have " = ?rhs"
    by (simp add: norm_mult) (simp add: exp_Euler complex_is_Real_iff [THEN iffD1] cos_diff algebra_simps)
  finally show ?thesis
    using of_real_eq_iff by blast
qed

lemma cmod_diff_squared:
  fixes r1 r2::real
  shows "(cmod (r1 * exp (𝗂 * θ1) - r2 * exp (𝗂 * θ2)))2 = r12 + r22 - 2*r1*r2*cos (θ1 - θ2)" 
  using cmod_add_squared [of r1 _ "-r2"] by simp

lemma polar_convergence:
  fixes R::real
  assumes "j. r j > 0" "R > 0"
  shows "((λj. r j * exp (𝗂 * θ j))  (R * exp (𝗂 * Θ))) 
         (r  R)  (k. (λj. θ j - of_int (k j) * (2 * pi))  Θ)"    (is "(?z  ?Z) = ?rhs")
proof
  assume L: "?z  ?Z"
  have rR: "r  R"
    using tendsto_norm [OF L] assms by (auto simp: norm_mult abs_of_pos)
  moreover obtain k where "(λj. θ j - of_int (k j) * (2 * pi))  Θ"
  proof -
    have "cos (θ j - Θ) = ((r j)2 + R2 - (norm(?z j - ?Z))2) / (2 * R * r j)" for j
      using assms by (auto simp: cmod_diff_squared less_le)
    moreover have "(λj. ((r j)2 + R2 - (norm(?z j - ?Z))2) / (2 * R * r j))  ((R2 + R2 - (norm(?Z - ?Z))2) / (2 * R * R))"
      by (intro L rR tendsto_intros) (use R > 0 in force)
    moreover have "((R2 + R2 - (norm(?Z - ?Z))2) / (2 * R * R)) = 1"
      using R > 0 by (simp add: power2_eq_square field_split_simps)
    ultimately have "(λj. cos (θ j - Θ))  1"
      by auto
    then show ?thesis
      using that cos_diff_limit_1 by blast
  qed
  ultimately show ?rhs
    by metis
next
  assume R: ?rhs
  show "?z  ?Z"
  proof (rule tendsto_mult)
    show "(λx. complex_of_real (r x))  of_real R"
      using R by (auto simp: tendsto_of_real_iff)
    obtain k where "(λj. θ j - of_int (k j) * (2 * pi))  Θ"
      using R by metis
    then have "(λj. complex_of_real (θ j - of_int (k j) * (2 * pi)))  of_real Θ"
      using tendsto_of_real_iff by force
    then have "(λj.  exp (𝗂 * of_real (θ j - of_int (k j) * (2 * pi))))  exp (𝗂 * Θ)"
      using tendsto_mult [OF tendsto_const] isCont_exp isCont_tendsto_compose by blast
    moreover have "exp (𝗂 * of_real (θ j - of_int (k j) * (2 * pi))) = exp (𝗂 * θ j)" for j
      unfolding exp_eq
      by (rule_tac x="- k j" in exI) (auto simp: algebra_simps)
    ultimately show "(λj. exp (𝗂 * θ j))  exp (𝗂 * Θ)"
      by auto
  qed
qed

lemma exp_i_ne_1:
  assumes "0 < x" "x < 2*pi"
  shows "exp(𝗂 * of_real x)  1"
  by (smt (verit) Im_i_times Re_complex_of_real assms exp_complex_eqI exp_zero zero_complex.sel(2))

lemma sin_eq_0:
  fixes z::complex
  shows "sin z = 0  (n::int. z = of_real(n * pi))"
  by (simp add: sin_exp_eq exp_eq)

lemma cos_eq_0:
  fixes z::complex
  shows "cos z = 0  (n::int. z = complex_of_real(n * pi) + of_real pi/2)"
  using sin_eq_0 [of "z - of_real pi/2"]
  by (simp add: sin_diff algebra_simps)

lemma cos_eq_1:
  fixes z::complex
  shows "cos z = 1  (n::int. z = complex_of_real(2 * n * pi))"
  by (metis Re_complex_of_real cos_of_real cos_one_2pi_int cos_one_sin_zero mult.commute of_real_1 sin_eq_0)

lemma csin_eq_1:
  fixes z::complex
  shows "sin z = 1  (n::int. z = of_real(2 * n * pi) + of_real pi/2)"
  using cos_eq_1 [of "z - of_real pi/2"]
  by (simp add: cos_diff algebra_simps)

lemma csin_eq_minus1:
  fixes z::complex
  shows "sin z = -1  (n::int. z = complex_of_real(2 * n * pi) + 3/2*pi)"
        (is "_ = ?rhs")
proof -
  have "sin z = -1  sin (-z) = 1"
    by (simp add: equation_minus_iff)
  also have "  (n::int. z = - of_real(2 * n * pi) - of_real pi/2)"
    by (metis (mono_tags, lifting) add_uminus_conv_diff csin_eq_1 equation_minus_iff minus_add_distrib)
  also have " = ?rhs"
    apply safe
    apply (rule_tac [2] x="-(x+1)" in exI)
    apply (rule_tac x="-(x+1)" in exI)
    apply (simp_all add: algebra_simps)
    done
  finally show ?thesis .
qed

lemma ccos_eq_minus1:
  fixes z::complex
  shows "cos z = -1  (n::int. z = complex_of_real(2 * n * pi) + pi)"
  using csin_eq_1 [of "z - of_real pi/2"]
  by (simp add: sin_diff algebra_simps equation_minus_iff)

lemma sin_eq_1: "sin x = 1  (n::int. x = (2 * n + 1 / 2) * pi)"
                (is "_ = ?rhs")
proof -
  have "sin x = 1  sin (complex_of_real x) = 1"
    by (metis of_real_1 one_complex.simps(1) real_sin_eq sin_of_real)
  also have "  (n::int. x = of_real(2 * n * pi) + of_real pi/2)"
    by (metis csin_eq_1 Re_complex_of_real id_apply of_real_add of_real_divide of_real_eq_id of_real_numeral)
  also have " = ?rhs"
    by (auto simp: algebra_simps)
  finally show ?thesis .
qed

lemma sin_eq_minus1: "sin x = -1  (n::int. x = (2*n + 3/2) * pi)"  (is "_ = ?rhs")
proof -
  have "sin x = -1  sin (complex_of_real x) = -1"
    by (metis Re_complex_of_real of_real_def scaleR_minus1_left sin_of_real)
  also have "  (n::int. x = of_real(2 * n * pi) + 3/2*pi)"
    by (metis Re_complex_of_real csin_eq_minus1 id_apply of_real_add of_real_eq_id)
  also have " = ?rhs"
    by (auto simp: algebra_simps)
  finally show ?thesis .
qed

lemma cos_eq_minus1: "cos x = -1  (n::int. x = (2*n + 1) * pi)"
                      (is "_ = ?rhs")
proof -
  have "cos x = -1  cos (complex_of_real x) = -1"
    by (metis Re_complex_of_real of_real_def scaleR_minus1_left cos_of_real)
  also have "  (n::int. x = of_real(2 * n * pi) + pi)"
    by (metis ccos_eq_minus1 id_apply of_real_add of_real_eq_id of_real_eq_iff)
  also have " = ?rhs"
    by (auto simp: algebra_simps)
  finally show ?thesis .
qed

lemma cos_gt_neg1:
  assumes "(t::real)  {-pi<..<pi}"
  shows   "cos t > -1"
  using assms
  by simp (metis cos_minus cos_monotone_0_pi cos_monotone_minus_pi_0 cos_pi linorder_le_cases)

lemma dist_exp_i_1: "norm(exp(𝗂 * of_real t) - 1) = 2 * ¦sin(t / 2)¦"
proof -
  have "sqrt (2 - cos t * 2) = 2 * ¦sin (t / 2)¦"
    using cos_double_sin [of "t/2"] by (simp add: real_sqrt_mult)
  then show ?thesis
    by (simp add: exp_Euler cmod_def power2_diff sin_of_real cos_of_real algebra_simps)
qed

lemma sin_cx_2pi [simp]: "z = of_int m; even m  sin (z * complex_of_real pi) = 0"
  by (simp add: sin_eq_0)

lemma cos_cx_2pi [simp]: "z = of_int m; even m  cos (z * complex_of_real pi) = 1"
  using cos_eq_1 by auto

lemma complex_sin_eq:
  fixes w :: complex
  shows "sin w = sin z  (n  . w = z + of_real(2*n*pi)  w = -z + of_real((2*n + 1)*pi))"
        (is "?lhs = ?rhs")
proof
  assume ?lhs
  then consider "sin ((w - z) / 2) = 0" | "cos ((w + z) / 2) = 0"
    by (metis divide_eq_0_iff nonzero_eq_divide_eq right_minus_eq sin_diff_sin zero_neq_numeral)
  then show ?rhs
  proof cases
    case 1
    then show ?thesis
      by (simp add: sin_eq_0 algebra_simps) (metis Ints_of_int of_real_of_int_eq)
  next
    case 2
    then show ?thesis
      by (simp add: cos_eq_0 algebra_simps) (metis Ints_of_int of_real_of_int_eq)
  qed
next
  assume ?rhs
  then consider n::int where "w = z + of_real (2 * of_int n * pi)"
              | n::int where  " w = -z + of_real ((2 * of_int n + 1) * pi)"
    using Ints_cases by blast
  then show ?lhs
  proof cases
    case 1
    then show ?thesis
      using Periodic_Fun.sin.plus_of_int [of z n]
      by (auto simp: algebra_simps)
  next
    case 2
    then show ?thesis
      using Periodic_Fun.sin.plus_of_int [of "-z" "n"]
      apply (simp add: algebra_simps)
      by (metis add.commute add.inverse_inverse add_diff_cancel_left diff_add_cancel sin_plus_pi)
  qed
qed

lemma complex_cos_eq:
  fixes w :: complex
  shows "cos w = cos z  (n  . w = z + of_real(2*n*pi)  w = -z + of_real(2*n*pi))"
        (is "?lhs = ?rhs")
proof 
  assume ?lhs
  then consider "sin ((w + z) / 2) = 0" | "sin ((z - w) / 2) = 0"
    by (metis mult_eq_0_iff cos_diff_cos right_minus_eq zero_neq_numeral)
  then show ?rhs
  proof cases
    case 1
    then obtain n where "w + z = of_int n * (complex_of_real pi * 2)"
      by (auto simp: sin_eq_0 algebra_simps)
    then have "w = -z + of_real(2 * of_int n * pi)"
      by (auto simp: algebra_simps)
    then show ?thesis
      using Ints_of_int by blast
  next
    case 2
    then obtain n where "z = w + of_int n * (complex_of_real pi * 2)"
      by (auto simp: sin_eq_0 algebra_simps)
    then have "w = z + complex_of_real (2 * of_int(-n) * pi)"
      by (auto simp: algebra_simps)
    then show ?thesis
      using Ints_of_int by blast
  qed
next
  assume ?rhs
  then obtain n::int where w: "w = z + of_real (2* of_int n*pi) 
                               w = -z + of_real(2*n*pi)"
    using Ints_cases  by (metis of_int_mult of_int_numeral)
  then show ?lhs
    using Periodic_Fun.cos.plus_of_int [of z n]
    apply (simp add: algebra_simps)
    by (metis cos.plus_of_int cos_minus minus_add_cancel mult.commute)
qed

lemma sin_eq:
   "sin x = sin y  (n  . x = y + 2*n*pi  x = -y + (2*n + 1)*pi)"
  using complex_sin_eq [of x y]
  by (simp only: sin_of_real Re_complex_of_real of_real_add [symmetric] of_real_minus [symmetric] of_real_mult [symmetric] of_real_eq_iff)

lemma cos_eq:
   "cos x = cos y  (n  . x = y + 2*n*pi  x = -y + 2*n*pi)"
  using complex_cos_eq [of x y] unfolding cos_of_real 
  by (metis Re_complex_of_real of_real_add of_real_minus)

lemma sinh_complex:
  fixes z :: complex
  shows "(exp z - inverse (exp z)) / 2 = -𝗂 * sin(𝗂 * z)"
  by (simp add: sin_exp_eq field_split_simps exp_minus)

lemma sin_i_times:
  fixes z :: complex
  shows "sin(𝗂 * z) = 𝗂 * ((exp z - inverse (exp z)) / 2)"
  using sinh_complex by auto

lemma sinh_real:
  fixes x :: real
  shows "of_real((exp x - inverse (exp x)) / 2) = -𝗂 * sin(𝗂 * of_real x)"
  by (simp add: exp_of_real sin_i_times)

lemma cosh_complex:
  fixes z :: complex
  shows "(exp z + inverse (exp z)) / 2 = cos(𝗂 * z)"
  by (simp add: cos_exp_eq field_split_simps exp_minus exp_of_real)

lemma cosh_real:
  fixes x :: real
  shows "of_real((exp x + inverse (exp x)) / 2) = cos(𝗂 * of_real x)"
  by (simp add: cos_exp_eq field_split_simps exp_minus exp_of_real)

lemmas cos_i_times = cosh_complex [symmetric]

lemma norm_cos_squared:
  "norm(cos z) ^ 2 = cos(Re z) ^ 2 + (exp(Im z) - inverse(exp(Im z))) ^ 2 / 4"
proof (cases z)
  case (Complex x1 x2)
  then show ?thesis
    apply (simp only: cos_add cmod_power2 cos_of_real sin_of_real Complex_eq)
    apply (simp add: cos_exp_eq sin_exp_eq exp_minus exp_of_real Re_divide Im_divide power_divide)
    apply (simp only: left_diff_distrib [symmetric] power_mult_distrib sin_squared_eq)
    apply (simp add: power2_eq_square field_split_simps)
    done
qed

lemma norm_sin_squared:
  "norm(sin z) ^ 2 = (exp(2 * Im z) + inverse(exp(2 * Im z)) - 2 * cos(2 * Re z)) / 4"
  using cos_double_sin [of "Re z"]
  apply (simp add: sin_cos_eq norm_cos_squared exp_minus mult.commute [of _ 2] exp_double)
  apply (simp add: algebra_simps power2_eq_square)
  done

lemma exp_uminus_Im: "exp (- Im z)  exp (cmod z)"
  using abs_Im_le_cmod linear order_trans by fastforce

lemma norm_cos_le:
  fixes z::complex
  shows "norm(cos z)  exp(norm z)"
proof -
  have "Im z  cmod z"
    using abs_Im_le_cmod abs_le_D1 by auto
  then have "exp (- Im z) + exp (Im z)  exp (cmod z) * 2"
    by (metis exp_uminus_Im add_mono exp_le_cancel_iff mult_2_right)
  then show ?thesis
    by (force simp add: cos_exp_eq norm_divide intro: order_trans [OF norm_triangle_ineq])
qed

lemma norm_cos_plus1_le:
  fixes z::complex
  shows "norm(1 + cos z)  2 * exp(norm z)"
  by (metis mult_2 norm_cos_le norm_ge_zero norm_one norm_triangle_mono one_le_exp_iff)

lemma sinh_conv_sin: "sinh z = -𝗂 * sin (𝗂*z)"
  by (simp add: sinh_field_def sin_i_times exp_minus)

lemma cosh_conv_cos: "cosh z = cos (𝗂*z)"
  by (simp add: cosh_field_def cos_i_times exp_minus)

lemma tanh_conv_tan: "tanh z = -𝗂 * tan (𝗂*z)"
  by (simp add: tanh_def sinh_conv_sin cosh_conv_cos tan_def)

lemma sin_conv_sinh: "sin z = -𝗂 * sinh (𝗂*z)"
  by (simp add: sinh_conv_sin)

lemma cos_conv_cosh: "cos z = cosh (𝗂*z)"
  by (simp add: cosh_conv_cos)

lemma tan_conv_tanh: "tan z = -𝗂 * tanh (𝗂*z)"
  by (simp add: tan_def sin_conv_sinh cos_conv_cosh tanh_def)

lemma sinh_complex_eq_iff:
  "sinh (z :: complex) = sinh w 
      (n. z = w - 2 * 𝗂 * of_real n * of_real pi 
              z = -(2 * complex_of_real n + 1) * 𝗂 * complex_of_real pi - w)" (is "_ = ?rhs")
proof -
  have "sinh z = sinh w  sin (𝗂 * z) = sin (𝗂 * w)"
    by (simp add: sinh_conv_sin)
  also have "  ?rhs"
    by (subst complex_sin_eq) (force simp: field_simps complex_eq_iff)
  finally show ?thesis .
qed


subsectiontag unimportant›‹Taylor series for complex exponential, sine and cosine›

declare power_Suc [simp del]

lemma Taylor_exp_field:
  fixes z::"'a::{banach,real_normed_field}"
  shows "norm (exp z - (in. z ^ i / fact i))  exp (norm z) * (norm z ^ Suc n) / fact n"
proof (rule field_Taylor[of _ n "λk. exp" "exp (norm z)" 0 z, simplified])
  show "convex (closed_segment 0 z)"
    by (rule convex_closed_segment [of 0 z])
next
  fix k x
  assume "x  closed_segment 0 z" "k  n"
  show "(exp has_field_derivative exp x) (at x within closed_segment 0 z)"
    using DERIV_exp DERIV_subset by blast
next
  fix x
  assume x: "x  closed_segment 0 z"
  have "norm (exp x)  exp (norm x)"
    by (rule norm_exp)
  also have "norm x  norm z"
    using x by (auto simp: closed_segment_def intro!: mult_left_le_one_le)
  finally show "norm (exp x)  exp (norm z)"
    by simp
qed auto

text ‹For complex @{term z}, a tighter bound than in the previous result›
lemma Taylor_exp:
  "norm(exp z - (kn. z ^ k / (fact k)))  exp¦Re z¦ * (norm z) ^ (Suc n) / (fact n)"
proof (rule complex_Taylor [of _ n "λk. exp" "exp¦Re z¦" 0 z, simplified])
  show "convex (closed_segment 0 z)"
    by (rule convex_closed_segment [of 0 z])
next
  fix k x
  assume "x  closed_segment 0 z" "k  n"
  show "(exp has_field_derivative exp x) (at x within closed_segment 0 z)"
    using DERIV_exp DERIV_subset by blast
next
  fix x
  assume "x  closed_segment 0 z"
  then obtain u where u: "x = complex_of_real u * z" "0  u" "u  1"
    by (auto simp: closed_segment_def scaleR_conv_of_real)
  then have "u * Re z  ¦Re z¦"
    by (metis abs_ge_self abs_ge_zero mult.commute mult.right_neutral mult_mono)
  then show "Re x  ¦Re z¦"
    by (simp add: u)
qed auto

lemma
  assumes "0  u" "u  1"
  shows cmod_sin_le_exp: "cmod (sin (u *R z))  exp ¦Im z¦"
    and cmod_cos_le_exp: "cmod (cos (u *R z))  exp ¦Im z¦"
proof -
  have mono: "u w z::real. w  u  z  u  (w + z)/2  u"
    by simp
  have *: "(cmod (exp (𝗂 * (u * z))) + cmod (exp (- (𝗂 * (u * z)))) ) / 2  exp ¦Im z¦"
  proof (rule mono)
    show "cmod (exp (𝗂 * (u * z)))  exp ¦Im z¦"
      using assms
      by (auto simp: abs_if mult_left_le_one_le not_less intro: order_trans [of _ 0])
    show "cmod (exp (- (𝗂 * (u * z))))  exp ¦Im z¦"
      using assms
      by (auto simp: abs_if mult_left_le_one_le mult_nonneg_nonpos intro: order_trans [of _ 0])
  qed
  have "cmod (sin (u *R z)) = cmod (exp (𝗂 * (u * z)) - exp (- (𝗂 * (u * z)))) / 2"
    by (auto simp: scaleR_conv_of_real norm_mult norm_power sin_exp_eq norm_divide)
  also have "  (cmod (exp (𝗂 * (u * z))) + cmod (exp (- (𝗂 * (u * z)))) ) / 2"
    by (intro divide_right_mono norm_triangle_ineq4) simp
  also have "  exp ¦Im z¦"
    by (rule *)
  finally show "cmod (sin (u *R z))  exp ¦Im z¦" .
  have "cmod (cos (u *R z)) = cmod (exp (𝗂 * (u * z)) + exp (- (𝗂 * (u * z)))) / 2"
    by (auto simp: scaleR_conv_of_real norm_mult norm_power cos_exp_eq norm_divide)
  also have "  (cmod (exp (𝗂 * (u * z))) + cmod (exp (- (𝗂 * (u * z)))) ) / 2"
    by (intro divide_right_mono norm_triangle_ineq) simp
  also have "  exp ¦Im z¦"
    by (rule *)
  finally show "cmod (cos (u *R z))  exp ¦Im z¦" .
qed

lemma Taylor_sin:
  "norm(sin z - (kn. complex_of_real (sin_coeff k) * z ^ k))
    exp¦Im z¦ * (norm z) ^ (Suc n) / (fact n)"
proof -
  have mono: "u w z::real. w  u  z  u  w + z  u*2"
      by arith
  have *: "cmod (sin z -
                 (in. (-1) ^ (i div 2) * (if even i then sin 0 else cos 0) * z ^ i / (fact i)))
            exp ¦Im z¦ * cmod z ^ Suc n / (fact n)"
  proof (rule complex_Taylor [of "closed_segment 0 z" n
                                 "λk x. (-1)^(k div 2) * (if even k then sin x else cos x)"
                                 "exp¦Im z¦" 0 z, simplified])
    fix k x
    show "((λx. (- 1) ^ (k div 2) * (if even k then sin x else cos x)) has_field_derivative
            (- 1) ^ (Suc k div 2) * (if odd k then sin x else cos x))
            (at x within closed_segment 0 z)"
      by (cases "even k") (intro derivative_eq_intros | simp add: power_Suc)+
  next
    fix x
    assume "x  closed_segment 0 z"
    then show "cmod ((- 1) ^ (Suc n div 2) * (if odd n then sin x else cos x))  exp ¦Im z¦"
      by (auto simp: closed_segment_def norm_mult norm_power cmod_sin_le_exp cmod_cos_le_exp)
  qed
  have **: "k. complex_of_real (sin_coeff k) * z ^ k
            = (-1)^(k div 2) * (if even k then sin 0 else cos 0) * z^k / of_nat (fact k)"
    by (auto simp: sin_coeff_def elim!: oddE)
  show ?thesis
    by (simp add: ** order_trans [OF _ *])
qed

lemma Taylor_cos:
  "norm(cos z - (kn. complex_of_real (cos_coeff k) * z ^ k))
    exp¦Im z¦ * (norm z) ^ Suc n / (fact n)"
proof -
  have mono: "u w z::real. w  u  z  u  w + z  u*2"
      by arith
  have *: "cmod (cos z -
                 (in. (-1) ^ (Suc i div 2) * (if even i then cos 0 else sin 0) * z ^ i / (fact i)))
            exp ¦Im z¦ * cmod z ^ Suc n / (fact n)"
  proof (rule complex_Taylor [of "closed_segment 0 z" n 
                                 "λk x. (-1)^(Suc k div 2) * (if even k then cos x else sin x)" 
                                 "exp¦Im z¦" 0 z, simplified])
    fix k x
    assume "x  closed_segment 0 z" "k  n"
    show "((λx. (- 1) ^ (Suc k div 2) * (if even k then cos x else sin x)) has_field_derivative
            (- 1) ^ Suc (k div 2) * (if odd k then cos x else sin x))
             (at x within closed_segment 0 z)"
      by (cases "even k") (intro derivative_eq_intros | simp add: power_Suc)+
  next
    fix x
    assume "x  closed_segment 0 z"
    then show "cmod ((- 1) ^ Suc (n div 2) * (if odd n then cos x else sin x))  exp ¦Im z¦"
      by (auto simp: closed_segment_def norm_mult norm_power cmod_sin_le_exp cmod_cos_le_exp)
  qed
  have **: "k. complex_of_real (cos_coeff k) * z ^ k
            = (-1)^(Suc k div 2) * (if even k then cos 0 else sin 0) * z^k / of_nat (fact k)"
    by (auto simp: cos_coeff_def elim!: evenE)
  show ?thesis
    by (simp add: ** order_trans [OF _ *])
qed

declare power_Suc [simp]

text‹32-bit Approximation to e›
lemma e_approx_32: "¦exp(1) - 5837465777 / 2147483648¦  (inverse(2 ^ 32)::real)"
  using Taylor_exp [of 1 14] exp_le
  apply (simp add: sum_distrib_right in_Reals_norm Re_exp atMost_nat_numeral fact_numeral)
  apply (simp only: pos_le_divide_eq [symmetric])
  done

lemma e_less_272: "exp 1 < (272/100::real)"
  using e_approx_32
  by (simp add: abs_if split: if_split_asm)

lemma ln_272_gt_1: "ln (272/100) > (1::real)"
  by (metis e_less_272 exp_less_cancel_iff exp_ln_iff less_trans ln_exp)

text‹Apparently redundant. But many arguments involve integers.›
lemma ln3_gt_1: "ln 3 > (1::real)"
  by (simp add: less_trans [OF ln_272_gt_1])

subsection‹The argument of a complex number (HOL Light version)›

definitiontag important› is_Arg :: "[complex,real]  bool"
  where "is_Arg z r  z = of_real(norm z) * exp(𝗂 * of_real r)"

definitiontag important› Arg2pi :: "complex  real"
  where "Arg2pi z  if z = 0 then 0 else THE t. 0  t  t < 2*pi  is_Arg z t"

lemma is_Arg_2pi_iff: "is_Arg z (r + of_int k * (2 * pi))  is_Arg z r"
  by (simp add: algebra_simps is_Arg_def)

lemma is_Arg_eqI:
  assumes "is_Arg z r" and "is_Arg z s" and "abs (r-s) < 2*pi" and "z  0"
  shows "r = s"
  using assms unfolding is_Arg_def
  by (metis Im_i_times Re_complex_of_real exp_complex_eqI mult_cancel_left mult_eq_0_iff)

text‹This function returns the angle of a complex number from its representation in polar coordinates.
Due to periodicity, its range is arbitrary. termArg2pi follows HOL Light in adopting the interval [0,2π)›.
But we have the same periodicity issue with logarithms, and it is usual to adopt the same interval
for the complex logarithm and argument functions. Further on down, we shall define both functions for the interval (-π,π]›.
The present version is provided for compatibility.›

lemma Arg2pi_0 [simp]: "Arg2pi(0) = 0"
  by (simp add: Arg2pi_def)

lemma Arg2pi_unique_lemma:
  assumes "is_Arg z t"
      and "is_Arg z t'"
      and "0  t"  "t < 2*pi"
      and "0  t'" "t' < 2*pi"
      and "z  0"
  shows "t' = t"
  using is_Arg_eqI assms by force

lemma Arg2pi: "0  Arg2pi z  Arg2pi z < 2*pi  is_Arg z (Arg2pi z)"
proof (cases "z=0")
  case True then show ?thesis
    by (simp add: Arg2pi_def is_Arg_def)
next
  case False
  obtain t where t: "0  t" "t < 2*pi"
             and ReIm: "Re z / cmod z = cos t" "Im z / cmod z = sin t"
    using sincos_total_2pi [OF complex_unit_circle [OF False]]
    by blast
  then have z: "is_Arg z t"
    unfolding is_Arg_def
    using t False ReIm
    by (intro complex_eqI) (auto simp: exp_Euler sin_of_real cos_of_real field_split_simps)
  show ?thesis
    apply (simp add: Arg2pi_def False)
    apply (rule theI [where a=t])
    using t z False
    apply (auto intro: Arg2pi_unique_lemma)
    done
qed

corollarytag unimportant›
  shows Arg2pi_ge_0: "0  Arg2pi z"
    and Arg2pi_lt_2pi: "Arg2pi z < 2*pi"
    and Arg2pi_eq: "z = of_real(norm z) * exp(𝗂 * of_real(Arg2pi z))"
  using Arg2pi is_Arg_def by auto

lemma complex_norm_eq_1_exp: "norm z = 1  exp(𝗂 * of_real (Arg2pi z)) = z"
  by (metis Arg2pi_eq cis_conv_exp mult.left_neutral norm_cis of_real_1)

lemma Arg2pi_unique: "of_real r * exp(𝗂 * of_real a) = z; 0 < r; 0  a; a < 2*pi  Arg2pi z = a"
  by (rule Arg2pi_unique_lemma [unfolded is_Arg_def, OF _ Arg2pi_eq]) (use Arg2pi [of z] in auto simp: norm_mult)

lemma cos_Arg2pi: "cmod z * cos (Arg2pi z) = Re z" and sin_Arg2pi: "cmod z * sin (Arg2pi z) = Im z"
  using Arg2pi_eq [of z] cis_conv_exp Re_rcis Im_rcis unfolding rcis_def by metis+

lemma Arg2pi_minus:
  assumes "z  0" shows "Arg2pi (-z) = (if Arg2pi z < pi then Arg2pi z + pi else Arg2pi z - pi)"
  apply (rule Arg2pi_unique [of "norm z", OF complex_eqI])
  using cos_Arg2pi sin_Arg2pi Arg2pi_ge_0 Arg2pi_lt_2pi [of z] assms
  by (auto simp: Re_exp Im_exp)

lemma Arg2pi_times_of_real [simp]:
  assumes "0 < r" shows "Arg2pi (of_real r * z) = Arg2pi z"
  by (metis (no_types, lifting) Arg2pi Arg2pi_eq Arg2pi_unique assms mult.assoc 
      mult_eq_0_iff mult_pos_pos of_real_mult zero_less_norm_iff)

lemma Arg2pi_times_of_real2 [simp]: "0 < r  Arg2pi (z * of_real r) = Arg2pi z"
  by (metis Arg2pi_times_of_real mult.commute)

lemma Arg2pi_divide_of_real [simp]: "0 < r  Arg2pi (z / of_real r) = Arg2pi z"
  by (metis Arg2pi_times_of_real2 less_irrefl nonzero_eq_divide_eq of_real_eq_0_iff)

lemma Arg2pi_le_pi: "Arg2pi z  pi  0  Im z"
proof (cases "z=0")
  case False
  have "0  Im z  0  Im (of_real (cmod z) * exp (𝗂 * complex_of_real (Arg2pi z)))"
    by (metis Arg2pi_eq)
  also have " = (0  Im (exp (𝗂 * complex_of_real (Arg2pi z))))"
    using False  by (simp add: zero_le_mult_iff)
  also have "  Arg2pi z  pi"
    by (simp add: Im_exp) (metis Arg2pi_ge_0 Arg2pi_lt_2pi sin_lt_zero sin_ge_zero not_le)
  finally show ?thesis
    by blast
qed auto

lemma Arg2pi_lt_pi: "0 < Arg2pi z  Arg2pi z < pi  0 < Im z"
  using Arg2pi_le_pi [of z]
  by (smt (verit, del_insts) Arg2pi_0 Arg2pi_le_pi Arg2pi_minus uminus_complex.simps(2) zero_complex.simps(2))

lemma Arg2pi_eq_0: "Arg2pi z = 0  z    0  Re z"
proof (cases "z=0")
  case False
  then have "z    0  Re z  z    0  Re (exp (𝗂 * complex_of_real (Arg2pi z)))"
    by (metis cis.sel(1) cis_conv_exp cos_Arg2pi norm_ge_zero norm_le_zero_iff zero_le_mult_iff)
  also have "  Arg2pi z = 0"
  proof -
    have [simp]: "Arg2pi z = 0  z  "
      using Arg2pi_eq [of z] by (auto simp: Reals_def)
    moreover have "z  ; 0  cos (Arg2pi z)  Arg2pi z = 0"
      by (smt (verit, ccfv_SIG) Arg2pi_ge_0 Arg2pi_le_pi Arg2pi_lt_pi complex_is_Real_iff cos_pi)
    ultimately show ?thesis
      by (auto simp: Re_exp)
  qed
  finally show ?thesis
    by blast
qed auto

corollarytag unimportant› Arg2pi_gt_0:
  assumes "z  0"
    shows "Arg2pi z > 0"
  using Arg2pi_eq_0 Arg2pi_ge_0 assms dual_order.strict_iff_order
  unfolding nonneg_Reals_def by fastforce

lemma Arg2pi_eq_pi: "Arg2pi z = pi  z    Re z < 0"
    using Arg2pi_le_pi [of z] Arg2pi_lt_pi [of z] Arg2pi_eq_0 [of z] Arg2pi_ge_0 [of z]
    by (fastforce simp: complex_is_Real_iff)

lemma Arg2pi_eq_0_pi: "Arg2pi z = 0  Arg2pi z = pi  z  "
  using Arg2pi_eq_0 Arg2pi_eq_pi not_le by auto

lemma Arg2pi_of_real: "Arg2pi (of_real r) = (if r<0 then pi else 0)"
  using Arg2pi_eq_0_pi Arg2pi_eq_pi by fastforce

lemma Arg2pi_real: "z    Arg2pi z = (if 0  Re z then 0 else pi)"
  using Arg2pi_eq_0 Arg2pi_eq_0_pi by auto

lemma Arg2pi_inverse: "Arg2pi(inverse z) = (if z   then Arg2pi z else 2*pi - Arg2pi z)"
proof (cases "z=0")
  case False
  show ?thesis
    apply (rule Arg2pi_unique [of "inverse (norm z)"])
    using Arg2pi_eq False Arg2pi_ge_0 [of z] Arg2pi_lt_2pi [of z] Arg2pi_eq_0 [of z]
    by (auto simp: Arg2pi_real in_Reals_norm exp_diff field_simps)
qed auto

lemma Arg2pi_eq_iff:
  assumes "w  0" "z  0"
  shows "Arg2pi w = Arg2pi z  (x. 0 < x  w = of_real x * z)" (is "?lhs = ?rhs")
proof
  assume ?lhs
  then have "(cmod w) * (z / cmod z) = w"
    by (metis Arg2pi_eq assms(2) mult_eq_0_iff nonzero_mult_div_cancel_left)
  then show ?rhs
    by (metis assms divide_pos_pos of_real_divide times_divide_eq_left times_divide_eq_right zero_less_norm_iff)
qed auto

lemma Arg2pi_inverse_eq_0: "Arg2pi(inverse z) = 0  Arg2pi z = 0"
  by (metis Arg2pi_eq_0 Arg2pi_inverse inverse_inverse_eq)

lemma Arg2pi_divide:
  assumes "w  0" "z  0" "Arg2pi w  Arg2pi z"
    shows "Arg2pi(z / w) = Arg2pi z - Arg2pi w"
  apply (rule Arg2pi_unique [of "norm(z / w)"])
  using assms Arg2pi_eq Arg2pi_ge_0 [of w] Arg2pi_lt_2pi [of z]
  apply (auto simp: exp_diff norm_divide field_simps)
  done

lemma Arg2pi_le_div_sum:
  assumes "w  0" "z  0" "Arg2pi w  Arg2pi z"
    shows "Arg2pi z = Arg2pi w + Arg2pi(z / w)"
  by (simp add: Arg2pi_divide assms)

lemma Arg2pi_le_div_sum_eq:
  assumes "w  0" "z  0"
    shows "Arg2pi w  Arg2pi z  Arg2pi z = Arg2pi w + Arg2pi(z / w)"
  using assms by (auto simp: Arg2pi_ge_0 intro: Arg2pi_le_div_sum)

lemma Arg2pi_diff:
  assumes "w  0" "z  0"
    shows "Arg2pi w - Arg2pi z = (if Arg2pi z  Arg2pi w then Arg2pi(w / z) else Arg2pi(w/z) - 2*pi)"
  using assms Arg2pi_divide Arg2pi_inverse [of "w/z"] Arg2pi_eq_0_pi
  by (force simp add: Arg2pi_ge_0 Arg2pi_divide not_le split: if_split_asm)

lemma Arg2pi_add:
  assumes "w  0" "z  0"
    shows "Arg2pi w + Arg2pi z = (if Arg2pi w + Arg2pi z < 2*pi then Arg2pi(w * z) else Arg2pi(w * z) + 2*pi)"
  using assms Arg2pi_diff [of "w*z" z] Arg2pi_le_div_sum_eq [of z "w*z"] Arg2pi [of "w * z"]
  by auto

lemma Arg2pi_times:
  assumes "w  0" "z  0"
    shows "Arg2pi (w * z) = (if Arg2pi w + Arg2pi z < 2*pi then Arg2pi w + Arg2pi z
                            else (Arg2pi w + Arg2pi z) - 2*pi)"
  using Arg2pi_add [OF assms] by auto

lemma Arg2pi_cnj_eq_inverse:
  assumes "z  0" shows "Arg2pi (cnj z) = Arg2pi (inverse z)"
proof -
  have "r>0. of_real r / z = cnj z"
    by (metis assms complex_norm_square nonzero_mult_div_cancel_left zero_less_norm_iff zero_less_power)
  then show ?thesis
    by (metis Arg2pi_times_of_real2 divide_inverse_commute)
qed

lemma Arg2pi_cnj: "Arg2pi(cnj z) = (if z   then Arg2pi z else 2*pi - Arg2pi z)"
  by (metis Arg2pi_cnj_eq_inverse Arg2pi_inverse Reals_cnj_iff complex_cnj_zero)

lemma Arg2pi_exp: "0  Im z  Im z < 2*pi  Arg2pi(exp z) = Im z"
  by (simp add: Arg2pi_unique exp_eq_polar)

lemma complex_split_polar:
  obtains r a::real where "z = complex_of_real r * (cos a + 𝗂 * sin a)" "0  r" "0  a" "a < 2*pi"
  using Arg2pi cis.ctr cis_conv_exp unfolding Complex_eq is_Arg_def by fastforce

lemma Re_Im_le_cmod: "Im w * sin φ + Re w * cos φ  cmod w"
proof (cases w rule: complex_split_polar)
  case (1 r a) with sin_cos_le1 [of a φ] show ?thesis
    apply (simp add: norm_mult cmod_unit_one)
    by (metis (no_types, opaque_lifting) abs_le_D1 distrib_left mult.commute mult.left_commute mult_left_le)
qed

subsectiontag unimportant›‹Analytic properties of tangent function›

lemma cnj_tan: "cnj(tan z) = tan(cnj z)"
  by (simp add: cnj_cos cnj_sin tan_def)

lemma field_differentiable_at_tan: "cos z  0  tan field_differentiable at z"
  unfolding field_differentiable_def
  using DERIV_tan by blast

lemma field_differentiable_within_tan: "cos z  0
          tan field_differentiable (at z within s)"
  using field_differentiable_at_tan field_differentiable_at_within by blast

lemma continuous_within_tan: "cos z  0  continuous (at z within s) tan"
  using continuous_at_imp_continuous_within isCont_tan by blast

lemma continuous_on_tan [continuous_intros]: "(z. z  s  cos z  0)  continuous_on s tan"
  by (simp add: continuous_at_imp_continuous_on)

lemma holomorphic_on_tan: "(z. z  s  cos z  0)  tan holomorphic_on s"
  by (simp add: field_differentiable_within_tan holomorphic_on_def)


subsection‹The principal branch of the Complex logarithm›

instantiation complex :: ln
begin

definitiontag important› ln_complex :: "complex  complex"
  where "ln_complex  λz. THE w. exp w = z & -pi < Im(w) & Im(w)  pi"

text‹NOTE: within this scope, the constant Ln is not yet available!›
lemma
  assumes "z  0"
    shows exp_Ln [simp]:  "exp(ln z) = z"
      and mpi_less_Im_Ln: "-pi < Im(ln z)"
      and Im_Ln_le_pi:    "Im(ln z)  pi"
proof -
  obtain ψ where z: "z / (cmod z) = Complex (cos ψ) (sin ψ)"
    using complex_unimodular_polar [of "z / (norm z)"] assms
    by (auto simp: norm_divide field_split_simps)
  obtain φ where φ: "- pi < φ" "φ  pi" "sin φ = sin ψ" "cos φ = cos ψ"
    using sincos_principal_value [of "ψ"] assms
    by (auto simp: norm_divide field_split_simps)
  have "exp(ln z) = z & -pi < Im(ln z) & Im(ln z)  pi" unfolding ln_complex_def
    apply (rule theI [where a = "Complex (ln(norm z)) φ"])
    using z assms φ
    apply (auto simp: field_simps exp_complex_eqI exp_eq_polar cis.code)
    done
  then show "exp(ln z) = z" "-pi < Im(ln z)" "Im(ln z)  pi"
    by auto
qed

lemma Ln_exp [simp]:
  assumes "-pi < Im(z)" "Im(z)  pi"
    shows "ln(exp z) = z"
proof (rule exp_complex_eqI)
  show "¦Im (ln (exp z)) - Im z¦ < 2 * pi"
    using assms mpi_less_Im_Ln  [of "exp z"] Im_Ln_le_pi [of "exp z"] by auto
qed auto

subsectiontag unimportant›‹Relation to Real Logarithm›

lemma Ln_of_real:
  assumes "0 < z"
    shows "ln(of_real z::complex) = of_real(ln z)"
  by (smt (verit) Im_complex_of_real Ln_exp assms exp_ln of_real_exp pi_ge_two)

corollarytag unimportant› Ln_in_Reals [simp]: "z    Re z > 0  ln z  "
  by (auto simp: Ln_of_real elim: Reals_cases)

corollarytag unimportant› Im_Ln_of_real [simp]: "r > 0  Im (ln (of_real r)) = 0"
  by (simp add: Ln_of_real)

lemma cmod_Ln_Reals [simp]: "z    0 < Re z  cmod (ln z) = norm (ln (Re z))"
  using Ln_of_real by force

lemma Ln_Reals_eq: "x  ; Re x > 0  ln x = of_real (ln (Re x))"
  using Ln_of_real by force

lemma Ln_1 [simp]: "ln 1 = (0::complex)"
  by (smt (verit, best) Ln_of_real ln_one of_real_0 of_real_1)

lemma Ln_eq_zero_iff [simp]: "x  0  ln x = 0  x = 1" for x::complex
  by (metis (mono_tags, lifting) Ln_1 exp_Ln exp_zero nonpos_Reals_zero_I)

instance
  by intro_classes (rule ln_complex_def Ln_1)

end

abbreviation Ln :: "complex  complex"
  where "Ln  ln"

lemma Ln_eq_iff: "w  0  z  0  (Ln w = Ln z  w = z)"
  by (metis exp_Ln)

lemma Ln_unique: "exp(z) = w  -pi < Im(z)  Im(z)  pi  Ln w = z"
  using Ln_exp by blast

lemma Re_Ln [simp]: "z  0  Re(Ln z) = ln(norm z)"
  by (metis exp_Ln ln_exp norm_exp_eq_Re)

corollarytag unimportant› ln_cmod_le:
  assumes z: "z  0"
    shows "ln (cmod z)  cmod (Ln z)"
  by (metis Re_Ln complex_Re_le_cmod z)

propositiontag unimportant› exists_complex_root:
  fixes z :: complex
  assumes "n  0"  obtains w where "z = w ^ n"
  by (metis assms exp_Ln exp_of_nat_mult nonzero_mult_div_cancel_left of_nat_eq_0_iff power_0_left times_divide_eq_right)

corollarytag unimportant› exists_complex_root_nonzero:
  fixes z::complex
  assumes "z  0" "n  0"
  obtains w where "w  0" "z = w ^ n"
  by (metis exists_complex_root [of n z] assms power_0_left)

subsectiontag unimportant›‹Derivative of Ln away from the branch cut›

lemma Im_Ln_less_pi: 
  assumes "z  0"shows "Im (Ln z) < pi"
proof -
  have znz [simp]: "z  0"
    using assms by auto
  with Im_Ln_le_pi [of z] show ?thesis
    by (smt (verit, best) Arg2pi_eq_0_pi Arg2pi_exp Ln_in_Reals assms complex_is_Real_iff complex_nonpos_Reals_iff exp_Ln pi_ge_two)
qed

lemma has_field_derivative_Ln: 
  assumes "z  0"
  shows "(Ln has_field_derivative inverse(z)) (at z)"
proof -
  have znz [simp]: "z  0"
    using assms by auto
  then have "Im (Ln z)  pi"
    by (smt (verit, best) Arg2pi_eq_0_pi Arg2pi_exp Ln_in_Reals assms complex_is_Real_iff complex_nonpos_Reals_iff exp_Ln pi_ge_two)
  let ?U = "{w. -pi < Im(w)  Im(w) < pi}"
  have 1: "open ?U"
    by (simp add: open_Collect_conj open_halfspace_Im_gt open_halfspace_Im_lt)
  have 2: "x. x  ?U  (exp has_derivative blinfun_apply(Blinfun ((*) (exp x)))) (at x)"
    by (simp add: bounded_linear_Blinfun_apply bounded_linear_mult_right has_field_derivative_imp_has_derivative)
  have 3: "continuous_on ?U (λx. Blinfun ((*) (exp x)))"
    unfolding blinfun_mult_right.abs_eq [symmetric] by (intro continuous_intros)
  have 4: "Ln z  ?U"
    by (simp add: Im_Ln_less_pi assms mpi_less_Im_Ln)
  have 5: "Blinfun ((*) (inverse z)) oL Blinfun ((*) (exp (Ln z))) = id_blinfun"
    by (rule blinfun_eqI) (simp add: bounded_linear_mult_right bounded_linear_Blinfun_apply)
  obtain U' V g g' where "open U'" and sub: "U'  ?U"
    and "Ln z  U'" "open V" "z  V"
    and hom: "homeomorphism U' V exp g"
    and g: "y. y  V  (g has_derivative (g' y)) (at y)"
    and g': "y. y  V  g' y = inv ((*) (exp (g y)))"
    and bij: "y. y  V  bij ((*) (exp (g y)))"
    using inverse_function_theorem [OF 1 2 3 4 5]
    by (simp add: bounded_linear_Blinfun_apply bounded_linear_mult_right) blast
  show "(Ln has_field_derivative inverse(z)) (at z)"
    unfolding has_field_derivative_def
  proof (rule has_derivative_transform_within_open)
    show g_eq_Ln: "g y = Ln y" if "y  V" for y
      by (smt (verit, ccfv_threshold) Ln_exp hom homeomorphism_def imageI mem_Collect_eq sub subset_iff that)
    have "0  V"
      by (meson exp_not_eq_zero hom homeomorphism_def)
    then have "y. y  V  g' y = inv ((*) y)"
      by (metis exp_Ln g' g_eq_Ln)
    then have g': "g' z = (λx. x/z)"
      by (metis z  V bij bij_inv_eq_iff exp_Ln g_eq_Ln nonzero_mult_div_cancel_left znz)
    show "(g has_derivative (*) (inverse z)) (at z)"
      using g [OF z  V] g' by (simp add: divide_inverse_commute)
  qed (auto simp: z  V open V)
qed

declare has_field_derivative_Ln [derivative_intros]
declare has_field_derivative_Ln [THEN DERIV_chain2, derivative_intros]

lemma field_differentiable_at_Ln: "z  0  Ln field_differentiable at z"
  using field_differentiable_def has_field_derivative_Ln by blast

lemma field_differentiable_within_Ln: "z  0
          Ln field_differentiable (at z within S)"
  using field_differentiable_at_Ln field_differentiable_within_subset by blast

lemma continuous_at_Ln: "z  0  continuous (at z) Ln"
  by (simp add: field_differentiable_imp_continuous_at field_differentiable_within_Ln)

lemma isCont_Ln' [simp,continuous_intros]:
   "isCont f z; f z  0  isCont (λx. Ln (f x)) z"
  by (blast intro: isCont_o2 [OF _ continuous_at_Ln])

lemma continuous_within_Ln [continuous_intros]: "z  0  continuous (at z within S) Ln"
  using continuous_at_Ln continuous_at_imp_continuous_within by blast

lemma continuous_on_Ln [continuous_intros]: "(z. z  S  z  0)  continuous_on S Ln"
  by (simp add: continuous_at_imp_continuous_on continuous_within_Ln)

lemma continuous_on_Ln' [continuous_intros]:
  "continuous_on S f  (z. z  S  f z  0)  continuous_on S (λx. Ln (f x))"
  by (rule continuous_on_compose2[OF continuous_on_Ln, of "UNIV - nonpos_Reals" S f]) auto

lemma holomorphic_on_Ln [holomorphic_intros]: "S  0 = {}  Ln holomorphic_on S"
  by (simp add: disjoint_iff field_differentiable_within_Ln holomorphic_on_def)

lemma holomorphic_on_Ln' [holomorphic_intros]:
  "(z. z  A  f z  0)  f holomorphic_on A  (λz. Ln (f z)) holomorphic_on A"
  using holomorphic_on_compose_gen[OF _ holomorphic_on_Ln, of f A "- 0"]
  by (auto simp: o_def)

lemma analytic_on_ln [analytic_intros]:
  assumes "f analytic_on A" "f ` A  0 = {}"
  shows   "(λw. ln (f w)) analytic_on A"
proof -
  have *: "ln analytic_on (-0)"
    by (subst analytic_on_open) (auto intro!: holomorphic_intros)
  have "(ln  f) analytic_on A"
    by (rule analytic_on_compose_gen[OF assms(1) *]) (use assms(2) in auto)
  thus ?thesis
    by (simp add: o_def)
qed

lemma tendsto_Ln [tendsto_intros]:
  assumes "(f  L) F" "L  0"
  shows   "((λx. Ln (f x))  Ln L) F"
  by (simp add: assms isCont_tendsto_compose)

lemma divide_ln_mono:
  fixes x y::real
  assumes "3  x" "x  y"
  shows "x / ln x  y / ln y"
proof -
  have "u. x  u; u  y  ((λz. z / Ln z) has_field_derivative 1 / Ln u - 1 / (Ln u)2) (at u)"
    using 3  x by (force intro!: derivative_eq_intros simp: field_simps power_eq_if)
  moreover
  have "x / ln x  y / ln y"
    if "Re (y / Ln y) - Re (x / Ln x) = (Re (1 / Ln u) - Re (1 / (Ln u)2)) * (y - x)"
    and x: "x  u" "u  y" for u
  proof -
    have eq: "y / ln y = (1 / ln u - 1 / (ln u)2) * (y - x) + x / ln x"
      using that 3  x by (auto simp: Ln_Reals_eq in_Reals_norm group_add_class.diff_eq_eq)
    show ?thesis
      using exp_le 3  x x by (simp add: eq) (simp add: power_eq_if divide_simps ln_ge_iff)
  qed
  ultimately show ?thesis
    using complex_mvt_line [of x y "λz. z / Ln z" "λz. 1/(Ln z) - 1/(Ln z)^2"] assms
    by (force simp add: closed_segment_Reals closed_segment_eq_real_ivl)
qed

theorem Ln_series:
  fixes z :: complex
  assumes "norm z < 1"
  shows   "(λn. (-1)^Suc n / of_nat n * z^n) sums ln (1 + z)" (is "(λn. ?f n * z^n) sums _")
proof -
  let ?F = "λz. n. ?f n * z^n" and ?F' = "λz. n. diffs ?f n * z^n"
  have r: "conv_radius ?f = 1"
    by (intro conv_radius_ratio_limit_nonzero[of _ 1])
       (simp_all add: norm_divide LIMSEQ_Suc_n_over_n del: of_nat_Suc)

  have "c. zball 0 1. ln (1 + z) - ?F z = c"
  proof (rule has_field_derivative_zero_constant)
    fix z :: complex assume z': "z  ball 0 1"
    hence z: "norm z < 1" by simp
    define t :: complex where "t = of_real (1 + norm z) / 2"
    from z have t: "norm z < norm t" "norm t < 1" unfolding t_def
      by (simp_all add: field_simps norm_divide del: of_real_add)

    have "Re (-z)  norm (-z)" by (rule complex_Re_le_cmod)
    also from z have " < 1" by simp
    finally have "((λz. ln (1 + z)) has_field_derivative inverse (1+z)) (at z)"
      by (auto intro!: derivative_eq_intros simp: complex_nonpos_Reals_iff)
    moreover have "(?F has_field_derivative ?F' z) (at z)" using t r
      by (intro termdiffs_strong[of _ t] summable_in_conv_radius) simp_all
    ultimately have "((λz. ln (1 + z) - ?F z) has_field_derivative (inverse (1 + z) - ?F' z))
                       (at z within ball 0 1)"
      by (intro derivative_intros) (simp_all add: at_within_open[OF z'])
    also have "(λn. of_nat n * ?f n * z ^ (n - Suc 0)) sums ?F' z" using t r
      by (intro diffs_equiv termdiff_converges[OF t(1)] summable_in_conv_radius) simp_all
    from sums_split_initial_segment[OF this, of 1]
      have "(λi. (-z) ^ i) sums ?F' z" by (simp add: power_minus[of z] del: of_nat_Suc)
    hence "?F' z = inverse (1 + z)" using z by (simp add: sums_iff suminf_geometric divide_inverse)
    also have "inverse (1 + z) - inverse (1 + z) = 0" by simp
    finally show "((λz. ln (1 + z) - ?F z) has_field_derivative 0) (at z within ball 0 1)" .
  qed simp_all
  then obtain c where c: "z. z  ball 0 1  ln (1 + z) - ?F z = c" by blast
  from c[of 0] have "c = 0" by (simp only: powser_zero) simp
  with c[of z] assms have "ln (1 + z) = ?F z" by simp
  moreover have "summable (λn. ?f n * z^n)" using assms r
    by (intro summable_in_conv_radius) simp_all
  ultimately show ?thesis by (simp add: sums_iff)
qed

lemma Ln_series': "cmod z < 1  (λn. - ((-z)^n) / of_nat n) sums ln (1 + z)"
  by (drule Ln_series) (simp add: power_minus')

lemma ln_series':
  fixes x::real
  assumes "¦x¦ < 1"
  shows   "(λn. - ((-x)^n) / of_nat n) sums ln (1 + x)"
proof -
  from assms have "(λn. - ((-of_real x)^n) / of_nat n) sums ln (1 + complex_of_real x)"
    by (intro Ln_series') simp_all
  also have "(λn. - ((-of_real x)^n) / of_nat n) = (λn. complex_of_real (- ((-x)^n) / of_nat n))"
    by (rule ext) simp
  also from assms have "ln (1 + complex_of_real x) = of_real (ln (1 + x))"
    by (smt (verit) Ln_of_real of_real_1 of_real_add)
  finally show ?thesis by (subst (asm) sums_of_real_iff)
qed

lemma Ln_approx_linear:
  fixes z :: complex
  assumes "norm z < 1"
  shows   "norm (ln (1 + z) - z)  norm z^2 / (1 - norm z)"
proof -
  let ?f = "λn. (-1)^Suc n / of_nat n"
  from assms have "(λn. ?f n * z^n) sums ln (1 + z)" using Ln_series by simp
  moreover have "(λn. (if n = 1 then 1 else 0) * z^n) sums z" using powser_sums_if[of 1] by simp
  ultimately have "(λn. (?f n - (if n = 1 then 1 else 0)) * z^n) sums (ln (1 + z) - z)"
    by (subst left_diff_distrib, intro sums_diff) simp_all
  from sums_split_initial_segment[OF this, of "Suc 1"]
    have "(λi. (-(z^2)) * inverse (2 + of_nat i) * (- z)^i) sums (Ln (1 + z) - z)"
    by (simp add: power2_eq_square mult_ac power_minus[of z] divide_inverse)
  hence "(Ln (1 + z) - z) = (i. (-(z^2)) * inverse (of_nat (i+2)) * (-z)^i)"
    by (simp add: sums_iff)
  also have A: "summable (λn. norm z^2 * (inverse (real_of_nat (Suc (Suc n))) * cmod z ^ n))"
    by (rule summable_mult, rule summable_comparison_test_ev[OF _ summable_geometric[of "norm z"]])
       (auto simp: assms field_simps intro!: always_eventually)
  hence "norm (i. (-(z^2)) * inverse (of_nat (i+2)) * (-z)^i)
       (i. norm (-(z^2) * inverse (of_nat (i+2)) * (-z)^i))"
    by (intro summable_norm)
       (auto simp: norm_power norm_inverse norm_mult mult_ac simp del: of_nat_add of_nat_Suc)
  also have "norm ((-z)^2 * (-z)^i) * inverse (of_nat (i+2))  norm ((-z)^2 * (-z)^i) * 1" for i
    by (intro mult_left_mono) (simp_all add: field_split_simps)
  hence "(i. norm (-(z^2) * inverse (of_nat (i+2)) * (-z)^i))
        (i. norm (-(z^2) * (-z)^i))"
    using A assms
    unfolding norm_power norm_inverse norm_divide norm_mult
    apply (intro suminf_le summable_mult summable_geometric)
    apply (auto simp: norm_power field_simps simp del: of_nat_add of_nat_Suc)
    done
  also have " = norm z^2 * (i. norm z^i)" using assms
    by (subst suminf_mult [symmetric]) (auto intro!: summable_geometric simp: norm_mult norm_power)
  also have "(i. norm z^i) = inverse (1 - norm z)" using assms
    by (subst suminf_geometric) (simp_all add: divide_inverse)
  also have "norm z^2 *  = norm z^2 / (1 - norm z)" by (simp add: divide_inverse)
  finally show ?thesis .
qed


lemma norm_Ln_le:
  fixes z :: complex
  assumes "norm z < 1/2"
  shows   "norm (Ln(1+z))  2 * norm z"
proof -
  have sums: "(λn. - ((- z) ^ n) / of_nat n) sums ln (1 + z)"
    by (intro Ln_series') (use assms in auto)
  have summable: "summable (λn. norm (- ((- z) ^ n / of_nat n)))"
    using ln_series'[of "-norm z"] assms
    by (simp add: sums_iff summable_minus_iff norm_power norm_divide)

  have "norm (ln (1 + z)) = norm (n. -((-z) ^ n / of_nat n))"
    using sums by (simp add: sums_iff)
  also have "  (n. norm (-((-z) ^ n / of_nat n)))"
    using summable by (rule summable_norm)
  also have " = (n. norm (-((-z) ^ Suc n / of_nat (Suc n))))"
    using summable by (subst suminf_split_head) auto
  also have "  (n. norm z * (1 / 2) ^ n)"
  proof (rule suminf_le)
    show "summable (λn. norm z * (1 / 2) ^ n)"
      by (intro summable_mult summable_geometric) auto
  next
    show "summable (λn. norm (- ((- z) ^ Suc n / of_nat (Suc n))))"
      using summable by (subst summable_Suc_iff)
  next
    fix n
    have "norm (-((-z) ^ Suc n / of_nat (Suc n))) = norm z * (norm z ^ n / real (Suc n))"
      by (simp add: norm_power norm_divide norm_mult del: of_nat_Suc)
    also have "  norm z * ((1 / 2) ^ n / 1)"
      using assms by (intro mult_left_mono frac_le power_mono) auto
    finally show "norm (- ((- z) ^ Suc n / of_nat (Suc n)))  norm z * (1 / 2) ^ n"
      by simp
  qed
  also have " = norm z * (n. (1 / 2) ^ n)"
    by (subst suminf_mult) (auto intro: summable_geometric)
  also have "(n. (1 / 2 :: real) ^ n) = 2"
    using geometric_sums[of "1 / 2 :: real"] by (simp add: sums_iff)
  finally show ?thesis
    by (simp add: mult_ac)
qed

subsectiontag unimportant›‹Quadrant-type results for Ln›

lemma cos_lt_zero_pi: "pi/2 < x  x < 3*pi/2  cos x < 0"
  using cos_minus_pi cos_gt_zero_pi [of "x-pi"]
  by simp

lemma Re_Ln_pos_le:
  assumes "z  0"
  shows "¦Im(Ln z)¦  pi/2  0  Re(z)"
proof -
  { fix w
    assume "w = Ln z"
    then have w: "Im w  pi" "- pi < Im w"
      using Im_Ln_le_pi [of z]  mpi_less_Im_Ln [of z]  assms
      by auto
    then have "¦Im w¦  pi/2  0  Re(exp w)"
      using cos_lt_zero_pi [of "- (Im w)"] cos_lt_zero_pi [of "(Im w)"] not_le
      by (auto simp: Re_exp zero_le_mult_iff abs_if intro: cos_ge_zero)
  }
  then show ?thesis using assms
    by auto
qed

lemma Re_Ln_pos_lt:
  assumes "z  0"
  shows "¦Im(Ln z)¦ < pi/2  0 < Re(z)"
  using Re_Ln_pos_le assms
  by (smt (verit) Re_exp arccos_cos cos_minus cos_pi_half exp_Ln exp_gt_zero field_sum_of_halves mult_eq_0_iff)

lemma Im_Ln_pos_le:
  assumes "z  0"
    shows "0  Im(Ln z)  Im(Ln z)  pi  0  Im(z)"
proof -
  { fix w
    assume "w = Ln z"
    then have w: "Im w  pi" "- pi < Im w"
      using Im_Ln_le_pi [of z]  mpi_less_Im_Ln [of z]  assms
      by auto
    then have "0  Im w  Im w  pi  0  Im(exp w)"
      using sin_ge_zero [of "- (Im w)"] sin_ge_zero [of "abs(Im w)"] sin_zero_pi_iff [of "Im w"]
      by (force simp: Im_exp zero_le_mult_iff sin_ge_zero) }
  then show ?thesis using assms
    by auto
qed

lemma Im_Ln_pos_lt:
  assumes "z  0"
  shows "0 < Im(Ln z)  Im(Ln z) < pi  0 < Im(z)"
  using Im_Ln_pos_le [OF assms] assms
  by (smt (verit, best) Arg2pi_exp Arg2pi_lt_pi exp_Ln)

lemma Re_Ln_pos_lt_imp: "0 < Re(z)  ¦Im(Ln z)¦ < pi/2"
  by (metis Re_Ln_pos_lt less_irrefl zero_complex.simps(1))

lemma Im_Ln_pos_lt_imp: "0 < Im(z)  0 < Im(Ln z)  Im(Ln z) < pi"
  by (metis Im_Ln_pos_lt not_le order_refl zero_complex.simps(2))

text‹A reference to the set of positive real numbers›
lemma Im_Ln_eq_0: "z  0  (Im(Ln z) = 0  0 < Re(z)  Im(z) = 0)"
  using Im_Ln_pos_le Im_Ln_pos_lt Re_Ln_pos_lt by fastforce

lemma Im_Ln_eq_pi: "z  0  (Im(Ln z) = pi  Re(z) < 0  Im(z) = 0)"
  using Im_Ln_eq_0 Im_Ln_pos_le Im_Ln_pos_lt complex.expand by fastforce


subsectiontag unimportant›‹More Properties of Ln›

lemma cnj_Ln: assumes "z  0" shows "cnj(Ln z) = Ln(cnj z)"
proof (cases "z=0")
  case False
  show ?thesis
    by (smt (verit) False Im_Ln_less_pi Ln_exp assms cnj.sel(2) exp_Ln exp_cnj mpi_less_Im_Ln)
qed (use assms in auto)


lemma Ln_inverse: assumes "z  0" shows "Ln(inverse z) = -(Ln z)"
proof (cases "z=0")
  case False
  show ?thesis
    by (smt (verit) False Im_Ln_less_pi Ln_exp assms exp_Ln exp_minus mpi_less_Im_Ln uminus_complex.sel(2))
qed (use assms in auto)

lemma Ln_minus1 [simp]: "Ln(-1) = 𝗂 * pi"
proof (rule exp_complex_eqI)
  show "¦Im (Ln (- 1)) - Im (𝗂 * pi)¦ < 2 * pi"
    using Im_Ln_le_pi [of "-1"] mpi_less_Im_Ln [of "-1"] by auto
qed auto

lemma Ln_ii [simp]: "Ln 𝗂 = 𝗂 * of_real pi/2"
  using Ln_exp [of "𝗂 * (of_real pi/2)"]
  unfolding exp_Euler
  by simp

lemma Ln_minus_ii [simp]: "Ln(-𝗂) = - (𝗂 * pi/2)"
  using Ln_inverse by fastforce

lemma Ln_times:
  assumes "w  0" "z  0"
    shows "Ln(w * z) =
           (if Im(Ln w + Ln z)  -pi then (Ln(w) + Ln(z)) + 𝗂 * of_real(2*pi)
            else if Im(Ln w + Ln z) > pi then (Ln(w) + Ln(z)) - 𝗂 * of_real(2*pi)
            else Ln(w) + Ln(z))"
  using pi_ge_zero Im_Ln_le_pi [of w] Im_Ln_le_pi [of z]
  using assms mpi_less_Im_Ln [of w] mpi_less_Im_Ln [of z]
  by (auto simp: exp_add exp_diff sin_double cos_double exp_Euler intro!: Ln_unique)

corollarytag unimportant› Ln_times_simple:
    "w  0; z  0; -pi < Im(Ln w) + Im(Ln z); Im(Ln w) + Im(Ln z)  pi
          Ln(w * z) = Ln(w) + Ln(z)"
  by (simp add: Ln_times)

corollarytag unimportant› Ln_times_of_real:
    "r > 0; z  0  Ln(of_real r * z) = ln r + Ln(z)"
  using mpi_less_Im_Ln Im_Ln_le_pi
  by (force simp: Ln_times)

corollarytag unimportant› Ln_times_of_nat:
    "r > 0; z  0  Ln(of_nat r * z :: complex) = ln (of_nat r) + Ln(z)"
  using Ln_times_of_real[of "of_nat r" z] by simp

corollarytag unimportant› Ln_times_Reals:
    "r  Reals; Re r > 0; z  0  Ln(r * z) = ln (Re r) + Ln(z)"
  using Ln_Reals_eq Ln_times_of_real by fastforce

corollarytag unimportant› Ln_divide_of_real:
  "r > 0; z  0  Ln(z / of_real r) = Ln(z) - ln r"
  using Ln_times_of_real [of "inverse r" z]
  by (simp add: ln_inverse Ln_of_real mult.commute divide_inverse flip: of_real_inverse)

corollarytag unimportant› Ln_prod:
  fixes f :: "'a  complex"
  assumes "finite A" "x. x  A  f x  0"
  shows "n. Ln (prod f A) = (x  A. Ln (f x) + (of_int (n x) * (2 * pi)) * 𝗂)"
  using assms
proof (induction A)
  case (insert x A)
  then obtain n where n: "Ln (prod f A) = (xA. Ln (f x) + of_real (of_int (n x) * (2 * pi)) * 𝗂)"
    by auto
  define D where "D  Im (Ln (f x)) + Im (Ln (prod f A))"
  define q::int where "q  (if D  -pi then 1 else if D > pi then -1 else 0)"
  have "prod f A  0" "f x  0"
    by (auto simp: insert.hyps insert.prems)
  with insert.hyps pi_ge_zero show ?case
    by (rule_tac x="n(x:=q)" in exI) (force simp: Ln_times q_def D_def n intro!: sum.cong)
qed auto

lemma Ln_minus:
  assumes "z  0"
    shows "Ln(-z) = (if Im(z)  0  ¬(Re(z) < 0  Im(z) = 0)
                     then Ln(z) + 𝗂 * pi
                     else Ln(z) - 𝗂 * pi)" 
  using Im_Ln_le_pi [of z] mpi_less_Im_Ln [of z] assms
        Im_Ln_eq_pi [of z] Im_Ln_pos_lt [of z]
  by (intro Ln_unique) (auto simp: exp_add exp_diff)

lemma Ln_inverse_if:
  assumes "z  0"
    shows "Ln (inverse z) = (if z  0 then -(Ln z) + 𝗂 * 2 * complex_of_real pi else -(Ln z))"
proof (cases "z  0")
  case False then show ?thesis
    by (simp add: Ln_inverse)
next
  case True
  then have z: "Im z = 0" "Re z < 0" "- z  0"
    using assms complex_eq_iff complex_nonpos_Reals_iff by auto
  have "Ln(inverse z) = Ln(- (inverse (-z)))"
    by simp
  also have " = Ln (inverse (-z)) + 𝗂 * complex_of_real pi"
    using assms z by (simp add: Ln_minus divide_less_0_iff)
  also have " = - Ln (- z) + 𝗂 * complex_of_real pi"
    using z Ln_inverse by presburger
  also have " = - (Ln z) + 𝗂 * 2 * complex_of_real pi"
    using Ln_minus assms z by auto
  finally show ?thesis by (simp add: True)
qed

lemma Ln_times_ii:
  assumes "z  0"
    shows  "Ln(𝗂 * z) = (if 0  Re(z) | Im(z) < 0
                          then Ln(z) + 𝗂 * of_real pi/2
                          else Ln(z) - 𝗂 * of_real(3 * pi/2))"
  using Im_Ln_le_pi [of z] mpi_less_Im_Ln [of z] assms
        Im_Ln_eq_pi [of z] Im_Ln_pos_lt [of z] Re_Ln_pos_le [of z]
  by (simp add: Ln_times) auto

lemma Ln_of_nat [simp]: "0 < n  Ln (of_nat n) = of_real (ln (of_nat n))"
  by (metis Ln_of_real of_nat_0_less_iff of_real_of_nat_eq)

lemma Ln_of_nat_over_of_nat:
  assumes "m > 0" "n > 0"
  shows   "Ln (of_nat m / of_nat n) = of_real (ln (of_nat m) - ln (of_nat n))"
proof -
  have "of_nat m / of_nat n = (of_real (of_nat m / of_nat n) :: complex)" by simp
  also from assms have "Ln  = of_real (ln (of_nat m / of_nat n))"
    by (simp add: Ln_of_real[symmetric])
  also from assms have " = of_real (ln (of_nat m) - ln (of_nat n))"
    by (simp add: ln_div)
  finally show ?thesis .
qed

lemma norm_Ln_times_le:
  assumes "w  0" "z  0"
  shows  "cmod (Ln(w * z))  cmod (Ln(w) + Ln(z))"
proof (cases "- pi < Im(Ln w + Ln z)  Im(Ln w + Ln z)  pi")
  case True
  then show ?thesis
    by (simp add: Ln_times_simple assms)
next
  case False
  then show ?thesis
    by (smt (verit) Im_Ln_le_pi assms cmod_Im_le_iff exp_Ln exp_add ln_unique mpi_less_Im_Ln mult_eq_0_iff norm_exp_eq_Re)
qed

corollary norm_Ln_prod_le:
  fixes f :: "'a  complex"
  assumes "x. x  A  f x  0"
  shows "cmod (Ln (prod f A))  (x  A. cmod (Ln (f x)))"
  using assms
proof (induction A rule: infinite_finite_induct)
  case (insert x A)
  then show ?case
    by simp (smt (verit) norm_Ln_times_le norm_triangle_ineq prod_zero_iff)
qed auto

lemma norm_Ln_exp_le: "norm (Ln (exp z))  norm z"
  by (smt (verit) Im_Ln_le_pi Ln_exp Re_Ln cmod_Im_le_iff exp_not_eq_zero ln_exp mpi_less_Im_Ln norm_exp_eq_Re)

subsectiontag unimportant›‹Uniform convergence and products›

(* TODO: could be generalised perhaps, but then one would have to do without the ln *)
lemma uniformly_convergent_on_prod_aux:
  fixes f :: "nat  complex  complex"
  assumes norm_f: "n x. x  A  norm (f n x) < 1"
  assumes cont: "n. continuous_on A (f n)"
  assumes conv: "uniformly_convergent_on A (λN x. n<N. ln (1 + f n x))"
  assumes A: "compact A"
  shows "uniformly_convergent_on A (λN x. n<N. 1 + f n x)"
proof -
  from conv obtain S where S: "uniform_limit A (λN x. n<N. ln (1 + f n x)) S sequentially"
    by (auto simp: uniformly_convergent_on_def)
  have cont': "continuous_on A S"
  proof (rule uniform_limit_theorem[OF _ S])
    have "f n x + 1  0" if "x  A" for n x
    proof
      assume "f n x + 1  0"
      then obtain t where t: "t  0" "f n x = of_real (t - 1)"
        by (metis add_diff_cancel nonpos_Reals_cases of_real_1 of_real_diff)
      moreover have "norm   1"
        using t by (subst norm_of_real) auto
      ultimately show False
        using norm_f[of x n] that by auto
    qed
    thus "F n in sequentially. continuous_on A (λx. n<n. Ln (1 + f n x))"
      by (auto intro!: always_eventually continuous_intros cont simp: add_ac)
  qed auto

  define B where "B = {x + y |x y. x  S ` A  y  cball 0 1}"
  have "compact B"
    unfolding B_def by (intro compact_sums compact_continuous_image cont' A) auto

  have "uniformly_convergent_on A (λN x. exp ((n<N. ln (1 + f n x))))"
    using conv
  proof (rule uniformly_convergent_on_compose_uniformly_continuous_on)
    show "closed B"
      using compact B by (auto dest: compact_imp_closed)
    show "uniformly_continuous_on B exp"
      by (intro compact_uniformly_continuous continuous_intros compact B)

    have "eventually (λN. xA. dist (n<N. Ln (1 + f n x)) (S x) < 1) sequentially"
      using S unfolding uniform_limit_iff by simp
    thus "eventually (λN. xA. (n<N. Ln (1 + f n x))  B) sequentially"
    proof eventually_elim
      case (elim N)
      show "xA. (n<N. Ln (1 + f n x))  B"
      proof safe
        fix x assume x: "x  A"
        have "(n<N. Ln (1 + f n x)) = S x + ((n<N. Ln (1 + f n x)) - S x)"
          by auto
        moreover have "((n<N. Ln (1 + f n x)) - S x)  ball 0 1"
          using elim x by (auto simp: dist_norm norm_minus_commute)
        ultimately show "(n<N. Ln (1 + f n x))  B"
          unfolding B_def using x by fastforce
      qed
    qed
  qed
  also have "?this  uniformly_convergent_on A (λN x. n<N. 1 + f n x)"
  proof (intro uniformly_convergent_cong refl always_eventually allI ballI)
    fix N :: nat and x assume x: "x  A"
    have "exp (n<N. ln (1 + f n x)) = (n<N. exp (ln (1 + f n x)))"
      by (simp add: exp_sum)
    also have " = (n<N. 1 + f n x)"
      using norm_f[of x] x
      by (smt (verit, best) add.right_neutral add_diff_cancel exp_Ln norm_minus_commute norm_one prod.cong)
    finally show "exp (n<N. ln (1 + f n x)) = (n<N. 1 + f n x)" .
  qed
  finally show ?thesis .
qed

text ‹Theorem 17.6 by Bak and Newman, Complex Analysis [roughly]›
lemma uniformly_convergent_on_prod:
  fixes f :: "nat  complex  complex"
  assumes cont: "n. continuous_on A (f n)"
  assumes A: "compact A"
  assumes conv_sum: "uniformly_convergent_on A (λN x. n<N. norm (f n x))"
  shows   "uniformly_convergent_on A (λN x. n<N. 1 + f n x)"
proof -
  obtain M where M: "n x. n  M  x  A  norm (f n x) < 1 / 2"
  proof -
    from conv_sum have "uniformly_Cauchy_on A (λN x. n<N. norm (f n x))"
      using uniformly_convergent_Cauchy by blast
    moreover have "(1 / 2 :: real) > 0"
      by simp
    ultimately obtain M where M:
      "x m n. x  A  m  M  n  M  dist (k<m. norm (f k x)) (k<n. norm (f k x)) < 1 / 2"
      unfolding uniformly_Cauchy_on_def by fast
    show ?thesis
    proof (rule that[of M])
      fix n x assume nx: "n  M" "x  A"
      have "dist (k<Suc n. norm (f k x)) (k<n. norm (f k x)) < 1 / 2"
        by (rule M) (use nx in auto)
      also have "dist (k<Suc n. norm (f k x)) (k<n. norm (f k x)) = norm (f n x)"
        by (simp add: dist_norm)
      finally show "norm (f n x) < 1 / 2" .
    qed
  qed

  have conv: "uniformly_convergent_on A (λN x. n<N. ln (1 + f (n + M) x))"
  proof (rule uniformly_summable_comparison_test)
    show "norm (ln (1 + f (n + M) x))  2 * norm (f (n + M) x)" if "x  A" for n x
      by (rule norm_Ln_le) (use M[of "n + M" x] that in auto)
    have *: "filterlim (λn. n + M) at_top at_top"
      by (rule filterlim_add_const_nat_at_top)
    have "uniformly_convergent_on A (λN x. 2 * ((n<N+M. norm (f n x)) - (n<M. norm (f n x))))"
      by (intro uniformly_convergent_mult uniformly_convergent_minus
                uniformly_convergent_on_compose[OF conv_sum *] uniformly_convergent_on_const)
    also have "(λN x. 2 * ((n<N+M. norm (f n x)) - (n<M. norm (f n x)))) =
               (λN x. n<N. 2 * norm (f (n + M) x))" (is "?lhs = ?rhs")
    proof (intro ext)
      fix N x
      have "(n<N+M. norm (f n x)) - (n<M. norm (f n x)) = (n{..<N+M}-{..<M}. norm (f n x))"
        by (subst sum_diff) auto
      also have " = (n<N. norm (f (n + M) x))"
        by (intro sum.reindex_bij_witness[of _ "λn. n + M" "λn. n - M"]) auto
      finally show "?lhs N x = ?rhs N x"
        by (simp add: sum_distrib_left)
    qed
    finally show "uniformly_convergent_on A (λN x. n<N. 2 * cmod (f (n + M) x))" .
  qed

  have conv': "uniformly_convergent_on A (λN x. n<N. 1 + f (n + M) x)"
  proof (rule uniformly_convergent_on_prod_aux)
    show "norm (f (n + M) x) < 1" if "x  A" for n x
      using M[of "n + M" x] that by simp
  qed (use M assms conv in auto)

  then obtain S where S: "uniform_limit A (λN x. n<N. 1 + f (n + M) x) S sequentially"
    by (auto simp: uniformly_convergent_on_def)
  have cont':  "continuous_on A S"
    by (intro uniform_limit_theorem[OF _ S] always_eventually ballI allI continuous_intros cont) auto

  have "uniform_limit A (λN x. (n<M. 1 + f n x) * (n<N. 1 + f (n + M) x)) (λx. (n<M. 1 + f n x) * S x) sequentially"
  proof (rule uniform_lim_mult[OF uniform_limit_const S])
    show "bounded (S ` A)"
      by (intro compact_imp_bounded compact_continuous_image A cont')
    show "bounded ((λx. n<M. 1 + f n x) ` A)"
      by (intro compact_imp_bounded compact_continuous_image A continuous_intros cont)
  qed
  hence "uniformly_convergent_on A (λN x. (n<M. 1 + f n x) * (n<N. 1 + f (n + M) x))"
    by (auto simp: uniformly_convergent_on_def)
  also have "(λN x. (n<M. 1 + f n x) * (n<N. 1 + f (n + M) x)) = (λN x. (n<M+N. 1 + f n x))"
  proof (intro ext)
    fix N :: nat and x :: complex
    have "(n<N. 1 + f (n + M) x) = (n{M..<M+N}. 1 + f n x)"
      by (intro prod.reindex_bij_witness[of _ "λn. n - M" "λn. n + M"]) auto
    also have "(n<M. 1 + f n x) *  = (n{..<M}{M..<M+N}. 1 + f n x)"
      by (subst prod.union_disjoint) auto
    also have "{..<M}  {M..<M+N} = {..<M+N}"
      by auto
    finally show "(n<M. 1 + f n x) * (n<N. 1 + f (n + M) x) = (n<M+N. 1 + f n x)" .
  qed
  finally have "uniformly_convergent_on A (λN x. n<M + N. 1 + f n x)"
    by simp
  hence "uniformly_convergent_on A (λN x. n<M + (N - M). 1 + f n x)"
    by (rule uniformly_convergent_on_compose) (rule filterlim_minus_const_nat_at_top)
  also have "?this  ?thesis"
  proof (rule uniformly_convergent_cong)
    show "eventually (λx. yA. (n<M + (x - M). 1 + f n y) = (n<x. 1 + f n y)) at_top"
      using eventually_ge_at_top[of M] by eventually_elim auto
  qed auto
  finally show ?thesis .
qed

lemma uniformly_convergent_on_prod':
  fixes f :: "nat  complex  complex"
  assumes cont: "n. continuous_on A (f n)"
  assumes A: "compact A"
  assumes conv_sum: "uniformly_convergent_on A (λN x. n<N. norm (f n x - 1))"
  shows "uniformly_convergent_on A (λN x. n<N. f n x)"
proof -
  have "uniformly_convergent_on A (λN x. n<N. 1 + (f n x - 1))"
    by (rule uniformly_convergent_on_prod) (use assms in auto intro!: continuous_intros)
  thus ?thesis
    by simp
qed

text‹Prop 17.6 of Bak and Newman, Complex Analysis, p. 243. 
     Only this version is for the reals. Can the two proofs be consolidated?›
lemma uniformly_convergent_on_prod_real:
  fixes u :: "nat  real  real"
  assumes contu: "k. continuous_on K (u k)" 
     and uconv: "uniformly_convergent_on K (λn x. k<n. ¦u k x¦)"
     and K: "compact K"
   shows "uniformly_convergent_on K (λn x. k<n. 1 + u k x)"
proof -
  define f where "f  λk. complex_of_real  u k  Re"
  define L where "L  complex_of_real ` K"
  have "compact L"
    by (simp add: compact K L_def compact_continuous_image)
  have "Re ` complex_of_real ` X = X" for X
    by (auto simp: image_iff)
  with contu have contf: "k. continuous_on L (f k)"
    unfolding f_def L_def by (intro continuous_intros) auto
  obtain S where S: "ε. ε>0  F n in sequentially. xK. dist (k<n. ¦u k x¦) (S x) < ε"
    using uconv unfolding uniformly_convergent_on_def uniform_limit_iff by presburger 
  have "F n in sequentially. zL. dist (k<n. cmod (f k z)) ((of_real  S  Re) z) < ε" 
    if "ε>0" for ε
    using S [OF that] by eventually_elim (simp add: L_def f_def)
  then have uconvf: "uniformly_convergent_on L (λn z. k<n. norm (f k z))"
    unfolding uniformly_convergent_on_def uniform_limit_iff by blast
  obtain P where P: "ε. ε>0  F n in sequentially. zL. dist (k<n. 1 + f k z) (P z) < ε"
    using uniformly_convergent_on_prod [OF contf compact L uconvf]
    unfolding uniformly_convergent_on_def uniform_limit_iff by blast
  have §: "¦(k<n. 1 + u k x) - Re (P x)¦  cmod ((k<n. 1 + of_real (u k x)) - P x)" for n x
  proof -
    have "(kN. of_real (1 + u k x)) = (kN. 1 + of_real (u k x))" for N
      by force
    then show ?thesis
      by (metis Re_complex_of_real abs_Re_le_cmod minus_complex.sel(1) of_real_prod)
  qed
  have "F n in sequentially. xK. dist (k<n. 1 + u k x) ((Re  P  of_real) x) < ε" 
    if "ε>0" for ε
    using P [OF that] by eventually_elim (simp add: L_def f_def dist_norm le_less_trans [OF §])
  then show ?thesis
    unfolding uniformly_convergent_on_def uniform_limit_iff by blast 
qed


subsection‹The Argument of a Complex Number›

text‹Unlike in HOL Light, it's defined for the same interval as the complex logarithm: (-π,π]›.›

lemma Arg_eq_Im_Ln:
  assumes "z  0" shows "Arg z = Im (Ln z)"
proof (rule cis_Arg_unique)
  show "sgn z = cis (Im (Ln z))"
    by (metis assms exp_Ln exp_eq_polar nonzero_mult_div_cancel_left norm_eq_zero
              norm_exp_eq_Re of_real_eq_0_iff sgn_eq)
  show "- pi < Im (Ln z)"
    by (simp add: assms mpi_less_Im_Ln)
  show "Im (Ln z)  pi"
    by (simp add: Im_Ln_le_pi assms)
qed

text ‹The 1990s definition of argument coincides with the more recent one›
lemmatag important› Arg_def:
  shows "Arg z = (if z = 0 then 0 else Im (Ln z))"
  by (simp add: Arg_eq_Im_Ln Arg_zero)

lemma Arg_of_real [simp]: "Arg (of_real r) = (if r<0 then pi else 0)"
  by (simp add: Im_Ln_eq_pi Arg_def)

lemma mpi_less_Arg: "-pi < Arg z" and Arg_le_pi: "Arg z  pi"
  by (auto simp: Arg_def mpi_less_Im_Ln Im_Ln_le_pi)

lemma Arg_eq: 
  assumes "z  0"
  shows "z = of_real(norm z) * exp(𝗂 * Arg z)"
  using cis_conv_exp rcis_cmod_Arg rcis_def by force

lemma is_Arg_Arg: "z  0  is_Arg z (Arg z)"
  by (simp add: Arg_eq is_Arg_def)

lemma Argument_exists:
  assumes "z  0" and R: "R = {r-pi<..r+pi}"
  obtains s where "is_Arg z s" "sR"
proof -
  let ?rp = "r - Arg z + pi"
  define k where "k  ?rp / (2 * pi)"
  have "(Arg z + of_int k * (2 * pi))  R"
    using floor_divide_lower [of "2*pi" ?rp] floor_divide_upper [of "2*pi" ?rp]
    by (auto simp: k_def algebra_simps R)
  then show ?thesis
    using Arg_eq z  0 is_Arg_2pi_iff is_Arg_def that by blast
qed

lemma Argument_exists_unique:
  assumes "z  0" and R: "R = {r-pi<..r+pi}"
  obtains s where "is_Arg z s" "sR" "t. is_Arg z t; tR  s=t"
proof -
  obtain s where s: "is_Arg z s" "sR"
    using Argument_exists [OF assms] .
  moreover have "t. is_Arg z t; tR  s=t"
    using assms s  by (auto simp: is_Arg_eqI)
  ultimately show thesis
    using that by blast
qed

lemma Argument_Ex1:
  assumes "z  0" and R: "R = {r-pi<..r+pi}"
  shows "∃!s. is_Arg z s  s  R"
  using Argument_exists_unique [OF assms]  by metis

lemma Arg_divide:
  assumes "w  0" "z  0"
  shows "is_Arg (z / w) (Arg z - Arg w)"
  using Arg_eq [of z] Arg_eq [of w] Arg_eq [of "norm(z / w)"] assms
  by (auto simp: is_Arg_def norm_divide field_simps exp_diff Arg_of_real)

lemma Arg_unique_lemma:
  assumes "is_Arg z t" "is_Arg z t'"
      and "- pi < t"  "t  pi"
      and "- pi < t'" "t'  pi"
      and "z  0"
    shows "t' = t"
  using is_Arg_eqI assms by force

lemma complex_norm_eq_1_exp_eq: "norm z = 1  exp(𝗂 * (Arg z)) = z"
  by (metis Arg2pi_eq Arg_eq complex_norm_eq_1_exp norm_eq_zero norm_exp_i_times)

lemma Arg_unique: "of_real r * exp(𝗂 * a) = z; 0 < r; -pi < a; a  pi  Arg z = a"
  by (rule Arg_unique_lemma [unfolded is_Arg_def, OF _ Arg_eq])
     (use mpi_less_Arg Arg_le_pi in auto simp: norm_mult)

lemma Arg_minus:
  assumes "z  0"
  shows "Arg (-z) = (if Arg z  0 then Arg z + pi else Arg z - pi)"
proof -
  have [simp]: "cmod z * cos (Arg z) = Re z"
    using assms Arg_eq [of z] by (metis Re_exp exp_Ln norm_exp_eq_Re Arg_def)
  have [simp]: "cmod z * sin (Arg z) = Im z"
    using assms Arg_eq [of z] by (metis Im_exp exp_Ln norm_exp_eq_Re Arg_def)
  show ?thesis
    using mpi_less_Arg [of z] Arg_le_pi [of z] assms
    by (intro Arg_unique [of "norm z", OF complex_eqI]) (auto simp: Re_exp Im_exp)
qed

lemma Arg_1 [simp]: "Arg 1 = 0"
  by (rule Arg_unique[of 1]) auto

lemma Arg_numeral [simp]: "Arg (numeral n) = 0"
  by (rule Arg_unique[of "numeral n"]) auto
  
lemma Arg_times_of_real [simp]:
  assumes "0 < r" shows "Arg (of_real r * z) = Arg z"
  using Arg_def Ln_times_of_real assms by auto

lemma Arg_times_of_real2 [simp]: "0 < r  Arg (z * of_real r) = Arg z"
  by (metis Arg_times_of_real mult.commute)

lemma Arg_divide_of_real [simp]: "0 < r  Arg (z / of_real r) = Arg z"
  by (metis Arg_times_of_real2 less_irrefl nonzero_eq_divide_eq of_real_eq_0_iff)

lemma Arg_less_0: "0  Arg z  0  Im z"
  using Im_Ln_le_pi Im_Ln_pos_le
  by (simp add: Arg_def)

text ‹converse fails because the argument can equal $\pi$.› 
lemma Arg_uminus: "Arg z < 0  Arg (-z) > 0"
  by (smt (verit) Arg_bounded Arg_minus Complex.Arg_def)

lemma Arg_eq_pi: "Arg z = pi  Re z < 0  Im z = 0"
  by (auto simp: Arg_def Im_Ln_eq_pi)

lemma Arg_lt_pi: "0 < Arg z  Arg z < pi  0 < Im z"
  using Arg_less_0 [of z] Im_Ln_pos_lt
  by (auto simp: order.order_iff_strict Arg_def)

lemma Arg_eq_0: "Arg z = 0  z    0  Re z"
  using Arg_def Im_Ln_eq_0 complex_eq_iff complex_is_Real_iff by auto

corollarytag unimportant› Arg_ne_0: assumes "z  0" shows "Arg z  0"
  using assms by (auto simp: nonneg_Reals_def Arg_eq_0)

lemma Arg_eq_pi_iff: "Arg z = pi  z    Re z < 0"
  using Arg_eq_pi complex_is_Real_iff by blast

lemma Arg_eq_0_pi: "Arg z = 0  Arg z = pi  z  "
  using Arg_eq_pi_iff Arg_eq_0 by force

lemma Arg_real: "z    Arg z = (if 0  Re z then 0 else pi)"
  using Arg_eq_0 Arg_eq_0_pi by auto

lemma Arg_inverse: "Arg(inverse z) = (if z   then Arg z else - Arg z)"
proof (cases "z  ")
  case False
  then show ?thesis
    by (simp add: Arg_def Ln_inverse complex_is_Real_iff complex_nonpos_Reals_iff)
qed (use Arg_real Re_inverse in auto)

lemma Arg_eq_iff:
  assumes "w  0" "z  0"
  shows "Arg w = Arg z  (x. 0 < x  w = of_real x * z)" (is "?lhs = ?rhs")
proof
  assume ?lhs
  then have "w = (cmod w / cmod z) * z"
    by (metis Arg_eq assms divide_divide_eq_right eq_divide_eq exp_not_eq_zero of_real_divide)
  then show ?rhs
    using assms divide_pos_pos zero_less_norm_iff by blast
qed auto

lemma Arg_inverse_eq_0: "Arg(inverse z) = 0  Arg z = 0"
  by (metis Arg_eq_0 Arg_inverse inverse_inverse_eq)

lemma Arg_cnj_eq_inverse: "z0  Arg (cnj z) = Arg (inverse z)"
  using Arg2pi_cnj_eq_inverse Arg2pi_eq_iff Arg_eq_iff by auto

lemma Arg_cnj: "Arg(cnj z) = (if z   then Arg z else - Arg z)"
  by (metis Arg_cnj_eq_inverse Arg_inverse Reals_0 complex_cnj_zero)

lemma Arg_exp: "-pi < Im z  Im z  pi  Arg(exp z) = Im z"
  by (simp add: Arg_eq_Im_Ln)

lemma Arg_cis: "x  {-pi<..pi}  Arg (cis x) = x"
  unfolding cis_conv_exp by (subst Arg_exp) auto

lemma Arg_rcis: "x  {-pi<..pi}  r > 0  Arg (rcis r x) = x"
  unfolding rcis_def by (subst Arg_times_of_real) (auto simp: Arg_cis)

lemma Ln_Arg: "z0  Ln(z) = ln(norm z) + 𝗂 * Arg(z)"
  by (metis Arg_def Re_Ln complex_eq)

lemma continuous_at_Arg:
  assumes "z  0"
    shows "continuous (at z) Arg"
proof -
  have "(λz. Im (Ln z)) z Arg z"
    using Arg_def assms continuous_at by fastforce
  then show ?thesis
    unfolding continuous_at
    by (smt (verit, del_insts) Arg_eq_Im_Ln Lim_transform_away_at assms nonpos_Reals_zero_I)
qed

lemma continuous_within_Arg: "z  0  continuous (at z within S) Arg"
  using continuous_at_Arg continuous_at_imp_continuous_within by blast

lemma Arg_Re_pos: "¦Arg z¦ < pi / 2  Re z > 0  z = 0"
  using Arg_def Re_Ln_pos_lt by auto

lemma Arg_Re_nonneg: "¦Arg z¦  pi / 2  Re z  0"
  using Re_Ln_pos_le[of z] by (cases "z = 0") (auto simp: Arg_eq_Im_Ln Arg_zero)

lemma Arg_times:
  assumes "Arg z + Arg w  {-pi<..pi}" "z  0" "w  0"
  shows   "Arg (z * w) = Arg z + Arg w"
  using Arg_eq_Im_Ln Ln_times_simple assms by auto
  
subsection‹The Unwinding Number and the Ln product Formula›

text‹Note that in this special case the unwinding number is -1, 0 or 1. But it's always an integer.›

lemma is_Arg_exp_Im: "is_Arg (exp z) (Im z)"
  using exp_eq_polar is_Arg_def norm_exp_eq_Re by auto

lemma is_Arg_exp_diff_2pi:
  assumes "is_Arg (exp z) θ"
  shows "k. Im z - of_int k * (2 * pi) = θ"
proof (intro exI is_Arg_eqI)
  let ?k = "(Im z - θ) / (2 * pi)"
  show "is_Arg (exp z) (Im z - real_of_int ?k * (2 * pi))"
    by (metis diff_add_cancel is_Arg_2pi_iff is_Arg_exp_Im)
  show "¦Im z - real_of_int ?k * (2 * pi) - θ¦ < 2 * pi"
    using floor_divide_upper [of "2*pi" "Im z - θ"] floor_divide_lower [of "2*pi" "Im z - θ"]
    by (auto simp: algebra_simps abs_if)
qed (auto simp: is_Arg_exp_Im assms)

lemma Arg_exp_diff_2pi: "k. Im z - of_int k * (2 * pi) = Arg (exp z)"
  using is_Arg_exp_diff_2pi [OF is_Arg_Arg] by auto

lemma unwinding_in_Ints: "(z - Ln(exp z)) / (of_real(2*pi) * 𝗂)  "
  using Arg_exp_diff_2pi [of z]
  by (force simp: Ints_def image_def field_simps Arg_def intro!: complex_eqI)

definitiontag important› unwinding :: "complex  int" where
   "unwinding z  THE k. of_int k = (z - Ln(exp z)) / (of_real(2*pi) * 𝗂)"

lemma unwinding: "of_int (unwinding z) = (z - Ln(exp z)) / (of_real(2*pi) * 𝗂)"
  using unwinding_in_Ints [of z]
  unfolding unwinding_def Ints_def by force

lemma unwinding_2pi: "(2*pi) * 𝗂 * unwinding(z) = z - Ln(exp z)"
  by (simp add: unwinding)

lemma Ln_times_unwinding:
    "w  0  z  0  Ln(w * z) = Ln(w) + Ln(z) - (2*pi) * 𝗂 * unwinding(Ln w + Ln z)"
  using unwinding_2pi by (simp add: exp_add)


lemma arg_conv_arctan:
  assumes "Re z > 0"
  shows   "Arg z = arctan (Im z / Re z)"
proof (rule cis_Arg_unique)
  show "sgn z = cis (arctan (Im z / Re z))"
  proof (rule complex_eqI)
    have "Re (cis (arctan (Im z / Re z))) = 1 / sqrt (1 + (Im z)2 / (Re z)2)"
      by (simp add: cos_arctan power_divide)
    also have "1 + Im z ^ 2 / Re z ^ 2 = norm z ^ 2 / Re z ^ 2"
      using assms by (simp add: cmod_def field_simps)
    also have "1 / sqrt  = Re z / norm z"
      using assms by (simp add: real_sqrt_divide)
    finally show "Re (sgn z) = Re (cis (arctan (Im z / Re z)))"
      by simp
  next
    have "Im (cis (arctan (Im z / Re z))) = Im z / (Re z * sqrt (1 + (Im z)2 / (Re z)2))"
      by (simp add: sin_arctan field_simps)
    also have "1 + Im z ^ 2 / Re z ^ 2 = norm z ^ 2 / Re z ^ 2"
      using assms by (simp add: cmod_def field_simps)
    also have "Im z / (Re z * sqrt ) = Im z / norm z"
      using assms by (simp add: real_sqrt_divide)
    finally show "Im (sgn z) = Im (cis (arctan (Im z / Re z)))"
      by simp
  qed
next
  show "arctan (Im z / Re z) > -pi"
    by (smt (verit, ccfv_SIG) arctan field_sum_of_halves)
next
 show "arctan (Im z / Re z)  pi"
   by (smt (verit, best) arctan field_sum_of_halves)
qed


subsection ‹Characterisation of @{term "Im (Ln z)"} (Wenda Li)›

lemma Im_Ln_eq_pi_half:
    "z  0  (Im(Ln z) = pi/2  0 < Im(z)  Re(z) = 0)"
    "z  0  (Im(Ln z) = -pi/2  Im(z) < 0  Re(z) = 0)"
  using Im_Ln_pos_lt Im_Ln_pos_le Re_Ln_pos_le Re_Ln_pos_lt pi_ge_two by fastforce+

lemma Im_Ln_eq:
  assumes "z0"
  shows "Im (Ln z) = (if Re z0 then
                        if Re z>0 then
                           arctan (Im z/Re z)
                        else if Im z0 then
                           arctan (Im z/Re z) + pi
                        else
                           arctan (Im z/Re z) - pi
                      else
                        if Im z>0 then pi/2 else -pi/2)"
proof -
  have eq_arctan_pos: "Im (Ln z) = arctan (Im z/Re z)" when "Re z>0" for z
    by (metis Arg_eq_Im_Ln arg_conv_arctan order_less_irrefl that zero_complex.simps(1))
  have ?thesis when "Re z=0"
    using Im_Ln_eq_pi_half[OF z0] that
    using assms complex_eq_iff by auto
  moreover have ?thesis when "Re z>0"
    using eq_arctan_pos[OF that] that by auto
  moreover have ?thesis when "Re z<0" "Im z0"
  proof -
    have "Im (Ln (- z)) = arctan (Im (- z) / Re (- z))"
      by (simp add: eq_arctan_pos that(1))
    moreover have "Ln (- z) = Ln z - 𝗂 * complex_of_real pi"
      using Ln_minus assms that by fastforce
    ultimately show ?thesis using that by auto
  qed
  moreover have ?thesis when "Re z<0" "Im z<0"
  proof -
    have "Im (Ln (- z)) = arctan (Im (- z) / Re (- z))"
      by (simp add: eq_arctan_pos that(1))
    moreover have "Ln (- z) = Ln z + 𝗂 * complex_of_real pi"
      using Ln_minus assms that by auto
    ultimately show ?thesis using that by auto
  qed
  ultimately show ?thesis by linarith
qed

subsectiontag unimportant›‹Relation between Ln and Arg2pi, and hence continuity of Arg2pi›

lemma Arg2pi_Ln: "0 < Arg2pi z  Arg2pi z = Im(Ln(-z)) + pi"
  by (smt (verit, best) Arg2pi_0 Arg2pi_exp Arg2pi_minus Arg_exp Arg_minus Im_Ln_le_pi 
      exp_Ln mpi_less_Im_Ln neg_equal_0_iff_equal)

lemma continuous_at_Arg2pi:
  assumes "z  0"
    shows "continuous (at z) Arg2pi"
proof -
  have "isCont (λz. Im (Ln (- z)) + pi) z"
    by (rule Complex.isCont_Im isCont_Ln' continuous_intros | simp add: assms complex_is_Real_iff)+
  moreover consider "Re z < 0" | "Im z  0" using assms
    using complex_nonneg_Reals_iff not_le by blast
  ultimately have "(λz. Im (Ln (- z)) + pi) z Arg2pi z"
    by (simp add: Arg2pi_Ln Arg2pi_gt_0 assms continuous_within)
  then show ?thesis
    unfolding continuous_at
    by (metis (mono_tags, lifting) Arg2pi_Ln Arg2pi_gt_0 Compl_iff Lim_transform_within_open assms 
        closed_nonneg_Reals_complex open_Compl)
qed


text‹Relation between Arg2pi and arctangent in upper halfplane›
lemma Arg2pi_arctan_upperhalf:
  assumes "0 < Im z"
    shows "Arg2pi z = pi/2 - arctan(Re z / Im z)"
proof (cases "z = 0")
  case False
  show ?thesis
  proof (rule Arg2pi_unique [of "norm z"])
    show "(cmod z) * exp (𝗂 * (pi / 2 - arctan (Re z / Im z))) = z"
      apply (rule complex_eqI)
      using assms norm_complex_def [of z, symmetric]
      unfolding exp_Euler cos_diff sin_diff sin_of_real cos_of_real
      by (simp_all add: field_simps real_sqrt_divide sin_arctan cos_arctan)
  qed (use False arctan [of "Re z / Im z"] in auto)
qed (use assms in auto)

lemma Arg2pi_eq_Im_Ln:
  assumes "0  Im z" "0 < Re z"
    shows "Arg2pi z = Im (Ln z)"
  by (smt (verit, ccfv_SIG) Arg2pi_exp Im_Ln_pos_le assms exp_Ln pi_neq_zero zero_complex.simps(1))

lemma continuous_within_upperhalf_Arg2pi:
  assumes "z  0"
    shows "continuous (at z within {z. 0  Im z}) Arg2pi"
proof (cases "z  0")
  case False then show ?thesis
    using continuous_at_Arg2pi continuous_at_imp_continuous_within by auto
next
  case True
  then have z: "z  " "0 < Re z"
    using assms  by (auto simp: complex_nonneg_Reals_iff complex_is_Real_iff complex_neq_0)
  then have [simp]: "Arg2pi z = 0" "Im (Ln z) = 0"
    by (auto simp: Arg2pi_eq_0 Im_Ln_eq_0 assms complex_is_Real_iff)
  show ?thesis
  proof (clarsimp simp add: continuous_within Lim_within dist_norm)
    fix e::real
    assume "0 < e"
    moreover have "continuous (at z) (λx. Im (Ln x))"
      using z by (simp add: continuous_at_Ln complex_nonpos_Reals_iff)
    ultimately
    obtain d where d: "d>0" "x. x  z  cmod (x - z) < d  ¦Im (Ln x)¦ < e"
      by (auto simp: continuous_within Lim_within dist_norm)
    { fix x
      assume "cmod (x - z) < Re z / 2"
      then have "¦Re x - Re z¦ < Re z / 2"
        by (metis le_less_trans abs_Re_le_cmod minus_complex.simps(1))
      then have "0 < Re x"
        using z by linarith
    }
    then show "d>0. x. 0  Im x  x  z  cmod (x - z) < d  ¦Arg2pi x¦ < e"
      apply (rule_tac x="min d (Re z / 2)" in exI)
      using z d by (auto simp: Arg2pi_eq_Im_Ln)
  qed
qed

lemma continuous_on_upperhalf_Arg2pi: "continuous_on ({z. 0  Im z} - {0}) Arg2pi"
  unfolding continuous_on_eq_continuous_within
  by (metis DiffE Diff_subset continuous_within_subset continuous_within_upperhalf_Arg2pi insertCI)

lemma open_Arg2pi2pi_less_Int:
  assumes "0  s" "t  2*pi"
    shows "open ({y. s < Arg2pi y}  {y. Arg2pi y < t})"
proof -
  have 1: "continuous_on (UNIV - 0) Arg2pi"
    using continuous_at_Arg2pi continuous_at_imp_continuous_within
    by (auto simp: continuous_on_eq_continuous_within)
  have 2: "open (UNIV - 0 :: complex set)"  by (simp add: open_Diff)
  have "open ({z. s < z}  {z. z < t})"
    using open_lessThan [of t] open_greaterThan [of s]
    by (metis greaterThan_def lessThan_def open_Int)
  moreover have "{y. s < Arg2pi y}  {y. Arg2pi y < t}  - 0"
    using assms by (auto simp: Arg2pi_real complex_nonneg_Reals_iff complex_is_Real_iff)
  ultimately show ?thesis
    using continuous_imp_open_vimage [OF 1 2, of  "{z. Re z > s}  {z. Re z < t}"]
    by auto
qed

lemma open_Arg2pi2pi_gt: "open {z. t < Arg2pi z}"
proof (cases "t < 0")
  case True then have "{z. t < Arg2pi z} = UNIV"
    using Arg2pi_ge_0 less_le_trans by auto
  then show ?thesis
    by simp
next
  case False then show ?thesis
    using open_Arg2pi2pi_less_Int [of t "2*pi"] Arg2pi_lt_2pi
    by auto
qed

lemma closed_Arg2pi2pi_le: "closed {z. Arg2pi z  t}"
  using open_Arg2pi2pi_gt [of t]
  by (simp add: closed_def Set.Collect_neg_eq [symmetric] not_le)

subsectiontag unimportant›‹Complex Powers›

lemma powr_to_1 [simp]: "z powr 1 = (z::complex)"
  by (simp add: powr_def)

lemma powr_nat:
  fixes n::nat and z::complex shows "z powr n = (if z = 0 then 0 else z^n)"
  by (simp add: exp_of_nat_mult powr_def)

lemma powr_nat': "(z :: complex)  0  n  0  z powr of_nat n = z ^ n"
  by (cases "z = 0") (auto simp: powr_nat)
    
lemma norm_powr_real: "w    0 < Re w  norm(w powr z) = exp(Re z * ln(Re w))"
  using Ln_Reals_eq norm_exp_eq_Re by (auto simp: Im_Ln_eq_0 powr_def norm_complex_def)

lemma norm_powr_real_powr': "w    norm (z powr w) = norm z powr Re w"
  by (auto simp: powr_def Reals_def)

lemma powr_complexpow [simp]:
  fixes x::complex shows "x  0  x powr (of_nat n) = x^n"
  by (simp add: powr_nat')

lemma powr_complexnumeral [simp]:
  fixes x::complex shows "x powr (numeral n) = x ^ (numeral n)"
  by (metis of_nat_numeral power_zero_numeral powr_nat)

lemma cnj_powr:
  assumes "Im a = 0  Re a  0"
  shows   "cnj (a powr b) = cnj a powr cnj b"
proof (cases "a = 0")
  case False
  with assms have "a  0" by (auto simp: complex_eq_iff complex_nonpos_Reals_iff)
  with False show ?thesis by (simp add: powr_def exp_cnj cnj_Ln)
qed simp

lemma powr_real_real:
  assumes "w  " "z  " "0 < Re w"
  shows "w powr z = exp(Re z * ln(Re w))"
proof -
  have "w  0"
    using assms by auto
  with assms show ?thesis
    by (simp add: powr_def Ln_Reals_eq of_real_exp)
qed

lemma powr_of_real:
  fixes x::real and y::real
  shows "0  x  of_real x powr (of_real y::complex) = of_real (x powr y)"
  by (simp_all add: powr_def exp_eq_polar)

lemma powr_of_int:
  fixes z::complex and n::int
  assumes "z(0::complex)"
  shows "z powr of_int n = (if n0 then z^nat n else inverse (z^nat (-n)))"
  by (metis assms not_le of_int_of_nat powr_complexpow powr_minus)

lemma complex_powr_of_int: "z  0  n  0  z powr of_int n = (z :: complex) powi n"
  by (cases "z = 0  n = 0")
     (auto simp: power_int_def powr_minus powr_nat powr_of_int power_0_left power_inverse)
  
lemma powr_Reals_eq: "x  ; y  ; Re x  0  x powr y = of_real (Re x powr Re y)"
  by (metis of_real_Re powr_of_real)

lemma norm_powr_real_mono:
    "w  ; 1 < Re w  cmod(w powr z1)  cmod(w powr z2)  Re z1  Re z2"
  by (auto simp: powr_def algebra_simps Reals_def Ln_of_real)

lemma powr_times_real:
    "x  ; y  ; 0  Re x; 0  Re y  (x * y) powr z = x powr z * y powr z"
  by (auto simp: Reals_def powr_def Ln_times exp_add algebra_simps less_eq_real_def Ln_of_real)

lemma Re_powr_le: "r  0  Re (r powr z)  Re r powr Re z"
  by (auto simp: powr_def nonneg_Reals_def order_trans [OF complex_Re_le_cmod])

lemma
  fixes w::complex
  assumes "w  0" "z  "
  shows Reals_powr [simp]: "w powr z  " and nonneg_Reals_powr [simp]: "w powr z  0"
  using assms by (auto simp: nonneg_Reals_def Reals_def powr_of_real)

lemma exp_powr_complex:
  fixes x::complex 
  assumes "-pi < Im(x)" "Im(x)  pi"
  shows "exp x powr y = exp (x*y)"
  using assms by (simp add: powr_def mult.commute)

lemma powr_neg_real_complex:
  fixes w::complex
  shows "(- of_real x) powr w = (-1) powr (of_real (sgn x) * w) * of_real x powr w"
proof (cases "x = 0")
  assume x: "x  0"
  hence "(-x) powr w = exp (w * ln (-of_real x))" by (simp add: powr_def)
  also from x have "ln (-of_real x) = Ln (of_real x) + of_real (sgn x) * pi * 𝗂"
    by (simp add: Ln_minus Ln_of_real)
  also from x have "exp (w * ) = cis pi powr (of_real (sgn x) * w) * of_real x powr w"
    by (simp add: powr_def exp_add algebra_simps Ln_of_real cis_conv_exp)
  also note cis_pi
  finally show ?thesis by simp
qed simp_all

lemma has_field_derivative_powr:
  fixes z :: complex
  assumes "z  0"
  shows "((λz. z powr s) has_field_derivative (s * z powr (s - 1))) (at z)"
proof (cases "z=0")
  case False
  then have §: "exp (s * Ln z) * inverse z = exp ((s - 1) * Ln z)"
    by (simp add: divide_complex_def exp_diff left_diff_distrib')
  show ?thesis
    unfolding powr_def
  proof (rule has_field_derivative_transform_within)
    show "((λz. exp (s * Ln z)) has_field_derivative s * (if z = 0 then 0 else exp ((s - 1) * Ln z)))
           (at z)"
      by (intro derivative_eq_intros | simp add: assms False §)+
  qed (use False in auto)
qed (use assms in auto)

declare has_field_derivative_powr[THEN DERIV_chain2, derivative_intros]

(*Seemingly impossible to use DERIV_power_int without introducing the assumption z∈S*)
lemma has_field_derivative_powr_of_int:
  fixes z :: complex
  assumes gderiv: "(g has_field_derivative gd) (at z within S)" and "g z  0"
  shows "((λz. g z powr of_int n) has_field_derivative (n * g z powr (of_int n - 1) * gd)) (at z within S)"
proof -
  obtain e where "e>0" and e_dist: "yS. dist z y < e  g y  0"
    using DERIV_continuous assms continuous_within_avoid gderiv by blast
  define D where "D = of_int n * g z powr (of_int (n - 1)) * gd"
  define E where "E = of_int n * g z powi (n - 1) * gd"
  have "((λz. g z powr of_int n) has_field_derivative D) (at z within S)
     ((λz. g z powr of_int n) has_field_derivative E) (at z within S)"
    using assms complex_powr_of_int D_def E_def by presburger
  also have "  ((λz. g z powi n) has_field_derivative E) (at z within S)"
  proof (rule has_field_derivative_cong_eventually)
    show "F x in at z within S. g x powr of_int n = g x powi n"
      unfolding eventually_at by (metis 0 < e complex_powr_of_int dist_commute e_dist)
  qed (simp add: assms complex_powr_of_int)
  also have "((λz. g z powi n) has_field_derivative E) (at z within S)"
    unfolding E_def using gderiv assms by (auto intro!: derivative_eq_intros)
  finally show ?thesis
    by (simp add: D_def) 
qed

lemma field_differentiable_powr_of_int:
  fixes z :: complex
  assumes "g field_differentiable (at z within S)" and "g z  0"
  shows "(λz. g z powr of_int n) field_differentiable (at z within S)"
  using has_field_derivative_powr_of_int assms field_differentiable_def by blast

lemma holomorphic_on_powr_of_int [holomorphic_intros]:
  assumes "f holomorphic_on S" and "z. zS  f z  0"
  shows "(λz. (f z) powr of_int n) holomorphic_on S"
  using assms field_differentiable_powr_of_int holomorphic_on_def by auto

lemma has_field_derivative_powr_right [derivative_intros]:
    "w  0  ((λz. w powr z) has_field_derivative Ln w * w powr z) (at z)"
  unfolding powr_def by (intro derivative_eq_intros | simp)+

lemma field_differentiable_powr_right [derivative_intros]:
  fixes w::complex
  shows "w  0  (λz. w powr z) field_differentiable (at z)"
using field_differentiable_def has_field_derivative_powr_right by blast

lemma holomorphic_on_powr_right [holomorphic_intros]:
  assumes "f holomorphic_on S"
  shows "(λz. w powr (f z)) holomorphic_on S"
proof (cases "w = 0")
  case False
  with assms show ?thesis
    unfolding holomorphic_on_def field_differentiable_def
    by (metis (full_types) DERIV_chain' has_field_derivative_powr_right)
qed simp

lemma holomorphic_on_divide_gen [holomorphic_intros]:
  assumes "f holomorphic_on S" "g holomorphic_on S" and "z z'. z  S; z'  S  g z = 0  g z' = 0"
  shows "(λz. f z / g z) holomorphic_on S"
  by (metis (no_types, lifting) assms division_ring_divide_zero holomorphic_on_divide holomorphic_transform)

lemma norm_powr_real_powr:
  "w    0  Re w  cmod (w powr z) = Re w powr Re z"
  by (metis dual_order.order_iff_strict norm_powr_real norm_zero of_real_0 of_real_Re powr_def)

lemma tendsto_powr_complex:
  fixes f g :: "_  complex"
  assumes a: "a  0"
  assumes f: "(f  a) F" and g: "(g  b) F"
  shows   "((λz. f z powr g z)  a powr b) F"
proof -
  from a have [simp]: "a  0" by auto
  from f g a have "((λz. exp (g z * ln (f z)))  a powr b) F" (is ?P)
    by (auto intro!: tendsto_intros simp: powr_def)
  also {
    have "eventually (λz. z  0) (nhds a)"
      by (intro t1_space_nhds) simp_all
    with f have "eventually (λz. f z  0) F" using filterlim_iff by blast
  }
  hence "?P  ((λz. f z powr g z)  a powr b) F"
    by (intro tendsto_cong refl) (simp_all add: powr_def mult_ac)
  finally show ?thesis .
qed

lemma tendsto_powr_complex_0:
  fixes f g :: "'a  complex"
  assumes f: "(f  0) F" and g: "(g  b) F" and b: "Re b > 0"
  shows   "((λz. f z powr g z)  0) F"
proof (rule tendsto_norm_zero_cancel)
  define h where
    "h = (λz. if f z = 0 then 0 else exp (Re (g z) * ln (cmod (f z)) + abs (Im (g z)) * pi))"
  {
    fix z :: 'a assume z: "f z  0" 
    define c where "c = abs (Im (g z)) * pi"
    from mpi_less_Im_Ln[OF z] Im_Ln_le_pi[OF z]
      have "abs (Im (Ln (f z)))  pi" by simp
    from mult_left_mono[OF this, of "abs (Im (g z))"]
      have "abs (Im (g z) * Im (ln (f z)))  c" by (simp add: abs_mult c_def)
    hence "-Im (g z) * Im (ln (f z))  c" by simp
    hence "norm (f z powr g z)  h z" by (simp add: powr_def field_simps h_def c_def)
  }
  hence le: "norm (f z powr g z)  h z" for z
    by (simp add: h_def) 

  have g': "(g  b) (inf F (principal {z. f z  0}))"
    by (rule tendsto_mono[OF _ g]) simp_all
  have "((λx. norm (f x))  0) (inf F (principal {z. f z  0}))"
    by (subst tendsto_norm_zero_iff, rule tendsto_mono[OF _ f]) simp_all
  moreover {
    have "filterlim (λx. norm (f x)) (principal {0<..}) (principal {z. f z  0})"
      by (auto simp: filterlim_def)
    hence "filterlim (λx. norm (f x)) (principal {0<..}) (inf F (principal {z. f z  0}))"
      by (rule filterlim_mono) simp_all
  }
  ultimately have norm: "filterlim (λx. norm (f x)) (at_right 0) (inf F (principal {z. f z  0}))"
    by (simp add: filterlim_inf at_within_def)

  have A: "LIM x inf F (principal {z. f z  0}). Re (g x) * -ln (cmod (f x)) :> at_top"
    by (rule filterlim_tendsto_pos_mult_at_top tendsto_intros g' b
          filterlim_compose[OF filterlim_uminus_at_top_at_bot] filterlim_compose[OF ln_at_0] norm)+
  have B: "LIM x inf F (principal {z. f z  0}).
          -¦Im (g x)¦ * pi + -(Re (g x) * ln (cmod (f x))) :> at_top"
    by (rule filterlim_tendsto_add_at_top tendsto_intros g')+ (insert A, simp_all)
  have C: "(h  0) F" unfolding h_def
    by (intro filterlim_If tendsto_const filterlim_compose[OF exp_at_bot])
       (insert B, auto simp: filterlim_uminus_at_bot algebra_simps)
  show "((λx. norm (f x powr g x))  0) F"
    by (rule Lim_null_comparison[OF always_eventually C]) (insert le, auto)
qed

lemma tendsto_powr_complex' [tendsto_intros]:
  fixes f g :: "_  complex"
  assumes "a  0  (a = 0  Re b > 0)" and "(f  a) F" "(g  b) F"
  shows   "((λz. f z powr g z)  a powr b) F"
  using assms tendsto_powr_complex tendsto_powr_complex_0 by fastforce

lemma tendsto_neg_powr_complex_of_real:
  assumes "filterlim f at_top F" and "Re s < 0"
  shows   "((λx. complex_of_real (f x) powr s)  0) F"
proof -
  have "((λx. norm (complex_of_real (f x) powr s))  0) F"
  proof (rule Lim_transform_eventually)
    from assms(1) have "eventually (λx. f x  0) F"
      by (auto simp: filterlim_at_top)
    thus "eventually (λx. f x powr Re s = norm (of_real (f x) powr s)) F"
      by eventually_elim (simp add: norm_powr_real_powr)
    from assms show "((λx. f x powr Re s)  0) F"
      by (intro tendsto_neg_powr)
  qed
  thus ?thesis by (simp add: tendsto_norm_zero_iff)
qed

lemma tendsto_neg_powr_complex_of_nat:
  assumes "filterlim f at_top F" and "Re s < 0"
  shows   "((λx. of_nat (f x) powr s)  0) F"
  using tendsto_neg_powr_complex_of_real [of "real o f" F s]
proof -
  have "((λx. of_real (real (f x)) powr s)  0) F" using assms(2)
    by (intro filterlim_compose[OF _ tendsto_neg_powr_complex_of_real]
              filterlim_compose[OF _ assms(1)] filterlim_real_sequentially filterlim_ident) auto
  thus ?thesis by simp
qed

lemma continuous_powr_complex:
  assumes "f (netlimit F)  0" "continuous F f" "continuous F g"
  shows   "continuous F (λz. f z powr g z :: complex)"
  using assms unfolding continuous_def by (intro tendsto_powr_complex) simp_all

lemma isCont_powr_complex [continuous_intros]:
  assumes "f z  0" "isCont f z" "isCont g z"
  shows   "isCont (λz. f z powr g z :: complex) z"
  using assms unfolding isCont_def by (intro tendsto_powr_complex) simp_all

lemma continuous_on_powr_complex [continuous_intros]:
  assumes "A  {z. Re (f z)  0  Im (f z)  0}"
  assumes "z. z  A  f z = 0  Re (g z) > 0"
  assumes "continuous_on A f" "continuous_on A g"
  shows   "continuous_on A (λz. f z powr g z)"
  unfolding continuous_on_def
proof
  fix z assume z: "z  A"
  show "((λz. f z powr g z)  f z powr g z) (at z within A)"
  proof (cases "f z = 0")
    case False
    from assms(1,2) z have "Re (f z)  0  Im (f z)  0" "f z = 0  Re (g z) > 0" by auto
    with assms(3,4) z show ?thesis
      by (intro tendsto_powr_complex')
         (auto elim!: nonpos_Reals_cases simp: complex_eq_iff continuous_on_def)
  next
    case True
    with assms z show ?thesis
      by (auto intro!: tendsto_powr_complex_0 simp: continuous_on_def)
  qed
qed

subsectiontag unimportant›‹Some Limits involving Logarithms›

lemma lim_Ln_over_power:
  fixes s::complex
  assumes "0 < Re s"
    shows "(λn. Ln (of_nat n) / of_nat n powr s)  0"
proof (simp add: lim_sequentially dist_norm, clarify)
  fix e::real
  assume e: "0 < e"
  have "xo>0. xxo. 0 < e * 2 + (e * Re s * 2 - 2) * x + e * (Re s)2 * x2"
  proof (rule_tac x="2/(e * (Re s)2)" in exI, safe)
    show "0 < 2 / (e * (Re s)2)"
      using e assms by (simp add: field_simps)
  next
    fix x::real
    assume x: "2 / (e * (Re s)2)  x"
    have "2 / (e * (Re s)2) > 0"
      using e assms by simp
    with x have "x > 0"
      by linarith
    then have "x * 2  e * (x2 * (Re s)2)"
      using e assms x by (auto simp: power2_eq_square field_simps)
    also have " < e * (2 + (x * (Re s * 2) + x2 * (Re s)2))"
      using e assms x > 0
      by (auto simp: power2_eq_square field_simps add_pos_pos)
    finally show "0 < e * 2 + (e * Re s * 2 - 2) * x + e * (Re s)2 * x2"
      by (auto simp: algebra_simps)
  qed
  then have "xo>0. xxo. x / e < 1 + (Re s * x) + (1/2) * (Re s * x)^2"
    using e  by (simp add: field_simps)
  then have "xo>0. xxo. x / e < exp (Re s * x)"
    using assms
    by (force intro: less_le_trans [OF _ exp_lower_Taylor_quadratic])
  then obtain xo where "xo > 0" and xo: "x. x  xo  x < e * exp (Re s * x)"
    using e  by (auto simp: field_simps)
  have "norm (Ln (of_nat n) / of_nat n powr s) < e" if "n  nat exp xo" for n
  proof -
    have "ln (real n)  xo"
      using that exp_gt_zero ln_ge_iff [of n] nat_ceiling_le_eq by fastforce
    then show ?thesis
      using e xo [of "ln n"]  by (auto simp: norm_divide norm_powr_real field_split_simps)
  qed
  then show "no. nno. norm (Ln (of_nat n) / of_nat n powr s) < e"
    by blast
qed

lemma lim_Ln_over_n: "((λn. Ln(of_nat n) / of_nat n)  0) sequentially"
  using lim_Ln_over_power [of 1] by simp

lemma lim_ln_over_power:
  fixes s :: real
  assumes "0 < s"
  shows "(λn. ln (real n) / real n powr s)  0"
proof -
  have "(λn. ln (Suc n) / (Suc n) powr s)  0"
    using lim_Ln_over_power [of "of_real s", THEN filterlim_sequentially_Suc [THEN iffD2]] assms
    by (simp add: lim_sequentially dist_norm Ln_Reals_eq norm_powr_real_powr norm_divide)
  then show ?thesis
    using filterlim_sequentially_Suc[of "λn::nat. ln n / n powr s"] by auto
qed

lemma lim_ln_over_n [tendsto_intros]: "((λn. ln(real_of_nat n) / of_nat n)  0) sequentially"
  using lim_ln_over_power [of 1] by auto

lemma lim_log_over_n [tendsto_intros]:
  "(λn. log k n/n)  0"
proof -
  have *: "log k n/n = (1/ln k) * (ln n / n)" for n
    unfolding log_def by auto
  have "(λn. (1/ln k) * (ln n / n))  (1/ln k) * 0"
    by (intro tendsto_intros)
  then show ?thesis
    unfolding * by auto
qed

lemma lim_1_over_complex_power:
  assumes "0 < Re s"
  shows "(λn. 1 / of_nat n powr s)  0"
proof (rule Lim_null_comparison)
  have "n>0. 3  n  1  ln (real_of_nat n)"
    using ln_272_gt_1
    by (force intro: order_trans [of _ "ln (272/100)"])
  then show "F x in sequentially. cmod (1 / of_nat x powr s)  cmod (Ln (of_nat x) / of_nat x powr s)"
    by (auto simp: norm_divide field_split_simps eventually_sequentially)
  show "(λn. cmod (Ln (of_nat n) / of_nat n powr s))  0"
    using lim_Ln_over_power [OF assms] by (metis tendsto_norm_zero_iff)
qed

lemma lim_1_over_real_power:
  fixes s :: real
  assumes "0 < s"
  shows "((λn. 1 / (of_nat n powr s))  0) sequentially"
  using lim_1_over_complex_power [of "of_real s", THEN filterlim_sequentially_Suc [THEN iffD2]] assms
  apply (subst filterlim_sequentially_Suc [symmetric])
  by (simp add: lim_sequentially dist_norm Ln_Reals_eq norm_powr_real_powr norm_divide)

lemma lim_1_over_Ln: "(λn. 1 / Ln (complex_of_nat n))  0"
proof (clarsimp simp add: lim_sequentially dist_norm norm_divide field_split_simps)
  fix r::real
  assume "0 < r"
  have ir: "inverse (exp (inverse r)) > 0"
    by simp
  obtain n where n: "1 < of_nat n * inverse (exp (inverse r))"
    using ex_less_of_nat_mult [of _ 1, OF ir]
    by auto
  then have "exp (inverse r) < of_nat n"
    by (simp add: field_split_simps)
  then have "ln (exp (inverse r)) < ln (of_nat n)"
    by (metis exp_gt_zero less_trans ln_exp ln_less_cancel_iff)
  with 0 < r have "1 < r * ln (real_of_nat n)"
    by (simp add: field_simps)
  moreover have "n > 0" using n
    using neq0_conv by fastforce
  ultimately show "no. k. Ln (of_nat k)  0  no  k  1 < r * cmod (Ln (of_nat k))"
    using n 0 < r
    by (rule_tac x=n in exI) (force simp: field_split_simps intro: less_le_trans)
qed

lemma lim_1_over_ln: "(λn. 1 / ln (real n))  0"
  using lim_1_over_Ln [THEN filterlim_sequentially_Suc [THEN iffD2]]
  apply (subst filterlim_sequentially_Suc [symmetric])
  by (simp add: lim_sequentially dist_norm Ln_Reals_eq norm_powr_real_powr norm_divide)

lemma lim_ln1_over_ln: "(λn. ln(Suc n) / ln n)  1"
proof (rule Lim_transform_eventually)
  have "(λn. ln(1 + 1/n) / ln n)  0"
  proof (rule Lim_transform_bound)
    show "(inverse o real)  0"
      by (metis comp_def lim_inverse_n lim_explicit)
    show "F n in sequentially. norm (ln (1 + 1 / n) / ln n)  norm ((inverse  real) n)"
    proof
      fix n::nat
      assume n: "3  n"
      then have "ln 3  ln n" and ln0: "0  ln n"
        by auto
      with ln3_gt_1 have "1/ ln n  1"
        by (simp add: field_split_simps)
      moreover have "ln (1 + 1 / real n)  1/n"
        by (simp add: ln_add_one_self_le_self)
      ultimately have "ln (1 + 1 / real n) * (1 / ln n)  (1/n) * 1"
        by (intro mult_mono) (use n in auto)
      then show "norm (ln (1 + 1 / n) / ln n)  norm ((inverse  real) n)"
        by (simp add: field_simps ln0)
      qed
  qed
  then show "(λn. 1 + ln(1 + 1/n) / ln n)  1"
    by (metis (full_types) add.right_neutral tendsto_add_const_iff)
  show "F k in sequentially. 1 + ln (1 + 1 / k) / ln k = ln(Suc k) / ln k"
    by (simp add: field_split_simps ln_div eventually_sequentiallyI [of 2])
qed

lemma lim_ln_over_ln1: "(λn. ln n / ln(Suc n))  1"
  using tendsto_inverse [OF lim_ln1_over_ln] by force


subsectiontag unimportant›‹Relation between Square Root and exp/ln, hence its derivative›

lemma csqrt_exp_Ln:
  assumes "z  0"
    shows "csqrt z = exp(Ln z / 2)"
proof -
  have "(exp (Ln z / 2))2 = (exp (Ln z))"
    by (metis exp_double nonzero_mult_div_cancel_left times_divide_eq_right zero_neq_numeral)
  also have " = z"
    using assms exp_Ln by blast
  finally have "csqrt z = csqrt ((exp (Ln z / 2))2)"
    by simp
  also have " = exp (Ln z / 2)"
    apply (rule csqrt_square)
    using cos_gt_zero_pi [of "(Im (Ln z) / 2)"] Im_Ln_le_pi mpi_less_Im_Ln assms
    by (fastforce simp: Re_exp Im_exp)
  finally show ?thesis using assms csqrt_square
    by simp
qed

lemma csqrt_conv_powr: "csqrt z = z powr (1/2)"
  by (auto simp: csqrt_exp_Ln powr_def)

lemma csqrt_mult:
  assumes "Arg z + Arg w  {-pi<..pi}"
  shows   "csqrt (z * w) = csqrt z * csqrt w"
proof (cases "z = 0  w = 0")
  case False
  have "csqrt (z * w) = exp ((ln (z * w)) / 2)"
    using False by (intro csqrt_exp_Ln) auto
  also have " = exp ((Ln z + Ln w) / 2)"
    using False assms by (subst Ln_times_simple) (auto simp: Arg_eq_Im_Ln)
  also have "(Ln z + Ln w) / 2 = Ln z / 2 + Ln w / 2"
    by (simp add: add_divide_distrib)
  also have "exp  = csqrt z * csqrt w"
    using False by (simp add: exp_add csqrt_exp_Ln)
  finally show ?thesis .
qed auto

lemma Arg_csqrt [simp]: "Arg (csqrt z) = Arg z / 2"
proof (cases "z = 0")
  case False
  have "Im (Ln z)  {-pi<..pi}"
    by (simp add: False Im_Ln_le_pi mpi_less_Im_Ln)
  also have "  {-2*pi<..2*pi}"
    by auto
  finally show ?thesis
    using False by (auto simp: csqrt_exp_Ln Arg_exp Arg_eq_Im_Ln)
qed (auto simp: Arg_zero)

lemma csqrt_inverse:
  "z  0  csqrt (inverse z) = inverse (csqrt z)"
  by (metis Ln_inverse csqrt_eq_0 csqrt_exp_Ln divide_minus_left exp_minus 
      inverse_nonzero_iff_nonzero)

lemma cnj_csqrt: "z  0  cnj(csqrt z) = csqrt(cnj z)"
  by (metis cnj_Ln complex_cnj_divide complex_cnj_numeral complex_cnj_zero_iff csqrt_eq_0 csqrt_exp_Ln exp_cnj)

lemma has_field_derivative_csqrt:
  assumes "z  0"
    shows "(csqrt has_field_derivative inverse(2 * csqrt z)) (at z)"
proof -
  have z: "z  0"
    using assms by auto
  then have *: "inverse z = inverse (2*z) * 2"
    by (simp add: field_split_simps)
  have [simp]: "exp (Ln z / 2) * inverse z = inverse (csqrt z)"
    by (simp add: z field_simps csqrt_exp_Ln [symmetric]) (metis power2_csqrt power2_eq_square)
  have "Im z = 0  0 < Re z"
    using assms complex_nonpos_Reals_iff not_less by blast
  with z have "((λz. exp (Ln z / 2)) has_field_derivative inverse (2 * csqrt z)) (at z)"
    by (force intro: derivative_eq_intros * simp add: assms)
  then show ?thesis
  proof (rule has_field_derivative_transform_within)
    show "x. dist x z < cmod z  exp (Ln x / 2) = csqrt x"
      by (metis csqrt_exp_Ln dist_0_norm less_irrefl)
  qed (use z in auto)
qed

lemma field_differentiable_at_csqrt:
    "z  0  csqrt field_differentiable at z"
  using field_differentiable_def has_field_derivative_csqrt by blast

lemma field_differentiable_within_csqrt:
    "z  0  csqrt field_differentiable (at z within s)"
  using field_differentiable_at_csqrt field_differentiable_within_subset by blast

lemma continuous_at_csqrt:
    "z  0  continuous (at z) csqrt"
  by (simp add: field_differentiable_within_csqrt field_differentiable_imp_continuous_at)

corollarytag unimportant› isCont_csqrt' [simp]:
   "isCont f z; f z  0  isCont (λx. csqrt (f x)) z"
  by (blast intro: isCont_o2 [OF _ continuous_at_csqrt])

lemma continuous_within_csqrt:
    "z  0  continuous (at z within s) csqrt"
  by (simp add: field_differentiable_imp_continuous_at field_differentiable_within_csqrt)

lemma continuous_on_csqrt [continuous_intros]:
    "continuous_on (-0) csqrt"
  by (simp add: continuous_at_imp_continuous_on continuous_within_csqrt)

lemma holomorphic_on_csqrt [holomorphic_intros]: "csqrt holomorphic_on -0"
  by (simp add: field_differentiable_within_csqrt holomorphic_on_def)

lemma holomorphic_on_csqrt' [holomorphic_intros]:
  "f holomorphic_on A  (z. z  A  f z  0)  (λz. csqrt (f z)) holomorphic_on A"
  using holomorphic_on_compose_gen[OF _ holomorphic_on_csqrt, of f A] by (auto simp: o_def)

lemma analytic_on_csqrt [analytic_intros]: "csqrt analytic_on -0"
  using holomorphic_on_csqrt by (subst analytic_on_open) auto

lemma analytic_on_csqrt' [analytic_intros]:
  "f analytic_on A  (z. z  A  f z  0)  (λz. csqrt (f z)) analytic_on A"
  using analytic_on_compose_gen[OF _ analytic_on_csqrt, of f A] by (auto simp: o_def)

lemma continuous_within_closed_nontrivial:
    "closed s  a  s ==> continuous (at a within s) f"
  using Compl_iff continuous_within_topological open_Compl by fastforce

lemma continuous_within_csqrt_posreal:
    "continuous (at z within (  {w. 0  Re(w)})) csqrt"
proof (cases "z  0")
  case True
  then have [simp]: "Im z = 0" and 0: "Re z < 0  z = 0"
    using complex_nonpos_Reals_iff complex_eq_iff by force+
  show ?thesis
    using 0
  proof
    assume "Re z < 0"
    then show ?thesis
      by (auto simp: continuous_within_closed_nontrivial [OF closed_Real_halfspace_Re_ge])
  next
    assume "z = 0"
    moreover
    have "e. 0 < e
          x'  {w. 0  Re w}. cmod x' < e^2  cmod (csqrt x') < e"
      by (auto simp: Reals_def real_less_lsqrt)
    ultimately show ?thesis
      using zero_less_power by (fastforce simp: continuous_within_eps_delta)
  qed
qed (blast intro: continuous_within_csqrt)

subsection‹Complex arctangent›

text‹The branch cut gives standard bounds in the real case.›

definitiontag important› Arctan :: "complex  complex" where
    "Arctan  λz. (𝗂/2) * Ln((1 - 𝗂*z) / (1 + 𝗂*z))"

lemma Arctan_def_moebius: "Arctan z = 𝗂/2 * Ln (moebius (-𝗂) 1 𝗂 1 z)"
  by (simp add: Arctan_def moebius_def add_ac)

lemma Ln_conv_Arctan:
  assumes "z  -1"
  shows   "Ln z = -2*𝗂 * Arctan (moebius 1 (- 1) (- 𝗂) (- 𝗂) z)"
proof -
  have "Arctan (moebius 1 (- 1) (- 𝗂) (- 𝗂) z) =
             𝗂/2 * Ln (moebius (- 𝗂) 1 𝗂 1 (moebius 1 (- 1) (- 𝗂) (- 𝗂) z))"
    by (simp add: Arctan_def_moebius)
  also from assms have "𝗂 * z  𝗂 * (-1)" by (subst mult_left_cancel) simp
  hence "𝗂 * z - -𝗂  0" by (simp add: eq_neg_iff_add_eq_0)
  from moebius_inverse'[OF _ this, of 1 1]
    have "moebius (- 𝗂) 1 𝗂 1 (moebius 1 (- 1) (- 𝗂) (- 𝗂) z) = z" by simp
  finally show ?thesis by (simp add: field_simps)
qed

lemma Arctan_0 [simp]: "Arctan 0 = 0"
  by (simp add: Arctan_def)

lemma Im_complex_div_lemma: "Im((1 - 𝗂*z) / (1 + 𝗂*z)) = 0  Re z = 0"
  by (auto simp: Im_complex_div_eq_0 algebra_simps)

lemma Re_complex_div_lemma: "0 < Re((1 - 𝗂*z) / (1 + 𝗂*z))  norm z < 1"
  by (simp add: Re_complex_div_gt_0 algebra_simps cmod_def power2_eq_square)

lemma tan_Arctan:
  assumes "z2  -1"
  shows [simp]: "tan(Arctan z) = z"
proof -
  obtain "1 + 𝗂*z  0" "1 - 𝗂*z  0"
    by (metis add_diff_cancel_left' assms diff_0 i_times_eq_iff mult_cancel_left2 power2_i power2_minus right_minus_eq)
  then show ?thesis
    by (simp add: Arctan_def tan_def sin_exp_eq cos_exp_eq exp_minus divide_simps 
        flip: csqrt_exp_Ln power2_eq_square)
qed

lemma Arctan_tan [simp]:
  assumes "¦Re z¦ < pi/2"
    shows "Arctan(tan z) = z"
proof -
  have "Ln ((1 - 𝗂 * tan z) / (1 + 𝗂 * tan z)) = 2 * z / 𝗂"
  proof (rule Ln_unique)
    have ge_pi2: "n::int. ¦of_int (2*n + 1) * pi/2¦  pi/2"
      by (case_tac n rule: int_cases) (auto simp: abs_mult)
    have "exp (𝗂*z)*exp (𝗂*z) = -1  exp (2*𝗂*z) = -1"
      by (metis distrib_right exp_add mult_2)
    also have "  exp (2*𝗂*z) = exp (𝗂*pi)"
      using cis_conv_exp cis_pi by auto
    also have "  exp (2*𝗂*z - 𝗂*pi) = 1"
      by (metis (no_types) diff_add_cancel diff_minus_eq_add exp_add exp_minus_inverse mult.commute)
    also have "  Re(𝗂*2*z - 𝗂*pi) = 0  (n::int. Im(𝗂*2*z - 𝗂*pi) = of_int (2 * n) * pi)"
      by (simp add: exp_eq_1)
    also have "  Im z = 0  (n::int. 2 * Re z = of_int (2*n + 1) * pi)"
      by (simp add: algebra_simps)
    also have "  False"
      using assms ge_pi2
      by (metis eq_divide_eq linorder_not_less mult.commute zero_neq_numeral)
    finally have "exp (𝗂*z)*exp (𝗂*z) + 1  0"
      by (auto simp: add.commute minus_unique)
    then show "exp (2 * z / 𝗂) = (1 - 𝗂 * tan z) / (1 + 𝗂 * tan z)"
      apply (simp add: tan_def sin_exp_eq cos_exp_eq exp_minus divide_simps)
      by (simp add: algebra_simps flip: power2_eq_square exp_double)
  qed (use assms in auto)
  then show ?thesis
    by (auto simp: Arctan_def)
qed

lemma
  assumes "Re z = 0  ¦Im z¦ < 1"
  shows Re_Arctan_bounds: "¦Re(Arctan z)¦ < pi/2"
    and has_field_derivative_Arctan: "(Arctan has_field_derivative inverse(1 + z2)) (at z)"
proof -
  have nz0: "1 + 𝗂*z  0"
    using assms
    by (metis abs_one add_diff_cancel_left' complex_i_mult_minus diff_0 i_squared imaginary_unit.simps
                less_asym neg_equal_iff_equal)
  have "z  -𝗂" using assms
    by auto
  then have zz: "1 + z * z  0"
    by (metis abs_one assms i_squared imaginary_unit.simps less_irrefl minus_unique square_eq_iff)
  have nz1: "1 - 𝗂*z  0"
    using assms by (force simp add: i_times_eq_iff)
  have nz2: "inverse (1 + 𝗂*z)  0"
    using assms
    by (metis Im_complex_div_lemma Re_complex_div_lemma cmod_eq_Im divide_complex_def
              less_irrefl mult_zero_right zero_complex.simps(1) zero_complex.simps(2))
  have nzi: "((1 - 𝗂*z) * inverse (1 + 𝗂*z))  0"
    using nz1 nz2 by auto
  have "Im ((1 - 𝗂*z) / (1 + 𝗂*z)) = 0  0 < Re ((1 - 𝗂*z) / (1 + 𝗂*z))"
    by (simp add: Im_complex_div_lemma Re_complex_div_lemma assms cmod_eq_Im)
  then have *: "((1 - 𝗂*z) / (1 + 𝗂*z))  0"
    by (auto simp add: complex_nonpos_Reals_iff)
  show "¦Re(Arctan z)¦ < pi/2"
    unfolding Arctan_def divide_complex_def
    using mpi_less_Im_Ln [OF nzi]
    by (auto simp: abs_if intro!: Im_Ln_less_pi * [unfolded divide_complex_def])
  show "(Arctan has_field_derivative inverse(1 + z2)) (at z)"
    unfolding Arctan_def scaleR_conv_of_real
    apply (intro derivative_eq_intros | simp add: nz0 *)+
    using nz1 zz
    apply (simp add: field_split_simps power2_eq_square)
    apply algebra
    done
qed

lemma field_differentiable_at_Arctan: "(Re z = 0  ¦Im z¦ < 1)  Arctan field_differentiable at z"
  using has_field_derivative_Arctan
  by (auto simp: field_differentiable_def)

lemma field_differentiable_within_Arctan:
    "(Re z = 0  ¦Im z¦ < 1)  Arctan field_differentiable (at z within s)"
  using field_differentiable_at_Arctan field_differentiable_at_within by blast

declare has_field_derivative_Arctan [derivative_intros]
declare has_field_derivative_Arctan [THEN DERIV_chain2, derivative_intros]

lemma continuous_at_Arctan:
    "(Re z = 0  ¦Im z¦ < 1)  continuous (at z) Arctan"
  by (simp add: field_differentiable_imp_continuous_at field_differentiable_within_Arctan)

lemma continuous_within_Arctan:
    "(Re z = 0  ¦Im z¦ < 1)  continuous (at z within s) Arctan"
  using continuous_at_Arctan continuous_at_imp_continuous_within by blast

lemma continuous_on_Arctan [continuous_intros]:
    "(z. z  s  Re z = 0  ¦Im z¦ < 1)  continuous_on s Arctan"
  by (auto simp: continuous_at_imp_continuous_on continuous_within_Arctan)

lemma holomorphic_on_Arctan:
    "(z. z  s  Re z = 0  ¦Im z¦ < 1)  Arctan holomorphic_on s"
  by (simp add: field_differentiable_within_Arctan holomorphic_on_def)

theorem Arctan_series:
  assumes z: "norm (z :: complex) < 1"
  defines "g  λn. if odd n then -𝗂*𝗂^n / n else 0"
  defines "h  λz n. (-1)^n / of_nat (2*n+1) * (z::complex)^(2*n+1)"
  shows   "(λn. g n * z^n) sums Arctan z"
  and     "h z sums Arctan z"
proof -
  define G where [abs_def]: "G z = (n. g n * z^n)" for z
  have summable: "summable (λn. g n * u^n)" if "norm u < 1" for u
  proof (cases "u = 0")
    case False
    have "(λn. ereal (norm (h u n) / norm (h u (Suc n)))) = (λn. ereal (inverse (norm u)^2) *
              ereal ((2 + inverse (real (Suc n))) / (2 - inverse (real (Suc n)))))"
    proof
      fix n
      have "ereal (norm (h u n) / norm (h u (Suc n))) =
             ereal (inverse (norm u)^2) * ereal (((2*Suc n+1) / (Suc n)) /
                 ((2*Suc n-1) / (Suc n)))"
      by (simp add: h_def norm_mult norm_power norm_divide field_split_simps
                    power2_eq_square eval_nat_numeral del: of_nat_add of_nat_Suc)
      also have "of_nat (2*Suc n+1) / of_nat (Suc n) = (2::real) + inverse (real (Suc n))"
        by (auto simp: field_split_simps simp del: of_nat_Suc) simp_all?
      also have "of_nat (2*Suc n-1) / of_nat (Suc n) = (2::real) - inverse (real (Suc n))"
        by (auto simp: field_split_simps simp del: of_nat_Suc) simp_all?
      finally show "ereal (norm (h u n) / norm (h u (Suc n))) = ereal (inverse (norm u)^2) *
              ereal ((2 + inverse (real (Suc n))) / (2 - inverse (real (Suc n))))" .
    qed
    also have "  ereal (inverse (norm u)^2) * ereal ((2 + 0) / (2 - 0))"
      by (intro tendsto_intros LIMSEQ_inverse_real_of_nat) simp_all
    finally have "liminf (λn. ereal (cmod (h u n) / cmod (h u (Suc n)))) = inverse (norm u)^2"
      by (intro lim_imp_Liminf) simp_all
    moreover from power_strict_mono[OF that, of 2] False have "inverse (norm u)^2 > 1"
      by (simp add: field_split_simps)
    ultimately have A: "liminf (λn. ereal (cmod (h u n) / cmod (h u (Suc n)))) > 1" by simp
    from False have "summable (h u)"
      by (intro summable_norm_cancel[OF ratio_test_convergence[OF _ A]])
         (auto simp: h_def norm_divide norm_mult norm_power simp del: of_nat_Suc
               intro!: mult_pos_pos divide_pos_pos always_eventually)
    thus "summable (λn. g n * u^n)"
      by (subst summable_mono_reindex[of "λn. 2*n+1", symmetric])
         (auto simp: power_mult strict_mono_def g_def h_def elim!: oddE)
  qed (simp add: h_def)

  have "c. uball 0 1. Arctan u - G u = c"
  proof (rule has_field_derivative_zero_constant)
    fix u :: complex assume "u  ball 0 1"
    hence u: "norm u < 1" by (simp)
    define K where "K = (norm u + 1) / 2"
    from u and abs_Im_le_cmod[of u] have Im_u: "¦Im u¦ < 1" by linarith
    from u have K: "0  K" "norm u < K" "K < 1" by (simp_all add: K_def)
    hence "(G has_field_derivative (n. diffs g n * u ^ n)) (at u)" unfolding G_def
      by (intro termdiffs_strong[of _ "of_real K"] summable) simp_all
    also have "(λn. diffs g n * u^n) = (λn. if even n then (𝗂*u)^n else 0)"
      by (intro ext) (simp_all del: of_nat_Suc add: g_def diffs_def power_mult_distrib)
    also have "suminf  = (n. (-(u^2))^n)"
      by (subst suminf_mono_reindex[of "λn. 2*n", symmetric])
         (auto elim!: evenE simp: strict_mono_def power_mult power_mult_distrib)
    also from u have "norm u^2 < 1^2" by (intro power_strict_mono) simp_all
    hence "(n. (-(u^2))^n) = inverse (1 + u^2)"
      by (subst suminf_geometric) (simp_all add: norm_power inverse_eq_divide)
    finally have "(G has_field_derivative inverse (1 + u2)) (at u)" .
    from DERIV_diff[OF has_field_derivative_Arctan this] Im_u u
      show "((λu. Arctan u - G u) has_field_derivative 0) (at u within ball 0 1)"
      by (simp_all add: at_within_open[OF _ open_ball])
  qed simp_all
  then obtain c where c: "u. norm u < 1  Arctan u - G u = c" by auto
  from this[of 0] have "c = 0" by (simp add: G_def g_def)
  with c z have "Arctan z = G z" by simp
  with summable[OF z] show "(λn. g n * z^n) sums Arctan z" unfolding G_def by (simp add: sums_iff)
  thus "h z sums Arctan z" by (subst (asm) sums_mono_reindex[of "λn. 2*n+1", symmetric])
                              (auto elim!: oddE simp: strict_mono_def power_mult g_def h_def)
qed

text ‹A quickly-converging series for the logarithm, based on the arctangent.›
theorem ln_series_quadratic:
  assumes x: "x > (0::real)"
  shows "(λn. (2*((x - 1) / (x + 1)) ^ (2*n+1) / of_nat (2*n+1))) sums ln x"
proof -
  define y :: complex where "y = of_real ((x-1)/(x+1))"
  from x have x': "complex_of_real x  of_real (-1)"  by (subst of_real_eq_iff) auto
  from x have "¦x - 1¦ < ¦x + 1¦" by linarith
  hence "norm (complex_of_real (x - 1) / complex_of_real (x + 1)) < 1"
    by (simp add: norm_divide del: of_real_add of_real_diff)
  hence "norm (𝗂 * y) < 1" unfolding y_def by (subst norm_mult) simp
  hence "(λn. (-2*𝗂) * ((-1)^n / of_nat (2*n+1) * (𝗂*y)^(2*n+1))) sums ((-2*𝗂) * Arctan (𝗂*y))"
    by (intro Arctan_series sums_mult) simp_all
  also have "(λn. (-2*𝗂) * ((-1)^n / of_nat (2*n+1) * (𝗂*y)^(2*n+1))) =
                 (λn. (-2*𝗂) * ((-1)^n * (𝗂*y*(-y2)^n)/of_nat (2*n+1)))"
    by (intro ext) (simp_all add: power_mult power_mult_distrib)
  also have " = (λn. 2*y* ((-1) * (-y2))^n/of_nat (2*n+1))"
    by (intro ext, subst power_mult_distrib) (simp add: algebra_simps power_mult)
  also have " = (λn. 2*y^(2*n+1) / of_nat (2*n+1))"
    by (subst power_add, subst power_mult) (simp add: mult_ac)
  also have " = (λn. of_real (2*((x-1)/(x+1))^(2*n+1) / of_nat (2*n+1)))"
    by (intro ext) (simp add: y_def)
  also have "𝗂 * y = (of_real x - 1) / (-𝗂 * (of_real x + 1))"
    by (subst divide_divide_eq_left [symmetric]) (simp add: y_def)
  also have " = moebius 1 (-1) (-𝗂) (-𝗂) (of_real x)" by (simp add: moebius_def algebra_simps)
  also from x' have "-2*𝗂*Arctan  = Ln (of_real x)" by (intro Ln_conv_Arctan [symmetric]) simp_all
  also from x have " = ln x" by (rule Ln_of_real)
  finally show ?thesis by (subst (asm) sums_of_real_iff)
qed

subsectiontag unimportant› ‹Real arctangent›

lemma Im_Arctan_of_real [simp]: "Im (Arctan (of_real x)) = 0"
proof -
  have ne: "1 + x2  0"
    by (metis power_one sum_power2_eq_zero_iff zero_neq_one)
  have ne1: "1 + 𝗂 * complex_of_real x  0"
    using Complex_eq complex_eq_cancel_iff2 by fastforce
  have "Re (Ln ((1 - 𝗂 * x) * inverse (1 + 𝗂 * x))) = 0"
    apply (rule norm_exp_imaginary)
    using ne
    apply (simp add: ne1 cmod_def)
    apply (auto simp: field_split_simps)
    apply algebra
    done
  then show ?thesis
    unfolding Arctan_def divide_complex_def by (simp add: complex_eq_iff)
qed

lemma arctan_eq_Re_Arctan: "arctan x = Re (Arctan (of_real x))"
proof (rule arctan_unique)
  have "(1 - 𝗂 * x) / (1 + 𝗂 * x)  0"
    by (auto simp: Im_complex_div_lemma complex_nonpos_Reals_iff)
  then show "- (pi / 2) < Re (Arctan (complex_of_real x))"
    by (simp add: Arctan_def Im_Ln_less_pi)
next
  have *: " (1 - 𝗂*x) / (1 + 𝗂*x)  0"
    by (simp add: field_split_simps) ( simp add: complex_eq_iff)
  show "Re (Arctan (complex_of_real x)) < pi / 2"
    using mpi_less_Im_Ln [OF *]
    by (simp add: Arctan_def)
next
  have "tan (Re (Arctan (of_real x))) = Re (tan (Arctan (of_real x)))"
    by (metis Im_Arctan_of_real Re_complex_of_real complex_is_Real_iff of_real_Re tan_of_real)
  also have " = x"
  proof -
    have "(complex_of_real x)2  - 1"
      by (smt (verit, best) Im_complex_of_real imaginary_unit.sel(2) of_real_minus power2_eq_iff power2_i)
    then show ?thesis
      by simp
  qed
  finally show "tan (Re (Arctan (complex_of_real x))) = x" .
qed

lemma Arctan_of_real: "Arctan (of_real x) = of_real (arctan x)"
  unfolding arctan_eq_Re_Arctan divide_complex_def
  by (simp add: complex_eq_iff)

lemma Arctan_in_Reals [simp]: "z    Arctan z  "
  by (metis Reals_cases Reals_of_real Arctan_of_real)

declare arctan_one [simp]

lemma arctan_less_pi4_pos: "x < 1  arctan x < pi/4"
  by (metis arctan_less_iff arctan_one)

lemma arctan_less_pi4_neg: "-1 < x  -(pi/4) < arctan x"
  by (metis arctan_less_iff arctan_minus arctan_one)

lemma arctan_less_pi4: "¦x¦ < 1  ¦arctan x¦ < pi/4"
  by (metis abs_less_iff arctan_less_pi4_pos arctan_minus)

lemma arctan_le_pi4: "¦x¦  1  ¦arctan x¦  pi/4"
  by (metis abs_le_iff arctan_le_iff arctan_minus arctan_one)

lemma abs_arctan: "¦arctan x¦ = arctan ¦x¦"
  by (simp add: abs_if arctan_minus)

lemma arctan_add_raw:
  assumes "¦arctan x + arctan y¦ < pi/2"
    shows "arctan x + arctan y = arctan((x + y) / (1 - x * y))"
proof (rule arctan_unique [symmetric])
  show 12: "- (pi / 2) < arctan x + arctan y" "arctan x + arctan y < pi / 2"
    using assms by linarith+
  show "tan (arctan x + arctan y) = (x + y) / (1 - x * y)"
    using cos_gt_zero_pi [OF 12] by (simp add: arctan tan_add)
qed

lemma arctan_inverse:
  "0 < x arctan(inverse x) = pi/2 - arctan x"
  by (smt (verit, del_insts) arctan arctan_unique tan_cot zero_less_arctan_iff)

lemma arctan_add_small:
  assumes "¦x * y¦ < 1"
    shows "(arctan x + arctan y = arctan((x + y) / (1 - x * y)))"
proof (cases "x = 0  y = 0")
  case False
  with assms have "¦x¦ < inverse ¦y¦"
    by (simp add: field_split_simps abs_mult)
  with False have "¦arctan x¦ < pi / 2 - ¦arctan y¦" using assms
    by (auto simp add: abs_arctan arctan_inverse [symmetric] arctan_less_iff)
  then show ?thesis
    by (intro arctan_add_raw) linarith
qed auto

lemma abs_arctan_le:
  fixes x::real shows "¦arctan x¦  ¦x¦"
proof -
  have 1: "x. x    cmod (inverse (1 + x2))  1"
    by (simp add: norm_divide divide_simps in_Reals_norm complex_is_Real_iff power2_eq_square)
  have "cmod (Arctan w - Arctan z)  1 * cmod (w-z)" if "w  " "z  " for w z
    apply (rule field_differentiable_bound [OF convex_Reals, of Arctan _ 1])
       apply (rule has_field_derivative_at_within [OF has_field_derivative_Arctan])
    using 1 that by (auto simp: Reals_def)
  then have "cmod (Arctan (of_real x) - Arctan 0)  1 * cmod (of_real x - 0)"
    using Reals_0 Reals_of_real by blast
  then show ?thesis
    by (simp add: Arctan_of_real)
qed

lemma arctan_le_self: "0  x  arctan x  x"
  by (metis abs_arctan_le abs_of_nonneg zero_le_arctan_iff)

lemma abs_tan_ge: "¦x¦ < pi/2  ¦x¦  ¦tan x¦"
  by (metis abs_arctan_le abs_less_iff arctan_tan minus_less_iff)

lemma arctan_bounds:
  assumes "0  x" "x < 1"
  shows arctan_lower_bound:
    "(k<2 * n. (- 1) ^ k * (1 / real (k * 2 + 1) * x ^ (k * 2 + 1)))  arctan x" (is "(k<_. _ * ?a k)  _")
    and arctan_upper_bound:
    "arctan x  (k<2 * n + 1. (- 1) ^ k * (1 / real (k * 2 + 1) * x ^ (k * 2 + 1)))"
proof -
  have tendsto_zero: "?a  0"
  proof (rule tendsto_eq_rhs)
    show "(λk. 1 / real (k * 2 + 1) * x ^ (k * 2 + 1))  0 * 0"
      using assms
      by (intro tendsto_mult real_tendsto_divide_at_top)
        (auto simp: filterlim_sequentially_iff_filterlim_real
          intro!: real_tendsto_divide_at_top tendsto_power_zero filterlim_real_sequentially
          tendsto_eq_intros filterlim_at_top_mult_tendsto_pos filterlim_tendsto_add_at_top)
  qed simp
  have nonneg: "0  ?a n" for n
    by (force intro!: divide_nonneg_nonneg mult_nonneg_nonneg zero_le_power assms)
  have le: "?a (Suc n)  ?a n" for n
    by (rule mult_mono[OF _ power_decreasing]) (auto simp: field_split_simps assms less_imp_le)
  from summable_Leibniz'(4)[of ?a, OF tendsto_zero nonneg le, of n]
    summable_Leibniz'(2)[of ?a, OF tendsto_zero nonneg le, of n]
    assms
  show "(k<2*n. (- 1)^ k * ?a k)  arctan x" "arctan x  (k<2 * n + 1. (- 1)^ k * ?a k)"
    by (auto simp: arctan_series)
qed

subsectiontag unimportant› ‹Bounds on pi using real arctangent›

lemma pi_machin: "pi = 16 * arctan (1 / 5) - 4 * arctan (1 / 239)"
  using machin by simp

lemma pi_approx: "3.141592653588  pi" "pi  3.1415926535899"
  unfolding pi_machin
  using arctan_bounds[of "1/5"   4]
        arctan_bounds[of "1/239" 4]
  by (simp_all add: eval_nat_numeral)

lemma pi_gt3: "pi > 3"
  using pi_approx by simp


subsection‹Inverse Sine›

definitiontag important› Arcsin :: "complex  complex" where
   "Arcsin  λz. -𝗂 * Ln(𝗂 * z + csqrt(1 - z2))"

lemma Arcsin_body_lemma: "𝗂 * z + csqrt(1 - z2)  0"
  using power2_csqrt [of "1 - z2"]
  by (metis add.inverse_unique diff_0 diff_add_cancel mult.left_commute mult_minus1_right power2_i power2_minus power_mult_distrib zero_neq_one)

lemma Arcsin_range_lemma: "¦Re z¦ < 1  0 < Re(𝗂 * z + csqrt(1 - z2))"
  using Complex.cmod_power2 [of z, symmetric]
  by (simp add: real_less_rsqrt algebra_simps Re_power2 cmod_square_less_1_plus)

lemma Re_Arcsin: "Re(Arcsin z) = Im (Ln (𝗂 * z + csqrt(1 - z2)))"
  by (simp add: Arcsin_def)

lemma Im_Arcsin: "Im(Arcsin z) = - ln (cmod (𝗂 * z + csqrt (1 - z2)))"
  by (simp add: Arcsin_def Arcsin_body_lemma)

lemma one_minus_z2_notin_nonpos_Reals:
  assumes "Im z = 0  ¦Re z¦ < 1"
  shows "1 - z2  0"
proof (cases "Im z = 0")
  case True
  with assms show ?thesis
    by (simp add: complex_nonpos_Reals_iff flip: abs_square_less_1)
next
  case False
  have "¬ (Im z)2  - 1"
    using False power2_less_eq_zero_iff by fastforce
  with False show ?thesis
    by (auto simp add: complex_nonpos_Reals_iff Re_power2 Im_power2)
qed

lemma isCont_Arcsin_lemma:
  assumes le0: "Re (𝗂 * z + csqrt (1 - z2))  0" and "(Im z = 0  ¦Re z¦ < 1)"
    shows False
proof (cases "Im z = 0")
  case True
  then show ?thesis
    using assms by (fastforce simp: cmod_def abs_square_less_1 [symmetric])
next
  case False
  have leim: "(cmod (1 - z2) + (1 - Re (z2))) / 2  (Im z)2"
    using le0 sqrt_le_D by fastforce
  have neq: "(cmod z)2  1 + cmod (1 - z2)"
  proof (clarsimp simp add: cmod_def)
    assume "(Re z)2 + (Im z)2 = 1 + sqrt ((1 - Re (z2))2 + (Im (z2))2)"
    then have "((Re z)2 + (Im z)2 - 1)2 = ((1 - Re (z2))2 + (Im (z2))2)"
      by simp
    then show False using False
      by (simp add: power2_eq_square algebra_simps)
  qed
  moreover have 2: "(Im z)2 = (1 + ((Im z)2 + cmod (1 - z2)) - (Re z)2) / 2"
    using leim cmod_power2 [of z] norm_triangle_ineq2 [of "z^2" 1]
    by (simp add: norm_power Re_power2 norm_minus_commute [of 1])
  ultimately show False
    by (simp add: Re_power2 Im_power2 cmod_power2)
qed

lemma isCont_Arcsin:
  assumes "(Im z = 0  ¦Re z¦ < 1)"
    shows "isCont Arcsin z"
proof -
  have 1: "𝗂 * z + csqrt (1 - z2)  0"
    by (metis isCont_Arcsin_lemma assms complex_nonpos_Reals_iff)
  have 2: "1 - z2  0"
    by (simp add: one_minus_z2_notin_nonpos_Reals assms)
  show ?thesis
    using assms unfolding Arcsin_def by (intro isCont_Ln' isCont_csqrt' continuous_intros 1 2)
qed

lemma isCont_Arcsin' [simp]:
  shows "isCont f z  (Im (f z) = 0  ¦Re (f z)¦ < 1)  isCont (λx. Arcsin (f x)) z"
  by (blast intro: isCont_o2 [OF _ isCont_Arcsin])

lemma sin_Arcsin [simp]: "sin(Arcsin z) = z"
proof -
  have "𝗂*z*2 + csqrt (1 - z2)*2 = 0  (𝗂*z)*2 + csqrt (1 - z2)*2 = 0"
    by (simp add: algebra_simps)  ― ‹Cancelling a factor of 2›
  moreover have "  (𝗂*z) + csqrt (1 - z2) = 0"
    by (metis Arcsin_body_lemma distrib_right no_zero_divisors zero_neq_numeral)
  ultimately show ?thesis
    apply (simp add: sin_exp_eq Arcsin_def Arcsin_body_lemma exp_minus divide_simps)
    apply (simp add: algebra_simps)
    apply (simp add: right_diff_distrib flip: power2_eq_square)
    done
qed

lemma Re_eq_pihalf_lemma:
    "¦Re z¦ = pi/2  Im z = 0 
      Re ((exp (𝗂*z) + inverse (exp (𝗂*z))) / 2) = 0  0  Im ((exp (𝗂*z) + inverse (exp (𝗂*z))) / 2)"
  apply (simp add: cos_i_times [symmetric] Re_cos Im_cos abs_if del: eq_divide_eq_numeral1)
  by (metis cos_minus cos_pi_half)

lemma Re_less_pihalf_lemma:
  assumes "¦Re z¦ < pi / 2"
    shows "0 < Re ((exp (𝗂*z) + inverse (exp (𝗂*z))) / 2)"
proof -
  have "0 < cos (Re z)" using assms
    using cos_gt_zero_pi by auto
  then show ?thesis
    by (simp add: cos_i_times [symmetric] Re_cos Im_cos add_pos_pos)
qed

lemma Arcsin_sin:
    assumes "¦Re z¦ < pi/2  (¦Re z¦ = pi/2  Im z = 0)"
      shows "Arcsin(sin z) = z"
proof -
  have "Arcsin(sin z) = - (𝗂 * Ln (csqrt (1 - (𝗂 * (exp (𝗂*z) - inverse (exp (𝗂*z))))2 / 4) - (inverse (exp (𝗂*z)) - exp (𝗂*z)) / 2))"
    by (simp add: sin_exp_eq Arcsin_def exp_minus power_divide)
  also have " = - (𝗂 * Ln (csqrt (((exp (𝗂*z) + inverse (exp (𝗂*z)))/2)2) - (inverse (exp (𝗂*z)) - exp (𝗂*z)) / 2))"
    by (simp add: field_simps power2_eq_square)
  also have " = - (𝗂 * Ln (((exp (𝗂*z) + inverse (exp (𝗂*z)))/2) - (inverse (exp (𝗂*z)) - exp (𝗂*z)) / 2))"
    apply (subst csqrt_square)
    using assms Re_eq_pihalf_lemma Re_less_pihalf_lemma by auto
  also have " =  - (𝗂 * Ln (exp (𝗂*z)))"
    by (simp add: field_simps power2_eq_square)
  also have " = z"
    using assms by (auto simp: abs_if simp del: eq_divide_eq_numeral1 split: if_split_asm)
  finally show ?thesis .
qed

lemma Arcsin_unique:
    "sin z = w; ¦Re z¦ < pi/2  (¦Re z¦ = pi/2  Im z = 0)  Arcsin w = z"
  by (metis Arcsin_sin)

lemma Arcsin_0 [simp]: "Arcsin 0 = 0"
  by (simp add: Arcsin_unique)

lemma Arcsin_1 [simp]: "Arcsin 1 = pi/2"
  using Arcsin_unique sin_of_real_pi_half by fastforce

lemma Arcsin_minus_1 [simp]: "Arcsin(-1) = - (pi/2)"
  by (simp add: Arcsin_unique)

lemma has_field_derivative_Arcsin:
  assumes "Im z = 0  ¦Re z¦ < 1"
    shows "(Arcsin has_field_derivative inverse(cos(Arcsin z))) (at z)"
proof -
  have "(sin (Arcsin z))2  1"
    using assms one_minus_z2_notin_nonpos_Reals by force
  then have "cos (Arcsin z)  0"
    by (metis diff_0_right power_zero_numeral sin_squared_eq)
  then show ?thesis
    by (rule has_field_derivative_inverse_basic [OF DERIV_sin _ _ open_ball [of z 1]]) (auto intro: isCont_Arcsin assms)
qed

declare has_field_derivative_Arcsin [derivative_intros]
declare has_field_derivative_Arcsin [THEN DERIV_chain2, derivative_intros]

lemma field_differentiable_at_Arcsin:
    "(Im z = 0  ¦Re z¦ < 1)  Arcsin field_differentiable at z"
  using field_differentiable_def has_field_derivative_Arcsin by blast

lemma field_differentiable_within_Arcsin:
    "(Im z = 0  ¦Re z¦ < 1)  Arcsin field_differentiable (at z within s)"
  using field_differentiable_at_Arcsin field_differentiable_within_subset by blast

lemma continuous_within_Arcsin:
    "(Im z = 0  ¦Re z¦ < 1)  continuous (at z within s) Arcsin"
  using continuous_at_imp_continuous_within isCont_Arcsin by blast

lemma continuous_on_Arcsin [continuous_intros]:
    "(z. z  s  Im z = 0  ¦Re z¦ < 1)  continuous_on s Arcsin"
  by (simp add: continuous_at_imp_continuous_on)

lemma holomorphic_on_Arcsin: "(z. z  s  Im z = 0  ¦Re z¦ < 1)  Arcsin holomorphic_on s"
  by (simp add: field_differentiable_within_Arcsin holomorphic_on_def)


subsection‹Inverse Cosine›

definitiontag important› Arccos :: "complex  complex" where
   "Arccos  λz. -𝗂 * Ln(z + 𝗂 * csqrt(1 - z2))"

lemma Arccos_range_lemma: "¦Re z¦ < 1  0 < Im(z + 𝗂 * csqrt(1 - z2))"
  using Arcsin_range_lemma [of "-z"]  by simp

lemma Arccos_body_lemma: "z + 𝗂 * csqrt(1 - z2)  0"
  by (metis Arcsin_body_lemma complex_i_mult_minus diff_0 diff_eq_eq power2_minus)

lemma Re_Arccos: "Re(Arccos z) = Im (Ln (z + 𝗂 * csqrt(1 - z2)))"
  by (simp add: Arccos_def)

lemma Im_Arccos: "Im(Arccos z) = - ln (cmod (z + 𝗂 * csqrt (1 - z2)))"
  by (simp add: Arccos_def Arccos_body_lemma)

text‹A very tricky argument to find!›
lemma isCont_Arccos_lemma:
  assumes eq0: "Im (z + 𝗂 * csqrt (1 - z2)) = 0" and "Im z = 0  ¦Re z¦ < 1"
    shows False
proof (cases "Im z = 0")
  case True
  then show ?thesis
    using assms by (fastforce simp add: cmod_def abs_square_less_1 [symmetric])
next
  case False
  have Imz: "Im z = - sqrt ((1 + ((Im z)2 + cmod (1 - z2)) - (Re z)2) / 2)"
    using eq0 abs_Re_le_cmod [of "1-z2"]
    by (simp add: Re_power2 algebra_simps)
  have "(cmod z)2 - 1  cmod (1 - z2)"
  proof (clarsimp simp add: cmod_def)
    assume "(Re z)2 + (Im z)2 - 1 = sqrt ((1 - Re (z2))2 + (Im (z2))2)"
    then have "((Re z)2 + (Im z)2 - 1)2 = ((1 - Re (z2))2 + (Im (z2))2)"
      by simp
    then show False using False
      by (simp add: power2_eq_square algebra_simps)
  qed
  moreover have "(Im z)2 = (1 + ((Im z)2 + cmod (1 - z2)) - (Re z)2) / 2"
    using abs_Re_le_cmod [of "1-z2"] by (subst Imz) (simp add: Re_power2)
  ultimately show False
    by (simp add: cmod_power2)
qed

lemma isCont_Arccos:
  assumes "(Im z = 0  ¦Re z¦ < 1)"
    shows "isCont Arccos z"
proof -
  have "z + 𝗂 * csqrt (1 - z2)  0"
    by (metis complex_nonpos_Reals_iff isCont_Arccos_lemma assms)
  with assms show ?thesis
    unfolding Arccos_def
    by (simp_all add: one_minus_z2_notin_nonpos_Reals assms)
qed

lemma isCont_Arccos' [simp]:
  "isCont f z  (Im (f z) = 0  ¦Re (f z)¦ < 1)  isCont (λx. Arccos (f x)) z"
  by (blast intro: isCont_o2 [OF _ isCont_Arccos])

lemma cos_Arccos [simp]: "cos(Arccos z) = z"
proof -
  have "z*2 + 𝗂 * (2 * csqrt (1 - z2)) = 0  z*2 + 𝗂 * csqrt (1 - z2)*2 = 0"
    by (simp add: algebra_simps)  ― ‹Cancelling a factor of 2›
  moreover have "  z + 𝗂 * csqrt (1 - z2) = 0"
    by (metis distrib_right mult_eq_0_iff zero_neq_numeral)
  ultimately show ?thesis
    by (simp add: cos_exp_eq Arccos_def Arccos_body_lemma exp_minus field_simps flip: power2_eq_square)
qed

lemma Arccos_cos:
    assumes "0 < Re z  Re z < pi 
             Re z = 0  0  Im z 
             Re z = pi  Im z  0"
      shows "Arccos(cos z) = z"
proof -
  have *: "((𝗂 - (exp (𝗂 * z))2 * 𝗂) / (2 * exp (𝗂 * z))) = sin z"
    by (simp add: sin_exp_eq exp_minus field_simps power2_eq_square)
  have "1 - (exp (𝗂 * z) + inverse (exp (𝗂 * z)))2 / 4 = ((𝗂 - (exp (𝗂 * z))2 * 𝗂) / (2 * exp (𝗂 * z)))2"
    by (simp add: field_simps power2_eq_square)
  then have "Arccos(cos z) = - (𝗂 * Ln ((exp (𝗂 * z) + inverse (exp (𝗂 * z))) / 2 +
                           𝗂 * csqrt (((𝗂 - (exp (𝗂 * z))2 * 𝗂) / (2 * exp (𝗂 * z)))2)))"
    by (simp add: cos_exp_eq Arccos_def exp_minus power_divide)
  also have " = - (𝗂 * Ln ((exp (𝗂 * z) + inverse (exp (𝗂 * z))) / 2 +
                              𝗂 * ((𝗂 - (exp (𝗂 * z))2 * 𝗂) / (2 * exp (𝗂 * z)))))"
    apply (subst csqrt_square)
    using assms Re_sin_pos [of z] Im_sin_nonneg [of z] Im_sin_nonneg2 [of z]
    by (auto simp: * Re_sin Im_sin)
  also have " =  - (𝗂 * Ln (exp (𝗂*z)))"
    by (simp add: field_simps power2_eq_square)
  also have " = z"
    using assms
    by (subst Complex_Transcendental.Ln_exp, auto)
  finally show ?thesis .
qed

lemma Arccos_unique:
    "cos z = w;
      0 < Re z  Re z < pi 
      Re z = 0  0  Im z 
      Re z = pi  Im z  0  Arccos w = z"
  using Arccos_cos by blast

lemma Arccos_0 [simp]: "Arccos 0 = pi/2"
  by (rule Arccos_unique) auto

lemma Arccos_1 [simp]: "Arccos 1 = 0"
  by (rule Arccos_unique) auto

lemma Arccos_minus1: "Arccos(-1) = pi"
  by (rule Arccos_unique) auto

lemma has_field_derivative_Arccos:
  assumes "(Im z = 0  ¦Re z¦ < 1)"
    shows "(Arccos has_field_derivative - inverse(sin(Arccos z))) (at z)"
proof -
  have "x2  -1" for x::real
    by (sos "((R<1 + (([~1] * A=0) + (R<1 * (R<1 * [x__]^2)))))")
  with assms have "(cos (Arccos z))2  1"
    by (auto simp: complex_eq_iff Re_power2 Im_power2 abs_square_eq_1)
  then have "- sin (Arccos z)  0"
    by (metis cos_squared_eq diff_0_right mult_zero_left neg_0_equal_iff_equal power2_eq_square)
  then have "(Arccos has_field_derivative inverse(- sin(Arccos z))) (at z)"
    by (rule has_field_derivative_inverse_basic [OF DERIV_cos _ _ open_ball [of z 1]])
       (auto intro: isCont_Arccos assms)
  then show ?thesis
    by simp
qed

declare has_field_derivative_Arcsin [derivative_intros]
declare has_field_derivative_Arcsin [THEN DERIV_chain2, derivative_intros]

lemma field_differentiable_at_Arccos:
    "(Im z = 0  ¦Re z¦ < 1)  Arccos field_differentiable at z"
  using field_differentiable_def has_field_derivative_Arccos by blast

lemma field_differentiable_within_Arccos:
    "(Im z = 0  ¦Re z¦ < 1)  Arccos field_differentiable (at z within s)"
  using field_differentiable_at_Arccos field_differentiable_within_subset by blast

lemma continuous_within_Arccos:
    "(Im z = 0  ¦Re z¦ < 1)  continuous (at z within s) Arccos"
  using continuous_at_imp_continuous_within isCont_Arccos by blast

lemma continuous_on_Arccos [continuous_intros]:
    "(z. z  s  Im z = 0  ¦Re z¦ < 1)  continuous_on s Arccos"
  by (simp add: continuous_at_imp_continuous_on)

lemma holomorphic_on_Arccos: "(z. z  s  Im z = 0  ¦Re z¦ < 1)  Arccos holomorphic_on s"
  by (simp add: field_differentiable_within_Arccos holomorphic_on_def)

subsectiontag unimportant›‹Upper and Lower Bounds for Inverse Sine and Cosine›

lemma Arcsin_bounds: "¦Re z¦ < 1  ¦Re(Arcsin z)¦ < pi/2"
  unfolding Re_Arcsin
  by (blast intro: Re_Ln_pos_lt_imp Arcsin_range_lemma)

lemma Arccos_bounds: "¦Re z¦ < 1  0 < Re(Arccos z)  Re(Arccos z) < pi"
  unfolding Re_Arccos
  by (blast intro!: Im_Ln_pos_lt_imp Arccos_range_lemma)

lemma Re_Arccos_bounds: "-pi < Re(Arccos z)  Re(Arccos z)  pi"
  unfolding Re_Arccos
  by (blast intro!: mpi_less_Im_Ln Im_Ln_le_pi Arccos_body_lemma)

lemma Re_Arccos_bound: "¦Re(Arccos z)¦  pi"
  by (meson Re_Arccos_bounds abs_le_iff less_eq_real_def minus_less_iff)

lemma Im_Arccos_bound: "¦Im (Arccos w)¦  cmod w"
proof -
  have "(Im (Arccos w))2  (cmod (cos (Arccos w)))2 - (cos (Re (Arccos w)))2"
    using norm_cos_squared [of "Arccos w"] real_le_abs_sinh [of "Im (Arccos w)"]
    by (simp only: abs_le_square_iff) (simp add: field_split_simps)
  also have "  (cmod w)2"
    by (auto simp: cmod_power2)
  finally show ?thesis
    using abs_le_square_iff by force
qed

lemma Re_Arcsin_bounds: "-pi < Re(Arcsin z) & Re(Arcsin z)  pi"
  unfolding Re_Arcsin
  by (blast intro!: mpi_less_Im_Ln Im_Ln_le_pi Arcsin_body_lemma)

lemma Re_Arcsin_bound: "¦Re(Arcsin z)¦  pi"
  by (meson Re_Arcsin_bounds abs_le_iff less_eq_real_def minus_less_iff)

lemma norm_Arccos_bounded:
  fixes w :: complex
  shows "norm (Arccos w)  pi + norm w"
proof -
  have Re: "(Re (Arccos w))2  pi2" "(Im (Arccos w))2  (cmod w)2"
    using Re_Arccos_bound [of w] Im_Arccos_bound [of w] abs_le_square_iff by force+
  have "Arccos w  Arccos w  pi2 + (cmod w)2"
    using Re by (simp add: dot_square_norm cmod_power2 [of "Arccos w"])
  then have "cmod (Arccos w)  pi + cmod (cos (Arccos w))"
    by (smt (verit) Im_Arccos_bound Re_Arccos_bound cmod_le cos_Arccos)
  then show "cmod (Arccos w)  pi + cmod w"
    by auto
qed


subsectiontag unimportant›‹Interrelations between Arcsin and Arccos›

lemma cos_Arcsin_nonzero: "z2  1 cos(Arcsin z)  0"
  by (metis diff_0_right power_zero_numeral sin_Arcsin sin_squared_eq)

lemma sin_Arccos_nonzero: "z2  1 sin(Arccos z)  0"
  by (metis add.right_neutral cos_Arccos power2_eq_square power_zero_numeral sin_cos_squared_add3)

lemma cos_sin_csqrt:
  assumes "0 < cos(Re z)    cos(Re z) = 0  Im z * sin(Re z)  0"
    shows "cos z = csqrt(1 - (sin z)2)"
proof (rule csqrt_unique [THEN sym])
  show "(cos z)2 = 1 - (sin z)2"
    by (simp add: cos_squared_eq)
qed (use assms in auto simp: Re_cos Im_cos add_pos_pos mult_le_0_iff zero_le_mult_iff)

lemma sin_cos_csqrt:
  assumes "0 < sin(Re z)    sin(Re z) = 0  0  Im z * cos(Re z)"
    shows "sin z = csqrt(1 - (cos z)2)"
proof (rule csqrt_unique [THEN sym])
  show "(sin z)2 = 1 - (cos z)2"
    by (simp add: sin_squared_eq)
qed (use assms in auto simp: Re_sin Im_sin add_pos_pos mult_le_0_iff zero_le_mult_iff)

lemma Arcsin_Arccos_csqrt_pos:
    "(0 < Re z  Re z = 0  0  Im z)  Arcsin z = Arccos(csqrt(1 - z2))"
  by (simp add: Arcsin_def Arccos_def Complex.csqrt_square add.commute)

lemma Arccos_Arcsin_csqrt_pos:
    "(0 < Re z  Re z = 0  0  Im z)  Arccos z = Arcsin(csqrt(1 - z2))"
  by (simp add: Arcsin_def Arccos_def Complex.csqrt_square add.commute)

lemma sin_Arccos:
    "0 < Re z  Re z = 0  0  Im z  sin(Arccos z) = csqrt(1 - z2)"
  by (simp add: Arccos_Arcsin_csqrt_pos)

lemma cos_Arcsin:
    "0 < Re z  Re z = 0  0  Im z  cos(Arcsin z) = csqrt(1 - z2)"
  by (simp add: Arcsin_Arccos_csqrt_pos)


subsectiontag unimportant›‹Relationship with Arcsin on the Real Numbers›

lemma of_real_arcsin: "¦x¦  1  of_real(arcsin x) = Arcsin(of_real x)"
  by (smt (verit, best) Arcsin_sin Im_complex_of_real Re_complex_of_real arcsin sin_of_real)

lemma Im_Arcsin_of_real: "¦x¦  1  Im (Arcsin (of_real x)) = 0"
  by (metis Im_complex_of_real of_real_arcsin)

corollarytag unimportant› Arcsin_in_Reals [simp]: "z    ¦Re z¦  1  Arcsin z  "
  by (metis Im_Arcsin_of_real Re_complex_of_real Reals_cases complex_is_Real_iff)

lemma arcsin_eq_Re_Arcsin: "¦x¦  1  arcsin x = Re (Arcsin (of_real x))"
  by (metis Re_complex_of_real of_real_arcsin)


subsectiontag unimportant›‹Relationship with Arccos on the Real Numbers›

lemma of_real_arccos: "¦x¦  1  of_real(arccos x) = Arccos(of_real x)"
  by (smt (verit, del_insts) Arccos_unique Im_complex_of_real Re_complex_of_real arccos_lbound 
      arccos_ubound cos_arccos_abs cos_of_real)

lemma Im_Arccos_of_real: "¦x¦  1  Im (Arccos (of_real x)) = 0"
  by (metis Im_complex_of_real of_real_arccos)

corollarytag unimportant› Arccos_in_Reals [simp]: "z    ¦Re z¦  1  Arccos z  "
  by (metis Im_Arccos_of_real complex_is_Real_iff of_real_Re)

lemma arccos_eq_Re_Arccos: "¦x¦  1  arccos x = Re (Arccos (of_real x))"
  by (metis Re_complex_of_real of_real_arccos)

subsectiontag unimportant›‹Continuity results for arcsin and arccos›

lemma continuous_on_Arcsin_real [continuous_intros]:
    "continuous_on {w  . ¦Re w¦  1} Arcsin"
proof -
  have "continuous_on {w  . ¦Re w¦  1} (λx. complex_of_real (arcsin (Re x))) =
        continuous_on {w  . ¦Re w¦  1} (λx. complex_of_real (Re (Arcsin (of_real (Re x)))))"
    by (rule continuous_on_cong [OF refl]) (simp add: arcsin_eq_Re_Arcsin)
  also have " = ?thesis"
    by (rule continuous_on_cong [OF refl]) simp
  finally show ?thesis
    using continuous_on_arcsin [OF continuous_on_Re [OF continuous_on_id], of "{w  . ¦Re w¦  1}"]
          continuous_on_of_real
    by fastforce
qed

lemma continuous_within_Arcsin_real:
    "continuous (at z within {w  . ¦Re w¦  1}) Arcsin"
  using closed_real_abs_le continuous_on_Arcsin_real continuous_on_eq_continuous_within 
        continuous_within_closed_nontrivial by blast

lemma continuous_on_Arccos_real:
    "continuous_on {w  . ¦Re w¦  1} Arccos"
proof -
  have "continuous_on {w  . ¦Re w¦  1} (λx. complex_of_real (arccos (Re x))) =
        continuous_on {w  . ¦Re w¦  1} (λx. complex_of_real (Re (Arccos (of_real (Re x)))))"
    by (rule continuous_on_cong [OF refl]) (simp add: arccos_eq_Re_Arccos)
  also have " = ?thesis"
    by (rule continuous_on_cong [OF refl]) simp
  finally show ?thesis
    using continuous_on_arccos [OF continuous_on_Re [OF continuous_on_id], of "{w  . ¦Re w¦  1}"]
          continuous_on_of_real
    by fastforce
qed

lemma continuous_within_Arccos_real:
    "continuous (at z within {w  . ¦Re w¦  1}) Arccos"
  using closed_real_abs_le continuous_on_Arccos_real continuous_on_eq_continuous_within 
        continuous_within_closed_nontrivial by blast

lemma sinh_ln_complex: "x  0  sinh (ln x :: complex) = (x - inverse x) / 2"
  by (simp add: sinh_def exp_minus scaleR_conv_of_real exp_of_real)

lemma cosh_ln_complex: "x  0  cosh (ln x :: complex) = (x + inverse x) / 2"
  by (simp add: cosh_def exp_minus scaleR_conv_of_real)

lemma tanh_ln_complex: "x  0  tanh (ln x :: complex) = (x ^ 2 - 1) / (x ^ 2 + 1)"
  by (simp add: tanh_def sinh_ln_complex cosh_ln_complex divide_simps power2_eq_square)


subsection‹Roots of unity›

theorem complex_root_unity:
  fixes j::nat
  assumes "n  0"
    shows "exp(2 * of_real pi * 𝗂 * of_nat j / of_nat n)^n = 1"
  by (metis assms bot_nat_0.not_eq_extremum exp_divide_power_eq exp_of_nat2_mult exp_two_pi_i power_one)

lemma complex_root_unity_eq:
  fixes j::nat and k::nat
  assumes "1  n"
    shows "(exp(2 * of_real pi * 𝗂 * of_nat j / of_nat n) = exp(2 * of_real pi * 𝗂 * of_nat k / of_nat n)
            j mod n = k mod n)"
proof -
    have "(z::int. 𝗂 * (of_nat j * (of_real pi * 2)) =
               𝗂 * (of_nat k * (of_real pi * 2)) + 𝗂 * (of_int z * (of_nat n * (of_real pi * 2)))) 
          (z::int. of_nat j * (𝗂 * (of_real pi * 2)) =
              (of_nat k + of_nat n * of_int z) * (𝗂 * (of_real pi * 2)))"
      by (simp add: algebra_simps)
    also have "  (z::int. of_nat j = of_nat k + of_nat n * (of_int z :: complex))"
      by simp
    also have "  (z::int. of_nat j = of_nat k + of_nat n * z)"
      by (metis (mono_tags, opaque_lifting) of_int_add of_int_eq_iff of_int_mult of_int_of_nat_eq)
    also have "  int j mod int n = int k mod int n"
      by (auto simp: mod_eq_dvd_iff dvd_def algebra_simps)
    also have "  j mod n = k mod n"
      by (metis of_nat_eq_iff zmod_int)
    finally have "(z. 𝗂 * (of_nat j * (of_real pi * 2)) =
             𝗂 * (of_nat k * (of_real pi * 2)) + 𝗂 * (of_int z * (of_nat n * (of_real pi * 2))))  j mod n = k mod n" .
   note * = this
  show ?thesis
    using assms by (simp add: exp_eq field_split_simps *)
qed

corollary bij_betw_roots_unity:
    "bij_betw (λj. exp(2 * of_real pi * 𝗂 * of_nat j / of_nat n))
              {..<n}  {exp(2 * of_real pi * 𝗂 * of_nat j / of_nat n) | j. j < n}"
  by (auto simp: bij_betw_def inj_on_def complex_root_unity_eq)

lemma complex_root_unity_eq_1:
  fixes j::nat and k::nat
  assumes "1  n"
    shows "exp(2 * of_real pi * 𝗂 * of_nat j / of_nat n) = 1  n dvd j"
proof -
  have "1 = exp(2 * of_real pi * 𝗂 * (of_nat n / of_nat n))"
    using assms by simp
  then have "exp(2 * of_real pi * 𝗂 * (of_nat j / of_nat n)) = 1  j mod n = n mod n"
     using complex_root_unity_eq [of n j n] assms
     by simp
  then show ?thesis
    by auto
qed

lemma finite_complex_roots_unity_explicit:
  "finite {exp(2 * of_real pi * 𝗂 * of_nat j / of_nat n) | j::nat. j < n}"
  by simp

lemma card_complex_roots_unity_explicit:
     "card {exp(2 * of_real pi * 𝗂 * of_nat j / of_nat n) | j::nat. j < n} = n"
  by (simp add:  Finite_Set.bij_betw_same_card [OF bij_betw_roots_unity, symmetric])

lemma complex_roots_unity:
  assumes "1  n"
    shows "{z::complex. z^n = 1} = {exp(2 * of_real pi * 𝗂 * of_nat j / of_nat n) | j. j < n}"
  apply (rule Finite_Set.card_seteq [symmetric])
  using assms
  apply (auto simp: card_complex_roots_unity_explicit finite_roots_unity complex_root_unity card_roots_unity)
  done

lemma card_complex_roots_unity: "1  n  card {z::complex. z^n = 1} = n"
  by (simp add: card_complex_roots_unity_explicit complex_roots_unity)

lemma complex_not_root_unity:
    "1  n  u::complex. norm u = 1  u^n  1"
  apply (rule_tac x="exp (of_real pi * 𝗂 * of_real (1 / n))" in exI)
  apply (auto simp: Re_complex_div_eq_0 exp_of_nat_mult [symmetric] mult_ac exp_Euler)
  done

end