Skip to content

Commit 992bfaf

Browse files
pjabardoararslan
authored andcommitted
Julia v0.7 compatibility - lastindex/range (#135)
The function `endof` is now `lastindex` and `linspace` was substituted by `range` using keyword arguments. Another change is from `reprmime` to `repr`. In some cases simply using Compat was enough. In some of the tests, the version number was explicitly called: I Compat wasn't enough.
1 parent e7689f7 commit 992bfaf

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

src/Polynomials.jl

+10-7
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ export degree, coeffs, variable, printpoly
1212
export polyval, polyint, polyder, roots, polyfit
1313
export Pade, padeval
1414

15+
import Compat.lastindex
16+
1517
import Base: start, next, done, length, size, eltype, collect, eachindex
16-
import Base: endof, getindex, setindex!, copy, zero, one, convert, gcd
18+
import Base: getindex, setindex!, copy, zero, one, convert, gcd
1719
import Base: show, print, *, /, //, -, +, ==, isapprox, divrem, div, rem, eltype
1820
import Base: promote_rule, truncate, chop, conj, transpose, hash
1921
import Base: isequal
@@ -141,7 +143,7 @@ eltype(::Poly{T}) where {T} = T
141143
eltype(::Type{Poly{T}}) where {T} = Poly{T}
142144

143145
length(p::Poly) = length(coeffs(p))
144-
endof(p::Poly) = length(p) - 1
146+
lastindex(p::Poly) = length(p) - 1
145147

146148
start(p::Poly) = start(coeffs(p)) - 1
147149
next(p::Poly, state) = (temp = fill!(similar(coeffs(p)), 0); temp[state+1] = p[state]; (Poly(temp), state+1))
@@ -448,7 +450,7 @@ function polyval(p::Poly{T}, x::S) where {T,S}
448450
return zero(R) * x
449451
else
450452
y = convert(R, p[end])
451-
for i = (endof(p)-1):-1:0
453+
for i = (lastindex(p)-1):-1:0
452454
y = p[i] + x*y
453455
end
454456
return y
@@ -475,8 +477,9 @@ julia> polyint(Poly([1, 0, -1]), 2)
475477
Poly(2.0 + 1.0⋅x - 0.3333333333333333⋅x^3)
476478
```
477479
"""
478-
# if we do not have any initial condition, assume k = zero(Int)
479-
polyint(p::Poly{T}) where {T} = polyint(p, 0)
480+
polyint(p::Poly{T}) where {T} = polyint(p, 0) # if we do not have any initial
481+
# condition, assume k = zero(Int)
482+
480483

481484
# if we have coefficients that have `NaN` representation
482485
function polyint(p::Poly{T}, k::S) where {T<:Union{Real,Complex},S<:Number}
@@ -542,8 +545,8 @@ julia> polyder(Poly([1, 3, -1]), 2)
542545
Poly(-2)
543546
```
544547
"""
545-
# if we have coefficients that can represent `NaN`s
546548
function polyder(p::Poly{T}, order::Int=1) where {T<:Union{Real,Complex}}
549+
# if we have coefficients that can represent `NaN`s
547550
n = length(p)
548551
order < 0 && error("Order of derivative must be non-negative")
549552
order == 0 && return p
@@ -628,7 +631,7 @@ function roots(p::Poly{T}) where {T}
628631
while p[end - num_trailing_zeros] zero(T)
629632
num_trailing_zeros += 1
630633
end
631-
n = endof(p)-(num_leading_zeros + num_trailing_zeros)
634+
n = lastindex(p)-(num_leading_zeros + num_trailing_zeros)
632635
n < 1 && return zeros(R, length(p) - num_trailing_zeros - 1)
633636

634637
companion = @compat diagm(-1 => ones(R, n-1))

test/runtests.jl

+16-4
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,12 @@ println("The approximate sum of the convergent series is: ",exp(1)*(-_γ-sum([(-
151151

152152

153153
## polyfit
154-
xs = linspace(0, pi, 10)
154+
if VERSION < v"0.7-"
155+
xs = linspace(0, pi, 10)
156+
else
157+
xs = range(0, stop=pi, length=10)
158+
end
159+
155160
ys = map(sin,xs)
156161
p = polyfit(xs, ys)
157162
p = polyfit(xs, ys, :t)
@@ -258,9 +263,16 @@ p = Poly([1.0, 0 + NaN*im, NaN, Inf, 0 - Inf*im]) # handle NaN or Inf appropriat
258263
@test repr(p) == "Poly(1.0 + NaN*im*x + NaN*x^2 + Inf*x^3 - Inf*im*x^4)"
259264

260265
p = Poly([1,2,3])
261-
@test reprmime("text/latex", p) == "\$1 + 2\\cdot x + 3\\cdot x^{2}\$"
262-
p = Poly([1//2, 2//3, 1])
263-
@test reprmime("text/latex", p) == "\$\\frac{1}{2} + \\frac{2}{3}\\cdot x + x^{2}\$"
266+
267+
if VERSION < v"0.7-"
268+
@test reprmime("text/latex", p) == "\$1 + 2\\cdot x + 3\\cdot x^{2}\$"
269+
p = Poly([1//2, 2//3, 1])
270+
@test reprmime("text/latex", p) == "\$\\frac{1}{2} + \\frac{2}{3}\\cdot x + x^{2}\$"
271+
else
272+
@test repr("text/latex", p) == "\$1 + 2\\cdot x + 3\\cdot x^{2}\$"
273+
p = Poly([1//2, 2//3, 1])
274+
@test repr("text/latex", p) == "\$\\frac{1}{2} + \\frac{2}{3}\\cdot x + x^{2}\$"
275+
end
264276

265277
# customized printing with printpoly
266278
function printpoly_to_string(args...; kwargs...)

0 commit comments

Comments
 (0)