Skip to content
Open
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
1 change: 1 addition & 0 deletions src/MultivariatePolynomials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Abstract type for a polynomial of coefficient type `T`, i.e. a sum of `AbstractT
abstract type AbstractPolynomial{T} <: AbstractPolynomialLike{T} end

const _APL{T} = AbstractPolynomialLike{T}
const _Constant = Union{Number,MA.AbstractMutable,AbstractArray}

include("zip.jl")
include("lazy_iterators.jl")
Expand Down
14 changes: 4 additions & 10 deletions src/comparison.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,8 @@ function Base.isone(p::AbstractPolynomial)
return isone(nterms(p)) && isone(first(terms(p)))
end

# See https://github.com/blegat/MultivariatePolynomials.jl/issues/22
# avoids the call to be transfered to left_constant_eq
Base.:(==)(α::Nothing, x::_APL) = false
Base.:(==)(x::_APL, α::Nothing) = false
Base.:(==)(α::Dict, x::_APL) = false
Base.:(==)(x::_APL, α::Dict) = false
Base.:(==)(α::Nothing, x::RationalPoly) = false
Base.:(==)(x::RationalPoly, α::Nothing) = false
Base.:(==)(α::Dict, x::RationalPoly) = false
Base.:(==)(x::RationalPoly, α::Dict) = false

Expand Down Expand Up @@ -133,16 +127,16 @@ Base.:(==)(p::RationalPoly, q::RationalPoly) = p.num * q.den == q.num * p.den
# Solve ambiguity with (::PolyType, ::Any)
Base.:(==)(p::_APL, q::RationalPoly) = p * q.den == q.num
Base.:(==)(q::RationalPoly, p::_APL) = p == q
Base.:(==)(α, q::RationalPoly) = α * q.den == q.num
Base.:(==)(q::RationalPoly, α) = α == q
Base.:(==)(α::_Constant, q::RationalPoly) = α * q.den == q.num
Base.:(==)(q::RationalPoly, α::_Constant) = α == q
function Base.isequal(p::RationalPoly, q::RationalPoly)
return isequal(p.num * q.den, q.num * p.den)
end
# Solve ambiguity with (::PolyType, ::Any)
Base.isequal(p::_APL, q::RationalPoly) = isequal(p * q.den, q.num)
Base.isequal(q::RationalPoly, p::_APL) = isequal(p, q)
Base.isequal(α, q::RationalPoly) = isequal(α * q.den, q.num)
Base.isequal(q::RationalPoly, α) = isequal(α, q)
Base.isequal(α::_Constant, q::RationalPoly) = isequal(α * q.den, q.num)
Base.isequal(q::RationalPoly, α::_Constant) = isequal(α, q)

# α could be a JuMP affine expression
isapproxzero(α; ztol::Real = 0.0) = false
Expand Down
6 changes: 4 additions & 2 deletions src/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,20 @@ for (op, fun) in [
(:+, :right_constant_plus),
(:-, :right_constant_minus),
(:*, :right_constant_mult),
(:(==), :right_constant_eq),
]
@eval Base.$op(p::_APL, α) = $fun(p, α)
end
Base.:(==)(p::_APL, α::_Constant) = right_constant_eq(p, α)

for (op, fun) in [
(:+, :left_constant_plus),
(:-, :left_constant_minus),
(:*, :left_constant_mult),
(:(==), :left_constant_eq),
]
@eval Base.$op(α, p::_APL) = $fun(α, p)
end
Base.:(==)(α::_Constant, p::_APL) = left_constant_eq(α, p)

## Fix ambiguity between above methods and methods in MA
Base.:+(::MA.Zero, p::_APL) = MA.copy_if_mutable(p)
Base.:+(p::_APL, ::MA.Zero) = MA.copy_if_mutable(p)
Expand Down
11 changes: 11 additions & 0 deletions test/commutative/comparison.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,27 @@
@testset "RationalPoly equality" begin
Mod.@polyvar x y
@test (x^2 - x - 6) / (x + 2) != x + 3
@test !isequal((x^2 - x - 6) / (x + 2), x + 3)
@test x - 3 == (x^2 - x - 6) / (x + 2)
@test isequal(x - 3, (x^2 - x - 6) / (x + 2))
@test (x^2 - x - 6) / (x - 3) == x + 2
@test isequal((x^2 - x - 6) / (x - 3), x + 2)
@test 3 != 4x / 2x
@test !isequal(3, 4x / 2x)
@test 4x / 2x == 2
@test isequal(4x / 2x, 2)
@test 3 != 4x / 2x
@test !isequal(3, 4x / 2x)
@test 1 - 1 / x == (x - 1) / x
@test isequal(1 - 1 / x, (x - 1) / x)
@test 1 - 1 / x != (1 - x) / x
@test !isequal(1 - 1 / x, (1 - x) / x)
@test x + x / x == 1 + x^2 / x
@test isequal(x + x / x, 1 + x^2 / x)
@test x - x / x == -(1 - x^2 / x)
@test isequal(x - x / x, -(1 - x^2 / x))
@test (1 + x) / x - 1 == 1 / x
@test isequal((1 + x) / x - 1, 1 / x)
@test isapprox((1 + 1e-8)x, (x * y) / y, rtol = 1e-7)
@test isapproxzero(((1 + 1e-8)x - x) / y, ztol = 1e-7)
@test !isapproxzero(((1 + 1e-8)x - y) / y, ztol = 1e-9)
Expand Down