Browse Source

Examples: convert F# scripts to normal F# source files

pull/555/merge
Christoph Ruegg 8 years ago
parent
commit
fd2e040bc6
  1. 15
      examples/examples-csharp/examples-csharp.csproj
  2. 55
      examples/examples-fsharp/Apply.fs
  3. 86
      examples/examples-fsharp/Apply.fsx
  4. 17
      examples/examples-fsharp/Histogram.fs
  5. 48
      examples/examples-fsharp/Histogram.fsx
  6. 33
      examples/examples-fsharp/LinearRegression.fs
  7. 33
      examples/examples-fsharp/MCMC.fs
  8. 33
      examples/examples-fsharp/Matrices.fs
  9. 47
      examples/examples-fsharp/RandomAndDistributions.fs
  10. 33
      examples/examples-fsharp/Vectors.fs
  11. 27
      examples/examples-fsharp/examples-fsharp.fsproj

15
examples/examples-csharp/examples-csharp.csproj

@ -1,22 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net40;net45;net46;netstandard1.6;netstandard2.0</TargetFrameworks>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net40'">
<PackageReference Include="MathNet.Numerics" Version="4.0.0-beta05" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net45'">
<PackageReference Include="MathNet.Numerics" Version="4.0.0-beta05" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net46'">
<PackageReference Include="MathNet.Numerics" Version="4.0.0-beta05" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<ItemGroup>
<PackageReference Include="MathNet.Numerics" Version="4.0.0-beta05" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.6'">
<PackageReference Include="MathNet.Numerics" Version="4.0.0-beta05" />
<PackageReference Include="NETStandard.Library" Version="2.0.1" />
</ItemGroup>
</Project>

55
examples/examples-fsharp/Apply.fs

@ -0,0 +1,55 @@
module Apply
open System.Numerics
open MathNet.Numerics
open MathNet.Numerics.Distributions
open MathNet.Numerics.Random
open MathNet.Numerics.LinearAlgebra
open MathNet.Numerics.LinearAlgebra.Double
/// 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)) [] =
[| ("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 dist = Normal(1.0, 10.0, Random.mersenneTwister ())
DenseVector.random N dist
/// A stopwatch to time the execution.
let sw = System.Diagnostics.Stopwatch()
printfn "%d-dimensional vector for %d iterations:" N T
for (name, f) in FunctionList do
let v = w.Clone()
sw.Restart()
for t in 1 .. T do Vector.mapInPlace f v
sw.Stop()
printfn "%s:\t\t%d ms" name sw.ElapsedMilliseconds

86
examples/examples-fsharp/Apply.fsx

@ -1,86 +0,0 @@
// <copyright file="Apply.fsx" 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-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.
// </copyright>
#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
/// 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)) [] =
[| ("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 dist = Normal(1.0, 10.0) |> withRandom (Random.mersenneTwister ())
DenseVector.randomCreate N dist
/// A stopwatch to time the execution.
let sw = System.Diagnostics.Stopwatch()
printfn "%d-dimensional vector for %d iterations:" N T
for (name, f) in FunctionList do
let v = w.Clone()
sw.Restart()
for t in 1 .. T do Vector.mapInPlace f v
sw.Stop()
printfn "%s:\t\t%d ms" name sw.ElapsedMilliseconds

17
examples/examples-fsharp/Histogram.fs

@ -0,0 +1,17 @@
module Histogram
open MathNet.Numerics.Statistics
/// The number of buckets to use in our histogram.
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 4 buckets for this dataset.
let hist = new Histogram(data, B)
// Print some histogram information.
printfn "Histogram.ToString(): %O" hist
for i in 0 .. B-1 do
printfn "Bucket %d contains %f datapoints." i hist.[i].Count

48
examples/examples-fsharp/Histogram.fsx

@ -1,48 +0,0 @@
// <copyright file="Histogram.fsx" 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-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.
// </copyright>
#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 = 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 4 buckets for this dataset.
let hist = new Histogram(data, B)
// Print some histogram information.
printfn "Histogram.ToString(): %O" hist
for i in 0 .. B-1 do
printfn "Bucket %d contains %f datapoints." i hist.[i].Count

33
examples/examples-fsharp/LinearRegression.fsx → examples/examples-fsharp/LinearRegression.fs

@ -1,35 +1,4 @@
// <copyright file="LinearRegression.fsx" 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-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.
// </copyright>
#r "../../out/lib/Net40/MathNet.Numerics.dll"
#r "../../out/lib/Net40/MathNet.Numerics.FSharp.dll"
module LinearRegression
open System
open MathNet.Numerics

33
examples/examples-fsharp/MCMC.fsx → examples/examples-fsharp/MCMC.fs

@ -1,35 +1,4 @@
// <copyright file="MCMC.fsx" 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-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.
// </copyright>
#r "../../out/lib/Net40/MathNet.Numerics.dll"
#r "../../out/lib/Net40/MathNet.Numerics.FSharp.dll"
module MCMC
open MathNet.Numerics
open MathNet.Numerics.Random

33
examples/examples-fsharp/Matrices.fsx → examples/examples-fsharp/Matrices.fs

@ -1,35 +1,4 @@
// <copyright file="Matrices.fsx" 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-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.
// </copyright>
#r "../../out/lib/Net40/MathNet.Numerics.dll"
#r "../../out/lib/Net40/MathNet.Numerics.FSharp.dll"
module Matrices
open MathNet.Numerics.LinearAlgebra
open MathNet.Numerics.LinearAlgebra.Double

47
examples/examples-fsharp/RandomAndDistributions.fsx → examples/examples-fsharp/RandomAndDistributions.fs

@ -15,22 +15,9 @@ distribution, like a Gaussian or Poisson. You can do that with one of our probab
distribution classes, or in F# also using the `Sample` module. Once parametrized,
the distribution classes also provide a variety of other functionality around probability
distributions, like evaluating statistical distribution properties or functions.
Initialization
--------------
We need to reference Math.NET Numerics and open the namespaces for
random numbers and probability distributions:
using MathNet.Numerics.Random;
using MathNet.Numerics.Distributions;
Or in F#:
*)
// Only needed in scripts/interactive
#r "../../out/lib/Net40/MathNet.Numerics.dll"
#r "../../out/lib/Net40/MathNet.Numerics.FSharp.dll"
module RandomAndDistributions
open MathNet.Numerics.Random
open MathNet.Numerics.Distributions
@ -115,7 +102,7 @@ boolean argument at creation or by setting `Control.ThreadSafeRandomNumberGenera
let a = Random.system ()
let b = Random.systemSeed (RandomSeed.Guid())
let c = Random.crypto ()
//let c = Random.crypto ()
let d = Random.mersenneTwister ()
let e = Random.mersenneTwisterWith 1000 true (* thread-safe *)
let f = Random.xorshift ()
@ -146,7 +133,7 @@ as last argument. A few more examples, this time in F#:
// some probability distributions
let normal = Normal.WithMeanVariance(3.0, 1.5, g)
let exponential = Exponential(2.4)
let gamma = Gamma(2.0, 1.5, Random.crypto())
//let gamma = Gamma(2.0, 1.5, Random.crypto())
let cauchy = Cauchy(0.0, 1.0, Random.mrg32k3aWith 10 false)
let poisson = Poisson(3.0)
let geometric = Geometric(0.8, Random.system())
@ -154,8 +141,8 @@ let geometric = Geometric(0.8, Random.system())
// sample some random rumbers from these distributions
let continuous =
[ yield normal.Sample()
yield exponential.Sample()
yield! gamma.Samples() |> Seq.take 10 ]
yield exponential.Sample() ]
// yield! gamma.Samples() |> Seq.take 10 ]
let discrete =
[ poisson.Sample()
@ -165,7 +152,7 @@ let discrete =
// direct sampling (without creating a distribution object)
let u = Normal.Sample(Random.system(), 2.0, 4.0)
let v = Laplace.Samples(Random.mersenneTwister(), 1.0, 3.0) |> Seq.take 100 |> List.ofSeq
let w = Rayleigh.Sample(c, 1.5)
//let w = Rayleigh.Sample(c, 1.5)
let x = Hypergeometric.Sample(h, 100, 20, 5)
(**
@ -180,13 +167,13 @@ some of them are also available with the `Ln`-suffix.
*)
// distribution properties of the gamma we've configured above
let gammaStats =
( gamma.Mean,
gamma.Variance,
gamma.StdDev,
gamma.Entropy,
gamma.Skewness,
gamma.Mode )
//let gammaStats =
// ( gamma.Mean,
// gamma.Variance,
// gamma.StdDev,
// gamma.Entropy,
// gamma.Skewness,
// gamma.Mode )
// probability distribution functions of the normal we've configured above.
let nd = normal.Density(4.0) (* pdf *)
@ -220,8 +207,8 @@ or in C#:
Let's do some random walks, using distributions and random sources defined above (TODO: Graph):
*)
Seq.scan (+) 0.0 (normal.Samples()) |> Seq.take 10 |> Seq.toArray
Seq.scan (+) 0.0 (Sample.normalSeq 0.0 0.5 a) |> Seq.take 10 |> Seq.toArray
let a1 = Seq.scan (+) 0.0 (normal.Samples()) |> Seq.take 10 |> Seq.toArray
let a2 = Seq.scan (+) 0.0 (Sample.normalSeq 0.0 0.5 a) |> Seq.take 10 |> Seq.toArray
(**
Composing Distributions
@ -247,7 +234,7 @@ let s2f rng = Sample.map2 (*) (Sample.normal 2.0 1.5) (Sample.cauchy 2.0 0.5) rn
let s2s rng = Sample.mapSeq2 (*) (Sample.normalSeq 2.0 1.5) (Sample.cauchySeq 2.0 0.5) rng
// Taking some samples from the composed function
Seq.take 10 (s2s (Random.system())) |> Seq.toArray
let a3 = Seq.take 10 (s2s (Random.system())) |> Seq.toArray
// The random walk from above, but this time using the composition from above
Seq.scan (+) 0.0 (s1s a) |> Seq.take 10 |> Seq.toArray
let a4 = Seq.scan (+) 0.0 (s1s a) |> Seq.take 10 |> Seq.toArray

33
examples/examples-fsharp/Vectors.fsx → examples/examples-fsharp/Vectors.fs

@ -1,35 +1,4 @@
// <copyright file="Vectors.fsx" 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-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.
// </copyright>
#r "../../out/lib/Net40/MathNet.Numerics.dll"
#r "../../out/lib/Net40/MathNet.Numerics.FSharp.dll"
module Vectors
open MathNet.Numerics.LinearAlgebra
open MathNet.Numerics.LinearAlgebra.Double

27
examples/examples-fsharp/examples-fsharp.fsproj

@ -1,4 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net45;net46;netstandard1.6;netstandard2.0</TargetFrameworks>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
@ -7,17 +8,23 @@
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)'=='netstandard1.6'">
<DefineConstants>NETSTANDARD;NOSYSNUMERICS</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Compile Include="RandomAndDistributions.fs" />
<Compile Include="Histogram.fs" />
<Compile Include="MCMC.fs" />
<Compile Include="Vectors.fs" />
<Compile Include="Matrices.fs" />
<Compile Include="LinearRegression.fs" />
<Compile Include="Apply.fs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.6'">
<PackageReference Include="NETStandard.Library" Version="2.0.1" />
</ItemGroup>
<ItemGroup>
<None Include="RandomAndDistributions.fsx" />
<None Include="Histogram.fsx" />
<None Include="MCMC.fsx" />
<None Include="Vectors.fsx" />
<None Include="Matrices.fsx" />
<None Include="LinearRegression.fsx" />
<None Include="Apply.fsx" />
<PackageReference Update="FSharp.Core" Version="4.2.3" />
<PackageReference Include="MathNet.Numerics" Version="4.0.0-beta05" />
<PackageReference Include="MathNet.Numerics.FSharp" Version="4.0.0-beta05" />
</ItemGroup>
</Project>

Loading…
Cancel
Save