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

FXComplexd.h

Go to the documentation of this file.
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,2010 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 
00028 /// Double-precision complex
00029 class FXAPI FXComplexd {
00030 public:
00031   FXdouble re;
00032   FXdouble im;
00033 public:
00034 
00035   /// Default constructor; value is not initialized
00036   FXComplexd(){ }
00037 
00038   /// Construct from real
00039   FXComplexd(FXdouble r):re(r),im(0.0){ }
00040 
00041   /// Construct from components
00042   FXComplexd(FXdouble r,FXdouble i):re(r),im(i){ }
00043 
00044   /// Initialize from another complex
00045   FXComplexd(const FXComplexd& c):re(c.re),im(c.im){ }
00046 
00047   /// Set value from real
00048   FXComplexd& set(FXdouble r){ re=r; im=0.0; return *this; }
00049 
00050   /// Set value from components
00051   FXComplexd& set(FXdouble r,FXdouble i){ re=r; im=i; return *this;}
00052 
00053   /// Set value from another complex
00054   FXComplexd& set(const FXComplexd& c){ re=c.re; im=c.im; return *this;}
00055 
00056   /// Test for non-zero
00057   operator FXbool() const { return (re!=0.0) || (im!=0.0); }
00058 
00059   /// Test if zero
00060   FXbool operator!() const { return (re==0.0) && (im==0.0); }
00061 
00062   /// Squared modulus
00063   FXdouble modulus2() const { return re*re+im*im; }
00064 
00065   /// Modulus or absolute value of complex
00066   FXdouble modulus() const { return sqrt(modulus2()); }
00067 
00068   /// Argument of complex
00069   FXdouble argument() const { return atan2(im,re); }
00070 
00071   /// Return a non-const reference to the ith element
00072   FXdouble& operator[](FXint i){ return (&re)[i]; }
00073 
00074   /// Return a const reference to the ith element
00075   const FXdouble& operator[](FXint i) const { return (&re)[i]; }
00076 
00077   /// Unary
00078   FXComplexd operator+() const { return *this; }
00079   FXComplexd operator-() const { return FXComplexd(-re,-im); }
00080 
00081   /// Assignment from real
00082   FXComplexd& operator=(const FXdouble r){ return set(r); }
00083 
00084   /// Assignment from another complex
00085   FXComplexd& operator=(const FXComplexd& c){ return set(c); }
00086 
00087   /// Assigning operators with real
00088   FXComplexd& operator+=(FXdouble r){ re+=r; return *this; }
00089   FXComplexd& operator-=(FXdouble r){ re-=r; return *this; }
00090   FXComplexd& operator*=(FXdouble r){ re*=r; im*=r; return *this; }
00091   FXComplexd& operator/=(FXdouble r){ re/=r; im/=r; return *this; }
00092 
00093   /// Assigning operators with another complex
00094   FXComplexd& operator+=(const FXComplexd& c){ return set(re+c.re,im+c.im); }
00095   FXComplexd& operator-=(const FXComplexd& c){ return set(re-c.re,im-c.im); }
00096   FXComplexd& operator*=(const FXComplexd& c){ return set(re*c.re-im*c.im,re*c.im+im*c.re); }
00097   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); }
00098 
00099   /// Equality between one complex and another
00100   FXbool operator==(const FXComplexd& c) const { return re==c.re && im==c.im; }
00101   FXbool operator!=(const FXComplexd& c) const { return re!=c.re || im!=c.im; }
00102   };
00103 
00104 
00105 /// Return complex complex conjugate
00106 inline FXComplexd conjugate(const FXComplexd& c){ return FXComplexd(c.re,-c.im); }
00107 
00108 /// Return complex number from modulus and argument
00109 inline FXComplexd polar(FXdouble mod,FXdouble arg){ return FXComplexd(cos(arg)*mod,sin(arg)*mod); }
00110 
00111 /// Returns the complex base e exponential of c
00112 inline FXComplexd exponent(const FXComplexd& c){ return polar(exp(c.re),c.im); }
00113 
00114 /// Returns the complex base e logarithm of c
00115 inline FXComplexd logarithm(const FXComplexd& c){ return FXComplexd(log(c.modulus()),c.argument()); }
00116 
00117 
00118 /// Equality between complex and real
00119 inline FXbool operator==(const FXComplexd& c,FXdouble r){ return c.re==r && c.im==0.0; }
00120 inline FXbool operator!=(const FXComplexd& c,FXdouble r){ return c.re!=r || c.im!=0.0; }
00121 
00122 /// Equality between real and complex
00123 inline FXbool operator==(FXdouble r,const FXComplexd& c){ return r==c.re && c.im==0.0; }
00124 inline FXbool operator!=(FXdouble r,const FXComplexd& c){ return r!=c.re || c.im!=0.0; }
00125 
00126 
00127 /// Operators between one complex and another
00128 inline FXComplexd operator+(const FXComplexd& a,const FXComplexd& b){ return FXComplexd(a.re+b.re,a.im+b.im); }
00129 inline FXComplexd operator-(const FXComplexd& a,const FXComplexd& b){ return FXComplexd(a.re-b.re,a.im-b.im); }
00130 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); }
00131 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); }
00132 
00133 
00134 /// Operators between complex and real
00135 inline FXComplexd operator+(const FXComplexd& a,FXdouble b){ return FXComplexd(a.re+b,a.im); }
00136 inline FXComplexd operator-(const FXComplexd& a,FXdouble b){ return FXComplexd(a.re-b,a.im); }
00137 inline FXComplexd operator*(const FXComplexd& a,FXdouble b){ return FXComplexd(a.re*b,a.im*b); }
00138 inline FXComplexd operator/(const FXComplexd& a,FXdouble b){ return FXComplexd(a.re/b,a.im/b); }
00139 
00140 
00141 /// Operators between real and complex
00142 inline FXComplexd operator+(FXdouble a,const FXComplexd& b){ return FXComplexd(a+b.re,b.im); }
00143 inline FXComplexd operator-(FXdouble a,const FXComplexd& b){ return FXComplexd(a-b.re,b.im); }
00144 inline FXComplexd operator*(FXdouble a,const FXComplexd& b){ return FXComplexd(a*b.re,a*b.im); }
00145 inline FXComplexd operator/(FXdouble a,const FXComplexd& b){ FXdouble m=b.modulus2(); return FXComplexd((a*b.re)/m,(-a*b.im)/m); }
00146 
00147 /// Save to a stream
00148 extern FXAPI FXStream& operator<<(FXStream& store,const FXComplexd& c);
00149 
00150 /// Load from a stream
00151 extern FXAPI FXStream& operator>>(FXStream& store,FXComplexd& c);
00152 
00153 }
00154 
00155 #endif

Copyright © 1997-2010 Jeroen van der Zijp