Browse Source

Rework F# Samples

pull/112/head
Christoph Ruegg 13 years ago
parent
commit
acce19675e
  1. 4
      build/NuGet/nuget.proj
  2. 84
      src/FSharpExamples/Apply.fsx
  3. 4
      src/FSharpExamples/DenseVector.fsx
  4. 8
      src/FSharpExamples/Histogram.fsx
  5. 11
      src/FSharpExamples/MCMC.fsx
  6. 6
      src/FSharpExamples/RandomAndDistributions.fsx

4
build/NuGet/nuget.proj

@ -117,11 +117,11 @@
ReplacementText="namespace %24rootnamespace%24.Samples.MathNet.Numerics"/>
<FileUpdate
Files="@(FSharpSamplePreprocessingFiles)"
Regex="(/out/(debug|release)/Net40/MathNet\.Numerics\.dll)"
Regex="(/out/(debug|lib)/Net40/MathNet\.Numerics\.dll)"
ReplacementText="/../packages/MathNet.Numerics.$(NumericsPackVersion)/lib/net40/MathNet.Numerics.dll"/>
<FileUpdate
Files="@(FSharpSamplePreprocessingFiles)"
Regex="(/out/(debug|release)/Net40/MathNet\.Numerics\.FSharp\.dll)"
Regex="(/out/(debug|lib)/Net40/MathNet\.Numerics\.FSharp\.dll)"
ReplacementText="/../packages/MathNet.Numerics.FSharp.$(FSharpPackVersion)/lib/net40/MathNet.Numerics.FSharp.dll"/>
</Target>

84
src/FSharpExamples/Apply.fsx

@ -28,71 +28,59 @@
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
#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<float>
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

4
src/FSharpExamples/DenseVector.fsx

@ -28,8 +28,8 @@
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
#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

8
src/FSharpExamples/Histogram.fsx

@ -28,18 +28,18 @@
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
#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.

11
src/FSharpExamples/MCMC.fsx

@ -28,8 +28,8 @@
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
#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

6
src/FSharpExamples/RandomAndDistributions.fsx

@ -28,8 +28,8 @@
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
#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 = [

Loading…
Cancel
Save