CLAM-Development  1.4.0
ComplexTmplDec.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 
23 #ifndef _ComplexTmplDec_
24 #define _ComplexTmplDec_
25 
26 #include <iosfwd>
27 #include "CLAM_Math.hxx"
28 
29 namespace CLAM
30 {
31 
32  template <class T>
34  {
35  private:
36  T mRe;
37  T mIm;
38 
39  public:
40  ComplexTmpl(T re = 0.f, T im = 0.f) : mRe(re), mIm(im) {}
41  ComplexTmpl(const ComplexTmpl<T> &rhs) : mRe(rhs.mRe), mIm(rhs.mIm) {}
42 
43  const T Real() const { return mRe; } //< accessor returns the real part
44  const T Imag() const { return mIm; } //< accessor returns the imaginary part
45 
46  void SetReal(const T re) { mRe = re; } //< accesor sets the real part
47  void SetImag(const T im) { mIm = im; } //< accesor sets the imaginary part
48 
50  const T Mag() const
51  {
52 #ifdef CLAM_OPTIMIZE
53  const float insignificant = 0.000001;
54  T absIm = Abs(mIm);
55  T absRe = Abs(mRe);
56  if(absIm<insignificant && absRe>insignificant) return absRe;
57  if(absRe<insignificant && absIm>insignificant) return absIm;
58 #endif
59  return CLAM_sqrt(mRe*mRe + mIm*mIm);
60  }
61 
63  const T SquaredMag() const
64  {
65  return mRe*mRe + mIm*mIm;
66  }
67 
69  const T Ang() const
70  {
71  return CLAM_atan2(mIm, mRe);
72  }
73 
75  ComplexTmpl<T> ToPolar(const T& r, const T& theta)
76  {
77  return ComplexTmpl<T>(r*CLAM_cos(theta), r*CLAM_sin(theta));
78  }
79 
80  // ------ member operators ... ------
81 
84  {
85  mRe = re;
86  mIm = 0.f;
87  return *this;
88  }
89 
92  {
93  mRe = rhs.mRe;
94  mIm = rhs.mIm;
95  return *this;
96 
97  }
98 
101  {
102  mRe += rhs.mRe;
103  mIm += rhs.mIm;
104  return *this;
105  }
106 
109  {
110  mRe -= rhs.mRe;
111  mIm -= rhs.mIm;
112  return *this;
113  }
114 
117  {
118  return ComplexTmpl<T>(mRe + rhs.mRe, mIm + rhs.mIm);
119  }
120 
123  {
124  return ComplexTmpl<T>(mRe - rhs.mRe, mIm - rhs.mIm);
125  }
126 
129  {
130  return ComplexTmpl<T>(mRe*rhs.mRe - mIm*rhs.mIm, mRe*rhs.mIm + rhs.mRe*mIm);
131  }
132 
134  ComplexTmpl<T> operator * (const T& scalar) const
135  {
136  return ComplexTmpl<T>(mRe*scalar, mIm*scalar);
137  }
138 
141  {
142  const float rhsInvSquaredMag = 1.f/(rhs.mRe*rhs.mRe + rhs.mIm*rhs.mIm);
143 
144  return ComplexTmpl<T>((mRe*rhs.mRe + mIm*rhs.mIm)*rhsInvSquaredMag, (rhs.mRe*mIm - mRe*rhs.mIm)*rhsInvSquaredMag);
145  }
146 
148  bool operator == (const ComplexTmpl<T>& b) const
149  {
150  return (mRe == b.mRe) && (mIm == b.mIm);
151  }
152 
154  bool operator != (const ComplexTmpl<T>& b) const
155  {
156  return !(*this == b);
157  }
158 
159  };
160 
161  template <class T>
162  std::istream& operator >> (std::istream & stream, ComplexTmpl<T> & a);
163 
164  template <class T>
165  std::ostream& operator << (std::ostream & stream, const ComplexTmpl<T> & a);
166 
167 } // namespace CLAM
168 
169 #endif // _ComplexTmplDec_
170