Skip to content

Commit

Permalink
gh-37367: improve random sampling of quotient-ring elements
Browse files Browse the repository at this point in the history
    
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
  • Loading branch information
Release Manager committed Feb 21, 2024
2 parents 702e32f + 5c1309c commit 193e49d
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/sage/rings/quotient_ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.<x,y> = 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"""
Expand Down

0 comments on commit 193e49d

Please sign in to comment.