1 /*# ---------------------------------------------------------------------
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 # This software is governed by the CeCILL-B license under French law and
10 # abiding by the rules of distribution of free software. You can use,
11 # modify and/ or redistribute the software under the terms of the CeCILL-B
12 # license as circulated by CEA, CNRS and INRIA at the following URL
13 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 # or in the file LICENSE.txt.
16 # As a counterpart to the access to the source code and rights to copy,
17 # modify and redistribute granted by the license, users are provided only
18 # with a limited warranty and the software's author, the holder of the
19 # economic rights, and the successive licensors have only limited
22 # The fact that you are presently reading this means that you have had
23 # knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
26 // marMatrix.cpp: implementation of the marMatrix class.
28 //////////////////////////////////////////////////////////////////////
30 #include "marMatrix.h"
33 //////////////////////////////////////////////////////////////////////
35 //////////////////////////////////////////////////////////////////////
36 marMatrix::marMatrix(size_t size1, size_t size2)
41 _data=new double[_size1*_size2];
44 marMatrix::marMatrix(double *data, size_t size1, size_t size2)
47 if (size1==0 || size2==0)
57 // PS -> size_t iMax=_size1*_size2;
58 // PS -> _data=new double[iMax];
60 // PS -> for (int i=0;i<iMax;i++)
62 // PS -> _data[i]=data[i];
69 marMatrix::marMatrix(const marMatrix &m)
72 if (m._size1==0 || m._size2==0)
81 size_t iMax=_size1*_size2;
82 _data=new double[iMax];
92 //////////////////////////////////////////////////////////////////////
94 //////////////////////////////////////////////////////////////////////
95 marMatrix::~marMatrix()
107 //////////////////////////////////////////////////////////////////////
109 //////////////////////////////////////////////////////////////////////
110 // ---------------------------------------------------------------------
112 std::ostream& operator<<(std::ostream& os,const marMatrix& m)
115 for (i =0;i<m._size1;i++)
117 for (j =0;j<m._size2;j++)
119 os << " " << m._data[i*m._size2+j];
126 // ---------------------------------------------------------------------
128 marMatrix::operator double*() const
133 // ---------------------------------------------------------------------
135 double& marMatrix::operator()(size_t i, size_t j)
137 return(_data[i*_size2+j]);
140 const double& marMatrix::operator()(size_t i, size_t j) const
142 return(_data[i*_size2+j]);
145 // ---------------------------------------------------------------------
147 marMatrix& marMatrix::operator=(const marMatrix& o)
151 if (!(_data==NULL)&&(!shallowCopy))
156 size_t iMax=_size1*_size2;
157 _data=new double[iMax];
167 marMatrix& marMatrix::operator=(double o)
169 if (_size1!=0 && _size2!=0)
171 if (!(_data==NULL)&&(!shallowCopy))
176 size_t iMax=_size1*_size2;
177 _data=new double[iMax];
178 for (int i=0;i<iMax;i++)
190 marMatrix& marMatrix::operator=(double*o)
192 if (_size1!=0 && _size2!=0)
194 if (!(_data==NULL)&&(!shallowCopy))
199 size_t iMax=_size1*_size2;
200 _data=new double[iMax];
201 for (int i=0;i<iMax;i++)
213 // ---------------------------------------------------------------------
215 bool marMatrix::operator==(const marMatrix& o) const
219 if(_size1!=o._size1 || _size2!=o._size2)
223 size_t iMax=_size1*_size2;
225 for (int i=0;i<iMax && equal;i++)
227 equal=equal && (_data[i]==o._data[i]);
232 bool marMatrix::operator!=(const marMatrix& o) const
234 return(!((*this)==o));
237 // ---------------------------------------------------------------------
239 marMatrix marMatrix::operator+(const marMatrix& o)
241 marMatrix result(*this);
243 if ((o._size1!=result._size1)
244 || (result._size1==0)
245 || (o._size2!=result._size2)
246 || (result._size2==0))
252 size_t iMax=result._size1*result._size2;
254 for (i=0 ; i < iMax ; i++)
256 result._data[i]+=o._data[i];
262 marMatrix marMatrix::operator+(double o)
264 marMatrix result(*this);
266 if ((result._size1==0) || (result._size2==0))
272 size_t iMax=result._size1*result._size2;
274 for (i=0;i < iMax;i++)
282 marMatrix marMatrix::operator+(double*o)
284 marMatrix result(*this);
286 if ((result._size1==0) || (result._size2==0))
292 size_t iMax=result._size1*result._size2;
294 for (i=0;i < iMax;i++)
296 result._data[i]+=o[i];
302 // ---------------------------------------------------------------------
304 marMatrix marMatrix::operator-(const marMatrix& o)
306 marMatrix result(*this);
308 if ((o._size1!=result._size1)
309 || (result._size1==0)
310 || (o._size2!=result._size2)
311 || (result._size2==0))
317 size_t iMax=result._size1*result._size2;
319 for (i=0;i < iMax;i++)
321 result._data[i]-=o._data[i];
327 marMatrix marMatrix::operator-(double o)
329 marMatrix result(*this);
331 if ((result._size1==0) || (result._size2==0))
337 size_t iMax=result._size1*result._size2;
339 for (i=0;i < iMax;i++)
347 marMatrix marMatrix::operator-(double*o)
349 marMatrix result(*this);
351 if ((result._size1==0) || (result._size2==0))
357 size_t iMax=result._size1*result._size2;
359 for (i=0;i < iMax;i++)
361 result._data[i]-=o[i];
367 // ---------------------------------------------------------------------
368 /*Multiplication (produit scalaire)*/
369 marMatrix marMatrix::operator*(double o)
371 marMatrix result(*this);
373 if ((result._size1==0) || (result._size2==0))
379 size_t iMax=result._size1*result._size2;
381 for (i=0;i < iMax;i++)
389 marMatrix marMatrix::operator*(const marMatrix &o)
398 marMatrix result(mR,nR);
410 result(i,j)+=(*this)(i,k)*o(k,j);
422 marVector marMatrix::operator*(const marVector &o)
424 marVector result(_size1);
425 if (o.size()!=_size2)
431 marMatrix resultM(_size1,1);
432 marMatrix B((double*)o,o.size(),1);
434 result=(double*)resultM;
439 //////////////////////////////////////////////////////////////////////
441 //////////////////////////////////////////////////////////////////////
442 size_t marMatrix::rows() const
447 size_t marMatrix::columns() const