-
Notifications
You must be signed in to change notification settings - Fork 0
/
showmat.cpp
64 lines (44 loc) · 1.5 KB
/
showmat.cpp
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
// show sparse matrix
#include <iostream>
#include <fstream>
#include "sparse_matrix.hpp"
#include "sparse_matrix_io.hpp"
using namespace std;
void show_matrix(const sparse_matrix& M, ostream& outs=cerr) {
outs << "nonz:\t" << M.nonZeros() << endl;
outs << "size:\t" << M.size() << endl;
outs << "rho:\t" << 100 * ((float) M.nonZeros() / M.size()) << endl;
scalar_t max_v = 0;
scalar_t min_v = 10E20; //hmm need a max scalar_t!
// iterate non-zero values to accumulate value stats
for (int j = 0; j < M.outerSize(); ++j) {
for (sparse_matrix::InnerIterator it(M, j); it; ++it) {
//it.index();
scalar_t v = it.value();
max_v = (v > max_v) ? v : max_v;
min_v = (v < min_v) ? v : min_v;
}
}
outs << "min:\t" << min_v << endl;
outs << "max:\t" << max_v << endl;
}
int main(int argc, char *argv[]) {
sparse_matrix B;
// filename on command line
if (argc > 1) {
string filename(argv[1]);
std::ifstream ins;
ins.open(filename);
if (ins.good()) {
cerr << "reading matrix data from: " << filename << endl;
if (deserialize_matrix(B, ins)) show_matrix(B);
else cerr << "cannot deserialize matrix!" << endl;
} else cerr << "cannot read file: " << filename << "!" << endl;
} else {
cerr << "reading matrix data from stdin..." << endl;
// read matrix from stdin
if (deserialize_matrix(B)) show_matrix(B);
else cerr << "couldn't deserialize matrix on stdin!" << endl;
}
return 0;
}