| 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 | Class for inferior triangular matrix (base class for the class Alm) | 
|---|
| 14 | */ | 
|---|
| 15 |  | 
|---|
| 16 | namespace SOPHYA { | 
|---|
| 17 |  | 
|---|
| 18 | //! Class for inferior triangular matrix (base class for the class Alm) | 
|---|
| 19 | template <class T> | 
|---|
| 20 | class TriangularMatrix { | 
|---|
| 21 | public : | 
|---|
| 22 |  | 
|---|
| 23 | //! Default constructor | 
|---|
| 24 | TriangularMatrix()   {;}; | 
|---|
| 25 | //! instanciate a triangular matrix from the number of rows | 
|---|
| 26 | TriangularMatrix(int rowSize)  : long_diag_((uint_4)rowSize) | 
|---|
| 27 | { | 
|---|
| 28 | elem_.ReSize((uint_4) (rowSize*(rowSize+1)/2) ); | 
|---|
| 29 | } | 
|---|
| 30 | //! Copy constructor (possibility of sharing datas) | 
|---|
| 31 | TriangularMatrix(const TriangularMatrix<T>& a,  bool share=false)  : elem_(a.elem_, share),  long_diag_(a.long_diag_) {;} | 
|---|
| 32 |  | 
|---|
| 33 | //! resize the matrix with a new number of rows | 
|---|
| 34 | inline void ReSizeRow(int rowSize) | 
|---|
| 35 | { | 
|---|
| 36 | long_diag_=(uint_4)rowSize; | 
|---|
| 37 | elem_.ReSize(long_diag_*(long_diag_+1)/2); | 
|---|
| 38 | } | 
|---|
| 39 |  | 
|---|
| 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 | } | 
|---|
| 47 |  | 
|---|
| 48 | //! () operator : access to elements row \b l and column \b m | 
|---|
| 49 | inline T& operator()(int l, int m) | 
|---|
| 50 | { | 
|---|
| 51 | return  elem_(indexOfElement(l,m)); | 
|---|
| 52 | } | 
|---|
| 53 |  | 
|---|
| 54 | inline T& operator()(int index) | 
|---|
| 55 | { | 
|---|
| 56 | return  elem_(index); | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 |  | 
|---|
| 60 | //! () operator : access to elements row \b l and column \b m | 
|---|
| 61 | inline T const& operator()(int l, int m) const | 
|---|
| 62 | { | 
|---|
| 63 | return *(elem_.Begin()+ indexOfElement(l,m)); | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 | inline T const& operator()(int index) const | 
|---|
| 67 | { | 
|---|
| 68 | return *(elem_.Begin()+ index); | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 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 | } | 
|---|
| 82 |  | 
|---|
| 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 |  | 
|---|
| 105 | //! Return number of rows | 
|---|
| 106 | inline  int_4  rowNumber() const {return (int_4)long_diag_;} | 
|---|
| 107 |  | 
|---|
| 108 | //! Return size of the total array | 
|---|
| 109 | inline int_4 Size() const {return elem_.Size();} | 
|---|
| 110 |  | 
|---|
| 111 |  | 
|---|
| 112 | void Print(int nbLignes=0) | 
|---|
| 113 | { | 
|---|
| 114 | if (nbLignes == 0 ) nbLignes = long_diag_; | 
|---|
| 115 | cout << " ***** matrice triangulaire : ********* " << endl; | 
|---|
| 116 | for (int k=0; k < nbLignes; k++) | 
|---|
| 117 | { | 
|---|
| 118 | for (int kc = 0; kc <= k ; kc++) | 
|---|
| 119 | { | 
|---|
| 120 | cout << " " << elem_(indexOfElement(k,kc)); | 
|---|
| 121 | } | 
|---|
| 122 | cout << endl; | 
|---|
| 123 | } | 
|---|
| 124 | cout << "---------------- fin matrice ------------" << endl; | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | //Return pointer to first element address | 
|---|
| 128 | //inline T* Data()  {return elem_.Begin();} | 
|---|
| 129 |  | 
|---|
| 130 | //! compute the address of an element in the single array representing the matrix | 
|---|
| 131 | inline uint_4 indexOfElement(int i,int j) const | 
|---|
| 132 | { | 
|---|
| 133 | //  return(i*(i+1)/2+j); | 
|---|
| 134 | // the (inferior triangular )matrix is stored column by column | 
|---|
| 135 | return(i+ long_diag_*j-j*(j+1)/2); | 
|---|
| 136 | } | 
|---|
| 137 |  | 
|---|
| 138 | private: | 
|---|
| 139 |  | 
|---|
| 140 | uint_4 long_diag_;    //!< size of the square matrix | 
|---|
| 141 | NDataBlock<T> elem_;  //!< Data block | 
|---|
| 142 |  | 
|---|
| 143 | }; | 
|---|
| 144 |  | 
|---|
| 145 | }   // namespace SOPHYA | 
|---|
| 146 |  | 
|---|
| 147 | #endif | 
|---|