xcomplex.h
Go to the documentation of this file.
1 /*
2  Harmonic sinusoidal modelling and tools
3 
4  C++ code package for harmonic sinusoidal modelling and relevant signal processing.
5  Centre for Digital Music, Queen Mary, University of London.
6  This file copyright 2011 Wen Xue.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version.
12 */
13 #ifndef XCOMPLEX
14 #define XCOMPLEX
15 
16 
17 #include <math.h>
18 
26 template <class T> class cmplx
27 {
28 public:
29 //standard members
30  T x;
31  T y;
32  cmplx(){}
33  cmplx(const T& c, const T& d) {x=c; y=d;}
34  cmplx(const T& c) {x=c; y=0;}
35  template<class X> cmplx(const cmplx<X>& c){x=c.x, y=c.y;}
36 
37  T real(){return x;}
38  T imag(){return y;}
39 
40  cmplx<T>& operator=(const T& c){x=c; y=0; return *this;}
41  template<class X> cmplx<T>& operator+=(const X& c){x+=c; return *this;}
42  template<class X> cmplx<T>& operator-=(const X& c){x-=c; return *this;}
43  template<class X> cmplx<T>& operator*=(const X& c){x*=c; y*=c; return *this;}
44  template<class X> cmplx<T>& operator/=(const X& c){x/=c; y/=c; return *this;}
45  template<class X> cmplx<T>& operator=(const cmplx<X>& c){x=c.x; y=c.y; return* this;}
46 
47  template<class X> cmplx<T>& operator+=(const cmplx<X>& c){x+=c.x; y+=c.y; return *this;}
48  template<class X> cmplx<T>& operator-=(const cmplx<X>& c){x-=c.x; y-=c.y; return *this;}
49  template<class X> cmplx<T>& operator*=(const cmplx<X>& c){T tmpx=x*c.x-y*c.y; y=x*c.y+y*c.x; x=tmpx; return *this;}
50  template<class X> cmplx<T>& operator/=(const cmplx<X>& c){if (c.y==0){x/=c.x; y/=c.x;} else if (c.x==0){T tmpx=y/c.y; y=-x/c.y; x=tmpx;} else {T xm=c.x*c.x+c.y*c.y; T tmpx=(c.x*x+c.y*y)/xm; y=(y*c.x-x*c.y)/xm; x=tmpx;} return *this;}
51 
52 //non-standard members
53  //operator~: square of absolute value
54  T operator~() {return x*x+y*y;}
55  //operator*: complex conjugate
56  cmplx<T> operator*(){cmplx<T> result; result.x=x; result.y=-y; return result;}
57  //operator^: multiplicaiton with the complex conjugate of the argument
58  cmplx<T> operator^(const cmplx &b){cmplx<T> result; result.x=x*b.x+y*b.y; result.y=y*b.x-x*b.y; return result;}
59  //cinv: complex reciprocal
60  cmplx<T> cinv(){cmplx<T> result; if (y==0) result.x=1/x, result.y=0; else if (x==0) result.y=-1/y, result.x=0; else{T xm=x*x+y*y; result.x=x/xm; result.y=-y/xm;} return result;}
61  //rotate: rotate by an angle
62  cmplx<T>& rotate(const T ph) {double s=sin(ph), c=cos(ph), tmpx; tmpx=x*c-y*s; y=x*s+y*c; x=tmpx; return *this;}
63 }; //*/
64 //standard Non-member Operators
65 template<class Ta, class Tb> cmplx<Ta> operator+(const cmplx<Ta>& a, const cmplx<Tb>& b){cmplx<Ta> result=a; result+=b; return result;}
66 template<class Ta, class Tb> cmplx<Ta> operator+(const cmplx<Ta>& a, Tb& b){cmplx<Ta> result=a; result+=b; return result;}
67 template<class T> cmplx<T> operator+(T a, const cmplx<T>& b){cmplx<T> result=a; result+=b; return result;}
68 template<class Ta, class Tb> cmplx<Ta> operator-(const cmplx<Ta>& a, const cmplx<Tb>& b){cmplx<Ta> result=a; result-=b; return result;}
69 template<class Ta, class Tb> cmplx<Ta> operator-(const cmplx<Ta>& a, Tb& b){cmplx<Ta> result=a; result-=b; return result;}
70 template<class T> cmplx<T> operator-(T a, const cmplx<T>& b){cmplx<T> result=a; result-=b; return result;}
71 template<class Ta, class Tb> cmplx<Ta> operator*(const cmplx<Ta>& a, const cmplx<Tb>& b){cmplx<Ta> result=a; result*=b; return result;}
72 template<class Ta, class Tb> cmplx<Ta> operator*(const cmplx<Ta>& a, Tb& b){cmplx<Ta> result=a; result*=b; return result;}
73 template<class T> cmplx<T> operator*(T& a, const cmplx<T>& b){cmplx<T> result=b; result*=a; return result;}
74 template<class Ta, class Tb> cmplx<Ta> operator/(const cmplx<Ta>& a, const cmplx<Tb>& b){cmplx<Ta> result=a; result/=b; return result;}
75 template<class Ta, class Tb> cmplx<Ta> operator/(const cmplx<Ta>& a, Tb& b){cmplx<Ta> result=a; result/=b; return result;}
76 template<class T> cmplx<T> operator/(T a, const cmplx<T>& b){cmplx<T> result=a; result/=b; return result;}
77 template<class T> cmplx<T> operator+(const cmplx<T>& a){return a;}
78 template<class T> cmplx<T> operator-(const cmplx<T>& a){cmplx<T> result; result.x=-a.x; result.y=-a.y; return result;}
79 template<class Ta, class Tb> bool operator==(const cmplx<Ta>& a, const cmplx<Tb>& b){return (a.x==b.x && a.y==b.y);}
80 template<class Ta, class Tb> bool operator==(const cmplx<Ta>& a, Tb b){return (a.x==b && a.y==0);}
81 template<class Ta, class Tb> bool operator==(Ta a, const cmplx<Tb>& b){return (a==b.x && 0==b.y);}
82 template<class Ta, class Tb> bool operator!=(const cmplx<Ta>& a, const cmplx<Tb>& b){return (a.x!=b.x || a.y!=b.y);}
83 template<class Ta, class Tb> bool operator!=(const cmplx<Ta>& a, Tb b){return (a.x!=b || a.y!=0);}
84 template<class Ta, class Tb> bool operator!=(Ta a, const cmplx<Tb>& b){return (a!=b.x || 0!=b.y);}
85 /*
86 template <class T, class charT, class traits> basic_istream<charT, traits>& operator>>(istream&, complex<T>&);
87 template <class T, class charT, class traits> basic_ostream<charT, traits>& operator<<(ostream&, const complex<T>&);
88 */
89 //Values
90 template<class T> T real(const cmplx<T>& a){return a.x;}
91 template<class T> T imag(const cmplx<T>& a){return a.y;}
92 template<class T> T abs(const cmplx<T>& a){return sqrt(a.x*a.x+a.y*a.y);}
93 template<class T> T fabs(const cmplx<T>& a){return sqrt(a.x*a.x+a.y*a.y);}
94 template<class T> T arg(const cmplx<T>& a){return (a.x==0 && a.y==0)?0:atan2(a.y, a.x);}
95 template<class T> T norm(const cmplx<T>& a){return a.x*a.x+a.y*a.y;}
96 template<class T> cmplx<T> conj(const cmplx<T>& a){cmplx<T> result; result.x=a.x; result.y=-a.y; return result;}
97 template<class T> cmplx<T> polar(const T& r, const T& theta){cmplx<T> result; result.x=r*cos(theta); result.y=r*sin(theta); return result;}
98 //Transcendentals
99 /*
100 template<class T> cmplx<T> cos (const cmplx<T>&);
101 template<class T> cmplx<T> cosh (const cmplx<T>&);
102 */
103 template<class T> cmplx<T> exp(const cmplx<T>& a){return polar(exp(a.x), a.y);}
104 template<class T> cmplx<T> log(const cmplx<T>& a){cmplx<T> result; result.x=0.5*log(norm(a)); result.y=arg(a); return result;}
105 /*
106 template<class T> cmplx<T> log10 (const cmplx<T>&);
107 template<class T> cmplx<T> pow (const cmplx<T>&, int);
108 */
109 template<class T> cmplx<T> pow(const cmplx<T>& a, T& e){cmplx<T> result; T rad=abs(a); T angle=arg(a); return polar(rad, angle*e);}
110 /*
111 template<class T> cmplx<T> pow (const cmplx<T>&, const cmplx<T>&);
112 template<class T> cmplx<T> pow (const T&, const cmplx<T>&);
113 template<class T> cmplx<T> sin (const cmplx<T>&);
114 template<class T> cmplx<T> sinh (const cmplx<T>&);
115 */
116 template<class T> cmplx<T> sqrt(const cmplx<T>& a){cmplx<T> result; if (a.y==0) {if (a.x>=0){result.x=sqrt(a.x); result.y=0;} else{result.y=sqrt(-a.x); result.x=0;}} else {result.y=sqrt((sqrt(a.x*a.x+a.y*a.y)-a.x)*0.5); result.x=0.5*a.y/result.y;} return result;}
117 /*
118 template<class T> cmplx<T> tan (const cmplx<T>&);
119 template<class T> cmplx<T> tanh (const cmplx<T>&);
120 */
121 
122 //non-standard non-member functions
123 //template operator^: multiplying one complex number with the complex conjugate of another
124 template<class T> cmplx<T> operator^(const cmplx<T>& a, const cmplx<T>& b){cmplx<T> result=a; result^=b; return result;}
125 
126 typedef cmplx<double> cdouble;
127 typedef cmplx<float> cfloat;
128 
129 #endif
Definition: xcomplex.h:26