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

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

define module HiStats in Doc cmv 13/4/00

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