TransWikia.com

Cache a function in mathematica

Mathematica Asked by doublefelix on August 28, 2021

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.

One Answer

Not going to accept this answer since it's my own and I feel weird about that, but in case other people need it here it is.

Just use https://reference.wolfram.com/language/ref/FunctionInterpolation.html .

Mathematica then picks the points which are best to evaluate the function for you. No need to try to intelligently guess them. For an example function, mathematica used 177 points and the difference between the actual function and interpolated polynomial was about 10^-6 at worst.

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.

Answered by doublefelix on August 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP