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