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