libGAP shared library Interface to GAP
This module implements a fast C library interface to GAP. To use libGAP you simply call libgap (the parent of all GapElement instances) and use it to convert Sage objects into GAP objects.
EXAMPLES:
sage: a = libgap(10)
sage: a
10
sage: type(a)
<type 'sage.libs.gap.element.GapElement_Integer'>
sage: a*a
100
sage: timeit('a*a') # random output
625 loops, best of 3: 898 ns per loop
Compared to the expect interface this is >1000 times faster:
sage: b = gap('10')
sage: timeit('b*b') # random output; long time
125 loops, best of 3: 2.05 ms per loop
If you want to evaluate GAP commands, use the Gap.eval() method:
sage: libgap.eval('List([1..10], i->i^2)')
[ 1, 4, 9, 16, 25, 36, 49, 64, 81, 100 ]
not to be confused with the libgap call, which converts Sage objects to GAP objects, for example strings to strings:
sage: libgap('List([1..10], i->i^2)')
"List([1..10], i->i^2)"
sage: type(_)
<type 'sage.libs.gap.element.GapElement_String'>
You can usually use the sage() method to convert the resulting GAP element back to its Sage equivalent:
sage: a.sage()
10
sage: type(_)
<type 'sage.rings.integer.Integer'>
sage: libgap.eval('5/3 + 7*E(3)').sage()
7*zeta3 + 5/3
sage: generators = libgap.AlternatingGroup(4).GeneratorsOfGroup().sage()
sage: generators # a Sage list of Sage permutations!
[(1,2,3), (2,3,4)]
sage: PermutationGroup(generators).cardinality() # computed in Sage
12
sage: libgap.AlternatingGroup(4).Size() # computed in GAP
12
So far, the following GAP data types can be directly converted to the corresponding Sage datatype:
Special support is available for the GAP container classes. GAP lists can be used as follows:
sage: lst = libgap([1,5,7]); lst
[ 1, 5, 7 ]
sage: type(lst)
<type 'sage.libs.gap.element.GapElement_List'>
sage: len(lst)
3
sage: lst[0]
1
sage: [ x^2 for x in lst ]
[1, 25, 49]
sage: type(_[0])
<type 'sage.libs.gap.element.GapElement_Integer'>
Note that you can access the elements of GAP List objects as you would expect from Python (with indexing starting at 0), but the elements are still of type GapElement. The other GAP container type are records, which are similar to Python dictionaries. You can construct them directly from Python dictionaries:
sage: libgap({'a':123, 'b':456})
rec( a := 123, b := 456 )
Or get them as results of computations:
sage: rec = libgap.eval('rec(a:=123, b:=456, Sym3:=SymmetricGroup(3))')
sage: rec['Sym3']
Sym( [ 1 .. 3 ] )
sage: dict(rec)
{'a': 123, 'Sym3': Sym( [ 1 .. 3 ] ), 'b': 456}
The output is a Sage dictionary whose keys are Sage strings and whose Values are instances of GapElement(). So, for example, rec['a'] is not a Sage integer. To recursively convert the entries into Sage objects, you should use the sage() method:
sage: rec.sage()
{'a': 123,
'Sym3': NotImplementedError('cannot construct equivalent Sage object',),
'b': 456}
Now rec['a'] is a Sage integer. We have not implemented the conversion of the GAP symmetric group to the Sage symmetric group yet, so you end up with a NotImplementedError exception object. The exception is returned and not raised so that you can work with the partial result.
While we don’t directly support matrices yet, you can convert them to Gap List of Lists. These lists are then easily converted into Sage using the recursive expansion of the sage() method:
sage: M = libgap.eval('BlockMatrix([[1,1,[[1, 2],[ 3, 4]]], [1,2,[[9,10],[11,12]]], [2,2,[[5, 6],[ 7, 8]]]],2,2)')
sage: M
<block matrix of dimensions (2*2)x(2*2)>
sage: M.List() # returns a GAP List of Lists
[ [ 1, 2, 9, 10 ], [ 3, 4, 11, 12 ], [ 0, 0, 5, 6 ], [ 0, 0, 7, 8 ] ]
sage: M.List().sage() # returns a Sage list of lists
[[1, 2, 9, 10], [3, 4, 11, 12], [0, 0, 5, 6], [0, 0, 7, 8]]
sage: matrix(ZZ, _)
[ 1 2 9 10]
[ 3 4 11 12]
[ 0 0 5 6]
[ 0 0 7 8]
The lower-case libgap_foobar functions are ones that we added to make the libGAP C shared library. The libGAP_foobar methods are the original GAP methods simply prefixed with the string libGAP_. The latter were originally not designed to be in a library, so some care needs to be taken to call them.
In particular, you must call libgap_mark_stack_bottom() in every function that calls into the libGAP C functions. The reason is that the GAP memory manager will automatically keep objects alive that are referenced in local (stack-allocated) variables. While convenient, this requires to look through the stack to find anything that looks like an address to a memory bag. But this requires vigilance against the following pattern:
cdef f()
libgap_mark_stack_bottom()
libGAP_function()
cdef g()
libgap_mark_stack_bottom();
f() # f() changed the stack bottom marker
libGAP_function() # boom
The solution is to re-order g() to first call f(). In order to catch this error, it is recommended that you wrap calls into libGAP in libgap_enter / libgap_exit blocks and not call libgap_mark_stack_bottom manually. So instead, always write
- cdef f()
- libgap_enter() libGAP_function() libgap_exit()
- cdef g()
- f() libgap_enter() libGAP_function() libgap_exit()
If you accidentally call libgap_enter() twice then an error message is printed to help you debug this:
sage: from sage.libs.gap.util import error_enter_libgap_block_twice
sage: error_enter_libgap_block_twice()
Traceback (most recent call last):
...
RuntimeError: Entered a critical block twice
AUTHORS:
- William Stein, Robert Miller (2009-06-23): first version
- Volker Braun, Dmitrii Pasechnik, Ivan Andrus (2011-03-25, Sage Days 29): almost complete rewrite; first usable version.
- Volker Braun (2012-08-28, GAP/Singular workshop): update to gap-4.5.5, make it ready for public consumption.
Bases: sage.structure.parent.Parent
The libgap interpreter object.
Note
This object must be instantiated exactly once by the libgap. Always use the provided libgap instance, and never instantiate Gap manually.
EXAMPLES:
sage: libgap.eval('SymmetricGroup(4)')
Sym( [ 1 .. 4 ] )
TESTS:
sage: TestSuite(libgap).run(skip=['_test_category', '_test_elements', '_test_pickling'])
alias of GapElement
Manually run the garbage collector
EXAMPLES:
sage: a = libgap(123)
sage: del a
sage: libgap.collect()
Return the number of GAP objects that are being tracked by libGAP
OUTPUT:
An integer
EXAMPLES:
sage: libgap.count_GAP_objects() # random output
5
Evaluate a gap command and wrap the result.
INPUT:
OUTPUT:
A GapElement.
EXAMPLES:
sage: libgap.eval('0')
0
sage: libgap.eval('"string"')
"string"
Return a GAP function wrapper
This is almost the same as calling libgap.eval(function_name), but faster and makes it obvious in your code that you are wrapping a function.
INPUT:
OUTPUT:
A function wrapper GapElement_Function for the GAP function. Calling it from Sage is equivalent to calling the wrapped function from GAP.
EXAMPLES:
sage: libgap.function_factory('Print')
<Gap function "Print">
Get a GAP global variable
INPUT:
OUTPUT:
A GapElement wrapping the GAP output. A ValueError is raised if there is no such variable in GAP.
EXAMPLES:
sage: libgap.set_global('FooBar', 1)
sage: libgap.get_global('FooBar')
1
sage: libgap.unset_global('FooBar')
sage: libgap.get_global('FooBar')
Traceback (most recent call last):
...
ValueError: libGAP: Error, VAL_GVAR: No value bound to FooBar
Temporarily change a global variable
INPUT:
OUTPUT:
A context manager that sets/reverts the given global variable.
EXAMPLES:
sage: libgap.set_global('FooBar', 1)
sage: with libgap.global_context('FooBar', 2):
....: print libgap.get_global('FooBar')
2
sage: libgap.get_global('FooBar')
1
Return information about libGAP memory usage
The GAP workspace is partitioned into 5 pieces (see gasman.c in the GAP sources for more details):
OUTPUT:
This function returns a tuple containing 5 integers. Each is the size (in bytes) of the five partitions of the workspace. This will potentially change after each GAP garbage collection.
EXAMPLES:
sage: libgap.collect()
sage: libgap.mem() # random output
(1048576, 6706782, 0, 960930, 0)
sage: libgap.FreeGroup(3)
<free group on the generators [ f1, f2, f3 ]>
sage: libgap.mem() # random output
(1048576, 6706782, 47571, 913359, 0)
sage: libgap.collect()
sage: libgap.mem() # random output
(1048576, 6734785, 0, 998463, 0)
Set a GAP global variable
INPUT:
EXAMPLES:
sage: libgap.set_global('FooBar', 1)
sage: libgap.get_global('FooBar')
1
sage: libgap.unset_global('FooBar')
sage: libgap.get_global('FooBar')
Traceback (most recent call last):
...
ValueError: libGAP: Error, VAL_GVAR: No value bound to FooBar
Print statistics about the GAP owned object list
Slight complication is that we want to do it without accessing libgap objects, so we don’t create new GapElements as a side effect.
EXAMPLES:
sage: a = libgap(123)
sage: b = libgap(456)
sage: c = libgap(789)
sage: del b
sage: libgap.show() # random output
11 LibGAP elements currently alive
rec( full := rec( cumulative := 122, deadbags := 9,
deadkb := 0, freekb := 7785, livebags := 304915,
livekb := 47367, time := 33, totalkb := 68608 ),
nfull := 3, npartial := 14 )
Return all Gap function names.
OUTPUT:
A list of strings.
EXAMPLES:
sage: len(libgap.trait_names()) > 1000
True
Remove a GAP global variable
INPUT:
EXAMPLES:
sage: libgap.set_global('FooBar', 1)
sage: libgap.get_global('FooBar')
1
sage: libgap.unset_global('FooBar')
sage: libgap.get_global('FooBar')
Traceback (most recent call last):
...
ValueError: libGAP: Error, VAL_GVAR: No value bound to FooBar
Return (integer) zero in GAP.
OUTPUT:
A GapElement.
EXAMPLES:
sage: libgap.zero_element()
0
Enter search terms or a module, class or function name.