# Cache a function in mathematica

I have a function of one variable $$f(x): \mathbb{R}\to \mathbb{C}$$ which is very difficult to evaluate. Single evaluations may take hours, and I need to use the function at a large variety of points.

Despite its complexity, the graph of the function is very simple; it looks nearly gaussian.

Therefore I would like to just cache individual values of the function and use some kind of interpolation in between for the intermediate values. This interpolated approximation could then be evaluated at any point.

I have not found a nice function for cacheing in mathematica nor is it obvious to me what kind of interpolation would be suited for such a problem.

Maybe someone here has expertise which they could share with me on this issue?

I have already implemented the function as a "function which remembers values it has found" but this is not sufficient as I need speedy evaluation of intermediate points as well.

• Have a look at Interpolation. The smoother your function is, the higher a InterpolationOrder you may use. Feb 20, 2020 at 14:01
• data = Table[{x, f[x]}, {x, xmin, xmax}]; Then use Interpolation Feb 20, 2020 at 15:05
• Both very helpful thank you :) Feb 20, 2020 at 15:09
• What is still missing, though, is: the best interpolation algorithm would choose the points x at which it best samples, to get a more accurate interpolation. I guess these would be points with high curvature but I don't really know. Is there such an option in mathematica? Of course I can choose the points myself and then interpolate but the result won't be as precise and unfortunately my application requires a good amount of precision Feb 20, 2020 at 15:25
• The result of Interpolation is a piecewise function (InterpolatingFunction) and will go exactly through all the points. Feb 20, 2020 at 17:34

It only works for a fixed interval. Therefore I had to come up with bounds $$[a,b]$$ and outside of these bounds, my function was effectively 0 for my purposes anyway.