Pari/GP Reference Documentation  Contents - Index - Meta commands

Conversions and similar elementary functions or commands


List   Mat   Mod   Pol   Polrev   Qfb   Ser   Set   Str   Vec   binary   bitand   bitneg   bitnegimply   bitor   bittest   bitxor   ceil   centerlift   changevar   component   conj   conjvec   denominator   floor   frac   imag   length   lift   norm   norml2   numerator   numtoperm   padicprec   permtonum   precision   random   real   round   simplify   sizebyte   sizedigit   truncate   valuation   variable  
 
List({x = []})  

transforms a (row or column) vector x into a list. The only other way to create a t_LIST is to use the function listcreate.

This is useless in library mode.

Mat({x = []})  

transforms the object x into a matrix. If x is not a vector or a matrix, this creates a 1 x 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 concat (see there).

The library syntax is gtomat(x).

Mod(x,y,{flag = 0})  

creates the PARI object (x mod y), i.e.an integermod or a polmod. y must be an integer or a polynomial. If y is an integer, x must be an integer, a rational number, or a p-adic number compatible with the modulus y. If y is a polynomial, x must be a scalar (which is not a polmod), a polynomial, a rational function, or a power series.

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

If flag is equal to 1, the modulus of 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 Section (??)). Functions will operate faster on such objects and memory consumption will be lower. On the other hand, care should be taken to avoid creating too many such objects.

Under GP, the same effect can be obtained by assigning the object to a GP variable (the value of which is a permanent object for the duration of the relevant library function call, and is treated as such). This value is subject to garbage collection, since it will be deleted when the value changes. This is preferable and the above flag is only retained for compatibility reasons (it can still be useful in library mode).

The library syntax is Mod0(x,y,flag). Also available are

* for flag = 1: gmodulo(x,y).

* for flag = 0: gmodulcp(x,y).

Pol(x,{v = x})  

transforms 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 truncate (see there), i.e.it chops off the O(X^k). 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 Pol(a,y) on the polynomial a = x+y, you will get y+y, which is not a valid PARI object.

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

Polrev(x,{v = x})  

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 truncate (see there), i.e.it chops off the O(X^k). If x is a vector, this function creates the polynomial whose coefficients are given in x, with x[1] being the constant term. Note that this is the reverse of Pol if x is a vector, otherwise it is identical to Pol.

The library syntax is gtopolyrev(x,v), where v is a variable number.

Qfb(a,b,c,{D = 0.})  

creates the binary quadratic form ax^2+bxy+cy^2. If b^2-4ac > 0, initialize Shanks' distance function to D.

The library syntax is Qfb0(a,b,c,D,prec). Also available are qfi(a,b,c) (when b^2-4ac < 0), and qfr(a,b,c,d) (when b^2-4ac > 0).

Ser(x,{v = x})  

transforms the object x into a power series with main variable v (x by default). If x is a scalar, this gives a constant power series with precision given by the default serieslength (corresponding to the C global variable precdl). 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 Pol).

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

The library syntax is gtoser(x,v), where v is a variable number (i.e.a C integer).

Set({x = []})  

converts x into a set, i.e.into a row vector with strictly increasing entries. x can be of any type, but is most useful when x is already a vector. The components of x are put in canonical form (type t_STR) so as to be easily sorted. To recover an ordinary GEN from such an element, you can apply eval to it.

The library syntax is gtoset(x).

Str  

Str({x = ""},{flag = 0})

converts x into a character string (type t_STR, the empty string if x is omitted). To recover an ordinary GEN from a string, apply eval to it. The arguments of Str are evaluated in string context (see Section (??)). If flag is set, treat x as a filename and perform environment expansion on the string. This feature can be used to read environment variable values.

? i = 1; Str("x" i)
 %1 = "x1"
 ? eval(%)
 %2 = x1;
 ? Str("$HOME", 1)
 %3 = "/home/pari"

The library syntax is strtoGENstr(x,flag). This function is mostly useless in library mode. Use the pair strtoGEN/ GENtostr to convert between char* and GEN.

Vec({x = []})  

transforms the object x into a row 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 start 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 gtovec(x).

binary(x)  

outputs the vector of the binary digits of |x|. Here x can be an integer, a real number (in which case the result has two components, one for the integer part, one for the fractional part) or a vector/matrix.

The library syntax is binaire(x).

bitand(x,y)  

bitwise and of two integers x and y, that is the integer sum (x_i andy_i) 2^i

Negative numbers behave as if modulo a huge power of 2.

The library syntax is gbitand(x,y).

bitneg(x,{n = -1})  

bitwise negation of an integer x, truncated to n bits, that is the integer sum_{i = 0}^n not(x_i) 2^i The special case n = -1 means no truncation: an infinite sequence of leading 1 is then represented as a negative number.

Negative numbers behave as if modulo a huge power of 2.

The library syntax is gbitneg(x).

bitnegimply(x,y)  

bitwise negated imply of two integers x and y (or not (x ==> y)), that is the integer sum (x_i and not(y_i)) 2^i

Negative numbers behave as if modulo a huge power of 2.

The library syntax is gbitnegimply(x,y).

bitor(x,y)  

bitwise (inclusive) or\sidx{bitwise inclusive or} of two integers x and y, that is the integer sum (x_i ory_i) 2^i

Negative numbers behave as if modulo a huge power of 2.

The library syntax is gbitor(x,y).

bittest(x,n)  

outputs the n^{th} bit of |x| starting from the right (i.e.the coefficient of 2^n in the binary expansion of x). The result is 0 or 1. To extract several bits at once as a vector, pass a vector for n.

The library syntax is bittest(x,n), where n and the result are longs.

bitxor(x,y)  

bitwise (exclusive) or\sidx{bitwise exclusive or} of two integers x and y, that is the integer sum (x_i xory_i) 2^i Negative numbers behave as if modulo a huge power of 2.

The library syntax is gbitxor(x,y).

ceil(x)  

ceiling of x. When x is in R, the result is the smallest integer greater than or equal to x. Applied to a rational function, ceil(x) returns the euclidian quotient of the numerator by the denominator.

The library syntax is gceil(x).

centerlift(x,{v})  

lifts an element x = a mod n of Z/nZ to a in Z, and similarly lifts a polmod to a polynomial. This is the same as lift except that in the particular case of elements of Z/nZ, the lift y is such that -n/2 < y <= n/2. If x is of type fraction, complex, quadratic, polynomial, power series, rational function, vector or matrix, the lift is done for each coefficient. Real and p-adics are forbidden.

The library syntax is centerlift0(x,v), where v is a long and an omitted v is coded as -1. Also available is centerlift(x) = centerlift0(x,-1).

changevar(x,y)  

creates a copy of the object x where its variables are modified 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 c 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 (as well as c which is the fourth variable of the initial list). In particular, the new variable names must be distinct.

The library syntax is changevar(x,y).

components 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 component(x,n) which extracts the n^{th}-component of x. This is to be understood as follows: every PARI type has one or two initial code words. The components are counted, starting at 1, after these code words. In particular if x is a vector, this is indeed the n^{th}-component of x, if x is a matrix, the n^{th} column, if x is a polynomial, the n^{th} coefficient (i.e.of degree n-1), and for power series, the n^{th} significant coefficient. The use of the function component implies the knowledge of the structure of the different PARI types, which can be recalled by typing \t under GP.

The library syntax is compo(x,n), where n is a long.

The two other methods are more natural but more restricted. The function polcoeff(x,n) gives the coefficient of degree n of the polynomial or power series x, with respect to the main variable of x (to check variable ordering, or to change it, use the function reorder, see Section (??)). 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 polcoeff as a polynomial of degree zero.

The library syntax is truecoeff(x,n).

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

Finally note that in library mode, the macros coeff and mael are available to deal with the non-recursivity of the GEN type from the compiler's point of view. See the discussion on typecasts in Chapter 4.

conj(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 polmod (see conjvec for this).

The library syntax is gconj(x).

conjvec(x)  

conjugate vector representation of x. If x is a polmod, equal to Mod(a,q), this gives a vector of length degree(q) containing the complex embeddings of the polmod if q has integral or rational coefficients, and the conjugates of the polmod if q has some integermod coefficients. The order is the same as that of the polroots functions. If x is an integer or a rational number, the result isx. If x is a (row or column) vector, the result is a matrix whose columns are the conjugate vectors of the individual elements of x.

The library syntax is conjvec(x,prec).

denominator(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 denom(x).

floor(x)  

floor of x. When x is in R, the result is the largest integer smaller than or equal to x. Applied to a rational function, floor(x) returns the euclidian quotient of the numerator by the denominator.

The library syntax is 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 gfrac(x).

imag(x)  

imaginary part of x. When x is a quadratic number, this is the coefficient of omega in the "canonical" integral basis (1,omega).

The library syntax is gimag(x).

length(x)  

number of non-code words in x really used (i.e.the effective length minus 2 for integers and polynomials). In particular, the degree of a polynomial is equal to its length minus 1. If x has type t_STR, output number of letters.

The library syntax is glength(x) and the result is a C long.

lift(x,{v})  

lifts an element x = a mod n of Z/nZ to a in Z, and similarly lifts a polmod to a polynomial if v is omitted. Otherwise, lifts only polmods with main variable v (if v does not occur in x, lifts only intmods). 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.

The library syntax is lift0(x,v), where v is a long and an omitted v is coded as -1. Also available is lift(x) = lift0(x,-1).

norm(x)  

algebraic norm of x, i.e.the product of x with its conjugate (no square roots are taken), or conjugates for polmods. For vectors and matrices, the norm is taken componentwise and hence is not the L^2-norm (see norml2). Note that the norm of an element of R is its square, so as to be compatible with the complex norm.

The library syntax is gnorm(x).

norml2(x)  

square of the L^2-norm of x. x must be a (row or column) vector.

The library syntax is gnorml2(x).

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

The library syntax is numer(x).

numtoperm(n,k)  

generates the k-th permutation (as a row vector of length n) of the numbers 1 to n. The number k is taken modulo n!, i.e.inverse function of permtonum.

The library syntax is permute(n,k), where n is a long.

padicprec(x,p)  

absolute p-adic precision of the object x. This is the minimum precision of the components of x. The result is VERYBIGINT (2^{31}-1 for 32-bit machines or 2^{63}-1 for 64-bit machines) if x is an exact object.

The library syntax is padicprec(x,p) and the result is a long integer.

permtonum(x)  

given a permutation x on n elements, gives the number k such that x = numtoperm(n,k), i.e.inverse function of numtoperm.

The library syntax is permuteInv(x).

precision(x,{n})  

gives the precision in decimal digits of the PARI object x. If x is an exact object, the largest single precision integer is returned. If n is not omitted, creates a new object equal to x 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 precision never changes the type of the result. In particular it is not possible to use it to obtain a polynomial from a power series. For that, see truncate.

The library syntax is precision0(x,n), where n is a long. Also available are ggprecision(x) (result is a GEN) and gprec(x,n), where n is a long.

random({N = 2^{31}})  

gives a random integer between 0 and N-1. N can be arbitrary large. This is an internal PARI function and does not depend on the system's random number generator. Note that the resulting integer is obtained by means of linear congruences and will not be well distributed in arithmetic progressions.

The library syntax is genrand(N).

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,omega).

The library syntax is greal(x).

round(x,{&e})  

If x is in R, rounds x to the nearest integer and sets e to the number of error bits, that is the binary exponent of the difference between the original and the rounded value (the "fractional part"). If the exponent of x is too large compared to its precision (i.e.e > 0), the result is undefined and an error occurs if e was not given.

Important remark: note that, contrary to the other truncation functions, this function operates on every coefficient at every level of a PARI object. For example truncate((2.4*X^2-1.7)/(X)) = 2.4*X, whereas round((2.4*X^2-1.7)/(X)) = (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 grndtoi(x,&e), where e is a long integer. Also available is ground(x).

simplify(x)  

this function tries to simplify the object x as much as it can. The simplifications do not concern rational functions (which PARI automatically tries to simplify), but type changes. Specifically, a complex or quadratic number whose imaginary part is exactly equal to 0 (i.e.not a real zero) is converted to its real part, and a polynomial of degree zero is converted to its constant term. For all types, this of course occurs recursively. This function is useful in any case, but in particular before the use of arithmetic functions which expect integer arguments, and not for example a complex number of 0 imaginary part and integer real part (which is however printed as an integer).

The library syntax is simplify(x).

sizebyte(x)  

outputs the total number of bytes occupied by the tree representing the PARI object x.

The library syntax is taille2(x) which returns a long. The function taille returns the number of words instead.

sizedigit(x)  

outputs a quick bound for the number of decimal digits of (the components of) x, off by at most 1. If you want the exact value, you can use length(Str(x)), which is much slower.

The library syntax is sizedigit(x) which returns a long.

truncate(x,{&e})  

truncates x and sets e to the number of error bits. When x is in R, this means that the part after the decimal point is chopped away, e is the binary exponent of the difference between the original and the truncated value (the "fractional part"). If the exponent of x is too large compared to its precision (i.e.e > 0), the result is undefined and an error occurs if e was not given. The function applies componentwise on rational functions and vector / matrices; e is then the maximal number of error bits.

Note a very special use of truncate: 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(X^k). Similarly, when applied to a p-adic number, it transforms it into an integer or a rational number by chopping away the O(p^k).

The library syntax is gcvtoi(x,&e), where e is a long integer. Also available is gtrunc(x).

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 divisible 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. Finally, the valuation of a vector, complex or quadratic number is the minimum of the component valuations.

If x = 0, the result is VERYBIGINT (2^{31}-1 for 32-bit machines or 2^{63}-1 for 64-bit machines) if x is an exact object. If x is a p-adic numbers or power series, the result is the exponent of the zero. Any other type combinations gives an error.

The library syntax is ggval(x,p), and the result is a long.

variable(x)  

gives the main variable of the object x, and p if x is a p-adic number. Gives an error if x has no variable associated to it. Note that this function is useful only in GP, since in library mode the function gvar is more appropriate.

The library syntax is gpolvar(x). However, in library mode, this function should not be used. Instead, test whether x is a p-adic (type t_PADIC), in which case p is in x[2], or call the function gvar(x) which returns the variable number of x if it exists, BIGINT otherwise.