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