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.Wed, 25 Jan 2012 19:14:18 +0100Coefficients of a constant polynomialhttps://ask.sagemath.org/question/8668/coefficients-of-a-constant-polynomial/If have a list A of polynomials in x and y and
want the coefficients of x. Here is what I do:
<code>
var('y')
A = [x, y, x*y + 3*y^2]
for p in A :
print p.coefficients(x)
</code>
And here is what I get:
<code>
[[1, 1]]
[[y, 0]]
[[3*y^2, 0], [y, 1]]
</code>
That's fine. My next list to process is
<code>
B = [1, x, y, x*y + 3*y^2]
</code>
And here is what I get:
AttributeError: 'sage.rings.integer.Integer' object has no attribute 'coefficients'.
I expected the answer 0 as the coefficient of x of the polynomial p(x, y) = 1 is 0.
How do I get around this behavior of Sage?
Wed, 25 Jan 2012 18:56:05 +0100https://ask.sagemath.org/question/8668/coefficients-of-a-constant-polynomial/Answer by DSM for <p>If have a list A of polynomials in x and y and
want the coefficients of x. Here is what I do:</p>
<p><code>
var('y')</code></p><code>
<p>A = [x, y, x<em>y + 3</em>y^2]</p>
</code><p><code>for p in A :
print p.coefficients(x)
</code></p>
<p>And here is what I get:</p>
<p><code>
[[1, 1]]</code></p><code>
<p>[[y, 0]]</p>
</code><p><code>[[3*y^2, 0], [y, 1]]
</code></p>
<p>That's fine. My next list to process is </p>
<p><code>
B = [1, x, y, x<em>y + 3</em>y^2]
</code></p>
<p>And here is what I get: </p>
<p>AttributeError: 'sage.rings.integer.Integer' object has no attribute 'coefficients'.</p>
<p>I expected the answer 0 as the coefficient of x of the polynomial p(x, y) = 1 is 0.</p>
<p>How do I get around this behavior of Sage? </p>
https://ask.sagemath.org/question/8668/coefficients-of-a-constant-polynomial/?answer=13193#post-id-13193This is because 1 is an Integer, not a polynomial or a symbolic expression:
sage: var("y")
y
sage: B = [1, x, y, x*y+3*y*2]
sage:
sage: for term in B:
....: print term, 'has parent', parent(term)
....:
1 has parent Integer Ring
x has parent Symbolic Ring
y has parent Symbolic Ring
x*y + 6*y has parent Symbolic Ring
In Sage, you often coerce objects which live in one location into another (viewing "1" as a complex number, for example) by calling the parent. In this case, we can convert 1 by calling SR:
sage: parent(1)
Integer Ring
sage: SR(1)
1
sage: parent(SR(1))
Symbolic Ring
So in this case:
sage: for p in B:
....: print p, SR(p).coefficients(x)
....:
1 [[1, 0]]
x [[1, 1]]
y [[y, 0]]
x*y + 6*y [[6*y, 0], [y, 1]]
This is using the symbolic ring. There are more fundamental polynomial objects (type "PolynomialRing?" to look at some examples), but this should suffice here.
Wed, 25 Jan 2012 19:14:18 +0100https://ask.sagemath.org/question/8668/coefficients-of-a-constant-polynomial/?answer=13193#post-id-13193