annotate xcomplex.h @ 5:5f3c32dc6e17

* Adjust comment syntax to permit Doxygen to generate HTML documentation; add Doxyfile
author Chris Cannam
date Wed, 06 Oct 2010 15:19:49 +0100
parents 6422640a802f
children c6528c38b23c
rev   line source
xue@1 1 #ifndef XCOMPLEX
xue@1 2 #define XCOMPLEX
xue@1 3
xue@1 4
Chris@5 5 #include <math.h>
Chris@5 6
Chris@5 7 /**
Chris@5 8 \file xcomplex.h - Xue's complex number class.
xue@1 9
xue@1 10 This classes is modelled after standard c++ complex class, with a few additional methods. Unused
xue@1 11 standard member and non-member functions/operators may not have been implemented.
xue@1 12 */
xue@1 13
xue@1 14 template <class T> class cmplx
xue@1 15 {
xue@1 16 public:
xue@1 17 //standard members
xue@1 18 T x;
xue@1 19 T y;
xue@1 20 cmplx(){}
xue@1 21 cmplx(const T& c, const T& d) {x=c; y=d;}
xue@1 22 cmplx(const T& c) {x=c; y=0;}
xue@1 23 template<class X> cmplx(const cmplx<X>& c){x=c.x, y=c.y;}
xue@1 24
xue@1 25 T real(){return x;}
xue@1 26 T imag(){return y;}
xue@1 27
xue@1 28 cmplx<T>& operator=(const T& c){x=c; y=0; return *this;}
xue@1 29 template<class X> cmplx<T>& operator+=(const X& c){x+=c; y=0; return *this;}
xue@1 30 template<class X> cmplx<T>& operator-=(const X& c){x-=c; y=0; return *this;}
xue@1 31 template<class X> cmplx<T>& operator*=(const X& c){x*=c; y*=c; return *this;}
xue@1 32 template<class X> cmplx<T>& operator/=(const X& c){x/=c; y/=c; return *this;}
xue@1 33 template<class X> cmplx<T>& operator=(const cmplx<X>& c){x=c.x; y=c.y; return* this;}
xue@1 34
xue@1 35 template<class X> cmplx<T>& operator+=(const cmplx<X>& c){x+=c.x; y+=c.y; return *this;}
xue@1 36 template<class X> cmplx<T>& operator-=(const cmplx<X>& c){x-=c.x; y-=c.y; return *this;}
xue@1 37 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;}
xue@1 38 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;}
xue@1 39
xue@1 40 //non-standard members
xue@1 41 //operator~: square of absolute value
xue@1 42 T operator~() {return x*x+y*y;}
xue@1 43 //operator*: complex conjugate
xue@1 44 cmplx<T> operator*(){cmplx<T> result; result.x=x; result.y=-y; return result;}
xue@1 45 //operator^: multiplicaiton with the complex conjugate of the argument
xue@1 46 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;}
xue@1 47 //cinv: complex reciprocal
xue@1 48 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;}
xue@1 49 //rotate: rotate by an angle
xue@1 50 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;}
xue@1 51 }; //*/
xue@1 52 //standard Non-member Operators
xue@1 53 template<class Ta, class Tb> cmplx<Ta> operator+(const cmplx<Ta>& a, const cmplx<Tb>& b){cmplx<Ta> result=a; result+=b; return result;}
xue@1 54 template<class Ta, class Tb> cmplx<Ta> operator+(const cmplx<Ta>& a, Tb& b){cmplx<Ta> result=a; result+=b; return result;}
xue@1 55 template<class T> cmplx<T> operator+(T a, const cmplx<T>& b){cmplx<T> result=a; result+=b; return result;}
xue@1 56 template<class Ta, class Tb> cmplx<Ta> operator-(const cmplx<Ta>& a, const cmplx<Tb>& b){cmplx<Ta> result=a; result-=b; return result;}
xue@1 57 template<class Ta, class Tb> cmplx<Ta> operator-(const cmplx<Ta>& a, Tb& b){cmplx<Ta> result=a; result-=b; return result;}
xue@1 58 template<class T> cmplx<T> operator-(T a, const cmplx<T>& b){cmplx<T> result=a; result-=b; return result;}
xue@1 59 template<class Ta, class Tb> cmplx<Ta> operator*(const cmplx<Ta>& a, const cmplx<Tb>& b){cmplx<Ta> result=a; result*=b; return result;}
xue@1 60 template<class Ta, class Tb> cmplx<Ta> operator*(const cmplx<Ta>& a, Tb& b){cmplx<Ta> result=a; result*=b; return result;}
xue@1 61 template<class T> cmplx<T> operator*(T& a, const cmplx<T>& b){cmplx<T> result=b; result*=a; return result;}
xue@1 62 template<class Ta, class Tb> cmplx<Ta> operator/(const cmplx<Ta>& a, const cmplx<Tb>& b){cmplx<Ta> result=a; result/=b; return result;}
xue@1 63 template<class Ta, class Tb> cmplx<Ta> operator/(const cmplx<Ta>& a, Tb& b){cmplx<Ta> result=a; result/=b; return result;}
xue@1 64 template<class T> cmplx<T> operator/(T a, const cmplx<T>& b){cmplx<T> result=a; result/=b; return result;}
xue@1 65 template<class T> cmplx<T> operator+(const cmplx<T>& a){return a;}
xue@1 66 template<class T> cmplx<T> operator-(const cmplx<T>& a){cmplx<T> result; result.x=-a.x; result.y=-a.y; return result;}
xue@1 67 template<class Ta, class Tb> bool operator==(const cmplx<Ta>& a, const cmplx<Tb>& b){return (a.x==b.x && a.y==b.y);}
xue@1 68 template<class Ta, class Tb> bool operator==(const cmplx<Ta>& a, Tb b){return (a.x==b && a.y==0);}
xue@1 69 template<class Ta, class Tb> bool operator==(Ta a, const cmplx<Tb>& b){return (a==b.x && 0==b.y);}
xue@1 70 template<class Ta, class Tb> bool operator!=(const cmplx<Ta>& a, const cmplx<Tb>& b){return (a.x!=b.x || a.y!=b.y);}
xue@1 71 template<class Ta, class Tb> bool operator!=(const cmplx<Ta>& a, Tb b){return (a.x!=b || a.y!=0);}
xue@1 72 template<class Ta, class Tb> bool operator!=(Ta a, const cmplx<Tb>& b){return (a!=b.x || 0!=b.y);}
xue@1 73 /*
xue@1 74 template <class T, class charT, class traits> basic_istream<charT, traits>& operator>>(istream&, complex<T>&);
xue@1 75 template <class T, class charT, class traits> basic_ostream<charT, traits>& operator<<(ostream&, const complex<T>&);
xue@1 76 */
xue@1 77 //Values
xue@1 78 template<class T> T real(const cmplx<T>& a){return a.x;}
xue@1 79 template<class T> T imag(const cmplx<T>& a){return a.y;}
xue@1 80 template<class T> T abs(const cmplx<T>& a){return sqrt(a.x*a.x+a.y*a.y);}
xue@1 81 template<class T> T fabs(const cmplx<T>& a){return sqrt(a.x*a.x+a.y*a.y);}
xue@1 82 template<class T> T arg(const cmplx<T>& a){return (a.x==0 && a.y==0)?0:atan2(a.y, a.x);}
xue@1 83 template<class T> T norm(const cmplx<T>& a){return a.x*a.x+a.y*a.y;}
xue@1 84 template<class T> cmplx<T> conj(const cmplx<T>& a){cmplx<T> result; result.x=a.x; result.y=-a.y; return result;}
xue@1 85 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;}
xue@1 86 //Transcendentals
xue@1 87 /*
xue@1 88 template<class T> cmplx<T> cos (const cmplx<T>&);
xue@1 89 template<class T> cmplx<T> cosh (const cmplx<T>&);
xue@1 90 */
xue@1 91 template<class T> cmplx<T> exp(const cmplx<T>& a){return polar(exp(a.x), a.y);}
xue@1 92 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;}
xue@1 93 /*
xue@1 94 template<class T> cmplx<T> log10 (const cmplx<T>&);
xue@1 95 template<class T> cmplx<T> pow (const cmplx<T>&, int);
xue@1 96 */
xue@1 97 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);}
xue@1 98 /*
xue@1 99 template<class T> cmplx<T> pow (const cmplx<T>&, const cmplx<T>&);
xue@1 100 template<class T> cmplx<T> pow (const T&, const cmplx<T>&);
xue@1 101 template<class T> cmplx<T> sin (const cmplx<T>&);
xue@1 102 template<class T> cmplx<T> sinh (const cmplx<T>&);
xue@1 103 */
xue@1 104 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;}
xue@1 105 /*
xue@1 106 template<class T> cmplx<T> tan (const cmplx<T>&);
xue@1 107 template<class T> cmplx<T> tanh (const cmplx<T>&);
xue@1 108 */
xue@1 109
xue@1 110 //non-standard non-member functions
xue@1 111 //template operator^: multiplying one complex number with the complex conjugate of another
xue@1 112 template<class T> cmplx<T> operator^(const cmplx<T>& a, const cmplx<T>& b){cmplx<T> result=a; result^=b; return result;}
xue@1 113
xue@1 114 typedef cmplx<double> cdouble;
xue@1 115 typedef cmplx<float> cfloat;
xue@1 116
xue@1 117 #endif