| 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 | 
 | 
|---|
| 16 | #include "tmatrix_tsnl.h"  /* For two level name look-up gcc >= 3.4 */
 | 
|---|
| 17 | 
 | 
|---|
| 18 |   // Creation / destruction 
 | 
|---|
| 19 |   TMatrix();
 | 
|---|
| 20 |   TMatrix(sa_size_t r,sa_size_t c, short mm=BaseArray::AutoMemoryMapping, bool fzero=true);
 | 
|---|
| 21 |   TMatrix(const TMatrix<T>& a);
 | 
|---|
| 22 |   TMatrix(const TMatrix<T>& a, bool share);
 | 
|---|
| 23 |   TMatrix(const TArray<T>& a,  bool share=true);
 | 
|---|
| 24 |   TMatrix(const BaseArray& a); 
 | 
|---|
| 25 | 
 | 
|---|
| 26 |   virtual ~TMatrix();
 | 
|---|
| 27 | 
 | 
|---|
| 28 |   //  Pour verifiez la compatibilite de dimensions lors de l'affectation
 | 
|---|
| 29 |   virtual TArray<T>& Set(const TArray<T>& a);
 | 
|---|
| 30 |   //! Operator = between matrices
 | 
|---|
| 31 |   /*! \warning Datas are copied (cloned) from \b a.
 | 
|---|
| 32 |       \sa NDataBlock::operator=(const NDataBlock<T>&) */
 | 
|---|
| 33 |   inline  TMatrix<T>& operator = (const TMatrix<T>& a)
 | 
|---|
| 34 |                      { Set(a);  return(*this); }
 | 
|---|
| 35 |   //! Operator = between a matrix and an array 
 | 
|---|
| 36 |   inline  TMatrix<T>& operator = (const TArray<T>& a)
 | 
|---|
| 37 |                      { Set(a);  return(*this); }
 | 
|---|
| 38 | 
 | 
|---|
| 39 |   virtual TArray<T>& SetBA(const BaseArray& a);
 | 
|---|
| 40 |   //! Operator = between matrices with different types
 | 
|---|
| 41 |   inline  TMatrix<T>& operator = (const BaseArray& a)
 | 
|---|
| 42 |                      { SetBA(a);  return(*this); }
 | 
|---|
| 43 | 
 | 
|---|
| 44 |   // Size - Changing the Size 
 | 
|---|
| 45 |   //! return number of rows
 | 
|---|
| 46 |   inline sa_size_t NRows() const {return size_[marowi_]; }
 | 
|---|
| 47 |   //! return number of columns
 | 
|---|
| 48 |   inline sa_size_t NCols() const {return size_[macoli_]; }
 | 
|---|
| 49 |   //! return number of columns
 | 
|---|
| 50 |   inline sa_size_t NCol()  const {return size_[macoli_]; } // back-compat Peida
 | 
|---|
| 51 | 
 | 
|---|
| 52 |   void ReSize(sa_size_t r,sa_size_t c, short mm=BaseArray::SameMemoryMapping, bool fzero=true);
 | 
|---|
| 53 |   //! a synonym (alias) for method ReSize(sa_size_t, sa_size_t, short)
 | 
|---|
| 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); }
 | 
|---|
| 56 |   // Reallocation de place
 | 
|---|
| 57 |   void Realloc(sa_size_t r,sa_size_t c, short mm=BaseArray::SameMemoryMapping, bool force=false);
 | 
|---|
| 58 | 
 | 
|---|
| 59 |   // Sub-matrix extraction $CHECK$ Reza 03/2000  Doit-on declarer ces methode const ?
 | 
|---|
| 60 |   TMatrix<T> SubMatrix(Range rline, Range rcol) const ;
 | 
|---|
| 61 |   //! () : Return submatrix define by \b Range \b rline and \b rcol
 | 
|---|
| 62 |   inline TMatrix<T> operator () (Range rline, Range rcol) const 
 | 
|---|
| 63 |                     { return SubMatrix(rline, rcol); }
 | 
|---|
| 64 |   // Lignes et colonnes de la matrice
 | 
|---|
| 65 |   //! Return submatrix define by line \b ir (line vector)
 | 
|---|
| 66 |   inline TMatrix<T> Row(sa_size_t ir) const
 | 
|---|
| 67 |                     { return SubMatrix(Range(ir,ir), Range(0,NCols()-1)); }
 | 
|---|
| 68 |   //! Return submatrix define by column \b ic (column vector)
 | 
|---|
| 69 |   inline TMatrix<T> Column(sa_size_t ic) const
 | 
|---|
| 70 |                     { return SubMatrix(Range(0,NRows()-1), Range(ic,ic)); }
 | 
|---|
| 71 | 
 | 
|---|
| 72 |   // Inline element acces methods 
 | 
|---|
| 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);
 | 
|---|
| 75 | 
 | 
|---|
| 76 |   // Operations matricielles
 | 
|---|
| 77 |   TMatrix<T>& TransposeSelf();
 | 
|---|
| 78 |   TMatrix<T>  Transpose() const;
 | 
|---|
| 79 |   //mm = SameMemoryMapping or CMemoryMapping or FortranMemoryMapping 
 | 
|---|
| 80 |   TMatrix<T>  Transpose(short mm) const ; 
 | 
|---|
| 81 |   // Rearranging Matrix Elements
 | 
|---|
| 82 |   TMatrix<T>  Rearrange(short mm) const;
 | 
|---|
| 83 | 
 | 
|---|
| 84 |   // Operateur d'affectation
 | 
|---|
| 85 |   // A = x (matrice diagonale Identite)
 | 
|---|
| 86 |   virtual TMatrix<T>& SetIdentity(IdentityMatrix imx);
 | 
|---|
| 87 |   // = : fill matrix with an identity matrix \b imx
 | 
|---|
| 88 |   inline  TMatrix<T>& operator = (IdentityMatrix imx) { return SetIdentity(imx); }
 | 
|---|
| 89 | 
 | 
|---|
| 90 |   // = : fill matrix with a Sequence \b seq
 | 
|---|
| 91 |   inline  TMatrix<T>&  operator = (Sequence const & seq)    { SetSeq(seq); return(*this); }
 | 
|---|
| 92 | 
 | 
|---|
| 93 |   // Operations diverses  avec une constante
 | 
|---|
| 94 |   //! = : fill matrix with constant value \b x
 | 
|---|
| 95 |   inline  TMatrix<T>&  operator = (T x)             { SetT(x); return(*this); }
 | 
|---|
| 96 |   //! += : add constant value \b x to matrix
 | 
|---|
| 97 |   inline  TMatrix<T>&  operator += (T x)            { AddCst(x,*this); return(*this); }
 | 
|---|
| 98 |   //! -= : substract constant value \b x to matrix
 | 
|---|
| 99 |   inline  TMatrix<T>&  operator -= (T x)            { SubCst(x,*this); return(*this); }
 | 
|---|
| 100 |   //! *= : multiply matrix by constant value \b x
 | 
|---|
| 101 |   inline  TMatrix<T>&  operator *= (T x)            { MulCst(x,*this); return(*this); }
 | 
|---|
| 102 |   //! /= : divide matrix by constant value \b x
 | 
|---|
| 103 |   inline  TMatrix<T>&  operator /= (T x)            { DivCst(x,*this); return(*this); }
 | 
|---|
| 104 | 
 | 
|---|
| 105 |   //  operations avec matrices 
 | 
|---|
| 106 |   //! += : add a matrix
 | 
|---|
| 107 |   inline  TMatrix<T>&  operator += (const TMatrix<T>& a)  { AddElt(a,*this); return(*this); }
 | 
|---|
| 108 |   //! -= : substract a matrix
 | 
|---|
| 109 |   inline  TMatrix<T>&  operator -= (const TMatrix<T>& a)  { SubElt(a,*this); return(*this); }
 | 
|---|
| 110 | 
 | 
|---|
| 111 |   TMatrix<T>  Multiply(const TMatrix<T>& b, short mm=BaseArray::SameMemoryMapping) const;
 | 
|---|
| 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); }
 | 
|---|
| 115 | 
 | 
|---|
| 116 |   // I/O print, ...
 | 
|---|
| 117 |   virtual string InfoString() const;
 | 
|---|
| 118 |   virtual void  Print(ostream& os, sa_size_t maxprt=-1, 
 | 
|---|
| 119 |                       bool si=false, bool ascd=false) const ;
 | 
|---|
| 120 | 
 | 
|---|
| 121 | protected:
 | 
|---|
| 122 | };
 | 
|---|
| 123 | 
 | 
|---|
| 124 | //  ---- inline acces methods ------
 | 
|---|
| 125 |  //! () : return element for line \b r and column \b c
 | 
|---|
| 126 | template <class T>
 | 
|---|
| 127 | inline T const& TMatrix<T>::operator()(sa_size_t r, sa_size_t c) const
 | 
|---|
| 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 | }
 | 
|---|
| 136 | 
 | 
|---|
| 137 | //! () : return element for line \b r and column \b c
 | 
|---|
| 138 | template <class T>
 | 
|---|
| 139 | inline T & TMatrix<T>::operator()(sa_size_t r, sa_size_t c) 
 | 
|---|
| 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 | }
 | 
|---|
| 148 | ////////////////////////////////////////////////////////////////
 | 
|---|
| 149 | // Surcharge d'operateurs A (+,-,*,/) (T) x
 | 
|---|
| 150 | 
 | 
|---|
| 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)
 | 
|---|
| 154 |     {TMatrix<T> result; result.SetTemp(true); 
 | 
|---|
| 155 |     a.AddCst(b,result); return result;}
 | 
|---|
| 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)
 | 
|---|
| 160 |     {TMatrix<T> result; result.SetTemp(true); 
 | 
|---|
| 161 |     a.AddCst(b,result); return result;}
 | 
|---|
| 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)
 | 
|---|
| 166 |     {TMatrix<T> result; result.SetTemp(true); 
 | 
|---|
| 167 |     a.SubCst(b,result); return result;}
 | 
|---|
| 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)
 | 
|---|
| 172 |     {TMatrix<T> result; result.SetTemp(true); 
 | 
|---|
| 173 |     a.SubCst(b,result,true); return result;}
 | 
|---|
| 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)
 | 
|---|
| 178 |     {TMatrix<T> result; result.SetTemp(true); 
 | 
|---|
| 179 |     a.MulCst(b,result); return result;}
 | 
|---|
| 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)
 | 
|---|
| 184 |     {TMatrix<T> result; result.SetTemp(true); 
 | 
|---|
| 185 |     a.MulCst(b,result); return result;}
 | 
|---|
| 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)
 | 
|---|
| 190 |     {TMatrix<T> result; result.SetTemp(true); 
 | 
|---|
| 191 |     a.DivCst(b,result); return result;}
 | 
|---|
| 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)
 | 
|---|
| 196 |     {TMatrix<T> result; result.SetTemp(true); 
 | 
|---|
| 197 |     a.Div(b,result,true); return result;}
 | 
|---|
| 198 | 
 | 
|---|
| 199 | ////////////////////////////////////////////////////////////////
 | 
|---|
| 200 | // Surcharge d'operateurs B = -A
 | 
|---|
| 201 | 
 | 
|---|
| 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)
 | 
|---|
| 206 |     {TMatrix<T> result; result.SetTemp(true); 
 | 
|---|
| 207 |     a.NegateElt(result); return result;}
 | 
|---|
| 208 | 
 | 
|---|
| 209 | 
 | 
|---|
| 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 | 
 | 
|---|
| 215 | /*! \ingroup TArray \fn operator+(const TMatrix<T>&,const TMatrix<T>&)
 | 
|---|
| 216 |   \brief + : add matrixes \b a and \b b */
 | 
|---|
| 217 | template <class T>
 | 
|---|
| 218 | inline TMatrix<T> operator + (const TMatrix<T>& a,const TMatrix<T>& b)
 | 
|---|
| 219 |     {TMatrix<T> result; result.SetTemp(true); 
 | 
|---|
| 220 |     a.AddElt(b, result);     return result; }
 | 
|---|
| 221 | 
 | 
|---|
| 222 | 
 | 
|---|
| 223 | /*! \ingroup TArray \fn operator-(const TMatrix<T>&,const TMatrix<T>&)
 | 
|---|
| 224 |   \brief \- : substract matrixes \b a and \b b */
 | 
|---|
| 225 | template <class T>
 | 
|---|
| 226 | inline TMatrix<T> operator - (const TMatrix<T>& a,const TMatrix<T>& b)
 | 
|---|
| 227 |     {TMatrix<T> result; result.SetTemp(true); 
 | 
|---|
| 228 |     a.SubElt(b, result);     return result; }
 | 
|---|
| 229 |     
 | 
|---|
| 230 | 
 | 
|---|
| 231 | // Surcharge d'operateurs C = A * B
 | 
|---|
| 232 | /*! \ingroup TArray \fn operator*(const TMatrix<T>&,const TMatrix<T>&)
 | 
|---|
| 233 |   \brief * : multiply matrixes \b a and \b b */
 | 
|---|
| 234 | template <class T> inline TMatrix<T> operator * (const TMatrix<T>& a, const TMatrix<T>& b)
 | 
|---|
| 235 |    { return(a.Multiply(b)); }
 | 
|---|
| 236 | 
 | 
|---|
| 237 | // Typedef pour simplifier et compatibilite Peida
 | 
|---|
| 238 | /*! \ingroup TArray
 | 
|---|
| 239 |   \typedef Matrix
 | 
|---|
| 240 |   \brief To simplified TMatrix<r_8> writing
 | 
|---|
| 241 | */
 | 
|---|
| 242 | typedef TMatrix<r_8> Matrix;
 | 
|---|
| 243 | 
 | 
|---|
| 244 | } // Fin du namespace
 | 
|---|
| 245 | 
 | 
|---|
| 246 | #endif
 | 
|---|