User's Guide to PARI / GP
Total Page:16
File Type:pdf, Size:1020Kb
User’s Guide to PARI / GP C. Batut, K. Belabas, D. Bernardi, H. Cohen, M. Olivier Laboratoire A2X, U.M.R. 9936 du C.N.R.S. Universit´eBordeaux I, 351 Cours de la Lib´eration 33405 TALENCE Cedex, FRANCE e-mail: [email protected] Home Page: http://pari.math.u-bordeaux.fr/ Primary ftp site: ftp://pari.math.u-bordeaux.fr/pub/pari/ last updated 11 December 2003 for version 2.2.7 Copyright c 2000–2003 The PARI Group ° Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies. Permission is granted to copy and distribute modified versions, or translations, of this manual under the conditions for verbatim copying, provided also that the entire resulting derived work is distributed under the terms of a permission notice identical to this one. PARI/GP is Copyright c 2000–2003 The PARI Group ° PARI/GP is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation. It is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY WHATSOEVER. Table of Contents Chapter1: OverviewofthePARIsystem . ...........5 1.1Introduction ..................................... ....... 5 1.2ThePARItypes..................................... ...... 6 1.3Operationsandfunctions. ...........9 Chapter 2: Specific Use of the GP Calculator . ............ 13 2.1Defaultsandoutputformats . ............14 2.2Simplemetacommands. ........21 2.3InputformatsforthePARItypes . .............25 2.4GPoperators ....................................... .28 2.5ThegeneralGPinputline . ........31 2.6 The GP/PARI programming language . .............32 2.7Errorsanderrorrecovery. .........42 2.8 Interfacing GP with other languages . ............46 2.9 The preferences file . .47 2.10 Using GP under GNU Emacs . 49 2.11UsingGPwithreadline . ........50 Chapter 3: Functions and Operations Available in PARI and GP ................ 53 3.1 Standard monadic or dyadic operators . ................54 3.2 Conversions and similar elementary functions or commands . .................58 3.3 Transcendental functions . .........68 3.4Arithmeticfunctions. ..........76 3.5 Functions related to elliptic curves . ............89 3.6 Functions related to general number fields . ............96 3.7 Polynomials and power series . ......... 135 3.8 Vectors, matrices, linear algebra and sets . .......... 141 3.9 Sums, products, integrals and similar functions . ................ 153 3.10Plottingfunctions . ......... 156 3.11ProgrammingunderGP. 162 Chapter 4: Programming PARI in Library Mode . ...........171 4.1 Introduction: initializations, universal objects . .................. 171 4.2Importanttechnicalnotes . ......... 172 4.3 Creation of PARI objects, assignments, conversions . ............ 175 4.4Garbagecollection. 179 4.5 Implementation of the PARI types . .......... 186 4.6PARIvariables .................................... 190 4.7Inputandoutput . 192 4.8Acompleteprogram . 197 4.9AddingfunctionstoPARI . ......... 201 Chapter 5: Technical Reference Guide for Low-Level Functions .................207 5.1 Level 0 kernel (operations on unsigned longs) . ............ 207 5.2 Level 1 kernel (operations on longs, integers and reals) . .............. 208 5.3 Level 2 kernel (operations on general PARI objects) . ............ 215 Appendix A: Installation Guide for the UNIX Versions . ...............221 Appendix B: A Sample program and Makefile . .........229 Appendix C: Summary of Available Constants . ............231 Index .............................................. 233 Chapter 1: Overview of the PARI system 1.1 Introduction. The PARI system is a package which is capable of doing formal computations on recursive types at high speed; it is primarily aimed at number theorists, but can be used by anybody whose primary need is speed. Although quite an amount of symbolic manipulation is possible in PARI, this system does very badly compared to much more sophisticated systems like Axiom, Macsyma, Maple, Mathematica or Reduce on such manipulations (e.g. multivariate polynomials, formal integration, etc... ). On the other hand, the three main advantages of the system are its speed (which can be between 5 and 100 times better on many computations), the possibility of using directly data types which are familiar to mathematicians, and its extensive algebraic number theory module which has no equivalent in the above-mentioned systems. It is possible to use PARI in three different ways: 1) as a library, which can be called from an upper-level language application, for instance written in C or C++; 2) as a sophisticated programmable calculator, named GP, which contains most of the control instructions of a standard language like C; 3) the compiler GP2C can translate GP code to C, and load them into the GP interpreter. A typical script compiled by GP2C will typically run 3 to 10 times faster. The C code can also be edited and optimized by hand. The use of GP is explained in chapters 2 and 3, and the programming in library mode is explained in chapters 3, 4 and 5. In the present Chapter 1, we give an overview of the system. Important note: A tutorial for GP is provided in the standard distribution (tutorial.dvi) and you should read this first (at least the beginning of it, you can skip the specialized topics you’re not interested in). You can then start over and read the more boring stuff which lies ahead. But you should do that eventually, at the very least the various Chapter headings. You can have a quick idea of what is available by looking at the GP reference card (refcard.dvi or refcard.ps). In case of need, you can then refer to the complete function description in Chapter 3. How to get the latest version? This package can be obtained by anonymous ftp from quite a number of sites (ask your favourite Web search engine for the site nearest to you). But, if you want the very latest version (including development versions), you should use the anonymous ftp address ftp://pari.math.u-bordeaux.fr/pub/pari where you will find all the different ports and possibly some binaries. A lot of version information, mailing list archives, and various tips can be found on PARI’s home page: http://pari.math.u-bordeaux.fr/ 5 Implementation notes: (You can skip this section and switch to Section 1.2 if you’re not inter- ested in hardware technicalities. You won’t miss anything that would be mentioned here.) The PARI package contains essentially three variants. See Appendix A for how to set up one of these on your system. In a first version, some time-critical parts of the multiprecision kernel are written in assembler, a few hundred lines at most. At present there exists three versions for the Sparc architecture: one for Sparc version 7 (e.g. Sparcstation 1, 1+, IPC, IPX or 2), one for Sparc version 8 with SuperSparc processors (e.g. SparcStation 10 and 20) and one for Sparc version 8 with MicroSparc I or II processors (e.g. SparcClassic or SparcStation 4 and 5). No specific version is written for the UltraSparc since it can use the MicroSparc II version. In addition, versions exist for the DEC Alpha 64-bit processors, and finally the whole ix86 family (Intel, AMD, Cyrix) starting at the 386 (up to the Xbox gaming console!). Finally, there are unmaintained, much less optimized and not really usable anymore, micro-kernels for the HP-PA architecture (only for HPUX 9, does not work with HPUX 10), and for the PowerPC architecture (only for the 601, does not compile anymore). A second version uses the GNU MP library to implement its multiprecision kernel, and should run on any platform where GMP could be installed. The GMP kernel is about as fast as the native one for small operands, and much faster for huge ones (say, about twice faster around 10000 decimal digits). A third version is written entirely in ANSI C, with a C++-compatible syntax, and should be portable without much trouble to any 32 or 64-bit computer having no real memory constraints. It is about 2 times slower than versions including an assembly kernel. This version has been tested for example on MIPS based DECstations 3100 and 5000 and SGI computers, on various Macs (based on 680x0 chips or more recent PowerPCs, like the G3 or G4), new Intel chips like ARM (handheld devices) or ia64 (Itanium). In addition, PARI has been ported to a considerable number of smaller and larger machines, for example the VAX, 68000-based machines like the Atari, Mac Classic or Amiga 500, 68020 machines such as the Amiga 2500 or 3000, or mainframes like the IBM/S390. An historical version of the PARI/GP kernel, written in 1985, was specific to 680x0 based computers, and was entirely writen in MC68020 assembly language (around 6000 lines). It ran on SUN-3/xx, Sony News, NeXT cubes and on 680x0 based Macs. Since 1997, this version was unmaintained; it has been removed from the PARI distribution in version 2.2.5. To run PARI with a 68k assembler micro-kernel, one now uses the GMP version. 1.2 The PARI types. The crucial word in PARI is recursiveness: most of the types it knows about are recursive. For example, the basic type Complex exists (actually called t_COMPLEX). However, the components (i.e. the real and imaginary part) of such a “complex number” can be of any type. The only sensible ones are integers (we are then in Z[i]), rational numbers (Q[i]), real numbers (R[i] = C), or even elements of Z/nZ ((Z/nZ)[i] when this makes sense), or p-adic numbers when p 3 mod 4 (Q [i]). ≡ p This feature must of course not be used too rashly: for example you are in principle allowed to create objects which are “complex numbers of complex numbers”, but don’t expect PARI to make sensible use of such objects: you will mainly get nonsense. On the other hand, one thing which is allowed is to have components of different, but com- patible, types. For example, taking again complex numbers, the real part could be of type integer, and the imaginary part of type rational.