diff --git a/src/Numerics/Numerics.csproj b/src/Numerics/Numerics.csproj index e0478543..9f659b72 100644 --- a/src/Numerics/Numerics.csproj +++ b/src/Numerics/Numerics.csproj @@ -112,7 +112,6 @@ - diff --git a/src/Numerics/RootFinding/Algorithms/Bisection.cs b/src/Numerics/RootFinding/Algorithms/Bisection.cs index 09d1772f..71b109d2 100644 --- a/src/Numerics/RootFinding/Algorithms/Bisection.cs +++ b/src/Numerics/RootFinding/Algorithms/Bisection.cs @@ -39,7 +39,7 @@ namespace MathNet.Numerics.RootFinding.Algorithms /// public static double FindRootExpand(Func f, double guessLowerBound, double guessUpperBound, double accuracy = 1e-8, double expandFactor = 1.6, int maxExpandIteratons = 100) { - Bracketing.Expand(f, ref guessLowerBound, ref guessUpperBound, expandFactor, maxExpandIteratons); + ZeroCrossingBracketing.Expand(f, ref guessLowerBound, ref guessUpperBound, expandFactor, maxExpandIteratons); return FindRoot(f, guessLowerBound, guessUpperBound, accuracy); } diff --git a/src/Numerics/RootFinding/Algorithms/ZeroCrossingBracketing.cs b/src/Numerics/RootFinding/Algorithms/ZeroCrossingBracketing.cs index 68915567..600449cf 100644 --- a/src/Numerics/RootFinding/Algorithms/ZeroCrossingBracketing.cs +++ b/src/Numerics/RootFinding/Algorithms/ZeroCrossingBracketing.cs @@ -1,5 +1,36 @@ -using System; +// +// 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 System.Collections.Generic; +using MathNet.Numerics.Properties; namespace MathNet.Numerics.RootFinding.Algorithms { @@ -9,8 +40,8 @@ namespace MathNet.Numerics.RootFinding.Algorithms { // TODO: Consider binary-style search instead of linear scan - var fmin = f(lowerBound); - var fmax = f(upperBound); + double fmin = f(lowerBound); + double fmax = f(upperBound); if (Math.Sign(fmin) != Math.Sign(fmax)) { @@ -19,13 +50,13 @@ namespace MathNet.Numerics.RootFinding.Algorithms } double subdiv = (upperBound - lowerBound)/parts; - var smin = lowerBound; + double smin = lowerBound; int sign = Math.Sign(fmin); for (int k = 0; k < parts; k++) { - var smax = smin + subdiv; - var sfmax = f(smax); + double smax = smin + subdiv; + double sfmax = f(smax); if (double.IsInfinity(sfmax)) { // expand interval to include pole @@ -40,5 +71,45 @@ namespace MathNet.Numerics.RootFinding.Algorithms smin = smax; } } + + /// Detect a range containing at least one root. + /// The function to detect roots from. + /// Lower value of the range. + /// Upper value of the range + /// The growing factor of research. Usually 1.6. + /// Maximum number of iterations. Usually 50. + /// True if the bracketing operation succeeded, false otherwise. + /// This iterative methods stops when two values with opposite signs are found. + public static bool Expand(Func f, ref double lowerBound, ref double upperBound, double factor = 1.6, int maxIterations = 50) + { + if (lowerBound >= upperBound) + { + throw new ArgumentOutOfRangeException("upperBound", string.Format(Resources.ArgumentOutOfRangeGreater, "xmax", "xmin")); + } + + double fmin = f(lowerBound); + double fmax = f(upperBound); + + for (int i = 0; i < maxIterations; i++) + { + if (Math.Sign(fmin) != Math.Sign(fmax)) + { + return true; + } + + if (Math.Abs(fmin) < Math.Abs(fmax)) + { + lowerBound += factor * (lowerBound - upperBound); + fmin = f(lowerBound); + } + else + { + upperBound += factor * (upperBound - lowerBound); + fmax = f(upperBound); + } + } + + return false; + } } } diff --git a/src/Numerics/RootFinding/Bracketing.cs b/src/Numerics/RootFinding/Bracketing.cs deleted file mode 100644 index 019c4291..00000000 --- a/src/Numerics/RootFinding/Bracketing.cs +++ /dev/null @@ -1,78 +0,0 @@ -// -// 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.Properties; - -namespace MathNet.Numerics.RootFinding -{ - public static class Bracketing - { - /// Detect a range containing at least one root. - /// The function to detect roots from. - /// Lower value of the range. - /// Upper value of the range - /// The growing factor of research. Usually 1.6. - /// Maximum number of iterations. Usually 50. - /// True if the bracketing operation succeeded, false otherwise. - /// This iterative methods stops when two values with opposite signs are found. - public static bool Expand(Func f, ref double lowerBound, ref double upperBound, double factor = 1.6, int maxIterations = 50) - { - if (lowerBound >= upperBound) - { - throw new ArgumentOutOfRangeException("upperBound", string.Format(Resources.ArgumentOutOfRangeGreater, "xmax", "xmin")); - } - - double fmin = f(lowerBound); - double fmax = f(upperBound); - - for (int i = 0; i < maxIterations; i++) - { - if (Math.Sign(fmin) != Math.Sign(fmax)) - { - return true; - } - - if (Math.Abs(fmin) < Math.Abs(fmax)) - { - lowerBound += factor*(lowerBound - upperBound); - fmin = f(lowerBound); - } - else - { - upperBound += factor*(upperBound - lowerBound); - fmax = f(upperBound); - } - } - - return false; - } - } -} diff --git a/src/Portable/Portable.csproj b/src/Portable/Portable.csproj index df66b7d6..b7c8bed7 100644 --- a/src/Portable/Portable.csproj +++ b/src/Portable/Portable.csproj @@ -996,9 +996,6 @@ RootFinding\Algorithms\ZeroCrossingBracketing.cs - - RootFinding\Bracketing.cs - RootFinding\RealRoots.cs