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