Skip to content

Commit 02fbbdd

Browse files
author
Release Manager
committed
gh-37159: Fix `change_ring` method of multi-variate polynomials <!-- ^^^^^ Please provide a concise, informative and self-explanatory title. Don't put issue numbers in there, do this in the PR body below. For example, instead of "Fixes #1234" use "Introduce new method to calculate 1+1" --> <!-- Describe your changes here in detail --> Fixes #36832. <!-- Why is this change required? What problem does it solve? --> <!-- If this PR resolves an open issue, please link to it here. For example "Fixes #12345". --> <!-- If your change requires a documentation PR, please link it appropriately. --> ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> <!-- If your change requires a documentation PR, please link it appropriately --> <!-- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> <!-- Feel free to remove irrelevant items. --> - [x] The title is concise, informative, and self-explanatory. - [ ] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation accordingly. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on - #12345: short description why this is a dependency - #34567: ... --> <!-- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> URL: #37159 Reported by: Kwankyu Lee Reviewer(s): Gonzalo Tornaría, Kwankyu Lee
2 parents b6a4bb5 + fb1b108 commit 02fbbdd

File tree

3 files changed

+57
-60
lines changed

3 files changed

+57
-60
lines changed

src/sage/rings/polynomial/multi_polynomial.pyx

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ r"""
22
Base class for elements of multivariate polynomial rings
33
"""
44

5-
#*****************************************************************************
5+
# ********************************************************************
6+
# Copyright (C) 2005 William Stein <[email protected]>
7+
#
68
# This program is free software: you can redistribute it and/or modify
79
# it under the terms of the GNU General Public License as published by
810
# the Free Software Foundation, either version 2 of the License, or
911
# (at your option) any later version.
1012
# https://www.gnu.org/licenses/
11-
#*****************************************************************************
13+
# ********************************************************************
1214

1315
from sage.rings.integer cimport Integer
1416
from sage.rings.integer_ring import ZZ
@@ -33,9 +35,9 @@ from sage.rings.polynomial.polynomial_element cimport Polynomial
3335

3436
cdef class MPolynomial(CommutativePolynomial):
3537

36-
####################
38+
# -------------------------
3739
# Some standard conversions
38-
####################
40+
# -------------------------
3941
def _scalar_conversion(self, R):
4042
r"""
4143
TESTS::
@@ -360,7 +362,6 @@ cdef class MPolynomial(CommutativePolynomial):
360362
"""
361363
return multi_derivative(self, args)
362364

363-
364365
def polynomial(self, var):
365366
r"""
366367
Let ``var`` be one of the variables of the parent of ``self``. This
@@ -840,26 +841,43 @@ cdef class MPolynomial(CommutativePolynomial):
840841

841842
def change_ring(self, R):
842843
r"""
843-
Return a copy of this polynomial but with coefficients in ``R``,
844-
if at all possible.
844+
Return this polynomial with coefficients converted to ``R``.
845845
846846
INPUT:
847847
848-
- ``R`` -- a ring or morphism.
848+
- ``R`` -- a ring or morphism; if a morphism, the coefficients
849+
are mapped to the codomain of ``R``
850+
851+
OUTPUT: a new polynomial with the base ring changed to ``R``.
849852
850853
EXAMPLES::
851854
852855
sage: R.<x,y> = QQ[]
853856
sage: f = x^3 + 3/5*y + 1
854857
sage: f.change_ring(GF(7))
855858
x^3 + 2*y + 1
859+
sage: g = x^2 + 5*y
860+
sage: g.change_ring(GF(5))
861+
x^2
856862
857863
::
858864
859-
sage: R.<x,y> = GF(9,'a')[] # needs sage.rings.finite_rings
865+
sage: # needs sage.rings.finite_rings
866+
sage: R.<x,y> = GF(9,'a')[]
860867
sage: (x+2*y).change_ring(GF(3))
861868
x - y
862869
870+
::
871+
872+
sage: # needs sage.rings.finite_rings
873+
sage: F.<a> = GF(7^2)
874+
sage: R.<x,y> = F[]
875+
sage: f = x^2 + a^2*y^2 + a*x + a^3*y
876+
sage: g = f.change_ring(F.frobenius_endomorphism()); g
877+
x^2 + (-a - 2)*y^2 + (-a + 1)*x + (2*a + 2)*y
878+
sage: g.change_ring(F.frobenius_endomorphism()) == f
879+
True
880+
863881
::
864882
865883
sage: # needs sage.rings.number_field
@@ -869,21 +887,35 @@ cdef class MPolynomial(CommutativePolynomial):
869887
sage: f.change_ring(K.embeddings(CC)[1])
870888
x^2 + (-0.500000000000000 - 0.866025403784438*I)*y
871889
890+
::
891+
892+
sage: # needs sage.rings.number_field
893+
sage: K.<w> = CyclotomicField(5)
894+
sage: R.<x,y> = K[]
895+
sage: f = x^2 + w*y
896+
sage: f.change_ring(K.embeddings(QQbar)[1])
897+
x^2 + (-0.8090169943749474? + 0.5877852522924731?*I)*y
898+
872899
TESTS:
873900
874-
Check that :trac:`25022` is fixed::
901+
Check that :issue:`25022` is fixed::
875902
903+
sage: # needs sage.rings.number_field sage.symbolic
876904
sage: K.<x,y> = ZZ[]
877-
sage: (x*y).change_ring(SR).monomials() # needs sage.rings.number_field sage.symbolic
905+
sage: (x*y).change_ring(SR).monomials()
878906
[x*y]
907+
908+
Check that :issue:`36832` is fixed::
909+
910+
sage: F = GF(11)
911+
sage: phi = Hom(F,F).an_element()
912+
sage: R.<x,y> = F[]
913+
sage: x.change_ring(phi)
914+
x
879915
"""
880916
if isinstance(R, Map):
881-
#if we're given a hom of the base ring extend to a poly hom
882-
if R.domain() == self.base_ring():
883-
R = self.parent().hom(R, self.parent().change_ring(R.codomain()))
884-
return R(self)
885-
else:
886-
return self.parent().change_ring(R)(self.dict())
917+
return self.map_coefficients(R)
918+
return self.parent().change_ring(R)(self.dict())
887919

888920
def is_symmetric(self, group=None):
889921
r"""

src/sage/rings/polynomial/multi_polynomial_element.py

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -412,40 +412,6 @@ def __rpow__(self, n):
412412
def element(self):
413413
return self.__element
414414

415-
def change_ring(self, R):
416-
r"""
417-
Change the base ring of this polynomial to ``R``.
418-
419-
INPUT:
420-
421-
- ``R`` -- ring or morphism.
422-
423-
OUTPUT: a new polynomial converted to ``R``.
424-
425-
EXAMPLES::
426-
427-
sage: R.<x,y> = QQ[]
428-
sage: f = x^2 + 5*y
429-
sage: f.change_ring(GF(5))
430-
x^2
431-
432-
::
433-
434-
sage: # needs sage.rings.number_field
435-
sage: K.<w> = CyclotomicField(5)
436-
sage: R.<x,y> = K[]
437-
sage: f = x^2 + w*y
438-
sage: f.change_ring(K.embeddings(QQbar)[1])
439-
x^2 + (-0.8090169943749474? + 0.5877852522924731?*I)*y
440-
"""
441-
if isinstance(R, Morphism):
442-
#if we're given a hom of the base ring extend to a poly hom
443-
if R.domain() == self.base_ring():
444-
R = self.parent().hom(R, self.parent().change_ring(R.codomain()))
445-
return R(self)
446-
else:
447-
return self.parent().change_ring(R)(self)
448-
449415

450416
class MPolynomial_polydict(Polynomial_singular_repr, MPolynomial_element):
451417
r"""

src/sage/rings/polynomial/multi_polynomial_libsingular.pyx

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,15 @@ AUTHORS:
149149
150150
"""
151151

152-
#*****************************************************************************
152+
# ********************************************************************
153+
# Copyright (C) 2005 William Stein <[email protected]>
154+
#
153155
# This program is free software: you can redistribute it and/or modify
154156
# it under the terms of the GNU General Public License as published by
155157
# the Free Software Foundation, either version 2 of the License, or
156158
# (at your option) any later version.
157-
# http://www.gnu.org/licenses/
158-
#*****************************************************************************
159+
# https://www.gnu.org/licenses/
160+
# ********************************************************************
159161

160162
# The Singular API is as follows:
161163
#
@@ -1530,9 +1532,9 @@ cdef class MPolynomialRing_libsingular(MPolynomialRing_base):
15301532

15311533
return old
15321534

1533-
### The following methods are handy for implementing Groebner
1534-
### basis algorithms. They do only superficial type/sanity checks
1535-
### and should be called carefully.
1535+
# The following methods are handy for implementing Groebner
1536+
# basis algorithms. They do only superficial type/sanity checks
1537+
# and should be called carefully.
15361538

15371539
def monomial_quotient(self, MPolynomial_libsingular f, MPolynomial_libsingular g, coeff=False):
15381540
r"""
@@ -1677,7 +1679,6 @@ cdef class MPolynomialRing_libsingular(MPolynomialRing_base):
16771679
else:
16781680
return True
16791681

1680-
16811682
def monomial_lcm(self, MPolynomial_libsingular f, MPolynomial_libsingular g):
16821683
"""
16831684
LCM for monomials. Coefficients are ignored.
@@ -5242,7 +5243,6 @@ cdef class MPolynomial_libsingular(MPolynomial_libsingular_base):
52425243
y += base_map(c)*mul([ im_gens[i]**m[i] for i in range(n) if m[i]])
52435244
return y
52445245

5245-
52465246
def _derivative(self, MPolynomial_libsingular var):
52475247
"""
52485248
Differentiates this polynomial with respect to the provided
@@ -5694,7 +5694,6 @@ cdef class MPolynomial_libsingular(MPolynomial_libsingular_base):
56945694
i.append( new_MP(self._parent, pDiff(self._poly, k)))
56955695
return i
56965696

5697-
56985697
def numerator(self):
56995698
"""
57005699
Return a numerator of self computed as self * self.denominator()

0 commit comments

Comments
 (0)