MathDemos:
Pari/GP examples
Pari-GP reference card
\q ; \p200; \u; \r filename; ?function ??keyword ???pattern whatnow(f) Input/Output previous result, the result before %, %`, %``, etc. n-th result since startup %n separate multiple statements on line ; extend statement on additional lines \ extend statements on several lines {seq1; seq2;} comment /* . . . */ one-line comment \\ . . . Metacommands & Defaults set default d to val default({d}, {val}) toggle timer on/off # note #(seq) return length(seq) print time for last result ## print defaults \d set debug level to n \g n set memory debug level to n \gm n set n significant digits / bits \p n, \pb n set n terms in series \ps n quit GP \q print the list of PARI types \t print the list of user-defined functions \u read file into GP \r filename; read({file}) set debuglevel for domain D to n setdebug(D, n) PARI Types & Input Formats t_INT. Integers; hex, binary ±31; ±0x1F, ±0b101 t_REAL. Reals ±3.14, 6.022 E23 t_INTMOD. Integers modulo m Mod(n, m) t_FRAC. Rational Numbers n/m t_FFELT. Elt in finite field Fq ffgen(q,’t) t_COMPLEX. Complex Numbers x + y ∗ I t_PADIC. p-adic Numbers x + O(p^k) t_QUAD. Quadratic Numbers x + y ∗ quadgen(D,{’w}) t_POLMOD. Polynomials modulo g Mod(f, g) t_POL. Polynomials a ∗ x^n + · · · + b t_SER. Power Series f + O(x^k) t_RFRAC. Rational Functions f /g t_QFB. Binary quadratic form Qfb(a, b, c) t_VEC/t_COL. Row/Column Vectors [x, y, z], [x, y, z]~ t_VEC integer range [1..10] t_VECSMALL. Vector of small ints Vecsmall([x, y, z]) t_MAT. Matrices [a, b;c, d] t_LIST. Lists List([x, y, z]) t_STR. Strings "abc" t_INFINITY. ±∞ +oo, -oo Input/Output print with/without \n, TEX format print, print1, printtex pretty print matrix printp print fields with separator printsep(sep, . . .),, printsep1 formatted printing printf() write args to file write, write1, writetex(file, args) write x in binary format writebin(file, x) read file into GP read({file}) . . . return as vector of lines readvec({file}) . . . return as vector of strings readstr({file}) read a string from keyboard input() User functions and closures x, y are formal parameters; y defaults to Pi if parameter omitted; z, t are local variables (lexical scope), z initialized to 1. fun(x, y=Pi) = my(z=1, t); seq fun = (x, y=Pi) -> my(z=1, t); seq attach help message h to s addhelp(s, h) undefine symbol s (also kills help) kill(s) Control Statements (X: formal parameter in expression seq) if(a, {seq1}, {seq2}) eval. seq for a ≤ X ≤ b for(X = a, b, seq) for X ∈ v foreach(v, X, seq) for primes a ≤ X ≤ b forprime(X = a, b, seq) for primes ≡ a (mod q) forprimestep(X = a, b, q, seq) for primes ≡ a (mod q) forprimestep(X = a, b, q, seq) for composites a ≤ X ≤ b forcomposite(X = a, b, seq) for a ≤ X ≤ b stepping s forstep(X = a, b, s, seq) for X dividing n fordiv(n, X, seq) X = [n, factor(n)], a ≤ n ≤ b forfactored(X = a, b, seq) as above, n squarefree forsquarefree(X = a, b, seq) X = [d, factor(d)], d | n fordivfactored(n, X, seq) multivariable for, lex ordering forvec(X = v, seq) loop over partitions of n forpart(p = n, seq) permutations of S forperm(S, p, seq) subsets of {1, . . . , n} forsubset(n, p, seq) k-subsets of {1, . . . , n} forsubset([n, k], p, seq) vectors v, q(v) ≤ B; q > 0 forqfvec(v, q, b, seq) H < G finite abelian group forsubgroup(H = G) evaluate seq until a = 0 until(a, seq) while a = 0, evaluate seq while(a, seq) exit n innermost enclosing loops break({n}) start new iteration of n-th enclosing loop next({n}) return x from current subroutine return({x})