Ask Your Question
4

Order of coefficients of univariate polynomial: Bug or Feature?

asked 2022-05-05 10:14:31 +0100

Matthias Steiner gravatar image

I obsevered the following behaviour in SageMath 9.3 and 9.5.

sage: P.<x> = PolynomialRing(QQ)
sage: f = 1 + 2 * x + 3 * x**2
sage: f.coefficients()
[1, 2, 3]
sage: f.monomials()
[x^2, x, 1]

So the coefficients of $f$ are sorted in ascending order while the monomials are sorted in descending order. Is this a bug or a feature, because for multivariate polynomials coefficients and monomials are sorted in descending order with respect to the chosen monomial order?

sage: P.<x, y> = PolynomialRing(QQ)
sage: f = 1 + 2 * x + 3 * y + 4 * x * y
sage: f.coefficients()
[4, 2, 3, 1]
sage: f.monomials()
[x*y, x, y, 1]
edit retag flag offensive close merge delete

Comments

That inconsistent behavior in the univariate case looks undesirable to me, but changing it would break compatibility with existing code (if anyone ever used it). As a workaround you can use P.<x> = PolynomialRing(QQ, 1) to create a multivariate polynomial ring with one variable.

rburing gravatar imagerburing ( 2022-05-05 12:01:23 +0100 )edit
3

If the inconsistency is not changed, then it should at least be mentioned in the documentation. It took me quite a while to figure out why my code did not produced the desired result.

Matthias Steiner gravatar imageMatthias Steiner ( 2022-05-05 12:59:16 +0100 )edit
2

It also may be unexpected that if f = 1 + 2*x**2, then f.coefficients() will return (1, 2): by default it only includes nonzero coefficients. Note that f.exponents() returns information consistent with f.coefficients(). Maybe they're supposed to be used together?

John Palmieri gravatar imageJohn Palmieri ( 2022-05-05 18:21:19 +0100 )edit
2

I created https://trac.sagemath.org/ticket/33813 to improve the documentation for these methods.

John Palmieri gravatar imageJohn Palmieri ( 2022-05-06 01:11:14 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-05-06 19:13:44 +0100

dan_fulea gravatar image

I will say some words here, not in a comment, since the space is more generous. First of all, methods implemented for some objects are often implemented with a special purpose. They may be used in further implemented algorithms, or are implemented to provide a "library usage". The questions assumes tacitly, that the two methods coefficients and monomials of the object f, that i chose in a different way

sage: P.<x> = PolynomialRing(QQ)
sage: f = 11*x^3 - 17*x^7 + 56*x^10 + 9*x^5
sage: f.parent()
Univariate Polynomial Ring in x over Rational Field
sage: type(f)
<class 'sage.rings.polynomial.polynomial_rational_flint.Polynomial_rational_flint'>

should be related to each other. No, in this case the programmer expects one method. Let us ask for the coefficients and the monomials involved:

sage: f.coefficients()
[11, 9, -17, 56]
sage: f.monomials()
[x^10, x^7, x^5, x^3]

In the doc string of the last method we have the information:

sage: f.monomials??

::::::::::::: many lines then... :::::::::::::

Source:   
    def monomials(self):
        """
        Return the list of the monomials in ``self`` in a decreasing order of their degrees.

        EXAMPLES::

            sage: P.<x> = QQ[]
            sage: f = x^2 + (2/3)*x + 1

so it is an intention to sort in this manner. For the coefficients, the natural, "expected" behavior is kept, we can for instance ask for:

sage: f.coefficients()
[11, 9, -17, 56]
sage: f.coefficients(sparse=True)
[11, 9, -17, 56]
sage: f.coefficients(sparse=False)
[0, 0, 0, 11, 0, 9, 0, -17, 0, 0, 56]
sage: f.list()
[0, 0, 0, 11, 0, 9, 0, -17, 0, 0, 56]
sage: f.dict()
{3: 11, 5: 9, 7: -17, 10: 56}

to extract in a way or an other the information on the polynomial.

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 2022-05-05 10:14:31 +0100

Seen: 197 times

Last updated: May 06 '22