我想从Python调用PARI/GP。我需要使用PARI(see function no 3.15.35 on page 441 in this link:)的ellisdivisible(E; P; n;{&Q})
函数,所以我必须传递2个向量或数组(例如,e=ellinit([0,-1,1,0,0], K);P = [0,0];
),我是如何做到的
要从Python(given by Thomas Baruchel)调用单参数/变量的PARI函数(在C中),我们有如下内容-
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) )
例如,我试过-
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))
我犯了以下错误-
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)
如何传递元组/数组/向量?谢谢
编辑:
尝试获取ellisdivisible(E; P; n;{&Q})
失败-
from ctypes import *
pari = cdll.LoadLibrary("C:\\Program Files\\Python37\\Pari64-2-11-3\\libpari.dll")
pari.stoi.restype = POINTER(c_long)
pari.cgetg.restype = POINTER(POINTER(c_long))
pari.ellinit.restype = POINTER(POINTER(c_long))
#-------------------------CHANGE 1
pari.ellisdivisible.restype = c_long
Q = pari.stoi(c_long(0))
#-------------------------
(t_VEC, t_COL, t_MAT) = (17, 18, 19) # incomplete
precision = c_long(38)
pari.pari_init(2 ** 19, 0)
def t_vec(numbers):
l = len(numbers) + 1
p1 = pari.cgetg(c_long(l), c_long(t_VEC))
for i in range(1, l):
p1[i] = pari.stoi(c_long(numbers[i - 1]))
return p1
def main():
h = (0, 0, 0, 0, 1)
P=(0,0)
res = pari.ellinit(t_vec(h), pari.stoi(c_long(1)), precision)
#---------------CHANGE 2
# res = pari.ellinit(t_vec(h), pari.stoi(c_long(1)), precision).disc
y = pari.ellisdivisible(res, t_vec(P), pari.stoi(c_long(5)), byref(Q))
print(pari.itos(y))
#---------------
for i in range(1, 13):
print(pari.itos(res[i]))
if __name__ == '__main__':
main()
错误是——
Traceback (most recent call last):
File "C:\Users\miron\Desktop\trash5\x\ex - Copy (2).py", line 34, in <module>
main()
File "C:\Users\miron\Desktop\trash5\x\ex - Copy (2).py", line 28, in main
print(pari.itos(y))
OSError: exception: access violation reading 0x0000000000000009
Python元组或C数组不能直接使用,因为PARI使用的是PARI/GP特定向量,其中类型/长度在第一个元素中编码
在第4.4.1节{}中,它说:
见https://pari.math.u-bordeaux.fr/pub/pari/manuals/2.7.6/libpari.pdf
在本文档的示例中,您可以看到,要创建一个包含两个元素的向量,需要使用大小l=3来调用它,以获得合适的向量。实际数字向量的第一个元素不是以索引0开始,而是以索引1开始(参见本PDF文档中的第4.5.15节)
与
可以获取PARI的源代码
在这里,您可以在src/headers/parigen.h的末尾看到不同的类型。它是一个枚举,我们需要的类型是t_VEC。相应的整数是17
因此,我们现在可以定义一个小函数,将元组转换为GP向量,如下所示:
然后我们可以调用
ellinit
,因此:为了使用[0,0,0,4,6]参数对其进行测试,可以从命令行调用GP:
引用的PDF文档第441页示例中的一个小型、自包含Python程序可能如下所示:
测试
现在我们可以调用Python程序,并将其与交互式GP程序的输出进行比较,它实际上给出了相同的结果:
相关问题 更多 >
编程相关推荐