src/HOL/Multivariate_Analysis/Complex_Transcendental.thy
author wenzelm
Mon Aug 31 21:28:08 2015 +0200 (2015-08-31)
changeset 61070 b72a990adfe2
parent 60809 457abb82fb9e
child 61426 d53db136e8fd
permissions -rw-r--r--
prefer symbols;
     1 (*  Author: John Harrison
     2     Ported from "hol_light/Multivariate/transcendentals.ml" by L C Paulson (2015)
     3 *)
     4 
     5 section \<open>Complex Transcendental Functions\<close>
     6 
     7 theory Complex_Transcendental
     8 imports  "~~/src/HOL/Multivariate_Analysis/Complex_Analysis_Basics"
     9 begin
    10 
    11 lemma cmod_add_real_less:
    12   assumes "Im z \<noteq> 0" "r\<noteq>0"
    13     shows "cmod (z + r) < cmod z + abs r"
    14 proof (cases z)
    15   case (Complex x y)
    16   have "r * x / \<bar>r\<bar> < sqrt (x*x + y*y)"
    17     apply (rule real_less_rsqrt)
    18     using assms
    19     apply (simp add: Complex power2_eq_square)
    20     using not_real_square_gt_zero by blast
    21   then show ?thesis using assms Complex
    22     apply (auto simp: cmod_def)
    23     apply (rule power2_less_imp_less, auto)
    24     apply (simp add: power2_eq_square field_simps)
    25     done
    26 qed
    27 
    28 lemma cmod_diff_real_less: "Im z \<noteq> 0 \<Longrightarrow> x\<noteq>0 \<Longrightarrow> cmod (z - x) < cmod z + abs x"
    29   using cmod_add_real_less [of z "-x"]
    30   by simp
    31 
    32 lemma cmod_square_less_1_plus:
    33   assumes "Im z = 0 \<Longrightarrow> \<bar>Re z\<bar> < 1"
    34     shows "(cmod z)\<^sup>2 < 1 + cmod (1 - z\<^sup>2)"
    35   using assms
    36   apply (cases "Im z = 0 \<or> Re z = 0")
    37   using abs_square_less_1
    38     apply (force simp add: Re_power2 Im_power2 cmod_def)
    39   using cmod_diff_real_less [of "1 - z\<^sup>2" "1"]
    40   apply (simp add: norm_power Im_power2)
    41   done
    42 
    43 subsection\<open>The Exponential Function is Differentiable and Continuous\<close>
    44 
    45 lemma complex_differentiable_within_exp: "exp complex_differentiable (at z within s)"
    46   using DERIV_exp complex_differentiable_at_within complex_differentiable_def by blast
    47 
    48 lemma continuous_within_exp:
    49   fixes z::"'a::{real_normed_field,banach}"
    50   shows "continuous (at z within s) exp"
    51 by (simp add: continuous_at_imp_continuous_within)
    52 
    53 lemma continuous_on_exp:
    54   fixes s::"'a::{real_normed_field,banach} set"
    55   shows "continuous_on s exp"
    56 by (simp add: continuous_on_exp continuous_on_id)
    57 
    58 lemma holomorphic_on_exp: "exp holomorphic_on s"
    59   by (simp add: complex_differentiable_within_exp holomorphic_on_def)
    60 
    61 subsection\<open>Euler and de Moivre formulas.\<close>
    62 
    63 text\<open>The sine series times @{term i}\<close>
    64 lemma sin_ii_eq: "(\<lambda>n. (ii * sin_coeff n) * z^n) sums (ii * sin z)"
    65 proof -
    66   have "(\<lambda>n. ii * sin_coeff n *\<^sub>R z^n) sums (ii * sin z)"
    67     using sin_converges sums_mult by blast
    68   then show ?thesis
    69     by (simp add: scaleR_conv_of_real field_simps)
    70 qed
    71 
    72 theorem exp_Euler: "exp(ii * z) = cos(z) + ii * sin(z)"
    73 proof -
    74   have "(\<lambda>n. (cos_coeff n + ii * sin_coeff n) * z^n)
    75         = (\<lambda>n. (ii * z) ^ n /\<^sub>R (fact n))"
    76   proof
    77     fix n
    78     show "(cos_coeff n + ii * sin_coeff n) * z^n = (ii * z) ^ n /\<^sub>R (fact n)"
    79       by (auto simp: cos_coeff_def sin_coeff_def scaleR_conv_of_real field_simps elim!: evenE oddE)
    80   qed
    81   also have "... sums (exp (ii * z))"
    82     by (rule exp_converges)
    83   finally have "(\<lambda>n. (cos_coeff n + ii * sin_coeff n) * z^n) sums (exp (ii * z))" .
    84   moreover have "(\<lambda>n. (cos_coeff n + ii * sin_coeff n) * z^n) sums (cos z + ii * sin z)"
    85     using sums_add [OF cos_converges [of z] sin_ii_eq [of z]]
    86     by (simp add: field_simps scaleR_conv_of_real)
    87   ultimately show ?thesis
    88     using sums_unique2 by blast
    89 qed
    90 
    91 corollary exp_minus_Euler: "exp(-(ii * z)) = cos(z) - ii * sin(z)"
    92   using exp_Euler [of "-z"]
    93   by simp
    94 
    95 lemma sin_exp_eq: "sin z = (exp(ii * z) - exp(-(ii * z))) / (2*ii)"
    96   by (simp add: exp_Euler exp_minus_Euler)
    97 
    98 lemma sin_exp_eq': "sin z = ii * (exp(-(ii * z)) - exp(ii * z)) / 2"
    99   by (simp add: exp_Euler exp_minus_Euler)
   100 
   101 lemma cos_exp_eq:  "cos z = (exp(ii * z) + exp(-(ii * z))) / 2"
   102   by (simp add: exp_Euler exp_minus_Euler)
   103 
   104 subsection\<open>Relationships between real and complex trig functions\<close>
   105 
   106 lemma real_sin_eq [simp]:
   107   fixes x::real
   108   shows "Re(sin(of_real x)) = sin x"
   109   by (simp add: sin_of_real)
   110 
   111 lemma real_cos_eq [simp]:
   112   fixes x::real
   113   shows "Re(cos(of_real x)) = cos x"
   114   by (simp add: cos_of_real)
   115 
   116 lemma DeMoivre: "(cos z + ii * sin z) ^ n = cos(n * z) + ii * sin(n * z)"
   117   apply (simp add: exp_Euler [symmetric])
   118   by (metis exp_of_nat_mult mult.left_commute)
   119 
   120 lemma exp_cnj:
   121   fixes z::complex
   122   shows "cnj (exp z) = exp (cnj z)"
   123 proof -
   124   have "(\<lambda>n. cnj (z ^ n /\<^sub>R (fact n))) = (\<lambda>n. (cnj z)^n /\<^sub>R (fact n))"
   125     by auto
   126   also have "... sums (exp (cnj z))"
   127     by (rule exp_converges)
   128   finally have "(\<lambda>n. cnj (z ^ n /\<^sub>R (fact n))) sums (exp (cnj z))" .
   129   moreover have "(\<lambda>n. cnj (z ^ n /\<^sub>R (fact n))) sums (cnj (exp z))"
   130     by (metis exp_converges sums_cnj)
   131   ultimately show ?thesis
   132     using sums_unique2
   133     by blast
   134 qed
   135 
   136 lemma cnj_sin: "cnj(sin z) = sin(cnj z)"
   137   by (simp add: sin_exp_eq exp_cnj field_simps)
   138 
   139 lemma cnj_cos: "cnj(cos z) = cos(cnj z)"
   140   by (simp add: cos_exp_eq exp_cnj field_simps)
   141 
   142 lemma complex_differentiable_at_sin: "sin complex_differentiable at z"
   143   using DERIV_sin complex_differentiable_def by blast
   144 
   145 lemma complex_differentiable_within_sin: "sin complex_differentiable (at z within s)"
   146   by (simp add: complex_differentiable_at_sin complex_differentiable_at_within)
   147 
   148 lemma complex_differentiable_at_cos: "cos complex_differentiable at z"
   149   using DERIV_cos complex_differentiable_def by blast
   150 
   151 lemma complex_differentiable_within_cos: "cos complex_differentiable (at z within s)"
   152   by (simp add: complex_differentiable_at_cos complex_differentiable_at_within)
   153 
   154 lemma holomorphic_on_sin: "sin holomorphic_on s"
   155   by (simp add: complex_differentiable_within_sin holomorphic_on_def)
   156 
   157 lemma holomorphic_on_cos: "cos holomorphic_on s"
   158   by (simp add: complex_differentiable_within_cos holomorphic_on_def)
   159 
   160 subsection\<open>Get a nice real/imaginary separation in Euler's formula.\<close>
   161 
   162 lemma Euler: "exp(z) = of_real(exp(Re z)) *
   163               (of_real(cos(Im z)) + ii * of_real(sin(Im z)))"
   164 by (cases z) (simp add: exp_add exp_Euler cos_of_real exp_of_real sin_of_real)
   165 
   166 lemma Re_sin: "Re(sin z) = sin(Re z) * (exp(Im z) + exp(-(Im z))) / 2"
   167   by (simp add: sin_exp_eq field_simps Re_divide Im_exp)
   168 
   169 lemma Im_sin: "Im(sin z) = cos(Re z) * (exp(Im z) - exp(-(Im z))) / 2"
   170   by (simp add: sin_exp_eq field_simps Im_divide Re_exp)
   171 
   172 lemma Re_cos: "Re(cos z) = cos(Re z) * (exp(Im z) + exp(-(Im z))) / 2"
   173   by (simp add: cos_exp_eq field_simps Re_divide Re_exp)
   174 
   175 lemma Im_cos: "Im(cos z) = sin(Re z) * (exp(-(Im z)) - exp(Im z)) / 2"
   176   by (simp add: cos_exp_eq field_simps Im_divide Im_exp)
   177 
   178 lemma Re_sin_pos: "0 < Re z \<Longrightarrow> Re z < pi \<Longrightarrow> Re (sin z) > 0"
   179   by (auto simp: Re_sin Im_sin add_pos_pos sin_gt_zero)
   180 
   181 lemma Im_sin_nonneg: "Re z = 0 \<Longrightarrow> 0 \<le> Im z \<Longrightarrow> 0 \<le> Im (sin z)"
   182   by (simp add: Re_sin Im_sin algebra_simps)
   183 
   184 lemma Im_sin_nonneg2: "Re z = pi \<Longrightarrow> Im z \<le> 0 \<Longrightarrow> 0 \<le> Im (sin z)"
   185   by (simp add: Re_sin Im_sin algebra_simps)
   186 
   187 subsection\<open>More on the Polar Representation of Complex Numbers\<close>
   188 
   189 lemma exp_Complex: "exp(Complex r t) = of_real(exp r) * Complex (cos t) (sin t)"
   190   by (simp add: exp_add exp_Euler exp_of_real sin_of_real cos_of_real)
   191 
   192 lemma exp_eq_1: "exp z = 1 \<longleftrightarrow> Re(z) = 0 \<and> (\<exists>n::int. Im(z) = of_int (2 * n) * pi)"
   193 apply auto
   194 apply (metis exp_eq_one_iff norm_exp_eq_Re norm_one)
   195 apply (metis Re_exp cos_one_2pi_int mult.commute mult.left_neutral norm_exp_eq_Re norm_one one_complex.simps(1) real_of_int_def)
   196 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 real_of_int_def sin_zero_iff_int2)
   197 
   198 lemma exp_eq: "exp w = exp z \<longleftrightarrow> (\<exists>n::int. w = z + (of_int (2 * n) * pi) * ii)"
   199                 (is "?lhs = ?rhs")
   200 proof -
   201   have "exp w = exp z \<longleftrightarrow> exp (w-z) = 1"
   202     by (simp add: exp_diff)
   203   also have "... \<longleftrightarrow> (Re w = Re z \<and> (\<exists>n::int. Im w - Im z = of_int (2 * n) * pi))"
   204     by (simp add: exp_eq_1)
   205   also have "... \<longleftrightarrow> ?rhs"
   206     by (auto simp: algebra_simps intro!: complex_eqI)
   207   finally show ?thesis .
   208 qed
   209 
   210 lemma exp_complex_eqI: "abs(Im w - Im z) < 2*pi \<Longrightarrow> exp w = exp z \<Longrightarrow> w = z"
   211   by (auto simp: exp_eq abs_mult)
   212 
   213 lemma exp_integer_2pi:
   214   assumes "n \<in> \<int>"
   215   shows "exp((2 * n * pi) * ii) = 1"
   216 proof -
   217   have "exp((2 * n * pi) * ii) = exp 0"
   218     using assms
   219     by (simp only: Ints_def exp_eq) auto
   220   also have "... = 1"
   221     by simp
   222   finally show ?thesis .
   223 qed
   224 
   225 lemma sin_cos_eq_iff: "sin y = sin x \<and> cos y = cos x \<longleftrightarrow> (\<exists>n::int. y = x + 2 * n * pi)"
   226 proof -
   227   { assume "sin y = sin x" "cos y = cos x"
   228     then have "cos (y-x) = 1"
   229       using cos_add [of y "-x"] by simp
   230     then have "\<exists>n::int. y-x = real n * 2 * pi"
   231       using cos_one_2pi_int by blast }
   232   then show ?thesis
   233   apply (auto simp: sin_add cos_add)
   234   apply (metis add.commute diff_add_cancel mult.commute)
   235   done
   236 qed
   237 
   238 lemma exp_i_ne_1:
   239   assumes "0 < x" "x < 2*pi"
   240   shows "exp(\<i> * of_real x) \<noteq> 1"
   241 proof
   242   assume "exp (\<i> * of_real x) = 1"
   243   then have "exp (\<i> * of_real x) = exp 0"
   244     by simp
   245   then obtain n where "\<i> * of_real x = (of_int (2 * n) * pi) * \<i>"
   246     by (simp only: Ints_def exp_eq) auto
   247   then have  "of_real x = (of_int (2 * n) * pi)"
   248     by (metis complex_i_not_zero mult.commute mult_cancel_left of_real_eq_iff real_scaleR_def scaleR_conv_of_real)
   249   then have  "x = (of_int (2 * n) * pi)"
   250     by simp
   251   then show False using assms
   252     by (cases n) (auto simp: zero_less_mult_iff mult_less_0_iff)
   253 qed
   254 
   255 lemma sin_eq_0:
   256   fixes z::complex
   257   shows "sin z = 0 \<longleftrightarrow> (\<exists>n::int. z = of_real(n * pi))"
   258   by (simp add: sin_exp_eq exp_eq of_real_numeral)
   259 
   260 lemma cos_eq_0:
   261   fixes z::complex
   262   shows "cos z = 0 \<longleftrightarrow> (\<exists>n::int. z = of_real(n * pi) + of_real pi/2)"
   263   using sin_eq_0 [of "z - of_real pi/2"]
   264   by (simp add: sin_diff algebra_simps)
   265 
   266 lemma cos_eq_1:
   267   fixes z::complex
   268   shows "cos z = 1 \<longleftrightarrow> (\<exists>n::int. z = of_real(2 * n * pi))"
   269 proof -
   270   have "cos z = cos (2*(z/2))"
   271     by simp
   272   also have "... = 1 - 2 * sin (z/2) ^ 2"
   273     by (simp only: cos_double_sin)
   274   finally have [simp]: "cos z = 1 \<longleftrightarrow> sin (z/2) = 0"
   275     by simp
   276   show ?thesis
   277     by (auto simp: sin_eq_0 of_real_numeral)
   278 qed
   279 
   280 lemma csin_eq_1:
   281   fixes z::complex
   282   shows "sin z = 1 \<longleftrightarrow> (\<exists>n::int. z = of_real(2 * n * pi) + of_real pi/2)"
   283   using cos_eq_1 [of "z - of_real pi/2"]
   284   by (simp add: cos_diff algebra_simps)
   285 
   286 lemma csin_eq_minus1:
   287   fixes z::complex
   288   shows "sin z = -1 \<longleftrightarrow> (\<exists>n::int. z = of_real(2 * n * pi) + 3/2*pi)"
   289         (is "_ = ?rhs")
   290 proof -
   291   have "sin z = -1 \<longleftrightarrow> sin (-z) = 1"
   292     by (simp add: equation_minus_iff)
   293   also have "...  \<longleftrightarrow> (\<exists>n::int. -z = of_real(2 * n * pi) + of_real pi/2)"
   294     by (simp only: csin_eq_1)
   295   also have "...  \<longleftrightarrow> (\<exists>n::int. z = - of_real(2 * n * pi) - of_real pi/2)"
   296     apply (rule iff_exI)
   297     by (metis (no_types)  is_num_normalize(8) minus_minus of_real_def real_vector.scale_minus_left uminus_add_conv_diff)
   298   also have "... = ?rhs"
   299     apply (auto simp: of_real_numeral)
   300     apply (rule_tac [2] x="-(x+1)" in exI)
   301     apply (rule_tac x="-(x+1)" in exI)
   302     apply (simp_all add: algebra_simps)
   303     done
   304   finally show ?thesis .
   305 qed
   306 
   307 lemma ccos_eq_minus1:
   308   fixes z::complex
   309   shows "cos z = -1 \<longleftrightarrow> (\<exists>n::int. z = of_real(2 * n * pi) + pi)"
   310   using csin_eq_1 [of "z - of_real pi/2"]
   311   apply (simp add: sin_diff)
   312   apply (simp add: algebra_simps of_real_numeral equation_minus_iff)
   313   done
   314 
   315 lemma sin_eq_1: "sin x = 1 \<longleftrightarrow> (\<exists>n::int. x = (2 * n + 1 / 2) * pi)"
   316                 (is "_ = ?rhs")
   317 proof -
   318   have "sin x = 1 \<longleftrightarrow> sin (complex_of_real x) = 1"
   319     by (metis of_real_1 one_complex.simps(1) real_sin_eq sin_of_real)
   320   also have "...  \<longleftrightarrow> (\<exists>n::int. complex_of_real x = of_real(2 * n * pi) + of_real pi/2)"
   321     by (simp only: csin_eq_1)
   322   also have "...  \<longleftrightarrow> (\<exists>n::int. x = of_real(2 * n * pi) + of_real pi/2)"
   323     apply (rule iff_exI)
   324     apply (auto simp: algebra_simps of_real_numeral)
   325     apply (rule injD [OF inj_of_real [where 'a = complex]])
   326     apply (auto simp: of_real_numeral)
   327     done
   328   also have "... = ?rhs"
   329     by (auto simp: algebra_simps)
   330   finally show ?thesis .
   331 qed
   332 
   333 lemma sin_eq_minus1: "sin x = -1 \<longleftrightarrow> (\<exists>n::int. x = (2*n + 3/2) * pi)"  (is "_ = ?rhs")
   334 proof -
   335   have "sin x = -1 \<longleftrightarrow> sin (complex_of_real x) = -1"
   336     by (metis Re_complex_of_real of_real_def scaleR_minus1_left sin_of_real)
   337   also have "...  \<longleftrightarrow> (\<exists>n::int. complex_of_real x = of_real(2 * n * pi) + 3/2*pi)"
   338     by (simp only: csin_eq_minus1)
   339   also have "...  \<longleftrightarrow> (\<exists>n::int. x = of_real(2 * n * pi) + 3/2*pi)"
   340     apply (rule iff_exI)
   341     apply (auto simp: algebra_simps)
   342     apply (rule injD [OF inj_of_real [where 'a = complex]], auto)
   343     done
   344   also have "... = ?rhs"
   345     by (auto simp: algebra_simps)
   346   finally show ?thesis .
   347 qed
   348 
   349 lemma cos_eq_minus1: "cos x = -1 \<longleftrightarrow> (\<exists>n::int. x = (2*n + 1) * pi)"
   350                       (is "_ = ?rhs")
   351 proof -
   352   have "cos x = -1 \<longleftrightarrow> cos (complex_of_real x) = -1"
   353     by (metis Re_complex_of_real of_real_def scaleR_minus1_left cos_of_real)
   354   also have "...  \<longleftrightarrow> (\<exists>n::int. complex_of_real x = of_real(2 * n * pi) + pi)"
   355     by (simp only: ccos_eq_minus1)
   356   also have "...  \<longleftrightarrow> (\<exists>n::int. x = of_real(2 * n * pi) + pi)"
   357     apply (rule iff_exI)
   358     apply (auto simp: algebra_simps)
   359     apply (rule injD [OF inj_of_real [where 'a = complex]], auto)
   360     done
   361   also have "... = ?rhs"
   362     by (auto simp: algebra_simps)
   363   finally show ?thesis .
   364 qed
   365 
   366 lemma dist_exp_ii_1: "norm(exp(ii * of_real t) - 1) = 2 * abs(sin(t / 2))"
   367   apply (simp add: exp_Euler cmod_def power2_diff sin_of_real cos_of_real algebra_simps)
   368   using cos_double_sin [of "t/2"]
   369   apply (simp add: real_sqrt_mult)
   370   done
   371 
   372 lemma sinh_complex:
   373   fixes z :: complex
   374   shows "(exp z - inverse (exp z)) / 2 = -ii * sin(ii * z)"
   375   by (simp add: sin_exp_eq divide_simps exp_minus of_real_numeral)
   376 
   377 lemma sin_ii_times:
   378   fixes z :: complex
   379   shows "sin(ii * z) = ii * ((exp z - inverse (exp z)) / 2)"
   380   using sinh_complex by auto
   381 
   382 lemma sinh_real:
   383   fixes x :: real
   384   shows "of_real((exp x - inverse (exp x)) / 2) = -ii * sin(ii * of_real x)"
   385   by (simp add: exp_of_real sin_ii_times of_real_numeral)
   386 
   387 lemma cosh_complex:
   388   fixes z :: complex
   389   shows "(exp z + inverse (exp z)) / 2 = cos(ii * z)"
   390   by (simp add: cos_exp_eq divide_simps exp_minus of_real_numeral exp_of_real)
   391 
   392 lemma cosh_real:
   393   fixes x :: real
   394   shows "of_real((exp x + inverse (exp x)) / 2) = cos(ii * of_real x)"
   395   by (simp add: cos_exp_eq divide_simps exp_minus of_real_numeral exp_of_real)
   396 
   397 lemmas cos_ii_times = cosh_complex [symmetric]
   398 
   399 lemma norm_cos_squared:
   400     "norm(cos z) ^ 2 = cos(Re z) ^ 2 + (exp(Im z) - inverse(exp(Im z))) ^ 2 / 4"
   401   apply (cases z)
   402   apply (simp add: cos_add cmod_power2 cos_of_real sin_of_real)
   403   apply (simp add: cos_exp_eq sin_exp_eq exp_minus exp_of_real Re_divide Im_divide)
   404   apply (simp only: left_diff_distrib [symmetric] power_mult_distrib)
   405   apply (simp add: sin_squared_eq)
   406   apply (simp add: power2_eq_square algebra_simps divide_simps)
   407   done
   408 
   409 lemma norm_sin_squared:
   410     "norm(sin z) ^ 2 = (exp(2 * Im z) + inverse(exp(2 * Im z)) - 2 * cos(2 * Re z)) / 4"
   411   apply (cases z)
   412   apply (simp add: sin_add cmod_power2 cos_of_real sin_of_real cos_double_cos exp_double)
   413   apply (simp add: cos_exp_eq sin_exp_eq exp_minus exp_of_real Re_divide Im_divide)
   414   apply (simp only: left_diff_distrib [symmetric] power_mult_distrib)
   415   apply (simp add: cos_squared_eq)
   416   apply (simp add: power2_eq_square algebra_simps divide_simps)
   417   done
   418 
   419 lemma exp_uminus_Im: "exp (- Im z) \<le> exp (cmod z)"
   420   using abs_Im_le_cmod linear order_trans by fastforce
   421 
   422 lemma norm_cos_le:
   423   fixes z::complex
   424   shows "norm(cos z) \<le> exp(norm z)"
   425 proof -
   426   have "Im z \<le> cmod z"
   427     using abs_Im_le_cmod abs_le_D1 by auto
   428   with exp_uminus_Im show ?thesis
   429     apply (simp add: cos_exp_eq norm_divide)
   430     apply (rule order_trans [OF norm_triangle_ineq], simp)
   431     apply (metis add_mono exp_le_cancel_iff mult_2_right)
   432     done
   433 qed
   434 
   435 lemma norm_cos_plus1_le:
   436   fixes z::complex
   437   shows "norm(1 + cos z) \<le> 2 * exp(norm z)"
   438 proof -
   439   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"
   440       by arith
   441   have *: "Im z \<le> cmod z"
   442     using abs_Im_le_cmod abs_le_D1 by auto
   443   have triangle3: "\<And>x y z. norm(x + y + z) \<le> norm(x) + norm(y) + norm(z)"
   444     by (simp add: norm_add_rule_thm)
   445   have "norm(1 + cos z) = cmod (1 + (exp (\<i> * z) + exp (- (\<i> * z))) / 2)"
   446     by (simp add: cos_exp_eq)
   447   also have "... = cmod ((2 + exp (\<i> * z) + exp (- (\<i> * z))) / 2)"
   448     by (simp add: field_simps)
   449   also have "... = cmod (2 + exp (\<i> * z) + exp (- (\<i> * z))) / 2"
   450     by (simp add: norm_divide)
   451   finally show ?thesis
   452     apply (rule ssubst, simp)
   453     apply (rule order_trans [OF triangle3], simp)
   454     using exp_uminus_Im *
   455     apply (auto intro: mono)
   456     done
   457 qed
   458 
   459 subsection\<open>Taylor series for complex exponential, sine and cosine.\<close>
   460 
   461 declare power_Suc [simp del]
   462 
   463 lemma Taylor_exp:
   464   "norm(exp z - (\<Sum>k\<le>n. z ^ k / (fact k))) \<le> exp\<bar>Re z\<bar> * (norm z) ^ (Suc n) / (fact n)"
   465 proof (rule complex_taylor [of _ n "\<lambda>k. exp" "exp\<bar>Re z\<bar>" 0 z, simplified])
   466   show "convex (closed_segment 0 z)"
   467     by (rule convex_segment [of 0 z])
   468 next
   469   fix k x
   470   assume "x \<in> closed_segment 0 z" "k \<le> n"
   471   show "(exp has_field_derivative exp x) (at x within closed_segment 0 z)"
   472     using DERIV_exp DERIV_subset by blast
   473 next
   474   fix x
   475   assume "x \<in> closed_segment 0 z"
   476   then show "Re x \<le> \<bar>Re z\<bar>"
   477     apply (auto simp: closed_segment_def scaleR_conv_of_real)
   478     by (meson abs_ge_self abs_ge_zero linear mult_left_le_one_le mult_nonneg_nonpos order_trans)
   479 next
   480   show "0 \<in> closed_segment 0 z"
   481     by (auto simp: closed_segment_def)
   482 next
   483   show "z \<in> closed_segment 0 z"
   484     apply (simp add: closed_segment_def scaleR_conv_of_real)
   485     using of_real_1 zero_le_one by blast
   486 qed
   487 
   488 lemma
   489   assumes "0 \<le> u" "u \<le> 1"
   490   shows cmod_sin_le_exp: "cmod (sin (u *\<^sub>R z)) \<le> exp \<bar>Im z\<bar>"
   491     and cmod_cos_le_exp: "cmod (cos (u *\<^sub>R z)) \<le> exp \<bar>Im z\<bar>"
   492 proof -
   493   have mono: "\<And>u w z::real. w \<le> u \<Longrightarrow> z \<le> u \<Longrightarrow> w + z \<le> u*2"
   494     by arith
   495   show "cmod (sin (u *\<^sub>R z)) \<le> exp \<bar>Im z\<bar>" using assms
   496     apply (auto simp: scaleR_conv_of_real norm_mult norm_power sin_exp_eq norm_divide)
   497     apply (rule order_trans [OF norm_triangle_ineq4])
   498     apply (rule mono)
   499     apply (auto simp: abs_if mult_left_le_one_le)
   500     apply (meson mult_nonneg_nonneg neg_le_0_iff_le not_le order_trans)
   501     apply (meson less_eq_real_def mult_nonneg_nonpos neg_0_le_iff_le order_trans)
   502     done
   503   show "cmod (cos (u *\<^sub>R z)) \<le> exp \<bar>Im z\<bar>" using assms
   504     apply (auto simp: scaleR_conv_of_real norm_mult norm_power cos_exp_eq norm_divide)
   505     apply (rule order_trans [OF norm_triangle_ineq])
   506     apply (rule mono)
   507     apply (auto simp: abs_if mult_left_le_one_le)
   508     apply (meson mult_nonneg_nonneg neg_le_0_iff_le not_le order_trans)
   509     apply (meson less_eq_real_def mult_nonneg_nonpos neg_0_le_iff_le order_trans)
   510     done
   511 qed
   512 
   513 lemma Taylor_sin:
   514   "norm(sin z - (\<Sum>k\<le>n. complex_of_real (sin_coeff k) * z ^ k))
   515    \<le> exp\<bar>Im z\<bar> * (norm z) ^ (Suc n) / (fact n)"
   516 proof -
   517   have mono: "\<And>u w z::real. w \<le> u \<Longrightarrow> z \<le> u \<Longrightarrow> w + z \<le> u*2"
   518       by arith
   519   have *: "cmod (sin z -
   520                  (\<Sum>i\<le>n. (-1) ^ (i div 2) * (if even i then sin 0 else cos 0) * z ^ i / (fact i)))
   521            \<le> exp \<bar>Im z\<bar> * cmod z ^ Suc n / (fact n)"
   522   proof (rule complex_taylor [of "closed_segment 0 z" n 
   523                                  "\<lambda>k x. (-1)^(k div 2) * (if even k then sin x else cos x)" 
   524                                  "exp\<bar>Im z\<bar>" 0 z,  simplified])
   525   show "convex (closed_segment 0 z)"
   526     by (rule convex_segment [of 0 z])
   527   next
   528     fix k x
   529     show "((\<lambda>x. (- 1) ^ (k div 2) * (if even k then sin x else cos x)) has_field_derivative
   530             (- 1) ^ (Suc k div 2) * (if odd k then sin x else cos x))
   531             (at x within closed_segment 0 z)"
   532       apply (auto simp: power_Suc)
   533       apply (intro derivative_eq_intros | simp)+
   534       done
   535   next
   536     fix x
   537     assume "x \<in> closed_segment 0 z"
   538     then show "cmod ((- 1) ^ (Suc n div 2) * (if odd n then sin x else cos x)) \<le> exp \<bar>Im z\<bar>"
   539       by (auto simp: closed_segment_def norm_mult norm_power cmod_sin_le_exp cmod_cos_le_exp)
   540   next
   541     show "0 \<in> closed_segment 0 z"
   542       by (auto simp: closed_segment_def)
   543   next
   544     show "z \<in> closed_segment 0 z"
   545       apply (simp add: closed_segment_def scaleR_conv_of_real)
   546       using of_real_1 zero_le_one by blast
   547   qed
   548   have **: "\<And>k. complex_of_real (sin_coeff k) * z ^ k
   549             = (-1)^(k div 2) * (if even k then sin 0 else cos 0) * z^k / of_nat (fact k)"
   550     by (auto simp: sin_coeff_def elim!: oddE)
   551   show ?thesis
   552     apply (rule order_trans [OF _ *])
   553     apply (simp add: **)
   554     done
   555 qed
   556 
   557 lemma Taylor_cos:
   558   "norm(cos z - (\<Sum>k\<le>n. complex_of_real (cos_coeff k) * z ^ k))
   559    \<le> exp\<bar>Im z\<bar> * (norm z) ^ Suc n / (fact n)"
   560 proof -
   561   have mono: "\<And>u w z::real. w \<le> u \<Longrightarrow> z \<le> u \<Longrightarrow> w + z \<le> u*2"
   562       by arith
   563   have *: "cmod (cos z -
   564                  (\<Sum>i\<le>n. (-1) ^ (Suc i div 2) * (if even i then cos 0 else sin 0) * z ^ i / (fact i)))
   565            \<le> exp \<bar>Im z\<bar> * cmod z ^ Suc n / (fact n)"
   566   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,
   567 simplified])
   568   show "convex (closed_segment 0 z)"
   569     by (rule convex_segment [of 0 z])
   570   next
   571     fix k x
   572     assume "x \<in> closed_segment 0 z" "k \<le> n"
   573     show "((\<lambda>x. (- 1) ^ (Suc k div 2) * (if even k then cos x else sin x)) has_field_derivative
   574             (- 1) ^ Suc (k div 2) * (if odd k then cos x else sin x))
   575              (at x within closed_segment 0 z)"
   576       apply (auto simp: power_Suc)
   577       apply (intro derivative_eq_intros | simp)+
   578       done
   579   next
   580     fix x
   581     assume "x \<in> closed_segment 0 z"
   582     then show "cmod ((- 1) ^ Suc (n div 2) * (if odd n then cos x else sin x)) \<le> exp \<bar>Im z\<bar>"
   583       by (auto simp: closed_segment_def norm_mult norm_power cmod_sin_le_exp cmod_cos_le_exp)
   584   next
   585     show "0 \<in> closed_segment 0 z"
   586       by (auto simp: closed_segment_def)
   587   next
   588     show "z \<in> closed_segment 0 z"
   589       apply (simp add: closed_segment_def scaleR_conv_of_real)
   590       using of_real_1 zero_le_one by blast
   591   qed
   592   have **: "\<And>k. complex_of_real (cos_coeff k) * z ^ k
   593             = (-1)^(Suc k div 2) * (if even k then cos 0 else sin 0) * z^k / of_nat (fact k)"
   594     by (auto simp: cos_coeff_def elim!: evenE)
   595   show ?thesis
   596     apply (rule order_trans [OF _ *])
   597     apply (simp add: **)
   598     done
   599 qed
   600 
   601 declare power_Suc [simp]
   602 
   603 text\<open>32-bit Approximation to e\<close>
   604 lemma e_approx_32: "abs(exp(1) - 5837465777 / 2147483648) \<le> (inverse(2 ^ 32)::real)"
   605   using Taylor_exp [of 1 14] exp_le
   606   apply (simp add: setsum_left_distrib in_Reals_norm Re_exp atMost_nat_numeral fact_numeral)
   607   apply (simp only: pos_le_divide_eq [symmetric], linarith)
   608   done
   609 
   610 lemma e_less_3: "exp 1 < (3::real)"
   611   using e_approx_32
   612   by (simp add: abs_if split: split_if_asm)
   613 
   614 lemma ln3_gt_1: "ln 3 > (1::real)"
   615   by (metis e_less_3 exp_less_cancel_iff exp_ln_iff less_trans ln_exp)
   616 
   617 
   618 subsection\<open>The argument of a complex number\<close>
   619 
   620 definition Arg :: "complex \<Rightarrow> real" where
   621  "Arg z \<equiv> if z = 0 then 0
   622            else THE t. 0 \<le> t \<and> t < 2*pi \<and>
   623                     z = of_real(norm z) * exp(ii * of_real t)"
   624 
   625 lemma Arg_0 [simp]: "Arg(0) = 0"
   626   by (simp add: Arg_def)
   627 
   628 lemma Arg_unique_lemma:
   629   assumes z:  "z = of_real(norm z) * exp(ii * of_real t)"
   630       and z': "z = of_real(norm z) * exp(ii * of_real t')"
   631       and t:  "0 \<le> t"  "t < 2*pi"
   632       and t': "0 \<le> t'" "t' < 2*pi"
   633       and nz: "z \<noteq> 0"
   634   shows "t' = t"
   635 proof -
   636   have [dest]: "\<And>x y z::real. x\<ge>0 \<Longrightarrow> x+y < z \<Longrightarrow> y<z"
   637     by arith
   638   have "of_real (cmod z) * exp (\<i> * of_real t') = of_real (cmod z) * exp (\<i> * of_real t)"
   639     by (metis z z')
   640   then have "exp (\<i> * of_real t') = exp (\<i> * of_real t)"
   641     by (metis nz mult_left_cancel mult_zero_left z)
   642   then have "sin t' = sin t \<and> cos t' = cos t"
   643     apply (simp add: exp_Euler sin_of_real cos_of_real)
   644     by (metis Complex_eq complex.sel)
   645   then obtain n::int where n: "t' = t + 2 * real n * pi"
   646     by (auto simp: sin_cos_eq_iff)
   647   then have "n=0"
   648     apply (rule_tac z=n in int_cases)
   649     using t t'
   650     apply (auto simp: mult_less_0_iff algebra_simps)
   651     done
   652   then show "t' = t"
   653       by (simp add: n)
   654 qed
   655 
   656 lemma Arg: "0 \<le> Arg z & Arg z < 2*pi & z = of_real(norm z) * exp(ii * of_real(Arg z))"
   657 proof (cases "z=0")
   658   case True then show ?thesis
   659     by (simp add: Arg_def)
   660 next
   661   case False
   662   obtain t where t: "0 \<le> t" "t < 2*pi"
   663              and ReIm: "Re z / cmod z = cos t" "Im z / cmod z = sin t"
   664     using sincos_total_2pi [OF complex_unit_circle [OF False]]
   665     by blast
   666   have z: "z = of_real(norm z) * exp(ii * of_real t)"
   667     apply (rule complex_eqI)
   668     using t False ReIm
   669     apply (auto simp: exp_Euler sin_of_real cos_of_real divide_simps)
   670     done
   671   show ?thesis
   672     apply (simp add: Arg_def False)
   673     apply (rule theI [where a=t])
   674     using t z False
   675     apply (auto intro: Arg_unique_lemma)
   676     done
   677 qed
   678 
   679 
   680 corollary
   681   shows Arg_ge_0: "0 \<le> Arg z"
   682     and Arg_lt_2pi: "Arg z < 2*pi"
   683     and Arg_eq: "z = of_real(norm z) * exp(ii * of_real(Arg z))"
   684   using Arg by auto
   685 
   686 lemma complex_norm_eq_1_exp: "norm z = 1 \<longleftrightarrow> (\<exists>t. z = exp(ii * of_real t))"
   687   using Arg [of z] by auto
   688 
   689 lemma Arg_unique: "\<lbrakk>of_real r * exp(ii * of_real a) = z; 0 < r; 0 \<le> a; a < 2*pi\<rbrakk> \<Longrightarrow> Arg z = a"
   690   apply (rule Arg_unique_lemma [OF _ Arg_eq])
   691   using Arg [of z]
   692   apply (auto simp: norm_mult)
   693   done
   694 
   695 lemma Arg_minus: "z \<noteq> 0 \<Longrightarrow> Arg (-z) = (if Arg z < pi then Arg z + pi else Arg z - pi)"
   696   apply (rule Arg_unique [of "norm z"])
   697   apply (rule complex_eqI)
   698   using Arg_ge_0 [of z] Arg_eq [of z] Arg_lt_2pi [of z] Arg_eq [of z]
   699   apply auto
   700   apply (auto simp: Re_exp Im_exp cos_diff sin_diff cis_conv_exp [symmetric])
   701   apply (metis Re_rcis Im_rcis rcis_def)+
   702   done
   703 
   704 lemma Arg_times_of_real [simp]: "0 < r \<Longrightarrow> Arg (of_real r * z) = Arg z"
   705   apply (cases "z=0", simp)
   706   apply (rule Arg_unique [of "r * norm z"])
   707   using Arg
   708   apply auto
   709   done
   710 
   711 lemma Arg_times_of_real2 [simp]: "0 < r \<Longrightarrow> Arg (z * of_real r) = Arg z"
   712   by (metis Arg_times_of_real mult.commute)
   713 
   714 lemma Arg_divide_of_real [simp]: "0 < r \<Longrightarrow> Arg (z / of_real r) = Arg z"
   715   by (metis Arg_times_of_real2 less_numeral_extra(3) nonzero_eq_divide_eq of_real_eq_0_iff)
   716 
   717 lemma Arg_le_pi: "Arg z \<le> pi \<longleftrightarrow> 0 \<le> Im z"
   718 proof (cases "z=0")
   719   case True then show ?thesis
   720     by simp
   721 next
   722   case False
   723   have "0 \<le> Im z \<longleftrightarrow> 0 \<le> Im (of_real (cmod z) * exp (\<i> * complex_of_real (Arg z)))"
   724     by (metis Arg_eq)
   725   also have "... = (0 \<le> Im (exp (\<i> * complex_of_real (Arg z))))"
   726     using False
   727     by (simp add: zero_le_mult_iff)
   728   also have "... \<longleftrightarrow> Arg z \<le> pi"
   729     by (simp add: Im_exp) (metis Arg_ge_0 Arg_lt_2pi sin_lt_zero sin_ge_zero not_le)
   730   finally show ?thesis
   731     by blast
   732 qed
   733 
   734 lemma Arg_lt_pi: "0 < Arg z \<and> Arg z < pi \<longleftrightarrow> 0 < Im z"
   735 proof (cases "z=0")
   736   case True then show ?thesis
   737     by simp
   738 next
   739   case False
   740   have "0 < Im z \<longleftrightarrow> 0 < Im (of_real (cmod z) * exp (\<i> * complex_of_real (Arg z)))"
   741     by (metis Arg_eq)
   742   also have "... = (0 < Im (exp (\<i> * complex_of_real (Arg z))))"
   743     using False
   744     by (simp add: zero_less_mult_iff)
   745   also have "... \<longleftrightarrow> 0 < Arg z \<and> Arg z < pi"
   746     using Arg_ge_0  Arg_lt_2pi sin_le_zero sin_gt_zero
   747     apply (auto simp: Im_exp)
   748     using le_less apply fastforce
   749     using not_le by blast
   750   finally show ?thesis
   751     by blast
   752 qed
   753 
   754 lemma Arg_eq_0: "Arg z = 0 \<longleftrightarrow> z \<in> \<real> \<and> 0 \<le> Re z"
   755 proof (cases "z=0")
   756   case True then show ?thesis
   757     by simp
   758 next
   759   case False
   760   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)))"
   761     by (metis Arg_eq)
   762   also have "... \<longleftrightarrow> z \<in> \<real> \<and> 0 \<le> Re (exp (\<i> * complex_of_real (Arg z)))"
   763     using False
   764     by (simp add: zero_le_mult_iff)
   765   also have "... \<longleftrightarrow> Arg z = 0"
   766     apply (auto simp: Re_exp)
   767     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)
   768     using Arg_eq [of z]
   769     apply (auto simp: Reals_def)
   770     done
   771   finally show ?thesis
   772     by blast
   773 qed
   774 
   775 corollary Arg_gt_0: 
   776   assumes "z \<in> \<real> \<Longrightarrow> Re z < 0"
   777     shows "Arg z > 0"
   778   using Arg_eq_0 Arg_ge_0 assms dual_order.strict_iff_order by fastforce
   779 
   780 lemma Arg_of_real: "Arg(of_real x) = 0 \<longleftrightarrow> 0 \<le> x"
   781   by (simp add: Arg_eq_0)
   782 
   783 lemma Arg_eq_pi: "Arg z = pi \<longleftrightarrow> z \<in> \<real> \<and> Re z < 0"
   784   apply  (cases "z=0", simp)
   785   using Arg_eq_0 [of "-z"]
   786   apply (auto simp: complex_is_Real_iff Arg_minus)
   787   apply (simp add: complex_Re_Im_cancel_iff)
   788   apply (metis Arg_minus pi_gt_zero add.left_neutral minus_minus minus_zero)
   789   done
   790 
   791 lemma Arg_eq_0_pi: "Arg z = 0 \<or> Arg z = pi \<longleftrightarrow> z \<in> \<real>"
   792   using Arg_eq_0 Arg_eq_pi not_le by auto
   793 
   794 lemma Arg_inverse: "Arg(inverse z) = (if z \<in> \<real> \<and> 0 \<le> Re z then Arg z else 2*pi - Arg z)"
   795   apply (cases "z=0", simp)
   796   apply (rule Arg_unique [of "inverse (norm z)"])
   797   using Arg_ge_0 [of z] Arg_lt_2pi [of z] Arg_eq [of z] Arg_eq_0 [of z] Exp_two_pi_i
   798   apply (auto simp: of_real_numeral algebra_simps exp_diff divide_simps)
   799   done
   800 
   801 lemma Arg_eq_iff:
   802   assumes "w \<noteq> 0" "z \<noteq> 0"
   803      shows "Arg w = Arg z \<longleftrightarrow> (\<exists>x. 0 < x & w = of_real x * z)"
   804   using assms Arg_eq [of z] Arg_eq [of w]
   805   apply auto
   806   apply (rule_tac x="norm w / norm z" in exI)
   807   apply (simp add: divide_simps)
   808   by (metis mult.commute mult.left_commute)
   809 
   810 lemma Arg_inverse_eq_0: "Arg(inverse z) = 0 \<longleftrightarrow> Arg z = 0"
   811   using complex_is_Real_iff
   812   apply (simp add: Arg_eq_0)
   813   apply (auto simp: divide_simps not_sum_power2_lt_zero)
   814   done
   815 
   816 lemma Arg_divide:
   817   assumes "w \<noteq> 0" "z \<noteq> 0" "Arg w \<le> Arg z"
   818     shows "Arg(z / w) = Arg z - Arg w"
   819   apply (rule Arg_unique [of "norm(z / w)"])
   820   using assms Arg_eq [of z] Arg_eq [of w] Arg_ge_0 [of w] Arg_lt_2pi [of z]
   821   apply (auto simp: exp_diff norm_divide algebra_simps divide_simps)
   822   done
   823 
   824 lemma Arg_le_div_sum:
   825   assumes "w \<noteq> 0" "z \<noteq> 0" "Arg w \<le> Arg z"
   826     shows "Arg z = Arg w + Arg(z / w)"
   827   by (simp add: Arg_divide assms)
   828 
   829 lemma Arg_le_div_sum_eq:
   830   assumes "w \<noteq> 0" "z \<noteq> 0"
   831     shows "Arg w \<le> Arg z \<longleftrightarrow> Arg z = Arg w + Arg(z / w)"
   832   using assms
   833   by (auto simp: Arg_ge_0 intro: Arg_le_div_sum)
   834 
   835 lemma Arg_diff:
   836   assumes "w \<noteq> 0" "z \<noteq> 0"
   837     shows "Arg w - Arg z = (if Arg z \<le> Arg w then Arg(w / z) else Arg(w/z) - 2*pi)"
   838   using assms
   839   apply (auto simp: Arg_ge_0 Arg_divide not_le)
   840   using Arg_divide [of w z] Arg_inverse [of "w/z"]
   841   apply auto
   842   by (metis Arg_eq_0 less_irrefl minus_diff_eq right_minus_eq)
   843 
   844 lemma Arg_add:
   845   assumes "w \<noteq> 0" "z \<noteq> 0"
   846     shows "Arg w + Arg z = (if Arg w + Arg z < 2*pi then Arg(w * z) else Arg(w * z) + 2*pi)"
   847   using assms
   848   using Arg_diff [of "w*z" z] Arg_le_div_sum_eq [of z "w*z"]
   849   apply (auto simp: Arg_ge_0 Arg_divide not_le)
   850   apply (metis Arg_lt_2pi add.commute)
   851   apply (metis (no_types) Arg add.commute diff_0 diff_add_cancel diff_less_eq diff_minus_eq_add not_less)
   852   done
   853 
   854 lemma Arg_times:
   855   assumes "w \<noteq> 0" "z \<noteq> 0"
   856     shows "Arg (w * z) = (if Arg w + Arg z < 2*pi then Arg w + Arg z
   857                             else (Arg w + Arg z) - 2*pi)"
   858   using Arg_add [OF assms]
   859   by auto
   860 
   861 lemma Arg_cnj: "Arg(cnj z) = (if z \<in> \<real> \<and> 0 \<le> Re z then Arg z else 2*pi - Arg z)"
   862   apply (cases "z=0", simp)
   863   apply (rule trans [of _ "Arg(inverse z)"])
   864   apply (simp add: Arg_eq_iff divide_simps complex_norm_square [symmetric] mult.commute)
   865   apply (metis norm_eq_zero of_real_power zero_less_power2)
   866   apply (auto simp: of_real_numeral Arg_inverse)
   867   done
   868 
   869 lemma Arg_real: "z \<in> \<real> \<Longrightarrow> Arg z = (if 0 \<le> Re z then 0 else pi)"
   870   using Arg_eq_0 Arg_eq_0_pi
   871   by auto
   872 
   873 lemma Arg_exp: "0 \<le> Im z \<Longrightarrow> Im z < 2*pi \<Longrightarrow> Arg(exp z) = Im z"
   874   by (rule Arg_unique [of  "exp(Re z)"]) (auto simp: Exp_eq_polar)
   875 
   876 
   877 subsection\<open>Analytic properties of tangent function\<close>
   878 
   879 lemma cnj_tan: "cnj(tan z) = tan(cnj z)"
   880   by (simp add: cnj_cos cnj_sin tan_def)
   881 
   882 lemma complex_differentiable_at_tan: "~(cos z = 0) \<Longrightarrow> tan complex_differentiable at z"
   883   unfolding complex_differentiable_def
   884   using DERIV_tan by blast
   885 
   886 lemma complex_differentiable_within_tan: "~(cos z = 0)
   887          \<Longrightarrow> tan complex_differentiable (at z within s)"
   888   using complex_differentiable_at_tan complex_differentiable_at_within by blast
   889 
   890 lemma continuous_within_tan: "~(cos z = 0) \<Longrightarrow> continuous (at z within s) tan"
   891   using continuous_at_imp_continuous_within isCont_tan by blast
   892 
   893 lemma continuous_on_tan [continuous_intros]: "(\<And>z. z \<in> s \<Longrightarrow> ~(cos z = 0)) \<Longrightarrow> continuous_on s tan"
   894   by (simp add: continuous_at_imp_continuous_on)
   895 
   896 lemma holomorphic_on_tan: "(\<And>z. z \<in> s \<Longrightarrow> ~(cos z = 0)) \<Longrightarrow> tan holomorphic_on s"
   897   by (simp add: complex_differentiable_within_tan holomorphic_on_def)
   898 
   899 
   900 subsection\<open>Complex logarithms (the conventional principal value)\<close>
   901 
   902 instantiation complex :: ln
   903 begin
   904 
   905 definition ln_complex :: "complex \<Rightarrow> complex"
   906   where "ln_complex \<equiv> \<lambda>z. THE w. exp w = z & -pi < Im(w) & Im(w) \<le> pi"
   907 
   908 lemma
   909   assumes "z \<noteq> 0"
   910     shows exp_Ln [simp]:  "exp(ln z) = z"
   911       and mpi_less_Im_Ln: "-pi < Im(ln z)"
   912       and Im_Ln_le_pi:    "Im(ln z) \<le> pi"
   913 proof -
   914   obtain \<psi> where z: "z / (cmod z) = Complex (cos \<psi>) (sin \<psi>)"
   915     using complex_unimodular_polar [of "z / (norm z)"] assms
   916     by (auto simp: norm_divide divide_simps)
   917   obtain \<phi> where \<phi>: "- pi < \<phi>" "\<phi> \<le> pi" "sin \<phi> = sin \<psi>" "cos \<phi> = cos \<psi>"
   918     using sincos_principal_value [of "\<psi>"] assms
   919     by (auto simp: norm_divide divide_simps)
   920   have "exp(ln z) = z & -pi < Im(ln z) & Im(ln z) \<le> pi" unfolding ln_complex_def
   921     apply (rule theI [where a = "Complex (ln(norm z)) \<phi>"])
   922     using z assms \<phi>
   923     apply (auto simp: field_simps exp_complex_eqI Exp_eq_polar cis.code)
   924     done
   925   then show "exp(ln z) = z" "-pi < Im(ln z)" "Im(ln z) \<le> pi"
   926     by auto
   927 qed
   928 
   929 lemma Ln_exp [simp]:
   930   assumes "-pi < Im(z)" "Im(z) \<le> pi"
   931     shows "ln(exp z) = z"
   932   apply (rule exp_complex_eqI)
   933   using assms mpi_less_Im_Ln  [of "exp z"] Im_Ln_le_pi [of "exp z"]
   934   apply auto
   935   done
   936 
   937 subsection\<open>Relation to Real Logarithm\<close>
   938 
   939 lemma Ln_of_real:
   940   assumes "0 < z"
   941     shows "ln(of_real z::complex) = of_real(ln z)"
   942 proof -
   943   have "ln(of_real (exp (ln z))::complex) = ln (exp (of_real (ln z)))"
   944     by (simp add: exp_of_real)
   945   also have "... = of_real(ln z)"
   946     using assms
   947     by (subst Ln_exp) auto
   948   finally show ?thesis
   949     using assms by simp
   950 qed
   951 
   952 corollary Ln_in_Reals [simp]: "z \<in> \<real> \<Longrightarrow> Re z > 0 \<Longrightarrow> ln z \<in> \<real>"
   953   by (auto simp: Ln_of_real elim: Reals_cases)
   954 
   955 corollary Im_Ln_of_real [simp]: "r > 0 \<Longrightarrow> Im (ln (of_real r)) = 0"
   956   by (simp add: Ln_of_real)
   957 
   958 lemma cmod_Ln_Reals [simp]: "z \<in> \<real> \<Longrightarrow> 0 < Re z \<Longrightarrow> cmod (ln z) = norm (ln (Re z))"
   959   using Ln_of_real by force
   960 
   961 lemma Ln_1: "ln 1 = (0::complex)"
   962 proof -
   963   have "ln (exp 0) = (0::complex)"
   964     by (metis (mono_tags, hide_lams) Ln_of_real exp_zero ln_one of_real_0 of_real_1 zero_less_one)
   965   then show ?thesis
   966     by simp
   967 qed
   968 
   969 instance
   970   by intro_classes (rule ln_complex_def Ln_1)
   971 
   972 end
   973 
   974 abbreviation Ln :: "complex \<Rightarrow> complex"
   975   where "Ln \<equiv> ln"
   976 
   977 lemma Ln_eq_iff: "w \<noteq> 0 \<Longrightarrow> z \<noteq> 0 \<Longrightarrow> (Ln w = Ln z \<longleftrightarrow> w = z)"
   978   by (metis exp_Ln)
   979 
   980 lemma Ln_unique: "exp(z) = w \<Longrightarrow> -pi < Im(z) \<Longrightarrow> Im(z) \<le> pi \<Longrightarrow> Ln w = z"
   981   using Ln_exp by blast
   982 
   983 lemma Re_Ln [simp]: "z \<noteq> 0 \<Longrightarrow> Re(Ln z) = ln(norm z)"
   984   by (metis exp_Ln assms ln_exp norm_exp_eq_Re)
   985 
   986 corollary ln_cmod_le: 
   987   assumes z: "z \<noteq> 0"
   988     shows "ln (cmod z) \<le> cmod (Ln z)"
   989   using norm_exp [of "Ln z", simplified exp_Ln [OF z]]
   990   by (metis Re_Ln complex_Re_le_cmod z)
   991 
   992 lemma exists_complex_root:
   993   fixes a :: complex
   994   shows "n \<noteq> 0 \<Longrightarrow> \<exists>z. z ^ n = a"
   995   apply (cases "a=0", simp)
   996   apply (rule_tac x= "exp(Ln(a) / n)" in exI)
   997   apply (auto simp: exp_of_nat_mult [symmetric])
   998   done
   999 
  1000 
  1001 subsection\<open>The Unwinding Number and the Ln-product Formula\<close>
  1002 
  1003 text\<open>Note that in this special case the unwinding number is -1, 0 or 1.\<close>
  1004 
  1005 definition unwinding :: "complex \<Rightarrow> complex" where
  1006    "unwinding(z) = (z - Ln(exp z)) / (of_real(2*pi) * ii)"
  1007 
  1008 lemma unwinding_2pi: "(2*pi) * ii * unwinding(z) = z - Ln(exp z)"
  1009   by (simp add: unwinding_def)
  1010 
  1011 lemma Ln_times_unwinding:
  1012     "w \<noteq> 0 \<Longrightarrow> z \<noteq> 0 \<Longrightarrow> Ln(w * z) = Ln(w) + Ln(z) - (2*pi) * ii * unwinding(Ln w + Ln z)"
  1013   using unwinding_2pi by (simp add: exp_add)
  1014 
  1015 
  1016 subsection\<open>Derivative of Ln away from the branch cut\<close>
  1017 
  1018 lemma
  1019   assumes "Im(z) = 0 \<Longrightarrow> 0 < Re(z)"
  1020     shows has_field_derivative_Ln: "(Ln has_field_derivative inverse(z)) (at z)"
  1021       and Im_Ln_less_pi:           "Im (Ln z) < pi"
  1022 proof -
  1023   have znz: "z \<noteq> 0"
  1024     using assms by auto
  1025   then show *: "Im (Ln z) < pi" using assms
  1026     by (metis exp_Ln Im_Ln_le_pi Im_exp Re_exp abs_of_nonneg cmod_eq_Re cos_pi mult.right_neutral mult_minus_right mult_zero_right neg_less_0_iff_less norm_exp_eq_Re not_less not_less_iff_gr_or_eq sin_pi)
  1027   show "(Ln has_field_derivative inverse(z)) (at z)"
  1028     apply (rule has_complex_derivative_inverse_strong_x
  1029               [where f = exp and s = "{w. -pi < Im(w) & Im(w) < pi}"])
  1030     using znz *
  1031     apply (auto simp: continuous_on_exp open_Collect_conj open_halfspace_Im_gt open_halfspace_Im_lt)
  1032     apply (metis DERIV_exp exp_Ln)
  1033     apply (metis mpi_less_Im_Ln)
  1034     done
  1035 qed
  1036 
  1037 declare has_field_derivative_Ln [derivative_intros]
  1038 declare has_field_derivative_Ln [THEN DERIV_chain2, derivative_intros]
  1039 
  1040 lemma complex_differentiable_at_Ln: "(Im(z) = 0 \<Longrightarrow> 0 < Re(z)) \<Longrightarrow> Ln complex_differentiable at z"
  1041   using complex_differentiable_def has_field_derivative_Ln by blast
  1042 
  1043 lemma complex_differentiable_within_Ln: "(Im(z) = 0 \<Longrightarrow> 0 < Re(z))
  1044          \<Longrightarrow> Ln complex_differentiable (at z within s)"
  1045   using complex_differentiable_at_Ln complex_differentiable_within_subset by blast
  1046 
  1047 lemma continuous_at_Ln: "(Im(z) = 0 \<Longrightarrow> 0 < Re(z)) \<Longrightarrow> continuous (at z) Ln"
  1048   by (simp add: complex_differentiable_imp_continuous_at complex_differentiable_within_Ln)
  1049 
  1050 lemma isCont_Ln' [simp]:
  1051    "\<lbrakk>isCont f z; Im(f z) = 0 \<Longrightarrow> 0 < Re(f z)\<rbrakk> \<Longrightarrow> isCont (\<lambda>x. Ln (f x)) z"
  1052   by (blast intro: isCont_o2 [OF _ continuous_at_Ln])
  1053 
  1054 lemma continuous_within_Ln: "(Im(z) = 0 \<Longrightarrow> 0 < Re(z)) \<Longrightarrow> continuous (at z within s) Ln"
  1055   using continuous_at_Ln continuous_at_imp_continuous_within by blast
  1056 
  1057 lemma continuous_on_Ln [continuous_intros]: "(\<And>z. z \<in> s \<Longrightarrow> Im(z) = 0 \<Longrightarrow> 0 < Re(z)) \<Longrightarrow> continuous_on s Ln"
  1058   by (simp add: continuous_at_imp_continuous_on continuous_within_Ln)
  1059 
  1060 lemma holomorphic_on_Ln: "(\<And>z. z \<in> s \<Longrightarrow> Im(z) = 0 \<Longrightarrow> 0 < Re(z)) \<Longrightarrow> Ln holomorphic_on s"
  1061   by (simp add: complex_differentiable_within_Ln holomorphic_on_def)
  1062 
  1063 
  1064 subsection\<open>Quadrant-type results for Ln\<close>
  1065 
  1066 lemma cos_lt_zero_pi: "pi/2 < x \<Longrightarrow> x < 3*pi/2 \<Longrightarrow> cos x < 0"
  1067   using cos_minus_pi cos_gt_zero_pi [of "x-pi"]
  1068   by simp
  1069 
  1070 lemma Re_Ln_pos_lt:
  1071   assumes "z \<noteq> 0"
  1072     shows "abs(Im(Ln z)) < pi/2 \<longleftrightarrow> 0 < Re(z)"
  1073 proof -
  1074   { fix w
  1075     assume "w = Ln z"
  1076     then have w: "Im w \<le> pi" "- pi < Im w"
  1077       using Im_Ln_le_pi [of z]  mpi_less_Im_Ln [of z]  assms
  1078       by auto
  1079     then have "abs(Im w) < pi/2 \<longleftrightarrow> 0 < Re(exp w)"
  1080       apply (auto simp: Re_exp zero_less_mult_iff cos_gt_zero_pi)
  1081       using cos_lt_zero_pi [of "-(Im w)"] cos_lt_zero_pi [of "(Im w)"]
  1082       apply (simp add: abs_if split: split_if_asm)
  1083       apply (metis (no_types) cos_minus cos_pi_half eq_divide_eq_numeral1(1) eq_numeral_simps(4)
  1084                less_numeral_extra(3) linorder_neqE_linordered_idom minus_mult_minus minus_mult_right
  1085                mult_numeral_1_right)
  1086       done
  1087   }
  1088   then show ?thesis using assms
  1089     by auto
  1090 qed
  1091 
  1092 lemma Re_Ln_pos_le:
  1093   assumes "z \<noteq> 0"
  1094     shows "abs(Im(Ln z)) \<le> pi/2 \<longleftrightarrow> 0 \<le> Re(z)"
  1095 proof -
  1096   { fix w
  1097     assume "w = Ln z"
  1098     then have w: "Im w \<le> pi" "- pi < Im w"
  1099       using Im_Ln_le_pi [of z]  mpi_less_Im_Ln [of z]  assms
  1100       by auto
  1101     then have "abs(Im w) \<le> pi/2 \<longleftrightarrow> 0 \<le> Re(exp w)"
  1102       apply (auto simp: Re_exp zero_le_mult_iff cos_ge_zero)
  1103       using cos_lt_zero_pi [of "- (Im w)"] cos_lt_zero_pi [of "(Im w)"] not_le
  1104       apply (auto simp: abs_if split: split_if_asm)
  1105       done
  1106   }
  1107   then show ?thesis using assms
  1108     by auto
  1109 qed
  1110 
  1111 lemma Im_Ln_pos_lt:
  1112   assumes "z \<noteq> 0"
  1113     shows "0 < Im(Ln z) \<and> Im(Ln z) < pi \<longleftrightarrow> 0 < Im(z)"
  1114 proof -
  1115   { fix w
  1116     assume "w = Ln z"
  1117     then have w: "Im w \<le> pi" "- pi < Im w"
  1118       using Im_Ln_le_pi [of z]  mpi_less_Im_Ln [of z]  assms
  1119       by auto
  1120     then have "0 < Im w \<and> Im w < pi \<longleftrightarrow> 0 < Im(exp w)"
  1121       using sin_gt_zero [of "- (Im w)"] sin_gt_zero [of "(Im w)"]
  1122       apply (auto simp: Im_exp zero_less_mult_iff)
  1123       using less_linear apply fastforce
  1124       using less_linear apply fastforce
  1125       done
  1126   }
  1127   then show ?thesis using assms
  1128     by auto
  1129 qed
  1130 
  1131 lemma Im_Ln_pos_le:
  1132   assumes "z \<noteq> 0"
  1133     shows "0 \<le> Im(Ln z) \<and> Im(Ln z) \<le> pi \<longleftrightarrow> 0 \<le> Im(z)"
  1134 proof -
  1135   { fix w
  1136     assume "w = Ln z"
  1137     then have w: "Im w \<le> pi" "- pi < Im w"
  1138       using Im_Ln_le_pi [of z]  mpi_less_Im_Ln [of z]  assms
  1139       by auto
  1140     then have "0 \<le> Im w \<and> Im w \<le> pi \<longleftrightarrow> 0 \<le> Im(exp w)"
  1141       using sin_ge_zero [of "- (Im w)"] sin_ge_zero [of "(Im w)"]
  1142       apply (auto simp: Im_exp zero_le_mult_iff sin_ge_zero)
  1143       apply (metis not_le not_less_iff_gr_or_eq pi_not_less_zero sin_eq_0_pi)
  1144       done }
  1145   then show ?thesis using assms
  1146     by auto
  1147 qed
  1148 
  1149 lemma Re_Ln_pos_lt_imp: "0 < Re(z) \<Longrightarrow> abs(Im(Ln z)) < pi/2"
  1150   by (metis Re_Ln_pos_lt less_irrefl zero_complex.simps(1))
  1151 
  1152 lemma Im_Ln_pos_lt_imp: "0 < Im(z) \<Longrightarrow> 0 < Im(Ln z) \<and> Im(Ln z) < pi"
  1153   by (metis Im_Ln_pos_lt not_le order_refl zero_complex.simps(2))
  1154 
  1155 lemma Im_Ln_eq_0: "z \<noteq> 0 \<Longrightarrow> (Im(Ln z) = 0 \<longleftrightarrow> 0 < Re(z) \<and> Im(z) = 0)"
  1156   by (metis exp_Ln Im_Ln_less_pi Im_Ln_pos_le Im_Ln_pos_lt Re_complex_of_real add.commute add.left_neutral
  1157        complex_eq exp_of_real le_less mult_zero_right norm_exp_eq_Re norm_le_zero_iff not_le of_real_0)
  1158 
  1159 lemma Im_Ln_eq_pi: "z \<noteq> 0 \<Longrightarrow> (Im(Ln z) = pi \<longleftrightarrow> Re(z) < 0 \<and> Im(z) = 0)"
  1160   by (metis Im_Ln_eq_0 Im_Ln_less_pi Im_Ln_pos_le Im_Ln_pos_lt add.right_neutral complex_eq mult_zero_right not_less not_less_iff_gr_or_eq of_real_0)
  1161 
  1162 
  1163 subsection\<open>More Properties of Ln\<close>
  1164 
  1165 lemma cnj_Ln: "(Im z = 0 \<Longrightarrow> 0 < Re z) \<Longrightarrow> cnj(Ln z) = Ln(cnj z)"
  1166   apply (cases "z=0", auto)
  1167   apply (rule exp_complex_eqI)
  1168   apply (auto simp: abs_if split: split_if_asm)
  1169   apply (metis Im_Ln_less_pi add_mono_thms_linordered_field(5) cnj.simps mult_2 neg_equal_0_iff_equal)
  1170   apply (metis complex_cnj_zero_iff diff_minus_eq_add diff_strict_mono minus_less_iff mpi_less_Im_Ln mult.commute mult_2_right)
  1171   by (metis exp_Ln exp_cnj)
  1172 
  1173 lemma Ln_inverse: "(Im(z) = 0 \<Longrightarrow> 0 < Re z) \<Longrightarrow> Ln(inverse z) = -(Ln z)"
  1174   apply (cases "z=0", auto)
  1175   apply (rule exp_complex_eqI)
  1176   using mpi_less_Im_Ln [of z] mpi_less_Im_Ln [of "inverse z"]
  1177   apply (auto simp: abs_if exp_minus split: split_if_asm)
  1178   apply (metis Im_Ln_less_pi Im_Ln_pos_le add_less_cancel_left add_strict_mono
  1179                inverse_inverse_eq inverse_zero le_less mult.commute mult_2_right)
  1180   done
  1181 
  1182 lemma Ln_minus1 [simp]: "Ln(-1) = ii * pi"
  1183   apply (rule exp_complex_eqI)
  1184   using Im_Ln_le_pi [of "-1"] mpi_less_Im_Ln [of "-1"] cis_conv_exp cis_pi
  1185   apply (auto simp: abs_if)
  1186   done
  1187 
  1188 lemma Ln_ii [simp]: "Ln ii = ii * of_real pi/2"
  1189   using Ln_exp [of "ii * (of_real pi/2)"]
  1190   unfolding exp_Euler
  1191   by simp
  1192 
  1193 lemma Ln_minus_ii [simp]: "Ln(-ii) = - (ii * pi/2)"
  1194 proof -
  1195   have  "Ln(-ii) = Ln(1/ii)"
  1196     by simp
  1197   also have "... = - (Ln ii)"
  1198     by (metis Ln_inverse ii.sel(2) inverse_eq_divide zero_neq_one)
  1199   also have "... = - (ii * pi/2)"
  1200     by simp
  1201   finally show ?thesis .
  1202 qed
  1203 
  1204 lemma Ln_times:
  1205   assumes "w \<noteq> 0" "z \<noteq> 0"
  1206     shows "Ln(w * z) =
  1207                 (if Im(Ln w + Ln z) \<le> -pi then
  1208                   (Ln(w) + Ln(z)) + ii * of_real(2*pi)
  1209                 else if Im(Ln w + Ln z) > pi then
  1210                   (Ln(w) + Ln(z)) - ii * of_real(2*pi)
  1211                 else Ln(w) + Ln(z))"
  1212   using pi_ge_zero Im_Ln_le_pi [of w] Im_Ln_le_pi [of z]
  1213   using assms mpi_less_Im_Ln [of w] mpi_less_Im_Ln [of z]
  1214   by (auto simp: of_real_numeral exp_add exp_diff sin_double cos_double exp_Euler intro!: Ln_unique)
  1215 
  1216 corollary Ln_times_simple:
  1217     "\<lbrakk>w \<noteq> 0; z \<noteq> 0; -pi < Im(Ln w) + Im(Ln z); Im(Ln w) + Im(Ln z) \<le> pi\<rbrakk>
  1218          \<Longrightarrow> Ln(w * z) = Ln(w) + Ln(z)"
  1219   by (simp add: Ln_times)
  1220 
  1221 corollary Ln_times_of_real:
  1222     "\<lbrakk>r > 0; z \<noteq> 0\<rbrakk> \<Longrightarrow> Ln(of_real r * z) = ln r + Ln(z)"
  1223   using mpi_less_Im_Ln Im_Ln_le_pi
  1224   by (force simp: Ln_times)
  1225 
  1226 corollary Ln_divide_of_real:
  1227     "\<lbrakk>r > 0; z \<noteq> 0\<rbrakk> \<Longrightarrow> Ln(z / of_real r) = Ln(z) - ln r"
  1228 using Ln_times_of_real [of "inverse r" z]
  1229 by (simp add: ln_inverse Ln_of_real mult.commute divide_inverse of_real_inverse [symmetric] 
  1230          del: of_real_inverse)
  1231 
  1232 lemma Ln_minus:
  1233   assumes "z \<noteq> 0"
  1234     shows "Ln(-z) = (if Im(z) \<le> 0 \<and> ~(Re(z) < 0 \<and> Im(z) = 0)
  1235                      then Ln(z) + ii * pi
  1236                      else Ln(z) - ii * pi)" (is "_ = ?rhs")
  1237   using Im_Ln_le_pi [of z] mpi_less_Im_Ln [of z] assms
  1238         Im_Ln_eq_pi [of z] Im_Ln_pos_lt [of z]
  1239     by (auto simp: of_real_numeral exp_add exp_diff exp_Euler intro!: Ln_unique)
  1240 
  1241 lemma Ln_inverse_if:
  1242   assumes "z \<noteq> 0"
  1243     shows "Ln (inverse z) =
  1244             (if (Im(z) = 0 \<longrightarrow> 0 < Re z)
  1245              then -(Ln z)
  1246              else -(Ln z) + \<i> * 2 * complex_of_real pi)"
  1247 proof (cases "(Im(z) = 0 \<longrightarrow> 0 < Re z)")
  1248   case True then show ?thesis
  1249     by (simp add: Ln_inverse)
  1250 next
  1251   case False
  1252   then have z: "Im z = 0" "Re z < 0"
  1253     using assms
  1254     apply auto
  1255     by (metis cnj.code complex_cnj_cnj not_less_iff_gr_or_eq zero_complex.simps(1) zero_complex.simps(2))
  1256   have "Ln(inverse z) = Ln(- (inverse (-z)))"
  1257     by simp
  1258   also have "... = Ln (inverse (-z)) + \<i> * complex_of_real pi"
  1259     using assms z
  1260     apply (simp add: Ln_minus)
  1261     apply (simp add: field_simps)
  1262     done
  1263   also have "... = - Ln (- z) + \<i> * complex_of_real pi"
  1264     apply (subst Ln_inverse)
  1265     using z assms by auto
  1266   also have "... = - (Ln z) + \<i> * 2 * complex_of_real pi"
  1267     apply (subst Ln_minus [OF assms])
  1268     using assms z
  1269     apply simp
  1270     done
  1271   finally show ?thesis
  1272     using assms z
  1273     by simp
  1274 qed
  1275 
  1276 lemma Ln_times_ii:
  1277   assumes "z \<noteq> 0"
  1278     shows  "Ln(ii * z) = (if 0 \<le> Re(z) | Im(z) < 0
  1279                           then Ln(z) + ii * of_real pi/2
  1280                           else Ln(z) - ii * of_real(3 * pi/2))"
  1281   using Im_Ln_le_pi [of z] mpi_less_Im_Ln [of z] assms
  1282         Im_Ln_eq_pi [of z] Im_Ln_pos_lt [of z] Re_Ln_pos_le [of z]
  1283   by (auto simp: of_real_numeral Ln_times)
  1284 
  1285 
  1286 subsection\<open>Relation between Ln and Arg, and hence continuity of Arg\<close>
  1287 
  1288 lemma Arg_Ln: 
  1289   assumes "0 < Arg z" shows "Arg z = Im(Ln(-z)) + pi"
  1290 proof (cases "z = 0")
  1291   case True
  1292   with assms show ?thesis
  1293     by simp
  1294 next
  1295   case False
  1296   then have "z / of_real(norm z) = exp(ii * of_real(Arg z))"
  1297     using Arg [of z]
  1298     by (metis abs_norm_cancel nonzero_mult_divide_cancel_left norm_of_real zero_less_norm_iff)
  1299   then have "- z / of_real(norm z) = exp (\<i> * (of_real (Arg z) - pi))"
  1300     using cis_conv_exp cis_pi
  1301     by (auto simp: exp_diff algebra_simps)
  1302   then have "ln (- z / of_real(norm z)) = ln (exp (\<i> * (of_real (Arg z) - pi)))"
  1303     by simp
  1304   also have "... = \<i> * (of_real(Arg z) - pi)"
  1305     using Arg [of z] assms pi_not_less_zero
  1306     by auto
  1307   finally have "Arg z =  Im (Ln (- z / of_real (cmod z))) + pi"
  1308     by simp
  1309   also have "... = Im (Ln (-z) - ln (cmod z)) + pi"
  1310     by (metis diff_0_right minus_diff_eq zero_less_norm_iff Ln_divide_of_real False)
  1311   also have "... = Im (Ln (-z)) + pi"
  1312     by simp
  1313   finally show ?thesis .
  1314 qed
  1315 
  1316 lemma continuous_at_Arg: 
  1317   assumes "z \<in> \<real> \<Longrightarrow> Re z < 0"
  1318     shows "continuous (at z) Arg"
  1319 proof -
  1320   have *: "isCont (\<lambda>z. Im (Ln (- z)) + pi) z"
  1321     by (rule Complex.isCont_Im isCont_Ln' continuous_intros | simp add: assms complex_is_Real_iff)+
  1322   then show ?thesis
  1323     apply (simp add: continuous_at)
  1324     apply (rule Lim_transform_within_open [of "-{z. z \<in> \<real> & 0 \<le> Re z}" _ "\<lambda>z. Im(Ln(-z)) + pi"])
  1325     apply (simp add: closed_def [symmetric] closed_Collect_conj closed_complex_Reals closed_halfspace_Re_ge)
  1326     apply (simp_all add: assms not_le Arg_Ln [OF Arg_gt_0])
  1327     done
  1328 qed
  1329 
  1330 text\<open>Relation between Arg and arctangent in upper halfplane\<close>
  1331 lemma Arg_arctan_upperhalf: 
  1332   assumes "0 < Im z"
  1333     shows "Arg z = pi/2 - arctan(Re z / Im z)"
  1334 proof (cases "z = 0")
  1335   case True with assms show ?thesis
  1336     by simp
  1337 next
  1338   case False
  1339   show ?thesis
  1340     apply (rule Arg_unique [of "norm z"])
  1341     using False assms arctan [of "Re z / Im z"] pi_ge_two pi_half_less_two
  1342     apply (auto simp: exp_Euler cos_diff sin_diff)
  1343     using norm_complex_def [of z, symmetric]
  1344     apply (simp add: of_real_numeral sin_of_real cos_of_real sin_arctan cos_arctan field_simps real_sqrt_divide)
  1345     apply (metis complex_eq mult.assoc ring_class.ring_distribs(2))
  1346     done
  1347 qed
  1348 
  1349 lemma Arg_eq_Im_Ln: 
  1350   assumes "0 \<le> Im z" "0 < Re z" 
  1351     shows "Arg z = Im (Ln z)"
  1352 proof (cases "z = 0 \<or> Im z = 0")
  1353   case True then show ?thesis
  1354     using assms Arg_eq_0 complex_is_Real_iff  
  1355     apply auto
  1356     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))
  1357 next
  1358   case False 
  1359   then have "Arg z > 0"
  1360     using Arg_gt_0 complex_is_Real_iff by blast
  1361   then show ?thesis
  1362     using assms False 
  1363     by (subst Arg_Ln) (auto simp: Ln_minus)
  1364 qed
  1365 
  1366 lemma continuous_within_upperhalf_Arg: 
  1367   assumes "z \<noteq> 0"
  1368     shows "continuous (at z within {z. 0 \<le> Im z}) Arg"
  1369 proof (cases "z \<in> \<real> & 0 \<le> Re z")
  1370   case False then show ?thesis
  1371     using continuous_at_Arg continuous_at_imp_continuous_within by auto
  1372 next
  1373   case True
  1374   then have z: "z \<in> \<real>" "0 < Re z"
  1375     using assms  by (auto simp: complex_is_Real_iff complex_neq_0)
  1376   then have [simp]: "Arg z = 0" "Im (Ln z) = 0"
  1377     by (auto simp: Arg_eq_0 Im_Ln_eq_0 assms complex_is_Real_iff)
  1378   show ?thesis  
  1379   proof (clarsimp simp add: continuous_within Lim_within dist_norm)
  1380     fix e::real
  1381     assume "0 < e"
  1382     moreover have "continuous (at z) (\<lambda>x. Im (Ln x))"
  1383       using z  by (rule continuous_intros | simp)
  1384     ultimately
  1385     obtain d where d: "d>0" "\<And>x. x \<noteq> z \<Longrightarrow> cmod (x - z) < d \<Longrightarrow> \<bar>Im (Ln x)\<bar> < e"
  1386       by (auto simp: continuous_within Lim_within dist_norm)
  1387     { fix x
  1388       assume "cmod (x - z) < Re z / 2"
  1389       then have "\<bar>Re x - Re z\<bar> < Re z / 2"
  1390         by (metis le_less_trans abs_Re_le_cmod minus_complex.simps(1))
  1391       then have "0 < Re x"
  1392         using z by linarith
  1393     }
  1394     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"
  1395       apply (rule_tac x="min d (Re z / 2)" in exI)
  1396       using z d
  1397       apply (auto simp: Arg_eq_Im_Ln)
  1398       done
  1399   qed
  1400 qed
  1401 
  1402 lemma continuous_on_upperhalf_Arg: "continuous_on ({z. 0 \<le> Im z} - {0}) Arg"
  1403   apply (auto simp: continuous_on_eq_continuous_within)
  1404   by (metis Diff_subset continuous_within_subset continuous_within_upperhalf_Arg)
  1405 
  1406 lemma open_Arg_less_Int: 
  1407   assumes "0 \<le> s" "t \<le> 2*pi"
  1408     shows "open ({y. s < Arg y} \<inter> {y. Arg y < t})"
  1409 proof -
  1410   have 1: "continuous_on (UNIV - {z \<in> \<real>. 0 \<le> Re z}) Arg"
  1411     using continuous_at_Arg continuous_at_imp_continuous_within 
  1412     by (auto simp: continuous_on_eq_continuous_within set_diff_eq)
  1413   have 2: "open (UNIV - {z \<in> \<real>. 0 \<le> Re z})"
  1414     by (simp add: closed_Collect_conj closed_complex_Reals closed_halfspace_Re_ge open_Diff)
  1415   have "open ({z. s < z} \<inter> {z. z < t})"
  1416     using open_lessThan [of t] open_greaterThan [of s]
  1417     by (metis greaterThan_def lessThan_def open_Int)
  1418   moreover have "{y. s < Arg y} \<inter> {y. Arg y < t} \<subseteq> - {z \<in> \<real>. 0 \<le> Re z}"
  1419     using assms
  1420     by (auto simp: Arg_real)
  1421   ultimately show ?thesis
  1422     using continuous_imp_open_vimage [OF 1 2, of  "{z. Re z > s} \<inter> {z. Re z < t}"] 
  1423     by auto
  1424 qed
  1425 
  1426 lemma open_Arg_gt: "open {z. t < Arg z}"
  1427 proof (cases "t < 0")
  1428   case True then have "{z. t < Arg z} = UNIV"
  1429     using Arg_ge_0 less_le_trans by auto
  1430   then show ?thesis
  1431     by simp
  1432 next
  1433   case False then show ?thesis
  1434     using open_Arg_less_Int [of t "2*pi"] Arg_lt_2pi
  1435     by auto
  1436 qed
  1437 
  1438 lemma closed_Arg_le: "closed {z. Arg z \<le> t}"
  1439   using open_Arg_gt [of t]
  1440   by (simp add: closed_def Set.Collect_neg_eq [symmetric] not_le)
  1441 
  1442 subsection\<open>Complex Powers\<close>
  1443 
  1444 lemma powr_to_1 [simp]: "z powr 1 = (z::complex)"
  1445   by (simp add: powr_def)
  1446 
  1447 lemma powr_nat:
  1448   fixes n::nat and z::complex shows "z powr n = (if z = 0 then 0 else z^n)"
  1449   by (simp add: exp_of_nat_mult powr_def)
  1450 
  1451 lemma powr_add_complex:
  1452   fixes w::complex shows "w powr (z1 + z2) = w powr z1 * w powr z2"
  1453   by (simp add: powr_def algebra_simps exp_add)
  1454 
  1455 lemma powr_minus_complex:
  1456   fixes w::complex shows  "w powr (-z) = inverse(w powr z)"
  1457   by (simp add: powr_def exp_minus)
  1458 
  1459 lemma powr_diff_complex:
  1460   fixes w::complex shows  "w powr (z1 - z2) = w powr z1 / w powr z2"
  1461   by (simp add: powr_def algebra_simps exp_diff)
  1462 
  1463 lemma norm_powr_real: "w \<in> \<real> \<Longrightarrow> 0 < Re w \<Longrightarrow> norm(w powr z) = exp(Re z * ln(Re w))"
  1464   apply (simp add: powr_def)
  1465   using Im_Ln_eq_0 complex_is_Real_iff norm_complex_def
  1466   by auto
  1467 
  1468 lemma powr_real_real:
  1469     "\<lbrakk>w \<in> \<real>; z \<in> \<real>; 0 < Re w\<rbrakk> \<Longrightarrow> w powr z = exp(Re z * ln(Re w))"
  1470   apply (simp add: powr_def)
  1471   by (metis complex_eq complex_is_Real_iff diff_0 diff_0_right diff_minus_eq_add exp_ln exp_not_eq_zero
  1472        exp_of_real Ln_of_real mult_zero_right of_real_0 of_real_mult)
  1473 
  1474 lemma powr_of_real:
  1475   fixes x::real and y::real
  1476   shows "0 < x \<Longrightarrow> of_real x powr (of_real y::complex) = of_real (x powr y)"
  1477   by (simp add: powr_def) (metis exp_of_real of_real_mult Ln_of_real)
  1478 
  1479 lemma norm_powr_real_mono:
  1480     "\<lbrakk>w \<in> \<real>; 1 < Re w\<rbrakk>
  1481      \<Longrightarrow> cmod(w powr z1) \<le> cmod(w powr z2) \<longleftrightarrow> Re z1 \<le> Re z2"
  1482   by (auto simp: powr_def algebra_simps Reals_def Ln_of_real)
  1483 
  1484 lemma powr_times_real:
  1485     "\<lbrakk>x \<in> \<real>; y \<in> \<real>; 0 \<le> Re x; 0 \<le> Re y\<rbrakk>
  1486            \<Longrightarrow> (x * y) powr z = x powr z * y powr z"
  1487   by (auto simp: Reals_def powr_def Ln_times exp_add algebra_simps less_eq_real_def Ln_of_real)
  1488 
  1489 lemma has_field_derivative_powr:
  1490     "(Im z = 0 \<Longrightarrow> 0 < Re z)
  1491      \<Longrightarrow> ((\<lambda>z. z powr s) has_field_derivative (s * z powr (s - 1))) (at z)"
  1492   apply (cases "z=0", auto)
  1493   apply (simp add: powr_def)
  1494   apply (rule DERIV_transform_at [where d = "norm z" and f = "\<lambda>z. exp (s * Ln z)"])
  1495   apply (auto simp: dist_complex_def)
  1496   apply (intro derivative_eq_intros | simp add: assms)+
  1497   apply (simp add: field_simps exp_diff)
  1498   done
  1499 
  1500 lemma has_field_derivative_powr_right:
  1501     "w \<noteq> 0 \<Longrightarrow> ((\<lambda>z. w powr z) has_field_derivative Ln w * w powr z) (at z)"
  1502   apply (simp add: powr_def)
  1503   apply (intro derivative_eq_intros | simp add: assms)+
  1504   done
  1505 
  1506 lemma complex_differentiable_powr_right:
  1507     "w \<noteq> 0 \<Longrightarrow> (\<lambda>z. w powr z) complex_differentiable (at z)"
  1508 using complex_differentiable_def has_field_derivative_powr_right by blast
  1509 
  1510 lemma holomorphic_on_powr_right:
  1511     "f holomorphic_on s \<Longrightarrow> w \<noteq> 0 \<Longrightarrow> (\<lambda>z. w powr (f z)) holomorphic_on s"
  1512     unfolding holomorphic_on_def
  1513     using DERIV_chain' complex_differentiable_def has_field_derivative_powr_right by fastforce
  1514 
  1515 lemma norm_powr_real_powr:
  1516   "w \<in> \<real> \<Longrightarrow> 0 < Re w \<Longrightarrow> norm(w powr z) = Re w powr Re z"
  1517   by (auto simp add: norm_powr_real powr_def Im_Ln_eq_0 complex_is_Real_iff in_Reals_norm)
  1518 
  1519 
  1520 subsection\<open>Some Limits involving Logarithms\<close>
  1521         
  1522 lemma lim_Ln_over_power:
  1523   fixes s::complex
  1524   assumes "0 < Re s"
  1525     shows "((\<lambda>n. Ln n / (n powr s)) ---> 0) sequentially"
  1526 proof (simp add: lim_sequentially dist_norm, clarify)
  1527   fix e::real 
  1528   assume e: "0 < e"
  1529   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"
  1530   proof (rule_tac x="2/(e * (Re s)\<^sup>2)" in exI, safe)
  1531     show "0 < 2 / (e * (Re s)\<^sup>2)"
  1532       using e assms by (simp add: field_simps)
  1533   next
  1534     fix x::real
  1535     assume x: "2 / (e * (Re s)\<^sup>2) \<le> x"
  1536     then have "x>0"
  1537     using e assms
  1538       by (metis less_le_trans mult_eq_0_iff mult_pos_pos pos_less_divide_eq power2_eq_square
  1539                 zero_less_numeral)
  1540     then show "0 < e * 2 + (e * Re s * 2 - 2) * x + e * (Re s)\<^sup>2 * x\<^sup>2"
  1541       using e assms x
  1542       apply (auto simp: field_simps)
  1543       apply (rule_tac y = "e * (x\<^sup>2 * (Re s)\<^sup>2)" in le_less_trans)
  1544       apply (auto simp: power2_eq_square field_simps add_pos_pos)
  1545       done
  1546   qed
  1547   then have "\<exists>xo>0. \<forall>x\<ge>xo. x / e < 1 + (Re s * x) + (1/2) * (Re s * x)^2"
  1548     using e  by (simp add: field_simps)
  1549   then have "\<exists>xo>0. \<forall>x\<ge>xo. x / e < exp (Re s * x)"
  1550     using assms
  1551     by (force intro: less_le_trans [OF _ exp_lower_taylor_quadratic])
  1552   then have "\<exists>xo>0. \<forall>x\<ge>xo. x < e * exp (Re s * x)"
  1553     using e   by (auto simp: field_simps)
  1554   with e show "\<exists>no. \<forall>n\<ge>no. norm (Ln (of_nat n) / of_nat n powr s) < e"
  1555     apply (auto simp: norm_divide norm_powr_real divide_simps)
  1556     apply (rule_tac x="nat (ceiling (exp xo))" in exI)
  1557     apply clarify
  1558     apply (drule_tac x="ln n" in spec)
  1559     apply (auto simp: real_of_nat_def exp_less_mono nat_ceiling_le_eq not_le)
  1560     apply (metis exp_less_mono exp_ln not_le of_nat_0_less_iff)
  1561     done
  1562 qed
  1563 
  1564 lemma lim_Ln_over_n: "((\<lambda>n. Ln(of_nat n) / of_nat n) ---> 0) sequentially"
  1565   using lim_Ln_over_power [of 1]
  1566   by simp
  1567 
  1568 lemma Ln_Reals_eq: "x \<in> \<real> \<Longrightarrow> Re x > 0 \<Longrightarrow> Ln x = of_real (ln (Re x))"
  1569   using Ln_of_real by force
  1570 
  1571 lemma powr_Reals_eq: "x \<in> \<real> \<Longrightarrow> Re x > 0 \<Longrightarrow> x powr complex_of_real y = of_real (x powr y)"
  1572   by (simp add: powr_of_real)
  1573 
  1574 lemma lim_ln_over_power:
  1575   fixes s :: real
  1576   assumes "0 < s"
  1577     shows "((\<lambda>n. ln n / (n powr s)) ---> 0) sequentially"
  1578   using lim_Ln_over_power [of "of_real s", THEN filterlim_sequentially_Suc [THEN iffD2]] assms
  1579   apply (subst filterlim_sequentially_Suc [symmetric])
  1580   apply (simp add: lim_sequentially dist_norm
  1581           Ln_Reals_eq norm_powr_real_powr norm_divide real_of_nat_def)
  1582   done
  1583 
  1584 lemma lim_ln_over_n: "((\<lambda>n. ln(real_of_nat n) / of_nat n) ---> 0) sequentially"
  1585   using lim_ln_over_power [of 1, THEN filterlim_sequentially_Suc [THEN iffD2]]
  1586   apply (subst filterlim_sequentially_Suc [symmetric])
  1587   apply (simp add: lim_sequentially dist_norm real_of_nat_def)
  1588   done
  1589 
  1590 lemma lim_1_over_complex_power:
  1591   assumes "0 < Re s"
  1592     shows "((\<lambda>n. 1 / (of_nat n powr s)) ---> 0) sequentially"
  1593 proof -
  1594   have "\<forall>n>0. 3 \<le> n \<longrightarrow> 1 \<le> ln (real_of_nat n)"
  1595     using ln3_gt_1
  1596     by (force intro: order_trans [of _ "ln 3"] ln3_gt_1)
  1597   moreover have "(\<lambda>n. cmod (Ln (of_nat n) / of_nat n powr s)) ----> 0"
  1598     using lim_Ln_over_power [OF assms]
  1599     by (metis tendsto_norm_zero_iff)
  1600   ultimately show ?thesis
  1601     apply (auto intro!: Lim_null_comparison [where g = "\<lambda>n. norm (Ln(of_nat n) / of_nat n powr s)"])
  1602     apply (auto simp: norm_divide divide_simps eventually_sequentially)
  1603     done
  1604 qed
  1605 
  1606 lemma lim_1_over_real_power:
  1607   fixes s :: real
  1608   assumes "0 < s"
  1609     shows "((\<lambda>n. 1 / (of_nat n powr s)) ---> 0) sequentially"
  1610   using lim_1_over_complex_power [of "of_real s", THEN filterlim_sequentially_Suc [THEN iffD2]] assms
  1611   apply (subst filterlim_sequentially_Suc [symmetric])
  1612   apply (simp add: lim_sequentially dist_norm)
  1613   apply (simp add: Ln_Reals_eq norm_powr_real_powr norm_divide real_of_nat_def)
  1614   done
  1615 
  1616 lemma lim_1_over_Ln: "((\<lambda>n. 1 / Ln(of_nat n)) ---> 0) sequentially"
  1617 proof (clarsimp simp add: lim_sequentially dist_norm norm_divide divide_simps)
  1618   fix r::real
  1619   assume "0 < r"
  1620   have ir: "inverse (exp (inverse r)) > 0"
  1621     by simp
  1622   obtain n where n: "1 < of_nat n * inverse (exp (inverse r))"
  1623     using ex_less_of_nat_mult [of _ 1, OF ir]
  1624     by auto
  1625   then have "exp (inverse r) < of_nat n"
  1626     by (simp add: divide_simps)
  1627   then have "ln (exp (inverse r)) < ln (of_nat n)"
  1628     by (metis exp_gt_zero less_trans ln_exp ln_less_cancel_iff)
  1629   with \<open>0 < r\<close> have "1 < r * ln (real_of_nat n)"
  1630     by (simp add: field_simps)
  1631   moreover have "n > 0" using n
  1632     using neq0_conv by fastforce
  1633   ultimately show "\<exists>no. \<forall>n. Ln (of_nat n) \<noteq> 0 \<longrightarrow> no \<le> n \<longrightarrow> 1 < r * cmod (Ln (of_nat n))"
  1634     using n \<open>0 < r\<close>
  1635     apply (rule_tac x=n in exI)
  1636     apply (auto simp: divide_simps)
  1637     apply (erule less_le_trans, auto)
  1638     done
  1639 qed
  1640 
  1641 lemma lim_1_over_ln: "((\<lambda>n. 1 / ln(real_of_nat n)) ---> 0) sequentially"
  1642   using lim_1_over_Ln [THEN filterlim_sequentially_Suc [THEN iffD2]] assms
  1643   apply (subst filterlim_sequentially_Suc [symmetric])
  1644   apply (simp add: lim_sequentially dist_norm)
  1645   apply (simp add: Ln_Reals_eq norm_powr_real_powr norm_divide real_of_nat_def)
  1646   done
  1647 
  1648 
  1649 subsection\<open>Relation between Square Root and exp/ln, hence its derivative\<close>
  1650 
  1651 lemma csqrt_exp_Ln:
  1652   assumes "z \<noteq> 0"
  1653     shows "csqrt z = exp(Ln(z) / 2)"
  1654 proof -
  1655   have "(exp (Ln z / 2))\<^sup>2 = (exp (Ln z))"
  1656     by (metis exp_double nonzero_mult_divide_cancel_left times_divide_eq_right zero_neq_numeral)
  1657   also have "... = z"
  1658     using assms exp_Ln by blast
  1659   finally have "csqrt z = csqrt ((exp (Ln z / 2))\<^sup>2)"
  1660     by simp
  1661   also have "... = exp (Ln z / 2)"
  1662     apply (subst csqrt_square)
  1663     using cos_gt_zero_pi [of "(Im (Ln z) / 2)"] Im_Ln_le_pi mpi_less_Im_Ln assms
  1664     apply (auto simp: Re_exp Im_exp zero_less_mult_iff zero_le_mult_iff, fastforce+)
  1665     done
  1666   finally show ?thesis using assms csqrt_square
  1667     by simp
  1668 qed
  1669 
  1670 lemma csqrt_inverse:
  1671   assumes "Im(z) = 0 \<Longrightarrow> 0 < Re z"
  1672     shows "csqrt (inverse z) = inverse (csqrt z)"
  1673 proof (cases "z=0", simp)
  1674   assume "z \<noteq> 0 "
  1675   then show ?thesis
  1676     using assms
  1677     by (simp add: csqrt_exp_Ln Ln_inverse exp_minus)
  1678 qed
  1679 
  1680 lemma cnj_csqrt:
  1681   assumes "Im z = 0 \<Longrightarrow> 0 \<le> Re(z)"
  1682     shows "cnj(csqrt z) = csqrt(cnj z)"
  1683 proof (cases "z=0", simp)
  1684   assume z: "z \<noteq> 0"
  1685   then have "Im z = 0 \<Longrightarrow> 0 < Re(z)"
  1686     using assms cnj.code complex_cnj_zero_iff by fastforce
  1687   then show ?thesis
  1688    using z by (simp add: csqrt_exp_Ln cnj_Ln exp_cnj)
  1689 qed
  1690 
  1691 lemma has_field_derivative_csqrt:
  1692   assumes "Im z = 0 \<Longrightarrow> 0 < Re(z)"
  1693     shows "(csqrt has_field_derivative inverse(2 * csqrt z)) (at z)"
  1694 proof -
  1695   have z: "z \<noteq> 0"
  1696     using assms by auto
  1697   then have *: "inverse z = inverse (2*z) * 2"
  1698     by (simp add: divide_simps)
  1699   show ?thesis
  1700     apply (rule DERIV_transform_at [where f = "\<lambda>z. exp(Ln(z) / 2)" and d = "norm z"])
  1701     apply (intro derivative_eq_intros | simp add: assms)+
  1702     apply (rule *)
  1703     using z
  1704     apply (auto simp: field_simps csqrt_exp_Ln [symmetric])
  1705     apply (metis power2_csqrt power2_eq_square)
  1706     apply (metis csqrt_exp_Ln dist_0_norm less_irrefl)
  1707     done
  1708 qed
  1709 
  1710 lemma complex_differentiable_at_csqrt:
  1711     "(Im z = 0 \<Longrightarrow> 0 < Re(z)) \<Longrightarrow> csqrt complex_differentiable at z"
  1712   using complex_differentiable_def has_field_derivative_csqrt by blast
  1713 
  1714 lemma complex_differentiable_within_csqrt:
  1715     "(Im z = 0 \<Longrightarrow> 0 < Re(z)) \<Longrightarrow> csqrt complex_differentiable (at z within s)"
  1716   using complex_differentiable_at_csqrt complex_differentiable_within_subset by blast
  1717 
  1718 lemma continuous_at_csqrt:
  1719     "(Im z = 0 \<Longrightarrow> 0 < Re(z)) \<Longrightarrow> continuous (at z) csqrt"
  1720   by (simp add: complex_differentiable_within_csqrt complex_differentiable_imp_continuous_at)
  1721 
  1722 corollary isCont_csqrt' [simp]:
  1723    "\<lbrakk>isCont f z; Im(f z) = 0 \<Longrightarrow> 0 < Re(f z)\<rbrakk> \<Longrightarrow> isCont (\<lambda>x. csqrt (f x)) z"
  1724   by (blast intro: isCont_o2 [OF _ continuous_at_csqrt])
  1725 
  1726 lemma continuous_within_csqrt:
  1727     "(Im z = 0 \<Longrightarrow> 0 < Re(z)) \<Longrightarrow> continuous (at z within s) csqrt"
  1728   by (simp add: complex_differentiable_imp_continuous_at complex_differentiable_within_csqrt)
  1729 
  1730 lemma continuous_on_csqrt [continuous_intros]:
  1731     "(\<And>z. z \<in> s \<and> Im z = 0 \<Longrightarrow> 0 < Re(z)) \<Longrightarrow> continuous_on s csqrt"
  1732   by (simp add: continuous_at_imp_continuous_on continuous_within_csqrt)
  1733 
  1734 lemma holomorphic_on_csqrt:
  1735     "(\<And>z. z \<in> s \<and> Im z = 0 \<Longrightarrow> 0 < Re(z)) \<Longrightarrow> csqrt holomorphic_on s"
  1736   by (simp add: complex_differentiable_within_csqrt holomorphic_on_def)
  1737 
  1738 lemma continuous_within_closed_nontrivial:
  1739     "closed s \<Longrightarrow> a \<notin> s ==> continuous (at a within s) f"
  1740   using open_Compl
  1741   by (force simp add: continuous_def eventually_at_topological filterlim_iff open_Collect_neg)
  1742 
  1743 lemma continuous_within_csqrt_posreal:
  1744     "continuous (at z within (\<real> \<inter> {w. 0 \<le> Re(w)})) csqrt"
  1745 proof (cases "Im z = 0 --> 0 < Re(z)")
  1746   case True then show ?thesis
  1747     by (blast intro: continuous_within_csqrt)
  1748 next
  1749   case False
  1750   then have "Im z = 0" "Re z < 0 \<or> z = 0"
  1751     using False cnj.code complex_cnj_zero_iff by auto force
  1752   then show ?thesis
  1753     apply (auto simp: continuous_within_closed_nontrivial [OF closed_Real_halfspace_Re_ge])
  1754     apply (auto simp: continuous_within_eps_delta norm_conv_dist [symmetric])
  1755     apply (rule_tac x="e^2" in exI)
  1756     apply (auto simp: Reals_def)
  1757 by (metis linear not_less real_sqrt_less_iff real_sqrt_pow2_iff real_sqrt_power)
  1758 qed
  1759 
  1760 subsection\<open>Complex arctangent\<close>
  1761 
  1762 text\<open>branch cut gives standard bounds in real case.\<close>
  1763 
  1764 definition Arctan :: "complex \<Rightarrow> complex" where
  1765     "Arctan \<equiv> \<lambda>z. (\<i>/2) * Ln((1 - \<i>*z) / (1 + \<i>*z))"
  1766 
  1767 lemma Arctan_0 [simp]: "Arctan 0 = 0"
  1768   by (simp add: Arctan_def)
  1769 
  1770 lemma Im_complex_div_lemma: "Im((1 - \<i>*z) / (1 + \<i>*z)) = 0 \<longleftrightarrow> Re z = 0"
  1771   by (auto simp: Im_complex_div_eq_0 algebra_simps)
  1772 
  1773 lemma Re_complex_div_lemma: "0 < Re((1 - \<i>*z) / (1 + \<i>*z)) \<longleftrightarrow> norm z < 1"
  1774   by (simp add: Re_complex_div_gt_0 algebra_simps cmod_def power2_eq_square)
  1775 
  1776 lemma tan_Arctan:
  1777   assumes "z\<^sup>2 \<noteq> -1"
  1778     shows [simp]:"tan(Arctan z) = z"
  1779 proof -
  1780   have "1 + \<i>*z \<noteq> 0"
  1781     by (metis assms complex_i_mult_minus i_squared minus_unique power2_eq_square power2_minus)
  1782   moreover
  1783   have "1 - \<i>*z \<noteq> 0"
  1784     by (metis assms complex_i_mult_minus i_squared power2_eq_square power2_minus right_minus_eq)
  1785   ultimately
  1786   show ?thesis
  1787     by (simp add: Arctan_def tan_def sin_exp_eq cos_exp_eq exp_minus csqrt_exp_Ln [symmetric]
  1788                   divide_simps power2_eq_square [symmetric])
  1789 qed
  1790 
  1791 lemma Arctan_tan [simp]:
  1792   assumes "\<bar>Re z\<bar> < pi/2"
  1793     shows "Arctan(tan z) = z"
  1794 proof -
  1795   have ge_pi2: "\<And>n::int. abs (of_int (2*n + 1) * pi/2) \<ge> pi/2"
  1796     by (case_tac n rule: int_cases) (auto simp: abs_mult)
  1797   have "exp (\<i>*z)*exp (\<i>*z) = -1 \<longleftrightarrow> exp (2*\<i>*z) = -1"
  1798     by (metis distrib_right exp_add mult_2)
  1799   also have "... \<longleftrightarrow> exp (2*\<i>*z) = exp (\<i>*pi)"
  1800     using cis_conv_exp cis_pi by auto
  1801   also have "... \<longleftrightarrow> exp (2*\<i>*z - \<i>*pi) = 1"
  1802     by (metis (no_types) diff_add_cancel diff_minus_eq_add exp_add exp_minus_inverse mult.commute)
  1803   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)"
  1804     by (simp add: exp_eq_1)
  1805   also have "... \<longleftrightarrow> Im z = 0 \<and> (\<exists>n::int. 2 * Re z = of_int (2*n + 1) * pi)"
  1806     by (simp add: algebra_simps)
  1807   also have "... \<longleftrightarrow> False"
  1808     using assms ge_pi2
  1809     apply (auto simp: algebra_simps)
  1810     by (metis abs_mult_pos not_less not_real_of_nat_less_zero real_of_nat_numeral)
  1811   finally have *: "exp (\<i>*z)*exp (\<i>*z) + 1 \<noteq> 0"
  1812     by (auto simp: add.commute minus_unique)
  1813   show ?thesis
  1814     using assms *
  1815     apply (simp add: Arctan_def tan_def sin_exp_eq cos_exp_eq exp_minus divide_simps
  1816                      ii_times_eq_iff power2_eq_square [symmetric])
  1817     apply (rule Ln_unique)
  1818     apply (auto simp: divide_simps exp_minus)
  1819     apply (simp add: algebra_simps exp_double [symmetric])
  1820     done
  1821 qed
  1822 
  1823 lemma
  1824   assumes "Re z = 0 \<Longrightarrow> abs(Im z) < 1"
  1825   shows Re_Arctan_bounds: "abs(Re(Arctan z)) < pi/2"
  1826     and has_field_derivative_Arctan: "(Arctan has_field_derivative inverse(1 + z\<^sup>2)) (at z)"
  1827 proof -
  1828   have nz0: "1 + \<i>*z \<noteq> 0"
  1829     using assms
  1830     by (metis abs_one complex_i_mult_minus diff_0_right diff_minus_eq_add ii.simps(1) ii.simps(2)
  1831               less_irrefl minus_diff_eq mult.right_neutral right_minus_eq)
  1832   have "z \<noteq> -\<i>" using assms
  1833     by auto
  1834   then have zz: "1 + z * z \<noteq> 0"
  1835     by (metis abs_one assms i_squared ii.simps less_irrefl minus_unique square_eq_iff)
  1836   have nz1: "1 - \<i>*z \<noteq> 0"
  1837     using assms by (force simp add: ii_times_eq_iff)
  1838   have nz2: "inverse (1 + \<i>*z) \<noteq> 0"
  1839     using assms
  1840     by (metis Im_complex_div_lemma Re_complex_div_lemma cmod_eq_Im divide_complex_def
  1841               less_irrefl mult_zero_right zero_complex.simps(1) zero_complex.simps(2))
  1842   have nzi: "((1 - \<i>*z) * inverse (1 + \<i>*z)) \<noteq> 0"
  1843     using nz1 nz2 by auto
  1844   have *: "Im ((1 - \<i>*z) / (1 + \<i>*z)) = 0 \<Longrightarrow> 0 < Re ((1 - \<i>*z) / (1 + \<i>*z))"
  1845     apply (simp add: divide_complex_def)
  1846     apply (simp add: divide_simps split: split_if_asm)
  1847     using assms
  1848     apply (auto simp: algebra_simps abs_square_less_1 [unfolded power2_eq_square])
  1849     done
  1850   show "abs(Re(Arctan z)) < pi/2"
  1851     unfolding Arctan_def divide_complex_def
  1852     using mpi_less_Im_Ln [OF nzi]
  1853     by (auto simp: abs_if intro: Im_Ln_less_pi * [unfolded divide_complex_def])
  1854   show "(Arctan has_field_derivative inverse(1 + z\<^sup>2)) (at z)"
  1855     unfolding Arctan_def scaleR_conv_of_real
  1856     apply (rule DERIV_cong)
  1857     apply (intro derivative_eq_intros | simp add: nz0 *)+
  1858     using nz0 nz1 zz
  1859     apply (simp add: divide_simps power2_eq_square)
  1860     apply (auto simp: algebra_simps)
  1861     done
  1862 qed
  1863 
  1864 lemma complex_differentiable_at_Arctan: "(Re z = 0 \<Longrightarrow> abs(Im z) < 1) \<Longrightarrow> Arctan complex_differentiable at z"
  1865   using has_field_derivative_Arctan
  1866   by (auto simp: complex_differentiable_def)
  1867 
  1868 lemma complex_differentiable_within_Arctan:
  1869     "(Re z = 0 \<Longrightarrow> abs(Im z) < 1) \<Longrightarrow> Arctan complex_differentiable (at z within s)"
  1870   using complex_differentiable_at_Arctan complex_differentiable_at_within by blast
  1871 
  1872 declare has_field_derivative_Arctan [derivative_intros]
  1873 declare has_field_derivative_Arctan [THEN DERIV_chain2, derivative_intros]
  1874 
  1875 lemma continuous_at_Arctan:
  1876     "(Re z = 0 \<Longrightarrow> abs(Im z) < 1) \<Longrightarrow> continuous (at z) Arctan"
  1877   by (simp add: complex_differentiable_imp_continuous_at complex_differentiable_within_Arctan)
  1878 
  1879 lemma continuous_within_Arctan:
  1880     "(Re z = 0 \<Longrightarrow> abs(Im z) < 1) \<Longrightarrow> continuous (at z within s) Arctan"
  1881   using continuous_at_Arctan continuous_at_imp_continuous_within by blast
  1882 
  1883 lemma continuous_on_Arctan [continuous_intros]:
  1884     "(\<And>z. z \<in> s \<Longrightarrow> Re z = 0 \<Longrightarrow> abs \<bar>Im z\<bar> < 1) \<Longrightarrow> continuous_on s Arctan"
  1885   by (auto simp: continuous_at_imp_continuous_on continuous_within_Arctan)
  1886 
  1887 lemma holomorphic_on_Arctan:
  1888     "(\<And>z. z \<in> s \<Longrightarrow> Re z = 0 \<Longrightarrow> abs \<bar>Im z\<bar> < 1) \<Longrightarrow> Arctan holomorphic_on s"
  1889   by (simp add: complex_differentiable_within_Arctan holomorphic_on_def)
  1890 
  1891 
  1892 subsection \<open>Real arctangent\<close>
  1893 
  1894 lemma norm_exp_ii_times [simp]: "norm (exp(\<i> * of_real y)) = 1"
  1895   by simp
  1896 
  1897 lemma norm_exp_imaginary: "norm(exp z) = 1 \<Longrightarrow> Re z = 0"
  1898   by (simp add: complex_norm_eq_1_exp)
  1899 
  1900 lemma Im_Arctan_of_real [simp]: "Im (Arctan (of_real x)) = 0"
  1901   unfolding Arctan_def divide_complex_def
  1902   apply (simp add: complex_eq_iff)
  1903   apply (rule norm_exp_imaginary)
  1904   apply (subst exp_Ln, auto)
  1905   apply (simp_all add: cmod_def complex_eq_iff)
  1906   apply (auto simp: divide_simps)
  1907   apply (metis power_one realpow_two_sum_zero_iff zero_neq_one, algebra)
  1908   done
  1909 
  1910 lemma arctan_eq_Re_Arctan: "arctan x = Re (Arctan (of_real x))"
  1911 proof (rule arctan_unique)
  1912   show "- (pi / 2) < Re (Arctan (complex_of_real x))"
  1913     apply (simp add: Arctan_def)
  1914     apply (rule Im_Ln_less_pi)
  1915     apply (auto simp: Im_complex_div_lemma)
  1916     done
  1917 next
  1918   have *: " (1 - \<i>*x) / (1 + \<i>*x) \<noteq> 0"
  1919     by (simp add: divide_simps) ( simp add: complex_eq_iff)
  1920   show "Re (Arctan (complex_of_real x)) < pi / 2"
  1921     using mpi_less_Im_Ln [OF *]
  1922     by (simp add: Arctan_def)
  1923 next
  1924   have "tan (Re (Arctan (of_real x))) = Re (tan (Arctan (of_real x)))"
  1925     apply (auto simp: tan_def Complex.Re_divide Re_sin Re_cos Im_sin Im_cos)
  1926     apply (simp add: field_simps)
  1927     by (simp add: power2_eq_square)
  1928   also have "... = x"
  1929     apply (subst tan_Arctan, auto)
  1930     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)
  1931   finally show "tan (Re (Arctan (complex_of_real x))) = x" .
  1932 qed
  1933 
  1934 lemma Arctan_of_real: "Arctan (of_real x) = of_real (arctan x)"
  1935   unfolding arctan_eq_Re_Arctan divide_complex_def
  1936   by (simp add: complex_eq_iff)
  1937 
  1938 lemma Arctan_in_Reals [simp]: "z \<in> \<real> \<Longrightarrow> Arctan z \<in> \<real>"
  1939   by (metis Reals_cases Reals_of_real Arctan_of_real)
  1940 
  1941 declare arctan_one [simp]
  1942 
  1943 lemma arctan_less_pi4_pos: "x < 1 \<Longrightarrow> arctan x < pi/4"
  1944   by (metis arctan_less_iff arctan_one)
  1945 
  1946 lemma arctan_less_pi4_neg: "-1 < x \<Longrightarrow> -(pi/4) < arctan x"
  1947   by (metis arctan_less_iff arctan_minus arctan_one)
  1948 
  1949 lemma arctan_less_pi4: "abs x < 1 \<Longrightarrow> abs(arctan x) < pi/4"
  1950   by (metis abs_less_iff arctan_less_pi4_pos arctan_minus)
  1951 
  1952 lemma arctan_le_pi4: "abs x \<le> 1 \<Longrightarrow> abs(arctan x) \<le> pi/4"
  1953   by (metis abs_le_iff arctan_le_iff arctan_minus arctan_one)
  1954 
  1955 lemma abs_arctan: "abs(arctan x) = arctan(abs x)"
  1956   by (simp add: abs_if arctan_minus)
  1957 
  1958 lemma arctan_add_raw:
  1959   assumes "abs(arctan x + arctan y) < pi/2"
  1960     shows "arctan x + arctan y = arctan((x + y) / (1 - x * y))"
  1961 proof (rule arctan_unique [symmetric])
  1962   show 12: "- (pi / 2) < arctan x + arctan y" "arctan x + arctan y < pi / 2"
  1963     using assms by linarith+
  1964   show "tan (arctan x + arctan y) = (x + y) / (1 - x * y)"
  1965     using cos_gt_zero_pi [OF 12]
  1966     by (simp add: arctan tan_add)
  1967 qed
  1968 
  1969 lemma arctan_inverse:
  1970   assumes "0 < x"
  1971     shows "arctan(inverse x) = pi/2 - arctan x"
  1972 proof -
  1973   have "arctan(inverse x) = arctan(inverse(tan(arctan x)))"
  1974     by (simp add: arctan)
  1975   also have "... = arctan (tan (pi / 2 - arctan x))"
  1976     by (simp add: tan_cot)
  1977   also have "... = pi/2 - arctan x"
  1978   proof -
  1979     have "0 < pi - arctan x"
  1980     using arctan_ubound [of x] pi_gt_zero by linarith
  1981     with assms show ?thesis
  1982       by (simp add: Transcendental.arctan_tan)
  1983   qed
  1984   finally show ?thesis .
  1985 qed
  1986 
  1987 lemma arctan_add_small:
  1988   assumes "abs(x * y) < 1"
  1989     shows "(arctan x + arctan y = arctan((x + y) / (1 - x * y)))"
  1990 proof (cases "x = 0 \<or> y = 0")
  1991   case True then show ?thesis
  1992     by auto
  1993 next
  1994   case False
  1995   then have *: "\<bar>arctan x\<bar> < pi / 2 - \<bar>arctan y\<bar>" using assms
  1996     apply (auto simp add: abs_arctan arctan_inverse [symmetric] arctan_less_iff)
  1997     apply (simp add: divide_simps abs_mult)
  1998     done
  1999   show ?thesis
  2000     apply (rule arctan_add_raw)
  2001     using * by linarith
  2002 qed
  2003 
  2004 lemma abs_arctan_le:
  2005   fixes x::real shows "abs(arctan x) \<le> abs x"
  2006 proof -
  2007   { fix w::complex and z::complex
  2008     assume *: "w \<in> \<real>" "z \<in> \<real>"
  2009     have "cmod (Arctan w - Arctan z) \<le> 1 * cmod (w-z)"
  2010       apply (rule complex_differentiable_bound [OF convex_Reals, of Arctan _ 1])
  2011       apply (rule has_field_derivative_at_within [OF has_field_derivative_Arctan])
  2012       apply (force simp add: Reals_def)
  2013       apply (simp add: norm_divide divide_simps in_Reals_norm complex_is_Real_iff power2_eq_square)
  2014       using * by auto
  2015   }
  2016   then have "cmod (Arctan (of_real x) - Arctan 0) \<le> 1 * cmod (of_real x -0)"
  2017     using Reals_0 Reals_of_real by blast
  2018   then show ?thesis
  2019     by (simp add: Arctan_of_real)
  2020 qed
  2021 
  2022 lemma arctan_le_self: "0 \<le> x \<Longrightarrow> arctan x \<le> x"
  2023   by (metis abs_arctan_le abs_of_nonneg zero_le_arctan_iff)
  2024 
  2025 lemma abs_tan_ge: "abs x < pi/2 \<Longrightarrow> abs x \<le> abs(tan x)"
  2026   by (metis abs_arctan_le abs_less_iff arctan_tan minus_less_iff)
  2027 
  2028 
  2029 subsection\<open>Inverse Sine\<close>
  2030 
  2031 definition Arcsin :: "complex \<Rightarrow> complex" where
  2032    "Arcsin \<equiv> \<lambda>z. -\<i> * Ln(\<i> * z + csqrt(1 - z\<^sup>2))"
  2033 
  2034 lemma Arcsin_body_lemma: "\<i> * z + csqrt(1 - z\<^sup>2) \<noteq> 0"
  2035   using power2_csqrt [of "1 - z\<^sup>2"]
  2036   apply auto
  2037   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)
  2038 
  2039 lemma Arcsin_range_lemma: "abs (Re z) < 1 \<Longrightarrow> 0 < Re(\<i> * z + csqrt(1 - z\<^sup>2))"
  2040   using Complex.cmod_power2 [of z, symmetric]
  2041   by (simp add: real_less_rsqrt algebra_simps Re_power2 cmod_square_less_1_plus)
  2042 
  2043 lemma Re_Arcsin: "Re(Arcsin z) = Im (Ln (\<i> * z + csqrt(1 - z\<^sup>2)))"
  2044   by (simp add: Arcsin_def)
  2045 
  2046 lemma Im_Arcsin: "Im(Arcsin z) = - ln (cmod (\<i> * z + csqrt (1 - z\<^sup>2)))"
  2047   by (simp add: Arcsin_def Arcsin_body_lemma)
  2048 
  2049 lemma isCont_Arcsin:
  2050   assumes "(Im z = 0 \<Longrightarrow> \<bar>Re z\<bar> < 1)"
  2051     shows "isCont Arcsin z"
  2052 proof -
  2053   have rez: "Im (1 - z\<^sup>2) = 0 \<Longrightarrow> 0 < Re (1 - z\<^sup>2)"
  2054     using assms
  2055     by (auto simp: Re_power2 Im_power2 abs_square_less_1 add_pos_nonneg algebra_simps)
  2056   have cmz: "(cmod z)\<^sup>2 < 1 + cmod (1 - z\<^sup>2)"
  2057     by (blast intro: assms cmod_square_less_1_plus)
  2058   show ?thesis
  2059     using assms
  2060     apply (simp add: Arcsin_def)
  2061     apply (rule isCont_Ln' isCont_csqrt' continuous_intros)+
  2062     apply (erule rez)
  2063     apply (auto simp: Re_power2 Im_power2 abs_square_less_1 [symmetric] real_less_rsqrt algebra_simps split: split_if_asm)
  2064     apply (simp add: norm_complex_def)
  2065     using cmod_power2 [of z, symmetric] cmz
  2066     apply (simp add: real_less_rsqrt)
  2067     done
  2068 qed
  2069 
  2070 lemma isCont_Arcsin' [simp]:
  2071   shows "isCont f z \<Longrightarrow> (Im (f z) = 0 \<Longrightarrow> \<bar>Re (f z)\<bar> < 1) \<Longrightarrow> isCont (\<lambda>x. Arcsin (f x)) z"
  2072   by (blast intro: isCont_o2 [OF _ isCont_Arcsin])
  2073 
  2074 lemma sin_Arcsin [simp]: "sin(Arcsin z) = z"
  2075 proof -
  2076   have "\<i>*z*2 + csqrt (1 - z\<^sup>2)*2 = 0 \<longleftrightarrow> (\<i>*z)*2 + csqrt (1 - z\<^sup>2)*2 = 0"
  2077     by (simp add: algebra_simps)  --\<open>Cancelling a factor of 2\<close>
  2078   moreover have "... \<longleftrightarrow> (\<i>*z) + csqrt (1 - z\<^sup>2) = 0"
  2079     by (metis Arcsin_body_lemma distrib_right no_zero_divisors zero_neq_numeral)
  2080   ultimately show ?thesis
  2081     apply (simp add: sin_exp_eq Arcsin_def Arcsin_body_lemma exp_minus divide_simps)
  2082     apply (simp add: algebra_simps)
  2083     apply (simp add: power2_eq_square [symmetric] algebra_simps)
  2084     done
  2085 qed
  2086 
  2087 lemma Re_eq_pihalf_lemma:
  2088     "\<bar>Re z\<bar> = pi/2 \<Longrightarrow> Im z = 0 \<Longrightarrow>
  2089       Re ((exp (\<i>*z) + inverse (exp (\<i>*z))) / 2) = 0 \<and> 0 \<le> Im ((exp (\<i>*z) + inverse (exp (\<i>*z))) / 2)"
  2090   apply (simp add: cos_ii_times [symmetric] Re_cos Im_cos abs_if del: eq_divide_eq_numeral1)
  2091   by (metis cos_minus cos_pi_half)
  2092 
  2093 lemma Re_less_pihalf_lemma:
  2094   assumes "\<bar>Re z\<bar> < pi / 2"
  2095     shows "0 < Re ((exp (\<i>*z) + inverse (exp (\<i>*z))) / 2)"
  2096 proof -
  2097   have "0 < cos (Re z)" using assms
  2098     using cos_gt_zero_pi by auto
  2099   then show ?thesis
  2100     by (simp add: cos_ii_times [symmetric] Re_cos Im_cos add_pos_pos)
  2101 qed
  2102 
  2103 lemma Arcsin_sin:
  2104     assumes "\<bar>Re z\<bar> < pi/2 \<or> (\<bar>Re z\<bar> = pi/2 \<and> Im z = 0)"
  2105       shows "Arcsin(sin z) = z"
  2106 proof -
  2107   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))"
  2108     by (simp add: sin_exp_eq Arcsin_def exp_minus)
  2109   also have "... = - (\<i> * Ln (csqrt (((exp (\<i>*z) + inverse (exp (\<i>*z)))/2)\<^sup>2) - (inverse (exp (\<i>*z)) - exp (\<i>*z)) / 2))"
  2110     by (simp add: field_simps power2_eq_square)
  2111   also have "... = - (\<i> * Ln (((exp (\<i>*z) + inverse (exp (\<i>*z)))/2) - (inverse (exp (\<i>*z)) - exp (\<i>*z)) / 2))"
  2112     apply (subst csqrt_square)
  2113     using assms Re_eq_pihalf_lemma Re_less_pihalf_lemma
  2114     apply auto
  2115     done
  2116   also have "... =  - (\<i> * Ln (exp (\<i>*z)))"
  2117     by (simp add: field_simps power2_eq_square)
  2118   also have "... = z"
  2119     apply (subst Complex_Transcendental.Ln_exp)
  2120     using assms
  2121     apply (auto simp: abs_if simp del: eq_divide_eq_numeral1 split: split_if_asm)
  2122     done
  2123   finally show ?thesis .
  2124 qed
  2125 
  2126 lemma Arcsin_unique:
  2127     "\<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"
  2128   by (metis Arcsin_sin)
  2129 
  2130 lemma Arcsin_0 [simp]: "Arcsin 0 = 0"
  2131   by (metis Arcsin_sin norm_zero pi_half_gt_zero real_norm_def sin_zero zero_complex.simps(1))
  2132 
  2133 lemma Arcsin_1 [simp]: "Arcsin 1 = pi/2"
  2134   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)
  2135 
  2136 lemma Arcsin_minus_1 [simp]: "Arcsin(-1) = - (pi/2)"
  2137   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)
  2138 
  2139 lemma has_field_derivative_Arcsin:
  2140   assumes "(Im z = 0 \<Longrightarrow> \<bar>Re z\<bar> < 1)"
  2141     shows "(Arcsin has_field_derivative inverse(cos(Arcsin z))) (at z)"
  2142 proof -
  2143   have "(sin (Arcsin z))\<^sup>2 \<noteq> 1"
  2144     using assms
  2145     apply atomize
  2146     apply (auto simp: complex_eq_iff Re_power2 Im_power2 abs_square_eq_1)
  2147     apply (metis abs_minus_cancel abs_one abs_power2 numeral_One numeral_neq_neg_one)
  2148     by (metis abs_minus_cancel abs_one abs_power2 one_neq_neg_one)
  2149   then have "cos (Arcsin z) \<noteq> 0"
  2150     by (metis diff_0_right power_zero_numeral sin_squared_eq)
  2151   then show ?thesis
  2152     apply (rule has_complex_derivative_inverse_basic [OF DERIV_sin])
  2153     apply (auto intro: isCont_Arcsin open_ball [of z 1] assms)
  2154     done
  2155 qed
  2156 
  2157 declare has_field_derivative_Arcsin [derivative_intros]
  2158 declare has_field_derivative_Arcsin [THEN DERIV_chain2, derivative_intros]
  2159 
  2160 lemma complex_differentiable_at_Arcsin:
  2161     "(Im z = 0 \<Longrightarrow> \<bar>Re z\<bar> < 1) \<Longrightarrow> Arcsin complex_differentiable at z"
  2162   using complex_differentiable_def has_field_derivative_Arcsin by blast
  2163 
  2164 lemma complex_differentiable_within_Arcsin:
  2165     "(Im z = 0 \<Longrightarrow> \<bar>Re z\<bar> < 1) \<Longrightarrow> Arcsin complex_differentiable (at z within s)"
  2166   using complex_differentiable_at_Arcsin complex_differentiable_within_subset by blast
  2167 
  2168 lemma continuous_within_Arcsin:
  2169     "(Im z = 0 \<Longrightarrow> \<bar>Re z\<bar> < 1) \<Longrightarrow> continuous (at z within s) Arcsin"
  2170   using continuous_at_imp_continuous_within isCont_Arcsin by blast
  2171 
  2172 lemma continuous_on_Arcsin [continuous_intros]:
  2173     "(\<And>z. z \<in> s \<Longrightarrow> Im z = 0 \<Longrightarrow> \<bar>Re z\<bar> < 1) \<Longrightarrow> continuous_on s Arcsin"
  2174   by (simp add: continuous_at_imp_continuous_on)
  2175 
  2176 lemma holomorphic_on_Arcsin: "(\<And>z. z \<in> s \<Longrightarrow> Im z = 0 \<Longrightarrow> \<bar>Re z\<bar> < 1) \<Longrightarrow> Arcsin holomorphic_on s"
  2177   by (simp add: complex_differentiable_within_Arcsin holomorphic_on_def)
  2178 
  2179 
  2180 subsection\<open>Inverse Cosine\<close>
  2181 
  2182 definition Arccos :: "complex \<Rightarrow> complex" where
  2183    "Arccos \<equiv> \<lambda>z. -\<i> * Ln(z + \<i> * csqrt(1 - z\<^sup>2))"
  2184 
  2185 lemma Arccos_range_lemma: "\<bar>Re z\<bar> < 1 \<Longrightarrow> 0 < Im(z + \<i> * csqrt(1 - z\<^sup>2))"
  2186   using Arcsin_range_lemma [of "-z"]
  2187   by simp
  2188 
  2189 lemma Arccos_body_lemma: "z + \<i> * csqrt(1 - z\<^sup>2) \<noteq> 0"
  2190   using Arcsin_body_lemma [of z]
  2191   by (metis complex_i_mult_minus diff_add_cancel minus_diff_eq minus_unique mult.assoc mult.left_commute
  2192            power2_csqrt power2_eq_square zero_neq_one)
  2193 
  2194 lemma Re_Arccos: "Re(Arccos z) = Im (Ln (z + \<i> * csqrt(1 - z\<^sup>2)))"
  2195   by (simp add: Arccos_def)
  2196 
  2197 lemma Im_Arccos: "Im(Arccos z) = - ln (cmod (z + \<i> * csqrt (1 - z\<^sup>2)))"
  2198   by (simp add: Arccos_def Arccos_body_lemma)
  2199 
  2200 text\<open>A very tricky argument to find!\<close>
  2201 lemma abs_Re_less_1_preserve:
  2202   assumes "(Im z = 0 \<Longrightarrow> \<bar>Re z\<bar> < 1)"  "Im (z + \<i> * csqrt (1 - z\<^sup>2)) = 0"
  2203     shows "0 < Re (z + \<i> * csqrt (1 - z\<^sup>2))"
  2204 proof (cases "Im z = 0")
  2205   case True
  2206   then show ?thesis
  2207     using assms
  2208     by (fastforce simp add: cmod_def Re_power2 Im_power2 algebra_simps abs_square_less_1 [symmetric])
  2209 next
  2210   case False
  2211   have Imz: "Im z = - sqrt ((1 + ((Im z)\<^sup>2 + cmod (1 - z\<^sup>2)) - (Re z)\<^sup>2) / 2)"
  2212     using assms abs_Re_le_cmod [of "1-z\<^sup>2"]
  2213     by (simp add: Re_power2 algebra_simps)
  2214   have "(cmod z)\<^sup>2 - 1 \<noteq> cmod (1 - z\<^sup>2)"
  2215   proof (clarsimp simp add: cmod_def)
  2216     assume "(Re z)\<^sup>2 + (Im z)\<^sup>2 - 1 = sqrt ((1 - Re (z\<^sup>2))\<^sup>2 + (Im (z\<^sup>2))\<^sup>2)"
  2217     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)"
  2218       by simp
  2219     then show False using False
  2220       by (simp add: power2_eq_square algebra_simps)
  2221   qed
  2222   moreover have "(Im z)\<^sup>2 = ((1 + ((Im z)\<^sup>2 + cmod (1 - z\<^sup>2)) - (Re z)\<^sup>2) / 2)"
  2223     apply (subst Imz, simp)
  2224     apply (subst real_sqrt_pow2)
  2225     using abs_Re_le_cmod [of "1-z\<^sup>2"]
  2226     apply (auto simp: Re_power2 field_simps)
  2227     done
  2228   ultimately show ?thesis
  2229     by (simp add: Re_power2 Im_power2 cmod_power2)
  2230 qed
  2231 
  2232 lemma isCont_Arccos:
  2233   assumes "(Im z = 0 \<Longrightarrow> \<bar>Re z\<bar> < 1)"
  2234     shows "isCont Arccos z"
  2235 proof -
  2236   have rez: "Im (1 - z\<^sup>2) = 0 \<Longrightarrow> 0 < Re (1 - z\<^sup>2)"
  2237     using assms
  2238     by (auto simp: Re_power2 Im_power2 abs_square_less_1 add_pos_nonneg algebra_simps)
  2239   show ?thesis
  2240     using assms
  2241     apply (simp add: Arccos_def)
  2242     apply (rule isCont_Ln' isCont_csqrt' continuous_intros)+
  2243     apply (erule rez)
  2244     apply (blast intro: abs_Re_less_1_preserve)
  2245     done
  2246 qed
  2247 
  2248 lemma isCont_Arccos' [simp]:
  2249   shows "isCont f z \<Longrightarrow> (Im (f z) = 0 \<Longrightarrow> \<bar>Re (f z)\<bar> < 1) \<Longrightarrow> isCont (\<lambda>x. Arccos (f x)) z"
  2250   by (blast intro: isCont_o2 [OF _ isCont_Arccos])
  2251 
  2252 lemma cos_Arccos [simp]: "cos(Arccos z) = z"
  2253 proof -
  2254   have "z*2 + \<i> * (2 * csqrt (1 - z\<^sup>2)) = 0 \<longleftrightarrow> z*2 + \<i> * csqrt (1 - z\<^sup>2)*2 = 0"
  2255     by (simp add: algebra_simps)  --\<open>Cancelling a factor of 2\<close>
  2256   moreover have "... \<longleftrightarrow> z + \<i> * csqrt (1 - z\<^sup>2) = 0"
  2257     by (metis distrib_right mult_eq_0_iff zero_neq_numeral)
  2258   ultimately show ?thesis
  2259     apply (simp add: cos_exp_eq Arccos_def Arccos_body_lemma exp_minus field_simps)
  2260     apply (simp add: power2_eq_square [symmetric])
  2261     done
  2262 qed
  2263 
  2264 lemma Arccos_cos:
  2265     assumes "0 < Re z & Re z < pi \<or>
  2266              Re z = 0 & 0 \<le> Im z \<or>
  2267              Re z = pi & Im z \<le> 0"
  2268       shows "Arccos(cos z) = z"
  2269 proof -
  2270   have *: "((\<i> - (exp (\<i> * z))\<^sup>2 * \<i>) / (2 * exp (\<i> * z))) = sin z"
  2271     by (simp add: sin_exp_eq exp_minus field_simps power2_eq_square)
  2272   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"
  2273     by (simp add: field_simps power2_eq_square)
  2274   then have "Arccos(cos z) = - (\<i> * Ln ((exp (\<i> * z) + inverse (exp (\<i> * z))) / 2 +
  2275                            \<i> * csqrt (((\<i> - (exp (\<i> * z))\<^sup>2 * \<i>) / (2 * exp (\<i> * z)))\<^sup>2)))"
  2276     by (simp add: cos_exp_eq Arccos_def exp_minus)
  2277   also have "... = - (\<i> * Ln ((exp (\<i> * z) + inverse (exp (\<i> * z))) / 2 +
  2278                               \<i> * ((\<i> - (exp (\<i> * z))\<^sup>2 * \<i>) / (2 * exp (\<i> * z)))))"
  2279     apply (subst csqrt_square)
  2280     using assms Re_sin_pos [of z] Im_sin_nonneg [of z] Im_sin_nonneg2 [of z]
  2281     apply (auto simp: * Re_sin Im_sin)
  2282     done
  2283   also have "... =  - (\<i> * Ln (exp (\<i>*z)))"
  2284     by (simp add: field_simps power2_eq_square)
  2285   also have "... = z"
  2286     using assms
  2287     apply (subst Complex_Transcendental.Ln_exp, auto)
  2288     done
  2289   finally show ?thesis .
  2290 qed
  2291 
  2292 lemma Arccos_unique:
  2293     "\<lbrakk>cos z = w;
  2294       0 < Re z \<and> Re z < pi \<or>
  2295       Re z = 0 \<and> 0 \<le> Im z \<or>
  2296       Re z = pi \<and> Im z \<le> 0\<rbrakk> \<Longrightarrow> Arccos w = z"
  2297   using Arccos_cos by blast
  2298 
  2299 lemma Arccos_0 [simp]: "Arccos 0 = pi/2"
  2300   by (rule Arccos_unique) (auto simp: of_real_numeral)
  2301 
  2302 lemma Arccos_1 [simp]: "Arccos 1 = 0"
  2303   by (rule Arccos_unique) auto
  2304 
  2305 lemma Arccos_minus1: "Arccos(-1) = pi"
  2306   by (rule Arccos_unique) auto
  2307 
  2308 lemma has_field_derivative_Arccos:
  2309   assumes "(Im z = 0 \<Longrightarrow> \<bar>Re z\<bar> < 1)"
  2310     shows "(Arccos has_field_derivative - inverse(sin(Arccos z))) (at z)"
  2311 proof -
  2312   have "(cos (Arccos z))\<^sup>2 \<noteq> 1"
  2313     using assms
  2314     apply atomize
  2315     apply (auto simp: complex_eq_iff Re_power2 Im_power2 abs_square_eq_1)
  2316     apply (metis abs_minus_cancel abs_one abs_power2 numeral_One numeral_neq_neg_one)
  2317     apply (metis left_minus less_irrefl power_one sum_power2_gt_zero_iff zero_neq_neg_one)
  2318     done
  2319   then have "- sin (Arccos z) \<noteq> 0"
  2320     by (metis cos_squared_eq diff_0_right mult_zero_left neg_0_equal_iff_equal power2_eq_square)
  2321   then have "(Arccos has_field_derivative inverse(- sin(Arccos z))) (at z)"
  2322     apply (rule has_complex_derivative_inverse_basic [OF DERIV_cos])
  2323     apply (auto intro: isCont_Arccos open_ball [of z 1] assms)
  2324     done
  2325   then show ?thesis
  2326     by simp
  2327 qed
  2328 
  2329 declare has_field_derivative_Arcsin [derivative_intros]
  2330 declare has_field_derivative_Arcsin [THEN DERIV_chain2, derivative_intros]
  2331 
  2332 lemma complex_differentiable_at_Arccos:
  2333     "(Im z = 0 \<Longrightarrow> \<bar>Re z\<bar> < 1) \<Longrightarrow> Arccos complex_differentiable at z"
  2334   using complex_differentiable_def has_field_derivative_Arccos by blast
  2335 
  2336 lemma complex_differentiable_within_Arccos:
  2337     "(Im z = 0 \<Longrightarrow> \<bar>Re z\<bar> < 1) \<Longrightarrow> Arccos complex_differentiable (at z within s)"
  2338   using complex_differentiable_at_Arccos complex_differentiable_within_subset by blast
  2339 
  2340 lemma continuous_within_Arccos:
  2341     "(Im z = 0 \<Longrightarrow> \<bar>Re z\<bar> < 1) \<Longrightarrow> continuous (at z within s) Arccos"
  2342   using continuous_at_imp_continuous_within isCont_Arccos by blast
  2343 
  2344 lemma continuous_on_Arccos [continuous_intros]:
  2345     "(\<And>z. z \<in> s \<Longrightarrow> Im z = 0 \<Longrightarrow> \<bar>Re z\<bar> < 1) \<Longrightarrow> continuous_on s Arccos"
  2346   by (simp add: continuous_at_imp_continuous_on)
  2347 
  2348 lemma holomorphic_on_Arccos: "(\<And>z. z \<in> s \<Longrightarrow> Im z = 0 \<Longrightarrow> \<bar>Re z\<bar> < 1) \<Longrightarrow> Arccos holomorphic_on s"
  2349   by (simp add: complex_differentiable_within_Arccos holomorphic_on_def)
  2350 
  2351 
  2352 subsection\<open>Upper and Lower Bounds for Inverse Sine and Cosine\<close>
  2353 
  2354 lemma Arcsin_bounds: "\<bar>Re z\<bar> < 1 \<Longrightarrow> abs(Re(Arcsin z)) < pi/2"
  2355   unfolding Re_Arcsin
  2356   by (blast intro: Re_Ln_pos_lt_imp Arcsin_range_lemma)
  2357 
  2358 lemma Arccos_bounds: "\<bar>Re z\<bar> < 1 \<Longrightarrow> 0 < Re(Arccos z) \<and> Re(Arccos z) < pi"
  2359   unfolding Re_Arccos
  2360   by (blast intro!: Im_Ln_pos_lt_imp Arccos_range_lemma)
  2361 
  2362 lemma Re_Arccos_bounds: "-pi < Re(Arccos z) \<and> Re(Arccos z) \<le> pi"
  2363   unfolding Re_Arccos
  2364   by (blast intro!: mpi_less_Im_Ln Im_Ln_le_pi Arccos_body_lemma)
  2365 
  2366 lemma Re_Arccos_bound: "abs(Re(Arccos z)) \<le> pi"
  2367   using Re_Arccos_bounds abs_le_interval_iff less_eq_real_def by blast
  2368 
  2369 lemma Re_Arcsin_bounds: "-pi < Re(Arcsin z) & Re(Arcsin z) \<le> pi"
  2370   unfolding Re_Arcsin
  2371   by (blast intro!: mpi_less_Im_Ln Im_Ln_le_pi Arcsin_body_lemma)
  2372 
  2373 lemma Re_Arcsin_bound: "abs(Re(Arcsin z)) \<le> pi"
  2374   using Re_Arcsin_bounds abs_le_interval_iff less_eq_real_def by blast
  2375 
  2376 
  2377 subsection\<open>Interrelations between Arcsin and Arccos\<close>
  2378 
  2379 lemma cos_Arcsin_nonzero:
  2380   assumes "z\<^sup>2 \<noteq> 1" shows "cos(Arcsin z) \<noteq> 0"
  2381 proof -
  2382   have eq: "(\<i> * z * (csqrt (1 - z\<^sup>2)))\<^sup>2 = z\<^sup>2 * (z\<^sup>2 - 1)"
  2383     by (simp add: power_mult_distrib algebra_simps)
  2384   have "\<i> * z * (csqrt (1 - z\<^sup>2)) \<noteq> z\<^sup>2 - 1"
  2385   proof
  2386     assume "\<i> * z * (csqrt (1 - z\<^sup>2)) = z\<^sup>2 - 1"
  2387     then have "(\<i> * z * (csqrt (1 - z\<^sup>2)))\<^sup>2 = (z\<^sup>2 - 1)\<^sup>2"
  2388       by simp
  2389     then have "z\<^sup>2 * (z\<^sup>2 - 1) = (z\<^sup>2 - 1)*(z\<^sup>2 - 1)"
  2390       using eq power2_eq_square by auto
  2391     then show False
  2392       using assms by simp
  2393   qed
  2394   then have "1 + \<i> * z * (csqrt (1 - z * z)) \<noteq> z\<^sup>2"
  2395     by (metis add_minus_cancel power2_eq_square uminus_add_conv_diff)
  2396   then have "2*(1 + \<i> * z * (csqrt (1 - z * z))) \<noteq> 2*z\<^sup>2"  (*FIXME cancel_numeral_factor*)
  2397     by (metis mult_cancel_left zero_neq_numeral)
  2398   then have "(\<i> * z + csqrt (1 - z\<^sup>2))\<^sup>2 \<noteq> -1"
  2399     using assms
  2400     apply (auto simp: power2_sum)
  2401     apply (simp add: power2_eq_square algebra_simps)
  2402     done
  2403   then show ?thesis
  2404     apply (simp add: cos_exp_eq Arcsin_def exp_minus)
  2405     apply (simp add: divide_simps Arcsin_body_lemma)
  2406     apply (metis add.commute minus_unique power2_eq_square)
  2407     done
  2408 qed
  2409 
  2410 lemma sin_Arccos_nonzero:
  2411   assumes "z\<^sup>2 \<noteq> 1" shows "sin(Arccos z) \<noteq> 0"
  2412 proof -
  2413   have eq: "(\<i> * z * (csqrt (1 - z\<^sup>2)))\<^sup>2 = -(z\<^sup>2) * (1 - z\<^sup>2)"
  2414     by (simp add: power_mult_distrib algebra_simps)
  2415   have "\<i> * z * (csqrt (1 - z\<^sup>2)) \<noteq> 1 - z\<^sup>2"
  2416   proof
  2417     assume "\<i> * z * (csqrt (1 - z\<^sup>2)) = 1 - z\<^sup>2"
  2418     then have "(\<i> * z * (csqrt (1 - z\<^sup>2)))\<^sup>2 = (1 - z\<^sup>2)\<^sup>2"
  2419       by simp
  2420     then have "-(z\<^sup>2) * (1 - z\<^sup>2) = (1 - z\<^sup>2)*(1 - z\<^sup>2)"
  2421       using eq power2_eq_square by auto
  2422     then have "-(z\<^sup>2) = (1 - z\<^sup>2)"
  2423       using assms
  2424       by (metis add.commute add.right_neutral diff_add_cancel mult_right_cancel)
  2425     then show False
  2426       using assms by simp
  2427   qed
  2428   then have "z\<^sup>2 + \<i> * z * (csqrt (1 - z\<^sup>2)) \<noteq> 1"
  2429     by (simp add: algebra_simps)
  2430   then have "2*(z\<^sup>2 + \<i> * z * (csqrt (1 - z\<^sup>2))) \<noteq> 2*1"
  2431     by (metis mult_cancel_left2 zero_neq_numeral)  (*FIXME cancel_numeral_factor*)
  2432   then have "(z + \<i> * csqrt (1 - z\<^sup>2))\<^sup>2 \<noteq> 1"
  2433     using assms
  2434     apply (auto simp: Power.comm_semiring_1_class.power2_sum power_mult_distrib)
  2435     apply (simp add: power2_eq_square algebra_simps)
  2436     done
  2437   then show ?thesis
  2438     apply (simp add: sin_exp_eq Arccos_def exp_minus)
  2439     apply (simp add: divide_simps Arccos_body_lemma)
  2440     apply (simp add: power2_eq_square)
  2441     done
  2442 qed
  2443 
  2444 lemma cos_sin_csqrt:
  2445   assumes "0 < cos(Re z)  \<or>  cos(Re z) = 0 \<and> Im z * sin(Re z) \<le> 0"
  2446     shows "cos z = csqrt(1 - (sin z)\<^sup>2)"
  2447   apply (rule csqrt_unique [THEN sym])
  2448   apply (simp add: cos_squared_eq)
  2449   using assms
  2450   apply (auto simp: Re_cos Im_cos add_pos_pos mult_le_0_iff zero_le_mult_iff)
  2451   apply (auto simp: algebra_simps)
  2452   done
  2453 
  2454 lemma sin_cos_csqrt:
  2455   assumes "0 < sin(Re z)  \<or>  sin(Re z) = 0 \<and> 0 \<le> Im z * cos(Re z)"
  2456     shows "sin z = csqrt(1 - (cos z)\<^sup>2)"
  2457   apply (rule csqrt_unique [THEN sym])
  2458   apply (simp add: sin_squared_eq)
  2459   using assms
  2460   apply (auto simp: Re_sin Im_sin add_pos_pos mult_le_0_iff zero_le_mult_iff)
  2461   apply (auto simp: algebra_simps)
  2462   done
  2463 
  2464 lemma Arcsin_Arccos_csqrt_pos:
  2465     "(0 < Re z | Re z = 0 & 0 \<le> Im z) \<Longrightarrow> Arcsin z = Arccos(csqrt(1 - z\<^sup>2))"
  2466   by (simp add: Arcsin_def Arccos_def Complex.csqrt_square add.commute)
  2467 
  2468 lemma Arccos_Arcsin_csqrt_pos:
  2469     "(0 < Re z | Re z = 0 & 0 \<le> Im z) \<Longrightarrow> Arccos z = Arcsin(csqrt(1 - z\<^sup>2))"
  2470   by (simp add: Arcsin_def Arccos_def Complex.csqrt_square add.commute)
  2471 
  2472 lemma sin_Arccos:
  2473     "0 < Re z | Re z = 0 & 0 \<le> Im z \<Longrightarrow> sin(Arccos z) = csqrt(1 - z\<^sup>2)"
  2474   by (simp add: Arccos_Arcsin_csqrt_pos)
  2475 
  2476 lemma cos_Arcsin:
  2477     "0 < Re z | Re z = 0 & 0 \<le> Im z \<Longrightarrow> cos(Arcsin z) = csqrt(1 - z\<^sup>2)"
  2478   by (simp add: Arcsin_Arccos_csqrt_pos)
  2479 
  2480 
  2481 subsection\<open>Relationship with Arcsin on the Real Numbers\<close>
  2482 
  2483 lemma Im_Arcsin_of_real:
  2484   assumes "abs x \<le> 1"
  2485     shows "Im (Arcsin (of_real x)) = 0"
  2486 proof -
  2487   have "csqrt (1 - (of_real x)\<^sup>2) = (if x^2 \<le> 1 then sqrt (1 - x^2) else \<i> * sqrt (x^2 - 1))"
  2488     by (simp add: of_real_sqrt del: csqrt_of_real_nonneg)
  2489   then have "cmod (\<i> * of_real x + csqrt (1 - (of_real x)\<^sup>2))^2 = 1"
  2490     using assms abs_square_le_1
  2491     by (force simp add: Complex.cmod_power2)
  2492   then have "cmod (\<i> * of_real x + csqrt (1 - (of_real x)\<^sup>2)) = 1"
  2493     by (simp add: norm_complex_def)
  2494   then show ?thesis
  2495     by (simp add: Im_Arcsin exp_minus)
  2496 qed
  2497 
  2498 corollary Arcsin_in_Reals [simp]: "z \<in> \<real> \<Longrightarrow> \<bar>Re z\<bar> \<le> 1 \<Longrightarrow> Arcsin z \<in> \<real>"
  2499   by (metis Im_Arcsin_of_real Re_complex_of_real Reals_cases complex_is_Real_iff)
  2500 
  2501 lemma arcsin_eq_Re_Arcsin:
  2502   assumes "abs x \<le> 1"
  2503     shows "arcsin x = Re (Arcsin (of_real x))"
  2504 unfolding arcsin_def
  2505 proof (rule the_equality, safe)
  2506   show "- (pi / 2) \<le> Re (Arcsin (complex_of_real x))"
  2507     using Re_Ln_pos_le [OF Arcsin_body_lemma, of "of_real x"]
  2508     by (auto simp: Complex.in_Reals_norm Re_Arcsin)
  2509 next
  2510   show "Re (Arcsin (complex_of_real x)) \<le> pi / 2"
  2511     using Re_Ln_pos_le [OF Arcsin_body_lemma, of "of_real x"]
  2512     by (auto simp: Complex.in_Reals_norm Re_Arcsin)
  2513 next
  2514   show "sin (Re (Arcsin (complex_of_real x))) = x"
  2515     using Re_sin [of "Arcsin (of_real x)"] Arcsin_body_lemma [of "of_real x"]
  2516     by (simp add: Im_Arcsin_of_real assms)
  2517 next
  2518   fix x'
  2519   assume "- (pi / 2) \<le> x'" "x' \<le> pi / 2" "x = sin x'"
  2520   then show "x' = Re (Arcsin (complex_of_real (sin x')))"
  2521     apply (simp add: sin_of_real [symmetric])
  2522     apply (subst Arcsin_sin)
  2523     apply (auto simp: )
  2524     done
  2525 qed
  2526 
  2527 lemma of_real_arcsin: "abs x \<le> 1 \<Longrightarrow> of_real(arcsin x) = Arcsin(of_real x)"
  2528   by (metis Im_Arcsin_of_real add.right_neutral arcsin_eq_Re_Arcsin complex_eq mult_zero_right of_real_0)
  2529 
  2530 
  2531 subsection\<open>Relationship with Arccos on the Real Numbers\<close>
  2532 
  2533 lemma Im_Arccos_of_real:
  2534   assumes "abs x \<le> 1"
  2535     shows "Im (Arccos (of_real x)) = 0"
  2536 proof -
  2537   have "csqrt (1 - (of_real x)\<^sup>2) = (if x^2 \<le> 1 then sqrt (1 - x^2) else \<i> * sqrt (x^2 - 1))"
  2538     by (simp add: of_real_sqrt del: csqrt_of_real_nonneg)
  2539   then have "cmod (of_real x + \<i> * csqrt (1 - (of_real x)\<^sup>2))^2 = 1"
  2540     using assms abs_square_le_1
  2541     by (force simp add: Complex.cmod_power2)
  2542   then have "cmod (of_real x + \<i> * csqrt (1 - (of_real x)\<^sup>2)) = 1"
  2543     by (simp add: norm_complex_def)
  2544   then show ?thesis
  2545     by (simp add: Im_Arccos exp_minus)
  2546 qed
  2547 
  2548 corollary Arccos_in_Reals [simp]: "z \<in> \<real> \<Longrightarrow> \<bar>Re z\<bar> \<le> 1 \<Longrightarrow> Arccos z \<in> \<real>"
  2549   by (metis Im_Arccos_of_real Re_complex_of_real Reals_cases complex_is_Real_iff)
  2550 
  2551 lemma arccos_eq_Re_Arccos:
  2552   assumes "abs x \<le> 1"
  2553     shows "arccos x = Re (Arccos (of_real x))"
  2554 unfolding arccos_def
  2555 proof (rule the_equality, safe)
  2556   show "0 \<le> Re (Arccos (complex_of_real x))"
  2557     using Im_Ln_pos_le [OF Arccos_body_lemma, of "of_real x"]
  2558     by (auto simp: Complex.in_Reals_norm Re_Arccos)
  2559 next
  2560   show "Re (Arccos (complex_of_real x)) \<le> pi"
  2561     using Im_Ln_pos_le [OF Arccos_body_lemma, of "of_real x"]
  2562     by (auto simp: Complex.in_Reals_norm Re_Arccos)
  2563 next
  2564   show "cos (Re (Arccos (complex_of_real x))) = x"
  2565     using Re_cos [of "Arccos (of_real x)"] Arccos_body_lemma [of "of_real x"]
  2566     by (simp add: Im_Arccos_of_real assms)
  2567 next
  2568   fix x'
  2569   assume "0 \<le> x'" "x' \<le> pi" "x = cos x'"
  2570   then show "x' = Re (Arccos (complex_of_real (cos x')))"
  2571     apply (simp add: cos_of_real [symmetric])
  2572     apply (subst Arccos_cos)
  2573     apply (auto simp: )
  2574     done
  2575 qed
  2576 
  2577 lemma of_real_arccos: "abs x \<le> 1 \<Longrightarrow> of_real(arccos x) = Arccos(of_real x)"
  2578   by (metis Im_Arccos_of_real add.right_neutral arccos_eq_Re_Arccos complex_eq mult_zero_right of_real_0)
  2579 
  2580 subsection\<open>Some interrelationships among the real inverse trig functions.\<close>
  2581 
  2582 lemma arccos_arctan:
  2583   assumes "-1 < x" "x < 1"
  2584     shows "arccos x = pi/2 - arctan(x / sqrt(1 - x\<^sup>2))"
  2585 proof -
  2586   have "arctan(x / sqrt(1 - x\<^sup>2)) - (pi/2 - arccos x) = 0"
  2587   proof (rule sin_eq_0_pi)
  2588     show "- pi < arctan (x / sqrt (1 - x\<^sup>2)) - (pi / 2 - arccos x)"
  2589       using arctan_lbound [of "x / sqrt(1 - x\<^sup>2)"]  arccos_bounded [of x] assms
  2590       by (simp add: algebra_simps)
  2591   next
  2592     show "arctan (x / sqrt (1 - x\<^sup>2)) - (pi / 2 - arccos x) < pi"
  2593       using arctan_ubound [of "x / sqrt(1 - x\<^sup>2)"]  arccos_bounded [of x] assms
  2594       by (simp add: algebra_simps)
  2595   next
  2596     show "sin (arctan (x / sqrt (1 - x\<^sup>2)) - (pi / 2 - arccos x)) = 0"
  2597       using assms
  2598       by (simp add: algebra_simps sin_diff cos_add sin_arccos sin_arctan cos_arctan
  2599                     power2_eq_square square_eq_1_iff)
  2600   qed
  2601   then show ?thesis
  2602     by simp
  2603 qed
  2604 
  2605 lemma arcsin_plus_arccos:
  2606   assumes "-1 \<le> x" "x \<le> 1"
  2607     shows "arcsin x + arccos x = pi/2"
  2608 proof -
  2609   have "arcsin x = pi/2 - arccos x"
  2610     apply (rule sin_inj_pi)
  2611     using assms arcsin [OF assms] arccos [OF assms]
  2612     apply (auto simp: algebra_simps sin_diff)
  2613     done
  2614   then show ?thesis
  2615     by (simp add: algebra_simps)
  2616 qed
  2617 
  2618 lemma arcsin_arccos_eq: "-1 \<le> x \<Longrightarrow> x \<le> 1 \<Longrightarrow> arcsin x = pi/2 - arccos x"
  2619   using arcsin_plus_arccos by force
  2620 
  2621 lemma arccos_arcsin_eq: "-1 \<le> x \<Longrightarrow> x \<le> 1 \<Longrightarrow> arccos x = pi/2 - arcsin x"
  2622   using arcsin_plus_arccos by force
  2623 
  2624 lemma arcsin_arctan: "-1 < x \<Longrightarrow> x < 1 \<Longrightarrow> arcsin x = arctan(x / sqrt(1 - x\<^sup>2))"
  2625   by (simp add: arccos_arctan arcsin_arccos_eq)
  2626 
  2627 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))"
  2628   by ( simp add: of_real_sqrt del: csqrt_of_real_nonneg)
  2629 
  2630 lemma arcsin_arccos_sqrt_pos: "0 \<le> x \<Longrightarrow> x \<le> 1 \<Longrightarrow> arcsin x = arccos(sqrt(1 - x\<^sup>2))"
  2631   apply (simp add: abs_square_le_1 diff_le_iff arcsin_eq_Re_Arcsin arccos_eq_Re_Arccos)
  2632   apply (subst Arcsin_Arccos_csqrt_pos)
  2633   apply (auto simp: power_le_one csqrt_1_diff_eq)
  2634   done
  2635 
  2636 lemma arcsin_arccos_sqrt_neg: "-1 \<le> x \<Longrightarrow> x \<le> 0 \<Longrightarrow> arcsin x = -arccos(sqrt(1 - x\<^sup>2))"
  2637   using arcsin_arccos_sqrt_pos [of "-x"]
  2638   by (simp add: arcsin_minus)
  2639 
  2640 lemma arccos_arcsin_sqrt_pos: "0 \<le> x \<Longrightarrow> x \<le> 1 \<Longrightarrow> arccos x = arcsin(sqrt(1 - x\<^sup>2))"
  2641   apply (simp add: abs_square_le_1 diff_le_iff arcsin_eq_Re_Arcsin arccos_eq_Re_Arccos)
  2642   apply (subst Arccos_Arcsin_csqrt_pos)
  2643   apply (auto simp: power_le_one csqrt_1_diff_eq)
  2644   done
  2645 
  2646 lemma arccos_arcsin_sqrt_neg: "-1 \<le> x \<Longrightarrow> x \<le> 0 \<Longrightarrow> arccos x = pi - arcsin(sqrt(1 - x\<^sup>2))"
  2647   using arccos_arcsin_sqrt_pos [of "-x"]
  2648   by (simp add: arccos_minus)
  2649 
  2650 subsection\<open>continuity results for arcsin and arccos.\<close>
  2651 
  2652 lemma continuous_on_Arcsin_real [continuous_intros]:
  2653     "continuous_on {w \<in> \<real>. \<bar>Re w\<bar> \<le> 1} Arcsin"
  2654 proof -
  2655   have "continuous_on {w \<in> \<real>. \<bar>Re w\<bar> \<le> 1} (\<lambda>x. complex_of_real (arcsin (Re x))) =
  2656         continuous_on {w \<in> \<real>. \<bar>Re w\<bar> \<le> 1} (\<lambda>x. complex_of_real (Re (Arcsin (of_real (Re x)))))"
  2657     by (rule continuous_on_cong [OF refl]) (simp add: arcsin_eq_Re_Arcsin)
  2658   also have "... = ?thesis"
  2659     by (rule continuous_on_cong [OF refl]) simp
  2660   finally show ?thesis
  2661     using continuous_on_arcsin [OF continuous_on_Re [OF continuous_on_id], of "{w \<in> \<real>. \<bar>Re w\<bar> \<le> 1}"]
  2662           continuous_on_of_real
  2663     by fastforce
  2664 qed
  2665 
  2666 lemma continuous_within_Arcsin_real:
  2667     "continuous (at z within {w \<in> \<real>. \<bar>Re w\<bar> \<le> 1}) Arcsin"
  2668 proof (cases "z \<in> {w \<in> \<real>. \<bar>Re w\<bar> \<le> 1}")
  2669   case True then show ?thesis
  2670     using continuous_on_Arcsin_real continuous_on_eq_continuous_within
  2671     by blast
  2672 next
  2673   case False
  2674   with closed_real_abs_le [of 1] show ?thesis
  2675     by (rule continuous_within_closed_nontrivial)
  2676 qed
  2677 
  2678 lemma continuous_on_Arccos_real:
  2679     "continuous_on {w \<in> \<real>. \<bar>Re w\<bar> \<le> 1} Arccos"
  2680 proof -
  2681   have "continuous_on {w \<in> \<real>. \<bar>Re w\<bar> \<le> 1} (\<lambda>x. complex_of_real (arccos (Re x))) =
  2682         continuous_on {w \<in> \<real>. \<bar>Re w\<bar> \<le> 1} (\<lambda>x. complex_of_real (Re (Arccos (of_real (Re x)))))"
  2683     by (rule continuous_on_cong [OF refl]) (simp add: arccos_eq_Re_Arccos)
  2684   also have "... = ?thesis"
  2685     by (rule continuous_on_cong [OF refl]) simp
  2686   finally show ?thesis
  2687     using continuous_on_arccos [OF continuous_on_Re [OF continuous_on_id], of "{w \<in> \<real>. \<bar>Re w\<bar> \<le> 1}"]
  2688           continuous_on_of_real
  2689     by fastforce
  2690 qed
  2691 
  2692 lemma continuous_within_Arccos_real:
  2693     "continuous (at z within {w \<in> \<real>. \<bar>Re w\<bar> \<le> 1}) Arccos"
  2694 proof (cases "z \<in> {w \<in> \<real>. \<bar>Re w\<bar> \<le> 1}")
  2695   case True then show ?thesis
  2696     using continuous_on_Arccos_real continuous_on_eq_continuous_within
  2697     by blast
  2698 next
  2699   case False
  2700   with closed_real_abs_le [of 1] show ?thesis
  2701     by (rule continuous_within_closed_nontrivial)
  2702 qed
  2703 
  2704 
  2705 subsection\<open>Roots of unity\<close>
  2706 
  2707 lemma complex_root_unity:
  2708   fixes j::nat
  2709   assumes "n \<noteq> 0"
  2710     shows "exp(2 * of_real pi * \<i> * of_nat j / of_nat n)^n = 1"
  2711 proof -
  2712   have *: "of_nat j * (complex_of_real pi * 2) = complex_of_real (2 * real j * pi)"
  2713     by (simp add: of_real_numeral)
  2714   then show ?thesis
  2715     apply (simp add: exp_of_nat_mult [symmetric] mult_ac exp_Euler)
  2716     apply (simp only: * cos_of_real sin_of_real)
  2717     apply (simp add: )
  2718     done
  2719 qed
  2720 
  2721 lemma complex_root_unity_eq:
  2722   fixes j::nat and k::nat
  2723   assumes "1 \<le> n"
  2724     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)
  2725            \<longleftrightarrow> j mod n = k mod n)"
  2726 proof -
  2727     have "(\<exists>z::int. \<i> * (of_nat j * (of_real pi * 2)) =
  2728                \<i> * (of_nat k * (of_real pi * 2)) + \<i> * (of_int z * (of_nat n * (of_real pi * 2)))) \<longleftrightarrow>
  2729           (\<exists>z::int. of_nat j * (\<i> * (of_real pi * 2)) =
  2730               (of_nat k + of_nat n * of_int z) * (\<i> * (of_real pi * 2)))"
  2731       by (simp add: algebra_simps)
  2732     also have "... \<longleftrightarrow> (\<exists>z::int. of_nat j = of_nat k + of_nat n * (of_int z :: complex))"
  2733       by simp
  2734     also have "... \<longleftrightarrow> (\<exists>z::int. of_nat j = of_nat k + of_nat n * z)"
  2735       apply (rule HOL.iff_exI)
  2736       apply (auto simp: )
  2737       using of_int_eq_iff apply fastforce
  2738       by (metis of_int_add of_int_mult of_int_of_nat_eq)
  2739     also have "... \<longleftrightarrow> int j mod int n = int k mod int n"
  2740       by (auto simp: zmod_eq_dvd_iff dvd_def algebra_simps)
  2741     also have "... \<longleftrightarrow> j mod n = k mod n"
  2742       by (metis of_nat_eq_iff zmod_int)
  2743     finally have "(\<exists>z. \<i> * (of_nat j * (of_real pi * 2)) =
  2744              \<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" .
  2745    note * = this
  2746   show ?thesis
  2747     using assms
  2748     by (simp add: exp_eq divide_simps mult_ac of_real_numeral *)
  2749 qed
  2750 
  2751 corollary bij_betw_roots_unity:
  2752     "bij_betw (\<lambda>j. exp(2 * of_real pi * \<i> * of_nat j / of_nat n))
  2753               {..<n}  {exp(2 * of_real pi * \<i> * of_nat j / of_nat n) | j. j < n}"
  2754   by (auto simp: bij_betw_def inj_on_def complex_root_unity_eq)
  2755 
  2756 lemma complex_root_unity_eq_1:
  2757   fixes j::nat and k::nat
  2758   assumes "1 \<le> n"
  2759     shows "exp(2 * of_real pi * \<i> * of_nat j / of_nat n) = 1 \<longleftrightarrow> n dvd j"
  2760 proof -
  2761   have "1 = exp(2 * of_real pi * \<i> * (of_nat n / of_nat n))"
  2762     using assms by simp
  2763   then have "exp(2 * of_real pi * \<i> * (of_nat j / of_nat n)) = 1 \<longleftrightarrow> j mod n = n mod n"
  2764      using complex_root_unity_eq [of n j n] assms
  2765      by simp
  2766   then show ?thesis
  2767     by auto
  2768 qed
  2769 
  2770 lemma finite_complex_roots_unity_explicit:
  2771      "finite {exp(2 * of_real pi * \<i> * of_nat j / of_nat n) | j::nat. j < n}"
  2772 by simp
  2773 
  2774 lemma card_complex_roots_unity_explicit:
  2775      "card {exp(2 * of_real pi * \<i> * of_nat j / of_nat n) | j::nat. j < n} = n"
  2776   by (simp add:  Finite_Set.bij_betw_same_card [OF bij_betw_roots_unity, symmetric])
  2777 
  2778 lemma complex_roots_unity:
  2779   assumes "1 \<le> n"
  2780     shows "{z::complex. z^n = 1} = {exp(2 * of_real pi * \<i> * of_nat j / of_nat n) | j::nat. j < n}"
  2781   apply (rule Finite_Set.card_seteq [symmetric])
  2782   using assms
  2783   apply (auto simp: card_complex_roots_unity_explicit finite_roots_unity complex_root_unity card_roots_unity)
  2784   done
  2785 
  2786 lemma card_complex_roots_unity: "1 \<le> n \<Longrightarrow> card {z::complex. z^n = 1} = n"
  2787   by (simp add: card_complex_roots_unity_explicit complex_roots_unity)
  2788 
  2789 lemma complex_not_root_unity:
  2790     "1 \<le> n \<Longrightarrow> \<exists>u::complex. norm u = 1 \<and> u^n \<noteq> 1"
  2791   apply (rule_tac x="exp (of_real pi * \<i> * of_real (1 / n))" in exI)
  2792   apply (auto simp: Re_complex_div_eq_0 exp_of_nat_mult [symmetric] mult_ac exp_Euler)
  2793   done
  2794 
  2795 end