ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 06 May 2022 19:13:44 +0200Order of coefficients of univariate polynomial: Bug or Feature?https://ask.sagemath.org/question/62321/order-of-coefficients-of-univariate-polynomial-bug-or-feature/ 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]Thu, 05 May 2022 10:14:31 +0200https://ask.sagemath.org/question/62321/order-of-coefficients-of-univariate-polynomial-bug-or-feature/Comment by John Palmieri for <p>I obsevered the following behaviour in SageMath 9.3 and 9.5.</p>
<pre><code>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]
</code></pre>
<p>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?</p>
<pre><code>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]
</code></pre>
https://ask.sagemath.org/question/62321/order-of-coefficients-of-univariate-polynomial-bug-or-feature/?comment=62332#post-id-62332I created https://trac.sagemath.org/ticket/33813 to improve the documentation for these methods.Fri, 06 May 2022 01:11:14 +0200https://ask.sagemath.org/question/62321/order-of-coefficients-of-univariate-polynomial-bug-or-feature/?comment=62332#post-id-62332Comment by John Palmieri for <p>I obsevered the following behaviour in SageMath 9.3 and 9.5.</p>
<pre><code>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]
</code></pre>
<p>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?</p>
<pre><code>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]
</code></pre>
https://ask.sagemath.org/question/62321/order-of-coefficients-of-univariate-polynomial-bug-or-feature/?comment=62328#post-id-62328It 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?Thu, 05 May 2022 18:21:19 +0200https://ask.sagemath.org/question/62321/order-of-coefficients-of-univariate-polynomial-bug-or-feature/?comment=62328#post-id-62328Comment by Matthias Steiner for <p>I obsevered the following behaviour in SageMath 9.3 and 9.5.</p>
<pre><code>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]
</code></pre>
<p>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?</p>
<pre><code>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]
</code></pre>
https://ask.sagemath.org/question/62321/order-of-coefficients-of-univariate-polynomial-bug-or-feature/?comment=62324#post-id-62324If 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.Thu, 05 May 2022 12:59:16 +0200https://ask.sagemath.org/question/62321/order-of-coefficients-of-univariate-polynomial-bug-or-feature/?comment=62324#post-id-62324Comment by rburing for <p>I obsevered the following behaviour in SageMath 9.3 and 9.5.</p>
<pre><code>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]
</code></pre>
<p>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?</p>
<pre><code>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]
</code></pre>
https://ask.sagemath.org/question/62321/order-of-coefficients-of-univariate-polynomial-bug-or-feature/?comment=62322#post-id-62322That 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.Thu, 05 May 2022 12:01:23 +0200https://ask.sagemath.org/question/62321/order-of-coefficients-of-univariate-polynomial-bug-or-feature/?comment=62322#post-id-62322Answer by dan_fulea for <p>I obsevered the following behaviour in SageMath 9.3 and 9.5.</p>
<pre><code>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]
</code></pre>
<p>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?</p>
<pre><code>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]
</code></pre>
https://ask.sagemath.org/question/62321/order-of-coefficients-of-univariate-polynomial-bug-or-feature/?answer=62345#post-id-62345I 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.
Fri, 06 May 2022 19:13:44 +0200https://ask.sagemath.org/question/62321/order-of-coefficients-of-univariate-polynomial-bug-or-feature/?answer=62345#post-id-62345