Go to the documentation of this file.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
00027
00028
00029
00030
00031
00032 #ifndef ComplexH
00033 #define ComplexH
00034
00035
00036 #if _MSC_VER > 1000
00037 #pragma once
00038 #endif // _MSC_VER > 1000
00039
00040 #include <zeusmath/System/Constants.h>
00041 #include <zeusbase/System/Float.h>
00042 #include <math.h>
00043
00044 #ifdef _BORLAND
00045 #pragma warn -8027
00046 #endif
00047
00048 BEGIN_NAMESPACE_Zeus
00049
00050
00053
00054 zeusmath_class TComplex
00055 {
00056 public:
00057
00060
00061 inline TComplex()
00062 : m_fReal(0),
00063 m_fIm(0)
00064 {
00065 }
00066
00067
00071
00072 inline TComplex(const TComplex& rInpar)
00073 {
00074 assign(rInpar);
00075 }
00076
00077
00084
00085 inline TComplex(const Float& fValue1, const Float& fValue2, bool bPolar = false)
00086 {
00087 if (bPolar)
00088 {
00089 setPolarValues(fValue1, fValue2);
00090 }
00091 else
00092 {
00093 setCartesianValues(fValue1, fValue2);
00094 }
00095 }
00096
00097
00101
00102 inline TComplex(const Float& fX)
00103 {
00104 m_fReal = fX;
00105 m_fIm = 0;
00106 }
00107
00108
00112
00113 inline TComplex(Int iX)
00114 {
00115 m_fReal = iX;
00116 m_fIm = 0;
00117 }
00118
00119
00123
00124 inline TComplex(int iX)
00125 {
00126 m_fReal = iX;
00127 m_fIm = 0;
00128 }
00129
00130
00133
00134 virtual inline ~TComplex() { }
00135
00136
00140
00141 inline void assign(const TComplex& rInpar)
00142 {
00143 m_fReal = rInpar.m_fReal;
00144 m_fIm = rInpar.m_fIm;
00145 }
00146
00147
00154
00155 inline bool equals(const TComplex& rInpar, Float fPrecision = FLOAT_PRECISION) const
00156 {
00157 return (TFloat::equalsFloat(this->getReal(), rInpar.getReal(), fPrecision) &&
00158 TFloat::equalsFloat(this->getIm(), rInpar.getIm(), fPrecision));
00159 }
00160
00161
00168
00169 inline bool equals(const Float& rX, Float fPrecision = FLOAT_PRECISION) const
00170 {
00171 return (this->isReal() &&
00172 TFloat::equalsFloat(this->getReal(), rX, fPrecision));
00173 }
00174
00175
00178
00179 inline const Float& getReal() const
00180 {
00181 return m_fReal;
00182 }
00183
00184
00187
00188 inline const Float& getIm() const
00189 {
00190 return m_fIm;
00191 }
00192
00193
00196
00197 inline const Float getAbsValue() const
00198 {
00199 return TMath::sqrt(TMath::pow(getReal(), 2) + TMath::pow(getIm(), 2));
00200 }
00201
00202
00205
00206 inline const Float getArgument() const
00207 {
00208 Float fArgument = 0;
00209 if (TFloat::isZero(getIm() , FLOAT_PRECISION) &&
00210 TFloat::isZero(getReal(), FLOAT_PRECISION))
00211 {
00212 fArgument = 0;
00213 }
00214 else
00215 {
00216 fArgument = ::atan2(getIm(), getReal());
00217 }
00218 return fArgument;
00219 }
00220
00221
00226
00227 inline bool isReal() const
00228 {
00229 return TFloat::isZero(getIm(), FLOAT_PRECISION);
00230 }
00231
00232
00237
00238 inline bool isImaginary() const
00239 {
00240 return TFloat::isZero(getReal(), FLOAT_PRECISION) &&
00241 !TFloat::isZero(getIm() , FLOAT_PRECISION);
00242 }
00243
00244
00249
00250 inline bool isComplex() const
00251 {
00252 return !isReal();
00253 }
00254
00255
00256
00260
00261 inline void setReal(const Float& fReal)
00262 {
00263 m_fReal = fReal;
00264 }
00265
00266
00270
00271 inline void setIm(const Float& fIm)
00272 {
00273 m_fIm = fIm;
00274 }
00275
00276
00280
00281 inline void setAbsValue(const Float& fAbsValue)
00282 {
00283 m_fReal = fAbsValue * ::cos(getArgument());
00284 m_fIm = fAbsValue * ::sin(getArgument());
00285 }
00286
00287
00291
00292 inline void setArgument(const Float& fArgument)
00293 {
00294 m_fReal = getAbsValue() * ::cos(fArgument);
00295 m_fIm = getAbsValue() * ::sin(fArgument);
00296 }
00297
00298
00303
00304 inline void setCartesianValues(const Float& fReal, const Float& fIm)
00305 {
00306 m_fReal = fReal;
00307 m_fIm = fIm;
00308 }
00309
00310
00315
00316 inline void setPolarValues(const Float& fAbsValue, const Float& fArgument)
00317 {
00318 m_fReal = fAbsValue * ::cos(fArgument);
00319 m_fIm = fAbsValue * ::sin(fArgument);
00320 }
00321
00322 TString toString(Float fRoundPrecision = 0.0001, bool bPolar = false) const;
00323
00324
00325 inline TComplex operator+(const TComplex& z) const { return TComplex(getReal() + z.getReal(), getIm() + z.getIm()); }
00326 inline TComplex operator+(const Float& x) const { return TComplex(getReal() + x, getIm()); }
00327 inline TComplex operator-(const TComplex& z) const { return TComplex(getReal() - z.getReal(), getIm() - z.getIm()); }
00328 inline TComplex operator-(const Float& x) const { return TComplex(getReal() - x, getIm()); }
00329 inline TComplex operator*(const TComplex& b) const
00330 {
00331 Float real = m_fReal * b.m_fReal - m_fIm * b.m_fIm;
00332 Float imag = m_fReal * b.m_fIm + m_fIm * b.m_fReal;
00333 return TComplex(real, imag);
00334 }
00335 inline TComplex operator*(const Float& x) const { return TComplex(getReal() * x, getIm() * x); }
00336 inline TComplex operator/(const Float& x) const { return TComplex(getReal() / x, getIm() / x); }
00337 inline void operator+=(const TComplex& z) { (*this) = (*this) + z; }
00338 inline void operator+=(const Float& x ) { (*this) = (*this) + x; }
00339 inline void operator-=(const TComplex& z) { (*this) = (*this) - z; }
00340 inline void operator-=(const Float& x ) { (*this) = (*this) - x; }
00341 inline void operator*=(const TComplex& z) { (*this) = (*this) * z; }
00342 inline void operator*=(const Float& x ) { (*this) = (*this) * x; }
00343
00344 inline void operator/=(const Float& x ) { (*this) = (*this) / x; }
00345
00346
00347 inline const TComplex& operator=(const TComplex& rX) { assign(rX); return *this; }
00348 inline const TComplex& operator=(Float fReal) { setReal(fReal); setIm(0); return *this; }
00349
00350
00351 inline bool operator==(const TComplex& z) const { return equals(z); }
00352 inline bool operator==(const Float& x) const { return equals(x); }
00353 inline bool operator!=(const TComplex& z) const { return !equals(z); }
00354 inline bool operator!=(const Float& x) const { return !equals(x); }
00355
00356
00357
00358
00363
00364 static inline TComplex log(const TComplex& z)
00365 {
00366 return TComplex(::log(z.getAbsValue()), z.getArgument());
00367 }
00368
00369
00375
00376 static inline TComplex log(const TComplex& z, const Float& fBasis)
00377 {
00378 return TComplex::log(z) / ::log(fBasis);
00379 }
00380
00381
00386
00387 static inline TComplex log10(const TComplex& z)
00388 {
00389 return TComplex::log(z, 10);
00390 }
00391
00392
00397
00398 static inline TComplex sqrt(const TComplex& z)
00399 {
00400 return TComplex::pow(z, TComplex(0.5));
00401 }
00402
00403 static TComplex pow(const TComplex& zBase, const TComplex& zExponent);
00404
00405
00406 protected:
00407
00408 private:
00410 Float m_fReal;
00412 Float m_fIm;
00413
00414 };
00415
00416
00417
00418
00419
00424
00425 inline TComplex operator-(const TComplex& z)
00426 {
00427 return TComplex(-z.getReal(), -z.getIm());
00428 }
00429
00430
00435
00436 inline TComplex operator+(const TComplex& z)
00437 {
00438 return z;
00439 }
00440
00441
00442
00448
00449 inline TComplex operator+(const Float& x, const TComplex& z)
00450 {
00451 return z + x;
00452 }
00453
00454
00460
00461 inline TComplex operator-(const Float& x, const TComplex& z)
00462 {
00463 return -z + x;
00464 }
00465
00466
00472
00473 inline TComplex operator*(const Float& x, const TComplex& z)
00474 {
00475 return z * x;
00476 }
00477
00478
00484
00485
00486
00487
00488
00489
00490
00491
00498
00499 inline bool operator==(const Float& x, const TComplex& z)
00500 {
00501 return z == x;
00502 }
00503
00504
00511
00512 inline bool operator!=(const Float& x, const TComplex& z)
00513 {
00514 return !(z == x);
00515 }
00516
00517
00518 END_NAMESPACE_Zeus
00519
00520
00521 #endif