From d5666d7b6784e8c68e66ce2cf967ae1f954598ff Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Fri, 3 May 2013 12:25:29 +0200 Subject: [PATCH] RootFinding: apply common algorithms pattern --- src/Numerics/Numerics.csproj | 4 +- .../{FindRoots.cs => Algorithms/Brent.cs} | 8 ++-- .../RootFinding/FloatingPointRoots.cs | 43 +++++++++++++++++++ src/Portable/Portable.csproj | 7 ++- src/UnitTests/RootFindingTests/BrentTest.cs | 4 +- 5 files changed, 57 insertions(+), 9 deletions(-) rename src/Numerics/RootFinding/{FindRoots.cs => Algorithms/Brent.cs} (95%) create mode 100644 src/Numerics/RootFinding/FloatingPointRoots.cs diff --git a/src/Numerics/Numerics.csproj b/src/Numerics/Numerics.csproj index 8faea8bed..c3d618514 100644 --- a/src/Numerics/Numerics.csproj +++ b/src/Numerics/Numerics.csproj @@ -111,7 +111,8 @@ - + + @@ -486,6 +487,7 @@ + diff --git a/src/Numerics/RootFinding/FindRoots.cs b/src/Numerics/RootFinding/Algorithms/Brent.cs similarity index 95% rename from src/Numerics/RootFinding/FindRoots.cs rename to src/Numerics/RootFinding/Algorithms/Brent.cs index b62b58b8a..dcfb8e9f5 100644 --- a/src/Numerics/RootFinding/FindRoots.cs +++ b/src/Numerics/RootFinding/Algorithms/Brent.cs @@ -1,4 +1,4 @@ -// +// // Math.NET Numerics, part of the Math.NET Project // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics @@ -30,9 +30,9 @@ using System; -namespace MathNet.Numerics.RootFinding +namespace MathNet.Numerics.RootFinding.Algorithms { - public static class FindRoots + public static class Brent { /// Find a solution of the equation f(x)=0. /// The function to find roots from. @@ -46,7 +46,7 @@ public static class FindRoots /// Implementation inspired by Press, Teukolsky, Vetterling, and Flannery, "Numerical Recipes in C", 2nd edition, Cambridge University Press /// /// - public static double BrentMethod(Func f, double xmin, double xmax, double accuracy = 1e-8, int maxIterations = 100) + public static double FindRoot(Func f, double xmin, double xmax, double accuracy = 1e-8, int maxIterations = 100) { double fxmin = f(xmin); double fxmax = f(xmax); diff --git a/src/Numerics/RootFinding/FloatingPointRoots.cs b/src/Numerics/RootFinding/FloatingPointRoots.cs new file mode 100644 index 000000000..0c24ce3c7 --- /dev/null +++ b/src/Numerics/RootFinding/FloatingPointRoots.cs @@ -0,0 +1,43 @@ +// +// Math.NET Numerics, part of the Math.NET Project +// http://numerics.mathdotnet.com +// http://github.com/mathnet/mathnet-numerics +// http://mathnetnumerics.codeplex.com +// +// Copyright (c) 2009-2013 Math.NET +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// + +using System; +using MathNet.Numerics.RootFinding.Algorithms; + +namespace MathNet.Numerics.RootFinding +{ + public static class FloatingPointRoots + { + public static double Find(Func f, double xmin, double xmax) + { + return Brent.FindRoot(f, xmin, xmax, 1e-8, 100); + } + } +} diff --git a/src/Portable/Portable.csproj b/src/Portable/Portable.csproj index b190ce8c2..0c9f57ee1 100644 --- a/src/Portable/Portable.csproj +++ b/src/Portable/Portable.csproj @@ -984,11 +984,14 @@ Random\Xorshift.cs + + RootFinding\Algorithms\Brent.cs + RootFinding\Bracketing.cs - - RootFinding\FindRoots.cs + + RootFinding\FloatingPointRoots.cs SerializableAttribute.cs diff --git a/src/UnitTests/RootFindingTests/BrentTest.cs b/src/UnitTests/RootFindingTests/BrentTest.cs index 1610b7421..d5bc1d174 100644 --- a/src/UnitTests/RootFindingTests/BrentTest.cs +++ b/src/UnitTests/RootFindingTests/BrentTest.cs @@ -28,7 +28,7 @@ // OTHER DEALINGS IN THE SOFTWARE. // -using MathNet.Numerics.RootFinding; +using MathNet.Numerics.RootFinding.Algorithms; using NUnit.Framework; namespace MathNet.Numerics.UnitTests.RootFindingTests @@ -39,7 +39,7 @@ public class BrentTest [Test] public void MultipleRoots() { - double root = FindRoots.BrentMethod(x => x*x - 4, -5, 5, 1e-14, 100); + double root = Brent.FindRoot(x => x*x - 4, -5, 5, 1e-14, 100); Assert.AreEqual(0, root*root - 4); } }