next | previous | forward | backward | up | top | index | toc | Macaulay2 website
Macaulay2Doc > matrices > basic arithmetic of matrices

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.

-

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

o4 = | -3 -3 -3 |
     | 3  3  3  |

              2        3
o4 : 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.
i5 : R = ZZ/17[a..l];
i6 : ff = matrix {{a,b,c},{d,e,f}}

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

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

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

             3       2
o7 : Matrix R  <--- R
i8 : ff * gg

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

             2       2
o8 : Matrix R  <--- R

^

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

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

              3        3
o9 : Matrix ZZ  <--- ZZ
i10 : ff^4

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

               3        3
o10 : 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 ==.
i11 : ff == gg

o11 = false
i12 : ff == ff

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

       3
o13 = R

o13 : R-module, free, degrees {-1, -2, -3}
i14 : N = R^3

       3
o14 = R

o14 : R-module, free
i15 : ff = id_M

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

              3       3
o15 : Matrix R  <--- R
i16 : gg = id_N

o16 = | 1 0 0 |
      | 0 1 0 |
      | 0 0 1 |

              3       3
o16 : Matrix R  <--- R
i17 : ff - gg == 0

o17 = true
i18 : ff == gg

o18 = 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 !=:
i19 : ff != gg

o19 = true
From the definition above of ff and gg we see that != will return a value of true if the degrees attached the 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
       
i20 : ff = matrix {{a,b,c},{d,e,f}}

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

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

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

              3       2
o21 : Matrix R  <--- R
i22 : ff ** gg

o22 = | 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
o22 : Matrix R  <--- R

See also