00001 
00002 #ifndef IG_VECTOR_H
00003 #define IG_VECTOR_H
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
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     
00044     CWTVector(const CWTMatrix<T>& , unsigned, unsigned, unsigned);
00045     
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     
00072     T operator *(const CWTVector &) const;
00073     CWTVector operator /(const T &value) const
00074     {
00075       
00076       
00077 
00078       T tTmp = CWTUnity<T>();
00079       tTmp /= value;
00080       return (*this)*tTmp;
00081     }
00082 
00083     
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     
00091     T operator !() const { return (*this).norm(); };
00092 
00093     void storeAtRow(unsigned, const CWTVector &);
00094     
00095     T norm() const;
00096     
00097     CWTVector unit() const { return (*this)/norm(); }
00098 
00099     
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   
00113 
00114   
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   
00125   
00126 
00127   
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   
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   
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   
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     
00197     
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     
00227     
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   
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   
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   
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   
00284   template < class T >
00285   inline T norm(const CWTVector<T> &vec)
00286   {
00287     return vec.norm();
00288   }
00289 
00290   
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