From 478991b870240dcec9e1fb8daafb9a963e46f03a Mon Sep 17 00:00:00 2001 From: Marcus Cuda Date: Thu, 20 Aug 2009 15:20:12 +0800 Subject: [PATCH] vector: added equals and gethashcode methods Signed-off-by: Marcus Cuda --- src/Numerics/LinearAlgebra/Double/Matrix.cs | 37 ++++++++++++ src/Numerics/LinearAlgebra/Double/Vector.cs | 57 +++++++++++++++---- .../LinearAlgebraTests/Double/VectorTests.cs | 22 +++++++ 3 files changed, 104 insertions(+), 12 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Double/Matrix.cs b/src/Numerics/LinearAlgebra/Double/Matrix.cs index 1699e284..7bb13c27 100644 --- a/src/Numerics/LinearAlgebra/Double/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Double/Matrix.cs @@ -341,5 +341,42 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentOutOfRangeException("column"); } } + + #region System.Object overrides + + /// + /// Determines whether the specified is equal to this instance. + /// + /// The to compare with this instance. + /// + /// true if the specified is equal to this instance; otherwise, false. + /// + public override bool Equals(object obj) + { + return Equals(obj as Matrix); + } + + /// + /// Returns a hash code for this instance. + /// + /// + /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. + /// + 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 } } \ No newline at end of file diff --git a/src/Numerics/LinearAlgebra/Double/Vector.cs b/src/Numerics/LinearAlgebra/Double/Vector.cs index 9c7c2397..24e60d19 100644 --- a/src/Numerics/LinearAlgebra/Double/Vector.cs +++ b/src/Numerics/LinearAlgebra/Double/Vector.cs @@ -829,17 +829,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double } } - /// - /// Returns a that represents this instance. - /// - /// - /// A that represents this instance. - /// - 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 + + /// + /// Determines whether the specified is equal to this instance. + /// + /// The to compare with this instance. + /// + /// true if the specified is equal to this instance; otherwise, false. + /// + public override bool Equals(object obj) + { + return Equals(obj as Vector); + } + + /// + /// Returns a hash code for this instance. + /// + /// + /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. + /// + 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); + } + + /// + /// Returns a that represents this instance. + /// + /// + /// A that represents this instance. + /// + public override string ToString() + { + return ToString(null, null); + } + #endregion } } \ No newline at end of file diff --git a/src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs b/src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs index 1a95836c..546ed2e5 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs @@ -157,6 +157,28 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double Assert.Throws(() => 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 data);