Ask Your Question

Revision history [back]

The internal representation is dense. You can (efficiently) get the dense list of coefficients as follows:

sage: p.coefficients(sparse=False)
[1, 0, 2]
sage: p.list()
[1, 0, 2]

Note that the implementation of the coefficients method is the following:

def coefficients(self, sparse=True):
    """
    Return the coefficients of the monomials appearing in self.
    If ``sparse=True`` (the default), it returns only the non-zero coefficients.
    Otherwise, it returns the same value as ``self.list()``.
    (In this case, it may be slightly faster to invoke ``self.list()`` directly.)

    EXAMPLES::

        sage: _.<x> = PolynomialRing(ZZ)
        sage: f = x^4 + 2*x^2 + 1
        sage: f.coefficients()
        [1, 2, 1]
        sage: f.coefficients(sparse=False)
        [1, 0, 2, 0, 1]
    """
    zero = self._parent.base_ring().zero()
    if sparse:
        return [c for c in self.list() if c != zero]
    else:
        return self.list()

The internal representation is dense. You can (efficiently) get the dense list of coefficients as follows:

sage: p.coefficients(sparse=False)
[1, 0, 2]
sage: p.list()
[1, 0, 2]

Note that the implementation of the coefficients method (for the polynomial ring constructed with sparse=False) is the following:

def coefficients(self, sparse=True):
    """
    Return the coefficients of the monomials appearing in self.
    If ``sparse=True`` (the default), it returns only the non-zero coefficients.
    Otherwise, it returns the same value as ``self.list()``.
    (In this case, it may be slightly faster to invoke ``self.list()`` directly.)

    EXAMPLES::

        sage: _.<x> = PolynomialRing(ZZ)
        sage: f = x^4 + 2*x^2 + 1
        sage: f.coefficients()
        [1, 2, 1]
        sage: f.coefficients(sparse=False)
        [1, 0, 2, 0, 1]
    """
    zero = self._parent.base_ring().zero()
    if sparse:
        return [c for c in self.list() if c != zero]
    else:
        return self.list()