src/HOL/Complex.thy
changeset 60758 d8d85a8172b5
parent 60429 d3d1e185cd63
child 61076 bdc1e2f0a86a
equal deleted inserted replaced
60757:c09598a97436 60758:d8d85a8172b5
     2     Author:      Jacques D. Fleuriot
     2     Author:      Jacques D. Fleuriot
     3     Copyright:   2001 University of Edinburgh
     3     Copyright:   2001 University of Edinburgh
     4     Conversion to Isar and new proofs by Lawrence C Paulson, 2003/4
     4     Conversion to Isar and new proofs by Lawrence C Paulson, 2003/4
     5 *)
     5 *)
     6 
     6 
     7 section {* Complex Numbers: Rectangular and Polar Representations *}
     7 section \<open>Complex Numbers: Rectangular and Polar Representations\<close>
     8 
     8 
     9 theory Complex
     9 theory Complex
    10 imports Transcendental
    10 imports Transcendental
    11 begin
    11 begin
    12 
    12 
    13 text {*
    13 text \<open>
    14 We use the @{text codatatype} command to define the type of complex numbers. This allows us to use
    14 We use the @{text codatatype} command to define the type of complex numbers. This allows us to use
    15 @{text primcorec} to define complex functions by defining their real and imaginary result
    15 @{text primcorec} to define complex functions by defining their real and imaginary result
    16 separately.
    16 separately.
    17 *}
    17 \<close>
    18 
    18 
    19 codatatype complex = Complex (Re: real) (Im: real)
    19 codatatype complex = Complex (Re: real) (Im: real)
    20 
    20 
    21 lemma complex_surj: "Complex (Re z) (Im z) = z"
    21 lemma complex_surj: "Complex (Re z) (Im z) = z"
    22   by (rule complex.collapse)
    22   by (rule complex.collapse)
    25   by (rule complex.expand) simp
    25   by (rule complex.expand) simp
    26 
    26 
    27 lemma complex_eq_iff: "x = y \<longleftrightarrow> Re x = Re y \<and> Im x = Im y"
    27 lemma complex_eq_iff: "x = y \<longleftrightarrow> Re x = Re y \<and> Im x = Im y"
    28   by (auto intro: complex.expand)
    28   by (auto intro: complex.expand)
    29 
    29 
    30 subsection {* Addition and Subtraction *}
    30 subsection \<open>Addition and Subtraction\<close>
    31 
    31 
    32 instantiation complex :: ab_group_add
    32 instantiation complex :: ab_group_add
    33 begin
    33 begin
    34 
    34 
    35 primcorec zero_complex where
    35 primcorec zero_complex where
    51 instance
    51 instance
    52   by intro_classes (simp_all add: complex_eq_iff)
    52   by intro_classes (simp_all add: complex_eq_iff)
    53 
    53 
    54 end
    54 end
    55 
    55 
    56 subsection {* Multiplication and Division *}
    56 subsection \<open>Multiplication and Division\<close>
    57 
    57 
    58 instantiation complex :: field
    58 instantiation complex :: field
    59 begin
    59 begin
    60 
    60 
    61 primcorec one_complex where
    61 primcorec one_complex where
    97   by (induct n) simp_all
    97   by (induct n) simp_all
    98 
    98 
    99 lemma Im_power_real [simp]: "Im x = 0 \<Longrightarrow> Im (x ^ n) = 0"
    99 lemma Im_power_real [simp]: "Im x = 0 \<Longrightarrow> Im (x ^ n) = 0"
   100   by (induct n) simp_all
   100   by (induct n) simp_all
   101 
   101 
   102 subsection {* Scalar Multiplication *}
   102 subsection \<open>Scalar Multiplication\<close>
   103 
   103 
   104 instantiation complex :: real_field
   104 instantiation complex :: real_field
   105 begin
   105 begin
   106 
   106 
   107 primcorec scaleR_complex where
   107 primcorec scaleR_complex where
   125     by (simp add: complex_eq_iff algebra_simps)
   125     by (simp add: complex_eq_iff algebra_simps)
   126 qed
   126 qed
   127 
   127 
   128 end
   128 end
   129 
   129 
   130 subsection {* Numerals, Arithmetic, and Embedding from Reals *}
   130 subsection \<open>Numerals, Arithmetic, and Embedding from Reals\<close>
   131 
   131 
   132 abbreviation complex_of_real :: "real \<Rightarrow> complex"
   132 abbreviation complex_of_real :: "real \<Rightarrow> complex"
   133   where "complex_of_real \<equiv> of_real"
   133   where "complex_of_real \<equiv> of_real"
   134 
   134 
   135 declare [[coercion "of_real :: real \<Rightarrow> complex"]]
   135 declare [[coercion "of_real :: real \<Rightarrow> complex"]]
   169 
   169 
   170 lemma of_real_Re [simp]:
   170 lemma of_real_Re [simp]:
   171     "z \<in> \<real> \<Longrightarrow> of_real (Re z) = z"
   171     "z \<in> \<real> \<Longrightarrow> of_real (Re z) = z"
   172   by (auto simp: Reals_def)
   172   by (auto simp: Reals_def)
   173 
   173 
   174 subsection {* The Complex Number $i$ *}
   174 subsection \<open>The Complex Number $i$\<close>
   175 
   175 
   176 primcorec "ii" :: complex  ("\<i>") where
   176 primcorec "ii" :: complex  ("\<i>") where
   177   "Re ii = 0"
   177   "Re ii = 0"
   178 | "Im ii = 1"
   178 | "Im ii = 1"
   179 
   179 
   229   by auto
   229   by auto
   230 
   230 
   231 lemma divide_numeral_i [simp]: "z / (numeral n * ii) = -(ii*z) / numeral n"
   231 lemma divide_numeral_i [simp]: "z / (numeral n * ii) = -(ii*z) / numeral n"
   232   by (metis divide_divide_eq_left divide_i mult.commute mult_minus_right)
   232   by (metis divide_divide_eq_left divide_i mult.commute mult_minus_right)
   233 
   233 
   234 subsection {* Vector Norm *}
   234 subsection \<open>Vector Norm\<close>
   235 
   235 
   236 instantiation complex :: real_normed_field
   236 instantiation complex :: real_normed_field
   237 begin
   237 begin
   238 
   238 
   239 definition "norm z = sqrt ((Re z)\<^sup>2 + (Im z)\<^sup>2)"
   239 definition "norm z = sqrt ((Re z)\<^sup>2 + (Im z)\<^sup>2)"
   327 
   327 
   328 lemma complex_unit_circle: "z \<noteq> 0 \<Longrightarrow> (Re z / cmod z)\<^sup>2 + (Im z / cmod z)\<^sup>2 = 1"
   328 lemma complex_unit_circle: "z \<noteq> 0 \<Longrightarrow> (Re z / cmod z)\<^sup>2 + (Im z / cmod z)\<^sup>2 = 1"
   329   by (simp add: norm_complex_def divide_simps complex_eq_iff)
   329   by (simp add: norm_complex_def divide_simps complex_eq_iff)
   330 
   330 
   331 
   331 
   332 text {* Properties of complex signum. *}
   332 text \<open>Properties of complex signum.\<close>
   333 
   333 
   334 lemma sgn_eq: "sgn z = z / complex_of_real (cmod z)"
   334 lemma sgn_eq: "sgn z = z / complex_of_real (cmod z)"
   335   by (simp add: sgn_div_norm divide_inverse scaleR_conv_of_real mult.commute)
   335   by (simp add: sgn_div_norm divide_inverse scaleR_conv_of_real mult.commute)
   336 
   336 
   337 lemma Re_sgn [simp]: "Re(sgn z) = Re(z)/cmod z"
   337 lemma Re_sgn [simp]: "Re(sgn z) = Re(z)/cmod z"
   339 
   339 
   340 lemma Im_sgn [simp]: "Im(sgn z) = Im(z)/cmod z"
   340 lemma Im_sgn [simp]: "Im(sgn z) = Im(z)/cmod z"
   341   by (simp add: complex_sgn_def divide_inverse)
   341   by (simp add: complex_sgn_def divide_inverse)
   342 
   342 
   343 
   343 
   344 subsection {* Completeness of the Complexes *}
   344 subsection \<open>Completeness of the Complexes\<close>
   345 
   345 
   346 lemma bounded_linear_Re: "bounded_linear Re"
   346 lemma bounded_linear_Re: "bounded_linear Re"
   347   by (rule bounded_linear_intro [where K=1], simp_all add: norm_complex_def)
   347   by (rule bounded_linear_intro [where K=1], simp_all add: norm_complex_def)
   348 
   348 
   349 lemma bounded_linear_Im: "bounded_linear Im"
   349 lemma bounded_linear_Im: "bounded_linear Im"
   405 qed
   405 qed
   406 
   406 
   407 declare
   407 declare
   408   DERIV_power[where 'a=complex, unfolded of_nat_def[symmetric], derivative_intros]
   408   DERIV_power[where 'a=complex, unfolded of_nat_def[symmetric], derivative_intros]
   409 
   409 
   410 subsection {* Complex Conjugation *}
   410 subsection \<open>Complex Conjugation\<close>
   411 
   411 
   412 primcorec cnj :: "complex \<Rightarrow> complex" where
   412 primcorec cnj :: "complex \<Rightarrow> complex" where
   413   "Re (cnj z) = Re z"
   413   "Re (cnj z) = Re z"
   414 | "Im (cnj z) = - Im z"
   414 | "Im (cnj z) = - Im z"
   415 
   415 
   512 
   512 
   513 lemma sums_cnj: "((\<lambda>x. cnj(f x)) sums cnj l) \<longleftrightarrow> (f sums l)"
   513 lemma sums_cnj: "((\<lambda>x. cnj(f x)) sums cnj l) \<longleftrightarrow> (f sums l)"
   514   by (simp add: sums_def lim_cnj cnj_setsum [symmetric] del: cnj_setsum)
   514   by (simp add: sums_def lim_cnj cnj_setsum [symmetric] del: cnj_setsum)
   515 
   515 
   516 
   516 
   517 subsection{*Basic Lemmas*}
   517 subsection\<open>Basic Lemmas\<close>
   518 
   518 
   519 lemma complex_eq_0: "z=0 \<longleftrightarrow> (Re z)\<^sup>2 + (Im z)\<^sup>2 = 0"
   519 lemma complex_eq_0: "z=0 \<longleftrightarrow> (Re z)\<^sup>2 + (Im z)\<^sup>2 = 0"
   520   by (metis zero_complex.sel complex_eqI sum_power2_eq_zero_iff)
   520   by (metis zero_complex.sel complex_eqI sum_power2_eq_zero_iff)
   521 
   521 
   522 lemma complex_neq_0: "z\<noteq>0 \<longleftrightarrow> (Re z)\<^sup>2 + (Im z)\<^sup>2 > 0"
   522 lemma complex_neq_0: "z\<noteq>0 \<longleftrightarrow> (Re z)\<^sup>2 + (Im z)\<^sup>2 > 0"
   614     apply (auto simp: g sums_Re)
   614     apply (auto simp: g sums_Re)
   615     apply (metis fg g)
   615     apply (metis fg g)
   616     done
   616     done
   617 qed
   617 qed
   618 
   618 
   619 subsection{*Polar Form for Complex Numbers*}
   619 subsection\<open>Polar Form for Complex Numbers\<close>
   620 
   620 
   621 lemma complex_unimodular_polar: "(norm z = 1) \<Longrightarrow> \<exists>x. z = Complex (cos x) (sin x)"
   621 lemma complex_unimodular_polar: "(norm z = 1) \<Longrightarrow> \<exists>x. z = Complex (cos x) (sin x)"
   622   using sincos_total_2pi [of "Re z" "Im z"]
   622   using sincos_total_2pi [of "Re z" "Im z"]
   623   by auto (metis cmod_power2 complex_eq power_one)
   623   by auto (metis cmod_power2 complex_eq power_one)
   624 
   624 
   625 subsubsection {* $\cos \theta + i \sin \theta$ *}
   625 subsubsection \<open>$\cos \theta + i \sin \theta$\<close>
   626 
   626 
   627 primcorec cis :: "real \<Rightarrow> complex" where
   627 primcorec cis :: "real \<Rightarrow> complex" where
   628   "Re (cis a) = cos a"
   628   "Re (cis a) = cos a"
   629 | "Im (cis a) = sin a"
   629 | "Im (cis a) = sin a"
   630 
   630 
   659   by (auto simp add: DeMoivre)
   659   by (auto simp add: DeMoivre)
   660 
   660 
   661 lemma cis_pi: "cis pi = -1"
   661 lemma cis_pi: "cis pi = -1"
   662   by (simp add: complex_eq_iff)
   662   by (simp add: complex_eq_iff)
   663 
   663 
   664 subsubsection {* $r(\cos \theta + i \sin \theta)$ *}
   664 subsubsection \<open>$r(\cos \theta + i \sin \theta)$\<close>
   665 
   665 
   666 definition rcis :: "real \<Rightarrow> real \<Rightarrow> complex" where
   666 definition rcis :: "real \<Rightarrow> real \<Rightarrow> complex" where
   667   "rcis r a = complex_of_real r * cis a"
   667   "rcis r a = complex_of_real r * cis a"
   668 
   668 
   669 lemma Re_rcis [simp]: "Re(rcis r a) = r * cos a"
   669 lemma Re_rcis [simp]: "Re(rcis r a) = r * cos a"
   700   by (simp add: divide_inverse rcis_def)
   700   by (simp add: divide_inverse rcis_def)
   701 
   701 
   702 lemma rcis_divide: "rcis r1 a / rcis r2 b = rcis (r1/r2) (a - b)"
   702 lemma rcis_divide: "rcis r1 a / rcis r2 b = rcis (r1/r2) (a - b)"
   703   by (simp add: rcis_def cis_divide [symmetric])
   703   by (simp add: rcis_def cis_divide [symmetric])
   704 
   704 
   705 subsubsection {* Complex exponential *}
   705 subsubsection \<open>Complex exponential\<close>
   706 
   706 
   707 abbreviation Exp :: "complex \<Rightarrow> complex"
   707 abbreviation Exp :: "complex \<Rightarrow> complex"
   708   where "Exp \<equiv> exp"
   708   where "Exp \<equiv> exp"
   709 
   709 
   710 lemma cis_conv_exp: "cis b = exp (\<i> * b)"
   710 lemma cis_conv_exp: "cis b = exp (\<i> * b)"
   745   done
   745   done
   746 
   746 
   747 lemma Exp_two_pi_i [simp]: "Exp((2::complex) * complex_of_real pi * ii) = 1"
   747 lemma Exp_two_pi_i [simp]: "Exp((2::complex) * complex_of_real pi * ii) = 1"
   748   by (simp add: Exp_eq_polar complex_eq_iff)
   748   by (simp add: Exp_eq_polar complex_eq_iff)
   749 
   749 
   750 subsubsection {* Complex argument *}
   750 subsubsection \<open>Complex argument\<close>
   751 
   751 
   752 definition arg :: "complex \<Rightarrow> real" where
   752 definition arg :: "complex \<Rightarrow> real" where
   753   "arg z = (if z = 0 then 0 else (SOME a. sgn z = cis a \<and> -pi < a \<and> a \<le> pi))"
   753   "arg z = (if z = 0 then 0 else (SOME a. sgn z = cis a \<and> -pi < a \<and> a \<le> pi))"
   754 
   754 
   755 lemma arg_zero: "arg 0 = 0"
   755 lemma arg_zero: "arg 0 = 0"
   773     ultimately have "d = 0"
   773     ultimately have "d = 0"
   774       unfolding sin_zero_iff
   774       unfolding sin_zero_iff
   775       by (auto elim!: evenE dest!: less_2_cases)
   775       by (auto elim!: evenE dest!: less_2_cases)
   776     thus "a = x" unfolding d_def by simp
   776     thus "a = x" unfolding d_def by simp
   777   qed (simp add: assms del: Re_sgn Im_sgn)
   777   qed (simp add: assms del: Re_sgn Im_sgn)
   778   with `z \<noteq> 0` show "arg z = x"
   778   with \<open>z \<noteq> 0\<close> show "arg z = x"
   779     unfolding arg_def by simp
   779     unfolding arg_def by simp
   780 qed
   780 qed
   781 
   781 
   782 lemma arg_correct:
   782 lemma arg_correct:
   783   assumes "z \<noteq> 0" shows "sgn z = cis (arg z) \<and> -pi < arg z \<and> arg z \<le> pi"
   783   assumes "z \<noteq> 0" shows "sgn z = cis (arg z) \<and> -pi < arg z \<and> arg z \<le> pi"
   784 proof (simp add: arg_def assms, rule someI_ex)
   784 proof (simp add: arg_def assms, rule someI_ex)
   785   obtain r a where z: "z = rcis r a" using rcis_Ex by fast
   785   obtain r a where z: "z = rcis r a" using rcis_Ex by fast
   786   with assms have "r \<noteq> 0" by auto
   786   with assms have "r \<noteq> 0" by auto
   787   def b \<equiv> "if 0 < r then a else a + pi"
   787   def b \<equiv> "if 0 < r then a else a + pi"
   788   have b: "sgn z = cis b"
   788   have b: "sgn z = cis b"
   789     unfolding z b_def rcis_def using `r \<noteq> 0`
   789     unfolding z b_def rcis_def using \<open>r \<noteq> 0\<close>
   790     by (simp add: of_real_def sgn_scaleR sgn_if complex_eq_iff)
   790     by (simp add: of_real_def sgn_scaleR sgn_if complex_eq_iff)
   791   have cis_2pi_nat: "\<And>n. cis (2 * pi * real_of_nat n) = 1"
   791   have cis_2pi_nat: "\<And>n. cis (2 * pi * real_of_nat n) = 1"
   792     by (induct_tac n) (simp_all add: distrib_left cis_mult [symmetric] complex_eq_iff)
   792     by (induct_tac n) (simp_all add: distrib_left cis_mult [symmetric] complex_eq_iff)
   793   have cis_2pi_int: "\<And>x. cis (2 * pi * real_of_int x) = 1"
   793   have cis_2pi_int: "\<And>x. cis (2 * pi * real_of_int x) = 1"
   794     by (case_tac x rule: int_diff_cases)
   794     by (case_tac x rule: int_diff_cases)
   813   by (cases "z = 0") (simp_all add: rcis_def cis_arg sgn_div_norm of_real_def)
   813   by (cases "z = 0") (simp_all add: rcis_def cis_arg sgn_div_norm of_real_def)
   814 
   814 
   815 lemma cos_arg_i_mult_zero [simp]: "y \<noteq> 0 \<Longrightarrow> Re y = 0 \<Longrightarrow> cos (arg y) = 0"
   815 lemma cos_arg_i_mult_zero [simp]: "y \<noteq> 0 \<Longrightarrow> Re y = 0 \<Longrightarrow> cos (arg y) = 0"
   816   using cis_arg [of y] by (simp add: complex_eq_iff)
   816   using cis_arg [of y] by (simp add: complex_eq_iff)
   817 
   817 
   818 subsection {* Square root of complex numbers *}
   818 subsection \<open>Square root of complex numbers\<close>
   819 
   819 
   820 primcorec csqrt :: "complex \<Rightarrow> complex" where
   820 primcorec csqrt :: "complex \<Rightarrow> complex" where
   821   "Re (csqrt z) = sqrt ((cmod z + Re z) / 2)"
   821   "Re (csqrt z) = sqrt ((cmod z + Re z) / 2)"
   822 | "Im (csqrt z) = (if Im z = 0 then 1 else sgn (Im z)) * sqrt ((cmod z - Re z) / 2)"
   822 | "Im (csqrt z) = (if Im z = 0 then 1 else sgn (Im z)) * sqrt ((cmod z - Re z) / 2)"
   823 
   823 
   900   also have "(\<i> * csqrt x)^2 = - x"
   900   also have "(\<i> * csqrt x)^2 = - x"
   901     by (simp add: power_mult_distrib)
   901     by (simp add: power_mult_distrib)
   902   finally show ?thesis .
   902   finally show ?thesis .
   903 qed
   903 qed
   904 
   904 
   905 text {* Legacy theorem names *}
   905 text \<open>Legacy theorem names\<close>
   906 
   906 
   907 lemmas expand_complex_eq = complex_eq_iff
   907 lemmas expand_complex_eq = complex_eq_iff
   908 lemmas complex_Re_Im_cancel_iff = complex_eq_iff
   908 lemmas complex_Re_Im_cancel_iff = complex_eq_iff
   909 lemmas complex_equality = complex_eqI
   909 lemmas complex_equality = complex_eqI
   910 lemmas cmod_def = norm_complex_def
   910 lemmas cmod_def = norm_complex_def