# Introduction to PARI/GP¶

• PARI is a C library, allowing fast computations.
• GP is the name of the scripting language giving access to the PARI functions.

There are many ways to use PARI/GP:

• Jupyter interface to GP (the application that you are currently using)
• In your browser using JavaScript
• GP interactive shell (command-line application)
• Python package cypari2
• C code calling the PARI functions

## Basic objects¶

In :
57!

Out:
40526919504877216755680601905432322134980384796226602145184481280000000000000
In :
2 / 6

Out:
1/3
In :
(1+I)^2

Out:
2*I
In :
(x+1)^(-2)

Out:
1/(x^2 + 2*x + 1)
In :
Mod(2,5)^3

Out:
Mod(3, 5)
In :
Mod(x, x^2+x+1)^3

Out:
Mod(1, x^2 + x + 1)
In :
a = ffgen([3,5],'a)
a^12  \\ in F_3^5

Out:
2*a^4 + 2*a^3 + 2
In :
Pi

Out:
3.1415926535897932384626433832795028842
In :
log(2)

Out:
0.69314718055994530941723212145817656807
In :
\p100

   realprecision = 115 significant digits (100 digits displayed)

In :
log(2)

Out:
0.6931471805599453094172321214581765680755001343602552541206800094933936219696947156058633269964186875
In :
exp(%)

Out:
2.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In :
log(1+x)

Out:
x - 1/2*x^2 + 1/3*x^3 - 1/4*x^4 + 1/5*x^5 - 1/6*x^6 + 1/7*x^7 - 1/8*x^8 + 1/9*x^9 - 1/10*x^10 + 1/11*x^11 - 1/12*x^12 + 1/13*x^13 - 1/14*x^14 + 1/15*x^15 + O(x^16)
In :
exp(%)

Out:
1 + x + O(x^16)

## Functions¶

In :
?

Help topics: for a list of relevant subtopics, type ?n for n in
0: user-defined functions (aliases, installed and user functions)
1: PROGRAMMING under GP
3: CONVERSIONS and similar elementary functions
4: functions related to COMBINATORICS
5: NUMBER THEORETICAL functions
6: POLYNOMIALS and power series
7: Vectors, matrices, LINEAR ALGEBRA and sets
8: TRANSCENDENTAL functions
9: SUMS, products, integrals and similar functions
10: General NUMBER FIELDS
11: Associative and central simple ALGEBRAS
12: ELLIPTIC CURVES
13: L-FUNCTIONS
14: MODULAR FORMS
15: MODULAR SYMBOLS
16: GRAPHIC functions
17: The PARI community
Also:
? functionname (short on-line help)
?\             (keyboard shortcuts)
?.             (member functions)
Extended help (if available):
??             (opens the full user's manual in a dvi previewer)
??  tutorial / refcard / libpari (tutorial/reference card/libpari manual)
??  refcard-ell (or -lfun/-mf/-nf: specialized reference card)
??  keyword    (long help text about "keyword" from the user's manual)
??? keyword    (a propos: list of related functions).


## Help¶

In :
?4

binomial      fibonacci     hammingweight numbpart      numtoperm
partitions    permorder     permsign      permtonum     stirling


In :
?atan

atan(x): arc tangent of x.


In :
???determinant

algdisc           bnfsunit          charker           charpoly
matdetint         matdetmod         mathnfmod         matrixqz
mspolygon         nfdetint          nfhnfmod          polresultant
rnfdet

Finite abelian groups
Pseudo-bases, determinant



## Vectors and matrices¶

In :
V = [1,2,3]

Out:
[1, 2, 3]
In :
W = [4,5,6]~

Out:
[4, 5, 6]~
In :
M = [1,2,3;4,5,6]

Out:
[1 2 3]

[4 5 6]

In :
V*W

Out:
32
In :
M*W

Out:
[32, 77]~
In :
U = [1..10]

Out:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

## Components¶

In :
V

Out:
2
In :
W[1..2]

Out:
[4, 5]~
In :
M[2,2]

Out:
5
In :
M[1,]

Out:
[1, 2, 3]
In :
M[,2]

Out:
[2, 5]~
In :
M[1..2,1..2]

Out:
[1 2]

[4 5]


## Polymorphism¶

In :
factor(91)

Out:
[ 7 1]

[13 1]

In :
factor(91+I)

Out:
[      -1 1]

[   1 + I 1]

[ 4 + 5*I 1]

[1 + 10*I 1]

In :
factor(x^4+4)

Out:
[x^2 - 2*x + 2 1]

[x^2 + 2*x + 2 1]

In :
factor((x^4+4)*I)

Out:
[x + (-1 - I) 1]

[ x + (1 - I) 1]

[x + (-1 + I) 1]

[ x + (1 + I) 1]

In :
factor((x^4+1)*Mod(1,y^2-2))

Out:
[x^2 + Mod(-y, y^2 - 2)*x + 1 1]

[ x^2 + Mod(y, y^2 - 2)*x + 1 1]

In :
factor((x^4+4)*Mod(1,13))

Out:
[Mod(1, 13)*x + Mod(4, 13) 1]

[Mod(1, 13)*x + Mod(6, 13) 1]

[Mod(1, 13)*x + Mod(7, 13) 1]

[Mod(1, 13)*x + Mod(9, 13) 1]


## Numerical integration¶

In :
\p38

   realprecision = 38 significant digits

In :
intnum(x=0,1,1/(1+x^2))/Pi

Out:
0.25000000000000000000000000000000000000
In :
sumnum(n=1,1/n^2)/Pi^2

Out:
0.16666666666666666666666666666666666667
In :
sumalt(n=1,(-1)^n*log(n))

Out:
0.22579135264472743236309761494744107198
In :
exp(2*%)

Out:
1.5707963267948966192313216916397514427

## Comprehension¶

In :
[n^2|n<-[1..10]]

Out:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
In :
[n^2|n<-[1..10],isprime(n)]

Out:
[4, 9, 25, 49]
In :
[a,b] = [1,2];

In :
print("a=",a," b=",b)

a=1 b=2


# Programming in GP¶

In :
dist(a,b) = sqrt(a^2+b^2);

In :
dist(1,2)

Out:
2.2360679774997896964091736687312762354

In a GP file, line ending terminates the line unless the they are preceded by = or \ or are in a section delimited by braces:

In :
cmpz(a,b) = if (abs(a)>abs(b), print(a), print(b))

Out:
(a,b)->if(abs(a)>abs(b),print(a),print(b))
In :
cmpz(a,b) =
if (abs(a)>abs(b), print(a), print(b))

Out:
(a,b)->if(abs(a)>abs(b),print(a),print(b))
In :
cmpz(a,b) = if (abs(a)>abs(b), \
print(a), \
print(b))

Out:
(a,b)->if(abs(a)>abs(b),print(a),print(b))
In :
cmpz(a,b) =
{
if (abs(a)>abs(b),
print(a)
,print(b))
}

Out:
(a,b)->if(abs(a)>abs(b),print(a),print(b))

## Example of a function¶

• Put the opening brace on the line after the = sign.
• End the function by a semicolon.
• Declare any local variables with my().
• Do not declare the loop index: it is local to the loop anyway.
• The function return value is the last computed value.
• Indent you code following the example.
In :
fibo(n)=
{
my(u0=0,u1=1);
for(i=2,n,
[u0,u1]=[u1,u0+u1]);
u1;
}

In :
fibo(100)

Out:
354224848179261915075

## While loop¶

In :
rho(n)=
{
my(x=2,y=5);
while(gcd(y-x,n)==1,
x=(x^2+1)%n;
y=(y^2+1)%n; y=(y^2+1)%n
);
gcd(n,y-x);
}

In :
rho(2^64+1)

Out:
274177

## Control flow¶

In :
wieferich(n)=
{
forprime(p=2,n,
if(Mod(2,p^2)^(p-1)==1,
return(p)));
}

In :
wieferich2(n)=
{
my(r);
forprime(p=2,n,
if(Mod(2,p^2)^(p-1)==1,r=p;break));
r;
}

In :
wieferich(10000)

Out:
1093
In :
wieferich2(10000)

Out:
1093

## Constructors¶

In :
V=vector(10,i,1/i)

Out:
[1, 1/2, 1/3, 1/4, 1/5, 1/6, 1/7, 1/8, 1/9, 1/10]
In :
[1/i|i<-[1..10]]

Out:
[1, 1/2, 1/3, 1/4, 1/5, 1/6, 1/7, 1/8, 1/9, 1/10]
In :
M=matrix(4,4,i,j,i*j)

Out:
[1 2  3  4]

[2 4  6  8]

[3 6  9 12]

[4 8 12 16]


## forvec¶

In :
s3(n)=
{
my(m=sqrtint(n));
for (i=1,m,
for (j=1,m,
for (k=1,m,
if (i^2+j^2+k^2==n,
return([i,j,k])))));
}
s3(12345)

Out:
[4, 77, 80]

use forvec:

In :
s3(n)=
{
my(m=sqrtint(n));
forvec(v=vector(3,i,[1,m]),
if (v*v~==n,
return(v)));
}
s3(12345)

Out:
[4, 77, 80]

For a better algorithm, see qfsolve.

## Associative arrays¶

In :
birthday(n)=
{
my(M = Map());
for(i=1,oo,
my(x=random(n), j);
if(mapisdefined(M,x,&j),
return([i,j]));
mapput(M,x,i));
}

In :
birthday(2^30)

Out:
[23001, 18881]

# Algebraic Number Theory¶

## Irreducibility¶

In GP, we describe a number field $K$ as $K = Q[x]/f(x)$ where $f \in \mathbb{Z}[x]$ is a monic irreducible polynomial.

In :
f = x^4 - 2*x^3 + x^2 - 5

Out:
x^4 - 2*x^3 + x^2 - 5
In :
polisirreducible(f)

Out:
1

GP knows cyclotomic polynomials:

In :
g = polcyclo(30)

Out:
x^8 + x^7 - x^5 - x^4 - x^3 + x + 1

## Polmod¶

To perform simple operations in $K = \mathbb{Q}[x]/f(x) = \mathbb{Q}(\alpha)$ where $f(\alpha) = 0$, we can use Mod:

In :
Mod(x,f)^5

Out:
Mod(3*x^3 - 2*x^2 + 5*x + 10, x^4 - 2*x^3 + x^2 - 5)

Interpretation: $\alpha^5 = 3 \alpha^3 - 2 \alpha^2 + 5 \alpha + 10$. We check that the roots of $g$ are 30th roots of unity:

In :
lift(Mod(x,g)^15)

Out:
-1

We used lift to make the output more readable.

## polredbest¶

Sometimes we can find a simpler defining polynomial for the same number field by using polredbest:

In :
h = x^5 + 7*x^4 + 22550*x^3 - 281686*x^2 - 85911*x + 3821551;

In :
polredbest(h)

Out:
x^5 - x^3 - 2*x^2 + 1

Interpretation: $\mathbb{Q}[x]/h(x) \cong \mathbb{Q}[x]/(x^5 - x^3 - 2 x^2 + 1)$.

## nfinit and precomputed information¶

Most operations on number fields require a precomputation, which is performed by the initialisation function nfinit.

In :
K = nfinit(f);


K contains the precomputation of the number field K = \mathbb{Q}[x]/f(x).

In :
 K.pol

Out:
x^4 - 2*x^3 + x^2 - 5
In :
K.sign

Out:
[2, 1]

K has signature (2, 1): it has two real embeddings and one pair of conjugate complex embeddings.

In :
K.disc

Out:
-1975
In :
K.zk

Out:
[1, 1/2*x^2 - 1/2*x - 1/2, x, 1/2*x^3 - 1/2*x^2 - 1/2*x]
In :
w = K.zk

Out:
1/2*x^2 - 1/2*x - 1/2

K has discriminant $-1975$, and its ring of integers is $$\mathbb{Z}_K = \mathbb{Z} + \mathbb{Z}\frac{\alpha^2 - \alpha - 1}{2} + \mathbb{Z}\alpha + \mathbb{Z}\frac{\alpha^3 -\alpha^2 - \alpha}{2}$$

In [ ]: