From acce19675e24b70d133fd7ec306410a353724c3e Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sat, 13 Apr 2013 14:02:54 +0200 Subject: [PATCH] Rework F# Samples --- build/NuGet/nuget.proj | 4 +- src/FSharpExamples/Apply.fsx | 84 ++++++++----------- src/FSharpExamples/DenseVector.fsx | 4 +- src/FSharpExamples/Histogram.fsx | 8 +- src/FSharpExamples/MCMC.fsx | 11 +-- src/FSharpExamples/RandomAndDistributions.fsx | 6 +- 6 files changed, 49 insertions(+), 68 deletions(-) diff --git a/build/NuGet/nuget.proj b/build/NuGet/nuget.proj index 94748fd5..a5222d60 100644 --- a/build/NuGet/nuget.proj +++ b/build/NuGet/nuget.proj @@ -117,11 +117,11 @@ ReplacementText="namespace %24rootnamespace%24.Samples.MathNet.Numerics"/> diff --git a/src/FSharpExamples/Apply.fsx b/src/FSharpExamples/Apply.fsx index ee375bd9..4dd17f8f 100644 --- a/src/FSharpExamples/Apply.fsx +++ b/src/FSharpExamples/Apply.fsx @@ -28,71 +28,59 @@ // OTHER DEALINGS IN THE SOFTWARE. // -#r "../../out/debug/Net40/MathNet.Numerics.dll" -#r "../../out/debug/Net40/MathNet.Numerics.FSharp.dll" +#r "../../out/lib/Net40/MathNet.Numerics.dll" +#r "../../out/lib/Net40/MathNet.Numerics.FSharp.dll" open System.Numerics open MathNet.Numerics +open MathNet.Numerics.Distributions +open MathNet.Numerics.Random open MathNet.Numerics.LinearAlgebra.Double open MathNet.Numerics.LinearAlgebra.Generic -/// Flag to specify wether we want pretty printing or tab separated output. -let prettyPrint = false /// The size of the vector we want to map things for. let N = 1000000 + /// The number of times we repeat a call. let T = 10 + /// The list of all functions we want to test. -let FunctionList : (string * (float -> float) * (float -> float)) [] = - [| ("Cosine", cos, System.Math.Cos); - ("Sine", sin, System.Math.Sin); - ("Tangent", tan, System.Math.Tan); - ("Inverse Cosine", acos, System.Math.Acos); - ("Inverse Sine", asin, System.Math.Asin); - ("Inverse Tangent", atan, System.Math.Atan); - ("Hyperbolic Cosine", cosh, System.Math.Cosh); - ("Hyperbolic Sine", sinh, System.Math.Sinh); - ("Hyperbolic Tangent", tanh, System.Math.Tanh); - ("Abs", abs, System.Math.Abs); - ("Exp", exp, System.Math.Exp); - ("Log", log, System.Math.Log); - ("Sqrt", sqrt, System.Math.Sqrt); - ("Error Function", SpecialFunctions.Erf, SpecialFunctions.Erf); - ("Error Function Complement", SpecialFunctions.Erfc, SpecialFunctions.Erfc); - ("Inverse Error Function", SpecialFunctions.ErfInv, SpecialFunctions.ErfInv); - ("Inverse Error Function Complement", SpecialFunctions.ErfcInv, SpecialFunctions.ErfcInv) |] +let FunctionList : (string * (float -> float)) [] = + [| ("Cosine", cos); + ("Sine", sin); + ("Tangent", tan); + ("Inverse Cosine", acos); + ("Inverse Sine", asin); + ("Inverse Tangent", atan); + ("Hyperbolic Cosine", cosh); + ("Hyperbolic Sine", sinh); + ("Hyperbolic Tangent", tanh); + ("Abs", abs); + ("Exp", exp); + ("Log", log); + ("Sqrt", sqrt); + ("Error Function", SpecialFunctions.Erf); + ("Error Function Complement", SpecialFunctions.Erfc); + ("Inverse Error Function", SpecialFunctions.ErfInv); + ("Inverse Error Function Complement", SpecialFunctions.ErfcInv) |] /// A vector with random entries. let w = - let rnd = new Random.MersenneTwister() - (new DenseVector(Array.init N (fun _ -> rnd.NextDouble() * 10.0))) :> Vector + let dist = Normal(1.0, 10.0) |> withRandom (Random.mersenneTwister ()) + DenseVector.randomCreate N dist /// A stopwatch to time the execution. -let sw = new System.Diagnostics.Stopwatch() +let sw = System.Diagnostics.Stopwatch() + +printfn "%d-dimensional vector for %d iterations:" N T -for (name, fs, dotnet) in FunctionList do - if prettyPrint then printfn "Running %s on an %d dimensional vector for %d iterations:" name N T - else printf "%s" name +for (name, f) in FunctionList do + + let v = w.Clone() - /// Perform the standard F# map function. - do - let v = w.Clone() - sw.Start() - for t in 1 .. T do Vector.mapInPlace fs v - sw.Stop() - if prettyPrint then printfn "\tVector.map (F#): %d milliseconds." sw.ElapsedMilliseconds - else printf "\t%d" sw.ElapsedMilliseconds - sw.Reset() - (* - /// Perform the Apply.Map function. - do - let v = w.Clone() - sw.Start() - for t in 1 .. T do v.Map(fun x -> dotnet x) - sw.Stop() - if prettyPrint then printfn "\tApply.Map (MKL): %d milliseconds." sw.ElapsedMilliseconds - else printf "\t%d" sw.ElapsedMilliseconds - sw.Reset()*) + sw.Restart() + for t in 1 .. T do Vector.mapInPlace f v + sw.Stop() - printfn "" + printfn "%s:\t\t%d ms" name sw.ElapsedMilliseconds diff --git a/src/FSharpExamples/DenseVector.fsx b/src/FSharpExamples/DenseVector.fsx index d7fffdcb..8d209632 100644 --- a/src/FSharpExamples/DenseVector.fsx +++ b/src/FSharpExamples/DenseVector.fsx @@ -28,8 +28,8 @@ // OTHER DEALINGS IN THE SOFTWARE. // -#r "../../out/debug/Net40/MathNet.Numerics.dll" -#r "../../out/debug/Net40/MathNet.Numerics.FSharp.dll" +#r "../../out/lib/Net40/MathNet.Numerics.dll" +#r "../../out/lib/Net40/MathNet.Numerics.FSharp.dll" open MathNet.Numerics.LinearAlgebra open MathNet.Numerics.LinearAlgebra.Double diff --git a/src/FSharpExamples/Histogram.fsx b/src/FSharpExamples/Histogram.fsx index b28770df..b1dca295 100644 --- a/src/FSharpExamples/Histogram.fsx +++ b/src/FSharpExamples/Histogram.fsx @@ -28,18 +28,18 @@ // OTHER DEALINGS IN THE SOFTWARE. // -#r "../../out/debug/Net40/MathNet.Numerics.dll" -#r "../../out/debug/Net40/MathNet.Numerics.FSharp.dll" +#r "../../out/lib/Net40/MathNet.Numerics.dll" +#r "../../out/lib/Net40/MathNet.Numerics.FSharp.dll" open MathNet.Numerics.Statistics /// The number of buckets to use in our histogram. -let B = 9 +let B = 4 /// Create a small dataset. let data = [| 0.5; 1.5; 2.5; 3.5; 4.5; 5.5; 6.5; 7.5; 8.5; 9.5 |] -/// A histogram with 9 buckets for this dataset. +/// A histogram with 4 buckets for this dataset. let hist = new Histogram(data, B) // Print some histogram information. diff --git a/src/FSharpExamples/MCMC.fsx b/src/FSharpExamples/MCMC.fsx index 3801d179..669de302 100644 --- a/src/FSharpExamples/MCMC.fsx +++ b/src/FSharpExamples/MCMC.fsx @@ -28,8 +28,8 @@ // OTHER DEALINGS IN THE SOFTWARE. // -#r "../../out/debug/Net40/MathNet.Numerics.dll" -#r "../../out/debug/Net40/MathNet.Numerics.FSharp.dll" +#r "../../out/lib/Net40/MathNet.Numerics.dll" +#r "../../out/lib/Net40/MathNet.Numerics.FSharp.dll" open MathNet.Numerics.Random open MathNet.Numerics.Statistics @@ -196,10 +196,3 @@ do printfn "\tEmpirical Mean = %f (should be %f)" (Statistics.Mean(arr)) normal.Mean printfn "\tEmpirical StdDev = %f (should be %f)" (Statistics.StandardDeviation(arr)) normal.StdDev printfn "" - - - - - - -System.Console.ReadLine() |> ignore diff --git a/src/FSharpExamples/RandomAndDistributions.fsx b/src/FSharpExamples/RandomAndDistributions.fsx index b1399055..23bcac37 100644 --- a/src/FSharpExamples/RandomAndDistributions.fsx +++ b/src/FSharpExamples/RandomAndDistributions.fsx @@ -28,8 +28,8 @@ // OTHER DEALINGS IN THE SOFTWARE. // -#r "../../out/debug/Net40/MathNet.Numerics.dll" -#r "../../out/debug/Net40/MathNet.Numerics.FSharp.dll" +#r "../../out/lib/Net40/MathNet.Numerics.dll" +#r "../../out/lib/Net40/MathNet.Numerics.FSharp.dll" open MathNet.Numerics.Random open MathNet.Numerics.Distributions @@ -65,7 +65,7 @@ let exponential = new Exponential(2.4) let gamma = new Gamma(2.0, 1.5) |> withCryptoRandom let cauchy = new Cauchy() |> withRandom (Random.mrg32k3aWith 10 false) let poisson = new Poisson(3.0) -let geometric = new Geometric(1.2) |> withSystemRandom +let geometric = new Geometric(0.8) |> withSystemRandom // generate some random samples from these distributions let continuous = [