from cypari2 import Pari
pari = Pari()
pari.zeta(2)
pari.ellinit([-112, 400]).ellanalyticrank()
pari.polmodular(3)
pari("my(Z=lfuninit(1, [100])); ploth(x=0, 100, lfunhardy(Z,x))")
discs = set(pari.quaddisc(-1-n) for n in range(200))
sorted([D for D in discs if D.qfbclassno() == 1], reverse=True)
def cube(x):
return x**3
pari.apply(cube, range(10))
1 / pari.Mod(4, 6)
cypari2
is written in Cython, a Python-like language compiling to C (like gp2c
but for Python)
PARI objects (GEN
) are wrapped in a Python object Gen
(independent of the PARI type):
x = pari.pi()
print(type(x))
print(x.type())
Cython code wrapping most GP functions is auto-generated from pari.desc
. Example:
Function: cos
Class: basic
Section: transcendental
C-Name: gcos
Prototype: Gp
Help: cos(x): cosine of x.
Doc: cosine of $x$.
This becomes
def cos(self, x, long precision=0):
r'''
Cosine of :math:`x`.
'''
x = objtogen(x)
sig_on()
cdef GEN _x = (<Gen>x).g
precision = prec_bits_to_words(precision)
cdef GEN _ret = gcos(_x, precision)
return new_gen(_ret)
cypari2
is very fast:
Only overhead comes from dealing with Python objects and methods
pari.allocatemem(2**28)
def hilberttrace(n):
m = pari.mathilbert(n)
m.trace()
%time hilberttrace(1000)
%%script gp -q
hilberttrace(n) = {my(m=mathilbert(n)); trace(m); return;}
hilberttrace(1000);
cypari2
?)¶cypari
(to use it with their SnapPy
topology package)cypari2
cypari2
version 2 released, keeping GEN
s on the PARI stack instead of always copyingcypari
and cypari2
are still separate packages because of build/packaging issues and Windows compatibility of cysignals
.clgp
(problem: omega()
versus .omega
incompatibility)M[,1]
for a matrix M
) Assuming Python ≥ 2.7 and PARI/GP version ≥ 2.9.4 (installed in a location where Python will find it):
pip install cypari2
...or use SageMath.
Sources: https://github.com/sagemath/cypari2