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

Last change on this file since 2806 was 2752, checked in by ansari, 20 years ago

1/ Suppression du constructeur de copie avec specification d'organisation memoire (MemoryMapping) pour TMatrix<T> et TVector<T>
2/ Amelioration de l'impression des TArray/TMatrix avec la specification setw(largeur)
3/ Correction petit bug dans la lecture fichiers ASCII argument non transmis ds utilarr.cc

Reza 23 Mai 2005

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