Ask Your Question
1

The use of %gp to switch to Pari/gp commands

asked 2018-03-01 17:20:08 +0200

RuudH gravatar image

I am using Sagemath 8.0 on my computer and also have subscribed to Cocalc (the former Sagemathcloud). I currently do quite a bit of programming in Pari/gp and noted that using %gp in the Cocalc-cloud allows me to switch to entering a sequence of Pari/gp commands, however this doesn't seem to work for the PC-version I use. Of course I can circumvent this by transforming each Pari/gp command into a string with pari(' .... '), however that can only be done line by line and produces ugly code.

Is it correct that %gp doesn't work on the PC version and if so, is there another way to write multiple lines of Pari/gp code other than producing line by line strings and/or use the terminal?

Thanks.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2018-03-01 18:58:07 +0200

B r u n o gravatar image

Short answer: You probably need to use %%gp instead of %gp.

Explanations:

The behavior depends on the front-end you are using:

  • If you use the Ipython shell (running in a terminal),

    • typing %gpswitches to GP and you can use GP in a standard manner. The same thing occurs if you run SageMath using sage -gp.
    • typing %%gp allows you to write several lines of Pari/GP. (A final blank line is needed to evaluate the previous lines.)
  • If you use Jupyter notebooks, you have two options:

    • %gp <command> (on a single line) executes <command> from Pari/GP (example: %gp primepi(123456))
    • %%gp as the first line of a cell turns the cell into a Pari/GP cell (in the same way basically as %%gp in the Ipython shell).
  • If you use the old Sage notebook, you can turn a cell into a Pari/GP cell by using %gp on its first line. This is the same as the second option for Jupyter notebooks, but with one % instead of %%. You cannot use %%gp in this case.

As far as I understand, Cocalc's choice is to mimic the behavior of the old Sage notebook.

Examples of use in Jupyter notebooks:

%gp primepi(123456)
11601

or

%%gp
n = primepi(123456)
n + 1
11601
11602
edit flag offensive delete link more

Comments

Many thanks for your swift response, Bruno. That is exactly what I was looking for! Have tried it and it works perfectly :-)

Just one more question if I may, what is the code to toggle back from pari/gp to Sage/Python code?

RuudH gravatar imageRuudH ( 2018-03-01 19:58:50 +0200 )edit

Oops. Just realised there probably isn't a code to toggle back, since %%gp turns the entire cell in Pari/GP code and using %gp <command> allows me to mix and match Pari/GP commands and other languages.

RuudH gravatar imageRuudH ( 2018-03-01 20:38:06 +0200 )edit

You're right. In the Ipython shell, typing <ctrl>+D brings you back to "normal" SageMath. In notebooks, simply going to a new cell in enough.

B r u n o gravatar imageB r u n o ( 2018-03-02 12:22:35 +0200 )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: 2018-03-01 17:20:08 +0200

Seen: 744 times

Last updated: Mar 01 '18