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