Testing latest pari + WASM + node.js... and it works?! Wow.
License: GPL3
ubuntu2004
% Copyright (c) 2000 The PARI Group1%2% This file is part of the PARI/GP documentation3%4% Permission is granted to copy, distribute and/or modify this document5% under the terms of the GNU General Public License6\chapter{Overview of the PARI system}78\section{Introduction}910\noindent11PARI/GP is a specialized computer algebra system, primarily aimed at number12theorists, but has been put to good use in many other different fields, from13topology or numerical analysis to physics.1415Although quite an amount of symbolic manipulation is possible, PARI does16badly compared to systems like Axiom, Magma, Maple, Mathematica, Maxima, or17Reduce on such tasks, e.g.~multivariate polynomials, formal integration,18etc. On the other hand, the three main advantages of the system are its19speed, the possibility of using directly data types which are familiar to20mathematicians, and its extensive algebraic number theory module (from21the above-mentioned systems, only Magma provides similar features).2223Non-mathematical strong points include the possibility to program either24in high-level scripting languages or with the PARI library, a mature system25(development started in the mid eighties) that was used to conduct and26disseminate original mathematical research, while building a large user27community, linked by helpful mailing lists and a tradition of great user28support from the developers. And, of course, PARI/GP is Free Software,29covered by the GNU General Public License, either version 2 of the License or30(at your option) any later version.3132PARI is used in three different ways:3334\quad 1) as a library \tet{libpari}, which can be called from an upper-level35language application, for instance written in ANSI C or C$++$;3637\quad 2) as a sophisticated programmable calculator, named \tet{gp}, whose38language \tet{GP} contains most of the control instructions of a standard39language like C;4041\quad 3) the compiler \tet{gp2c} translates GP code to C, and loads it into42the \kbd{gp} interpreter. A typical script compiled by \kbd{gp2c} runs 3 to 1043times faster. The generated C code can be edited and optimized by hand. It44may also be used as a tutorial to \kbd{libpari} programming.4546The present Chapter 1 gives an overview of the PARI/GP system; \kbd{gp2c} is47distributed separately and comes with its own manual. Chapter 2 describes the48\kbd{GP} programming language and the \kbd{gp} calculator. Chapter 349describes all routines available in the calculator. Programming in library50mode is explained in Chapters 4 and 5 in a separate booklet: \emph{User's51Guide to the PARI library} (\kbd{libpari.dvi}.5253A tutorial for \kbd{gp} is provided in the standard distribution: \emph{A54tutorial for PARI/GP} (\kbd{tutorial.dvi}) and you should read this first.55You can then start over and read the more boring stuff which lies ahead. You56can have a quick idea of what is available by looking at the \kbd{gp}57reference card (\kbd{refcard.dvi} or \kbd{refcard.ps}). In case of need, you58can refer to the complete function description in Chapter 3.5960\subsectitle{How to get the latest version} Everything can be found on61PARI's home page:62$$\url{http://pari.math.u-bordeaux.fr/}.$$63%64From that point you may access all sources, some binaries,65version information, the complete mailing list archives, frequently asked66questions and various tips. All threaded and fully searchable.6768\subsectitle{How to report bugs} Bugs are submitted online to our Bug69Tracking System, available from PARI's home page, or directly from the URL70$$\url{http://pari.math.u-bordeaux.fr/Bugs/}.$$71%72Further instructions can be found on that page.7374\section{Multiprecision kernels / Portability}7576The PARI multiprecision kernel comes in three non exclusive flavors. See77Appendix~A for how to set up these on your system; various compilers are78supported, but the GNU \kbd{gcc} compiler is the definite favorite.7980A first version is written entirely in ANSI C, with a C++-compatible syntax,81and should be portable without trouble to any 32 or 64-bit computer having no82drastic memory constraints. We do not know any example of a computer where a83port was attempted and failed.8485In a second version, time-critical parts of the kernel are written in86inlined assembler. At present this includes8788\item the whole ix86 family (Intel, AMD, Cyrix) starting at the 386, up to89the Xbox gaming console, including the Opteron 64 bit processor.9091\item three versions for the Sparc architecture: version 7, version 8 with92SuperSparc processors, and version 8 with MicroSparc I or II processors.93UltraSparcs use the MicroSparc II version;9495\item the DEC Alpha 64-bit processor;9697\item the Intel Itanium 64-bit processor;9899\item the PowerPC equipping old macintoshs (G3, G4, etc.);100101\item the HPPA processors (both 32 and 64 bit);102103A third version uses the GNU MP library to implement most of its104multiprecision kernel. It improves significantly on the native one for large105operands, say 100 decimal digits of accuracy or more. You \emph{should}106enable it if GMP is present on your system. Parts of the first version are107still in use within the GMP kernel, but are scheduled to disappear.108109A historical version of the PARI/GP kernel, written in 1985, was specific to110680x0 based computers, and was entirely written in MC68020 assembly language.111It ran on SUN-3/xx, Sony News, NeXT cubes and on 680x0 based Macs. It is no112longer part of the PARI distribution; to run PARI with a 68k assembler113micro-kernel, use the GMP kernel!114115\section{The PARI types} \label{se:start}116117\noindent The GP language is not typed in the traditional sense; in118particular, variables have no type. In library mode, the type of all PARI119objects is \kbd{GEN}, a generic type. On the other hand, it is dynamically120typed: each object has a specific internal type, depending on the121mathematical object it represents.122123The crucial word is recursiveness: most of the PARI types are recursive. For124example, the basic internal type \typ{COMPLEX} exists. However, the125components (i.e.~the real and imaginary part) of such a ``complex number''126can be of any type. The only sensible ones are integers (we are then in127$\Z[i]$), rational numbers ($\Q[i]$), real numbers ($\R[i]=\C$), or even128elements of $\Z/n\Z$ (in $(\Z/n\Z)[t]/(t^2+1)$), or $p$-adic numbers when129$p\equiv 3 \mod 4$ ($\Q_{p}[i]$). This feature must not be used too rashly in130library mode: for example you are in principle allowed to create objects131which are ``complex numbers of complex numbers''. (This is not possible under132\kbd{gp}.) But do not expect PARI to make sensible use of such objects: you133will mainly get nonsense.134135On the other hand, it \emph{is} allowed to have components of different, but136compatible, types, which can be freely mixed in basic ring operations $+$ or137$\times$. For example, taking again complex numbers, the real part could be138an integer, and the imaginary part a rational number. On the other hand, if139the real part is a real number, the imaginary part cannot be an integer140modulo $n$ !141142Let us now describe the types. As explained above, they are built recursively143from basic types which are as follows. We use the letter $T$ to designate any144type; the symbolic names \typ{xxx} correspond to the internal representations145of the types.\medskip146\settabs\+xxxxxx&typexxxxxxxxxxxxx&xxxxxxxxxxxxx&xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\cr147%148\+&type \tet{t_INT}& $\Z$& Integers (with arbitrary149precision)\sidx{integer}\cr150%151\+&type \tet{t_REAL}& $\R$& Real numbers (with arbitrary precision)\sidx{real152number}\cr153%154\+&type \tet{t_INTMOD}& $\Z/n\Z$& Intmods (integers modulo155$n$)\varsidx{intmod}\cr156%157\+&type \tet{t_FRAC}& $\Q$& Rational numbers (in irreducible158form)\sidx{rational number}\cr159%160\+&type \tet{t_FFELT}& $\F_q$& Finite field element\sidx{finite field161element}\cr162%163%164\+&type \tet{t_COMPLEX}& $T[i]$& Complex numbers\sidx{complex number}\cr165%166\+&type \tet{t_PADIC}& $\Q_p$& $p$-adic\sidx{p-adic number} numbers\cr167%168\+&type \tet{t_QUAD}& $\Q[w]$& Quadratic Numbers (where169$[\Z[w]:\Z]=2$)\sidx{quadratic number}\cr170%171\+&type \tet{t_POLMOD}& $T[X]/(P)$& Polmods (polynomials modulo172$P\in T[X]$)\varsidx{polmod}\cr173%174\+&type \tet{t_POL}& $T[X]$& Polynomials \sidx{polynomial}\cr175%176\+&type \tet{t_SER}& $T((X))$& Power series (finite Laurent177series)\sidx{power series}\cr178%179\+&type \tet{t_RFRAC}& $T(X)$& Rational functions (in irreducible180form)\sidx{rational function}\cr181%182\+&type \tet{t_VEC}& $T^n$& Row (i.e.~horizontal) vectors\sidx{row vector}\cr183%184\+&type \tet{t_COL}& $T^n$& Column (i.e.~vertical) vectors\sidx{column185vector}\cr186%187\+&type \tet{t_MAT}& ${\cal M}_{m,n}(T)$& Matrices\sidx{matrix}\cr188%189\+&type \tet{t_LIST}& $T^n$& Lists\sidx{list}\cr190%191\+&type \tet{t_STR}& & Character strings\sidx{string}\cr192%193\+&type \tet{t_CLOSURE}& & Functions\cr194%195\+&type \tet{t_ERROR}& & Error messages\cr196%197\+&type \tet{t_INFINITY}& & $-\infty$ and $+\infty$\cr198199\noindent and where the types $T$ in recursive types can be different in each200component. \sidx{scalar type} The first nine basic types, from \typ{INT} to201\typ{POLMOD}, are called scalar types because they essentially occur as202coefficients of other more complicated objects. Type \typ{POLMOD} is used to203define algebraic extensions of a base ring, and as such is a scalar type.204205In addition, there exist the type \tet{t_QFB} for integral206binary quadratic forms, and the internal type \tet{t_VECSMALL}. The latter207holds vectors of small integers\sidx{vecsmall}, whose absolute value is208bounded by $2^{31}$ (resp.~$2^{63}$) on 32-bit, resp.~64-bit, machines. They209are used internally to represent permutations, polynomials or matrices over a210small finite field, etc.211212Every PARI object (called \tet{GEN} in the sequel) belongs to one of these213basic types. Let us have a closer look.214215\subsec{Integers and reals} They are of216arbitrary and varying length (each number carrying in its internal217\sidx{integer}\sidx{real number}218representation its own length or precision) with the following mild219restrictions (given for 32-bit machines, the restrictions for 64-bit machines220being so weak as to be considered nonexistent): integers must be in absolute221value less than $2^{536870815}$ (i.e.~roughly 161614219 decimal digits). The222precision of real numbers is also at most 161614219 significant decimal223digits, and the binary exponent must be in absolute value less than224$2^{29}$, resp. $2^{61}$, on 32-bit, resp.~64-bit machines.225226Integers and real numbers are nonrecursive types.227228\subsec{Intmods, rational numbers, $p$-adic numbers, polmods, and rational229functions} These are recursive, but in a restricted way.230\sidx{intmod}\sidx{rational number}\sidx{p-adic number}\sidx{polmod}231232For intmods or polmods, there are two components: the modulus, which must233be of type integer (resp.\ polynomial), and the representative number (resp.\234polynomial).235236For rational numbers or rational functions, there are also only two237components: the numerator and the denominator, which must both be of type238integer (resp.\ polynomial).239240\def\limproj{{\displaystyle\lim_{\textstyle\longleftarrow}}}241242Finally, $p$-adic numbers have three components: the prime $p$, the243``modulus'' $p^k$, and an approximation to the $p$-adic number. Here $\Z_p$244is considered as the projective limit $\limproj \Z/p^k\Z$ via its finite245quotients, and $\Q_p$ as its field of fractions. Like real numbers, the246codewords contain an exponent, giving the $p$-adic valuation of the number,247and also the information on the precision of the number, which is248redundant with $p^k$, but is included for the sake of efficiency.249250\subsec{Finite field elements}\sidx{finite field element}251The exact internal format depends of the finite field size, but it includes252the field characteristic $p$, an irreducible polynomial $T\in\F_p[X]$253defining the finite field $\F_p[X]/(T)$ and the element expressed as254a polynomial in (the class of) $X$.255256\subsec{Complex numbers and quadratic numbers}\sidx{complex257number}\sidx{quadratic number} Quadratic numbers are numbers of the form258$a+bw$, where $w$ is such that $[\Z[w]:\Z]=2$, and more precisely $w=\sqrt259d/2$ when $d\equiv 0 \mod 4$, and $w=(1+\sqrt d)/2$ when $d\equiv 1 \mod 4$,260where $d$ is the discriminant of a quadratic order. Complex numbers261correspond to the important special case $w=\sqrt{-1}$.262263Complex numbers are partially recursive: the two components $a$264and $b$ can be of type \typ{INT}, \typ{REAL}, \typ{INTMOD}, \typ{FRAC}, or265\typ{PADIC}, and can be mixed, subject to the limitations mentioned above.266For example, $a+bi$ with $a$ and $b$ $p$-adic is in $\Q_p[i]$, but this is267equal to $\Q_p$ when $p\equiv 1 \mod 4$, hence we must exclude these $p$ when268one explicitly uses a complex $p$-adic type. Quadratic numbers are more269restricted: their components may be as above, except that \typ{REAL} is not270allowed.271272\subsec{Polynomials, power series, vectors, matrices}273\sidx{polynomial}\sidx{power series}\sidx{vector}\sidx{matrix}274They are completely recursive, over a commutative base ring: their components275can be of any type, and types can be mixed (however beware when doing276operations). Note in particular that a polynomial in two variables is simply277a polynomial with polynomial coefficients. Polynomials or matrices over278noncommutative rings are not supported.279280In the present version \vers{} of PARI, it is not possible to handle281conveniently power series of power series, i.e.~power series in several282variables. However power series of polynomials (which are power series in283several variables of a special type) are OK. This is a difficult design284problem: the mathematical problem itself contains some amount of imprecision,285and it is not easy to design an intuitive generic interface for such beasts.286287\subsec{Strings} These contain objects just as they would be printed by the288\kbd{gp} calculator.289290\subsec{Zero} What is zero? This is a crucial question in all computer291systems. The answer we give in PARI is the following. For exact types, all292zeros are equivalent and are exact, and thus are usually represented as an293integer \idx{zero}. The problem becomes nontrivial for imprecise types:294there are infinitely many distinct zeros of each of these types! For295$p$-adics and power series the answer is as follows: every such object,296including 0, has an exponent $e$. This $p$-adic or $X$-adic zero is297understood to be equal to $O(p^e)$ or $O(X^e)$ respectively.298\label{se:whatzero}299300Real numbers also have exponents and a real zero is in fact $O(2^e)$ where301$e$ is now usually a negative binary exponent. This of course is printed as302usual for a floating point number ($0.00\cdots$ or $0.Exx$ depending on the303output format) and not with a $O$ symbol as with $p$-adics or power series.304With respect to the natural ordering on the reals we make the following305convention: whatever its exponent a real zero is smaller than any positive306number, and any two real zeroes are equal.307308\section{The PARI philosophy}309The basic principles which govern PARI is that operations and functions310should, firstly, give as exact a result as possible, and secondly, be311permitted if they make any kind of sense.312313In this respect, we make an important distinction between exact and inexact314objects: by definition, types \typ{REAL}, \typ{PADIC} or \typ{SER} are315imprecise. A PARI object having one of these imprecise types anywhere in316its tree is \emph{inexact}, and \emph{exact} otherwise. No loss of accuracy317(rounding error) is involved when dealing with exact objects. Specifically,318an exact operation between exact objects will yield an exact object. For319example, dividing 1 by 3 does not give $0.333\cdots$, but the rational number320$(1/3)$. To get the result as a floating point real number, evaluate321\kbd{1./3} or \kbd{0.+1/3}.322323Conversely, the result of operations between imprecise objects, although324inexact by nature, will be as precise as possible. Consider for example the325addition of two real numbers $x$ and $y$. The \idx{accuracy} of the result is326\emph{a priori} unpredictable; it depends on the precisions of $x$ and $y$,327on their sizes, and also on the size of $x+y$. From this data, PARI works out328the right precision for the result. Even if it is working in calculator mode329\kbd{gp}, where there is a notion of \idx{default precision}, its value is330only used to convert exact types to inexact ones.331332In particular, if an operation involves objects of different accuracies, some333digits will be disregarded by PARI. It is a common source of errors to334forget, for instance, that a real number is given as $r + 2^e \varepsilon$335where $r$ is a rational approximation, $e$ a binary exponent and336$\varepsilon$ is a nondescript real number less than 1 in absolute value.337Hence, any number less than $2^e$ may be treated as an exact zero:338\bprog339? 0.E-28 + 1.E-100340%1 = 0.E-28341? 0.E100 + 1342%2 = 0.E100343@eprog344\noindent As an exercise, if \kbd{a = 2\pow (-100)}, why do \kbd{a + 0.} and345\kbd{a * 1.} differ?346347The second principle is that PARI operations are in general quite permissive.348For instance taking the exponential of a vector should not make sense.349However, it frequently happens that one wants to apply a given function350to all elements in a vector. This is easily done using a loop,351or using the \tet{apply} built-in function, but in fact PARI assumes that352this is exactly what you want to do when you apply a scalar function to a353vector. Taking the exponential of a vector will do just that, so no work is354necessary. Most transcendental functions work in the same way\footnote{*}{An355ambiguity arises with square matrices. PARI always considers that you want to356do componentwise function evaluation in this context, hence to get for357example the standard exponential of a square matrix you would need to358implement a different function.}.359360In the same spirit, when objects of different types are combined they361are first automatically mapped to a suitable ring, where the computation362becomes meaningful:363\bprog364? 1/3 + Mod(1,5)365%1 = Mod(3, 5)366? I + O(5^9)367%2 = 2 + 5 + 2*5^2 + 5^3 + 3*5^4 + 4*5^5 + 2*5^6 + 3*5^7 + O(5^9)368? Mod(1,15) + Mod(1,10)369%3 = Mod(2, 5)370@eprog371The first example is straightforward: since $3$ is invertible mod $5$, $(1/3)$372is easily mapped to $\Z/5\Z$. In the second example, \kbd{I} stands for the373customary square root of $-1$; we obtain a $5$-adic number, $5$-adically374close to a square root of $-1$. The final example is more problematic, but375there are natural maps from $\Z/15\Z$ and $\Z/10\Z$ to $\Z/5\Z$, and the376computation takes place there.377378\section{Operations and functions}379380The available operations and functions in PARI are described in detail in381Chapter 3. Here is a brief summary:382383\subsec{Standard arithmetic operations}384385\noindent386Of course, the four standard operators \kbd{+}, \kbd{-}, \kbd{*}, \kbd{/}387exist. We emphasize once more that division is, as far as possible,388an exact operation: $4$ divided by $3$ gives \kbd{(4/3)}. In addition to389this, operations on integers or polynomials, like \b{} (Euclidean390division), \kbd{\%} (Euclidean remainder) exist; for integers, {\b{/}}391computes the quotient such that the remainder has smallest possible absolute392value. There is also the exponentiation operator \kbd{\pow }, when the393exponent is of type integer; otherwise, it is considered as a transcendental394function. Finally, the logical operators \kbd{!} (\kbd{not} prefix operator),395\kbd{\&\&} (\kbd{and} operator), \kbd{||} (\kbd{or} operator) exist, giving396as results \kbd{1} (true) or \kbd{0} (false).397398\subsec{Conversions and similar functions}399400\noindent401Many conversion functions are available to convert between different types.402For example floor, ceiling, rounding, truncation, etc\dots. Other simple403functions are included like real and imaginary part, conjugation, norm,404absolute value, changing precision or creating an intmod or a polmod.405406\subsec{Transcendental functions}407408\noindent409They usually operate on any complex number, power series, and some also on410$p$-adics. The list is ever-expanding and of course contains all the411elementary functions (exp/log, trigonometric functions), plus many others412(modular functions, Bessel functions, polylogarithms\dots). Recall that by413extension, PARI usually allows a transcendental function to operate414componentwise on vectors or matrices.415416\subsec{Arithmetic functions}417418\noindent419Apart from a few like the factorial function or the Fibonacci numbers, these420are functions which explicitly use the prime factor decomposition of421integers. The standard functions are included. A number of factoring methods422are used by a rather sophisticated factoring engine (to name a few, Shanks's423SQUFOF, Pollard's rho, Lenstra's ECM, the MPQS quadratic sieve). These424routines output strong pseudoprimes, which may be certified by the APRCL425test.426427There is also a large package to work with algebraic number fields. All the428usual operations on elements, ideals, prime ideals, etc.~are available.429More sophisticated functions are also implemented, like solving Thue430equations, finding integral bases and discriminants of number fields,431computing class groups and fundamental units, computing in relative number432field extensions, Galois and class field theory, and also many functions433dealing with elliptic curves over $\Q$ or over local fields.434435\subsec{Other functions}436437\noindent438Quite a number of other functions dealing with polynomials (e.g.~finding439complex or $p$-adic roots, factoring, etc), power series (e.g.~substitution,440reversion), linear algebra (e.g.~determinant, characteristic polynomial,441linear systems), and different kinds of recursions are also included. In442addition, standard numerical analysis routines like univariate integration443(using the double exponential method), real root finding (when the root is444bracketed), polynomial interpolation, infinite series evaluation, and445plotting are included.446\medskip447448And now, you should really have a look at the tutorial before proceeding.449\newpage450451452