IM: im_complex.h Source File

IM - An Imaging Tool

im_complex.h

Go to the documentation of this file.
00001 /** \file
00002  * \brief Complex Data Type.
00003  *
00004  * See Copyright Notice in im_lib.h
00005  * $Id: im_complex.h,v 1.1 2005/04/02 22:07:00 scuri Exp $
00006  */
00007 
00008 #ifndef __IM_COMPLEX_H
00009 #define __IM_COMPLEX_H
00010 
00011 #include "im_math.h"
00012 
00013 /** \defgroup cpx Complex Numbers
00014  * \par
00015  * See \ref im_complex.h
00016  * \ingroup util
00017  */
00018 
00019 /** \brief Complex Float Data Type  
00020  *         
00021  * \par
00022  * Complex class using two floats, one for real part, one for the imaginary part.
00023  * \par
00024  * It is not a complete complex class, we just implement constructors inside the class.
00025  * All the other operators and functions are external to the class.
00026  * \ingroup cpx */
00027 class imcfloat
00028 {
00029 public:
00030   float real;  ///< Real part.
00031   float imag;  ///< Imaginary part.
00032 
00033   /// Default Constructor (0,0).
00034   imcfloat():real(0), imag(0) {}
00035 
00036   /// Constructor from (real, imag)
00037   imcfloat(const float& r, const float& i):real(r),imag(i) {}
00038 
00039   /// Constructor from (real)
00040   imcfloat(const float& r):real(r),imag(0) {}
00041 };
00042 
00043 /** \addtogroup cpx
00044  * Complex numbers operators.
00045  * @{ 
00046  */
00047 
00048 inline int operator <= (const imcfloat& C1, const imcfloat& C2)
00049 {
00050   return ((C1.real <= C2.real) && (C1.imag <= C2.imag));
00051 }
00052 
00053 inline int operator <= (const imcfloat& C, const float& F)
00054 {
00055   return ((F <= C.real) && (0 <= C.imag));
00056 }
00057 
00058 inline imcfloat operator + (const imcfloat& C1, const imcfloat& C2)
00059 {
00060   return imcfloat(C1.real + C2.real, C1.imag + C2.imag);
00061 }
00062 
00063 inline imcfloat operator += (const imcfloat& C1, const imcfloat& C2)
00064 {
00065   return imcfloat(C1.real + C2.real, C1.imag + C2.imag);
00066 }
00067 
00068 inline imcfloat operator - (const imcfloat& C1, const imcfloat& C2)
00069 {
00070   return imcfloat(C1.real - C2.real, C1.imag - C2.imag);
00071 }
00072 
00073 inline imcfloat operator * (const imcfloat& C1, const imcfloat& C2)
00074 {
00075   return imcfloat(C1.real * C2.real - C1.imag * C2.imag, 
00076                         C1.imag * C2.real + C1.real * C2.imag);
00077 }
00078 
00079 inline imcfloat operator / (const imcfloat& C1, const imcfloat& C2)
00080 {
00081   float den = C2.real * C2.real - C2.imag * C2.imag;
00082   return imcfloat((C1.real * C2.real + C1.imag * C2.imag) / den, 
00083                         (C1.imag * C2.real - C1.real * C2.imag) / den);
00084 }
00085 
00086 inline imcfloat operator / (const imcfloat& C, const float& R)
00087 {
00088   return imcfloat(C.real / R, C.imag / R);
00089 }
00090 
00091 inline imcfloat operator /= (const imcfloat& C, const float& R)
00092 {
00093   return imcfloat(C.real / R, C.imag / R);
00094 }
00095 
00096 inline imcfloat operator * (const imcfloat& C, const float& R)
00097 {
00098   return imcfloat(C.real * R, C.imag * R);
00099 }
00100 
00101 inline int operator == (const imcfloat& C1, const imcfloat& C2)
00102 {
00103   return ((C1.real == C2.real) && (C1.imag == C2.imag));
00104 }
00105 
00106 inline float cpxreal(const imcfloat& C)
00107 {
00108   return C.real;
00109 }
00110 
00111 inline float cpximag(const imcfloat& C)
00112 {
00113   return C.imag;
00114 }
00115 
00116 inline float cpxmag(const imcfloat& C)
00117 {
00118   return sqrtf(C.real*C.real + C.imag*C.imag);
00119 }
00120 
00121 inline float cpxphase(const imcfloat& C)
00122 {
00123   return atan2f(C.real, C.imag);
00124 }
00125 
00126 inline imcfloat cpxconj(const imcfloat& C)
00127 {
00128   return imcfloat(C.real, -C.imag);
00129 }
00130 
00131 inline imcfloat log(const imcfloat& C)
00132 {
00133   return imcfloat(logf(cpxmag(C)), atan2f(C.real, C.imag));
00134 }
00135 
00136 inline imcfloat exp(const imcfloat& C)
00137 {
00138   float mag = expf(C.real);
00139   return imcfloat(mag * cosf(C.imag), mag * sinf(C.imag));
00140 }
00141 
00142 inline imcfloat pow(const imcfloat& C1, const imcfloat& C2)
00143 {
00144   return exp(C1 * log(C2));
00145 }
00146 
00147 inline imcfloat sqrt(const imcfloat& C)
00148 {
00149   float mag = sqrtf(sqrtf(C.real*C.real + C.imag*C.imag));
00150   float phase = atan2f(C.real, C.imag) / 2;
00151   return imcfloat(mag * cosf(phase), mag * sinf(phase));
00152 }
00153 
00154 inline imcfloat cpxpolar(const float& mag, const float& phase)
00155 {
00156   return imcfloat(mag * cosf(phase), mag * sinf(phase));
00157 }
00158 
00159 /** @} */
00160 
00161 #endif