From 9905ef30389ac45d6bb69a925313cd9081a6e52b Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sat, 29 Jun 2013 15:57:04 +0200 Subject: [PATCH] RootFinding: s/hybrid/robust/ --- src/Numerics/FindRoots.cs | 2 +- src/Numerics/Numerics.csproj | 2 +- ...ewtonRaphson.cs => RobustNewtonRaphson.cs} | 4 +- src/Portable/Portable.csproj | 4 +- .../RootFindingTests/NewtonRaphsonTest.cs | 62 +++++++++---------- 5 files changed, 37 insertions(+), 37 deletions(-) rename src/Numerics/RootFinding/{HybridNewtonRaphson.cs => RobustNewtonRaphson.cs} (98%) diff --git a/src/Numerics/FindRoots.cs b/src/Numerics/FindRoots.cs index 1ad86be3..d6387547 100644 --- a/src/Numerics/FindRoots.cs +++ b/src/Numerics/FindRoots.cs @@ -43,7 +43,7 @@ namespace MathNet.Numerics public static double OfFunctionAndDerivative(Func f, Func df, double lowerBound, double upperBound, double accuracy = 1e-8) { double root; - if (HybridNewtonRaphson.TryFindRoot(f, df, lowerBound, upperBound, accuracy, 100, 20, out root)) + if (RobustNewtonRaphson.TryFindRoot(f, df, lowerBound, upperBound, accuracy, 100, 20, out root)) { return root; } diff --git a/src/Numerics/Numerics.csproj b/src/Numerics/Numerics.csproj index 6ec92b7f..b51c7df3 100644 --- a/src/Numerics/Numerics.csproj +++ b/src/Numerics/Numerics.csproj @@ -111,7 +111,7 @@ - + diff --git a/src/Numerics/RootFinding/HybridNewtonRaphson.cs b/src/Numerics/RootFinding/RobustNewtonRaphson.cs similarity index 98% rename from src/Numerics/RootFinding/HybridNewtonRaphson.cs rename to src/Numerics/RootFinding/RobustNewtonRaphson.cs index a76ac9a1..5ee9a374 100644 --- a/src/Numerics/RootFinding/HybridNewtonRaphson.cs +++ b/src/Numerics/RootFinding/RobustNewtonRaphson.cs @@ -1,4 +1,4 @@ -// +// // Math.NET Numerics, part of the Math.NET Project // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics @@ -32,7 +32,7 @@ using System; namespace MathNet.Numerics.RootFinding { - public static class HybridNewtonRaphson + public static class RobustNewtonRaphson { /// Find a solution of the equation f(x)=0. /// The function to find roots from. diff --git a/src/Portable/Portable.csproj b/src/Portable/Portable.csproj index 7c94f354..0dc803b8 100644 --- a/src/Portable/Portable.csproj +++ b/src/Portable/Portable.csproj @@ -996,8 +996,8 @@ RootFinding\Brent.cs - - RootFinding\HybridNewtonRaphson.cs + + RootFinding\RobustNewtonRaphson.cs RootFinding\ZeroCrossingBracketing.cs diff --git a/src/UnitTests/RootFindingTests/NewtonRaphsonTest.cs b/src/UnitTests/RootFindingTests/NewtonRaphsonTest.cs index a2a0da79..155284af 100644 --- a/src/UnitTests/RootFindingTests/NewtonRaphsonTest.cs +++ b/src/UnitTests/RootFindingTests/NewtonRaphsonTest.cs @@ -43,21 +43,21 @@ namespace MathNet.Numerics.UnitTests.RootFindingTests // Roots at -2, 2 Func f1 = x => x * x - 4; Func df1 = x => 2 * x; - Assert.AreEqual(0, f1(HybridNewtonRaphson.FindRoot(f1, df1, -5, 5, 1e-14, 100, 20))); - Assert.AreEqual(-2, HybridNewtonRaphson.FindRoot(f1, df1, -5, -1, 1e-14, 100, 20)); - Assert.AreEqual(2, HybridNewtonRaphson.FindRoot(f1, df1, 1, 4, 1e-14, 100, 20)); - Assert.AreEqual(0, f1(HybridNewtonRaphson.FindRoot(x => -f1(x), x => -df1(x), -5, 5, 1e-14, 100, 20))); - Assert.AreEqual(-2, HybridNewtonRaphson.FindRoot(x => -f1(x), x => -df1(x), -5, -1, 1e-14, 100, 20)); - Assert.AreEqual(2, HybridNewtonRaphson.FindRoot(x => -f1(x), x => -df1(x), 1, 4, 1e-14, 100, 20)); + Assert.AreEqual(0, f1(RobustNewtonRaphson.FindRoot(f1, df1, -5, 5, 1e-14, 100, 20))); + Assert.AreEqual(-2, RobustNewtonRaphson.FindRoot(f1, df1, -5, -1, 1e-14, 100, 20)); + Assert.AreEqual(2, RobustNewtonRaphson.FindRoot(f1, df1, 1, 4, 1e-14, 100, 20)); + Assert.AreEqual(0, f1(RobustNewtonRaphson.FindRoot(x => -f1(x), x => -df1(x), -5, 5, 1e-14, 100, 20))); + Assert.AreEqual(-2, RobustNewtonRaphson.FindRoot(x => -f1(x), x => -df1(x), -5, -1, 1e-14, 100, 20)); + Assert.AreEqual(2, RobustNewtonRaphson.FindRoot(x => -f1(x), x => -df1(x), 1, 4, 1e-14, 100, 20)); // Roots at 3, 4 Func f2 = x => (x - 3) * (x - 4); Func df2 = x => 2 * x - 7; - Assert.AreEqual(0, f2(HybridNewtonRaphson.FindRoot(f2, df2, -5, 5, 1e-14, 100, 20))); - Assert.AreEqual(3, HybridNewtonRaphson.FindRoot(f2, df2, -5, 3.5, 1e-14, 100, 20)); - Assert.AreEqual(4, HybridNewtonRaphson.FindRoot(f2, df2, 3.2, 5, 1e-14, 100, 20)); - Assert.AreEqual(3, HybridNewtonRaphson.FindRoot(f2, df2, 2.1, 3.9, 0.001, 50, 20), 0.001); - Assert.AreEqual(3, HybridNewtonRaphson.FindRoot(f2, df2, 2.1, 3.4, 0.001, 50, 20), 0.001); + Assert.AreEqual(0, f2(RobustNewtonRaphson.FindRoot(f2, df2, -5, 5, 1e-14, 100, 20))); + Assert.AreEqual(3, RobustNewtonRaphson.FindRoot(f2, df2, -5, 3.5, 1e-14, 100, 20)); + Assert.AreEqual(4, RobustNewtonRaphson.FindRoot(f2, df2, 3.2, 5, 1e-14, 100, 20)); + Assert.AreEqual(3, RobustNewtonRaphson.FindRoot(f2, df2, 2.1, 3.9, 0.001, 50, 20), 0.001); + Assert.AreEqual(3, RobustNewtonRaphson.FindRoot(f2, df2, 2.1, 3.4, 0.001, 50, 20), 0.001); } [Test] @@ -65,8 +65,8 @@ namespace MathNet.Numerics.UnitTests.RootFindingTests { Func f1 = x => x * x * x - 2 * x + 2; Func df1 = x => 3 * x * x - 2; - Assert.AreEqual(0, f1(HybridNewtonRaphson.FindRoot(f1, df1, -5, 5, 1e-14, 100, 20))); - Assert.AreEqual(0, f1(HybridNewtonRaphson.FindRoot(f1, df1, -2, 4, 1e-14, 100, 20))); + Assert.AreEqual(0, f1(RobustNewtonRaphson.FindRoot(f1, df1, -5, 5, 1e-14, 100, 20))); + Assert.AreEqual(0, f1(RobustNewtonRaphson.FindRoot(f1, df1, -2, 4, 1e-14, 100, 20))); } [Test] @@ -74,29 +74,29 @@ namespace MathNet.Numerics.UnitTests.RootFindingTests { Func f1 = x => 1/(x - 2) + 2; Func df1 = x => -1/(x*x - 4*x + 4); - Assert.AreEqual(1.5, HybridNewtonRaphson.FindRoot(f1, df1, 1, 2, 1e-14, 100, 20)); - Assert.AreEqual(1.5, HybridNewtonRaphson.FindRoot(f1, df1, 1, 6, 1e-14, 100, 20)); + Assert.AreEqual(1.5, RobustNewtonRaphson.FindRoot(f1, df1, 1, 2, 1e-14, 100, 20)); + Assert.AreEqual(1.5, RobustNewtonRaphson.FindRoot(f1, df1, 1, 6, 1e-14, 100, 20)); Assert.AreEqual(1.5, FindRoots.OfFunctionAndDerivative(f1, df1, 1, 6)); Func f2 = x => -1/(x - 2) + 2; Func df2 = x => 1/(x*x - 4*x + 4); - Assert.AreEqual(2.5, HybridNewtonRaphson.FindRoot(f2, df2, 2, 3, 1e-14, 100, 20)); - Assert.AreEqual(2.5, HybridNewtonRaphson.FindRoot(f2, df2, -2, 3, 1e-14, 100, 20)); + Assert.AreEqual(2.5, RobustNewtonRaphson.FindRoot(f2, df2, 2, 3, 1e-14, 100, 20)); + Assert.AreEqual(2.5, RobustNewtonRaphson.FindRoot(f2, df2, -2, 3, 1e-14, 100, 20)); Assert.AreEqual(2.5, FindRoots.OfFunctionAndDerivative(f2, df2, -2, 3)); Func f3 = x => 1/(x - 2) + x + 2; Func df3 = x => -1/(x*x - 4*x + 4) + 1; - Assert.AreEqual(-Math.Sqrt(3), HybridNewtonRaphson.FindRoot(f3, df3, -2, -1, 1e-14, 100, 20), 1e-14); - Assert.AreEqual(Math.Sqrt(3), HybridNewtonRaphson.FindRoot(f3, df3, 1, 1.99, 1e-14, 100, 20)); - Assert.AreEqual(Math.Sqrt(3), HybridNewtonRaphson.FindRoot(f3, df3, -1.5, 1.99, 1e-14, 100, 20)); - Assert.AreEqual(Math.Sqrt(3), HybridNewtonRaphson.FindRoot(f3, df3, 1, 6, 1e-14, 100, 20)); + Assert.AreEqual(-Math.Sqrt(3), RobustNewtonRaphson.FindRoot(f3, df3, -2, -1, 1e-14, 100, 20), 1e-14); + Assert.AreEqual(Math.Sqrt(3), RobustNewtonRaphson.FindRoot(f3, df3, 1, 1.99, 1e-14, 100, 20)); + Assert.AreEqual(Math.Sqrt(3), RobustNewtonRaphson.FindRoot(f3, df3, -1.5, 1.99, 1e-14, 100, 20)); + Assert.AreEqual(Math.Sqrt(3), RobustNewtonRaphson.FindRoot(f3, df3, 1, 6, 1e-14, 100, 20)); Func f4 = x => 1/(2 - x) - x + 6; Func df4 = x => 1/(x*x - 4*x + 4) - 1; - Assert.AreEqual(4 + Math.Sqrt(3), HybridNewtonRaphson.FindRoot(f4, df4, 5, 6, 1e-14, 100, 20), 1e-14); - Assert.AreEqual(4 - Math.Sqrt(3), HybridNewtonRaphson.FindRoot(f4, df4, 2.01, 3, 1e-14, 100, 20)); - Assert.AreEqual(4 - Math.Sqrt(3), HybridNewtonRaphson.FindRoot(f4, df4, 2.01, 5, 1e-14, 100, 20)); - Assert.AreEqual(4 - Math.Sqrt(3), HybridNewtonRaphson.FindRoot(f4, df4, -2, 4, 1e-14, 100, 20)); + Assert.AreEqual(4 + Math.Sqrt(3), RobustNewtonRaphson.FindRoot(f4, df4, 5, 6, 1e-14, 100, 20), 1e-14); + Assert.AreEqual(4 - Math.Sqrt(3), RobustNewtonRaphson.FindRoot(f4, df4, 2.01, 3, 1e-14, 100, 20)); + Assert.AreEqual(4 - Math.Sqrt(3), RobustNewtonRaphson.FindRoot(f4, df4, 2.01, 5, 1e-14, 100, 20)); + Assert.AreEqual(4 - Math.Sqrt(3), RobustNewtonRaphson.FindRoot(f4, df4, -2, 4, 1e-14, 100, 20)); } [Test] @@ -105,15 +105,15 @@ namespace MathNet.Numerics.UnitTests.RootFindingTests // with complex roots (looking for the real root only): 3x^3 + 4x^2 + 5x + 6, derivative 9x^2 + 8x + 5 Func f1 = x => Evaluate.Polynomial(x, 6, 5, 4, 3); Func df1 = x => Evaluate.Polynomial(x, 5, 8, 9); - Assert.AreEqual(-1.265328088928, HybridNewtonRaphson.FindRoot(f1, df1, -2, -1, 1e-10, 100, 20), 1e-6); - Assert.AreEqual(-1.265328088928, HybridNewtonRaphson.FindRoot(f1, df1, -5, 5, 1e-10, 100, 20), 1e-6); + Assert.AreEqual(-1.265328088928, RobustNewtonRaphson.FindRoot(f1, df1, -2, -1, 1e-10, 100, 20), 1e-6); + Assert.AreEqual(-1.265328088928, RobustNewtonRaphson.FindRoot(f1, df1, -5, 5, 1e-10, 100, 20), 1e-6); // real roots only: 2x^3 + 4x^2 - 50x + 6, derivative 6x^2 + 8x - 50 Func f2 = x => Evaluate.Polynomial(x, 6, -50, 4, 2); Func df2 = x => Evaluate.Polynomial(x, -50, 8, 6); - Assert.AreEqual(-6.1466562197069, HybridNewtonRaphson.FindRoot(f2, df2, -8, -5, 1e-10, 100, 20), 1e-6); - Assert.AreEqual(0.12124737195841, HybridNewtonRaphson.FindRoot(f2, df2, -1, 1, 1e-10, 100, 20), 1e-6); - Assert.AreEqual(4.0254088477485, HybridNewtonRaphson.FindRoot(f2, df2, 3, 5, 1e-10, 100, 20), 1e-6); + Assert.AreEqual(-6.1466562197069, RobustNewtonRaphson.FindRoot(f2, df2, -8, -5, 1e-10, 100, 20), 1e-6); + Assert.AreEqual(0.12124737195841, RobustNewtonRaphson.FindRoot(f2, df2, -1, 1, 1e-10, 100, 20), 1e-6); + Assert.AreEqual(4.0254088477485, RobustNewtonRaphson.FindRoot(f2, df2, 3, 5, 1e-10, 100, 20), 1e-6); } [Test] @@ -121,7 +121,7 @@ namespace MathNet.Numerics.UnitTests.RootFindingTests { Func f1 = x => x * x + 4; Func df1 = x => 2 * x; - Assert.Throws(() => HybridNewtonRaphson.FindRoot(f1, df1, -5, 5, 1e-14, 50, 20)); + Assert.Throws(() => RobustNewtonRaphson.FindRoot(f1, df1, -5, 5, 1e-14, 50, 20)); } } }