diff --git a/src/FSharp/Fit.fs b/src/FSharp/Fit.fs index 4497b7ad..686449a2 100644 --- a/src/FSharp/Fit.fs +++ b/src/FSharp/Fit.fs @@ -62,7 +62,7 @@ module Fit = let linear functions (x:_[]) (y:float[]) = functions |> 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() |> List.ofArray diff --git a/src/FSharp/LinearAlgebra.Double.Matrix.fs b/src/FSharp/LinearAlgebra.Double.Matrix.fs index 6ad192b4..55a35dc3 100644 --- a/src/FSharp/LinearAlgebra.Double.Matrix.fs +++ b/src/FSharp/LinearAlgebra.Double.Matrix.fs @@ -61,6 +61,7 @@ module Matrix = v.At(k, macc) v :> _ Vector + /// A module which implements functional dense vector operations. [] 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 /// 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) for i=0 to min (rows-1) (cols-1) do A.At(i,i,x) A :> _ Matrix @@ -86,8 +87,14 @@ module DenseMatrix = /// 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 + /// Initialize a matrix by calling a construction function for every row. + let inline initRows (rows: int) (f: int -> Vector) = 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) = DenseMatrix.OfColumnVectors(Array.init cols f) :> _ Matrix + /// 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) for i=0 to min (rows-1) (cols-1) do A.At(i,i,f i) A :> _ Matrix @@ -95,67 +102,54 @@ module DenseMatrix = /// Create an identity matrix with the given dimension. let inline identity (rows: int) (cols: int) = createDiag rows cols 1.0 - /// Create a matrix from a 2D array of floating point numbers. 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>) = - 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. - let inline ofRows (vectors: #Vector list) = DenseMatrix.OfRowVectors(vectors |> Array.ofList |> box |> unbox) :> _ Matrix + let inline ofRows (rows: Vector 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. - 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>) = 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. - let inline ofRowSeq (rows: int) (cols: int) (fss: #seq<#seq>) = DenseMatrix.OfRowsCovariant(rows, cols, fss) :> _ Matrix + let inline ofRowSeq2 (rows: int) (cols: int) (seqOfRows: #seq>) = DenseMatrix.OfRows(rows, cols, seqOfRows) :> _ Matrix /// Create a matrix from a list of column vectors. - let inline ofColumns (vectors: #Vector list) = DenseMatrix.OfColumnVectors(vectors |> Array.ofList |> box |> unbox) :> _ Matrix + let inline ofColumns (columns: Vector 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. - 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>) = 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. - let inline ofColumnSeq (rows: int) (cols: int) (fss: #seq<#seq>) = DenseMatrix.OfColumnsCovariant(rows, cols, fss) :> _ Matrix + let inline ofColumnSeq2 (rows: int) (cols: int) (seqOfCols: #seq>) = DenseMatrix.OfColumns(rows, cols, seqOfCols) :> _ Matrix /// 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) = DenseMatrix.OfIndexed(rows, cols, Seq.ofList fl) :> _ Matrix + let inline ofListi (rows: int) (cols: int) (indexed: list) = DenseMatrix.OfIndexed(rows, cols, Seq.ofList indexed) :> _ Matrix /// 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) = DenseMatrix.OfIndexed(rows, cols, fs) :> _ Matrix + let inline ofSeqi (rows: int) (cols: int) (indexed: #seq) = DenseMatrix.OfIndexed(rows, cols, indexed) :> _ Matrix - /// Create a square matrix with the vector elements on the diagonal. - let inline ofDiag (v: #Vector) = - let n = v.Count - let A = DenseMatrix(n,n) + /// Create a matrix with the vector elements on the diagonal. + let ofDiag2 (rows: int) (cols: int) (v: Vector) = + let A = DenseMatrix(rows,cols) A.SetDiagonal(v) A :> _ Matrix - /// Initialize a matrix by calling a construction function for every row. - let inline initRow (rows: int) (cols: int) (f: int -> #Vector) = - let A = DenseMatrix(rows,cols) - for i=0 to rows-1 do A.SetRow(i, f i) - A :> _ Matrix + /// Create a square matrix with the vector elements on the diagonal. + let inline ofDiag (v: Vector) = ofDiag2 v.Count v.Count v - /// Initialize a matrix by calling a construction function for every column. - let inline initCol (rows: int) (cols: int) (f: int -> #Vector) = - 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. [] @@ -165,7 +159,7 @@ module SparseMatrix = 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. - let inline createDiag (rows: int) (cols: int) x = + let createDiag (rows: int) (cols: int) x = let A = SparseMatrix(rows,cols) for i=0 to min (rows-1) (cols-1) do A.At(i,i,x) A :> _ Matrix @@ -173,8 +167,14 @@ module SparseMatrix = /// 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 + /// Initialize a matrix by calling a construction function for every row. + let inline initRows (rows: int) (f: int -> Vector) = 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) = SparseMatrix.OfColumnVectors(Array.init cols f) :> _ Matrix + /// 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) for i=0 to min (rows-1) (cols-1) do A.At(i,i,f i) A :> _ Matrix @@ -182,58 +182,50 @@ module SparseMatrix = /// Create an identity matrix with the given dimension. let inline identity (rows: int) (cols: int) = createDiag rows cols 1.0 - /// Create a matrix from a 2D array of floating point numbers. let inline ofArray2 array = SparseMatrix.OfArray(array) :> _ Matrix - /// Create a matrix with a given dimension from an indexed sequences of row, column, value tuples. - [] - let inline ofSeq (rows: int) (cols: int) (fs: #seq) = SparseMatrix.OfIndexed(rows, cols, fs) :> _ Matrix - - /// Create a matrix with a given dimension from an indexed list of row, column, value tuples. - [] - let inline ofList (rows: int) (cols: int) (fl: list) = SparseMatrix.OfIndexed(rows, cols, Seq.ofList fl) :> _ Matrix - - /// Create a matrix from a list of row vectors. - let inline ofRows (vectors: #Vector list) = SparseMatrix.OfRowVectors(vectors |> Array.ofList |> box |> unbox) :> _ Matrix + let inline ofRows (rows: Vector 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. - 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>) = 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. - let inline ofRowSeq (rows: int) (cols: int) (fss: #seq<#seq>) = SparseMatrix.OfRowsCovariant(rows, cols, fss) :> _ Matrix + let inline ofRowSeq2 (rows: int) (cols: int) (seqOfRows: #seq>) = SparseMatrix.OfRows(rows, cols, seqOfRows) :> _ Matrix /// Create a matrix from a list of column vectors. - let inline ofColumns (vectors: #Vector list) = SparseMatrix.OfColumnVectors(vectors |> Array.ofList |> box |> unbox) :> _ Matrix + let inline ofColumns (columns: Vector 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. - 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>) = 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. - let inline ofColumnSeq (rows: int) (cols: int) (fss: #seq<#seq>) = SparseMatrix.OfColumnsCovariant(rows, cols, fss) :> _ Matrix + let inline ofColumnSeq2 (rows: int) (cols: int) (seqOfCols: #seq>) = SparseMatrix.OfColumns(rows, cols, seqOfCols) :> _ Matrix /// 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) = SparseMatrix.OfIndexed(rows, cols, Seq.ofList fl) :> _ Matrix + let inline ofListi (rows: int) (cols: int) (indexed: list) = SparseMatrix.OfIndexed(rows, cols, Seq.ofList indexed) :> _ Matrix /// 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) = SparseMatrix.OfIndexed(rows, cols, fs) :> _ Matrix - - /// Create a square matrix with the vector elements on the diagonal. - let inline ofDiag (v: #Vector) = - let n = v.Count - let A = SparseMatrix(n,n) - A.SetDiagonal(v) - A :> _ Matrix + let inline ofSeqi (rows: int) (cols: int) (indexed: #seq) = SparseMatrix.OfIndexed(rows, cols, indexed) :> _ Matrix - /// Initialize a matrix by calling a construction function for every row. - let inline initRow (rows: int) (cols: int) (f: int -> #Vector) = + /// Create a matrix with the vector elements on the diagonal. + let ofDiag2 (rows: int) (cols: int) (v: Vector) = let A = SparseMatrix(rows,cols) - for i=0 to rows-1 do A.SetRow(i, f i) + A.SetDiagonal(v) A :> _ Matrix - /// Initialize a matrix by calling a construction function for every column. - let inline initCol (rows: int) (cols: int) (f: int -> #Vector) = - let A = SparseMatrix(rows,cols) - for i=0 to cols-1 do A.SetColumn(i, f i) - A :> _ Matrix + /// Create a square matrix with the vector elements on the diagonal. + let inline ofDiag (v: Vector) = ofDiag2 v.Count v.Count v diff --git a/src/FSharp/LinearAlgebra.Double.Vector.fs b/src/FSharp/LinearAlgebra.Double.Vector.fs index 202c4b8c..e7f61568 100644 --- a/src/FSharp/LinearAlgebra.Double.Vector.fs +++ b/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 /// 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. - let inline ofSeq (n: int) (fs: #seq) = SparseVector.OfEnumerable(fs) :> _ Vector + let inline ofSeq (fs: #seq) = SparseVector.OfEnumerable(fs) :> _ Vector /// Create a sparse vector with a given dimension from an indexed list of index, value pairs. let inline ofListi (n: int) (fl: list) = SparseVector.OfIndexedEnumerable(n, Seq.ofList fl) :> _ Vector diff --git a/src/FSharp/LinearAlgebra.Double.fs b/src/FSharp/LinearAlgebra.Double.fs index 5f3adb33..827d2821 100644 --- a/src/FSharp/LinearAlgebra.Double.fs +++ b/src/FSharp/LinearAlgebra.Double.fs @@ -36,8 +36,8 @@ open MathNet.Numerics.LinearAlgebra [] module Utility = - /// Construct a dense matrix from a list of floating point numbers. - let inline matrix (lst: list>) = DenseMatrix.ofList lst + /// Construct a dense matrix from a nested list of floating point numbers. + let inline matrix (lst: list>) = DenseMatrix.ofRowList lst /// Construct a dense vector from a list of floating point numbers. let inline vector (lst: list) = DenseVector.ofList lst diff --git a/src/FSharpExamples/Matrices.fsx b/src/FSharpExamples/Matrices.fsx index 8dec98b1..883f90ff 100644 --- a/src/FSharpExamples/Matrices.fsx +++ b/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) // 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 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 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 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 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 (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 (seq { for j in 1 .. 10 do yield Array.init 20 (fun i -> float j + 100.0 * float i) }) +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 -let f1 = DenseMatrix.ofRowList 20 10 [ 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 f3 = DenseMatrix.ofColumnList 20 10 [ 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 f1 = DenseMatrix.ofRowList [ for i in 1 .. 20 -> List.init 10 (fun j -> float j + 100.0 * float i) ] +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 [ for j in 1 .. 10 -> List.init 20 (fun i -> float j + 100.0 * float i) ] +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 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] +// 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) 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)] diff --git a/src/FSharpUnitTests/DenseMatrixTests.fs b/src/FSharpUnitTests/DenseMatrixTests.fs index f27a5f33..1964b6ec 100644 --- a/src/FSharpUnitTests/DenseMatrixTests.fs +++ b/src/FSharpUnitTests/DenseMatrixTests.fs @@ -44,21 +44,13 @@ module DenseMatrixTests = 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 - [] - let ``DenseMatrix.ofSeq`` () = - DenseMatrix.ofSeq (Seq.ofList [[0.3;0.3];[0.3;0.3];[0.3;0.3]]) |> should equal smallM - - [] - let ``DenseMatrix.ofList`` () = - DenseMatrix.ofList [[0.3;0.3];[0.3;0.3];[0.3;0.3]] |> should equal smallM - [] 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 [] 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 [] let ``DenseMatrix.ofRows`` () = @@ -66,11 +58,11 @@ module DenseMatrixTests = [] 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 [] 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 [] let ``DenseMatrix.ofColumns`` () = @@ -96,8 +88,8 @@ module DenseMatrixTests = [] 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 [] 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 diff --git a/src/FSharpUnitTests/SparseMatrixTests.fs b/src/FSharpUnitTests/SparseMatrixTests.fs index 86e0fb79..eb6b48b0 100644 --- a/src/FSharpUnitTests/SparseMatrixTests.fs +++ b/src/FSharpUnitTests/SparseMatrixTests.fs @@ -26,19 +26,19 @@ module SparseMatrixTests = [] 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 [] 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 [] 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 [] 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 [] let ``SparseMatrix.ofSeqi`` () = @@ -53,13 +53,13 @@ module SparseMatrixTests = SparseMatrix.createDiag 100 100 2.0 |> should equal (2.0 * (SparseMatrix.Identity 100)) [] - let ``SparseMatrix.diag`` () = + let ``SparseMatrix.ofDiag`` () = SparseMatrix.ofDiag (DenseVector.Create(100, fun i -> 2.0)) |> should equal (2.0 * (SparseMatrix.Identity 100)) [] 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 [] 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 diff --git a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs index 4fc9e4ec..34bca505 100644 --- a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs @@ -173,6 +173,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnEnumerables(rows, columns, data)); } + /// + /// 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. + /// + public static DenseMatrix OfColumnArrays(params Complex[][] columns) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnArrays(columns)); + } + /// /// Create a new dense matrix as a copy of the given column vectors. /// This new matrix will be independent from the vectors. @@ -189,26 +199,24 @@ namespace MathNet.Numerics.LinearAlgebra.Complex } /// - /// Create a new dense matrix as a copy of the given enumerable of enumerable columns. - /// Each enumerable in the master enumerable specifies a column. + /// 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. /// - public static DenseMatrix OfColumnsCovariant(int rows, int columns, IEnumerable data) - where TColumn : IEnumerable + public static DenseMatrix OfRows(int rows, int columns, IEnumerable> data) { - return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnEnumerables(rows, columns, data)); + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowEnumerables(rows, columns, data)); } /// - /// 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. + /// Create a new dense matrix as a copy of the given row arrays. + /// This new matrix will be independent from the arrays. /// A new memory block will be allocated for storing the matrix. /// - public static DenseMatrix OfRows(int rows, int columns, IEnumerable> data) + public static DenseMatrix OfRowArrays(params Complex[][] rows) { - return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowEnumerables(rows, columns, data)); + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowArrays(rows)); } /// @@ -226,18 +234,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowVectors(storage)); } - /// - /// 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. - /// - public static DenseMatrix OfRowsCovariant(int rows, int columns, IEnumerable data) - where TRow : IEnumerable - { - return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowEnumerables(rows, columns, data)); - } - /// /// Create a new dense matrix and initialize each value using the provided init function. /// diff --git a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs index fc316714..0294873f 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs @@ -162,6 +162,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnEnumerables(rows, columns, data)); } + /// + /// 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. + /// + public static SparseMatrix OfColumnArrays(params Complex[][] columns) + { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnArrays(columns)); + } + /// /// Create a new sparse matrix as a copy of the given column vectors. /// This new matrix will be independent from the vectors. @@ -178,27 +188,24 @@ namespace MathNet.Numerics.LinearAlgebra.Complex } /// - /// Create a new sparse matrix as a copy of the given enumerable of enumerable columns. - /// Each enumerable in the master enumerable specifies a column. + /// 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. /// - public static SparseMatrix OfColumnsCovariant(int rows, int columns, IEnumerable data) - // NOTE: flexible typing to 'backport' generic covariance. - where TColumn : IEnumerable + public static SparseMatrix OfRows(int rows, int columns, IEnumerable> data) { - return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnEnumerables(rows, columns, data)); + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowEnumerables(rows, columns, data)); } /// - /// 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. + /// Create a new sparse matrix as a copy of the given row arrays. + /// This new matrix will be independent from the arrays. /// A new memory block will be allocated for storing the matrix. /// - public static SparseMatrix OfRows(int rows, int columns, IEnumerable> data) + public static SparseMatrix OfRowArrays(params Complex[][] rows) { - return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowEnumerables(rows, columns, data)); + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowArrays(rows)); } /// @@ -216,19 +223,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowVectors(storage)); } - /// - /// 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. - /// - public static SparseMatrix OfRowsCovariant(int rows, int columns, IEnumerable data) - // NOTE: flexible typing to 'backport' generic covariance. - where TRow : IEnumerable - { - return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowEnumerables(rows, columns, data)); - } - /// /// Create a new sparse matrix and initialize each value using the provided init function. /// diff --git a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs index 540964a5..76f912e0 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs @@ -168,6 +168,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnEnumerables(rows, columns, data)); } + /// + /// 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. + /// + public static DenseMatrix OfColumnArrays(params Complex32[][] columns) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnArrays(columns)); + } + /// /// Create a new dense matrix as a copy of the given column vectors. /// This new matrix will be independent from the vectors. @@ -184,26 +194,24 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } /// - /// Create a new dense matrix as a copy of the given enumerable of enumerable columns. - /// Each enumerable in the master enumerable specifies a column. + /// 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. /// - public static DenseMatrix OfColumnsCovariant(int rows, int columns, IEnumerable data) - where TColumn : IEnumerable + public static DenseMatrix OfRows(int rows, int columns, IEnumerable> data) { - return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnEnumerables(rows, columns, data)); + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowEnumerables(rows, columns, data)); } /// - /// 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. + /// Create a new dense matrix as a copy of the given row arrays. + /// This new matrix will be independent from the arrays. /// A new memory block will be allocated for storing the matrix. /// - public static DenseMatrix OfRows(int rows, int columns, IEnumerable> data) + public static DenseMatrix OfRowArrays(params Complex32[][] rows) { - return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowEnumerables(rows, columns, data)); + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowArrays(rows)); } /// @@ -221,18 +229,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowVectors(storage)); } - /// - /// 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. - /// - public static DenseMatrix OfRowsCovariant(int rows, int columns, IEnumerable data) - where TRow : IEnumerable - { - return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowEnumerables(rows, columns, data)); - } - /// /// Create a new dense matrix and initialize each value using the provided init function. /// diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs index 9a62af35..be1eafe9 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs @@ -157,6 +157,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnEnumerables(rows, columns, data)); } + /// + /// 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. + /// + public static SparseMatrix OfColumnArrays(params Complex32[][] columns) + { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnArrays(columns)); + } + /// /// Create a new sparse matrix as a copy of the given column vectors. /// This new matrix will be independent from the vectors. @@ -173,27 +183,24 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } /// - /// Create a new sparse matrix as a copy of the given enumerable of enumerable columns. - /// Each enumerable in the master enumerable specifies a column. + /// 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. /// - public static SparseMatrix OfColumnsCovariant(int rows, int columns, IEnumerable data) - // NOTE: flexible typing to 'backport' generic covariance. - where TColumn : IEnumerable + public static SparseMatrix OfRows(int rows, int columns, IEnumerable> data) { - return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnEnumerables(rows, columns, data)); + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowEnumerables(rows, columns, data)); } /// - /// 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. + /// Create a new sparse matrix as a copy of the given row arrays. + /// This new matrix will be independent from the arrays. /// A new memory block will be allocated for storing the matrix. /// - public static SparseMatrix OfRows(int rows, int columns, IEnumerable> data) + public static SparseMatrix OfRowArrays(params Complex32[][] rows) { - return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowEnumerables(rows, columns, data)); + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowArrays(rows)); } /// @@ -211,19 +218,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowVectors(storage)); } - /// - /// 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. - /// - public static SparseMatrix OfRowsCovariant(int rows, int columns, IEnumerable data) - // NOTE: flexible typing to 'backport' generic covariance. - where TRow : IEnumerable - { - return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowEnumerables(rows, columns, data)); - } - /// /// Create a new sparse matrix and initialize each value using the provided init function. /// diff --git a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs index 4fe6c565..f5ed2cdc 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs @@ -166,6 +166,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnEnumerables(rows, columns, data)); } + /// + /// 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. + /// + public static DenseMatrix OfColumnArrays(params double[][] columns) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnArrays(columns)); + } + /// /// Create a new dense matrix as a copy of the given column vectors. /// This new matrix will be independent from the vectors. @@ -182,26 +192,24 @@ namespace MathNet.Numerics.LinearAlgebra.Double } /// - /// Create a new dense matrix as a copy of the given enumerable of enumerable columns. - /// Each enumerable in the master enumerable specifies a column. + /// 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. /// - public static DenseMatrix OfColumnsCovariant(int rows, int columns, IEnumerable data) - where TColumn : IEnumerable + public static DenseMatrix OfRows(int rows, int columns, IEnumerable> data) { - return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnEnumerables(rows, columns, data)); + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowEnumerables(rows, columns, data)); } /// - /// 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. + /// Create a new dense matrix as a copy of the given row arrays. + /// This new matrix will be independent from the arrays. /// A new memory block will be allocated for storing the matrix. /// - public static DenseMatrix OfRows(int rows, int columns, IEnumerable> data) + public static DenseMatrix OfRowArrays(params double[][] rows) { - return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowEnumerables(rows, columns, data)); + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowArrays(rows)); } /// @@ -219,18 +227,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowVectors(storage)); } - /// - /// 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. - /// - public static DenseMatrix OfRowsCovariant(int rows, int columns, IEnumerable data) - where TRow : IEnumerable - { - return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowEnumerables(rows, columns, data)); - } - /// /// Create a new dense matrix and initialize each value using the provided init function. /// diff --git a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs index 5e5a703e..e16865ee 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs @@ -155,6 +155,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnEnumerables(rows, columns, data)); } + /// + /// 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. + /// + public static SparseMatrix OfColumnArrays(params double[][] columns) + { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnArrays(columns)); + } + /// /// Create a new sparse matrix as a copy of the given column vectors. /// This new matrix will be independent from the vectors. @@ -171,27 +181,24 @@ namespace MathNet.Numerics.LinearAlgebra.Double } /// - /// Create a new sparse matrix as a copy of the given enumerable of enumerable columns. - /// Each enumerable in the master enumerable specifies a column. + /// 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. /// - public static SparseMatrix OfColumnsCovariant(int rows, int columns, IEnumerable data) - // NOTE: flexible typing to 'backport' generic covariance. - where TColumn : IEnumerable + public static SparseMatrix OfRows(int rows, int columns, IEnumerable> data) { - return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnEnumerables(rows, columns, data)); + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowEnumerables(rows, columns, data)); } /// - /// 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. + /// Create a new sparse matrix as a copy of the given row arrays. + /// This new matrix will be independent from the arrays. /// A new memory block will be allocated for storing the matrix. /// - public static SparseMatrix OfRows(int rows, int columns, IEnumerable> data) + public static SparseMatrix OfRowArrays(params double[][] rows) { - return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowEnumerables(rows, columns, data)); + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowArrays(rows)); } /// @@ -209,19 +216,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowVectors(storage)); } - /// - /// 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. - /// - public static SparseMatrix OfRowsCovariant(int rows, int columns, IEnumerable data) - // NOTE: flexible typing to 'backport' generic covariance. - where TRow : IEnumerable - { - return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowEnumerables(rows, columns, data)); - } - /// /// Create a new sparse matrix and initialize each value using the provided init function. /// diff --git a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs index 179a8954..209c6778 100644 --- a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs @@ -166,6 +166,16 @@ namespace MathNet.Numerics.LinearAlgebra.Single return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnEnumerables(rows, columns, data)); } + /// + /// 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. + /// + public static DenseMatrix OfColumnArrays(params float[][] columns) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnArrays(columns)); + } + /// /// Create a new dense matrix as a copy of the given column vectors. /// This new matrix will be independent from the vectors. @@ -182,26 +192,24 @@ namespace MathNet.Numerics.LinearAlgebra.Single } /// - /// Create a new dense matrix as a copy of the given enumerable of enumerable columns. - /// Each enumerable in the master enumerable specifies a column. + /// 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. /// - public static DenseMatrix OfColumnsCovariant(int rows, int columns, IEnumerable data) - where TColumn : IEnumerable + public static DenseMatrix OfRows(int rows, int columns, IEnumerable> data) { - return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnEnumerables(rows, columns, data)); + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowEnumerables(rows, columns, data)); } /// - /// 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. + /// Create a new dense matrix as a copy of the given row arrays. + /// This new matrix will be independent from the arrays. /// A new memory block will be allocated for storing the matrix. /// - public static DenseMatrix OfRows(int rows, int columns, IEnumerable> data) + public static DenseMatrix OfRowArrays(params float[][] rows) { - return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowEnumerables(rows, columns, data)); + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowArrays(rows)); } /// @@ -219,18 +227,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowVectors(storage)); } - /// - /// 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. - /// - public static DenseMatrix OfRowsCovariant(int rows, int columns, IEnumerable data) - where TRow : IEnumerable - { - return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowEnumerables(rows, columns, data)); - } - /// /// Create a new dense matrix and initialize each value using the provided init function. /// diff --git a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs index 0ce2ae3c..ce426e54 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs @@ -155,6 +155,16 @@ namespace MathNet.Numerics.LinearAlgebra.Single return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnEnumerables(rows, columns, data)); } + /// + /// 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. + /// + public static SparseMatrix OfColumnArrays(params float[][] columns) + { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnArrays(columns)); + } + /// /// Create a new sparse matrix as a copy of the given column vectors. /// This new matrix will be independent from the vectors. @@ -171,27 +181,24 @@ namespace MathNet.Numerics.LinearAlgebra.Single } /// - /// Create a new sparse matrix as a copy of the given enumerable of enumerable columns. - /// Each enumerable in the master enumerable specifies a column. + /// 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. /// - public static SparseMatrix OfColumnsCovariant(int rows, int columns, IEnumerable data) - // NOTE: flexible typing to 'backport' generic covariance. - where TColumn : IEnumerable + public static SparseMatrix OfRows(int rows, int columns, IEnumerable> data) { - return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnEnumerables(rows, columns, data)); + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowEnumerables(rows, columns, data)); } /// - /// 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. + /// Create a new sparse matrix as a copy of the given row arrays. + /// This new matrix will be independent from the arrays. /// A new memory block will be allocated for storing the matrix. /// - public static SparseMatrix OfRows(int rows, int columns, IEnumerable> data) + public static SparseMatrix OfRowArrays(params float[][] rows) { - return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowEnumerables(rows, columns, data)); + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowArrays(rows)); } /// @@ -209,19 +216,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowVectors(storage)); } - /// - /// 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. - /// - public static SparseMatrix OfRowsCovariant(int rows, int columns, IEnumerable data) - // NOTE: flexible typing to 'backport' generic covariance. - where TRow : IEnumerable - { - return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowEnumerables(rows, columns, data)); - } - /// /// Create a new sparse matrix and initialize each value using the provided init function. /// diff --git a/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs index d9cb7b1d..ce4f0dd5 100644 --- a/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs @@ -110,34 +110,62 @@ namespace MathNet.Numerics.LinearAlgebra.Storage return storage; } - public static DenseColumnMajorMatrixStorage OfArray(T[,] array) + public static DenseColumnMajorMatrixStorage OfInit(int rows, int columns, Func init) { - var storage = new DenseColumnMajorMatrixStorage(array.GetLength(0), array.GetLength(1)); + var storage = new DenseColumnMajorMatrixStorage(rows, columns); int index = 0; for (var j = 0; j < storage.ColumnCount; j++) { for (var i = 0; i < storage.RowCount; i++) { - storage.Data[index++] = array[i, j]; + storage.Data[index++] = init(i, j); } } return storage; } - public static DenseColumnMajorMatrixStorage OfInit(int rows, int columns, Func init) + public static DenseColumnMajorMatrixStorage OfArray(T[,] array) { - var storage = new DenseColumnMajorMatrixStorage(rows, columns); + var storage = new DenseColumnMajorMatrixStorage(array.GetLength(0), array.GetLength(1)); int index = 0; for (var j = 0; j < storage.ColumnCount; j++) { for (var i = 0; i < storage.RowCount; i++) { - storage.Data[index++] = init(i, j); + storage.Data[index++] = array[i, j]; } } return storage; } + public static DenseColumnMajorMatrixStorage 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(rows, columns, array); + } + + public static DenseColumnMajorMatrixStorage 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(rows, columns, array); + } + public static DenseColumnMajorMatrixStorage OfColumnVectors(VectorStorage[] data) { int columns = data.Length; @@ -200,13 +228,10 @@ namespace MathNet.Numerics.LinearAlgebra.Storage return new DenseColumnMajorMatrixStorage(rows, columns, copy); } - var array = System.Linq.Enumerable.ToArray(data); - return new DenseColumnMajorMatrixStorage(rows, columns, array); + return new DenseColumnMajorMatrixStorage(rows, columns, data.ToArray()); } - public static DenseColumnMajorMatrixStorage OfColumnEnumerables(int rows, int columns, IEnumerable data) - // NOTE: flexible typing to 'backport' generic covariance. - where TColumn : IEnumerable + public static DenseColumnMajorMatrixStorage OfColumnEnumerables(int rows, int columns, IEnumerable> data) { var array = new T[rows*columns]; using (var columnIterator = data.GetEnumerator()) @@ -238,9 +263,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage return new DenseColumnMajorMatrixStorage(rows, columns, array); } - public static DenseColumnMajorMatrixStorage OfRowEnumerables(int rows, int columns, IEnumerable data) - // NOTE: flexible typing to 'backport' generic covariance. - where TRow : IEnumerable + public static DenseColumnMajorMatrixStorage OfRowEnumerables(int rows, int columns, IEnumerable> data) { var array = new T[rows*columns]; using (var rowIterator = data.GetEnumerator()) diff --git a/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs index 7dfed791..9ea86671 100644 --- a/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs @@ -372,6 +372,33 @@ namespace MathNet.Numerics.LinearAlgebra.Storage return storage; } + public static SparseCompressedRowMatrixStorage OfInit(int rows, int columns, Func init) + { + var storage = new SparseCompressedRowMatrixStorage(rows, columns); + var rowPointers = storage.RowPointers; + var columnIndices = new List(); + var values = new List(); + + 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 OfArray(T[,] array) { var storage = new SparseCompressedRowMatrixStorage(array.GetLength(0), array.GetLength(1)); @@ -398,22 +425,49 @@ namespace MathNet.Numerics.LinearAlgebra.Storage return storage; } - public static SparseCompressedRowMatrixStorage OfInit(int rows, int columns, Func init) + public static SparseCompressedRowMatrixStorage OfRowArrays(T[][] data) { - var storage = new SparseCompressedRowMatrixStorage(rows, columns); + var storage = new SparseCompressedRowMatrixStorage(data.Length, data[0].Length); var rowPointers = storage.RowPointers; var columnIndices = new List(); var values = new List(); - for (int row = 0; row < rows; row++) + for (int row = 0; row < storage.RowCount; row++) { rowPointers[row] = values.Count; - for (int col = 0; col < columns; col++) + for (int col = 0; col < storage.ColumnCount; col++) { - var item = init(row, col); - if (!Zero.Equals(item)) + T x = data[row][col]; + 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 OfColumnArrays(T[][] data) + { + var storage = new SparseCompressedRowMatrixStorage(data[0].Length, data.Length); + var rowPointers = storage.RowPointers; + var columnIndices = new List(); + var values = new List(); + + 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); } } @@ -522,9 +576,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage return storage; } - public static SparseCompressedRowMatrixStorage OfRowEnumerables(int rows, int columns, IEnumerable data) - // NOTE: flexible typing to 'backport' generic covariance. - where TRow : IEnumerable + public static SparseCompressedRowMatrixStorage OfRowEnumerables(int rows, int columns, IEnumerable> data) { var storage = new SparseCompressedRowMatrixStorage(rows, columns); var rowPointers = storage.RowPointers; @@ -559,9 +611,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage return storage; } - public static SparseCompressedRowMatrixStorage OfColumnEnumerables(int rows, int columns, IEnumerable data) - // NOTE: flexible typing to 'backport' generic covariance. - where TColumn : IEnumerable + public static SparseCompressedRowMatrixStorage OfColumnEnumerables(int rows, int columns, IEnumerable> data) { var trows = new List>[rows]; using (var columnIterator = data.GetEnumerator())