Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

/home/jeroen/FOX/fox/fox-1.7.33/include/FXComplexd.h
00001 /********************************************************************************
00002 *                                                                               *
00003 *          D o u b l e - P r e c i s i o n   C o m p l e x   N u m b e r        *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 2006,2012 by Jeroen van der Zijp.   All Rights Reserved.        *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or modify          *
00009 * it under the terms of the GNU Lesser General Public License as published by   *
00010 * the Free Software Foundation; either version 3 of the License, or             *
00011 * (at your option) any later version.                                           *
00012 *                                                                               *
00013 * This library is distributed in the hope that it will be useful,               *
00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 *
00016 * GNU Lesser General Public License for more details.                           *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public License      *
00019 * along with this program.  If not, see <http://www.gnu.org/licenses/>          *
00020 ********************************************************************************/
00021 #ifndef FXCOMPLEXD_H
00022 #define FXCOMPLEXD_H
00023 
00024 
00025 namespace FX {
00026 
00027 
00029 class FXAPI FXComplexd {
00030 public:
00031   FXdouble re;
00032   FXdouble im;
00033 public:
00034 
00036   FXComplexd(){ }
00037 
00039   FXComplexd(FXdouble r):re(r),im(0.0){ }
00040 
00042   FXComplexd(FXdouble r,FXdouble i):re(r),im(i){ }
00043 
00045   FXComplexd(const FXComplexd& c):re(c.re),im(c.im){ }
00046 
00048   FXComplexd& set(FXdouble r){ re=r; im=0.0; return *this; }
00049 
00051   FXComplexd& set(FXdouble r,FXdouble i){ re=r; im=i; return *this;}
00052 
00054   FXComplexd& set(const FXComplexd& c){ re=c.re; im=c.im; return *this;}
00055 
00057   FXbool operator!() const { return (re==0.0) && (im==0.0); }
00058 
00060   FXdouble modulus2() const { return re*re+im*im; }
00061 
00063   FXdouble modulus() const { return sqrt(modulus2()); }
00064 
00066   FXdouble argument() const { return atan2(im,re); }
00067 
00069   FXdouble& operator[](FXint i){ return (&re)[i]; }
00070 
00072   const FXdouble& operator[](FXint i) const { return (&re)[i]; }
00073 
00075   FXComplexd operator+() const { return *this; }
00076   FXComplexd operator-() const { return FXComplexd(-re,-im); }
00077 
00079   FXComplexd& operator=(const FXdouble r){ return set(r); }
00080 
00082   FXComplexd& operator=(const FXComplexd& c){ return set(c); }
00083 
00085   FXComplexd& operator+=(FXdouble r){ re+=r; return *this; }
00086   FXComplexd& operator-=(FXdouble r){ re-=r; return *this; }
00087   FXComplexd& operator*=(FXdouble r){ re*=r; im*=r; return *this; }
00088   FXComplexd& operator/=(FXdouble r){ re/=r; im/=r; return *this; }
00089 
00091   FXComplexd& operator+=(const FXComplexd& c){ return set(re+c.re,im+c.im); }
00092   FXComplexd& operator-=(const FXComplexd& c){ return set(re-c.re,im-c.im); }
00093   FXComplexd& operator*=(const FXComplexd& c){ return set(re*c.re-im*c.im,re*c.im+im*c.re); }
00094   FXComplexd& operator/=(const FXComplexd& c){ FXdouble m=c.modulus2(); return set((re*c.re+im*c.im)/m,(im*c.re-re*c.im)/m); }
00095 
00097  ~FXComplexd(){}
00098   };
00099 
00100 
00102 inline FXComplexd conjugate(const FXComplexd& c){ return FXComplexd(c.re,-c.im); }
00103 
00105 inline FXComplexd polar(FXdouble mod,FXdouble arg){ return FXComplexd(cos(arg)*mod,sin(arg)*mod); }
00106 
00108 inline FXComplexd exponent(const FXComplexd& c){ return polar(exp(c.re),c.im); }
00109 
00111 inline FXComplexd logarithm(const FXComplexd& c){ return FXComplexd(log(c.modulus()),c.argument()); }
00112 
00113 
00115 inline FXbool operator==(const FXComplexd& c,FXdouble r){ return c.re==r && c.im==0.0; }
00116 inline FXbool operator!=(const FXComplexd& c,FXdouble r){ return c.re!=r || c.im!=0.0; }
00117 
00119 inline FXbool operator==(FXdouble r,const FXComplexd& c){ return r==c.re && c.im==0.0; }
00120 inline FXbool operator!=(FXdouble r,const FXComplexd& c){ return r!=c.re || c.im!=0.0; }
00121 
00123 inline FXbool operator==(const FXComplexd& a,const FXComplexd& b){ return a.re==b.re && a.im==b.im; }
00124 inline FXbool operator!=(const FXComplexd& a,const FXComplexd& b){ return a.re!=b.re || a.im!=b.im; }
00125 
00127 inline FXComplexd operator+(const FXComplexd& a,FXdouble b){ return FXComplexd(a.re+b,a.im); }
00128 inline FXComplexd operator-(const FXComplexd& a,FXdouble b){ return FXComplexd(a.re-b,a.im); }
00129 inline FXComplexd operator*(const FXComplexd& a,FXdouble b){ return FXComplexd(a.re*b,a.im*b); }
00130 inline FXComplexd operator/(const FXComplexd& a,FXdouble b){ return FXComplexd(a.re/b,a.im/b); }
00131 
00133 inline FXComplexd operator+(FXdouble a,const FXComplexd& b){ return FXComplexd(a+b.re,b.im); }
00134 inline FXComplexd operator-(FXdouble a,const FXComplexd& b){ return FXComplexd(a-b.re,-b.im); }
00135 inline FXComplexd operator*(FXdouble a,const FXComplexd& b){ return FXComplexd(a*b.re,a*b.im); }
00136 inline FXComplexd operator/(FXdouble a,const FXComplexd& b){ FXdouble m=b.modulus2(); return FXComplexd((a*b.re)/m,(-a*b.im)/m); }
00137 
00139 inline FXComplexd operator+(const FXComplexd& a,const FXComplexd& b){ return FXComplexd(a.re+b.re,a.im+b.im); }
00140 inline FXComplexd operator-(const FXComplexd& a,const FXComplexd& b){ return FXComplexd(a.re-b.re,a.im-b.im); }
00141 inline FXComplexd operator*(const FXComplexd& a,const FXComplexd& b){ return FXComplexd(a.re*b.re-a.im*b.im,a.re*b.im+a.im*b.re); }
00142 inline FXComplexd operator/(const FXComplexd& a,const FXComplexd& b){ FXdouble m=b.modulus2(); return FXComplexd((a.re*b.re+a.im*b.im)/m,(a.im*b.re-a.re*b.im)/m); }
00143 
00145 extern FXAPI FXStream& operator<<(FXStream& store,const FXComplexd& c);
00146 
00148 extern FXAPI FXStream& operator>>(FXStream& store,FXComplexd& c);
00149 
00150 }
00151 
00152 #endif

Copyright © 1997-2011 Jeroen van der Zijp