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

Last change on this file since 3572 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
RevLine 
[762]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
[926]9// doit etre mis en dehors du namespace
[920]10/*!
11 \class SOPHYA::TriangularMatrix
12 \ingroup TArray
[2957]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
[920]24*/
[926]25
26namespace SOPHYA {
[1683]27
[926]28//! Class for inferior triangular matrix (base class for the class Alm)
[762]29template <class T>
[914]30class TriangularMatrix {
31public :
[762]32
[914]33//! Default constructor
[3572]34TriangularMatrix()
35 : long_diag_(0)
36{
37}
38
[914]39//! instanciate a triangular matrix from the number of rows
[3572]40TriangularMatrix(sa_size_t rowSize)
41 : long_diag_(rowSize)
42{
43 elem_.ReSize((rowSize*(rowSize+1)/2) );
44}
45
[914]46//! Copy constructor (possibility of sharing datas)
[3572]47TriangularMatrix(const TriangularMatrix<T>& a, bool share=false)
48 : long_diag_(a.long_diag_) , elem_(a.elem_, share)
49{
50}
[914]51
52//! resize the matrix with a new number of rows
[2957]53inline void ReSizeRow(sa_size_t rowSize)
[3572]54{
55 long_diag_=(uint_4)rowSize;
56 elem_.ReSize(long_diag_*(long_diag_+1)/2);
57}
[762]58
[1683]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 }
[914]66
67//! () operator : access to elements row \b l and column \b m
[2957]68inline T& operator()(sa_size_t l, sa_size_t m)
[762]69 {
[1683]70 return elem_(indexOfElement(l,m));
[762]71 }
[1683]72
[2957]73inline T& operator()(sa_size_t index)
[1683]74 {
75 return elem_(index);
76 }
77
78
[914]79//! () operator : access to elements row \b l and column \b m
[2957]80inline T const& operator()(sa_size_t l, sa_size_t m) const
[762]81 {
[1683]82 return *(elem_.Begin()+ indexOfElement(l,m));
[762]83 }
[914]84
[2957]85inline T const& operator()(sa_size_t index) const
[1683]86 {
87 return *(elem_.Begin()+ index);
88 }
89
[1757]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 }
[1683]101
[1757]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_;
[2957]112 sa_size_t k;
[1757]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
[914]124//! Return number of rows
[2957]125inline sa_size_t rowNumber() const {return (int_4)long_diag_;}
[914]126
[1757]127//! Return size of the total array
[2957]128 inline sa_size_t Size() const {return elem_.Size();}
[1757]129
[2957]130 inline bool CheckRelativeIndices(sa_size_t l, sa_size_t m) const
[2291]131 {
132 if ( l < m )
133 {
134 throw RangeCheckError("TriangularMatrix<T>::CheckRelativeIndices: indices out of range " );
135 }
136 return true;
137 }
[2957]138 inline bool CheckAbsoluteIndice(sa_size_t l, sa_size_t m) const
[2291]139 {
140 if ( indexOfElement(l,m) >= elem_.Size() )
141 {
142 throw RangeCheckError("TriangularMatrix<T>::CheckAbsoluteIndice: indices out of range " );
143 }
144 }
[2957]145 inline bool CheckAbsoluteIndice(sa_size_t ind) const
[2291]146 {
147 if ( ind >= elem_.Size() )
148 {
149 throw RangeCheckError("TriangularMatrix<T>::CheckAbsoluteIndice: indices out of range " );
150 }
151 }
[1757]152
[2957]153 //! ASCII dump of the matrix (set nbLignes=-1) for dumping the complete matrix
154void Print(ostream& os, sa_size_t nbLignes=0) const
[1683]155 {
[2957]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;
[1683]169 }
170
[2957]171inline void Print(sa_size_t nbLignes=0) const { Print(cout, nbLignes); }
[1683]172
[2957]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
[914]180//! compute the address of an element in the single array representing the matrix
[2957]181inline sa_size_t indexOfElement(sa_size_t i,sa_size_t j) const
[762]182{
[1683]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);
[762]186}
187
[1683]188private:
189
[2957]190sa_size_t long_diag_; //!< size of the square matrix
[914]191NDataBlock<T> elem_; //!< Data block
[762]192
193 };
[2957]194
195template <class T>
196inline ostream& operator << (ostream& os, const TriangularMatrix<T>& a)
197 { a.Print(os, 0); return(os); }
[1683]198
[762]199} // namespace SOPHYA
200
201#endif
Note: See TracBrowser for help on using the repository browser.