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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ A = log.(xs)
```
Create linear interpolation object without extrapolation
```julia
interp_linear = LinearInterpolation(xs, A)
interp_linear = linear_interpolation(xs, A)
interp_linear(3) # exactly log(3)
interp_linear(3.1) # approximately log(3.1)
interp_linear(0.9) # outside grid: error
```
Create linear interpolation object with extrapolation
```julia
interp_linear_extrap = LinearInterpolation(xs, A,extrapolation_bc=Line())
interp_linear_extrap = linear_interpolation(xs, A,extrapolation_bc=Line())
interp_linear_extrap(0.9) # outside grid: linear extrapolation
```

Expand Down
24 changes: 12 additions & 12 deletions docs/src/convenience-construction.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

## Convenience notation

For constant, linear, and cubic spline interpolations, `ConstantInterpolation`, `LinearInterpolation`, and `CubicSplineInterpolation`
For constant, linear, and cubic spline interpolations, `constant_interpolation`, `linear_interpolation`, and `cubic_spline_interpolation`
can be used to create interpolating and extrapolating objects handily.

### Motivating Example
Expand All @@ -11,7 +11,7 @@ extrap_full = extrapolate(scale(interpolate(A, BSpline(Linear())), xs), Line())
```
can be written as the more readable
```julia
extrap = LinearInterpolation(xs, A, extrapolation_bc = Line())
extrap = linear_interpolation(xs, A, extrapolation_bc = Line())
```
by using the convenience constructor.

Expand All @@ -23,12 +23,12 @@ xs = 1:0.2:5
A = [f(x) for x in xs]

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

# cubic spline interpolation
interp_cubic = CubicSplineInterpolation(xs, A)
interp_cubic = cubic_spline_interpolation(xs, A)
interp_cubic(3) # exactly log(3)
interp_cubic(3.1) # approximately log(3.1)
```
Expand All @@ -40,12 +40,12 @@ ys = 2:0.1:5
A = [f(x,y) for x in xs, y in ys]

# linear interpolation
interp_linear = LinearInterpolation((xs, ys), A)
interp_linear = linear_interpolation((xs, ys), A)
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 = cubic_spline_interpolation((xs, ys), A)
interp_cubic(3, 2) # exactly log(3 + 2)
interp_cubic(3.1, 2.1) # approximately log(3.1 + 2.1)
```
Expand All @@ -57,25 +57,25 @@ xs = 1:0.2:5
A = [f(x) for x in xs]

# extrapolation with linear boundary conditions
extrap = LinearInterpolation(xs, A, extrapolation_bc = Line())
extrap = linear_interpolation(xs, A, extrapolation_bc = Line())

@test extrap(1 - 0.2) # ≈ f(1) - (f(1.2) - f(1))
@test extrap(5 + 0.2) # ≈ f(5) + (f(5) - f(4.8))
```
You can also use a "fill" value, which gets returned whenever you ask for out-of-range values:

```julia
extrap = LinearInterpolation(xs, A, extrapolation_bc = NaN)
extrap = linear_interpolation(xs, A, extrapolation_bc = NaN)
@test isnan(extrap(5.2))
```

Irregular grids are supported as well; note that presently only `ConstantInterpolation` and `LinearInterpolation` supports irregular grids.
Irregular grids are supported as well; note that presently only `constant_interpolation` and `linear_interpolation` supports irregular grids.
```julia
xs = [x^2 for x = 1:0.2:5]
A = [f(x) for x in xs]

# linear interpolation
interp_linear = LinearInterpolation(xs, A)
interp_linear = linear_interpolation(xs, A)
interp_linear(1) # exactly log(1)
interp_linear(1.05) # approximately log(1.05)
```
Expand All @@ -96,8 +96,8 @@ x = a:1.0:b
# length(x) == length(y)
y = @. cos(x^2 / 9.0) # Function application by broadcasting
# Interpolations
itp_linear = LinearInterpolation(x, y)
itp_cubic = CubicSplineInterpolation(x, y)
itp_linear = linear_interpolation(x, y)
itp_cubic = cubic_spline_interpolation(x, y)
# Interpolation functions
f_linear(x) = itp_linear(x)
f_cubic(x) = itp_cubic(x)
Expand Down
4 changes: 2 additions & 2 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ A = log.(xs)
```
Create linear interpolation object without extrapolation
```julia
interp_linear = LinearInterpolation(xs, A)
interp_linear = linear_interpolation(xs, A)
interp_linear(3) # exactly log(3)
interp_linear(3.1) # approximately log(3.1)
interp_linear(0.9) # outside grid: error
```
Create linear interpolation object with extrapolation
```julia
interp_linear_extrap = LinearInterpolation(xs, A, extrapolation_bc=Line())
interp_linear_extrap = linear_interpolation(xs, A,extrapolation_bc=Line())
interp_linear_extrap(0.9) # outside grid: linear extrapolation
```

Expand Down
18 changes: 9 additions & 9 deletions docs/src/iterate.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ last knot being co-located. (ie. in the example below `etp(2.0) = 1.0` not
```jldoctest periodic-demo; setup = :(using Interpolations)
julia> x = [1.0, 1.5, 1.75, 2.0];

julia> etp = LinearInterpolation(x, x.^2, extrapolation_bc=Periodic());
julia> etp = linear_interpolation(x, x.^2, extrapolation_bc=Periodic());

julia> kiter = knots(etp);

Expand Down Expand Up @@ -101,7 +101,7 @@ k[1], k[2], ..., k[end-1], k[end], k[end+1], ... k[2], k[1], k[2], ...
```jldoctest reflect-demo; setup = :(using Interpolations)
julia> x = [1.0, 1.5, 1.75, 2.0];

julia> etp = LinearInterpolation(x, x.^2, extrapolation_bc=Reflect());
julia> etp = linear_interpolation(x, x.^2, extrapolation_bc=Reflect());

julia> kiter = knots(etp);

Expand Down Expand Up @@ -139,7 +139,7 @@ multi-dimensional extrapolation also supported.
```jldoctest; setup = :(using Interpolations)
julia> x = [1.0, 1.5, 1.75, 2.0];

julia> etp = LinearInterpolation((x, x), x.*x');
julia> etp = linear_interpolation((x, x), x.*x');

julia> knots(etp) |> collect
4×4 Matrix{Tuple{Float64, Float64}}:
Expand All @@ -156,7 +156,7 @@ iteration over knots can end up "stuck" iterating along a single axis:
```jldoctest; setup = :(using Interpolations)
julia> x = [1.0, 1.5, 1.75, 2.0];

julia> etp = LinearInterpolation((x, x), x.*x', extrapolation_bc=(Periodic(), Throw()));
julia> etp = linear_interpolation((x, x), x.*x', extrapolation_bc=(Periodic(), Throw()));

julia> knots(etp) |> k -> Iterators.take(k, 6) |> collect
6-element Vector{Tuple{Float64, Float64}}:
Expand All @@ -174,7 +174,7 @@ Rearranging the axes so non-repeating knots are first can address this issue:
```jldoctest; setup = :(using Interpolations)
julia> x = [1.0, 1.5, 1.75, 2.0];

julia> etp = LinearInterpolation((x, x), x.*x', extrapolation_bc=(Throw(), Periodic()));
julia> etp = linear_interpolation((x, x), x.*x', extrapolation_bc=(Throw(), Periodic()));

julia> knots(etp) |> k -> Iterators.take(k, 6) |> collect
6-element Vector{Tuple{Float64, Float64}}:
Expand All @@ -199,7 +199,7 @@ result, the iterator will generate an infinite sequence of knots starting at `1.
```jldoctest iterate-directional-unbounded; setup = :(using Interpolations)
julia> x = [1.0, 1.2, 1.3, 2.0];

julia> etp = LinearInterpolation(x, x.^2, extrapolation_bc=((Throw(), Periodic()),));
julia> etp = linear_interpolation(x, x.^2, extrapolation_bc=((Throw(), Periodic()),));

julia> kiter = knots(etp);

Expand Down Expand Up @@ -227,7 +227,7 @@ Swapping the boundary conditions, results in a finite sequence of knots from
```jldoctest iterate-directional-bounded; setup = :(using Interpolations)
julia> x = [1.0, 1.2, 1.3, 2.0];

julia> etp = LinearInterpolation(x, x.^2, extrapolation_bc=((Periodic(), Throw()),));
julia> etp = linear_interpolation(x, x.^2, extrapolation_bc=((Periodic(), Throw()),));

julia> kiter = knots(etp);

Expand Down Expand Up @@ -272,7 +272,7 @@ We can iterate over all knots greater than `start` by omitting `stop`
```jldoctest knotsbetween-usage; setup = :(using Interpolations)
julia> x = [1.0, 1.5, 1.75, 2.0];

julia> etp = LinearInterpolation(x, x.^2, extrapolation_bc=Periodic());
julia> etp = linear_interpolation(x, x.^2, extrapolation_bc=Periodic());

julia> krange = knotsbetween(etp; start=4.0);

Expand Down Expand Up @@ -322,7 +322,7 @@ dimensions.
```jldoctest; setup = :(using Interpolations)
julia> x = [1.0, 1.5, 1.75, 2.0];

julia> etp = LinearInterpolation((x, x), x.*x');
julia> etp = linear_interpolation((x, x), x.*x');

julia> knotsbetween(etp; start=(1.2, 1.5), stop=(1.8, 3.0)) |> collect
2×2 Matrix{Tuple{Float64, Float64}}:
Expand Down
6 changes: 3 additions & 3 deletions src/Interpolations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ export
InPlaceQ,
Throw,

ConstantInterpolation,
LinearInterpolation,
CubicSplineInterpolation,
constant_interpolation,
linear_interpolation,
cubic_spline_interpolation,

knots,
knotsbetween
Expand Down
32 changes: 16 additions & 16 deletions src/convenience-constructors.jl
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
# convenience copnstructors for constant / linear / cubic spline interpolations
# 1D version
ConstantInterpolation(range::AbstractRange, vs::AbstractVector; extrapolation_bc = Throw()) =
constant_interpolation(range::AbstractRange, vs::AbstractVector; extrapolation_bc = Throw()) =
extrapolate(scale(interpolate(vs, BSpline(Constant())), range), extrapolation_bc)
ConstantInterpolation(range::AbstractVector, vs::AbstractVector; extrapolation_bc = Throw()) =
constant_interpolation(range::AbstractVector, vs::AbstractVector; extrapolation_bc = Throw()) =
extrapolate(interpolate((range, ), vs, Gridded(Constant())), extrapolation_bc)
LinearInterpolation(range::AbstractRange, vs::AbstractVector; extrapolation_bc = Throw()) =
linear_interpolation(range::AbstractRange, vs::AbstractVector; extrapolation_bc = Throw()) =
extrapolate(scale(interpolate(vs, BSpline(Linear())), range), extrapolation_bc)
LinearInterpolation(range::AbstractVector, vs::AbstractVector; extrapolation_bc = Throw()) =
linear_interpolation(range::AbstractVector, vs::AbstractVector; extrapolation_bc = Throw()) =
extrapolate(interpolate((range, ), vs, Gridded(Linear())), extrapolation_bc)
CubicSplineInterpolation(range::AbstractRange, vs::AbstractVector;
cubic_spline_interpolation(range::AbstractRange, vs::AbstractVector;
bc = Line(OnGrid()), extrapolation_bc = Throw()) =
extrapolate(scale(interpolate(vs, BSpline(Cubic(bc))), range), extrapolation_bc)

# multivariate versions
ConstantInterpolation(ranges::NTuple{N,AbstractRange}, vs::AbstractArray{T,N};
constant_interpolation(ranges::NTuple{N,AbstractRange}, vs::AbstractArray{T,N};
extrapolation_bc = Throw()) where {N,T} =
extrapolate(scale(interpolate(vs, BSpline(Constant())), ranges...), extrapolation_bc)
ConstantInterpolation(ranges::NTuple{N,AbstractVector}, vs::AbstractArray{T,N};
constant_interpolation(ranges::NTuple{N,AbstractVector}, vs::AbstractArray{T,N};
extrapolation_bc = Throw()) where {N,T} =
extrapolate(interpolate(ranges, vs, Gridded(Constant())), extrapolation_bc)
LinearInterpolation(ranges::NTuple{N,AbstractRange}, vs::AbstractArray{T,N};
linear_interpolation(ranges::NTuple{N,AbstractRange}, vs::AbstractArray{T,N};
extrapolation_bc = Throw()) where {N,T} =
extrapolate(scale(interpolate(vs, BSpline(Linear())), ranges...), extrapolation_bc)
LinearInterpolation(ranges::NTuple{N,AbstractVector}, vs::AbstractArray{T,N};
linear_interpolation(ranges::NTuple{N,AbstractVector}, vs::AbstractArray{T,N};
extrapolation_bc = Throw()) where {N,T} =
extrapolate(interpolate(ranges, vs, Gridded(Linear())), extrapolation_bc)
CubicSplineInterpolation(ranges::NTuple{N,AbstractRange}, vs::AbstractArray{T,N};
cubic_spline_interpolation(ranges::NTuple{N,AbstractRange}, vs::AbstractArray{T,N};
bc = Line(OnGrid()), extrapolation_bc = Throw()) where {N,T} =
extrapolate(scale(interpolate(vs, BSpline(Cubic(bc))), ranges...), extrapolation_bc)

"""
etp = LinearInterpolation(knots, A; extrapolation_bc=Throw())
etp = linear_interpolation(knots, A; extrapolation_bc=Throw())

A shorthand for one of the following.
* `extrapolate(scale(interpolate(A, BSpline(Linear())), knots), extrapolation_bc)`
Expand All @@ -41,10 +41,10 @@ Consider using `interpolate`, `scale`, or `extrapolate` explicitly as needed
rather than using this convenience constructor. Performance will improve
without scaling or extrapolation.
"""
LinearInterpolation
linear_interpolation

"""
etp = ConstantInterpolation(knots, A; extrapolation_bc=Throw())
etp = constant_interpolation(knots, A; extrapolation_bc=Throw())

A shorthand for `extrapolate(interpolate(knots, A, scheme), extrapolation_bc)`,
where `scheme` is either `BSpline(Constant())` or `Gridded(Constant())` depending on whether
Expand All @@ -54,15 +54,15 @@ Consider using `interpolate` or `extrapolate` explicitly as needed
rather than using this convenience constructor. Performance will improve
without extrapolation.
"""
ConstantInterpolation
constant_interpolation

"""
etp = CubicSplineInterpolation(knots, A; bc=Line(OnGrid()), extrapolation_bc=Throw())
etp = cubic_spline_interpolation(knots, A; bc=Line(OnGrid()), extrapolation_bc=Throw())

A shorthand for `extrapolate(scale(interpolate(A, BSpline(Cubic(bc))),knots), extrapolation_bc)`.

Consider using `interpolate`, `scale`, or `extrapolate` explicitly as needed
rather than using this convenience constructor. Performance will improve
without scaling or extrapolation.
"""
CubicSplineInterpolation
cubic_spline_interpolation
5 changes: 5 additions & 0 deletions src/deprecations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,8 @@ create_bcs(it::Flag, gt::Tuple) = map(t->it(t), gt)
replace_linear_line(::Linear) = Line()
replace_linear_line(bc::BoundaryCondition) = bc
replace_linear_line(etpflag::Tuple) = replace_linear_line.(etpflag)

# Old convenience constructors
@deprecate LinearInterpolation linear_interpolation
@deprecate ConstantInterpolation constant_interpolation
@deprecate CubicSplineInterpolation cubic_spline_interpolation
6 changes: 3 additions & 3 deletions src/iterate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ multiple KnotIterator within `Iterators.product`.
```jldoctest
julia> using Interpolations;

julia> etp = LinearInterpolation([1.0, 1.2, 2.3, 3.0], rand(4); extrapolation_bc=Periodic());
julia> etp = linear_interpolation([1.0, 1.2, 2.3, 3.0], rand(4); extrapolation_bc=Periodic());

julia> kiter = knots(etp);

Expand Down Expand Up @@ -121,7 +121,7 @@ infinite sequence of knots.
```jldoctest
julia> using Interpolations;

julia> etp = LinearInterpolation([1.0, 1.2, 2.3, 3.0], rand(4); extrapolation_bc=Periodic());
julia> etp = linear_interpolation([1.0, 1.2, 2.3, 3.0], rand(4); extrapolation_bc=Periodic());

julia> Iterators.take(knots(etp), 5) |> collect
5-element $(Array{typeof(1.0),1}):
Expand Down Expand Up @@ -424,7 +424,7 @@ If no such knots exists will return a KnotIterator with length 0
```jldoctest
julia> using Interpolations;

julia> etp = LinearInterpolation([1.0, 1.2, 2.3, 3.0], rand(4); extrapolation_bc=Periodic());
julia> etp = linear_interpolation([1.0, 1.2, 2.3, 3.0], rand(4); extrapolation_bc=Periodic());

julia> knotsbetween(etp; start=38, stop=42) |> collect
6-element $(Array{typeof(1.0),1}):
Expand Down
Loading