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