-
Notifications
You must be signed in to change notification settings - Fork 0
/
SPDMatrix.cs
375 lines (311 loc) · 11.4 KB
/
SPDMatrix.cs
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
using System;
namespace MNS
{
/// <summary>
/// Encapsulates a lower triangular part of a symmetric positive definite matrix
/// </summary>
public sealed class SPDMatrix : SMatrix, ICholesky
{
private bool isFactorized = false;
public SPDMatrix(double[] d, int n, int nmax = 0) : base(d, n, nmax) {}
public void Factorize()
{
// Computes Cholesky factor of the symmetric positive-definite matrix
if (isFactorized)
return;
for (int i = 0; i < N; ++i)
{
for (int k = 0; k <= i; ++k)
{
double s = 0.0d;
for (int j = 0; j < k; ++j)
s += D[j + i * (i + 1) / 2] * D[j + k * (k + 1) / 2]; // D[i][j] * D[k][j]
if (i == k)
{
int ii = i + i * (i + 1) / 2;
double d = D[i + i * (i + 1) / 2] - s;
if (d <= Precision.DoubleEpsilon)
{
// Matrix is not positive definite
isFactorized = false;
throw new ArithmeticException("Matrix is not positive definite.");
}
D[ii] = Math.Sqrt(d);
}
else
{
int ik = k + i * (i + 1) / 2;
D[ik] = (D[ik] - s) / D[k + k * (k + 1) / 2]; // (D[i][k] - s)) / D[k][k]
}
}
}
isFactorized = true;
}
public bool IsFactorized()
{
return isFactorized;
}
public double[] Solve(double[] b)
{
// Solves the linear equations system using the Cholesky decomposition
if(!isFactorized)
throw new InvalidOperationException("Matrix is not factorized.");
if (b.Length < N)
throw new ArgumentException("b.Length");
double[] r = new double[N];
Array.Copy(b, r, N);
double s;
for (int i = 0; i < N; ++i)
{
s = r[i];
for (int j = 0; j < i; ++j)
s -= D[j + i * (i + 1) / 2] * r[j];
r[i] = s / D[i + i * (i + 1) / 2];
}
for (int i = N - 1; i >= 0; --i)
{
r[i] /= D[i + i * (i + 1) / 2];
for (int j = 0; j < i; ++j)
r[j] -= D[j + i * (i + 1) / 2] * r[i];
}
return r;
}
public double GetNorm1()
{
int ij;
double norm1 = 0.0d;
for (int j = 0; j < N; ++j)
{
double s = 0.0d;
for (int i = 0; i < N; ++i)
{
ij = GetIndex(i, j);
s += Math.Abs(D[ij]);
}
if (s > norm1)
norm1 = s;
}
return norm1;
}
public double GetCond()
{
double norm = GetNorm1();
double cond = GetCond(norm);
return cond;
}
public double GetCond(double matnorm, int niter = 5)
{
// HAGER’S ESTIMATOR
// Returns the L1 condition number of a matrix estimation.
//
// Reference:
// 1) C. P. Bras, W. W. Hager, and J. J. Judice,
// An investigation of feasible descent algorithms for estimating the condition number of a matrix,
// TOP, Journal of the Spanish Society of Statistics and Operations Research, 20 (2012), 791-809
// https://link.springer.com/article/10.1007/s11750-010-0161-9
// http://users.clas.ufl.edu/hager/papers/condition.pdf
// 2) William Hager. Condition Estimates, SIAM Journal on Scientific and Statistical Computing,
// Volume 5, Number 2, June 1984, pages 311-316.
//
if (!isFactorized)
throw new InvalidOperationException("Matrix is not factorized.");
double gamma = 0.0d;
double[] x = new double[N];
for (int i = 0; i < N; ++i)
x[i] = 1.0d / N;
for (int it = 0; it < niter; ++it)
{
double[] z = Solve(x);
gamma = 0.0d;
for (int i = 0; i < N; ++i)
{
gamma += Math.Abs(z[i]);
z[i] = Math.Sign(z[i]);
}
z = Solve(z);
double zx = 0.0d;
for (int i = 0; i < N; ++i)
zx += z[i] * x[i];
int idx = 0;
for (int i = 0; i < N; ++i)
{
z[i] = Math.Abs(z[i]);
if (z[i] > z[idx])
idx = i;
}
if (z[idx] <= zx)
break;
for (int i = 0; i < N; ++i)
x[i] = 0.0d;
x[idx] = 1.0d;
}
double cond = gamma * matnorm;
return cond;
}
public double GetCond2012(double matnorm, int niter = 5)
{
// HAGER’S CONDITIONAL GRADIENT ESTIMATOR
// Returns the L1 condition number of a matrix estimation.
//
// Reference:
// 1) C. P. Bras, W. W. Hager, and J. J. Judice,
// An investigation of feasible descent algorithms for estimating the condition number of a matrix,
// TOP, Journal of the Spanish Society of Statistics and Operations Research, 20 (2012), 791-809
// https://link.springer.com/article/10.1007/s11750-010-0161-9
// http://users.clas.ufl.edu/hager/papers/condition.pdf
// 2) William Hager. Condition Estimates, SIAM Journal on Scientific and Statistical Computing,
// Volume 5, Number 2, June 1984, pages 311-316.
//
if (!isFactorized)
throw new InvalidOperationException("Matrix is not factorized.");
double gamma = 0.0d;
double[] x = new double[N];
for (int i = 0; i < N; ++i)
x[i] = 1.0d / N;
for (int it = 0; it < niter; ++it)
{
double[] z = Solve(x);
gamma = 0.0d;
for (int i = 0; i < N; ++i)
{
gamma += Math.Abs(z[i]);
z[i] = Math.Sign(z[i]);
}
z = Solve(z);
double zx = 0.0d;
for (int i = 0; i < N; ++i)
zx += z[i] * x[i];
int idx = 0;
for (int i = 0; i < N; ++i)
{
if (z[i] > z[idx])
idx = i;
}
if (z[idx] <= zx)
break;
for (int i = 0; i < N; ++i)
x[i] = 0.0d;
x[idx] = 1.0d;
}
double cond = gamma * matnorm;
return cond;
}
public void UpdateAdd(double[] d)
{
// Updates the Cholesky factor after a symmetric column/row addition
// d - new matrix column
// d.Length must be N + 1
if (!isFactorized)
throw new InvalidOperationException("Matrix is not factorized.");
if (d.Length < N + 1)
throw new ArgumentException("d.Length");
if ((N + 1) > NMax)
throw new InvalidOperationException("(N + 1) > NMax");
if (D.Length < (N + 1) * (N + 2) / 2)
throw new ArgumentException("D.Length");
// Calculate a new row of the matrix decomposition
// Solve L * y = d
double s;
int i, j, k;
for (j = 0; j < N; ++j)
{
s = d[j];
for (k = 0; k < j; ++k)
{
s -= D[k + j * (j + 1) / 2] * d[k];
}
d[j] = s / D[j + j * (j + 1) / 2];
}
s = 0.0d;
for (i = 0; i < N; ++i)
s += d[i] * d[i];
s = d[N] - s;
if (s <= Precision.DoubleEpsilon)
throw new ArithmeticException("Matrix is not positive definite.");
d[N] = Math.Sqrt(s);
int size = N * (N + 1) / 2;
for (i = 0; i <= N; ++i)
{
D[size + i] = d[i];
}
++N;
}
public void UpdateDel(int ix)
{
if (!isFactorized)
throw new InvalidOperationException("Matrix is not factorized.");
int nm1 = N - 1;
// Calculates a new Cholesky factor for a matrix with deleted row and column
if (ix < 0 || ix > nm1)
{
throw new ArgumentOutOfRangeException("ix < 0 || ix > N - 1");
}
if (ix < nm1)
{
int ii1, ii2;
double m1, m2, c, s;
for (int i = ix; i < nm1; ++i)
{
int ip1 = i + 1;
ii1 = i + ip1 * (ip1 + 1) / 2;
ii2 = ip1 + ip1 * (ip1 + 1) / 2;
m1 = D[ii1];
m2 = D[ii2];
GetGivensRotation(m1, m2, out c, out s);
D[ii1] = c * m1 + s * m2;
D[ii2] = -s * m1 + c * m2;
if (i < N - 2)
{
for (int k = i + 2; k < N; ++k)
{
ii1 = i + k * (k + 1) / 2;
ii2 = ip1 + k * (k + 1) / 2;
m1 = D[ii1];
m2 = D[ii2];
D[ii1] = c * m1 + s * m2;
D[ii2] = -s * m1 + c * m2;
}
}
}
Compress(ix);
} // if ( ix == n - 1 ) then we do nothing besides
--N; // reducing the matrix dimension
}
//----------------
private void Compress(int ix)
{
if (ix < N - 1)
{
int ij = (ix) * (ix + 1) / 2;
for (int i = ix + 1; i < N; ++i)
{
for (int j = 0; j < i; ++j)
{
D[ij++] = D[j + i * (i + 1) / 2];
}
}
}
}
private void GetGivensRotation(double x, double y, out double c, out double s)
{
// Computes a Givens plane rotation for values x and y
double ax, ay, t, u, w, r;
ax = Math.Abs(x);
ay = Math.Abs(y);
t = Math.Max(ax, ay);
u = Math.Min(ax, ay);
if (t != 0.0d)
{
w = u / t;
r = t * Math.Sqrt(1.0d + w * w);
c = x / r;
s = y / r;
}
else
{
c = 1.0d;
s = 0.0d;
};
}
}
}