| 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 |   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 |    }
 | 
|---|
| 133 | 
 | 
|---|
| 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 | 
 | 
|---|
| 152 | //! compute the address of an element in the single array representing the matrix
 | 
|---|
| 153 | inline uint_4 indexOfElement(int i,int j) const 
 | 
|---|
| 154 | {
 | 
|---|
| 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);
 | 
|---|
| 158 | }
 | 
|---|
| 159 | 
 | 
|---|
| 160 | private: 
 | 
|---|
| 161 | 
 | 
|---|
| 162 | uint_4 long_diag_;    //!< size of the square matrix
 | 
|---|
| 163 | NDataBlock<T> elem_;  //!< Data block
 | 
|---|
| 164 | 
 | 
|---|
| 165 |   };
 | 
|---|
| 166 |   
 | 
|---|
| 167 | }   // namespace SOPHYA
 | 
|---|
| 168 | 
 | 
|---|
| 169 | #endif
 | 
|---|