Hi,
I noticed a bug using your otherwise quite nice library with sparse matrices.
Currently, if you do the following:
var m1 = new SparseMatrix(1, 3);
var m2 = new SparseMatrix(new double[,] { { 0, 1, 1 } });
var sum1 = m1 + m2;
var sum2 = m2 + m1;
var diff = m1 - m2;
Then sum1 will give you { 0, 0, 0 } (instead of { 0, 1, 1 }) whereas sum2 will have the correct result; diff will also stay incorrectly equal to zero.
I have fixed the corresponding SparseMatrix.cs file by adding the following at the end of the for loop in the else block for both DoAdd() and DoSubtract():
// Get the begin / end index for the current row of the 'other' sparse matrix
var sparseOther = other as SparseMatrix;
var startIndexOther = sparseOther._rowIndex[i];
var endIndexOther = i < sparseOther._rowIndex.Length - 1 ? sparseOther._rowIndex[i + 1] : sparseOther.NonZerosCount;
// Look for 'forgotten' components
for (var j = startIndexOther; j < endIndexOther; j++)
{
var realColumnIndex = sparseOther._columnIndices[j];
if (At(i, realColumnIndex) == 0.0)
{
result.At(i, realColumnIndex, sparseOther._nonZeroValues[j]);
// VARIANT for DoSubtract(): result.At(i, realColumnIndex, -sparseOther._nonZeroValues[j]);
}
}
This worked for me, so I hope this will help you too. And keep up the good work!
Cheers,
Comments: (Not sure why Codeplex keeps reopening these issues all the time)
I noticed a bug using your otherwise quite nice library with sparse matrices.
Currently, if you do the following:
var m1 = new SparseMatrix(1, 3);
var m2 = new SparseMatrix(new double[,] { { 0, 1, 1 } });
var sum1 = m1 + m2;
var sum2 = m2 + m1;
var diff = m1 - m2;
Then sum1 will give you { 0, 0, 0 } (instead of { 0, 1, 1 }) whereas sum2 will have the correct result; diff will also stay incorrectly equal to zero.
I have fixed the corresponding SparseMatrix.cs file by adding the following at the end of the for loop in the else block for both DoAdd() and DoSubtract():
// Get the begin / end index for the current row of the 'other' sparse matrix
var sparseOther = other as SparseMatrix;
var startIndexOther = sparseOther._rowIndex[i];
var endIndexOther = i < sparseOther._rowIndex.Length - 1 ? sparseOther._rowIndex[i + 1] : sparseOther.NonZerosCount;
// Look for 'forgotten' components
for (var j = startIndexOther; j < endIndexOther; j++)
{
var realColumnIndex = sparseOther._columnIndices[j];
if (At(i, realColumnIndex) == 0.0)
{
result.At(i, realColumnIndex, sparseOther._nonZeroValues[j]);
// VARIANT for DoSubtract(): result.At(i, realColumnIndex, -sparseOther._nonZeroValues[j]);
}
}
This worked for me, so I hope this will help you too. And keep up the good work!
Cheers,
Comments: (Not sure why Codeplex keeps reopening these issues all the time)