PARI/GP

Try GP in your browser
Main
  Download
  Packages
  Funding
  SEARCH

Help / Community
  FAQ
  Documentation
  Tutorials
  Mailing Lists
  Bugs
  Timeline
  Ateliers PARI/GP

Library
  Publications
  Contributed GP scripts
  Links
  Fun!

Development
  Latest Changes
  Version Control
  Coding Guidelines
  PariDroid
  Logo

Tests & benchmarks
  Buildlogs
  Coverage Report
  Doc Coverage
  Refcards test
  Benchmarks

  WWW Stats

How to use extern: the example of mwrank

The extern routine allows gp to call an arbitrary program, and feed its result to the interpreter. This mechanism passes arguments to the external program as character strings, and reads its output as if it had been typed directly into gp. It is powerful, and simpler than the tight integration provided by install, which is more appropriate for heavy-duty usage of low level routines.

Unfortunately, using extern involves a number of interface problems which will vary from one application to the next. In this page, we shall use John Cremona's mwrank package as a simple case study. Given an elliptic curve E over Q, mwrank returns the rank of the free part of the group of its rational points, and a set of points generating a subgroup of finite index in this group.

We assume henceforth that you have a working version of mwrank installed on your computer.

We will successively For this particular example, printing GP data as a character string representing the vector of all 5 integer coefficients of the Weierstrass model is directly understood by mwrank. And similarly the output of mwrank with proper flags follows the GP syntax. In more complicated cases, one would also need a wrapper script converting GP data to the format expected by the external program, and the program's output to GP syntax. This is easily done in Bash, Perl or Python, but beyond the scope of this document.

Wrapper gpscript

In order to define the function mwrank, create a file mwrank.gp containing the following lines:
  mwrank(E)=
  {
    my(cmd=Str("echo '", E[1..5], "' | mwrank -q -o -v 0"));
    eval(externstr(cmd)[2]);
  }

This calls mwrank feeding the first 5 arguments of the elliptic curve structure (containing the Weierstrass model) and returns the second component of its output.

Put this file somewhere in your GP path. (See the path default in the manual.)

Loading mwrank on startup

Add the following line at the end of your .gprc file:
    read "mwrank.gp"

This causes the mwrank.gp file to be read into gp upon startup, thereby defining mwrank() routine in all gp sessions!

Of course, the names of the file and function do not have to coincide. They were chosen as above for consistency and self-documentation: mwrank.gp tells about its content, whereas lib.gp or extra.gp would not.

You may want to add a statement like
    path = "$HOME/pari/gpscripts"
in your .gprc, if for instance mwrank.gp went there.

Testing

You are ready to test the installation. Start gp and try some examples:
    ? mwrank([0,-1,1,-10,-20])
    %1 = [[0], []]

    ? mwrank([0,0,1,-1,0])
    %2 = [[1], [[0, 0]]]

    ? mwrank([1,0,0,0,1])
    %3 = [[2], [[1, 1], [3/4, 7/8]]]

PARI/GP Development
Last Modified: 2024-10-06 17:08:25
Copyleft © 2003-2022 the PARI group.