Skip to content

Commit

Permalink
RootFinding: apply common algorithms pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
cdrnet committed May 3, 2013
1 parent 9570ae1 commit d5666d7
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/Numerics/Numerics.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@
<Compile Include="LinearAlgebra\Generic\Vector.BCL.cs" />
<Compile Include="NonConvergenceException.cs" />
<Compile Include="RootFinding\Bracketing.cs" />
<Compile Include="RootFinding\FindRoots.cs" />
<Compile Include="RootFinding\Algorithms\Brent.cs" />
<Compile Include="RootFinding\FloatingPointRoots.cs" />
<Compile Include="SpecialFunctions\Evaluate.cs" />
<Compile Include="SpecialFunctions\ModifiedStruve.cs" />
<Compile Include="SpecialFunctions\ModifiedBessel.cs" />
Expand Down Expand Up @@ -486,6 +487,7 @@
<ItemGroup>
<None Include="MathNet.Numerics.snk" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PreBuildEvent>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="FindRoots.cs" company="Math.NET">
// <copyright file="Brent.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
Expand Down Expand Up @@ -30,9 +30,9 @@

using System;

namespace MathNet.Numerics.RootFinding
namespace MathNet.Numerics.RootFinding.Algorithms
{
public static class FindRoots
public static class Brent
{
/// <summary>Find a solution of the equation f(x)=0.</summary>
/// <param name="f">The function to find roots from.</param>
Expand All @@ -46,7 +46,7 @@ public static class FindRoots
/// 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)
public static double FindRoot(Func<double, double> f, double xmin, double xmax, double accuracy = 1e-8, int maxIterations = 100)
{
double fxmin = f(xmin);
double fxmax = f(xmax);
Expand Down
43 changes: 43 additions & 0 deletions src/Numerics/RootFinding/FloatingPointRoots.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// <copyright file="FloatingPointRoots.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.RootFinding.Algorithms;

namespace MathNet.Numerics.RootFinding
{
public static class FloatingPointRoots
{
public static double Find(Func<double, double> f, double xmin, double xmax)
{
return Brent.FindRoot(f, xmin, xmax, 1e-8, 100);
}
}
}
7 changes: 5 additions & 2 deletions src/Portable/Portable.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -984,11 +984,14 @@
<Compile Include="..\Numerics\Random\Xorshift.cs">
<Link>Random\Xorshift.cs</Link>
</Compile>
<Compile Include="..\Numerics\RootFinding\Algorithms\Brent.cs">
<Link>RootFinding\Algorithms\Brent.cs</Link>
</Compile>
<Compile Include="..\Numerics\RootFinding\Bracketing.cs">
<Link>RootFinding\Bracketing.cs</Link>
</Compile>
<Compile Include="..\Numerics\RootFinding\FindRoots.cs">
<Link>RootFinding\FindRoots.cs</Link>
<Compile Include="..\Numerics\RootFinding\FloatingPointRoots.cs">
<Link>RootFinding\FloatingPointRoots.cs</Link>
</Compile>
<Compile Include="..\Numerics\SerializableAttribute.cs">
<Link>SerializableAttribute.cs</Link>
Expand Down
4 changes: 2 additions & 2 deletions src/UnitTests/RootFindingTests/BrentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>

using MathNet.Numerics.RootFinding;
using MathNet.Numerics.RootFinding.Algorithms;
using NUnit.Framework;

namespace MathNet.Numerics.UnitTests.RootFindingTests
Expand All @@ -39,7 +39,7 @@ public class BrentTest
[Test]
public void MultipleRoots()
{
double root = FindRoots.BrentMethod(x => x*x - 4, -5, 5, 1e-14, 100);
double root = Brent.FindRoot(x => x*x - 4, -5, 5, 1e-14, 100);
Assert.AreEqual(0, root*root - 4);
}
}
Expand Down

0 comments on commit d5666d7

Please sign in to comment.