Ask Your Question
1

Coefficients of a constant polynomial

asked 2012-01-25 18:56:05 +0200

petropolis gravatar image

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 flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
4

answered 2012-01-25 19:14:18 +0200

DSM gravatar image

updated 2012-01-26 10:48:45 +0200

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2012-01-25 18:56:05 +0200

Seen: 1,817 times

Last updated: Jan 26 '12