-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.cpp
More file actions
107 lines (97 loc) · 2.4 KB
/
matrix.cpp
File metadata and controls
107 lines (97 loc) · 2.4 KB
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
#include "matrix.h"
// initlization of the A (influence), B (selectivity) matrices
void Matrix2::init(int n, int m)
{
this->n = n;
this->m = m;
cout << "Creating two " << n << "*" << m << " matrix" << endl;
A = new atomic<double> *[n]; B = new atomic<double> *[n];
for (int i = 0;i < n; ++i)
{
A[i] = new atomic<double>[m]; B[i] = new atomic<double>[m];
for (int j = 0; j < m - 1; ++j)
{
double r = ((double) rand() / (RAND_MAX));
A[i][j] = 0.002 * r;
B[i][j] = 0.002 * r;
}
A[i][m - 1] = B[i][m - 1] = 0.01;
}
Bsum = new Vec(m);
sumB();
}
void Matrix2::sumB()
{
// sum up all the "B_v"s for later usage
Bsum->clear();
for (int i = 0; i < n; ++i)
*Bsum += B[i];
}
Matrix2::~Matrix2()
{
for (int i = 0;i < n;++i)
{
delete A[i];
delete B[i];
}
delete A;
delete B;
delete Bsum;
}
//======================UTILITY FUNCTIONS======================
bool pairCompare(const pair<int, double>& firstElem, const pair<int, double>& secondElem)
{
return firstElem.second > secondElem.second;
}
// return the top node with highest norms
void Matrix2::getMaxNorm(vector<pair<int,double> >& ret)
{
for (int i = 0;i < n; ++i)
{
ret.push_back( make_pair(i, Vec(m, A[i]).norm()) );
}
sort(ret.begin(), ret.end(), pairCompare);
}
// return the top node with highest norms
void Matrix2::getMostInfluencialSites(vector<pair<int,double> >& ret, int k)
{
for (int i = 0;i < n; ++i)
{
ret.push_back( make_pair(i, Vec(m, A[i]).getCompAt(k) ) );
}
sort(ret.begin(), ret.end(), pairCompare);
}
// output the matrix to a txt file
void Matrix2::writeMat(string path)
{
ofstream outfile_A;
outfile_A.open((path + "A.csv").c_str(), ofstream::out);
ofstream outfile_B;
outfile_B.open((path + "B.csv").c_str(), ofstream::out);
cout << "Writing matrix A,B to";
//=========header=======
for (int j = 0; j < m; ++j)
{
if (j != 0) outfile_A << ",";
outfile_A << j;
if (j != 0) outfile_B << ",";
outfile_B << j;
}
outfile_A << "\n";
outfile_B << "\n";
//=========content=======
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
if (j != 0) outfile_A << ",";
outfile_A << A[i][j];
if (j != 0) outfile_B << ",";
outfile_B << B[i][j];
}
outfile_A << "\n";
outfile_B << "\n";
}
cout << " to " << path << "A.csv" << endl;
cout << " and " << path << "B.csv" << endl;
}