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