Skip to content

Commit

Permalink
added IAlmostEquatable pattern to real and complex vectors and polyno…
Browse files Browse the repository at this point in the history
…mials as well
  • Loading branch information
cdrnet committed May 24, 2009
1 parent 1d0f020 commit 5e9307a
Show file tree
Hide file tree
Showing 5 changed files with 466 additions and 19 deletions.
107 changes: 102 additions & 5 deletions src/app/MathNet.Iridium/Library/ComplexPolynomial.cs
Expand Up @@ -41,7 +41,9 @@ namespace MathNet.Numerics
/// that is y = c[0]*x^0+c[1]*x^1+c[2]*x^2+...</remarks>
[Serializable]
public class ComplexPolynomial :
IComparable,
IEquatable<ComplexPolynomial>,
IAlmostEquatable<ComplexPolynomial>,
IComparable<ComplexPolynomial>,
ICloneable
{
Complex[] coefficients;
Expand Down Expand Up @@ -1541,25 +1543,120 @@ public override
Equals(object obj)
{
ComplexPolynomial p = obj as ComplexPolynomial;
return object.ReferenceEquals(p, null) ? false : Equals(p);
return object.ReferenceEquals(p, null) ? false : this.Equals(p);
}

/// <summary>
/// Check whether this polynomial is equal to another polynomial.
/// </summary>
public
bool
Equals(ComplexPolynomial polynomial)
Equals(ComplexPolynomial other)
{
return CompareTo(polynomial) == 0;
if(object.ReferenceEquals(other, null)
|| order != other.Order)
{
return false;
}

// compare all values
Complex[] otherData = other.coefficients;
for(int i = 0; i < order; i++)
{
if(!coefficients[i].Equals(otherData[i]))
{
return false;
}
}

return true;
}

/// <summary>
/// Returns true if two polynomials are almost equal, up to the default maximum relative error.
/// </summary>
public
bool
AlmostEquals(ComplexPolynomial other)
{
if(object.ReferenceEquals(other, null)
|| order != other.Order)
{
return false;
}

// compare all values
Complex[] otherData = other.coefficients;
for(int i = 0; i < order; i++)
{
if(!coefficients[i].AlmostEquals(otherData[i]))
{
return false;
}
}

return true;
}

/// <summary>
/// Returns true if two polynomials are almost equal, up to the provided maximum relative error.
/// </summary>
public
bool
AlmostEquals(
ComplexPolynomial other,
double maximumRelativeError)
{
if(object.ReferenceEquals(other, null)
|| order != other.Order)
{
return false;
}

// compare all values
Complex[] otherData = other.coefficients;
for(int i = 0; i < order; i++)
{
if(!coefficients[i].AlmostEquals(otherData[i], maximumRelativeError))
{
return false;
}
}

return true;
}

/// <summary>
/// Returns true if two polynomials are almost equal, up to the provided maximum relative error.
/// </summary>
public static
bool
AlmostEqual(
ComplexPolynomial u,
ComplexPolynomial v,
double maximumRelativeError)
{
return EqualityComparers.AlmostEqual(u, v, maximumRelativeError);
}

/// <summary>
/// Returns true if two polynomials are almost equal, up to the default maximum relative error.
/// </summary>
public static
bool
AlmostEqual(
ComplexPolynomial u,
ComplexPolynomial v)
{
return EqualityComparers.AlmostEqual(u, v);
}

/// <summary>
/// Check whether two polynomials are equal.
/// </summary>
public static
bool
Equals(
Equal(
ComplexPolynomial polynomial1,
ComplexPolynomial polynomial2)
{
Expand Down
98 changes: 98 additions & 0 deletions src/app/MathNet.Iridium/Library/LinearAlgebra/ComplexVector.cs
Expand Up @@ -42,6 +42,8 @@ namespace MathNet.Numerics.LinearAlgebra
public class ComplexVector :
IVector<Complex>,
IList<Complex>,
IEquatable<ComplexVector>,
IAlmostEquatable<ComplexVector>,
ICloneable
{
private Complex[] _data;
Expand Down Expand Up @@ -1453,6 +1455,102 @@ private static
return Create(_data);
}

/// <summary>
/// Indicates whether <c>obj</c> is equal to this instance.
/// </summary>
public override
bool
Equals(object obj)
{
ComplexVector v = obj as ComplexVector;
return object.ReferenceEquals(v, null) ? false : this.Equals(v);
}

/// <summary>
/// Indicates whether <c>other</c> is equal to this matrix.
/// </summary>
public
bool
Equals(ComplexVector other)
{
if(object.ReferenceEquals(other, null)
|| _length != other.Length)
{
return false;
}

// compare all values
Complex[] otherData = other._data;
for(int i = 0; i < _data.Length; i++)
{
if(!_data[i].Equals(otherData[i]))
{
return false;
}
}

return true;
}

/// <summary>
/// Returns true if two vectors are almost equal, up to the default maximum relative error.
/// </summary>
public
bool
AlmostEquals(ComplexVector other)
{
if(object.ReferenceEquals(other, null)
|| _length != other.Length)
{
return false;
}

return Number.AlmostEqualNorm(Norm1(), other.Norm1(), (this - other).Norm1());
}

/// <summary>
/// Returns true if two vectors are almost equal, up to the provided maximum relative error.
/// </summary>
public
bool
AlmostEquals(
ComplexVector other,
double maximumRelativeError)
{
if(object.ReferenceEquals(other, null)
|| _length != other.Length)
{
return false;
}

return Number.AlmostEqualNorm(Norm1(), other.Norm1(), (this - other).Norm1(), maximumRelativeError);
}

/// <summary>
/// Returns true if two vectors are almost equal, up to the provided maximum relative error.
/// </summary>
public static
bool
AlmostEqual(
ComplexVector u,
ComplexVector v,
double maximumRelativeError)
{
return EqualityComparers.AlmostEqual(u, v, maximumRelativeError);
}

/// <summary>
/// Returns true if two vectors are almost equal, up to the default maximum relative error.
/// </summary>
public static
bool
AlmostEqual(
ComplexVector u,
ComplexVector v)
{
return EqualityComparers.AlmostEqual(u, v);
}

/// <summary>
/// Formats this vector to a human-readable string
/// </summary>
Expand Down
83 changes: 78 additions & 5 deletions src/app/MathNet.Iridium/Library/LinearAlgebra/Vector.cs
Expand Up @@ -47,6 +47,8 @@ namespace MathNet.Numerics.LinearAlgebra
public class Vector :
IVector<double>,
IList<double>,
IEquatable<Vector>,
IAlmostEquatable<Vector>,
ICloneable
{
private double[] _data;
Expand Down Expand Up @@ -1107,28 +1109,99 @@ private static
}

/// <summary>
/// Returns true if two vectors are almost equal (with some given relative accuracy).
/// Indicates whether <c>obj</c> is equal to this instance.
/// </summary>
public override
bool
Equals(object obj)
{
Vector v = obj as Vector;
return object.ReferenceEquals(v, null) ? false : this.Equals(v);
}

/// <summary>
/// Indicates whether <c>other</c> is equal to this matrix.
/// </summary>
public
bool
Equals(Vector other)
{
if(object.ReferenceEquals(other, null)
|| _length != other.Length)
{
return false;
}

// compare all values
double[] otherData = other._data;
for(int i = 0; i < _data.Length; i++)
{
if(_data[i] != otherData[i])
{
return false;
}
}

return true;
}

/// <summary>
/// Returns true if two vectors are almost equal, up to the default maximum relative error.
/// </summary>
public
bool
AlmostEquals(Vector other)
{
if(object.ReferenceEquals(other, null)
|| _length != other.Length)
{
return false;
}

return Number.AlmostEqualNorm(Norm1(), other.Norm1(), (this - other).Norm1());
}

/// <summary>
/// Returns true if two vectors are almost equal, up to the provided maximum relative error.
/// </summary>
public
bool
AlmostEquals(
Vector other,
double maximumRelativeError)
{
if(object.ReferenceEquals(other, null)
|| _length != other.Length)
{
return false;
}

return Number.AlmostEqualNorm(Norm1(), other.Norm1(), (this - other).Norm1(), maximumRelativeError);
}

/// <summary>
/// Returns true if two vectors are almost equal, up to the provided maximum relative error.
/// </summary>
public static
bool
AlmostEqual(
Vector u,
Vector v,
double relativeAccuracy)
double maximumRelativeError)
{
return Number.AlmostEqualNorm(u.Norm1(), v.Norm1(), (u - v).Norm1(), relativeAccuracy);
return EqualityComparers.AlmostEqual(u, v, maximumRelativeError);
}

/// <summary>
/// Returns true if two vectors are almost equal.
/// Returns true if two vectors are almost equal, up to the default maximum relative error.
/// </summary>
public static
bool
AlmostEqual(
Vector u,
Vector v)
{
return Number.AlmostEqualNorm(u.Norm1(), v.Norm1(), (u - v).Norm1(), 10 * Number.DefaultRelativeAccuracy);
return EqualityComparers.AlmostEqual(u, v);
}

/// <summary>
Expand Down

0 comments on commit 5e9307a

Please sign in to comment.