Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Constant coefficient of symbolic expression

Suppose we have the following

sage: var("x,y,z")
sage: expr = x*y+z^2+4

I am looking for a function which does something like

sage: expr.constant_coefficient()
4

However, I did not find such a function. If I use coefficient I can get the desired result using

sage: expr.coefficient(x,0).coefficient(y,0).coefficient(z,0)
4

Of course if I have more variables that gets more tedious and I'd write a small helper function which goes through the variables contained in expr. I feel like this is much too complicated and I'm just overlooking some function. Is anyone aware of some cleaner way to do this if the expression contains a lot of variables?

Thank you for your help!

Constant coefficient of symbolic expression

Suppose we have the following

sage: var("x,y,z")
sage: expr = x*y+z^2+4

I am looking for a function which does something like

sage: expr.constant_coefficient()
4

However, I did not find such a function. If I use coefficient I can get the desired result using

sage: expr.coefficient(x,0).coefficient(y,0).coefficient(z,0)
4

Of course if I have more variables that gets more tedious and I'd write a small helper function which goes through the variables contained in expr. I feel like this is much too complicated and I'm just overlooking some function. Is anyone aware of some cleaner way to do this if the expression contains a lot of variables?variables (i.e. without having to write a helper-function)?

Thank you for your help!