Polynomial Simplification

108 Views Asked by At

I've been working with Maxima and its thrown me an expression like this

$$ \left( \left( \left( 6\,{w}_{1}-6\right) \,{p}_{2}-6\,{p}_{1}\,{w}_{1}+6\,{p}_{0}\right) \,{w}_{2}^{2}+\left( \left( 6\,{w}_{1}-6\,{w}_{1}^{2}\right) \,{p}_{2}+6\,{p}_{1}\,{w}_{1}^{2}+\left( 6\,{p}_{1}-12\,{p}_{0}\right) \,{w}_{1}\right) \,{w}_{2}+\left( 6\,{p}_{0}-6\,{p}_{1}\right) \,{w}_{1}^{2}\right) \,{t}_{3}+\left( \left( \left( 6-6\,{w}_{1}\right) \,{t}_{2}+6\,{t}_{1}\,{w}_{1}-6\,{t}_{0}\right) \,{w}_{2}^{2}+\left( \left( 6\,{w}_{1}^{2}-6\,{w}_{1}\right) \,{t}_{2}-6\,{t}_{1}\,{w}_{1}^{2}+\left( 12\,{t}_{0}-6\,{t}_{1}\right) \,{w}_{1}\right) \,{w}_{2}+\left( 6\,{t}_{1}-6\,{t}_{0}\right) \,{w}_{1}^{2}\right) \,{p}_{3}+\left( \left( 6\,{p}_{0}\,{w}_{1}-6\,{p}_{0}\right) \,{t}_{2}+\left( 6\,{t}_{0}-6\,{t}_{0}\,{w}_{1}\right) \,{p}_{2}+\left( 6\,{t}_{0}\,{p}_{1}-6\,{p}_{0}\,{t}_{1}\right) \,{w}_{1}\right) \,{w}_{2}^{2}+\left( \left( 6\,{p}_{0}\,{w}_{1}-6\,{p}_{0}\,{w}_{1}^{2}\right) \,{t}_{2}+\left( 6\,{t}_{0}\,{w}_{1}^{2}-6\,{t}_{0}\,{w}_{1}\right) \,{p}_{2}+\left( 6\,{p}_{0}\,{t}_{1}-6\,{t}_{0}\,{p}_{1}\right) \,{w}_{1}^{2}+\left( 6\,{p}_{0}\,{t}_{1}-6\,{t}_{0}\,{p}_{1}\right) \,{w}_{1}\right) \,{w}_{2}+\left( 6\,{t}_{0}\,{p}_{1}-6\,{p}_{0}\,{t}_{1}\right) \,{w}_{1}^{2} $$

I can see that this polynomial can be simplified, e.g. over 3 dozen multiplications by 6 can be replaced by a single multiplication by 6, and lots of computations of $t_1 - t_0$ can be precomputed. This is important because I'm going to evaluating this expression (and others like it, generated by Maxima) hundreds of thousands of times in a computer program. Can anyone recommend a symbolic maths application that does a better job than Maxima on such tasks.

Here is the formula readable for Maxima:

(((6*w[1]-6)*p[2]-6*p[1]*w[1]+6*p[0])*w[2]^2+((6*w[1]-6*w[1]^2)*p[2]+6*p[1]*w[1]^2+(6*p[1]-12*p[0])*w[1])*w[2]+(6*p[0]-6*p[1])*w[1]^2)*t[3]+(((6-6*w[1])*t[2]+6*t[1]*w[1]-6*t[0])*w[2]^2+((6*w[1]^2-6*w[1])*t[2]-6*t[1]*w[1]^2+(12*t[0]-6*t[1])*w[1])*w[2]+(6*t[1]-6*t[0])*w[1]^2)*p[3]+((6*p[0]*w[1]-6*p[0])*t[2]+(6*t[0]-6*t[0]*w[1])*p[2]+(6*t[0]*p[1]-6*p[0]*t[1])*w[1])*w[2]^2+((6*p[0]*w[1]-6*p[0]*w[1]^2)*t[2]+(6*t[0]*w[1]^2-6*t[0]*w[1])*p[2]+(6*p[0]*t[1]-6*t[0]*p[1])*w[1]^2+(6*p[0]*t[1]-6*t[0]*p[1])*w[1])*w[2]+(6*t[0]*p[1]-6*p[0]*t[1])*w[1]^2
2

There are 2 best solutions below

4
On

If I set $X$ to the expression and execute factor(X) I get $$6\,\left(w_{2}-w_{1}\right)\,\left(w_{1}\,p_{2}\,w_{2}\,t_{3}-p_{2} \,w_{2}\,t_{3}-p_{1}\,w_{1}\,w_{2}\,t_{3}+p_{0}\,w_{2}\,t_{3}+p_{1} \,w_{1}\,t_{3}-p_{0}\,w_{1}\,t_{3}-w_{1}\,t_{2}\,w_{2}\,p_{3}+t_{2} \,w_{2}\,p_{3}+t_{1}\,w_{1}\,w_{2}\,p_{3}-t_{0}\,w_{2}\,p_{3}-t_{1} \,w_{1}\,p_{3}+t_{0}\,w_{1}\,p_{3}+p_{0}\,w_{1}\,t_{2}\,w_{2}-p_{0} \,t_{2}\,w_{2}-t_{0}\,w_{1}\,p_{2}\,w_{2}+t_{0}\,p_{2}\,w_{2}-p_{0} \,t_{1}\,w_{1}\,w_{2}+t_{0}\,p_{1}\,w_{1}\,w_{2}+p_{0}\,t_{1}\,w_{1} -t_{0}\,p_{1}\,w_{1}\right)$$

Not sure what further simplification is possible.

Added in response to comment

Here is how I would isolate the $w$'s (you can do the same for other variables also:

X:(((6*w[1]-6)*p[2]-6*p[1]*w[1]+6*p[0])*w[2]^2+((6*w[1]-6*w[1]^2)*p[2]+6*p[1]*w[1]^2+(6*p[1]-12*p[0])*w[1])*w[2]+(6*p[0]-6*p[1])*w[1]^2)*t[3]+(((6-6*w[1])*t[2]+6*t[1]*w[1]-6*t[0])*w[2]^2+((6*w[1]^2-6*w[1])*t[2]-6*t[1]*w[1]^2+(12*t[0]-6*t[1])*w[1])*w[2]+(6*t[1]-6*t[0])*w[1]^2)*p[3]+((6*p[0]*w[1]-6*p[0])*t[2]+(6*t[0]-6*t[0]*w[1])*p[2]+(6*t[0]*p[1]-6*p[0]*t[1])*w[1])*w[2]^2+((6*p[0]*w[1]-6*p[0]*w[1]^2)*t[2]+(6*t[0]*w[1]^2-6*t[0]*w[1])*p[2]+(6*p[0]*t[1]-6*t[0]*p[1])*w[1]^2+(6*p[0]*t[1]-6*t[0]*p[1])*w[1])*w[2]+(6*t[0]*p[1]-6*p[0]*t[1])*w[1]^2;

Y:factor(X/(6*(w[1]-w[2])))$
    C:factor(diff(diff(Y,w[1]),w[2]))$
Y2:factor(Y-C*w[1]*w[2])$
    A:diff(Y2,w[1])$
B:diff(Y2, w[2])$
(A*w[1]+B*w[2]+C*w[1]*w[2])*6*(w[1]-w[2]);
factor(%-X);

So your expression is $$ 6 (w_1 - w_2 ) ~(A w_1 + B w_2 + C w_1 w_2) $$

I use the wxmaxima front end.

0
On

You can try something like the following. I obtained this using Maple's codegen[optimize].

t30 = w[1]
t28 = t30^2
t42 = 6*t28
t41 = 6*t30
t32 = t[1]
t40 = -6*t32
t34 = p[1]
t39 = 6*t34
t23 = -t30+t28
t29 = w[2]
t38 = t23*t29
t33 = p[2]
t35 = t[0]
t37 = t35*t33
t36 = p[0]
t31 = t[2]
t27 = t29^2
t26 = t41-6
t25 = t36*t30
t22 = t32*t36-t34*t35
t1 = ((t26*t33-6*t30*t34+6*t36)*t27+(t28*t39+(t39-12*t36)*t30)*t29-6*t33*t38+(t36-t34)*t42)*t[3]+((-t26*t31+t32*t41-6*t35)*t27+(t28*t40+(12*t35+t40)*t30)
*t29+6*t31*t38+(t32-t35)*t42)*p[3]+6*((-t36+t25)*t31+t37+(-t22-t37)*t30)*t27+6*((-t28*t36+t25)*t31+t23*t37+(t28+t30)*t22)*t29-6*t22*t28