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