[762] | 1 | // This may look like C code, but it is really -*- C++ -*-
|
---|
| 2 | // C.Magneville 04/99
|
---|
| 3 | #ifndef TMatrix_SEEN
|
---|
| 4 | #define TMatrix_SEEN
|
---|
| 5 |
|
---|
| 6 | #include "machdefs.h"
|
---|
[804] | 7 | #include "tarray.h"
|
---|
[762] | 8 |
|
---|
| 9 | namespace SOPHYA {
|
---|
| 10 |
|
---|
| 11 | template <class T>
|
---|
[804] | 12 | class TMatrix : public TArray<T> {
|
---|
[762] | 13 | public:
|
---|
| 14 | // Creation / destruction
|
---|
| 15 | TMatrix();
|
---|
[804] | 16 | TMatrix(uint_4 r,uint_4 c, short mm=AutoMemoryMapping);
|
---|
[762] | 17 | TMatrix(const TMatrix<T>& a);
|
---|
[804] | 18 | TMatrix(const TMatrix<T>& a, bool share);
|
---|
| 19 | TMatrix(const TArray<T>& a);
|
---|
| 20 | TMatrix(const TArray<T>& a, bool share, short mm=CMemoryMapping);
|
---|
[762] | 21 | virtual ~TMatrix();
|
---|
| 22 |
|
---|
[804] | 23 | // Pour verifiez la compatibilite de dimensions lors de l'affectation
|
---|
| 24 | virtual TArray<T>& Set(const TArray<T>& a);
|
---|
| 25 | inline TMatrix<T>& operator = (const TMatrix<T>& a)
|
---|
| 26 | { Set(a); return(*this); }
|
---|
[762] | 27 |
|
---|
[804] | 28 | // Size - Changing the Size
|
---|
| 29 | inline uint_4 NRows() const {return Size(marowi_); }
|
---|
| 30 | inline uint_4 NCols() const {return Size(macoli_); }
|
---|
| 31 | inline uint_4 NCol() const {return Size(macoli_); } // back-compat Peida
|
---|
[762] | 32 |
|
---|
[804] | 33 | void ReSize(uint_4 r,uint_4 c, short mm=SameMemoryMapping); // Reallocation de place
|
---|
| 34 | void Realloc(uint_4 r,uint_4 c, short mm=SameMemoryMapping, bool force=false);
|
---|
[762] | 35 |
|
---|
[813] | 36 | // Sub-matrix extraction $CHECK$ Reza 03/2000 Doit-on declarer ces methode const ?
|
---|
| 37 | TMatrix<T> SubMatrix(Range rline, Range rcol) const ;
|
---|
| 38 | inline TMatrix<T> operator () (Range rline, Range rcol) const
|
---|
| 39 | { return SubMatrix(rline, rcol); }
|
---|
| 40 | // Lignes et colonnes de la matrice
|
---|
| 41 | inline TMatrix<T> Row(uint_4 ir) const
|
---|
| 42 | { return SubMatrix(Range(ir,ir), Range(0,NCols()-1)); }
|
---|
| 43 | inline TMatrix<T> Column(uint_4 ic) const
|
---|
| 44 | { return SubMatrix(Range(0,NRows()-1), Range(ic,ic)); }
|
---|
[804] | 45 |
|
---|
| 46 | // Inline element acces methods
|
---|
| 47 | inline T const& operator()(uint_4 r,uint_4 c) const;
|
---|
| 48 | inline T& operator()(uint_4 r,uint_4 c);
|
---|
| 49 |
|
---|
[762] | 50 | // Operations matricielles
|
---|
[804] | 51 | TMatrix<T>& Transpose();
|
---|
| 52 | //mm = SameMemoryMapping or CMemoryMapping or FortranMemoryMapping
|
---|
| 53 | TMatrix<T> Transpose(short mm);
|
---|
| 54 | // Rearranging Matrix Elements
|
---|
| 55 | TMatrix<T> Rearrange(short mm);
|
---|
[762] | 56 |
|
---|
| 57 | // Operateur d'affectation
|
---|
[804] | 58 | // A = x (matrice diagonale Identite)
|
---|
| 59 | virtual TMatrix<T>& SetIdentity(IdentityMatrix imx);
|
---|
| 60 | inline TMatrix<T>& operator = (IdentityMatrix imx) { return SetIdentity(imx); }
|
---|
[762] | 61 |
|
---|
[813] | 62 | inline TMatrix<T>& operator = (Sequence seq) { SetSeq(seq); return(*this); }
|
---|
| 63 |
|
---|
[804] | 64 | // Operations diverses avec une constante
|
---|
[813] | 65 | inline TMatrix<T>& operator = (T x) { SetT(x); return(*this); }
|
---|
[804] | 66 | inline TMatrix<T>& operator += (T x) { Add(x); return(*this); }
|
---|
| 67 | inline TMatrix<T>& operator -= (T x) { Sub(x); return(*this); }
|
---|
| 68 | inline TMatrix<T>& operator *= (T x) { Mul(x); return(*this); }
|
---|
| 69 | inline TMatrix<T>& operator /= (T x) { Div(x); return(*this); }
|
---|
[762] | 70 |
|
---|
[804] | 71 | // operations avec matrices
|
---|
[813] | 72 | inline TMatrix<T>& operator += (const TMatrix<T>& a) { AddElt(a); return(*this); }
|
---|
| 73 | inline TMatrix<T>& operator -= (const TMatrix<T>& a) { SubElt(a); return(*this); }
|
---|
| 74 | // Produit matriciel Multiply : C = (*this)*B
|
---|
| 75 | TMatrix<T> Multiply(const TMatrix<T>& b, short mm=SameMemoryMapping) const;
|
---|
| 76 | inline TMatrix<T>& operator *= (const TMatrix<T>& b)
|
---|
| 77 | { this->Set(Multiply(b)); return(*this); }
|
---|
[762] | 78 |
|
---|
[813] | 79 | // I/O print, ...
|
---|
| 80 | virtual string InfoString() const;
|
---|
[804] | 81 | virtual void Print(ostream& os, int_4 maxprt=-1, bool si=false) const ;
|
---|
[762] | 82 |
|
---|
| 83 | protected:
|
---|
| 84 | };
|
---|
| 85 |
|
---|
[804] | 86 | // ---- inline acces methods ------
|
---|
| 87 | template <class T>
|
---|
| 88 | inline T const& TMatrix<T>::operator()(uint_4 r, uint_4 c) const
|
---|
| 89 | {
|
---|
| 90 | #ifdef SO_BOUNDCHECKING
|
---|
| 91 | if (marowi_ == 0) CheckBound(r, c, 0, 0, 0, 4);
|
---|
| 92 | else CheckBound(c, r, 0, 0, 0, 4);
|
---|
| 93 | #endif
|
---|
| 94 | return ( *( mNDBlock.Begin()+ offset_+
|
---|
| 95 | r*step_[marowi_] + c*step_[macoli_] ) );
|
---|
| 96 | }
|
---|
[762] | 97 |
|
---|
| 98 | template <class T>
|
---|
[804] | 99 | inline T & TMatrix<T>::operator()(uint_4 r, uint_4 c)
|
---|
| 100 | {
|
---|
| 101 | #ifdef SO_BOUNDCHECKING
|
---|
| 102 | if (marowi_ == 0) CheckBound(r, c, 0, 0, 0, 4);
|
---|
| 103 | else CheckBound(c, r, 0, 0, 0, 4);
|
---|
| 104 | #endif
|
---|
| 105 | return ( *( mNDBlock.Begin()+ offset_+
|
---|
| 106 | r*step_[marowi_] + c*step_[macoli_] ) );
|
---|
| 107 | }
|
---|
[762] | 108 |
|
---|
| 109 |
|
---|
[813] | 110 | // Surcharge d'operateurs C = A (+,-) B
|
---|
| 111 | // $CHECK$ Reza 3/4/2000 Pas necessaire de redefinir les operateurs
|
---|
| 112 | // Defini au niveau de TArray<T> - Pour ameliorer l'efficacite
|
---|
| 113 | // Doit-on le faire aussi pour les constantes ? - Fin de $CHECK$ Reza 3/4/2000
|
---|
| 114 |
|
---|
| 115 | template <class T>
|
---|
| 116 | inline TMatrix<T> operator + (const TMatrix<T>& a,const TMatrix<T>& b)
|
---|
| 117 | {TMatrix<T> result(a); result.SetTemp(true); result.AddElt(b); return result;}
|
---|
| 118 |
|
---|
| 119 | template <class T>
|
---|
| 120 | inline TMatrix<T> operator - (const TMatrix<T>& a,const TMatrix<T>& b)
|
---|
| 121 | {TMatrix<T> result(a); result.SetTemp(true); result.SubElt(b); return result;}
|
---|
| 122 |
|
---|
[804] | 123 | // Surcharge d'operateurs C = A * B
|
---|
[762] | 124 |
|
---|
[804] | 125 | template <class T> inline TMatrix<T> operator * (const TMatrix<T>& a, const TMatrix<T>& b)
|
---|
[813] | 126 | { TMatrix<T> result(a); result.SetTemp(true); return(result.Multiply(b)); }
|
---|
[762] | 127 |
|
---|
| 128 | typedef TMatrix<r_8> Matrix;
|
---|
| 129 |
|
---|
| 130 | } // Fin du namespace
|
---|
| 131 |
|
---|
| 132 | #endif
|
---|