Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/comp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
14 changes: 14 additions & 0 deletions test/comp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading