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