| 1 | // $Id: tmatrix.cc,v 1.10 2000-04-26 17:55:10 ansari Exp $ | 
|---|
| 2 | //                         C.Magneville          04/99 | 
|---|
| 3 | #include "machdefs.h" | 
|---|
| 4 | #include <stdio.h> | 
|---|
| 5 | #include <stdlib.h> | 
|---|
| 6 | #include "pexceptions.h" | 
|---|
| 7 | #include "tmatrix.h" | 
|---|
| 8 |  | 
|---|
| 9 | /*! | 
|---|
| 10 | \class SOPHYA::TMatrix | 
|---|
| 11 | \ingroup TArray | 
|---|
| 12 | Class of matrixes | 
|---|
| 13 | \sa TArray | 
|---|
| 14 | */ | 
|---|
| 15 |  | 
|---|
| 16 | //////////////////////////////////////////////////////////////// | 
|---|
| 17 | //**** Createur, Destructeur | 
|---|
| 18 | //! Default constructor | 
|---|
| 19 | template <class T> | 
|---|
| 20 | TMatrix<T>::TMatrix() | 
|---|
| 21 | // Constructeur par defaut. | 
|---|
| 22 | : TArray<T>() | 
|---|
| 23 | { | 
|---|
| 24 | ck_memo_vt_ = true; | 
|---|
| 25 | } | 
|---|
| 26 |  | 
|---|
| 27 | //! constructor of a matrix with  r lines et c columns. | 
|---|
| 28 | /*! | 
|---|
| 29 | \param r : number of rows | 
|---|
| 30 | \param c : number of columns | 
|---|
| 31 | \param mm : define the memory mapping type | 
|---|
| 32 | \sa ReSize | 
|---|
| 33 | */ | 
|---|
| 34 | template <class T> | 
|---|
| 35 | TMatrix<T>::TMatrix(uint_4 r,uint_4 c, short mm) | 
|---|
| 36 | // Construit une matrice de r lignes et c colonnes. | 
|---|
| 37 | :  TArray<T>() | 
|---|
| 38 | { | 
|---|
| 39 | if ( (r == 0) || (c == 0) ) | 
|---|
| 40 | throw ParmError("TMatrix<T>::TMatrix(uint_4 r,uint_4 c) NRows or NCols = 0"); | 
|---|
| 41 | ReSize(r, c, mm); | 
|---|
| 42 | } | 
|---|
| 43 |  | 
|---|
| 44 | //! Constructor by copy | 
|---|
| 45 | /*! \sa NDataBlock::NDataBlock(const NDataBlock<T>&) */ | 
|---|
| 46 | template <class T> | 
|---|
| 47 | TMatrix<T>::TMatrix(const TMatrix<T>& a) | 
|---|
| 48 | // Constructeur par copie | 
|---|
| 49 | : TArray<T>(a) | 
|---|
| 50 | { | 
|---|
| 51 | } | 
|---|
| 52 |  | 
|---|
| 53 | //! Constructor by copy | 
|---|
| 54 | /*! | 
|---|
| 55 | \param share : if true, share data. If false copy data | 
|---|
| 56 | */ | 
|---|
| 57 | template <class T> | 
|---|
| 58 | TMatrix<T>::TMatrix(const TMatrix<T>& a, bool share) | 
|---|
| 59 | // Constructeur par copie avec possibilite de forcer le partage ou non. | 
|---|
| 60 | : TArray<T>(a, share) | 
|---|
| 61 | { | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | //! Constructor of a matrix from a TArray \b a | 
|---|
| 65 | template <class T> | 
|---|
| 66 | TMatrix<T>::TMatrix(const TArray<T>& a) | 
|---|
| 67 | : TArray<T>(a) | 
|---|
| 68 | { | 
|---|
| 69 | if (a.NbDimensions() > 2) | 
|---|
| 70 | throw SzMismatchError("TMatrix<T>::TMatrix(const TArray<T>& a) a.NbDimensions()>2 "); | 
|---|
| 71 | if (a.NbDimensions() == 1) { | 
|---|
| 72 | size_[1] = 1; | 
|---|
| 73 | step_[1] = size_[0]*step_[0]; | 
|---|
| 74 | ndim_ = 2; | 
|---|
| 75 | } | 
|---|
| 76 | UpdateMemoryMapping(a, SameMemoryMapping); | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | //! Constructor of a matrix from a TArray \b a | 
|---|
| 80 | /*! | 
|---|
| 81 | \param a : TArray to be copied or shared | 
|---|
| 82 | \param share : if true, share data. If false copy data | 
|---|
| 83 | \param mm : define the memory mapping type | 
|---|
| 84 | */ | 
|---|
| 85 | template <class T> | 
|---|
| 86 | TMatrix<T>::TMatrix(const TArray<T>& a, bool share, short mm ) | 
|---|
| 87 | : TArray<T>(a, share) | 
|---|
| 88 | { | 
|---|
| 89 | if (a.NbDimensions() > 2) | 
|---|
| 90 | throw SzMismatchError("TMatrix<T>::TMatrix(const TArray<T>& a, ...) a.NbDimensions()>2"); | 
|---|
| 91 | if (a.NbDimensions() == 1) { | 
|---|
| 92 | size_[1] = 1; | 
|---|
| 93 | step_[1] = size_[0]*step_[0]; | 
|---|
| 94 | ndim_ = 2; | 
|---|
| 95 | } | 
|---|
| 96 | UpdateMemoryMapping(a, mm); | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | //! Destructor | 
|---|
| 100 | template <class T> | 
|---|
| 101 | TMatrix<T>::~TMatrix() | 
|---|
| 102 | { | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | //! Set matirx equal to \b a and return *this | 
|---|
| 106 | template <class T> | 
|---|
| 107 | TArray<T>& TMatrix<T>::Set(const TArray<T>& a) | 
|---|
| 108 | { | 
|---|
| 109 | if (a.NbDimensions() > 2) | 
|---|
| 110 | throw SzMismatchError("TMatrix<T>::Set(const TArray<T>& a) a.NbDimensions() > 2"); | 
|---|
| 111 | TArray<T>::Set(a); | 
|---|
| 112 | if (NbDimensions() == 1) { | 
|---|
| 113 | size_[1] = 1; | 
|---|
| 114 | step_[1] = size_[0]*step_[0]; | 
|---|
| 115 | ndim_ = 2; | 
|---|
| 116 | } | 
|---|
| 117 | UpdateMemoryMapping(*this, SameMemoryMapping); | 
|---|
| 118 | return(*this); | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | //! Resize the matrix | 
|---|
| 122 | /*! | 
|---|
| 123 | \param r : number of rows | 
|---|
| 124 | \param c : number of columns | 
|---|
| 125 | \param mm : define the memory mapping type | 
|---|
| 126 | (SameMemoryMapping,CMemoryMapping | 
|---|
| 127 | ,FortranMemoryMapping,DefaultMemoryMapping) | 
|---|
| 128 | */ | 
|---|
| 129 | template <class T> | 
|---|
| 130 | void TMatrix<T>::ReSize(uint_4 r, uint_4 c, short mm) | 
|---|
| 131 | { | 
|---|
| 132 | if(r==0||c==0) | 
|---|
| 133 | throw(SzMismatchError("TMatrix::ReSize r or c==0 ")); | 
|---|
| 134 | uint_4 size[BASEARRAY_MAXNDIMS]; | 
|---|
| 135 | for(int kk=0; kk<BASEARRAY_MAXNDIMS; kk++)  size[kk] = 0; | 
|---|
| 136 | if (mm == SameMemoryMapping) mm = GetMemoryMapping(); | 
|---|
| 137 | else if ( (mm != CMemoryMapping) && (mm != FortranMemoryMapping) ) | 
|---|
| 138 | mm = GetDefaultMemoryMapping(); | 
|---|
| 139 | if (mm == CMemoryMapping) { | 
|---|
| 140 | size[0] = c;  size[1] = r; | 
|---|
| 141 | } | 
|---|
| 142 | else { | 
|---|
| 143 | size[0] = r;  size[1] = c; | 
|---|
| 144 | } | 
|---|
| 145 | TArray<T>::ReSize(2, size, 1); | 
|---|
| 146 | UpdateMemoryMapping(mm); | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|
| 149 | //! Re-allocate space for the matrix | 
|---|
| 150 | /*! | 
|---|
| 151 | \param r : number of rows | 
|---|
| 152 | \param c : number of columns | 
|---|
| 153 | \param mm : define the memory mapping type | 
|---|
| 154 | \param force : if true re-allocation is forced, if not it occurs | 
|---|
| 155 | only if the required space is greater than the old one. | 
|---|
| 156 | \sa ReSize | 
|---|
| 157 | */ | 
|---|
| 158 | template <class T> | 
|---|
| 159 | void TMatrix<T>::Realloc(uint_4 r,uint_4 c, short mm, bool force) | 
|---|
| 160 | { | 
|---|
| 161 | if(r==0||c==0) | 
|---|
| 162 | throw(SzMismatchError("TMatrix::Realloc r or c==0 ")); | 
|---|
| 163 | uint_4 size[BASEARRAY_MAXNDIMS]; | 
|---|
| 164 | for(int kk=0; kk<BASEARRAY_MAXNDIMS; kk++)  size[kk] = 0; | 
|---|
| 165 | if (mm == SameMemoryMapping) mm = GetMemoryMapping(); | 
|---|
| 166 | else if ( (mm != CMemoryMapping) && (mm != FortranMemoryMapping) ) | 
|---|
| 167 | mm = GetDefaultMemoryMapping(); | 
|---|
| 168 | if (mm == CMemoryMapping) { | 
|---|
| 169 | size[0] = c;  size[1] = r; | 
|---|
| 170 | } | 
|---|
| 171 | else { | 
|---|
| 172 | size[0] = r;  size[1] = c; | 
|---|
| 173 | } | 
|---|
| 174 | TArray<T>::Realloc(2, size, 1, force); | 
|---|
| 175 | UpdateMemoryMapping(mm); | 
|---|
| 176 | } | 
|---|
| 177 |  | 
|---|
| 178 | // $CHECK$ Reza 03/2000  Doit-on declarer cette methode const ? | 
|---|
| 179 | //! Return a submatrix define by \b Range \b rline and \b rcol | 
|---|
| 180 | template <class T> | 
|---|
| 181 | TMatrix<T> TMatrix<T>::SubMatrix(Range rline, Range rcol) const | 
|---|
| 182 | { | 
|---|
| 183 | short mm = GetMemoryMapping(); | 
|---|
| 184 | Range rx, ry; | 
|---|
| 185 | if (mm == CMemoryMapping)  { rx = rcol;  ry = rline; } | 
|---|
| 186 | else { ry = rcol;  rx = rline; } | 
|---|
| 187 | TMatrix sm(SubArray(rx, ry, Range(0), Range(0), Range(0)),true, mm); | 
|---|
| 188 | sm.UpdateMemoryMapping(mm); | 
|---|
| 189 | sm.SetTemp(true); | 
|---|
| 190 | return(sm); | 
|---|
| 191 | } | 
|---|
| 192 |  | 
|---|
| 193 | //////////////////////////////////////////////////////////////// | 
|---|
| 194 | // Transposition | 
|---|
| 195 | //! Transpose matrix in place | 
|---|
| 196 | template <class T> | 
|---|
| 197 | TMatrix<T>& TMatrix<T>::Transpose() | 
|---|
| 198 | { | 
|---|
| 199 | short vt = (marowi_ == veceli_) ? ColumnVector : RowVector; | 
|---|
| 200 | uint_4 rci = macoli_; | 
|---|
| 201 | macoli_ = marowi_; | 
|---|
| 202 | marowi_ = rci; | 
|---|
| 203 | veceli_ = (vt ==  ColumnVector ) ?  marowi_ : macoli_; | 
|---|
| 204 | return(*this); | 
|---|
| 205 | } | 
|---|
| 206 |  | 
|---|
| 207 |  | 
|---|
| 208 | //! Transpose matrix into new matrix | 
|---|
| 209 | /*! | 
|---|
| 210 | \param mm : define the memory mapping type | 
|---|
| 211 | (SameMemoryMapping,CMemoryMapping,FortranMemoryMapping) | 
|---|
| 212 | \return return a new matrix | 
|---|
| 213 | */ | 
|---|
| 214 | template <class T> | 
|---|
| 215 | TMatrix<T> TMatrix<T>::Transpose(short mm) | 
|---|
| 216 | { | 
|---|
| 217 | if (mm == SameMemoryMapping) mm = GetMemoryMapping(); | 
|---|
| 218 | TMatrix<T> tm(NCols(), NRows(), mm); | 
|---|
| 219 | for(uint_4 i=0; i<NRows(); i++) | 
|---|
| 220 | for(uint_4 j=0; j<NCols(); j++) | 
|---|
| 221 | tm(j,i) = (*this)(i,j); | 
|---|
| 222 | tm.SetTemp(true); | 
|---|
| 223 | return tm; | 
|---|
| 224 | } | 
|---|
| 225 |  | 
|---|
| 226 | //! Rearrange data in memory memoire according to \b mm | 
|---|
| 227 | /*! | 
|---|
| 228 | \param mm : define the memory mapping type | 
|---|
| 229 | (SameMemoryMapping,CMemoryMapping,FortranMemoryMapping) | 
|---|
| 230 | \warning If identical, return a matrix that share the datas | 
|---|
| 231 | */ | 
|---|
| 232 | template <class T> | 
|---|
| 233 | TMatrix<T> TMatrix<T>::Rearrange(short mm) | 
|---|
| 234 | { | 
|---|
| 235 | if ( mm == SameMemoryMapping)  mm = GetMemoryMapping(); | 
|---|
| 236 | else if ( (mm != CMemoryMapping) && (mm != FortranMemoryMapping) ) | 
|---|
| 237 | mm = GetDefaultMemoryMapping(); | 
|---|
| 238 |  | 
|---|
| 239 | if  (mm == GetMemoryMapping()) | 
|---|
| 240 | return (TMatrix<T>(*this, true)); | 
|---|
| 241 |  | 
|---|
| 242 | TMatrix<T> tm(NRows(), NCols(), mm); | 
|---|
| 243 | for(uint_4 i=0; i<NRows(); i++) | 
|---|
| 244 | for(uint_4 j=0; j<NCols(); j++) | 
|---|
| 245 | tm(i,j) = (*this)(i,j); | 
|---|
| 246 | tm.SetTemp(true); | 
|---|
| 247 | return tm; | 
|---|
| 248 | } | 
|---|
| 249 |  | 
|---|
| 250 | //! Set the matrix to the identity matrix \b imx | 
|---|
| 251 | template <class T> | 
|---|
| 252 | TMatrix<T>& TMatrix<T>::SetIdentity(IdentityMatrix imx) | 
|---|
| 253 | { | 
|---|
| 254 | if (ndim_ == 0) { | 
|---|
| 255 | uint_4 sz = imx.Size(); | 
|---|
| 256 | if (sz < 1) sz = 1; | 
|---|
| 257 | ReSize(sz, sz); | 
|---|
| 258 | } | 
|---|
| 259 | T diag = (T)imx.Diag(); | 
|---|
| 260 | if (NRows() != NCols()) | 
|---|
| 261 | throw SzMismatchError("TMatrix::operator= (IdentityMatrix) NRows() != NCols()") ; | 
|---|
| 262 | for(uint_4 i=0; i<NRows(); i++) (*this)(i,i) = diag; | 
|---|
| 263 |  | 
|---|
| 264 | return (*this); | 
|---|
| 265 | } | 
|---|
| 266 |  | 
|---|
| 267 |  | 
|---|
| 268 |  | 
|---|
| 269 | //////////////////////////////////////////////////////////////// | 
|---|
| 270 | //**** Impression | 
|---|
| 271 | //! Return info on number of rows, column and type \b T | 
|---|
| 272 | template <class T> | 
|---|
| 273 | string TMatrix<T>::InfoString() const | 
|---|
| 274 | { | 
|---|
| 275 | string rs = "TMatrix<"; | 
|---|
| 276 | rs += typeid(T).name(); | 
|---|
| 277 | char buff[64]; | 
|---|
| 278 | sprintf(buff, ">(NRows=%ld, NCols=%ld)", (long)NRows(), (long)NCols()); | 
|---|
| 279 | rs += buff; | 
|---|
| 280 | return(rs); | 
|---|
| 281 | } | 
|---|
| 282 |  | 
|---|
| 283 | //! Print matrix | 
|---|
| 284 | /*! | 
|---|
| 285 | \param maxprt : maximum numer of print | 
|---|
| 286 | \param si : if true,  display attached DvList | 
|---|
| 287 | \sa SetMaxPrint | 
|---|
| 288 | */ | 
|---|
| 289 | template <class T> | 
|---|
| 290 | void TMatrix<T>::Print(ostream& os, int_4 maxprt, bool si) const | 
|---|
| 291 | { | 
|---|
| 292 | if (maxprt < 0)  maxprt = max_nprt_; | 
|---|
| 293 | uint_4 npr = 0; | 
|---|
| 294 | Show(os, si); | 
|---|
| 295 | if (ndim_ < 1)  return; | 
|---|
| 296 | uint_4 kc,kr; | 
|---|
| 297 | for(kr=0; kr<size_[marowi_]; kr++) { | 
|---|
| 298 | if ( (size_[marowi_] > 1) && (size_[macoli_] > 10) ) cout << "----- Ligne Line= " << kr << endl; | 
|---|
| 299 | for(kc=0; kc<size_[macoli_]; kc++) { | 
|---|
| 300 | if(kc > 0) os << ", "; | 
|---|
| 301 | os << (*this)(kr, kc);   npr++; | 
|---|
| 302 | if (npr >= (uint_4) maxprt) { | 
|---|
| 303 | if (npr < totsize_)  os << "\n     .... " << endl; return; | 
|---|
| 304 | } | 
|---|
| 305 | } | 
|---|
| 306 | os << endl; | 
|---|
| 307 | } | 
|---|
| 308 | os << endl; | 
|---|
| 309 | } | 
|---|
| 310 |  | 
|---|
| 311 | //////////////////////////////////////////////////////////////// | 
|---|
| 312 | //****  Multiplication matricielle  ***** | 
|---|
| 313 | //////////////////////////////////////////////////////////////// | 
|---|
| 314 |  | 
|---|
| 315 | //! Return the matrix product C = (*this)*B | 
|---|
| 316 | /*! | 
|---|
| 317 | \param mm : define the memory mapping type for the return matrix | 
|---|
| 318 | */ | 
|---|
| 319 | template <class T> | 
|---|
| 320 | TMatrix<T> TMatrix<T>::Multiply(const TMatrix<T>& b, short mm) const | 
|---|
| 321 | { | 
|---|
| 322 | if (NCols() != b.NRows()) | 
|---|
| 323 | throw(SzMismatchError("TMatrix<T>::Multiply(b) NCols() != b.NRows() ") ); | 
|---|
| 324 | if (mm == SameMemoryMapping) mm = GetMemoryMapping(); | 
|---|
| 325 | TMatrix<T> rm(NRows(), b.NCols(), mm); | 
|---|
| 326 |  | 
|---|
| 327 | const T * pea; | 
|---|
| 328 | const T * peb; | 
|---|
| 329 | T sum; | 
|---|
| 330 | uint_4 r,c,k; | 
|---|
| 331 | uint_4 stepa = Step(ColsKA()); | 
|---|
| 332 | uint_4 stepb = b.Step(RowsKA()); | 
|---|
| 333 | // Calcul de C=rm = A*B   (A=*this) | 
|---|
| 334 | for(r=0; r<rm.NRows(); r++)      // Boucle sur les lignes de A | 
|---|
| 335 | for(c=0; c<rm.NCols(); c++) {     // Boucle sur les colonnes de B | 
|---|
| 336 | sum = 0; | 
|---|
| 337 | pea = &((*this)(r,0));       // 1er element de la ligne r de A | 
|---|
| 338 | peb = &(b(0,c));                // 1er element de la colonne c de B | 
|---|
| 339 | for(k=0; k<NCols(); k++)  sum += pea[k*stepa]*peb[k*stepb]; | 
|---|
| 340 | rm(r,c) = sum; | 
|---|
| 341 | } | 
|---|
| 342 |  | 
|---|
| 343 | rm.SetTemp(true); | 
|---|
| 344 | return rm; | 
|---|
| 345 | } | 
|---|
| 346 |  | 
|---|
| 347 | /////////////////////////////////////////////////////////////// | 
|---|
| 348 | #ifdef __CXX_PRAGMA_TEMPLATES__ | 
|---|
| 349 | #pragma define_template TMatrix<uint_2> | 
|---|
| 350 | #pragma define_template TMatrix<int_4> | 
|---|
| 351 | #pragma define_template TMatrix<int_8> | 
|---|
| 352 | #pragma define_template TMatrix<r_4> | 
|---|
| 353 | #pragma define_template TMatrix<r_8> | 
|---|
| 354 | #pragma define_template TMatrix< complex<r_4> > | 
|---|
| 355 | #pragma define_template TMatrix< complex<r_8> > | 
|---|
| 356 | #endif | 
|---|
| 357 |  | 
|---|
| 358 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES) | 
|---|
| 359 | template class TMatrix<uint_2>; | 
|---|
| 360 | template class TMatrix<int_4>; | 
|---|
| 361 | template class TMatrix<int_8>; | 
|---|
| 362 | template class TMatrix<r_4>; | 
|---|
| 363 | template class TMatrix<r_8>; | 
|---|
| 364 | template class TMatrix< complex<r_4> >; | 
|---|
| 365 | template class TMatrix< complex<r_8> >; | 
|---|
| 366 | #endif | 
|---|