From dc0523ce0c3a9be81ada0d2b404edc8d1792cb01 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Mon, 16 Jun 2014 00:21:10 +0200 Subject: [PATCH] LA: F# modules add DiagonalMatrix module to create diagonal matrices --- src/FSharp/LinearAlgebra.Matrix.fs | 50 ++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/FSharp/LinearAlgebra.Matrix.fs b/src/FSharp/LinearAlgebra.Matrix.fs index 0f2e442c..226e3050 100644 --- a/src/FSharp/LinearAlgebra.Matrix.fs +++ b/src/FSharp/LinearAlgebra.Matrix.fs @@ -555,6 +555,56 @@ module SparseMatrix = let inline ofDiagArray2 (rows: int) (cols: int) (array: 'T array) = Matrix<'T>.Build.SparseOfDiagonalArray(rows, cols, array) +/// A module which helps constructing generic diagonal matrices. +[] +module DiagonalMatrix = + + /// Create a matrix that directly binds to a storage object. + let inline ofStorage (storage:Storage.DiagonalMatrixStorage<_>) = Matrix<'T>.Build.Diagonal(storage) + + /// Create a square matrix that directly binds to a raw storage array that represents the diagonal, without copying. + let inline raw (diagonal: 'T[]) = Matrix<'T>.Build.Diagonal(diagonal) + + /// Create a matrix that directly binds to a raw storage array that represents the diagonal, without copying. + let inline raw2 (rows: int) (cols: int) (diagonal: 'T[]) = Matrix<'T>.Build.Diagonal(rows, cols, diagonal) + + /// Create an all-zero matrix with the given dimension. + let inline zero<'T when 'T:struct and 'T :> ValueType and 'T: (new: unit ->'T) and 'T :> IEquatable<'T> and 'T :> IFormattable> + (rows: int) (cols: int) = Matrix<'T>.Build.Diagonal(rows, cols) + + /// Create a square matrix with the given order and set all diagonal values to x. + let inline create (order: int) (x: 'T) = Matrix<'T>.Build.Diagonal(order, order, x) + + /// Create a matrix with the given dimension and set all diagonal values to x. + let inline create2 (rows: int) (cols: int) (x: 'T) = Matrix<'T>.Build.Diagonal(rows, cols, x) + + /// Create an identity matrix with the given dimension. + let inline identity<'T when 'T:struct and 'T :> ValueType and 'T: (new: unit ->'T) and 'T :> IEquatable<'T> and 'T :> IFormattable> + (order: int) = Matrix<'T>.Build.DiagonalIdentity(order) + + /// Create an identity matrix with the given dimension. + let inline identity2<'T when 'T:struct and 'T :> ValueType and 'T: (new: unit ->'T) and 'T :> IEquatable<'T> and 'T :> IFormattable> + (rows: int) (cols: int) = Matrix<'T>.Build.DiagonalIdentity(rows, cols) + + /// Initialize a square matrix by calling a construction function for every element. + let inline init (order: int) (f: int -> 'T) = Matrix<'T>.Build.Diagonal(order, order, fun k -> f k) + + /// Initialize a matrix by calling a construction function for every element. + let inline init2 (rows: int) (cols: int) (f: int -> 'T) = Matrix<'T>.Build.Diagonal(rows, cols, fun k -> f k) + + /// Create a square matrix with the vector elements on the diagonal. + let inline ofDiag (v: Vector<'T>) = Matrix<'T>.Build.DiagonalOfDiagonalVector(v) + + /// Create a matrix with the vector elements on the diagonal. + let inline ofDiag2 (rows: int) (cols: int) (v: Vector<'T>) = Matrix<'T>.Build.DiagonalOfDiagonalVector(rows, cols, v) + + /// Create a square matrix with the array elements on the diagonal. + let inline ofDiagArray (array: 'T array) = Matrix<'T>.Build.DiagonalOfDiagonalArray(array) + + /// Create a matrix with the array elements on the diagonal. + let inline ofDiagArray2 (rows: int) (cols: int) (array: 'T array) = Matrix<'T>.Build.DiagonalOfDiagonalArray(rows, cols, array) + + /// Module that contains implementation of useful F#-specific extension members for generic matrices [] module MatrixExtensions =