You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note: the current version of `Interpolations` supports interpolation evaluation using index calls `[]`, but this feature will be deprecated in future. We highly recommend function calls with `()` as follows.
43
42
44
43
Given an `AbstractArray``A`, construct an "interpolation object" `itp` as
45
-
```jl
44
+
```julia
46
45
itp =interpolate(A, options...)
47
46
```
48
47
where `options...` (discussed below) controls the type of
49
48
interpolation you want to perform. This syntax assumes that the
50
49
samples in `A` are equally-spaced.
51
50
52
51
To evaluate the interpolation at position `(x, y, ...)`, simply do
53
-
```jl
52
+
```julia
54
53
v =itp(x, y, ...)
55
54
```
56
55
57
56
Some interpolation objects support computation of the gradient, which
58
57
can be obtained as
59
-
```jl
58
+
```julia
60
59
g =gradient(itp, x, y, ...)
61
60
```
62
61
or, if you're evaluating the gradient repeatedly, a somewhat more
63
62
efficient option is
64
-
```jl
63
+
```julia
65
64
gradient!(g, itp, x, y, ...)
66
65
```
67
66
where `g` is a pre-allocated vector.
68
67
69
68
Some interpolation objects support computation of the hessian, which
70
69
can be obtained as
71
-
```jl
70
+
```julia
72
71
h =hessian(itp, x, y, ...)
73
72
```
74
73
or, if you're evaluating the hessian repeatedly, a somewhat more
75
74
efficient option is
76
-
```jl
75
+
```julia
77
76
hessian!(h, itp, x, y, ...)
78
77
```
79
78
where `h` is a pre-allocated matrix.
@@ -85,25 +84,28 @@ and `Rational`, but also multi-valued types like `RGB` color vectors.
85
84
Positions `(x, y, ...)` are n-tuples of numbers. Typically these will
86
85
be real-valued (not necessarily integer-valued), but can also be of types
87
86
such as [DualNumbers](https://github.com/JuliaDiff/DualNumbers.jl) if
88
-
you want to verify the computed value of gradients. You can also use
87
+
you want to verify the computed value of gradients.
88
+
(Alternatively, verify gradients using [ForwardDiff](https://github.com/JuliaDiff/ForwardDiff.jl).)
89
+
You can also use
89
90
Julia's iterator objects, e.g.,
90
91
91
-
```jl
92
+
```julia
92
93
functionongrid!(dest, itp)
93
-
for I inCartesianRange(size(itp))
94
+
for I inCartesianIndices(itp)
94
95
dest[I] =itp(I)
95
96
end
96
97
end
97
98
```
98
99
would store the on-grid value at each grid point of `itp` in the output `dest`.
99
100
Finally, courtesy of Julia's indexing rules, you can also use
100
-
```jl
101
-
fine =itp(linspace(1,10,1001), linspace(1,15,201))
101
+
```julia
102
+
fine =itp(range(1,stop=10,length=1001), range(1,stop=15,length=201))
102
103
```
103
104
104
105
### Quickstart guide
106
+
105
107
For linear and cubic spline interpolations, `LinearInterpolation` and `CubicSplineInterpolation` can be used to create interpolation objects handily:
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.
139
141
Interested users can specify boundary conditions by providing an extra parameter for `extrapolation_bc`:
140
-
```jl
142
+
```julia
141
143
f(x) =log(x)
142
144
xs =1:0.2:5
143
145
A = [f(x) for x in xs]
144
146
145
147
# extrapolation with linear boundary conditions
146
-
extrap =LinearInterpolation(xs, A, extrapolation_bc =Interpolations.Linear())
148
+
extrap =LinearInterpolation(xs, A, extrapolation_bc =Line())
147
149
148
150
@testextrap(1-0.2) # ≈ f(1) - (f(1.2) - f(1))
149
151
@testextrap(5+0.2) # ≈ f(5) + (f(5) - f(4.8))
150
152
```
151
153
Irregular grids are supported as well; note that presently only `LinearInterpolation` supports irregular grids.
152
-
```jl
154
+
```julia
153
155
xs = [x^2for x =1:0.2:5]
154
156
A = [f(x) for x in xs]
155
157
@@ -163,34 +165,34 @@ interp_linear(1.05) # approximately log(1.05)
163
165
164
166
### BSplines
165
167
166
-
The interpolation type is described in terms of *degree*, *grid behavior* and, if necessary, *boundary conditions*. There are currently three degrees available: `Constant`, `Linear`, `Quadratic`, and `Cubic` corresponding to B-splines of degree 0, 1, 2, and 3 respectively.
167
-
168
-
You also have to specify what *grid representation* you want. There are currently two choices: `OnGrid`, in which the supplied data points are assumed to lie *on* the boundaries of the interpolation interval, and `OnCell` in which the data points are assumed to lie on half-intervals between cell boundaries.
168
+
The interpolation type is described in terms of *degree* and, if necessary, *boundary conditions*. There are currently three degrees available: `Constant`, `Linear`, `Quadratic`, and `Cubic` corresponding to B-splines of degree 0, 1, 2, and 3 respectively.
169
169
170
170
B-splines of quadratic or higher degree require solving an equation system to obtain the interpolation coefficients, and for that you must specify a *boundary condition* that is applied to close the system. The following boundary conditions are implemented: `Flat`, `Line` (alternatively, `Natural`), `Free`, `Periodic` and `Reflect`; their mathematical implications are described in detail in the pdf document under `/doc/latex`.
171
+
When specifying these boundary conditions you also have to specify whether they apply at the edge grid point (`OnGrid()`)
172
+
or beyond the edge point halfway to the next (fictitious) grid point (`OnCell()`).
which destroys the input `A` but also does not need to allocate as much memory.
196
198
@@ -199,22 +201,22 @@ which destroys the input `A` but also does not need to allocate as much memory.
199
201
BSplines assume your data is uniformly spaced on the grid `1:N`, or its multidimensional equivalent. If you have data of the form `[f(x) for x in A]`, you need to tell Interpolations about the grid `A`. If `A` is not uniformly spaced, you must use gridded interpolation described below. However, if `A` is a collection of ranges or linspaces, you can use scaled BSplines. This is more efficient because the gridded algorithm does not exploit the uniform spacing. Scaled BSplines can also be used with any spline degree available for BSplines, while gridded interpolation does not currently support quadratic or cubic splines.
0 commit comments