From d87f23e046f9f8af021510f514077d7fab0cee75 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sat, 6 Apr 2013 19:02:10 +0200 Subject: [PATCH] LA: Initialize sparse matrix by enumerable #104 --- .../LinearAlgebra/Complex/DenseMatrix.cs | 4 +- .../LinearAlgebra/Complex/SparseMatrix.cs | 11 ++++++ .../LinearAlgebra/Complex32/DenseMatrix.cs | 4 +- .../LinearAlgebra/Complex32/SparseMatrix.cs | 11 ++++++ .../LinearAlgebra/Double/DenseMatrix.cs | 4 +- .../LinearAlgebra/Double/SparseMatrix.cs | 11 ++++++ .../LinearAlgebra/Single/DenseMatrix.cs | 4 +- .../LinearAlgebra/Single/SparseMatrix.cs | 11 ++++++ .../SparseCompressedRowMatrixStorage.cs | 37 +++++++++++++++++++ 9 files changed, 89 insertions(+), 8 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs index 1860380b..11641275 100644 --- a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs @@ -152,8 +152,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// This new matrix will be independent from the enumerable. /// A new memory block will be allocated for storing the vector. /// - public DenseMatrix(int rows, int columns, IEnumerable other) - : this(DenseColumnMajorMatrixStorage.FromColumnMajorEnumerable(rows, columns, other)) + public DenseMatrix(int rows, int columns, IEnumerable columnMajor) + : this(DenseColumnMajorMatrixStorage.FromColumnMajorEnumerable(rows, columns, columnMajor)) { } diff --git a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs index 9ee45cf1..f5626fca 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs @@ -172,6 +172,17 @@ namespace MathNet.Numerics.LinearAlgebra.Complex } } + /// + /// Create a new sparse matrix as a copy of the given enumerable. + /// The enumerable is assumed to be in row-major order (row by row). + /// This new matrix will be independent from the enumerable. + /// A new memory block will be allocated for storing the vector. + /// + public SparseMatrix(int rows, int columns, IEnumerable rowMajor) + : this(SparseCompressedRowMatrixStorage.FromRowMajorEnumerable(rows, columns, rowMajor)) + { + } + /// /// Create a new sparse matrix as a copy of the given other matrix. /// This new matrix will be independent from the other matrix. diff --git a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs index b9f085c4..632565cf 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs @@ -152,8 +152,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// This new matrix will be independent from the enumerable. /// A new memory block will be allocated for storing the vector. /// - public DenseMatrix(int rows, int columns, IEnumerable other) - : this(DenseColumnMajorMatrixStorage.FromColumnMajorEnumerable(rows, columns, other)) + public DenseMatrix(int rows, int columns, IEnumerable columnMajor) + : this(DenseColumnMajorMatrixStorage.FromColumnMajorEnumerable(rows, columns, columnMajor)) { } diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs index 5518af2b..d61b6b6f 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs @@ -172,6 +172,17 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } } + /// + /// Create a new sparse matrix as a copy of the given enumerable. + /// The enumerable is assumed to be in row-major order (row by row). + /// This new matrix will be independent from the enumerable. + /// A new memory block will be allocated for storing the vector. + /// + public SparseMatrix(int rows, int columns, IEnumerable rowMajor) + : this(SparseCompressedRowMatrixStorage.FromRowMajorEnumerable(rows, columns, rowMajor)) + { + } + /// /// Create a new sparse matrix as a copy of the given other matrix. /// This new matrix will be independent from the other matrix. diff --git a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs index 5722cd5e..50c30a09 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs @@ -152,8 +152,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// This new matrix will be independent from the enumerable. /// A new memory block will be allocated for storing the vector. /// - public DenseMatrix(int rows, int columns, IEnumerable other) - : this(DenseColumnMajorMatrixStorage.FromColumnMajorEnumerable(rows, columns, other)) + public DenseMatrix(int rows, int columns, IEnumerable columnMajor) + : this(DenseColumnMajorMatrixStorage.FromColumnMajorEnumerable(rows, columns, columnMajor)) { } diff --git a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs index b322a908..8aff8f54 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs @@ -171,6 +171,17 @@ namespace MathNet.Numerics.LinearAlgebra.Double } } + /// + /// Create a new sparse matrix as a copy of the given enumerable. + /// The enumerable is assumed to be in row-major order (row by row). + /// This new matrix will be independent from the enumerable. + /// A new memory block will be allocated for storing the vector. + /// + public SparseMatrix(int rows, int columns, IEnumerable rowMajor) + : this(SparseCompressedRowMatrixStorage.FromRowMajorEnumerable(rows, columns, rowMajor)) + { + } + /// /// Create a new sparse matrix as a copy of the given other matrix. /// This new matrix will be independent from the other matrix. diff --git a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs index c7e886a3..75466261 100644 --- a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs @@ -152,8 +152,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// This new matrix will be independent from the enumerable. /// A new memory block will be allocated for storing the vector. /// - public DenseMatrix(int rows, int columns, IEnumerable other) - : this(DenseColumnMajorMatrixStorage.FromColumnMajorEnumerable(rows, columns, other)) + public DenseMatrix(int rows, int columns, IEnumerable columnMajor) + : this(DenseColumnMajorMatrixStorage.FromColumnMajorEnumerable(rows, columns, columnMajor)) { } diff --git a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs index 74405fc3..ea482214 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs @@ -171,6 +171,17 @@ namespace MathNet.Numerics.LinearAlgebra.Single } } + /// + /// Create a new sparse matrix as a copy of the given enumerable. + /// The enumerable is assumed to be in row-major order (row by row). + /// This new matrix will be independent from the enumerable. + /// A new memory block will be allocated for storing the vector. + /// + public SparseMatrix(int rows, int columns, IEnumerable rowMajor) + : this(SparseCompressedRowMatrixStorage.FromRowMajorEnumerable(rows, columns, rowMajor)) + { + } + /// /// Create a new sparse matrix as a copy of the given other matrix. /// This new matrix will be independent from the other matrix. diff --git a/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs index 131f3abf..6371d17f 100644 --- a/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs @@ -361,6 +361,43 @@ namespace MathNet.Numerics.LinearAlgebra.Storage return hash; } + // INITIALIZATION + + public static SparseCompressedRowMatrixStorage FromRowMajorEnumerable(int rows, int columns, IEnumerable data) + { + if (data == null) + { + throw new ArgumentNullException("data"); + } + + var storage = new SparseCompressedRowMatrixStorage(rows, columns); + var rowPointers = storage.RowPointers; + var columnIndices = new List(); + var values = new List(); + + var iterator = data.GetEnumerator(); + for (int row = 0; row < rows; row++) + { + rowPointers[row] = values.Count; + for (int col = 0; col < columns; col++) + { + iterator.MoveNext(); + if (!Zero.Equals(iterator.Current)) + { + values.Add(iterator.Current); + columnIndices.Add(col); + } + } + } + + storage.ColumnIndices = columnIndices.ToArray(); + storage.Values = values.ToArray(); + storage.ValueCount = values.Count; + return storage; + } + + // MATRIX COPY + internal override void CopyToUnchecked(MatrixStorage target, bool skipClearing = false) { var sparseTarget = target as SparseCompressedRowMatrixStorage;