source: Sophya/trunk/SophyaLib/TArray/tmatrix.h@ 2589

Last change on this file since 2589 was 2575, checked in by ansari, 21 years ago

1/ Remplacement des methodes Add/Sub/Mul/DivElt(a) par

Add/Sub/Mul/DivElt(TArray a, TArray res)

2/ Operateurs += -= A+B A-B TArray et TMatrix/TVecteur modifies en consequence
3/ Ajout methode TArray::ScalarProduct()
4/ Methode TArray::SetT renomme en SetCst() SetT garde en alias
5/ Ajout parametre bool fzero (mise a zero) ajoute ds constructeur et

ReSize() de TMatrix et TVecteur.

Reza 29/07/2004

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