Another way to start Macaulay 2 is to run it in an emacs buffer. See running Macaulay 2 in emacs.
An expression entered at the keyboard will be evaluated -- no punctuation is required at the end of the line.
i1 = 2+2
o1 = 4
The answer, 4, is displayed.
Multiplication is indicated with *.
i2 = 1*2*3*4
o2 = 24
Powers are obtained with ^. i3 = 2^200
o3 = 1606938044258990275541962092341162602522202993782792835301376
Factorials are obtained with !. i4 = 40!
o4 = 815915283247897734345611269596115894272000000000
Because some answers can be very long, it is a good idea to run the
program in a window which does not wrap output lines, and allows the
user to scroll left horizontally to see the rest of the output. (See
emacs.) i5 = 100!
o5 = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
Here is some arithmetic with fractions. i6 = 3/5 + 7/11
68
o6 = --
55
o6 : QQ
Multiple expressions may be separated by semicolons. (See ;.) 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. i12 = o4 + 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 with |. i15 = s | " - " | s
o15 = "hi there - hi there"
A list of expressions can be formed with braces.
(See lists, arrays, and sequences.) i16 = {1, 2, s}
o16 = {1,2,"hi there"}
o16 : List
Lists behave like vectors. i17 = 10*{1,2,3} + {1,1,1}
o17 = {11,21,31}
o17 : List
A function can be created with the arrow operator, -> . i18 = f = i -> i^3
o18 = f
o18 : Function
To evaluate a function, place its argument to the right of the
function. i19 = f 5
o19 = 125
Functions of more than one variable take a parenthesized sequence of
arguments. i20 = g = (x,y) -> x * y
o20 = g
o20 : Function
i21 = g(6,9)
o21 = 54
The function apply can be used to apply a function to each
element of a list. i22 = apply({1,2,3,4}, i -> i^2)
o22 = {1,4,9,16}
o22 : List
i23 = apply({1,2,3,4}, f)
o23 = {1,8,27,64}
o23 : List
The operator .. may be used to generate sequences of
consecutive numbers. i24 = apply(1 .. 4, f)
o24 = 1,8,27,64
o24 : Sequence
If the first argument to apply
is an integer n
then
it stands for the list {0, 1, ..., n-1}
. i25 = apply(5, f)
o25 = {0,1,8,27,64}
o25 : List
The function scan is analogous to apply except
that no value is returned. It may be used to implement loops in
programs. i26 = scan(5, i -> print (i, i^3))
0,0
1,1
2,8
3,27
4,64 i27 = j=1; scan(10, i -> j = 2*j); j
o29 = 1024
Most computations with polynomials take place in rings that may be
specified in usual mathematical notation. i30 = R = ZZ/5[x,y,z]
o30 = R
o30 : PolynomialRing
(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; protect quote Z
in your
initialization file.) i31 = (x+y)^5
5 5
o31 = x + y
o31 : R
Rings with long names (not precomputed and stored
in R.name
) acquire the name of the global variable
they are assigned to. i32 = R
o32 = R
o32 : PolynomialRing
To see the description of a ring, use describe. i33 = describe R
ZZ/5[x,y,z]A free module can be created as follows. i34 = F = R^3
3
o34 = R
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. i35 = F_1
o35 = <1>
3
o35 : R
Using a list of indices instead will produce the homomorphism corresponding
to the basis vectors indicated. i36 = F_{1,2}
o36 = | 0 0 |
| 1 0 |
| 0 1 |
3 2
o36 : Matrix R <--- R
Repetitions are allowed. i37 = F_{2,1,1}
o37 = | 0 0 0 |
| 0 1 1 |
| 1 0 0 |
3 3
o37 : 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. i38 = f = matrix {{x,y,z}}
o38 = | x y z |
1 3
o38 : Matrix R <--- R
Use image to get the image of f. i39 = image f
o39 = image | x y z |
1
R - module, submodule of R
A short cut applicable to such one-rowed matrices is ideal. i40 = ideal (x,y,z)
o40 = ideal | x y z |
o40 : Ideal
We may use kernel to compute the kernel of f. i41 = kernel f
o41 = image | 0 -y -z |
| -z x 0 |
| y 0 x |
3
R - module, submodule of R
The answer comes out as a module which is expressed as the image of
a homomorphism whose matrix is displayed. In case the matrix itself
is desired, it can be obtained with generators. i42 = generators oo
o42 = | 0 -y -z |
| -z x 0 |
| y 0 x |
3 3
o42 : Matrix R <--- R
We may use poincare to compute the Poincare polynomial. i43 = poincare kernel f
3 2
o43 = - $T + 3 $T
o43 : ZZ[ZZ^1]
We may use rank to compute the rank. i44 = rank kernel f
o44 = 2
A presentation for the kernel can be obtained with presentation. i45 = presentation kernel f
o45 = | x |
| z |
| -y |
3 1
o45 : Matrix R <--- R
We can produce the cokernel with cokernel; no computation is
performed. i46 = cokernel f
o46 = cokernel | x y z |
1
R - module, quotient of R
The direct sum is formed with ++. i47 = N = kernel f ++ cokernel f
o47 = subquotient(| 0 -y -z 0 |,| 0 0 0 |)
| -z x 0 0 | | 0 0 0 |
| y 0 x 0 | | 0 0 0 |
| 0 0 0 1 | | x y z |
4
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. i48 = generators N
o48 = | 0 -y -z 0 |
| -z x 0 0 |
| y 0 x 0 |
| 0 0 0 1 |
4 4
o48 : Matrix R <--- R
i49 = relations N
o49 = | 0 0 0 |
| 0 0 0 |
| 0 0 0 |
| x y z |
4 3
o49 : Matrix R <--- R
The function prune can be used to convert a subquotient
module to a quotient module. i50 = prune N
o50 = cokernel | 0 0 0 -y |
| 0 0 0 z |
| 0 0 0 x |
| z y x 0 |
4
R - module, quotient of R
We can use resolution to compute a projective resolution of the
cokernel of f. i51 = C = resolution cokernel f
1 3 3 1
o51 = R <-- R <-- R <-- R
0 1 2 3
o51 : ChainComplex
To see the differentials we examine 'C.dd'. i52 = C.dd
1 3
o52 = 1: R <--| x y z |-- R
3 3
2: R <--| -y -z 0 |-- R
| x 0 -z |
| 0 x y |
3 1
3: R <--| z |-- R
| -y |
| x |
o52 : ChainComplexMap
We can verify that C
is a complex by squaring the differential map. i53 = C.dd^2 == 0
o53 = true
We can use betti to see the degrees of the components of C. i54 = betti C
total: 1 3 3 1
0: 1 3 3 1Let's try a harder example. We can use vars to create a sequence
of variables. i55 = R = ZZ/101[a .. r]
o55 = R
o55 : PolynomialRing
We use genericMatrix to make a 3 by 6 generic matrix whose
entries are drawn from the variables of the ring R. i56 = g = genericMatrix(R,a,3,6)
o56 = | a d g j m p |
| b e h k n q |
| c f i l o r |
3 6
o56 : Matrix R <--- R
Then we construct its cokernel with cokernel. i57 = M = cokernel g
o57 = cokernel | a d g j m p |
| b e h k n q |
| c f i l o r |
3
R - module, quotient of R
We may use resolution to produce a projective resolution of it, and
time to report the time required. i58 = time C = resolution M
-- used 0.14 seconds
3 6 15 18 6
o58 = R <-- R <-- R <-- R <-- R
0 1 2 3 4
o58 : ChainComplex
As before, we may examine the degrees of its components, or display it. i59 = betti C
total: 3 6 15 18 6
0: 3 6 . . .
1: . . . . .
2: . . 15 18 6We can make a polynomial ring with 18 IndexedVariables. i60 = S = ZZ/101[t_1 .. t_9, u_1 .. u_9]
o60 = S
o60 : PolynomialRing
We can use genericMatrix to pack the variables into
3-by-3 matrices. i61 = m = genericMatrix(S, t_1, 3, 3)
o61 = | t_1 t_4 t_7 |
| t_2 t_5 t_8 |
| t_3 t_6 t_9 |
3 3
o61 : Matrix S <--- S
i62 = n = genericMatrix(S, u_1, 3, 3)
o62 = | u_1 u_4 u_7 |
| u_2 u_5 u_8 |
| u_3 u_6 u_9 |
3 3
o62 : Matrix S <--- S
We may look at the matrix product. i63 = m*n
o63 = | 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
o63 : Matrix S <--- S
Let's produce the equations generated by the equations which assert
that m and n commute with each other. (See flatten.) i64 = j = flatten(m*n - n*m)
o64 = | 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
o64 : Matrix S <--- S
Let's compute a Groebner basis for the image of j with gb. i65 = gb j
o65 = gb | 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 |
o65 : GroebnerBasis
The resulting Groebner 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. i66 = generators gb j;
1 26
o66 : 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 Groebner basis.
i67 = betti gb j
total: 1 26
0: 1 .
1: . 8
2: . 12
3: . 5
4: . 1
We can use the Degrees option to make a graded polynomial ring with a nonstandard grading.
i68 = ZZ/103[x,y,z, Degrees=>{1,2,3}]
ZZ
o68 = ---[x,y,z,Degrees => {{1},{2},{3}}]
103
o68 : PolynomialRing
We can use isHomogeneous to tell whether a polynomial is
homogeneous with respect to this grading. i69 = isHomogeneous(x*y^2 - y*z)
o69 = true
Go to main index.
Go to concepts index.