From c117e106515b1d3e5da82d95a7af43e6b4171ff1 Mon Sep 17 00:00:00 2001 From: Mohamed Moussa Date: Tue, 18 Jan 2022 16:51:06 +1100 Subject: [PATCH] Fixes NormalizeDuplicates (for sparse COO) When values are removed, `RowPointers` is modified so that the start of the next row is pointing to the correct element. Howevever, since we are doing things in-place, when we start reading the next row we sart from the modified location instead of the original location. To fix this, `index` is set to the end of the original (unmodified) row instead of reading directly from `RowPointers`. The logic of the internal `while` loop is also simplified. --- .../SparseCompressedRowMatrixStorage.cs | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs index 198121db..210fb500 100644 --- a/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs @@ -302,26 +302,20 @@ namespace MathNet.Numerics.LinearAlgebra.Storage var builder = BuilderInstance.Matrix; int valueCount = 0; + int last = 0; for (int i = 0; i < RowCount; i++) { - int index = RowPointers[i]; - int last = RowPointers[i + 1]; + int index = last; + last = RowPointers[i + 1]; while (index < last) { var col = ColumnIndices[index]; var val = Values[index]; index++; - while (index < last) + while (index < last && ColumnIndices[index] == col) { - if (ColumnIndices[index] == col) - { - val = builder.Add(val, Values[index]); - index++; - } - else - { - break; - } + val = builder.Add(val, Values[index]); + index++; } ColumnIndices[valueCount] = col; Values[valueCount] = val;