00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00035 template <class t_exp, class t_res>
00036 class expression;
00037
00038 template<typename Type>
00039 class Vector
00040 {
00041 public:
00042
00043
00044 Vector() : _size(0), _values(NULL) {}
00045
00046 Vector(int Size) : _values(NULL) { _set(Size); }
00047 Vector(int Size, std::string const & strVals) : _values(NULL) { _set(Size,strVals); }
00048
00049 ~Vector()
00050 {
00051 if (_values!=NULL) delete [] _values;
00052 }
00053
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
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
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
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
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
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 };
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 };
00208
00209 #endif // MECHSYS_LINALG_VECTOR_H
00210
00211