Pari/GP Reference Documentation | Contents - Global index - GP keyboard shortcuts |
* + +/- - / Comparison and Boolean operators % \ \/ ^ cmp divrem lex max min powers shift shiftmul sign vecmax vecmin | |
+/- | |
The expressions
The library syntax is
| |
+ | |
The expression x
The library syntax is
| |
- | |
The expression x
The library syntax is
| |
* | |
The expression x
Multiplication between two
? a = [1,2,3]; ? a * a *** at top-level: a*a *** ^-- *** _*_: forbidden multiplication t_VEC * t_VEC. ? a * a~ %2 = 14
If x,y are binary quadratic forms, compose them; see also
The library syntax is
| |
/ | |
The expression x
The library syntax is
| |
\ | |
The expression Note that when y is an integer and x a polynomial, y is first promoted to a polynomial of degree 0. When x is a vector or matrix, the operator is applied componentwise.
The library syntax is
| |
\/ | |
The expression x When x is a vector or matrix, the operator is applied componentwise.
The library syntax is
| |
% | |
The expression
? (1/2) % 3 %1 = 2 ? 0.5 % 3 %2 = 0.5000000000000000000000000000 ? (1/2) % 3.0 %3 = 1/2 Note that when y is an integer and x a polynomial, y is first promoted to a polynomial of degree 0. When x is a vector or matrix, the operator is applied componentwise.
The library syntax is
| |
^ | |
The expression x^n is powering.
* If the exponent n is an integer, then exact operations are performed
using binary (left-shift) powering techniques. If x is a p-adic number, its
precision will increase if v_p(n) > 0. Powering a binary quadratic form
(types
PARI is able to rewrite the multiplication x * x of two identical
objects as x^2, or * If the exponent n is not an integer, powering is treated as the transcendental function exp(nlog x), and in particular acts componentwise on vector or matrices, even square matrices ! (See Section se:trans.) * As an exception, if the exponent is a rational number p/q and x an integer modulo a prime or a p-adic number, 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 */
* If the exponent is a negative integer, an inverse must be computed.
For non-invertible
? Mod(4,6)^(-1) *** at top-level: Mod(4,6)^(-1) *** ^----- *** _^_: impossible inverse modulo: Mod(2, 6). (Here, a factor 2 is obtained directly. In general, take the gcd of the representative and the modulus.) This is most useful when performing complicated operations modulo an integer N whose factorization is unknown. Either the computation succeeds and all is well, or a factor d is discovered and the computation may be restarted modulo d or N/d.
For non-invertible
? Mod(x^2, x^3-x)^(-1) *** at top-level: Mod(x^2,x^3-x)^(-1) *** ^----- *** _^_: impossible inverse in RgXQ_inv: Mod(x^2, x^3 - x). Note that the underlying algorihm (subresultant) assumes the base ring is a domain:
? a = Mod(3*y^3+1, 4); b = y^6+y^5+y^4+y^3+y^2+y+1; c = Mod(a,b); ? c^(-1) *** at top-level: Mod(a,b)^(-1) *** ^----- *** _^_: impossible inverse modulo: Mod(2, 4). In fact c is invertible, but ℤ/4ℤ is not a domain and the algorithm fails. It is possible for the algorithm to succeed in such situations and any returned result will be correct, but chances are an error will occur first. In this specific case, one should work with 2-adics. In general, one can also try the following approach
? inversemod(a, b) = { my(m, v = variable(b)); m = polsylvestermatrix(polrecip(a), polrecip(b)); m = matinverseimage(m, matid(#m)[,1]); Polrev(m[1..poldegree(b)], v); } ? inversemod(a,b) %2 = Mod(2,4)*y^5 + Mod(3,4)*y^3 + Mod(1,4)*y^2 + Mod(3,4)*y + Mod(2,4)
This is not guaranteed to work either since
For a
? x = Mat([1;2]) %1 = [1] [2] ? x^(-1) %2 = [1 0]
The library syntax is
| |
cmp | |
Gives the result of a comparison between arbitrary objects x and y
(as -1, 0 or 1). The underlying order relation is transitive,
the function returns 0 if and only if x In case all components are equal up to the smallest length of the operands, the more complex is considered to be larger. More precisely, the longest is the largest; when lengths are equal, we have matrix > vector > scalar. For example:
? cmp(1, 2) %1 = -1 ? cmp(2, 1) %2 = 1 ? cmp(1, 1.0) \\ note that 1 == 1.0, but (1===1.0) is false. %3 = -1 ? cmp(x + Pi, []) %4 = -1
This function is mostly useful to handle sorted lists or
vectors of arbitrary objects. For instance, if v is a vector, the
construction
The library syntax is
| |
divrem | |
Creates a column vector with two components, the first being the Euclidean
quotient (
Beware that
? divrem(1/2, 3)[2] %1 = 1/2 ? (1/2) % 3 %2 = 2 ? divrem(Mod(2,9), 3)[2] *** at top-level: divrem(Mod(2,9),3)[2 *** ^-------------------- *** forbidden division t_INTMOD \ t_INT. ? Mod(2,9) % 6 %3 = Mod(2,3)
The library syntax is
| |
lex | |
Gives the result of a lexicographic comparison between x and y (as -1, 0 or 1). This is to be interpreted in quite a wide sense: It is admissible to compare objects of different types (scalars, vectors, matrices), provided the scalars can be compared, as well as vectors/matrices of different lengths. The comparison is recursive. In case all components are equal up to the smallest length of the operands, the more complex is considered to be larger. More precisely, the longest is the largest; when lengths are equal, we have matrix > vector > scalar. For example:
? lex([1,3], [1,2,5]) %1 = 1 ? lex([1,3], [1,3,-1]) %2 = -1 ? lex([1], [[1]]) %3 = -1 ? lex([1], [1]~) %4 = 0
The library syntax is
| |
max | |
Creates the maximum of x and y when they can be compared.
The library syntax is
| |
min | |
Creates the maximum of x and y when they can be compared.
The library syntax is
| |
powers | |
For non-negative n, return the vector with n+1 components
[1,x,...,x^n] if
? powers(Mod(3,17), 4) %1 = [Mod(1, 17), Mod(3, 17), Mod(9, 17), Mod(10, 17), Mod(13, 17)] ? powers(Mat([1,2;3,4]), 3) %2 = [[1, 0; 0, 1], [1, 2; 3, 4], [7, 10; 15, 22], [37, 54; 81, 118]] ? powers(3, 5, 2) %3 = [2, 6, 18, 54, 162, 486]
When n < 0, the function returns the empty vector
The library syntax is
| |
shift | |
Shifts x componentwise left by n bits if n ≥ 0 and right by |n|
bits if n < 0. May be abbreviated as x
The library syntax is
| |
shiftmul | |
Multiplies x by 2^n. The difference with
The library syntax is
| |
sign | |
sign (0, 1 or -1) of x, which must be of
type integer, real or fraction;
The library syntax is
| |
vecmax | |
If x is a vector or a matrix, returns the largest entry of x, otherwise returns a copy of x. Error if x is empty. If v is given, set it to the index of a largest entry (indirect maximum), when x is a vector. If x is a matrix, set v to coordinates [i,j] such that x[i,j] is a largest entry. This flag is ignored if x is not a vector or matrix.
? vecmax([10, 20, -30, 40]) %1 = 40 ? vecmax([10, 20, -30, 40], &v); v %2 = 4 ? vecmax([10, 20; -30, 40], &v); v %3 = [2, 2]
The library syntax is
| |
vecmin | |
If x is a vector or a matrix, returns the smallest entry of x, otherwise returns a copy of x. Error if x is empty. If v is given, set it to the index of a smallest entry (indirect minimum), when x is a vector. If x is a matrix, set v to coordinates [i,j] such that x[i,j] is a smallest entry. This is ignored if x is not a vector or matrix.
? vecmin([10, 20, -30, 40]) %1 = -30 ? vecmin([10, 20, -30, 40], &v); v %2 = 3 ? vecmin([10, 20; -30, 40], &v); v %3 = [2, 1]
The library syntax is
| |
Comparison and Boolean operators | |
The six
standard comparison operators
The extra operator
For the purpose of comparison,
GP accepts
The standard boolean operators
| |