Browse Source

RootFinding: add very basic F# unit tests

v2
Christoph Ruegg 13 years ago
parent
commit
ed6f278cdf
  1. 3
      src/FSharpPortableUnitTests/FSharpPortableUnitTests.fsproj
  2. 1
      src/FSharpUnitTests/FSharpUnitTests.fsproj
  3. 43
      src/FSharpUnitTests/FindRootsTests.fs

3
src/FSharpPortableUnitTests/FSharpPortableUnitTests.fsproj

@ -95,6 +95,9 @@
<Compile Include="..\FSharpUnitTests\FitTests.fs">
<Link>FitTests.fs</Link>
</Compile>
<Compile Include="..\FSharpUnitTests\FindRootsTests.fs">
<Link>FindRootsTests.fs</Link>
</Compile>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>

1
src/FSharpUnitTests/FSharpUnitTests.fsproj

@ -80,6 +80,7 @@
<Compile Include="RandomVariableTests.fs" />
<Compile Include="PokerTests.fs" />
<Compile Include="FitTests.fs" />
<Compile Include="FindRootsTests.fs" />
<None Include="packages.config" />
<None Include="App.config" />
</ItemGroup>

43
src/FSharpUnitTests/FindRootsTests.fs

@ -0,0 +1,43 @@
namespace MathNet.Numerics.Tests
open System
open MathNet.Numerics
open NUnit.Framework
open FsUnit
module FindRootsTests =
let f x = (x - 3.0)*(x - 4.0)
let df x = 2.0*x - 7.0
[<Test>]
let ``Bisection should find both roots of (x - 3) * (x - 4)``() =
f |> FindRoots.bisection 100 1e-14 -5.0 3.5 |> should equal (Some 3.0)
f |> FindRoots.bisection 100 1e-14 3.2 5.0 |> should equal (Some 4.0)
[<Test>]
let ``Brent should find both roots of (x - 3) * (x - 4)``() =
f |> FindRoots.brent 100 1e-14 -5.0 3.5 |> should equal (Some 3.0)
f |> FindRoots.brent 100 1e-14 3.2 5.0 |> should equal (Some 4.0)
[<Test>]
let ``Newton-Raphson should find both roots of (x - 3) * (x - 4)``() =
(f, df) ||> FindRoots.newtonRaphson 100 1e-14 -5.0 3.5 |> should equal (Some 3.0)
(f, df) ||> FindRoots.newtonRaphson 100 1e-14 3.2 5.0 |> should equal (Some 4.0)
[<Test>]
let ``Newton-Raphson by Guess should find both roots of (x - 3) * (x - 4)``() =
(f, df) ||> FindRoots.newtonRaphsonGuess 100 1e-14 2.8 |> should equal (Some 3.0)
(f, df) ||> FindRoots.newtonRaphsonGuess 100 1e-14 3.7 |> should equal (Some 4.0)
[<Test>]
let ``Robust Newton-Raphson should find both roots of (x - 3) * (x - 4)``() =
(f, df) ||> FindRoots.newtonRaphsonRobust 100 20 1e-14 -5.0 3.5 |> should equal (Some 3.0)
(f, df) ||> FindRoots.newtonRaphsonRobust 100 20 1e-14 3.2 5.0 |> should equal (Some 4.0)
[<Test>]
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
Loading…
Cancel
Save