| 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 |
|
|---|
| 26 | namespace SOPHYA {
|
|---|
| 27 |
|
|---|
| 28 | //! Class for inferior triangular matrix (base class for the class Alm)
|
|---|
| 29 | template <class T>
|
|---|
| 30 | class TriangularMatrix {
|
|---|
| 31 | public :
|
|---|
| 32 |
|
|---|
| 33 | //! Default constructor
|
|---|
| 34 | TriangularMatrix() {;};
|
|---|
| 35 | //! instanciate a triangular matrix from the number of rows
|
|---|
| 36 | TriangularMatrix(sa_size_t rowSize) : long_diag_(rowSize)
|
|---|
| 37 | {
|
|---|
| 38 | elem_.ReSize((rowSize*(rowSize+1)/2) );
|
|---|
| 39 | }
|
|---|
| 40 | //! Copy constructor (possibility of sharing datas)
|
|---|
| 41 | TriangularMatrix(const TriangularMatrix<T>& a, bool share=false) : elem_(a.elem_, share), long_diag_(a.long_diag_) {;}
|
|---|
| 42 |
|
|---|
| 43 | //! resize the matrix with a new number of rows
|
|---|
| 44 | inline void ReSizeRow(sa_size_t rowSize)
|
|---|
| 45 | {
|
|---|
| 46 | long_diag_=(uint_4)rowSize;
|
|---|
| 47 | elem_.ReSize(long_diag_*(long_diag_+1)/2);
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 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 | }
|
|---|
| 57 |
|
|---|
| 58 | //! () operator : access to elements row \b l and column \b m
|
|---|
| 59 | inline T& operator()(sa_size_t l, sa_size_t m)
|
|---|
| 60 | {
|
|---|
| 61 | return elem_(indexOfElement(l,m));
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | inline T& operator()(sa_size_t index)
|
|---|
| 65 | {
|
|---|
| 66 | return elem_(index);
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 | //! () operator : access to elements row \b l and column \b m
|
|---|
| 71 | inline T const& operator()(sa_size_t l, sa_size_t m) const
|
|---|
| 72 | {
|
|---|
| 73 | return *(elem_.Begin()+ indexOfElement(l,m));
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | inline T const& operator()(sa_size_t index) const
|
|---|
| 77 | {
|
|---|
| 78 | return *(elem_.Begin()+ index);
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 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 | }
|
|---|
| 92 |
|
|---|
| 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_;
|
|---|
| 103 | sa_size_t k;
|
|---|
| 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 |
|
|---|
| 115 | //! Return number of rows
|
|---|
| 116 | inline sa_size_t rowNumber() const {return (int_4)long_diag_;}
|
|---|
| 117 |
|
|---|
| 118 | //! Return size of the total array
|
|---|
| 119 | inline sa_size_t Size() const {return elem_.Size();}
|
|---|
| 120 |
|
|---|
| 121 | inline bool CheckRelativeIndices(sa_size_t l, sa_size_t m) const
|
|---|
| 122 | {
|
|---|
| 123 | if ( l < m )
|
|---|
| 124 | {
|
|---|
| 125 | throw RangeCheckError("TriangularMatrix<T>::CheckRelativeIndices: indices out of range " );
|
|---|
| 126 | }
|
|---|
| 127 | return true;
|
|---|
| 128 | }
|
|---|
| 129 | inline bool CheckAbsoluteIndice(sa_size_t l, sa_size_t m) const
|
|---|
| 130 | {
|
|---|
| 131 | if ( indexOfElement(l,m) >= elem_.Size() )
|
|---|
| 132 | {
|
|---|
| 133 | throw RangeCheckError("TriangularMatrix<T>::CheckAbsoluteIndice: indices out of range " );
|
|---|
| 134 | }
|
|---|
| 135 | }
|
|---|
| 136 | inline bool CheckAbsoluteIndice(sa_size_t ind) const
|
|---|
| 137 | {
|
|---|
| 138 | if ( ind >= elem_.Size() )
|
|---|
| 139 | {
|
|---|
| 140 | throw RangeCheckError("TriangularMatrix<T>::CheckAbsoluteIndice: indices out of range " );
|
|---|
| 141 | }
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 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
|
|---|
| 146 | {
|
|---|
| 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;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | inline void Print(sa_size_t nbLignes=0) const { Print(cout, nbLignes); }
|
|---|
| 163 |
|
|---|
| 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 |
|
|---|
| 171 | //! compute the address of an element in the single array representing the matrix
|
|---|
| 172 | inline sa_size_t indexOfElement(sa_size_t i,sa_size_t j) const
|
|---|
| 173 | {
|
|---|
| 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);
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | private:
|
|---|
| 180 |
|
|---|
| 181 | sa_size_t long_diag_; //!< size of the square matrix
|
|---|
| 182 | NDataBlock<T> elem_; //!< Data block
|
|---|
| 183 |
|
|---|
| 184 | };
|
|---|
| 185 |
|
|---|
| 186 | template <class T>
|
|---|
| 187 | inline ostream& operator << (ostream& os, const TriangularMatrix<T>& a)
|
|---|
| 188 | { a.Print(os, 0); return(os); }
|
|---|
| 189 |
|
|---|
| 190 | } // namespace SOPHYA
|
|---|
| 191 |
|
|---|
| 192 | #endif
|
|---|