# 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:

 var('y')

 A = [x, y, xy + 3y^2]

for p in A : print p.coefficients(x) 

And here is what I get:

 [[1, 1]]

 [[y, 0]]

[[3*y^2, 0], [y, 1]] 

That's fine. My next list to process is

 B = [1, x, y, xy + 3y^2] 

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?

edit retag close merge delete

Sort by » oldest newest most voted This 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.

more