From 418cb8cf2295b9ade02cd103b3f5ede5b5747bda Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sun, 24 Feb 2013 21:10:19 +0100 Subject: [PATCH] LA: Drop redundant sparse vector ToColumn/RowMatrix overrides --- .../LinearAlgebra/Complex/SparseVector.cs | 30 -------------- .../LinearAlgebra/Complex32/SparseVector.cs | 30 -------------- .../LinearAlgebra/Double/SparseVector.cs | 36 ----------------- src/Numerics/LinearAlgebra/Generic/Vector.cs | 4 +- .../LinearAlgebra/Single/SparseVector.cs | 30 -------------- .../Storage/SparseVectorStorage.cs | 40 +++++++++++++++++++ 6 files changed, 42 insertions(+), 128 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs index 4bb26717..ba6e7534 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs @@ -145,36 +145,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex #endregion - /// - /// Create a matrix based on this vector in column form (one single column). - /// - /// This vector as a column matrix. - public override Matrix ToColumnMatrix() - { - var matrix = new SparseMatrix(Count, 1); - for (var i = 0; i < _storage.ValueCount; i++) - { - matrix.At(_storage.Indices[i], 0, _storage.Values[i]); - } - - return matrix; - } - - /// - /// Create a matrix based on this vector in row form (one single row). - /// - /// This vector as a row matrix. - public override Matrix ToRowMatrix() - { - var matrix = new SparseMatrix(1, Count); - for (var i = 0; i < _storage.ValueCount; i++) - { - matrix.At(0, _storage.Indices[i], _storage.Values[i]); - } - - return matrix; - } - /// /// Creates a matrix with the given dimensions using the same storage type /// as this vector. diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs index 6b50585c..ba0cdbaa 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs @@ -145,36 +145,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 #endregion - /// - /// Create a matrix based on this vector in column form (one single column). - /// - /// This vector as a column matrix. - public override Matrix ToColumnMatrix() - { - var matrix = new SparseMatrix(Count, 1); - for (var i = 0; i < _storage.ValueCount; i++) - { - matrix.At(_storage.Indices[i], 0, _storage.Values[i]); - } - - return matrix; - } - - /// - /// Create a matrix based on this vector in row form (one single row). - /// - /// This vector as a row matrix. - public override Matrix ToRowMatrix() - { - var matrix = new SparseMatrix(1, Count); - for (var i = 0; i < _storage.ValueCount; i++) - { - matrix.At(0, _storage.Indices[i], _storage.Values[i]); - } - - return matrix; - } - /// /// Creates a matrix with the given dimensions using the same storage type /// as this vector. diff --git a/src/Numerics/LinearAlgebra/Double/SparseVector.cs b/src/Numerics/LinearAlgebra/Double/SparseVector.cs index b30b698b..a54c12b3 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseVector.cs @@ -145,42 +145,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double #endregion - /// - /// Create a matrix based on this vector in column form (one single column). - /// - /// This vector as a column matrix. - public override Matrix ToColumnMatrix() - { - var indices = _storage.Indices; - var values = _storage.Values; - - var matrix = new SparseMatrix(Count, 1); - for (var i = 0; i < _storage.ValueCount; i++) - { - matrix.At(indices[i], 0, values[i]); - } - - return matrix; - } - - /// - /// Create a matrix based on this vector in row form (one single row). - /// - /// This vector as a row matrix. - public override Matrix ToRowMatrix() - { - var indices = _storage.Indices; - var values = _storage.Values; - - var matrix = new SparseMatrix(1, Count); - for (var i = 0; i < _storage.ValueCount; i++) - { - matrix.At(0, indices[i], values[i]); - } - - return matrix; - } - /// /// Creates a matrix with the given dimensions using the same storage type /// as this vector. diff --git a/src/Numerics/LinearAlgebra/Generic/Vector.cs b/src/Numerics/LinearAlgebra/Generic/Vector.cs index ff1f8088..0572e618 100644 --- a/src/Numerics/LinearAlgebra/Generic/Vector.cs +++ b/src/Numerics/LinearAlgebra/Generic/Vector.cs @@ -1173,7 +1173,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// /// This vector as a column matrix. /// - public virtual Matrix ToColumnMatrix() + public Matrix ToColumnMatrix() { var result = CreateMatrix(Count, 1); Storage.CopyToColumnUnchecked(result.Storage, 0, skipClearing: true); @@ -1186,7 +1186,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// /// This vector as a row matrix. /// - public virtual Matrix ToRowMatrix() + public Matrix ToRowMatrix() { var result = CreateMatrix(1, Count); Storage.CopyToRowUnchecked(result.Storage, 0, skipClearing: true); diff --git a/src/Numerics/LinearAlgebra/Single/SparseVector.cs b/src/Numerics/LinearAlgebra/Single/SparseVector.cs index 837c05ec..b3b16ac3 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseVector.cs @@ -145,36 +145,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single #endregion - /// - /// Create a matrix based on this vector in column form (one single column). - /// - /// This vector as a column matrix. - public override Matrix ToColumnMatrix() - { - var matrix = new SparseMatrix(Count, 1); - for (var i = 0; i < _storage.ValueCount; i++) - { - matrix.At(_storage.Indices[i], 0, _storage.Values[i]); - } - - return matrix; - } - - /// - /// Create a matrix based on this vector in row form (one single row). - /// - /// This vector as a row matrix. - public override Matrix ToRowMatrix() - { - var matrix = new SparseMatrix(1, Count); - for (var i = 0; i < _storage.ValueCount; i++) - { - matrix.At(0, _storage.Indices[i], _storage.Values[i]); - } - - return matrix; - } - /// /// Creates a matrix with the given dimensions using the same storage type /// as this vector. diff --git a/src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs b/src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs index 1094ba7b..4f029592 100644 --- a/src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs @@ -335,6 +335,46 @@ namespace MathNet.Numerics.LinearAlgebra.Storage } } + // Row COPY + + internal override void CopyToRowUnchecked(MatrixStorage target, int rowIndex, bool skipClearing = false) + { + if (!skipClearing) + { + target.Clear(rowIndex, 1, 0, Length); + } + + if (ValueCount == 0) + { + return; + } + + for (int i = 0; i < ValueCount; i++) + { + target.At(rowIndex, Indices[i], Values[i]); + } + } + + // COLUMN COPY + + internal override void CopyToColumnUnchecked(MatrixStorage target, int columnIndex, bool skipClearing = false) + { + if (!skipClearing) + { + target.Clear(0, Length, columnIndex, 1); + } + + if (ValueCount == 0) + { + return; + } + + for (int i = 0; i < ValueCount; i++) + { + target.At(Indices[i], columnIndex, Values[i]); + } + } + // SUB-VECTOR COPY internal override void CopySubVectorToUnchecked(VectorStorage target,