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