From 88be79de1227485ee7573dc4749ff1aec6eeffd5 Mon Sep 17 00:00:00 2001 From: Kwankyu Lee Date: Tue, 30 Jan 2024 12:41:43 +0900 Subject: [PATCH 1/4] Use map_coefficients --- src/sage/rings/polynomial/multi_polynomial.pyx | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/sage/rings/polynomial/multi_polynomial.pyx b/src/sage/rings/polynomial/multi_polynomial.pyx index ea4ce506e98..10e1b1c4263 100644 --- a/src/sage/rings/polynomial/multi_polynomial.pyx +++ b/src/sage/rings/polynomial/multi_polynomial.pyx @@ -871,16 +871,23 @@ cdef class MPolynomial(CommutativePolynomial): TESTS: - Check that :trac:`25022` is fixed:: + Check that :issue:`25022` is fixed:: sage: K. = ZZ[] sage: (x*y).change_ring(SR).monomials() # needs sage.rings.number_field sage.symbolic [x*y] + + Check that :issue:`36832` is fixed:: + + sage: F = GF(11) + sage: phi = Hom(F,F).an_element() + sage: R. = F[] + sage: x.change_ring(phi) + x """ if isinstance(R, Map): - #if we're given a hom of the base ring extend to a poly hom if R.domain() == self.base_ring(): - R = self.parent().hom(R, self.parent().change_ring(R.codomain())) + return self.map_coefficients(R) return R(self) else: return self.parent().change_ring(R)(self.dict()) From 3688c189bf4aba97da7bb061e52aa6e3fb4fd337 Mon Sep 17 00:00:00 2001 From: Kwankyu Lee Date: Tue, 30 Jan 2024 12:41:48 +0900 Subject: [PATCH 2/4] Minor style changes --- src/sage/rings/polynomial/multi_polynomial.pyx | 11 ++++++----- .../polynomial/multi_polynomial_libsingular.pyx | 17 ++++++++--------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/sage/rings/polynomial/multi_polynomial.pyx b/src/sage/rings/polynomial/multi_polynomial.pyx index 10e1b1c4263..7ab6804c0d8 100644 --- a/src/sage/rings/polynomial/multi_polynomial.pyx +++ b/src/sage/rings/polynomial/multi_polynomial.pyx @@ -2,13 +2,15 @@ r""" Base class for elements of multivariate polynomial rings """ -#***************************************************************************** +# ******************************************************************** +# Copyright (C) 2005 William Stein +# # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # https://www.gnu.org/licenses/ -#***************************************************************************** +# ******************************************************************** from sage.rings.integer cimport Integer from sage.rings.integer_ring import ZZ @@ -33,9 +35,9 @@ from sage.rings.polynomial.polynomial_element cimport Polynomial cdef class MPolynomial(CommutativePolynomial): - #################### + # ------------------------- # Some standard conversions - #################### + # ------------------------- def _scalar_conversion(self, R): r""" TESTS:: @@ -360,7 +362,6 @@ cdef class MPolynomial(CommutativePolynomial): """ return multi_derivative(self, args) - def polynomial(self, var): r""" Let ``var`` be one of the variables of the parent of ``self``. This diff --git a/src/sage/rings/polynomial/multi_polynomial_libsingular.pyx b/src/sage/rings/polynomial/multi_polynomial_libsingular.pyx index f519400c3c3..70386eb0b50 100644 --- a/src/sage/rings/polynomial/multi_polynomial_libsingular.pyx +++ b/src/sage/rings/polynomial/multi_polynomial_libsingular.pyx @@ -149,13 +149,15 @@ AUTHORS: """ -#***************************************************************************** +# ******************************************************************** +# Copyright (C) 2005 William Stein +# # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. -# http://www.gnu.org/licenses/ -#***************************************************************************** +# https://www.gnu.org/licenses/ +# ******************************************************************** # The Singular API is as follows: # @@ -1530,9 +1532,9 @@ cdef class MPolynomialRing_libsingular(MPolynomialRing_base): return old - ### The following methods are handy for implementing Groebner - ### basis algorithms. They do only superficial type/sanity checks - ### and should be called carefully. + # The following methods are handy for implementing Groebner + # basis algorithms. They do only superficial type/sanity checks + # and should be called carefully. def monomial_quotient(self, MPolynomial_libsingular f, MPolynomial_libsingular g, coeff=False): r""" @@ -1677,7 +1679,6 @@ cdef class MPolynomialRing_libsingular(MPolynomialRing_base): else: return True - def monomial_lcm(self, MPolynomial_libsingular f, MPolynomial_libsingular g): """ LCM for monomials. Coefficients are ignored. @@ -5242,7 +5243,6 @@ cdef class MPolynomial_libsingular(MPolynomial_libsingular_base): y += base_map(c)*mul([ im_gens[i]**m[i] for i in range(n) if m[i]]) return y - def _derivative(self, MPolynomial_libsingular var): """ Differentiates this polynomial with respect to the provided @@ -5694,7 +5694,6 @@ cdef class MPolynomial_libsingular(MPolynomial_libsingular_base): i.append( new_MP(self._parent, pDiff(self._poly, k))) return i - def numerator(self): """ Return a numerator of self computed as self * self.denominator() From 3b8ae11e87653f07bdf7685ec936ab319a3f2ac5 Mon Sep 17 00:00:00 2001 From: Kwankyu Lee Date: Tue, 30 Jan 2024 13:38:02 +0900 Subject: [PATCH 3/4] Combined with change_ring() in multi_polynomial_element.py --- .../rings/polynomial/multi_polynomial.pyx | 26 +++++++++++--- .../polynomial/multi_polynomial_element.py | 34 ------------------- 2 files changed, 21 insertions(+), 39 deletions(-) diff --git a/src/sage/rings/polynomial/multi_polynomial.pyx b/src/sage/rings/polynomial/multi_polynomial.pyx index 7ab6804c0d8..2926f251382 100644 --- a/src/sage/rings/polynomial/multi_polynomial.pyx +++ b/src/sage/rings/polynomial/multi_polynomial.pyx @@ -841,12 +841,14 @@ cdef class MPolynomial(CommutativePolynomial): def change_ring(self, R): r""" - Return a copy of this polynomial but with coefficients in ``R``, - if at all possible. + Return this polynomial with coefficients converted to ``R``. INPUT: - - ``R`` -- a ring or morphism. + - ``R`` -- a ring or morphism; if a morphism, the coefficients + are mapped to the codomain of ``R`` + + OUTPUT: a new polynomial with the base ring changed to ``R``. EXAMPLES:: @@ -854,10 +856,14 @@ cdef class MPolynomial(CommutativePolynomial): sage: f = x^3 + 3/5*y + 1 sage: f.change_ring(GF(7)) x^3 + 2*y + 1 + sage: g = x^2 + 5*y + sage: g.change_ring(GF(5)) + x^2 :: - sage: R. = GF(9,'a')[] # needs sage.rings.finite_rings + sage: # needs sage.rings.finite_rings + sage: R. = GF(9,'a')[] sage: (x+2*y).change_ring(GF(3)) x - y @@ -870,12 +876,22 @@ cdef class MPolynomial(CommutativePolynomial): sage: f.change_ring(K.embeddings(CC)[1]) x^2 + (-0.500000000000000 - 0.866025403784438*I)*y + :: + + sage: # needs sage.rings.number_field + sage: K. = CyclotomicField(5) + sage: R. = K[] + sage: f = x^2 + w*y + sage: f.change_ring(K.embeddings(QQbar)[1]) + x^2 + (-0.8090169943749474? + 0.5877852522924731?*I)*y + TESTS: Check that :issue:`25022` is fixed:: + sage: # needs sage.rings.number_field sage.symbolic sage: K. = ZZ[] - sage: (x*y).change_ring(SR).monomials() # needs sage.rings.number_field sage.symbolic + sage: (x*y).change_ring(SR).monomials() [x*y] Check that :issue:`36832` is fixed:: diff --git a/src/sage/rings/polynomial/multi_polynomial_element.py b/src/sage/rings/polynomial/multi_polynomial_element.py index f5c1b0e480c..0369cb2edbd 100644 --- a/src/sage/rings/polynomial/multi_polynomial_element.py +++ b/src/sage/rings/polynomial/multi_polynomial_element.py @@ -412,40 +412,6 @@ def __rpow__(self, n): def element(self): return self.__element - def change_ring(self, R): - r""" - Change the base ring of this polynomial to ``R``. - - INPUT: - - - ``R`` -- ring or morphism. - - OUTPUT: a new polynomial converted to ``R``. - - EXAMPLES:: - - sage: R. = QQ[] - sage: f = x^2 + 5*y - sage: f.change_ring(GF(5)) - x^2 - - :: - - sage: # needs sage.rings.number_field - sage: K. = CyclotomicField(5) - sage: R. = K[] - sage: f = x^2 + w*y - sage: f.change_ring(K.embeddings(QQbar)[1]) - x^2 + (-0.8090169943749474? + 0.5877852522924731?*I)*y - """ - if isinstance(R, Morphism): - #if we're given a hom of the base ring extend to a poly hom - if R.domain() == self.base_ring(): - R = self.parent().hom(R, self.parent().change_ring(R.codomain())) - return R(self) - else: - return self.parent().change_ring(R)(self) - class MPolynomial_polydict(Polynomial_singular_repr, MPolynomial_element): r""" From fb1b108f85f0834f90ab7454ef6f9da0a0111078 Mon Sep 17 00:00:00 2001 From: Kwankyu Lee Date: Tue, 30 Jan 2024 14:12:25 +0900 Subject: [PATCH 4/4] Simplify isinstance(R, Map) case --- src/sage/rings/polynomial/multi_polynomial.pyx | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/sage/rings/polynomial/multi_polynomial.pyx b/src/sage/rings/polynomial/multi_polynomial.pyx index 2926f251382..370e5ef3f81 100644 --- a/src/sage/rings/polynomial/multi_polynomial.pyx +++ b/src/sage/rings/polynomial/multi_polynomial.pyx @@ -867,6 +867,17 @@ cdef class MPolynomial(CommutativePolynomial): sage: (x+2*y).change_ring(GF(3)) x - y + :: + + sage: # needs sage.rings.finite_rings + sage: F. = GF(7^2) + sage: R. = F[] + sage: f = x^2 + a^2*y^2 + a*x + a^3*y + sage: g = f.change_ring(F.frobenius_endomorphism()); g + x^2 + (-a - 2)*y^2 + (-a + 1)*x + (2*a + 2)*y + sage: g.change_ring(F.frobenius_endomorphism()) == f + True + :: sage: # needs sage.rings.number_field @@ -903,11 +914,8 @@ cdef class MPolynomial(CommutativePolynomial): x """ if isinstance(R, Map): - if R.domain() == self.base_ring(): - return self.map_coefficients(R) - return R(self) - else: - return self.parent().change_ring(R)(self.dict()) + return self.map_coefficients(R) + return self.parent().change_ring(R)(self.dict()) def is_symmetric(self, group=None): r"""