Skip to main content

Python interface to GAP

Project description

gappy logo

gappy — a Python interface to GAP

Documentation Status Test Status

gappy provides a Python interface to the GAP computer algebra system by linking to its library interface.

It allows calling functions in GAP directly from Python, and passing supported Python objects back to GAP.

gappy is based on SageMath’s LibGAP interface to GAP, originally developed by Volker Braun, but is completely independent of Sage–it does not require or use Sage at all, and can be used in any Python code. If there is enough interest, it may also be enhanced with a complementary GAP package for interacting with Python from within GAP.

Quickstart

To start using GAP functions from Python, just run:

>>> from gappy import gap

Then any global variable in GAP, including functions, can be accessed as attributes on gap like:

>>> gap.Cite()
Please use one of the following samples
to cite GAP version from this installation

Text:

[GAP] GAP  Groups, Algorithms, and Programming, Version 4.dev, The GAP Group, https://www.gap-system.org.
...

All global variables that would be available in a GAP session can be accessed in this way:

>>> GAPInfo.Version
"4.dev"

Most basic Python types have direct equivalents in GAP, and can be passed directly to GAP functions without explicit conversion to their equivalent GAP types:

>>> S4 = gap.SymmetricGroup(4)
>>> S4
Sym( [ 1 .. 4 ] )

You can also call “methods” on GapObjs. This is just syntactic sugar for calling a GAP function with that object as its first argument, in cases where that function supports the object bound to the method. For example:

>>> S4.GeneratorsOfGroup()
[ (1,2,3,4), (1,2) ]

Values returned from GAP functions are GAP objects wrapped in a Python class for containing them called GapObj:

>>> type(S4)
<class 'gappy.gapobj.GapObj'>

There are also specialized subclasses of GapObj for many types of objects in GAP. To explicitly convert a Python object directly to its GAP equivalent, you can call gap like:

>>> one = gap(1)
>>> type(one)
<class 'gappy.gapobj.GapInteger'>

GAP objects are displayed (with repr) or stringified (with str) the same way they would be in GAP, when displaying the object in the REPL or when calling GAP’s Print() function on the object, respectively:

>>> one
1
>>> s = gap("Hello GAP!")
>>> s
"Hello GAP!"
>>> print(s)
Hello GAP!

Not all GAP objects have an equivalent in basic Python types, so there is no implicit conversion from GAP back to Python. However, all Python types that can be converted to GAP objects can be converted back to their equivalent Python types in a symmetrical manner:

>>> int(one)
1
>>> type(int(one))
<class 'int'>
>>> str(s)
'Hello GAP!'
>>> type(str(s))
<class 'str'>

Likewise for floats, lists, dicts, among others.

You can also call obj.python() to convert to its equivalent Python type if one exists:

>>> type(one.python())
<class 'int'>

To register your own converters for GAP objects to custom Python types, see the gap.convert_to decorator.

Finally, you can execute arbitrary GAP code directly with gap.eval. This is often the easiest way to construct more complicated GAP objects, especially if you are more familiar with GAP syntax. The return value of gap.eval is the result of evaluating the same statement in GAP (the semicolon is optional when evaluating a single statement):

>>> rec = gap.eval('rec(a:=123, b:=456, Sym3:=SymmetricGroup(3))')
>>> rec['Sym3']
Sym( [ 1 .. 3 ] )

This is also an easy way to declare new GAP functions from gappy:

>>> sign = gap.eval("""sign := function(n)
...     if n < 0 then
...         return -1;
...     elif n = 0 then
...         return 0;
...     else
...         return 1;
...     fi;
... end;""")
>>> sign
<GAP function "sign">
>>> sign(0)
0
>>> sign(-99)
-1

See the full API documentation for many additional examples of how to use the gap object as well as the built-in GapObj types.

Installation

Prerequisites

  • Supported platforms: Linux, MacOS, Cygwin.

    • Likely works with most other *BSD flavors but has not been tested.

  • Python 3.6 or up with development headers installed. On Debian-based systems this means:

    $ sudo apt-get install python3.7-dev
  • GAP 4.10.2 or greater

It is possible to install from PyPI (note the distribution name gappy-system, do not install the package “gappy” which is an unrelated obsolete package):

$ pip install gappy-system

or from source:

$ git clone https://github.com/embray/gappy.git
$ cd gappy/
$ pip install .

However, depending on how GAP is installed, some extra steps may be required. In particular, if you installed GAP from source using the typical instructions on the GAP website you will need to make sure the libgap shared library is built by running:

$ make install-libgap

in the GAP source directory.

You will also need to point to the location of your GAP installation by setting the GAP_ROOT environment variable like:

$ GAP_ROOT=<path/to/gap/root> pip install .

If you needed to provide GAP_ROOT for the installation, it is also generally necessary to set this environment variable before using gappy, so that it can find the path to your GAP installation. See the documentation for the Gap class for more information.

If using GAP from a distribution system such as APT on Debian/Ubuntu or from Conda, however, the GAP library (libgap) is typically installed in a standard system location, and it may not be necessary to provide GAP_ROOT. See the next section for example.

Conda installation

To give an example of the above point, you can install gappy in a Conda environment as follows:

$ conda create -n gap
$ conda activate gap
$ conda install -c conda-forge gap-defaults==4.11 python==3.8
$ pip install .

Alternatively, you can create the conda environment using the supplied environment.yml file:

$ conda env create

Cygwin installation

Additional notes for installation on Cygwin:

  • The dependency psutil does not support Cygwin. However, there is an unofficial fork which does at: https://github.com/embray/psutil/tree/cygwin/v3. You can install it by running:

    $ pip install git+https://github.com/embray/psutil.git@cygwin/v3
  • The path to the libgap DLL (filename cyggap-0.dll) needs to be on your PATH environment variable in order for gappy to be importable. To do this you can either copy it from your GAP installation to a standard location like:

    $ cp /path/to/gap_root/.libs/cyggap-0.dll /usr/local/bin

    or you can modify your environment to point to where GAP places the built DLL:

    $ export PATH="/path/to/gap_root/.libs:$PATH"

    and add this to your .profile.

Changelog

v0.1.0a3 (2021-02-15)

Enhancements
  • Renamed the special method _gap_, for converting arbitrary Python objects to GAP objects, to __gap__ as inspired by the discussion at https://trac.sagemath.org/ticket/31297#comment:23

    • Likewise, the special method _gap_init_ is now named __gap_eval__ to emphasize that it returns a string to be passed to Gap.eval(). It still does not take any arguments.

  • Added GapObj.python() method for converting a GapObj to its equivalent type if one exists (it does not always, but it does in the cases where there is an equivalent type built into Python).

    • GapList.python() and GapRecord.python() also recursively convert the values they contain to equivalent Python types if possible.

  • New interface for registering converters to/from GAP object types:

    • Gap.register_converter is replaced with the Gap.convert_from decorator.

    • The GapObj.convert_to decorator can be used to register new conversion methods on GapObj, or specific subclasses thereof.

  • Added some C-level utility methods on GapInteger to help convert to different integer types (C long ints and mpz_t, depending on the size of the int). This helps with more efficient conversion to Sage Integers without having to pass through an intermediary Python int.

  • Implemented the __invert__ and __neg__ magic methods for GapObj.

  • Implemented a default __bool__ for all GapObj which returns False if its value is equal to zero.

  • Install the .pyx sources so that Cython tracebacks can work better.

Bug fixes
  • When converting a GapRecord to a dict with dict(rec) the keys remain as GapString instead of str. This is more consistent with the fact that the values are not converted to Python equivalents.

  • If an arbitrary GAP error occurs while looking up a global variable with Gap.__getattr__ it is handled and re-raised as an AttributeError.

  • The Gap.__repr__ method displays names of subclasses correctly.

v0.1.0a2 (2021-02-03)

Bug fixes

  • Made fixes for MacOS and Cygwin support.

v0.1.0a1 (2021-02-03)

Enhancements
  • Added LRU cache for functions defined with gap.gap_functions, restoring some of the caching functionality from Sage’s Gap.function_factory.

Bug fixes
  • Fixed bug in multi-indexing of nested lists.

  • Fixed minor formatting difference in the IndexError message when indexing single lists versus multi-indexing nested lists.

  • Fixed a bug when using functions defined with gap.gap_function as arguments to another GAP function before they have been called once.

v0.1.0a0 (2021-01-26)

  • Initial alpha release for testing against SageMath.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gappy-system-0.1.0a3.tar.gz (119.2 kB view details)

Uploaded Source

File details

Details for the file gappy-system-0.1.0a3.tar.gz.

File metadata

  • Download URL: gappy-system-0.1.0a3.tar.gz
  • Upload date:
  • Size: 119.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.5

File hashes

Hashes for gappy-system-0.1.0a3.tar.gz
Algorithm Hash digest
SHA256 90a2c9b6c4fd07ef3b9197303a929c00806e6c397b383895ffae1044864b0e5b
MD5 cb4f31f5a89bf806f7f5c68c9241dd04
BLAKE2b-256 5a4fa3b8001054faab5459105c5f9343f8d09747bd7a9e52e5152dcaf65e7076

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page