Skip to content

Commit 550b6c9

Browse files
committed
Generalize the signature of LinearInterpolation and CubicInterpolation
1 parent 25d42a2 commit 550b6c9

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ which support multidimensional data as well:
127127
f(x,y) = log(x+y)
128128
xs = 1:0.2:5
129129
ys = 2:0.1:5
130-
A = [f(x+y) for x in xs, y in ys]
130+
A = [f(x,y) for x in xs, y in ys]
131131

132132
# linear interpolation
133133
interp_linear = LinearInterpolation((xs, ys), A)

src/convenience-constructors.jl

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
# convenience copnstructors for linear / cubic spline interpolations
22
# 1D version
3-
LinearInterpolation(range::T, vs; extrapolation_bc = Throw()) where {T <: AbstractRange} = extrapolate(scale(interpolate(vs, BSpline(Linear())), range), extrapolation_bc)
4-
LinearInterpolation(range::T, vs; extrapolation_bc = Throw()) where {T <: AbstractArray} = extrapolate(interpolate((range, ), vs, Gridded(Linear())), extrapolation_bc)
5-
CubicSplineInterpolation(range::T, vs; bc = Line(OnGrid()), extrapolation_bc = Throw()) where {T <: AbstractRange} = extrapolate(scale(interpolate(vs, BSpline(Cubic(bc))), range), extrapolation_bc)
3+
LinearInterpolation(range::AbstractRange, vs::AbstractVector; extrapolation_bc = Throw()) =
4+
extrapolate(scale(interpolate(vs, BSpline(Linear())), range), extrapolation_bc)
5+
LinearInterpolation(range::AbstractVector, vs::AbstractVector; extrapolation_bc = Throw()) =
6+
extrapolate(interpolate((range, ), vs, Gridded(Linear())), extrapolation_bc)
7+
CubicSplineInterpolation(range::AbstractRange, vs::AbstractVector;
8+
bc = Line(OnGrid()), extrapolation_bc = Throw()) =
9+
extrapolate(scale(interpolate(vs, BSpline(Cubic(bc))), range), extrapolation_bc)
610

711
# multivariate versions
8-
LinearInterpolation(ranges::NTuple{N,T}, vs; extrapolation_bc = Throw()) where {N,T <: AbstractRange} = extrapolate(scale(interpolate(vs, BSpline(Linear())), ranges...), extrapolation_bc)
9-
LinearInterpolation(ranges::NTuple{N,T}, vs; extrapolation_bc = Throw()) where {N,T <: AbstractArray} = extrapolate(interpolate(ranges, vs, Gridded(Linear())), extrapolation_bc)
10-
CubicSplineInterpolation(ranges::NTuple{N,T}, vs; bc = Line(OnGrid()), extrapolation_bc = Throw()) where {N,T <: AbstractRange} = extrapolate(scale(interpolate(vs, BSpline(Cubic(bc))), ranges...), extrapolation_bc)
12+
LinearInterpolation(ranges::NTuple{N,AbstractRange}, vs::AbstractArray{T,N};
13+
extrapolation_bc = Throw()) where {N,T} =
14+
extrapolate(scale(interpolate(vs, BSpline(Linear())), ranges...), extrapolation_bc)
15+
LinearInterpolation(ranges::NTuple{N,AbstractVector}, vs::AbstractArray{T,N};
16+
extrapolation_bc = Throw()) where {N,T} =
17+
extrapolate(interpolate(ranges, vs, Gridded(Linear())), extrapolation_bc)
18+
CubicSplineInterpolation(ranges::NTuple{N,AbstractRange}, vs::AbstractArray{T,N};
19+
bc = Line(OnGrid()), extrapolation_bc = Throw()) where {N,T} =
20+
extrapolate(scale(interpolate(vs, BSpline(Cubic(bc))), ranges...), extrapolation_bc)

test/convenience-constructors.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,17 @@ end
178178
@test extrap(x_lower, y_lower) A[1, 1] - ΔA_l
179179
@test extrap(x_higher, y_higher) A[end, end] + ΔA_h
180180
end
181+
182+
@testset "issue #230" begin # at least, I think this is what issue #230 is really about
183+
f(x,y) = log(x+y)
184+
xs = 1:5
185+
ys = 2:0.1:5
186+
A = [f(x,y) for x in xs, y in ys]
187+
itp = LinearInterpolation((xs, ys), A)
188+
for (i, j) in zip(Iterators.product(xs, ys), eachindex(A))
189+
@test itp(i...) A[j]
190+
end
191+
end
181192
end
182193

183194
end

0 commit comments

Comments
 (0)