Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions test/comp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,9 @@ 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
@test (NaN + p) != (NaN + p)
@test isequal(NaN + p, NaN + p)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also check that it's allocation-free ? Sometimes Julia decides to not specialize on function arguments so if you add q = NaN + p and then @test 0 == @allocated q != q and @test 0 == @allocated isequal(q, q), it should make sure of that

end
Loading