# Speed up an interpolating function in a finite interval

I want to evaluate a 2D interpolating function many times in a finite interval. What is the fastest way to do that?

I imagine that its possible to specify a discrete grid and memoize the function on this grid. Afterwards round any input value to values on this grid and call the memoized function. But I am not sure if this is the most convenient and fastest way to do.

• I'd try fInt=Interpolation[...,InterpolationOrder->0] first, which is basically what you're describing. If that's not fast enough you can try to build your own version, but that might not be worth the effort. – N.J.Evans Nov 29 '17 at 15:17
• Try using Listable functions first, because they are vectorized. Next try Mapping your functions, and last try ParallelTable to parallelise over cores. – Thies Heidecke Nov 29 '17 at 15:17

InterpolationFunctions are listable, but not in a very obvious way. Take the following example from the documentation:

data = Flatten[Table[{{x, y}, LCM[x, y]}, {x, 4}, {y, 4}], 1]
f = Interpolation[data]


Suppose we want to evaluate f on a grid between {1, 1} and {4, 4}:

grid = N @ Flatten[CoordinateBoundingBoxArray[{{1, 1}, {4, 4}}, Into[20]], 1]


To evaluate f on the grid points in a vectorize way, we have to use the following syntax:

f @@ Transpose[grid]


Note that this is different from the way a PredictorFunction generated by Predict works: a predictor function can directly accept grid as an argument, while InterpolationFunctions expect the arguments in successive slots.