<?xml version="1.0" encoding="utf-8" ?> <!-- for emacs: -*- coding: utf-8 -*- --> <!-- Apache may like this line in the file .htaccess: AddCharset utf-8 .html --> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN" "http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg-flat.dtd" > <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head><title>basic arithmetic of matrices</title> <link rel="stylesheet" type="text/css" href="../../../../Macaulay2/Style/doc.css"/> </head> <body> <table class="buttons"> <tr> <td><div><a href="_concatenating_spmatrices.html">next</a> | <a href="_extracting_spinformation_spabout_spa_spmatrix.html">previous</a> | <a href="_concatenating_spmatrices.html">forward</a> | <a href="_extracting_spinformation_spabout_spa_spmatrix.html">backward</a> | <a href="_matrices.html">up</a> | <a href="index.html">top</a> | <a href="master.html">index</a> | <a href="toc.html">toc</a> | <a href="http://www.math.uiuc.edu/Macaulay2/">Macaulay2 web site</a></div> </td> </tr> </table> <div><a href="index.html" title="">Macaulay2Doc</a> > <a href="_matrices.html" title="">matrices</a> > <a href="_basic_sparithmetic_spof_spmatrices.html" title="">basic arithmetic of matrices</a></div> <hr/> <div><h1>basic arithmetic of matrices</h1> <div><h2>+</h2> To add two matrices, use the <a href="__pl.html" title="a unary or binary operator, usually used for addition">+</a> operator.<table class="examples"><tr><td><pre>i1 : ff = matrix{{1,2,3},{4,5,6}} o1 = | 1 2 3 | | 4 5 6 | 2 3 o1 : Matrix ZZ <--- ZZ</pre> </td></tr> <tr><td><pre>i2 : gg = matrix{{4,5,6},{1,2,3}} o2 = | 4 5 6 | | 1 2 3 | 2 3 o2 : Matrix ZZ <--- ZZ</pre> </td></tr> <tr><td><pre>i3 : ff+gg o3 = | 5 7 9 | | 5 7 9 | 2 3 o3 : Matrix ZZ <--- ZZ</pre> </td></tr> </table> The matrices in question must have the same number of rows and columns and also must have the same ring.<h2>-</h2> To subtract two matrices, use the <a href="_-.html" title="a unary or binary operator, usually used for negation or subtraction">-</a> operator.<table class="examples"><tr><td><pre>i4 : ff-gg o4 = | -3 -3 -3 | | 3 3 3 | 2 3 o4 : Matrix ZZ <--- ZZ</pre> </td></tr> </table> The matrices in question must have the same number of rows and columns and also must have the same ring.<h2>*</h2> To multiply two matrices use the <a href="__st.html" title="a binary operator, usually used for multiplication">*</a> operator.<table class="examples"><tr><td><pre>i5 : R = ZZ/17[a..l];</pre> </td></tr> <tr><td><pre>i6 : ff = matrix {{a,b,c},{d,e,f}} o6 = | a b c | | d e f | 2 3 o6 : Matrix R <--- R</pre> </td></tr> <tr><td><pre>i7 : gg = matrix {{g,h},{i,j},{k,l}} o7 = | g h | | i j | | k l | 3 2 o7 : Matrix R <--- R</pre> </td></tr> <tr><td><pre>i8 : ff * gg o8 = | ag+bi+ck ah+bj+cl | | dg+ei+fk dh+ej+fl | 2 2 o8 : Matrix R <--- R</pre> </td></tr> </table> <h2>^</h2> To raise a square matrix to a power, use the <a href="_^.html" title="a binary operator, usually used for powers">^</a> operator.<table class="examples"><tr><td><pre>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</pre> </td></tr> <tr><td><pre>i10 : ff^4 o10 = | 7560 9288 11016 | | 17118 21033 24948 | | 26676 32778 38880 | 3 3 o10 : Matrix ZZ <--- ZZ</pre> </td></tr> </table> <h2>inverse of a matrix</h2> If a matrix <tt>f</tt> is invertible, then <tt>f^-1</tt> will work.<table class="examples"/> <h2>==</h2> To check whether two matrices are equal, one can use <a href="__eq_eq.html" title="equality">==</a>.<table class="examples"><tr><td><pre>i11 : ff == gg o11 = false</pre> </td></tr> <tr><td><pre>i12 : ff == ff o12 = true</pre> </td></tr> </table> However, given two matrices <tt>ff</tt> and <tt>gg</tt>, it can be the case that <tt>ff - gg == 0</tt> returns <a href="_true.html" title="">true</a> but <tt>ff == gg</tt> returns <a href="_false.html" title="">false</a>.<table class="examples"><tr><td><pre>i13 : M = R^{1,2,3} 3 o13 = R o13 : R-module, free, degrees {-1, -2, -3}</pre> </td></tr> <tr><td><pre>i14 : N = R^3 3 o14 = R o14 : R-module, free</pre> </td></tr> <tr><td><pre>i15 : ff = id_M o15 = {-1} | 1 0 0 | {-2} | 0 1 0 | {-3} | 0 0 1 | 3 3 o15 : Matrix R <--- R</pre> </td></tr> <tr><td><pre>i16 : gg = id_N o16 = | 1 0 0 | | 0 1 0 | | 0 0 1 | 3 3 o16 : Matrix R <--- R</pre> </td></tr> <tr><td><pre>i17 : ff - gg == 0 o17 = true</pre> </td></tr> <tr><td><pre>i18 : ff == gg o18 = false</pre> </td></tr> </table> Since the degrees attached to the matrices were different, <a href="__eq_eq.html" title="equality">==</a> returned the value <a href="_false.html" title="">false</a>.<h2>!=</h2> To check whether two matrices are not equal, one can use <a href="_!_eq.html" title="inequality">!=</a>:<table class="examples"><tr><td><pre>i19 : ff != gg o19 = true</pre> </td></tr> </table> From the definition above of <tt>ff</tt> and <tt>gg</tt> we see that <a href="_!_eq.html" title="inequality">!=</a> will return a value of <a href="_true.html" title="">true</a> if the degrees attached the the matrices are different, even if the entries are the same.<h2>**</h2> 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:<pre> ff : K ---> L gg : M ---> N ff ** gg : K ** M ---> L ** N </pre> <table class="examples"><tr><td><pre>i20 : ff = matrix {{a,b,c},{d,e,f}} o20 = | a b c | | d e f | 2 3 o20 : Matrix R <--- R</pre> </td></tr> <tr><td><pre>i21 : gg = matrix {{g,h},{i,j},{k,l}} o21 = | g h | | i j | | k l | 3 2 o21 : Matrix R <--- R</pre> </td></tr> <tr><td><pre>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</pre> </td></tr> </table> </div> <div class="single"><h2>See also</h2> <ul><li><span><a href="_extracting_spinformation_spabout_spa_spmatrix.html" title="">extracting information about a matrix</a></span></li> </ul> </div> </div> </body> </html>