From 5749cc36e7231651a92edc4386113d5998c08ea0 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sun, 30 Jun 2013 12:13:20 +0200 Subject: [PATCH] RootFinding: F# module --- src/FSharp/FSharp.fsproj | 3 +- src/FSharp/FindRoots.fs | 61 ++++++++++++++++++++++++ src/FSharpPortable/FSharpPortable.fsproj | 3 ++ 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 src/FSharp/FindRoots.fs diff --git a/src/FSharp/FSharp.fsproj b/src/FSharp/FSharp.fsproj index 8b141462..26bf6816 100644 --- a/src/FSharp/FSharp.fsproj +++ b/src/FSharp/FSharp.fsproj @@ -71,8 +71,9 @@ - + + diff --git a/src/FSharp/FindRoots.fs b/src/FSharp/FindRoots.fs new file mode 100644 index 00000000..9ec6173f --- /dev/null +++ b/src/FSharp/FindRoots.fs @@ -0,0 +1,61 @@ +// +// 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. +// + +namespace MathNet.Numerics + +open System +open MathNet.Numerics.RootFinding + +[] +module FindRoots = + + let private tobcl (f:'a->'b) = Func<'a,'b>(f) + let private (|>|) option orElse = match option with | Some x -> Some x | None -> orElse() + + // direct algorithms + + let brent maxIterations accuracy lowerBound upperBound (f:double->double) = + match Brent.TryFindRoot(tobcl f, lowerBound, upperBound, accuracy, maxIterations) with + | true, root -> Some root + | false, _ -> None + + let newtonRaphsonRobust maxIterations subdivision accuracy lowerBound upperBound (f:double->double) (df:double->double) = + match RobustNewtonRaphson.TryFindRoot(tobcl f, tobcl df, lowerBound, upperBound, accuracy, maxIterations, subdivision) with + | true, root -> Some root + | false, _ -> None + + + // simple usage + + let ofFunction lowerBound upperBound (f:double->double) = brent 100 1e-8 lowerBound upperBound f + + let ofFunctionAndDerivative lowerBound upperBound (f:double->double) (df:double->double) = + newtonRaphsonRobust 100 20 1e-8 lowerBound upperBound f df + |>| fun () -> brent 100 1e-8 lowerBound upperBound f diff --git a/src/FSharpPortable/FSharpPortable.fsproj b/src/FSharpPortable/FSharpPortable.fsproj index 763334b2..00a8ae3b 100644 --- a/src/FSharpPortable/FSharpPortable.fsproj +++ b/src/FSharpPortable/FSharpPortable.fsproj @@ -80,6 +80,9 @@ Fit.fs + + FindRoots.fs + RandomVariable.fs