diff --git a/src/Numerics/NonConvergenceException.cs b/src/Numerics/NonConvergenceException.cs new file mode 100644 index 00000000..cf800d8f --- /dev/null +++ b/src/Numerics/NonConvergenceException.cs @@ -0,0 +1,28 @@ +using System; +using System.Runtime.Serialization; +using MathNet.Numerics.Properties; + +namespace MathNet.Numerics +{ + /// + /// An algorithm failed to converge. + /// + 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) + { + } + } +} diff --git a/src/Numerics/Numerics.csproj b/src/Numerics/Numerics.csproj index 2eee6c0b..8faea8be 100644 --- a/src/Numerics/Numerics.csproj +++ b/src/Numerics/Numerics.csproj @@ -109,9 +109,9 @@ + - diff --git a/src/Numerics/RootFinding/FindRoots.cs b/src/Numerics/RootFinding/FindRoots.cs index 5604cabf..6a4e243b 100644 --- a/src/Numerics/RootFinding/FindRoots.cs +++ b/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 /// + /// public static double BrentMethod(Func 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(); } /// Helper method useful for preventing rounding errors. diff --git a/src/Numerics/RootFinding/RootFindingException.cs b/src/Numerics/RootFinding/RootFindingException.cs deleted file mode 100644 index 3aaac181..00000000 --- a/src/Numerics/RootFinding/RootFindingException.cs +++ /dev/null @@ -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; } - } -} \ No newline at end of file