Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members  

Complex.h

00001 /****************************************************************************
00002  *                                                                          *
00003  *  Program: FDDlib                                                         *
00004  *  Version: 1.1                                                            *
00005  *                                                                          *
00006  *  Copyright (C) 2002 - 2004 by Eric Miller and Dana Brooks                *
00007  *  All rights reserved.                                                    *
00008  *                                                                          *
00009  *  This software is Version 1.1 of the fddlib tomography toolbox.          *
00010  *  It is not to be redistributed or used for any commercial purpose        *
00011  *  without the prior written consent of the authors and Northeastern       *
00012  *  University.                                                             *
00013  *                                                                          *
00014  *  This software is provided as is, and any express or implied warranties, *
00015  *  including but not limited to the implied warranty of merchantability    *
00016  *  and the implied warranty of fitness for a particular purpose, are dis-  *
00017  *  claimed.  In no event shall the authors or Northeastern University be   *
00018  *  liable for any direct, indirect, incidental, special, exemplary, or     *
00019  *  consequential damages (including but not limited to procurement of      * 
00020  *  substitute goods or services; loss of use, data, or profits; or busi-   *
00021  *  ness interruption) however caused and on any theory of liability,       *
00022  *  whether in contract, strict liability, or tort (including negligence or *
00023  *  otherwise) arising in any way from the use of this software, even if    *
00024  *  advised of the possibility of such damage.                              *
00025  *                                                                          *
00026  *  Portions of this code benefit from ideas from Kyle Guilbert, Greg       *
00027  *  Boverman, Derek Uluski, David Kaeli, and Jennifer Black                 *
00028  *                                                                          *
00029  ****************************************************************************/
00030 
00031 #ifndef _COMPLEX_H_
00032 #define _COMPLEX_H_
00033 
00034 #include <math.h>
00035 #include <stdlib.h>
00036 
00037 namespace FDDlib {
00038 
00044 template<class T>
00045 class Complex
00046 {
00047 protected:
00049   T realPart_;
00051   T imagPart_;
00052 
00053 public:
00055   Complex();
00056 
00058   ~Complex() { }
00059 
00064   Complex(const T realPart, const T imagPart);
00065 
00070   Complex(const T t);
00071 
00073   Complex(const Complex<T>& cn);
00074 
00078   Complex<T>& operator=(const Complex<T>& c);
00079 
00081   T getReal() const;
00082 
00084   T getImag() const;
00085 
00087   void setReal(T r);
00088 
00090   void setImag(T i);
00091 
00095   Complex<T>& operator+=(const Complex<T>& c);
00096 
00100   Complex<T>& operator-=(const Complex<T>& c);
00101 
00105   Complex<T>& operator*=(const Complex<T>& c);
00106 
00110   Complex<T>& operator*=(const T s);
00111 
00115   Complex<T>& operator/=(const Complex<T>& c);
00116 
00120   Complex<T>& operator/=(const T s);
00121 
00125   Complex<T> operator+(const Complex<T>& c) const;
00126 
00130   Complex<T> operator-(const Complex<T>& c) const;
00131 
00135   Complex<T> operator*(const Complex<T>& c) const;
00136 
00140   Complex<T> operator*(const T s) const;
00141 
00145   Complex<T> operator/(const Complex<T>& c) const;
00146 
00151   Complex<T> operator/(const T s) const;
00152 
00154   double magnitude() const;
00155 
00157   double squareMagnitude() const;
00158 
00160   Complex<T> conjugate() const;
00161 
00162 };
00163 
00164 template<class T>
00165 Complex<T>::Complex()
00166 {
00167   realPart_ = 0;
00168   imagPart_ = 0;
00169 }
00170 
00171 template<class T>
00172 Complex<T>::Complex(const T realPart, const T imagPart)
00173 {
00174   realPart_ = realPart;
00175   imagPart_ = imagPart;
00176 }
00177 
00178 template<class T>
00179 Complex<T>::Complex(const T t)
00180 {
00181   realPart_ = t;
00182   imagPart_ = 0;
00183 }
00184 
00185 template<class T>
00186 Complex<T>::Complex(const Complex<T>& cn)
00187 {
00188   realPart_ = cn.getReal();
00189   imagPart_ = cn.getImag();
00190 }
00191 
00192 template<class T>
00193 Complex<T>& Complex<T>::operator=(const Complex<T>& c)
00194 {
00195   realPart_ = c.getReal();
00196   imagPart_ = c.getImag();
00197 
00198   return *this;
00199 }
00200 
00201 template<class T>
00202 T Complex<T>::getReal() const
00203 {
00204   return realPart_;
00205 }
00206 
00207 template<class T>
00208 T Complex<T>::getImag() const
00209 {
00210   return imagPart_;
00211 }
00212 
00213 template<class T>
00214 void Complex<T>::setReal(T r)
00215 {
00216   realPart_ = r;
00217 }
00218 
00219 template<class T>
00220 void Complex<T>::setImag(T i)
00221 {
00222   imagPart_ = i;
00223 }
00224 
00225 template<class T>
00226 Complex<T>& Complex<T>::operator+=(const Complex<T>& c)
00227 {
00228   realPart_ += c.getReal();
00229   imagPart_ += c.getImag();
00230 
00231   return *this;
00232 }
00233 
00234 template<class T>
00235 Complex<T>& Complex<T>::operator-=(const Complex<T>& c)
00236 {
00237   realpart_ -= c.getReal();
00238   imagpart_ -= c.getImag();
00239 
00240   return *this;
00241 }
00242 
00243 template<class T>
00244 Complex<T>& Complex<T>::operator*=(const Complex<T>& c)
00245 {
00246   realPart_ = getReal() * c.getReal() - getImag() * c.getImag();
00247   imagPart_ = getReal() * c.getImag() + getImag() * c.getReal();
00248 
00249   return *this;
00250 }
00251 
00252 template<class T>
00253 Complex<T>& Complex<T>::operator*=(const T s)
00254 {
00255   realPart_ *= s;
00256   imagPart_ *= s;
00257 
00258   return *this;
00259 }
00260 
00261 template<class T>
00262 Complex<T>& Complex<T>::operator/=(const Complex<T>& c)
00263 {
00264   T mag;
00265 
00266   mag = (T) ((double) (getReal()*c.getReal() + getImag()*c.getImag()));
00267 
00268   if (mag == (T) 0)
00269     return NULL;
00270 
00271   realPart_ = getReal() * c.getReal() + getImag() * c.getImag();
00272   imagPart_ = getImag() * c.getReal() - getReal() * c.getImag();
00273 
00274   realPart_ /= mag;
00275   imagPart_ /= mag;
00276 
00277   return *this;
00278 }
00279 
00280 template<class T>
00281 Complex<T>& Complex<T>::operator/=(const T a)
00282 {
00283   realPart_ /= a;
00284   imagPart_ /= a;
00285 
00286   return *this;
00287 }
00288 
00289 template<class T>
00290 Complex<T> Complex<T>::operator+(const Complex<T>& c) const
00291 {
00292   Complex<T> ret;
00293   ret.setReal(getReal() + c.getReal());
00294   ret.setImag(getImag() + c.getImag());
00295   return ret;
00296 }
00297 
00298 template<class T>
00299 Complex<T> Complex<T>::operator-(const Complex<T>& c) const
00300 {
00301   Complex<T> ret;
00302   ret.setReal(getReal() - c.getReal());
00303   ret.setImag(getImag() - c.getImag());
00304   return ret;
00305 }
00306 
00307 template<class T>
00308 Complex<T> Complex<T>::operator*(const Complex<T>& c) const
00309 {
00310   Complex<T> ret;
00311   ret.setReal(getReal() * c.getReal() - getImag() * c.getImag());
00312   ret.setImag(getReal() * c.getImag() + getImag() * c.getReal());
00313   return ret;
00314 }
00315 
00316 template<class T>
00317 Complex<T> Complex<T>::operator*(const T s) const
00318 {
00319   Complex<T> ret;
00320   ret.setReal(getReal() * s);
00321   ret.setImag(getImag() * s);
00322   return ret;
00323 }
00324 
00325 template<class T>
00326 Complex<T> Complex<T>::operator/(const Complex<T>& c) const
00327 {
00328   T mag, rp, ip;
00329   Complex<T> ret;
00330 
00331   mag = (T) ((double) (getReal()*c.getReal() + getImag()*c.getImag()));
00332 
00333   if (mag == (T) 0)
00334     return NULL;
00335 
00336   rp = getReal() * c.getReal() + getImag() * c.getImag();
00337   ip = getImag() * c.getReal() - getReal() * c.getImag();
00338 
00339   rp /= mag;
00340   ip /= mag;
00341 
00342   ret.setReal(rp);
00343   ret.setImag(ip);
00344 
00345   return ret;
00346 }
00347 
00348 template<class T>
00349 Complex<T> Complex<T>::operator/(const T s) const
00350 {
00351   Complex<T> ret;
00352   ret.setReal(getReal() / s);
00353   ret.setImag(getImag() / s);
00354   return ret;
00355 }
00356 
00357 template<class T>
00358 double Complex<T>::magnitude() const
00359 {
00360   return (sqrt((double) (realPart_*realPart_ + imagPart_*imagPart_)));
00361 }
00362 
00363 template<class T>
00364 double Complex<T>::squareMagnitude() const
00365 {
00366   return ((double) (realPart_*realPart_ + imagPart_*imagPart_));
00367 }
00368 
00369 template<class T>
00370 Complex<T> Complex<T>::conjugate() const
00371 {
00372   return Complex<T>(realPart_, -1*imagPart_);
00373 }
00374 
00375 // end namespace
00376 }
00377 #endif

Generated on Mon Aug 30 15:41:05 2004 for FDDLib by doxygen1.2.18