From 29bc58398fe88cc87bd7dd76d158aaa668d87acd Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Wed, 29 Jan 2014 22:21:04 +0100 Subject: [PATCH] Generate: LogSpacedMap, F# func argument support in F# module --- src/FSharp/FSharp-Portable136.fsproj | 1 + src/FSharp/FSharp-Portable47.fsproj | 1 + src/FSharp/FSharp.fsproj | 1 + src/FSharp/Generate.fs | 64 ++++++++++++++++++++++++++++ src/Numerics/Generate.cs | 19 +++++++++ 5 files changed, 86 insertions(+) create mode 100644 src/FSharp/Generate.fs diff --git a/src/FSharp/FSharp-Portable136.fsproj b/src/FSharp/FSharp-Portable136.fsproj index 288ce7ed..cf48fcf4 100644 --- a/src/FSharp/FSharp-Portable136.fsproj +++ b/src/FSharp/FSharp-Portable136.fsproj @@ -49,6 +49,7 @@ + diff --git a/src/FSharp/FSharp-Portable47.fsproj b/src/FSharp/FSharp-Portable47.fsproj index 2ac81180..58a50c3e 100644 --- a/src/FSharp/FSharp-Portable47.fsproj +++ b/src/FSharp/FSharp-Portable47.fsproj @@ -49,6 +49,7 @@ + diff --git a/src/FSharp/FSharp.fsproj b/src/FSharp/FSharp.fsproj index 5b8c6352..a044527e 100644 --- a/src/FSharp/FSharp.fsproj +++ b/src/FSharp/FSharp.fsproj @@ -62,6 +62,7 @@ + diff --git a/src/FSharp/Generate.fs b/src/FSharp/Generate.fs new file mode 100644 index 00000000..f10ea30c --- /dev/null +++ b/src/FSharp/Generate.fs @@ -0,0 +1,64 @@ +// +// 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-2014 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 + +[] +module Generate = + + let inline private tobcl (f:'a->'b) = Func<'a,'b>(f) + let inline private tobcl2 (f:'a->'b->'c) = Func<'a,'b,'c>(f) + + let inline map map points = Array.map map points + let inline mapSeq map points = Seq.map map points + + let inline map2 map pointsA pointsB = Array.map2 map pointsA pointsB + let inline map2Seq map pointsA pointsB = Seq.map2 map pointsA pointsB + + let inline linearSpacedMap length start stop map = Generate.LinearSpacedMap(length, start, stop, tobcl map) + let inline logSpacedMap length startExp stopExp map = Generate.LogSpacedMap(length, startExp, stopExp, tobcl map) + let inline linearRangeMap start step stop map = [| for x in start .. step .. stop -> map x |] + + let inline periodicMap length map samplingRate frequency amplitude phase delay = + Generate.PeriodicMap(length, tobcl map, samplingRate, frequency, amplitude, phase, delay) + let inline periodicMapSeq map samplingRate frequency amplitude phase delay = + Generate.PeriodicMapSequence(tobcl map, samplingRate, frequency, amplitude, phase, delay) + + let inline randomMap length distribution map = Generate.RandomMap(length, distribution, tobcl map) + let inline randomMapSeq distribution map = Generate.RandomMapSequence(distribution, tobcl map) + let inline randomMap2 length distribution map = Generate.RandomMap2(length, distribution, tobcl2 map) + let inline randomMap2Seq distribution map = Generate.RandomMap2Sequence(distribution, tobcl2 map) + + let inline randomUniformMap length map = Generate.RandomUniformMap(length, tobcl map) + let inline randomUniformMapSeq map = Generate.RandomUniformMapSequence(tobcl map) + let inline randomUniformMap2 length map = Generate.RandomUniformMap2(length, tobcl2 map) + let inline randomUniformMap2Seq map = Generate.RandomUniformMap2Sequence(tobcl2 map) diff --git a/src/Numerics/Generate.cs b/src/Numerics/Generate.cs index 5a720bd2..903f73b5 100644 --- a/src/Numerics/Generate.cs +++ b/src/Numerics/Generate.cs @@ -149,6 +149,25 @@ namespace MathNet.Numerics return data; } + /// + /// Generate samples by sampling a function at base 10 logarithmically spaced points between the specified decade exponents (inclusive). + /// + public static T[] LogSpacedMap(int length, double startExponent, double stopExponent, Func map) + { + if (length <= 0) return new T[0]; + if (length == 1) return new[] { map(Math.Pow(10, stopExponent)) }; + + double step = (stopExponent - startExponent)/(length - 1); + + var data = new T[length]; + for (int i = 0; i < data.Length; i++) + { + data[i] = map(Math.Pow(10, startExponent + i*step)); + } + data[data.Length - 1] = map(Math.Pow(10, stopExponent)); + return data; + } + /// /// Generate a linearly spaced sample vector within the inclusive interval (start, stop) and step 1. /// Equivalent to MATLAB colon operator (:).