Skip to content

Commit c117e10

Browse files
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.
1 parent 640c125 commit c117e10

1 file changed

Lines changed: 6 additions & 12 deletions

File tree

src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -302,26 +302,20 @@ public void NormalizeDuplicates()
302302
var builder = BuilderInstance<T>.Matrix;
303303

304304
int valueCount = 0;
305+
int last = 0;
305306
for (int i = 0; i < RowCount; i++)
306307
{
307-
int index = RowPointers[i];
308-
int last = RowPointers[i + 1];
308+
int index = last;
309+
last = RowPointers[i + 1];
309310
while (index < last)
310311
{
311312
var col = ColumnIndices[index];
312313
var val = Values[index];
313314
index++;
314-
while (index < last)
315+
while (index < last && ColumnIndices[index] == col)
315316
{
316-
if (ColumnIndices[index] == col)
317-
{
318-
val = builder.Add(val, Values[index]);
319-
index++;
320-
}
321-
else
322-
{
323-
break;
324-
}
317+
val = builder.Add(val, Values[index]);
318+
index++;
325319
}
326320
ColumnIndices[valueCount] = col;
327321
Values[valueCount] = val;

0 commit comments

Comments
 (0)