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

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

documentation cmv 12/4/00

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