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

FXComplexf.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *          S i n g 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,2009 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 * $Id: FXComplexf.h,v 1.18 2009/01/26 09:40:52 fox Exp $                        *
00022 ********************************************************************************/
00023 #ifndef FXCOMPLEXF_H
00024 #define FXCOMPLEXF_H
00025 
00026 
00027 namespace FX {
00028 
00029 
00030 /// Single-precision complex
00031 class FXAPI FXComplexf {
00032 public:
00033   FXfloat re;
00034   FXfloat im;
00035 public:
00036 
00037   /// Default constructor; value is not initialized
00038   FXComplexf(){ }
00039 
00040   /// Construct from real
00041   FXComplexf(FXfloat r):re(r),im(0.0f){ }
00042 
00043   /// Construct from components
00044   FXComplexf(FXfloat r,FXfloat i):re(r),im(i){ }
00045 
00046   /// Initialize from another complex
00047   FXComplexf(const FXComplexf& c):re(c.re),im(c.im){ }
00048 
00049   /// Set value from real
00050   FXComplexf& set(FXfloat r){ re=r; im=0.0f; return *this; }
00051 
00052   /// Set value from components
00053   FXComplexf& set(FXfloat r,FXfloat i){ re=r; im=i; return *this;}
00054 
00055   /// Set value from another complex
00056   FXComplexf& set(const FXComplexf& c){ re=c.re; im=c.im; return *this;}
00057 
00058   /// Test for non-zero
00059   operator FXbool() const { return (re!=0.0f) || (im!=0.0f); }
00060 
00061   /// Test if zero
00062   FXbool operator!() const { return (re==0.0f) && (im==0.0f); }
00063 
00064   /// Squared modulus
00065   FXfloat modulus2() const { return re*re+im*im; }
00066 
00067   /// Modulus or absolute value of complex
00068   FXfloat modulus() const { return sqrtf(modulus2()); }
00069 
00070   /// Argument of complex
00071   FXfloat argument() const { return atan2f(im,re); }
00072 
00073   /// Return a non-const reference to the ith element
00074   FXfloat& operator[](FXint i){ return (&re)[i]; }
00075 
00076   /// Return a const reference to the ith element
00077   const FXfloat& operator[](FXint i) const { return (&re)[i]; }
00078 
00079   /// Unary
00080   FXComplexf operator+() const { return *this; }
00081   FXComplexf operator-() const { return FXComplexf(-re,-im); }
00082 
00083   /// Assignment from real
00084   FXComplexf& operator=(const FXfloat r){ return set(r); }
00085 
00086   /// Assignment from another complex
00087   FXComplexf& operator=(const FXComplexf& c){ return set(c); }
00088 
00089   /// Assigning operators with real
00090   FXComplexf& operator+=(FXfloat r){ re+=r; return *this; }
00091   FXComplexf& operator-=(FXfloat r){ re-=r; return *this; }
00092   FXComplexf& operator*=(FXfloat r){ re*=r; im*=r; return *this; }
00093   FXComplexf& operator/=(FXfloat r){ re/=r; im/=r; return *this; }
00094 
00095   /// Assigning operators with another complex
00096   FXComplexf& operator+=(const FXComplexf& c){ return set(re+c.re,im+c.im); }
00097   FXComplexf& operator-=(const FXComplexf& c){ return set(re-c.re,im-c.im); }
00098   FXComplexf& operator*=(const FXComplexf& c){ return set(re*c.re-im*c.im,re*c.im+im*c.re); }
00099   FXComplexf& operator/=(const FXComplexf& c){ FXfloat m=c.modulus2(); return set((re*c.re+im*c.im)/m,(im*c.re-re*c.im)/m); }
00100 
00101   /// Equality between one complex and another
00102   FXbool operator==(const FXComplexf& c) const { return re==c.re && im==c.im; }
00103   FXbool operator!=(const FXComplexf& c) const { return re!=c.re || im!=c.im; }
00104 
00105   /// Return complex complex conjugate
00106   friend inline FXComplexf conjugate(const FXComplexf& c);
00107 
00108   /// Return complex number from modulus and argument
00109   friend inline FXComplexf polar(FXfloat mod,FXfloat arg);
00110 
00111   /// Returns the complex base e exponential of c
00112   friend inline FXComplexf exponent(const FXComplexf& c);
00113 
00114   /// Returns the complex base e logarithm of c
00115   friend inline FXComplexf logarithm(const FXComplexf& c);
00116 
00117   /// Equality between one complex and real
00118   friend inline FXbool operator==(const FXComplexf& c,FXfloat r);
00119   friend inline FXbool operator!=(const FXComplexf& c,FXfloat r);
00120 
00121   /// Equality between one real and complex
00122   friend inline FXbool operator==(FXfloat r,const FXComplexf& c);
00123   friend inline FXbool operator!=(FXfloat r,const FXComplexf& c);
00124 
00125   /// Operators between one complex and another
00126   friend inline FXComplexf operator+(const FXComplexf& a,const FXComplexf& b);
00127   friend inline FXComplexf operator-(const FXComplexf& a,const FXComplexf& b);
00128   friend inline FXComplexf operator*(const FXComplexf& a,const FXComplexf& b);
00129   friend inline FXComplexf operator/(const FXComplexf& a,const FXComplexf& b);
00130 
00131   /// Operators between complex and real
00132   friend inline FXComplexf operator+(const FXComplexf& a,FXfloat b);
00133   friend inline FXComplexf operator-(const FXComplexf& a,FXfloat b);
00134   friend inline FXComplexf operator*(const FXComplexf& a,FXfloat b);
00135   friend inline FXComplexf operator/(const FXComplexf& a,FXfloat b);
00136 
00137   /// Operators between real and complex
00138   friend inline FXComplexf operator+(FXfloat a,const FXComplexf& b);
00139   friend inline FXComplexf operator-(FXfloat a,const FXComplexf& b);
00140   friend inline FXComplexf operator*(FXfloat a,const FXComplexf& b);
00141   friend inline FXComplexf operator/(FXfloat a,const FXComplexf& b);
00142 
00143   /// Save to a stream
00144   friend FXAPI FXStream& operator<<(FXStream& store,const FXComplexf& c);
00145 
00146   /// Load from a stream
00147   friend FXAPI FXStream& operator>>(FXStream& store,FXComplexf& c);
00148   };
00149 
00150 
00151 
00152 inline FXComplexf conjugate(const FXComplexf& c){ return FXComplexf(c.re,-c.im); }
00153 inline FXComplexf polar(FXfloat mod,FXfloat arg){ return FXComplexf(cosf(arg)*mod,sinf(arg)*mod); }
00154 inline FXComplexf exponent(const FXComplexf& c){ return polar(expf(c.re),c.im); }
00155 inline FXComplexf logarithm(const FXComplexf& c){ return FXComplexf(logf(c.modulus()),c.argument()); }
00156 
00157 
00158 inline FXbool operator==(const FXComplexf& c,FXfloat r){ return c.re==r && c.im==0.0f; }
00159 inline FXbool operator!=(const FXComplexf& c,FXfloat r){ return c.re!=r || c.im!=0.0f; }
00160 
00161 inline FXbool operator==(FXfloat r,const FXComplexf& c){ return r==c.re && c.im==0.0f; }
00162 inline FXbool operator!=(FXfloat r,const FXComplexf& c){ return r!=c.re || c.im!=0.0f; }
00163 
00164 
00165 inline FXComplexf operator+(const FXComplexf& a,const FXComplexf& b){ return FXComplexf(a.re+b.re,a.im+b.im); }
00166 inline FXComplexf operator-(const FXComplexf& a,const FXComplexf& b){ return FXComplexf(a.re-b.re,a.im-b.im); }
00167 inline FXComplexf operator*(const FXComplexf& a,const FXComplexf& b){ return FXComplexf(a.re*b.re-a.im*b.im,a.re*b.im+a.im*b.re); }
00168 inline FXComplexf operator/(const FXComplexf& a,const FXComplexf& b){ FXfloat m=b.modulus2(); return FXComplexf((a.re*b.re+a.im*b.im)/m,(a.im*b.re-a.re*b.im)/m); }
00169 
00170 
00171 inline FXComplexf operator+(const FXComplexf& a,FXfloat b){ return FXComplexf(a.re+b,a.im); }
00172 inline FXComplexf operator-(const FXComplexf& a,FXfloat b){ return FXComplexf(a.re-b,a.im); }
00173 inline FXComplexf operator*(const FXComplexf& a,FXfloat b){ return FXComplexf(a.re*b,a.im*b); }
00174 inline FXComplexf operator/(const FXComplexf& a,FXfloat b){ return FXComplexf(a.re/b,a.im/b); }
00175 
00176 
00177 inline FXComplexf operator+(FXfloat a,const FXComplexf& b){ return FXComplexf(a+b.re,b.im); }
00178 inline FXComplexf operator-(FXfloat a,const FXComplexf& b){ return FXComplexf(a-b.re,b.im); }
00179 inline FXComplexf operator*(FXfloat a,const FXComplexf& b){ return FXComplexf(a*b.re,a*b.im); }
00180 inline FXComplexf operator/(FXfloat a,const FXComplexf& b){ FXfloat m=b.modulus2(); return FXComplexf((a*b.re)/m,(-a*b.im)/m); }
00181 
00182 extern FXAPI FXStream& operator<<(FXStream& store,const FXComplexf& c);
00183 extern FXAPI FXStream& operator>>(FXStream& store,FXComplexf& c);
00184 
00185 }
00186 
00187 #endif

Copyright © 1997-2009 Jeroen van der Zijp