source: Sophya/trunk/SophyaLib/NTools/matrix.h@ 291

Last change on this file since 291 was 288, checked in by ansari, 26 years ago

grosses modif avec refonte code dans tmatrix cmv 3/5/99

File size: 7.5 KB
Line 
1// This may look like C code, but it is really -*- C++ -*-
2// Eric Aubourg, Septembre 94
3#ifndef MATRIX_SEEN
4#define MATRIX_SEEN
5
6#define RGCHECK
7
8#include <stdio.h>
9#include <iostream.h>
10#include "peida.h"
11#include "ppersist.h"
12
13class GeneralFit;
14#include "tmatrix.h"
15//cmv template <class T> class TMatrix;
16
17// <summary> Operations matricielles </summary>
18// Operations matricielles, algebre lineaire...
19class Matrix : public PPersist {
20 friend class Vector;
21 friend class MatrixRC;
22 friend class TMatrix<r_8>;
23public:
24 Matrix();
25 // Constructeur, matrice a zero
26 Matrix(int r, int c);
27 // Constructeur avec des valeurs. Pas d'allocation, juste pointe.
28 Matrix(int r, int c, double* values);
29 // Constructeur par copie
30 Matrix(const Matrix& a);
31 // Constructeur par copie a partir d'une TMatrix<r_8>
32 Matrix(const TMatrix<r_8>& a);
33 // Destructeur
34 virtual ~Matrix();
35
36 // Construction automatique pour PPF
37 // <group>
38 enum {classId = ClassId_Matrix};
39 int_4 ClassId() const { return classId; }
40 static PPersist* Create() {return new Matrix(1,1);}
41 // </group>
42
43 // Remise a zero de tous les elements
44 void Zero();
45 // Change la taille de la matrice. Reallocation physique seulement si
46 // pas assez de place, ou forcee si force=true
47 void Realloc(int r, int c, bool force=false);
48
49 // Operateur d'affectation
50 Matrix& operator = (const Matrix& a);
51 // Affectation depuis scalaire : identite * scalaire
52 Matrix& operator = (double x);
53
54 // Impression
55 friend ostream& operator << (ostream& s, const Matrix& a);
56
57 // Acces aux elements
58 // <group>
59 r_8& operator()(int r, int c);
60 r_8 const& operator()(int r, int c) const;
61 // </group>
62
63 // Acces au tableau des elements
64 // <group>
65 inline r_8* Data() {return data;}
66 inline const r_8* Data() const {return data;}
67 // </group>
68 // Norme 1 : somme des valeurs absolues
69 double Norm1();
70 // Norme 2, euclidienne
71 double Norm2();
72
73 // Operations matricielles avec scalaire, allocation d'une nouvelle matrice
74 // <group>
75 friend Matrix operator * (const Matrix& a, double b);
76 friend Matrix operator * (double b, const Matrix& a);
77 friend Matrix operator + (const Matrix& a, double b);
78 friend Matrix operator + (double b, const Matrix& a);
79 friend Matrix operator - (const Matrix& a, double b);
80 friend Matrix operator - (double b, const Matrix& a);
81 friend Matrix operator / (const Matrix& a, double b);
82 // </group>
83
84 // Operations matricielles avec scalaires entiers, pour eviter l'appel
85 // du constructeur de vecteur a partir d'un entier...
86 // sinon v*2 = v * Vector(2), BUG !
87 // <group>
88 friend Matrix operator * (const Matrix& a, int b);
89 friend Matrix operator * (int b, const Matrix& a);
90 friend Matrix operator + (const Matrix& a, int b);
91 friend Matrix operator + (int b, const Matrix& a);
92 friend Matrix operator - (const Matrix& a, int b);
93 friend Matrix operator - (int b, const Matrix& a);
94 friend Matrix operator / (const Matrix& a, int b);
95 // </group>
96
97 // Operations matricielles "en place", avec scalaire
98 // <group>
99 Matrix& operator *= (double b);
100 Matrix& operator += (double b);
101 Matrix& operator -= (double b);
102 Matrix& operator /= (double b);
103 // </group>
104
105 // Operations matricielles
106 // <group>
107 friend Matrix operator * (const Matrix& a, const Matrix& b);
108 friend Matrix operator + (const Matrix& a, const Matrix& b);
109 friend Matrix operator - (const Matrix& a, const Matrix& b);
110
111 Matrix& operator *= (const Matrix& a);
112 Matrix& operator += (const Matrix& b);
113 Matrix& operator -= (const Matrix& b);
114 // </group>
115
116 // Matrice transposee
117 Matrix Transpose() const;
118 // Matrice inverse
119 // <thrown> singMatxErr </thrown>
120 Matrix Inverse() const;
121
122 // Nombre de lignes
123 inline int NRows() const {return nr;}
124 // Nombre de colonnes
125 inline int NCol() const {return nc;}
126
127 // Entrees-sorties PPF
128 // <group>
129 void ReadSelf(PInPersist&);
130 void WriteSelf(POutPersist&) const;
131 // </group>
132
133 // Acces aux rangees et colonnes
134 // <group>
135 MatrixRC Row(int r) const;
136 MatrixRC Col(int c) const;
137 MatrixRC Diag() const;
138 // </group>
139 /* Versions const ? Alors il faut constructeur const pour MatrixRC, */
140 /* mais MatrixRC contient un Matrix* qui detruit const.... */
141 /* mais en eux-meme, ils ne modifient pas la matrice. Ils permettent */
142 /* de le faire... */
143
144 // Pivot de Gauss : diagonalise la matrice A, en effectuant les memes
145 // operations sur la matrice B
146 static double GausPiv(Matrix& A, Matrix& B);
147
148 // Residus et fonction fittees.
149 Matrix* FitResidus(GeneralFit& gfit);
150 Matrix* FitFunction(GeneralFit& gfit);
151
152protected:
153 int_4 nr;
154 int_4 nc;
155 int ndata;
156 int nalloc;
157 r_8* data;
158 bool FromTmatrix;
159};
160
161
162inline r_8& Matrix::operator()(int r, int c)
163{
164#ifdef RGCHECK
165 if (r<0 || r>=nr || c<0 || c>=nc) THROW(rangeCheckErr);
166#endif
167 return(data[r*nc + c]);
168}
169
170
171inline r_8 const& Matrix::operator()(int r, int c) const
172{
173#ifdef RGCHECK
174 if (r<0 || r>=nr || c<0 || c>=nc) THROW(rangeCheckErr);
175#endif
176 return(data[r*nc + c]);
177}
178
179enum RCKind {matrixRow=0, matrixCol=1, matrixDiag=2};
180
181
182// <summary> Acces aux lignes et colonnes des matrices </summary>
183// Permet d'acceder a une ligne ou colonne d'une matrice, en place, comme
184// avec un vecteur.
185class MatrixRC EXC_AWARE {
186friend class Vector;
187friend class Matrix;
188public:
189 MatrixRC();
190
191 virtual ~MatrixRC() {}
192
193 int Next();
194 int Prev();
195 int SetCol(int c);
196 int SetRow(int r);
197 int SetDiag();
198
199 static int Step(const Matrix& m, RCKind rckind);
200 static double* Org(const Matrix&, RCKind rckind, int ind=0);
201
202 int NElts() const;
203 r_8& operator()(int i);
204 r_8 operator()(int i) const;
205
206 MatrixRC& operator = (const MatrixRC& rc);
207 //operator Vector() const;
208 Vector GetVect() const;
209
210 MatrixRC& operator += (const MatrixRC& rc);
211 MatrixRC& operator -= (const MatrixRC& rc);
212
213 MatrixRC& operator *= (double x);
214 MatrixRC& operator /= (double x);
215 MatrixRC& operator -= (double x);
216 MatrixRC& operator += (double x);
217
218 friend double operator * (const MatrixRC& a, const MatrixRC& b);
219
220 MatrixRC& LinComb(double a, double b, const MatrixRC& rc, int first=0);
221 MatrixRC& LinComb(double b, const MatrixRC& rc, int first=0);
222
223 int IMaxAbs(int first=0);
224
225 static void Swap(MatrixRC& rc1, MatrixRC& rc2);
226 // comme c'est symetrique, soit c'est static MatrixRC, soit static Matrix,
227 // soit Matrix avec verification qu'on parle bien de la meme...
228
229protected:
230 MatrixRC(Matrix& m, RCKind kind, int index=0);
231 Matrix* matrix;
232 r_8* data;
233 int index;
234 int step;
235 RCKind kind;
236};
237
238
239inline int MatrixRC::Step(const Matrix& m, RCKind rckind)
240{
241 switch (rckind)
242 {
243 case matrixRow :
244 return 1;
245 case matrixCol :
246 return m.nc;
247 case matrixDiag :
248 return m.nc+1;
249 }
250 return -1;
251}
252
253
254inline double* MatrixRC::Org(const Matrix& m, RCKind rckind, int index)
255{
256 switch (rckind)
257 {
258 case matrixRow :
259 return m.data + index * m.nc;
260 case matrixCol :
261 return m.data + index;
262 case matrixDiag :
263 return m.data;
264 }
265 return (double*) -1;
266}
267
268
269inline int MatrixRC::NElts() const
270{
271 if (!matrix) return -1; // Failure ?
272 switch (kind)
273 {
274 case matrixRow :
275 return matrix->nc;
276 case matrixCol :
277 return matrix->nr;
278 case matrixDiag :
279 return matrix->nc;
280 }
281 return -1;
282}
283
284
285
286inline double& MatrixRC::operator()(int i)
287{
288 return data[i*step];
289}
290
291
292inline double MatrixRC::operator()(int i) const
293{
294 return data[i*step];
295}
296
297#endif /* MATRIX_SEEN */
298
Note: See TracBrowser for help on using the repository browser.