[850] | 1 | // $Id: tmatrix.cc,v 1.5 2000-04-10 12:57:44 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
|
---|
| 14 | template <class T>
|
---|
| 15 | TMatrix<T>::TMatrix()
|
---|
| 16 | // Constructeur par defaut.
|
---|
[804] | 17 | : TArray<T>()
|
---|
[762] | 18 | {
|
---|
[813] | 19 | ck_memo_vt_ = true;
|
---|
[762] | 20 | }
|
---|
| 21 |
|
---|
| 22 | template <class T>
|
---|
[804] | 23 | TMatrix<T>::TMatrix(uint_4 r,uint_4 c, short mm)
|
---|
[762] | 24 | // Construit une matrice de r lignes et c colonnes.
|
---|
[804] | 25 | : TArray<T>()
|
---|
[762] | 26 | {
|
---|
[804] | 27 | if ( (r == 0) || (c == 0) )
|
---|
| 28 | throw ParmError("TMatrix<T>::TMatrix(uint_4 r,uint_4 c) NRows or NCols = 0");
|
---|
| 29 | ReSize(r, c, mm);
|
---|
[762] | 30 | }
|
---|
| 31 |
|
---|
| 32 | template <class T>
|
---|
| 33 | TMatrix<T>::TMatrix(const TMatrix<T>& a)
|
---|
| 34 | // Constructeur par copie (partage si "a" temporaire).
|
---|
[804] | 35 | : TArray<T>(a)
|
---|
[762] | 36 | {
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | template <class T>
|
---|
[804] | 40 | TMatrix<T>::TMatrix(const TMatrix<T>& a, bool share)
|
---|
[762] | 41 | // Constructeur par copie avec possibilite de forcer le partage ou non.
|
---|
[804] | 42 | : TArray<T>(a, share)
|
---|
[762] | 43 | {
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 |
|
---|
| 47 | template <class T>
|
---|
[804] | 48 | TMatrix<T>::TMatrix(const TArray<T>& a)
|
---|
| 49 | : TArray<T>(a)
|
---|
[762] | 50 | {
|
---|
[813] | 51 | if (a.NbDimensions() > 2)
|
---|
| 52 | throw SzMismatchError("TMatrix<T>::TMatrix(const TArray<T>& a) a.NbDimensions()>2 ");
|
---|
| 53 | if (a.NbDimensions() == 1) {
|
---|
| 54 | size_[1] = 1;
|
---|
| 55 | step_[1] = size_[0]*step_[0];
|
---|
| 56 | ndim_ = 2;
|
---|
| 57 | }
|
---|
| 58 | UpdateMemoryMapping(a, SameMemoryMapping);
|
---|
[762] | 59 | }
|
---|
| 60 |
|
---|
| 61 | template <class T>
|
---|
[804] | 62 | TMatrix<T>::TMatrix(const TArray<T>& a, bool share, short mm )
|
---|
| 63 | : TArray<T>(a, share)
|
---|
[762] | 64 | {
|
---|
[813] | 65 | if (a.NbDimensions() > 2)
|
---|
| 66 | throw SzMismatchError("TMatrix<T>::TMatrix(const TArray<T>& a, ...) a.NbDimensions()>2");
|
---|
| 67 | if (a.NbDimensions() == 1) {
|
---|
| 68 | size_[1] = 1;
|
---|
| 69 | step_[1] = size_[0]*step_[0];
|
---|
| 70 | ndim_ = 2;
|
---|
| 71 | }
|
---|
[804] | 72 | UpdateMemoryMapping(a, mm);
|
---|
[762] | 73 | }
|
---|
| 74 |
|
---|
| 75 | template <class T>
|
---|
[804] | 76 | TMatrix<T>::~TMatrix()
|
---|
| 77 | // Destructeur
|
---|
[762] | 78 | {
|
---|
| 79 |
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[804] | 82 | template <class T>
|
---|
| 83 | TArray<T>& TMatrix<T>::Set(const TArray<T>& a)
|
---|
[762] | 84 | {
|
---|
[813] | 85 | if (a.NbDimensions() > 2)
|
---|
| 86 | throw SzMismatchError("TMatrix<T>::Set(const TArray<T>& a) a.NbDimensions() > 2");
|
---|
| 87 | TArray<T>::Set(a);
|
---|
| 88 | if (a.NbDimensions() == 1) {
|
---|
| 89 | size_[1] = 1;
|
---|
| 90 | step_[1] = size_[0]*step_[0];
|
---|
| 91 | ndim_ = 2;
|
---|
| 92 | }
|
---|
| 93 | UpdateMemoryMapping(a, SameMemoryMapping);
|
---|
| 94 | return(*this);
|
---|
[762] | 95 | }
|
---|
| 96 |
|
---|
[804] | 97 | template <class T>
|
---|
| 98 | void TMatrix<T>::ReSize(uint_4 r, uint_4 c, short mm)
|
---|
[762] | 99 | {
|
---|
[804] | 100 | if(r==0||c==0)
|
---|
| 101 | throw(SzMismatchError("TMatrix::ReSize r or c==0 "));
|
---|
| 102 | uint_4 size[BASEARRAY_MAXNDIMS];
|
---|
| 103 | for(int kk=0; kk<BASEARRAY_MAXNDIMS; kk++) size[kk] = 0;
|
---|
| 104 | if (mm == SameMemoryMapping) mm = GetMemoryMapping();
|
---|
[813] | 105 | else if ( (mm != CMemoryMapping) && (mm != FortranMemoryMapping) )
|
---|
| 106 | mm = GetDefaultMemoryMapping();
|
---|
| 107 | if (mm == CMemoryMapping) {
|
---|
| 108 | size[0] = c; size[1] = r;
|
---|
| 109 | }
|
---|
| 110 | else {
|
---|
| 111 | size[0] = r; size[1] = c;
|
---|
| 112 | }
|
---|
[804] | 113 | TArray<T>::ReSize(2, size, 1);
|
---|
[813] | 114 | UpdateMemoryMapping(mm);
|
---|
[762] | 115 | }
|
---|
| 116 |
|
---|
| 117 | template <class T>
|
---|
[804] | 118 | void TMatrix<T>::Realloc(uint_4 r,uint_4 c, short mm, bool force)
|
---|
[762] | 119 | {
|
---|
[804] | 120 | if(r==0||c==0)
|
---|
| 121 | throw(SzMismatchError("TMatrix::Realloc r or c==0 "));
|
---|
| 122 | uint_4 size[BASEARRAY_MAXNDIMS];
|
---|
| 123 | for(int kk=0; kk<BASEARRAY_MAXNDIMS; kk++) size[kk] = 0;
|
---|
[813] | 124 | if (mm == SameMemoryMapping) mm = GetMemoryMapping();
|
---|
| 125 | else if ( (mm != CMemoryMapping) && (mm != FortranMemoryMapping) )
|
---|
| 126 | mm = GetDefaultMemoryMapping();
|
---|
| 127 | if (mm == CMemoryMapping) {
|
---|
| 128 | size[0] = c; size[1] = r;
|
---|
| 129 | }
|
---|
| 130 | else {
|
---|
| 131 | size[0] = r; size[1] = c;
|
---|
| 132 | }
|
---|
[804] | 133 | TArray<T>::Realloc(2, size, 1, force);
|
---|
[813] | 134 | UpdateMemoryMapping(mm);
|
---|
[762] | 135 | }
|
---|
| 136 |
|
---|
[804] | 137 | // $CHECK$ Reza 03/2000 Doit-on declarer cette methode const ?
|
---|
[762] | 138 | template <class T>
|
---|
[813] | 139 | TMatrix<T> TMatrix<T>::SubMatrix(Range rline, Range rcol) const
|
---|
[762] | 140 | {
|
---|
[813] | 141 | short mm = GetMemoryMapping();
|
---|
| 142 | Range rx, ry;
|
---|
| 143 | if (mm == CMemoryMapping) { rx = rcol; ry = rline; }
|
---|
| 144 | else { ry = rcol; rx = rline; }
|
---|
| 145 | TMatrix sm(SubArray(rx, ry, Range(0), Range(0), Range(0)),true, mm);
|
---|
| 146 | sm.UpdateMemoryMapping(mm);
|
---|
| 147 | sm.SetTemp(true);
|
---|
| 148 | return(sm);
|
---|
[762] | 149 | }
|
---|
| 150 |
|
---|
[804] | 151 | ////////////////////////////////////////////////////////////////
|
---|
| 152 | // Transposition
|
---|
[762] | 153 | template <class T>
|
---|
[804] | 154 | TMatrix<T>& TMatrix<T>::Transpose()
|
---|
| 155 | {
|
---|
[813] | 156 | short vt = (marowi_ == veceli_) ? ColumnVector : RowVector;
|
---|
[804] | 157 | uint_4 rci = macoli_;
|
---|
| 158 | macoli_ = marowi_;
|
---|
| 159 | marowi_ = rci;
|
---|
[813] | 160 | veceli_ = (vt == ColumnVector ) ? marowi_ : macoli_;
|
---|
[804] | 161 | return(*this);
|
---|
[762] | 162 | }
|
---|
| 163 |
|
---|
| 164 |
|
---|
| 165 | template <class T>
|
---|
[804] | 166 | TMatrix<T> TMatrix<T>::Transpose(short mm)
|
---|
[762] | 167 | {
|
---|
[804] | 168 | if (mm == SameMemoryMapping) mm = GetMemoryMapping();
|
---|
| 169 | TMatrix<T> tm(NCols(), NRows(), mm);
|
---|
| 170 | for(uint_4 i=0; i<NRows(); i++)
|
---|
| 171 | for(uint_4 j=0; j<NCols(); j++)
|
---|
| 172 | tm(j,i) = (*this)(i,j);
|
---|
| 173 | tm.SetTemp(true);
|
---|
| 174 | return tm;
|
---|
[762] | 175 | }
|
---|
| 176 |
|
---|
[804] | 177 | // Rearrangement memoire
|
---|
[813] | 178 | // Si identique, renvoie une matrice qui partage les donnees
|
---|
[762] | 179 | template <class T>
|
---|
[804] | 180 | TMatrix<T> TMatrix<T>::Rearrange(short mm)
|
---|
[762] | 181 | {
|
---|
[813] | 182 | if ( mm == SameMemoryMapping) mm = GetMemoryMapping();
|
---|
| 183 | else if ( (mm != CMemoryMapping) && (mm != FortranMemoryMapping) )
|
---|
| 184 | mm = GetDefaultMemoryMapping();
|
---|
| 185 |
|
---|
| 186 | if (mm == GetMemoryMapping())
|
---|
| 187 | return (TMatrix<T>(*this, true));
|
---|
| 188 |
|
---|
[804] | 189 | TMatrix<T> tm(NRows(), NCols(), mm);
|
---|
| 190 | for(uint_4 i=0; i<NRows(); i++)
|
---|
| 191 | for(uint_4 j=0; j<NCols(); j++)
|
---|
| 192 | tm(i,j) = (*this)(i,j);
|
---|
| 193 | tm.SetTemp(true);
|
---|
| 194 | return tm;
|
---|
[762] | 195 | }
|
---|
| 196 |
|
---|
| 197 | template <class T>
|
---|
[804] | 198 | TMatrix<T>& TMatrix<T>::SetIdentity(IdentityMatrix imx)
|
---|
[762] | 199 | {
|
---|
[804] | 200 | if (ndim_ == 0) {
|
---|
| 201 | uint_4 sz = imx.Size();
|
---|
| 202 | if (sz < 1) sz = 1;
|
---|
| 203 | ReSize(sz, sz);
|
---|
| 204 | }
|
---|
| 205 | T diag = (T)imx.Diag();
|
---|
| 206 | if (NRows() != NCols())
|
---|
| 207 | throw SzMismatchError("TMatrix::operator= (IdentityMatrix) NRows() != NCols()") ;
|
---|
| 208 | for(uint_4 i=0; i<NRows(); i++) (*this)(i,i) = diag;
|
---|
[762] | 209 |
|
---|
[804] | 210 | return (*this);
|
---|
[762] | 211 | }
|
---|
| 212 |
|
---|
[804] | 213 |
|
---|
| 214 |
|
---|
| 215 | ////////////////////////////////////////////////////////////////
|
---|
| 216 | //**** Impression
|
---|
[762] | 217 | template <class T>
|
---|
[813] | 218 | string TMatrix<T>::InfoString() const
|
---|
| 219 | {
|
---|
| 220 | string rs = "TMatrix<";
|
---|
| 221 | rs += typeid(T).name();
|
---|
| 222 | char buff[64];
|
---|
| 223 | sprintf(buff, ">(NRows=%ld, NCols=%ld)", (long)NRows(), (long)NCols());
|
---|
| 224 | rs += buff;
|
---|
| 225 | return(rs);
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | template <class T>
|
---|
[804] | 229 | void TMatrix<T>::Print(ostream& os, int_4 maxprt, bool si) const
|
---|
[762] | 230 | {
|
---|
[804] | 231 | if (maxprt < 0) maxprt = max_nprt_;
|
---|
| 232 | int_4 npr = 0;
|
---|
| 233 | Show(os, si);
|
---|
[850] | 234 | if (ndim_ < 1) return;
|
---|
[804] | 235 | uint_4 kc,kr;
|
---|
| 236 | for(kr=0; kr<size_[marowi_]; kr++) {
|
---|
| 237 | if ( (size_[marowi_] > 1) && (size_[macoli_] > 10) ) cout << "----- Ligne Line= " << kr << endl;
|
---|
| 238 | for(kc=0; kc<size_[macoli_]; kc++) {
|
---|
| 239 | if(kc > 0) os << ", ";
|
---|
| 240 | os << (*this)(kr, kc); npr++;
|
---|
| 241 | if (npr >= maxprt) {
|
---|
| 242 | if (npr < totsize_) os << "\n .... " << endl; return;
|
---|
| 243 | }
|
---|
| 244 | }
|
---|
| 245 | os << endl;
|
---|
| 246 | }
|
---|
[813] | 247 | os << endl;
|
---|
[762] | 248 | }
|
---|
| 249 |
|
---|
| 250 | ////////////////////////////////////////////////////////////////
|
---|
[804] | 251 | //**** Multiplication matricielle *****
|
---|
[762] | 252 | ////////////////////////////////////////////////////////////////
|
---|
| 253 |
|
---|
[804] | 254 | template <class T>
|
---|
| 255 | TMatrix<T> TMatrix<T>::Multiply(const TMatrix<T>& b, short mm) const
|
---|
| 256 | {
|
---|
| 257 | if (NCols() != b.NRows())
|
---|
| 258 | throw(SzMismatchError("TMatrix<T>::Multiply(b) NCols() != b.NRows() ") );
|
---|
| 259 | if (mm == SameMemoryMapping) mm = GetMemoryMapping();
|
---|
| 260 | TMatrix<T> rm(NRows(), b.NCols(), mm);
|
---|
[762] | 261 |
|
---|
[804] | 262 | const T * pea;
|
---|
| 263 | const T * peb;
|
---|
| 264 | T sum;
|
---|
| 265 | uint_4 r,c,k;
|
---|
| 266 | uint_4 stepa = Step(ColsKA());
|
---|
| 267 | uint_4 stepb = b.Step(RowsKA());
|
---|
| 268 | // Calcul de C=rm = A*B (A=*this)
|
---|
| 269 | for(r=0; r<rm.NRows(); r++) // Boucle sur les lignes de A
|
---|
| 270 | for(c=0; c<rm.NCols(); c++) { // Boucle sur les colonnes de B
|
---|
| 271 | sum = 0;
|
---|
| 272 | pea = &((*this)(r,0)); // 1er element de la ligne r de A
|
---|
| 273 | peb = &(b(0,c)); // 1er element de la colonne c de B
|
---|
| 274 | for(k=0; k<NCols(); k++) sum += pea[k*stepa]*peb[k*stepb];
|
---|
| 275 | rm(r,c) = sum;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | rm.SetTemp(true);
|
---|
| 279 | return rm;
|
---|
| 280 | }
|
---|
| 281 |
|
---|
[762] | 282 | ///////////////////////////////////////////////////////////////
|
---|
| 283 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
| 284 | #pragma define_template TMatrix<uint_2>
|
---|
| 285 | #pragma define_template TMatrix<int_4>
|
---|
| 286 | #pragma define_template TMatrix<int_8>
|
---|
| 287 | #pragma define_template TMatrix<r_4>
|
---|
[804] | 288 | #pragma define_template TMatrix<r_8>
|
---|
[762] | 289 | #pragma define_template TMatrix< complex<r_4> >
|
---|
| 290 | #pragma define_template TMatrix< complex<r_8> >
|
---|
| 291 | #endif
|
---|
| 292 |
|
---|
| 293 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
| 294 | template class TMatrix<uint_2>;
|
---|
| 295 | template class TMatrix<int_4>;
|
---|
| 296 | template class TMatrix<int_8>;
|
---|
| 297 | template class TMatrix<r_4>;
|
---|
| 298 | template class TMatrix<r_8>;
|
---|
| 299 | template class TMatrix< complex<r_4> >;
|
---|
| 300 | template class TMatrix< complex<r_8> >;
|
---|
| 301 | #endif
|
---|