Skip to content

Commit

Permalink
gh-37605: Ensure returned degree of multivariate polynomial is type `…
Browse files Browse the repository at this point in the history
…Integer` for `MPolynomialRing_libsingular` class

    
Previously the degree of multivariate polynomials was returned as a
python `int` instead of a SageMath `Integer` resulting in ugly
`ZZ(f.degree())` calls in various places.

This PR only focuses on the case of `MPolynomialRing_libsingular` to
keep the noise of the PR as low as possible.

**Future work**: This same issue is in `MPolynomial_polydict`, I am
happy to also do the work here, or make a new PR -- really I don't like
sage returning a `int` when we dont have to.

**Edit**

This follow up work has been done in PR:
#37611
    
URL: #37605
Reported by: Giacomo Pope
Reviewer(s): Matthias Köppe
  • Loading branch information
Release Manager committed Mar 29, 2024
2 parents 5d920a9 + 49c34c4 commit 3ee8402
Showing 1 changed file with 37 additions and 6 deletions.
43 changes: 37 additions & 6 deletions src/sage/rings/polynomial/multi_polynomial_libsingular.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2671,13 +2671,25 @@ cdef class MPolynomial_libsingular(MPolynomial_libsingular_base):
1
sage: poly.degree(S.1)
2
Ensure that :issue:`37603` is fixed::
sage: R.<x, y> = QQ[]
sage: f = x + y + 1
sage: type(f.degree())
<class 'sage.rings.integer.Integer'>
sage: type(f.degree(x))
<class 'sage.rings.integer.Integer'>
sage: type(f.degree(y))
<class 'sage.rings.integer.Integer'>
"""
cdef ring *r = self._parent_ring
cdef poly *p = self._poly
if not x:
if std_grading:
return self.total_degree(std_grading=True)
return singular_polynomial_deg(p, NULL, r)
return Integer(self.total_degree(std_grading=True))
return Integer(singular_polynomial_deg(p, NULL, r))

if not x.parent() is self.parent():
try:
Expand All @@ -2687,7 +2699,7 @@ cdef class MPolynomial_libsingular(MPolynomial_libsingular_base):
if not x.is_generator():
raise TypeError("argument is not a generator")

return singular_polynomial_deg(p, x._poly, r)
return Integer(singular_polynomial_deg(p, x._poly, r))

def total_degree(self, int std_grading=False):
"""
Expand Down Expand Up @@ -2739,6 +2751,14 @@ cdef class MPolynomial_libsingular(MPolynomial_libsingular_base):
-1
sage: R(1).total_degree()
0
Ensure that :issue:`37603` is fixed::
sage: R.<x,y,z> = QQ[]
sage: f = x^4 + y + z
sage: f.total_degree()
4
sage: type(f.total_degree())
<class 'sage.rings.integer.Integer'>
"""
cdef int i, result
cdef poly *p = self._poly
Expand All @@ -2749,8 +2769,8 @@ cdef class MPolynomial_libsingular(MPolynomial_libsingular_base):
while p:
result = max(result, sum([p_GetExp(p,i,r) for i in range(1,r.N+1)]))
p = pNext(p)
return result
return singular_polynomial_deg(p, NULL, r)
return Integer(result)
return Integer(singular_polynomial_deg(p, NULL, r))

def degrees(self):
"""
Expand All @@ -2767,6 +2787,17 @@ cdef class MPolynomial_libsingular(MPolynomial_libsingular_base):
(1, 2, 1)
sage: (q + y0^5).degrees()
(5, 2, 1)
TESTS:
Ensure that :issue:`37603` is fixed::
sage: R.<x,y,z> = QQ[]
sage: f = x^4 + y + z
sage: f.degrees()
(4, 1, 1)
sage: type(f.degrees()[0])
<class 'sage.rings.integer.Integer'>
"""
cdef poly *p = self._poly
cdef ring *r = self._parent_ring
Expand All @@ -2776,7 +2807,7 @@ cdef class MPolynomial_libsingular(MPolynomial_libsingular_base):
for i from 0 <= i < r.N:
d[i] = max(d[i],p_GetExp(p, i+1, r))
p = pNext(p)
return tuple(d)
return tuple(map(Integer, d))

def coefficient(self, degrees):
"""
Expand Down

0 comments on commit 3ee8402

Please sign in to comment.