Ask Your Question

Coefficients of a polynomial including zero coefficients

asked 2020-07-23 09:14:56 +0200

Laurian gravatar image

updated 2020-07-23 10:24:58 +0200

slelievre gravatar image

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()

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

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2020-07-23 10:17:24 +0200

slelievre gravatar image

updated 2020-07-23 10:22:54 +0200

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.


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[0]
sage: f[2]
sage: f[9]

Try this to explore further.

sage: f.coefficients?
sage: f.coefficients()
sage: f.coefficients(sparse=True)
sage: f.coefficients(sparse=False)
edit flag offensive delete link 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?

eric_g gravatar imageeric_g ( 2020-07-24 11:17:50 +0200 )edit

Your Answer

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

Add Answer

Question Tools


Asked: 2020-07-23 09:14:56 +0200

Seen: 920 times

Last updated: Jul 23 '20