00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef __OPENKN_MATH__COMPLEX_HPP__
00027 #define __OPENKN_MATH__COMPLEX_HPP__
00028
00029
00030
00031
00032 #include <iostream>
00033 #include <cmath>
00034
00035
00036
00037
00038
00039 #include "MathException.hpp"
00040
00041
00042
00043
00044
00045 namespace kn{
00046
00047
00048
00049
00050
00054 template<class T>
00055 class Complex {
00056
00057 public :
00058
00059
00060
00061
00062
00066 T re;
00067
00071 T im;
00072
00073
00074
00075
00076
00077
00078
00084 Complex(const T &part_re = T(0.0), const T &part_im = T(0.0));
00085
00090 Complex(const Complex<T> &c);
00091
00095 ~Complex(void);
00096
00097
00098
00099
00100
00101
00102
00108 Complex<T> &operator=(const Complex<T> &c);
00109
00110
00111
00112
00113
00114
00120 bool operator==(const Complex<T> &c) const;
00121
00127 bool operator!=(const Complex<T> &c) const;
00128
00129
00130
00131
00132
00133
00134
00140 Complex<T> operator+(const Complex<T> &c) const;
00141
00147 Complex<T> operator-(const Complex<T> &c) const;
00148
00154 Complex<T> operator*(const Complex<T> &c) const;
00155
00161 Complex<T> operator/(const Complex<T> &c) const;
00162
00163
00164
00165
00166
00167
00168
00174 Complex<T> &operator+=(const Complex<T> &c);
00175
00181 Complex<T> &operator-=(const Complex<T> &c);
00182
00188 Complex<T> &operator*=(const Complex<T> &c);
00189
00195 Complex<T> &operator/=(const Complex<T> &c);
00196
00197
00198
00199
00200
00201
00202
00207 T module() const;
00208
00213 Complex<T> conjugate() const;
00214 };
00215
00216
00217
00218
00219
00220
00221
00222 template<class T> Complex<T>::Complex(const T &part_re, const T &part_im):re(part_re), im(part_im){
00223 }
00224
00225 template<class T> Complex<T>::Complex(const Complex<T> &c):re(c.re), im(c.im){
00226 }
00227
00228 template<class T> Complex<T>::~Complex(){}
00229
00230
00231
00232
00233
00234
00235
00236 template<class T> Complex<T> &Complex<T>::operator=(const Complex<T> &c){
00237 this->re = c.re;
00238 this->im = c.im;
00239 return *this;
00240 }
00241
00242
00243
00244
00245
00246
00247
00248 template<class T> bool Complex<T>::operator==(const Complex<T> &c) const{
00249 return this->re == c.re
00250 && this->im == c.im;
00251 }
00252
00253 template<class T> bool Complex<T>::operator!=(const Complex<T> &c) const{
00254 return this->re != c.re
00255 || this->im != c.im;
00256 }
00257
00258
00259
00260
00261
00262
00263
00264 template<class T> Complex<T> Complex<T>::operator+(const Complex<T> &c) const{
00265 return Complex<T>(this->re+c.re,
00266 this->re+c.re);
00267 }
00268
00269 template<class T> Complex<T> Complex<T>::operator-(const Complex<T> &c) const{
00270 return Complex<T>(this->re-c.re,
00271 this->im-c.im);
00272 }
00273
00274 template<class T> Complex<T> Complex<T>::operator*(const Complex<T> &c) const{
00275 return Complex<T>(this->re*c.re - this->im*c.im,
00276 this->re*c.im + this->im*c.re);
00277 }
00278
00279 template<class T> Complex<T> Complex<T>::operator/(const Complex<T> &c) const{
00280 return (*this) * Complex<T>(c.re/(pow(c.re, 2.0f)+pow(c.im, 2.0f)),
00281 -c.im/(pow(c.re, 2.0f)+pow(c.im, 2.0f)));
00282 }
00283
00284
00285
00286
00287
00288
00289
00290 template<class T> Complex<T> &Complex<T>::operator+=(const Complex<T> &c){
00291 this->re = this->re+c.re;
00292 this->im = this->im+c.im;
00293 return *this;
00294 }
00295
00296 template<class T> Complex<T> &Complex<T>::operator-=(const Complex<T> &c){
00297 this->re = this->re-c.re;
00298 this->im = this->im-c.im;
00299 return *this;
00300 }
00301
00302 template<class T> Complex<T> &Complex<T>::operator*=(const Complex<T> &c){
00303 this->re = this->re*c.re - this->im*c.im;
00304 this->im = this->re*c.im + this->im*c.re;
00305 return *this;
00306 }
00307
00308 template<class T> Complex<T> &Complex<T>::operator/=(const Complex<T> &c){
00309 this->re = c.re/(pow(c.re, 2.0f)+pow(c.im, 2.0f));
00310 this->im = -c.im/(pow(c.re, 2.0f)+pow(c.im, 2.0f));
00311 return *this;
00312 }
00313
00314
00315
00316
00317
00318
00319
00320 template<class T> T Complex<T>::module() const{
00321 return sqrt(pow(this->re, 2.0f)+pow(this->im, 2.0f));
00322 }
00323
00324 template<class T> Complex<T> Complex<T>::conjugate() const{
00325 return Complex<T>(this->re, -this->im);
00326 }
00327
00328
00329
00330
00331
00332
00333 }
00334
00335
00336
00337
00338 #endif
00339