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