Skip to content

Commit

Permalink
Merge pull request #1000 from jkalias/residualstopcriterion-crash-zer…
Browse files Browse the repository at this point in the history
…o-vector

Bug fix: no crash for zero vector in ResidualStopCriterion.
  • Loading branch information
cdrnet authored Aug 1, 2023
2 parents 037b98c + 5ddb3ef commit e010de7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/Numerics.Tests/LinearAlgebraTests/Double/DenseMatrixTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
using System.Collections.Generic;
using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Double;
using MathNet.Numerics.LinearAlgebra.Double.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers;
using NUnit.Framework;

namespace MathNet.Numerics.Tests.LinearAlgebraTests.Double
Expand Down Expand Up @@ -247,6 +249,20 @@ public void MatrixToMatrixString()
}
}

[Test]
public void ResidualStopCriterionDoesNotCrashForZeroVector()
{
var A = new DenseMatrix(2, 2, new double[] { 1, 2, 3, 4 });
var b = new DenseVector(2);
var x = new DenseVector(2);

var iterator = new Iterator<double>(new ResidualStopCriterion<double>(1e-5));
var solver = new BiCgStab();

solver.Solve(A, b, x, iterator, null);
Assert.AreEqual(new DenseVector(2), x);
}

private Matrix<double> GetMatrixWithRowsButNoColumns()
{
var matrix = TestMatrices["Tall3x2"];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public IterationStatus DetermineStatus(int iterationNumber, Vector<T> solutionVe

// ||r_i|| <= stop_tol * ||b||
// Stop the calculation if it's clearly smaller than the tolerance
if (residualNorm < stopCriterion)
if (residualNorm <= stopCriterion)
{
if (_lastIteration <= iterationNumber)
{
Expand Down

0 comments on commit e010de7

Please sign in to comment.