Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

vector.h

Go to the documentation of this file.
00001 // -*-c++-*-
00002 #ifndef IG_VECTOR_H
00003 #define IG_VECTOR_H
00004 
00005 // $Id: vector.h,v 1.30 2005/07/02 15:58:26 hkuiper Exp $
00006 
00007 // CwMtx matrix and vector math library
00008 // Copyright (C) 1999-2001  Harry Kuiper
00009 // Copyright (C) 2000  Will DeVore (template conversion)
00010 
00011 // This library is free software; you can redistribute it and/or
00012 // modify it under the terms of the GNU Lesser General Public
00013 // License as published by the Free Software Foundation; either
00014 // version 2 of the License, or (at your option) any later version.
00015 
00016 // This library is distributed in the hope that it will be useful,
00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019 // Lesser General Public License for more details.
00020 
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License along with this library; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00024 // USA
00025 
00026 #include <cmath>
00027 
00028 #ifndef IG_MATRIX_H
00029 #include "matrix.h"
00030 #endif
00031 
00032 namespace CwMtx
00033 {
00034 
00035   template < class T = double >
00036   class CWTVector : public CWTMatrix<T>
00037   {
00038   public:
00039     CWTVector(): CWTMatrix<T>() {};
00040     CWTVector(unsigned crowInit): CWTMatrix<T>(crowInit, 1) {};
00041     CWTVector(const CWTMatrix<T>& mat): CWTMatrix<T>(mat) {};
00042     CWTVector(const CWTVector& vec): CWTMatrix<T>(vec) {};
00043     // mapping into a matrix
00044     CWTVector(const CWTMatrix<T>& , unsigned, unsigned, unsigned);
00045     // mapping into a vector
00046     CWTVector(const CWTVector& , unsigned, unsigned);
00047 
00048     ~CWTVector() {};
00049 
00050     void mapInto(const CWTMatrix<T> &, unsigned, unsigned, unsigned);
00051     void mapInto(const CWTVector &, unsigned, unsigned);
00052     void dimension(unsigned crowInit)
00053     {
00054       CWTMatrix<T>::dimension(crowInit, 1);
00055     }
00056 
00057     T & operator [](unsigned irow)
00058     {
00059       return this->CWTMatrix<T>::operator[](irow)[0];
00060     }
00061 
00062     const T & operator [](unsigned irow) const
00063     {
00064       return this->CWTMatrix<T>::operator[](irow)[0];
00065     }
00066 
00067     CWTVector operator +(const CWTVector &) const;
00068     CWTVector operator -(const CWTVector &) const;
00069     CWTVector operator -() const;
00070     CWTVector operator *(const T &) const;
00071     // CWTVector*CWTVector, inner product
00072     T operator *(const CWTVector &) const;
00073     CWTVector operator /(const T &value) const
00074     {
00075       // NOTE: This used to work with g++ <= 3.2.
00076       // return (*this)*static_cast<const T &>(CWTUnity<T>()/value);
00077 
00078       T tTmp = CWTUnity<T>();
00079       tTmp /= value;
00080       return (*this)*tTmp;
00081     }
00082 
00083     // not inherited
00084     CWTVector & operator =(const CWTVector &vec);
00085     CWTVector & operator +=(const CWTVector &vec);
00086     CWTVector & operator -=(const CWTVector &vec);
00087     CWTVector & operator *=(const T &value);
00088     CWTVector & operator /=(const T &value);
00089 
00090     // CWTVector norm
00091     T operator !() const { return (*this).norm(); };
00092 
00093     void storeAtRow(unsigned, const CWTVector &);
00094     // returns vector norm (length)
00095     T norm() const;
00096     // returns a unit vector with same direction as this
00097     CWTVector unit() const { return (*this)/norm(); }
00098 
00099     // make this a unit vector
00100     void makeUnit() { (*this) /= norm(); }
00101   };
00102 
00103   template <class T, unsigned crow>
00104   class CWTVec: public T
00105   {
00106   public:
00107     CWTVec(): T(crow) {}
00108 
00109     T & operator =(const T &mtx) { return T::operator=(mtx); }
00110   };
00111 
00112   // NOTE: There exists no unity vector for a general vector!
00113 
00114   // Zero matrix.
00115   template <class T, unsigned crow>
00116   class CWTZero< CWTVec<CWTVector<T>, crow> >:
00117     public CWTVec<CWTVector<T>, crow>
00118   {
00119   public:
00120     CWTZero() { fill(CWTZero<T>()); }
00121   };
00122 
00123   //
00124   // Constructors
00125   //
00126 
00127   // mapping into a vector
00128   template < class T >
00129   inline CWTVector<T>::CWTVector(const CWTVector<T> &vec,
00130                                  unsigned irowStart,
00131                                  unsigned irowEnd)
00132     : CWTMatrix<T>(vec, irowStart, 0, irowEnd, 0)
00133   {
00134   }
00135 
00136   // mapping into a matrix
00137   template < class T >
00138   inline CWTVector<T>::CWTVector(const CWTMatrix<T> &mat,
00139                                  unsigned irowStart,
00140                                  unsigned icolStart,
00141                                  unsigned irowEnd)
00142     :
00143     CWTMatrix<T>(mat, irowStart, icolStart, irowEnd, icolStart)
00144   {
00145   }
00146 
00147   //
00148   // User Methods
00149   //
00150 
00151   template < class T >
00152   inline void CWTVector<T>::mapInto(const CWTMatrix<T> &mat,
00153                                     unsigned irowStart,
00154                                     unsigned icol,
00155                                     unsigned irowEnd)
00156   {
00157     CWTMatrix<T>::mapInto(mat, irowStart, icol, irowEnd, icol);
00158   }
00159 
00160   template < class T >
00161   inline void CWTVector<T>::mapInto(const CWTVector &vec,
00162                                     unsigned irowStart,
00163                                     unsigned irowEnd)
00164   {
00165     CWTMatrix<T>::mapInto(vec, irowStart, 0, irowEnd, 0);
00166   }
00167 
00168   // not inherited
00169   template < class T >
00170   inline CWTVector<T> & CWTVector<T>::operator =(const CWTVector<T> &vec)
00171   {
00172     return static_cast<CWTVector &>(CWTMatrix<T>::operator=(vec));
00173   }
00174 
00175   template < class T >
00176   inline CWTVector<T> & CWTVector<T>::operator +=(const CWTVector<T> &vec)
00177   {
00178     return static_cast<CWTVector &>(CWTMatrix<T>::operator+=(vec));
00179   }
00180 
00181   template < class T >
00182   inline CWTVector<T> & CWTVector<T>::operator -=(const CWTVector<T> &vec)
00183   {
00184     return static_cast<CWTVector &>(CWTMatrix<T>::operator-=(vec));
00185   }
00186 
00187   template < class T >
00188   inline CWTVector<T> & CWTVector<T>::operator *=(const T &value)
00189   {
00190     return static_cast<CWTVector &>(CWTMatrix<T>::operator*=(value));
00191   }
00192 
00193   template < class T >
00194   inline CWTVector<T> & CWTVector<T>::operator /=(const T &value)
00195   {
00196     // NOTE: This used to work with g++ <= 3.2.
00197     // return (*this) *= static_cast<const T &>(CWTUnity<T>()/value);
00198 
00199     T tTmp = CWTUnity<T>();
00200     tTmp /= value;
00201     return (*this) *= tTmp;
00202   }
00203 
00204   template < class T >
00205   inline void CWTVector<T>::storeAtRow(unsigned irowStart,
00206                                        const CWTVector<T> &vec)
00207   {
00208     CWTMatrix<T>::storeAtPosition(irowStart, 0, vec);
00209   }
00210 
00211   template < class T >
00212   CWTVector<T> CWTVector<T>::operator +(const CWTVector<T> &vec) const
00213   {
00214     return CWTVector<T>(*this) += vec;
00215   }
00216 
00217   template < class T >
00218   CWTVector<T> CWTVector<T>::operator -(const CWTVector<T> &vec) const
00219   {
00220     return CWTVector<T>(*this) -= vec;
00221   }
00222 
00223   template < class T >
00224   CWTVector<T> CWTVector<T>::operator -() const
00225   {
00226     // NOTE: This used to work with g++ <= 3.2.
00227     // return (*this)*static_cast<const T &>(CWTZero<T>() - CWTUnity<T>());
00228 
00229     T tTmp = CWTZero<T>();
00230     tTmp -= CWTUnity<T>();
00231     return (*this)*tTmp;
00232   }
00233 
00234   template < class T >
00235   CWTVector<T> CWTVector<T>::operator *(const T &value) const
00236   {
00237     return CWTVector<T>(*this) *= value;
00238   }
00239 
00240   template < class T >
00241   T CWTVector<T>::operator *(const CWTVector<T> &vec) const
00242   {
00243     T elemResult = CWTZero<T>();
00244 
00245     for (unsigned irow = 0; irow < (*this).getRows(); ++irow)
00246       {
00247         elemResult += (*this)[irow]*vec[irow];
00248       }
00249 
00250     return elemResult;
00251   }
00252 
00253   // length of vector
00254   template < class T >
00255   T CWTVector<T>::norm() const
00256   {
00257     T elemResult = CWTZero<T>();
00258 
00259     elemResult = (*this)*(*this);
00260 
00261     return sqrt( elemResult );
00262   }
00263 
00264   //
00265   // template functions designed work with the vector class.
00266   //
00267 
00268   template < class T >
00269   inline CWTVector<T> operator *(const T &value, const CWTVector<T> &vec)
00270   {
00271     return vec*value;
00272   }
00273 
00274   // matrix*vector must yield a vector
00275   template < class T >
00276   CWTVector<T> operator *(const CWTMatrix<T> &mat, const CWTVector<T> &vec)
00277   {
00278     CWTVector<T> vecResult(mat.getRows());
00279     vecResult.storeProduct(mat, vec);
00280     return vecResult;
00281   }
00282 
00283   // norm computation as a function
00284   template < class T >
00285   inline T norm(const CWTVector<T> &vec)
00286   {
00287     return vec.norm();
00288   }
00289 
00290   // the sign of a vector is a unit vector with the same direction
00291   template <class T>
00292   inline CWTVector<T>
00293   sgn(const CWTVector<T> &vec)
00294   {
00295     return vec.unit();
00296   }
00297 }
00298 
00299 #endif // IG_VECTOR_H

Generated on Sun Jul 3 12:18:14 2005 for Matrix and vector library by  doxygen 1.4.2