Skip to content

Commit

Permalink
Trac #34653: __floordiv__ for sparse polynomials
Browse files Browse the repository at this point in the history
Before this ticket, the following would raise an `ArithmeticError` in
the sparse case:
{{{
sage: P.<x,y> = PolynomialRing(SR)
sage: R.<z> = PolynomialRing(P, sparse=False); R
Univariate Polynomial Ring in z over Multivariate Polynomial Ring in x,
y over Symbolic Ring
sage: (x*z) // x
z
sage: R.<z> = PolynomialRing(P, sparse=True); R
Sparse Univariate Polynomial Ring in z over Multivariate Polynomial Ring
in x, y over Symbolic Ring
sage: (x*z) // x
z
}}}

URL: https://trac.sagemath.org/34653
Reported by: mantepse
Ticket author(s): Martin Rubey
Reviewer(s): Travis Scrimshaw
  • Loading branch information
Release Manager committed Oct 16, 2022
2 parents 66ad265 + 9577386 commit 9c07190
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/sage/rings/polynomial/polynomial_element_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

from sage.libs.pari.all import pari_gen
from sage.structure.richcmp import richcmp, richcmp_item, rich_to_bool, rich_to_bool_sgn
from sage.structure.element import coerce_binop
from sage.structure.element import coerce_binop, parent

from sage.rings.infinity import infinity, Infinity
from sage.rings.integer_ring import ZZ
Expand Down Expand Up @@ -543,6 +543,29 @@ def degree(self, gen=None):
return -1
return max(self.__coeffs)

def __floordiv__(self, right):
"""
Return the quotient upon division (no remainder).
EXAMPLES::
sage: R.<x> = PolynomialRing(QQbar, sparse=True)
sage: f = (1+2*x)^3 + 3*x; f
8*x^3 + 12*x^2 + 9*x + 1
sage: g = f // (1+2*x); g
4*x^2 + 4*x + 5/2
sage: f - g * (1+2*x)
-3/2
sage: f.quo_rem(1+2*x)
(4*x^2 + 4*x + 5/2, -3/2)
"""
P = self.parent()
if P is parent(right):
return self._floordiv_(right)
d = P.base_ring()(right)
return self.map_coefficients(lambda c: c // d)

def _add_(self, right):
r"""
EXAMPLES::
Expand Down

0 comments on commit 9c07190

Please sign in to comment.