[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 |
|
---|
[926] | 11 | //! Class of matrices
|
---|
[762] | 12 | template <class T>
|
---|
[804] | 13 | class TMatrix : public TArray<T> {
|
---|
[762] | 14 | public:
|
---|
| 15 | // Creation / destruction
|
---|
| 16 | TMatrix();
|
---|
[804] | 17 | TMatrix(uint_4 r,uint_4 c, short mm=AutoMemoryMapping);
|
---|
[762] | 18 | TMatrix(const TMatrix<T>& a);
|
---|
[804] | 19 | TMatrix(const TMatrix<T>& a, bool share);
|
---|
| 20 | TMatrix(const TArray<T>& a);
|
---|
[914] | 21 | TMatrix(const TArray<T>& a, bool share, short mm=AutoMemoryMapping);
|
---|
[762] | 22 | virtual ~TMatrix();
|
---|
| 23 |
|
---|
[804] | 24 | // Pour verifiez la compatibilite de dimensions lors de l'affectation
|
---|
| 25 | virtual TArray<T>& Set(const TArray<T>& a);
|
---|
[894] | 26 | //! Operator = between matrices
|
---|
[804] | 27 | inline TMatrix<T>& operator = (const TMatrix<T>& a)
|
---|
[894] | 28 | { Set(a); return(*this); }
|
---|
[762] | 29 |
|
---|
[804] | 30 | // Size - Changing the Size
|
---|
[894] | 31 | //! return number of rows
|
---|
[804] | 32 | inline uint_4 NRows() const {return Size(marowi_); }
|
---|
[894] | 33 | //! return number of columns
|
---|
[804] | 34 | inline uint_4 NCols() const {return Size(macoli_); }
|
---|
[894] | 35 | //! return number of columns
|
---|
[804] | 36 | inline uint_4 NCol() const {return Size(macoli_); } // back-compat Peida
|
---|
[762] | 37 |
|
---|
[804] | 38 | void ReSize(uint_4 r,uint_4 c, short mm=SameMemoryMapping); // Reallocation de place
|
---|
| 39 | void Realloc(uint_4 r,uint_4 c, short mm=SameMemoryMapping, bool force=false);
|
---|
[762] | 40 |
|
---|
[813] | 41 | // Sub-matrix extraction $CHECK$ Reza 03/2000 Doit-on declarer ces methode const ?
|
---|
| 42 | TMatrix<T> SubMatrix(Range rline, Range rcol) const ;
|
---|
[894] | 43 | //! () : Return submatrix define by \b Range \b rline and \b rcol
|
---|
[813] | 44 | inline TMatrix<T> operator () (Range rline, Range rcol) const
|
---|
| 45 | { return SubMatrix(rline, rcol); }
|
---|
| 46 | // Lignes et colonnes de la matrice
|
---|
[894] | 47 | //! Return submatrix define by line \b ir (line vector)
|
---|
[813] | 48 | inline TMatrix<T> Row(uint_4 ir) const
|
---|
| 49 | { return SubMatrix(Range(ir,ir), Range(0,NCols()-1)); }
|
---|
[894] | 50 | //! Return submatrix define by column \b ic (column vector)
|
---|
[813] | 51 | inline TMatrix<T> Column(uint_4 ic) const
|
---|
| 52 | { return SubMatrix(Range(0,NRows()-1), Range(ic,ic)); }
|
---|
[804] | 53 |
|
---|
| 54 | // Inline element acces methods
|
---|
| 55 | inline T const& operator()(uint_4 r,uint_4 c) const;
|
---|
| 56 | inline T& operator()(uint_4 r,uint_4 c);
|
---|
| 57 |
|
---|
[762] | 58 | // Operations matricielles
|
---|
[804] | 59 | TMatrix<T>& Transpose();
|
---|
| 60 | //mm = SameMemoryMapping or CMemoryMapping or FortranMemoryMapping
|
---|
| 61 | TMatrix<T> Transpose(short mm);
|
---|
| 62 | // Rearranging Matrix Elements
|
---|
| 63 | TMatrix<T> Rearrange(short mm);
|
---|
[762] | 64 |
|
---|
| 65 | // Operateur d'affectation
|
---|
[804] | 66 | // A = x (matrice diagonale Identite)
|
---|
| 67 | virtual TMatrix<T>& SetIdentity(IdentityMatrix imx);
|
---|
[894] | 68 | // = : fill matrix with an identity matrix \b imx
|
---|
[804] | 69 | inline TMatrix<T>& operator = (IdentityMatrix imx) { return SetIdentity(imx); }
|
---|
[762] | 70 |
|
---|
[894] | 71 | // = : fill matrix with a Sequence \b seq
|
---|
[813] | 72 | inline TMatrix<T>& operator = (Sequence seq) { SetSeq(seq); return(*this); }
|
---|
| 73 |
|
---|
[804] | 74 | // Operations diverses avec une constante
|
---|
[894] | 75 | //! = : fill matrix with constant value \b x
|
---|
[813] | 76 | inline TMatrix<T>& operator = (T x) { SetT(x); return(*this); }
|
---|
[894] | 77 | //! += : add constant value \b x to matrix
|
---|
[804] | 78 | inline TMatrix<T>& operator += (T x) { Add(x); return(*this); }
|
---|
[894] | 79 | //! -= : substract constant value \b x to matrix
|
---|
[804] | 80 | inline TMatrix<T>& operator -= (T x) { Sub(x); return(*this); }
|
---|
[894] | 81 | //! *= : multiply matrix by constant value \b x
|
---|
[804] | 82 | inline TMatrix<T>& operator *= (T x) { Mul(x); return(*this); }
|
---|
[894] | 83 | //! /= : divide matrix by constant value \b x
|
---|
[804] | 84 | inline TMatrix<T>& operator /= (T x) { Div(x); return(*this); }
|
---|
[762] | 85 |
|
---|
[804] | 86 | // operations avec matrices
|
---|
[894] | 87 | //! += : add a matrix
|
---|
[813] | 88 | inline TMatrix<T>& operator += (const TMatrix<T>& a) { AddElt(a); return(*this); }
|
---|
[894] | 89 | //! -= : substract a matrix
|
---|
[813] | 90 | inline TMatrix<T>& operator -= (const TMatrix<T>& a) { SubElt(a); return(*this); }
|
---|
| 91 | TMatrix<T> Multiply(const TMatrix<T>& b, short mm=SameMemoryMapping) const;
|
---|
[894] | 92 | //! *= : matrix product : C = (*this)*B
|
---|
[813] | 93 | inline TMatrix<T>& operator *= (const TMatrix<T>& b)
|
---|
| 94 | { this->Set(Multiply(b)); return(*this); }
|
---|
[762] | 95 |
|
---|
[813] | 96 | // I/O print, ...
|
---|
| 97 | virtual string InfoString() const;
|
---|
[804] | 98 | virtual void Print(ostream& os, int_4 maxprt=-1, bool si=false) const ;
|
---|
[762] | 99 |
|
---|
| 100 | protected:
|
---|
| 101 | };
|
---|
| 102 |
|
---|
[804] | 103 | // ---- inline acces methods ------
|
---|
[894] | 104 | //! () : return element for line \b r and column \b c
|
---|
[804] | 105 | template <class T>
|
---|
| 106 | inline T const& TMatrix<T>::operator()(uint_4 r, uint_4 c) const
|
---|
| 107 | {
|
---|
| 108 | #ifdef SO_BOUNDCHECKING
|
---|
| 109 | if (marowi_ == 0) CheckBound(r, c, 0, 0, 0, 4);
|
---|
| 110 | else CheckBound(c, r, 0, 0, 0, 4);
|
---|
| 111 | #endif
|
---|
| 112 | return ( *( mNDBlock.Begin()+ offset_+
|
---|
| 113 | r*step_[marowi_] + c*step_[macoli_] ) );
|
---|
| 114 | }
|
---|
[762] | 115 |
|
---|
[894] | 116 | //! () : return element for line \b r and column \b c
|
---|
[762] | 117 | template <class T>
|
---|
[804] | 118 | inline T & TMatrix<T>::operator()(uint_4 r, uint_4 c)
|
---|
| 119 | {
|
---|
| 120 | #ifdef SO_BOUNDCHECKING
|
---|
| 121 | if (marowi_ == 0) CheckBound(r, c, 0, 0, 0, 4);
|
---|
| 122 | else CheckBound(c, r, 0, 0, 0, 4);
|
---|
| 123 | #endif
|
---|
| 124 | return ( *( mNDBlock.Begin()+ offset_+
|
---|
| 125 | r*step_[marowi_] + c*step_[macoli_] ) );
|
---|
| 126 | }
|
---|
[762] | 127 |
|
---|
[813] | 128 | // Surcharge d'operateurs C = A (+,-) B
|
---|
| 129 | // $CHECK$ Reza 3/4/2000 Pas necessaire de redefinir les operateurs
|
---|
| 130 | // Defini au niveau de TArray<T> - Pour ameliorer l'efficacite
|
---|
| 131 | // Doit-on le faire aussi pour les constantes ? - Fin de $CHECK$ Reza 3/4/2000
|
---|
| 132 |
|
---|
[958] | 133 | /*! \ingroup TArray \fn operator+(const TMatrix<T>&,const TMatrix<T>&)
|
---|
| 134 | \brief + : add matrixes \b a and \b b */
|
---|
[813] | 135 | template <class T>
|
---|
| 136 | inline TMatrix<T> operator + (const TMatrix<T>& a,const TMatrix<T>& b)
|
---|
| 137 | {TMatrix<T> result(a); result.SetTemp(true); result.AddElt(b); return result;}
|
---|
| 138 |
|
---|
[958] | 139 | /*! \ingroup TArray \fn operator-(const TMatrix<T>&,const TMatrix<T>&)
|
---|
| 140 | \brief \- : substract matrixes \b a and \b b */
|
---|
[813] | 141 | template <class T>
|
---|
| 142 | inline TMatrix<T> operator - (const TMatrix<T>& a,const TMatrix<T>& b)
|
---|
| 143 | {TMatrix<T> result(a); result.SetTemp(true); result.SubElt(b); return result;}
|
---|
| 144 |
|
---|
[804] | 145 | // Surcharge d'operateurs C = A * B
|
---|
[958] | 146 | /*! \ingroup TArray \fn operator*(const TMatrix<T>&,const TMatrix<T>&)
|
---|
| 147 | \brief * : multiply matrixes \b a and \b b */
|
---|
[804] | 148 | template <class T> inline TMatrix<T> operator * (const TMatrix<T>& a, const TMatrix<T>& b)
|
---|
[813] | 149 | { TMatrix<T> result(a); result.SetTemp(true); return(result.Multiply(b)); }
|
---|
[762] | 150 |
|
---|
[956] | 151 | // Typedef pour simplifier et compatibilite Peida
|
---|
| 152 | /*! \ingroup TArray
|
---|
| 153 | \typedef Matrix
|
---|
| 154 | \brief To simplified TMatrix<r_8> writing
|
---|
| 155 | */
|
---|
[762] | 156 | typedef TMatrix<r_8> Matrix;
|
---|
| 157 |
|
---|
| 158 | } // Fin du namespace
|
---|
| 159 |
|
---|
| 160 | #endif
|
---|