# Simplifying expression by known terms

I have expression that need to be simplified to the known terms. Below are the expression:

var('b0 a1 a2 h v_0 v_1 v_2 u_0 u_1 u_2');
expr = b0 == (16*h^2*v_0^2+(38*h*u_0-38*h*u_1)*v_0+25*u_1^2-50*u_0*u_1+25*u_0^2)/3;
expr.substitute(-h*v_0+2*u_1-2*u_0==a1,-2*(h*v_0-u_1+u_0)==a2)


The expected result is

b0 == c1*(a1)^q1 + c2*(a2)^q2


where c1, c2, q1, q2 are constants.

The output from Sage is

b0 == 16/3*h^2*v_0^2 + 25/3*u_0^2 - 50/3*u_0*u_1 + 25/3*u_1^2 + 38/3*(h*u_0 - h*u_1)*v_0

edit retag close merge delete

Sort by » oldest newest most voted

Since you symbolic expression is a polynomial, i would suggest to work in polynomial rings:

sage: R.<b0,a1,a2,h,v_0,v_1,v_2,u_0,u_1,u_2> = QQ[]
sage: R
Multivariate Polynomial Ring in b0, a1, a2, h, v_0, v_1, v_2, u_0, u_1, u_2 over Rational Field
sage: P = (16*h^2*v_0^2+(38*h*u_0-38*h*u_1)*v_0+25*u_1^2-50*u_0*u_1+25*u_0^2)/3 ; P
16/3*h^2*v_0^2 + 38/3*h*v_0*u_0 - 38/3*h*v_0*u_1 + 25/3*u_0^2 - 50/3*u_0*u_1 + 25/3*u_1^2


You can replace the substitution by working in a quotient ring:

sage: Q = R.quotient([-h*v_0+2*u_1-2*u_0-a1,-2*(h*v_0-u_1+u_0)-a2]) ; Q
Quotient of Multivariate Polynomial Ring in b0, a1, a2, h, v_0, v_1, v_2, u_0, u_1, u_2 over Rational Field by the ideal (-h*v_0 - a1 - 2*u_0 + 2*u_1, -2*h*v_0 - a2 - 2*u_0 + 2*u_1)


And see how your polynomial behaves there:

sage: Q(P)
4/3*a2bar^2 - a2bar*u_0bar + u_0bar^2 + a2bar*u_1bar - 2*u_0bar*u_1bar + u_1bar^2


You can lift the result back to the initial polynomial ring :

sage: Q(P).lift()
4/3*a2^2 - a2*u_0 + u_0^2 + a2*u_1 - 2*u_0*u_1 + u_1^2


And make it a symbolic expression if needed:

sage: SR(Q(P).lift())
4/3*a2^2 - a2*u_0 + u_0^2 + a2*u_1 - 2*u_0*u_1 + u_1^2

more