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