# Coefficients of a polynomial including zero coefficients

I want to obtain the list of coefficients of a polynomials, including the zero coefficients.

I tried this

R.<x> = QQ[]
f = x^4 - x^2 + 1
A = f.coefficients()
A


This gave the answer [1, -1, 1]. But I want to get [1, 0, -1, 0, 1].

edit retag close merge delete

Sort by » oldest newest most voted

### The list method and the optional parameter sparse

Short answer: use the list method.

Longer answer: use f.coefficients? to get the documentation.

This reveals an optional parameter sparse which decides whether zero coefficients are included.

### Examples

Having defined a polynomial:

sage: R.<x> = QQ[]
sage: f = x^4 - x^2 + 1


we get the list of coefficients:

sage: A = f.list()
sage: A
[1, 0, -1, 0, 1]


Then A[i] is the coefficient of x^i in f.

Important note: we can get the coefficient of x^i in f directly using f[i]. This works even beyond the degree of f:

sage: f
1
sage: f
-1
sage: f
0


Try this to explore further.

sage: f.coefficients?
sage: f.coefficients()
sage: f.coefficients(sparse=True)
sage: f.coefficients(sparse=False)

more

What's the point of having sparse=True as the default argument of coefficients()? It seems that the output is useless, without any further information: how do we know that -1 in the list [1,-1,1] is the coefficient of x^2?