# a first Macaulay2 session

Your first input prompt will be i1 : . In response to the prompt, type 2+2 and press return. The expression you entered will be evaluated - no punctuation is required at the end of the line.

 i1 : 2+2 o1 = 4
The answer is displayed to the right of the output label o1 =.

Here is some arithmetic with fractions.

 i2 : 3/5 + 7/11 68 o2 = -- 55 o2 : QQ

Notice the additional line of output labeled with o2 :. Output lines labeled with colons provide information about the type of output. In this case, the symbol QQ is our notation for the class of all rational numbers, and indicates that the answer on the previous line is a rational number.

Multiplication is indicated with *.

 i3 : 1*2*3*4 o3 = 24
Powers are obtained with ^.
 i4 : 2^200 o4 = 1606938044258990275541962092341162602522202993782792835301376
Factorials are obtained with !.
 i5 : 40! o5 = 815915283247897734345611269596115894272000000000
Because some answers can be very long, it is a good idea to run the program in a window that does not wrap output lines, and allows the user to scroll left horizontally to see the rest of the output. (See using Macaulay2 with emacs.)
 i6 : 100! o6 = 933262154439441526816992388562667004907159682643816214685929638952175999 932299156089414639761565182862536979208272237582511852109168640000000000 00000000000000
Multiple expressions may be separated by semicolons.
 i7 : 1;2;3*4 o9 = 12
A semicolon at the end of the line suppresses the printing of the value.
 i10 : 4*5;
The output from the previous line can be obtained with oo, even if a semicolon prevented it from being printed.
 i11 : oo o11 = 20
Lines before that can be obtained with ooo and oooo. Alternatively, the symbol labeling an output line can be used to retrieve the value, as in the following example.
 i12 : o5 + 1 o12 = 815915283247897734345611269596115894272000000001
To enter a string, use quotation marks.
 i13 : "hi there" o13 = hi there
A value can be assigned to a variable with =.
 i14 : s = "hi there" o14 = hi there
Strings may be concatenated horizontally with |, (see String | String).
 i15 : s | " - " | s o15 = hi there - hi there
or vertically with ||, (see Net || Net).
 i16 : s || " - " || s o16 = hi there - hi there
A list of expressions can be formed with braces.
 i17 : {1, 2, s} o17 = {1, 2, hi there} o17 : List
Lists behave like vectors.
 i18 : 10*{1,2,3} + {1,1,1} o18 = {11, 21, 31} o18 : List
A function can be created with the arrow operator, -> .
 i19 : f = i -> i^3 o19 = f o19 : FunctionClosure
To evaluate a function, place its argument to the right of the function.
 i20 : f 5 o20 = 125
Functions of more than one variable take a parenthesized sequence of arguments.
 i21 : g = (x,y) -> x * y o21 = g o21 : FunctionClosure i22 : g(6,9) o22 = 54
The function apply can be used to apply a function to each element of a list.
 i23 : apply({1,2,3,4}, i -> i^2) o23 = {1, 4, 9, 16} o23 : List i24 : apply({1,2,3,4}, f) o24 = {1, 8, 27, 64} o24 : List
The operator .. may be used to generate sequences of consecutive numbers.
 i25 : apply(1 .. 4, f) o25 = (1, 8, 27, 64) o25 : Sequence
If the first argument to apply is an integer n then it stands for the list {0, 1, ..., n-1}.
 i26 : apply(5, f) o26 = {0, 1, 8, 27, 64} o26 : List
The function scan is analogous to apply except that no value is returned. It may be used to implement loops in programs.
 i27 : scan(5, i -> print (i, i^3)) (0, 0) (1, 1) (2, 8) (3, 27) (4, 64) i28 : j=1; scan(10, i -> j = 2*j); j o30 = 1024
Most computations with polynomials take place in rings that may be specified in usual mathematical notation.
 i31 : R = ZZ/5[x,y,z];
(We reserve single letter symbols such as Z for use as variables in rings, hence we must use something like ZZ to stand for the ring of integers. It may remind you of the "blackboard bold" font of AMSTeX. If you prefer Z to ZZ, you may put Z=ZZ in your initialization file. The symbols ZZ/5 represent the quotient ring Z/5Z, and then ZZ/5[x,y,z] represents the ring of polynomials in the variables x,y, and z with coefficients in the ring Z/5Z.)
 i32 : (x+y)^5 5 5 o32 = x + y o32 : R
Rings and certain other types of things acquire the name of the global variable they are assigned to.
 i33 : R o33 = R o33 : PolynomialRing
To see the original description of a ring, use describe.
 i34 : describe R ZZ o34 = --[x..z, Degrees => {3:1}, Heft => {1}, MonomialOrder => {MonomialSize => 32}, DegreeRank => 1] 5 {GRevLex => {3:1} } {Position => Up }
A free module can be created as follows.
 i35 : F = R^3 3 o35 = R o35 : R-module, free
The i-th basis element of F can be obtained as F_i. In this example, the valid values for i are 0, 1, and 2.
 i36 : F_1 o36 = | 0 | | 1 | | 0 | 3 o36 : R
Using a list of indices instead will produce the homomorphism corresponding to the basis vectors indicated.
 i37 : F_{1,2} o37 = | 0 0 | | 1 0 | | 0 1 | 3 2 o37 : Matrix R <--- R
Repetitions are allowed.
 i38 : F_{2,1,1} o38 = | 0 0 0 | | 0 1 1 | | 1 0 0 | 3 3 o38 : Matrix R <--- R
We can create a homomorphism between free modules with matrix by providing the list of rows of the matrix, each of which is in turn a list of ring elements.
 i39 : f = matrix {{x,y,z}} o39 = | x y z | 1 3 o39 : Matrix R <--- R
Use image to get the image of f.
 i40 : image f o40 = image | x y z | 1 o40 : R-module, submodule of R
We may use ideal to produce the corresponding ideal.
 i41 : ideal (x,y,z) o41 = ideal (x, y, z) o41 : Ideal of R
We may use kernel to compute the kernel of f.
 i42 : kernel f o42 = image {1} | -y 0 -z | {1} | x -z 0 | {1} | 0 y x | 3 o42 : R-module, submodule of R
The answer comes out as a module that is expressed as the image of a homomorphism whose matrix is displayed. Integers inside braces to the left of the matrix give the degrees of the basis elements of the target of the matrix; they are omitted if the degrees are all zero. In case the matrix itself is desired, it can be obtained with generators, as follows.
 i43 : generators oo o43 = {1} | -y 0 -z | {1} | x -z 0 | {1} | 0 y x | 3 3 o43 : Matrix R <--- R
We may use poincare to compute the Poincare polynomial.
 i44 : poincare kernel f 2 3 o44 = 3T - T o44 : ZZ[T]
We may use rank to compute the rank.
 i45 : rank kernel f o45 = 2
A presentation for the kernel can be obtained with presentation.
 i46 : presentation kernel f o46 = {2} | z | {2} | x | {2} | -y | 3 1 o46 : Matrix R <--- R
We can produce the cokernel with cokernel; no computation is performed.
 i47 : cokernel f o47 = cokernel | x y z | 1 o47 : R-module, quotient of R
The direct sum is formed with Module ++ Module.
 i48 : N = kernel f ++ cokernel f o48 = subquotient ({1} | -y 0 -z 0 |, {1} | 0 0 0 |) {1} | x -z 0 0 | {1} | 0 0 0 | {1} | 0 y x 0 | {1} | 0 0 0 | {0} | 0 0 0 1 | {0} | x y z | 4 o48 : R-module, subquotient of R
The answer is expressed in terms of the subquotient function, which produces subquotient modules. Each subquotient module is accompanied by its matrix of generators and its matrix of relations. These matrices can be recovered with generators and relations.
 i49 : generators N o49 = {1} | -y 0 -z 0 | {1} | x -z 0 0 | {1} | 0 y x 0 | {0} | 0 0 0 1 | 4 4 o49 : Matrix R <--- R i50 : relations N o50 = {1} | 0 0 0 | {1} | 0 0 0 | {1} | 0 0 0 | {0} | x y z | 4 3 o50 : Matrix R <--- R
The function prune can be used to convert a subquotient module to a quotient module.
 i51 : prune N o51 = cokernel {2} | 0 0 0 z | {2} | 0 0 0 x | {2} | 0 0 0 -y | {0} | z y x 0 | 4 o51 : R-module, quotient of R
We can use resolution to compute a projective resolution of the cokernel of f.
 i52 : C = resolution cokernel f 1 3 3 1 o52 = R <-- R <-- R <-- R <-- 0 0 1 2 3 4 o52 : ChainComplex
To see the differentials we examine 'C.dd'.
 i53 : C.dd 1 3 o53 = 0 : R <------------- R : 1 | x y z | 3 3 1 : R <-------------------- R : 2 {1} | -y -z 0 | {1} | x 0 -z | {1} | 0 x y | 3 1 2 : R <-------------- R : 3 {2} | z | {2} | -y | {2} | x | 1 3 : R <----- 0 : 4 0 o53 : ChainComplexMap
We can verify that C is a complex by squaring the differential map.
 i54 : C.dd^2 == 0 o54 = true
We can use betti to see the degrees of the components of C.
 i55 : betti C 0 1 2 3 o55 = total: 1 3 3 1 0: 1 3 3 1 o55 : BettiTally
Let's try a harder example. We can use vars to create a sequence of variables.
 i56 : R = ZZ/101[a .. r];
We use genericMatrix to make a 3 by 6 generic matrix whose entries are drawn from the variables of the ring R.
 i57 : g = genericMatrix(R,a,3,6) o57 = | a d g j m p | | b e h k n q | | c f i l o r | 3 6 o57 : Matrix R <--- R
Then we construct its cokernel with cokernel.
 i58 : M = cokernel g o58 = cokernel | a d g j m p | | b e h k n q | | c f i l o r | 3 o58 : R-module, quotient of R
We may use resolution to produce a projective resolution of it, and time to report the time required.
 i59 : time C = resolution M -- used 0.0032851 seconds 3 6 15 18 6 o59 = R <-- R <-- R <-- R <-- R <-- 0 0 1 2 3 4 5 o59 : ChainComplex
As before, we may examine the degrees of its components, or display it.
 i60 : betti C 0 1 2 3 4 o60 = total: 3 6 15 18 6 0: 3 6 . . . 1: . . . . . 2: . . 15 18 6 o60 : BettiTally
We can make a polynomial ring with 18 IndexedVariables.
 i61 : S = ZZ/101[t_1 .. t_9, u_1 .. u_9];
We can use genericMatrix to pack the variables into 3-by-3 matrices.
 i62 : m = genericMatrix(S, t_1, 3, 3) o62 = | t_1 t_4 t_7 | | t_2 t_5 t_8 | | t_3 t_6 t_9 | 3 3 o62 : Matrix S <--- S i63 : n = genericMatrix(S, u_1, 3, 3) o63 = | u_1 u_4 u_7 | | u_2 u_5 u_8 | | u_3 u_6 u_9 | 3 3 o63 : Matrix S <--- S
We may look at the matrix product.
 i64 : m*n o64 = | t_1u_1+t_4u_2+t_7u_3 t_1u_4+t_4u_5+t_7u_6 t_1u_7+t_4u_8+t_7u_9 | | t_2u_1+t_5u_2+t_8u_3 t_2u_4+t_5u_5+t_8u_6 t_2u_7+t_5u_8+t_8u_9 | | t_3u_1+t_6u_2+t_9u_3 t_3u_4+t_6u_5+t_9u_6 t_3u_7+t_6u_8+t_9u_9 | 3 3 o64 : Matrix S <--- S
Let's produce the equations generated by the equations that assert that m and n commute with each other. (See flatten.)
 i65 : j = flatten(m*n - n*m) o65 = | t_4u_2+t_7u_3-t_2u_4-t_3u_7 t_2u_1-t_1u_2+t_5u_2+t_8u_3-t_2u_5-t_3u_8 ----------------------------------------------------------------------- t_3u_1+t_6u_2-t_1u_3+t_9u_3-t_2u_6-t_3u_9 ----------------------------------------------------------------------- -t_4u_1+t_1u_4-t_5u_4+t_4u_5+t_7u_6-t_6u_7 -t_4u_2+t_2u_4+t_8u_6-t_6u_8 ----------------------------------------------------------------------- -t_4u_3+t_3u_4+t_6u_5-t_5u_6+t_9u_6-t_6u_9 ----------------------------------------------------------------------- -t_7u_1-t_8u_4+t_1u_7-t_9u_7+t_4u_8+t_7u_9 ----------------------------------------------------------------------- -t_7u_2-t_8u_5+t_2u_7+t_5u_8-t_9u_8+t_8u_9 -t_7u_3-t_8u_6+t_3u_7+t_6u_8 ----------------------------------------------------------------------- | 1 9 o65 : Matrix S <--- S
Let's compute a Gröbner basis for the image of j with gb.
 i66 : gb j o66 = GroebnerBasis[status: done; S-pairs encountered up to degree 5] o66 : GroebnerBasis
The resulting Gröbner basis contains a lot of information. We can get the generators of the basis, and even though we call upon gb again, the computation will not be repeated.
 i67 : generators gb j; 1 26 o67 : Matrix S <--- S
The semicolon prevents the matrix of generators from appearing on the screen, but the class of the matrix appears -- we see that there are 26 generators.

We can use betti to see the degrees involved in the Gröbner basis.

 i68 : betti gb j 0 1 o68 = total: 1 26 0: 1 . 1: . 8 2: . 12 3: . 5 4: . 1 o68 : BettiTally