public void When_PartialSolve_Expect_Reference() { var solver = new SparseRealSolver { PivotSearchReduction = 2, // Limit to only the 2 first elements Degeneracy = 2 // Only perform elimination on the first two rows }; solver[1, 1] = 1; solver[1, 3] = 2; solver[1, 4] = 3; solver[1] = 1; solver[2, 2] = 2; solver[2, 3] = 4; solver[2] = 2; solver.Factor(); // We should now be able to solve for multiple solutions, where the last two elements // will determine the result. var solution = new DenseVector <double>(4); solution[3] = 0; solution[4] = 0; solver.Solve(solution); Assert.AreEqual(1.0, solution[1], 1e-12); Assert.AreEqual(1.0, solution[2], 1e-12); solution[3] = 1.0; solution[4] = 2.0; solver.Solve(solution); Assert.AreEqual(-7.0, solution[1], 1e-12); Assert.AreEqual(-1.0, solution[2], 1e-12); }
public void When_PartialDecompositionSingular_Expect_Reference() { var solver = new SparseRealSolver(); solver[1, 1] = 1; solver[2, 2] = 1; solver[2, 3] = 1; solver[3, 1] = 1; solver[3, 2] = 1; solver[3, 3] = 1; solver.Degeneracy = 1; Assert.AreEqual(true, solver.Factor()); AssertInternal(solver, 1, 1, 1); AssertInternal(solver, 2, 2, 1); AssertInternal(solver, 2, 3, 1); AssertInternal(solver, 3, 1, 1); AssertInternal(solver, 3, 2, 1); AssertInternal(solver, 3, 3, 0); }
public void When_Factoring_Expect_Reference() { double[][] matrixElements = { new[] { 1.0, 1.0, 1.0 }, new[] { 2.0, 3.0, 5.0 }, new[] { 4.0, 6.0, 8.0 } }; double[][] expected = { new[] { 1.0, 1.0, 1.0 }, new[] { 2.0, 1.0, 3.0 }, new[] { 4.0, 2.0, -0.5 } }; // Create matrix var solver = new SparseRealSolver(); for (var r = 0; r < matrixElements.Length; r++) { for (var c = 0; c < matrixElements[r].Length; c++) { solver.GetElement(new MatrixLocation(r + 1, c + 1)).Value = matrixElements[r][c]; } } // Factor solver.Factor(); // Compare for (var r = 0; r < matrixElements.Length; r++) { for (var c = 0; c < matrixElements[r].Length; c++) { Assert.AreEqual(expected[r][c], solver.GetElement(new MatrixLocation(r + 1, c + 1)).Value, 1e-12); } } }