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