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