CVR-Lib last update 20 Sep 2009

cvrComplex.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 1998-2004
00003  * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany
00004  *
00005  *
00006  * This file is part of the Computer Vision and Robotics Library (CVR-Lib)
00007  *
00008  * The CVR-Lib is free software; you can redistribute it and/or
00009  * modify it under the terms of the BSD License.
00010  *
00011  * All rights reserved.
00012  *
00013  * Redistribution and use in source and binary forms, with or without
00014  * modification, are permitted provided that the following conditions are met:
00015  *
00016  * 1. Redistributions of source code must retain the above copyright notice,
00017  *    this list of conditions and the following disclaimer.
00018  *
00019  * 2. Redistributions in binary form must reproduce the above copyright notice,
00020  *    this list of conditions and the following disclaimer in the documentation
00021  *    and/or other materials provided with the distribution.
00022  *
00023  * 3. Neither the name of the authors nor the names of its contributors may be
00024  *    used to endorse or promote products derived from this software without
00025  *    specific prior written permission.
00026  *
00027  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00028  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00029  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00030  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00031  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00032  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00033  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00034  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00035  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00036  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00037  * POSSIBILITY OF SUCH DAMAGE.
00038  */
00039 
00040 
00041 
00042 /**
00043  * \file   cvrComplex.h
00044  *         CVR-Lib version of the complex numbers.  There are a few features
00045  *         besides the std::complex, but both types are compatible.
00046  * \author Pablo Alvarado
00047  * \date   06.04.2003
00048  *
00049  * $Id: cvrComplex.h,v 1.3 2007/11/03 23:55:10 alvarado Exp $
00050  */
00051 
00052 #ifndef _CVR_COMPLEX_H
00053 #define _CVR_COMPLEX_H
00054 
00055 #include "cvrMath.h"
00056 #include "cvrIoHandler.h"
00057 #include <complex>
00058 #include <iostream>
00059 
00060 namespace cvr {
00061   /**
00062    * Class to represent and manipulate complex numbers.
00063    *
00064    * The complex numbers contain a real and an imaginary part, which can
00065    * be accessed through the methods real() and imag().
00066    *
00067    * The interface of this class is similar to the one provided by the
00068    * Standard C++ library.  It has being included in the CVR-Lib because
00069    * different library versions do not provide the same functionality.
00070    *
00071    * Note that many of the provided mathematical operations make only sense if
00072    * the template type T used for the components of the complex number is
00073    * double or float.
00074    */
00075   template<typename T>
00076   class complex {
00077   public:
00078     typedef T value_type;
00079 
00080     /**
00081      * Return the real component of the complex number
00082      */
00083     T real() const;
00084 
00085     /**
00086      * Return the imaginary component of the complex number
00087      */
00088     T imag() const;
00089 
00090     /**
00091      * Default constructor.
00092      */
00093     inline complex(const T& = T(), const T& = T());
00094 
00095     /**
00096      * Copy constructor
00097      */
00098     template<class U>
00099     inline complex(const complex<U>& other);
00100 
00101     /**
00102      * Cast a std::complex to an cvr::complex of the same type
00103      */
00104     inline complex(const std::complex<T>& other);
00105 
00106     /**
00107      * Cast a cvr::complex into a std::complex of the same type
00108      */
00109     inline operator std::complex<T>() const;
00110 
00111     /**
00112      * Copy a real number into this complex one, making the imaginary part
00113      * zero.
00114      */
00115     inline complex<T>& operator=(const T& other);
00116 
00117     /**
00118      * Add a real number to this complex one.
00119      */
00120     inline complex<T>& operator+=(const T& other);
00121 
00122     /**
00123      * Subtract a real number to this complex one.
00124      */
00125     inline complex<T>& operator-=(const T& other);
00126 
00127     /**
00128      * Multiply this complex number with a real one.
00129      */
00130     inline complex<T>& operator*=(const T& other);
00131 
00132     /**
00133      * Divide this complex number by a real one.
00134      */
00135     inline complex<T>& operator/=(const T& other);
00136 
00137     /**
00138      * Copy another complex number into this one
00139      */
00140     template<typename U>
00141     inline complex<T>& operator=(const complex<U>& other);
00142 
00143     /**
00144      * Add another complex number
00145      */
00146     template<typename U>
00147     inline complex<T>& operator+=(const complex<U>& other);
00148 
00149     /**
00150      * subtract another complex number
00151      */
00152     template<typename U>
00153     inline complex<T>& operator-=(const complex<U>& other);
00154 
00155     /**
00156      * multiply with another complex number
00157      */
00158     template<typename U>
00159     inline complex<T>& operator*=(const complex<U>& other);
00160 
00161     /**
00162      * divide with another complex number
00163      */
00164     template<typename U>
00165     inline complex<T>& operator/=(const complex<U>& other);
00166 
00167     /**
00168      * Set real and imaginary components at once.
00169      */
00170     inline void set(const T& re, const T& im = T());
00171 
00172     /**
00173      * Set real part
00174      */
00175     inline void setReal(const T& re);
00176 
00177     /**
00178      * Set imaginary part
00179      */
00180     inline void setImag(const T& im);
00181 
00182     /**
00183      * Get the real and imaginary components at once.
00184      */
00185     inline void get(T& re, T& im) const;
00186 
00187   private:
00188     /**
00189      * real component
00190      */
00191     T realPart_;
00192 
00193     /**
00194      * imaginary component
00195      */
00196     T imagPart_;
00197   };
00198 
00199   // -------------------------------------------------------------------------
00200   // global operators
00201   // -------------------------------------------------------------------------
00202   /**
00203    * @name Operators for complex numbers
00204    */
00205   //@{
00206 
00207   /**
00208    * Add two complex numbers
00209    */
00210   template<typename T>
00211   inline complex<T> operator+(const complex<T>& a, const complex<T>& b);
00212 
00213   /**
00214    * Add a complex number with a real number
00215    */
00216   template<typename T>
00217   inline complex<T> operator+(const complex<T>& a, const T& b);
00218 
00219   /**
00220    * Add a real number with a complex number
00221    */
00222   template<typename T>
00223   inline complex<T> operator+(const T& a, const complex<T>& b);
00224 
00225   /**
00226    * Subtract two complex numbers
00227    */
00228   template<typename T>
00229   inline complex<T> operator-(const complex<T>& a, const complex<T>& b);
00230 
00231   /**
00232    * Subtract a real number from a complex one
00233    */
00234   template<typename T>
00235   inline complex<T> operator-(const complex<T>& a, const T& b);
00236 
00237   /**
00238    * Subtract a complex number from a real one
00239    */
00240   template<typename T>
00241   inline complex<T> operator-(const T& a, const complex<T>& b);
00242 
00243   /**
00244    * Multiply two complex numbers
00245    */
00246   template<typename T>
00247   inline complex<T> operator*(const complex<T>& a, const complex<T>& b);
00248 
00249   /**
00250    * Multiply a complex number with a real one
00251    */
00252   template<typename T>
00253   inline complex<T> operator*(const complex<T>& a, const T& b);
00254 
00255   /**
00256    * Multiply a real number with a complex one
00257    */
00258   template<typename T>
00259   inline complex<T> operator*(const T& a, const complex<T>& b);
00260 
00261   /**
00262    * Divide two complex numbers
00263    */
00264   template<typename T>
00265   inline complex<T> operator/(const complex<T>& a, const complex<T>& b);
00266 
00267   /**
00268    * Divide a complex number by a real one
00269    */
00270   template<typename T>
00271   inline complex<T> operator/(const complex<T>& a, const T& b);
00272 
00273   /**
00274    * Divide a real number by a complex one
00275    */
00276   template<typename T>
00277   inline complex<T> operator/(const T& a, const complex<T>& b);
00278 
00279   /**
00280    * Plus sign a complex number
00281    */
00282   template<typename T>
00283   inline complex<T> operator+(const complex<T>& a);
00284 
00285   /**
00286    * Minus sign a complex number
00287    */
00288   template<typename T>
00289   inline complex<T> operator-(const complex<T>& a);
00290 
00291   /**
00292    * Compare if two complex numbers are equal
00293    */
00294   template<typename T>
00295   inline bool operator==(const complex<T>& a, const complex<T>& b);
00296 
00297   /**
00298    * Compare if two complex numbers differ
00299    */
00300   template<typename T>
00301   inline bool operator==(const complex<T>& a, const T& b);
00302 
00303   /**
00304    * Compare if a real number and a complex number are the same
00305    */
00306   template<typename T>
00307   inline bool operator==(const T& a, const complex<T>& b);
00308 
00309   /**
00310    * Compare if two complex numbers differ
00311    */
00312   template<typename T>
00313   inline bool operator!=(const complex<T>& a, const complex<T>& b);
00314 
00315   /**
00316    * Compare if a complex number and a real one differ
00317    */
00318   template<typename T>
00319   inline bool operator!=(const complex<T>& a, const T& b);
00320 
00321   /**
00322    * Compare if a real number and a complex one differ
00323    */
00324   template<typename T>
00325   inline bool operator!=(const T& a, const complex<T>& b);
00326 
00327   /**
00328    * A complex number is called here "less than" another one if its real part
00329    * is smaller, or when both real parts are identical, if its imaginary part
00330    * is smaller
00331    */
00332   template<typename T>
00333   inline bool operator<(const complex<T>& a,const complex<T>& b);
00334 
00335   /**
00336    * A complex number is "less than" another one if its real part is
00337    * smaller, or when both real parts are identical, if its imaginary
00338    * part is smaller
00339    */
00340   template<typename T>
00341   inline bool operator<(const complex<T>& a,const T& b);
00342 
00343   /**
00344    * A complex number is "less than" another one if its real part is
00345    * smaller, or when both real parts are identical, if its imaginary
00346    * part is smaller
00347    */
00348   template<typename T>
00349   inline bool operator<(const T& a,const complex<T>& b);
00350 
00351   /**
00352    * A complex number is "greater than" another one if its real part is
00353    * greater, or when both real parts are identical, if its imaginary
00354    * part is greater
00355    */
00356   template<typename T>
00357   inline bool operator>(const complex<T>& a,const complex<T>& b);
00358 
00359   /**
00360    * A complex number is "greater than" another one if its real part is
00361    * greater, or when both real parts are identical, if its imaginary
00362    * part is greater
00363    */
00364   template<typename T>
00365   inline bool operator>(const complex<T>& a,const T& b);
00366 
00367   /**
00368    * A complex number is "greater than" another one if its real part is
00369    * greater, or when both real parts are identical, if its imaginary
00370    * part is greater
00371    */
00372   template<typename T>
00373   inline bool operator>(const T& a,const complex<T>& b);
00374 
00375   /**
00376    * Get the real part of a complex number
00377    */
00378   template<typename T>
00379   inline T real(const complex<T>& cn);
00380 
00381   /**
00382    * Get the imaginary part of a complex number
00383    */
00384   template<typename T>
00385   inline T imag(const complex<T>& cn);
00386 
00387   /**
00388    * Square of the magnitude of a number (|x|*|x|)
00389    *
00390    * This function is useful in generic programming for those cases where
00391    * complex numbers will occur in products of conjugate pairs.
00392    *
00393    * @ingroup gBasicMathFunc
00394    */
00395   template<typename T>
00396   inline T sqrAbs(const complex<T>& x);
00397 
00398   /**
00399    * Get the absolute value of a complex number
00400    */
00401   template<typename T>
00402   T abs(const complex<T>& cn);
00403 
00404   /**
00405    * Get the argument (angle or phase) of a complex number
00406    */
00407   template<typename T>
00408   inline T arg(const complex<T>& cn);
00409 
00410   /**
00411    * Return the square magnitude of the given complex number
00412    */
00413   template<typename T>
00414   inline T norm(const complex<T>& cn);
00415 
00416   /**
00417    * Convert the given polar values into a complex number
00418    *
00419    * @param radius magnitude of the number
00420    * @param angle  angle of the number
00421    *
00422    * @return Equivalent complex number to the polar one
00423    */
00424   template<typename T>
00425   inline complex<T> polar(const T& radius, const T& angle);
00426 
00427   /**
00428    * Return the conjugate of the complex number
00429    */
00430   template<typename T>
00431   inline complex<T> conj(const complex<T>& cn);
00432 
00433   /**
00434    * Cosine of a complex number
00435    */
00436   template<typename T>
00437   inline complex<T> cos(const complex<T>& cn);
00438 
00439   /**
00440    * Hyperbolic cosine of a complex number
00441    */
00442   template<typename T>
00443   inline complex<T> cosh(const complex<T>& cn);
00444 
00445   template<typename T>
00446   inline complex<T> acos(const complex<T>& cn);
00447 
00448   /**
00449    * Exponetial of a complex number
00450    */
00451   template<typename T>
00452   inline complex<T> exp(const complex<T>& cn);
00453 
00454   /**
00455    * Natural logarithm of a complex number (base \e e)
00456    */
00457   template<typename T>
00458   inline complex<T> log(const complex<T>& cn);
00459 
00460   /**
00461    * Logarithm base 10 of a complex number
00462    */
00463   template<typename T>
00464   inline complex<T> log10(const complex<T>& cn);
00465 
00466   /**
00467    * Sine of a complex number
00468    */
00469   template<typename T>
00470   inline complex<T> sin(const complex<T>& cn);
00471 
00472   /**
00473    * Hyperbolic sine of a complex number
00474    */
00475   template<typename T>
00476   inline complex<T> sinh(const complex<T>& cn);
00477 
00478   /**
00479    * Arc sine of a complex number
00480    */
00481   template<typename T>
00482   inline complex<T> asin(const complex<T>& cn);
00483 
00484   /**
00485    * Square root of a complex number.
00486    *
00487    * A there are always two solutions for sqrt(x+iy), this method provides
00488    * the first one consisting in sqrt(m*e^(i*a))=sqrt(m)*e^(i*a/2)
00489    */
00490   template<typename T>
00491   complex<T> sqrt(const complex<T>& cn);
00492 
00493   /**
00494    * Cube root of a complex number
00495    *
00496    * A there are always two solutions for sqrt(x+iy), this method provides
00497    * the first one consisting in sqrt(m*e^(i*a))=sqrt(m)*e^(i*a/2)
00498    */
00499   template<typename T>
00500   inline complex<T> cbrt(const complex<T>& cn);
00501 
00502   /**
00503    * Cube root of a complex number (alias for cbrt())
00504    */
00505   template<typename T>
00506   inline complex<T> cubeRoot(const complex<T>& cn);
00507 
00508   /**
00509    * Tangent of a complex number
00510    */
00511   template<typename T>
00512   inline complex<T> tan(const complex<T>& cn);
00513 
00514   /**
00515    * Hyperbolic tangent of a complex number
00516    */
00517   template<typename T>
00518   inline complex<T> tanh(const complex<T>& cn);
00519 
00520   /**
00521    * Hyperbolic arc tangent of a complex number
00522    */
00523   template<typename T>
00524   inline complex<T> atan(const complex<T>& cn);
00525 
00526   /**
00527    * Complex number to the power of \a b, with \b real
00528    */
00529   template<typename T>
00530   inline complex<T> pow(const complex<T>& a, const T& b);
00531 
00532   /**
00533    * Complex number to the power of \a b, with \b complex
00534    */
00535   template<typename T>
00536   inline complex<T> pow(const complex<T>& a, const complex<T>& b);
00537 
00538   /**
00539    * Real number to the power of \a b, with \b complex
00540    */
00541   template<typename T>
00542   inline complex<T> pow(const T& a, const complex<T>& b);
00543 
00544   /**
00545    * Read the vector from the given ioHandler.  The complete flag indicates
00546    * if the enclosing begin and end should be also be readed
00547    *
00548    * @ingroup gStorable
00549    */
00550   template <typename T>
00551   bool read(ioHandler& handler,complex<T>& p,const bool complete=true);
00552 
00553   /**
00554    * Write the vector in the given ioHandler.  The complete flag indicates
00555    * if the enclosing begin and end should be also be written or not
00556    *
00557    * @ingroup gStorable
00558    */
00559   template<typename T>
00560   bool write(ioHandler& handler,const complex<T>& p,const bool complete=true);
00561 
00562   /**
00563    * Alias for cvr::complex<float>
00564    */
00565   typedef complex<float> fcomplex;
00566 
00567   /**
00568    * Alias for cvr::complex<double>
00569    */
00570   typedef complex<double> dcomplex;
00571 
00572 } // end namespace cvr
00573 
00574 namespace std {
00575 
00576   template<typename T>
00577   istream& operator>>(istream& in, cvr::complex<T>& a);
00578 
00579   template<typename T>
00580   inline ostream& operator<<(ostream& out, const cvr::complex<T>& a);
00581 
00582 } // namespace std
00583 
00584 #include "cvrComplex_inline.h"
00585 #include "cvrComplex_template.h"
00586 
00587 #endif

Generated on Sun Sep 20 22:07:59 2009 for CVR-Lib by Doxygen 1.5.8