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

Last change on this file since 2585 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
Line 
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
9namespace SOPHYA {
10
11//! Class of matrices
12template <class T>
13class TMatrix : public TArray<T> {
14public:
15 // Creation / destruction
16 TMatrix();
17 TMatrix(sa_size_t r,sa_size_t c, short mm=BaseArray::AutoMemoryMapping, bool fzero=true);
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, bool fzero=true);
51 //! a synonym (alias) for method ReSize(sa_size_t, sa_size_t, short)
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); }
54 // Reallocation de place
55 void Realloc(sa_size_t r,sa_size_t c, short mm=BaseArray::SameMemoryMapping, bool force=false);
56
57 // Sub-matrix extraction $CHECK$ Reza 03/2000 Doit-on declarer ces methode const ?
58 TMatrix<T> SubMatrix(Range rline, Range rcol) const ;
59 //! () : Return submatrix define by \b Range \b rline and \b rcol
60 inline TMatrix<T> operator () (Range rline, Range rcol) const
61 { return SubMatrix(rline, rcol); }
62 // Lignes et colonnes de la matrice
63 //! Return submatrix define by line \b ir (line vector)
64 inline TMatrix<T> Row(sa_size_t ir) const
65 { return SubMatrix(Range(ir,ir), Range(0,NCols()-1)); }
66 //! Return submatrix define by column \b ic (column vector)
67 inline TMatrix<T> Column(sa_size_t ic) const
68 { return SubMatrix(Range(0,NRows()-1), Range(ic,ic)); }
69
70 // Inline element acces methods
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);
73
74 // Operations matricielles
75 TMatrix<T>& TransposeSelf();
76 TMatrix<T> Transpose() const;
77 //mm = SameMemoryMapping or CMemoryMapping or FortranMemoryMapping
78 TMatrix<T> Transpose(short mm) const ;
79 // Rearranging Matrix Elements
80 TMatrix<T> Rearrange(short mm) const;
81
82 // Operateur d'affectation
83 // A = x (matrice diagonale Identite)
84 virtual TMatrix<T>& SetIdentity(IdentityMatrix imx);
85 // = : fill matrix with an identity matrix \b imx
86 inline TMatrix<T>& operator = (IdentityMatrix imx) { return SetIdentity(imx); }
87
88 // = : fill matrix with a Sequence \b seq
89 inline TMatrix<T>& operator = (Sequence const & seq) { SetSeq(seq); return(*this); }
90
91 // Operations diverses avec une constante
92 //! = : fill matrix with constant value \b x
93 inline TMatrix<T>& operator = (T x) { SetT(x); return(*this); }
94 //! += : add constant value \b x to matrix
95 inline TMatrix<T>& operator += (T x) { AddCst(x,*this); return(*this); }
96 //! -= : substract constant value \b x to matrix
97 inline TMatrix<T>& operator -= (T x) { SubCst(x,*this); return(*this); }
98 //! *= : multiply matrix by constant value \b x
99 inline TMatrix<T>& operator *= (T x) { MulCst(x,*this); return(*this); }
100 //! /= : divide matrix by constant value \b x
101 inline TMatrix<T>& operator /= (T x) { DivCst(x,*this); return(*this); }
102
103 // operations avec matrices
104 //! += : add a matrix
105 inline TMatrix<T>& operator += (const TMatrix<T>& a) { AddElt(a,*this); return(*this); }
106 //! -= : substract a matrix
107 inline TMatrix<T>& operator -= (const TMatrix<T>& a) { SubElt(a,*this); return(*this); }
108
109 TMatrix<T> Multiply(const TMatrix<T>& b, short mm=BaseArray::SameMemoryMapping) const;
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); }
113
114 // I/O print, ...
115 virtual string InfoString() const;
116 virtual void Print(ostream& os, sa_size_t maxprt=-1,
117 bool si=false, bool ascd=false) const ;
118
119protected:
120};
121
122// ---- inline acces methods ------
123 //! () : return element for line \b r and column \b c
124template <class T>
125inline T const& TMatrix<T>::operator()(sa_size_t r, sa_size_t c) const
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}
134
135//! () : return element for line \b r and column \b c
136template <class T>
137inline T & TMatrix<T>::operator()(sa_size_t r, sa_size_t c)
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}
146////////////////////////////////////////////////////////////////
147// Surcharge d'operateurs A (+,-,*,/) (T) x
148
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)
152 {TMatrix<T> result; result.SetTemp(true);
153 a.AddCst(b,result); return result;}
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)
158 {TMatrix<T> result; result.SetTemp(true);
159 a.AddCst(b,result); return result;}
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)
164 {TMatrix<T> result; result.SetTemp(true);
165 a.SubCst(b,result); return result;}
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)
170 {TMatrix<T> result; result.SetTemp(true);
171 a.SubCst(b,result,true); return result;}
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)
176 {TMatrix<T> result; result.SetTemp(true);
177 a.MulCst(b,result); return result;}
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)
182 {TMatrix<T> result; result.SetTemp(true);
183 a.MulCst(b,result); return result;}
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)
188 {TMatrix<T> result; result.SetTemp(true);
189 a.DivCst(b,result); return result;}
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)
194 {TMatrix<T> result; result.SetTemp(true);
195 a.Div(b,result,true); return result;}
196
197////////////////////////////////////////////////////////////////
198// Surcharge d'operateurs B = -A
199
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)
204 {TMatrix<T> result; result.SetTemp(true);
205 a.NegateElt(result); return result;}
206
207
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
213/*! \ingroup TArray \fn operator+(const TMatrix<T>&,const TMatrix<T>&)
214 \brief + : add matrixes \b a and \b b */
215template <class T>
216inline TMatrix<T> operator + (const TMatrix<T>& a,const TMatrix<T>& b)
217 {TMatrix<T> result; result.SetTemp(true);
218 a.AddElt(b, result); return result; }
219
220
221/*! \ingroup TArray \fn operator-(const TMatrix<T>&,const TMatrix<T>&)
222 \brief \- : substract matrixes \b a and \b b */
223template <class T>
224inline TMatrix<T> operator - (const TMatrix<T>& a,const TMatrix<T>& b)
225 {TMatrix<T> result; result.SetTemp(true);
226 a.SubElt(b, result); return result; }
227
228
229// Surcharge d'operateurs C = A * B
230/*! \ingroup TArray \fn operator*(const TMatrix<T>&,const TMatrix<T>&)
231 \brief * : multiply matrixes \b a and \b b */
232template <class T> inline TMatrix<T> operator * (const TMatrix<T>& a, const TMatrix<T>& b)
233 { return(a.Multiply(b)); }
234
235// Typedef pour simplifier et compatibilite Peida
236/*! \ingroup TArray
237 \typedef Matrix
238 \brief To simplified TMatrix<r_8> writing
239*/
240typedef TMatrix<r_8> Matrix;
241
242} // Fin du namespace
243
244#endif
Note: See TracBrowser for help on using the repository browser.