Skip to content

Commit 421fc51

Browse files
authored
fix == of different types (#410)
* fix == of different types * oops * promote before == or approx
1 parent 6a6be69 commit 421fc51

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

src/common.jl

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,23 +1025,41 @@ Base.rem(n::AbstractPolynomial, d::AbstractPolynomial) = divrem(n, d)[2]
10251025
#=
10261026
Comparisons =#
10271027
Base.isequal(p1::P, p2::P) where {P <: AbstractPolynomial} = hash(p1) == hash(p2)
1028-
function Base.:(==)(p1::AbstractPolynomial, p2::AbstractPolynomial)
1029-
check_same_variable(p1,p2) || return false
1028+
function Base.:(==)(p1::P, p2::P) where {P <: AbstractPolynomial}
10301029
iszero(p1) && iszero(p2) && return true
10311030
eachindex(p1) == eachindex(p2) || return false
10321031
# coeffs(p1) == coeffs(p2), but non-allocating
10331032
p1val = (p1[i] for i in eachindex(p1))
10341033
p2val = (p2[i] for i in eachindex(p2))
10351034
all(((a,b),) -> a == b, zip(p1val, p2val))
10361035
end
1036+
function Base.:(==)(p1::AbstractPolynomial, p2::AbstractPolynomial)
1037+
if isconstant(p1)
1038+
isconstant(p2) && return constantterm(p1) == constantterm(p2)
1039+
return false
1040+
elseif isconstant(p2)
1041+
return false # p1 is not constant
1042+
end
1043+
check_same_variable(p1, p2) || return false
1044+
==(promote(p1,p2)...)
1045+
end
10371046
Base.:(==)(p::AbstractPolynomial, n::Number) = degree(p) <= 0 && constantterm(p) == n
10381047
Base.:(==)(n::Number, p::AbstractPolynomial) = p == n
10391048

1049+
function Base.isapprox(p1::AbstractPolynomial, p2::AbstractPolynomial; kwargs...)
1050+
if isconstant(p1)
1051+
isconstant(p2) && return constantterm(p1) == constantterm(p2)
1052+
return false
1053+
elseif isconstant(p2)
1054+
return false
1055+
end
1056+
isapprox(promote(p1, p2)...; kwargs...)
1057+
end
1058+
10401059
function Base.isapprox(p1::AbstractPolynomial{T,X},
1041-
p2::AbstractPolynomial{S,Y};
1042-
rtol::Real = (Base.rtoldefault(T, S, 0)),
1043-
atol::Real = 0,) where {T,X,S,Y}
1044-
assert_same_variable(p1, p2)
1060+
p2::AbstractPolynomial{T,X};
1061+
rtol::Real = (Base.rtoldefault(T,T,0)),
1062+
atol::Real = 0,) where {T,X}
10451063
(hasnan(p1) || hasnan(p2)) && return false # NaN poisons comparisons
10461064
# copy over from abstractarray.jl
10471065
Δ = norm(p1-p2)

test/StandardBasis.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,11 @@ end
583583
@test_throws ArgumentError variable(P, :x) variable(P, :y)
584584

585585
end
586+
587+
## Issue 408
588+
p = Polynomial([1,2,3])
589+
q = ChebyshevT([1,2,3])
590+
@test p != q
586591
end
587592

588593
@testset "Fitting" begin

0 commit comments

Comments
 (0)