1 | // $Id: tmatrix.cc,v 1.6 1999-05-18 12:23:13 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 <complex>
|
---|
8 | #include "pexceptions.h"
|
---|
9 | #include "tmatrix.h"
|
---|
10 | #include "objfio.h"
|
---|
11 | #include "generalfit.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 | #include "matrix.h"
|
---|
166 | ////////////////////////////////////////////////////////////////
|
---|
167 | //**** Pour inversion
|
---|
168 | TMatrix<r_8> TMatrix<r_8>::Inverse() const
|
---|
169 | // Inversion
|
---|
170 | {
|
---|
171 | TMatrix<r_8> b(mNc,mNr);
|
---|
172 | TMatrix<r_8> a(*this);
|
---|
173 | b = 1.;
|
---|
174 | if (fabs(TMatrix<r_8>::GausPiv(a,b)) < 1.e-50)
|
---|
175 | throw(MathExc("TMatrix Inverse() Singular Matrix"));
|
---|
176 | return b;
|
---|
177 | }
|
---|
178 |
|
---|
179 | double TMatrix<r_8>::GausPiv(TMatrix<r_8>& a, TMatrix<r_8>& b)
|
---|
180 | // Pivot de Gauss
|
---|
181 | {
|
---|
182 | Matrix A(a);
|
---|
183 | Matrix B(b);
|
---|
184 | return Matrix::GausPiv(A,B);
|
---|
185 | }
|
---|
186 |
|
---|
187 | //////////////////////////////////////////////////////////
|
---|
188 | //**** Residus des fits
|
---|
189 | TMatrix<r_8> TMatrix<r_8>::FitResidus(GeneralFit& gfit
|
---|
190 | ,double xorg,double yorg,double dx,double dy)
|
---|
191 | // Retourne une classe contenant les residus du fit ``gfit''.
|
---|
192 | // On suppose que x=j (colonnes) et y=i (lignes) pour m(i,j).
|
---|
193 | // Les coordonnees de l'element (i,j) sont :
|
---|
194 | // (i,j) -> x = xorg+j*dx , y = yorg+i*dy
|
---|
195 | {
|
---|
196 | if(NCols()<=0||NRows()<=0)
|
---|
197 | throw(SzMismatchError("TMatrix::FitResidus size mismatch\n"));
|
---|
198 | GeneralFunction* f = gfit.GetFunction();
|
---|
199 | if(f==NULL)
|
---|
200 | throw(NullPtrError("TMatrix::FitResidus GeneraFit==NULL\n"));
|
---|
201 | int npar = gfit.GetNPar();
|
---|
202 | if(npar==0)
|
---|
203 | throw(SzMismatchError("TMatrix::FitResidus GeneraFit 0 parametre\n"));
|
---|
204 | double* par = new double[npar];
|
---|
205 | {for(int i=0;i<npar;i++) par[i] = gfit.GetParm(i);}
|
---|
206 | TMatrix<r_8> m(*this);
|
---|
207 | for(uint_4 i=0;i<NRows();i++) for(uint_4 j=0;j<NCols();j++) {
|
---|
208 | double x[2] = {xorg+j*dx,yorg+i*dy};
|
---|
209 | m(i,j) -= f->Value(x,par);
|
---|
210 | }
|
---|
211 | delete [] par;
|
---|
212 | return m;
|
---|
213 | }
|
---|
214 |
|
---|
215 | TMatrix<r_8> TMatrix<r_8>::FitFunction(GeneralFit& gfit
|
---|
216 | ,double xorg,double yorg,double dx,double dy)
|
---|
217 | // Retourne une classe contenant la fonction du fit ``gfit''.
|
---|
218 | // On suppose que x=j (colonnes) et y=i (lignes) pour m(i,j).
|
---|
219 | // Les coordonnees de l'element (i,j) sont :
|
---|
220 | // (i,j) -> x = xorg + j*dx , y = yorg + i*dy
|
---|
221 | {
|
---|
222 | if(NCols()<=0||NRows()<=0)
|
---|
223 | throw(SzMismatchError("TMatrix::FitFunction size mismatch\n"));
|
---|
224 | GeneralFunction* f = gfit.GetFunction();
|
---|
225 | if(f==NULL)
|
---|
226 | throw(NullPtrError("TMatrix::FitFunction GeneraFit==NULL\n"));
|
---|
227 | int npar = gfit.GetNPar();
|
---|
228 | if(npar==0)
|
---|
229 | throw(SzMismatchError("TMatrix::FitFunction GeneraFit 0 parametre\n"));
|
---|
230 | double* par = new double[npar];
|
---|
231 | {for(int i=0;i<npar;i++) par[i] = gfit.GetParm(i);}
|
---|
232 | TMatrix<r_8> m(*this);
|
---|
233 | for(uint_4 i=0;i<NRows();i++) for(uint_4 j=0;j<NCols();j++) {
|
---|
234 | double x[2] = {xorg+j*dx,yorg+i*dy};
|
---|
235 | m(i,j) = f->Value(x,par);
|
---|
236 | }
|
---|
237 | delete [] par;
|
---|
238 | return m;
|
---|
239 | }
|
---|
240 |
|
---|
241 | ////////////////////////////////////////////////////////////////
|
---|
242 | // -------------------------------------------------------------------------
|
---|
243 | // Les objets delegues pour la gestion de persistance
|
---|
244 | // -------------------------------------------------------------------------
|
---|
245 |
|
---|
246 | template <class T>
|
---|
247 | FIO_TMatrix<T>::FIO_TMatrix()
|
---|
248 | {
|
---|
249 | dobj=new TMatrix<T>;
|
---|
250 | ownobj=true;
|
---|
251 | }
|
---|
252 |
|
---|
253 | template <class T>
|
---|
254 | FIO_TMatrix<T>::FIO_TMatrix(string const & filename)
|
---|
255 | {
|
---|
256 | dobj=new TMatrix<T>;
|
---|
257 | ownobj=true;
|
---|
258 | Read(filename);
|
---|
259 | }
|
---|
260 |
|
---|
261 | template <class T>
|
---|
262 | FIO_TMatrix<T>::FIO_TMatrix(const TMatrix<T> & obj)
|
---|
263 | {
|
---|
264 | dobj = new TMatrix<T>(obj);
|
---|
265 | ownobj=true;
|
---|
266 | }
|
---|
267 |
|
---|
268 | template <class T>
|
---|
269 | FIO_TMatrix<T>::FIO_TMatrix(TMatrix<T> * obj)
|
---|
270 | {
|
---|
271 | dobj = obj;
|
---|
272 | ownobj=false;
|
---|
273 | }
|
---|
274 |
|
---|
275 | template <class T>
|
---|
276 | FIO_TMatrix<T>::~FIO_TMatrix()
|
---|
277 | {
|
---|
278 | if (ownobj && dobj) delete dobj;
|
---|
279 | }
|
---|
280 |
|
---|
281 | template <class T>
|
---|
282 | AnyDataObj* FIO_TMatrix<T>::DataObj()
|
---|
283 | {
|
---|
284 | return(dobj);
|
---|
285 | }
|
---|
286 |
|
---|
287 |
|
---|
288 | template <class T>
|
---|
289 | void FIO_TMatrix<T>::ReadSelf(PInPersist& is)
|
---|
290 | {
|
---|
291 | // On lit les 3 premiers uint_8
|
---|
292 | // 0: Numero de version, 1 : NRows, 2 : NCol
|
---|
293 | uint_4 itab[3];
|
---|
294 | is.Get(itab,3);
|
---|
295 | if (dobj == NULL) dobj = new TMatrix<T>(itab[1],itab[2]);
|
---|
296 | else dobj->ReSize(itab[1],itab[2]);
|
---|
297 | // On lit le NDataBlock
|
---|
298 | FIO_NDataBlock<T> fio_nd(&dobj->DataBlock());
|
---|
299 | fio_nd.Read(is);
|
---|
300 | }
|
---|
301 |
|
---|
302 | template <class T>
|
---|
303 | void FIO_TMatrix<T>::WriteSelf(POutPersist& os) const
|
---|
304 | {
|
---|
305 | if (dobj == NULL) return;
|
---|
306 | // On ecrit 3 uint_4 ....
|
---|
307 | // 0: Numero de version, 1 : NRows, 2 : NCol
|
---|
308 | uint_4 itab[3];
|
---|
309 | itab[0] = 1; // Numero de version a 1
|
---|
310 | itab[1] = dobj->NRows();
|
---|
311 | itab[2] = dobj->NCols();
|
---|
312 | os.Put(itab,3);
|
---|
313 | // On ecrit le NDataBlock
|
---|
314 | FIO_NDataBlock<T> fio_nd(&dobj->DataBlock());
|
---|
315 | fio_nd.Write(os);
|
---|
316 | }
|
---|
317 |
|
---|
318 | ///////////////////////////////////////////////////////////////
|
---|
319 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
320 | #pragma define_template TMatrix<uint_1>
|
---|
321 | #pragma define_template TMatrix<uint_2>
|
---|
322 | #pragma define_template TMatrix<int_2>
|
---|
323 | #pragma define_template TMatrix<int_4>
|
---|
324 | #pragma define_template TMatrix<int_8>
|
---|
325 | #pragma define_template TMatrix<uint_4>
|
---|
326 | #pragma define_template TMatrix<uint_8>
|
---|
327 | #pragma define_template TMatrix<r_4>
|
---|
328 | #pragma define_template TMatrix<r_8>
|
---|
329 | #pragma define_template TMatrix< complex<float> >
|
---|
330 | #pragma define_template TMatrix< complex<double> >
|
---|
331 | // Instances des delegues FileIO (PPersist)
|
---|
332 | #pragma define_template FIO_TMatrix<uint_1>
|
---|
333 | #pragma define_template FIO_TMatrix<uint_2>
|
---|
334 | #pragma define_template FIO_TMatrix<int_2>
|
---|
335 | #pragma define_template FIO_TMatrix<int_4>
|
---|
336 | #pragma define_template FIO_TMatrix<int_8>
|
---|
337 | #pragma define_template FIO_TMatrix<uint_4>
|
---|
338 | #pragma define_template FIO_TMatrix<uint_8>
|
---|
339 | #pragma define_template FIO_TMatrix<r_8>
|
---|
340 | #pragma define_template FIO_TMatrix<r_4>
|
---|
341 | #pragma define_template FIO_TMatrix< complex<float> >
|
---|
342 | #pragma define_template FIO_TMatrix< complex<double> >
|
---|
343 | #endif
|
---|
344 |
|
---|
345 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
346 | template class TMatrix<uint_1>;
|
---|
347 | template class TMatrix<uint_2>;
|
---|
348 | template class TMatrix<int_2>;
|
---|
349 | template class TMatrix<int_4>;
|
---|
350 | template class TMatrix<int_8>;
|
---|
351 | template class TMatrix<uint_4>;
|
---|
352 | template class TMatrix<uint_8>;
|
---|
353 | template class TMatrix<r_4>;
|
---|
354 | template class TMatrix<r_8>;
|
---|
355 | template class TMatrix< complex<float> >;
|
---|
356 | template class TMatrix< complex<double> >;
|
---|
357 | // Instances des delegues FileIO (PPersist)
|
---|
358 | template class FIO_TMatrix<uint_1>;
|
---|
359 | template class FIO_TMatrix<uint_2>;
|
---|
360 | template class FIO_TMatrix<int_2>;
|
---|
361 | template class FIO_TMatrix<int_4>;
|
---|
362 | template class FIO_TMatrix<int_8>;
|
---|
363 | template class FIO_TMatrix<uint_4>;
|
---|
364 | template class FIO_TMatrix<uint_8>;
|
---|
365 | template class FIO_TMatrix<r_8>;
|
---|
366 | template class FIO_TMatrix<r_4>;
|
---|
367 | template class FIO_TMatrix< complex<float> >;
|
---|
368 | template class FIO_TMatrix< complex<double> >;
|
---|
369 | #endif
|
---|