1 | // C.Magneville, R.Ansari 03/2000
|
---|
2 |
|
---|
3 | #include "machdefs.h"
|
---|
4 | #include <stdio.h>
|
---|
5 | #include <iostream.h>
|
---|
6 | #include <complex>
|
---|
7 | #include <math.h>
|
---|
8 | #include "sopemtx.h"
|
---|
9 | #include "smathconst.h"
|
---|
10 |
|
---|
11 | ////////////////////////////////////////////////////////////////
|
---|
12 | // -------------------------------------------------------------
|
---|
13 | // La classe de gestion des lignes et colonnes d'une matrice
|
---|
14 | // -------------------------------------------------------------
|
---|
15 | ////////////////////////////////////////////////////////////////
|
---|
16 |
|
---|
17 | template <class T> TMatrixRC<T>::TMatrixRC()
|
---|
18 | : matrix(NULL), data(NULL), index(0), step(0)
|
---|
19 | {}
|
---|
20 |
|
---|
21 | template <class T> TMatrixRC<T>::TMatrixRC(TMatrix<T>& m,TRCKind rckind,uint_4 ind)
|
---|
22 | : matrix(&m), data(Org(m,rckind,ind)),
|
---|
23 | index(ind), step(Step(m,rckind)), kind(rckind)
|
---|
24 | {
|
---|
25 | if (kind == TmatrixDiag && m.NCols() != m.NRows())
|
---|
26 | throw(SzMismatchError("TMatrixRC::TMatrixRC(...,TmatrixDiag,...) size mismatch\n"));
|
---|
27 | }
|
---|
28 |
|
---|
29 | ////////////////////////////////////////////////////////////////
|
---|
30 | // Acces aux rangees et colonnes de matrices
|
---|
31 |
|
---|
32 | template <class T>
|
---|
33 | TMatrixRC<T> TMatrixRC<T>::Row(TMatrix<T> & m, uint_4 r)
|
---|
34 | {
|
---|
35 | TMatrixRC<T> rc(m, TmatrixRow, r);
|
---|
36 | return rc;
|
---|
37 | }
|
---|
38 |
|
---|
39 | template <class T>
|
---|
40 | TMatrixRC<T> TMatrixRC<T>::Col(TMatrix<T> & m, uint_4 c)
|
---|
41 | {
|
---|
42 | TMatrixRC<T> rc(m, TmatrixCol, c);
|
---|
43 | return rc;
|
---|
44 | }
|
---|
45 |
|
---|
46 | template <class T>
|
---|
47 | TMatrixRC<T> TMatrixRC<T>::Diag(TMatrix<T> & m)
|
---|
48 | {
|
---|
49 | TMatrixRC<T> rc(m, TmatrixDiag);
|
---|
50 | return rc;
|
---|
51 | }
|
---|
52 |
|
---|
53 |
|
---|
54 | template <class T> int_4 TMatrixRC<T>::Next()
|
---|
55 | {
|
---|
56 | if (!matrix || kind==TmatrixDiag) return -1;
|
---|
57 | index++;
|
---|
58 | if(kind == TmatrixRow) {
|
---|
59 | if(index > (int_4)matrix->NRows()) {index = (int_4)matrix->NRows(); return -1;}
|
---|
60 | data += matrix->NCols();
|
---|
61 | } else {
|
---|
62 | if (index > (int_4)matrix->NCols()) {index = (int_4)matrix->NCols(); return -1;}
|
---|
63 | data++;
|
---|
64 | }
|
---|
65 | return index;
|
---|
66 | }
|
---|
67 |
|
---|
68 | template <class T> int_4 TMatrixRC<T>::Prev()
|
---|
69 | {
|
---|
70 | if (!matrix || kind == TmatrixDiag) return -1;
|
---|
71 | index--;
|
---|
72 | if(index < 0) {index = 0; return -1;}
|
---|
73 | if(kind == TmatrixRow) data -= matrix->NCols();
|
---|
74 | else data--;
|
---|
75 | return index;
|
---|
76 | }
|
---|
77 |
|
---|
78 | template <class T> int_4 TMatrixRC<T>::SetCol(int_4 c)
|
---|
79 | {
|
---|
80 | if(!matrix) return -1;
|
---|
81 | if(c<0 || c>(int_4)matrix->NCols()) return -1;
|
---|
82 | kind = TmatrixCol;
|
---|
83 | index = c;
|
---|
84 | step = Step(*matrix, TmatrixCol);
|
---|
85 | data = Org(*matrix, TmatrixCol, c);
|
---|
86 | return c;
|
---|
87 | }
|
---|
88 |
|
---|
89 | template <class T> int_4 TMatrixRC<T>::SetRow(int_4 r)
|
---|
90 | {
|
---|
91 | if(!matrix) return -1;
|
---|
92 | if(r<0 && r>(int_4)matrix->NRows()) return -1;
|
---|
93 | kind = TmatrixRow;
|
---|
94 | index = r;
|
---|
95 | step = Step(*matrix, TmatrixRow);
|
---|
96 | data = Org(*matrix, TmatrixRow, r);
|
---|
97 | return r;
|
---|
98 | }
|
---|
99 |
|
---|
100 | template <class T> int_4 TMatrixRC<T>::SetDiag()
|
---|
101 | {
|
---|
102 | if (!matrix) return -1;
|
---|
103 | if (matrix->NCols() != matrix->NRows())
|
---|
104 | throw(SzMismatchError("TMatrixRC::SetDiag size mismatch\n"));
|
---|
105 | kind = TmatrixDiag;
|
---|
106 | index = 0;
|
---|
107 | step = Step(*matrix, TmatrixDiag);
|
---|
108 | data = Org(*matrix, TmatrixDiag);
|
---|
109 | return 0;
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 | template <class T> TMatrixRC<T>& TMatrixRC<T>::operator = (const TMatrixRC<T>& rc)
|
---|
114 | {
|
---|
115 | matrix = rc.matrix;
|
---|
116 | data = rc.data;
|
---|
117 | index = rc.index;
|
---|
118 | step = rc.step;
|
---|
119 | kind = rc.kind;
|
---|
120 | return *this;
|
---|
121 | }
|
---|
122 |
|
---|
123 | template <class T> TVector<T> TMatrixRC<T>::GetVect() const
|
---|
124 | {
|
---|
125 | TVector<T> v(NElts());
|
---|
126 | for (uint_4 i=0; i<NElts(); i++) v(i) = (*this)(i);
|
---|
127 | return v;
|
---|
128 | }
|
---|
129 |
|
---|
130 | template <class T> TMatrixRC<T>& TMatrixRC<T>::operator += (const TMatrixRC<T>& rc)
|
---|
131 | {
|
---|
132 | if ( NElts() != rc.NElts() )
|
---|
133 | throw(SzMismatchError("TMatrixRC::operator+= size mismatch\n"));
|
---|
134 | if ( kind != rc.kind )
|
---|
135 | throw(SzMismatchError("TMatrixRC::operator+= type mismatch\n"));
|
---|
136 | for (uint_4 i=0; i<NElts(); i++) (*this)(i) += rc(i);
|
---|
137 | return *this;
|
---|
138 | }
|
---|
139 |
|
---|
140 | template <class T> TMatrixRC<T>& TMatrixRC<T>::operator -= (const TMatrixRC<T>& rc)
|
---|
141 | {
|
---|
142 | if( NElts() != rc.NElts() )
|
---|
143 | throw(SzMismatchError("TMatrixRC::operator-= size mismatch\n"));
|
---|
144 | if( kind != rc.kind )
|
---|
145 | throw(SzMismatchError("TMatrixRC::operator-= type mismatch\n"));
|
---|
146 | for(uint_4 i=0; i<NElts(); i++) (*this)(i) -= rc(i);
|
---|
147 | return *this;
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | template <class T> TMatrixRC<T>& TMatrixRC<T>::operator *= (T x)
|
---|
152 | {
|
---|
153 | for(uint_4 i=0; i<NElts(); i++) (*this)(i) *= x;
|
---|
154 | return *this;
|
---|
155 | }
|
---|
156 |
|
---|
157 | template <class T> TMatrixRC<T>& TMatrixRC<T>::operator /= (T x)
|
---|
158 | {
|
---|
159 | for(uint_4 i=0; i<NElts(); i++) (*this)(i) /= x;
|
---|
160 | return *this;
|
---|
161 | }
|
---|
162 |
|
---|
163 |
|
---|
164 | template <class T> TMatrixRC<T>& TMatrixRC<T>::operator -= (T x)
|
---|
165 | {
|
---|
166 | for(uint_4 i=0; i<NElts(); i++) (*this)(i) -= x;
|
---|
167 | return *this;
|
---|
168 | }
|
---|
169 |
|
---|
170 | template <class T> TMatrixRC<T>& TMatrixRC<T>::operator += (T x)
|
---|
171 | {
|
---|
172 | for(uint_4 i=0; i<NElts(); i++) (*this)(i) += x;
|
---|
173 | return *this;
|
---|
174 | }
|
---|
175 |
|
---|
176 | template <class T>
|
---|
177 | TMatrixRC<T>& TMatrixRC<T>::LinComb(T a, T b, const TMatrixRC<T>& rc, uint_4 first)
|
---|
178 | {
|
---|
179 | if ( NElts() != rc.NElts() )
|
---|
180 | throw(SzMismatchError("TMatrixRC::LinComb size mismatch\n"));
|
---|
181 | if ( kind != rc.kind )
|
---|
182 | throw(SzMismatchError("TMatrixRC::LinComb type mismatch\n"));
|
---|
183 | for(uint_4 i=first; i<NElts(); i++) (*this)(i) = (*this)(i)*a + rc(i)*b;
|
---|
184 | return *this;
|
---|
185 | }
|
---|
186 |
|
---|
187 | template <class T>
|
---|
188 | TMatrixRC<T>& TMatrixRC<T>::LinComb(T b, const TMatrixRC<T>& rc, uint_4 first)
|
---|
189 | {
|
---|
190 | if ( NElts() != rc.NElts() )
|
---|
191 | throw(SzMismatchError("TMatrixRC::LinComb size mismatch\n"));
|
---|
192 | if ( kind != rc.kind )
|
---|
193 | throw(SzMismatchError("TMatrixRC::LinComb type mismatch\n"));
|
---|
194 | for(uint_4 i=first; i<NElts(); i++) (*this)(i) += rc(i)*b;
|
---|
195 | return *this;
|
---|
196 | }
|
---|
197 |
|
---|
198 | template <class T> uint_4 TMatrixRC<T>::IMaxAbs(uint_4 first)
|
---|
199 | {
|
---|
200 | if (first>NElts())
|
---|
201 | throw(SzMismatchError("TMatrixRC::IMaxAbs size mismatch\n"));
|
---|
202 | uint_4 imax = first;
|
---|
203 | double vmax = Abs_Value((*this)(first));
|
---|
204 | for(uint_4 i=first+1; i<NElts(); i++) {
|
---|
205 | double v = Abs_Value((*this)(i));
|
---|
206 | if(v > vmax) {vmax = v; imax = i;}
|
---|
207 | }
|
---|
208 | return imax;
|
---|
209 | }
|
---|
210 |
|
---|
211 | template <class T>
|
---|
212 | void TMatrixRC<T>::Swap(TMatrixRC<T>& rc1, TMatrixRC<T>& rc2)
|
---|
213 | {
|
---|
214 | if(rc1.NElts() != rc2.NElts())
|
---|
215 | throw(SzMismatchError("TMatrixRC::Swap size mismatch\n"));
|
---|
216 | if(rc1.kind != rc2.kind)
|
---|
217 | throw(SzMismatchError("TMatrixRC::Swap type mismatch\n"));
|
---|
218 | if(rc1.data == rc2.data) return;
|
---|
219 | for(uint_4 i=0; i<rc1.NElts(); i++)
|
---|
220 | {T tmp = rc1(i); rc1(i) = rc2(i); rc2(i) = tmp;}
|
---|
221 | }
|
---|
222 |
|
---|
223 |
|
---|
224 |
|
---|
225 |
|
---|
226 | ////////////////////////////////////////////////////////////////
|
---|
227 | //**** Pour inversion
|
---|
228 | #ifndef M_LN2
|
---|
229 | #define M_LN2 0.69314718055994530942
|
---|
230 | #endif
|
---|
231 | #ifndef LN_MINDOUBLE
|
---|
232 | #define LN_MINDOUBLE (M_LN2 * (DMINEXP - 1))
|
---|
233 | #endif
|
---|
234 | #ifndef LN_MAXDOUBLE
|
---|
235 | #define LN_MAXDOUBLE (M_LN2 * DMAXEXP)
|
---|
236 | #endif
|
---|
237 |
|
---|
238 | template <class T>
|
---|
239 | T SimpleMatrixOperation<T>::GausPiv(TMatrix<T>& a, TMatrix<T>& b)
|
---|
240 | // Pivot de Gauss
|
---|
241 | // * Attention: egcs impose que cette fonction soit mise dans le .cc
|
---|
242 | // avant ::Inverse() (car Inverse() l'utilise)
|
---|
243 | // {TMatrix A(a); TMatrix B(b); return (T) TMatrix::GausPiv(A,B);}
|
---|
244 | {
|
---|
245 | uint_4 n = a.NRows();
|
---|
246 | if(n!=b.NRows())
|
---|
247 | throw(SzMismatchError("TMatrix::GausPiv size mismatch\n"));
|
---|
248 | // On fait une normalisation un peu brutale...
|
---|
249 | double vmin=MAXDOUBLE;
|
---|
250 | double vmax=0;
|
---|
251 | for(uint_4 iii=0; iii<a.NRows(); iii++)
|
---|
252 | for(uint_4 jjj=0; jjj<a.NCols(); jjj++) {
|
---|
253 | double v = TMatrixRC<T>::Abs_Value(a(iii,jjj));
|
---|
254 | if(v>vmax) vmax = v;
|
---|
255 | if(v<vmin && v>0) vmin = v;
|
---|
256 | }
|
---|
257 | double nrm = sqrt(vmin*vmax);
|
---|
258 | if(nrm > 1.e5 || nrm < 1.e-5) {
|
---|
259 | a /= nrm;
|
---|
260 | b /= nrm;
|
---|
261 | //cout << "normalisation matrice " << nrm << endl;
|
---|
262 | } else nrm=1;
|
---|
263 |
|
---|
264 | double det = 1.0;
|
---|
265 | if(nrm != 1) {
|
---|
266 | double ld = a.NRows() * log(nrm);
|
---|
267 | if (ld <= LN_MINDOUBLE || ld >= LN_MAXDOUBLE) {
|
---|
268 | // cerr << "TMatrix warning, overflow for det" << endl;
|
---|
269 | } else {
|
---|
270 | det = exp(ld);
|
---|
271 | }
|
---|
272 | }
|
---|
273 |
|
---|
274 | TMatrixRC<T> pivRowa(a,TmatrixRow);
|
---|
275 | TMatrixRC<T> pivRowb(b,TmatrixRow);
|
---|
276 |
|
---|
277 | for(uint_4 k=0; k<n-1; k++) {
|
---|
278 | uint_4 iPiv = TMatrixRC<T>::Col(a, k).IMaxAbs(k);
|
---|
279 | if(iPiv != k) {
|
---|
280 | TMatrixRC<T> aIPiv(TMatrixRC<T>::Row(a,iPiv));
|
---|
281 | TMatrixRC<T> aK(TMatrixRC<T>::Row(a, k));
|
---|
282 | TMatrixRC<T>::Swap(aIPiv,aK);
|
---|
283 | TMatrixRC<T> bIPiv(TMatrixRC<T>::Row(b, iPiv));
|
---|
284 | TMatrixRC<T> bK(TMatrixRC<T>::Row(b, k));
|
---|
285 | TMatrixRC<T>::Swap(bIPiv,bK);
|
---|
286 | }
|
---|
287 | double pivot = a(k,k);
|
---|
288 | if (fabs(pivot) < 1.e-50) return 0.0;
|
---|
289 | //det *= pivot;
|
---|
290 | pivRowa.SetRow(k); // to avoid constructors
|
---|
291 | pivRowb.SetRow(k);
|
---|
292 | for (uint_4 i=k+1; i<n; i++) {
|
---|
293 | double r = -a(i,k)/pivot;
|
---|
294 | TMatrixRC<T>::Row(a, i).LinComb(r, pivRowa); // + rapide que -= r * pivRowa
|
---|
295 | TMatrixRC<T>::Row(b, i).LinComb(r, pivRowb);
|
---|
296 | }
|
---|
297 | }
|
---|
298 | det *= a(n-1, n-1);
|
---|
299 |
|
---|
300 | // on remonte
|
---|
301 | for(uint_4 kk=n-1; kk>0; kk--) {
|
---|
302 | double pivot = a(kk,kk);
|
---|
303 | if (fabs(pivot) <= 1.e-50) return 0.0;
|
---|
304 | pivRowa.SetRow(kk); // to avoid constructors
|
---|
305 | pivRowb.SetRow(kk);
|
---|
306 | for(uint_4 jj=0; jj<kk; jj++) {
|
---|
307 | double r = -a(jj,kk)/pivot;
|
---|
308 | TMatrixRC<T>::Row(a, jj).LinComb(r, pivRowa);
|
---|
309 | TMatrixRC<T>::Row(b, jj).LinComb(r, pivRowb);
|
---|
310 | }
|
---|
311 | }
|
---|
312 |
|
---|
313 | for(uint_4 l=0; l<n; l++) {
|
---|
314 | if (fabs(a(l,l)) <= 1.e-50) return 0.0;
|
---|
315 | TMatrixRC<T>::Row(b, l) /= a(l,l);
|
---|
316 | }
|
---|
317 |
|
---|
318 | return det;
|
---|
319 | }
|
---|
320 |
|
---|
321 | template <class T>
|
---|
322 | TMatrix<T> SimpleMatrixOperation<T>::Inverse(TMatrix<T> const & A)
|
---|
323 | // Inversion
|
---|
324 | {
|
---|
325 | TMatrix<T> a(A);
|
---|
326 | TMatrix<T> b(a.NCols(),a.NRows()); b = 1.;
|
---|
327 | if(fabs(GausPiv(a,b)) < 1.e-50)
|
---|
328 | throw(MathExc("TMatrix Inverse() Singular OMatrix"));
|
---|
329 | return b;
|
---|
330 | }
|
---|
331 |
|
---|
332 |
|
---|
333 |
|
---|
334 | ///////////////////////////////////////////////////////////////
|
---|
335 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
336 | // Instances gestion lignes/colonnes
|
---|
337 | #pragma define_template TMatrixRC<uint_1>
|
---|
338 | #pragma define_template TMatrixRC<uint_2>
|
---|
339 | #pragma define_template TMatrixRC<int_2>
|
---|
340 | #pragma define_template TMatrixRC<int_4>
|
---|
341 | #pragma define_template TMatrixRC<int_8>
|
---|
342 | #pragma define_template TMatrixRC<uint_4>
|
---|
343 | #pragma define_template TMatrixRC<uint_8>
|
---|
344 | #pragma define_template TMatrixRC<r_4>
|
---|
345 | #pragma define_template TMatrixRC<r_8>
|
---|
346 | #pragma define_template TMatrixRC< complex<r_4> >
|
---|
347 | #pragma define_template TMatrixRC< complex<r_8> >
|
---|
348 | // #pragma define_template SimpleMatrixOperation<r_4>
|
---|
349 | #pragma define_template SimpleMatrixOperation<r_8>
|
---|
350 | #endif
|
---|
351 |
|
---|
352 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
353 | // Instances gestion lignes/colonnes
|
---|
354 | template class TMatrixRC<uint_1>;
|
---|
355 | template class TMatrixRC<uint_2>;
|
---|
356 | template class TMatrixRC<int_2>;
|
---|
357 | template class TMatrixRC<int_4>;
|
---|
358 | template class TMatrixRC<int_8>;
|
---|
359 | template class TMatrixRC<uint_4>;
|
---|
360 | template class TMatrixRC<uint_8>;
|
---|
361 | template class TMatrixRC<r_4>;
|
---|
362 | template class TMatrixRC<r_8>;
|
---|
363 | template class TMatrixRC< complex<r_4> >;
|
---|
364 | template class TMatrixRC< complex<r_8> >;
|
---|
365 | template class SimpleMatrixOperation<r_4>;
|
---|
366 | template class SimpleMatrixOperation<r_8>;
|
---|
367 | #endif
|
---|