from cypari2 import Pari
pari = Pari()
pari.zeta(2)
1.64493406684823
pari.ellinit([-112, 400]).ellanalyticrank()
[3, 10.3910994007158]
pari.polmodular(3)
x^4 + (-y^3 + 2232*y^2 - 1069956*y + 36864000)*x^3 + (2232*y^3 + 2587918086*y^2 + 8900222976000*y + 452984832000000)*x^2 + (-1069956*y^3 + 8900222976000*y^2 - 770845966336000000*y + 1855425871872000000000)*x + (y^4 + 36864000*y^3 + 452984832000000*y^2 + 1855425871872000000000*y)
pari("my(Z=lfuninit(1, [100])); ploth(x=0, 100, lfunhardy(Z,x))")
[0.E-307, 100.000000000000, -7.26626527057968, 7.80311802062338]
discs = set(pari.quaddisc(-1-n) for n in range(200))
sorted([D for D in discs if D.qfbclassno() == 1], reverse=True)
[-3, -4, -7, -8, -11, -19, -43, -67, -163]
def cube(x):
return x**3
pari.apply(cube, range(10))
[0, 1, 8, 27, 64, 125, 216, 343, 512, 729]
1 / pari.Mod(4, 6)
--------------------------------------------------------------------------- PariError Traceback (most recent call last) <ipython-input-27-817f380fe1ec> in <module>() ----> 1 1 / pari.Mod(4, 6) cypari2/gen.pyx in cypari2.gen.Gen.__div__() cypari2/handle_error.pyx in cypari2.handle_error._pari_err_handle() PariError: impossible inverse in Fp_inv: Mod(2, 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())
<type 'cypari2.gen.Gen'> t_REAL
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()
PARI stack size set to 268435456 bytes, maximum size set to 268435456
%time hilberttrace(1000)
CPU times: user 5 ms, sys: 13 ms, total: 18 ms Wall time: 18.7 ms
%%script gp -q --default timer=1
allocatemem(2^28)
hilberttrace(n) = {my(m=mathilbert(n)); trace(m); return;}
hilberttrace(1000);
time = 61 ms.
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