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