public void TestRandomRectangularA() { RandomHelper.Random = new Random(1); // Every transpose(A) * A is SPD if A has full column rank and m>n. for (int i = 0; i < 100; i++) { // Create A. MatrixD a = new MatrixD(10, 3); RandomHelper.Random.NextMatrixD(a, 0, 1); QRDecompositionD d = new QRDecompositionD(a); MatrixD b = new MatrixD(10, 1); RandomHelper.Random.NextMatrixD(b, 0, 1); MatrixD x = d.SolveLinearEquations(b); Assert.IsTrue(MatrixD.AreNumericallyEqual(a, d.Q * d.R)); Assert.IsTrue(MatrixD.AreNumericallyEqual(a, d.Q * d.R)); // Second time with the cached values. // Check solving of linear equations. if (d.HasNumericallyFullRank) { // Compare with Least squares solution (Gauss-Transformation and Cholesky). MatrixD spdMatrix = a.Transposed * a; CholeskyDecompositionD ch = new CholeskyDecompositionD(spdMatrix); MatrixD x2 = ch.SolveLinearEquations(a.Transposed * b); Assert.IsTrue(MatrixD.AreNumericallyEqual(x, x2, 0.0001f)); } MatrixD h = d.H; // Just call this one to see if it runs through. } }
public void Test1() { MatrixD a = new MatrixD(new double[,] { { 2, -1, 0}, { -1, 2, -1}, { 0, -1, 2} }); CholeskyDecompositionD d = new CholeskyDecompositionD(a); Assert.AreEqual(true, d.IsSymmetricPositiveDefinite); MatrixD l = d.L; Assert.AreEqual(0, l[0, 1]); Assert.AreEqual(0, l[0, 2]); Assert.AreEqual(0, l[1, 2]); Assert.IsTrue(MatrixD.AreNumericallyEqual(a, l * l.Transposed)); Assert.IsTrue(MatrixD.AreNumericallyEqual(a, l * l.Transposed)); // Check solving of linear equations. MatrixD x = new MatrixD(new double[,] { { 1, 2}, { 3, 4}, { 5, 6} }); MatrixD b = a * x; Assert.IsTrue(MatrixD.AreNumericallyEqual(x, d.SolveLinearEquations(b))); }
public void Test1() { MatrixD a = new MatrixD(new double[, ] { { 2, -1, 0 }, { -1, 2, -1 }, { 0, -1, 2 } }); CholeskyDecompositionD d = new CholeskyDecompositionD(a); Assert.AreEqual(true, d.IsSymmetricPositiveDefinite); MatrixD l = d.L; Assert.AreEqual(0, l[0, 1]); Assert.AreEqual(0, l[0, 2]); Assert.AreEqual(0, l[1, 2]); Assert.IsTrue(MatrixD.AreNumericallyEqual(a, l * l.Transposed)); Assert.IsTrue(MatrixD.AreNumericallyEqual(a, l * l.Transposed)); // Check solving of linear equations. MatrixD x = new MatrixD(new double[, ] { { 1, 2 }, { 3, 4 }, { 5, 6 } }); MatrixD b = a * x; Assert.IsTrue(MatrixD.AreNumericallyEqual(x, d.SolveLinearEquations(b))); }
public void TestRandomSpdA() { RandomHelper.Random = new Random(1); // Every transpose(A) * A is SPD if A has full column rank and m>n. for (int i = 0; i < 100; i++) { VectorD column1 = new VectorD(4); RandomHelper.Random.NextVectorD(column1, 1, 2); VectorD column2 = new VectorD(4); RandomHelper.Random.NextVectorD(column2, 1, 2); // Make linearly independent. if (column1 / column1[0] == column2 / column2[0]) { column2[0]++; } // Create linearly independent third column. VectorD column3 = column1 + column2; column3[1]++; // Create A. MatrixD a = new MatrixD(4, 3); a.SetColumn(0, column1); a.SetColumn(1, column2); a.SetColumn(2, column3); MatrixD spdMatrix = a.Transposed * a; CholeskyDecompositionD d = new CholeskyDecompositionD(spdMatrix); Assert.AreEqual(true, d.IsSymmetricPositiveDefinite); MatrixD l = d.L; // Test if L is a lower triangular matrix. for (int j = 0; j < l.NumberOfRows; j++) { for (int k = 0; k < l.NumberOfColumns; k++) { if (j < k) { Assert.AreEqual(0, l[j, k]); } } } Assert.IsTrue(MatrixD.AreNumericallyEqual(spdMatrix, l * l.Transposed)); // Check solving of linear equations. MatrixD b = new MatrixD(3, 2); RandomHelper.Random.NextMatrixD(b, 0, 1); MatrixD x = d.SolveLinearEquations(b); MatrixD b2 = spdMatrix * x; Assert.IsTrue(MatrixD.AreNumericallyEqual(b, b2, 0.01f)); } }
public void SolveLinearEquationsException1() { // Create A. MatrixD a = new MatrixD(new double[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, -9 } }); CholeskyDecompositionD decomp = new CholeskyDecompositionD(a); decomp.SolveLinearEquations(null); }
public void SolveLinearEquationsException1() { // Create A. MatrixD a = new MatrixD(new double[, ] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, -9 } }); CholeskyDecompositionD decomp = new CholeskyDecompositionD(a); decomp.SolveLinearEquations(null); }
public void TestRandomRectangularA() { RandomHelper.Random = new Random(1); // Every transpose(A) * A is SPD if A has full column rank and m>n. for (int i = 0; i < 100; i++) { // Create A. MatrixD a = new MatrixD(10, 3); RandomHelper.Random.NextMatrixD(a, 0, 1); QRDecompositionD d = new QRDecompositionD(a); MatrixD b = new MatrixD(10, 1); RandomHelper.Random.NextMatrixD(b, 0, 1); MatrixD x = d.SolveLinearEquations(b); Assert.IsTrue(MatrixD.AreNumericallyEqual(a, d.Q * d.R)); Assert.IsTrue(MatrixD.AreNumericallyEqual(a, d.Q * d.R)); // Second time with the cached values. // Check solving of linear equations. if (d.HasNumericallyFullRank) { // Compare with Least squares solution (Gauss-Transformation and Cholesky). MatrixD spdMatrix = a.Transposed * a; CholeskyDecompositionD ch = new CholeskyDecompositionD(spdMatrix); MatrixD x2 = ch.SolveLinearEquations(a.Transposed * b); Assert.IsTrue(MatrixD.AreNumericallyEqual(x, x2, 0.0001f)); } MatrixD h = d.H; // Just call this one to see if it runs through. } }
public void TestRandomSpdA() { RandomHelper.Random = new Random(1); // Every transpose(A) * A is SPD if A has full column rank and m>n. for (int i = 0; i < 100; i++) { VectorD column1 = new VectorD(4); RandomHelper.Random.NextVectorD(column1, 1, 2); VectorD column2 = new VectorD(4); RandomHelper.Random.NextVectorD(column2, 1, 2); // Make linearly independent. if (column1 / column1[0] == column2 / column2[0]) column2[0]++; // Create linearly independent third column. VectorD column3 = column1 + column2; column3[1]++; // Create A. MatrixD a = new MatrixD(4, 3); a.SetColumn(0, column1); a.SetColumn(1, column2); a.SetColumn(2, column3); MatrixD spdMatrix = a.Transposed * a; CholeskyDecompositionD d = new CholeskyDecompositionD(spdMatrix); Assert.AreEqual(true, d.IsSymmetricPositiveDefinite); MatrixD l = d.L; // Test if L is a lower triangular matrix. for (int j = 0; j < l.NumberOfRows; j++) for (int k = 0; k < l.NumberOfColumns; k++) if (j < k) Assert.AreEqual(0, l[j, k]); Assert.IsTrue(MatrixD.AreNumericallyEqual(spdMatrix, l * l.Transposed)); // Check solving of linear equations. MatrixD b = new MatrixD(3, 2); RandomHelper.Random.NextMatrixD(b, 0, 1); MatrixD x = d.SolveLinearEquations(b); MatrixD b2 = spdMatrix * x; Assert.IsTrue(MatrixD.AreNumericallyEqual(b, b2, 0.01f)); } }