00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "Complex.h"
00014
00015 Complex::Complex()
00016 {
00017 re = 0.0;
00018 im = 0.0;
00019 }
00020
00021 Complex::Complex(double re, double im)
00022 {
00023 this->re = re;
00024 this->im = im;
00025 }
00026
00027 Complex::Complex(const Complex & rhs)
00028 {
00029 *this = rhs;
00030 }
00031
00032 double Complex::real()
00033 {
00034 return re;
00035 }
00036
00037 double Complex::imaginary()
00038 {
00039 return im;
00040 }
00041
00042 void Complex::operator=(const Complex & rhs)
00043 {
00044 this->re = rhs.re;
00045 this->im = rhs.im;
00046 }
00047
00048 void Complex::operator=(const double & rhs)
00049 {
00050 this->re = rhs;
00051 this->im = 0;
00052 }
00053
00054 Complex Complex::operator+(const Complex & rhs)
00055 {
00056 Complex result;
00057
00058 result.re = this->re + rhs.re;
00059 result.im = this->im + rhs.im;
00060
00061 return result;
00062 }
00063
00064 Complex Complex::operator+(const double & rhs)
00065 {
00066 Complex result;
00067
00068 result.re = this->re + rhs;
00069 result.im = this->im;
00070
00071 return result;
00072 }
00073
00074 Complex Complex::operator-(const Complex & rhs)
00075 {
00076 Complex result;
00077
00078 result.re = this->re - rhs.re;
00079 result.im = this->im - rhs.im;
00080
00081 return result;
00082 }
00083
00084 Complex Complex::operator-(const double & rhs)
00085 {
00086 Complex result;
00087
00088 result.re = this->re - rhs;
00089 result.im = this->im;
00090
00091 return result;
00092 }
00093
00094 Complex Complex::operator*(const Complex & rhs)
00095 {
00096 Complex result;
00097
00098 result.re = this->re*rhs.re - this->im*rhs.im;
00099 result.im = this->im*rhs.re + this->re*rhs.im;
00100
00101 return result;
00102 }
00103
00104 Complex Complex::operator*(const double & rhs)
00105 {
00106 Complex result;
00107
00108 result.re = this->re*rhs;
00109 result.im = this->im*rhs;
00110
00111 return result;
00112 }
00113
00114 Complex Complex::operator/(const Complex & rhs)
00115 {
00116 Complex result;
00117
00118 if( fabs(rhs.re) >= fabs(rhs.im) )
00119 {
00120 double r = rhs.im/rhs.re;
00121 double den = rhs.re+r*rhs.im;
00122 result.re = (this->re+r*this->im)/den;
00123 result.im = (this->im-r*this->re)/den;
00124 }
00125 else
00126 {
00127 double r = rhs.re/rhs.im;
00128 double den = rhs.im+r*rhs.re;
00129 result.re = (this->re*r+this->im)/den;
00130 result.im = (this->im*r-this->re)/den;
00131 }
00132
00133 return result;
00134 }
00135
00136 Complex Complex::conjugate()
00137 {
00138 Complex result;
00139
00140 result.re = this->re;
00141 result.im = -this->im;
00142
00143 return result;
00144 }
00145
00146 double Complex::abs()
00147 {
00148 double x = fabs(re);
00149 double y = fabs(im);
00150 double ans;
00151
00152 if(x == 0.0)
00153 {
00154 ans = y;
00155 }
00156 else if(y == 0.0)
00157 {
00158 ans = x;
00159 }
00160 else if( x > y)
00161 {
00162 double temp = y/x;
00163 ans = x*sqrt(1.0+temp*temp);
00164 }
00165 else
00166 {
00167 double temp = x/y;
00168 ans = y*sqrt(1.0+temp*temp);
00169 }
00170
00171 return ans;
00172 }
00173
00174 Complex Complex::squareroot()
00175 {
00176 Complex c;
00177
00178 if ((this->re == 0.0) && (this->im == 0.0))
00179 {
00180 c.re = 0.0;
00181 c.im = 0.0;
00182 return c;
00183 }
00184 else
00185 {
00186 double x = fabs(this->re);
00187 double y = fabs(this->im);
00188 double w,r;
00189
00190 if (x >= y)
00191 {
00192 r = y/x;
00193 w = sqrt(x)*sqrt(0.5*(1.0+sqrt(1.0+r*r)));
00194 }
00195 else
00196 {
00197 r = x/y;
00198 w = sqrt(y)*sqrt(0.5*(r+sqrt(1.0+r*r)));
00199 }
00200
00201 if (this->re >= 0.0)
00202 {
00203 c.re = w;
00204 c.im = this->im/(2.0*w);
00205 }
00206 else
00207 {
00208 c.im = (this->im >= 0) ? w : -w;
00209 c.re = this->im/(2.0*c.im);
00210 }
00211 return c;
00212 }
00213 }
00214
00215 ostream & operator<<(ostream & strm, Complex & c)
00216 {
00217 if(c.real() >= 0.0) strm << " ";
00218 strm << c.real();
00219 strm << (c.imaginary() > 0? "+i":"-i") << fabs(c.imaginary());
00220
00221 return strm;
00222 }
00223
00224
00225
00226
00227
00228
00229
00230
00231