CLAM-Development
1.1
|
00001 /* 00002 * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG) 00003 * UNIVERSITAT POMPEU FABRA 00004 * 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 * 00020 */ 00021 00022 00023 #ifndef _ComplexTmplDec_ 00024 #define _ComplexTmplDec_ 00025 00026 #include <iosfwd> 00027 #include "CLAM_Math.hxx" 00028 00029 namespace CLAM 00030 { 00031 00032 template <class T> 00033 class ComplexTmpl 00034 { 00035 private: 00036 T mRe; 00037 T mIm; 00038 00039 public: 00040 ComplexTmpl(T re = 0.f, T im = 0.f) : mRe(re), mIm(im) {} 00041 ComplexTmpl(const ComplexTmpl<T> &rhs) : mRe(rhs.mRe), mIm(rhs.mIm) {} 00042 00043 const T Real() const { return mRe; } //< accessor returns the real part 00044 const T Imag() const { return mIm; } //< accessor returns the imaginary part 00045 00046 void SetReal(const T re) { mRe = re; } //< accesor sets the real part 00047 void SetImag(const T im) { mIm = im; } //< accesor sets the imaginary part 00048 00050 const T Mag() const 00051 { 00052 #ifdef CLAM_OPTIMIZE 00053 const float insignificant = 0.000001; 00054 T absIm = Abs(mIm); 00055 T absRe = Abs(mRe); 00056 if(absIm<insignificant && absRe>insignificant) return absRe; 00057 if(absRe<insignificant && absIm>insignificant) return absIm; 00058 #endif 00059 return CLAM_sqrt(mRe*mRe + mIm*mIm); 00060 } 00061 00063 const T SquaredMag() const 00064 { 00065 return mRe*mRe + mIm*mIm; 00066 } 00067 00069 const T Ang() const 00070 { 00071 return CLAM_atan2(mIm, mRe); 00072 } 00073 00075 ComplexTmpl<T> ToPolar(const T& r, const T& theta) 00076 { 00077 return ComplexTmpl<T>(r*CLAM_cos(theta), r*CLAM_sin(theta)); 00078 } 00079 00080 // ------ member operators ... ------ 00081 00083 ComplexTmpl<T>& operator = (const T re) 00084 { 00085 mRe = re; 00086 mIm = 0.f; 00087 return *this; 00088 } 00089 00091 ComplexTmpl<T>& operator = (const ComplexTmpl<T>& rhs) 00092 { 00093 mRe = rhs.mRe; 00094 mIm = rhs.mIm; 00095 return *this; 00096 00097 } 00098 00100 ComplexTmpl<T>& operator += (const ComplexTmpl<T>& rhs) 00101 { 00102 mRe += rhs.mRe; 00103 mIm += rhs.mIm; 00104 return *this; 00105 } 00106 00108 ComplexTmpl<T>& operator -= (const ComplexTmpl<T>& rhs) 00109 { 00110 mRe -= rhs.mRe; 00111 mIm -= rhs.mIm; 00112 return *this; 00113 } 00114 00116 ComplexTmpl<T> operator + (const ComplexTmpl<T>& rhs) const 00117 { 00118 return ComplexTmpl<T>(mRe + rhs.mRe, mIm + rhs.mIm); 00119 } 00120 00122 ComplexTmpl<T> operator - (const ComplexTmpl<T>& rhs) const 00123 { 00124 return ComplexTmpl<T>(mRe - rhs.mRe, mIm - rhs.mIm); 00125 } 00126 00128 ComplexTmpl<T> operator * (const ComplexTmpl<T>& rhs) const 00129 { 00130 return ComplexTmpl<T>(mRe*rhs.mRe - mIm*rhs.mIm, mRe*rhs.mIm + rhs.mRe*mIm); 00131 } 00132 00134 ComplexTmpl<T> operator * (const T& scalar) const 00135 { 00136 return ComplexTmpl<T>(mRe*scalar, mIm*scalar); 00137 } 00138 00140 ComplexTmpl<T> operator / (const ComplexTmpl<T>& rhs) const 00141 { 00142 const float rhsInvSquaredMag = 1.f/(rhs.mRe*rhs.mRe + rhs.mIm*rhs.mIm); 00143 00144 return ComplexTmpl<T>((mRe*rhs.mRe + mIm*rhs.mIm)*rhsInvSquaredMag, (rhs.mRe*mIm - mRe*rhs.mIm)*rhsInvSquaredMag); 00145 } 00146 00148 bool operator == (const ComplexTmpl<T>& b) const 00149 { 00150 return (mRe == b.mRe) && (mIm == b.mIm); 00151 } 00152 00154 bool operator != (const ComplexTmpl<T>& b) const 00155 { 00156 return !(*this == b); 00157 } 00158 00159 }; 00160 00161 template <class T> 00162 std::istream& operator >> (std::istream & stream, ComplexTmpl<T> & a); 00163 00164 template <class T> 00165 std::ostream& operator << (std::ostream & stream, const ComplexTmpl<T> & a); 00166 00167 } // namespace CLAM 00168 00169 #endif // _ComplexTmplDec_ 00170