# Reuse symbolic polynomial

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?

• Does the calculation of the polynomial change at all based on x, or only the parameters? That is, is there ever a case where to get from GetPloy[params1, x1] to GetPloy[params1, x2] is more complicated than simply substituting x2 for x1? – Jason B. Mar 11 '16 at 16:08
• It is not clear how your code looks like so maybe this question dealing with a memoization technique for certain polynomials: Why does Expand not work within a function? and answers providing correct implementations will be helpful. – Artes Mar 11 '16 at 16:21
• Seen this? – J. M. will be back soon Mar 11 '16 at 16:22
• @JasonB: Simply substituting. That's exactly what happens. But different params give different polynomials. – Per Alexandersson Mar 11 '16 at 17:27
• @PerAlexandersson Then I would definitely do it the way you have at the end of the post. – Jason B. Mar 11 '16 at 19:41