[2615] | 1 | // $Id: tmatrix.cc,v 1.29 2004-09-10 09:55:07 cmv Exp $
|
---|
[762] | 2 | // C.Magneville 04/99
|
---|
[2615] | 3 | #include "sopnamsp.h"
|
---|
[762] | 4 | #include "machdefs.h"
|
---|
| 5 | #include <stdio.h>
|
---|
| 6 | #include <stdlib.h>
|
---|
| 7 | #include "pexceptions.h"
|
---|
| 8 | #include "tmatrix.h"
|
---|
| 9 |
|
---|
[926] | 10 | /*!
|
---|
| 11 | \class SOPHYA::TMatrix
|
---|
| 12 | \ingroup TArray
|
---|
[2267] | 13 |
|
---|
| 14 | The TMatrix class specializes the TArray class for representing
|
---|
| 15 | two dimensional arrays as matrices. Matrix and vector operations,
|
---|
| 16 | such as matrix multiplication or transposition is implemented.
|
---|
| 17 | \b Matrix is a typedef for double precision floating point matrix ( TMatrix<r_8> ).
|
---|
| 18 |
|
---|
| 19 | \sa SOPHYA::TArray SOPHYA::TVector
|
---|
| 20 | \sa SOPHYA::Range \sa SOPHYA::Sequence
|
---|
| 21 | \sa SOPHYA::MathArray \sa SOPHYA::SimpleMatrixOperation
|
---|
| 22 |
|
---|
| 23 | The following sample code illustrates vector-matrix multiplication
|
---|
| 24 | and matrix inversion, using simple gauss inversion.
|
---|
| 25 | \code
|
---|
| 26 | #include "array.h"
|
---|
| 27 | // ....
|
---|
| 28 | int n = 5; // Size of matrix and vectors here
|
---|
| 29 | Matrix a(n,n);
|
---|
| 30 | a = RandomSequence(RandomSequence::Gaussian, 0., 2.5);
|
---|
| 31 | Vector x(n);
|
---|
| 32 | x = RegularSequence(1.,3.);
|
---|
| 33 | Vector b = a*x;
|
---|
| 34 | cout << " ----- Vector x = \n " << x << endl;
|
---|
| 35 | cout << " ----- Vector b = a*x = \n " << b << endl;
|
---|
| 36 | SimpleMatrixOperation<r_8> smo;
|
---|
| 37 | Matrix inva = smo.Inverse(a);
|
---|
| 38 | cout << " ----- Matrix Inverse(a) = \n " << inva << endl;
|
---|
| 39 | cout << " ----- Matrix a*Inverse(a) = \n " << inva*a << endl;
|
---|
| 40 | cout << " ----- Matrix Inverse(a)*b (=Inv(a)*a*x) = \n " << inva*b << endl;
|
---|
| 41 | cout << " ----- Matrix x-Inverse(a)*b = (=0 ?)\n " << x-inva*b << endl;
|
---|
| 42 | \endcode
|
---|
| 43 |
|
---|
[926] | 44 | */
|
---|
[804] | 45 |
|
---|
[762] | 46 | ////////////////////////////////////////////////////////////////
|
---|
| 47 | //**** Createur, Destructeur
|
---|
[894] | 48 | //! Default constructor
|
---|
[762] | 49 | template <class T>
|
---|
| 50 | TMatrix<T>::TMatrix()
|
---|
| 51 | // Constructeur par defaut.
|
---|
[804] | 52 | : TArray<T>()
|
---|
[762] | 53 | {
|
---|
[1099] | 54 | arrtype_ = 1; // Type = Matrix
|
---|
[762] | 55 | }
|
---|
| 56 |
|
---|
[894] | 57 | //! constructor of a matrix with r lines et c columns.
|
---|
| 58 | /*!
|
---|
| 59 | \param r : number of rows
|
---|
| 60 | \param c : number of columns
|
---|
| 61 | \param mm : define the memory mapping type
|
---|
[2575] | 62 | \param fzero : if \b true , set matrix elements to zero
|
---|
[894] | 63 | \sa ReSize
|
---|
| 64 | */
|
---|
[762] | 65 | template <class T>
|
---|
[2575] | 66 | TMatrix<T>::TMatrix(sa_size_t r,sa_size_t c, short mm, bool fzero)
|
---|
[762] | 67 | // Construit une matrice de r lignes et c colonnes.
|
---|
[804] | 68 | : TArray<T>()
|
---|
[762] | 69 | {
|
---|
[804] | 70 | if ( (r == 0) || (c == 0) )
|
---|
[1156] | 71 | throw ParmError("TMatrix<T>::TMatrix(sa_size_t r,sa_size_t c) NRows or NCols = 0");
|
---|
[1099] | 72 | arrtype_ = 1; // Type = Matrix
|
---|
[2575] | 73 | ReSize(r, c, mm, fzero);
|
---|
[762] | 74 | }
|
---|
| 75 |
|
---|
[967] | 76 | //! Constructor by copy
|
---|
[976] | 77 | /*!
|
---|
| 78 | \warning datas are \b SHARED with \b a.
|
---|
| 79 | \sa NDataBlock::NDataBlock(const NDataBlock<T>&)
|
---|
| 80 | */
|
---|
[762] | 81 | template <class T>
|
---|
| 82 | TMatrix<T>::TMatrix(const TMatrix<T>& a)
|
---|
[967] | 83 | // Constructeur par copie
|
---|
[804] | 84 | : TArray<T>(a)
|
---|
[762] | 85 | {
|
---|
[1099] | 86 | arrtype_ = 1; // Type = Matrix
|
---|
[1103] | 87 | UpdateMemoryMapping(a, SameMemoryMapping);
|
---|
[762] | 88 | }
|
---|
| 89 |
|
---|
[894] | 90 | //! Constructor by copy
|
---|
| 91 | /*!
|
---|
| 92 | \param share : if true, share data. If false copy data
|
---|
| 93 | */
|
---|
[762] | 94 | template <class T>
|
---|
[804] | 95 | TMatrix<T>::TMatrix(const TMatrix<T>& a, bool share)
|
---|
[762] | 96 | // Constructeur par copie avec possibilite de forcer le partage ou non.
|
---|
[804] | 97 | : TArray<T>(a, share)
|
---|
[762] | 98 | {
|
---|
[1099] | 99 | arrtype_ = 1; // Type = Matrix
|
---|
[1103] | 100 | UpdateMemoryMapping(a, SameMemoryMapping);
|
---|
[762] | 101 | }
|
---|
| 102 |
|
---|
[894] | 103 | //! Constructor of a matrix from a TArray \b a
|
---|
[762] | 104 | template <class T>
|
---|
[804] | 105 | TMatrix<T>::TMatrix(const TArray<T>& a)
|
---|
| 106 | : TArray<T>(a)
|
---|
[762] | 107 | {
|
---|
[813] | 108 | if (a.NbDimensions() > 2)
|
---|
| 109 | throw SzMismatchError("TMatrix<T>::TMatrix(const TArray<T>& a) a.NbDimensions()>2 ");
|
---|
| 110 | if (a.NbDimensions() == 1) {
|
---|
| 111 | size_[1] = 1;
|
---|
| 112 | step_[1] = size_[0]*step_[0];
|
---|
| 113 | ndim_ = 2;
|
---|
| 114 | }
|
---|
[1099] | 115 | arrtype_ = 1; // Type = Matrix
|
---|
[813] | 116 | UpdateMemoryMapping(a, SameMemoryMapping);
|
---|
[762] | 117 | }
|
---|
| 118 |
|
---|
[894] | 119 | //! Constructor of a matrix from a TArray \b a
|
---|
| 120 | /*!
|
---|
| 121 | \param a : TArray to be copied or shared
|
---|
| 122 | \param share : if true, share data. If false copy data
|
---|
| 123 | \param mm : define the memory mapping type
|
---|
| 124 | */
|
---|
[762] | 125 | template <class T>
|
---|
[804] | 126 | TMatrix<T>::TMatrix(const TArray<T>& a, bool share, short mm )
|
---|
| 127 | : TArray<T>(a, share)
|
---|
[762] | 128 | {
|
---|
[813] | 129 | if (a.NbDimensions() > 2)
|
---|
| 130 | throw SzMismatchError("TMatrix<T>::TMatrix(const TArray<T>& a, ...) a.NbDimensions()>2");
|
---|
| 131 | if (a.NbDimensions() == 1) {
|
---|
| 132 | size_[1] = 1;
|
---|
| 133 | step_[1] = size_[0]*step_[0];
|
---|
| 134 | ndim_ = 2;
|
---|
| 135 | }
|
---|
[1099] | 136 | arrtype_ = 1; // Type = Matrix
|
---|
[804] | 137 | UpdateMemoryMapping(a, mm);
|
---|
[762] | 138 | }
|
---|
| 139 |
|
---|
[1099] | 140 | //! Constructor of a matrix from a TArray \b a , with a different data type
|
---|
[1081] | 141 | template <class T>
|
---|
| 142 | TMatrix<T>::TMatrix(const BaseArray& a)
|
---|
| 143 | : TArray<T>()
|
---|
| 144 | {
|
---|
[1099] | 145 | arrtype_ = 1; // Type = Matrix
|
---|
[1103] | 146 | UpdateMemoryMapping(a, SameMemoryMapping);
|
---|
[1081] | 147 | SetBA(a);
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 |
|
---|
| 151 |
|
---|
[894] | 152 | //! Destructor
|
---|
[762] | 153 | template <class T>
|
---|
[804] | 154 | TMatrix<T>::~TMatrix()
|
---|
[762] | 155 | {
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[976] | 158 | //! Set matrix equal to \b a and return *this
|
---|
| 159 | /*!
|
---|
| 160 | \warning Datas are copied (cloned) from \b a.
|
---|
| 161 | \sa NDataBlock::operator=(const NDataBlock<T>&)
|
---|
| 162 | */
|
---|
[804] | 163 | template <class T>
|
---|
| 164 | TArray<T>& TMatrix<T>::Set(const TArray<T>& a)
|
---|
[762] | 165 | {
|
---|
[813] | 166 | if (a.NbDimensions() > 2)
|
---|
| 167 | throw SzMismatchError("TMatrix<T>::Set(const TArray<T>& a) a.NbDimensions() > 2");
|
---|
[1099] | 168 | if ((arrtype_ == 2) && (a.NbDimensions() > 1) && (a.Size(0) > 1) && (a.Size(1) > 1) )
|
---|
| 169 | throw SzMismatchError("TMatrix<T>::Set(const TArray<T>& a) Size(0,1)>1 for Vector");
|
---|
[813] | 170 | TArray<T>::Set(a);
|
---|
[970] | 171 | if (NbDimensions() == 1) {
|
---|
[813] | 172 | size_[1] = 1;
|
---|
| 173 | step_[1] = size_[0]*step_[0];
|
---|
| 174 | ndim_ = 2;
|
---|
| 175 | }
|
---|
[970] | 176 | UpdateMemoryMapping(*this, SameMemoryMapping);
|
---|
[813] | 177 | return(*this);
|
---|
[762] | 178 | }
|
---|
| 179 |
|
---|
[1081] | 180 | template <class T>
|
---|
| 181 | TArray<T>& TMatrix<T>::SetBA(const BaseArray& a)
|
---|
| 182 | {
|
---|
| 183 | if (a.NbDimensions() > 2)
|
---|
| 184 | throw SzMismatchError("TMatrix<T>::SetBA(const BaseArray& a) a.NbDimensions() > 2");
|
---|
[1099] | 185 | if ((arrtype_ == 2) && (a.NbDimensions() > 1) && (a.Size(0) > 1) && (a.Size(1) > 1) )
|
---|
| 186 | throw SzMismatchError("TMatrix<T>::Set(const TArray<T>& a) Size(0,1)>1 for Vector");
|
---|
[1081] | 187 | TArray<T>::SetBA(a);
|
---|
| 188 | if (NbDimensions() == 1) {
|
---|
| 189 | size_[1] = 1;
|
---|
| 190 | step_[1] = size_[0]*step_[0];
|
---|
| 191 | ndim_ = 2;
|
---|
| 192 | }
|
---|
| 193 | UpdateMemoryMapping(*this, SameMemoryMapping);
|
---|
| 194 | return(*this);
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 |
|
---|
| 198 |
|
---|
[894] | 199 | //! Resize the matrix
|
---|
| 200 | /*!
|
---|
| 201 | \param r : number of rows
|
---|
| 202 | \param c : number of columns
|
---|
| 203 | \param mm : define the memory mapping type
|
---|
| 204 | (SameMemoryMapping,CMemoryMapping
|
---|
| 205 | ,FortranMemoryMapping,DefaultMemoryMapping)
|
---|
[2575] | 206 | \param fzero : if \b true , set matrix elements to zero
|
---|
[894] | 207 | */
|
---|
[804] | 208 | template <class T>
|
---|
[2575] | 209 | void TMatrix<T>::ReSize(sa_size_t r, sa_size_t c, short mm, bool fzero)
|
---|
[762] | 210 | {
|
---|
[804] | 211 | if(r==0||c==0)
|
---|
| 212 | throw(SzMismatchError("TMatrix::ReSize r or c==0 "));
|
---|
[1099] | 213 | if ((arrtype_ == 2) && (r > 1) && (c > 1))
|
---|
| 214 | throw(SzMismatchError("TMatrix::ReSize r>1&&c>1 for Vector "));
|
---|
[1156] | 215 | sa_size_t size[BASEARRAY_MAXNDIMS];
|
---|
| 216 | for(int_4 kk=0; kk<BASEARRAY_MAXNDIMS; kk++) size[kk] = 0;
|
---|
[804] | 217 | if (mm == SameMemoryMapping) mm = GetMemoryMapping();
|
---|
[813] | 218 | else if ( (mm != CMemoryMapping) && (mm != FortranMemoryMapping) )
|
---|
| 219 | mm = GetDefaultMemoryMapping();
|
---|
| 220 | if (mm == CMemoryMapping) {
|
---|
| 221 | size[0] = c; size[1] = r;
|
---|
| 222 | }
|
---|
| 223 | else {
|
---|
| 224 | size[0] = r; size[1] = c;
|
---|
| 225 | }
|
---|
[2575] | 226 | TArray<T>::ReSize(2, size, 1, fzero);
|
---|
[813] | 227 | UpdateMemoryMapping(mm);
|
---|
[762] | 228 | }
|
---|
| 229 |
|
---|
[894] | 230 | //! Re-allocate space for the matrix
|
---|
| 231 | /*!
|
---|
| 232 | \param r : number of rows
|
---|
| 233 | \param c : number of columns
|
---|
| 234 | \param mm : define the memory mapping type
|
---|
| 235 | \param force : if true re-allocation is forced, if not it occurs
|
---|
| 236 | only if the required space is greater than the old one.
|
---|
| 237 | \sa ReSize
|
---|
| 238 | */
|
---|
[762] | 239 | template <class T>
|
---|
[1156] | 240 | void TMatrix<T>::Realloc(sa_size_t r,sa_size_t c, short mm, bool force)
|
---|
[762] | 241 | {
|
---|
[804] | 242 | if(r==0||c==0)
|
---|
| 243 | throw(SzMismatchError("TMatrix::Realloc r or c==0 "));
|
---|
[1099] | 244 | if ((arrtype_ == 2) && (r > 1) && (c > 1))
|
---|
| 245 | throw(SzMismatchError("TMatrix::Realloc r>1&&c>1 for Vector "));
|
---|
[1156] | 246 | sa_size_t size[BASEARRAY_MAXNDIMS];
|
---|
| 247 | for(int_4 kk=0; kk<BASEARRAY_MAXNDIMS; kk++) size[kk] = 0;
|
---|
[813] | 248 | if (mm == SameMemoryMapping) mm = GetMemoryMapping();
|
---|
| 249 | else if ( (mm != CMemoryMapping) && (mm != FortranMemoryMapping) )
|
---|
| 250 | mm = GetDefaultMemoryMapping();
|
---|
| 251 | if (mm == CMemoryMapping) {
|
---|
| 252 | size[0] = c; size[1] = r;
|
---|
| 253 | }
|
---|
| 254 | else {
|
---|
| 255 | size[0] = r; size[1] = c;
|
---|
| 256 | }
|
---|
[804] | 257 | TArray<T>::Realloc(2, size, 1, force);
|
---|
[813] | 258 | UpdateMemoryMapping(mm);
|
---|
[762] | 259 | }
|
---|
| 260 |
|
---|
[804] | 261 | // $CHECK$ Reza 03/2000 Doit-on declarer cette methode const ?
|
---|
[894] | 262 | //! Return a submatrix define by \b Range \b rline and \b rcol
|
---|
[762] | 263 | template <class T>
|
---|
[813] | 264 | TMatrix<T> TMatrix<T>::SubMatrix(Range rline, Range rcol) const
|
---|
[762] | 265 | {
|
---|
[813] | 266 | short mm = GetMemoryMapping();
|
---|
| 267 | Range rx, ry;
|
---|
| 268 | if (mm == CMemoryMapping) { rx = rcol; ry = rline; }
|
---|
| 269 | else { ry = rcol; rx = rline; }
|
---|
| 270 | TMatrix sm(SubArray(rx, ry, Range(0), Range(0), Range(0)),true, mm);
|
---|
| 271 | sm.UpdateMemoryMapping(mm);
|
---|
| 272 | return(sm);
|
---|
[762] | 273 | }
|
---|
| 274 |
|
---|
[804] | 275 | ////////////////////////////////////////////////////////////////
|
---|
| 276 | // Transposition
|
---|
[1412] | 277 | //! Transpose matrix in place, by changing the memory mapping
|
---|
[762] | 278 | template <class T>
|
---|
[1412] | 279 | TMatrix<T>& TMatrix<T>::TransposeSelf()
|
---|
[804] | 280 | {
|
---|
[813] | 281 | short vt = (marowi_ == veceli_) ? ColumnVector : RowVector;
|
---|
[1156] | 282 | int_4 rci = macoli_;
|
---|
[804] | 283 | macoli_ = marowi_;
|
---|
| 284 | marowi_ = rci;
|
---|
[813] | 285 | veceli_ = (vt == ColumnVector ) ? marowi_ : macoli_;
|
---|
[804] | 286 | return(*this);
|
---|
[762] | 287 | }
|
---|
| 288 |
|
---|
| 289 |
|
---|
[1412] | 290 | //! Returns the transpose of the original matrix.
|
---|
[894] | 291 | /*!
|
---|
[1412] | 292 | The data is shared between the two matrices
|
---|
| 293 | \return return a new matrix
|
---|
| 294 | */
|
---|
| 295 | template <class T>
|
---|
[2421] | 296 | TMatrix<T> TMatrix<T>::Transpose() const
|
---|
[1412] | 297 | {
|
---|
| 298 | TMatrix<T> tm(*this);
|
---|
| 299 | tm.TransposeSelf();
|
---|
| 300 | return tm;
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | //! Returns a new matrix, corresponding to the transpose of the original matrix
|
---|
| 304 | /*!
|
---|
[894] | 305 | \param mm : define the memory mapping type
|
---|
| 306 | (SameMemoryMapping,CMemoryMapping,FortranMemoryMapping)
|
---|
| 307 | \return return a new matrix
|
---|
| 308 | */
|
---|
[762] | 309 | template <class T>
|
---|
[2421] | 310 | TMatrix<T> TMatrix<T>::Transpose(short mm) const
|
---|
[762] | 311 | {
|
---|
[804] | 312 | if (mm == SameMemoryMapping) mm = GetMemoryMapping();
|
---|
| 313 | TMatrix<T> tm(NCols(), NRows(), mm);
|
---|
[1156] | 314 | for(sa_size_t i=0; i<NRows(); i++)
|
---|
| 315 | for(sa_size_t j=0; j<NCols(); j++)
|
---|
[804] | 316 | tm(j,i) = (*this)(i,j);
|
---|
| 317 | return tm;
|
---|
[762] | 318 | }
|
---|
| 319 |
|
---|
[1412] | 320 | //! Rearrange data in memory according to \b mm
|
---|
[894] | 321 | /*!
|
---|
| 322 | \param mm : define the memory mapping type
|
---|
| 323 | (SameMemoryMapping,CMemoryMapping,FortranMemoryMapping)
|
---|
| 324 | \warning If identical, return a matrix that share the datas
|
---|
| 325 | */
|
---|
[762] | 326 | template <class T>
|
---|
[2421] | 327 | TMatrix<T> TMatrix<T>::Rearrange(short mm) const
|
---|
[762] | 328 | {
|
---|
[813] | 329 | if ( mm == SameMemoryMapping) mm = GetMemoryMapping();
|
---|
| 330 | else if ( (mm != CMemoryMapping) && (mm != FortranMemoryMapping) )
|
---|
| 331 | mm = GetDefaultMemoryMapping();
|
---|
| 332 |
|
---|
| 333 | if (mm == GetMemoryMapping())
|
---|
| 334 | return (TMatrix<T>(*this, true));
|
---|
| 335 |
|
---|
[804] | 336 | TMatrix<T> tm(NRows(), NCols(), mm);
|
---|
[1156] | 337 | for(sa_size_t i=0; i<NRows(); i++)
|
---|
| 338 | for(sa_size_t j=0; j<NCols(); j++)
|
---|
[804] | 339 | tm(i,j) = (*this)(i,j);
|
---|
| 340 | return tm;
|
---|
[762] | 341 | }
|
---|
| 342 |
|
---|
[894] | 343 | //! Set the matrix to the identity matrix \b imx
|
---|
[762] | 344 | template <class T>
|
---|
[804] | 345 | TMatrix<T>& TMatrix<T>::SetIdentity(IdentityMatrix imx)
|
---|
[762] | 346 | {
|
---|
[804] | 347 | if (ndim_ == 0) {
|
---|
[1156] | 348 | sa_size_t sz = imx.Size();
|
---|
[804] | 349 | if (sz < 1) sz = 1;
|
---|
| 350 | ReSize(sz, sz);
|
---|
| 351 | }
|
---|
| 352 | T diag = (T)imx.Diag();
|
---|
| 353 | if (NRows() != NCols())
|
---|
| 354 | throw SzMismatchError("TMatrix::operator= (IdentityMatrix) NRows() != NCols()") ;
|
---|
[996] | 355 | *this = (T) 0;
|
---|
[1156] | 356 | for(sa_size_t i=0; i<NRows(); i++) (*this)(i,i) = diag;
|
---|
[762] | 357 |
|
---|
[804] | 358 | return (*this);
|
---|
[762] | 359 | }
|
---|
| 360 |
|
---|
[804] | 361 |
|
---|
| 362 |
|
---|
| 363 | ////////////////////////////////////////////////////////////////
|
---|
| 364 | //**** Impression
|
---|
[894] | 365 | //! Return info on number of rows, column and type \b T
|
---|
[762] | 366 | template <class T>
|
---|
[813] | 367 | string TMatrix<T>::InfoString() const
|
---|
| 368 | {
|
---|
| 369 | string rs = "TMatrix<";
|
---|
| 370 | rs += typeid(T).name();
|
---|
| 371 | char buff[64];
|
---|
| 372 | sprintf(buff, ">(NRows=%ld, NCols=%ld)", (long)NRows(), (long)NCols());
|
---|
| 373 | rs += buff;
|
---|
| 374 | return(rs);
|
---|
| 375 | }
|
---|
| 376 |
|
---|
[894] | 377 | //! Print matrix
|
---|
| 378 | /*!
|
---|
[1554] | 379 | \param os : output stream
|
---|
[894] | 380 | \param maxprt : maximum numer of print
|
---|
| 381 | \param si : if true, display attached DvList
|
---|
[1554] | 382 | \param ascd : if true, suppresses the display of line numbers,
|
---|
| 383 | suitable for ascii dump format.
|
---|
[894] | 384 | \sa SetMaxPrint
|
---|
| 385 | */
|
---|
[813] | 386 | template <class T>
|
---|
[1581] | 387 | void TMatrix<T>::Print(ostream& os, sa_size_t maxprt, bool si, bool ascd) const
|
---|
[762] | 388 | {
|
---|
[804] | 389 | if (maxprt < 0) maxprt = max_nprt_;
|
---|
[1156] | 390 | sa_size_t npr = 0;
|
---|
[804] | 391 | Show(os, si);
|
---|
[850] | 392 | if (ndim_ < 1) return;
|
---|
[1156] | 393 | sa_size_t kc,kr;
|
---|
[804] | 394 | for(kr=0; kr<size_[marowi_]; kr++) {
|
---|
[1554] | 395 | if ( (size_[marowi_] > 1) && (size_[macoli_] > 10) && ascd) cout << "----- Line= " << kr << endl;
|
---|
[804] | 396 | for(kc=0; kc<size_[macoli_]; kc++) {
|
---|
[1554] | 397 | if(kc > 0) os << " ";
|
---|
[804] | 398 | os << (*this)(kr, kc); npr++;
|
---|
[1156] | 399 | if (npr >= (sa_size_t) maxprt) {
|
---|
[804] | 400 | if (npr < totsize_) os << "\n .... " << endl; return;
|
---|
| 401 | }
|
---|
| 402 | }
|
---|
| 403 | os << endl;
|
---|
| 404 | }
|
---|
[813] | 405 | os << endl;
|
---|
[762] | 406 | }
|
---|
| 407 |
|
---|
[2585] | 408 | //////////////////////////////////////////////////////////
|
---|
| 409 | /////////////// Multiplication matricielle ///////////////
|
---|
| 410 | //////////////////////////////////////////////////////////
|
---|
[762] | 411 |
|
---|
[894] | 412 | //! Return the matrix product C = (*this)*B
|
---|
| 413 | /*!
|
---|
| 414 | \param mm : define the memory mapping type for the return matrix
|
---|
| 415 | */
|
---|
[2574] | 416 | ////////////// Routine de base sans optimisation //////////////
|
---|
| 417 | /*
|
---|
[804] | 418 | template <class T>
|
---|
| 419 | TMatrix<T> TMatrix<T>::Multiply(const TMatrix<T>& b, short mm) const
|
---|
| 420 | {
|
---|
| 421 | if (NCols() != b.NRows())
|
---|
| 422 | throw(SzMismatchError("TMatrix<T>::Multiply(b) NCols() != b.NRows() ") );
|
---|
| 423 | if (mm == SameMemoryMapping) mm = GetMemoryMapping();
|
---|
| 424 | TMatrix<T> rm(NRows(), b.NCols(), mm);
|
---|
[762] | 425 |
|
---|
[804] | 426 | const T * pea;
|
---|
| 427 | const T * peb;
|
---|
| 428 | T sum;
|
---|
[1156] | 429 | sa_size_t r,c,k;
|
---|
| 430 | sa_size_t stepa = Step(ColsKA());
|
---|
[1415] | 431 | sa_size_t stepb = b.Step(b.RowsKA());
|
---|
[804] | 432 | // Calcul de C=rm = A*B (A=*this)
|
---|
| 433 | for(r=0; r<rm.NRows(); r++) // Boucle sur les lignes de A
|
---|
| 434 | for(c=0; c<rm.NCols(); c++) { // Boucle sur les colonnes de B
|
---|
| 435 | sum = 0;
|
---|
| 436 | pea = &((*this)(r,0)); // 1er element de la ligne r de A
|
---|
| 437 | peb = &(b(0,c)); // 1er element de la colonne c de B
|
---|
| 438 | for(k=0; k<NCols(); k++) sum += pea[k*stepa]*peb[k*stepb];
|
---|
| 439 | rm(r,c) = sum;
|
---|
| 440 | }
|
---|
| 441 |
|
---|
| 442 | return rm;
|
---|
| 443 | }
|
---|
[2574] | 444 | */
|
---|
[804] | 445 |
|
---|
[2574] | 446 | ////////////// Routine optimisee //////////////
|
---|
| 447 | template <class T>
|
---|
| 448 | TMatrix<T> TMatrix<T>::Multiply(const TMatrix<T>& b, short mm) const
|
---|
[2583] | 449 | // Calcul de C= rm = A*B (A=*this)
|
---|
| 450 | // Remember: C-like matrices are column packed
|
---|
| 451 | // Fortan-like matrices are line packed
|
---|
[2574] | 452 | {
|
---|
| 453 | if (NCols() != b.NRows())
|
---|
| 454 | throw(SzMismatchError("TMatrix<T>::Multiply(b) NCols() != b.NRows() ") );
|
---|
| 455 |
|
---|
[2583] | 456 | // Commentaire: pas de difference de vitesse notable selon le mapping de la matrice produit "rm"
|
---|
[2585] | 457 | if (mm == SameMemoryMapping) mm = GetMemoryMapping();
|
---|
[2574] | 458 | TMatrix<T> rm(NRows(), b.NCols(), mm);
|
---|
| 459 |
|
---|
| 460 | // Les "steps" pour l'adressage des colonnes de A et des lignes de B
|
---|
| 461 | sa_size_t stepa = Step(ColsKA());
|
---|
| 462 | sa_size_t stepb = b.Step(b.RowsKA());
|
---|
| 463 |
|
---|
[2583] | 464 | // Taille totale des matrices A et B
|
---|
| 465 | size_t totsiza = this->DataBlock().Size();
|
---|
| 466 | size_t totsizb = b.DataBlock().Size();
|
---|
[2574] | 467 |
|
---|
[2583] | 468 |
|
---|
| 469 | ///////////////////////////////////////////////////////////////////
|
---|
| 470 | // On decide si on optimise ou non selon les dimensions de A et B //
|
---|
| 471 | // (il semble que optimiser ou non ne degrade pas //
|
---|
| 472 | // beaucoup la vitesse pour les petites matrices) //
|
---|
| 473 | ////////////////////////////////////////////////////////////////////
|
---|
| 474 |
|
---|
| 475 | uint_2 popt = GetMatProdOpt();
|
---|
| 476 | bool no_optim = false; // optimization demandee par default
|
---|
| 477 | if( (popt&(uint_2)1) == 0 ) { // pas d'optimization explicitement demande
|
---|
| 478 | no_optim = true;
|
---|
| 479 | } else if( (popt&(uint_2)2) == 0 ) { // pas d'optimization forcee, la methode decide
|
---|
| 480 | // On part sur une disponibilite dans le cache processeur de 100 ko
|
---|
| 481 | // (A et B peuvent etre stoquees dans le cache)
|
---|
| 482 | if((totsiza+totsizb)*sizeof(T)<100000) no_optim = true;
|
---|
| 483 | }
|
---|
| 484 |
|
---|
[2574] | 485 | sa_size_t r,c,k;
|
---|
| 486 | T sum;
|
---|
| 487 | const T * pe;
|
---|
| 488 |
|
---|
[2583] | 489 |
|
---|
| 490 | /////////////////////////////////
|
---|
| 491 | // Pas d'optimisation demandee //
|
---|
| 492 | /////////////////////////////////
|
---|
| 493 |
|
---|
[2574] | 494 | if( no_optim ) {
|
---|
| 495 | //cout<<"no_optim("<<no_optim<<") "<<stepa<<" "<<stepb<<endl;
|
---|
| 496 | const T * pea;
|
---|
| 497 | const T * peb;
|
---|
| 498 | for(r=0; r<rm.NRows(); r++) { // Boucle sur les lignes de A
|
---|
| 499 | for(c=0; c<rm.NCols(); c++) { // Boucle sur les colonnes de B
|
---|
| 500 | sum = 0;
|
---|
| 501 | pea = &((*this)(r,0));
|
---|
| 502 | peb = &(b(0,c));
|
---|
| 503 | // On gagne un peu en remplacant "pea[k*stepa]" par "pea+=stepa" pour les grosses matrices
|
---|
| 504 | //for(k=0; k<NCols(); k++) sum += pea[k*stepa]*peb[k*stepb];
|
---|
| 505 | for(k=0; k<NCols(); k++) {sum += (*pea)*(*peb); pea+=stepa; peb+=stepb;}
|
---|
| 506 | rm(r,c) = sum;
|
---|
| 507 | }
|
---|
| 508 | }
|
---|
[2583] | 509 | return rm;
|
---|
[2574] | 510 | }
|
---|
[2583] | 511 |
|
---|
| 512 |
|
---|
| 513 | //////////////////////////////////////////////////////////////////////////////////
|
---|
| 514 | // A.col est packed et B.row est packed (on a interet a optimiser quand meme) //
|
---|
| 515 | //////////////////////////////////////////////////////////////////////////////////
|
---|
| 516 |
|
---|
| 517 | if(stepa==1 && stepb==1) {
|
---|
[2585] | 518 | //cout<<"A.col packed && B.row packed "<<stepa<<" "<<stepb<<endl;
|
---|
| 519 | T * pea = new T[NCols()];
|
---|
[2574] | 520 | const T * peb;
|
---|
| 521 | for(r=0; r<rm.NRows(); r++) { // Boucle sur les lignes de A
|
---|
| 522 | pe = &((*this)(r,0));
|
---|
| 523 | for(k=0; k<NCols(); k++) {pea[k] = *(pe++);}
|
---|
| 524 | for(c=0; c<rm.NCols(); c++) { // Boucle sur les colonnes de B
|
---|
| 525 | sum = 0;
|
---|
| 526 | peb = &(b(0,c));
|
---|
| 527 | for(k=0; k<NCols(); k++) sum += *(peb++)*pea[k];
|
---|
| 528 | rm(r,c) = sum;
|
---|
| 529 | }
|
---|
| 530 | }
|
---|
| 531 | delete [] pea;
|
---|
[2583] | 532 | return rm;
|
---|
| 533 | }
|
---|
| 534 |
|
---|
| 535 |
|
---|
| 536 | //////////////////////////////////////////////////
|
---|
| 537 | // A.col est packed et B.row n'est pas packed //
|
---|
| 538 | //////////////////////////////////////////////////
|
---|
| 539 |
|
---|
| 540 | if(stepa==1 && stepb!=1) {
|
---|
[2574] | 541 | //cout<<"A.col packed && B.row not packed "<<stepa<<" "<<stepb<<endl;
|
---|
| 542 | const T * pea;
|
---|
[2585] | 543 | T * peb = new T[NCols()];
|
---|
[2574] | 544 | for(c=0; c<rm.NCols(); c++) { // Boucle sur les colonnes de B
|
---|
| 545 | pe = &(b(0,c));
|
---|
| 546 | for(k=0; k<NCols(); k++) {peb[k] = *pe; pe+=stepb;}
|
---|
| 547 | for(r=0; r<rm.NRows(); r++) { // Boucle sur les lignes de A
|
---|
| 548 | sum = 0;
|
---|
| 549 | pea = &((*this)(r,0));
|
---|
| 550 | for(k=0; k<NCols(); k++) sum += pea[k]*peb[k];
|
---|
| 551 | rm(r,c) = sum;
|
---|
| 552 | }
|
---|
| 553 | }
|
---|
| 554 | delete [] peb;
|
---|
[2583] | 555 | return rm;
|
---|
[2574] | 556 | }
|
---|
[2583] | 557 |
|
---|
| 558 |
|
---|
| 559 | //////////////////////////////////////////////////
|
---|
| 560 | // A.col n'est pas packed et B.row est packed //
|
---|
| 561 | //////////////////////////////////////////////////
|
---|
| 562 |
|
---|
| 563 | if(stepa!=1 && stepb==1) {
|
---|
[2574] | 564 | //cout<<"A.col not packed && B.row packed "<<stepa<<" "<<stepb<<endl;
|
---|
[2585] | 565 | T * pea = new T[NCols()];
|
---|
[2574] | 566 | const T * peb;
|
---|
| 567 | for(r=0; r<rm.NRows(); r++) { // Boucle sur les lignes de A
|
---|
| 568 | pe = &((*this)(r,0));
|
---|
| 569 | for(k=0; k<NCols(); k++) {pea[k] = *pe; pe+=stepa;}
|
---|
| 570 | for(c=0; c<rm.NCols(); c++) { // Boucle sur les colonnes de B
|
---|
| 571 | sum = 0;
|
---|
| 572 | peb = &(b(0,c));
|
---|
| 573 | for(k=0; k<NCols(); k++) sum += pea[k]*peb[k];
|
---|
| 574 | rm(r,c) = sum;
|
---|
| 575 | }
|
---|
| 576 | }
|
---|
| 577 | delete [] pea;
|
---|
[2583] | 578 | return rm;
|
---|
[2574] | 579 | }
|
---|
[2583] | 580 |
|
---|
| 581 |
|
---|
| 582 | ////////////////////////////////////////////////////////
|
---|
| 583 | // A.col n'est pas packed et B.row n'est pas packed //
|
---|
| 584 | ////////////////////////////////////////////////////////
|
---|
| 585 |
|
---|
| 586 | //---- On demande l'optimization par copie d'une des matrices
|
---|
| 587 |
|
---|
| 588 | if( (popt&(uint_2)4) ) {
|
---|
| 589 | // On copie la plus petite
|
---|
| 590 | if(totsiza<totsizb) { // on copie A
|
---|
| 591 | //cout<<"A.col not packed && B.row not packed ==> copy A to optimize "<<stepa<<" "<<stepb<<endl;
|
---|
| 592 | // Acopy doit etre C-like pour etre column-packed
|
---|
| 593 | TMatrix<T> acopy(NRows(),NCols(),BaseArray::CMemoryMapping);
|
---|
| 594 | acopy = *this;
|
---|
| 595 | rm = acopy.Multiply(b,mm);
|
---|
| 596 | } else { // on copie B
|
---|
| 597 | //cout<<"A.col not packed && B.row not packed ==> copy B to optimize "<<stepa<<" "<<stepb<<endl;
|
---|
| 598 | // Bcopy doit etre Fortran-like pour etre column-packed
|
---|
| 599 | TMatrix<T> bcopy(b.NRows(),b.NCols(),BaseArray::FortranMemoryMapping);
|
---|
| 600 | bcopy = b;
|
---|
| 601 | rm = Multiply(bcopy,mm);
|
---|
| 602 | }
|
---|
| 603 | return rm;
|
---|
| 604 | }
|
---|
| 605 |
|
---|
| 606 | //---- stepb>stepa
|
---|
| 607 |
|
---|
| 608 | if(stepa!=1 && stepb!=1 && stepb>stepa) {
|
---|
[2574] | 609 | //cout<<"A.col not packed && B.row not packed ==> optimize on B "<<stepa<<" "<<stepb<<endl;
|
---|
| 610 | const T * pea;
|
---|
[2585] | 611 | T * peb = new T[NCols()];
|
---|
[2574] | 612 | for(c=0; c<rm.NCols(); c++) { // Boucle sur les colonnes de B
|
---|
| 613 | pe = &(b(0,c));
|
---|
| 614 | for(k=0; k<NCols(); k++) {peb[k] = *pe; pe+=stepb;}
|
---|
| 615 | for(r=0; r<rm.NRows(); r++) { // Boucle sur les lignes de A
|
---|
| 616 | sum = 0;
|
---|
| 617 | pea = &((*this)(r,0));
|
---|
| 618 | for(k=0; k<NCols(); k++) {sum += (*pea)*peb[k]; pea+=stepa;}
|
---|
| 619 | rm(r,c) = sum;
|
---|
| 620 | }
|
---|
| 621 | }
|
---|
| 622 | delete [] peb;
|
---|
[2583] | 623 | return rm;
|
---|
[2574] | 624 | }
|
---|
[2583] | 625 |
|
---|
| 626 | //---- stepa>=stepb
|
---|
| 627 |
|
---|
| 628 | if(stepa!=1 && stepb!=1) {
|
---|
[2574] | 629 | //cout<<"A.col not packed && B.row not packed ==> optimize on A "<<stepa<<" "<<stepb<<endl;
|
---|
[2585] | 630 | T * pea = new T[NCols()];
|
---|
[2574] | 631 | const T * peb;
|
---|
| 632 | for(r=0; r<rm.NRows(); r++) { // Boucle sur les lignes de A
|
---|
| 633 | pe = &((*this)(r,0));
|
---|
| 634 | for(k=0; k<NCols(); k++) {pea[k] = *pe; pe+=stepa;}
|
---|
| 635 | for(c=0; c<rm.NCols(); c++) { // Boucle sur les colonnes de B
|
---|
| 636 | sum = 0;
|
---|
| 637 | peb = &(b(0,c));
|
---|
| 638 | for(k=0; k<NCols(); k++) {sum += pea[k]*(*peb); peb+=stepb;}
|
---|
| 639 | rm(r,c) = sum;
|
---|
| 640 | }
|
---|
| 641 | }
|
---|
| 642 | delete [] pea;
|
---|
[2583] | 643 | return rm;
|
---|
[2574] | 644 | }
|
---|
| 645 |
|
---|
[2583] | 646 |
|
---|
| 647 | //////////////////////////////////////////////////
|
---|
| 648 | // Cas non prevu, on ne doit JAMAIS arriver ici //
|
---|
| 649 | //////////////////////////////////////////////////
|
---|
| 650 | cout<<"TMatrix<T>::Multiply(b) Optimize case not treated... Please report BUG !!! "<<endl;
|
---|
| 651 | throw(SzMismatchError("TMatrix<T>::Multiply(b) Optimize case not treated... Please report BUG !!! ") );
|
---|
[2574] | 652 | return rm;
|
---|
[2585] | 653 |
|
---|
[2574] | 654 | }
|
---|
| 655 |
|
---|
[2585] | 656 |
|
---|
[762] | 657 | ///////////////////////////////////////////////////////////////
|
---|
| 658 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
| 659 | #pragma define_template TMatrix<uint_2>
|
---|
[1543] | 660 | #pragma define_template TMatrix<uint_8>
|
---|
[762] | 661 | #pragma define_template TMatrix<int_4>
|
---|
| 662 | #pragma define_template TMatrix<int_8>
|
---|
| 663 | #pragma define_template TMatrix<r_4>
|
---|
[804] | 664 | #pragma define_template TMatrix<r_8>
|
---|
[762] | 665 | #pragma define_template TMatrix< complex<r_4> >
|
---|
| 666 | #pragma define_template TMatrix< complex<r_8> >
|
---|
| 667 | #endif
|
---|
| 668 |
|
---|
| 669 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
| 670 | template class TMatrix<uint_2>;
|
---|
[1543] | 671 | template class TMatrix<uint_8>;
|
---|
[762] | 672 | template class TMatrix<int_4>;
|
---|
| 673 | template class TMatrix<int_8>;
|
---|
| 674 | template class TMatrix<r_4>;
|
---|
| 675 | template class TMatrix<r_8>;
|
---|
| 676 | template class TMatrix< complex<r_4> >;
|
---|
| 677 | template class TMatrix< complex<r_8> >;
|
---|
| 678 | #endif
|
---|