Problem with randstate, set_seed_gap

29 views
Skip to first unread message

Martin R

unread,
Aug 20, 2025, 6:16:18 AMAug 20
to sage-devel
I would like to use libgap in PermutationGroup_generic.random_element, because of the following:

sage: G = PermutationGroup([[(1,2,3),(4,5)], [(1,2)]])
sage: %timeit G(G._libgap_().Random(), check=False)
9.83 µs ± 24.8 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
sage: %timeit G(G._gap_().Random(), check=False)
6.73 ms ± 97 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

I think that's a factor of about 700.

However, I am running into a problem with randstate.pyx.

The code for random_element is:
```
    def random_element(self):
...
        current_randstate().set_seed_gap()
        return self(self._gap_().Random(), check=False)
```
If I change the last line to use libgap, `set_seed_gap` does not achieve its goal anymore, because it uses the gap interface:

```
        global _gap_seed_randstate
        if _gap_seed_randstate is not self:
            from sage.interfaces.gap import gap

            if self._gap_saved_seed is not None:
                mersenne_seed, classic_seed = self._gap_saved_seed
            else:
                from sage.rings.integer_ring import ZZ
                seed = ZZ.random_element(1<<128)
                classic_seed = seed
                mersenne_seed = seed

            prev_mersenne_seed = gap.Reset(gap.GlobalMersenneTwister, mersenne_seed)
            prev_classic_seed = gap.Reset(gap.GlobalRandomSource, classic_seed)

            if _gap_seed_randstate is not None:
                _gap_seed_randstate._gap_saved_seed = \
                    prev_mersenne_seed, prev_classic_seed

            _gap_seed_randstate = self
```
I'm currently trying to simply duplicate that function, replacing gap with libgap , but I would rather have only one that synchronizes the two.

Michael Orlitzky

unread,
Aug 20, 2025, 9:08:50 AMAug 20
to 'Martin R' via sage-devel
On 2025-08-20 03:16:18, 'Martin R' via sage-devel wrote:
> I'm currently trying to simply duplicate that function, replacing
> gap with libgap , but I would rather have only one that synchronizes
> the two.

Personally I would go with a copy. The existing set_seed_gap()
launches a new GAP process for the pexpect interface if one is not
already running. The start-up is slow, and will be a waste of
resources if you're using libgap exclusively.

Martin R

unread,
Aug 20, 2025, 10:06:02 AMAug 20
to sage-devel
perfect, thank you!  This is done as part of https://github.com/sagemath/sage/pull/40630, maybe you could have a brief look at this specific part?
Reply all
Reply to author
Forward
0 new messages