From d8ba6745fab32e3a419a20f1c8f8a54e0074af72 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Tue, 6 Jan 2015 21:17:46 +0100 Subject: [PATCH] LA: more convenient F# functions to append and stack matrices #277 --- src/FSharp/LinearAlgebra.Matrix.fs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/FSharp/LinearAlgebra.Matrix.fs b/src/FSharp/LinearAlgebra.Matrix.fs index 1196e391..af4cdd20 100644 --- a/src/FSharp/LinearAlgebra.Matrix.fs +++ b/src/FSharp/LinearAlgebra.Matrix.fs @@ -288,7 +288,7 @@ module Matrix = let inline inplaceAssign f (A: #Matrix<_>) = A.MapIndexedInplace((fun i j x -> f i j), Zeros.Include) - /// Fold all columns into one row vector. + /// Fold all columns into one row vector. let inline foldByCol f acc (A: #Matrix<'T>) = let v = Vector<'T>.Build.SameAs(A, A.ColumnCount) for k=0 to A.ColumnCount-1 do @@ -309,7 +309,6 @@ module Matrix = v :> _ Vector - /// In-place matrix addition. let inline addInPlace (v: #Matrix<_>) (w: #Matrix<_>) = v.Add(w, v) @@ -427,6 +426,9 @@ module DenseMatrix = /// Create a matrix from a 2D array of matrices. let inline ofMatrixArray2 array = Matrix<'T>.Build.DenseOfMatrixArray(array) + /// Create a matrix from a list of matrix lists forming a 2D grid. + let inline ofMatrixList2 (matrices: Matrix<'T> list list) = Matrix<'T>.Build.DenseOfMatrixArray(array2D matrices) + /// Create a matrix from a list of row vectors. let inline ofRows (rows: Vector<'T> list) = Matrix<'T>.Build.DenseOfRowVectors(Array.ofList rows) @@ -475,6 +477,12 @@ module DenseMatrix = /// Create a matrix with the array elements on the diagonal. let inline ofDiagArray2 (rows: int) (cols: int) (array: 'T array) = Matrix<'T>.Build.DenseOfDiagonalArray(rows, cols, array) + /// Create a matrix by appending a list of matrices horizontally, the first matrix on the left. + let inline append matrices = ofMatrixList2 [matrices] + + /// Create a matrix by stacking a list of matrices vertically, the first matrix on the top. + let inline stack matrices = matrices |> List.map (fun x -> [x]) |> ofMatrixList2 + /// A module which helps constructing generic sparse matrices. [] @@ -522,6 +530,9 @@ module SparseMatrix = /// Create a matrix from a 2D array of matrices. let inline ofMatrixArray2 array = Matrix<'T>.Build.SparseOfMatrixArray(array) + /// Create a matrix from a list of matrix lists forming a 2D grid. + let inline ofMatrixList2 (matrices: Matrix<'T> list list) = Matrix<'T>.Build.SparseOfMatrixArray(array2D matrices) + /// Create a matrix from a list of row vectors. let inline ofRows (rows: Vector<'T> list) = Matrix<'T>.Build.SparseOfRowVectors(Array.ofList rows) @@ -570,6 +581,12 @@ module SparseMatrix = /// Create a matrix with the array elements on the diagonal. let inline ofDiagArray2 (rows: int) (cols: int) (array: 'T array) = Matrix<'T>.Build.SparseOfDiagonalArray(rows, cols, array) + /// Create a matrix by appending a list of matrices horizontally, the first matrix on the left. + let inline append matrices = ofMatrixList2 [matrices] + + /// Create a matrix by stacking a list of matrices vertically, the first matrix on the top. + let inline stack matrices = matrices |> List.map (fun x -> [x]) |> ofMatrixList2 + /// A module which helps constructing generic diagonal matrices. []