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

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

Suite operations entre matrices de <> MemOrg, Amelioration des Sequences - Reza 27/7/2000

File size: 11.4 KB
RevLine 
[1103]1// $Id: tmatrix.cc,v 1.15 2000-07-27 00:00:09 ansari Exp $
[762]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
[926]9/*!
10 \class SOPHYA::TMatrix
11 \ingroup TArray
12 Class of matrixes
13 \sa TArray
14 */
[804]15
[762]16////////////////////////////////////////////////////////////////
17//**** Createur, Destructeur
[894]18//! Default constructor
[762]19template <class T>
20TMatrix<T>::TMatrix()
21// Constructeur par defaut.
[804]22 : TArray<T>()
[762]23{
[1099]24 arrtype_ = 1; // Type = Matrix
[762]25}
26
[894]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 */
[762]34template <class T>
[804]35TMatrix<T>::TMatrix(uint_4 r,uint_4 c, short mm)
[762]36// Construit une matrice de r lignes et c colonnes.
[804]37 : TArray<T>()
[762]38{
[804]39 if ( (r == 0) || (c == 0) )
40 throw ParmError("TMatrix<T>::TMatrix(uint_4 r,uint_4 c) NRows or NCols = 0");
[1099]41 arrtype_ = 1; // Type = Matrix
[804]42 ReSize(r, c, mm);
[762]43}
44
[967]45//! Constructor by copy
[976]46/*!
47 \warning datas are \b SHARED with \b a.
48 \sa NDataBlock::NDataBlock(const NDataBlock<T>&)
49*/
[762]50template <class T>
51TMatrix<T>::TMatrix(const TMatrix<T>& a)
[967]52// Constructeur par copie
[804]53 : TArray<T>(a)
[762]54{
[1099]55 arrtype_ = 1; // Type = Matrix
[1103]56 UpdateMemoryMapping(a, SameMemoryMapping);
[762]57}
58
[894]59//! Constructor by copy
60/*!
61 \param share : if true, share data. If false copy data
62 */
[762]63template <class T>
[804]64TMatrix<T>::TMatrix(const TMatrix<T>& a, bool share)
[762]65// Constructeur par copie avec possibilite de forcer le partage ou non.
[804]66: TArray<T>(a, share)
[762]67{
[1099]68 arrtype_ = 1; // Type = Matrix
[1103]69 UpdateMemoryMapping(a, SameMemoryMapping);
[762]70}
71
[894]72//! Constructor of a matrix from a TArray \b a
[762]73template <class T>
[804]74TMatrix<T>::TMatrix(const TArray<T>& a)
75: TArray<T>(a)
[762]76{
[813]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 }
[1099]84 arrtype_ = 1; // Type = Matrix
[813]85 UpdateMemoryMapping(a, SameMemoryMapping);
[762]86}
87
[894]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 */
[762]94template <class T>
[804]95TMatrix<T>::TMatrix(const TArray<T>& a, bool share, short mm )
96: TArray<T>(a, share)
[762]97{
[813]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 }
[1099]105 arrtype_ = 1; // Type = Matrix
[804]106 UpdateMemoryMapping(a, mm);
[762]107}
108
[1099]109//! Constructor of a matrix from a TArray \b a , with a different data type
[1081]110template <class T>
111TMatrix<T>::TMatrix(const BaseArray& a)
112: TArray<T>()
113{
[1099]114 arrtype_ = 1; // Type = Matrix
[1103]115 UpdateMemoryMapping(a, SameMemoryMapping);
[1081]116 SetBA(a);
117}
118
119
120
[894]121//! Destructor
[762]122template <class T>
[804]123TMatrix<T>::~TMatrix()
[762]124{
125}
126
[976]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*/
[804]132template <class T>
133TArray<T>& TMatrix<T>::Set(const TArray<T>& a)
[762]134{
[813]135 if (a.NbDimensions() > 2)
136 throw SzMismatchError("TMatrix<T>::Set(const TArray<T>& a) a.NbDimensions() > 2");
[1099]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");
[813]139 TArray<T>::Set(a);
[970]140 if (NbDimensions() == 1) {
[813]141 size_[1] = 1;
142 step_[1] = size_[0]*step_[0];
143 ndim_ = 2;
144 }
[970]145 UpdateMemoryMapping(*this, SameMemoryMapping);
[813]146 return(*this);
[762]147}
148
[1081]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");
[1099]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");
[1081]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
[894]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 */
[804]176template <class T>
177void TMatrix<T>::ReSize(uint_4 r, uint_4 c, short mm)
[762]178{
[804]179 if(r==0||c==0)
180 throw(SzMismatchError("TMatrix::ReSize r or c==0 "));
[1099]181 if ((arrtype_ == 2) && (r > 1) && (c > 1))
182 throw(SzMismatchError("TMatrix::ReSize r>1&&c>1 for Vector "));
[804]183 uint_4 size[BASEARRAY_MAXNDIMS];
184 for(int kk=0; kk<BASEARRAY_MAXNDIMS; kk++) size[kk] = 0;
185 if (mm == SameMemoryMapping) mm = GetMemoryMapping();
[813]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 }
[804]194 TArray<T>::ReSize(2, size, 1);
[813]195 UpdateMemoryMapping(mm);
[762]196}
197
[894]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 */
[762]207template <class T>
[804]208void TMatrix<T>::Realloc(uint_4 r,uint_4 c, short mm, bool force)
[762]209{
[804]210 if(r==0||c==0)
211 throw(SzMismatchError("TMatrix::Realloc r or c==0 "));
[1099]212 if ((arrtype_ == 2) && (r > 1) && (c > 1))
213 throw(SzMismatchError("TMatrix::Realloc r>1&&c>1 for Vector "));
[804]214 uint_4 size[BASEARRAY_MAXNDIMS];
215 for(int kk=0; kk<BASEARRAY_MAXNDIMS; kk++) size[kk] = 0;
[813]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 }
[804]225 TArray<T>::Realloc(2, size, 1, force);
[813]226 UpdateMemoryMapping(mm);
[762]227}
228
[804]229// $CHECK$ Reza 03/2000 Doit-on declarer cette methode const ?
[894]230//! Return a submatrix define by \b Range \b rline and \b rcol
[762]231template <class T>
[813]232TMatrix<T> TMatrix<T>::SubMatrix(Range rline, Range rcol) const
[762]233{
[813]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);
[762]242}
243
[804]244////////////////////////////////////////////////////////////////
245// Transposition
[894]246//! Transpose matrix in place
[762]247template <class T>
[804]248TMatrix<T>& TMatrix<T>::Transpose()
249{
[813]250 short vt = (marowi_ == veceli_) ? ColumnVector : RowVector;
[804]251 uint_4 rci = macoli_;
252 macoli_ = marowi_;
253 marowi_ = rci;
[813]254 veceli_ = (vt == ColumnVector ) ? marowi_ : macoli_;
[804]255 return(*this);
[762]256}
257
258
[894]259//! Transpose matrix into new matrix
260/*!
261 \param mm : define the memory mapping type
262 (SameMemoryMapping,CMemoryMapping,FortranMemoryMapping)
263 \return return a new matrix
264 */
[762]265template <class T>
[804]266TMatrix<T> TMatrix<T>::Transpose(short mm)
[762]267{
[804]268 if (mm == SameMemoryMapping) mm = GetMemoryMapping();
269 TMatrix<T> tm(NCols(), NRows(), mm);
270 for(uint_4 i=0; i<NRows(); i++)
271 for(uint_4 j=0; j<NCols(); j++)
272 tm(j,i) = (*this)(i,j);
273 tm.SetTemp(true);
274 return tm;
[762]275}
276
[894]277//! Rearrange data in memory memoire according to \b mm
278/*!
279 \param mm : define the memory mapping type
280 (SameMemoryMapping,CMemoryMapping,FortranMemoryMapping)
281 \warning If identical, return a matrix that share the datas
282 */
[762]283template <class T>
[804]284TMatrix<T> TMatrix<T>::Rearrange(short mm)
[762]285{
[813]286 if ( mm == SameMemoryMapping) mm = GetMemoryMapping();
287 else if ( (mm != CMemoryMapping) && (mm != FortranMemoryMapping) )
288 mm = GetDefaultMemoryMapping();
289
290 if (mm == GetMemoryMapping())
291 return (TMatrix<T>(*this, true));
292
[804]293 TMatrix<T> tm(NRows(), NCols(), mm);
294 for(uint_4 i=0; i<NRows(); i++)
295 for(uint_4 j=0; j<NCols(); j++)
296 tm(i,j) = (*this)(i,j);
297 tm.SetTemp(true);
298 return tm;
[762]299}
300
[894]301//! Set the matrix to the identity matrix \b imx
[762]302template <class T>
[804]303TMatrix<T>& TMatrix<T>::SetIdentity(IdentityMatrix imx)
[762]304{
[804]305 if (ndim_ == 0) {
306 uint_4 sz = imx.Size();
307 if (sz < 1) sz = 1;
308 ReSize(sz, sz);
309 }
310 T diag = (T)imx.Diag();
311 if (NRows() != NCols())
312 throw SzMismatchError("TMatrix::operator= (IdentityMatrix) NRows() != NCols()") ;
[996]313 *this = (T) 0;
[804]314 for(uint_4 i=0; i<NRows(); i++) (*this)(i,i) = diag;
[762]315
[804]316 return (*this);
[762]317}
318
[804]319
320
321////////////////////////////////////////////////////////////////
322//**** Impression
[894]323//! Return info on number of rows, column and type \b T
[762]324template <class T>
[813]325string TMatrix<T>::InfoString() const
326{
327 string rs = "TMatrix<";
328 rs += typeid(T).name();
329 char buff[64];
330 sprintf(buff, ">(NRows=%ld, NCols=%ld)", (long)NRows(), (long)NCols());
331 rs += buff;
332 return(rs);
333}
334
[894]335//! Print matrix
336/*!
337 \param maxprt : maximum numer of print
338 \param si : if true, display attached DvList
339 \sa SetMaxPrint
340 */
[813]341template <class T>
[804]342void TMatrix<T>::Print(ostream& os, int_4 maxprt, bool si) const
[762]343{
[804]344 if (maxprt < 0) maxprt = max_nprt_;
[958]345 uint_4 npr = 0;
[804]346 Show(os, si);
[850]347 if (ndim_ < 1) return;
[804]348 uint_4 kc,kr;
349 for(kr=0; kr<size_[marowi_]; kr++) {
350 if ( (size_[marowi_] > 1) && (size_[macoli_] > 10) ) cout << "----- Ligne Line= " << kr << endl;
351 for(kc=0; kc<size_[macoli_]; kc++) {
352 if(kc > 0) os << ", ";
353 os << (*this)(kr, kc); npr++;
[958]354 if (npr >= (uint_4) maxprt) {
[804]355 if (npr < totsize_) os << "\n .... " << endl; return;
356 }
357 }
358 os << endl;
359 }
[813]360 os << endl;
[762]361}
362
363////////////////////////////////////////////////////////////////
[804]364//**** Multiplication matricielle *****
[762]365////////////////////////////////////////////////////////////////
366
[894]367//! Return the matrix product C = (*this)*B
368/*!
369 \param mm : define the memory mapping type for the return matrix
370 */
[804]371template <class T>
372TMatrix<T> TMatrix<T>::Multiply(const TMatrix<T>& b, short mm) const
373{
374 if (NCols() != b.NRows())
375 throw(SzMismatchError("TMatrix<T>::Multiply(b) NCols() != b.NRows() ") );
376 if (mm == SameMemoryMapping) mm = GetMemoryMapping();
377 TMatrix<T> rm(NRows(), b.NCols(), mm);
[762]378
[804]379 const T * pea;
380 const T * peb;
381 T sum;
382 uint_4 r,c,k;
383 uint_4 stepa = Step(ColsKA());
384 uint_4 stepb = b.Step(RowsKA());
385 // Calcul de C=rm = A*B (A=*this)
386 for(r=0; r<rm.NRows(); r++) // Boucle sur les lignes de A
387 for(c=0; c<rm.NCols(); c++) { // Boucle sur les colonnes de B
388 sum = 0;
389 pea = &((*this)(r,0)); // 1er element de la ligne r de A
390 peb = &(b(0,c)); // 1er element de la colonne c de B
391 for(k=0; k<NCols(); k++) sum += pea[k*stepa]*peb[k*stepb];
392 rm(r,c) = sum;
393 }
394
395 rm.SetTemp(true);
396 return rm;
397}
398
[762]399///////////////////////////////////////////////////////////////
400#ifdef __CXX_PRAGMA_TEMPLATES__
401#pragma define_template TMatrix<uint_2>
402#pragma define_template TMatrix<int_4>
403#pragma define_template TMatrix<int_8>
404#pragma define_template TMatrix<r_4>
[804]405#pragma define_template TMatrix<r_8>
[762]406#pragma define_template TMatrix< complex<r_4> >
407#pragma define_template TMatrix< complex<r_8> >
408#endif
409
410#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
411template class TMatrix<uint_2>;
412template class TMatrix<int_4>;
413template class TMatrix<int_8>;
414template class TMatrix<r_4>;
415template class TMatrix<r_8>;
416template class TMatrix< complex<r_4> >;
417template class TMatrix< complex<r_8> >;
418#endif
Note: See TracBrowser for help on using the repository browser.