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 10:15:34 -0500Extract coefficients of polynomialshttp://ask.sagemath.org/question/33096/extract-coefficients-of-polynomials/ I have the following code:
var('a')
_.<k> = PolynomialRing(ZZ)
f = k^3+2*k^2+1
g = k^3 + a*k^2 + 1
f.coefficients()
g.coefficients()
the coefficients of f that i get are perfect:
[1, 2, 1]
the coefficients of g should be [1,a,1], but instead i get:
[[k^3 + 1, 0], [k^2, 1]]
I've tried the solution suggested [here](http://ask.sagemath.org/question/10195/extracting-coefficients-of-multivariate-polynomials/):
but it doesn't always produce the coefficients in a logical order.
As always, any help figuring out how to get the correct coefficients of g would be greatly appreciated.Wed, 13 Apr 2016 22:59:50 -0500http://ask.sagemath.org/question/33096/extract-coefficients-of-polynomials/Answer by nbruin for <p>I have the following code:</p>
<pre><code>var('a')
_.<k> = PolynomialRing(ZZ)
f = k^3+2*k^2+1
g = k^3 + a*k^2 + 1
f.coefficients()
g.coefficients()
</code></pre>
<p>the coefficients of f that i get are perfect:</p>
<pre><code>[1, 2, 1]
</code></pre>
<p>the coefficients of g should be [1,a,1], but instead i get:</p>
<pre><code> [[k^3 + 1, 0], [k^2, 1]]
</code></pre>
<p>I've tried the solution suggested <a href="http://ask.sagemath.org/question/10195/extracting-coefficients-of-multivariate-polynomials/">here</a>:</p>
<p>but it doesn't always produce the coefficients in a logical order.</p>
<p>As always, any help figuring out how to get the correct coefficients of g would be greatly appreciated.</p>
http://ask.sagemath.org/question/33096/extract-coefficients-of-polynomials/?answer=33097#post-id-33097One solution is to force g into the univariate polynomial ring with respect to which you want to take coefficients:
sage: ZZ['a']['k'](g).coefficients()
[1, a, 1]
By mixing `var('a')` and `_.<k>` you're creating an element that has a rather complicated representation. You're better off either sticking entirely with `var('a,k')` or with defining the appropriate polynomial ring explicitly.Thu, 14 Apr 2016 01:41:55 -0500http://ask.sagemath.org/question/33096/extract-coefficients-of-polynomials/?answer=33097#post-id-33097Comment by sophia for <p>One solution is to force g into the univariate polynomial ring with respect to which you want to take coefficients:</p>
<pre><code>sage: ZZ['a']['k'](g).coefficients()
[1, a, 1]
</code></pre>
<p>By mixing <code>var('a')</code> and <code>_.<k></code> you're creating an element that has a rather complicated representation. You're better off either sticking entirely with <code>var('a,k')</code> or with defining the appropriate polynomial ring explicitly.</p>
http://ask.sagemath.org/question/33096/extract-coefficients-of-polynomials/?comment=33108#post-id-33108that works, thank you!Thu, 14 Apr 2016 10:15:34 -0500http://ask.sagemath.org/question/33096/extract-coefficients-of-polynomials/?comment=33108#post-id-33108