[302] | 1 | // $Id: tmatrix.cc,v 1.7 1999-05-18 12:58:24 ansari Exp $
|
---|
[279] | 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 |
|
---|
| 12 | using namespace PlanckDPC;
|
---|
| 13 |
|
---|
| 14 | ////////////////////////////////////////////////////////////////
|
---|
[288] | 15 | //**** Createur, Destructeur
|
---|
[279] | 16 |
|
---|
| 17 | template <class T>
|
---|
| 18 | TMatrix<T>::TMatrix()
|
---|
| 19 | // Constructeur par defaut.
|
---|
[286] | 20 | : mNr(0), mNc(0), mNDBlock()
|
---|
[279] | 21 | {
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | template <class T>
|
---|
| 25 | TMatrix<T>::TMatrix(uint_4 r,uint_4 c)
|
---|
| 26 | // Construit une matrice de r lignes et c colonnes.
|
---|
[286] | 27 | : mNr(r), mNc(c), mNDBlock(r*c)
|
---|
[279] | 28 | {
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | template <class T>
|
---|
| 32 | TMatrix<T>::TMatrix(uint_4 r,uint_4 c,T* values,Bridge* br)
|
---|
| 33 | // Construit une matrice de r lignes et c colonnes. On fournit
|
---|
| 34 | // le tableau des valeurs et eventuellement un Bridge.
|
---|
[286] | 35 | : mNr(r), mNc(c), mNDBlock(r*c,values,br)
|
---|
[279] | 36 | {
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | template <class T>
|
---|
| 40 | TMatrix<T>::TMatrix(const TMatrix<T>& a)
|
---|
[288] | 41 | // Constructeur par copie (partage si "a" temporaire).
|
---|
[286] | 42 | : mNr(a.mNr), mNc(a.mNc), mNDBlock(a.mNDBlock)
|
---|
[279] | 43 | {
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | template <class T>
|
---|
| 47 | TMatrix<T>::TMatrix(const TMatrix<T>& a,bool share)
|
---|
[288] | 48 | // Constructeur par copie avec possibilite de forcer le partage ou non.
|
---|
[286] | 49 | : mNr(a.mNr), mNc(a.mNc), mNDBlock(a.mNDBlock,share)
|
---|
[279] | 50 | {
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | template <class T>
|
---|
| 54 | TMatrix<T>::~TMatrix()
|
---|
| 55 | // Destructeur
|
---|
| 56 | {
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | ////////////////////////////////////////////////////////////////
|
---|
[288] | 60 | // Operations matricielles
|
---|
[279] | 61 | template <class T>
|
---|
[288] | 62 | TMatrix<T> TMatrix<T>::Transpose(void) const
|
---|
| 63 | // Transposition
|
---|
[279] | 64 | {
|
---|
[288] | 65 | TMatrix<T> a; a.Clone(*this); a.SetTemp(true);
|
---|
| 66 | a.mNr = mNc; a.mNc = mNr;
|
---|
[301] | 67 | {for(uint_4 i=0; i<mNr; i++)
|
---|
| 68 | for(uint_4 j=0; j<mNc; j++) {
|
---|
[288] | 69 | a(j,i) = (*this)(i,j);
|
---|
| 70 | }}
|
---|
| 71 | return a;
|
---|
[279] | 72 | }
|
---|
| 73 |
|
---|
| 74 | ////////////////////////////////////////////////////////////////
|
---|
| 75 | //**** Impression
|
---|
| 76 |
|
---|
| 77 | template <class T>
|
---|
| 78 | void TMatrix<T>::Print(ostream& os,int lp
|
---|
| 79 | ,uint_4 i0,uint_4 ni,uint_4 j0,uint_4 nj) const
|
---|
| 80 | // Impression de la sous-matrice (i:i+ni-1,i:j+nj-1)
|
---|
| 81 | {
|
---|
[288] | 82 | os<<"TMatrix::Print("<<mNr<<","<<mNc<<")"<<endl;
|
---|
[279] | 83 | if(lp>0)
|
---|
[286] | 84 | {os<<" this="<<this<<endl; mNDBlock.Print(0,0);}
|
---|
[279] | 85 | if(mNr==0 || mNc==0) return;
|
---|
| 86 | if(i0>=mNr || j0>=mNc || ni==0 || nj==0) return;
|
---|
| 87 | uint_4 i1 = i0+ni; if(i1>mNr) i1 = mNr;
|
---|
| 88 | uint_4 j1 = j0+nj; if(j1>mNc) j1 = mNc;
|
---|
[286] | 89 | for(uint_4 i=i0;i<i1;i++) {
|
---|
| 90 | for(uint_4 j=j0;j<j1;j++) cout<<" "<<(*this)(i,j);
|
---|
| 91 | cout<<endl;
|
---|
[279] | 92 | }
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | ////////////////////////////////////////////////////////////////
|
---|
[288] | 96 | //**** Surcharge de *= (INPLACE): TMatrix *= TMatrix;
|
---|
[279] | 97 |
|
---|
| 98 | template <class T>
|
---|
| 99 | TMatrix<T>& TMatrix<T>::operator *= (const TMatrix<T>& a)
|
---|
| 100 | // A = A*B -> A(n,m) = A(n,m)*B(m,m)
|
---|
| 101 | {
|
---|
| 102 | uint_4 ndata = mNr*mNc;
|
---|
[286] | 103 | if(ndata==0 || mNc!=a.mNr || a.mNr!=a.mNc)
|
---|
| 104 | throw(SzMismatchError("TMatrix::operator*=A size mismatch"));
|
---|
[279] | 105 | // A(i,j) = Sum(k) A(i,k)*B(k,j) ... il faut sauver la ligne "i" de A
|
---|
| 106 | // Vecteur oi : vecteur ou est sauve la ligne i de la matrice *this
|
---|
| 107 | // oi,oe = pointeur de debut et de fin du vecteur temporaire
|
---|
| 108 | // oij = pointeur parcourant le vecteur oi
|
---|
| 109 | // Matrice *this: i = pointeur du debut de la ligne i
|
---|
| 110 | // ij = pointeur parcourant la ligne i
|
---|
| 111 | // Matrice a : aj = pointeur de debut de la colonne j
|
---|
| 112 | // aji = pointeur parcourant la colonne j
|
---|
| 113 | T* oi = new T[mNc]; T* oe = oi+mNc;
|
---|
| 114 | for(T *i=Data(); i<Data()+ndata; i+=mNc) {
|
---|
| 115 | {for(T *oij=oi,*ij=i; oij<oe;) *oij++ = *ij++;}
|
---|
| 116 | {for(T *ij=i,*aj=const_cast<T *>(a.Data()); aj<a.Data()+a.mNc; ij++,aj++) {
|
---|
| 117 | T sum = 0;
|
---|
| 118 | for(T *oij=oi,*aji=aj; oij<oe; oij++,aji+=a.mNc) sum += *oij * *aji;
|
---|
| 119 | *ij = sum;
|
---|
| 120 | }}
|
---|
| 121 | }
|
---|
| 122 | delete [] oi;
|
---|
| 123 | return *this;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[286] | 126 | ////////////////////////////////////////////////////////////////
|
---|
[288] | 127 | //**** Pour surcharge d'operateurs C = A (+,-,*,/) B
|
---|
[286] | 128 |
|
---|
[288] | 129 | template <class T> TMatrix<T> TMatrix<T>::Add(const TMatrix<T>& b) const
|
---|
[286] | 130 | {
|
---|
| 131 | if(mNr!=b.mNr || mNc!=b.mNc)
|
---|
[299] | 132 | throw(SzMismatchError("TMatrix operator C=A+B size mismatch\n"));
|
---|
[288] | 133 | TMatrix<T> result; result.SetTemp(true); result.mNr=mNr; result.mNc=mNc;
|
---|
| 134 | result.mNDBlock = mNDBlock+b.mNDBlock;
|
---|
[286] | 135 | return result;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[288] | 138 | template <class T> TMatrix<T> TMatrix<T>::Sub(const TMatrix<T>& b) const
|
---|
[286] | 139 | {
|
---|
| 140 | if(mNr!=b.mNr || mNc!=b.mNc)
|
---|
[299] | 141 | throw(SzMismatchError("TMatrix operator C=A-B size mismatch\n"));
|
---|
[288] | 142 | TMatrix<T> result; result.SetTemp(true); result.mNr=mNr; result.mNc=mNc;
|
---|
| 143 | result.mNDBlock = mNDBlock-b.mNDBlock;
|
---|
[286] | 144 | return result;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
[288] | 147 | template <class T> TMatrix<T> TMatrix<T>::Mul(const TMatrix<T>& b) const
|
---|
[286] | 148 | // C = A(this)*B : Cij = Aik Bkj (allocation forcee dans tous les cas)
|
---|
| 149 | {
|
---|
| 150 | if(mNr==0 || mNc==0 || b.mNc==0 || mNc!=b.mNr)
|
---|
[299] | 151 | throw(SzMismatchError("TMatrix operator C=A*B size mismatch\n"));
|
---|
[288] | 152 | TMatrix<T> r; r.SetTemp(true); r.ReSize(mNr,b.mNc);
|
---|
[286] | 153 | T *ai,*aik,*bj,*bkj,*ri,*rij;
|
---|
| 154 | for(ri=const_cast<T *>(r.Data()),ai=const_cast<T *>(Data());
|
---|
| 155 | ri<r.Data()+r.mNr*r.mNc;ri+=r.mNc,ai+=mNc) {
|
---|
| 156 | for(rij=ri,bj=const_cast<T *>(b.Data());rij<ri+r.mNc;rij++,bj++) {
|
---|
| 157 | *rij = 0;
|
---|
| 158 | for(aik=ai,bkj=bj;aik<ai+mNc;aik++,bkj+=b.mNc) *rij += *aik * *bkj;
|
---|
| 159 | }
|
---|
| 160 | }
|
---|
| 161 | return r;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[299] | 164 | #include "matrix.h"
|
---|
[286] | 165 | ////////////////////////////////////////////////////////////////
|
---|
[299] | 166 | //**** Pour inversion
|
---|
[302] | 167 | r_8 TMatrix<r_8>::GausPiv(TMatrix<r_8>& a, TMatrix<r_8>& b)
|
---|
| 168 | // Pivot de Gauss
|
---|
| 169 | // * Attention: egcs impose que cette fonction soit mise dans le .cc
|
---|
| 170 | // avant ::Inverse() (car Inverse() l'utilise)
|
---|
| 171 | {
|
---|
| 172 | Matrix A(a);
|
---|
| 173 | Matrix B(b);
|
---|
| 174 | return (r_8) Matrix::GausPiv(A,B);
|
---|
| 175 | }
|
---|
| 176 |
|
---|
[299] | 177 | TMatrix<r_8> TMatrix<r_8>::Inverse() const
|
---|
| 178 | // Inversion
|
---|
| 179 | {
|
---|
| 180 | TMatrix<r_8> b(mNc,mNr);
|
---|
| 181 | TMatrix<r_8> a(*this);
|
---|
| 182 | b = 1.;
|
---|
| 183 | if (fabs(TMatrix<r_8>::GausPiv(a,b)) < 1.e-50)
|
---|
| 184 | throw(MathExc("TMatrix Inverse() Singular Matrix"));
|
---|
| 185 | return b;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
[302] | 188 | #include "generalfit.h"
|
---|
[299] | 189 | //////////////////////////////////////////////////////////
|
---|
| 190 | //**** Residus des fits
|
---|
[301] | 191 | TMatrix<r_8> TMatrix<r_8>::FitResidus(GeneralFit& gfit
|
---|
| 192 | ,double xorg,double yorg,double dx,double dy)
|
---|
| 193 | // Retourne une classe contenant les residus du fit ``gfit''.
|
---|
| 194 | // On suppose que x=j (colonnes) et y=i (lignes) pour m(i,j).
|
---|
| 195 | // Les coordonnees de l'element (i,j) sont :
|
---|
| 196 | // (i,j) -> x = xorg+j*dx , y = yorg+i*dy
|
---|
[299] | 197 | {
|
---|
| 198 | if(NCols()<=0||NRows()<=0)
|
---|
| 199 | throw(SzMismatchError("TMatrix::FitResidus size mismatch\n"));
|
---|
| 200 | GeneralFunction* f = gfit.GetFunction();
|
---|
| 201 | if(f==NULL)
|
---|
| 202 | throw(NullPtrError("TMatrix::FitResidus GeneraFit==NULL\n"));
|
---|
| 203 | int npar = gfit.GetNPar();
|
---|
| 204 | if(npar==0)
|
---|
| 205 | throw(SzMismatchError("TMatrix::FitResidus GeneraFit 0 parametre\n"));
|
---|
| 206 | double* par = new double[npar];
|
---|
| 207 | {for(int i=0;i<npar;i++) par[i] = gfit.GetParm(i);}
|
---|
| 208 | TMatrix<r_8> m(*this);
|
---|
[301] | 209 | for(uint_4 i=0;i<NRows();i++) for(uint_4 j=0;j<NCols();j++) {
|
---|
| 210 | double x[2] = {xorg+j*dx,yorg+i*dy};
|
---|
[299] | 211 | m(i,j) -= f->Value(x,par);
|
---|
| 212 | }
|
---|
| 213 | delete [] par;
|
---|
| 214 | return m;
|
---|
| 215 | }
|
---|
| 216 |
|
---|
[301] | 217 | TMatrix<r_8> TMatrix<r_8>::FitFunction(GeneralFit& gfit
|
---|
| 218 | ,double xorg,double yorg,double dx,double dy)
|
---|
| 219 | // Retourne une classe contenant la fonction du fit ``gfit''.
|
---|
| 220 | // On suppose que x=j (colonnes) et y=i (lignes) pour m(i,j).
|
---|
| 221 | // Les coordonnees de l'element (i,j) sont :
|
---|
| 222 | // (i,j) -> x = xorg + j*dx , y = yorg + i*dy
|
---|
[299] | 223 | {
|
---|
| 224 | if(NCols()<=0||NRows()<=0)
|
---|
| 225 | throw(SzMismatchError("TMatrix::FitFunction size mismatch\n"));
|
---|
| 226 | GeneralFunction* f = gfit.GetFunction();
|
---|
| 227 | if(f==NULL)
|
---|
| 228 | throw(NullPtrError("TMatrix::FitFunction GeneraFit==NULL\n"));
|
---|
| 229 | int npar = gfit.GetNPar();
|
---|
| 230 | if(npar==0)
|
---|
| 231 | throw(SzMismatchError("TMatrix::FitFunction GeneraFit 0 parametre\n"));
|
---|
| 232 | double* par = new double[npar];
|
---|
| 233 | {for(int i=0;i<npar;i++) par[i] = gfit.GetParm(i);}
|
---|
| 234 | TMatrix<r_8> m(*this);
|
---|
[301] | 235 | for(uint_4 i=0;i<NRows();i++) for(uint_4 j=0;j<NCols();j++) {
|
---|
| 236 | double x[2] = {xorg+j*dx,yorg+i*dy};
|
---|
[299] | 237 | m(i,j) = f->Value(x,par);
|
---|
| 238 | }
|
---|
| 239 | delete [] par;
|
---|
| 240 | return m;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | ////////////////////////////////////////////////////////////////
|
---|
[286] | 244 | // -------------------------------------------------------------------------
|
---|
| 245 | // Les objets delegues pour la gestion de persistance
|
---|
| 246 | // -------------------------------------------------------------------------
|
---|
| 247 |
|
---|
| 248 | template <class T>
|
---|
| 249 | FIO_TMatrix<T>::FIO_TMatrix()
|
---|
| 250 | {
|
---|
| 251 | dobj=new TMatrix<T>;
|
---|
| 252 | ownobj=true;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | template <class T>
|
---|
| 256 | FIO_TMatrix<T>::FIO_TMatrix(string const & filename)
|
---|
| 257 | {
|
---|
| 258 | dobj=new TMatrix<T>;
|
---|
| 259 | ownobj=true;
|
---|
| 260 | Read(filename);
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | template <class T>
|
---|
| 264 | FIO_TMatrix<T>::FIO_TMatrix(const TMatrix<T> & obj)
|
---|
| 265 | {
|
---|
| 266 | dobj = new TMatrix<T>(obj);
|
---|
| 267 | ownobj=true;
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | template <class T>
|
---|
| 271 | FIO_TMatrix<T>::FIO_TMatrix(TMatrix<T> * obj)
|
---|
| 272 | {
|
---|
| 273 | dobj = obj;
|
---|
| 274 | ownobj=false;
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | template <class T>
|
---|
| 278 | FIO_TMatrix<T>::~FIO_TMatrix()
|
---|
| 279 | {
|
---|
| 280 | if (ownobj && dobj) delete dobj;
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | template <class T>
|
---|
| 284 | AnyDataObj* FIO_TMatrix<T>::DataObj()
|
---|
| 285 | {
|
---|
| 286 | return(dobj);
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 |
|
---|
| 290 | template <class T>
|
---|
| 291 | void FIO_TMatrix<T>::ReadSelf(PInPersist& is)
|
---|
| 292 | {
|
---|
| 293 | // On lit les 3 premiers uint_8
|
---|
[291] | 294 | // 0: Numero de version, 1 : NRows, 2 : NCol
|
---|
| 295 | uint_4 itab[3];
|
---|
| 296 | is.Get(itab,3);
|
---|
| 297 | if (dobj == NULL) dobj = new TMatrix<T>(itab[1],itab[2]);
|
---|
| 298 | else dobj->ReSize(itab[1],itab[2]);
|
---|
[286] | 299 | // On lit le NDataBlock
|
---|
[291] | 300 | FIO_NDataBlock<T> fio_nd(&dobj->DataBlock());
|
---|
| 301 | fio_nd.Read(is);
|
---|
[286] | 302 | }
|
---|
| 303 |
|
---|
| 304 | template <class T>
|
---|
| 305 | void FIO_TMatrix<T>::WriteSelf(POutPersist& os) const
|
---|
| 306 | {
|
---|
| 307 | if (dobj == NULL) return;
|
---|
[291] | 308 | // On ecrit 3 uint_4 ....
|
---|
| 309 | // 0: Numero de version, 1 : NRows, 2 : NCol
|
---|
| 310 | uint_4 itab[3];
|
---|
| 311 | itab[0] = 1; // Numero de version a 1
|
---|
| 312 | itab[1] = dobj->NRows();
|
---|
| 313 | itab[2] = dobj->NCols();
|
---|
| 314 | os.Put(itab,3);
|
---|
[286] | 315 | // On ecrit le NDataBlock
|
---|
[291] | 316 | FIO_NDataBlock<T> fio_nd(&dobj->DataBlock());
|
---|
| 317 | fio_nd.Write(os);
|
---|
[286] | 318 | }
|
---|
| 319 |
|
---|
[279] | 320 | ///////////////////////////////////////////////////////////////
|
---|
| 321 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
| 322 | #pragma define_template TMatrix<uint_1>
|
---|
| 323 | #pragma define_template TMatrix<uint_2>
|
---|
| 324 | #pragma define_template TMatrix<int_2>
|
---|
| 325 | #pragma define_template TMatrix<int_4>
|
---|
| 326 | #pragma define_template TMatrix<int_8>
|
---|
| 327 | #pragma define_template TMatrix<uint_4>
|
---|
| 328 | #pragma define_template TMatrix<uint_8>
|
---|
| 329 | #pragma define_template TMatrix<r_4>
|
---|
| 330 | #pragma define_template TMatrix<r_8>
|
---|
[286] | 331 | #pragma define_template TMatrix< complex<float> >
|
---|
| 332 | #pragma define_template TMatrix< complex<double> >
|
---|
[279] | 333 | // Instances des delegues FileIO (PPersist)
|
---|
[286] | 334 | #pragma define_template FIO_TMatrix<uint_1>
|
---|
| 335 | #pragma define_template FIO_TMatrix<uint_2>
|
---|
| 336 | #pragma define_template FIO_TMatrix<int_2>
|
---|
| 337 | #pragma define_template FIO_TMatrix<int_4>
|
---|
| 338 | #pragma define_template FIO_TMatrix<int_8>
|
---|
| 339 | #pragma define_template FIO_TMatrix<uint_4>
|
---|
| 340 | #pragma define_template FIO_TMatrix<uint_8>
|
---|
| 341 | #pragma define_template FIO_TMatrix<r_8>
|
---|
| 342 | #pragma define_template FIO_TMatrix<r_4>
|
---|
| 343 | #pragma define_template FIO_TMatrix< complex<float> >
|
---|
| 344 | #pragma define_template FIO_TMatrix< complex<double> >
|
---|
[279] | 345 | #endif
|
---|
| 346 |
|
---|
| 347 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
| 348 | template class TMatrix<uint_1>;
|
---|
| 349 | template class TMatrix<uint_2>;
|
---|
| 350 | template class TMatrix<int_2>;
|
---|
| 351 | template class TMatrix<int_4>;
|
---|
| 352 | template class TMatrix<int_8>;
|
---|
| 353 | template class TMatrix<uint_4>;
|
---|
| 354 | template class TMatrix<uint_8>;
|
---|
| 355 | template class TMatrix<r_4>;
|
---|
| 356 | template class TMatrix<r_8>;
|
---|
| 357 | template class TMatrix< complex<float> >;
|
---|
| 358 | template class TMatrix< complex<double> >;
|
---|
| 359 | // Instances des delegues FileIO (PPersist)
|
---|
[286] | 360 | template class FIO_TMatrix<uint_1>;
|
---|
| 361 | template class FIO_TMatrix<uint_2>;
|
---|
| 362 | template class FIO_TMatrix<int_2>;
|
---|
| 363 | template class FIO_TMatrix<int_4>;
|
---|
| 364 | template class FIO_TMatrix<int_8>;
|
---|
| 365 | template class FIO_TMatrix<uint_4>;
|
---|
| 366 | template class FIO_TMatrix<uint_8>;
|
---|
| 367 | template class FIO_TMatrix<r_8>;
|
---|
| 368 | template class FIO_TMatrix<r_4>;
|
---|
| 369 | template class FIO_TMatrix< complex<float> >;
|
---|
| 370 | template class FIO_TMatrix< complex<double> >;
|
---|
[279] | 371 | #endif
|
---|