|
|
namespace Mapack |
|
|
{ |
|
|
using System; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class CholeskyDecomposition |
|
|
{ |
|
|
private Matrix L; |
|
|
private bool symmetric; |
|
|
private bool positiveDefinite; |
|
|
|
|
|
|
|
|
public CholeskyDecomposition(Matrix value) |
|
|
{ |
|
|
if (value == null) |
|
|
{ |
|
|
throw new ArgumentNullException("value"); |
|
|
} |
|
|
|
|
|
if (!value.Square) |
|
|
{ |
|
|
throw new ArgumentException("Matrix is not square.", "value"); |
|
|
} |
|
|
|
|
|
int dimension = value.Rows; |
|
|
L = new Matrix(dimension, dimension); |
|
|
|
|
|
double[][] a = value.Array; |
|
|
double[][] l = L.Array; |
|
|
|
|
|
this.positiveDefinite = true; |
|
|
this.symmetric = true; |
|
|
|
|
|
for (int j = 0; j < dimension; j++) |
|
|
{ |
|
|
double[] Lrowj = l[j]; |
|
|
double d = 0.0; |
|
|
for (int k = 0; k < j; k++) |
|
|
{ |
|
|
double[] Lrowk = l[k]; |
|
|
double s = 0.0; |
|
|
for (int i = 0; i < k; i++) |
|
|
{ |
|
|
s += Lrowk[i] * Lrowj[i]; |
|
|
} |
|
|
Lrowj[k] = s = (a[j][k] - s) / l[k][k]; |
|
|
d = d + s*s; |
|
|
|
|
|
this.symmetric = this.symmetric & (a[k][j] == a[j][k]); |
|
|
} |
|
|
|
|
|
d = a[j][j] - d; |
|
|
|
|
|
this.positiveDefinite = this.positiveDefinite & (d > 0.0); |
|
|
l[j][j] = Math.Sqrt(Math.Max(d,0.0)); |
|
|
for (int k = j + 1; k < dimension; k++) |
|
|
{ |
|
|
l[j][k] = 0.0; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public bool Symmetric |
|
|
{ |
|
|
get |
|
|
{ |
|
|
return this.symmetric; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public bool PositiveDefinite |
|
|
{ |
|
|
get |
|
|
{ |
|
|
return this.positiveDefinite; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public Matrix LeftTriangularFactor |
|
|
{ |
|
|
get |
|
|
{ |
|
|
return this.L; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Matrix Solve(Matrix value) |
|
|
{ |
|
|
if (value == null) |
|
|
{ |
|
|
throw new ArgumentNullException("value"); |
|
|
} |
|
|
|
|
|
if (value.Rows != L.Rows) |
|
|
{ |
|
|
throw new ArgumentException("Matrix dimensions do not match."); |
|
|
} |
|
|
|
|
|
if (!this.symmetric) |
|
|
{ |
|
|
throw new InvalidOperationException("Matrix is not symmetric."); |
|
|
} |
|
|
|
|
|
if (!this.positiveDefinite) |
|
|
{ |
|
|
throw new InvalidOperationException("Matrix is not positive definite."); |
|
|
} |
|
|
|
|
|
|
|
|
int dimension = L.Rows; |
|
|
int count = value.Columns; |
|
|
|
|
|
Matrix B = (Matrix)value.Clone(); |
|
|
double[][] l = L.Array; |
|
|
|
|
|
|
|
|
for (int k = 0; k < dimension; k++) |
|
|
{ |
|
|
for (int j = 0; j < count; j++) |
|
|
{ |
|
|
for (int i = 0; i < k; i++) |
|
|
{ |
|
|
B[k, j] -= B[i, j] * l[k][i]; |
|
|
} |
|
|
B[k, j] /= l[k][k]; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
for (int k = dimension - 1; k >= 0; k--) |
|
|
{ |
|
|
for (int j = 0; j < count; j++) |
|
|
{ |
|
|
for (int i = k + 1; i < dimension; i++) |
|
|
{ |
|
|
B[k, j] -= B[i, j] * L[i, k]; |
|
|
} |
|
|
B[k, j] /= l[k][k]; |
|
|
} |
|
|
} |
|
|
|
|
|
return B; |
|
|
} |
|
|
} |
|
|
} |
|
|
|