Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

moving random_element to category of rings #39497

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/sage/categories/rings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from sage.misc.cachefunc import cached_method
from sage.misc.lazy_import import LazyImport
from sage.misc.prandom import randint
from sage.categories.category_with_axiom import CategoryWithAxiom
from sage.categories.rngs import Rngs
from sage.structure.element import Element
Expand Down Expand Up @@ -1646,6 +1647,40 @@ def _random_nonzero_element(self, *args, **kwds):
if not x.is_zero():
return x

def random_element(self, *args):
"""
Return a random integer coerced into this ring.

INPUT:

- either no integer, one integer or two integers

The integer is chosen uniformly from the closed interval
``[-2,2]``, ``[-a,a]`` or ``[a,b]`` according to the
length of the input.

ALGORITHM:

This uses Python's ``randint``.

EXAMPLES::

sage: ZZ.random_element(8) # random
1
sage: QQ.random_element(8) # random
2
sage: ZZ.random_element(4,12) # random
7
"""
if not args:
a, b = -2, 2
elif len(args) == 1:
bound = args[0]
a, b = -bound, bound
else:
a, b = args[0], args[1]
return randint(a, b) * self.one()

class ElementMethods:
def is_unit(self) -> bool:
r"""
Expand Down
4 changes: 2 additions & 2 deletions src/sage/categories/sets_cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2472,11 +2472,11 @@ def random_element(self, *args):
sage: c2 = C.random_element(4,7)
sage: c2 # random
(6, 5, 6, 4, 5, 6, 6, 4, 5, 5)
sage: all(4 <= i < 7 for i in c2)
sage: all(4 <= i <= 7 for i in c2)
True
"""
return self._cartesian_product_of_elements(
c.random_element(*args) for c in self.cartesian_factors())
c.random_element(*args) for c in self.cartesian_factors())

@abstract_method
def _sets_keys(self):
Expand Down
11 changes: 6 additions & 5 deletions src/sage/rings/finite_rings/integer_mod_ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
from sage.arith.misc import factor
from sage.arith.misc import primitive_root
from sage.arith.misc import CRT_basis
from sage.rings.ring import Field, CommutativeRing
from sage.rings.ring import Field
from sage.misc.mrange import cartesian_product_iterator
import sage.rings.abc
from sage.rings.finite_rings import integer_mod
Expand Down Expand Up @@ -1541,14 +1541,15 @@ def random_element(self, bound=None):
sage: while not all(found):
....: found[R.random_element()] = True

We test ``bound``-option::
We test the ``bound`` option::

sage: R.random_element(2) in [R(16), R(17), R(0), R(1), R(2)]
sage: R.random_element(2) in [R(-2), R(-1), R(0), R(1), R(2)]
True
"""
if bound is not None:
return CommutativeRing.random_element(self, bound)
a = random.randint(0, self.order() - 1)
a = random.randint(-bound, bound)
else:
a = random.randint(0, self.order() - 1)
return self(a)

@staticmethod
Expand Down
31 changes: 0 additions & 31 deletions src/sage/rings/ring.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ from sage.misc.superseded import deprecation
from sage.structure.coerce cimport coercion_model
from sage.structure.parent cimport Parent
from sage.structure.category_object cimport check_default_category
from sage.misc.prandom import randint
from sage.categories.rings import Rings
from sage.categories.algebras import Algebras
from sage.categories.commutative_algebras import CommutativeAlgebras
Expand Down Expand Up @@ -637,36 +636,6 @@ cdef class Ring(ParentWithGens):
"""
return self.zeta().multiplicative_order()

def random_element(self, bound=2):
"""
Return a random integer coerced into this ring, where the
integer is chosen uniformly from the interval ``[-bound,bound]``.

INPUT:

- ``bound`` -- integer (default: 2)

ALGORITHM:

Uses Python's randint.

TESTS:

The following example returns a :exc:`NotImplementedError` since the
generic ring class ``__call__`` function returns a
:exc:`NotImplementedError`. Note that
``sage.rings.ring.Ring.random_element`` performs a call in the generic
ring class by a random integer::

sage: R = sage.rings.ring.Ring(ZZ); R
<sage.rings.ring.Ring object at ...>
sage: R.random_element()
Traceback (most recent call last):
...
NotImplementedError: cannot construct elements of <sage.rings.ring.Ring object at ...>
"""
return self(randint(-bound,bound))

@cached_method
def epsilon(self):
"""
Expand Down
Loading