mersenneforum.org

mersenneforum.org (https://www.mersenneforum.org/index.php)
-   PARI/GP (https://www.mersenneforum.org/forumdisplay.php?f=155)
-   -   How to Get C files of PARI Functions? (https://www.mersenneforum.org/showthread.php?t=25392)

Andrew99 2020-03-21 19:40

How to Get C files of PARI Functions?
 
I want to use 4 specific functions of elliptic curve of PARI/GP found in the folder in [B]\pari2.11.3\src\functions\elliptic_curves[/B], this folder can be downloaded (Stable version: pari-2.11.3.tar.gz) from below link -

[url]https://pari.math.u-bordeaux.fr/download.html[/url]

But these functions do not have `.c` extension, but it is known that, PARI is a C library. I want use these function to call from python, so I need C version of these functions.

So, where or how can I get C version of these functions?


PS: When I say 4 specific functions, it could be any 4 from that folder, and exactly 4 of them, I hope, it is not important which 4 because procedure is same for all functions, also, I don't want use .dll file which has all the function.

CRGreathouse 2020-03-22 02:45

You can get the files from [url=https://pari.math.u-bordeaux.fr/git.html]version control[/url]. Many of the functions, for example, are in [url=https://pari.math.u-bordeaux.fr/cgi-bin/gitweb.cgi?p=pari.git;a=blob_plain;f=src/basemath/elliptic.c;hb=HEAD]src/basemath/elliptic.c[/url].

But it's not trivial to pull the functions into Python, because the numbers aren't stored in the same format -- Python doesn't use PARI GENs. (This doesn't apply if you stick strictly to functions taking long and ulong values, and returning the same or void, but that won't apply to many elliptic functions.) You might consider using one of[list][*] [url=https://code.google.com/p/pari-python/]pari-python[/url][*] [url=https://pypi.python.org/pypi/cypari/]cypari[/url][*] [url=https://github.com/sagemath/cypari2]cypari2[/url][/list]to simplify this for you. If not, I suggest using [url=http://pari.math.u-bordeaux.fr/pub/pari/manuals/gp2c/gp2c.html]gp2c[/url] to wrap the whole process from a declared long or ulong to your desired output to get at least a rough draft.

Andrew99 2020-03-22 02:57

[QUOTE=CRGreathouse;540427]You can get the files.....
But it's not trivial to pull the functions into Python, because the numbers aren't stored in the same format -- Python doesn't use PARI GENs. (This doesn't apply if you stick strictly to functions taking long and ulong values, and returning the same or void, but that won't apply to many elliptic functions.) You might consider using one of[list][*] [url=https://code.google.com/p/pari-python/]pari-python[/url][*] [url=https://pypi.python.org/pypi/cypari/]cypari[/url][*] [url=https://github.com/sagemath/cypari2]cypari2[/url][/list]to simplify this for you. If not, I suggest using [url=http://pari.math.u-bordeaux.fr/pub/pari/manuals/gp2c/gp2c.html]gp2c[/url] to wrap the whole process from a declared long or ulong to your desired output to get at least a rough draft.[/QUOTE]

The problem is I can't find any documentation on how to use cypari, I can't install cypari2 due to cysignals, pari-python is too old.

Could you plz guide me how to pass a vector/array into PARI using python by calling a finction of PARI through the use of ctypes?

And do you have any information about the documents on cypari? Thanks.

Andrew99 2020-03-22 03:04

We have something like below -

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)

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

def nextprime(v):
g = pari.nextprime(pari.stoi(ctypes.c_long(v))) # nextprime(argument) is a PARI function
return pari.itos(g)



print( nextprime(456) )


For example I tried -

h=(0,0,0, 4,6)
pari.stoi.restype = ctypes.POINTER(ctypes.c_long*5)
pari.ellinit.restype = ctypes.POINTER(ctypes.c_long)
def ellinit(v):
g = pari.ellinit(pari.stoi(ctypes.c_long(v)*5))
return pari.itos(g)


print(ellinit(h))

I got below error -

File "C:\Users\miron\Desktop\trash5\x\f.py", line 68, in <module>
print( ellinit(h) )
File "C:\Users\miron\Desktop\trash5\x\f.py", line 62, in ellinit
g = pari.ellinit(pari.stoi(ctypes.c_long(v)*5))
TypeError: an integer is required (got type tuple)

How do I pass a tuple/array/vector? Thanks.

CRGreathouse 2020-03-22 05:43

[QUOTE=Andrew99;540433]How do I pass a tuple/array/vector? Thanks.[/QUOTE]

This is not a simple question to answer for PARI in general (you get into issues of the stack and universal objects). But for what you are doing, the correct PARI code, I believe, is

[code]retmkvec5(gen_0, gen_0, gen_0, utoipos(4), utoipos(6))[/code]

I'll let you translate that into the appropriate python code.


All times are UTC. The time now is 07:54.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.