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
Copy file name to clipboardExpand all lines: docs/src/interface.md
+54-1Lines changed: 54 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -32,11 +32,64 @@ A2(300.0)
32
32
```
33
33
34
34
!!! note
35
-
35
+
36
36
The values computed beyond the range of the time points provided during interpolation will not be reliable, as these methods only perform well within the range and the first/last piece polynomial fit is extrapolated on either side which might not reflect the true nature of the data.
37
37
38
38
The keyword `cache_parameters = true` can be passed to precalculate parameters at initialization, making evaluations cheaper to compute. This is not compatible with modifying `u` and `t`. The default `cache_parameters = false` does however not prevent allocation in every interpolation constructor call.
39
39
40
+
### In-place Evaluation (Allocation-free)
41
+
42
+
When performance is critical, such as in ODE solvers or tight loops, you can use the in-place variant to avoid memory allocations. This is particularly useful when you need to interpolate at many points repeatedly.
43
+
44
+
To use in-place interpolation, pass a pre-allocated output array as the first argument:
The in-place form `interp(out, t)` writes the interpolated values directly into `out`, avoiding allocation of a new array. The output array must have the same length as the input time vector.
58
+
59
+
For vector-of-arrays data (where `u` is a `Vector{Vector}` or `Vector{Matrix}`), the output should be a pre-allocated vector of arrays with the same structure:
For multi-dimensional data (where `u` is a matrix or higher-dimensional array), the output array should have the same leading dimensions as `u`, with the last dimension matching the length of `t`:
75
+
76
+
```@example interface
77
+
# Matrix example (stacked form)
78
+
u_matrix = [14.7 11.51 10.41 14.95 12.24 11.22;
79
+
7.35 5.76 5.21 7.48 6.12 5.61]
80
+
A_matrix = CubicSpline(u_matrix, t)
81
+
82
+
# Pre-allocate for 3 evaluation points
83
+
out_matrix = zeros(2, 3)
84
+
A_matrix(out_matrix, t_eval_3)
85
+
86
+
out_matrix
87
+
```
88
+
89
+
!!! tip "Performance tip"
90
+
91
+
Using in-place interpolation can significantly reduce allocations in performance-critical code. For example, in an ODE derivative function, switching from `interp(t_vec)` to `interp(out, t_vec)` can eliminate allocations entirely within the hot loop.
92
+
40
93
## Derivatives
41
94
42
95
Derivatives of the interpolated curves can also be computed at any point for all the methods. Derivatives upto second order is supported where first order derivative is computed analytically and second order using `ForwardDiff.jl`. Order is passed as the third argument. It is 1 by default.
0 commit comments