-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Function call syntax support * README.md in favour of function calls
- Loading branch information
Showing
16 changed files
with
401 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module ExtrapFunctionCallSyntax | ||
|
||
using Base.Test, Interpolations, DualNumbers | ||
|
||
# Test if b-spline interpolation by function syntax yields identical results | ||
f(x) = sin((x-3)*2pi/9 - 1) | ||
xmax = 10 | ||
A = Float64[f(x) for x in 1:xmax] | ||
itpg = interpolate(A, BSpline(Linear()), OnGrid()) | ||
schemes = (Flat,Line,Free) | ||
|
||
for T in (Cubic, Quadratic), GC in (OnGrid, OnCell) | ||
for etp in map(S -> @inferred(interpolate(A, BSpline(T(S())), GC())), schemes), | ||
x in linspace(1,xmax,100) | ||
@test (getindex(etp, x)) == etp(x) | ||
end | ||
end | ||
|
||
for T in (Constant, Linear), GC in (OnGrid, OnCell), x in linspace(1,xmax,100) | ||
etp = interpolate(A, BSpline(T()), GC()) | ||
@test (getindex(etp, x)) == etp(x) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
module ExtrapFunctionCallSyntax | ||
|
||
using Base.Test, Interpolations, DualNumbers | ||
|
||
# Test if extrapolation by function syntax yields identical results | ||
f(x) = sin((x-3)*2pi/9 - 1) | ||
xmax = 10 | ||
A = Float64[f(x) for x in 1:xmax] | ||
itpg = interpolate(A, BSpline(Linear()), OnGrid()) | ||
|
||
schemes = ( | ||
Flat, | ||
Linear, | ||
Reflect, | ||
Periodic | ||
) | ||
|
||
for etp in map(E -> @inferred(extrapolate(itpg, E())), schemes), | ||
x in [ | ||
# In-bounds evaluation | ||
3.4, 3, dual(3.1), | ||
# Out-of-bounds evaluation | ||
-3.4, -3, dual(-3,1), | ||
13.4, 13, dual(13,1) | ||
] | ||
@test (getindex(etp, x)) == etp(x) | ||
end | ||
|
||
etpg = extrapolate(itpg, Flat()) | ||
@test typeof(etpg) <: AbstractExtrapolation | ||
|
||
@test etpg(-3) == etpg(-4.5) == etpg(0.9) == etpg(1.0) == A[1] | ||
@test etpg(10.1) == etpg(11) == etpg(148.298452) == A[end] | ||
|
||
etpf = @inferred(extrapolate(itpg, NaN)) | ||
@test typeof(etpf) <: Interpolations.FilledExtrapolation | ||
@test parent(etpf) === itpg | ||
|
||
@test @inferred(size(etpf)) == (xmax,) | ||
@test isnan(@inferred(etpf(-2.5))) | ||
@test isnan(etpf(0.999)) | ||
@test @inferred(etpf(1)) ≈ f(1) | ||
@test etpf(10) ≈ f(10) | ||
@test isnan(@inferred(etpf(10.001))) | ||
|
||
@test etpf(2.5,1) == etpf(2.5) # for show method | ||
@test_throws BoundsError etpf(2.5,2) | ||
@test_throws BoundsError etpf(2.5,2,1) | ||
|
||
x = @inferred(etpf(dual(-2.5,1))) | ||
@test isa(x, Dual) | ||
|
||
etpl = extrapolate(itpg, Linear()) | ||
k_lo = A[2] - A[1] | ||
x_lo = -3.2 | ||
@test etpl(x_lo) ≈ A[1] + k_lo * (x_lo - 1) | ||
k_hi = A[end] - A[end-1] | ||
x_hi = xmax + 5.7 | ||
@test etpl(x_hi) ≈ A[end] + k_hi * (x_hi - xmax) | ||
|
||
xmax, ymax = 8,8 | ||
g(x, y) = (x^2 + 3x - 8) * (-2y^2 + y + 1) | ||
|
||
itp2g = interpolate(Float64[g(x,y) for x in 1:xmax, y in 1:ymax], (BSpline(Quadratic(Free())), BSpline(Linear())), OnGrid()) | ||
etp2g = extrapolate(itp2g, (Linear(), Flat())) | ||
|
||
@test @inferred(etp2g(-0.5,4)) ≈ itp2g(1,4) - 1.5 * epsilon(etp2g(dual(1,1),4)) | ||
@test @inferred(etp2g(5,100)) ≈ itp2g(5,ymax) | ||
|
||
etp2ud = extrapolate(itp2g, ((Linear(), Flat()), Flat())) | ||
@test @inferred(etp2ud(-0.5,4)) ≈ itp2g(1,4) - 1.5 * epsilon(etp2g(dual(1,1),4)) | ||
@test @inferred(etp2ud(5, -4)) == etp2ud(5,1) | ||
@test @inferred(etp2ud(100, 4)) == etp2ud(8,4) | ||
@test @inferred(etp2ud(-.5, 100)) == itp2g(1,8) - 1.5 * epsilon(etp2g(dual(1,1),8)) | ||
|
||
etp2ll = extrapolate(itp2g, Linear()) | ||
@test @inferred(etp2ll(-0.5,100)) ≈ (itp2g(1,8) - 1.5 * epsilon(etp2ll(dual(1,1),8))) + (100 - 8) * epsilon(etp2ll(1,dual(8,1))) | ||
|
||
# Allow element types that don't support conversion to Int (#87): | ||
etp87g = extrapolate(interpolate([1.0im, 2.0im, 3.0im], BSpline(Linear()), OnGrid()), 0.0im) | ||
@test @inferred(etp87g(1)) == 1.0im | ||
@test @inferred(etp87g(1.5)) == 1.5im | ||
@test @inferred(etp87g(0.75)) == 0.0im | ||
@test @inferred(etp87g(3.25)) == 0.0im | ||
|
||
etp87c = extrapolate(interpolate([1.0im, 2.0im, 3.0im], BSpline(Linear()), OnCell()), 0.0im) | ||
@test @inferred(etp87c(1)) == 1.0im | ||
@test @inferred(etp87c(1.5)) == 1.5im | ||
@test @inferred(etp87c(0.75)) == 0.75im | ||
@test @inferred(etp87c(3.25)) == 3.25im | ||
@test @inferred(etp87g(0)) == 0.0im | ||
@test @inferred(etp87g(3.7)) == 0.0im | ||
|
||
# Make sure it works with Gridded too | ||
etp100g = extrapolate(interpolate(([10;20],),[100;110], Gridded(Linear())), Flat()) | ||
@test @inferred(etp100g(5)) == 100 | ||
@test @inferred(etp100g(15)) == 105 | ||
@test @inferred(etp100g(25)) == 110 | ||
# issue #178 | ||
a = randn(10,10) + im*rand(10,10) | ||
etp = @inferred(extrapolate(interpolate((1:10, 1:10), a, Gridded(Linear())), 0.0)) | ||
@test @inferred(etp(-1,0)) === 0.0+0.0im | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,3 +103,4 @@ end | |
|
||
include("type-stability.jl") | ||
include("non1.jl") | ||
include("function-call-syntax.jl") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
module GriddedFunctionCallSyntax | ||
|
||
using Interpolations, Base.Test | ||
|
||
for D in (Constant, Linear), G in (OnCell, OnGrid) | ||
## 1D | ||
a = rand(5) | ||
knots = (collect(linspace(1,length(a),length(a))),) | ||
itp = @inferred(interpolate(knots, a, Gridded(D()))) | ||
@inferred(getindex(itp, 2)) | ||
@inferred(getindex(itp, CartesianIndex((2,)))) | ||
for i = 2:length(a)-1 | ||
@test itp(i) ≈ a[i] | ||
@test itp(CartesianIndex((i,))) ≈ a[i] | ||
end | ||
@inferred(getindex(itp, knots...)) | ||
@test itp[knots...] ≈ a | ||
# compare scalar indexing and vector indexing | ||
x = knots[1]+0.1 | ||
v = itp(x) | ||
for i = 1:length(x) | ||
@test v[i] ≈ itp(x[i]) | ||
end | ||
# check the fallback vector indexing | ||
x = [2.3,2.2] # non-increasing order | ||
v = itp[x] | ||
for i = 1:length(x) | ||
@test v[i] ≈ itp(x[i]) | ||
end | ||
# compare against BSpline | ||
itpb = @inferred(interpolate(a, BSpline(D()), G())) | ||
for x in linspace(1.1,4.9,101) | ||
@test itp(x) ≈ itpb(x) | ||
end | ||
|
||
## 2D | ||
A = rand(6,5) | ||
knots = (collect(linspace(1,size(A,1),size(A,1))),collect(linspace(1,size(A,2),size(A,2)))) | ||
itp = @inferred(interpolate(knots, A, Gridded(D()))) | ||
@test parent(itp) === A | ||
@inferred(getindex(itp, 2, 2)) | ||
@inferred(getindex(itp, CartesianIndex((2,2)))) | ||
for j = 2:size(A,2)-1, i = 2:size(A,1)-1 | ||
@test itp(i,j) ≈ A[i,j] | ||
@test itp(CartesianIndex((i,j))) ≈ A[i,j] | ||
end | ||
@test itp[knots...] ≈ A | ||
@inferred(getindex(itp, knots...)) | ||
# compare scalar indexing and vector indexing | ||
x, y = knots[1]+0.1, knots[2]+0.6 | ||
v = itp(x,y) | ||
for j = 1:length(y), i = 1:length(x) | ||
@test v[i,j] ≈ itp(x[i],y[j]) | ||
end | ||
# check the fallback vector indexing | ||
x = [2.3,2.2] # non-increasing order | ||
y = [3.5,2.8] | ||
v = itp[x,y] | ||
for j = 1:length(y), i = 1:length(x) | ||
@test v[i,j] ≈ itp(x[i],y[j]) | ||
end | ||
# compare against BSpline | ||
itpb = @inferred(interpolate(A, BSpline(D()), G())) | ||
for y in linspace(1.1,5.9,101), x in linspace(1.1,4.9,101) | ||
@test itp(x,y) ≈ itpb(x,y) | ||
end | ||
|
||
A = rand(8,20) | ||
knots = ([x^2 for x = 1:8], [0.2y for y = 1:20]) | ||
itp = interpolate(knots, A, Gridded(D())) | ||
@test itp(4,1.2) ≈ A[2,6] | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,6 @@ module GriddedTests | |
|
||
include("gridded.jl") | ||
include("mixed.jl") | ||
include("function-call-syntax.jl") | ||
|
||
end | ||
end |
Oops, something went wrong.