Skip to content

Commit 3500f61

Browse files
author
Release Manager
committed
gh-39497: moving random_element to category of rings moving the random_element out of the auld `Ring` class ### 📝 Checklist - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. URL: #39497 Reported by: Frédéric Chapoton Reviewer(s): Travis Scrimshaw
2 parents e368fa7 + 06530ce commit 3500f61

File tree

5 files changed

+43
-39
lines changed

5 files changed

+43
-39
lines changed

src/doc/en/thematic_tutorials/coercion_and_categories.rst

-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ This base class provides a lot more methods than a general parent::
132132
'ngens',
133133
'one',
134134
'order',
135-
'random_element',
136135
'zero',
137136
'zeta',
138137
'zeta_order']

src/sage/categories/rings.py

+35
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
from sage.misc.cachefunc import cached_method
1919
from sage.misc.lazy_import import LazyImport
20+
from sage.misc.prandom import randint
2021
from sage.categories.category_with_axiom import CategoryWithAxiom
2122
from sage.categories.rngs import Rngs
2223
from sage.structure.element import Element
@@ -1529,6 +1530,40 @@ def _random_nonzero_element(self, *args, **kwds):
15291530
if not x.is_zero():
15301531
return x
15311532

1533+
def random_element(self, *args):
1534+
"""
1535+
Return a random integer coerced into this ring.
1536+
1537+
INPUT:
1538+
1539+
- either no integer, one integer or two integers
1540+
1541+
The integer is chosen uniformly from the closed interval
1542+
``[-2,2]``, ``[-a,a]`` or ``[a,b]`` according to the
1543+
length of the input.
1544+
1545+
ALGORITHM:
1546+
1547+
This uses Python's ``randint``.
1548+
1549+
EXAMPLES::
1550+
1551+
sage: -8 <= ZZ.random_element(8) <= 8
1552+
True
1553+
sage: -8 <= QQ.random_element(8) <= 8
1554+
True
1555+
sage: 4 <= ZZ.random_element(4,12) <= 12
1556+
True
1557+
"""
1558+
if not args:
1559+
a, b = -2, 2
1560+
elif len(args) == 1:
1561+
bound = args[0]
1562+
a, b = -bound, bound
1563+
else:
1564+
a, b = args[0], args[1]
1565+
return randint(a, b) * self.one()
1566+
15321567
class ElementMethods:
15331568
def is_unit(self) -> bool:
15341569
r"""

src/sage/categories/sets_cat.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2472,11 +2472,11 @@ def random_element(self, *args):
24722472
sage: c2 = C.random_element(4,7)
24732473
sage: c2 # random
24742474
(6, 5, 6, 4, 5, 6, 6, 4, 5, 5)
2475-
sage: all(4 <= i < 7 for i in c2)
2475+
sage: all(4 <= i <= 7 for i in c2)
24762476
True
24772477
"""
24782478
return self._cartesian_product_of_elements(
2479-
c.random_element(*args) for c in self.cartesian_factors())
2479+
c.random_element(*args) for c in self.cartesian_factors())
24802480

24812481
@abstract_method
24822482
def _sets_keys(self):

src/sage/rings/finite_rings/integer_mod_ring.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
from sage.arith.misc import factor
6868
from sage.arith.misc import primitive_root
6969
from sage.arith.misc import CRT_basis
70-
from sage.rings.ring import Field, CommutativeRing
70+
from sage.rings.ring import Field
7171
from sage.misc.mrange import cartesian_product_iterator
7272
import sage.rings.abc
7373
from sage.rings.finite_rings import integer_mod
@@ -1541,14 +1541,15 @@ def random_element(self, bound=None):
15411541
sage: while not all(found):
15421542
....: found[R.random_element()] = True
15431543
1544-
We test ``bound``-option::
1544+
We test the ``bound`` option::
15451545
1546-
sage: R.random_element(2) in [R(16), R(17), R(0), R(1), R(2)]
1546+
sage: R.random_element(2) in [R(-2), R(-1), R(0), R(1), R(2)]
15471547
True
15481548
"""
15491549
if bound is not None:
1550-
return CommutativeRing.random_element(self, bound)
1551-
a = random.randint(0, self.order() - 1)
1550+
a = random.randint(-bound, bound)
1551+
else:
1552+
a = random.randint(0, self.order() - 1)
15521553
return self(a)
15531554

15541555
@staticmethod

src/sage/rings/ring.pyx

-31
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ from sage.misc.superseded import deprecation
114114
from sage.structure.coerce cimport coercion_model
115115
from sage.structure.parent cimport Parent
116116
from sage.structure.category_object cimport check_default_category
117-
from sage.misc.prandom import randint
118117
from sage.categories.rings import Rings
119118
from sage.categories.algebras import Algebras
120119
from sage.categories.commutative_algebras import CommutativeAlgebras
@@ -637,36 +636,6 @@ cdef class Ring(ParentWithGens):
637636
"""
638637
return self.zeta().multiplicative_order()
639638

640-
def random_element(self, bound=2):
641-
"""
642-
Return a random integer coerced into this ring, where the
643-
integer is chosen uniformly from the interval ``[-bound,bound]``.
644-
645-
INPUT:
646-
647-
- ``bound`` -- integer (default: 2)
648-
649-
ALGORITHM:
650-
651-
Uses Python's randint.
652-
653-
TESTS:
654-
655-
The following example returns a :exc:`NotImplementedError` since the
656-
generic ring class ``__call__`` function returns a
657-
:exc:`NotImplementedError`. Note that
658-
``sage.rings.ring.Ring.random_element`` performs a call in the generic
659-
ring class by a random integer::
660-
661-
sage: R = sage.rings.ring.Ring(ZZ); R
662-
<sage.rings.ring.Ring object at ...>
663-
sage: R.random_element()
664-
Traceback (most recent call last):
665-
...
666-
NotImplementedError: cannot construct elements of <sage.rings.ring.Ring object at ...>
667-
"""
668-
return self(randint(-bound,bound))
669-
670639
@cached_method
671640
def epsilon(self):
672641
"""

0 commit comments

Comments
 (0)