Skip to content

Commit 447ec74

Browse files
committed
Improve the documentation for the convenience notation
1 parent 550b6c9 commit 447ec74

File tree

2 files changed

+84
-65
lines changed

2 files changed

+84
-65
lines changed

README.md

Lines changed: 68 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -104,71 +104,7 @@ Finally, courtesy of Julia's indexing rules, you can also use
104104
fine = itp(range(1,stop=10,length=1001), range(1,stop=15,length=201))
105105
```
106106

107-
### Quickstart guide
108-
109-
For linear and cubic spline interpolations, `LinearInterpolation` and `CubicSplineInterpolation` can be used to create interpolation objects handily:
110-
```julia
111-
f(x) = log(x)
112-
xs = 1:0.2:5
113-
A = [f(x) for x in xs]
114-
115-
# linear interpolation
116-
interp_linear = LinearInterpolation(xs, A)
117-
interp_linear(3) # exactly log(3)
118-
interp_linear(3.1) # approximately log(3.1)
119-
120-
# cubic spline interpolation
121-
interp_cubic = CubicSplineInterpolation(xs, A)
122-
interp_cubic(3) # exactly log(3)
123-
interp_cubic(3.1) # approximately log(3.1)
124-
```
125-
which support multidimensional data as well:
126-
```julia
127-
f(x,y) = log(x+y)
128-
xs = 1:0.2:5
129-
ys = 2:0.1:5
130-
A = [f(x,y) for x in xs, y in ys]
131-
132-
# linear interpolation
133-
interp_linear = LinearInterpolation((xs, ys), A)
134-
interp_linear(3, 2) # exactly log(3 + 2)
135-
interp_linear(3.1, 2.1) # approximately log(3.1 + 2.1)
136-
137-
# cubic spline interpolation
138-
interp_cubic = CubicSplineInterpolation((xs, ys), A)
139-
interp_cubic(3, 2) # exactly log(3 + 2)
140-
interp_cubic(3.1, 2.1) # approximately log(3.1 + 2.1)
141-
```
142-
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.
143-
Interested users can specify boundary conditions by providing an extra parameter for `extrapolation_bc`:
144-
```julia
145-
f(x) = log(x)
146-
xs = 1:0.2:5
147-
A = [f(x) for x in xs]
148-
149-
# extrapolation with linear boundary conditions
150-
extrap = LinearInterpolation(xs, A, extrapolation_bc = Line())
151-
152-
@test extrap(1 - 0.2) # ≈ f(1) - (f(1.2) - f(1))
153-
@test extrap(5 + 0.2) # ≈ f(5) + (f(5) - f(4.8))
154-
```
155-
You can also use a "fill" value, which gets returned whenever you ask for out-of-range values:
156-
157-
```julia
158-
extrap = LinearInterpolation(xs, A, extrapolation_bc = NaN)
159-
@test isnan(extrap(5.2))
160-
```
161-
162-
Irregular grids are supported as well; note that presently only `LinearInterpolation` supports irregular grids.
163-
```julia
164-
xs = [x^2 for x = 1:0.2:5]
165-
A = [f(x) for x in xs]
166-
167-
# linear interpolation
168-
interp_linear = LinearInterpolation(xs, A)
169-
interp_linear(1) # exactly log(1)
170-
interp_linear(1.05) # approximately log(1.05)
171-
```
107+
There is also an abbreviated notion [described below](#convenience-notation).
172108

173109
## Control of interpolation algorithm
174110

@@ -323,6 +259,73 @@ etpf = extrapolate(itp, Flat()) # gives 1 on the left edge and 7 on the right
323259
etp0 = extrapolate(itp, 0) # gives 0 everywhere outside [1,7]
324260
```
325261

262+
## Convenience notation
263+
264+
For linear and cubic spline interpolations, `LinearInterpolation` and `CubicSplineInterpolation`
265+
can be used to create interpolating and extrapolating objects handily:
266+
```julia
267+
f(x) = log(x)
268+
xs = 1:0.2:5
269+
A = [f(x) for x in xs]
270+
271+
# linear interpolation
272+
interp_linear = LinearInterpolation(xs, A)
273+
interp_linear(3) # exactly log(3)
274+
interp_linear(3.1) # approximately log(3.1)
275+
276+
# cubic spline interpolation
277+
interp_cubic = CubicSplineInterpolation(xs, A)
278+
interp_cubic(3) # exactly log(3)
279+
interp_cubic(3.1) # approximately log(3.1)
280+
```
281+
which support multidimensional data as well:
282+
```julia
283+
f(x,y) = log(x+y)
284+
xs = 1:0.2:5
285+
ys = 2:0.1:5
286+
A = [f(x,y) for x in xs, y in ys]
287+
288+
# linear interpolation
289+
interp_linear = LinearInterpolation((xs, ys), A)
290+
interp_linear(3, 2) # exactly log(3 + 2)
291+
interp_linear(3.1, 2.1) # approximately log(3.1 + 2.1)
292+
293+
# cubic spline interpolation
294+
interp_cubic = CubicSplineInterpolation((xs, ys), A)
295+
interp_cubic(3, 2) # exactly log(3 + 2)
296+
interp_cubic(3.1, 2.1) # approximately log(3.1 + 2.1)
297+
```
298+
For extrapolation, i.e., when interpolation objects are evaluated in coordinates outside the range provided in constructors, the default option for a boundary condition is `Throw` so that they will return an error.
299+
Interested users can specify boundary conditions by providing an extra parameter for `extrapolation_bc`:
300+
```julia
301+
f(x) = log(x)
302+
xs = 1:0.2:5
303+
A = [f(x) for x in xs]
304+
305+
# extrapolation with linear boundary conditions
306+
extrap = LinearInterpolation(xs, A, extrapolation_bc = Line())
307+
308+
@test extrap(1 - 0.2) # ≈ f(1) - (f(1.2) - f(1))
309+
@test extrap(5 + 0.2) # ≈ f(5) + (f(5) - f(4.8))
310+
```
311+
You can also use a "fill" value, which gets returned whenever you ask for out-of-range values:
312+
313+
```julia
314+
extrap = LinearInterpolation(xs, A, extrapolation_bc = NaN)
315+
@test isnan(extrap(5.2))
316+
```
317+
318+
Irregular grids are supported as well; note that presently only `LinearInterpolation` supports irregular grids.
319+
```julia
320+
xs = [x^2 for x = 1:0.2:5]
321+
A = [f(x) for x in xs]
322+
323+
# linear interpolation
324+
interp_linear = LinearInterpolation(xs, A)
325+
interp_linear(1) # exactly log(1)
326+
interp_linear(1.05) # approximately log(1.05)
327+
```
328+
326329
## Performance shootout
327330

328331
In the `perf` directory, you can find a script that tests

src/convenience-constructors.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,19 @@ LinearInterpolation(ranges::NTuple{N,AbstractVector}, vs::AbstractArray{T,N};
1818
CubicSplineInterpolation(ranges::NTuple{N,AbstractRange}, vs::AbstractArray{T,N};
1919
bc = Line(OnGrid()), extrapolation_bc = Throw()) where {N,T} =
2020
extrapolate(scale(interpolate(vs, BSpline(Cubic(bc))), ranges...), extrapolation_bc)
21+
22+
"""
23+
etp = LinearInterpolation(knots, A; extrapolation_bc=Throw())
24+
25+
A shorthand for `extrapolate(interpolate(knots, A, scheme), extrapolation_bc)`,
26+
where `scheme` is either `BSpline(Linear())` or `Gridded(Linear())` depending on whether
27+
`knots` are ranges or vectors.
28+
"""
29+
LinearInterpolation
30+
31+
"""
32+
etp = CubicSplineInterpolation(knots, A; bc=Line(OnGrid()), extrapolation_bc=Throw())
33+
34+
A shorthand for `extrapolate(interpolate(knots, A, BSpline(Cubic(bc))), extrapolation_bc)`.
35+
"""
36+
CubicSplineInterpolation

0 commit comments

Comments
 (0)