-
Notifications
You must be signed in to change notification settings - Fork 23
/
linalg.cpp
334 lines (306 loc) · 8.2 KB
/
linalg.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
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
/*
* linalg.cpp
*
* Linear algebra routines
*
* Created on: 13 Nov 2011
* Author: dan
*/
#include "linalg.h"
#include<math.h>
#include<vector>
#include "numerics.h"
using namespace std;
BlockPlusDiagonalMatrix::BlockPlusDiagonalMatrix(vector<integer> &_Masked, vector<integer> &_Unmasked)
{
Masked = &_Masked;
Unmasked = &_Unmasked;
NumUnmasked = Unmasked->size();
NumMasked = Masked->size();
Block.resize(NumUnmasked*NumUnmasked);
Diagonal.resize(NumMasked);
}
void BlockPlusDiagonalMatrix::compare(scalar *Flat)
{
integer nDims = NumUnmasked + NumMasked;
scalar meanerr = 0.0;
scalar maxerr = 0.0;
integer nerr = 0;
integer ntotal = 0;
for (integer ii = 0; ii < NumUnmasked; ii++)
{
integer i = (*Unmasked)[ii];
for (integer jj = 0; jj < NumUnmasked; jj++)
{
integer j = (*Unmasked)[jj];
scalar x = Block[ii*NumUnmasked + jj];
scalar y = Flat[i*nDims + j];
scalar err = fabs(x - y);
ntotal++;
if (err > 0)
{
nerr++;
meanerr += err;
if (err > maxerr)
maxerr = err;
}
}
}
for (integer ii = 0; ii < NumMasked; ii++)
{
integer i = (*Masked)[ii];
scalar x = Diagonal[ii];
scalar y = Flat[i*nDims + i];
scalar err = fabs(x - y);
ntotal++;
if (err > 0)
{
nerr++;
meanerr += err;
if (err > maxerr)
maxerr = err;
}
}
if (nerr)
{
meanerr /= nerr;
cout << "Comparison error n=" << nerr << " (" << (100.0*nerr) / ntotal << "%), mean=" << meanerr << ", max=" << maxerr << endl;
}
else
cout << "No comparison error." << endl;
}
// Cholesky Decomposition
// In provides upper triangle of input matrix (In[i*D + j] >0 if j>=i);
// which is the top half of a symmetric matrix
// Out provides lower triange of output matrix (Out[i*D + j] >0 if j<=i);
// such that Out' * Out = In.
// D is number of dimensions
//
// returns 0 if OK, returns 1 if matrix is not positive definite
integer Cholesky(SafeArray<scalar> &In, SafeArray<scalar> &Out, integer D)
{
integer i, j, k;
scalar sum;
// empty output array
for (i=0; i<D*D; i++) Out[i] = 0;
// main bit
for (i=0; i<D; i++) {
for (j=i; j<D; j++) { // j>=i
sum = In[i*D + j];
for (k=i-1; k>=0; k--) sum -= Out[i*D + k] * Out[j*D + k]; // i,j >= k
if (i==j) {
if (sum <=0) return(1); // Cholesky decomposition has failed
Out[i*D + i] = (scalar)sqrt(sum);
}
else {
Out[j*D + i] = sum/Out[i*D + i];
}
}
}
return 0; // for sucess
}
integer MaskedCholesky(SafeArray<scalar> &In, SafeArray<scalar> &Out, integer D, vector<integer> &Masked, vector<integer> &Unmasked)
{
integer i, j, k;
integer ii, jj, kk;
scalar sum;
integer NumUnmasked = (integer)Unmasked.size();
// empty output array
for (i = 0; i<D*D; i++) Out[i] = 0;
// main bit for unmasked features
//for (i = 0; i<D; i++)
for (ii = 0; ii < NumUnmasked; ii++)
{
i = Unmasked[ii];
//for (j = i; j<D; j++)
for (jj = ii; jj < NumUnmasked; jj++)
{ // j>=i
j = Unmasked[jj];
sum = In[i*D + j];
//for (k = i - 1; k >= 0; k--)
for (kk = ii - 1; kk >= 0; kk--)
{
k = Unmasked[kk];
sum -= Out[i*D + k] * Out[j*D + k]; // i,j >= k
}
if (i == j) {
if (sum <= 0) return(1); // Cholesky decomposition has failed
Out[i*D + i] = (scalar)sqrt(sum);
}
else {
Out[j*D + i] = sum / Out[i*D + i];
}
}
}
// main bit for masked features
for (ii = 0; ii < (integer)Masked.size(); ii++)
{
i = Masked[ii];
scalar sum = In[i*D + i];
if (sum <= 0)
return 1; // Cholesky failed
Out[i*D + i] = (scalar)sqrt(sum);
}
return 0; // for sucess
}
integer BPDCholesky(BlockPlusDiagonalMatrix &In, BlockPlusDiagonalMatrix &Out)
{
integer ii, jj, kk;
scalar sum;
integer NumUnmasked = (integer)In.NumUnmasked;
// main bit for unmasked features
for (ii = 0; ii < NumUnmasked; ii++)
{
for (jj = ii; jj < NumUnmasked; jj++)
{
sum = In.Block[ii*NumUnmasked + jj];
for (kk = ii - 1; kk >= 0; kk--)
{
sum -= Out.Block[ii*NumUnmasked + kk] * Out.Block[jj*NumUnmasked + kk];
}
if (ii == jj) {
if (sum <= 0) return(1); // Cholesky decomposition has failed
Out.Block[ii*NumUnmasked + ii] = (scalar)sqrt(sum);
}
else {
Out.Block[jj*NumUnmasked + ii] = sum / Out.Block[ii*NumUnmasked + ii];
}
}
}
// main bit for masked features
for (ii = 0; ii < (integer)In.NumMasked; ii++)
{
scalar sum = In.Diagonal[ii];
if (sum <= 0)
return 1; // Cholesky failed
Out.Diagonal[ii] = (scalar)sqrt(sum);
}
return 0; // for success
}
// Solve a set of linear equations M*Out = x.
// Where M is lower triangular (M[i*D + j] >0 if j>=i);
// D is number of dimensions
void TriSolve(SafeArray<scalar> &M, SafeArray<scalar> &x,
SafeArray<scalar> &Out, integer D)
{
for(integer i=0; i<D; i++)
{
scalar *MiD = &M[i*D];
scalar sum = x[i];
for(integer j=0; j<i; j++) // j<i
//sum += M[i*D + j] * Out[j];
sum += MiD[j] * Out[j];
//Out[i] = - sum / M[i*D + i];
Out[i] = - sum / MiD[i];
}
}
//void MaskedTriSolve(SafeArray<scalar> &M, SafeArray<scalar> &x,
// SafeArray<scalar> &Out, integer D,
// vector<integer> &Masked, vector<integer> &Unmasked)
//{
// integer NumUnmasked = (integer)Unmasked.size();
// integer NumMasked = (integer)Masked.size();
// for (integer ii = 0; ii < NumUnmasked; ii++)
// {
// integer i = Unmasked[ii];
// scalar sum = x[i];
// for (integer jj = 0; jj < ii; jj++) // j<i
// {
// integer j = Unmasked[jj];
// sum += M[i*D + j] * Out[j];
// }
// Out[i] = - sum / M[i*D + i];
// }
// for (integer ii = 0; ii < NumMasked; ii++)
// {
// integer i = Masked[ii];
// Out[i] = -x[i] / M[i*D + i];
// }
//}
// fast version with pointers and restricts
void FastMaskedTriSolve(scalar * __restrict M, scalar * __restrict x,
scalar * __restrict Out, integer D,
integer * __restrict Masked, integer * __restrict Unmasked,
integer NumMasked, integer NumUnmasked)
{
for (integer ii = 0; ii < NumUnmasked; ii++)
{
const integer i = Unmasked[ii];
scalar sum = x[i];
scalar * __restrict MiD = M + i*D;
for (integer jj = 0; jj < ii; jj++) // j<i
{
const integer j = Unmasked[jj];
sum += MiD[j] * Out[j];
}
Out[i] = -sum / MiD[i];
}
for (integer ii = 0; ii < NumMasked; ii++)
{
const integer i = Masked[ii];
Out[i] = -x[i] / M[i*D + i];
}
}
void MaskedTriSolve(SafeArray<scalar> &M, SafeArray<scalar> &x,
SafeArray<scalar> &Out, integer D,
vector<integer> &Masked, vector<integer> &Unmasked)
{
FastMaskedTriSolve(&(M[0]), &(x[0]), &(Out[0]), D, &(Masked[0]), &(Unmasked[0]), Masked.size(), Unmasked.size());
}
//void BPDTriSolve(BlockPlusDiagonalMatrix &M, SafeArray<scalar> &x,
// SafeArray<scalar> &Out)
//{
// for (integer ii = 0; ii < M.NumUnmasked; ii++)
// {
// const integer i = M.Unmasked[ii];
// scalar sum = x[i];
// //scalar * __restrict MiD = &(M.Block[ii*M.NumUnmasked]);
// for (integer jj = 0; jj < ii; jj++) // j<i
// {
// const integer j = M.Unmasked[jj];
// //sum += MiD[jj] * Out[j];
// sum += M.Block[ii*M.NumUnmasked + jj] * Out[j];
// }
// //Out[i] = -sum / MiD[ii];
// Out[i] = -sum / M.Block[ii*M.NumUnmasked + ii];
// }
// for (integer ii = 0; ii < M.NumMasked; ii++)
// {
// const integer i = M.Masked[ii];
// Out[i] = -x[i] / M.Diagonal[ii];
// }
//}
void BPDTriSolve(BlockPlusDiagonalMatrix &M, SafeArray<scalar> &x,
SafeArray<scalar> &Out)
{
const integer NumUnmasked = M.NumUnmasked;
const integer NumMasked = M.NumMasked;
const scalar * __restrict ptr_x = &(x[0]);
scalar * __restrict ptr_Out = &(Out[0]);
if (NumUnmasked)
{
const integer * __restrict Unmasked = &((*M.Unmasked)[0]);
for (integer ii = 0; ii < NumUnmasked; ii++)
{
const integer i = Unmasked[ii];
scalar sum = ptr_x[i];
const scalar * __restrict row = &(M.Block[ii*M.NumUnmasked]);
for (integer jj = 0; jj < ii; jj++) // j<i
{
const integer j = Unmasked[jj];
sum += row[jj] * ptr_Out[j];
}
ptr_Out[i] = -sum / row[ii];
}
}
if (NumMasked)
{
const integer * __restrict Masked = &((*M.Masked)[0]);
const scalar * __restrict Diagonal = &(M.Diagonal[0]);
for (integer ii = 0; ii < NumMasked; ii++)
{
const integer i = Masked[ii];
Out[i] = -ptr_x[i] / Diagonal[ii];
}
}
}