public void SolveTest1() { double[,] value = { { 2, 3, 0 }, { -1, 2, 1 }, { 0, -1, 3 } }; double[] rhs = { 5, 0, 1 }; double[] expected = { 1.6522, 0.5652, 0.5217, }; var target = new LuDecomposition(value); double[] actual = target.Solve(rhs); Assert.IsTrue(Matrix.IsEqual(expected, actual, 1e-3)); Assert.IsTrue(Matrix.IsEqual(value, target.Reverse())); var target2 = new JaggedLuDecomposition(value.ToJagged()); actual = target2.Solve(rhs); Assert.IsTrue(Matrix.IsEqual(expected, actual, 1e-3)); Assert.IsTrue(Matrix.IsEqual(value, target2.Reverse())); }
public void InverseTest() { double[,] value = { { 2, 3, 0 }, { -1, 2, 1 }, { 0, -1, 3 } }; double[,] expectedInverse = { { 0.3043, -0.3913, 0.1304 }, { 0.1304, 0.2609, -0.0870 }, { 0.0435, 0.0870, 0.3043 }, }; var target = new LuDecomposition(value); double[,] actualInverse = target.Inverse(); Assert.IsTrue(Matrix.IsEqual(expectedInverse, actualInverse, 0.001)); Assert.IsTrue(Matrix.IsEqual(value, target.Reverse())); var target2 = new JaggedLuDecomposition(value.ToJagged()); actualInverse = target2.Inverse().ToMatrix(); Assert.IsTrue(Matrix.IsEqual(expectedInverse, actualInverse, 0.001)); Assert.IsTrue(Matrix.IsEqual(value, target2.Reverse())); }
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 }, }; Assert.IsTrue(Matrix.IsEqual(expected, new LuDecomposition(b, true).SolveTranspose(a), 1e-3)); Assert.IsTrue(Matrix.IsEqual(expected, new JaggedLuDecomposition(b.ToJagged(), true).SolveTranspose(a.ToJagged()), 1e-3)); var target = new LuDecomposition(b, true); var p = target.PivotPermutationVector; int[] idx = p.ArgSort(); var r = target.LowerTriangularFactor.Dot(target.UpperTriangularFactor) .Submatrix(idx, null).Transpose(); Assert.IsTrue(Matrix.IsEqual(b, r, 1e-3)); Assert.IsTrue(Matrix.IsEqual(b.Transpose(), target.Reverse(), 1e-3)); Assert.IsTrue(Matrix.IsEqual(b.Transpose(), new JaggedLuDecomposition(b.ToJagged(), true).Reverse(), 1e-3)); }