Skip to content

Commit ceb2b65

Browse files
judobermkitti
andauthored
Add Previous and Next for Constant interpolation (#512)
* extended Constant docstring * Docstrings for nearest, previous, next * Extend example for previous, next * Previous and Next with Gridded Interpolation * Update docs/src/control.md * Update src/b-splines/constant.jl * Added note for const and scaling Co-authored-by: Mark Kittisopikul <mkitti@users.noreply.github.com>
1 parent e6d076a commit ceb2b65

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

docs/src/control.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ Some examples:
1414
itp = interpolate(a, BSpline(Constant()))
1515
v = itp(5.4) # returns a[5]
1616

17+
# Previous-neighbor interpolation
18+
itp = interpolate(a, BSpline(Constant(Previous)))
19+
v = itp(1.8) # returns a[1]
20+
21+
# Next-neighbor interpolation
22+
itp = interpolate(a, BSpline(Constant(Next)))
23+
v = itp(5.4) # returns a[6]
24+
1725
# (Multi)linear interpolation
1826
itp = interpolate(A, BSpline(Linear()))
1927
v = itp(3.2, 4.1) # returns 0.9*(0.8*A[3,4]+0.2*A[4,4]) + 0.1*(0.8*A[3,5]+0.2*A[4,5])
@@ -111,6 +119,10 @@ NoInterp()
111119
whereby the coordinate of the selected input vector MUST be located on a grid point. Requests for off grid
112120
coordinates results in the throwing of an error.
113121

122+
For [`Constant`](@ref) there are additional parameters. Use `Constant{Previous}()` in order to perform a previous
123+
neighbor interpolation. Use `Constant{Next}()` for a next neighbor interpolation.
124+
Note that rounding can be an issue, see [#473](https://github.com/JuliaMath/Interpolations.jl/issues/473).
125+
114126
`missing` data will naturally propagate through the interpolation,
115127
where some values will become missing. To avoid that, one can
116128
filter out the missing data points and use a gridded interpolation.

src/b-splines/constant.jl

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,32 @@
11
export Nearest, Previous, Next
22

33
abstract type ConstantInterpType end
4+
5+
"""
6+
Default parameter for `Constant` that performs *nearest-neighbor* interpolation.
7+
Can optionally be specified as
8+
```
9+
Constant() === Constant{Nearest}()
10+
```
11+
"""
412
struct Nearest <: ConstantInterpType end
13+
14+
"""
15+
Parameter for `Constant` that performs *previous-neighbor* interpolations.
16+
Applied through
17+
´´´
18+
Constant{Previous}()
19+
´´´
20+
"""
521
struct Previous <: ConstantInterpType end
22+
23+
"""
24+
Parameter for `Constant` that performs *next-neighbor* interpolations.
25+
Applied through
26+
```
27+
Constant{Next}()
28+
```
29+
"""
630
struct Next <: ConstantInterpType end
731

832
struct Constant{T<:ConstantInterpType,BC<:Union{Throw{OnGrid},Periodic{OnCell}}} <: DegreeBC{0}
@@ -30,7 +54,13 @@ end
3054

3155
"""
3256
Constant b-splines are *nearest-neighbor* interpolations, and effectively
33-
return `A[round(Int,x)]` when interpolating.
57+
return `A[round(Int,x)]` when interpolating without scaling.
58+
Scaling can lead to inaccurate position of the *neighbors* due to limited numerical precision.
59+
60+
`Constant{Previous}` interpolates to the previous value and is thus equivalent
61+
to `A[floor(Int,x)]` without scaling.
62+
`Constant{Next}` interpolates to the next value and is thus equivalent
63+
to `A[ceil(Int,x)]` without scaling.
3464
"""
3565
Constant
3666

0 commit comments

Comments
 (0)