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

stat_svector.h

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

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