|
From: <in...@dn...> - 2006-01-25 16:47:47
|
Author: patrick
Date: 2006-01-25 11:47:33 -0500 (Wed, 25 Jan 2006)
New Revision: 194
Modified:
trunk/LinearAlgebra/SparseVector.cs
trunk/UnitTests/LinearAlgebra/AbstractVectorTest.cs
trunk/UnitTests/LinearAlgebra/SparseVectorTest.cs
Log:
- Fixed bugs in the SparseVector implementation.
- Fixed bugs in the AbstractVectorTest implementation.
Modified: trunk/LinearAlgebra/SparseVector.cs
===================================================================
--- trunk/LinearAlgebra/SparseVector.cs 2006-01-25 13:59:09 UTC (rev 193)
+++ trunk/LinearAlgebra/SparseVector.cs 2006-01-25 16:47:33 UTC (rev 194)
@@ -77,7 +77,11 @@
}
mSize = source.mSize;
mValueCount = source.mValueCount;
+
+ mValues = new double[source.mValues.Length];
Buffer.BlockCopy(source.mValues, 0, mValues, 0, source.mValues.Length*Constants.SizeOfDouble);
+
+ mIndices = new int[source.mIndices.Length];
Buffer.BlockCopy(source.mIndices, 0, mIndices, 0, source.mIndices.Length*Constants.SizeOfInt);
}
@@ -115,6 +119,11 @@
{
get
{
+ if ((index < 0) || (index >= mSize))
+ {
+ throw new IndexOutOfRangeException();
+ }
+
int itemIndex;
if (FindItem(index, out itemIndex))
{
@@ -124,6 +133,11 @@
}
set
{
+ if ((index < 0) || (index >= mSize))
+ {
+ throw new IndexOutOfRangeException();
+ }
+
int itemIndex;
if (FindItem(index, out itemIndex))
{
@@ -583,7 +597,7 @@
for (int i = 1; i < mValueCount; i++)
{
double test = mValues[i];
- if (test > max)
+ if (test < max)
{
index = mIndices[i];
max = test;
@@ -620,7 +634,7 @@
target.Clear();
for (int i = 0; i < mValueCount; i++)
{
- target[i] = mValues[mIndices[i]];
+ target[mIndices[i]] = mValues[i];
}
}
@@ -655,7 +669,7 @@
if (other.Count != mSize)
{
- throw new ArgumentOutOfRangeException("DenseVector lengths are unequal");
+ throw new NotConformableException();
}
double result = 0;
@@ -778,7 +792,7 @@
int internalIndex;
if (FindItem(i, out internalIndex))
{
- ret.InsertValue(i, mValues[internalIndex]);
+ ret.InsertValue(j, mValues[internalIndex]);
}
}
return ret;
@@ -1282,9 +1296,9 @@
// the fact that we are a sparse vector. Multiply a whole row in one go.
for (int i = 0; i < Count; i++)
{
- for (int j = 0; i < rightSide.Count; j++)
+ for (int j = 0; j < rightSide.Count; j++)
{
- result[i, j] = this[i]*rightSide[j];
+ result[i,j] = this[i]*rightSide[j];
}
}
}
Modified: trunk/UnitTests/LinearAlgebra/AbstractVectorTest.cs
===================================================================
--- trunk/UnitTests/LinearAlgebra/AbstractVectorTest.cs 2006-01-25 13:59:09 UTC (rev 193)
+++ trunk/UnitTests/LinearAlgebra/AbstractVectorTest.cs 2006-01-25 16:47:33 UTC (rev 194)
@@ -339,11 +339,10 @@
public void GetEnumerator()
{
Vector vector = GetVector(testArray);
- int i = 0;
- foreach (KeyValuePair<int, double> pair in vector)
- {
- Assert.AreEqual(testArray[i++], pair.Value);
- }
+ foreach (KeyValuePair<int, double> pair in vector)
+ {
+ Assert.AreEqual(testArray[pair.Key], pair.Value);
+ }
}
[Test]
@@ -353,12 +352,13 @@
Vector vector = GetVector(testArray);
IEnumerator<KeyValuePair<int, double>> iterator = vector.GetEnumerator(range);
- int i = 1;
while (iterator.MoveNext())
{
- Assert.AreEqual(testArray[i], iterator.Current.Value);
- i += 2;
-
+ KeyValuePair<int, double> pair = iterator.Current;
+ // Check that the key (int) is in the range.
+ Assert.AreEqual(0, (pair.Key - range.Start) % range.Stride);
+ // Check that the value is the correct value
+ Assert.AreEqual(testArray[pair.Key], pair.Value);
}
}
Modified: trunk/UnitTests/LinearAlgebra/SparseVectorTest.cs
===================================================================
--- trunk/UnitTests/LinearAlgebra/SparseVectorTest.cs 2006-01-25 13:59:09 UTC (rev 193)
+++ trunk/UnitTests/LinearAlgebra/SparseVectorTest.cs 2006-01-25 16:47:33 UTC (rev 194)
@@ -4,7 +4,7 @@
namespace dnAnalytics.UnitTests.LinearAlgebra
{
[TestFixture]
- public sealed class DoubleSparseVectorTest : AbstractVectorTest
+ public sealed class SparseVectorTest : AbstractVectorTest
{
public override Vector GetVector(int order)
{
|