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

svector.h

Go to the documentation of this file.
00001 // -*-c++-*-
00002 #ifndef IG_SVECTOR_H
00003 #define IG_SVECTOR_H
00004 
00005 // $Id: svector.h,v 1.29 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 #ifndef IG_VECTOR_H
00027 #include "vector.h"
00028 #endif
00029 
00030 #ifndef IG_SMATRIX_H
00031 #include "smatrix.h"
00032 #endif
00033 
00034 namespace CwMtx
00035 {
00036 
00037   // prefix svec
00038   template < class T = double >
00039   class CWTSpaceVector: public CWTVector<T>
00040   {
00041   public:
00042     CWTSpaceVector(): CWTVector<T>(3U) {};
00043     CWTSpaceVector(const CWTMatrix<T> &mat): CWTVector<T>(mat) {};
00044     CWTSpaceVector(const CWTVector<T> &vec): CWTVector<T>(vec) {};
00045     CWTSpaceVector(const CWTSpaceVector &svec): CWTVector<T>(svec) {};
00046     // construct from 3 elements
00047     CWTSpaceVector(const T &, const T &, const T &);
00048     CWTSpaceVector(const CWTMatrix<T> &, unsigned, unsigned);
00049     CWTSpaceVector(const CWTVector<T> &vec,
00050                    unsigned irowStart): CWTVector<T>(vec,
00051                                                      irowStart,
00052                                                      irowStart + 2) {};
00053 
00054     ~CWTSpaceVector() {};
00055 
00056     void dimension() { CWTVector<T>::dimension(3); };
00057     void mapInto(const CWTMatrix<T> &mat,
00058                  unsigned irowStart,
00059                  unsigned icolStart);
00060     void mapInto(const CWTVector<T> &vec,
00061                  unsigned irowStart = 0);
00062 
00063     CWTSpaceVector operator +(const CWTSpaceVector &) const;
00064     CWTSpaceVector operator -(const CWTSpaceVector &) const;
00065     CWTSpaceVector operator -() const;
00066     CWTSpaceVector operator *(const T &) const;
00067     // inner product
00068     T operator *(const CWTSpaceVector &) const;
00069     // outer product
00070     CWTSpaceVector operator %(const CWTSpaceVector &) const;
00071     CWTSpaceVector operator /(const T &value) const;
00072 
00073     // not inherited
00074     CWTSpaceVector & operator =(const CWTSpaceVector &);
00075     CWTSpaceVector & operator +=(const CWTSpaceVector &);
00076     CWTSpaceVector & operator -=(const CWTSpaceVector &);
00077     CWTSpaceVector & operator *=(const T &);
00078     // outer product
00079     CWTSpaceVector & operator %=(const CWTSpaceVector &);
00080     CWTSpaceVector & operator /=(const T &value);
00081 
00082     void storeOuterProduct(const CWTSpaceVector &, const CWTSpaceVector &);
00083 
00084     // returns a unit vector with same direction as this
00085     CWTSpaceVector unit() const { return (*this)/(this->norm()); }
00086   };
00087 
00088   // NOTE: There exists no unity space vector!
00089 
00090   // Zero space vector.
00091   template <class T>
00092   class CWTZero< CWTSpaceVector<T> >: public CWTSpaceVector<T>
00093   {
00094   public:
00095     CWTZero() { fill(CWTZero<T>()); }
00096   };
00097 
00098   //
00099   // Constructors
00100   //
00101 
00102   template < class T >
00103   inline CWTSpaceVector<T>::CWTSpaceVector(const CWTMatrix<T> &mat,
00104                                            unsigned irowStart,
00105                                            unsigned icolStart)
00106     :
00107     CWTVector<T>(mat, irowStart, icolStart, irowStart + 2)
00108   {
00109   }
00110 
00111   template < class T >
00112   inline CWTSpaceVector<T>::CWTSpaceVector(const T &elem1,
00113                                            const T &elem2,
00114                                            const T &elem3)
00115     :
00116     CWTVector<T>(3U)
00117   {
00118     (*this)[0] = elem1;
00119     (*this)[1] = elem2;
00120     (*this)[2] = elem3;
00121   }
00122 
00123   //
00124   // User Methods
00125   //
00126 
00127   template < class T >
00128   inline void CWTSpaceVector<T>::mapInto(const CWTMatrix<T> &mat,
00129                                          unsigned irowStart,
00130                                          unsigned icolStart)
00131   {
00132     CWTVector<T>::mapInto(mat, irowStart, icolStart, irowStart + 2);
00133   }
00134 
00135   template < class T >
00136   inline void CWTSpaceVector<T>::mapInto(const CWTVector<T> &vec,
00137                                          unsigned irowStart)
00138   {
00139     CWTVector<T>::mapInto(vec, irowStart, irowStart + 2);
00140   }
00141 
00142   template < class T >
00143   inline CWTSpaceVector<T> CWTSpaceVector<T>::operator /(const T &value) const
00144   {
00145     // NOTE: This used to work with g++ <= 3.2.
00146     // return (*this)*static_cast<const T &>(CWTUnity<T>()/value);
00147 
00148     T tTmp = CWTUnity<T>();
00149     tTmp /= value;
00150     return (*this)*tTmp;
00151   }
00152 
00153   // not inherited
00154   template < class T >
00155   inline CWTSpaceVector<T> &
00156   CWTSpaceVector<T>::operator =(const CWTSpaceVector<T> &svec)
00157   {
00158     return static_cast<CWTSpaceVector &>(CWTMatrix<T>::operator=(svec));
00159   }
00160 
00161   template < class T >
00162   inline CWTSpaceVector<T> &
00163   CWTSpaceVector<T>::operator +=(const CWTSpaceVector<T> &svec)
00164   {
00165     return static_cast<CWTSpaceVector &>(CWTMatrix<T>::operator+=(svec));
00166   }
00167 
00168   template < class T >
00169   inline CWTSpaceVector<T> &
00170   CWTSpaceVector<T>::operator -=(const CWTSpaceVector<T> &svec)
00171   {
00172     return static_cast<CWTSpaceVector &>(CWTMatrix<T>::operator-=(svec));
00173   }
00174 
00175   template < class T >
00176   inline CWTSpaceVector<T> & CWTSpaceVector<T>::operator *=(const T &value)
00177   {
00178     return static_cast<CWTSpaceVector &>(CWTMatrix<T>::operator*=(value));
00179   }
00180 
00181   template < class T >
00182   inline CWTSpaceVector<T> & CWTSpaceVector<T>::operator /=(const T &value)
00183   {
00184     // NOTE: This used to work with g++ <= 3.2.
00185     // return (*this) *= static_cast<const T &>(CWTUnity<T>()/value);
00186 
00187     T tTmp = CWTUnity<T>();
00188     tTmp /= value;
00189     return (*this) *= tTmp;
00190   }
00191 
00192   // outer product
00193   template < class T >
00194   inline CWTSpaceVector<T> &
00195   CWTSpaceVector<T>::operator %=(const CWTSpaceVector<T> &vec)
00196   {
00197     return (*this) = (*this)%vec;
00198   }
00199 
00200   template < class T >
00201   CWTSpaceVector<T>
00202   CWTSpaceVector<T>::operator +(const CWTSpaceVector<T> &svec) const
00203   {
00204     return CWTSpaceVector(*this) += svec;
00205   }
00206 
00207   template < class T >
00208   CWTSpaceVector<T>
00209   CWTSpaceVector<T>::operator -(const CWTSpaceVector<T> &svec) const
00210   {
00211     return CWTSpaceVector(*this) -= svec;
00212   }
00213 
00214   template < class T >
00215   CWTSpaceVector<T> CWTSpaceVector<T>::operator -() const
00216   {
00217     // NOTE: This used to work with g++ <= 3.2.
00218     // return (*this)*static_cast<const T &>(CWTZero<T>() - CWTUnity<T>());
00219 
00220     T tTmp = CWTZero<T>();
00221     tTmp -= CWTUnity<T>();
00222     return (*this)*tTmp;
00223   }
00224 
00225   template < class T >
00226   CWTSpaceVector<T> CWTSpaceVector<T>::operator *(const T &value) const
00227   {
00228     return CWTSpaceVector(*this) *= value;
00229   }
00230 
00231   // inner product
00232   template < class T >
00233   T CWTSpaceVector<T>::operator *(const CWTSpaceVector<T> &vec) const
00234   {
00235     return CWTVector<T>::operator*(vec);
00236   }
00237 
00238   template < class T >
00239   void CWTSpaceVector<T>::
00240   storeOuterProduct(const CWTSpaceVector<T> &svecLeft,
00241                     const CWTSpaceVector<T> &svecRight)
00242   {
00243     // to reduce the number of calls to the subscript operator
00244     T svecLeft0 = svecLeft[0];
00245     T svecLeft1 = svecLeft[1];
00246     T svecLeft2 = svecLeft[2];
00247 
00248     T svecRight0 = svecRight[0];
00249     T svecRight1 = svecRight[1];
00250     T svecRight2 = svecRight[2];
00251 
00252     (*this)[0] = svecLeft1*svecRight2 - svecLeft2*svecRight1;
00253     (*this)[1] = svecLeft2*svecRight0 - svecLeft0*svecRight2;
00254     (*this)[2] = svecLeft0*svecRight1 - svecLeft1*svecRight0;
00255   }
00256 
00257   template < class T >
00258   CWTSpaceVector<T>
00259   CWTSpaceVector<T>::operator %(const CWTSpaceVector<T> &svec) const
00260   {
00261     CWTSpaceVector<T> svecResult;
00262     svecResult.storeOuterProduct(*this, svec);
00263     return svecResult;
00264   }
00265 
00266   //
00267   // template functions designed work with the base matrix class.
00268   //
00269 
00270   template < class T >
00271   inline CWTSpaceVector<T> operator *(const T &value,
00272                                       const CWTSpaceVector<T> &svec)
00273   {
00274     return svec*value;
00275   }
00276 
00277   // (square matrix)*(space vector) must yield a space vector
00278   template < class T >
00279   CWTSpaceVector<T> operator *(const CWTSquareMatrix<T> &smat,
00280                                const CWTSpaceVector<T> &svec)
00281   {
00282     CWTSpaceVector<T> svecResult;
00283     svecResult.storeProduct(smat, svec);
00284     return svecResult;
00285   }
00286 
00287   // the sign of a vector is a unit vector with the same direction
00288   template <class T>
00289   inline CWTSpaceVector<T>
00290   sgn(const CWTSpaceVector<T> &svec)
00291   {
00292     return svec.unit();
00293   }
00294 }
00295 
00296 #endif // IG_SVECTOR_H

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