Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Testing latest pari + WASM + node.js... and it works?! Wow.

28495 views
License: GPL3
ubuntu2004
1
% Copyright (c) 2000 The PARI Group
2
%
3
% This file is part of the PARI/GP documentation
4
%
5
% Permission is granted to copy, distribute and/or modify this document
6
% under the terms of the GNU General Public License
7
\chapter{The gp Calculator}
8
9
\section{Introduction}
10
11
Originally, \tet{gp} was designed as a debugging device for the PARI system
12
library. Over the years, it has become a powerful user-friendly stand-alone
13
calculator. The mathematical functions available in PARI and \kbd{gp} are
14
described in the next chapter. In the present one, we describe the specific
15
use of the \kbd{gp} programmable calculator.
16
17
\emacs If you have GNU Emacs and use the PariEmacs package, you can work in a
18
special Emacs shell, described in \secref{se:emacs}. Specific features of
19
this Emacs shell are indicated by an EMACS sign in the left margin.
20
21
We briefly mention at this point GNU
22
TeXmacs (\url{http://www.texmacs.org/}), a free wysiwyg editing
23
platform that allows to embed an entire gp session in a document, and
24
provides a nice alternative to PariEmacs.
25
26
\subsec{Startup}
27
28
To start the calculator, the general command line syntax is:
29
30
\kbd{gp [-D \var{key}=\var{val}] [\var{files}]}
31
32
\noindent
33
where items within brackets are optional. The [\var{files}] argument is a
34
list of files written in the GP scripting language, which will be loaded on
35
startup. There can be any number of arguments of the form
36
\kbd{-D \var{key}=\var{val}}, setting some internal parameters of \kbd{gp},
37
or \var{defaults}: each sets the default \var{key} to the value \var{val}. See
38
\secref{se:defaults} below for a list and explanation of all defaults. These
39
defaults can be changed by adding parameters to the input line as above, or
40
interactively during a \kbd{gp} session, or in a preferences file also known
41
as \tet{gprc}.
42
43
If a \idx{preferences file} (to be discussed in \secref{se:gprc}) is
44
found, \kbd{gp} then reads it and executes the commands it contains. This
45
provides an easy way to customize \kbd{gp}. The \var{files} argument is
46
processed right after the \kbd{gprc}.
47
48
A copyright banner then appears which includes the version number, and a lot
49
of useful technical information. After the copyright, the computer writes the
50
top-level help information, some initial defaults, and then waits after
51
printing its prompt, which is '\kbd{?~}' by default . Whether extended
52
on-line help and line editing are available or not is indicated in this
53
\kbd{gp} banner, between the version number and the copyright message.
54
Consider investigating the matter with the person who installed \kbd{gp} if
55
they are not. Do this as well if there is no mention of the GMP kernel.
56
57
\subsec{Getting help}
58
59
To get help, type a \kbd{?} and hit return. A menu appears, describing the
60
main categories of available functions and how to get more detailed
61
help. If you now type \kbd{?$n$} with $n = 1, 2, \dots$, you get the list of
62
commands corresponding to category $n$ and simultaneously to Section $3.n$ of
63
this manual. If you type \kbd{?}\var{functionname} where \var{functionname}
64
is the name of a PARI function, you will get a short explanation of this
65
function.
66
67
If extended help (see \secref{se:exthelp}) is available on your system,
68
you can double or triple the \kbd{?} sign to get much more: respectively the
69
complete description of the function (e.g.~\kbd{??sqrt}), or a list of
70
\kbd{gp} functions relevant to your query (e.g.~ \kbd{???"elliptic curve"}
71
or \kbd{???"quadratic field"}).
72
73
If \kbd{gp} was properly installed (see Appendix~A), a line editor is
74
available to correct the command line, get automatic completions, and so on.
75
See \secref{se:readline} or \kbd{??readline} for a short summary of the line
76
editor's commands.
77
78
If you type \kbd{?\bs} you will get a short description of the metacommands
79
(keyboard shortcuts).
80
81
Finally, typing \kbd{?.} will return the list of available (pre-defined)
82
member functions. These are functions attached to specific kind of objects,
83
used to retrieve easily some information from complicated structures (you can
84
define your own but they won't be shown here). We will soon describe these
85
commands in more detail.
86
87
More generally, commands starting with the symbols \b\ or \kbd{?}, are not
88
computing commands, but are metacommands which allow you to exchange
89
information with \kbd{gp}. The available metacommands can be divided into
90
default setting commands (explained below) and simple commands (or keyboard
91
shortcuts, to be dealt with in \secref{se:meta}).
92
93
\subsec{Input}
94
95
Just type in an instruction, e.g. \kbd{1 + 1}, or \kbd{Pi}. No action is
96
undertaken until you hit the \kbd{<Return>} key. Then computation starts, and
97
a result is eventually printed. To suppress printing of the result, end the
98
expression with a \kbd{;} sign. Note that many systems use \kbd{;} to
99
indicate end of input. Not so in \kbd{gp}: a final semicolon means the
100
result should not be printed. (Which is certainly useful if it occupies
101
several screens.)
102
103
\subsec{Interrupt, Quit}
104
105
Typing \kbd{quit} at the prompt ends the session and exits \kbd{gp}. At any
106
point you can type \kbd{Ctrl-C} (that is press simultaneously the
107
\kbd{Control} and \kbd{C} keys): the current computation is interrupted and
108
control given back to you at the \kbd{gp} prompt, together with a message
109
like
110
\bprog
111
*** at top-level: gcd(a,b)
112
*** ^--------
113
*** gcd: user interrupt after 236 ms.
114
@eprog\noindent
115
telling you how much time elapsed since the last command was typed in and
116
in which GP function the computation was aborted. It does not mean that that
117
much time was spent in the function, only that the evaluator was busy
118
processing that specific function when you stopped it.
119
120
\section{The general gp input line}
121
122
The \kbd{gp} calculator uses a purely interpreted language GP. The structure
123
of this language is reminiscent of LISP with a functional notation,
124
\kbd{f(x,y)} rather than \kbd{(f x y)}: all programming constructs,
125
such as \kbd{if}, \kbd{while,} etc\dots are functions\footnote{*}{Not exactly,
126
since not all their arguments need be evaluated. For instance it would be
127
stupid to evaluate both branches of an \kbd{if} statement: since only one
128
will apply, only this one is evaluated.}, and the main loop does not really
129
execute, but rather evaluates (sequences of) expressions. Of course, it is by
130
no means a true LISP, and has been strongly influenced by C and Perl since
131
then.
132
133
\subsec{Introduction} User interaction with a \kbd{gp} session proceeds as
134
follows. First, one types a sequence of characters at the \kbd{gp} prompt;
135
see \secref{se:readline} for a description of the line editor. When you hit
136
the \kbd{<Return>} key, \kbd{gp} gets your input, evaluates it, then prints
137
the result and assigns it to an ``history'' array.
138
139
More precisely, the input is case-sensitive and, outside of character
140
strings, blanks are completely ignored. Inputs are either metacommands or
141
sequences of expressions. Metacommands are shortcuts designed to alter gp's
142
internal state, such as the working precision or general verbosity level; we
143
shall describe them in \secref{se:meta}, and ignore them for the time being.
144
145
The evaluation of a sequence of instructions proceeds in two phases: your
146
input is first digested (byte-compiled) to a bytecode suitable for fast
147
evaluation, in particular loop bodies are compiled only once but a priori
148
evaluated many times; then the bytecode is evaluated.
149
150
An \idx{expression}\sidx{expression sequence} is formed by combining
151
constants, variables, operator symbols, functions and control statements.
152
It is evaluated using the conventions about operator priorities and left to
153
right associativity. An expression always has a value, which can be any PARI
154
object:
155
\bprog
156
? 1 + 1
157
%1 = 2 \\@com an ordinary integer
158
? x
159
%2 = x \\@com a polynomial of degree 1 in the unknown \kbd{x}
160
? print("Hello")
161
Hello \\@com \kbd{void} return value, 'Hello' printed as side effect
162
? f(x) = x^2
163
%4 = (x)->x^2 \\@com a user function
164
@eprog
165
\noindent In the third example, \kbd{Hello} is printed as a side effect, but
166
is not the return value. The \kbd{print} command is a \emph{procedure},
167
which conceptually returns nothing. But in fact procedures return a special
168
\kbd{void} object, meant to be ignored (but which evaluates
169
to $0$ in a numeric context, and stored as $0$ in the history or results).
170
The final example assigns to the variable \kbd{f} the function $x\mapsto
171
x^2$, the alternative form \kbd{f = x->x\pow2} achieving the same effect; the
172
return value of a function definition is, unsurprisingly, a function object
173
(of type \typ{CLOSURE}).
174
175
Several expressions are combined on a single line by separating them with
176
semicolons ('\kbd{;}'). Such an expression sequence will be called a
177
\var{seq}. A \var{seq} also has a value, which is the value of the last
178
expression in the sequence. Under \kbd{gp}, the value of the \var{seq}, and
179
only this last value, becomes an history entry. The values of the other
180
expressions in the \var{seq} are discarded after the execution of the
181
\var{seq} is complete, except of course if they were assigned into variables.
182
In addition, the value of the \var{seq} is printed if the line does not end
183
with a semicolon \kbd{;}.
184
185
\subsec{The gp history of results}
186
187
This is not to be confused with the history of your \emph{commands},
188
maintained by \kbd{readline}. The \kbd{gp} history contains the \emph{results}
189
they produced, in sequence.
190
191
The successive elements of the history array are called \kbd{\%1}, \kbd{\%2},
192
\dots As a shortcut, the latest computed expression can also be
193
called \kbd{\%}, the previous one \kbd{\%`},
194
the one before that \kbd{\%``} and so on.
195
196
When you suppress the printing of the result with a semicolon, it is still
197
stored in the history, but its history number will not appear either. It is a
198
better idea to assign it to a variable for later use than to mentally
199
recompute what its number is. Of course, on the next line, you may just use
200
\kbd{\%}.
201
202
The time used to compute that history entry is also stored as part of the entry
203
and can be recovered using the \kbd{\%\#} operator: \kbd{\%\#1}, \kbd{\%\#2},
204
\kbd{\%\#`}; \kbd{\%\#} by itself returns the time needed to compute the last
205
result (the one returned by \kbd{\%}). The output is a vector with two
206
components \kbd{[cpu, real]} where \kbd{cpu} is the CPU time and \kbd{real} is
207
the wall clock time.
208
209
\misctitle{Remark}
210
The history ``array'' is in fact better thought of as a queue: its size is
211
limited to 5000 entries by default, after which \kbd{gp} starts forgetting
212
the initial entries. So \kbd{\%1} becomes unavailable as \kbd{gp} prints
213
\kbd{\%5001}. You can modify the history size using \tet{histsize}.
214
215
\subsec{Special editing characters}\sidx{editing characters} A GP program
216
can of course have more than one line. Since your commands are executed as
217
soon as you have finished typing them, there must be a way to tell \kbd{gp}
218
to wait for the next line or lines of input before doing anything. There are
219
three ways of doing this.
220
221
The first one is to use the \idx{backslash character} \kbd{\bs} at the end of
222
the line that you are typing, just before hitting \kbd{<Return>}. This tells
223
\kbd{gp} that what you will write on the next line is the physical
224
continuation of what you have just written. In other words, it makes \kbd{gp}
225
forget your newline character. You can type a \kbd{\bs} anywhere. It is
226
interpreted as above only if (apart from ignored whitespace characters) it is
227
immediately followed by a newline. For example, you can type
228
\bprog
229
? 3 + \
230
4
231
@eprog
232
\noindent instead of typing \kbd{3 + 4}.
233
234
The second one is a variation on the first, and is mostly useful when
235
defining a user function (see \secref{se:user_defined}): since an equal sign
236
can never end a valid expression, \kbd{gp} disregards a newline immediately
237
following an \kbd{=}.
238
\bprog
239
? a =
240
123
241
%1 = 123
242
@eprog
243
244
The third one is in general much more useful, and uses braces \kbd{\obr} and
245
\kbd{\cbr}.\sidx{brace characters} An opening brace \kbd{\obr} signals that
246
you are typing a multi-line command, and newlines are ignored until you type
247
a closing brace \kbd{\cbr}. There are two important, but easily obeyed,
248
restrictions: first, braces do not nest; second, inside an open brace-close
249
brace pair, all input lines are concatenated, suppressing any newlines. Thus,
250
all newlines should occur after a semicolon (\kbd{;}), a comma (\kbd{,}) or
251
an operator (for clarity's sake, never split an identifier over two lines in
252
this way). For instance, the following program
253
\bprog
254
{
255
a = b
256
b = c
257
}
258
@eprog
259
260
\noindent would silently produce garbage, since this is interpreted as
261
\kbd{a=bb=c} which assigns the value of \kbd{c} to both \kbd{bb} and
262
\kbd{a}. It should have been written
263
\bprog
264
{
265
a = b;
266
b = c;
267
}
268
@eprog
269
270
\section{The PARI types}
271
272
\noindent
273
We see here how to input values of the different data types known to PARI.
274
Recall that blanks are ignored in any expression which is not a string (see
275
below).
276
277
\misctitle{A note on efficiency}
278
The following types are provided for convenience, not for speed:
279
\typ{INTMOD}, \typ{FRAC}, \typ{PADIC}, \typ{QUAD}, \typ{POLMOD},
280
\typ{RFRAC}. Indeed, they always perform a reduction of some kind after
281
each basic operation, even though it is usually more efficient to perform
282
a single reduction at the end of some complex computation. For instance,
283
in a convolution product $\sum_{i+j = n} x_i y_j$ in $\Z/N\Z$ --- common
284
when multiplying polynomials! ---, it is quite wasteful to perform $n$
285
reductions modulo $N$. In short, basic individual operations on these types
286
are fast, but recursive objects with such components could be handled more
287
efficiently: programming with libpari will save large constant factors here,
288
compared to GP.
289
290
\subsec{Integers (\typ{INT})}%
291
\sidx{integer}\kbdsidx{t_INT}
292
After an (optional) leading \kbd{+} or \kbd{-}, type in the
293
decimal digits of your integer. No decimal point!
294
\bprog
295
? 1234567
296
%1 = 1234567
297
? -3
298
%2 = -3
299
? 1. \\@com oops, not an integer
300
%3 = 1.000000000000000000000000000
301
@eprog
302
303
Integers can be input in hexadecimal notation by prefixing them with
304
\kbd{0x}; hexadecimal digits ($a, \dots, f$) can be input either in lowercase
305
or in uppercase:
306
\bprog
307
? 0xF
308
%4 = 15
309
? 0x1abcd
310
%5 = 109517
311
@eprog
312
313
Integers can also be input in binary by prefixing them with
314
\kbd{0b}:
315
\bprog
316
? 0b010101
317
%6 = 21
318
@eprog
319
320
\subsec{Real numbers (\typ{REAL})}%
321
\sidx{real number}\kbdsidx{t_REAL}
322
323
Real numbers are represented (approximately) in a floating point system,
324
internally in base 2, but converted to base 10 for input / output purposes.
325
A \typ{REAL} object has a given \emph{bit accuracy} (or \emph{bit precision})
326
$\ell \geq 0$; it comprises
327
328
\item a sign $s$: $+1$, $-1$ or $0$;
329
330
\item a mantissa $m$: a multiprecision integer, $0\leq m < 2^\ell$;
331
332
\item an exponent $e$: a small integer in $[-2^B,2^B[$, where $B = 31$ on a
333
32-bit machine and 63 otherwise.
334
335
This data may represent any real number $x$ such that
336
$$|x - s m 2^e| < 2^{e-\ell}.$$
337
We consider that a \typ{REAL} with sign $s = 0$ has accuracy $\ell = 0$, so
338
that its mantissa is useless, but it still has an exponent $e$ and acts like
339
a machine epsilon for all accuracies $< e$.
340
341
After an (optional) leading \kbd{+} or \kbd{-}, type a number with a decimal
342
point. Leading zeroes may be omitted, up to the decimal point, but trailing
343
zeroes are important: your \typ{REAL} is assigned an internal precision,
344
which is the supremum of the input precision, one more than the number of
345
decimal digits input, and the default \tet{realprecision}. For example, if
346
the default precision is 38 digits, typing \kbd{2.} yields a precision of 38
347
digits, but \kbd{2.0\dots0} with 45 zeros gives a number with internal
348
decimal precision at least 45, although less may be printed.
349
350
You can also use scientific notation with the letter \kbd{E} or
351
\kbd{e}. As usual, \kbd{e$n$} is interpreted as $\times 10^n$ for all
352
integers $n$. Since the result is converted to a \typ{REAL}, you may often
353
omit the decimal point in this case: \kbd{6.02 E 23} or \kbd{1e-5} are fine,
354
but \kbd{e10} is not.
355
356
By definition, \kbd{0.E $n$} returns a real $0$ of exponent $n$, whereas
357
\kbd{0.} returns a real 0 ``of default precision'' (of exponent
358
$-\tet{realprecision}$), see \secref{se:whatzero}, behaving like the machine
359
epsilon for the current default accuracy: any float of smaller absolute value
360
is indistinguishable from $0$.
361
362
\misctitle{Note on output formats} A zero real number is printed in \kbd{e}
363
format as $0.Exx$ where $xx$ is the (usually negative) \emph{decimal}
364
exponent of the number (cf.~\secref{se:whatzero}). This allows the user to
365
check the accuracy of that particular zero.
366
367
When the integer part of a real number $x$ is not known exactly because the
368
exponent of $x$ is greater than the internal precision, the real number is
369
printed in \kbd{e} format.
370
371
\misctitle{Technical note} The internal \emph{precision} is actually
372
expressed in bits and can be viewed and manipulated globally in interactive
373
use via \kbd{realprecision} (decimal digits, as explained above; shortcut
374
\kbd{\bs p}) or \kbd{realbitprecision} (bits; shortcut \kbd{\bs ps}), the
375
latter allowing finer granularity. See \secref{se:trans} for details. In
376
programs we advise to leave this global variable alone and adapt precision
377
locally for a given sequence of computations using \tet{localbitprec}.
378
379
Note that most decimal floating point numbers cannot be converted exactly
380
in binary, the (binary) number actually stored is a rounded version of the
381
(decimal) number input. Analogously, a decimal output is rounded
382
from the internal binary representation.
383
384
\subsec{Intmods (\typ{INTMOD})}%
385
\sidx{intmod}\kbdsidx{t_INTMOD}
386
To create the image of the integer $a$ in $\Z/b\Z$ (for some nonzero
387
integer $b$), type \kbd{Mod(a,b)}; \emph{not} \kbd{a\%b}.
388
Internally, all operations are done on integer representatives belonging to
389
$[0,b-1]$.
390
391
Note that this type is available for convenience, not for speed: each
392
elementary operation involves a reduction modulo $b$.
393
394
If $x$ is a \typ{INTMOD} \kbd{Mod(a,b)}, the following member function is
395
defined:
396
397
\kbd{x.mod}: return the modulus \kbd{b}.
398
399
\subsec{Rational numbers (\typ{FRAC})}%
400
\sidx{rational number}\kbdsidx{t_FRAC}
401
All fractions are automatically reduced to lowest
402
terms, so it is impossible to work with reducible fractions. To enter $n/m$
403
just type it as written. As explained in \secref{se:gdiv}, floating point
404
division is \emph{not} performed, only reduction to lowest
405
terms.\label{se:FRAC}
406
407
Note that rational computation are almost never the fastest method to proceed:
408
in the PARI implementation, each elementary operation involves computing a gcd.
409
It is generally a little more efficient to cancel denominators and work with
410
integers only:
411
\bprog
412
? P = Pol( vector(10^3,i, 1/i) ); \\@com big polynomial with small rational coeffs
413
? P^2
414
time = 1,392 ms.
415
? c = content(P); c^2 * (P/c)^2; \\@com same computation in integers
416
time = 1,116 ms.
417
@eprog\noindent
418
And much more efficient (but harder to setup) to use homomorphic imaging
419
schemes and modular computations. As the simple example below indicates, if you
420
only need modular information, it is very worthwhile to work with
421
\typ{INTMOD}s directly, rather than deal with \typ{FRAC}s all the way through:
422
\bprog
423
? p = nextprime(10^7);
424
? sum(i=1, 10^5, 1/i) % p
425
time = 13,288 ms.
426
%1 = 2759492
427
? sum(i=1, 10^5, Mod(1/i, p))
428
time = 60 ms.
429
%2 = Mod(2759492, 10000019)
430
@eprog\noindent
431
432
\subsec{Finite field elements (\typ{FFELT})}%
433
\sidx{finite field element}\kbdsidx{t_FFELT}
434
Let $T\in\F_p[X]$ be a monic irreducible polynomial defining your
435
finite field over $\F_p$, for instance obtained using \tet{ffinit}. Then the
436
\tet{ffgen} function creates a generator of the finite field as an
437
$\F_p$-algebra, namely the class of $X$ in $\F_p[X]/(T)$, from which you can
438
build all other elements. For instance, to create the field $\F_{2^8}$, we
439
write
440
\bprog
441
? T = ffinit(2, 8);
442
? y = ffgen(T, 'y);
443
? y^0 \\ the unit element in the field
444
%3 = 1
445
? y^8
446
%4 = y^6 + y^5 + y^4 + y^3 + y + 1
447
@eprog\noindent
448
The second (optional) parameter to \tet{ffgen} is only used to display
449
the result; it is customary to use the name of the variable we assign the
450
generator to. If \kbd{g} is a \typ{FFELT}, the following member functions are
451
defined:
452
453
\kbd{g.pol}: the polynomial (with reduced integer coefficients)
454
expressing \kbd{g} in term of the field generator.
455
456
\kbd{g.p}: the characteristic of the finite field.
457
458
\kbd{g.f}: the dimension of the definition field over its prime field; the
459
cardinality of the definition field is thus $p^f$.
460
461
\kbd{g.mod}: the minimal polynomial (with reduced integer
462
coefficients) of the field generator.
463
464
\subsec{Complex numbers (\typ{COMPLEX})}%
465
\sidx{complex number}\kbdsidx{t_COMPLEX}
466
To enter $x+iy$, type \kbd{x + I*y}. (That's \kbd{I}, \emph{not} \kbd{i}!)
467
The letter \tet{I} stands for $\sqrt{-1}$. The ``real'' and ``imaginary''
468
parts $x$ and $y$ can be of type \typ{INT}, \typ{REAL}, \typ{INTMOD},
469
\typ{FRAC}, or \typ{PADIC}.
470
471
\subsec{$p$-adic numbers (\typ{PADIC}):}%
472
\sidx{p-adic number}\label{se:padic}\kbdsidx{t_PADIC}
473
Typing \kbd{O($p$\pow $k$)}, where $p$ is a prime and $k$ is an integer,
474
yields a $p$-adic $0$ of accuracy~$k$, representing any $p$-adic number whose
475
valuation is $\geq k$. To input a general nonzero $p$-adic number, write
476
a suitably precise rational or integer approximation and add \kbd{O($p$\pow
477
$k$)} to it. For example, you can type in the $7$-adic number
478
\bprog
479
2*7^(-1) + 3 + 4*7 + 2*7^2 + O(7^3)
480
@eprog\noindent exactly as shown, or equivalently as \kbd{905/7 + O(7\pow3)}.
481
482
Note that it is not checked whether $p$ is indeed prime but results are
483
undefined if this is not the case: you can try to work on $10$-adics if
484
you want, but disasters will happen as soon as you do something nontrivial.
485
For instance:
486
\bprog
487
? t = 2 * (1/10 + O(10^5));
488
? lift(t)
489
%2 = 2/10 \\ not reduced (invalid t_FRAC)
490
? factor(x^2-t)
491
*** at top-level: factor(x^2-%1)
492
*** ^--------------
493
*** factor: impossible inverse in Fl_inv: Mod(2, 10000).
494
@eprog\noindent Note that \kbd{O(25)} is not the same as \kbd{O(5\pow 2)};
495
you want the latter!
496
497
If $a$ is a \typ{PADIC}, the following member functions are defined:
498
499
\kbd{a.mod}: returns the modulus $p^k$.
500
501
\kbd{a.p}: returns $p$.
502
503
Note that this type is available for convenience, not for speed:
504
internally, \typ{PADIC}s are stored as $p$-adic units modulo some $p^k$.
505
Each elementary operation involves updating $p^k$ (multiplying or
506
dividing by powers of $p$) and a reduction mod $p^k$. In particular,
507
additions are slow.
508
\bprog
509
? n = 1+O(2^20); for (i=1,10^6, n++)
510
time = 841 ms.
511
? n = Mod(1,2^20); for (i=1,10^6, n++)
512
time = 441 ms.
513
? n = 1; for (i=1,10^6, n++)
514
time = 328 ms.
515
@eprog\noindent The penalty attached to maintaining $p^k$ decreases
516
steeply as $p$ increases (and updates become rare). But \typ{INTMOD}s
517
remain at least 25\% more efficient. (On the other hand, they do not allow
518
denominators!)
519
% n = 1+O(1009^2); for (i=1,10^6, n++)
520
% n = Mod(1,1009^2); for (i=1,10^6, n++)
521
522
\subsec{Quadratic numbers (\typ{QUAD})}%
523
\sidx{quadratic number}\kbdsidx{t_QUAD}
524
This type is used to work in the quadratic order of \emph{discriminant}
525
\kbd{d}, where \kbd{d} is a nonsquare integer congruent to $0$ or $1$
526
(modulo $4$). The command
527
\bprog
528
w = quadgen(d,'w)
529
@eprog\noindent
530
assigns to \kbd{w} the ``canonical'' generator for the integer basis
531
of the order of discriminant $d$, i.e.~$w=\sqrt{d}/2$ if $d\equiv 0 \mod 4$,
532
and $w=(1+\sqrt{d})/2$ if $d\equiv 1 \mod 4$ and set its name to \kbd{w}.
533
The name \kbd{'w} is used for printing and we advise to store it in a variable
534
of the same name.
535
Beware, two \typ{QUAD}s with different discriminants can be printed in the same
536
way and not be equal; however, \kbd{gp} will refuse to add or multiply them for
537
example, so use different names for different discriminants.
538
539
Since the order is $\Z + \kbd{w}\Z$, any other element can be input
540
as \kbd{z = $x$+$y$*w} for some integers $x$ and $y$. In fact, you may work in
541
its fraction field $\Q(\sqrt{d})$ and use \typ{FRAC} values for $x$ and $y$.
542
543
The member function \kbd{z.disc} retrieves the discriminant $d$; $x$ and $y$
544
are obtained via \kbd{real(z)} and \kbd{imag(z)} respectively.
545
546
\subsec{Polmods (\typ{POLMOD})}%
547
\sidx{polmod}\kbdsidx{t_POLMOD}
548
Exactly as for intmods, to enter $x \mod y$ (where $x$ and $y$ are
549
polynomials), type \kbd{Mod(x,y)}, not \kbd{x\%y}. Note that when $y$ is an
550
irreducible polynomial in one variable, polmods whose modulus is $y$ are simply
551
algebraic numbers in the finite extension defined by the polynomial $y$.
552
This allows us to work easily in \idx{number field}s, finite extensions of
553
the $p$-adic field $\Q_p$, or \idx{finite field}s.
554
555
Note that this type is available for convenience, not for speed: each
556
elementary operation involves a reduction modulo $y$.
557
If $p$ is a \typ{POLMOD}, the following member functions are defined:
558
559
\kbd{p.pol}: return a representative of the polynomial class of minimal degree.
560
561
\kbd{p.mod}: return the modulus.
562
563
\label{se:rempolmod}
564
\misctitle{Important remark}\sidx{variable (priority)}
565
Mathematically, the variables\sidx{variable} occurring in a polmod are not
566
free variables. But internally, a congruence class in $R[t]/(y)$ is
567
represented by its representative of lowest degree, which is a \typ{POL} in
568
$R[t]$, and computations occur with polynomials in the variable $t$. PARI
569
will not recognize that \kbd{Mod(y, y\pow2 + 1)} is ``the same'' as
570
\kbd{Mod(x, x\pow2 + 1)}, since \kbd{x} and \kbd{y} are different variables.
571
572
To avoid inconsistencies, polmods must use the same variable in internal
573
operations (i.e.~between polmods) and variables of lower priority for
574
external operations, typically between a polynomial and a polmod. See
575
\secref{se:priority} for a definition of ``priority'' and a discussion of
576
(PARI's idea of) multivariate polynomial arithmetic.
577
For instance:
578
\bprog
579
? Mod(x, x^2+ 1) + Mod(x, x^2 + 1)
580
%1 = Mod(2*x, x^2 + 1) \\@com $2i$ (or $-2i$), with $i^2=-1$
581
? x + Mod(y, y^2 + 1)
582
%2 = x + Mod(y, y^2 + 1) \\@com in $\Q(i)[x]$
583
? y + Mod(x, x^2 + 1)
584
%3 = Mod(x + y, x^2 + 1) \\@com in $\Q(y)[i]$
585
@eprog\noindent
586
The first two are straightforward, but the last one may not be what you
587
want: \kbd{y} is treated here as a numerical parameter, not as a polynomial
588
variable.
589
590
If the main variables are the same, it is allowed to mix \typ{POL} and
591
\typ{POLMOD}s. The result is the expected \typ{POLMOD}. For instance
592
\bprog
593
? x + Mod(x, x^2 + 1)
594
%1 = Mod(2*x, x^2 + 1)
595
@eprog
596
597
\subsec{Polynomials (\typ{POL})}%
598
\sidx{polynomial}\label{se:pol}\kbdsidx{t_POL}
599
Type the polynomial in a natural way, not forgetting to put a ``$*$'' between
600
a coefficient and a formal variable;
601
\bprog
602
? 1 + 2*x + 3*x^2
603
%1 = 3*x^2 + 2*x + 1
604
@eprog\noindent
605
This assumes that \kbd{x} is still a ''free variable''.
606
\bprog
607
? x = 1; 1 + 2*x + 3*x^2
608
%2 = 6
609
@eprog\noindent
610
generates an integer, not a polynomial! It is good practice to never assign
611
values to polynomial variables to avoid the above problem, but a foolproof
612
construction is available using \kbd{'x} instead of~\kbd{x}: \kbd{'x}
613
is a constant evaluating to the free variable with name \kbd{x},
614
independently of the current value of~\kbd{x}.
615
\bprog
616
? x = 1; 1 + 2*'x + 3*'x^2
617
%3 = 1 + 2*x + 3*x^2
618
? x = 'x; 1 + 2*x + 3*x^2
619
%4 = 1 + 2*x + 3*x^2
620
@eprog\noindent
621
You may also use the functions \kbd{Pol} or \kbd{Polrev}:
622
\bprog
623
? Pol([1,2,3]) \\@com \kbd{Pol} creates a polynomial in \kbd{x} by default
624
%1 = x^2 + 2*x + 3
625
? Polrev([1,2,3])
626
%2 = 3*x^2 + 2*x + 1
627
? Pol([1,2,3], 'y) \\@com we use \kbd{'y}, safer than \kbd{y}
628
%3 = y^2 + 2*y + 3
629
@eprog\noindent The latter two are much more efficient constructors than an
630
explicit summation (the latter is quadratic in the degree, the former linear):
631
\bprog
632
? for (i=1, 10^4, Polrev( vector(100, i,i) ) )
633
time = 124ms
634
635
? for (i=1, 10^4, sum(i = 1, 100, (i+1) * 'x^i) )
636
time = 3,985ms
637
@eprog
638
639
Polynomials are always printed as \emph{univariate} polynomials over a
640
commutative base ring, with monomials sorted by decreasing degree:
641
\bprog
642
? (x+y+1)^2
643
%1 = x^2 + (2*y + 2)*x + (y^2 + 2*y + 1)
644
@eprog\noindent
645
(Univariate polynomial in \kbd{x} whose coefficients are polynomials in
646
\kbd{y}.) See \secref{se:varsymb} for valid variable names, and a discussion
647
of multivariate polynomial rings. Polynomials over noncommutative rings
648
are not supported.
649
650
\subsec{Power series (\typ{SER})}%
651
\sidx{power series}\kbdsidx{t_SER}\label{se:series}
652
Typing \kbd{O(X\pow $k$)}, where $k$ is an integer, yields an $X$-adic $0$ of
653
accuracy~$k$, representing any power series in \kbd{X} whose valuation is
654
$\geq k$. Of course, \kbd{X} can be replaced by any other variable name! To
655
input a general nonzero power series, type in a polynomial or rational
656
function (in \kbd{X}, say), and add \kbd{O(X\pow $k$)} to it. The discussion
657
in the \typ{POL} section about variables remains valid; a constructor
658
\tet{Ser} replaces \tet{Pol} and \tet{Polrev}. Power series over
659
noncommutative rings are not supported.
660
661
\misctitle{Caveat} Power series with inexact coefficients sometimes have a
662
nonintuitive behavior: if $k$ significant terms are requested, an inexact
663
zero is counted as significant, even if it is the coefficient of lowest
664
degree. This means that useful higher order terms may be disregarded.
665
666
If a series with a zero leading coefficient must be inverted, then as a
667
desperation measure that coefficient is discarded, and a warning is issued:
668
\bprog
669
? C = 0. + y + O(y^2);
670
? 1/C
671
*** _/_: Warning: normalizing a series with 0 leading term.
672
%2 = y^-1 + O(1)
673
@eprog\noindent
674
The last output could be construed as a bug since it is a priori impossible
675
to deduce such a result from the input ($0.$ represents any sufficiently
676
small real number). But it was thought more useful to try and go on with an
677
approximate computation than to raise an early exception.
678
679
If the series precision is insufficient, errors may occur (mostly division
680
by $0$), which could have been avoided by a better global understanding of
681
the computation:
682
\bprog
683
? A = 1/(y + 0.); B = 1. + O(y);
684
? B * denominator(A)
685
%2 = 0.E-28 + O(y)
686
? A/B
687
*** _/_: Warning: normalizing a series with 0 leading term.
688
%3 = 1.000000000000000000000000000*y^-1 + O(1)
689
? A*B
690
*** _*_: Warning: normalizing a series with 0 leading term.
691
%4 = 1.000000000000000000000000000*y^-1 + O(1)
692
@eprog
693
694
\subsec{Rational functions (\typ{RFRAC})}%
695
\sidx{rational function}\kbdsidx{t_RFRAC}
696
As for fractions, all rational functions are automatically reduced to lowest
697
terms. All that was said about fractions in \secref{se:FRAC} remains valid
698
here.
699
700
\subsec{Binary quadratic forms (\typ{QFB})}%
701
\sidx{binary quadratic form}\kbdsidx{t_QFB}
702
These are input using the function \kbd{Qfb}. For example, both \kbd{Qfb(1,2,3)}
703
and \kbd{Qfb([1,2,3])} create the binary form $q = x^2+2xy+3y^2$. It is
704
imaginary since its discriminant $2^2 - 4\times 3 = -8$ is negative. Although
705
imaginary forms could be positive or negative definite, only positive
706
definite forms are implemented.
707
708
The discriminant can be retrieved via \kbd{q.disc}. The individual
709
components are obtained via either of
710
\bprog
711
[a,b,c] = Vec(q);
712
713
a = component(q,1);
714
b = component(q,2);
715
c = component(q,3);
716
@eprog
717
718
See also the function
719
\kbd{qfbprimeform} which creates a prime form of given discriminant.
720
721
\subsec{Row and column vectors (\typ{VEC} and \typ{COL})}%
722
\sidx{row vector}\sidx{column vector}\kbdsidx{t_VEC}\kbdsidx{t_COL}
723
To enter a row vector, type the components separated by commas ``\kbd{,}'',
724
and enclosed between brackets ``\kbd{[}$\,$'' and ``$\,$\kbd{]}'',
725
e.g.~\kbd{[1,2,3]}. To enter a column vector, type the vector horizontally,
726
and add a tilde ``\til'' to transpose. \kbd{[ ]} yields the empty (row)
727
vector. The function \tet{Vec} can be used to transform any object into a
728
vector (see Chapter~3). The construction $[i..j]$, where $i \leq j$ are two
729
integers returns the vector $[i, i+1,\dots, j-1, j]$
730
\bprog
731
? [1,2,3]
732
%1 = [1, 2, 3]
733
? [-2..3]
734
%2 = [-2, -1, 0, 1, 2, 3]
735
@eprog
736
737
Let the variable $v$ contain a (row or column) vector:
738
739
\item \kbd{v[m]} refers to its $m$-th entry; you can assign any value to
740
\kbd{v[m]}, i.e.~write something like $v[m]=\var{expr}$.
741
742
\item \kbd{v[i..j]}, where $i \leq j$, returns the vector slice containing
743
elements $v[i],\dots, v[j]$; you can \emph{not} assign a result to
744
\kbd{v[i..j]}.
745
746
\item \kbd{v[\pow i]} returns the vector whose $i$-th entry has been removed;
747
you can \emph{not} assign a result to \kbd{v[\pow i]}.
748
749
\noindent In the last two constructions \kbd{v[i..j]} and \kbd{v[\pow i]},
750
$i$ and $j$ are allowed to be negative integers, in which case, we start
751
counting from the end of the vector: e.g., $-1$ is the index of the last
752
element.
753
\bprog
754
? v = [1,2,3,4];
755
? v[2..4]
756
%2 = [2, 3, 4]
757
? v[^3]
758
%3 = [1, 2, 4]
759
? v[^-1]
760
%3 = [1, 2, 3]
761
? v[-3..-1]
762
%4 = [2, 3, 4]
763
@eprog
764
765
\misctitle{Remark} \tet{vector} is the standard constructor for row vectors
766
whose $i$-th entry is given by a simple function of $i$; \tet{vectorv}
767
is similar for column vectors:
768
\bprog
769
? vector(10, i, i^2+1)
770
%1 = [2, 5, 10, 17, 26, 37, 50, 65, 82, 101]
771
@eprog
772
The functions \tet{Vec} and \tet{Col} convert objects to row and column
773
vectors respectively (as well as \tet{Vecrev} and \tet{Colrev}, which revert
774
the indexing):
775
\bprog
776
? T = poltchebi(5) \\ 5-th Chebyshev polynomial
777
%1 = 16*x^5 - 20*x^3 + 5*x
778
? Vec(T)
779
%2 = [16, 0, -20, 0, 5, 0] \\ coefficients of T
780
? Vecrev(T)
781
%3 = [0, 5, 0, -20, 0, 16] \\ ... in reverse order
782
@eprog
783
784
\misctitle{Remark} For $v$ a \typ{VEC}, \typ{COL}, \typ{VECSMALL},
785
\typ{LIST} or \typ{MAT}, the alternative set-notations
786
\bprog
787
[g(x) | x <- v, f(x)]
788
[x | x <- v, f(x)]
789
[g(x) | x <- v]
790
@eprog\noindent
791
are available as shortcuts for
792
\bprog
793
apply(g, select(f, Vec(v)))
794
select(f, Vec(v))
795
apply(g, Vec(v))
796
@eprog\noindent respectively, and may serve as \typ{VEC} constructors:
797
\bprog
798
? [ p | p <- primes(10), isprime(p+2) ]
799
%2 = [3, 5, 11, 17, 29]
800
@eprog\noindent returns the primes $p$ (among the first 10 primes) such
801
that $(p, p+2)$ is a twin pair;
802
\bprog
803
? [ p^2 | p <- primes(10), p % 4 == 1 ]
804
%1 = [25, 169, 289, 841]
805
@eprog\noindent returns the squares of the primes congruent to $1$ modulo $4$,
806
where $p$ runs among the first 10 primes.
807
808
\subsec{Matrices (\typ{MAT})}%
809
\sidx{matrix}\kbdsidx{t_MAT}
810
To enter a matrix, type the components row by row, the components being
811
separated by commas ``\kbd{,}'', the rows by semicolons ``\kbd{;}'', and
812
everything enclosed in brackets ``\kbd{[}$\,$'' and ``$\,$\kbd{]}'', e.g.
813
\kbd{[x,y; z,t; u,v]}. \kbd{[;]} yields an empty ($0 \times 0$) matrix. The
814
function \tet{Mat} transforms any object into a matrix, and \tet{matrix}
815
creates matrices whose $(i,j)$-th entry is described by a function $f(i,j)$:
816
\bprog
817
? Mat(1)
818
%1 =
819
[1]
820
? matrix(2,2, i,j, 2*i+j)
821
%2 =
822
[3 4]
823
824
[5 6]
825
@eprog\noindent Matrix multiplication assumes that the base ring containing
826
the matrix entries is commutative.
827
828
\noindent Let the variable $M$ contain a matrix, and let $i,j,k,l$ denote
829
four
830
integers:
831
832
\item \kbd{M[i,j]} refers to its $(i,j)$-th entry; you can assign any result
833
to \kbd{M[i,j]}.
834
835
\item \kbd{M[i,]} refers to its $i$-th row; you can assign a \typ{VEC}
836
of the right dimension to \kbd{M[i,]}.
837
838
\item \kbd{M[,j]} refers to its $j$-th column; you can assign a \typ{COL}
839
of the right dimension to \kbd{M[,j]}.
840
841
\noindent \emph{But} \kbd{M[i]} is meaningless and triggers an error. The
842
``range'' $i..j$ and ``caret'' \kbd{\pow}$c$ notations are available as for
843
vectors; you can not \emph{assign} to any of these:
844
845
\item \kbd{M[i..j, k..l]}, $i\leq j$, $k\leq l$, returns the submatrix built
846
from the rows $i$ to $j$ and columns $k$ to $l$ of $M$.
847
848
\item \kbd{M[i..j,]} returns the submatrix built from the rows $i$ to
849
$j$ of $M$.
850
851
\item \kbd{M[,i..j]} returns the submatrix built from the columns $i$ to
852
$j$ of $M$.
853
854
\item \kbd{M[i..j, \pow k]}, $i\leq j$, returns the submatrix built
855
from the rows $i$ to $j$ and column $k$ removed.
856
857
\item \kbd{M[\pow k,]} returns the submatrix with row $k$ removed.
858
859
\item \kbd{M[,\pow k]} returns the submatrix with column $k$ removed.
860
861
\noindent Finally,
862
863
\item \kbd{M[i..j, k]} returns the \typ{COL} built from the $k$-th column
864
(entries $i$ to $j$).
865
866
\item \kbd{M[\pow i, k]} returns the \typ{COL} built from the $k$-th column
867
(entry $i$ removed).
868
869
\item \kbd{M[k, i..j]} returns the \typ{VEC} built from the $k$-th row
870
(entries $i$ to $j$).
871
872
\item \kbd{M[k, \pow i]} returns the \typ{VEC} built from the $k$-th row
873
(entry $i$ removed).
874
875
\bprog
876
? M = [1,2,3;4,5,6;7,8,9];
877
? M[1..2, 2..3]
878
%2 =
879
[2 3]
880
881
[5 6]
882
? M[1..2,]
883
%3 =
884
[1 2 3]
885
886
[4 5 6]
887
? M[,2..3]
888
%4 =
889
[2 3]
890
891
[5 6]
892
893
[8 9]
894
@eprog
895
896
All this is recursive, so if \kbd{M} is a matrix of matrices of \dots, an
897
expression such as \kbd{M[1,1][,3][4] = 1} is perfectly valid (and actually
898
identical to \kbd{M[1,1][4,3] = 1}), assuming that all matrices along the way
899
have compatible dimensions.
900
901
\misctitle{Technical note (design flaw)} Matrices are internally represented
902
as a vector of columns. All matrices with $0$ columns are thus represented
903
by the same object (internally, an empty vector), and there is no way to
904
distinguish between them. Thus it is not possible to create or represent
905
matrices with zero columns and an actual nonzero number of rows.
906
The empty matrix \kbd{[;]} is handled as though it had an arbitrary number of
907
rows, exactly as many as needed for the current computation to make sense:
908
\bprog
909
? [1,2,3; 4,5,6] * [;]
910
%1 = [;]
911
@eprog\noindent
912
The empty matrix on the first line is understood as a $3\times 0$ matrix, and
913
the result as a $2\times 0$ matrix. On the other hand, it is possible to
914
create matrices with a given positive number of columns, each of which has
915
zero rows, e.g.~using \kbd{Mat} as above or using the \kbd{matrix} function.
916
917
Note that although the internal representation is essentially the same, a row
918
vector of column vectors is \emph{not} a matrix; for example, multiplication
919
will not work in the same way. It is easy to go from one representation to
920
the other using \tet{Vec} / \tet{Mat}, though:
921
\bprog
922
? [1,2,3;4,5,6]
923
%1 =
924
[1 2 3]
925
926
[4 5 6]
927
928
? Vec(%)
929
%2 = [[1, 4]~, [2, 5]~, [3, 6]~]
930
? Mat(%)
931
%3 =
932
[1 2 3]
933
934
[4 5 6]
935
@eprog
936
937
\subsec{Lists (\typ{LIST})}%
938
\sidx{list}\kbdsidx{t_LIST}
939
Lists can be input directly, as in \kbd{List([1,2,3,4])}; but in most cases,
940
one creates an empty list, then appends elements using \kbd{listput}:
941
\bprog
942
? L = List(); listput(~L,1); listput(~L,2);
943
? L
944
%2 = List([1, 2])
945
@eprog\noindent
946
Note the \kbd{\til L}: this means that the function is called with a
947
\emph{reference} to \kbd{L} and changes \kbd{L} in place. Elements can be
948
accessed directly as with the vector types described above.
949
950
\subsec{Strings (\typ{STR})}%
951
\sidx{string}\sidx{character string}\kbdsidx{t_STR}
952
To enter a string, enclose it between double quotes \kbd{"}, as in:
953
\kbd{"this is a string"}. The function \kbd{Str} can be used to transform any
954
object into a string.
955
956
\subsec{Small vectors (\typ{VECSMALL})}%
957
\kbdsidx{t_VECSMALL}
958
This type codes in an efficient way vectors containing only small integers,
959
such as permutations. Most \kbd{gp} functions will refuse to operate on these
960
objects, notable exceptions being \kbd{vecsort} and conversion functions
961
such as \kbd{VEC}, but you can retrieve entries and assign to them as
962
for ordinary vectors. You can also convert back and forth between
963
\typ{VECSMALL} and \typ{VEC} objects using \tet{Vec} and \tet{Vecsmall}.
964
\bprog
965
? v = Vecsmall([2, 4, 6])
966
%1 = Vecsmall([1, 2, 3])
967
? v[1]
968
%2 = 2
969
? v[1] = 3; v
970
%3 = Vecsmall([3, 2, 3])
971
? v[2..3]
972
%4 = Vecsmall([2, 3])
973
? v[^2]
974
%5 = Vecsmall([3, 3])
975
? Vec(v)
976
%6 = [3, 2, 3]
977
@eprog\noindent
978
Allowed entries for a \typ{VECMALL} are signed integer $x$ such that $|x| <
979
2^{31}$ on a 32-bit architecture, resp.~$|x| < 2^{63}$ on a 64-bit
980
architecture Assigning a larger integer to a \typ{VECSMALL} entry triggers an
981
exception:
982
\bprog
983
? v[1] = 2^63
984
*** at top-level: v[1]=2^63
985
*** ^----------
986
*** incorrect type in t_VECSMALL assignment (t_INT).
987
@eprog
988
989
\subsec{Functions (\typ{CLOSURE})}%
990
\kbdsidx{t_CLOSURE}
991
We will explain this at length in \secref{se:user_defined}. For the time
992
being, suffice it to say that functions can be assigned to variables, as any
993
other object, and the following equivalent basic forms are available to
994
create new ones
995
\bprog
996
f = (x,y) -> x^2 + y^2
997
998
f(x,y) = x^2 + y^2
999
@eprog
1000
1001
\subsec{Error contexts (\typ{ERROR})}%
1002
\kbdsidx{t_ERROR}
1003
An object of this type is created whenever an error occurs: it contains some
1004
information about the error and the error context. Usually, an appropriate
1005
error is printed immediately, the computation is aborted, and GP enters the
1006
``break loop'':
1007
\bprog
1008
? 1/0; 1 + 1
1009
*** at top-level: 1/0;1+1
1010
*** ^------
1011
*** _/_: division by a noninvertible object
1012
*** Break loop: type 'break' to go back to the GP prompt
1013
1014
@eprog\noindent Here the computation is aborted as soon as we try to evaluate
1015
$1/0$, and $1 + 1$ is never executed. Exceptions can be trapped
1016
using \tet{iferr}, however: we can evaluate some expression and either recover
1017
an ordinary result (no error occurred), or an exception (an error did occur).
1018
\bprog
1019
? i = Mod(6,12); iferr(1/i, E, print(E)); 1 + 1
1020
error("impossible inverse modulo: Mod(6, 12).")
1021
%1 = 2
1022
@eprog\noindent One can ignore the exception, print it as above, or extract
1023
non trivial information from the error context:
1024
\bprog
1025
? i = Mod(6,12); iferr(1/i, E, print(component(E,1)));
1026
Mod(6, 12)
1027
@eprog\noindent We can also rethrow the exception: \kbd{error(E)}.
1028
1029
\subsec{Infinity (\typ{INFINITY})}%
1030
\kbdsidx{t_INFINITY}
1031
1032
There are only two objects of this type \kbd{+oo} and \kbd{-oo}, representing
1033
$\pm\infty$. This type only contain only two elements \kbd{oo} and \kbd{-oo},
1034
They are used in functions sur as \kbd{intnum} or \kbd{polrootsreal}, to
1035
encode infinite real intervals. These objects can only be negated and
1036
compared to real numbers (\typ{INT}, \typ{REAL}, \typ{FRAC}), but not
1037
included in any computation, i.e.~\kbd{1+oo} is an error, not kbd{oo} again.
1038
1039
\section{GP operators}\label{se:operators}
1040
1041
\noindent Loosely speaking, an \idx{operator} is a function, usually
1042
attached to basic arithmetic operations, whose name contains only
1043
nonalphanumeric characters. For instance \kbd{+} or \kbd{-}, but also
1044
\kbd{=} or \kbd{+=}, or even \kbd{[ ]} (the selection operator). As all
1045
functions, operators take arguments, and return a value; \emph{assignment}
1046
operators also have side effects: besides returning a value, they change the
1047
value of some variable.
1048
1049
Each operator has a fixed and unchangeable priority, which means that, in
1050
a given expression, the operations with the highest priority is performed
1051
first. Unless mentioned otherwise, operators at the same priority level are
1052
left-associative (performed from left to right), unless they are assignments,
1053
in which case they are right-associative. Anything enclosed between
1054
parenthesis is considered a complete subexpression, and is resolved
1055
recursively, independently of the surrounding context. For instance,
1056
\bprog
1057
a + b + c --> (a + b) + c \\@com left-associative
1058
a = b = c --> a = (b = c) \\@com right-associative
1059
@eprog\noindent
1060
Assuming that \var{op}$_1$, \var{op}$_2$, \var{op}$_3$ are
1061
binary operators with increasing priorities (think of \kbd{+},
1062
\kbd{*}, \kbd{\pow}),
1063
$$ x~\var{op}_1~y~\var{op}_2~z~\var{op}_2~x~\var{op}_3~y $$ is
1064
equivalent to $$ x~\var{op}_1~((y~\var{op}_2~z)~\var{op}_2~
1065
(x~\var{op}_3~y)).$$
1066
1067
GP contains many different operators, either unary (having only
1068
one argument) or binary, plus a few special selection operators. Unary
1069
operators are defined as either \emph{prefix} or \emph{postfix}, meaning
1070
that they respectively precede (\var{op}~$x$) and follow ($x$~\var{op}) their
1071
single argument. Some symbols are syntactically correct in both positions,
1072
like \kbd{!}, but then represent different operators: the \kbd{!} symbol
1073
represents the negation and factorial operators when in prefix and postfix
1074
position respectively. Binary operators all use the (infix) syntax
1075
$x$~\var{op}~$y$.
1076
1077
Most operators are standard (\kbd{+}, \kbd{\%}, \kbd{=}), some are
1078
borrowed from the C language (\kbd{++}, \kbd{<<}), and a few are
1079
specific to GP (\kbd{\bs}, \kbd{\#}). Beware that some GP operators differ
1080
slightly from their C counterparts. For instance, GP's postfix \kbd{++}
1081
returns the \emph{new} value, like the prefix \kbd{++} of~C, and the binary
1082
shifts \kbd{<<}, \kbd{>>} have a priority which is different from (higher
1083
than) that of their C counterparts. When in doubt, just surround everything
1084
by parentheses; besides, your code will be more legible.
1085
1086
\noindent Here is the list of available operators, ordered by decreasing
1087
\idx{priority}, binary and left-associative unless mentioned otherwise. An
1088
expression is an \tev{lvalue} if something can be assigned to it. (The name
1089
comes from left-value, to the left of a \kbd{=} operator; e.g.
1090
\kbd{x}, or \kbd{v[1]} are lvalues, but \kbd{x + 1} is not.)
1091
1092
\def\point#1{\noindent\item #1\hfill\break\indent\strut}
1093
\point{Priority 14}
1094
%
1095
\kbd{:} as in \kbd{x:small}, is used to indicate to the GP2C compiler that the
1096
variable on the left-hand side always contains objects of the type specified
1097
on the right hand-side (here, a small integer) in order to produce more
1098
efficient or more readable C code. This is ignored by GP.
1099
1100
%
1101
\point{Priority 13}
1102
\kbd{( )} is the function call operator. If $f$ is a closure and \var{args}
1103
is a comma-separated list of arguments (possibly empty),
1104
$f\kbd{(\var{args})}$ evaluates $f$ on those arguments.
1105
1106
\point{Priority 12}
1107
%
1108
\kbd{++} and \kbd{--} (unary, postfix): if $x$ is an \tet{lvalue},
1109
\kbd{$x$++} assigns the value $x+1$ to $x$, then returns the new value of
1110
$x$. This corresponds to the C statement \kbd{++$x$}: there is no prefix
1111
\kbd{++} operator in GP. \kbd{$x$--} does the same with $x-1$. These
1112
operators are not associative, i.e. \kbd{x++++} is invalid, since
1113
\kbd{x++} is not an lvalue.
1114
1115
\point{Priority 11}
1116
%
1117
\kbd{.}\var{member} (unary, postfix): \kbd{$x$.\var{member}} extracts
1118
\var{member} from structure $x$ (see~\secref{se:member}).
1119
1120
\kbd{[ ]} is the selection operator. \kbd{$x$[$i$]} returns the $i$-th
1121
component of vector $x$; \kbd{$x$[$i$,$j$]}, \kbd{$x$[,$j$]} and
1122
\kbd{$x$[$i$,]} respectively return the entry of coordinates $(i,j)$, the
1123
$j$-th column, and the $i$-th row of matrix $x$. If the assignment operator
1124
(\kbd{=}) immediately follows a sequence of selections, it assigns its right
1125
hand side to the selected component. E.g \kbd{x[1][1] = 0} is valid; but
1126
beware that \kbd{(x[1])[1] = 0} is not (because the parentheses force the
1127
complete evaluation of \kbd{x[1]}, and the result is not modifiable).
1128
1129
\point{Priority 10}
1130
%
1131
\kbd{'} (unary, postfix): derivative with respect to the main variable.
1132
If $f$ is a function (\typ{CLOSURE}), $f'$ is allowed and defines a new
1133
function, which will perform \idx{numerical derivation} when evaluated
1134
at a scalar $x$; this is defined as $(f(x+\varepsilon) - f(x-\varepsilon)) /
1135
2\varepsilon$ for a suitably small epsilon depending on current precision.
1136
\bprog
1137
? (x^2 + y*x + y^2)' \\@com derive with respect to main variable \kbd{x}
1138
%1 = 2*x + y
1139
? SIN = cos'
1140
%2 = cos'
1141
? SIN(Pi/6) \\@com numerical derivation
1142
%3 = -0.5000000000000000000000000000
1143
? cos'(Pi/6) \\@com works directly: no need for intermediate \kbd{SIN}
1144
%4 = -0.5000000000000000000000000000
1145
@eprog
1146
1147
\strut\kbd{\til} (unary, postfix): vector/matrix transpose.
1148
1149
\kbd{!} (unary, postfix): factorial. $x\kbd{!}=x(x-1)\cdots 1$.
1150
1151
\kbd{!} (unary, prefix): logical \var{not}. \kbd{!$x$} returns $1$ if $x$ is
1152
equal to $0$ (specifically, if \kbd{gequal0($x$)==1}), and $0$ otherwise.
1153
1154
\point{Priority 9}
1155
%
1156
\kbd{\#} (unary, prefix): cardinality; \kbd{\#$x$} returns \kbd{length($x$)}.
1157
1158
\point{Priority 8}
1159
%
1160
\kbd{\pow}: powering. This operator is right associative:
1161
\kbd{2 \pow 3\pow 4} is understood as \kbd{2 \pow (3\pow 4)}.
1162
1163
\point{Priority 7}
1164
%
1165
\kbd{+}, \kbd{-} (unary, prefix): \kbd{-} toggles the sign of its argument,
1166
\kbd{+} has no effect whatsoever.
1167
1168
\point{Priority 6}
1169
%
1170
\kbd{*}: multiplication.
1171
1172
\kbd{/}: exact division (\kbd{3/2} yields $3/2$, not $1.5$).
1173
1174
\kbd{\bs}, \kbd{\%}: Euclidean quotient and remainder, i.e.~if $x =
1175
qy + r$, then $\kbd{x \b{ } y} = q$, $\kbd{x\%y} = r$. If $x$ and $y$
1176
are scalars, then $q$ is an integer and $r$ satisfies $0\le r < |y|$; if $x$
1177
and $y$ are polynomials, then $q$ and $r$ are polynomials such that $\deg r<
1178
\deg y$ and the leading terms of $r$ and $x$ have the same sign.
1179
1180
\kbd{\bs/}: rounded Euclidean quotient for integers (rounded towards
1181
$+\infty$ when the exact quotient would be a half-integer).
1182
1183
\kbd{<<}, \kbd{>>}: left and right binary shift. By definition,
1184
\kbd{x<<n}$~=~x * 2^n$ if $n>0$, and $\kbd{truncate}(x 2^{-n})$ otherwise.
1185
Right shift is defined by \kbd{x>>n}$~=~$\kbd{x<<(-n)}.
1186
1187
\point{Priority 5}
1188
%
1189
\kbd{+}, \kbd{-}: addition/subtraction.
1190
1191
\point{Priority 4}
1192
%
1193
\kbd{<}, \kbd{>}, \kbd{<=}, \kbd{>=}: the usual comparison operators,
1194
returning 1 for \kbd{true} and 0 for \kbd{false}. For instance,
1195
\kbd{x<=1} returns $1$ if $x\le 1$ and $0$ otherwise.
1196
1197
\kbd{<>}, \kbd{!=}: test for (exact) inequality.
1198
1199
\kbd{==}: test for (exact) equality.
1200
1201
\kbd{===}: test whether two objects are identical component-wise. This is
1202
stricter than \kbd{==}: for instance, the integer 0, a 0 polynomial or a
1203
vector with 0 entries, are all tested equal by \kbd{==}, but they are not
1204
identical.
1205
1206
\point{Priority 3}
1207
%
1208
\kbd{\&\&}: logical \var{and}.
1209
1210
\kbd{||}: logical (inclusive) \var{or}. Any sequence of logical
1211
\var{or} and \var{and} operations is evaluated from left to right,
1212
and aborted as soon as the final truth value is known. Thus, for instance,
1213
\bprog
1214
x == 0 || test(1/x)
1215
@eprog\noindent
1216
will never produce an error since \kbd{test(1/x)} is not even evaluated
1217
when the first test is true (hence the final truth value is true). Similarly
1218
\bprog
1219
type(p) == "t_INT" && isprime(p)
1220
@eprog\noindent
1221
does not evaluate \kbd{isprime(p)} if \kbd{p} is not an integer.
1222
1223
\point{Priority 2}
1224
%
1225
\kbd{=} (assignment, \var{lvalue} \kbd{=} \var{expr}). The result of
1226
\kbd{x~=~$y$} is the value of the expression~$y$, which is also assigned to
1227
the variable~\kbd{x}. This assignment operator is right-associative. This is
1228
\emph{not} the equality test operator; a statement like \kbd{x~=~1} is always
1229
true (i.e.~nonzero), and sets \kbd{x} to~1; the equality test would be
1230
\kbd{x == 1}. The right hand side of the assignment operator is evaluated
1231
before the left hand side.
1232
1233
It is crucial that the left hand-side be an \var{lvalue} there, it avoids
1234
ambiguities in expressions like \kbd{1 + x = 1}. The latter evaluates as
1235
\kbd{1 + (x = 1)}, not as \kbd{(1 + x) = 1}, even though the priority of
1236
\kbd{=} is lower than the priority of \kbd{+}: \kbd{1 + x} is not an lvalue.
1237
1238
If the expression cannot be parsed in a way where the left hand side is an
1239
lvalue, raise an error.
1240
\bprog
1241
? x + 1 = 1
1242
*** syntax error, unexpected '=', expecting $end or ';': x+1=1
1243
*** ^--
1244
@eprog\noindent
1245
Assignment to all variables is a deep copy: after $x = y$, modifying a
1246
component of $y$ will \emph{not} change $x$. To globals it is a full copy to
1247
the heap. Space used by local objects in local variables is released when
1248
they go out of scope or when the value changes in local scope. Assigning a
1249
value to a vector or matrix entry allocates room for that entry only (on the
1250
heap).
1251
1252
\leavevmode
1253
\kbd{\var{op}=}, where \var{op} is any binary operator
1254
among \kbd{+}, \kbd{-}, \kbd{*}, \kbd{\%}, \kbd{/}, \kbd{\bs}, \kbd{\bs/},
1255
\kbd{<<}, or
1256
\kbd{>>} (composed assignment \var{lvalue} \var{op}\kbd{=} \var{expr}).
1257
The expression \kbd{x~\var{op}=~$y$} assigns $(\kbd{x}~\var{op}~y)$
1258
to~\kbd{x}, and returns the new value of~\kbd{x}. The result is \emph{not}
1259
an \tev{lvalue}; thus
1260
\bprog
1261
(x += 2) = 3
1262
@eprog\noindent
1263
is invalid. These assignment operators are right-associative:
1264
\bprog
1265
? x = 'x; x += x *= 2
1266
%1 = 3*x
1267
@eprog
1268
1269
\point{Priority 1}
1270
\kbd{->} (function definition): \kbd{(\var{vars})->\var{expr}} returns a
1271
function object, of type \typ{CLOSURE}.
1272
1273
\misctitle{Remark} Use the \var{op}\kbd{=} operators as often as possible
1274
since they make complex assignments more legible. Compare
1275
\bprog
1276
v[i+j-1] = v[i+j-1] + 1 --> v[i+j-1]++
1277
1278
M[i,i+j] = M[i,i+j] * 2 --> M[i,i+j] *= 2
1279
@eprog
1280
1281
\misctitle{Remark about efficiency} the operators \kbd{++}
1282
and \kbd{--} are usually a little more efficient than their expended
1283
counterpart:
1284
\bprog
1285
? N = 10^7;
1286
? i = 0; for(k = 1, N, i=i+1)
1287
time = 949 ms.
1288
? i = 0; for(k = 1, N, i++)
1289
time = 933 ms.
1290
@eprog\noindent On the other hand, this is not the case for the
1291
\var{op}\kbd{=} operators which may even be a little less efficient:
1292
\bprog
1293
? i = 0; for(k = 1, N, i=i+10)
1294
time = 949 ms.
1295
? i = 0; for(k = 1, N, i+=10)
1296
time = 1,064 ms.
1297
@eprog
1298
1299
\section{Variables and symbolic expressions}\sidx{variable}\label{se:varsymb}
1300
In this section we use \emph{variable} in the standard mathematical
1301
sense, symbols representing algebraically independent elements used to build
1302
rings of polynomials and power series, and explain the all-important concept
1303
of \emph{variable priority}. In the next \secref{se:scope}, we shall no
1304
longer consider only free variables, but adopt the viewpoint of computer
1305
programming and assign values to these symbols: (bound) variables are names
1306
attached to values in a given scope.
1307
1308
\subsec{Variable names}\label{se:varname} A valid name starts with a letter,
1309
followed by any number of keyword characters: \kbd{\_} or alphanumeric
1310
characters ([\kbd{A-Za-z0-9}]). As a rule, the built-in function names are
1311
reserved and cannot be used; see the list with \b{c}, including the constants
1312
\kbd{Pi}, \kbd{Euler}, \kbd{Catalan}, $\kbd{I}=\sqrt{-1}$ and $\kbd{oo} =
1313
\infty$. Beware in particular of \kbd{gamma}, \kbd{omega}, \kbd{theta},
1314
\kbd{sum} or \kbd{O}, none of which are free to use. (We shall see
1315
in~\secref{se:scope} how this rule can be circumvented. It \emph{is} possible
1316
to name a \var{lexical} variable \kbd{gamma}.)
1317
1318
GP names are case sensitive. For instance, the symbol \kbd{i} is perfectly
1319
safe to use, and will not be mistaken for $\kbd{I} = \sqrt{-1}$; analogously,
1320
\kbd{o} is not synonymous to \kbd{O}.
1321
1322
In GP you can use up to 16383 variable names (up to 65535 on 64-bit
1323
machines). If you ever need thousands of variables and this becomes a serious
1324
limitation, you should probably be using vectors instead: e.g. instead of
1325
variables \kbd{X1}, \kbd{X2}, \kbd{X3}, \dots, you might equally well store
1326
their values in \kbd{X[1]}, \kbd{X[2]}, \kbd{X[3]}, \dots
1327
1328
\subsec{Variables and polynomials}\sidx{free variable}
1329
The quote operator \kbd{'t} registers a new \emph{free variable} with the
1330
interpreter, which will be written as \kbd{t}, and evaluates to a monomial
1331
of degree $1$ in the said variable.
1332
1333
\misctitle{Caveat}
1334
For reasons of backward compatibility, there is no such thing as an
1335
``unbound'' (uninitialized) variable in GP. If you use a valid variable name
1336
in an expression, \kbd{t} say, for the first time \emph{before} assigning a
1337
value into it, it is interpreted as \kbd{'t} rather than raising an
1338
exception. One should not rely on this feature in serious programs, which
1339
would otherwise break if some unexpected assignment (e.g. \kbd{t = 1})
1340
occurs: use \kbd{'t} directly or \kbd{t = 't} first, then \kbd{t}.
1341
A statement like \kbd{t = 't} in effect restores \kbd{t} as a free variable.
1342
%
1343
\bprog
1344
? t = 't; t^2 + 1
1345
%1 = t^2 + 1
1346
? t = 2; t^2 + 1
1347
%2 = 5
1348
? %1
1349
%3 = t^2 + 1
1350
? eval(%1)
1351
%4 = 5
1352
@eprog\noindent
1353
In the above, we initialize \kbd{t} to a monomial, then bind it to $2$.
1354
Assigning a value to a polynomial variable does not affect previous
1355
expressions involving it; to take into account the new variable's value, one
1356
must force a new evaluation, using the function \kbd{eval} (see
1357
\secref{se:eval}).
1358
1359
\misctitle{Caveat2}
1360
The use of an explicit quote operator avoids the following kind of problems:
1361
\bprog
1362
? t = 't; p = t^2 + 1; subst(p, t, 2)
1363
%1 = 5
1364
? t = 2;
1365
? subst(p, t, 3) \\@com \kbd{t} is no longer free: it evaluates to 2
1366
*** at top-level: subst(p,t,3)
1367
*** ^----
1368
*** variable name expected.
1369
? subst(p, 't, 3) \\ OK
1370
%3 = 10
1371
@eprog
1372
1373
\subsec{Variable priorities, multivariate objects}\sidx{variable (priority)}\label{se:priority}
1374
A multivariate polynomial in PARI is just a polynomial (in one variable),
1375
whose coefficients are themselves polynomials, arbitrary but for the fact
1376
that they do not involve the main variable. (PARI currently has no sparse
1377
representation for polynomials, listing only nonzero monomials.) All
1378
computations are then done formally on the coefficients as if the
1379
polynomial was univariate.
1380
1381
This is not symmetrical. So if I enter \kbd{'x + 'y} in a clean session,
1382
what happens? This is understood as
1383
$$ x^1 + (y^1 + 0*y^0)*x^0 \in (\Z[y])[x] $$
1384
but how do we know that $x$ is ``more important'' than $y$ ? Why not $y^1 +
1385
x*y^0$, which is the same mathematical entity after all?
1386
1387
The answer is that variables are ordered implicitly by the interpreter:
1388
when a new identifier (e.g~$x$, or $y$ as above) is input, the corresponding
1389
variable is registered as having a strictly lower priority than any variable in
1390
use at this point\footnote{*}{This is not strictly true:
1391
the variables $x$ and $y$ are predefined, and satisfy $x > y$.
1392
Variables of higher priority than $x$ can be created
1393
using \tet{varhigher}.}%
1394
. To see the ordering used by \kbd{gp} at any given time, type
1395
\kbd{variable()}.\kbdsidx{variable}
1396
1397
Given such an ordering, multivariate polynomials are stored so that the
1398
variable with the highest priority is the main variable. And so on,
1399
recursively, until all variables are exhausted. A different storage pattern
1400
(which could only be obtained via \kbd{libpari} programming and low-level
1401
constructors) would produce an invalid object, and eventually a disaster.
1402
1403
In any case, if you are working with expressions involving several variables
1404
and want to have them ordered in a specific manner in the internal
1405
representation just described, the simplest is just to write down the
1406
variables one after the other under \kbd{gp} before starting any real
1407
computations. You may also define variables from your \tet{gprc} to have a
1408
consistent ordering of common variable names in all your \kbd{gp} sessions,
1409
e.g read in a file \kbd{variables.gp} containing
1410
\bprog
1411
'x; 'y; 'z; 't; 'a;
1412
@eprog\noindent There is no way to change the priority of existing variables,
1413
but you may always create new ones with well-defined priorities using
1414
\kbd{varhigher} or \kbd{varlower}.
1415
1416
\misctitle{Important note} PARI allows Euclidean division of multivariate
1417
polynomials, but assumes that the computation takes place in the fraction
1418
field of the coefficient ring (if it is not an integral domain, the result
1419
will a priori not make sense). This can become tricky. For instance
1420
assume $x$ has highest priority, then $y$:
1421
\bprog
1422
? x % y
1423
%1 = 0
1424
? y % x
1425
%2 = y \\@com these two take place in $\Q(y)[x]$
1426
? x * Mod(1,y)
1427
%3 = Mod(1, y)*x \\@com in $(\Q(y)/y\Q(y))[x] \sim \Q[x]$
1428
? Mod(x,y)
1429
%4 = 0
1430
@eprog
1431
\noindent In the last example, the division by $y$ takes place in
1432
$\Q(y)[x]$,
1433
hence the \kbd{Mod} object is a coset in $(\Q(y)[x]) / (y\Q(y)[x])$, which
1434
is the null ring since $y$ is invertible! So be very wary of variable
1435
ordering when your computations involve implicit divisions and many
1436
variables. This also affects functions like \tet{numerator}/\tet{denominator}
1437
or \tet{content}:
1438
\bprog
1439
? denominator(x / y)
1440
%1 = 1
1441
? denominator(y / x)
1442
%2 = x
1443
? content(x / y)
1444
%3 = 1/y
1445
? content(y / x)
1446
%4 = y
1447
? content(2 / x)
1448
%5 = 2
1449
@eprog
1450
\noindent Can you see why? Hint: $x/y = (1/y) * x$ is in $\Q(y)[x]$ and
1451
denominator is taken with respect to $\Q(y)(x)$; $y/x = (y*x^0) / x$ is in
1452
$\Q(y)(x)$ so $y$ is invertible in the coefficient ring. On the other hand,
1453
$2/x$ involves a single variable and the coefficient ring is simply $\Z$.
1454
1455
These problems arise because the variable ordering defines an \emph{implicit}
1456
variable with respect to which division takes place. This is
1457
the price to pay to allow \kbd{\%} and \kbd{/} operators on polynomials
1458
instead of requiring a more cumbersome \kbd{divrem($x$, $y$, \var{var})}
1459
(which also exists). Unfortunately, in some functions like \tet{content} and
1460
\tet{denominator}, there is no way to set explicitly a main variable like in
1461
\tet{divrem} and remove the dependence on implicit orderings. This will
1462
hopefully be corrected in future versions.
1463
1464
\subsec{Multivariate power series}
1465
Just like multivariate polynomials, power series are fundamentally
1466
single-variable objects. It is awkward to handle many variables at once,
1467
since PARI's implementation cannot handle multivariate error terms like
1468
$O(x^i y^j)$. (It can handle the polynomial $O(y^j) \times x^i$ which is
1469
a very different thing, see below.)
1470
1471
The basic assumption in our model is that if variable $x$ has higher
1472
priority than $y$, then $y$ does not depend on $x$: setting $y$ to a
1473
function of $x$ after some computations with bivariate power series does
1474
not make sense a priori. This is because implicit constants in
1475
expressions like $O(x^i)$ depend on $y$ (whereas in $O(y^j)$ they can not
1476
depend on $x$). For instance
1477
\bprog
1478
? O(x) * y
1479
%1 = O(x)
1480
? O(y) * x
1481
%2 = O(y)*x
1482
@eprog\noindent
1483
Here is a more involved example:
1484
\bprog
1485
? A = 1/x^2 + 1 + O(x); B = 1/x + 1 + O(x^3);
1486
? subst(z*A, z, B)
1487
%2 = x^-3 + x^-2 + x^-1 + 1 + O(x)
1488
? B * A
1489
%3 = x^-3 + x^-2 + x^-1 + O(1)
1490
? z * A
1491
%4 = z*x^-2 + z + O(x)
1492
@eprog\noindent
1493
The discrepancy between \kbd{\%2} and \kbd{\%3} is surprising. Why does
1494
\kbd{\%2} contain a spurious constant term, which cannot be
1495
deduced from the input? Well, we ignored the rule that forbids to
1496
substitute an expression involving high-priority variables
1497
to a low-priority variable. The result \kbd{\%4} is correct according to
1498
our rules since the implicit constant in $O(x)$ may depend on $z$. It is
1499
obviously wrong if $z$ is allowed to have negative valuation in $x$. Of
1500
course, the correct error term should be $O(xz)$, but this is not
1501
possible in PARI.
1502
1503
\section{Variables and Scope}\sidx{variable scope}\label{se:scope}
1504
This section is rather technical, and strives to explain potentially
1505
confusing concepts. Skip to the last subsection for practical advice, if the
1506
next discussion does not make sense to you. After learning about user
1507
functions, study the example in \secref{se:bewarescope} then come back.
1508
1509
\misctitle{Definitions} % rather not make it a section to allow for ??my
1510
1511
A \emph{scope} is an enclosing context where names and values are attached.
1512
A user's function body, the body of a loop, an individual command line, all
1513
define scopes; the whole program defines the \emph{global} scope. The
1514
argument of \tet{eval} is evaluated in the enclosing scope.
1515
1516
Variables are bound to values within a given scope. This is traditionally
1517
implemented in two different ways:
1518
1519
\item\sidx{lexical scoping} lexical (or static) scoping: the binding makes
1520
sense within a given block of program text. The value is private to the block
1521
and may not be accessed from outside. Where to find the value is determined
1522
at compile time.
1523
1524
\item\sidx{dynamic scoping} dynamic scoping: introducing a local variable,
1525
say \kbd{x}, pushes a new value on a stack attached to the name \kbd{x}
1526
(possibly empty at this point), which is popped out when the control flow
1527
leaves the scope. Evaluating \kbd{x} in any context, possibly outside of the
1528
given block, always yields the top value on this dynamic stack.
1529
1530
GP implements both lexical and dynamic scoping, using the keywords%
1531
\footnote{*}{The names are borrowed from the \tet{Perl} scripting language.}
1532
\tet{my} (lexical) and \tet{local} (dynamic):
1533
\bprog
1534
x = 0;
1535
f() = x
1536
g() = my(x = 1); f()
1537
h() = local(x = 1); f()
1538
@eprog\noindent
1539
The function \kbd{g} returns 0 since the global \kbd{x} binding
1540
is unaffected by the introduction of a private variable of the same name in
1541
\kbd{g}. On the other hand, \kbd{h} returns 1; when it calls \kbd{f()}, the
1542
binding stack for the \kbd{x} identifier contains two items: the global
1543
binding to 0, and the binding to 1 introduced in \kbd{h}, which is still
1544
present on the stack since the control flow has not left \kbd{h} yet.
1545
1546
The rule mentionned in the previous section about built-in function names
1547
being reserved does \emph{not} apply to lexically scoped variables. Those may
1548
temporarily shadow an existing function name:
1549
\bprog
1550
my(gamma = 0) ; ...
1551
@eprog
1552
Without the \kbd{my}, this would be invalid since \kbd{gamma} is the $\Gamma$
1553
function.
1554
1555
\subsec{Scoping rules}
1556
1557
Named parameters in a function definition, as well as all loop
1558
indices\footnote{**}{
1559
More generally, in all iterative constructs which use a variable name
1560
(\kbd{for}, \kbd{prod}, \kbd{sum}, \kbd{vector}, \kbd{matrix},
1561
\kbd{plot}, etc.) the given variable is lexically scoped to the construct's
1562
body.},
1563
have lexical scope within the function body and the loop body respectively.
1564
\bprog
1565
p = 0;
1566
forprime (p = 2, 11, print(p)); p \\ prints 0 at the end
1567
1568
x = 0;
1569
f(x) = x++;
1570
f(1) \\ returns 2, and leave global x unaffected (= 0)
1571
@eprog\noindent
1572
If you exit the loop prematurely, e.g.~using the \kbd{break} statement, you
1573
must save the loop index in another variable since its value prior the loop
1574
will be restored upon exit. For instance
1575
\bprog
1576
for(i = 1, n,
1577
if (ok(i), break);
1578
);
1579
if (i > n, return(failure));
1580
@eprog\noindent
1581
is incorrect, since the value of $i$ tested by the $(i > n)$ is quite
1582
unrelated to the loop index. One ugly workaround is
1583
\bprog
1584
for(i = 1, n,
1585
if (ok(i), isave = i; break);
1586
);
1587
if (isave > n, return(failure));
1588
@eprog\noindent
1589
But it is usually more natural to wrap the loop in a user function
1590
and use \kbd{return} instead of \kbd{break}:
1591
\bprog
1592
try() =
1593
{
1594
for(i = 1, n,
1595
if (ok(i), return (i));
1596
);
1597
0 \\ failure
1598
}
1599
@eprog
1600
1601
A list of variables can be lexically or dynamically scoped (to the block
1602
between the declaration and the end of the innermost enclosing scope) using a
1603
\kbd{my} or \kbd{local} declaration:
1604
\bprog
1605
for (i = 1, 10,
1606
my(x, y, z, i2 = i^2); \\ temps needed within the loop body
1607
...
1608
)
1609
@eprog\noindent
1610
Note how the declaration can include (optional) initial values, \kbd{i2 =
1611
i\pow 2} in the above. Variables for which no explicit default value is given
1612
in the declaration are initialized to $0$. It would be more natural to
1613
initialize them to free variables, but this would break backward
1614
compatibility. To obtain this behavior, you may explicitly use the quoting
1615
operator:
1616
\bprog
1617
my(x = 'x, y = 'y, z = 'z);
1618
@eprog\noindent
1619
A more complicated example:
1620
\bprog
1621
for (i = 1, 3,
1622
print("main loop");
1623
my(x = i); \\ local to the outermost loop
1624
for (j = 1, 3,
1625
my (y = x^2); \\ local to the innermost loop
1626
print (y + y^2);
1627
x++;
1628
)
1629
)
1630
@eprog\noindent
1631
When we leave the loops, the values of \kbd{x}, \kbd{y}, \kbd{i}, \kbd{j}
1632
are the same as before they were started.
1633
1634
Note that \tet{eval} is evaluated in the given scope, and can access values
1635
of lexical variables:
1636
\bprog
1637
? x = 1;
1638
? my(x = 0); eval("x")
1639
%2 = 0 \\@com we see the local \kbd{x} scoped to this command line, not the global one
1640
@eprog
1641
1642
Variables dynamically scoped using \kbd{local} should more appropriately be
1643
called \emph{temporary values} since they are in fact local to the function
1644
declaring them \emph{and} any subroutine called from within. In practice, you
1645
almost certainly want true private variables, hence should use almost
1646
exclusively \kbd{my}.
1647
1648
We strongly recommended to explicitly scope (lexically) all variables to the
1649
smallest possible block. Should you forget this, in expressions involving such
1650
``rogue'' variables, the value used will be the one which happens to be on
1651
top of the value stack at the time of the call; which depends on the whole
1652
calling context in a nontrivial way. This is in general \emph{not} what you
1653
want.
1654
1655
\section{User defined functions}\sidx{user defined functions}
1656
\label{se:user_defined}
1657
1658
User-defined functions are ordinary GP objects, bound to variables just like
1659
any other object. Those variables are subject to scoping rules as any other:
1660
while you can define all your functions in global scope, it is usually
1661
possible and cleaner to lexically scope your private helper functions to the
1662
block of text where they will be needed.
1663
1664
Whenever gp meets a construction of the form \kbd{expr(\var{argument list})}
1665
and the expression \kbd{expr} evaluates to a function (an object of type
1666
\typ{CLOSURE}), the function is called with the proper arguments. For
1667
instance, constructions like \kbd{funcs[i](x)} are perfectly valid,
1668
assuming \kbd{funcs} is an array of functions.
1669
1670
As regards argument passing conventions, GP functions support both
1671
1672
\item \idx{call by value}: the function operates on a copy of a variable,
1673
changes made to the argument in the function do not affect the original
1674
variable;
1675
1676
\item and \idx{call by reference}: the function receives a reference to the
1677
variable and original data is affected.
1678
1679
\subsec{Defining a function}\label{se:userfundef}
1680
1681
A user function is defined as follows:
1682
1683
\kbd{(\var{list of formal variables}) -> \var{seq}}.
1684
1685
\noindent The list of formal variables is a comma-separated list of
1686
\emph{distinct} variable names and allowed to be empty. It there is a single
1687
formal variable, the parentheses are optional. This list corresponds to the
1688
list of parameters you will supply to your function when calling it. By
1689
default, GP functions use call by value to pass arguments; a variable
1690
name may be prefixed by a tilde \kbd{\til} to use instead a call by
1691
reference.
1692
1693
In most cases you want to assign a function to a variable immediately, as in
1694
\bprog
1695
R = (x,y) -> sqrt( x^2+y^2 );
1696
sq = x -> x^2; \\@com or equivalently \kbd{(x) -> x\pow2}
1697
@eprog\noindent
1698
but it is quite possible to define short-lived anonymous functions.
1699
The trailing semicolon is not part of the definition, but as usual prevents
1700
\kbd{gp} from printing the result of the evaluation, i.e. the function
1701
object. The construction
1702
1703
\kbd{f(\var{list of formal variables}) = \var{seq}}
1704
1705
\noindent is available as an alias for
1706
1707
\kbd{f = (\var{list of formal variables}) -> \var{seq}}
1708
1709
\noindent Using that syntax, it is not possible to define anonymous functions
1710
(obviously), and the above two examples become:
1711
\bprog
1712
R(x,y) = sqrt( x^2+y^2 );
1713
sq(x) = x^2;
1714
@eprog\noindent
1715
The semicolon serves the same purpose as above: preventing the printing
1716
of the resulting function object; compare
1717
\bprog
1718
? sq(x) = x^2; \\@com no output
1719
? sq(x) = x^2 \\@com print the result: a function object
1720
%2 = (x)->x^2
1721
@eprog\noindent Of course, the sequence \var{seq} can be arbitrarily
1722
complicated, in which case it will look better written on consecutive lines,
1723
with properly scoped variables:
1724
\bprog
1725
{
1726
f(x0, x1, @dots) =
1727
my(t0, t1, @dots); \\@com variables lexically scoped to the function body
1728
@dots
1729
}
1730
@eprog \noindent Note that the following variant would also work:
1731
\bprog
1732
f(x0, x1, @dots) =
1733
{
1734
my(t0, t1, @dots); \\@com variables lexically scoped to the function body
1735
@dots
1736
}
1737
@eprog \noindent
1738
(the first newline is disregarded due to the preceding \kbd{=} sign, and the
1739
others because of the enclosing braces). The \tet{my} statements can actually
1740
occur anywhere within the function body, scoping the variables to more
1741
restricted blocks than the whole function body.
1742
1743
Formal parameters are lexically scoped to the function body. It is not
1744
allowed to use the same variable name for different parameters of your
1745
function:
1746
\bprog
1747
? f(x,x) = 1
1748
*** variable declared twice: f(x,x)=1
1749
*** ^----
1750
@eprog
1751
1752
By default, arguments are passed by value, not as variables: modifying a
1753
function's argument in the function body is allowed, but does not modify its
1754
value in the calling scope. In fact, a \emph{copy} of the actual parameter is
1755
assigned to the formal parameter when the function is called. (This is not
1756
litterally true: a form of copy-on-write is implemented so an object is not
1757
duplicated unless modified in the function.) If an argument is prefixed by a
1758
tilde \kbd{\til} in the function declaration \emph{and} the call, it is
1759
passed by reference. (If either the declaration or the call is missing
1760
a tilde, we revert to a call by value.)
1761
1762
\bprog
1763
? x = [1];
1764
? f(v) = v[1]++;
1765
? F(~v) = v[1]++;
1766
1767
? f(x)
1768
%4 = 2
1769
? x \\ unchanged
1770
%5 = [1]
1771
? F(~x)
1772
%6 = 2
1773
? x \\ incremented
1774
%7 = [2]
1775
? F(x) \\ forgot the ~: call by value
1776
%8 = 3
1777
? x \\ => contents of x did not change
1778
%9 = [2]
1779
? f(~x) \\ adding a ~ in call, missing in declaration
1780
%10 = 3
1781
? x \\ => call by value
1782
%11 = [2]
1783
@eprog
1784
\misctitle{Caveat} In GP, a call by reference means that the function
1785
accesses the value and may change the original variable content, but only if
1786
it is a container type (a vector, list or matrice), as shown above with the
1787
vector $x$. It will not alter its value in other cases !
1788
\bprog
1789
? v = 1 \\ not a container
1790
? F(~v) = v++
1791
? F(~v)
1792
%3 = 2
1793
? v \\ components of v could be altered, not v itself
1794
%4 = 1
1795
@eprog
1796
1797
\misctitle{Functions taking an unlimited number of arguments}
1798
1799
A function taking an unlimited number of arguments is called \emph{variadic}.
1800
To create such a function, use the syntax
1801
1802
\kbd{(\var{list of formal variables}, \var{var}[..]) -> \var{seq}}
1803
1804
\noindent The parameter \var{var} is replaced by a vector containing all the
1805
remaining arguments. The name may not be prefixed by a tilde to (absurdly)
1806
indicate a call by reference.
1807
1808
\bprog
1809
? f(c[..]) = sum(i=1,#c,c[i]);
1810
? f(1,2,3)
1811
%1 = 6
1812
? sep(s,v[..]) = for(i=1,#v-1,print1(v[i],s)); if (#v, print(v[#v]));
1813
? sep(":", 1, 2, 3)
1814
1:2:3
1815
@eprog
1816
1817
\misctitle{Finishing touch}
1818
You can add a specific help message for your function using \kbd{addhelp},
1819
but the online help system already handles it. By default \kbd{?\var{name}}
1820
will print the definition of the function \var{name}: the list of arguments,
1821
as well as their default values, the text of \var{seq} as you input it.
1822
Just as \b{c} prints the list of all built-in commands, \b{u} outputs the
1823
list of all user-defined functions.
1824
1825
\misctitle{Backward compatibility (lexical scope)} Lexically scoped
1826
variables were introduced in version~2.4.2. Before that, the formal
1827
parameters were dynamically scoped. If your script depends on this behavior,
1828
you may use the following trick: replace the initial \kbd{f(x) =} \ by
1829
\bprog
1830
f(x_orig) = local(x = x_orig)
1831
@eprog
1832
\misctitle{Backward compatibility (disjoint namespaces)} Before version
1833
2.4.2, variables and functions lived in disjoint namespaces and it was not
1834
possible to have a variable and a function share the same name. Hence the
1835
need for a \kbd{kill} function allowing to reuse symbols. This is no longer
1836
the case.
1837
1838
There is now no distinction between variable and function names: we
1839
have PARI objects (functions of type \typ{CLOSURE}, or more mundane
1840
mathematical entities, like \typ{INT}, etc.) and variables bound to them.
1841
There is nothing wrong with the following sequence of assignments:
1842
\bprog
1843
? f = 1 \\@com assigns the integer 1 to \kbd{f}
1844
%1 = 1;
1845
? f() = 1 \\@com a function with a constant value
1846
%2 = ()->1
1847
? f = x^2 \\@com \kbd{f} now holds a polynomial
1848
%3 = x^2
1849
? f(x) = x^2 \\@com \dots and now a polynomial function
1850
%4 = (x)->x^2
1851
? g(fun) = fun(Pi);\\@com a function taking a function as argument
1852
? g(cos)
1853
%6 = -1.000000000000000000000000000
1854
1855
@eprog\noindent
1856
Previously used names can be recycled as above: you are just redefining the
1857
variable. The previous definition is lost of course.
1858
1859
\misctitle{Important technical note} Built-in functions are a special case
1860
since they are read-only (you cannot overwrite their default meaning),
1861
and they use features not available to user functions, in particular pointer
1862
arguments. In the present version \vers{}, it is possible to assign a built-in
1863
function to a variable, or to use a built-in function name to create an
1864
anonymous function, but some special argument combinations may not be
1865
available:
1866
\bprog
1867
? issquare(9, &e)
1868
%1 = 1
1869
? e
1870
%2 = 3
1871
? g = issquare;
1872
? g(9)
1873
%4 = 1
1874
? g(9, &e) \\@com pointers are not implemented for user functions
1875
*** unexpected &: g(9,&e)
1876
*** ^---
1877
@eprog
1878
1879
\subsec{Function call, Default arguments}
1880
1881
You may now call your function, as in \kbd{f(1,2)}, supplying values
1882
for the formal variables. The number of parameters actually supplied may be
1883
\emph{less} than the number of formal variables in the function definition.
1884
An uninitialized formal variable is given an implicit default value of (the
1885
integer)~0, i.e. after the definition
1886
\bprog
1887
f(x, y) = ...
1888
@eprog\noindent
1889
you may call \kbd{f(1, 2)}, supplying values for the two formal
1890
parameters, or for example
1891
\settabs\+\indent&xxxxxxxxx& equivalent to xxxx &\cr
1892
\+& \kbd{f(2)} & equivalent to &\kbd{f(2,0)},\cr
1893
\+& \kbd{f()} & & \kbd{f(0,0)},\cr
1894
\+& \kbd{f(,3)} & &\kbd{f(0,3)}. (``Empty argument'' trick)\cr
1895
\noindent This \emph{implicit} default value of $0$, is actually deprecated
1896
and setting
1897
\bprog
1898
default(strictargs, 1)
1899
@eprog\noindent allows to disable it (see \secref{se:def,strictargs}).\kbdsidx{strictargs}
1900
1901
The recommended practice is to \emph{explicitly} set a default value:
1902
in the function definition, you can append \kbd{=}\var{expr} to a formal
1903
parameter, to give that variable a default value. The expression
1904
gets evaluated the moment the function is called, and may involve the
1905
preceding function parameters: a default value for $x_i$ may involve $x_j$
1906
for $j < i$. For instance, after
1907
\bprog
1908
f(x = 1, y = 2, z = y+1) = ....
1909
@eprog\noindent
1910
typing in \kbd{f(3,4)} would give you \kbd{f(3,4,5)}. In the rare case when
1911
you want to set some far away argument, and leave the defaults in between as
1912
they stand, use the ``empty argument'' trick: \kbd{f(6,,1)} would yield
1913
\kbd{f(6,2,1)}. Of course, \kbd{f()} by itself yields \kbd{f(1,2,3)} as was
1914
to be expected.
1915
1916
In short, the argument list is filled with user supplied values, in
1917
order. A comma or closing parenthesis, where a value should have been,
1918
signals we must use a default value. When no input arguments are left, the
1919
defaults are used instead to fill in remaining formal parameters.
1920
A final example:
1921
\bprog
1922
f(x, y=2, z=3) = print(x, ":", y, ":", z);
1923
@eprog
1924
\noindent defines a function which prints its arguments (at most three of
1925
them), separated by colons.
1926
\bprog
1927
? f(6,7)
1928
6:7:3
1929
? f(,5)
1930
0:5:3
1931
? f()
1932
0:2:3
1933
@eprog\noindent If \kbd{strictargs} is set (recommended), $x$ is now a
1934
mandatory argument, and the above becomes:
1935
\bprog
1936
? default(strictargs,1)
1937
? f(6,7)
1938
6:7:3
1939
? f(,5)
1940
*** at top-level: f(,5)
1941
*** ^-----
1942
*** in function f: x,y=2,z=3
1943
*** ^---------
1944
*** missing mandatory argument 'x' in user function.
1945
@eprog
1946
1947
\misctitle{Example} We conclude with an amusing example, intended to
1948
illustrate both user-defined functions and the power of the \kbd{sumalt}
1949
function. Although the \idx{Riemann zeta-function} is included (as
1950
\kbd{zeta}) among the standard functions, let us assume that we want to check
1951
other implementations. Since we are highly interested in the critical strip,
1952
we use the classical formula
1953
$$ (2^{1-s} - 1)\zeta(s) = \sum_{n\geq 1} (-1)^n n^{-s},
1954
\qquad\Re s > 0.$$
1955
The implementation is obvious:\sidx{zeta function}
1956
\bprog
1957
ZETA(s) = sumalt(n=1, (-1)^n*n^(-s)) / (2^(1-s) - 1)
1958
@eprog
1959
\noindent
1960
Note that \kbd{n} is automatically lexically scoped to the \kbd{sumalt}
1961
``loop'', so that it is unnecessary to add a \kbd{my(n)} declaration to the
1962
function body. Surprisingly, this gives very good accuracy in a larger region
1963
than expected:
1964
\bprog
1965
? check = z -> ZETA(z) / zeta(z);
1966
? check(2)
1967
%1 = 1.000000000000000000000000000
1968
? check(200)
1969
%2 = 1.000000000000000000000000000
1970
? check(0)
1971
%3 = 0.9999999999999999999999999994
1972
? check(-5)
1973
%4 = 1.00000000000000007549266557
1974
? check(-11)
1975
%5 = 0.9999752641047824902660847745
1976
? check(1/2+14.134*I) \\@com very close to a nontrivial zero
1977
%6 = 1.000000000000000000003747432 + 7.62329066 E-21*I
1978
? check(-1+10*I)
1979
%7 = 1.000000000000000000000002511 + 2.989950968 E-24*I
1980
@eprog\noindent Now wait a minute; not only are we summing a series which is
1981
certainly no longer alternating (it has complex coefficients), but we are
1982
also way outside of the region of convergence, and still get decent results! No
1983
programming mistake this time: \kbd{sumalt} is a
1984
``magic'' function\footnote{*}{\kbd{sumalt} is heuristic, but its use can be
1985
rigorously justified for a given function, in particular our $\zeta(s)$
1986
formula. Indeed, Peter Borwein (\emph{An efficient algorithm for the Riemann
1987
zeta function}, CMS Conf.~Proc.~{\bf 27} (2000), pp.~29--34) proved that the
1988
formula used in \kbd{sumalt} with $n$ terms computes $(1-2^{1-s})\zeta(s)$
1989
with a relative error of the order of $(3+\sqrt{8})^{-n}|\Gamma(s)|^{-1}$.},
1990
providing very good convergence acceleration; in effect, we are computing
1991
the analytic continuation of our original function. To convince ourselves
1992
that \kbd{sumalt} is a nontrivial implementation, let us try a simpler
1993
example:
1994
\bprog
1995
? sum(n=1, 10^7, (-1)^n/n, 0.) / (-log(2)) \\@com approximates the well-known formula
1996
time = 7,417 ms.
1997
%1 = 0.9999999278652515622893405457
1998
? sumalt(n=1, (-1)^n/n) / (-log(2)) \\@com accurate and fast
1999
time = 0 ms.
2000
%2 = 1.000000000000000000000000000
2001
@eprog\noindent No, we are not using a powerful simplification tool here,
2002
only numerical computations. Remember, PARI is not a computer algebra system!
2003
2004
2005
\subsec{Beware scopes}\label{se:bewarescope}
2006
Be extra careful with the scopes of variables. What is wrong with the
2007
following definition?
2008
\bprog
2009
FirstPrimeDiv(x) =
2010
{ my(p);
2011
forprime(p=2, x, if (x%p == 0, break));
2012
p
2013
}
2014
? FirstPrimeDiv(10)
2015
%1 = 0
2016
@eprog\noindent \misctitle{Hint} The function body is equivalent to
2017
\bprog
2018
{ my(newp = 0);
2019
forprime(p=2, x, if (x%p == 0, break));
2020
newp
2021
}
2022
@eprog\noindent
2023
\misctitle{Detailed explanation} The index \kbd{p} in the \kbd{forprime}
2024
loop is lexically scoped to the loop and is not visible to the outside world.
2025
Hence, it will not survive the \kbd{break} statement. More precisely, at this
2026
point the loop index is restored to its preceding value. The initial
2027
\kbd{my(p)}, although well-meant, adds to the confusion: it indeed scopes
2028
\kbd{p} to the function body, with initial value $0$, but the \kbd{forprime}
2029
loop introduces \emph{another} variable, unfortunately also called \kbd{p},
2030
scoped to the loop body, which shadows the one we wanted. So we always return
2031
$0$, since the value of the \kbd{p} scoped to the function body never changes
2032
and is initially $0$.
2033
2034
To sum up, the routine returns the \kbd{p} declared local to
2035
it, not the one which was local to \kbd{forprime} and ran through consecutive
2036
prime numbers. Here is a corrected version:
2037
\bprog
2038
? FirstPrimeDiv(x) = forprime(p=2, x, if (x%p == 0, return(p)))
2039
@eprog
2040
2041
\subsec{Recursive functions} Recursive functions\sidx{recursion} can easily
2042
be written as long as one pays proper attention to variable scope. Here is an
2043
example, used to retrieve the coefficient array of a multivariate polynomial
2044
(a nontrivial task due to PARI's unsophisticated representation for those
2045
objects): \sidx{multivariate polynomial}
2046
\bprog
2047
coeffs(P, nbvar) =
2048
{
2049
if (type(P) != "t_POL",
2050
for (i=1, nbvar, P = [P]);
2051
return (P)
2052
);
2053
vector(poldegree(P)+1, i, coeffs(polcoef(P, i-1), nbvar-1))
2054
}
2055
@eprog
2056
2057
\noindent If $P$ is a polynomial in $k$ variables, show that after the
2058
assignment {\tt v = coeffs(P,k)}, the coefficient of $x_1^{n_1}\dots
2059
x_k^{n_k}$ in P is given by {\tt v[$n_1$+1][\dots][$n_k$+1]}.
2060
2061
When the operating system allows querying the maximum size of the process
2062
stack, we automatically limit the \idx{recursion depth}: \sidx{deep recursion}
2063
\bprog
2064
? dive(n) = dive(n+1)
2065
? dive(0);
2066
*** [...] at: dive(n+1)
2067
*** ^---------
2068
*** in function dive: dive(n+1)
2069
*** ^---------
2070
\\@com (last 2 lines repeated 19 times)
2071
*** deep recursion.
2072
@eprog\noindent
2073
All Unix variants support this mechanism and the recursion limit
2074
may be different from one machine to the next; other systems may crash on
2075
deep recursion. There is no way to increase the limit from within \kbd{gp}.
2076
On a Unix system, you may increase it before launching \kbd{gp} with
2077
\tet{ulimit} or \tet{limit}, depending on your shell, and raise the process
2078
available stack space (increase \tet{stacksize}).
2079
2080
\subsec{Function which take functions as parameters} This is done as follows:
2081
\bprog
2082
? calc(f, x) = f(x)
2083
? calc(sin, Pi)
2084
%2 = -5.04870979 E-29
2085
? g(x) = x^2;
2086
? calc(g, 3)
2087
%4 = 9
2088
@eprog
2089
\noindent If we do not need \kbd{g} elsewhere, we should use an anonymous
2090
function here, \kbd{calc(x->x\pow 2, 3)}. Here is a variation:
2091
\bprog
2092
? funs = [cos, sin, tan, x->x^3+1]; \\@com an array of functions
2093
? call(i, x) = funs[i](x)
2094
@eprog\noindent
2095
evaluates the appropriate function on argument \kbd{x},
2096
provided $1\leq i\leq 4$. Finally, a more useful example:
2097
\bprog
2098
APPLY(f, v) = vector(#v, i, f(v[i]))
2099
@eprog\noindent
2100
applies the function \kbd{f} to every element in the vector \kbd{v}.
2101
(The built-in function \kbd{apply} is more powerful since it also applies to
2102
lists and matrices.)
2103
2104
\subsec{Defining functions within a function}
2105
Defining a single function is easy:
2106
\bprog
2107
init(x) = (add = y -> x+y);
2108
@eprog\noindent Basically, we are defining a global variable \kbd{add}
2109
whose value is the function \kbd{y->x+y}. The parentheses were added for
2110
clarity and are not mandatory.
2111
\bprog
2112
? init(5);
2113
? add(2)
2114
%2 = 7
2115
@eprog\noindent
2116
A more refined approach is to
2117
avoid global variables and \emph{return} the function:
2118
\bprog
2119
init(x) = y -> x+y
2120
add = init(5)
2121
@eprog\noindent Then \kbd{add(2)} still returns 7, as expected! Of course,
2122
if \kbd{add} is in global scope, there is no gain, but we can
2123
lexically scope it to the place where it is useful:
2124
\bprog
2125
my ( add = init(5) );
2126
@eprog
2127
2128
How about multiple functions then? We can use the last idea and return a
2129
vector of functions, but if we insist on global variables?
2130
The first idea
2131
\bprog
2132
init(x) = add(y) = x+y; mul(y) = x*y;
2133
@eprog
2134
\noindent does not work since in the construction \kbd{f() = }\var{seq}, the
2135
function body contains everything until the end of the expression. Hence
2136
executing \kbd{init} defines the wrong function \kbd{add} (itself defining
2137
a function \kbd{mul}). The way out is to
2138
use parentheses for grouping, so that enclosed subexpressions will be
2139
evaluated independently:
2140
\bprog
2141
? init(x) = ( add(y) = x+y ); ( mul(y) = x*y );
2142
? init(5);
2143
? add(2)
2144
%3 = 7
2145
? mul(3)
2146
%4 = 15
2147
@eprog\noindent This defines two global functions which have access to the
2148
lexical variables private to \kbd{init}! The following would work in exactly
2149
the same way:
2150
\bprog
2151
? init5() = my(x = 5); ( add(y) = x+y ); ( mul(y) = x*y );
2152
@eprog
2153
2154
\subsec{Closures as Objects} Contrary to what you might think after the
2155
preceding examples, GP's closures may not be used to simulate true
2156
``objects'', with private and public parts and methods to access and
2157
manipulate them. In fact, closures indeed incorporate an existing context
2158
(they may access lexical variables that existed at the time of their
2159
definition), but then may not change it. More precisely, they access a copy,
2160
which they are welcome to change, but a further function call still accesses
2161
the original context, as it existed at the time the function was defined:
2162
\bprog
2163
init() =
2164
{ my(count = 0);
2165
(inc()=count++);
2166
(dec()=count--);
2167
}
2168
? init();
2169
? inc()
2170
%1 = 1
2171
? inc()
2172
%2 = 1
2173
? dec()
2174
%3 = -1
2175
? dec()
2176
%4 = -1
2177
@eprog
2178
2179
\section{Member functions}\sidx{member functions} \label{se:member}
2180
2181
Member functions use the `dot' notation to retrieve information from
2182
complicated structures. The built-in structures are \tev{bid}, \tev{ell},
2183
\tev{galois}, \tev{ff}, \tev{nf}, \tev{bnf}, \tev{bnr} and \tev{prid}, which
2184
will be described at length in Chapter~3. The syntax \kbd{structure.member}
2185
is taken to mean: retrieve \kbd{member} from \kbd{structure},
2186
e.g.~\kbd{E.j} returns the $j$-invariant of the elliptic curve \kbd{E},
2187
or outputs an error message if \kbd{E} is not a proper \tev{ell} structure.
2188
To define your own member functions, use the syntax
2189
2190
\ \kbd{\var{var}.\var{member} = \var{seq}},
2191
2192
\noindent where the formal variable \var{var} is scoped to the function
2193
body \var{seq}. This is of course reminiscent of a user function with a
2194
single formal variable \var{var}. For instance, the current implementation of
2195
the \kbd{ell} type is a vector, the $j$-invariant being the thirteenth
2196
component. It could be implemented as
2197
2198
\bprog
2199
x.j =
2200
{
2201
if (type(x) != "t_VEC" || #x < 14, error("not an elliptic curve: " x));
2202
x[13]
2203
}
2204
@eprog\noindent As for user functions, you can redefine your member functions
2205
simply by typing new definitions. On the other hand, as a safety measure, you
2206
cannot redefine the built-in member functions, so attempting to redefine
2207
\kbd{x.j} as above would in fact produce an error; you would have to call it
2208
e.g.~\kbd{x.myj} in order for \kbd{gp} to accept it.
2209
2210
Member functions use call by reference to pass arguments, your function
2211
may modify in place the contents of a variable (of container type).
2212
2213
\misctitle{Rationale} In most cases, member functions are simple accessors
2214
of the form
2215
\bprog
2216
x.a = x[1];
2217
x.b = x[2];
2218
x.c = x[3];
2219
@eprog\noindent
2220
where \kbd{x} is a vector containing relevant data. There are at least
2221
three alternative approaches to the above member functions: 1) hardcode
2222
\kbd{x[1]}, etc. in the program text, 2) define constant global variables
2223
\kbd{AINDEX = 1}, \kbd{BINDEX = 2} and hardcode \kbd{x[AINDEX]}, 3)
2224
user functions \kbd{a(x) = x[1]} and so on.
2225
2226
Even if 2) improves on 1), these solutions are neither elegant nor flexible,
2227
and they scale badly. 3) is a genuine possibility, but the main advantage of
2228
member functions is that their namespace is independent from the variables
2229
(and functions) namespace, hence we can use very short identifiers without
2230
risk. The $j$-invariant is a good example: it would clearly not be a good
2231
idea to define \kbd{j(E) = E[13]}, because clashes with loop indices are
2232
likely.
2233
2234
Beware that there is no guarantee that a built-in member function \emph{is}
2235
a simple accessor and it could involve a computation. Thus you should
2236
not use them on a constant object in tight loops: store them in a variable
2237
before the loop.
2238
2239
\misctitle{Note} Typing \b{um} will output all user-defined member functions.
2240
2241
\misctitle{Member function names} A valid name starts with a letter followed
2242
by any number of keyword characters: \kbd{\_} or alphanumeric characters
2243
([\kbd{A-Za-z0-9}]). The built-in member function names are reserved and
2244
cannot be used (see the list with \kbd{?.}). Finally, names starting with
2245
\kbd{e} or \kbd{E} followed by a digit are forbidden, due to a clash with
2246
the floating point exponent notation: we understand \kbd{1.e2} as
2247
$100.000\dots$, not as extracting member \kbd{e2} of object \kbd{1}.
2248
2249
\section{Strings and Keywords}\sidx{string}\sidx{keyword}
2250
\label{se:strings}
2251
2252
\subsec{Strings} GP variables can hold values of type character string
2253
(internal type \typ{STR}). This section describes how they are actually used,
2254
as well as some convenient tricks (automatic concatenation and expansion,
2255
keywords) valid in string context.
2256
2257
As explained above, the general way to input a string is to enclose
2258
characters between quotes~\kbd{"}. This is the only input construct where
2259
whitespace characters are significant: the string will contain the exact
2260
number of spaces you typed in. Besides, you can ``escape'' characters by
2261
putting a \kbd{\bs} just before them; the translation is as follows
2262
\bprog
2263
\e: <Escape>
2264
\n: <Newline>
2265
\t: <Tab>
2266
@eprog
2267
For any other character $x$, \b{$x$} is expanded to $x$. In particular, the
2268
only way to put a \kbd{"} into a string is to escape it. Thus, for
2269
instance, \kbd{"\bs"a\bs""} would produce the string whose content is
2270
``a''. This is definitely \emph{not} the same thing as typing \kbd{"a"},
2271
whose content is merely the one-letter string a.
2272
2273
You can concatenate two strings using the \tet{concat} function. If either
2274
argument is a string, the other is automatically converted to a string if
2275
necessary (it will be evaluated first).
2276
2277
\bprog
2278
? concat("ex", 1+1)
2279
%1 = "ex2"
2280
? a = 2; b = "ex"; concat(b, a)
2281
%2 = "ex2"
2282
? concat(a, b)
2283
%3 = "2ex"
2284
@eprog
2285
2286
Some functions expect strings for some of their arguments: \tet{print} would
2287
be an obvious example, \tet{Str} is a less obvious but useful one (see the
2288
end of this section for a complete list). While typing in such an argument,
2289
you will be said to be in \tev{string context}. The rest of this section is
2290
devoted to special syntactical tricks which can be used with such arguments
2291
(and only here; you will get an error message if you try these outside of
2292
string context):
2293
2294
\item Writing two strings alongside one another will just concatenate
2295
them, producing a longer string. Thus it is equivalent to type in
2296
\kbd{"a " "b"} or \kbd{"a b"}. A little tricky point in the first expression:
2297
the first whitespace is enclosed between quotes, and so is part of a string;
2298
while the second (before the \kbd{"b"}) is completely optional and \kbd{gp}
2299
actually suppresses it, as it would with any number of whitespace characters
2300
at this point (i.e.~outside of any string).
2301
2302
\item If you insert any expression when a string is expected, it gets
2303
``expanded'': it is evaluated as a standard GP expression, and the final
2304
result (as would have been printed if you had typed it by itself) is then
2305
converted to a string, as if you had typed it directly. For instance \kbd{"a"
2306
1+1 "b"} is equivalent to \kbd{"a2b"}: three strings get created, the middle
2307
one being the expansion of \kbd{1+1}, and these are then concatenated
2308
according to the rule described above. Another tricky point here: assume you
2309
did not assign a value to \kbd{aaa} in a GP expression before. Then typing
2310
\kbd{aaa} by itself in a string context will actually produce the correct
2311
output (i.e.~the string whose content is aaa), but in a fortuitous way. This
2312
\kbd{aaa} gets expanded to the monomial of degree one in the variable
2313
\kbd{aaa}, which is of course printed as \kbd{aaa}, and thus will expand to
2314
the three letters you were expecting.
2315
2316
\misctitle{Warning} Expression involving strings are not handled in a
2317
special way; even in string context, the largest possible expression is
2318
evaluated, hence \kbd{print("a"[1])} is incorrect since \kbd{"a"} is not an
2319
object whose first component can be extracted. On the other hand
2320
\kbd{print("a", [1])} is correct (two distinct argument, each converted to a
2321
string), and so is \kbd{print("a" 1)} (since \kbd{"a"1} is not a valid
2322
expression, only \kbd{"a"} gets expanded, then \kbd{1}, and the result is
2323
concatenated as explained above).
2324
2325
\subsec{Keywords} Since there are cases where expansion is not desirable, we
2326
now distinguish between ``Keywords'' and ``Strings''. String is what has been
2327
described so far. Keywords are special relatives of Strings which are
2328
automatically assumed to be quoted, whether you actually type in the quotes
2329
or not. Thus expansion is never performed on them. They get concatenated,
2330
though. The analyzer supplies automatically the quotes you have ``forgotten''
2331
and treats Keywords just as normal strings otherwise. For instance, if you
2332
type \kbd{"a"b+b} in Keyword context, you will get the string whose contents
2333
are ab+b. In String context, on the other hand, you would get a2\kbd{*}b.
2334
2335
All GP functions have prototypes (described in Chapter~3 below) which
2336
specify the types of arguments they expect: either generic PARI objects
2337
(GEN), or strings, or keywords, or unevaluated expression sequences. In the
2338
keyword case, only a very small set of words will actually be meaningful
2339
(the \kbd{default} function is a prominent example).
2340
2341
\misctitle{Reference} The arguments of the following functions are processed
2342
in string context:
2343
2344
\settabs\+\indent&\cr
2345
\+&\tet{Str}\cr
2346
\+&\tet{addhelp} (second argument)\cr
2347
\+&\tet{default} (second argument)\cr
2348
\+&\tet{error}\cr
2349
\+&\tet{extern}\cr
2350
\+&\tet{plotstring} (second argument)\cr
2351
\+&\tet{plotterm} (first argument)\cr
2352
\+&\tet{read} and \tet{readvec}\cr
2353
\+&\tet{system}\cr
2354
\+&all the \tet{print}\var{xxx} functions\cr
2355
\+&all the \tet{write}\var{xxx} functions\cr
2356
2357
\noindent The arguments of the following functions are processed as keywords:
2358
2359
\+&\tet{alias}\cr
2360
\+&\tet{default} (first argument)\cr
2361
\+&\tet{install} (all arguments but the last)\cr
2362
\+&\tet{trap} (first argument)\cr
2363
\+&\tet{whatnow}\cr
2364
2365
\subsec{Useful example} The function \kbd{Str} converts its arguments into
2366
strings and concatenate them. Coupled with \tet{eval}, it is very powerful.
2367
The following example creates generic matrices\sidx{generic
2368
matrix}\sidx{matrix}:
2369
\bprog
2370
? genmat(u,v,s="x") = matrix(u,v,i,j, eval( Str(s,i,j) ))
2371
? genmat(2,3) + genmat(2,3,"m")
2372
%1 =
2373
[x11 + m11 x12 + m12 x13 + m13]
2374
[x21 + m21 x22 + m22 x23 + m23]
2375
@eprog
2376
2377
\section{Errors and error recovery}
2378
2379
\subsec{Errors} Your input program is first compiled to a more efficient
2380
bytecode; then the latter is evaluated, calling appropriate functions from
2381
the PARI library. Accordingly, there are two kind of errors: syntax errors
2382
produced by the compiler, and runtime errors produced by the PARI library
2383
either by the evaluator itself, or in a mathematical function.
2384
Both kinds are fatal to your computation: \kbd{gp} will report the error
2385
and perform some cleanup (restore variables modified while evaluating the
2386
erroneous command, close open files, reclaim unused memory, etc.).
2387
2388
At this point, the default is to return to the usual prompt, but if the
2389
\kbd{recover} option (\secref{se:def,recover}) is off then \kbd{gp} exits
2390
immediately. This can be useful for batch-mode operation to make untrapped
2391
errors fatal.
2392
2393
When reporting a \emph{syntax error}, \kbd{gp} gives meaningful
2394
context by copying (part of) the expression it was trying to compile,
2395
indicating where the error occurred with a caret \kbd{\pow-}, as in
2396
\bprog
2397
? factor()
2398
*** too few arguments: factor()
2399
*** ^-
2400
? 1+
2401
*** syntax error, unexpected $end: 1+
2402
*** ^-
2403
@eprog\noindent
2404
possibly enlarged to a full arrow given enough trailing context
2405
\bprog
2406
? if (isprime(1+, do_something())
2407
*** syntax error, unexpected ',': if(isprime(1+,do_something()))
2408
*** ^----------------
2409
@eprog\noindent
2410
These error messages may be mysterious, because \kbd{gp} cannot guess what
2411
you were trying to do, and the error may occur once \kbd{gp} has been
2412
sidetracked. The first error is straightforward: \kbd{factor} has one
2413
mandatory argument, which is missing.
2414
2415
The other two are simple typos involving an ill-formed addition
2416
\kbd{1 + } missing its second operand. The error messages differ
2417
because the parsing context is slightly different: in the first
2418
case we reach the end of input (\kbd{\$end}) while still expecting a token,
2419
and in the second one, we received an unexpected token (the comma).
2420
2421
Here is a more complicated one:
2422
\bprog
2423
? factor(x
2424
*** syntax error, unexpected $end, expecting )-> or ',' or ')': factor(x
2425
*** ^-
2426
@eprog\noindent
2427
The error is a missing parenthesis, but from \kbd{gp}'s point of view,
2428
you might as well have intended to give further arguments to \kbd{factor}
2429
(this is possible and useful, see the description of the function). In fact
2430
\kbd{gp} expected either a closing parenthesis, or a second argument
2431
separated from the first by a comma. And this is essentially what the error
2432
message says: we reached the end of the input (\kbd{\$end}) while expecting
2433
a \kbd{')'} or a \kbd{','}.
2434
2435
Actually, a third possibility is mentioned in the error message \kbd{)->},
2436
which could never be valid in the above context, but a subexpression like
2437
\kbd{(x)->sin(x)}, defining an inline closure would be valid, and the parser is
2438
not clever enough to rule that out, so we get the same message as in
2439
\bprog
2440
? (x
2441
*** syntax error, unexpected $end, expecting )-> or ',' or ')': (x
2442
*** ^-
2443
@eprog\noindent
2444
where all three proposed continuations would be valid.
2445
2446
\emph{Runtime errors} from the evaluator are nicer because they answer a
2447
correctly worded query, otherwise the bytecode compiler would have protested
2448
first; here is a slightly pathological case:
2449
\bprog
2450
? if (siN(x) < eps, do_something())
2451
*** at top-level: if(siN(x)<eps,do_someth
2452
*** ^--------------------
2453
*** not a function in function call
2454
@eprog\noindent (no arrow!) The code is syntactically correct and compiled
2455
correctly, even though the \kbd{siN} function, a typo for \kbd{sin}, was not
2456
defined at this point. When trying to evaluate the bytecode, however, it
2457
turned out that \kbd{siN} is still undefined so we cannot evaluate the
2458
function call \kbd{siN(x)}.
2459
2460
\emph{Library runtime errors} are even nicer because they have more
2461
mathematical content, which is easier to grasp than a parser's logic:
2462
\bprog
2463
? 1/Mod(2,4)
2464
*** at top-level: 1/Mod(2,4)
2465
*** ^---------
2466
*** _/_: impossible inverse in Fp_inv: Mod(2, 4).
2467
@eprog\noindent telling us that a runtime error occurred while evaluating the
2468
binary \kbd{/} operator (the \kbd{\_} surrounding the operator are
2469
placeholders), more precisely the \kbd{Fp\_inv} library function was fed
2470
the argument \kbd{Mod(2,4)} and could not invert it. More context is provided
2471
if the error occurs deep in the call chain:
2472
\bprog
2473
? f(x) = 1/x;
2474
? g(N) = for(i = -N, N, f(i + O(5)));
2475
? g(10)
2476
*** at top-level: g(10)
2477
*** ^-----
2478
*** in function g: for(i=-N,N,f(i))
2479
*** ^-----
2480
*** in function f: 1/x
2481
*** ^--
2482
*** _/_: impossible inverse in ginv: O(5).
2483
@eprog\noindent In this example, the debugger reports (at least) 3 enclosed
2484
frames: last (innermost) is the body of user function $f$, the body of $g$,
2485
and the top-level (global scope). In fact, the \kbd{for} loop in $g$'s body
2486
defines an extra frame, since there exist variables scoped to the loop body.
2487
2488
\subsec{Error recovery}\sidx{error recovery}\label{se:errorrec}
2489
2490
It is annoying to wait for a program to finish and find out the hard
2491
way that there was a mistake in it (like the division by 0 above), sending
2492
you back to the prompt. First you may lose some valuable intermediate data.
2493
Also, correcting the error may not be obvious; you might have to change your
2494
program, adding a number of extra statements and tests to narrow down
2495
the problem.
2496
2497
A different situation, still related to error recovery, is when you
2498
actually foresee that some error may occur, are unable to prevent it, but
2499
quite capable of recovering from it, given the chance. Examples include lazy
2500
factorization, where you knowingly use a pseudo prime $N$ as if it were
2501
prime; you may then encounter an ``impossible'' situation, but this would
2502
usually exhibit a factor of $N$, enabling you to refine the factorization and
2503
go on. Or you might run an expensive computation at low precision to guess
2504
the size of the output, hence the right precision to use. You can then
2505
encounter errors like ``precision loss in truncation'', e.g when trying to
2506
convert \kbd{1E1000}, known to $28$ digits of accuracy, to an integer; or
2507
``division by 0'', e.g inverting \kbd{0E1000} when all accuracy has been
2508
lost, and no significant digit remains. It would be enough to restart part of
2509
the computation at a slightly higher precision.
2510
2511
We now describe \tev{error trapping}, a useful mechanism which alleviates
2512
much of the pain in the first situation (the break loop debugger), and
2513
provides satisfactory ways out of the second one (the \tet{iferr} exception
2514
handler).
2515
2516
\subsec{Break loop}\label{se:break_loop}
2517
2518
A \tev{break loop} is a special debugging mode that you enter whenever a
2519
user interrupt (\kbd{Control-C}) or runtime error occurs, freezing the
2520
\kbd{gp} state, and preventing cleanup until you get out of the loop. By
2521
runtime error, we mean an error from the evaluator, the library or a user error
2522
(from \tet{error}), \emph{not} syntax errors. When a break loop starts, a
2523
prompt is issued (\kbd{break>}). You can type in a \kbd{gp} command, which is
2524
evaluated when you hit the \kbd{<Return>} key, and the result is printed as
2525
during the main \kbd{gp} loop, except that no history of results is kept. Then
2526
the break loop prompt reappears and you can type further commands as long as
2527
you do not exit the loop. If you are using readline, the history of commands is
2528
kept, and line editing is available as usual. If you type in a command that
2529
results in an error, you are sent back to the break loop prompt: errors do
2530
\var{not} terminate the loop.
2531
2532
To get out of a break loop, you can use \tet{next}, \tet{break}, \tet{return},
2533
or type \kbd{C-d} (\kbd{EOF}), any of which will let \kbd{gp} perform its
2534
usual cleanup, and send you back to the \kbd{gp} prompt. Note that \kbd{C-d}
2535
is slightly dangerous, since typing it \emph{twice} will not only send you
2536
back to the \kbd{gp} prompt, but to your shell prompt! (Since \kbd{C-d} at
2537
the \kbd{gp} prompt exits the gp session.)
2538
2539
If the break loop was started by a user interrupt \kbd{Control-C}, and not by
2540
an error, inputting an empty line, i.e hitting the \kbd{<Return>} key at the
2541
\kbd{break>} prompt, resumes the temporarily interrupted computation. A single
2542
empty line has no effect in case of a fatal error, to avoid getting get out of
2543
the loop prematurely, thereby losing valuable debugging data. Any of
2544
\tet{next}, \tet{break}, \tet{return}, or \kbd{C-d} will abort the computation
2545
and send you back to the \kbd{gp} prompt as above.
2546
2547
Break loops are useful as a debugging tool. You may inspect the values of
2548
\kbd{gp} variables to understand why an error occurred, or change
2549
\kbd{gp}'s state in the middle of a computation (increase debugging level,
2550
start storing results in a log file, set variables to different values\dots):
2551
hit \kbd{C-c}, type in your modifications, then let the computation go on as
2552
explained above. A break loop looks like this:
2553
\bprog
2554
? v = 0; 1/v
2555
*** at top-level: v=0;1/v
2556
*** ^--
2557
*** _/_: impossible inverse in gdiv: 0.
2558
*** Break loop (type 'break' to go back to the GP prompt)
2559
break>
2560
@eprog
2561
\noindent So the standard error message is printed first. The
2562
\kbd{break>} at the bottom is a prompt, and hitting \kbd{v} then
2563
\kbd{<Return>}, we see:
2564
\bprog
2565
break> v
2566
0
2567
@eprog\noindent explaining the problem. We could have typed any \kbd{gp}
2568
command, not only the name of a variable, of course. Lexically-scoped
2569
variables are accessible to the evaluator during the break loop:
2570
\bprog
2571
? for(v = -2, 2, print(1/v))
2572
-1/2
2573
-1
2574
*** at top-level: for(v=-2,2,print(1/v))
2575
*** ^----
2576
*** _/_: impossible inverse in gdiv: 0.
2577
*** Break loop (type 'break' to go back to the GP prompt)
2578
break> v
2579
0
2580
@eprog\noindent
2581
Even though loop indices are automatically lexically scoped and no longer
2582
exist when the break loop is run, enough debugging information is retained in
2583
the bytecode to reconstruct the evaluation context. Of course, when the error
2584
occurs in a nested chain of user function calls, lexically scoped variables are
2585
available only in the corresponding frame:
2586
\bprog
2587
? f(x) = 1/x;
2588
? g(x) = for(i = 1, 10, f(x+i));
2589
? for(j = -5,5, g(j))
2590
*** at top-level: for(j=-5,5,g(j))
2591
*** ^-----
2592
*** in function g: for(i=1,10,f(x+i))
2593
*** ^-------
2594
*** in function f: 1/x
2595
*** ^--
2596
*** _/_: impossible inverse in gdiv: 0.
2597
*** Break loop: type 'break' to go back to GP prompt
2598
break> [i,j,x] \\ @com the $x$ in $f$'s body.
2599
[i, j, 0]
2600
break> dbg_up \\ @com go up one frame
2601
*** at top-level: for(j=-5,5,g(j))
2602
*** ^-----
2603
*** in function g: for(i=1,10,f(x+i))
2604
*** ^-------
2605
break> [i,j,x] \\ @com the $x$ in $g$'s body, $i$ in the for loop.
2606
[5, j, -5]
2607
@eprog
2608
The following GP commands are available during a break loop to help debugging:
2609
2610
\tet{dbg_up}$(n)$: go up $n$ frames, as seen above.
2611
2612
\tet{dbg_down}$(n)$: go down $n$ frames, cancelling previous \kbd{dbg\_up}'s.
2613
2614
\tet{dbg_x}$(t)$: examine $t$, as \kbd{\bs x} but more flexible.
2615
2616
\tet{dbg_err}$()$: returns the current error context \typ{ERROR}. The error
2617
components often provide useful additional information:
2618
\bprog
2619
? O(2) + O(3)
2620
*** at top-level: O(2)+O(3)
2621
*** ^-----
2622
*** _+_: inconsistent addition t_PADIC + t_PADIC.
2623
*** Break loop: type 'break' to go back to GP prompt
2624
break> E = dbg_err()
2625
error("inconsistent addition t_PADIC + t_PADIC.")
2626
break> Vec(E)
2627
["e_OP", "+", O(2), O(3)]
2628
@eprog
2629
2630
\misctitle{Note} The debugger is enabled by default, and fires up as soon as
2631
a runtime error occurs. If you do not like this behavior, you may disable it by
2632
setting the default \tet{breakloop} to 0 in for \kbd{gprc}. A runtime error
2633
will send you back to the prompt. Note that the break loop is automatically
2634
disabled when running \kbd{gp} in non interactive mode, i.e.~when the program's
2635
standard input is not attached to a terminal.
2636
2637
\misctitle{Technical Note} When you enter a break loop due to a PARI stack
2638
overflow, the PARI stack is reset so that you can run commands. Otherwise the
2639
stack would immediately overflow again! Still, as explained above, you do not
2640
lose the value of any \kbd{gp} variable in the process.
2641
2642
\subsec{Protecting code}
2643
The expression
2644
2645
\kbd{iferr(\var{statements}, ERR, \var{recovery})}
2646
2647
\noindent evaluates and returns the value of \var{statements}, unless an
2648
error occurs during the evaluation in which case the value of \var{recovery}
2649
is returned. As in an if/else clause, with the difference that
2650
\var{statements} has been partially evaluated, with possible side effects.
2651
We shall give a lot more details about the \kbd{ERR} argument shortly; it is
2652
the name of a variable, lexically scoped to the \var{recovery} expression
2653
sequence, whose value is set by the exception handler to help the recovery
2654
code decide what to do about the error.
2655
2656
For instance one can define a fault tolerant inversion function as follows:
2657
\bprog
2658
? inv(x) = iferr(1/x, ERR, "oo") \\ ERR is unused...
2659
? for (i=-1,1, print(inv(i)))
2660
-1
2661
oo
2662
1
2663
@eprog\noindent Protected codes can be nested without adverse effect.
2664
Let's now see how \kbd{ERR} can be used; as written, \kbd{inv} is too
2665
tolerant:
2666
\bprog
2667
? inv("blah")
2668
%2 = "oo"
2669
@eprog Let's improve it by checking that we caught a ``division by 0''
2670
exception, and not an unrelated one like the type error \kbd{1 / "blah"}.
2671
\bprog
2672
? inv2(x) = {
2673
iferr(1/x,
2674
ERR, if (errname(ERR) != "e_INV", error(ERR)); "oo")
2675
}
2676
? inv2(0)
2677
%3 = "oo" \\ as before
2678
? inv2("blah")
2679
*** at top-level: inv2("blah")
2680
*** ^------------
2681
*** in function inv2: ...f(errname(ERR)!="e_INV",error(ERR));"oo")
2682
*** ^-----------------
2683
*** error: forbidden division t_INT / t_STR.
2684
@eprog\noindent In the \kbd{inv2("blah")} example, the error type was not
2685
expected, so we rethrow the exception: \kbd{error(ERR)} triggers the original
2686
error that we mistakenly trapped. Since the recovery code should always check
2687
whether the error is the one expected, this construction is very common and
2688
can be simplified to
2689
\bprog
2690
? inv3(x) = iferr(1/x,
2691
ERR, "oo",
2692
errname(ERR) == "e_INV")
2693
@eprog\noindent More generally
2694
2695
\kbd{iferr(\var{statements}, ERR, \var{recovery}, \var{predicate})}
2696
2697
\noindent only catches the exception if \var{predicate} (allowed to check
2698
various things about \kbd{ERR}, not only its name) is nonzero.
2699
2700
Rather than trapping everything, then rethrowing whatever we do not like, we
2701
advise to only trap errors of a specific kind, as above. Of course,
2702
sometimes, one just want to trap \emph{everything} because we do not know
2703
what to expect. The following function check whether \tet{install} works
2704
correctly in your \kbd{gp}:
2705
\bprog
2706
broken_install() =
2707
{ \\ can we install?
2708
iferr(install(addii,GG),
2709
ERR, return ("OS"));
2710
\\ can we use the installed function?
2711
iferr(if (addii(1,1) != 2, return("BROKEN")),
2712
ERR, return("USE"));
2713
return (0);
2714
}
2715
@eprog
2716
\noindent The function returns
2717
\kbd{OS} if the operating system does not support \kbd{install},
2718
\kbd{USE} if using an installed function triggers an error,
2719
\kbd{BROKEN} if the installed function did not behave as expected,
2720
and 0 if everything works.
2721
2722
The \kbd{ERR} formal parameter contains more useful data than just the error
2723
name, which we recovered using \kbd{errname(ERR)}. In fact, a \typ{ERROR}
2724
object usually has extra components, which can be accessed as
2725
\kbd{component(ERR,1)}, \kbd{component(ERR,2)}, and so on. Or globally by
2726
casting the error to a \typ{VEC}: \kbd{Vec(ERR)} returns the vector
2727
of all components at once. See \secref{se:iferr} for the list of all
2728
exception types, and the corresponding contents of \kbd{ERR}.
2729
2730
\section{Interfacing GP with other languages}
2731
\noindent
2732
The PARI library was meant to be interfaced with C programs. This specific
2733
use is dealt with extensively in the \emph{User's guide to the PARI library}.
2734
Of course, \kbd{gp} itself provides a convenient interpreter to execute
2735
rather intricate scripts (see \secref{se:programming}).
2736
2737
Scripts, when properly written, tend to be shorter and clearer than C
2738
programs, and are certainly easier to write, maintain or debug. You don't
2739
need to deal with memory management, garbage collection, pointers,
2740
declarations, and so on. Because of their intrinsic simplicity, they are more
2741
robust as well. They are unfortunately somewhat slower. Thus their use will
2742
remain complementary: it is suggested that you test and debug your algorithms
2743
using scripts, before actually coding them in C if speed is paramount.
2744
The GP2C compiler often eases this part.
2745
2746
The \kbd{install} command (see~\secref{se:install}) efficiently imports
2747
foreign functions for use under \kbd{gp}, which can of course be written
2748
using other libraries than PARI. Thus you may code only critical parts
2749
of your program in C, and still maintain most of the program as a GP script.
2750
2751
We are aware of three PARI-related Free Software packages to embed PARI in
2752
other languages. We \emph{neither endorse nor support} any of them, but you
2753
may want to give them a try if you are familiar with the languages they are
2754
based on. The first is the Python-based SAGE
2755
system (\url{http://sagemath.org/}). The second is the \tet{Math::Pari} Perl
2756
module (see any CPAN mirror), written by Ilya Zakharevich.
2757
Finally, Michael Stoll and Sam Steingold have integrated PARI into \tet{CLISP}
2758
(\url{http://clisp.cons.org/}), a Common Lisp implementation.
2759
2760
These provide interfaces to \kbd{gp} functions for use in
2761
\kbd{python}, \kbd{perl}, or \kbd{Lisp}\sidx{Perl}\sidx{Python}\sidx{Lisp}
2762
programs, respectively.
2763
2764
\section{Defaults}\sidx{defaults}
2765
\label{se:defaults}
2766
2767
\noindent There are many internal variables in \kbd{gp}, defining how the
2768
system will behave in certain situations, unless a specific override has been
2769
given. Most of them are a matter of basic customization (colors, prompt) and
2770
will be set once and for all in your \idx{preferences file} (see
2771
\secref{se:gprc}), but some of them are useful interactively (set timer on,
2772
increase precision, etc.).
2773
2774
The function used to manipulate these values is called \kbd{default}, which
2775
is described in \secref{se:default}. The basic syntax is
2776
2777
\kbd{default(\var{def}, \var{value})},
2778
2779
\noindent which sets the default \var{def} to \var{value}. In interactive
2780
use, most of these can be abbreviated using \kbd{gp} metacommands
2781
(mostly, starting with \b), which we shall describe in the next section.
2782
2783
Available defaults are described in the reference guide,
2784
\secref{se:gp_defaults}, the most important one being \tet{parisizemax}.
2785
Just be aware that typing \kbd{default} by itself will list all of them, as
2786
well as their current values (see \b{d}).
2787
2788
\misctitle{Note} The suffixes \kbd{k}, \kbd{M} or \kbd{G} can be appended to
2789
a \var{value} which is a numeric argument, with the effect of multiplying it
2790
by $10^3$, $10^6$ and $10^9$ respectively. Case is not taken into account
2791
there, so for instance \kbd{30k} and \kbd{30K} both stand for $30000$. This
2792
is mostly useful to modify or set the defaults \kbd{parisize} and
2793
\kbd{parisizemax} which typically involve a lot of trailing zeroes.
2794
2795
\misctitle{(somewhat technical) Note} As we saw in \secref{se:strings},
2796
the second argument to \kbd{default} is subject to string context
2797
expansion, which means you can use run-time values. In other words, something
2798
like
2799
\bprog
2800
a = 3;
2801
default(logfile, "file" a ".log")
2802
@eprog
2803
logs the output in \kbd{file3.log}.
2804
2805
Some special defaults, corresponding to file names and prompts, expand further
2806
the resulting value at the time they are set. Two kinds of expansions may be
2807
performed:
2808
2809
\item \teb{time expansion}: the string is sent through the library
2810
function \tet{strftime}. This means that \kbd{\%}\var{char} combinations have
2811
a special meaning, usually related to the time and date. For instance,
2812
\kbd{\%H} = hour (24-hour clock) and \kbd{\%M} = minute [00,59] (on a Unix
2813
system, you can try \kbd{man strftime} at your shell prompt to get a complete
2814
list). This is applied to \kbd{prompt} and \kbd{logfile}. For
2815
instance,
2816
2817
\kbd{default(prompt,"(\%H:\%M) ? ")}
2818
2819
\noindent
2820
will prepend the time of day, in the form \kbd{(\var{hh}:\var{mm})}
2821
to \kbd{gp}'s usual prompt.
2822
2823
\item \teb{environment expansion}: When the string contains a sequence of
2824
the form \kbd{\$\var{SOMEVAR}}, e.g.~\kbd{\$HOME}, the environment is
2825
searched and if \var{SOMEVAR} is defined, the sequence is replaced by the
2826
corresponding value. Also the \kbd{\til} symbol has the same meaning as in
2827
many shells~--- \kbd{\til} by itself stands for your home directory, and
2828
\kbd{\til{}user} is expanded to \kbd{user}'s home directory. This is applied
2829
to all file names\sidx{filename}. \label{se:envir}
2830
2831
\section{Simple metacommands}\label{se:meta}
2832
2833
\noindent
2834
Simple metacommands are meant as shortcuts and should not be used in GP
2835
scripts (see \secref{se:programming}). Beware that these, as all of \kbd{gp}
2836
input, are \emph{case sensitive}. For example, \b{Q} is not identical to
2837
\b{q}. Two kinds of arguments are allowed: numbers (denoted $n$ below)
2838
and names (denoted \var{filename} below); braces are used to denote optional
2839
arguments, , e.g.~$\{n\}$ means that a numeric argument is expected but
2840
can be omitted. Names can be optionally surrounded by double quotes
2841
and in this case can contain whitespace, e.g. \kbd{"a b"} and are treated
2842
as ordinary character strings, see \secref{se:strings} for details.
2843
2844
Whitespace (or spaces) between the metacommand and its arguments and within
2845
unquoted arguments is optional. (This can cause problems with \b{w},
2846
when you insist on having a file name whose first character is a digit, and
2847
with \b{r} or \b{w}, if the file name itself contains a space. In such cases,
2848
just quote filenames or use the underlying \tet{read} or \tet{write} function).
2849
2850
\subseckbd{?$\{\var{command}\}$} The \kbd{gp} on-line help interface.
2851
If you type \kbd{?$n$} where $n$ is a number from 1 to 11, you will get the
2852
list of functions in Section $3.n$ of the manual (the list of sections being
2853
obtained by simply typing \kbd{?}). \label{se:exthelp}
2854
2855
These names are in general not informative enough. More details can be
2856
obtained by typing \kbd{?\var{function}}, which gives a short explanation of
2857
the function's calling convention and effects. A help string is also attached
2858
to a symbolic operator, where arguments are replaced by a placeholder
2859
character \kbd{\_}:
2860
\bprog
2861
? ?sin
2862
sin(x): sine of x.
2863
2864
? ?_*_
2865
x*y: product of x and y.
2866
2867
? ?!_
2868
!a: boolean operator "not".
2869
2870
? ?_!
2871
n!: factorial of n.
2872
2873
? ? _^_
2874
x^y: compute x to the power y.
2875
@eprog\noindent Of course, to have complete information, read Chapter 3 of
2876
this manual. The source code is at your disposal as well, though a trifle
2877
less readable.
2878
2879
If the line before the copyright message indicates that extended help is
2880
available (this means \kbd{perl} is present on your system and the PARI
2881
distribution was correctly installed), you can add more \kbd{?} signs for
2882
extended functionality:
2883
2884
\kbd{??~\var{keyword}} yields the function description as it stands in this
2885
manual, usually in Chapter~2 or~3. If you're not satisfied with the default
2886
chapter chosen, you can impose a given chapter by ending the keyword with
2887
\kbd{@} followed by the chapter number, e.g.~\kbd{??~Hello@2} will look in
2888
Chapter~2 for section heading \kbd{Hello} (which doesn't exist, by the way).
2889
2890
All operators (e.g.~\kbd{+}, \kbd{\&\&}, etc.) are accepted by this
2891
extended help, as well as a few other keywords describing key \kbd{gp} concepts,
2892
e.g.~\kbd{readline} (the line editor), \kbd{integer}, \kbd{nf} (``number
2893
field'' as used in most algebraic number theory computations), \kbd{ell}
2894
(elliptic curves), etc.
2895
2896
In case of conflicts between \emph{function} and \emph{default} names (e.g
2897
\tet{log}, \tet{simplify}), the function has higher priority. To get the
2898
\emph{default} help, use
2899
\bprog
2900
?? default(log)
2901
?? default(simplify)
2902
@eprog
2903
2904
\kbd{???~\var{pattern}} produces a list of sections in Chapter~3 of the
2905
manual related to your query. As before, if \var{pattern} ends by \kbd{@}
2906
followed by a chapter number, that chapter is searched instead; you also
2907
have the option to append a simple \kbd{@} (without a chapter number) to
2908
browse through the whole manual.
2909
2910
If your query contains dangerous characters (e.g \kbd{?} or blanks) it is
2911
advisable to enclose it within double quotes, as for GP strings (e.g
2912
\kbd{???~"elliptic curve"}).
2913
2914
Note that extended help is more powerful than the short help, since
2915
it knows about operators as well: you can type \kbd{??~*} or
2916
\kbd{??~\&\&}, whereas a single \kbd{?} would just yield a not too helpful
2917
\bprog
2918
&&: unknown identifier.
2919
@eprog\noindent message. Also, you can ask for extended help on section
2920
number~$n$ in Chapter~3, just by typing \kbd{??~$n$} (where \kbd{?$n$} would
2921
yield merely a list of functions). Finally, a few key concepts in \kbd{gp} are
2922
documented in this way: metacommands (e.g \kbd{??~"??"}), defaults (e.g
2923
\kbd{??~default(log)}) not to be mistaken with \kbd{??~log} (the natural
2924
logarithm) and type names (e.g \typ{INT} or \kbd{integer}), as well as
2925
various miscellaneous keywords such as \kbd{edit} (short summary of line
2926
editor commands), \kbd{operator}, \kbd{member}, \kbd{"user defined"},
2927
\kbd{nf}, \kbd{ell}, \dots
2928
2929
Last but not least: \kbd{??} without argument will open a \kbd{dvi}
2930
previewer (\kbd{xdvi} by default, \kbd{\$GPXDVI} if it is defined in your
2931
environment) containing the full user's manual. \kbd{??tutorial} and
2932
\kbd{??refcard} do the same with the \idx{tutorial} and \idx{reference card}
2933
respectively.
2934
2935
\misctitle{Technical note} This functionality is provided by an
2936
external \kbd{perl} script that you are free to use outside any \kbd{gp} session
2937
(and modify to your liking, if you are perl-knowledgeable). It is called
2938
\tet{gphelp}, lies in the \kbd{doc} subdirectory of your distribution
2939
(just make sure you run \kbd{Configure} first, see Appendix~A) and is
2940
really two programs in one. The one which is used from within \kbd{gp} is
2941
\kbd{gphelp} which runs \TeX\ on a selected part of this manual, then opens
2942
a previewer. \kbd{gphelp -detex} is a text mode equivalent, which looks
2943
often nicer especially on a colour-capable terminal (see
2944
\kbd{misc/gprc.dft} for examples). The default \kbd{help} selects which
2945
help program will be used from within \kbd{gp}. You are welcome to improve this
2946
help script, or write new ones (and we would like to know about it
2947
so that we may include them in future distributions). By the way, outside
2948
of \kbd{gp} you can give more than one keyword as argument to \kbd{gphelp}.
2949
2950
\subseckbd{/*...*/} A comment. Everything between the stars is ignored by
2951
\kbd{gp}. These comments can span any number of lines.
2952
2953
\subseckbd{\bs\bs} A one-line comment. The rest of the line
2954
is ignored by \kbd{gp}.
2955
2956
\subsec{\b{a} $\{n\}$} Prints the object number $n$ ($\%n$)
2957
in raw format. If the number $n$ is omitted, print the latest computed object
2958
($\%$). \label{se:history}
2959
2960
\subsec{\b{c}}\sidx{available commands} Prints the list of all available
2961
hardcoded functions under \kbd{gp}, not including operators written as special
2962
symbols (see \secref{se:operators}). More information can be obtained using
2963
the \kbd{?} metacommand (see above). For user-defined functions / member
2964
functions, see \b{u} and \b{um}.
2965
2966
\subsec{\b{d}} Prints the \idx{defaults} as described in the
2967
previous section (shortcut for \kbd{default()}, see \secref{se:default}).
2968
2969
\subsec{\b{e} $\{n\}$} Switches the \tet{echo} mode on (1) or off (0). If
2970
$n$ is explicitly given, set echo to $n$.
2971
2972
\subsec{\b{g} $\{n\}$} Sets the debugging level \tet{debug} to the
2973
nonnegative integer $n$. Diagnostics attached to a particular feature can be
2974
turned on and off using \kbd{setdebug}.
2975
2976
\subsec{\b{gf} $\{n\}$} Sets the file usage debugging level \tet{debugfiles}
2977
to the nonnegative integer $n$.
2978
2979
\subsec{\b{gm} $\{n\}$} Sets the memory debugging level \tet{debugmem}
2980
to the nonnegative integer $n$.
2981
2982
\subsec{\b{h} $\{m$\kbd{-}$n\}$} Outputs some debugging info about the
2983
hashtable. If the argument is a number $n$, outputs the contents of cell
2984
$n$. Ranges can be given in the form $m$\kbd{-}$n$ (from cell $m$ to cell
2985
$n$, \$ = last cell). If a function name is given instead of a number or
2986
range, outputs info on the internal structure of the hash cell this
2987
function occupies (a \kbd{struct entree} in C). If the range is reduced to
2988
a dash ('\kbd{-}'), outputs statistics about hash cell usage.
2989
2990
\subsec{\b{l} $\{$\var{logfile}$\}$} Switches \tet{log} mode on and off.
2991
If a \var{logfile} argument is given, change the default logfile name to
2992
\var{logfile} and switch log mode on.
2993
2994
\subsec{\b{m}} As \b{a}, but using prettymatrix format.
2995
2996
\subsec{\b{o} $\{n\}$} Sets \tet{output} mode to $n$ ($0$: raw, $1$:
2997
prettymatrix, $3$: external prettyprint).
2998
2999
\subsec{\b{p} $\{n\}$} Sets \tet{realprecision} to $n$ decimal digits.
3000
Prints its current value if $n$ is omitted.
3001
3002
\subsec{\b{pb} $\{n\}$} Sets \tet{realbitprecision} to $n$ bits.
3003
Prints its current value if $n$ is omitted.
3004
3005
\subsec{\b{ps} $\{n\}$} Sets \tet{seriesprecision} to $n$ significant terms.
3006
Prints its current value if $n$ is omitted.
3007
3008
\subsec{\b{q}} Quits the \kbd{gp} session and returns to the system.
3009
Shortcut for \tet{quit}\kbd{()} (see \secref{se:quit}).
3010
3011
\subsec{\b{r} $\{$\var{filename}$\}$} Reads into \kbd{gp} all the
3012
commands contained in the named file as if they had been typed from the
3013
keyboard, one line after the other. Can be used in combination with the \b{w}
3014
command (see below). Related but not equivalent to the function \kbd{read}
3015
(see \secref{se:read}); in particular, if the file contains more than one
3016
line of input, there will be one history entry for each of them, whereas
3017
\kbd{read} would only record the last one. If \var{filename} is omitted,
3018
re-read the previously used input file (fails if no file has ever been
3019
successfully read in the current session). If a \kbd{gp} \tet{binary file}
3020
(see \secref{se:writebin}) is read using this command, it is silently loaded,
3021
without cluttering the history.
3022
3023
Assuming \kbd{gp} figures how to decompress files on your machine, this
3024
command accepts compressed files in \tet{compress}ed (\kbd{.Z}) or
3025
\tet{gzip}ped (\kbd{.gz} or \kbd{.z}) format. They will be uncompressed on
3026
the fly as \kbd{gp} reads them, without changing the files themselves.
3027
3028
\subsec{\b{s}} Prints the state of the PARI \tev{stack} and \tev{heap}.
3029
This is used primarily as a debugging device for PARI.
3030
3031
\subsec{\b{t}} Prints the \idx{internal longword format} of all the PARI
3032
types. The detailed bit or byte format of the initial codeword(s) is
3033
explained in Chapter~4, but its knowledge is not necessary for a \kbd{gp}
3034
user.
3035
3036
\subsec{\b{u}} Prints the definitions of all user-defined functions.
3037
3038
\subsec{\b{um}} Prints the definitions of all user-defined member functions.
3039
3040
\subsec{\b{v}} Prints the \idx{version number} and implementation architecture
3041
(680x0, Sparc, Alpha, other) of the \kbd{gp} executable you are using.
3042
3043
\subsec{\b{w} $\{n\}$ $\{$\var{filename}$\}$} Writes the object number
3044
$n$ ( $\%n$ ) into the named file, in raw format. If the number $n$ is
3045
omitted, writes the latest computed object ( $\%$ ). If \var{filename} is
3046
omitted, appends to \kbd{logfile} (the GP function \tet{write} is a trifle more
3047
powerful, as you can have arbitrary file names).
3048
3049
\subsec{\b{x} $\{n\}$} Prints the complete tree with addresses and contents
3050
(in hexadecimal) of the \idx{internal representation} of the object number
3051
$n$ ( $\%n$ ). If the number $n$ is omitted, uses the latest computed object
3052
in \kbd{gp}. As for \b{s}, this is used primarily as a debugging device for
3053
PARI, and the format should be self-explanatory. The underlying GP
3054
function \tet{dbg_x} is more versatile, since it can be applied to other
3055
objects than history entries.
3056
3057
\subsec{\b{y} $\{n\}$} Switches \kbd{simplify} on (1) or off (0). If $n$
3058
is explicitly given, set simplify to $n$.
3059
3060
\subseckbd{\#} Switches the \kbd{timer} on or off.
3061
3062
\subseckbd{\#\#} Prints the time taken by the latest computation.
3063
Useful when you forgot to turn on the \kbd{timer}.
3064
3065
\section{The preferences file}\sidx{startup}\sidx{preferences file}
3066
\label{se:gprc}
3067
3068
This file, called \tet{gprc} in the sequel, is used to modify or extend
3069
\kbd{gp} default behavior, in all \kbd{gp} sessions: e.g customize
3070
\kbd{default} values or load common user functions and aliases. \kbd{gp}
3071
opens the \kbd{gprc} file and processes the commands in there, \emph{before}
3072
doing anything else, e.g.~creating the PARI stack. If the file does not exist
3073
or cannot be read, \kbd{gp} will proceed to the initialization phase at once,
3074
eventually emitting a prompt. If any explicit command line switches are
3075
given, they override the values read from the preferences file.
3076
3077
\subsec{Syntax} The syntax in the \kbd{gprc} file (and valid in this file
3078
only) is simple-minded, but should be sufficient for most purposes. The file
3079
is read line by line; as usual, white space is ignored unless surrounded by
3080
quotes and the standard multiline constructions using braces, \kbd{\bs}, or
3081
\kbd{=} are available (multiline comments between \kbd{/*~\dots~*/} are also
3082
recognized).
3083
3084
\subsubsec{Preprocessor:}
3085
Two types of lines are first dealt with by a preprocessor:
3086
3087
\item comments are removed. This applies to all text surrounded by
3088
\kbd{/*~\dots~*/} as well as to everything following \kbd{\bs\bs} on a given
3089
line.
3090
3091
\item lines starting with \kbd{\#if} \var{boolean} are treated as
3092
comments if \var{boolean} evaluates to \kbd{false}, and read normally
3093
otherwise. The condition can be negated using either \kbd{\#if not} (or
3094
\kbd{\#if !}). If the rest of the current line is empty, the test applies to
3095
the next line (same behavior as \kbd{=} under \kbd{gp}). The following
3096
tests can be performed:
3097
3098
\kbd{EMACS}: \kbd{true} if \kbd{gp} is running in an Emacs or TeXmacs shell (see
3099
\secref{se:emacs}).
3100
3101
\kbd{READL}: \kbd{true} if \kbd{gp} is compiled with \kbd{readline} support (see
3102
\secref{se:readline}).
3103
3104
\kbd{VERSION} \var{op} \var{number}: where \var{op} is in the set
3105
$\{ \kbd{>}, \kbd{<}, \kbd{<=}, \kbd{>=} \}$, and \var{number} is a PARI
3106
version number of the form \var{Major}.\var{Minor}.\var{patch}, where the
3107
last two components can be omitted (i.e.~$1$ is understood as version $1.0.0$).
3108
This is \kbd{true} if \kbd{gp}'s version number satisfies the required
3109
inequality.
3110
3111
\kbd{BITS\_IN\_LONG} \kbd{==} \var{number}: \var{number} is $32$ (resp.~$64$).
3112
This is \kbd{true} if \kbd{gp} was built for a 32-bit (resp.~64-bit)
3113
architecture.
3114
3115
\subsubsec{Commands:}
3116
After preprocessing, the remaining lines are executed as
3117
sequence of expressions (as usual, separated by \kbd{;} if necessary). Only
3118
two kinds of expressions are recognized:
3119
3120
\item \var{default} \kbd{=} \var{value}, where \var{default} is one of
3121
the available defaults (see \secref{se:defaults}), which will be set to
3122
\var{value} on actual startup. Don't forget the quotes around strings
3123
(e.g.~for \kbd{prompt} or \kbd{help}).
3124
3125
\item \kbd{read "\var{some\_GP\_file}"} where \kbd{\var{some\_GP\_file}}
3126
is a regular GP script this time, which will be read just before \kbd{gp}
3127
prompts you for commands, but after initializing the defaults. In particular,
3128
file input is delayed until the \kbd{gprc} has been fully loaded. This is the
3129
right place to input files containing \kbd{alias} commands, or your favorite
3130
macros.
3131
3132
\noindent For instance you could set your prompt in the following portable way:
3133
\bprog
3134
\\ self modifying prompt looking like @com\hbox{\rm(18:03) \key{gp}\kbd{ >}}
3135
prompt = "(%H:%M) \e[1mgp\e[m > "
3136
3137
\\ readline wants nonprinting characters to be braced between ^A/^B pairs
3138
#if READL prompt = "(%H:%M) ^A\e[1m^Bgp^A\e[m^B > "
3139
3140
\\ escape sequences not supported under emacs
3141
#if EMACS prompt = "(%H:%M) gp > "
3142
@eprog
3143
3144
\noindent Note that any of the last two lines could be broken in the
3145
following way
3146
\bprog
3147
#if EMACS
3148
prompt = "(%H:%M) gp > "
3149
@eprog
3150
\noindent since the preprocessor directive applies to the next line if the
3151
current one is empty.
3152
3153
A sample \kbd{gprc} file called \kbd{misc/gprc.dft} is provided in the
3154
standard distribution. It is a good idea to have a look at it and customize
3155
it to your needs. Since this file does not use multiline constructs, here is
3156
one (note the terminating \kbd{;} to separate the expressions):
3157
\bprog
3158
#if VERSION > 2.2.3
3159
{
3160
read "my_scripts"; \\ syntax errors in older versions
3161
new_galois_format = 1; \\ default introduced in 2.2.4
3162
}
3163
#if ! EMACS
3164
{
3165
colors = "9, 5, no, no, 4, 1, 2";
3166
help = "gphelp -detex -ch 4 -cb 0 -cu 2";
3167
}
3168
@eprog
3169
3170
\subsec{The gprc location}
3171
When \kbd{gp} is started, it looks for a customization file, or \kbd{gprc} in
3172
the following places (in this order, only the first one found will be
3173
loaded):
3174
3175
\noindent\item \kbd{gp} checks whether the environment variable
3176
\tet{GPRC} is set. On Unix, this can be done with something like: \smallskip
3177
3178
\settabs\+\indent&\kbd{GPRC=/my/dir/anyname; export GPRC}\quad&\cr
3179
3180
\+&\kbd{GPRC=/my/dir/anyname; export GPRC}\quad&in \kbd{sh} syntax
3181
(for instance in your \kbd{.profile}),\cr
3182
3183
\+&\kbd{setenv GPRC /my/dir/anyname} &in \kbd{csh} syntax
3184
(in your \kbd{.login} or \kbd{.cshrc} file).\cr
3185
3186
\+&\kbd{env GPRC=/my/dir/anyname gp} &on the command line launching \kbd{gp}.\cr
3187
3188
\noindent If so, the file named by \kbd{\$GPRC} is the \kbd{gprc}.
3189
3190
\noindent\item If \kbd{GPRC} is not set, and if the environment variable
3191
\kbd{HOME} is defined, \kbd{gp} then tries
3192
3193
\kbd{\$HOME/.gprc} on a Unix system
3194
3195
\kbd{\$HOME\bs gprc.txt} on a DOS, OS/2, or Windows system.
3196
3197
\noindent\item If no gprc was found among the user files mentioned above
3198
we look for \kbd{/etc/gprc} for a system-wide gprc file (you will need root
3199
privileges to set up such a file yourself).
3200
3201
\noindent\item Finally, we look in pari's \kbd{datadir} for a file named
3202
3203
\kbd{.gprc} on a Unix system
3204
3205
\kbd{gprc.txt} on a DOS, OS/2, or Windows system. If you are using our
3206
Windows installer, this is where the default preferences file is written.
3207
3208
\noindent Note that on Unix systems, the \kbd{gprc}'s default name starts
3209
with a '.' and thus is hidden to regular \kbd{ls} commands; you need to type
3210
\kbd{ls -a} to list it.
3211
3212
3213
\section{Using readline} \sidx{line editor}\sidx{completion}
3214
3215
This very useful library provides line editing and contextual completion
3216
to \kbd{gp}. You are encouraged to read the \kbd{readline} user manual,
3217
but we describe basic usage here.
3218
3219
\misctitle{A (too) short introduction to readline}\label{se:readline}
3220
In the following, \kbd{C-} stands for ``the \kbd{Control} key combined with
3221
another'' and the same for \kbd{M-} with the \kbd{Meta} key; generally
3222
\kbd{C-} combinations act on characters, while the \kbd{M-} ones operate on
3223
words. The \kbd{Meta} key might be called \kbd{Alt} on some keyboards, will
3224
display a black diamond on most others, and can safely be replaced by
3225
\kbd{Esc} in any case.
3226
3227
Typing any ordinary key inserts text where the cursor stands, the arrow keys
3228
enabling you to move in the line. There are many more movement commands,
3229
which will be familiar to the Emacs user, for instance \kbd{C-a}/\kbd{C-e}
3230
will take you to the start/end of the line, \kbd{M-b}/\kbd{M-f} move the
3231
cursor backward/forward by a word, etc. Just press the \kbd{<Return>} key at
3232
any point to send your command to \kbd{gp}.
3233
3234
All the commands you type at the \kbd{gp} prompt are stored in a history,
3235
a multiline command being saved as a single concatenated line. The Up and Down
3236
arrows (or \kbd{C-p}/\kbd{C-n}) will move you through the history,
3237
\kbd{M-<}/\kbd{M->} sending you to the start/end of the history.
3238
\kbd{C-r}/\kbd{C-s} will start an incremental backward/forward search. You
3239
can kill text (\kbd{C-k} kills till the end of line, \kbd{M-d} to the end of
3240
current word) which you can then yank back using the \kbd{C-y} key (\kbd{M-y}
3241
will rotate the kill-ring). \kbd{C-\_} will undo your last changes
3242
incrementally (\kbd{M-r} undoes all changes made to the current line).
3243
\kbd{C-t} and \kbd{M-t} will transpose the character (word) preceding the
3244
cursor and the one under the cursor.
3245
3246
Keeping the \kbd{M-} key down while you enter an integer (a minus sign
3247
meaning reverse behavior) gives an argument to your next readline command
3248
(for instance \kbd{M-- C-k} will kill text back to the start of line). If you
3249
prefer \idx{Vi}--style editing, \kbd{M-C-j} will toggle you to Vi mode.
3250
3251
Of course you can change all these default bindings. For that you need to
3252
create a file named \kbd{.inputrc} in your home directory. For instance
3253
(notice the embedding conditional in case you would want specific bindings
3254
for \kbd{gp}):
3255
%
3256
\bprog
3257
$if Pari-GP
3258
set show-all-if-ambiguous
3259
"\C-h": backward-delete-char
3260
"\e\C-h": backward-kill-word
3261
"\C-xd": dump-functions
3262
(: "\C-v()\C-b" #@com can be annoying when copy-pasting!
3263
[: "\C-v[]\C-b"
3264
$endif
3265
@eprog
3266
\noindent\kbd{C-x C-r} will re-read this init file, incorporating any
3267
changes made to it during the current session.
3268
3269
\misctitle{Note} By default, \kbd{(} and \kbd{[} are bound to the function
3270
\kbd{pari-matched-insert} which, if ``electric parentheses'' are enabled
3271
(default: off) will automatically insert the matching closure (respectively
3272
\kbd{)} and \kbd{]}). This behavior can be toggled on and off by giving
3273
the numeric argument $-2$ to \kbd{(} (\kbd{M--2(}), which is useful if you
3274
want, e.g to copy-paste some text into the calculator. If you do not want a
3275
toggle, you can use \kbd{M--0} / \kbd{M--1} to specifically switch it on or
3276
off).
3277
3278
\misctitle{Note} In some versions of readline (2.1 for instance), the
3279
\kbd{Alt} or \kbd{Meta} key can give funny results (output 8-bit accented
3280
characters for instance). If you do not want to fall back to the \kbd{Esc}
3281
combination, put the following two lines in your \kbd{.inputrc}:
3282
%
3283
\bprog
3284
set convert-meta on
3285
set output-meta off
3286
@eprog
3287
3288
\misctitle{Command completion and online help} Hitting
3289
\kbd{<TAB>} will complete words for you. This mechanism is context-dependent:
3290
\kbd{gp} will strive to only give you meaningful completions in a given
3291
context (it will fail sometimes, but only under rare and restricted
3292
conditions).
3293
3294
For instance, shortly after a \kbd{\til}, we expect a user name, then a
3295
path to some file. Directly after \kbd{default(} has been typed, we would
3296
expect one of the \kbd{default} keywords. After a '.', we expect a member
3297
keyword. And generally of course, we expect any GP symbol which may be found
3298
in the hashing lists: functions (both yours and GP's), and variables.
3299
3300
If, at any time, only one completion is meaningful, \kbd{gp} will provide it
3301
together with
3302
3303
\item an ending comma if we are completing a default,
3304
3305
\item a pair of parentheses if we are completing a function name. In
3306
that case hitting \kbd{<TAB>} again will provide the argument list as given
3307
by the online help. (Recall that you can always undo the effect
3308
of the preceding keys by hitting \kbd{C-\_}; this applies here.)
3309
3310
Otherwise, hitting \kbd{<TAB>} once more will give you the list of possible
3311
completions. Just experiment with this mechanism as often as possible,
3312
you will probably find it very convenient. For instance, you can obtain
3313
\kbd{default(seriesprecision,10)}, just by hitting \kbd{def<TAB>se<TAB>10},
3314
which saves 18 keystrokes (out of 27).
3315
3316
Hitting \kbd{M-h} will give you the usual short online help concerning the
3317
word directly beneath the cursor, \kbd{M-H} will yield the extended help
3318
corresponding to the \kbd{help} default program (usually opens a \idx{dvi}
3319
previewer, or runs a primitive tex-to-ASCII program). None of these disturb
3320
the line you were editing.
3321
3322
\section{GNU Emacs and PariEmacs}
3323
\label{se:emacs}
3324
3325
If you install the PariEmacs package (see Appendix A), you may use \kbd{gp}
3326
as a subprocess in \idx{Emacs}. You then need to include in your \kbd{.emacs}
3327
file the following lines:
3328
\bprog
3329
(autoload 'gp-mode "pari" nil t)
3330
(autoload 'gp-script-mode "pari" nil t)
3331
(autoload 'gp "pari" nil t)
3332
(autoload 'gpman "pari" nil t)
3333
3334
(setq auto-mode-alist
3335
(cons '("\\.gp$" . gp-script-mode) auto-mode-alist))
3336
@eprog
3337
\noindent which autoloads functions from the PariEmacs package and ensures
3338
that file with the \kbd{.gp} suffix are edited in gp-script mode.
3339
3340
Once this is done, under GNU Emacs if you type \kbd{M-x gp} (where as usual
3341
\kbd{M} is the \kbd{Meta} key), a special shell will be started launching
3342
\kbd{gp} with the default stack size and prime limit. You can then work as
3343
usual under \kbd{gp}, but with all the facilities of an advanced text editor.
3344
See the PariEmacs documentation for customizations, menus, etc.
3345
3346
\newpage
3347
3348