From 2d4ac98a2971c0628d476bfea4f4a9cbb311aa9f Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Wed, 1 May 2013 17:44:31 +0200 Subject: [PATCH] RootFinding: simplify exception --- src/Numerics/Numerics.csproj | 1 + src/Numerics/RootFinding/BrentRootFinder.cs | 2 +- src/Numerics/RootFinding/RootFinder.cs | 45 ++----------------- .../RootFinding/RootFindingException.cs | 21 +++++++++ 4 files changed, 26 insertions(+), 43 deletions(-) create mode 100644 src/Numerics/RootFinding/RootFindingException.cs diff --git a/src/Numerics/Numerics.csproj b/src/Numerics/Numerics.csproj index b44a866c..23c22630 100644 --- a/src/Numerics/Numerics.csproj +++ b/src/Numerics/Numerics.csproj @@ -111,6 +111,7 @@ + diff --git a/src/Numerics/RootFinding/BrentRootFinder.cs b/src/Numerics/RootFinding/BrentRootFinder.cs index d5f4bb90..364cf333 100644 --- a/src/Numerics/RootFinding/BrentRootFinder.cs +++ b/src/Numerics/RootFinding/BrentRootFinder.cs @@ -109,7 +109,7 @@ namespace MathNet.Numerics.RootFinding } // The algorithm has exceeded the number of iterations allowed - throw new RootFinderException(Resources.AccuracyNotReached, i, new Range(XMin, XMax), Math.Abs(xMid)); + throw new RootFindingException(Resources.AccuracyNotReached, i, XMin, XMax, Math.Abs(xMid)); } } } diff --git a/src/Numerics/RootFinding/RootFinder.cs b/src/Numerics/RootFinding/RootFinder.cs index 4d56a650..8fe6540d 100644 --- a/src/Numerics/RootFinding/RootFinder.cs +++ b/src/Numerics/RootFinding/RootFinder.cs @@ -3,44 +3,7 @@ using MathNet.Numerics.Properties; namespace MathNet.Numerics.RootFinding { - public struct Range - { - double Min, Max; - - public Range(double min, double max) - { - Min = min; Max = max; - } - } - public class RootFinderException : Exception - { - private int m_Iteration; - private Range m_Range; - private double m_Accuracy; - - public RootFinderException(string message, int iteration, Range range, double accuracy) - : base(message) - { - m_Iteration = iteration; - m_Range = range; - m_Accuracy = accuracy; - } - - public int Iteration - { - get { return m_Iteration; } - set { m_Iteration = value; } - } - - public Range Range - { - get { return m_Range; } - set { m_Range = value; } - } - - public double Accuracy { set; get; } - } - public abstract class RootFinder + public abstract class RootFinder { protected const double DOUBLE_ACCURACY = 9.99200722162641E-16; private const int DEFAULT_MAX_ITERATIONS = 30; @@ -59,8 +22,6 @@ namespace MathNet.Numerics.RootFinding Func _func; private double bracketingFactor = 1.6; - - /// Constructor. /// A continuous function. public RootFinder() : this(DEFAULT_MAX_ITERATIONS, DEFAULT_ACCURACY) @@ -117,7 +78,7 @@ namespace MathNet.Numerics.RootFinding { if (xmin >= xmax) { - throw new RootFinderException(string.Format(Resources.ArgumentOutOfRangeGreater,"xmax","xmin"), 0, new Range(xmin, xmax), 0.0); + throw new RootFindingException(string.Format(Resources.ArgumentOutOfRangeGreater,"xmax","xmin"), 0, xmin, xmax, 0.0); } double fmin = _func(xmin); @@ -139,7 +100,7 @@ namespace MathNet.Numerics.RootFinding } } - throw new RootFinderException(Resources.RootNotFound, i, new Range(fmin, fmax), 0.0); + throw new RootFindingException(Resources.RootNotFound, i, fmin, fmax, 0.0); } /// Prototype algorithm for solving the equation f(x)=0. diff --git a/src/Numerics/RootFinding/RootFindingException.cs b/src/Numerics/RootFinding/RootFindingException.cs new file mode 100644 index 00000000..3aaac181 --- /dev/null +++ b/src/Numerics/RootFinding/RootFindingException.cs @@ -0,0 +1,21 @@ +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