Ask Your Question
1

Coefficients of a polynomial including zero coefficients

asked 2020-07-23 02:14:56 -0500

Laurian gravatar image

updated 2020-07-23 03:24:58 -0500

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

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
1

answered 2020-07-23 03:17:24 -0500

slelievre gravatar image

updated 2020-07-23 03:22:54 -0500

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

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

Comments

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 04:17:50 -0500 )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

Stats

Asked: 2020-07-23 02:14:56 -0500

Seen: 45 times

Last updated: Jul 23