NASA - Jet Propulsion Laboratory
    + View the NASA Portal
Search JPL
Jet Propulsion Laboratory Home Earth Solar System Stars & Galaxies Technology
Introduction Background Software Links

xcomplex.h

Go to the documentation of this file.
00001 /*
00002  *  This file is part of Healpix_cxx.
00003  *
00004  *  Healpix_cxx 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  *  Healpix_cxx 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 Healpix_cxx; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00017  *
00018  *  For more information about HEALPix, see http://healpix.jpl.nasa.gov
00019  */
00020 
00021 /*
00022  *  Healpix_cxx is being developed at the Max-Planck-Institut fuer Astrophysik
00023  *  and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
00024  *  (DLR).
00025  */
00026 
00027 /*! \file xcomplex.h
00028  *  Class for representing complex numbers, strongly inspired by C++'s
00029  *  std::complex
00030  *
00031  *  Copyright (C) 2003 Max-Planck-Society
00032  *  \author Martin Reinecke
00033  */
00034 
00035 #ifndef PLANCK_XCOMPLEX_H
00036 #define PLANCK_XCOMPLEX_H
00037 
00038 #include <iostream>
00039 
00040 /*! \defgroup complexgroup Complex number support */
00041 /*! \{ */
00042 
00043 /*! A class for representing complex numbers.
00044 
00045     This template is intended as an (under-encapsulated) replacement for
00046     the (over-encapsulated) std::complex<>. The goal is to include the
00047     whole functionality of std::complex<>, with some additional methods
00048     that allow higher performance.
00049 
00050     The (known and intentional) differences between xcomplex<> and
00051     std::complex<> are:
00052      - the default constructor of xcomplex<> does nothing, in contrast to
00053        std::complex<>, which initialises its members to zero.
00054      - xcomplex<> implements the methods real() and imag() according
00055        to defect report DR387
00056 */
00057 template<typename T> class xcomplex
00058   {
00059   public:
00060     T re, /*!< real part */
00061       im; /*!< imaginary part */
00062 
00063     /*! Default constructor. \a re and \a im are not initialised. */
00064     xcomplex () {}
00065     /*! Creates the complex number (\a re_, \a im_). */
00066     xcomplex (const T &re_, const T &im_)
00067       : re(re_), im(im_) {}
00068     /*! Creates the complex number (\a re_, 0). */
00069     xcomplex (const T &re_)
00070       : re(re_), im(0) {}
00071     /*! Creates a complex number as a copy of \a orig. */
00072     template<typename U> xcomplex (const xcomplex<U> &orig)
00073       : re(orig.re), im(orig.im) {}
00074 
00075     /*! Returns the real part as lvalue. */
00076     T &real() { return re; }
00077     /*! Returns the real part. */
00078     const T &real() const { return re; }
00079     /*! Returns the imaginary part as lvalue. */
00080     T &imag() { return im; }
00081     /*! Returns the imaginary part. */
00082     const T &imag() const { return im; }
00083 
00084     /*! Sets the number to (\a re_, \a im_). */
00085     void Set (const T &re_, const T &im_)
00086       { re = re_; im = im_; }
00087 
00088     /*! Sets the number to \a orig. */
00089     template<typename U> xcomplex &operator= (const xcomplex<U> &orig)
00090       { re=orig.re; im=orig.im; return *this; }
00091     /*! Sets the number to (\a orig, 0). */
00092     xcomplex &operator= (const T &orig)
00093       { re=orig; im=0; return *this; }
00094     /*! Adds \a orig to \a *this. */
00095     xcomplex &operator+= (const xcomplex &orig)
00096       { re+=orig.re; im+=orig.im; return *this; }
00097     /*! Subtracts \a orig from \a *this. */
00098     xcomplex &operator-= (const xcomplex &orig)
00099       { re-=orig.re; im-=orig.im; return *this; }
00100     /*! Multiplies \a *this by \a orig. */
00101     xcomplex &operator*= (const xcomplex &orig)
00102       {
00103       T tmp=re;
00104       re=re*orig.re-im*orig.im; im=tmp*orig.im+im*orig.re;
00105       return *this;
00106       }
00107     /*! Multiplies \a *this by \a fact. */
00108     xcomplex &operator*= (const T &fact)
00109       { re*=fact; im*=fact; return *this; }
00110     /*! Returns \a *this * \a fact. */
00111     xcomplex operator* (const T &fact) const
00112       { return xcomplex (re*fact,im*fact); }
00113     /*! Returns \a *this * \a b. */
00114     xcomplex operator* (const xcomplex &b) const
00115       { return xcomplex (re*b.re-im*b.im, re*b.im+im*b.re); }
00116     /*! Returns \a *this + \a b. */
00117     xcomplex operator+ (const xcomplex &b) const
00118       { return xcomplex (re+b.re, im+b.im); }
00119     /*! Returns \a *this - \a b. */
00120     xcomplex operator- (const xcomplex &b) const
00121       { return xcomplex (re-b.re, im-b.im); }
00122     /*! Returns \a -(*this) */
00123     xcomplex operator- () const
00124       { return xcomplex (-re,-im); }
00125 
00126     /*! Flips the signs of both components. */
00127     void Negate()
00128       { re=-re; im=-im; }
00129     /*! Flips the signs of the imaginary component. */
00130     void Conjugate()
00131       { im=-im; }
00132     /*! Returns the complex conjugate of \a *this. */
00133     xcomplex conj() const
00134       { return xcomplex (re,-im); }
00135 
00136     /*! Returns the norm of \a *this. */
00137     T norm() const
00138       { return re*re + im*im; }
00139   };
00140 
00141 /*! Returns the complex conjugate of \a num.
00142     \relates xcomplex */
00143 template <typename T> inline xcomplex<T> conj (const xcomplex<T> &num)
00144   { return xcomplex<T> (num.re, -num.im); }
00145 /*! Returns the norm of \a num.
00146     \relates xcomplex */
00147 template <typename T> inline T norm (const xcomplex<T> &num)
00148   { return num.re*num.re + num.im*num.im; }
00149 /*! Returns \a f1*f2.
00150     \relates xcomplex */
00151 template <typename T> inline xcomplex<T> operator*
00152   (const T &f1, const xcomplex<T> &f2)
00153   { return xcomplex<T> (f1*f2.re, f1*f2.im); }
00154 /*! Writes \a val to \a os.
00155     \relates xcomplex */
00156 template<typename T>
00157   inline std::ostream &operator<< (std::ostream &os, const xcomplex<T> &val)
00158   { os << "(" << val.re << "," << val.im << ")"; return os; }
00159 
00160 /*! \} */
00161 
00162 #endif

Generated on Fri Jun 18 16:12:29 2010 for LevelS C++ support library
Privacy / Copyright
FIRST GOV Contact: NASA Home Page Site Manager:
Webmaster:

CL 03-2650