Browse Source

RootFinding: drop 'and' in functions; migrate exception text to resx.

v2
Christoph Ruegg 13 years ago
parent
commit
e126c553db
  1. 2
      src/FSharp/FindRoots.fs
  2. 4
      src/FSharpUnitTests/FindRootsTests.fs
  3. 7
      src/Numerics/FindRoots.cs
  4. 20
      src/Numerics/Properties/Resources.Designer.cs
  5. 6
      src/Numerics/Properties/Resources.resx
  6. 3
      src/Numerics/RootFinding/Bisection.cs
  7. 3
      src/Numerics/RootFinding/Brent.cs
  8. 5
      src/Numerics/RootFinding/NewtonRaphson.cs
  9. 3
      src/Numerics/RootFinding/RobustNewtonRaphson.cs
  10. 4
      src/UnitTests/RootFindingTests/RobustNewtonRaphsonTest.cs

2
src/FSharp/FindRoots.fs

@ -73,6 +73,6 @@ module FindRoots =
brent 100 1e-8 lowerBound upperBound f
|>| fun () -> bisection 100 1e-8 lowerBound upperBound f
let ofFunctionAndDerivative lowerBound upperBound (f:double->double) (df:double->double) =
let ofFunctionDerivative lowerBound upperBound (f:double->double) (df:double->double) =
newtonRaphsonRobust 100 20 1e-8 lowerBound upperBound f df
|>| fun () -> bisection 100 1e-8 lowerBound upperBound f

4
src/FSharpUnitTests/FindRootsTests.fs

@ -39,5 +39,5 @@ module FindRootsTests =
let ``Simple method should find both roots of (x - 3) * (x - 4)``() =
f |> FindRoots.ofFunction -5.0 3.5 |> Option.get |> should (equalWithin 1e-8) 3.0
f |> FindRoots.ofFunction 3.2 5.0 |> Option.get |> should (equalWithin 1e-8) 4.0
(f, df) ||> FindRoots.ofFunctionAndDerivative -5.0 3.5 |> Option.get |> should (equalWithin 1e-8) 3.0
(f, df) ||> FindRoots.ofFunctionAndDerivative 3.2 5.0 |> Option.get |> should (equalWithin 1e-8) 4.0
(f, df) ||> FindRoots.ofFunctionDerivative -5.0 3.5 |> Option.get |> should (equalWithin 1e-8) 3.0
(f, df) ||> FindRoots.ofFunctionDerivative 3.2 5.0 |> Option.get |> should (equalWithin 1e-8) 4.0

7
src/Numerics/FindRoots.cs

@ -29,6 +29,7 @@
// </copyright>
using System;
using MathNet.Numerics.Properties;
using MathNet.Numerics.RootFinding;
namespace MathNet.Numerics
@ -49,10 +50,10 @@ namespace MathNet.Numerics
return root;
}
throw new NonConvergenceException("The algorithm has failed or exceeded the number of iterations allowed");
throw new NonConvergenceException(Resources.RootFindingFailed);
}
public static double OfFunctionAndDerivative(Func<double, double> f, Func<double, double> df, double lowerBound, double upperBound, double accuracy = 1e-8, int maxIterations = 100)
public static double OfFunctionDerivative(Func<double, double> f, Func<double, double> df, double lowerBound, double upperBound, double accuracy = 1e-8, int maxIterations = 100)
{
double root;
@ -66,7 +67,7 @@ namespace MathNet.Numerics
return root;
}
throw new NonConvergenceException("The algorithm has exceeded the number of iterations allowed");
throw new NonConvergenceException(Resources.RootFindingFailed);
}
}
}

20
src/Numerics/Properties/Resources.Designer.cs

@ -1,7 +1,7 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.18033
// Runtime Version:4.0.30319.18051
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
@ -753,6 +753,24 @@ namespace MathNet.Numerics.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to The algorithm has failed, exceeded the number of iterations allowed or there is no root within the provided bounds..
/// </summary>
public static string RootFindingFailed {
get {
return ResourceManager.GetString("RootFindingFailed", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to The algorithm has failed, exceeded the number of iterations allowed or there is no root within the provided bounds. Consider to use RobustNewtonRaphson instead..
/// </summary>
public static string RootFindingFailedRecommendRobustNewtonRaphson {
get {
return ResourceManager.GetString("RootFindingFailedRecommendRobustNewtonRaphson", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to The lower and upper bounds must bracket a single root..
/// </summary>

6
src/Numerics/Properties/Resources.resx

@ -384,4 +384,10 @@
<data name="RootMustBeBracketedByBounds" xml:space="preserve">
<value>The lower and upper bounds must bracket a single root.</value>
</data>
<data name="RootFindingFailed" xml:space="preserve">
<value>The algorithm has failed, exceeded the number of iterations allowed or there is no root within the provided bounds.</value>
</data>
<data name="RootFindingFailedRecommendRobustNewtonRaphson" xml:space="preserve">
<value>The algorithm has failed, exceeded the number of iterations allowed or there is no root within the provided bounds. Consider to use RobustNewtonRaphson instead.</value>
</data>
</root>

3
src/Numerics/RootFinding/Bisection.cs

@ -29,6 +29,7 @@
// </copyright>
using System;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.RootFinding
{
@ -68,7 +69,7 @@ namespace MathNet.Numerics.RootFinding
{
return root;
}
throw new NonConvergenceException("The algorithm has failed or exceeded the number of iterations allowed");
throw new NonConvergenceException(Resources.RootFindingFailed);
}
/// <summary>Find a solution of the equation f(x)=0.</summary>

3
src/Numerics/RootFinding/Brent.cs

@ -29,6 +29,7 @@
// </copyright>
using System;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.RootFinding
{
@ -53,7 +54,7 @@ namespace MathNet.Numerics.RootFinding
{
return root;
}
throw new NonConvergenceException("The algorithm has exceeded the number of iterations allowed");
throw new NonConvergenceException(Resources.RootFindingFailed);
}
/// <summary>Find a solution of the equation f(x)=0.</summary>

5
src/Numerics/RootFinding/NewtonRaphson.cs

@ -29,6 +29,7 @@
// </copyright>
using System;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.RootFinding
{
@ -55,7 +56,7 @@ namespace MathNet.Numerics.RootFinding
{
return root;
}
throw new NonConvergenceException("The algorithm failed or has exceeded the number of iterations allowed. Consider to use RobustNewtonRaphson instead.");
throw new NonConvergenceException(Resources.RootFindingFailedRecommendRobustNewtonRaphson);
}
/// <summary>Find a solution of the equation f(x)=0.</summary>
@ -75,7 +76,7 @@ namespace MathNet.Numerics.RootFinding
{
return root;
}
throw new NonConvergenceException("The algorithm failed or has exceeded the number of iterations allowed. Consider to use RobustNewtonRaphson instead.");
throw new NonConvergenceException(Resources.RootFindingFailedRecommendRobustNewtonRaphson);
}
/// <summary>Find a solution of the equation f(x)=0.</summary>

3
src/Numerics/RootFinding/RobustNewtonRaphson.cs

@ -29,6 +29,7 @@
// </copyright>
using System;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.RootFinding
{
@ -55,7 +56,7 @@ namespace MathNet.Numerics.RootFinding
{
return root;
}
throw new NonConvergenceException("The algorithm has exceeded the number of iterations allowed");
throw new NonConvergenceException(Resources.RootFindingFailed);
}
/// <summary>Find a solution of the equation f(x)=0.</summary>

4
src/UnitTests/RootFindingTests/RobustNewtonRaphsonTest.cs

@ -76,13 +76,13 @@ namespace MathNet.Numerics.UnitTests.RootFindingTests
Func<double, double> df1 = x => -1/(x*x - 4*x + 4);
Assert.AreEqual(1.5, RobustNewtonRaphson.FindRoot(f1, df1, 1, 2, 1e-14, 100, 20));
Assert.AreEqual(1.5, RobustNewtonRaphson.FindRoot(f1, df1, 1, 6, 1e-14, 100, 20));
Assert.AreEqual(1.5, FindRoots.OfFunctionAndDerivative(f1, df1, 1, 6));
Assert.AreEqual(1.5, FindRoots.OfFunctionDerivative(f1, df1, 1, 6));
Func<double, double> f2 = x => -1/(x - 2) + 2;
Func<double, double> df2 = x => 1/(x*x - 4*x + 4);
Assert.AreEqual(2.5, RobustNewtonRaphson.FindRoot(f2, df2, 2, 3, 1e-14, 100, 20));
Assert.AreEqual(2.5, RobustNewtonRaphson.FindRoot(f2, df2, -2, 3, 1e-14, 100, 20));
Assert.AreEqual(2.5, FindRoots.OfFunctionAndDerivative(f2, df2, -2, 3));
Assert.AreEqual(2.5, FindRoots.OfFunctionDerivative(f2, df2, -2, 3));
Func<double, double> f3 = x => 1/(x - 2) + x + 2;
Func<double, double> df3 = x => -1/(x*x - 4*x + 4) + 1;

Loading…
Cancel
Save