Browse Source

FSharp: matrix/vector zeroCreate, randomCreate, create, raw, ofArray2

pull/112/head
Christoph Ruegg 13 years ago
parent
commit
3cb3eccf2b
  1. 44
      src/FSharp/LinearAlgebra.Double.Matrix.fs
  2. 35
      src/FSharp/LinearAlgebra.Double.Vector.fs

44
src/FSharp/LinearAlgebra.Double.Matrix.fs

@ -246,14 +246,23 @@ module Matrix =
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module DenseMatrix =
/// Create a matrix that directly binds to a raw storage array in column-major (column by column) format, without copying.
let inline raw (n: int) (m: int) (columnMajor: float[]) = DenseMatrix(n, m, columnMajor)
/// Create an all-zero matrix with the given dimension.
let inline zeroCreate (n: int) (m: int) = DenseMatrix(n, m)
/// Create a random matrix with the given dimension and value distribution.
let inline randomCreate (n: int) (m: int) dist = DenseMatrix.CreateRandom(n, m, dist)
/// Create a matrix with the given dimension and set all values to x.
let inline create (n: int) (m: int) x = DenseMatrix.Create(n, m, fun i j -> x)
/// Initialize a matrix by calling a construction function for every element.
let inline init (n: int) (m: int) (f: int -> int -> float) = DenseMatrix.Create(n, m, fun n m -> f n m)
let inline init (n: int) (m: int) (f: int -> int -> float) = DenseMatrix.Create(n, m, fun i j -> f i j)
/// Create a matrix with a given dimension from an indexed list of row, column, value tuples.
let inline ofListi (n: int) (m: int) (fl: list<int * int * float>) = DenseMatrix.OfIndexed(n, m, Seq.ofList fl)
/// Create a matrix with a given dimension from an indexed sequences of row, column, value tuples.
let inline ofSeqi (n: int) (m: int) (fs: #seq<int * int * float>) = DenseMatrix.OfIndexed(n, m, fs)
/// Create a matrix from a 2D array of floating point numbers.
let inline ofArray2 array = DenseMatrix.OfArray(array)
/// 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) =
@ -275,8 +284,11 @@ module DenseMatrix =
Seq.iteri (fun j f -> A.At(i,j,f)) fs)
A
/// Create a matrix from a 2D array of floating point numbers.
let inline ofArray2 (arr: float[,]) = DenseMatrix.OfArray(arr)
/// Create a matrix with a given dimension from an indexed list of row, column, value tuples.
let inline ofListi (n: int) (m: int) (fl: list<int * int * float>) = DenseMatrix.OfIndexed(n, m, Seq.ofList fl)
/// Create a matrix with a given dimension from an indexed sequences of row, column, value tuples.
let inline ofSeqi (n: int) (m: int) (fs: #seq<int * int * float>) = DenseMatrix.OfIndexed(n, m, fs)
/// Create a matrix with the given entries.
let inline initDense (n: int) (m: int) (es: #seq<int * int * float>) =
@ -314,14 +326,14 @@ module DenseMatrix =
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module SparseMatrix =
/// Create an all-zero matrix with the given dimension.
let inline zeroCreate (n: int) (m: int) = SparseMatrix(n, m)
/// 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 with a given dimension from an indexed list of row, column, value tuples.
let inline ofListi (n: int) (m: int) (fl: list<int * int * float>) = SparseMatrix.OfIndexed(n, m, Seq.ofList fl)
/// Create a matrix with a given dimension from an indexed sequences of row, column, value tuples.
let inline ofSeqi (n: int) (m: int) (fs: #seq<int * int * float>) = SparseMatrix.OfIndexed(n, m, fs)
/// Create a matrix from a 2D array of floating point numbers.
let inline ofArray2 array = SparseMatrix.OfArray(array)
/// 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<int * int * float>) =
@ -335,6 +347,12 @@ module SparseMatrix =
fss |> Seq.iter (fun (i, j, x) -> A.At(i,j,x))
A
/// Create a matrix with a given dimension from an indexed list of row, column, value tuples.
let inline ofListi (n: int) (m: int) (fl: list<int * int * float>) = SparseMatrix.OfIndexed(n, m, Seq.ofList fl)
/// Create a matrix with a given dimension from an indexed sequences of row, column, value tuples.
let inline ofSeqi (n: int) (m: int) (fs: #seq<int * int * float>) = SparseMatrix.OfIndexed(n, m, fs)
/// Create a square matrix with constant diagonal entries.
let inline constDiag (n: int) (f: float) =
let A = new SparseMatrix(n,n)

35
src/FSharp/LinearAlgebra.Double.Vector.fs

@ -186,20 +186,32 @@ module Vector =
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module DenseVector =
/// Create a vector that directly binds to a raw storage array, without copying.
let inline raw (raw: float[]) = DenseVector(raw)
/// Initialize an all-zero vector with the given dimension.
let inline zeroCreate (n: int) = DenseVector(n)
/// Initialize a random vector with the given dimension and distribution.
let inline randomCreate (n: int) dist = DenseVector.CreateRandom(n, dist)
/// Initialize an all-zero vector with the given dimension.
let inline create (n: int) x = DenseVector.Create(n, fun i -> x)
/// Initialize a vector by calling a construction function for every element.
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)
/// Create a vector with a given dimension from an indexed list of index, value pairs.
let inline ofListi (dim: int) (fl: list<int * float>) = DenseVector.OfIndexedEnumerable(dim, Seq.ofList fl)
/// Create a vector from a sequences.
let inline ofSeq (fs: #seq<float>) = DenseVector.OfEnumerable(fs)
/// Create a vector with a given dimension from an indexed list of index, value pairs.
let inline ofListi (n: int) (fl: list<int * float>) = DenseVector.OfIndexedEnumerable(n, Seq.ofList fl)
/// Create a vector with a given dimension from an indexed sequences of index, value pairs.
let inline ofSeqi (dim: int) (fs: #seq<int * float>) = DenseVector.OfIndexedEnumerable(dim, fs)
let inline ofSeqi (n: int) (fs: #seq<int * float>) = DenseVector.OfIndexedEnumerable(n, fs)
/// Create a vector with evenly spaced entries: e.g. rangef -1.0 0.5 1.0 = [-1.0 -0.5 0.0 0.5 1.0]
let inline rangef (start: float) (step: float) (stop: float) =
@ -217,19 +229,22 @@ module DenseVector =
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module SparseVector =
/// Initialize an all-zero vector with the given dimension.
let inline zeroCreate (n: int) = SparseVector(n)
/// 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 index, value pairs.
[<System.ObsoleteAttribute("Use ofListi instead. Will be changed to expect a non-indexed seq in a future version.")>]
let inline ofList (dim: int) (fl: list<int * float>) = SparseVector.OfIndexedEnumerable(dim, Seq.ofList fl)
/// Create a sparse vector with a given dimension from an indexed list of index, value pairs.
let inline ofListi (dim: int) (fl: list<int * float>) = SparseVector.OfIndexedEnumerable(dim, Seq.ofList fl)
let inline ofList (n: int) (fl: list<int * float>) = SparseVector.OfIndexedEnumerable(n, Seq.ofList fl)
/// Create a sparse vector with a given dimension from a sequence of index, value pairs.
[<System.ObsoleteAttribute("Use ofSeqi instead. Will be changed to expect a non-indexed seq in a future version.")>]
let inline ofSeq (dim: int) (fs: #seq<int * float>) = SparseVector.OfIndexedEnumerable(dim, fs)
let inline ofSeq (n: int) (fs: #seq<int * float>) = SparseVector.OfIndexedEnumerable(n, fs)
/// Create a sparse vector with a given dimension from an indexed list of index, value pairs.
let inline ofListi (n: int) (fl: list<int * float>) = SparseVector.OfIndexedEnumerable(n, Seq.ofList fl)
/// Create a sparse vector with a given dimension from an indexed sequence of index, value pairs.
let inline ofSeqi (dim: int) (fs: #seq<int * float>) = SparseVector.OfIndexedEnumerable(dim, fs)
let inline ofSeqi (n: int) (fs: #seq<int * float>) = SparseVector.OfIndexedEnumerable(n, fs)

Loading…
Cancel
Save