xcomplex.h

Go to the documentation of this file.
00001 /*
00002  *  This file is part of libcxxsupport.
00003  *
00004  *  libcxxsupport is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  libcxxsupport is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with libcxxsupport; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00017  */
00018 
00019 /*
00020  *  libcxxsupport is being developed at the Max-Planck-Institut fuer Astrophysik
00021  *  and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
00022  *  (DLR).
00023  */
00024 
00025 /*! \file xcomplex.h
00026  *  Class for representing complex numbers, strongly inspired by C++'s
00027  *  std::complex
00028  *
00029  *  Copyright (C) 2003-2011 Max-Planck-Society
00030  *  \author Martin Reinecke
00031  */
00032 
00033 #ifndef PLANCK_XCOMPLEX_H
00034 #define PLANCK_XCOMPLEX_H
00035 
00036 #include <iostream>
00037 #include <complex>
00038 
00039 /*! \defgroup complexgroup Complex number support */
00040 /*! \{ */
00041 
00042 /*! A class for representing complex numbers.
00043 
00044     This template is intended as an (under-encapsulated) replacement for
00045     the (over-encapsulated) std::complex<>. The goal is to include the
00046     whole functionality of std::complex<>, with some additional methods
00047     that allow higher performance.
00048 
00049     The (known and intentional) differences between xcomplex<> and
00050     std::complex<> are:
00051      - the default constructor of xcomplex<> does nothing, in contrast to
00052        std::complex<>, which initialises its members to zero.
00053      - xcomplex<> implements the methods real() and imag() according
00054        to defect report DR387
00055 */
00056 template<typename T> class xcomplex
00057   {
00058   public:
00059     T re, /*!< real part */
00060       im; /*!< imaginary part */
00061 
00062     /*! Default constructor. \a re and \a im are not initialised. */
00063     xcomplex () {}
00064     /*! Creates the complex number (\a re_, \a im_). */
00065     xcomplex (const T &re_, const T &im_)
00066       : re(re_), im(im_) {}
00067     /*! Creates the complex number (\a re_, 0). */
00068     xcomplex (const T &re_)
00069       : re(re_), im(0) {}
00070     /*! Creates an xcomplex from a std::complex of identical precision. */
00071     xcomplex (const std::complex<T> &orig)
00072       : re(orig.real()), im(orig.imag()) {}
00073     /*! Creates a complex number as a copy of \a orig. */
00074     template<typename U> explicit xcomplex (const xcomplex<U> &orig)
00075       : re(T(orig.re)), im(T(orig.im)) {}
00076 
00077     /*! Conversion operator to std::complex<T> */
00078     operator std::complex<T> () const
00079       { return std::complex<T>(re,im); }
00080 
00081     /*! Returns the real part as lvalue. */
00082     T &real() { return re; }
00083     /*! Returns the real part. */
00084     const T &real() const { return re; }
00085     /*! Returns the imaginary part as lvalue. */
00086     T &imag() { return im; }
00087     /*! Returns the imaginary part. */
00088     const T &imag() const { return im; }
00089 
00090     /*! Sets the number to (\a re_, \a im_). */
00091     void Set (const T &re_, const T &im_)
00092       { re = re_; im = im_; }
00093 
00094     /*! Sets the number to \a orig. */
00095     xcomplex &operator= (const xcomplex &orig)
00096       { re=orig.re; im=orig.im; return *this; }
00097     /*! Sets the number to \a orig. */
00098     xcomplex &operator= (const std::complex<T> &orig)
00099       { re=orig.real(); im=orig.imag(); return *this; }
00100     /*! Sets the number to (\a orig, 0). */
00101     xcomplex &operator= (const T &orig)
00102       { re=orig; im=0; return *this; }
00103     /*! Adds \a b to \a *this. */
00104     xcomplex &operator+= (const xcomplex &b)
00105       { re+=b.re; im+=b.im; return *this; }
00106     /*! Subtracts \a b from \a *this. */
00107     xcomplex &operator-= (const xcomplex &b)
00108       { re-=b.re; im-=b.im; return *this; }
00109     /*! Multiplies \a *this by \a b. */
00110     xcomplex &operator*= (const xcomplex &b)
00111       {
00112       T tr=re*b.re-im*b.im, ti=re*b.im+im*b.re;
00113       re=tr; im=ti;
00114       return *this;
00115       }
00116     /*! Divides \a *this by \a b. */
00117     xcomplex &operator/= (const xcomplex &b)
00118       {
00119       std::complex<T> tmp=*this;
00120       tmp /= std::complex<T>(b);
00121       *this=tmp;
00122       return *this;
00123       }
00124     /*! Multiplies \a *this by \a fact. */
00125     xcomplex &operator*= (const T &fact)
00126       { re*=fact; im*=fact; return *this; }
00127     /*! Divides \a *this by \a div. */
00128     xcomplex &operator/= (const T &div)
00129       { re/=div; im/=div; return *this; }
00130     /*! Returns \a *this * \a fact. */
00131     xcomplex operator* (const T &fact) const
00132       { return xcomplex (re*fact,im*fact); }
00133     /*! Returns \a *this * \a b. */
00134     xcomplex operator* (const xcomplex &b) const
00135       { return xcomplex (re*b.re-im*b.im, re*b.im+im*b.re); }
00136     /*! Returns \a *this / \a b. */
00137     xcomplex operator/ (const xcomplex &b) const
00138       { return xcomplex(std::complex<T>(*this)/std::complex<T>(b)); }
00139     /*! Returns \a *this / \a div. */
00140     xcomplex operator/ (const T &div) const
00141       { return xcomplex (re/div,im/div); }
00142     /*! Returns \a *this + \a b. */
00143     xcomplex operator+ (const xcomplex &b) const
00144       { return xcomplex (re+b.re, im+b.im); }
00145     /*! Returns \a *this - \a b. */
00146     xcomplex operator- (const xcomplex &b) const
00147       { return xcomplex (re-b.re, im-b.im); }
00148     /*! Returns \a -(*this) */
00149     xcomplex operator- () const
00150       { return xcomplex (-re,-im); }
00151 
00152     /*! Flips the signs of both components. */
00153     void Negate()
00154       { re=-re; im=-im; }
00155     /*! Flips the signs of the imaginary component. */
00156     void Conjugate()
00157       { im=-im; }
00158     /*! Multiplies the number by exp(i*\a angle) */
00159     void Rotate(T angle)
00160       {
00161       T ca=cos(angle), sa=sin(angle);
00162       T tmp=re;
00163       re=tmp*ca-im*sa; im=tmp*sa+im*ca;
00164       }
00165     /*! Returns the complex conjugate of \a *this. */
00166     xcomplex conj() const
00167       { return xcomplex (re,-im); }
00168 
00169     /*! Returns \a *this*i. */
00170     xcomplex times_i() const
00171       { return xcomplex (-im,re); }
00172 
00173     /*! Returns the norm of \a *this. */
00174     T norm() const
00175       { return re*re + im*im; }
00176   };
00177 
00178 /*! Returns the complex conjugate of \a num.
00179     \relates xcomplex */
00180 template <typename T> inline xcomplex<T> conj (const xcomplex<T> &num)
00181   { return xcomplex<T> (num.re, -num.im); }
00182 /*! Returns the norm of \a num.
00183     \relates xcomplex */
00184 template <typename T> inline T norm (const xcomplex<T> &num)
00185   { return num.re*num.re + num.im*num.im; }
00186 /*! Returns the absolute value of \a num.
00187     \relates xcomplex */
00188 template <typename T> inline T abs (const xcomplex<T> &num)
00189   {
00190   using namespace std;
00191   return abs(complex<T>(num));
00192   }
00193 /*! Returns the exponential of \a num.
00194     \relates xcomplex */
00195 template <typename T> inline xcomplex<T> exp (const xcomplex<T> &num)
00196   {
00197   using namespace std;
00198   return xcomplex<T>(exp(complex<T>(num)));
00199   }
00200 /*! Returns \a f1*f2.
00201     \relates xcomplex */
00202 template <typename T> inline xcomplex<T> operator*
00203   (const T &f1, const xcomplex<T> &f2)
00204   { return xcomplex<T> (f1*f2.re, f1*f2.im); }
00205 /*! Returns \a f1/f2.
00206     \relates xcomplex */
00207 template <typename T> inline xcomplex<T> operator/
00208   (const T &f1, const xcomplex<T> &f2)
00209   { return xcomplex<T>(f1)/f2; }
00210 /*! Writes \a val to \a os.
00211     \relates xcomplex */
00212 template<typename T>
00213   inline std::ostream &operator<< (std::ostream &os, const xcomplex<T> &val)
00214   { os << "(" << val.re << "," << val.im << ")"; return os; }
00215 
00216 typedef xcomplex<double> dcomplex;
00217 typedef xcomplex<float>  fcomplex;
00218 
00219 /*! \} */
00220 
00221 #endif

Generated on Wed Apr 24 11:31:17 2013 for LevelS C++ support library