diff --git a/src/FSharp/FindRoots.fs b/src/FSharp/FindRoots.fs index 16d3ef9a..fb6304d3 100644 --- a/src/FSharp/FindRoots.fs +++ b/src/FSharp/FindRoots.fs @@ -73,6 +73,6 @@ module FindRoots = brent 100 1e-8 lowerBound upperBound f |>| fun () -> bisection 100 1e-8 lowerBound upperBound f - let ofFunctionAndDerivative lowerBound upperBound (f:double->double) (df:double->double) = + let ofFunctionDerivative lowerBound upperBound (f:double->double) (df:double->double) = newtonRaphsonRobust 100 20 1e-8 lowerBound upperBound f df |>| fun () -> bisection 100 1e-8 lowerBound upperBound f diff --git a/src/FSharpUnitTests/FindRootsTests.fs b/src/FSharpUnitTests/FindRootsTests.fs index d336a52b..393eb79d 100644 --- a/src/FSharpUnitTests/FindRootsTests.fs +++ b/src/FSharpUnitTests/FindRootsTests.fs @@ -39,5 +39,5 @@ module FindRootsTests = let ``Simple method should find both roots of (x - 3) * (x - 4)``() = f |> FindRoots.ofFunction -5.0 3.5 |> Option.get |> should (equalWithin 1e-8) 3.0 f |> FindRoots.ofFunction 3.2 5.0 |> Option.get |> should (equalWithin 1e-8) 4.0 - (f, df) ||> FindRoots.ofFunctionAndDerivative -5.0 3.5 |> Option.get |> should (equalWithin 1e-8) 3.0 - (f, df) ||> FindRoots.ofFunctionAndDerivative 3.2 5.0 |> Option.get |> should (equalWithin 1e-8) 4.0 + (f, df) ||> FindRoots.ofFunctionDerivative -5.0 3.5 |> Option.get |> should (equalWithin 1e-8) 3.0 + (f, df) ||> FindRoots.ofFunctionDerivative 3.2 5.0 |> Option.get |> should (equalWithin 1e-8) 4.0 diff --git a/src/Numerics/FindRoots.cs b/src/Numerics/FindRoots.cs index d92db9b9..183d6651 100644 --- a/src/Numerics/FindRoots.cs +++ b/src/Numerics/FindRoots.cs @@ -29,6 +29,7 @@ // using System; +using MathNet.Numerics.Properties; using MathNet.Numerics.RootFinding; namespace MathNet.Numerics @@ -49,10 +50,10 @@ namespace MathNet.Numerics return root; } - throw new NonConvergenceException("The algorithm has failed or exceeded the number of iterations allowed"); + throw new NonConvergenceException(Resources.RootFindingFailed); } - public static double OfFunctionAndDerivative(Func f, Func df, double lowerBound, double upperBound, double accuracy = 1e-8, int maxIterations = 100) + public static double OfFunctionDerivative(Func f, Func df, double lowerBound, double upperBound, double accuracy = 1e-8, int maxIterations = 100) { double root; @@ -66,7 +67,7 @@ namespace MathNet.Numerics return root; } - throw new NonConvergenceException("The algorithm has exceeded the number of iterations allowed"); + throw new NonConvergenceException(Resources.RootFindingFailed); } } } diff --git a/src/Numerics/Properties/Resources.Designer.cs b/src/Numerics/Properties/Resources.Designer.cs index 2a09d9bb..1ba3e5ab 100644 --- a/src/Numerics/Properties/Resources.Designer.cs +++ b/src/Numerics/Properties/Resources.Designer.cs @@ -1,7 +1,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:4.0.30319.18033 +// Runtime Version:4.0.30319.18051 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -753,6 +753,24 @@ namespace MathNet.Numerics.Properties { } } + /// + /// Looks up a localized string similar to The algorithm has failed, exceeded the number of iterations allowed or there is no root within the provided bounds.. + /// + public static string RootFindingFailed { + get { + return ResourceManager.GetString("RootFindingFailed", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to The algorithm has failed, exceeded the number of iterations allowed or there is no root within the provided bounds. Consider to use RobustNewtonRaphson instead.. + /// + public static string RootFindingFailedRecommendRobustNewtonRaphson { + get { + return ResourceManager.GetString("RootFindingFailedRecommendRobustNewtonRaphson", resourceCulture); + } + } + /// /// Looks up a localized string similar to The lower and upper bounds must bracket a single root.. /// diff --git a/src/Numerics/Properties/Resources.resx b/src/Numerics/Properties/Resources.resx index a402e54b..8f8c7995 100644 --- a/src/Numerics/Properties/Resources.resx +++ b/src/Numerics/Properties/Resources.resx @@ -384,4 +384,10 @@ The lower and upper bounds must bracket a single root. + + The algorithm has failed, exceeded the number of iterations allowed or there is no root within the provided bounds. + + + The algorithm has failed, exceeded the number of iterations allowed or there is no root within the provided bounds. Consider to use RobustNewtonRaphson instead. + \ No newline at end of file diff --git a/src/Numerics/RootFinding/Bisection.cs b/src/Numerics/RootFinding/Bisection.cs index 038887ea..a2b8cac8 100644 --- a/src/Numerics/RootFinding/Bisection.cs +++ b/src/Numerics/RootFinding/Bisection.cs @@ -29,6 +29,7 @@ // using System; +using MathNet.Numerics.Properties; namespace MathNet.Numerics.RootFinding { @@ -68,7 +69,7 @@ namespace MathNet.Numerics.RootFinding { return root; } - throw new NonConvergenceException("The algorithm has failed or exceeded the number of iterations allowed"); + throw new NonConvergenceException(Resources.RootFindingFailed); } /// Find a solution of the equation f(x)=0. diff --git a/src/Numerics/RootFinding/Brent.cs b/src/Numerics/RootFinding/Brent.cs index 87ece682..0ef4897b 100644 --- a/src/Numerics/RootFinding/Brent.cs +++ b/src/Numerics/RootFinding/Brent.cs @@ -29,6 +29,7 @@ // using System; +using MathNet.Numerics.Properties; namespace MathNet.Numerics.RootFinding { @@ -53,7 +54,7 @@ namespace MathNet.Numerics.RootFinding { return root; } - throw new NonConvergenceException("The algorithm has exceeded the number of iterations allowed"); + throw new NonConvergenceException(Resources.RootFindingFailed); } /// Find a solution of the equation f(x)=0. diff --git a/src/Numerics/RootFinding/NewtonRaphson.cs b/src/Numerics/RootFinding/NewtonRaphson.cs index 5fa8d3b9..dfd88fa8 100644 --- a/src/Numerics/RootFinding/NewtonRaphson.cs +++ b/src/Numerics/RootFinding/NewtonRaphson.cs @@ -29,6 +29,7 @@ // using System; +using MathNet.Numerics.Properties; namespace MathNet.Numerics.RootFinding { @@ -55,7 +56,7 @@ namespace MathNet.Numerics.RootFinding { return root; } - throw new NonConvergenceException("The algorithm failed or has exceeded the number of iterations allowed. Consider to use RobustNewtonRaphson instead."); + throw new NonConvergenceException(Resources.RootFindingFailedRecommendRobustNewtonRaphson); } /// Find a solution of the equation f(x)=0. @@ -75,7 +76,7 @@ namespace MathNet.Numerics.RootFinding { return root; } - throw new NonConvergenceException("The algorithm failed or has exceeded the number of iterations allowed. Consider to use RobustNewtonRaphson instead."); + throw new NonConvergenceException(Resources.RootFindingFailedRecommendRobustNewtonRaphson); } /// Find a solution of the equation f(x)=0. diff --git a/src/Numerics/RootFinding/RobustNewtonRaphson.cs b/src/Numerics/RootFinding/RobustNewtonRaphson.cs index 18520334..73a22c81 100644 --- a/src/Numerics/RootFinding/RobustNewtonRaphson.cs +++ b/src/Numerics/RootFinding/RobustNewtonRaphson.cs @@ -29,6 +29,7 @@ // using System; +using MathNet.Numerics.Properties; namespace MathNet.Numerics.RootFinding { @@ -55,7 +56,7 @@ namespace MathNet.Numerics.RootFinding { return root; } - throw new NonConvergenceException("The algorithm has exceeded the number of iterations allowed"); + throw new NonConvergenceException(Resources.RootFindingFailed); } /// Find a solution of the equation f(x)=0. diff --git a/src/UnitTests/RootFindingTests/RobustNewtonRaphsonTest.cs b/src/UnitTests/RootFindingTests/RobustNewtonRaphsonTest.cs index 9f649ea1..dac72389 100644 --- a/src/UnitTests/RootFindingTests/RobustNewtonRaphsonTest.cs +++ b/src/UnitTests/RootFindingTests/RobustNewtonRaphsonTest.cs @@ -76,13 +76,13 @@ namespace MathNet.Numerics.UnitTests.RootFindingTests Func df1 = x => -1/(x*x - 4*x + 4); 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)); + Assert.AreEqual(1.5, FindRoots.OfFunctionDerivative(f1, df1, 1, 6)); Func f2 = x => -1/(x - 2) + 2; Func df2 = x => 1/(x*x - 4*x + 4); 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)); + Assert.AreEqual(2.5, FindRoots.OfFunctionDerivative(f2, df2, -2, 3)); Func f3 = x => 1/(x - 2) + x + 2; Func df3 = x => -1/(x*x - 4*x + 4) + 1;