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

Last change on this file since 1007 was 1003, checked in by ansari, 25 years ago

portage Mac. de la cosmetique.

File size: 6.4 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(uint_4 r,uint_4 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::CMemoryMapping);
22
23 virtual ~TMatrix();
24
25 // Pour verifiez la compatibilite de dimensions lors de l'affectation
26 virtual TArray<T>& Set(const TArray<T>& a);
27 //! Operator = between matrices
28 /*! \warning Datas are copied (cloned) from \b a.
29 \sa NDataBlock::operator=(const NDataBlock<T>&) */
30 inline TMatrix<T>& operator = (const TMatrix<T>& a)
31 { Set(a); return(*this); }
32
33 // Size - Changing the Size
34 //! return number of rows
35 inline uint_4 NRows() const {return Size(marowi_); }
36 //! return number of columns
37 inline uint_4 NCols() const {return Size(macoli_); }
38 //! return number of columns
39 inline uint_4 NCol() const {return Size(macoli_); } // back-compat Peida
40
41 void ReSize(uint_4 r,uint_4 c, short mm=BaseArray::SameMemoryMapping); // Reallocation de place
42 void Realloc(uint_4 r,uint_4 c, short mm=BaseArray::SameMemoryMapping, bool force=false);
43
44 // Sub-matrix extraction $CHECK$ Reza 03/2000 Doit-on declarer ces methode const ?
45 TMatrix<T> SubMatrix(Range rline, Range rcol) const ;
46 //! () : Return submatrix define by \b Range \b rline and \b rcol
47 inline TMatrix<T> operator () (Range rline, Range rcol) const
48 { return SubMatrix(rline, rcol); }
49 // Lignes et colonnes de la matrice
50 //! Return submatrix define by line \b ir (line vector)
51 inline TMatrix<T> Row(uint_4 ir) const
52 { return SubMatrix(Range(ir,ir), Range(0,NCols()-1)); }
53 //! Return submatrix define by column \b ic (column vector)
54 inline TMatrix<T> Column(uint_4 ic) const
55 { return SubMatrix(Range(0,NRows()-1), Range(ic,ic)); }
56
57 // Inline element acces methods
58 inline T const& operator()(uint_4 r,uint_4 c) const;
59 inline T& operator()(uint_4 r,uint_4 c);
60
61 // Operations matricielles
62 TMatrix<T>& Transpose();
63 //mm = SameMemoryMapping or CMemoryMapping or FortranMemoryMapping
64 TMatrix<T> Transpose(short mm);
65 // Rearranging Matrix Elements
66 TMatrix<T> Rearrange(short mm);
67
68 // Operateur d'affectation
69 // A = x (matrice diagonale Identite)
70 virtual TMatrix<T>& SetIdentity(IdentityMatrix imx);
71 // = : fill matrix with an identity matrix \b imx
72 inline TMatrix<T>& operator = (IdentityMatrix imx) { return SetIdentity(imx); }
73
74 // = : fill matrix with a Sequence \b seq
75 inline TMatrix<T>& operator = (Sequence seq) { SetSeq(seq); return(*this); }
76
77 // Operations diverses avec une constante
78 //! = : fill matrix with constant value \b x
79 inline TMatrix<T>& operator = (T x) { SetT(x); return(*this); }
80 //! += : add constant value \b x to matrix
81 inline TMatrix<T>& operator += (T x) { Add(x); return(*this); }
82 //! -= : substract constant value \b x to matrix
83 inline TMatrix<T>& operator -= (T x) { Sub(x); return(*this); }
84 //! *= : multiply matrix by constant value \b x
85 inline TMatrix<T>& operator *= (T x) { Mul(x); return(*this); }
86 //! /= : divide matrix by constant value \b x
87 inline TMatrix<T>& operator /= (T x) { Div(x); return(*this); }
88
89 // operations avec matrices
90 //! += : add a matrix
91 inline TMatrix<T>& operator += (const TMatrix<T>& a) { AddElt(a); return(*this); }
92 //! -= : substract a matrix
93 inline TMatrix<T>& operator -= (const TMatrix<T>& a) { SubElt(a); return(*this); }
94
95 // Produit matriciel Multiply : C = (*this)*B
96 TMatrix<T> Multiply(const TMatrix<T>& b, short mm=BaseArray::SameMemoryMapping) const;
97 inline TMatrix<T>& operator *= (const TMatrix<T>& b)
98 { this->Set(Multiply(b)); return(*this); }
99
100 // I/O print, ...
101 virtual string InfoString() const;
102 virtual void Print(ostream& os, int_4 maxprt=-1, bool si=false) const ;
103
104protected:
105};
106
107// ---- inline acces methods ------
108 //! () : return element for line \b r and column \b c
109template <class T>
110inline T const& TMatrix<T>::operator()(uint_4 r, uint_4 c) const
111{
112#ifdef SO_BOUNDCHECKING
113 if (marowi_ == 0) CheckBound(r, c, 0, 0, 0, 4);
114 else CheckBound(c, r, 0, 0, 0, 4);
115#endif
116 return ( *( mNDBlock.Begin()+ offset_+
117 r*step_[marowi_] + c*step_[macoli_] ) );
118}
119
120//! () : return element for line \b r and column \b c
121template <class T>
122inline T & TMatrix<T>::operator()(uint_4 r, uint_4 c)
123{
124#ifdef SO_BOUNDCHECKING
125 if (marowi_ == 0) CheckBound(r, c, 0, 0, 0, 4);
126 else CheckBound(c, r, 0, 0, 0, 4);
127#endif
128 return ( *( mNDBlock.Begin()+ offset_+
129 r*step_[marowi_] + c*step_[macoli_] ) );
130}
131
132// Surcharge d'operateurs C = A (+,-) B
133// $CHECK$ Reza 3/4/2000 Pas necessaire de redefinir les operateurs
134// Defini au niveau de TArray<T> - Pour ameliorer l'efficacite
135// Doit-on le faire aussi pour les constantes ? - Fin de $CHECK$ Reza 3/4/2000
136
137/*! \ingroup TArray \fn operator+(const TMatrix<T>&,const TMatrix<T>&)
138 \brief + : add matrixes \b a and \b b */
139template <class T>
140inline TMatrix<T> operator + (const TMatrix<T>& a,const TMatrix<T>& b)
141 {TMatrix<T> result; result.SetTemp(true);
142 if (b.IsTemp()) { result.Share(b); result.AddElt(a); }
143 else { result.CloneOrShare(a); result.AddElt(b); }
144 return result; }
145
146
147/*! \ingroup TArray \fn operator-(const TMatrix<T>&,const TMatrix<T>&)
148 \brief \- : substract matrixes \b a and \b b */
149template <class T>
150inline TMatrix<T> operator - (const TMatrix<T>& a,const TMatrix<T>& b)
151 {TMatrix<T> result; result.SetTemp(true);
152 if (b.IsTemp()) { result.Share(b); result.SubElt(a, true); }
153 else { result.CloneOrShare(a); result.SubElt(b); }
154 return result; }
155
156// Surcharge d'operateurs C = A * B
157/*! \ingroup TArray \fn operator*(const TMatrix<T>&,const TMatrix<T>&)
158 \brief * : multiply matrixes \b a and \b b */
159template <class T> inline TMatrix<T> operator * (const TMatrix<T>& a, const TMatrix<T>& b)
160 { return(a.Multiply(b)); }
161
162// Typedef pour simplifier et compatibilite Peida
163/*! \ingroup TArray
164 \typedef Matrix
165 \brief To simplified TMatrix<r_8> writing
166*/
167typedef TMatrix<r_8> Matrix;
168
169} // Fin du namespace
170
171#endif
Note: See TracBrowser for help on using the repository browser.