Hi everyone,

This summer, I’m doing some research involving mathematical particle simulations. Part of my work revolves around a “shape function”, which for the purposes of this post, is simply a function that takes an input n (called the “order” of the function), and returns an output value y. Here is an example of how such a function would work.

y_test = function(n)

if n == 0:

y = 0

elseif n == 1:

y = 1

else:

y = 1 + y_test(n-1)

end

end

As you can see, this function is recursive. In order to access shape functions of higher order, you must first calculate the function for lower orders. In this example, it is very easy to manually calculate higher-order shape functions. However, in the function I am using in my research, it is much more complicated, and impractical to do it by hand. I am wondering if there was a way that I could store the compiled version of a higher-order function without having to calculate the lower-order versions? For instance, is there a way for me to access y_test(5) without having to recalculate y_test(4), y_test(3), and so on every single time I run y_test(5)? Perhaps there is a way to run the recursive loop once and from there on have access to y_test(5) without running the recursion again?

Thanks for you help!