public void CholeskyDecompositionConstructorTest() { // Based on tests by Ken Johnson double[,] value = // positive-definite { { 2, -1, 0 }, { -1, 2, -1 }, { 0, -1, 2 } }; double[,] expected = { { 1.4142, 0.0000, 0.0000 }, { -0.7071, 1.2247, 0.0000 }, { 0.0000, -0.8165, 1.1547 } }; var chol = new CholeskyDecomposition(value); double[,] L = chol.LeftTriangularFactor; Assert.IsTrue(Matrix.IsEqual(L, expected, 0.0001)); // Decomposition Identity Assert.IsTrue(Matrix.IsEqual(Matrix.Multiply(L, L.Transpose()), value, 1e-3)); Assert.IsTrue(Matrix.IsEqual(chol.Reverse(), value, 1e-3)); Assert.AreEqual(new LuDecomposition(value).Determinant, chol.Determinant, 1e-10); Assert.IsTrue(chol.IsPositiveDefinite); //Assert.AreEqual(true, chol.Symmetric); }
public void CholeskyDecompositionConstructorTest4() { // Based on tests by Ken Johnson double[,] value = // positive-definite { { 2, 0, 0 }, { -1, 2, 0 }, { 0, -1, 2 } }; double[,] expected = { { 1.4142, 0.0000, 0.0000 }, { -0.7071, 1.2247, 0.0000 }, { 0.0000, -0.8165, 1.1547 } }; var chol = new CholeskyDecomposition(value, false, valueType: MatrixType.LowerTriangular); double[,] L = chol.LeftTriangularFactor; Assert.IsTrue(Matrix.IsEqual(L, expected, 1e-4)); Assert.AreEqual(4, chol.Determinant, 1e-10); Assert.IsTrue(chol.IsPositiveDefinite); Assert.IsTrue(Matrix.IsEqual(chol.Reverse(), value.GetSymmetric(type: MatrixType.LowerTriangular), 1e-4)); double[,] expected2 = { { 1.0000, 0.0000, 0.0000 }, { -0.5000, 1.0000, 0.0000 }, { 0.0000, -0.6667, 1.0000 } }; chol = new CholeskyDecomposition(value, robust: true, valueType: MatrixType.LowerTriangular); L = chol.LeftTriangularFactor; Assert.IsTrue(Matrix.IsEqual(L, expected2, 1e-4)); Assert.IsTrue(Matrix.IsEqual(chol.Reverse(), value.GetSymmetric(type: MatrixType.LowerTriangular), 1e-6)); Assert.IsTrue(chol.IsPositiveDefinite); Assert.AreEqual(4, chol.Determinant, 1e-10); }
public void LogDeterminantTest() { var chol = new CholeskyDecomposition(bigmatrix); Assert.AreEqual(0.0, chol.Determinant); Assert.AreEqual(-2224.8931093738875, chol.LogDeterminant, 1e-10); Assert.IsTrue(chol.IsPositiveDefinite); Assert.IsTrue(chol.Nonsingular); Assert.IsTrue(Matrix.IsEqual(chol.Reverse(), bigmatrix, 1e-6)); }
public void CholeskyDecompositionConstructorTest3() { double[,] value = // not positive-definite { { 6, -1, 2, 6 }, { -1, 3, -3, -2 }, { 2, -3, 2, 0 }, { 6, -2, 0, 0 }, }; double[,] expected = { { 1.0000, 0, 0, 0 }, { -0.1667, 1.0000, 0, 0 }, { 0.3333, -0.9412, 1.0000, 0 }, { 1.0000, -0.3529, 2.5000, 1.0000 }, }; double[,] diagonal = { { 6.0000, 0, 0, 0 }, { 0, 2.8333, 0, 0 }, { 0, 0, -1.1765, 0 }, { 0, 0, 0, 1.0000 }, }; var chol = new CholeskyDecomposition(value, true); double[,] L = chol.LeftTriangularFactor; double[,] D = chol.DiagonalMatrix; Assert.IsTrue(Matrix.IsEqual(L, expected, 0.001)); Assert.IsTrue(Matrix.IsEqual(D, diagonal, 0.001)); Assert.IsFalse(chol.IsPositiveDefinite); // Decomposition Identity Assert.IsTrue(Matrix.IsEqual(L.Multiply(D).Multiply(L.Transpose()), value, 0.001)); Assert.IsTrue(Matrix.IsEqual(chol.Reverse(), value, 1e-6)); Assert.AreEqual(new LuDecomposition(value).Determinant, chol.Determinant, 1e-10); }
public void CholeskyDecompositionConstructorTest2() { double[,] value = // positive-definite { { 2, -1, 0 }, { -1, 2, -1 }, { 0, -1, 2 } }; double[,] expected = { { 1.0000, 0.0000, 0.0000 }, { -0.5000, 1.0000, 0.0000 }, { 0.0000, -0.6667, 1.0000 } }; double[,] diagonal = { { 2.0000, 0.0000, 0.0000 }, { 0.0000, 1.5000, 0.0000 }, { 0.0000, 0.0000, 1.3333 }, }; var chol = new CholeskyDecomposition(value, true); double[,] L = chol.LeftTriangularFactor; double[,] D = chol.DiagonalMatrix; Assert.IsTrue(Matrix.IsEqual(L, expected, 0.001)); Assert.IsTrue(Matrix.IsEqual(D, diagonal, 0.001)); Assert.IsTrue(chol.IsPositiveDefinite); // Decomposition Identity Assert.IsTrue(Matrix.IsEqual(Matrix.Multiply(Matrix.Multiply(L, D), L.Transpose()), value, 0.001)); Assert.IsTrue(Matrix.IsEqual(chol.Reverse(), value, 1e-6)); Assert.AreEqual(new LuDecomposition(value).Determinant, chol.Determinant, 1e-10); }
public void InverseTest() { double[,] value = // not positive-definite { { 6, -1, 2, 6 }, { -1, 3, -3, -2 }, { 2, -3, 2, 0 }, { 6, -2, 0, 0 }, }; double[,] expected = { { 0.40000, 1.20000, 1.40000, -0.50000 }, { 1.20000, 3.60000, 4.20000, -2.00000 }, { 1.40000, 4.20000, 5.40000, -2.50000 }, { -0.50000, -2.00000, -2.50000, 1.00000 }, }; var chol = new CholeskyDecomposition(value, robust: true); double[,] L = chol.LeftTriangularFactor; Assert.IsFalse(chol.IsPositiveDefinite); double[,] actual = chol.Inverse(); Assert.IsTrue(Matrix.IsEqual(expected, actual, 1e-6)); double[,] inv = Matrix.Inverse(value); Assert.IsTrue(Matrix.IsEqual(expected, inv, 1e-10)); Assert.IsTrue(Matrix.IsEqual(chol.Reverse(), value, 1e-6)); }
public void SolveTest2() { double[,] value = // not positive-definite { { 6, -1, 2, 6 }, { -1, 3, -3, -2 }, { 2, -3, 2, 0 }, { 6, -2, 0, 0 }, }; var chol = new CholeskyDecomposition(value, true); double[,] L = chol.LeftTriangularFactor; double[,] B = Matrix.Identity(4); double[,] expected = { { 0.4000, 1.2000, 1.4000, -0.5000 }, { 1.2000, 3.6000, 4.2000, -2.0000 }, { 1.4000, 4.2000, 5.4000, -2.5000 }, { -0.5000, -2.0000, -2.5000, 1.0000 }, }; double[,] actual = chol.Solve(B); Assert.IsTrue(Matrix.IsEqual(expected, actual, 1e-10)); Assert.IsTrue(Matrix.IsEqual(chol.Reverse(), value, 1e-6)); }
public void SolveTest3() { double[,] value = // positive-definite { { 2, -1, 0 }, { -1, 2, -1 }, { 0, -1, 2 } }; var chol = new CholeskyDecomposition(value); Assert.IsTrue(chol.IsPositiveDefinite); double[,] L = chol.LeftTriangularFactor; double[] B = new double[] { 1, 2, 3 }; double[] expected = new double[] { 2.5, 4.0, 3.5 }; double[] actual = chol.Solve(B); Assert.IsTrue(Matrix.IsEqual(expected, actual, 1e-10)); actual = chol.Solve(B, true); Assert.AreEqual(actual, B); Assert.IsTrue(Matrix.IsEqual(expected, B, 1e-10)); Assert.IsTrue(Matrix.IsEqual(chol.Reverse(), value, 1e-6)); }