@ -36,6 +36,8 @@ open MathNet.Numerics.LinearAlgebra
[ < C o m p i l a t i o n R e p r e s e n t a t i o n ( C o m p i l a t i o n R e p r e s e n t a t i o n F l a g s . M o d u l e S u f f i x ) > ]
[ < C o m p i l a t i o n R e p r e s e n t a t i o n ( C o m p i l a t i o n R e p r e s e n t a t i o n F l a g s . M o d u l e S u f f i x ) > ]
module Matrix =
module Matrix =
// TODO: generalize or r e c o n s i d e r
/// Returns the sum of all elements of a m a t r i x .
/// Returns the sum of all elements of a m a t r i x .
let inline sum (A: #Matrix<float>) = A |> Matrix.foldnz (+) 0 .0
let inline sum (A: #Matrix<float>) = A |> Matrix.foldnz (+) 0 .0
@ -75,19 +77,28 @@ module DenseMatrix =
/// Create a matrix with the given dimension and set all values to x .
/// Create a matrix with the given dimension and set all values to x .
let inline create (rows: int) (cols: int) x = DenseMatrix.Create(rows, cols, fun i j -> x) :> _ M a t r i x
let inline create (rows: int) (cols: int) x = DenseMatrix.Create(rows, cols, fun i j -> x) :> _ M a t r i x
/// Create a matrix with the given dimension and set all diagonal values to x. All other values are z e r o .
let inline createDiag (rows: int) (cols: int) x =
let A = D e n s e M a t r i x ( r o w s , c o l s )
for i=0 to min (rows-1) (cols-1) do A . A t ( i , i , x )
A :> _ M a t r i x
/// Initialize a matrix by calling a construction function for every e l e m e n t .
/// Initialize a matrix by calling a construction function for every e l e m e n t .
let inline init (rows: int) (cols: int) (f: int -> int -> float) = DenseMatrix.Create(rows, cols, fun i j -> f i j) :> _ M a t r i x
let inline init (rows: int) (cols: int) (f: int -> int -> float) = DenseMatrix.Create(rows, cols, fun i j -> f i j) :> _ M a t r i x
/// Initialize a matrix by calling a construction function for every diagonal element. All other values are z e r o .
let inline initDiag (rows: int) (cols: int) (f: int -> float) =
let A = D e n s e M a t r i x ( r o w s , c o l s )
for i=0 to min (rows-1) (cols-1) do A.At(i,i,f i )
A :> _ M a t r i x
/// Create an identity matrix with the given d i m e n s i o n .
let inline identity (rows: int) (cols: int) = createDiag rows cols 1 .0
/// Create a matrix from a 2 D array of floating point n u m b e r s .
/// Create a matrix from a 2 D array of floating point n u m b e r s .
let inline ofArray2 array = DenseMatrix.OfArray(array) :> _ M a t r i x
let inline ofArray2 array = DenseMatrix.OfArray(array) :> _ M a t r i x
/// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a r o w .
/// If the dimensions are known, consider to use ofRowSeq instead to avoid multiple e n u m e r a t i o n .
let inline ofSeq (fss: #seq<#seq<float>>) =
let n = Seq.length f s s
let m = Seq.length (Seq.head f s s )
DenseMatrix.OfRowsCovariant(n, m, fss) :> _ M a t r i x
/// Create a matrix from a list of float lists. Every list in the master list specifies a r o w .
/// Create a matrix from a list of float lists. Every list in the master list specifies a r o w .
/// If the dimensions are known, consider to use ofRowList instead to avoid multiple e n u m e r a t i o n .
/// If the dimensions are known, consider to use ofRowList instead to avoid multiple e n u m e r a t i o n .
let inline ofList (fll: float list list) =
let inline ofList (fll: float list list) =
@ -96,38 +107,39 @@ module DenseMatrix =
DenseMatrix.OfRowsCovariant(n, m, fll) :> _ M a t r i x
DenseMatrix.OfRowsCovariant(n, m, fll) :> _ M a t r i x
/// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a r o w .
/// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a r o w .
let inline ofRows (rows: int) (cols: int) (fss: #seq<#seq<float>>) = DenseMatrix.OfRowsCovariant(rows, cols, fss) :> _ M a t r i x
/// If the dimensions are known, consider to use ofRowSeq instead to avoid multiple e n u m e r a t i o n .
let inline ofSeq (fss: #seq<#seq<float>>) =
let n = Seq.length f s s
let m = Seq.length (Seq.head f s s )
DenseMatrix.OfRowsCovariant(n, m, fss) :> _ M a t r i x
/// Create a matrix from a list of float lists. Every list in the master list specifies a r o w .
let inline ofRowsList (rows: int) (cols: int) (fll: float list list) = DenseMatrix.OfRowsCovariant(rows, cols, fll) :> _ M a t r i x
/// Create a matrix from a list of row v e c t o r s .
/// Create a matrix from a list of row v e c t o r s .
let inline ofRowVector s (vectors: #Vector<float> list) = DenseMatrix.OfRowVectors(vectors |> Array.ofList |> box |> unbox) :> _ M a t r i x
let inline ofRows (vectors: #Vector<float> list) = DenseMatrix.OfRowVectors(vectors |> Array.ofList |> box |> unbox) :> _ M a t r i x
/// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a c o l u m n .
/// Create a matrix from a list of float lists. Every list in the master list specifies a r o w .
let inline ofColumns (rows: int) (cols: int) (fss: #seq<#seq<float>>) = DenseMatrix.OfColumnsCovariant(rows, cols, fss ) :> _ M a t r i x
let inline ofRowList (rows: int) (cols: int) (fll: float list list) = DenseMatrix.OfRowsCovariant(rows, cols, fll ) :> _ M a t r i x
/// Create a matrix from a list of float lists. Every list in the master list specifies a c o l u m n .
/// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a r o w .
let inline ofColumnsList (rows: int) (cols: int) (fll: float list list) = DenseMatrix.OfColumnsCovariant(rows, cols, fll ) :> _ M a t r i x
let inline ofRowSeq (rows: int) (cols: int) (fss: #seq<#seq<float>>) = DenseMatrix.OfRowsCovariant(rows, cols, fss ) :> _ M a t r i x
/// Create a matrix from a list of column v e c t o r s .
/// Create a matrix from a list of column v e c t o r s .
let inline ofColumnVector s (vectors: #Vector<float> list) = DenseMatrix.OfColumnVectors(vectors |> Array.ofList |> box |> unbox) :> _ M a t r i x
let inline ofColumns (vectors: #Vector<float> list) = DenseMatrix.OfColumnVectors(vectors |> Array.ofList |> box |> unbox) :> _ M a t r i x
/// Create a matrix with a given dimension from an indexed sequences of row, column, value tuples.
/// Create a matrix from a list of float lists. Every list in the master list specifies a c o l u m n .
let inline ofSeqi (rows: int) (cols: int) (fs: #seq<int * int * float>) = DenseMatrix.OfIndexed(rows, cols, fs) :> _ M a t r i x
let inline ofColumnList (rows: int) (cols: int) (fll: float list list) = DenseMatrix.OfColumnsCovariant(rows, cols, fll) :> _ M a t r i x
/// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a c o l u m n .
let inline ofColumnSeq (rows: int) (cols: int) (fss: #seq<#seq<float>>) = DenseMatrix.OfColumnsCovariant(rows, cols, fss) :> _ M a t r i x
/// Create a matrix with a given dimension from an indexed list of row, column, value tuples.
/// Create a matrix with a given dimension from an indexed list of row, column, value tuples.
let inline ofListi (rows: int) (cols: int) (fl: list<int * int * float>) = DenseMatrix.OfIndexed(rows, cols, Seq.ofList fl) :> _ M a t r i x
let inline ofListi (rows: int) (cols: int) (fl: list<int * int * float>) = DenseMatrix.OfIndexed(rows, cols, Seq.ofList fl) :> _ M a t r i x
/// Create a square matrix with constant diagonal e n t r i e s .
/// Create a matrix with a given dimension from an indexed sequences of row, column, value tuples.
let inline constDiag (n: int) (f: float) =
let inline ofSeqi (rows: int) (cols: int) (fs: #seq<int * int * float>) = DenseMatrix.OfIndexed(rows, cols, fs) :> _ M a t r i x
let A = D e n s e M a t r i x ( n , n )
for i=0 to n-1 d o
A . A t ( i , i , f )
A :> _ M a t r i x
/// Create a square matrix with the vector elements on the d i a g o n a l .
/// Create a square matrix with the vector elements on the d i a g o n a l .
let inline d iag (v: #Vector<float>) =
let inline ofDiag (v: #Vector<float>) =
let n = v . C o u n t
let n = v . C o u n t
let A = D e n s e M a t r i x ( n , n )
let A = D e n s e M a t r i x ( n , n )
A . S e t D i a g o n a l ( v )
A . S e t D i a g o n a l ( v )
@ -152,9 +164,25 @@ module SparseMatrix =
/// Create an all-zero matrix with the given d i m e n s i o n .
/// Create an all-zero matrix with the given d i m e n s i o n .
let inline zeroCreate (rows: int) (cols: int) = SparseMatrix(rows, cols) :> _ M a t r i x
let inline zeroCreate (rows: int) (cols: int) = SparseMatrix(rows, cols) :> _ M a t r i x
/// Create a matrix with the given dimension and set all diagonal values to x. All other values are z e r o .
let inline createDiag (rows: int) (cols: int) x =
let A = S p a r s e M a t r i x ( r o w s , c o l s )
for i=0 to min (rows-1) (cols-1) do A . A t ( i , i , x )
A :> _ M a t r i x
/// Initialize a matrix by calling a construction function for every e l e m e n t .
/// Initialize a matrix by calling a construction function for every e l e m e n t .
let inline init (rows: int) (cols: int) (f: int -> int -> float) = SparseMatrix.Create(rows, cols, fun n m -> f n m) :> _ M a t r i x
let inline init (rows: int) (cols: int) (f: int -> int -> float) = SparseMatrix.Create(rows, cols, fun n m -> f n m) :> _ M a t r i x
/// Initialize a matrix by calling a construction function for every diagonal element. All other values are z e r o .
let inline initDiag (rows: int) (cols: int) (f: int -> float) =
let A = S p a r s e M a t r i x ( r o w s , c o l s )
for i=0 to min (rows-1) (cols-1) do A.At(i,i,f i )
A :> _ M a t r i x
/// Create an identity matrix with the given d i m e n s i o n .
let inline identity (rows: int) (cols: int) = createDiag rows cols 1 .0
/// Create a matrix from a 2 D array of floating point n u m b e r s .
/// Create a matrix from a 2 D array of floating point n u m b e r s .
let inline ofArray2 array = SparseMatrix.OfArray(array) :> _ M a t r i x
let inline ofArray2 array = SparseMatrix.OfArray(array) :> _ M a t r i x
@ -166,33 +194,33 @@ module SparseMatrix =
[<System.ObsoleteAttribute("Use ofListi instead. Will be changed to expect a non-indexed list in a future v e r s i o n . " ) > ]
[<System.ObsoleteAttribute("Use ofListi instead. Will be changed to expect a non-indexed list in a future v e r s i o n . " ) > ]
let inline ofList (rows: int) (cols: int) (fl: list<int * int * float>) = SparseMatrix.OfIndexed(rows, cols, Seq.ofList fl) :> _ M a t r i x
let inline ofList (rows: int) (cols: int) (fl: list<int * int * float>) = SparseMatrix.OfIndexed(rows, cols, Seq.ofList fl) :> _ M a t r i x
/// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a r o w .
let inline ofRows (rows: int) (cols: int) (fss: #seq<#seq<float>>) = SparseMatrix.OfRowsCovariant(rows, cols, fss) :> _ M a t r i x
/// Create a matrix from a list of row v e c t o r s .
let inline ofRows (vectors: #Vector<float> list) = SparseMatrix.OfRowVectors(vectors |> Array.ofList |> box |> unbox) :> _ M a t r i x
/// Create a matrix from a list of float lists. Every list in the master list specifies a r o w .
/// Create a matrix from a list of float lists. Every list in the master list specifies a r o w .
let inline ofRows List (rows: int) (cols: int) (fll: float list list) = SparseMatrix.OfRowsCovariant(rows, cols, fll) :> _ M a t r i x
let inline ofRowList (rows: int) (cols: int) (fll: float list list) = SparseMatrix.OfRowsCovariant(rows, cols, fll) :> _ M a t r i x
/// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a c o l u m n .
/// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a r o w .
let inline ofColumns (rows: int) (cols: int) (fss: #seq<#seq<float>>) = SparseMatrix.OfColumnsCovariant(rows, cols, fss) :> _ M a t r i x
let inline ofRowSeq (rows: int) (cols: int) (fss: #seq<#seq<float>>) = SparseMatrix.OfRowsCovariant(rows, cols, fss) :> _ M a t r i x
/// Create a matrix from a list of column v e c t o r s .
let inline ofColumns (vectors: #Vector<float> list) = SparseMatrix.OfColumnVectors(vectors |> Array.ofList |> box |> unbox) :> _ M a t r i x
/// Create a matrix from a list of float lists. Every list in the master list specifies a c o l u m n .
/// Create a matrix from a list of float lists. Every list in the master list specifies a c o l u m n .
let inline ofColumnsList (rows: int) (cols: int) (fll: float list list) = SparseMatrix.OfColumnsCovariant(rows, cols, fll) :> _ M a t r i x
let inline ofColumnList (rows: int) (cols: int) (fll: float list list) = SparseMatrix.OfColumnsCovariant(rows, cols, fll) :> _ M a t r i x
/// Create a matrix with a given dimension from an indexed sequences of row, column, value tuples .
/// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a c o l u m n .
let inline ofSeqi (rows: int) (cols: int) (fs: #seq<int * int * float>) = SparseMatrix.OfIndexed (rows, cols, fs) :> _ M a t r i x
let inline ofColumn Seq (rows: int) (cols: int) (fss: #seq<#seq<float>>) = SparseMatrix.OfColumnsCovariant (rows, cols, fs s) :> _ M a t r i x
/// Create a matrix with a given dimension from an indexed list of row, column, value tuples.
/// Create a matrix with a given dimension from an indexed list of row, column, value tuples.
let inline ofListi (rows: int) (cols: int) (fl: list<int * int * float>) = SparseMatrix.OfIndexed(rows, cols, Seq.ofList fl) :> _ M a t r i x
let inline ofListi (rows: int) (cols: int) (fl: list<int * int * float>) = SparseMatrix.OfIndexed(rows, cols, Seq.ofList fl) :> _ M a t r i x
/// Create a square matrix with constant diagonal e n t r i e s .
/// Create a matrix with a given dimension from an indexed sequences of row, column, value tuples.
let inline constDiag (n: int) (f: float) =
let inline ofSeqi (rows: int) (cols: int) (fs: #seq<int * int * float>) = SparseMatrix.OfIndexed(rows, cols, fs) :> _ M a t r i x
let A = S p a r s e M a t r i x ( n , n )
for i=0 to n-1 d o
A . A t ( i , i , f )
A :> _ M a t r i x
/// Create a square matrix with the vector elements on the d i a g o n a l .
/// Create a square matrix with the vector elements on the d i a g o n a l .
let inline d iag (v: #Vector<float>) =
let inline ofDiag (v: #Vector<float>) =
let n = v . C o u n t
let n = v . C o u n t
let A = S p a r s e M a t r i x ( n , n )
let A = S p a r s e M a t r i x ( n , n )
A . S e t D i a g o n a l ( v )
A . S e t D i a g o n a l ( v )