diff --git a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs index 9addf2aa..7b2fb10f 100644 --- a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs @@ -167,6 +167,21 @@ 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 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(params Vector[] columns) + { + var storage = new VectorStorage[columns.Length]; + for (int i = 0; i < columns.Length; i++) + { + storage[i] = columns[i].Storage; + } + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnVectors(storage)); + } + /// /// Create a new dense matrix as a copy of the given enumerable of enumerable columns. /// Each enumerable in the master enumerable specifies a column. @@ -190,6 +205,21 @@ namespace MathNet.Numerics.LinearAlgebra.Complex return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowEnumerables(rows, columns, data)); } + /// + /// 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(params Vector[] rows) + { + var storage = new VectorStorage[rows.Length]; + for (int i = 0; i < rows.Length; i++) + { + storage[i] = rows[i].Storage; + } + 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. diff --git a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs index 414d8dc7..6c6d6d6c 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs @@ -167,6 +167,21 @@ 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 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(params Vector[] columns) + { + var storage = new VectorStorage[columns.Length]; + for (int i = 0; i < columns.Length; i++) + { + storage[i] = columns[i].Storage; + } + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnVectors(storage)); + } + /// /// Create a new dense matrix as a copy of the given enumerable of enumerable columns. /// Each enumerable in the master enumerable specifies a column. @@ -190,6 +205,21 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowEnumerables(rows, columns, data)); } + /// + /// 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(params Vector[] rows) + { + var storage = new VectorStorage[rows.Length]; + for (int i = 0; i < rows.Length; i++) + { + storage[i] = rows[i].Storage; + } + 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. diff --git a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs index 8b1b9edf..1a519517 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs @@ -28,8 +28,6 @@ // OTHER DEALINGS IN THE SOFTWARE. // -using System.Runtime; - namespace MathNet.Numerics.LinearAlgebra.Double { using Algorithms.LinearAlgebra; @@ -169,6 +167,21 @@ 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 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(params Vector[] columns) + { + var storage = new VectorStorage[columns.Length]; + for (int i = 0; i < columns.Length; i++) + { + storage[i] = columns[i].Storage; + } + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnVectors(storage)); + } + /// /// Create a new dense matrix as a copy of the given enumerable of enumerable columns. /// Each enumerable in the master enumerable specifies a column. @@ -192,6 +205,21 @@ namespace MathNet.Numerics.LinearAlgebra.Double return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowEnumerables(rows, columns, data)); } + /// + /// 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(params Vector[] rows) + { + var storage = new VectorStorage[rows.Length]; + for (int i = 0; i < rows.Length; i++) + { + storage[i] = rows[i].Storage; + } + 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. diff --git a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs index 5365e415..993efde9 100644 --- a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs @@ -167,6 +167,21 @@ 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 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(params Vector[] columns) + { + var storage = new VectorStorage[columns.Length]; + for (int i = 0; i < columns.Length; i++) + { + storage[i] = columns[i].Storage; + } + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnVectors(storage)); + } + /// /// Create a new dense matrix as a copy of the given enumerable of enumerable columns. /// Each enumerable in the master enumerable specifies a column. @@ -190,6 +205,21 @@ namespace MathNet.Numerics.LinearAlgebra.Single return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowEnumerables(rows, columns, data)); } + /// + /// 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(params Vector[] rows) + { + var storage = new VectorStorage[rows.Length]; + for (int i = 0; i < rows.Length; i++) + { + storage[i] = rows[i].Storage; + } + 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. diff --git a/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs index 1d909095..4a1b1b01 100644 --- a/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs @@ -171,6 +171,50 @@ namespace MathNet.Numerics.LinearAlgebra.Storage return new DenseColumnMajorMatrixStorage(rows, columns, array); } + public static DenseColumnMajorMatrixStorage OfColumnVectors(VectorStorage[] data) + { + if (data == null) throw new ArgumentNullException("data"); + int columns = data.Length; + int rows = data[0].Length; + var array = new T[rows * columns]; + for (int j = 0; j < data.Length; j++) + { + var column = data[j]; + var denseColumn = column as DenseVectorStorage; + if (denseColumn != null) + { + Array.Copy(denseColumn.Data, 0, array, j * rows, rows); + } + else + { + // FALL BACK + int offset = j*rows; + for (int i = 0; i < rows; i++) + { + array[offset + i] = column.At(i); + } + } + } + return new DenseColumnMajorMatrixStorage(rows, columns, array); + } + + public static DenseColumnMajorMatrixStorage OfRowVectors(VectorStorage[] data) + { + if (data == null) throw new ArgumentNullException("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].At(j); + } + } + return new DenseColumnMajorMatrixStorage(rows, columns, array); + } + public static DenseColumnMajorMatrixStorage OfColumnEnumerables(int rows, int columns, IEnumerable data) // NOTE: flexible typing to 'backport' generic covariance. where TColumn : IEnumerable