Ask Your Question

Revision history [back]

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.

Note that we can also get the coefficient of x^i directly using f[i].

Try this to explore further.

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

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.

Note that Important note: we can also 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)