// This may look like C code, but it is really -*- C++ -*- #ifndef DIAGMTX_H_SEEN #define DIAGMTX_H_SEEN #include "spesqmtx.h" // doit etre mis en dehors du namespace /*! \class SOPHYA::DiagonalMatrix \ingroup TArray \brief Class representing a diagonal matrix. This class offers similar functionalities to the TArray / TMatrix classes, like reference sharing and counting, arithmetic operators ... However, this class has no sub matrix extraction method. */ namespace SOPHYA { //! Class for inferior triangular matrix (base class for the class Alm) template class DiagonalMatrix : public SpecialSquareMatrix { public : #include "spesqmtx_tsnl.h" //! Default constructor - TriangMatrix of size 0, SetSize() should be called before the object is used explicit DiagonalMatrix() : SpecialSquareMatrix(C_DiagonalMatrix) { mOffDiag = T(0); } //! Instanciate a triangular matrix from the number of rows (rowSize must be > 0) explicit DiagonalMatrix(sa_size_t rowSize) : SpecialSquareMatrix(rowSize, C_DiagonalMatrix) { if (rowSize < 1) throw ParmError("DiagonalMatrix::DiagonalMatrix(rsz) rsz <= 0"); mElems.ReSize(rowSize); mInfo = NULL; mOffDiag = T(0); } //! Copy constructor (possibility of sharing datas) DiagonalMatrix(DiagonalMatrix const & a, bool share=false) : SpecialSquareMatrix(a, share) { mOffDiag = T(0); } //! Copy constructor (possibility of sharing datas) DiagonalMatrix(SpecialSquareMatrix const & a, bool share=false) : SpecialSquareMatrix(a, share) { if (a.MtxType() != C_DiagonalMatrix) throw TypeMismatchExc("DiagonalMatrix(a) a NOT a DiagonalMatrix"); mOffDiag = T(0); } /*! \brief Create a lower triangular matrix from a square matrix. Off diagonal elements are ignored. */ explicit DiagonalMatrix(TMatrix const & mx) : SpecialSquareMatrix(C_DiagonalMatrix) { if ((mx.NRows() != mx.NCols()) || (mx.NRows() < 1)) throw ParmError("DiagonalMatrix::(TMatrix const & mx) mx not allocated OR NOT a square matrix"); SetSize(mx.NRows()); for(sa_size_t l=0; l::SetSize(rsz) rsz <= 0"); if (rowSize == mNrows) return; mNrows=rowSize; mElems.ReSize(mNrows); } //! Return the object (diagonal matrix) as a standard (TMatrix) square matrix virtual TMatrix ConvertToStdMatrix() const { if (mNrows < 1) throw SzMismatchError("DiagonalMatrix::ConvertToStdMatrix() (this) not allocated !"); TMatrix mx(NRows(), NRows()); for(sa_size_t l=0; l b) //! operator = a , to set all elements to the value \b a inline DiagonalMatrix& operator = (T a) { SetCst(a); return (*this); } //! operator = DiagonalMatrix a , element by element copy operator inline DiagonalMatrix& operator = (DiagonalMatrix const & a) { Set(a); return (*this); } //! operator = Sequence seq inline DiagonalMatrix& operator = (Sequence const & seq) { SetSeq(seq); return (*this); } //! operator = Sequence seq inline DiagonalMatrix& operator = (IdentityMatrix & idmx) { SetCst((T)(idmx.Diag())); return (*this); } //--- Operateurs d'acces aux elements //! Element access operator (R/W): access to elements row \b r and column \b c inline T& operator()(sa_size_t r, sa_size_t c) { if ((r<0)||(r>=mNrows)) throw RangeCheckError("DiagonalMatrix::operator()(r,c) (r<0)||(r>=NRows())"); if (r!=c) { mOffDiag = T(0); return mOffDiag; } return mElems(r); } //! Element access operator (RO): access to elements row \b r and column \b c inline T operator()(sa_size_t r, sa_size_t c) const { if ((r<0)||(r>=mNrows)) throw RangeCheckError("DiagonalMatrix::operator()(r,c) (r<0)||(r>=NRows())"); if (r!=c) { mOffDiag = T(0); return mOffDiag; } return mElems(r); } //! Diagonal Matrix product (multiplication) : ret_matrix = (*this) * dmx DiagonalMatrix Multiply(DiagonalMatrix const & dmx) const { if (NRows() != dmx.NRows()) throw SzMismatchError("DiagonalMatrix::Multiply(DiagonalMatrix dmx): different sizes"); DiagonalMatrix ret(NRows()); for(size_t k=0; k MultiplyDG(TMatrix const & mx) const { if (NCols() != mx.NRows()) throw SzMismatchError("DiagonalMatrix::MultiplyDG(TMatrix mx): NCols()!=mx.NRows()"); TMatrix ret(mx, false); for(sa_size_t r=0; r MultiplyGD(TMatrix const & mx) const { if (NRows() != mx.NCols()) throw SzMismatchError("DiagonalMatrix::MultiplyGD(TMatrix mx): NRows()!=mx.NCols()"); TMatrix ret(mx, false); for(sa_size_t c=0; c NRow=" << mNrows << " NbElem<>0 : " << Size() << endl; for(sa_size_t k=0; kSize()) jmx = Size(); for(sa_size_t j=k; j= nprt) break; } return os; } protected: mutable T mOffDiag; }; //----- Surcharge d'operateurs C = A * B (multiplication matricielle) /*! \ingroup TArray \fn operator*(const DiagonalMatrix&,const DiagonalMatrix&) \brief * : DiagonalMatrix multiplication \b a and \b b */ template inline DiagonalMatrix operator * (const DiagonalMatrix& a, const DiagonalMatrix& b) { return(a.Multiply(b)); } /*! \ingroup TArray \fn operator*(const DiagonalMatrix&,const TMatrix&) \brief * : Matrix multiplication DiagonalMatrix (\b a ) * TMatrix ( \b b ) */ template inline TMatrix operator * (const DiagonalMatrix& a, const TMatrix& b) { return(a.MultiplyDG(b)); } /*! \ingroup TArray \fn operator*(const TMatrix&,const DiagonalMatrix&) \brief * : Matrix multiplication TMatrix (\b a ) * DiagonalMatrix ( \b b ) */ template inline TMatrix operator * (const TMatrix& a, const DiagonalMatrix& b) { return(b.MultiplyGD(a)); } } // namespace SOPHYA #endif