Debian Codegraph @

src/matrices.cpp

raw
1 /***************************************************************************
2 matrices.cpp - 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 #include "matrices.h"
19
20 namespace biolib {
21 namespace vecteurs {
22
23
24 Matrice::Matrice(const Matrice& original) {
25 //constructeur de copie
26 unsigned long i;
27
28 // if (_tab != 0) delete [] _tab;
29 _oformat = 1;
30 _nc = original._nc;
31 _nl = original._nl;
32 _t = _nc * _nl;
33
34 _tab = new long double[_t];
35
36 _titre = original._titre;
37 _flag = original._flag;
38 _tcol = original._tcol;
39 _tlig = original._tlig;
40 _miss = original._miss;
41 _type = original._type;
42
43 for (i = 0; i < _t; i++) {
44 //nouvelle s�quence
45 _tab[i] = original._tab[i];
46 }
47
48 }
49
50 Matrice::Matrice() {
51
52 // _Pointeur = *( new (Pointeur<T>));
53 _oformat = 1;
54 _nc = 0;
55 _nl = 0;
56 _t = 0;
57 _tab = 0;
58 _type = 0;
59 _miss = 0;
60 _flag = 0;
61 }
62
63 Matrice::Matrice(unsigned long nl, unsigned long nc) {
64 unsigned long i;
65
66 _oformat = 1;
67 _nc = nc;
68 _nl = nl;
69 _t = nc * nl;
70 _tab = new long double[_t];
71 _type = 1;
72
73 _miss = 0;
74 _flag = 0;
75 _titre.resize(0);
76
77 _tcol.reserve(_nc);
78 _tlig.reserve(_nl);
79 for (i = 0; i < _nc; i++)
80 _tcol.push_back("");
81 for (i = 0; i < _nl; i++)
82 _tlig.push_back("");
83
84 for (i = 0; i < _t; i++)
85 _tab[i] = 0;
86
87 }
88
89 bool Matrice::SetType(int type) {
90
91 switch (type) {
92 case 1: //rectangulaire normale
93 _type = type;
94 break;
95 case 2: //matrice triangulaire (distances)
96 if (_nc == _nl) {
97 unsigned long i, j;
98 for (i = 0; i < _nc; i++) {
99 for (j = 0; j < i; j++) {
100 if (GetCase(i, j) != GetCase(j, i)) {
101 qDebug() << "Matrices::Anomalie "
102 << "failed to convert into symetric matrix (i,j)!=(j,i) "
103 << (float) GetCase(i, j) << " "
104 << (float) GetCase(j, i) << " i " << i << " j "
105 << j;
106 return (false);
107 //throw typename biolib::vecteurs::Matrice<T>::Anomalie(5);
108 }
109 }
110 }
111 _type = type;
112 } else {
113 qDebug() << "Matrices::Anomalie "
114 << "failed to convert into symetric matrix nc!=nl " << _nc
115 << " " << _nl;
116 return (false);
117 //throw typename biolib::vecteurs::Matrice<T>::Anomalie(5); //Echec de conversion
118 }
119 break;
120 default:
121 break;
122 }
123 return (true);
124 }
125
126 const Matrice& Matrice::operator=(
127 const Matrice &rval) {
128
129 unsigned long i;
130
131 if (_tab != 0)
132 delete[] _tab;
133
134 _type = rval._type;
135 _flag = rval._flag;
136 _nc = rval._nc;
137 _nl = rval._nl;
138 _t = rval._t;
139 _tab = new long double[rval._t];
140
141 _titre = rval._titre;
142
143 _tlig = rval._tlig;
144 _tcol = rval._tcol;
145
146 for (i = 0; i < _t; i++)
147 _tab[i] = rval._tab[i];
148
149 return (*this);
150 }
151
152 const Matrice& Matrice::operator=(vector<long double> &rval) {
153
154 int i;
155
156 if (_tab != 0)
157 delete[] _tab;
158
159 _type = 1;
160 _flag = 0;
161 _nc = rval.size();
162 _nl = 1;
163 _t = _nc;
164 _tab = new long double[_nc];
165
166 // _titre = rval._titre;
167
168 // _tlig = rval._tlig;
169 // _tcol = rval._tcol;
170
171 for (i = 0; i < _t; i++)
172 _tab[i] = rval[i];
173
174 return (*this);
175 }
176
177 void Matrice::oExcel(ostream& fichier) {
178 unsigned long i, j;
179
180 // fichier.setf(ios::scientific);
181 //fichier << "coucou";
182 // �criture du titre
183 for (i = 0; i < _titre.size(); i++) {
184 fichier << _titre[i];
185 fichier << endl;
186 }
187
188 fichier << "#";
189 if ((_flag == 2) || (_flag == 3)) {
190 //�tiquettes des colonnes
191 fichier << '\t' << _tcol[0];
192 for (i = 1; i < _nc; i++)
193 fichier << '\t' << _tcol[i];
194 }
195 fichier << endl;
196
197 //�criture de la matrice
198 for (i = 0; i < _nl; i++) {
199 if ((_flag == 1) || (_flag == 3)) {
200 //�tiquettes des lignes
201 fichier << _tlig[i];
202 }
203
204 for (j = 0; j < _nc; j++) {
205 // fichier << '\t' << _tab [i * _nc + j];
206 fichier << '\t' << GetCase(i, j);
207 }
208 fichier << endl;
209 }
210
211 }
212
213 void Matrice::oGnumeric(ostream& fichier) {
214 QString * p_output_xml = new QString("");
215 QXmlStreamWriter xml_stream(p_output_xml);
216 xml_stream.setAutoFormatting(true);
217 xml_stream.writeStartDocument();
218
219 xml_stream.writeNamespace("http://www.gnumeric.org/v10.dtd", "gmr");
220 xml_stream.writeNamespace("http://www.w3.org/2001/XMLSchema-instance",
221 "xsi");
222 xml_stream.writeStartElement("http://www.gnumeric.org/v10.dtd", "Workbook");
223 xml_stream.writeAttribute("http://www.w3.org/2001/XMLSchema-instance",
224 "schemaLocation", "http://www.gnumeric.org/v8.xsd");
225
226 //fichier
227 // << "<gmr:Workbook xmlns:gmr=\"http://www.gnumeric.org/v10.dtd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.gnumeric.org/v8.xsd\">"
228 // << endl;
229
230 xml_stream.writeStartElement("http://www.gnumeric.org/v10.dtd",
231 "Attributes");
232 xml_stream.writeStartElement("http://www.gnumeric.org/v10.dtd", "Attribute");
233 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd", "name",
234 "WorkbookView::show_horizontal_scrollbar");
235 //fichier << "<gmr:Attributes>" << endl;
236 //fichier << " <gmr:Attribute>" << endl;
237 //fichier << "<gmr:name>WorkbookView::show_horizontal_scrollbar</gmr:name>"
238 // << endl;
239 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd", "type", "4");
240 //fichier << " <gmr:type>4</gmr:type>" << endl;
241 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd", "value",
242 "TRUE");
243 //fichier << " <gmr:value>TRUE</gmr:value>" << endl;
244 xml_stream.writeEndElement();
245 // fichier << " </gmr:Attribute>" << endl;
246 xml_stream.writeStartElement("http://www.gnumeric.org/v10.dtd", "Attribute");
247 //fichier << " <gmr:Attribute>" << endl;
248 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd", "name",
249 "WorkbookView::show_vertical_scrollbar");
250 //fichier << " <gmr:name>WorkbookView::show_vertical_scrollbar</gmr:name>"
251 // << endl;
252 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd", "type", "4");
253 //fichier << " <gmr:type>4</gmr:type>" << endl;
254 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd", "value",
255 "TRUE");
256 //fichier << " <gmr:value>TRUE</gmr:value>" << endl;
257 xml_stream.writeEndElement();
258 //fichier << " </gmr:Attribute>" << endl;
259 xml_stream.writeStartElement("http://www.gnumeric.org/v10.dtd", "Attribute");
260 //fichier << " <gmr:Attribute>" << endl;
261 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd", "name",
262 "WorkbookView::show_notebook_tabs");
263 //fichier << " <gmr:name>WorkbookView::show_notebook_tabs</gmr:name>"
264 // << endl;
265 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd", "type", "4");
266 //fichier << " <gmr:type>4</gmr:type>" << endl;
267 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd", "value",
268 "TRUE");
269 //fichier << " <gmr:value>TRUE</gmr:value>" << endl;
270 xml_stream.writeEndElement();
271 //fichier << " </gmr:Attribute>" << endl;
272
273 xml_stream.writeStartElement("http://www.gnumeric.org/v10.dtd", "Attribute");
274 //fichier << " <gmr:Attribute>" << endl;
275 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd", "name",
276 "WorkbookView::do_auto_completion");
277 //fichier << " <gmr:name>WorkbookView::do_auto_completion</gmr:name>"
278 // << endl;
279 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd", "type", "4");
280 //fichier << " <gmr:type>4</gmr:type>" << endl;
281 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd", "value",
282 "TRUE");
283 //fichier << " <gmr:value>TRUE</gmr:value>" << endl;
284 xml_stream.writeEndElement();
285 //fichier << " </gmr:Attribute>" << endl;
286 xml_stream.writeEndElement();
287 //fichier << " </gmr:Attributes>" << endl;
288 xml_stream.writeStartElement("http://www.gnumeric.org/v10.dtd", "Summary");
289 //fichier << " <gmr:Summary>" << endl;
290 xml_stream.writeStartElement("http://www.gnumeric.org/v10.dtd", "Item");
291 //fichier << " <gmr:Item>" << endl;
292 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd", "name",
293 "biology softwares");
294 //fichier << " <gmr:name>biology softwares</gmr:name>" << endl;
295 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd",
296 "val-string", "http://www.pge.cnrs-gif.fr/bioinfo");
297 //fichier
298 // << " <gmr:val-string>http://www.pge.cnrs-gif.fr/bioinfo</gmr:val-string>"
299 // << endl;
300 xml_stream.writeEndElement();
301 //fichier << " </gmr:Item>" << endl;
302 xml_stream.writeStartElement("http://www.gnumeric.org/v10.dtd", "Item");
303 //fichier << " <gmr:Item>" << endl;
304 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd", "name",
305 "author");
306 //fichier << " <gmr:name>author</gmr:name>" << endl;
307 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd",
308 "val-string", "Olivier Langella <[email protected]>");
309 //fichier
310 // << " <gmr:val-string>Olivier Langella, [email protected]</gmr:val-string>"
311 // << endl;
312 xml_stream.writeEndElement();
313 //fichier << " </gmr:Item>" << endl;
314 xml_stream.writeEndElement();
315 //fichier << "</gmr:Summary>" << endl;
316 xml_stream.writeStartElement("http://www.gnumeric.org/v10.dtd",
317 "SheetNameIndex");
318 //fichier << "<gmr:SheetNameIndex>" << endl;
319 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd", "SheetName",
320 "Matrix 1");
321 //fichier << " <gmr:SheetName>Matrix 1</gmr:SheetName>" << endl;
322 xml_stream.writeEndElement();
323 //fichier << "</gmr:SheetNameIndex> " << endl;
324 xml_stream.writeStartElement("http://www.gnumeric.org/v10.dtd", "Sheets");
325 //fichier << "<gmr:Sheets> " << endl;
326 // fichier.setf(ios::scientific);
327
328 QString sheetname("Matrix 1");
329 oGnumericSheet(xml_stream, sheetname);
330
331 //fichier << " </gmr:Sheets> " << endl;
332 //fichier << "</gmr:Workbook> " << endl;
333
334 xml_stream.writeEndDocument();
335
336 fichier << p_output_xml->toStdString();
337 delete (p_output_xml);
338
339 }
340
341 void Matrice::oGnumericSheet(QXmlStreamWriter& xml_stream,
342 const QString & sheetname) const {
343
344 qDebug()
345 << "Matrice<T>::oGnumericSheet(QXmlStreamWriter& xml_stream, const QString & sheetname) begin";
346 unsigned long i, j, irow(0), jcol(0);
347 const QString value_format("0.000000");
348
349 /*fichier << "<gmr:Sheet ";
350 fichier
351 << "DisplayFormulas=\"false\" HideZero=\"false\" HideGrid=\"false\" HideColHeader=\"false\" HideRowHeader=\"false\" DisplayOutlines=\"true\" OutlineSymbolsBelow=\"true\" OutlineSymbolsRight=\"true\">"
352 << endl;*/
353 xml_stream.writeStartElement("http://www.gnumeric.org/v10.dtd", "Sheet");
354 xml_stream.writeAttribute("DisplayFormulas", "false");
355 xml_stream.writeAttribute("HideZero", "false");
356 xml_stream.writeAttribute("HideGrid", "false");
357 xml_stream.writeAttribute("HideColHeader", "false");
358 xml_stream.writeAttribute("HideRowHeader", "false");
359 xml_stream.writeAttribute("DisplayOutlines", "true");
360 xml_stream.writeAttribute("OutlineSymbolsBelow", "true");
361 xml_stream.writeAttribute("OutlineSymbolsRight", "true");
362
363 /*_p_xml_stream->writeAttribute("xml", "lang", "en");
364 _p_xml_stream->writeStartElement("body");
365 _p_xml_stream->writeStartElement("table");
366 _p_xml_stream->writeStartElement("tbody");*/
367
368 // fichier.imbue();
369 // fichier << "<gmr:Name>" << sheetname << "</gmr:Name>" << endl;
370 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd", "Name",
371 sheetname);
372
373 //fichier << "<gmr:MaxCol>" << (GetNC() + 2) << "</gmr:MaxCol>" << endl;
374 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd", "MaxCol",
375 QString::number(GetNC() + 2));
376 //fichier << "<gmr:MaxRow>" << (GetNL() + 5 + _titre.size())
377 // << "</gmr:MaxRow>" << endl;
378 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd", "MaxRow",
379 QString::number((GetNL() + 5 + _titre.size())));
380 //fichier << "<gmr:Zoom>1.000000</gmr:Zoom>" << endl;
381 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd", "Zoom",
382 "1.000000");
383
384 //fichier << "<gmr:Names/>" << endl;
385 xml_stream.writeEmptyElement("http://www.gnumeric.org/v10.dtd", "Names");
386
387 //fichier << "<gmr:Cells>" << endl;
388 xml_stream.writeStartElement("http://www.gnumeric.org/v10.dtd", "Cells");
389 // �criture du titre
390 for (i = 0; i < _titre.size(); i++, irow++) {
391 xml_stream.writeStartElement("http://www.gnumeric.org/v10.dtd", "Cell");
392 //fichier << "<gmr:Cell Col=\"0\" Row=\"" << irow
393 // << "\" ValueType=\"60\">";
394 xml_stream.writeAttribute("Row", QString::number(irow));
395 xml_stream.writeAttribute("Col", "0");
396 xml_stream.writeAttribute("ValueType", "60");
397 //fichier << _titre[i];
398 xml_stream.writeCharacters(_titre[i].c_str());
399 //fichier << "</gmr:Cell>" << endl;
400 xml_stream.writeEndElement();
401 }
402 xml_stream.writeStartElement("http://www.gnumeric.org/v10.dtd", "Cell");
403 xml_stream.writeAttribute("Col", "0");
404 xml_stream.writeAttribute("Row", QString::number(irow));
405 xml_stream.writeAttribute("ValueType", "60");
406 xml_stream.writeCharacters("#");
407 xml_stream.writeEndElement();
408
409 //fichier << "<gmr:Cell Col=\"0\" Row=\"" << irow
410 // << "\" ValueType=\"60\">#</gmr:Cell>";
411 if ((_flag == 2) || (_flag == 3)) {
412 //�tiquettes des colonnes
413 jcol = 1;
414 xml_stream.writeStartElement("http://www.gnumeric.org/v10.dtd", "Cell");
415 xml_stream.writeAttribute("Col", QString::number(jcol));
416 xml_stream.writeAttribute("Row", QString::number(irow));
417 xml_stream.writeAttribute("ValueType", "60");
418 xml_stream.writeCharacters(_tcol[0].c_str());
419 xml_stream.writeEndElement();
420
421 //fichier << "<gmr:Cell Col=\"" << jcol << "\" Row=\"" << irow
422 // << "\" ValueType=\"60\">";
423 //fichier << _tcol[0];
424 //fichier << "</gmr:Cell>" << endl;
425 jcol++;
426 for (i = 1; i < _nc; i++, jcol++) {
427 xml_stream.writeStartElement("http://www.gnumeric.org/v10.dtd",
428 "Cell");
429 xml_stream.writeAttribute("Col", QString::number(jcol));
430 xml_stream.writeAttribute("Row", QString::number(irow));
431 xml_stream.writeAttribute("ValueType", "60");
432 xml_stream.writeCharacters(_tcol[i].c_str());
433 xml_stream.writeEndElement();
434 //fichier << "<gmr:Cell Col=\"" << jcol << "\" Row=\"" << irow
435 // << "\" ValueType=\"60\">";
436 //fichier << _tcol[i];
437 //fichier << "</gmr:Cell>" << endl;
438 }
439 }
440 irow++;
441
442 //fichier.setf(ios::scientific);
443 //�criture de la matrice
444 for (i = 0; i < _nl; i++, irow++) {
445 if ((_flag == 1) || (_flag == 3)) {
446 //�tiquettes des lignes
447 xml_stream.writeStartElement("http://www.gnumeric.org/v10.dtd",
448 "Cell");
449 xml_stream.writeAttribute("Col", "0");
450 xml_stream.writeAttribute("Row", QString::number(irow));
451 xml_stream.writeAttribute("ValueType", "60");
452 xml_stream.writeCharacters(_tlig[i].c_str());
453 xml_stream.writeEndElement();
454 //fichier << "<gmr:Cell Col=\"0\" Row=\"" << irow
455 // << "\" ValueType=\"60\">";
456 //fichier << _tlig[i];
457 //fichier << "</gmr:Cell>" << endl;
458 }
459 jcol = 1;
460 for (j = 0; j < _nc; j++, jcol++) {
461 // fichier << '\t' << _tab [i * _nc + j];
462 xml_stream.writeStartElement("http://www.gnumeric.org/v10.dtd",
463 "Cell");
464 xml_stream.writeAttribute("Col", QString::number(jcol));
465 xml_stream.writeAttribute("Row", QString::number(irow));
466 xml_stream.writeAttribute("ValueType", "40");
467 xml_stream.writeAttribute("ValueFormat", value_format);
468 xml_stream.writeCharacters(QString::number((double) GetCase(i, j)));
469 xml_stream.writeEndElement();
470 //fichier << "<gmr:Cell Col=\"" << jcol << "\" Row=\"" << irow
471 // << "\" ValueType=\"40\" ValueFormat=\"" << value_format
472 // << "\">";
473 //fichier << GetCase(i, j);
474 //fichier << "</gmr:Cell>" << endl;
475 }
476 //fichier << endl;
477 }
478 //fichier << "</gmr:Cells>" << endl;
479 //fichier << " </gmr:Sheet> " << endl;
480 qDebug()
481 << "Matrice<T>::oGnumericSheet(QXmlStreamWriter& xml_stream, const QString & sheetname) end";
482
483 }
484
485 void Matrice::oNtsys(ostream& sortie) {
486 unsigned long i, j, k, tmaxligne(100);
487 long pos;
488
489 // sortie << setf(ios::fixed);
490
491 // �criture du titre
492 for (i = 0; i < _titre.size(); i++) {
493 sortie << "\"";
494 sortie << _titre[i];
495 sortie << endl;
496 }
497
498 sortie << _type << " " << _nl;
499 if ((_flag == 1) || (_flag == 3))
500 sortie << "L";
501
502 sortie << " " << _nc;
503 if ((_flag == 2) || (_flag == 3))
504 sortie << "L";
505
506 sortie << " " << _miss << endl;
507
508 //�criture des �tiquettes, lignes puis colonnes
509 if ((_flag == 1) || (_flag == 3)) {
510 //�tiquettes des lignes
511 pos = sortie.tellp();
512 sortie << _tlig[0];
513 for (i = 1; i < _nl; i++) {
514 sortie << " " << _tlig[i];
515 j = sortie.tellp();
516 if ((j - pos) > tmaxligne) {
517 sortie << endl;
518 pos = j;
519 }
520 }
521 sortie << endl;
522 }
523
524 if ((_flag == 2) || (_flag == 3)) {
525 //�tiquettes des colonnes
526 pos = sortie.tellp();
527 sortie << _tcol[0];
528 for (i = 1; i < _nc; i++) {
529 sortie << " " << _tcol[i];
530 j = sortie.tellp();
531 if ((j - pos) > tmaxligne) {
532 sortie << endl;
533 pos = j;
534 }
535 }
536 sortie << endl;
537 }
538
539 //�criture de la matrice
540 switch (_type) {
541 case 2:
542 // matrice triangulaire
543 // attention, ne sort que la partie triangle du bas
544 for (i = 0; i < _nc; i++) {
545 pos = sortie.tellp();
546 sortie << _tab[i * _nc];
547 for (j = 1; j <= i; j++) { //scan d'une moiti� (triangle)
548 //[ligne i] [colonne j] triangle du bas
549 sortie << " " << _tab[(i * _nc) + j];
550 k = sortie.tellp();
551 if ((k - pos) > tmaxligne) {
552 sortie << endl;
553 pos = k;
554 }
555
556 }
557 sortie << endl;
558 }
559 break;
560
561 case 1:
562 for (i = 0; i < _nl; i++) {
563
564 pos = sortie.tellp();
565 sortie << _tab[i * _nc];
566 for (j = 1; j < _nc; j++) {
567 sortie << " " << _tab[i * _nc + j];
568 k = sortie.tellp();
569 if ((k - pos) > tmaxligne) {
570 sortie << endl;
571 pos = k;
572 }
573 }
574 sortie << endl;
575 }
576 break;
577
578 default:
579 break;
580 }
581 }
582
583 long double Matrice::get_ppvaleur() const {
584 //retourne la plus petite valeur de la matrice
585 unsigned long taille(_nc * _nl);
586 unsigned long j;
587 long double ppvaleur(_tab[0]);
588
589 for (j = 1; j < taille; j++)
590 if (_tab[j] < ppvaleur)
591 ppvaleur = _tab[j];
592 return (ppvaleur);
593 }
594
595 void Matrice::f_neg2zero() {
596 //remet � z�ro les chiffres n�gatifs
597 unsigned long taille(_nc * _nl);
598 unsigned long j(_nc * _nl);
599
600 for (j = 0; j < taille; j++)
601 if (_tab[j] < 0)
602 _tab[j] = 0;
603 }
604
605 void Matrice::iGnumeric(istream& entree) {
606 //lecture de matrices contenues dans les fichiers Gnumeric XML
607 #ifdef XmlParse_INCLUDED
608 //entree.seekg(0);
609
610 //cerr << "void Matrice<long double>::iGnumeric(istream& entree) 1" << endl;
611
612 try {
613 olivxml::ImportGnumeric parser;
614 parser.iParseFlux(entree);
615 *this = parser.get_set_of_matrix().GetConstMatrice(0);
616 }
617 catch (olivxml::ImportGnumeric::Anomalie pb) {
618 throw Anomalie(1);
619 }
620 //cerr << "void Matrice<long double>::iGnumeric(istream& entree)" << endl;
621 //cerr << "void Matrice<long double>::iGnumeric(istream& entree) apres" << endl;
622 #else
623 throw Anomalie(1);
624 #endif
625 }
626
627 void Matrice::iNtsys(istream& entree) {
628 //lecture de matrices contenues dans les fichiers NTSYS
629 char car;
630 ChaineCar mot;
631 ChaineCar sousmot;
632 int titres; //1=> titres des lignes 2=> titres des colonnes
633 //3=> les 2 //0=> rien
634 unsigned long i, j, nc, nl;
635 int miss, type;
636
637 //cerr << "Matrice<T>::iNtsys(istream& entree)" << endl;
638 entree.seekg(0);
639
640 car = entree.peek();
641 // entree.get(car);
642 // while ((car != '"') && (entree.eof()==0)) entree.get(car);
643 while ((car == '"') && (entree.eof() == 0)) {
644 entree.get(car);
645 GetLigneFlot(entree, mot);
646 // entree.getline(mot,200,'\n');
647 _titre.push_back(mot);
648 // entree.get(car);
649 car = entree.peek();
650 }
651 //cerr << "Matrice<T>::iNtsys(istream& entree) 2" << endl;
652 if ((entree.eof() != 0) || (entree.fail()))
653 throw Anomalie(1);//erreur de lecture
654 //cerr << "Matrice<T>::iNtsys(istream& entree) 3" << endl;
655
656 // mot[0] = car;
657 // mot[1] = '\0';
658 // type = atoi(mot);
659
660 GetLigneFlot(entree, mot); //ligne 0 L23 L34 missing_value
661 if ((mot.GetNbMots() != 4) || (entree.fail()) || (entree.eof() != 0))
662 throw Anomalie(1);
663
664 mot.GetMot(1, sousmot);
665 type = atoi(sousmot.c_str());
666 if ((_type > 2) || (_type < 0))
667 throw Anomalie(1);
668
669 titres = 0;
670 //nombre de lignes
671 mot.GetMot(2, sousmot);
672 // entree >> mot;
673 if (sousmot[sousmot.size() - 1] == 'L') {
674 sousmot.Remplacer("L", "");
675 titres = 1;
676 // strcpy(mot + strlen(mot) - 1,"");
677 }
678 nl = atoi(sousmot.c_str());
679 if ((nl > 9999) || (nl < 1))
680 throw Anomalie(1);
681
682 //nombre de colonnes
683 mot.GetMot(3, sousmot);
684 if (sousmot[sousmot.size() - 1] == 'L') {
685 sousmot.Remplacer("L", "");
686 titres = titres + 2;
687 // strcpy(mot + strlen(mot) - 1,"");
688 }
689 nc = atoi(sousmot.c_str());
690 if ((nc > 9999) || (nc < 1))
691 throw Anomalie(1);
692
693 // entree >> mot; //missing value
694 mot.GetMot(4, sousmot);
695 miss = atoi(mot.c_str());
696
697 resize(nl, nc);
698 //cerr << "Matrice<T>::iNtsys(istream& entree) nc nl " << _nc << _nl << endl;
699
700 _miss = miss;
701 _type = type;
702
703 _flag = titres;
704 //cerr << "Matrice<T>::iNtsys(istream& entree) 2" << endl;
705 //lecture des �tiquettes, lignes puis colonnes
706 if ((titres == 1) || (titres == 3)) {
707 //�tiquettes des lignes
708 for (i = 0; i < _nl; i++) {
709 entree >> _tlig[i];
710 //cerr << "Matrice<T>::iNtsys(istream& entree) _tlig " << _tlig[i] << endl;
711 if (entree.fail())
712 throw Anomalie(1);//erreur de lecture
713 // _tlig[i].assign(mot);
714 }
715 }
716 //cerr << "Matrice<T>::iNtsys(istream& entree) 3" << endl;
717
718 if ((titres == 2) || (titres == 3)) {
719 //�tiquettes des colonnes
720 for (i = 0; i < _nc; i++) {
721 entree >> _tcol[i];
722 //cerr << "Matrice<T>::iNtsys(istream& entree) tcol " << _tcol[i] << endl;
723 if (entree.fail())
724 throw Anomalie(1);//erreur de lecture
725 }
726 }
727
728 //lecture de la matrice proprement dite
729 switch (_type) {
730 case 1:
731 // cas des matrices rectangulaires ou carr�es
732 _t = _nc * _nl;
733 // long double chiffre;
734
735 for (i = 0; i < _t; i++) {
736 entree >> _tab[i];
737 if (entree.fail())
738 throw Anomalie(1);//erreur de lecture
739 // _tab[i] = (T) chiffre;
740 // _tab[i] = atof (mot);
741 }
742 break;
743
744 case 2:
745 // cas des matrices triangulaires
746 if (_flag == 2)
747 _tlig = _tcol;
748 if (_flag == 1)
749 _tcol = _tlig;
750
751 entree.clear();
752
753 for (i = 0; i < _nc; i++) {
754 for (j = 0; j <= i; j++) {
755 // entree >> (long double) _tab[(i*_nc) + j];
756 entree >> _tab[(i * _nc) + j];
757 //cerr << "Matrice<T>::iNtsys(istream& entree) 4 " << _tab[(i*_nc) + j] << " " << i << " " << j << endl;
758 if (entree.fail())
759 throw Anomalie(1);//erreur de lecture
760 _tab[(j * _nc) + i] = _tab[(i * _nc) + j];
761 }
762 }
763 break;
764
765 default:
766 throw Anomalie(1);
767 break;
768 }
769
770 }
771
772 void Matrice::iPhylip(istream& entree) {
773 //lecture de matrices contenues dans des fichiers Phylip
774 ChaineCar mot;
775 ChaineCar sousmot;
776 unsigned long nbmot;
777 unsigned long i, j;
778 long nl(0), nc(0), top(0);
779
780 // entree.seekg(0);
781 entree.clear();
782
783 //cerr << "Matrice<T>::iPhylip(istream& entree)" << endl;
784 mot.GetLigneFlot(entree);
785
786 //cerr << "Matrice<T>::iPhylip(istream& entree) 2" << endl;
787 nbmot = mot.GetNbMots();
788 if ((nbmot < 1) || (nbmot > 2))
789 throw Anomalie(1);
790
791 mot.GetMot(1, sousmot);
792 if (sousmot.EstUnChiffre() == false)
793 throw Anomalie(1);
794 nl = atol(sousmot.c_str());
795
796 //cerr << "Matrice<T>::iPhylip(istream& entree) 3" << endl;
797 _flag = 0;
798 if (nbmot == 1) {
799 resize(nl, nl);
800 // _type = 3;
801 } else {
802 mot.GetMot(2, sousmot);
803 if (sousmot.EstUnChiffre() == false)
804 throw Anomalie(1);
805 nc = atol(sousmot.c_str());
806 resize(nl, nc);
807 }
808 //cerr << nl << " " << nc << endl;
809 top = entree.tellg();
810 // GetLigneFlot(entree, mot);
811 GetLigneFlot(entree, mot);
812 mot.GetMot(1, sousmot);
813 //cerr << "Matrice<T>::iPhylip(istream& entree) coucou 1 " << sousmot << endl;
814 if (sousmot.EstUnChiffre())
815 _flag = 0;
816 else { // titre de ligne
817 _flag = 1;
818 _tlig.resize(nl);
819 }
820 /* nbmot = mot.GetNbMots();
821 if (nbmot == (_nc + 1)) {
822 _flag = 1;
823 }
824 else if (nbmot == _nc) {
825 _flag = 0;
826 }
827 else throw Anomalie(1);
828 */
829 entree.seekg(top);
830
831 for (i = 0; i < _nl; i++) {
832 if ((_flag == 1) || (_flag == 3))
833 entree >> _tlig[i];
834 //cerr << "Matrice<T>::iPhylip(istream& entree) coucou 2 " << _tlig[i] << endl;
835 if (entree.fail())
836 throw Anomalie(1);//erreur de lecture
837 for (j = 0; j < _nc; j++) {
838 entree >> GetCase(i, j);
839 if (entree.fail())
840 throw Anomalie(1);//erreur de lecture
841 }
842 }
843 SetType(1);
844 SetType(2);
845
846 //cerr << "Matrice<T>::iPhylip(istream& entree) fin" << endl;
847
848 }
849
850 void Matrice::iExcel(istream& entree) {
851 //lecture de matrices contenues dans des fichiers Excel
852 // la matrice doit �tre indiqu�e par "/" ou "#" et commencer
853 // 1 case en dessous et � gauche.
854 char car;
855 qDebug() << "Matrice<T>::iExcel(istream& entree) begin";
856 ChaineCar mot;
857 // char mot[200];
858 // string ligne;
859 //1=> titres des lignes 2=> titres des colonnes
860 //3=> les 2 //0=> rien
861 unsigned long i, j;
862 long nc(0), nl(0);
863 long debut, top;
864
865 entree.seekg(0);
866 qDebug() << "Matrice<T>::iExcel(istream& entree) 1";
867
868 entree.get(car);
869 // while ((car != '"') && (entree.eof()==0)) entree.get(car);
870 while (((car != '/') && (car != '#')) && (entree.good())) {
871 // mot = car;
872 GetLigneFlot(entree, mot);
873 mot = car + mot;
874 _titre.push_back(mot);
875 entree.get(car);
876 }
877
878 qDebug() << "Matrice<T>::iExcel(istream& entree) 2";
879 //cerr << "coucou excel 2";
880 if (entree.fail())
881 throw Anomalie(1);//erreur de lecture
882 nc = 0;
883 nl = 0;
884 _flag = 0;
885 debut = entree.tellg();
886 GetLigneFlot(entree, mot);
887 if ((mot[0] > 4) && (mot[1] != '\t')) {
888 _flag = 2; //il ya des titres de colonnes
889 }
890 if (entree.eof() != 0)
891 throw Anomalie(1);//erreur de lecture
892 //cerr << "coucou " << mot << endl ;
893 qDebug() << "Matrice<T>::iExcel(istream& entree) 3 " << mot.c_str();
894
895 top = entree.tellg();
896 //cerr << top << endl;
897
898 GetLigneFlot(entree, mot);
899 for (i = 0; i < mot.size(); i++) {
900 if (mot[i] > 64) {
901 _flag++; //il ya les titres des lignes
902 //cerr << mot << endl;
903 break;
904 }
905 }
906 qDebug() << "Matrice<T>::iExcel(istream& entree) 4 ";
907
908 //determination du nombre de colonnes:
909 nc = 0;
910 entree.seekg(top);
911 entree.clear();
912 if ((_flag == 1) || (_flag == 3)) {
913 entree >> mot;
914 }
915 car = '3';
916 GetLigneFlot(entree, mot);
917 nc = mot.GetNbMots();
918 //cerr << " nc " << nc << endl;
919 qDebug() << "Matrice<T>::iExcel(istream& entree) 5 nc " << nc;
920
921 //cerr << "coucou " << mot << endl ;
922 // if (entree.eof() != 0) throw Anomalie(1);//erreur de lecture
923 //cerr << "coucou2 " << mot << endl ;
924 //determination du nombre de lignes:
925 nl = 1;
926 entree.seekg(top);
927 GetLigneFlot(entree, mot);
928 entree >> mot;
929 while ((mot.GetNbMots() > 0) && (entree.eof() == 0)) {
930 GetLigneFlot(entree, mot);
931 nl++;
932 entree >> mot;
933 //cerr << mot << endl;
934 //cin >> i;
935 }
936 entree.clear();
937
938 qDebug() << "Matrice<T>::iExcel(istream& entree) 6 nl " << nl << " nc "
939 << nc;
940 //cerr << nl << endl;
941 //cerr << nc << endl;
942
943 // on a le nb de colonnes, le nb de lignes, les �tiquettes,
944 // remplissons !!!
945 resize(nl, nc);
946 entree.clear();
947
948 if ((_flag == 2) || (_flag == 3)) { //saisie des titres des colonnes
949 entree.seekg(debut);
950 // entree >> _tcol[i];
951 for (i = 0; i < _nc; i++) {
952 entree >> _tcol[i];
953 //cerr << _tcol[i] << endl;
954 if (entree.fail())
955 throw Anomalie(1);//erreur de lecture
956 }
957 }
958 entree.clear();
959
960 qDebug() << "Matrice<T>::iExcel(istream& entree) 7 ";
961
962 // entree.seekg(top);
963 entree.seekg(debut);
964 GetLigneFlot(entree, mot);
965 //cerr << "coucou" << top << endl;
966 for (i = 0; i < _nl; i++) {
967 if ((_flag == 1) || (_flag == 3))
968 entree >> _tlig[i];
969 //cerr << _tlig[i] << endl;
970 for (j = 0; j < _nc; j++) {
971 entree >> _tab[(i * _nc) + j];
972 //cerr << _tab[(i * _nc) + j] << endl;
973 if (entree.fail())
974 throw Anomalie(1);//erreur de lecture
975 }
976 }
977 qDebug() << "Matrice<T>::iExcel(istream& entree) 8 ";
978
979 //try {
980 SetType(1);
981 SetType(2);
982 //} catch (biolib::vecteurs::Matrice<T>::Anomalie erreur) {
983 // cerr << erreur.fmessage(erreur.le_pb);
984 //SetType(1);
985 //}
986 }
987
988 void Matrice::resize(unsigned long nblig,
989 unsigned long nbcol) {
990 unsigned long i;
991
992 if (_tab != 0)
993 delete[] _tab;
994
995 if (_nl > nblig)
996 _tlig.erase(_tlig.begin() + _nl, _tlig.end());
997 if (_nc > nbcol)
998 _tcol.erase(_tcol.begin() + _nc, _tcol.end());
999
1000 _tcol.reserve(nbcol);
1001 _tlig.reserve(nblig);
1002
1003 for (i = _nc; i < nbcol; i++)
1004 _tcol.push_back("");
1005 for (i = _nl; i < nblig; i++)
1006 _tlig.push_back("");
1007
1008 _nc = nbcol;
1009 _nl = nblig;
1010 _t = _nc * _nl;
1011 _tab = new long double[_t];
1012 // _miss = 0;
1013 // _flag = 0;
1014
1015 for (i = 0; i < _t; i++)
1016 _tab[i] = 0;
1017
1018 }
1019
1020 bool Matrice::redim(unsigned long nblig,
1021 unsigned long nbcol) {
1022 unsigned long i, j, t;
1023 long double* Ptab;
1024
1025 if (_tab == 0)
1026 return (false);
1027 if ((_type == 3) && (nblig != nbcol))
1028 return (false);
1029
1030 if (_nl > nblig)
1031 _tlig.erase(_tlig.begin() + _nl, _tlig.end());
1032 if (_nc > nbcol)
1033 _tcol.erase(_tcol.begin() + _nc, _tcol.end());
1034
1035 _tcol.reserve(nbcol);
1036 _tlig.reserve(nblig);
1037 for (i = _nc; i < nbcol; i++)
1038 _tcol.push_back("");
1039 for (i = _nl; i < nblig; i++)
1040 _tlig.push_back("");
1041
1042 Ptab = _tab;
1043 t = nblig * nbcol;
1044 _tab = new long double[t];
1045
1046 for (i = 0; i < nblig; i++) {
1047 for (j = 0; j < nbcol; j++) {
1048 if ((j < _nc) && (i < _nl))
1049 *(_tab + (i * nbcol) + j) = *(Ptab + (i * _nc) + j);
1050 else
1051 *(_tab + (i * nbcol) + j) = 0;
1052 }
1053 }
1054
1055 delete[] Ptab;
1056 _nc = nbcol;
1057 _nl = nblig;
1058 _t = t;
1059 return (true);
1060 }
1061
1062 void Matrice::fslig(vector<long double>& somme) const {
1063
1064 int i, j;
1065
1066 if (_type == 3)
1067 throw Anomalie(3); //op�ration impossible sur ce type de matrice
1068
1069 if (somme.size() < _nl)
1070 somme.resize(_nl);
1071
1072 for (i = 0; i < _nl; i++) {
1073 somme[i] = _tab[i * _nc];
1074 }
1075 for (j = 1; j < _nc; j++) {
1076 for (i = 0; i < _nl; i++) {
1077 somme[i] += _tab[j + (i * _nc)];
1078 }
1079 }
1080 }
1081
1082 void Matrice::fscol(vector<long double>& somme) const {
1083
1084 int pos, i, j;
1085
1086 if (_type == 3)
1087 throw Anomalie(3); //op�ration impossible sur ce type de matrice
1088
1089 if (somme.size() < _nc)
1090 somme.resize(_nc);
1091
1092 for (i = 0; i < _nc; i++) {
1093 somme[i] = _tab[i];
1094 }
1095 pos = _nc;
1096 for (j = 1; j < _nl; j++) {
1097 for (i = 0; i < _nc; i++) {
1098 somme[i] += _tab[pos];
1099 pos++;
1100 }
1101 }
1102 }
1103
1104 void Matrice::ftranspose() {
1105 //transposition de matrices:
1106
1107 Titre swap;
1108 unsigned long i, j;
1109
1110 swap = _tcol;
1111 _tcol = _tlig;
1112 _tlig = swap;
1113
1114 if (_type == 3)
1115 return;
1116
1117 //_flag = 2;
1118 if (_flag == 1)
1119 _flag = 2;
1120 else if (_flag == 2)
1121 _flag = 1;
1122
1123 long double* Ptab;
1124 Ptab = new long double[_t];
1125
1126 for (i = 0; i < _nl; i++) {
1127 for (j = 0; j < _nc; j++) {
1128 *(Ptab + (j * _nl) + i) = *(_tab + rindice(i, j));
1129 }
1130 }
1131
1132 i = _nc;
1133 _nc = _nl;
1134 _nl = i;
1135
1136 delete[] _tab;
1137 _tab = Ptab;
1138 }
1139
1140 Matrice Matrice::operator*(Matrice &rval) const {
1141
1142 //produit matriciel
1143 Matrice Resultat(_nl, rval._nc);
1144 // long double limite(1e-030);
1145
1146 if (_nc != rval._nl)
1147 throw Anomalie(4); // 4-> calcul impossible
1148 // if (_nl != rval._nc) throw Anomalie(4); // 4-> calcul impossible
1149
1150 int i, j, k; //ligne, colonne
1151
1152
1153 Resultat.SetFlag(0);
1154 if ((rval.GetFlag() == 2) || (rval.GetFlag() == 3)) {
1155 Resultat._tcol = rval._tcol;
1156 Resultat.SetFlag(2);
1157 }
1158 if ((_flag == 1) || (_flag == 3)) {
1159 Resultat._tlig = _tlig;
1160 Resultat._flag++;
1161 }
1162
1163 for (i = 0; i < _nl; i++) {
1164 for (j = 0; j < rval._nc; j++) {
1165 Resultat.GetCase(i, j) = 0;
1166 for (k = 0; k < _nc; k++) {
1167 Resultat.GetCase(i, j) = Resultat.GetCase(i, j)
1168 + ((long double) GetCase(i, k) * rval.GetCase(k, j));
1169 }
1170 }
1171 }
1172
1173 //v�rification limite
1174 /* for (i=0; i < _t; i++) {
1175 if (Resultat._tab[i] < limite) Resultat._tab[i] = 0;
1176 }*/
1177
1178 return Resultat;
1179 }
1180
1181 vector<long double> Matrice::operator*(const vector<long double> &rval) {
1182 //produit d'une matrice par un vecteur
1183
1184 vector<long double> Resultat;
1185 Matrice& lamatrice = *this;
1186 long i, k; //ligne, colonne
1187
1188 Resultat.resize(lamatrice._nl);
1189
1190 if (lamatrice._nl != rval.size())
1191 throw Anomalie(4); // 4-> calcul impossible
1192
1193
1194 for (i = 0; i < Resultat.size(); i++) {
1195 Resultat[i] = 0;
1196 for (k = 0; k < lamatrice._nc; k++) {
1197 Resultat[i] = Resultat[i] + (lamatrice.GetCase(i, k) * rval[i]);
1198 }
1199 }
1200
1201 return Resultat;
1202 }
1203
1204 Matrice Matrice::operator*(long double scalaire) {
1205
1206 Matrice Resultat(_nl, _nc);
1207 long i;
1208
1209 // Resultat.resize(_nl,_nc);
1210 // cout << "coucou";
1211 Resultat.SetType(1);
1212 Resultat._titre = _titre;
1213 // Resultat._titre.push_back("Produit par un scalaire");
1214 Resultat._flag = 0;
1215
1216 if ((_flag == 2) || (_flag == 3)) {
1217 //copie des �tiquettes des colonnes
1218 Resultat._flag = 2;
1219 Resultat._tcol = _tcol;
1220 Resultat._tlig = _tcol;
1221 }
1222
1223 for (i = 0; i < _t; i++) {
1224 Resultat._tab[i] = _tab[i] * scalaire;
1225 }
1226
1227 return Resultat;
1228 }
1229
1230 Matrice Matrice::operator-(Matrice& rval) {
1231
1232 Matrice Resultat(_nl, _nc);
1233 long i;
1234
1235 if (_nl != rval._nl)
1236 throw Anomalie(4);
1237 if (_nc != rval._nc)
1238 throw Anomalie(4);
1239
1240 // cout << "coucou";
1241 Resultat.SetType(_type);
1242 Resultat._titre = _titre;
1243 // Resultat._titre.push_back("Produit par un scalaire");
1244 Resultat._flag = _flag;
1245
1246 if ((_flag == 2) || (_flag == 3)) {
1247 //copie des �tiquettes des colonnes
1248 Resultat._tcol = _tcol;
1249 }
1250 if ((_flag == 1) || (_flag == 3)) {
1251 //copie des �tiquettes des lignes
1252 Resultat._tlig = _tlig;
1253 }
1254
1255 for (i = 0; i < _t; i++) {
1256 Resultat._tab[i] = _tab[i] - rval._tab[i];
1257 }
1258
1259 return Resultat;
1260
1261 }
1262
1263 Matrice Matrice::operator+(Matrice &rval) {
1264 long i;
1265
1266 Matrice Resultat;
1267
1268 if (_nl != rval._nl)
1269 throw Anomalie(4);
1270 if (_nc != rval._nc)
1271 throw Anomalie(4);
1272
1273 Resultat.resize(_nl, _nc);
1274 // cout << "coucou";
1275 Resultat.SetType(_type);
1276 Resultat._titre = _titre;
1277 // Resultat._titre.push_back("Produit par un scalaire");
1278 Resultat._flag = _flag;
1279
1280 if ((_flag == 2) || (_flag == 3)) {
1281 //copie des �tiquettes des colonnes
1282 Resultat._tcol = _tcol;
1283 }
1284 if ((_flag == 1) || (_flag == 3)) {
1285 //copie des �tiquettes des lignes
1286 Resultat._tlig = _tlig;
1287 }
1288
1289 for (i = 0; i < _t; i++) {
1290 Resultat._tab[i] = _tab[i] + rval._tab[i];
1291 }
1292
1293 return Resultat;
1294
1295 }
1296
1297 long double Matrice::fdet() const {
1298 //calcul du d�terminant d'une matrice par r�cursivit
1299 // Olivier Langella 29 oct 98
1300 // D'apr�s J. Lefebvre "introduction aux analyses statistiques
1301 // multidimensionnelle" p 36
1302 long double det;
1303 if (_nc != _nl)
1304 throw Anomalie(4); //calcul impossible
1305
1306 MatriceLD calculs;
1307
1308 calculs = *this;
1309
1310 det = calculs.fdetrec();
1311
1312 return (det);
1313 }
1314
1315 long double Matrice::fdetrec() const {
1316 //calcul du d�terminant d'une matrice par r�cursivit
1317 // utilis� par fdet;
1318 long double det(0);
1319 unsigned long i;
1320 long cofacteur(1);
1321 Matrice calculs;
1322
1323 if (_nc == 2) {
1324 //solution
1325 det = ((long double) GetCase(0, 0) * GetCase(1, 1))
1326 - ((long double) GetCase(1, 0) * GetCase(0, 1));
1327 return (det);
1328 } else {
1329
1330 for (i = 0; i < _nl; i++) {
1331 calculs = *this;
1332 calculs.SupprCol(0);
1333 calculs.SupprLig(i);
1334
1335 det = det + ((long double) calculs.fdetrec()
1336 * ((long double) GetCase(i, 0) * cofacteur));
1337 cofacteur = cofacteur * -1;
1338 }
1339 return (det);
1340 }
1341 }
1342
1343 void Matrice::SupprCol(unsigned long nbcol) {
1344 unsigned long i, j, k, n;
1345 long double * Ptab;
1346
1347 // if (_nl > nblig) _tlig.erase(_tlig.begin() + _nl,_tlig.end());
1348 if (nbcol < _tcol.size())
1349 _tcol.erase(_tcol.begin() + nbcol);
1350
1351 n = _nc - 1;
1352
1353 Ptab = new long double[n * _nl];
1354 k = 0;
1355
1356 for (i = 0; i < _nl; i++) {
1357 for (j = 0; j < _nc; j++) {
1358 if (j != nbcol) {
1359 Ptab[k] = GetCase(i, j);
1360 k++;
1361 }
1362 }
1363 }
1364
1365 _nc = _nc - 1;
1366 _t = _nc * _nl;
1367
1368 if (_tab != 0)
1369 delete[] _tab;
1370 _tab = Ptab;
1371 _type = 1;
1372
1373 }
1374
1375 void Matrice::SupprLig(unsigned long nblig) {
1376 unsigned long i, j, k, n;
1377 long double* Ptab;
1378
1379 // if (_nl > nblig) _tlig.erase(_tlig.begin() + _nl,_tlig.end());
1380 if (nblig < _tlig.size())
1381 _tlig.erase(_tlig.begin() + nblig);
1382
1383 n = _nl - 1;
1384
1385 Ptab = new long double[n * _nc];
1386 k = 0;
1387
1388 for (i = 0; i < _nl; i++) {
1389 for (j = 0; j < _nc; j++) {
1390 if (i != nblig) {
1391 Ptab[k] = GetCase(i, j);
1392 k++;
1393 }
1394 }
1395 }
1396
1397 _nl = _nl - 1;
1398 _t = _nc * _nl;
1399
1400 if (_tab != 0)
1401 delete[] _tab;
1402 _tab = Ptab;
1403 _type = 1;
1404
1405 }
1406
1407 void MatriceLD::finv(MatriceLD& inverse) const {
1408 //calcul de l'inverse d'une matrice
1409 // Olivier Langella 29 oct 98
1410 // D'apr�s J. Lefebvre "introduction aux analyses statistiques
1411 // multidimensionnelle" p 42
1412 if (_nc != _nl)
1413 throw Anomalie(4); //calcul impossible
1414
1415 long double detA;
1416 MatriceLD calculs;
1417 int cofacteur, cofl(-1);
1418 unsigned long i, j;
1419
1420 inverse = *this;
1421
1422 detA = fdet();
1423
1424 for (i = 0; i < _nl; i++) {
1425 cofacteur = cofl;
1426 for (j = 0; j < _nc; j++) {
1427 cofacteur *= -1;
1428 calculs = *this;
1429 calculs.SupprCol(i);
1430 calculs.SupprLig(j);
1431
1432 inverse.GetCase(i, j) = (calculs.fdet() * cofacteur) / detA;
1433 }
1434
1435 cofl *= -1;
1436 }
1437
1438 }
1439
1440 void MatriceLD::fvalpropres(MatriceLD& vectpropres,
1441 vector<float>& valpropres, float precision) {
1442
1443 //Calcul des valeurs propres pour une matrice sym�trique
1444 // M�thode de Jacobi
1445 // Olivier Langella 29 oct 98
1446 // D'apr�s J. Lefebvre "introduction aux analyses statistiques
1447 // multidimensionnelle" p 67
1448 // (m�thode it�rative)
1449
1450 if (GetType() != 3)
1451 throw Anomalie(3); //calcul impossible
1452 //ATTENTION, ne v�rifie pas si la matrice est bien symetrique !!
1453
1454 valpropres.resize(_nc);
1455 vectpropres.resize(_nc, _nc);
1456 vectpropres.SetType(1);
1457 MatriceLD matC;
1458 MatriceLD matA;
1459 matA = *this;
1460
1461 matA._titre.push_back("Matrice A");
1462 matC._titre.push_back("Matrice C");
1463 vectpropres = vectpropres + 1;
1464
1465 //it�rations de C'AC
1466 unsigned long poss, posr;
1467 long double temp, teta;
1468 unsigned long i, j, nc(_nc);
1469 long ind1, ind2;
1470
1471 long double scarre(0), tracecar;
1472
1473 for (i = 0; i < _t; i++) {
1474 scarre = scarre + (_tab[i] * _tab[i]);
1475 }
1476
1477 matC.resize(nc, nc);
1478 MatriceLD matInter(nc, nc); //matrice pour les calculs
1479 //intermediaires
1480 do {
1481 // choix du point � annuler
1482 // : le plus grand non situ� sur la diagonale
1483 posr = 0;
1484 poss = 0;
1485 temp = -1;
1486 for (i = 1; i < nc; i++) {
1487 for (j = 0; j < i; j++) {
1488 teta = fabs(matA._tab[(i * nc) + j]);
1489 if (temp < teta) {
1490 temp = teta;
1491 poss = i;
1492 posr = j;
1493 }
1494 }
1495 }
1496
1497 // calcul de teta
1498 teta = (matA._tab[(posr * nc) + poss] * 2) / (matA._tab[(posr * nc)
1499 + posr] - matA._tab[(poss * nc) + poss]);
1500 teta = atan(teta) / 2;
1501
1502 // fabriquer la matrice C'
1503 for (i = 0; i < nc; i++) {
1504 matC._tab[(i * nc) + i] = 1;
1505 if ((i == poss) || (i == posr)) {
1506 matC._tab[(i * nc) + i] = cos(teta);
1507 }
1508 for (j = 0; j < i; j++) {
1509 ind1 = (j * nc) + i;
1510 ind2 = (i * nc) + j;
1511 if ((i == poss) && (j == posr)) {
1512 matC._tab[ind1] = sin(teta);
1513 matC._tab[ind2] = matC._tab[ind1] * -1;
1514 } else {
1515 matC._tab[ind1] = 0;
1516 matC._tab[ind2] = 0;
1517 }
1518 }
1519 }
1520
1521 // matA = matC * matA;
1522 matInter.fmultiplier(matC, matA); // ajoute pour optimiser
1523 matC.ftranspose();
1524 // matA = matA * matC;
1525 matA.fmultiplier(matInter, matC); // ajoute pour optimiser
1526
1527 // vectpropres = vectpropres * matC;
1528 for (i = 0; i < _t; i++) {
1529 matInter._tab[i] = vectpropres._tab[i];
1530 }
1531 vectpropres.fmultiplier(matInter, matC);
1532
1533 tracecar = 0;
1534 for (i = 0; i < nc; i++) {
1535 teta = matA._tab[(i * nc) + i];
1536 tracecar = tracecar + (teta * teta);
1537 }
1538 } while (((scarre - tracecar) / tracecar) > precision);
1539
1540 for (i = 0; i < _nc; i++) {
1541 valpropres[i] = matA.GetCase(i, i);
1542 }
1543
1544 }
1545
1546 long double Matrice::ftrace() const {
1547 //calcul de la trace d'une matrice (somme des E diagonaux)
1548 // Olivier Langella 29 oct 98
1549 if (_nc != _nl)
1550 throw Anomalie(4); //calcul impossible
1551
1552 int i, j(0);
1553
1554 long double res(0);
1555
1556 for (i = 0; i < _nc; i++) {
1557 res += _tab[i + j];
1558 j += _nc;
1559 }
1560
1561 return res;
1562 }
1563
1564 Matrice Matrice::operator+(long double rval) {
1565
1566 Matrice Resultat;
1567
1568 Resultat = *this;
1569 unsigned long i;
1570
1571 for (i = 0; i < _nc; i++) {
1572 Resultat.GetCase(i, i) = Resultat.GetCase(i, i) + rval;
1573 }
1574
1575 return Resultat;
1576
1577 }
1578
1579 void Matrice::fswapcol(long i, long j) {
1580 long double swap;
1581 long k;
1582
1583 for (k = 0; k < _nc; k++) {
1584 swap = GetCase(k, i);
1585 GetCase(k, i) = GetCase(k, j);
1586 GetCase(k, j) = swap;
1587 }
1588 }
1589
1590 void Matrice::fmultiplier(const Matrice& matA,
1591 const Matrice& matB) {
1592 //pour multiplier 2 matrices plus rapidement
1593 // A et B sont carrees !!!!
1594 long i, j, k, nc(_nc);
1595 long double res;
1596
1597 for (i = 0; i < nc; i++) {
1598 for (j = 0; j < nc; j++) {
1599 res = 0;
1600 for (k = 0; k < nc; k++) {
1601 res += matA._tab[(i * nc) + k] * matB._tab[(k * nc) + j];
1602 }
1603 _tab[(i * nc) + j] = res;
1604 }
1605 }
1606 }
1607
1608 void Matrice::oXgobi(const string &fnom) {
1609 unsigned long i, j;
1610 ofstream fichier;
1611 // string fnom(nomfichier);
1612 string fnomplus(fnom);
1613
1614 if (_type != 1)
1615 throw Anomalie(3);
1616
1617 fnomplus = fnom + ".dat";
1618 fichier.open(fnomplus.c_str(), ios::out);
1619 if (fichier.is_open() == false)
1620 throw Anomalie(6);
1621
1622 for (i = 0; i < _nl; i++) {
1623 fichier << _tab[i * _nc];
1624 for (j = 1; j < _nc; j++) {
1625 fichier << " " << _tab[(i * _nc) + j];
1626 }
1627 fichier << endl;
1628 }
1629
1630 fichier.close();
1631
1632 if (_titre.size() > 0) {
1633 fnomplus = fnom + ".doc";
1634 fichier.open(fnomplus.c_str(), ios::out);
1635 if (fichier.is_open() == false)
1636 throw Anomalie(6);
1637
1638 for (i = 0; i < _titre.size(); i++) {
1639 fichier << _titre[i];
1640 fichier << endl;
1641 }
1642 fichier.close();
1643 }
1644
1645 if ((_flag == 1) || (_flag == 3)) {
1646 fnomplus = fnom + ".row";
1647 fichier.open(fnomplus.c_str(), ios::out);
1648 if (fichier.is_open() == false)
1649 throw Anomalie(6);
1650
1651 for (i = 0; i < _tlig.size(); i++) {
1652 fichier << _tlig[i];
1653 fichier << endl;
1654 }
1655
1656 fichier.close();
1657 }
1658
1659 if ((_flag == 2) || (_flag == 3)) {
1660 fnomplus = fnom + ".col";
1661 fichier.open(fnomplus.c_str(), ios::out);
1662 if (fichier.is_open() == false)
1663 throw Anomalie(6);
1664
1665 for (i = 0; i < _tcol.size(); i++) {
1666 fichier << _tcol[i];
1667 fichier << endl;
1668 }
1669
1670 fichier.close();
1671 }
1672
1673 }
1674
1675 ////////////////////////////////////////////////
1676 // GLOBAL
1677 ///////////////////////////////////////////////
1678 ostream& operator<<(ostream& sortie, Matrice& lamatrice) {
1679 //1= Excel
1680 //2= Ntsys
1681 //3= xgobi ==!!!! seulement en ofstream !!!!!
1682
1683 lamatrice.oFormat(sortie, lamatrice._oformat);
1684 return (sortie);
1685
1686 }
1687
1688 ofstream& operator<<(ofstream& sortie, Matrice& lamatrice) {
1689 //1= Excel
1690 //2= Ntsys
1691 //3= xgobi ==!!!! seulement en ofstream !!!!!
1692
1693 if (lamatrice._oformat == 3) {
1694 lamatrice.oXgobi("xgobi");
1695 } else {
1696 lamatrice.oFormat(sortie, lamatrice._oformat);
1697 }
1698 return (sortie);
1699
1700 }
1701
1702 /*ofstream& operator<<(ofstream& sortie,MatriceLD& lamatrice) {
1703 //1= Excel
1704 //2= Ntsys
1705 //3= xgobi ==!!!! seulement en ofstream !!!!!
1706
1707 if (lamatrice._oformat == 3) {
1708 lamatrice.oXgobi("xgobi");
1709 }
1710 else {
1711 lamatrice.oFormat(sortie, lamatrice._oformat);
1712 }
1713 return(sortie);
1714
1715 }*/
1716
1717 istream& operator>>(istream& ientree, Matrice& lamatrice) {
1718
1719 //cerr << "coucou";
1720 lamatrice.iFichier(ientree);
1721 //cerr << "coucou";
1722 return (ientree);
1723 }
1724
1725 void Matrice::oPhylip(ostream& fichier) {
1726 unsigned long i, j;
1727
1728 fichier.setf(ios::scientific);
1729
1730 /* // �criture du titre
1731 for (i=0; i < _titre.size();i++) {
1732 fichier << _titre[i];
1733 fichier << endl;
1734 }*/
1735 if (_type == 3) { //matrice triangulaire
1736 fichier << '\t' << GetNL() << endl;
1737 _tlig = _tcol;
1738 } else {
1739 fichier << '\t' << GetNL() << '\t' << GetNC() << endl;
1740 }
1741
1742 // fichier << "#";
1743 /* if ((_flag == 2)||(_flag == 3)){
1744 //�tiquettes des colonnes
1745 fichier << '\t' << _tcol[0];
1746 for (i=1; i<_nc; i++) fichier << '\t' << _tcol[i];
1747 }
1748 fichier << endl;
1749 */
1750 //�criture de la matrice
1751 for (i = 0; i < _nl; i++) {
1752 if ((_flag == 1) || (_flag == 3)) {
1753 //�tiquettes des lignes
1754 fichier << _tlig[i];
1755 }
1756
1757 for (j = 0; j < _nc; j++) {
1758 fichier << '\t' << GetCase(i, j);
1759 }
1760 fichier << endl;
1761 }
1762
1763 }
1764
1765 void Matrice::iFichier(istream& ientree) {
1766 int nbformat(6), i(1);
1767 char car;
1768 bool autre(true);
1769
1770 // entree = ientree;
1771 qDebug() << "Matrice<T>::iFichier(istream& ientree) begin nbformat "
1772 << nbformat;
1773 ientree.get(car);
1774 if (ientree.fail()) {
1775 // cerr << "Le fichier n'a pas pu �tre ouvert... "<< endl;
1776 throw Anomalie(1);
1777 }
1778 while ((autre) & (i < nbformat)) {
1779 qDebug() << "Matrice<T>::iFichier(istream& ientree) ifichier " << i
1780 << " autre " << autre;
1781 //cerr << "coucou ifichier" << i << endl;
1782 if (i == 3) {
1783 i = 4;
1784 continue;
1785 } //xgobi
1786 try {
1787 ientree.clear();
1788 ientree.seekg(0);
1789 //ientree.seekg (0, ios::beg);
1790 // ientree.rewind();
1791 _titre.resize(0);
1792 //cerr << "coucou ifichier" << i << endl;
1793 iFichier(ientree, i);
1794 //cerr << "coucou ifichier" << endl;
1795
1796 autre = false;
1797 qDebug() << "Matrice<T>::iFichier(istream& ientree) 1 autre "
1798 << autre;
1799 } catch (Anomalie erreur) {
1800 if (erreur.le_pb > 0) {
1801 autre = true;
1802 if (i == (nbformat - 1)) {
1803 // cerr << erreur.fmessage(erreur.le_pb)<< endl;
1804 throw Anomalie(erreur.le_pb);
1805 autre = false;
1806 }
1807 erreur.le_pb = 0;
1808 }
1809 }
1810 qDebug() << "Matrice<T>::iFichier(istream& ientree) 2 autre " << autre;
1811 i++;
1812 }
1813 qDebug() << "Matrice<T>::iFichier(istream& ientree) end";
1814 }
1815
1816 MatriceLD::MatriceLD() :
1817 Matrice () {
1818 }
1819 ;
1820
1821 void MatriceLD::fdiagonalisation(MatriceLD & matdiag, vector<long double>& pvlp) {
1822 /******************************************************************************/
1823 /* DIAGONALISATION DE LA MATRICE (Householder) */
1824 /* 1) tridiagonalisation (tridiag) */
1825 /* 2) diagonalisation par la methode Q-L (diaggl) */
1826 /******************************************************************************/
1827 int m;
1828 unsigned long i;
1829 long double *ps;
1830
1831 if (_type != 2)
1832 throw Anomalie(3);
1833 // 3-> op�ration impossible sur ce type de matrice
1834 pvlp.resize(_nc);
1835
1836 matdiag.resize(_nc, _nc);
1837 matdiag.SetType(2);
1838
1839 ps = new long double[_nc];
1840
1841 matdiag._titre = _titre;
1842 matdiag._titre.push_back("diagonalisation par la methode Q-L");
1843 matdiag._flag = _flag;
1844
1845 if ((_flag == 2) || (_flag == 3)) {
1846 matdiag._tcol = _tcol;
1847 matdiag._tlig = _tcol;
1848 }
1849
1850 //copie du tableau
1851 for (i = 0; i < _t; i++) {
1852 matdiag._tab[i] = _tab[i];
1853 }
1854
1855 m = matdiag.ftridiag(ps, pvlp);
1856
1857 matdiag.fmdiagql(m, ps, pvlp);
1858
1859 delete[] ps;
1860
1861 }
1862
1863 /*====================== methode Q-L =====================================*/
1864
1865 void MatriceLD::fmdiagql(int m, long double *ps, vector<long double> & pvlp) {
1866 long double *pcov;
1867 unsigned long i, ij, j, k, l;
1868 long ijk, is;
1869 long double q, h, t, xp, u, b, a, v;
1870 long double p1;
1871
1872 if (_type != 2)
1873 throw Anomalie(3);
1874 // 3-> op�ration impossible sur ce type de matrice
1875 pcov = _tab;
1876 a = (long double) 1e-10;
1877
1878 for (i = 1; i < _nc; i++) {
1879 *(ps + i - 1) = *(ps + i);
1880 }
1881 *(ps + _nc - 1) = 0;
1882
1883 for (k = 0; k < _nc; k++) {
1884 j = k;
1885 while ((long double) (((long double) j < (long double) _nc - 1)
1886 && fabs((long double) *(ps + j))) >= (long double) (a * (fabs(
1887 (long double) pvlp[j]) + fabs((long double) pvlp[j + 1]))))
1888 j++;
1889 while (j != k) {
1890 h = pvlp[k];
1891 m = m + 1;
1892 q = (pvlp[k + 1] - h) * 0.5 / (*(ps + k));
1893 t = sqrt((long double) q * q + 1);
1894 if (q < 0)
1895 is = -1;
1896 else
1897 is = 1;
1898 q = pvlp[j] - h + (*(ps + k)) / (q + t * is);
1899 u = 1;
1900 v = 1;
1901 h = 0;
1902 for (ijk = 1; ijk <= (long) (j - k); ijk++) {
1903 i = j - ijk;
1904 xp = u * (*(ps + i));
1905 b = v * (*(ps + i));
1906 if (q == 0 || xp == 0) {
1907 // printf("cette matrice ne peut etre diagonalisee\n");
1908 // erreur
1909 // return();
1910 delete[] ps;
1911 throw Anomalie(2);
1912 }
1913 if (fabs((long double) xp) >= fabs((long double) q)) {
1914 u = xp / q;
1915 t = sqrt((long double) u * u + 1);
1916 *(ps + i + 1) = q * t;
1917 v = 1 / t;
1918 u = u * v;
1919 } else {
1920 v = q / xp;
1921 t = sqrt((long double) 1 + v * v);
1922 *(ps + i + 1) = t * xp;
1923 u = 1 / t;
1924 v = v * u;
1925 }
1926 q = pvlp[i + 1] - h;
1927 t = ((pvlp[i]) - q) * u + 2 * v * b;
1928 h = u * t;
1929 pvlp[i + 1] = q + h;
1930 q = v * t - b;
1931 l = 0;
1932 while (l < _nc) {
1933 xp = *(pcov + rindice(l, i + 1));
1934 *(pcov + rindice(l, i + 1)) = u * (*(pcov + rindice(l, i)))
1935 + v * xp;
1936 *(pcov + rindice(l, i)) = v * (*(pcov + rindice(l, i))) - u
1937 * xp;
1938 l = l + 1;
1939 }
1940 }
1941 pvlp[k] = pvlp[k] - h;
1942 *(ps + k) = q;
1943 *(ps + j) = 0;
1944 j = k;
1945 while (j < _nc - 1 && fabs((long double) *(ps + j)) >= a * (fabs(
1946 (long double) pvlp[j]) + fabs((long double) pvlp[j + 1])))
1947 j++;
1948
1949 }
1950 }
1951 for (ij = 1; ij < _nc; ij++) {
1952 i = ij - 1;
1953 l = i;
1954 h = pvlp[i];
1955 for (m = ij; m < (int) _nc; m++) {
1956 if (pvlp[m] >= h) {
1957 l = m;
1958 h = pvlp[m];
1959 }
1960 }
1961 if (l != i) {
1962 pvlp[l] = pvlp[i];
1963 pvlp[i] = h;
1964 for (m = 0; m < (int) _nc; m++) {
1965 p1 = *(pcov + rindice(m, i));
1966 *(pcov + rindice(m, i)) = *(pcov + rindice(m, l));
1967 *(pcov + rindice(m, l)) = p1;
1968 }
1969 }
1970 }
1971 }
1972
1973 int MatriceLD::ftridiag(long double *ps, vector<long double> & pvlp) {
1974 long double *pcov;
1975 unsigned long i, j;
1976 int k, l, m(0);
1977 long double b, c, q, x, xp, bp;
1978
1979 pcov = _tab;
1980
1981 for (j = 1; j < _nc; j++) {
1982 b = 0;
1983 c = 0;
1984 i = (_nc - 1) - j + 1;
1985 k = i - 1;
1986
1987 if (k < 1) {
1988 *(ps + i) = *(pcov + rindice(i, k));
1989 pvlp[i] = b;
1990 continue;
1991 }
1992 for (l = 0; l <= k; l++) {
1993 c = c + fabs((long double) *(pcov + rindice(i, l)));
1994 }
1995 if (c == 0) {
1996 *(ps + i) = *(pcov + rindice(i, k));
1997 pvlp[i] = b;
1998 continue;
1999 }
2000 for (l = 0; l <= k; l++) {
2001 x = (*(pcov + rindice(i, l))) / c;
2002 *(pcov + rindice(i, l)) = x;
2003 b = b + (x * x);
2004 }
2005 xp = *(pcov + rindice(i, k));
2006 if (xp < 0) {
2007 q = sqrt((long double) b);
2008 } else {
2009 q = (sqrt((long double) b)) * (-1);
2010 }
2011 *(ps + i) = c * q;
2012 b = b - xp * q;
2013 *(pcov + rindice(i, k)) = xp - q;
2014 xp = 0;
2015 for (m = 0; m <= k; m++) {
2016 *(pcov + rindice(m, i)) = (*(pcov + rindice(i, m))) / (b * c);
2017 q = 0;
2018 for (l = 0; l <= m; l++) {
2019 q = q + (*(pcov + rindice(m, l))) * (*(pcov + rindice(i, l)));
2020 }
2021 for (l = m + 1; l <= k; l++) {
2022 q = q + ((*(pcov + rindice(l, m))) * (*(pcov + rindice(i, l))));
2023
2024 }
2025 *(ps + m) = q / b;
2026 xp = xp + (*(ps + m)) * (*(pcov + rindice(i, m)));
2027 }
2028 bp = xp * 0.5 / b;
2029 for (m = 0; m <= k; m++) {
2030 xp = *(pcov + rindice(i, m));
2031 q = (*(ps + m)) - bp * xp;
2032 *(ps + m) = q;
2033 for (l = 0; l <= m; l++) {
2034 *(pcov + rindice(m, l)) = (*(pcov + rindice(m, l))) - xp
2035 * (*(ps + l)) - q * (*(pcov + rindice(i, l)));
2036 }
2037 }
2038 for (l = 0; l <= k; l++) {
2039 *(pcov + rindice(i, l)) = c * (*(pcov + rindice(i, l)));
2040 }
2041 pvlp[i] = b;
2042 }
2043 *ps = 0;
2044 pvlp[0] = 0;
2045 for (i = 0; i < _nc; i++) {
2046 k = i - 1;
2047 if (pvlp[i] != 0) {
2048 for (m = 0; m <= k; m++) {
2049 q = 0;
2050 for (l = 0; l <= k; l++) {
2051 q = q + (*(pcov + rindice(i, l)))
2052 * (*(pcov + rindice(l, m)));
2053 }
2054 for (l = 0; l <= k; l++) {
2055 *(pcov + rindice(l, m)) = (*(pcov + rindice(l, m))) - q
2056 * (*(pcov + rindice(l, i)));
2057 }
2058 }
2059 }
2060 pvlp[i] = *(pcov + rindice(i, i));
2061 *(pcov + rindice(i, i)) = 1;
2062 for (m = 0; m <= k; m++) {
2063 *(pcov + rindice(i, m)) = 0;
2064 *(pcov + rindice(m, i)) = 0;
2065 }
2066 }
2067 return (m);
2068 }
2069
2070 void MatriceLD::fcoordcol(MatriceLD& stat, MatriceLD& res, MatriceLD& norm,
2071 vector<long double> & pvlp) {
2072 /******************************************************************************/
2073 /* calcul des coordonnees */
2074 /******************************************************************************/
2075 /*=================== coordonnees des colonnes =============================*/
2076 unsigned long p0; //nb d'axes
2077 unsigned long i, j;
2078 int PC, k;
2079 long double tr; //somme des valeurs propres
2080 long double limite; //limite des possibilit�s de calculs
2081 float t; //cumul des pourcentages
2082 float v; //inertie (pourcentage sur un axe)
2083 long double *pcov, a1;
2084 char * temp;
2085 // string temps;
2086
2087 limite = 0.000001; //1e-010;//1e-030;
2088
2089 if (norm._type != 2)
2090 throw Anomalie(3);
2091 // 3-> op�ration impossible sur ce type de matrice
2092
2093 temp = new (char[50]);
2094 p0 = _nc - 1;
2095
2096 // redimensionnement
2097 res.resize(_nc, p0);
2098 //
2099 res.SetType(1);
2100 res._titre = _titre;
2101 res._titre.push_back("coordonn�es spatiales sur les colonnes");
2102 res._titre.push_back("espace � ");
2103 // itoa (p0,temp,10);
2104 res._titre[res._titre.size() - 1].AjEntier(p0); //temp ;
2105 res._titre[res._titre.size() - 1] += " dimensions";
2106 res._flag = 0;
2107
2108 // redimensionnement
2109 stat.resize(p0, 3);
2110 //
2111 stat.SetType(1);
2112 stat._titre = _titre;
2113 stat._titre.push_back(
2114 "statistiques des coordonn�es spatiales sur les colonnes");
2115 stat._titre.push_back("espace � ");
2116 // temps = p0;
2117 // itoa (p0,temp,10);
2118 stat._titre[stat._titre.size() - 1].AjEntier(p0); //temps.c_str() ;
2119 stat._titre[stat._titre.size() - 1] += " dimensions";
2120 stat._flag = 3;
2121
2122 if ((_flag == 2) || (_flag == 3)) {
2123 res._flag = 1;
2124 //copie des �tiquettes des lignes
2125 // res._tlig = _tcol;
2126 res._tlig = _tlig;
2127 }
2128
2129 res._flag = res._flag + 2; //num�rotation des axes
2130 for (i = 0; i < res._nc; i++) {
2131 // strcpy(temp,"axe_");
2132 // itoa(i + 1,temp + 4,10);
2133 // strcat(temp,"");
2134 res._tcol[i].assign("axe_");
2135 res._tcol[i].AjEntier(i + 1);
2136
2137 // res._tcol[i].assign(temp);
2138 stat._tlig[i].assign(res._tcol[i]);
2139 }
2140 stat._tcol[0].assign("Valeur_Propre");
2141 stat._tcol[1].assign("Inertie");
2142 stat._tcol[2].assign("Cumul");
2143
2144 pcov = norm._tab;
2145 PC = 100;
2146
2147 //test sur les valeurs propres pour �liminer l'effet Ariane V
2148 j = 0;
2149 while ((fabs(pvlp[j]) > limite) && (j < _nc)) {
2150 j++;
2151 }
2152 if (j < _nc)
2153 for (i = j; i < _nc; i++)
2154 pvlp[i] = 0;
2155
2156 //somme des valeurs propres
2157 tr = 0;
2158 for (j = 0; j < _nc; j++)
2159 tr = tr + pvlp[j];
2160
2161 v = 0;
2162 t = 0;
2163 for (i = 0; i < p0; i++) {
2164 a1 = pvlp[i] / tr;
2165 v = PC * (float) a1;
2166 t = t + v;
2167 stat._tab[i * 3] = pvlp[i];
2168 stat._tab[(i * 3) + 1] = v;
2169 stat._tab[(i * 3) + 2] = t;
2170 }
2171
2172 k = 0;
2173 for (i = 0; i < norm._nc; i++) { //scan sur les colonnes
2174 for (j = 0; j < p0; j++) { //coord col i, axe j
2175 a1 = (*(pcov + rindice(i, j))) * (sqrt((long double) pvlp[j]));
2176 res._tab[k] = a1 * PC;
2177 k++;
2178 }
2179 }
2180 }
2181
2182 void MatriceLD::fscalaire(MatriceLD& pscalaire) {
2183 /******************************************************************************/
2184 /* calcul du produit scalaire */
2185 /******************************************************************************/
2186 unsigned long i, a, j;
2187
2188 long double dt, x, x2, z;
2189 long double *pcov, *pvp, *ps;
2190 unsigned long nbcolonnes(GetNC());
2191 //cerr << "MatriceLD::fscalaire(MatriceLD& pscalaire) debut " << nbcolonnes << endl;
2192 pvp = new long double[nbcolonnes];
2193 ps = new long double[nbcolonnes];
2194 //cerr << "MatriceLD::fscalaire(MatriceLD& pscalaire) coucou 1" << endl;
2195 for (i = 0; i < nbcolonnes; i++) {
2196 pvp[i] = 0;
2197 ps[i] = 0;
2198 }
2199
2200 pscalaire.resize(nbcolonnes, nbcolonnes);
2201 pscalaire.SetType(2);
2202 //cerr << "MatriceLD::fscalaire(MatriceLD& pscalaire) coucou 2" << endl;
2203 pscalaire._titre = _titre;
2204 pscalaire._titre.push_back("produit scalaire");
2205 pscalaire._flag = _flag;
2206
2207 if ((_flag == 2) || (_flag == 3)) {
2208 //copie des �tiquettes des colonnes
2209 pscalaire._tcol = _tcol;
2210 pscalaire._tlig = _tcol;
2211 }
2212
2213 //cerr << "MatriceLD::fscalaire(MatriceLD& pscalaire) coucou 3" << endl;
2214 //copie du tableau
2215 a = nbcolonnes * nbcolonnes;
2216 for (i = 0; i < a; i++) {
2217 if (i < _t)
2218 pscalaire._tab[i] = _tab[i];
2219 else
2220 pscalaire._tab[i] = 0;
2221 }
2222
2223 pcov = pscalaire._tab;
2224
2225 dt = 0.;
2226 z = (long double) 1 / nbcolonnes;
2227
2228 //cerr << "MatriceLD::fscalaire(MatriceLD& pscalaire) coucou 4" << endl;
2229 for (i = 0; i < nbcolonnes; i++) {
2230 for (j = 0; j < nbcolonnes; j++) {
2231 x = *(pcov + rindice(i, j));
2232 x2 = x * x;
2233 *(ps + i) = (*(ps + i)) + x2;
2234 dt = dt + x2;
2235 }
2236 *(ps + i) = (*(ps + i)) * z;
2237 }
2238 //cerr << "MatriceLD::fscalaire(MatriceLD& pscalaire) coucou 5 " << nbcolonnes << " " << z << endl;
2239 dt = dt * z * z;
2240 for (i = 0; i < nbcolonnes; i++) {
2241 //cerr << "MatriceLD::fscalaire(MatriceLD& pscalaire) coucou i " << i << endl;
2242 for (j = 0; ((i > 0) && (j <= (i - 1))); j++) {
2243 //cerr << "MatriceLD::fscalaire(MatriceLD& pscalaire) coucou j " << j << endl;
2244 a = rindice(i, j);
2245 *(pcov + a) = 0.5 * ((*(ps + i)) + (*(ps + j)) - dt - (*(pcov + a))
2246 * (*(pcov + a)));
2247 *(pcov + rindice(j, i)) = *(pcov + a);
2248 }
2249 //cerr << "MatriceLD::fscalaire(MatriceLD& pscalaire) coucou 1i " << i << endl;
2250 *(pcov + rindice(i, j)) = 0.5 * (2 * (*(ps + i)) - dt);
2251 *(pvp + i) = (*(pcov + rindice(i, i))) / z;
2252 }
2253
2254 //cerr << "MatriceLD::fscalaire(MatriceLD& pscalaire) delete" << endl;
2255 delete[] pvp;
2256 delete[] ps;
2257 //cerr << "MatriceLD::fscalaire(MatriceLD& pscalaire) fin" << endl;
2258
2259 }
2260
2261 void MatriceLD::fatd(MatriceLD& pcoord, MatriceLD& pstat) {
2262 //pmat1, matrice de d�part: tableau de distances (matrice
2263 //triangulaire inf�rieure, de type 2 pour NTSYS
2264 //pstat: matrice contenant les statistiques sur l'importance de
2265 //chaque axe dans un espace � n dimensions
2266 //pcoord: matrice contenant les coordonn�es des OTUs (r�sultat) dans cet
2267 // espace � n dimensions
2268
2269 MatriceLD& pmat1 = *this;
2270 pmat1.SetType(1);
2271
2272 //cerr << "MatriceLD::fatd (MatriceLD& pcoord, MatriceLD& pstat) debut" << endl;
2273 if (pmat1.GetFlag() == 2)
2274 pmat1.ftranspose();
2275
2276 // au fomat NTSys: mettre les labels sur les colonnes (2� chiffre)
2277
2278 MatriceLD mscalaire; //produit scalaire
2279 MatriceLD mdiag;//matrice diagonalis�e
2280
2281 MatriceLD mnormalisee; //matrice normalisee
2282 vector<long double> pvlp; //vecteurs propres
2283
2284 try {
2285 //travail sur une matrice de distances
2286 if (_type != 2)
2287 pmat1.SetType(2);
2288
2289 //cerr << "MatriceLD::fatd (MatriceLD& pcoord, MatriceLD& pstat) coucou 1" << endl;
2290 pmat1.fscalaire(mscalaire);
2291
2292 //cerr << "MatriceLD::fatd (MatriceLD& pcoord, MatriceLD& pstat) coucou 2" << endl;
2293
2294 mscalaire.fdiagonalisation(mdiag, pvlp);
2295
2296 //cerr << "MatriceLD::fatd (MatriceLD& pcoord, MatriceLD& pstat) coucou 3" << endl;
2297 } catch (MatriceLD::Anomalie erreur) {
2298 cout << erreur.fmessage(erreur.le_pb);
2299 }
2300
2301 // mdiag.normalise(&mnormalisee, pmat1, pvlp) ;
2302
2303 //coordcol redimensionne pstat et pcoord
2304 pmat1.fcoordcol(pstat, pcoord, mdiag, pvlp);
2305 //cerr << "MatriceLD::fatd (MatriceLD& pcoord, MatriceLD& pstat) fin" << endl;
2306
2307 }
2308
2309 JeuMatriceLD::JeuMatriceLD(istream& entree) {
2310 // Lecture de fichiers contenant plusieurs matrices
2311 iFlux(entree);
2312 }
2313
2314 // Lecture de fichiers contenant plusieurs matrices
2315 void JeuMatriceLD::iFluxXML(istream& entree) {
2316
2317 }
2318
2319 void JeuMatriceLD::iFlux(istream& entree) {
2320 // Lecture de fichiers contenant plusieurs matrices
2321
2322 stringstream* tampon;
2323 bool is_xml(false);
2324 // iostream ensortie(tampon.rdbuf());
2325 char car;
2326 ChaineCar ligne;
2327 vector<unsigned long> positions;
2328 MatriceLD* pmat;
2329 unsigned long i, j;
2330
2331 //balayage de l'entree pour trouver les positions
2332 // (delimitee par //)
2333 entree.clear();
2334 entree.seekg(0);
2335 positions.push_back(0);
2336 positions.push_back(0);
2337
2338 entree.get(car);
2339 while (entree.good()) {
2340 if ((car == '/') && (entree.peek() == '/')) {
2341 positions.back() = entree.tellg();
2342 positions.back() -= 2;
2343
2344 //cerr <<"coucou";
2345 GetLigneFlot(entree, ligne);
2346 positions.push_back(entree.tellg());
2347
2348 positions.push_back(entree.tellg());
2349 } else {
2350 GetLigneFlot(entree, ligne);
2351 if (ligne.find("xml version=\"1.0\"", 0) > 0) {
2352 // c'est du ricqlèèèès !!!!!
2353 is_xml = true;
2354 break;
2355 }
2356 }
2357 positions.back() = entree.tellg();
2358 entree.get(car);
2359 }
2360
2361 if (is_xml) {
2362 iFluxXML(entree);
2363 return;
2364 }
2365 // remplissage du buffer de matrices
2366
2367 for (i = 0; i < (positions.size() - 1); i += 2) {
2368 if ((positions[i + 1] - positions[i]) < 6)
2369 continue;
2370
2371 entree.clear();
2372 entree.seekg(positions[i]);
2373
2374 _titres.push_back("");
2375 if (entree.peek() == '@') {
2376 entree.get(car);
2377 GetLigneFlot(entree, ligne);//_titres.back());
2378
2379 //cerr << ligne;
2380 ligne.GetMot(1, _titres.back());
2381 //cerr << _titres.back();
2382 }
2383
2384 tampon = new stringstream();
2385 for (j = positions[i]; j < positions[i + 1]; j++) {
2386 entree.get(car);
2387 *tampon << car;
2388 j = entree.tellg();
2389
2390 }
2391 *tampon << ends;
2392
2393 //cin >> car;
2394 // cout << tampon->str() <<endl << endl;
2395 pmat = new MatriceLD();
2396 try {
2397 *tampon >> *pmat;
2398 _tableau.push_back(pmat);
2399 } catch (MatriceLD::Anomalie pb) {
2400 delete pmat;
2401 _titres.pop_back();
2402 }
2403
2404 delete tampon;
2405 // delete pmat;
2406 // delete pensortie;
2407 }
2408 }
2409
2410 void JeuMatriceLD::oFlux(ostream& sortie, int format) const {
2411 // Ecriture de fichiers contenant plusieurs matrices
2412 if (format == 5) {
2413 oGnumeric(sortie);
2414 return;
2415 }
2416 long i, taille(_tableau.size());
2417
2418 for (i = 0; i < taille; i++) {
2419 sortie << "@" << _titres[i] << endl;
2420 _tableau[i]->oFormat(sortie, format);
2421 sortie << endl << "//" << endl;
2422 }
2423
2424 }
2425
2426 void JeuMatriceLD::oGnumeric(ostream& fichier) const {
2427 QString * p_output_xml = new QString("");
2428 QXmlStreamWriter xml_stream(p_output_xml);
2429 xml_stream.setAutoFormatting(true);
2430 xml_stream.writeStartDocument();
2431 // Ecriture de fichiers contenant plusieurs matrices
2432 long i, taille(_tableau.size());
2433
2434 xml_stream.writeNamespace("http://www.gnumeric.org/v10.dtd", "gmr");
2435 xml_stream.writeNamespace("http://www.w3.org/2001/XMLSchema-instance",
2436 "xsi");
2437 xml_stream.writeStartElement("http://www.gnumeric.org/v10.dtd", "Workbook");
2438 xml_stream.writeAttribute("http://www.w3.org/2001/XMLSchema-instance",
2439 "schemaLocation", "http://www.gnumeric.org/v8.xsd");
2440
2441 //fichier
2442 // << "<gmr:Workbook xmlns:gmr=\"http://www.gnumeric.org/v10.dtd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.gnumeric.org/v8.xsd\">"
2443 // << endl;
2444
2445 xml_stream.writeStartElement("http://www.gnumeric.org/v10.dtd",
2446 "Attributes");
2447 xml_stream.writeStartElement("http://www.gnumeric.org/v10.dtd", "Attribute");
2448 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd", "name",
2449 "WorkbookView::show_horizontal_scrollbar");
2450 //fichier << "<gmr:Attributes>" << endl;
2451 //fichier << " <gmr:Attribute>" << endl;
2452 //fichier << "<gmr:name>WorkbookView::show_horizontal_scrollbar</gmr:name>"
2453 // << endl;
2454 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd", "type", "4");
2455 //fichier << " <gmr:type>4</gmr:type>" << endl;
2456 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd", "value",
2457 "TRUE");
2458 //fichier << " <gmr:value>TRUE</gmr:value>" << endl;
2459 xml_stream.writeEndElement();
2460 // fichier << " </gmr:Attribute>" << endl;
2461 xml_stream.writeStartElement("http://www.gnumeric.org/v10.dtd", "Attribute");
2462 //fichier << " <gmr:Attribute>" << endl;
2463 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd", "name",
2464 "WorkbookView::show_vertical_scrollbar");
2465 //fichier << " <gmr:name>WorkbookView::show_vertical_scrollbar</gmr:name>"
2466 // << endl;
2467 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd", "type", "4");
2468 //fichier << " <gmr:type>4</gmr:type>" << endl;
2469 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd", "value",
2470 "TRUE");
2471 //fichier << " <gmr:value>TRUE</gmr:value>" << endl;
2472 xml_stream.writeEndElement();
2473 //fichier << " </gmr:Attribute>" << endl;
2474 xml_stream.writeStartElement("http://www.gnumeric.org/v10.dtd", "Attribute");
2475 //fichier << " <gmr:Attribute>" << endl;
2476 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd", "name",
2477 "WorkbookView::show_notebook_tabs");
2478 //fichier << " <gmr:name>WorkbookView::show_notebook_tabs</gmr:name>"
2479 // << endl;
2480 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd", "type", "4");
2481 //fichier << " <gmr:type>4</gmr:type>" << endl;
2482 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd", "value",
2483 "TRUE");
2484 //fichier << " <gmr:value>TRUE</gmr:value>" << endl;
2485 xml_stream.writeEndElement();
2486 //fichier << " </gmr:Attribute>" << endl;
2487
2488 xml_stream.writeStartElement("http://www.gnumeric.org/v10.dtd", "Attribute");
2489 //fichier << " <gmr:Attribute>" << endl;
2490 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd", "name",
2491 "WorkbookView::do_auto_completion");
2492 //fichier << " <gmr:name>WorkbookView::do_auto_completion</gmr:name>"
2493 // << endl;
2494 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd", "type", "4");
2495 //fichier << " <gmr:type>4</gmr:type>" << endl;
2496 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd", "value",
2497 "TRUE");
2498 //fichier << " <gmr:value>TRUE</gmr:value>" << endl;
2499 xml_stream.writeEndElement();
2500 //fichier << " </gmr:Attribute>" << endl;
2501 xml_stream.writeEndElement();
2502 //fichier << " </gmr:Attributes>" << endl;
2503 xml_stream.writeStartElement("http://www.gnumeric.org/v10.dtd", "Summary");
2504 //fichier << " <gmr:Summary>" << endl;
2505 xml_stream.writeStartElement("http://www.gnumeric.org/v10.dtd", "Item");
2506 //fichier << " <gmr:Item>" << endl;
2507 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd", "name",
2508 "biology softwares");
2509 //fichier << " <gmr:name>biology softwares</gmr:name>" << endl;
2510 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd",
2511 "val-string", "http://www.pge.cnrs-gif.fr/bioinfo");
2512 //fichier
2513 // << " <gmr:val-string>http://www.pge.cnrs-gif.fr/bioinfo</gmr:val-string>"
2514 // << endl;
2515 xml_stream.writeEndElement();
2516 //fichier << " </gmr:Item>" << endl;
2517 xml_stream.writeStartElement("http://www.gnumeric.org/v10.dtd", "Item");
2518 //fichier << " <gmr:Item>" << endl;
2519 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd", "name",
2520 "author");
2521 //fichier << " <gmr:name>author</gmr:name>" << endl;
2522 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd",
2523 "val-string", "Olivier Langella <[email protected]>");
2524 //fichier
2525 // << " <gmr:val-string>Olivier Langella, [email protected]</gmr:val-string>"
2526 // << endl;
2527 xml_stream.writeEndElement();
2528 //fichier << " </gmr:Item>" << endl;
2529 xml_stream.writeEndElement();
2530 //fichier << "</gmr:Summary>" << endl;
2531 xml_stream.writeStartElement("http://www.gnumeric.org/v10.dtd",
2532 "SheetNameIndex");
2533
2534 //fichier << " <gmr:SheetName>Matrix 1</gmr:SheetName>" << endl;
2535
2536
2537 //fichier << "<gmr:SheetNameIndex>" << endl;
2538 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd", "SheetName",
2539 "Matrix 1");
2540 for (i = 0; i < taille; i++) {
2541 xml_stream.writeTextElement("http://www.gnumeric.org/v10.dtd",
2542 "SheetName", _titres[i].c_str());
2543 //fichier << "<gmr:SheetName>" << _titres[i] << "</gmr:SheetName>"
2544 // << endl;
2545 }
2546 xml_stream.writeEndElement();
2547 //fichier << "</gmr:SheetNameIndex> " << endl;
2548
2549 xml_stream.writeStartElement("http://www.gnumeric.org/v10.dtd", "Sheets");
2550 // fichier.setf(ios::scientific);
2551
2552 for (i = 0; i < taille; i++) {
2553 QString sheetname(_titres[i].c_str());
2554 _tableau[i]->oGnumericSheet(xml_stream, sheetname);
2555 }
2556
2557 fichier << " </gmr:Sheets> " << endl;
2558 fichier << "</gmr:Workbook> " << endl;
2559
2560 xml_stream.writeEndDocument();
2561
2562 fichier << p_output_xml->toStdString();
2563 delete (p_output_xml);
2564
2565 }
2566
2567 void JeuMatriceLD::SetTitre(long i, const char * chaine) {
2568
2569 // if ((i < 0) || (i >= _titres.size())) throw Anomalie(1);
2570 // cerr << chaine << endl;
2571 _titres[i].assign(chaine);
2572 // cerr << "fini" << endl;
2573 }
2574
2575 void JeuMatriceLD::resize(long nouvtaille) {
2576 //on efface:
2577 long i, taille(_tableau.size());
2578
2579 _titres.resize(nouvtaille);
2580
2581 for (i = 0; i < taille; i++)
2582 delete _tableau[i];
2583
2584 //on r�alloue
2585 _tableau.resize(nouvtaille);
2586 for (i = 0; i < nouvtaille; i++)
2587 _tableau[i] = new MatriceLD;
2588
2589 }
2590
2591 } //namespace biolib {
2592 } //namespace vecteurs {