ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 15 Apr 2013 04:48:49 -0500coefficient() with composite variablehttp://ask.sagemath.org/question/10021/coefficient-with-composite-variable/Say I have something like this:
var('a','b','c')
test=b*a + c*a + 3*a + b + c
I can find that the coefficient of 'a' is (b+c+3) using:
test.coefficient(a,1)
However, I am interested in find the coefficient of $ab$ (which in this case would just be $1$). I tried
test.coefficient(a*b,1)
but it just returns zero. Is something like this possible?Mon, 15 Apr 2013 03:45:57 -0500http://ask.sagemath.org/question/10021/coefficient-with-composite-variable/Answer by slelievre for <p>Say I have something like this:</p>
<pre><code>var('a','b','c')
test=b*a + c*a + 3*a + b + c
</code></pre>
<p>I can find that the coefficient of 'a' is (b+c+3) using:</p>
<pre><code>test.coefficient(a,1)
</code></pre>
<p>However, I am interested in find the coefficient of $ab$ (which in this case would just be $1$). I tried </p>
<pre><code>test.coefficient(a*b,1)
</code></pre>
<p>but it just returns zero. Is something like this possible?</p>
http://ask.sagemath.org/question/10021/coefficient-with-composite-variable/?answer=14791#post-id-14791You can work in a polynomial ring.
sage: R.<a,b,c> = PolynomialRing(QQ)
sage: p = b * a + c * a + 3 * a + b + c
sage: p.coefficient(a*b)
1
Mon, 15 Apr 2013 04:48:49 -0500http://ask.sagemath.org/question/10021/coefficient-with-composite-variable/?answer=14791#post-id-14791