Skip to content

fix == of different types #410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 31, 2022
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
30 changes: 24 additions & 6 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1025,23 +1025,41 @@ Base.rem(n::AbstractPolynomial, d::AbstractPolynomial) = divrem(n, d)[2]
#=
Comparisons =#
Base.isequal(p1::P, p2::P) where {P <: AbstractPolynomial} = hash(p1) == hash(p2)
function Base.:(==)(p1::AbstractPolynomial, p2::AbstractPolynomial)
check_same_variable(p1,p2) || return false
function Base.:(==)(p1::P, p2::P) where {P <: AbstractPolynomial}
iszero(p1) && iszero(p2) && return true
eachindex(p1) == eachindex(p2) || return false
# coeffs(p1) == coeffs(p2), but non-allocating
p1val = (p1[i] for i in eachindex(p1))
p2val = (p2[i] for i in eachindex(p2))
all(((a,b),) -> a == b, zip(p1val, p2val))
end
function Base.:(==)(p1::AbstractPolynomial, p2::AbstractPolynomial)
if isconstant(p1)
isconstant(p2) && return constantterm(p1) == constantterm(p2)
return false
elseif isconstant(p2)
return false # p1 is not constant
end
check_same_variable(p1, p2) || return false
==(promote(p1,p2)...)
end
Base.:(==)(p::AbstractPolynomial, n::Number) = degree(p) <= 0 && constantterm(p) == n
Base.:(==)(n::Number, p::AbstractPolynomial) = p == n

function Base.isapprox(p1::AbstractPolynomial, p2::AbstractPolynomial; kwargs...)
if isconstant(p1)
isconstant(p2) && return constantterm(p1) == constantterm(p2)
return false
elseif isconstant(p2)
return false
end
isapprox(promote(p1, p2)...; kwargs...)
end

function Base.isapprox(p1::AbstractPolynomial{T,X},
p2::AbstractPolynomial{S,Y};
rtol::Real = (Base.rtoldefault(T, S, 0)),
atol::Real = 0,) where {T,X,S,Y}
assert_same_variable(p1, p2)
p2::AbstractPolynomial{T,X};
rtol::Real = (Base.rtoldefault(T,T,0)),
atol::Real = 0,) where {T,X}
(hasnan(p1) || hasnan(p2)) && return false # NaN poisons comparisons
# copy over from abstractarray.jl
Δ = norm(p1-p2)
Expand Down
5 changes: 5 additions & 0 deletions test/StandardBasis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,11 @@ end
@test_throws ArgumentError variable(P, :x) ≈ variable(P, :y)

end

## Issue 408
p = Polynomial([1,2,3])
q = ChebyshevT([1,2,3])
@test p != q
end

@testset "Fitting" begin
Expand Down