Browse Source

vector: added equals and gethashcode methods

Signed-off-by: Marcus Cuda <marcus@cuda.net>
la-knuth
Marcus Cuda 17 years ago
parent
commit
478991b870
  1. 37
      src/Numerics/LinearAlgebra/Double/Matrix.cs
  2. 57
      src/Numerics/LinearAlgebra/Double/Vector.cs
  3. 22
      src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs

37
src/Numerics/LinearAlgebra/Double/Matrix.cs

@ -341,5 +341,42 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentOutOfRangeException("column");
}
}
#region System.Object overrides
/// <summary>
/// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
public override bool Equals(object obj)
{
return Equals(obj as Matrix);
}
/// <summary>
/// Returns a hash code for this instance.
/// </summary>
/// <returns>
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns>
public override int GetHashCode()
{
var hashNum = Math.Min(RowCount * ColumnCount, 25);
long hash = 0;
for (var i = 0; i < hashNum; i++)
{
var col = i % ColumnCount;
var row = (i - col) / RowCount;
hash ^= BitConverter.DoubleToInt64Bits(this[row, col]);
}
return BitConverter.ToInt32(BitConverter.GetBytes(hash), 4);
}
#endregion
}
}

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

@ -829,17 +829,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public override string ToString()
{
return ToString(null, null);
}
#endregion
#region Implemented Interfaces
@ -930,7 +919,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
// If all else fails, perform element wise comparison.
for (var index = 0; index < Count; index++)
{
if (this[index] != other[index])
if (!this[index].AlmostEqual(other[index]))
{
return false;
}
@ -973,5 +962,49 @@ namespace MathNet.Numerics.LinearAlgebra.Double
#endregion
#endregion
#region System.Object overrides
/// <summary>
/// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
public override bool Equals(object obj)
{
return Equals(obj as Vector);
}
/// <summary>
/// Returns a hash code for this instance.
/// </summary>
/// <returns>
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns>
public override int GetHashCode()
{
var hashNum = Math.Min(Count, 20);
long hash = 0;
for (var i = 0; i < hashNum; i++)
{
hash ^= BitConverter.DoubleToInt64Bits(this[i]);
}
return BitConverter.ToInt32(BitConverter.GetBytes(hash), 4);
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public override string ToString()
{
return ToString(null, null);
}
#endregion
}
}

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

@ -157,6 +157,28 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
Assert.Throws<ArgumentException>(() => CreateVector(0));
}
[Test]
public void TestingForEqualityWithNonVectorReturnsFalse()
{
var vector = CreateVector(_data);
Assert.IsFalse(vector.Equals(2));
}
[Test]
public void CanTestForEqualityUsingObjectEquals()
{
var vector1 = CreateVector(_data);
var vector2 = CreateVector(_data);
Assert.IsTrue(vector1.Equals((object)vector2));
}
[Test]
public void VectorGetHashCode()
{
var vector = CreateVector(new double[] { 1, 2, 3, 4 });
Assert.AreEqual(2145910784, vector.GetHashCode());
}
protected abstract Vector CreateVector(int size);
protected abstract Vector CreateVector(IList<double> data);

Loading…
Cancel
Save