0

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?

Per Alexandersson
  • 2,469
  • 15
  • 19

0 Answers0