-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathSparseQR.cs
More file actions
266 lines (220 loc) · 8.99 KB
/
SparseQR.cs
File metadata and controls
266 lines (220 loc) · 8.99 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
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
namespace CSparse.Complex.Factorization
{
using CSparse.Factorization;
using CSparse.Ordering;
using CSparse.Storage;
using System;
using System.Numerics;
/// <summary>
/// Sparse QR decomposition.
/// </summary>
/// <remarks>
/// See Chapter 5 (Orthogonal methods) in "Direct Methods for Sparse Linear Systems"
/// by Tim Davis.
/// </remarks>
public class SparseQR : SparseQR<Complex>
{
#region Static methods
/// <summary>
/// Creates a sparse QR factorization.
/// </summary>
/// <param name="A">Column-compressed matrix, symmetric positive definite.</param>
/// <param name="order">Ordering method to use (natural or A+A').</param>
public static SparseQR Create(CompressedColumnStorage<Complex> A, ColumnOrdering order)
{
return Create(A, order, null);
}
/// <summary>
/// Creates a sparse QR factorization.
/// </summary>
/// <param name="A">Column-compressed matrix, symmetric positive definite.</param>
/// <param name="order">Ordering method to use (natural or A+A').</param>
/// <param name="progress">Report progress (range from 0.0 to 1.0).</param>
public static SparseQR Create(CompressedColumnStorage<Complex> A, ColumnOrdering order,
IProgress<double> progress)
{
Check.NotNull(A, "A");
int m = A.RowCount;
int n = A.ColumnCount;
var C = new SparseQR(m, n);
if (m >= n)
{
var p = AMD.Generate(A, order);
// Ordering and symbolic analysis
C.SymbolicAnalysis(A, p, order == ColumnOrdering.Natural);
// Numeric QR factorization
C.Factorize(A, progress);
}
else
{
// Ax=b is underdetermined
var AT = A.Transpose();
var p = AMD.Generate(AT, order);
// Ordering and symbolic analysis
C.SymbolicAnalysis(AT, p, order == ColumnOrdering.Natural);
// Numeric QR factorization of A'
C.Factorize(AT, progress);
}
return C;
}
#endregion
private SparseQR(int rows, int columns)
: base(rows, columns)
{
}
/// <summary>
/// Solves a system of linear equations, <c>Ax = b</c>.
/// </summary>
/// <param name="input">The right hand side vector, <c>b</c>.</param>
/// <param name="result">The left hand side vector, <c>x</c>.</param>
/// <remarks>
/// Let A be a m-by-n matrix. If m >= n a least-squares problem (min |Ax-b|)
/// is solved. If m < n the underdetermined system is solved.
/// </remarks>
public override void Solve(Complex[] input, Complex[] result) => Solve(input.AsSpan(), result.AsSpan());
/// <summary>
/// Solves a system of linear equations, <c>Ax = b</c>.
/// </summary>
/// <param name="input">The right hand side vector, <c>b</c>.</param>
/// <param name="result">The left hand side vector, <c>x</c>.</param>
/// <remarks>
/// Let A be a m-by-n matrix. If m >= n a least-squares problem (min |Ax-b|)
/// is solved. If m < n the underdetermined system is solved.
/// </remarks>
public override void Solve(ReadOnlySpan<Complex> input, Span<Complex> result)
{
if (input.IsEmpty) throw new ArgumentNullException(nameof(input));
if (result.IsEmpty) throw new ArgumentNullException(nameof(result));
var x = new Complex[S.m2];
if (m >= n)
{
// x(0:m-1) = b(p(0:m-1)
Permutation.ApplyInverse(S.pinv, input, x, m);
// Apply Householder reflection to x.
for (int k = 0; k < n; k++)
{
ApplyHouseholder(Q, k, beta[k], x);
}
SolverHelper.SolveUpper(R, x); // x = R\x
// b(q(0:n-1)) = x(0:n-1)
Permutation.ApplyInverse(S.q, x, result, n);
}
else
{
// x(q(0:m-1)) = b(0:m-1)
Permutation.Apply(S.q, input, x, m);
SolverHelper.SolveUpperTranspose(R, x); // x = R'\x
// Apply Householder reflection to x.
for (int k = m - 1; k >= 0; k--)
{
ApplyHouseholder(Q, k, beta[k], x);
}
// b(0:n-1) = x(p(0:n-1))
Permutation.Apply(S.pinv, x, result, n);
}
}
/// <summary>
/// Solves a system of linear equations, <c>A'x = b</c>.
/// </summary>
/// <param name="input">The right hand side vector, <c>b</c>.</param>
/// <param name="result">The left hand side vector, <c>x</c>.</param>
public void SolveTranspose(Complex[] input, Complex[] result) => SolveTranspose(input.AsSpan(), result.AsSpan());
/// <summary>
/// Solves a system of linear equations, <c>A'x = b</c>.
/// </summary>
/// <param name="input">The right hand side vector, <c>b</c>.</param>
/// <param name="result">The left hand side vector, <c>x</c>.</param>
public void SolveTranspose(ReadOnlySpan<Complex> input, Span<Complex> result)
{
if (input.IsEmpty) throw new ArgumentNullException(nameof(input));
if (result.IsEmpty) throw new ArgumentNullException(nameof(result));
int m2 = S.m2;
var x = new Complex[m2];
if (m >= n)
{
// x(q(0:m-1)) = b(0:m-1)
Permutation.Apply(S.q, input, x, n);
SolverHelper.SolveUpperTranspose(R, x); // x = R'\x
// Apply Householder reflection to x.
for (int k = n - 1; k >= 0; k--)
{
ApplyHouseholder(Q, k, beta[k], x);
}
// b(0:n-1) = x(p(0:n-1))
Permutation.Apply(S.pinv, x, result, m);
}
else
{
// x(0:m-1) = b(p(0:m-1)
Permutation.ApplyInverse(S.pinv, input, x, n);
// Apply Householder reflection to x.
for (int k = 0; k < m; k++)
{
ApplyHouseholder(Q, k, beta[k], x);
}
SolverHelper.SolveUpper(R, x); // x = R\x
// b(q(0:n-1)) = x(0:n-1)
Permutation.ApplyInverse(S.q, x, result, m);
}
}
/// <summary>
/// Create a Householder reflection [v,beta,s]=house(x), overwrite x with v,
/// where (I-beta*v*v')*x = s*e1 and e1 = [1 0 ... 0]'.
/// </summary>
/// <remarks>
/// Note that this CXSparse version is different than CSparse. See Higham,
/// Accuracy and Stability of Num Algorithms, 2nd ed, 2002, page 357.
/// </remarks>
protected override Complex CreateHouseholder(Complex[] x, int offset, ref double beta, int n)
{
Complex s = Complex.Zero;
int i;
if (x == null) return -1; // check inputs
// s = norm(x)
for (i = 0; i < n; i++)
{
s += x[offset + i] * Complex.Conjugate(x[offset + i]);
}
s = Complex.Sqrt(s);
if (s == 0)
{
beta = 0;
x[offset] = 1;
}
else
{
// s = sign(x[0]) * norm (x) ;
if (x[offset] != 0)
{
s *= x[offset] / Complex.Abs(x[offset]);
}
x[offset] += s;
beta = 1 / (Complex.Conjugate(s) * x[offset]).Real;
}
return (-s);
}
/// <summary>
/// Apply the ith Householder vector to x.
/// </summary>
protected override bool ApplyHouseholder(CompressedColumnStorage<Complex> V, int i, double beta, Complex[] x)
{
int p = 0;
Complex tau = Complex.Zero;
if (x == null) return false; // check inputs
var vp = V.ColumnPointers;
var vi = V.RowIndices;
var vx = V.Values;
var vpi1 = vp[i + 1];
for (p = vp[i]; p < vpi1; p++) // tau = v'*x
{
tau += Complex.Conjugate(vx[p]) * x[vi[p]];
}
tau *= beta; // tau = beta*(v'*x)
for (p = vp[i]; p < vpi1; p++) // x = x - v*tau
{
x[vi[p]] -= vx[p] * tau;
}
return true;
}
}
}