# Memory Allocation and Interpolation

**URL:** <https://discourse.julialang.org/t/memory-allocation-and-interpolation/29015>\
**Category:** General Usage\
**Created:** [September 20, 2019, 8:42pm UTC](https://discourse.julialang.org/t/memory-allocation-and-interpolation/29015 "2019-09-20T20:42:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gideonsimpson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gideonsimpson/32/1928_2.png) [@gideonsimpson](https://discourse.julialang.org/u/gideonsimpson)\
**Post date:** [September 20, 2019, 8:42pm UTC](https://discourse.julialang.org/t/memory-allocation-and-interpolation/29015/1 "2019-09-20T20:42:13Z")

</div>

I’ve got some code where I need to perform interpolations at every step of a loop (it’s the integration in time of a PDE). While the code works, currently with `Dierckx`, I was wondering if any of the interpolation packages can be used such that the memory allocation for the interpolant only needs to be done once, and I just update the interpolation data at each iterate (the number of data points is constnat).

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [September 20, 2019, 9:28pm UTC](https://discourse.julialang.org/t/memory-allocation-and-interpolation/29015/2 "2019-09-20T21:28:05Z")

</div>

I believe this is possible with [https://github.com/JuliaMath/Interpolations.jl](https://github.com/JuliaMath/Interpolations.jl)

Even if it isn’t possible with that package, it should still be faster.

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [September 20, 2019, 9:44pm UTC](https://discourse.julialang.org/t/memory-allocation-and-interpolation/29015/3 "2019-09-20T21:44:16Z")

</div>

Im actually sure Dierckx can do evaluations into preallocated arrays.

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [September 20, 2019, 9:51pm UTC](https://discourse.julialang.org/t/memory-allocation-and-interpolation/29015/4 "2019-09-20T21:51:55Z")

</div>

Looks like I was wrong, but it would be very easy to fix; the evaluation routine

```julia
function _evaluate(t::Vector{Float64}, c::Vector{Float64}, k::Int,
                   x::Vector{Float64}, bc::Int)
    bc in (0, 1, 2, 3) || error("bc = $bc not in (0, 1, 2, 3)")
    m = length(x)
    xin = convert(Vector{Float64}, x)
    y = Vector{Float64}(undef, m)
    ier = Ref{Int32}(0)
    ccall((:splev_, libddierckx), Nothing,
         (Ref{Float64}, Ref{Int32},
          Ref{Float64}, Ref{Int32},
          Ref{Float64}, Ref{Float64}, Ref{Int32},
          Ref{Int32}, Ref{Int32}),
         t, length(t), c, k, xin, y, m, bc, ier)

    ier[] == 0 || error(_eval1d_messages[ier[]])
    return y
end

```

could be rewritten, e.g., like this

```julia
function _evaluate(t::Vector{Float64}, c::Vector{Float64}, k::Int,
                   x::Vector{Float64}, bc::Int, y=Vector{Float64}(undef, length(x)))
    bc in (0, 1, 2, 3) || error("bc = $bc not in (0, 1, 2, 3)")
    m = length(x)
    xin = convert(Vector{Float64}, x)
    ier = Ref{Int32}(0)
    ccall((:splev_, libddierckx), Nothing,
         (Ref{Float64}, Ref{Int32},
          Ref{Float64}, Ref{Int32},
          Ref{Float64}, Ref{Float64}, Ref{Int32},
          Ref{Int32}, Ref{Int32}),
         t, length(t), c, k, xin, y, m, bc, ier)

    ier[] == 0 || error(_eval1d_messages[ier[]])
    return y
end

```

this - or something similar - should do what you want. Should be a straightforward PR.

---

<div class="post-metadata">

**Author:** ![gideonsimpson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gideonsimpson/32/1928_2.png) [@gideonsimpson](https://discourse.julialang.org/u/gideonsimpson)\
**Post date:** [September 21, 2019, 10:21pm UTC](https://discourse.julialang.org/t/memory-allocation-and-interpolation/29015/5 "2019-09-21T22:21:44Z")

</div>

Interpolations appears to not yet support better than linear interpolation when working with off grid data.
