| [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: | 
|---|
| [2884] | 15 |  | 
|---|
|  | 16 | #include "tmatrix_tsnl.h"  /* For two level name look-up gcc >= 3.4 */ | 
|---|
|  | 17 |  | 
|---|
| [762] | 18 | // Creation / destruction | 
|---|
|  | 19 | TMatrix(); | 
|---|
| [2575] | 20 | TMatrix(sa_size_t r,sa_size_t c, short mm=BaseArray::AutoMemoryMapping, bool fzero=true); | 
|---|
| [762] | 21 | TMatrix(const TMatrix<T>& a); | 
|---|
| [804] | 22 | TMatrix(const TMatrix<T>& a, bool share); | 
|---|
| [2752] | 23 | TMatrix(const TArray<T>& a,  bool share=true); | 
|---|
| [1081] | 24 | TMatrix(const BaseArray& a); | 
|---|
| [1003] | 25 |  | 
|---|
| [762] | 26 | virtual ~TMatrix(); | 
|---|
|  | 27 |  | 
|---|
| [804] | 28 | //  Pour verifiez la compatibilite de dimensions lors de l'affectation | 
|---|
|  | 29 | virtual TArray<T>& Set(const TArray<T>& a); | 
|---|
| [894] | 30 | //! Operator = between matrices | 
|---|
| [976] | 31 | /*! \warning Datas are copied (cloned) from \b a. | 
|---|
|  | 32 | \sa NDataBlock::operator=(const NDataBlock<T>&) */ | 
|---|
| [804] | 33 | inline  TMatrix<T>& operator = (const TMatrix<T>& a) | 
|---|
| [894] | 34 | { Set(a);  return(*this); } | 
|---|
| [1099] | 35 | //! Operator = between a matrix and an array | 
|---|
|  | 36 | inline  TMatrix<T>& operator = (const TArray<T>& a) | 
|---|
|  | 37 | { Set(a);  return(*this); } | 
|---|
| [762] | 38 |  | 
|---|
| [1081] | 39 | virtual TArray<T>& SetBA(const BaseArray& a); | 
|---|
| [1099] | 40 | //! Operator = between matrices with different types | 
|---|
| [1081] | 41 | inline  TMatrix<T>& operator = (const BaseArray& a) | 
|---|
|  | 42 | { SetBA(a);  return(*this); } | 
|---|
|  | 43 |  | 
|---|
| [804] | 44 | // Size - Changing the Size | 
|---|
| [894] | 45 | //! return number of rows | 
|---|
| [2421] | 46 | inline sa_size_t NRows() const {return size_[marowi_]; } | 
|---|
| [894] | 47 | //! return number of columns | 
|---|
| [2421] | 48 | inline sa_size_t NCols() const {return size_[macoli_]; } | 
|---|
| [894] | 49 | //! return number of columns | 
|---|
| [2421] | 50 | inline sa_size_t NCol()  const {return size_[macoli_]; } // back-compat Peida | 
|---|
| [762] | 51 |  | 
|---|
| [2575] | 52 | void ReSize(sa_size_t r,sa_size_t c, short mm=BaseArray::SameMemoryMapping, bool fzero=true); | 
|---|
| [1412] | 53 | //! a synonym (alias) for method ReSize(sa_size_t, sa_size_t, short) | 
|---|
| [2575] | 54 | inline void SetSize(sa_size_t r,sa_size_t c, short mm=BaseArray::SameMemoryMapping, bool fzero=true) | 
|---|
|  | 55 | { ReSize(r, c, mm, fzero); } | 
|---|
| [1412] | 56 | // Reallocation de place | 
|---|
| [1156] | 57 | void Realloc(sa_size_t r,sa_size_t c, short mm=BaseArray::SameMemoryMapping, bool force=false); | 
|---|
| [762] | 58 |  | 
|---|
| [813] | 59 | // Sub-matrix extraction $CHECK$ Reza 03/2000  Doit-on declarer ces methode const ? | 
|---|
|  | 60 | TMatrix<T> SubMatrix(Range rline, Range rcol) const ; | 
|---|
| [894] | 61 | //! () : Return submatrix define by \b Range \b rline and \b rcol | 
|---|
| [813] | 62 | inline TMatrix<T> operator () (Range rline, Range rcol) const | 
|---|
|  | 63 | { return SubMatrix(rline, rcol); } | 
|---|
|  | 64 | // Lignes et colonnes de la matrice | 
|---|
| [894] | 65 | //! Return submatrix define by line \b ir (line vector) | 
|---|
| [1156] | 66 | inline TMatrix<T> Row(sa_size_t ir) const | 
|---|
| [813] | 67 | { return SubMatrix(Range(ir,ir), Range(0,NCols()-1)); } | 
|---|
| [894] | 68 | //! Return submatrix define by column \b ic (column vector) | 
|---|
| [1156] | 69 | inline TMatrix<T> Column(sa_size_t ic) const | 
|---|
| [813] | 70 | { return SubMatrix(Range(0,NRows()-1), Range(ic,ic)); } | 
|---|
| [804] | 71 |  | 
|---|
|  | 72 | // Inline element acces methods | 
|---|
| [1156] | 73 | inline T const& operator()(sa_size_t r,sa_size_t c) const; | 
|---|
|  | 74 | inline T&       operator()(sa_size_t r,sa_size_t c); | 
|---|
| [804] | 75 |  | 
|---|
| [762] | 76 | // Operations matricielles | 
|---|
| [1412] | 77 | TMatrix<T>& TransposeSelf(); | 
|---|
| [2421] | 78 | TMatrix<T>  Transpose() const; | 
|---|
| [804] | 79 | //mm = SameMemoryMapping or CMemoryMapping or FortranMemoryMapping | 
|---|
| [2421] | 80 | TMatrix<T>  Transpose(short mm) const ; | 
|---|
| [804] | 81 | // Rearranging Matrix Elements | 
|---|
| [2421] | 82 | TMatrix<T>  Rearrange(short mm) const; | 
|---|
| [762] | 83 |  | 
|---|
|  | 84 | // Operateur d'affectation | 
|---|
| [804] | 85 | // A = x (matrice diagonale Identite) | 
|---|
|  | 86 | virtual TMatrix<T>& SetIdentity(IdentityMatrix imx); | 
|---|
| [894] | 87 | // = : fill matrix with an identity matrix \b imx | 
|---|
| [804] | 88 | inline  TMatrix<T>& operator = (IdentityMatrix imx) { return SetIdentity(imx); } | 
|---|
| [762] | 89 |  | 
|---|
| [894] | 90 | // = : fill matrix with a Sequence \b seq | 
|---|
| [1103] | 91 | inline  TMatrix<T>&  operator = (Sequence const & seq)    { SetSeq(seq); return(*this); } | 
|---|
| [813] | 92 |  | 
|---|
| [804] | 93 | // Operations diverses  avec une constante | 
|---|
| [894] | 94 | //! = : fill matrix with constant value \b x | 
|---|
| [813] | 95 | inline  TMatrix<T>&  operator = (T x)             { SetT(x); return(*this); } | 
|---|
| [894] | 96 | //! += : add constant value \b x to matrix | 
|---|
| [2564] | 97 | inline  TMatrix<T>&  operator += (T x)            { AddCst(x,*this); return(*this); } | 
|---|
| [894] | 98 | //! -= : substract constant value \b x to matrix | 
|---|
| [2564] | 99 | inline  TMatrix<T>&  operator -= (T x)            { SubCst(x,*this); return(*this); } | 
|---|
| [894] | 100 | //! *= : multiply matrix by constant value \b x | 
|---|
| [2564] | 101 | inline  TMatrix<T>&  operator *= (T x)            { MulCst(x,*this); return(*this); } | 
|---|
| [894] | 102 | //! /= : divide matrix by constant value \b x | 
|---|
| [2564] | 103 | inline  TMatrix<T>&  operator /= (T x)            { DivCst(x,*this); return(*this); } | 
|---|
| [762] | 104 |  | 
|---|
| [804] | 105 | //  operations avec matrices | 
|---|
| [894] | 106 | //! += : add a matrix | 
|---|
| [2575] | 107 | inline  TMatrix<T>&  operator += (const TMatrix<T>& a)  { AddElt(a,*this); return(*this); } | 
|---|
| [894] | 108 | //! -= : substract a matrix | 
|---|
| [2575] | 109 | inline  TMatrix<T>&  operator -= (const TMatrix<T>& a)  { SubElt(a,*this); return(*this); } | 
|---|
|  | 110 |  | 
|---|
| [1003] | 111 | TMatrix<T>  Multiply(const TMatrix<T>& b, short mm=BaseArray::SameMemoryMapping) const; | 
|---|
| [2575] | 112 | //A supprimer ? Reza Juillet 2004 ! *= : matrix product : C = (*this)*B | 
|---|
|  | 113 | //  inline  TMatrix<T>&  operator *= (const TMatrix<T>& b) | 
|---|
|  | 114 | //       { this->Set(Multiply(b));  return(*this); } | 
|---|
| [762] | 115 |  | 
|---|
| [813] | 116 | // I/O print, ... | 
|---|
|  | 117 | virtual string InfoString() const; | 
|---|
| [1581] | 118 | virtual void  Print(ostream& os, sa_size_t maxprt=-1, | 
|---|
| [1554] | 119 | bool si=false, bool ascd=false) const ; | 
|---|
| [762] | 120 |  | 
|---|
|  | 121 | protected: | 
|---|
|  | 122 | }; | 
|---|
|  | 123 |  | 
|---|
| [804] | 124 | //  ---- inline acces methods ------ | 
|---|
| [894] | 125 | //! () : return element for line \b r and column \b c | 
|---|
| [804] | 126 | template <class T> | 
|---|
| [1156] | 127 | inline T const& TMatrix<T>::operator()(sa_size_t r, sa_size_t c) const | 
|---|
| [804] | 128 | { | 
|---|
|  | 129 | #ifdef SO_BOUNDCHECKING | 
|---|
|  | 130 | if (marowi_ == 0)   CheckBound(r, c, 0, 0, 0, 4); | 
|---|
|  | 131 | else   CheckBound(c, r, 0, 0, 0, 4); | 
|---|
|  | 132 | #endif | 
|---|
|  | 133 | return ( *( mNDBlock.Begin()+ offset_+ | 
|---|
|  | 134 | r*step_[marowi_] + c*step_[macoli_] ) ); | 
|---|
|  | 135 | } | 
|---|
| [762] | 136 |  | 
|---|
| [894] | 137 | //! () : return element for line \b r and column \b c | 
|---|
| [762] | 138 | template <class T> | 
|---|
| [1156] | 139 | inline T & TMatrix<T>::operator()(sa_size_t r, sa_size_t c) | 
|---|
| [804] | 140 | { | 
|---|
|  | 141 | #ifdef SO_BOUNDCHECKING | 
|---|
|  | 142 | if (marowi_ == 0)   CheckBound(r, c, 0, 0, 0, 4); | 
|---|
|  | 143 | else   CheckBound(c, r, 0, 0, 0, 4); | 
|---|
|  | 144 | #endif | 
|---|
|  | 145 | return ( *( mNDBlock.Begin()+ offset_+ | 
|---|
|  | 146 | r*step_[marowi_] + c*step_[macoli_] ) ); | 
|---|
|  | 147 | } | 
|---|
| [1103] | 148 | //////////////////////////////////////////////////////////////// | 
|---|
|  | 149 | // Surcharge d'operateurs A (+,-,*,/) (T) x | 
|---|
| [762] | 150 |  | 
|---|
| [1103] | 151 | /*! \ingroup TMatrix \fn operator+(const TMatrix<T>&,T) | 
|---|
|  | 152 | \brief Operator TMatrix = TMatrix + constant */ | 
|---|
|  | 153 | template <class T> inline TMatrix<T> operator + (const TMatrix<T>& a, T b) | 
|---|
| [2564] | 154 | {TMatrix<T> result; result.SetTemp(true); | 
|---|
|  | 155 | a.AddCst(b,result); return result;} | 
|---|
| [1103] | 156 |  | 
|---|
|  | 157 | /*! \ingroup TMatrix \fn operator+(T,const TMatrix<T>&) | 
|---|
|  | 158 | \brief Operator TMatrix = constant + TMatrix */ | 
|---|
|  | 159 | template <class T> inline TMatrix<T> operator + (T b,const TMatrix<T>& a) | 
|---|
| [2564] | 160 | {TMatrix<T> result; result.SetTemp(true); | 
|---|
|  | 161 | a.AddCst(b,result); return result;} | 
|---|
| [1103] | 162 |  | 
|---|
|  | 163 | /*! \ingroup TMatrix \fn operator-(const TMatrix<T>&,T) | 
|---|
|  | 164 | \brief Operator TMatrix = TMatrix - constant */ | 
|---|
|  | 165 | template <class T> inline TMatrix<T> operator - (const TMatrix<T>& a, T b) | 
|---|
| [2564] | 166 | {TMatrix<T> result; result.SetTemp(true); | 
|---|
|  | 167 | a.SubCst(b,result); return result;} | 
|---|
| [1103] | 168 |  | 
|---|
|  | 169 | /*! \ingroup TMatrix \fn operator-(T,const TMatrix<T>&) | 
|---|
|  | 170 | \brief Operator TMatrix = constant - TMatrix */ | 
|---|
|  | 171 | template <class T> inline TMatrix<T> operator - (T b,const TMatrix<T>& a) | 
|---|
| [2564] | 172 | {TMatrix<T> result; result.SetTemp(true); | 
|---|
|  | 173 | a.SubCst(b,result,true); return result;} | 
|---|
| [1103] | 174 |  | 
|---|
|  | 175 | /*! \ingroup TMatrix \fn operator*(const TMatrix<T>&,T) | 
|---|
|  | 176 | \brief Operator TMatrix = TMatrix * constant */ | 
|---|
|  | 177 | template <class T> inline TMatrix<T> operator * (const TMatrix<T>& a, T b) | 
|---|
| [2564] | 178 | {TMatrix<T> result; result.SetTemp(true); | 
|---|
|  | 179 | a.MulCst(b,result); return result;} | 
|---|
| [1103] | 180 |  | 
|---|
|  | 181 | /*! \ingroup TMatrix \fn operator*(T,const TMatrix<T>&) | 
|---|
|  | 182 | \brief Operator TMatrix = constant * TMatrix */ | 
|---|
|  | 183 | template <class T> inline TMatrix<T> operator * (T b,const TMatrix<T>& a) | 
|---|
| [2564] | 184 | {TMatrix<T> result; result.SetTemp(true); | 
|---|
|  | 185 | a.MulCst(b,result); return result;} | 
|---|
| [1103] | 186 |  | 
|---|
|  | 187 | /*! \ingroup TMatrix \fn operator/(const TMatrix<T>&,T) | 
|---|
|  | 188 | \brief Operator TMatrix = TMatrix / constant */ | 
|---|
|  | 189 | template <class T> inline TMatrix<T> operator / (const TMatrix<T>& a, T b) | 
|---|
| [2564] | 190 | {TMatrix<T> result; result.SetTemp(true); | 
|---|
|  | 191 | a.DivCst(b,result); return result;} | 
|---|
| [1103] | 192 |  | 
|---|
|  | 193 | /*! \ingroup TMatrix \fn operator/(T,const TMatrix<T>&) | 
|---|
|  | 194 | \brief Operator TMatrix = constant / TMatrix  */ | 
|---|
|  | 195 | template <class T> inline TMatrix<T> operator / (T b, const TMatrix<T>& a) | 
|---|
| [2564] | 196 | {TMatrix<T> result; result.SetTemp(true); | 
|---|
|  | 197 | a.Div(b,result,true); return result;} | 
|---|
| [1103] | 198 |  | 
|---|
| [1156] | 199 | //////////////////////////////////////////////////////////////// | 
|---|
|  | 200 | // Surcharge d'operateurs B = -A | 
|---|
| [1103] | 201 |  | 
|---|
| [1156] | 202 | /*! \ingroup TMatrix \fn operator - (const TMatrix<T>&) | 
|---|
|  | 203 | \brief Operator - Returns a matrix with elements equal to the opposite of | 
|---|
|  | 204 | the original matrix elements.  */ | 
|---|
|  | 205 | template <class T> inline TMatrix<T> operator - (const TMatrix<T>& a) | 
|---|
| [2575] | 206 | {TMatrix<T> result; result.SetTemp(true); | 
|---|
|  | 207 | a.NegateElt(result); return result;} | 
|---|
| [1156] | 208 |  | 
|---|
|  | 209 |  | 
|---|
| [813] | 210 | // Surcharge d'operateurs C = A (+,-) B | 
|---|
|  | 211 | // $CHECK$ Reza 3/4/2000 Pas necessaire  de redefinir les operateurs | 
|---|
|  | 212 | // Defini au niveau de TArray<T> - Pour ameliorer l'efficacite | 
|---|
|  | 213 | // Doit-on le faire aussi pour les constantes ? - Fin de $CHECK$ Reza 3/4/2000 | 
|---|
|  | 214 |  | 
|---|
| [958] | 215 | /*! \ingroup TArray \fn operator+(const TMatrix<T>&,const TMatrix<T>&) | 
|---|
|  | 216 | \brief + : add matrixes \b a and \b b */ | 
|---|
| [813] | 217 | template <class T> | 
|---|
|  | 218 | inline TMatrix<T> operator + (const TMatrix<T>& a,const TMatrix<T>& b) | 
|---|
| [970] | 219 | {TMatrix<T> result; result.SetTemp(true); | 
|---|
| [2575] | 220 | a.AddElt(b, result);     return result; } | 
|---|
| [813] | 221 |  | 
|---|
| [970] | 222 |  | 
|---|
| [958] | 223 | /*! \ingroup TArray \fn operator-(const TMatrix<T>&,const TMatrix<T>&) | 
|---|
|  | 224 | \brief \- : substract matrixes \b a and \b b */ | 
|---|
| [813] | 225 | template <class T> | 
|---|
|  | 226 | inline TMatrix<T> operator - (const TMatrix<T>& a,const TMatrix<T>& b) | 
|---|
| [970] | 227 | {TMatrix<T> result; result.SetTemp(true); | 
|---|
| [2575] | 228 | a.SubElt(b, result);     return result; } | 
|---|
|  | 229 |  | 
|---|
| [813] | 230 |  | 
|---|
| [804] | 231 | // Surcharge d'operateurs C = A * B | 
|---|
| [958] | 232 | /*! \ingroup TArray \fn operator*(const TMatrix<T>&,const TMatrix<T>&) | 
|---|
|  | 233 | \brief * : multiply matrixes \b a and \b b */ | 
|---|
| [804] | 234 | template <class T> inline TMatrix<T> operator * (const TMatrix<T>& a, const TMatrix<T>& b) | 
|---|
| [970] | 235 | { return(a.Multiply(b)); } | 
|---|
| [762] | 236 |  | 
|---|
| [956] | 237 | // Typedef pour simplifier et compatibilite Peida | 
|---|
|  | 238 | /*! \ingroup TArray | 
|---|
|  | 239 | \typedef Matrix | 
|---|
|  | 240 | \brief To simplified TMatrix<r_8> writing | 
|---|
|  | 241 | */ | 
|---|
| [762] | 242 | typedef TMatrix<r_8> Matrix; | 
|---|
|  | 243 |  | 
|---|
|  | 244 | } // Fin du namespace | 
|---|
|  | 245 |  | 
|---|
|  | 246 | #endif | 
|---|