|
|
namespace Mapack |
|
|
{ |
|
|
using System; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class QrDecomposition |
|
|
{ |
|
|
private Matrix QR; |
|
|
private double[] Rdiag; |
|
|
|
|
|
|
|
|
public QrDecomposition(Matrix value) |
|
|
{ |
|
|
if (value == null) |
|
|
{ |
|
|
throw new ArgumentNullException("value"); |
|
|
} |
|
|
|
|
|
this.QR = (Matrix) value.Clone(); |
|
|
double[][] qr = this.QR.Array; |
|
|
int m = value.Rows; |
|
|
int n = value.Columns; |
|
|
this.Rdiag = new double[n]; |
|
|
|
|
|
for (int k = 0; k < n; k++) |
|
|
{ |
|
|
|
|
|
double nrm = 0; |
|
|
for (int i = k; i < m; i++) |
|
|
{ |
|
|
nrm = Hypotenuse(nrm,qr[i][k]); |
|
|
} |
|
|
|
|
|
if (nrm != 0.0) |
|
|
{ |
|
|
|
|
|
if (qr[k][k] < 0) |
|
|
{ |
|
|
nrm = -nrm; |
|
|
} |
|
|
|
|
|
for (int i = k; i < m; i++) |
|
|
{ |
|
|
qr[i][k] /= nrm; |
|
|
} |
|
|
|
|
|
qr[k][k] += 1.0; |
|
|
|
|
|
|
|
|
for (int j = k+1; j < n; j++) |
|
|
{ |
|
|
double s = 0.0; |
|
|
|
|
|
for (int i = k; i < m; i++) |
|
|
{ |
|
|
s += qr[i][k]*qr[i][j]; |
|
|
} |
|
|
|
|
|
s = -s/qr[k][k]; |
|
|
|
|
|
for (int i = k; i < m; i++) |
|
|
{ |
|
|
qr[i][j] += s*qr[i][k]; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
this.Rdiag[k] = -nrm; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Matrix Solve(Matrix value) |
|
|
{ |
|
|
if (value == null) |
|
|
{ |
|
|
throw new ArgumentNullException("value"); |
|
|
} |
|
|
|
|
|
if (value.Rows != QR.Rows) |
|
|
{ |
|
|
throw new ArgumentException("Matrix row dimensions must agree."); |
|
|
} |
|
|
|
|
|
if (!this.FullRank) |
|
|
{ |
|
|
throw new InvalidOperationException("Matrix is rank deficient."); |
|
|
} |
|
|
|
|
|
|
|
|
int count = value.Columns; |
|
|
Matrix X = value.Clone(); |
|
|
int m = QR.Rows; |
|
|
int n = QR.Columns; |
|
|
double[][] qr = QR.Array; |
|
|
|
|
|
|
|
|
for (int k = 0; k < n; k++) |
|
|
{ |
|
|
for (int j = 0; j < count; j++) |
|
|
{ |
|
|
double s = 0.0; |
|
|
|
|
|
for (int i = k; i < m; i++) |
|
|
{ |
|
|
s += qr[i][k] * X[i,j]; |
|
|
} |
|
|
|
|
|
s = -s / qr[k][k]; |
|
|
|
|
|
for (int i = k; i < m; i++) |
|
|
{ |
|
|
X[i,j] += s * qr[i][k]; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
for (int k = n-1; k >= 0; k--) |
|
|
{ |
|
|
for (int j = 0; j < count; j++) |
|
|
{ |
|
|
X[k,j] /= Rdiag[k]; |
|
|
} |
|
|
|
|
|
for (int i = 0; i < k; i++) |
|
|
{ |
|
|
for (int j = 0; j < count; j++) |
|
|
{ |
|
|
X[i,j] -= X[k,j] * qr[i][k]; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
return X.Submatrix(0, n-1, 0, count-1); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public bool FullRank |
|
|
{ |
|
|
get |
|
|
{ |
|
|
int columns = this.QR.Columns; |
|
|
for (int i = 0; i < columns; i++) |
|
|
{ |
|
|
if (this.Rdiag[i] == 0) |
|
|
{ |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
return true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public Matrix UpperTriangularFactor |
|
|
{ |
|
|
get |
|
|
{ |
|
|
int n = this.QR.Columns; |
|
|
Matrix X = new Matrix(n, n); |
|
|
double[][] x = X.Array; |
|
|
double[][] qr = QR.Array; |
|
|
for (int i = 0; i < n; i++) |
|
|
{ |
|
|
for (int j = 0; j < n; j++) |
|
|
{ |
|
|
if (i < j) |
|
|
{ |
|
|
x[i][j] = qr[i][j]; |
|
|
} |
|
|
else if (i == j) |
|
|
{ |
|
|
x[i][j] = Rdiag[i]; |
|
|
} |
|
|
else |
|
|
{ |
|
|
x[i][j] = 0.0; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
return X; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public Matrix OrthogonalFactor |
|
|
{ |
|
|
get |
|
|
{ |
|
|
Matrix X = new Matrix(QR.Rows, QR.Columns); |
|
|
double[][] x = X.Array; |
|
|
double[][] qr = QR.Array; |
|
|
for (int k = QR.Columns - 1; k >= 0; k--) |
|
|
{ |
|
|
for (int i = 0; i < QR.Rows; i++) |
|
|
{ |
|
|
x[i][k] = 0.0; |
|
|
} |
|
|
|
|
|
x[k][k] = 1.0; |
|
|
for (int j = k; j < QR.Columns; j++) |
|
|
{ |
|
|
if (qr[k][k] != 0) |
|
|
{ |
|
|
double s = 0.0; |
|
|
|
|
|
for (int i = k; i < QR.Rows; i++) |
|
|
{ |
|
|
s += qr[i][k] * x[i][j]; |
|
|
} |
|
|
|
|
|
s = -s / qr[k][k]; |
|
|
|
|
|
for (int i = k; i < QR.Rows; i++) |
|
|
{ |
|
|
x[i][j] += s * qr[i][k]; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
return X; |
|
|
} |
|
|
} |
|
|
|
|
|
private static double Hypotenuse(double a, double b) |
|
|
{ |
|
|
if (Math.Abs(a) > Math.Abs(b)) |
|
|
{ |
|
|
double r = b / a; |
|
|
return Math.Abs(a) * Math.Sqrt(1 + r * r); |
|
|
} |
|
|
|
|
|
if (b != 0) |
|
|
{ |
|
|
double r = a / b; |
|
|
return Math.Abs(b) * Math.Sqrt(1 + r * r); |
|
|
} |
|
|
|
|
|
return 0.0; |
|
|
} |
|
|
} |
|
|
} |
|
|
|