[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 DIAGMTX_H_SEEN
|
---|
| 7 | #define DIAGMTX_H_SEEN
|
---|
| 8 |
|
---|
| 9 | #include "spesqmtx.h"
|
---|
| 10 |
|
---|
[3870] | 11 | namespace SOPHYA {
|
---|
| 12 |
|
---|
[3809] | 13 | /*!
|
---|
[3870] | 14 | \class DiagonalMatrix
|
---|
[3809] | 15 | \ingroup TArray
|
---|
| 16 | \brief Class representing a diagonal matrix.
|
---|
| 17 |
|
---|
| 18 | This class offers similar functionalities to the TArray<T> / TMatrix<T> classes, like
|
---|
| 19 | reference sharing and counting, arithmetic operators ... However, this class has no
|
---|
| 20 | sub matrix extraction method.
|
---|
| 21 | */
|
---|
| 22 |
|
---|
| 23 | template <class T>
|
---|
| 24 | class DiagonalMatrix : public SpecialSquareMatrix<T> {
|
---|
| 25 | public :
|
---|
| 26 |
|
---|
| 27 | #include "spesqmtx_tsnl.h"
|
---|
| 28 |
|
---|
| 29 | //! Default constructor - TriangMatrix of size 0, SetSize() should be called before the object is used
|
---|
| 30 | explicit DiagonalMatrix()
|
---|
| 31 | : SpecialSquareMatrix<T>(C_DiagonalMatrix)
|
---|
| 32 | {
|
---|
| 33 | mOffDiag = T(0);
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | //! Instanciate a triangular matrix from the number of rows (rowSize must be > 0)
|
---|
| 37 | explicit DiagonalMatrix(sa_size_t rowSize)
|
---|
| 38 | : SpecialSquareMatrix<T>(rowSize, C_DiagonalMatrix)
|
---|
| 39 | {
|
---|
| 40 | if (rowSize < 1)
|
---|
| 41 | throw ParmError("DiagonalMatrix<T>::DiagonalMatrix(rsz) rsz <= 0");
|
---|
| 42 | mElems.ReSize(rowSize);
|
---|
| 43 | mInfo = NULL;
|
---|
| 44 | mOffDiag = T(0);
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | //! Copy constructor (possibility of sharing datas)
|
---|
| 48 | DiagonalMatrix(DiagonalMatrix<T> const & a, bool share=false)
|
---|
| 49 | : SpecialSquareMatrix<T>(a, share)
|
---|
| 50 | {
|
---|
| 51 | mOffDiag = T(0);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | //! Copy constructor (possibility of sharing datas)
|
---|
| 55 | DiagonalMatrix(SpecialSquareMatrix<T> const & a, bool share=false)
|
---|
| 56 | : SpecialSquareMatrix<T>(a, share)
|
---|
| 57 | {
|
---|
| 58 | if (a.MtxType() != C_DiagonalMatrix)
|
---|
| 59 | throw TypeMismatchExc("DiagonalMatrix(a) a NOT a DiagonalMatrix");
|
---|
| 60 | mOffDiag = T(0);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | /*!
|
---|
| 64 | \brief Create a lower triangular matrix from a square matrix.
|
---|
| 65 | Off diagonal elements are ignored.
|
---|
| 66 | */
|
---|
| 67 | explicit DiagonalMatrix(TMatrix<T> const & mx)
|
---|
| 68 | : SpecialSquareMatrix<T>(C_DiagonalMatrix)
|
---|
| 69 | {
|
---|
| 70 | if ((mx.NRows() != mx.NCols()) || (mx.NRows() < 1))
|
---|
| 71 | throw ParmError("DiagonalMatrix<T>::(TMatrix<T> const & mx) mx not allocated OR NOT a square matrix");
|
---|
| 72 | SetSize(mx.NRows());
|
---|
| 73 | for(sa_size_t l=0; l<NRows(); l++) (*this)(l,l) = mx(l,l);
|
---|
| 74 | mOffDiag = T(0);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | //! Sets or change the triangular matrix size, specifying the new number of rows
|
---|
| 78 | virtual void SetSize(sa_size_t rowSize)
|
---|
| 79 | {
|
---|
| 80 | if (rowSize < 1)
|
---|
| 81 | throw ParmError("DiagonalMatrix<T>::SetSize(rsz) rsz <= 0");
|
---|
| 82 | if (rowSize == mNrows) return;
|
---|
| 83 | mNrows=rowSize;
|
---|
| 84 | mElems.ReSize(mNrows);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 |
|
---|
| 88 | //! Return the object (diagonal matrix) as a standard (TMatrix<T>) square matrix
|
---|
| 89 | virtual TMatrix<T> ConvertToStdMatrix() const
|
---|
| 90 | {
|
---|
| 91 | if (mNrows < 1)
|
---|
| 92 | throw SzMismatchError("DiagonalMatrix<T>::ConvertToStdMatrix() (this) not allocated !");
|
---|
| 93 | TMatrix<T> mx(NRows(), NRows());
|
---|
| 94 | for(sa_size_t l=0; l<NRows(); l++) mx(l,l) = (*this)(l,l);
|
---|
| 95 | return mx;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 |
|
---|
| 99 | //--- Operateurs = (T b) , = (DiagonalMatrix<T> b)
|
---|
| 100 | //! operator = a , to set all elements to the value \b a
|
---|
| 101 | inline DiagonalMatrix<T>& operator = (T a)
|
---|
| 102 | { SetCst(a); return (*this); }
|
---|
| 103 | //! operator = DiagonalMatrix<T> a , element by element copy operator
|
---|
| 104 | inline DiagonalMatrix<T>& operator = (DiagonalMatrix<T> const & a)
|
---|
| 105 | { Set(a); return (*this); }
|
---|
| 106 | //! operator = Sequence seq
|
---|
| 107 | inline DiagonalMatrix<T>& operator = (Sequence const & seq)
|
---|
| 108 | { SetSeq(seq); return (*this); }
|
---|
| 109 | //! operator = Sequence seq
|
---|
| 110 | inline DiagonalMatrix<T>& operator = (IdentityMatrix & idmx)
|
---|
[3819] | 111 | { SetCst((T)(idmx.Diag())); return (*this); }
|
---|
[3809] | 112 |
|
---|
| 113 | //--- Operateurs d'acces aux elements
|
---|
| 114 | //! Element access operator (R/W): access to elements row \b r and column \b c
|
---|
| 115 | inline T& operator()(sa_size_t r, sa_size_t c)
|
---|
| 116 | {
|
---|
| 117 | if ((r<0)||(r>=mNrows))
|
---|
| 118 | throw RangeCheckError("DiagonalMatrix<T>::operator()(r,c) (r<0)||(r>=NRows())");
|
---|
| 119 | if (r!=c) { mOffDiag = T(0); return mOffDiag; }
|
---|
| 120 | return mElems(r);
|
---|
| 121 | }
|
---|
| 122 | //! Element access operator (RO): access to elements row \b r and column \b c
|
---|
| 123 | inline T operator()(sa_size_t r, sa_size_t c) const
|
---|
| 124 | {
|
---|
| 125 | if ((r<0)||(r>=mNrows))
|
---|
| 126 | throw RangeCheckError("DiagonalMatrix<T>::operator()(r,c) (r<0)||(r>=NRows())");
|
---|
| 127 | if (r!=c) { mOffDiag = T(0); return mOffDiag; }
|
---|
| 128 | return mElems(r);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | //! Diagonal Matrix product (multiplication) : ret_matrix = (*this) * dmx
|
---|
| 132 | DiagonalMatrix<T> Multiply(DiagonalMatrix<T> const & dmx) const
|
---|
| 133 | {
|
---|
| 134 | if (NRows() != dmx.NRows())
|
---|
| 135 | throw SzMismatchError("DiagonalMatrix<T>::Multiply(DiagonalMatrix<T> dmx): different sizes");
|
---|
| 136 | DiagonalMatrix<T> ret(NRows());
|
---|
| 137 | for(size_t k=0; k<mElems.Size(); k++)
|
---|
| 138 | ret.mElems(k) = mElems(k)*dmx.mElems(k);
|
---|
| 139 | ret.SetTemp(true);
|
---|
| 140 | return ret;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | //! Matrix product (multiplication) : ret_matrix = (*this) * mx
|
---|
| 144 | TMatrix<T> MultiplyDG(TMatrix<T> const & mx) const
|
---|
| 145 | {
|
---|
| 146 | if (NCols() != mx.NRows())
|
---|
| 147 | throw SzMismatchError("DiagonalMatrix<T>::MultiplyDG(TMatrix<T> mx): NCols()!=mx.NRows()");
|
---|
| 148 |
|
---|
| 149 | TMatrix<T> ret(mx, false);
|
---|
| 150 | for(sa_size_t r=0; r<NRows(); r++)
|
---|
| 151 | ret.Row(r) *= (*this)(r,r);
|
---|
| 152 | ret.SetTemp(true);
|
---|
| 153 | return ret;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | //! Matrix product (multiplication) : ret_matrix = mx * (*this)
|
---|
| 157 | TMatrix<T> MultiplyGD(TMatrix<T> const & mx) const
|
---|
| 158 | {
|
---|
| 159 | if (NRows() != mx.NCols())
|
---|
| 160 | throw SzMismatchError("DiagonalMatrix<T>::MultiplyGD(TMatrix<T> mx): NRows()!=mx.NCols()");
|
---|
| 161 |
|
---|
| 162 | TMatrix<T> ret(mx, false);
|
---|
| 163 | for(sa_size_t c=0; c<NRows(); c++)
|
---|
| 164 | ret.Column(c) *= (*this)(c,c);
|
---|
| 165 | ret.SetTemp(true);
|
---|
| 166 | return ret;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 |
|
---|
| 170 | //! ASCII dump/print of the triangular matrix object (nprt=-1 for printing all diagonal elements)
|
---|
| 171 | ostream& Print(ostream& os, sa_size_t nprt=-1) const
|
---|
| 172 | {
|
---|
| 173 | os << "DiagonalMatrix< " << typeid(T).name()
|
---|
| 174 | << " > NRow=" << mNrows << " NbElem<>0 : " << Size() << endl;
|
---|
| 175 | for(sa_size_t k=0; k<Size(); k+=8) {
|
---|
| 176 | if (k%32==0) os << "DiagonalElements: [ " << k << " ..." << k+31 <<" ] :" << endl;
|
---|
| 177 | sa_size_t jmx=k+8;
|
---|
| 178 | if (jmx>Size()) jmx = Size();
|
---|
| 179 | for(sa_size_t j=k; j<jmx; j++) os << mElems(j) << " , ";
|
---|
| 180 | os << endl;
|
---|
| 181 | if (k >= nprt) break;
|
---|
| 182 | }
|
---|
| 183 | return os;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | protected:
|
---|
| 187 | mutable T mOffDiag;
|
---|
| 188 | };
|
---|
| 189 |
|
---|
| 190 | //----- Surcharge d'operateurs C = A * B (multiplication matricielle)
|
---|
| 191 | /*! \ingroup TArray \fn operator*(const DiagonalMatrix<T>&,const DiagonalMatrix<T>&)
|
---|
| 192 | \brief * : DiagonalMatrix multiplication \b a and \b b */
|
---|
| 193 | template <class T>
|
---|
| 194 | inline DiagonalMatrix<T> operator * (const DiagonalMatrix<T>& a, const DiagonalMatrix<T>& b)
|
---|
| 195 | { return(a.Multiply(b)); }
|
---|
| 196 |
|
---|
| 197 | /*! \ingroup TArray \fn operator*(const DiagonalMatrix<T>&,const TMatrix<T>&)
|
---|
| 198 | \brief * : Matrix multiplication DiagonalMatrix (\b a ) * TMatrix<T> ( \b b ) */
|
---|
| 199 | template <class T>
|
---|
| 200 | inline TMatrix<T> operator * (const DiagonalMatrix<T>& a, const TMatrix<T>& b)
|
---|
| 201 | { return(a.MultiplyDG(b)); }
|
---|
| 202 |
|
---|
| 203 | /*! \ingroup TArray \fn operator*(const TMatrix<T>&,const DiagonalMatrix<T>&)
|
---|
| 204 | \brief * : Matrix multiplication TMatrix (\b a ) * DiagonalMatrix<T> ( \b b ) */
|
---|
| 205 | template <class T>
|
---|
| 206 | inline TMatrix<T> operator * (const TMatrix<T>& a, const DiagonalMatrix<T>& b)
|
---|
| 207 | { return(b.MultiplyGD(a)); }
|
---|
| 208 |
|
---|
| 209 |
|
---|
| 210 | } // namespace SOPHYA
|
---|
| 211 |
|
---|
| 212 | #endif
|
---|