From fc3c9a18479a2aa28de6e72ddda438ac82ee23d6 Mon Sep 17 00:00:00 2001 From: Giacomo Pope Date: Fri, 1 Mar 2024 17:43:52 +0000 Subject: [PATCH 1/6] Add -Infinity as degree for LaurentPolynomialRing --- src/sage/rings/polynomial/laurent_polynomial.pyx | 12 +++++++++++- .../rings/polynomial/laurent_polynomial_mpair.pyx | 13 ++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/sage/rings/polynomial/laurent_polynomial.pyx b/src/sage/rings/polynomial/laurent_polynomial.pyx index 03de4a1179f..b5d5323643e 100644 --- a/src/sage/rings/polynomial/laurent_polynomial.pyx +++ b/src/sage/rings/polynomial/laurent_polynomial.pyx @@ -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): @@ -1018,7 +1019,7 @@ cdef class LaurentPolynomial_univariate(LaurentPolynomial): return ret def degree(self): - """ + r""" Return the degree of ``self``. EXAMPLES:: @@ -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. = 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): diff --git a/src/sage/rings/polynomial/laurent_polynomial_mpair.pyx b/src/sage/rings/polynomial/laurent_polynomial_mpair.pyx index 893b81c87af..c84a34a4a90 100644 --- a/src/sage/rings/polynomial/laurent_polynomial_mpair.pyx +++ b/src/sage/rings/polynomial/laurent_polynomial_mpair.pyx @@ -16,6 +16,7 @@ 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 minus_infinity cdef class LaurentPolynomial_mpair(LaurentPolynomial): @@ -1155,7 +1156,7 @@ cdef class LaurentPolynomial_mpair(LaurentPolynomial): return [a.eadd(self._mon) for a in self._poly.exponents()] def degree(self, x=None): - """ + r""" Return the degree of ``x`` in ``self``. EXAMPLES:: @@ -1168,7 +1169,17 @@ cdef class LaurentPolynomial_mpair(LaurentPolynomial): 1 sage: f.degree(z) 0 + + The zero polynomial is defined to have degree `-\infty`:: + + sage: R. = LaurentPolynomialRing(ZZ) + sage: R.zero().degree() + -Infinity """ + # The zero polynomial is defined to have degree -Infinity + if self.is_zero(): + return minus_infinity + if not x: return self._poly.total_degree() + sum(self._mon) From 976ad8c81c5a878d06491afbcaaa54b210df13fe Mon Sep 17 00:00:00 2001 From: Giacomo Pope Date: Fri, 1 Mar 2024 18:14:32 +0000 Subject: [PATCH 2/6] Improve docstring for degree --- .../polynomial/laurent_polynomial_mpair.pyx | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/sage/rings/polynomial/laurent_polynomial_mpair.pyx b/src/sage/rings/polynomial/laurent_polynomial_mpair.pyx index c84a34a4a90..3a75f349677 100644 --- a/src/sage/rings/polynomial/laurent_polynomial_mpair.pyx +++ b/src/sage/rings/polynomial/laurent_polynomial_mpair.pyx @@ -1157,12 +1157,15 @@ cdef class LaurentPolynomial_mpair(LaurentPolynomial): def degree(self, x=None): r""" - Return the degree of ``x`` in ``self``. + Return the degree of ``x`` in ``self``. If ``x`` is ``None`` + then insteads returns the degree of the highest degree term. EXAMPLES:: sage: R. = 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) @@ -1172,15 +1175,28 @@ cdef class LaurentPolynomial_mpair(LaurentPolynomial): The zero polynomial is defined to have degree `-\infty`:: - sage: R. = LaurentPolynomialRing(ZZ) + sage: R. = 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. = LaurentPolynomialRing(ZZ) + sage: f = x + y + z + sage: f.degree(1) + Traceback (most recent call last): + ... + TypeError: x must be a generator of parent """ # The zero polynomial is defined to have degree -Infinity if self.is_zero(): return minus_infinity - if not x: + if x is None: return self._poly.total_degree() + sum(self._mon) cdef tuple g = self._parent.gens() From 0023d7d103be6001ed6311421a75e311b252eb0f Mon Sep 17 00:00:00 2001 From: Giacomo Pope <44242839+GiacomoPope@users.noreply.github.com> Date: Mon, 4 Mar 2024 10:42:42 +0000 Subject: [PATCH 3/6] Update comment to show the degree is the total degree --- src/sage/rings/polynomial/laurent_polynomial_mpair.pyx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/sage/rings/polynomial/laurent_polynomial_mpair.pyx b/src/sage/rings/polynomial/laurent_polynomial_mpair.pyx index 3a75f349677..51cec89f3c5 100644 --- a/src/sage/rings/polynomial/laurent_polynomial_mpair.pyx +++ b/src/sage/rings/polynomial/laurent_polynomial_mpair.pyx @@ -1157,8 +1157,9 @@ cdef class LaurentPolynomial_mpair(LaurentPolynomial): def degree(self, x=None): r""" - Return the degree of ``x`` in ``self``. If ``x`` is ``None`` - then insteads returns the degree of the highest degree term. + If ``x`` is ``None``, return the total degreee of ``self``. + Otherwise, if ``x`` is generator of the ring, returns the + degree of ``x`` in ``self``. EXAMPLES:: From 52a0b30a858343df1932c2bc2d0d01e66b344a93 Mon Sep 17 00:00:00 2001 From: Giacomo Pope Date: Mon, 4 Mar 2024 14:52:03 +0000 Subject: [PATCH 4/6] Reformaty docstring --- .../rings/polynomial/laurent_polynomial_mpair.pyx | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/sage/rings/polynomial/laurent_polynomial_mpair.pyx b/src/sage/rings/polynomial/laurent_polynomial_mpair.pyx index 51cec89f3c5..74b6f57fd7b 100644 --- a/src/sage/rings/polynomial/laurent_polynomial_mpair.pyx +++ b/src/sage/rings/polynomial/laurent_polynomial_mpair.pyx @@ -1157,9 +1157,17 @@ cdef class LaurentPolynomial_mpair(LaurentPolynomial): def degree(self, x=None): r""" - If ``x`` is ``None``, return the total degreee of ``self``. - Otherwise, if ``x`` is generator of the ring, returns the - degree of ``x`` in ``self``. + Return the degree of ``self``. + + INPUT: + + - ``x`` -- (default: ``None``) a generator of the parent ring + + OUTPUT: + + If ``x`` is ``None``, return the maximum degree of the monomials of + ``self``. If ``x`` is given and it is a generator of the parent + ring, the output is the maximum degree in ``x`` of ``self``. EXAMPLES:: From 6dd48d7bd6fd9352477ba45e1da062609c7aba8a Mon Sep 17 00:00:00 2001 From: Giacomo Pope <44242839+GiacomoPope@users.noreply.github.com> Date: Thu, 7 Mar 2024 21:50:30 +0000 Subject: [PATCH 5/6] Update src/sage/rings/polynomial/laurent_polynomial_mpair.pyx Co-authored-by: Travis Scrimshaw --- src/sage/rings/polynomial/laurent_polynomial_mpair.pyx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/sage/rings/polynomial/laurent_polynomial_mpair.pyx b/src/sage/rings/polynomial/laurent_polynomial_mpair.pyx index 74b6f57fd7b..93fd74fcda9 100644 --- a/src/sage/rings/polynomial/laurent_polynomial_mpair.pyx +++ b/src/sage/rings/polynomial/laurent_polynomial_mpair.pyx @@ -1165,9 +1165,9 @@ cdef class LaurentPolynomial_mpair(LaurentPolynomial): OUTPUT: - If ``x`` is ``None``, return the maximum degree of the monomials of - ``self``. If ``x`` is given and it is a generator of the parent - ring, the output is the maximum degree in ``x`` of ``self``. + 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:: From 5a4fdd298958a264f0db5261ffe7c01ad65fea4f Mon Sep 17 00:00:00 2001 From: Giacomo Pope Date: Wed, 3 Apr 2024 11:58:15 +0100 Subject: [PATCH 6/6] Fix a failing docstring --- src/sage/rings/polynomial/laurent_polynomial_mpair.pyx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sage/rings/polynomial/laurent_polynomial_mpair.pyx b/src/sage/rings/polynomial/laurent_polynomial_mpair.pyx index fc50f83406b..17834718d1c 100644 --- a/src/sage/rings/polynomial/laurent_polynomial_mpair.pyx +++ b/src/sage/rings/polynomial/laurent_polynomial_mpair.pyx @@ -1199,7 +1199,7 @@ cdef class LaurentPolynomial_mpair(LaurentPolynomial): sage: f.degree(1) Traceback (most recent call last): ... - TypeError: x must be a generator of parent + TypeError: 1 is not a generator of parent """ # The zero polynomial is defined to have degree -Infinity if self.is_zero():