Calling PARI / GP from Python

I would like to call PARI / GP from Python just to compute the function nextprime(n)

for the different n

ones I define. Unfortunately I can't get pari-python to install, so I thought I'd just call it using the command line via os.system

in Python. However, I don't see on the man page how to make PARI / GP work in non-interactive mode. Is there a way to achieve this?

+3


source to share


3 answers


You can enter input into gp stdin using a flag -q

to remove verbosity:

senderle:~ $ echo "print(isprime(5))" | gp -q
1

      

However, creating a simple python extension isn't that hard so that you can pass strings to the internal parser and return the results (as strings). Here's a bare bones version I wrote a while ago so that I can call the pari implementation of the APRT test from python. You can continue this to make the appropriate transformations and so on.

//pariparse.c

#include<Python.h>
#include<pari/pari.h>

static PyObject * pariparse_run(PyObject *self, PyObject *args) {
    pari_init(40000000, 2);
    const char *pari_code;
    char *outstr;

    if (!PyArg_ParseTuple(args, "s", &pari_code)) { return NULL; }
    outstr = GENtostr(gp_read_str(pari_code));
    pari_close();
    return Py_BuildValue("s", outstr);
}

static PyMethodDef PariparseMethods[] = {
    {"run", pariparse_run, METH_VARARGS, "Run a pari command."},
    {NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC initpariparse(void) {
    (void) Py_InitModule("pariparse", PariparseMethods);
}

      

And the installation file:



#setup.py

from distutils.core import setup, Extension

module1 = Extension('pariparse',
                    include_dirs = ['/usr/include', '/usr/local/include'],
                    libraries = ['pari'],
                    library_dirs = ['/usr/lib', '/usr/local/lib'],
                    sources = ['pariparse.c'])

setup (name = 'pariparse',
       version = '0.01a',
       description = 'A super tiny python-pari interface',
       ext_modules = [module1])

      

Then just type python setup.py build

in to build the extension. Then you can call it like this:

>>> pariparse.run('nextprime(5280)')
'5281'

      

I only tested this now and it compiled for me with the latest pari available via homebrew (on OS X). YMMV!

+4


source


You might want to try using the Sage math tool . Sage uses Python to glue all kinds of math libraries together, including PARI. Some of the math libraries are well integrated, others use hacks (passing strings to the library and then parsing the string results), but in all cases someone else did the integration for you and you can just use it.



You can set up your own Sage system, or get a free account and try Sage on the University of Washington servers.

+2


source


I don't think it's a good idea to call os.system

other than a quick and dirty workaround when you have a reliable C library. It's very easy to call C functions from Python; here are two functions to call nextprime. One uses long

whole numbers (despite the name, it means you are using small integers); the other uses the type string

(for longer integers).

First make sure you have it installed libpari

. Below is a Linux solution and assumes your library is named libpari.so

. On Windows this will probably be called with the suffix .dll

. You may have to type the entire path to the DLL file if it is not found on the first try:

import ctypes

# load the library
pari=ctypes.cdll.LoadLibrary("libpari.so")

# set the right return type of the functions
pari.stoi.restype = ctypes.POINTER(ctypes.c_long)
pari.nextprime.restype = ctypes.POINTER(ctypes.c_long)
pari.strtoGENstr.restype = ctypes.POINTER(ctypes.c_long)
pari.geval.restype = ctypes.POINTER(ctypes.c_long)
pari.itostr.restype = ctypes.c_char_p

# initialize the library 
pari.pari_init(2**19,0)

def nextprime(v):
  g = pari.nextprime(pari.stoi(ctypes.c_long(v)))
  return pari.itos(g)

def nextprime2(v):
  g = pari.nextprime(pari.geval(pari.strtoGENstr(str(v))))
  return int(pari.itostr(g))

print( nextprime(456) )
print( nextprime2(456) )

      

+1


source







All Articles