| 1 |
/*************************************************************************** |
| 2 |
matrices.h - Librairie d'objets pour manipuler des matrices |
| 3 |
------------------- |
| 4 |
begin : ven aug 14 10:25:55 CEST 2000 |
| 5 |
copyright : (C) 2000 by Olivier Langella CNRS UPR9034 |
| 6 |
email : [email protected] |
| 7 |
***************************************************************************/ |
| 8 |
|
| 9 |
/*************************************************************************** |
| 10 |
* * |
| 11 |
* This program is free software; you can redistribute it and/or modify * |
| 12 |
* it under the terms of the GNU General Public License as published by * |
| 13 |
* the Free Software Foundation; either version 2 of the License, or * |
| 14 |
* (at your option) any later version. * |
| 15 |
* * |
| 16 |
***************************************************************************/ |
| 17 |
|
| 18 |
// Objets permettant de manipuler des matrices |
| 19 |
// Olivier Langella le 11/3/98 |
| 20 |
// [email protected] |
| 21 |
|
| 22 |
//#include "vecteurs.h" |
| 23 |
|
| 24 |
// les matrices |
| 25 |
#ifndef MATRICES_H |
| 26 |
#define MATRICES_H |
| 27 |
|
| 28 |
//#define WITHOUT_EXPAT |
| 29 |
//#ifndef WITHOUT_EXPAT |
| 30 |
//#include "expat.h" |
| 31 |
//#endif |
| 32 |
|
| 33 |
#include <iostream> |
| 34 |
#include <sstream> |
| 35 |
#include <fstream> |
| 36 |
#include <cstdlib> |
| 37 |
#include <string> |
| 38 |
#include <vector> |
| 39 |
#include <cmath> |
| 40 |
#include <QDebug> |
| 41 |
#include <QXmlStreamWriter> |
| 42 |
#include <QString> |
| 43 |
//#include <locale> |
| 44 |
#include "vecteurs.h" |
| 45 |
|
| 46 |
namespace biolib { |
| 47 |
namespace vecteurs { |
| 48 |
|
| 49 |
class MatriceLD; |
| 50 |
// les matrices |
| 51 |
class Matrice { |
| 52 |
public: |
| 53 |
Matrice(); |
| 54 |
Matrice(unsigned long nl, unsigned long nc = 0); |
| 55 |
Matrice(const Matrice&); //constructeur de copie |
| 56 |
//Matrice(const MatriceLD& lamatrice):Matrice<long double>(lamatrice) {}; //constructeur de copie |
| 57 |
//Matrice(const MatriceF& lamatrice):Matrice<float>(lamatrice) {}; //constructeur de copie |
| 58 |
virtual ~Matrice() { |
| 59 |
if (_tab != 0) |
| 60 |
delete[] _tab; |
| 61 |
} |
| 62 |
|
| 63 |
virtual long double& GetCase(const ChaineCar& nomLig, const ChaineCar& nomCol) const { |
| 64 |
long ni(_tlig.Position(nomLig)); |
| 65 |
long nj(_tcol.Position(nomCol)); |
| 66 |
//cerr << nomLig << "GetCase ni " << ni; |
| 67 |
//cerr << nomCol << "GetCase nj " << nj; |
| 68 |
if ((ni < 0) || (nj < 0)) |
| 69 |
throw Anomalie(7); |
| 70 |
return (GetCase(ni, nj)); |
| 71 |
} |
| 72 |
|
| 73 |
virtual long double& GetCase(unsigned long i, unsigned long j) const { |
| 74 |
if ((i >= _nl) || (j >= _nc)) |
| 75 |
throw Anomalie(7); |
| 76 |
//renvoie la valeur de la case i (ligne), j (colonne) |
| 77 |
long swap; |
| 78 |
if ((_type == 3) && (j > i)) { |
| 79 |
swap = i; |
| 80 |
i = j; |
| 81 |
j = swap; |
| 82 |
} |
| 83 |
return (_tab[(i * _nc) + j]); |
| 84 |
} |
| 85 |
|
| 86 |
virtual void SetFlag(int i) { |
| 87 |
_flag = i; |
| 88 |
} |
| 89 |
//etiquettes sur lignes ou colonnes |
| 90 |
|
| 91 |
|
| 92 |
long GetNC() const { |
| 93 |
return (_nc); |
| 94 |
} |
| 95 |
|
| 96 |
long GetNL() const { |
| 97 |
return (_nl); |
| 98 |
} |
| 99 |
|
| 100 |
int GetType() const { |
| 101 |
return (_type); |
| 102 |
} |
| 103 |
|
| 104 |
int GetFlag() const { |
| 105 |
return (_flag); |
| 106 |
} |
| 107 |
|
| 108 |
virtual long double get_ppvaleur() const; //recherche la plus petite valeur |
| 109 |
|
| 110 |
virtual bool SetType(int type); //type de matrice: |
| 111 |
//1 -> rectangulaire |
| 112 |
//3 -> triangulaire |
| 113 |
virtual void resize(unsigned long nblig, unsigned long nbcol); //efface et réalloue de la mémoire |
| 114 |
virtual void SupprCol(unsigned long); //efface et réalloue de la mémoire |
| 115 |
virtual void SupprLig(unsigned long); //efface et réalloue de la mémoire |
| 116 |
virtual bool redim(unsigned long, unsigned long); //redimensionne la matrice |
| 117 |
|
| 118 |
void ftranspose(); |
| 119 |
virtual void f_neg2zero(); |
| 120 |
void fscol(vector<long double> & somme) const; //somme des colonnes |
| 121 |
void fslig(vector<long double> & somme) const; //somme des colonnes |
| 122 |
void fswapcol(long i, long j); |
| 123 |
long double fdet() const; |
| 124 |
long double ftrace() const; |
| 125 |
|
| 126 |
inline void fmultiplier(const Matrice& matA, const Matrice& matB); |
| 127 |
|
| 128 |
Titre _titre; |
| 129 |
|
| 130 |
Titre _tcol; |
| 131 |
Titre _tlig; |
| 132 |
|
| 133 |
virtual void iFichier(istream& ientree); |
| 134 |
void iFichier(istream & entree, int i) { |
| 135 |
//cerr << "coucou " << i << endl; |
| 136 |
|
| 137 |
switch (i) { |
| 138 |
case 1: |
| 139 |
iExcel(entree); |
| 140 |
return; |
| 141 |
break; |
| 142 |
case 2: |
| 143 |
iNtsys(entree); |
| 144 |
return; |
| 145 |
break; |
| 146 |
case 4: |
| 147 |
iPhylip(entree); |
| 148 |
return; |
| 149 |
break; |
| 150 |
case 5: |
| 151 |
iGnumeric(entree); |
| 152 |
return; |
| 153 |
break; |
| 154 |
default: |
| 155 |
return; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
virtual void oFormat(ostream & sortie, int i) { |
| 160 |
switch (i) { |
| 161 |
case 1: |
| 162 |
oExcel(sortie); |
| 163 |
break; |
| 164 |
case 2: |
| 165 |
oNtsys(sortie); |
| 166 |
break; |
| 167 |
case 3: |
| 168 |
// oXgobi(sortie); |
| 169 |
throw Anomalie(6); |
| 170 |
break; |
| 171 |
case 4: |
| 172 |
oPhylip(sortie); |
| 173 |
// throw Anomalie(6); |
| 174 |
break; |
| 175 |
case 5: |
| 176 |
oGnumeric(sortie); |
| 177 |
// throw Anomalie(6); |
| 178 |
break; |
| 179 |
default: |
| 180 |
break; |
| 181 |
} |
| 182 |
|
| 183 |
} |
| 184 |
|
| 185 |
//1= Excel |
| 186 |
//2= Ntsys |
| 187 |
//3= xgobi ==!!!! seulement en ofstream !!!!! |
| 188 |
|
| 189 |
virtual void ofFormat(ofstream & sortie, int i = 1) { |
| 190 |
if (i == 3) { |
| 191 |
string fichier("xgobi"); |
| 192 |
oXgobi(fichier); |
| 193 |
} else { |
| 194 |
oFormat(sortie, i); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
virtual void ofFormat(ofstream & sortie, int i, const string &fichier) { |
| 199 |
if (i == 3) { |
| 200 |
oXgobi(fichier); |
| 201 |
} else { |
| 202 |
oFormat(sortie, i); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
virtual void iNtsys(istream & entree); |
| 207 |
virtual void iExcel(istream & entree); |
| 208 |
virtual void iGnumeric(istream & entree); |
| 209 |
virtual void iPhylip(istream & entree); |
| 210 |
void oNtsys(ostream & sortie); |
| 211 |
void oExcel(ostream & sortie); |
| 212 |
void oGnumeric(ostream & sortie); |
| 213 |
void |
| 214 |
oGnumericSheet(QXmlStreamWriter& xml_stream, |
| 215 |
const QString & sheetname) const; |
| 216 |
void oPhylip(ostream& sortie); |
| 217 |
void oXgobi(const string&); |
| 218 |
|
| 219 |
virtual const Matrice& operator=(const Matrice &rval); |
| 220 |
const Matrice& operator=(vector<long double> &rval); |
| 221 |
Matrice operator*(Matrice &rval) const; |
| 222 |
vector<long double> operator*(const vector<long double> &rval); |
| 223 |
Matrice operator*(long double scalaire); |
| 224 |
Matrice operator-(Matrice &rval); |
| 225 |
Matrice operator+(Matrice &rval); |
| 226 |
Matrice operator+(long double rval); |
| 227 |
|
| 228 |
protected: |
| 229 |
|
| 230 |
long double fdetrec() const; |
| 231 |
|
| 232 |
long rindice(long ligne, long colonne) { |
| 233 |
//if (_type == 3) if (colonne > ligne) return((colonne * _nc) + ligne); |
| 234 |
return ((ligne * GetNC()) + colonne); |
| 235 |
} |
| 236 |
|
| 237 |
unsigned long _t; //taille (_nc * _nl) |
| 238 |
unsigned long _nc; //nb de colonnes (i) |
| 239 |
unsigned long _nl; //nb de lignes (j) |
| 240 |
unsigned int _type; //type de matrice |
| 241 |
int _miss; //missing value |
| 242 |
unsigned int _flag; //étiquettes |
| 243 |
//1=> titres des lignes 2=> titres des colonnes |
| 244 |
//3=> les 2 //0=> rien |
| 245 |
|
| 246 |
public: |
| 247 |
|
| 248 |
long double* _tab; // matrice |
| 249 |
//formats de fichiers |
| 250 |
unsigned int _oformat; //1-> Excel 2->NTsys 3->xgobi (UNIX) 4->phylip |
| 251 |
|
| 252 |
struct Anomalie { |
| 253 |
Anomalie(int i) : |
| 254 |
le_pb(i) { |
| 255 |
} |
| 256 |
|
| 257 |
// 1-> erreur pendant lecture de fichier |
| 258 |
// 2-> erreur dans mdiaggl |
| 259 |
// 3-> opération impossible sur ce type de matrice |
| 260 |
// 4-> calcul impossible |
| 261 |
// 5-> Echec de la conversion de la matrice en matrice symétrique |
| 262 |
|
| 263 |
// 6-> erreur pendant l'écriture du fichier |
| 264 |
// 7-> acces hors bornes dans la matrice |
| 265 |
int le_pb; |
| 266 |
string _message; |
| 267 |
|
| 268 |
string& fmessage(int num) { |
| 269 |
switch (num) { |
| 270 |
case 1: |
| 271 |
_message = "Erreur pendant la lecture du fichier..."; |
| 272 |
break; |
| 273 |
case 2: |
| 274 |
_message = "Cette matrice ne peut être diagonalisée"; |
| 275 |
break; |
| 276 |
case 3: |
| 277 |
_message = "Opération impossible sur ce type de matrice"; |
| 278 |
break; |
| 279 |
case 4: |
| 280 |
_message = "Calcul impossible"; |
| 281 |
break; |
| 282 |
case 5: |
| 283 |
_message |
| 284 |
= "Echec de la conversion de la matrice en matrice symétrique"; |
| 285 |
break; |
| 286 |
case 6: |
| 287 |
_message = "Erreur pendant l'ecriture du fichier"; |
| 288 |
break; |
| 289 |
case 7: |
| 290 |
_message = "Acces hors bornes dans la matrice"; |
| 291 |
break; |
| 292 |
|
| 293 |
default: |
| 294 |
_message = ""; |
| 295 |
break; |
| 296 |
} |
| 297 |
return (_message); |
| 298 |
} |
| 299 |
}; |
| 300 |
|
| 301 |
}; |
| 302 |
|
| 303 |
// les matrices |
| 304 |
class MatriceLD: public Matrice { |
| 305 |
public: |
| 306 |
MatriceLD(); |
| 307 |
MatriceLD(long nl, long nc = 0) : |
| 308 |
Matrice (nl, nc) { |
| 309 |
} |
| 310 |
|
| 311 |
MatriceLD(const MatriceLD& lamatrice) : |
| 312 |
Matrice (lamatrice) { |
| 313 |
} |
| 314 |
//constructeur de copie |
| 315 |
MatriceLD(const Matrice& lamatrice) : |
| 316 |
Matrice (lamatrice) { |
| 317 |
} |
| 318 |
//constructeur de copie |
| 319 |
virtual ~MatriceLD() { |
| 320 |
} |
| 321 |
|
| 322 |
void fatd(MatriceLD&, MatriceLD&); |
| 323 |
void fscalaire(MatriceLD& pscalaire); |
| 324 |
void fdiagonalisation(MatriceLD &, vector<long double> &); |
| 325 |
void fmdiagql(int m, long double *ps, vector<long double> & pvlp); |
| 326 |
int ftridiag(long double *ps, vector<long double> & pvlp); |
| 327 |
void fcoordcol(MatriceLD& stat, MatriceLD& res, MatriceLD& norm, vector< |
| 328 |
long double> & pvlp); |
| 329 |
/* |
| 330 |
|
| 331 |
void folicovariance(MatriceLD& pcov, bool facteur=false); //ya un truc |
| 332 |
void fcovariance(MatriceLD& pcov); //ya un truc |
| 333 |
// pourquoi on n eprend qu'une partie de l'originale ? |
| 334 |
void fnormalise(MatriceLD & ,MatriceLD & , vector<long double> &) const; |
| 335 |
void ftrivectshell(int, int, vector<long double> & pvlp); |
| 336 |
void fprojection(MatriceLD& mccord,MatriceLD& mstat,MatriceLD& vectpropres, vector<long double> & pvlp) const; |
| 337 |
|
| 338 |
void fafc (MatriceLD&, MatriceLD&); |
| 339 |
void foliafc (MatriceLD&, MatriceLD&, long double precision=0.000001); |
| 340 |
void foliatd (MatriceLD&, MatriceLD&, long double precision=0.000001); |
| 341 |
void facp (MatriceLD&, MatriceLD&, long double precision=0.000001); |
| 342 |
void fvalpropres (MatriceLD&, vector<long double> &, long double precision=0.000001); |
| 343 |
|
| 344 |
*/ |
| 345 |
|
| 346 |
void finv(MatriceLD& resultat) const; |
| 347 |
|
| 348 |
void fvalpropres(MatriceLD&, vector<float> &, float precision = |
| 349 |
0.000001); |
| 350 |
|
| 351 |
}; |
| 352 |
|
| 353 |
// les tableaux de matrices |
| 354 |
class JeuMatriceLD { |
| 355 |
public: |
| 356 |
|
| 357 |
JeuMatriceLD(istream& entree); |
| 358 |
JeuMatriceLD() { |
| 359 |
} |
| 360 |
|
| 361 |
// JeuMatriceLD(deque<VecteurLD *>, Titre &, ) {}; |
| 362 |
~JeuMatriceLD() { |
| 363 |
long i, t(_tableau.size()); |
| 364 |
for (i = 0; i < t; i++) |
| 365 |
delete _tableau[i]; |
| 366 |
} |
| 367 |
|
| 368 |
MatriceLD& GetMatrice(long i) { |
| 369 |
return *(_tableau[i]); |
| 370 |
} |
| 371 |
const MatriceLD& GetConstMatrice(long i) const { |
| 372 |
return *(_tableau[i]); |
| 373 |
} |
| 374 |
MatriceLD& GetMatrice(ChaineCar chaine) { |
| 375 |
long i(_titres.Position(chaine)); |
| 376 |
if (i < 0) |
| 377 |
throw Anomalie(1); |
| 378 |
return GetMatrice(i); |
| 379 |
} |
| 380 |
MatriceLD& back() { |
| 381 |
return *(_tableau.back()); |
| 382 |
} |
| 383 |
|
| 384 |
void iFlux(istream& entree); |
| 385 |
|
| 386 |
void oFlux(ostream& sortie, int format = 1) const; |
| 387 |
|
| 388 |
void push_back(MatriceLD* Pmatrice, const string titre) { |
| 389 |
_titres.push_back(titre); |
| 390 |
_tableau.push_back(Pmatrice); |
| 391 |
} |
| 392 |
|
| 393 |
void SetTitre(long i, const char * chaine); |
| 394 |
void resize(long i); |
| 395 |
long size() const { |
| 396 |
return (_tableau.size()); |
| 397 |
} |
| 398 |
|
| 399 |
private: |
| 400 |
void iFluxXML(istream& entree); |
| 401 |
void oGnumeric(ostream& sortie) const; |
| 402 |
|
| 403 |
vector<MatriceLD*> _tableau; |
| 404 |
Titre _titres; |
| 405 |
|
| 406 |
public: |
| 407 |
struct Anomalie { |
| 408 |
Anomalie(int i) : |
| 409 |
le_pb(i) { |
| 410 |
} |
| 411 |
|
| 412 |
// 1-> Matrice introuvable |
| 413 |
int le_pb; |
| 414 |
string _message; |
| 415 |
|
| 416 |
string& fmessage(int num) { |
| 417 |
switch (num) { |
| 418 |
case 1: |
| 419 |
_message = "Matrice introuvable..."; |
| 420 |
break; |
| 421 |
|
| 422 |
default: |
| 423 |
_message = ""; |
| 424 |
break; |
| 425 |
} |
| 426 |
return (_message); |
| 427 |
} |
| 428 |
}; |
| 429 |
|
| 430 |
}; |
| 431 |
|
| 432 |
ostream& operator<<(ostream& sortie, Matrice& lamatrice); |
| 433 |
|
| 434 |
//ofstream& operator<<(ofstream& sortie,MatriceLD& lamatrice); |
| 435 |
ofstream& operator<<(ofstream& sortie, Matrice& lamatrice); |
| 436 |
|
| 437 |
istringstream& operator>>(istringstream& ientree, |
| 438 |
Matrice& lamatrice); |
| 439 |
|
| 440 |
} //namespace biolib { |
| 441 |
} //namespace vecteurs { |
| 442 |
|
| 443 |
#endif |
| 444 |
|