Browse Source

Common NonConvergenceException

pull/121/head
Christoph Ruegg 13 years ago
parent
commit
f142f3abb7
  1. 28
      src/Numerics/NonConvergenceException.cs
  2. 2
      src/Numerics/Numerics.csproj
  3. 10
      src/Numerics/RootFinding/FindRoots.cs
  4. 21
      src/Numerics/RootFinding/RootFindingException.cs

28
src/Numerics/NonConvergenceException.cs

@ -0,0 +1,28 @@
using System;
using System.Runtime.Serialization;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics
{
/// <summary>
/// An algorithm failed to converge.
/// </summary>
public class NonConvergenceException : Exception
{
public NonConvergenceException() : base(Resources.ConvergenceFailed)
{
}
public NonConvergenceException(string message) : base(message)
{
}
public NonConvergenceException(string message, Exception innerException) : base(message, innerException)
{
}
protected NonConvergenceException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
}

2
src/Numerics/Numerics.csproj

@ -109,9 +109,9 @@
<Compile Include="Financial\AbsoluteRiskMeasures.cs" />
<Compile Include="LinearAlgebra\Generic\Matrix.BCL.cs" />
<Compile Include="LinearAlgebra\Generic\Vector.BCL.cs" />
<Compile Include="NonConvergenceException.cs" />
<Compile Include="RootFinding\Bracketing.cs" />
<Compile Include="RootFinding\FindRoots.cs" />
<Compile Include="RootFinding\RootFindingException.cs" />
<Compile Include="SpecialFunctions\Evaluate.cs" />
<Compile Include="SpecialFunctions\ModifiedStruve.cs" />
<Compile Include="SpecialFunctions\ModifiedBessel.cs" />

10
src/Numerics/RootFinding/FindRoots.cs

@ -1,5 +1,4 @@
using System;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.RootFinding
{
@ -16,15 +15,14 @@ namespace MathNet.Numerics.RootFinding
/// Algorithm by by Brent, Van Wijngaarden, Dekker et al.
/// Implementation inspired by Press, Teukolsky, Vetterling, and Flannery, "Numerical Recipes in C", 2nd edition, Cambridge University Press
/// </remarks>
/// <exception cref="NonConvergenceException"></exception>
public static double BrentMethod(Func<double, double> f, double xmin, double xmax, double accuracy = 1e-8, int maxIterations = 100)
{
double xMid = 0;
double d = 0.0, e = 0.0;
double fxmin = f(xmin);
double fxmax = f(xmax);
double root = xmax;
double froot = fxmax;
double d = 0.0, e = 0.0;
for (int i = 0; i <= maxIterations; i++)
{
@ -48,7 +46,7 @@ namespace MathNet.Numerics.RootFinding
// convergence check
double xAcc1 = 2.0 * Precision.DoubleMachinePrecision * Math.Abs(root) + 0.5 * accuracy;
xMid = (xmax - root) / 2.0;
double xMid = (xmax - root) / 2.0;
if (Math.Abs(xMid) <= xAcc1 || froot.AlmostEqualWithAbsoluteError(0, froot, accuracy))
{
return root;
@ -112,7 +110,7 @@ namespace MathNet.Numerics.RootFinding
}
// The algorithm has exceeded the number of iterations allowed
throw new RootFindingException(Resources.AccuracyNotReached, maxIterations, xmin, xmax, Math.Abs(xMid));
throw new NonConvergenceException();
}
/// <summary>Helper method useful for preventing rounding errors.</summary>

21
src/Numerics/RootFinding/RootFindingException.cs

@ -1,21 +0,0 @@
using System;
namespace MathNet.Numerics.RootFinding
{
public class RootFindingException : Exception
{
public RootFindingException(string message, int iteration, double rangeMin, double rangeMax, double accuracy)
: base(message)
{
Iteration = iteration;
RangeMin = rangeMin;
RangeMax = rangeMax;
Accuracy = accuracy;
}
public int Iteration { get; set; }
public double RangeMin { get; set; }
public double RangeMax { get; set; }
public double Accuracy { set; get; }
}
}
Loading…
Cancel
Save