Skip to content

Commit 193e49d

Browse files
author
Release Manager
committed
gh-37367: improve random sampling of quotient-ring elements Currently, random sampling in generic quotient rings is restricted to a very small (and special) subset of the elements: ```sage sage: R.<x,y> = QQ[] sage: S = R.quotient([x^3, y^2]) sage: {S.random_element() for _ in range(999)} {-2, -1, 0, 1, 2} ``` In this patch we add an implementation of `.random_element()` which simply calls the `.random_element()` method of the cover ring and maps the result to the quotient. This is still far from perfect for many kinds of quotient rings, but it's definitely an improvement compared to the current behavior. URL: #37367 Reported by: Lorenz Panny Reviewer(s): Giacomo Pope
2 parents 702e32f + 5c1309c commit 193e49d

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/sage/rings/quotient_ring.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,6 +1354,30 @@ def term_order(self):
13541354
"""
13551355
return self.__R.term_order()
13561356

1357+
def random_element(self):
1358+
r"""
1359+
Return a random element of this quotient ring obtained by
1360+
sampling a random element of the cover ring and reducing
1361+
it modulo the defining ideal.
1362+
1363+
EXAMPLES::
1364+
1365+
sage: R.<x,y> = QQ[]
1366+
sage: S = R.quotient([x^3, y^2])
1367+
sage: S.random_element() # random
1368+
-8/5*xbar^2 + 3/2*xbar*ybar + 2*xbar - 4/23
1369+
1370+
TESTS:
1371+
1372+
Make sure we are not just getting images of integers in this
1373+
ring (which would be the case if the default implementation
1374+
of this method was inherited from generic rings)::
1375+
1376+
sage: any(S.random_element() not in ZZ for _ in range(999))
1377+
True
1378+
"""
1379+
return self.retract(self.cover_ring().random_element())
1380+
13571381

13581382
class QuotientRing_generic(QuotientRing_nc, ring.CommutativeRing):
13591383
r"""

0 commit comments

Comments
 (0)