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