00001 #ifndef __TComplex__
00002 #define __TComplex__
00003
00004 #include "../Basics/CCountedObject.hpp"
00005 using Exponent::Basics::CCountedObject;
00006
00007
00008
00009 namespace Exponent
00010 {
00011 namespace MathTools
00012 {
00030 template <class TypeName> class TComplex : public CCountedObject
00031 {
00033 EXPONENT_CLASS_DECLARATION;
00036
00037
00038 public:
00039
00040
00041
00047 TComplex(const TypeName real = 0, const TypeName imag = 0) : m_real(real), m_imag(imag)
00048 {
00049 EXPONENT_CLASS_CONSTRUCTION(TComplex<TypeName>);
00050 }
00051
00055 virtual ~TComplex()
00056 {
00057 EXPONENT_CLASS_DESTRUCTION(TComplex<TypeName>);
00058 }
00059
00060
00061
00067 TComplex<TypeName> operator + (const TComplex<TypeName> &complex)
00068 {
00069 TComplex<TypeName> answer(m_real + complex.m_real, m_imag + complex.m_imag);
00070 return answer;
00071 }
00072
00078 TComplex<TypeName> operator - (const TComplex<TypeName> &complex)
00079 {
00080 TComplex<TypeName> answer(m_real - complex.m_real, m_imag - complex.m_imag);
00081 return answer;
00082 }
00083
00089 TComplex<TypeName> operator * (const TComplex<TypeName> &complex)
00090 {
00091 TComplex<TypeName> answer(m_real * complex.m_real - m_imag * complex.m_imag, m_real * complex.m_imag + complex.m_imag * complex.m_real);
00092 return answer;
00093 }
00094
00100 TComplex<TypeName> operator / (const TComplex<TypeName> &complex)
00101 {
00102 TComplex<TypeName> answer(m_real * complex.m_real + m_imag * complex.m_imag, m_imag * complex.m_real - m_real * complex.m_imag);
00103 return answer / complex.getMagnitudeSquared();
00104 }
00105
00111 TComplex<TypeName> operator = (const TComplex<TypeName> &complex)
00112 {
00113 TComplex<TypeName> answer(complex.m_real, complex.m_imag);
00114 return answer;
00115 }
00116
00122 TComplex<TypeName> operator * (const TypeName value)
00123 {
00124 TComplex<TypeName> answer(m_real * value, m_imag * value);
00125 return answer;
00126 }
00127
00133 TComplex<TypeName> operator / (const TypeName value)
00134 {
00135 assert(value != 0);
00136 TComplex<TypeName> answer(m_real / value, m_imag / value);
00137 return answer;
00138 }
00139
00140
00141
00147 void setRealAndImaginaryFromMagnitudeAndPhase(const TypeName magnitude, const TypeName phase)
00148 {
00149 m_real = magnitude * cos(phase);
00150 m_imag = magnitude * sin(phase);
00151 }
00152
00158 void setRealAndImaginary(const TypeName real, const TypeName imag)
00159 {
00160 m_real = real;
00161 m_imag = imag;
00162 }
00163
00164
00165
00170 TypeName getMagnitude() const
00171 {
00172 return sqrt(m_real * m_real + m_imag * m_imag);
00173 }
00174
00179 TypeName getMagnitudeSquared() const
00180 {
00181 return m_real * m_real + m_imag * m_imag;
00182 }
00183
00188 TypeName getPhase() const
00189 {
00190 return atan2(m_imag, m_real);
00191 }
00192
00193
00194
00199 TypeName getReal() const
00200 {
00201 return m_real;
00202 }
00203
00208 TypeName getImaginary() const
00209 {
00210 return m_imag;
00211 }
00212
00213
00214
00215 protected:
00216
00217
00218
00219 TypeName m_real;
00220 TypeName m_imag;
00221 };
00222
00224 EXPONENT_TEMPLATE_CLASS_IMPLEMENTATION(TComplex<TypeName>, TypeName, CCountedObject);
00226 }
00227 }
00228 #endif // End of TComplex.hpp