Change Array in Interpolation without recreating weights

I’d like to change the Array of data within an interpolation without recreating a new instance of the interpolant. Here is a MWE:

I am using the same nodes in each instance, so it seems there should be a performance gain of modifying the Array inplace. However, I can’t figure out how.

using Interpolations

n = 3
A = zeros(n)
B = ones(n)

nodes = (1.0:n,)
scheme = Gridded(Linear())

itp = interpolate(nodes, A, scheme)
itp(2.5) # Outputs 0.0

itp = interpolate(nodes, B, scheme)
itp(2.5) # Outputs 1.0

It doesn’t appear that Interpolations.jl provides a method to modify the underlying array in place. However, I wouldn’t really worry about the performance of creating a new interpolant. The interpolate function simply wraps up the provided data into a struct, so there’s almost no computation going on. The real computation occurs when you call itp(2.5).

Also, since (1.0:n,) is immutable, you don’t really need to worry about avoiding copies. The compiler can decide for itself whether or not to copy immutable values.

In general don’t worry too much about optimizing your code for speed until you observe that it is running too slow. If you observe that your code is running too slow, then you can use the profiler to figure out where the bottleneck in your code is.

2 Likes