diff --git a/src/comp.jl b/src/comp.jl index 62ccfbb..16f8697 100644 --- a/src/comp.jl +++ b/src/comp.jl @@ -58,25 +58,28 @@ end (==)(x::MonomialVector, mv::AbstractVector) = x == monomial_vector(mv) # Comparison of Term -function (==)(p::Polynomial{V,M}, q::Polynomial{V,M}) where {V,M} +function _compare(p::Polynomial{V,M}, q::Polynomial{V,M}, comparator) where {V,M} # terms should be sorted and without zeros if MP.nterms(p) != MP.nterms(q) return false end for i in eachindex(p.a) - if p.x[i] != q.x[i] + if !comparator(p.x[i], q.x[i]) # There should not be zero terms @assert p.a[i] != 0 @assert q.a[i] != 0 return false end - if p.a[i] != q.a[i] + if !comparator(p.a[i], q.a[i]) return false end end return true end +(==)(p::Polynomial{V, M}, q::Polynomial{V, M}) where {V, M} = _compare(p, q, (==)) +Base.isequal(p::Polynomial{V, M}, q::Polynomial{V, M}) where {V, M} = _compare(p, q, isequal) + function Base.isapprox( p::Polynomial{V,M,S}, q::Polynomial{V,M,T}; diff --git a/test/comp.jl b/test/comp.jl index 94c526b..6486cb3 100644 --- a/test/comp.jl +++ b/test/comp.jl @@ -97,3 +97,17 @@ end _test_monomials([x, y], 1:2, [y, x, y^2, x * y, x^2]) _test_monomials([x, y], [0, 1, 3], [1, y, x, y^3, x*y^2, x^2*y, x^3]) end + +@testset "Comparison with NaN" begin + @polyvar p + poly1 = NaN + p + poly2 = NaN + p + @test poly1 != poly2 + @test (@allocated poly1 != poly2) == 0 + @test poly1 != poly1 + @test (@allocated poly1 != poly1) == 0 + @test isequal(poly1, poly2) + @test (@allocated isequal(poly1, poly2)) == 0 + @test isequal(poly1, poly1) + @test (@allocated isequal(poly1, poly1)) == 0 +end