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

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

modifs doc cmv 27/4/00

File size: 10.0 KB
RevLine 
[976]1// $Id: tmatrix.cc,v 1.11 2000-04-27 17:53:51 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{
[813]24 ck_memo_vt_ = true;
[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");
41 ReSize(r, c, mm);
[762]42}
43
[967]44//! Constructor by copy
[976]45/*!
46 \warning datas are \b SHARED with \b a.
47 \sa NDataBlock::NDataBlock(const NDataBlock<T>&)
48*/
[762]49template <class T>
50TMatrix<T>::TMatrix(const TMatrix<T>& a)
[967]51// Constructeur par copie
[804]52 : TArray<T>(a)
[762]53{
54}
55
[894]56//! Constructor by copy
57/*!
58 \param share : if true, share data. If false copy data
59 */
[762]60template <class T>
[804]61TMatrix<T>::TMatrix(const TMatrix<T>& a, bool share)
[762]62// Constructeur par copie avec possibilite de forcer le partage ou non.
[804]63: TArray<T>(a, share)
[762]64{
65}
66
[894]67//! Constructor of a matrix from a TArray \b a
[762]68template <class T>
[804]69TMatrix<T>::TMatrix(const TArray<T>& a)
70: TArray<T>(a)
[762]71{
[813]72 if (a.NbDimensions() > 2)
73 throw SzMismatchError("TMatrix<T>::TMatrix(const TArray<T>& a) a.NbDimensions()>2 ");
74 if (a.NbDimensions() == 1) {
75 size_[1] = 1;
76 step_[1] = size_[0]*step_[0];
77 ndim_ = 2;
78 }
79 UpdateMemoryMapping(a, SameMemoryMapping);
[762]80}
81
[894]82//! Constructor of a matrix from a TArray \b a
83/*!
84 \param a : TArray to be copied or shared
85 \param share : if true, share data. If false copy data
86 \param mm : define the memory mapping type
87 */
[762]88template <class T>
[804]89TMatrix<T>::TMatrix(const TArray<T>& a, bool share, short mm )
90: TArray<T>(a, share)
[762]91{
[813]92 if (a.NbDimensions() > 2)
93 throw SzMismatchError("TMatrix<T>::TMatrix(const TArray<T>& a, ...) a.NbDimensions()>2");
94 if (a.NbDimensions() == 1) {
95 size_[1] = 1;
96 step_[1] = size_[0]*step_[0];
97 ndim_ = 2;
98 }
[804]99 UpdateMemoryMapping(a, mm);
[762]100}
101
[894]102//! Destructor
[762]103template <class T>
[804]104TMatrix<T>::~TMatrix()
[762]105{
106}
107
[976]108//! Set matrix equal to \b a and return *this
109/*!
110 \warning Datas are copied (cloned) from \b a.
111 \sa NDataBlock::operator=(const NDataBlock<T>&)
112*/
[804]113template <class T>
114TArray<T>& TMatrix<T>::Set(const TArray<T>& a)
[762]115{
[813]116 if (a.NbDimensions() > 2)
117 throw SzMismatchError("TMatrix<T>::Set(const TArray<T>& a) a.NbDimensions() > 2");
118 TArray<T>::Set(a);
[970]119 if (NbDimensions() == 1) {
[813]120 size_[1] = 1;
121 step_[1] = size_[0]*step_[0];
122 ndim_ = 2;
123 }
[970]124 UpdateMemoryMapping(*this, SameMemoryMapping);
[813]125 return(*this);
[762]126}
127
[894]128//! Resize the matrix
129/*!
130 \param r : number of rows
131 \param c : number of columns
132 \param mm : define the memory mapping type
133 (SameMemoryMapping,CMemoryMapping
134 ,FortranMemoryMapping,DefaultMemoryMapping)
135 */
[804]136template <class T>
137void TMatrix<T>::ReSize(uint_4 r, uint_4 c, short mm)
[762]138{
[804]139 if(r==0||c==0)
140 throw(SzMismatchError("TMatrix::ReSize r or c==0 "));
141 uint_4 size[BASEARRAY_MAXNDIMS];
142 for(int kk=0; kk<BASEARRAY_MAXNDIMS; kk++) size[kk] = 0;
143 if (mm == SameMemoryMapping) mm = GetMemoryMapping();
[813]144 else if ( (mm != CMemoryMapping) && (mm != FortranMemoryMapping) )
145 mm = GetDefaultMemoryMapping();
146 if (mm == CMemoryMapping) {
147 size[0] = c; size[1] = r;
148 }
149 else {
150 size[0] = r; size[1] = c;
151 }
[804]152 TArray<T>::ReSize(2, size, 1);
[813]153 UpdateMemoryMapping(mm);
[762]154}
155
[894]156//! Re-allocate space for the matrix
157/*!
158 \param r : number of rows
159 \param c : number of columns
160 \param mm : define the memory mapping type
161 \param force : if true re-allocation is forced, if not it occurs
162 only if the required space is greater than the old one.
163 \sa ReSize
164 */
[762]165template <class T>
[804]166void TMatrix<T>::Realloc(uint_4 r,uint_4 c, short mm, bool force)
[762]167{
[804]168 if(r==0||c==0)
169 throw(SzMismatchError("TMatrix::Realloc r or c==0 "));
170 uint_4 size[BASEARRAY_MAXNDIMS];
171 for(int kk=0; kk<BASEARRAY_MAXNDIMS; kk++) size[kk] = 0;
[813]172 if (mm == SameMemoryMapping) mm = GetMemoryMapping();
173 else if ( (mm != CMemoryMapping) && (mm != FortranMemoryMapping) )
174 mm = GetDefaultMemoryMapping();
175 if (mm == CMemoryMapping) {
176 size[0] = c; size[1] = r;
177 }
178 else {
179 size[0] = r; size[1] = c;
180 }
[804]181 TArray<T>::Realloc(2, size, 1, force);
[813]182 UpdateMemoryMapping(mm);
[762]183}
184
[804]185// $CHECK$ Reza 03/2000 Doit-on declarer cette methode const ?
[894]186//! Return a submatrix define by \b Range \b rline and \b rcol
[762]187template <class T>
[813]188TMatrix<T> TMatrix<T>::SubMatrix(Range rline, Range rcol) const
[762]189{
[813]190 short mm = GetMemoryMapping();
191 Range rx, ry;
192 if (mm == CMemoryMapping) { rx = rcol; ry = rline; }
193 else { ry = rcol; rx = rline; }
194 TMatrix sm(SubArray(rx, ry, Range(0), Range(0), Range(0)),true, mm);
195 sm.UpdateMemoryMapping(mm);
196 sm.SetTemp(true);
197 return(sm);
[762]198}
199
[804]200////////////////////////////////////////////////////////////////
201// Transposition
[894]202//! Transpose matrix in place
[762]203template <class T>
[804]204TMatrix<T>& TMatrix<T>::Transpose()
205{
[813]206 short vt = (marowi_ == veceli_) ? ColumnVector : RowVector;
[804]207 uint_4 rci = macoli_;
208 macoli_ = marowi_;
209 marowi_ = rci;
[813]210 veceli_ = (vt == ColumnVector ) ? marowi_ : macoli_;
[804]211 return(*this);
[762]212}
213
214
[894]215//! Transpose matrix into new matrix
216/*!
217 \param mm : define the memory mapping type
218 (SameMemoryMapping,CMemoryMapping,FortranMemoryMapping)
219 \return return a new matrix
220 */
[762]221template <class T>
[804]222TMatrix<T> TMatrix<T>::Transpose(short mm)
[762]223{
[804]224 if (mm == SameMemoryMapping) mm = GetMemoryMapping();
225 TMatrix<T> tm(NCols(), NRows(), mm);
226 for(uint_4 i=0; i<NRows(); i++)
227 for(uint_4 j=0; j<NCols(); j++)
228 tm(j,i) = (*this)(i,j);
229 tm.SetTemp(true);
230 return tm;
[762]231}
232
[894]233//! Rearrange data in memory memoire according to \b mm
234/*!
235 \param mm : define the memory mapping type
236 (SameMemoryMapping,CMemoryMapping,FortranMemoryMapping)
237 \warning If identical, return a matrix that share the datas
238 */
[762]239template <class T>
[804]240TMatrix<T> TMatrix<T>::Rearrange(short mm)
[762]241{
[813]242 if ( mm == SameMemoryMapping) mm = GetMemoryMapping();
243 else if ( (mm != CMemoryMapping) && (mm != FortranMemoryMapping) )
244 mm = GetDefaultMemoryMapping();
245
246 if (mm == GetMemoryMapping())
247 return (TMatrix<T>(*this, true));
248
[804]249 TMatrix<T> tm(NRows(), NCols(), mm);
250 for(uint_4 i=0; i<NRows(); i++)
251 for(uint_4 j=0; j<NCols(); j++)
252 tm(i,j) = (*this)(i,j);
253 tm.SetTemp(true);
254 return tm;
[762]255}
256
[894]257//! Set the matrix to the identity matrix \b imx
[762]258template <class T>
[804]259TMatrix<T>& TMatrix<T>::SetIdentity(IdentityMatrix imx)
[762]260{
[804]261 if (ndim_ == 0) {
262 uint_4 sz = imx.Size();
263 if (sz < 1) sz = 1;
264 ReSize(sz, sz);
265 }
266 T diag = (T)imx.Diag();
267 if (NRows() != NCols())
268 throw SzMismatchError("TMatrix::operator= (IdentityMatrix) NRows() != NCols()") ;
269 for(uint_4 i=0; i<NRows(); i++) (*this)(i,i) = diag;
[762]270
[804]271 return (*this);
[762]272}
273
[804]274
275
276////////////////////////////////////////////////////////////////
277//**** Impression
[894]278//! Return info on number of rows, column and type \b T
[762]279template <class T>
[813]280string TMatrix<T>::InfoString() const
281{
282 string rs = "TMatrix<";
283 rs += typeid(T).name();
284 char buff[64];
285 sprintf(buff, ">(NRows=%ld, NCols=%ld)", (long)NRows(), (long)NCols());
286 rs += buff;
287 return(rs);
288}
289
[894]290//! Print matrix
291/*!
292 \param maxprt : maximum numer of print
293 \param si : if true, display attached DvList
294 \sa SetMaxPrint
295 */
[813]296template <class T>
[804]297void TMatrix<T>::Print(ostream& os, int_4 maxprt, bool si) const
[762]298{
[804]299 if (maxprt < 0) maxprt = max_nprt_;
[958]300 uint_4 npr = 0;
[804]301 Show(os, si);
[850]302 if (ndim_ < 1) return;
[804]303 uint_4 kc,kr;
304 for(kr=0; kr<size_[marowi_]; kr++) {
305 if ( (size_[marowi_] > 1) && (size_[macoli_] > 10) ) cout << "----- Ligne Line= " << kr << endl;
306 for(kc=0; kc<size_[macoli_]; kc++) {
307 if(kc > 0) os << ", ";
308 os << (*this)(kr, kc); npr++;
[958]309 if (npr >= (uint_4) maxprt) {
[804]310 if (npr < totsize_) os << "\n .... " << endl; return;
311 }
312 }
313 os << endl;
314 }
[813]315 os << endl;
[762]316}
317
318////////////////////////////////////////////////////////////////
[804]319//**** Multiplication matricielle *****
[762]320////////////////////////////////////////////////////////////////
321
[894]322//! Return the matrix product C = (*this)*B
323/*!
324 \param mm : define the memory mapping type for the return matrix
325 */
[804]326template <class T>
327TMatrix<T> TMatrix<T>::Multiply(const TMatrix<T>& b, short mm) const
328{
329 if (NCols() != b.NRows())
330 throw(SzMismatchError("TMatrix<T>::Multiply(b) NCols() != b.NRows() ") );
331 if (mm == SameMemoryMapping) mm = GetMemoryMapping();
332 TMatrix<T> rm(NRows(), b.NCols(), mm);
[762]333
[804]334 const T * pea;
335 const T * peb;
336 T sum;
337 uint_4 r,c,k;
338 uint_4 stepa = Step(ColsKA());
339 uint_4 stepb = b.Step(RowsKA());
340 // Calcul de C=rm = A*B (A=*this)
341 for(r=0; r<rm.NRows(); r++) // Boucle sur les lignes de A
342 for(c=0; c<rm.NCols(); c++) { // Boucle sur les colonnes de B
343 sum = 0;
344 pea = &((*this)(r,0)); // 1er element de la ligne r de A
345 peb = &(b(0,c)); // 1er element de la colonne c de B
346 for(k=0; k<NCols(); k++) sum += pea[k*stepa]*peb[k*stepb];
347 rm(r,c) = sum;
348 }
349
350 rm.SetTemp(true);
351 return rm;
352}
353
[762]354///////////////////////////////////////////////////////////////
355#ifdef __CXX_PRAGMA_TEMPLATES__
356#pragma define_template TMatrix<uint_2>
357#pragma define_template TMatrix<int_4>
358#pragma define_template TMatrix<int_8>
359#pragma define_template TMatrix<r_4>
[804]360#pragma define_template TMatrix<r_8>
[762]361#pragma define_template TMatrix< complex<r_4> >
362#pragma define_template TMatrix< complex<r_8> >
363#endif
364
365#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
366template class TMatrix<uint_2>;
367template class TMatrix<int_4>;
368template class TMatrix<int_8>;
369template class TMatrix<r_4>;
370template class TMatrix<r_8>;
371template class TMatrix< complex<r_4> >;
372template class TMatrix< complex<r_8> >;
373#endif
Note: See TracBrowser for help on using the repository browser.