source: Sophya/trunk/SophyaLib/TArray/triangmtx.h@ 3615

Last change on this file since 3615 was 3572, checked in by cmv, 17 years ago

char* -> const char* pour regler les problemes de deprecated string const... + comparaison unsigned signed + suppression EVOL_PLANCK rz+cmv 07/02/2009

File size: 5.5 KB
Line 
1// This may look like C code, but it is really -*- C++ -*-
2
3#ifndef TRIANGMTX_H_SEEN
4#define TRIANGMTX_H_SEEN
5
6#include "ndatablock.h"
7#include "pexceptions.h"
8
9// doit etre mis en dehors du namespace
10/*!
11 \class SOPHYA::TriangularMatrix
12 \ingroup TArray
13 \brief Class for inferior triangular matrix (base class for the class Alm)
14 The inferior triangular matrix is represented in memory as column packed,
15 as illustrated below for a 5x5 triangular matrix.
16 \verbatim
17 5x5 Inf.Triang.Matrix, Size= 15 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
26namespace SOPHYA {
27
28//! Class for inferior triangular matrix (base class for the class Alm)
29template <class T>
30class TriangularMatrix {
31public :
32
33//! Default constructor
34TriangularMatrix()
35 : long_diag_(0)
36{
37}
38
39//! instanciate a triangular matrix from the number of rows
40TriangularMatrix(sa_size_t rowSize)
41 : long_diag_(rowSize)
42{
43 elem_.ReSize((rowSize*(rowSize+1)/2) );
44}
45
46//! Copy constructor (possibility of sharing datas)
47TriangularMatrix(const TriangularMatrix<T>& a, bool share=false)
48 : long_diag_(a.long_diag_) , elem_(a.elem_, share)
49{
50}
51
52//! resize the matrix with a new number of rows
53inline void ReSizeRow(sa_size_t rowSize)
54{
55 long_diag_=(uint_4)rowSize;
56 elem_.ReSize(long_diag_*(long_diag_+1)/2);
57}
58
59TriangularMatrix<T>& SetT(T a)
60 {
61 if (long_diag_ < 1)
62 throw RangeCheckError("TriangularMatrix<T>::SetT(T ) - TriangularMatrix not dimensionned ! ");
63 elem_ = a;
64 return (*this);
65 }
66
67//! () operator : access to elements row \b l and column \b m
68inline T& operator()(sa_size_t l, sa_size_t m)
69 {
70 return elem_(indexOfElement(l,m));
71 }
72
73inline T& operator()(sa_size_t index)
74 {
75 return elem_(index);
76 }
77
78
79//! () operator : access to elements row \b l and column \b m
80inline T const& operator()(sa_size_t l, sa_size_t m) const
81 {
82 return *(elem_.Begin()+ indexOfElement(l,m));
83 }
84
85inline T const& operator()(sa_size_t index) const
86 {
87 return *(elem_.Begin()+ index);
88 }
89
90TriangularMatrix<T>& Set(const TriangularMatrix<T>& a)
91 {
92 if (this != &a)
93 {
94 if (a.Size() < 1)
95 throw RangeCheckError(" TriangularMatrix<T>::Set()- Array a not allocated ! ");
96 }
97 if (Size() < 1) CloneOrShare(a);
98 else CopyElt(a);
99 return(*this);
100 }
101
102inline TriangularMatrix<T>& operator = (const TriangularMatrix<T>& a)
103 {return Set(a);}
104
105TriangularMatrix<T>& CopyElt(const TriangularMatrix<T>& a)
106 {
107 if (Size() < 1)
108 throw RangeCheckError("TriangularMatrix<T>::CopyElt(const TriangularMatrix<T>& ) - Not Allocated Array ! ");
109 if (Size() != a.Size() )
110 throw(SzMismatchError("TriangularMatrix<T>::CopyElt(const TriangularMatrix<T>&) SizeMismatch")) ;
111 long_diag_ = a.long_diag_;
112 sa_size_t k;
113 for (k=0; k< Size(); k++) elem_(k) = a.elem_(k);
114 return(*this);
115 }
116
117void CloneOrShare(const TriangularMatrix<T>& a)
118 {
119 long_diag_ = a.long_diag_;
120 elem_.CloneOrShare(a.elem_);
121 }
122
123
124//! Return number of rows
125inline sa_size_t rowNumber() const {return (int_4)long_diag_;}
126
127//! Return size of the total array
128 inline sa_size_t Size() const {return elem_.Size();}
129
130 inline bool CheckRelativeIndices(sa_size_t l, sa_size_t m) const
131 {
132 if ( l < m )
133 {
134 throw RangeCheckError("TriangularMatrix<T>::CheckRelativeIndices: indices out of range " );
135 }
136 return true;
137 }
138 inline bool CheckAbsoluteIndice(sa_size_t l, sa_size_t m) const
139 {
140 if ( indexOfElement(l,m) >= elem_.Size() )
141 {
142 throw RangeCheckError("TriangularMatrix<T>::CheckAbsoluteIndice: indices out of range " );
143 }
144 }
145 inline bool CheckAbsoluteIndice(sa_size_t ind) const
146 {
147 if ( ind >= elem_.Size() )
148 {
149 throw RangeCheckError("TriangularMatrix<T>::CheckAbsoluteIndice: indices out of range " );
150 }
151 }
152
153 //! ASCII dump of the matrix (set nbLignes=-1) for dumping the complete matrix
154void Print(ostream& os, sa_size_t nbLignes=0) const
155 {
156 os << "TriangularMatrix< " << typeid(T).name()
157 << " > NRow=" << long_diag_ << " NbElem<>0 : " << Size() << endl;
158 if (nbLignes == 0) return;
159 if (nbLignes < 0 ) nbLignes = long_diag_;
160 if (nbLignes > long_diag_ ) nbLignes = long_diag_;
161 for (sa_size_t k=0; k < nbLignes; k++) {
162 os << "L[" << k << "]: " ;
163 for (sa_size_t kc = 0; kc <= k ; kc++)
164 os << " " << elem_(indexOfElement(k,kc));
165 os << endl;
166 }
167 if (nbLignes < long_diag_) os << " ... ... ... " << endl;
168 return;
169 }
170
171inline void Print(sa_size_t nbLignes=0) const { Print(cout, nbLignes); }
172
173
174//! Return the pointer to the first non zero element in column \b j = &(tmmtx(j,j))
175inline const T* columnData(sa_size_t j) const {return elem_.Begin()+(long_diag_*j-j*(j-1)/2) ;}
176
177//! Return the pointer to the first non zero element in column \b j = &(tmmtx(j,j))
178inline T* columnData(sa_size_t j) {return elem_.Begin()+(long_diag_*j-j*(j-1)/2) ;}
179
180//! compute the address of an element in the single array representing the matrix
181inline sa_size_t indexOfElement(sa_size_t i,sa_size_t j) const
182{
183 // return(i*(i+1)/2+j);
184 // the (inferior triangular )matrix is stored column by column
185 return(i+ long_diag_*j-j*(j+1)/2);
186}
187
188private:
189
190sa_size_t long_diag_; //!< size of the square matrix
191NDataBlock<T> elem_; //!< Data block
192
193 };
194
195template <class T>
196inline ostream& operator << (ostream& os, const TriangularMatrix<T>& a)
197 { a.Print(os, 0); return(os); }
198
199} // namespace SOPHYA
200
201#endif
Note: See TracBrowser for help on using the repository browser.