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

stat_vector.h

Go to the documentation of this file.
00001 // -*-c++-*-
00002 #ifndef IG_STAT_VECTOR_H
00003 #define IG_STAT_VECTOR_H
00004 
00005 // $Id: stat_vector.h,v 1.1.2.6 2005/05/30 09:14:25 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_STAT_MATRIX_H
00029 #include "stat_matrix.h"
00030 #endif
00031 
00032 namespace CwMtx
00033 {
00034 
00035   template <unsigned r, class T = double>
00036   class CWTSVector : public CWTSMatrix<r, 1, T>
00037   {
00038   public:
00039     CWTSVector(): CWTSMatrix<r, 1, T>() {}
00040     CWTSVector(const CWTSMatrix<r, 1, T>& mat): CWTSMatrix<r, 1, T>(mat) {}
00041     CWTSVector(const CWTSVector& vec): CWTSMatrix<r, 1, T>(vec) {}
00042 
00043     ~CWTSVector() {}
00044 
00045     T & operator [](unsigned irow);
00046     const T & operator [](unsigned irow) const;
00047 
00048     CWTSVector operator +(const CWTSVector &) const;
00049     CWTSVector operator -(const CWTSVector &) const;
00050     CWTSVector operator -() const;
00051     CWTSVector operator *(const T &) const;
00052     CWTSVector operator /(const T &value) const;
00053     // CWTSVector*CWTSVector, inner product
00054     T operator *(const CWTSVector &) const;
00055 
00056     // not inherited
00057     CWTSVector & operator =(const CWTSVector &vec);
00058     CWTSVector & operator +=(const CWTSVector &vec);
00059     CWTSVector & operator -=(const CWTSVector &vec);
00060     CWTSVector & operator *=(const T &value);
00061     CWTSVector & operator /=(const T &value);
00062 
00063     // CWTSVector norm
00064     T operator !() const { return (*this).norm(); }
00065     // returns vector norm (length)
00066     T norm() const;
00067     // returns a unit vector with same direction as this
00068     CWTSVector unit() const { return (*this)/norm(); }
00069 
00070     // make this a unit vector
00071     void makeUnit() { (*this) /= norm(); }
00072 
00073     template <unsigned r2>
00074     void storeAtRow(unsigned, const CWTSVector<r2, T> &);
00075   };
00076 
00077   template <unsigned r, class T>
00078   inline T &
00079   CWTSVector<r, T>::operator [](unsigned irow)
00080   {
00081     return this->CWTSMatrix<r, 1, T>::operator[](irow)[0];
00082   }
00083 
00084   template <unsigned r, class T>
00085   inline const T &
00086   CWTSVector<r, T>::operator [](unsigned irow) const
00087   {
00088     return this->CWTSMatrix<r, 1, T>::operator[](irow)[0];
00089   }
00090 
00091   // not inherited
00092   template <unsigned r, class T>
00093   inline CWTSVector<r, T> &
00094   CWTSVector<r, T>::operator =(const CWTSVector<r, T> &vec)
00095   {
00096     return static_cast<CWTSVector &>(CWTSMatrix<r, 1, T>::operator=(vec));
00097   }
00098 
00099   template <unsigned r, class T>
00100   inline CWTSVector<r, T> &
00101   CWTSVector<r, T>::operator +=(const CWTSVector<r, T> &vec)
00102   {
00103     return static_cast<CWTSVector &>(CWTSMatrix<r, 1, T>::operator+=(vec));
00104   }
00105 
00106   template <unsigned r, class T>
00107   inline CWTSVector<r, T> &
00108   CWTSVector<r, T>::operator -=(const CWTSVector<r, T> &vec)
00109   {
00110     return static_cast<CWTSVector &>(CWTSMatrix<r, 1, T>::operator-=(vec));
00111   }
00112 
00113   template <unsigned r, class T>
00114   inline CWTSVector<r, T> &
00115   CWTSVector<r, T>::operator *=(const T &value)
00116   {
00117     return static_cast<CWTSVector &>(CWTSMatrix<r, 1, T>::operator*=(value));
00118   }
00119 
00120   template <unsigned r, class T>
00121   inline CWTSVector<r, T> &
00122   CWTSVector<r, T>::operator /=(const T &value)
00123   {
00124     return (*this) *= CWTSUnity<T>()/value;
00125   }
00126 
00127   template <unsigned r, class T>
00128   CWTSVector<r, T>
00129   CWTSVector<r, T>::operator +(const CWTSVector<r, T> &vec) const
00130   {
00131     return CWTSVector<r, T>(*this) += vec;
00132   }
00133 
00134   template <unsigned r, class T>
00135   CWTSVector<r, T>
00136   CWTSVector<r, T>::operator -(const CWTSVector<r, T> &vec) const
00137   {
00138     return CWTSVector<r, T>(*this) -= vec;
00139   }
00140 
00141   template <unsigned r, class T>
00142   CWTSVector<r, T>
00143   CWTSVector<r, T>::operator -() const
00144   {
00145     return (*this)*(CWTSZero<T>() - CWTSUnity<T>());
00146   }
00147 
00148   template <unsigned r, class T>
00149   CWTSVector<r, T>
00150   CWTSVector<r, T>::operator *(const T &value) const
00151   {
00152     return CWTSVector<r, T>(*this) *= value;
00153   }
00154 
00155   template <unsigned r, class T>
00156   CWTSVector<r, T>
00157   CWTSVector<r, T>::operator /(const T &value) const
00158   {
00159     return (*this)*(CWTSUnity<T>()/value);
00160   }
00161 
00162   template <unsigned r, class T>
00163   T
00164   CWTSVector<r, T>::operator *(const CWTSVector<r, T> &vec) const
00165   {
00166     T elemResult = CWTSZero<T>();
00167 
00168     for (unsigned irow = 0; irow < r; ++irow)
00169       {
00170         elemResult += (*this)[irow]*vec[irow];
00171       }
00172 
00173     return elemResult;
00174   }
00175 
00176   // length of vector
00177   template <unsigned r, class T>
00178   T
00179   CWTSVector<r, T>::norm() const
00180   {
00181     T elemResult = CWTSZero<T>();
00182 
00183     elemResult = (*this)*(*this);
00184 
00185     return sqrt(elemResult);
00186   }
00187 
00188   template <unsigned r, class T>
00189   template <unsigned r2>
00190   inline void
00191   CWTSVector<r, T>::storeAtRow(unsigned irowStart,
00192                                const CWTSVector<r2, T> &vec)
00193   {
00194     CWTSMatrix<r, 1, T>::storeAtPosition(irowStart, 0, vec);
00195   }
00196 
00197   // template functions designed work with the vector class.
00198 
00199   template <unsigned r, class T>
00200   inline CWTSVector<r, T>
00201   operator *(const T &value, const CWTSVector<r, T> &vec)
00202   {
00203     return vec*value;
00204   }
00205 
00206   // matrix*vector must yield a vector
00207   template <unsigned r, unsigned c, class T>
00208   CWTSVector<r, T>
00209   operator *(const CWTSMatrix<r, c, T> &mat, const CWTSVector<r, T> &vec)
00210   {
00211     CWTSVector<r, T> vecResult;
00212 
00213     for (unsigned irow = 0; irow < r; ++irow)
00214       {
00215         vecResult[irow] = CWTSZero<T>();
00216 
00217         for (unsigned icol = 0; icol < c; ++icol)
00218           {
00219             vecResult[irow] += mat[irow][icol]*vec[icol];
00220           }
00221       }
00222 
00223     return vecResult;
00224   }
00225 
00226   // norm computation as a function
00227   template <unsigned r, class T>
00228   inline T
00229   norm(const CWTSVector<r, T> &vec)
00230   {
00231     return vec.norm();
00232   }
00233 
00234   // the sign of a vector is a unit vector with the same direction
00235   template <unsigned r, class T>
00236   inline CWTSVector<r, T>
00237   sgn(const CWTSVector<r, T> &vec)
00238   {
00239     return vec.unit();
00240   }
00241 }
00242 
00243 #endif // IG_STAT_VECTOR_H

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