Skip to content

Commit

Permalink
gh-37513: Define the zero polynomial to have degree -Infinity for `…
Browse files Browse the repository at this point in the history
…LaurentPolynomialRing`

    
At the moment `LaurentPolynomialRing` computes the degree of the zero
polynomial element to be `-1`, which is ambiguous as `R(1/x)` also has
degree `-1`.

This PR changes the behaviour for both univariate and multivariate
`LaurentPolynomialRings` so the degree of the zero polynomial is
`-Infinity`. This is a common definition for the zero polynomial.

This PR has been made following the discussion on `sage-devel`:
https://groups.google.com/g/sage-devel/c/XVz-4kqWuuo about exactly when
the degree of the zero polynomial should be `-1`, `-Infinity` or
undefined.


```py
sage: R.<x> = LaurentPolynomialRing(ZZ)
sage: R(1/x).degree()
-1
sage: R.zero().degree()
-Infinity
sage:
sage: R.<x, y> = LaurentPolynomialRing(ZZ)
sage: R(1/x).degree()
-1
sage: R.zero().degree()
-Infinity
```

Fixes #37491
    
URL: #37513
Reported by: Giacomo Pope
Reviewer(s): Giacomo Pope, Martin Rubey, Travis Scrimshaw
  • Loading branch information
Release Manager committed Apr 24, 2024
2 parents 04ecb27 + abd4188 commit 6ae7938
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
12 changes: 11 additions & 1 deletion src/sage/rings/polynomial/laurent_polynomial.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ from sage.misc.derivative import multi_derivative
from sage.rings.polynomial.polynomial_element import Polynomial
from sage.rings.polynomial.polynomial_ring import is_PolynomialRing
from sage.structure.richcmp cimport richcmp, rich_to_bool
from sage.rings.infinity import minus_infinity


cdef class LaurentPolynomial(CommutativeAlgebraElement):
Expand Down Expand Up @@ -1018,7 +1019,7 @@ cdef class LaurentPolynomial_univariate(LaurentPolynomial):
return ret

def degree(self):
"""
r"""
Return the degree of ``self``.
EXAMPLES::
Expand All @@ -1030,7 +1031,16 @@ cdef class LaurentPolynomial_univariate(LaurentPolynomial):
sage: g = -10/x^5 + x^2 - x^7
sage: g.degree()
7
The zero polynomial is defined to have degree `-\infty`::
sage: R.<x> = LaurentPolynomialRing(ZZ)
sage: R.zero().degree()
-Infinity
"""
# The zero polynomial is defined to have degree -Infinity
if self.is_zero():
return minus_infinity
return self.__u.degree() + self.__n

def __neg__(self):
Expand Down
44 changes: 40 additions & 4 deletions src/sage/rings/polynomial/laurent_polynomial_mpair.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ from sage.structure.factorization import Factorization
from sage.misc.derivative import multi_derivative
from sage.rings.polynomial.polydict cimport monomial_exponent
from sage.matrix.matrix0 cimport Matrix
from sage.rings.infinity import Infinity
from sage.rings.infinity import Infinity, minus_infinity


cdef class LaurentPolynomial_mpair(LaurentPolynomial):
"""
Expand Down Expand Up @@ -1155,21 +1156,56 @@ cdef class LaurentPolynomial_mpair(LaurentPolynomial):
return [a.eadd(self._mon) for a in self._poly.exponents()]

def degree(self, x=None):
"""
Return the degree of ``x`` in ``self``.
r"""
Return the degree of ``self``.
INPUT:
- ``x`` -- (default: ``None``) a generator of the parent ring
OUTPUT:
If ``x`` is ``None``, return the total degree of ``self``.
If ``x`` is a given generator of the parent ring,
the output is the maximum degree of ``x`` in ``self``.
EXAMPLES::
sage: R.<x,y,z> = LaurentPolynomialRing(QQ)
sage: f = 4*x^7*z^-1 + 3*x^3*y + 2*x^4*z^-2 + x^6*y^-7
sage: f.degree()
6
sage: f.degree(x)
7
sage: f.degree(y)
1
sage: f.degree(z)
0
The zero polynomial is defined to have degree `-\infty`::
sage: R.<x, y, z> = LaurentPolynomialRing(ZZ)
sage: R.zero().degree()
-Infinity
sage: R.zero().degree(x)
-Infinity
sage: R.zero().degree(x) == R.zero().degree(y) == R.zero().degree(z)
True
TESTS::
sage: R.<x, y, z> = LaurentPolynomialRing(ZZ)
sage: f = x + y + z
sage: f.degree(1)
Traceback (most recent call last):
...
TypeError: 1 is not a generator of parent
"""
if not x:
# The zero polynomial is defined to have degree -Infinity
if self.is_zero():
return minus_infinity

if x is None:
return self._poly.total_degree() + sum(self._mon)

# Get the index of the generator or error
Expand Down

0 comments on commit 6ae7938

Please sign in to comment.