使用 Ctypes 从 PARI/GP in Python 获取 Array/Vector

Getting an Array/Vector from PARI/GP in Python using Ctypes

我写了一个代码来比较 sympyPARI/GP 的解决方案,但是我遇到了从 PARI/GP 得到 array/vector 的问题。

当我尝试从 PARI/GP 函数 nfroots return 向量 res 时,我得到了这样的地址(见最后一行)-

    [3, 4]
elements as long (only if of type t_INT): 
3
4
<__main__.LP_LP_c_long object at 0x00000000056166C8>

如何从 nfroots 中获取 res 作为 vector/array 以便我可以像正常使用该数组 python vector/array?

代码如下to download the libpari.dll file, click here-

from ctypes import *
from sympy.solvers import solve
from sympy import Symbol

pari = cdll.LoadLibrary("libpari.dll")
pari.stoi.restype = POINTER(c_long)
pari.cgetg.restype = POINTER(POINTER(c_long))
pari.gtopoly.restype = POINTER(c_long)
pari.nfroots.restype = POINTER(POINTER(c_long))

(t_VEC, t_COL, t_MAT) = (17, 18, 19)  # incomplete
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):
        #Changed c_long to c_float, but got no output
        p1[i] = pari.stoi(c_long(numbers[i - 1]))
    return p1


def Quartic_Comparison():
    x = Symbol('x')
    #a=0;A=0;B=1;C=-7;D=13/12 #PROBLEM 1
    a=0;A=0;B=1;C=-7;D=12
    #a=0;A=0;B=-1;C=-2;D=1
    solution=solve(a*x**4+A*x**3+B*x**2+ C*x + D, x)
    print(solution)
    V=(A,B,C,D)
    P = pari.gtopoly(t_vec(V), c_long(-1))
    res = pari.nfroots(None, P)

    print("elements as long (only if of type t_INT): ")
    for i in range(1, pari.glength(res) + 1):        
         print(pari.itos(res[i]))
    return res               #PROBLEM 2

f=Quartic_Comparison()
print(f)

res 是来自 PARI/C 世界的元素。它是 PARI 整数的 PARI 向量(t_VEC of t_INTs)。 Python不知道。

如果要在Python这边做进一步处理,就必须进行转换。如果需要在 Python 和 PARI/C 世界之间交换数据,这通常是必需的。

因此,如果您有一个 t_VEC,t_INTs 在 PARI/C 一侧,就像在本例中一样,您很可能希望将其转换为 Python 列表.

一种可能的方法可能如下所示:

...
roots = pari.nfroots(None, P)

result = []
for i in range(1, pari.glength(roots) + 1):
    result.append(pari.itos(roots[i]))
return result