[967] | 1 | // $Id: tmatrix.cc,v 1.9 2000-04-21 16:31:25 ansari Exp $
|
---|
[762] | 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 |
|
---|
[926] | 9 | /*!
|
---|
| 10 | \class SOPHYA::TMatrix
|
---|
| 11 | \ingroup TArray
|
---|
| 12 | Class of matrixes
|
---|
| 13 | \sa TArray
|
---|
| 14 | */
|
---|
[804] | 15 |
|
---|
[762] | 16 | ////////////////////////////////////////////////////////////////
|
---|
| 17 | //**** Createur, Destructeur
|
---|
[894] | 18 | //! Default constructor
|
---|
[762] | 19 | template <class T>
|
---|
| 20 | TMatrix<T>::TMatrix()
|
---|
| 21 | // Constructeur par defaut.
|
---|
[804] | 22 | : TArray<T>()
|
---|
[762] | 23 | {
|
---|
[813] | 24 | ck_memo_vt_ = true;
|
---|
[762] | 25 | }
|
---|
| 26 |
|
---|
[894] | 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 | */
|
---|
[762] | 34 | template <class T>
|
---|
[804] | 35 | TMatrix<T>::TMatrix(uint_4 r,uint_4 c, short mm)
|
---|
[762] | 36 | // Construit une matrice de r lignes et c colonnes.
|
---|
[804] | 37 | : TArray<T>()
|
---|
[762] | 38 | {
|
---|
[804] | 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);
|
---|
[762] | 42 | }
|
---|
| 43 |
|
---|
[967] | 44 | //! Constructor by copy
|
---|
| 45 | /*! \sa NDataBlock::NDataBlock(const NDataBlock<T>&) */
|
---|
[762] | 46 | template <class T>
|
---|
| 47 | TMatrix<T>::TMatrix(const TMatrix<T>& a)
|
---|
[967] | 48 | // Constructeur par copie
|
---|
[804] | 49 | : TArray<T>(a)
|
---|
[762] | 50 | {
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[894] | 53 | //! Constructor by copy
|
---|
| 54 | /*!
|
---|
| 55 | \param share : if true, share data. If false copy data
|
---|
| 56 | */
|
---|
[762] | 57 | template <class T>
|
---|
[804] | 58 | TMatrix<T>::TMatrix(const TMatrix<T>& a, bool share)
|
---|
[762] | 59 | // Constructeur par copie avec possibilite de forcer le partage ou non.
|
---|
[804] | 60 | : TArray<T>(a, share)
|
---|
[762] | 61 | {
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[894] | 64 | //! Constructor of a matrix from a TArray \b a
|
---|
[762] | 65 | template <class T>
|
---|
[804] | 66 | TMatrix<T>::TMatrix(const TArray<T>& a)
|
---|
| 67 | : TArray<T>(a)
|
---|
[762] | 68 | {
|
---|
[813] | 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);
|
---|
[762] | 77 | }
|
---|
| 78 |
|
---|
[894] | 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 | */
|
---|
[762] | 85 | template <class T>
|
---|
[804] | 86 | TMatrix<T>::TMatrix(const TArray<T>& a, bool share, short mm )
|
---|
| 87 | : TArray<T>(a, share)
|
---|
[762] | 88 | {
|
---|
[813] | 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 | }
|
---|
[804] | 96 | UpdateMemoryMapping(a, mm);
|
---|
[762] | 97 | }
|
---|
| 98 |
|
---|
[894] | 99 | //! Destructor
|
---|
[762] | 100 | template <class T>
|
---|
[804] | 101 | TMatrix<T>::~TMatrix()
|
---|
[762] | 102 | {
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[894] | 105 | //! Set matirx equal to \b a and return *this
|
---|
[804] | 106 | template <class T>
|
---|
| 107 | TArray<T>& TMatrix<T>::Set(const TArray<T>& a)
|
---|
[762] | 108 | {
|
---|
[813] | 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 (a.NbDimensions() == 1) {
|
---|
| 113 | size_[1] = 1;
|
---|
| 114 | step_[1] = size_[0]*step_[0];
|
---|
| 115 | ndim_ = 2;
|
---|
| 116 | }
|
---|
| 117 | UpdateMemoryMapping(a, SameMemoryMapping);
|
---|
| 118 | return(*this);
|
---|
[762] | 119 | }
|
---|
| 120 |
|
---|
[894] | 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 | */
|
---|
[804] | 129 | template <class T>
|
---|
| 130 | void TMatrix<T>::ReSize(uint_4 r, uint_4 c, short mm)
|
---|
[762] | 131 | {
|
---|
[804] | 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();
|
---|
[813] | 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 | }
|
---|
[804] | 145 | TArray<T>::ReSize(2, size, 1);
|
---|
[813] | 146 | UpdateMemoryMapping(mm);
|
---|
[762] | 147 | }
|
---|
| 148 |
|
---|
[894] | 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 | */
|
---|
[762] | 158 | template <class T>
|
---|
[804] | 159 | void TMatrix<T>::Realloc(uint_4 r,uint_4 c, short mm, bool force)
|
---|
[762] | 160 | {
|
---|
[804] | 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;
|
---|
[813] | 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 | }
|
---|
[804] | 174 | TArray<T>::Realloc(2, size, 1, force);
|
---|
[813] | 175 | UpdateMemoryMapping(mm);
|
---|
[762] | 176 | }
|
---|
| 177 |
|
---|
[804] | 178 | // $CHECK$ Reza 03/2000 Doit-on declarer cette methode const ?
|
---|
[894] | 179 | //! Return a submatrix define by \b Range \b rline and \b rcol
|
---|
[762] | 180 | template <class T>
|
---|
[813] | 181 | TMatrix<T> TMatrix<T>::SubMatrix(Range rline, Range rcol) const
|
---|
[762] | 182 | {
|
---|
[813] | 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);
|
---|
[762] | 191 | }
|
---|
| 192 |
|
---|
[804] | 193 | ////////////////////////////////////////////////////////////////
|
---|
| 194 | // Transposition
|
---|
[894] | 195 | //! Transpose matrix in place
|
---|
[762] | 196 | template <class T>
|
---|
[804] | 197 | TMatrix<T>& TMatrix<T>::Transpose()
|
---|
| 198 | {
|
---|
[813] | 199 | short vt = (marowi_ == veceli_) ? ColumnVector : RowVector;
|
---|
[804] | 200 | uint_4 rci = macoli_;
|
---|
| 201 | macoli_ = marowi_;
|
---|
| 202 | marowi_ = rci;
|
---|
[813] | 203 | veceli_ = (vt == ColumnVector ) ? marowi_ : macoli_;
|
---|
[804] | 204 | return(*this);
|
---|
[762] | 205 | }
|
---|
| 206 |
|
---|
| 207 |
|
---|
[894] | 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 | */
|
---|
[762] | 214 | template <class T>
|
---|
[804] | 215 | TMatrix<T> TMatrix<T>::Transpose(short mm)
|
---|
[762] | 216 | {
|
---|
[804] | 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;
|
---|
[762] | 224 | }
|
---|
| 225 |
|
---|
[894] | 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 | */
|
---|
[762] | 232 | template <class T>
|
---|
[804] | 233 | TMatrix<T> TMatrix<T>::Rearrange(short mm)
|
---|
[762] | 234 | {
|
---|
[813] | 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 |
|
---|
[804] | 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;
|
---|
[762] | 248 | }
|
---|
| 249 |
|
---|
[894] | 250 | //! Set the matrix to the identity matrix \b imx
|
---|
[762] | 251 | template <class T>
|
---|
[804] | 252 | TMatrix<T>& TMatrix<T>::SetIdentity(IdentityMatrix imx)
|
---|
[762] | 253 | {
|
---|
[804] | 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;
|
---|
[762] | 263 |
|
---|
[804] | 264 | return (*this);
|
---|
[762] | 265 | }
|
---|
| 266 |
|
---|
[804] | 267 |
|
---|
| 268 |
|
---|
| 269 | ////////////////////////////////////////////////////////////////
|
---|
| 270 | //**** Impression
|
---|
[894] | 271 | //! Return info on number of rows, column and type \b T
|
---|
[762] | 272 | template <class T>
|
---|
[813] | 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 |
|
---|
[894] | 283 | //! Print matrix
|
---|
| 284 | /*!
|
---|
| 285 | \param maxprt : maximum numer of print
|
---|
| 286 | \param si : if true, display attached DvList
|
---|
| 287 | \sa SetMaxPrint
|
---|
| 288 | */
|
---|
[813] | 289 | template <class T>
|
---|
[804] | 290 | void TMatrix<T>::Print(ostream& os, int_4 maxprt, bool si) const
|
---|
[762] | 291 | {
|
---|
[804] | 292 | if (maxprt < 0) maxprt = max_nprt_;
|
---|
[958] | 293 | uint_4 npr = 0;
|
---|
[804] | 294 | Show(os, si);
|
---|
[850] | 295 | if (ndim_ < 1) return;
|
---|
[804] | 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++;
|
---|
[958] | 302 | if (npr >= (uint_4) maxprt) {
|
---|
[804] | 303 | if (npr < totsize_) os << "\n .... " << endl; return;
|
---|
| 304 | }
|
---|
| 305 | }
|
---|
| 306 | os << endl;
|
---|
| 307 | }
|
---|
[813] | 308 | os << endl;
|
---|
[762] | 309 | }
|
---|
| 310 |
|
---|
| 311 | ////////////////////////////////////////////////////////////////
|
---|
[804] | 312 | //**** Multiplication matricielle *****
|
---|
[762] | 313 | ////////////////////////////////////////////////////////////////
|
---|
| 314 |
|
---|
[894] | 315 | //! Return the matrix product C = (*this)*B
|
---|
| 316 | /*!
|
---|
| 317 | \param mm : define the memory mapping type for the return matrix
|
---|
| 318 | */
|
---|
[804] | 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);
|
---|
[762] | 326 |
|
---|
[804] | 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 |
|
---|
[762] | 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>
|
---|
[804] | 353 | #pragma define_template TMatrix<r_8>
|
---|
[762] | 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
|
---|