We assume you've installed Macaulay2 and can type

on a command line to bring up the program. You should see something like:

We suggest you do that now, so that you can experiment while you read this tutorial!
You can immediately do arithmetic with integers:





Most Macaulay2 applications involve polynomial rings over fields and their quotient rings. Fields can be made in various ways:




After making k we can compute in it:

Computation is often fastest and needs least memory when performed over finite prime fields of the form $\ZZ/p$. Fortunately, when the characteristic $p$ is not too small, qualitative questions often have similar answers over $\ZZ/p$ and over $\QQ$, so we mostly use the former. In Macaulay2 the prime $p$ can range up to 32749.
We make a polynomial ring in 5 variables over $\ZZ/101$:


Here is another way:

One can do arithmetic on polynomials:

We make an ideal in $S$:

Using this ideal, we can make a factor ring:

Another way to make an ideal, with more compact notation (familiar to anyone who used the classic Macaulay) is:


Note the command ``use S'', which specifies that we want to work with the generators of the polynomial ring S again; otherwise the variables a, b, and c would still have had values in $R$ instead of in $S$.
Algebraic operations on ideals are available:



In case you forget any of these things, help is available! The most useful way to get it is often to type something like:

Then a browser window will pop up that contains documentation about the function ideal that we've been using; links on that page allow one to explore all of the Macaulay2 documentation.
On the other hand, we might have wanted information about the class of all ideals. Not too surprisingly, this class is called Ideal. We could get information about what functions create or use ideals by typing:

To see the names of classes, you can begin by looking at the output of commands; the second line output (the one introduced by a colon) often contains the name of the class of the result.
Here are some basic operations on matrices:





The function entries gives the entries of a matrix:

The result is a list of lists, one for each row of the matrix $M$. The function flatten can be used to merge the lists into a single list:

If you want a particular entry, say the one in the upper left corner, you can use the underscore operator _:

Here, as everywhere in Macaulay2, all indexing starts with 0. For example:

is the first generator of I. You can list all the generators with:

A module can be defined as a cokernel, kernel, image, or even as a subquotient:






Note that the matrix $N$ above was defined with an alternate syntax, parallel to the alternate syntax for ideal.
Before going on, the reader might want to explore a bit. A good place to start is the top of the documentation tree, which can be reached, for example, by typing:

To compute the Gröbner basis of an ideal $(x^2y,xy^2+x^3)$ in the polynomial ring in four variables we proceed as follows. First we make our favorite field:

Then the polynomial ring:

And then the ideal:

Now the punch line. We compute the Gröbner basis with the groebnerBasis function:

Gr\"obner bases are always computed with respect to a particular monomial order on the ring. In fact, the ring we defined above has a default monomial order, the graded reverse lex order. For many other possibilities, see MonomialOrder, or type:

The analogue of factorization in the theory of ideals is primary decomposition. For example, we can begin by intersecting three ideals:

We can almost undo this operation by computing a primary decomposition:

Inspecting the output, we see that the first two ideals are the same as the first two ideals we intersected, but the third one differs from the corresponding input ideal. This is because only the primary components corresponding to minimal primes (here, the first two) are unique. All three of the input ideals are primary, so they constitute a primary decomposition of $I$ different from the one provided by Macaulay2 on the output line.
For larger examples, primary decomposition is computationally challenging! Sometimes it is easier to compute just the minimal primes. To do this we can use decompose:

Using Gröbner bases we can compute codimensions, dimensions, degrees, Hilbert functions, and Hilbert polynomials. This will be more fun if we work with a meaningful example. We will use the ideal defining the smooth rational quartic curve in $\PP^3$ given parametrically (in an affine representation) by $$t \mapsto{} (t,t^3,t^4).$$ (The reader more interested in algebra than geometry may simply treat the ideal given below as a gift from the gods.) First we make the polynomial ring in 4 variables, to serve as the homogeneous coordinate ring of $\PP^3$:

We introduce the ring map $\phi: R \to kk[s,t]$ defined by $(a,b,c,d) \mapsto{} (s^4, s^3 t, s t^3, t^4)$:

Here the syntax of the function map has the target ring first and the source ring second: maps in Macaulay2 generally go from right to left! The last input to the command is a list of the elements to which to send the variables of the source ring. The ideal we want is the kernel of this map:

Shortcut notation for this construction is provided by the function monomialCurveIdeal:

We can compute the dimension, codimension (also called the height) and degree of this ideal:



The Hilbert polynomial is obtained with the function hilbertPolynomial:

The output above may not be what the user expected: the term ${\mathbf P}_m$ represents the Hilbert polynomial of projective $m$space. Thus the output tells us that the Hilbert polynomial of $M$ is $i \mapsto{} 3*1+4*(i+1) = 4i + 1$. Thus the degree is four, the dimension of the projective variety that is the support of $M$ is 1 (and so the affine dimension is 2), and the (arithmetic) genus is 0 (obtained as 1 minus the constant term of the polynomial.)
The more usual expression for the Hilbert polynomial can be obtained as follows:

The construction Projective => false is our first example of an option to a function: we specified that the option Projective was to have the value false. The form we used first could also have been written this way:

The Hilbert series of $M$ (the generating function for the dimensions of the graded pieces of $M$) is obtained with:

This generating function is expressed as a rational function with denominator equal to (1T)^n, where n is the number of variables in R. Since R/I has dimension 2, it can also be written with denominator (1t)^2. To see it in this form, use reduceHilbert:

It is possible to manipulate the numerator and denominator of this expression. To learn how to do so, see hilbertSeries or type:

A great deal of subtle information about a module is visible using free resolutions. For an example, we begin by turning $R/I$ into a module. Here the code R^1 produces the free module of rank 1 over $R$, and res computes a free resolution:


To get more precise information about Mres, we could compute its Betti table with betti:

The display is chosen for compactness. Each column of the table corresponds to a free module in the resolution. The column's heading specifies the homological degree (the position of the free module in the resolution). The entry just below the homological degree is the rank of the free module, also called the total betti number. The remaining entries in the column tell us how many generators of each degree this free module has: the number in the column labelled $j$ and in the row labelled $d$ tells how many generators of degree $j+d$ the $j$th free module has. Thus, in our case, the single generator of the third (and last) free module in the resolution has degree $3+2=5$.
Commonly computed homological invariants such as projective dimension and regularity are (also) available directly:


A major application of Gröbner bases is to give the normal form for an element modulo an ideal, allowing one, for example, to decide whether the element is in the ideal. For example, we can decide which power of the trace of a generic 3x3 matrix is expressible in terms of the entries of the cube of the matrix with the following code:



This gives the ideal of entries of the matrix. In the expression ``M = genericMatrix(R,a,3,3)'' the arguments ``R,a,3,3'' specify the ring, the first variable to use, and the numbers of rows and columns desired.


The expression ``Tr^p % I'' computes the normal form for the pth power of the trace Tr with respect to the Gröbner basis of I. The expression ``for p from 1 to 10 do'' specifies a for loop that executes the following expression, ``print (Tr^p % I)'', with 10 consecutive values of p. For more information on such loops see for or type:

Here we have put quotes around ``for'' because ``for'' is a keyword in the Macaulay2 language. (In general, it's always safe to use quotes with viewHelp.)
We see from the output of these commands that the 6th power of the trace is NOT in the ideal of entries of the cube of M, but the 7th power is. We can compute the coefficients in the expression for it using the division algorithm, denoted in this setting by //:

Consider the problem of projecting the ``twisted cubic'', a curve in $\PP^3$ defined by the three $2 \times{} 2$ minors of a certain $2 \times{} 3$ matrix. We already have the simplest tools for solving such a problem. We first clear the earlier meaning of x to allow it to be used as a subscripted variable:

Since we are going to deal with a curve in $\PP^3$, we begin with a polynomial ring in four variables:

The ideal of the twisted cubic curve is generated by the $2 \times{} 2$ minors of a ``catalecticant" or ``Hankel" matrix, conveniently defined as follows:


As projection center we take the point with homogeneous coordinates $(1,0,0,1)$, which is defined by the ideal:

The ideal J of the image of the curve under the projection from this point is the kernel of the ring map $S=kk[u,v,w] \to R/I$ sending the variables of S to the generators of pIdeal, regarded as elements of $R/I$. This is the same as the more usual formulation: $$J = I \cap{} kk[x_0+x_3, x_1, x_x]$$ To compute this we first substitute pIdeal into $R/I$, and then form the necessary ring map:




The ideal J defines a curve with one singular point. We can compute the ideal of the singular locus with:

This doesn't look like the ideal of a reduced point! But that's because it isn't yet saturated:

We have just seen the saturate function in its most common use: to saturate with respect to the maximal ideal. but we can also find the saturation of any ideal with respect to another:

We can also take the ``ideal quotient'' I:J of an ideal I with respect to another, J defined as the set of elements f such that f*J is contained in I:

It is easy to define your own functions in Macaulay2, and this can save a lot of typing. Functions are defined with the symbol >. For example, the famous Collatz Conjecture (also called the ``hailstone problem'') asks about the following procedure: given an integer $n$, divide it by 2 if possible, or else multiply by 3 and add 1. If we repeat this over and over, does the process always reach 1? Here is a function that performs the Hailstone procedure again and again, producing a list of the intermediate results.

For example:



In order to understand a process it is often useful to tabulate the results of applying it many times. One feature of the Collatz process is how many steps it takes to get to 1. We can tabulate this statistic for the first 25 values of n with the function tally, as follows:

A line of the form

in the result means that a Collatz sequence of length 18 was seen 3 times. To see the successive ``recordbreakers'', that is, the numbers with longer Collatz sequences than any number before them, we might try:


If you want to see a list of just the successive records, you can apply the function last to each element of the list $L$. A convenient way to do this is with this syntax:

Note that in writing functions of more than one expression (usually there's one expression per line), the expressions must be separated by semicolons. For example in the ``for'' loop above, the first expression was ``l = length Collatz n''. After the last expression of an input line or of a function body, a semicolon suppresses output, useful when the output would be large.
There are many packages of readymade functions available for your use, many written by other users (perhaps you'll contribute one someday!) A list of ``installed'' packages can be found with:

For example, there is a package called EdgeIdeals. To load the package, use:

After loading it, you can view its documentation with

or you can call its functions, such as randomGraph and edgeIdeal:





When testing a conjecture one sometimes wants to run a large number of randomly chosen examples. Here's some typical code that one might use to study a random graph ideal. First we use ``for ... list ...'' to construct a list L and suppress its printing by ending the line that creates it with a ``;''. Each entry of L is a triple consisting of the codimension, degree, and Betti table of a random graph ideal on 10 vertices having only 4 edges.


We can use tally to find out how many examples were found with each combination of codimension and degree and Betti table.

We can determine how many distinct patterns were found:

Macaulay2 can compute the homology of complexes; for example, let's compute the homology of a Koszul complex that is not a resolution: $$ {\mathbf K}(x^2, x y^2):\ \ 0 \rightarrow{} S(5) \rightarrow{} S(2)\oplus S(3) \rightarrow{} S \rightarrow 0 $$ The free module $S(2) \oplus{} S(3)$ can be defined with this syntax:

Here is how we can define the maps in the Koszul complex:



Let's check that this is will really make a complex:

To get the homology we can, for example compute:

We could also use the data type ChainComplex and use a builtin facility to take homology (in our case $H_1$):




Either way, the first homology is $((x^2):(xy^2)) / (x^2) \cong{} S/(x)$, in accord with general theory.
There are other ways to construct Koszul complexes. One way is as the tensor product of chain complexes of length 1:


Another way is by using the function koszul, designed for that purpose:


Since Macaulay2 can compute resolutions and homology, it can compute things such as $Ext$, $Tor$ and sheaf cohomology, as in the following examples. The first uses Serre's formula to compute the multiplicity with which a 2plane meets the union of two 2planes in 4space (this is the first case in which the length of the intersection scheme is NOT the right answer.) The notation ``M**N'' denotes the tensor product of the modules $M$ and $N$. We use the syntactical forms ``for j from 0 to 4 list ...'' to list some results and ``sum(0..4, j > ...)'' to sum some results.






Similarly, we can compute Hom and Ext:


or the cohomology of the sheaf associated to a module.
Here is how to compute the first cohomology of the structure sheaf twisted by $2$ of the curve $Proj(S/IX)$, which in this case is the disjoint union of two lines in $\PP^3$:

This documentation describes version 1.0 of BeginningMacaulay2.
The source code from which this documentation is derived is in the file BeginningMacaulay2.m2. The auxiliary files accompanying it are in the directory BeginningMacaulay2/.
The object BeginningMacaulay2 is a package.