Browse Source

vector: added GetIndexedEnumerator

la-knuth
Marcus Cuda 17 years ago
parent
commit
b0a4a23d81
  1. 17
      src/Numerics/LinearAlgebra/Double/Vector.cs
  2. 12
      src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs

17
src/Numerics/LinearAlgebra/Double/Vector.cs

@ -837,7 +837,22 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
#endregion
/// <summary>
/// Returns an <see cref="IEnumerator{T}"/> that contains the position and value of the element.
/// </summary>
/// <returns>An <see cref="IEnumerator{T}"/> over this vector that contains the position and value of each
/// non-zero element.</returns>
/// <remarks>The enumerator returns a <seealso cref="KeyValuePair{T,K}"/> with the key being the element index and the value
/// being the value of the element at that index. For sparse vectors, the enumerator will exclude all elements
/// with a zero value.</remarks>
public virtual IEnumerable<KeyValuePair<int, double>> GetIndexedEnumerator()
{
for (int i = 0; i < Count; i++)
{
yield return new KeyValuePair<int, double>(i, this[i]);
}
}
#region IEquatable<Vector>
/// <summary>

12
src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs

@ -150,7 +150,17 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
var vector = CreateVector(new double[] { 1, 2, 3, 4 });
Assert.AreEqual(2145910784, vector.GetHashCode());
}
[Test]
public void GetIndexedEnumerator()
{
Vector vector = CreateVector(_data);
foreach (KeyValuePair<int, double> pair in vector.GetIndexedEnumerator())
{
Assert.AreEqual(_data[pair.Key], pair.Value);
}
}
protected abstract Vector CreateVector(int size);
protected abstract Vector CreateVector(IList<double> data);

Loading…
Cancel
Save