# Singular Book 2.1.6 -- matrix operations

In Macaulay2, matrices are defined over a ring. There are many ways to make a matrix, but the easiest is to use the matrix routine.
 i1 : A = QQ[x,y,z]; i2 : M = matrix{{1, x+y, z^2}, {x, 0, x*y*z}} o2 = | 1 x+y z2 | | x 0 xyz | 2 3 o2 : Matrix A <--- A i3 : N = matrix(A, {{1,2,3},{4,5,6},{7,8,9}}) o3 = | 1 2 3 | | 4 5 6 | | 7 8 9 | 3 3 o3 : Matrix A <--- A
The usual matrix arithmetic operations work.
 i4 : M+M o4 = | 2 2x+2y 2z2 | | 2x 0 2xyz | 2 3 o4 : Matrix A <--- A i5 : x*N o5 = | x 2x 3x | | 4x 5x 6x | | 7x 8x 9x | 3 3 o5 : Matrix A <--- A i6 : M*N o6 = | 7z2+4x+4y+1 8z2+5x+5y+2 9z2+6x+6y+3 | | 7xyz+x 8xyz+2x 9xyz+3x | 2 3 o6 : Matrix A <--- A i7 : N^3 o7 = | 468 576 684 | | 1062 1305 1548 | | 1656 2034 2412 | 3 3 o7 : Matrix A <--- A i8 : ((x+y+z)*N)^3 o8 = | 468x3+1404x2y+1404xy2+ | 1062x3+3186x2y+3186xy2 | 1656x3+4968x2y+4968xy2 ------------------------------------------------------------------------ 468y3+1404x2z+2808xyz+1404y2z+1404xz2+1404yz2+468z3 +1062y3+3186x2z+6372xyz+3186y2z+3186xz2+3186yz2+1062z3 +1656y3+4968x2z+9936xyz+4968y2z+4968xz2+4968yz2+1656z3 ------------------------------------------------------------------------ 576x3+1728x2y+1728xy2+576y3+1728x2z+3456xyz+1728y2z+1728xz2+1728yz2+576z 1305x3+3915x2y+3915xy2+1305y3+3915x2z+7830xyz+3915y2z+3915xz2+3915yz2+13 2034x3+6102x2y+6102xy2+2034y3+6102x2z+12204xyz+6102y2z+6102xz2+6102yz2+2 ------------------------------------------------------------------------ 3 684x3+2052x2y+2052xy2+ 05z3 1548x3+4644x2y+4644xy2 034z3 2412x3+7236x2y+7236xy2 ------------------------------------------------------------------------ 684y3+2052x2z+4104xyz+2052y2z+2052xz2+2052yz2+684z3 | +1548y3+4644x2z+9288xyz+4644y2z+4644xz2+4644yz2+1548z3 | +2412y3+7236x2z+14472xyz+7236y2z+7236xz2+7236yz2+2412z3 | 3 3 o8 : Matrix A <--- A
Indices in Macaulay2 are always 0 based, so the upper left entry is (0,0). Indexing is performed using _.
 i9 : M_(1,2) o9 = x*y*z o9 : A
Matrices cannot be modified. Make a MutableMatrix if you want to modify a matrix.
 i10 : M1 = mutableMatrix M o10 = | 1 x+y z2 | | x 0 xyz | o10 : MutableMatrix i11 : M1_(1,2) = 37_A o11 = 37 o11 : A i12 : M1 o12 = | 1 x+y z2 | | x 0 37 | o12 : MutableMatrix i13 : matrix M1 o13 = | 1 x+y z2 | | x 0 37 | 2 3 o13 : Matrix A <--- A
Matrices can be concatenated, either horizontally or vertically. The number of rows must match for horizontal concatenation, and the number of columns must match for vertical concatenation.
 i14 : M | M o14 = | 1 x+y z2 1 x+y z2 | | x 0 xyz x 0 xyz | 2 6 o14 : Matrix A <--- A i15 : M || N o15 = | 1 x+y z2 | | x 0 xyz | | 1 2 3 | | 4 5 6 | | 7 8 9 | 5 3 o15 : Matrix A <--- A
Use ideal(Matrix) to obtain the ideal generated by the entries of a matrix.
 i16 : ideal M 2 o16 = ideal (1, x, x + y, 0, z , x*y*z) o16 : Ideal of A
The $n$ by $n$ identity matrix is the identity map of the freemodule $A^n$.
 i17 : F = A^5 5 o17 = A o17 : A-module, free i18 : id_(A^5) o18 = | 1 0 0 0 0 | | 0 1 0 0 0 | | 0 0 1 0 0 | | 0 0 0 1 0 | | 0 0 0 0 1 | 5 5 o18 : Matrix A <--- A
In Macaulay2, integer matrices are just matrices defined over the ring of integers ZZ.
 i19 : matrix{{1,2,3},{4,5,6}} o19 = | 1 2 3 | | 4 5 6 | 2 3 o19 : Matrix ZZ <--- ZZ