From 555879bdd2c82689d7174bd55b6a78e54bca2509 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sun, 7 Apr 2013 20:35:06 +0200 Subject: [PATCH] LA: sparse matrix OfColumnMajorList --- .../LinearAlgebra/Complex/SparseMatrix.cs | 31 ++-------------- .../LinearAlgebra/Complex32/SparseMatrix.cs | 31 ++-------------- .../LinearAlgebra/Double/SparseMatrix.cs | 31 ++-------------- .../LinearAlgebra/Single/SparseMatrix.cs | 31 ++-------------- .../SparseCompressedRowMatrixStorage.cs | 36 +++++++++++++++++++ 5 files changed, 48 insertions(+), 112 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs index 2cbdbe25..2a5b605c 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs @@ -131,22 +131,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// A new memory block will be allocated for storing the matrix. /// /// - public static SparseMatrix OfColumnMajor(int rows, int columns, Complex[] array) + public static SparseMatrix OfColumnMajor(int rows, int columns, Complex[] columnMajor) { - if (rows * columns > array.Length) - { - throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions); - } - - var storage = new SparseCompressedRowMatrixStorage(rows, columns); - for (var i = 0; i < rows; i++) - { - for (var j = 0; j < columns; j++) - { - storage.At(i, j, array[i + (j * rows)]); - } - } - return new SparseMatrix(storage); + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnMajorList(rows, columns, columnMajor)); } /// @@ -197,20 +184,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// [Obsolete("Use SparseMatrix.OfColumnMajor instead. Scheduled for removal in v3.0.")] public SparseMatrix(int rows, int columns, Complex[] array) - : this(rows, columns) + : this(SparseCompressedRowMatrixStorage.OfColumnMajorList(rows, columns, array)) { - if (rows * columns > array.Length) - { - throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions); - } - - for (var i = 0; i < rows; i++) - { - for (var j = 0; j < columns; j++) - { - _storage.At(i, j, array[i + (j * rows)]); - } - } } /// diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs index 57cbeb79..15faf7e3 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs @@ -131,22 +131,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// A new memory block will be allocated for storing the matrix. /// /// - public static SparseMatrix OfColumnMajor(int rows, int columns, Complex32[] array) + public static SparseMatrix OfColumnMajor(int rows, int columns, Complex32[] columnMajor) { - if (rows * columns > array.Length) - { - throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions); - } - - var storage = new SparseCompressedRowMatrixStorage(rows, columns); - for (var i = 0; i < rows; i++) - { - for (var j = 0; j < columns; j++) - { - storage.At(i, j, array[i + (j * rows)]); - } - } - return new SparseMatrix(storage); + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnMajorList(rows, columns, columnMajor)); } /// @@ -197,20 +184,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// [Obsolete("Use SparseMatrix.OfColumnMajor instead. Scheduled for removal in v3.0.")] public SparseMatrix(int rows, int columns, Complex32[] array) - : this(rows, columns) + : this(SparseCompressedRowMatrixStorage.OfColumnMajorList(rows, columns, array)) { - if (rows * columns > array.Length) - { - throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions); - } - - for (var i = 0; i < rows; i++) - { - for (var j = 0; j < columns; j++) - { - _storage.At(i, j, array[i + (j * rows)]); - } - } } /// diff --git a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs index 49431633..80c4e64e 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs @@ -130,22 +130,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// A new memory block will be allocated for storing the matrix. /// /// - public static SparseMatrix OfColumnMajor(int rows, int columns, double[] array) + public static SparseMatrix OfColumnMajor(int rows, int columns, double[] columnMajor) { - if (rows * columns > array.Length) - { - throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions); - } - - var storage = new SparseCompressedRowMatrixStorage(rows, columns); - for (var i = 0; i < rows; i++) - { - for (var j = 0; j < columns; j++) - { - storage.At(i, j, array[i + (j * rows)]); - } - } - return new SparseMatrix(storage); + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnMajorList(rows, columns, columnMajor)); } /// @@ -196,20 +183,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// [Obsolete("Use SparseMatrix.OfColumnMajor instead. Scheduled for removal in v3.0.")] public SparseMatrix(int rows, int columns, double[] array) - : this(rows, columns) + : this(SparseCompressedRowMatrixStorage.OfColumnMajorList(rows, columns, array)) { - if (rows * columns > array.Length) - { - throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions); - } - - for (var i = 0; i < rows; i++) - { - for (var j = 0; j < columns; j++) - { - _storage.At(i, j, array[i + (j * rows)]); - } - } } /// diff --git a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs index cf42d823..d6270828 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs @@ -130,22 +130,9 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// A new memory block will be allocated for storing the matrix. /// /// - public static SparseMatrix OfColumnMajor(int rows, int columns, float[] array) + public static SparseMatrix OfColumnMajor(int rows, int columns, float[] columnMajor) { - if (rows * columns > array.Length) - { - throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions); - } - - var storage = new SparseCompressedRowMatrixStorage(rows, columns); - for (var i = 0; i < rows; i++) - { - for (var j = 0; j < columns; j++) - { - storage.At(i, j, array[i + (j * rows)]); - } - } - return new SparseMatrix(storage); + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnMajorList(rows, columns, columnMajor)); } /// @@ -196,20 +183,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// [Obsolete("Use SparseMatrix.OfColumnMajor instead. Scheduled for removal in v3.0.")] public SparseMatrix(int rows, int columns, float[] array) - : this(rows, columns) + : this(SparseCompressedRowMatrixStorage.OfColumnMajorList(rows, columns, array)) { - if (rows * columns > array.Length) - { - throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions); - } - - for (var i = 0; i < rows; i++) - { - for (var j = 0; j < columns; j++) - { - _storage.At(i, j, array[i + (j * rows)]); - } - } } /// diff --git a/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs index f6953fb0..8dd33fe5 100644 --- a/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs @@ -434,6 +434,42 @@ namespace MathNet.Numerics.LinearAlgebra.Storage return storage; } + public static SparseCompressedRowMatrixStorage OfColumnMajorList(int rows, int columns, IList data) + { + if (data == null) + { + throw new ArgumentNullException("data"); + } + if (rows * columns != data.Count) + { + throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions); + } + + 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 = data[row + (col*rows)]; + if (!Zero.Equals(item)) + { + values.Add(item); + 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)