Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168769
Image: ubuntu2004

A matrix is a rectangular array of values.

\[
A_{m,n} =
\begin{bmatrix}
a_{1,1} & a_{1,2} & \cdots & a_{1,n} \\
a_{2,1} & a_{2,2} & \cdots & a_{2,n} \\
\vdots & \vdots & \ddots & \vdots \\
a_{m,1} & a_{m,2} & \cdots & a_{m,n}
\end{bmatrix}

Note \rightarrow in the computational world, indexing usually begins at 00

However, traditional math texts (such as ours) often begin indexing with 11.  An annoyance, maybe, but a good thing to get used to.

You'll see the use of both both types of indexing often.  It's just a matter of learning to pay attention to details.

\[
A_{m,n} =
\begin{bmatrix}
a_{0,0} & a_{0,1} & \cdots & a_{0,(n-1)} \\
a_{1,0} & a_{1,1} & \cdots & a_{1,(n-1)} \\
\vdots & \vdots & \ddots & \vdots \\
a_{(m-1),0} & a_{(m-1),1} & \cdots & a_{(m-1),(n-1)}
\end{bmatrix}

 

$M = 
\begin{bmatrix}
a & b & c \\
d & e & f \\
g & h & i
\end{bmatrix}
$

Computationally speaking, we can think of a matrix as a list of lists.

var('a b c d e f g h i') list1 = [a, b, c] list2 = [d, e, f] list3 = [g, h, i] M = [list1, list2, list3] M
\newcommand{\Bold}[1]{\mathbf{#1}}\left[\left[a, b, c\right], \left[d, e, f\right], \left[g, h, i\right]\right]
for i in [0..2]: M[i]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[a, b, c\right]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[d, e, f\right]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[g, h, i\right]
M[0]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[a, b, c\right]
M[0][0]
\newcommand{\Bold}[1]{\mathbf{#1}}a
M[0][1]
\newcommand{\Bold}[1]{\mathbf{#1}}b
M[0][2]
\newcommand{\Bold}[1]{\mathbf{#1}}c
M[1]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[d, e, f\right]
M[1][0]
\newcommand{\Bold}[1]{\mathbf{#1}}d
M[1][1]
\newcommand{\Bold}[1]{\mathbf{#1}}e
M[1][2]
\newcommand{\Bold}[1]{\mathbf{#1}}f
M[2]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[g, h, i\right]
M[2][0]
\newcommand{\Bold}[1]{\mathbf{#1}}g
M[2][1]
\newcommand{\Bold}[1]{\mathbf{#1}}h
M[2][2]
\newcommand{\Bold}[1]{\mathbf{#1}}i

Python supports negative indexing:

M[-1]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[g, h, i\right]
M[-1][-1]
\newcommand{\Bold}[1]{\mathbf{#1}}i
M[-2]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[d, e, f\right]
M[-3]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[a, b, c\right]

Negative indexing can be very useful.

This list of lists we created is structurally a matrix, but it doesn't yet know how to act like a matrix. 

It's kind of a dumb matrix. 

Sage provides a way to transform a dumb matrix into a smart matrix.
A smart matrix knows how to perform all kinds of useful matrix operations.

To create a smart matrix from a dumb matrix:

M
\newcommand{\Bold}[1]{\mathbf{#1}}\left[\left[a, b, c\right], \left[d, e, f\right], \left[g, h, i\right]\right]
M = matrix(M) M
\newcommand{\Bold}[1]{\mathbf{#1}}\left(abcdefghi\begin{array}{rrr} a & b & c \\ d & e & f \\ g & h & i \end{array}\right)

MM is now a smart matrix!  A matrix object will automatically display itself as a rectangular array rather than as a list of lists.

The indexing we were using works the same as before:

M[0][0]
\newcommand{\Bold}[1]{\mathbf{#1}}a
M[1][1]
\newcommand{\Bold}[1]{\mathbf{#1}}e
M[-1][-1]
\newcommand{\Bold}[1]{\mathbf{#1}}i

However, with a matrix object, we also have access to another kind of indexing:

M[1, 2]
\newcommand{\Bold}[1]{\mathbf{#1}}f

This kind of indexing allows us to specify submatrices:

M[[0,1],[1,2]]
\newcommand{\Bold}[1]{\mathbf{#1}}\left(bcef\begin{array}{rr} b & c \\ e & f \end{array}\right)

Now that MM is a matrix object, we can do all sorts of useful matrixy kinds of things with it.

To find out what these things are, enter 'M.M.' (the letter MM followed by a period) in the cell below, if it's not there already, and then press the TAB key. 

A window will pop up will all kinds of functions.

Wow!  (To close the window, press the escape key.)

 

M.

We'll be using just a few of those functions.

Here are some examples.

Evaluate the following:

transpose(M)
\newcommand{\Bold}[1]{\mathbf{#1}}\left(adgbehcfi\begin{array}{rrr} a & d & g \\ b & e & h \\ c & f & i \end{array}\right)
det(M)
\newcommand{\Bold}[1]{\mathbf{#1}}{\left(e i - f h\right)} a - {\left(b i - c h\right)} d + {\left(b f - c e\right)} g
M.columns()
\newcommand{\Bold}[1]{\mathbf{#1}}\left[\left(a,d,g\right), \left(b,e,h\right), \left(c,f,i\right)\right]
M.rows()
\newcommand{\Bold}[1]{\mathbf{#1}}\left[\left(a,b,c\right), \left(d,e,f\right), \left(g,h,i\right)\right]
var('k') k*M
\newcommand{\Bold}[1]{\mathbf{#1}}\left(akbkckdkekfkgkhkik\begin{array}{rrr} a k & b k & c k \\ d k & e k & f k \\ g k & h k & i k \end{array}\right)
A = matrix([[a, b], [c, d]]) B = matrix([[e, f], [g, h]])
A
\newcommand{\Bold}[1]{\mathbf{#1}}\left(abcd\begin{array}{rr} a & b \\ c & d \end{array}\right)
B
\newcommand{\Bold}[1]{\mathbf{#1}}\left(efgh\begin{array}{rr} e & f \\ g & h \end{array}\right)
A*B
\newcommand{\Bold}[1]{\mathbf{#1}}\left(ae+bgaf+bhce+dgcf+dh\begin{array}{rr} a e + b g & a f + b h \\ c e + d g & c f + d h \end{array}\right)
B*A
\newcommand{\Bold}[1]{\mathbf{#1}}\left(ae+cfbe+dfag+chbg+dh\begin{array}{rr} a e + c f & b e + d f \\ a g + c h & b g + d h \end{array}\right)
A+B
\newcommand{\Bold}[1]{\mathbf{#1}}\left(a+eb+fc+gd+h\begin{array}{rr} a + e & b + f \\ c + g & d + h \end{array}\right)
M = matrix(10, [1..100]) M
\newcommand{\Bold}[1]{\mathbf{#1}}\left(123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100\begin{array}{rrrrrrrrrr} 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 \\ 11 & 12 & 13 & 14 & 15 & 16 & 17 & 18 & 19 & 20 \\ 21 & 22 & 23 & 24 & 25 & 26 & 27 & 28 & 29 & 30 \\ 31 & 32 & 33 & 34 & 35 & 36 & 37 & 38 & 39 & 40 \\ 41 & 42 & 43 & 44 & 45 & 46 & 47 & 48 & 49 & 50 \\ 51 & 52 & 53 & 54 & 55 & 56 & 57 & 58 & 59 & 60 \\ 61 & 62 & 63 & 64 & 65 & 66 & 67 & 68 & 69 & 70 \\ 71 & 72 & 73 & 74 & 75 & 76 & 77 & 78 & 79 & 80 \\ 81 & 82 & 83 & 84 & 85 & 86 & 87 & 88 & 89 & 90 \\ 91 & 92 & 93 & 94 & 95 & 96 & 97 & 98 & 99 & 100 \end{array}\right)
det(M)
\newcommand{\Bold}[1]{\mathbf{#1}}0
P = matrix(5, [randint(1, 100) for i in range(25)]) P
\newcommand{\Bold}[1]{\mathbf{#1}}\left(553195604950396333025239845965948463589776469258\begin{array}{rrrrr} 55 & 31 & 95 & 60 & 49 \\ 50 & 3 & 96 & 33 & 30 \\ 25 & 23 & 98 & 4 & 59 \\ 65 & 94 & 84 & 63 & 58 \\ 97 & 76 & 46 & 92 & 58 \end{array}\right)
det(P)
\newcommand{\Bold}[1]{\mathbf{#1}}321289209
P^-1
\newcommand{\Bold}[1]{\mathbf{#1}}\left(1651746356988011222898356988011596243569880198682356988016992133569880113404261070964035713313212892091541492321289209770255356988012262455321289209113597356988013598663569880119421135698801349522356988014340703569880116941383569880183845435698801536930356988011860193569880126536535698801171158610709640397544093212892098668673321289209826405356988014866380321289209\begin{array}{rrrrr} -\frac{1651746}{35698801} & \frac{1222898}{35698801} & \frac{159624}{35698801} & -\frac{98682}{35698801} & \frac{699213}{35698801} \\ -\frac{1340426}{107096403} & \frac{571331}{321289209} & -\frac{1541492}{321289209} & \frac{770255}{35698801} & -\frac{2262455}{321289209} \\ \frac{113597}{35698801} & \frac{359866}{35698801} & -\frac{194211}{35698801} & \frac{349522}{35698801} & -\frac{434070}{35698801} \\ \frac{1694138}{35698801} & -\frac{838454}{35698801} & -\frac{536930}{35698801} & -\frac{186019}{35698801} & -\frac{265365}{35698801} \\ \frac{1711586}{107096403} & -\frac{9754409}{321289209} & \frac{8668673}{321289209} & -\frac{826405}{35698801} & \frac{4866380}{321289209} \end{array}\right)
P[[1..3], [2..4]]
\newcommand{\Bold}[1]{\mathbf{#1}}\left(96333098459846358\begin{array}{rrr} 96 & 33 & 30 \\ 98 & 4 & 59 \\ 84 & 63 & 58 \end{array}\right)
def minor(M, row, col): rows = range(row)+range(row+1, M.nrows()) cols = range(col)+range(col+1, M.ncols()) return M[rows, cols]
minor(P, 2, 3)
\newcommand{\Bold}[1]{\mathbf{#1}}\left(5531954950396306594845897764658\begin{array}{rrrr} 55 & 31 & 95 & 49 \\ 50 & 3 & 96 & 30 \\ 65 & 94 & 84 & 58 \\ 97 & 76 & 46 & 58 \end{array}\right)
M = matrix(4, [1..16]) M
\newcommand{\Bold}[1]{\mathbf{#1}}\left(12345678910111213141516\begin{array}{rrrr} 1 & 2 & 3 & 4 \\ 5 & 6 & 7 & 8 \\ 9 & 10 & 11 & 12 \\ 13 & 14 & 15 & 16 \end{array}\right)
select = [M[1, 2]] select
\newcommand{\Bold}[1]{\mathbf{#1}}\left[7\right]
M = minor(M, 1, 2) M
\newcommand{\Bold}[1]{\mathbf{#1}}\left(12491012131416\begin{array}{rrr} 1 & 2 & 4 \\ 9 & 10 & 12 \\ 13 & 14 & 16 \end{array}\right)
select += [M[1, 1]] select
\newcommand{\Bold}[1]{\mathbf{#1}}\left[7, 10\right]
M = minor(M, 1, 1) M
\newcommand{\Bold}[1]{\mathbf{#1}}\left(141316\begin{array}{rr} 1 & 4 \\ 13 & 16 \end{array}\right)
select += [M[1, 0]] select
\newcommand{\Bold}[1]{\mathbf{#1}}\left[7, 10, 13\right]
M = minor(M, 1, 0) M
\newcommand{\Bold}[1]{\mathbf{#1}}\left(4\begin{array}{r} 4 \end{array}\right)
select += [M[0, 0]] select
\newcommand{\Bold}[1]{\mathbf{#1}}\left[7, 10, 13, 4\right]
sum(select)
\newcommand{\Bold}[1]{\mathbf{#1}}34