Skip to content

Commit 5c3d108

Browse files
committed
A Poly object can be used as a function
Just some syntactic sugar. For julia version 0.4 or newer, the `call` method can be overloaded and can be convenient when using Poly objects. This pull request, adds the new `call` method if Julia version is 0.4 or newer. Added a test in test/runtests.jl that only gets executed if version is 0.4 or newer. The tests passed in both version 0.3 and 0.4.
1 parent 449a06c commit 5c3d108

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/Polynomials.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ export Pade, padeval
1111
import Base: length, endof, getindex, setindex!, copy, zero, one, convert
1212
import Base: show, print, *, /, //, -, +, ==, divrem, rem, eltype
1313
import Base: promote_rule
14+
if VERSION >= v"0.4"
15+
import Base.call
16+
end
1417

1518
eps{T}(::Type{T}) = zero(T)
1619
eps{F<:AbstractFloat}(x::Type{F}) = Base.eps(F)
@@ -261,6 +264,10 @@ end
261264

262265
polyval(p::Poly, v::AbstractVector) = map(x->polyval(p, x), v)
263266

267+
if VERSION >= v"0.4"
268+
call(p::Poly, x) = polyval(p, x)
269+
end
270+
264271
function polyint{T}(p::Poly{T}, k::Number=0)
265272
n = length(p)
266273
R = typeof(one(T)/1)

test/runtests.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ sprint(show, pNULL)
4646
@test polyder(p3) == Poly([2,2])
4747
@test polyder(p1) == polyder(p0) == polyder(pNULL) == pNULL
4848

49+
if VERSION >= v"0.4"
50+
@test pN(-.125) == 276.9609375
51+
@test pN([0.1, 0.2, 0.3]) == polyval(pN, [0.1, 0.2, 0.3])
52+
end
53+
4954
@test poly([-1,-1]) == p3
5055
@test roots(p0)==roots(p1)==roots(pNULL)==[]
5156
@test roots(p2) == [-1]

0 commit comments

Comments
 (0)