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