Go to the documentation of this file.00001
00002 #ifndef COMPLEX_H
00003 #define COMPLEX_H
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00020
00021 #include <iostream>
00022 #include <math.h>
00023 #include "Musimat.h"
00024
00025
00027 class Complex
00028 {
00031 friend ostream& operator<<( ostream& os, const Complex& c ) {
00032 cout << "(";
00033
00034 cout << c.m_r << ", ";
00035 cout << c.m_i;
00036 cout << ")";
00037 Return os;
00038 }
00039
00043 friend istream& operator>>( istream& is, Complex& c ) {
00044 char ch;
00045 is >> ch && (ch == '{' || ch == '(')
00046 && is >> c.m_r >> ch && ch == ','
00047 && is >> c.m_i >> ch && (ch == '}' || ch == '(');
00048 Return is;
00049 }
00050
00051 public:
00052
00054 Complex() :
00055 m_r(0),
00056 m_i(0) {
00057 }
00058
00061 Complex(Real i) :
00062 m_r(i),
00063 m_i(i) {
00064 }
00065
00069 Complex(Real r, Real i) :
00070 m_r(r),
00071 m_i(i) {
00072 }
00073
00076 Real r(Void) {
00077 Return m_r;
00078 }
00079
00082 Real i(Void) {
00083 Return m_i;
00084 }
00085
00088 Void r(Real r) { m_r = r; }
00089
00092 Void i(Real i) { m_i = i; }
00093
00095 Complex& operator=(Const Real& x) {
00096 m_r = x;
00097 m_i = 0;
00098 Return *this;
00099 }
00100
00102 Bool operator==(Const Complex& x) Const {
00103 Return m_r == x.m_r && m_i == x.m_i;
00104 }
00105
00107 Bool operator!=(Const Complex& x) Const {
00108 Return !(m_r == x.m_r && m_i == x.m_i);
00109 }
00110
00112 Bool operator<(Const Complex& x) Const {
00113 Return m_r < x.m_r && m_i < x.m_i;
00114 }
00115
00117 Bool operator<=(Const Complex& x) Const {
00118 Return m_r <= x.m_r && m_i <= x.m_i;
00119 }
00120
00122 Bool operator>=(Const Complex& x) Const {
00123 Return m_r >= x.m_r && m_i >= x.m_i;
00124 }
00125
00127 Bool operator>(Const Complex& x) Const {
00128 Return m_r > x.m_r && m_i > x.m_i;
00129 }
00130
00132 Complex operator+(Const Complex& c){
00133 Return Complex(m_r + c.m_r, m_i + c.m_i);
00134 }
00135
00137 Complex operator-(Const Complex& c){
00138 Return Complex(m_r - c.m_r, m_i - c.m_i);
00139 }
00140
00143 Complex operator-(){
00144 Return Complex(-m_r, -m_i);
00145 }
00146
00149 Complex operator~(){
00150 Return Complex(m_r, -m_i);
00151 }
00152
00154 Complex operator*(Const Complex& c){
00155 Return Complex(m_r * c.m_r - m_i * c.m_i,
00156 m_r * c.m_i + m_i * c.m_r);
00157 }
00158
00160 Complex operator*(Const Real& r){
00161 Complex c(r, 0.0);
00162 Return *this * c;
00163 }
00164
00166 Complex operator/(Const Complex& c){
00167 Real modulus = c.m_r * c.m_r + c.m_i * c.m_i;
00168 Real r = m_i * c.m_i + m_r * c.m_r;
00169 Real i = m_i * c.m_r - m_r * c.m_i;
00170
00171 Return Complex(r / modulus, i / modulus);
00172 }
00173
00175 Complex operator/(Const Real& r){
00176 Complex c(r, 0.0);
00177 Return *this / c;
00178 }
00179
00181 Complex& operator+=(const Complex x) {
00182 Return( *this = *this + x );
00183 }
00184
00186 Complex& operator-=(const Complex x) {
00187 Return( *this = *this - x );
00188 }
00189
00191 Complex& operator*=(const Complex x) {
00192 Return( *this = *this * x );
00193 }
00194
00196 Complex& operator/=(const Complex x) {
00197 Return( *this = *this / x );
00198 }
00199
00201 Complex& operator/=(const Real r) {
00202 Complex x(r, 0.0);
00203 Return( *this = *this / x );
00204 }
00205
00208 Complex Abs() {
00209 Complex t(m_r < 0.0 ? -m_r : m_r, m_i < 0.0 ? -m_i : m_i);
00210 Return(t);
00211 }
00212
00215 Complex Conjugate() {
00216 Complex t(m_r, -m_i);
00217 Return(t);
00218 }
00219
00221 Complex Exp(Complex e) {
00222 Real r = e.r();
00223 r = exp(r);
00224 Real theta = e.i();
00225 Complex t(r * Cos(theta), r * Sin(theta));
00226 Return t;
00227 }
00228
00232 Void print(String s = 0) {
00233 If (s)
00234 cout << s;
00235 cout << "{ ";
00236 cout.precision(2);
00237 cout << m_r << ", ";
00238 cout << m_i;
00239 cout << " }" << endl;
00240 }
00241
00242
00243 private:
00244 Real m_r;
00245 Real m_i;
00246 };
00247
00250 inline Real RealPart( Complex c ) { Return c.r(); }
00251
00254 inline Real ImagPart( Complex c ) { Return c.i(); }
00255
00257 inline Void RealPart( Complex& c, Real r ) { c.r(r); }
00258
00260 inline Void ImagPart( Complex& c, Real r ) { c.i(r); }
00261
00263 inline Complex Abs( Complex c ) { Return(c.Abs()); }
00264
00266 inline Complex Conjugate( Complex c ) { Return(~c); }
00267
00271 inline Complex Exp( Complex a, Complex b ) {
00272 Complex x = a.Exp( b );
00273 Return x;
00274 }
00275
00279 inline Complex Exp(Real r, Real theta) {
00280 Complex t(r * Cos(theta), r * Sin(theta));
00281 Return t;
00282 }
00283
00284 #endif // COMPLEX_H