Browse Source

LA: create matrix from row/col arrays, rework & simplify creating from F#

optimization-1
Christoph Ruegg 13 years ago
parent
commit
015d5211f0
  1. 2
      src/FSharp/Fit.fs
  2. 142
      src/FSharp/LinearAlgebra.Double.Matrix.fs
  3. 4
      src/FSharp/LinearAlgebra.Double.Vector.fs
  4. 4
      src/FSharp/LinearAlgebra.Double.fs
  5. 20
      src/FSharpExamples/Matrices.fsx
  6. 20
      src/FSharpUnitTests/DenseMatrixTests.fs
  7. 14
      src/FSharpUnitTests/SparseMatrixTests.fs
  8. 40
      src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs
  9. 42
      src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs
  10. 40
      src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs
  11. 42
      src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs
  12. 40
      src/Numerics/LinearAlgebra/Double/DenseMatrix.cs
  13. 42
      src/Numerics/LinearAlgebra/Double/SparseMatrix.cs
  14. 40
      src/Numerics/LinearAlgebra/Single/DenseMatrix.cs
  15. 42
      src/Numerics/LinearAlgebra/Single/SparseMatrix.cs
  16. 51
      src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs
  17. 76
      src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs

2
src/FSharp/Fit.fs

@ -62,7 +62,7 @@ module Fit =
let linear functions (x:_[]) (y:float[]) = let linear functions (x:_[]) (y:float[]) =
functions functions
|> List.map (fun f -> List.init (Array.length x) (fun i -> f x.[i])) |> List.map (fun f -> List.init (Array.length x) (fun i -> f x.[i]))
|> DenseMatrix.ofColumnList (Array.length x) (List.length functions) |> DenseMatrix.ofColumnList
|> fun m -> m.QR(QRMethod.Thin).Solve(DenseVector(y)).ToArray() |> fun m -> m.QR(QRMethod.Thin).Solve(DenseVector(y)).ToArray()
|> List.ofArray |> List.ofArray

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

@ -61,6 +61,7 @@ module Matrix =
v.At(k, macc) v.At(k, macc)
v :> _ Vector v :> _ Vector
/// A module which implements functional dense vector operations. /// A module which implements functional dense vector operations.
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>] [<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module DenseMatrix = module DenseMatrix =
@ -78,7 +79,7 @@ module DenseMatrix =
let inline create (rows: int) (cols: int) x = DenseMatrix.Create(rows, cols, fun i j -> x) :> _ Matrix let inline create (rows: int) (cols: int) x = DenseMatrix.Create(rows, cols, fun i j -> x) :> _ Matrix
/// Create a matrix with the given dimension and set all diagonal values to x. All other values are zero. /// Create a matrix with the given dimension and set all diagonal values to x. All other values are zero.
let inline createDiag (rows: int) (cols: int) x = let createDiag (rows: int) (cols: int) x =
let A = DenseMatrix(rows,cols) let A = DenseMatrix(rows,cols)
for i=0 to min (rows-1) (cols-1) do A.At(i,i,x) for i=0 to min (rows-1) (cols-1) do A.At(i,i,x)
A :> _ Matrix A :> _ Matrix
@ -86,8 +87,14 @@ module DenseMatrix =
/// Initialize a matrix by calling a construction function for every element. /// Initialize a matrix by calling a construction function for every element.
let inline init (rows: int) (cols: int) (f: int -> int -> float) = DenseMatrix.Create(rows, cols, fun i j -> f i j) :> _ Matrix let inline init (rows: int) (cols: int) (f: int -> int -> float) = DenseMatrix.Create(rows, cols, fun i j -> f i j) :> _ Matrix
/// Initialize a matrix by calling a construction function for every row.
let inline initRows (rows: int) (f: int -> Vector<float>) = DenseMatrix.OfRowVectors(Array.init rows f) :> _ Matrix
/// Initialize a matrix by calling a construction function for every column.
let inline initColumns (cols: int) (f: int -> Vector<float>) = DenseMatrix.OfColumnVectors(Array.init cols f) :> _ Matrix
/// Initialize a matrix by calling a construction function for every diagonal element. All other values are zero. /// Initialize a matrix by calling a construction function for every diagonal element. All other values are zero.
let inline initDiag (rows: int) (cols: int) (f: int -> float) = let initDiag (rows: int) (cols: int) (f: int -> float) =
let A = DenseMatrix(rows,cols) let A = DenseMatrix(rows,cols)
for i=0 to min (rows-1) (cols-1) do A.At(i,i,f i) for i=0 to min (rows-1) (cols-1) do A.At(i,i,f i)
A :> _ Matrix A :> _ Matrix
@ -95,67 +102,54 @@ module DenseMatrix =
/// Create an identity matrix with the given dimension. /// Create an identity matrix with the given dimension.
let inline identity (rows: int) (cols: int) = createDiag rows cols 1.0 let inline identity (rows: int) (cols: int) = createDiag rows cols 1.0
/// Create a matrix from a 2D array of floating point numbers. /// Create a matrix from a 2D array of floating point numbers.
let inline ofArray2 array = DenseMatrix.OfArray(array) :> _ Matrix let inline ofArray2 array = DenseMatrix.OfArray(array) :> _ Matrix
/// Create a matrix from a list of float lists. Every list in the master list specifies a row.
/// If the dimensions are known, consider to use ofRowList instead to avoid multiple enumeration.
let inline ofList (fll: float list list) =
let n = List.length fll
let m = List.length (List.head fll)
DenseMatrix.OfRowsCovariant(n, m, fll) :> _ Matrix
/// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a row.
/// If the dimensions are known, consider to use ofRowSeq instead to avoid multiple enumeration.
let inline ofSeq (fss: #seq<#seq<float>>) =
let n = Seq.length fss
let m = Seq.length (Seq.head fss)
DenseMatrix.OfRowsCovariant(n, m, fss) :> _ Matrix
/// Create a matrix from a list of row vectors. /// Create a matrix from a list of row vectors.
let inline ofRows (vectors: #Vector<float> list) = DenseMatrix.OfRowVectors(vectors |> Array.ofList |> box |> unbox) :> _ Matrix let inline ofRows (rows: Vector<float> list) = DenseMatrix.OfRowVectors(Array.ofList rows) :> _ Matrix
/// Create a matrix from a list of row arrays.
let inline ofRowArrays (rows: float[][]) = DenseMatrix.OfRowArrays(rows) :> _ Matrix
/// Create a matrix from a list of float lists. Every list in the master list specifies a row. /// Create a matrix from a list of float lists. Every list in the master list specifies a row.
let inline ofRowList (rows: int) (cols: int) (fll: float list list) = DenseMatrix.OfRowsCovariant(rows, cols, fll) :> _ Matrix let inline ofRowList (rows: float list list) = DenseMatrix.OfRowArrays(rows |> List.map List.toArray |> List.toArray) :> _ Matrix
/// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a row.
let inline ofRowSeq (rows: #seq<#seq<float>>) = DenseMatrix.OfRowArrays(rows |> Seq.map Seq.toArray |> Seq.toArray) :> _ Matrix
/// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a row. /// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a row.
let inline ofRowSeq (rows: int) (cols: int) (fss: #seq<#seq<float>>) = DenseMatrix.OfRowsCovariant(rows, cols, fss) :> _ Matrix let inline ofRowSeq2 (rows: int) (cols: int) (seqOfRows: #seq<seq<float>>) = DenseMatrix.OfRows(rows, cols, seqOfRows) :> _ Matrix
/// Create a matrix from a list of column vectors. /// Create a matrix from a list of column vectors.
let inline ofColumns (vectors: #Vector<float> list) = DenseMatrix.OfColumnVectors(vectors |> Array.ofList |> box |> unbox) :> _ Matrix let inline ofColumns (columns: Vector<float> list) = DenseMatrix.OfColumnVectors(Array.ofList columns) :> _ Matrix
/// Create a matrix from a list of column arrays.
let inline ofColumnArrays (columns: float[][]) = DenseMatrix.OfColumnArrays(columns) :> _ Matrix
/// Create a matrix from a list of float lists. Every list in the master list specifies a column. /// Create a matrix from a list of float lists. Every list in the master list specifies a column.
let inline ofColumnList (rows: int) (cols: int) (fll: float list list) = DenseMatrix.OfColumnsCovariant(rows, cols, fll) :> _ Matrix let inline ofColumnList (columns: float list list) = DenseMatrix.OfColumnArrays(columns |> List.map List.toArray |> List.toArray) :> _ Matrix
/// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a column.
let inline ofColumnSeq (columns: #seq<#seq<float>>) = DenseMatrix.OfColumnArrays(columns |> Seq.map Seq.toArray |> Seq.toArray) :> _ Matrix
/// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a column. /// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a column.
let inline ofColumnSeq (rows: int) (cols: int) (fss: #seq<#seq<float>>) = DenseMatrix.OfColumnsCovariant(rows, cols, fss) :> _ Matrix let inline ofColumnSeq2 (rows: int) (cols: int) (seqOfCols: #seq<seq<float>>) = DenseMatrix.OfColumns(rows, cols, seqOfCols) :> _ Matrix
/// 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) :> _ Matrix let inline ofListi (rows: int) (cols: int) (indexed: list<int * int * float>) = DenseMatrix.OfIndexed(rows, cols, Seq.ofList indexed) :> _ Matrix
/// Create a matrix with a given dimension from an indexed sequences of row, column, value tuples. /// Create a matrix with a given dimension from an indexed sequences of row, column, value tuples.
let inline ofSeqi (rows: int) (cols: int) (fs: #seq<int * int * float>) = DenseMatrix.OfIndexed(rows, cols, fs) :> _ Matrix let inline ofSeqi (rows: int) (cols: int) (indexed: #seq<int * int * float>) = DenseMatrix.OfIndexed(rows, cols, indexed) :> _ Matrix
/// Create a square matrix with the vector elements on the diagonal. /// Create a matrix with the vector elements on the diagonal.
let inline ofDiag (v: #Vector<float>) = let ofDiag2 (rows: int) (cols: int) (v: Vector<float>) =
let n = v.Count let A = DenseMatrix(rows,cols)
let A = DenseMatrix(n,n)
A.SetDiagonal(v) A.SetDiagonal(v)
A :> _ Matrix A :> _ Matrix
/// Initialize a matrix by calling a construction function for every row. /// Create a square matrix with the vector elements on the diagonal.
let inline initRow (rows: int) (cols: int) (f: int -> #Vector<float>) = let inline ofDiag (v: Vector<float>) = ofDiag2 v.Count v.Count v
let A = DenseMatrix(rows,cols)
for i=0 to rows-1 do A.SetRow(i, f i)
A :> _ Matrix
/// Initialize a matrix by calling a construction function for every column.
let inline initCol (rows: int) (cols: int) (f: int -> #Vector<float>) =
let A = DenseMatrix(rows,cols)
for i=0 to cols-1 do A.SetColumn(i, f i)
A :> _ Matrix
/// A module which implements functional sparse vector operations. /// A module which implements functional sparse vector operations.
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>] [<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
@ -165,7 +159,7 @@ module SparseMatrix =
let inline zeroCreate (rows: int) (cols: int) = SparseMatrix(rows, cols) :> _ Matrix let inline zeroCreate (rows: int) (cols: int) = SparseMatrix(rows, cols) :> _ Matrix
/// Create a matrix with the given dimension and set all diagonal values to x. All other values are zero. /// Create a matrix with the given dimension and set all diagonal values to x. All other values are zero.
let inline createDiag (rows: int) (cols: int) x = let createDiag (rows: int) (cols: int) x =
let A = SparseMatrix(rows,cols) let A = SparseMatrix(rows,cols)
for i=0 to min (rows-1) (cols-1) do A.At(i,i,x) for i=0 to min (rows-1) (cols-1) do A.At(i,i,x)
A :> _ Matrix A :> _ Matrix
@ -173,8 +167,14 @@ module SparseMatrix =
/// Initialize a matrix by calling a construction function for every element. /// Initialize a matrix by calling a construction function for every element.
let inline init (rows: int) (cols: int) (f: int -> int -> float) = SparseMatrix.Create(rows, cols, fun n m -> f n m) :> _ Matrix let inline init (rows: int) (cols: int) (f: int -> int -> float) = SparseMatrix.Create(rows, cols, fun n m -> f n m) :> _ Matrix
/// Initialize a matrix by calling a construction function for every row.
let inline initRows (rows: int) (f: int -> Vector<float>) = SparseMatrix.OfRowVectors(Array.init rows f) :> _ Matrix
/// Initialize a matrix by calling a construction function for every column.
let inline initColumns (cols: int) (f: int -> Vector<float>) = SparseMatrix.OfColumnVectors(Array.init cols f) :> _ Matrix
/// Initialize a matrix by calling a construction function for every diagonal element. All other values are zero. /// Initialize a matrix by calling a construction function for every diagonal element. All other values are zero.
let inline initDiag (rows: int) (cols: int) (f: int -> float) = let initDiag (rows: int) (cols: int) (f: int -> float) =
let A = SparseMatrix(rows,cols) let A = SparseMatrix(rows,cols)
for i=0 to min (rows-1) (cols-1) do A.At(i,i,f i) for i=0 to min (rows-1) (cols-1) do A.At(i,i,f i)
A :> _ Matrix A :> _ Matrix
@ -182,58 +182,50 @@ module SparseMatrix =
/// Create an identity matrix with the given dimension. /// Create an identity matrix with the given dimension.
let inline identity (rows: int) (cols: int) = createDiag rows cols 1.0 let inline identity (rows: int) (cols: int) = createDiag rows cols 1.0
/// Create a matrix from a 2D array of floating point numbers. /// Create a matrix from a 2D array of floating point numbers.
let inline ofArray2 array = SparseMatrix.OfArray(array) :> _ Matrix let inline ofArray2 array = SparseMatrix.OfArray(array) :> _ Matrix
/// Create a matrix with a given dimension from an indexed sequences of row, column, value tuples.
[<System.ObsoleteAttribute("Use ofSeqi instead. Will be changed to expect a non-indexed seq in a future version.")>]
let inline ofSeq (rows: int) (cols: int) (fs: #seq<int * int * float>) = SparseMatrix.OfIndexed(rows, cols, fs) :> _ Matrix
/// Create a matrix with a given dimension from an indexed list of row, column, value tuples.
[<System.ObsoleteAttribute("Use ofListi instead. Will be changed to expect a non-indexed list in a future version.")>]
let inline ofList (rows: int) (cols: int) (fl: list<int * int * float>) = SparseMatrix.OfIndexed(rows, cols, Seq.ofList fl) :> _ Matrix
/// Create a matrix from a list of row vectors. /// Create a matrix from a list of row vectors.
let inline ofRows (vectors: #Vector<float> list) = SparseMatrix.OfRowVectors(vectors |> Array.ofList |> box |> unbox) :> _ Matrix let inline ofRows (rows: Vector<float> list) = SparseMatrix.OfRowVectors(Array.ofList rows) :> _ Matrix
/// Create a matrix from a list of row arrays.
let inline ofRowArrays (rows: float[][]) = SparseMatrix.OfRowArrays(rows) :> _ Matrix
/// Create a matrix from a list of float lists. Every list in the master list specifies a row. /// Create a matrix from a list of float lists. Every list in the master list specifies a row.
let inline ofRowList (rows: int) (cols: int) (fll: float list list) = SparseMatrix.OfRowsCovariant(rows, cols, fll) :> _ Matrix let inline ofRowList (rows: float list list) = SparseMatrix.OfRowArrays(rows |> List.map List.toArray |> List.toArray) :> _ Matrix
/// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a row.
let inline ofRowSeq (rows: #seq<#seq<float>>) = SparseMatrix.OfRowArrays(rows |> Seq.map Seq.toArray |> Seq.toArray) :> _ Matrix
/// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a row. /// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a row.
let inline ofRowSeq (rows: int) (cols: int) (fss: #seq<#seq<float>>) = SparseMatrix.OfRowsCovariant(rows, cols, fss) :> _ Matrix let inline ofRowSeq2 (rows: int) (cols: int) (seqOfRows: #seq<seq<float>>) = SparseMatrix.OfRows(rows, cols, seqOfRows) :> _ Matrix
/// Create a matrix from a list of column vectors. /// Create a matrix from a list of column vectors.
let inline ofColumns (vectors: #Vector<float> list) = SparseMatrix.OfColumnVectors(vectors |> Array.ofList |> box |> unbox) :> _ Matrix let inline ofColumns (columns: Vector<float> list) = SparseMatrix.OfColumnVectors(Array.ofList columns) :> _ Matrix
/// Create a matrix from a list of column arrays.
let inline ofColumnArrays (columns: float[][]) = SparseMatrix.OfColumnArrays(columns) :> _ Matrix
/// Create a matrix from a list of float lists. Every list in the master list specifies a column. /// Create a matrix from a list of float lists. Every list in the master list specifies a column.
let inline ofColumnList (rows: int) (cols: int) (fll: float list list) = SparseMatrix.OfColumnsCovariant(rows, cols, fll) :> _ Matrix let inline ofColumnList (columns: float list list) = SparseMatrix.OfColumnArrays(columns |> List.map List.toArray |> List.toArray) :> _ Matrix
/// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a column.
let inline ofColumnSeq (columns: #seq<#seq<float>>) = SparseMatrix.OfColumnArrays(columns |> Seq.map Seq.toArray |> Seq.toArray) :> _ Matrix
/// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a column. /// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a column.
let inline ofColumnSeq (rows: int) (cols: int) (fss: #seq<#seq<float>>) = SparseMatrix.OfColumnsCovariant(rows, cols, fss) :> _ Matrix let inline ofColumnSeq2 (rows: int) (cols: int) (seqOfCols: #seq<seq<float>>) = SparseMatrix.OfColumns(rows, cols, seqOfCols) :> _ Matrix
/// 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) :> _ Matrix let inline ofListi (rows: int) (cols: int) (indexed: list<int * int * float>) = SparseMatrix.OfIndexed(rows, cols, Seq.ofList indexed) :> _ Matrix
/// Create a matrix with a given dimension from an indexed sequences of row, column, value tuples. /// Create a matrix with a given dimension from an indexed sequences of row, column, value tuples.
let inline ofSeqi (rows: int) (cols: int) (fs: #seq<int * int * float>) = SparseMatrix.OfIndexed(rows, cols, fs) :> _ Matrix let inline ofSeqi (rows: int) (cols: int) (indexed: #seq<int * int * float>) = SparseMatrix.OfIndexed(rows, cols, indexed) :> _ Matrix
/// Create a square matrix with the vector elements on the diagonal.
let inline ofDiag (v: #Vector<float>) =
let n = v.Count
let A = SparseMatrix(n,n)
A.SetDiagonal(v)
A :> _ Matrix
/// Initialize a matrix by calling a construction function for every row. /// Create a matrix with the vector elements on the diagonal.
let inline initRow (rows: int) (cols: int) (f: int -> #Vector<float>) = let ofDiag2 (rows: int) (cols: int) (v: Vector<float>) =
let A = SparseMatrix(rows,cols) let A = SparseMatrix(rows,cols)
for i=0 to rows-1 do A.SetRow(i, f i) A.SetDiagonal(v)
A :> _ Matrix A :> _ Matrix
/// Initialize a matrix by calling a construction function for every column. /// Create a square matrix with the vector elements on the diagonal.
let inline initCol (rows: int) (cols: int) (f: int -> #Vector<float>) = let inline ofDiag (v: Vector<float>) = ofDiag2 v.Count v.Count v
let A = SparseMatrix(rows,cols)
for i=0 to cols-1 do A.SetColumn(i, f i)
A :> _ Matrix

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

@ -97,10 +97,10 @@ module SparseVector =
let inline init (n: int) (f: int -> float) = SparseVector.Create(n, fun i -> f i) :> _ Vector let inline init (n: int) (f: int -> float) = SparseVector.Create(n, fun i -> f i) :> _ Vector
/// Create a sparse vector from a float list. /// Create a sparse vector from a float list.
let inline ofList (n: int) (fl: float list) = SparseVector.OfEnumerable(Seq.ofList fl) :> _ Vector let inline ofList (fl: float list) = SparseVector.OfEnumerable(Seq.ofList fl) :> _ Vector
/// Create a sparse vector from a float sequence. /// Create a sparse vector from a float sequence.
let inline ofSeq (n: int) (fs: #seq<float>) = SparseVector.OfEnumerable(fs) :> _ Vector let inline ofSeq (fs: #seq<float>) = SparseVector.OfEnumerable(fs) :> _ Vector
/// Create a sparse vector with a given dimension from an indexed list of index, value pairs. /// 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) :> _ Vector let inline ofListi (n: int) (fl: list<int * float>) = SparseVector.OfIndexedEnumerable(n, Seq.ofList fl) :> _ Vector

4
src/FSharp/LinearAlgebra.Double.fs

@ -36,8 +36,8 @@ open MathNet.Numerics.LinearAlgebra
[<AutoOpen>] [<AutoOpen>]
module Utility = module Utility =
/// Construct a dense matrix from a list of floating point numbers. /// Construct a dense matrix from a nested list of floating point numbers.
let inline matrix (lst: list<list<float>>) = DenseMatrix.ofList lst let inline matrix (lst: list<list<float>>) = DenseMatrix.ofRowList lst
/// Construct a dense vector from a list of floating point numbers. /// Construct a dense vector from a list of floating point numbers.
let inline vector (lst: list<float>) = DenseVector.ofList lst let inline vector (lst: list<float>) = DenseVector.ofList lst

20
src/FSharpExamples/Matrices.fsx

@ -53,21 +53,25 @@ let d1 = DenseMatrix.init 3 4 (fun i j -> float i / 100.0 + float j)
let d2 = SparseMatrix.init 3 4 (fun i j -> if i=j then float i / 100.0 + float j else 0.0) let d2 = SparseMatrix.init 3 4 (fun i j -> if i=j then float i / 100.0 + float j else 0.0)
// Matrices can also be constructed from sequences of rows or of columns // Matrices can also be constructed from sequences of rows or of columns
let e1 = DenseMatrix.ofRowSeq 20 10 (seq { for i in 1 .. 20 do yield Array.init 10 (fun j -> float j + 100.0 * float i) }) let e1 = DenseMatrix.ofRowSeq (seq { for i in 1 .. 20 do yield Array.init 10 (fun j -> float j + 100.0 * float i) })
let e2 = SparseMatrix.ofRowSeq 20 10 (seq { for i in 1 .. 20 do yield Array.init 10 (fun j -> if i%5 = 0 then float j + 100.0 * float i else 0.0) }) let e2 = SparseMatrix.ofRowSeq (seq { for i in 1 .. 20 do yield Array.init 10 (fun j -> if i%5 = 0 then float j + 100.0 * float i else 0.0) })
let e3 = DenseMatrix.ofColumnSeq 20 10 (seq { for j in 1 .. 10 do yield Array.init 20 (fun i -> float j + 100.0 * float i) }) let e3 = DenseMatrix.ofColumnSeq (seq { for j in 1 .. 10 do yield Array.init 20 (fun i -> float j + 100.0 * float i) })
let e4 = SparseMatrix.ofColumnSeq 20 10 (seq { for j in 1 .. 10 do yield Array.init 20 (fun i -> if i%5 = 0 then float j + 100.0 * float i else 0.0) }) let e4 = SparseMatrix.ofColumnSeq (seq { for j in 1 .. 10 do yield Array.init 20 (fun i -> if i%5 = 0 then float j + 100.0 * float i else 0.0) })
// Or from F# lists // Or from F# lists
let f1 = DenseMatrix.ofRowList 20 10 [ for i in 1 .. 20 -> List.init 10 (fun j -> float j + 100.0 * float i) ] let f1 = DenseMatrix.ofRowList [ for i in 1 .. 20 -> List.init 10 (fun j -> float j + 100.0 * float i) ]
let f2 = SparseMatrix.ofRowList 20 10 [ for i in 1 .. 20 -> List.init 10 (fun j -> if i%5 = 0 then float j + 100.0 * float i else 0.0) ] let f2 = SparseMatrix.ofRowList [ for i in 1 .. 20 -> List.init 10 (fun j -> if i%5 = 0 then float j + 100.0 * float i else 0.0) ]
let f3 = DenseMatrix.ofColumnList 20 10 [ for j in 1 .. 10 -> List.init 20 (fun i -> float j + 100.0 * float i) ] let f3 = DenseMatrix.ofColumnList [ for j in 1 .. 10 -> List.init 20 (fun i -> float j + 100.0 * float i) ]
let f4 = SparseMatrix.ofColumnList 20 10 [ for j in 1 .. 10 -> List.init 20 (fun i -> if i%5 = 0 then float j + 100.0 * float i else 0.0) ] let f4 = SparseMatrix.ofColumnList [ for j in 1 .. 10 -> List.init 20 (fun i -> if i%5 = 0 then float j + 100.0 * float i else 0.0) ]
// Or from row or column vectors // Or from row or column vectors
let m1 = DenseMatrix.ofRows [DenseVector.create 3 1.0; DenseVector.create 3 2.0] let m1 = DenseMatrix.ofRows [DenseVector.create 3 1.0; DenseVector.create 3 2.0]
let m2 = DenseMatrix.ofColumns [DenseVector.create 3 1.0; DenseVector.create 3 2.0] let m2 = DenseMatrix.ofColumns [DenseVector.create 3 1.0; DenseVector.create 3 2.0]
// Or from row or column arrays
let n1 = DenseMatrix.ofRowArrays [| Array.create 3 1.0; Array.create 3 2.0 |]
let n2 = DenseMatrix.ofColumnArrays [| Array.create 3 1.0; Array.create 3 2.0 |]
// Or from indexed lists or sequences where all other values are zero (useful mostly for sparse data) // Or from indexed lists or sequences where all other values are zero (useful mostly for sparse data)
let g1 = DenseMatrix.ofListi 20 10 [(4,3,20.0); (18,9,3.0); (2,1,100.0)] let g1 = DenseMatrix.ofListi 20 10 [(4,3,20.0); (18,9,3.0); (2,1,100.0)]
let g2 = SparseMatrix.ofListi 20 10 [(4,3,20.0); (18,9,3.0); (2,1,100.0)] let g2 = SparseMatrix.ofListi 20 10 [(4,3,20.0); (18,9,3.0); (2,1,100.0)]

20
src/FSharpUnitTests/DenseMatrixTests.fs

@ -44,21 +44,13 @@ module DenseMatrixTests =
DenseMatrix.ofArray2 (Array2D.create 3 2 0.3) |> should equal smallM DenseMatrix.ofArray2 (Array2D.create 3 2 0.3) |> should equal smallM
DenseMatrix.ofArray2 (Array2D.init 100 120 (fun i j -> float i * 100.0 + float j)) |> should equal largeM DenseMatrix.ofArray2 (Array2D.init 100 120 (fun i j -> float i * 100.0 + float j)) |> should equal largeM
[<Test>]
let ``DenseMatrix.ofSeq`` () =
DenseMatrix.ofSeq (Seq.ofList [[0.3;0.3];[0.3;0.3];[0.3;0.3]]) |> should equal smallM
[<Test>]
let ``DenseMatrix.ofList`` () =
DenseMatrix.ofList [[0.3;0.3];[0.3;0.3];[0.3;0.3]] |> should equal smallM
[<Test>] [<Test>]
let ``DenseMatrix.ofRowSeq`` () = let ``DenseMatrix.ofRowSeq`` () =
DenseMatrix.ofRowSeq 3 2 (Seq.ofList [[0.3;0.3];[0.3;0.3];[0.3;0.3]]) |> should equal smallM DenseMatrix.ofRowSeq (Seq.ofList [[0.3;0.3];[0.3;0.3];[0.3;0.3]]) |> should equal smallM
[<Test>] [<Test>]
let ``DenseMatrix.ofRowList`` () = let ``DenseMatrix.ofRowList`` () =
DenseMatrix.ofRowList 3 2 [[0.3;0.3];[0.3;0.3];[0.3;0.3]] |> should equal smallM DenseMatrix.ofRowList [[0.3;0.3];[0.3;0.3];[0.3;0.3]] |> should equal smallM
[<Test>] [<Test>]
let ``DenseMatrix.ofRows`` () = let ``DenseMatrix.ofRows`` () =
@ -66,11 +58,11 @@ module DenseMatrixTests =
[<Test>] [<Test>]
let ``DenseMatrix.ofColumnSeq`` () = let ``DenseMatrix.ofColumnSeq`` () =
DenseMatrix.ofColumnSeq 3 2 (Seq.ofList [[0.3;0.3;0.3];[0.3;0.3;0.3]]) |> should equal smallM DenseMatrix.ofColumnSeq (Seq.ofList [[0.3;0.3;0.3];[0.3;0.3;0.3]]) |> should equal smallM
[<Test>] [<Test>]
let ``DenseMatrix.ofColumnList`` () = let ``DenseMatrix.ofColumnList`` () =
DenseMatrix.ofColumnList 3 2 [[0.3;0.3;0.3];[0.3;0.3;0.3]] |> should equal smallM DenseMatrix.ofColumnList [[0.3;0.3;0.3];[0.3;0.3;0.3]] |> should equal smallM
[<Test>] [<Test>]
let ``DenseMatrix.ofColumns`` () = let ``DenseMatrix.ofColumns`` () =
@ -96,8 +88,8 @@ module DenseMatrixTests =
[<Test>] [<Test>]
let ``DenseMatrix.initRow`` () = let ``DenseMatrix.initRow`` () =
DenseMatrix.initRow 100 120 (fun i -> (DenseVector.init 120 (fun j -> float i * 100.0 + float j))) |> should equal largeM DenseMatrix.initRows 100 (fun i -> (DenseVector.init 120 (fun j -> float i * 100.0 + float j))) |> should equal largeM
[<Test>] [<Test>]
let ``DenseMatrix.initCol`` () = let ``DenseMatrix.initCol`` () =
DenseMatrix.initCol 100 120 (fun j -> (DenseVector.init 100 (fun i -> float i * 100.0 + float j))) |> should equal largeM DenseMatrix.initColumns 120 (fun j -> (DenseVector.init 100 (fun i -> float i * 100.0 + float j))) |> should equal largeM

14
src/FSharpUnitTests/SparseMatrixTests.fs

@ -26,19 +26,19 @@ module SparseMatrixTests =
[<Test>] [<Test>]
let ``SparseMatrix.ofRowSeq`` () = let ``SparseMatrix.ofRowSeq`` () =
SparseMatrix.ofRowSeq 4 6 (Seq.ofList [[0.;0.;0.;0.;0.;0.];[0.;0.;1.;0.;0.;0.];[0.;0.;0.;0.;0.;0.];[0.;0.;0.;0.;0.;0.]]) |> should equal smallM SparseMatrix.ofRowSeq (Seq.ofList [[0.;0.;0.;0.;0.;0.];[0.;0.;1.;0.;0.;0.];[0.;0.;0.;0.;0.;0.];[0.;0.;0.;0.;0.;0.]]) |> should equal smallM
[<Test>] [<Test>]
let ``SparseMatrix.ofRowList`` () = let ``SparseMatrix.ofRowList`` () =
SparseMatrix.ofRowList 4 6 [[0.;0.;0.;0.;0.;0.];[0.;0.;1.;0.;0.;0.];[0.;0.;0.;0.;0.;0.];[0.;0.;0.;0.;0.;0.]] |> should equal smallM SparseMatrix.ofRowList [[0.;0.;0.;0.;0.;0.];[0.;0.;1.;0.;0.;0.];[0.;0.;0.;0.;0.;0.];[0.;0.;0.;0.;0.;0.]] |> should equal smallM
[<Test>] [<Test>]
let ``SparseMatrix.ofColumnSeq`` () = let ``SparseMatrix.ofColumnSeq`` () =
SparseMatrix.ofColumnSeq 4 6 (Seq.ofList [[0.;0.;0.;0.];[0.;0.;0.;0.];[0.;1.;0.;0.];[0.;0.;0.;0.];[0.;0.;0.;0.];[0.;0.;0.;0.]]) |> should equal smallM SparseMatrix.ofColumnSeq (Seq.ofList [[0.;0.;0.;0.];[0.;0.;0.;0.];[0.;1.;0.;0.];[0.;0.;0.;0.];[0.;0.;0.;0.];[0.;0.;0.;0.]]) |> should equal smallM
[<Test>] [<Test>]
let ``SparseMatrix.ofColumnList`` () = let ``SparseMatrix.ofColumnList`` () =
SparseMatrix.ofColumnList 4 6 [[0.;0.;0.;0.];[0.;0.;0.;0.];[0.;1.;0.;0.];[0.;0.;0.;0.];[0.;0.;0.;0.];[0.;0.;0.;0.]] |> should equal smallM SparseMatrix.ofColumnList [[0.;0.;0.;0.];[0.;0.;0.;0.];[0.;1.;0.;0.];[0.;0.;0.;0.];[0.;0.;0.;0.];[0.;0.;0.;0.]] |> should equal smallM
[<Test>] [<Test>]
let ``SparseMatrix.ofSeqi`` () = let ``SparseMatrix.ofSeqi`` () =
@ -53,13 +53,13 @@ module SparseMatrixTests =
SparseMatrix.createDiag 100 100 2.0 |> should equal (2.0 * (SparseMatrix.Identity 100)) SparseMatrix.createDiag 100 100 2.0 |> should equal (2.0 * (SparseMatrix.Identity 100))
[<Test>] [<Test>]
let ``SparseMatrix.diag`` () = let ``SparseMatrix.ofDiag`` () =
SparseMatrix.ofDiag (DenseVector.Create(100, fun i -> 2.0)) |> should equal (2.0 * (SparseMatrix.Identity 100)) SparseMatrix.ofDiag (DenseVector.Create(100, fun i -> 2.0)) |> should equal (2.0 * (SparseMatrix.Identity 100))
[<Test>] [<Test>]
let ``SparseMatrix.init_row`` () = let ``SparseMatrix.init_row`` () =
SparseMatrix.initRow 4 6 (fun i -> if i=1 then DenseVector.raw [|0.;0.;1.;0.;0.;0.|] else DenseVector.zeroCreate 6) |> should equal smallM SparseMatrix.initRows 4 (fun i -> if i=1 then DenseVector.raw [|0.;0.;1.;0.;0.;0.|] else DenseVector.zeroCreate 6) |> should equal smallM
[<Test>] [<Test>]
let ``SparseMatrix.init_col`` () = let ``SparseMatrix.init_col`` () =
SparseMatrix.initCol 4 6 (fun j -> if j=2 then DenseVector.raw [|0.;1.;0.;0.|] else DenseVector.zeroCreate 4) |> should equal smallM SparseMatrix.initColumns 6 (fun j -> if j=2 then DenseVector.raw [|0.;1.;0.;0.|] else DenseVector.zeroCreate 4) |> should equal smallM

40
src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs

@ -173,6 +173,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex>.OfColumnEnumerables(rows, columns, data)); return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex>.OfColumnEnumerables(rows, columns, data));
} }
/// <summary>
/// Create a new dense matrix as a copy of the given column arrays.
/// This new matrix will be independent from the arrays.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static DenseMatrix OfColumnArrays(params Complex[][] columns)
{
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex>.OfColumnArrays(columns));
}
/// <summary> /// <summary>
/// Create a new dense matrix as a copy of the given column vectors. /// Create a new dense matrix as a copy of the given column vectors.
/// This new matrix will be independent from the vectors. /// This new matrix will be independent from the vectors.
@ -189,26 +199,24 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
} }
/// <summary> /// <summary>
/// Create a new dense matrix as a copy of the given enumerable of enumerable columns. /// Create a new dense matrix as a copy of the given enumerable of enumerable rows.
/// Each enumerable in the master enumerable specifies a column. /// Each enumerable in the master enumerable specifies a row.
/// This new matrix will be independent from the enumerables. /// This new matrix will be independent from the enumerables.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
public static DenseMatrix OfColumnsCovariant<TColumn>(int rows, int columns, IEnumerable<TColumn> data) public static DenseMatrix OfRows(int rows, int columns, IEnumerable<IEnumerable<Complex>> data)
where TColumn : IEnumerable<Complex>
{ {
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex>.OfColumnEnumerables(rows, columns, data)); return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex>.OfRowEnumerables(rows, columns, data));
} }
/// <summary> /// <summary>
/// Create a new dense matrix as a copy of the given enumerable of enumerable rows. /// Create a new dense matrix as a copy of the given row arrays.
/// Each enumerable in the master enumerable specifies a row. /// This new matrix will be independent from the arrays.
/// This new matrix will be independent from the enumerables.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
public static DenseMatrix OfRows(int rows, int columns, IEnumerable<IEnumerable<Complex>> data) public static DenseMatrix OfRowArrays(params Complex[][] rows)
{ {
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex>.OfRowEnumerables(rows, columns, data)); return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex>.OfRowArrays(rows));
} }
/// <summary> /// <summary>
@ -226,18 +234,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex>.OfRowVectors(storage)); return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex>.OfRowVectors(storage));
} }
/// <summary>
/// Create a new dense matrix as a copy of the given enumerable of enumerable rows.
/// Each enumerable in the master enumerable specifies a row.
/// This new matrix will be independent from the enumerables.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static DenseMatrix OfRowsCovariant<TRow>(int rows, int columns, IEnumerable<TRow> data)
where TRow : IEnumerable<Complex>
{
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex>.OfRowEnumerables(rows, columns, data));
}
/// <summary> /// <summary>
/// Create a new dense matrix and initialize each value using the provided init function. /// Create a new dense matrix and initialize each value using the provided init function.
/// </summary> /// </summary>

42
src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs

@ -162,6 +162,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return new SparseMatrix(SparseCompressedRowMatrixStorage<Complex>.OfColumnEnumerables(rows, columns, data)); return new SparseMatrix(SparseCompressedRowMatrixStorage<Complex>.OfColumnEnumerables(rows, columns, data));
} }
/// <summary>
/// Create a new sparse matrix as a copy of the given column arrays.
/// This new matrix will be independent from the arrays.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static SparseMatrix OfColumnArrays(params Complex[][] columns)
{
return new SparseMatrix(SparseCompressedRowMatrixStorage<Complex>.OfColumnArrays(columns));
}
/// <summary> /// <summary>
/// Create a new sparse matrix as a copy of the given column vectors. /// Create a new sparse matrix as a copy of the given column vectors.
/// This new matrix will be independent from the vectors. /// This new matrix will be independent from the vectors.
@ -178,27 +188,24 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
} }
/// <summary> /// <summary>
/// Create a new sparse matrix as a copy of the given enumerable of enumerable columns. /// Create a new sparse matrix as a copy of the given enumerable of enumerable rows.
/// Each enumerable in the master enumerable specifies a column. /// Each enumerable in the master enumerable specifies a row.
/// This new matrix will be independent from the enumerables. /// This new matrix will be independent from the enumerables.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
public static SparseMatrix OfColumnsCovariant<TColumn>(int rows, int columns, IEnumerable<TColumn> data) public static SparseMatrix OfRows(int rows, int columns, IEnumerable<IEnumerable<Complex>> data)
// NOTE: flexible typing to 'backport' generic covariance.
where TColumn : IEnumerable<Complex>
{ {
return new SparseMatrix(SparseCompressedRowMatrixStorage<Complex>.OfColumnEnumerables(rows, columns, data)); return new SparseMatrix(SparseCompressedRowMatrixStorage<Complex>.OfRowEnumerables(rows, columns, data));
} }
/// <summary> /// <summary>
/// Create a new sparse matrix as a copy of the given enumerable of enumerable rows. /// Create a new sparse matrix as a copy of the given row arrays.
/// Each enumerable in the master enumerable specifies a row. /// This new matrix will be independent from the arrays.
/// This new matrix will be independent from the enumerables.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
public static SparseMatrix OfRows(int rows, int columns, IEnumerable<IEnumerable<Complex>> data) public static SparseMatrix OfRowArrays(params Complex[][] rows)
{ {
return new SparseMatrix(SparseCompressedRowMatrixStorage<Complex>.OfRowEnumerables(rows, columns, data)); return new SparseMatrix(SparseCompressedRowMatrixStorage<Complex>.OfRowArrays(rows));
} }
/// <summary> /// <summary>
@ -216,19 +223,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return new SparseMatrix(SparseCompressedRowMatrixStorage<Complex>.OfRowVectors(storage)); return new SparseMatrix(SparseCompressedRowMatrixStorage<Complex>.OfRowVectors(storage));
} }
/// <summary>
/// Create a new sparse matrix as a copy of the given enumerable of enumerable rows.
/// Each enumerable in the master enumerable specifies a row.
/// This new matrix will be independent from the enumerables.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static SparseMatrix OfRowsCovariant<TRow>(int rows, int columns, IEnumerable<TRow> data)
// NOTE: flexible typing to 'backport' generic covariance.
where TRow : IEnumerable<Complex>
{
return new SparseMatrix(SparseCompressedRowMatrixStorage<Complex>.OfRowEnumerables(rows, columns, data));
}
/// <summary> /// <summary>
/// Create a new sparse matrix and initialize each value using the provided init function. /// Create a new sparse matrix and initialize each value using the provided init function.
/// </summary> /// </summary>

40
src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs

@ -168,6 +168,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex32>.OfColumnEnumerables(rows, columns, data)); return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex32>.OfColumnEnumerables(rows, columns, data));
} }
/// <summary>
/// Create a new dense matrix as a copy of the given column arrays.
/// This new matrix will be independent from the arrays.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static DenseMatrix OfColumnArrays(params Complex32[][] columns)
{
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex32>.OfColumnArrays(columns));
}
/// <summary> /// <summary>
/// Create a new dense matrix as a copy of the given column vectors. /// Create a new dense matrix as a copy of the given column vectors.
/// This new matrix will be independent from the vectors. /// This new matrix will be independent from the vectors.
@ -184,26 +194,24 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
} }
/// <summary> /// <summary>
/// Create a new dense matrix as a copy of the given enumerable of enumerable columns. /// Create a new dense matrix as a copy of the given enumerable of enumerable rows.
/// Each enumerable in the master enumerable specifies a column. /// Each enumerable in the master enumerable specifies a row.
/// This new matrix will be independent from the enumerables. /// This new matrix will be independent from the enumerables.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
public static DenseMatrix OfColumnsCovariant<TColumn>(int rows, int columns, IEnumerable<TColumn> data) public static DenseMatrix OfRows(int rows, int columns, IEnumerable<IEnumerable<Complex32>> data)
where TColumn : IEnumerable<Complex32>
{ {
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex32>.OfColumnEnumerables(rows, columns, data)); return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex32>.OfRowEnumerables(rows, columns, data));
} }
/// <summary> /// <summary>
/// Create a new dense matrix as a copy of the given enumerable of enumerable rows. /// Create a new dense matrix as a copy of the given row arrays.
/// Each enumerable in the master enumerable specifies a row. /// This new matrix will be independent from the arrays.
/// This new matrix will be independent from the enumerables.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
public static DenseMatrix OfRows(int rows, int columns, IEnumerable<IEnumerable<Complex32>> data) public static DenseMatrix OfRowArrays(params Complex32[][] rows)
{ {
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex32>.OfRowEnumerables(rows, columns, data)); return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex32>.OfRowArrays(rows));
} }
/// <summary> /// <summary>
@ -221,18 +229,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex32>.OfRowVectors(storage)); return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex32>.OfRowVectors(storage));
} }
/// <summary>
/// Create a new dense matrix as a copy of the given enumerable of enumerable rows.
/// Each enumerable in the master enumerable specifies a row.
/// This new matrix will be independent from the enumerables.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static DenseMatrix OfRowsCovariant<TRow>(int rows, int columns, IEnumerable<TRow> data)
where TRow : IEnumerable<Complex32>
{
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex32>.OfRowEnumerables(rows, columns, data));
}
/// <summary> /// <summary>
/// Create a new dense matrix and initialize each value using the provided init function. /// Create a new dense matrix and initialize each value using the provided init function.
/// </summary> /// </summary>

42
src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs

@ -157,6 +157,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return new SparseMatrix(SparseCompressedRowMatrixStorage<Complex32>.OfColumnEnumerables(rows, columns, data)); return new SparseMatrix(SparseCompressedRowMatrixStorage<Complex32>.OfColumnEnumerables(rows, columns, data));
} }
/// <summary>
/// Create a new sparse matrix as a copy of the given column arrays.
/// This new matrix will be independent from the arrays.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static SparseMatrix OfColumnArrays(params Complex32[][] columns)
{
return new SparseMatrix(SparseCompressedRowMatrixStorage<Complex32>.OfColumnArrays(columns));
}
/// <summary> /// <summary>
/// Create a new sparse matrix as a copy of the given column vectors. /// Create a new sparse matrix as a copy of the given column vectors.
/// This new matrix will be independent from the vectors. /// This new matrix will be independent from the vectors.
@ -173,27 +183,24 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
} }
/// <summary> /// <summary>
/// Create a new sparse matrix as a copy of the given enumerable of enumerable columns. /// Create a new sparse matrix as a copy of the given enumerable of enumerable rows.
/// Each enumerable in the master enumerable specifies a column. /// Each enumerable in the master enumerable specifies a row.
/// This new matrix will be independent from the enumerables. /// This new matrix will be independent from the enumerables.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
public static SparseMatrix OfColumnsCovariant<TColumn>(int rows, int columns, IEnumerable<TColumn> data) public static SparseMatrix OfRows(int rows, int columns, IEnumerable<IEnumerable<Complex32>> data)
// NOTE: flexible typing to 'backport' generic covariance.
where TColumn : IEnumerable<Complex32>
{ {
return new SparseMatrix(SparseCompressedRowMatrixStorage<Complex32>.OfColumnEnumerables(rows, columns, data)); return new SparseMatrix(SparseCompressedRowMatrixStorage<Complex32>.OfRowEnumerables(rows, columns, data));
} }
/// <summary> /// <summary>
/// Create a new sparse matrix as a copy of the given enumerable of enumerable rows. /// Create a new sparse matrix as a copy of the given row arrays.
/// Each enumerable in the master enumerable specifies a row. /// This new matrix will be independent from the arrays.
/// This new matrix will be independent from the enumerables.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
public static SparseMatrix OfRows(int rows, int columns, IEnumerable<IEnumerable<Complex32>> data) public static SparseMatrix OfRowArrays(params Complex32[][] rows)
{ {
return new SparseMatrix(SparseCompressedRowMatrixStorage<Complex32>.OfRowEnumerables(rows, columns, data)); return new SparseMatrix(SparseCompressedRowMatrixStorage<Complex32>.OfRowArrays(rows));
} }
/// <summary> /// <summary>
@ -211,19 +218,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return new SparseMatrix(SparseCompressedRowMatrixStorage<Complex32>.OfRowVectors(storage)); return new SparseMatrix(SparseCompressedRowMatrixStorage<Complex32>.OfRowVectors(storage));
} }
/// <summary>
/// Create a new sparse matrix as a copy of the given enumerable of enumerable rows.
/// Each enumerable in the master enumerable specifies a row.
/// This new matrix will be independent from the enumerables.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static SparseMatrix OfRowsCovariant<TRow>(int rows, int columns, IEnumerable<TRow> data)
// NOTE: flexible typing to 'backport' generic covariance.
where TRow : IEnumerable<Complex32>
{
return new SparseMatrix(SparseCompressedRowMatrixStorage<Complex32>.OfRowEnumerables(rows, columns, data));
}
/// <summary> /// <summary>
/// Create a new sparse matrix and initialize each value using the provided init function. /// Create a new sparse matrix and initialize each value using the provided init function.
/// </summary> /// </summary>

40
src/Numerics/LinearAlgebra/Double/DenseMatrix.cs

@ -166,6 +166,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return new DenseMatrix(DenseColumnMajorMatrixStorage<double>.OfColumnEnumerables(rows, columns, data)); return new DenseMatrix(DenseColumnMajorMatrixStorage<double>.OfColumnEnumerables(rows, columns, data));
} }
/// <summary>
/// Create a new dense matrix as a copy of the given column arrays.
/// This new matrix will be independent from the arrays.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static DenseMatrix OfColumnArrays(params double[][] columns)
{
return new DenseMatrix(DenseColumnMajorMatrixStorage<double>.OfColumnArrays(columns));
}
/// <summary> /// <summary>
/// Create a new dense matrix as a copy of the given column vectors. /// Create a new dense matrix as a copy of the given column vectors.
/// This new matrix will be independent from the vectors. /// This new matrix will be independent from the vectors.
@ -182,26 +192,24 @@ namespace MathNet.Numerics.LinearAlgebra.Double
} }
/// <summary> /// <summary>
/// Create a new dense matrix as a copy of the given enumerable of enumerable columns. /// Create a new dense matrix as a copy of the given enumerable of enumerable rows.
/// Each enumerable in the master enumerable specifies a column. /// Each enumerable in the master enumerable specifies a row.
/// This new matrix will be independent from the enumerables. /// This new matrix will be independent from the enumerables.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
public static DenseMatrix OfColumnsCovariant<TColumn>(int rows, int columns, IEnumerable<TColumn> data) public static DenseMatrix OfRows(int rows, int columns, IEnumerable<IEnumerable<double>> data)
where TColumn : IEnumerable<double>
{ {
return new DenseMatrix(DenseColumnMajorMatrixStorage<double>.OfColumnEnumerables(rows, columns, data)); return new DenseMatrix(DenseColumnMajorMatrixStorage<double>.OfRowEnumerables(rows, columns, data));
} }
/// <summary> /// <summary>
/// Create a new dense matrix as a copy of the given enumerable of enumerable rows. /// Create a new dense matrix as a copy of the given row arrays.
/// Each enumerable in the master enumerable specifies a row. /// This new matrix will be independent from the arrays.
/// This new matrix will be independent from the enumerables.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
public static DenseMatrix OfRows(int rows, int columns, IEnumerable<IEnumerable<double>> data) public static DenseMatrix OfRowArrays(params double[][] rows)
{ {
return new DenseMatrix(DenseColumnMajorMatrixStorage<double>.OfRowEnumerables(rows, columns, data)); return new DenseMatrix(DenseColumnMajorMatrixStorage<double>.OfRowArrays(rows));
} }
/// <summary> /// <summary>
@ -219,18 +227,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return new DenseMatrix(DenseColumnMajorMatrixStorage<double>.OfRowVectors(storage)); return new DenseMatrix(DenseColumnMajorMatrixStorage<double>.OfRowVectors(storage));
} }
/// <summary>
/// Create a new dense matrix as a copy of the given enumerable of enumerable rows.
/// Each enumerable in the master enumerable specifies a row.
/// This new matrix will be independent from the enumerables.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static DenseMatrix OfRowsCovariant<TRow>(int rows, int columns, IEnumerable<TRow> data)
where TRow : IEnumerable<double>
{
return new DenseMatrix(DenseColumnMajorMatrixStorage<double>.OfRowEnumerables(rows, columns, data));
}
/// <summary> /// <summary>
/// Create a new dense matrix and initialize each value using the provided init function. /// Create a new dense matrix and initialize each value using the provided init function.
/// </summary> /// </summary>

42
src/Numerics/LinearAlgebra/Double/SparseMatrix.cs

@ -155,6 +155,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return new SparseMatrix(SparseCompressedRowMatrixStorage<double>.OfColumnEnumerables(rows, columns, data)); return new SparseMatrix(SparseCompressedRowMatrixStorage<double>.OfColumnEnumerables(rows, columns, data));
} }
/// <summary>
/// Create a new sparse matrix as a copy of the given column arrays.
/// This new matrix will be independent from the arrays.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static SparseMatrix OfColumnArrays(params double[][] columns)
{
return new SparseMatrix(SparseCompressedRowMatrixStorage<double>.OfColumnArrays(columns));
}
/// <summary> /// <summary>
/// Create a new sparse matrix as a copy of the given column vectors. /// Create a new sparse matrix as a copy of the given column vectors.
/// This new matrix will be independent from the vectors. /// This new matrix will be independent from the vectors.
@ -171,27 +181,24 @@ namespace MathNet.Numerics.LinearAlgebra.Double
} }
/// <summary> /// <summary>
/// Create a new sparse matrix as a copy of the given enumerable of enumerable columns. /// Create a new sparse matrix as a copy of the given enumerable of enumerable rows.
/// Each enumerable in the master enumerable specifies a column. /// Each enumerable in the master enumerable specifies a row.
/// This new matrix will be independent from the enumerables. /// This new matrix will be independent from the enumerables.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
public static SparseMatrix OfColumnsCovariant<TColumn>(int rows, int columns, IEnumerable<TColumn> data) public static SparseMatrix OfRows(int rows, int columns, IEnumerable<IEnumerable<double>> data)
// NOTE: flexible typing to 'backport' generic covariance.
where TColumn : IEnumerable<double>
{ {
return new SparseMatrix(SparseCompressedRowMatrixStorage<double>.OfColumnEnumerables(rows, columns, data)); return new SparseMatrix(SparseCompressedRowMatrixStorage<double>.OfRowEnumerables(rows, columns, data));
} }
/// <summary> /// <summary>
/// Create a new sparse matrix as a copy of the given enumerable of enumerable rows. /// Create a new sparse matrix as a copy of the given row arrays.
/// Each enumerable in the master enumerable specifies a row. /// This new matrix will be independent from the arrays.
/// This new matrix will be independent from the enumerables.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
public static SparseMatrix OfRows(int rows, int columns, IEnumerable<IEnumerable<double>> data) public static SparseMatrix OfRowArrays(params double[][] rows)
{ {
return new SparseMatrix(SparseCompressedRowMatrixStorage<double>.OfRowEnumerables(rows, columns, data)); return new SparseMatrix(SparseCompressedRowMatrixStorage<double>.OfRowArrays(rows));
} }
/// <summary> /// <summary>
@ -209,19 +216,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return new SparseMatrix(SparseCompressedRowMatrixStorage<double>.OfRowVectors(storage)); return new SparseMatrix(SparseCompressedRowMatrixStorage<double>.OfRowVectors(storage));
} }
/// <summary>
/// Create a new sparse matrix as a copy of the given enumerable of enumerable rows.
/// Each enumerable in the master enumerable specifies a row.
/// This new matrix will be independent from the enumerables.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static SparseMatrix OfRowsCovariant<TRow>(int rows, int columns, IEnumerable<TRow> data)
// NOTE: flexible typing to 'backport' generic covariance.
where TRow : IEnumerable<double>
{
return new SparseMatrix(SparseCompressedRowMatrixStorage<double>.OfRowEnumerables(rows, columns, data));
}
/// <summary> /// <summary>
/// Create a new sparse matrix and initialize each value using the provided init function. /// Create a new sparse matrix and initialize each value using the provided init function.
/// </summary> /// </summary>

40
src/Numerics/LinearAlgebra/Single/DenseMatrix.cs

@ -166,6 +166,16 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return new DenseMatrix(DenseColumnMajorMatrixStorage<float>.OfColumnEnumerables(rows, columns, data)); return new DenseMatrix(DenseColumnMajorMatrixStorage<float>.OfColumnEnumerables(rows, columns, data));
} }
/// <summary>
/// Create a new dense matrix as a copy of the given column arrays.
/// This new matrix will be independent from the arrays.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static DenseMatrix OfColumnArrays(params float[][] columns)
{
return new DenseMatrix(DenseColumnMajorMatrixStorage<float>.OfColumnArrays(columns));
}
/// <summary> /// <summary>
/// Create a new dense matrix as a copy of the given column vectors. /// Create a new dense matrix as a copy of the given column vectors.
/// This new matrix will be independent from the vectors. /// This new matrix will be independent from the vectors.
@ -182,26 +192,24 @@ namespace MathNet.Numerics.LinearAlgebra.Single
} }
/// <summary> /// <summary>
/// Create a new dense matrix as a copy of the given enumerable of enumerable columns. /// Create a new dense matrix as a copy of the given enumerable of enumerable rows.
/// Each enumerable in the master enumerable specifies a column. /// Each enumerable in the master enumerable specifies a row.
/// This new matrix will be independent from the enumerables. /// This new matrix will be independent from the enumerables.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
public static DenseMatrix OfColumnsCovariant<TColumn>(int rows, int columns, IEnumerable<TColumn> data) public static DenseMatrix OfRows(int rows, int columns, IEnumerable<IEnumerable<float>> data)
where TColumn : IEnumerable<float>
{ {
return new DenseMatrix(DenseColumnMajorMatrixStorage<float>.OfColumnEnumerables(rows, columns, data)); return new DenseMatrix(DenseColumnMajorMatrixStorage<float>.OfRowEnumerables(rows, columns, data));
} }
/// <summary> /// <summary>
/// Create a new dense matrix as a copy of the given enumerable of enumerable rows. /// Create a new dense matrix as a copy of the given row arrays.
/// Each enumerable in the master enumerable specifies a row. /// This new matrix will be independent from the arrays.
/// This new matrix will be independent from the enumerables.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
public static DenseMatrix OfRows(int rows, int columns, IEnumerable<IEnumerable<float>> data) public static DenseMatrix OfRowArrays(params float[][] rows)
{ {
return new DenseMatrix(DenseColumnMajorMatrixStorage<float>.OfRowEnumerables(rows, columns, data)); return new DenseMatrix(DenseColumnMajorMatrixStorage<float>.OfRowArrays(rows));
} }
/// <summary> /// <summary>
@ -219,18 +227,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return new DenseMatrix(DenseColumnMajorMatrixStorage<float>.OfRowVectors(storage)); return new DenseMatrix(DenseColumnMajorMatrixStorage<float>.OfRowVectors(storage));
} }
/// <summary>
/// Create a new dense matrix as a copy of the given enumerable of enumerable rows.
/// Each enumerable in the master enumerable specifies a row.
/// This new matrix will be independent from the enumerables.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static DenseMatrix OfRowsCovariant<TRow>(int rows, int columns, IEnumerable<TRow> data)
where TRow : IEnumerable<float>
{
return new DenseMatrix(DenseColumnMajorMatrixStorage<float>.OfRowEnumerables(rows, columns, data));
}
/// <summary> /// <summary>
/// Create a new dense matrix and initialize each value using the provided init function. /// Create a new dense matrix and initialize each value using the provided init function.
/// </summary> /// </summary>

42
src/Numerics/LinearAlgebra/Single/SparseMatrix.cs

@ -155,6 +155,16 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return new SparseMatrix(SparseCompressedRowMatrixStorage<float>.OfColumnEnumerables(rows, columns, data)); return new SparseMatrix(SparseCompressedRowMatrixStorage<float>.OfColumnEnumerables(rows, columns, data));
} }
/// <summary>
/// Create a new sparse matrix as a copy of the given column arrays.
/// This new matrix will be independent from the arrays.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static SparseMatrix OfColumnArrays(params float[][] columns)
{
return new SparseMatrix(SparseCompressedRowMatrixStorage<float>.OfColumnArrays(columns));
}
/// <summary> /// <summary>
/// Create a new sparse matrix as a copy of the given column vectors. /// Create a new sparse matrix as a copy of the given column vectors.
/// This new matrix will be independent from the vectors. /// This new matrix will be independent from the vectors.
@ -171,27 +181,24 @@ namespace MathNet.Numerics.LinearAlgebra.Single
} }
/// <summary> /// <summary>
/// Create a new sparse matrix as a copy of the given enumerable of enumerable columns. /// Create a new sparse matrix as a copy of the given enumerable of enumerable rows.
/// Each enumerable in the master enumerable specifies a column. /// Each enumerable in the master enumerable specifies a row.
/// This new matrix will be independent from the enumerables. /// This new matrix will be independent from the enumerables.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
public static SparseMatrix OfColumnsCovariant<TColumn>(int rows, int columns, IEnumerable<TColumn> data) public static SparseMatrix OfRows(int rows, int columns, IEnumerable<IEnumerable<float>> data)
// NOTE: flexible typing to 'backport' generic covariance.
where TColumn : IEnumerable<float>
{ {
return new SparseMatrix(SparseCompressedRowMatrixStorage<float>.OfColumnEnumerables(rows, columns, data)); return new SparseMatrix(SparseCompressedRowMatrixStorage<float>.OfRowEnumerables(rows, columns, data));
} }
/// <summary> /// <summary>
/// Create a new sparse matrix as a copy of the given enumerable of enumerable rows. /// Create a new sparse matrix as a copy of the given row arrays.
/// Each enumerable in the master enumerable specifies a row. /// This new matrix will be independent from the arrays.
/// This new matrix will be independent from the enumerables.
/// A new memory block will be allocated for storing the matrix. /// A new memory block will be allocated for storing the matrix.
/// </summary> /// </summary>
public static SparseMatrix OfRows(int rows, int columns, IEnumerable<IEnumerable<float>> data) public static SparseMatrix OfRowArrays(params float[][] rows)
{ {
return new SparseMatrix(SparseCompressedRowMatrixStorage<float>.OfRowEnumerables(rows, columns, data)); return new SparseMatrix(SparseCompressedRowMatrixStorage<float>.OfRowArrays(rows));
} }
/// <summary> /// <summary>
@ -209,19 +216,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return new SparseMatrix(SparseCompressedRowMatrixStorage<float>.OfRowVectors(storage)); return new SparseMatrix(SparseCompressedRowMatrixStorage<float>.OfRowVectors(storage));
} }
/// <summary>
/// Create a new sparse matrix as a copy of the given enumerable of enumerable rows.
/// Each enumerable in the master enumerable specifies a row.
/// This new matrix will be independent from the enumerables.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static SparseMatrix OfRowsCovariant<TRow>(int rows, int columns, IEnumerable<TRow> data)
// NOTE: flexible typing to 'backport' generic covariance.
where TRow : IEnumerable<float>
{
return new SparseMatrix(SparseCompressedRowMatrixStorage<float>.OfRowEnumerables(rows, columns, data));
}
/// <summary> /// <summary>
/// Create a new sparse matrix and initialize each value using the provided init function. /// Create a new sparse matrix and initialize each value using the provided init function.
/// </summary> /// </summary>

51
src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs

@ -110,34 +110,62 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return storage; return storage;
} }
public static DenseColumnMajorMatrixStorage<T> OfArray(T[,] array) public static DenseColumnMajorMatrixStorage<T> OfInit(int rows, int columns, Func<int, int, T> init)
{ {
var storage = new DenseColumnMajorMatrixStorage<T>(array.GetLength(0), array.GetLength(1)); var storage = new DenseColumnMajorMatrixStorage<T>(rows, columns);
int index = 0; int index = 0;
for (var j = 0; j < storage.ColumnCount; j++) for (var j = 0; j < storage.ColumnCount; j++)
{ {
for (var i = 0; i < storage.RowCount; i++) for (var i = 0; i < storage.RowCount; i++)
{ {
storage.Data[index++] = array[i, j]; storage.Data[index++] = init(i, j);
} }
} }
return storage; return storage;
} }
public static DenseColumnMajorMatrixStorage<T> OfInit(int rows, int columns, Func<int, int, T> init) public static DenseColumnMajorMatrixStorage<T> OfArray(T[,] array)
{ {
var storage = new DenseColumnMajorMatrixStorage<T>(rows, columns); var storage = new DenseColumnMajorMatrixStorage<T>(array.GetLength(0), array.GetLength(1));
int index = 0; int index = 0;
for (var j = 0; j < storage.ColumnCount; j++) for (var j = 0; j < storage.ColumnCount; j++)
{ {
for (var i = 0; i < storage.RowCount; i++) for (var i = 0; i < storage.RowCount; i++)
{ {
storage.Data[index++] = init(i, j); storage.Data[index++] = array[i, j];
} }
} }
return storage; return storage;
} }
public static DenseColumnMajorMatrixStorage<T> OfColumnArrays(T[][] data)
{
int columns = data.Length;
int rows = data[0].Length;
var array = new T[rows * columns];
for (int j = 0; j < data.Length; j++)
{
Array.Copy(data[j], 0, array, j*rows, rows);
}
return new DenseColumnMajorMatrixStorage<T>(rows, columns, array);
}
public static DenseColumnMajorMatrixStorage<T> OfRowArrays(T[][] data)
{
int rows = data.Length;
int columns = data[0].Length;
var array = new T[rows * columns];
for (int j = 0; j < columns; j++)
{
int offset = j * rows;
for (int i = 0; i < rows; i++)
{
array[offset + i] = data[i][j];
}
}
return new DenseColumnMajorMatrixStorage<T>(rows, columns, array);
}
public static DenseColumnMajorMatrixStorage<T> OfColumnVectors(VectorStorage<T>[] data) public static DenseColumnMajorMatrixStorage<T> OfColumnVectors(VectorStorage<T>[] data)
{ {
int columns = data.Length; int columns = data.Length;
@ -200,13 +228,10 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return new DenseColumnMajorMatrixStorage<T>(rows, columns, copy); return new DenseColumnMajorMatrixStorage<T>(rows, columns, copy);
} }
var array = System.Linq.Enumerable.ToArray(data); return new DenseColumnMajorMatrixStorage<T>(rows, columns, data.ToArray());
return new DenseColumnMajorMatrixStorage<T>(rows, columns, array);
} }
public static DenseColumnMajorMatrixStorage<T> OfColumnEnumerables<TColumn>(int rows, int columns, IEnumerable<TColumn> data) public static DenseColumnMajorMatrixStorage<T> OfColumnEnumerables(int rows, int columns, IEnumerable<IEnumerable<T>> data)
// NOTE: flexible typing to 'backport' generic covariance.
where TColumn : IEnumerable<T>
{ {
var array = new T[rows*columns]; var array = new T[rows*columns];
using (var columnIterator = data.GetEnumerator()) using (var columnIterator = data.GetEnumerator())
@ -238,9 +263,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return new DenseColumnMajorMatrixStorage<T>(rows, columns, array); return new DenseColumnMajorMatrixStorage<T>(rows, columns, array);
} }
public static DenseColumnMajorMatrixStorage<T> OfRowEnumerables<TRow>(int rows, int columns, IEnumerable<TRow> data) public static DenseColumnMajorMatrixStorage<T> OfRowEnumerables(int rows, int columns, IEnumerable<IEnumerable<T>> data)
// NOTE: flexible typing to 'backport' generic covariance.
where TRow : IEnumerable<T>
{ {
var array = new T[rows*columns]; var array = new T[rows*columns];
using (var rowIterator = data.GetEnumerator()) using (var rowIterator = data.GetEnumerator())

76
src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs

@ -372,6 +372,33 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return storage; return storage;
} }
public static SparseCompressedRowMatrixStorage<T> OfInit(int rows, int columns, Func<int, int, T> init)
{
var storage = new SparseCompressedRowMatrixStorage<T>(rows, columns);
var rowPointers = storage.RowPointers;
var columnIndices = new List<int>();
var values = new List<T>();
for (int row = 0; row < rows; row++)
{
rowPointers[row] = values.Count;
for (int col = 0; col < columns; col++)
{
var item = init(row, col);
if (!Zero.Equals(item))
{
values.Add(item);
columnIndices.Add(col);
}
}
}
storage.ColumnIndices = columnIndices.ToArray();
storage.Values = values.ToArray();
storage.ValueCount = values.Count;
return storage;
}
public static SparseCompressedRowMatrixStorage<T> OfArray(T[,] array) public static SparseCompressedRowMatrixStorage<T> OfArray(T[,] array)
{ {
var storage = new SparseCompressedRowMatrixStorage<T>(array.GetLength(0), array.GetLength(1)); var storage = new SparseCompressedRowMatrixStorage<T>(array.GetLength(0), array.GetLength(1));
@ -398,22 +425,49 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return storage; return storage;
} }
public static SparseCompressedRowMatrixStorage<T> OfInit(int rows, int columns, Func<int, int, T> init) public static SparseCompressedRowMatrixStorage<T> OfRowArrays(T[][] data)
{ {
var storage = new SparseCompressedRowMatrixStorage<T>(rows, columns); var storage = new SparseCompressedRowMatrixStorage<T>(data.Length, data[0].Length);
var rowPointers = storage.RowPointers; var rowPointers = storage.RowPointers;
var columnIndices = new List<int>(); var columnIndices = new List<int>();
var values = new List<T>(); var values = new List<T>();
for (int row = 0; row < rows; row++) for (int row = 0; row < storage.RowCount; row++)
{ {
rowPointers[row] = values.Count; rowPointers[row] = values.Count;
for (int col = 0; col < columns; col++) for (int col = 0; col < storage.ColumnCount; col++)
{ {
var item = init(row, col); T x = data[row][col];
if (!Zero.Equals(item)) if (!Zero.Equals(x))
{ {
values.Add(item); values.Add(x);
columnIndices.Add(col);
}
}
}
storage.ColumnIndices = columnIndices.ToArray();
storage.Values = values.ToArray();
storage.ValueCount = values.Count;
return storage;
}
public static SparseCompressedRowMatrixStorage<T> OfColumnArrays(T[][] data)
{
var storage = new SparseCompressedRowMatrixStorage<T>(data[0].Length, data.Length);
var rowPointers = storage.RowPointers;
var columnIndices = new List<int>();
var values = new List<T>();
for (int row = 0; row < storage.RowCount; row++)
{
rowPointers[row] = values.Count;
for (int col = 0; col < storage.ColumnCount; col++)
{
T x = data[col][row];
if (!Zero.Equals(x))
{
values.Add(x);
columnIndices.Add(col); columnIndices.Add(col);
} }
} }
@ -522,9 +576,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return storage; return storage;
} }
public static SparseCompressedRowMatrixStorage<T> OfRowEnumerables<TRow>(int rows, int columns, IEnumerable<TRow> data) public static SparseCompressedRowMatrixStorage<T> OfRowEnumerables(int rows, int columns, IEnumerable<IEnumerable<T>> data)
// NOTE: flexible typing to 'backport' generic covariance.
where TRow : IEnumerable<T>
{ {
var storage = new SparseCompressedRowMatrixStorage<T>(rows, columns); var storage = new SparseCompressedRowMatrixStorage<T>(rows, columns);
var rowPointers = storage.RowPointers; var rowPointers = storage.RowPointers;
@ -559,9 +611,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return storage; return storage;
} }
public static SparseCompressedRowMatrixStorage<T> OfColumnEnumerables<TColumn>(int rows, int columns, IEnumerable<TColumn> data) public static SparseCompressedRowMatrixStorage<T> OfColumnEnumerables(int rows, int columns, IEnumerable<IEnumerable<T>> data)
// NOTE: flexible typing to 'backport' generic covariance.
where TColumn : IEnumerable<T>
{ {
var trows = new List<Tuple<int, T>>[rows]; var trows = new List<Tuple<int, T>>[rows];
using (var columnIterator = data.GetEnumerator()) using (var columnIterator = data.GetEnumerator())

Loading…
Cancel
Save