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