Skip to content

Commit f7d594d

Browse files
committed
adjust isapprox to match usage with Vector{T}
1 parent 7575879 commit f7d594d

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "Polynomials"
33
uuid = "f27b6e38-b328-58d1-80ce-0feddd5e7a45"
44
license = "MIT"
55
author = "JuliaMath"
6-
version = "1.1.0"
6+
version = "1.1.1"
77

88
[deps]
99
Intervals = "d8418881-c3e1-53bb-8760-2df7ec849ed5"

src/common.jl

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -561,26 +561,28 @@ Base.:(==)(n::Number, p::AbstractPolynomial) = p == n
561561
function Base.isapprox(p1::AbstractPolynomial{T},
562562
p2::AbstractPolynomial{S};
563563
rtol::Real = (Base.rtoldefault(T, S, 0)),
564-
atol::Real = 0,) where {T,S}
564+
atol::Real = 0,) where {T,S}
565+
565566
p1, p2 = promote(p1, p2)
566567
check_same_variable(p1, p2) || error("p1 and p2 must have same var")
567-
p1t = truncate(p1; rtol = rtol, atol = atol)
568-
p2t = truncate(p2; rtol = rtol, atol = atol)
569-
if length(p1t) length(p2t)
570-
return false
568+
569+
# copy over from abstractarray.jl
570+
Δ = norm(p1-p2)
571+
if isfinite(Δ)
572+
return Δ <= max(atol, rtol*max(norm(p1), norm(p2)))
573+
else
574+
for i in 0:max(degree(p1), degree(p2))
575+
isapprox(p1[i], p2[i]; rtol=rtol, atol=atol) || return false
576+
end
577+
return true
571578
end
572-
isapprox(coeffs(p1t), coeffs(p2t), rtol = rtol, atol = atol)
573579
end
574580

575-
function Base.isapprox(p1::AbstractPolynomial{T},
581+
function Base.isapprox(p1::P,
576582
n::S;
577583
rtol::Real = (Base.rtoldefault(T, S, 0)),
578-
atol::Real = 0,) where {T,S}
579-
p1t = truncate(p1, rtol = rtol, atol = atol)
580-
if length(p1t) != 1
581-
return false
582-
end
583-
isapprox(coeffs(p1t), [n], rtol = rtol, atol = atol)
584+
atol::Real = 0,) where {T,S, P<:AbstractPolynomial{T}}
585+
return isapprox(p1, (P){T}(n,p1.var))
584586
end
585587

586588
Base.isapprox(n::S,

test/StandardBasis.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,17 @@ end
230230
@test P([0.5]) + 2 == P([2.5])
231231
@test 2 - P([0.5]) == P([1.5])
232232

233+
# check ≈ for P matches usage for Vector{T} (possibly padded with trailing zeros)
234+
@test (P([NaN]) P([NaN])) == ([NaN] [NaN]) # false
235+
@test (P([NaN]) NaN) == (false)
236+
@test (P([Inf]) P([Inf])) == ([Inf] [Inf]) # true
237+
@test (P([Inf]) Inf) == (true)
238+
@test (P([1,Inf]) P([0,Inf])) == ([1,Inf] [0,Inf]) # false
239+
@test (P([1,NaN,Inf]) P([0,NaN, Inf])) == ([1,NaN,Inf] [0,NaN, Inf]) #false
240+
@test (P([eps(), eps()]) P([0,0])) == ([eps(), eps()] [0,0]) # false
241+
@test (P([1,eps(), 1]) P([1,0,1])) == ([1,eps(), 1] [1,0,1]) # true
242+
@test (P([1,2]) P([1,2,eps()])) == ([1,2,0] [1,2,eps()])
243+
233244

234245
# check how ==, ===, isapprox ignore variable mismatch when constants are involved, issue #217, issue #219
235246
@test zero(P, :x) == zero(P, :y)

0 commit comments

Comments
 (0)