namespace Mapack { using System; /// Cholesky Decomposition of a symmetric, positive definite matrix. /// /// For a symmetric, positive definite matrix A, the Cholesky decomposition is a /// lower triangular matrix L so that A = L * L'. /// If the matrix is not symmetric or positive definite, the constructor returns a partial /// decomposition and sets two internal variables that can be queried using the /// and properties. /// public class CholeskyDecomposition { private Matrix L; private bool symmetric; private bool positiveDefinite; /// Construct a Cholesky Decomposition. 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; } } } /// Returns if the matrix is symmetric. public bool Symmetric { get { return this.symmetric; } } /// Returns if the matrix is positive definite. public bool PositiveDefinite { get { return this.positiveDefinite; } } /// Returns the left triangular factor L so that A = L * L'. public Matrix LeftTriangularFactor { get { return this.L; } } /// Solves a set of equation systems of type A * X = B. /// Right hand side matrix with as many rows as A and any number of columns. /// Matrix X so that L * L' * X = B. /// Matrix dimensions do not match. /// Matrix is not symmetrix and positive definite. 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."); } // Solve L*Y = B; int dimension = L.Rows; int count = value.Columns; Matrix B = (Matrix)value.Clone(); double[][] l = L.Array; // Solve L*Y = B; 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]; } } // Solve L'*X = Y; 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; } } }