-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.h
210 lines (192 loc) · 4.56 KB
/
matrix.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
#pragma once
#include <vector>
#include <string>
#include <iostream>
#include <cmath>
#include <sstream>
#include <iomanip>
//A highly simplified, specialised and streamlined interface for matrices -- I could have used another implementation but I think this lightweight mimicry helps my case
//Though it can be used for other purposes within the Hypothesis subclasses (i.e. data binning), the squareMatrix class is designed entirely around the computation of the LU form, and hence the log-determinant of the matrix which is needed for the GAI.
class squareMatrix
{
public:
int Dimension;
squareMatrix()
{
Dimension = 0;
}
squareMatrix(int dim)
{
Dimension = dim;
Data = std::vector<std::vector<double>>(dim,std::vector<double>(dim,0.0));
};
//!Access to matrix is through (i,j) instead of [i][j], as this allows me to protect the internal structure of the arrays
double & operator()(unsigned int i, unsigned int j)
{
return Data[i][j];
}
//! If squareMatrix is ever const, need this to mimic the non-const one
const double & operator()(unsigned int i, unsigned int j) const
{
return Data[i][j];
}
//! Shouldn't be used much in real life, but useful to be able to printout the matrix as a diagnostic.
std::string Display()
{
std::ostringstream s;
for (int i = 0; i < Dimension; ++i)
{
if (i == 0 || i == Dimension -1)
s<<"| ";
else
s<< "( ";
for (int j = 0; j < Dimension; ++j)
{
s<< std::left << std::setw(15) << Data[i][j] << " ";
}
if (i == 0 || i == Dimension -1)
s<<"|\n";
else
s<< ")\n";
}
return s.str();
}
squareMatrix operator*(const squareMatrix & rhs)
{
if (rhs.Dimension != Dimension)
{
printf("Cannot compute product of different dimension square matrices");
exit(1);
}
squareMatrix out(Dimension);
for (int i = 0; i < Dimension; ++i)
{
for (int j = 0; j < Dimension; ++j)
{
for (int k = 0; k < Dimension; ++k)
{
out(i,j) += Data[i][k] * rhs(k,j);
}
}
}
return out;
}
//What this is here for: computing the logarithm of the determinant via the LU decomposition method
//The bits that are commented out are those necessary if you actually want the LU decomposition - since we just want the determinant they're only useful for diagnostics and checking the decomposition was successful
double log_LU_Determinant()
{
// squareMatrix P = Identity(Dimension);
squareMatrix L = Identity(Dimension);
squareMatrix A = *this;
for (int i = 0; i < Dimension-1; ++i)
{
// std::cout << i << "/"<< Dimension-1 << std::endl;
double base = A(i,i);
if (abs(base) < 1e-10)
{
squareMatrix newP = Identity(Dimension);
for (int q = i+1; q < Dimension; ++q)
{
if (abs(A(q,i)) > 1e-10)
{
newP(i,i) = 0;
newP(q,q) = 0;
newP(q,i) = 1;
newP(i,q) = 1;
break;
}
}
A = newP * A;
L = newP * L;
// P = newP * P;
base = A(i,i);
}
for (int q = i + 1; q < Dimension; ++q)
{
L(q,i) = A(q,i)/base;
double ell = - A(q,i)/base;
A(q,i) = 0;
for (int z = i + 1; z < Dimension; ++z)
{
A(q,z) += ell * A(i,z);
}
}
// if (A.isUpperTriangular() && L.isLowerTriangular())
// {
// break;
// }
}
double p = 0;
for (int i = 0; i < Dimension; ++i)
{
p += std::log(abs((double)A(i,i)));
}
return p;
}
squareMatrix Transpose()
{
squareMatrix out(Dimension);
for (int i = 0; i < Dimension; ++i)
{
out(i,i) = Data[i][i];
for (int j = i+1; j < Dimension; ++j)
{
out(i,j) = Data[j][i];
out(j,i) = Data[i][j];
}
}
return out;
}
static squareMatrix Random(int dim,int bounds)
{
squareMatrix s(dim);
for (int i = 0; i < dim; ++i)
{
for (int j = 0; j < dim; ++j)
{
s(i,j) = (rand() - RAND_MAX/2) % bounds;
s(i,j) /= 3;
}
}
return s;
}
static squareMatrix Identity(int dim)
{
squareMatrix out(dim);
for (int i = 0; i < dim; ++i)
{
out(i,i) = 1;
}
return out;
}
bool isLowerTriangular()
{
for (int i = 0; i < Dimension; ++i)
{
for (int j = i+1; j < Dimension; ++j)
{
if (abs(Data[i][j]) > 0)
{
return false;
}
}
}
return true;
}
bool isUpperTriangular()
{
for (int i = 0; i < Dimension; ++i)
{
for (int j = i+1; j < Dimension; ++j)
{
if (abs(Data[j][i]) > 0)
{
return false;
}
}
}
return true;
}
private:
std::vector<std::vector<double>> Data;
};