Macaulay2 » Documentation
Packages » Macaulay2Doc > matrices > basic arithmetic of matrices
next | previous | forward | backward | up | index | toc

basic arithmetic of matrices

+

To add two matrices, use the + operator.
i1 : ff = matrix{{1,2,3},{4,5,6}}

o1 = | 1 2 3 |
     | 4 5 6 |

              2       3
o1 : Matrix ZZ  <-- ZZ
i2 : gg = matrix{{4,5,6},{1,2,3}}

o2 = | 4 5 6 |
     | 1 2 3 |

              2       3
o2 : Matrix ZZ  <-- ZZ
i3 : ff+gg

o3 = | 5 7 9 |
     | 5 7 9 |

              2       3
o3 : Matrix ZZ  <-- ZZ
The matrices in question must have the same number of rows and columns and also must have the same ring.

Scalars are converted to scalar matrices when necessary.
i4 : matrix {{1, 2}, {3, 4}} + 5

o4 = | 6 2 |
     | 3 9 |

              2       2
o4 : Matrix ZZ  <-- ZZ
This is also true for the other arithmetic operations discussed below.

-

To subtract two matrices, use the - operator.
i5 : ff-gg

o5 = | -3 -3 -3 |
     | 3  3  3  |

              2       3
o5 : Matrix ZZ  <-- ZZ
The matrices in question must have the same number of rows and columns and also must have the same ring.

*

To multiply two matrices use the * operator.
i6 : R = ZZ/17[a..l];
i7 : ff = matrix {{a,b,c},{d,e,f}}

o7 = | a b c |
     | d e f |

             2      3
o7 : Matrix R  <-- R
i8 : gg = matrix {{g,h},{i,j},{k,l}}

o8 = | g h |
     | i j |
     | k l |

             3      2
o8 : Matrix R  <-- R
i9 : ff * gg

o9 = | ag+bi+ck ah+bj+cl |
     | dg+ei+fk dh+ej+fl |

             2      2
o9 : Matrix R  <-- R
Suppose we multiply a homogeneous polynomial by a homogeneous matrix. The result ought to be homogeneous, but how can we arrange that? Scalar multiplication should not change the source or target of a map! Instead, we introduce one final complication: each matrix records a degree of its own, which is normally zero, and is used when deciding whether the matrix is homogeneous.
i10 : R = ZZ/101[x,y,z];
i11 : degree matrix {{x^10}}

o11 = {0}

o11 : List
i12 : f = matrix {{x,0,y*z},{0,y^2,x^2}};

              2      3
o12 : Matrix R  <-- R
i13 : degree f

o13 = {0}

o13 : List
Multiplying a matrix by a homogeneous polynomial adds the degree of the polynomial to the degree of the map.
i14 : h = x^10 * f

o14 = | x11 0     x10yz |
      | 0   x10y2 x12   |

              2      3
o14 : Matrix R  <-- R
i15 : degree h

o15 = {10}

o15 : List
i16 : degrees source h

o16 = {{1}, {2}, {2}}

o16 : List
i17 : isHomogeneous h

o17 = true
If you don't like this, you have an alternative. The degree of a tensor product of two matrices is the sum of the degrees, and its source module is the tensor product of the source modules.
i18 : h = x^10 ** f

o18 = | x11 0     x10yz |
      | 0   x10y2 x12   |

              2      3
o18 : Matrix R  <-- R
i19 : degree h

o19 = {0}

o19 : List
i20 : degrees source h

o20 = {{11}, {12}, {12}}

o20 : List
i21 : isHomogeneous h

o21 = true

^

To raise a square matrix to a power, use the ^ operator.
i22 : ff = matrix{{1,2,3},{4,5,6},{7,8,9}}

o22 = | 1 2 3 |
      | 4 5 6 |
      | 7 8 9 |

               3       3
o22 : Matrix ZZ  <-- ZZ
i23 : ff^4

o23 = | 7560  9288  11016 |
      | 17118 21033 24948 |
      | 26676 32778 38880 |

               3       3
o23 : Matrix ZZ  <-- ZZ

inverse of a matrix

If a matrix f is invertible, then f^-1 will work.

==

To check whether two matrices are equal, one can use ==.
i24 : ff == gg

o24 = false
i25 : ff == ff

o25 = true
However, given two matrices ff and gg, it can be the case that ff - gg == 0 returns true but ff == gg returns false.
i26 : M = R^{1,2,3}

       3
o26 = R

o26 : R-module, free, degrees {-1, -2, -3}
i27 : N = R^3

       3
o27 = R

o27 : R-module, free
i28 : ff = id_M

o28 = {-1} | 1 0 0 |
      {-2} | 0 1 0 |
      {-3} | 0 0 1 |

              3      3
o28 : Matrix R  <-- R
i29 : gg = id_N

o29 = | 1 0 0 |
      | 0 1 0 |
      | 0 0 1 |

              3      3
o29 : Matrix R  <-- R
i30 : ff - gg == 0

o30 = true
i31 : ff == gg

o31 = false
Since the degrees attached to the matrices were different, == returned the value false.

!=

To check whether two matrices are not equal, one can use !=:
i32 : ff != gg

o32 = true
From the definition above of ff and gg we see that != will return a value of true if the degrees attached to the matrices are different, even if the entries are the same.

**

Since tensor product (also known as Kronecker product and outer product) is a functor of two variables, we may compute the tensor product of two matrices. Recalling that a matrix is a map between modules, we may write:
       ff : K ---> L
       gg : M ---> N
       ff ** gg : K ** M  ---> L ** N
       
i33 : R = ZZ/17[a..l];
i34 : ff = matrix {{a,b,c},{d,e,f}}

o34 = | a b c |
      | d e f |

              2      3
o34 : Matrix R  <-- R
i35 : gg = matrix {{g,h},{i,j},{k,l}}

o35 = | g h |
      | i j |
      | k l |

              3      2
o35 : Matrix R  <-- R
i36 : ff ** gg

o36 = | ag ah bg bh cg ch |
      | ai aj bi bj ci cj |
      | ak al bk bl ck cl |
      | dg dh eg eh fg fh |
      | di dj ei ej fi fj |
      | dk dl ek el fk fl |

              6      6
o36 : Matrix R  <-- R

See also