Browse Source

RootFinding: merge bracketing class into ZeroCrossingBracketing

pull/121/head
Christoph Ruegg 13 years ago
parent
commit
ca033cbae2
  1. 1
      src/Numerics/Numerics.csproj
  2. 2
      src/Numerics/RootFinding/Algorithms/Bisection.cs
  3. 83
      src/Numerics/RootFinding/Algorithms/ZeroCrossingBracketing.cs
  4. 78
      src/Numerics/RootFinding/Bracketing.cs
  5. 3
      src/Portable/Portable.csproj

1
src/Numerics/Numerics.csproj

@ -112,7 +112,6 @@
<Compile Include="NonConvergenceException.cs" />
<Compile Include="RootFinding\Algorithms\HybridNewtonRaphson.cs" />
<Compile Include="RootFinding\Algorithms\ZeroCrossingBracketing.cs" />
<Compile Include="RootFinding\Bracketing.cs" />
<Compile Include="RootFinding\Algorithms\Brent.cs" />
<Compile Include="RootFinding\RealRoots.cs" />
<Compile Include="RootFinding\Algorithms\Bisection.cs" />

2
src/Numerics/RootFinding/Algorithms/Bisection.cs

@ -39,7 +39,7 @@ namespace MathNet.Numerics.RootFinding.Algorithms
/// <exception cref="NonConvergenceException"></exception>
public static double FindRootExpand(Func<double, double> 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);
}

83
src/Numerics/RootFinding/Algorithms/ZeroCrossingBracketing.cs

@ -1,5 +1,36 @@
using System;
// <copyright file="ZeroCrossingBracketing.cs" company="Math.NET">
// 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.
// </copyright>
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;
}
}
/// <summary>Detect a range containing at least one root.</summary>
/// <param name="f">The function to detect roots from.</param>
/// <param name="lowerBound">Lower value of the range.</param>
/// <param name="upperBound">Upper value of the range</param>
/// <param name="factor">The growing factor of research. Usually 1.6.</param>
/// <param name="maxIterations">Maximum number of iterations. Usually 50.</param>
/// <returns>True if the bracketing operation succeeded, false otherwise.</returns>
/// <remarks>This iterative methods stops when two values with opposite signs are found.</remarks>
public static bool Expand(Func<double, double> 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;
}
}
}

78
src/Numerics/RootFinding/Bracketing.cs

@ -1,78 +0,0 @@
// <copyright file="Bracketing.cs" company="Math.NET">
// 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.
// </copyright>
using System;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.RootFinding
{
public static class Bracketing
{
/// <summary>Detect a range containing at least one root.</summary>
/// <param name="f">The function to detect roots from.</param>
/// <param name="lowerBound">Lower value of the range.</param>
/// <param name="upperBound">Upper value of the range</param>
/// <param name="factor">The growing factor of research. Usually 1.6.</param>
/// <param name="maxIterations">Maximum number of iterations. Usually 50.</param>
/// <returns>True if the bracketing operation succeeded, false otherwise.</returns>
/// <remarks>This iterative methods stops when two values with opposite signs are found.</remarks>
public static bool Expand(Func<double, double> 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;
}
}
}

3
src/Portable/Portable.csproj

@ -996,9 +996,6 @@
<Compile Include="..\Numerics\RootFinding\Algorithms\ZeroCrossingBracketing.cs">
<Link>RootFinding\Algorithms\ZeroCrossingBracketing.cs</Link>
</Compile>
<Compile Include="..\Numerics\RootFinding\Bracketing.cs">
<Link>RootFinding\Bracketing.cs</Link>
</Compile>
<Compile Include="..\Numerics\RootFinding\RealRoots.cs">
<Link>RootFinding\RealRoots.cs</Link>
</Compile>

Loading…
Cancel
Save