Ask Your Question
0

Is it possible to run GAP code in Sage?

asked 2024-09-08 00:54:41 +0100

Xico_pezao gravatar image

This might be a very basic question, but I'm struggling to find a solution. I have created a function in GAP, and I want to use it in Sagemath. So my question is: is it possible to run GAP code in Sage? For example:

f = gap('f := function(n) .... my entire code in GAP ... end;')

If it's not possible to run an entire code block, can I at least create a function in Sage that calls GAP functions?

To illustrate a simple scenario: Suppose I define vector spaces V and U in Sage. I want to define a morphism f using the GAP function LeftModuleHomomorphismByImages(V, U, gens, imgs).

edit retag flag offensive close merge delete

Comments

rather use libgap

sage: libgap.eval("4+4;")
8
sage: type(_)
<class 'sage.libs.gap.element.GapElement_Integer'>
FrédéricC gravatar imageFrédéricC ( 2024-09-08 11:07:07 +0100 )edit

Thanks, FrédéricC! This was very helpful, and my code is running now. One thing I couldn't fix was when we called a GAP function that contains some strings, but I managed to work around it in my code.

Xico_pezao gravatar imageXico_pezao ( 2024-09-09 21:18:20 +0100 )edit

You could post your solution as an answer below, if you like.

FrédéricC gravatar imageFrédéricC ( 2024-09-10 16:12:19 +0100 )edit

You could post your solution as an answer below, if you like.

You should post your solution as an answer below for the benefit of future ask.sagemath.org (per-)users...

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2024-09-11 07:30:59 +0100 )edit

2 Answers

Sort by » oldest newest most voted
1

answered 2024-09-14 17:06:22 +0100

Emmanuel Charpentier gravatar image

updated 2024-09-14 17:33:13 +0100

An alternative is to create a Sage (Python) function explicitly manipulating Gap objects. An example is needed to illustrate...

Note that you can also define "one liner" GAP functions (analogous to Sage's callable symbolic expressions, a.k.a. symbolic functions) with the -> Gap operator. Compare :

sage: bar=libgap.eval('x->x^3')
sage: type(bar)
<class 'sage.libs.gap.element.GapElement_Function'>
sage: bar.parent()
C library interface to GAP
sage: bar
<Gap function "unknown">
sage: bar(5)
125

with

sage: gee(x)=x^3
sage: type(gee)
<class 'sage.symbolic.expression.Expression'>
sage: gee.parent()
Callable function ring with argument x
sage: gee
x |--> x^3
sage: gee(5)
125

Note that this is not implemended in the pexpect gap interface.

sage: quux=gap.eval('x->x^3')
sage: quux(5)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[108], line 1
----> 1 quux(Integer(5))

TypeError: 'str' object is not callable
sage: quux
'function( x ) ... end'

More generally, the pexpect and library interfaces to Gap seem quite different in intent and possible uses. Compare the pexpect interface to the library interface.

HTH,

edit flag offensive delete link more
0

answered 2024-09-14 15:19:53 +0100

Xico_pezao gravatar image

To define GAP functions in Sage, you can use the command libgap.eval(), as mentioned earlier. For example:

F = libgap.eval("myfunction := function(x, y) if x = y then return true; else return false; fi; end;")

This allows you to call F(x, y) in Sage as if it were a native function. However, it's important to note that if the GAP function myfunction contains any string, Sage will return an error. A way to work around this issue is to use the String function to handle strings properly.

edit flag offensive delete link more

Comments

Errors can be avoided by using the correct Gap syntax for strings, quoting them if necessary :

sage: libgap.eval('s:="Hello"') # Single-quote-delimited Python string, double-quotes pass verbatim
"Hello"
sage: libgap.eval('Length(s);') # double-quote-delimited Python string : quote double-quotes to pass them to `libgap.eval`
5
sage: libgap.eval("s:=\"world !\"")
"world !"
sage: libgap.eval('Length(s);')
7

HTH,

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2024-09-14 16:29:56 +0100 )edit

It should also be noted that libgap.eval can parse and execute one statement, not a block :

sage: libgap.eval('s:="Hello" : Length(s);')
---------------------------------------------------------------------------
GAPError                                  Traceback (most recent call last)

[ Snip...]

GAPError: can only evaluate a single statement

The gap.eval function seems to do what you want :

sage: gap.eval('s:="Hello"; Length(s);')
'"Hello"\n5'
sage: print(gap.eval('s:="Hello"; Length(s);'))
"Hello"
5

Note that this will start an external gap process, communicating with the sage process by external means (pexpect ?). Expect (or at least do not rule out) mayhem...

HTH,

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2024-09-14 16:30:37 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2024-09-08 00:54:41 +0100

Seen: 289 times

Last updated: Sep 14