我想僅從Python中調用PARI/GP來計算我定義的不同n
的函數nextprime(n)
。不幸的是我不能得到pari-python安裝,所以我想我會通過Python中的os.system
使用命令行來調用它。但是,在man page中我看不到如何讓PARI/GP以非交互模式運行。有沒有辦法做到這一點?從Python中調用PARI/GP
3
A
回答
4
可以通過管道輸入到GP的標準輸入像這樣,使用-q
標誌推翻冗長:
senderle:~ $ echo "print(isprime(5))" | gp -q
1
然而,這不是更難創建一個簡單的Python擴展,使您可以將字符串傳遞到同等的內部解析器並返回結果(作爲字符串)。這是我前段時間寫的一個簡單版本,這樣我就可以從python中調用pari的APRT test實現。您可以進一步擴展以進行適當的轉換等。
//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);
}
而且安裝文件:
#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])
然後只需鍵入python setup.py build
打造的擴展。然後你可以這樣調用:
>>> pariparse.run('nextprime(5280)')
'5281'
我測試了這個剛纔並通過自制軟件的最新版本同等的(在OS X)編譯的我。因人而異!
2
1
我不認爲這是一個好主意,打電話os.system
除了一個快速和骯髒的解決辦法,當你有一個背後的可靠的C庫。從Python調用C函數非常簡單;這裏有兩個調用nextprime的函數。一個是使用long
整數(儘管名稱,這意味着你在使用小整數);另一種是使用string
類型(對於更長的整數)。
首先檢查是否安裝了libpari
。以下解決方案適用於Linux,並假定您的庫名爲libpari.so
。在Windows下,它可能會以.dll
後綴調用。您可能需要如果它不是在第一次嘗試中鍵入DLL文件的完整路徑:
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. SWIG:從Python中調用Go
- 2. 從調用類Python中
- 3. 從JavaScript中調用Python
- 4. 從python中調用python腳本
- 5. 替代python-config可從Python中調用
- 6. 從python調用webjob
- 7. 從C++調用Python
- 8. 從Python調用Java
- 9. 從Python調用PHP
- 10. 從Ruby調用Python
- 11. 從Actionscript調用python
- 12. 從Python調用gawk
- 13. 使用shell_exec從PHP中調用Python
- 14. 在Linux中使用system()調用從C中調用Python
- 15. 從python中調用dll函數
- 16. 從__init__在Python中調用setter
- 17. 從shell腳本中調用python函數
- 18. Python包可從shell中調用嗎?
- 19. 從Lua程序中調用python函數
- 20. 從Python中調用.jar文件
- 21. 如何從Python中調用函數?
- 22. 從Python中的函數調用「變量」?
- 23. 從python腳本中調用EDITOR(vim)
- 24. 從java類中調用python模塊?
- 25. 如何從Python中調用OpenSSL函數
- 26. 從Python中調用DLL函數
- 27. 從PBS作業中調用python腳本
- 28. 從Python中調用CAPL函數
- 29. 從Python中調用並控制GDB
- 30. 從python腳本中調用java函數
非常感謝您! – felipa 2013-02-18 21:18:11