[762] | 1 | // This may look like C code, but it is really -*- C++ -*-
|
---|
| 2 | // C.Magneville 04/99
|
---|
| 3 | #ifndef TMatrix_SEEN
|
---|
| 4 | #define TMatrix_SEEN
|
---|
| 5 |
|
---|
| 6 | #include "machdefs.h"
|
---|
[804] | 7 | #include "tarray.h"
|
---|
[762] | 8 |
|
---|
| 9 | namespace SOPHYA {
|
---|
| 10 |
|
---|
[894] | 11 | //! Class of matrixes
|
---|
| 12 | /*!
|
---|
[920] | 13 | \class SOPHYA::TMatrix
|
---|
| 14 | \ingroup TArray
|
---|
[894] | 15 | \sa TArray
|
---|
| 16 | */
|
---|
| 17 |
|
---|
[762] | 18 | template <class T>
|
---|
[804] | 19 | class TMatrix : public TArray<T> {
|
---|
[762] | 20 | public:
|
---|
| 21 | // Creation / destruction
|
---|
| 22 | TMatrix();
|
---|
[804] | 23 | TMatrix(uint_4 r,uint_4 c, short mm=AutoMemoryMapping);
|
---|
[762] | 24 | TMatrix(const TMatrix<T>& a);
|
---|
[804] | 25 | TMatrix(const TMatrix<T>& a, bool share);
|
---|
| 26 | TMatrix(const TArray<T>& a);
|
---|
[914] | 27 | TMatrix(const TArray<T>& a, bool share, short mm=AutoMemoryMapping);
|
---|
[762] | 28 | virtual ~TMatrix();
|
---|
| 29 |
|
---|
[804] | 30 | // Pour verifiez la compatibilite de dimensions lors de l'affectation
|
---|
| 31 | virtual TArray<T>& Set(const TArray<T>& a);
|
---|
[894] | 32 | //! Operator = between matrices
|
---|
[804] | 33 | inline TMatrix<T>& operator = (const TMatrix<T>& a)
|
---|
[894] | 34 | { Set(a); return(*this); }
|
---|
[762] | 35 |
|
---|
[804] | 36 | // Size - Changing the Size
|
---|
[894] | 37 | //! return number of rows
|
---|
[804] | 38 | inline uint_4 NRows() const {return Size(marowi_); }
|
---|
[894] | 39 | //! return number of columns
|
---|
[804] | 40 | inline uint_4 NCols() const {return Size(macoli_); }
|
---|
[894] | 41 | //! return number of columns
|
---|
[804] | 42 | inline uint_4 NCol() const {return Size(macoli_); } // back-compat Peida
|
---|
[762] | 43 |
|
---|
[804] | 44 | void ReSize(uint_4 r,uint_4 c, short mm=SameMemoryMapping); // Reallocation de place
|
---|
| 45 | void Realloc(uint_4 r,uint_4 c, short mm=SameMemoryMapping, bool force=false);
|
---|
[762] | 46 |
|
---|
[813] | 47 | // Sub-matrix extraction $CHECK$ Reza 03/2000 Doit-on declarer ces methode const ?
|
---|
| 48 | TMatrix<T> SubMatrix(Range rline, Range rcol) const ;
|
---|
[894] | 49 | //! () : Return submatrix define by \b Range \b rline and \b rcol
|
---|
[813] | 50 | inline TMatrix<T> operator () (Range rline, Range rcol) const
|
---|
| 51 | { return SubMatrix(rline, rcol); }
|
---|
| 52 | // Lignes et colonnes de la matrice
|
---|
[894] | 53 | //! Return submatrix define by line \b ir (line vector)
|
---|
[813] | 54 | inline TMatrix<T> Row(uint_4 ir) const
|
---|
| 55 | { return SubMatrix(Range(ir,ir), Range(0,NCols()-1)); }
|
---|
[894] | 56 | //! Return submatrix define by column \b ic (column vector)
|
---|
[813] | 57 | inline TMatrix<T> Column(uint_4 ic) const
|
---|
| 58 | { return SubMatrix(Range(0,NRows()-1), Range(ic,ic)); }
|
---|
[804] | 59 |
|
---|
| 60 | // Inline element acces methods
|
---|
| 61 | inline T const& operator()(uint_4 r,uint_4 c) const;
|
---|
| 62 | inline T& operator()(uint_4 r,uint_4 c);
|
---|
| 63 |
|
---|
[762] | 64 | // Operations matricielles
|
---|
[804] | 65 | TMatrix<T>& Transpose();
|
---|
| 66 | //mm = SameMemoryMapping or CMemoryMapping or FortranMemoryMapping
|
---|
| 67 | TMatrix<T> Transpose(short mm);
|
---|
| 68 | // Rearranging Matrix Elements
|
---|
| 69 | TMatrix<T> Rearrange(short mm);
|
---|
[762] | 70 |
|
---|
| 71 | // Operateur d'affectation
|
---|
[804] | 72 | // A = x (matrice diagonale Identite)
|
---|
| 73 | virtual TMatrix<T>& SetIdentity(IdentityMatrix imx);
|
---|
[894] | 74 | // = : fill matrix with an identity matrix \b imx
|
---|
[804] | 75 | inline TMatrix<T>& operator = (IdentityMatrix imx) { return SetIdentity(imx); }
|
---|
[762] | 76 |
|
---|
[894] | 77 | // = : fill matrix with a Sequence \b seq
|
---|
[813] | 78 | inline TMatrix<T>& operator = (Sequence seq) { SetSeq(seq); return(*this); }
|
---|
| 79 |
|
---|
[804] | 80 | // Operations diverses avec une constante
|
---|
[894] | 81 | //! = : fill matrix with constant value \b x
|
---|
[813] | 82 | inline TMatrix<T>& operator = (T x) { SetT(x); return(*this); }
|
---|
[894] | 83 | //! += : add constant value \b x to matrix
|
---|
[804] | 84 | inline TMatrix<T>& operator += (T x) { Add(x); return(*this); }
|
---|
[894] | 85 | //! -= : substract constant value \b x to matrix
|
---|
[804] | 86 | inline TMatrix<T>& operator -= (T x) { Sub(x); return(*this); }
|
---|
[894] | 87 | //! *= : multiply matrix by constant value \b x
|
---|
[804] | 88 | inline TMatrix<T>& operator *= (T x) { Mul(x); return(*this); }
|
---|
[894] | 89 | //! /= : divide matrix by constant value \b x
|
---|
[804] | 90 | inline TMatrix<T>& operator /= (T x) { Div(x); return(*this); }
|
---|
[762] | 91 |
|
---|
[804] | 92 | // operations avec matrices
|
---|
[894] | 93 | //! += : add a matrix
|
---|
[813] | 94 | inline TMatrix<T>& operator += (const TMatrix<T>& a) { AddElt(a); return(*this); }
|
---|
[894] | 95 | //! -= : substract a matrix
|
---|
[813] | 96 | inline TMatrix<T>& operator -= (const TMatrix<T>& a) { SubElt(a); return(*this); }
|
---|
| 97 | TMatrix<T> Multiply(const TMatrix<T>& b, short mm=SameMemoryMapping) const;
|
---|
[894] | 98 | //! *= : matrix product : C = (*this)*B
|
---|
[813] | 99 | inline TMatrix<T>& operator *= (const TMatrix<T>& b)
|
---|
| 100 | { this->Set(Multiply(b)); return(*this); }
|
---|
[762] | 101 |
|
---|
[813] | 102 | // I/O print, ...
|
---|
| 103 | virtual string InfoString() const;
|
---|
[804] | 104 | virtual void Print(ostream& os, int_4 maxprt=-1, bool si=false) const ;
|
---|
[762] | 105 |
|
---|
| 106 | protected:
|
---|
| 107 | };
|
---|
| 108 |
|
---|
[804] | 109 | // ---- inline acces methods ------
|
---|
[894] | 110 | //! () : return element for line \b r and column \b c
|
---|
[804] | 111 | template <class T>
|
---|
| 112 | inline T const& TMatrix<T>::operator()(uint_4 r, uint_4 c) const
|
---|
| 113 | {
|
---|
| 114 | #ifdef SO_BOUNDCHECKING
|
---|
| 115 | if (marowi_ == 0) CheckBound(r, c, 0, 0, 0, 4);
|
---|
| 116 | else CheckBound(c, r, 0, 0, 0, 4);
|
---|
| 117 | #endif
|
---|
| 118 | return ( *( mNDBlock.Begin()+ offset_+
|
---|
| 119 | r*step_[marowi_] + c*step_[macoli_] ) );
|
---|
| 120 | }
|
---|
[762] | 121 |
|
---|
[894] | 122 | //! () : return element for line \b r and column \b c
|
---|
[762] | 123 | template <class T>
|
---|
[804] | 124 | inline T & TMatrix<T>::operator()(uint_4 r, uint_4 c)
|
---|
| 125 | {
|
---|
| 126 | #ifdef SO_BOUNDCHECKING
|
---|
| 127 | if (marowi_ == 0) CheckBound(r, c, 0, 0, 0, 4);
|
---|
| 128 | else CheckBound(c, r, 0, 0, 0, 4);
|
---|
| 129 | #endif
|
---|
| 130 | return ( *( mNDBlock.Begin()+ offset_+
|
---|
| 131 | r*step_[marowi_] + c*step_[macoli_] ) );
|
---|
| 132 | }
|
---|
[762] | 133 |
|
---|
| 134 |
|
---|
[813] | 135 | // Surcharge d'operateurs C = A (+,-) B
|
---|
| 136 | // $CHECK$ Reza 3/4/2000 Pas necessaire de redefinir les operateurs
|
---|
| 137 | // Defini au niveau de TArray<T> - Pour ameliorer l'efficacite
|
---|
| 138 | // Doit-on le faire aussi pour les constantes ? - Fin de $CHECK$ Reza 3/4/2000
|
---|
| 139 |
|
---|
[894] | 140 | //! + : add matrixes \b a and \b b
|
---|
[813] | 141 | template <class T>
|
---|
| 142 | inline TMatrix<T> operator + (const TMatrix<T>& a,const TMatrix<T>& b)
|
---|
| 143 | {TMatrix<T> result(a); result.SetTemp(true); result.AddElt(b); return result;}
|
---|
| 144 |
|
---|
[894] | 145 | //! - : substract matrixes \b a and \b b
|
---|
[813] | 146 | template <class T>
|
---|
| 147 | inline TMatrix<T> operator - (const TMatrix<T>& a,const TMatrix<T>& b)
|
---|
| 148 | {TMatrix<T> result(a); result.SetTemp(true); result.SubElt(b); return result;}
|
---|
| 149 |
|
---|
[804] | 150 | // Surcharge d'operateurs C = A * B
|
---|
[894] | 151 | //! - : multiply matrixes \b a and \b b
|
---|
[804] | 152 | template <class T> inline TMatrix<T> operator * (const TMatrix<T>& a, const TMatrix<T>& b)
|
---|
[813] | 153 | { TMatrix<T> result(a); result.SetTemp(true); return(result.Multiply(b)); }
|
---|
[762] | 154 |
|
---|
[894] | 155 | //! Define Matrix to be TMatrix<r_8>
|
---|
[762] | 156 | typedef TMatrix<r_8> Matrix;
|
---|
| 157 |
|
---|
| 158 | } // Fin du namespace
|
---|
| 159 |
|
---|
| 160 | #endif
|
---|