private static void TestSystemSolution(LinearAlgebraProviderChoice providers) { TestSettings.RunMultiproviderTest(providers, delegate() { // invertible var A1 = TriangularUpper.CreateFromArray(UpperInvertible10by10.Matrix); var b1 = Vector.CreateFromArray(UpperInvertible10by10.Rhs); var x1Expected = Vector.CreateFromArray(UpperInvertible10by10.Lhs); Vector x1Computed = A1.SolveLinearSystem(b1); comparer.AssertEqual(x1Expected, x1Computed); // singular var A2 = TriangularUpper.CreateFromArray(UpperSingular10by10.Matrix); var b2 = Vector.CreateFromArray(UpperSingular10by10.Rhs); var x2Expected = Vector.CreateFromArray(UpperSingular10by10.Lhs); Vector x2Computed = A2.SolveLinearSystem(b2); Assert.False(comparer.AreEqual(x2Expected, x2Computed)); // invertible - solve transposed (forward substitution) Matrix A3 = Matrix.CreateFromArray(UpperInvertible10by10.Matrix).Invert().Transpose(); Vector x3Expected = A3 * b1; Vector x3Computed = A1.SolveLinearSystem(b1, true); comparer.AssertEqual(x3Expected, x3Computed); }); }
private static void TestIndefiniteSystem(LinearAlgebraProviderChoice providers) { TestSettings.RunMultiproviderTest(providers, delegate() { (Matrix A, Vector b, Vector xExpected, IPreconditioner M) = DiagonalIndefinite.BuildIndefiniteSystem(20); var builder = new CGAlgorithm.Builder(); builder.ResidualTolerance = 1E-6; builder.MaxIterationsProvider = new PercentageMaxIterationsProvider(1.0); var cg = builder.Build(); Vector xComputed = Vector.CreateZero(A.NumRows); IterativeStatistics stats = cg.Solve(A, b, xComputed, true); Assert.False(comparer.AreEqual(xExpected, xComputed)); }); }
private static void TestSystemSolution(LinearAlgebraProviderChoice providers) { TestSettings.RunMultiproviderTest(providers, delegate() { // invertible var A1 = TriangularLower.CreateFromArray(LowerInvertible10by10.Matrix); var b1 = Vector.CreateFromArray(LowerInvertible10by10.Rhs); var x1Expected = Vector.CreateFromArray(LowerInvertible10by10.Lhs); Vector x1Computed = A1.SolveLinearSystem(b1); comparer.AssertEqual(x1Expected, x1Computed); // singular var A2 = TriangularLower.CreateFromArray(LowerSingular10by10.Matrix); var b2 = Vector.CreateFromArray(LowerSingular10by10.Rhs); var x2Expected = Vector.CreateFromArray(LowerSingular10by10.Lhs); Vector x2Computed = A2.SolveLinearSystem(b2); Assert.False(comparer.AreEqual(x2Expected, x2Computed)); }); }
private static void TestRandomMatrix() { // Create the random matrix and write it to a temporary file DokSymmetric originalMatrix = RandomUtilities.CreateRandomMatrix(1000, 0.2); var coordinateWriter = new CoordinateTextFileWriter(); coordinateWriter.NumericFormat = new ExponentialFormat { NumDecimalDigits = 10 }; string tempFile = "temp.txt"; coordinateWriter.WriteToFile(originalMatrix, tempFile); // Read the temporary file and compare it with the generated matrix var reader = new CoordinateTextFileReader(); DokSymmetric readMatrix = reader.ReadFileAsDokSymmetricColMajor(tempFile); bool success = comparer.AreEqual(originalMatrix, readMatrix); // Clean up File.Delete(tempFile); Assert.True(success); }