1 // marMatrix.cpp: implementation of the marMatrix class.
3 //////////////////////////////////////////////////////////////////////
8 //////////////////////////////////////////////////////////////////////
10 //////////////////////////////////////////////////////////////////////
11 marMatrix::marMatrix(size_t size1, size_t size2)
16 _data=new double[_size1*_size2];
19 marMatrix::marMatrix(double *data, size_t size1, size_t size2)
22 if (size1==0 || size2==0)
32 // PS -> size_t iMax=_size1*_size2;
33 // PS -> _data=new double[iMax];
35 // PS -> for (int i=0;i<iMax;i++)
37 // PS -> _data[i]=data[i];
44 marMatrix::marMatrix(const marMatrix &m)
47 if (m._size1==0 || m._size2==0)
56 size_t iMax=_size1*_size2;
57 _data=new double[iMax];
67 //////////////////////////////////////////////////////////////////////
69 //////////////////////////////////////////////////////////////////////
70 marMatrix::~marMatrix()
82 //////////////////////////////////////////////////////////////////////
84 //////////////////////////////////////////////////////////////////////
85 // ---------------------------------------------------------------------
87 std::ostream& operator<<(std::ostream& os,const marMatrix& m)
90 for (i =0;i<m._size1;i++)
92 for (j =0;j<m._size2;j++)
94 os << " " << m._data[i*m._size2+j];
101 // ---------------------------------------------------------------------
103 marMatrix::operator double*() const
108 // ---------------------------------------------------------------------
110 double& marMatrix::operator()(size_t i, size_t j)
112 return(_data[i*_size2+j]);
115 const double& marMatrix::operator()(size_t i, size_t j) const
117 return(_data[i*_size2+j]);
120 // ---------------------------------------------------------------------
122 marMatrix& marMatrix::operator=(const marMatrix& o)
126 if (!(_data==NULL)&&(!shallowCopy))
131 size_t iMax=_size1*_size2;
132 _data=new double[iMax];
142 marMatrix& marMatrix::operator=(double o)
144 if (_size1!=0 && _size2!=0)
146 if (!(_data==NULL)&&(!shallowCopy))
151 size_t iMax=_size1*_size2;
152 _data=new double[iMax];
153 for (int i=0;i<iMax;i++)
165 marMatrix& marMatrix::operator=(double*o)
167 if (_size1!=0 && _size2!=0)
169 if (!(_data==NULL)&&(!shallowCopy))
174 size_t iMax=_size1*_size2;
175 _data=new double[iMax];
176 for (int i=0;i<iMax;i++)
188 // ---------------------------------------------------------------------
190 bool marMatrix::operator==(const marMatrix& o) const
194 if(_size1!=o._size1 || _size2!=o._size2)
198 size_t iMax=_size1*_size2;
200 for (int i=0;i<iMax && equal;i++)
202 equal=equal && (_data[i]==o._data[i]);
207 bool marMatrix::operator!=(const marMatrix& o) const
209 return(!((*this)==o));
212 // ---------------------------------------------------------------------
214 marMatrix marMatrix::operator+(const marMatrix& o)
216 marMatrix result(*this);
218 if ((o._size1!=result._size1)
219 || (result._size1==0)
220 || (o._size2!=result._size2)
221 || (result._size2==0))
227 size_t iMax=result._size1*result._size2;
229 for (i=0 ; i < iMax ; i++)
231 result._data[i]+=o._data[i];
237 marMatrix marMatrix::operator+(double o)
239 marMatrix result(*this);
241 if ((result._size1==0) || (result._size2==0))
247 size_t iMax=result._size1*result._size2;
249 for (i=0;i < iMax;i++)
257 marMatrix marMatrix::operator+(double*o)
259 marMatrix result(*this);
261 if ((result._size1==0) || (result._size2==0))
267 size_t iMax=result._size1*result._size2;
269 for (i=0;i < iMax;i++)
271 result._data[i]+=o[i];
277 // ---------------------------------------------------------------------
279 marMatrix marMatrix::operator-(const marMatrix& o)
281 marMatrix result(*this);
283 if ((o._size1!=result._size1)
284 || (result._size1==0)
285 || (o._size2!=result._size2)
286 || (result._size2==0))
292 size_t iMax=result._size1*result._size2;
294 for (i=0;i < iMax;i++)
296 result._data[i]-=o._data[i];
302 marMatrix marMatrix::operator-(double o)
304 marMatrix result(*this);
306 if ((result._size1==0) || (result._size2==0))
312 size_t iMax=result._size1*result._size2;
314 for (i=0;i < iMax;i++)
322 marMatrix marMatrix::operator-(double*o)
324 marMatrix result(*this);
326 if ((result._size1==0) || (result._size2==0))
332 size_t iMax=result._size1*result._size2;
334 for (i=0;i < iMax;i++)
336 result._data[i]-=o[i];
342 // ---------------------------------------------------------------------
343 /*Multiplication (produit scalaire)*/
344 marMatrix marMatrix::operator*(double o)
346 marMatrix result(*this);
348 if ((result._size1==0) || (result._size2==0))
354 size_t iMax=result._size1*result._size2;
356 for (i=0;i < iMax;i++)
364 marMatrix marMatrix::operator*(const marMatrix &o)
373 marMatrix result(mR,nR);
385 result(i,j)+=(*this)(i,k)*o(k,j);
397 marVector marMatrix::operator*(const marVector &o)
399 marVector result(_size1);
400 if (o.size()!=_size2)
406 marMatrix resultM(_size1,1);
407 marMatrix B((double*)o,o.size(),1);
409 result=(double*)resultM;
414 //////////////////////////////////////////////////////////////////////
416 //////////////////////////////////////////////////////////////////////
417 size_t marMatrix::rows() const
422 size_t marMatrix::columns() const