[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 |
|
---|
| 111 |
|
---|
[1683] | 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 |
|
---|
[914] | 130 | //! compute the address of an element in the single array representing the matrix
|
---|
[1683] | 131 | inline uint_4 indexOfElement(int i,int j) const
|
---|
[762] | 132 | {
|
---|
[1683] | 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);
|
---|
[762] | 136 | }
|
---|
| 137 |
|
---|
[1683] | 138 | private:
|
---|
| 139 |
|
---|
[914] | 140 | uint_4 long_diag_; //!< size of the square matrix
|
---|
| 141 | NDataBlock<T> elem_; //!< Data block
|
---|
[762] | 142 |
|
---|
| 143 | };
|
---|
[1683] | 144 |
|
---|
[762] | 145 | } // namespace SOPHYA
|
---|
| 146 |
|
---|
| 147 | #endif
|
---|