我想从Python中调用PARI/GP的nfroots({nf}; x)
函数(see function no 3.13.135.on page 371 in this link:),但问题是,我不能发送需要发送的代数表达式或多项式,例如,x^2-7x+12
,下面是一个非常简单的例子,说明gp
可以用四次多项式做什么:
> V = readvec("coeff.txt");
> print(V)
[1,-7,12]
> P = Pol(V); # I get following error when I use Pol in my code: func=self._FuncPtr((name_or_ordinal, self)) AttributeError: function 'pol' not found
> print(P)
x^2 -7*x +12
> print(nfroots(,P))
>4, 3
从Stephan Schlecht (click here)的答案中,我设法写了-
from ctypes import *
pari = cdll.LoadLibrary("C:\\Program Files\\Pari64-2-11-3\\libpari.dll")
pari.stoi.restype = POINTER(c_long)
pari.cgetg.restype = POINTER(POINTER(c_long))
pari.nfroots.restype = POINTER(POINTER(c_long))
pari.pari_init(2 ** 19, 0)
def t_vec(numbers):
l = len(numbers) + 1
p1 = pari.cgetg(c_long(l), c_long(10)) #t_POL = 10,
for i in range(1, l):
p1[i] = pari.stoi(c_long(numbers[i - 1]))
return p1
def main():
h = "x^2-7x+12"
res = pari.nfroots(t_vec(h))
for i in range(1, len(res)):
print(pari.itos(res[i]))
if __name__ == '__main__':
main()
请注意,创建PARI对象有一个特定的过程(请参见Stephan Schlecht),我更改了t_POL = 10
的值,但是代码不起作用,如何从python执行上面的PARI/GP代码
一种解决办法可以是:
POINTER(c_long)
POINTER(POINTER(c_long))
.pari_printf
输出结果代码
测试
如果运行该程序,您将在调试控制台中获得所需的输出:
转换
使用
glength
可以确定载体的长度,请参阅 https://pari.math.u-bordeaux.fr/dochtml/html/Conversions_and_similar_elementary_functions_or_commands.html#length对于
itos
,如果参数类型为t_INT,则可以返回long,请参见https://pari.math.u-bordeaux.fr/pub/pari/manuals/2.7.6/libpari.pdf的第4.4.6节在代码中,它将如下所示:
To
GENtostr
给出了参数的字符串表示形式。可以这样使用:还有更多的转换选项,请参见上面的两个链接
相关问题 更多 >
编程相关推荐