Ask Your Question

Revision history [back]

You can set all variables to zero:

sage: var("x,y,z")
sage: expr = x*y+z^2+4
sage: expr.subs(dict(zip(expr.variables(),[0]*len(expr.variables()))))
4

If you are working with polynomials, consider using polynomial ring instead:

sage: R.<x,y,z> = PolynomialRing(QQ)
sage: expr = x*y+z^2+4
sage: expr.constant_coefficient()
4

Also you can convert back and forth between symbolic expressions and polynomials:

sage: var("x,y,z")
sage: expr = x*y+z^2+4
sage: R = PolynomialRing(QQ, names='x,y,z')
sage: SR(R(expr).constant_coefficient())
4

You can set all variables to zero:

sage: var("x,y,z")
sage: expr = x*y+z^2+4
sage: expr.subs(dict(zip(expr.variables(),[0]*len(expr.variables()))))
expr.subs({v : 0 for v in expr.variables()})
4

If you are working with polynomials, consider using polynomial ring instead:

sage: R.<x,y,z> = PolynomialRing(QQ)
sage: expr = x*y+z^2+4
sage: expr.constant_coefficient()
4

Also you can convert back and forth between symbolic expressions and polynomials:

sage: var("x,y,z")
sage: expr = x*y+z^2+4
sage: R = PolynomialRing(QQ, names='x,y,z')
sage: SR(R(expr).constant_coefficient())
4