From 5abb1fa07fa82faf345334112ba63930fffa5cd4 Mon Sep 17 00:00:00 2001 From: RR Date: Wed, 20 Jun 2018 22:27:31 +0100 Subject: [PATCH] Addition of SparseMatrix implementation of OnTransposeThisAndMultiply method for all numeric types --- .../LinearAlgebra/Complex/SparseMatrix.cs | 29 +++++++++++++++++++ .../LinearAlgebra/Complex32/SparseMatrix.cs | 29 +++++++++++++++++++ .../LinearAlgebra/Double/SparseMatrix.cs | 29 +++++++++++++++++++ .../LinearAlgebra/Single/SparseMatrix.cs | 29 +++++++++++++++++++ 4 files changed, 116 insertions(+) diff --git a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs index 3f2e1b22..03fc2b19 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs @@ -1145,6 +1145,35 @@ namespace MathNet.Numerics.LinearAlgebra.Complex } } + /// + /// Multiplies the transpose of this matrix with a vector and places the results into the result vector. + /// + /// The vector to multiply with. + /// The result of the multiplication. + protected override void DoTransposeThisAndMultiply(Vector rightSide, Vector result) + { + var rowPointers = _storage.RowPointers; + var columnIndices = _storage.ColumnIndices; + var values = _storage.Values; + + for (var row = 0; row < RowCount; row++) + { + var startIndex = rowPointers[row]; + var endIndex = rowPointers[row + 1]; + + if (startIndex == endIndex) + { + continue; + } + + var rightSideValue = rightSide[row]; + for (var index = startIndex; index < endIndex; index++) + { + result[columnIndices[index]] += values[index] * rightSideValue; + } + } + } + /// /// Pointwise multiplies this matrix with another matrix and stores the result into the result matrix. /// diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs index 11ac2f98..d2394dff 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs @@ -1144,6 +1144,35 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } } + /// + /// Multiplies the transpose of this matrix with a vector and places the results into the result vector. + /// + /// The vector to multiply with. + /// The result of the multiplication. + protected override void DoTransposeThisAndMultiply(Vector rightSide, Vector result) + { + var rowPointers = _storage.RowPointers; + var columnIndices = _storage.ColumnIndices; + var values = _storage.Values; + + for (var row = 0; row < RowCount; row++) + { + var startIndex = rowPointers[row]; + var endIndex = rowPointers[row + 1]; + + if (startIndex == endIndex) + { + continue; + } + + var rightSideValue = rightSide[row]; + for (var index = startIndex; index < endIndex; index++) + { + result[columnIndices[index]] += values[index] * rightSideValue; + } + } + } + /// /// Pointwise multiplies this matrix with another matrix and stores the result into the result matrix. /// diff --git a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs index 51d35c57..dfaf0301 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs @@ -1142,6 +1142,35 @@ namespace MathNet.Numerics.LinearAlgebra.Double } } + /// + /// Multiplies the transpose of this matrix with a vector and places the results into the result vector. + /// + /// The vector to multiply with. + /// The result of the multiplication. + protected override void DoTransposeThisAndMultiply(Vector rightSide, Vector result) + { + var rowPointers = _storage.RowPointers; + var columnIndices = _storage.ColumnIndices; + var values = _storage.Values; + + for (var row = 0; row < RowCount; row++) + { + var startIndex = rowPointers[row]; + var endIndex = rowPointers[row + 1]; + + if (startIndex == endIndex) + { + continue; + } + + var rightSideValue = rightSide[row]; + for (var index = startIndex; index < endIndex; index++) + { + result[columnIndices[index]] += values[index] * rightSideValue; + } + } + } + /// /// Pointwise multiplies this matrix with another matrix and stores the result into the result matrix. /// diff --git a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs index 5391b975..8bd8e083 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs @@ -1147,6 +1147,35 @@ namespace MathNet.Numerics.LinearAlgebra.Single } } + /// + /// Multiplies the transpose of this matrix with a vector and places the results into the result vector. + /// + /// The vector to multiply with. + /// The result of the multiplication. + protected override void DoTransposeThisAndMultiply(Vector rightSide, Vector result) + { + var rowPointers = _storage.RowPointers; + var columnIndices = _storage.ColumnIndices; + var values = _storage.Values; + + for (var row = 0; row < RowCount; row++) + { + var startIndex = rowPointers[row]; + var endIndex = rowPointers[row + 1]; + + if (startIndex == endIndex) + { + continue; + } + + var rightSideValue = rightSide[row]; + for (var index = startIndex; index < endIndex; index++) + { + result[columnIndices[index]] += values[index] * rightSideValue; + } + } + } + /// /// Pointwise multiplies this matrix with another matrix and stores the result into the result matrix. ///