Browse Source

Differentiation: F# module for better F# function support

cuda
Christoph Ruegg 12 years ago
parent
commit
fb1f888089
  1. 63
      src/FSharp/Differentiate.fs
  2. 1
      src/FSharp/FSharp-Net35.fsproj
  3. 1
      src/FSharp/FSharp-Portable47.fsproj
  4. 1
      src/FSharp/FSharp.fsproj
  5. 18
      src/FSharp/FindRoots.fs
  6. 4
      src/FSharp/Random.fs

63
src/FSharp/Differentiate.fs

@ -0,0 +1,63 @@
// <copyright file="Differentiate.fs" 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-2015 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>
namespace MathNet.Numerics
open System
open MathNet.Numerics
open MathNet.Numerics.Differentiation
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module Differentiate =
let private tobcl (f:'a->'b) = Func<'a,'b>(f)
let private tofs (f:Func<_,_>) = fun a -> f.Invoke(a)
let private tofs2 (f:Func<_,_>) = fun a b -> f.Invoke([|a;b|])
let derivative order x (f:float->float) = Differentiate.Derivative(tobcl f, x, order)
let derivativeFunc order (f:float->float) = Differentiate.DerivativeFunc(tobcl f, order) |> tofs
let firstDerivative x (f:float->float) = Differentiate.FirstDerivative(tobcl f, x)
let firstDerivativeFunc (f:float->float) = Differentiate.FirstDerivativeFunc(tobcl f) |> tofs
let secondDerivative x (f:float->float) = Differentiate.SecondDerivative(tobcl f, x)
let secondDerivativeFunc (f:float->float) = Differentiate.SecondDerivativeFunc(tobcl f) |> tofs
let partialDerivative order parameterIndex x (f:float[]->float) = Differentiate.PartialDerivative(tobcl f, x, parameterIndex, order)
let partialDerivativeFunc order parameterIndex (f:float[]->float) = Differentiate.PartialDerivativeFunc(tobcl f, parameterIndex, order) |> tofs
let firstPartialDerivative parameterIndex x (f:float[]->float) = Differentiate.FirstPartialDerivative(tobcl f, x, parameterIndex)
let firstPartialDerivativeFunc parameterIndex (f:float[]->float) = Differentiate.FirstPartialDerivativeFunc(tobcl f, parameterIndex) |> tofs
let partialDerivative2 order parameterIndex x (f:float->float->float) = Differentiate.PartialDerivative((fun x -> f x.[0] x.[1]), x, parameterIndex, order)
let partialDerivative2Func order parameterIndex (f:float->float->float) = Differentiate.PartialDerivativeFunc((fun x -> f x.[0] x.[1]), parameterIndex, order) |> tofs2
let firstPartialDerivative2 parameterIndex x (f:float->float->float) = Differentiate.FirstPartialDerivative((fun x -> f x.[0] x.[1]), x, parameterIndex)
let firstPartialDerivative2Func parameterIndex (f:float->float->float) = Differentiate.FirstPartialDerivativeFunc((fun x -> f x.[0] x.[1]), parameterIndex) |> tofs2

1
src/FSharp/FSharp-Net35.fsproj

@ -70,6 +70,7 @@
<Compile Include="BigIntegerExtensions.fs" />
<Compile Include="BigRational.fsi" />
<Compile Include="BigRational.fs" />
<Compile Include="Differentiate.fs" />
<Compile Include="Fit.fs" />
<Compile Include="FindRoots.fs" />
<Compile Include="RandomVariable.fs" />

1
src/FSharp/FSharp-Portable47.fsproj

@ -58,6 +58,7 @@
<Compile Include="BigIntegerExtensions.fs" />
<Compile Include="BigRational.fsi" />
<Compile Include="BigRational.fs" />
<Compile Include="Differentiate.fs" />
<Compile Include="Fit.fs" />
<Compile Include="FindRoots.fs" />
<Compile Include="RandomVariable.fs" />

1
src/FSharp/FSharp.fsproj

@ -70,6 +70,7 @@
<Compile Include="BigIntegerExtensions.fs" />
<Compile Include="BigRational.fsi" />
<Compile Include="BigRational.fs" />
<Compile Include="Differentiate.fs" />
<Compile Include="Fit.fs" />
<Compile Include="FindRoots.fs" />
<Compile Include="RandomVariable.fs" />

18
src/FSharp/FindRoots.fs

@ -37,46 +37,46 @@ 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()
let private (|>|) option orElse = match option with | Some x -> Some x | None -> orElse()
// direct algorithms
let bisection maxIterations accuracy lowerBound upperBound (f:double->double) =
let bisection maxIterations accuracy lowerBound upperBound (f:float->float) =
match Bisection.TryFindRoot(tobcl f, lowerBound, upperBound, accuracy, maxIterations) with
| true, root -> Some root
| false, _ -> None
let brent maxIterations accuracy lowerBound upperBound (f:double->double) =
let brent maxIterations accuracy lowerBound upperBound (f:float->float) =
match Brent.TryFindRoot(tobcl f, lowerBound, upperBound, accuracy, maxIterations) with
| true, root -> Some root
| false, _ -> None
let newtonRaphson maxIterations accuracy lowerBound upperBound (f:double->double) (df:double->double) =
let newtonRaphson maxIterations accuracy lowerBound upperBound (f:float->float) (df:float->float) =
match NewtonRaphson.TryFindRoot(tobcl f, tobcl df, 0.5 * (lowerBound + upperBound), lowerBound, upperBound, accuracy, maxIterations) with
| true, root -> Some root
| false, _ -> None
let newtonRaphsonGuess maxIterations accuracy guess (f:double->double) (df:double->double) =
let newtonRaphsonGuess maxIterations accuracy guess (f:float->float) (df:float->float) =
match NewtonRaphson.TryFindRoot(tobcl f, tobcl df, guess, Double.MinValue, Double.MaxValue, accuracy, maxIterations) with
| true, root -> Some root
| false, _ -> None
let newtonRaphsonRobust maxIterations subdivision accuracy lowerBound upperBound (f:double->double) (df:double->double) =
let newtonRaphsonRobust maxIterations subdivision accuracy lowerBound upperBound (f:float->float) (df:float->float) =
match RobustNewtonRaphson.TryFindRoot(tobcl f, tobcl df, lowerBound, upperBound, accuracy, maxIterations, subdivision) with
| true, root -> Some root
| false, _ -> None
let broyden maxIterations accuracy guess (f:double[]->double[]) =
let broyden maxIterations accuracy guess (f:float[]->float[]) =
match Broyden.TryFindRoot(tobcl f, guess, accuracy, maxIterations) with
| true, root -> Some root
| false, _ -> None
// simple usage
let ofFunction lowerBound upperBound (f:double->double) =
let ofFunction lowerBound upperBound (f:float->float) =
brent 100 1e-8 lowerBound upperBound f
|>| fun () -> bisection 100 1e-8 lowerBound upperBound f
let ofFunctionDerivative lowerBound upperBound (f:double->double) (df:double->double) =
let ofFunctionDerivative lowerBound upperBound (f:float->float) (df:float->float) =
newtonRaphsonRobust 100 20 1e-8 lowerBound upperBound f df
|>| fun () -> bisection 100 1e-8 lowerBound upperBound f

4
src/FSharp/Random.fs

@ -39,11 +39,11 @@ module Random =
/// Default sampling, efficient but without custom seed (uses robust seeds internally)
let inline doubles (length:int) = SystemRandomSource.FastDoubles(length)
let inline doubleSeq () = SystemRandomSource.DoubleSequence()
let inline doubleFill (values:double[]) = SystemRandomSource.FastDoubles(values)
let inline doubleFill (values:float[]) = SystemRandomSource.FastDoubles(values)
let inline doublesSeed (seed:int) (length:int) = SystemRandomSource.Doubles(length, seed)
let inline doubleSeqSeed (seed:int) = SystemRandomSource.DoubleSequence(seed)
let inline doubleFillSeed (seed:int) (values:double[]) = SystemRandomSource.Doubles(values, seed)
let inline doubleFillSeed (seed:int) (values:float[]) = SystemRandomSource.Doubles(values, seed)
/// Creates a default .Net system pRNG with a robust seed
let systemShared = shared

Loading…
Cancel
Save