vector.h

00001 /*************************************************************************************
00002  * MechSys - A C++ library to simulate (Continuum) Mechanical Systems                *
00003  * Copyright (C) 2005 Dorival de Moraes Pedroso <dorival.pedroso at gmail.com>       *
00004  * Copyright (C) 2005 Raul Dario Durand Farfan  <raul.durand at gmail.com>           *
00005  *                                                                                   *
00006  * This file is part of MechSys.                                                     *
00007  *                                                                                   *
00008  * MechSys is free software; you can redistribute it and/or modify it under the      *
00009  * terms of the GNU General Public License as published by the Free Software         *
00010  * Foundation; either version 2 of the License, or (at your option) any later        *
00011  * version.                                                                          *
00012  *                                                                                   *
00013  * MechSys is distributed in the hope that it will be useful, but WITHOUT ANY        *
00014  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A   *
00015  * PARTICULAR PURPOSE. See the GNU General Public License for more details.          *
00016  *                                                                                   *
00017  * You should have received a copy of the GNU General Public License along with      *
00018  * MechSys; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, *
00019  * Fifth Floor, Boston, MA 02110-1301, USA                                           *
00020  *************************************************************************************/
00021 
00022 #ifndef MECHSYS_LINALG_VECTOR_H
00023 #define MECHSYS_LINALG_VECTOR_H
00024 
00025 #include <sstream>
00026 #include <cassert>
00027 #include <cmath>
00028 
00029 #include "util/numstreams.h"
00030 
00031 namespace LinAlg
00032 {
00033 
00034 // Prototype for expression classes
00035 template <class t_exp, class t_res>
00036 class expression;
00037 
00038 template<typename Type>
00039 class Vector
00040 {
00041 public:
00042     
00043     // Default constructor
00044     Vector() : _size(0), _values(NULL) {}
00045     // Constructor
00046     Vector(int Size)                              : _values(NULL) { _set(Size); }
00047     Vector(int Size, std::string const & strVals) : _values(NULL) { _set(Size,strVals); }
00048     // Destructor
00049     ~Vector()
00050     {
00051         if (_values!=NULL) delete [] _values;
00052     }
00053     // Copy constructor
00054     Vector(Vector<Type> const & Other)
00055     {
00056           _size = Other.Size();
00057         _values = new Type [_size];
00058         for (int i=0; i<_size; ++i)
00059             _values[i] = Other._values[i];
00060     }
00061     // Get
00062            int   Size()   const { return _size; }
00063           Type * GetPtr()       { assert(_values!=NULL); return _values; }
00064     const Type * GetPtr() const { assert(_values!=NULL); return _values; }
00065     // Set
00066     void Set(int Size)                              { _set(Size); }
00067     void Set(int Size, std::string const & strVals) { _set(Size,strVals); }
00068     void SetValues(Type Value) // {{{
00069     {
00070         assert(_values!=NULL);
00071         for (int i=0; i<_size; ++i)
00072             _values[i] = Value; 
00073     } // }}}
00074     void Reset(std::string const & strVals) // {{{
00075     {
00076         std::istringstream iss(strVals);
00077         for (int i=0; i<_size; ++i)
00078             iss >> _values[i];
00079     } // }}}
00080     void Resize(int Size) { _resize(Size); }
00081     // operators
00082     void operator= (const Vector<Type> & R) // {{{
00083     {
00084         assert(&R!=this);
00085         if (R.Size() != _size)
00086         {
00087             _size = R.Size();
00088             if (_values != NULL) delete [] _values;
00089             _values = new Type [_size];
00090         }
00091         
00092         int _components=_size;
00093         for (int i=0; i<_components; ++i)
00094             _values[i] = R._values[i];
00095 
00096     } // }}}
00097 
00098     // operator to assign values separated by commas {{{
00099     class CommaAssign
00100     {
00101     public:
00102         CommaAssign(Type * Values, int & Size, Type const & FirstValue):_values(Values),_size(Size),_index(0) 
00103         { 
00104             assert(_values!=NULL);
00105             _values[0] = FirstValue;
00106             for (int i=1; i<_size; ++i)
00107                 _values[i] = static_cast<Type>(0);
00108         }
00109         CommaAssign & operator, (Type const & Num) 
00110         {
00111             _index++;
00112             assert(_index<_size);
00113             _values[_index] = Num;
00114             return *this;
00115         }
00116     private:
00117         Type * _values;
00118         int  & _size;
00119         int    _index;
00120     };
00121 
00122     CommaAssign operator = (Type const & Num) 
00123     {
00124         return CommaAssign(_values, _size, Num);
00125     } // }}}
00126 
00127     void operator+= (const Vector<Type> & R) // {{{
00128     {
00129         assert(_values!=NULL);
00130         assert(R.Rows()==_size);
00131         int _components=_size;
00132         for (int i=0; i<_components; ++i)
00133             _values[i] += R._values[i];
00134     } // }}}
00135 
00136     void operator-= (const Vector<Type> & R) // {{{
00137     {
00138         assert(_values!=NULL);
00139         assert(R.Rows()==_size);
00140         int _components=_size;
00141         for (int i=0; i<_components; ++i)
00142             _values[i] -= R._values[i];
00143     } // }}}
00144     
00145     // Suport for expression evaluation {{{
00146     template<typename t_exp>
00147     void operator  = (const expression<t_exp, Vector<Type> > & Exp) { Exp.Apply(*this); } 
00148 
00149     template<typename t_exp>
00150     void operator += (const expression<t_exp, Vector<Type> > & Exp) { Exp.Apply_pe(*this); } 
00151 
00152     template<typename t_exp>
00153     void operator -= (const expression<t_exp, Vector<Type> > & Exp) { Exp.Apply_me(*this); } // }}}
00154 
00155     Type & operator() (int i) // {{{
00156     {
00157         assert(_values!=NULL);
00158         assert(i>=0 & i<_size);
00159         return _values[i];
00160     } // }}}
00161     const Type & operator() (int i) const // {{{
00162     {
00163         assert(_values!=NULL);
00164         assert(i>=0 & i<_size);
00165         return _values[i];
00166     } // }}}
00167 private:
00168     int    _size;
00169     Type * _values;
00170     void _set(int Size) // {{{
00171     {
00172         assert(_values==NULL);
00173         assert(Size>0);
00174           _size = Size;
00175         _values = new Type [_size];
00176     } // }}}
00177     void _set(int Size, std::string const & strVals) // {{{
00178     {
00179         assert(_values==NULL);
00180         assert(Size>0);
00181           _size = Size;
00182         _values = new Type [_size];
00183         std::istringstream iss(strVals);
00184         for (int i=0; i<_size; ++i)
00185             iss >> _values[i];
00186     } // }}}
00187     void _resize(int Size) // {{{
00188     {
00189         assert(Size>0);
00190         if (Size==_size && _values!=NULL) return;   
00191         if (_values!=NULL) delete [] _values;
00192         _size = Size;
00193         _values = new Type [_size];
00194     } // }}}
00195 }; // class Vector<Type>
00196 
00197 
00198 template<typename Type>
00199 std::ostream & operator<< (std::ostream & os, const LinAlg::Vector<Type> & V) // {{{
00200 {
00201     for (int i=0; i<V.Size(); ++i)
00202         os << _8s()<< V(i);
00203     os << std::endl;
00204     return os;
00205 } // }}}
00206 
00207 }; // namespace LinAlg
00208 
00209 #endif // MECHSYS_LINALG_VECTOR_H
00210 
00211 // vim:fdm=marker

Generated on Wed Jan 24 15:56:26 2007 for MechSys by  doxygen 1.4.7