Skip to content

Commit ec819d8

Browse files
authored
Merge pull request #136 from olof3/pull-request/f2ad20a3
reltol->rtol, abstol->atol for consistency with Base.
2 parents 992bfaf + f2ad20a commit ec819d8

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

src/Polynomials.jl

+23-23
Original file line numberDiff line numberDiff line change
@@ -200,30 +200,30 @@ variable(p::Poly{T}) where {T} = variable(T, p.var)
200200
variable(var::SymbolLike=:x) = variable(Float64, var)
201201

202202
"""
203-
truncate{T}(p::Poly{T}; reltol::Real = Base.rtoldefault(real(T)), abstol::Real = 0)
203+
truncate{T}(p::Poly{T}; rtol::Real = Base.rtoldefault(real(T)), atol::Real = 0)
204204
205-
Return a polynomial with coefficients `a_i` truncated to zero if `|a_i| <= reltol*maxabs(a)+abstol`.
205+
Return a polynomial with coefficients `a_i` truncated to zero if `|a_i| <= rtol*maxabs(a)+atol`.
206206
"""
207-
function truncate(p::Poly{T}; reltol::Real = Base.rtoldefault(real(T)),
208-
abstol::Real = 0) where {T}
207+
function truncate(p::Poly{T}; rtol::Real = Base.rtoldefault(real(T)),
208+
atol::Real = 0) where {T}
209209
a = coeffs(p)
210210
amax = maximum(abs,a)
211-
thresh = amax * reltol + abstol
211+
thresh = amax * rtol + atol
212212
anew = map(ai -> abs(ai) <= thresh ? zero(T) : ai, a)
213213
return Poly(anew, p.var)
214214
end
215215

216216
"""
217-
chop(p::Poly{T}; reltol::Real = Base.rtoldefault(real(T)), abstol::Real = 0)
217+
chop(p::Poly{T}; rtol::Real = Base.rtoldefault(real(T)), atol::Real = 0)
218218
219219
Chop off leading values from a polynomial which are approximately zero. The tolerances
220-
`reltol` and `abstol` are passed to `isapprox` to check for zeros.
220+
`rtol` and `atol` are passed to `isapprox` to check for zeros.
221221
"""
222-
function chop(p::Poly{T}; reltol::Real = Base.rtoldefault(real(T)),
223-
abstol::Real = 0) where {T}
222+
function chop(p::Poly{T}; rtol::Real = Base.rtoldefault(real(T)),
223+
atol::Real = 0) where {T}
224224
c = copy(p.a)
225225
for k=length(c):-1:1
226-
if !isapprox(c[k], zero(T); rtol=reltol, atol=abstol)
226+
if !isapprox(c[k], zero(T); rtol=rtol, atol=atol)
227227
resize!(c, k)
228228
return Poly(c, p.var)
229229
end
@@ -396,29 +396,29 @@ rem(num::Poly, den::Poly) = divrem(num, den)[2]
396396
==(n::Number, p1::Poly) = (p1 == n)
397397

398398
"""
399-
isapprox{T,S}(p1::Poly{T}, p2::Poly{S}; reltol::Real = Base.rtoldefault(T,S, 0), abstol::Real = 0, norm::Function = vecnorm)
399+
isapprox{T,S}(p1::Poly{T}, p2::Poly{S}; rtol::Real = Base.rtoldefault(T,S, 0), atol::Real = 0, norm::Function = vecnorm)
400400
401401
Truncate polynomials `p1` and `p2`, and compare the coefficient vectors using the
402-
given `norm` function. The tolerances `reltol` and `abstol` are passed to both
402+
given `norm` function. The tolerances `rtol` and `atol` are passed to both
403403
`truncate` and `isapprox`.
404404
"""
405405
function isapprox(p1::Poly{T}, p2::Poly{S};
406-
reltol::Real = (@compat Base.rtoldefault(T,S, 0)), abstol::Real = 0, norm::Function = vecnorm) where {T,S}
406+
rtol::Real = (@compat Base.rtoldefault(T,S, 0)), atol::Real = 0, norm::Function = vecnorm) where {T,S}
407407
p1.var == p2.var || error("Polynomials must have same variable")
408-
p1t = truncate(p1; reltol = reltol, abstol = abstol)
409-
p2t = truncate(p2; reltol = reltol, abstol = abstol)
410-
length(p1t) == length(p2t) && isapprox(coeffs(p1t), coeffs(p2t); rtol = reltol,
411-
atol = abstol, norm = norm)
408+
p1t = truncate(p1; rtol = rtol, atol = atol)
409+
p2t = truncate(p2; rtol = rtol, atol = atol)
410+
length(p1t) == length(p2t) && isapprox(coeffs(p1t), coeffs(p2t); rtol = rtol,
411+
atol = atol, norm = norm)
412412
end
413413

414-
function isapprox(p1::Poly{T}, n::S; reltol::Real = (@compat Base.rtoldefault(T,S, 0)),
415-
abstol::Real = 0) where {T,S<:Number}
416-
p1t = truncate(p1; reltol = reltol, abstol = abstol)
417-
degree(p1t) == 0 && isapprox(coeffs(p1), [n]; rtol = reltol, atol = abstol)
414+
function isapprox(p1::Poly{T}, n::S; rtol::Real = (@compat Base.rtoldefault(T,S, 0)),
415+
atol::Real = 0) where {T,S<:Number}
416+
p1t = truncate(p1; rtol = rtol, atol = atol)
417+
degree(p1t) == 0 && isapprox(coeffs(p1), [n]; rtol = rtol, atol = atol)
418418
end
419419

420-
isapprox(n::S, p1::Poly{T}; reltol::Real= (@compat Base.rtoldefault(T,S, 0)),
421-
abstol::Real = 0) where {T,S<:Number} = isapprox(p1, n; reltol = reltol, abstol = abstol)
420+
isapprox(n::S, p1::Poly{T}; rtol::Real= (@compat Base.rtoldefault(T,S, 0)),
421+
atol::Real = 0) where {T,S<:Number} = isapprox(p1, n; rtol = rtol, atol = atol)
422422

423423
hash(f::Poly, h::UInt) = hash(f.var, hash(f.a, h))
424424
isequal(p1::Poly, p2::Poly) = hash(p1) == hash(p2)

test/runtests.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,9 @@ psum = p1 + p2 - p3
172172
@test degree(psum) == 1 # will have wrong degree
173173
@test degree(truncate(psum)) == 0 # the degree should be correct after truncation
174174

175-
@test truncate(Poly([2,1]),reltol=1/2,abstol=0) == Poly([2])
176-
@test truncate(Poly([2,1]),reltol=1,abstol=0) == Poly([0])
177-
@test truncate(Poly([2,1]),reltol=0,abstol=1) == Poly([2])
175+
@test truncate(Poly([2,1]),rtol=1/2,atol=0) == Poly([2])
176+
@test truncate(Poly([2,1]),rtol=1,atol=0) == Poly([0])
177+
@test truncate(Poly([2,1]),rtol=0,atol=1) == Poly([2])
178178

179179
@test norm(Poly([1., 2.])) == norm([1., 2.])
180180
@test norm(Poly([1., 2.]), 1) == norm([1., 2.], 1)

0 commit comments

Comments
 (0)