ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 11 Aug 2020 09:40:38 +0200Constant coefficient of symbolic expressionhttps://ask.sagemath.org/question/52938/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 (i.e. without having to write a helper-function)?
Thank you for your help!Tue, 11 Aug 2020 08:50:59 +0200https://ask.sagemath.org/question/52938/constant-coefficient-of-symbolic-expression/Answer by rburing for <p>Suppose we have the following</p>
<pre><code>sage: var("x,y,z")
sage: expr = x*y+z^2+4
</code></pre>
<p>I am looking for a function which does something like</p>
<pre><code>sage: expr.constant_coefficient()
4
</code></pre>
<p>However, I did not find such a function. If I use <code>coefficient</code> I can get the desired result using</p>
<pre><code>sage: expr.coefficient(x,0).coefficient(y,0).coefficient(z,0)
4
</code></pre>
<p>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 <code>expr</code>. 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 (i.e. without having to write a helper-function)?</p>
<p>Thank you for your help!</p>
https://ask.sagemath.org/question/52938/constant-coefficient-of-symbolic-expression/?answer=52939#post-id-52939You can set all variables to zero:
sage: var("x,y,z")
sage: expr = x*y+z^2+4
sage: 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())
4Tue, 11 Aug 2020 09:05:13 +0200https://ask.sagemath.org/question/52938/constant-coefficient-of-symbolic-expression/?answer=52939#post-id-52939Comment by philipp7 for <p>You can set all variables to zero:</p>
<pre><code>sage: var("x,y,z")
sage: expr = x*y+z^2+4
sage: expr.subs({v : 0 for v in expr.variables()})
4
</code></pre>
<p>If you are working with polynomials, consider using polynomial ring instead:</p>
<pre><code>sage: R.<x,y,z> = PolynomialRing(QQ)
sage: expr = x*y+z^2+4
sage: expr.constant_coefficient()
4
</code></pre>
<p>Also you can convert back and forth between symbolic expressions and polynomials:</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/52938/constant-coefficient-of-symbolic-expression/?comment=52940#post-id-52940Thanks! I thought about setting the variables to 0 as well but clearly that expression gets very ugly and hard to read. Converting to polynomials is a great way to do it, thanks!Tue, 11 Aug 2020 09:09:18 +0200https://ask.sagemath.org/question/52938/constant-coefficient-of-symbolic-expression/?comment=52940#post-id-52940Comment by rburing for <p>You can set all variables to zero:</p>
<pre><code>sage: var("x,y,z")
sage: expr = x*y+z^2+4
sage: expr.subs({v : 0 for v in expr.variables()})
4
</code></pre>
<p>If you are working with polynomials, consider using polynomial ring instead:</p>
<pre><code>sage: R.<x,y,z> = PolynomialRing(QQ)
sage: expr = x*y+z^2+4
sage: expr.constant_coefficient()
4
</code></pre>
<p>Also you can convert back and forth between symbolic expressions and polynomials:</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/52938/constant-coefficient-of-symbolic-expression/?comment=52941#post-id-52941You're welcome! (I updated the answer to make the substitution method more readable.)Tue, 11 Aug 2020 09:15:20 +0200https://ask.sagemath.org/question/52938/constant-coefficient-of-symbolic-expression/?comment=52941#post-id-52941Comment by Emmanuel Charpentier for <p>You can set all variables to zero:</p>
<pre><code>sage: var("x,y,z")
sage: expr = x*y+z^2+4
sage: expr.subs({v : 0 for v in expr.variables()})
4
</code></pre>
<p>If you are working with polynomials, consider using polynomial ring instead:</p>
<pre><code>sage: R.<x,y,z> = PolynomialRing(QQ)
sage: expr = x*y+z^2+4
sage: expr.constant_coefficient()
4
</code></pre>
<p>Also you can convert back and forth between symbolic expressions and polynomials:</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/52938/constant-coefficient-of-symbolic-expression/?comment=52943#post-id-52943Possible shortcut:
sage: expr.polynomial(ZZ).constant_coefficient()
HTH,Tue, 11 Aug 2020 09:40:38 +0200https://ask.sagemath.org/question/52938/constant-coefficient-of-symbolic-expression/?comment=52943#post-id-52943