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