[286] | 1 | // $Id: tmatrix.cc,v 1.2 1999-04-30 11:02:52 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 | ////////////////////////////////////////////////////////////////
|
---|
| 15 | //************ Createur, Destructeur, gestion des donnees
|
---|
| 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)
|
---|
[286] | 41 | // Constructeur par copie (partage des donnees).
|
---|
| 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)
|
---|
| 48 | // Constructeur par copie.
|
---|
[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 | ////////////////////////////////////////////////////////////////
|
---|
| 60 | template <class T>
|
---|
| 61 | void TMatrix<T>::Clone(const TMatrix<T>& a)
|
---|
[286] | 62 | // Clone (copie de donnees) a partir de "a"
|
---|
| 63 | // Partage des donnees si "a" est temporaire.
|
---|
[279] | 64 | {
|
---|
[286] | 65 | mNDBlock.Clone(a.mNDBlock); mNr = a.mNr; mNc = a.mNc;
|
---|
[279] | 66 | }
|
---|
| 67 |
|
---|
| 68 | template <class T>
|
---|
| 69 | void TMatrix<T>::Reset(T v)
|
---|
| 70 | // Reset de la matrice a "v"
|
---|
| 71 | {
|
---|
[286] | 72 | mNDBlock.Reset(v);
|
---|
[279] | 73 | }
|
---|
| 74 |
|
---|
| 75 | template <class T>
|
---|
| 76 | void TMatrix<T>::Realloc(uint_4 r,uint_4 c,bool force_alloc)
|
---|
| 77 | // Reallocation de place
|
---|
| 78 | {
|
---|
| 79 | if(r==0 || c==0) throw(SzMismatchError("TMatrix::ReSize r ou c==0\n"));
|
---|
| 80 | if(!force_alloc && mNr==r && mNc==c) return;
|
---|
[286] | 81 | mNr = r; mNc = c; mNDBlock.ReSize(r*c,force_alloc);
|
---|
[279] | 82 | }
|
---|
| 83 |
|
---|
| 84 | ////////////////////////////////////////////////////////////////
|
---|
| 85 | //**** Surcharge de TMatrix=TMatrix; TMatrix=<T> b;
|
---|
| 86 |
|
---|
| 87 | template <class T>
|
---|
| 88 | TMatrix<T>& TMatrix<T>::operator = (T x)
|
---|
| 89 | // Operateur d'affectation depuis scalaire : identite * scalaire.
|
---|
| 90 | {
|
---|
[286] | 91 | if(mNr!=mNc || mNr==0)
|
---|
| 92 | throw(SzMismatchError("TMatrix::operator= mNc!=mNr ou ==0\n"));
|
---|
| 93 | for(uint_4 r=0;r<mNr;r++) for(uint_4 c=0;c<mNc;c++)
|
---|
| 94 | (*this)(r,c) = (r==c)? x: 0;
|
---|
[279] | 95 | return *this;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | template <class T>
|
---|
| 99 | TMatrix<T>& TMatrix<T>::operator = (const TMatrix<T>& a)
|
---|
[286] | 100 | // Operateur d'affectation A=a
|
---|
| 101 | // (Re)allocation de la place memoire
|
---|
| 102 | // sauf si "a" est cree temporairement (partage)
|
---|
[279] | 103 | {
|
---|
| 104 | if(this == &a) return *this;
|
---|
[286] | 105 | Clone(a);
|
---|
[279] | 106 | return *this;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | ////////////////////////////////////////////////////////////////
|
---|
| 110 | //**** Impression
|
---|
| 111 |
|
---|
| 112 | template <class T>
|
---|
| 113 | void TMatrix<T>::Print(ostream& os,int lp
|
---|
| 114 | ,uint_4 i0,uint_4 ni,uint_4 j0,uint_4 nj) const
|
---|
| 115 | // Impression de la sous-matrice (i:i+ni-1,i:j+nj-1)
|
---|
| 116 | {
|
---|
| 117 | os<<"TMatrix::Print("<<mNr<<","<<mNc<<")"<<endl;
|
---|
| 118 | if(lp>0)
|
---|
[286] | 119 | {os<<" this="<<this<<endl; mNDBlock.Print(0,0);}
|
---|
[279] | 120 | if(mNr==0 || mNc==0) return;
|
---|
| 121 | if(i0>=mNr || j0>=mNc || ni==0 || nj==0) return;
|
---|
| 122 | uint_4 i1 = i0+ni; if(i1>mNr) i1 = mNr;
|
---|
| 123 | uint_4 j1 = j0+nj; if(j1>mNc) j1 = mNc;
|
---|
[286] | 124 | for(uint_4 i=i0;i<i1;i++) {
|
---|
| 125 | for(uint_4 j=j0;j<j1;j++) cout<<" "<<(*this)(i,j);
|
---|
| 126 | cout<<endl;
|
---|
[279] | 127 | }
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | ////////////////////////////////////////////////////////////////
|
---|
| 131 | //**** Surcharge de +=,-=,*=,/= (INPLACE): TMatrix += <T> b;
|
---|
| 132 |
|
---|
| 133 | template <class T>
|
---|
| 134 | TMatrix<T>& TMatrix<T>::operator += (T b)
|
---|
| 135 | {
|
---|
[286] | 136 | mNDBlock += b;
|
---|
[279] | 137 | return *this;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | template <class T>
|
---|
| 141 | TMatrix<T>& TMatrix<T>::operator -= (T b)
|
---|
| 142 | {
|
---|
[286] | 143 | mNDBlock -= b;
|
---|
[279] | 144 | return *this;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | template <class T>
|
---|
| 148 | TMatrix<T>& TMatrix<T>::operator *= (T b)
|
---|
| 149 | {
|
---|
[286] | 150 | mNDBlock *= b;
|
---|
[279] | 151 | return *this;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | template <class T>
|
---|
| 155 | TMatrix<T>& TMatrix<T>::operator /= (T b)
|
---|
| 156 | {
|
---|
[286] | 157 | mNDBlock /= b;
|
---|
[279] | 158 | return *this;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | ////////////////////////////////////////////////////////////////
|
---|
| 162 | //**** Surcharge de +=,-=,*= (INPLACE): TMatrix += TMatrix;
|
---|
| 163 |
|
---|
| 164 | template <class T>
|
---|
| 165 | TMatrix<T>& TMatrix<T>::operator += (const TMatrix<T>& a)
|
---|
| 166 | {
|
---|
[286] | 167 | if(mNr==0 || mNc==0 || mNr!=a.mNr || mNc!=a.mNc)
|
---|
| 168 | throw(SzMismatchError("TMatrix::operator+=A size mismatch"));
|
---|
| 169 | mNDBlock += a.mNDBlock;
|
---|
[279] | 170 | return *this;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | template <class T>
|
---|
| 174 | TMatrix<T>& TMatrix<T>::operator -= (const TMatrix<T>& a)
|
---|
| 175 | {
|
---|
[286] | 176 | if(mNr==0 || mNc==0 || mNr!=a.mNr || mNc!=a.mNc)
|
---|
| 177 | throw(SzMismatchError("TMatrix::operator-=A size mismatch"));
|
---|
| 178 | mNDBlock -= a.mNDBlock;
|
---|
[279] | 179 | return *this;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | template <class T>
|
---|
| 183 | TMatrix<T>& TMatrix<T>::operator *= (const TMatrix<T>& a)
|
---|
| 184 | // A = A*B -> A(n,m) = A(n,m)*B(m,m)
|
---|
| 185 | {
|
---|
| 186 | uint_4 ndata = mNr*mNc;
|
---|
[286] | 187 | if(ndata==0 || mNc!=a.mNr || a.mNr!=a.mNc)
|
---|
| 188 | throw(SzMismatchError("TMatrix::operator*=A size mismatch"));
|
---|
[279] | 189 | // A(i,j) = Sum(k) A(i,k)*B(k,j) ... il faut sauver la ligne "i" de A
|
---|
| 190 | // Vecteur oi : vecteur ou est sauve la ligne i de la matrice *this
|
---|
| 191 | // oi,oe = pointeur de debut et de fin du vecteur temporaire
|
---|
| 192 | // oij = pointeur parcourant le vecteur oi
|
---|
| 193 | // Matrice *this: i = pointeur du debut de la ligne i
|
---|
| 194 | // ij = pointeur parcourant la ligne i
|
---|
| 195 | // Matrice a : aj = pointeur de debut de la colonne j
|
---|
| 196 | // aji = pointeur parcourant la colonne j
|
---|
| 197 | T* oi = new T[mNc]; T* oe = oi+mNc;
|
---|
| 198 | for(T *i=Data(); i<Data()+ndata; i+=mNc) {
|
---|
| 199 | {for(T *oij=oi,*ij=i; oij<oe;) *oij++ = *ij++;}
|
---|
| 200 | {for(T *ij=i,*aj=const_cast<T *>(a.Data()); aj<a.Data()+a.mNc; ij++,aj++) {
|
---|
| 201 | T sum = 0;
|
---|
| 202 | for(T *oij=oi,*aji=aj; oij<oe; oij++,aji+=a.mNc) sum += *oij * *aji;
|
---|
| 203 | *ij = sum;
|
---|
| 204 | }}
|
---|
| 205 | }
|
---|
| 206 | delete [] oi;
|
---|
| 207 | return *this;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[286] | 210 | ////////////////////////////////////////////////////////////////
|
---|
| 211 | //**** Surcharge de +,-,*: TMatrix = TMatrix + TMatrix;
|
---|
| 212 |
|
---|
| 213 | template <class T>
|
---|
| 214 | TMatrix<T> TMatrix<T>::Add(const TMatrix<T>& b) const
|
---|
| 215 | // C = A(this)+B
|
---|
| 216 | {
|
---|
| 217 | if(mNr!=b.mNr || mNc!=b.mNc)
|
---|
| 218 | throw(SzMismatchError("NDataBlock operator C=A+B size mismatch\n"));
|
---|
| 219 | TMatrix<T> result; result.SetTemp(true);
|
---|
| 220 | if(b.IsTemp()) {
|
---|
| 221 | result.Clone(b);
|
---|
| 222 | result += *this;
|
---|
| 223 | } else {
|
---|
| 224 | result.Clone(*this);
|
---|
| 225 | result += b;
|
---|
| 226 | }
|
---|
| 227 | return result;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | template <class T>
|
---|
| 231 | TMatrix<T> TMatrix<T>::Sub(const TMatrix<T>& b) const
|
---|
| 232 | // C = A(this)-B
|
---|
| 233 | {
|
---|
| 234 | if(mNr!=b.mNr || mNc!=b.mNc)
|
---|
| 235 | throw(SzMismatchError("NDataBlock operator C=A-B size mismatch\n"));
|
---|
| 236 | TMatrix<T> result; result.SetTemp(true);
|
---|
| 237 | if(b.IsTemp()) {
|
---|
| 238 | result.Clone(b);
|
---|
| 239 | result.mNDBlock = (*this).mNDBlock - result.mNDBlock;
|
---|
| 240 | } else {
|
---|
| 241 | result.Clone(*this);
|
---|
| 242 | result -= b;
|
---|
| 243 | }
|
---|
| 244 | return result;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | template <class T>
|
---|
| 248 | TMatrix<T> TMatrix<T>::Mul(const TMatrix<T>& b) const
|
---|
| 249 | // C = A(this)*B : Cij = Aik Bkj (allocation forcee dans tous les cas)
|
---|
| 250 | {
|
---|
| 251 | if(mNr==0 || mNc==0 || b.mNc==0 || mNc!=b.mNr)
|
---|
| 252 | throw(SzMismatchError("NDataBlock operator C=A*B size mismatch\n"));
|
---|
| 253 | TMatrix<T> r; r.SetTemp(true); r.Realloc(mNr,b.mNc,true);
|
---|
| 254 | T *ai,*aik,*bj,*bkj,*ri,*rij;
|
---|
| 255 | for(ri=const_cast<T *>(r.Data()),ai=const_cast<T *>(Data());
|
---|
| 256 | ri<r.Data()+r.mNr*r.mNc;ri+=r.mNc,ai+=mNc) {
|
---|
| 257 | for(rij=ri,bj=const_cast<T *>(b.Data());rij<ri+r.mNc;rij++,bj++) {
|
---|
| 258 | *rij = 0;
|
---|
| 259 | for(aik=ai,bkj=bj;aik<ai+mNc;aik++,bkj+=b.mNc) *rij += *aik * *bkj;
|
---|
| 260 | }
|
---|
| 261 | }
|
---|
| 262 | return r;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | ////////////////////////////////////////////////////////////////
|
---|
| 266 | // -------------------------------------------------------------------------
|
---|
| 267 | // Les objets delegues pour la gestion de persistance
|
---|
| 268 | // -------------------------------------------------------------------------
|
---|
| 269 |
|
---|
| 270 | template <class T>
|
---|
| 271 | FIO_TMatrix<T>::FIO_TMatrix()
|
---|
| 272 | {
|
---|
| 273 | dobj=new TMatrix<T>;
|
---|
| 274 | ownobj=true;
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | template <class T>
|
---|
| 278 | FIO_TMatrix<T>::FIO_TMatrix(string const & filename)
|
---|
| 279 | {
|
---|
| 280 | dobj=new TMatrix<T>;
|
---|
| 281 | ownobj=true;
|
---|
| 282 | Read(filename);
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | template <class T>
|
---|
| 286 | FIO_TMatrix<T>::FIO_TMatrix(const TMatrix<T> & obj)
|
---|
| 287 | {
|
---|
| 288 | dobj = new TMatrix<T>(obj);
|
---|
| 289 | ownobj=true;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | template <class T>
|
---|
| 293 | FIO_TMatrix<T>::FIO_TMatrix(TMatrix<T> * obj)
|
---|
| 294 | {
|
---|
| 295 | dobj = obj;
|
---|
| 296 | ownobj=false;
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | template <class T>
|
---|
| 300 | FIO_TMatrix<T>::~FIO_TMatrix()
|
---|
| 301 | {
|
---|
| 302 | if (ownobj && dobj) delete dobj;
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | template <class T>
|
---|
| 306 | AnyDataObj* FIO_TMatrix<T>::DataObj()
|
---|
| 307 | {
|
---|
| 308 | return(dobj);
|
---|
| 309 | }
|
---|
| 310 |
|
---|
| 311 |
|
---|
| 312 | template <class T>
|
---|
| 313 | void FIO_TMatrix<T>::ReadSelf(PInPersist& is)
|
---|
| 314 | {
|
---|
| 315 | // On lit les 3 premiers uint_8
|
---|
| 316 | uint_4 itab[2];
|
---|
| 317 | is.Get(itab,2);
|
---|
| 318 | if (dobj == NULL) dobj = new TMatrix<T>(itab[0],itab[1]);
|
---|
| 319 | else dobj->Realloc(itab[0],itab[1],false);
|
---|
| 320 | // On lit le NDataBlock
|
---|
| 321 | //cmv encore des problemes avec les consteries
|
---|
| 322 | //FIO_NDataBlock<T> fio_nd(&dobj->DataBlock());
|
---|
| 323 | //fio_nd.ReadSelf(is);
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | template <class T>
|
---|
| 327 | void FIO_TMatrix<T>::WriteSelf(POutPersist& os) const
|
---|
| 328 | {
|
---|
| 329 | if (dobj == NULL) return;
|
---|
| 330 | // On ecrit 2 uint_4 .... 0 : NRows, 1 : NCol
|
---|
| 331 | uint_4 itab[2];
|
---|
| 332 | itab[0] = dobj->NRows();
|
---|
| 333 | itab[1] = dobj->NCol();
|
---|
| 334 | os.Put(itab,2);
|
---|
| 335 | // On ecrit le NDataBlock
|
---|
| 336 | //cmv encore des problemes avec les consteries
|
---|
| 337 | //FIO_NDataBlock<T> fio_nd(&dobj->DataBlock());
|
---|
| 338 | //fio_nd.WriteSelf(os);
|
---|
| 339 | }
|
---|
| 340 |
|
---|
[279] | 341 | ///////////////////////////////////////////////////////////////
|
---|
| 342 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
| 343 | #pragma define_template TMatrix<uint_1>
|
---|
| 344 | #pragma define_template TMatrix<uint_2>
|
---|
| 345 | #pragma define_template TMatrix<int_2>
|
---|
| 346 | #pragma define_template TMatrix<int_4>
|
---|
| 347 | #pragma define_template TMatrix<int_8>
|
---|
| 348 | #pragma define_template TMatrix<uint_4>
|
---|
| 349 | #pragma define_template TMatrix<uint_8>
|
---|
| 350 | #pragma define_template TMatrix<r_4>
|
---|
| 351 | #pragma define_template TMatrix<r_8>
|
---|
[286] | 352 | #pragma define_template TMatrix< complex<float> >
|
---|
| 353 | #pragma define_template TMatrix< complex<double> >
|
---|
[279] | 354 | // Instances des delegues FileIO (PPersist)
|
---|
[286] | 355 | #pragma define_template FIO_TMatrix<uint_1>
|
---|
| 356 | #pragma define_template FIO_TMatrix<uint_2>
|
---|
| 357 | #pragma define_template FIO_TMatrix<int_2>
|
---|
| 358 | #pragma define_template FIO_TMatrix<int_4>
|
---|
| 359 | #pragma define_template FIO_TMatrix<int_8>
|
---|
| 360 | #pragma define_template FIO_TMatrix<uint_4>
|
---|
| 361 | #pragma define_template FIO_TMatrix<uint_8>
|
---|
| 362 | #pragma define_template FIO_TMatrix<r_8>
|
---|
| 363 | #pragma define_template FIO_TMatrix<r_4>
|
---|
| 364 | #pragma define_template FIO_TMatrix< complex<float> >
|
---|
| 365 | #pragma define_template FIO_TMatrix< complex<double> >
|
---|
[279] | 366 | #endif
|
---|
| 367 |
|
---|
| 368 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
| 369 | template class TMatrix<uint_1>;
|
---|
| 370 | template class TMatrix<uint_2>;
|
---|
| 371 | template class TMatrix<int_2>;
|
---|
| 372 | template class TMatrix<int_4>;
|
---|
| 373 | template class TMatrix<int_8>;
|
---|
| 374 | template class TMatrix<uint_4>;
|
---|
| 375 | template class TMatrix<uint_8>;
|
---|
| 376 | template class TMatrix<r_4>;
|
---|
| 377 | template class TMatrix<r_8>;
|
---|
| 378 | template class TMatrix< complex<float> >;
|
---|
| 379 | template class TMatrix< complex<double> >;
|
---|
| 380 | // Instances des delegues FileIO (PPersist)
|
---|
[286] | 381 | template class FIO_TMatrix<uint_1>;
|
---|
| 382 | template class FIO_TMatrix<uint_2>;
|
---|
| 383 | template class FIO_TMatrix<int_2>;
|
---|
| 384 | template class FIO_TMatrix<int_4>;
|
---|
| 385 | template class FIO_TMatrix<int_8>;
|
---|
| 386 | template class FIO_TMatrix<uint_4>;
|
---|
| 387 | template class FIO_TMatrix<uint_8>;
|
---|
| 388 | template class FIO_TMatrix<r_8>;
|
---|
| 389 | template class FIO_TMatrix<r_4>;
|
---|
| 390 | template class FIO_TMatrix< complex<float> >;
|
---|
| 391 | template class FIO_TMatrix< complex<double> >;
|
---|
[279] | 392 | #endif
|
---|