Suppose I have a function that returns a polynomial in x
,
GetPoly[params_,x_]:=GetPoly[params,x]...
but it is quite slow, so I memoize this.
However, doing GetPoly[params,2y]
or similar do not utilize the memoized data, it does the same computation now with 2y
.
What is the best way to reuse the existing computation?
The solution I can cook up is to use an auxiliary function that computes the polynomial symbolically, and then substitute with the actual symbol/value:
GetPoly[params_,x_]:=AuxPoly[params,y]/.{y->x};
AuxPoly[params_,x_]:=AuxPoly[params,x]...
Is this the best solution to my problem? What is the convention to do this in a package?
x
, or only the parameters? That is, is there ever a case where to get fromGetPloy[params1, x1]
toGetPloy[params1, x2]
is more complicated than simply substitutingx2
forx1
? $\endgroup$ – Jason B. Mar 11 '16 at 16:08