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

RealVector.h

00001 /****************************************************************************
00002  *                                                                          *
00003  *  Program: FDDlib                                                         *
00004  *  Version: 1.0                                                            *
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 _REAL_VECTOR_H_
00032 #define _REAL_VECTOR_H_
00033 
00034 #include <stdlib.h>
00035 #include <math.h>
00036 #include <assert.h>
00037 
00038 namespace FDDlib {
00039 
00047 template<class T>
00048 class RealVector
00049 {
00050  protected:
00052    int               len_;
00054    T*                valVec_;
00055 
00056  public:
00057  
00061   RealVector(int len);
00062 
00064   ~RealVector();
00065 
00067   void deallocate();
00068 
00070   RealVector();
00071 
00073   void init(int len);
00074 
00076   RealVector(const RealVector& rv);
00077 
00079   RealVector<T>& operator=(const RealVector<T>& rv);
00080 
00082   RealVector<T>& operator=(const T v);
00083 
00085   RealVector<T>& operator+=(const RealVector<T>& rv);
00086 
00088   RealVector<T>& operator-=(const RealVector<T>& rv);
00089 
00091   RealVector<T>& operator*=(const T v);
00092 
00094   RealVector<T> operator+(const RealVector<T>& rv) const;
00095 
00097   RealVector<T> operator-(const RealVector<T>& rv) const;
00098 
00100   RealVector<T> operator*(const T t) const;
00101 
00103   double dot(const RealVector<T>& rv) const;
00104 
00106   double norm() const;
00107 
00109   double norm1() const;
00110 
00112   int size() const;
00113 
00115   void set(int ind, T val);
00116   
00118   T val(int ind) const;
00119   
00123   T& operator()(int ind);
00124 
00126   T operator()(int ind) const;
00127 
00129   T max() const;
00130 
00132   T absmax() const;
00133 
00135   T min() const;
00136 };
00137 
00138 
00139 
00140 /* Added null pointer exception check 6/18/04
00141 */
00142 template<class T>
00143 RealVector<T>::RealVector(int len)
00144 {
00145   int i;
00146   len_ = len;
00147   valVec_ = new T[len_];
00148   assert (valVec_ != NULL);             // added 6/18/04
00149   for (i = 0; i < len_; i++)
00150   {
00151     set(i, (T) 0);
00152   }
00153 }
00154 
00155 template<class T>
00156 RealVector<T>::~RealVector()
00157 {
00158   if (valVec_ != (T*) NULL)
00159     delete[] valVec_;
00160 }
00161 
00162 template<class T>
00163 void RealVector<T>::deallocate()
00164 {
00165   if (valVec_ != (T*) NULL)
00166     delete[] valVec_;
00167   len_ = 0;
00168   valVec_ = (T*) NULL;
00169 }
00170 
00171 template<class T>
00172 RealVector<T>::RealVector()
00173 {
00174   len_ = 0;
00175   valVec_ = (T*) NULL;
00176 }
00177 
00178 /* Added check for null pointer exception 8/25/04
00179 */
00180 template<class T>
00181 void RealVector<T>::init(int len)
00182 {
00183   int i;
00184   len_ = len;
00185   if (valVec_ != (T*) NULL)
00186     delete[] valVec_;
00187 
00188   valVec_ = new T[len_];
00189   assert(valVec_ != NULL);      // added 8/25/04
00190 
00191   for (i = 0; i < len_; i++)
00192   {
00193     set(i, (T) 0);
00194   }
00195 }
00196 
00197 /* Added check for null pointer exception on allocation
00198    8/25/04
00199 */
00200 template<class T>
00201 RealVector<T>::RealVector(const RealVector& rv)
00202 {
00203   int i;
00204 
00205   len_ = rv.size();
00206   valVec_ = new T[len_];
00207   assert(valVec_ != NULL);      // added 8/25/04
00208 
00209   for (i = 0; i < len_; i++)
00210   {
00211     set(i, rv.val(i));
00212   }
00213 }
00214 
00215 /* Added check for null pointer exception on allocation
00216    1/21/04
00217 */
00218 template<class T>
00219 RealVector<T>& RealVector<T>::operator=(const RealVector<T>& rv)
00220 {
00221   int i;
00222 
00223   if (valVec_ != (T*) NULL)
00224     delete[] valVec_;
00225 
00226   len_ = rv.size();
00227   valVec_ = new T[len_];
00228   assert(valVec_ != NULL);
00229   for (i = 0; i < len_; i++)
00230   {
00231     set(i, rv.val(i));
00232   }
00233 
00234   return *this;
00235 }
00236 
00237 /* Added check for NULL array 1/21/04 
00238    Original code assumed that if valVec_ is NULL,
00239    then len_ would be zero, thus preventing a
00240    runtime error at set(i,v);  The extra check prevents
00241    problems in the event valVec_ is NULL but len_ is
00242    greater than zero.   JB 1/21/04
00243 */
00244 template<class T>
00245 RealVector<T>& RealVector<T>::operator=(const T v)
00246 {
00247   int i;
00248 
00249   if (valVec_ == (T*) NULL)     /* added 1/21/04 */
00250      return *this;
00251 
00252   for (i = 0; i < len_; i++)
00253   {
00254     set(i, v);
00255   }
00256 
00257   return *this;
00258 }
00259 
00260 template<class T>
00261 RealVector<T>& RealVector<T>::operator+=(const RealVector<T>& rv)
00262 {
00263   int i;
00264   T   v;
00265   const int s = size();
00266 
00267   for (i = 0; i < s; i++)
00268   {
00269     v = val(i) + rv.val(i);
00270     set(i, v);
00271   }
00272 
00273   return *this;
00274 }
00275 
00276 template<class T>
00277 RealVector<T>& RealVector<T>::operator-=(const RealVector<T>& rv)
00278 {
00279   int i;
00280   T   v;
00281   const int s = size();
00282 
00283   for (i = 0; i < s; i++)
00284   {
00285     v = val(i) - rv.val(i);
00286     set(i, v);
00287   }
00288 
00289   return *this;
00290 }
00291 
00292 template<class T>
00293 RealVector<T>& RealVector<T>::operator*=(const T v)
00294 {
00295   int i;
00296   const int s = size();
00297 
00298   for (i = 0; i < s; i++)
00299   {
00300     set(i, val(i)*v);
00301   }
00302 
00303   return *this;
00304 }
00305 
00306 template<class T>
00307 inline int RealVector<T>::size() const
00308 {
00309   return len_;
00310 }
00311 
00312 template<class T>
00313 inline void RealVector<T>::set(int ind, T val)
00314 {
00315   if (ind >= 0 && ind < len_)
00316      valVec_[ind] = val;
00317 }
00318 
00319 template<class T>
00320 inline T RealVector<T>::val(int ind) const
00321 {
00322   return valVec_[ind];
00323 }
00324 
00325 template<class T>
00326 inline T& RealVector<T>::operator()(int ind)
00327 {
00328   return valVec_[ind];
00329 }
00330 
00331 template<class T>
00332 inline T RealVector<T>::operator()(int ind) const
00333 {
00334   return valVec_[ind];
00335 }
00336 
00337 template<class T>
00338 inline T RealVector<T>::max() const
00339 {
00340   int i;
00341   T   val;
00342   const int s = size();
00343 
00344   if (len_ == 0)
00345     return (T) -1;
00346 
00347   val = valVec_[0];
00348   maxind = 0;
00349   for (i = 0; i < s; i++)
00350   {
00351     if (valVec_[i] > val)
00352     {
00353       val = valVec_[i];
00354       maxind = i;
00355     }
00356   }
00357 
00358   return val;
00359 }
00360 
00361 template<class T>
00362 inline T RealVector<T>::absmax() const
00363 {
00364   int i;
00365   T   val;
00366   const int s = size();
00367 
00368   if (len_ == 0)
00369     return (T) -1;
00370 
00371   val = (T) fabs((double) valVec_[0]);
00372   maxind = 0;
00373   for (i = 0; i < s; i++)
00374   {
00375     if (fabs((double) valVec_[i]) > (double) val)
00376     {
00377       val = (T) fabs((double) valVec_[i]);
00378       maxind = i;
00379     }
00380   }
00381 
00382   return val;
00383 }
00384 
00385 
00386 template<class T>
00387 inline T RealVector<T>::min() const
00388 {
00389   int i;
00390   T   val;
00391   const int s = size();
00392 
00393   if (len_ == 0)
00394     return (T) -1;
00395 
00396   val = valVec_[0];
00397   minind = 0;
00398   for (i = 0; i < s; i++)
00399   {
00400     if (valVec_[i] < val)
00401     {
00402       val = valVec_[i];
00403       minind = i;
00404     }
00405   }
00406 
00407   return val;
00408 }
00409 
00410 template<class T>
00411 RealVector<T> RealVector<T>::operator+(const RealVector<T>& rv) const
00412 {
00413   const int s = size();
00414   RealVector<T> ret(s);
00415   int           i;
00416   T             v;
00417 
00418   for (i = 0; i < s; i++)
00419   {
00420     v = val(i) + rv.val(i);
00421     ret.set(i, v);
00422   }
00423 
00424   return ret;
00425 }
00426 
00427 template<class T>
00428 RealVector<T> RealVector<T>::operator-(const RealVector<T>& rv) const
00429 {
00430   const int s = size();
00431   RealVector<T> ret(s);
00432   int           i;
00433   T             v;
00434 
00435   for (i = 0; i < s; i++)
00436   {
00437     v = val(i) - rv.val(i);
00438     ret.set(i, v);
00439   }
00440 
00441   return ret;
00442 }
00443 
00444 template<class T>
00445 RealVector<T> RealVector<T>::operator*(const T s) const
00446 {
00447   const int len = size();
00448   RealVector<T> ret(len);
00449   T             v;
00450   int           i;
00451 
00452   for (i = 0; i < len; i++)
00453   {
00454     v = val(i) * s;
00455     ret.set(i, v);
00456   }
00457 
00458   return ret;
00459 }
00460 
00461 template<class T>
00462 double RealVector<T>::dot(const RealVector<T>& rv) const
00463 {
00464   int    i;
00465   double ret;
00466   const int s = size();
00467 
00468   ret = 0;
00469   for (i = 0; i < s; i++)
00470   {
00471     ret += (double) (val(i) * rv.val(i));
00472   }
00473 
00474   return ret;
00475 
00476 }
00477 
00478 template<class T>
00479 double RealVector<T>::norm() const
00480 {
00481   int    i;
00482   double ret;
00483   const int s = size();
00484 
00485   ret = 0;
00486   for (i = 0; i < s; i++)
00487   {
00488     ret += (double) (val(i) * val(i));
00489   }
00490   ret = sqrt(ret);
00491 
00492   return ret;
00493 }
00494 
00495 template<class T>
00496 double RealVector<T>::norm1() const
00497 {
00498   int    i;
00499   double ret;
00500   const int s = size();
00501 
00502   ret = 0;
00503   for (i = 0; i < s; i++)
00504   {
00505     ret += fabs((double) val(i));
00506   }
00507 
00508   return ret;
00509 }
00510 
00512 template<class T>
00513 double dot(const RealVector<T>& a, const RealVector<T>& b)
00514 {
00515   return a.dot(b);
00516 }
00517 
00519 template<class T>
00520 double norm(const RealVector<T>& rv)
00521 {
00522   return rv.norm();
00523 }
00524 
00526 template<class T>
00527 double norm1(const RealVector<T>& rv)
00528 {
00529   return rv.norm1();
00530 }
00531 
00533 template<class T>
00534 RealVector<T> operator*(const T s, const RealVector<T>& rv)
00535 {
00536   return rv * s;
00537 }
00538 
00539 } // end namespace
00540 #endif

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