Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
fix coersion of libgap FFelements; switch to libgap
Browse files Browse the repository at this point in the history
With the ticket branch, this works:

sage: F=GF(25)
sage: F(libgap.Z(25)^3)
4*z2 + 3
  • Loading branch information
dimpase committed Nov 22, 2022
1 parent 84f02af commit 21e38a7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
10 changes: 7 additions & 3 deletions src/sage/rings/finite_rings/element_givaro.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ import operator
import sage.arith.all
import sage.rings.finite_rings.finite_field_constructor as finite_field

import sage.interfaces.gap
from sage.libs.pari.all import pari
from cypari2.gen cimport Gen
from cypari2.stack cimport clear_stack
Expand Down Expand Up @@ -431,9 +430,12 @@ cdef class Cache_givaro(Cache_base):
# Reduce to pari
e = e.__pari__()

elif isinstance(e, sage.libs.gap.element.GapElement_FiniteField):
return e.sage(ring=self.parent)

elif sage.interfaces.gap.is_GapElement(e):
from sage.interfaces.gap import gfq_gap_to_sage
return gfq_gap_to_sage(e, self.parent)
from sage.libs.gap.libgap import libgap
return libgap(e).sage(ring=self.parent)

elif isinstance(e, list):
if len(e) > self.exponent():
Expand Down Expand Up @@ -1637,6 +1639,8 @@ cdef class FiniteField_givaroElement(FinitePolyExtElement):
'Z(25)^3'
sage: S(gap('Z(25)^3'))
4*b + 3
sage: S(libgap.Z(25)^3)
4*b + 3
"""
cdef Cache_givaro cache = self._cache
if self == 0:
Expand Down
18 changes: 14 additions & 4 deletions src/sage/rings/finite_rings/element_ntl_gf2e.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ from sage.libs.pari.all import pari
from cypari2.gen cimport Gen
from cypari2.stack cimport clear_stack

from sage.interfaces.gap import is_GapElement

from sage.misc.randstate import current_randstate
from sage.arith.long cimport pyobject_to_long

from .element_pari_ffelt import FiniteFieldElement_pari_ffelt
from .finite_field_ntl_gf2e import FiniteField_ntl_gf2e

from sage.libs.gap.element import GapElement_FiniteField

from sage.interfaces.gap import is_GapElement

from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing

cdef object is_IntegerMod
Expand Down Expand Up @@ -364,9 +366,13 @@ cdef class Cache_ntl_gf2e(Cache_base):
# Reduce to pari
e = e.__pari__()

elif isinstance(e, GapElement_FiniteField):
return e.sage(ring=self._parent)

elif is_GapElement(e):
from sage.interfaces.gap import gfq_gap_to_sage
return gfq_gap_to_sage(e, self._parent)
from sage.libs.gap.libgap import libgap
return libgap(e).sage(ring=self._parent)

else:
raise TypeError("unable to coerce %r" % type(e))

Expand Down Expand Up @@ -1174,6 +1180,10 @@ cdef class FiniteField_ntl_gf2eElement(FinitePolyExtElement):
sage: k.<b> = GF(2^16)
sage: b._gap_init_()
'Z(65536)^1'
sage: k(gap('Z(2^16)^3+Z(2^16)^5'))
b^5 + b^3
sage: k(libgap.Z(2^16)^3+libgap.Z(2^16)^5)
b^5 + b^3
"""
F = self._parent
if not F.is_conway():
Expand Down
20 changes: 15 additions & 5 deletions src/sage/rings/finite_rings/element_pari_ffelt.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -505,9 +505,15 @@ cdef class FiniteFieldElement_pari_ffelt(FinitePolyExtElement):
self.construct_from(self._parent.polynomial_ring()(x))

elif is_GapElement(x):
from sage.interfaces.gap import gfq_gap_to_sage
try:
self.construct_from(gfq_gap_to_sage(x, self._parent))
from sage.libs.gap.libgap import libgap
self.construct_from(libgap(x).sage(ring=self._parent))
except (ValueError, IndexError, TypeError):
raise TypeError("no coercion defined")

elif isinstance(x, sage.libs.gap.element.GapElement_FiniteField):
try:
self.construct_from(x.sage(ring=self._parent))
except (ValueError, IndexError, TypeError):
raise TypeError("no coercion defined")

Expand Down Expand Up @@ -1258,16 +1264,20 @@ cdef class FiniteFieldElement_pari_ffelt(FinitePolyExtElement):
EXAMPLES::
sage: F = FiniteField(2^3, 'a', impl='pari_ffelt')
sage: a = F.multiplicative_generator()
sage: gap(a) # indirect doctest
sage: F = FiniteField(2^3, 'aa', impl='pari_ffelt')
sage: aa = F.multiplicative_generator()
sage: gap(aa) # indirect doctest
Z(2^3)
sage: b = F.multiplicative_generator()
sage: a = b^3
sage: gap(a)
Z(2^3)^3
sage: gap(a^3)
Z(2^3)^2
sage: F(gap('Z(8)^3'))
aa + 1
sage: F(libgap.Z(8)^3)
aa + 1
You can specify the instance of the Gap interpreter that is used::
Expand Down
6 changes: 2 additions & 4 deletions src/sage/rings/finite_rings/integer_mod_ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@

from sage.libs.pari.all import pari, PariError

import sage.interfaces.all
from sage.misc.cachefunc import cached_method

from sage.structure.factory import UniqueFactory
Expand Down Expand Up @@ -1187,9 +1186,8 @@ def _element_constructor_(self, x):
raise TypeError("error coercing to finite field")
except TypeError:
if sage.interfaces.gap.is_GapElement(x):
from sage.interfaces.gap import intmod_gap_to_sage
y = intmod_gap_to_sage(x)
return integer_mod.IntegerMod(self, y)
from sage.libs.gap.libgap import libgap
return libgap(x).sage()
raise # Continue up with the original TypeError

def __iter__(self):
Expand Down

0 comments on commit 21e38a7

Please sign in to comment.