GEL  2
GEL is a library for Geometry and Linear Algebra
/Users/jab/Documents/Teaching/02585/GEL2_and_demos/GEL/src/LinAlg/Vector.h
Go to the documentation of this file.
00001 
00005 #if !defined(VECTOR_H__HAA_AGUST_2001)
00006 #define VECTOR_H__HAA_AGUST_2001
00007 
00008 #include <cassert>
00009 #include <iostream>
00010 #include <cmath>
00011 #include "CGLA/Vec2f.h"
00012 #include "CGLA/Vec3f.h"
00013 
00014 namespace LinAlg
00015 {
00016         template <class T> class CMatrixType;  
00017 
00051         template <class T>
00052         class CVectorType  
00053         {
00054 
00055                 friend class CMatrixType<T>;
00056 
00057         private:
00058 
00060                 int nElems;
00061 
00063                 T*Data;
00064 
00066                 void SetScalar(const T& Scalar) 
00067                 {
00068                         T* Itt=Data;
00069                         T* Stop=&(Data[nElems]);
00070                         while(Itt!=Stop)
00071                                 {
00072                                         *Itt=Scalar;
00073                                         ++Itt;
00074                                 }
00075                 }; 
00076 
00078                 void CleanUp(void)  {if(Data!=NULL) delete [] Data;};
00080 
00081                 void Allocate(void) { Data= new T[nElems];};
00082 
00083 
00084         public:
00085 
00087 
00088 
00089                 CVectorType<T>():nElems(0),Data(NULL) {};
00090 
00092                 explicit CVectorType<T>(const int Length): nElems(Length) {Allocate();}
00093 
00095                 CVectorType<T>(const int Length,const T& Scalar): nElems(Length) {Allocate();SetScalar(Scalar);}
00096 
00098                 CVectorType<T>(const CVectorType<T>& Rhs): nElems(Rhs.nElems)
00099                 {
00100                         Allocate();
00101                         memcpy(Data,Rhs.Data,nElems*sizeof(T));
00102                 }
00103 
00105                 template <class TT, class V, unsigned int N> 
00106                 CVectorType<T>(const CGLA::ArithVec<TT,V,N>& Rhs):nElems(N)
00107                 {
00108                         Allocate(); 
00109                         for(int i=0;i<N;++i)
00110                                 Data[i]=Rhs[i];
00111                 }
00112 
00114                 virtual ~CVectorType<T>(){ CleanUp();};
00116 
00117 
00122                 CVectorType<T>& operator=(const T& Rhs) {SetScalar(Rhs);return *this;}
00123 
00124                 template <class TT, class V, unsigned int N> 
00125                 CVectorType<T>& operator=(const CGLA::ArithVec<TT,V,N>& Rhs)
00126                 {
00127                         Resize(N);
00128                         for(int i=0;i<N;++i)
00129                                 Data[i]=Rhs[i];
00130                 }
00131 
00132 
00133                 CVectorType<T>& operator=(const CVectorType<T>& Rhs)
00134                 {
00135                         Resize(Rhs.nElems);
00136                         memcpy(Data,Rhs.Data,nElems*sizeof(T));
00137                         return *this;
00138                 }
00140 
00142 
00143 
00144                 const int Length(void) const {return nElems;}
00146                 void Resize(const int Length)
00147                 {
00148                         if(Length!=nElems)
00149                                 {
00150                                         CleanUp();
00151                                         nElems=Length;
00152                                         Data= new T[nElems];
00153                                 }
00154                 }
00156 
00164                 const T& get(const int i)const 
00165                 {
00166                         assert(i>=0);
00167                         assert(i<nElems);
00168 
00169                         return Data[i];
00170                 }
00171 
00172                 void set(const int i,const T& val)
00173                 {
00174                         assert(i>=0);
00175                         assert(i<nElems);
00176 
00177                         Data[i]=val;
00178                 }
00179 
00180                 T& operator[](const int i) 
00181                 {
00182                         assert(i>=0);
00183                         assert(i<nElems);
00184                 
00185                         return Data[i];
00186                 }
00187 
00188                 const T& operator[](const int i) const 
00189                 {
00190                         assert(i>=0);
00191                         assert(i<nElems);
00192                 
00193                         return Data[i];
00194                 }
00196 
00198 
00199                 CVectorType<T> operator+(const CVectorType<T>& Rhs) const {CVectorType<T>Ret(*this); return Ret+=Rhs;};
00200                 CVectorType<T> operator-(const CVectorType<T>& Rhs) const {CVectorType<T>Ret(*this); return Ret-=Rhs;};
00201                 CVectorType<T> operator+(const T& Rhs) const {CVectorType<T>Ret(*this); return Ret+=Rhs;};
00202                 CVectorType<T> operator-(const T& Rhs) const {CVectorType<T>Ret(*this); return Ret-=Rhs;};
00203                 CVectorType<T> operator*(const T& Rhs) const {CVectorType<T>Ret(*this); return Ret*=Rhs;};
00204                 CVectorType<T> operator/(const T& Rhs) const {CVectorType<T>Ret(*this); return Ret/=Rhs;};
00205 
00206                 CVectorType<T>& operator+=(const CVectorType<T>& Rhs)
00207                 {
00208                         assert(nElems==Rhs.nElems);
00209 
00210                         T* IttRhs=Rhs.Data;
00211                         T* Itt=Data;
00212                         T* Stop=&(Data[nElems]);
00213                         while(Itt!=Stop)
00214                                 {
00215                                         (*Itt)+=(*IttRhs);
00216                                         ++Itt;
00217                                         ++IttRhs;
00218                                 }
00219                         return *this; 
00220                 }
00221 
00222                 CVectorType<T>& operator-=(const CVectorType<T>& Rhs)
00223                 {
00224                         assert(nElems==Rhs.nElems);
00225 
00226                         T* IttRhs=Rhs.Data;
00227                         T* Itt=Data;
00228                         T* Stop=&(Data[nElems]);
00229                         while(Itt!=Stop)
00230                                 {
00231                                         (*Itt)-=(*IttRhs);
00232                                         ++Itt;
00233                                         ++IttRhs;
00234                                 }
00235                         return *this; 
00236                 }
00237 
00238                 CVectorType<T>& operator+=(const T& Rhs)
00239                 {
00240                         T* Itt=Data;
00241                         T* Stop=&(Data[nElems]);
00242                         while(Itt!=Stop)
00243                                 {
00244                                         (*Itt)+=Rhs;
00245                                         ++Itt;
00246                                 }
00247                         return *this; 
00248                 }
00249 
00250                 CVectorType<T>& operator-=(const T& Rhs)
00251                 {
00252                         T* Itt=Data;
00253                         T* Stop=&(Data[nElems]);
00254                         while(Itt!=Stop)
00255                                 {
00256                                         (*Itt)-=Rhs;
00257                                         ++Itt;
00258                                 }
00259                         return *this; 
00260                 }
00261 
00262                 CVectorType<T>& operator*=(const T& Rhs)
00263                 {
00264                         T* Itt=Data;
00265                         T* Stop=&(Data[nElems]);
00266                         while(Itt!=Stop)
00267                                 {
00268                                         (*Itt)*=Rhs;
00269                                         ++Itt;
00270                                 }
00271                         return *this; 
00272                 }
00273 
00274                 CVectorType<T>& operator/=(const T& Rhs)
00275                 {
00276                         T* Itt=Data;
00277                         T* Stop=&(Data[nElems]);
00278                         while(Itt!=Stop)
00279                                 {
00280                                         (*Itt)/=Rhs;
00281                                         ++Itt;
00282                                 }
00283                         return *this; 
00284                 }
00285 
00286                 T operator*(const CVectorType<T>& Rhs) const
00287                 {
00288                         assert(nElems==Rhs.nElems);
00289                         T Ret=0;
00290 
00291                         T* IttRhs=Rhs.Data;
00292                         T* Itt=Data;
00293                         T* Stop=&(Data[nElems]);
00294                         while(Itt!=Stop)
00295                                 {
00296                                         Ret+=(*Itt)*(*IttRhs);
00297                                         ++Itt;
00298                                         ++IttRhs;
00299                                 }
00300                         return Ret; 
00301                 }
00302 
00303                 CVectorType<T> operator*(const CMatrixType<T>& Rhs) const
00304                 {
00305                         assert(nElems==Rhs.Rows());
00306                         CVectorType<T> Ret(Rhs.Cols(),0);
00307 
00308                         T* ThisItt=Data;
00309                         const T* RhsItt=Rhs.Data;
00310                         const T* Stop=&(Rhs.Data[Rhs.nElems]);
00311                         T* RetItt;
00312 
00313                         while(RhsItt!=Stop)
00314                                 {
00315                                         RetItt=Ret.Data;
00316                                         for(int cCol=Rhs.Cols()-1;cCol>=0;--cCol)
00317                                                 {
00318                                                         (*RetItt)+=(*ThisItt)*(*RhsItt);
00319                                                         ++RetItt;
00320                                                         ++RhsItt;
00321                                                 }
00322                                         ++ThisItt;
00323                                 }
00324                         return Ret;
00325                 }
00327 
00329 
00330                 const T Norm(void) const {return sqrt((*this)*(*this));}
00331                 const T NormMax(void) const
00332                 {
00333                         T Ret=0;
00334                         T Abs;
00335                         const T* Itt=Data;
00336                         const T* Stop=&(Data[nElems]);
00337                         while(Itt!=Stop)
00338                                 {
00339                                         Abs=(*Itt)>0?(*Itt):-(*Itt);
00340                                         if(Ret<Abs)
00341                                                 Ret=Abs;
00342 
00343                                         ++Itt;
00344                                 }
00345                         return Ret;
00346                 }
00348 
00350                 const T Sum(void) const
00351                 {
00352                         T Ret=0;
00353                         const T* Itt=Data;
00354                         const T* Stop=&(Data[nElems]);
00355                         while(Itt!=Stop)
00356                                 {
00357                                         Ret+=(*Itt);
00358                                         ++Itt;
00359                                 }
00360                 }
00361 
00362 
00369 
00370                 void ElemSqr(void)
00371                 {
00372                         T* Itt=Data;
00373                         T* Stop=&(Data[nElems]);
00374                         while(Itt!=Stop)
00375                                 {
00376                                         (*Itt)*=(*Itt);
00377                                         ++Itt;
00378                                 }
00379                 }
00380 
00381 
00382         };
00383 
00384 
00391         template<class T>
00392         inline CVectorType<T> operator+(const T& Lhs,const CVectorType<T>&Rhs ) 
00393         {
00394                 return Rhs+Lhs;
00395         }
00396 
00397         template<class T>
00398         inline CVectorType<T> operator*(const T& Lhs,const CVectorType<T>&Rhs ) 
00399         {
00400                 return Rhs*Lhs;
00401         }
00402 
00403         template <class T>
00404         std::ostream& operator<<(std::ostream &s, const CVectorType<T> &a)
00405         {
00406                 int nElems=a.Length();
00407         
00408                 for (int cElem=0; cElem<nElems; cElem++)
00409                         {
00410                                 s << a[cElem] << " ";
00411                         }
00412                 s << "\n";
00413         
00414                 return s;
00415         }
00416 
00417 
00418         template <class T>
00419         std::istream& operator>>(std::istream &s, CVectorType<T> &a)
00420         {
00421                 int nElems;
00422 
00423                 s >> nElems;
00424 
00425                 if ( nElems!=a.Length())
00426                         a.Resize(nElems);
00427 
00428 
00429                 for (int cElem=0; cElem<a.Length();cElem++)
00430                         {
00431                                 s >>  a[cElem];
00432                         }
00433 
00434                 return s;
00435         }
00437 
00439         typedef CVectorType<double> CVector;
00440 }
00441 #endif // !defined(VECTOR_H__HAA_AGUST_2001)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations