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