Skip to content

Remove confusion in gap interface conversion method #39909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/sage/algebras/quantum_groups/quantum_group_gap.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def gap(self):
"""
return self._libgap

_libgap_ = _gap_ = gap
_libgap_ = gap

def _add_(self, other):
r"""
Expand Down Expand Up @@ -432,7 +432,7 @@ def gap(self):
"""
return self._libgap

_libgap_ = _gap_ = gap
_libgap_ = gap

def cartan_type(self):
"""
Expand Down Expand Up @@ -1602,7 +1602,7 @@ def gap(self):
"""
return self._libgap

_libgap_ = _gap_ = gap
_libgap_ = gap

def _element_constructor_(self, elt):
"""
Expand Down
18 changes: 15 additions & 3 deletions src/sage/combinat/designs/incidence_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@
from sage.misc.latex import latex
from sage.misc.lazy_import import lazy_import
from sage.rings.integer import Integer
from sage.structure.sage_object import SageObject
from sage.sets.set import Set

lazy_import('sage.libs.gap.libgap', 'libgap')


class IncidenceStructure:
class IncidenceStructure(SageObject):
r"""
A base class for incidence structures (i.e. hypergraphs, i.e. set systems)

Expand Down Expand Up @@ -1049,14 +1050,25 @@
B = self._blocks
return all(B[i] != B[i + 1] for i in range(len(B) - 1))

def _gap_(self):
def _gap_init_(self):
"""
Return the GAP string describing the design.

EXAMPLES::

sage: # optional - gap_package_design
sage: BD = IncidenceStructure(7,[[0,1,2],[0,3,4],[0,5,6],[1,3,5],[1,4,6],[2,3,6],[2,4,5]])
sage: BD._gap_()
sage: gap.load_package("design")
sage: gap(BD)
rec(
blocks := [ [ 1, 2, 3 ], [ 1, 4, 5 ], [ 1, 6, 7 ], [ 2, 4, 6 ],
[ 2, 5, 7 ], [ 3, 4, 7 ], [ 3, 5, 6 ] ],
isBlockDesign := true,
v := 7 )

TESTS::

sage: BD._gap_init_()

Check failure on line 1071 in src/sage/combinat/designs/incidence_structures.py

View workflow job for this annotation

GitHub Actions / Conda (ubuntu, Python 3.12, new)

Failed example:

Failed example:: Exception raised: Traceback (most recent call last): File "/home/runner/miniconda3/envs/sage-dev/lib/python3.12/site-packages/sage/doctest/forker.py", line 730, in _run self.compile_and_execute(example, compiler, test.globs) File "/home/runner/miniconda3/envs/sage-dev/lib/python3.12/site-packages/sage/doctest/forker.py", line 1154, in compile_and_execute exec(compiled, globs) File "<doctest sage.combinat.designs.incidence_structures.IncidenceStructure._gap_init_[0]>", line 1, in <module> BD._gap_init_() ^^ NameError: name 'BD' is not defined

Check failure on line 1071 in src/sage/combinat/designs/incidence_structures.py

View workflow job for this annotation

GitHub Actions / Conda (ubuntu, Python 3.12, all)

Failed example:

Failed example:: Exception raised: Traceback (most recent call last): File "/home/runner/miniconda3/envs/sage-dev/lib/python3.12/site-packages/sage/doctest/forker.py", line 730, in _run self.compile_and_execute(example, compiler, test.globs) File "/home/runner/miniconda3/envs/sage-dev/lib/python3.12/site-packages/sage/doctest/forker.py", line 1154, in compile_and_execute exec(compiled, globs) File "<doctest sage.combinat.designs.incidence_structures.IncidenceStructure._gap_init_[0]>", line 1, in <module> BD._gap_init_() ^^ NameError: name 'BD' is not defined

Check failure on line 1071 in src/sage/combinat/designs/incidence_structures.py

View workflow job for this annotation

GitHub Actions / Conda (ubuntu, Python 3.12, all, editable)

Failed example:

Failed example:: Exception raised: Traceback (most recent call last): File "/home/runner/work/sage/sage/src/sage/doctest/forker.py", line 730, in _run self.compile_and_execute(example, compiler, test.globs) File "/home/runner/work/sage/sage/src/sage/doctest/forker.py", line 1154, in compile_and_execute exec(compiled, globs) File "<doctest sage.combinat.designs.incidence_structures.IncidenceStructure._gap_init_[0]>", line 1, in <module> BD._gap_init_() ^^ NameError: name 'BD' is not defined

Check failure on line 1071 in src/sage/combinat/designs/incidence_structures.py

View workflow job for this annotation

GitHub Actions / Conda (ubuntu, Python 3.11, all)

Failed example:

Failed example:: Exception raised: Traceback (most recent call last): File "/home/runner/miniconda3/envs/sage-dev/lib/python3.11/site-packages/sage/doctest/forker.py", line 730, in _run self.compile_and_execute(example, compiler, test.globs) File "/home/runner/miniconda3/envs/sage-dev/lib/python3.11/site-packages/sage/doctest/forker.py", line 1154, in compile_and_execute exec(compiled, globs) File "<doctest sage.combinat.designs.incidence_structures.IncidenceStructure._gap_init_[0]>", line 1, in <module> BD._gap_init_() ^^ NameError: name 'BD' is not defined

Check failure on line 1071 in src/sage/combinat/designs/incidence_structures.py

View workflow job for this annotation

GitHub Actions / Conda (macos, Python 3.11, all)

Failed example:

Failed example:: Exception raised: Traceback (most recent call last): File "/Users/runner/miniconda3/envs/sage-dev/lib/python3.11/site-packages/sage/doctest/forker.py", line 730, in _run self.compile_and_execute(example, compiler, test.globs) File "/Users/runner/miniconda3/envs/sage-dev/lib/python3.11/site-packages/sage/doctest/forker.py", line 1154, in compile_and_execute exec(compiled, globs) File "<doctest sage.combinat.designs.incidence_structures.IncidenceStructure._gap_init_[0]>", line 1, in <module> BD._gap_init_() ^^ NameError: name 'BD' is not defined

Check failure on line 1071 in src/sage/combinat/designs/incidence_structures.py

View workflow job for this annotation

GitHub Actions / Conda (macos, Python 3.12, all)

Failed example:

Failed example:: Exception raised: Traceback (most recent call last): File "/Users/runner/miniconda3/envs/sage-dev/lib/python3.12/site-packages/sage/doctest/forker.py", line 730, in _run self.compile_and_execute(example, compiler, test.globs) File "/Users/runner/miniconda3/envs/sage-dev/lib/python3.12/site-packages/sage/doctest/forker.py", line 1154, in compile_and_execute exec(compiled, globs) File "<doctest sage.combinat.designs.incidence_structures.IncidenceStructure._gap_init_[0]>", line 1, in <module> BD._gap_init_() ^^ NameError: name 'BD' is not defined

Check failure on line 1071 in src/sage/combinat/designs/incidence_structures.py

View workflow job for this annotation

GitHub Actions / test-long (src/sage/[a-f]*)

Failed example:

Failed example:: Exception raised: Traceback (most recent call last): File "/sage/src/sage/doctest/forker.py", line 730, in _run self.compile_and_execute(example, compiler, test.globs) File "/sage/src/sage/doctest/forker.py", line 1154, in compile_and_execute exec(compiled, globs) File "<doctest sage.combinat.designs.incidence_structures.IncidenceStructure._gap_init_[0]>", line 1, in <module> BD._gap_init_() ^^ NameError: name 'BD' is not defined
'BlockDesign(7,[[1, 2, 3], [1, 4, 5], [1, 6, 7], [2, 4, 6], [2, 5, 7], [3, 4, 7], [3, 5, 6]])'
"""
v = self.num_points()
Expand Down
4 changes: 2 additions & 2 deletions src/sage/groups/class_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
"""
return str(self._gap_classfunction)

def _gap_(self, *args):
def _gap_(self, gap):
r"""
Coerce ``self`` into a GAP element.

Expand All @@ -156,9 +156,9 @@
Character of Cyclic group of order 4 as a permutation group
sage: type(_)
<class 'sage.groups.class_function.ClassFunction_gap'>
sage: chi._gap_()

Check failure on line 159 in src/sage/groups/class_function.py

View workflow job for this annotation

GitHub Actions / Conda (ubuntu, Python 3.12, new)

Failed example:

Failed example:: Exception raised: Traceback (most recent call last): File "/home/runner/miniconda3/envs/sage-dev/lib/python3.12/site-packages/sage/doctest/forker.py", line 730, in _run self.compile_and_execute(example, compiler, test.globs) File "/home/runner/miniconda3/envs/sage-dev/lib/python3.12/site-packages/sage/doctest/forker.py", line 1154, in compile_and_execute exec(compiled, globs) File "<doctest sage.groups.class_function.ClassFunction_gap._gap_[4]>", line 1, in <module> chi._gap_() TypeError: ClassFunction_gap._gap_() missing 1 required positional argument: 'gap'

Check failure on line 159 in src/sage/groups/class_function.py

View workflow job for this annotation

GitHub Actions / Conda (ubuntu, Python 3.12, all)

Failed example:

Failed example:: Exception raised: Traceback (most recent call last): File "/home/runner/miniconda3/envs/sage-dev/lib/python3.12/site-packages/sage/doctest/forker.py", line 730, in _run self.compile_and_execute(example, compiler, test.globs) File "/home/runner/miniconda3/envs/sage-dev/lib/python3.12/site-packages/sage/doctest/forker.py", line 1154, in compile_and_execute exec(compiled, globs) File "<doctest sage.groups.class_function.ClassFunction_gap._gap_[4]>", line 1, in <module> chi._gap_() TypeError: ClassFunction_gap._gap_() missing 1 required positional argument: 'gap'

Check failure on line 159 in src/sage/groups/class_function.py

View workflow job for this annotation

GitHub Actions / Conda (ubuntu, Python 3.12, all, editable)

Failed example:

Failed example:: Exception raised: Traceback (most recent call last): File "/home/runner/work/sage/sage/src/sage/doctest/forker.py", line 730, in _run self.compile_and_execute(example, compiler, test.globs) File "/home/runner/work/sage/sage/src/sage/doctest/forker.py", line 1154, in compile_and_execute exec(compiled, globs) File "<doctest sage.groups.class_function.ClassFunction_gap._gap_[4]>", line 1, in <module> chi._gap_() TypeError: ClassFunction_gap._gap_() missing 1 required positional argument: 'gap'

Check failure on line 159 in src/sage/groups/class_function.py

View workflow job for this annotation

GitHub Actions / Conda (ubuntu, Python 3.11, all)

Failed example:

Failed example:: Exception raised: Traceback (most recent call last): File "/home/runner/miniconda3/envs/sage-dev/lib/python3.11/site-packages/sage/doctest/forker.py", line 730, in _run self.compile_and_execute(example, compiler, test.globs) File "/home/runner/miniconda3/envs/sage-dev/lib/python3.11/site-packages/sage/doctest/forker.py", line 1154, in compile_and_execute exec(compiled, globs) File "<doctest sage.groups.class_function.ClassFunction_gap._gap_[4]>", line 1, in <module> chi._gap_() TypeError: ClassFunction_gap._gap_() missing 1 required positional argument: 'gap'

Check failure on line 159 in src/sage/groups/class_function.py

View workflow job for this annotation

GitHub Actions / Conda (macos, Python 3.11, all)

Failed example:

Failed example:: Exception raised: Traceback (most recent call last): File "/Users/runner/miniconda3/envs/sage-dev/lib/python3.11/site-packages/sage/doctest/forker.py", line 730, in _run self.compile_and_execute(example, compiler, test.globs) File "/Users/runner/miniconda3/envs/sage-dev/lib/python3.11/site-packages/sage/doctest/forker.py", line 1154, in compile_and_execute exec(compiled, globs) File "<doctest sage.groups.class_function.ClassFunction_gap._gap_[4]>", line 1, in <module> chi._gap_() TypeError: ClassFunction_gap._gap_() missing 1 required positional argument: 'gap'

Check failure on line 159 in src/sage/groups/class_function.py

View workflow job for this annotation

GitHub Actions / Conda (macos, Python 3.12, all)

Failed example:

Failed example:: Exception raised: Traceback (most recent call last): File "/Users/runner/miniconda3/envs/sage-dev/lib/python3.12/site-packages/sage/doctest/forker.py", line 730, in _run self.compile_and_execute(example, compiler, test.globs) File "/Users/runner/miniconda3/envs/sage-dev/lib/python3.12/site-packages/sage/doctest/forker.py", line 1154, in compile_and_execute exec(compiled, globs) File "<doctest sage.groups.class_function.ClassFunction_gap._gap_[4]>", line 1, in <module> chi._gap_() TypeError: ClassFunction_gap._gap_() missing 1 required positional argument: 'gap'

Check failure on line 159 in src/sage/groups/class_function.py

View workflow job for this annotation

GitHub Actions / test-long (src/sage/[g-o]*)

Failed example:

Failed example:: Exception raised: Traceback (most recent call last): File "/sage/src/sage/doctest/forker.py", line 730, in _run self.compile_and_execute(example, compiler, test.globs) File "/sage/src/sage/doctest/forker.py", line 1154, in compile_and_execute exec(compiled, globs) File "<doctest sage.groups.class_function.ClassFunction_gap._gap_[4]>", line 1, in <module> chi._gap_() TypeError: ClassFunction_gap._gap_() missing 1 required positional argument: 'gap'
ClassFunction( CharacterTable( Group( [ (1,2,3,4) ] ) ), [ 1, -1, 1, -1 ] )
sage: type(_)

Check failure on line 161 in src/sage/groups/class_function.py

View workflow job for this annotation

GitHub Actions / Conda (ubuntu, Python 3.12, new)

Failed example:

Failed example:: Got: <class 'type'>

Check failure on line 161 in src/sage/groups/class_function.py

View workflow job for this annotation

GitHub Actions / Conda (ubuntu, Python 3.12, all)

Failed example:

Failed example:: Got: <class 'type'>

Check failure on line 161 in src/sage/groups/class_function.py

View workflow job for this annotation

GitHub Actions / Conda (ubuntu, Python 3.12, all, editable)

Failed example:

Failed example:: Got: <class 'type'>

Check failure on line 161 in src/sage/groups/class_function.py

View workflow job for this annotation

GitHub Actions / Conda (ubuntu, Python 3.11, all)

Failed example:

Failed example:: Got: <class 'type'>

Check failure on line 161 in src/sage/groups/class_function.py

View workflow job for this annotation

GitHub Actions / Conda (macos, Python 3.11, all)

Failed example:

Failed example:: Got: <class 'type'>

Check failure on line 161 in src/sage/groups/class_function.py

View workflow job for this annotation

GitHub Actions / Conda (macos, Python 3.12, all)

Failed example:

Failed example:: Got: <class 'type'>

Check failure on line 161 in src/sage/groups/class_function.py

View workflow job for this annotation

GitHub Actions / test-long (src/sage/[g-o]*)

Failed example:

Failed example:: Got: <class 'type'>
<class 'sage.interfaces.gap.GapElement'>
"""
return self._gap_classfunction
Expand Down Expand Up @@ -832,7 +832,7 @@
"""
return self._gap_classfunction

gap = _gap_ = _libgap_
gap = _libgap_

def _repr_(self):
r"""
Expand Down
4 changes: 2 additions & 2 deletions src/sage/groups/libgap_wrapper.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ class ParentLibGAP(SageObject):
"""
return self._libgap

_libgap_ = _gap_ = gap
_libgap_ = gap

def ngens(self):
"""
Expand Down Expand Up @@ -526,7 +526,7 @@ cdef class ElementLibGAP(MultiplicativeGroupElement):
"""
return self._libgap

_libgap_ = _gap_ = gap
_libgap_ = gap

def _test_libgap_conversion(self, **options):
r"""
Expand Down
9 changes: 3 additions & 6 deletions src/sage/interfaces/gap.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@

The GAP interface will only work if GAP is installed on your
computer; this should be the case, since GAP is included with Sage.
The interface offers three pieces of functionality:
The interface offers two pieces of functionality:


#. ``gap_console()`` -- a function that dumps you into
an interactive command-line GAP session.

#. ``gap(expr)`` -- evaluation of arbitrary GAP
expressions, with the result returned as a string.

#. ``gap.new(expr)`` -- creation of a Sage object that
#. ``gap(expr)`` -- creation of a Sage object that
wraps a GAP object. This provides a Pythonic interface to GAP. For
example, if ``f=gap.new(10)``, then
example, if ``f=gap(10)``, then
``f.Factors()`` returns the prime factorization of
`10` computed using GAP.

Expand Down
1 change: 0 additions & 1 deletion src/sage/libs/gap/util.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,4 @@ cdef initialize()
### Evaluate string in GAP #################################################
############################################################################

# Evaluate a string
cdef Obj gap_eval(str gap_string) except? NULL
4 changes: 4 additions & 0 deletions src/sage/libs/gap/util.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,10 @@ cdef Obj gap_eval(str gap_string) except? NULL:
r"""
Evaluate a string in GAP.

This function cannot be used directly from Python, use
:meth:`~sage.libs.gap.libgap.Gap.eval` method on global ``libgap``
variable instead.

INPUT:

- ``gap_string`` -- string; a valid statement in GAP
Expand Down
67 changes: 67 additions & 0 deletions src/sage/structure/sage_object.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -741,17 +741,84 @@ cdef class SageObject:
return True

def _gap_(self, G=None):
"""
Return a Gap object.

Unlike :meth:`_libgap_`, this method returns an instance of
:class:`sage.interfaces.gap.GapElement`, which wraps an object
in the GAP interpreter spawned as a subprocess of Sage.

Typically you should not need to call this method directly,
instead just call :mod:`~sage.interfaces.gap`
on the object. See example below.

EXAMPLES::

sage: a = gap(2/3); a
2/3
sage: type(a)
<class 'sage.interfaces.gap.GapElement'>

sage: a = (2/3)._gap_(); a
2/3
sage: type(a)
<class 'sage.interfaces.gap.GapElement'>
"""
if G is None:
import sage.interfaces.gap
G = sage.interfaces.gap.gap
return self._interface_(G)

def _gap_init_(self):
"""
Return a string that provides a representation of ``self`` in GAP.

This method is indirectly used by :meth:`_libgap_` and :meth:`_gap_`
by essentially passing their output to
:meth:`libgap.eval <sage.libs.gap.libgap.Gap.eval>`
and :mod:`~sage.interfaces.gap` respectively,
unless the subclass overrides them with more efficient variants.

EXAMPLES::

sage: (2/3)._gap_init_()
'2/3'
sage: Zmod(4)._gap_init_()
'ZmodnZ(4)'
"""
import sage.interfaces.gap
I = sage.interfaces.gap.gap
return self._interface_init_(I)

def _libgap_(self):
"""
Return a libgap object.

Unlike :meth:`_gap_`, this method returns an instance of
:class:`sage.libs.gap.libgap.GapElement`, which wraps an object
in libgap embedded in Sage. As explained in
:mod:`sage.libs.gap.libgap`, this is much faster.

Typically you should not need to call this method directly,
instead use :mod:`~sage.libs.gap.libgap`. See example below.

By default, this method makes use of :meth:`_gap_init_`.
Subclasses could override this method to provide a more efficient
implementation.

EXAMPLES::

sage: a = libgap(2/3); a
2/3
sage: type(a)
<class 'sage.libs.gap.element.GapElement_Rational'>

TESTS::

sage: from sage.libs.gap.element import GapElement
sage: isinstance(a, GapElement)
True
"""
from sage.libs.gap.libgap import libgap
return libgap.eval(self)

Expand Down
Loading