From a5503f85639f397fd78fc2a7de73a622130a45b1 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Fri, 27 Sep 2013 20:31:30 +0200 Subject: [PATCH] LA: create of column/row arrays/vectors IEnumerable overload #139; F# workaround no longer needed --- src/FSharp/LinearAlgebra.Matrix.fs | 21 +---- src/FSharp/LinearAlgebra.Vector.fs | 2 +- src/Numerics/LinearAlgebra/Builder.cs | 80 +++++++++++++++++++ .../LinearAlgebra/Complex/DenseMatrix.cs | 40 ++++++++++ .../LinearAlgebra/Complex/SparseMatrix.cs | 40 ++++++++++ .../LinearAlgebra/Complex32/DenseMatrix.cs | 40 ++++++++++ .../LinearAlgebra/Complex32/SparseMatrix.cs | 40 ++++++++++ .../LinearAlgebra/Double/DenseMatrix.cs | 40 ++++++++++ .../LinearAlgebra/Double/SparseMatrix.cs | 40 ++++++++++ .../LinearAlgebra/Single/DenseMatrix.cs | 40 ++++++++++ .../LinearAlgebra/Single/SparseMatrix.cs | 40 ++++++++++ 11 files changed, 405 insertions(+), 18 deletions(-) diff --git a/src/FSharp/LinearAlgebra.Matrix.fs b/src/FSharp/LinearAlgebra.Matrix.fs index d5cd2437..cd778a7b 100644 --- a/src/FSharp/LinearAlgebra.Matrix.fs +++ b/src/FSharp/LinearAlgebra.Matrix.fs @@ -346,19 +346,6 @@ module Matrix = -// Workaround an issue when passing generic arguments to a params-array. Get rid of this once we're on F# > 3.1. -type internal ParamsInvokeWorkaround<'T when 'T : (new: unit -> 'T) and 'T: struct and 'T :> ValueType and 'T :> IEquatable<'T> and 'T :> IFormattable> private ()= - static let build = Matrix<'T>.Build - static let dr = Delegate.CreateDelegate(typeof>>, build, typeof>.GetMethod("DenseMatrixOfRowArrays")) :?> Func<'T[][],Matrix<'T>> - static let dc = Delegate.CreateDelegate(typeof>>, build, typeof>.GetMethod("DenseMatrixOfColumnArrays")) :?> Func<'T[][],Matrix<'T>> - static let sr = Delegate.CreateDelegate(typeof>>, build, typeof>.GetMethod("SparseMatrixOfRowArrays")) :?> Func<'T[][],Matrix<'T>> - static let sc = Delegate.CreateDelegate(typeof>>, build, typeof>.GetMethod("SparseMatrixOfColumnArrays")) :?> Func<'T[][],Matrix<'T>> - static member DenseMatrixOfRowArrays(array) = dr.Invoke array - static member DenseMatrixOfColumnArrays(array) = dc.Invoke array - static member SparseMatrixOfRowArrays(array) = sr.Invoke array - static member SparseMatrixOfColumnArrays(array) = sc.Invoke array - - /// A module which helps constructing generic dense matrices. [] module DenseMatrix = @@ -400,7 +387,7 @@ module DenseMatrix = let inline ofRows (rows: Vector<'T> list) = Matrix<'T>.Build.DenseMatrixOfRowVectors(Array.ofList rows) /// Create a matrix from a list of row arrays. - let ofRowArrays (rows: 'T[][]) = ParamsInvokeWorkaround<'T>.DenseMatrixOfRowArrays(rows) + let inline ofRowArrays (rows: 'T[][]) = Matrix<'T>.Build.DenseMatrixOfRowArrays(rows :> seq<'T[]>) // workaround params issue - impl detects it's actually an array /// Create a matrix from a list of float lists. Every list in the master list specifies a row. let inline ofRowList (rows: 'T list list) = rows |> List.map List.toArray |> List.toArray |> ofRowArrays @@ -415,7 +402,7 @@ module DenseMatrix = let inline ofColumns (columns: Vector<'T> list) = Matrix<'T>.Build.DenseMatrixOfColumnVectors(Array.ofList columns) /// Create a matrix from a list of column arrays. - let ofColumnArrays (columns: 'T[][]) = ParamsInvokeWorkaround<'T>.DenseMatrixOfColumnArrays(columns) + let inline ofColumnArrays (columns: 'T[][]) = Matrix<'T>.Build.DenseMatrixOfColumnArrays(columns :> seq<'T[]>) // workaround params issue - impl detects it's actually an array /// Create a matrix from a list of float lists. Every list in the master list specifies a column. let inline ofColumnList (columns: 'T list list) = columns |> List.map List.toArray |> List.toArray |> ofColumnArrays @@ -480,7 +467,7 @@ module SparseMatrix = let inline ofRows (rows: Vector<'T> list) = Matrix<'T>.Build.SparseMatrixOfRowVectors(Array.ofList rows) /// Create a matrix from a list of row arrays. - let ofRowArrays (rows: 'T[][]) = ParamsInvokeWorkaround<'T>.SparseMatrixOfRowArrays(rows) + let inline ofRowArrays (rows: 'T[][]) = Matrix<'T>.Build.SparseMatrixOfRowArrays(rows :> seq<'T[]>) // workaround params issue - impl detects it's actually an array /// Create a matrix from a list of float lists. Every list in the master list specifies a row. let inline ofRowList (rows: 'T list list) = rows |> List.map List.toArray |> List.toArray |> ofRowArrays @@ -495,7 +482,7 @@ module SparseMatrix = let inline ofColumns (columns: Vector<'T> list) = Matrix<'T>.Build.SparseMatrixOfColumnVectors(Array.ofList columns) /// Create a matrix from a list of column arrays. - let ofColumnArrays (columns: 'T[][]) = ParamsInvokeWorkaround<'T>.SparseMatrixOfColumnArrays(columns) + let inline ofColumnArrays (columns: 'T[][]) = Matrix<'T>.Build.SparseMatrixOfColumnArrays(columns :> seq<'T[]>) // workaround params issue - impl detects it's actually an array /// Create a matrix from a list of float lists. Every list in the master list specifies a column. let inline ofColumnList (columns: 'T list list) = columns |> List.map List.toArray |> List.toArray |> ofColumnArrays diff --git a/src/FSharp/LinearAlgebra.Vector.fs b/src/FSharp/LinearAlgebra.Vector.fs index 1e194b17..612eec5e 100644 --- a/src/FSharp/LinearAlgebra.Vector.fs +++ b/src/FSharp/LinearAlgebra.Vector.fs @@ -272,7 +272,7 @@ module SparseVector = let inline init (n: int) (f: int -> 'T) = Vector<'T>.Build.SparseVector(n, f) /// Create a sparse vector from a float array. - let inline ofArray (fl: 'T array) = Vector<'T>.Build.SparseVectorOfEnumerable(Seq.ofArray fl) + let inline ofArray (fl: 'T array) = Vector<'T>.Build.SparseVectorOfEnumerable(fl :> seq<'T>) /// Create a sparse vector from a float list. let inline ofList (fl: 'T list) = Vector<'T>.Build.SparseVectorOfEnumerable(Seq.ofList fl) diff --git a/src/Numerics/LinearAlgebra/Builder.cs b/src/Numerics/LinearAlgebra/Builder.cs index 2ff05e93..d46d4558 100644 --- a/src/Numerics/LinearAlgebra/Builder.cs +++ b/src/Numerics/LinearAlgebra/Builder.cs @@ -500,6 +500,16 @@ namespace MathNet.Numerics.LinearAlgebra return DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnArrays(columns)); } + /// + /// Create a new dense matrix of T 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 Matrix DenseMatrixOfColumnArrays(IEnumerable columns) + { + return DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnArrays((columns as T[][]) ?? columns.ToArray())); + } + /// /// Create a new dense matrix as a copy of the given column vectors. /// This new matrix will be independent from the vectors. @@ -515,6 +525,16 @@ namespace MathNet.Numerics.LinearAlgebra return DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnVectors(storage)); } + /// + /// Create a new dense matrix as a copy of the given column vectors. + /// This new matrix will be independent from the vectors. + /// A new memory block will be allocated for storing the matrix. + /// + public Matrix DenseMatrixOfColumnVectors(IEnumerable> columns) + { + return DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnVectors(columns.Select(c => c.Storage).ToArray())); + } + /// /// Create a new dense matrix as a copy of the given enumerable of enumerable rows. /// Each enumerable in the master enumerable specifies a row. @@ -547,6 +567,16 @@ namespace MathNet.Numerics.LinearAlgebra return DenseMatrix(DenseColumnMajorMatrixStorage.OfRowArrays(rows)); } + /// + /// Create a new dense matrix of T 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 Matrix DenseMatrixOfRowArrays(IEnumerable rows) + { + return DenseMatrix(DenseColumnMajorMatrixStorage.OfRowArrays((rows as T[][]) ?? rows.ToArray())); + } + /// /// Create a new dense matrix as a copy of the given row vectors. /// This new matrix will be independent from the vectors. @@ -562,6 +592,16 @@ namespace MathNet.Numerics.LinearAlgebra return DenseMatrix(DenseColumnMajorMatrixStorage.OfRowVectors(storage)); } + /// + /// Create a new dense matrix as a copy of the given row vectors. + /// This new matrix will be independent from the vectors. + /// A new memory block will be allocated for storing the matrix. + /// + public Matrix DenseMatrixOfRowVectors(IEnumerable> rows) + { + return DenseMatrix(DenseColumnMajorMatrixStorage.OfRowVectors(rows.Select(r => r.Storage).ToArray())); + } + /// /// Create a new dense matrix with the diagonal as a copy of the given vector. /// This new matrix will be independent from the vector. @@ -749,6 +789,16 @@ namespace MathNet.Numerics.LinearAlgebra return SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnArrays(columns)); } + /// + /// 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 Matrix SparseMatrixOfColumnArrays(IEnumerable columns) + { + return SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnArrays((columns as T[][]) ?? columns.ToArray())); + } + /// /// Create a new sparse matrix as a copy of the given column vectors. /// This new matrix will be independent from the vectors. @@ -764,6 +814,16 @@ namespace MathNet.Numerics.LinearAlgebra return SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnVectors(storage)); } + /// + /// Create a new sparse matrix as a copy of the given column vectors. + /// This new matrix will be independent from the vectors. + /// A new memory block will be allocated for storing the matrix. + /// + public Matrix SparseMatrixOfColumnVectors(IEnumerable> columns) + { + return SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnVectors(columns.Select(c => c.Storage).ToArray())); + } + /// /// Create a new sparse matrix as a copy of the given enumerable of enumerable rows. /// Each enumerable in the master enumerable specifies a row. @@ -796,6 +856,16 @@ namespace MathNet.Numerics.LinearAlgebra return SparseMatrix(SparseCompressedRowMatrixStorage.OfRowArrays(rows)); } + /// + /// 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 Matrix SparseMatrixOfRowArrays(IEnumerable rows) + { + return SparseMatrix(SparseCompressedRowMatrixStorage.OfRowArrays((rows as T[][]) ?? rows.ToArray())); + } + /// /// Create a new sparse matrix as a copy of the given row vectors. /// This new matrix will be independent from the vectors. @@ -811,6 +881,16 @@ namespace MathNet.Numerics.LinearAlgebra return SparseMatrix(SparseCompressedRowMatrixStorage.OfRowVectors(storage)); } + /// + /// Create a new sparse matrix as a copy of the given row vectors. + /// This new matrix will be independent from the vectors. + /// A new memory block will be allocated for storing the matrix. + /// + public Matrix SparseMatrixOfRowVectors(IEnumerable> rows) + { + return SparseMatrix(SparseCompressedRowMatrixStorage.OfRowVectors(rows.Select(r => r.Storage).ToArray())); + } + /// /// Create a new sparse matrix with the diagonal as a copy of the given vector. /// This new matrix will be independent from the vector. diff --git a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs index e1305b4f..f2048841 100644 --- a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs @@ -197,6 +197,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnArrays(columns)); } + /// + /// 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(IEnumerable columns) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnArrays((columns as Complex[][]) ?? columns.ToArray())); + } + /// /// Create a new dense matrix as a copy of the given column vectors. /// This new matrix will be independent from the vectors. @@ -212,6 +222,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnVectors(storage)); } + /// + /// Create a new dense matrix as a copy of the given column vectors. + /// This new matrix will be independent from the vectors. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfColumnVectors(IEnumerable> columns) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnVectors(columns.Select(c => c.Storage).ToArray())); + } + /// /// Create a new dense matrix as a copy of the given enumerable of enumerable rows. /// Each enumerable in the master enumerable specifies a row. @@ -244,6 +264,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowArrays(rows)); } + /// + /// 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 OfRowArrays(IEnumerable rows) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowArrays((rows as Complex[][]) ?? rows.ToArray())); + } + /// /// Create a new dense matrix as a copy of the given row vectors. /// This new matrix will be independent from the vectors. @@ -259,6 +289,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowVectors(storage)); } + /// + /// Create a new dense matrix as a copy of the given row vectors. + /// This new matrix will be independent from the vectors. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfRowVectors(IEnumerable> rows) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowVectors(rows.Select(r => r.Storage).ToArray())); + } + /// /// Create a new dense matrix with the diagonal as a copy of the given vector. /// This new matrix will be independent from the vector. diff --git a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs index 5c8338d4..bce9fabb 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs @@ -184,6 +184,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnArrays(columns)); } + /// + /// 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(IEnumerable columns) + { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnArrays((columns as Complex[][]) ?? columns.ToArray())); + } + /// /// Create a new sparse matrix as a copy of the given column vectors. /// This new matrix will be independent from the vectors. @@ -199,6 +209,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnVectors(storage)); } + /// + /// Create a new sparse matrix as a copy of the given column vectors. + /// This new matrix will be independent from the vectors. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfColumnVectors(IEnumerable> columns) + { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnVectors(columns.Select(c => c.Storage).ToArray())); + } + /// /// Create a new sparse matrix as a copy of the given enumerable of enumerable rows. /// Each enumerable in the master enumerable specifies a row. @@ -231,6 +251,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowArrays(rows)); } + /// + /// 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 OfRowArrays(IEnumerable rows) + { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowArrays((rows as Complex[][]) ?? rows.ToArray())); + } + /// /// Create a new sparse matrix as a copy of the given row vectors. /// This new matrix will be independent from the vectors. @@ -246,6 +276,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowVectors(storage)); } + /// + /// Create a new sparse matrix as a copy of the given row vectors. + /// This new matrix will be independent from the vectors. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfRowVectors(IEnumerable> rows) + { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowVectors(rows.Select(r => r.Storage).ToArray())); + } + /// /// Create a new sparse matrix with the diagonal as a copy of the given vector. /// This new matrix will be independent from the vector. diff --git a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs index 5811a2c6..96f991f4 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs @@ -192,6 +192,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnArrays(columns)); } + /// + /// 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(IEnumerable columns) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnArrays((columns as Complex32[][]) ?? columns.ToArray())); + } + /// /// Create a new dense matrix as a copy of the given column vectors. /// This new matrix will be independent from the vectors. @@ -207,6 +217,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnVectors(storage)); } + /// + /// Create a new dense matrix as a copy of the given column vectors. + /// This new matrix will be independent from the vectors. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfColumnVectors(IEnumerable> columns) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnVectors(columns.Select(c => c.Storage).ToArray())); + } + /// /// Create a new dense matrix as a copy of the given enumerable of enumerable rows. /// Each enumerable in the master enumerable specifies a row. @@ -239,6 +259,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowArrays(rows)); } + /// + /// 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 OfRowArrays(IEnumerable rows) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowArrays((rows as Complex32[][]) ?? rows.ToArray())); + } + /// /// Create a new dense matrix as a copy of the given row vectors. /// This new matrix will be independent from the vectors. @@ -254,6 +284,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowVectors(storage)); } + /// + /// Create a new dense matrix as a copy of the given row vectors. + /// This new matrix will be independent from the vectors. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfRowVectors(IEnumerable> rows) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowVectors(rows.Select(r => r.Storage).ToArray())); + } + /// /// Create a new dense matrix with the diagonal as a copy of the given vector. /// This new matrix will be independent from the vector. diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs index c1efa898..f0572957 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs @@ -179,6 +179,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnArrays(columns)); } + /// + /// 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(IEnumerable columns) + { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnArrays((columns as Complex32[][]) ?? columns.ToArray())); + } + /// /// Create a new sparse matrix as a copy of the given column vectors. /// This new matrix will be independent from the vectors. @@ -194,6 +204,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnVectors(storage)); } + /// + /// Create a new sparse matrix as a copy of the given column vectors. + /// This new matrix will be independent from the vectors. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfColumnVectors(IEnumerable> columns) + { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnVectors(columns.Select(c => c.Storage).ToArray())); + } + /// /// Create a new sparse matrix as a copy of the given enumerable of enumerable rows. /// Each enumerable in the master enumerable specifies a row. @@ -226,6 +246,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowArrays(rows)); } + /// + /// 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 OfRowArrays(IEnumerable rows) + { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowArrays((rows as Complex32[][]) ?? rows.ToArray())); + } + /// /// Create a new sparse matrix as a copy of the given row vectors. /// This new matrix will be independent from the vectors. @@ -241,6 +271,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowVectors(storage)); } + /// + /// Create a new sparse matrix as a copy of the given row vectors. + /// This new matrix will be independent from the vectors. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfRowVectors(IEnumerable> rows) + { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowVectors(rows.Select(r => r.Storage).ToArray())); + } + /// /// Create a new sparse matrix with the diagonal as a copy of the given vector. /// This new matrix will be independent from the vector. diff --git a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs index d5c51408..f719b4bf 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs @@ -190,6 +190,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnArrays(columns)); } + /// + /// 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(IEnumerable columns) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnArrays((columns as double[][]) ?? columns.ToArray())); + } + /// /// Create a new dense matrix as a copy of the given column vectors. /// This new matrix will be independent from the vectors. @@ -205,6 +215,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnVectors(storage)); } + /// + /// Create a new dense matrix as a copy of the given column vectors. + /// This new matrix will be independent from the vectors. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfColumnVectors(IEnumerable> columns) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnVectors(columns.Select(c => c.Storage).ToArray())); + } + /// /// Create a new dense matrix as a copy of the given enumerable of enumerable rows. /// Each enumerable in the master enumerable specifies a row. @@ -237,6 +257,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowArrays(rows)); } + /// + /// 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 OfRowArrays(IEnumerable rows) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowArrays((rows as double[][]) ?? rows.ToArray())); + } + /// /// Create a new dense matrix as a copy of the given row vectors. /// This new matrix will be independent from the vectors. @@ -252,6 +282,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowVectors(storage)); } + /// + /// Create a new dense matrix as a copy of the given row vectors. + /// This new matrix will be independent from the vectors. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfRowVectors(IEnumerable> rows) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowVectors(rows.Select(r => r.Storage).ToArray())); + } + /// /// Create a new dense matrix with the diagonal as a copy of the given vector. /// This new matrix will be independent from the vector. diff --git a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs index cb34bc75..387486d0 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs @@ -177,6 +177,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnArrays(columns)); } + /// + /// 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(IEnumerable columns) + { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnArrays((columns as double[][]) ?? columns.ToArray())); + } + /// /// Create a new sparse matrix as a copy of the given column vectors. /// This new matrix will be independent from the vectors. @@ -192,6 +202,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnVectors(storage)); } + /// + /// Create a new sparse matrix as a copy of the given column vectors. + /// This new matrix will be independent from the vectors. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfColumnVectors(IEnumerable> columns) + { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnVectors(columns.Select(c => c.Storage).ToArray())); + } + /// /// Create a new sparse matrix as a copy of the given enumerable of enumerable rows. /// Each enumerable in the master enumerable specifies a row. @@ -224,6 +244,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowArrays(rows)); } + /// + /// 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 OfRowArrays(IEnumerable rows) + { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowArrays((rows as double[][]) ?? rows.ToArray())); + } + /// /// Create a new sparse matrix as a copy of the given row vectors. /// This new matrix will be independent from the vectors. @@ -239,6 +269,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowVectors(storage)); } + /// + /// Create a new sparse matrix as a copy of the given row vectors. + /// This new matrix will be independent from the vectors. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfRowVectors(IEnumerable> rows) + { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowVectors(rows.Select(r => r.Storage).ToArray())); + } + /// /// Create a new sparse matrix with the diagonal as a copy of the given vector. /// This new matrix will be independent from the vector. diff --git a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs index 995b3ce5..7293bb91 100644 --- a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs @@ -190,6 +190,16 @@ namespace MathNet.Numerics.LinearAlgebra.Single return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnArrays(columns)); } + /// + /// 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(IEnumerable columns) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnArrays((columns as float[][]) ?? columns.ToArray())); + } + /// /// Create a new dense matrix as a copy of the given column vectors. /// This new matrix will be independent from the vectors. @@ -205,6 +215,16 @@ namespace MathNet.Numerics.LinearAlgebra.Single return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnVectors(storage)); } + /// + /// Create a new dense matrix as a copy of the given column vectors. + /// This new matrix will be independent from the vectors. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfColumnVectors(IEnumerable> columns) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnVectors(columns.Select(c => c.Storage).ToArray())); + } + /// /// Create a new dense matrix as a copy of the given enumerable of enumerable rows. /// Each enumerable in the master enumerable specifies a row. @@ -237,6 +257,16 @@ namespace MathNet.Numerics.LinearAlgebra.Single return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowArrays(rows)); } + /// + /// 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 OfRowArrays(IEnumerable rows) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowArrays((rows as float[][]) ?? rows.ToArray())); + } + /// /// Create a new dense matrix as a copy of the given row vectors. /// This new matrix will be independent from the vectors. @@ -252,6 +282,16 @@ namespace MathNet.Numerics.LinearAlgebra.Single return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowVectors(storage)); } + /// + /// Create a new dense matrix as a copy of the given row vectors. + /// This new matrix will be independent from the vectors. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfRowVectors(IEnumerable> rows) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowVectors(rows.Select(r => r.Storage).ToArray())); + } + /// /// Create a new dense matrix with the diagonal as a copy of the given vector. /// This new matrix will be independent from the vector. diff --git a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs index 97f0e667..febb20cb 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs @@ -177,6 +177,16 @@ namespace MathNet.Numerics.LinearAlgebra.Single return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnArrays(columns)); } + /// + /// 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(IEnumerable columns) + { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnArrays((columns as float[][]) ?? columns.ToArray())); + } + /// /// Create a new sparse matrix as a copy of the given column vectors. /// This new matrix will be independent from the vectors. @@ -192,6 +202,16 @@ namespace MathNet.Numerics.LinearAlgebra.Single return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnVectors(storage)); } + /// + /// Create a new sparse matrix as a copy of the given column vectors. + /// This new matrix will be independent from the vectors. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfColumnVectors(IEnumerable> columns) + { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnVectors(columns.Select(c => c.Storage).ToArray())); + } + /// /// Create a new sparse matrix as a copy of the given enumerable of enumerable rows. /// Each enumerable in the master enumerable specifies a row. @@ -224,6 +244,16 @@ namespace MathNet.Numerics.LinearAlgebra.Single return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowArrays(rows)); } + /// + /// 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 OfRowArrays(IEnumerable rows) + { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowArrays((rows as float[][]) ?? rows.ToArray())); + } + /// /// Create a new sparse matrix as a copy of the given row vectors. /// This new matrix will be independent from the vectors. @@ -239,6 +269,16 @@ namespace MathNet.Numerics.LinearAlgebra.Single return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowVectors(storage)); } + /// + /// Create a new sparse matrix as a copy of the given row vectors. + /// This new matrix will be independent from the vectors. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfRowVectors(IEnumerable> rows) + { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowVectors(rows.Select(r => r.Storage).ToArray())); + } + /// /// Create a new sparse matrix with the diagonal as a copy of the given vector. /// This new matrix will be independent from the vector.