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