// This may look like C code, but it is really -*- C++ -*- // C.Magneville 04/99 #ifndef TMatrix_SEEN #define TMatrix_SEEN #include "machdefs.h" #include "tarray.h" namespace SOPHYA { //! Class of matrices template class TMatrix : public TArray { public: #include "tmatrix_tsnl.h" /* For two level name look-up gcc >= 3.4 */ // Creation / destruction TMatrix(); TMatrix(sa_size_t r,sa_size_t c, short mm=BaseArray::AutoMemoryMapping, bool fzero=true); TMatrix(const TMatrix& a); TMatrix(const TMatrix& a, bool share); TMatrix(const TArray& a, bool share=true); TMatrix(const BaseArray& a, bool pack=true); virtual ~TMatrix(); // Pour verifiez la compatibilite de dimensions lors de l'affectation virtual TArray& Set(const TArray& a); //! Operator = between matrices /*! \warning Datas are copied (cloned) from \b a. \sa NDataBlock::operator=(const NDataBlock&) */ inline TMatrix& operator = (const TMatrix& a) { Set(a); return(*this); } //! Operator = between a matrix and an array inline TMatrix& operator = (const TArray& a) { Set(a); return(*this); } virtual TArray& SetBA(const BaseArray& a); //! Operator = between matrices with different types inline TMatrix& operator = (const BaseArray& a) { SetBA(a); return(*this); } // Size - Changing the Size //! return number of rows inline sa_size_t NRows() const {return size_[marowi_]; } //! return number of columns inline sa_size_t NCols() const {return size_[macoli_]; } //! return number of columns inline sa_size_t NCol() const {return size_[macoli_]; } // back-compat Peida void ReSize(sa_size_t r,sa_size_t c, short mm=BaseArray::SameMemoryMapping, bool fzero=true); //! a synonym (alias) for method ReSize(sa_size_t, sa_size_t, short) inline void SetSize(sa_size_t r,sa_size_t c, short mm=BaseArray::SameMemoryMapping, bool fzero=true) { ReSize(r, c, mm, fzero); } // Reallocation de place void Realloc(sa_size_t r,sa_size_t c, short mm=BaseArray::SameMemoryMapping, bool force=false); // Sub-matrix extraction $CHECK$ Reza 03/2000 Doit-on declarer ces methode const ? TMatrix SubMatrix(Range rline, Range rcol) const ; //! () : Return submatrix define by \b Range \b rline and \b rcol inline TMatrix operator () (Range rline, Range rcol) const { return SubMatrix(rline, rcol); } // Lignes et colonnes de la matrice //! Return submatrix define by line \b ir (line vector) inline TMatrix Row(sa_size_t ir) const { return SubMatrix(Range(ir,ir), Range(0,NCols()-1)); } //! Return submatrix define by column \b ic (column vector) inline TMatrix Column(sa_size_t ic) const { return SubMatrix(Range(0,NRows()-1), Range(ic,ic)); } // Inline element acces methods inline T const& operator()(sa_size_t r,sa_size_t c) const; inline T& operator()(sa_size_t r,sa_size_t c); // Operations matricielles TMatrix& TransposeSelf(); TMatrix Transpose() const; //mm = SameMemoryMapping or CMemoryMapping or FortranMemoryMapping TMatrix Transpose(short mm) const ; // Rearranging Matrix Elements TMatrix Rearrange(short mm) const; // Operateur d'affectation // A = x (matrice diagonale Identite) virtual TMatrix& SetIdentity(IdentityMatrix imx); // = : fill matrix with an identity matrix \b imx inline TMatrix& operator = (IdentityMatrix imx) { return SetIdentity(imx); } // = : fill matrix with a Sequence \b seq inline TMatrix& operator = (Sequence const & seq) { SetSeq(seq); return(*this); } // Operations diverses avec une constante //! = : fill matrix with constant value \b x inline TMatrix& operator = (T x) { SetT(x); return(*this); } //! += : add constant value \b x to matrix inline TMatrix& operator += (T x) { AddCst(x,*this); return(*this); } //! -= : substract constant value \b x to matrix inline TMatrix& operator -= (T x) { SubCst(x,*this); return(*this); } //! *= : multiply matrix by constant value \b x inline TMatrix& operator *= (T x) { MulCst(x,*this); return(*this); } //! /= : divide matrix by constant value \b x inline TMatrix& operator /= (T x) { DivCst(x,*this); return(*this); } // operations avec matrices //! += : add a matrix inline TMatrix& operator += (const TMatrix& a) { AddElt(a,*this); return(*this); } //! -= : substract a matrix inline TMatrix& operator -= (const TMatrix& a) { SubElt(a,*this); return(*this); } TMatrix Multiply(const TMatrix& b, short mm=BaseArray::SameMemoryMapping) const; //A supprimer ? Reza Juillet 2004 ! *= : matrix product : C = (*this)*B // inline TMatrix& operator *= (const TMatrix& b) // { this->Set(Multiply(b)); return(*this); } // I/O print, ... virtual string InfoString() const; virtual void Print(ostream& os, sa_size_t maxprt=-1, bool si=false, bool ascd=false) const ; protected: }; // ---- inline acces methods ------ //! () : return element for line \b r and column \b c template inline T const& TMatrix::operator()(sa_size_t r, sa_size_t c) const { #ifdef SO_BOUNDCHECKING if (marowi_ == 0) CheckBound(r, c, 0, 0, 0, 4); else CheckBound(c, r, 0, 0, 0, 4); #endif return ( *( mNDBlock.Begin()+ offset_+ r*step_[marowi_] + c*step_[macoli_] ) ); } //! () : return element for line \b r and column \b c template inline T & TMatrix::operator()(sa_size_t r, sa_size_t c) { #ifdef SO_BOUNDCHECKING if (marowi_ == 0) CheckBound(r, c, 0, 0, 0, 4); else CheckBound(c, r, 0, 0, 0, 4); #endif return ( *( mNDBlock.Begin()+ offset_+ r*step_[marowi_] + c*step_[macoli_] ) ); } //////////////////////////////////////////////////////////////// // Surcharge d'operateurs A (+,-,*,/) (T) x /*! \ingroup TMatrix \fn operator+(const TMatrix&,T) \brief Operator TMatrix = TMatrix + constant */ template inline TMatrix operator + (const TMatrix& a, T b) {TMatrix result; result.SetTemp(true); a.AddCst(b,result); return result;} /*! \ingroup TMatrix \fn operator+(T,const TMatrix&) \brief Operator TMatrix = constant + TMatrix */ template inline TMatrix operator + (T b,const TMatrix& a) {TMatrix result; result.SetTemp(true); a.AddCst(b,result); return result;} /*! \ingroup TMatrix \fn operator-(const TMatrix&,T) \brief Operator TMatrix = TMatrix - constant */ template inline TMatrix operator - (const TMatrix& a, T b) {TMatrix result; result.SetTemp(true); a.SubCst(b,result); return result;} /*! \ingroup TMatrix \fn operator-(T,const TMatrix&) \brief Operator TMatrix = constant - TMatrix */ template inline TMatrix operator - (T b,const TMatrix& a) {TMatrix result; result.SetTemp(true); a.SubCst(b,result,true); return result;} /*! \ingroup TMatrix \fn operator*(const TMatrix&,T) \brief Operator TMatrix = TMatrix * constant */ template inline TMatrix operator * (const TMatrix& a, T b) {TMatrix result; result.SetTemp(true); a.MulCst(b,result); return result;} /*! \ingroup TMatrix \fn operator*(T,const TMatrix&) \brief Operator TMatrix = constant * TMatrix */ template inline TMatrix operator * (T b,const TMatrix& a) {TMatrix result; result.SetTemp(true); a.MulCst(b,result); return result;} /*! \ingroup TMatrix \fn operator/(const TMatrix&,T) \brief Operator TMatrix = TMatrix / constant */ template inline TMatrix operator / (const TMatrix& a, T b) {TMatrix result; result.SetTemp(true); a.DivCst(b,result); return result;} /*! \ingroup TMatrix \fn operator/(T,const TMatrix&) \brief Operator TMatrix = constant / TMatrix */ template inline TMatrix operator / (T b, const TMatrix& a) {TMatrix result; result.SetTemp(true); a.Div(b,result,true); return result;} //////////////////////////////////////////////////////////////// // Surcharge d'operateurs B = -A /*! \ingroup TMatrix \fn operator - (const TMatrix&) \brief Operator - Returns a matrix with elements equal to the opposite of the original matrix elements. */ template inline TMatrix operator - (const TMatrix& a) {TMatrix result; result.SetTemp(true); a.NegateElt(result); return result;} // Surcharge d'operateurs C = A (+,-) B // $CHECK$ Reza 3/4/2000 Pas necessaire de redefinir les operateurs // Defini au niveau de TArray - Pour ameliorer l'efficacite // Doit-on le faire aussi pour les constantes ? - Fin de $CHECK$ Reza 3/4/2000 /*! \ingroup TArray \fn operator+(const TMatrix&,const TMatrix&) \brief + : add matrixes \b a and \b b */ template inline TMatrix operator + (const TMatrix& a,const TMatrix& b) {TMatrix result; result.SetTemp(true); a.AddElt(b, result); return result; } /*! \ingroup TArray \fn operator-(const TMatrix&,const TMatrix&) \brief \- : substract matrixes \b a and \b b */ template inline TMatrix operator - (const TMatrix& a,const TMatrix& b) {TMatrix result; result.SetTemp(true); a.SubElt(b, result); return result; } // Surcharge d'operateurs C = A * B /*! \ingroup TArray \fn operator*(const TMatrix&,const TMatrix&) \brief * : multiply matrixes \b a and \b b */ template inline TMatrix operator * (const TMatrix& a, const TMatrix& b) { return(a.Multiply(b)); } // Typedef pour simplifier et compatibilite Peida /*! \ingroup TArray \typedef Matrix \brief To simplified TMatrix writing */ typedef TMatrix Matrix; //--------- extern template declarations (if needed) ----------- #if defined ( NEED_EXT_DECL_TEMP ) && !defined( TMATRIX_CC_BFILE ) extern template class TMatrix; extern template class TMatrix; extern template class TMatrix; extern template class TMatrix; extern template class TMatrix; extern template class TMatrix; extern template class TMatrix; extern template class TMatrix; extern template class TMatrix; extern template class TMatrix; extern template class TMatrix< complex >; extern template class TMatrix< complex >; #ifdef SO_LDBLE128 extern template class TMatrix; extern template class TMatrix< complex >; #endif #endif // Fin de if defined ( NEED_EXT_DECL_TEMP ) } // Fin du namespace #endif