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