diff --git a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs
index 318bacd9..952ca8b7 100644
--- a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs
+++ b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs
@@ -748,7 +748,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
{
sparseResult.NonZerosCount = NonZerosCount;
sparseResult._nonZeroIndices = new int[NonZerosCount];
- Buffer.BlockCopy(_nonZeroIndices, 0, sparseResult._nonZeroIndices, 0, _nonZeroIndices.Length * Constants.SizeOfInt);
+ Buffer.BlockCopy(_nonZeroIndices, 0, sparseResult._nonZeroIndices, 0, NonZerosCount * Constants.SizeOfInt);
sparseResult._nonZeroValues = new Complex[_nonZeroValues.Length];
}
diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs
index 753dc646..8fbe134e 100644
--- a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs
+++ b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs
@@ -778,7 +778,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
{
sparseResult.NonZerosCount = NonZerosCount;
sparseResult._nonZeroIndices = new int[NonZerosCount];
- Buffer.BlockCopy(_nonZeroIndices, 0, sparseResult._nonZeroIndices, 0, _nonZeroIndices.Length * Constants.SizeOfInt);
+ Buffer.BlockCopy(_nonZeroIndices, 0, sparseResult._nonZeroIndices, 0, NonZerosCount * Constants.SizeOfInt);
sparseResult._nonZeroValues = new Complex32[_nonZeroValues.Length];
}
diff --git a/src/Numerics/LinearAlgebra/Double/SparseVector.cs b/src/Numerics/LinearAlgebra/Double/SparseVector.cs
index d0d62eda..39c89fb7 100644
--- a/src/Numerics/LinearAlgebra/Double/SparseVector.cs
+++ b/src/Numerics/LinearAlgebra/Double/SparseVector.cs
@@ -722,7 +722,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{
sparseResult.NonZerosCount = NonZerosCount;
sparseResult._nonZeroIndices = new int[NonZerosCount];
- Buffer.BlockCopy(_nonZeroIndices, 0, sparseResult._nonZeroIndices, 0, _nonZeroIndices.Length * Constants.SizeOfInt);
+ Buffer.BlockCopy(_nonZeroIndices, 0, sparseResult._nonZeroIndices, 0, NonZerosCount * Constants.SizeOfInt);
sparseResult._nonZeroValues = new double[_nonZeroValues.Length];
}
diff --git a/src/Numerics/LinearAlgebra/Single/SparseVector.cs b/src/Numerics/LinearAlgebra/Single/SparseVector.cs
index ac5e8374..148da12b 100644
--- a/src/Numerics/LinearAlgebra/Single/SparseVector.cs
+++ b/src/Numerics/LinearAlgebra/Single/SparseVector.cs
@@ -730,7 +730,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
{
sparseResult.NonZerosCount = NonZerosCount;
sparseResult._nonZeroIndices = new int[NonZerosCount];
- Buffer.BlockCopy(_nonZeroIndices, 0, sparseResult._nonZeroIndices, 0, _nonZeroIndices.Length * Constants.SizeOfInt);
+ Buffer.BlockCopy(_nonZeroIndices, 0, sparseResult._nonZeroIndices, 0, NonZerosCount * Constants.SizeOfInt);
sparseResult._nonZeroValues = new float[_nonZeroValues.Length];
}
diff --git a/src/UnitTests/LinearAlgebraTests/Complex/SparseVectorTest.cs b/src/UnitTests/LinearAlgebraTests/Complex/SparseVectorTest.cs
index 724feaff..bae3cc3b 100644
--- a/src/UnitTests/LinearAlgebraTests/Complex/SparseVectorTest.cs
+++ b/src/UnitTests/LinearAlgebraTests/Complex/SparseVectorTest.cs
@@ -373,5 +373,25 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
Assert.AreEqual(2, result.NonZerosCount);
}
+
+ ///
+ /// Test for issues #52. When setting previous non-zero values to zero,
+ /// DoMultiply would copy non-zero values to the result, but use the
+ /// length of nonzerovalues instead of NonZerosCount.
+ ///
+ [Test]
+ public void CanScaleAVectorWhenSettingPreviousNonzeroElementsToZero()
+ {
+ var vector = new SparseVector(20);
+ vector[10] = 1.0;
+ vector[11] = 2.0;
+ vector[11] = 0.0;
+
+ var scaled = new SparseVector(20);
+ vector.Multiply(3.0, scaled);
+
+ Assert.AreEqual(3.0, scaled[10].Real);
+ Assert.AreEqual(0.0, scaled[11].Real);
+ }
}
}
diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/SparseVectorTest.cs b/src/UnitTests/LinearAlgebraTests/Complex32/SparseVectorTest.cs
index 765770cd..b0746766 100644
--- a/src/UnitTests/LinearAlgebraTests/Complex32/SparseVectorTest.cs
+++ b/src/UnitTests/LinearAlgebraTests/Complex32/SparseVectorTest.cs
@@ -373,5 +373,25 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
Assert.AreEqual(2, result.NonZerosCount);
}
+
+ ///
+ /// Test for issues #52. When setting previous non-zero values to zero,
+ /// DoMultiply would copy non-zero values to the result, but use the
+ /// length of nonzerovalues instead of NonZerosCount.
+ ///
+ [Test]
+ public void CanScaleAVectorWhenSettingPreviousNonzeroElementsToZero()
+ {
+ var vector = new SparseVector(20);
+ vector[10] = 1.0f;
+ vector[11] = 2.0f;
+ vector[11] = 0.0f;
+
+ var scaled = new SparseVector(20);
+ vector.Multiply(3.0f, scaled);
+
+ Assert.AreEqual(3.0f, scaled[10].Real);
+ Assert.AreEqual(0.0f, scaled[11].Real);
+ }
}
}
diff --git a/src/UnitTests/LinearAlgebraTests/Double/SparseVectorTest.cs b/src/UnitTests/LinearAlgebraTests/Double/SparseVectorTest.cs
index df81ebf7..52b8bcf5 100644
--- a/src/UnitTests/LinearAlgebraTests/Double/SparseVectorTest.cs
+++ b/src/UnitTests/LinearAlgebraTests/Double/SparseVectorTest.cs
@@ -399,5 +399,25 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
}
}
}
+
+ ///
+ /// Test for issues #52. When setting previous non-zero values to zero,
+ /// DoMultiply would copy non-zero values to the result, but use the
+ /// length of nonzerovalues instead of NonZerosCount.
+ ///
+ [Test]
+ public void CanScaleAVectorWhenSettingPreviousNonzeroElementsToZero()
+ {
+ var vector = new SparseVector(20);
+ vector[10] = 1.0;
+ vector[11] = 2.0;
+ vector[11] = 0.0;
+
+ var scaled = new SparseVector(20);
+ vector.Multiply(3.0, scaled);
+
+ Assert.AreEqual(3.0, scaled[10]);
+ Assert.AreEqual(0.0, scaled[11]);
+ }
}
}
diff --git a/src/UnitTests/LinearAlgebraTests/Single/SparseVectorTest.cs b/src/UnitTests/LinearAlgebraTests/Single/SparseVectorTest.cs
index 6bef3733..435632e0 100644
--- a/src/UnitTests/LinearAlgebraTests/Single/SparseVectorTest.cs
+++ b/src/UnitTests/LinearAlgebraTests/Single/SparseVectorTest.cs
@@ -372,5 +372,25 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
Assert.AreEqual(2, result.NonZerosCount);
}
+
+ ///
+ /// Test for issues #52. When setting previous non-zero values to zero,
+ /// DoMultiply would copy non-zero values to the result, but use the
+ /// length of nonzerovalues instead of NonZerosCount.
+ ///
+ [Test]
+ public void CanScaleAVectorWhenSettingPreviousNonzeroElementsToZero()
+ {
+ var vector = new SparseVector(20);
+ vector[10] = 1.0f;
+ vector[11] = 2.0f;
+ vector[11] = 0.0f;
+
+ var scaled = new SparseVector(20);
+ vector.Multiply(3.0f, scaled);
+
+ Assert.AreEqual(3.0f, scaled[10]);
+ Assert.AreEqual(0.0f, scaled[11]);
+ }
}
}