From 08c24a2c44de59f7e6e048782d5a097196c127d8 Mon Sep 17 00:00:00 2001 From: Kyle Hofmann Date: Fri, 9 Feb 2024 13:20:01 -0800 Subject: [PATCH] NotImplementedError when pow() called with modulus --- .../rings/polynomial/laurent_polynomial.pyx | 14 +++++++++++++- .../polynomial/laurent_polynomial_mpair.pyx | 14 +++++++++++++- .../polynomial/multi_polynomial_libsingular.pyx | 15 ++++++++++++++- src/sage/rings/polynomial/plural.pyx | 17 ++++++++++++++++- .../polynomial_integer_dense_flint.pyx | 15 ++++++++++++++- .../polynomial/polynomial_rational_flint.pyx | 15 ++++++++++++++- 6 files changed, 84 insertions(+), 6 deletions(-) diff --git a/src/sage/rings/polynomial/laurent_polynomial.pyx b/src/sage/rings/polynomial/laurent_polynomial.pyx index 70cec491cae..58a3ac2787d 100644 --- a/src/sage/rings/polynomial/laurent_polynomial.pyx +++ b/src/sage/rings/polynomial/laurent_polynomial.pyx @@ -1126,7 +1126,7 @@ cdef class LaurentPolynomial_univariate(LaurentPolynomial): """ return self.__u.is_monomial() - def __pow__(_self, r, dummy): + def __pow__(_self, r, mod): """ EXAMPLES:: @@ -1145,9 +1145,21 @@ cdef class LaurentPolynomial_univariate(LaurentPolynomial): x^-8 sage: (5*x^-4)^-3 5*x^12 + + Check that using third argument raises an error:: + + sage: L. = LaurentPolynomialRing(R) + sage: pow(x, 2, x) + Traceback (most recent call last): + ... + NotImplementedError: pow() with a modulus is not implemented for this ring """ cdef LaurentPolynomial_univariate self = _self cdef long right = r + if mod is not None: + raise NotImplementedError( + "pow() with a modulus is not implemented for this ring" + ) if right != r: raise ValueError("exponent must be an integer") try: diff --git a/src/sage/rings/polynomial/laurent_polynomial_mpair.pyx b/src/sage/rings/polynomial/laurent_polynomial_mpair.pyx index e6ea53e1756..be74a847c57 100644 --- a/src/sage/rings/polynomial/laurent_polynomial_mpair.pyx +++ b/src/sage/rings/polynomial/laurent_polynomial_mpair.pyx @@ -460,7 +460,7 @@ cdef class LaurentPolynomial_mpair(LaurentPolynomial): return P({e: c}) return super().__invert__() - def __pow__(LaurentPolynomial_mpair self, n, m): + def __pow__(LaurentPolynomial_mpair self, n, mod): """ EXAMPLES:: @@ -480,8 +480,20 @@ cdef class LaurentPolynomial_mpair(LaurentPolynomial): sage: f = (x+y+z^-1)^2 sage: f.substitute(z=1) x^2 + 2*x*y + y^2 + 2*x + 2*y + 1 + + Check that using third argument raises an error:: + + sage: L. = LaurentPolynomialRing(R) + sage: pow(x + y + z, 2, x) + Traceback (most recent call last): + ... + NotImplementedError: pow() with a modulus is not implemented for this ring """ cdef LaurentPolynomial_mpair ans + if mod is not None: + raise NotImplementedError( + "pow() with a modulus is not implemented for this ring" + ) if n < 0: return ~(self ** -n) ans = self._new_c() diff --git a/src/sage/rings/polynomial/multi_polynomial_libsingular.pyx b/src/sage/rings/polynomial/multi_polynomial_libsingular.pyx index d177889bd6e..bf680ffc674 100644 --- a/src/sage/rings/polynomial/multi_polynomial_libsingular.pyx +++ b/src/sage/rings/polynomial/multi_polynomial_libsingular.pyx @@ -2350,7 +2350,7 @@ cdef class MPolynomial_libsingular(MPolynomial_libsingular_base): else: return (left._parent).fraction_field()(left,right_ringelement) - def __pow__(MPolynomial_libsingular self, exp, ignored): + def __pow__(MPolynomial_libsingular self, exp, mod): """ Return ``self**(exp)``. @@ -2413,7 +2413,20 @@ cdef class MPolynomial_libsingular(MPolynomial_libsingular_base): Traceback (most recent call last): ... TypeError: R is neither an integer nor a rational + + Check that using third argument raises an error:: + + sage: R. = PolynomialRing(ZZ) + sage: pow(x + y + z, 2, x) + Traceback (most recent call last): + ... + NotImplementedError: pow() with a modulus is not implemented for this ring """ + if mod is not None: + raise NotImplementedError( + "pow() with a modulus is not implemented for this ring" + ) + if type(exp) is not Integer: try: exp = Integer(exp) diff --git a/src/sage/rings/polynomial/plural.pyx b/src/sage/rings/polynomial/plural.pyx index eb6b091c683..46289e490bb 100644 --- a/src/sage/rings/polynomial/plural.pyx +++ b/src/sage/rings/polynomial/plural.pyx @@ -1671,7 +1671,7 @@ cdef class NCPolynomial_plural(RingElement): else: return (left._parent).fraction_field()(left,right) - def __pow__(NCPolynomial_plural self, exp, ignored): + def __pow__(NCPolynomial_plural self, exp, mod): """ Return ``self**(exp)``. @@ -1697,7 +1697,22 @@ cdef class NCPolynomial_plural(RingElement): Traceback (most recent call last): .... OverflowError: exponent overflow (2147483648) + + Check that using third argument raises an error:: + + sage: A. = FreeAlgebra(QQ, 3) + sage: P = A.g_algebra(relations={y*x:-x*y + z}, order='lex') + sage: P.inject_variables() + Defining x, z, y + sage: pow(x + y + z, 2, x) + Traceback (most recent call last): + ... + NotImplementedError: pow() with a modulus is not implemented for this ring """ + if mod is not None: + raise NotImplementedError( + "pow() with a modulus is not implemented for this ring" + ) if type(exp) is not Integer: try: exp = Integer(exp) diff --git a/src/sage/rings/polynomial/polynomial_integer_dense_flint.pyx b/src/sage/rings/polynomial/polynomial_integer_dense_flint.pyx index 8aa1d13b5c1..fc2ab9b584e 100644 --- a/src/sage/rings/polynomial/polynomial_integer_dense_flint.pyx +++ b/src/sage/rings/polynomial/polynomial_integer_dense_flint.pyx @@ -1035,7 +1035,7 @@ cdef class Polynomial_integer_dense_flint(Polynomial): sig_off() return x - def __pow__(Polynomial_integer_dense_flint self, exp, ignored): + def __pow__(Polynomial_integer_dense_flint self, exp, mod): """ EXAMPLES:: @@ -1114,10 +1114,23 @@ cdef class Polynomial_integer_dense_flint(Polynomial): ... TypeError: no canonical coercion from Univariate Polynomial Ring in R over Integer Ring to Rational Field + + Check that using third argument raises an error:: + + sage: R. = PolynomialRing(ZZ, implementation='FLINT') + sage: pow(x, 2, x) + Traceback (most recent call last): + ... + NotImplementedError: pow() with a modulus is not implemented for this ring """ cdef long nn cdef Polynomial_integer_dense_flint res + if mod is not None: + raise NotImplementedError( + "pow() with a modulus is not implemented for this ring" + ) + try: nn = pyobject_to_long(exp) except TypeError: diff --git a/src/sage/rings/polynomial/polynomial_rational_flint.pyx b/src/sage/rings/polynomial/polynomial_rational_flint.pyx index df25358ff3b..ceaa9d7b2d5 100644 --- a/src/sage/rings/polynomial/polynomial_rational_flint.pyx +++ b/src/sage/rings/polynomial/polynomial_rational_flint.pyx @@ -1160,7 +1160,7 @@ cdef class Polynomial_rational_flint(Polynomial): if do_sig: sig_off() return res - def __pow__(Polynomial_rational_flint self, exp, ignored): + def __pow__(Polynomial_rational_flint self, exp, mod): """ Return ``self`` raised to the power of ``exp``. @@ -1266,10 +1266,23 @@ cdef class Polynomial_rational_flint(Polynomial): ... TypeError: no canonical coercion from Univariate Polynomial Ring in R over Rational Field to Rational Field + + Check that using third argument raises an error:: + + sage: R. = PolynomialRing(QQ) + sage: pow(x, 2, x) + Traceback (most recent call last): + ... + NotImplementedError: pow() with a modulus is not implemented for this ring """ cdef Polynomial_rational_flint res cdef long n + if mod is not None: + raise NotImplementedError( + "pow() with a modulus is not implemented for this ring" + ) + try: n = pyobject_to_long(exp) except TypeError: