Browse Source

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.
pull/895/head
Mohamed Moussa 5 years ago
parent
commit
c117e10651
  1. 18
      src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs

18
src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs

@ -302,26 +302,20 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
var builder = BuilderInstance<T>.Matrix; var builder = BuilderInstance<T>.Matrix;
int valueCount = 0; int valueCount = 0;
int last = 0;
for (int i = 0; i < RowCount; i++) for (int i = 0; i < RowCount; i++)
{ {
int index = RowPointers[i]; int index = last;
int last = RowPointers[i + 1]; last = RowPointers[i + 1];
while (index < last) while (index < last)
{ {
var col = ColumnIndices[index]; var col = ColumnIndices[index];
var val = Values[index]; var val = Values[index];
index++; index++;
while (index < last) while (index < last && ColumnIndices[index] == col)
{ {
if (ColumnIndices[index] == col) val = builder.Add(val, Values[index]);
{ index++;
val = builder.Add(val, Values[index]);
index++;
}
else
{
break;
}
} }
ColumnIndices[valueCount] = col; ColumnIndices[valueCount] = col;
Values[valueCount] = val; Values[valueCount] = val;

Loading…
Cancel
Save