| 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"
|
|---|
| 7 | #include "tarray.h"
|
|---|
| 8 |
|
|---|
| 9 | namespace SOPHYA {
|
|---|
| 10 |
|
|---|
| 11 | //! Class of matrices
|
|---|
| 12 | template <class T>
|
|---|
| 13 | class TMatrix : public TArray<T> {
|
|---|
| 14 | public:
|
|---|
| 15 | // Creation / destruction
|
|---|
| 16 | TMatrix();
|
|---|
| 17 | TMatrix(sa_size_t r,sa_size_t c, short mm=BaseArray::AutoMemoryMapping);
|
|---|
| 18 | TMatrix(const TMatrix<T>& a);
|
|---|
| 19 | TMatrix(const TMatrix<T>& a, bool share);
|
|---|
| 20 | TMatrix(const TArray<T>& a);
|
|---|
| 21 | TMatrix(const TArray<T>& a, bool share, short mm=BaseArray::AutoMemoryMapping);
|
|---|
| 22 | TMatrix(const BaseArray& a);
|
|---|
| 23 |
|
|---|
| 24 | virtual ~TMatrix();
|
|---|
| 25 |
|
|---|
| 26 | // Pour verifiez la compatibilite de dimensions lors de l'affectation
|
|---|
| 27 | virtual TArray<T>& Set(const TArray<T>& a);
|
|---|
| 28 | //! Operator = between matrices
|
|---|
| 29 | /*! \warning Datas are copied (cloned) from \b a.
|
|---|
| 30 | \sa NDataBlock::operator=(const NDataBlock<T>&) */
|
|---|
| 31 | inline TMatrix<T>& operator = (const TMatrix<T>& a)
|
|---|
| 32 | { Set(a); return(*this); }
|
|---|
| 33 | //! Operator = between a matrix and an array
|
|---|
| 34 | inline TMatrix<T>& operator = (const TArray<T>& a)
|
|---|
| 35 | { Set(a); return(*this); }
|
|---|
| 36 |
|
|---|
| 37 | virtual TArray<T>& SetBA(const BaseArray& a);
|
|---|
| 38 | //! Operator = between matrices with different types
|
|---|
| 39 | inline TMatrix<T>& operator = (const BaseArray& a)
|
|---|
| 40 | { SetBA(a); return(*this); }
|
|---|
| 41 |
|
|---|
| 42 | // Size - Changing the Size
|
|---|
| 43 | //! return number of rows
|
|---|
| 44 | inline sa_size_t NRows() const {return Size(marowi_); }
|
|---|
| 45 | //! return number of columns
|
|---|
| 46 | inline sa_size_t NCols() const {return Size(macoli_); }
|
|---|
| 47 | //! return number of columns
|
|---|
| 48 | inline sa_size_t NCol() const {return Size(macoli_); } // back-compat Peida
|
|---|
| 49 |
|
|---|
| 50 | void ReSize(sa_size_t r,sa_size_t c, short mm=BaseArray::SameMemoryMapping); // Reallocation de place
|
|---|
| 51 | void Realloc(sa_size_t r,sa_size_t c, short mm=BaseArray::SameMemoryMapping, bool force=false);
|
|---|
| 52 |
|
|---|
| 53 | // Sub-matrix extraction $CHECK$ Reza 03/2000 Doit-on declarer ces methode const ?
|
|---|
| 54 | TMatrix<T> SubMatrix(Range rline, Range rcol) const ;
|
|---|
| 55 | //! () : Return submatrix define by \b Range \b rline and \b rcol
|
|---|
| 56 | inline TMatrix<T> operator () (Range rline, Range rcol) const
|
|---|
| 57 | { return SubMatrix(rline, rcol); }
|
|---|
| 58 | // Lignes et colonnes de la matrice
|
|---|
| 59 | //! Return submatrix define by line \b ir (line vector)
|
|---|
| 60 | inline TMatrix<T> Row(sa_size_t ir) const
|
|---|
| 61 | { return SubMatrix(Range(ir,ir), Range(0,NCols()-1)); }
|
|---|
| 62 | //! Return submatrix define by column \b ic (column vector)
|
|---|
| 63 | inline TMatrix<T> Column(sa_size_t ic) const
|
|---|
| 64 | { return SubMatrix(Range(0,NRows()-1), Range(ic,ic)); }
|
|---|
| 65 |
|
|---|
| 66 | // Inline element acces methods
|
|---|
| 67 | inline T const& operator()(sa_size_t r,sa_size_t c) const;
|
|---|
| 68 | inline T& operator()(sa_size_t r,sa_size_t c);
|
|---|
| 69 |
|
|---|
| 70 | // Operations matricielles
|
|---|
| 71 | TMatrix<T>& Transpose();
|
|---|
| 72 | //mm = SameMemoryMapping or CMemoryMapping or FortranMemoryMapping
|
|---|
| 73 | TMatrix<T> Transpose(short mm);
|
|---|
| 74 | // Rearranging Matrix Elements
|
|---|
| 75 | TMatrix<T> Rearrange(short mm);
|
|---|
| 76 |
|
|---|
| 77 | // Operateur d'affectation
|
|---|
| 78 | // A = x (matrice diagonale Identite)
|
|---|
| 79 | virtual TMatrix<T>& SetIdentity(IdentityMatrix imx);
|
|---|
| 80 | // = : fill matrix with an identity matrix \b imx
|
|---|
| 81 | inline TMatrix<T>& operator = (IdentityMatrix imx) { return SetIdentity(imx); }
|
|---|
| 82 |
|
|---|
| 83 | // = : fill matrix with a Sequence \b seq
|
|---|
| 84 | inline TMatrix<T>& operator = (Sequence const & seq) { SetSeq(seq); return(*this); }
|
|---|
| 85 |
|
|---|
| 86 | // Operations diverses avec une constante
|
|---|
| 87 | //! = : fill matrix with constant value \b x
|
|---|
| 88 | inline TMatrix<T>& operator = (T x) { SetT(x); return(*this); }
|
|---|
| 89 | //! += : add constant value \b x to matrix
|
|---|
| 90 | inline TMatrix<T>& operator += (T x) { Add(x); return(*this); }
|
|---|
| 91 | //! -= : substract constant value \b x to matrix
|
|---|
| 92 | inline TMatrix<T>& operator -= (T x) { Sub(x); return(*this); }
|
|---|
| 93 | //! *= : multiply matrix by constant value \b x
|
|---|
| 94 | inline TMatrix<T>& operator *= (T x) { Mul(x); return(*this); }
|
|---|
| 95 | //! /= : divide matrix by constant value \b x
|
|---|
| 96 | inline TMatrix<T>& operator /= (T x) { Div(x); return(*this); }
|
|---|
| 97 |
|
|---|
| 98 | // operations avec matrices
|
|---|
| 99 | //! += : add a matrix
|
|---|
| 100 | inline TMatrix<T>& operator += (const TMatrix<T>& a) { AddElt(a); return(*this); }
|
|---|
| 101 | //! -= : substract a matrix
|
|---|
| 102 | inline TMatrix<T>& operator -= (const TMatrix<T>& a) { SubElt(a); return(*this); }
|
|---|
| 103 | TMatrix<T> Multiply(const TMatrix<T>& b, short mm=BaseArray::SameMemoryMapping) const;
|
|---|
| 104 | //! *= : matrix product : C = (*this)*B
|
|---|
| 105 | inline TMatrix<T>& operator *= (const TMatrix<T>& b)
|
|---|
| 106 | { this->Set(Multiply(b)); return(*this); }
|
|---|
| 107 |
|
|---|
| 108 | // I/O print, ...
|
|---|
| 109 | virtual string InfoString() const;
|
|---|
| 110 | virtual void Print(ostream& os, int_4 maxprt=-1, bool si=false) const ;
|
|---|
| 111 |
|
|---|
| 112 | protected:
|
|---|
| 113 | };
|
|---|
| 114 |
|
|---|
| 115 | // ---- inline acces methods ------
|
|---|
| 116 | //! () : return element for line \b r and column \b c
|
|---|
| 117 | template <class T>
|
|---|
| 118 | inline T const& TMatrix<T>::operator()(sa_size_t r, sa_size_t c) const
|
|---|
| 119 | {
|
|---|
| 120 | #ifdef SO_BOUNDCHECKING
|
|---|
| 121 | if (marowi_ == 0) CheckBound(r, c, 0, 0, 0, 4);
|
|---|
| 122 | else CheckBound(c, r, 0, 0, 0, 4);
|
|---|
| 123 | #endif
|
|---|
| 124 | return ( *( mNDBlock.Begin()+ offset_+
|
|---|
| 125 | r*step_[marowi_] + c*step_[macoli_] ) );
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | //! () : return element for line \b r and column \b c
|
|---|
| 129 | template <class T>
|
|---|
| 130 | inline T & TMatrix<T>::operator()(sa_size_t r, sa_size_t c)
|
|---|
| 131 | {
|
|---|
| 132 | #ifdef SO_BOUNDCHECKING
|
|---|
| 133 | if (marowi_ == 0) CheckBound(r, c, 0, 0, 0, 4);
|
|---|
| 134 | else CheckBound(c, r, 0, 0, 0, 4);
|
|---|
| 135 | #endif
|
|---|
| 136 | return ( *( mNDBlock.Begin()+ offset_+
|
|---|
| 137 | r*step_[marowi_] + c*step_[macoli_] ) );
|
|---|
| 138 | }
|
|---|
| 139 | ////////////////////////////////////////////////////////////////
|
|---|
| 140 | // Surcharge d'operateurs A (+,-,*,/) (T) x
|
|---|
| 141 |
|
|---|
| 142 | /*! \ingroup TMatrix \fn operator+(const TMatrix<T>&,T)
|
|---|
| 143 | \brief Operator TMatrix = TMatrix + constant */
|
|---|
| 144 | template <class T> inline TMatrix<T> operator + (const TMatrix<T>& a, T b)
|
|---|
| 145 | {TMatrix<T> result; result.CloneOrShare(a); result.SetTemp(true);
|
|---|
| 146 | result.Add(b); return result;}
|
|---|
| 147 |
|
|---|
| 148 | /*! \ingroup TMatrix \fn operator+(T,const TMatrix<T>&)
|
|---|
| 149 | \brief Operator TMatrix = constant + TMatrix */
|
|---|
| 150 | template <class T> inline TMatrix<T> operator + (T b,const TMatrix<T>& a)
|
|---|
| 151 | {TMatrix<T> result; result.CloneOrShare(a); result.SetTemp(true);
|
|---|
| 152 | result.Add(b); return result;}
|
|---|
| 153 |
|
|---|
| 154 | /*! \ingroup TMatrix \fn operator-(const TMatrix<T>&,T)
|
|---|
| 155 | \brief Operator TMatrix = TMatrix - constant */
|
|---|
| 156 | template <class T> inline TMatrix<T> operator - (const TMatrix<T>& a, T b)
|
|---|
| 157 | {TMatrix<T> result; result.CloneOrShare(a); result.SetTemp(true);
|
|---|
| 158 | result.Sub(b); return result;}
|
|---|
| 159 |
|
|---|
| 160 | /*! \ingroup TMatrix \fn operator-(T,const TMatrix<T>&)
|
|---|
| 161 | \brief Operator TMatrix = constant - TMatrix */
|
|---|
| 162 | template <class T> inline TMatrix<T> operator - (T b,const TMatrix<T>& a)
|
|---|
| 163 | {TMatrix<T> result; result.CloneOrShare(a); result.SetTemp(true);
|
|---|
| 164 | result.Sub(b,true); return result;}
|
|---|
| 165 |
|
|---|
| 166 | /*! \ingroup TMatrix \fn operator*(const TMatrix<T>&,T)
|
|---|
| 167 | \brief Operator TMatrix = TMatrix * constant */
|
|---|
| 168 | template <class T> inline TMatrix<T> operator * (const TMatrix<T>& a, T b)
|
|---|
| 169 | {TMatrix<T> result; result.CloneOrShare(a); result.SetTemp(true);
|
|---|
| 170 | result.Mul(b); return result;}
|
|---|
| 171 |
|
|---|
| 172 | /*! \ingroup TMatrix \fn operator*(T,const TMatrix<T>&)
|
|---|
| 173 | \brief Operator TMatrix = constant * TMatrix */
|
|---|
| 174 | template <class T> inline TMatrix<T> operator * (T b,const TMatrix<T>& a)
|
|---|
| 175 | {TMatrix<T> result; result.CloneOrShare(a); result.SetTemp(true);
|
|---|
| 176 | result.Mul(b); return result;}
|
|---|
| 177 |
|
|---|
| 178 | /*! \ingroup TMatrix \fn operator/(const TMatrix<T>&,T)
|
|---|
| 179 | \brief Operator TMatrix = TMatrix / constant */
|
|---|
| 180 | template <class T> inline TMatrix<T> operator / (const TMatrix<T>& a, T b)
|
|---|
| 181 | {TMatrix<T> result; result.CloneOrShare(a); result.SetTemp(true);
|
|---|
| 182 | result.Div(b); return result;}
|
|---|
| 183 |
|
|---|
| 184 | /*! \ingroup TMatrix \fn operator/(T,const TMatrix<T>&)
|
|---|
| 185 | \brief Operator TMatrix = constant / TMatrix */
|
|---|
| 186 | template <class T> inline TMatrix<T> operator / (T b, const TMatrix<T>& a)
|
|---|
| 187 | {TMatrix<T> result; result.CloneOrShare(a); result.SetTemp(true);
|
|---|
| 188 | result.Div(b, true); return result;}
|
|---|
| 189 |
|
|---|
| 190 | ////////////////////////////////////////////////////////////////
|
|---|
| 191 | // Surcharge d'operateurs B = -A
|
|---|
| 192 |
|
|---|
| 193 | /*! \ingroup TMatrix \fn operator - (const TMatrix<T>&)
|
|---|
| 194 | \brief Operator - Returns a matrix with elements equal to the opposite of
|
|---|
| 195 | the original matrix elements. */
|
|---|
| 196 | template <class T> inline TMatrix<T> operator - (const TMatrix<T>& a)
|
|---|
| 197 | {TMatrix<T> result; result.CloneOrShare(a); result.SetTemp(true);
|
|---|
| 198 | result.NegateElt(); return result;}
|
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 | // Surcharge d'operateurs C = A (+,-) B
|
|---|
| 202 | // $CHECK$ Reza 3/4/2000 Pas necessaire de redefinir les operateurs
|
|---|
| 203 | // Defini au niveau de TArray<T> - Pour ameliorer l'efficacite
|
|---|
| 204 | // Doit-on le faire aussi pour les constantes ? - Fin de $CHECK$ Reza 3/4/2000
|
|---|
| 205 |
|
|---|
| 206 | /*! \ingroup TArray \fn operator+(const TMatrix<T>&,const TMatrix<T>&)
|
|---|
| 207 | \brief + : add matrixes \b a and \b b */
|
|---|
| 208 | template <class T>
|
|---|
| 209 | inline TMatrix<T> operator + (const TMatrix<T>& a,const TMatrix<T>& b)
|
|---|
| 210 | {TMatrix<T> result; result.SetTemp(true);
|
|---|
| 211 | if (b.IsTemp()) { result.Share(b); result.AddElt(a); }
|
|---|
| 212 | else { result.CloneOrShare(a); result.AddElt(b); }
|
|---|
| 213 | return result; }
|
|---|
| 214 |
|
|---|
| 215 |
|
|---|
| 216 | /*! \ingroup TArray \fn operator-(const TMatrix<T>&,const TMatrix<T>&)
|
|---|
| 217 | \brief \- : substract matrixes \b a and \b b */
|
|---|
| 218 | template <class T>
|
|---|
| 219 | inline TMatrix<T> operator - (const TMatrix<T>& a,const TMatrix<T>& b)
|
|---|
| 220 | {TMatrix<T> result; result.SetTemp(true);
|
|---|
| 221 | if (b.IsTemp()) { result.Share(b); result.SubElt(a, true); }
|
|---|
| 222 | else { result.CloneOrShare(a); result.SubElt(b); }
|
|---|
| 223 | return result; }
|
|---|
| 224 |
|
|---|
| 225 | // Surcharge d'operateurs C = A * B
|
|---|
| 226 | /*! \ingroup TArray \fn operator*(const TMatrix<T>&,const TMatrix<T>&)
|
|---|
| 227 | \brief * : multiply matrixes \b a and \b b */
|
|---|
| 228 | template <class T> inline TMatrix<T> operator * (const TMatrix<T>& a, const TMatrix<T>& b)
|
|---|
| 229 | { return(a.Multiply(b)); }
|
|---|
| 230 |
|
|---|
| 231 | // Typedef pour simplifier et compatibilite Peida
|
|---|
| 232 | /*! \ingroup TArray
|
|---|
| 233 | \typedef Matrix
|
|---|
| 234 | \brief To simplified TMatrix<r_8> writing
|
|---|
| 235 | */
|
|---|
| 236 | typedef TMatrix<r_8> Matrix;
|
|---|
| 237 |
|
|---|
| 238 | } // Fin du namespace
|
|---|
| 239 |
|
|---|
| 240 | #endif
|
|---|