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

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

Corrections divers + RansomSequence - Reza 10/4/2000

File size: 7.8 KB
Line 
1// $Id: tmatrix.cc,v 1.5 2000-04-10 12:57:44 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
14template <class T>
15TMatrix<T>::TMatrix()
16// Constructeur par defaut.
17 : TArray<T>()
18{
19 ck_memo_vt_ = true;
20}
21
22template <class T>
23TMatrix<T>::TMatrix(uint_4 r,uint_4 c, short mm)
24// Construit une matrice de r lignes et c colonnes.
25 : TArray<T>()
26{
27 if ( (r == 0) || (c == 0) )
28 throw ParmError("TMatrix<T>::TMatrix(uint_4 r,uint_4 c) NRows or NCols = 0");
29 ReSize(r, c, mm);
30}
31
32template <class T>
33TMatrix<T>::TMatrix(const TMatrix<T>& a)
34// Constructeur par copie (partage si "a" temporaire).
35 : TArray<T>(a)
36{
37}
38
39template <class T>
40TMatrix<T>::TMatrix(const TMatrix<T>& a, bool share)
41// Constructeur par copie avec possibilite de forcer le partage ou non.
42: TArray<T>(a, share)
43{
44}
45
46
47template <class T>
48TMatrix<T>::TMatrix(const TArray<T>& a)
49: TArray<T>(a)
50{
51 if (a.NbDimensions() > 2)
52 throw SzMismatchError("TMatrix<T>::TMatrix(const TArray<T>& a) a.NbDimensions()>2 ");
53 if (a.NbDimensions() == 1) {
54 size_[1] = 1;
55 step_[1] = size_[0]*step_[0];
56 ndim_ = 2;
57 }
58 UpdateMemoryMapping(a, SameMemoryMapping);
59}
60
61template <class T>
62TMatrix<T>::TMatrix(const TArray<T>& a, bool share, short mm )
63: TArray<T>(a, share)
64{
65 if (a.NbDimensions() > 2)
66 throw SzMismatchError("TMatrix<T>::TMatrix(const TArray<T>& a, ...) a.NbDimensions()>2");
67 if (a.NbDimensions() == 1) {
68 size_[1] = 1;
69 step_[1] = size_[0]*step_[0];
70 ndim_ = 2;
71 }
72 UpdateMemoryMapping(a, mm);
73}
74
75template <class T>
76TMatrix<T>::~TMatrix()
77// Destructeur
78{
79
80}
81
82template <class T>
83TArray<T>& TMatrix<T>::Set(const TArray<T>& a)
84{
85 if (a.NbDimensions() > 2)
86 throw SzMismatchError("TMatrix<T>::Set(const TArray<T>& a) a.NbDimensions() > 2");
87 TArray<T>::Set(a);
88 if (a.NbDimensions() == 1) {
89 size_[1] = 1;
90 step_[1] = size_[0]*step_[0];
91 ndim_ = 2;
92 }
93 UpdateMemoryMapping(a, SameMemoryMapping);
94 return(*this);
95}
96
97template <class T>
98void TMatrix<T>::ReSize(uint_4 r, uint_4 c, short mm)
99{
100 if(r==0||c==0)
101 throw(SzMismatchError("TMatrix::ReSize r or c==0 "));
102 uint_4 size[BASEARRAY_MAXNDIMS];
103 for(int kk=0; kk<BASEARRAY_MAXNDIMS; kk++) size[kk] = 0;
104 if (mm == SameMemoryMapping) mm = GetMemoryMapping();
105 else if ( (mm != CMemoryMapping) && (mm != FortranMemoryMapping) )
106 mm = GetDefaultMemoryMapping();
107 if (mm == CMemoryMapping) {
108 size[0] = c; size[1] = r;
109 }
110 else {
111 size[0] = r; size[1] = c;
112 }
113 TArray<T>::ReSize(2, size, 1);
114 UpdateMemoryMapping(mm);
115}
116
117template <class T>
118void TMatrix<T>::Realloc(uint_4 r,uint_4 c, short mm, bool force)
119{
120 if(r==0||c==0)
121 throw(SzMismatchError("TMatrix::Realloc r or c==0 "));
122 uint_4 size[BASEARRAY_MAXNDIMS];
123 for(int kk=0; kk<BASEARRAY_MAXNDIMS; kk++) size[kk] = 0;
124 if (mm == SameMemoryMapping) mm = GetMemoryMapping();
125 else if ( (mm != CMemoryMapping) && (mm != FortranMemoryMapping) )
126 mm = GetDefaultMemoryMapping();
127 if (mm == CMemoryMapping) {
128 size[0] = c; size[1] = r;
129 }
130 else {
131 size[0] = r; size[1] = c;
132 }
133 TArray<T>::Realloc(2, size, 1, force);
134 UpdateMemoryMapping(mm);
135}
136
137// $CHECK$ Reza 03/2000 Doit-on declarer cette methode const ?
138template <class T>
139TMatrix<T> TMatrix<T>::SubMatrix(Range rline, Range rcol) const
140{
141 short mm = GetMemoryMapping();
142 Range rx, ry;
143 if (mm == CMemoryMapping) { rx = rcol; ry = rline; }
144 else { ry = rcol; rx = rline; }
145 TMatrix sm(SubArray(rx, ry, Range(0), Range(0), Range(0)),true, mm);
146 sm.UpdateMemoryMapping(mm);
147 sm.SetTemp(true);
148 return(sm);
149}
150
151////////////////////////////////////////////////////////////////
152// Transposition
153template <class T>
154TMatrix<T>& TMatrix<T>::Transpose()
155{
156 short vt = (marowi_ == veceli_) ? ColumnVector : RowVector;
157 uint_4 rci = macoli_;
158 macoli_ = marowi_;
159 marowi_ = rci;
160 veceli_ = (vt == ColumnVector ) ? marowi_ : macoli_;
161 return(*this);
162}
163
164
165template <class T>
166TMatrix<T> TMatrix<T>::Transpose(short mm)
167{
168 if (mm == SameMemoryMapping) mm = GetMemoryMapping();
169 TMatrix<T> tm(NCols(), NRows(), mm);
170 for(uint_4 i=0; i<NRows(); i++)
171 for(uint_4 j=0; j<NCols(); j++)
172 tm(j,i) = (*this)(i,j);
173 tm.SetTemp(true);
174 return tm;
175}
176
177// Rearrangement memoire
178// Si identique, renvoie une matrice qui partage les donnees
179template <class T>
180TMatrix<T> TMatrix<T>::Rearrange(short mm)
181{
182 if ( mm == SameMemoryMapping) mm = GetMemoryMapping();
183 else if ( (mm != CMemoryMapping) && (mm != FortranMemoryMapping) )
184 mm = GetDefaultMemoryMapping();
185
186 if (mm == GetMemoryMapping())
187 return (TMatrix<T>(*this, true));
188
189 TMatrix<T> tm(NRows(), NCols(), mm);
190 for(uint_4 i=0; i<NRows(); i++)
191 for(uint_4 j=0; j<NCols(); j++)
192 tm(i,j) = (*this)(i,j);
193 tm.SetTemp(true);
194 return tm;
195}
196
197template <class T>
198TMatrix<T>& TMatrix<T>::SetIdentity(IdentityMatrix imx)
199{
200 if (ndim_ == 0) {
201 uint_4 sz = imx.Size();
202 if (sz < 1) sz = 1;
203 ReSize(sz, sz);
204 }
205 T diag = (T)imx.Diag();
206 if (NRows() != NCols())
207 throw SzMismatchError("TMatrix::operator= (IdentityMatrix) NRows() != NCols()") ;
208 for(uint_4 i=0; i<NRows(); i++) (*this)(i,i) = diag;
209
210 return (*this);
211}
212
213
214
215////////////////////////////////////////////////////////////////
216//**** Impression
217template <class T>
218string TMatrix<T>::InfoString() const
219{
220 string rs = "TMatrix<";
221 rs += typeid(T).name();
222 char buff[64];
223 sprintf(buff, ">(NRows=%ld, NCols=%ld)", (long)NRows(), (long)NCols());
224 rs += buff;
225 return(rs);
226}
227
228template <class T>
229void TMatrix<T>::Print(ostream& os, int_4 maxprt, bool si) const
230{
231 if (maxprt < 0) maxprt = max_nprt_;
232 int_4 npr = 0;
233 Show(os, si);
234 if (ndim_ < 1) return;
235 uint_4 kc,kr;
236 for(kr=0; kr<size_[marowi_]; kr++) {
237 if ( (size_[marowi_] > 1) && (size_[macoli_] > 10) ) cout << "----- Ligne Line= " << kr << endl;
238 for(kc=0; kc<size_[macoli_]; kc++) {
239 if(kc > 0) os << ", ";
240 os << (*this)(kr, kc); npr++;
241 if (npr >= maxprt) {
242 if (npr < totsize_) os << "\n .... " << endl; return;
243 }
244 }
245 os << endl;
246 }
247 os << endl;
248}
249
250////////////////////////////////////////////////////////////////
251//**** Multiplication matricielle *****
252////////////////////////////////////////////////////////////////
253
254template <class T>
255TMatrix<T> TMatrix<T>::Multiply(const TMatrix<T>& b, short mm) const
256{
257 if (NCols() != b.NRows())
258 throw(SzMismatchError("TMatrix<T>::Multiply(b) NCols() != b.NRows() ") );
259 if (mm == SameMemoryMapping) mm = GetMemoryMapping();
260 TMatrix<T> rm(NRows(), b.NCols(), mm);
261
262 const T * pea;
263 const T * peb;
264 T sum;
265 uint_4 r,c,k;
266 uint_4 stepa = Step(ColsKA());
267 uint_4 stepb = b.Step(RowsKA());
268 // Calcul de C=rm = A*B (A=*this)
269 for(r=0; r<rm.NRows(); r++) // Boucle sur les lignes de A
270 for(c=0; c<rm.NCols(); c++) { // Boucle sur les colonnes de B
271 sum = 0;
272 pea = &((*this)(r,0)); // 1er element de la ligne r de A
273 peb = &(b(0,c)); // 1er element de la colonne c de B
274 for(k=0; k<NCols(); k++) sum += pea[k*stepa]*peb[k*stepb];
275 rm(r,c) = sum;
276 }
277
278 rm.SetTemp(true);
279 return rm;
280}
281
282///////////////////////////////////////////////////////////////
283#ifdef __CXX_PRAGMA_TEMPLATES__
284#pragma define_template TMatrix<uint_2>
285#pragma define_template TMatrix<int_4>
286#pragma define_template TMatrix<int_8>
287#pragma define_template TMatrix<r_4>
288#pragma define_template TMatrix<r_8>
289#pragma define_template TMatrix< complex<r_4> >
290#pragma define_template TMatrix< complex<r_8> >
291#endif
292
293#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
294template class TMatrix<uint_2>;
295template class TMatrix<int_4>;
296template class TMatrix<int_8>;
297template class TMatrix<r_4>;
298template class TMatrix<r_8>;
299template class TMatrix< complex<r_4> >;
300template class TMatrix< complex<r_8> >;
301#endif
Note: See TracBrowser for help on using the repository browser.