Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

In order to extract "by code" (not "by eyes"), there should be clear which is the general input. Let us assume we have a general polynomial equation eq. Then eq.lhs() - eq.rhs() is a polynomial and we have only to extract its free coefficient. To get the right method, it is worth to convert it to a true sage polynomial. As it comes, it is rather an expression.

Here is a more detailed dialog with the sage interpreter for our present case.

sage: var( 'x,y,z' );
sage: eq = (x - 1)^2 - (x - 2)^2 - (y - 1)^2 + y^2 + (z - 3)^2 - (z - 4)^2 == 1.75000000000000
sage: eq = eq.simplify_full()
sage: eq
2*x + 2*y + 2*z - 11 == 1.75
sage: eq = eq.subtract_from_both_sides( eq.rhs() )
sage: eq
2*x + 2*y + 2*z - 12.75 == 0.0
sage: LHS = eq.lhs()
sage: LHS
2*x + 2*y + 2*z - 12.75
# sage: LHS.coefficients?
sage: LHS.coefficients(x,sparse=False)
[2*y + 2*z - 12.75, 2]
sage: LHS.coefficients(x,sparse=False)[0]
2*y + 2*z - 12.75
sage: LHS . coefficients(x,sparse=False)[0] . coefficients(y,sparse=False)[0]
2*z - 12.75
sage: LHS . coefficients(x,sparse=False)[0] . coefficients(y,sparse=False)[0] . coefficients(z,sparse=False)[0]
-12.75

The "quick" way would have been:

sage: var( 'x,y,z' );
sage: eq = (x - 1)^2 - (x - 2)^2 - (y - 1)^2 + y^2 + (z - 3)^2 - (z - 4)^2 == 1.75000000000000
sage: P = eq.lhs() - eq.rhs()
sage: PP = P.polynomial(QQ)
sage: x,y,z = PP.parent().gens()
sage: PP.coefficient( {x:0, y:0, z:0 } )
-51/4

(The "quick" way can be converted rather quickly to a general solution.)