1 | // $Id: tmatrix.cc,v 1.9 1999-05-19 10:17:41 ansari Exp $
|
---|
2 | // C.Magneville 04/99
|
---|
3 | #include "machdefs.h"
|
---|
4 | #include <stdio.h>
|
---|
5 | #include <stdlib.h>
|
---|
6 | #include <iostream.h>
|
---|
7 | #include <values.h>
|
---|
8 | #include <complex>
|
---|
9 | #include "pexceptions.h"
|
---|
10 | #include "tmatrix.h"
|
---|
11 | #include "objfio.h"
|
---|
12 |
|
---|
13 | using namespace PlanckDPC;
|
---|
14 |
|
---|
15 | ////////////////////////////////////////////////////////////////
|
---|
16 | //**** Createur, Destructeur
|
---|
17 |
|
---|
18 | template <class T>
|
---|
19 | TMatrix<T>::TMatrix()
|
---|
20 | // Constructeur par defaut.
|
---|
21 | : mNr(0), mNc(0), mNDBlock()
|
---|
22 | {
|
---|
23 | }
|
---|
24 |
|
---|
25 | template <class T>
|
---|
26 | TMatrix<T>::TMatrix(uint_4 r,uint_4 c)
|
---|
27 | // Construit une matrice de r lignes et c colonnes.
|
---|
28 | : mNr(r), mNc(c), mNDBlock(r*c)
|
---|
29 | {
|
---|
30 | }
|
---|
31 |
|
---|
32 | template <class T>
|
---|
33 | TMatrix<T>::TMatrix(uint_4 r,uint_4 c,T* values,Bridge* br)
|
---|
34 | // Construit une matrice de r lignes et c colonnes. On fournit
|
---|
35 | // le tableau des valeurs et eventuellement un Bridge.
|
---|
36 | : mNr(r), mNc(c), mNDBlock(r*c,values,br)
|
---|
37 | {
|
---|
38 | }
|
---|
39 |
|
---|
40 | template <class T>
|
---|
41 | TMatrix<T>::TMatrix(const TMatrix<T>& a)
|
---|
42 | // Constructeur par copie (partage si "a" temporaire).
|
---|
43 | : mNr(a.mNr), mNc(a.mNc), mNDBlock(a.mNDBlock)
|
---|
44 | {
|
---|
45 | }
|
---|
46 |
|
---|
47 | template <class T>
|
---|
48 | TMatrix<T>::TMatrix(const TMatrix<T>& a,bool share)
|
---|
49 | // Constructeur par copie avec possibilite de forcer le partage ou non.
|
---|
50 | : mNr(a.mNr), mNc(a.mNc), mNDBlock(a.mNDBlock,share)
|
---|
51 | {
|
---|
52 | }
|
---|
53 |
|
---|
54 | template <class T>
|
---|
55 | TMatrix<T>::~TMatrix()
|
---|
56 | // Destructeur
|
---|
57 | {
|
---|
58 | }
|
---|
59 |
|
---|
60 | ////////////////////////////////////////////////////////////////
|
---|
61 | // Operations matricielles
|
---|
62 | template <class T>
|
---|
63 | TMatrix<T> TMatrix<T>::Transpose(void) const
|
---|
64 | // Transposition
|
---|
65 | {
|
---|
66 | TMatrix<T> a; a.Clone(*this); a.SetTemp(true);
|
---|
67 | a.mNr = mNc; a.mNc = mNr;
|
---|
68 | {for(uint_4 i=0; i<mNr; i++)
|
---|
69 | for(uint_4 j=0; j<mNc; j++) {
|
---|
70 | a(j,i) = (*this)(i,j);
|
---|
71 | }}
|
---|
72 | return a;
|
---|
73 | }
|
---|
74 |
|
---|
75 | ////////////////////////////////////////////////////////////////
|
---|
76 | //**** Impression
|
---|
77 |
|
---|
78 | template <class T>
|
---|
79 | void TMatrix<T>::Print(ostream& os,int lp
|
---|
80 | ,uint_4 i0,uint_4 ni,uint_4 j0,uint_4 nj) const
|
---|
81 | // Impression de la sous-matrice (i:i+ni-1,i:j+nj-1)
|
---|
82 | {
|
---|
83 | os<<"TMatrix::Print("<<mNr<<","<<mNc<<")"<<endl;
|
---|
84 | if(lp>0)
|
---|
85 | {os<<" this="<<this<<endl; mNDBlock.Print(0,0);}
|
---|
86 | if(mNr==0 || mNc==0) return;
|
---|
87 | if(i0>=mNr || j0>=mNc || ni==0 || nj==0) return;
|
---|
88 | uint_4 i1 = i0+ni; if(i1>mNr) i1 = mNr;
|
---|
89 | uint_4 j1 = j0+nj; if(j1>mNc) j1 = mNc;
|
---|
90 | for(uint_4 i=i0;i<i1;i++) {
|
---|
91 | for(uint_4 j=j0;j<j1;j++) cout<<" "<<(*this)(i,j);
|
---|
92 | cout<<endl;
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | ////////////////////////////////////////////////////////////////
|
---|
97 | //**** Surcharge de *= (INPLACE): TMatrix *= TMatrix;
|
---|
98 |
|
---|
99 | template <class T>
|
---|
100 | TMatrix<T>& TMatrix<T>::operator *= (const TMatrix<T>& a)
|
---|
101 | // A = A*B -> A(n,m) = A(n,m)*B(m,m)
|
---|
102 | {
|
---|
103 | uint_4 ndata = mNr*mNc;
|
---|
104 | if(ndata==0 || mNc!=a.mNr || a.mNr!=a.mNc)
|
---|
105 | throw(SzMismatchError("TMatrix::operator*=A size mismatch"));
|
---|
106 | // A(i,j) = Sum(k) A(i,k)*B(k,j) ... il faut sauver la ligne "i" de A
|
---|
107 | // Vecteur oi : vecteur ou est sauve la ligne i de la matrice *this
|
---|
108 | // oi,oe = pointeur de debut et de fin du vecteur temporaire
|
---|
109 | // oij = pointeur parcourant le vecteur oi
|
---|
110 | // Matrice *this: i = pointeur du debut de la ligne i
|
---|
111 | // ij = pointeur parcourant la ligne i
|
---|
112 | // Matrice a : aj = pointeur de debut de la colonne j
|
---|
113 | // aji = pointeur parcourant la colonne j
|
---|
114 | T* oi = new T[mNc]; T* oe = oi+mNc;
|
---|
115 | for(T *i=Data(); i<Data()+ndata; i+=mNc) {
|
---|
116 | {for(T *oij=oi,*ij=i; oij<oe;) *oij++ = *ij++;}
|
---|
117 | {for(T *ij=i,*aj=const_cast<T *>(a.Data()); aj<a.Data()+a.mNc; ij++,aj++) {
|
---|
118 | T sum = 0;
|
---|
119 | for(T *oij=oi,*aji=aj; oij<oe; oij++,aji+=a.mNc) sum += *oij * *aji;
|
---|
120 | *ij = sum;
|
---|
121 | }}
|
---|
122 | }
|
---|
123 | delete [] oi;
|
---|
124 | return *this;
|
---|
125 | }
|
---|
126 |
|
---|
127 | ////////////////////////////////////////////////////////////////
|
---|
128 | //**** Pour surcharge d'operateurs C = A (+,-,*,/) B
|
---|
129 |
|
---|
130 | template <class T> TMatrix<T> TMatrix<T>::Add(const TMatrix<T>& b) const
|
---|
131 | {
|
---|
132 | if(mNr!=b.mNr || mNc!=b.mNc)
|
---|
133 | throw(SzMismatchError("TMatrix operator C=A+B size mismatch\n"));
|
---|
134 | TMatrix<T> result; result.SetTemp(true); result.mNr=mNr; result.mNc=mNc;
|
---|
135 | result.mNDBlock = mNDBlock+b.mNDBlock;
|
---|
136 | return result;
|
---|
137 | }
|
---|
138 |
|
---|
139 | template <class T> TMatrix<T> TMatrix<T>::Sub(const TMatrix<T>& b) const
|
---|
140 | {
|
---|
141 | if(mNr!=b.mNr || mNc!=b.mNc)
|
---|
142 | throw(SzMismatchError("TMatrix operator C=A-B size mismatch\n"));
|
---|
143 | TMatrix<T> result; result.SetTemp(true); result.mNr=mNr; result.mNc=mNc;
|
---|
144 | result.mNDBlock = mNDBlock-b.mNDBlock;
|
---|
145 | return result;
|
---|
146 | }
|
---|
147 |
|
---|
148 | template <class T> TMatrix<T> TMatrix<T>::Mul(const TMatrix<T>& b) const
|
---|
149 | // C = A(this)*B : Cij = Aik Bkj (allocation forcee dans tous les cas)
|
---|
150 | {
|
---|
151 | if(mNr==0 || mNc==0 || b.mNc==0 || mNc!=b.mNr)
|
---|
152 | throw(SzMismatchError("TMatrix operator C=A*B size mismatch\n"));
|
---|
153 | TMatrix<T> r; r.SetTemp(true); r.ReSize(mNr,b.mNc);
|
---|
154 | T *ai,*aik,*bj,*bkj,*ri,*rij;
|
---|
155 | for(ri=const_cast<T *>(r.Data()),ai=const_cast<T *>(Data());
|
---|
156 | ri<r.Data()+r.mNr*r.mNc;ri+=r.mNc,ai+=mNc) {
|
---|
157 | for(rij=ri,bj=const_cast<T *>(b.Data());rij<ri+r.mNc;rij++,bj++) {
|
---|
158 | *rij = 0;
|
---|
159 | for(aik=ai,bkj=bj;aik<ai+mNc;aik++,bkj+=b.mNc) *rij += *aik * *bkj;
|
---|
160 | }
|
---|
161 | }
|
---|
162 | return r;
|
---|
163 | }
|
---|
164 |
|
---|
165 | ////////////////////////////////////////////////////////////////
|
---|
166 | //**** Pour gestion des lignes et des colonnes
|
---|
167 |
|
---|
168 | template <class T> TMatrixRC<T> TMatrix<T>::Row(uint_4 r) const
|
---|
169 | {
|
---|
170 | TMatrixRC<T> rc((TMatrix<T>&)*this, TmatrixRow, r);
|
---|
171 | return rc;
|
---|
172 | }
|
---|
173 |
|
---|
174 | template <class T> TMatrixRC<T> TMatrix<T>::Col(uint_4 c) const
|
---|
175 | {
|
---|
176 | TMatrixRC<T> rc((TMatrix<T>&)*this, TmatrixCol, c);
|
---|
177 | return rc;
|
---|
178 | }
|
---|
179 |
|
---|
180 | template <class T> TMatrixRC<T> TMatrix<T>::Diag() const
|
---|
181 | {
|
---|
182 | TMatrixRC<T> rc((TMatrix<T>&)*this, TmatrixDiag);
|
---|
183 | return rc;
|
---|
184 | }
|
---|
185 |
|
---|
186 | #include "matrix.h"
|
---|
187 | ////////////////////////////////////////////////////////////////
|
---|
188 | //**** Pour inversion
|
---|
189 | #ifndef M_LN2
|
---|
190 | #define M_LN2 0.69314718055994530942
|
---|
191 | #endif
|
---|
192 | #ifndef LN_MINDOUBLE
|
---|
193 | #define LN_MINDOUBLE (M_LN2 * (DMINEXP - 1))
|
---|
194 | #endif
|
---|
195 | #ifndef LN_MAXDOUBLE
|
---|
196 | #define LN_MAXDOUBLE (M_LN2 * DMAXEXP)
|
---|
197 | #endif
|
---|
198 |
|
---|
199 | r_8 TMatrix<r_8>::GausPiv(TMatrix<r_8>& a, TMatrix<r_8>& b)
|
---|
200 | // Pivot de Gauss
|
---|
201 | // * Attention: egcs impose que cette fonction soit mise dans le .cc
|
---|
202 | // avant ::Inverse() (car Inverse() l'utilise)
|
---|
203 | // {Matrix A(a); Matrix B(b); return (r_8) Matrix::GausPiv(A,B);}
|
---|
204 | {
|
---|
205 | uint_4 n = a.NRows();
|
---|
206 | if(n!=b.NRows())
|
---|
207 | throw(SzMismatchError("TMatrix::GausPiv size mismatch\n"));
|
---|
208 | // On fait une normalisation un peu brutale...
|
---|
209 | double vmin=MAXDOUBLE;
|
---|
210 | double vmax=0;
|
---|
211 | for(uint_4 iii=0; iii<a.NRows(); iii++)
|
---|
212 | for(uint_4 jjj=0; jjj<a.NCols(); jjj++) {
|
---|
213 | double v = TMatrixRC<r_8>::Abs_Value(a(iii,jjj));
|
---|
214 | if(v>vmax) vmax = v;
|
---|
215 | if(v<vmin && v>0) vmin = v;
|
---|
216 | }
|
---|
217 | double nrm = sqrt(vmin*vmax);
|
---|
218 | if(nrm > 1.e5 || nrm < 1.e-5) {
|
---|
219 | a /= nrm;
|
---|
220 | b /= nrm;
|
---|
221 | //cout << "normalisation matrice " << nrm << endl;
|
---|
222 | } else nrm=1;
|
---|
223 |
|
---|
224 | double det = 1.0;
|
---|
225 | if(nrm != 1) {
|
---|
226 | double ld = a.NRows() * log(nrm);
|
---|
227 | if (ld <= LN_MINDOUBLE || ld >= LN_MAXDOUBLE) {
|
---|
228 | // cerr << "Matrix warning, overflow for det" << endl;
|
---|
229 | } else {
|
---|
230 | det = exp(ld);
|
---|
231 | }
|
---|
232 | }
|
---|
233 |
|
---|
234 | TMatrixRC<r_8> pivRowa(a,TmatrixRow);
|
---|
235 | TMatrixRC<r_8> pivRowb(b,TmatrixRow);
|
---|
236 |
|
---|
237 | for(uint_4 k=0; k<n-1; k++) {
|
---|
238 | uint_4 iPiv = a.Col(k).IMaxAbs(k);
|
---|
239 | if(iPiv != k) {
|
---|
240 | TMatrixRC<r_8> aIPiv(a.Row(iPiv));
|
---|
241 | TMatrixRC<r_8> aK(a.Row(k));
|
---|
242 | TMatrixRC<r_8>::Swap(aIPiv,aK);
|
---|
243 | TMatrixRC<r_8> bIPiv(b.Row(iPiv));
|
---|
244 | TMatrixRC<r_8> bK(b.Row(k));
|
---|
245 | TMatrixRC<r_8>::Swap(bIPiv,bK);
|
---|
246 | }
|
---|
247 | double pivot = a(k,k);
|
---|
248 | if (fabs(pivot) < 1.e-50) return 0.0;
|
---|
249 | //det *= pivot;
|
---|
250 | pivRowa.SetRow(k); // to avoid constructors
|
---|
251 | pivRowb.SetRow(k);
|
---|
252 | for (uint_4 i=k+1; i<n; i++) {
|
---|
253 | double r = -a(i,k)/pivot;
|
---|
254 | a.Row(i).LinComb(r, pivRowa); // + rapide que -= r * pivRowa
|
---|
255 | b.Row(i).LinComb(r, pivRowb);
|
---|
256 | }
|
---|
257 | }
|
---|
258 | det *= a(n-1, n-1);
|
---|
259 |
|
---|
260 | // on remonte
|
---|
261 | for(uint_4 kk=n-1; kk>0; kk--) {
|
---|
262 | double pivot = a(kk,kk);
|
---|
263 | if (fabs(pivot) <= 1.e-50) return 0.0;
|
---|
264 | pivRowa.SetRow(kk); // to avoid constructors
|
---|
265 | pivRowb.SetRow(kk);
|
---|
266 | for(uint_4 jj=0; jj<kk; jj++) {
|
---|
267 | double r = -a(jj,kk)/pivot;
|
---|
268 | a.Row(jj).LinComb(r, pivRowa);
|
---|
269 | b.Row(jj).LinComb(r, pivRowb);
|
---|
270 | }
|
---|
271 | }
|
---|
272 |
|
---|
273 | for(uint_4 l=0; l<n; l++) {
|
---|
274 | if (fabs(a(l,l)) <= 1.e-50) return 0.0;
|
---|
275 | b.Row(l) /= a(l,l);
|
---|
276 | }
|
---|
277 |
|
---|
278 | return det;
|
---|
279 | }
|
---|
280 |
|
---|
281 | TMatrix<r_8> TMatrix<r_8>::Inverse() const
|
---|
282 | // Inversion
|
---|
283 | {
|
---|
284 | TMatrix<r_8> b(mNc,mNr); TMatrix<r_8> a(*this); b = 1.;
|
---|
285 | if(fabs(TMatrix<r_8>::GausPiv(a,b)) < 1.e-50)
|
---|
286 | throw(MathExc("TMatrix Inverse() Singular Matrix"));
|
---|
287 | return b;
|
---|
288 | }
|
---|
289 |
|
---|
290 | #include "generalfit.h"
|
---|
291 | //////////////////////////////////////////////////////////
|
---|
292 | //**** Residus des fits
|
---|
293 | TMatrix<r_8> TMatrix<r_8>::FitResidus(GeneralFit& gfit
|
---|
294 | ,double xorg,double yorg,double dx,double dy)
|
---|
295 | // Retourne une classe contenant les residus du fit ``gfit''.
|
---|
296 | // On suppose que x=j (colonnes) et y=i (lignes) pour m(i,j).
|
---|
297 | // Les coordonnees de l'element (i,j) sont :
|
---|
298 | // (i,j) -> x = xorg+j*dx , y = yorg+i*dy
|
---|
299 | {
|
---|
300 | if(NCols()<=0||NRows()<=0)
|
---|
301 | throw(SzMismatchError("TMatrix::FitResidus size mismatch\n"));
|
---|
302 | GeneralFunction* f = gfit.GetFunction();
|
---|
303 | if(f==NULL)
|
---|
304 | throw(NullPtrError("TMatrix::FitResidus GeneraFit==NULL\n"));
|
---|
305 | int npar = gfit.GetNPar();
|
---|
306 | if(npar==0)
|
---|
307 | throw(SzMismatchError("TMatrix::FitResidus GeneraFit 0 parametre\n"));
|
---|
308 | double* par = new double[npar];
|
---|
309 | {for(int i=0;i<npar;i++) par[i] = gfit.GetParm(i);}
|
---|
310 | TMatrix<r_8> m(*this);
|
---|
311 | for(uint_4 i=0;i<NRows();i++) for(uint_4 j=0;j<NCols();j++) {
|
---|
312 | double x[2] = {xorg+j*dx,yorg+i*dy};
|
---|
313 | m(i,j) -= f->Value(x,par);
|
---|
314 | }
|
---|
315 | delete [] par;
|
---|
316 | return m;
|
---|
317 | }
|
---|
318 |
|
---|
319 | TMatrix<r_8> TMatrix<r_8>::FitFunction(GeneralFit& gfit
|
---|
320 | ,double xorg,double yorg,double dx,double dy)
|
---|
321 | // Retourne une classe contenant la fonction du fit ``gfit''.
|
---|
322 | // On suppose que x=j (colonnes) et y=i (lignes) pour m(i,j).
|
---|
323 | // Les coordonnees de l'element (i,j) sont :
|
---|
324 | // (i,j) -> x = xorg + j*dx , y = yorg + i*dy
|
---|
325 | {
|
---|
326 | if(NCols()<=0||NRows()<=0)
|
---|
327 | throw(SzMismatchError("TMatrix::FitFunction size mismatch\n"));
|
---|
328 | GeneralFunction* f = gfit.GetFunction();
|
---|
329 | if(f==NULL)
|
---|
330 | throw(NullPtrError("TMatrix::FitFunction GeneraFit==NULL\n"));
|
---|
331 | int npar = gfit.GetNPar();
|
---|
332 | if(npar==0)
|
---|
333 | throw(SzMismatchError("TMatrix::FitFunction GeneraFit 0 parametre\n"));
|
---|
334 | double* par = new double[npar];
|
---|
335 | {for(int i=0;i<npar;i++) par[i] = gfit.GetParm(i);}
|
---|
336 | TMatrix<r_8> m(*this);
|
---|
337 | for(uint_4 i=0;i<NRows();i++) for(uint_4 j=0;j<NCols();j++) {
|
---|
338 | double x[2] = {xorg+j*dx,yorg+i*dy};
|
---|
339 | m(i,j) = f->Value(x,par);
|
---|
340 | }
|
---|
341 | delete [] par;
|
---|
342 | return m;
|
---|
343 | }
|
---|
344 |
|
---|
345 | ///////////////////////////////////////////////////////////
|
---|
346 | // --------------------------------------------------------
|
---|
347 | // Les objets delegues pour la gestion de persistance
|
---|
348 | // --------------------------------------------------------
|
---|
349 | ///////////////////////////////////////////////////////////
|
---|
350 |
|
---|
351 | template <class T>
|
---|
352 | FIO_TMatrix<T>::FIO_TMatrix()
|
---|
353 | {
|
---|
354 | dobj=new TMatrix<T>;
|
---|
355 | ownobj=true;
|
---|
356 | }
|
---|
357 |
|
---|
358 | template <class T>
|
---|
359 | FIO_TMatrix<T>::FIO_TMatrix(string const & filename)
|
---|
360 | {
|
---|
361 | dobj=new TMatrix<T>;
|
---|
362 | ownobj=true;
|
---|
363 | Read(filename);
|
---|
364 | }
|
---|
365 |
|
---|
366 | template <class T>
|
---|
367 | FIO_TMatrix<T>::FIO_TMatrix(const TMatrix<T> & obj)
|
---|
368 | {
|
---|
369 | dobj = new TMatrix<T>(obj);
|
---|
370 | ownobj=true;
|
---|
371 | }
|
---|
372 |
|
---|
373 | template <class T>
|
---|
374 | FIO_TMatrix<T>::FIO_TMatrix(TMatrix<T> * obj)
|
---|
375 | {
|
---|
376 | dobj = obj;
|
---|
377 | ownobj=false;
|
---|
378 | }
|
---|
379 |
|
---|
380 | template <class T>
|
---|
381 | FIO_TMatrix<T>::~FIO_TMatrix()
|
---|
382 | {
|
---|
383 | if (ownobj && dobj) delete dobj;
|
---|
384 | }
|
---|
385 |
|
---|
386 | template <class T>
|
---|
387 | AnyDataObj* FIO_TMatrix<T>::DataObj()
|
---|
388 | {
|
---|
389 | return(dobj);
|
---|
390 | }
|
---|
391 |
|
---|
392 | template <class T>
|
---|
393 | void FIO_TMatrix<T>::ReadSelf(PInPersist& is)
|
---|
394 | {
|
---|
395 | // On lit les 3 premiers uint_4
|
---|
396 | // 0: Numero de version, 1 : NRows, 2 : NCol
|
---|
397 | uint_4 itab[3];
|
---|
398 | is.Get(itab,3);
|
---|
399 | if (dobj == NULL) dobj = new TMatrix<T>(itab[1],itab[2]);
|
---|
400 | else dobj->ReSize(itab[1],itab[2]);
|
---|
401 | // On lit le NDataBlock
|
---|
402 | FIO_NDataBlock<T> fio_nd(&dobj->DataBlock());
|
---|
403 | fio_nd.Read(is);
|
---|
404 | }
|
---|
405 |
|
---|
406 | template <class T>
|
---|
407 | void FIO_TMatrix<T>::WriteSelf(POutPersist& os) const
|
---|
408 | {
|
---|
409 | if (dobj == NULL) return;
|
---|
410 | // On ecrit 3 uint_4 ....
|
---|
411 | // 0: Numero de version, 1 : NRows, 2 : NCol
|
---|
412 | uint_4 itab[3];
|
---|
413 | itab[0] = 1; // Numero de version a 1
|
---|
414 | itab[1] = dobj->NRows();
|
---|
415 | itab[2] = dobj->NCols();
|
---|
416 | os.Put(itab,3);
|
---|
417 | // On ecrit le NDataBlock
|
---|
418 | FIO_NDataBlock<T> fio_nd(&dobj->DataBlock());
|
---|
419 | fio_nd.Write(os);
|
---|
420 | }
|
---|
421 |
|
---|
422 | ////////////////////////////////////////////////////////////////
|
---|
423 | // -------------------------------------------------------------
|
---|
424 | // La classe de gestion des lignes et colonnes d'une matrice
|
---|
425 | // -------------------------------------------------------------
|
---|
426 | ////////////////////////////////////////////////////////////////
|
---|
427 | #include "tvector.h"
|
---|
428 |
|
---|
429 | template <class T> TMatrixRC<T>::TMatrixRC()
|
---|
430 | : matrix(NULL), data(NULL), index(0), step(0)
|
---|
431 | {}
|
---|
432 |
|
---|
433 | template <class T> TMatrixRC<T>::TMatrixRC(TMatrix<T>& m,TRCKind rckind,uint_4 ind)
|
---|
434 | : matrix(&m), data(Org(m,rckind,ind)),
|
---|
435 | index(ind), step(Step(m,rckind)), kind(rckind)
|
---|
436 | {
|
---|
437 | if (kind == TmatrixDiag && m.mNc != m.mNr)
|
---|
438 | throw(SzMismatchError("TMatrixRC::TMatrixRC(...,TmatrixDiag,...) size mismatch\n"));
|
---|
439 | }
|
---|
440 |
|
---|
441 | template <class T> int_4 TMatrixRC<T>::Next()
|
---|
442 | {
|
---|
443 | if (!matrix || kind==TmatrixDiag) return -1;
|
---|
444 | index++;
|
---|
445 | if(kind == TmatrixRow) {
|
---|
446 | if(index > (int_4)matrix->mNr) {index = (int_4)matrix->mNr; return -1;}
|
---|
447 | data += matrix->mNc;
|
---|
448 | } else {
|
---|
449 | if (index > (int_4)matrix->mNc) {index = (int_4)matrix->mNc; return -1;}
|
---|
450 | data++;
|
---|
451 | }
|
---|
452 | return index;
|
---|
453 | }
|
---|
454 |
|
---|
455 | template <class T> int_4 TMatrixRC<T>::Prev()
|
---|
456 | {
|
---|
457 | if (!matrix || kind == TmatrixDiag) return -1;
|
---|
458 | index--;
|
---|
459 | if(index < 0) {index = 0; return -1;}
|
---|
460 | if(kind == TmatrixRow) data -= matrix->mNc;
|
---|
461 | else data--;
|
---|
462 | return index;
|
---|
463 | }
|
---|
464 |
|
---|
465 | template <class T> int_4 TMatrixRC<T>::SetCol(int_4 c)
|
---|
466 | {
|
---|
467 | if(!matrix) return -1;
|
---|
468 | if(c<0 || c>(int_4)matrix->mNc) return -1;
|
---|
469 | kind = TmatrixCol;
|
---|
470 | index = c;
|
---|
471 | step = Step(*matrix, TmatrixCol);
|
---|
472 | data = Org(*matrix, TmatrixCol, c);
|
---|
473 | return c;
|
---|
474 | }
|
---|
475 |
|
---|
476 | template <class T> int_4 TMatrixRC<T>::SetRow(int_4 r)
|
---|
477 | {
|
---|
478 | if(!matrix) return -1;
|
---|
479 | if(r<0 && r>(int_4)matrix->mNr) return -1;
|
---|
480 | kind = TmatrixRow;
|
---|
481 | index = r;
|
---|
482 | step = Step(*matrix, TmatrixRow);
|
---|
483 | data = Org(*matrix, TmatrixRow, r);
|
---|
484 | return r;
|
---|
485 | }
|
---|
486 |
|
---|
487 | template <class T> int_4 TMatrixRC<T>::SetDiag()
|
---|
488 | {
|
---|
489 | if (!matrix) return -1;
|
---|
490 | if (matrix->mNc != matrix->mNr)
|
---|
491 | throw(SzMismatchError("TMatrixRC::SetDiag size mismatch\n"));
|
---|
492 | kind = TmatrixDiag;
|
---|
493 | index = 0;
|
---|
494 | step = Step(*matrix, TmatrixDiag);
|
---|
495 | data = Org(*matrix, TmatrixDiag);
|
---|
496 | return 0;
|
---|
497 | }
|
---|
498 |
|
---|
499 |
|
---|
500 | template <class T> TMatrixRC<T>& TMatrixRC<T>::operator = (const TMatrixRC<T>& rc)
|
---|
501 | {
|
---|
502 | matrix = rc.matrix;
|
---|
503 | data = rc.data;
|
---|
504 | index = rc.index;
|
---|
505 | step = rc.step;
|
---|
506 | kind = rc.kind;
|
---|
507 | return *this;
|
---|
508 | }
|
---|
509 |
|
---|
510 | template <class T> TVector<T> TMatrixRC<T>::GetVect() const
|
---|
511 | {
|
---|
512 | TVector<T> v(NElts());
|
---|
513 | for (uint_4 i=0; i<NElts(); i++) v(i) = (*this)(i);
|
---|
514 | return v;
|
---|
515 | }
|
---|
516 |
|
---|
517 | template <class T> TMatrixRC<T>& TMatrixRC<T>::operator += (const TMatrixRC<T>& rc)
|
---|
518 | {
|
---|
519 | if ( NElts() != rc.NElts() )
|
---|
520 | throw(SzMismatchError("TMatrixRC::operator+= size mismatch\n"));
|
---|
521 | if ( kind != rc.kind )
|
---|
522 | throw(SzMismatchError("TMatrixRC::operator+= type mismatch\n"));
|
---|
523 | for (uint_4 i=0; i<NElts(); i++) (*this)(i) += rc(i);
|
---|
524 | return *this;
|
---|
525 | }
|
---|
526 |
|
---|
527 | template <class T> TMatrixRC<T>& TMatrixRC<T>::operator -= (const TMatrixRC<T>& rc)
|
---|
528 | {
|
---|
529 | if( NElts() != rc.NElts() )
|
---|
530 | throw(SzMismatchError("TMatrixRC::operator-= size mismatch\n"));
|
---|
531 | if( kind != rc.kind )
|
---|
532 | throw(SzMismatchError("TMatrixRC::operator-= type mismatch\n"));
|
---|
533 | for(uint_4 i=0; i<NElts(); i++) (*this)(i) -= rc(i);
|
---|
534 | return *this;
|
---|
535 | }
|
---|
536 |
|
---|
537 |
|
---|
538 | template <class T> TMatrixRC<T>& TMatrixRC<T>::operator *= (T x)
|
---|
539 | {
|
---|
540 | for(uint_4 i=0; i<NElts(); i++) (*this)(i) *= x;
|
---|
541 | return *this;
|
---|
542 | }
|
---|
543 |
|
---|
544 | template <class T> TMatrixRC<T>& TMatrixRC<T>::operator /= (T x)
|
---|
545 | {
|
---|
546 | for(uint_4 i=0; i<NElts(); i++) (*this)(i) /= x;
|
---|
547 | return *this;
|
---|
548 | }
|
---|
549 |
|
---|
550 |
|
---|
551 | template <class T> TMatrixRC<T>& TMatrixRC<T>::operator -= (T x)
|
---|
552 | {
|
---|
553 | for(uint_4 i=0; i<NElts(); i++) (*this)(i) -= x;
|
---|
554 | return *this;
|
---|
555 | }
|
---|
556 |
|
---|
557 | template <class T> TMatrixRC<T>& TMatrixRC<T>::operator += (T x)
|
---|
558 | {
|
---|
559 | for(uint_4 i=0; i<NElts(); i++) (*this)(i) += x;
|
---|
560 | return *this;
|
---|
561 | }
|
---|
562 |
|
---|
563 | template <class T>
|
---|
564 | TMatrixRC<T>& TMatrixRC<T>::LinComb(T a, T b, const TMatrixRC<T>& rc, uint_4 first)
|
---|
565 | {
|
---|
566 | if ( NElts() != rc.NElts() )
|
---|
567 | throw(SzMismatchError("TMatrixRC::LinComb size mismatch\n"));
|
---|
568 | if ( kind != rc.kind )
|
---|
569 | throw(SzMismatchError("TMatrixRC::LinComb type mismatch\n"));
|
---|
570 | for(uint_4 i=first; i<NElts(); i++) (*this)(i) = (*this)(i)*a + rc(i)*b;
|
---|
571 | return *this;
|
---|
572 | }
|
---|
573 |
|
---|
574 | template <class T>
|
---|
575 | TMatrixRC<T>& TMatrixRC<T>::LinComb(T b, const TMatrixRC<T>& rc, uint_4 first)
|
---|
576 | {
|
---|
577 | if ( NElts() != rc.NElts() )
|
---|
578 | throw(SzMismatchError("TMatrixRC::LinComb size mismatch\n"));
|
---|
579 | if ( kind != rc.kind )
|
---|
580 | throw(SzMismatchError("TMatrixRC::LinComb type mismatch\n"));
|
---|
581 | for(uint_4 i=first; i<NElts(); i++) (*this)(i) += rc(i)*b;
|
---|
582 | return *this;
|
---|
583 | }
|
---|
584 |
|
---|
585 | template <class T> uint_4 TMatrixRC<T>::IMaxAbs(uint_4 first)
|
---|
586 | {
|
---|
587 | if (first>NElts())
|
---|
588 | throw(SzMismatchError("TMatrixRC::IMaxAbs size mismatch\n"));
|
---|
589 | uint_4 imax = first;
|
---|
590 | double vmax = Abs_Value((*this)(first));
|
---|
591 | for(uint_4 i=first+1; i<NElts(); i++) {
|
---|
592 | double v = Abs_Value((*this)(i));
|
---|
593 | if(v > vmax) {vmax = v; imax = i;}
|
---|
594 | }
|
---|
595 | return imax;
|
---|
596 | }
|
---|
597 |
|
---|
598 | template <class T>
|
---|
599 | void TMatrixRC<T>::Swap(TMatrixRC<T>& rc1, TMatrixRC<T>& rc2)
|
---|
600 | {
|
---|
601 | if(rc1.NElts() != rc2.NElts())
|
---|
602 | throw(SzMismatchError("TMatrixRC::Swap size mismatch\n"));
|
---|
603 | if(rc1.kind != rc2.kind)
|
---|
604 | throw(SzMismatchError("TMatrixRC::Swap type mismatch\n"));
|
---|
605 | if(rc1.data == rc2.data) return;
|
---|
606 | for(uint_4 i=0; i<rc1.NElts(); i++)
|
---|
607 | {T tmp = rc1(i); rc1(i) = rc2(i); rc2(i) = tmp;}
|
---|
608 | }
|
---|
609 |
|
---|
610 | ///////////////////////////////////////////////////////////////
|
---|
611 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
612 | #pragma define_template TMatrix<uint_1>
|
---|
613 | #pragma define_template TMatrix<uint_2>
|
---|
614 | #pragma define_template TMatrix<int_2>
|
---|
615 | #pragma define_template TMatrix<int_4>
|
---|
616 | #pragma define_template TMatrix<int_8>
|
---|
617 | #pragma define_template TMatrix<uint_4>
|
---|
618 | #pragma define_template TMatrix<uint_8>
|
---|
619 | #pragma define_template TMatrix<r_4>
|
---|
620 | #pragma define_template TMatrix<r_8>
|
---|
621 | #pragma define_template TMatrix< complex<float> >
|
---|
622 | #pragma define_template TMatrix< complex<double> >
|
---|
623 | // Instances des delegues FileIO (PPersist)
|
---|
624 | #pragma define_template FIO_TMatrix<uint_1>
|
---|
625 | #pragma define_template FIO_TMatrix<uint_2>
|
---|
626 | #pragma define_template FIO_TMatrix<int_2>
|
---|
627 | #pragma define_template FIO_TMatrix<int_4>
|
---|
628 | #pragma define_template FIO_TMatrix<int_8>
|
---|
629 | #pragma define_template FIO_TMatrix<uint_4>
|
---|
630 | #pragma define_template FIO_TMatrix<uint_8>
|
---|
631 | #pragma define_template FIO_TMatrix<r_8>
|
---|
632 | #pragma define_template FIO_TMatrix<r_4>
|
---|
633 | #pragma define_template FIO_TMatrix< complex<float> >
|
---|
634 | #pragma define_template FIO_TMatrix< complex<double> >
|
---|
635 | // Instances gestion lignes/colonnes
|
---|
636 | #pragma define_template TMatrixRC<uint_1>
|
---|
637 | #pragma define_template TMatrixRC<uint_2>
|
---|
638 | #pragma define_template TMatrixRC<int_2>
|
---|
639 | #pragma define_template TMatrixRC<int_4>
|
---|
640 | #pragma define_template TMatrixRC<int_8>
|
---|
641 | #pragma define_template TMatrixRC<uint_4>
|
---|
642 | #pragma define_template TMatrixRC<uint_8>
|
---|
643 | #pragma define_template TMatrixRC<r_4>
|
---|
644 | #pragma define_template TMatrixRC<r_8>
|
---|
645 | #pragma define_template TMatrixRC< complex<float> >
|
---|
646 | #pragma define_template TMatrixRC< complex<double> >
|
---|
647 | #endif
|
---|
648 |
|
---|
649 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
650 | template class TMatrix<uint_1>;
|
---|
651 | template class TMatrix<uint_2>;
|
---|
652 | template class TMatrix<int_2>;
|
---|
653 | template class TMatrix<int_4>;
|
---|
654 | template class TMatrix<int_8>;
|
---|
655 | template class TMatrix<uint_4>;
|
---|
656 | template class TMatrix<uint_8>;
|
---|
657 | template class TMatrix<r_4>;
|
---|
658 | template class TMatrix<r_8>;
|
---|
659 | template class TMatrix< complex<float> >;
|
---|
660 | template class TMatrix< complex<double> >;
|
---|
661 | // Instances des delegues FileIO (PPersist)
|
---|
662 | template class FIO_TMatrix<uint_1>;
|
---|
663 | template class FIO_TMatrix<uint_2>;
|
---|
664 | template class FIO_TMatrix<int_2>;
|
---|
665 | template class FIO_TMatrix<int_4>;
|
---|
666 | template class FIO_TMatrix<int_8>;
|
---|
667 | template class FIO_TMatrix<uint_4>;
|
---|
668 | template class FIO_TMatrix<uint_8>;
|
---|
669 | template class FIO_TMatrix<r_8>;
|
---|
670 | template class FIO_TMatrix<r_4>;
|
---|
671 | template class FIO_TMatrix< complex<float> >;
|
---|
672 | template class FIO_TMatrix< complex<double> >;
|
---|
673 | // Instances gestion lignes/colonnes
|
---|
674 | template class TMatrixRC<uint_1>;
|
---|
675 | template class TMatrixRC<uint_2>;
|
---|
676 | template class TMatrixRC<int_2>;
|
---|
677 | template class TMatrixRC<int_4>;
|
---|
678 | template class TMatrixRC<int_8>;
|
---|
679 | template class TMatrixRC<uint_4>;
|
---|
680 | template class TMatrixRC<uint_8>;
|
---|
681 | template class TMatrixRC<r_4>;
|
---|
682 | template class TMatrixRC<r_8>;
|
---|
683 | template class TMatrixRC< complex<float> >;
|
---|
684 | template class TMatrixRC< complex<double> >;
|
---|
685 | #endif
|
---|