I have a function defined something like this:
f[x_] := Module[...];
I can easily write a memoized version of it like this:
f2[x_] := f2[x] = f[x];
And then replace every instance of
f2[...] in my code.
However, I have code that uses
f directly in many places (the actual situation is more complicated: I have several such functions that call each other).
Can I retroactively memoize f? Something like this:
(* set ftemp to the original version of f *) ftemp = f; (* memoize the original f *) f[x_] := f[x] = ftemp[x];
This (and several variants) don't work, probably because
f is defined as
= (ie, it's evaluated when called).
When I do
?f, I see the definition of
f. If I could somehow copy this to
ftemp, I think my trick above would work.
Any thoughts on how I could do this and/or solve the general problem of memoizing retroactively?