Conversions and similar elementary functions

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.

binary(x): outputs the vector of the binary digits of | x|, where | x| < 232.

The library syntax is $\teb$binaire(x).

bittest(x, n): outputs the 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 $\teb$bittest(x, n) where n as well as the result are C-integers.

[*](x): ceiling of x. When x is in $\Bbb$R, the result is the smallest integer greater than or equal to x.

The library syntax is $\teb$gceil(x).

[*](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 x, a, b, c. Then, if y is the vector [x,c,a,b], the variable a will be replaced by c, b by a, and a by b, x being unchanged. Note that the permutation must be completely specified, e.g. [c,a,b] would not work, since this would replace x by c, and leave a and b unchanged.

The library syntax is $\teb$changevar(x, y).

[*] of a PARI object:

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

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

The library syntax is $\teb$compo(x, n), where n is a 32-bit C-integer.

The two other methods are more natural but more restricted. First, the function $\teb$coeff(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 reorder, 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 compo 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 compo.

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

The library syntax is $\teb$truecoeff(x, n).

The third method is specific to vectors or matrices under GP. If x is a (row or column) vector, then [*] represents the nth component of x, i.e. compo(x,n). It is more natural and shorter to write. If x is a matrix, [*] represents the coefficient of row m and column n of the matrix.

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

conj(x) or x$\_$: 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 $\teb$gconj(x).

cvtoi(x): If x is in $\Bbb$R, 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 $\teb$gcvtoi(x,&e) where e is a 32-bit C-integer.

denom(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 $\teb$denom(x).

floor(x): floor of x. When x is in $\Bbb$R, the result is the largest integer smaller than or equal to x.

The library syntax is $\teb$gfloor(x).

frac(x): fractional part of x. Identical to x - floor(x). If x is real, the result is in [0, 1[.

The library syntax is $\teb$gfrac(x).

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

The library syntax is $\teb$gimag(x).

length(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 $\teb$glength(x).

lift(x): lifts an element x = a mod n of $\Bbb$Z/n$\Bbb$Z to a in $\Bbb$Z, 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 $\teb$lift(x).

mod(x, y): creates the PARI object (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 $\teb$gmodulcp(x, y).

modp(x, y): same effect as mod, 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 $\teb$gmodulo(x, y).

norm(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 $\Bbb$R is its square, so as to be compatible with the complex norm.

The library syntax is $\teb$gnorm(x).

norml2(x): square of the L2-norm of x. x must be a (row or column) vector.

The library syntax is $\teb$gnorml2(x).

numer(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 numer(x) is defined to be denom(x)*x. All other types are forbidden.

The library syntax is $\teb$numer(x).

poly(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 trunc (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 not a substitution function. It is intended to be quick and dirty. So if you try poly(a,y) on the polynomial a=x+y, you will get y+y, which is not a valid PARI/GP object.

The library syntax is $\teb$gtopoly(x, v), where v is a variable number.

prec(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 decimal 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 prec never changes the type of the result. In particular it is not possible to use prec to obtain a polynomial from a power series. For that, see trunc (3.2.25 below).

The library syntax is $\teb$gprec(x, n), where n is a C-integer.

quadgen(x): creates the quadratic number omega ω = (a + $\sqrt{{x}}$)/2 where a = 0 if x≡0 mod 4, a = 1 if x≡1 mod 4, so that (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 $\teb$quadgen(x).

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

The library syntax is $\teb$quaddisc(x).

real(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 (1, ω).

The library syntax is $\teb$greal(x).

rndtoi(x): same as cvtoi (see 3.2.5) except that truncation is replaced by rounding (see 3.2.23).

The library syntax is $\teb$grndtoi(x,&e).

round(x): If x is in $\Bbb$R, 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 rndtoi (3.2.22) to suppress this error handling.

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

trunc$\displaystyle \left(\vphantom{\dfrac{2.4*X^2-1.7}{1.2*X}}\right.$$\displaystyle {\dfrac{{2.4*X^2-1.7}}{{1.2*X}}}$$\displaystyle \left.\vphantom{\dfrac{2.4*X^2-1.7}{1.2*X}}\right)$ = 2.0*X,

while

round$\displaystyle \left(\vphantom{\dfrac{2.4*X^2-1.7}{1.2*X}}\right.$$\displaystyle {\dfrac{{2.4*X^2-1.7}}{{1.2*X}}}$$\displaystyle \left.\vphantom{\dfrac{2.4*X^2-1.7}{1.2*X}}\right)$ = $\displaystyle {\dfrac{{2*X^2-2}}{{X}}}$.

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

The library syntax is $\teb$ground(x).

series(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 precdl (transparent under GP). If x is a polynomial, the precision is the greatest of precdl 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 poly, see 3.2.17 above).

The warning given for poly applies here: this is not a substitution function.

The library syntax is $\teb$gtoser(x, v), where v is a variable number.

setprecision(n): sets the current default precision equal to n decimal digits if n > 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 \precision=n command, with the difference that it can be used inside any GP expression or program.

The library syntax is $\teb$setprecr(n), where n is a C-integer, as is the returned value.

setserieslength(n): sets the current default series length equal to n if n > 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 \serieslength=n command, with the difference that it can be used inside any GP expression or program.

The library syntax is $\teb$setserieslength(n), where n is a C-integer, as is the returned value.

trunc(x): truncation of x. When x is in $\Bbb$R, 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 cvtoi (3.2.5) to suppress this error handling.

Note a very special use of trunc: 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 $\teb$gtrunc(x).

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

The library syntax is $\teb$gtype(x), but need not be used since the internal function typ is available. Note that gtype gives a GEN while typ gives a C-integer.

valuation(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 $\teb$ggval(x).

vec(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 $\teb$gtovec(x).