src/HOL/Complex.thy
author wenzelm
Fri Oct 27 13:50:08 2017 +0200 (22 months ago)
changeset 66924 b4d4027f743b
parent 66793 deabce3ccf1f
child 67082 4e4bea76e559
permissions -rw-r--r--
more permissive;
     1 (*  Title:       HOL/Complex.thy
     2     Author:      Jacques D. Fleuriot, 2001 University of Edinburgh
     3     Author:      Lawrence C Paulson, 2003/4
     4 *)
     5 
     6 section \<open>Complex Numbers: Rectangular and Polar Representations\<close>
     7 
     8 theory Complex
     9 imports Transcendental
    10 begin
    11 
    12 text \<open>
    13   We use the \<^theory_text>\<open>codatatype\<close> command to define the type of complex numbers. This
    14   allows us to use \<^theory_text>\<open>primcorec\<close> to define complex functions by defining their
    15   real and imaginary result separately.
    16 \<close>
    17 
    18 codatatype complex = Complex (Re: real) (Im: real)
    19 
    20 lemma complex_surj: "Complex (Re z) (Im z) = z"
    21   by (rule complex.collapse)
    22 
    23 lemma complex_eqI [intro?]: "Re x = Re y \<Longrightarrow> Im x = Im y \<Longrightarrow> x = y"
    24   by (rule complex.expand) simp
    25 
    26 lemma complex_eq_iff: "x = y \<longleftrightarrow> Re x = Re y \<and> Im x = Im y"
    27   by (auto intro: complex.expand)
    28 
    29 
    30 subsection \<open>Addition and Subtraction\<close>
    31 
    32 instantiation complex :: ab_group_add
    33 begin
    34 
    35 primcorec zero_complex
    36   where
    37     "Re 0 = 0"
    38   | "Im 0 = 0"
    39 
    40 primcorec plus_complex
    41   where
    42     "Re (x + y) = Re x + Re y"
    43   | "Im (x + y) = Im x + Im y"
    44 
    45 primcorec uminus_complex
    46   where
    47     "Re (- x) = - Re x"
    48   | "Im (- x) = - Im x"
    49 
    50 primcorec minus_complex
    51   where
    52     "Re (x - y) = Re x - Re y"
    53   | "Im (x - y) = Im x - Im y"
    54 
    55 instance
    56   by standard (simp_all add: complex_eq_iff)
    57 
    58 end
    59 
    60 
    61 subsection \<open>Multiplication and Division\<close>
    62 
    63 instantiation complex :: field
    64 begin
    65 
    66 primcorec one_complex
    67   where
    68     "Re 1 = 1"
    69   | "Im 1 = 0"
    70 
    71 primcorec times_complex
    72   where
    73     "Re (x * y) = Re x * Re y - Im x * Im y"
    74   | "Im (x * y) = Re x * Im y + Im x * Re y"
    75 
    76 primcorec inverse_complex
    77   where
    78     "Re (inverse x) = Re x / ((Re x)\<^sup>2 + (Im x)\<^sup>2)"
    79   | "Im (inverse x) = - Im x / ((Re x)\<^sup>2 + (Im x)\<^sup>2)"
    80 
    81 definition "x div y = x * inverse y" for x y :: complex
    82 
    83 instance
    84   by standard
    85      (simp_all add: complex_eq_iff divide_complex_def
    86       distrib_left distrib_right right_diff_distrib left_diff_distrib
    87       power2_eq_square add_divide_distrib [symmetric])
    88 
    89 end
    90 
    91 lemma Re_divide: "Re (x / y) = (Re x * Re y + Im x * Im y) / ((Re y)\<^sup>2 + (Im y)\<^sup>2)"
    92   by (simp add: divide_complex_def add_divide_distrib)
    93 
    94 lemma Im_divide: "Im (x / y) = (Im x * Re y - Re x * Im y) / ((Re y)\<^sup>2 + (Im y)\<^sup>2)"
    95   unfolding divide_complex_def times_complex.sel inverse_complex.sel
    96   by (simp add: divide_simps)
    97 
    98 lemma Complex_divide:
    99     "(x / y) = Complex ((Re x * Re y + Im x * Im y) / ((Re y)\<^sup>2 + (Im y)\<^sup>2))
   100                        ((Im x * Re y - Re x * Im y) / ((Re y)\<^sup>2 + (Im y)\<^sup>2))"
   101   by (metis Im_divide Re_divide complex_surj)
   102 
   103 lemma Re_power2: "Re (x ^ 2) = (Re x)^2 - (Im x)^2"
   104   by (simp add: power2_eq_square)
   105 
   106 lemma Im_power2: "Im (x ^ 2) = 2 * Re x * Im x"
   107   by (simp add: power2_eq_square)
   108 
   109 lemma Re_power_real [simp]: "Im x = 0 \<Longrightarrow> Re (x ^ n) = Re x ^ n "
   110   by (induct n) simp_all
   111 
   112 lemma Im_power_real [simp]: "Im x = 0 \<Longrightarrow> Im (x ^ n) = 0"
   113   by (induct n) simp_all
   114 
   115 
   116 subsection \<open>Scalar Multiplication\<close>
   117 
   118 instantiation complex :: real_field
   119 begin
   120 
   121 primcorec scaleR_complex
   122   where
   123     "Re (scaleR r x) = r * Re x"
   124   | "Im (scaleR r x) = r * Im x"
   125 
   126 instance
   127 proof
   128   fix a b :: real and x y :: complex
   129   show "scaleR a (x + y) = scaleR a x + scaleR a y"
   130     by (simp add: complex_eq_iff distrib_left)
   131   show "scaleR (a + b) x = scaleR a x + scaleR b x"
   132     by (simp add: complex_eq_iff distrib_right)
   133   show "scaleR a (scaleR b x) = scaleR (a * b) x"
   134     by (simp add: complex_eq_iff mult.assoc)
   135   show "scaleR 1 x = x"
   136     by (simp add: complex_eq_iff)
   137   show "scaleR a x * y = scaleR a (x * y)"
   138     by (simp add: complex_eq_iff algebra_simps)
   139   show "x * scaleR a y = scaleR a (x * y)"
   140     by (simp add: complex_eq_iff algebra_simps)
   141 qed
   142 
   143 end
   144 
   145 
   146 subsection \<open>Numerals, Arithmetic, and Embedding from R\<close>
   147 
   148 abbreviation complex_of_real :: "real \<Rightarrow> complex"
   149   where "complex_of_real \<equiv> of_real"
   150 
   151 declare [[coercion "of_real :: real \<Rightarrow> complex"]]
   152 declare [[coercion "of_rat :: rat \<Rightarrow> complex"]]
   153 declare [[coercion "of_int :: int \<Rightarrow> complex"]]
   154 declare [[coercion "of_nat :: nat \<Rightarrow> complex"]]
   155 
   156 lemma complex_Re_of_nat [simp]: "Re (of_nat n) = of_nat n"
   157   by (induct n) simp_all
   158 
   159 lemma complex_Im_of_nat [simp]: "Im (of_nat n) = 0"
   160   by (induct n) simp_all
   161 
   162 lemma complex_Re_of_int [simp]: "Re (of_int z) = of_int z"
   163   by (cases z rule: int_diff_cases) simp
   164 
   165 lemma complex_Im_of_int [simp]: "Im (of_int z) = 0"
   166   by (cases z rule: int_diff_cases) simp
   167 
   168 lemma complex_Re_numeral [simp]: "Re (numeral v) = numeral v"
   169   using complex_Re_of_int [of "numeral v"] by simp
   170 
   171 lemma complex_Im_numeral [simp]: "Im (numeral v) = 0"
   172   using complex_Im_of_int [of "numeral v"] by simp
   173 
   174 lemma Re_complex_of_real [simp]: "Re (complex_of_real z) = z"
   175   by (simp add: of_real_def)
   176 
   177 lemma Im_complex_of_real [simp]: "Im (complex_of_real z) = 0"
   178   by (simp add: of_real_def)
   179 
   180 lemma Re_divide_numeral [simp]: "Re (z / numeral w) = Re z / numeral w"
   181   by (simp add: Re_divide sqr_conv_mult)
   182 
   183 lemma Im_divide_numeral [simp]: "Im (z / numeral w) = Im z / numeral w"
   184   by (simp add: Im_divide sqr_conv_mult)
   185 
   186 lemma Re_divide_of_nat [simp]: "Re (z / of_nat n) = Re z / of_nat n"
   187   by (cases n) (simp_all add: Re_divide divide_simps power2_eq_square del: of_nat_Suc)
   188 
   189 lemma Im_divide_of_nat [simp]: "Im (z / of_nat n) = Im z / of_nat n"
   190   by (cases n) (simp_all add: Im_divide divide_simps power2_eq_square del: of_nat_Suc)
   191 
   192 lemma of_real_Re [simp]: "z \<in> \<real> \<Longrightarrow> of_real (Re z) = z"
   193   by (auto simp: Reals_def)
   194 
   195 lemma complex_Re_fact [simp]: "Re (fact n) = fact n"
   196 proof -
   197   have "(fact n :: complex) = of_real (fact n)"
   198     by simp
   199   also have "Re \<dots> = fact n"
   200     by (subst Re_complex_of_real) simp_all
   201   finally show ?thesis .
   202 qed
   203 
   204 lemma complex_Im_fact [simp]: "Im (fact n) = 0"
   205   by (subst of_nat_fact [symmetric]) (simp only: complex_Im_of_nat)
   206 
   207 
   208 subsection \<open>The Complex Number $i$\<close>
   209 
   210 primcorec imaginary_unit :: complex  ("\<i>")
   211   where
   212     "Re \<i> = 0"
   213   | "Im \<i> = 1"
   214 
   215 lemma Complex_eq: "Complex a b = a + \<i> * b"
   216   by (simp add: complex_eq_iff)
   217 
   218 lemma complex_eq: "a = Re a + \<i> * Im a"
   219   by (simp add: complex_eq_iff)
   220 
   221 lemma fun_complex_eq: "f = (\<lambda>x. Re (f x) + \<i> * Im (f x))"
   222   by (simp add: fun_eq_iff complex_eq)
   223 
   224 lemma i_squared [simp]: "\<i> * \<i> = -1"
   225   by (simp add: complex_eq_iff)
   226 
   227 lemma power2_i [simp]: "\<i>\<^sup>2 = -1"
   228   by (simp add: power2_eq_square)
   229 
   230 lemma inverse_i [simp]: "inverse \<i> = - \<i>"
   231   by (rule inverse_unique) simp
   232 
   233 lemma divide_i [simp]: "x / \<i> = - \<i> * x"
   234   by (simp add: divide_complex_def)
   235 
   236 lemma complex_i_mult_minus [simp]: "\<i> * (\<i> * x) = - x"
   237   by (simp add: mult.assoc [symmetric])
   238 
   239 lemma complex_i_not_zero [simp]: "\<i> \<noteq> 0"
   240   by (simp add: complex_eq_iff)
   241 
   242 lemma complex_i_not_one [simp]: "\<i> \<noteq> 1"
   243   by (simp add: complex_eq_iff)
   244 
   245 lemma complex_i_not_numeral [simp]: "\<i> \<noteq> numeral w"
   246   by (simp add: complex_eq_iff)
   247 
   248 lemma complex_i_not_neg_numeral [simp]: "\<i> \<noteq> - numeral w"
   249   by (simp add: complex_eq_iff)
   250 
   251 lemma complex_split_polar: "\<exists>r a. z = complex_of_real r * (cos a + \<i> * sin a)"
   252   by (simp add: complex_eq_iff polar_Ex)
   253 
   254 lemma i_even_power [simp]: "\<i> ^ (n * 2) = (-1) ^ n"
   255   by (metis mult.commute power2_i power_mult)
   256 
   257 lemma Re_i_times [simp]: "Re (\<i> * z) = - Im z"
   258   by simp
   259 
   260 lemma Im_i_times [simp]: "Im (\<i> * z) = Re z"
   261   by simp
   262 
   263 lemma i_times_eq_iff: "\<i> * w = z \<longleftrightarrow> w = - (\<i> * z)"
   264   by auto
   265 
   266 lemma divide_numeral_i [simp]: "z / (numeral n * \<i>) = - (\<i> * z) / numeral n"
   267   by (metis divide_divide_eq_left divide_i mult.commute mult_minus_right)
   268 
   269 lemma imaginary_eq_real_iff [simp]:
   270   assumes "y \<in> Reals" "x \<in> Reals"
   271   shows "\<i> * y = x \<longleftrightarrow> x=0 \<and> y=0"
   272     using assms
   273     unfolding Reals_def
   274     apply clarify
   275       apply (rule iffI)
   276     apply (metis Im_complex_of_real Im_i_times Re_complex_of_real mult_eq_0_iff of_real_0)
   277     by simp
   278 
   279 lemma real_eq_imaginary_iff [simp]:
   280   assumes "y \<in> Reals" "x \<in> Reals"
   281   shows "x = \<i> * y  \<longleftrightarrow> x=0 \<and> y=0"
   282     using assms imaginary_eq_real_iff by fastforce
   283 
   284 subsection \<open>Vector Norm\<close>
   285 
   286 instantiation complex :: real_normed_field
   287 begin
   288 
   289 definition "norm z = sqrt ((Re z)\<^sup>2 + (Im z)\<^sup>2)"
   290 
   291 abbreviation cmod :: "complex \<Rightarrow> real"
   292   where "cmod \<equiv> norm"
   293 
   294 definition complex_sgn_def: "sgn x = x /\<^sub>R cmod x"
   295 
   296 definition dist_complex_def: "dist x y = cmod (x - y)"
   297 
   298 definition uniformity_complex_def [code del]:
   299   "(uniformity :: (complex \<times> complex) filter) = (INF e:{0 <..}. principal {(x, y). dist x y < e})"
   300 
   301 definition open_complex_def [code del]:
   302   "open (U :: complex set) \<longleftrightarrow> (\<forall>x\<in>U. eventually (\<lambda>(x', y). x' = x \<longrightarrow> y \<in> U) uniformity)"
   303 
   304 instance
   305 proof
   306   fix r :: real and x y :: complex and S :: "complex set"
   307   show "(norm x = 0) = (x = 0)"
   308     by (simp add: norm_complex_def complex_eq_iff)
   309   show "norm (x + y) \<le> norm x + norm y"
   310     by (simp add: norm_complex_def complex_eq_iff real_sqrt_sum_squares_triangle_ineq)
   311   show "norm (scaleR r x) = \<bar>r\<bar> * norm x"
   312     by (simp add: norm_complex_def complex_eq_iff power_mult_distrib distrib_left [symmetric]
   313         real_sqrt_mult)
   314   show "norm (x * y) = norm x * norm y"
   315     by (simp add: norm_complex_def complex_eq_iff real_sqrt_mult [symmetric]
   316         power2_eq_square algebra_simps)
   317 qed (rule complex_sgn_def dist_complex_def open_complex_def uniformity_complex_def)+
   318 
   319 end
   320 
   321 declare uniformity_Abort[where 'a = complex, code]
   322 
   323 lemma norm_ii [simp]: "norm \<i> = 1"
   324   by (simp add: norm_complex_def)
   325 
   326 lemma cmod_unit_one: "cmod (cos a + \<i> * sin a) = 1"
   327   by (simp add: norm_complex_def)
   328 
   329 lemma cmod_complex_polar: "cmod (r * (cos a + \<i> * sin a)) = \<bar>r\<bar>"
   330   by (simp add: norm_mult cmod_unit_one)
   331 
   332 lemma complex_Re_le_cmod: "Re x \<le> cmod x"
   333   unfolding norm_complex_def by (rule real_sqrt_sum_squares_ge1)
   334 
   335 lemma complex_mod_minus_le_complex_mod: "- cmod x \<le> cmod x"
   336   by (rule order_trans [OF _ norm_ge_zero]) simp
   337 
   338 lemma complex_mod_triangle_ineq2: "cmod (b + a) - cmod b \<le> cmod a"
   339   by (rule ord_le_eq_trans [OF norm_triangle_ineq2]) simp
   340 
   341 lemma abs_Re_le_cmod: "\<bar>Re x\<bar> \<le> cmod x"
   342   by (simp add: norm_complex_def)
   343 
   344 lemma abs_Im_le_cmod: "\<bar>Im x\<bar> \<le> cmod x"
   345   by (simp add: norm_complex_def)
   346 
   347 lemma cmod_le: "cmod z \<le> \<bar>Re z\<bar> + \<bar>Im z\<bar>"
   348   apply (subst complex_eq)
   349   apply (rule order_trans)
   350    apply (rule norm_triangle_ineq)
   351   apply (simp add: norm_mult)
   352   done
   353 
   354 lemma cmod_eq_Re: "Im z = 0 \<Longrightarrow> cmod z = \<bar>Re z\<bar>"
   355   by (simp add: norm_complex_def)
   356 
   357 lemma cmod_eq_Im: "Re z = 0 \<Longrightarrow> cmod z = \<bar>Im z\<bar>"
   358   by (simp add: norm_complex_def)
   359 
   360 lemma cmod_power2: "(cmod z)\<^sup>2 = (Re z)\<^sup>2 + (Im z)\<^sup>2"
   361   by (simp add: norm_complex_def)
   362 
   363 lemma cmod_plus_Re_le_0_iff: "cmod z + Re z \<le> 0 \<longleftrightarrow> Re z = - cmod z"
   364   using abs_Re_le_cmod[of z] by auto
   365 
   366 lemma cmod_Re_le_iff: "Im x = Im y \<Longrightarrow> cmod x \<le> cmod y \<longleftrightarrow> \<bar>Re x\<bar> \<le> \<bar>Re y\<bar>"
   367   by (metis add.commute add_le_cancel_left norm_complex_def real_sqrt_abs real_sqrt_le_iff)
   368 
   369 lemma cmod_Im_le_iff: "Re x = Re y \<Longrightarrow> cmod x \<le> cmod y \<longleftrightarrow> \<bar>Im x\<bar> \<le> \<bar>Im y\<bar>"
   370   by (metis add_le_cancel_left norm_complex_def real_sqrt_abs real_sqrt_le_iff)
   371 
   372 lemma Im_eq_0: "\<bar>Re z\<bar> = cmod z \<Longrightarrow> Im z = 0"
   373   by (subst (asm) power_eq_iff_eq_base[symmetric, where n=2]) (auto simp add: norm_complex_def)
   374 
   375 lemma abs_sqrt_wlog: "(\<And>x. x \<ge> 0 \<Longrightarrow> P x (x\<^sup>2)) \<Longrightarrow> P \<bar>x\<bar> (x\<^sup>2)"
   376   for x::"'a::linordered_idom"
   377   by (metis abs_ge_zero power2_abs)
   378 
   379 lemma complex_abs_le_norm: "\<bar>Re z\<bar> + \<bar>Im z\<bar> \<le> sqrt 2 * norm z"
   380   unfolding norm_complex_def
   381   apply (rule abs_sqrt_wlog [where x="Re z"])
   382   apply (rule abs_sqrt_wlog [where x="Im z"])
   383   apply (rule power2_le_imp_le)
   384    apply (simp_all add: power2_sum add.commute sum_squares_bound real_sqrt_mult [symmetric])
   385   done
   386 
   387 lemma complex_unit_circle: "z \<noteq> 0 \<Longrightarrow> (Re z / cmod z)\<^sup>2 + (Im z / cmod z)\<^sup>2 = 1"
   388   by (simp add: norm_complex_def divide_simps complex_eq_iff)
   389 
   390 
   391 text \<open>Properties of complex signum.\<close>
   392 
   393 lemma sgn_eq: "sgn z = z / complex_of_real (cmod z)"
   394   by (simp add: sgn_div_norm divide_inverse scaleR_conv_of_real mult.commute)
   395 
   396 lemma Re_sgn [simp]: "Re(sgn z) = Re(z)/cmod z"
   397   by (simp add: complex_sgn_def divide_inverse)
   398 
   399 lemma Im_sgn [simp]: "Im(sgn z) = Im(z)/cmod z"
   400   by (simp add: complex_sgn_def divide_inverse)
   401 
   402 
   403 subsection \<open>Absolute value\<close>
   404 
   405 instantiation complex :: field_abs_sgn
   406 begin
   407 
   408 definition abs_complex :: "complex \<Rightarrow> complex"
   409   where "abs_complex = of_real \<circ> norm"
   410 
   411 instance
   412   apply standard
   413          apply (auto simp add: abs_complex_def complex_sgn_def norm_mult)
   414   apply (auto simp add: scaleR_conv_of_real field_simps)
   415   done
   416 
   417 end
   418 
   419 
   420 subsection \<open>Completeness of the Complexes\<close>
   421 
   422 lemma bounded_linear_Re: "bounded_linear Re"
   423   by (rule bounded_linear_intro [where K=1]) (simp_all add: norm_complex_def)
   424 
   425 lemma bounded_linear_Im: "bounded_linear Im"
   426   by (rule bounded_linear_intro [where K=1]) (simp_all add: norm_complex_def)
   427 
   428 lemmas Cauchy_Re = bounded_linear.Cauchy [OF bounded_linear_Re]
   429 lemmas Cauchy_Im = bounded_linear.Cauchy [OF bounded_linear_Im]
   430 lemmas tendsto_Re [tendsto_intros] = bounded_linear.tendsto [OF bounded_linear_Re]
   431 lemmas tendsto_Im [tendsto_intros] = bounded_linear.tendsto [OF bounded_linear_Im]
   432 lemmas isCont_Re [simp] = bounded_linear.isCont [OF bounded_linear_Re]
   433 lemmas isCont_Im [simp] = bounded_linear.isCont [OF bounded_linear_Im]
   434 lemmas continuous_Re [simp] = bounded_linear.continuous [OF bounded_linear_Re]
   435 lemmas continuous_Im [simp] = bounded_linear.continuous [OF bounded_linear_Im]
   436 lemmas continuous_on_Re [continuous_intros] = bounded_linear.continuous_on[OF bounded_linear_Re]
   437 lemmas continuous_on_Im [continuous_intros] = bounded_linear.continuous_on[OF bounded_linear_Im]
   438 lemmas has_derivative_Re [derivative_intros] = bounded_linear.has_derivative[OF bounded_linear_Re]
   439 lemmas has_derivative_Im [derivative_intros] = bounded_linear.has_derivative[OF bounded_linear_Im]
   440 lemmas sums_Re = bounded_linear.sums [OF bounded_linear_Re]
   441 lemmas sums_Im = bounded_linear.sums [OF bounded_linear_Im]
   442 
   443 lemma tendsto_Complex [tendsto_intros]:
   444   "(f \<longlongrightarrow> a) F \<Longrightarrow> (g \<longlongrightarrow> b) F \<Longrightarrow> ((\<lambda>x. Complex (f x) (g x)) \<longlongrightarrow> Complex a b) F"
   445   unfolding Complex_eq by (auto intro!: tendsto_intros)
   446 
   447 lemma tendsto_complex_iff:
   448   "(f \<longlongrightarrow> x) F \<longleftrightarrow> (((\<lambda>x. Re (f x)) \<longlongrightarrow> Re x) F \<and> ((\<lambda>x. Im (f x)) \<longlongrightarrow> Im x) F)"
   449 proof safe
   450   assume "((\<lambda>x. Re (f x)) \<longlongrightarrow> Re x) F" "((\<lambda>x. Im (f x)) \<longlongrightarrow> Im x) F"
   451   from tendsto_Complex[OF this] show "(f \<longlongrightarrow> x) F"
   452     unfolding complex.collapse .
   453 qed (auto intro: tendsto_intros)
   454 
   455 lemma continuous_complex_iff:
   456   "continuous F f \<longleftrightarrow> continuous F (\<lambda>x. Re (f x)) \<and> continuous F (\<lambda>x. Im (f x))"
   457   by (simp only: continuous_def tendsto_complex_iff)
   458 
   459 lemma continuous_on_of_real_o_iff [simp]:
   460      "continuous_on S (\<lambda>x. complex_of_real (g x)) = continuous_on S g"
   461   using continuous_on_Re continuous_on_of_real  by fastforce
   462 
   463 lemma continuous_on_of_real_id [simp]:
   464      "continuous_on S (of_real :: real \<Rightarrow> 'a::real_normed_algebra_1)"
   465   by (rule continuous_on_of_real [OF continuous_on_id])
   466 
   467 lemma has_vector_derivative_complex_iff: "(f has_vector_derivative x) F \<longleftrightarrow>
   468     ((\<lambda>x. Re (f x)) has_field_derivative (Re x)) F \<and>
   469     ((\<lambda>x. Im (f x)) has_field_derivative (Im x)) F"
   470   by (simp add: has_vector_derivative_def has_field_derivative_def has_derivative_def
   471       tendsto_complex_iff field_simps bounded_linear_scaleR_left bounded_linear_mult_right)
   472 
   473 lemma has_field_derivative_Re[derivative_intros]:
   474   "(f has_vector_derivative D) F \<Longrightarrow> ((\<lambda>x. Re (f x)) has_field_derivative (Re D)) F"
   475   unfolding has_vector_derivative_complex_iff by safe
   476 
   477 lemma has_field_derivative_Im[derivative_intros]:
   478   "(f has_vector_derivative D) F \<Longrightarrow> ((\<lambda>x. Im (f x)) has_field_derivative (Im D)) F"
   479   unfolding has_vector_derivative_complex_iff by safe
   480 
   481 instance complex :: banach
   482 proof
   483   fix X :: "nat \<Rightarrow> complex"
   484   assume X: "Cauchy X"
   485   then have "(\<lambda>n. Complex (Re (X n)) (Im (X n))) \<longlonglongrightarrow>
   486     Complex (lim (\<lambda>n. Re (X n))) (lim (\<lambda>n. Im (X n)))"
   487     by (intro tendsto_Complex convergent_LIMSEQ_iff[THEN iffD1]
   488         Cauchy_convergent_iff[THEN iffD1] Cauchy_Re Cauchy_Im)
   489   then show "convergent X"
   490     unfolding complex.collapse by (rule convergentI)
   491 qed
   492 
   493 declare DERIV_power[where 'a=complex, unfolded of_nat_def[symmetric], derivative_intros]
   494 
   495 
   496 subsection \<open>Complex Conjugation\<close>
   497 
   498 primcorec cnj :: "complex \<Rightarrow> complex"
   499   where
   500     "Re (cnj z) = Re z"
   501   | "Im (cnj z) = - Im z"
   502 
   503 lemma complex_cnj_cancel_iff [simp]: "cnj x = cnj y \<longleftrightarrow> x = y"
   504   by (simp add: complex_eq_iff)
   505 
   506 lemma complex_cnj_cnj [simp]: "cnj (cnj z) = z"
   507   by (simp add: complex_eq_iff)
   508 
   509 lemma complex_cnj_zero [simp]: "cnj 0 = 0"
   510   by (simp add: complex_eq_iff)
   511 
   512 lemma complex_cnj_zero_iff [iff]: "cnj z = 0 \<longleftrightarrow> z = 0"
   513   by (simp add: complex_eq_iff)
   514 
   515 lemma complex_cnj_add [simp]: "cnj (x + y) = cnj x + cnj y"
   516   by (simp add: complex_eq_iff)
   517 
   518 lemma cnj_sum [simp]: "cnj (sum f s) = (\<Sum>x\<in>s. cnj (f x))"
   519   by (induct s rule: infinite_finite_induct) auto
   520 
   521 lemma complex_cnj_diff [simp]: "cnj (x - y) = cnj x - cnj y"
   522   by (simp add: complex_eq_iff)
   523 
   524 lemma complex_cnj_minus [simp]: "cnj (- x) = - cnj x"
   525   by (simp add: complex_eq_iff)
   526 
   527 lemma complex_cnj_one [simp]: "cnj 1 = 1"
   528   by (simp add: complex_eq_iff)
   529 
   530 lemma complex_cnj_mult [simp]: "cnj (x * y) = cnj x * cnj y"
   531   by (simp add: complex_eq_iff)
   532 
   533 lemma cnj_prod [simp]: "cnj (prod f s) = (\<Prod>x\<in>s. cnj (f x))"
   534   by (induct s rule: infinite_finite_induct) auto
   535 
   536 lemma complex_cnj_inverse [simp]: "cnj (inverse x) = inverse (cnj x)"
   537   by (simp add: complex_eq_iff)
   538 
   539 lemma complex_cnj_divide [simp]: "cnj (x / y) = cnj x / cnj y"
   540   by (simp add: divide_complex_def)
   541 
   542 lemma complex_cnj_power [simp]: "cnj (x ^ n) = cnj x ^ n"
   543   by (induct n) simp_all
   544 
   545 lemma complex_cnj_of_nat [simp]: "cnj (of_nat n) = of_nat n"
   546   by (simp add: complex_eq_iff)
   547 
   548 lemma complex_cnj_of_int [simp]: "cnj (of_int z) = of_int z"
   549   by (simp add: complex_eq_iff)
   550 
   551 lemma complex_cnj_numeral [simp]: "cnj (numeral w) = numeral w"
   552   by (simp add: complex_eq_iff)
   553 
   554 lemma complex_cnj_neg_numeral [simp]: "cnj (- numeral w) = - numeral w"
   555   by (simp add: complex_eq_iff)
   556 
   557 lemma complex_cnj_scaleR [simp]: "cnj (scaleR r x) = scaleR r (cnj x)"
   558   by (simp add: complex_eq_iff)
   559 
   560 lemma complex_mod_cnj [simp]: "cmod (cnj z) = cmod z"
   561   by (simp add: norm_complex_def)
   562 
   563 lemma complex_cnj_complex_of_real [simp]: "cnj (of_real x) = of_real x"
   564   by (simp add: complex_eq_iff)
   565 
   566 lemma complex_cnj_i [simp]: "cnj \<i> = - \<i>"
   567   by (simp add: complex_eq_iff)
   568 
   569 lemma complex_add_cnj: "z + cnj z = complex_of_real (2 * Re z)"
   570   by (simp add: complex_eq_iff)
   571 
   572 lemma complex_diff_cnj: "z - cnj z = complex_of_real (2 * Im z) * \<i>"
   573   by (simp add: complex_eq_iff)
   574 
   575 lemma complex_mult_cnj: "z * cnj z = complex_of_real ((Re z)\<^sup>2 + (Im z)\<^sup>2)"
   576   by (simp add: complex_eq_iff power2_eq_square)
   577 
   578 lemma complex_mod_mult_cnj: "cmod (z * cnj z) = (cmod z)\<^sup>2"
   579   by (simp add: norm_mult power2_eq_square)
   580 
   581 lemma complex_mod_sqrt_Re_mult_cnj: "cmod z = sqrt (Re (z * cnj z))"
   582   by (simp add: norm_complex_def power2_eq_square)
   583 
   584 lemma complex_In_mult_cnj_zero [simp]: "Im (z * cnj z) = 0"
   585   by simp
   586 
   587 lemma complex_cnj_fact [simp]: "cnj (fact n) = fact n"
   588   by (subst of_nat_fact [symmetric], subst complex_cnj_of_nat) simp
   589 
   590 lemma complex_cnj_pochhammer [simp]: "cnj (pochhammer z n) = pochhammer (cnj z) n"
   591   by (induct n arbitrary: z) (simp_all add: pochhammer_rec)
   592 
   593 lemma bounded_linear_cnj: "bounded_linear cnj"
   594   using complex_cnj_add complex_cnj_scaleR by (rule bounded_linear_intro [where K=1]) simp
   595 
   596 lemmas tendsto_cnj [tendsto_intros] = bounded_linear.tendsto [OF bounded_linear_cnj]
   597   and isCont_cnj [simp] = bounded_linear.isCont [OF bounded_linear_cnj]
   598   and continuous_cnj [simp, continuous_intros] = bounded_linear.continuous [OF bounded_linear_cnj]
   599   and continuous_on_cnj [simp, continuous_intros] = bounded_linear.continuous_on [OF bounded_linear_cnj]
   600   and has_derivative_cnj [simp, derivative_intros] = bounded_linear.has_derivative [OF bounded_linear_cnj]
   601 
   602 lemma lim_cnj: "((\<lambda>x. cnj(f x)) \<longlongrightarrow> cnj l) F \<longleftrightarrow> (f \<longlongrightarrow> l) F"
   603   by (simp add: tendsto_iff dist_complex_def complex_cnj_diff [symmetric] del: complex_cnj_diff)
   604 
   605 lemma sums_cnj: "((\<lambda>x. cnj(f x)) sums cnj l) \<longleftrightarrow> (f sums l)"
   606   by (simp add: sums_def lim_cnj cnj_sum [symmetric] del: cnj_sum)
   607 
   608 
   609 subsection \<open>Basic Lemmas\<close>
   610 
   611 lemma complex_eq_0: "z=0 \<longleftrightarrow> (Re z)\<^sup>2 + (Im z)\<^sup>2 = 0"
   612   by (metis zero_complex.sel complex_eqI sum_power2_eq_zero_iff)
   613 
   614 lemma complex_neq_0: "z\<noteq>0 \<longleftrightarrow> (Re z)\<^sup>2 + (Im z)\<^sup>2 > 0"
   615   by (metis complex_eq_0 less_numeral_extra(3) sum_power2_gt_zero_iff)
   616 
   617 lemma complex_norm_square: "of_real ((norm z)\<^sup>2) = z * cnj z"
   618   by (cases z)
   619     (auto simp: complex_eq_iff norm_complex_def power2_eq_square[symmetric] of_real_power[symmetric]
   620       simp del: of_real_power)
   621 
   622 lemma complex_div_cnj: "a / b = (a * cnj b) / (norm b)\<^sup>2"
   623   using complex_norm_square by auto
   624 
   625 lemma Re_complex_div_eq_0: "Re (a / b) = 0 \<longleftrightarrow> Re (a * cnj b) = 0"
   626   by (auto simp add: Re_divide)
   627 
   628 lemma Im_complex_div_eq_0: "Im (a / b) = 0 \<longleftrightarrow> Im (a * cnj b) = 0"
   629   by (auto simp add: Im_divide)
   630 
   631 lemma complex_div_gt_0: "(Re (a / b) > 0 \<longleftrightarrow> Re (a * cnj b) > 0) \<and> (Im (a / b) > 0 \<longleftrightarrow> Im (a * cnj b) > 0)"
   632 proof (cases "b = 0")
   633   case True
   634   then show ?thesis by auto
   635 next
   636   case False
   637   then have "0 < (Re b)\<^sup>2 + (Im b)\<^sup>2"
   638     by (simp add: complex_eq_iff sum_power2_gt_zero_iff)
   639   then show ?thesis
   640     by (simp add: Re_divide Im_divide zero_less_divide_iff)
   641 qed
   642 
   643 lemma Re_complex_div_gt_0: "Re (a / b) > 0 \<longleftrightarrow> Re (a * cnj b) > 0"
   644   and Im_complex_div_gt_0: "Im (a / b) > 0 \<longleftrightarrow> Im (a * cnj b) > 0"
   645   using complex_div_gt_0 by auto
   646 
   647 lemma Re_complex_div_ge_0: "Re (a / b) \<ge> 0 \<longleftrightarrow> Re (a * cnj b) \<ge> 0"
   648   by (metis le_less Re_complex_div_eq_0 Re_complex_div_gt_0)
   649 
   650 lemma Im_complex_div_ge_0: "Im (a / b) \<ge> 0 \<longleftrightarrow> Im (a * cnj b) \<ge> 0"
   651   by (metis Im_complex_div_eq_0 Im_complex_div_gt_0 le_less)
   652 
   653 lemma Re_complex_div_lt_0: "Re (a / b) < 0 \<longleftrightarrow> Re (a * cnj b) < 0"
   654   by (metis less_asym neq_iff Re_complex_div_eq_0 Re_complex_div_gt_0)
   655 
   656 lemma Im_complex_div_lt_0: "Im (a / b) < 0 \<longleftrightarrow> Im (a * cnj b) < 0"
   657   by (metis Im_complex_div_eq_0 Im_complex_div_gt_0 less_asym neq_iff)
   658 
   659 lemma Re_complex_div_le_0: "Re (a / b) \<le> 0 \<longleftrightarrow> Re (a * cnj b) \<le> 0"
   660   by (metis not_le Re_complex_div_gt_0)
   661 
   662 lemma Im_complex_div_le_0: "Im (a / b) \<le> 0 \<longleftrightarrow> Im (a * cnj b) \<le> 0"
   663   by (metis Im_complex_div_gt_0 not_le)
   664 
   665 lemma Re_divide_of_real [simp]: "Re (z / of_real r) = Re z / r"
   666   by (simp add: Re_divide power2_eq_square)
   667 
   668 lemma Im_divide_of_real [simp]: "Im (z / of_real r) = Im z / r"
   669   by (simp add: Im_divide power2_eq_square)
   670 
   671 lemma Re_divide_Reals [simp]: "r \<in> \<real> \<Longrightarrow> Re (z / r) = Re z / Re r"
   672   by (metis Re_divide_of_real of_real_Re)
   673 
   674 lemma Im_divide_Reals [simp]: "r \<in> \<real> \<Longrightarrow> Im (z / r) = Im z / Re r"
   675   by (metis Im_divide_of_real of_real_Re)
   676 
   677 lemma Re_sum[simp]: "Re (sum f s) = (\<Sum>x\<in>s. Re (f x))"
   678   by (induct s rule: infinite_finite_induct) auto
   679 
   680 lemma Im_sum[simp]: "Im (sum f s) = (\<Sum>x\<in>s. Im(f x))"
   681   by (induct s rule: infinite_finite_induct) auto
   682 
   683 lemma sums_complex_iff: "f sums x \<longleftrightarrow> ((\<lambda>x. Re (f x)) sums Re x) \<and> ((\<lambda>x. Im (f x)) sums Im x)"
   684   unfolding sums_def tendsto_complex_iff Im_sum Re_sum ..
   685 
   686 lemma summable_complex_iff: "summable f \<longleftrightarrow> summable (\<lambda>x. Re (f x)) \<and>  summable (\<lambda>x. Im (f x))"
   687   unfolding summable_def sums_complex_iff[abs_def] by (metis complex.sel)
   688 
   689 lemma summable_complex_of_real [simp]: "summable (\<lambda>n. complex_of_real (f n)) \<longleftrightarrow> summable f"
   690   unfolding summable_complex_iff by simp
   691 
   692 lemma summable_Re: "summable f \<Longrightarrow> summable (\<lambda>x. Re (f x))"
   693   unfolding summable_complex_iff by blast
   694 
   695 lemma summable_Im: "summable f \<Longrightarrow> summable (\<lambda>x. Im (f x))"
   696   unfolding summable_complex_iff by blast
   697 
   698 lemma complex_is_Nat_iff: "z \<in> \<nat> \<longleftrightarrow> Im z = 0 \<and> (\<exists>i. Re z = of_nat i)"
   699   by (auto simp: Nats_def complex_eq_iff)
   700 
   701 lemma complex_is_Int_iff: "z \<in> \<int> \<longleftrightarrow> Im z = 0 \<and> (\<exists>i. Re z = of_int i)"
   702   by (auto simp: Ints_def complex_eq_iff)
   703 
   704 lemma complex_is_Real_iff: "z \<in> \<real> \<longleftrightarrow> Im z = 0"
   705   by (auto simp: Reals_def complex_eq_iff)
   706 
   707 lemma Reals_cnj_iff: "z \<in> \<real> \<longleftrightarrow> cnj z = z"
   708   by (auto simp: complex_is_Real_iff complex_eq_iff)
   709 
   710 lemma in_Reals_norm: "z \<in> \<real> \<Longrightarrow> norm z = \<bar>Re z\<bar>"
   711   by (simp add: complex_is_Real_iff norm_complex_def)
   712 
   713 lemma Re_Reals_divide: "r \<in> \<real> \<Longrightarrow> Re (r / z) = Re r * Re z / (norm z)\<^sup>2"
   714   by (simp add: Re_divide complex_is_Real_iff cmod_power2)
   715 
   716 lemma Im_Reals_divide: "r \<in> \<real> \<Longrightarrow> Im (r / z) = -Re r * Im z / (norm z)\<^sup>2"
   717   by (simp add: Im_divide complex_is_Real_iff cmod_power2)
   718 
   719 lemma series_comparison_complex:
   720   fixes f:: "nat \<Rightarrow> 'a::banach"
   721   assumes sg: "summable g"
   722     and "\<And>n. g n \<in> \<real>" "\<And>n. Re (g n) \<ge> 0"
   723     and fg: "\<And>n. n \<ge> N \<Longrightarrow> norm(f n) \<le> norm(g n)"
   724   shows "summable f"
   725 proof -
   726   have g: "\<And>n. cmod (g n) = Re (g n)"
   727     using assms by (metis abs_of_nonneg in_Reals_norm)
   728   show ?thesis
   729     apply (rule summable_comparison_test' [where g = "\<lambda>n. norm (g n)" and N=N])
   730     using sg
   731      apply (auto simp: summable_def)
   732      apply (rule_tac x = "Re s" in exI)
   733      apply (auto simp: g sums_Re)
   734     apply (metis fg g)
   735     done
   736 qed
   737 
   738 
   739 subsection \<open>Polar Form for Complex Numbers\<close>
   740 
   741 lemma complex_unimodular_polar:
   742   assumes "norm z = 1"
   743   obtains t where "0 \<le> t" "t < 2 * pi" "z = Complex (cos t) (sin t)"
   744   by (metis cmod_power2 one_power2 complex_surj sincos_total_2pi [of "Re z" "Im z"] assms)
   745 
   746 
   747 subsubsection \<open>$\cos \theta + i \sin \theta$\<close>
   748 
   749 primcorec cis :: "real \<Rightarrow> complex"
   750   where
   751     "Re (cis a) = cos a"
   752   | "Im (cis a) = sin a"
   753 
   754 lemma cis_zero [simp]: "cis 0 = 1"
   755   by (simp add: complex_eq_iff)
   756 
   757 lemma norm_cis [simp]: "norm (cis a) = 1"
   758   by (simp add: norm_complex_def)
   759 
   760 lemma sgn_cis [simp]: "sgn (cis a) = cis a"
   761   by (simp add: sgn_div_norm)
   762 
   763 lemma cis_neq_zero [simp]: "cis a \<noteq> 0"
   764   by (metis norm_cis norm_zero zero_neq_one)
   765 
   766 lemma cis_mult: "cis a * cis b = cis (a + b)"
   767   by (simp add: complex_eq_iff cos_add sin_add)
   768 
   769 lemma DeMoivre: "(cis a) ^ n = cis (real n * a)"
   770   by (induct n) (simp_all add: algebra_simps cis_mult)
   771 
   772 lemma cis_inverse [simp]: "inverse (cis a) = cis (- a)"
   773   by (simp add: complex_eq_iff)
   774 
   775 lemma cis_divide: "cis a / cis b = cis (a - b)"
   776   by (simp add: divide_complex_def cis_mult)
   777 
   778 lemma cos_n_Re_cis_pow_n: "cos (real n * a) = Re (cis a ^ n)"
   779   by (auto simp add: DeMoivre)
   780 
   781 lemma sin_n_Im_cis_pow_n: "sin (real n * a) = Im (cis a ^ n)"
   782   by (auto simp add: DeMoivre)
   783 
   784 lemma cis_pi: "cis pi = -1"
   785   by (simp add: complex_eq_iff)
   786 
   787 
   788 subsubsection \<open>$r(\cos \theta + i \sin \theta)$\<close>
   789 
   790 definition rcis :: "real \<Rightarrow> real \<Rightarrow> complex"
   791   where "rcis r a = complex_of_real r * cis a"
   792 
   793 lemma Re_rcis [simp]: "Re(rcis r a) = r * cos a"
   794   by (simp add: rcis_def)
   795 
   796 lemma Im_rcis [simp]: "Im(rcis r a) = r * sin a"
   797   by (simp add: rcis_def)
   798 
   799 lemma rcis_Ex: "\<exists>r a. z = rcis r a"
   800   by (simp add: complex_eq_iff polar_Ex)
   801 
   802 lemma complex_mod_rcis [simp]: "cmod (rcis r a) = \<bar>r\<bar>"
   803   by (simp add: rcis_def norm_mult)
   804 
   805 lemma cis_rcis_eq: "cis a = rcis 1 a"
   806   by (simp add: rcis_def)
   807 
   808 lemma rcis_mult: "rcis r1 a * rcis r2 b = rcis (r1 * r2) (a + b)"
   809   by (simp add: rcis_def cis_mult)
   810 
   811 lemma rcis_zero_mod [simp]: "rcis 0 a = 0"
   812   by (simp add: rcis_def)
   813 
   814 lemma rcis_zero_arg [simp]: "rcis r 0 = complex_of_real r"
   815   by (simp add: rcis_def)
   816 
   817 lemma rcis_eq_zero_iff [simp]: "rcis r a = 0 \<longleftrightarrow> r = 0"
   818   by (simp add: rcis_def)
   819 
   820 lemma DeMoivre2: "(rcis r a) ^ n = rcis (r ^ n) (real n * a)"
   821   by (simp add: rcis_def power_mult_distrib DeMoivre)
   822 
   823 lemma rcis_inverse: "inverse(rcis r a) = rcis (1 / r) (- a)"
   824   by (simp add: divide_inverse rcis_def)
   825 
   826 lemma rcis_divide: "rcis r1 a / rcis r2 b = rcis (r1 / r2) (a - b)"
   827   by (simp add: rcis_def cis_divide [symmetric])
   828 
   829 
   830 subsubsection \<open>Complex exponential\<close>
   831 
   832 lemma cis_conv_exp: "cis b = exp (\<i> * b)"
   833 proof -
   834   have "(\<i> * complex_of_real b) ^ n /\<^sub>R fact n =
   835       of_real (cos_coeff n * b^n) + \<i> * of_real (sin_coeff n * b^n)"
   836     for n :: nat
   837   proof -
   838     have "\<i> ^ n = fact n *\<^sub>R (cos_coeff n + \<i> * sin_coeff n)"
   839       by (induct n)
   840         (simp_all add: sin_coeff_Suc cos_coeff_Suc complex_eq_iff Re_divide Im_divide field_simps
   841           power2_eq_square add_nonneg_eq_0_iff)
   842     then show ?thesis
   843       by (simp add: field_simps)
   844   qed
   845   then show ?thesis
   846     using sin_converges [of b] cos_converges [of b]
   847     by (auto simp add: Complex_eq cis.ctr exp_def simp del: of_real_mult
   848         intro!: sums_unique sums_add sums_mult sums_of_real)
   849 qed
   850 
   851 lemma exp_eq_polar: "exp z = exp (Re z) * cis (Im z)"
   852   unfolding cis_conv_exp exp_of_real [symmetric] mult_exp_exp
   853   by (cases z) (simp add: Complex_eq)
   854 
   855 lemma Re_exp: "Re (exp z) = exp (Re z) * cos (Im z)"
   856   unfolding exp_eq_polar by simp
   857 
   858 lemma Im_exp: "Im (exp z) = exp (Re z) * sin (Im z)"
   859   unfolding exp_eq_polar by simp
   860 
   861 lemma norm_cos_sin [simp]: "norm (Complex (cos t) (sin t)) = 1"
   862   by (simp add: norm_complex_def)
   863 
   864 lemma norm_exp_eq_Re [simp]: "norm (exp z) = exp (Re z)"
   865   by (simp add: cis.code cmod_complex_polar exp_eq_polar Complex_eq)
   866 
   867 lemma complex_exp_exists: "\<exists>a r. z = complex_of_real r * exp a"
   868   apply (insert rcis_Ex [of z])
   869   apply (auto simp add: exp_eq_polar rcis_def mult.assoc [symmetric])
   870   apply (rule_tac x = "\<i> * complex_of_real a" in exI)
   871   apply auto
   872   done
   873 
   874 lemma exp_pi_i [simp]: "exp (of_real pi * \<i>) = -1"
   875   by (metis cis_conv_exp cis_pi mult.commute)
   876 
   877 lemma exp_pi_i' [simp]: "exp (\<i> * of_real pi) = -1"
   878   using cis_conv_exp cis_pi by auto
   879 
   880 lemma exp_two_pi_i [simp]: "exp (2 * of_real pi * \<i>) = 1"
   881   by (simp add: exp_eq_polar complex_eq_iff)
   882 
   883 lemma exp_two_pi_i' [simp]: "exp (\<i> * (of_real pi * 2)) = 1"
   884   by (metis exp_two_pi_i mult.commute)
   885 
   886 
   887 subsubsection \<open>Complex argument\<close>
   888 
   889 definition arg :: "complex \<Rightarrow> real"
   890   where "arg z = (if z = 0 then 0 else (SOME a. sgn z = cis a \<and> - pi < a \<and> a \<le> pi))"
   891 
   892 lemma arg_zero: "arg 0 = 0"
   893   by (simp add: arg_def)
   894 
   895 lemma arg_unique:
   896   assumes "sgn z = cis x" and "-pi < x" and "x \<le> pi"
   897   shows "arg z = x"
   898 proof -
   899   from assms have "z \<noteq> 0" by auto
   900   have "(SOME a. sgn z = cis a \<and> -pi < a \<and> a \<le> pi) = x"
   901   proof
   902     fix a
   903     define d where "d = a - x"
   904     assume a: "sgn z = cis a \<and> - pi < a \<and> a \<le> pi"
   905     from a assms have "- (2*pi) < d \<and> d < 2*pi"
   906       unfolding d_def by simp
   907     moreover
   908     from a assms have "cos a = cos x" and "sin a = sin x"
   909       by (simp_all add: complex_eq_iff)
   910     then have cos: "cos d = 1"
   911       by (simp add: d_def cos_diff)
   912     moreover from cos have "sin d = 0"
   913       by (rule cos_one_sin_zero)
   914     ultimately have "d = 0"
   915       by (auto simp: sin_zero_iff elim!: evenE dest!: less_2_cases)
   916     then show "a = x"
   917       by (simp add: d_def)
   918   qed (simp add: assms del: Re_sgn Im_sgn)
   919   with \<open>z \<noteq> 0\<close> show "arg z = x"
   920     by (simp add: arg_def)
   921 qed
   922 
   923 lemma arg_correct:
   924   assumes "z \<noteq> 0"
   925   shows "sgn z = cis (arg z) \<and> -pi < arg z \<and> arg z \<le> pi"
   926 proof (simp add: arg_def assms, rule someI_ex)
   927   obtain r a where z: "z = rcis r a"
   928     using rcis_Ex by fast
   929   with assms have "r \<noteq> 0" by auto
   930   define b where "b = (if 0 < r then a else a + pi)"
   931   have b: "sgn z = cis b"
   932     using \<open>r \<noteq> 0\<close> by (simp add: z b_def rcis_def of_real_def sgn_scaleR sgn_if complex_eq_iff)
   933   have cis_2pi_nat: "cis (2 * pi * real_of_nat n) = 1" for n
   934     by (induct n) (simp_all add: distrib_left cis_mult [symmetric] complex_eq_iff)
   935   have cis_2pi_int: "cis (2 * pi * real_of_int x) = 1" for x
   936     by (cases x rule: int_diff_cases)
   937       (simp add: right_diff_distrib cis_divide [symmetric] cis_2pi_nat)
   938   define c where "c = b - 2 * pi * of_int \<lceil>(b - pi) / (2 * pi)\<rceil>"
   939   have "sgn z = cis c"
   940     by (simp add: b c_def cis_divide [symmetric] cis_2pi_int)
   941   moreover have "- pi < c \<and> c \<le> pi"
   942     using ceiling_correct [of "(b - pi) / (2*pi)"]
   943     by (simp add: c_def less_divide_eq divide_le_eq algebra_simps del: le_of_int_ceiling)
   944   ultimately show "\<exists>a. sgn z = cis a \<and> -pi < a \<and> a \<le> pi"
   945     by fast
   946 qed
   947 
   948 lemma arg_bounded: "- pi < arg z \<and> arg z \<le> pi"
   949   by (cases "z = 0") (simp_all add: arg_zero arg_correct)
   950 
   951 lemma cis_arg: "z \<noteq> 0 \<Longrightarrow> cis (arg z) = sgn z"
   952   by (simp add: arg_correct)
   953 
   954 lemma rcis_cmod_arg: "rcis (cmod z) (arg z) = z"
   955   by (cases "z = 0") (simp_all add: rcis_def cis_arg sgn_div_norm of_real_def)
   956 
   957 lemma cos_arg_i_mult_zero [simp]: "y \<noteq> 0 \<Longrightarrow> Re y = 0 \<Longrightarrow> cos (arg y) = 0"
   958   using cis_arg [of y] by (simp add: complex_eq_iff)
   959 
   960 
   961 subsection \<open>Square root of complex numbers\<close>
   962 
   963 primcorec csqrt :: "complex \<Rightarrow> complex"
   964   where
   965     "Re (csqrt z) = sqrt ((cmod z + Re z) / 2)"
   966   | "Im (csqrt z) = (if Im z = 0 then 1 else sgn (Im z)) * sqrt ((cmod z - Re z) / 2)"
   967 
   968 lemma csqrt_of_real_nonneg [simp]: "Im x = 0 \<Longrightarrow> Re x \<ge> 0 \<Longrightarrow> csqrt x = sqrt (Re x)"
   969   by (simp add: complex_eq_iff norm_complex_def)
   970 
   971 lemma csqrt_of_real_nonpos [simp]: "Im x = 0 \<Longrightarrow> Re x \<le> 0 \<Longrightarrow> csqrt x = \<i> * sqrt \<bar>Re x\<bar>"
   972   by (simp add: complex_eq_iff norm_complex_def)
   973 
   974 lemma of_real_sqrt: "x \<ge> 0 \<Longrightarrow> of_real (sqrt x) = csqrt (of_real x)"
   975   by (simp add: complex_eq_iff norm_complex_def)
   976 
   977 lemma csqrt_0 [simp]: "csqrt 0 = 0"
   978   by simp
   979 
   980 lemma csqrt_1 [simp]: "csqrt 1 = 1"
   981   by simp
   982 
   983 lemma csqrt_ii [simp]: "csqrt \<i> = (1 + \<i>) / sqrt 2"
   984   by (simp add: complex_eq_iff Re_divide Im_divide real_sqrt_divide real_div_sqrt)
   985 
   986 lemma power2_csqrt[simp,algebra]: "(csqrt z)\<^sup>2 = z"
   987 proof (cases "Im z = 0")
   988   case True
   989   then show ?thesis
   990     using real_sqrt_pow2[of "Re z"] real_sqrt_pow2[of "- Re z"]
   991     by (cases "0::real" "Re z" rule: linorder_cases)
   992       (simp_all add: complex_eq_iff Re_power2 Im_power2 power2_eq_square cmod_eq_Re)
   993 next
   994   case False
   995   moreover have "cmod z * cmod z - Re z * Re z = Im z * Im z"
   996     by (simp add: norm_complex_def power2_eq_square)
   997   moreover have "\<bar>Re z\<bar> \<le> cmod z"
   998     by (simp add: norm_complex_def)
   999   ultimately show ?thesis
  1000     by (simp add: Re_power2 Im_power2 complex_eq_iff real_sgn_eq
  1001         field_simps real_sqrt_mult[symmetric] real_sqrt_divide)
  1002 qed
  1003 
  1004 lemma csqrt_eq_0 [simp]: "csqrt z = 0 \<longleftrightarrow> z = 0"
  1005   by auto (metis power2_csqrt power_eq_0_iff)
  1006 
  1007 lemma csqrt_eq_1 [simp]: "csqrt z = 1 \<longleftrightarrow> z = 1"
  1008   by auto (metis power2_csqrt power2_eq_1_iff)
  1009 
  1010 lemma csqrt_principal: "0 < Re (csqrt z) \<or> Re (csqrt z) = 0 \<and> 0 \<le> Im (csqrt z)"
  1011   by (auto simp add: not_less cmod_plus_Re_le_0_iff Im_eq_0)
  1012 
  1013 lemma Re_csqrt: "0 \<le> Re (csqrt z)"
  1014   by (metis csqrt_principal le_less)
  1015 
  1016 lemma csqrt_square:
  1017   assumes "0 < Re b \<or> (Re b = 0 \<and> 0 \<le> Im b)"
  1018   shows "csqrt (b^2) = b"
  1019 proof -
  1020   have "csqrt (b^2) = b \<or> csqrt (b^2) = - b"
  1021     by (simp add: power2_eq_iff[symmetric])
  1022   moreover have "csqrt (b^2) \<noteq> -b \<or> b = 0"
  1023     using csqrt_principal[of "b ^ 2"] assms
  1024     by (intro disjCI notI) (auto simp: complex_eq_iff)
  1025   ultimately show ?thesis
  1026     by auto
  1027 qed
  1028 
  1029 lemma csqrt_unique: "w\<^sup>2 = z \<Longrightarrow> 0 < Re w \<or> Re w = 0 \<and> 0 \<le> Im w \<Longrightarrow> csqrt z = w"
  1030   by (auto simp: csqrt_square)
  1031 
  1032 lemma csqrt_minus [simp]:
  1033   assumes "Im x < 0 \<or> (Im x = 0 \<and> 0 \<le> Re x)"
  1034   shows "csqrt (- x) = \<i> * csqrt x"
  1035 proof -
  1036   have "csqrt ((\<i> * csqrt x)^2) = \<i> * csqrt x"
  1037   proof (rule csqrt_square)
  1038     have "Im (csqrt x) \<le> 0"
  1039       using assms by (auto simp add: cmod_eq_Re mult_le_0_iff field_simps complex_Re_le_cmod)
  1040     then show "0 < Re (\<i> * csqrt x) \<or> Re (\<i> * csqrt x) = 0 \<and> 0 \<le> Im (\<i> * csqrt x)"
  1041       by (auto simp add: Re_csqrt simp del: csqrt.simps)
  1042   qed
  1043   also have "(\<i> * csqrt x)^2 = - x"
  1044     by (simp add: power_mult_distrib)
  1045   finally show ?thesis .
  1046 qed
  1047 
  1048 
  1049 text \<open>Legacy theorem names\<close>
  1050 
  1051 lemmas expand_complex_eq = complex_eq_iff
  1052 lemmas complex_Re_Im_cancel_iff = complex_eq_iff
  1053 lemmas complex_equality = complex_eqI
  1054 lemmas cmod_def = norm_complex_def
  1055 lemmas complex_norm_def = norm_complex_def
  1056 lemmas complex_divide_def = divide_complex_def
  1057 
  1058 lemma legacy_Complex_simps:
  1059   shows Complex_eq_0: "Complex a b = 0 \<longleftrightarrow> a = 0 \<and> b = 0"
  1060     and complex_add: "Complex a b + Complex c d = Complex (a + c) (b + d)"
  1061     and complex_minus: "- (Complex a b) = Complex (- a) (- b)"
  1062     and complex_diff: "Complex a b - Complex c d = Complex (a - c) (b - d)"
  1063     and Complex_eq_1: "Complex a b = 1 \<longleftrightarrow> a = 1 \<and> b = 0"
  1064     and Complex_eq_neg_1: "Complex a b = - 1 \<longleftrightarrow> a = - 1 \<and> b = 0"
  1065     and complex_mult: "Complex a b * Complex c d = Complex (a * c - b * d) (a * d + b * c)"
  1066     and complex_inverse: "inverse (Complex a b) = Complex (a / (a\<^sup>2 + b\<^sup>2)) (- b / (a\<^sup>2 + b\<^sup>2))"
  1067     and Complex_eq_numeral: "Complex a b = numeral w \<longleftrightarrow> a = numeral w \<and> b = 0"
  1068     and Complex_eq_neg_numeral: "Complex a b = - numeral w \<longleftrightarrow> a = - numeral w \<and> b = 0"
  1069     and complex_scaleR: "scaleR r (Complex a b) = Complex (r * a) (r * b)"
  1070     and Complex_eq_i: "Complex x y = \<i> \<longleftrightarrow> x = 0 \<and> y = 1"
  1071     and i_mult_Complex: "\<i> * Complex a b = Complex (- b) a"
  1072     and Complex_mult_i: "Complex a b * \<i> = Complex (- b) a"
  1073     and i_complex_of_real: "\<i> * complex_of_real r = Complex 0 r"
  1074     and complex_of_real_i: "complex_of_real r * \<i> = Complex 0 r"
  1075     and Complex_add_complex_of_real: "Complex x y + complex_of_real r = Complex (x+r) y"
  1076     and complex_of_real_add_Complex: "complex_of_real r + Complex x y = Complex (r+x) y"
  1077     and Complex_mult_complex_of_real: "Complex x y * complex_of_real r = Complex (x*r) (y*r)"
  1078     and complex_of_real_mult_Complex: "complex_of_real r * Complex x y = Complex (r*x) (r*y)"
  1079     and complex_eq_cancel_iff2: "(Complex x y = complex_of_real xa) = (x = xa \<and> y = 0)"
  1080     and complex_cnj: "cnj (Complex a b) = Complex a (- b)"
  1081     and Complex_sum': "sum (\<lambda>x. Complex (f x) 0) s = Complex (sum f s) 0"
  1082     and Complex_sum: "Complex (sum f s) 0 = sum (\<lambda>x. Complex (f x) 0) s"
  1083     and complex_of_real_def: "complex_of_real r = Complex r 0"
  1084     and complex_norm: "cmod (Complex x y) = sqrt (x\<^sup>2 + y\<^sup>2)"
  1085   by (simp_all add: norm_complex_def field_simps complex_eq_iff Re_divide Im_divide)
  1086 
  1087 lemma Complex_in_Reals: "Complex x 0 \<in> \<real>"
  1088   by (metis Reals_of_real complex_of_real_def)
  1089 
  1090 end