#./usersch1.tex# = 25cm =

P> =users.toc users.idx =0

2

<#31#> =titlepage;''<#31#> =1 by1=0 <#1932#><#1932#> <#1933#>=0<#1935#><#1935#><#1933#>

Chapter : Overview of the <#1936#><#1944#><#1944#>PARI<#1936#> System

by1=0 <#1946#>=0<#1948#><#1948#><#1946#>



<#1947#><#1949#>. Introduction.<#1949#><#1947#>


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 people 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 REDUCE, Macsyma, Maple, Mathematica or SCRATCHPAD on such manipulations (e.g. multivariate polynomials, formal integration, etc...). On the other hand, the two main advantages of the system is its speed (which can be between 5 and 100 times better on many computations than the above mentioned systems), and the possibility of using directly data types which are familiar to mathematicians.

It is possible to use PARI in two different ways:

;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;1) as a library, which can be called from any upper-level language application (for instance written in C, C+ +, Pascal or Fortran);

;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;2) as a sophisticated programmable calculator, named <#34#>GP<#34#>, which contains most of the standard control instructions of a standard language like C.

The use of GP is explained in chapters 2 and 3, and the programming in library mode is explained in chapters 3 and 4. In the present chapter 1, we give an overview of the system.

<#1952#>=0<#1954#><#1954#><#1952#>

<#1953#><#1955#>Implementation notes.<#1955#><#1953#>

The PARI package exists essentially in three versions. The first one is a specific implementation for 68020/68030/68040 based computers which contains a kernel (for the elementary arithmetic operations on multiprecise integers and real numbers, and binary/decimal conversion routines) entirely written in MC68020 assembly language (around 7000 lines), the rest being at present entirely written in C (although we are now looking into C++). The system runs on SUN-3/xx, Sony News, NeXT cubes and on the Macintosh II. It should be very easy to port on any other 680x0 based machine like for instance the Apollo Domain workstations.

Note that the assembly language source code uses the SUN syntax, which for some strange reason differs from the Motorola standard used by most other 680x0 machines in the world. On the Mac II disquettes, we have included a program which automatically converts from the SUN syntax into the standard one, at least for the needed PARI assembly file. On the Unix distribution, we have included other versions of the assembly file, using different syntaxes.

The second version is a specific implementation for SPARC based workstations, like the SPARCstation 1. This version contains only a few hundred lines of assembly code, and is usually slightly faster on the SPARCstation 1+ than on a SUN-3/60 or SUN-3/80.

Finally, a third version is written entirely in C, and should be portable with not too much trouble to any 32-bit computer having no real memory constraints (hence <#36#>not<#36#> to MS-DOS systems). It is again at least 2 times slower than the SPARC version. This version has been tested for example on the DECstation 3100.

by1=0 <#1966#>=0<#1968#><#1968#><#1966#>



<#1967#><#1969#>. The PARI <#1976#><#1976#>types.<#1969#><#1967#>


The crucial word in PARI is <#1978#><#1978#>recursivity: most types which are handled are recursive. For example, the basic type <#1657#><#1980#><#1980#>Complex<#1657#> exists. 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 #math18##tex2html_wrap_inline17046#[i]), rational numbers (#math19##tex2html_wrap_inline17048#[i]), real numbers (#math20##tex2html_wrap_inline17050#[i] = #tex2html_wrap_inline17051#), or even elements of #math21##tex2html_wrap_inline17053#/n#tex2html_wrap_inline17054# (#math22#(#tex2html_wrap_inline17056#/n#tex2html_wrap_inline17057#)[i] when this makes sense), or p-adic numbers when #math23#p≡3 mod 4 (#math24##tex2html_wrap_inline17063#[i]).

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 <#41#>is<#41#> allowed is to have components of different, but compatible, types. For example, taking again complex numbers, the real part could be of type integer, and the imaginary part of type rational.

By compatible, we mean types which can be freely mixed in operations like + or *. For example if the real part is of type real, the imaginary part cannot be of type integermod (integers modulo a given number n).

Let us now describe the types. As explained above, they are built recursively from basic types which are as follows. We use the letter T to designate any type; the type number corresponds to the internal representation of the type.


xxx;SPMamp;typexxxxxx;SPMamp;xxxxxxxxxxxxxxxxxxx;SPMamp;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxx ;SPMamp;type 1:;SPMamp; #tex2html_wrap_inline17069#;SPMamp; <#1993#><#1993#>Integers (with arbitrary precision) ;SPMamp;type 2:;SPMamp; #tex2html_wrap_inline17071#;SPMamp; <#1996#><#1996#>Real numbers (with arbitrary precision) ;SPMamp;type 3:;SPMamp; #math25##tex2html_wrap_inline17073#/n#tex2html_wrap_inline17074#;SPMamp; <#2000#><#2000#>Integermods (integers modulo n) ;SPMamp;type 4:;SPMamp; #tex2html_wrap_inline17077#;SPMamp; <#2003#><#2003#>Rational numbers (in irreducible form) ;SPMamp;type 5:;SPMamp; #tex2html_wrap_inline17079#;SPMamp; Rational numbers (in non necessarily irreducible form) ;SPMamp;type 6:;SPMamp; T[i];SPMamp; <#2006#><#2006#>Complex numbers ;SPMamp;type 7:;SPMamp; #tex2html_wrap_inline17084#;SPMamp; p-adic<#2009#><#2009#> numbers ;SPMamp;type 8:;SPMamp; T[w];SPMamp; <#2011#><#2011#>Quadratic numbers (where #math26#[#tex2html_wrap_inline17088#[w] : #tex2html_wrap_inline17089#] = 2) ;SPMamp;type 9:;SPMamp; #math27#T[X]/P(X)T[X];SPMamp; <#2015#><#2015#>Polymods (polynomials modulo P) ;SPMamp;type 10:;SPMamp; T[X];SPMamp; <#2017#><#2017#>Polynomials ;SPMamp;type 11:;SPMamp; T((X));SPMamp; <#2019#><#2019#>Power series (finite Laurent series) ;SPMamp;type 13:;SPMamp; T(X);SPMamp; <#2021#><#2021#>Rational functions (in irreducible form) ;SPMamp;type 14:;SPMamp; T(X);SPMamp; Rational functions (in non necessarily irreducible form) ;SPMamp;type 17:;SPMamp; Tn;SPMamp; Row (i.e. horizontal) <#2023#><#2023#>vectors ;SPMamp;type 18:;SPMamp; Tn;SPMamp; Column (i.e. vertical) <#2025#><#2025#>vectors ;SPMamp;type 19:;SPMamp; #math28##tex2html_wrap_inline17101#m, n(T);SPMamp; Matrices.<#2027#><#2027#>

In addition, there exist type 15 for binary quadratic forms of positive discriminant, type 16 for binary quadratic forms of negative discriminant <#2029#><#2029#> which can be used in specific operations, but which may disappear in future versions, hence their use is not documented in this manual.

Let us look closely at these types.

by 1=0 <#2031#>=0<#2033#><#2033#><#2031#>

<#2032#><#2034#>.. Integers and reals<#2034#><#2032#>:<#2038#><#2038#><#2040#><#2040#> they are of arbitrary and varying length (each number having coded internally its own length or precision) with the following mild restrictions: integers must be in absolute value less than #math29#21048480 (i.e. roughly 315623 digits). The precision of real numbers is also at most 315623 significant decimal digits, and the binary exponent must be in absolute value less than #math30#223 = 8388608.

Note that PARI has been optimized so that it works as fast as possible on numbers with 40 to 100 decimal digits. In particular, no fancy multiplication technique is used. Hence, although it is possible to use PARI to do computations with 300000 decimal digits, much better programs can be written for these loooong numbers.

Integers and real numbers are completely non recursive types and are sometimes called the <#1658#><#2042#><#2042#>leaves<#1658#>.

by 1=0 <#2044#>=0<#2046#><#2046#><#2044#>

<#2045#><#2047#>.. Integermods, rational numbers (irreducible or not), p-adic numbers, polymods, and rational functions<#2047#><#2045#>:<#2051#><#2051#><#2053#><#2053#><#2055#><#2055#> <#2057#><#2057#> they are recursive, but in a forced manner:

For integermods or polymods, there are two components: the modulus, which must be of type integer (resp. polynomial), and the number (or polynomial).

For rational numbers or rational functions, there are also only two components: the numerator and the denominator, which must both be of type integer (resp. polynomial).

Finally, p-adic numbers have three components: the prime p, the ``modulus'' pk, and an approximation to the p-adic number. Here #tex2html_wrap_inline17113# is considered as #math31##tex2html_wrap_indisplay17115##tex2html_wrap_inline17116#/pk#tex2html_wrap_inline17117#, and #tex2html_wrap_inline17121# as its field of fractions. Like real numbers, the codewords contain also the information on the precision of the number (which is in fact redundant with pk, but it is apparently more efficient to keep this redundancy).

by 1=0 <#2065#>=0<#2067#><#2067#><#2065#>

<#2066#><#2068#>.. Complex numbers (a + bi) and quadratic numbers<#2068#><#2066#>: <#2072#><#2072#> <#2074#><#2074#>quadratic numbers are numbers of the form a + bw, where w is such that #math32#[#tex2html_wrap_inline17128#[w] : #tex2html_wrap_inline17129#] = 2, and more precisely #math33#w = #tex2html_wrap_inline17131#/2 when #math34#d≡0 mod 4, and #math35#w = (1 + #tex2html_wrap_inline17134#)/2 when #math36#d≡1 mod 4, where d is the discriminant of a quadratic order.

Complex and quadratic numbers are partially recursive: the two components a and b can be of type integer, real, rational, integermod or p-adic, and can be mixed, subject to the limitations mentioned above. For example, a + bi with a and b p-adic is in #math37##tex2html_wrap_inline17147#[i], but this is equal to #tex2html_wrap_inline17151# when #math38#p≡1 mod 4, hence we must exclude these p when one explicitly uses a complex p-adic type.

by 1=0 <#2083#>=0<#2085#><#2085#><#2083#>

<#2084#><#2086#>.. Polynomials, power series, vectors and matrices<#2086#><#2084#>: <#2090#><#2090#><#2092#><#2092#><#2094#><#2094#><#2096#><#2096#> they are completely recursive: their components can be of any type, and types can be mixed (however beware when doing operations). Note in particular that a polynomial in two variables is simply a polynomial with polynomial coefficients.

Note that in the present version 1.35 of PARI, there is a bug in the handling of power series of power series (i.e. power series in several variables). However power series of polynomials (which are power series in several variables of a special type) are OK. The reason for this bug is known, but it is difficult to correct because the mathematical problem itself contains some amount of imprecision.

by 1=0 <#2098#>=0<#2100#><#2100#><#2098#>

<#2099#><#2101#>.. Exact and imprecise objects<#2101#><#2099#>: <#2105#><#2105#>we have already said that integers and reals are called the <#2107#><#2107#>leaves because they are ultimately at the end of every branch of a tree representing a PARI object. Another important notion is that of an <#1659#><#2109#><#2109#>exact object<#1659#>: by definition, numbers of basic type real, p-adic or power series are imprecise, and we will say that a PARI object having one of these imprecise types anywhere in its tree is not exact. All other PARI objects will be called exact. This is a very important notion since no numerical analysis is involved when dealing with exact objects.

by 1=0 <#2119#>=0<#2121#><#2121#><#2119#>

<#2120#><#2122#>.. What is <#2130#><#2130#>zero?<#2122#><#2120#>: this is a crucial question in all computer systems. The answer we give in PARI is the following. For exact types, all zeros are equivalent and are exact, so are usually represented as an integer zero. The problem becomes nontrivial for imprecise types. For p-adics the answer is as follows: every p-adic number (including 0) has an exponent e and a ``mantissa'' u which is a p-adic unit, except when the number is zero (in which case u is zero), the mantissa having a certain ``precision'' k (i.e. is defined modulo pk). Then this p-adic zero is understood to be equal to O(pe), i.e. there is an infinite number of zeros. The number k is thus irrelevant.

For power series the situation is similar, with p replaced by X, i.e. a power series zero will be O(Xe), the number k (here the length of the power series) being also irrelevant.

For real numbers, the precision k is also irrelevant, and a real zero will in fact be O(2e) where e is now usually a negative binary exponent. This of course will be printed as usual for a real number (#math39#0.0000 ... in <#83#>f<#83#> format or 0.Exx in <#84#>e<#84#> format) and not with a O() symbol as with p-adics or power series.

by 1=0 <#2132#>=0<#2134#><#2134#><#2132#>

<#2133#><#2135#>.. Scalar types<#2135#><#2133#>:<#2139#><#2139#> basic types with type number less than or equal to 9 will be called scalar types because they essentially occur as coefficients of some more complicated object. Note that type 9, i.e. polymod, is used to define algebraic extensions of a base ring, and as such is a scalar type.

by1=0 <#2141#>=0<#2143#><#2143#><#2141#>



<#2142#><#2144#>. Operations and functions.<#2144#><#2142#>


by 1=0 <#2147#>=0<#2149#><#2149#><#2147#>

<#2148#><#2150#>.. The PARI philosophy<#2150#><#2148#>. The basic philosophy which governs PARI is that operations and functions should first give as exact a result as possible, and second, be permitted if they make any kind of sense.

More specifically, if you do an operation (not a transcendental one) between exact objects, you will get an exact object. For example, dividing 1 by 3 does not give #math40#0.33333 ... as you could expect, but simply the rational number (1/3). If you really want the result in type real, evaluate 1./3 or add 0. to (1/3).

The result of operations between imprecise objects will be as precise as possible. Consider for example one of the most difficult cases, that is the addition of two real numbers x and y. The <#2154#><#2154#>accuracy of the result is <#90#>a priori<#90#> unpredictable; it depends on the precisions of x and y, on their sizes (i.e. their exponents), and also on the size of x + y. PARI works out automatically the right precision for the result, even when it is working in calculator mode GP where there is a <#2156#><#2156#>default precision. See the technical reference manual for the precise formulas giving the precision of the result.

The second part of the PARI philosophy is that PARI operations are in general quite permissive. For instance taking the exponential of a vector should not make sense. However, it is frequent that a computation comes out with a result which is a vector with many components, and one wants to get the exponential of each one. This could easily be done either under GP or in library mode, but in fact PARI assumes that this is exactly what you want to do when you take the exponential of a vector, so no work is necessary. Most transcendental functions work in the same way (see chapter 3 for details).

An ambiguity would arise with square matrices. PARI always considers that you want to do componentwise function evaluation, hence to get for example the exponential of a square matrix you would need to use a function with a different name, matexp for instance. In the present version 1.35, this is not yet implemented. See however the program in Appendix C, which is a first attempt for this particular function.

The available operations and functions in PARI are described in detail in chapter 3. Here is a brief summary:

by 1=0 <#2158#>=0<#2160#><#2160#><#2158#>

<#2159#><#2161#>.. Standard operations<#2161#><#2159#>.

The four standard operators +, -, *, / of course exist. It should be once more emphasized that division is, as far as it can, an exact operation (4 divided by 3 gives (4/3)). In addition to this, operations on integers or polynomials, like #math41#\ (euclidean division), % (euclidean remainder) exist. There is also the exponentiation operator #tex2html_wrap_inline17198#, when the exponent is of type integer (otherwise it is considered as a transcendental function). Finally, the logical operators (<#94#>and<#94#> operator), | | (<#95#>or<#95#> operator) exist, giving as results 1 (true) or 0 (false). Note that the C-syntax and | is also accepted as a synonym. However, there is no bitwise <#96#>and<#96#> or <#97#>or<#97#>.

by 1=0 <#2165#>=0<#2167#><#2167#><#2165#>

<#2166#><#2168#>.. Conversions and similar functions<#2168#><#2166#>.

Many conversion functions are available to convert between different types. For example floor, ceiling, rounding, truncation, etc.... Other simple functions are included like real and imaginary part, conjugation, norm, absolute value, changing precision or creating an integermod or a polymod.

by 1=0 <#2172#>=0<#2174#><#2174#><#2172#>

<#2173#><#2175#>.. Transcendental functions<#2175#><#2173#>.

They usually operate on any object in #tex2html_wrap_inline17204#, and some also on p-adics. The list is everexpanding and of course contains all the elementary functions, plus already a number of others. Recall that by extension, PARI usually allows a transcendental function to operate componentwise on vectors or matrices.

by 1=0 <#2180#>=0<#2182#><#2182#><#2180#>

<#2181#><#2183#>.. Arithmetic functions<#2183#><#2181#>.

Apart from a few like the factorial function or the fibonacci numbers, these are functions which explicitly use the prime factor decomposition of integers. The standard functions are included. In the present version 1.35, a primitive, but useful version of Lenstra's Elliptic Curve Method (ECM) has been implemented. More sophisticated functions are implemented, like finding integral bases and discriminants of number fields, computing class numbers and regulators (for quadratic fields only), and also many functions dealing with elliptic curves over #tex2html_wrap_inline17207# or over local fields.

by 1=0 <#2188#>=0<#2190#><#2190#><#2188#>

<#2189#><#2191#>.. Other functions<#2191#><#2189#>.

Quite a number of other functions dealing with polynomials (e.g. finding complex or p-adic roots, factoring,...), power series (e.g. substitution, reversion), linear algebra (e.g. determinant, characteristic polynomial, linear systems), and different kinds of recursions are also included. In addition, standard numerical analysis routines like Romberg integration (open or closed, on a finite or infinite interval), real root finding when the root is bracketed, polynomial interpolation, series acceleration, and plotting are included. See the last sections of chapter 3 for details.

#./usersch1.tex# by1=0 <#2203#><#2203#> <#2204#>=0<#2206#><#2206#><#2204#>

Chapter : Specific use of the <#2207#><#2215#><#2215#>GP<#2207#> calculator

Originally, GP was designed as a debugging tool for the PARI system library, and hence not much thought had been given to make it user-friendly. The situation has now changed, and GP is very useful as a standalone tool. Hence many new functionalities have been added. The operations and functions available in PARI and GP will be described in the next chapter; in the present one we describe the specific use of the GP programmable calculator.

GP (and in fact any program using the PARI library) needs a stack in which to do its computations, and a table of primes, which may be very small. The default stack size is 1000000 bytes on the MacII, and 4000000 on other machines. The prime number limit is by default 200000 on the MacII, and 500000 on other machines. These defaults can be changed by adding parameters to the input line. To start the calculator, the general syntax is:

gp [-s stacksize] [-p primelimit] [-b buffersize]

where as usual items within brackets are optional. The buffersize option is to allow for overly large input files read into GP, for example large matrices. The default is 30000, which is usually more than plenty.

(Note that on the Macintosh II, even after clicking on the gp icon, once in the MPW Shell, you still need to type explicity the above command.)

If you have GNUemacs, it is strongly advised to work in a special emacs shell (see section 3.10), which is started by typing <#104#>M-x gp<#104#> (where as usual <#105#>M<#105#> is the Meta key) if you accept the default stack, prime and buffer sizes, or <#106#>C-u M-x gp<#106#> which will ask you for the name of the gp executable, the stack size, the prime limit and the buffersize.

A copyright message then appears which includes the version number. Please note this number, so as to be sure to have the most recent version if you wish to have updates of PARI. The present manual is written for version 1.35, but only details will change (like new and faster functions, less bugs, better output, etc...) until the next major revision 2.00.

After the copyright, the computer works for a few seconds (it is in fact computing and storing a table of primes), writes some help information, the initial defaults, and then waits after its prompt, initially ``?''.

by1=0 <#2217#>=0<#2219#><#2219#><#2217#>



<#2218#><#2220#>. Metacommands.<#2220#><#2218#>


Let us consider the first three printed lines. It says that to get help, you should type #math42#\c, #math43#\d, #math44#\t, or ?command; to exit you must type #math45#\q; and for timing you should type #.

As a general rule, under GP, commands starting with ``#math46#\'' and some other symbols like ``?'', are not computing commands, but are metacommands which correspond to information exchange with GP. The available commands of this sort can be divided among simple commands (explained here) and default setting commands (explained in the next section).

Simple commands are case insensitive. For example, #math47#\Q is identical to #math48#\q.

by 1=0 <#2223#>=0<#2225#><#2225#><#2223#>

<#2224#><#2226#>.. #math50#\\<#2226#><#2224#>: comment. The rest of the line is ignored by GP.

by 1=0 <#2238#>=0<#2240#><#2240#><#2238#>

<#2239#><#2241#>.. #math52#\<#2243#>b<#2243#><#2241#><#2239#>n: print the object number n (%n) in beautified (or prettyprint) format if the default format is raw, and in raw format if the default format is prettyprint (see 2.1.6 and 2.2.6). If the number n is omitted, print the latest computed object ( % ).

by 1=0 <#2255#>=0<#2257#><#2257#><#2255#>

<#2256#><#2258#>.. #math54#\<#2260#>c<#2260#><#2258#><#2256#>: prints the list of all available functions <#2264#><#2264#> under GP, not including functions using a special character (specifically + , - , * , /, #math55#\ , #tex2html_wrap_inline17233# , ! , #math56##tex2html_wrap_inline17236# , _ , % , , |). These names being in general not informative enough, more information can be obtained by typing ``<#114#>?<#114#>command'', which gives in short form the effect of the command and the name and syntax of the corresponding library function. Of course, to have complete information, read chapter 3 of this manual. Much better help can be obtained if you work under GNUemacs (see section 3.10).

by 1=0 <#2274#>=0<#2276#><#2276#><#2274#>

<#2275#><#2277#>.. #math58#\<#2279#>d<#2279#><#2277#><#2275#>: prints the <#2283#><#2283#>defaults (see the next section), including the stacksize, primelimit and buffersize. This command is always executed (without user intervention), when GP is started.

by 1=0 <#2293#>=0<#2295#><#2295#><#2293#>

<#2294#><#2296#>.. #math60#\<#2298#>k<#2298#><#2296#><#2294#>: kill and reinitialize GP. Apart from the initial message, this is equivalent to exiting from GP and starting it again, apart from the fact that the variables which have been used are still known to GP.

by 1=0 <#2310#>=0<#2312#><#2312#><#2310#>

<#2311#><#2313#>.. #math62#\<#2315#>l<#2315#><#2313#><#2311#>: switch the logfile on/off. Initially, the GP session does not have a log file where all the commands and results are stored. Typing #math63#\l will open a file named <#119#>pari.log<#119#> (and destroy any previous file with that name), and until a subsequent #math64#\l, all the commands and results will be written in that file.

by 1=0 <#2327#>=0<#2329#><#2329#><#2327#>

<#2328#><#2330#>.. #math66#\<#2332#>p<#2332#><#2330#><#2328#>: switches the default format between the raw<#2336#><#2336#> and the beautified (prettyprint) format <#2338#><#2338#><#2340#><#2340#> (see 2.2.5 below).

by 1=0 <#2350#>=0<#2352#><#2352#><#2350#>

<#2351#><#2353#>.. #math68#\<#2355#>q<#2355#><#2353#><#2351#>: <#2359#><#2359#>quits GP and returns to the system.

by 1=0 <#2369#>=0<#2371#><#2371#><#2369#>

<#2370#><#2372#>.. #math70#\<#2374#>r<#2374#><#2372#><#2370#>filename: <#2378#><#2378#>reads into GP all the objects written on the named file using the #math71#\w command (see 2.1.13.).

by 1=0 <#2388#>=0<#2390#><#2390#><#2388#>

<#2389#><#2391#>.. #math73#\<#2393#>s<#2393#><#2391#><#2389#>: prints the state of the PARI <#2397#><#2397#>stack, and in particular the amount of memory used, expressed as a percentage. This enables the user to see whether the stack is getting too full.

by 1=0 <#2407#>=0<#2409#><#2409#><#2407#>

<#2408#><#2410#>.. #math75#\<#2412#>s<#2412#>(n)<#2410#><#2408#>: prints the state of the PARI stack, and in addition the explicit <#2416#><#2416#>hexadecimal representations of the first n longwords of the stack (going down) if n is positive, and the complete stack if n is negative. This is used primarily as a debugging device for PARI, and is not intended for the casual user.

by 1=0 <#2426#>=0<#2428#><#2428#><#2426#>

<#2427#><#2429#>.. #math77#\<#2431#>t<#2431#><#2429#><#2427#>: prints the <#2435#><#2435#>internal longword format of all the PARI types. The detailed bit or byte format of the initial codeword(s) is explained in the technical reference manual, but its knowledge is not necessary for a GP user.

by 1=0 <#2445#>=0<#2447#><#2447#><#2445#>

<#2446#><#2448#>.. #math79#\<#2450#>v<#2450#><#2448#><#2446#>: prints the <#2454#><#2454#>version number type of implementation (680x0, sparc, other) of the program you are using. Note that the subversion may be omitted (e.g. 1.35 instead of 1.35.1).

by 1=0 <#2464#>=0<#2466#><#2466#><#2464#>

<#2465#><#2467#>.. #math81#\<#2469#>w<#2469#><#2467#><#2465#>n filename: <#2473#><#2473#>write the object number n ( %n ) on the named file, in raw format. If the number n is omitted, write the latest computed object ( % ).

by 1=0 <#2483#>=0<#2485#><#2485#><#2483#>

<#2484#><#2486#>.. #math83#\<#2488#>x<#2488#><#2486#><#2484#>: print the complete tree with addresses and contents in hexadecimal, of the <#2492#><#2492#>internal representation of the latest computed object in GP. As for #math84#\s, this is used primarily as a debugging device for PARI, and is not intended for the casual user. However, used on a PARI integer, it can be used as a decimal#math85#→hexadecimal converter.

by 1=0 <#2494#>=0<#2496#><#2496#><#2494#>

<#2495#><#2497#>.. #<#2497#><#2495#>: switches the <#2501#><#2501#>timer on or off. The timer counts electrical cycles, so is precise only to ±20ms in Europe and ±16.7ms in North America. The time measured is the user <#2503#><#2503#>CPU time, <#143#>not<#143#> including the time for printing.

by1=0 <#2512#>=0<#2514#><#2514#><#2512#>



<#2513#><#2515#>. Defaults and <#2522#><#2522#>output formats.<#2515#><#2513#>


The other commands starting with ``#math86#\'' are for setting the defaults. Note that in the syntax given below, the ``='' sign must be written explicitly, and n represents a non negative integer.

by 1=0 <#2532#>=0<#2534#><#2534#><#2532#>

<#2533#><#2535#>.. #math88#\<#2543#><#2543#>precision<#2535#><#2533#>= n: Sets to n the number of significant digits, and at the same time the number of printed digits of real numbers. The initial default is 28. Note that one can also use the function <#146#>setprecision<#146#> (see chapter 3) to achieve the same goal, and this <#147#>must<#147#> be done if it is inside a GP expression or program.

by 1=0 <#2553#>=0<#2555#><#2555#><#2553#>

<#2554#><#2556#>.. #math90#\<#2564#><#2564#>serieslength<#2556#><#2554#>= n: Sets to n the number of significant terms in power series. The initial default is 16.Note that one can also use the function <#149#>setserieslength<#149#> (see chapter 3) to achieve the same goal, and this <#150#>must<#150#> be done if it is inside a GP expression or program.

by 1=0 <#2574#>=0<#2576#><#2576#><#2574#>

<#2575#><#2577#>.. #math92#\<#2585#><#2585#>format<#2577#><#2575#>=xn.m (where x is a letter): if the letter is ``f'', real numbers will be printed in <#2587#><#2587#>fixed floating point format with no explicit exponent (e.g. 0.000033); if the letter is ``e'', they will be printed in <#2589#><#2589#>scientific format, always with an explicit exponent (e.g. 3.3e-5). If the letter is ``g'', real numbers will be printed in ``f'' format, except when their absolute value is less than 2-32, in which case they are printed in ``e'' format. The initial default is ``g''.

The number n is the number of significant digits printed for real numbers except if n≤ 0 where all the significant digits will be printed (initial default 28), and the number m is the number of characters to be used for printing integers, but is ignored if is equal to zero (initial default 0). This is a feeble attempt at formatting.

by 1=0 <#2599#>=0<#2601#><#2601#><#2599#>

<#2600#><#2602#>.. #math94#\<#2610#><#2610#>prompt<#2602#><#2600#>=string: Sets the prompt equal to the given string. The initial default is ``?''.

by 1=0 <#2612#>=0<#2614#><#2614#><#2612#>

<#2613#><#2615#>.. Raw and beautifier format<#2615#><#2613#>: in addition to the default formats which can be set as explained above, you can print results either using a ``raw'' <#2619#><#2619#> format, i.e. a format which is equivalent to what you input, including explicit multiplication signs, and everything typed on a line instead of two dimensional boxes. This can have several advantages, for instance it allows you to pick the result with a mouse or an editor, and to put it somewhere else. This is the default.

Or else you can ask to ``beautify'' the result. In the present version 1.35, this is not beautiful at all. The syntax is:

<#158#>#math95#\bn<#158#>, where n is an integer. This prints %n in <#2621#><#2621#>``beautified'' format (but does not create any new object on the stack). If n is omitted, it prints the latest computed object % in ``beautified'' format (see 2.1.2). One can reverse the default (and the effect of #math96#\bn) by the command #math97#\p (see 2.1.6).

by 1=0 <#2623#>=0<#2625#><#2625#><#2623#>

<#2624#><#2626#>.. Note on the output formats.<#2626#><#2624#>

A zero real number is printed in ``e'' format as 0.Exx where xx is the (usually negative) <#161#>decimal<#161#> exponent of the number (cf. 1.2.6.). This allows the user to check the accuracy of the zero in question (this could also be done using #math98#\x, but it would be more technical).

When the integer part of a real number x is not significant because the exponent of x is greater than the internal precision, an approximation to the integer part is printed, followed by a ``*'' instead of a decimal point, indicating <#2630#><#2630#>corrupt digits on the left.

Note also that a number of type integer or real is written without enclosing parentheses, while most other types have them. Hence, if you see printed the expression (3.14), it is not of type real, but probably of type complex with zero imaginary part (if you want to be sure, type #math99#\x).

Finally note that in the present version 1.35, the printing of 0, 1, and -1 as coefficients in beautified format, although understandable, is buggy. Hence the default is the raw format until this is improved.

by1=0 <#2632#>=0<#2634#><#2634#><#2632#>



<#2633#><#2635#>. Input formats for the PARI types.<#2635#><#2633#>


Apart from commands, the general input in GP after the prompt is a sequence of legal expressions. Before describing this in detail in the next section, let us see here how to input the different types of PARI.

Note that blanks are ignored in an expression.

by 1=0 <#2646#>=0<#2648#><#2648#><#2646#>

<#2647#><#2649#>.. <#2657#><#2657#>Integers<#2649#><#2647#>(type 1): type the integer (with an initial + or - if desired) with no decimal point.

by 1=0 <#2667#>=0<#2669#><#2669#><#2667#>

<#2668#><#2670#>.. <#2678#><#2678#>Real numbers<#2670#><#2668#>(type 2): type the number with a decimal point. The internal precision of the real number will be the supremum of the input precision and the default precision. For example, if the default precision is 28 digits, typing 2. will give a number with internal precision 28, but typing a 45 significant digit real number will give a number with internal precision at least 45 (although less may be printed).

You can also use scientific notation with the letter ``E'' or ``e'' (like 6.02 E 23 or 1e-5).

by 1=0 <#2688#>=0<#2690#><#2690#><#2688#>

<#2689#><#2691#>.. <#2699#><#2699#>Integermods<#2691#><#2689#>(type 3): to enter #math100#n mod m, type #math101#mod(n, m), not n%m (see 3.2.12.).

by 1=0 <#2711#>=0<#2713#><#2713#><#2711#>

<#2712#><#2714#>.. <#2722#><#2722#>Rational numbers<#2714#><#2712#>(types 4 and 5): under GP, all fractions are automatically reduced to lowest terms, so it is not possible to enter a non-irreducible fraction (type 5), although of course in library mode this is easy. To enter n/m just type it as written. As explained in section 3.1.4., division will <#169#>not<#169#> be performed, only reduction to lowest terms.

by 1=0 <#2732#>=0<#2734#><#2734#><#2732#>

<#2733#><#2735#>.. <#2743#><#2743#>Complex numbers<#2735#><#2733#>(type 6): to enter x + iy, type x + I*y or x + i*y. The letters I and #math102#i stand for #tex2html_wrap_inline17329#. Recall from chapter 1 that x and y can be of type integer, real, integermod fraction, or p-adic.

by 1=0 <#2747#>=0<#2749#><#2749#><#2747#>

<#2748#><#2750#>.. p-adic numbers<#2750#><#2748#><#2754#><#2754#> (type 7): to enter a p-adic number, simply write a rational or integer expression and add to it #math103#O(p#tex2html_wrap_inline17337#k), where p and k are integers. This last expression indicates three things to GP: first that it is dealing with a p-adic (the fact that p is an integer), second the ``prime'' p (note that no verification is done that p is indeed prime; you can work on 10-adics if you want, but beware of disasters as soon as you do something non trivial like taking a square root), and finally the number of significant p-adic digits k.

For example, to obtain the 7-adic number

#math104#

2*7#tex2html_wrap_indisplay17348#(- 1) + 3 + 4*7 + 2*7#tex2html_wrap_indisplay17349#2 + O(7#tex2html_wrap_indisplay17350#3),

you can type it exactly as shown, or equivalently as

#math105#

905/7 + O(7#tex2html_wrap_indisplay17352#3).

by 1=0 <#2764#>=0<#2766#><#2766#><#2764#>

<#2765#><#2767#>.. <#2775#><#2775#>Quadratic numbers<#2767#><#2765#>(type 8): first, you must define the default quadratic order or field in which you want to work. This is done using the <#1687#><#2777#><#2777#>quadgen<#1687#> function, in the following way. Write something like

w=quadgen(d)

where <#183#>d<#183#> is the <#184#>discriminant<#184#> of the quadratic order in which you want to work (hence #math106#d≡0 or 1 mod 4). The name <#186#>w<#186#> is of course just a suggestion, but corresponds to traditional usage. You can of course use any variable name that you like. However, quadratic numbers are always printed with a <#187#>w<#187#>, regardless of the discriminant. So beware, two numbers can be printed in the same way and not be equal. However GP will refuse to add or multiply them for example.

Then, (1, w) will be the ``canonical'' integral basis of the quadratic order (i.e. #math107#w = #tex2html_wrap_inline17356#/2 if #math108#d≡0 mod 4, and #math109#w = (1 + #tex2html_wrap_inline17359#)/2 if #math110#d≡1 mod 4, where d is the discriminant), and to enter x + yw you just type x + y*w or x + y*W.

by 1=0 <#2790#>=0<#2792#><#2792#><#2790#>

<#2791#><#2793#>.. <#2801#><#2801#>Polymods<#2793#><#2791#>(type 9): exactly as for integermods, to enter #math111#x mod y (where x and y are polynomials), type #math112#mod(x, y), not x%y (see 3.2.12.). Note that when y is an irreducible polynomial in one variable, polymods whose modulus is y are simply algebraic numbers in the finite extension defined by the polynomial y. This allows us to work easily in <#2805#><#2805#>number fields, finite extensions of the p-adic field #tex2html_wrap_inline17377#, or <#2808#><#2808#>finite fields.

<#194#>Important remark.<#194#> Since the variables occuring in a polymod are not free variables, it is essential in order to avoid inconsistencies, that all polymods have the same structure, i.e. the main variable will be number 0, the next number 1, etc.... In other words, since 0 is the variable number of ``X'', do <#195#>not<#195#> use expressions like <#196#>mod(Y,Y2̂+1)<#196#> since PARI will not recognize that this is identical to <#197#>mod(X,X2̂+1)<#197#>. Another consequence is that an operation like <#198#>X+mod(X,X2̂+1)<#198#> gives exactly the same thing as a result (meaning X + i where i2 = - 1), and not <#199#>mod(2*X,X2̂+1)<#199#>.

by 1=0 <#2818#>=0<#2820#><#2820#><#2818#>

<#2819#><#2821#>.. <#2829#><#2829#>Polynomials<#2821#><#2819#>(type 10): type the polynomial in a natural way, not forgetting to put a ``*'' between a coefficient and a formal variable (this * does not appear on beautified output). Any variable name can be used except for the reserved names <#201#>i<#201#> (used exclusively for the square root of -1), <#202#>pi<#202#> (3.14...) and <#203#>euler<#203#> (Euler's constant). The total number of different variable names is limited to 256, which should be enough. If you ever need hundreds of variables, you should probably use vectors instead.

by 1=0 <#2839#>=0<#2841#><#2841#><#2839#>

<#2840#><#2842#>.. <#2850#><#2850#>Power series<#2842#><#2840#>(type 11): type a rational function or polynomial expression and add to it #math113#O(expr#tex2html_wrap_inline17386#k), where expr is an expression having a non zero valuation, like a polynomial, power series, or a rational function (the most common cases being simply expr =variable name). This indicates to GP that it is dealing with a power series, and the desired precision is k times the valuation of expr with respect to its main variable (to check the order of the variables, or to modify it, use the function <#206#>reorder<#206#> see 3.9.4.6).

by 1=0 <#2860#>=0<#2862#><#2862#><#2860#>

<#2861#><#2863#>.. <#2871#><#2871#>Rational functions<#2863#><#2861#>(types 13 and 14): under GP, all fractions are automatically reduced to lowest terms, so it is not possible to enter a non-irreducible rational function (type 14), although of course in library mode this is easy. To enter x/y just type it as written. As explained in section 3.1.4., division will <#208#>not<#208#> be performed, only reduction to lowest terms.

by 1=0 <#2881#>=0<#2883#><#2883#><#2881#>

<#2882#><#2884#>.. <#2892#><#2892#>Binary quadratic forms of positive discriminant<#2884#><#2882#>(type 15): enter a 4-component (row or column) vector, and transform it into a binary quadratic form using the function <#210#>qfr<#210#> (see 3.4.32). The fourth component (which is related to the regulator, more precisely to Shanks' ``distance'') must be a real number. See also the function <#211#>pf<#211#> which directly creates a prime form of given discriminant (see 3.4.29).

by 1=0 <#2902#>=0<#2904#><#2904#><#2902#>

<#2903#><#2905#>.. <#2913#><#2913#>Binary quadratic forms of negative discriminant<#2905#><#2903#>(type 16): enter a 3-component (row or column) vector, and transform it into a binary quadratic form using the function <#213#>qfi<#213#> (see 3.4.32). See also the function <#214#>pf<#214#> which directly creates a prime form of given discriminant (see 3.4.29).

by 1=0 <#2915#>=0<#2917#><#2917#><#2915#>

<#2916#><#2918#>.. Row and column vectors<#2918#><#2916#> <#2922#><#2922#><#2924#><#2924#> (types 17 and 18): to enter a row vector, type the components separated by commas ``,'', and enclosed between brackets ``['' and ``]'', e.g. [x, y, z]. To enter a column vector, type the vector horizontally, and add a tilde ``#math114##tex2html_wrap_inline17394#'' to transpose.

by 1=0 <#2926#>=0<#2928#><#2928#><#2926#>

<#2927#><#2929#>.. Matrices<#2929#><#2927#>(type 19):<#2933#><#2933#> to enter a matrix, type the components line by line, the components being separated by commas ``,'', the lines by semicolons ``;'', and everything enclosed in brackets ``['' and ``]'', e.g. #math115#[x, y;z, t;u, v].

Note that although the internal representation is essentially the same, (only the type number being different), a row vector of column vectors is <#221#>not<#221#> a matrix; for example, multiplication will not work in the same way. To transform it into a matrix, use the function <#222#>mat<#222#> (see 3.7.22).

by1=0 <#2935#>=0<#2937#><#2937#><#2935#>



<#2936#><#2938#>. The general GP input line.<#2938#><#2936#>


A general GP session goes as follows: a sequence of characters is typed by the user after the prompt. This can be either a function definition, or an expression or a sequence of expressions (i.e. a program). In this case, after the last expression is computed the result is put in an internal array. The successive elements of this array are called %1, %2, ...under GP, and they can also be used in library mode with the names <#224#>g[1], g[2], ...<#224#><#2941#><#2941#> In this case, the array is not automatically filled for you. As a shortcut, the last computed expression can also be called %.

If you want to suppress the printing of the result, for example because it is a long unimportant intermediate result, end the expression with a ``;'' sign. This same sign is used as an instruction separator when several instructions are written on the same line (note that the ``:'' sign can also be used, but we will try to keep C-style conventions). Note that the last expression you have computed, even if not printed, still has the name (%1, %2, ...) so care must be used when using it subsequently since its number does not appear explicitly. Of course, if it is simply used on the next line, use % as usual.

Any legal expressions can be typed, and it is evaluated using the usual conventions about operator priorities and left to right evaluation (including raising to a power), using the available operator symbols, function names (including user-defined functions), and special variables. Please note that in general there<#2943#><#2943#> is no distinction between lowercase. and uppercase Also, note that blanks are completely ignored in the input to GP.

The special variable names used by GP are <#1694#><#2945#><#2945#>euler<#1694#> (Euler's constant=0.577...), <#1695#><#2947#><#2947#>i<#1695#> (the square root of -1), <#1696#><#2949#><#2949#>pi<#1696#> (3.14...), which are really functions without parameters, and <#1697#><#2951#><#2951#>O<#1697#> whose syntax is the follwing:

<#231#>O<#231#>#math116#(expr#tex2html_wrap_inline17404#k)

When expr is an integer or a rational number, creates a expr-adic number (zero in fact) of precision k. When expr is a polynomial, a power series or a rational function whose main variable is X, say, this creates a power series (also zero) of precision v*k where v is the X-adic valuation of expr (see 2.3.6 and 2.3.9.).

Note: as usual, lowercase <#233#>o<#233#> is allowed, but we do not advise to use it since it has a different mathematical meaning.

by1=0 <#2953#><#2953#> <#2954#>=0<#2956#><#2956#><#2954#>

Chapter : Functions and Operations available in PARI and GP

The functions and operators available in PARI and in the GP/PARI calculator are numerous and everexpanding. Here is a description of the ones available in version 1.35. It should be noted that many of these functions accept quite different types as arguments, but others are more restricted. The list of acceptable types will be given for each function or class of functions. Except when stated otherwise, it is understood that a function or operation which should make natural sense is legal. In this chapter, we will describe the functions according to a rough classification. For the functions in alphabetical order, see the general index.

by1=0 <#2959#>=0<#2961#><#2961#><#2959#>



<#2960#><#2962#>. Standard monadic or dyadic operators.<#2962#><#2960#>


by 1=0 <#2965#>=0<#2967#><#2967#><#2965#>

<#2966#><#2968#>.. ±<#2968#><#2966#>+ x and - x refer to monadic operators (+ x does nothing and - x negates x).

The library syntax is #math117##tex2html_wrap_inline17422#(x) for - x.

by 1=0 <#2975#>=0<#2977#><#2977#><#2975#>

<#2976#><#2978#>.. +, -<#2978#><#2976#>x + y and x - y are the <#2982#><#2982#>sum and <#2984#><#2984#>difference of x and y. Among the prominent impossibilities are addition/subtraction between a scalar type and a vector or a matrix, between vector/matrices of incompatible sizes and between an integermod and a real number.

The library syntax is #math118##tex2html_wrap_inline17433#(x, y) for x + y, #math119##tex2html_wrap_inline17436#(x, y) for x - y.

by 1=0 <#2992#>=0<#2994#><#2994#><#2992#>

<#2993#><#2995#>.. *<#2995#><#2993#>x*y is the <#2999#><#2999#>product of x and y. Among the prominent impossibilities are multiplication between vector/matrices of incompatible sizes, between an integermod and a real number. Note that because of vector and matrix operations, * is not necessarily commutative. Note also that since multiplication between two column or two row vectors is not allowed, to obtain the <#3001#><#3001#>scalar product of two vectors of the same length, you must multiply a line vector by a column vector, if necessary by transposing one of the vectors (using <#1698#>˜<#1698#> or <#247#>trans( )<#247#>, see 3.7.30).

The library syntax is #math120##tex2html_wrap_inline17446#(x, y) for x*y.

by 1=0 <#3006#>=0<#3008#><#3008#><#3006#>

<#3007#><#3009#>.. /<#3009#><#3007#>x/y is the <#3013#><#3013#>quotient of x and y. In addition to the impossibilities for multiplication, note that if the divisor is a matrix, it must be an invertible square matrix, and in that case the result is x*y-1. Furthermore note that the result is as exact as possible: in particular, division of two integers always gives a rational number (which may be an integer if the quotient is exact) and <#252#>not<#252#> the euclidean quotient (see #math121#x\y for that), and similarly the quotient of two polynomials is a rational function in general. To obtain the approximate real value of the quotient of two integers, add 0. to the result; to obtain the approximate p-adic value of the quotient of two integers, add O(pk) to the result; finally, to obtain the <#3015#><#3015#>taylor series expansion of the quotient of two polynomials, add #math122#O(X#tex2html_wrap_inline17459#k) to the result or use the <#255#>taylor<#255#> function (3.6.33).

The library syntax is #math123##tex2html_wrap_inline17461#(x, y) for x/y.

by 1=0 <#3020#>=0<#3022#><#3022#><#3020#>

<#3021#><#3023#>.. #math125#\<#3023#><#3021#>#math126#x\y is the <#3027#><#3027#>euclidean quotient of x and y. The types must be either both integer or both polynomials. The result is the euclidean quotient. In the case of integer division, the quotient is such that the corresponding remainder is nonnegative.

The library syntax is #math127##tex2html_wrap_inline17469#(x, y) for #math128#x\y.

by 1=0 <#3032#>=0<#3034#><#3034#><#3032#>

<#3033#><#3035#>.. %<#3035#><#3033#>x%y is the <#3039#><#3039#>euclidean remainder of x and y. The modulus y must be of type integer or polynomial. The result is the remainder, always nonnegative in the case of integers. Allowed dividend types are scalar exact types when the modulus is an integer, and polynomials, polymods and rational functions when the modulus is a polynomial.

The library syntax is #math129##tex2html_wrap_inline17478#(x, y) for x%y.

by 1=0 <#3052#>=0<#3054#><#3054#><#3052#>

<#3053#><#3055#>.. <#3063#><#3063#>divres<#3055#><#3053#>(x, y) creates a vector with two components, the first being the euclidean quotient, the second the euclidean remainder, of the division of x by y. This avoids the need to do two divisions if one needs both the quotient and the remainder. The arguments must be both integers or both polynomials, and in the case of integers the remainder is always nonnegative.

The library syntax is #math130##tex2html_wrap_inline17484#(x, y).

by 1=0 <#3076#>=0<#3078#><#3078#><#3076#>

<#3077#><#3079#>.. #tex2html_wrap_inline17488#<#3079#><#3077#>#math131#x#tex2html_wrap_inline17490#y is <#3085#><#3085#>powering. If the exponent is an integer, then exact operations are performed using binary powering techniques. In particular, in this case the first argument cannot be a vector or matrix unless it is a square matrix (and moreover invertible if the exponent is negative). If the exponent is not of type integer, this is treated as a transcendental function (see 3.3.1.), and in particular has the effect of componentwise powering on vector or matrices.

The library syntax is #math132##tex2html_wrap_inline17492#(x, y) for #math133#x#tex2html_wrap_inline17494#y.

by 1=0 <#3098#>=0<#3100#><#3100#><#3098#>

<#3099#><#3101#>.. comparison and <#3109#><#3109#>boolean operators<#3101#><#3099#>. The six standard <#3111#><#3111#>comparison operators ;SPMlt; =, ;SPMlt;, ;SPMgt; =, ;SPMgt;, = =, ! = are available in GP, and in library mode under the names <#1702#><#3113#><#3113#>gle, <#3115#><#3115#>glt, <#3117#><#3117#>gge, <#3119#><#3119#>ggt, <#3121#><#3121#>geq, <#3123#><#3123#>gne<#1702#> respectively. The library syntax is #math134##tex2html_wrap_inline17502#(x, y), where co is the comparison operator. The result is 1 if the comparison is true, 0 if it is false.

The standard boolean functions | | (<#3125#><#3125#>inclusive or) and (<#3127#><#3127#>and) <#3129#><#3129#>are also available, and the library syntax is #math135##tex2html_wrap_inline17506#(x, y) and #math136##tex2html_wrap_inline17508#(x, y) respectively. Note that to avoid confusion with the factorial function, there is no ! (<#3137#><#3137#>not) operator, but this can easily be circumvented.

In library mode, it is in fact usually preferable to use the two basic functions which are #math137##tex2html_wrap_inline17511#(x, y) which gives the sign (1, 0, or -1) of x - y, where x and y must be in #tex2html_wrap_inline17516#, and #math138##tex2html_wrap_inline17518#(x, y) which can be applied to any two PARI objects x and y and gives 1 (i.e. true) if they are equal (but not necessarily identical), 0 (i.e. false) otherwise. Particular cases of gegal which should be used are #math139##tex2html_wrap_inline17522#(x) (x = 0 ?), #math140##tex2html_wrap_inline17525#(x) (x = 1 ?), and <#3152#><#3152#> <#290#>gcmp_1<#290#> (x) (x = - 1 ?).

Note that #math141##tex2html_wrap_inline17530#(x) tests whether x is equal to zero, even if x is not an exact object. To test whether x is an exact object which is equal to zero, one must use #math142##tex2html_wrap_inline17535#.

GP accepts the following synonyms for some of the above functions: since there is no bitwise <#293#>and<#293#> or bitwise <#294#>or<#294#>, | and are accepted as<#3160#><#3160#> <#3162#><#3162#> synonyms of | | and respectively. Also, ;SPMlt; ;SPMgt; is accepted as a synonym for ! =. On the other hand, = is definitely <#297#>not<#297#> a synomym for = = since it is the assignment statement.

by 1=0 <#3164#>=0<#3166#><#3166#><#3164#>

<#3165#><#3167#>.. sign<#3167#><#3165#>(x): <#3171#><#3171#>sign of x, which must be of type integer, real or fraction. The result (0, 1 or -1) is a C-integer.

The library syntax is #math143##tex2html_wrap_inline17547#(x).

by 1=0 <#3184#>=0<#3186#><#3186#><#3184#>

<#3185#><#3187#>.. <#3195#><#3195#>max<#3187#><#3185#>(x, y) and <#1704#><#3197#><#3197#>min<#1704#>(x, y) create the maximum and minimum of x and y when they can be compared.

The library syntax is #math144##tex2html_wrap_inline17553#(x, y) and #math145##tex2html_wrap_inline17555#(x, y).

<#3205#>=0<#3207#><#3207#><#3205#>

<#3206#><#3208#>Important remark.<#3208#><#3206#>In all the above library syntaxes, we have given only the basic names of the functions. For example #math146##tex2html_wrap_inline17557#(x, y) assumes that x and y are PARI objects (the GEN type) and <#307#>creates<#307#> the result x + y on the PARI stack. From most of these basic names, the names and effects of other functions can be obtained in the following way. We give the example for gadd, but the same is true for all the other functions above, and a few more that we have not given:

#math147##tex2html_wrap_inline17562#(x, y): here x is a GEN, y is an ordinary 32-bit integer.

#math148##tex2html_wrap_inline17566#(x, y): here x is an ordinary 32-bit integer, y is a GEN.

#math149##tex2html_wrap_inline17570#(x, y, z), #tex2html_wrap_inline17571#(x, y, z), #tex2html_wrap_inline17572#(x, y, z): here z is a preexisting GEN and the result of the corresponding operation is put in z. The size of the PARI stack does not change.

As simplification to programming, for many functions beginning with a <#313#>g<#313#>, such as gadd, one can replace the <#314#>g<#314#> by an <#315#>l<#315#> to obtain a <#316#>long<#316#> result instead of a GEN (i.e. pointer to long) result. For instance #math150##tex2html_wrap_inline17576#(x, y) is identical with #math151#(#tex2html_wrap_inline17578#(x, y)). These macros are written in the files listed in <#1705#><#3230#><#3230#>gencom.h<#1705#>.

by1=0 <#3232#>=0<#3234#><#3234#><#3232#>



<#3233#><#3235#>. Conversions and similar elementary functions<#3235#><#3233#>


Many of these conversion functions are rounding or truncating operations. In this case, if the argument is a rational function, the result is the euclidean quotient of the numerator by the denominator, and if the argument is a vector or a matrix, the operation is done componentwise. This will not be restated for every function.

<#3238#><#3238#> by 1=0 <#3239#>=0<#3242#><#3242#><#3239#>

<#3240#><#3243#>.. binary<#3243#><#3240#>(x): outputs the vector of the binary digits of | x|, where #math152#| x| ;SPMlt; 232.

The library syntax is #math153##tex2html_wrap_inline17583#(x).

<#3250#><#3250#> by 1=0 <#3251#>=0<#3254#><#3254#><#3251#>

<#3252#><#3255#>.. bittest<#3255#><#3252#>(x, n): outputs the #math154#nth bit of | x| starting from the right (i.e. the coefficient of 2n in the binary expansion of x. The result is 0 or 1.

The library syntax is #math155##tex2html_wrap_inline17590#(x, n) where n as well as the result are C-integers.

by 1=0 <#3271#>=0<#3273#><#3273#><#3271#>

<#3272#><#3274#>.. <#3282#><#3282#>ceil<#3274#><#3272#>(x): ceiling of x. When x is in #tex2html_wrap_inline17596#, the result is the smallest integer greater than or equal to x.

The library syntax is #math156##tex2html_wrap_inline17599#(x).

by 1=0 <#3296#>=0<#3298#><#3298#><#3296#>

<#3297#><#3299#>.. <#3307#><#3307#>changevar<#3299#><#3297#>(x, y): change the variables in the object x according to the permutation specified by the vector y. For example, assume that the variables have been introduced in the order <#330#>x<#330#>, <#331#>a<#331#>, <#332#>b<#332#>, <#333#>c<#333#>. Then, if y is the vector <#334#>[x,c,a,b]<#334#>, the variable <#335#>a<#335#> will be replaced by <#336#>c<#336#>, <#337#>b<#337#> by <#338#>a<#338#>, and <#339#>a<#339#> by <#340#>b<#340#>, <#341#>x<#341#> being unchanged. Note that the permutation must be completely specified, e.g. <#342#>[c,a,b]<#342#> would not work, since this would replace <#343#>x<#343#> by <#344#>c<#344#>, and leave <#345#>a<#345#> and <#346#>b<#346#> unchanged.

The library syntax is #math157##tex2html_wrap_inline17605#(x, y).

by 1=0 <#3320#>=0<#3322#><#3322#><#3320#>

<#3321#><#3323#>.. <#3331#><#3331#>components of a PARI object<#3323#><#3321#>:

There are essentially three ways to extract the components from a PARI object.

The first and most general, is the function #math158##tex2html_wrap_inline17607#(x, n) which extracts the #math159#nth-component of x. This is to be understood as follows: every PARI type has one or two initial <#3337#><#3337#>code words. The components are counted, starting at 1, after these code words. In particular if x is a vector, this is indeed the #math160#nth-component of x, if x is a matrix, the #math161#nth column, if x is a polynomial, the #math162#nth coefficient (i.e. of degree n - 1), and for power series, the #math163#nth significant coefficient. The use of the function <#356#>compo<#356#> implies the knowledge of the structure of the different PARI types, which can be recalled by typing #math164#\t under GP.

The library syntax is #math165##tex2html_wrap_inline17621#(x, n), where n is a 32-bit C-integer.

The two other methods are more natural but more restricted. First, the function #math166##tex2html_wrap_inline17624#(x, n) gives the coefficient of degree n of the polynomial or power series x, with respect to the main variable of x (to see the order of the variables or to change it, use the function <#359#>reorder<#359#>, see 3.9.4.6). In particular if n is less than the valuation of x or in the case of a polynomial, greater than the degree, the result is zero (contrary to <#360#>compo<#360#> which would send an error message). If x is a power series and n is greater than the largest significant degree, then an error message is issued.

For greater flexibility, vector or matrix types are also accepted for x, and the meaning is then identical with that of <#361#>compo<#361#>.

Finally note that a scalar type is considered by <#362#>coeff<#362#> as a polynomial of degree zero.

The library syntax is #math167##tex2html_wrap_inline17634#(x, n).

The third method is specific to vectors or matrices under GP. If x is a (row or column) vector, then <#1715#><#3352#><#3352#>x[n]<#1715#> represents the #math168#nth component of x, i.e. <#366#>compo(x,n)<#366#>. It is more natural and shorter to write. If x is a matrix, <#1717#><#3355#><#3355#>x[m,n]<#1717#> represents the coefficient of row <#368#>m<#368#> and column <#369#>n<#369#> of the matrix.

Finally note that in library mode, the macro <#370#>coeff(x,m,n)<#370#> exists with exactly the meaning of <#371#>x[m,n]<#371#> under GP when x is a matrix. This macro should not be confused with the two-variable macro <#372#>coeff<#372#> used primarily for polynomials and power series under GP.<#3357#><#3357#>

<#3359#><#3359#> by 1=0 <#3360#>=0<#3363#><#3363#><#3360#>

<#3361#><#3364#>.. conj<#3364#><#3361#>(x) or x#tex2html_wrap_inline17642#: conjugate of x. The meaning of this is clear, except that for real quadratic numbers, it means conjugation in the real quadratic field. This function has no effect on integers, reals, integermods, fractions or p-adics. The only forbidden type is polymod.

The library syntax is #math169##tex2html_wrap_inline17646#(x).

<#3371#><#3371#> by 1=0 <#3372#>=0<#3375#><#3375#><#3372#>

<#3373#><#3376#>.. cvtoi<#3376#><#3373#>(x): If x is in #tex2html_wrap_inline17650#, truncates x to an integer. If the result is not significant because the exponent of x is too large compared to its precision, no error occurs, but the number of lost significant bits is put at the address specified in a second argument. This is transparent under GP, but this second argument must be given in library mode. If no digits are lost, this second argument contains the negative of the number of significant bits of the fractional part.

The library syntax is #math170##tex2html_wrap_inline17654#(x,e) where e is a 32-bit C-integer.

<#3384#><#3384#> by 1=0 <#3385#>=0<#3388#><#3388#><#3385#>

<#3386#><#3389#>.. denom<#3389#><#3386#>(x): lowest denominator of x. The meaning of this is clear when x is a rational number or function. When x is an integer or a polynomial, the result is equal to 1. When x is a vector or a matrix, the lowest common denominator of the components of x is computed. All other types are forbidden.

The library syntax is #math171##tex2html_wrap_inline17663#(x).

<#3396#><#3396#> by 1=0 <#3397#>=0<#3400#><#3400#><#3397#>

<#3398#><#3401#>.. floor<#3401#><#3398#>(x): floor of x. When x is in #tex2html_wrap_inline17668#, the result is the largest integer smaller than or equal to x.

The library syntax is #math172##tex2html_wrap_inline17671#(x).

<#3409#><#3409#> by 1=0 <#3410#>=0<#3413#><#3413#><#3410#>

<#3411#><#3414#>.. frac<#3414#><#3411#>(x): fractional part of x. Identical to #math173#x - floor(x). If x is real, the result is in [0, 1[.

The library syntax is #math174##tex2html_wrap_inline17678#(x).

<#3422#><#3422#> by 1=0 <#3423#>=0<#3426#><#3426#><#3423#>

<#3424#><#3427#>.. imag<#3427#><#3424#>(x): imaginary part of x. When x is a quadratic number, this is the coefficient of ω in the ``canonical'' integral basis #math175#(1, ω).

The library syntax is #math176##tex2html_wrap_inline17685#(x).

<#3434#><#3434#> by 1=0 <#3435#>=0<#3438#><#3438#><#3435#>

<#3436#><#3439#>.. length<#3439#><#3436#>(x): number of non-code words in x really used (i.e. the effective length for integers and polynomials). In particular, the degree of a polynomial is equal to its length minus 1.

The library syntax is #math177##tex2html_wrap_inline17689#(x).

<#3446#><#3446#> by 1=0 <#3447#>=0<#3450#><#3450#><#3447#>

<#3448#><#3451#>.. lift<#3451#><#3448#>(x): lifts an element #math178#x = a mod n of #math179##tex2html_wrap_inline17693#/n#tex2html_wrap_inline17694# to a in #tex2html_wrap_inline17697#, and similarly lifts a polymod to a polynomial. If x is of type fraction, complex, quadratic, polynomial, power series, rational function, vector or matrix, the lift is done for each coefficient. Forbidden types for x are reals and p-adics. Note that the main variable of the lift of a polymod will be variable number 0 (i.e. 'X') if the rules concerning the creation of polymods explained in chapter 2 are observed.

The library syntax is #math180##tex2html_wrap_inline17702#(x).

<#3461#><#3461#> by 1=0 <#3462#>=0<#3465#><#3465#><#3462#>

<#3463#><#3466#>.. mod<#3466#><#3463#>(x, y): creates the PARI object #math181#(x mod y), i.e. an integermod or a polymod. y must be an integer or a polynomial. If y is an integer, x must be an integer. If y is a polynomial, x must be a scalar or a polynomial. The result is put on the PARI stack.

This function is not the same as x%y, the result of which is an integer or a polynomial.

The library syntax is #math182##tex2html_wrap_inline17712#(x, y).

<#3474#><#3474#> by 1=0 <#3475#>=0<#3478#><#3478#><#3475#>

<#3476#><#3479#>.. modp<#3479#><#3476#>(x, y): same effect as <#394#>mod<#394#>, except that the created result is put on the heap and not on the stack, and hence becomes a permanent copy which cannot be erased later by garbage collecting (see 4.2.4). In particular, care should be taken to avoid creating too many such objects, since the heap is very small (typically a few thousand objects at most).

The library syntax is #math183##tex2html_wrap_inline17715#(x, y).

<#3486#><#3486#> by 1=0 <#3487#>=0<#3490#><#3490#><#3487#>

<#3488#><#3491#>.. norm<#3491#><#3488#>(x): algebraic norm of x, i.e. the product of x with its conjugate (no square roots are taken), or conjugates for polymods. For vectors and matrices, the norm is taken componentwise and hence is not the L2-norm (see 3.2.15). Note that the norm of an element of #tex2html_wrap_inline17721# is its square, so as to be compatible with the complex norm.

The library syntax is #math184##tex2html_wrap_inline17723#(x).

<#3499#><#3499#> by 1=0 <#3500#>=0<#3503#><#3503#><#3500#>

<#3501#><#3504#>.. norml2<#3504#><#3501#>(x): square of the L2-norm of x. x must be a (row or column) vector.

The library syntax is #math185##tex2html_wrap_inline17729#(x).

<#3511#><#3511#> by 1=0 <#3512#>=0<#3515#><#3515#><#3512#>

<#3513#><#3516#>.. numer<#3516#><#3513#>(x): numerator of x. When x is a rational number or function, the meaning is clear. When x is an integer or a polynomial the result is x itself. When x is a vector or a matrix, then <#401#>numer(x)<#401#> is defined to be <#402#>denom(x)*x<#402#>. All other types are forbidden.

The library syntax is #math186##tex2html_wrap_inline17737#(x).

<#3523#><#3523#> by 1=0 <#3524#>=0<#3527#><#3527#><#3524#>

<#3525#><#3528#>.. poly<#3528#><#3525#>(x, v): transform the object x into a polynomial with main variable v. If x is a scalar, this gives a constant polynomial. If x is a power series, the effect is identical to <#405#>trunc<#405#> (see 3.2.25), i.e. it chops off the O(Xk). If x is a vector, this function creates the polynomial whose coefficients are given in x, with x[1] being the leading coefficient (which can be zero).

Warning: this is <#406#>not<#406#> a substitution function. It is intended to be quick and dirty. So if you try <#407#>poly(a,y)<#407#> on the polynomial <#408#>a=x+y<#408#>, you will get <#409#>y+y<#409#>, which is not a valid PARI/GP object.

The library syntax is #math187##tex2html_wrap_inline17748#(x, v), where v is a variable number.

<#3535#><#3535#> by 1=0 <#3536#>=0<#3539#><#3539#><#3536#>

<#3537#><#3540#>.. prec<#3540#><#3537#>(x, n): x being a PARI object and n a 32-bit C-integer, creates a new object equal to x but with a new precision n. This is to be understood as follows:

For exact types, no change. For x a vector or a matrix, the operation is done componentwise.

For real x, n is the number of desired significant <#412#>decimal<#412#> digits. If n is smaller than the precision of x, x is truncated, otherwise x is extended with zeros.

For x a p-adic or a power series, n is the desired number of significant p-adic or X-adic digits, where X is the main variable of x.

Note that the function <#413#>prec<#413#> never changes the type of the result. In particular it is not possible to use <#414#>prec<#414#> to obtain a polynomial from a power series. For that, see <#415#>trunc<#415#> (3.2.25 below).

The library syntax is #math188##tex2html_wrap_inline17770#(x, n), where n is a C-integer.

<#3547#><#3547#> by 1=0 <#3548#>=0<#3551#><#3551#><#3548#>

<#3549#><#3552#>.. quadgen<#3552#><#3549#>(x): creates the quadratic number <#3556#><#3556#> #math189#ω = (a + #tex2html_wrap_inline17774#)/2 where a = 0 if #math190#x≡0 mod 4, a = 1 if #math191#x≡1 mod 4, so that #math192#(1, ω) is an integral basis for the quadratic order of discriminant x. x must be an integer congruent to 0 or 1 modulo 4.

The library syntax is #math193##tex2html_wrap_inline17783#(x).

<#3563#><#3563#> by 1=0 <#3564#>=0<#3567#><#3567#><#3564#>

<#3565#><#3568#>.. quadpoly<#3568#><#3565#>(x): creates the ``canonical'' quadratic polynomial corresponding to the discriminant x, i.e. the minimal polynomial of <#422#>quadgen(x)<#422#>. x must be an integer congruent to 0 or 1 modulo 4.

The library syntax is #math194##tex2html_wrap_inline17788#(x).

<#3575#><#3575#> by 1=0 <#3576#>=0<#3579#><#3579#><#3576#>

<#3577#><#3580#>.. real<#3580#><#3577#>(x): real part of x. In the case where x is a quadratic number, this is the coefficient of 1 in the ``canonical'' integral basis #math195#(1, ω).

The library syntax is #math196##tex2html_wrap_inline17795#(x).

<#3587#><#3587#> by 1=0 <#3588#>=0<#3591#><#3591#><#3588#>

<#3589#><#3592#>.. rndtoi<#3592#><#3589#>(x): same as <#427#>cvtoi<#427#> (see 3.2.5) except that truncation is replaced by rounding (see 3.2.23).

The library syntax is #math197##tex2html_wrap_inline17798#(x,e).

<#3599#><#3599#> by 1=0 <#3600#>=0<#3603#><#3603#><#3600#>

<#3601#><#3604#>.. round<#3604#><#3601#>(x): If x is in #tex2html_wrap_inline17802#, rounds x to the nearest integer. If the exponent of x is too large compared to its precision, the result is undefined and an error message occurs. Use <#430#>rndtoi<#430#> (3.2.22) to suppress this error handling.

Important remark: note that, contrary to the other truncation functions (except <#431#>rndtoi<#431#> which is essentially the same), this function operates on every coefficient at every level of a PARI object. For example

#math198#

trunc#tex2html_wrap_indisplay17806##tex2html_wrap_indisplay17807##tex2html_wrap_indisplay17808# = 2.0*X,

while

#math199#

round#tex2html_wrap_indisplay17810##tex2html_wrap_indisplay17811##tex2html_wrap_indisplay17812# = #tex2html_wrap_indisplay17813#.

An important use of <#440#>round<#440#> is to get exact results after a long approximate computation, when theory tells you that the coefficients must be integers.

The library syntax is #math200##tex2html_wrap_inline17815#(x).

<#3623#><#3623#> by 1=0 <#3624#>=0<#3627#><#3627#><#3624#>

<#3625#><#3628#>.. series<#3628#><#3625#>(x, v): transform the object x into a power series with main variable v. If x is a scalar, this gives a constant power series with precision given by the global variable <#443#>precdl<#443#> (transparent under GP). If x is a polynomial, the precision is the greatest of <#444#>precdl<#444#> and the degree of the polynomial. If x is a vector, the precision is similarly given, and the coefficients of the vector are understood to be the coefficients of the power series starting from the constant term (i.e. the reverse of the function <#445#>poly<#445#>, see 3.2.17 above).

The warning given for <#446#>poly<#446#> applies here: this is not a substitution function.

The library syntax is #math201##tex2html_wrap_inline17823#(x, v), where v is a variable number.

<#3635#><#3635#> by 1=0 <#3636#>=0<#3639#><#3639#><#3636#>

<#3637#><#3640#>.. setprecision<#3640#><#3637#>(n): sets the current default precision equal to n decimal digits if n ;SPMgt; 0. In any case, it returns the value of the current default precision. Apart from the fact that it returns a value, this is identical to the #math202#\precision=n command, with the difference that it can be used inside any GP expression or program.

The library syntax is #math203##tex2html_wrap_inline17831#(n), where n is a C-integer, as is the returned value.

<#3647#><#3647#> by 1=0 <#3648#>=0<#3651#><#3651#><#3648#>

<#3649#><#3652#>.. setserieslength<#3652#><#3649#>(n): sets the current default series length equal to n if n ;SPMgt; 0. In any case, it returns the value of the current default length. Apart from the fact that it returns a value, this is identical to the #math204#\serieslength=n command, with the difference that it can be used inside any GP expression or program.

The library syntax is #math205##tex2html_wrap_inline17839#(n), where n is a C-integer, as is the returned value.

<#3659#><#3659#> by 1=0 <#3660#>=0<#3663#><#3663#><#3660#>

<#3661#><#3664#>.. trunc<#3664#><#3661#>(x): truncation of x. When x is in #tex2html_wrap_inline17845#, this means that the part after the decimal point is chopped away. If the result is not significant because the exponent is too large compared to the precision, an error occurs. Use <#453#>cvtoi<#453#> (3.2.5) to suppress this error handling.

Note a very special use of <#454#>trunc<#454#>: when applied to a power series, it transforms it into a polynomial or a rational function with denominator a power of X, by chopping away the O(Xk). Similarly, when applied to a p-adic number, it transforms it into an integer or a rational number by chopping away the O(pk).

The library syntax is #math206##tex2html_wrap_inline17851#(x).

<#3672#><#3672#> by 1=0 <#3673#>=0<#3676#><#3676#><#3673#>

<#3674#><#3677#>.. type<#3677#><#3674#>(x): internal type number (from 1 to 19) of the pari object x. This is useful only under GP.

The library syntax is #math207##tex2html_wrap_inline17855#(x), but need not be used since the internal function <#458#>typ<#458#> is available. Note that <#459#>gtype<#459#> gives a GEN while <#460#>typ<#460#> gives a C-integer.

<#3684#><#3684#> by 1=0 <#3685#>=0<#3688#><#3688#><#3685#>

<#3686#><#3689#>.. valuation<#3689#><#3686#>(x, p): Computes the highest exponent of p dividing x. If p is of type integer, x must be an integer, an integermod whose modulus is divible by p, a fraction, a q-adic number with q = p, or a polynomial or power series in which case the valuation is the minimum of the valuation of the coefficients.

If p is of type polynomial, x must be of type polynomial or rational function, and also a power series if x is a monomial. If x = 0, an error is issued except in case of p-adic numbers and power series, in which case the result is the exponent of the zero. Finally, the valuation of a vector, complex or quadratic number is the minimum of the component valuations. Any other type combinations gives an error.

The library syntax is #math208##tex2html_wrap_inline17870#(x).

<#3696#><#3696#> by 1=0 <#3697#>=0<#3700#><#3700#><#3697#>

<#3698#><#3701#>.. vec<#3701#><#3698#>(x): transform the object x into a vector. The vector will be with one component only, except when x is a vector/matrix or a quadratic form (in which case the resulting vector is simply the initial object considered as a row vector), but more importantly when x is a polynomial or a power series. In the case of a polynomial, the coefficients of the vector starts with the leading coefficient of the polynomial, while for power series only the significant coefficients are taken into account, but this time by increasing order of degree.

The library syntax is #math209##tex2html_wrap_inline17876#(x).

by1=0 <#3708#>=0<#3710#><#3710#><#3708#>



<#3709#><#3711#>. Transcendental functions.<#3711#><#3709#>


As a general rule, which of course in some cases may have exceptions, transcendental functions operate in the following way:

;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;If the argument is either an integer, a real, a rational, a complex or a quadratic number, it is, if necessary, first converted to a real (or complex) number using the current <#3714#><#3714#>precision held in the variable <#467#>prec<#467#>. Under GP this is transparent to the user, but when programming in library mode, care must be taken to supply the parameter <#468#>prec<#468#> as the last argument of the function if the first argument is an exact object (see 1.2.5.), otherwise disaster will occur.

Then, the function is computed. Note that even if the argument is real, the result may be complex (e.g. #math210#acos(2.0) or #math211#acosh(0.0)). Note also that the principal branch is always chosen.

;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;If the argument is an integermod or a p-adic, at present only a few functions like <#471#>sqrt<#471#> (square root), <#472#>sqr<#472#> (square), <#473#>log<#473#>, <#474#>exp<#474#>, powering, <#475#>teich<#475#> (Teichmüller character) and <#476#>agm<#476#> (arithmetic-geometric mean) are implemented. Note that in the case of a 2-adic number, #math212##tex2html_wrap_inline17882#(x) is not identical to x*x: for example if #math213#x = 1 + O(25) then #math214#x*x = 1 + O(25) while #math215##tex2html_wrap_inline17887#(x) = 1 + O(26). (Remark: note that if we wanted to be strictly consistent with the PARI philosophy, we should have #math216#x*y = (4 mod 8) when both x and y are congruent to 2 modulo 4, or #math217##tex2html_wrap_inline17894#(x) = (4 mod 32) when x is congruent to 2 modulo 4. However, since an integermod is an exact object, PARI assumes that the modulus must not change, and the result is hence #math218#0 mod 4 in both cases. On the other hand, p-adics are not exact objects, hence are treated differently.)

;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;If the argument is a polynomial, power series or rational function, it is, if necessary, first converted to a power series using the current precision held in the variable <#1718#><#3721#><#3721#>precdl<#1718#>. Under GP this again is transparent to the user. When programming in library mode, however, the global variable <#481#>precdl<#481#> must be set before calling the function if the argument has an exact type (i.e. not a power series). Here <#482#>precdl<#482#> is not an argument of the function, but a global variable.

Then the taylor series expansion of the function around X = 0 (where X is the main variable) is computed to a number of terms depending on the number of terms of the argument and the function being computed.

;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;If the argument is a vector or a matrix, the result is componentwise evaluation of the function. In particular, transcendental functions on square matrices, which are not implemented in the present version 1.35 (see Appendix C however), will have a slightly different name if they are implemented some day.

by 1=0 <#3731#>=0<#3733#><#3733#><#3731#>

<#3732#><#3734#>.. #tex2html_wrap_inline17905#<#3734#><#3732#>#math219#x#tex2html_wrap_inline17907#y: if y is not of type integer, this has the same effect as #math220#exp(y*ln(x)). It can be applied to p-adic numbers as well as to the more usual types.<#3740#><#3740#>

The library syntax is #math221##tex2html_wrap_inline17912#(x, y, prec).

<#3745#><#3745#> by 1=0 <#3746#>=0<#3749#><#3749#><#3746#>

<#3747#><#3750#>.. abs<#3750#><#3747#>(x): absolute value of x. Polynomials, power series and rational functions are not allowed.

The library syntax is #math222##tex2html_wrap_inline17916#(x, prec) where the second argument prec is necessary only in the complex or imaginary quadratic case.

<#3757#><#3757#> by 1=0 <#3758#>=0<#3761#><#3761#><#3758#>

<#3759#><#3762#>.. acos<#3762#><#3759#>(x): principal branch of #math223#cos-1(x), i.e. such that #math224#Re(acos(x))∈[0, π]. If #math225#x#tex2html_wrap_inline17924# and | x| ;SPMgt; 1 then #math226#acos(x) is complex.

The library syntax is #math227##tex2html_wrap_inline17928#(x, prec).

<#3773#><#3773#> by 1=0 <#3774#>=0<#3777#><#3777#><#3774#>

<#3775#><#3778#>.. acosh<#3778#><#3775#>(x): principal branch of #math228#cosh-1(x), i.e. such that #math229#Im(acosh(x))∈[0, π]. If #math230#x#tex2html_wrap_inline17935# and x ;SPMlt; 1 then #math231#acosh(x) is complex.

The library syntax is #math232##tex2html_wrap_inline17939#(x, prec).

<#3789#><#3789#> by 1=0 <#3790#>=0<#3793#><#3793#><#3790#>

<#3791#><#3794#>.. agm<#3794#><#3791#>(x, y): arithmetic-geometric mean of x and y. In the case of complex or negative numbers, the principal square root is always chosen. p-adic or power series arguments are also allowed. Note that a p-adic agm exists only if x/y is congruent to 1 modulo p (modulo 16 for p = 2). x and y cannot both be vectors or matrices.

The library syntax is #math233##tex2html_wrap_inline17951#(x, y, prec).

<#3801#><#3801#> by 1=0 <#3802#>=0<#3805#><#3805#><#3802#>

<#3803#><#3806#>.. asin<#3806#><#3803#>(x): principal branch of #math234#sin-1(x), i.e. such that #math235#Re(asin(x))∈[- π/2, π/2]. If #math236#x#tex2html_wrap_inline17958# and | x| ;SPMgt; 1 then #math237#asin(x) is complex.

The library syntax is #math238##tex2html_wrap_inline17962#(x, prec).

<#3817#><#3817#> by 1=0 <#3818#>=0<#3821#><#3821#><#3818#>

<#3819#><#3822#>.. asinh<#3822#><#3819#>(x): principal branch of #math239#sinh-1(x), i.e. such that #math240#Im(asinh)(x)∈[- π/2, π/2].

The library syntax is #math241##tex2html_wrap_inline17969#(x, prec).

<#3831#><#3831#> by 1=0 <#3832#>=0<#3835#><#3835#><#3832#>

<#3833#><#3836#>.. atan<#3836#><#3833#>(x): principal branch of #math242#tan-1(x), i.e. such that #math243#Re(atan(x))∈] - π/2, π/2[.

The library syntax is #math244##tex2html_wrap_inline17976#(x, prec).

<#3845#><#3845#> by 1=0 <#3846#>=0<#3849#><#3849#><#3846#>

<#3847#><#3850#>.. atanh<#3850#><#3847#>(x): principal branch of #math245#tanh-1(x). i.e. such that #math246#Im(atanh(x))∈] - π/2, π/2]. If #math247#x#tex2html_wrap_inline17983# and | x| ;SPMgt; 1 then #math248#atanh(x) is complex.

The library syntax is #math249##tex2html_wrap_inline17987#(x, prec).

<#3861#><#3861#> by 1=0 <#3862#>=0<#3865#><#3865#><#3862#>

<#3863#><#3866#>.. bernreal<#3866#><#3863#>(x): Bernoulli number Bx, where B0 = 1, B1 = - 1/2, B2 = 1/6,..., expressed as a real number with the current precision.

The library syntax is #math250##tex2html_wrap_inline17994#(x, prec).

<#3873#><#3873#> by 1=0 <#3874#>=0<#3877#><#3877#><#3874#>

<#3875#><#3878#>.. bernvec<#3878#><#3875#>(x): creates a vector containing, as rational numbers, the Bernoulli numbers B0, B2,..., B2x. These Bernoulli numbers can then be used as follows. Assume that this vector has been put into a variable, say b. Then we define under GP:

<#529#>bern(x)=if(x==1,-1/2,if((x;SPMlt;0)||(x2),0,b[x/2+1]))<#529#>

and then <#530#>bern(k)<#530#> gives the Bernoulli number of index k as a rational number, exactly as <#531#>bernreal(k)<#531#> gives it as a real number.

The library syntax is #math251##tex2html_wrap_inline18001#(x).

<#3885#><#3885#> by 1=0 <#3886#>=0<#3889#><#3889#><#3886#>

<#3887#><#3890#>.. cos<#3890#><#3887#>(x): cosine of x.

The library syntax is #math252##tex2html_wrap_inline18005#(x, prec).

<#3897#><#3897#> by 1=0 <#3898#>=0<#3901#><#3901#><#3898#>

<#3899#><#3902#>.. cosh<#3902#><#3899#>(x): hyperbolic cosine of x.

The library syntax is #math253##tex2html_wrap_inline18009#(x, prec).

<#3909#><#3909#> by 1=0 <#3910#>=0<#3913#><#3913#><#3910#>

<#3911#><#3914#>.. dilog<#3914#><#3911#>(x): principal branch of the dilogarithm of x, i.e. analytic continuation of the power series #math254#log2(x) = #tex2html_wrap_inline18013#xn/n2.

The library syntax is #math255##tex2html_wrap_inline18015#(x, prec).

<#3921#><#3921#> by 1=0 <#3922#>=0<#3925#><#3925#><#3922#>

<#3923#><#3926#>.. eint1<#3926#><#3923#>(x): exponential integral #math256##tex2html_wrap_inline18018##tex2html_wrap_inline18019# dt.

The library syntax is #math257##tex2html_wrap_inline18021#(x, prec).

<#3937#><#3937#> by 1=0 <#3938#>=0<#3941#><#3941#><#3938#>

<#3939#><#3942#>.. erfc<#3942#><#3939#>(x): complementary error function #math258#(2/#tex2html_wrap_inline18024#)#tex2html_wrap_inline18025#e-t2 dt.

The library syntax is #math259##tex2html_wrap_inline18027#(x, prec).

<#3949#><#3949#> by 1=0 <#3950#>=0<#3953#><#3953#><#3950#>

<#3951#><#3954#>.. eta<#3954#><#3951#>(x): Dedekind's η function, without the q1/24. The meaning is this. If x is a complex number with positive imaginary part, the result is #math260##tex2html_wrap_inline18033#(1 - qn), where #math261#q = e2iπx. If x is a power series (or can be converted to a power series) with positive valuation, the result is #math262##tex2html_wrap_inline18037#(1 - xn).

The library syntax is #math263##tex2html_wrap_inline18039#(x, prec).

<#3961#><#3961#> by 1=0 <#3962#>=0<#3965#><#3965#><#3962#>

<#3963#><#3966#>.. euler<#3966#><#3963#>: Euler's constant #math264#0.57721 ... .

Note that <#554#>euler<#554#> is one of the few special reserved names which cannot be used for variables.

The library syntax is #math265##tex2html_wrap_inline18042#(prec) where prec <#556#>must<#556#> be given. Note that this creates γ on the PARI stack. If one does not want to create it on that stack but be able to use it later under the global name <#1721#><#3973#><#3973#>geuler<#1721#> (with no parentheses) use instead #math266##tex2html_wrap_inline18046#(prec).

<#3978#><#3978#> by 1=0 <#3979#>=0<#3982#><#3982#><#3979#>

<#3980#><#3983#>.. exp<#3983#><#3980#>(x): exponential of x. p-adic arguments with positive valuation are accepted.

The library syntax is #math267##tex2html_wrap_inline18051#(x, prec).

<#3990#><#3990#> by 1=0 <#3991#>=0<#3994#><#3994#><#3991#>

<#3992#><#3995#>.. gamh<#3995#><#3992#>(x): gamma function evaluated at the argument x + 1/2. When x is an integer, this is much faster than using #math268#gamma(x + 1/2).

The library syntax is #math269##tex2html_wrap_inline18057#(x, prec).

<#4003#><#4003#> by 1=0 <#4004#>=0<#4007#><#4007#><#4004#>

<#4005#><#4008#>.. gamma<#4008#><#4005#>(x): gamma function of x.

The library syntax is #math270##tex2html_wrap_inline18061#(x, prec).

<#4015#><#4015#> by 1=0 <#4016#>=0<#4019#><#4019#><#4016#>

<#4017#><#4020#>.. hyperu<#4020#><#4017#>(a, b, x): U-confluent hypergeometric function with parameters a and b.

The library syntax is #math271##tex2html_wrap_inline18066#(a, b, x, prec).

<#4027#><#4027#> by 1=0 <#4028#>=0<#4031#><#4031#><#4028#>

<#4029#><#4032#>.. incgam<#4032#><#4029#>(x, y): incomplete gamma function.

The arguments x and y must be positive. The result returned is #math272##tex2html_wrap_inline18071#e-ttx-1 dt.

The library syntax is #math273##tex2html_wrap_inline18073#(x, y, prec).

Note: in addition, there exist also the functions <#1722#><#4039#><#4039#>incgam1, <#4041#><#4041#>incgam2 <#574#>and<#574#> <#4043#><#4043#>incgam3<#1722#>, which are used for internal purposes. The only really useful one of the three in addition to the standard <#576#>incgam<#576#>, is the function <#577#>incgam3<#577#> which computes #math274##tex2html_wrap_inline18075#e-ttx-1 dt. when y is not too large.

<#4045#><#4045#> by 1=0 <#4046#>=0<#4049#><#4049#><#4046#>

<#4047#><#4050#>.. jbesselh<#4050#><#4047#>(n, x): J-bessel function of half integral index. More precisely, <#581#>jbesselh(n,x)<#581#> computes #math275#Jn+1/2(x) where n must be of type integer, and x is any element of #tex2html_wrap_inline18083#. In the present version 1.35, this function is not very accurate when x is small. This will be improved soon.

The library syntax is #math276##tex2html_wrap_inline18086#(n, x, prec).

<#4058#><#4058#> by 1=0 <#4059#>=0<#4062#><#4062#><#4059#>

<#4060#><#4063#>.. jell<#4063#><#4060#>(x): elliptic j-invariant. x must be a complex number with positive imaginary part, or convertible into a power series or a p-adic number with positive valuation.

The library syntax is #math277##tex2html_wrap_inline18092#(x, prec).

<#4070#><#4070#> by 1=0 <#4071#>=0<#4074#><#4074#><#4071#>

<#4072#><#4075#>.. kbessel<#4075#><#4072#>(nu, x): K-bessel function of index nu (which can be complex) and argument x. Only real and positive arguments x are allowed in the present version 1.35.

The library syntax is #math278##tex2html_wrap_inline18098#(nu, x, prec).

Note: in addition, another implementation of this function which is often faster than kbessel is the function <#1723#><#4082#><#4082#>kbessel2<#1723#>.

<#4084#><#4084#> by 1=0 <#4085#>=0<#4088#><#4088#><#4085#>

<#4086#><#4089#>.. ln<#4089#><#4086#>(x) or <#590#>log<#590#>(x):<#4093#><#4093#> pricipal branch of the natural logarithm of x, i.e. such that #math279#Im(ln(x))∈] - π, π]. The result is complex (with imaginary part equal to π) if #math280#x#tex2html_wrap_inline18105# and x ;SPMlt; 0.

p-adic arguments are also accepted for x, with the convention that ln(p) = 0. Hence in particular #math281#exp(ln(x))/x will not in general be equal to 1 but to a p - 1-th root of unity (or ±1 if p = 2) times a power of p.

The library syntax is #math282##tex2html_wrap_inline18116#(x, prec).

<#4100#><#4100#> by 1=0 <#4101#>=0<#4104#><#4104#><#4101#>

<#4102#><#4105#>.. lngamma<#4105#><#4102#>(x): principal branch of the logarithm of the gamma function of x. Can have much larger arguments than <#595#>gamma<#595#> itself.

The library syntax is #math283##tex2html_wrap_inline18120#(x, prec).

<#4112#><#4112#> by 1=0 <#4113#>=0<#4116#><#4116#><#4113#>

<#4114#><#4117#>.. logagm<#4117#><#4114#>(x): principal branch of the natural logarithm of x, computed using an agm formula suggested by Mestre, when x is real, otherwise identical to <#598#>log<#598#>.

The library syntax is #math284##tex2html_wrap_inline18125#(x, prec).

<#4124#><#4124#> by 1=0 <#4125#>=0<#4128#><#4128#><#4125#>

<#4126#><#4129#>.. pi<#4129#><#4126#>: the constant pi (#math285#3.14159 ... ).

The library syntax is #math286##tex2html_wrap_inline18128#(prec) where prec <#602#>must<#602#> be given. Note that this creates π on the PARI stack. If one does not want to create it on that stack but be able to use it later under the global name <#1724#><#4136#><#4136#>gpi<#1724#> (with no parentheses) use instead #math287##tex2html_wrap_inline18132#(prec).

<#4141#><#4141#> by 1=0 <#4142#>=0<#4145#><#4145#><#4142#>

<#4143#><#4146#>.. polylog<#4146#><#4143#>(m, x): #math288#mth polylogarithm of x, i.e. analytic continuation of the power series #math289#logm(x) = #tex2html_wrap_inline18137#xn/nm.

The library syntax is #math290##tex2html_wrap_inline18139#(m, x, prec).

<#4154#><#4154#> by 1=0 <#4155#>=0<#4158#><#4158#><#4155#>

<#4156#><#4159#>.. polylogd<#4159#><#4156#>(m, x): modified #math291#mth polylogarithm of x, called Dm(x) in Zagier, defined for | x|≤1 by

#math292#

m#tex2html_wrap_indisplay18146##tex2html_wrap_indisplay18147##tex2html_wrap_indisplay18148#logm-k(x) - #tex2html_wrap_indisplay18149##tex2html_wrap_indisplay18150##tex2html_wrap_indisplay18151#

and such that #math293#Dm(m, 1/x) = (- 1)m-1Dm(m, x), where ℜm denotes ℜ or ℑ depending whether m is odd or even.

The library syntax is #math294##tex2html_wrap_inline18158#(m, x, prec).

<#4176#><#4176#> by 1=0 <#4177#>=0<#4180#><#4180#><#4177#>

<#4178#><#4181#>.. polylogp<#4181#><#4178#>(m, x): another modified #math295#mth polylogarithm of x, called Pm(x) in Zagier, defined for | x|≤1 by

#math296#

m#tex2html_wrap_indisplay18165##tex2html_wrap_indisplay18166##tex2html_wrap_indisplay18167#(log(| x|))klogm-k(x) - #tex2html_wrap_indisplay18168#(log(| x|))m#tex2html_wrap_indisplay18169#

and such that #math297#Pm(m, 1/x) = (- 1)m-1Pm(m, x), where ℜm denotes ℜ or ℑ depending whether m is odd or even.

The library syntax is #math298##tex2html_wrap_inline18176#(m, x, prec).

<#4196#><#4196#> by 1=0 <#4197#>=0<#4200#><#4200#><#4197#>

<#4198#><#4201#>.. psi<#4201#><#4198#>(x): the ψ-function of x, i.e. the logarithmic derivative #math299#Γ'(x)/Γ(x).

The library syntax is #math300##tex2html_wrap_inline18182#(x, prec).

<#4208#><#4208#> by 1=0 <#4209#>=0<#4212#><#4212#><#4209#>

<#4210#><#4213#>.. sin<#4213#><#4210#>(x): sine of x.

The library syntax is #math301##tex2html_wrap_inline18186#(x, prec).

<#4220#><#4220#> by 1=0 <#4221#>=0<#4224#><#4224#><#4221#>

<#4222#><#4225#>.. sinh<#4225#><#4222#>(x): hyperbolic sine of x.

The library syntax is #math302##tex2html_wrap_inline18190#(x, prec).

<#4232#><#4232#> by 1=0 <#4233#>=0<#4236#><#4236#><#4233#>

<#4234#><#4237#>.. sqr<#4237#><#4234#>(x): square of x. Not identical to x*x in the case of 2-adics (see above), and acts componentwise on vectors and matrices. In particular, if x is a square matrix, sqr(x) is not the square of x (use instead x*x or #math303#x#tex2html_wrap_inline18200#2).

The library syntax is #math304##tex2html_wrap_inline18202#(x).

<#4244#><#4244#> by 1=0 <#4245#>=0<#4248#><#4248#><#4245#>

<#4246#><#4249#>.. sqrt<#4249#><#4246#>(x): principal branch of the square root of x, i.e. such that #math305#Arg(sqrt(x))∈] - π/2, π/2], or in other words such that #math306#Re(sqrt(x)) ;SPMgt; 0 or #math307#Re(sqrt(x)) = 0 and #math308#Im(sqrt(x)) ;SPMgt; = 0. Integermod a prime and p-adics are allowed as arguments. If #math309#x#tex2html_wrap_inline18211# and x ;SPMlt; 0 the result is complex with positive imaginary part.

The library syntax is #math310##tex2html_wrap_inline18214#(x, prec).

<#4261#><#4261#> by 1=0 <#4262#>=0<#4265#><#4265#><#4262#>

<#4263#><#4266#>.. tan<#4266#><#4263#>(x): tangent of x.

The library syntax is #math311##tex2html_wrap_inline18218#(x, prec).

<#4273#><#4273#> by 1=0 <#4274#>=0<#4277#><#4277#><#4274#>

<#4275#><#4278#>.. tanh<#4278#><#4275#>(x): hyperbolic tangent of x.

The library syntax is #math312##tex2html_wrap_inline18222#(x, prec).

<#4285#><#4285#> by 1=0 <#4286#>=0<#4289#><#4289#><#4286#>

<#4287#><#4290#>.. teich<#4290#><#4287#>(x): teichmuller character of the p-adic number x.

The library syntax is #math313##tex2html_wrap_inline18227#(x).

<#4297#><#4297#> by 1=0 <#4298#>=0<#4301#><#4301#><#4298#>

<#4299#><#4302#>.. wf<#4302#><#4299#>(x): Weber's f function, i.e. such that #math314#j = (f24 -16)3/f24.

The library syntax is #math315##tex2html_wrap_inline18232#(x, prec).

<#4309#><#4309#> by 1=0 <#4310#>=0<#4313#><#4313#><#4310#>

<#4311#><#4314#>.. wf2<#4314#><#4311#>(x): Weber's f2 function, i.e. such that #math316#j = (f224 +16)3/f224.

The library syntax is #math317##tex2html_wrap_inline18237#(x, prec).

<#4321#><#4321#> by 1=0 <#4322#>=0<#4325#><#4325#><#4322#>

<#4323#><#4326#>.. zeta<#4326#><#4323#>(s): Riemann's zeta function #math318#ζ(s) = #tex2html_wrap_inline18240#n-s, computed using the Euler-Maclaurin summation formula.

The library syntax is #math319##tex2html_wrap_inline18242#(s, prec).

by1=0 <#4333#>=0<#4335#><#4335#><#4333#>



<#4334#><#4336#>. Arithmetic functions.<#4336#><#4334#>


These functions are by definition functions whose natural domain of definition is either #tex2html_wrap_inline18244# (or #tex2html_wrap_inline18246#), or sometimes polynomials over a base ring. Functions which concern polynomials exclusively will be explained in the next section. The way these functions are used is completely different from transcendental functions: in general only the types integer and polynomial are accepted as arguments.

In the present version 1.35 a primitive but useful version of the <#4341#><#4341#>ECM method has been implemented. Also, numbers found to be pseudo-primes after 10 successful trials of the <#4343#><#4343#>Rabin-Miller test are declared primes.

<#4345#><#4345#> by 1=0 <#4346#>=0<#4349#><#4349#><#4346#>

<#4347#><#4350#>.. bezout<#4350#><#4347#>(x, y): finds u and v minimal in a natural sense such that #math320#x*u + y*v = gcd(x, y). The arguments must be both integers or both polynomials, and the result is a row vector with three components u, v, and #math321#gcd(x, y).

The library syntax is either #math322##tex2html_wrap_inline18255#(x, y) to get the vector, or #math323##tex2html_wrap_inline18257#(x, y,u,v) which gives as result the address of the created gcd, and puts in u and v the addresses of the corresponding created objects.

<#4362#><#4362#> by 1=0 <#4363#>=0<#4366#><#4366#><#4363#>

<#4364#><#4367#>.. bigomega<#4367#><#4364#>(x): number of prime divisors of x counted with multiplicity. x must be an integer, and the result is a 32-bit C-integer.

The library syntax is #math324##tex2html_wrap_inline18264#(x).

<#4374#><#4374#> by 1=0 <#4375#>=0<#4378#><#4378#><#4375#>

<#4376#><#4379#>.. bin<#4379#><#4376#>(x, y): <#4383#><#4383#>binomial coefficient #math325##tex2html_wrap_inline18267#x#tex2html_wrap_inline18268#y. Here y must be an integer, but x can be any PARI object.

The library syntax is #math326##tex2html_wrap_inline18272#(x, y).

<#4391#><#4391#> by 1=0 <#4392#>=0<#4395#><#4395#><#4392#>

<#4393#><#4396#>.. boundcf<#4396#><#4393#>(x, lmax): creates the row vector whose components are the partial quotients of the <#4400#><#4400#>continued fraction expansion of x, the number of partial quotients being limited to lmax. If x is a real number, the expansion stops at the last significant partial quotient or after lmax terms, whichever is smallest. x can also be a rational function or a power series.

The library syntax is #math327##tex2html_wrap_inline18278#(x, lmax), where lmax is a C integer.

<#4405#><#4405#> by 1=0 <#4406#>=0<#4409#><#4409#><#4406#>

<#4407#><#4410#>.. boundfact<#4410#><#4407#>(x, p): For integer x, finds only the prime factors up to p or to the default bound of the prime table created upon initialization of GP, whichever is lowest (except when p ;SPMlt; = 1 where the effect is identical to <#683#>smallfact<#683#>. The remaining part is hence not necessarily prime.

The library syntax is #math328##tex2html_wrap_inline18285#(x, p).

<#4417#><#4417#> by 1=0 <#4418#>=0<#4421#><#4421#><#4418#>

<#4419#><#4422#>.. cf<#4422#><#4419#>(x): creates the row vector whose components are the partial quotients of the <#4426#><#4426#>continued fraction expansion of x. If x is a real number, the expansion stops at the last significant partial quotient. x can also be a rational function.

The library syntax is #math329##tex2html_wrap_inline18291#(x).

<#4431#><#4431#> by 1=0 <#4432#>=0<#4435#><#4435#><#4432#>

<#4433#><#4436#>.. cf2<#4436#><#4433#>(b, x): creates the row vector whose components are the partial quotients of the continued fraction expansion of x, the numerators being equal to the coefficients of the vector b. The length of the result is equal to the length of b unless a partial remainder is encountered which is equal to zero, in which case the expansion stops. In the case of real numbers, the stopping criterion in thus different from that of <#689#>cf<#689#> since if b is too long, some partial quotients may not be significant. x can also be a rational function or a power series.

The library syntax is #math330##tex2html_wrap_inline18299#(b, x).

<#4443#><#4443#> by 1=0 <#4444#>=0<#4447#><#4447#><#4444#>

<#4445#><#4448#>.. chinese<#4448#><#4445#>(x, y): x and y being both integermods or polymods, creates (with the same type) a z in the same residue class as x and in the same residue class as y, if it is possible.

The library syntax is #math331##tex2html_wrap_inline18307#(x, y).

<#4455#><#4455#> by 1=0 <#4456#>=0<#4459#><#4459#><#4456#>

<#4457#><#4460#>.. classno<#4460#><#4457#>(x): class number of the quadratic field of discriminant x. In the present version 1.35, a simple algorithm is used for x ;SPMgt; 0, so x should not be too large (say x ;SPMlt; 107) for the time to be reasonable. On the other hand, for x ;SPMlt; 0 one can reasonably compute classno(x) for #math332#| x| ;SPMlt; 1025, since the method used is Shanks' method which is in #math333#O(| x|1/4).

The library syntax is #math334##tex2html_wrap_inline18318#(x).

There also exists the function <#1726#><#4467#><#4467#>classno2<#1726#>, which computes the class number using the functional equation. However, it is in #math335#O(| x|1/2). Finally, in library mode only there exists the function <#1727#><#4469#><#4469#>classno3<#1727#> which computes the class number of an imaginary quadratic field by counting reduced forms, a O(| x|) algorithm. See also 3.4.14 below.

<#4471#><#4471#> by 1=0 <#4472#>=0<#4475#><#4475#><#4472#>

<#4473#><#4476#>.. content<#4476#><#4473#>(x): computes the gcd of all the coefficients of x, when this gcd makes sense. If x is a scalar, this simply gives x. If x is a polynomial (and by extension a power series), it gives the usual content of x. If x is a rational function, it gives the ratio of the contents of the numerator and the denominator. Finally, if x is a vector or a matrix, it gives the gcd of all the entries.

The library syntax is #math336##tex2html_wrap_inline18330#(x).

<#4483#><#4483#> by 1=0 <#4484#>=0<#4487#><#4487#><#4484#>

<#4485#><#4488#>.. divisors<#4488#><#4485#>(x): creates a row vector whose components are the positive divisors of the integer x in increasing order.

The library syntax is #math337##tex2html_wrap_inline18334#(x).

<#4495#><#4495#> by 1=0 <#4496#>=0<#4499#><#4499#><#4496#>

<#4497#><#4500#>.. fact<#4500#><#4497#>(x) or x!: factorial of x.

The library syntax is #math338##tex2html_wrap_inline18339#(x). x must be a 32-bit C-integer and not a PARI integer.

<#4507#><#4507#> by 1=0 <#4508#>=0<#4511#><#4511#><#4508#>

<#4509#><#4512#>.. factfq<#4512#><#4509#>(x, p, a): factorization in the field #tex2html_wrap_inline18345# defined by the irreducible polynomial a over #tex2html_wrap_inline18350# of the polynomial x. The coefficients of x must be operation-compatible with #math339##tex2html_wrap_inline18354#/p#tex2html_wrap_inline18355#. The result is a two column matrix, the first column being the irreducible polynomials dividing x, and the second the exponents. It is advised to use for the variable of a (which will be used as variable of a polymod), a name distinct from the other variables used, so that a <#707#>lift()<#707#> of the result will be legible.

The library syntax is #math340##tex2html_wrap_inline18359#(x, p, a).

<#4523#><#4523#> by 1=0 <#4524#>=0<#4527#><#4527#><#4524#>

<#4525#><#4528#>.. factmod<#4528#><#4525#>(x, p): factorization mod p of the polynomial x. The coefficients of x must be operation-compatible with #math341##tex2html_wrap_inline18365#/p#tex2html_wrap_inline18366#. The result is a two column matrix, the first column being the irreducible polynomials dividing x, and the second the exponents.

The library syntax is #math342##tex2html_wrap_inline18369#(x, p).

<#4537#><#4537#> by 1=0 <#4538#>=0<#4541#><#4541#><#4538#>

<#4539#><#4542#>.. factor<#4542#><#4539#>(x): general factorization function. If x is of type integer, rational, polynomial or rational function, the result is a two column matrix, the first column being the irreducibles dividing x (prime numbers or polynomials), and the second the exponents. If x is a vector or a matrix, the factoring is done componentwise (hence the result is a vector or matrix of two column matrices).

The polynomials or rational functions to be factored must have coefficients either integers or integermods. Note that PARI does <#712#>not<#712#> know how to factor multivariate polynomials.

The library syntax is #math343##tex2html_wrap_inline18375#(x). See also <#1728#><#4549#><#4549#>factpol<#1728#> and <#1729#><#4551#><#4551#>factpol2<#1729#> (see 3.6.10).

<#4553#><#4553#> by 1=0 <#4554#>=0<#4557#><#4557#><#4554#>

<#4555#><#4558#>.. fibo<#4558#><#4555#>(x): #math344#xth fibonacci number.

The library syntax is #math345##tex2html_wrap_inline18379#(x). x must be a 32-bit C-integer and not a PARI integer.

<#4566#><#4566#> by 1=0 <#4567#>=0<#4570#><#4570#><#4567#>

<#4568#><#4571#>.. gcd<#4571#><#4568#>(x, y): creates the greatest common divisor of x and y. x and y can be of quite general types; for instance both rational numbers. Experiment will tell you what the result in these cases will be.

The library syntax is #math346##tex2html_wrap_inline18387#(x, y).

<#4578#><#4578#> by 1=0 <#4579#>=0<#4582#><#4582#><#4579#>

<#4580#><#4583#>.. hclassno<#4583#><#4580#>(x): <#4587#><#4587#>Hurwitz class number of x, where x is nonnegative and congruent to 0 or 3 modulo 4. See also 3.4.6 above.

The library syntax is #math347##tex2html_wrap_inline18392#(x).

<#4592#><#4592#> by 1=0 <#4593#>=0<#4596#><#4596#><#4593#>

<#4594#><#4597#>.. hilb<#4597#><#4594#>#math348#(x, y, p) or <#725#>hilbp<#725#>(x, y): <#4601#><#4601#>Hilbert symbol of x and y. If x and y are of type integer or fraction, the function <#727#>hilb<#727#> must be used with an explicit third parameter p, p = 0 meaning the place at infinity. Otherwise, p need not be given, and x and y can be of compatible types integer, fraction, real, integermod or p-adic.

The library syntax is in both cases #math349##tex2html_wrap_inline18406#(x, y, p) or #math350##tex2html_wrap_inline18408#(x, y).

<#4609#><#4609#> by 1=0 <#4610#>=0<#4613#><#4613#><#4610#>

<#4611#><#4614#>.. isfund<#4614#><#4611#>(x): true (1) if x is equal to 1 or to the discriminant of a quadratic field, false (0) otherwise.

The library syntax is #math351##tex2html_wrap_inline18412#(x).

<#4621#><#4621#> by 1=0 <#4622#>=0<#4625#><#4625#><#4622#>

<#4623#><#4626#>.. isprime<#4626#><#4623#>(x): true (1) if x is a strong pseudo-prime for 10 randomly chosen bases, false (0) otherwise.

The library syntax is #math352##tex2html_wrap_inline18416#(x).

<#4633#><#4633#> by 1=0 <#4634#>=0<#4637#><#4637#><#4634#>

<#4635#><#4638#>.. ispsp<#4638#><#4635#>(x): true (1) if x is a strong pseudo-prime for a randomly chosen base, false (0) otherwise.

The library syntax is #math353##tex2html_wrap_inline18420#(x).

<#4645#><#4645#> by 1=0 <#4646#>=0<#4649#><#4649#><#4646#>

<#4647#><#4650#>.. isqrt<#4650#><#4647#>(x): integer square root of x, which must be of PARI type integer. A negative x is allowed, and the result in that case is <#737#>i*isqrt(-x)<#737#>.

The library syntax is #math354##tex2html_wrap_inline18425#(x).

<#4657#><#4657#> by 1=0 <#4658#>=0<#4661#><#4661#><#4658#>

<#4659#><#4662#>.. issqfree<#4662#><#4659#>(x): true (1) if x is squarefree, false if not. Here x can be an integer or a polynomial.

The library syntax is #math355##tex2html_wrap_inline18430#(x).

<#4669#><#4669#> by 1=0 <#4670#>=0<#4673#><#4673#><#4670#>

<#4671#><#4674#>.. issquare<#4674#><#4671#>(x): true (1) if x is square, false if not.

The library syntax is #math356##tex2html_wrap_inline18434#(x).

<#4681#><#4681#> by 1=0 <#4682#>=0<#4685#><#4685#><#4682#>

<#4683#><#4686#>.. kronecker<#4686#><#4683#>(x, y) or <#744#>kro<#744#>(x, y): Kronecker <#4690#><#4690#> (i.e. generalized Legendre) symbol #math357##tex2html_wrap_inline18438##tex2html_wrap_inline18439##tex2html_wrap_inline18440#. x and y must be of type integer and the result (0 or ±1) is a 32-bit C-integer.

The library syntax is #math358##tex2html_wrap_inline18446#(x, y).

<#4698#><#4698#> by 1=0 <#4699#>=0<#4702#><#4702#><#4699#>

<#4700#><#4703#>.. lcm<#4703#><#4700#>(x, y): least common multiple of x and y, i.e. such that #math359#lcm(x, y)*gcd(x, y) = x*y.

The library syntax is #math360##tex2html_wrap_inline18452#(x, y).

<#4712#><#4712#> by 1=0 <#4713#>=0<#4716#><#4716#><#4713#>

<#4714#><#4717#>.. mu<#4717#><#4714#>(x): Möbius μ-function of x. x must be of type integer and the result (0 or ±1) is a 32-bit C-integer.

The library syntax is #math361##tex2html_wrap_inline18460#(x).

<#4724#><#4724#> by 1=0 <#4725#>=0<#4728#><#4728#><#4725#>

<#4726#><#4729#>.. nextprime<#4729#><#4726#>(x): finds the smallest prime greater than or equal to x. x must be of type integer.

The library syntax is #math362##tex2html_wrap_inline18465#(x).

<#4736#><#4736#> by 1=0 <#4737#>=0<#4740#><#4740#><#4737#>

<#4738#><#4741#>.. numdiv<#4741#><#4738#>(x): number of divisors of x. x must be of type integer, and the result is a 32-bit C-integer.

The library syntax is #math363##tex2html_wrap_inline18470#(x).

<#4748#><#4748#> by 1=0 <#4749#>=0<#4752#><#4752#><#4749#>

<#4750#><#4753#>.. omega<#4753#><#4750#>(x): number of distinct prime divisors of x. x must be of type integer, and the result is a 32-bit C-integer.

The library syntax is #math364##tex2html_wrap_inline18475#(x).

<#4760#><#4760#> by 1=0 <#4761#>=0<#4764#><#4764#><#4761#>

<#4762#><#4765#>.. order<#4765#><#4762#>(x): x must be an integer mod n, and the result is the order of x in the multiplicative group #math365#(#tex2html_wrap_inline18481#/n#tex2html_wrap_inline18482#)*. Error if x is not invertible.

The library syntax is #math366##tex2html_wrap_inline18485#(x).

<#4774#><#4774#> by 1=0 <#4775#>=0<#4778#><#4778#><#4775#>

<#4776#><#4779#>.. pf<#4779#><#4776#>(x, p): prime binary quadratic form of discriminant x whose first coefficient is the prime number p. Error if x is not a quadratic residue mod p. In the case where x ;SPMgt; 0, the ``distance'' component of the form is set equal to zero to the current precision.

The library syntax is #math367##tex2html_wrap_inline18493#(x, p, prec), where the third variable prec is a C-integer, but is necessary only when x ;SPMgt; 0.

<#4786#><#4786#> by 1=0 <#4787#>=0<#4790#><#4790#><#4787#>

<#4788#><#4791#>.. phi<#4791#><#4788#>(x): Euler's φ-function of x. x must be of type integer.

The library syntax is #math368##tex2html_wrap_inline18501#(x).

<#4798#><#4798#> by 1=0 <#4799#>=0<#4802#><#4802#><#4799#>

<#4800#><#4803#>.. pnqn<#4803#><#4800#>(x): When x is a vector or a one row matrix, x is considered as the list of partial quotients #math369#[a0, a1,..., an] of a rational number, and the result is the 2 by 2 matrix #math370#[pn, pn-1;qn, qn-1] in the standard notation of continued fractions, so #math371#pn/qn = a0 +1/(a1 + ... +1/an)...). If x is a matrix with two rows #math372#[b0, b1,..., bn] and #math373#[a0, a1,..., an], this is then considered as a generalized continued fraction and we have similarly #math374#pn/qn = 1/b0(a0 + b1/(a1 + ... + bn/an)...). Note that in this case one usually has b0 = 1.

The library syntax is #math375##tex2html_wrap_inline18514#(x).

<#4810#><#4810#> by 1=0 <#4811#>=0<#4814#><#4814#><#4811#>

<#4812#><#4815#>.. prime<#4815#><#4812#>(x): the #math376#xth prime number.

The library syntax is #math377##tex2html_wrap_inline18518#(x). x must be a 32-bit C-integer.

<#4823#><#4823#> by 1=0 <#4824#>=0<#4827#><#4827#><#4824#>

<#4825#><#4828#>.. primes<#4828#><#4825#>(x): creates a row vector whose components are the first x prime numbers.

The library syntax is #math378##tex2html_wrap_inline18523#(x). x must be a 32-bit C-integer.

<#4835#><#4835#> by 1=0 <#4836#>=0<#4839#><#4839#><#4836#>

<#4837#><#4840#>.. primroot<#4840#><#4837#>(x): returns a primitive root of x, where x is a prime power.

The library syntax is #math379##tex2html_wrap_inline18529#(x).

<#4847#><#4847#> by 1=0 <#4848#>=0<#4851#><#4851#><#4848#>

<#4849#><#4852#>.. qfi<#4852#><#4849#>(a, b, c): creates the binary quadratic form #math380#ax2 + bxy + cy2 with b2 -4ac ;SPMlt; 0.

The library syntax is #math381##tex2html_wrap_inline18534#(a, b, c). <#4859#><#4859#>

<#4861#><#4861#> by 1=0 <#4862#>=0<#4865#><#4865#><#4862#>

<#4863#><#4866#>.. qfr<#4866#><#4863#>(a, b, c, d ): creates the binary quadratic form #math382#ax2 + bxy + cy2 with b2 -4ac ;SPMgt; 0 and distance function d.

The library syntax is #math383##tex2html_wrap_inline18540#(a, b, c, d ).

<#4873#><#4873#> by 1=0 <#4874#>=0<#4877#><#4877#><#4874#>

<#4875#><#4878#>.. regula<#4878#><#4875#>(x): regulator of the quadratic field of positive discriminant x. Error if x is not a discriminant (fundamental or not) or if x is a square.

The library syntax is #math384##tex2html_wrap_inline18546#(x, prec).

<#4885#><#4885#> by 1=0 <#4886#>=0<#4889#><#4889#><#4886#>

<#4887#><#4890#>.. sigma<#4890#><#4887#>(x): sum of the positive divisors of x. x must be of type integer.

The library syntax is #math385##tex2html_wrap_inline18551#(x).

<#4897#><#4897#> by 1=0 <#4898#>=0<#4901#><#4901#><#4898#>

<#4899#><#4902#>.. sigmak<#4902#><#4899#>(k, x): sum of the #math386#kth powers of the positive divisors of x. x must be of type integer, but (in library mode) k must be a 32-bit C-integer.

The library syntax is #math387##tex2html_wrap_inline18558#(k, x).

<#4910#><#4910#> by 1=0 <#4911#>=0<#4914#><#4914#><#4911#>

<#4912#><#4915#>.. smallfact<#4915#><#4912#>(x): For integer x, finds only the prime factors up to the default table created when GP was started (usually 500000). The remaining part is hence not necessarily prime. This never takes more than a few seconds, and is particularly advantageous with respect to <#791#>factor<#791#> when one needs only the small prime divisors and not the complete factorisation.

The library syntax is #math388##tex2html_wrap_inline18563#(x).

<#4922#><#4922#> by 1=0 <#4923#>=0<#4926#><#4926#><#4923#>

<#4924#><#4927#>.. unit<#4927#><#4924#>(x): <#4931#><#4931#>fundamental unit of the real quadratic field #math389##tex2html_wrap_inline18566#(#tex2html_wrap_inline18567#) where x is the positive discriminant of the field. If x is not a fundamental discriminant, this probably gives the fundamental unit of the corresponding order. x must be of type integer, and the result is a quadratic number.

The library syntax is #math390##tex2html_wrap_inline18572#(x).

by1=0 <#4937#>=0<#4939#><#4939#><#4937#>



<#4938#><#4940#>. Functions related to elliptic curves.<#4940#><#4938#>


We have implemented a number of functions which are useful for number theorists working on elliptic curves. We always use Tate's notations.

The functions assume that the curve is given by a general Weierstrass model

#math391#

y2 + a1xy + a3y = x3 + a2x2 + a4x + a6,

where a priori the ai can be of any scalar type. This curve can be considered as a five component vector <#797#>e=[a1,a2,a3,a4,a6]<#797#>, but for most functions it is useful to have at one's disposal more information. This is given either by the function <#798#>initell<#798#> (see below), which gives a 19 component vector (which we will call a long vector in this section), or by the faster function <#799#>smallinitell<#799#> which gives a 13 component vector (which we will call a medium vector), all these vectors starting in the same way. Consequently, in functions which do not use the extra information given by <#800#>initell<#800#>, the curve can be given either as a five component vector, or by one of the longer vectors computed by <#801#>initell<#801#> or <#802#>smallinitell<#802#>.

Other functions, in particular those relative to height computations (see <#803#>hell<#803#> below) require also that the curve be in minimal Weierstrass form. This is achieved by the function <#804#>globalred<#804#> below.

Points on elliptic curves are represented as two component vectors <#805#>[x,y]<#805#>, except for the point at infinity, i.e. the identity element of the group law, represented by the one-component vector <#806#>[0]<#806#>.

<#4943#><#4943#> by 1=0 <#4944#>=0<#4947#><#4947#><#4944#>

<#4945#><#4948#>.. addell<#4948#><#4945#>(e, z1, z2): sum of the points z1 and z2 on the elliptic curve corresponding to the vector e.

The library syntax is #math392##tex2html_wrap_inline18580#(e, z1, z2).

<#4955#><#4955#> by 1=0 <#4956#>=0<#4959#><#4959#><#4956#>

<#4957#><#4960#>.. anell<#4960#><#4957#>(e, n): compute the vector of the first n ak corresponding to the elliptic curve e, i.e. in principle coefficients of a newform of weight 2 assuming Taniyama-Weil. e must be a medium or long vector of the type given by <#810#>smallinitell<#810#> or <#811#>initell<#811#>.

The library syntax is #math393##tex2html_wrap_inline18587#(e, n).

<#4967#><#4967#> by 1=0 <#4968#>=0<#4971#><#4971#><#4968#>

<#4969#><#4972#>.. apell<#4972#><#4969#>(e, p): compute the ap corresponding to the elliptic curve e and the prime number p, using the baby-step giant-step method and a trick due to Mestre. No checking is done that p is indeed prime. The number of points of e over #tex2html_wrap_inline18597# is p + 1 - ap. e must be a medium or long vector of the type given by <#814#>smallinitell<#814#> or <#815#>initell<#815#>.

The library syntax is #math394##tex2html_wrap_inline18601#(e, p).

<#4980#><#4980#> by 1=0 <#4981#>=0<#4984#><#4984#><#4981#>

<#4982#><#4985#>.. apell2<#4985#><#4982#>(e, p): compute the ap corresponding to the elliptic curve e and the prime number p as a sum of legendre symbols. This is slower than <#818#>apell<#818#> as soon as p is greater than 100, say. e must be a medium or long vector of the type given by <#819#>smallinitell<#819#> or <#820#>initell<#820#>.

The library syntax is #math395##tex2html_wrap_inline18609#(e, p).

<#4992#><#4992#> by 1=0 <#4993#>=0<#4996#><#4996#><#4993#>

<#4994#><#4997#>.. chell<#4997#><#4994#>(e, v): change the data for the elliptic curve e by changing the coordinates using the vector <#823#>v=[u,r,s,t]<#823#>, i.e. if x' and y' are the new coordinates, then x = u2x' + r, #math396#y = u3y' + su2x' + t. The vector e must be a medium or long vector of the type given by <#824#>smallinitell<#824#> or <#825#>initell<#825#>.

The library syntax is #math397##tex2html_wrap_inline18618#(e, v).

<#5004#><#5004#> by 1=0 <#5005#>=0<#5008#><#5008#><#5005#>

<#5006#><#5009#>.. chptell<#5009#><#5006#>(x, v): change the coordinates of the point or vector of points x using the vector <#828#>v=[u,r,s,t]<#828#>, i.e. if x' and y' are the new coordinates, then x = u2x' + r, #math398#y = u3y' + su2x' + t (see also <#829#>chell<#829#> above).

The library syntax is #math399##tex2html_wrap_inline18626#(x, v).

<#5016#><#5016#> by 1=0 <#5017#>=0<#5020#><#5020#><#5017#>

<#5018#><#5021#>.. globalred<#5021#><#5018#>(e): calculate the arithmetic conductor and the global minimal model of e. Here e is an elliptic curve given by a medium or long vector of the type given by <#832#>smallinitell<#832#> or <#833#>initell<#833#>, and is supposed to have all its coefficients ai in #tex2html_wrap_inline18632#. The result is a 2 component vector [N, v]. N is the arithmetic conductor of the curve, v is itself a vector #math400#[u, r, s, t] with rational components. It gives a coordinate change for e over #tex2html_wrap_inline18639# such that the resulting model has integral coefficients, is everywhere minimal, a1 is 0 or 1, a2 is 0, 1 or -1 and a3 is 0 or 1. Such a model is unique, and the vector v is unique if we specify that u is positive.

The library syntax is #math401##tex2html_wrap_inline18647#(e).

<#5030#><#5030#> by 1=0 <#5031#>=0<#5034#><#5034#><#5031#>

<#5032#><#5035#>.. hell<#5035#><#5032#>(e, z): global <#5039#><#5039#>Néron-Tate height of the point z on the elliptic curve e. The vector e must be a long vector of the type given by <#837#>initell<#837#>. This computation is done using sigma and theta-functions and a trick due to J. Silverman.

The library syntax is #math402##tex2html_wrap_inline18653#(e, z, prec). The archimedean contribution alone is given by the library function #math403##tex2html_wrap_inline18655#(e, z, prec).

<#5047#><#5047#> by 1=0 <#5048#>=0<#5051#><#5051#><#5048#>

<#5049#><#5052#>.. hell2<#5052#><#5049#>(e, z): same as <#841#>hell<#841#>, except that the algorithm used is Tate's 4n algorithm, and is much slower.

The library syntax is #math404##tex2html_wrap_inline18659#(e, z, prec).

<#5059#><#5059#> by 1=0 <#5060#>=0<#5063#><#5063#><#5060#>

<#5061#><#5064#>.. initell<#5064#><#5061#>(e): compute some fixed data concerning the elliptic curve given by the five component vector e, which will be essential for most further computations on the curve. The result is a 19-component vector E (called a long vector in this section), containing the following information:

The first 13 components contain

#math405#

a1, a2, a3, a4, a6, b2, b4, b6, b8, c4, c6, Δ, j.

In particular, the discriminant is E[12], and the j-invariant is E[13].

For the other six components, their content depends on whether the curve is defined over #tex2html_wrap_inline18667# or not.

When e is defined over #tex2html_wrap_inline18670#, E[14] is a vector with three components containing the roots of the associated Weierstrass equation. If the roots are all real, then they are ordered by decreasing value. If only one is real, it is the first component of E[14].

E[15] is the real period of E (integral of #math406#dx/(2y + a1x + a3) over the connected component of the identity element of the real points of the curve), and E[16] is a complex period. In other words, #math407#ω1 = E[15] and #math408#ω2 = E[16] form a basis of the complex lattice defining e, with #math409#τ = #tex2html_wrap_inline18681# having positive imaginary part.

E[17] and E[18] are the corresponding values η1 and η2 such that #math410#η1ω2 - η2ω1 = .

Finally, E[19] is the volume of the complex lattice defining e.

When e is defined over #tex2html_wrap_inline18693#, the p-adic valuation of j must be negative. Then E[14] is the vector with a single component equal to the p-adic root of the associated Weierstrass equation corresponding to -1 under the Tate parametrization.

E[15] is equal to the square of the u-value, in the notation of Tate.

E[16] is the u-value itself, if it belongs to #tex2html_wrap_inline18706#, otherwise zero.

E[17] is the value of Tate's q for the curve e.

E[18] is the value of Mestre's w (this is technical), and E[19] is arbitrarily set equal to zero.

For all other base fields or rings, the last six components are arbitrarily set equal to zero.

The library syntax is #math411##tex2html_wrap_inline18714#(e, prec).

<#5078#><#5078#> by 1=0 <#5079#>=0<#5082#><#5082#><#5079#>

<#5080#><#5083#>.. isoncurve<#5083#><#5080#>(e, z): gives 1 (i.e. true) if the point z is on the elliptic curve e, 0 otherwise. Here e can be a five-component vector or a long vector.

The library syntax is #math412##tex2html_wrap_inline18720#(e, z), and the result is a C-integer.

<#5090#><#5090#> by 1=0 <#5091#>=0<#5094#><#5094#><#5091#>

<#5092#><#5095#>.. localred<#5095#><#5092#>(e, p): calculate the Kodaira type of the local fiber of the elliptic curve e at the prime p. e must be given by a medium or long vector of the type given by <#850#>smallinitell<#850#> or <#851#>initell<#851#>, and is assumed to have all its coefficients ai in #tex2html_wrap_inline18727#. The result is a 3 component vector #math413#[f, kod, v]. Here f is the exponent of p in the arithmetic conductor of e, kod is the Kodaira type which is coded as follows: 1 means good reduction (type I0), 2, 3 and 4 mean types II, III and IV respectively, 4 + ν with ν ;SPMgt; 0 means type Iν; finally the opposite values -1, -2, etc. refer to the starred types I0*, II*, etc. The third component v is itself a vector #math414#[u, r, s, t] giving the coordinate changes done during the local reduction. Normally, this has no use if u is 1, that is, if the given equation was already minimal.

The library syntax is #math415##tex2html_wrap_inline18742#(e, p).

<#5103#><#5103#> by 1=0 <#5104#>=0<#5107#><#5107#><#5104#>

<#5105#><#5108#>.. matell<#5108#><#5105#>(e, x): here x is a vector of points, and this function outputs the Gram matrix of x with respect to the Néron-Tate height, in other words, the <#854#>(i,j)<#854#> component of the matrix is equal to <#855#>(hell(e,x[i]+x[j])-hell(e,x[i])-hell(e,x[j]))/2<#855#>, where <#856#>x[i]+x[j]<#856#> denotes of course the sum of the points on the curve e. The rank of this matrix, at least in some approximate sense, gives the rank of the set of points.

The library syntax is #math416##tex2html_wrap_inline18748#(e, x, prec).

<#5115#><#5115#> by 1=0 <#5116#>=0<#5119#><#5119#><#5116#>

<#5117#><#5120#>.. ordell<#5120#><#5117#>(e, x): gives a 0, 1 or 2-component vector containing the y-coordinates of the points of the curve e having x as x-coordinate.

The library syntax is #math417##tex2html_wrap_inline18755#(e, x).

<#5127#><#5127#> by 1=0 <#5128#>=0<#5131#><#5131#><#5128#>

<#5129#><#5132#>.. powell<#5132#><#5129#>(e, n, z): computes n times the point z for the group law on the elliptic curve e. Here n is in #tex2html_wrap_inline18762#.

The library syntax is #math418##tex2html_wrap_inline18764#(e, n, z).

<#5140#><#5140#> by 1=0 <#5141#>=0<#5144#><#5144#><#5141#>

<#5142#><#5145#>.. smallinitell<#5145#><#5142#>(e): compute some fixed data concerning the elliptic curve given by the five component vector e, which may be useful for further computations on the curve. The result is a 13-component vector E (called a medium vector in this section), containing the first 13 components of the vector given by <#863#>initell<#863#>, in other words the vector

#math419#

[a1, a2, a3, a4, a6, b2, b4, b6, b8, c4, c6, Δ, j].

In particular, the discriminant is E[12], and the j-invariant is E[13].

The library syntax is #math420##tex2html_wrap_inline18772#(e).

<#5152#><#5152#> by 1=0 <#5153#>=0<#5156#><#5156#><#5153#>

<#5154#><#5157#>.. subell<#5157#><#5154#>(e, z1, z2): difference of the points z1 and z2 on the elliptic curve corresponding to the vector e.

The library syntax is #math421##tex2html_wrap_inline18778#(e, z1, z2).

<#5164#><#5164#> by 1=0 <#5165#>=0<#5168#><#5168#><#5165#>

<#5166#><#5169#>.. zell<#5169#><#5166#>(e, z): If e is an elliptic curve with coefficients in #tex2html_wrap_inline18782#, this computes a complex number t (modulo the lattice defining e) corresponding to the point z, i.e. such that, in the standard Weierstrass model, #math422#℘(t) = z[1],℘'(t) = z[2]. If e has coefficients in #tex2html_wrap_inline18791#, then either Tate's u is in #tex2html_wrap_inline18796#, in which case the output is a p-adic number t corresponding to the point z under the Tate parametrization, or only its square is, in which case the output is t + 1/t. e must be a long vector of the type given by <#868#>initell<#868#>.

The library syntax is #math423##tex2html_wrap_inline18803#(e, z, prec).

by1=0 <#5179#>=0<#5181#><#5181#><#5179#>



<#5180#><#5182#>. Polynomial and power series functions.<#5182#><#5180#>


We group here all functions which are specific to polynomials or power series, including functions related to number fields. Many other functions which can be applied on these objects are described in the other sections. Also, some of the functions described here can be applied to other types.

<#5185#><#5185#> by 1=0 <#5186#>=0<#5189#><#5189#><#5186#>

<#5187#><#5190#>.. apprpadic<#5190#><#5187#>(x, a): vector of p-adic roots of the polynomial x congruent to the p-adic number a modulo p (or modulo 4 if p = 2), and with the same p-adic precision as a. The number a can be an ordinary p-adic number (type 7, i.e. an element of #tex2html_wrap_inline18818#) or can be an element of a finite extension of #tex2html_wrap_inline18822#, in which case it is of type 9 (polymod), where at least one of the coefficients of the polymod is a p-adic number. In this case, the result is the vector of roots belonging to the same extension of #tex2html_wrap_inline18827# as a.

The library syntax is #math424##tex2html_wrap_inline18830#(x, a), but if a is known to be simply a p-adic number (type 7), the syntax #math425##tex2html_wrap_inline18834#(x, a) can be used.

<#5203#><#5203#> by 1=0 <#5204#>=0<#5207#><#5207#><#5204#>

<#5205#><#5208#>.. base<#5208#><#5205#>(x): <#5212#><#5212#>integral basis of the number field defined by the monic irreducible polynomial x, using the round 2 algorithm.

The library syntax is #math426##tex2html_wrap_inline18838#(x,d ), where d will receive the discriminant of the number field (<#877#>not<#877#> of the polynomial x). This program is the translation in C of a program written by David Ford.

<#5217#><#5217#> by 1=0 <#5218#>=0<#5221#><#5221#><#5218#>

<#5219#><#5222#>.. convol<#5222#><#5219#>(x, y): convolution (or <#5226#><#5226#>Hadamard product) of the two power series x and y; in other words if #math427#x = #tex2html_wrap_inline18845#ak*Xk and #math428#y = #tex2html_wrap_inline18847#bk*Xk then #math429#convol(x, y) = #tex2html_wrap_inline18849#ak*bk*Xk.

The library syntax is #math430##tex2html_wrap_inline18851#(x, y).

<#5232#><#5232#> by 1=0 <#5233#>=0<#5236#><#5236#><#5233#>

<#5234#><#5237#>.. deriv<#5237#><#5234#>(x, y): derivative of x with respect to the simple variable y. x can be any type except polymod. The derivative of a scalar type is zero, and the derivative of a vector or matrix is done componentwise.

The library syntax is #math431##tex2html_wrap_inline18857#(x, v), where v is the number of the variable y.

<#5244#><#5244#> by 1=0 <#5245#>=0<#5248#><#5248#><#5245#>

<#5246#><#5249#>.. disc<#5249#><#5246#>(x): discriminant of x. x must be a polynomial. The algorithm used is the <#5253#><#5253#>subresultant algorithm.

The library syntax is #math432##tex2html_wrap_inline18864#(x).

<#5258#><#5258#> by 1=0 <#5259#>=0<#5262#><#5262#><#5259#>

<#5260#><#5263#>.. discf<#5263#><#5260#>(x): <#5267#><#5267#>field discriminant of the number field defined by the monic irreducible polynomial x.

The library syntax is #math433##tex2html_wrap_inline18868#(x). See also <#890#>base<#890#>, 3.6.1 above.

<#5272#><#5272#> by 1=0 <#5273#>=0<#5276#><#5276#><#5273#>

<#5274#><#5277#>.. eval<#5277#><#5274#>(x): replace in x the formal variables by the values that have been assigned to them after the creation of x. This is mainly useful in GP, and not in library mode. Do not confuse this with substitution (see <#892#>subst<#892#>, 3.6.32 below).

The library syntax is #math434##tex2html_wrap_inline18873#(x).

<#5284#><#5284#> by 1=0 <#5285#>=0<#5288#><#5288#><#5285#>

<#5286#><#5289#>.. factoredbase<#5289#><#5286#>(x, p): <#5293#><#5293#>integral basis of the number field defined by the monic irreducible polynomial x, using the round 2 algorithm, where p is the two column matrix of the factorization of the discriminant of the polynomial x.

The library syntax is #math435##tex2html_wrap_inline18879#(x, p,d ), where d will receive the discriminant of the number field.

<#5298#><#5298#> by 1=0 <#5299#>=0<#5302#><#5302#><#5299#>

<#5300#><#5303#>.. factoreddiscf<#5303#><#5300#>(x, p): <#5307#><#5307#>field discriminant of the number field defined by the monic irreducible polynomial x, using the round 2 algorithm, where p is the two column matrix of the factorization of the discriminant of the polynomial x.

The library syntax is #math436##tex2html_wrap_inline18886#(x, p).

<#5312#><#5312#> by 1=0 <#5313#>=0<#5316#><#5316#><#5313#>

<#5314#><#5317#>.. factoredpolred<#5317#><#5314#>(x, p): same as <#901#>polred<#901#> (see 3.6.18 below) except that p is the two column matrix of the factorization of the discriminant of the polynomial x.

The library syntax is #math437##tex2html_wrap_inline18891#(x, p, prec).

<#5324#><#5324#> by 1=0 <#5325#>=0<#5328#><#5328#><#5325#>

<#5326#><#5329#>.. factorpadic<#5329#><#5326#>(x, p, r): p-adic factorization of the polynomial x to precision r, the result being a two column matrix as in <#904#>factor<#904#>. In the present version 1.35, only separable factors are given, hence the sum of the degrees of the factors may not add up to the degree of x. In any case, r must be stricly larger than some value which is at most equal to the p-adic valuation of the discriminant of x for the result to make any sense.

The library syntax is #math438##tex2html_wrap_inline18901#(x, p, r), where r is a C-integer.

<#5336#><#5336#> by 1=0 <#5337#>=0<#5340#><#5340#><#5337#>

<#5338#><#5341#>.. factpol<#5341#><#5338#>(x, l ): x must be a polynomial with coefficients in #tex2html_wrap_inline18906#. If l = 0, find the complete factorization of x, and if l ;SPMgt; 0, search only for irreducible factors of degree less than or equal to l. The result is a two column matrix, the first one containing the irreducible factors, the second one the exponents.

The library syntax is #math439##tex2html_wrap_inline18912#(x, l ). The algorithm used is the standard Hensel lifting of a mod p factorization. Another implementation using instead root finding over #tex2html_wrap_inline18915# (instead of implicitly using #tex2html_wrap_inline18919#) is #math440##tex2html_wrap_inline18921#(x, l ).

<#5354#><#5354#> by 1=0 <#5355#>=0<#5358#><#5358#><#5355#>

<#5356#><#5359#>.. integ<#5359#><#5356#>(x, y): <#5363#><#5363#>formal integration of x with respect to the simple variable y. No logarithmic terms must occur in the result. x can be of any type, but the case where x is a rational function is not implemented in the present version (1.35).

The library syntax is #math441##tex2html_wrap_inline18928#(x).

<#5368#><#5368#> by 1=0 <#5369#>=0<#5372#><#5372#><#5369#>

<#5370#><#5373#>.. laplace<#5373#><#5370#>(x): x must be a power series with only nonnegative exponents. If #math442#x = #tex2html_wrap_inline18932#(ak/k!)*Xk then the result is #math443##tex2html_wrap_inline18934#ak*Xk.

The library syntax is #math444##tex2html_wrap_inline18936#(x).

<#5380#><#5380#> by 1=0 <#5381#>=0<#5384#><#5384#><#5381#>

<#5382#><#5385#>.. legendre<#5385#><#5382#>(x): creates the #math445#xth <#5390#><#5390#>Legendre polynomial.

The library syntax is #math446##tex2html_wrap_inline18940#(x), where x is a 32-bit C-integer.

<#5395#><#5395#> by 1=0 <#5396#>=0<#5399#><#5399#><#5396#>

<#5397#><#5400#>.. newtonpoly<#5400#><#5397#>(x, p): gives the vector of the slopes of the Newton polygon of the polynomial x with respect to the prime number p. The n components of the vector are in decreasing order, and n is equal to the degree of x.

The library syntax is #math447##tex2html_wrap_inline18949#(x, p).

<#5407#><#5407#> by 1=0 <#5408#>=0<#5411#><#5411#><#5408#>

<#5409#><#5412#>.. ordred<#5412#><#5409#>(x): finds polynomials with reasonably small coefficients and of the same degree as that of x defining suborders of the order defined by x. One of the polynomials always defines #tex2html_wrap_inline18954# (hence is equal to (x - 1)n, where n is the degree), and another always defines the same order as x if x is irreducible.

The library syntax is #math448##tex2html_wrap_inline18960#(x).

<#5420#><#5420#> by 1=0 <#5421#>=0<#5424#><#5424#><#5421#>

<#5422#><#5425#>.. polint<#5425#><#5422#>(xa, ya, x): given the data vectors xa and ya of the same length n (xa containing the x-coordinates, and ya the corresponding y-coordinates), this function finds the <#5429#><#5429#>interpolating polynomial passing through these points and evaluates it at the value x.

The library syntax is #math449##tex2html_wrap_inline18971#(xa, ya, x,er), where er will contain an error estimate on the returned value.

<#5434#><#5434#> by 1=0 <#5435#>=0<#5438#><#5438#><#5435#>

<#5436#><#5439#>.. polred<#5439#><#5436#>(x): finds polynomials with reasonably small coefficients defining subfields of the number field defined by x. One of the polynomials always defines #tex2html_wrap_inline18976# (hence is equal to (x - 1), where n is the degree), and another always defines the same number field as x if x is irreducible.

The library syntax is #math450##tex2html_wrap_inline18982#(x, prec).

<#5447#><#5447#> by 1=0 <#5448#>=0<#5451#><#5451#><#5448#>

<#5449#><#5452#>.. polredreal<#5452#><#5449#>(x): similar to <#928#>polred<#928#> but only valid for totally real polynomials x, i.e. polynomials having only real roots. In that case it is often faster than polred.

The library syntax is #math451##tex2html_wrap_inline18986#(x, prec).

<#5459#><#5459#> by 1=0 <#5460#>=0<#5463#><#5463#><#5460#>

<#5461#><#5464#>.. polsym<#5464#><#5461#>(x, n): creates the vector of the <#5468#><#5468#>symmetric powers of the roots of the polynomial x up to power n.

The library syntax is #math452##tex2html_wrap_inline18991#(x).

<#5473#><#5473#> by 1=0 <#5474#>=0<#5477#><#5477#><#5474#>

<#5475#><#5478#>.. recip<#5478#><#5475#>(x): reciprocal polynomial of x, i.e. the coefficients are in reverse order. x must be a polynomial.

The library syntax is #math453##tex2html_wrap_inline18996#(x).

<#5485#><#5485#> by 1=0 <#5486#>=0<#5489#><#5489#><#5486#>

<#5487#><#5490#>.. resultant<#5490#><#5487#>(x, y): resultant of the two polynomials x and y. The algorithm used is the subresultant algorithm.

The library syntax is #math454##tex2html_wrap_inline19001#(x, y).

<#5497#><#5497#> by 1=0 <#5498#>=0<#5501#><#5501#><#5498#>

<#5499#><#5502#>.. reverse<#5502#><#5499#>(x): reverse power series (i.e. x-1, not 1/x ) of x. x must be a power series whose valuation is exactly equal to one.

The library syntax is #math455##tex2html_wrap_inline19008#(x).

<#5509#><#5509#> by 1=0 <#5510#>=0<#5513#><#5513#><#5510#>

<#5511#><#5514#>.. rootmod<#5514#><#5511#>(x, p): roots modulo p of the polynomial x. Slightly slower than <#941#>rootmod2<#941#> below for p ;SPMlt; 100, but much faster for larger values. The particular nonprime value p = 4 is accepted, mainly for 2-adic computations. Multiple roots are <#942#>not<#942#> repeated.

The library syntax is #math456##tex2html_wrap_inline19016#(x, p).

<#5521#><#5521#> by 1=0 <#5522#>=0<#5525#><#5525#><#5522#>

<#5523#><#5526#>.. rootmod2<#5526#><#5523#>(x, p): roots modulo p of the polynomial x. To be used only when p is small. Multiple roots are repeated with their order of multiplicity.

The library syntax is #math457##tex2html_wrap_inline19022#(x, p).

<#5533#><#5533#> by 1=0 <#5534#>=0<#5537#><#5537#><#5534#>

<#5535#><#5538#>.. rootpadic<#5538#><#5535#>(x, p, r): vector of p-adic roots of the polynomial x with p-adic precision equal to r. Multiple roots are <#947#>not<#947#> repeated.

The library syntax is #math458##tex2html_wrap_inline19029#(x, p, r), where r is a C-integer.

<#5545#><#5545#> by 1=0 <#5546#>=0<#5549#><#5549#><#5546#>

<#5547#><#5550#>.. roots<#5550#><#5547#>(x): complex roots of the polynomial x, given as a column vector where each root is repeated according to its multiplicity. The precision is given as for transcendental functions: under GP it is kept in the variable <#950#>prec<#950#> and is transparent to the user, but it must be explicitly given as a second argument in library mode.

The algorithm used is a variant of the Newton-Raphson method and is not guaranteed to converge, but is rather fast. If you get the messages ``too many iterations in roots'' or ``INTERNAL ERROR: incorrect result in roots'', try modifying your polynomial to get the roots.

The library syntax is #math459##tex2html_wrap_inline19034#(x, prec).

<#5557#><#5557#> by 1=0 <#5558#>=0<#5561#><#5561#><#5558#>

<#5559#><#5562#>.. smallbase<#5562#><#5559#>(x): <#5566#><#5566#>integral basis of the number field defined by the monic irreducible polynomial x, using the round 2 algorithm, where one does not take into account squares of primes which are not precomputed.

The library syntax is #math460##tex2html_wrap_inline19038#(x,y), where y will receive the discriminant of the number field (<#955#>not<#955#> of the polynomial x). This program is the translation in C of a program written by David Ford.

<#5571#><#5571#> by 1=0 <#5572#>=0<#5575#><#5575#><#5572#>

<#5573#><#5576#>.. smalldiscf<#5576#><#5573#>(x): <#5580#><#5580#>field discriminant of the number field defined by the monic irreducible polynomial x, where one does not take into account squares of primes which are not precomputed.

The library syntax is #math461##tex2html_wrap_inline19044#(x). See also <#959#>smallbase<#959#>, 3.6.27 above.

<#5585#><#5585#> by 1=0 <#5586#>=0<#5589#><#5589#><#5586#>

<#5587#><#5590#>.. smallpolred<#5590#><#5587#>(x): same as <#961#>polred<#961#> (see 3.6.18 above) except that only a suborder of the maximal order may be used.

The library syntax is #math462##tex2html_wrap_inline19047#(x, prec).

<#5597#><#5597#> by 1=0 <#5598#>=0<#5601#><#5601#><#5598#>

<#5599#><#5602#>.. sturm<#5602#><#5599#>(x): number of real roots of the real polynomial x, using Sturm's algorithm.

The library syntax is #math463##tex2html_wrap_inline19051#(x). The result is a 32-bit C-integer.

<#5609#><#5609#> by 1=0 <#5610#>=0<#5613#><#5613#><#5610#>

<#5611#><#5614#>.. sturmpart<#5614#><#5611#>(x, a, b): number of real roots of the real polynomial x in the interval (a, b], using Sturm's algorithm.

The library syntax is #math464##tex2html_wrap_inline19056#(x). The result is a 32-bit C-integer.

<#5621#><#5621#> by 1=0 <#5622#>=0<#5625#><#5625#><#5622#>

<#5623#><#5626#>.. subst<#5626#><#5623#>#math465#(x, y, z): replace the simple variable y by the argument z in expression x. Every non-scalar type is allowed for x. If x is a power series, z must be either a polynomial, a power series, or a rational function. y must be a simple variable name.

The library syntax is #math466##tex2html_wrap_inline19066#(x, v, z), where v is the number of the variable y.

<#5633#><#5633#> by 1=0 <#5634#>=0<#5637#><#5637#><#5634#>

<#5635#><#5638#>.. taylor<#5638#><#5635#>(x, y): taylor expansion around 0 of x with respect to the simple variable y. x can be of any reasonable type, for example a rational function. The number of terms of the expansion is transparent to the user under GP, but must be given as a second argument in library mode.

The library syntax is #math467##tex2html_wrap_inline19075#(x, y, n), where the 32-bit C-integer n is the desired number of terms in the expansion.

<#5645#><#5645#> by 1=0 <#5646#>=0<#5649#><#5649#><#5646#>

<#5647#><#5650#>.. tchebi<#5650#><#5647#>(x): creates the #math468#xth Tchebicheff polynomial.

The library syntax is #math469##tex2html_wrap_inline19080#(x), where x is a 32-bit C-integer.

by1=0 <#5658#>=0<#5660#><#5660#><#5658#>



<#5659#><#5661#>. Vectors, matrices and linear algebra.<#5661#><#5659#>


<#5664#><#5664#> by 1=0 <#5665#>=0<#5668#><#5668#><#5665#>

<#5666#><#5669#>.. adj<#5669#><#5666#>(x): <#5673#><#5673#>adjoint matrix of x, i.e. the matrix y of cofactors of x such that #math470#x*y = det(x)*Id. x must be a (non necessarily invertible) square matrix.

The library syntax is #math471##tex2html_wrap_inline19089#(x).

<#5680#><#5680#> by 1=0 <#5681#>=0<#5684#><#5684#><#5681#>

<#5682#><#5685#>.. algdep<#5685#><#5682#>(x, k): <#5689#><#5689#>algebraic dependence x being real or complex, finds a polynomial of degree at most k having x as approximate root. The algorithm used is a variant of the LLL algorithm due to Hastad, Lagarias and Schnorr (STACS 1986). Note that the polynomial which is obtained is not necessarily the ``correct'' one. One can check the closeness either by a polynomial evaluation or substitution, or by finding the roots of the polynomial given by algdep.

The library syntax is #math472##tex2html_wrap_inline19095#(x, k, prec), where k is a 32-bit C-integer.

<#5694#><#5694#> by 1=0 <#5695#>=0<#5698#><#5698#><#5695#>

<#5696#><#5699#>.. algdep2<#5699#><#5696#>#math473#(x, k, bit): <#5703#><#5703#>algebraic dependence x being real or complex, finds a polynomial of degree at most k having x as approximate root. bit is a number which should be approximately equal to half the number of precision bits. The algorithm used is the LLL algorithm. Note that the polynomial which is obtained is not necessarily the ``correct'' one. One can check the closeness either by a polynomial evaluation or substitution, or by finding the roots of the polynomial given by algdep2.

The library syntax is #math474##tex2html_wrap_inline19103#(x, k, bit, prec), where k and bit are 32-bit C-integers.

<#5708#><#5708#> by 1=0 <#5709#>=0<#5712#><#5712#><#5709#>

<#5710#><#5713#>.. char<#5713#><#5710#>(x, y): <#5717#><#5717#>characteristic polynomial of x with respect to the variable y, i.e. determinant of y*I - x. x must be a square matrix.

The library syntax is #math475##tex2html_wrap_inline19112#(x, v), where v is the variable number.

<#5722#><#5722#> by 1=0 <#5723#>=0<#5726#><#5726#><#5723#>

<#5724#><#5727#>.. char2<#5727#><#5724#>(x, y): characteristic polynomial of the square matrix x with respect to the variable y using the hessenberg form. This is faster than <#990#>char<#990#> when the coefficients are integermod a prime or real numbers, but can be slower in other base rings.

The library syntax is #math476##tex2html_wrap_inline19118#(x, v), where v is the variable number.

<#5734#><#5734#> by 1=0 <#5735#>=0<#5738#><#5738#><#5735#>

<#5736#><#5739#>.. concat<#5739#><#5736#>(x, y): concatenation of x and y. If x or y is not a vector or matrix, it is considered as a one dimensional vector. All types are allowed for x and y, but the sizes must be compatible. Note that matrices are concatenated horizontally, i.e. the number of rows stays the same. Using transpositions, it is easy to concatenate them vertically.

To concatenate vectors sideways (i.e. to obtain a 2-row or two column matrix), first transform the vector into a 1-row or 1-column matrix using the function mat (3.7.22.).

The library syntax is #math477##tex2html_wrap_inline19128#(x, y).

<#5746#><#5746#> by 1=0 <#5747#>=0<#5750#><#5750#><#5747#>

<#5748#><#5751#>.. det<#5751#><#5748#>(x): determinant of x. x must be a square matrix. Another program called <#1735#><#5755#><#5755#>det2<#1735#>(x) is better when the entries of the matrix are reals or integers for example, but can be much worse for more complicated entries like multivariate polynomials.

The library syntax is #math478##tex2html_wrap_inline19134#(x) and #math479##tex2html_wrap_inline19136#(x).

<#5763#><#5763#> by 1=0 <#5764#>=0<#5767#><#5767#><#5764#>

<#5765#><#5768#>.. eigen<#5768#><#5765#>(x): gives the eigenvectors of x as columns of a matrix.

The library syntax is #math480##tex2html_wrap_inline19140#(x).

<#5775#><#5775#> by 1=0 <#5776#>=0<#5779#><#5779#><#5776#>

<#5777#><#5780#>.. extract<#5780#><#5777#>(x, y): extraction of components of the vector or matrix x according to y. x must be a vector or a matrix. In the case of a matrix, the components are as usual the <#1001#>columns<#1001#> of x. y must be either a number of PARI type integer, in which case it is considered as a mask: The binary bits of y are read from right to left, but correspond to taking the components from left to right. For example, if #math481#y = 13 = (1101)2 then the components 1,3 and 4 are extracted.

Or y can be a vector, with integer entries, in which case these entries correspond to the component number to be extracted, in the order specified.

In the case of a matrix, the difference between extract and matextract is that only columns are extracted.

The library syntax is #math482##tex2html_wrap_inline19151#(x, y).

<#5787#><#5787#> by 1=0 <#5788#>=0<#5791#><#5791#><#5788#>

<#5789#><#5792#>.. gauss<#5792#><#5789#>(x, y): x being a square matrix and y a column vector, finds the solution u of x*u = y, using gaussian elimination. This has the same effect, but is much faster, than x-1*y.

The library syntax is #math483##tex2html_wrap_inline19159#(x, y).

<#5799#><#5799#> by 1=0 <#5800#>=0<#5803#><#5803#><#5800#>

<#5801#><#5804#>.. hermite<#5804#><#5801#>(x): if x is a (not necessarily square) matrix of maximal rank, finds the <#1007#>upper triangular<#1007#> Hermite Normal Form of x (which is a square matrix).

The library syntax is #math484##tex2html_wrap_inline19164#(x).

<#5811#><#5811#> by 1=0 <#5812#>=0<#5815#><#5815#><#5812#>

<#5813#><#5816#>.. hess<#5816#><#5813#>(x): Hessenberg form of the square matrix x.

The library syntax is #math485##tex2html_wrap_inline19168#(x).

by 1=0 <#5823#>=0<#5825#><#5825#><#5823#>

<#5824#><#5826#>.. hilbert<#5826#><#5824#>(x): x being a 32-bit C-integer, creates the <#5830#><#5830#>Hilbert matrix of order x, i.e. the matrix whose coefficient (i,j) is #math486#1#tex2html_wrap_inline19175#i+j-1.

The library syntax is #math487##tex2html_wrap_inline19177#(x).

<#5835#><#5835#> by 1=0 <#5836#>=0<#5839#><#5839#><#5836#>

<#5837#><#5840#>.. indsort<#5840#><#5837#>(x): indirect sorting of the vector x, i.e. if x is an n-dimensional vector, creates the permutation of #math488#[1, 2,..., n] which applied to the components of x sorts x in increasing order.

The library syntax is #math489##tex2html_wrap_inline19186#(x).

<#5847#><#5847#> by 1=0 <#5848#>=0<#5851#><#5851#><#5848#>

<#5849#><#5852#>.. jacobi<#5852#><#5849#>(x): x being a real symmetric matrix, this gives a vector having two components: the first one is the vector of eigenvalues of x, the second is the corresponding orthogonal matrix of eigenvectors of x. The method used is Jacobi's method for symmetric matrices.

The library syntax is #math490##tex2html_wrap_inline19192#(x).

<#5859#><#5859#> by 1=0 <#5860#>=0<#5863#><#5863#><#5860#>

<#5861#><#5864#>.. ker<#5864#><#5861#>(x): gives a basis for the kernel of the matrix x as columns of a matrix. A priori the matrix can have entries of any type. See also <#1020#>keri<#1020#> and <#1021#>kerr<#1021#>.

The library syntax is #math491##tex2html_wrap_inline19196#(x).

<#5871#><#5871#> by 1=0 <#5872#>=0<#5875#><#5875#><#5872#>

<#5873#><#5876#>.. keri<#5876#><#5873#>(x): same as <#1024#>ker<#1024#>, except that it assumes that the matrix has entries of type integer. In that case it is much faster.

The library syntax is #math492##tex2html_wrap_inline19199#(x).

<#5883#><#5883#> by 1=0 <#5884#>=0<#5887#><#5887#><#5884#>

<#5885#><#5888#>.. kerr<#5888#><#5885#>(x): gives a basis for the kernel of the matrix x as columns of a matrix, where x has elements which can be nonexact real or complex numbers. In that case, the precision of the matrix entries determines what is meant by an element of the kernel. In particular, if the matrix is ill conditioned, the results may not be what you expect.

The library syntax is #math493##tex2html_wrap_inline19204#(x).

<#5895#><#5895#> by 1=0 <#5896#>=0<#5899#><#5899#><#5896#>

<#5897#><#5900#>.. image<#5900#><#5897#>(x): gives a basis for the image of the matrix x as columns of a matrix. A priori the matrix can have entries of any type.

The library syntax is #math494##tex2html_wrap_inline19208#(x).

<#5907#><#5907#> by 1=0 <#5908#>=0<#5911#><#5911#><#5908#>

<#5909#><#5912#>.. lindep<#5912#><#5909#>(x): <#5916#><#5916#>x being a vector with real or complex coefficients, finds a small integral linear combination among these coefficients using a variant of the LLL algorithm due to Hastad, Lagarias and Schnorr (STACS 1986).

The library syntax is #math495##tex2html_wrap_inline19212#(x, prec).

<#5921#><#5921#> by 1=0 <#5922#>=0<#5925#><#5925#><#5922#>

<#5923#><#5926#>.. lindep2<#5926#><#5923#>(x, bit): <#5930#><#5930#>x being a vector with real or complex coefficients, finds a small integral linear combination among these coefficients using the LLL algorithm. bit is a parameter which should be about one half the number of precision bits.

The library syntax is #math496##tex2html_wrap_inline19217#(x, bit, prec) where bit is a C-integer.

<#5935#><#5935#> by 1=0 <#5936#>=0<#5939#><#5939#><#5936#>

<#5937#><#5940#>.. lll<#5940#><#5937#>(x): LLL algorithm applied to the <#1037#>columns<#1037#> of the (non necessarily square) matrix x. The result is a square transformation matrix T such that xT is an LLL-reduced basis of the lattice generated by the column vectors of x. The computations are done with real numbers (i.e. not with rational numbers) hence are fast but as presently programmed (version 1.35) are numerically unstable.

The library syntax is #math497##tex2html_wrap_inline19225#(x, prec).

<#5947#><#5947#> by 1=0 <#5948#>=0<#5951#><#5951#><#5948#>

<#5949#><#5952#>.. lllgram<#5952#><#5949#>(x): same as LLL except that the matrix x which is now square is the gram matrix of the lattice vectors, and not the coordinates of the vectors themselves. The result is again the transformation matrix T which gives (as columns) the coefficients with respect to the initial basis vectors. Same remarks as for LLL about numerical instability in the present version 1.35.

The library syntax is #math498##tex2html_wrap_inline19230#(x).

<#5959#><#5959#> by 1=0 <#5960#>=0<#5963#><#5963#><#5960#>

<#5961#><#5964#>.. lllrat<#5964#><#5961#>(x): same as LLL except that the computations are all done in rational numbers. Hence no risk of numerical instability, but extremely slow.

The library syntax is #math499##tex2html_wrap_inline19233#(x).

<#5971#><#5971#> by 1=0 <#5972#>=0<#5975#><#5975#><#5972#>

<#5973#><#5976#>.. mat<#5976#><#5973#>(x): transform the object x into a matrix. If x is not a vector or a matrix, this creates a 1×1 matrix. If x is a row (resp. column) vector, this creates a 1-row (resp. 1-column) matrix. If x is already a matrix, a copy of x is created.

This function can be useful in connection with the function <#1044#>concat<#1044#> (see 3.7.5.).

The library syntax is #math500##tex2html_wrap_inline19242#(x).

<#5983#><#5983#> by 1=0 <#5984#>=0<#5987#><#5987#><#5984#>

<#5985#><#5988#>.. matextract<#5988#><#5985#>#math501#(x, y, z): extraction of a matrix from the matrix x, the line mask or vector y and the column mask or vector z. x must be a matrix, and y and z either numbers of PARI type integer or vectors. The extraction is done using the same rules as for <#1047#>extract<#1047#> (3.7.8). The difference with <#1048#>extract<#1048#> is that both lines and columns are extracted.

The library syntax is #math502##tex2html_wrap_inline19251#(x, y, z).

<#5995#><#5995#> by 1=0 <#5996#>=0<#5999#><#5999#><#5996#>

<#5997#><#6000#>.. minim<#6000#><#5997#>(x): number of minimal vectors and minimum on #tex2html_wrap_inline19254# of the positive definite integral quadratic form x, as a two component vector. x is represented by a square and symmetric matrix with integer entries.

The library syntax is #math503##tex2html_wrap_inline19257#(x)

by 1=0 <#6008#>=0<#6010#><#6010#><#6008#>

<#6009#><#6011#>.. pascal<#6011#><#6009#>(x): creates as a matrix the lower triangular <#6015#><#6015#>pascal triangle of order x + 1 (i.e. with binomial coefficients up to x).

The library syntax is #math504##tex2html_wrap_inline19262#(x), where x is a 32-bit C-integer.

<#6020#><#6020#> by 1=0 <#6021#>=0<#6024#><#6024#><#6021#>

<#6022#><#6025#>.. rank<#6025#><#6022#>(x): rank of the matrix x.

The library syntax is #math505##tex2html_wrap_inline19267#(x), and the result is a 32-bit C-integer.

<#6032#><#6032#> by 1=0 <#6033#>=0<#6036#><#6036#><#6033#>

<#6034#><#6037#>.. signat<#6037#><#6034#>(x): signature of the quadratic form represented by the symmetric matrix x. The result is a two-component vector.

The library syntax is #math506##tex2html_wrap_inline19271#(x)

<#6044#><#6044#> by 1=0 <#6045#>=0<#6048#><#6048#><#6045#>

<#6046#><#6049#>.. smith<#6049#><#6046#>(x): if x is a nonsingular square matrix, outputs the vector of elementary divisors of x (i.e. the diagonal of the Smith normal form of x)

The library syntax is #math507##tex2html_wrap_inline19277#(x).

<#6056#><#6056#> by 1=0 <#6057#>=0<#6060#><#6060#><#6057#>

<#6058#><#6061#>.. sort<#6061#><#6058#>(x): sort the vector x in ascending order, using the heapsort method. x must be a vector, and its components integers, reals, or fractions. A related function is <#1062#>indsort<#1062#> (see 3.7.13 above) which gives the indexes of the sorted vector in terms of the initial one. For example, <#1063#>extract(x,indsort(x))<#1063#> is equivalent to <#1064#>sort(x)<#1064#>.

The library syntax is #math508##tex2html_wrap_inline19282#(x).

<#6068#><#6068#> by 1=0 <#6069#>=0<#6072#><#6072#><#6069#>

<#6070#><#6073#>.. sqred<#6073#><#6070#>(x): <#6077#><#6077#>decomposition into squares of the quadratic form represented by the symmetric matrix x. The result is a matrix whose diagonal entries are the coefficients of the squares, and the non diagonal entries represent the bilinear forms.

The library syntax is #math509##tex2html_wrap_inline19286#(x).

<#6082#><#6082#> by 1=0 <#6083#>=0<#6086#><#6086#><#6083#>

<#6084#><#6087#>.. supplement<#6087#><#6084#>(x): assuming that the columns of the matrix x are linearly independent (if they are not, an error message is issued), find a square invertible matrix whose first columns are the columns of x, i.e. supplement the columns of x to a basis of the whole space.

The library syntax is #math510##tex2html_wrap_inline19292#(x).

<#6094#><#6094#> by 1=0 <#6095#>=0<#6098#><#6098#><#6095#>

<#6096#><#6099#>.. trace<#6099#><#6096#>(x): This applies to quite general x. If x is not a matrix, it is equal to the sum of x and its conjugate, except for polymods where it is the trace as an algebraic number.

For x a square matrix, it is the ordinary trace. If x is a non square matrix (but not a vector), an error occurs.

The library syntax is #math511##tex2html_wrap_inline19300#(x).

<#6106#><#6106#> by 1=0 <#6107#>=0<#6110#><#6110#><#6107#>

<#6108#><#6111#>.. trans<#6111#><#6108#>(x) or #math512#x#tex2html_wrap_inline19303#: transpose of x. This has an effect only on vectors and matrices.

The library syntax is #math513##tex2html_wrap_inline19306#(x).

by1=0 <#6118#>=0<#6120#><#6120#><#6118#>



<#6119#><#6121#>. Sums, products, integrals and similar functions.<#6121#><#6119#>


Although the GP calculator is programmable, it is useful to have preprogrammed a number of loops, including sums, products, and a certain number of recursions. Also, a number of functions from numerical analysis like numerical integration, summation of series or plotting will be described here.

One of the parameters in these loops must be the control variable, hence a simple variable name. The last parameter must be any legal PARI expression, including of course expressions using loops. Since it is much easier to program directly the loops in library mode, these functions are mainly useful for GP programming. The use of these functions in library mode is a little tricky and its explanation will be omitted, although the reader can try and figure it out by himself by reading the source code. A brief explanatory sketch is given in section 3.9.7. Hence in this section we do not give the library syntax.

The letter X will always denote any simple variable name, and represents the formal parameter used in the function.

<#6124#><#6124#> by 1=0 <#6125#>=0<#6128#><#6128#><#6125#>

<#6126#><#6129#>.. divsum<#6129#><#6126#>#math514#(n, X, expr): sum of expression expr over the positive divisors of n.

In the present version 1.35, n is restricted to being less than 231.

<#6133#><#6133#> by 1=0 <#6134#>=0<#6137#><#6137#><#6134#>

<#6135#><#6138#>.. hvector<#6138#><#6135#>#math515#(n, X, expr): creates a row vector with n components whose components are the expression expr evaluated at the integral points between 1 and n.

by 1=0 <#6142#>=0<#6144#><#6144#><#6142#>

<#6143#><#6145#>.. (numerical) integration<#6145#><#6143#>:<#6149#><#6149#> A number of Romberg-like integration methods are implemented. The user should not require too much accuracy: 18 or 28 decimal digits is OK, but not much more. In addition, analytical cleanup of the integral must have been done: there must be no singularities in the interval or at the boundaries. In practice this can be accomplished with a simple change of variable. Furthermore, for improper integrals, where one or both of the limits of integration are plus or minus infinity, the function must decrease sufficiently rapidly at infinity. This can often be accomplished through integration by parts.

Note that <#6151#><#6151#>infinity can be represented with essentially no loss of accuracy by 1e4000. However beware of real underflow when dealing with rapidly decreasing functions. For example, if one wants to compute the #math516##tex2html_wrap_inline19318#e-x2 dx to 28 decimal digits, one should set infinity equal to 10 for example, and cetrainly not to 1e4000.

The integrand may have values belonging to a vector space over the real numbers; in particular, it can be complex-valued or vector-valued.

<#6153#><#6153#> by 1=0 <#6154#>=0<#6157#><#6157#><#6154#>

<#6155#><#6158#>.. intgen<#6158#><#6155#>#math517#(X = a, b, expr): general driver routine for doing numerical integration from a to b of the expression expr, with respect to the formal variable X.

<#6162#><#6162#> by 1=0 <#6163#>=0<#6166#><#6166#><#6163#>

<#6164#><#6167#>.. intinf<#6167#><#6164#>#math518#(X = a, b, expr): numerical integration from a to b, tailored for being used when a or b are infinite. One <#1086#>must<#1086#> have ab ;SPMgt; 0, and in fact if for example b = ∞, then it is preferable to have a as large as possible, at least a≥1.

<#6171#><#6171#> by 1=0 <#6172#>=0<#6175#><#6175#><#6172#>

<#6173#><#6176#>.. intnum<#6176#><#6173#>#math519#(X = a, b, expr): simple numerical integration from a to b. This is the fastest method, and should be used when a and b are not too large, the function is smooth, and can be evaluated exactly everywhere on the interval [a, b].

<#6180#><#6180#> by 1=0 <#6181#>=0<#6184#><#6184#><#6181#>

<#6182#><#6185#>.. intopen<#6185#><#6182#>#math520#(X = a, b, expr): numerical integration from a to b. This method should be used, again when a and b are not too large, but when the function is now allowed to be undefined (but continuous) at a or b, for example the function sin(x)/x at x = 0.

Two summation methods are also implemented, for alternating series, and for series with terms of the same sign (see 3.8.18 and 3.8.20).

<#6189#><#6189#> by 1=0 <#6190#>=0<#6193#><#6193#><#6190#>

<#6191#><#6194#>.. matrix<#6194#><#6191#>#math521#(m, n, X, Y, expr): creation of the m×n matrix whose coefficients are given by the expression expr. There are two formal parameters in expr, the first one (X) corresponding to the rows, the second (Y) to the columns, and X goes from 1 to m, Y goes from 1 to n.

<#6198#><#6198#> by 1=0 <#6199#>=0<#6202#><#6202#><#6199#>

<#6200#><#6203#>.. plot<#6203#><#6200#>#math522#(X = a, b, expr): crude plot of the function represented by expression expr from a to b.

<#6207#><#6207#> by 1=0 <#6208#>=0<#6211#><#6211#><#6208#>

<#6209#><#6212#>.. ploth<#6212#><#6209#>#math523#(X = a, b, expr): high precision plot of the function y = f (x) represented by the expression expr, x going from a to b. Since this involves around 1000 function calls, it is advised to keep the current precision to a minimum (i.e. 9) before calling <#1092#>ploth<#1092#>.

Note that this function is very much implementation dependent and has been written for the moment for the Macintosh, for the SUN under Sunview/Suntools and for the X-window system (release X11). An Atari/gem version is also available upon request.

<#6216#><#6216#> by 1=0 <#6217#>=0<#6220#><#6220#><#6217#>

<#6218#><#6221#>.. ploth2<#6221#><#6218#>#math524#(T = a, b, expr): high precision plot of the curve represented in parametric form by #math525#x = f (t), y = g(t), represented by the expression expr, t going from a to b. The expression must be a two component vector. Since this involves around 2000 function calls, it is advised to keep the current precision to a minimum (i.e. 9) before calling <#1094#>ploth2<#1094#>.

Note that, as for <#1095#>ploth<#1095#> above, this function has been written for the moment only for the Macintosh, for the SUN under Sunview/Suntools and for the X-window system (release X11). An Atari/gem version is also available upon request.

<#6225#><#6225#> by 1=0 <#6226#>=0<#6229#><#6229#><#6226#>

<#6227#><#6230#>.. prod<#6230#><#6227#>#math526#(x, X = a, b, expr): product of expression expr, initialized at x, the formal parameter X going from a to b. As for <#1097#>sum<#1097#>, the initialization parameter x must be given: its main purpose is to force the type of the operations being performed. For example if it is put equal to the integer 1, operations will start being done exactly. If it is put equal to the real 1., they will be done using real numbers having the default precision. If it is put equal to the power series 1 + O(Xk) for a certain k, they will be done using power series of precision at most k. These are the three most common initializations.

As an extreme example, compare

#math527#

prod(1, j = 1, 100,(1 - X#tex2html_wrap_indisplay19386#j))

with

#math528#

prod(1 + O(X#tex2html_wrap_indisplay19388#101), j = 1, 100,(1 - X#tex2html_wrap_indisplay19389#j)).

<#6236#><#6236#> by 1=0 <#6237#>=0<#6240#><#6240#><#6237#>

<#6238#><#6241#>.. prodeuler<#6241#><#6238#>#math529#(X = a, b, expr): product of expression expr, initialized at 1., the formal parameter X ranging over the prime numbers between a and b.<#6245#><#6245#>

<#6247#><#6247#> by 1=0 <#6248#>=0<#6251#><#6251#><#6248#>

<#6249#><#6252#>.. prodinf<#6252#><#6249#>#math530#(X = a, expr): <#6256#><#6256#>infinite product of expression expr, the formal parameter X starting at a. The evaluation stops when the relative error of the expression minus 1 is less than the default precision. The expressions must always evaluate to an element of #tex2html_wrap_inline19400#.

<#6259#><#6259#> by 1=0 <#6260#>=0<#6263#><#6263#><#6260#>

<#6261#><#6264#>.. prodinf1<#6264#><#6261#>#math531#(X = a, expr): infinite product of expression 1 + expr, the formal parameter X starting at a. The evaluation stops when the relative error of the expression is less than the default precision. The expressions must always evaluate to an element of #tex2html_wrap_inline19406#.

<#6269#><#6269#> by 1=0 <#6270#>=0<#6273#><#6273#><#6270#>

<#6271#><#6274#>.. solve<#6274#><#6271#>#math532#(X = a, b, expr): real root of expression expr between a and b, under the condition #math533#expr(X = a)*expr(X = b)≤ 0. The method used is Brent's method.

<#6278#><#6278#> by 1=0 <#6279#>=0<#6282#><#6282#><#6279#>

<#6280#><#6283#>.. sum<#6283#><#6280#>#math534#(x, X = a, b, expr): sum of expression expr, initialized at x, the formal parameter going from a to b. As for <#1110#>prod<#1110#>, the initialization parameter x must be given: its main purpose is to force the type of the operations being performed. For example if it is put equal to the integer 0, operations will start being done exactly. If it is put equal to the real 0., they will be done using real numbers having the default precision. If it is put equal to the power series O(Xk) for a certain k, they will be done using power series of precision at most k. These are the three most common initializations.

As an extreme example, compare

#math535#

sum(0, j = 1, 1000, 1/j)

with

#math536#

sum(0., j = 1, 1000, 1/j).

<#6289#><#6289#> by 1=0 <#6290#>=0<#6293#><#6293#><#6290#>

<#6291#><#6294#>.. sumalt<#6294#><#6291#>#math537#(X = a, expr): numerical summation of the series expr, which should be an <#6298#><#6298#>alternating series, the formal variable X starting at a. The algorithm used is Euler's method of finite differences combined with a trick due to van Wijngaarden.

Divergent alternating series can sometimes be summed by this method, as well as series which are not exactly alternating (see for example section 3.9.5).

<#6300#><#6300#> by 1=0 <#6301#>=0<#6304#><#6304#><#6301#>

<#6302#><#6305#>.. suminf<#6305#><#6302#>#math538#(X = a, expr): <#6309#><#6309#>infinite sum of expression expr, the formal parameter X starting at a. The evaluation stops when the relative error of the expression is less than the default precision. The expressions must always evaluate to an element of #tex2html_wrap_inline19433#.

<#6312#><#6312#> by 1=0 <#6313#>=0<#6316#><#6316#><#6313#>

<#6314#><#6317#>.. sumpos<#6317#><#6314#>#math539#(X = a, expr): numerical summation of the series expr, which must be a series of terms having the same sign, the formal variable X starting at a. The algorithm used is van Wijngaarden's trick for converting such a series into an alternating one, and is quite slow.

<#6321#><#6321#> by 1=0 <#6322#>=0<#6325#><#6325#><#6322#>

<#6323#><#6326#>.. vector<#6326#><#6323#>#math540#(n, X, expr): creates a row vector with n components whose components are the expression expr evaluated at the integral points between 1 and n (identical with <#1119#>hvector<#1119#>).

<#6330#><#6330#> by 1=0 <#6331#>=0<#6334#><#6334#><#6331#>

<#6332#><#6335#>.. vvector<#6335#><#6332#>#math541#(n, X, expr): creates a column vector with n components whose components are the expression expr evaluated at the integral points between 1 and n.

by1=0 <#6339#>=0<#6341#><#6341#><#6339#>



<#6340#><#6342#>. Programming under GP and user-defined functions.<#6342#><#6340#>


<#6345#><#6345#>

by 1=0 <#6355#>=0<#6357#><#6357#><#6355#>

<#6356#><#6358#>.. <#6366#><#6366#>Variables and symbolic expressions<#6358#><#6356#>.

In GP you can use up to 256 variable names. These names can be any standard identifier names, i.e. they must start with a letter and contain only alphanumeric characters. To avoid confusion with other symbols, you must not use non alphanumeric symbols like '_', '$', or '.'. In addition to the function names which you must not use (see the list with #math542#\c) there are exactly three special variable names which you are not allowed to use: <#1124#>pi<#1124#> and <#1125#>euler<#1125#>, which represent well known constants, but also <#1126#>i<#1126#> which is always equal to the square root of -1. Since usually you are not going to use the variable name <#1127#>pi<#1127#> for anything else but the number π, and similarly for <#1128#>euler<#1128#>, these two will not create any problems. On the other hand, you will have to get used to calling a loop control variable with a name different from <#1129#>i<#1129#>. We realize that this may initially be the cause for some confusion, but we do not have any replacement symbol for the square root of -1.

Now the main thing to understand is that PARI/GP is <#1130#>not<#1130#> a symbolic manipulation package, although it shares some of the functionalities. One of the main consequences of this fact is that all expressions are evaluated as soon as they are written, they never stay in a purely abstract form. As an important example, consider what happens when you use a variable name <#1131#>before<#1131#> assigning a value into it. This is perfectly acceptable to GP, who considers this variable in fact as a polynomial of degree 1, with coefficients 1 in degree 1, 0 in degree 0, whose variable is the variable name you used.

If later you assign a value to that variable, the objects which you have created before will still be considered as polynomials. If you want to obtain their value, use the function <#1132#>eval<#1132#> (see 3.6.6 above).

Another consequence is that the variables are numbered in the order that they appear, and the main variable of an expression is always the lowest numbered variable. Hence if you are working with expressions involving several variables and want to have them ordered in a specific manner <#1133#>in the internal representation<#1133#>, the simplest is just to write down the variables one after the other under GP before starting any real computations. If you already have started working and want to change the names of the variables in an object, use the function <#1134#>changevar<#1134#> (see 3.2.2). If you only want to have them ordered when the result is printed, you can also use the function <#1135#>reorder<#1135#> (see 3.9.4.6).

Finally, note that if x is a vector, you can assign a result to x[m] (i.e. write something like x[k] = expr). If x is a matrix, you can assign a result to x[m, n], but <#1136#>not<#1136#> to x[m], which is a vector.

by 1=0 <#6377#>=0<#6379#><#6379#><#6377#>

<#6378#><#6380#>.. <#6392#><#6392#>Expressions and <#6394#><#6394#>expression sequences<#6380#><#6378#>.

An expression is formed by combining the GP operators, functions (including user-defined functions) and control statements. It may be preceded by an assignment statement '=' into a variable. It always has a value, which can be any PARI object.

Several expressions can be combined on a single line by separating them with semicolons (';') and also with colons (':') for those who are used to BASIC. Such an expression sequence will be called simply a seq. A seq also has a value, which is the value of the last nonempty expression in the sequence. Under GP, the value of the seq is always put on the stack (i.e. it will become the next object %n), and only that value. The values of the other expressions in the seq are discarded after the execution of the seq is complete, except of course if they were assigned into variables. In addition, the value of the seq (or of course of an expression if there is only one) is printed if the line does not end with a semicolon (';').

<#6396#><#6396#> by 1=0 <#6397#>=0<#6400#><#6400#><#6397#>

<#6398#><#6401#>.. Control statements<#6401#><#6398#>.

A number of control statements are available under GP. They are simpler and have a slightly different syntax than their C counterparts, but are quite powerful enough to write any kind of program. Some of them are specific to GP, since they are made for number theorists. They are as follows. As usual, X will denote any simple variable name, and seq will always denote a sequence of expressions, including the empty sequence.

<#6405#><#6405#> by1 <#6406#>=0<#6409#><#6409#><#6406#>

<#6407#><#6410#>.. . for<#6410#><#6407#>#math543#(X = a, b, seq): the formal variable X going from a to b, the seq is evaluated. Nothing is done if a ;SPMgt; b. a and b must be in #tex2html_wrap_inline19475#.

<#6415#><#6415#> by1 <#6416#>=0<#6419#><#6419#><#6416#>

<#6417#><#6420#>.. . fordiv<#6420#><#6417#>(n, X, seq): the formal variable X ranging through the positive divisors of n, the sequence seq is evaluated. n must be of type integer.

<#6424#><#6424#> by1 <#6425#>=0<#6428#><#6428#><#6425#>

<#6426#><#6429#>.. . forprime<#6429#><#6426#>#math544#(X = a, b, seq): the formal variable X ranging over the prime numbers between a to b (including a and b if they are prime), the seq is evaluated. Nothing is done if a ;SPMgt; b. Note that a and b must be in #tex2html_wrap_inline19492#.

<#6434#><#6434#> by1 <#6435#>=0<#6438#><#6438#><#6435#>

<#6436#><#6439#>.. . forstep<#6439#><#6436#>#math545#(X = a, b, s, seq): the formal variable X going from a to b, in increments of s, the seq is evaluated. Nothing is done if s ;SPMgt; 0 and a ;SPMgt; b or if s ;SPMlt; 0 and a ;SPMlt; b. a, b and s must be in #tex2html_wrap_inline19507#, and s must be nonzero.

<#6444#><#6444#> by1 <#6445#>=0<#6448#><#6448#><#6445#>

<#6446#><#6449#>.. . if<#6449#><#6446#>#math546#(a, seq1, seq2): if a is nonzero, the expression sequence seq1 is evaluated, otherwise the expression seq2 is evaluated. Of course, seq1 or seq2 may be empty, but the syntax must stay the same: <#1145#>if(a,seq,)<#1145#> evaluates seq if a is not equal to zero, does nothing otherwise; <#1146#>if(a,,seq)<#1146#> evaluates seq if a is equal to zero, does nothing otherwise.

<#6453#><#6453#> by1 <#6454#>=0<#6457#><#6457#><#6454#>

<#6455#><#6458#>.. . until<#6458#><#6455#>(a, seq): evaluate expression sequence seq until a is not equal to 0 (i.e. until a is true). If a is initially not equal to 0, seq is evaluated once (more generally, the condition on a is tested <#1148#>after<#1148#> execution of the seq, not before as in <#1149#>while<#1149#>.

<#6462#><#6462#> by1 <#6463#>=0<#6466#><#6466#><#6463#>

<#6464#><#6467#>.. . while<#6467#><#6464#>(a, seq): while a is nonzero evaluate the expression sequence seq. The test is made <#1151#>before<#1151#> evaluating the seq, hence in particular if a is initially equal to zero the seq will not be evaluated at all.

by 1=0 <#6471#>=0<#6473#><#6473#><#6471#>

<#6472#><#6474#>.. Specific functions used in GP programming<#6474#><#6472#>.

In addition to the general PARI functions, it is necessary to have some functions which will be of use specificlly for GP. They are as follows:

<#6478#><#6478#> by1 <#6479#>=0<#6482#><#6482#><#6479#>

<#6480#><#6483#>.. . kill<#6483#><#6480#>(x): kills the present value of the variable or user-defined function x. After <#1154#>kill<#1154#> of a variable, since the variable does not have a value, as has been explained above it is again considered as a monic polynomial of degree 1 with no constant term.

For the following four printing functions, list represents a list (separated by commas) either of PARI objects or of character strings between double quotes '<#1155#>;SPMquot;<#1155#>', which are always printed as they are.

<#6487#><#6487#> by1 <#6488#>=0<#6491#><#6491#><#6488#>

<#6489#><#6492#>.. . pprint<#6492#><#6489#>(list): output list in prettyprint (beautified) format, ending with a newline.

<#6496#><#6496#> by1 <#6497#>=0<#6500#><#6500#><#6497#>

<#6498#><#6501#>.. . pprint1<#6501#><#6498#>(list): output list in prettyprint (beautified) format, without ending with a newline.

<#6505#><#6505#> by1 <#6506#>=0<#6509#><#6509#><#6506#>

<#6507#><#6510#>.. . print<#6510#><#6507#>(list): output list in raw format, ending with a newline.

<#6514#><#6514#> by1 <#6515#>=0<#6518#><#6518#><#6515#>

<#6516#><#6519#>.. . print1<#6519#><#6516#>(list): output list in raw format, without ending with a newline.

<#6523#><#6523#> by1 <#6524#>=0<#6527#><#6527#><#6524#>

<#6525#><#6528#>.. . reorder<#6528#><#6525#>(x): x must be a vector. If x is the empty vector, this gives the vector whose components are the existing variables in increasing order (i.e. in decreasing importance). If x is nonempty, it must be a permutation of variable names, and this permutation gives a new order of importance of the variables. For example, if the existing order is <#1161#>[x,y,z]<#1161#>, then after <#1162#>reorder([z,x])<#1162#> the order of importance of the variables will be <#1163#>[z,y,x]<#1163#>.

<#6532#><#6532#> by1 <#6533#>=0<#6536#><#6536#><#6533#>

<#6534#><#6537#>.. . texprint<#6537#><#6534#>(list): outputs list in TEX<#1165#><#1165#> format. This output can then be used in a TEX<#1166#><#1166#> manuscript.

Warning: in the present version 1.35, the printing is done on the standard output, hence to be able to get the output into a file, you must be either in an emacs session or shell (see 3.10), in a windowing system (which enables you to grab the output with the mouse), under a UNIX script command, or with the logfile enabled (see the command #math547#\l). This will be improved.

<#6541#><#6541#> by 1=0 <#6542#>=0<#6545#><#6545#><#6542#>

<#6543#><#6546#>.. User defined functions<#6546#><#6543#>.

It is very easy to define a new function under GP, which can then be used like any other function. The syntax is as follows:

<#1168#>name(list of true formal variables, list of local variables)=seq<#1168#>

where <#1169#>name<#1169#> is the name that you want to give to your function (same syntactic restrictions as for variable names). <#1170#>list of true formal variables<#1170#> is the list of variables corresponding to those which you will actually use when calling your function, the variables being separated by commas, and the list being allowed to be empty. <#1171#>list of local variables<#1171#> is the list of the additional local variables which you will use in the definition of the function. If you omit some or all of these local variable declarations, the function will probably still work, but the nondeclared variables will become global, hence known outside of the function, and this may have undesirable side-effects. On the other hand, in some cases it may also be what you want. Finally, as usual <#1172#>seq<#1172#> is any expression sequence.

Once the function is defined using the above syntax, you can use it like any other function. In addition, you can also recall its definition exactly as you do for predefined functions, that is by writing <#1173#>?name<#1173#>. One small difference with predefined functions is that you can never redefine such a function, while you can redefine a user-defined function as many times as you want, without using the <#1174#>kill<#1174#> instruction.

In a given session you can give any identifier name to a function, <#1175#>except<#1175#> those of predefined functions (of course) but also those of any variables that have been used, even if they have been killed. On the other hand, if you want to use as a variable name the name of a user-defined function, it is enough (and necessary) to kill this function name first.

An amusing example of a user-defined function is the following. It is intended to illustrate both the use of user-defined functions and the power of the <#1176#>sumalt<#1176#> function. Although since version 1.34 the <#6550#><#6550#>Riemann zeta-function is included in the standard functions, let us assume that this is not the case (or that we want another implementation). One way to define it, which is probably the simplest (but certainly not the most efficient), is as follows:

<#6552#><#6552#> <#1738#>zet(s,j)=sumalt(j=1,(-1)ˆ(j-1)*jˆ(-s))/(1-2ˆ(1-s))<#1738#>

Then this gives reasonably good accuracy and speed as long as you are not too far from the domain of convergence. Try it for s integral between -5 and 5, say, or for s = 0.5 + i*t where #math548#t = 14.134... Of course, the call to the function is done by <#1182#>zet(s)<#1182#>, the variable <#1183#>j<#1183#> must not be given.

by 1=0 <#6562#>=0<#6564#><#6564#><#6562#>

<#6563#><#6565#>.. Special <#6573#><#6573#>editing characters<#6565#><#6563#>. A GP program can of course have more than one line. Since GP executes your commands as soon as you have finished typing them, there must be a way to tell it to wait for the next line or lines of input before doing anything. There are two ways of doing this.

The first one is simply to use the <#6575#><#6575#>backslash character '#math549#\' at the end of the line that you are typing, just before hitting ;SPMlt;return;SPMgt;. This tells GP that what you will write on the next line is the physical continuation of what you have just written. In other words, it makes GP forget your newline character. For example if you use this while defining a function, and if you ask for the definition of the function using <#1186#>?name<#1186#>, you will see that your backslash has disappeared and that everything is on the same line. You can type a #math550#\ anywhere. It will be interpreted as above only if it is immediately followed by a newline. For example, you can type

<#1187#>3+#math551#\<#1187#>

<#1188#>4<#1188#>

instead of typing <#1189#>3+4<#1189#>.

The second one cannot be used everywhere, but is in general more useful. It is the use <#6577#><#6577#> of braces '{' and '}'. When GP sees an opening brace ('{`) at the beginning of a line, it understands that you are writing a program, and newlines will be ignored (but registered, contrary to when you use a backslash) until you type a closing brace '}'. However, there is an important (but easily obeyed) restriction: inside an open brace-close brace pair, all newlines must occur after a semicolon (';'), i.e. between two expressions forming a seq.

Of course you can combine the use of backslash and braces.

by 1=0 <#6587#>=0<#6589#><#6589#><#6587#>

<#6588#><#6590#>.. The GP/PARI <#6598#><#6598#>programming language<#6590#><#6588#>.

As we have seen, the GP calculator uses a primitive purely interpreted language. The structure of this language is in fact more reminiscent of LISP with a functional notation, <#1192#>f(x, y)<#1192#> rather than <#1193#>(f x y)<#1193#>: all programming constructs, such as <#1194#>if, while,<#1194#> etc.) are functions, and the main loop does not really execute, but rather evaluates (sequences of) expressions.

Of course, it is by no means a true LISP. Function names are distinct from variable names: once a name has been used as a variable name, this cannot be changed in the same session. Predefined function names cannot be used for anything else, and the number of actual parameters must match the declaration given in the online help. Identifiers that are used for user-defined functions can be freely redefined for other functions, or totally forgotten (using <#1195#>kill<#1195#>) and later reused for a variable, for example.

Each variable has a stack of values, implemented as a linked list. When a new scope is entered, during a function call, the value of the actual parameter is pushed on the stack. If the parameter is not supplied, a special 0 value called <#1741#><#6600#><#6600#>gnil<#1741#> is pushed on the stack (this value is not printed if it is returned as the result of a GP expression sequence). Upon exit, the stack decreases. You can <#1197#>kill<#1197#> a variable, decreasing the stack yourself. This should be used only at the top level of GP, to undo the effect of an assignment, not from a function. However, the stack has a bottom: the value of a variable is the monomial of degre 1 in this variable, as is natural for a mathematician.

Note that the iterative constructs which use a variable name (<#1198#>for, fordiv, forprime, prod, sum, vector, matrix, plot,<#1198#> etc.) also consider the given variable to be local to the construct. A value is pushed on entry and pulled on exit. So, it is not necessary for a function using such a construct to declare the variable as a dummy formal parameter. In particular, in our <#1199#>zet<#1199#> example above, the variable <#1200#>j<#1200#> need not be declared.

Otherwise, it is strongly recommended to declare in this way all other variables that are used inside a function: If a function accesses a variable which is not one of its formal parameters, the value used will be the one which happens to be on top of the stack at the time of the call. This could be a ``global'' value, or a local value belonging to any function higher in the call chain. So, be warned.

<#1201#>Implementation note.<#1201#> For the curious reader, here is how these stacks are handled: a <#6602#><#6602#>hashing function is computed from the identifer, and used as an index in <#1742#><#6604#><#6604#>hashtable<#1742#>, a table of pointers. Each of these pointers begins a linked list of structures (type <#1743#><#6606#><#6606#>entree<#1743#>). The linked list is searched linearly for the identifer (each list will have less than 7 components or so). When the correct <#1205#>entree<#1205#> is found, it points to the top of the stack of values for that identifier if it is a variable, to the function itself if it is a predefined function, and to a copy of the text of the function if it is a user-defined function. When an error occurs, all of this maze (rather a tree, in fact) is searched and (hopefully) replaced in the situation preceding the last call of the main evaluator.

by1=0 <#6615#>=0<#6617#><#6617#><#6615#>



<#6616#><#6618#>. Using GP under <#6625#><#6625#>gnuemacs.<#6618#><#6616#>


Thanks to the help of Annette Hoffman from the university of Saarbrücken, and since version 1.34.05 from David Carlisle from the university of Manchester, it is possible and in fact desirable to use GP as a subprocess of gnuemacs. This is of course possible only if gnuemacs has been installed on your machine.

To use this, you must include in your <#1207#>.emacs<#1207#> file the following line:

<#1745#>(load ;SPMquot;<#6627#><#6627#>pari;SPMquot; nil t)<#1745#>

where <#1209#>pari.el<#1209#> is the name of the file that will have to be loaded by gnuemacs (if you have changed the name, or if you have the file in a different directory, you must of course supply the correct name). This file is included in the PARI distribution.

Once this is done, under gnuemacs if you type <#1210#>M-x gp<#1210#> (where as usual M is the Meta key, i.e. Escape, or on SUN keyboards, the Left key), a special shell will be started, which in particular launches GP with the default stack size, prime limit and input buffer size. If you type instead <#1211#>C-u M-x gp<#1211#>, you will be asked for the name of the GP executable, the stack size, the prime limit and the input buffer size before the execution of GP begins. If for any of these you simply type return, the default value will be used (on Unix machines it will be <#1212#>/usr/local/bin/gp<#1212#> for the executable, 4000000 for the stack, 500000 for the prime limit and 30000 for the buffer size).

You can then work as usual under GP, but with two notable advantages. First and foremost, you have at your disposal all the facilities of a text editor like emacs, in particular for correcting or copying blocks. Second, you can have an on-line help which is much more complete than what you obtain by typing <#1213#>?name<#1213#>. This is done by typing M-? (where M is the Meta key). In the minibuffer, emacs asks what function you want to describe, and after your reply you obtain the description which is in the users manual, including the description of functions (such as #math552#\, %) which use special symbols.

This help system can also be menu-driven, by using the command <#1214#>M-\c<#1214#> which opens a help menu window which enables you to choose the category of commands for which you want an explanation.

You also have at your disposal a few other commands. Read the file pari.txt for details.

Note that if for some reason the session crashes (due to a bug in your program or in the PARI system), you will usually stay under emacs, but the GP buffer will be killed. To recover it, simply type again M-x gp (or C-u M-x gp), and a new session of GP will be started after the old one, so you can recover what you have typed. Note that this will of course <#1215#>not<#1215#> work if for some reason you exited emacs before coming back (except for the C-z temporary stopping command).

by1=0 <#6629#><#6629#> <#6630#>=0<#6632#><#6632#><#6630#>

Chapter : Programming PARI in Library Mode

by1=0 <#6635#>=0<#6637#><#6637#><#6635#>



<#6636#><#6638#>. Introduction: initializations, universal objects, input and output.<#6638#><#6636#>


To be able to use PARI in <#6641#><#6641#>library mode, you must write a C program and link it to the PARI library and the PARI include files. See the installation guide (given in Appendix A) on how to create and install the PARI library and include files. A sample Makefile is also given in Appendix B.

Probably the best way to understand how programming is done is to work through a complete example. We will write such a program in section 4.3. Before doing this, a few explanations are in order.

by 1=0 <#6643#>=0<#6645#><#6645#><#6643#>

<#6644#><#6646#>.. Initializations and universal objects.<#6646#><#6644#>

First, one must explain to the outside world what kind of objects and programs we are going to use. This is done simply with the statement

<#1220#>#include ;SPMlt;genpari.h;SPMgt;<#1220#>

Note that this is usually a link, created when you make the library, to a file called either <#1221#>genpari68k.h<#1221#> (for 680x0-based machines with x at least 2) or <#1222#>genpariother.h<#1222#> (for all other machines).

This file <#1746#><#6650#><#6650#>genpari.h<#1746#>, first exports all the necessary constants, variables and functions, defines some important macros, and also defines the fundamental type for all PARI objects: the type <#1747#><#6652#><#6652#>GEN<#1747#>, which is simply a pointer to <#1225#>long<#1225#>.

<#1226#>Technical note<#1226#>: we would have liked to define a type GEN to be a pointer on itself. This unfortunately is not possible in C, except by using structures, but then the names become unwieldly. On the other hand, by a simple trick, it can be done in Pascal for example. The result of this is that when we will use a component of a PARI object, it will be a <#1227#>long<#1227#>, hence will need to be typecasted to a GEN again if we want to avoid warnings from the compiler. This will sometimes be quite tedious, but of course is trivially done.

To take an example, a polynomial P of degree 2 will be represented by a chunk of memory pointed to by the GEN P. P[0] and P[1] contain code information, in particular the type of the object, the degree of P, etc.... P[2], P[3], P[4] contain pointers to the coefficients of degree 0, 1, and 2 of P respectively (note the ascending order). This is where typecasting will be necessary: in principle P[i] (for i = 2, 3, 4) is a long, but we will want to use it as a GEN. The coefficients P[i] themselves are in chunks of memory whose complexity depends on the types of the coefficients, and so on.

Now we must state the most important law about programming in PARI, which must be respected if one wants to avoid disasters:

<#1748#>Apart from universal objects <#1228#>(see below)<#1228#> the chunks of memory used by a given PARI object must be in consecutive memory locations<#1748#>.

Don't panic: let's see the reason and the meaning of this, and how it can be achieved.

When doing large computations, unwanted intermediate results clutter up memory very fast so some kind of garbage collecting is needed. Most large systems do garbage collecting when the cluttering gets heavy, and this slows down the performance. In PARI we have taken a different approach: you must do your own cleaning up as soon as the intermediate results are not needed. Special purpose routines have been written to do this, but the primary requirement is exactly as stated above: a PARI object must be (essentially) connected. As a consequence of this explanation, one also sees that there is an evident exception to the above law: if your computation is small enough so that you don't need to do any garbage collecting, then just go ahead, PARI won't mind disconnected objects in most cases. However, since PARI routines do their own garbage collecting, watch carefully what you are doing.

The notion of <#6654#><#6654#>universal object alluded to above is quite simple: during the execution of your program, a number of objects will have been defined (by the system or by yourself) with the idea that they stay permanently with the same values. Examples are the integer 1, the fraction #math553##tex2html_wrap_inline19582#, the polynomial X, or a prime p which is used as a base modulus for integermods or p-adics. These universal objects are of course allowed to be disconnected from the other PARI objects.

After declaring the use of the file genpari.h, the first executable statement of a main program should be to initialize the PARI system, and in particular the PARI stack which will contain all the computations. This is simply done with a call to the two variable function <#1749#><#6659#><#6659#>init<#1749#>, like <#1231#>init(4000000,100000)<#1231#>. The first argument (here 4 million) is the number of bytes given to PARI to work with (it should not reasonably drop under 500000), and the second is the upper limit on a precomputed prime number table. If you don't want prime numbers, just put 2, but put an argument anyway because <#1232#>init()<#1232#> expects one.

We have now at our disposal:

• the following universal objects: the integer 0 (<#1750#><#6661#><#6661#>gzero<#1750#> as a GEN, <#1751#><#6663#><#6663#>zero<#1751#> as a long), the integer 1 (<#1752#><#6665#><#6665#>gun<#1752#> as a GEN, <#1753#><#6667#><#6667#>un<#1753#> as a long), the integer 2 (<#1754#><#6669#><#6669#>gdeux<#1754#> as a GEN, <#1755#><#6671#><#6671#>deux<#1755#> as a long), the fraction #math554##tex2html_wrap_inline19588# (<#1756#><#6676#><#6676#>ghalf<#1756#> as a GEN, <#1757#><#6678#><#6678#>lhalf<#1757#> as a long), the complex number i, <#1758#><#6680#><#6680#>gi<#1758#> as a GEN. In addition, space is reserved for the polynomials 1 (<#1759#><#6682#><#6682#>polun[]<#1759#> as a GEN, <#1760#><#6684#><#6684#>lpolun<#1760#> as a long), and the polynomials xv, (<#1761#><#6686#><#6686#>polx[]<#1761#> as GENs, <#1762#><#6688#><#6688#>lpolx<#1762#> as longs), where xv is the name of variable number v, where #math555#0≤v≤255. However, they are not created upon initialization, and it is the programmer's responsiblity to fill them before use. Since this is not very easy, we advise the user to use the function <#1763#><#6690#><#6690#>lisseq<#1763#> which has essentially the same effect as <#1764#><#6692#><#6692#>lisexpr<#1764#> except that it can execute a sequence of expressions and not only a single expression. For example, to prepare for use the variables a,b,c,x, write

lisseq(;SPMquot;x;a;b;c;SPMquot;)

Note that <#1249#>polun<#1249#> and <#1250#>polx<#1250#> are arrays, the index being the polynomial variable number.

• a large PARI <#6694#><#6694#>stack containing nothing but (in the present version) the 167 long words (668 bytes) of the predefined universal objects.

• a <#6696#><#6696#>heap which is dealt with in a different way from the stack, and will contain other permanent universal objects.

• a table of primes.

• access to all the built-in functions of the PARI library.

We have already described many of these functions in the preceding chapters. However some of them are specific to library mode and thus will be explained in this chapter.

by 1=0 <#6698#>=0<#6700#><#6700#><#6698#>

<#6699#><#6701#>.. Input and output.<#6701#><#6699#>

Two important aspects have not yet been explained since they are specific to library mode: input and output of PARI objects.

For <#6705#><#6705#>input, PARI provides you with two powerful high level functions which enables you to input your objects as if you were under GP. In fact, the second one <#1255#>is <#1255#> essentially the GP syntactical parser, hence you can use it not only for input but for any computation that you can do under GP. These functions are called <#1765#><#6707#><#6707#>lisexpr<#1765#> and <#1766#><#6709#><#6709#>lisseq<#1766#>. The first one has the following syntax:

<#1258#>GEN lisexpr(char* s);<#1258#>

Its effect is to analyze the input string s and to compute the result as in GP. However it is limited to one expression. If you want to read and evaluate a sequence of expressions, use

<#1259#>GEN lisseq(char* s);<#1259#>

Warning: there is a slight difference between these functions and the GP syntactical parser: the expressions and sequences which you use must not contain any spaces.

Once in a while, it may be useful to have the evaluation of the string involving a call to a function you have defined in C. The function <#1767#><#6711#><#6711#>install<#1767#> allows you to give a name to a function taking 0, 1, 2 or 3 GEN arguments and returning a single GEN. The syntax is

<#1261#>void install(GEN (*f)(), char *name, int valence)<#1261#>

where <#1262#>f<#1262#> is the (address of) the function, <#1263#>name<#1263#> its new name, and <#1264#>valence<#1264#> the number of its arguments, an integer between 0 and 3.

For <#6713#><#6713#>output, there exist four different functions. First, you can use the function <#1768#><#6715#><#6715#>sor<#1768#> with the following syntax:

<#1267#>void sor(GEN x,char format,long dec,long field);<#1267#>

Here format is either <#1268#>'e'<#1268#>, <#1269#>'f'<#1269#> or <#1270#>'g'<#1270#> corresponding to the three output formats of GP, <#1271#>dec<#1271#> is the number of printed significant digits for real numbers, and should be put equal to -1 if all of them are wanted, and <#1272#>field<#1272#> corresponds to the field width of GP used for printing integers.

A default use of this function is to use the macro <#1769#><#6717#><#6717#>outbeaut(GEN x)<#1769#> which is equivalent to <#1274#>sor(x,'g',-1,0)<#1274#>.

The second format corresponds to the ``raw'' format of GP (see section 2.2.5) and is obtained by using the function <#1770#><#6719#><#6719#>brute<#1770#> with the following syntax:

<#1276#>void brute(GEN x,char format,long dec);<#1276#>

A default use of this function is to use the macro <#1771#><#6721#><#6721#>output(GEN x)<#1771#> which is equivalent to <#1278#>brute(x,'g',-1)<#1278#>.

The third format corresponds to the <#1279#>texprint<#1279#> function of GP, and gives a TEX<#1280#><#1280#> output of the result. It is obtained by using the function <#1281#>texe<#1281#> with the following syntax:

<#1772#>void <#6723#><#6723#>texe(GEN x,char format,long dec);<#1772#>

Finally, you can use the <#6725#><#6725#>hexadecimal tree output corresponding to the GP command #math556#\x using the function <#1773#><#6727#><#6727#>voir<#1773#> with the following syntax:

<#1285#>void voir(GEN x,-1);<#1285#>

Again the last parameter must be given and put equal to -1. In principle this last type of output is used only for debugging purposes.

by1=0 <#6729#>=0<#6731#><#6731#><#6729#>



<#6730#><#6732#>. Creation, destruction, and implementation of the PARI objects.<#6732#><#6730#>


By far the most important functions which are specific to the library mode are the functions which allow the programmer to create and delete PARI objects, and the assignment statements.

by 1=0 <#6735#>=0<#6737#><#6737#><#6735#>

<#6736#><#6738#>.. Creation of PARI objects.<#6738#><#6736#><#6742#><#6742#> The basic function which creates a PARI object is the function <#1774#><#6744#><#6744#>cgetg<#1774#> whose syntax is:

<#1290#>cgetg(long length,long type);<#1290#>

Here length is a long specifying the number of longwords to be allocated to the object, and type is a long which is the type number of the object (see chapter 1 or below for the list of these type numbers). Note that the length is the first argument, and type the second. The precise effect of this function is as follows: it first creates on the stack a chunk of memory of size ``length'' longwords, and puts the address of the chunk as the returned value. If not enough memory is available, a message to the effect that the PARI stack overflows will be printed. Otherwise, it sets the type of the chunk to ``type'', sets the length, and sets the reference count to 1. In effect, it fills correctly and completely the first codeword (z[0] or *z) of the PARI object. Many PARI objects having also a second codeword (types 1,2,7,10,11), <#1291#>it is the programmer's responsibility to fill this second codeword<#1291#>, either explicitly (see below), or implicitly using an assignment statement.

Note that the argument ``length'' is forced for a number of types: 3 for types 3,4,5,6,9,13,14 and 16, 4 for type 8 (Quad), and 5 for type 7 (p-adic). However for efficiency's sake no checking is made in the function <#1292#>cgetg<#1292#> so disastrous results can occur if you give an incorrect length.

Notes: 1) the creation of leaves, i.e. integers or reals, being so common, <#1775#><#6746#><#6746#>cgeti<#1775#> and <#1776#><#6748#><#6748#>cgetr<#1776#> should be used instead of <#1295#>cgetg( ,1)<#1295#> and <#1296#>cgetg( ,2)<#1296#>.

2) the macros <#1777#><#6750#><#6750#>lgetg<#1777#>, <#1778#><#6752#><#6752#>lgeti<#1778#> and <#1779#><#6754#><#6754#>lgetr<#1779#> are predefined as <#1300#>(long)cgetg<#1300#>, <#1301#>(long)cgeti<#1301#> and <#1302#>(long)cgetr<#1302#> respectively.

Examples:

<#1303#>z = cgeti(6);<#1303#> (or <#1304#>cgetg(6, 1);<#1304#>) creates an integer type which can hold numbers of absolute value less than 2128 (2 codewords + 4 mantissa longwords).

<#1306#>z = cgetg(3, 6); z[1] = lgetr(5); z[2] = lgetr(5);<#1306#> creates a complex type whose real and imaginary part can hold real numbers of precision 96 bits.

<#1307#>z = cgetg(4, 19); for(i=1; i;SPMlt;4; i++) z[i] = lgetg(5, 18);<#1307#> creates a matrix type for 4×3 matrices. One must also create space for the matrix elements themselves using a double loop.

These last two examples illustrates the fact that since PARI types are recursive, all the branches of the tree must be created. The function <#1308#>cgetg<#1308#> creates only the ``root'', and other calls to <#1309#>cgetg<#1309#> must be made to get the whole tree. For matrices, a common mistake is to think that <#1310#>z = cgetg(4, 19)<#1310#> (for example) will create the root of the matrix: one needs also to create the column vectors of the matrix. This is because a matrix is really nothing else than a line vector of column vectors (hence a priori not a basic type), but it has been given a special type number so that operations with matrices become possible.

by 1=0 <#6756#>=0<#6758#><#6758#><#6756#>

<#6757#><#6759#>.. Implementation of the PARI types<#6759#><#6757#>.

Although it is a little tedious, we must go through each type and explain their implementation. Let z be a PARI object. Common to all the types is the first codeword (z[0]), which we don't have to worry about since this is taken care of by <#1312#>cgetg<#1312#>. However we need its precise description: the first byte is the <#6763#><#6763#>type number, the second byte is the <#6765#><#6765#>reference count (used only for garbage collecting purposes), and the last two bytes (the low order word if you prefer) is the <#6767#><#6767#>length of the root in longwords. For instance in the example <#1316#>z = cgeti(6)<#1316#> above, z[0] will be set to <#1317#>0x01010006<#1317#>.

These bytes can be handled through the following functions:

<#1780#>long <#6769#><#6769#>typ(GEN z);<#1780#> returns the type number of z.

<#1781#>void <#6771#><#6771#>settyp(GEN z, long n);<#1781#> sets equal to <#1320#>n<#1320#> the type number of z (you should not have to use this function if you use cgetg).

<#1782#>long <#6773#><#6773#>pere(GEN z);<#1782#> returns the reference count of z.

<#1783#>void <#6775#><#6775#>setpere(GEN z, long n);<#1783#> sets equal to <#1323#>n<#1323#> the reference count of z.

<#1784#>void <#6777#><#6777#>incpere(GEN z);<#1784#> increments the reference count of z (with saturation at 255).

<#1785#>long <#6779#><#6779#>lg(GEN z);<#1785#> returns the length (in longwords) of the root of z.

<#1786#>long <#6781#><#6781#>setlg(GEN z, long l);<#1786#> sets equal to <#1327#>l<#1327#> the length of z (you should not have to use this function if you use <#1328#>cgetg<#1328#>; however, see an example of the use of this function in the matexp function given in section 4.3).

Let us now look precisely at the types:

<#1329#>Type 1 (integers)<#1329#>: <#6783#><#6783#> this type has a second codeword z[1] which contains the following information. The first byte is the sign of z, i.e. 1 if z ;SPMgt; 0, 0 if z = 0, -1 if z ;SPMlt; 0. The second byte is unused. The low order word is the <#1331#>effective length<#1331#> of z, i.e. the total number of significant longwords. This means the following: apart from the integer 0 (whose second codeword is equal to 2), every integer is ``normalized'', meaning that the first mantissa longword (i.e. z[2]) is non zero. However, the integer may have been created with a longer length. Hence the ``length'' which is in z[0] can be larger than the ``effective length'' which is in z[1]. In fact, it would be a disaster to try to access z[i] for i larger than or equal to the effective length.

This information can be handled using the following functions:

<#1787#>long <#6785#><#6785#>signe(GEN z);<#1787#> returns the sign of z.

<#1788#>void <#6787#><#6787#>setsigne(GEN z, long s);<#1788#> sets equal to <#1334#>s<#1334#> the sign of z.

<#1789#>long <#6789#><#6789#>lgef(GEN z);<#1789#> returns the <#6791#><#6791#>effective length of z.

<#1790#>void <#6793#><#6793#>setlgef(GEN z, long l);<#1790#> sets equal to <#1338#>l<#1338#> the effective length of z.

The integer 0 can be characterized either by its sign equal to 0, or by its effective length equal to 2. Apart from z = 0, z[2] exists and is non zero, and the absolute value of z is (z[2],z[3],...,z[lgef(z)-1]) in base 232, where as usual in this notation z[2] is the high order longword.

<#1340#>Type 2 (real numbers)<#1340#>: <#6795#><#6795#> this type has a second codeword z[1] whose first byte is also the sign (obtained or set using the same functions as for the integers), but whose 3 low order bytes contains a biased binary exponent (i.e. the exponent plus 223). This exponent can be handled using the following functions:

<#1791#>long <#6797#><#6797#>expo(GEN z);<#1791#> returns the true (unbiased) exponent of z. This is defined even when z is equal to zero, see section 1.2.6.

Note also the function <#1792#>long <#6799#><#6799#>gexpo(GEN z)<#1792#> which tries to return an exponent for z even if z is not a real number.

<#1793#>void <#6801#><#6801#>setexpo(GEN z, long e);<#1793#> sets equal to <#1346#>e<#1346#> the exponent of z, of course after adding the bias.

The real zero is characterized by having its sign equal to 0. However, usually the first mantissa word z[2] is defined and equal to 0. This fact must <#1347#>never<#1347#> be used to characterize 0. If z is not equal to 0, the first mantissa word z[2] is normalized, i.e. its first bit is 1. The mantissa is (z[2],z[3],...,z[lg(z]-1]) in base 232 where z[2] is the most significant longword, and the mantissa is understood to be between 1 and 2. Thus the real number -3.5 is represented (if the length is 4) as <#1349#>0x02010004, 0xff800001, 0xe0000000, 0<#1349#>.

<#1350#>Type 3 (integermods)<#1350#>: <#6803#><#6803#> z[1] points to the modulus, and z[2] on the number representing the class z. Both must be of type integer. In principle z[1] ;SPMgt; 0 and 0 ;SPMlt; = z[2] ;SPMlt; z[1], but this rule does not have to be strictly obeyed by the user. Any integermod obtained after a PARI operation will in principle satisfy these conditions.

<#1352#>Types 4 and 5 (rational numbers)<#1352#>: <#6805#><#6805#> z[1] points to the numerator, and z[2] on the denominator. Both must be of type integer. In principle z[2] ;SPMgt; 0, but this rule does not have to be strictly obeyed either.

<#1354#>Type 6 (complex numbers)<#1354#>: <#6807#><#6807#>z[1] points on the real part, and z[2] on the imaginary part. A priori z[1] and z[2] can be of any type, but only certain types are useful and make sense.

<#1356#>Type 7 (p-adic numbers)<#1356#>: <#6809#><#6809#> this type has a second codeword z[1] which contains the following information: the first <#1358#>word<#1358#> contains the exponent of p modulo which the p-adic unit corresponding to z is defined (if z is not 0), i.e. one less than the number of significant p-adic digits. the second word contains the biased exponent of z (the bias being here equal to 215). This information can be handled using the following functions:

<#1794#>long <#6811#><#6811#>precp(GEN z);<#1794#> returns the p-adic precision of z, i.e. one less than the number of significant p-adic digits.

<#1795#>void <#6813#><#6813#>setprecp(GEN z, long l);<#1795#> sets equal to <#1362#>l<#1362#> the p-adic precision of z.

<#1796#>long <#6815#><#6815#>valp(GEN z);<#1796#> returns the p-adic valuation of z (i.e. the unbiased exponent). This is defined even if z is equal to 0, see section 1.2.6.

<#1797#>void <#6817#><#6817#>setvalp(GEN z, long e);<#1797#> sets equal to <#1365#>e<#1365#> the p-adic valuation of z.

In addition to this codeword, z[2] points to the prime p, z[3] points to #math557#pprecp(z), and z[4] points to an integer reprenting the p-adic unit associated to z modulo z[3] (or points to zero if z is zero). To summarize, we have the equality:

#math558#

z = pvalp(z)*(z[4] + O(z[3])) = pvalp(z)*(z[4] + O(pprecp(z)))

<#1370#>Type 8 (quadratic numbers)<#1370#>: <#6823#><#6823#> z[1] points on the polynomial defining the quadratic field, z[2] on the ``real part'' and z[3] on the ``imaginary part'', where this is to be taken as the coefficients of z on the ``canonical'' basis (1,w) (see section 1.2.3). Complex numbers are particular cases of quadratics but deserve a separate type.

<#1372#>Type 9 (polymods)<#1372#>: <#6825#><#6825#> exactly as for integermods, z[1] points on the modulus, and z[2] on a polynomial representing the class of z. Both must be of type polynomial. However, one must obey the rules explained in chapter 2 concerning the hierarchical structure of the variables of a polymod.

<#1374#>Type 10 (polynomials)<#1374#>: <#6827#><#6827#> this type has a second codeword which is analogous to the one for integers. The first byte is the ``sign'', i.e. 0 if the polynomial is equal to 0, and 1 if not (see however the important remark below). The second byte is the variable number (e.g. 0 for X, 1 for Y, etc...). This number can be handled with the following functions:

<#1802#>long <#6829#><#6829#>varn(GEN z);<#1802#> returns the variable number of the object z.

Note also the function <#1803#>long <#6831#><#6831#>gvar(GEN z)<#1803#> which tries to return a variable number for z, even if z is not a polynomial or power series. The variable number of a scalar type is set by definition equal to 32767.

<#1804#>void <#6833#><#6833#>setvarn(GEN z,long v);<#1804#> sets equal to <#1379#>v<#1379#> the variable number of z.

The least significant word of the second codeword is the effective length of the polynomial. Note that the degree of the polynomial is equal to the effective length minus three. The functions used to handle this codeword are the same as for integers. The components z[2], z[3],...z[lgef(z)-1] point to the coefficients of the polynomial <#1380#>in ascending order<#1380#>, with z[2] being the constant term and so on.

<#1381#>Important remark<#1381#>. A zero polynomial can be characterized by the fact that its sign is 0. However, its effective length may be equal to 2, or greater than 2. If it is greater than 2, this means that all the coefficients of the polynomial are equal to zero (as they should for a zero polynomial), but not all of these zeros are exact zeros, and more precisely the leading term z[lgef(z)-1] is not an exact zero.

<#1382#>Type 11 (power series)<#1382#>: <#6835#><#6835#> This type also has a second codeword. Its first byte is the ``sign'', i.e. 0 if the power series is 0, and 1 if not. The second byte is the variable number, as for polynomials. The last two bytes code for a biased exponent with a bias of 215. This information can be handled with the following functions:

<#1805#><#6837#><#6837#>signe, <#6839#><#6839#>setsigne, <#6841#><#6841#>varn, <#6843#><#6843#>setvarn<#1805#> as for polynomials, and <#1806#><#6845#><#6845#>valp, <#6847#><#6847#>setvalp<#1806#> for the exponent. Beware not to use <#1391#>expo<#1391#> and <#1392#>setexpo<#1392#> for power series.

If the power series is non zero, z[2], z[3],...z[lg(z)-1] point to the coefficients of z in ascending order, z[2] being the first non-zero coefficient. Note that the exponent of a power series can be negative, i.e. we can deal with Laurent series with a finite number of negative terms.

<#1393#>Types 13 and 14 (rational functions)<#1393#>: <#6849#><#6849#> z[1] points on the numerator, and z[2] on the denominator. Both must be of type polynomial.

<#1395#>Type 16 (binary quadratic forms)<#1395#>: <#6851#><#6851#> z[1], z[2], z[3] point on the three coefficients of the form. All three should be of type integer.

<#1397#>Types 17 and 18 (vectors)<#1397#>: <#6853#><#6853#><#6855#><#6855#> z[1], z[2],...z[lg(z)-1] point on the components of the vector.

<#1400#>Type 19 (matrices)<#1400#>: <#6857#><#6857#> z[1], z[2],...z[lg(z)-1] point on the column vectors of z, i.e. they must be of type 18 and of the same length.

by 1=0 <#6859#>=0<#6861#><#6861#><#6859#>

<#6860#><#6862#>.. Assignment and copying of PARI objects<#6862#><#6860#>.

The general <#6866#><#6866#>assignment function is the function <#1807#><#6868#><#6868#>gaffect<#1807#> with the following syntax:

<#1405#>void gaffect(GEN x, GEN y);<#1405#>

Its effect is to assign the PARI object x into the (preexisting) object y. Many conditions must be met for the assignment to be possible. For instance it is allowed to assign an integer into a real number, but the converse is forbidden. For that, you must use the truncation or rounding function of your choice (see section 3.2). It can also happen that y is not large enough or does not have the proper tree structure to receive the object x. As an extreme example, assume y is the zero integer with length equal to 2. Then all assignments of a non zero integer into y will result in an error message since y is not large enough to fit a non zero integer. In general common sense will tell you what is possible, keeping in mind the PARI philosophy which says that if it makes sense it is legal (the assignment of an imprecise object into a precise one does <#1406#>not<#1406#> make sense. However, a change in precision of imprecise objects is allowed).

It is also necessary to assign ordinary 32-bit long numbers of C into a PARI object. This is done using the function <#1808#><#6870#><#6870#>gaffsg<#1808#> with the following syntax:

<#1408#>void gaffsg(long s, GEN y);<#1408#>

It is also very useful to copy a PARI object, not just by moving around a pointer, but by physically creating a copy of the whole tree structure. The function which does this is called <#1809#><#6872#><#6872#>gcopy<#1809#>, with the predefined macro <#1810#><#6874#><#6874#>lcopy<#1810#> as a synonym for <#1411#>(long)gcopy<#1411#>. Its syntax is:

<#1412#>GEN gcopy(GEN x);<#1412#>

and the effect is to create a new copy of x on the PARI stack.

However, it may also be useful to create a <#1413#>permanent<#1413#> copy of a PARI object. This will not be created on the stack but on the heap. The function which does this is called <#1811#><#6876#><#6876#>gclone<#1811#>, with the predefined macro <#1812#><#6878#><#6878#>lclone<#1812#> as a synonym for <#1416#>(long)gclone<#1416#>. Its syntax is:

<#1417#>GEN gclone(GEN x);<#1417#>

A final class of functions should be mentioned in this subsection: functions which convert from C objects to PARI objects (with creation of the objects on the stack). They are as follows:

<#1813#>GEN <#6880#><#6880#>stoi(long s);

GEN <#6882#><#6882#>dbltor(double s);<#1813#>

Their meaning is clear from their names. The converse functions

<#1814#>long <#6884#><#6884#>gtolong(GEN z);

double <#6886#><#6886#>gtodouble(GEN z);<#1814#>

also exist, but are seldom used.

by 1=0 <#6888#>=0<#6890#><#6890#><#6888#>

<#6889#><#6891#>.. Destruction of PARI objects and garbage collection<#6891#><#6889#>.

If you want to destroy (i.e. give back the memory occupied by) the latest PARI object on the stack (e.g. the latest one obtained by a cgetg or a function), this is very simple: just use the function <#1815#><#6895#><#6895#>cgiv<#1815#> with the syntax:<#6897#><#6897#>

<#1425#>void cgiv(z);<#1425#>

where z is the object (which can be a tree), which you want to give back. Unfortunately life is not so simple, and in general you are doing a computation and want to give back accumulated garbage. A simple example is the following.

Let x and y be two preexisting PARI objects and suppose that we want to compute x*x+y*y. This can trivially be done using the following program (we skip the necessary declarations):

<#1426#>p1=gmul(x,x);p2=gmul(y,y);z=gadd(p1,p2);<#1426#>

The GEN z will indeed point on the PARI object equal to x*x+y*y. However, consider the stack. It contains as unnecessary garbage p1 and p2. More precisely it contains (in that order) z, p1, p2. We need a way to get rid of this garbage (in this case it causes no harm except that it occupies memory space, but in other cases it could disconnect PARI objects and this is forbidden). It would not have been possible to get rid of p1, p2 before since they are used in the final operation. It is not possible to use the function <#1427#>cgiv<#1427#> since p1 and p2 are not at the bottom of the stack. Hence PARI provides us with a much more powerful tool whose correct handling is not always easy: the function <#1816#><#6899#><#6899#>gerepile<#1816#> (with the macro <#1817#><#6901#><#6901#>lpile<#1817#> as a synonym to <#1430#>(long)gerepile<#1430#>). This function has the following syntax:

<#1431#>GEN gerepile(long ltop, long lbot, GEN z);<#1431#>

The understanding of the behavior of this function is certainly the most difficult part of this chapter, hence we will try to go slowly in the explanation.

First, the PARI stack has a current stack pointer which is a global variable (hence must not be declared in a program) called <#1818#><#6903#><#6903#>avma<#1818#> (which stands for <#1433#>av<#1433#>ailable <#1434#>m<#1434#>emory <#1435#>a<#1435#>ddress, which as its name indicates points always just after the first free address on the stack (remember that the stack grows from high to low addresses). For certain reasons this variable is of type long and not of type GEN as would be natural.

Now let us see the effects of the function <#1436#>gerepile<#1436#>. For this function to work one must have lbot ;SPMlt; ltop. As a first approximation, we describe the effects of this function as follows. Give back the memory locations between lbot and ltop, and move the object z upwards so that no space is lost. Specifically, we rewrite our previous example as follows:

<#1819#><#1437#><#1437#> ltop=avma; /* keep in mind the current address of the top of the stack */ p1=gmul(x,x); p2=gmul(y,y); lbot=avma; /* keep the address of the bottom of the garbage pile */ z=gadd(p1,p2); z=gerepile(ltop,lbot,z); /* do the garbage collecting */<#1819#>

The last two instructions could also have been written more simply:

<#1438#>z=gerepile(ltop,lbot,gadd(p1,p2));<#1438#>

As you can see, in simple conditions the use of <#1439#>gerepile<#1439#> is not really difficult. However it is absolutely necessary to understand what has happened. When entering an instruction such as <#1440#>gerepile(ltop,lbot,z)<#1440#>, we must have avma ;SPMlt; = lbot ;SPMlt; ltop. As we will see, the variable z is in fact used very little. The function then considers all the PARI objects between avma and lbot (i.e. the ones that we want to keep) and looks at every component of such an object which is not a codeword. This component is a pointer on an object whose address is either between avma and lbot, in which case it will be suitably updated, larger than or equal to ltop, in which case it will not change, or between lbot and ltop in which case <#1441#>gerepile<#1441#> will scream an error message at you. If all goes well, the pointers are suitably updated, the chunk of memory from avma to lbot-1 is physically copied to addresses avma+ltop-lbot to ltop-1, avma is updated (to the value avma+ltop-lbot), and finally the displacement ltop-lbot is added to z and given as a result of the function. In addition, if z is not 0 (in the sense of being address 0), the PARI object pointed to by ltop is checked to see whether certain of its pointers need also to be updated. This is a horrible hack which should not be used.

<#1442#>Important remark<#1442#>: as we will see presently it is often necessary to do several <#1443#>gerepile<#1443#> in a computation. However, the least the better. The only condition for <#1444#>gerepile<#1444#> to work is that the garbage be connected. If the computation can be arranged so that there is a minimal number of connected pieces of garbage then it should be done.

For example suppose we want to write a function of two GEN variables x and y which creates the vector [x*x+y,y*y+x]. Without garbage collecting, one would write:

<#1820#><#1445#><#1445#> p1=gmul(x,x);p2=gadd(p1,y);p3=gmul(y,y);p4=gadd(p3,x); z=cgetg(3,17);z[1]=(long)p2;z[2]=(long)p4;<#1820#>

This leaves a dirty stack with (in that order) z,p4,p3,p2,p1 and p1 and p3 being garbage. But if we compute p3 <#1446#>before <#1446#> p2 then the garbage becomes connected and we get the following program with garbage collecting:

<#1821#><#1447#><#1447#> ltop=avma;p1=gmul(x,x);p3=gmul(y,y);lbot=avma; z=cgetg(3,17);z[1]=ladd(p1,y);z[2]=ladd(p3,x); z=gerepile(ltop,lbot,z);<#1821#>

We take our next example directly from the implementation of <#1448#>gmul<#1448#> in the file <#1449#>gen2.c<#1449#>, case 6 times case 6. In other words we want to write a program to compute the product of two complex numbers x and y, using a method which takes only 3 multiplications instead of 4. Let z=x*y, and set x=xr+i*xi and similarly for y and z. The well known trick is to compute p1=xr*yr, p2=xi*yi, p3=(xr+xi)*(yr+yi), and then we have zr=p1-p2, zi=p3-p1-p2. The program is essentially as follows:

<#1822#><#1450#><#1450#> ltop=avma;p1=gmul(x[1],y[1]);p2=gmul(x[2],y[2]); p3=gmul(gadd(x[1],x[2]),gadd(y[1],y[2]));p4=gadd(p1,p2);lbot=avma; z=cgetg(3,6);z[1]=lsub(p1,p2);z[2]=lsub(p3,p4); z=gerepile(ltop,lbot,z);<#1822#>

Let us now look at a less trivial example where more than one <#1451#>gerepile<#1451#> is needed in practice (in theory one can always use only one, see below, but generally at the expense of efficiency). Suppose for instance that we want to write a function which multiplies a line vector by a matrix (such a function is of course already part of <#1452#>gmul<#1452#>, but let's ignore this for a moment). Then the most natural way is to do a <#1453#>cgetg<#1453#> of the result immediately, and then for each coefficient of the result vector to do a <#1454#>gerepile<#1454#> to get rid of the garbage which has accumulated for that particular coefficient. We leave the details to the reader, who can look at the answer in the file gen2.c, in the function gmul, case 17 times case 19. As was said above, it would theoretically be possible to have a single connected piece of garbage, but it would be a much less natural and unecessarily complicated program.

Finally, let us take an example which is probably the least trivial way of using <#1455#>gerepile<#1455#>, but is unfortunately sometimes necessary. Although it it not an infrequent occurence, we will not give a specific example but a general one. Suppose that we want to do a computation (usually inside a larger function) giving more than one PARI object as a result, say two for instance. Then even if we set up the work properly, before cleaning up we will have a stack which has the desired results z1, z2 (say), and then connected garbage from lbot to ltop. If we write

<#1456#>z1=gerepile(ltop,lbot,z1);<#1456#>

then the stack will be cleaned, the pointers OK, but we will have lost the address of z2. Hence the best way in this case is to write the following, assuming that <#1457#>dec<#1457#> has been declared as a long and not as a GEN:

<#1458#>dec=lpile(ltop,lbot,0)/4;z1+=dec;z2+=dec;<#1458#>

This works because <#1459#>gerepile<#1459#> thinks that 0 is a valid address, hence puts in <#1460#>dec<#1460#> the displacement in bytes. Since z1 and z2 are pointers, we need to divide <#1461#>dec<#1461#> by 4 to get a longword displacement.

If you followed us this far, congratulations, and rejoice, the rest is much easier.

by 1=0 <#6905#>=0<#6907#><#6907#><#6905#>

<#6906#><#6908#>.. Some tricks and hints<#6908#><#6906#>. Even for the authors, the use of <#1463#>gerepile<#1463#> was not evident at first. Hence we give some indications on how to avoid most problems connected with <#1464#>gerepile<#1464#>, at the expense of speed.

First, although it looks complicated, <#1465#>gerepile<#1465#> has turned out to be a very flexible and fast garbage collector, which can compare very favorably with much more sophisticated methods used in other systems. A few tests that we have made indicate that the price paid for using <#1466#>gerepile<#1466#> is never more than 10 percent, and usually around 5 or 6 percent of the total time, which is quite acceptable.

Second, in many cases, in particular when the tree structure and the size of the PARI objects which will appear in a computation are under control, one can avoid <#1467#>gerepile<#1467#> altogether by creating sufficiently large objects at the beginning (using <#1468#>cgetg<#1468#>), and then using assignment statements and operations ending with z (such as <#1469#>gaddz<#1469#>). Coming back to our first example, note that if we know that x and y are of type real and of length less than or equal to 5, we can program without using <#1470#>gerepile<#1470#> at all:

<#1471#>z=cgetr(5);ltop=avma;p1=gmul(x,x);p2=gmul(y,y);gaddz(p1,p2,z);avma=ltop;<#1471#>

Here the cleaning up is done using the simple assignment avma=ltop which takes essentially no time at all.

Third, the philosophy of <#1472#>gerepile<#1472#> is the following: keep the value of the stack pointer avma at the beginning, and just <#1473#>before<#1473#> the last operation. Afterwards, it would be too late since the garbage address would be lost.

Finally, if everything seems hopeless, at the expense of speed you can do the following: after keeping in ltop the value of avma, perform your computation as you wish, in any order, leaving a messy stack. Let z be your result (let's assume you have only one. If you have more than one, you can always create a vector which is a single PARI object whose components are your results; after all, we are already cheating, we can cheat some more). Then write the following:

<#1474#>lbot=avma;z=gerepile(ltop,lbot,gcopy(z));<#1474#>

The trick is to force a copy of z at the bottom of the stack, hence all the rest including the initial z becomes connected garbage. Note that this may not work in some cases where you have created new universal objects between lbot and ltop, for instance moduli for mod p or p-adic computations. In that case you must also copy these objects on top of the stack and modify the components of your results which point to these objects. This in fact involves rewriting a simplified version of a part of <#1475#>gerepile<#1475#>, hence is not recommended. If you are in this situation and still want to use this trick, we strongly advise you to create all your new universal objects before starting your computation, hence before the instruction <#1476#>ltop=avma;<#1476#>. Then you should have no problems.

Another solution is to use clones, i.e. the function <#1477#>gclone<#1477#> (see 4.2.3), which creates a permanent object on the heap, and not on the stack.

by1=0 <#6912#>=0<#6914#><#6914#><#6912#>



<#6913#><#6915#>. A complete program.<#6915#><#6913#>


Now that the preliminaries are out of the way, the best way to learn how to use the library mode is to work through a detailed nontrivial example of a main program. We will write a program which computes the exponential of a square matrix x. The complete listing is given in Appendix C, but each part of the program will be written and commented here. We will use an algorithm which is not optimal but is not far from the one used for the PARI function <#1479#>gexp<#1479#> (in fact in the function <#1480#>mpexp1<#1480#>). This consists in calculating the sum of the series:

#math559#

ex/(2n) = #tex2html_wrap_indisplay19676##tex2html_wrap_indisplay19677#

for a suitable positive integer n, and then computing ex by repeated squarings. First, we will need to compute the L2-norm of the matrix x, i.e. the quantity:

#math560#

z = | x|2 = #tex2html_wrap_indisplay19683#.

We will then choose the integer n such that the L2-norm of x/(2n) is less than or equal to 1, i.e.

#math561#

n = #tex2html_wrap_indisplay19688##tex2html_wrap_indisplay19689##tex2html_wrap_indisplay19690#

if z ;SPMgt; = 1. Then the convergence of the series will be at least as good as the one for e1, and will be easy to estimate. In fact a larger value of n would be preferable, but this is slightly machine dependent and more complicated, and will be left to the reader.

We will now start writing our program. So as to be able to use it in other contexts, we will structure it in the following way: a main program which will do the input and output, and a function which we shall call <#1489#>matexp<#1489#> which does the real work. The main program is easy to write. It can be something like this:

<#1824#><#1490#><#1490#> #include ;SPMlt;genpari.h;SPMgt; GEN matexp(); <#1491#><#1491#> main() { ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;GEN x,y; ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;long prec,d; ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;char s[512]; <#1492#><#1492#> ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;init(1000000,2); /* take a million bytes of memory for the stack */ ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;printf(;SPMquot;precision of the computation in decimal digits? ;SPMquot;); ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;scanf(;SPMquot;d;SPMquot;,d);if(d;SPMlt;0) prec=3;else prec=d*K1+3; ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;printf(;SPMquot;input your matrix in GP format:#math562#\n;SPMquot;); ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;s[0]=0;while(!s[0]) gets(s);x=lisexpr(s); ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;y=matexp(x,prec); ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;sor(y,'g',d,0); }<#1824#>

The variable <#1493#>prec<#1493#> represents the length in longwords of the real numbers used. K1 is the constant (defined in <#1494#>gencom.h<#1494#>) equal to #math563#ln(10)/(32*ln(2)), which allows us to convert from a number of decimal digits to a number of longwords. The function <#1495#>lisexpr<#1495#> was mentioned earlier and avoids any trouble in the input. In fact, as was also mentioned, <#1496#>lisexpr<#1496#> can take any legal GP expression hence can do not only input but also computations. Note that we have used the construction <#1497#>s[0]=0;while(!s[0]) gets(s);<#1497#> to get our string, instead of <#1498#>scanf<#1498#> which would make trouble in this case.

Finally, <#1499#>sor<#1499#> is the general output routine. We have chosen to give d significant digits since this is what was asked. Note that there is a trick hidden here: if d was given to be negative, then the computation will be done in precision 3 (i.e. about 9.7 decimal digits) and in the function <#1500#>sor<#1500#>, giving a negative third argument outputs all the significant digits, hence nothing is wrong.

Now let us attack the main course, the function matexp:

<#1825#><#1501#><#1501#> GEN matexp(x,prec) ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;GEN x; ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;long prec; <#1502#><#1502#> { ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;GEN y,r,s,p1,p2; ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;long tx=typ(x),lx=lg(x),i,k,n,lbot,ltop; <#1503#><#1503#> /* check that x is a square matrix */ <#1504#><#1504#> ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;if(tx!=19) {printf(;SPMquot;This expression is not a matrix#math564#\n;SPMquot;);return(gzero);} ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;if(lx!=lg(x[1])) {printf(;SPMquot;This matrix is not square#math565#\n;SPMquot;);return(gzero);} <#1505#><#1505#> /* compute the L2 norm of x */ <#1506#><#1506#> ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;ltop=avma;s=gzero; ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;for(i=1;i;SPMlt;lx;i++) s=gadd(s,gnorml2(x[i])); ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;if(typ(s)==2) setlg(s,3); ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;s=gsqrt(s,3); /* we do not need much precision on s */ <#1507#><#1507#> /* if s;SPMlt;1, we are happy */ <#1508#><#1508#> ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;if(expo(s);SPMlt;0) {n=0;p1=x;} ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;else {n=expo(s)+1;p1=gmul2n(x,-n);s=gmul2n(s,-n);}<#1825#>

Before continuing, a few remarks are in order. First, after printing an error message we need to return a GEN value otherwise a fussy compiler will complain. Hence we return gzero, which in any case is an impossible result for an exponential.

Second, to compute the square of the L2-norm of x we just add the squares of the L2-norms of the column vectors which we obtain using the library function <#1509#>gnorml2<#1509#>. Had this function not existed, it would of course have been just as easy, but we would have had to make a double loop. Then, we take the square root of s, in precision 3. For that we use the function <#1510#>setlg<#1510#> which tells s to be in such a precision, only if s is of type real. Note that the matrix x is allowed to have complex entries, but the function <#1511#>gnorml2<#1511#> guarantees that s is a nonnegative number of type real. If we had not known this fact, we would simply have added the instruction <#1512#>s = greal(s);<#1512#> just after the <#1513#>for<#1513#> loop.

Third, before starting this computation which will produce garbage on the stack, we have carefully kept the value of the stack pointer avma in ltop. Note that we are going to assume throughout that the garbage does not overflow the memory that we allocated on the stack. If it did, we would have two solutions. Either allocate more memory in the main program (for instance change 1000000 into 2000000), or do some <#1514#>gerepile<#1514#> along the way.

Fourth, note that we initialized the sum <#1515#>s<#1515#> to gzero, which is an exact zero. This is logical, but has some disadvantages: if all the entries of the matrix are integers (or rational numbers), the computation will be rather long, about twice as long as with real numbers of the same length. It would be better to initialize <#1516#>s<#1516#> to a real zero, using for instance the instructions:

<#1517#>s=cgetr(prec+1);gaffsg(0,s);<#1517#>.

However, this would not make much sense. In fact you should avoid making an assignment of an exact zero (essentially the integer zero) into a real number: which real zero is it going to give as a result? In fact a choice has been made, and it will give you the zero with exponent equal to -32 times the number of longwords in the mantissa, but this is not really satisfactory. Perhaps PARI should give an error message in that case, or at least a warning.

Fifth, we will want to look at the approximate size of real numbers, and the fastest way to do this is to look at their binary exponents. Hence we need to have <#1518#>s<#1518#> as a real number, and not as an integer or a rational number. This is done automatically when we take the square root.

Finally, note the use of the function <#1826#><#6924#><#6924#>gmul2n<#1826#>. This function has the following syntax:

<#1520#>GEN gmul2n(GEN x,long n);<#1520#>

The effect is simply to multiply <#1521#>x<#1521#> by #math566#2n, where <#1523#>n<#1523#> can be positive or negative. This is much faster than gmul or gmulgs. Note that since n=expo(s)+1, the last gmul2n call could be replaced by <#1524#>setexpo(s,-1);<#1524#>.

There is another function <#1827#><#6927#><#6927#>gshift<#1827#> with exactly the same syntax. When <#1526#>n<#1526#> is nonnegative, the effects of these two functions is the same. However when <#1527#>n<#1527#> is negative, gshift acts like a right shift of <#1528#>-n<#1528#>, hence does not give the same effect on integers. The function gshift is the PARI analogue of the C operators <#1529#>;SPMlt;;SPMlt;<#1529#> and <#1530#>;SPMgt;;SPMgt;<#1530#>.

We now come to the heart of the function. We have now a GEN p1 which points to a certain matrix of which we want to take the exponential. We will want to transform this matrix into a matrix with real (or complex of real) entries before starting the computation. To do this, we simply multiply by the real number 1 in precision prec+1 (to be safe). Then, to compute the series we will keep three variables: a variable p2 which at stage k will contain #math567##tex2html_wrap_inline19717#/k!, a variable y which will contain #math568##tex2html_wrap_inline19719##tex2html_wrap_inline19722#/i!, and the variable r which will contain #math569##tex2html_wrap_inline19726#/k!. Note that we do not use horner's rule. this is simply because we are lazy and do not want to compute in advance the number of terms that we need. We leave this modification (and many other improvements!) to the reader. The program continues as follows:

<#1850#><#1535#><#1535#> /* initialisations before the loop */ <#1536#><#1536#> ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;r=cgetr(prec+1);gaffsg(1,r);p1=gmul(r,p1); ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;y=gscalmat(r,lx-1); /* this creates the scalar matrix r#math570#*#tex2html_wrap_inline19728# */ ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;p2=p1;r=s;k=1; ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;y=gadd(y,p2); <#1538#><#1538#> /* now the main loop */ <#1539#><#1539#> ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;while(expo(r) ;SPMgt;= -32*(prec-1)) ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;{k++;p2=gdivgs(gmul(p2,p1),k);r=gdivgs(gmul(s,r),k);y=gadd(y,p2);} <#1540#><#1540#> /* now square back n times if necessary */ <#1541#><#1541#> ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;if(!n){lbot=avma;y=gerepile(ltop,lbot,gcopy(y));} ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;else ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;{ ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;for(i=0;i;SPMlt;n;i++){lbot=avma;y=gmul(y,y);} ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;y=gerepile(ltop,lbot,y); ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;} ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;return(y); }<#1850#>

A few remarks once again. First note the use of the function <#1829#><#6930#><#6930#>gscalmat<#1829#> with the following syntax:

<#1543#>GEN gscalmat(GEN x, long l);<#1543#>

The effect of this function is to create the #math571##tex2html_wrap_inline19739#×#tex2html_wrap_inline19740# scalar matrix whose diagonal entries is the GEN x. Hence the length of the matrix including the codeword will in fact be <#1546#>l+1<#1546#>. There is a corresponding function <#1830#><#6932#><#6932#>gscalsmat<#1830#> which takes a long as a first argument.

If we refer to what has been said above, the main loop is clear.

When we do the final squarings, according to the fundamental theorem on the use of <#1548#>gerepile<#1548#> we keep the value of avma in lbot just <#1549#>before<#1549#> the squaring, so that if it is the last one, lbot will indeed be the bottom address of the garbage pile, and <#1550#>gerepile<#1550#> will work. Note that it takes a completely negligible time to do this in each loop compared to a matrix squaring. However, when n is initially equal to 0, no squarings have to be done, and we have our final result ready but we lost the address of the bottom of the garbage pile. Hence we use the trick of copying y again on top of the stack. This is inefficient, but does the work. If we wanted to avoid this, the best thing to do would be to put the instruction <#1551#>lbot=avma<#1551#> just before both occurences of the instruction <#1552#>y=gadd(p2,y);<#1552#>.

Remarks on the program: as such, the program should work most of the time if x is a square matrix with real or complex entries. Indeed, since essentially the first thing that we do is to multiply by the real number 1, the program should work for integer, real, rational, complex or quadratic entries. This is in accordance with the behavior of transcendental functions.

Furthermore, since this program is intended to be only an illustrative example, it has been written a little sloppily. In particular many error checks have been omitted, and the efficiency is far from optimal. An evident improvement is to avoid the unnecessary gcopy by inserting a couple of extra <#1553#>lbot=avma;<#1553#> instructions. Another improvement is to multiply the matrix x by the real number 1 right at the beginning, speeding up the computation of the L2-norm in many cases. These improvements are included in the version given in Appendix C. Still another improvement would come from a better choice of n. If the reader takes a look at the implementation of the function <#1831#><#6934#><#6934#>mpexp1<#1831#> in the file trans1.c he can make himself the necessary changes. Finally, there exist other algorithms of a different nature to compute the exponential of a matrix.


<#1555#>Remark<#1555#>: While writing this program, we have seen a few new functions (the complete list of available functions is given by alphabetical order in the index). However, if you care to look at the file <#1556#>gencom.h<#1556#>, you will notice that many more functions are defined. But in every case these missing functions are particular cases of general functions. For example, we have the function <#1557#>gneg<#1557#>, which takes the negative of a PARI object. However, there also exist functions like <#1558#>negi<#1558#> (for type integer), <#1559#>negr<#1559#> (for type real), and <#1560#>mpneg<#1560#> (for type integer or real).

These functions can of course be called by the user but we feel that the few microseconds lost in calling more general functions (in this case <#1561#>gneg<#1561#>) is compensated by the fact that one needs to remember a much smaller number of functions, and also because there is a hidden danger here: the type of the objects that you use, if they are themselves results of a previous computation, is not completely predetermined. For instance the multiplication of a type real by a type integer <#1562#>usually<#1562#> gives a result of type real, except when the integer is 0, in which case according to the PARI philosophy the result is the exact integer 0. Hence if afterwards you call a function which specifically needs a real type argument, you are in trouble.

If you really want to use these functions, their names are selfexplanatory once you know that <#1563#>i<#1563#> stands for a PARI integer, <#1564#>r<#1564#> for a PARI real, <#1565#>mp<#1565#> for i or r, <#1566#>s<#1566#> for an ordinary 32-bit signed long, <#1567#>z<#1567#> (as a suffix) meaning as usual that the result is not created on the PARI stack but assigned to a preexisting GEN given as an extra argument.

Please note that in the present version 1.35 the names of the functions are not always consistent. This will be changed. Hence anyone programming in PARI must be aware that the names of almost all functions that he uses will change eventually. If the need arises (i.e. if there really are people out there who delve into the innards of PARI), updated versions with no name changes will be released.

<#6936#><#6936#> <#6937#>=0<#6939#><#6939#><#6937#>

Appendix A : Installation Guide for the UNIX Versions

1) To print the users' manual, type ``make manual'' in the main directory or ``make'' in the tex subdirectory. This will create a file users.dvi containing the manual and a table of contents, and a separate index.dvi containing the index. You must then send the two .dvi files to your favorite printer in the usual way.

2) To compile the library and the calculator (gp).

a) Choose the Makefile appropriate to your system. Choose Makefile.sun3 if your machine is 68020/68030/68040 based. In that case, you must also choose which assembly file to use (see 8) below). Choose Makefile.sun4 if your machine is Sparc based. For any other machine, use Makefile.port. On DECstations, for example, a few extra modifications are necessary.

b) If you have the GNU gcc compiler installed (we recommend that you do), then replace in the Makefile CC=cc by CC=gcc, add the flag -g in the CFLAGS if you know how to use dbx or gdb and want to debug PARI yourself, and add the flag -traditional in the compilation options of plot.o.

c) If you are not running under suntools or X11, change in the Makefile plot.sun to plot.null. If you are using the X11 window system, change plot.sun (or plot.null in the port version) to plot.X. Some slight modifications may have to be made so that the compiler knows where to access the X11 libraries.

d) If you have the GNU readline library installed (distributed with gdb), replace gp.c by gpreadline.c, i.e. mv gp.c gpold.c; mv gpreadline.c gp.c. Then in the Makefile add -lreadline -ltermcap to the list of libraries in the compilation line for gp. Note: if you are compiling with sunview (-lsuntool -lsunwindow -lpixrect) you will have an error message about a redefined function <#1569#>rl_copy<#1569#>. Since the sun source code is not available, the way out is to recompile the GNU readline library by changing in the file readline.c <#1570#>rl_copy<#1570#> to some weird name, say <#1571#>rl_copy_kludge<#1571#>. The use of the readline and history library (suggested to me by E. Roeder) is not yet documented but is similar to emacs commands. However note that it is incompatible with SUN commandtools (but not with shelltools).

e) Then simply type ``make'' in the distribution directory. Be sure to ``make clean'' before changing to another architecture using the same file system. Note that the non 68020 versions are slower, especially the ``port'' version.

f) To test the executable, run gp on the file <#1572#>testin<#1572#> as follows: <#1573#>gp;SPMlt;testin;SPMgt;fileout <#1573#>. Then do a <#1574#>diff<#1574#> with the file <#1575#>testout<#1575#>. If any difference occurs other than the heading (version number and type), this means something is wrong. Most probably with your installation procedure, but it may be a bug in the system, in which case we would appreciate a report. Note that testin is not a severe test and is quite fast (a few minutes), but does check at least one instance of every function.

3) To install the PARI library so that it can be easily used from a user program, create the directory /usr/include/pari-include, and type ;SPMquot;make install;SPMquot;. This puts the executable gp in /usr/local/bin, the library in the directory /usr/local/lib, and the necessary include files in /usr/include/pari-include. If these directories do not suit your installation, change the LIBDIR or the INCLUDEDIR in the Makefile.

4) Once installed, to link to the PARI library just add -lpari in your link command. A sample makefile (Makesimple) is given for gp itself. All modifications made to the Makefile should of course be made on the Makesimple file.

5) If you want to use gp under gnuemacs (see section 3.10 of the users' manual) change the pathnames in the file pari.el to suit your installation, and read the file pari.txt.

6) If you want to get rid of your .o files and the created binaries in the working directory, type ``make clean''.

7) For the example of section 4.3 of the user's manual, type make in the directory example.

8) The syntax used by the SUN 3 assembler is not standard. On the MacII distribution, the correct Mac assembler syntax is given. In the present distribution, in addition to mp.s which has the SUN 3 syntax, two files called mp.news and mp.ami are included so as to help people having machines with a 680x0 processor (x ;SPMgt; = 2) but a more standard syntax.

This may not correspond to the actual syntax used, but may be closer than mp.s. In principle, apart from whitespace and the different syntax, the semantics of the two files should be identical. In case of conflict, correct mp.news or mp.ami (i.e. NEVER touch mp.s).

The file mp.news has been successfully tested on a Sony NEWS, and the file mp.ami on a Commodore amiga 2500 running Lattice C 5.10.

9) Send bugs, comments, etc...to:

pari@mizar.greco-prog.fr

10) Good luck!

<#6942#><#6942#> <#6943#>=0<#6945#><#6945#><#6943#>

Appendix B : A sample Makefile

We assume that you have installed the PARI library and include files as explained in Appendix A or in the installation guide. If you chose differently any of the directory names, please change them accordingly in the Makefiles.

If the program example that we have given is in the file mattrans.c (say as the first of several matrix transcendental functions), then a sample Makefile is the following:

<#1832#><#1578#><#1578#> CC = cc CFLAGS = -O -I/usr/include/pari-include -c <#1579#><#1579#> all:;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;mattrans <#1580#><#1580#> mattrans:;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;mattrans.c ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;$(CC) $(CFLAGS) -o mattrans mattrans.c -lpari -lm <#1832#>

<#6948#><#6948#> <#6949#>=0<#6951#><#6951#><#6949#>

Appendix C : A complete program

We give here the listing of the program seen in detail in section 4.3, with the slight modifications explained at the end of that section.

<#1851#><#1582#><#1582#> #include ;SPMlt;genpari.h;SPMgt; <#1583#><#1583#> GEN matexp(); <#1584#><#1584#> main() { ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;GEN x,y; ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;long prec,d; ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;char s[512]; <#1585#><#1585#> ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;init(1000000,2); /* take a million bytes of memory for the stack */ ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;printf(;SPMquot;precision of the computation in decimal digits? ;SPMquot;); ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;scanf(;SPMquot;d;SPMquot;,d);if(d;SPMlt;0) prec=3;else prec=d*K1+3; ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;printf(;SPMquot;input your matrix in GP format:#math572#\n;SPMquot;); ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;s[0]=0;while(!s[0]) gets(s);x=lisexpr(s); ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;y=matexp(x,prec); ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;sor(y,'g',d,0); } <#1586#><#1586#> GEN matexp(x,prec) ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;GEN x; ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;long prec; <#1587#><#1587#> { ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;GEN y,r,s,p1,p2; ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;long tx=typ(x),lx=lg(x),i,k,n,lbot,ltop; <#1588#><#1588#> /* check that x is a square matrix */ <#1589#><#1589#> ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;if(tx!=19) {printf(;SPMquot;This expression is not a matrix#math573#\n;SPMquot;);return(gzero);} ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;if(lx!=lg(x[1])) {printf(;SPMquot;This matrix is not square#math574#\n;SPMquot;);return(gzero);} <#1590#><#1590#> /* convert x to real or complex of real and compute its L2 norm */ <#1591#><#1591#> ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;ltop=avma;s=gzero; ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;r=cgetr(prec+1);gaffsg(1,r);p1=gmul(r,x); ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;for(i=1;i;SPMlt;lx;i++) s=gadd(s,gnorml2(p1[i])); ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;if(typ(s)==2) setlg(s,3); ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;s=gsqrt(s,3); /* we do not need much precision on s */ <#1592#><#1592#> /* if s;SPMlt;1, we are happy */ <#1593#><#1593#> ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;if(expo(s);SPMlt;0) n=0; ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;else {n=expo(s)+1;p1=gmul2n(p1,-n);setexpo(s,-1);} <#1594#><#1594#> /* initialisations before the loop */ <#1595#><#1595#> ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;y=gscalmat(r,lx-1); /* this creates the scalar matrix r#math575#*#tex2html_wrap_inline19758# */ ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;p2=p1;r=s;k=1; ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;lbot=avma;y=gadd(y,p2); <#1597#><#1597#> /* now the main loop */ <#1598#><#1598#> ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;while(expo(r) ;SPMgt;= -32*(prec-1)) ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;{ ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;k++;p2=gdivgs(gmul(p2,p1),k);r=gdivgs(gmul(s,r),k); ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;lbot=avma;y=gadd(y,p2); ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;} <#1599#><#1599#> /* now square back n times if necessary */ <#1600#><#1600#> ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;for(i=0;i;SPMlt;n;i++) {lbot=avma;y=gmul(y,y);} ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;y=gerepile(ltop,lbot,y); ;SPMnbsp;;SPMnbsp;;SPMnbsp;;SPMnbsp;return(y); }<#1851#>

<#6955#><#6955#> <#6956#>=0<#6958#><#6958#><#6956#>

Appendix D : Summary of available Constants

In this appendix we give the list of predefined constants available in the PARI library.

<#1834#><#1602#><#1602#> <#1603#>gzero (zero)<#1603#> see 4.1.1. <#1604#>gun (un)<#1604#> see 4.1.1. <#1605#>gdeux (deux)<#1605#> see 4.1.1. <#1606#>ghalf (lhalf)<#1606#> see 4.1.1. <#1607#>gi<#1607#> see 4.1.1. <#1608#>polun[] (lpolun[])<#1608#> see 4.1.1. <#1609#>polx[] (lpolx[])<#1609#> see 4.1.1.<#1834#>

<#1610#>geuler<#1610#>. This is Euler's constant, and is in the heap, <#1611#>not<#1611#> in the PARI stack. It is not initialized, and if you want to use it you must call <#1612#>consteuler<#1612#>(prec) ( see 3.3.18.).

<#1613#>gpi<#1613#>. This is the number pi, and is in the heap, <#1614#>not<#1614#> in the PARI stack. It is not initialized, and if you want to use it you must call <#1615#>constpi<#1615#>(prec) (see 3.3.27.).

<#1616#>bern<#1616#>(i). This is the 2i-th Bernoulli number (B0 = 1, B2 = 1/6, B4 = - 1/30, etc...) The Bernoulli numbers are in the heap and <#1617#>not<#1617#> in the PARI stack, and are not initialized. To initialize them you must use the function <#1618#>mpbern<#1618#> which has the following syntax:

<#1619#>void mpbern(long n, long prec);<#1619#>

The effect of this function is to create the even numbered bernoulli numbers up to B2n-2 <#1621#>as real numbers<#1621#> of precision prec. They can then be used with the macro <#1622#>bern<#1622#>(i). Note that this is not a function but simply an abbreviation, hence care must be taken that i is inside the right bounds (i.e. #math576#0≤in - 1) before using it, since no checking is done in PARI itself.

Finally, one has access to a table of (differences of) primes through the pointer <#1623#>diffptr<#1623#>. This is used as follows: after <#1624#>init<#1624#> has been called, this table is initialized with the differences of primes up to 500000 (default which can trivially be changed by calling <#1625#>init<#1625#> with different arguments, see 4.1.1). Then one declares <#1626#>byteptr d=diffptr;<#1626#>, where d is the name of the pointer that one uses. This will point to the first difference in the table, i.e. 2. To get to the next difference, just do <#1627#>d++<#1627#>.

In addition, some single or double-precision real numbers are predefined, and their list is in the file <#1628#>gencom.h<#1628#>

=-1 <#6962#><#6962#> <#6963#>=0<#6965#><#6965#><#6963#>

Table of Contents
=users.toc

;''