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);
|
---|
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)
|
---|
53 | { ReSize(r, c, mm); }
|
---|
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();
|
---|
77 | //mm = SameMemoryMapping or CMemoryMapping or FortranMemoryMapping
|
---|
78 | TMatrix<T> Transpose(short mm);
|
---|
79 | // Rearranging Matrix Elements
|
---|
80 | TMatrix<T> Rearrange(short mm);
|
---|
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) { Add(x); return(*this); }
|
---|
96 | //! -= : substract constant value \b x to matrix
|
---|
97 | inline TMatrix<T>& operator -= (T x) { Sub(x); return(*this); }
|
---|
98 | //! *= : multiply matrix by constant value \b x
|
---|
99 | inline TMatrix<T>& operator *= (T x) { Mul(x); return(*this); }
|
---|
100 | //! /= : divide matrix by constant value \b x
|
---|
101 | inline TMatrix<T>& operator /= (T x) { Div(x); return(*this); }
|
---|
102 |
|
---|
103 | // operations avec matrices
|
---|
104 | //! += : add a matrix
|
---|
105 | inline TMatrix<T>& operator += (const TMatrix<T>& a) { AddElt(a); return(*this); }
|
---|
106 | //! -= : substract a matrix
|
---|
107 | inline TMatrix<T>& operator -= (const TMatrix<T>& a) { SubElt(a); return(*this); }
|
---|
108 | TMatrix<T> Multiply(const TMatrix<T>& b, short mm=BaseArray::SameMemoryMapping) const;
|
---|
109 | //! *= : matrix product : C = (*this)*B
|
---|
110 | inline TMatrix<T>& operator *= (const TMatrix<T>& b)
|
---|
111 | { this->Set(Multiply(b)); return(*this); }
|
---|
112 |
|
---|
113 | // I/O print, ...
|
---|
114 | virtual string InfoString() const;
|
---|
115 | virtual void Print(ostream& os, sa_size_t maxprt=-1,
|
---|
116 | bool si=false, bool ascd=false) const ;
|
---|
117 |
|
---|
118 | protected:
|
---|
119 | };
|
---|
120 |
|
---|
121 | // ---- inline acces methods ------
|
---|
122 | //! () : return element for line \b r and column \b c
|
---|
123 | template <class T>
|
---|
124 | inline T const& TMatrix<T>::operator()(sa_size_t r, sa_size_t c) const
|
---|
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 | }
|
---|
133 |
|
---|
134 | //! () : return element for line \b r and column \b c
|
---|
135 | template <class T>
|
---|
136 | inline T & TMatrix<T>::operator()(sa_size_t r, sa_size_t c)
|
---|
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 | }
|
---|
145 | ////////////////////////////////////////////////////////////////
|
---|
146 | // Surcharge d'operateurs A (+,-,*,/) (T) x
|
---|
147 |
|
---|
148 | /*! \ingroup TMatrix \fn operator+(const TMatrix<T>&,T)
|
---|
149 | \brief Operator TMatrix = TMatrix + constant */
|
---|
150 | template <class T> inline TMatrix<T> operator + (const TMatrix<T>& a, T b)
|
---|
151 | {TMatrix<T> result; result.CloneOrShare(a); result.SetTemp(true);
|
---|
152 | result.Add(b); return result;}
|
---|
153 |
|
---|
154 | /*! \ingroup TMatrix \fn operator+(T,const TMatrix<T>&)
|
---|
155 | \brief Operator TMatrix = constant + TMatrix */
|
---|
156 | template <class T> inline TMatrix<T> operator + (T b,const TMatrix<T>& a)
|
---|
157 | {TMatrix<T> result; result.CloneOrShare(a); result.SetTemp(true);
|
---|
158 | result.Add(b); return result;}
|
---|
159 |
|
---|
160 | /*! \ingroup TMatrix \fn operator-(const TMatrix<T>&,T)
|
---|
161 | \brief Operator TMatrix = TMatrix - constant */
|
---|
162 | template <class T> inline TMatrix<T> operator - (const TMatrix<T>& a, T b)
|
---|
163 | {TMatrix<T> result; result.CloneOrShare(a); result.SetTemp(true);
|
---|
164 | result.Sub(b); return result;}
|
---|
165 |
|
---|
166 | /*! \ingroup TMatrix \fn operator-(T,const TMatrix<T>&)
|
---|
167 | \brief Operator TMatrix = constant - TMatrix */
|
---|
168 | template <class T> inline TMatrix<T> operator - (T b,const TMatrix<T>& a)
|
---|
169 | {TMatrix<T> result; result.CloneOrShare(a); result.SetTemp(true);
|
---|
170 | result.Sub(b,true); return result;}
|
---|
171 |
|
---|
172 | /*! \ingroup TMatrix \fn operator*(const TMatrix<T>&,T)
|
---|
173 | \brief Operator TMatrix = TMatrix * constant */
|
---|
174 | template <class T> inline TMatrix<T> operator * (const TMatrix<T>& a, T b)
|
---|
175 | {TMatrix<T> result; result.CloneOrShare(a); result.SetTemp(true);
|
---|
176 | result.Mul(b); return result;}
|
---|
177 |
|
---|
178 | /*! \ingroup TMatrix \fn operator*(T,const TMatrix<T>&)
|
---|
179 | \brief Operator TMatrix = constant * TMatrix */
|
---|
180 | template <class T> inline TMatrix<T> operator * (T b,const TMatrix<T>& a)
|
---|
181 | {TMatrix<T> result; result.CloneOrShare(a); result.SetTemp(true);
|
---|
182 | result.Mul(b); return result;}
|
---|
183 |
|
---|
184 | /*! \ingroup TMatrix \fn operator/(const TMatrix<T>&,T)
|
---|
185 | \brief Operator TMatrix = TMatrix / constant */
|
---|
186 | template <class T> inline TMatrix<T> operator / (const TMatrix<T>& a, T b)
|
---|
187 | {TMatrix<T> result; result.CloneOrShare(a); result.SetTemp(true);
|
---|
188 | result.Div(b); return result;}
|
---|
189 |
|
---|
190 | /*! \ingroup TMatrix \fn operator/(T,const TMatrix<T>&)
|
---|
191 | \brief Operator TMatrix = constant / TMatrix */
|
---|
192 | template <class T> inline TMatrix<T> operator / (T b, const TMatrix<T>& a)
|
---|
193 | {TMatrix<T> result; result.CloneOrShare(a); result.SetTemp(true);
|
---|
194 | result.Div(b, true); return result;}
|
---|
195 |
|
---|
196 | ////////////////////////////////////////////////////////////////
|
---|
197 | // Surcharge d'operateurs B = -A
|
---|
198 |
|
---|
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. */
|
---|
202 | template <class T> inline TMatrix<T> operator - (const TMatrix<T>& a)
|
---|
203 | {TMatrix<T> result; result.CloneOrShare(a); result.SetTemp(true);
|
---|
204 | result.NegateElt(); return result;}
|
---|
205 |
|
---|
206 |
|
---|
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 |
|
---|
212 | /*! \ingroup TArray \fn operator+(const TMatrix<T>&,const TMatrix<T>&)
|
---|
213 | \brief + : add matrixes \b a and \b b */
|
---|
214 | template <class T>
|
---|
215 | inline TMatrix<T> operator + (const TMatrix<T>& a,const TMatrix<T>& b)
|
---|
216 | {TMatrix<T> result; result.SetTemp(true);
|
---|
217 | if (b.IsTemp()) { result.Share(b); result.AddElt(a); }
|
---|
218 | else { result.CloneOrShare(a); result.AddElt(b); }
|
---|
219 | return result; }
|
---|
220 |
|
---|
221 |
|
---|
222 | /*! \ingroup TArray \fn operator-(const TMatrix<T>&,const TMatrix<T>&)
|
---|
223 | \brief \- : substract matrixes \b a and \b b */
|
---|
224 | template <class T>
|
---|
225 | inline TMatrix<T> operator - (const TMatrix<T>& a,const TMatrix<T>& b)
|
---|
226 | {TMatrix<T> result; result.SetTemp(true);
|
---|
227 | if (b.IsTemp()) { result.Share(b); result.SubElt(a, true); }
|
---|
228 | else { result.CloneOrShare(a); result.SubElt(b); }
|
---|
229 | return result; }
|
---|
230 |
|
---|
231 | // Surcharge d'operateurs C = A * B
|
---|
232 | /*! \ingroup TArray \fn operator*(const TMatrix<T>&,const TMatrix<T>&)
|
---|
233 | \brief * : multiply matrixes \b a and \b b */
|
---|
234 | template <class T> inline TMatrix<T> operator * (const TMatrix<T>& a, const TMatrix<T>& b)
|
---|
235 | { return(a.Multiply(b)); }
|
---|
236 |
|
---|
237 | // Typedef pour simplifier et compatibilite Peida
|
---|
238 | /*! \ingroup TArray
|
---|
239 | \typedef Matrix
|
---|
240 | \brief To simplified TMatrix<r_8> writing
|
---|
241 | */
|
---|
242 | typedef TMatrix<r_8> Matrix;
|
---|
243 |
|
---|
244 | } // Fin du namespace
|
---|
245 |
|
---|
246 | #endif
|
---|