Skip to content
Merged
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
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ A = [f(x) for x in xs]

# linear interpolation
interp_linear = LinearInterpolation(xs, A)
interp_linear[3] # exactly log(3)
interp_linear[3.1] # approximately log(3.1)
interp_linear(3) # exactly log(3)
interp_linear(3.1) # approximately log(3.1)

# cubic spline interpolation
interp_cubic = CubicSplineInterpolation(xs, A)
interp_cubic[3] # exactly log(3)
interp_cubic[3.1] # approximately log(3.1)
interp_cubic(3) # exactly log(3)
interp_cubic(3.1) # approximately log(3.1)
```
which support multidimensional data as well:
```jl
Expand All @@ -127,13 +127,13 @@ A = [f(x+y) for x in xs, y in ys]

# linear interpolation
interp_linear = LinearInterpolation((xs, ys), A)
interp_linear[3, 2] # exactly log(3 + 2)
interp_linear[3.1, 2.1] # approximately log(3.1 + 2.1)
interp_linear(3, 2) # exactly log(3 + 2)
interp_linear(3.1, 2.1) # approximately log(3.1 + 2.1)

# cubic spline interpolation
interp_cubic = CubicSplineInterpolation((xs, ys), A)
interp_cubic[3, 2] # exactly log(3 + 2)
interp_cubic[3.1, 2.1] # approximately log(3.1 + 2.1)
interp_cubic(3, 2) # exactly log(3 + 2)
interp_cubic(3.1, 2.1) # approximately log(3.1 + 2.1)
```
For extrapolation, i.e., when interpolation objects are evaluated in coordinates outside of range provided in constructors, the default option for a boundary condition is `Throw` so that they will return an error.
Interested users can specify boundary conditions by providing an extra parameter for `extrapolation_bc`:
Expand All @@ -145,8 +145,8 @@ A = [f(x) for x in xs]
# extrapolation with linear boundary conditions
extrap = LinearInterpolation(xs, A, extrapolation_bc = Interpolations.Linear())

@test extrap[1 - 0.2] # ≈ f(1) - (f(1.2) - f(1))
@test extrap[5 + 0.2] # ≈ f(5) + (f(5) - f(4.8))
@test extrap(1 - 0.2) # ≈ f(1) - (f(1.2) - f(1))
@test extrap(5 + 0.2) # ≈ f(5) + (f(5) - f(4.8))
```
Irregular grids are supported as well; note that presently only `LinearInterpolation` supports irregular grids.
```jl
Expand All @@ -155,8 +155,8 @@ A = [f(x) for x in xs]

# linear interpolation
interp_linear = LinearInterpolation(xs, A)
interp_linear[1] # exactly log(1)
interp_linear[1.05] # approximately log(1.05)
interp_linear(1) # exactly log(1)
interp_linear(1.05) # approximately log(1.05)
```

## Control of interpolation algorithm
Expand Down
120 changes: 60 additions & 60 deletions test/convenience-constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ YLEN = convert(Integer, floor((YMAX - YMIN)/ΔY) + 1)
interp_full = extrapolate(scale(interpolate(A, BSpline(Linear()), OnGrid()), xs), Interpolations.Throw()) # using full constructor

@test typeof(interp) == typeof(interp_full)
@test interp[XMIN] ≈ f(XMIN)
@test interp[XMAX] ≈ f(XMAX)
@test interp[XMIN + ΔX] ≈ f(XMIN + ΔX)
@test interp[XMAX - ΔX] ≈ f(XMAX - ΔX)
@test interp[XMIN + ΔX / 2] ≈ f(XMIN + ΔX / 2) atol=.1
@test_throws BoundsError interp[XMIN - ΔX / 2]
@test_throws BoundsError interp[XMAX + ΔX / 2]
@test interp(XMIN) ≈ f(XMIN)
@test interp(XMAX) ≈ f(XMAX)
@test interp(XMIN + ΔX) ≈ f(XMIN + ΔX)
@test interp(XMAX - ΔX) ≈ f(XMAX - ΔX)
@test interp(XMIN + ΔX / 2) ≈ f(XMIN + ΔX / 2) atol=.1
@test_throws BoundsError interp(XMIN - ΔX / 2)
@test_throws BoundsError interp(XMAX + ΔX / 2)
end

@testset "1d-regular-grids-cubic" begin
Expand All @@ -40,13 +40,13 @@ YLEN = convert(Integer, floor((YMAX - YMIN)/ΔY) + 1)
interp_full = extrapolate(scale(interpolate(A, BSpline(Cubic(Line())), OnGrid()), xs), Interpolations.Throw())

@test typeof(interp) == typeof(interp_full)
@test interp[XMIN] ≈ f(XMIN)
@test interp[XMAX] ≈ f(XMAX)
@test interp[XMIN + ΔX] ≈ f(XMIN + ΔX)
@test interp[XMAX - ΔX] ≈ f(XMAX - ΔX)
@test interp[XMIN + ΔX / 2] ≈ f(XMIN + ΔX / 2) atol=.1
@test_throws BoundsError interp[XMIN - ΔX / 2]
@test_throws BoundsError interp[XMAX + ΔX / 2]
@test interp(XMIN) ≈ f(XMIN)
@test interp(XMAX) ≈ f(XMAX)
@test interp(XMIN + ΔX) ≈ f(XMIN + ΔX)
@test interp(XMAX - ΔX) ≈ f(XMAX - ΔX)
@test interp(XMIN + ΔX / 2) ≈ f(XMIN + ΔX / 2) atol=.1
@test_throws BoundsError interp(XMIN - ΔX / 2)
@test_throws BoundsError interp(XMAX + ΔX / 2)
end

@testset "1d-irregular-grids" begin
Expand All @@ -59,12 +59,12 @@ YLEN = convert(Integer, floor((YMAX - YMIN)/ΔY) + 1)
interp_full = extrapolate(interpolate((xs, ), A, Gridded(Linear())), Interpolations.Throw())

@test typeof(interp) == typeof(interp_full)
@test interp[xmin] ≈ f(xmin)
@test interp[xmax] ≈ f(xmax)
@test interp[xs[2]] ≈ f(xs[2])
@test interp[xmin + ΔX / 2] ≈ f(xmin + ΔX / 2) atol=.1
@test_throws BoundsError interp[xmin - ΔX / 2]
@test_throws BoundsError interp[xmax + ΔX / 2]
@test interp(xmin) ≈ f(xmin)
@test interp(xmax) ≈ f(xmax)
@test interp(xs[2]) ≈ f(xs[2])
@test interp(xmin + ΔX / 2) ≈ f(xmin + ΔX / 2) atol=.1
@test_throws BoundsError interp(xmin - ΔX / 2)
@test_throws BoundsError interp(xmax + ΔX / 2)
end

@testset "1d-handling-extrapolation" begin
Expand All @@ -80,8 +80,8 @@ YLEN = convert(Integer, floor((YMAX - YMIN)/ΔY) + 1)
extrap_full = extrapolate(scale(interpolate(A, BSpline(Linear()), OnGrid()), xs), Interpolations.Linear())

@test typeof(extrap) == typeof(extrap_full)
@test extrap[x_lower] ≈ A[1] - ΔA_l
@test extrap[x_higher] ≈ A[end] + ΔA_h
@test extrap(x_lower) ≈ A[1] - ΔA_l
@test extrap(x_higher) ≈ A[end] + ΔA_h
end
end

Expand All @@ -95,18 +95,18 @@ end
interp_full = extrapolate(scale(interpolate(A, BSpline(Linear()), OnGrid()), xs, ys), Interpolations.Throw())

@test typeof(interp) == typeof(interp_full)
@test interp[XMIN,YMIN] ≈ f(XMIN,YMIN)
@test interp[XMIN,YMAX] ≈ f(XMIN,YMAX)
@test interp[XMAX,YMIN] ≈ f(XMAX,YMIN)
@test interp[XMAX,YMAX] ≈ f(XMAX,YMAX)
@test interp[XMIN + ΔX,YMIN] ≈ f(XMIN + ΔX,YMIN)
@test interp[XMIN,YMIN + ΔY] ≈ f(XMIN,YMIN + ΔY)
@test interp[XMIN + ΔX,YMIN + ΔY] ≈ f(XMIN + ΔX,YMIN + ΔY)
@test interp[XMIN + ΔX / 2,YMIN + ΔY / 2] ≈ f(XMIN + ΔX / 2,YMIN + ΔY / 2) atol=.1
@test_throws BoundsError interp[XMIN - ΔX / 2,YMIN - ΔY / 2]
@test_throws BoundsError interp[XMIN - ΔX / 2,YMIN + ΔY / 2]
@test_throws BoundsError interp[XMIN + ΔX / 2,YMIN - ΔY / 2]
@test_throws BoundsError interp[XMAX + ΔX / 2,YMAX + ΔY / 2]
@test interp(XMIN,YMIN) ≈ f(XMIN,YMIN)
@test interp(XMIN,YMAX) ≈ f(XMIN,YMAX)
@test interp(XMAX,YMIN) ≈ f(XMAX,YMIN)
@test interp(XMAX,YMAX) ≈ f(XMAX,YMAX)
@test interp(XMIN + ΔX,YMIN) ≈ f(XMIN + ΔX,YMIN)
@test interp(XMIN,YMIN + ΔY) ≈ f(XMIN,YMIN + ΔY)
@test interp(XMIN + ΔX,YMIN + ΔY) ≈ f(XMIN + ΔX,YMIN + ΔY)
@test interp(XMIN + ΔX / 2,YMIN + ΔY / 2) ≈ f(XMIN + ΔX / 2,YMIN + ΔY / 2) atol=.1
@test_throws BoundsError interp(XMIN - ΔX / 2,YMIN - ΔY / 2)
@test_throws BoundsError interp(XMIN - ΔX / 2,YMIN + ΔY / 2)
@test_throws BoundsError interp(XMIN + ΔX / 2,YMIN - ΔY / 2)
@test_throws BoundsError interp(XMAX + ΔX / 2,YMAX + ΔY / 2)
end

@testset "2d-regular-grids-cubic" begin
Expand All @@ -118,18 +118,18 @@ end
interp_full = extrapolate(scale(interpolate(A, BSpline(Cubic(Line())), OnGrid()), xs, ys), Interpolations.Throw())

@test typeof(interp) == typeof(interp_full)
@test interp[XMIN,YMIN] ≈ f(XMIN,YMIN)
@test interp[XMIN,YMAX] ≈ f(XMIN,YMAX)
@test interp[XMAX,YMIN] ≈ f(XMAX,YMIN)
@test interp[XMAX,YMAX] ≈ f(XMAX,YMAX)
@test interp[XMIN + ΔX,YMIN] ≈ f(XMIN + ΔX,YMIN)
@test interp[XMIN,YMIN + ΔY] ≈ f(XMIN,YMIN + ΔY)
@test interp[XMIN + ΔX,YMIN + ΔY] ≈ f(XMIN + ΔX,YMIN + ΔY)
@test interp[XMIN + ΔX / 2,YMIN + ΔY / 2] ≈ f(XMIN + ΔX / 2,YMIN + ΔY / 2) atol=.1
@test_throws BoundsError interp[XMIN - ΔX / 2,YMIN - ΔY / 2]
@test_throws BoundsError interp[XMIN - ΔX / 2,YMIN + ΔY / 2]
@test_throws BoundsError interp[XMIN + ΔX / 2,YMIN - ΔY / 2]
@test_throws BoundsError interp[XMAX + ΔX / 2,YMAX + ΔY / 2]
@test interp(XMIN,YMIN) ≈ f(XMIN,YMIN)
@test interp(XMIN,YMAX) ≈ f(XMIN,YMAX)
@test interp(XMAX,YMIN) ≈ f(XMAX,YMIN)
@test interp(XMAX,YMAX) ≈ f(XMAX,YMAX)
@test interp(XMIN + ΔX,YMIN) ≈ f(XMIN + ΔX,YMIN)
@test interp(XMIN,YMIN + ΔY) ≈ f(XMIN,YMIN + ΔY)
@test interp(XMIN + ΔX,YMIN + ΔY) ≈ f(XMIN + ΔX,YMIN + ΔY)
@test interp(XMIN + ΔX / 2,YMIN + ΔY / 2) ≈ f(XMIN + ΔX / 2,YMIN + ΔY / 2) atol=.1
@test_throws BoundsError interp(XMIN - ΔX / 2,YMIN - ΔY / 2)
@test_throws BoundsError interp(XMIN - ΔX / 2,YMIN + ΔY / 2)
@test_throws BoundsError interp(XMIN + ΔX / 2,YMIN - ΔY / 2)
@test_throws BoundsError interp(XMAX + ΔX / 2,YMAX + ΔY / 2)
end

@testset "2d-irregular-grids" begin
Expand All @@ -145,18 +145,18 @@ end
interp_full = extrapolate(interpolate((xs, ys), A, Gridded(Linear())), Interpolations.Throw())

@test typeof(interp) == typeof(interp_full)
@test interp[xmin,ymin] ≈ f(xmin,ymin)
@test interp[xmin,ymax] ≈ f(xmin,ymax)
@test interp[xmax,ymin] ≈ f(xmax,ymin)
@test interp[xmax,ymax] ≈ f(xmax,ymax)
@test interp[xs[2],ymin] ≈ f(xs[2],ymin)
@test interp[xmin,ys[2]] ≈ f(xmin,ys[2])
@test interp[xs[2],ys[2]] ≈ f(xs[2],ys[2])
@test interp[xmin + ΔX / 2,ymin + ΔY / 2] ≈ f(xmin + ΔX / 2,ymin + ΔY / 2) atol=.1
@test_throws BoundsError interp[xmin - ΔX / 2,ymin - ΔY / 2]
@test_throws BoundsError interp[xmin - ΔX / 2,ymin + ΔY / 2]
@test_throws BoundsError interp[xmin + ΔX / 2,ymin - ΔY / 2]
@test_throws BoundsError interp[xmax + ΔX / 2,ymax + ΔY / 2]
@test interp(xmin,ymin) ≈ f(xmin,ymin)
@test interp(xmin,ymax) ≈ f(xmin,ymax)
@test interp(xmax,ymin) ≈ f(xmax,ymin)
@test interp(xmax,ymax) ≈ f(xmax,ymax)
@test interp(xs[2],ymin) ≈ f(xs[2],ymin)
@test interp(xmin,ys[2]) ≈ f(xmin,ys[2])
@test interp(xs[2],ys[2]) ≈ f(xs[2],ys[2])
@test interp(xmin + ΔX / 2,ymin + ΔY / 2) ≈ f(xmin + ΔX / 2,ymin + ΔY / 2) atol=.1
@test_throws BoundsError interp(xmin - ΔX / 2,ymin - ΔY / 2)
@test_throws BoundsError interp(xmin - ΔX / 2,ymin + ΔY / 2)
@test_throws BoundsError interp(xmin + ΔX / 2,ymin - ΔY / 2)
@test_throws BoundsError interp(xmax + ΔX / 2,ymax + ΔY / 2)
end

@testset "2d-handling-extrapolation" begin
Expand All @@ -175,8 +175,8 @@ end
extrap_full = extrapolate(scale(interpolate(A, BSpline(Linear()), OnGrid()), xs, ys), (Interpolations.Linear(), Interpolations.Flat()))

@test typeof(extrap) == typeof(extrap_full)
@test extrap[x_lower, y_lower] ≈ A[1, 1] - ΔA_l
@test extrap[x_higher, y_higher] ≈ A[end, end] + ΔA_h
@test extrap(x_lower, y_lower) ≈ A[1, 1] - ΔA_l
@test extrap(x_higher, y_higher) ≈ A[end, end] + ΔA_h
end
end

Expand Down