[3809] | 1 | // This may look like C code, but it is really -*- C++ -*-
|
---|
[3870] | 2 | // This code is part of the SOPHYA library
|
---|
| 3 | // (C) Univ. Paris-Sud (C) LAL-IN2P3/CNRS (C) IRFU-CEA
|
---|
| 4 | // (C) R. Ansari, C.Magneville 2009-2010
|
---|
[3809] | 5 |
|
---|
| 6 | #ifndef SYMMTX_H_SEEN
|
---|
| 7 | #define SYMMTX_H_SEEN
|
---|
| 8 |
|
---|
| 9 | #include "spesqmtx.h"
|
---|
| 10 |
|
---|
[3870] | 11 | namespace SOPHYA {
|
---|
| 12 |
|
---|
[3809] | 13 | /*!
|
---|
[3870] | 14 | \class SymmetricMatrix
|
---|
[3809] | 15 | \ingroup TArray
|
---|
| 16 | \brief Class representing a symmetric matrix.
|
---|
| 17 |
|
---|
| 18 | The symmetric matrix is represented in memory as column packed,
|
---|
| 19 | corresponding to the lower triangular part, as illustrated below for a 5x5 matrix.
|
---|
| 20 | \verbatim
|
---|
| 21 | 5x5 symmetric.Matrix, Size= 5*(5+1)/2 = 15 independent elements (0 ... 14)
|
---|
| 22 | | 0 |
|
---|
| 23 | | 1 5 |
|
---|
| 24 | | 2 6 9 |
|
---|
| 25 | | 3 7 10 12 |
|
---|
| 26 | | 4 8 11 13 14 |
|
---|
| 27 | \endverbatim
|
---|
| 28 |
|
---|
| 29 | This class offers similar functionalities to the TArray<T> / TMatrix<T> classes, like
|
---|
| 30 | reference sharing and counting, arithmetic operators ... However, this class has no
|
---|
| 31 | sub matrix extraction method.
|
---|
| 32 | */
|
---|
| 33 |
|
---|
| 34 | template <class T>
|
---|
| 35 | class SymmetricMatrix : public SpecialSquareMatrix<T> {
|
---|
| 36 | public :
|
---|
| 37 |
|
---|
| 38 | #include "spesqmtx_tsnl.h"
|
---|
| 39 |
|
---|
| 40 | //! Default constructor - TriangMatrix of size 0, SetSize() should be called before the object is used
|
---|
| 41 | explicit SymmetricMatrix()
|
---|
| 42 | : SpecialSquareMatrix<T>(C_SymmetricMatrix)
|
---|
| 43 | {
|
---|
| 44 |
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | //! Instanciate a triangular matrix from the number of rows (rowSize must be > 0)
|
---|
| 48 | explicit SymmetricMatrix(sa_size_t rowSize)
|
---|
| 49 | : SpecialSquareMatrix<T>(rowSize, C_SymmetricMatrix)
|
---|
| 50 | {
|
---|
| 51 | if (rowSize < 1)
|
---|
| 52 | throw ParmError("SymmetricMatrix<T>::SymmetricMatrix(rsz) rsz <= 0");
|
---|
| 53 | mElems.ReSize((rowSize*(rowSize+1)/2) );
|
---|
| 54 | mInfo = NULL;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | //! Copy constructor (possibility of sharing datas)
|
---|
| 58 | SymmetricMatrix(SymmetricMatrix<T> const & a, bool share=false)
|
---|
| 59 | : SpecialSquareMatrix<T>(a, share)
|
---|
| 60 | {
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | //! Copy constructor (possibility of sharing datas)
|
---|
| 64 | SymmetricMatrix(SpecialSquareMatrix<T> const & a, bool share=false)
|
---|
| 65 | : SpecialSquareMatrix<T>(a, share)
|
---|
| 66 | {
|
---|
| 67 | if (a.MtxType() != C_SymmetricMatrix)
|
---|
| 68 | throw TypeMismatchExc("SymmetricMatrix(a) a NOT a SymmetricMatrix");
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | /*!
|
---|
| 72 | \brief Create a lower triangular matrix from a square matrix.
|
---|
| 73 | Elements above the diagonal are ignored.
|
---|
| 74 | */
|
---|
| 75 | explicit SymmetricMatrix(TMatrix<T> const & mx)
|
---|
| 76 | : SpecialSquareMatrix<T>(C_SymmetricMatrix)
|
---|
| 77 | {
|
---|
| 78 | if ((mx.NRows() != mx.NCols()) || (mx.NRows() < 1))
|
---|
| 79 | throw ParmError("SymmetricMatrix<T>::(TMatrix<T> const & mx) mx not allocated OR NOT a square matrix");
|
---|
| 80 | SetSize(mx.NRows());
|
---|
| 81 | for(sa_size_t l=0; l<NRows(); l++)
|
---|
| 82 | for(sa_size_t m=0; m<=l; m++) (*this)(l,m) = mx(l,m);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | //! Sets or change the triangular matrix size, specifying the new number of rows
|
---|
| 86 | virtual void SetSize(sa_size_t rowSize)
|
---|
| 87 | {
|
---|
| 88 | if (rowSize < 1)
|
---|
| 89 | throw ParmError("SymmetricMatrix<T>::SetSize(rsz) rsz <= 0");
|
---|
| 90 | if (rowSize == mNrows) return;
|
---|
| 91 | mNrows=rowSize;
|
---|
| 92 | mElems.ReSize(mNrows*(mNrows+1)/2);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | //! Return number of rows (for compatibility with the old TriangularMatrix interface)
|
---|
| 96 | inline sa_size_t rowNumber() const {return (int_4)mNrows;}
|
---|
| 97 |
|
---|
| 98 | //! Return the object (triangular matrix) as a standard square matrix
|
---|
| 99 | virtual TMatrix<T> ConvertToStdMatrix() const
|
---|
| 100 | {
|
---|
| 101 | if (mNrows < 1)
|
---|
| 102 | throw SzMismatchError("SymmetricMatrix<T>::ConvertToStdMatrix() (this) not allocated !");
|
---|
| 103 | SOPHYA::TMatrix<T> mx(NRows(), NRows());
|
---|
| 104 | for(sa_size_t l=0; l<NRows(); l++)
|
---|
| 105 | for(sa_size_t m=0; m<=l; m++) mx(l,m) = mx(m,l) = (*this)(l,m);
|
---|
| 106 | return mx;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | //--- Operateurs = (T b) , = (SymmetricMatrix<T> b)
|
---|
| 110 | //! operator = a , to set all elements to the value \b a
|
---|
| 111 | inline SymmetricMatrix<T>& operator = (T a)
|
---|
| 112 | { SetCst(a); return (*this); }
|
---|
| 113 | //! operator = SymmetricMatrix<T> a , element by element copy operator
|
---|
| 114 | inline SymmetricMatrix<T>& operator = (SymmetricMatrix<T> const & a)
|
---|
| 115 | { Set(a); return (*this); }
|
---|
| 116 | //! operator = Sequence seq
|
---|
| 117 | inline SymmetricMatrix<T>& operator = (Sequence const & seq)
|
---|
| 118 | { SetSeq(seq); return (*this); }
|
---|
| 119 |
|
---|
| 120 |
|
---|
| 121 | //--- Operateurs d'acces aux elements
|
---|
| 122 | //! Element access operator (R/W): access to elements row \b r and column \b c
|
---|
| 123 | inline T& operator()(sa_size_t r, sa_size_t c)
|
---|
| 124 | {
|
---|
| 125 | if ((r<0)||(r>=mNrows))
|
---|
| 126 | throw RangeCheckError("DiagonalMatrix<T>::operator()(r,c) (r<0)||(r>=NRows())");
|
---|
| 127 | if (c>r) { sa_size_t rc = r; r=c; c=rc; }
|
---|
| 128 | // the inferior triangular part of the matrix is stored column by column
|
---|
| 129 | return(mElems(r+ mNrows*c-c*(c+1)/2));
|
---|
| 130 | }
|
---|
| 131 | //! Element access operator (RO): access to elements row \b l and column \b m
|
---|
| 132 | inline T operator()(sa_size_t r, sa_size_t c) const
|
---|
| 133 | {
|
---|
| 134 | if ((r<0)||(r>=mNrows))
|
---|
| 135 | throw RangeCheckError("DiagonalMatrix<T>::operator()(r,c) (r<0)||(r>=NRows())");
|
---|
| 136 | if (c>r) { sa_size_t rc = r; r=c; c=rc; }
|
---|
| 137 | // the inferior triangular part of the matrix is stored column by column
|
---|
| 138 | return(mElems(r+ mNrows*c-c*(c+1)/2));
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | //! Return the pointer to the first non zero element in column \b j = &(tmmtx(j,j))
|
---|
| 142 | inline const T* columnData(sa_size_t j) const {return mElems.Begin()+(mNrows*j-j*(j-1)/2) ;}
|
---|
| 143 |
|
---|
| 144 | //! Return the pointer to the first non zero element in column \b j = &(tmmtx(j,j))
|
---|
| 145 | inline T* columnData(sa_size_t j) {return mElems.Begin()+(mNrows*j-j*(j-1)/2) ;}
|
---|
| 146 |
|
---|
| 147 | //! compute the position of the element \b tm(i,j) relative to the first element
|
---|
| 148 | inline sa_size_t indexOfElement(sa_size_t i,sa_size_t j) const
|
---|
| 149 | {
|
---|
| 150 | // return(i*(i+1)/2+j);
|
---|
| 151 | // the (inferior triangular )matrix is stored column by column
|
---|
| 152 | return(i+ mNrows*j-j*(j+1)/2);
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | //! Triangular Matrix product (multiplication) : ret_matrix = (*this) * tmx
|
---|
| 156 | TMatrix<T> Multiply(SymmetricMatrix<T> const & tmx) const
|
---|
| 157 | {
|
---|
| 158 | if (NRows() != tmx.NRows())
|
---|
| 159 | throw SzMismatchError("Matrix<T>::Multiply(SymmetricMatrix<T> tmx): different sizes");
|
---|
| 160 | // codage peu efficace : on utilise la multiplication de matrices generales ...
|
---|
| 161 | TMatrix<T> a = ConvertToStdMatrix();
|
---|
| 162 | TMatrix<T> b = tmx.ConvertToStdMatrix();
|
---|
| 163 | return (a.Multiply(b));
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | //! Matrix product (multiplication) : ret_matrix = (*this) * mx
|
---|
| 167 | TMatrix<T> MultiplySG(TMatrix<T> const & mx) const
|
---|
| 168 | {
|
---|
| 169 | if (NCols() != mx.NRows())
|
---|
| 170 | throw SzMismatchError("SymmetricMatrix<T>::MultiplySG(TMatrix<T> mx): NCols()!=mx.NRows()");
|
---|
| 171 | TMatrix<T> a = ConvertToStdMatrix();
|
---|
| 172 | return a.Multiply(mx);
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | //! Matrix product (multiplication) : ret_matrix = mx * (*this)
|
---|
| 176 | TMatrix<T> MultiplyGS(TMatrix<T> const & mx) const
|
---|
| 177 | {
|
---|
| 178 | if (NRows() != mx.NCols())
|
---|
| 179 | throw SzMismatchError("SymmetricMatrix<T>::MultiplyGS(TMatrix<T> mx): NRows()!=mx.NCols()");
|
---|
| 180 | TMatrix<T> a = ConvertToStdMatrix();
|
---|
| 181 | return mx.Multiply(a);
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | //! ASCII dump/print of the triangular matrix object (set nbLignes=-1 for dumping the complete matrix)
|
---|
| 185 | ostream& Print(ostream& os, sa_size_t nbLignes=0) const
|
---|
| 186 | {
|
---|
| 187 | os << "SymmetricMatrix< " << typeid(T).name()
|
---|
| 188 | << " > NRow=" << mNrows << " NbElem<>0 : " << Size() << endl;
|
---|
| 189 | if (nbLignes == 0) return os;
|
---|
| 190 | if (nbLignes < 0 ) nbLignes = mNrows;
|
---|
| 191 | if (nbLignes > mNrows ) nbLignes = mNrows;
|
---|
| 192 | for (sa_size_t r=0; r<nbLignes; r++) {
|
---|
| 193 | os << "Row[" << r << "]: " ;
|
---|
| 194 | for (sa_size_t c=0; c<NRows(); c++)
|
---|
| 195 | os << " " << (*this)(r,c);
|
---|
| 196 | os << endl;
|
---|
| 197 | }
|
---|
| 198 | if (nbLignes < mNrows) os << " ... ... ... " << endl;
|
---|
| 199 | return os;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | protected:
|
---|
| 203 | };
|
---|
| 204 |
|
---|
| 205 | //----- Surcharge d'operateurs C = A * B (multiplication matricielle)
|
---|
| 206 | /*! \ingroup TArray \fn operator*(const SymmetricMatrix<T>&,const SymmetricMatrix<T>&)
|
---|
| 207 | \brief * : SymmetricMatrix multiplication \b a and \b b */
|
---|
| 208 | template <class T>
|
---|
| 209 | inline TMatrix<T> operator * (const SymmetricMatrix<T>& a, const SymmetricMatrix<T>& b)
|
---|
| 210 | { return(a.Multiply(b)); }
|
---|
| 211 |
|
---|
| 212 | /*! \ingroup TArray \fn operator*(const SymmetricMatrix<T>&,const TMatrix<T>&)
|
---|
| 213 | \brief * : Matrix multiplication SymmetricMatrix (\b a ) * TMatrix<T> ( \b b ) */
|
---|
| 214 | template <class T>
|
---|
| 215 | inline TMatrix<T> operator * (const SymmetricMatrix<T>& a, const TMatrix<T>& b)
|
---|
| 216 | { return(a.MultiplySG(b)); }
|
---|
| 217 |
|
---|
| 218 | /*! \ingroup TArray \fn operator*(const TMatrix<T>&,const SymmetricMatrix<T>&)
|
---|
| 219 | \brief * : Matrix multiplication TMatrix (\b a ) * SymmetricMatrix<T> ( \b b ) */
|
---|
| 220 | template <class T>
|
---|
| 221 | inline TMatrix<T> operator * (const TMatrix<T>& a, const SymmetricMatrix<T>& b)
|
---|
| 222 | { return(b.MultiplyGS(a)); }
|
---|
| 223 |
|
---|
| 224 |
|
---|
| 225 | } // namespace SOPHYA
|
---|
| 226 |
|
---|
| 227 | #endif
|
---|