[279] | 1 | // $Id: tmatrix.cc,v 1.1 1999-04-29 08:57:15 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 |
|
---|
| 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.
|
---|
| 20 | : mNr(0), mNc(0), mNDBlock(NULL)
|
---|
| 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.
|
---|
| 27 | : mNr(r), mNc(c), mNDBlock(new NDataBlock<T>(r*c))
|
---|
| 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.
|
---|
| 35 | : mNr(r), mNc(c), mNDBlock(new NDataBlock<T>(r*c,values,br))
|
---|
| 36 | {
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | template <class T>
|
---|
| 40 | TMatrix<T>::TMatrix(const TMatrix<T>& a)
|
---|
| 41 | // Constructeur par copie.
|
---|
| 42 | : mNr(a.mNr), mNc(a.mNc)
|
---|
| 43 | , mNDBlock(new NDataBlock<T>(*(a.DataBlock())))
|
---|
| 44 | {
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | template <class T>
|
---|
| 48 | TMatrix<T>::TMatrix(const TMatrix<T>& a,bool share)
|
---|
| 49 | // Constructeur par copie.
|
---|
| 50 | : mNr(a.mNr), mNc(a.mNc)
|
---|
| 51 | , mNDBlock(new NDataBlock<T>(*(a.DataBlock()),share))
|
---|
| 52 | {
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | template <class T>
|
---|
| 56 | TMatrix<T>::~TMatrix()
|
---|
| 57 | // Destructeur
|
---|
| 58 | {
|
---|
| 59 | Delete();
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | template <class T>
|
---|
| 63 | void TMatrix<T>::Delete(void)
|
---|
| 64 | // Pour desallouer
|
---|
| 65 | {
|
---|
| 66 | mNr = mNc = 0;
|
---|
| 67 | if(mNDBlock) delete mNDBlock; mNDBlock = NULL;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | ////////////////////////////////////////////////////////////////
|
---|
| 71 | template <class T>
|
---|
| 72 | void TMatrix<T>::Clone(const TMatrix<T>& a)
|
---|
| 73 | // Clone (copie de donnee) a partir de "a"
|
---|
| 74 | {
|
---|
| 75 | if(a.mNr==0 || a.mNc==0 || a.DataBlock()==NULL) {Delete(); return;}
|
---|
| 76 | if(mNDBlock) Delete();
|
---|
| 77 | else NDataBlock<T>* mNDBlock = new NDataBlock<T>(1); // cas "a" vide
|
---|
| 78 | mNr = a.mNr; mNc = a.mNc;
|
---|
| 79 | mNDBlock->Clone(*(a.DataBlock()));
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | template <class T>
|
---|
| 83 | void TMatrix<T>::Reset(T v)
|
---|
| 84 | // Reset de la matrice a "v"
|
---|
| 85 | {
|
---|
| 86 | if(mNDBlock==NULL)
|
---|
| 87 | throw(NullPtrError("TMatrix::Reset mNDBlock==NULL\n"));
|
---|
| 88 | if(mNr==0 || mNc==0)
|
---|
| 89 | throw(SzMismatchError("TMatrix::Reset mNr==0 || mNc==0\n"));
|
---|
| 90 | mNDBlock->Reset(v);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | template <class T>
|
---|
| 94 | void TMatrix<T>::Realloc(uint_4 r,uint_4 c,bool force_alloc)
|
---|
| 95 | // Reallocation de place
|
---|
| 96 | {
|
---|
| 97 | if(r==0 || c==0) throw(SzMismatchError("TMatrix::ReSize r ou c==0\n"));
|
---|
| 98 | if(mNDBlock==NULL) NDataBlock<T>* mNDBlock = new NDataBlock<T>(1);
|
---|
| 99 | if(!force_alloc && mNr==r && mNc==c) return;
|
---|
| 100 | mNr = r; mNc = c;
|
---|
| 101 | mNDBlock->ReSize(r*c);
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | ////////////////////////////////////////////////////////////////
|
---|
| 105 | //**** Surcharge de TMatrix=TMatrix; TMatrix=<T> b;
|
---|
| 106 |
|
---|
| 107 | template <class T>
|
---|
| 108 | TMatrix<T>& TMatrix<T>::operator = (T x)
|
---|
| 109 | // Operateur d'affectation depuis scalaire : identite * scalaire.
|
---|
| 110 | {
|
---|
| 111 | if(mNr!=mNc || mNr==0 || mNDBlock==NULL)
|
---|
| 112 | throw(SzMismatchError("TMatrix::operator= mNc!=mNr ou ==0 ou mNDBlock==NULL\n"));
|
---|
| 113 | for(int r=0;r<mNr;r++) for(int c=0;c<mNc;c++) (*this)(r,c) = (r==c)? x: 0;
|
---|
| 114 | return *this;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | template <class T>
|
---|
| 118 | TMatrix<T>& TMatrix<T>::operator = (const TMatrix<T>& a)
|
---|
| 119 | // Operateur d'affectation A=B (sans reaffectation)
|
---|
| 120 | {
|
---|
| 121 | if(this == &a) return *this;
|
---|
| 122 | if(mNr!=a.mNc || mNc!=a.mNc || mNr==0 || mNc==0)
|
---|
| 123 | throw(SzMismatchError("TMatrix::operator= mNc!=a.mNc ou mNr!=a.mNr ou ==0\n"));
|
---|
| 124 | if(mNDBlock==NULL)
|
---|
| 125 | throw(SzMismatchError("TMatrix::operator= mNDBlock==NULL\n"));
|
---|
| 126 | *(mNDBlock) = *(a.DataBlock());
|
---|
| 127 | return *this;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | ////////////////////////////////////////////////////////////////
|
---|
| 131 | //**** Impression
|
---|
| 132 |
|
---|
| 133 | template <class T>
|
---|
| 134 | void TMatrix<T>::Print(ostream& os,int lp
|
---|
| 135 | ,uint_4 i0,uint_4 ni,uint_4 j0,uint_4 nj) const
|
---|
| 136 | // Impression de la sous-matrice (i:i+ni-1,i:j+nj-1)
|
---|
| 137 | {
|
---|
| 138 | os<<"TMatrix::Print("<<mNr<<","<<mNc<<")"<<endl;
|
---|
| 139 | if(lp>0)
|
---|
| 140 | {os<<" this="<<this<<" mNDBlock="<<mNDBlock<<endl;
|
---|
| 141 | if(mNDBlock) mNDBlock->Print(0,0);}
|
---|
| 142 | if(mNr==0 || mNc==0) return;
|
---|
| 143 | if(i0>=mNr || j0>=mNc || ni==0 || nj==0) return;
|
---|
| 144 | uint_4 i1 = i0+ni; if(i1>mNr) i1 = mNr;
|
---|
| 145 | uint_4 j1 = j0+nj; if(j1>mNc) j1 = mNc;
|
---|
| 146 | for(int i=i0;i<i1;i++) {
|
---|
| 147 | for(int j=j0;j<j1;j++) cout<<" "<<(*this)(i,j)<<endl;
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | ////////////////////////////////////////////////////////////////
|
---|
| 152 | //**** Surcharge de +=,-=,*=,/= (INPLACE): TMatrix += <T> b;
|
---|
| 153 |
|
---|
| 154 | template <class T>
|
---|
| 155 | TMatrix<T>& TMatrix<T>::operator += (T b)
|
---|
| 156 | {
|
---|
| 157 | if(mNr==0 || mNc==0 || !mNDBlock)
|
---|
| 158 | throw(SzMismatchError("TMatrix::operator+=v mNr==0 || mNc==0 || !mNDBlock\n"));
|
---|
| 159 | *mNDBlock += b;
|
---|
| 160 | return *this;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | template <class T>
|
---|
| 164 | TMatrix<T>& TMatrix<T>::operator -= (T b)
|
---|
| 165 | {
|
---|
| 166 | if(mNr==0 || mNc==0 || !mNDBlock)
|
---|
| 167 | throw(SzMismatchError("TMatrix::operator-=v mNr==0 || mNc==0 || !mNDBlock\n"));
|
---|
| 168 | *mNDBlock -= b;
|
---|
| 169 | return *this;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | template <class T>
|
---|
| 173 | TMatrix<T>& TMatrix<T>::operator *= (T b)
|
---|
| 174 | {
|
---|
| 175 | if(mNr==0 || mNc==0 || !mNDBlock)
|
---|
| 176 | throw(SzMismatchError("TMatrix::operator*=v mNr==0 || mNc==0 || !mNDBlock\n"));
|
---|
| 177 | *mNDBlock *= b;
|
---|
| 178 | return *this;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | template <class T>
|
---|
| 182 | TMatrix<T>& TMatrix<T>::operator /= (T b)
|
---|
| 183 | {
|
---|
| 184 | if(b==(T) 0) throw(ParmError("TMatrix::operator/=v divide by zero\n"));
|
---|
| 185 | if(mNr==0 || mNc==0 || !mNDBlock)
|
---|
| 186 | throw(SzMismatchError("TMatrix::operator/=v mNr==0 || mNc==0 || !mNDBlock\n"));
|
---|
| 187 | *mNDBlock /= b;
|
---|
| 188 | return *this;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | ////////////////////////////////////////////////////////////////
|
---|
| 192 | //**** Surcharge de +=,-=,*= (INPLACE): TMatrix += TMatrix;
|
---|
| 193 |
|
---|
| 194 | template <class T>
|
---|
| 195 | TMatrix<T>& TMatrix<T>::operator += (const TMatrix<T>& a)
|
---|
| 196 | {
|
---|
| 197 | if(mNr==0 || mNc==0 || mNr!=a.mNr || mNc!=a.mNc || !mNDBlock || !a.mNDBlock)
|
---|
| 198 | throw(SzMismatchError("TMatrix::operator+=A size mismatch/null"));
|
---|
| 199 | *(mNDBlock) += *(a.mNDBlock);
|
---|
| 200 | return *this;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | template <class T>
|
---|
| 204 | TMatrix<T>& TMatrix<T>::operator -= (const TMatrix<T>& a)
|
---|
| 205 | {
|
---|
| 206 | if(mNr==0 || mNc==0 || mNr!=a.mNr || mNc!=a.mNc || !mNDBlock || !a.mNDBlock)
|
---|
| 207 | throw(SzMismatchError("TMatrix::operator-=A size mismatch/null"));
|
---|
| 208 | *(mNDBlock) -= *(a.mNDBlock);
|
---|
| 209 | return *this;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | template <class T>
|
---|
| 213 | TMatrix<T>& TMatrix<T>::operator *= (const TMatrix<T>& a)
|
---|
| 214 | // A = A*B -> A(n,m) = A(n,m)*B(m,m)
|
---|
| 215 | {
|
---|
| 216 | uint_4 ndata = mNr*mNc;
|
---|
| 217 | if(ndata==0 || mNc!=a.mNr || a.mNr!=a.mNc || !mNDBlock || !a.mNDBlock)
|
---|
| 218 | throw(SzMismatchError("TMatrix::operator*=A size mismatch/null"));
|
---|
| 219 | // A(i,j) = Sum(k) A(i,k)*B(k,j) ... il faut sauver la ligne "i" de A
|
---|
| 220 | // Vecteur oi : vecteur ou est sauve la ligne i de la matrice *this
|
---|
| 221 | // oi,oe = pointeur de debut et de fin du vecteur temporaire
|
---|
| 222 | // oij = pointeur parcourant le vecteur oi
|
---|
| 223 | // Matrice *this: i = pointeur du debut de la ligne i
|
---|
| 224 | // ij = pointeur parcourant la ligne i
|
---|
| 225 | // Matrice a : aj = pointeur de debut de la colonne j
|
---|
| 226 | // aji = pointeur parcourant la colonne j
|
---|
| 227 | T* oi = new T[mNc]; T* oe = oi+mNc;
|
---|
| 228 | for(T *i=Data(); i<Data()+ndata; i+=mNc) {
|
---|
| 229 | {for(T *oij=oi,*ij=i; oij<oe;) *oij++ = *ij++;}
|
---|
| 230 | {for(T *ij=i,*aj=const_cast<T *>(a.Data()); aj<a.Data()+a.mNc; ij++,aj++) {
|
---|
| 231 | T sum = 0;
|
---|
| 232 | for(T *oij=oi,*aji=aj; oij<oe; oij++,aji+=a.mNc) sum += *oij * *aji;
|
---|
| 233 | *ij = sum;
|
---|
| 234 | }}
|
---|
| 235 | }
|
---|
| 236 | delete [] oi;
|
---|
| 237 | return *this;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | ///////////////////////////////////////////////////////////////
|
---|
| 241 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
| 242 | /*
|
---|
| 243 | #pragma define_template TMatrix<uint_1>
|
---|
| 244 | #pragma define_template TMatrix<uint_2>
|
---|
| 245 | #pragma define_template TMatrix<int_2>
|
---|
| 246 | #pragma define_template TMatrix<int_4>
|
---|
| 247 | #pragma define_template TMatrix<int_8>
|
---|
| 248 | #pragma define_template TMatrix<uint_4>
|
---|
| 249 | #pragma define_template TMatrix<uint_8>
|
---|
| 250 | #pragma define_template TMatrix<r_4>
|
---|
| 251 | */
|
---|
| 252 | #pragma define_template TMatrix<r_8>
|
---|
| 253 | /*
|
---|
| 254 | #pragma define_template TMatrix< complex<float> >
|
---|
| 255 | #pragma define_template TMatrix< complex<double> >
|
---|
| 256 | */
|
---|
| 257 | // Instances des delegues FileIO (PPersist)
|
---|
| 258 | #endif
|
---|
| 259 |
|
---|
| 260 |
|
---|
| 261 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
| 262 | template class TMatrix<uint_1>;
|
---|
| 263 | template class TMatrix<uint_2>;
|
---|
| 264 | template class TMatrix<int_2>;
|
---|
| 265 | template class TMatrix<int_4>;
|
---|
| 266 | template class TMatrix<int_8>;
|
---|
| 267 | template class TMatrix<uint_4>;
|
---|
| 268 | template class TMatrix<uint_8>;
|
---|
| 269 | template class TMatrix<r_4>;
|
---|
| 270 | template class TMatrix<r_8>;
|
---|
| 271 | template class TMatrix< complex<float> >;
|
---|
| 272 | template class TMatrix< complex<double> >;
|
---|
| 273 | // Instances des delegues FileIO (PPersist)
|
---|
| 274 | #endif
|
---|