From e40928256ef3a6618c717495c2cfbcd38ca11fca Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sun, 7 Apr 2013 23:26:24 +0200 Subject: [PATCH] FSharp: leverage functional matrix/vector init of core --- src/FSharp/LinearAlgebra.Double.Matrix.fs | 10 ++++------ src/FSharp/LinearAlgebra.Double.Vector.fs | 9 ++++----- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/FSharp/LinearAlgebra.Double.Matrix.fs b/src/FSharp/LinearAlgebra.Double.Matrix.fs index f7b1c19f..fdc82bf0 100644 --- a/src/FSharp/LinearAlgebra.Double.Matrix.fs +++ b/src/FSharp/LinearAlgebra.Double.Matrix.fs @@ -247,12 +247,7 @@ module Matrix = module DenseMatrix = /// Initialize a matrix by calling a construction function for every element. - let inline init (n: int) (m: int) f = - let A = new DenseMatrix(n,m) - for i=0 to n-1 do - for j=0 to m-1 do - A.At(i, j, f i j) - A + let inline init (n: int) (m: int) (f: int -> int -> float) = DenseMatrix.Create(n, m, fun n m -> f n m) /// Create a matrix from a list of float lists. Every list in the master list specifies a row. let inline ofList (fll: float list list) = @@ -314,6 +309,9 @@ module DenseMatrix = [] module SparseMatrix = + /// Initialize a matrix by calling a construction function for every element. + let inline init (n: int) (m: int) (f: int -> int -> float) = SparseMatrix.Create(n, m, fun n m -> f n m) + /// Create a matrix from a list of float lists. Every list in the master list specifies a row. let inline ofList (rows: int) (cols: int) (fll: list) = let A = new SparseMatrix(rows, cols) diff --git a/src/FSharp/LinearAlgebra.Double.Vector.fs b/src/FSharp/LinearAlgebra.Double.Vector.fs index 2b0b1661..127743b7 100644 --- a/src/FSharp/LinearAlgebra.Double.Vector.fs +++ b/src/FSharp/LinearAlgebra.Double.Vector.fs @@ -187,11 +187,7 @@ module Vector = module DenseVector = /// Initialize a vector by calling a construction function for every element. - let inline init (n: int) f = - let v = new DenseVector(n) - for i=0 to n-1 do - v.At(i, f i) - v + let inline init (n: int) (f: int -> float) = DenseVector.Create(n, fun i -> f i) /// Create a vector from a float list. let inline ofList (fl: float list) = DenseVector(Array.ofList fl) @@ -215,6 +211,9 @@ module DenseVector = [] module SparseVector = + /// Initialize a vector by calling a construction function for every element. + let inline init (n: int) (f: int -> float) = SparseVector.Create(n, fun i -> f i) + /// Create a sparse vector with a given dimension from a list of entry, value pairs. let inline ofList (dim: int) (fl: list) = let v = new SparseVector(dim)