From 5c1309c95277944104e3f9f24048a4bbc952cb62 Mon Sep 17 00:00:00 2001 From: Lorenz Panny Date: Fri, 16 Feb 2024 15:15:34 +0100 Subject: [PATCH] improve random sampling of quotient-ring elements --- src/sage/rings/quotient_ring.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/sage/rings/quotient_ring.py b/src/sage/rings/quotient_ring.py index a6a0a8ce7f8..838fe890e62 100644 --- a/src/sage/rings/quotient_ring.py +++ b/src/sage/rings/quotient_ring.py @@ -1354,6 +1354,30 @@ def term_order(self): """ return self.__R.term_order() + def random_element(self): + r""" + Return a random element of this quotient ring obtained by + sampling a random element of the cover ring and reducing + it modulo the defining ideal. + + EXAMPLES:: + + sage: R. = QQ[] + sage: S = R.quotient([x^3, y^2]) + sage: S.random_element() # random + -8/5*xbar^2 + 3/2*xbar*ybar + 2*xbar - 4/23 + + TESTS: + + Make sure we are not just getting images of integers in this + ring (which would be the case if the default implementation + of this method was inherited from generic rings):: + + sage: any(S.random_element() not in ZZ for _ in range(999)) + True + """ + return self.retract(self.cover_ring().random_element()) + class QuotientRing_generic(QuotientRing_nc, ring.CommutativeRing): r"""