From 76494aa44bcff04ebadc449accc6c3feba1d573e Mon Sep 17 00:00:00 2001 From: Arthur Date: Fri, 24 Jun 2016 17:49:31 +0200 Subject: [PATCH] Directly address underlying data-array for multiplying sparse with dense matrices with DenseColumnMajorMatrixStorage --- .../LinearAlgebra/Double/SparseMatrix.cs | 32 +++++++++++++++++-- .../LinearAlgebra/Single/SparseMatrix.cs | 31 ++++++++++++++++-- 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs index 05932563..d332005d 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs @@ -906,12 +906,40 @@ namespace MathNet.Numerics.LinearAlgebra.Double } result.Clear(); - var columnVector = new DenseVector(other.RowCount); - var rowPointers = _storage.RowPointers; var columnIndices = _storage.ColumnIndices; var values = _storage.Values; + var denseOther = other.Storage as DenseColumnMajorMatrixStorage; + if (denseOther != null) + { + // in this case we can directly address the underlying data-array + for (var row = 0; row < RowCount; row++) + { + var startIndex = rowPointers[row]; + var endIndex = rowPointers[row + 1]; + + if (startIndex == endIndex) + { + continue; + } + + for (var column = 0; column < other.ColumnCount; column++) + { + int otherColumnStartPosition = column * other.RowCount; + var sum = 0d; + for (var index = startIndex; index < endIndex; index++) + { + sum += values[index] * denseOther.Data[otherColumnStartPosition + columnIndices[index]]; + } + + result.At(row, column, sum); + } + } + return; + } + + var columnVector = new DenseVector(other.RowCount); for (var row = 0; row < RowCount; row++) { var startIndex = rowPointers[row]; diff --git a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs index 38fb42c3..15d30bae 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs @@ -909,12 +909,39 @@ namespace MathNet.Numerics.LinearAlgebra.Single } result.Clear(); - var columnVector = new DenseVector(other.RowCount); - var rowPointers = _storage.RowPointers; var columnIndices = _storage.ColumnIndices; var values = _storage.Values; + var denseOther = other.Storage as DenseColumnMajorMatrixStorage; + if (denseOther != null) + { + // in this case we can directly address the underlying data-array + for (var row = 0; row < RowCount; row++) + { + var startIndex = rowPointers[row]; + var endIndex = rowPointers[row + 1]; + if (startIndex == endIndex) + { + continue; + } + + for (var column = 0; column < other.ColumnCount; column++) + { + int otherColumnStartPosition = column * other.RowCount; + var sum = 0f; + for (var index = startIndex; index < endIndex; index++) + { + sum += values[index] * denseOther.Data[otherColumnStartPosition + columnIndices[index]]; + } + + result.At(row, column, sum); + } + } + return; + } + + var columnVector = new DenseVector(other.RowCount); for (var row = 0; row < RowCount; row++) { var startIndex = rowPointers[row];