Conversions and similar elementary functions or commands

Col({x = []})

transforms the object x into a column vector. The vector has a single component , 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),

* 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. In this last case, Col is the reciprocal function of Pol and Ser respectively.

Note that the function Colrev does not exist, use Vecrev.

The library syntax is GEN gtocol(GEN x = NULL).




List({x = []})

transforms a (row or column) vector x into a list, whose components are the entries of x. Similarly for a list, but rather useless in this case. For other types, creates a list with the single element x. Note that, except when x is omitted, this function creates a small memory leak; so, either initialize all lists to the empty list, or use them sparingly.

The library syntax is GEN gtolist(GEN x = NULL). The variant GEN listcreate(void) creates an empty list.




Mat({x = []})

transforms the object x into a matrix. If x is already a matrix, a copy of x is created. 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. If x is a binary quadratic form, creates the associated 2 x 2 matrix. Otherwise, this creates a 1 x 1 matrix containing x.

? 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]
 ? Mat(Qfb(1,2,3))
 %6 =
 [1 1]
 
 [1 3]

The library syntax is GEN gtomat(GEN x = NULL).




Mod(x,y)

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.

The library syntax is GEN gmodulo(GEN x, GEN 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 with non-negative valuation or a rational function, the effect is similar to truncate, i.e.we chop off the O(X^k) or compute the Euclidean quotient of the numerator by the denominator, then change the main variable of the result to v.

The main use of this function is when x is a vector: it creates the polynomial whose coefficients are given by x, with x[1] being the leading coefficient (which can be zero). It is much faster to evaluate Pol on a vector of coefficients in this way, than the corresponding formal expression a_n X^n +...+ a_0, which is evaluated naively exactly as written (linear versus quadratic time in n). Polrev can be used if one wants x[1] to be the constant coefficient:

? Pol([1,2,3])
 %1 = x^2 + 2*x + 3
 ? Polrev([1,2,3])
 %2 = 3*x^2 + 2*x + 1

The reciprocal function of Pol (resp. Polrev) is Vec (resp. Vecrev).

? Vec(Pol([1,2,3]))
 %1 = [1, 2, 3]
 ? Vecrev( Polrev([1,2,3]) )
 %2 = [1, 2, 3]

Warning. This is not a substitution function. It will not transform an object containing variables of higher priority thanv.

? Pol(x + y, y)
   ***   at top-level: Pol(x+y,y)
   ***                 ^----------
   *** Pol: variable must have higher priority in gtopoly.

The library syntax is GEN gtopoly(GEN x, long v = -1), 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, i.e.it chops off the O(X^k).

The main use of this function is when x is a vector: it creates the polynomial whose coefficients are given by x, with x[1] being the constant term. Pol can be used if one wants x[1] to be the leading coefficient:

? Polrev([1,2,3])
 %1 = 3*x^2 + 2*x + 1
 ? Pol([1,2,3])
 %2 = x^2 + 2*x + 3

The reciprocal function of Pol (resp. Polrev) is Vec (resp. Vecrev).

The library syntax is GEN gtopolyrev(GEN x, long v = -1), 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 GEN Qfb0(GEN a, GEN b, GEN c, GEN D = NULL, long prec). Also available are GEN qfi(GEN a, GEN b, GEN c) (assumes b^2-4ac < 0) and GEN qfr(GEN a, GEN b, GEN c, GEN D) (assumes b^2-4ac > 0).




Ser(s,{v = x},{d = seriesprecision})

transforms the object s into a power series with main variable v (x by default) and precision (number of significant terms) equal to d ( = the default seriesprecision by default). If s is a scalar, this gives a constant power series with precision d. If s is a polynomial, the precision is the maximum of d and the degree of the polynomial. If s is a vector, the coefficients of the vector are understood to be the coefficients of the power series starting from the constant term (as in Polrev(x)), and the precision d is ignored.

? Ser(x^2,, 5)
 %1 = x^2 + O(x^7)
 ? Ser([1,2,3], t)
 %2 = 1 + 2*t + 3*t^2 + O(t^3)

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

The library syntax is GEN gtoser(GEN s, long v = -1, long precdl), where v is a variable number.




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.

Note that most set functions also accept ordinary vectors, provided their components can be compared with < . Sets as created by this function are only useful when e.g. polynomial or vector entries are involved.

The library syntax is GEN gtoset(GEN x = NULL).




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"

The library syntax is GEN Strchr(GEN x).




Strexpand({x}*)

converts its argument list into a single character string (type t_STR, the empty string if x is omitted). Then perform 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. That vector has a single component, 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),

* 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. In this last case, Vec is the reciprocal function of Pol and Ser respectively.

The library syntax is GEN gtovec(GEN x = NULL).




Vecrev({x = []})

as Vec(x), then reverse the result. In particular In this case, Vecrev is the reciprocal function of Polrev: the coefficients of the vector start with the constant coefficient of the polynomial and the others follow by increasing degree.

The library syntax is GEN gtovecrev(GEN x = NULL).




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 GEN gtovecsmall(GEN x = NULL).




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 GEN binaire(GEN 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 GEN gbitand(GEN x, GEN y). Also available is GEN ibitand(GEN x, GEN y), which returns the bitwise and of |x| and |y|, two integers.




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 behavior for negative arguments.

The library syntax is GEN gbitneg(GEN x, long n).




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 behavior for negative arguments.

The library syntax is GEN gbitnegimply(GEN x, GEN y). Also available is GEN ibitnegimply(GEN x, GEN y), which returns the bitwise negated imply of |x| and |y|, two integers.




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 behavior for negative arguments.

The library syntax is GEN gbitor(GEN x, GEN y). Also available is GEN ibitor(GEN x, GEN y), which returns the bitwise ir of |x| and |y|, two integers.




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.

? bittest(7, 3)
 %1 = 1 \\ the 3rd bit is 1
 ? bittest(7, 4)
 %2 = 0 \\ the 4th bit is 0

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

The library syntax is GEN gbittest(GEN x, long n). For a t_INT x, the variant long bittest(GEN x, long n) is generally easier to use.




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 behavior for negative arguments.

The library syntax is GEN gbitxor(GEN x, GEN y). Also available is GEN ibitxor(GEN x, GEN y), which returns the bitwise xor of |x| and |y|, two integers.




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 Euclidean quotient of the numerator by the denominator.

The library syntax is GEN gceil(GEN 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. A t_PADIC is lifted as above if its valuation v is non-negative; if not, returns the fraction p^v centerlift(x^{-v}); in particular, note that rational reconstruction is not attempted.

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 GEN centerlift0(GEN x, long v = -1), where v is a variable number. Also available is GEN centerlift(GEN x) corresponding to centerlift0(x,-1).




component(x,n)

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.

For polynomials and power series, one should rather use polcoeff, and for vectors and matrices, the [] operator. Namely, if x is a vector, then x[n] represents the n-th component of x. 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.

Using of this function requires detailed knowledge of the structure of the different PARI types, and thus it should almost never be used directly. Some useful exceptions:

    ? x = 3 + O(3^5);
     ? component(x, 2)
     %2 = 81   \\ p^(p-adic acurracy)
     ? component(x, 1)
     %3 = 3    \\ p
     ? q = Qfb(1,2,3);
     ? component(q, 1)
     %5 = 1

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




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 GEN gconj(GEN x).




conjvec(z)

conjugate vector representation of z. If z is a polmod, equal to Mod(a,T), this gives a vector of length {degree}(T) containing:

* the complex embeddings of z if T has rational coefficients, i.e.the a(r[i]) where r = polroots(T);

* the conjugates of z if T has some intmod coefficients;

if z is a finite field element, the result is the vector of conjugates [z,z^p,z^{p^2},...,z^{p^{n-1}}] where n = {degree}(T).

If z is an integer or a rational number, the result isz. If z is a (row or column) vector, the result is a matrix whose columns are the conjugate vectors of the individual elements of z.

The library syntax is GEN conjvec(GEN z, long 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 or 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 GEN denom(GEN 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 Euclidean quotient of the numerator by the denominator.

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




length(x)

length of x; #x is a shortcut for length(x). This is mostly useful for

* vectors: dimension (0 for empty vectors),

* lists: number of entries (0 for empty lists),

* matrices: number of columns,

* character strings: number of actual characters (without trailing \0, should you expect it from C char*).

 ? #"a string"
  %1 = 8
  ? #[3,2,1]
  %2 = 3
  ? #[]
  %3 = 0
  ? #matrix(2,5)
  %4 = 5
  ? L = List([1,2,3,4]); #L
  %5 = 4

The routine is in fact defined for arbitrary GP types, but is awkward and useless in other cases: it returns the number of non-code words in x, e.g. the effective length minus 2 for integers since the t_INT type has two code words.

The library syntax is long glength(GEN x).




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 GEN lift0(GEN x, long v = -1), where v is a variable number. Also available is GEN lift(GEN x) corresponding to 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 GEN gnorm(GEN 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 polynomial, 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 polynomial or vector (resp.matrix) with complex components.

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

The library syntax is GEN gnorml2(GEN 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 or 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 GEN numer(GEN 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 GEN numtoperm(long n, GEN k).




padicprec(x,p)

absolute p-adic precision of the object x. This is the minimum precision of the components of x. The result is LONG_MAX (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 long padicprec(GEN x, GEN p).




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 GEN permtonum(GEN 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.

? precision(exp(1e-100))
 %1 = 134                \\ 134 significant decimal digits
 ? precision(2 + x)
 %2 = 2147483647         \\ exact object
 ? precision(0.5 + O(x))
 %3 = 28                 \\ floating point accuracy, NOT series precision
 ? precision( [ exp(1e-100), 0.5 ] )
 %4 = 28                 \\ minimal accuracy among components

The return value for exact objects is meaningless since it is not even the same on 32 and 64-bit machines. The proper way to test whether an object is exact is

? isexact(x) = precision(x) == precision(0)

If n is not omitted, creates a new object equal to x with a new "precision" n. (This never changes the type of the result. In particular it is not possible to use it to obtain a polynomial from a power series; tor that, see truncate.) Now the meaning of precision is different from the above (floating point accuracy), and depends on the type of x:

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: yes, this is inconsistent.) Note that the precision is a priori distinct from the exponent k appearing in O(*^k); it is indeed equal to k if and only if x is a p-adic or X-adic unit.

? precision(1 + O(x), 10)
 %1 = 1 + O(x^10)
 ? precision(x^2 + O(x^10), 3)
 %2 = x^2 + O(x^5)
 ? precision(7^2 + O(7^10), 3)
 %3 = 7^2 + O(7^5)

For the last two examples, note that x^2 + O(x^5) = x^2(1 + O(x^3)) indeed has 3 significant coefficients

The library syntax is GEN precision0(GEN x, long n). Also available are GEN gprec(GEN x, long n) and long precision(GEN x). In both, the accuracy is expressed in words (32-bit or 64-bit depending on the architecture).




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

returns a random element in various natural sets depending on the argument N.

* t_INT: returns an integer uniformly distributed between 0 and N-1. Omitting the argument is equivalent to random(2^31).

* t_REAL: returns a real number in [0,1[ with the same accuracy as N (whose mantissa has the same number of significant words).

* t_INTMOD: returns a random intmod for the same modulus.

* t_FFELT: returns a random element in the same finite field.

* t_VEC generated by ellinit over a finite field k (coefficients are t_INTMODs modulo a prime or t_FFELTs): returns a random k-rational affine point on the curve. More precisely an abscissa is drawn uniformly at random until ellordinate succeeds. In particular, the curves over F_2 with a single point (at infinity!) will trigger an infinite loop. Note that this is definitely not a uniform distribution over E(k).

* t_POL return a random polynomial of degree at most the degree of N. The coefficients are drawn by applying random to the leading coefficient of N.

? random(10)
 %1 = 9
 ? random(Mod(0,7))
 %2 = Mod(1, 7)
 ? a = ffgen(ffinit(3,7), 'a); random(a)
 %3 = a^6 + 2*a^5 + a^4 + a^3 + a^2 + 2*a
 ? E = ellinit([0,0,0,3,7]*Mod(1,109)); random(E)
 %4 = [Mod(103, 109), Mod(10, 109)]
 ? E = ellinit([0,0,0,1,7]*a^0); random(E)
 %5 = [a^6 + a^5 + 2*a^4 + 2*a^2, 2*a^6 + 2*a^4 + 2*a^3 + a^2 + 2*a]
 ? random(Mod(1,7)*x^4)
 %6 = Mod(5, 7)*x^4 + Mod(6, 7)*x^3 + Mod(2, 7)*x^2 + Mod(2, 7)*x + Mod(5, 7)
 

These variants all depend on a single internal generator, and are independent from the system's random number generator. A random seed may be obtained via getrand, and reset using setrand: from a given seed, and given sequence of randoms, the exact same values will be generated. The same seed is used at each startup, reseed the generator yourself if this is a problem.

Technical note. Up to version 2.4 included, the internal generator produced pseudo-random numbers by means of linear congruences, which were not well distributed in arithmetic progressions. We now use Brent's XORGEN algorithm, based on Feedback Shift Registers, see http://wwwmaths.anu.edu.au/~brent/random.html. The generator has period 2^{4096}-1, passes the Crush battery of statistical tests of L'Ecuyer and Simard, but is not suitable for cryptographic purposes: one can reconstruct the state vector from a small sample of consecutive values, thus predicting the entire sequence.

The library syntax is GEN genrand(GEN N = NULL).

Also available: GEN ellrandom(GEN E) and GEN ffrandom(GEN a).




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 GEN greal(GEN x).




round(x,{&e})

If x is in R, rounds x to the nearest integer (rounding to + oo in case of ties), then 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. 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 an approximate computation, when theory tells you that the coefficients must be integers.

The library syntax is GEN round0(GEN x, GEN *e = NULL). Also available are GEN grndtoi(GEN x, long *e) and GEN ground(GEN x).




simplify(x)

this function simplifies x as much as it can. Specifically, a complex or quadratic number whose imaginary part is the integer 0 (i.e.not Mod(0,2) 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 = 2 + y - y
 %1 = 2
 ? isprime(x)
   ***   at top-level: isprime(x)
   ***                 ^----------
   *** isprime: 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 GEN simplify(GEN x).




sizebyte(x)

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

The library syntax is long gsizebyte(GEN x). Also available is long gsizeword(GEN x) returning a number of words.




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 long sizedigit(GEN x).




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 GEN trunc0(GEN x, GEN *e = NULL). The following functions are also available: GEN gtrunc(GEN x) and GEN gcvtoi(GEN x, long *e).




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 LONG_MAX (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 long ggval(GEN x, GEN p).




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. If x is omitted, returns the list of user variables known to the interpreter, by order of decreasing priority. (Highest priority is x, which always come first.)

The library syntax is GEN gpolvar(GEN x = NULL). However, in library mode, this function should not be used for x non- NULL, since gvar is more appropriate. Instead, for x a p-adic (type t_PADIC), p is gel(x,2); otherwise, use long gvar(GEN x) which returns the variable number of x if it exists, NO_VARIABLE otherwise, which satisfies the property varncmp( NO_VARIABLE, v) > 0 for all valid variable number v, i.e. it has lower priority than any variable.