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