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.Thu, 14 Apr 2016 01:45:04 -0500Extracting coefficients of multivariate polynomials?http://ask.sagemath.org/question/10195/extracting-coefficients-of-multivariate-polynomials/If I enter, say:
P.<x,y,z> = QQ[]
ex = 6*x*y^2-4*x*y*z+2*x^2*z
ex.coefficients()
then Sage returns
[6, -4, 2]
as it should. My problem is that I'm trying to obtain a list of coefficients when the coefficients are themselves expressions:
ex = (1-a^2)*x*y^2+(a-b^2+c)*x*y*z+(b^2-c^2-a)*x^2*z
What I want returned is
[1-a^2, a-b^2+c, b^2-c^2-a]
(the order doesn't matter). If I enter `a, b, c` as elements of the same polynomial ring as `x, y, z` then I have the problem of distinguishing the different variables: `x, y, z`, from `a, b, c`.
What's the best way to do this?
Thanks,
Alasdair
Wed, 05 Jun 2013 14:46:50 -0500http://ask.sagemath.org/question/10195/extracting-coefficients-of-multivariate-polynomials/Answer by nbruin for <p>If I enter, say:</p>
<pre><code>P.<x,y,z> = QQ[]
ex = 6*x*y^2-4*x*y*z+2*x^2*z
ex.coefficients()
</code></pre>
<p>then Sage returns</p>
<pre><code>[6, -4, 2]
</code></pre>
<p>as it should. My problem is that I'm trying to obtain a list of coefficients when the coefficients are themselves expressions:</p>
<pre><code>ex = (1-a^2)*x*y^2+(a-b^2+c)*x*y*z+(b^2-c^2-a)*x^2*z
</code></pre>
<p>What I want returned is</p>
<pre><code>[1-a^2, a-b^2+c, b^2-c^2-a]
</code></pre>
<p>(the order doesn't matter). If I enter <code>a, b, c</code> as elements of the same polynomial ring as <code>x, y, z</code> then I have the problem of distinguishing the different variables: <code>x, y, z</code>, from <code>a, b, c</code>.</p>
<p>What's the best way to do this?</p>
<p>Thanks,
Alasdair</p>
http://ask.sagemath.org/question/10195/extracting-coefficients-of-multivariate-polynomials/?answer=33099#post-id-33099Just define the appropriate polynomial rings so that `coefficients` does what you want:
sage: A.<a,b,c>=QQ[]
sage: B.<x,y,z>=A[]
sage: ex = (1-a^2)*x*y^2+(a-b^2+c)*x*y*z+(b^2-c^2-a)*x^2*z
sage: ex.coefficients()
[-a^2 + 1, b^2 - c^2 - a, -b^2 + a + c]
sage: ex.monomials()
[x*y^2, x^2*z, x*y*z]
Thu, 14 Apr 2016 01:45:04 -0500http://ask.sagemath.org/question/10195/extracting-coefficients-of-multivariate-polynomials/?answer=33099#post-id-33099Answer by calc314 for <p>If I enter, say:</p>
<pre><code>P.<x,y,z> = QQ[]
ex = 6*x*y^2-4*x*y*z+2*x^2*z
ex.coefficients()
</code></pre>
<p>then Sage returns</p>
<pre><code>[6, -4, 2]
</code></pre>
<p>as it should. My problem is that I'm trying to obtain a list of coefficients when the coefficients are themselves expressions:</p>
<pre><code>ex = (1-a^2)*x*y^2+(a-b^2+c)*x*y*z+(b^2-c^2-a)*x^2*z
</code></pre>
<p>What I want returned is</p>
<pre><code>[1-a^2, a-b^2+c, b^2-c^2-a]
</code></pre>
<p>(the order doesn't matter). If I enter <code>a, b, c</code> as elements of the same polynomial ring as <code>x, y, z</code> then I have the problem of distinguishing the different variables: <code>x, y, z</code>, from <code>a, b, c</code>.</p>
<p>What's the best way to do this?</p>
<p>Thanks,
Alasdair</p>
http://ask.sagemath.org/question/10195/extracting-coefficients-of-multivariate-polynomials/?answer=15034#post-id-15034Here is a quick hack.
map(lambda w: w.subs(x=1,y=1,z=1),ex.operands())
I don't see an obvious command to extract abstract coefficients, even `list` doesn't work.
Wed, 05 Jun 2013 15:40:40 -0500http://ask.sagemath.org/question/10195/extracting-coefficients-of-multivariate-polynomials/?answer=15034#post-id-15034Comment by Alasdair for <p>Here is a quick hack.</p>
<pre><code>map(lambda w: w.subs(x=1,y=1,z=1),ex.operands())
</code></pre>
<p>I don't see an obvious command to extract abstract coefficients, even <code>list</code> doesn't work.</p>
http://ask.sagemath.org/question/10195/extracting-coefficients-of-multivariate-polynomials/?comment=17557#post-id-17557Yep, that certainly works - thanks! I was hoping, though, that there might be a natural, simple command...Wed, 05 Jun 2013 15:53:11 -0500http://ask.sagemath.org/question/10195/extracting-coefficients-of-multivariate-polynomials/?comment=17557#post-id-17557