Pari/GP Reference Documentation  Contents - Index - Meta commands

Conversions and similar elementary functions or commands


Col   List   Mat   Mod   Pol   Polrev   Qfb   Ser   Set   Str   Strchr   Strexpand   Strtex   Vec   Vecsmall   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  
 
Col({x = []})  

transforms the object x into a column vector. The vector will be with one component only, except when x is a vector or a quadratic form (in which case the resulting vector is simply the initial object considered as a column vector), a matrix (the column of row vectors comprising the matrix is returned), a character string (a column of individual characters is returned), 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 gtocol(x).

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 already a matrix, a copy of x is created. 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, unless all elements are column (resp.row) vectors of the same length, in which case the vectors are concatenated sideways and the associated big matrix is returned.

  ? Mat(x + 1)
   %1 =
   [x + 1]
   ? Vec( matid(3) )
   %2 = [[1, 0, 0]~, [0, 1, 0]~, [0, 0, 1]~]
   ? Mat(%)
   %3 =
   [1 0 0]
 
   [0 1 0]
 
   [0 0 1]
   ? Col( [1,2; 3,4] )
   %4 = [[1, 2], [3, 4]]~
   ? Mat(%)
   %5 =
   [1 2]
 
   [3 4]

The library syntax is gtomat(x).

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

creates the PARI object (x mod y), i.e.an intmod 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.

flag is obsolete and should not be used.

The library syntax is gmodulo(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 will not transform an object containing variables of higher priority thanv.

? Pol(x + y, y)
   *** Pol: variable must have higher priority in gtopoly.

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. Negative definite forms are not implemented, use their positive definite counterpart instead.

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 also 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 of character strings, with strictly increasing entries with respect to lexicographic ordering. 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({x}*)  

converts its argument list into a single 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 [Label: se:strings].

? x2 = 0; i = 2; Str(x, i)
 %1 = "x2"
 ? eval(%)
 %2 = 0

This function is mostly useless in library mode. Use the pair strtoGEN/ GENtostr to convert between GEN and char*. The latter returns a malloced string, which should be freed after usage.

Strchr(x)  

converts x to a string, translating each integer into a character.

? Strchr(97)
 %1 = "a"
 ? Vecsmall("hello world")
 %2 = Vecsmall([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100])
 ? Strchr(%)
 %3 = "hello world"

Strexpand({x}*)  

converts its argument list into a single character string (type t_STR, the empty string if x is omitted). Then performe environment expansion, see Section [Label: se:envir]. This feature can be used to read environment variable values.

? Strexpand("$HOME/doc")
 %1 = "/home/pari/doc"

The individual arguments are read in string context, see Section [Label: se:strings].

Strtex({x}*)  

translates its arguments to TeX format, and concatenates the results into a single character string (type t_STR, the empty string if x is omitted).

The individual arguments are read in string context, see Section [Label: se:strings].

Vec({x = []})  

transforms the object x into a row vector. The vector will be with one component only, except when x is a vector or a quadratic form (in which case the resulting vector is simply the initial object considered as a row vector), a matrix (the vector of columns comprising the matrix is return), a character string (a vector of individual characters is returned), 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).

Vecsmall({x = []})  

transforms the object x into a row vector of type t_VECSMALL. This acts as Vec, but only on a limited set of objects (the result must be representable as a vector of small integers). In particular, polynomials and power series are forbidden. If x is a character string, a vector of individual characters in ASCII encoding is returned ( Strchr yields back the character string).

The library syntax is gtovecsmall(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_i (x_i andy_i) 2^i

Negative numbers behave 2-adically, i.e.the result is the 2-adic limit of bitand(x_n,y_n), where x_n and y_n are non-negative integers tending to x and y respectively. (The result is an ordinary integer, possibly negative.)

? bitand(5, 3)
 %1 = 1
 ? bitand(-5, 3)
 %2 = 3
 ? bitand(-5, -3)
 %3 = -7

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-1} 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.

See Section [Label: se:bitand] for the behaviour for negative arguments.

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

See Section [Label: se:bitand] for the behaviour for negative arguments.

The library syntax is gbitnegimply(x,y).

bitor(x,y)  

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

See Section [Label: se:bitand] for the behaviour for negative arguments.

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.

See Section [Label: se:bitand] for the behaviour at negative arguments.

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

bitxor(x,y)  

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

See Section [Label: se:bitand] for the behaviour for negative arguments.

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. Reals 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 [Label: se:reorder]). 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 in 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 gcoeff and gmael are available as direct accessors to a GEN component. See Chapter 4 for details.

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, intmods, 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 intmod 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)  

denominator of x. The meaning of this is clear when x is a rational number or function. If x is an integer or a polynomial, it is treated as a rational number of function, respectively, and the result is equal to 1. For polynomials, you probably want to use

    denominator( content(x) )

instead. As for modular objects, t_INTMOD and t_PADIC have denominator 1, and the denominator of a t_POLMOD is the denominator of its (minimal degree) polynomial representative.

If x is a recursive structure, for instance a vector or matrix, the lcm of the denominators of its components (a common denominator) is computed. This also applies for t_COMPLEXs and t_QUADs.

Warning: multivariate objects are created according to variable priorities, with possibly surprising side effects (x/y is a polynomial, but y/x is a rational function). See Section [Label: se:priority].

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). This returns a copy of the imaginary part. The internal routine imag_i is faster, since it returns the pointer and skips the copy.

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 whose modulus has main variable v (if v does not occur in x, lifts only intmods). If x is of recursive (non modular) type, the lift is done coefficientwise. For p-adics, this routine acts as truncate. It is not allowed to have x of type t_REAL.

? lift(Mod(5,3))
 %1 = 2
 ? lift(3 + O(3^9))
 %2 = 3
 ? lift(Mod(x,x^2+1))
 %3 = x
 ? lift(x * Mod(1,3) + Mod(2,3))
 %4 = x + 2
 ? lift(x * Mod(y,y^2+1) + Mod(2,3))
 %5 = y*x + Mod(2, 3)   \\ do you understand this one ?

 ? lift(x * Mod(y,y^2+1) + Mod(2,3), x)  %6 = Mod(y, y^2+1) * x + Mod(2, y^2+1)

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. More precisely, if x is a scalar, norml2(x) is defined to be x * conj(x). If x is a (row or column) vector or a matrix, norml2(x) is defined recursively as sum_i norml2(x_i), where (x_i) run through the components of x. In particular, this yields the usual sum |x_i|^2 (resp.sum |x_{i,j}|^2) if x is a vector (resp.matrix) with complex components.

? norml2( [ 1, 2, 3 ] )      \\ vector
 %1 = 14
 ? norml2( [ 1, 2; 3, 4] )   \\ matrix
 %1 = 30
 ? norml2( I + x )
 %3 = x^2 + 1
 ? norml2( [ [1,2], [3,4], 5, 6 ] )   \\ recursively defined
 %4 = 91

The library syntax is gnorml2(x).

numerator(x)  

numerator of x. The meaning of this is clear when x is a rational number or function. If x is an integer or a polynomial, it is treated as a rational number of function, respectively, and the result is x itself. For polynomials, you probably want to use

    numerator( content(x) )

instead.

In other cases, numerator(x) is defined to be denominator(x)*x. This is the case when x is a vector or a matrix, but also for t_COMPLEX or t_QUAD. In particular since a t_PADIC or t_INTMOD has denominator 1, its numerator is itself.

Warning: multivariate objects are created according to variable priorities, with possibly surprising side effects (x/y is a polynomial, but y/x is a rational function). See Section [Label: se:priority].

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 numtoperm(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 permtonum(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}})  

returns a random integer between 0 and N-1. N is an integer, which can be arbitrary large. This is an internal PARI function and does not depend on the system's random number generator.

The resulting integer is obtained by means of linear congruences and will not be well distributed in arithmetic progressions. The random seed may be obtained via getrand, and reset using setrand.

Note that random(2^31) is not equivalent to random(), although both return an integer between 0 and 2^{31}-1. In fact, calling random with an argument generates a number of random words (32bit or 64bit depending on the architecture), rescaled to the desired interval. The default uses directly a 31-bit generator.

The library syntax is genrand(N). Also available are pari_rand() which returns a random unsigned long (32bit or 64bit depending on the architecture), and pari_rand31() which returns a 31bit long integer.

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). This returns a copy of the real part. The internal routine real_i is faster, since it returns the pointer and skips the copy.

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 simplifies x as much as it can. Specifically, a complex or quadratic number whose imaginary part is an exact 0 (i.e.not an approximate one as a O(3) or 0.E-28) is converted to its real part, and a polynomial of degree 0 is converted to its constant term. Simplifications occur recursively.

This function is especially useful before using arithmetic functions, which expect integer arguments:

? x = 1 + y - y
 %1 = 1
 ? divisors(x)
   *** divisors: not an integer argument in an arithmetic function
 ? type(x)
 %2 = "t_POL"
 ? type(simplify(x))
 %3 = "t_INT"

Note that GP results are simplified as above before they are stored in the history. (Unless you disable automatic simplification with \y, that is.) In particular

? type(%1)
 %4 = "t_INT"

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; taille(x) 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 #Str(x), which is 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 vector / matrices; e is then the maximal number of error bits. If x is a rational function, the result is the "integer part" (Euclidean quotient of numerator by denominator) and e is not set.

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 intmod 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.