This repository was archived by the owner on May 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathQRD.cs
259 lines (248 loc) · 8.26 KB
/
QRD.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
using System;
using System.Threading.Tasks;
namespace RCNet.MathTools.MatrixMath
{
/// <summary>
/// Implements the QR decomposition of a matrix.
/// </summary>
/// <remarks>
/// <para>
/// This class is based on a class from the public domain JAMA package.
/// RCNet project adds the parallel computation to improve the performance.
/// </para>
/// <para>
/// http://math.nist.gov/javanumerics/jama/
/// </para>
/// </remarks>
public class QRD
{
//Attributes
private readonly int _numOfRows;
private readonly int _numOfCols;
private readonly double[][] _QRData;
private readonly double[] _RDiagData;
//Constructor
/// <summary>
/// Creates an initialized instance.
/// </summary>
/// <param name="matrix">The matrix.</param>
public QRD(Matrix matrix)
{
//Initialization
_QRData = matrix.GetDataClone();
_numOfRows = matrix.NumOfRows;
_numOfCols = matrix.NumOfCols;
_RDiagData = new double[_numOfCols];
//Main loop
for (int k = 0; k < _numOfCols; k++)
{
//Compute 2-norm of k-th column
double norm = 0d;
for (int i = k; i < _numOfRows; i++)
{
norm = Matrix.Hypotenuse(norm, _QRData[i][k]);
}//i
if (norm != 0d)
{
// Form k-th Householder vector.
if (_QRData[k][k] < 0d)
{
norm = -norm;
}
for (int i = k; i < _numOfRows; i++)
{
_QRData[i][k] /= norm;
}
_QRData[k][k] += 1d;
//Apply transformation to remaining columns.
Parallel.For(k + 1, _numOfCols, j =>
{
double s = 0.0;
for (int i = k; i < _numOfRows; i++)
{
s += _QRData[i][k] * _QRData[i][j];
}
s = -s / _QRData[k][k];
for (int i = k; i < _numOfRows; i++)
{
_QRData[i][j] += s * _QRData[i][k];
}
});
}
_RDiagData[k] = -norm;
}//k
if (!FullRank)
{
throw new ArgumentException($"Matrix is rank deficient.", "matrix");
}
return;
}
//Properties
/// <summary>
/// Returns the Householder vectors.
/// </summary>
public Matrix H
{
get
{
Matrix result = new Matrix(_numOfRows, _numOfCols);
double[][] resultData = result.Data;
Parallel.For(0, _numOfRows, row =>
{
for (int col = 0; col < _numOfCols; col++)
{
if (row >= col)
{
resultData[row][col] = _QRData[row][col];
}
else
{
resultData[row][col] = 0d;
}
}//col
});//row
return result;
}//get
}//H
/// <summary>
/// Returns the upper triangular factor.
/// </summary>
public Matrix R
{
get
{
Matrix result = new Matrix(_numOfCols, _numOfCols);
double[][] resultData = result.Data;
Parallel.For(0, _numOfCols, i =>
{
for (int j = 0; j < _numOfCols; j++)
{
if (i < j)
{
resultData[i][j] = _QRData[i][j];
}
else if (i == j)
{
resultData[i][j] = _RDiagData[i];
}
else
{
resultData[i][j] = 0d;
}
}//j
});//i
return result;
}
}//R
/// <summary>
/// Generates and returns the (economy-sized) orthogonal factor.
/// </summary>
public Matrix Q
{
get
{
Matrix result = new Matrix(_numOfRows, _numOfCols);
double[][] resultData = result.Data;
for (int k = _numOfCols - 1; k >= 0; k--)
{
for (int row = 0; row < _numOfRows; row++)
{
resultData[row][k] = 0d;
}//row
resultData[k][k] = 1d;
for (int j = k; j < _numOfCols; j++)
{
if (_QRData[k][k] != 0d)
{
double s = 0d;
for (int i = k; i < _numOfRows; i++)
{
s += _QRData[i][k] * resultData[i][j];
}
s = -s / _QRData[k][k];
for (int i = k; i < _numOfRows; i++)
{
resultData[i][j] += s * _QRData[i][k];
}
}
}//j
}//k
return result;
}
}//Q
/// <summary>
/// Is full rank?
/// </summary>
public bool FullRank
{
get
{
for (int col = 0; col < _numOfCols; col++)
{
/*
if (_RDiagData[col] == 0)
{
return false;
}
*/
//Improved original zero condition to "close to zero" for the stability
if (Math.Abs(_RDiagData[col]) < 1E-20)
{
return false;
}
}
return true;
}
}
//Methods
//Instance methods
/// <summary>
/// Solves the least squares of A*X = B.
/// </summary>
/// <param name="matrixB">The matrix with as many rows as A and at least one column (desired values).</param>
public Matrix Solve(Matrix matrixB)
{
//Check the number of rows in matrix B
if (matrixB.NumOfRows != _numOfRows)
{
throw new ArgumentException($"Different number of rows in Matrix B.", "matrixB");
}
// Copy right hand side
int nx = matrixB.NumOfCols;
double[][] X = matrixB.GetDataClone();
// Compute Y = transpose(Q)*B
for (int k = 0; k < _numOfCols; k++)
{
for (int j = 0; j < nx; j++)
{
double s = 0.0;
for (int i = k; i < _numOfRows; i++)
{
s += _QRData[i][k] * X[i][j];
}
s = -s / _QRData[k][k];
for (int i = k; i < _numOfRows; i++)
{
X[i][j] += s * _QRData[i][k];
}
}
}
// Solve R*X = Y;
for (int k = _numOfCols - 1; k >= 0; k--)
{
for (int j = 0; j < nx; j++)
{
X[k][j] /= _RDiagData[k];
}
for (int i = 0; i < k; i++)
{
for (int j = 0; j < nx; j++)
{
X[i][j] -= X[k][j] * _QRData[i][k];
}
}
}
return (new Matrix(X).CreateSubMatrix(0, _numOfCols - 1, 0, nx - 1));
}
}//QRD
}//Namespace