Pari/GP Reference Documentation  Contents - Index - Meta commands

Standard monadic or dyadic operators


*   +/-   +   /   <,<,>=,>,==,!=,||,&&,!,|,&,<>   \/   \   ^   divrem   lex   max   shift   shiftmul   sign   vecmax   vecmin  
 
+/-  

The expressions +x and -x refer to monadic operators (the first does nothing, the second negates x).

The library syntax is gneg(x) for -x.

+  

, -: The expression x + y is the sum and x - y is the difference of x and y. Among the prominent impossibilities are addition/subtraction between a scalar type and a vector or a matrix, between vector/matrices of incompatible sizes and between an integermod and a real number.

The library syntax is gadd(x,y) x + y, gsub(x,y) for x - y.

*  

The expression x * y is the product of x and y. Among the prominent impossibilities are multiplication between vector/matrices of incompatible sizes, between an integermod and a real number. Note that because of vector and matrix operations, * is not necessarily commutative. Note also that since multiplication between two column or two row vectors is not allowed, to obtain the scalar product of two vectors of the same length, you must multiply a line vector by a column vector, if necessary by transposing one of the vectors (using the operator ~ or the function mattranspose, see Section (??)).

If x and y are binary quadratic forms, compose them. See also qfbnucomp and qfbnupow.

The library syntax is gmul(x,y) for x * y. Also available is gsqr(x) for x * x (faster of course!).

/  

The expression x / y is the quotient of x and y. In addition to the impossibilities for multiplication, note that if the divisor is a matrix, it must be an invertible square matrix, and in that case the result is x*y^{-1}. Furthermore note that the result is as exact as possible: in particular, division of two integers always gives a rational number (which may be an integer if the quotient is exact) and not the Euclidean quotient (see x \ y for that), and similarly the quotient of two polynomials is a rational function in general. To obtain the approximate real value of the quotient of two integers, add 0. to the result; to obtain the approximate p-adic value of the quotient of two integers, add O(p^k) to the result; finally, to obtain the Taylor series expansion of the quotient of two polynomials, add O(X^k) to the result or use the taylor function (see Section (??)).

The library syntax is gdiv(x,y) for x / y.

\  

The expression x \ y is the

Euclidean quotient of x and y. The types must be either both integer or both polynomials. The result is the Euclidean quotient. In the case of integer division, the quotient is such that the corresponding remainder is non-negative.

The library syntax is gdivent(x,y) for x \ y.

\/  

The expression x \/ y is the Euclidean quotient of x and y. The types must be either both integer or both polynomials. The result is the rounded Euclidean quotient. In the case of integer division, the quotient is such that the corresponding remainder is smallest in absolute value and in case of a tie the quotient closest to + oo is chosen.

The library syntax is gdivround(x,y) for x \/ y.

divrem(x,y)  

creates a column vector with two components, the first being the Euclidean quotient, the second the Euclidean remainder, of the division of x by y. This avoids the need to do two divisions if one needs both the quotient and the remainder. The arguments must be both integers or both polynomials; in the case of integers, the remainder is non-negative.

The library syntax is gdiventres(x,y).

^  

The expression x ^n is powering. If the exponent is an integer, then exact operations are performed using binary (left-shift) powering techniques. In particular, in this case x cannot be a vector or matrix unless it is a square matrix (and moreover invertible if the exponent is negative). If x is a p-adic number, its precision will increase if v_p(n) > 0. PARI is able to rewrite the multiplication x * x of two identical objects as x^2, or sqr(x) (here, identical means the operands are two different labels referencing the same chunk of memory; no equality test is performed). This is no longer true when more than two arguments are involved.

If the exponent is not of type integer, this is treated as a transcendental function (see Section (??)), and in particular has the effect of componentwise powering on vector or matrices.

As an exception, if the exponent is a rational number p/q and x an integer modulo a prime, return a solution y of y^q = x^p if it exists. Currently, q must not have large prime factors.

Beware that

? Mod(7,19)^(1/2)
 %1 = Mod(11, 19)/*is any square root*/
 ? sqrt(Mod(7,19))
 %2 = Mod(8, 19)/*is the smallest square root*/
 ? Mod(7,19)^(3/5)
 %3 = Mod(1, 19)
 ? %3^(5/3)
 %4 = Mod(1, 19)/*Mod(7,19) is just another cubic root*/

The library syntax is gpow(x,n,prec) for x ^n.

shift(x,n)  

or x << n ( = x >> (-n)): shifts x componentwise left by n bits if n >= 0 and right by |n| bits if n < 0. A left shift by n corresponds to multiplication by 2^n. A right shift of an integer x by |n| corresponds to a Euclidean division of x by 2^{|n|} with a remainder of the same sign as x, hence is not the same (in general) as x \ 2^n.

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

shiftmul(x,n)  

multiplies x by 2^n. The difference with shift is that when n < 0, ordinary division takes place, hence for example if x is an integer the result may be a fraction, while for shift Euclidean division takes place when n < 0 hence if x is an integer the result is still an integer.

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

Comparison and boolean operators  

. The six standard comparison operators <= , < , >= , > , == , ! = are available in GP, and in library mode under the names gle, glt, gge, ggt, geq, gne respectively. The library syntax is co(x,y), where co is the comparison operator. The result is 1 (as a GEN) if the comparison is true, 0 (as a GEN) if it is false.

The standard boolean functions || (inclusive or), && (and) and ! (not) are also available, and the library syntax is gor(x,y), gand(x,y) and gnot(x) respectively.

In library mode, it is in fact usually preferable to use the two basic functions which are gcmp(x,y) which gives the sign (1, 0, or -1) of x-y, where x and y must be in R, and gegal(x,y) which can be applied to any two PARI objects x and y and gives 1 (i.e.true) if they are equal (but not necessarily identical), 0 (i.e.false) otherwise. Particular cases of gegal which should be used are gcmp0(x) (x == 0 ?), gcmp1(x) (x == 1 ?), and gcmp_1(x) (x == -1 ?).

Note that gcmp0(x) tests whether x is equal to zero, even if x is not an exact object. To test whether x is an exact object which is equal to zero, one must use isexactzero.

Also note that the gcmp and gegal functions return a C-integer, and not a GEN like gle etc.

GP accepts the following synonyms for some of the above functions: since we thought it might easily lead to confusion, we don't use the customary C operators for bitwise and or bitwise or (use bitand or bitor), hence | and & are accepted as\sidx{bitwise and} synonyms of || and && respectively. Also, < > is accepted as a synonym for ! = . On the other hand, = is definitely not a synonym for == since it is the assignment statement.

lex(x,y)  

gives the result of a lexicographic comparison between x and y. This is to be interpreted in quite a wide sense. For example, the vector [1,3] will be considered smaller than the longer vector [1,3,-1] (but of course larger than [1,2,5]), i.e. lex([1,3], [1,3,-1]) will return -1.

The library syntax is lexcmp(x,y).

sign(x)  

sign (0, 1 or -1) of x, which must be of type integer, real or fraction.

The library syntax is gsigne(x). The result is a long.

max(x,y)  

and min(x,y): creates the maximum and minimum of x and y when they can be compared.

The library syntax is gmax(x,y) and gmin(x,y).

vecmax(x)  

if x is a vector or a matrix, returns the maximum of the elements of x, otherwise returns a copy of x. Returns - oo in the form of -(2^{31}-1) (or -(2^{63}-1) for 64-bit machines) if x is empty.

The library syntax is vecmax(x).

vecmin(x)  

if x is a vector or a matrix, returns the minimum of the elements of x, otherwise returns a copy of x. Returns + oo in the form of 2^{31}-1 (or 2^{63}-1 for 64-bit machines) if x is empty.

The library syntax is vecmin(x).