]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/kernel/include/marMatrix.cpp
Support #1768 CREATIS Licence insertion
[creaMaracasVisu.git] / lib / maracasVisuLib / src / kernel / include / marMatrix.cpp
1 /*# ---------------------------------------------------------------------
2 #
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
4 #                        pour la Sant�)
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
8 #
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.
15 #
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
20 #  liability.
21 #
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 # ------------------------------------------------------------------------ */
25
26 // marMatrix.cpp: implementation of the marMatrix class.
27 //
28 //////////////////////////////////////////////////////////////////////
29
30 #include "marMatrix.h"
31 #include <assert.h>
32
33 //////////////////////////////////////////////////////////////////////
34 // Construction
35 //////////////////////////////////////////////////////////////////////
36 marMatrix::marMatrix(size_t size1, size_t size2)
37         :shallowCopy(false)
38 {
39         _size1=size1;
40         _size2=size2;
41         _data=new double[_size1*_size2];
42 }
43
44 marMatrix::marMatrix(double *data, size_t size1, size_t size2)
45         :shallowCopy(true)
46 {
47         if (size1==0 || size2==0)
48         {
49                 assert(false);
50         }
51         else
52         {
53                 _size1=size1;
54                 _size2=size2;
55                 _data=data;
56
57 // PS ->                size_t iMax=_size1*_size2;
58 // PS ->                _data=new double[iMax];
59 // PS -> 
60 // PS ->                for (int i=0;i<iMax;i++)
61 // PS ->                {
62 // PS ->                        _data[i]=data[i];
63 // PS ->                }
64
65         }
66 }
67
68
69 marMatrix::marMatrix(const marMatrix &m)
70         :shallowCopy(false)
71 {
72         if (m._size1==0 || m._size2==0)
73         {
74                 assert(false);
75         }
76         else
77         {
78                 _size1=m._size1;
79                 _size2=m._size2;
80
81                 size_t iMax=_size1*_size2;
82                 _data=new double[iMax];
83
84                 int i;
85                 for (i=0;i<iMax;i++)
86                 {
87                         _data[i]=m._data[i];
88                 }
89         }
90 }
91
92 //////////////////////////////////////////////////////////////////////
93 // Destruction
94 //////////////////////////////////////////////////////////////////////
95 marMatrix::~marMatrix()
96 {
97         if (!shallowCopy) 
98         {
99                 delete _data;
100         }
101         else
102         {
103                 _data=NULL;
104         }
105 }
106
107 //////////////////////////////////////////////////////////////////////
108 // Op�rateurs
109 //////////////////////////////////////////////////////////////////////
110 // ---------------------------------------------------------------------
111 /*Affichage*/
112 std::ostream& operator<<(std::ostream& os,const marMatrix& m)
113 {
114         int i,j;
115         for (i =0;i<m._size1;i++)
116         {
117                 for (j =0;j<m._size2;j++)
118                 {
119                         os << " " << m._data[i*m._size2+j];
120                 }
121                 os << "\n";
122         }
123     return(os);
124 }
125
126 // ---------------------------------------------------------------------
127 /*Casting*/
128 marMatrix::operator double*() const 
129
130         return(_data);
131 }
132
133 // ---------------------------------------------------------------------
134 /*Indexation*/
135 double& marMatrix::operator()(size_t i, size_t j) 
136
137         return(_data[i*_size2+j]);
138 }
139
140 const double& marMatrix::operator()(size_t i, size_t j) const
141 {
142         return(_data[i*_size2+j]);
143 }
144
145 // ---------------------------------------------------------------------
146 /*Affectation*/
147 marMatrix& marMatrix::operator=(const marMatrix& o)
148 {
149         _size1=o._size1;
150         _size2=o._size2;
151         if (!(_data==NULL)&&(!shallowCopy))
152         {
153                 delete _data;
154         }
155         shallowCopy=false;
156         size_t iMax=_size1*_size2;
157         _data=new double[iMax];
158
159         int i;
160         for (i=0;i<iMax;i++)
161         {
162                 _data[i]=o._data[i];
163         }
164         return (*this);
165 }
166
167 marMatrix& marMatrix::operator=(double o)
168 {
169         if (_size1!=0 && _size2!=0)
170         {
171                 if (!(_data==NULL)&&(!shallowCopy))
172                 {
173                         delete _data;
174                 }
175                 shallowCopy=false;
176                 size_t iMax=_size1*_size2;
177                 _data=new double[iMax];
178                 for (int i=0;i<iMax;i++)
179                 {
180                         _data[i]=o;
181                 }
182         }
183         else
184         {
185                 assert(false);
186         }
187         return (*this);
188 }
189
190 marMatrix& marMatrix::operator=(double*o)
191 {
192         if (_size1!=0 && _size2!=0)
193         {
194                 if (!(_data==NULL)&&(!shallowCopy))
195                 {
196                         delete _data;
197                 }
198                 shallowCopy=false;
199                 size_t iMax=_size1*_size2;
200                 _data=new double[iMax];
201                 for (int i=0;i<iMax;i++)
202                 {
203                         _data[i]=o[i];
204                 }
205         }
206         else
207         {
208                 assert(false);
209         }
210         return (*this);
211 }
212
213 // ---------------------------------------------------------------------
214 /*Comparaison*/
215 bool marMatrix::operator==(const marMatrix& o) const
216 {
217     bool equal=true;
218         
219     if(_size1!=o._size1 || _size2!=o._size2)
220         {
221                 return(false);
222         }
223         size_t iMax=_size1*_size2;
224
225         for (int i=0;i<iMax && equal;i++)
226         {
227                 equal=equal && (_data[i]==o._data[i]); 
228         }
229     return(equal);
230 }
231
232 bool marMatrix::operator!=(const marMatrix& o) const
233 {
234         return(!((*this)==o));
235 }
236
237 // ---------------------------------------------------------------------
238 /*Addition*/
239 marMatrix marMatrix::operator+(const marMatrix& o)
240 {
241         marMatrix result(*this);
242         
243         if ((o._size1!=result._size1) 
244                 || (result._size1==0) 
245                 || (o._size2!=result._size2) 
246                 || (result._size2==0))
247         {
248                 assert(false);
249         }
250         else 
251         {
252                 size_t iMax=result._size1*result._size2;
253                 int i;
254                 for (i=0 ; i < iMax ; i++)
255                 {
256                         result._data[i]+=o._data[i];
257                 }
258         }
259         return result;
260 }
261
262 marMatrix marMatrix::operator+(double o)
263 {
264         marMatrix result(*this);
265         
266         if ((result._size1==0) || (result._size2==0))
267         {
268                 assert(false);
269         }
270         else 
271         {
272                 size_t iMax=result._size1*result._size2;
273                 int i;
274                 for (i=0;i < iMax;i++)
275                 {
276                         result._data[i]+=o;
277                 }
278         }
279         return result;
280 }
281
282 marMatrix marMatrix::operator+(double*o)
283 {
284         marMatrix result(*this);
285         
286         if ((result._size1==0) || (result._size2==0))
287         {
288                 assert(false);
289         }
290         else 
291         {
292                 size_t iMax=result._size1*result._size2;
293                 int i;
294                 for (i=0;i < iMax;i++)
295                 {
296                         result._data[i]+=o[i];
297                 }
298         }
299         return result;
300 }
301
302 // ---------------------------------------------------------------------
303 /*Soustraction*/
304 marMatrix marMatrix::operator-(const marMatrix& o)
305 {
306         marMatrix result(*this);
307         
308         if ((o._size1!=result._size1) 
309                 || (result._size1==0) 
310                 || (o._size2!=result._size2) 
311                 || (result._size2==0))
312         {
313                 assert(false);
314         }
315         else 
316         {
317                 size_t iMax=result._size1*result._size2;
318                 int i;
319                 for (i=0;i < iMax;i++)
320                 {
321                         result._data[i]-=o._data[i];
322                 }
323         }
324         return result;
325 }
326
327 marMatrix marMatrix::operator-(double o)
328 {
329         marMatrix result(*this);
330         
331         if ((result._size1==0) || (result._size2==0))
332         {
333                 assert(false);
334         }
335         else 
336         {
337                 size_t iMax=result._size1*result._size2;
338                 int i;
339                 for (i=0;i < iMax;i++)
340                 {
341                         result._data[i]-=o;
342                 }
343         }
344         return result;
345 }
346
347 marMatrix marMatrix::operator-(double*o)
348 {
349         marMatrix result(*this);
350         
351         if ((result._size1==0) || (result._size2==0))
352         {
353                 assert(false);
354         }
355         else 
356         {
357                 size_t iMax=result._size1*result._size2;
358                 int i;
359                 for (i=0;i < iMax;i++)
360                 {
361                         result._data[i]-=o[i];
362                 }
363         }
364         return result;
365 }
366
367 // ---------------------------------------------------------------------
368 /*Multiplication (produit scalaire)*/
369 marMatrix marMatrix::operator*(double o)
370 {
371         marMatrix result(*this);
372         
373         if ((result._size1==0) || (result._size2==0))
374         {
375                 assert(false);
376         }
377         else 
378         {
379                 size_t iMax=result._size1*result._size2;
380                 int i;
381                 for (i=0;i < iMax;i++)
382                 {
383                         result._data[i]*=o;
384                 }
385         }
386         return result;
387 }
388
389 marMatrix marMatrix::operator*(const marMatrix &o)
390 {
391         size_t mA=_size1;
392         size_t nA=_size2;
393         size_t mB=o._size1;
394         size_t nB=o._size2;
395         size_t mR=mA;
396         size_t nR=nB;
397
398         marMatrix result(mR,nR);
399         result=0.0;
400
401         int k,i,j;
402         if (nA==mB)
403         {
404                 for (k=0;k<nA;k++)
405                 {
406                         for (i=0;i<mR;i++)
407                         {
408                                 for (j=0;j<nR;j++)
409                                 {
410                                         result(i,j)+=(*this)(i,k)*o(k,j);
411                                 }
412                         }
413                 }
414         }
415         else
416         {
417                 assert(false);
418         }
419         return result;
420 }
421
422 marVector marMatrix::operator*(const marVector &o)
423 {
424         marVector result(_size1);
425         if (o.size()!=_size2)
426         {
427                 assert(false);
428         }
429         else
430         {
431                 marMatrix resultM(_size1,1);
432                 marMatrix B((double*)o,o.size(),1);
433                 resultM=(*this)*B;
434                 result=(double*)resultM;
435         }
436         return result;
437 }
438
439 //////////////////////////////////////////////////////////////////////
440 // M�thodes
441 //////////////////////////////////////////////////////////////////////
442 size_t marMatrix::rows() const
443 {
444     return _size1; 
445 }
446
447 size_t marMatrix::columns() const
448 {
449         return _size2;
450 }