Thursday, February 20, 2020 | Toby Opferman

## Matrices

```Toby Opferman
http://www.opferman.net
toby@opferman.net

Matrices

What is a Matrix?  It is an arrangement of numbers into
a column/row form.

[ 1 3 3 ]
[ 3 4 3 ]
[ 3 4 2 ]
[ 1 2 1 ]

This is a 4x3 Matrix.  mxn or RowsxCols

What is a use for a matrix?  To solve a linear system is one use.

3*x1 + 3*x2 + 9*x3 = 2
x2 + 6*x3 = 1
6*x1 + 7*x3 = 5

That is a linear system.

You can make a matrix out of it

[ 3 3 9 | 2 ]
[ 0 1 6 | 1 ]
[ 6 0 7 | 5 ]

Now, you want to reduce the matrix so that every column has a 1 in it.
You can multiple a row by a number, you can divide a row by a number (Both must
be non-zero of course)  You can switch rows you can add a row to another row.

Those are your basic row operations.

First, multiply the first row by -2

[ 3 3 9 | 2 ]    [ -6 -6 -18 | -4 ]
[ 0 1 6 | 1 ] =  [  0  1   6 |  1 ]
[ 6 0 7 | 5 ]    [  6  0   7 |  5 ]

Then add Row 1 to Row 3
[ -6 -6 -18 | -4 ]
[  0  1   6 |  1 ]
[  0 -6 -11 |  1 ]

Divide -2 out of row 1

[  3  3   9 |  2 ]
[  0  1   6 |  1 ]
[  0 -6 -11 |  1 ]

Multiply the second row by 6

[  3  3   9 |  2 ]
[  0  6  36 |  6 ]
[  0 -6 -11 |  1 ]

Add it to the bottom row

[  3  3   9 |  2 ]
[  0  6  36 |  6 ]
[  0  0  25 |  7 ]

Divide out 6 from the 2nd row

[  3  3   9 |  2 ]
[  0  1   6 |  1 ]
[  0  0  25 |  7 ]

Multiply -3 into the 2nd row

[  3  3   9 |  2 ]
[  0 -3 -18 | -3 ]
[  0  0  25 |  7 ]

Add Row 2 to the top row

[  3  0  -9 | -1 ]
[  0 -3 -18 | -3 ]
[  0  0  25 |  7 ]

Divide -3 out of the second row

[  3  0  -9 | -1 ]
[  0  1   6 |  1 ]
[  0  0  25 |  7 ]

Divide the bottom row by 25

[  3  0  -9 | -1    ]
[  0  1   6 |  1    ]
[  0  0   1 |  7/25 ]

Multiply bottom row by -6

[  3  0  -9 | -1      ]
[  0  1   6 |  1      ]
[  0  0  -6 |  -42/25 ]

Add Bottom Row to Second Row

[  3  0  -9 | -1              ]
[  0  1   0 |  25/25 - 42/25  ]
[  0  0  -6 |  -42/25         ]

Divide -6 out of the bottom row

[  3  0  -9 | -1              ]
[  0  1   0 |  25/25 - 42/25  ]
[  0  0   1 |  7/25           ]

Multiply bottom row by 9

[  3  0  -9 | -1              ]
[  0  1   0 |  25/25 - 42/25  ]
[  0  0   9 |  63/25          ]

Add Bottom Row to the top row

[  3  0   0 | -25/25 + 63/25  ]
[  0  1   0 |  25/25 - 42/25  ]
[  0  0   9 |  63/25          ]

Divide 9 out of the bottom row

[  3  0   0 |  38/25  ]
[  0  1   0 | -17/25  ]
[  0  0   1 |  7/25   ]

Divide 3 out of the top row

[  1  0   0 |  38/75  ]
[  0  1   0 | -17/25  ]
[  0  0   1 |  7/25   ]

x1 = 38/75
x2 = -17/25
x3 = 7/25

Subsitute into the original equation

3*x1 + 3*x2 + 9*x3 = 2
x2 + 6*x3 = 1
6*x1 + 7*x3 = 5

3*x1 + 3*x2 + 9*x3 = 2
-----------------------------
3*(38/75) + 3*(-17/25) + 9*(7/25) = 2
114/75 - 51/25 + 63/25 = 2
114/75 - 153/75 + 189/75 = 2
150/75 = 2
2 = 2

x2 + 6*x3 = 1
----------------------------
(-17/25) + 6*(7/25) = 1
-17/25 + 42/25 = 1
25/25 = 1
1 = 1

6*x1 + 7*x3 = 5
------------------
6*(38/75) + 7*(7/25) = 5
228/75 + 49/25 = 5
228/75 + 147/75 = 5
375/75 = 5
5 = 5

And there you have it.  Each column represents a variable.  The first
colomn is x1, second x2 and the third x3.   The rows represent an equation
and we converted the equations into solutions by simple row manipulation.

The best way to introduce a matrix was to show you a real example that serves
a real purpose.  Matrices can also be described as a linear combination of
vectors like so:

3*x1 + 3*x2 + 9*x3 = 2
x2 + 6*x3 = 1
6*x1 + 7*x3 = 5

The original equation:

[ 3 ]     [ 3 ]     [ 9 ]   [ 2 ]
x1[ 0 ] + x2[ 1 ] + x3[ 6 ] = [ 1 ]
[ 6 ]     [ 0 ]     [ 7 ]   [ 5 ]

As we see, this is a combination of vectors as is the solution.
BUT, the answer set does not always have to be defined.

[ 3 ]     [ 3 ]     [ 9 ]
x1[ 0 ] + x2[ 1 ] + x3[ 6 ]
[ 6 ]     [ 0 ]     [ 7 ]

We could insert another vector and solve again to see the values of x1, x2 and x3.

Once we get the solution for the linear combination, we can actually describe
the result in another way.

Matrix Multiplication.

Ax = b  where x and b are vectors and A is a matrix.

The Matrix A:
[ 3 3 9 ]
[ 0 1 6 ]
[ 6 0 7 ]

The Vector X:
[ x1 ]
[ x2 ]
[ x3 ]

Ax

[ 3 3 9 ][ x1 ]
[ 0 1 6 ][ x2 ]
[ 6 0 7 ][ x3 ]

To mutliply a matrix, you Take the first row of the first matrix and mulitply
it by the first column of the second matrix.  Each location.  And then you
add the results and that goes in the first row, first column.  You do this
for all columns of the second matrix.  Then you go to the next row of the first
matrix and do it again.

To multiply 2 matrices the following has to be true:

AB Where A and B are matrices such that A = mxn  B = nxr is true.

The number of rows in B must match the number of columns in A.

To demonstrate, here is the following matrices:
[ A D G ][ 1 4 ]
[ B E H ][ 2 5 ]
[ C F I ][ 3 6 ]

AB = C

[ (A*1 + D*2 + G*3) (A*4 + D*5 + G*6) ]
[ (B*1 + E*2 + H*3) (B*4 + E*5 + H*6) ]
[ (C*1 + F*2 + I*3) (C*4 + F*5 + I*6) ]

And thus from A = 3x3 matrix and B = 3x2 matrix, you get a
3x2 matrix.

For any A = mxn and B = nxr, you will get a mxr result when you multiply.

To demonstrate again:
x1 = 38/75
x2 = -17/25
x3 = 7/25

[ 3 3 9 ][ x1 ]
[ 0 1 6 ][ x2 ]  =
[ 6 0 7 ][ x3 ]

[ 3*x1 + 3*x2 + 9*x3 ]
[ 0*x1 + 1*x2 + 6*x3 ]
[ 6*x1 + 0*x2 + 7*x3 ]

[ 3*(38/75) + 3*(-17/25) + 9*(7/25) ]
[     0     + 1*(-17/25) + 6*(7/25) ]
[ 6*(38/75) +     0      + 7*(7/25) ]

Reduces to:

[ 2 ]
[ 1 ]
[ 5 ]

And, that is the solution to the linear system.

Pay attention to what is going on You multiply this way:

AB = C

(A = 4x4, B = 4x2, so C = 4x2)

You Do Row by Column.
[ *****> ][  |    ]   [ X   ]
[        ][  |    ]   [     ]
[        ][  |    ] = [     ]
[        ][ \ /   ]   [     ]

This time, you go to Y's spot.
[ *****> ][     | ]   [ X Y ]
[        ][     | ]   [     ]
[        ][     | ] = [     ]
[        ][    \ /]   [     ]

This time, you go to Z's spot.
[        ][ |     ]   [ X Y ]
[ *****> ][ |     ]   [ Z   ]
[        ][ |     ] = [     ]
[        ][\ /    ]   [     ]

And you get the drift.  Row of First Matrix is multiplied each corrisponding
element in the column of the Second matrix and then they are added together
to get 1 element in the result matrix.  The result goes in the Row of the
First matrix being multipled and the column of the second matrix.

```

Professional software engineer with over 15 years...