00001 using System;
00002
00003 namespace FFT {
00004
00005
00006
00007
00008
00009
00010
00011
00012 public struct Complex : IEquatable<Complex>
00013 {
00014
00015
00016
00017 public double Re {get; private set;}
00018
00019
00020
00021
00022 public double Im {get; private set;}
00023
00024
00025
00026
00027
00028
00029
00030 public Complex(double re, double im) : this()
00031 {
00032 this.Re = re;
00033 this.Im = im;
00034 }
00035
00036
00037
00038
00039
00040
00041
00042 public static Complex FromPolar(double modulus, double argument)
00043 {
00044 return new Complex(modulus*Math.Cos(argument),
00045 modulus*Math.Sin(argument));
00046 }
00047
00048
00049
00050
00051 public double Modulus
00052 {
00053 get { return Math.Sqrt(this.Re*this.Re+this.Im*this.Im); }
00054 }
00055
00056
00057
00058
00059 public double Argument
00060 {
00061 get { return Math.Atan2(this.Im,this.Re);}
00062 }
00063
00064
00065
00066
00067
00068
00069 public Complex Conjugate
00070 {
00071 get { return new Complex(this.Re,-this.Im); }
00072 }
00073
00074
00075
00076
00077 public static implicit operator Complex(double r)
00078 {
00079 return new Complex(r,0);
00080 }
00081
00082
00083
00084
00085 public static implicit operator Complex(int i)
00086 {
00087 return new Complex(i,0);
00088 }
00089
00090
00091
00092
00093 public static Complex operator +(Complex z, Complex s)
00094 {
00095 return new Complex(z.Re + s.Re,z.Im + s.Im);
00096 }
00097
00098
00099
00100
00101 public static Complex operator -(Complex z, Complex s)
00102 {
00103 return new Complex(z.Re - s.Re, z.Im - s.Im);
00104 }
00105
00106
00107
00108
00109 public static Complex operator -(Complex z)
00110 {
00111 return new Complex(-z.Re,-z.Im);
00112 }
00113
00114
00115
00116
00117 public static Complex operator *(Complex z, Complex s)
00118 {
00119 return new Complex(z.Re * s.Re - z.Im * s.Im, z.Re * s.Im + z.Im * s.Re);
00120 }
00121
00122
00123
00124
00125 public static Complex operator /(Complex z, Complex s)
00126 {
00127 double d = s.Re*s.Re + s.Im*s.Im;
00128 Complex res = z * s.Conjugate;
00129
00130 res.Re /= d;
00131 res.Im /= d;
00132
00133 return res;
00134 }
00135
00136 public bool Equals(Complex c)
00137 {
00138 return (this.Re == c.Re && this.Im == c.Im);
00139 }
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153 public override string ToString()
00154 {
00155 if (this.Im == 0) {
00156 return this.Re.ToString();
00157 } else if (this.Re == 0) {
00158 return this.Im.ToString() + "i";
00159 } else {
00160 return String.Format("{0} {1} {2}i",
00161 this.Re,
00162 (this.Im < 0)?('-'):('+'),
00163 (this.Im < 0)?(-this.Im):(this.Im));
00164 }
00165 }
00166
00167 }
00168
00169 }