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