From c2cc20a2acc9d5c3e228430500e500c5f15ed79b Mon Sep 17 00:00:00 2001 From: Scott Stephens Date: Sat, 4 Jan 2014 16:36:55 -0600 Subject: [PATCH] Optimization: Add tests for cases involving a constant zero region (updated by cdrnet) --- .../TestBisectionRootFinder.cs | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/UnitTests/OptimizationTests/TestBisectionRootFinder.cs b/src/UnitTests/OptimizationTests/TestBisectionRootFinder.cs index 4d1149db..451f04bf 100644 --- a/src/UnitTests/OptimizationTests/TestBisectionRootFinder.cs +++ b/src/UnitTests/OptimizationTests/TestBisectionRootFinder.cs @@ -52,5 +52,31 @@ namespace MathNet.Numerics.UnitTests.OptimizationTests Assert.That(Math.Abs(r2 - 3.0), Is.LessThan(0.001)); } + [Test] + public void FindRoot_ConstantZeroRegion_HitsFirstZeroOnExpansion() + { + var algorithm = new BisectionRootFinder(1e-3,1e-3,lowerExpansionFactor:2,upperExpansionFactor:2); + + var r1 = algorithm.FindRoot(function_goes_flat_at_zero, -1,0); + + Assert.That(Math.Abs(r1 - 1), Is.LessThan(1e-3)); + + } + + [Test] + public void FindRoot_ConstantZeroRegion_HitsPastFirstZeroOnExpansion() + { + var algorithm = new BisectionRootFinder(1e-3,1e-3,lowerExpansionFactor:2,upperExpansionFactor:2); + + var r1 = algorithm.FindRoot(function_goes_flat_at_zero, -2.25,0); + + Assert.That(Math.Abs(r1 - 1), Is.LessThan(1e-3)); + + } + + private static double function_goes_flat_at_zero(double x) + { + return x < 1 ? -3*(x - 1) : 0.0; + } } }