Complex.hxx
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 #ifndef CLAM_Complex_hxx
00024 #define CLAM_Complex_hxx
00025
00026 #include <iosfwd>
00027 #include "CLAM_Math.hxx"
00028 #include "TypeInfo.hxx"
00029
00030 namespace CLAM
00031 {
00032
00033 class Complex
00034 {
00035 typedef TData T;
00036 private:
00037 T mRe;
00038 T mIm;
00039
00040 public:
00041 Complex(T re = 0.f, T im = 0.f) : mRe(re), mIm(im) {}
00042 Complex(const Complex &rhs) : mRe(rhs.mRe), mIm(rhs.mIm) {}
00043
00044 const T Real() const { return mRe; }
00045 const T Imag() const { return mIm; }
00046
00047 void SetReal(const T re) { mRe = re; }
00048 void SetImag(const T im) { mIm = im; }
00049
00051 const T Mag() const
00052 {
00053 #ifdef CLAM_OPTIMIZE
00054 const float insignificant = 0.000001;
00055 T absIm = Abs(mIm);
00056 T absRe = Abs(mRe);
00057 if(absIm<insignificant && absRe>insignificant) return absRe;
00058 if(absRe<insignificant && absIm>insignificant) return absIm;
00059 #endif
00060 return CLAM_sqrt(mRe*mRe + mIm*mIm);
00061 }
00062
00064 const T SquaredMag() const
00065 {
00066 return mRe*mRe + mIm*mIm;
00067 }
00068
00070 const T Ang() const
00071 {
00072 return CLAM_atan2(mIm, mRe);
00073 }
00074
00076 Complex ToPolar(const T& r, const T& theta)
00077 {
00078 return Complex(r*CLAM_cos(theta), r*CLAM_sin(theta));
00079 }
00080
00081
00082
00084 Complex& operator = (const T re)
00085 {
00086 mRe = re;
00087 mIm = 0.f;
00088 return *this;
00089 }
00090
00092 Complex& operator = (const Complex& rhs)
00093 {
00094 mRe = rhs.mRe;
00095 mIm = rhs.mIm;
00096 return *this;
00097
00098 }
00099
00101 Complex& operator += (const Complex& rhs)
00102 {
00103 mRe += rhs.mRe;
00104 mIm += rhs.mIm;
00105 return *this;
00106 }
00107
00109 Complex& operator -= (const Complex& rhs)
00110 {
00111 mRe -= rhs.mRe;
00112 mIm -= rhs.mIm;
00113 return *this;
00114 }
00115
00117 Complex operator + (const Complex& rhs) const
00118 {
00119 return Complex(mRe + rhs.mRe, mIm + rhs.mIm);
00120 }
00121
00123 Complex operator - (const Complex& rhs) const
00124 {
00125 return Complex(mRe - rhs.mRe, mIm - rhs.mIm);
00126 }
00127
00129 Complex operator * (const Complex& rhs) const
00130 {
00131 return Complex(mRe*rhs.mRe - mIm*rhs.mIm, mRe*rhs.mIm + rhs.mRe*mIm);
00132 }
00133
00135 Complex operator * (const T& scalar) const
00136 {
00137 return Complex(mRe*scalar, mIm*scalar);
00138 }
00139
00141 Complex operator / (const Complex& rhs) const
00142 {
00143 const float rhsInvSquaredMag = 1.f/(rhs.mRe*rhs.mRe + rhs.mIm*rhs.mIm);
00144
00145 return Complex((mRe*rhs.mRe + mIm*rhs.mIm)*rhsInvSquaredMag, (rhs.mRe*mIm - mRe*rhs.mIm)*rhsInvSquaredMag);
00146 }
00147
00149 bool operator == (const Complex& b) const
00150 {
00151 return (mRe == b.mRe) && (mIm == b.mIm);
00152 }
00153
00155 bool operator != (const Complex& b) const
00156 {
00157 return !(*this == b);
00158 }
00159
00160 };
00161
00162 std::istream& operator >> (std::istream & stream, Complex & a);
00163 std::ostream& operator << (std::ostream & stream, const Complex & a);
00164
00165 CLAM_TYPEINFOGROUP(BasicCTypeInfo, Complex);
00166
00167 }
00168
00169 #endif // CLAM_Complex_hxx
00170