-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSMatrix.h
680 lines (612 loc) · 15.6 KB
/
SMatrix.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
#ifndef SMATRIX_H
#define SMATRIX_H
#include <cstddef>
#include <cstdio>
#include <cstring>
#include <cerrno>
#include <exception>
#include "Error.h"
#include "fhash_map.h"
#include "hash_functions.h"
#include "MatrixInterface.h"
#if 0
#include "GSMatrix.h"
#endif
namespace flux {
namespace la {
class MVector;
class MMatrix;
class PMatrix;
}}
namespace flux {
namespace la {
#if 0
class SMatrix : public GSMatrix< double >
{
public:
inline SMatrix(size_t rows, size_t cols, double expct_fill = 0.05)
: GSMatrix< double >(rows,cols,expct_fill) { }
inline SMatrix(SMatrix const & copy)
: GSMatrix< double >(copy){ }
inline SMatrix(MMatrix const & copy)
: GSMatrix< double >(copy) { }
inline SMatrix()
: GSMatrix< double >() { }
virtual operator MMatrix () const
{
MMatrix M(rows(),cols());
for (iterator a_ij=begin(); a_ij!=end(); a_ij++)
M.set(a_ij->row,a_ij->col,a_ij->value);
return M;
}
};
#endif
/**
* Klasse SMatrix -- modelliert eine dünn besetzte Matrix (sparse matrix).
* Zur Speicherung der Elemente wird ein schnelles Hash verwendet.
*
* @author Michael Weitzel <[email protected]>
*/
class SMatrix : public MatrixInterface< double >
{
private:
/** der double-Wert 0. */
static double zero_;
/** Anzahl der Zeilen */
size_t rows_;
/** Anzahl der Spalten */
size_t cols_;
/** interne Hash-Datenstruktur */
fhash_map< mxkoo,double,mxkoo_hashf > * hash_;
public:
/**
* Ein Referenzen-Tripel aus (Zeile,Spalte,Wert).
* Referenziert einen Eintrag in der Sparse-Matrix, wobei die
* Koordinaten konstant sind, der Wert aber änderbar bleibt.
*
* @author Michael Weitzel <[email protected]>
*/
struct Elem
{
size_t const & row;
size_t const & col;
double & value;
/** Constructor -- wird von SMatrix::iterator aufgerufen */
Elem(size_t const & r, size_t const & c, double & v)
: row(r), col(c), value(v) {}
/**
* Dereferenzierungs-Operator, Struct-Zugriff.
* Implementierung für den "->"-Operator der SMatrix::iterator-Klasse.
* Das hier funktioniert, weil der "->"-Operator in C++ als
* transitiv definiert ist.
*/
Elem const * operator->() { return this; }
};
/**
* Ein Referenzen-Tripel aus (Zeile,Spalte,Wert).
* Referenziert einen Eintrag in der Sparse-Matrix, wobei die
* Koordinaten konstant sind, der Wert aber änderbar bleibt.
*
* @author Michael Weitzel <[email protected]>
*/
struct ConstElem
{
size_t const & row;
size_t const & col;
double const & value;
/** Constructor -- wird von SMatrix::iterator aufgerufen */
ConstElem(size_t const & r, size_t const & c, double const & v)
: row(r), col(c), value(v) {}
/**
* Dereferenzierungs-Operator, Struct-Zugriff.
* Implementierung für den "->"-Operator der SMatrix::iterator-Klasse.
* Das hier funktioniert, weil der "->"-Operator in C++ als
* transitiv definiert ist.
*/
ConstElem const * operator->() { return this; }
};
class iterator;
/**
* Ein Iterator zum Iterieren über die Elemente der Sparse-Matrix.
*
* @author Michael Weitzel <[email protected]>
*/
class const_iterator
{
/** die SMatrix-Klasse hat Zugriff auf den privaten Constructor */
friend class SMatrix;
friend class iterator;
private:
/** das SMatrix-Objekt, auf das der Iterator "zeigt". */
SMatrix const * pobj_;
/** aktuelle Position im Hash-Objekt */
fhash_map< mxkoo,double,mxkoo_hashf >::const_iterator pos_;
private:
/**
* Privater Constructor;
* wird von der SMatrix-Klasse in SMatrix::begin() verwendet
* und ist ansonsten von außen nicht zugreifbar.
*
* @param pobj das SMatrix-Objekt, auf dem der Iterator
* arbeitet
*/
const_iterator(SMatrix const * pobj) : pobj_(pobj)
{
pos_ = pobj_->hash_->begin();
}
/**
* Privater Constructor.
*
* @param pobj das SMatrix-Objekt, auf dem der Iterator
* arbeitet
* @param pos aktuelle Position im Hash
*/
const_iterator(
SMatrix const * pobj,
fhash_map< mxkoo,double,mxkoo_hashf >::const_iterator const & pos
) : pobj_(pobj), pos_(pos) { }
public:
/**
* Constructor.
* Erzeugt einen Iterator, der ins Nirvana "zeigt"
*/
const_iterator() : pobj_(0) {}
public:
/**
* Prä-Inkrement-Operator.
* Setzt den Iterator auf das nächste Element im Hash.
*
* @return true, falls Inkrementierung möglich war.
*/
inline const_iterator & operator++()
{
++pos_;
return *this;
}
/**
* Post-Inkrement-Operator.
* Setzt den Iterator auf das nächste Element im Hash.
*
* @return true, falls Inkrementierung möglich war.
*/
inline const_iterator operator++(int)
{
const_iterator tmp(*this);
++*this;
return tmp;
}
/**
* Dereferenzierungs-Operator.
* Simuliert eine Dereferenzierung des Iterators und gibt ein
* Elem-Objekt mit Koordinaten und Wert des Eintrages zurück.
*
* @return Elem-Objekt für den SMatrix-Eintrag
*/
inline ConstElem operator*() const
{
if (!pobj_ || pos_ == pobj_->hash_->end())
throw std::exception();
return ConstElem(pos_->key.i_, pos_->key.j_, pos_->value);
}
/**
* Dereferenzierungs-Operator.
* Wegen der Transitivität von -> wird hier der ->-Operator
* von ConstElem verwendent:
*
* @return Elem-Objekt für den SMatrix-Eintrag
*/
inline ConstElem operator->() const { return operator*(); }
/**
* Vergleichs-Operator.
* Bei Gleichheit von zwei Iteratoren entsprechen sich die
* Kollisionslisten-Zeiger
*
* @param rval zu vergleichendes Iterator-Objekt
* @return true, falls Gleichheit
*/
inline bool operator==(const_iterator const & rval) const
{
return pos_==rval.pos_;
}
/**
* Vergleichs-Operator (Ungleichheit).
* Siehe operator==().
*
* @param rval zu vergleichendes Iterator-Objekt
* @return true, falls Ungleichheit
*/
inline bool operator!=(const_iterator const & rval) const
{
return pos_!=rval.pos_;
}
};
/**
* Ein Iterator zum Iterieren über die Elemente der Sparse-Matrix.
*
* @author Michael Weitzel <[email protected]>
*/
class iterator
{
/** die SMatrix-Klasse hat Zugriff auf den privaten Constructor */
friend class SMatrix;
private:
/** das SMatrix-Objekt, auf das der Iterator "zeigt". */
SMatrix * pobj_;
/** aktuelle Position im Hash-Objekt */
fhash_map< mxkoo,double,mxkoo_hashf >::iterator pos_;
private:
/**
* Privater Constructor;
* wird von der SMatrix-Klasse in SMatrix::begin() verwendet
* und ist ansonsten von außen nicht zugreifbar.
*
* @param pobj das SMatrix-Objekt, auf dem der Iterator
* arbeitet
*/
iterator(SMatrix * pobj) : pobj_(pobj)
{
pos_ = pobj_->hash_->begin();
}
/**
* Privater Constructor.
*
* @param pobj das SMatrix-Objekt, auf dem der Iterator
* arbeitet
* @param pos aktuelle Position im Hash
*/
iterator(
SMatrix * pobj,
fhash_map< mxkoo,double,mxkoo_hashf >::iterator const & pos
) : pobj_(pobj), pos_(pos) { }
public:
/**
* Constructor.
* Erzeugt einen Iterator, der ins Nirvana "zeigt"
*/
iterator() : pobj_(0) {}
public:
/**
* Prä-Inkrement-Operator.
* Setzt den Iterator auf das nächste Element im Hash.
*
* @return true, falls Inkrementierung möglich war.
*/
inline iterator & operator++()
{
++pos_;
return *this;
}
/**
* Post-Inkrement-Operator.
* Setzt den Iterator auf das nächste Element im Hash.
*
* @return true, falls Inkrementierung möglich war.
*/
inline iterator operator++(int)
{
iterator tmp(*this);
++*this;
return tmp;
}
/**
* Dereferenzierungs-Operator.
* Simuliert eine Dereferenzierung des Iterators und gibt ein
* Elem-Objekt mit Koordinaten und Wert des Eintrages zurück.
*
* @return Elem-Objekt für den SMatrix-Eintrag
*/
inline Elem operator*() const
{
if (!pobj_ || pos_ == pobj_->hash_->end())
throw std::exception();
return Elem(pos_->key.i_,pos_->key.j_,pos_->value);
}
/**
* Dereferenzierungs-Operator.
* Wegen der Transitivität von -> wird hier der ->-Operator
* von hpair verwendent:
*
* @return Elem-Objekt für den SMatrix-Eintrag
*/
inline Elem operator->() const { return operator*(); }
/**
* Vergleichs-Operator.
* Bei Gleichheit von zwei Iteratoren entsprechen sich die
* Kollisionslisten-Zeiger
*
* @param rval zu vergleichendes Iterator-Objekt
* @return true, falls Gleichheit
*/
inline bool operator==(iterator const & rval) const
{
return pos_==rval.pos_;
}
/**
* Vergleichs-Operator (Ungleichheit).
* Siehe operator==().
*
* @param rval zu vergleichendes Iterator-Objekt
* @return true, falls Ungleichheit
*/
inline bool operator!=(iterator const & rval) const
{
return pos_!=rval.pos_;
}
/**
* Ein Cast-Operator zum casten von iterator nach
* const_iterator. Ermöglicht eine Zuweisung vom Typ
* iterator=const_iterator.
*
* @return const_iterator object
*/
inline operator const_iterator() const
{
return const_iterator(pobj_,pos_);
}
};
public:
/**
* Constructor
*
* @param rows Spalten
* @param cols Zeilen
* @param expct_fill Erwarteter Füllfaktor der Matrix
*/
inline SMatrix(size_t rows, size_t cols, double expct_fill = 0.05)
: rows_(rows), cols_(cols)
{
hash_ = new fhash_map< mxkoo,double,mxkoo_hashf >(
size_t(1. + double(rows_) * double(cols_) * expct_fill)
);
}
/**
* Copy-Constructor. Kopiert das Hash mit den Matrix-Elementen
*
* @param copy Zu kopierende Sparse-Matrix
*/
inline SMatrix(SMatrix const & copy)
: rows_(copy.rows_), cols_(copy.cols_)
{
hash_ = new fhash_map< mxkoo,double,mxkoo_hashf >( *(copy.hash_) );
}
/**
* "Copy"-Constructor für die MMatrix-Klasse.
* Kopiert von 0 verschiedene Elemente in die Sparse-Matrix
*
* @param copy Zu kopierende Matrix
*/
SMatrix(MMatrix const & copy);
/**
* Constructor für Dummy-Objekte
*/
inline SMatrix() :
rows_(0), cols_(0),
hash_(new fhash_map< mxkoo,double,mxkoo_hashf >) { }
/**
* Destructor. Löscht das Hash mit den Matrix-Elementen
*/
inline ~SMatrix() { delete hash_; }
public:
/**
* Ein Cast-Operator von SMatrix nach MMatrix
*
* @return ein MMatrix-Objekt mit dem Inhalt der Sparse-Matrix
*/
operator MMatrix () const;
/**
* SMatrix-MVektor-Multiplikation
*
* @param rval rechtes Argument
* @return Produkt
*/
MVector operator*(MVector const & rval) const;
/**
* SMatrix-MVektor-Multiplikation
*
* @param rval rechtes Argument
* @return Produkt
*/
SMatrix operator*= (double rval);
/**
* SMatrix-MMatrix-Multiplikation
*
* @param rval rechtes Argument
* @return Produktmatrix
*/
MMatrix operator*(MMatrix const & Rval) const;
/**
* Zugriff zum Lesen/Schreiben auf ein Element (i,j). Wenn das Element
* (i,j) noch nicht existerte, wird es zunächst erzeugt.
*
* @param i Zeile
* @param j Spalte
* @return Referenz auf das evtl. neu erzeugte Element (i,j)
*/
inline double & operator() (size_t i, size_t j)
{
fASSERT(i<rows_ && j<cols_);
return hash_->operator[](mxkoo(i,j));
}
/**
* Zuweisungsoperator.
*
* @param copy Zu kopierendes Objekt
* @return Referenz auf *this
*/
inline SMatrix & operator= (SMatrix const & copy)
{
delete hash_;
hash_ = new fhash_map< mxkoo,double,mxkoo_hashf >( *(copy.hash_) );
rows_ = copy.rows_;
cols_ = copy.cols_;
return *this;
}
/**
* Bevorzugte Schnittstelle zum Schreiben von Elementen
*
* @param i Zeile
* @param j Spalte
* @param val Wert
*/
inline void set(size_t i, size_t j, double const & val)
{
fASSERT(i<rows_ && j<cols_);
hash_->insert(mxkoo(i,j),val);
}
/**
* Bevorzugte Schnittstelle zum Lesen von Elementen
*
* @param i Zeile
* @param j Spalte
* @return Elementwert oder Default-Wert 0.0, falls Element nicht existiert.
*/
inline double const & get(size_t i, size_t j) const
{
fASSERT(i<rows_ && j<cols_);
double * dptr = hash_->findPtr(mxkoo(i,j));
return dptr?*dptr:zero_;
}
/**
* Abfrage von Elementen. Wenn das Element (i,j) existiert, wird
* ein Zeiger auf seinen Wert zurückgegeben. Wenn es nicht existiert,
* wird ein Nullzeiger zurückgegeben.
*
* @param i Zeile
* @param j Spalte
* @return Zeiger auf den Wert (i,j) oder Null-Zeiger
*/
inline double * refIfSet(size_t i, size_t j) const
{
fASSERT(i<rows_ && j<cols_);
return hash_->findPtr(mxkoo(i,j));
}
/**
* Löschen von Elementen.
* Wenn das Element (i,j) existiert, wird es gelöscht und es wird
* <tt>true</tt> zurückgegeben. Existiert (i,j) nicht, wird <tt>false</tt>
* zurückgegeben
*
* @param i Zeile
* @param j Spalte
* @return true, falls Löschung erfolgreich, false, falls Element nicht gefunden
*/
inline bool erase(size_t i, size_t j)
{
fASSERT(i<rows_ && j<cols_);
return hash_->erase(mxkoo(i,j));
}
/**
* Löscht alle Elemente.
*/
inline void eraseAll() { hash_->clear(); }
/**
* Anzahl der Zeilen
*
* @return Anzahl der Zeilen
*/
inline size_t rows() const { return rows_; }
/**
* Anzahl der Spalten
*
* @return Anzahl der Spalten
*/
inline size_t cols() const { return cols_; }
/**
* Füllfaktor (der Matrix, nicht des Hashes)
*
* @return Füllfaktor der Matrix
*/
inline float fillFactor() const
{
return float(hash_->size())/float(rows_*cols_);
}
/**
* Anzahl der Einträge (ungleich 0).
*
* @return Anzahl der Non-Zero-Einträge
*/
inline size_t nnz() const { return hash_->size(); }
/**
* Gibt eine Statistik zum internen Hash zurück.
* Berechnet statistische Kenngrößen der Verteilung der
* Kollisionslistenlängen. Damit läßt sich die Qualität der
* Hash-Funktion bewerten. Die chi-Quadrat-Parameter sind
* optional.
*
* @param N Größe der Hash-Tabelle
* @param E Erwartungswert der Kollisionslistenlänge
* @param V Varianz der Kollisionlistenlängenverteilung
* @param chisq chi-Quadrat-Wert der Kollisionlistenlängenverteilung
* @param chidof Anzahl der Freiheitsgrade der chi-Quadrat-Verteilung
*/
inline void stats(
int & N,
double & E,
double & V,
double & chisq,
int & chidof
) const
{
hash_->stats(N,E,V,chisq,chidof);
}
public:
/**
* Erzeugt einen iterator und setzt ihn auf das erste
* Element der Sparse-Matrix (Hash)
*
* @return Iterator, der auf das erste Matrix-Element "zeigt"
*/
inline iterator begin() { return iterator(this); }
/**
* Erzeugt einen const_iterator und setzt ihn auf das erste
* Element der Sparse-Matrix (Hash)
*
* @return Iterator, der auf das erste Matrix-Element "zeigt"
*/
inline const_iterator begin() const { return const_iterator(this); }
/**
* Erzeugt einen iterator mit Ende-Markierung.
*
* @return end-Iterator
*/
inline iterator end() { return iterator(); }
/**
* Erzeugt einen const_iterator mit Ende-Markierung.
*
* @return end-Iterator
*/
inline const_iterator end() const { return const_iterator(); }
public:
/**
* Zeigt die Besetztheit der Sparse-Matrix
*/
void spy() const;
/**
* Gibt die Elemente der Sparse-Matrix auf stdout aus.
*/
void dump(FILE * outf = stdout, dump_t dt = dump_default) const;
/**
* Symmetrische Permutation: P^T*A*P
*
* @param P Permutationsmatrix
*/
void symmPerm(PMatrix const & P);
/**
* Messung der Bandbreite einer Sparse-Matrix A.
*
* @param lb gemessene untere/linke Bandbreite
* @param ub gemessene obere/rechte Bandbreite
*/
void measureBandWidth(int & lb, int & ub) const;
/**
* Macht die Matrix sparse: Identifiziert 0-Elemente und löscht sie
* aus der Datenstruktur
*/
void compress();
MVector diag() const;
double trace() const;
double norm1() const;
};
} // namespace flux::la
} // namespace flux
#endif