public void QrDecompositionConstructorTest() { double[,] value = { { 2, -1, 0 }, { -1, 2, -1 }, { 0, -1, 2 } }; var target = new QrDecomposition(value); // Decomposition Identity var Q = target.OrthogonalFactor; var QQt = Q.DotWithTransposed(Q); Assert.IsTrue(Matrix.IsEqual(QQt, Matrix.Identity(3), 1e-6)); Assert.IsTrue(Matrix.IsEqual(value, target.Reverse(), 1e-6)); // Linear system solving double[,] B = Matrix.ColumnVector(new double[] { 1, 2, 3 }); double[,] expected = Matrix.ColumnVector(new double[] { 2.5, 4.0, 3.5 }); double[,] actual = target.Solve(B); Assert.IsTrue(Matrix.IsEqual(expected, actual, 0.0000000000001)); }
public void SolveTransposeTest() { double[,] a = { { 2, 1, 4 }, { 6, 2, 2 }, { 0, 1, 6 }, }; double[,] b = { { 1, 0, 7 }, { 5, 2, 1 }, { 1, 5, 2 }, }; double[,] expected = { { 0.5062, 0.2813, 0.0875 }, { 0.1375, 1.1875, -0.0750 }, { 0.8063, -0.2188, 0.2875 }, }; var target = new QrDecomposition(b, true); double[,] actual = target.SolveTranspose(a); Assert.IsTrue(Matrix.IsEqual(expected, actual, 1e-3)); Assert.IsTrue(Matrix.IsEqual(b.Transpose(), target.Reverse(), 1e-6)); }
public void SolveTest2() { // Example from Lecture notes for MATHS 370: Advanced Numerical Methods // http://www.math.auckland.ac.nz/~sharp/370/qr-solving.pdf double[,] value = { { 1, 0, 0 }, { 1, 7, 49 }, { 1, 14, 196 }, { 1, 21, 441 }, { 1, 28, 784 }, { 1, 35, 1225 }, }; // Matrices { double[,] b = { { 4 }, { 1 }, { 0 }, { 0 }, { 2 }, { 5 }, }; double[,] expected = { { 3.9286 }, { -0.5031 }, { 0.0153 }, }; var target = new QrDecomposition(value); double[,] actual = target.Solve(b); Assert.IsTrue(Matrix.IsEqual(expected, actual, atol: 1e-4)); Assert.IsTrue(Matrix.IsEqual(value, target.Reverse(), 1e-6)); var target2 = new JaggedQrDecomposition(value.ToJagged()); double[][] actual2 = target2.Solve(b.ToJagged()); Assert.IsTrue(Matrix.IsEqual(expected, actual2, atol: 1e-4)); Assert.IsTrue(Matrix.IsEqual(value, target2.Reverse(), 1e-6)); } // Vectors { double[] b = { 4, 1, 0, 0, 2, 5 }; double[] expected = { 3.9286, -0.5031, 0.0153 }; var target = new QrDecomposition(value); double[] actual = target.Solve(b); Assert.IsTrue(Matrix.IsEqual(expected, actual, atol: 1e-4)); } }
public void InverseTest() { int n = 5; var I = Matrix.Identity(n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { double[,] value = Matrix.Magic(n); var target = new QrDecomposition(value); var solution = target.Solve(I); var inverse = target.Inverse(); var reverse = target.Reverse(); Assert.IsTrue(Matrix.IsEqual(solution, inverse, 1e-4)); Assert.IsTrue(Matrix.IsEqual(value, reverse, 1e-4)); } } }