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