Browse Source

Sparse: fix bug in submatrix-clear

la-knuth
Christoph Ruegg 14 years ago
parent
commit
21a2dab933
  1. 6
      src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs
  2. 14
      src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.cs

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

@ -152,17 +152,17 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return;
}
for (int i = rowIndex, row = 0; i < rowIndex + rowCount; i++, row++)
for (int i = rowIndex + rowCount - 1, row = rowCount - 1; i >= rowIndex; i--, row--)
{
var startIndex = RowPointers[i];
var endIndex = i < RowPointers.Length - 1 ? RowPointers[i + 1] : ValueCount;
for (int j = startIndex; j < endIndex; j++)
for (int j = endIndex - 1; j >= startIndex; j--)
{
// check if the column index is in the range
if ((ColumnIndices[j] >= columnIndex) && (ColumnIndices[j] < columnIndex + columnCount))
{
var column = ColumnIndices[j] - columnIndex;
var column = ColumnIndices[j];
// NOTE: potential for more efficient implementation
At(row, column, _zero);

14
src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.cs

@ -120,6 +120,20 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
Assert.That(cleared, Is.EqualTo(matrix.CreateMatrix(matrix.RowCount, matrix.ColumnCount)));
}
[Theory, Timeout(200)]
public void CanClearSubMatrix(Matrix<T> matrix)
{
var cleared = matrix.Clone();
Assume.That(cleared.RowCount, Is.GreaterThanOrEqualTo(2));
Assume.That(cleared.ColumnCount, Is.GreaterThanOrEqualTo(2));
cleared.Storage.Clear(0,2,1,1);
Assert.That(cleared.At(0, 0), Is.EqualTo(matrix.At(0, 0)));
Assert.That(cleared.At(1, 0), Is.EqualTo(matrix.At(1, 0)));
Assert.That(cleared.At(0, 1), Is.EqualTo(Zero));
Assert.That(cleared.At(1, 1), Is.EqualTo(Zero));
}
[Theory, Timeout(200)]
public void CanToArray(Matrix<T> matrix)
{

Loading…
Cancel
Save