source: Sophya/trunk/SophyaLib/TArray/symmtx.h@ 3859

Last change on this file since 3859 was 3809, checked in by ansari, 15 years ago

1/ Ajout fichiers / classes de matrices carrees speciales (DiagonalMatrix<T>,

SymmetricMatrix<T> LowerTriangularMatrix<T>

2/ Suppression fichier triangmtx.h et la classe TriangularMatrix<T>
3/ adaptation array.h et enregistrement handlers PPF

Reza, 26/07/2010

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