Browse Source

Sparse Linear Algebra: complex vectors now override right Equals. gh-20.

Previously sparse vectors for Complex and Complex32 did override
Object.Equals instead of the generic IEquatable<Vector<T>>.Equals.
Now only overrides the generic version to ensure that the right version
is called in all code paths.
pull/36/head
Christoph Ruegg 15 years ago
parent
commit
7b47fb50e3
  1. 79
      src/Numerics/LinearAlgebra/Complex/SparseVector.cs
  2. 79
      src/Numerics/LinearAlgebra/Complex32/SparseVector.cs

79
src/Numerics/LinearAlgebra/Complex/SparseVector.cs

@ -1271,28 +1271,65 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
} }
/// <summary> /// <summary>
/// Check equality. If this is regular vector, then check by base implementation. If Sparse - use own method. /// Returns a hash code for this instance.
/// </summary> /// </summary>
/// <param name="obj">Object to compare</param>
/// <returns> /// <returns>
/// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>. /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns> /// </returns>
public override bool Equals(object obj) public override int GetHashCode()
{ {
var sparseVector = obj as SparseVector; var hashNum = Math.Min(NonZerosCount, 20);
long hash = 0;
for (var i = 0; i < hashNum; i++)
{
#if SILVERLIGHT
hash ^= Precision.DoubleToInt64Bits(this._nonZeroValues[i].GetHashCode());
#else
hash ^= BitConverter.DoubleToInt64Bits(_nonZeroValues[i].GetHashCode());
#endif
}
if (sparseVector == null) return BitConverter.ToInt32(BitConverter.GetBytes(hash), 4);
}
#endregion
/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">
/// An object to compare with this object.
/// </param>
/// <returns>
/// <c>true</c> if the current object is equal to the <paramref name="other"/> parameter; otherwise, <c>false</c>.
/// </returns>
public override bool Equals(Vector<Complex> other)
{
// Reject equality when the argument is null or has a different length.
if (other == null)
{ {
return base.Equals(obj); return false;
}
if (Count != other.Count)
{
return false;
} }
// Accept if the argument is the same object as this. // Accept if the argument is the same object as this.
if (ReferenceEquals(this, sparseVector)) if (ReferenceEquals(this, other))
{ {
return true; return true;
} }
if ((Count != sparseVector.Count) || (NonZerosCount != sparseVector.NonZerosCount)) var sparseVector = other as SparseVector;
if (sparseVector == null)
{
return base.Equals(other);
}
if (NonZerosCount != sparseVector.NonZerosCount)
{ {
return false; return false;
} }
@ -1309,30 +1346,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return true; return true;
} }
/// <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(NonZerosCount, 20);
long hash = 0;
for (var i = 0; i < hashNum; i++)
{
#if SILVERLIGHT
hash ^= Precision.DoubleToInt64Bits(this._nonZeroValues[i].GetHashCode());
#else
hash ^= BitConverter.DoubleToInt64Bits(_nonZeroValues[i].GetHashCode());
#endif
}
return BitConverter.ToInt32(BitConverter.GetBytes(hash), 4);
}
#endregion
/// <summary> /// <summary>
/// Returns an <see cref="IEnumerator{T}"/> that contains the position and value of the element. /// Returns an <see cref="IEnumerator{T}"/> that contains the position and value of the element.
/// </summary> /// </summary>

79
src/Numerics/LinearAlgebra/Complex32/SparseVector.cs

@ -1301,28 +1301,65 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
} }
/// <summary> /// <summary>
/// Check equality. If this is regular vector, then check by base implementation. If Sparse - use own method. /// Returns a hash code for this instance.
/// </summary> /// </summary>
/// <param name="obj">Object to compare</param>
/// <returns> /// <returns>
/// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>. /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns> /// </returns>
public override bool Equals(object obj) public override int GetHashCode()
{ {
var sparseVector = obj as SparseVector; var hashNum = Math.Min(NonZerosCount, 20);
long hash = 0;
for (var i = 0; i < hashNum; i++)
{
#if SILVERLIGHT
hash ^= Precision.DoubleToInt64Bits(this._nonZeroValues[i].GetHashCode());
#else
hash ^= BitConverter.DoubleToInt64Bits(_nonZeroValues[i].GetHashCode());
#endif
}
if (sparseVector == null) return BitConverter.ToInt32(BitConverter.GetBytes(hash), 4);
}
#endregion
/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">
/// An object to compare with this object.
/// </param>
/// <returns>
/// <c>true</c> if the current object is equal to the <paramref name="other"/> parameter; otherwise, <c>false</c>.
/// </returns>
public override bool Equals(Vector<Complex32> other)
{
// Reject equality when the argument is null or has a different length.
if (other == null)
{ {
return base.Equals(obj); return false;
}
if (Count != other.Count)
{
return false;
} }
// Accept if the argument is the same object as this. // Accept if the argument is the same object as this.
if (ReferenceEquals(this, sparseVector)) if (ReferenceEquals(this, other))
{ {
return true; return true;
} }
if ((Count != sparseVector.Count) || (NonZerosCount != sparseVector.NonZerosCount)) var sparseVector = other as SparseVector;
if (sparseVector == null)
{
return base.Equals(other);
}
if (NonZerosCount != sparseVector.NonZerosCount)
{ {
return false; return false;
} }
@ -1339,30 +1376,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return true; return true;
} }
/// <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(NonZerosCount, 20);
long hash = 0;
for (var i = 0; i < hashNum; i++)
{
#if SILVERLIGHT
hash ^= Precision.DoubleToInt64Bits(this._nonZeroValues[i].GetHashCode());
#else
hash ^= BitConverter.DoubleToInt64Bits(_nonZeroValues[i].GetHashCode());
#endif
}
return BitConverter.ToInt32(BitConverter.GetBytes(hash), 4);
}
#endregion
/// <summary> /// <summary>
/// Returns an <see cref="IEnumerator{T}"/> that contains the position and value of the element. /// Returns an <see cref="IEnumerator{T}"/> that contains the position and value of the element.
/// </summary> /// </summary>

Loading…
Cancel
Save