From bd6918d52eff5711320f135cd1557de13d964589 Mon Sep 17 00:00:00 2001 From: jvangael Date: Tue, 18 Aug 2009 15:57:39 +0800 Subject: [PATCH] Finished the initial F# interface for Double.DenseVector. Signed-off-by: jvangael --- src/FSharp/Vector.fs | 4 ++-- src/FSharpExamples/DenseVector.fs | 23 ++++++++++++++++++++--- src/FSharpUnitTests/Program.fs | 13 ++++++++++--- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/FSharp/Vector.fs b/src/FSharp/Vector.fs index c7e7934a..2e25d8cd 100644 --- a/src/FSharp/Vector.fs +++ b/src/FSharp/Vector.fs @@ -67,7 +67,7 @@ module Vector = /// let inline map f (v: #Vector) = let w = v.Clone() - inplace_mapi (fun _ x -> f x) w + mapInPlace (fun x -> f x) w w /// Applies a function to all elements of the vector. @@ -83,7 +83,7 @@ module Vector = /// Maps a vector to a new vector by applying a function to every element. let inline mapi (f: int -> float -> float) (v: #Vector) = let w = v.Clone() - inplace_mapi f w + mapiInPlace f w w /// Fold all entries of a vector. diff --git a/src/FSharpExamples/DenseVector.fs b/src/FSharpExamples/DenseVector.fs index 8fee1915..12fdbd9e 100644 --- a/src/FSharpExamples/DenseVector.fs +++ b/src/FSharpExamples/DenseVector.fs @@ -1,8 +1,25 @@ open MathNet.Numerics.FSharp open MathNet.Numerics.LinearAlgebra -/// Create a new 100 dimensional dense vector. +// Create a new 100 dimensional dense vector. let v = Double.DenseVector.init 100 (fun i -> float i / 100.0) -/// Another way to create a 100 dimensional dense vector is as follows. -let w = vector (List.init 100 (fun i -> float i ** 2.0)) \ No newline at end of file +// Another way to create a 100 dimensional dense vector is using the vector function. +let w = vector (List.init 100 (fun i -> float i ** 2.0)) + +// Vectors can also be constructed from sequences. +let t = Double.DenseVector.of_seq (seq { for i in 1 .. 100 do yield float i }) + +// We can now add two vectors together ... +let z = v + w + +// ... or scale them in the process. +let x = v + 3.0 * t + + + +// We can create a vector from an integer range (in this case, 5 and 10 inclusive) ... +let s = Double.DenseVector.range 5 10 + +// ... or we can create a vector from a double range with a particular step size. +let r = Double.DenseVector.rangef 0.0 0.1 10.0 \ No newline at end of file diff --git a/src/FSharpUnitTests/Program.fs b/src/FSharpUnitTests/Program.fs index a07b02bb..6f5a8b0f 100644 --- a/src/FSharpUnitTests/Program.fs +++ b/src/FSharpUnitTests/Program.fs @@ -8,7 +8,7 @@ let DenseVectorTests = /// A small uniform vector. let smallv = new Double.DenseVector(5, 0.3 ) - (*/// A large vector with increasingly large entries + /// A large vector with increasingly large entries let largev = new Double.DenseVector( Array.init 100 (fun i -> float i / 100.0) ) specs "DenseVector" [ @@ -16,5 +16,12 @@ let DenseVectorTests = (Double.DenseVector.init 100 (fun i -> float i / 100.0) |> should equal largev) spec "DenseVector.of_list" (Double.DenseVector.of_list [ for i in 0 .. 99 -> float i / 100.0 ] |> should equal largev) - ]*) - () \ No newline at end of file + spec "DenseVector.of_seq" + (Double.DenseVector.of_seq (seq { for i in 0 .. 99 -> float i / 100.0 }) |> should equal largev) + spec "DenseVector.rangef" + (Double.DenseVector.rangef 0.0 0.01 0.99 + |> should equal (new Double.DenseVector( [| for i in 0 .. 99 -> 0.01 * float i |] ) )) + spec "DenseVector.range" + (Double.DenseVector.range 0 99 + |> should equal (new Double.DenseVector( [| for i in 0 .. 99 -> float i |] ) )) + ] \ No newline at end of file