src/HOL/Analysis/Complex_Transcendental.thy
author immler
Tue Jul 04 09:36:25 2017 +0100 (2017-07-04)
changeset 66252 b73f94b366b7
parent 65719 7c57d79d61b7
child 66447 a1f5c5c26fa6
permissions -rw-r--r--
some generalizations complex=>real_normed_field
     1 section \<open>Complex Transcendental Functions\<close>
     2 
     3 text\<open>By John Harrison et al.  Ported from HOL Light by L C Paulson (2015)\<close>
     4 
     5 theory Complex_Transcendental
     6 imports
     7   Complex_Analysis_Basics
     8   Summation_Tests
     9    "~~/src/HOL/Library/Periodic_Fun"
    10 begin
    11 
    12 (* TODO: Figure out what to do with Möbius transformations *)
    13 definition "moebius a b c d = (\<lambda>z. (a*z+b) / (c*z+d :: 'a :: field))"
    14 
    15 lemma moebius_inverse:
    16   assumes "a * d \<noteq> b * c" "c * z + d \<noteq> 0"
    17   shows   "moebius d (-b) (-c) a (moebius a b c d z) = z"
    18 proof -
    19   from assms have "(-c) * moebius a b c d z + a \<noteq> 0" unfolding moebius_def
    20     by (simp add: field_simps)
    21   with assms show ?thesis
    22     unfolding moebius_def by (simp add: moebius_def divide_simps) (simp add: algebra_simps)?
    23 qed
    24 
    25 lemma moebius_inverse':
    26   assumes "a * d \<noteq> b * c" "c * z - a \<noteq> 0"
    27   shows   "moebius a b c d (moebius d (-b) (-c) a z) = z"
    28   using assms moebius_inverse[of d a "-b" "-c" z]
    29   by (auto simp: algebra_simps)
    30 
    31 lemma cmod_add_real_less:
    32   assumes "Im z \<noteq> 0" "r\<noteq>0"
    33     shows "cmod (z + r) < cmod z + \<bar>r\<bar>"
    34 proof (cases z)
    35   case (Complex x y)
    36   have "r * x / \<bar>r\<bar> < sqrt (x*x + y*y)"
    37     apply (rule real_less_rsqrt)
    38     using assms
    39     apply (simp add: Complex power2_eq_square)
    40     using not_real_square_gt_zero by blast
    41   then show ?thesis using assms Complex
    42     apply (auto simp: cmod_def)
    43     apply (rule power2_less_imp_less, auto)
    44     apply (simp add: power2_eq_square field_simps)
    45     done
    46 qed
    47 
    48 lemma cmod_diff_real_less: "Im z \<noteq> 0 \<Longrightarrow> x\<noteq>0 \<Longrightarrow> cmod (z - x) < cmod z + \<bar>x\<bar>"
    49   using cmod_add_real_less [of z "-x"]
    50   by simp
    51 
    52 lemma cmod_square_less_1_plus:
    53   assumes "Im z = 0 \<Longrightarrow> \<bar>Re z\<bar> < 1"
    54     shows "(cmod z)\<^sup>2 < 1 + cmod (1 - z\<^sup>2)"
    55   using assms
    56   apply (cases "Im z = 0 \<or> Re z = 0")
    57   using abs_square_less_1
    58     apply (force simp add: Re_power2 Im_power2 cmod_def)
    59   using cmod_diff_real_less [of "1 - z\<^sup>2" "1"]
    60   apply (simp add: norm_power Im_power2)
    61   done
    62 
    63 subsection\<open>The Exponential Function is Differentiable and Continuous\<close>
    64 
    65 lemma field_differentiable_within_exp: "exp field_differentiable (at z within s)"
    66   using DERIV_exp field_differentiable_at_within field_differentiable_def by blast
    67 
    68 lemma continuous_within_exp:
    69   fixes z::"'a::{real_normed_field,banach}"
    70   shows "continuous (at z within s) exp"
    71 by (simp add: continuous_at_imp_continuous_within)
    72 
    73 lemma holomorphic_on_exp [holomorphic_intros]: "exp holomorphic_on s"
    74   by (simp add: field_differentiable_within_exp holomorphic_on_def)
    75 
    76 subsection\<open>Euler and de Moivre formulas.\<close>
    77 
    78 text\<open>The sine series times @{term i}\<close>
    79 lemma sin_i_eq: "(\<lambda>n. (\<i> * sin_coeff n) * z^n) sums (\<i> * sin z)"
    80 proof -
    81   have "(\<lambda>n. \<i> * sin_coeff n *\<^sub>R z^n) sums (\<i> * sin z)"
    82     using sin_converges sums_mult by blast
    83   then show ?thesis
    84     by (simp add: scaleR_conv_of_real field_simps)
    85 qed
    86 
    87 theorem exp_Euler: "exp(\<i> * z) = cos(z) + \<i> * sin(z)"
    88 proof -
    89   have "(\<lambda>n. (cos_coeff n + \<i> * sin_coeff n) * z^n)
    90         = (\<lambda>n. (\<i> * z) ^ n /\<^sub>R (fact n))"
    91   proof
    92     fix n
    93     show "(cos_coeff n + \<i> * sin_coeff n) * z^n = (\<i> * z) ^ n /\<^sub>R (fact n)"
    94       by (auto simp: cos_coeff_def sin_coeff_def scaleR_conv_of_real field_simps elim!: evenE oddE)
    95   qed
    96   also have "... sums (exp (\<i> * z))"
    97     by (rule exp_converges)
    98   finally have "(\<lambda>n. (cos_coeff n + \<i> * sin_coeff n) * z^n) sums (exp (\<i> * z))" .
    99   moreover have "(\<lambda>n. (cos_coeff n + \<i> * sin_coeff n) * z^n) sums (cos z + \<i> * sin z)"
   100     using sums_add [OF cos_converges [of z] sin_i_eq [of z]]
   101     by (simp add: field_simps scaleR_conv_of_real)
   102   ultimately show ?thesis
   103     using sums_unique2 by blast
   104 qed
   105 
   106 corollary exp_minus_Euler: "exp(-(\<i> * z)) = cos(z) - \<i> * sin(z)"
   107   using exp_Euler [of "-z"]
   108   by simp
   109 
   110 lemma sin_exp_eq: "sin z = (exp(\<i> * z) - exp(-(\<i> * z))) / (2*\<i>)"
   111   by (simp add: exp_Euler exp_minus_Euler)
   112 
   113 lemma sin_exp_eq': "sin z = \<i> * (exp(-(\<i> * z)) - exp(\<i> * z)) / 2"
   114   by (simp add: exp_Euler exp_minus_Euler)
   115 
   116 lemma cos_exp_eq:  "cos z = (exp(\<i> * z) + exp(-(\<i> * z))) / 2"
   117   by (simp add: exp_Euler exp_minus_Euler)
   118 
   119 subsection\<open>Relationships between real and complex trig functions\<close>
   120 
   121 lemma real_sin_eq [simp]:
   122   fixes x::real
   123   shows "Re(sin(of_real x)) = sin x"
   124   by (simp add: sin_of_real)
   125 
   126 lemma real_cos_eq [simp]:
   127   fixes x::real
   128   shows "Re(cos(of_real x)) = cos x"
   129   by (simp add: cos_of_real)
   130 
   131 lemma DeMoivre: "(cos z + \<i> * sin z) ^ n = cos(n * z) + \<i> * sin(n * z)"
   132   apply (simp add: exp_Euler [symmetric])
   133   by (metis exp_of_nat_mult mult.left_commute)
   134 
   135 lemma exp_cnj:
   136   fixes z::complex
   137   shows "cnj (exp z) = exp (cnj z)"
   138 proof -
   139   have "(\<lambda>n. cnj (z ^ n /\<^sub>R (fact n))) = (\<lambda>n. (cnj z)^n /\<^sub>R (fact n))"
   140     by auto
   141   also have "... sums (exp (cnj z))"
   142     by (rule exp_converges)
   143   finally have "(\<lambda>n. cnj (z ^ n /\<^sub>R (fact n))) sums (exp (cnj z))" .
   144   moreover have "(\<lambda>n. cnj (z ^ n /\<^sub>R (fact n))) sums (cnj (exp z))"
   145     by (metis exp_converges sums_cnj)
   146   ultimately show ?thesis
   147     using sums_unique2
   148     by blast
   149 qed
   150 
   151 lemma cnj_sin: "cnj(sin z) = sin(cnj z)"
   152   by (simp add: sin_exp_eq exp_cnj field_simps)
   153 
   154 lemma cnj_cos: "cnj(cos z) = cos(cnj z)"
   155   by (simp add: cos_exp_eq exp_cnj field_simps)
   156 
   157 lemma field_differentiable_at_sin: "sin field_differentiable at z"
   158   using DERIV_sin field_differentiable_def by blast
   159 
   160 lemma field_differentiable_within_sin: "sin field_differentiable (at z within s)"
   161   by (simp add: field_differentiable_at_sin field_differentiable_at_within)
   162 
   163 lemma field_differentiable_at_cos: "cos field_differentiable at z"
   164   using DERIV_cos field_differentiable_def by blast
   165 
   166 lemma field_differentiable_within_cos: "cos field_differentiable (at z within s)"
   167   by (simp add: field_differentiable_at_cos field_differentiable_at_within)
   168 
   169 lemma holomorphic_on_sin: "sin holomorphic_on s"
   170   by (simp add: field_differentiable_within_sin holomorphic_on_def)
   171 
   172 lemma holomorphic_on_cos: "cos holomorphic_on s"
   173   by (simp add: field_differentiable_within_cos holomorphic_on_def)
   174 
   175 subsection\<open>Get a nice real/imaginary separation in Euler's formula.\<close>
   176 
   177 lemma Euler: "exp(z) = of_real(exp(Re z)) *
   178               (of_real(cos(Im z)) + \<i> * of_real(sin(Im z)))"
   179 by (cases z) (simp add: exp_add exp_Euler cos_of_real exp_of_real sin_of_real Complex_eq)
   180 
   181 lemma Re_sin: "Re(sin z) = sin(Re z) * (exp(Im z) + exp(-(Im z))) / 2"
   182   by (simp add: sin_exp_eq field_simps Re_divide Im_exp)
   183 
   184 lemma Im_sin: "Im(sin z) = cos(Re z) * (exp(Im z) - exp(-(Im z))) / 2"
   185   by (simp add: sin_exp_eq field_simps Im_divide Re_exp)
   186 
   187 lemma Re_cos: "Re(cos z) = cos(Re z) * (exp(Im z) + exp(-(Im z))) / 2"
   188   by (simp add: cos_exp_eq field_simps Re_divide Re_exp)
   189 
   190 lemma Im_cos: "Im(cos z) = sin(Re z) * (exp(-(Im z)) - exp(Im z)) / 2"
   191   by (simp add: cos_exp_eq field_simps Im_divide Im_exp)
   192 
   193 lemma Re_sin_pos: "0 < Re z \<Longrightarrow> Re z < pi \<Longrightarrow> Re (sin z) > 0"
   194   by (auto simp: Re_sin Im_sin add_pos_pos sin_gt_zero)
   195 
   196 lemma Im_sin_nonneg: "Re z = 0 \<Longrightarrow> 0 \<le> Im z \<Longrightarrow> 0 \<le> Im (sin z)"
   197   by (simp add: Re_sin Im_sin algebra_simps)
   198 
   199 lemma Im_sin_nonneg2: "Re z = pi \<Longrightarrow> Im z \<le> 0 \<Longrightarrow> 0 \<le> Im (sin z)"
   200   by (simp add: Re_sin Im_sin algebra_simps)
   201 
   202 subsection\<open>More on the Polar Representation of Complex Numbers\<close>
   203 
   204 lemma exp_Complex: "exp(Complex r t) = of_real(exp r) * Complex (cos t) (sin t)"
   205   by (simp add: Complex_eq exp_add exp_Euler exp_of_real sin_of_real cos_of_real)
   206 
   207 lemma exp_eq_1: "exp z = 1 \<longleftrightarrow> Re(z) = 0 \<and> (\<exists>n::int. Im(z) = of_int (2 * n) * pi)"
   208                  (is "?lhs = ?rhs")
   209 proof 
   210   assume "exp z = 1"
   211   then have "Re z = 0"
   212     by (metis exp_eq_one_iff norm_exp_eq_Re norm_one)
   213   with \<open>?lhs\<close> show ?rhs
   214     by (metis Re_exp complex_Re_of_int cos_one_2pi_int exp_zero mult.commute mult_numeral_1 numeral_One of_int_mult of_int_numeral)
   215 next
   216   assume ?rhs then show ?lhs
   217     using Im_exp Re_exp complex_Re_Im_cancel_iff by force
   218 qed
   219 
   220 lemma exp_eq: "exp w = exp z \<longleftrightarrow> (\<exists>n::int. w = z + (of_int (2 * n) * pi) * \<i>)"
   221                 (is "?lhs = ?rhs")
   222 proof -
   223   have "exp w = exp z \<longleftrightarrow> exp (w-z) = 1"
   224     by (simp add: exp_diff)
   225   also have "... \<longleftrightarrow> (Re w = Re z \<and> (\<exists>n::int. Im w - Im z = of_int (2 * n) * pi))"
   226     by (simp add: exp_eq_1)
   227   also have "... \<longleftrightarrow> ?rhs"
   228     by (auto simp: algebra_simps intro!: complex_eqI)
   229   finally show ?thesis .
   230 qed
   231 
   232 lemma exp_complex_eqI: "\<bar>Im w - Im z\<bar> < 2*pi \<Longrightarrow> exp w = exp z \<Longrightarrow> w = z"
   233   by (auto simp: exp_eq abs_mult)
   234 
   235 lemma exp_integer_2pi:
   236   assumes "n \<in> \<int>"
   237   shows "exp((2 * n * pi) * \<i>) = 1"
   238 proof -
   239   have "exp((2 * n * pi) * \<i>) = exp 0"
   240     using assms
   241     by (simp only: Ints_def exp_eq) auto
   242   also have "... = 1"
   243     by simp
   244   finally show ?thesis .
   245 qed
   246 
   247 lemma exp_plus_2pin [simp]: "exp (z + \<i> * (of_int n * (of_real pi * 2))) = exp z"
   248   by (simp add: exp_eq)
   249 
   250 lemma inj_on_exp_pi:
   251   fixes z::complex shows "inj_on exp (ball z pi)"
   252 proof (clarsimp simp: inj_on_def exp_eq)
   253   fix y n
   254   assume "dist z (y + 2 * of_int n * of_real pi * \<i>) < pi"
   255          "dist z y < pi"
   256   then have "dist y (y + 2 * of_int n * of_real pi * \<i>) < pi+pi"
   257     using dist_commute_lessI dist_triangle_less_add by blast
   258   then have "norm (2 * of_int n * of_real pi * \<i>) < 2*pi"
   259     by (simp add: dist_norm)
   260   then show "n = 0"
   261     by (auto simp: norm_mult)
   262 qed
   263 
   264 lemma sin_cos_eq_iff: "sin y = sin x \<and> cos y = cos x \<longleftrightarrow> (\<exists>n::int. y = x + 2 * n * pi)"
   265 proof -
   266   { assume "sin y = sin x" "cos y = cos x"
   267     then have "cos (y-x) = 1"
   268       using cos_add [of y "-x"] by simp
   269     then have "\<exists>n::int. y-x = n * 2 * pi"
   270       using cos_one_2pi_int by blast }
   271   then show ?thesis
   272   apply (auto simp: sin_add cos_add)
   273   apply (metis add.commute diff_add_cancel mult.commute)
   274   done
   275 qed
   276 
   277 lemma exp_i_ne_1:
   278   assumes "0 < x" "x < 2*pi"
   279   shows "exp(\<i> * of_real x) \<noteq> 1"
   280 proof
   281   assume "exp (\<i> * of_real x) = 1"
   282   then have "exp (\<i> * of_real x) = exp 0"
   283     by simp
   284   then obtain n where "\<i> * of_real x = (of_int (2 * n) * pi) * \<i>"
   285     by (simp only: Ints_def exp_eq) auto
   286   then have  "of_real x = (of_int (2 * n) * pi)"
   287     by (metis complex_i_not_zero mult.commute mult_cancel_left of_real_eq_iff real_scaleR_def scaleR_conv_of_real)
   288   then have  "x = (of_int (2 * n) * pi)"
   289     by simp
   290   then show False using assms
   291     by (cases n) (auto simp: zero_less_mult_iff mult_less_0_iff)
   292 qed
   293 
   294 lemma sin_eq_0:
   295   fixes z::complex
   296   shows "sin z = 0 \<longleftrightarrow> (\<exists>n::int. z = of_real(n * pi))"
   297   by (simp add: sin_exp_eq exp_eq of_real_numeral)
   298 
   299 lemma cos_eq_0:
   300   fixes z::complex
   301   shows "cos z = 0 \<longleftrightarrow> (\<exists>n::int. z = of_real(n * pi) + of_real pi/2)"
   302   using sin_eq_0 [of "z - of_real pi/2"]
   303   by (simp add: sin_diff algebra_simps)
   304 
   305 lemma cos_eq_1:
   306   fixes z::complex
   307   shows "cos z = 1 \<longleftrightarrow> (\<exists>n::int. z = of_real(2 * n * pi))"
   308 proof -
   309   have "cos z = cos (2*(z/2))"
   310     by simp
   311   also have "... = 1 - 2 * sin (z/2) ^ 2"
   312     by (simp only: cos_double_sin)
   313   finally have [simp]: "cos z = 1 \<longleftrightarrow> sin (z/2) = 0"
   314     by simp
   315   show ?thesis
   316     by (auto simp: sin_eq_0 of_real_numeral)
   317 qed
   318 
   319 lemma csin_eq_1:
   320   fixes z::complex
   321   shows "sin z = 1 \<longleftrightarrow> (\<exists>n::int. z = of_real(2 * n * pi) + of_real pi/2)"
   322   using cos_eq_1 [of "z - of_real pi/2"]
   323   by (simp add: cos_diff algebra_simps)
   324 
   325 lemma csin_eq_minus1:
   326   fixes z::complex
   327   shows "sin z = -1 \<longleftrightarrow> (\<exists>n::int. z = of_real(2 * n * pi) + 3/2*pi)"
   328         (is "_ = ?rhs")
   329 proof -
   330   have "sin z = -1 \<longleftrightarrow> sin (-z) = 1"
   331     by (simp add: equation_minus_iff)
   332   also have "...  \<longleftrightarrow> (\<exists>n::int. -z = of_real(2 * n * pi) + of_real pi/2)"
   333     by (simp only: csin_eq_1)
   334   also have "...  \<longleftrightarrow> (\<exists>n::int. z = - of_real(2 * n * pi) - of_real pi/2)"
   335     apply (rule iff_exI)
   336     by (metis (no_types)  is_num_normalize(8) minus_minus of_real_def real_vector.scale_minus_left uminus_add_conv_diff)
   337   also have "... = ?rhs"
   338     apply (auto simp: of_real_numeral)
   339     apply (rule_tac [2] x="-(x+1)" in exI)
   340     apply (rule_tac x="-(x+1)" in exI)
   341     apply (simp_all add: algebra_simps)
   342     done
   343   finally show ?thesis .
   344 qed
   345 
   346 lemma ccos_eq_minus1:
   347   fixes z::complex
   348   shows "cos z = -1 \<longleftrightarrow> (\<exists>n::int. z = of_real(2 * n * pi) + pi)"
   349   using csin_eq_1 [of "z - of_real pi/2"]
   350   apply (simp add: sin_diff)
   351   apply (simp add: algebra_simps of_real_numeral equation_minus_iff)
   352   done
   353 
   354 lemma sin_eq_1: "sin x = 1 \<longleftrightarrow> (\<exists>n::int. x = (2 * n + 1 / 2) * pi)"
   355                 (is "_ = ?rhs")
   356 proof -
   357   have "sin x = 1 \<longleftrightarrow> sin (complex_of_real x) = 1"
   358     by (metis of_real_1 one_complex.simps(1) real_sin_eq sin_of_real)
   359   also have "...  \<longleftrightarrow> (\<exists>n::int. complex_of_real x = of_real(2 * n * pi) + of_real pi/2)"
   360     by (simp only: csin_eq_1)
   361   also have "...  \<longleftrightarrow> (\<exists>n::int. x = of_real(2 * n * pi) + of_real pi/2)"
   362     apply (rule iff_exI)
   363     apply (auto simp: algebra_simps of_real_numeral)
   364     apply (rule injD [OF inj_of_real [where 'a = complex]])
   365     apply (auto simp: of_real_numeral)
   366     done
   367   also have "... = ?rhs"
   368     by (auto simp: algebra_simps)
   369   finally show ?thesis .
   370 qed
   371 
   372 lemma sin_eq_minus1: "sin x = -1 \<longleftrightarrow> (\<exists>n::int. x = (2*n + 3/2) * pi)"  (is "_ = ?rhs")
   373 proof -
   374   have "sin x = -1 \<longleftrightarrow> sin (complex_of_real x) = -1"
   375     by (metis Re_complex_of_real of_real_def scaleR_minus1_left sin_of_real)
   376   also have "...  \<longleftrightarrow> (\<exists>n::int. complex_of_real x = of_real(2 * n * pi) + 3/2*pi)"
   377     by (simp only: csin_eq_minus1)
   378   also have "...  \<longleftrightarrow> (\<exists>n::int. x = of_real(2 * n * pi) + 3/2*pi)"
   379     apply (rule iff_exI)
   380     apply (auto simp: algebra_simps)
   381     apply (rule injD [OF inj_of_real [where 'a = complex]], auto)
   382     done
   383   also have "... = ?rhs"
   384     by (auto simp: algebra_simps)
   385   finally show ?thesis .
   386 qed
   387 
   388 lemma cos_eq_minus1: "cos x = -1 \<longleftrightarrow> (\<exists>n::int. x = (2*n + 1) * pi)"
   389                       (is "_ = ?rhs")
   390 proof -
   391   have "cos x = -1 \<longleftrightarrow> cos (complex_of_real x) = -1"
   392     by (metis Re_complex_of_real of_real_def scaleR_minus1_left cos_of_real)
   393   also have "...  \<longleftrightarrow> (\<exists>n::int. complex_of_real x = of_real(2 * n * pi) + pi)"
   394     by (simp only: ccos_eq_minus1)
   395   also have "...  \<longleftrightarrow> (\<exists>n::int. x = of_real(2 * n * pi) + pi)"
   396     apply (rule iff_exI)
   397     apply (auto simp: algebra_simps)
   398     apply (rule injD [OF inj_of_real [where 'a = complex]], auto)
   399     done
   400   also have "... = ?rhs"
   401     by (auto simp: algebra_simps)
   402   finally show ?thesis .
   403 qed
   404 
   405 lemma dist_exp_i_1: "norm(exp(\<i> * of_real t) - 1) = 2 * \<bar>sin(t / 2)\<bar>"
   406   apply (simp add: exp_Euler cmod_def power2_diff sin_of_real cos_of_real algebra_simps)
   407   using cos_double_sin [of "t/2"]
   408   apply (simp add: real_sqrt_mult)
   409   done
   410 
   411 
   412 lemma complex_sin_eq:
   413   fixes w :: complex
   414   shows "sin w = sin z \<longleftrightarrow> (\<exists>n \<in> \<int>. w = z + of_real(2*n*pi) \<or> w = -z + of_real((2*n + 1)*pi))"
   415         (is "?lhs = ?rhs")
   416 proof
   417   assume ?lhs
   418   then have "sin w - sin z = 0"
   419     by (auto simp: algebra_simps)
   420   then have "sin ((w - z) / 2)*cos ((w + z) / 2) = 0"
   421     by (auto simp: sin_diff_sin)
   422   then consider "sin ((w - z) / 2) = 0" | "cos ((w + z) / 2) = 0"
   423     using mult_eq_0_iff by blast
   424   then show ?rhs
   425   proof cases
   426     case 1
   427     then show ?thesis
   428       apply (auto simp: sin_eq_0 algebra_simps)
   429       by (metis Ints_of_int of_real_of_int_eq)
   430   next
   431     case 2
   432     then show ?thesis
   433       apply (auto simp: cos_eq_0 algebra_simps)
   434       by (metis Ints_of_int of_real_of_int_eq)
   435   qed
   436 next
   437   assume ?rhs
   438   then obtain n::int where w: "w = z + of_real (2* of_int n*pi) \<or>
   439                                w = -z + of_real ((2* of_int n + 1)*pi)"
   440     using Ints_cases by blast
   441   then show ?lhs
   442     using Periodic_Fun.sin.plus_of_int [of z n]
   443     apply (auto simp: algebra_simps)
   444     by (metis (no_types, hide_lams) add_diff_cancel_left add_diff_cancel_left' add_minus_cancel
   445               mult.commute sin.plus_of_int sin_minus sin_plus_pi)
   446 qed
   447 
   448 lemma complex_cos_eq:
   449   fixes w :: complex
   450   shows "cos w = cos z \<longleftrightarrow>
   451          (\<exists>n \<in> \<int>. w = z + of_real(2*n*pi) \<or> w = -z + of_real(2*n*pi))"
   452         (is "?lhs = ?rhs")
   453 proof
   454   assume ?lhs
   455   then have "cos w - cos z = 0"
   456     by (auto simp: algebra_simps)
   457   then have "sin ((w + z) / 2) * sin ((z - w) / 2) = 0"
   458     by (auto simp: cos_diff_cos)
   459   then consider "sin ((w + z) / 2) = 0" | "sin ((z - w) / 2) = 0"
   460     using mult_eq_0_iff by blast
   461   then show ?rhs
   462   proof cases
   463     case 1
   464     then show ?thesis
   465       apply (auto simp: sin_eq_0 algebra_simps)
   466       by (metis Ints_of_int of_real_of_int_eq)
   467   next
   468     case 2
   469     then show ?thesis
   470       apply (auto simp: sin_eq_0 algebra_simps)
   471       by (metis Ints_of_int add_minus_cancel distrib_right mult_of_int_commute mult_zero_right of_int_0 of_int_add of_real_of_int_eq)
   472   qed
   473 next
   474   assume ?rhs
   475   then obtain n::int where w: "w = z + of_real (2* of_int n*pi) \<or>
   476                                w = -z + of_real(2*n*pi)"
   477     using Ints_cases  by (metis of_int_mult of_int_numeral)
   478   then show ?lhs
   479     using Periodic_Fun.cos.plus_of_int [of z n]
   480     apply (auto simp: algebra_simps)
   481     by (metis cos.plus_of_int cos_minus minus_add_cancel mult.commute)
   482 qed
   483 
   484 lemma sin_eq:
   485    "sin x = sin y \<longleftrightarrow> (\<exists>n \<in> \<int>. x = y + 2*n*pi \<or> x = -y + (2*n + 1)*pi)"
   486   using complex_sin_eq [of x y]
   487   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)
   488 
   489 lemma cos_eq:
   490    "cos x = cos y \<longleftrightarrow> (\<exists>n \<in> \<int>. x = y + 2*n*pi \<or> x = -y + 2*n*pi)"
   491   using complex_cos_eq [of x y]
   492   by (simp only: cos_of_real Re_complex_of_real of_real_add [symmetric] of_real_minus [symmetric] of_real_mult [symmetric] of_real_eq_iff)
   493 
   494 lemma sinh_complex:
   495   fixes z :: complex
   496   shows "(exp z - inverse (exp z)) / 2 = -\<i> * sin(\<i> * z)"
   497   by (simp add: sin_exp_eq divide_simps exp_minus)
   498 
   499 lemma sin_i_times:
   500   fixes z :: complex
   501   shows "sin(\<i> * z) = \<i> * ((exp z - inverse (exp z)) / 2)"
   502   using sinh_complex by auto
   503 
   504 lemma sinh_real:
   505   fixes x :: real
   506   shows "of_real((exp x - inverse (exp x)) / 2) = -\<i> * sin(\<i> * of_real x)"
   507   by (simp add: exp_of_real sin_i_times)
   508 
   509 lemma cosh_complex:
   510   fixes z :: complex
   511   shows "(exp z + inverse (exp z)) / 2 = cos(\<i> * z)"
   512   by (simp add: cos_exp_eq divide_simps exp_minus exp_of_real)
   513 
   514 lemma cosh_real:
   515   fixes x :: real
   516   shows "of_real((exp x + inverse (exp x)) / 2) = cos(\<i> * of_real x)"
   517   by (simp add: cos_exp_eq divide_simps exp_minus exp_of_real)
   518 
   519 lemmas cos_i_times = cosh_complex [symmetric]
   520 
   521 lemma norm_cos_squared:
   522     "norm(cos z) ^ 2 = cos(Re z) ^ 2 + (exp(Im z) - inverse(exp(Im z))) ^ 2 / 4"
   523   apply (cases z)
   524   apply (simp add: cos_add cmod_power2 cos_of_real sin_of_real Complex_eq)
   525   apply (simp add: cos_exp_eq sin_exp_eq exp_minus exp_of_real Re_divide Im_divide power_divide)
   526   apply (simp only: left_diff_distrib [symmetric] power_mult_distrib)
   527   apply (simp add: sin_squared_eq)
   528   apply (simp add: power2_eq_square algebra_simps divide_simps)
   529   done
   530 
   531 lemma norm_sin_squared:
   532     "norm(sin z) ^ 2 = (exp(2 * Im z) + inverse(exp(2 * Im z)) - 2 * cos(2 * Re z)) / 4"
   533   apply (cases z)
   534   apply (simp add: sin_add cmod_power2 cos_of_real sin_of_real cos_double_cos exp_double Complex_eq)
   535   apply (simp add: cos_exp_eq sin_exp_eq exp_minus exp_of_real Re_divide Im_divide power_divide)
   536   apply (simp only: left_diff_distrib [symmetric] power_mult_distrib)
   537   apply (simp add: cos_squared_eq)
   538   apply (simp add: power2_eq_square algebra_simps divide_simps)
   539   done
   540 
   541 lemma exp_uminus_Im: "exp (- Im z) \<le> exp (cmod z)"
   542   using abs_Im_le_cmod linear order_trans by fastforce
   543 
   544 lemma norm_cos_le:
   545   fixes z::complex
   546   shows "norm(cos z) \<le> exp(norm z)"
   547 proof -
   548   have "Im z \<le> cmod z"
   549     using abs_Im_le_cmod abs_le_D1 by auto
   550   with exp_uminus_Im show ?thesis
   551     apply (simp add: cos_exp_eq norm_divide)
   552     apply (rule order_trans [OF norm_triangle_ineq], simp)
   553     apply (metis add_mono exp_le_cancel_iff mult_2_right)
   554     done
   555 qed
   556 
   557 lemma norm_cos_plus1_le:
   558   fixes z::complex
   559   shows "norm(1 + cos z) \<le> 2 * exp(norm z)"
   560 proof -
   561   have mono: "\<And>u w z::real. (1 \<le> w | 1 \<le> z) \<Longrightarrow> (w \<le> u & z \<le> u) \<Longrightarrow> 2 + w + z \<le> 4 * u"
   562       by arith
   563   have *: "Im z \<le> cmod z"
   564     using abs_Im_le_cmod abs_le_D1 by auto
   565   have triangle3: "\<And>x y z. norm(x + y + z) \<le> norm(x) + norm(y) + norm(z)"
   566     by (simp add: norm_add_rule_thm)
   567   have "norm(1 + cos z) = cmod (1 + (exp (\<i> * z) + exp (- (\<i> * z))) / 2)"
   568     by (simp add: cos_exp_eq)
   569   also have "... = cmod ((2 + exp (\<i> * z) + exp (- (\<i> * z))) / 2)"
   570     by (simp add: field_simps)
   571   also have "... = cmod (2 + exp (\<i> * z) + exp (- (\<i> * z))) / 2"
   572     by (simp add: norm_divide)
   573   finally show ?thesis
   574     apply (rule ssubst, simp)
   575     apply (rule order_trans [OF triangle3], simp)
   576     using exp_uminus_Im *
   577     apply (auto intro: mono)
   578     done
   579 qed
   580 
   581 subsection\<open>Taylor series for complex exponential, sine and cosine.\<close>
   582 
   583 declare power_Suc [simp del]
   584 
   585 lemma Taylor_exp_field:
   586   fixes z::"'a::{banach,real_normed_field}"
   587   shows "norm (exp z - (\<Sum>i\<le>n. z ^ i / fact i)) \<le> exp (norm z) * (norm z ^ Suc n) / fact n"
   588 proof (rule field_taylor[of _ n "\<lambda>k. exp" "exp (norm z)" 0 z, simplified])
   589   show "convex (closed_segment 0 z)"
   590     by (rule convex_closed_segment [of 0 z])
   591 next
   592   fix k x
   593   assume "x \<in> closed_segment 0 z" "k \<le> n"
   594   show "(exp has_field_derivative exp x) (at x within closed_segment 0 z)"
   595     using DERIV_exp DERIV_subset by blast
   596 next
   597   fix x
   598   assume x: "x \<in> closed_segment 0 z"
   599   have "norm (exp x) \<le> exp (norm x)"
   600     by (rule norm_exp)
   601   also have "norm x \<le> norm z"
   602     using x by (auto simp: closed_segment_def intro!: mult_left_le_one_le)
   603   finally show "norm (exp x) \<le> exp (norm z)"
   604     by simp
   605 next
   606   show "0 \<in> closed_segment 0 z"
   607     by (auto simp: closed_segment_def)
   608 next
   609   show "z \<in> closed_segment 0 z"
   610     apply (simp add: closed_segment_def scaleR_conv_of_real)
   611     using of_real_1 zero_le_one by blast
   612 qed
   613 
   614 lemma Taylor_exp:
   615   "norm(exp z - (\<Sum>k\<le>n. z ^ k / (fact k))) \<le> exp\<bar>Re z\<bar> * (norm z) ^ (Suc n) / (fact n)"
   616 proof (rule complex_taylor [of _ n "\<lambda>k. exp" "exp\<bar>Re z\<bar>" 0 z, simplified])
   617   show "convex (closed_segment 0 z)"
   618     by (rule convex_closed_segment [of 0 z])
   619 next
   620   fix k x
   621   assume "x \<in> closed_segment 0 z" "k \<le> n"
   622   show "(exp has_field_derivative exp x) (at x within closed_segment 0 z)"
   623     using DERIV_exp DERIV_subset by blast
   624 next
   625   fix x
   626   assume "x \<in> closed_segment 0 z"
   627   then show "Re x \<le> \<bar>Re z\<bar>"
   628     apply (auto simp: closed_segment_def scaleR_conv_of_real)
   629     by (meson abs_ge_self abs_ge_zero linear mult_left_le_one_le mult_nonneg_nonpos order_trans)
   630 next
   631   show "0 \<in> closed_segment 0 z"
   632     by (auto simp: closed_segment_def)
   633 next
   634   show "z \<in> closed_segment 0 z"
   635     apply (simp add: closed_segment_def scaleR_conv_of_real)
   636     using of_real_1 zero_le_one by blast
   637 qed
   638 
   639 lemma
   640   assumes "0 \<le> u" "u \<le> 1"
   641   shows cmod_sin_le_exp: "cmod (sin (u *\<^sub>R z)) \<le> exp \<bar>Im z\<bar>"
   642     and cmod_cos_le_exp: "cmod (cos (u *\<^sub>R z)) \<le> exp \<bar>Im z\<bar>"
   643 proof -
   644   have mono: "\<And>u w z::real. w \<le> u \<Longrightarrow> z \<le> u \<Longrightarrow> w + z \<le> u*2"
   645     by arith
   646   show "cmod (sin (u *\<^sub>R z)) \<le> exp \<bar>Im z\<bar>" using assms
   647     apply (auto simp: scaleR_conv_of_real norm_mult norm_power sin_exp_eq norm_divide)
   648     apply (rule order_trans [OF norm_triangle_ineq4])
   649     apply (rule mono)
   650     apply (auto simp: abs_if mult_left_le_one_le)
   651     apply (meson mult_nonneg_nonneg neg_le_0_iff_le not_le order_trans)
   652     apply (meson less_eq_real_def mult_nonneg_nonpos neg_0_le_iff_le order_trans)
   653     done
   654   show "cmod (cos (u *\<^sub>R z)) \<le> exp \<bar>Im z\<bar>" using assms
   655     apply (auto simp: scaleR_conv_of_real norm_mult norm_power cos_exp_eq norm_divide)
   656     apply (rule order_trans [OF norm_triangle_ineq])
   657     apply (rule mono)
   658     apply (auto simp: abs_if mult_left_le_one_le)
   659     apply (meson mult_nonneg_nonneg neg_le_0_iff_le not_le order_trans)
   660     apply (meson less_eq_real_def mult_nonneg_nonpos neg_0_le_iff_le order_trans)
   661     done
   662 qed
   663 
   664 lemma Taylor_sin:
   665   "norm(sin z - (\<Sum>k\<le>n. complex_of_real (sin_coeff k) * z ^ k))
   666    \<le> exp\<bar>Im z\<bar> * (norm z) ^ (Suc n) / (fact n)"
   667 proof -
   668   have mono: "\<And>u w z::real. w \<le> u \<Longrightarrow> z \<le> u \<Longrightarrow> w + z \<le> u*2"
   669       by arith
   670   have *: "cmod (sin z -
   671                  (\<Sum>i\<le>n. (-1) ^ (i div 2) * (if even i then sin 0 else cos 0) * z ^ i / (fact i)))
   672            \<le> exp \<bar>Im z\<bar> * cmod z ^ Suc n / (fact n)"
   673   proof (rule complex_taylor [of "closed_segment 0 z" n
   674                                  "\<lambda>k x. (-1)^(k div 2) * (if even k then sin x else cos x)"
   675                                  "exp\<bar>Im z\<bar>" 0 z,  simplified])
   676     fix k x
   677     show "((\<lambda>x. (- 1) ^ (k div 2) * (if even k then sin x else cos x)) has_field_derivative
   678             (- 1) ^ (Suc k div 2) * (if odd k then sin x else cos x))
   679             (at x within closed_segment 0 z)"
   680       apply (auto simp: power_Suc)
   681       apply (intro derivative_eq_intros | simp)+
   682       done
   683   next
   684     fix x
   685     assume "x \<in> closed_segment 0 z"
   686     then show "cmod ((- 1) ^ (Suc n div 2) * (if odd n then sin x else cos x)) \<le> exp \<bar>Im z\<bar>"
   687       by (auto simp: closed_segment_def norm_mult norm_power cmod_sin_le_exp cmod_cos_le_exp)
   688   qed
   689   have **: "\<And>k. complex_of_real (sin_coeff k) * z ^ k
   690             = (-1)^(k div 2) * (if even k then sin 0 else cos 0) * z^k / of_nat (fact k)"
   691     by (auto simp: sin_coeff_def elim!: oddE)
   692   show ?thesis
   693     apply (rule order_trans [OF _ *])
   694     apply (simp add: **)
   695     done
   696 qed
   697 
   698 lemma Taylor_cos:
   699   "norm(cos z - (\<Sum>k\<le>n. complex_of_real (cos_coeff k) * z ^ k))
   700    \<le> exp\<bar>Im z\<bar> * (norm z) ^ Suc n / (fact n)"
   701 proof -
   702   have mono: "\<And>u w z::real. w \<le> u \<Longrightarrow> z \<le> u \<Longrightarrow> w + z \<le> u*2"
   703       by arith
   704   have *: "cmod (cos z -
   705                  (\<Sum>i\<le>n. (-1) ^ (Suc i div 2) * (if even i then cos 0 else sin 0) * z ^ i / (fact i)))
   706            \<le> exp \<bar>Im z\<bar> * cmod z ^ Suc n / (fact n)"
   707   proof (rule complex_taylor [of "closed_segment 0 z" n "\<lambda>k x. (-1)^(Suc k div 2) * (if even k then cos x else sin x)" "exp\<bar>Im z\<bar>" 0 z,
   708 simplified])
   709     fix k x
   710     assume "x \<in> closed_segment 0 z" "k \<le> n"
   711     show "((\<lambda>x. (- 1) ^ (Suc k div 2) * (if even k then cos x else sin x)) has_field_derivative
   712             (- 1) ^ Suc (k div 2) * (if odd k then cos x else sin x))
   713              (at x within closed_segment 0 z)"
   714       apply (auto simp: power_Suc)
   715       apply (intro derivative_eq_intros | simp)+
   716       done
   717   next
   718     fix x
   719     assume "x \<in> closed_segment 0 z"
   720     then show "cmod ((- 1) ^ Suc (n div 2) * (if odd n then cos x else sin x)) \<le> exp \<bar>Im z\<bar>"
   721       by (auto simp: closed_segment_def norm_mult norm_power cmod_sin_le_exp cmod_cos_le_exp)
   722   qed
   723   have **: "\<And>k. complex_of_real (cos_coeff k) * z ^ k
   724             = (-1)^(Suc k div 2) * (if even k then cos 0 else sin 0) * z^k / of_nat (fact k)"
   725     by (auto simp: cos_coeff_def elim!: evenE)
   726   show ?thesis
   727     apply (rule order_trans [OF _ *])
   728     apply (simp add: **)
   729     done
   730 qed
   731 
   732 declare power_Suc [simp]
   733 
   734 text\<open>32-bit Approximation to e\<close>
   735 lemma e_approx_32: "\<bar>exp(1) - 5837465777 / 2147483648\<bar> \<le> (inverse(2 ^ 32)::real)"
   736   using Taylor_exp [of 1 14] exp_le
   737   apply (simp add: sum_distrib_right in_Reals_norm Re_exp atMost_nat_numeral fact_numeral)
   738   apply (simp only: pos_le_divide_eq [symmetric], linarith)
   739   done
   740 
   741 lemma e_less_272: "exp 1 < (272/100::real)"
   742   using e_approx_32
   743   by (simp add: abs_if split: if_split_asm)
   744 
   745 lemma ln_272_gt_1: "ln (272/100) > (1::real)"
   746   by (metis e_less_272 exp_less_cancel_iff exp_ln_iff less_trans ln_exp)
   747 
   748 text\<open>Apparently redundant. But many arguments involve integers.\<close>
   749 lemma ln3_gt_1: "ln 3 > (1::real)"
   750   by (simp add: less_trans [OF ln_272_gt_1])
   751 
   752 subsection\<open>The argument of a complex number\<close>
   753 
   754 definition Arg :: "complex \<Rightarrow> real" where
   755  "Arg z \<equiv> if z = 0 then 0
   756            else THE t. 0 \<le> t \<and> t < 2*pi \<and>
   757                     z = of_real(norm z) * exp(\<i> * of_real t)"
   758 
   759 lemma Arg_0 [simp]: "Arg(0) = 0"
   760   by (simp add: Arg_def)
   761 
   762 lemma Arg_unique_lemma:
   763   assumes z:  "z = of_real(norm z) * exp(\<i> * of_real t)"
   764       and z': "z = of_real(norm z) * exp(\<i> * of_real t')"
   765       and t:  "0 \<le> t"  "t < 2*pi"
   766       and t': "0 \<le> t'" "t' < 2*pi"
   767       and nz: "z \<noteq> 0"
   768   shows "t' = t"
   769 proof -
   770   have [dest]: "\<And>x y z::real. x\<ge>0 \<Longrightarrow> x+y < z \<Longrightarrow> y<z"
   771     by arith
   772   have "of_real (cmod z) * exp (\<i> * of_real t') = of_real (cmod z) * exp (\<i> * of_real t)"
   773     by (metis z z')
   774   then have "exp (\<i> * of_real t') = exp (\<i> * of_real t)"
   775     by (metis nz mult_left_cancel mult_zero_left z)
   776   then have "sin t' = sin t \<and> cos t' = cos t"
   777     apply (simp add: exp_Euler sin_of_real cos_of_real)
   778     by (metis Complex_eq complex.sel)
   779   then obtain n::int where n: "t' = t + 2 * n * pi"
   780     by (auto simp: sin_cos_eq_iff)
   781   then have "n=0"
   782     apply (rule_tac z=n in int_cases)
   783     using t t'
   784     apply (auto simp: mult_less_0_iff algebra_simps)
   785     done
   786   then show "t' = t"
   787       by (simp add: n)
   788 qed
   789 
   790 lemma Arg: "0 \<le> Arg z & Arg z < 2*pi & z = of_real(norm z) * exp(\<i> * of_real(Arg z))"
   791 proof (cases "z=0")
   792   case True then show ?thesis
   793     by (simp add: Arg_def)
   794 next
   795   case False
   796   obtain t where t: "0 \<le> t" "t < 2*pi"
   797              and ReIm: "Re z / cmod z = cos t" "Im z / cmod z = sin t"
   798     using sincos_total_2pi [OF complex_unit_circle [OF False]]
   799     by blast
   800   have z: "z = of_real(norm z) * exp(\<i> * of_real t)"
   801     apply (rule complex_eqI)
   802     using t False ReIm
   803     apply (auto simp: exp_Euler sin_of_real cos_of_real divide_simps)
   804     done
   805   show ?thesis
   806     apply (simp add: Arg_def False)
   807     apply (rule theI [where a=t])
   808     using t z False
   809     apply (auto intro: Arg_unique_lemma)
   810     done
   811 qed
   812 
   813 corollary
   814   shows Arg_ge_0: "0 \<le> Arg z"
   815     and Arg_lt_2pi: "Arg z < 2*pi"
   816     and Arg_eq: "z = of_real(norm z) * exp(\<i> * of_real(Arg z))"
   817   using Arg by auto
   818 
   819 lemma complex_norm_eq_1_exp: "norm z = 1 \<longleftrightarrow> exp(\<i> * of_real (Arg z)) = z"
   820   by (metis Arg_eq cis_conv_exp mult.left_neutral norm_cis of_real_1)
   821 
   822 lemma Arg_unique: "\<lbrakk>of_real r * exp(\<i> * of_real a) = z; 0 < r; 0 \<le> a; a < 2*pi\<rbrakk> \<Longrightarrow> Arg z = a"
   823   apply (rule Arg_unique_lemma [OF _ Arg_eq])
   824   using Arg [of z]
   825   apply (auto simp: norm_mult)
   826   done
   827 
   828 lemma Arg_minus: "z \<noteq> 0 \<Longrightarrow> Arg (-z) = (if Arg z < pi then Arg z + pi else Arg z - pi)"
   829   apply (rule Arg_unique [of "norm z"])
   830   apply (rule complex_eqI)
   831   using Arg_ge_0 [of z] Arg_eq [of z] Arg_lt_2pi [of z] Arg_eq [of z]
   832   apply auto
   833   apply (auto simp: Re_exp Im_exp cos_diff sin_diff cis_conv_exp [symmetric])
   834   apply (metis Re_rcis Im_rcis rcis_def)+
   835   done
   836 
   837 lemma Arg_times_of_real [simp]: "0 < r \<Longrightarrow> Arg (of_real r * z) = Arg z"
   838   apply (cases "z=0", simp)
   839   apply (rule Arg_unique [of "r * norm z"])
   840   using Arg
   841   apply auto
   842   done
   843 
   844 lemma Arg_times_of_real2 [simp]: "0 < r \<Longrightarrow> Arg (z * of_real r) = Arg z"
   845   by (metis Arg_times_of_real mult.commute)
   846 
   847 lemma Arg_divide_of_real [simp]: "0 < r \<Longrightarrow> Arg (z / of_real r) = Arg z"
   848   by (metis Arg_times_of_real2 less_numeral_extra(3) nonzero_eq_divide_eq of_real_eq_0_iff)
   849 
   850 lemma Arg_le_pi: "Arg z \<le> pi \<longleftrightarrow> 0 \<le> Im z"
   851 proof (cases "z=0")
   852   case True then show ?thesis
   853     by simp
   854 next
   855   case False
   856   have "0 \<le> Im z \<longleftrightarrow> 0 \<le> Im (of_real (cmod z) * exp (\<i> * complex_of_real (Arg z)))"
   857     by (metis Arg_eq)
   858   also have "... = (0 \<le> Im (exp (\<i> * complex_of_real (Arg z))))"
   859     using False
   860     by (simp add: zero_le_mult_iff)
   861   also have "... \<longleftrightarrow> Arg z \<le> pi"
   862     by (simp add: Im_exp) (metis Arg_ge_0 Arg_lt_2pi sin_lt_zero sin_ge_zero not_le)
   863   finally show ?thesis
   864     by blast
   865 qed
   866 
   867 lemma Arg_lt_pi: "0 < Arg z \<and> Arg z < pi \<longleftrightarrow> 0 < Im z"
   868 proof (cases "z=0")
   869   case True then show ?thesis
   870     by simp
   871 next
   872   case False
   873   have "0 < Im z \<longleftrightarrow> 0 < Im (of_real (cmod z) * exp (\<i> * complex_of_real (Arg z)))"
   874     by (metis Arg_eq)
   875   also have "... = (0 < Im (exp (\<i> * complex_of_real (Arg z))))"
   876     using False
   877     by (simp add: zero_less_mult_iff)
   878   also have "... \<longleftrightarrow> 0 < Arg z \<and> Arg z < pi"
   879     using Arg_ge_0  Arg_lt_2pi sin_le_zero sin_gt_zero
   880     apply (auto simp: Im_exp)
   881     using le_less apply fastforce
   882     using not_le by blast
   883   finally show ?thesis
   884     by blast
   885 qed
   886 
   887 lemma Arg_eq_0: "Arg z = 0 \<longleftrightarrow> z \<in> \<real> \<and> 0 \<le> Re z"
   888 proof (cases "z=0")
   889   case True then show ?thesis
   890     by simp
   891 next
   892   case False
   893   have "z \<in> \<real> \<and> 0 \<le> Re z \<longleftrightarrow> z \<in> \<real> \<and> 0 \<le> Re (of_real (cmod z) * exp (\<i> * complex_of_real (Arg z)))"
   894     by (metis Arg_eq)
   895   also have "... \<longleftrightarrow> z \<in> \<real> \<and> 0 \<le> Re (exp (\<i> * complex_of_real (Arg z)))"
   896     using False
   897     by (simp add: zero_le_mult_iff)
   898   also have "... \<longleftrightarrow> Arg z = 0"
   899     apply (auto simp: Re_exp)
   900     apply (metis Arg_lt_pi Arg_ge_0 Arg_le_pi cos_pi complex_is_Real_iff leD less_linear less_minus_one_simps(2) minus_minus neg_less_eq_nonneg order_refl)
   901     using Arg_eq [of z]
   902     apply (auto simp: Reals_def)
   903     done
   904   finally show ?thesis
   905     by blast
   906 qed
   907 
   908 corollary Arg_gt_0:
   909   assumes "z \<in> \<real> \<Longrightarrow> Re z < 0"
   910     shows "Arg z > 0"
   911   using Arg_eq_0 Arg_ge_0 assms dual_order.strict_iff_order by fastforce
   912 
   913 lemma Arg_of_real: "Arg(of_real x) = 0 \<longleftrightarrow> 0 \<le> x"
   914   by (simp add: Arg_eq_0)
   915 
   916 lemma Arg_eq_pi: "Arg z = pi \<longleftrightarrow> z \<in> \<real> \<and> Re z < 0"
   917   apply  (cases "z=0", simp)
   918   using Arg_eq_0 [of "-z"]
   919   apply (auto simp: complex_is_Real_iff Arg_minus)
   920   apply (simp add: complex_Re_Im_cancel_iff)
   921   apply (metis Arg_minus pi_gt_zero add.left_neutral minus_minus minus_zero)
   922   done
   923 
   924 lemma Arg_eq_0_pi: "Arg z = 0 \<or> Arg z = pi \<longleftrightarrow> z \<in> \<real>"
   925   using Arg_eq_0 Arg_eq_pi not_le by auto
   926 
   927 lemma Arg_inverse: "Arg(inverse z) = (if z \<in> \<real> \<and> 0 \<le> Re z then Arg z else 2*pi - Arg z)"
   928   apply (cases "z=0", simp)
   929   apply (rule Arg_unique [of "inverse (norm z)"])
   930   using Arg_ge_0 [of z] Arg_lt_2pi [of z] Arg_eq [of z] Arg_eq_0 [of z] exp_two_pi_i
   931   apply (auto simp: of_real_numeral algebra_simps exp_diff divide_simps)
   932   done
   933 
   934 lemma Arg_eq_iff:
   935   assumes "w \<noteq> 0" "z \<noteq> 0"
   936      shows "Arg w = Arg z \<longleftrightarrow> (\<exists>x. 0 < x & w = of_real x * z)"
   937   using assms Arg_eq [of z] Arg_eq [of w]
   938   apply auto
   939   apply (rule_tac x="norm w / norm z" in exI)
   940   apply (simp add: divide_simps)
   941   by (metis mult.commute mult.left_commute)
   942 
   943 lemma Arg_inverse_eq_0: "Arg(inverse z) = 0 \<longleftrightarrow> Arg z = 0"
   944   using complex_is_Real_iff
   945   apply (simp add: Arg_eq_0)
   946   apply (auto simp: divide_simps not_sum_power2_lt_zero)
   947   done
   948 
   949 lemma Arg_divide:
   950   assumes "w \<noteq> 0" "z \<noteq> 0" "Arg w \<le> Arg z"
   951     shows "Arg(z / w) = Arg z - Arg w"
   952   apply (rule Arg_unique [of "norm(z / w)"])
   953   using assms Arg_eq [of z] Arg_eq [of w] Arg_ge_0 [of w] Arg_lt_2pi [of z]
   954   apply (auto simp: exp_diff norm_divide algebra_simps divide_simps)
   955   done
   956 
   957 lemma Arg_le_div_sum:
   958   assumes "w \<noteq> 0" "z \<noteq> 0" "Arg w \<le> Arg z"
   959     shows "Arg z = Arg w + Arg(z / w)"
   960   by (simp add: Arg_divide assms)
   961 
   962 lemma Arg_le_div_sum_eq:
   963   assumes "w \<noteq> 0" "z \<noteq> 0"
   964     shows "Arg w \<le> Arg z \<longleftrightarrow> Arg z = Arg w + Arg(z / w)"
   965   using assms
   966   by (auto simp: Arg_ge_0 intro: Arg_le_div_sum)
   967 
   968 lemma Arg_diff:
   969   assumes "w \<noteq> 0" "z \<noteq> 0"
   970     shows "Arg w - Arg z = (if Arg z \<le> Arg w then Arg(w / z) else Arg(w/z) - 2*pi)"
   971   using assms
   972   apply (auto simp: Arg_ge_0 Arg_divide not_le)
   973   using Arg_divide [of w z] Arg_inverse [of "w/z"]
   974   apply auto
   975   by (metis Arg_eq_0 less_irrefl minus_diff_eq right_minus_eq)
   976 
   977 lemma Arg_add:
   978   assumes "w \<noteq> 0" "z \<noteq> 0"
   979     shows "Arg w + Arg z = (if Arg w + Arg z < 2*pi then Arg(w * z) else Arg(w * z) + 2*pi)"
   980   using assms
   981   using Arg_diff [of "w*z" z] Arg_le_div_sum_eq [of z "w*z"]
   982   apply (auto simp: Arg_ge_0 Arg_divide not_le)
   983   apply (metis Arg_lt_2pi add.commute)
   984   apply (metis (no_types) Arg add.commute diff_0 diff_add_cancel diff_less_eq diff_minus_eq_add not_less)
   985   done
   986 
   987 lemma Arg_times:
   988   assumes "w \<noteq> 0" "z \<noteq> 0"
   989     shows "Arg (w * z) = (if Arg w + Arg z < 2*pi then Arg w + Arg z
   990                             else (Arg w + Arg z) - 2*pi)"
   991   using Arg_add [OF assms]
   992   by auto
   993 
   994 lemma Arg_cnj: "Arg(cnj z) = (if z \<in> \<real> \<and> 0 \<le> Re z then Arg z else 2*pi - Arg z)"
   995   apply (cases "z=0", simp)
   996   apply (rule trans [of _ "Arg(inverse z)"])
   997   apply (simp add: Arg_eq_iff divide_simps complex_norm_square [symmetric] mult.commute)
   998   apply (metis norm_eq_zero of_real_power zero_less_power2)
   999   apply (auto simp: of_real_numeral Arg_inverse)
  1000   done
  1001 
  1002 lemma Arg_real: "z \<in> \<real> \<Longrightarrow> Arg z = (if 0 \<le> Re z then 0 else pi)"
  1003   using Arg_eq_0 Arg_eq_0_pi
  1004   by auto
  1005 
  1006 lemma Arg_exp: "0 \<le> Im z \<Longrightarrow> Im z < 2*pi \<Longrightarrow> Arg(exp z) = Im z"
  1007   by (rule Arg_unique [of  "exp(Re z)"]) (auto simp: exp_eq_polar)
  1008 
  1009 lemma complex_split_polar:
  1010   obtains r a::real where "z = complex_of_real r * (cos a + \<i> * sin a)" "0 \<le> r" "0 \<le> a" "a < 2*pi"
  1011   using Arg cis.ctr cis_conv_exp unfolding Complex_eq by fastforce
  1012 
  1013 lemma Re_Im_le_cmod: "Im w * sin \<phi> + Re w * cos \<phi> \<le> cmod w"
  1014 proof (cases w rule: complex_split_polar)
  1015   case (1 r a) with sin_cos_le1 [of a \<phi>] show ?thesis
  1016     apply (simp add: norm_mult cmod_unit_one)
  1017     by (metis (no_types, hide_lams) abs_le_D1 distrib_left mult.commute mult.left_commute mult_left_le)
  1018 qed
  1019 
  1020 subsection\<open>Analytic properties of tangent function\<close>
  1021 
  1022 lemma cnj_tan: "cnj(tan z) = tan(cnj z)"
  1023   by (simp add: cnj_cos cnj_sin tan_def)
  1024 
  1025 lemma field_differentiable_at_tan: "~(cos z = 0) \<Longrightarrow> tan field_differentiable at z"
  1026   unfolding field_differentiable_def
  1027   using DERIV_tan by blast
  1028 
  1029 lemma field_differentiable_within_tan: "~(cos z = 0)
  1030          \<Longrightarrow> tan field_differentiable (at z within s)"
  1031   using field_differentiable_at_tan field_differentiable_at_within by blast
  1032 
  1033 lemma continuous_within_tan: "~(cos z = 0) \<Longrightarrow> continuous (at z within s) tan"
  1034   using continuous_at_imp_continuous_within isCont_tan by blast
  1035 
  1036 lemma continuous_on_tan [continuous_intros]: "(\<And>z. z \<in> s \<Longrightarrow> ~(cos z = 0)) \<Longrightarrow> continuous_on s tan"
  1037   by (simp add: continuous_at_imp_continuous_on)
  1038 
  1039 lemma holomorphic_on_tan: "(\<And>z. z \<in> s \<Longrightarrow> ~(cos z = 0)) \<Longrightarrow> tan holomorphic_on s"
  1040   by (simp add: field_differentiable_within_tan holomorphic_on_def)
  1041 
  1042 
  1043 subsection\<open>Complex logarithms (the conventional principal value)\<close>
  1044 
  1045 instantiation complex :: ln
  1046 begin
  1047 
  1048 definition ln_complex :: "complex \<Rightarrow> complex"
  1049   where "ln_complex \<equiv> \<lambda>z. THE w. exp w = z & -pi < Im(w) & Im(w) \<le> pi"
  1050 
  1051 text\<open>NOTE: within this scope, the constant Ln is not yet available!\<close>
  1052 lemma
  1053   assumes "z \<noteq> 0"
  1054     shows exp_Ln [simp]:  "exp(ln z) = z"
  1055       and mpi_less_Im_Ln: "-pi < Im(ln z)"
  1056       and Im_Ln_le_pi:    "Im(ln z) \<le> pi"
  1057 proof -
  1058   obtain \<psi> where z: "z / (cmod z) = Complex (cos \<psi>) (sin \<psi>)"
  1059     using complex_unimodular_polar [of "z / (norm z)"] assms
  1060     by (auto simp: norm_divide divide_simps)
  1061   obtain \<phi> where \<phi>: "- pi < \<phi>" "\<phi> \<le> pi" "sin \<phi> = sin \<psi>" "cos \<phi> = cos \<psi>"
  1062     using sincos_principal_value [of "\<psi>"] assms
  1063     by (auto simp: norm_divide divide_simps)
  1064   have "exp(ln z) = z & -pi < Im(ln z) & Im(ln z) \<le> pi" unfolding ln_complex_def
  1065     apply (rule theI [where a = "Complex (ln(norm z)) \<phi>"])
  1066     using z assms \<phi>
  1067     apply (auto simp: field_simps exp_complex_eqI exp_eq_polar cis.code)
  1068     done
  1069   then show "exp(ln z) = z" "-pi < Im(ln z)" "Im(ln z) \<le> pi"
  1070     by auto
  1071 qed
  1072 
  1073 lemma Ln_exp [simp]:
  1074   assumes "-pi < Im(z)" "Im(z) \<le> pi"
  1075     shows "ln(exp z) = z"
  1076   apply (rule exp_complex_eqI)
  1077   using assms mpi_less_Im_Ln  [of "exp z"] Im_Ln_le_pi [of "exp z"]
  1078   apply auto
  1079   done
  1080 
  1081 subsection\<open>Relation to Real Logarithm\<close>
  1082 
  1083 lemma Ln_of_real:
  1084   assumes "0 < z"
  1085     shows "ln(of_real z::complex) = of_real(ln z)"
  1086 proof -
  1087   have "ln(of_real (exp (ln z))::complex) = ln (exp (of_real (ln z)))"
  1088     by (simp add: exp_of_real)
  1089   also have "... = of_real(ln z)"
  1090     using assms
  1091     by (subst Ln_exp) auto
  1092   finally show ?thesis
  1093     using assms by simp
  1094 qed
  1095 
  1096 corollary Ln_in_Reals [simp]: "z \<in> \<real> \<Longrightarrow> Re z > 0 \<Longrightarrow> ln z \<in> \<real>"
  1097   by (auto simp: Ln_of_real elim: Reals_cases)
  1098 
  1099 corollary Im_Ln_of_real [simp]: "r > 0 \<Longrightarrow> Im (ln (of_real r)) = 0"
  1100   by (simp add: Ln_of_real)
  1101 
  1102 lemma cmod_Ln_Reals [simp]: "z \<in> \<real> \<Longrightarrow> 0 < Re z \<Longrightarrow> cmod (ln z) = norm (ln (Re z))"
  1103   using Ln_of_real by force
  1104 
  1105 lemma Ln_Reals_eq: "\<lbrakk>x \<in> \<real>; Re x > 0\<rbrakk> \<Longrightarrow> ln x = of_real (ln (Re x))"
  1106   using Ln_of_real by force
  1107 
  1108 lemma Ln_1 [simp]: "ln 1 = (0::complex)"
  1109 proof -
  1110   have "ln (exp 0) = (0::complex)"
  1111     by (metis (mono_tags, hide_lams) Ln_of_real exp_zero ln_one of_real_0 of_real_1 zero_less_one)
  1112   then show ?thesis
  1113     by simp                              
  1114 qed
  1115 
  1116   
  1117 lemma Ln_eq_zero_iff [simp]: "x \<notin> \<real>\<^sub>\<le>\<^sub>0 \<Longrightarrow> ln x = 0 \<longleftrightarrow> x = 1" for x::complex
  1118   by auto (metis exp_Ln exp_zero nonpos_Reals_zero_I)
  1119 
  1120 instance
  1121   by intro_classes (rule ln_complex_def Ln_1)
  1122 
  1123 end
  1124 
  1125 abbreviation Ln :: "complex \<Rightarrow> complex"
  1126   where "Ln \<equiv> ln"
  1127 
  1128 lemma Ln_eq_iff: "w \<noteq> 0 \<Longrightarrow> z \<noteq> 0 \<Longrightarrow> (Ln w = Ln z \<longleftrightarrow> w = z)"
  1129   by (metis exp_Ln)
  1130 
  1131 lemma Ln_unique: "exp(z) = w \<Longrightarrow> -pi < Im(z) \<Longrightarrow> Im(z) \<le> pi \<Longrightarrow> Ln w = z"
  1132   using Ln_exp by blast
  1133 
  1134 lemma Re_Ln [simp]: "z \<noteq> 0 \<Longrightarrow> Re(Ln z) = ln(norm z)"
  1135   by (metis exp_Ln ln_exp norm_exp_eq_Re)
  1136 
  1137 corollary ln_cmod_le:
  1138   assumes z: "z \<noteq> 0"
  1139     shows "ln (cmod z) \<le> cmod (Ln z)"
  1140   using norm_exp [of "Ln z", simplified exp_Ln [OF z]]
  1141   by (metis Re_Ln complex_Re_le_cmod z)
  1142 
  1143 proposition exists_complex_root:
  1144   fixes z :: complex
  1145   assumes "n \<noteq> 0"  obtains w where "z = w ^ n"
  1146   apply (cases "z=0")
  1147   using assms apply (simp add: power_0_left)
  1148   apply (rule_tac w = "exp(Ln z / n)" in that)
  1149   apply (auto simp: assms exp_of_nat_mult [symmetric])
  1150   done
  1151 
  1152 corollary exists_complex_root_nonzero:
  1153   fixes z::complex
  1154   assumes "z \<noteq> 0" "n \<noteq> 0"
  1155   obtains w where "w \<noteq> 0" "z = w ^ n"
  1156   by (metis exists_complex_root [of n z] assms power_0_left)
  1157 
  1158 subsection\<open>The Unwinding Number and the Ln-product Formula\<close>
  1159 
  1160 text\<open>Note that in this special case the unwinding number is -1, 0 or 1.\<close>
  1161 
  1162 definition unwinding :: "complex \<Rightarrow> complex" where
  1163    "unwinding(z) = (z - Ln(exp z)) / (of_real(2*pi) * \<i>)"
  1164 
  1165 lemma unwinding_2pi: "(2*pi) * \<i> * unwinding(z) = z - Ln(exp z)"
  1166   by (simp add: unwinding_def)
  1167 
  1168 lemma Ln_times_unwinding:
  1169     "w \<noteq> 0 \<Longrightarrow> z \<noteq> 0 \<Longrightarrow> Ln(w * z) = Ln(w) + Ln(z) - (2*pi) * \<i> * unwinding(Ln w + Ln z)"
  1170   using unwinding_2pi by (simp add: exp_add)
  1171 
  1172 
  1173 subsection\<open>Derivative of Ln away from the branch cut\<close>
  1174 
  1175 lemma
  1176   assumes "z \<notin> \<real>\<^sub>\<le>\<^sub>0"
  1177     shows has_field_derivative_Ln: "(Ln has_field_derivative inverse(z)) (at z)"
  1178       and Im_Ln_less_pi:           "Im (Ln z) < pi"
  1179 proof -
  1180   have znz: "z \<noteq> 0"
  1181     using assms by auto
  1182   then have "Im (Ln z) \<noteq> pi"
  1183     by (metis (no_types) Im_exp Ln_in_Reals assms complex_nonpos_Reals_iff complex_is_Real_iff exp_Ln mult_zero_right not_less pi_neq_zero sin_pi znz)
  1184   then show *: "Im (Ln z) < pi" using assms Im_Ln_le_pi
  1185     by (simp add: le_neq_trans znz)
  1186   have "(exp has_field_derivative z) (at (Ln z))"
  1187     by (metis znz DERIV_exp exp_Ln)
  1188   then show "(Ln has_field_derivative inverse(z)) (at z)"
  1189     apply (rule has_complex_derivative_inverse_strong_x
  1190               [where s = "{w. -pi < Im(w) \<and> Im(w) < pi}"])
  1191     using znz *
  1192     apply (auto simp: Transcendental.continuous_on_exp [OF continuous_on_id] open_Collect_conj open_halfspace_Im_gt open_halfspace_Im_lt mpi_less_Im_Ln)
  1193     done
  1194 qed
  1195 
  1196 declare has_field_derivative_Ln [derivative_intros]
  1197 declare has_field_derivative_Ln [THEN DERIV_chain2, derivative_intros]
  1198 
  1199 lemma field_differentiable_at_Ln: "z \<notin> \<real>\<^sub>\<le>\<^sub>0 \<Longrightarrow> Ln field_differentiable at z"
  1200   using field_differentiable_def has_field_derivative_Ln by blast
  1201 
  1202 lemma field_differentiable_within_Ln: "z \<notin> \<real>\<^sub>\<le>\<^sub>0
  1203          \<Longrightarrow> Ln field_differentiable (at z within s)"
  1204   using field_differentiable_at_Ln field_differentiable_within_subset by blast
  1205 
  1206 lemma continuous_at_Ln: "z \<notin> \<real>\<^sub>\<le>\<^sub>0 \<Longrightarrow> continuous (at z) Ln"
  1207   by (simp add: field_differentiable_imp_continuous_at field_differentiable_within_Ln)
  1208 
  1209 lemma isCont_Ln' [simp]:
  1210    "\<lbrakk>isCont f z; f z \<notin> \<real>\<^sub>\<le>\<^sub>0\<rbrakk> \<Longrightarrow> isCont (\<lambda>x. Ln (f x)) z"
  1211   by (blast intro: isCont_o2 [OF _ continuous_at_Ln])
  1212 
  1213 lemma continuous_within_Ln: "z \<notin> \<real>\<^sub>\<le>\<^sub>0 \<Longrightarrow> continuous (at z within s) Ln"
  1214   using continuous_at_Ln continuous_at_imp_continuous_within by blast
  1215 
  1216 lemma continuous_on_Ln [continuous_intros]: "(\<And>z. z \<in> s \<Longrightarrow> z \<notin> \<real>\<^sub>\<le>\<^sub>0) \<Longrightarrow> continuous_on s Ln"
  1217   by (simp add: continuous_at_imp_continuous_on continuous_within_Ln)
  1218 
  1219 lemma holomorphic_on_Ln: "(\<And>z. z \<in> s \<Longrightarrow> z \<notin> \<real>\<^sub>\<le>\<^sub>0) \<Longrightarrow> Ln holomorphic_on s"
  1220   by (simp add: field_differentiable_within_Ln holomorphic_on_def)
  1221 
  1222 lemma divide_ln_mono:
  1223   fixes x y::real
  1224   assumes "3 \<le> x" "x \<le> y"
  1225   shows "x / ln x \<le> y / ln y"
  1226 proof (rule exE [OF complex_mvt_line [of x y "\<lambda>z. z / Ln z" "\<lambda>z. 1/(Ln z) - 1/(Ln z)^2"]];
  1227     clarsimp simp add: closed_segment_Reals closed_segment_eq_real_ivl assms)
  1228   show "\<And>u. \<lbrakk>x \<le> u; u \<le> y\<rbrakk> \<Longrightarrow> ((\<lambda>z. z / Ln z) has_field_derivative 1 / Ln u - 1 / (Ln u)\<^sup>2) (at u)"
  1229     using \<open>3 \<le> x\<close> apply -
  1230     apply (rule derivative_eq_intros | simp)+
  1231     apply (force simp: field_simps power_eq_if)
  1232     done
  1233   show "x / ln x \<le> y / ln y"
  1234     if "Re (y / Ln y) - Re (x / Ln x) = (Re (1 / Ln u) - Re (1 / (Ln u)\<^sup>2)) * (y - x)"
  1235     and x: "x \<le> u" "u \<le> y" for u
  1236   proof -
  1237     have eq: "y / ln y = (1 / ln u - 1 / (ln u)\<^sup>2) * (y - x) + x / ln x"
  1238       using that \<open>3 \<le> x\<close> by (auto simp: Ln_Reals_eq in_Reals_norm group_add_class.diff_eq_eq)
  1239     show ?thesis
  1240       using exp_le \<open>3 \<le> x\<close> x by (simp add: eq) (simp add: power_eq_if divide_simps ln_ge_iff)
  1241   qed
  1242 qed
  1243     
  1244 
  1245 subsection\<open>Quadrant-type results for Ln\<close>
  1246 
  1247 lemma cos_lt_zero_pi: "pi/2 < x \<Longrightarrow> x < 3*pi/2 \<Longrightarrow> cos x < 0"
  1248   using cos_minus_pi cos_gt_zero_pi [of "x-pi"]
  1249   by simp
  1250 
  1251 lemma Re_Ln_pos_lt:
  1252   assumes "z \<noteq> 0"
  1253     shows "\<bar>Im(Ln z)\<bar> < pi/2 \<longleftrightarrow> 0 < Re(z)"
  1254 proof -
  1255   { fix w
  1256     assume "w = Ln z"
  1257     then have w: "Im w \<le> pi" "- pi < Im w"
  1258       using Im_Ln_le_pi [of z]  mpi_less_Im_Ln [of z]  assms
  1259       by auto
  1260     then have "\<bar>Im w\<bar> < pi/2 \<longleftrightarrow> 0 < Re(exp w)"
  1261       apply (auto simp: Re_exp zero_less_mult_iff cos_gt_zero_pi)
  1262       using cos_lt_zero_pi [of "-(Im w)"] cos_lt_zero_pi [of "(Im w)"]
  1263       apply (simp add: abs_if split: if_split_asm)
  1264       apply (metis (no_types) cos_minus cos_pi_half eq_divide_eq_numeral1(1) eq_numeral_simps(4)
  1265                less_numeral_extra(3) linorder_neqE_linordered_idom minus_mult_minus minus_mult_right
  1266                mult_numeral_1_right)
  1267       done
  1268   }
  1269   then show ?thesis using assms
  1270     by auto
  1271 qed
  1272 
  1273 lemma Re_Ln_pos_le:
  1274   assumes "z \<noteq> 0"
  1275     shows "\<bar>Im(Ln z)\<bar> \<le> pi/2 \<longleftrightarrow> 0 \<le> Re(z)"
  1276 proof -
  1277   { fix w
  1278     assume "w = Ln z"
  1279     then have w: "Im w \<le> pi" "- pi < Im w"
  1280       using Im_Ln_le_pi [of z]  mpi_less_Im_Ln [of z]  assms
  1281       by auto
  1282     then have "\<bar>Im w\<bar> \<le> pi/2 \<longleftrightarrow> 0 \<le> Re(exp w)"
  1283       apply (auto simp: Re_exp zero_le_mult_iff cos_ge_zero)
  1284       using cos_lt_zero_pi [of "- (Im w)"] cos_lt_zero_pi [of "(Im w)"] not_le
  1285       apply (auto simp: abs_if split: if_split_asm)
  1286       done
  1287   }
  1288   then show ?thesis using assms
  1289     by auto
  1290 qed
  1291 
  1292 lemma Im_Ln_pos_lt:
  1293   assumes "z \<noteq> 0"
  1294     shows "0 < Im(Ln z) \<and> Im(Ln z) < pi \<longleftrightarrow> 0 < Im(z)"
  1295 proof -
  1296   { fix w
  1297     assume "w = Ln z"
  1298     then have w: "Im w \<le> pi" "- pi < Im w"
  1299       using Im_Ln_le_pi [of z]  mpi_less_Im_Ln [of z]  assms
  1300       by auto
  1301     then have "0 < Im w \<and> Im w < pi \<longleftrightarrow> 0 < Im(exp w)"
  1302       using sin_gt_zero [of "- (Im w)"] sin_gt_zero [of "(Im w)"]
  1303       apply (auto simp: Im_exp zero_less_mult_iff)
  1304       using less_linear apply fastforce
  1305       using less_linear apply fastforce
  1306       done
  1307   }
  1308   then show ?thesis using assms
  1309     by auto
  1310 qed
  1311 
  1312 lemma Im_Ln_pos_le:
  1313   assumes "z \<noteq> 0"
  1314     shows "0 \<le> Im(Ln z) \<and> Im(Ln z) \<le> pi \<longleftrightarrow> 0 \<le> Im(z)"
  1315 proof -
  1316   { fix w
  1317     assume "w = Ln z"
  1318     then have w: "Im w \<le> pi" "- pi < Im w"
  1319       using Im_Ln_le_pi [of z]  mpi_less_Im_Ln [of z]  assms
  1320       by auto
  1321     then have "0 \<le> Im w \<and> Im w \<le> pi \<longleftrightarrow> 0 \<le> Im(exp w)"
  1322       using sin_ge_zero [of "- (Im w)"] sin_ge_zero [of "(Im w)"]
  1323       apply (auto simp: Im_exp zero_le_mult_iff sin_ge_zero)
  1324       apply (metis not_le not_less_iff_gr_or_eq pi_not_less_zero sin_eq_0_pi)
  1325       done }
  1326   then show ?thesis using assms
  1327     by auto
  1328 qed
  1329 
  1330 lemma Re_Ln_pos_lt_imp: "0 < Re(z) \<Longrightarrow> \<bar>Im(Ln z)\<bar> < pi/2"
  1331   by (metis Re_Ln_pos_lt less_irrefl zero_complex.simps(1))
  1332 
  1333 lemma Im_Ln_pos_lt_imp: "0 < Im(z) \<Longrightarrow> 0 < Im(Ln z) \<and> Im(Ln z) < pi"
  1334   by (metis Im_Ln_pos_lt not_le order_refl zero_complex.simps(2))
  1335 
  1336 text\<open>A reference to the set of positive real numbers\<close>
  1337 lemma Im_Ln_eq_0: "z \<noteq> 0 \<Longrightarrow> (Im(Ln z) = 0 \<longleftrightarrow> 0 < Re(z) \<and> Im(z) = 0)"
  1338 by (metis Im_complex_of_real Im_exp Ln_in_Reals Re_Ln_pos_lt Re_Ln_pos_lt_imp
  1339           Re_complex_of_real complex_is_Real_iff exp_Ln exp_of_real pi_gt_zero)
  1340 
  1341 lemma Im_Ln_eq_pi: "z \<noteq> 0 \<Longrightarrow> (Im(Ln z) = pi \<longleftrightarrow> Re(z) < 0 \<and> Im(z) = 0)"
  1342 by (metis Im_Ln_eq_0 Im_Ln_pos_le Im_Ln_pos_lt add.left_neutral complex_eq less_eq_real_def
  1343     mult_zero_right not_less_iff_gr_or_eq pi_ge_zero pi_neq_zero rcis_zero_arg rcis_zero_mod)
  1344 
  1345 
  1346 subsection\<open>More Properties of Ln\<close>
  1347 
  1348 lemma cnj_Ln: "z \<notin> \<real>\<^sub>\<le>\<^sub>0 \<Longrightarrow> cnj(Ln z) = Ln(cnj z)"
  1349   apply (cases "z=0", auto)
  1350   apply (rule exp_complex_eqI)
  1351   apply (auto simp: abs_if split: if_split_asm)
  1352   using Im_Ln_less_pi Im_Ln_le_pi apply force
  1353   apply (metis complex_cnj_zero_iff diff_minus_eq_add diff_strict_mono minus_less_iff
  1354           mpi_less_Im_Ln mult.commute mult_2_right)
  1355   by (metis exp_Ln exp_cnj)
  1356 
  1357 lemma Ln_inverse: "z \<notin> \<real>\<^sub>\<le>\<^sub>0 \<Longrightarrow> Ln(inverse z) = -(Ln z)"
  1358   apply (cases "z=0", auto)
  1359   apply (rule exp_complex_eqI)
  1360   using mpi_less_Im_Ln [of z] mpi_less_Im_Ln [of "inverse z"]
  1361   apply (auto simp: abs_if exp_minus split: if_split_asm)
  1362   apply (metis Im_Ln_less_pi Im_Ln_le_pi add.commute add_mono_thms_linordered_field(3) inverse_nonzero_iff_nonzero mult_2)
  1363   done
  1364 
  1365 lemma Ln_minus1 [simp]: "Ln(-1) = \<i> * pi"
  1366   apply (rule exp_complex_eqI)
  1367   using Im_Ln_le_pi [of "-1"] mpi_less_Im_Ln [of "-1"] cis_conv_exp cis_pi
  1368   apply (auto simp: abs_if)
  1369   done
  1370 
  1371 lemma Ln_ii [simp]: "Ln \<i> = \<i> * of_real pi/2"
  1372   using Ln_exp [of "\<i> * (of_real pi/2)"]
  1373   unfolding exp_Euler
  1374   by simp
  1375 
  1376 lemma Ln_minus_ii [simp]: "Ln(-\<i>) = - (\<i> * pi/2)"
  1377 proof -
  1378   have  "Ln(-\<i>) = Ln(inverse \<i>)"    by simp
  1379   also have "... = - (Ln \<i>)"         using Ln_inverse by blast
  1380   also have "... = - (\<i> * pi/2)"     by simp
  1381   finally show ?thesis .
  1382 qed
  1383 
  1384 lemma Ln_times:
  1385   assumes "w \<noteq> 0" "z \<noteq> 0"
  1386     shows "Ln(w * z) =
  1387                 (if Im(Ln w + Ln z) \<le> -pi then
  1388                   (Ln(w) + Ln(z)) + \<i> * of_real(2*pi)
  1389                 else if Im(Ln w + Ln z) > pi then
  1390                   (Ln(w) + Ln(z)) - \<i> * of_real(2*pi)
  1391                 else Ln(w) + Ln(z))"
  1392   using pi_ge_zero Im_Ln_le_pi [of w] Im_Ln_le_pi [of z]
  1393   using assms mpi_less_Im_Ln [of w] mpi_less_Im_Ln [of z]
  1394   by (auto simp: exp_add exp_diff sin_double cos_double exp_Euler intro!: Ln_unique)
  1395 
  1396 corollary Ln_times_simple:
  1397     "\<lbrakk>w \<noteq> 0; z \<noteq> 0; -pi < Im(Ln w) + Im(Ln z); Im(Ln w) + Im(Ln z) \<le> pi\<rbrakk>
  1398          \<Longrightarrow> Ln(w * z) = Ln(w) + Ln(z)"
  1399   by (simp add: Ln_times)
  1400 
  1401 corollary Ln_times_of_real:
  1402     "\<lbrakk>r > 0; z \<noteq> 0\<rbrakk> \<Longrightarrow> Ln(of_real r * z) = ln r + Ln(z)"
  1403   using mpi_less_Im_Ln Im_Ln_le_pi
  1404   by (force simp: Ln_times)
  1405 
  1406 corollary Ln_divide_of_real:
  1407     "\<lbrakk>r > 0; z \<noteq> 0\<rbrakk> \<Longrightarrow> Ln(z / of_real r) = Ln(z) - ln r"
  1408 using Ln_times_of_real [of "inverse r" z]
  1409 by (simp add: ln_inverse Ln_of_real mult.commute divide_inverse of_real_inverse [symmetric]
  1410          del: of_real_inverse)
  1411 
  1412 lemma Ln_minus:
  1413   assumes "z \<noteq> 0"
  1414     shows "Ln(-z) = (if Im(z) \<le> 0 \<and> ~(Re(z) < 0 \<and> Im(z) = 0)
  1415                      then Ln(z) + \<i> * pi
  1416                      else Ln(z) - \<i> * pi)" (is "_ = ?rhs")
  1417   using Im_Ln_le_pi [of z] mpi_less_Im_Ln [of z] assms
  1418         Im_Ln_eq_pi [of z] Im_Ln_pos_lt [of z]
  1419     by (fastforce simp: exp_add exp_diff exp_Euler intro!: Ln_unique)
  1420 
  1421 lemma Ln_inverse_if:
  1422   assumes "z \<noteq> 0"
  1423     shows "Ln (inverse z) = (if z \<in> \<real>\<^sub>\<le>\<^sub>0 then -(Ln z) + \<i> * 2 * complex_of_real pi else -(Ln z))"
  1424 proof (cases "z \<in> \<real>\<^sub>\<le>\<^sub>0")
  1425   case False then show ?thesis
  1426     by (simp add: Ln_inverse)
  1427 next
  1428   case True
  1429   then have z: "Im z = 0" "Re z < 0"
  1430     using assms
  1431     apply (auto simp: complex_nonpos_Reals_iff)
  1432     by (metis complex_is_Real_iff le_imp_less_or_eq of_real_0 of_real_Re)
  1433   have "Ln(inverse z) = Ln(- (inverse (-z)))"
  1434     by simp
  1435   also have "... = Ln (inverse (-z)) + \<i> * complex_of_real pi"
  1436     using assms z
  1437     apply (simp add: Ln_minus)
  1438     apply (simp add: field_simps)
  1439     done
  1440   also have "... = - Ln (- z) + \<i> * complex_of_real pi"
  1441     apply (subst Ln_inverse)
  1442     using z by (auto simp add: complex_nonneg_Reals_iff)
  1443   also have "... = - (Ln z) + \<i> * 2 * complex_of_real pi"
  1444     apply (subst Ln_minus [OF assms])
  1445     using assms z
  1446     apply simp
  1447     done
  1448   finally show ?thesis by (simp add: True)
  1449 qed
  1450 
  1451 lemma Ln_times_ii:
  1452   assumes "z \<noteq> 0"
  1453     shows  "Ln(\<i> * z) = (if 0 \<le> Re(z) | Im(z) < 0
  1454                           then Ln(z) + \<i> * of_real pi/2
  1455                           else Ln(z) - \<i> * of_real(3 * pi/2))"
  1456   using Im_Ln_le_pi [of z] mpi_less_Im_Ln [of z] assms
  1457         Im_Ln_eq_pi [of z] Im_Ln_pos_lt [of z] Re_Ln_pos_le [of z]
  1458   by (simp add: Ln_times) auto
  1459 
  1460 lemma Ln_of_nat [simp]: "0 < n \<Longrightarrow> Ln (of_nat n) = of_real (ln (of_nat n))"
  1461   by (subst of_real_of_nat_eq[symmetric], subst Ln_of_real[symmetric]) simp_all
  1462 
  1463 lemma Ln_of_nat_over_of_nat:
  1464   assumes "m > 0" "n > 0"
  1465   shows   "Ln (of_nat m / of_nat n) = of_real (ln (of_nat m) - ln (of_nat n))"
  1466 proof -
  1467   have "of_nat m / of_nat n = (of_real (of_nat m / of_nat n) :: complex)" by simp
  1468   also from assms have "Ln ... = of_real (ln (of_nat m / of_nat n))"
  1469     by (simp add: Ln_of_real[symmetric])
  1470   also from assms have "... = of_real (ln (of_nat m) - ln (of_nat n))"
  1471     by (simp add: ln_div)
  1472   finally show ?thesis .
  1473 qed
  1474 
  1475 
  1476 subsection\<open>Relation between Ln and Arg, and hence continuity of Arg\<close>
  1477 
  1478 lemma Arg_Ln:
  1479   assumes "0 < Arg z" shows "Arg z = Im(Ln(-z)) + pi"
  1480 proof (cases "z = 0")
  1481   case True
  1482   with assms show ?thesis
  1483     by simp
  1484 next
  1485   case False
  1486   then have "z / of_real(norm z) = exp(\<i> * of_real(Arg z))"
  1487     using Arg [of z]
  1488     by (metis abs_norm_cancel nonzero_mult_div_cancel_left norm_of_real zero_less_norm_iff)
  1489   then have "- z / of_real(norm z) = exp (\<i> * (of_real (Arg z) - pi))"
  1490     using cis_conv_exp cis_pi
  1491     by (auto simp: exp_diff algebra_simps)
  1492   then have "ln (- z / of_real(norm z)) = ln (exp (\<i> * (of_real (Arg z) - pi)))"
  1493     by simp
  1494   also have "... = \<i> * (of_real(Arg z) - pi)"
  1495     using Arg [of z] assms pi_not_less_zero
  1496     by auto
  1497   finally have "Arg z =  Im (Ln (- z / of_real (cmod z))) + pi"
  1498     by simp
  1499   also have "... = Im (Ln (-z) - ln (cmod z)) + pi"
  1500     by (metis diff_0_right minus_diff_eq zero_less_norm_iff Ln_divide_of_real False)
  1501   also have "... = Im (Ln (-z)) + pi"
  1502     by simp
  1503   finally show ?thesis .
  1504 qed
  1505 
  1506 lemma continuous_at_Arg:
  1507   assumes "z \<notin> \<real>\<^sub>\<ge>\<^sub>0"
  1508     shows "continuous (at z) Arg"
  1509 proof -
  1510   have *: "isCont (\<lambda>z. Im (Ln (- z)) + pi) z"
  1511     by (rule Complex.isCont_Im isCont_Ln' continuous_intros | simp add: assms complex_is_Real_iff)+
  1512   have [simp]: "\<And>x. \<lbrakk>Im x \<noteq> 0\<rbrakk> \<Longrightarrow> Im (Ln (- x)) + pi = Arg x"
  1513       using Arg_Ln Arg_gt_0 complex_is_Real_iff by auto
  1514   consider "Re z < 0" | "Im z \<noteq> 0" using assms
  1515     using complex_nonneg_Reals_iff not_le by blast
  1516   then have [simp]: "(\<lambda>z. Im (Ln (- z)) + pi) \<midarrow>z\<rightarrow> Arg z"
  1517       using "*"  by (simp add: isCont_def) (metis Arg_Ln Arg_gt_0 complex_is_Real_iff)
  1518   show ?thesis
  1519       apply (simp add: continuous_at)
  1520       apply (rule Lim_transform_within_open [where s= "-\<real>\<^sub>\<ge>\<^sub>0" and f = "\<lambda>z. Im(Ln(-z)) + pi"])
  1521       apply (auto simp add: not_le Arg_Ln [OF Arg_gt_0] complex_nonneg_Reals_iff closed_def [symmetric])
  1522       using assms apply (force simp add: complex_nonneg_Reals_iff)
  1523       done
  1524 qed
  1525 
  1526 lemma Ln_series:
  1527   fixes z :: complex
  1528   assumes "norm z < 1"
  1529   shows   "(\<lambda>n. (-1)^Suc n / of_nat n * z^n) sums ln (1 + z)" (is "(\<lambda>n. ?f n * z^n) sums _")
  1530 proof -
  1531   let ?F = "\<lambda>z. \<Sum>n. ?f n * z^n" and ?F' = "\<lambda>z. \<Sum>n. diffs ?f n * z^n"
  1532   have r: "conv_radius ?f = 1"
  1533     by (intro conv_radius_ratio_limit_nonzero[of _ 1])
  1534        (simp_all add: norm_divide LIMSEQ_Suc_n_over_n del: of_nat_Suc)
  1535 
  1536   have "\<exists>c. \<forall>z\<in>ball 0 1. ln (1 + z) - ?F z = c"
  1537   proof (rule has_field_derivative_zero_constant)
  1538     fix z :: complex assume z': "z \<in> ball 0 1"
  1539     hence z: "norm z < 1" by (simp add: dist_0_norm)
  1540     define t :: complex where "t = of_real (1 + norm z) / 2"
  1541     from z have t: "norm z < norm t" "norm t < 1" unfolding t_def
  1542       by (simp_all add: field_simps norm_divide del: of_real_add)
  1543 
  1544     have "Re (-z) \<le> norm (-z)" by (rule complex_Re_le_cmod)
  1545     also from z have "... < 1" by simp
  1546     finally have "((\<lambda>z. ln (1 + z)) has_field_derivative inverse (1+z)) (at z)"
  1547       by (auto intro!: derivative_eq_intros simp: complex_nonpos_Reals_iff)
  1548     moreover have "(?F has_field_derivative ?F' z) (at z)" using t r
  1549       by (intro termdiffs_strong[of _ t] summable_in_conv_radius) simp_all
  1550     ultimately have "((\<lambda>z. ln (1 + z) - ?F z) has_field_derivative (inverse (1 + z) - ?F' z))
  1551                        (at z within ball 0 1)"
  1552       by (intro derivative_intros) (simp_all add: at_within_open[OF z'])
  1553     also have "(\<lambda>n. of_nat n * ?f n * z ^ (n - Suc 0)) sums ?F' z" using t r
  1554       by (intro diffs_equiv termdiff_converges[OF t(1)] summable_in_conv_radius) simp_all
  1555     from sums_split_initial_segment[OF this, of 1]
  1556       have "(\<lambda>i. (-z) ^ i) sums ?F' z" by (simp add: power_minus[of z] del: of_nat_Suc)
  1557     hence "?F' z = inverse (1 + z)" using z by (simp add: sums_iff suminf_geometric divide_inverse)
  1558     also have "inverse (1 + z) - inverse (1 + z) = 0" by simp
  1559     finally show "((\<lambda>z. ln (1 + z) - ?F z) has_field_derivative 0) (at z within ball 0 1)" .
  1560   qed simp_all
  1561   then obtain c where c: "\<And>z. z \<in> ball 0 1 \<Longrightarrow> ln (1 + z) - ?F z = c" by blast
  1562   from c[of 0] have "c = 0" by (simp only: powser_zero) simp
  1563   with c[of z] assms have "ln (1 + z) = ?F z" by (simp add: dist_0_norm)
  1564   moreover have "summable (\<lambda>n. ?f n * z^n)" using assms r
  1565     by (intro summable_in_conv_radius) simp_all
  1566   ultimately show ?thesis by (simp add: sums_iff)
  1567 qed
  1568 
  1569 lemma Ln_series': "cmod z < 1 \<Longrightarrow> (\<lambda>n. - ((-z)^n) / of_nat n) sums ln (1 + z)"
  1570   by (drule Ln_series) (simp add: power_minus')
  1571 
  1572 lemma ln_series':
  1573   assumes "abs (x::real) < 1"
  1574   shows   "(\<lambda>n. - ((-x)^n) / of_nat n) sums ln (1 + x)"
  1575 proof -
  1576   from assms have "(\<lambda>n. - ((-of_real x)^n) / of_nat n) sums ln (1 + complex_of_real x)"
  1577     by (intro Ln_series') simp_all
  1578   also have "(\<lambda>n. - ((-of_real x)^n) / of_nat n) = (\<lambda>n. complex_of_real (- ((-x)^n) / of_nat n))"
  1579     by (rule ext) simp
  1580   also from assms have "ln (1 + complex_of_real x) = of_real (ln (1 + x))"
  1581     by (subst Ln_of_real [symmetric]) simp_all
  1582   finally show ?thesis by (subst (asm) sums_of_real_iff)
  1583 qed
  1584 
  1585 lemma Ln_approx_linear:
  1586   fixes z :: complex
  1587   assumes "norm z < 1"
  1588   shows   "norm (ln (1 + z) - z) \<le> norm z^2 / (1 - norm z)"
  1589 proof -
  1590   let ?f = "\<lambda>n. (-1)^Suc n / of_nat n"
  1591   from assms have "(\<lambda>n. ?f n * z^n) sums ln (1 + z)" using Ln_series by simp
  1592   moreover have "(\<lambda>n. (if n = 1 then 1 else 0) * z^n) sums z" using powser_sums_if[of 1] by simp
  1593   ultimately have "(\<lambda>n. (?f n - (if n = 1 then 1 else 0)) * z^n) sums (ln (1 + z) - z)"
  1594     by (subst left_diff_distrib, intro sums_diff) simp_all
  1595   from sums_split_initial_segment[OF this, of "Suc 1"]
  1596     have "(\<lambda>i. (-(z^2)) * inverse (2 + of_nat i) * (- z)^i) sums (Ln (1 + z) - z)"
  1597     by (simp add: power2_eq_square mult_ac power_minus[of z] divide_inverse)
  1598   hence "(Ln (1 + z) - z) = (\<Sum>i. (-(z^2)) * inverse (of_nat (i+2)) * (-z)^i)"
  1599     by (simp add: sums_iff)
  1600   also have A: "summable (\<lambda>n. norm z^2 * (inverse (real_of_nat (Suc (Suc n))) * cmod z ^ n))"
  1601     by (rule summable_mult, rule summable_comparison_test_ev[OF _ summable_geometric[of "norm z"]])
  1602        (auto simp: assms field_simps intro!: always_eventually)
  1603   hence "norm (\<Sum>i. (-(z^2)) * inverse (of_nat (i+2)) * (-z)^i) \<le>
  1604              (\<Sum>i. norm (-(z^2) * inverse (of_nat (i+2)) * (-z)^i))"
  1605     by (intro summable_norm)
  1606        (auto simp: norm_power norm_inverse norm_mult mult_ac simp del: of_nat_add of_nat_Suc)
  1607   also have "norm ((-z)^2 * (-z)^i) * inverse (of_nat (i+2)) \<le> norm ((-z)^2 * (-z)^i) * 1" for i
  1608     by (intro mult_left_mono) (simp_all add: divide_simps)
  1609   hence "(\<Sum>i. norm (-(z^2) * inverse (of_nat (i+2)) * (-z)^i)) \<le>
  1610            (\<Sum>i. norm (-(z^2) * (-z)^i))" using A assms
  1611     apply (simp_all only: norm_power norm_inverse norm_divide norm_mult)
  1612     apply (intro suminf_le summable_mult summable_geometric)
  1613     apply (auto simp: norm_power field_simps simp del: of_nat_add of_nat_Suc)
  1614     done
  1615   also have "... = norm z^2 * (\<Sum>i. norm z^i)" using assms
  1616     by (subst suminf_mult [symmetric]) (auto intro!: summable_geometric simp: norm_mult norm_power)
  1617   also have "(\<Sum>i. norm z^i) = inverse (1 - norm z)" using assms
  1618     by (subst suminf_geometric) (simp_all add: divide_inverse)
  1619   also have "norm z^2 * ... = norm z^2 / (1 - norm z)" by (simp add: divide_inverse)
  1620   finally show ?thesis .
  1621 qed
  1622 
  1623 
  1624 text\<open>Relation between Arg and arctangent in upper halfplane\<close>
  1625 lemma Arg_arctan_upperhalf:
  1626   assumes "0 < Im z"
  1627     shows "Arg z = pi/2 - arctan(Re z / Im z)"
  1628 proof (cases "z = 0")
  1629   case True with assms show ?thesis
  1630     by simp
  1631 next
  1632   case False
  1633   show ?thesis
  1634     apply (rule Arg_unique [of "norm z"])
  1635     using False assms arctan [of "Re z / Im z"] pi_ge_two pi_half_less_two
  1636     apply (auto simp: exp_Euler cos_diff sin_diff)
  1637     using norm_complex_def [of z, symmetric]
  1638     apply (simp add: sin_of_real cos_of_real sin_arctan cos_arctan field_simps real_sqrt_divide)
  1639     apply (metis complex_eq mult.assoc ring_class.ring_distribs(2))
  1640     done
  1641 qed
  1642 
  1643 lemma Arg_eq_Im_Ln:
  1644   assumes "0 \<le> Im z" "0 < Re z"
  1645     shows "Arg z = Im (Ln z)"
  1646 proof (cases "z = 0 \<or> Im z = 0")
  1647   case True then show ?thesis
  1648     using assms Arg_eq_0 complex_is_Real_iff
  1649     apply auto
  1650     by (metis Arg_eq_0_pi Arg_eq_pi Im_Ln_eq_0 Im_Ln_eq_pi less_numeral_extra(3) zero_complex.simps(1))
  1651 next
  1652   case False
  1653   then have "Arg z > 0"
  1654     using Arg_gt_0 complex_is_Real_iff by blast
  1655   then show ?thesis
  1656     using assms False
  1657     by (subst Arg_Ln) (auto simp: Ln_minus)
  1658 qed
  1659 
  1660 lemma continuous_within_upperhalf_Arg:
  1661   assumes "z \<noteq> 0"
  1662     shows "continuous (at z within {z. 0 \<le> Im z}) Arg"
  1663 proof (cases "z \<in> \<real>\<^sub>\<ge>\<^sub>0")
  1664   case False then show ?thesis
  1665     using continuous_at_Arg continuous_at_imp_continuous_within by auto
  1666 next
  1667   case True
  1668   then have z: "z \<in> \<real>" "0 < Re z"
  1669     using assms  by (auto simp: complex_nonneg_Reals_iff complex_is_Real_iff complex_neq_0)
  1670   then have [simp]: "Arg z = 0" "Im (Ln z) = 0"
  1671     by (auto simp: Arg_eq_0 Im_Ln_eq_0 assms complex_is_Real_iff)
  1672   show ?thesis
  1673   proof (clarsimp simp add: continuous_within Lim_within dist_norm)
  1674     fix e::real
  1675     assume "0 < e"
  1676     moreover have "continuous (at z) (\<lambda>x. Im (Ln x))"
  1677       using z by (simp add: continuous_at_Ln complex_nonpos_Reals_iff)
  1678     ultimately
  1679     obtain d where d: "d>0" "\<And>x. x \<noteq> z \<Longrightarrow> cmod (x - z) < d \<Longrightarrow> \<bar>Im (Ln x)\<bar> < e"
  1680       by (auto simp: continuous_within Lim_within dist_norm)
  1681     { fix x
  1682       assume "cmod (x - z) < Re z / 2"
  1683       then have "\<bar>Re x - Re z\<bar> < Re z / 2"
  1684         by (metis le_less_trans abs_Re_le_cmod minus_complex.simps(1))
  1685       then have "0 < Re x"
  1686         using z by linarith
  1687     }
  1688     then show "\<exists>d>0. \<forall>x. 0 \<le> Im x \<longrightarrow> x \<noteq> z \<and> cmod (x - z) < d \<longrightarrow> \<bar>Arg x\<bar> < e"
  1689       apply (rule_tac x="min d (Re z / 2)" in exI)
  1690       using z d
  1691       apply (auto simp: Arg_eq_Im_Ln)
  1692       done
  1693   qed
  1694 qed
  1695 
  1696 lemma continuous_on_upperhalf_Arg: "continuous_on ({z. 0 \<le> Im z} - {0}) Arg"
  1697   apply (auto simp: continuous_on_eq_continuous_within)
  1698   by (metis Diff_subset continuous_within_subset continuous_within_upperhalf_Arg)
  1699 
  1700 lemma open_Arg_less_Int:
  1701   assumes "0 \<le> s" "t \<le> 2*pi"
  1702     shows "open ({y. s < Arg y} \<inter> {y. Arg y < t})"
  1703 proof -
  1704   have 1: "continuous_on (UNIV - \<real>\<^sub>\<ge>\<^sub>0) Arg"
  1705     using continuous_at_Arg continuous_at_imp_continuous_within
  1706     by (auto simp: continuous_on_eq_continuous_within)
  1707   have 2: "open (UNIV - \<real>\<^sub>\<ge>\<^sub>0 :: complex set)"  by (simp add: open_Diff)
  1708   have "open ({z. s < z} \<inter> {z. z < t})"
  1709     using open_lessThan [of t] open_greaterThan [of s]
  1710     by (metis greaterThan_def lessThan_def open_Int)
  1711   moreover have "{y. s < Arg y} \<inter> {y. Arg y < t} \<subseteq> - \<real>\<^sub>\<ge>\<^sub>0"
  1712     using assms by (auto simp: Arg_real complex_nonneg_Reals_iff complex_is_Real_iff)
  1713   ultimately show ?thesis
  1714     using continuous_imp_open_vimage [OF 1 2, of  "{z. Re z > s} \<inter> {z. Re z < t}"]
  1715     by auto
  1716 qed
  1717 
  1718 lemma open_Arg_gt: "open {z. t < Arg z}"
  1719 proof (cases "t < 0")
  1720   case True then have "{z. t < Arg z} = UNIV"
  1721     using Arg_ge_0 less_le_trans by auto
  1722   then show ?thesis
  1723     by simp
  1724 next
  1725   case False then show ?thesis
  1726     using open_Arg_less_Int [of t "2*pi"] Arg_lt_2pi
  1727     by auto
  1728 qed
  1729 
  1730 lemma closed_Arg_le: "closed {z. Arg z \<le> t}"
  1731   using open_Arg_gt [of t]
  1732   by (simp add: closed_def Set.Collect_neg_eq [symmetric] not_le)
  1733 
  1734 subsection\<open>Complex Powers\<close>
  1735 
  1736 lemma powr_to_1 [simp]: "z powr 1 = (z::complex)"
  1737   by (simp add: powr_def)
  1738 
  1739 lemma powr_nat:
  1740   fixes n::nat and z::complex shows "z powr n = (if z = 0 then 0 else z^n)"
  1741   by (simp add: exp_of_nat_mult powr_def)
  1742 
  1743 lemma norm_powr_real: "w \<in> \<real> \<Longrightarrow> 0 < Re w \<Longrightarrow> norm(w powr z) = exp(Re z * ln(Re w))"
  1744   apply (simp add: powr_def)
  1745   using Im_Ln_eq_0 complex_is_Real_iff norm_complex_def
  1746   by auto
  1747 
  1748 lemma powr_complexpow [simp]:
  1749   fixes x::complex shows "x \<noteq> 0 \<Longrightarrow> x powr (of_nat n) = x^n"
  1750   by (induct n) (auto simp: ac_simps powr_add)
  1751 
  1752 lemma powr_complexnumeral [simp]:
  1753   fixes x::complex shows "x \<noteq> 0 \<Longrightarrow> x powr (numeral n) = x ^ (numeral n)"
  1754   by (metis of_nat_numeral powr_complexpow)
  1755 
  1756 lemma cnj_powr:
  1757   assumes "Im a = 0 \<Longrightarrow> Re a \<ge> 0"
  1758   shows   "cnj (a powr b) = cnj a powr cnj b"
  1759 proof (cases "a = 0")
  1760   case False
  1761   with assms have "a \<notin> \<real>\<^sub>\<le>\<^sub>0" by (auto simp: complex_eq_iff complex_nonpos_Reals_iff)
  1762   with False show ?thesis by (simp add: powr_def exp_cnj cnj_Ln)
  1763 qed simp
  1764 
  1765 lemma powr_real_real:
  1766     "\<lbrakk>w \<in> \<real>; z \<in> \<real>; 0 < Re w\<rbrakk> \<Longrightarrow> w powr z = exp(Re z * ln(Re w))"
  1767   apply (simp add: powr_def)
  1768   by (metis complex_eq complex_is_Real_iff diff_0 diff_0_right diff_minus_eq_add exp_ln exp_not_eq_zero
  1769        exp_of_real Ln_of_real mult_zero_right of_real_0 of_real_mult)
  1770 
  1771 lemma powr_of_real:
  1772   fixes x::real and y::real
  1773   shows "0 \<le> x \<Longrightarrow> of_real x powr (of_real y::complex) = of_real (x powr y)"
  1774   by (simp_all add: powr_def exp_eq_polar)
  1775 
  1776 lemma powr_Reals_eq: "\<lbrakk>x \<in> \<real>; y \<in> \<real>; Re x > 0\<rbrakk> \<Longrightarrow> x powr y = of_real (Re x powr Re y)"
  1777   by (metis linear not_le of_real_Re powr_of_real)
  1778 
  1779 lemma norm_powr_real_mono:
  1780     "\<lbrakk>w \<in> \<real>; 1 < Re w\<rbrakk>
  1781      \<Longrightarrow> cmod(w powr z1) \<le> cmod(w powr z2) \<longleftrightarrow> Re z1 \<le> Re z2"
  1782   by (auto simp: powr_def algebra_simps Reals_def Ln_of_real)
  1783 
  1784 lemma powr_times_real:
  1785     "\<lbrakk>x \<in> \<real>; y \<in> \<real>; 0 \<le> Re x; 0 \<le> Re y\<rbrakk>
  1786            \<Longrightarrow> (x * y) powr z = x powr z * y powr z"
  1787   by (auto simp: Reals_def powr_def Ln_times exp_add algebra_simps less_eq_real_def Ln_of_real)
  1788 
  1789 lemma Re_powr_le: "r \<in> \<real>\<^sub>\<ge>\<^sub>0 \<Longrightarrow> Re (r powr z) \<le> Re r powr Re z"
  1790   by (auto simp: powr_def nonneg_Reals_def order_trans [OF complex_Re_le_cmod])
  1791 
  1792 lemma
  1793   fixes w::complex
  1794   shows Reals_powr [simp]: "\<lbrakk>w \<in> \<real>\<^sub>\<ge>\<^sub>0; z \<in> \<real>\<rbrakk> \<Longrightarrow> w powr z \<in> \<real>"
  1795   and nonneg_Reals_powr [simp]: "\<lbrakk>w \<in> \<real>\<^sub>\<ge>\<^sub>0; z \<in> \<real>\<rbrakk> \<Longrightarrow> w powr z \<in> \<real>\<^sub>\<ge>\<^sub>0"
  1796   by (auto simp: nonneg_Reals_def Reals_def powr_of_real)
  1797 
  1798 lemma powr_neg_real_complex:
  1799   shows   "(- of_real x) powr a = (-1) powr (of_real (sgn x) * a) * of_real x powr (a :: complex)"
  1800 proof (cases "x = 0")
  1801   assume x: "x \<noteq> 0"
  1802   hence "(-x) powr a = exp (a * ln (-of_real x))" by (simp add: powr_def)
  1803   also from x have "ln (-of_real x) = Ln (of_real x) + of_real (sgn x) * pi * \<i>"
  1804     by (simp add: Ln_minus Ln_of_real)
  1805   also from x have "exp (a * ...) = cis pi powr (of_real (sgn x) * a) * of_real x powr a"
  1806     by (simp add: powr_def exp_add algebra_simps Ln_of_real cis_conv_exp)
  1807   also note cis_pi
  1808   finally show ?thesis by simp
  1809 qed simp_all
  1810 
  1811 lemma has_field_derivative_powr:
  1812   fixes z :: complex
  1813   shows "z \<notin> \<real>\<^sub>\<le>\<^sub>0 \<Longrightarrow> ((\<lambda>z. z powr s) has_field_derivative (s * z powr (s - 1))) (at z)"
  1814   apply (cases "z=0", auto)
  1815   apply (simp add: powr_def)
  1816   apply (rule DERIV_transform_at [where d = "norm z" and f = "\<lambda>z. exp (s * Ln z)"])
  1817   apply (auto simp: dist_complex_def)
  1818   apply (intro derivative_eq_intros | simp)+
  1819   apply (simp add: field_simps exp_diff)
  1820   done
  1821 
  1822 declare has_field_derivative_powr[THEN DERIV_chain2, derivative_intros]
  1823 
  1824 
  1825 lemma has_field_derivative_powr_right [derivative_intros]:
  1826     "w \<noteq> 0 \<Longrightarrow> ((\<lambda>z. w powr z) has_field_derivative Ln w * w powr z) (at z)"
  1827   apply (simp add: powr_def)
  1828   apply (intro derivative_eq_intros | simp)+
  1829   done
  1830 
  1831 lemma field_differentiable_powr_right [derivative_intros]:
  1832   fixes w::complex
  1833   shows "w \<noteq> 0 \<Longrightarrow> (\<lambda>z. w powr z) field_differentiable (at z)"
  1834 using field_differentiable_def has_field_derivative_powr_right by blast
  1835 
  1836 lemma holomorphic_on_powr_right [holomorphic_intros]:
  1837     "f holomorphic_on s \<Longrightarrow> w \<noteq> 0 \<Longrightarrow> (\<lambda>z. w powr (f z)) holomorphic_on s"
  1838   unfolding holomorphic_on_def field_differentiable_def
  1839   by (metis (full_types) DERIV_chain' has_field_derivative_powr_right)
  1840 
  1841 lemma norm_powr_real_powr:
  1842   "w \<in> \<real> \<Longrightarrow> 0 \<le> Re w \<Longrightarrow> cmod (w powr z) = Re w powr Re z"
  1843   by (cases "w = 0") (auto simp add: norm_powr_real powr_def Im_Ln_eq_0
  1844                                      complex_is_Real_iff in_Reals_norm complex_eq_iff)
  1845 
  1846 lemma tendsto_ln_complex [tendsto_intros]:
  1847   assumes "(f \<longlongrightarrow> a) F" "a \<notin> \<real>\<^sub>\<le>\<^sub>0"
  1848   shows   "((\<lambda>z. ln (f z :: complex)) \<longlongrightarrow> ln a) F"
  1849   using tendsto_compose[OF continuous_at_Ln[of a, unfolded isCont_def] assms(1)] assms(2) by simp
  1850 
  1851 lemma tendsto_powr_complex:
  1852   fixes f g :: "_ \<Rightarrow> complex"
  1853   assumes a: "a \<notin> \<real>\<^sub>\<le>\<^sub>0"
  1854   assumes f: "(f \<longlongrightarrow> a) F" and g: "(g \<longlongrightarrow> b) F"
  1855   shows   "((\<lambda>z. f z powr g z) \<longlongrightarrow> a powr b) F"
  1856 proof -
  1857   from a have [simp]: "a \<noteq> 0" by auto
  1858   from f g a have "((\<lambda>z. exp (g z * ln (f z))) \<longlongrightarrow> a powr b) F" (is ?P)
  1859     by (auto intro!: tendsto_intros simp: powr_def)
  1860   also {
  1861     have "eventually (\<lambda>z. z \<noteq> 0) (nhds a)"
  1862       by (intro t1_space_nhds) simp_all
  1863     with f have "eventually (\<lambda>z. f z \<noteq> 0) F" using filterlim_iff by blast
  1864   }
  1865   hence "?P \<longleftrightarrow> ((\<lambda>z. f z powr g z) \<longlongrightarrow> a powr b) F"
  1866     by (intro tendsto_cong refl) (simp_all add: powr_def mult_ac)
  1867   finally show ?thesis .
  1868 qed
  1869 
  1870 lemma tendsto_powr_complex_0:
  1871   fixes f g :: "'a \<Rightarrow> complex"
  1872   assumes f: "(f \<longlongrightarrow> 0) F" and g: "(g \<longlongrightarrow> b) F" and b: "Re b > 0"
  1873   shows   "((\<lambda>z. f z powr g z) \<longlongrightarrow> 0) F"
  1874 proof (rule tendsto_norm_zero_cancel)
  1875   define h where
  1876     "h = (\<lambda>z. if f z = 0 then 0 else exp (Re (g z) * ln (cmod (f z)) + abs (Im (g z)) * pi))"
  1877   {
  1878     fix z :: 'a assume z: "f z \<noteq> 0"
  1879     define c where "c = abs (Im (g z)) * pi"
  1880     from mpi_less_Im_Ln[OF z] Im_Ln_le_pi[OF z]
  1881       have "abs (Im (Ln (f z))) \<le> pi" by simp
  1882     from mult_left_mono[OF this, of "abs (Im (g z))"]
  1883       have "abs (Im (g z) * Im (ln (f z))) \<le> c" by (simp add: abs_mult c_def)
  1884     hence "-Im (g z) * Im (ln (f z)) \<le> c" by simp
  1885     hence "norm (f z powr g z) \<le> h z" by (simp add: powr_def field_simps h_def c_def)
  1886   }
  1887   hence le: "norm (f z powr g z) \<le> h z" for z by (cases "f x = 0") (simp_all add: h_def)
  1888 
  1889   have g': "(g \<longlongrightarrow> b) (inf F (principal {z. f z \<noteq> 0}))"
  1890     by (rule tendsto_mono[OF _ g]) simp_all
  1891   have "((\<lambda>x. norm (f x)) \<longlongrightarrow> 0) (inf F (principal {z. f z \<noteq> 0}))"
  1892     by (subst tendsto_norm_zero_iff, rule tendsto_mono[OF _ f]) simp_all
  1893   moreover {
  1894     have "filterlim (\<lambda>x. norm (f x)) (principal {0<..}) (principal {z. f z \<noteq> 0})"
  1895       by (auto simp: filterlim_def)
  1896     hence "filterlim (\<lambda>x. norm (f x)) (principal {0<..})
  1897              (inf F (principal {z. f z \<noteq> 0}))"
  1898       by (rule filterlim_mono) simp_all
  1899   }
  1900   ultimately have norm: "filterlim (\<lambda>x. norm (f x)) (at_right 0) (inf F (principal {z. f z \<noteq> 0}))"
  1901     by (simp add: filterlim_inf at_within_def)
  1902 
  1903   have A: "LIM x inf F (principal {z. f z \<noteq> 0}). Re (g x) * -ln (cmod (f x)) :> at_top"
  1904     by (rule filterlim_tendsto_pos_mult_at_top tendsto_intros g' b
  1905           filterlim_compose[OF filterlim_uminus_at_top_at_bot] filterlim_compose[OF ln_at_0] norm)+
  1906   have B: "LIM x inf F (principal {z. f z \<noteq> 0}).
  1907           -\<bar>Im (g x)\<bar> * pi + -(Re (g x) * ln (cmod (f x))) :> at_top"
  1908     by (rule filterlim_tendsto_add_at_top tendsto_intros g')+ (insert A, simp_all)
  1909   have C: "(h \<longlongrightarrow> 0) F" unfolding h_def
  1910     by (intro filterlim_If tendsto_const filterlim_compose[OF exp_at_bot])
  1911        (insert B, auto simp: filterlim_uminus_at_bot algebra_simps)
  1912   show "((\<lambda>x. norm (f x powr g x)) \<longlongrightarrow> 0) F"
  1913     by (rule Lim_null_comparison[OF always_eventually C]) (insert le, auto)
  1914 qed
  1915 
  1916 lemma tendsto_powr_complex' [tendsto_intros]:
  1917   fixes f g :: "_ \<Rightarrow> complex"
  1918   assumes fz: "a \<notin> \<real>\<^sub>\<le>\<^sub>0 \<or> (a = 0 \<and> Re b > 0)"
  1919   assumes fg: "(f \<longlongrightarrow> a) F" "(g \<longlongrightarrow> b) F"
  1920   shows   "((\<lambda>z. f z powr g z) \<longlongrightarrow> a powr b) F"
  1921 proof (cases "a = 0")
  1922   case True
  1923   with assms show ?thesis by (auto intro!: tendsto_powr_complex_0)
  1924 next
  1925   case False
  1926   with assms show ?thesis by (auto intro!: tendsto_powr_complex elim!: nonpos_Reals_cases)
  1927 qed
  1928 
  1929 lemma continuous_powr_complex:
  1930   assumes "f (netlimit F) \<notin> \<real>\<^sub>\<le>\<^sub>0" "continuous F f" "continuous F g"
  1931   shows   "continuous F (\<lambda>z. f z powr g z :: complex)"
  1932   using assms unfolding continuous_def by (intro tendsto_powr_complex) simp_all
  1933 
  1934 lemma isCont_powr_complex [continuous_intros]:
  1935   assumes "f z \<notin> \<real>\<^sub>\<le>\<^sub>0" "isCont f z" "isCont g z"
  1936   shows   "isCont (\<lambda>z. f z powr g z :: complex) z"
  1937   using assms unfolding isCont_def by (intro tendsto_powr_complex) simp_all
  1938 
  1939 lemma continuous_on_powr_complex [continuous_intros]:
  1940   assumes "A \<subseteq> {z. Re (f z) \<ge> 0 \<or> Im (f z) \<noteq> 0}"
  1941   assumes "\<And>z. z \<in> A \<Longrightarrow> f z = 0 \<Longrightarrow> Re (g z) > 0"
  1942   assumes "continuous_on A f" "continuous_on A g"
  1943   shows   "continuous_on A (\<lambda>z. f z powr g z)"
  1944   unfolding continuous_on_def
  1945 proof
  1946   fix z assume z: "z \<in> A"
  1947   show "((\<lambda>z. f z powr g z) \<longlongrightarrow> f z powr g z) (at z within A)"
  1948   proof (cases "f z = 0")
  1949     case False
  1950     from assms(1,2) z have "Re (f z) \<ge> 0 \<or> Im (f z) \<noteq> 0" "f z = 0 \<longrightarrow> Re (g z) > 0" by auto
  1951     with assms(3,4) z show ?thesis
  1952       by (intro tendsto_powr_complex')
  1953          (auto elim!: nonpos_Reals_cases simp: complex_eq_iff continuous_on_def)
  1954   next
  1955     case True
  1956     with assms z show ?thesis
  1957       by (auto intro!: tendsto_powr_complex_0 simp: continuous_on_def)
  1958   qed
  1959 qed
  1960 
  1961 
  1962 subsection\<open>Some Limits involving Logarithms\<close>
  1963 
  1964 lemma lim_Ln_over_power:
  1965   fixes s::complex
  1966   assumes "0 < Re s"
  1967     shows "((\<lambda>n. Ln n / (n powr s)) \<longlongrightarrow> 0) sequentially"
  1968 proof (simp add: lim_sequentially dist_norm, clarify)
  1969   fix e::real
  1970   assume e: "0 < e"
  1971   have "\<exists>xo>0. \<forall>x\<ge>xo. 0 < e * 2 + (e * Re s * 2 - 2) * x + e * (Re s)\<^sup>2 * x\<^sup>2"
  1972   proof (rule_tac x="2/(e * (Re s)\<^sup>2)" in exI, safe)
  1973     show "0 < 2 / (e * (Re s)\<^sup>2)"
  1974       using e assms by (simp add: field_simps)
  1975   next
  1976     fix x::real
  1977     assume x: "2 / (e * (Re s)\<^sup>2) \<le> x"
  1978     then have "x>0"
  1979     using e assms
  1980       by (metis less_le_trans mult_eq_0_iff mult_pos_pos pos_less_divide_eq power2_eq_square
  1981                 zero_less_numeral)
  1982     then show "0 < e * 2 + (e * Re s * 2 - 2) * x + e * (Re s)\<^sup>2 * x\<^sup>2"
  1983       using e assms x
  1984       apply (auto simp: field_simps)
  1985       apply (rule_tac y = "e * (x\<^sup>2 * (Re s)\<^sup>2)" in le_less_trans)
  1986       apply (auto simp: power2_eq_square field_simps add_pos_pos)
  1987       done
  1988   qed
  1989   then have "\<exists>xo>0. \<forall>x\<ge>xo. x / e < 1 + (Re s * x) + (1/2) * (Re s * x)^2"
  1990     using e  by (simp add: field_simps)
  1991   then have "\<exists>xo>0. \<forall>x\<ge>xo. x / e < exp (Re s * x)"
  1992     using assms
  1993     by (force intro: less_le_trans [OF _ exp_lower_taylor_quadratic])
  1994   then have "\<exists>xo>0. \<forall>x\<ge>xo. x < e * exp (Re s * x)"
  1995     using e   by (auto simp: field_simps)
  1996   with e show "\<exists>no. \<forall>n\<ge>no. norm (Ln (of_nat n) / of_nat n powr s) < e"
  1997     apply (auto simp: norm_divide norm_powr_real divide_simps)
  1998     apply (rule_tac x="nat \<lceil>exp xo\<rceil>" in exI)
  1999     apply clarify
  2000     apply (drule_tac x="ln n" in spec)
  2001     apply (auto simp: exp_less_mono nat_ceiling_le_eq not_le)
  2002     apply (metis exp_less_mono exp_ln not_le of_nat_0_less_iff)
  2003     done
  2004 qed
  2005 
  2006 lemma lim_Ln_over_n: "((\<lambda>n. Ln(of_nat n) / of_nat n) \<longlongrightarrow> 0) sequentially"
  2007   using lim_Ln_over_power [of 1] by simp
  2008 
  2009 lemma lim_ln_over_power:
  2010   fixes s :: real
  2011   assumes "0 < s"
  2012     shows "((\<lambda>n. ln n / (n powr s)) \<longlongrightarrow> 0) sequentially"
  2013   using lim_Ln_over_power [of "of_real s", THEN filterlim_sequentially_Suc [THEN iffD2]] assms
  2014   apply (subst filterlim_sequentially_Suc [symmetric])
  2015   apply (simp add: lim_sequentially dist_norm
  2016           Ln_Reals_eq norm_powr_real_powr norm_divide)
  2017   done
  2018 
  2019 lemma lim_ln_over_n: "((\<lambda>n. ln(real_of_nat n) / of_nat n) \<longlongrightarrow> 0) sequentially"
  2020   using lim_ln_over_power [of 1, THEN filterlim_sequentially_Suc [THEN iffD2]]
  2021   apply (subst filterlim_sequentially_Suc [symmetric])
  2022   apply (simp add: lim_sequentially dist_norm)
  2023   done
  2024 
  2025 lemma lim_1_over_complex_power:
  2026   assumes "0 < Re s"
  2027     shows "((\<lambda>n. 1 / (of_nat n powr s)) \<longlongrightarrow> 0) sequentially"
  2028 proof -
  2029   have "\<forall>n>0. 3 \<le> n \<longrightarrow> 1 \<le> ln (real_of_nat n)"
  2030     using ln_272_gt_1
  2031     by (force intro: order_trans [of _ "ln (272/100)"])
  2032   moreover have "(\<lambda>n. cmod (Ln (of_nat n) / of_nat n powr s)) \<longlonglongrightarrow> 0"
  2033     using lim_Ln_over_power [OF assms]
  2034     by (metis tendsto_norm_zero_iff)
  2035   ultimately show ?thesis
  2036     apply (auto intro!: Lim_null_comparison [where g = "\<lambda>n. norm (Ln(of_nat n) / of_nat n powr s)"])
  2037     apply (auto simp: norm_divide divide_simps eventually_sequentially)
  2038     done
  2039 qed
  2040 
  2041 lemma lim_1_over_real_power:
  2042   fixes s :: real
  2043   assumes "0 < s"
  2044     shows "((\<lambda>n. 1 / (of_nat n powr s)) \<longlongrightarrow> 0) sequentially"
  2045   using lim_1_over_complex_power [of "of_real s", THEN filterlim_sequentially_Suc [THEN iffD2]] assms
  2046   apply (subst filterlim_sequentially_Suc [symmetric])
  2047   apply (simp add: lim_sequentially dist_norm)
  2048   apply (simp add: Ln_Reals_eq norm_powr_real_powr norm_divide)
  2049   done
  2050 
  2051 lemma lim_1_over_Ln: "((\<lambda>n. 1 / Ln(of_nat n)) \<longlongrightarrow> 0) sequentially"
  2052 proof (clarsimp simp add: lim_sequentially dist_norm norm_divide divide_simps)
  2053   fix r::real
  2054   assume "0 < r"
  2055   have ir: "inverse (exp (inverse r)) > 0"
  2056     by simp
  2057   obtain n where n: "1 < of_nat n * inverse (exp (inverse r))"
  2058     using ex_less_of_nat_mult [of _ 1, OF ir]
  2059     by auto
  2060   then have "exp (inverse r) < of_nat n"
  2061     by (simp add: divide_simps)
  2062   then have "ln (exp (inverse r)) < ln (of_nat n)"
  2063     by (metis exp_gt_zero less_trans ln_exp ln_less_cancel_iff)
  2064   with \<open>0 < r\<close> have "1 < r * ln (real_of_nat n)"
  2065     by (simp add: field_simps)
  2066   moreover have "n > 0" using n
  2067     using neq0_conv by fastforce
  2068   ultimately show "\<exists>no. \<forall>n. Ln (of_nat n) \<noteq> 0 \<longrightarrow> no \<le> n \<longrightarrow> 1 < r * cmod (Ln (of_nat n))"
  2069     using n \<open>0 < r\<close>
  2070     apply (rule_tac x=n in exI)
  2071     apply (auto simp: divide_simps)
  2072     apply (erule less_le_trans, auto)
  2073     done
  2074 qed
  2075 
  2076 lemma lim_1_over_ln: "((\<lambda>n. 1 / ln(real_of_nat n)) \<longlongrightarrow> 0) sequentially"
  2077   using lim_1_over_Ln [THEN filterlim_sequentially_Suc [THEN iffD2]]
  2078   apply (subst filterlim_sequentially_Suc [symmetric])
  2079   apply (simp add: lim_sequentially dist_norm)
  2080   apply (simp add: Ln_Reals_eq norm_powr_real_powr norm_divide)
  2081   done
  2082 
  2083 lemma lim_ln1_over_ln: "(\<lambda>n. ln(Suc n) / ln n) \<longlonglongrightarrow> 1"
  2084 proof (rule Lim_transform_eventually)
  2085   have "(\<lambda>n. ln(1 + 1/n) / ln n) \<longlonglongrightarrow> 0"
  2086   proof (rule Lim_transform_bound)
  2087     show "(inverse o real) \<longlonglongrightarrow> 0"
  2088       by (metis comp_def seq_harmonic tendsto_explicit)
  2089     show "\<forall>\<^sub>F n in sequentially. norm (ln (1 + 1 / n) / ln n) \<le> norm ((inverse \<circ> real) n)"
  2090     proof
  2091       fix n::nat
  2092       assume n: "3 \<le> n"
  2093       then have "ln 3 \<le> ln n" and ln0: "0 \<le> ln n"
  2094         by auto
  2095       with ln3_gt_1 have "1/ ln n \<le> 1"
  2096         by (simp add: divide_simps)
  2097       moreover have "ln (1 + 1 / real n) \<le> 1/n"
  2098         by (simp add: ln_add_one_self_le_self)
  2099       ultimately have "ln (1 + 1 / real n) * (1 / ln n) \<le> (1/n) * 1"
  2100         by (intro mult_mono) (use n in auto)
  2101       then show "norm (ln (1 + 1 / n) / ln n) \<le> norm ((inverse \<circ> real) n)"
  2102         by (simp add: field_simps ln0)
  2103       qed
  2104   qed
  2105   then show "(\<lambda>n. 1 + ln(1 + 1/n) / ln n) \<longlonglongrightarrow> 1"
  2106     by (metis (full_types) add.right_neutral tendsto_add_const_iff)
  2107   show "\<forall>\<^sub>F k in sequentially. 1 + ln (1 + 1 / k) / ln k = ln(Suc k) / ln k"
  2108     by (simp add: divide_simps ln_div eventually_sequentiallyI [of 2])
  2109 qed
  2110 
  2111 lemma lim_ln_over_ln1: "(\<lambda>n. ln n / ln(Suc n)) \<longlonglongrightarrow> 1"
  2112 proof -
  2113   have "(\<lambda>n. inverse (ln(Suc n) / ln n)) \<longlonglongrightarrow> inverse 1"
  2114     by (rule tendsto_inverse [OF lim_ln1_over_ln]) auto
  2115   then show ?thesis
  2116     by simp
  2117 qed
  2118 
  2119 
  2120 subsection\<open>Relation between Square Root and exp/ln, hence its derivative\<close>
  2121 
  2122 lemma csqrt_exp_Ln:
  2123   assumes "z \<noteq> 0"
  2124     shows "csqrt z = exp(Ln(z) / 2)"
  2125 proof -
  2126   have "(exp (Ln z / 2))\<^sup>2 = (exp (Ln z))"
  2127     by (metis exp_double nonzero_mult_div_cancel_left times_divide_eq_right zero_neq_numeral)
  2128   also have "... = z"
  2129     using assms exp_Ln by blast
  2130   finally have "csqrt z = csqrt ((exp (Ln z / 2))\<^sup>2)"
  2131     by simp
  2132   also have "... = exp (Ln z / 2)"
  2133     apply (subst csqrt_square)
  2134     using cos_gt_zero_pi [of "(Im (Ln z) / 2)"] Im_Ln_le_pi mpi_less_Im_Ln assms
  2135     apply (auto simp: Re_exp Im_exp zero_less_mult_iff zero_le_mult_iff, fastforce+)
  2136     done
  2137   finally show ?thesis using assms csqrt_square
  2138     by simp
  2139 qed
  2140 
  2141 lemma csqrt_inverse:
  2142   assumes "z \<notin> \<real>\<^sub>\<le>\<^sub>0"
  2143     shows "csqrt (inverse z) = inverse (csqrt z)"
  2144 proof (cases "z=0", simp)
  2145   assume "z \<noteq> 0"
  2146   then show ?thesis
  2147     using assms csqrt_exp_Ln Ln_inverse exp_minus
  2148     by (simp add: csqrt_exp_Ln Ln_inverse exp_minus)
  2149 qed
  2150 
  2151 lemma cnj_csqrt:
  2152   assumes "z \<notin> \<real>\<^sub>\<le>\<^sub>0"
  2153     shows "cnj(csqrt z) = csqrt(cnj z)"
  2154 proof (cases "z=0", simp)
  2155   assume "z \<noteq> 0"
  2156   then show ?thesis
  2157      by (simp add: assms cnj_Ln csqrt_exp_Ln exp_cnj)
  2158 qed
  2159 
  2160 lemma has_field_derivative_csqrt:
  2161   assumes "z \<notin> \<real>\<^sub>\<le>\<^sub>0"
  2162     shows "(csqrt has_field_derivative inverse(2 * csqrt z)) (at z)"
  2163 proof -
  2164   have z: "z \<noteq> 0"
  2165     using assms by auto
  2166   then have *: "inverse z = inverse (2*z) * 2"
  2167     by (simp add: divide_simps)
  2168   have [simp]: "exp (Ln z / 2) * inverse z = inverse (csqrt z)"
  2169     by (simp add: z field_simps csqrt_exp_Ln [symmetric]) (metis power2_csqrt power2_eq_square)
  2170   have "Im z = 0 \<Longrightarrow> 0 < Re z"
  2171     using assms complex_nonpos_Reals_iff not_less by blast
  2172   with z have "((\<lambda>z. exp (Ln z / 2)) has_field_derivative inverse (2 * csqrt z)) (at z)"
  2173     by (force intro: derivative_eq_intros * simp add: assms)
  2174   then show ?thesis
  2175     apply (rule DERIV_transform_at[where d = "norm z"])
  2176     apply (intro z derivative_eq_intros | simp add: assms)+
  2177     using z
  2178     apply (metis csqrt_exp_Ln dist_0_norm less_irrefl)
  2179     done
  2180 qed
  2181 
  2182 lemma field_differentiable_at_csqrt:
  2183     "z \<notin> \<real>\<^sub>\<le>\<^sub>0 \<Longrightarrow> csqrt field_differentiable at z"
  2184   using field_differentiable_def has_field_derivative_csqrt by blast
  2185 
  2186 lemma field_differentiable_within_csqrt:
  2187     "z \<notin> \<real>\<^sub>\<le>\<^sub>0 \<Longrightarrow> csqrt field_differentiable (at z within s)"
  2188   using field_differentiable_at_csqrt field_differentiable_within_subset by blast
  2189 
  2190 lemma continuous_at_csqrt:
  2191     "z \<notin> \<real>\<^sub>\<le>\<^sub>0 \<Longrightarrow> continuous (at z) csqrt"
  2192   by (simp add: field_differentiable_within_csqrt field_differentiable_imp_continuous_at)
  2193 
  2194 corollary isCont_csqrt' [simp]:
  2195    "\<lbrakk>isCont f z; f z \<notin> \<real>\<^sub>\<le>\<^sub>0\<rbrakk> \<Longrightarrow> isCont (\<lambda>x. csqrt (f x)) z"
  2196   by (blast intro: isCont_o2 [OF _ continuous_at_csqrt])
  2197 
  2198 lemma continuous_within_csqrt:
  2199     "z \<notin> \<real>\<^sub>\<le>\<^sub>0 \<Longrightarrow> continuous (at z within s) csqrt"
  2200   by (simp add: field_differentiable_imp_continuous_at field_differentiable_within_csqrt)
  2201 
  2202 lemma continuous_on_csqrt [continuous_intros]:
  2203     "(\<And>z. z \<in> s \<Longrightarrow> z \<notin> \<real>\<^sub>\<le>\<^sub>0) \<Longrightarrow> continuous_on s csqrt"
  2204   by (simp add: continuous_at_imp_continuous_on continuous_within_csqrt)
  2205 
  2206 lemma holomorphic_on_csqrt:
  2207     "(\<And>z. z \<in> s \<Longrightarrow> z \<notin> \<real>\<^sub>\<le>\<^sub>0) \<Longrightarrow> csqrt holomorphic_on s"
  2208   by (simp add: field_differentiable_within_csqrt holomorphic_on_def)
  2209 
  2210 lemma continuous_within_closed_nontrivial:
  2211     "closed s \<Longrightarrow> a \<notin> s ==> continuous (at a within s) f"
  2212   using open_Compl
  2213   by (force simp add: continuous_def eventually_at_topological filterlim_iff open_Collect_neg)
  2214 
  2215 lemma continuous_within_csqrt_posreal:
  2216     "continuous (at z within (\<real> \<inter> {w. 0 \<le> Re(w)})) csqrt"
  2217 proof (cases "z \<in> \<real>\<^sub>\<le>\<^sub>0")
  2218   case True
  2219   then have "Im z = 0" "Re z < 0 \<or> z = 0"
  2220     using cnj.code complex_cnj_zero_iff  by (auto simp: Complex_eq complex_nonpos_Reals_iff) fastforce
  2221   then show ?thesis
  2222     apply (auto simp: continuous_within_closed_nontrivial [OF closed_Real_halfspace_Re_ge])
  2223     apply (auto simp: continuous_within_eps_delta norm_conv_dist [symmetric])
  2224     apply (rule_tac x="e^2" in exI)
  2225     apply (auto simp: Reals_def)
  2226     by (metis linear not_less real_sqrt_less_iff real_sqrt_pow2_iff real_sqrt_power)
  2227 next
  2228   case False
  2229     then show ?thesis   by (blast intro: continuous_within_csqrt)
  2230 qed
  2231 
  2232 subsection\<open>Complex arctangent\<close>
  2233 
  2234 text\<open>The branch cut gives standard bounds in the real case.\<close>
  2235 
  2236 definition Arctan :: "complex \<Rightarrow> complex" where
  2237     "Arctan \<equiv> \<lambda>z. (\<i>/2) * Ln((1 - \<i>*z) / (1 + \<i>*z))"
  2238 
  2239 lemma Arctan_def_moebius: "Arctan z = \<i>/2 * Ln (moebius (-\<i>) 1 \<i> 1 z)"
  2240   by (simp add: Arctan_def moebius_def add_ac)
  2241 
  2242 lemma Ln_conv_Arctan:
  2243   assumes "z \<noteq> -1"
  2244   shows   "Ln z = -2*\<i> * Arctan (moebius 1 (- 1) (- \<i>) (- \<i>) z)"
  2245 proof -
  2246   have "Arctan (moebius 1 (- 1) (- \<i>) (- \<i>) z) =
  2247              \<i>/2 * Ln (moebius (- \<i>) 1 \<i> 1 (moebius 1 (- 1) (- \<i>) (- \<i>) z))"
  2248     by (simp add: Arctan_def_moebius)
  2249   also from assms have "\<i> * z \<noteq> \<i> * (-1)" by (subst mult_left_cancel) simp
  2250   hence "\<i> * z - -\<i> \<noteq> 0" by (simp add: eq_neg_iff_add_eq_0)
  2251   from moebius_inverse'[OF _ this, of 1 1]
  2252     have "moebius (- \<i>) 1 \<i> 1 (moebius 1 (- 1) (- \<i>) (- \<i>) z) = z" by simp
  2253   finally show ?thesis by (simp add: field_simps)
  2254 qed
  2255 
  2256 lemma Arctan_0 [simp]: "Arctan 0 = 0"
  2257   by (simp add: Arctan_def)
  2258 
  2259 lemma Im_complex_div_lemma: "Im((1 - \<i>*z) / (1 + \<i>*z)) = 0 \<longleftrightarrow> Re z = 0"
  2260   by (auto simp: Im_complex_div_eq_0 algebra_simps)
  2261 
  2262 lemma Re_complex_div_lemma: "0 < Re((1 - \<i>*z) / (1 + \<i>*z)) \<longleftrightarrow> norm z < 1"
  2263   by (simp add: Re_complex_div_gt_0 algebra_simps cmod_def power2_eq_square)
  2264 
  2265 lemma tan_Arctan:
  2266   assumes "z\<^sup>2 \<noteq> -1"
  2267     shows [simp]:"tan(Arctan z) = z"
  2268 proof -
  2269   have "1 + \<i>*z \<noteq> 0"
  2270     by (metis assms complex_i_mult_minus i_squared minus_unique power2_eq_square power2_minus)
  2271   moreover
  2272   have "1 - \<i>*z \<noteq> 0"
  2273     by (metis assms complex_i_mult_minus i_squared power2_eq_square power2_minus right_minus_eq)
  2274   ultimately
  2275   show ?thesis
  2276     by (simp add: Arctan_def tan_def sin_exp_eq cos_exp_eq exp_minus csqrt_exp_Ln [symmetric]
  2277                   divide_simps power2_eq_square [symmetric])
  2278 qed
  2279 
  2280 lemma Arctan_tan [simp]:
  2281   assumes "\<bar>Re z\<bar> < pi/2"
  2282     shows "Arctan(tan z) = z"
  2283 proof -
  2284   have ge_pi2: "\<And>n::int. \<bar>of_int (2*n + 1) * pi/2\<bar> \<ge> pi/2"
  2285     by (case_tac n rule: int_cases) (auto simp: abs_mult)
  2286   have "exp (\<i>*z)*exp (\<i>*z) = -1 \<longleftrightarrow> exp (2*\<i>*z) = -1"
  2287     by (metis distrib_right exp_add mult_2)
  2288   also have "... \<longleftrightarrow> exp (2*\<i>*z) = exp (\<i>*pi)"
  2289     using cis_conv_exp cis_pi by auto
  2290   also have "... \<longleftrightarrow> exp (2*\<i>*z - \<i>*pi) = 1"
  2291     by (metis (no_types) diff_add_cancel diff_minus_eq_add exp_add exp_minus_inverse mult.commute)
  2292   also have "... \<longleftrightarrow> Re(\<i>*2*z - \<i>*pi) = 0 \<and> (\<exists>n::int. Im(\<i>*2*z - \<i>*pi) = of_int (2 * n) * pi)"
  2293     by (simp add: exp_eq_1)
  2294   also have "... \<longleftrightarrow> Im z = 0 \<and> (\<exists>n::int. 2 * Re z = of_int (2*n + 1) * pi)"
  2295     by (simp add: algebra_simps)
  2296   also have "... \<longleftrightarrow> False"
  2297     using assms ge_pi2
  2298     apply (auto simp: algebra_simps)
  2299     by (metis abs_mult_pos not_less of_nat_less_0_iff of_nat_numeral)
  2300   finally have *: "exp (\<i>*z)*exp (\<i>*z) + 1 \<noteq> 0"
  2301     by (auto simp: add.commute minus_unique)
  2302   show ?thesis
  2303     using assms *
  2304     apply (simp add: Arctan_def tan_def sin_exp_eq cos_exp_eq exp_minus divide_simps
  2305                      i_times_eq_iff power2_eq_square [symmetric])
  2306     apply (rule Ln_unique)
  2307     apply (auto simp: divide_simps exp_minus)
  2308     apply (simp add: algebra_simps exp_double [symmetric])
  2309     done
  2310 qed
  2311 
  2312 lemma
  2313   assumes "Re z = 0 \<Longrightarrow> \<bar>Im z\<bar> < 1"
  2314   shows Re_Arctan_bounds: "\<bar>Re(Arctan z)\<bar> < pi/2"
  2315     and has_field_derivative_Arctan: "(Arctan has_field_derivative inverse(1 + z\<^sup>2)) (at z)"
  2316 proof -
  2317   have nz0: "1 + \<i>*z \<noteq> 0"
  2318     using assms
  2319     by (metis abs_one complex_i_mult_minus diff_0_right diff_minus_eq_add imaginary_unit.simps
  2320               less_irrefl minus_diff_eq mult.right_neutral right_minus_eq)
  2321   have "z \<noteq> -\<i>" using assms
  2322     by auto
  2323   then have zz: "1 + z * z \<noteq> 0"
  2324     by (metis abs_one assms i_squared imaginary_unit.simps less_irrefl minus_unique square_eq_iff)
  2325   have nz1: "1 - \<i>*z \<noteq> 0"
  2326     using assms by (force simp add: i_times_eq_iff)
  2327   have nz2: "inverse (1 + \<i>*z) \<noteq> 0"
  2328     using assms
  2329     by (metis Im_complex_div_lemma Re_complex_div_lemma cmod_eq_Im divide_complex_def
  2330               less_irrefl mult_zero_right zero_complex.simps(1) zero_complex.simps(2))
  2331   have nzi: "((1 - \<i>*z) * inverse (1 + \<i>*z)) \<noteq> 0"
  2332     using nz1 nz2 by auto
  2333   have "Im ((1 - \<i>*z) / (1 + \<i>*z)) = 0 \<Longrightarrow> 0 < Re ((1 - \<i>*z) / (1 + \<i>*z))"
  2334     apply (simp add: divide_complex_def)
  2335     apply (simp add: divide_simps split: if_split_asm)
  2336     using assms
  2337     apply (auto simp: algebra_simps abs_square_less_1 [unfolded power2_eq_square])
  2338     done
  2339   then have *: "((1 - \<i>*z) / (1 + \<i>*z)) \<notin> \<real>\<^sub>\<le>\<^sub>0"
  2340     by (auto simp add: complex_nonpos_Reals_iff)
  2341   show "\<bar>Re(Arctan z)\<bar> < pi/2"
  2342     unfolding Arctan_def divide_complex_def
  2343     using mpi_less_Im_Ln [OF nzi]
  2344     apply (auto simp: abs_if intro!: Im_Ln_less_pi * [unfolded divide_complex_def])
  2345     done
  2346   show "(Arctan has_field_derivative inverse(1 + z\<^sup>2)) (at z)"
  2347     unfolding Arctan_def scaleR_conv_of_real
  2348     apply (rule DERIV_cong)
  2349     apply (intro derivative_eq_intros | simp add: nz0 *)+
  2350     using nz0 nz1 zz
  2351     apply (simp add: divide_simps power2_eq_square)
  2352     apply (auto simp: algebra_simps)
  2353     done
  2354 qed
  2355 
  2356 lemma field_differentiable_at_Arctan: "(Re z = 0 \<Longrightarrow> \<bar>Im z\<bar> < 1) \<Longrightarrow> Arctan field_differentiable at z"
  2357   using has_field_derivative_Arctan
  2358   by (auto simp: field_differentiable_def)
  2359 
  2360 lemma field_differentiable_within_Arctan:
  2361     "(Re z = 0 \<Longrightarrow> \<bar>Im z\<bar> < 1) \<Longrightarrow> Arctan field_differentiable (at z within s)"
  2362   using field_differentiable_at_Arctan field_differentiable_at_within by blast
  2363 
  2364 declare has_field_derivative_Arctan [derivative_intros]
  2365 declare has_field_derivative_Arctan [THEN DERIV_chain2, derivative_intros]
  2366 
  2367 lemma continuous_at_Arctan:
  2368     "(Re z = 0 \<Longrightarrow> \<bar>Im z\<bar> < 1) \<Longrightarrow> continuous (at z) Arctan"
  2369   by (simp add: field_differentiable_imp_continuous_at field_differentiable_within_Arctan)
  2370 
  2371 lemma continuous_within_Arctan:
  2372     "(Re z = 0 \<Longrightarrow> \<bar>Im z\<bar> < 1) \<Longrightarrow> continuous (at z within s) Arctan"
  2373   using continuous_at_Arctan continuous_at_imp_continuous_within by blast
  2374 
  2375 lemma continuous_on_Arctan [continuous_intros]:
  2376     "(\<And>z. z \<in> s \<Longrightarrow> Re z = 0 \<Longrightarrow> \<bar>Im z\<bar> < 1) \<Longrightarrow> continuous_on s Arctan"
  2377   by (auto simp: continuous_at_imp_continuous_on continuous_within_Arctan)
  2378 
  2379 lemma holomorphic_on_Arctan:
  2380     "(\<And>z. z \<in> s \<Longrightarrow> Re z = 0 \<Longrightarrow> \<bar>Im z\<bar> < 1) \<Longrightarrow> Arctan holomorphic_on s"
  2381   by (simp add: field_differentiable_within_Arctan holomorphic_on_def)
  2382 
  2383 lemma Arctan_series:
  2384   assumes z: "norm (z :: complex) < 1"
  2385   defines "g \<equiv> \<lambda>n. if odd n then -\<i>*\<i>^n / n else 0"
  2386   defines "h \<equiv> \<lambda>z n. (-1)^n / of_nat (2*n+1) * (z::complex)^(2*n+1)"
  2387   shows   "(\<lambda>n. g n * z^n) sums Arctan z"
  2388   and     "h z sums Arctan z"
  2389 proof -
  2390   define G where [abs_def]: "G z = (\<Sum>n. g n * z^n)" for z
  2391   have summable: "summable (\<lambda>n. g n * u^n)" if "norm u < 1" for u
  2392   proof (cases "u = 0")
  2393     assume u: "u \<noteq> 0"
  2394     have "(\<lambda>n. ereal (norm (h u n) / norm (h u (Suc n)))) = (\<lambda>n. ereal (inverse (norm u)^2) *
  2395               ereal ((2 + inverse (real (Suc n))) / (2 - inverse (real (Suc n)))))"
  2396     proof
  2397       fix n
  2398       have "ereal (norm (h u n) / norm (h u (Suc n))) =
  2399              ereal (inverse (norm u)^2) * ereal ((of_nat (2*Suc n+1) / of_nat (Suc n)) /
  2400                  (of_nat (2*Suc n-1) / of_nat (Suc n)))"
  2401       by (simp add: h_def norm_mult norm_power norm_divide divide_simps
  2402                     power2_eq_square eval_nat_numeral del: of_nat_add of_nat_Suc)
  2403       also have "of_nat (2*Suc n+1) / of_nat (Suc n) = (2::real) + inverse (real (Suc n))"
  2404         by (auto simp: divide_simps simp del: of_nat_Suc) simp_all?
  2405       also have "of_nat (2*Suc n-1) / of_nat (Suc n) = (2::real) - inverse (real (Suc n))"
  2406         by (auto simp: divide_simps simp del: of_nat_Suc) simp_all?
  2407       finally show "ereal (norm (h u n) / norm (h u (Suc n))) = ereal (inverse (norm u)^2) *
  2408               ereal ((2 + inverse (real (Suc n))) / (2 - inverse (real (Suc n))))" .
  2409     qed
  2410     also have "\<dots> \<longlonglongrightarrow> ereal (inverse (norm u)^2) * ereal ((2 + 0) / (2 - 0))"
  2411       by (intro tendsto_intros LIMSEQ_inverse_real_of_nat) simp_all
  2412     finally have "liminf (\<lambda>n. ereal (cmod (h u n) / cmod (h u (Suc n)))) = inverse (norm u)^2"
  2413       by (intro lim_imp_Liminf) simp_all
  2414     moreover from power_strict_mono[OF that, of 2] u have "inverse (norm u)^2 > 1"
  2415       by (simp add: divide_simps)
  2416     ultimately have A: "liminf (\<lambda>n. ereal (cmod (h u n) / cmod (h u (Suc n)))) > 1" by simp
  2417     from u have "summable (h u)"
  2418       by (intro summable_norm_cancel[OF ratio_test_convergence[OF _ A]])
  2419          (auto simp: h_def norm_divide norm_mult norm_power simp del: of_nat_Suc
  2420                intro!: mult_pos_pos divide_pos_pos always_eventually)
  2421     thus "summable (\<lambda>n. g n * u^n)"
  2422       by (subst summable_mono_reindex[of "\<lambda>n. 2*n+1", symmetric])
  2423          (auto simp: power_mult subseq_def g_def h_def elim!: oddE)
  2424   qed (simp add: h_def)
  2425 
  2426   have "\<exists>c. \<forall>u\<in>ball 0 1. Arctan u - G u = c"
  2427   proof (rule has_field_derivative_zero_constant)
  2428     fix u :: complex assume "u \<in> ball 0 1"
  2429     hence u: "norm u < 1" by (simp add: dist_0_norm)
  2430     define K where "K = (norm u + 1) / 2"
  2431     from u and abs_Im_le_cmod[of u] have Im_u: "\<bar>Im u\<bar> < 1" by linarith
  2432     from u have K: "0 \<le> K" "norm u < K" "K < 1" by (simp_all add: K_def)
  2433     hence "(G has_field_derivative (\<Sum>n. diffs g n * u ^ n)) (at u)" unfolding G_def
  2434       by (intro termdiffs_strong[of _ "of_real K"] summable) simp_all
  2435     also have "(\<lambda>n. diffs g n * u^n) = (\<lambda>n. if even n then (\<i>*u)^n else 0)"
  2436       by (intro ext) (simp_all del: of_nat_Suc add: g_def diffs_def power_mult_distrib)
  2437     also have "suminf \<dots> = (\<Sum>n. (-(u^2))^n)"
  2438       by (subst suminf_mono_reindex[of "\<lambda>n. 2*n", symmetric])
  2439          (auto elim!: evenE simp: subseq_def power_mult power_mult_distrib)
  2440     also from u have "norm u^2 < 1^2" by (intro power_strict_mono) simp_all
  2441     hence "(\<Sum>n. (-(u^2))^n) = inverse (1 + u^2)"
  2442       by (subst suminf_geometric) (simp_all add: norm_power inverse_eq_divide)
  2443     finally have "(G has_field_derivative inverse (1 + u\<^sup>2)) (at u)" .
  2444     from DERIV_diff[OF has_field_derivative_Arctan this] Im_u u
  2445       show "((\<lambda>u. Arctan u - G u) has_field_derivative 0) (at u within ball 0 1)"
  2446       by (simp_all add: dist_0_norm at_within_open[OF _ open_ball])
  2447   qed simp_all
  2448   then obtain c where c: "\<And>u. norm u < 1 \<Longrightarrow> Arctan u - G u = c" by (auto simp: dist_0_norm)
  2449   from this[of 0] have "c = 0" by (simp add: G_def g_def powser_zero)
  2450   with c z have "Arctan z = G z" by simp
  2451   with summable[OF z] show "(\<lambda>n. g n * z^n) sums Arctan z" unfolding G_def by (simp add: sums_iff)
  2452   thus "h z sums Arctan z" by (subst (asm) sums_mono_reindex[of "\<lambda>n. 2*n+1", symmetric])
  2453                               (auto elim!: oddE simp: subseq_def power_mult g_def h_def)
  2454 qed
  2455 
  2456 text \<open>A quickly-converging series for the logarithm, based on the arctangent.\<close>
  2457 lemma ln_series_quadratic:
  2458   assumes x: "x > (0::real)"
  2459   shows "(\<lambda>n. (2*((x - 1) / (x + 1)) ^ (2*n+1) / of_nat (2*n+1))) sums ln x"
  2460 proof -
  2461   define y :: complex where "y = of_real ((x-1)/(x+1))"
  2462   from x have x': "complex_of_real x \<noteq> of_real (-1)"  by (subst of_real_eq_iff) auto
  2463   from x have "\<bar>x - 1\<bar> < \<bar>x + 1\<bar>" by linarith
  2464   hence "norm (complex_of_real (x - 1) / complex_of_real (x + 1)) < 1"
  2465     by (simp add: norm_divide del: of_real_add of_real_diff)
  2466   hence "norm (\<i> * y) < 1" unfolding y_def by (subst norm_mult) simp
  2467   hence "(\<lambda>n. (-2*\<i>) * ((-1)^n / of_nat (2*n+1) * (\<i>*y)^(2*n+1))) sums ((-2*\<i>) * Arctan (\<i>*y))"
  2468     by (intro Arctan_series sums_mult) simp_all
  2469   also have "(\<lambda>n. (-2*\<i>) * ((-1)^n / of_nat (2*n+1) * (\<i>*y)^(2*n+1))) =
  2470                  (\<lambda>n. (-2*\<i>) * ((-1)^n * (\<i>*y*(-y\<^sup>2)^n)/of_nat (2*n+1)))"
  2471     by (intro ext) (simp_all add: power_mult power_mult_distrib)
  2472   also have "\<dots> = (\<lambda>n. 2*y* ((-1) * (-y\<^sup>2))^n/of_nat (2*n+1))"
  2473     by (intro ext, subst power_mult_distrib) (simp add: algebra_simps power_mult)
  2474   also have "\<dots> = (\<lambda>n. 2*y^(2*n+1) / of_nat (2*n+1))"
  2475     by (subst power_add, subst power_mult) (simp add: mult_ac)
  2476   also have "\<dots> = (\<lambda>n. of_real (2*((x-1)/(x+1))^(2*n+1) / of_nat (2*n+1)))"
  2477     by (intro ext) (simp add: y_def)
  2478   also have "\<i> * y = (of_real x - 1) / (-\<i> * (of_real x + 1))"
  2479     by (subst divide_divide_eq_left [symmetric]) (simp add: y_def)
  2480   also have "\<dots> = moebius 1 (-1) (-\<i>) (-\<i>) (of_real x)" by (simp add: moebius_def algebra_simps)
  2481   also from x' have "-2*\<i>*Arctan \<dots> = Ln (of_real x)" by (intro Ln_conv_Arctan [symmetric]) simp_all
  2482   also from x have "\<dots> = ln x" by (rule Ln_of_real)
  2483   finally show ?thesis by (subst (asm) sums_of_real_iff)
  2484 qed
  2485 
  2486 subsection \<open>Real arctangent\<close>
  2487 
  2488 lemma norm_exp_i_times [simp]: "norm (exp(\<i> * of_real y)) = 1"
  2489   by simp
  2490 
  2491 lemma norm_exp_imaginary: "norm(exp z) = 1 \<Longrightarrow> Re z = 0"
  2492   by simp
  2493 
  2494 lemma Im_Arctan_of_real [simp]: "Im (Arctan (of_real x)) = 0"
  2495   unfolding Arctan_def divide_complex_def
  2496   apply (simp add: complex_eq_iff)
  2497   apply (rule norm_exp_imaginary)
  2498   apply (subst exp_Ln, auto)
  2499   apply (simp_all add: cmod_def complex_eq_iff)
  2500   apply (auto simp: divide_simps)
  2501   apply (metis power_one sum_power2_eq_zero_iff zero_neq_one, algebra)
  2502   done
  2503 
  2504 lemma arctan_eq_Re_Arctan: "arctan x = Re (Arctan (of_real x))"
  2505 proof (rule arctan_unique)
  2506   show "- (pi / 2) < Re (Arctan (complex_of_real x))"
  2507     apply (simp add: Arctan_def)
  2508     apply (rule Im_Ln_less_pi)
  2509     apply (auto simp: Im_complex_div_lemma complex_nonpos_Reals_iff)
  2510     done
  2511 next
  2512   have *: " (1 - \<i>*x) / (1 + \<i>*x) \<noteq> 0"
  2513     by (simp add: divide_simps) ( simp add: complex_eq_iff)
  2514   show "Re (Arctan (complex_of_real x)) < pi / 2"
  2515     using mpi_less_Im_Ln [OF *]
  2516     by (simp add: Arctan_def)
  2517 next
  2518   have "tan (Re (Arctan (of_real x))) = Re (tan (Arctan (of_real x)))"
  2519     apply (auto simp: tan_def Complex.Re_divide Re_sin Re_cos Im_sin Im_cos)
  2520     apply (simp add: field_simps)
  2521     by (simp add: power2_eq_square)
  2522   also have "... = x"
  2523     apply (subst tan_Arctan, auto)
  2524     by (metis diff_0_right minus_diff_eq mult_zero_left not_le of_real_1 of_real_eq_iff of_real_minus of_real_power power2_eq_square real_minus_mult_self_le zero_less_one)
  2525   finally show "tan (Re (Arctan (complex_of_real x))) = x" .
  2526 qed
  2527 
  2528 lemma Arctan_of_real: "Arctan (of_real x) = of_real (arctan x)"
  2529   unfolding arctan_eq_Re_Arctan divide_complex_def
  2530   by (simp add: complex_eq_iff)
  2531 
  2532 lemma Arctan_in_Reals [simp]: "z \<in> \<real> \<Longrightarrow> Arctan z \<in> \<real>"
  2533   by (metis Reals_cases Reals_of_real Arctan_of_real)
  2534 
  2535 declare arctan_one [simp]
  2536 
  2537 lemma arctan_less_pi4_pos: "x < 1 \<Longrightarrow> arctan x < pi/4"
  2538   by (metis arctan_less_iff arctan_one)
  2539 
  2540 lemma arctan_less_pi4_neg: "-1 < x \<Longrightarrow> -(pi/4) < arctan x"
  2541   by (metis arctan_less_iff arctan_minus arctan_one)
  2542 
  2543 lemma arctan_less_pi4: "\<bar>x\<bar> < 1 \<Longrightarrow> \<bar>arctan x\<bar> < pi/4"
  2544   by (metis abs_less_iff arctan_less_pi4_pos arctan_minus)
  2545 
  2546 lemma arctan_le_pi4: "\<bar>x\<bar> \<le> 1 \<Longrightarrow> \<bar>arctan x\<bar> \<le> pi/4"
  2547   by (metis abs_le_iff arctan_le_iff arctan_minus arctan_one)
  2548 
  2549 lemma abs_arctan: "\<bar>arctan x\<bar> = arctan \<bar>x\<bar>"
  2550   by (simp add: abs_if arctan_minus)
  2551 
  2552 lemma arctan_add_raw:
  2553   assumes "\<bar>arctan x + arctan y\<bar> < pi/2"
  2554     shows "arctan x + arctan y = arctan((x + y) / (1 - x * y))"
  2555 proof (rule arctan_unique [symmetric])
  2556   show 12: "- (pi / 2) < arctan x + arctan y" "arctan x + arctan y < pi / 2"
  2557     using assms by linarith+
  2558   show "tan (arctan x + arctan y) = (x + y) / (1 - x * y)"
  2559     using cos_gt_zero_pi [OF 12]
  2560     by (simp add: arctan tan_add)
  2561 qed
  2562 
  2563 lemma arctan_inverse:
  2564   assumes "0 < x"
  2565     shows "arctan(inverse x) = pi/2 - arctan x"
  2566 proof -
  2567   have "arctan(inverse x) = arctan(inverse(tan(arctan x)))"
  2568     by (simp add: arctan)
  2569   also have "... = arctan (tan (pi / 2 - arctan x))"
  2570     by (simp add: tan_cot)
  2571   also have "... = pi/2 - arctan x"
  2572   proof -
  2573     have "0 < pi - arctan x"
  2574     using arctan_ubound [of x] pi_gt_zero by linarith
  2575     with assms show ?thesis
  2576       by (simp add: Transcendental.arctan_tan)
  2577   qed
  2578   finally show ?thesis .
  2579 qed
  2580 
  2581 lemma arctan_add_small:
  2582   assumes "\<bar>x * y\<bar> < 1"
  2583     shows "(arctan x + arctan y = arctan((x + y) / (1 - x * y)))"
  2584 proof (cases "x = 0 \<or> y = 0")
  2585   case True then show ?thesis
  2586     by auto
  2587 next
  2588   case False
  2589   then have *: "\<bar>arctan x\<bar> < pi / 2 - \<bar>arctan y\<bar>" using assms
  2590     apply (auto simp add: abs_arctan arctan_inverse [symmetric] arctan_less_iff)
  2591     apply (simp add: divide_simps abs_mult)
  2592     done
  2593   show ?thesis
  2594     apply (rule arctan_add_raw)
  2595     using * by linarith
  2596 qed
  2597 
  2598 lemma abs_arctan_le:
  2599   fixes x::real shows "\<bar>arctan x\<bar> \<le> \<bar>x\<bar>"
  2600 proof -
  2601   { fix w::complex and z::complex
  2602     assume *: "w \<in> \<real>" "z \<in> \<real>"
  2603     have "cmod (Arctan w - Arctan z) \<le> 1 * cmod (w-z)"
  2604       apply (rule field_differentiable_bound [OF convex_Reals, of Arctan _ 1])
  2605       apply (rule has_field_derivative_at_within [OF has_field_derivative_Arctan])
  2606       apply (force simp add: Reals_def)
  2607       apply (simp add: norm_divide divide_simps in_Reals_norm complex_is_Real_iff power2_eq_square)
  2608       using * by auto
  2609   }
  2610   then have "cmod (Arctan (of_real x) - Arctan 0) \<le> 1 * cmod (of_real x -0)"
  2611     using Reals_0 Reals_of_real by blast
  2612   then show ?thesis
  2613     by (simp add: Arctan_of_real)
  2614 qed
  2615 
  2616 lemma arctan_le_self: "0 \<le> x \<Longrightarrow> arctan x \<le> x"
  2617   by (metis abs_arctan_le abs_of_nonneg zero_le_arctan_iff)
  2618 
  2619 lemma abs_tan_ge: "\<bar>x\<bar> < pi/2 \<Longrightarrow> \<bar>x\<bar> \<le> \<bar>tan x\<bar>"
  2620   by (metis abs_arctan_le abs_less_iff arctan_tan minus_less_iff)
  2621 
  2622 lemma arctan_bounds:
  2623   assumes "0 \<le> x" "x < 1"
  2624   shows arctan_lower_bound:
  2625     "(\<Sum>k<2 * n. (- 1) ^ k * (1 / real (k * 2 + 1) * x ^ (k * 2 + 1))) \<le> arctan x"
  2626     (is "(\<Sum>k<_. (- 1)^ k * ?a k) \<le> _")
  2627     and arctan_upper_bound:
  2628     "arctan x \<le> (\<Sum>k<2 * n + 1. (- 1) ^ k * (1 / real (k * 2 + 1) * x ^ (k * 2 + 1)))"
  2629 proof -
  2630   have tendsto_zero: "?a \<longlonglongrightarrow> 0"
  2631     using assms
  2632     apply -
  2633     apply (rule tendsto_eq_rhs[where x="0 * 0"])
  2634     subgoal by (intro tendsto_mult real_tendsto_divide_at_top)
  2635         (auto simp: filterlim_real_sequentially filterlim_sequentially_iff_filterlim_real
  2636           intro!: real_tendsto_divide_at_top tendsto_power_zero filterlim_real_sequentially
  2637            tendsto_eq_intros filterlim_at_top_mult_tendsto_pos filterlim_tendsto_add_at_top)
  2638     subgoal by simp
  2639     done
  2640   have nonneg: "0 \<le> ?a n" for n
  2641     by (force intro!: divide_nonneg_nonneg mult_nonneg_nonneg zero_le_power assms)
  2642   have le: "?a (Suc n) \<le> ?a n" for n
  2643     by (rule mult_mono[OF _ power_decreasing]) (auto simp: divide_simps assms less_imp_le)
  2644   from summable_Leibniz'(4)[of ?a, OF tendsto_zero nonneg le, of n]
  2645     summable_Leibniz'(2)[of ?a, OF tendsto_zero nonneg le, of n]
  2646     assms
  2647   show "(\<Sum>k<2*n. (- 1)^ k * ?a k) \<le> arctan x" "arctan x \<le> (\<Sum>k<2 * n + 1. (- 1)^ k * ?a k)"
  2648     by (auto simp: arctan_series)
  2649 qed
  2650 
  2651 subsection \<open>Bounds on pi using real arctangent\<close>
  2652 
  2653 lemma pi_machin: "pi = 16 * arctan (1 / 5) - 4 * arctan (1 / 239)"
  2654   using machin
  2655   by simp
  2656 
  2657 lemma pi_approx: "3.141592653588 \<le> pi" "pi \<le> 3.1415926535899"
  2658   unfolding pi_machin
  2659   using arctan_bounds[of "1/5"   4]
  2660         arctan_bounds[of "1/239" 4]
  2661   by (simp_all add: eval_nat_numeral)
  2662     
  2663 corollary pi_gt3: "pi > 3"
  2664   using pi_approx by simp
  2665 
  2666 
  2667 subsection\<open>Inverse Sine\<close>
  2668 
  2669 definition Arcsin :: "complex \<Rightarrow> complex" where
  2670    "Arcsin \<equiv> \<lambda>z. -\<i> * Ln(\<i> * z + csqrt(1 - z\<^sup>2))"
  2671 
  2672 lemma Arcsin_body_lemma: "\<i> * z + csqrt(1 - z\<^sup>2) \<noteq> 0"
  2673   using power2_csqrt [of "1 - z\<^sup>2"]
  2674   apply auto
  2675   by (metis complex_i_mult_minus diff_add_cancel diff_minus_eq_add diff_self mult.assoc mult.left_commute numeral_One power2_csqrt power2_eq_square zero_neq_numeral)
  2676 
  2677 lemma Arcsin_range_lemma: "\<bar>Re z\<bar> < 1 \<Longrightarrow> 0 < Re(\<i> * z + csqrt(1 - z\<^sup>2))"
  2678   using Complex.cmod_power2 [of z, symmetric]
  2679   by (simp add: real_less_rsqrt algebra_simps Re_power2 cmod_square_less_1_plus)
  2680 
  2681 lemma Re_Arcsin: "Re(Arcsin z) = Im (Ln (\<i> * z + csqrt(1 - z\<^sup>2)))"
  2682   by (simp add: Arcsin_def)
  2683 
  2684 lemma Im_Arcsin: "Im(Arcsin z) = - ln (cmod (\<i> * z + csqrt (1 - z\<^sup>2)))"
  2685   by (simp add: Arcsin_def Arcsin_body_lemma)
  2686 
  2687 lemma one_minus_z2_notin_nonpos_Reals:
  2688   assumes "(Im z = 0 \<Longrightarrow> \<bar>Re z\<bar> < 1)"
  2689   shows "1 - z\<^sup>2 \<notin> \<real>\<^sub>\<le>\<^sub>0"
  2690     using assms
  2691     apply (auto simp: complex_nonpos_Reals_iff Re_power2 Im_power2)
  2692     using power2_less_0 [of "Im z"] apply force
  2693     using abs_square_less_1 not_le by blast
  2694 
  2695 lemma isCont_Arcsin_lemma:
  2696   assumes le0: "Re (\<i> * z + csqrt (1 - z\<^sup>2)) \<le> 0" and "(Im z = 0 \<Longrightarrow> \<bar>Re z\<bar> < 1)"
  2697     shows False
  2698 proof (cases "Im z = 0")
  2699   case True
  2700   then show ?thesis
  2701     using assms by (fastforce simp: cmod_def abs_square_less_1 [symmetric])
  2702 next
  2703   case False
  2704   have neq: "(cmod z)\<^sup>2 \<noteq> 1 + cmod (1 - z\<^sup>2)"
  2705   proof (clarsimp simp add: cmod_def)
  2706     assume "(Re z)\<^sup>2 + (Im z)\<^sup>2 = 1 + sqrt ((1 - Re (z\<^sup>2))\<^sup>2 + (Im (z\<^sup>2))\<^sup>2)"
  2707     then have "((Re z)\<^sup>2 + (Im z)\<^sup>2 - 1)\<^sup>2 = ((1 - Re (z\<^sup>2))\<^sup>2 + (Im (z\<^sup>2))\<^sup>2)"
  2708       by simp
  2709     then show False using False
  2710       by (simp add: power2_eq_square algebra_simps)
  2711   qed
  2712   moreover have 2: "(Im z)\<^sup>2 = (1 + ((Im z)\<^sup>2 + cmod (1 - z\<^sup>2)) - (Re z)\<^sup>2) / 2"
  2713     using le0
  2714     apply simp
  2715     apply (drule sqrt_le_D)
  2716     using cmod_power2 [of z] norm_triangle_ineq2 [of "z^2" 1]
  2717     apply (simp add: norm_power Re_power2 norm_minus_commute [of 1])
  2718     done
  2719   ultimately show False
  2720     by (simp add: Re_power2 Im_power2 cmod_power2)
  2721 qed
  2722 
  2723 lemma isCont_Arcsin:
  2724   assumes "(Im z = 0 \<Longrightarrow> \<bar>Re z\<bar> < 1)"
  2725     shows "isCont Arcsin z"
  2726 proof -
  2727   have *: "\<i> * z + csqrt (1 - z\<^sup>2) \<notin> \<real>\<^sub>\<le>\<^sub>0"
  2728     by (metis isCont_Arcsin_lemma assms complex_nonpos_Reals_iff)
  2729   show ?thesis
  2730     using assms
  2731     apply (simp add: Arcsin_def)
  2732     apply (rule isCont_Ln' isCont_csqrt' continuous_intros)+
  2733     apply (simp add: one_minus_z2_notin_nonpos_Reals assms)
  2734     apply (rule *)
  2735     done
  2736 qed
  2737 
  2738 lemma isCont_Arcsin' [simp]:
  2739   shows "isCont f z \<Longrightarrow> (Im (f z) = 0 \<Longrightarrow> \<bar>Re (f z)\<bar> < 1) \<Longrightarrow> isCont (\<lambda>x. Arcsin (f x)) z"
  2740   by (blast intro: isCont_o2 [OF _ isCont_Arcsin])
  2741 
  2742 lemma sin_Arcsin [simp]: "sin(Arcsin z) = z"
  2743 proof -
  2744   have "\<i>*z*2 + csqrt (1 - z\<^sup>2)*2 = 0 \<longleftrightarrow> (\<i>*z)*2 + csqrt (1 - z\<^sup>2)*2 = 0"
  2745     by (simp add: algebra_simps)  \<comment>\<open>Cancelling a factor of 2\<close>
  2746   moreover have "... \<longleftrightarrow> (\<i>*z) + csqrt (1 - z\<^sup>2) = 0"
  2747     by (metis Arcsin_body_lemma distrib_right no_zero_divisors zero_neq_numeral)
  2748   ultimately show ?thesis
  2749     apply (simp add: sin_exp_eq Arcsin_def Arcsin_body_lemma exp_minus divide_simps)
  2750     apply (simp add: algebra_simps)
  2751     apply (simp add: power2_eq_square [symmetric] algebra_simps)
  2752     done
  2753 qed
  2754 
  2755 lemma Re_eq_pihalf_lemma:
  2756     "\<bar>Re z\<bar> = pi/2 \<Longrightarrow> Im z = 0 \<Longrightarrow>
  2757       Re ((exp (\<i>*z) + inverse (exp (\<i>*z))) / 2) = 0 \<and> 0 \<le> Im ((exp (\<i>*z) + inverse (exp (\<i>*z))) / 2)"
  2758   apply (simp add: cos_i_times [symmetric] Re_cos Im_cos abs_if del: eq_divide_eq_numeral1)
  2759   by (metis cos_minus cos_pi_half)
  2760 
  2761 lemma Re_less_pihalf_lemma:
  2762   assumes "\<bar>Re z\<bar> < pi / 2"
  2763     shows "0 < Re ((exp (\<i>*z) + inverse (exp (\<i>*z))) / 2)"
  2764 proof -
  2765   have "0 < cos (Re z)" using assms
  2766     using cos_gt_zero_pi by auto
  2767   then show ?thesis
  2768     by (simp add: cos_i_times [symmetric] Re_cos Im_cos add_pos_pos)
  2769 qed
  2770 
  2771 lemma Arcsin_sin:
  2772     assumes "\<bar>Re z\<bar> < pi/2 \<or> (\<bar>Re z\<bar> = pi/2 \<and> Im z = 0)"
  2773       shows "Arcsin(sin z) = z"
  2774 proof -
  2775   have "Arcsin(sin z) = - (\<i> * Ln (csqrt (1 - (\<i> * (exp (\<i>*z) - inverse (exp (\<i>*z))))\<^sup>2 / 4) - (inverse (exp (\<i>*z)) - exp (\<i>*z)) / 2))"
  2776     by (simp add: sin_exp_eq Arcsin_def exp_minus power_divide)
  2777   also have "... = - (\<i> * Ln (csqrt (((exp (\<i>*z) + inverse (exp (\<i>*z)))/2)\<^sup>2) - (inverse (exp (\<i>*z)) - exp (\<i>*z)) / 2))"
  2778     by (simp add: field_simps power2_eq_square)
  2779   also have "... = - (\<i> * Ln (((exp (\<i>*z) + inverse (exp (\<i>*z)))/2) - (inverse (exp (\<i>*z)) - exp (\<i>*z)) / 2))"
  2780     apply (subst csqrt_square)
  2781     using assms Re_eq_pihalf_lemma Re_less_pihalf_lemma
  2782     apply auto
  2783     done
  2784   also have "... =  - (\<i> * Ln (exp (\<i>*z)))"
  2785     by (simp add: field_simps power2_eq_square)
  2786   also have "... = z"
  2787     apply (subst Complex_Transcendental.Ln_exp)
  2788     using assms
  2789     apply (auto simp: abs_if simp del: eq_divide_eq_numeral1 split: if_split_asm)
  2790     done
  2791   finally show ?thesis .
  2792 qed
  2793 
  2794 lemma Arcsin_unique:
  2795     "\<lbrakk>sin z = w; \<bar>Re z\<bar> < pi/2 \<or> (\<bar>Re z\<bar> = pi/2 \<and> Im z = 0)\<rbrakk> \<Longrightarrow> Arcsin w = z"
  2796   by (metis Arcsin_sin)
  2797 
  2798 lemma Arcsin_0 [simp]: "Arcsin 0 = 0"
  2799   by (metis Arcsin_sin norm_zero pi_half_gt_zero real_norm_def sin_zero zero_complex.simps(1))
  2800 
  2801 lemma Arcsin_1 [simp]: "Arcsin 1 = pi/2"
  2802   by (metis Arcsin_sin Im_complex_of_real Re_complex_of_real numeral_One of_real_numeral pi_half_ge_zero real_sqrt_abs real_sqrt_pow2 real_sqrt_power sin_of_real sin_pi_half)
  2803 
  2804 lemma Arcsin_minus_1 [simp]: "Arcsin(-1) = - (pi/2)"
  2805   by (metis Arcsin_1 Arcsin_sin Im_complex_of_real Re_complex_of_real abs_of_nonneg of_real_minus pi_half_ge_zero power2_minus real_sqrt_abs sin_Arcsin sin_minus)
  2806 
  2807 lemma has_field_derivative_Arcsin:
  2808   assumes "(Im z = 0 \<Longrightarrow> \<bar>Re z\<bar> < 1)"
  2809     shows "(Arcsin has_field_derivative inverse(cos(Arcsin z))) (at z)"
  2810 proof -
  2811   have "(sin (Arcsin z))\<^sup>2 \<noteq> 1"
  2812     using assms
  2813     apply atomize
  2814     apply (auto simp: complex_eq_iff Re_power2 Im_power2 abs_square_eq_1)
  2815     apply (metis abs_minus_cancel abs_one abs_power2 numeral_One numeral_neq_neg_one)
  2816     by (metis abs_minus_cancel abs_one abs_power2 one_neq_neg_one)
  2817   then have "cos (Arcsin z) \<noteq> 0"
  2818     by (metis diff_0_right power_zero_numeral sin_squared_eq)
  2819   then show ?thesis
  2820     apply (rule has_complex_derivative_inverse_basic [OF DERIV_sin _ _ open_ball [of z 1]])
  2821     apply (auto intro: isCont_Arcsin assms)
  2822     done
  2823 qed
  2824 
  2825 declare has_field_derivative_Arcsin [derivative_intros]
  2826 declare has_field_derivative_Arcsin [THEN DERIV_chain2, derivative_intros]
  2827 
  2828 lemma field_differentiable_at_Arcsin:
  2829     "(Im z = 0 \<Longrightarrow> \<bar>Re z\<bar> < 1) \<Longrightarrow> Arcsin field_differentiable at z"
  2830   using field_differentiable_def has_field_derivative_Arcsin by blast
  2831 
  2832 lemma field_differentiable_within_Arcsin:
  2833     "(Im z = 0 \<Longrightarrow> \<bar>Re z\<bar> < 1) \<Longrightarrow> Arcsin field_differentiable (at z within s)"
  2834   using field_differentiable_at_Arcsin field_differentiable_within_subset by blast
  2835 
  2836 lemma continuous_within_Arcsin:
  2837     "(Im z = 0 \<Longrightarrow> \<bar>Re z\<bar> < 1) \<Longrightarrow> continuous (at z within s) Arcsin"
  2838   using continuous_at_imp_continuous_within isCont_Arcsin by blast
  2839 
  2840 lemma continuous_on_Arcsin [continuous_intros]:
  2841     "(\<And>z. z \<in> s \<Longrightarrow> Im z = 0 \<Longrightarrow> \<bar>Re z\<bar> < 1) \<Longrightarrow> continuous_on s Arcsin"
  2842   by (simp add: continuous_at_imp_continuous_on)
  2843 
  2844 lemma holomorphic_on_Arcsin: "(\<And>z. z \<in> s \<Longrightarrow> Im z = 0 \<Longrightarrow> \<bar>Re z\<bar> < 1) \<Longrightarrow> Arcsin holomorphic_on s"
  2845   by (simp add: field_differentiable_within_Arcsin holomorphic_on_def)
  2846 
  2847 
  2848 subsection\<open>Inverse Cosine\<close>
  2849 
  2850 definition Arccos :: "complex \<Rightarrow> complex" where
  2851    "Arccos \<equiv> \<lambda>z. -\<i> * Ln(z + \<i> * csqrt(1 - z\<^sup>2))"
  2852 
  2853 lemma Arccos_range_lemma: "\<bar>Re z\<bar> < 1 \<Longrightarrow> 0 < Im(z + \<i> * csqrt(1 - z\<^sup>2))"
  2854   using Arcsin_range_lemma [of "-z"]
  2855   by simp
  2856 
  2857 lemma Arccos_body_lemma: "z + \<i> * csqrt(1 - z\<^sup>2) \<noteq> 0"
  2858   using Arcsin_body_lemma [of z]
  2859   by (metis complex_i_mult_minus diff_add_cancel minus_diff_eq minus_unique mult.assoc mult.left_commute
  2860            power2_csqrt power2_eq_square zero_neq_one)
  2861 
  2862 lemma Re_Arccos: "Re(Arccos z) = Im (Ln (z + \<i> * csqrt(1 - z\<^sup>2)))"
  2863   by (simp add: Arccos_def)
  2864 
  2865 lemma Im_Arccos: "Im(Arccos z) = - ln (cmod (z + \<i> * csqrt (1 - z\<^sup>2)))"
  2866   by (simp add: Arccos_def Arccos_body_lemma)
  2867 
  2868 text\<open>A very tricky argument to find!\<close>
  2869 lemma isCont_Arccos_lemma:
  2870   assumes eq0: "Im (z + \<i> * csqrt (1 - z\<^sup>2)) = 0" and "(Im z = 0 \<Longrightarrow> \<bar>Re z\<bar> < 1)"
  2871     shows False
  2872 proof (cases "Im z = 0")
  2873   case True
  2874   then show ?thesis
  2875     using assms by (fastforce simp add: cmod_def abs_square_less_1 [symmetric])
  2876 next
  2877   case False
  2878   have Imz: "Im z = - sqrt ((1 + ((Im z)\<^sup>2 + cmod (1 - z\<^sup>2)) - (Re z)\<^sup>2) / 2)"
  2879     using eq0 abs_Re_le_cmod [of "1-z\<^sup>2"]
  2880     by (simp add: Re_power2 algebra_simps)
  2881   have "(cmod z)\<^sup>2 - 1 \<noteq> cmod (1 - z\<^sup>2)"
  2882   proof (clarsimp simp add: cmod_def)
  2883     assume "(Re z)\<^sup>2 + (Im z)\<^sup>2 - 1 = sqrt ((1 - Re (z\<^sup>2))\<^sup>2 + (Im (z\<^sup>2))\<^sup>2)"
  2884     then have "((Re z)\<^sup>2 + (Im z)\<^sup>2 - 1)\<^sup>2 = ((1 - Re (z\<^sup>2))\<^sup>2 + (Im (z\<^sup>2))\<^sup>2)"
  2885       by simp
  2886     then show False using False
  2887       by (simp add: power2_eq_square algebra_simps)
  2888   qed
  2889   moreover have "(Im z)\<^sup>2 = ((1 + ((Im z)\<^sup>2 + cmod (1 - z\<^sup>2)) - (Re z)\<^sup>2) / 2)"
  2890     apply (subst Imz)
  2891     using abs_Re_le_cmod [of "1-z\<^sup>2"]
  2892     apply (simp add: Re_power2)
  2893     done
  2894   ultimately show False
  2895     by (simp add: cmod_power2)
  2896 qed
  2897 
  2898 lemma isCont_Arccos:
  2899   assumes "(Im z = 0 \<Longrightarrow> \<bar>Re z\<bar> < 1)"
  2900     shows "isCont Arccos z"
  2901 proof -
  2902   have "z + \<i> * csqrt (1 - z\<^sup>2) \<notin> \<real>\<^sub>\<le>\<^sub>0"
  2903     by (metis complex_nonpos_Reals_iff isCont_Arccos_lemma assms)
  2904   with assms show ?thesis
  2905     apply (simp add: Arccos_def)
  2906     apply (rule isCont_Ln' isCont_csqrt' continuous_intros)+
  2907     apply (simp_all add: one_minus_z2_notin_nonpos_Reals assms)
  2908     done
  2909 qed
  2910 
  2911 lemma isCont_Arccos' [simp]:
  2912   shows "isCont f z \<Longrightarrow> (Im (f z) = 0 \<Longrightarrow> \<bar>Re (f z)\<bar> < 1) \<Longrightarrow> isCont (\<lambda>x. Arccos (f x)) z"
  2913   by (blast intro: isCont_o2 [OF _ isCont_Arccos])
  2914 
  2915 lemma cos_Arccos [simp]: "cos(Arccos z) = z"
  2916 proof -
  2917   have "z*2 + \<i> * (2 * csqrt (1 - z\<^sup>2)) = 0 \<longleftrightarrow> z*2 + \<i> * csqrt (1 - z\<^sup>2)*2 = 0"
  2918     by (simp add: algebra_simps)  \<comment>\<open>Cancelling a factor of 2\<close>
  2919   moreover have "... \<longleftrightarrow> z + \<i> * csqrt (1 - z\<^sup>2) = 0"
  2920     by (metis distrib_right mult_eq_0_iff zero_neq_numeral)
  2921   ultimately show ?thesis
  2922     apply (simp add: cos_exp_eq Arccos_def Arccos_body_lemma exp_minus field_simps)
  2923     apply (simp add: power2_eq_square [symmetric])
  2924     done
  2925 qed
  2926 
  2927 lemma Arccos_cos:
  2928     assumes "0 < Re z & Re z < pi \<or>
  2929              Re z = 0 & 0 \<le> Im z \<or>
  2930              Re z = pi & Im z \<le> 0"
  2931       shows "Arccos(cos z) = z"
  2932 proof -
  2933   have *: "((\<i> - (exp (\<i> * z))\<^sup>2 * \<i>) / (2 * exp (\<i> * z))) = sin z"
  2934     by (simp add: sin_exp_eq exp_minus field_simps power2_eq_square)
  2935   have "1 - (exp (\<i> * z) + inverse (exp (\<i> * z)))\<^sup>2 / 4 = ((\<i> - (exp (\<i> * z))\<^sup>2 * \<i>) / (2 * exp (\<i> * z)))\<^sup>2"
  2936     by (simp add: field_simps power2_eq_square)
  2937   then have "Arccos(cos z) = - (\<i> * Ln ((exp (\<i> * z) + inverse (exp (\<i> * z))) / 2 +
  2938                            \<i> * csqrt (((\<i> - (exp (\<i> * z))\<^sup>2 * \<i>) / (2 * exp (\<i> * z)))\<^sup>2)))"
  2939     by (simp add: cos_exp_eq Arccos_def exp_minus power_divide)
  2940   also have "... = - (\<i> * Ln ((exp (\<i> * z) + inverse (exp (\<i> * z))) / 2 +
  2941                               \<i> * ((\<i> - (exp (\<i> * z))\<^sup>2 * \<i>) / (2 * exp (\<i> * z)))))"
  2942     apply (subst csqrt_square)
  2943     using assms Re_sin_pos [of z] Im_sin_nonneg [of z] Im_sin_nonneg2 [of z]
  2944     apply (auto simp: * Re_sin Im_sin)
  2945     done
  2946   also have "... =  - (\<i> * Ln (exp (\<i>*z)))"
  2947     by (simp add: field_simps power2_eq_square)
  2948   also have "... = z"
  2949     using assms
  2950     apply (subst Complex_Transcendental.Ln_exp, auto)
  2951     done
  2952   finally show ?thesis .
  2953 qed
  2954 
  2955 lemma Arccos_unique:
  2956     "\<lbrakk>cos z = w;
  2957       0 < Re z \<and> Re z < pi \<or>
  2958       Re z = 0 \<and> 0 \<le> Im z \<or>
  2959       Re z = pi \<and> Im z \<le> 0\<rbrakk> \<Longrightarrow> Arccos w = z"
  2960   using Arccos_cos by blast
  2961 
  2962 lemma Arccos_0 [simp]: "Arccos 0 = pi/2"
  2963   by (rule Arccos_unique) (auto simp: of_real_numeral)
  2964 
  2965 lemma Arccos_1 [simp]: "Arccos 1 = 0"
  2966   by (rule Arccos_unique) auto
  2967 
  2968 lemma Arccos_minus1: "Arccos(-1) = pi"
  2969   by (rule Arccos_unique) auto
  2970 
  2971 lemma has_field_derivative_Arccos:
  2972   assumes "(Im z = 0 \<Longrightarrow> \<bar>Re z\<bar> < 1)"
  2973     shows "(Arccos has_field_derivative - inverse(sin(Arccos z))) (at z)"
  2974 proof -
  2975   have "(cos (Arccos z))\<^sup>2 \<noteq> 1"
  2976     using assms
  2977     apply atomize
  2978     apply (auto simp: complex_eq_iff Re_power2 Im_power2 abs_square_eq_1)
  2979     apply (metis abs_minus_cancel abs_one abs_power2 numeral_One numeral_neq_neg_one)
  2980     apply (metis left_minus less_irrefl power_one sum_power2_gt_zero_iff zero_neq_neg_one)
  2981     done
  2982   then have "- sin (Arccos z) \<noteq> 0"
  2983     by (metis cos_squared_eq diff_0_right mult_zero_left neg_0_equal_iff_equal power2_eq_square)
  2984   then have "(Arccos has_field_derivative inverse(- sin(Arccos z))) (at z)"
  2985     apply (rule has_complex_derivative_inverse_basic [OF DERIV_cos _ _ open_ball [of z 1]])
  2986     apply (auto intro: isCont_Arccos assms)
  2987     done
  2988   then show ?thesis
  2989     by simp
  2990 qed
  2991 
  2992 declare has_field_derivative_Arcsin [derivative_intros]
  2993 declare has_field_derivative_Arcsin [THEN DERIV_chain2, derivative_intros]
  2994 
  2995 lemma field_differentiable_at_Arccos:
  2996     "(Im z = 0 \<Longrightarrow> \<bar>Re z\<bar> < 1) \<Longrightarrow> Arccos field_differentiable at z"
  2997   using field_differentiable_def has_field_derivative_Arccos by blast
  2998 
  2999 lemma field_differentiable_within_Arccos:
  3000     "(Im z = 0 \<Longrightarrow> \<bar>Re z\<bar> < 1) \<Longrightarrow> Arccos field_differentiable (at z within s)"
  3001   using field_differentiable_at_Arccos field_differentiable_within_subset by blast
  3002 
  3003 lemma continuous_within_Arccos:
  3004     "(Im z = 0 \<Longrightarrow> \<bar>Re z\<bar> < 1) \<Longrightarrow> continuous (at z within s) Arccos"
  3005   using continuous_at_imp_continuous_within isCont_Arccos by blast
  3006 
  3007 lemma continuous_on_Arccos [continuous_intros]:
  3008     "(\<And>z. z \<in> s \<Longrightarrow> Im z = 0 \<Longrightarrow> \<bar>Re z\<bar> < 1) \<Longrightarrow> continuous_on s Arccos"
  3009   by (simp add: continuous_at_imp_continuous_on)
  3010 
  3011 lemma holomorphic_on_Arccos: "(\<And>z. z \<in> s \<Longrightarrow> Im z = 0 \<Longrightarrow> \<bar>Re z\<bar> < 1) \<Longrightarrow> Arccos holomorphic_on s"
  3012   by (simp add: field_differentiable_within_Arccos holomorphic_on_def)
  3013 
  3014 
  3015 subsection\<open>Upper and Lower Bounds for Inverse Sine and Cosine\<close>
  3016 
  3017 lemma Arcsin_bounds: "\<bar>Re z\<bar> < 1 \<Longrightarrow> \<bar>Re(Arcsin z)\<bar> < pi/2"
  3018   unfolding Re_Arcsin
  3019   by (blast intro: Re_Ln_pos_lt_imp Arcsin_range_lemma)
  3020 
  3021 lemma Arccos_bounds: "\<bar>Re z\<bar> < 1 \<Longrightarrow> 0 < Re(Arccos z) \<and> Re(Arccos z) < pi"
  3022   unfolding Re_Arccos
  3023   by (blast intro!: Im_Ln_pos_lt_imp Arccos_range_lemma)
  3024 
  3025 lemma Re_Arccos_bounds: "-pi < Re(Arccos z) \<and> Re(Arccos z) \<le> pi"
  3026   unfolding Re_Arccos
  3027   by (blast intro!: mpi_less_Im_Ln Im_Ln_le_pi Arccos_body_lemma)
  3028 
  3029 lemma Re_Arccos_bound: "\<bar>Re(Arccos z)\<bar> \<le> pi"
  3030   by (meson Re_Arccos_bounds abs_le_iff less_eq_real_def minus_less_iff)
  3031 
  3032 lemma Im_Arccos_bound: "\<bar>Im (Arccos w)\<bar> \<le> cmod w"
  3033 proof -
  3034   have "(Im (Arccos w))\<^sup>2 \<le> (cmod (cos (Arccos w)))\<^sup>2 - (cos (Re (Arccos w)))\<^sup>2"
  3035     using norm_cos_squared [of "Arccos w"] real_le_abs_sinh [of "Im (Arccos w)"]
  3036     apply (simp only: abs_le_square_iff)
  3037     apply (simp add: divide_simps)
  3038     done
  3039   also have "... \<le> (cmod w)\<^sup>2"
  3040     by (auto simp: cmod_power2)
  3041   finally show ?thesis
  3042     using abs_le_square_iff by force
  3043 qed
  3044 
  3045 lemma Re_Arcsin_bounds: "-pi < Re(Arcsin z) & Re(Arcsin z) \<le> pi"
  3046   unfolding Re_Arcsin
  3047   by (blast intro!: mpi_less_Im_Ln Im_Ln_le_pi Arcsin_body_lemma)
  3048 
  3049 lemma Re_Arcsin_bound: "\<bar>Re(Arcsin z)\<bar> \<le> pi"
  3050   by (meson Re_Arcsin_bounds abs_le_iff less_eq_real_def minus_less_iff)
  3051 
  3052 lemma norm_Arccos_bounded:
  3053   fixes w :: complex
  3054   shows "norm (Arccos w) \<le> pi + norm w"
  3055 proof -
  3056   have Re: "(Re (Arccos w))\<^sup>2 \<le> pi\<^sup>2" "(Im (Arccos w))\<^sup>2 \<le> (cmod w)\<^sup>2"
  3057     using Re_Arccos_bound [of w] Im_Arccos_bound [of w] abs_le_square_iff by force+
  3058   have "Arccos w \<bullet> Arccos w \<le> pi\<^sup>2 + (cmod w)\<^sup>2"
  3059     using Re by (simp add: dot_square_norm cmod_power2 [of "Arccos w"])
  3060   then have "cmod (Arccos w) \<le> pi + cmod (cos (Arccos w))"
  3061     apply (simp add: norm_le_square)
  3062     by (metis dot_square_norm norm_ge_zero norm_le_square pi_ge_zero triangle_lemma)
  3063   then show "cmod (Arccos w) \<le> pi + cmod w"
  3064     by auto
  3065 qed
  3066 
  3067 
  3068 subsection\<open>Interrelations between Arcsin and Arccos\<close>
  3069 
  3070 lemma cos_Arcsin_nonzero:
  3071   assumes "z\<^sup>2 \<noteq> 1" shows "cos(Arcsin z) \<noteq> 0"
  3072 proof -
  3073   have eq: "(\<i> * z * (csqrt (1 - z\<^sup>2)))\<^sup>2 = z\<^sup>2 * (z\<^sup>2 - 1)"
  3074     by (simp add: power_mult_distrib algebra_simps)
  3075   have "\<i> * z * (csqrt (1 - z\<^sup>2)) \<noteq> z\<^sup>2 - 1"
  3076   proof
  3077     assume "\<i> * z * (csqrt (1 - z\<^sup>2)) = z\<^sup>2 - 1"
  3078     then have "(\<i> * z * (csqrt (1 - z\<^sup>2)))\<^sup>2 = (z\<^sup>2 - 1)\<^sup>2"
  3079       by simp
  3080     then have "z\<^sup>2 * (z\<^sup>2 - 1) = (z\<^sup>2 - 1)*(z\<^sup>2 - 1)"
  3081       using eq power2_eq_square by auto
  3082     then show False
  3083       using assms by simp
  3084   qed
  3085   then have "1 + \<i> * z * (csqrt (1 - z * z)) \<noteq> z\<^sup>2"
  3086     by (metis add_minus_cancel power2_eq_square uminus_add_conv_diff)
  3087   then have "2*(1 + \<i> * z * (csqrt (1 - z * z))) \<noteq> 2*z\<^sup>2"  (*FIXME cancel_numeral_factor*)
  3088     by (metis mult_cancel_left zero_neq_numeral)
  3089   then have "(\<i> * z + csqrt (1 - z\<^sup>2))\<^sup>2 \<noteq> -1"
  3090     using assms
  3091     apply (auto simp: power2_sum)
  3092     apply (simp add: power2_eq_square algebra_simps)
  3093     done
  3094   then show ?thesis
  3095     apply (simp add: cos_exp_eq Arcsin_def exp_minus)
  3096     apply (simp add: divide_simps Arcsin_body_lemma)
  3097     apply (metis add.commute minus_unique power2_eq_square)
  3098     done
  3099 qed
  3100 
  3101 lemma sin_Arccos_nonzero:
  3102   assumes "z\<^sup>2 \<noteq> 1" shows "sin(Arccos z) \<noteq> 0"
  3103 proof -
  3104   have eq: "(\<i> * z * (csqrt (1 - z\<^sup>2)))\<^sup>2 = -(z\<^sup>2) * (1 - z\<^sup>2)"
  3105     by (simp add: power_mult_distrib algebra_simps)
  3106   have "\<i> * z * (csqrt (1 - z\<^sup>2)) \<noteq> 1 - z\<^sup>2"
  3107   proof
  3108     assume "\<i> * z * (csqrt (1 - z\<^sup>2)) = 1 - z\<^sup>2"
  3109     then have "(\<i> * z * (csqrt (1 - z\<^sup>2)))\<^sup>2 = (1 - z\<^sup>2)\<^sup>2"
  3110       by simp
  3111     then have "-(z\<^sup>2) * (1 - z\<^sup>2) = (1 - z\<^sup>2)*(1 - z\<^sup>2)"
  3112       using eq power2_eq_square by auto
  3113     then have "-(z\<^sup>2) = (1 - z\<^sup>2)"
  3114       using assms
  3115       by (metis add.commute add.right_neutral diff_add_cancel mult_right_cancel)
  3116     then show False
  3117       using assms by simp
  3118   qed
  3119   then have "z\<^sup>2 + \<i> * z * (csqrt (1 - z\<^sup>2)) \<noteq> 1"
  3120     by (simp add: algebra_simps)
  3121   then have "2*(z\<^sup>2 + \<i> * z * (csqrt (1 - z\<^sup>2))) \<noteq> 2*1"
  3122     by (metis mult_cancel_left2 zero_neq_numeral)  (*FIXME cancel_numeral_factor*)
  3123   then have "(z + \<i> * csqrt (1 - z\<^sup>2))\<^sup>2 \<noteq> 1"
  3124     using assms
  3125     apply (auto simp: Power.comm_semiring_1_class.power2_sum power_mult_distrib)
  3126     apply (simp add: power2_eq_square algebra_simps)
  3127     done
  3128   then show ?thesis
  3129     apply (simp add: sin_exp_eq Arccos_def exp_minus)
  3130     apply (simp add: divide_simps Arccos_body_lemma)
  3131     apply (simp add: power2_eq_square)
  3132     done
  3133 qed
  3134 
  3135 lemma cos_sin_csqrt:
  3136   assumes "0 < cos(Re z)  \<or>  cos(Re z) = 0 \<and> Im z * sin(Re z) \<le> 0"
  3137     shows "cos z = csqrt(1 - (sin z)\<^sup>2)"
  3138   apply (rule csqrt_unique [THEN sym])
  3139   apply (simp add: cos_squared_eq)
  3140   using assms
  3141   apply (auto simp: Re_cos Im_cos add_pos_pos mult_le_0_iff zero_le_mult_iff)
  3142   done
  3143 
  3144 lemma sin_cos_csqrt:
  3145   assumes "0 < sin(Re z)  \<or>  sin(Re z) = 0 \<and> 0 \<le> Im z * cos(Re z)"
  3146     shows "sin z = csqrt(1 - (cos z)\<^sup>2)"
  3147   apply (rule csqrt_unique [THEN sym])
  3148   apply (simp add: sin_squared_eq)
  3149   using assms
  3150   apply (auto simp: Re_sin Im_sin add_pos_pos mult_le_0_iff zero_le_mult_iff)
  3151   done
  3152 
  3153 lemma Arcsin_Arccos_csqrt_pos:
  3154     "(0 < Re z | Re z = 0 & 0 \<le> Im z) \<Longrightarrow> Arcsin z = Arccos(csqrt(1 - z\<^sup>2))"
  3155   by (simp add: Arcsin_def Arccos_def Complex.csqrt_square add.commute)
  3156 
  3157 lemma Arccos_Arcsin_csqrt_pos:
  3158     "(0 < Re z | Re z = 0 & 0 \<le> Im z) \<Longrightarrow> Arccos z = Arcsin(csqrt(1 - z\<^sup>2))"
  3159   by (simp add: Arcsin_def Arccos_def Complex.csqrt_square add.commute)
  3160 
  3161 lemma sin_Arccos:
  3162     "0 < Re z | Re z = 0 & 0 \<le> Im z \<Longrightarrow> sin(Arccos z) = csqrt(1 - z\<^sup>2)"
  3163   by (simp add: Arccos_Arcsin_csqrt_pos)
  3164 
  3165 lemma cos_Arcsin:
  3166     "0 < Re z | Re z = 0 & 0 \<le> Im z \<Longrightarrow> cos(Arcsin z) = csqrt(1 - z\<^sup>2)"
  3167   by (simp add: Arcsin_Arccos_csqrt_pos)
  3168 
  3169 
  3170 subsection\<open>Relationship with Arcsin on the Real Numbers\<close>
  3171 
  3172 lemma Im_Arcsin_of_real:
  3173   assumes "\<bar>x\<bar> \<le> 1"
  3174     shows "Im (Arcsin (of_real x)) = 0"
  3175 proof -
  3176   have "csqrt (1 - (of_real x)\<^sup>2) = (if x^2 \<le> 1 then sqrt (1 - x^2) else \<i> * sqrt (x^2 - 1))"
  3177     by (simp add: of_real_sqrt del: csqrt_of_real_nonneg)
  3178   then have "cmod (\<i> * of_real x + csqrt (1 - (of_real x)\<^sup>2))^2 = 1"
  3179     using assms abs_square_le_1
  3180     by (force simp add: Complex.cmod_power2)
  3181   then have "cmod (\<i> * of_real x + csqrt (1 - (of_real x)\<^sup>2)) = 1"
  3182     by (simp add: norm_complex_def)
  3183   then show ?thesis
  3184     by (simp add: Im_Arcsin exp_minus)
  3185 qed
  3186 
  3187 corollary Arcsin_in_Reals [simp]: "z \<in> \<real> \<Longrightarrow> \<bar>Re z\<bar> \<le> 1 \<Longrightarrow> Arcsin z \<in> \<real>"
  3188   by (metis Im_Arcsin_of_real Re_complex_of_real Reals_cases complex_is_Real_iff)
  3189 
  3190 lemma arcsin_eq_Re_Arcsin:
  3191   assumes "\<bar>x\<bar> \<le> 1"
  3192     shows "arcsin x = Re (Arcsin (of_real x))"
  3193 unfolding arcsin_def
  3194 proof (rule the_equality, safe)
  3195   show "- (pi / 2) \<le> Re (Arcsin (complex_of_real x))"
  3196     using Re_Ln_pos_le [OF Arcsin_body_lemma, of "of_real x"]
  3197     by (auto simp: Complex.in_Reals_norm Re_Arcsin)
  3198 next
  3199   show "Re (Arcsin (complex_of_real x)) \<le> pi / 2"
  3200     using Re_Ln_pos_le [OF Arcsin_body_lemma, of "of_real x"]
  3201     by (auto simp: Complex.in_Reals_norm Re_Arcsin)
  3202 next
  3203   show "sin (Re (Arcsin (complex_of_real x))) = x"
  3204     using Re_sin [of "Arcsin (of_real x)"] Arcsin_body_lemma [of "of_real x"]
  3205     by (simp add: Im_Arcsin_of_real assms)
  3206 next
  3207   fix x'
  3208   assume "- (pi / 2) \<le> x'" "x' \<le> pi / 2" "x = sin x'"
  3209   then show "x' = Re (Arcsin (complex_of_real (sin x')))"
  3210     apply (simp add: sin_of_real [symmetric])
  3211     apply (subst Arcsin_sin)
  3212     apply (auto simp: )
  3213     done
  3214 qed
  3215 
  3216 lemma of_real_arcsin: "\<bar>x\<bar> \<le> 1 \<Longrightarrow> of_real(arcsin x) = Arcsin(of_real x)"
  3217   by (metis Im_Arcsin_of_real add.right_neutral arcsin_eq_Re_Arcsin complex_eq mult_zero_right of_real_0)
  3218 
  3219 
  3220 subsection\<open>Relationship with Arccos on the Real Numbers\<close>
  3221 
  3222 lemma Im_Arccos_of_real:
  3223   assumes "\<bar>x\<bar> \<le> 1"
  3224     shows "Im (Arccos (of_real x)) = 0"
  3225 proof -
  3226   have "csqrt (1 - (of_real x)\<^sup>2) = (if x^2 \<le> 1 then sqrt (1 - x^2) else \<i> * sqrt (x^2 - 1))"
  3227     by (simp add: of_real_sqrt del: csqrt_of_real_nonneg)
  3228   then have "cmod (of_real x + \<i> * csqrt (1 - (of_real x)\<^sup>2))^2 = 1"
  3229     using assms abs_square_le_1
  3230     by (force simp add: Complex.cmod_power2)
  3231   then have "cmod (of_real x + \<i> * csqrt (1 - (of_real x)\<^sup>2)) = 1"
  3232     by (simp add: norm_complex_def)
  3233   then show ?thesis
  3234     by (simp add: Im_Arccos exp_minus)
  3235 qed
  3236 
  3237 corollary Arccos_in_Reals [simp]: "z \<in> \<real> \<Longrightarrow> \<bar>Re z\<bar> \<le> 1 \<Longrightarrow> Arccos z \<in> \<real>"
  3238   by (metis Im_Arccos_of_real Re_complex_of_real Reals_cases complex_is_Real_iff)
  3239 
  3240 lemma arccos_eq_Re_Arccos:
  3241   assumes "\<bar>x\<bar> \<le> 1"
  3242     shows "arccos x = Re (Arccos (of_real x))"
  3243 unfolding arccos_def
  3244 proof (rule the_equality, safe)
  3245   show "0 \<le> Re (Arccos (complex_of_real x))"
  3246     using Im_Ln_pos_le [OF Arccos_body_lemma, of "of_real x"]
  3247     by (auto simp: Complex.in_Reals_norm Re_Arccos)
  3248 next
  3249   show "Re (Arccos (complex_of_real x)) \<le> pi"
  3250     using Im_Ln_pos_le [OF Arccos_body_lemma, of "of_real x"]
  3251     by (auto simp: Complex.in_Reals_norm Re_Arccos)
  3252 next
  3253   show "cos (Re (Arccos (complex_of_real x))) = x"
  3254     using Re_cos [of "Arccos (of_real x)"] Arccos_body_lemma [of "of_real x"]
  3255     by (simp add: Im_Arccos_of_real assms)
  3256 next
  3257   fix x'
  3258   assume "0 \<le> x'" "x' \<le> pi" "x = cos x'"
  3259   then show "x' = Re (Arccos (complex_of_real (cos x')))"
  3260     apply (simp add: cos_of_real [symmetric])
  3261     apply (subst Arccos_cos)
  3262     apply (auto simp: )
  3263     done
  3264 qed
  3265 
  3266 lemma of_real_arccos: "\<bar>x\<bar> \<le> 1 \<Longrightarrow> of_real(arccos x) = Arccos(of_real x)"
  3267   by (metis Im_Arccos_of_real add.right_neutral arccos_eq_Re_Arccos complex_eq mult_zero_right of_real_0)
  3268 
  3269 subsection\<open>Some interrelationships among the real inverse trig functions.\<close>
  3270 
  3271 lemma arccos_arctan:
  3272   assumes "-1 < x" "x < 1"
  3273     shows "arccos x = pi/2 - arctan(x / sqrt(1 - x\<^sup>2))"
  3274 proof -
  3275   have "arctan(x / sqrt(1 - x\<^sup>2)) - (pi/2 - arccos x) = 0"
  3276   proof (rule sin_eq_0_pi)
  3277     show "- pi < arctan (x / sqrt (1 - x\<^sup>2)) - (pi / 2 - arccos x)"
  3278       using arctan_lbound [of "x / sqrt(1 - x\<^sup>2)"]  arccos_bounded [of x] assms
  3279       by (simp add: algebra_simps)
  3280   next
  3281     show "arctan (x / sqrt (1 - x\<^sup>2)) - (pi / 2 - arccos x) < pi"
  3282       using arctan_ubound [of "x / sqrt(1 - x\<^sup>2)"]  arccos_bounded [of x] assms
  3283       by (simp add: algebra_simps)
  3284   next
  3285     show "sin (arctan (x / sqrt (1 - x\<^sup>2)) - (pi / 2 - arccos x)) = 0"
  3286       using assms
  3287       by (simp add: algebra_simps sin_diff cos_add sin_arccos sin_arctan cos_arctan
  3288                     power2_eq_square square_eq_1_iff)
  3289   qed
  3290   then show ?thesis
  3291     by simp
  3292 qed
  3293 
  3294 lemma arcsin_plus_arccos:
  3295   assumes "-1 \<le> x" "x \<le> 1"
  3296     shows "arcsin x + arccos x = pi/2"
  3297 proof -
  3298   have "arcsin x = pi/2 - arccos x"
  3299     apply (rule sin_inj_pi)
  3300     using assms arcsin [OF assms] arccos [OF assms]
  3301     apply (auto simp: algebra_simps sin_diff)
  3302     done
  3303   then show ?thesis
  3304     by (simp add: algebra_simps)
  3305 qed
  3306 
  3307 lemma arcsin_arccos_eq: "-1 \<le> x \<Longrightarrow> x \<le> 1 \<Longrightarrow> arcsin x = pi/2 - arccos x"
  3308   using arcsin_plus_arccos by force
  3309 
  3310 lemma arccos_arcsin_eq: "-1 \<le> x \<Longrightarrow> x \<le> 1 \<Longrightarrow> arccos x = pi/2 - arcsin x"
  3311   using arcsin_plus_arccos by force
  3312 
  3313 lemma arcsin_arctan: "-1 < x \<Longrightarrow> x < 1 \<Longrightarrow> arcsin x = arctan(x / sqrt(1 - x\<^sup>2))"
  3314   by (simp add: arccos_arctan arcsin_arccos_eq)
  3315 
  3316 lemma csqrt_1_diff_eq: "csqrt (1 - (of_real x)\<^sup>2) = (if x^2 \<le> 1 then sqrt (1 - x^2) else \<i> * sqrt (x^2 - 1))"
  3317   by ( simp add: of_real_sqrt del: csqrt_of_real_nonneg)
  3318 
  3319 lemma arcsin_arccos_sqrt_pos: "0 \<le> x \<Longrightarrow> x \<le> 1 \<Longrightarrow> arcsin x = arccos(sqrt(1 - x\<^sup>2))"
  3320   apply (simp add: abs_square_le_1 arcsin_eq_Re_Arcsin arccos_eq_Re_Arccos)
  3321   apply (subst Arcsin_Arccos_csqrt_pos)
  3322   apply (auto simp: power_le_one csqrt_1_diff_eq)
  3323   done
  3324 
  3325 lemma arcsin_arccos_sqrt_neg: "-1 \<le> x \<Longrightarrow> x \<le> 0 \<Longrightarrow> arcsin x = -arccos(sqrt(1 - x\<^sup>2))"
  3326   using arcsin_arccos_sqrt_pos [of "-x"]
  3327   by (simp add: arcsin_minus)
  3328 
  3329 lemma arccos_arcsin_sqrt_pos: "0 \<le> x \<Longrightarrow> x \<le> 1 \<Longrightarrow> arccos x = arcsin(sqrt(1 - x\<^sup>2))"
  3330   apply (simp add: abs_square_le_1 arcsin_eq_Re_Arcsin arccos_eq_Re_Arccos)
  3331   apply (subst Arccos_Arcsin_csqrt_pos)
  3332   apply (auto simp: power_le_one csqrt_1_diff_eq)
  3333   done
  3334 
  3335 lemma arccos_arcsin_sqrt_neg: "-1 \<le> x \<Longrightarrow> x \<le> 0 \<Longrightarrow> arccos x = pi - arcsin(sqrt(1 - x\<^sup>2))"
  3336   using arccos_arcsin_sqrt_pos [of "-x"]
  3337   by (simp add: arccos_minus)
  3338 
  3339 subsection\<open>continuity results for arcsin and arccos.\<close>
  3340 
  3341 lemma continuous_on_Arcsin_real [continuous_intros]:
  3342     "continuous_on {w \<in> \<real>. \<bar>Re w\<bar> \<le> 1} Arcsin"
  3343 proof -
  3344   have "continuous_on {w \<in> \<real>. \<bar>Re w\<bar> \<le> 1} (\<lambda>x. complex_of_real (arcsin (Re x))) =
  3345         continuous_on {w \<in> \<real>. \<bar>Re w\<bar> \<le> 1} (\<lambda>x. complex_of_real (Re (Arcsin (of_real (Re x)))))"
  3346     by (rule continuous_on_cong [OF refl]) (simp add: arcsin_eq_Re_Arcsin)
  3347   also have "... = ?thesis"
  3348     by (rule continuous_on_cong [OF refl]) simp
  3349   finally show ?thesis
  3350     using continuous_on_arcsin [OF continuous_on_Re [OF continuous_on_id], of "{w \<in> \<real>. \<bar>Re w\<bar> \<le> 1}"]
  3351           continuous_on_of_real
  3352     by fastforce
  3353 qed
  3354 
  3355 lemma continuous_within_Arcsin_real:
  3356     "continuous (at z within {w \<in> \<real>. \<bar>Re w\<bar> \<le> 1}) Arcsin"
  3357 proof (cases "z \<in> {w \<in> \<real>. \<bar>Re w\<bar> \<le> 1}")
  3358   case True then show ?thesis
  3359     using continuous_on_Arcsin_real continuous_on_eq_continuous_within
  3360     by blast
  3361 next
  3362   case False
  3363   with closed_real_abs_le [of 1] show ?thesis
  3364     by (rule continuous_within_closed_nontrivial)
  3365 qed
  3366 
  3367 lemma continuous_on_Arccos_real:
  3368     "continuous_on {w \<in> \<real>. \<bar>Re w\<bar> \<le> 1} Arccos"
  3369 proof -
  3370   have "continuous_on {w \<in> \<real>. \<bar>Re w\<bar> \<le> 1} (\<lambda>x. complex_of_real (arccos (Re x))) =
  3371         continuous_on {w \<in> \<real>. \<bar>Re w\<bar> \<le> 1} (\<lambda>x. complex_of_real (Re (Arccos (of_real (Re x)))))"
  3372     by (rule continuous_on_cong [OF refl]) (simp add: arccos_eq_Re_Arccos)
  3373   also have "... = ?thesis"
  3374     by (rule continuous_on_cong [OF refl]) simp
  3375   finally show ?thesis
  3376     using continuous_on_arccos [OF continuous_on_Re [OF continuous_on_id], of "{w \<in> \<real>. \<bar>Re w\<bar> \<le> 1}"]
  3377           continuous_on_of_real
  3378     by fastforce
  3379 qed
  3380 
  3381 lemma continuous_within_Arccos_real:
  3382     "continuous (at z within {w \<in> \<real>. \<bar>Re w\<bar> \<le> 1}) Arccos"
  3383 proof (cases "z \<in> {w \<in> \<real>. \<bar>Re w\<bar> \<le> 1}")
  3384   case True then show ?thesis
  3385     using continuous_on_Arccos_real continuous_on_eq_continuous_within
  3386     by blast
  3387 next
  3388   case False
  3389   with closed_real_abs_le [of 1] show ?thesis
  3390     by (rule continuous_within_closed_nontrivial)
  3391 qed
  3392 
  3393 
  3394 subsection\<open>Roots of unity\<close>
  3395 
  3396 lemma complex_root_unity:
  3397   fixes j::nat
  3398   assumes "n \<noteq> 0"
  3399     shows "exp(2 * of_real pi * \<i> * of_nat j / of_nat n)^n = 1"
  3400 proof -
  3401   have *: "of_nat j * (complex_of_real pi * 2) = complex_of_real (2 * real j * pi)"
  3402     by (simp add: of_real_numeral)
  3403   then show ?thesis
  3404     apply (simp add: exp_of_nat_mult [symmetric] mult_ac exp_Euler)
  3405     apply (simp only: * cos_of_real sin_of_real)
  3406     apply (simp add: )
  3407     done
  3408 qed
  3409 
  3410 lemma complex_root_unity_eq:
  3411   fixes j::nat and k::nat
  3412   assumes "1 \<le> n"
  3413     shows "(exp(2 * of_real pi * \<i> * of_nat j / of_nat n) = exp(2 * of_real pi * \<i> * of_nat k / of_nat n)
  3414            \<longleftrightarrow> j mod n = k mod n)"
  3415 proof -
  3416     have "(\<exists>z::int. \<i> * (of_nat j * (of_real pi * 2)) =
  3417                \<i> * (of_nat k * (of_real pi * 2)) + \<i> * (of_int z * (of_nat n * (of_real pi * 2)))) \<longleftrightarrow>
  3418           (\<exists>z::int. of_nat j * (\<i> * (of_real pi * 2)) =
  3419               (of_nat k + of_nat n * of_int z) * (\<i> * (of_real pi * 2)))"
  3420       by (simp add: algebra_simps)
  3421     also have "... \<longleftrightarrow> (\<exists>z::int. of_nat j = of_nat k + of_nat n * (of_int z :: complex))"
  3422       by simp
  3423     also have "... \<longleftrightarrow> (\<exists>z::int. of_nat j = of_nat k + of_nat n * z)"
  3424       apply (rule HOL.iff_exI)
  3425       apply (auto simp: )
  3426       using of_int_eq_iff apply fastforce
  3427       by (metis of_int_add of_int_mult of_int_of_nat_eq)
  3428     also have "... \<longleftrightarrow> int j mod int n = int k mod int n"
  3429       by (auto simp: mod_eq_dvd_iff dvd_def algebra_simps)
  3430     also have "... \<longleftrightarrow> j mod n = k mod n"
  3431       by (metis of_nat_eq_iff zmod_int)
  3432     finally have "(\<exists>z. \<i> * (of_nat j * (of_real pi * 2)) =
  3433              \<i> * (of_nat k * (of_real pi * 2)) + \<i> * (of_int z * (of_nat n * (of_real pi * 2)))) \<longleftrightarrow> j mod n = k mod n" .
  3434    note * = this
  3435   show ?thesis
  3436     using assms
  3437     by (simp add: exp_eq divide_simps mult_ac of_real_numeral *)
  3438 qed
  3439 
  3440 corollary bij_betw_roots_unity:
  3441     "bij_betw (\<lambda>j. exp(2 * of_real pi * \<i> * of_nat j / of_nat n))
  3442               {..<n}  {exp(2 * of_real pi * \<i> * of_nat j / of_nat n) | j. j < n}"
  3443   by (auto simp: bij_betw_def inj_on_def complex_root_unity_eq)
  3444 
  3445 lemma complex_root_unity_eq_1:
  3446   fixes j::nat and k::nat
  3447   assumes "1 \<le> n"
  3448     shows "exp(2 * of_real pi * \<i> * of_nat j / of_nat n) = 1 \<longleftrightarrow> n dvd j"
  3449 proof -
  3450   have "1 = exp(2 * of_real pi * \<i> * (of_nat n / of_nat n))"
  3451     using assms by simp
  3452   then have "exp(2 * of_real pi * \<i> * (of_nat j / of_nat n)) = 1 \<longleftrightarrow> j mod n = n mod n"
  3453      using complex_root_unity_eq [of n j n] assms
  3454      by simp
  3455   then show ?thesis
  3456     by auto
  3457 qed
  3458 
  3459 lemma finite_complex_roots_unity_explicit:
  3460      "finite {exp(2 * of_real pi * \<i> * of_nat j / of_nat n) | j::nat. j < n}"
  3461 by simp
  3462 
  3463 lemma card_complex_roots_unity_explicit:
  3464      "card {exp(2 * of_real pi * \<i> * of_nat j / of_nat n) | j::nat. j < n} = n"
  3465   by (simp add:  Finite_Set.bij_betw_same_card [OF bij_betw_roots_unity, symmetric])
  3466 
  3467 lemma complex_roots_unity:
  3468   assumes "1 \<le> n"
  3469     shows "{z::complex. z^n = 1} = {exp(2 * of_real pi * \<i> * of_nat j / of_nat n) | j::nat. j < n}"
  3470   apply (rule Finite_Set.card_seteq [symmetric])
  3471   using assms
  3472   apply (auto simp: card_complex_roots_unity_explicit finite_roots_unity complex_root_unity card_roots_unity)
  3473   done
  3474 
  3475 lemma card_complex_roots_unity: "1 \<le> n \<Longrightarrow> card {z::complex. z^n = 1} = n"
  3476   by (simp add: card_complex_roots_unity_explicit complex_roots_unity)
  3477 
  3478 lemma complex_not_root_unity:
  3479     "1 \<le> n \<Longrightarrow> \<exists>u::complex. norm u = 1 \<and> u^n \<noteq> 1"
  3480   apply (rule_tac x="exp (of_real pi * \<i> * of_real (1 / n))" in exI)
  3481   apply (auto simp: Re_complex_div_eq_0 exp_of_nat_mult [symmetric] mult_ac exp_Euler)
  3482   done
  3483 
  3484 subsection\<open> Formulation of loop homotopy in terms of maps out of type complex\<close>
  3485 
  3486 lemma homotopic_circlemaps_imp_homotopic_loops:
  3487   assumes "homotopic_with (\<lambda>h. True) (sphere 0 1) S f g"
  3488    shows "homotopic_loops S (f \<circ> exp \<circ> (\<lambda>t. 2 * of_real pi * of_real t * \<i>))
  3489                             (g \<circ> exp \<circ> (\<lambda>t. 2 * of_real pi * of_real t * \<i>))"
  3490 proof -
  3491   have "homotopic_with (\<lambda>f. True) {z. cmod z = 1} S f g"
  3492     using assms by (auto simp: sphere_def)
  3493   moreover have "continuous_on {0..1} (exp \<circ> (\<lambda>t. 2 * of_real pi * of_real t * \<i>))"
  3494      by (intro continuous_intros)
  3495   moreover have "(exp \<circ> (\<lambda>t. 2 * of_real pi * of_real t * \<i>)) ` {0..1} \<subseteq> {z. cmod z = 1}"
  3496     by (auto simp: norm_mult)
  3497   ultimately
  3498   show ?thesis
  3499     apply (simp add: homotopic_loops_def comp_assoc)
  3500     apply (rule homotopic_with_compose_continuous_right)
  3501       apply (auto simp: pathstart_def pathfinish_def)
  3502     done
  3503 qed
  3504 
  3505 lemma homotopic_loops_imp_homotopic_circlemaps:
  3506   assumes "homotopic_loops S p q"
  3507     shows "homotopic_with (\<lambda>h. True) (sphere 0 1) S
  3508                           (p \<circ> (\<lambda>z. (Arg z / (2 * pi))))
  3509                           (q \<circ> (\<lambda>z. (Arg z / (2 * pi))))"
  3510 proof -
  3511   obtain h where conth: "continuous_on ({0..1::real} \<times> {0..1}) h"
  3512              and him: "h ` ({0..1} \<times> {0..1}) \<subseteq> S"
  3513              and h0: "(\<forall>x. h (0, x) = p x)"
  3514              and h1: "(\<forall>x. h (1, x) = q x)"
  3515              and h01: "(\<forall>t\<in>{0..1}. h (t, 1) = h (t, 0)) "
  3516     using assms
  3517     by (auto simp: homotopic_loops_def sphere_def homotopic_with_def pathstart_def pathfinish_def)
  3518   define j where "j \<equiv> \<lambda>z. if 0 \<le> Im (snd z)
  3519                           then h (fst z, Arg (snd z) / (2 * pi))
  3520                           else h (fst z, 1 - Arg (cnj (snd z)) / (2 * pi))"
  3521   have Arg_eq: "1 - Arg (cnj y) / (2 * pi) = Arg y / (2 * pi) \<or> Arg y = 0 \<and> Arg (cnj y) = 0" if "cmod y = 1" for y
  3522     using that Arg_eq_0_pi Arg_eq_pi by (force simp: Arg_cnj divide_simps)
  3523   show ?thesis
  3524   proof (simp add: homotopic_with; intro conjI ballI exI)
  3525     show "continuous_on ({0..1} \<times> sphere 0 1) (\<lambda>w. h (fst w, Arg (snd w) / (2 * pi)))"
  3526     proof (rule continuous_on_eq)
  3527       show j: "j x = h (fst x, Arg (snd x) / (2 * pi))" if "x \<in> {0..1} \<times> sphere 0 1" for x
  3528         using Arg_eq that h01 by (force simp: j_def)
  3529       have eq:  "S = S \<inter> (UNIV \<times> {z. 0 \<le> Im z}) \<union> S \<inter> (UNIV \<times> {z. Im z \<le> 0})" for S :: "(real*complex)set"
  3530         by auto
  3531       have c1: "continuous_on ({0..1} \<times> sphere 0 1 \<inter> UNIV \<times> {z. 0 \<le> Im z}) (\<lambda>x. h (fst x, Arg (snd x) / (2 * pi)))"
  3532         apply (intro continuous_intros continuous_on_compose2 [OF conth]  continuous_on_compose2 [OF continuous_on_upperhalf_Arg])
  3533             apply (auto simp: Arg)
  3534         apply (meson Arg_lt_2pi linear not_le)
  3535         done
  3536       have c2: "continuous_on ({0..1} \<times> sphere 0 1 \<inter> UNIV \<times> {z. Im z \<le> 0}) (\<lambda>x. h (fst x, 1 - Arg (cnj (snd x)) / (2 * pi)))"
  3537         apply (intro continuous_intros continuous_on_compose2 [OF conth]  continuous_on_compose2 [OF continuous_on_upperhalf_Arg])
  3538             apply (auto simp: Arg)
  3539         apply (meson Arg_lt_2pi linear not_le)
  3540         done
  3541       show "continuous_on ({0..1} \<times> sphere 0 1) j"
  3542         apply (simp add: j_def)
  3543         apply (subst eq)
  3544         apply (rule continuous_on_cases_local)
  3545             apply (simp_all add: eq [symmetric] closedin_closed_Int closed_Times closed_halfspace_Im_le closed_halfspace_Im_ge c1 c2)
  3546         using Arg_eq h01
  3547         by force
  3548     qed
  3549     have "(\<lambda>w. h (fst w, Arg (snd w) / (2 * pi))) ` ({0..1} \<times> sphere 0 1) \<subseteq> h ` ({0..1} \<times> {0..1})"
  3550       by (auto simp: Arg_ge_0 Arg_lt_2pi less_imp_le)
  3551     also have "... \<subseteq> S"
  3552       using him by blast
  3553     finally show "(\<lambda>w. h (fst w, Arg (snd w) / (2 * pi))) ` ({0..1} \<times> sphere 0 1) \<subseteq> S" .
  3554   qed (auto simp: h0 h1)
  3555 qed
  3556 
  3557 lemma simply_connected_homotopic_loops:
  3558   "simply_connected S \<longleftrightarrow>
  3559        (\<forall>p q. homotopic_loops S p p \<and> homotopic_loops S q q \<longrightarrow> homotopic_loops S p q)"
  3560 unfolding simply_connected_def using homotopic_loops_refl by metis
  3561 
  3562 
  3563 lemma simply_connected_eq_homotopic_circlemaps1:
  3564   fixes f :: "complex \<Rightarrow> 'a::topological_space" and g :: "complex \<Rightarrow> 'a"
  3565   assumes S: "simply_connected S"
  3566       and contf: "continuous_on (sphere 0 1) f" and fim: "f ` (sphere 0 1) \<subseteq> S"
  3567       and contg: "continuous_on (sphere 0 1) g" and gim: "g ` (sphere 0 1) \<subseteq> S"
  3568     shows "homotopic_with (\<lambda>h. True) (sphere 0 1) S f g"
  3569 proof -
  3570   have "homotopic_loops S (f \<circ> exp \<circ> (\<lambda>t. of_real(2 * pi * t) * \<i>)) (g \<circ> exp \<circ> (\<lambda>t. of_real(2 * pi *  t) * \<i>))"
  3571     apply (rule S [unfolded simply_connected_homotopic_loops, rule_format])
  3572     apply (simp add: homotopic_circlemaps_imp_homotopic_loops homotopic_with_refl contf fim contg gim)
  3573     done
  3574   then show ?thesis
  3575     apply (rule homotopic_with_eq [OF homotopic_loops_imp_homotopic_circlemaps])
  3576       apply (auto simp: o_def complex_norm_eq_1_exp mult.commute)
  3577     done
  3578 qed
  3579 
  3580 lemma simply_connected_eq_homotopic_circlemaps2a:
  3581   fixes h :: "complex \<Rightarrow> 'a::topological_space"
  3582   assumes conth: "continuous_on (sphere 0 1) h" and him: "h ` (sphere 0 1) \<subseteq> S"
  3583       and hom: "\<And>f g::complex \<Rightarrow> 'a.
  3584                 \<lbrakk>continuous_on (sphere 0 1) f; f ` (sphere 0 1) \<subseteq> S;
  3585                 continuous_on (sphere 0 1) g; g ` (sphere 0 1) \<subseteq> S\<rbrakk>
  3586                 \<Longrightarrow> homotopic_with (\<lambda>h. True) (sphere 0 1) S f g"
  3587             shows "\<exists>a. homotopic_with (\<lambda>h. True) (sphere 0 1) S h (\<lambda>x. a)"
  3588     apply (rule_tac x="h 1" in exI)
  3589     apply (rule hom)
  3590     using assms
  3591     by (auto simp: continuous_on_const)
  3592 
  3593 lemma simply_connected_eq_homotopic_circlemaps2b:
  3594   fixes S :: "'a::real_normed_vector set"
  3595   assumes "\<And>f g::complex \<Rightarrow> 'a.
  3596                 \<lbrakk>continuous_on (sphere 0 1) f; f ` (sphere 0 1) \<subseteq> S;
  3597                 continuous_on (sphere 0 1) g; g ` (sphere 0 1) \<subseteq> S\<rbrakk>
  3598                 \<Longrightarrow> homotopic_with (\<lambda>h. True) (sphere 0 1) S f g"
  3599   shows "path_connected S"
  3600 proof (clarsimp simp add: path_connected_eq_homotopic_points)
  3601   fix a b
  3602   assume "a \<in> S" "b \<in> S"
  3603   then show "homotopic_loops S (linepath a a) (linepath b b)"
  3604     using homotopic_circlemaps_imp_homotopic_loops [OF assms [of "\<lambda>x. a" "\<lambda>x. b"]]
  3605     by (auto simp: o_def continuous_on_const linepath_def)
  3606 qed
  3607 
  3608 lemma simply_connected_eq_homotopic_circlemaps3:
  3609   fixes h :: "complex \<Rightarrow> 'a::real_normed_vector"
  3610   assumes "path_connected S"
  3611       and hom: "\<And>f::complex \<Rightarrow> 'a.
  3612                   \<lbrakk>continuous_on (sphere 0 1) f; f `(sphere 0 1) \<subseteq> S\<rbrakk>
  3613                   \<Longrightarrow> \<exists>a. homotopic_with (\<lambda>h. True) (sphere 0 1) S f (\<lambda>x. a)"
  3614     shows "simply_connected S"
  3615 proof (clarsimp simp add: simply_connected_eq_contractible_loop_some assms)
  3616   fix p
  3617   assume p: "path p" "path_image p \<subseteq> S" "pathfinish p = pathstart p"
  3618   then have "homotopic_loops S p p"
  3619     by (simp add: homotopic_loops_refl)
  3620   then obtain a where homp: "homotopic_with (\<lambda>h. True) (sphere 0 1) S (p \<circ> (\<lambda>z. Arg z / (2 * pi))) (\<lambda>x. a)"
  3621     by (metis homotopic_with_imp_subset2 homotopic_loops_imp_homotopic_circlemaps homotopic_with_imp_continuous hom)
  3622   show "\<exists>a. a \<in> S \<and> homotopic_loops S p (linepath a a)"
  3623   proof (intro exI conjI)
  3624     show "a \<in> S"
  3625       using homotopic_with_imp_subset2 [OF homp]
  3626       by (metis dist_0_norm image_subset_iff mem_sphere norm_one)
  3627     have teq: "\<And>t. \<lbrakk>0 \<le> t; t \<le> 1\<rbrakk>
  3628                \<Longrightarrow> t = Arg (exp (2 * of_real pi * of_real t * \<i>)) / (2 * pi) \<or> t=1 \<and> Arg (exp (2 * of_real pi * of_real t * \<i>)) = 0"
  3629       apply (rule disjCI)
  3630       using Arg_of_real [of 1] apply (auto simp: Arg_exp)
  3631       done
  3632     have "homotopic_loops S p (p \<circ> (\<lambda>z. Arg z / (2 * pi)) \<circ> exp \<circ> (\<lambda>t. 2 * complex_of_real pi * complex_of_real t * \<i>))"
  3633       apply (rule homotopic_loops_eq [OF p])
  3634       using p teq apply (fastforce simp: pathfinish_def pathstart_def)
  3635       done
  3636     then
  3637     show "homotopic_loops S p (linepath a a)"
  3638       by (simp add: linepath_refl  homotopic_loops_trans [OF _ homotopic_circlemaps_imp_homotopic_loops [OF homp, simplified K_record_comp]])
  3639   qed
  3640 qed
  3641 
  3642 
  3643 proposition simply_connected_eq_homotopic_circlemaps:
  3644   fixes S :: "'a::real_normed_vector set"
  3645   shows "simply_connected S \<longleftrightarrow>
  3646          (\<forall>f g::complex \<Rightarrow> 'a.
  3647               continuous_on (sphere 0 1) f \<and> f ` (sphere 0 1) \<subseteq> S \<and>
  3648               continuous_on (sphere 0 1) g \<and> g ` (sphere 0 1) \<subseteq> S
  3649               \<longrightarrow> homotopic_with (\<lambda>h. True) (sphere 0 1) S f g)"
  3650   apply (rule iffI)
  3651    apply (blast elim: dest: simply_connected_eq_homotopic_circlemaps1)
  3652   by (simp add: simply_connected_eq_homotopic_circlemaps2a simply_connected_eq_homotopic_circlemaps2b simply_connected_eq_homotopic_circlemaps3)
  3653 
  3654 proposition simply_connected_eq_contractible_circlemap:
  3655   fixes S :: "'a::real_normed_vector set"
  3656   shows "simply_connected S \<longleftrightarrow>
  3657          path_connected S \<and>
  3658          (\<forall>f::complex \<Rightarrow> 'a.
  3659               continuous_on (sphere 0 1) f \<and> f `(sphere 0 1) \<subseteq> S
  3660               \<longrightarrow> (\<exists>a. homotopic_with (\<lambda>h. True) (sphere 0 1) S f (\<lambda>x. a)))"
  3661   apply (rule iffI)
  3662    apply (simp add: simply_connected_eq_homotopic_circlemaps1 simply_connected_eq_homotopic_circlemaps2a simply_connected_eq_homotopic_circlemaps2b)
  3663   using simply_connected_eq_homotopic_circlemaps3 by blast
  3664 
  3665 corollary homotopy_eqv_simple_connectedness:
  3666   fixes S :: "'a::real_normed_vector set" and T :: "'b::real_normed_vector set"
  3667   shows "S homotopy_eqv T \<Longrightarrow> simply_connected S \<longleftrightarrow> simply_connected T"
  3668   by (simp add: simply_connected_eq_homotopic_circlemaps homotopy_eqv_homotopic_triviality)
  3669 
  3670 
  3671 subsection\<open>Homeomorphism of simple closed curves to circles\<close>
  3672 
  3673 proposition homeomorphic_simple_path_image_circle:
  3674   fixes a :: complex and \<gamma> :: "real \<Rightarrow> 'a::t2_space"
  3675   assumes "simple_path \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>" and "0 < r"
  3676   shows "(path_image \<gamma>) homeomorphic sphere a r"
  3677 proof -
  3678   have "homotopic_loops (path_image \<gamma>) \<gamma> \<gamma>"
  3679     by (simp add: assms homotopic_loops_refl simple_path_imp_path)
  3680   then have hom: "homotopic_with (\<lambda>h. True) (sphere 0 1) (path_image \<gamma>)
  3681                (\<gamma> \<circ> (\<lambda>z. Arg z / (2*pi))) (\<gamma> \<circ> (\<lambda>z. Arg z / (2*pi)))"
  3682     by (rule homotopic_loops_imp_homotopic_circlemaps)
  3683   have "\<exists>g. homeomorphism (sphere 0 1) (path_image \<gamma>) (\<gamma> \<circ> (\<lambda>z. Arg z / (2*pi))) g"
  3684   proof (rule homeomorphism_compact)
  3685     show "continuous_on (sphere 0 1) (\<gamma> \<circ> (\<lambda>z. Arg z / (2*pi)))"
  3686       using hom homotopic_with_imp_continuous by blast
  3687     show "inj_on (\<gamma> \<circ> (\<lambda>z. Arg z / (2*pi))) (sphere 0 1)"
  3688     proof
  3689       fix x y
  3690       assume xy: "x \<in> sphere 0 1" "y \<in> sphere 0 1"
  3691          and eq: "(\<gamma> \<circ> (\<lambda>z. Arg z / (2*pi))) x = (\<gamma> \<circ> (\<lambda>z. Arg z / (2*pi))) y"
  3692       then have "(Arg x / (2*pi)) = (Arg y / (2*pi))"
  3693       proof -
  3694         have "(Arg x / (2*pi)) \<in> {0..1}" "(Arg y / (2*pi)) \<in> {0..1}"
  3695           using Arg_ge_0 Arg_lt_2pi dual_order.strict_iff_order by fastforce+
  3696         with eq show ?thesis
  3697           using \<open>simple_path \<gamma>\<close> Arg_lt_2pi unfolding simple_path_def o_def
  3698           by (metis eq_divide_eq_1 not_less_iff_gr_or_eq)
  3699       qed
  3700       with xy show "x = y"
  3701         by (metis Arg Arg_0 dist_0_norm divide_cancel_right dual_order.strict_iff_order mem_sphere)
  3702     qed
  3703     have "\<And>z. cmod z = 1 \<Longrightarrow> \<exists>x\<in>{0..1}. \<gamma> (Arg z / (2*pi)) = \<gamma> x"
  3704        by (metis Arg_ge_0 Arg_lt_2pi atLeastAtMost_iff divide_less_eq_1 less_eq_real_def zero_less_mult_iff pi_gt_zero zero_le_divide_iff zero_less_numeral)
  3705      moreover have "\<exists>z\<in>sphere 0 1. \<gamma> x = \<gamma> (Arg z / (2*pi))" if "0 \<le> x" "x \<le> 1" for x
  3706      proof (cases "x=1")
  3707        case True
  3708        then show ?thesis
  3709          apply (rule_tac x=1 in bexI)
  3710          apply (metis loop Arg_of_real divide_eq_0_iff of_real_1 pathfinish_def pathstart_def \<open>0 \<le> x\<close>, auto)
  3711          done
  3712      next
  3713        case False
  3714        then have *: "(Arg (exp (\<i>*(2* of_real pi* of_real x))) / (2*pi)) = x"
  3715          using that by (auto simp: Arg_exp divide_simps)
  3716        show ?thesis
  3717          by (rule_tac x="exp(\<i> * of_real(2*pi*x))" in bexI) (auto simp: *)
  3718     qed
  3719     ultimately show "(\<gamma> \<circ> (\<lambda>z. Arg z / (2*pi))) ` sphere 0 1 = path_image \<gamma>"
  3720       by (auto simp: path_image_def image_iff)
  3721     qed auto
  3722     then have "path_image \<gamma> homeomorphic sphere (0::complex) 1"
  3723       using homeomorphic_def homeomorphic_sym by blast
  3724   also have "... homeomorphic sphere a r"
  3725     by (simp add: assms homeomorphic_spheres)
  3726   finally show ?thesis .
  3727 qed
  3728 
  3729 lemma homeomorphic_simple_path_images:
  3730   fixes \<gamma>1 :: "real \<Rightarrow> 'a::t2_space" and \<gamma>2 :: "real \<Rightarrow> 'b::t2_space"
  3731   assumes "simple_path \<gamma>1" and loop: "pathfinish \<gamma>1 = pathstart \<gamma>1"
  3732   assumes "simple_path \<gamma>2" and loop: "pathfinish \<gamma>2 = pathstart \<gamma>2"
  3733   shows "(path_image \<gamma>1) homeomorphic (path_image \<gamma>2)"
  3734   by (meson assms homeomorphic_simple_path_image_circle homeomorphic_sym homeomorphic_trans loop pi_gt_zero)
  3735 
  3736 end