Michael Day on Thu, 19 Nov 2026 23:43:07 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Qn about execution of Multinomial expressions


Thanks

On 19/11/2026 22:05, Bill Allombert wrote:
On Thu, Nov 19, 2026 at 08:41:07PM +0000, Michael Day wrote:

In an expression like
1/4*b^2*x^4 + (b^2*i - 1/2*b^2)*x^3 + (b^2*i^2 - b^2*i + (1/4*b^2 +
p*b))*x^2 + (2*p*b*i - p*b)*x + p^2
does the interpreter recalculate repeated terms such as b^2
Yes. The GP interpreter does the computation exactly as it is written.
I consider this a fundamental property.

Hitherto,  I've been precalculating such values in a my() statement,  eg
my(b2=b^2, x2=x^2, x3=x*x2,x4=x3+x, ....) ; \\ followed by
1/4*b2*x4 + (b2*i - 1/2*b2)*x3 + (b2*i2 - b2*i + (1/4*b2 + p*b))*x2 +
(2*p*b*i - p*b)*x + p2
but do I need to do so to improve calculation speed?
Yes.

But instead you can write this as a polynomial expression and use substvec
to evaluate it, which allows GP to use a better evaluation strategy.

substvec(1/4*'b^2*'x^4 + ('b^2*i - 1/2*'b^2)*'x^3 + ('b^2*'i^2 - 'b^2*'i + (1/4*'b^2 +
'p*'b))*'x^2 + (2*'p*'b*'i - 'p*'b)*'x + 'p^2,['b,'x,'i,'p],[b,x,i,p])

Cheers,
Bill.


--
This email has been checked for viruses by Avast antivirus software.
www.avast.com