diff --git a/src/FSharp/LinearAlgebra.Double.Matrix.fs b/src/FSharp/LinearAlgebra.Double.Matrix.fs index 66c0e8b6..b9b4cff4 100644 --- a/src/FSharp/LinearAlgebra.Double.Matrix.fs +++ b/src/FSharp/LinearAlgebra.Double.Matrix.fs @@ -246,14 +246,23 @@ module Matrix = [] 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) = 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) = 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) = 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) = DenseMatrix.OfIndexed(n, m, fs) /// Create a matrix with the given entries. let inline initDense (n: int) (m: int) (es: #seq) = @@ -314,14 +326,14 @@ module DenseMatrix = [] 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) = 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) = 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) = @@ -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) = 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) = 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) diff --git a/src/FSharp/LinearAlgebra.Double.Vector.fs b/src/FSharp/LinearAlgebra.Double.Vector.fs index 211c709f..96f339ac 100644 --- a/src/FSharp/LinearAlgebra.Double.Vector.fs +++ b/src/FSharp/LinearAlgebra.Double.Vector.fs @@ -186,20 +186,32 @@ module Vector = [] 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) = DenseVector.OfIndexedEnumerable(dim, Seq.ofList fl) - /// Create a vector from a sequences. let inline ofSeq (fs: #seq) = 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) = 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) = DenseVector.OfIndexedEnumerable(dim, fs) + let inline ofSeqi (n: int) (fs: #seq) = 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 = [] 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. [] - let inline ofList (dim: int) (fl: list) = 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) = SparseVector.OfIndexedEnumerable(dim, Seq.ofList fl) + let inline ofList (n: int) (fl: list) = SparseVector.OfIndexedEnumerable(n, Seq.ofList fl) /// Create a sparse vector with a given dimension from a sequence of index, value pairs. [] - let inline ofSeq (dim: int) (fs: #seq) = SparseVector.OfIndexedEnumerable(dim, fs) + let inline ofSeq (n: int) (fs: #seq) = 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) = 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) = SparseVector.OfIndexedEnumerable(dim, fs) + let inline ofSeqi (n: int) (fs: #seq) = SparseVector.OfIndexedEnumerable(n, fs)