source: Sophya/trunk/SophyaLib/TArray/tmatrix.cc@ 1412

Last change on this file since 1412 was 1412, checked in by ansari, 25 years ago

Modifs sur TMatrix::Transpose() et ajout methode SetSize comme alias de ReSize ds TMatrix et TVector - Reza 20/2/2001

File size: 11.8 KB
Line 
1// $Id: tmatrix.cc,v 1.17 2001-02-20 18:29:17 ansari Exp $
2// C.Magneville 04/99
3#include "machdefs.h"
4#include <stdio.h>
5#include <stdlib.h>
6#include "pexceptions.h"
7#include "tmatrix.h"
8
9/*!
10 \class SOPHYA::TMatrix
11 \ingroup TArray
12 Class of matrixes
13 \sa TArray
14 */
15
16////////////////////////////////////////////////////////////////
17//**** Createur, Destructeur
18//! Default constructor
19template <class T>
20TMatrix<T>::TMatrix()
21// Constructeur par defaut.
22 : TArray<T>()
23{
24 arrtype_ = 1; // Type = Matrix
25}
26
27//! constructor of a matrix with r lines et c columns.
28/*!
29 \param r : number of rows
30 \param c : number of columns
31 \param mm : define the memory mapping type
32 \sa ReSize
33 */
34template <class T>
35TMatrix<T>::TMatrix(sa_size_t r,sa_size_t c, short mm)
36// Construit une matrice de r lignes et c colonnes.
37 : TArray<T>()
38{
39 if ( (r == 0) || (c == 0) )
40 throw ParmError("TMatrix<T>::TMatrix(sa_size_t r,sa_size_t c) NRows or NCols = 0");
41 arrtype_ = 1; // Type = Matrix
42 ReSize(r, c, mm);
43}
44
45//! Constructor by copy
46/*!
47 \warning datas are \b SHARED with \b a.
48 \sa NDataBlock::NDataBlock(const NDataBlock<T>&)
49*/
50template <class T>
51TMatrix<T>::TMatrix(const TMatrix<T>& a)
52// Constructeur par copie
53 : TArray<T>(a)
54{
55 arrtype_ = 1; // Type = Matrix
56 UpdateMemoryMapping(a, SameMemoryMapping);
57}
58
59//! Constructor by copy
60/*!
61 \param share : if true, share data. If false copy data
62 */
63template <class T>
64TMatrix<T>::TMatrix(const TMatrix<T>& a, bool share)
65// Constructeur par copie avec possibilite de forcer le partage ou non.
66: TArray<T>(a, share)
67{
68 arrtype_ = 1; // Type = Matrix
69 UpdateMemoryMapping(a, SameMemoryMapping);
70}
71
72//! Constructor of a matrix from a TArray \b a
73template <class T>
74TMatrix<T>::TMatrix(const TArray<T>& a)
75: TArray<T>(a)
76{
77 if (a.NbDimensions() > 2)
78 throw SzMismatchError("TMatrix<T>::TMatrix(const TArray<T>& a) a.NbDimensions()>2 ");
79 if (a.NbDimensions() == 1) {
80 size_[1] = 1;
81 step_[1] = size_[0]*step_[0];
82 ndim_ = 2;
83 }
84 arrtype_ = 1; // Type = Matrix
85 UpdateMemoryMapping(a, SameMemoryMapping);
86}
87
88//! Constructor of a matrix from a TArray \b a
89/*!
90 \param a : TArray to be copied or shared
91 \param share : if true, share data. If false copy data
92 \param mm : define the memory mapping type
93 */
94template <class T>
95TMatrix<T>::TMatrix(const TArray<T>& a, bool share, short mm )
96: TArray<T>(a, share)
97{
98 if (a.NbDimensions() > 2)
99 throw SzMismatchError("TMatrix<T>::TMatrix(const TArray<T>& a, ...) a.NbDimensions()>2");
100 if (a.NbDimensions() == 1) {
101 size_[1] = 1;
102 step_[1] = size_[0]*step_[0];
103 ndim_ = 2;
104 }
105 arrtype_ = 1; // Type = Matrix
106 UpdateMemoryMapping(a, mm);
107}
108
109//! Constructor of a matrix from a TArray \b a , with a different data type
110template <class T>
111TMatrix<T>::TMatrix(const BaseArray& a)
112: TArray<T>()
113{
114 arrtype_ = 1; // Type = Matrix
115 UpdateMemoryMapping(a, SameMemoryMapping);
116 SetBA(a);
117}
118
119
120
121//! Destructor
122template <class T>
123TMatrix<T>::~TMatrix()
124{
125}
126
127//! Set matrix equal to \b a and return *this
128/*!
129 \warning Datas are copied (cloned) from \b a.
130 \sa NDataBlock::operator=(const NDataBlock<T>&)
131*/
132template <class T>
133TArray<T>& TMatrix<T>::Set(const TArray<T>& a)
134{
135 if (a.NbDimensions() > 2)
136 throw SzMismatchError("TMatrix<T>::Set(const TArray<T>& a) a.NbDimensions() > 2");
137 if ((arrtype_ == 2) && (a.NbDimensions() > 1) && (a.Size(0) > 1) && (a.Size(1) > 1) )
138 throw SzMismatchError("TMatrix<T>::Set(const TArray<T>& a) Size(0,1)>1 for Vector");
139 TArray<T>::Set(a);
140 if (NbDimensions() == 1) {
141 size_[1] = 1;
142 step_[1] = size_[0]*step_[0];
143 ndim_ = 2;
144 }
145 UpdateMemoryMapping(*this, SameMemoryMapping);
146 return(*this);
147}
148
149template <class T>
150TArray<T>& TMatrix<T>::SetBA(const BaseArray& a)
151{
152 if (a.NbDimensions() > 2)
153 throw SzMismatchError("TMatrix<T>::SetBA(const BaseArray& a) a.NbDimensions() > 2");
154 if ((arrtype_ == 2) && (a.NbDimensions() > 1) && (a.Size(0) > 1) && (a.Size(1) > 1) )
155 throw SzMismatchError("TMatrix<T>::Set(const TArray<T>& a) Size(0,1)>1 for Vector");
156 TArray<T>::SetBA(a);
157 if (NbDimensions() == 1) {
158 size_[1] = 1;
159 step_[1] = size_[0]*step_[0];
160 ndim_ = 2;
161 }
162 UpdateMemoryMapping(*this, SameMemoryMapping);
163 return(*this);
164}
165
166
167
168//! Resize the matrix
169/*!
170 \param r : number of rows
171 \param c : number of columns
172 \param mm : define the memory mapping type
173 (SameMemoryMapping,CMemoryMapping
174 ,FortranMemoryMapping,DefaultMemoryMapping)
175 */
176template <class T>
177void TMatrix<T>::ReSize(sa_size_t r, sa_size_t c, short mm)
178{
179 if(r==0||c==0)
180 throw(SzMismatchError("TMatrix::ReSize r or c==0 "));
181 if ((arrtype_ == 2) && (r > 1) && (c > 1))
182 throw(SzMismatchError("TMatrix::ReSize r>1&&c>1 for Vector "));
183 sa_size_t size[BASEARRAY_MAXNDIMS];
184 for(int_4 kk=0; kk<BASEARRAY_MAXNDIMS; kk++) size[kk] = 0;
185 if (mm == SameMemoryMapping) mm = GetMemoryMapping();
186 else if ( (mm != CMemoryMapping) && (mm != FortranMemoryMapping) )
187 mm = GetDefaultMemoryMapping();
188 if (mm == CMemoryMapping) {
189 size[0] = c; size[1] = r;
190 }
191 else {
192 size[0] = r; size[1] = c;
193 }
194 TArray<T>::ReSize(2, size, 1);
195 UpdateMemoryMapping(mm);
196}
197
198//! Re-allocate space for the matrix
199/*!
200 \param r : number of rows
201 \param c : number of columns
202 \param mm : define the memory mapping type
203 \param force : if true re-allocation is forced, if not it occurs
204 only if the required space is greater than the old one.
205 \sa ReSize
206 */
207template <class T>
208void TMatrix<T>::Realloc(sa_size_t r,sa_size_t c, short mm, bool force)
209{
210 if(r==0||c==0)
211 throw(SzMismatchError("TMatrix::Realloc r or c==0 "));
212 if ((arrtype_ == 2) && (r > 1) && (c > 1))
213 throw(SzMismatchError("TMatrix::Realloc r>1&&c>1 for Vector "));
214 sa_size_t size[BASEARRAY_MAXNDIMS];
215 for(int_4 kk=0; kk<BASEARRAY_MAXNDIMS; kk++) size[kk] = 0;
216 if (mm == SameMemoryMapping) mm = GetMemoryMapping();
217 else if ( (mm != CMemoryMapping) && (mm != FortranMemoryMapping) )
218 mm = GetDefaultMemoryMapping();
219 if (mm == CMemoryMapping) {
220 size[0] = c; size[1] = r;
221 }
222 else {
223 size[0] = r; size[1] = c;
224 }
225 TArray<T>::Realloc(2, size, 1, force);
226 UpdateMemoryMapping(mm);
227}
228
229// $CHECK$ Reza 03/2000 Doit-on declarer cette methode const ?
230//! Return a submatrix define by \b Range \b rline and \b rcol
231template <class T>
232TMatrix<T> TMatrix<T>::SubMatrix(Range rline, Range rcol) const
233{
234 short mm = GetMemoryMapping();
235 Range rx, ry;
236 if (mm == CMemoryMapping) { rx = rcol; ry = rline; }
237 else { ry = rcol; rx = rline; }
238 TMatrix sm(SubArray(rx, ry, Range(0), Range(0), Range(0)),true, mm);
239 sm.UpdateMemoryMapping(mm);
240 sm.SetTemp(true);
241 return(sm);
242}
243
244////////////////////////////////////////////////////////////////
245// Transposition
246//! Transpose matrix in place, by changing the memory mapping
247template <class T>
248TMatrix<T>& TMatrix<T>::TransposeSelf()
249{
250 short vt = (marowi_ == veceli_) ? ColumnVector : RowVector;
251 int_4 rci = macoli_;
252 macoli_ = marowi_;
253 marowi_ = rci;
254 veceli_ = (vt == ColumnVector ) ? marowi_ : macoli_;
255 return(*this);
256}
257
258
259//! Returns the transpose of the original matrix.
260/*!
261 The data is shared between the two matrices
262 \return return a new matrix
263 */
264template <class T>
265TMatrix<T> TMatrix<T>::Transpose()
266{
267 TMatrix<T> tm(*this);
268 tm.TransposeSelf();
269 return tm;
270}
271
272//! Returns a new matrix, corresponding to the transpose of the original matrix
273/*!
274 \param mm : define the memory mapping type
275 (SameMemoryMapping,CMemoryMapping,FortranMemoryMapping)
276 \return return a new matrix
277 */
278template <class T>
279TMatrix<T> TMatrix<T>::Transpose(short mm)
280{
281 if (mm == SameMemoryMapping) mm = GetMemoryMapping();
282 TMatrix<T> tm(NCols(), NRows(), mm);
283 for(sa_size_t i=0; i<NRows(); i++)
284 for(sa_size_t j=0; j<NCols(); j++)
285 tm(j,i) = (*this)(i,j);
286 tm.SetTemp(true);
287 return tm;
288}
289
290//! Rearrange data in memory according to \b mm
291/*!
292 \param mm : define the memory mapping type
293 (SameMemoryMapping,CMemoryMapping,FortranMemoryMapping)
294 \warning If identical, return a matrix that share the datas
295 */
296template <class T>
297TMatrix<T> TMatrix<T>::Rearrange(short mm)
298{
299 if ( mm == SameMemoryMapping) mm = GetMemoryMapping();
300 else if ( (mm != CMemoryMapping) && (mm != FortranMemoryMapping) )
301 mm = GetDefaultMemoryMapping();
302
303 if (mm == GetMemoryMapping())
304 return (TMatrix<T>(*this, true));
305
306 TMatrix<T> tm(NRows(), NCols(), mm);
307 for(sa_size_t i=0; i<NRows(); i++)
308 for(sa_size_t j=0; j<NCols(); j++)
309 tm(i,j) = (*this)(i,j);
310 tm.SetTemp(true);
311 return tm;
312}
313
314//! Set the matrix to the identity matrix \b imx
315template <class T>
316TMatrix<T>& TMatrix<T>::SetIdentity(IdentityMatrix imx)
317{
318 if (ndim_ == 0) {
319 sa_size_t sz = imx.Size();
320 if (sz < 1) sz = 1;
321 ReSize(sz, sz);
322 }
323 T diag = (T)imx.Diag();
324 if (NRows() != NCols())
325 throw SzMismatchError("TMatrix::operator= (IdentityMatrix) NRows() != NCols()") ;
326 *this = (T) 0;
327 for(sa_size_t i=0; i<NRows(); i++) (*this)(i,i) = diag;
328
329 return (*this);
330}
331
332
333
334////////////////////////////////////////////////////////////////
335//**** Impression
336//! Return info on number of rows, column and type \b T
337template <class T>
338string TMatrix<T>::InfoString() const
339{
340 string rs = "TMatrix<";
341 rs += typeid(T).name();
342 char buff[64];
343 sprintf(buff, ">(NRows=%ld, NCols=%ld)", (long)NRows(), (long)NCols());
344 rs += buff;
345 return(rs);
346}
347
348//! Print matrix
349/*!
350 \param maxprt : maximum numer of print
351 \param si : if true, display attached DvList
352 \sa SetMaxPrint
353 */
354template <class T>
355void TMatrix<T>::Print(ostream& os, int_4 maxprt, bool si) const
356{
357 if (maxprt < 0) maxprt = max_nprt_;
358 sa_size_t npr = 0;
359 Show(os, si);
360 if (ndim_ < 1) return;
361 sa_size_t kc,kr;
362 for(kr=0; kr<size_[marowi_]; kr++) {
363 if ( (size_[marowi_] > 1) && (size_[macoli_] > 10) ) cout << "----- Ligne Line= " << kr << endl;
364 for(kc=0; kc<size_[macoli_]; kc++) {
365 if(kc > 0) os << ", ";
366 os << (*this)(kr, kc); npr++;
367 if (npr >= (sa_size_t) maxprt) {
368 if (npr < totsize_) os << "\n .... " << endl; return;
369 }
370 }
371 os << endl;
372 }
373 os << endl;
374}
375
376////////////////////////////////////////////////////////////////
377//**** Multiplication matricielle *****
378////////////////////////////////////////////////////////////////
379
380//! Return the matrix product C = (*this)*B
381/*!
382 \param mm : define the memory mapping type for the return matrix
383 */
384template <class T>
385TMatrix<T> TMatrix<T>::Multiply(const TMatrix<T>& b, short mm) const
386{
387 if (NCols() != b.NRows())
388 throw(SzMismatchError("TMatrix<T>::Multiply(b) NCols() != b.NRows() ") );
389 if (mm == SameMemoryMapping) mm = GetMemoryMapping();
390 TMatrix<T> rm(NRows(), b.NCols(), mm);
391
392 const T * pea;
393 const T * peb;
394 T sum;
395 sa_size_t r,c,k;
396 sa_size_t stepa = Step(ColsKA());
397 sa_size_t stepb = b.Step(RowsKA());
398 // Calcul de C=rm = A*B (A=*this)
399 for(r=0; r<rm.NRows(); r++) // Boucle sur les lignes de A
400 for(c=0; c<rm.NCols(); c++) { // Boucle sur les colonnes de B
401 sum = 0;
402 pea = &((*this)(r,0)); // 1er element de la ligne r de A
403 peb = &(b(0,c)); // 1er element de la colonne c de B
404 for(k=0; k<NCols(); k++) sum += pea[k*stepa]*peb[k*stepb];
405 rm(r,c) = sum;
406 }
407
408 rm.SetTemp(true);
409 return rm;
410}
411
412///////////////////////////////////////////////////////////////
413#ifdef __CXX_PRAGMA_TEMPLATES__
414#pragma define_template TMatrix<uint_2>
415#pragma define_template TMatrix<int_4>
416#pragma define_template TMatrix<int_8>
417#pragma define_template TMatrix<r_4>
418#pragma define_template TMatrix<r_8>
419#pragma define_template TMatrix< complex<r_4> >
420#pragma define_template TMatrix< complex<r_8> >
421#endif
422
423#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
424template class TMatrix<uint_2>;
425template class TMatrix<int_4>;
426template class TMatrix<int_8>;
427template class TMatrix<r_4>;
428template class TMatrix<r_8>;
429template class TMatrix< complex<r_4> >;
430template class TMatrix< complex<r_8> >;
431#endif
Note: See TracBrowser for help on using the repository browser.