Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BREAKING: Unexport gradient and friends #225

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ sudo: false
julia:
- 0.6
- 0.7
- 1.0
- nightly
matrix:
allow_failures:
Expand Down
8 changes: 1 addition & 7 deletions src/Interpolations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ export
extrapolate,
scale,

gradient!,
gradient1,
hessian!,
hessian,
hessian1,

AbstractInterpolation,
AbstractExtrapolation,

Expand Down Expand Up @@ -47,7 +41,7 @@ import Base: convert, size, getindex, promote_rule,
@static if VERSION < v"0.7.0-DEV.3449"
import Base: gradient
else
import LinearAlgebra: gradient
function gradient end
end

import Compat: axes
Expand Down
10 changes: 5 additions & 5 deletions test/b-splines/cubic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,16 @@ for (constructor, copier) in ((interpolate, identity), (interpolate!, copy))
itp = constructor(copier(A), BSpline(Cubic(BC())), GT())
# test that inner region is close to data
for x in range(ix[5], stop=ix[end-4], length=100)
@test ≈(g(x),(gradient(itp,x))[1],atol=cbrt(cbrt(eps(g(x)))))
@test ≈(g(x),(Interpolations.gradient(itp,x))[1],atol=cbrt(cbrt(eps(g(x)))))
end
end
end
itp_flat_g = interpolate(A, BSpline(Cubic(Flat())), OnGrid())
@test ≈((gradient(itp_flat_g,1))[1],0,atol=eps())
@test ≈((gradient(itp_flat_g,ix[end]))[1],0,atol=eps())
@test ≈((Interpolations.gradient(itp_flat_g,1))[1],0,atol=eps())
@test ≈((Interpolations.gradient(itp_flat_g,ix[end]))[1],0,atol=eps())

itp_flat_c = interpolate(A, BSpline(Cubic(Flat())), OnCell())
@test ≈((gradient(itp_flat_c,0.5))[1],0,atol=eps())
@test ≈((gradient(itp_flat_c,ix[end] + 0.5))[1],0,atol=eps())
@test ≈((Interpolations.gradient(itp_flat_c,0.5))[1],0,atol=eps())
@test ≈((Interpolations.gradient(itp_flat_c,ix[end] + 0.5))[1],0,atol=eps())

end
36 changes: 18 additions & 18 deletions test/gradient.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ itp1 = interpolate(Float64[f1(x) for x in 1:nx-1],
g = Array{Float64}(undef, 1)

for x in 1:nx
@test gradient(itp1, x)[1] == 0
@test gradient!(g, itp1, x)[1] == 0
@test Interpolations.gradient(itp1, x)[1] == 0
@test Interpolations.gradient!(g, itp1, x)[1] == 0
@test g[1] == 0
end

Expand All @@ -25,14 +25,14 @@ itp2 = interpolate((1:nx-1,), Float64[f1(x) for x in 1:nx-1],
Gridded(Linear()))
for itp in (itp1, itp2)
for x in 2.5:nx-1.5
@test ≈(g1(x),(gradient(itp,x))[1],atol=abs(0.1 * g1(x)))
@test ≈(g1(x),(gradient!(g,itp,x))[1],atol=abs(0.1 * g1(x)))
@test ≈(g1(x),(Interpolations.gradient(itp,x))[1],atol=abs(0.1 * g1(x)))
@test ≈(g1(x),(Interpolations.gradient!(g,itp,x))[1],atol=abs(0.1 * g1(x)))
@test ≈(g1(x),g[1],atol=abs(0.1 * g1(x)))
end

for i = 1:10
x = rand()*(nx-2)+1.5
gtmp = gradient(itp, x)[1]
gtmp = Interpolations.gradient(itp, x)[1]
xd = dual(x, 1)
@test epsilon(itp[xd]) ≈ gtmp
end
Expand All @@ -44,23 +44,23 @@ itp_grid = interpolate(knots, Float64[f1(x) for x in knots[1]],
Gridded(Linear()))

for x in 1.5:0.5:nx-1.5
@test ≈(g1(x),(gradient(itp_grid,x))[1],atol=abs(0.5 * g1(x)))
@test ≈(g1(x),(gradient!(g,itp_grid,x))[1],atol=abs(0.5 * g1(x)))
@test ≈(g1(x),(Interpolations.gradient(itp_grid,x))[1],atol=abs(0.5 * g1(x)))
@test ≈(g1(x),(Interpolations.gradient!(g,itp_grid,x))[1],atol=abs(0.5 * g1(x)))
@test ≈(g1(x),g[1],atol=abs(0.5 * g1(x)))
end

# Since Quadratic is OnCell in the domain, check gradients at grid points
itp1 = interpolate(Float64[f1(x) for x in 1:nx-1],
BSpline(Quadratic(Periodic())), OnCell())
for x in 2:nx-1
@test ≈(g1(x),(gradient(itp1,x))[1],atol=abs(0.05 * g1(x)))
@test ≈(g1(x),(gradient!(g,itp1,x))[1],atol=abs(0.05 * g1(x)))
@test ≈(g1(x),(Interpolations.gradient(itp1,x))[1],atol=abs(0.05 * g1(x)))
@test ≈(g1(x),(Interpolations.gradient!(g,itp1,x))[1],atol=abs(0.05 * g1(x)))
@test ≈(g1(x),g[1],atol=abs(0.1 * g1(x)))
end

for i = 1:10
x = rand()*(nx-2)+1.5
gtmp = gradient(itp1, x)[1]
gtmp = Interpolations.gradient(itp1, x)[1]
xd = dual(x, 1)
@test epsilon(itp1[xd]) ≈ gtmp
end
Expand All @@ -79,30 +79,30 @@ y = qfunc(xg)
iq = interpolate(y, BSpline(Quadratic(Free())), OnCell())
x = 1.8
@test iq[x] ≈ qfunc(x)
@test (gradient(iq,x))[1] ≈ dqfunc(x)
@test (Interpolations.gradient(iq,x))[1] ≈ dqfunc(x)

# 2d (biquadratic)
p = [(x-1.75)^2 for x = 1:7]
A = p*p'
iq = interpolate(A, BSpline(Quadratic(Free())), OnCell())
@test iq[4,4] ≈ (4 - 1.75) ^ 4
@test iq[4,3] ≈ (4 - 1.75) ^ 2 * (3 - 1.75) ^ 2
g = gradient(iq, 4, 3)
g = Interpolations.gradient(iq, 4, 3)
@test g[1] ≈ 2 * (4 - 1.75) * (3 - 1.75) ^ 2
@test g[2] ≈ 2 * (4 - 1.75) ^ 2 * (3 - 1.75)

iq = interpolate!(copy(A), BSpline(Quadratic(InPlace())), OnCell())
@test iq[4,4] ≈ (4 - 1.75) ^ 4
@test iq[4,3] ≈ (4 - 1.75) ^ 2 * (3 - 1.75) ^ 2
g = gradient(iq, 4, 3)
g = Interpolations.gradient(iq, 4, 3)
@test ≈(g[1],2 * (4 - 1.75) * (3 - 1.75) ^ 2,atol=0.03)
@test ≈(g[2],2 * (4 - 1.75) ^ 2 * (3 - 1.75),atol=0.2)

# InPlaceQ is exact for an underlying quadratic
iq = interpolate!(copy(A), BSpline(Quadratic(InPlaceQ())), OnCell())
@test iq[4,4] ≈ (4 - 1.75) ^ 4
@test iq[4,3] ≈ (4 - 1.75) ^ 2 * (3 - 1.75) ^ 2
g = gradient(iq, 4, 3)
g = Interpolations.gradient(iq, 4, 3)
@test g[1] ≈ 2 * (4 - 1.75) * (3 - 1.75) ^ 2
@test g[2] ≈ 2 * (4 - 1.75) ^ 2 * (3 - 1.75)

Expand All @@ -119,19 +119,19 @@ for BC in (Flat,Line,Free,Periodic,Reflect,Natural), GT in (OnGrid, OnCell)
y = rand()*(nx-2)+1.5
xd = dual(x, 1)
yd = dual(y, 1)
gtmp = gradient(itp_a, x, y)
gtmp = Interpolations.gradient(itp_a, x, y)
@test length(gtmp) == 2
@test epsilon(itp_a[xd,y]) ≈ gtmp[1]
@test epsilon(itp_a[x,yd]) ≈ gtmp[2]
gtmp = gradient(itp_b, x, y)
gtmp = Interpolations.gradient(itp_b, x, y)
@test length(gtmp) == 2
@test epsilon(itp_b[xd,y]) ≈ gtmp[1]
@test epsilon(itp_b[x,yd]) ≈ gtmp[2]
ix, iy = round(Int, x), round(Int, y)
gtmp = gradient(itp_c, ix, y)
gtmp = Interpolations.gradient(itp_c, ix, y)
@test length(gtmp) == 1
@test epsilon(itp_c[ix,yd]) ≈ gtmp[1]
gtmp = gradient(itp_d, x, iy)
gtmp = Interpolations.gradient(itp_d, x, iy)
@test length(gtmp) == 1
@test epsilon(itp_d[xd,iy]) ≈ gtmp[1]
end
Expand Down
2 changes: 1 addition & 1 deletion test/scaling/dimspecs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ sitp = scale(itp, xs, ys)
for (ix,x) in enumerate(xs), (iy,y) in enumerate(ys)
@test ≈(sitp[x,y],f(x,y),atol=sqrt(eps(1.0)))

g = gradient(sitp, x, y)
g = Interpolations.gradient(sitp, x, y)
fx = epsilon(sitp[dual(x,1), dual(y,0)])
fy = epsilon(sitp[dual(x,0), dual(y,1)])

Expand Down
4 changes: 2 additions & 2 deletions test/scaling/nointerp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ for (ix,x0) in enumerate(xs[1:end-1]), y0 in ys
@test ≈(sitp[x,y],f(x,y),atol=0.05)
end

@test length(gradient(sitp, pi/3, 2)) == 1
@test length(Interpolations.gradient(sitp, pi/3, 2)) == 1

# check for case where initial/middle indices are NoInterp but later ones are <:BSpline
isdefined(Random, :seed!) ? Random.seed!(1234) : srand(1234) # `srand` was renamed to `seed!`
Expand All @@ -34,6 +34,6 @@ itpb = interpolate(zb, (NoInterp(), BSpline(Linear())), OnGrid())
rng = range(1.0, stop=19.0, length=10)
sitpa = scale(itpa, rng, 1:10)
sitpb = scale(itpb, 1:10, rng)
@test gradient(sitpa, 3.0, 3) == gradient(sitpb, 3, 3.0)
@test Interpolations.gradient(sitpa, 3.0, 3) == Interpolations.gradient(sitpb, 3, 3.0)

end
2 changes: 1 addition & 1 deletion test/scaling/scaling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ itp = interpolate(ys, BSpline(Linear()), OnGrid())
sitp = @inferred scale(itp, xs)

for x in -pi:.1:pi
g = @inferred(gradient(sitp, x))[1]
g = @inferred(Interpolations.gradient(sitp, x))[1]
@test ≈(cos(x),g,atol=0.05)
end

Expand Down
4 changes: 2 additions & 2 deletions test/typing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ end
@test typeof(itp[3.5f0]) == Float32

for x in 3.1:.2:4.3
@test ≈([g(x)], gradient(itp,x),atol=abs(0.1 * g(x)))
@test ≈([g(x)], Interpolations.gradient(itp,x),atol=abs(0.1 * g(x)))
end

@test typeof(gradient(itp, 3.5f0)[1]) == Float32
@test typeof(Interpolations.gradient(itp, 3.5f0)[1]) == Float32

# Rational element types
R = Rational{Int}[x^2//10 for x in 1:10]
Expand Down