Matrix Operations

Psychic Symphony / Evolve

Many coders don't know simple basic stuff like matrix operations, basically because they are only really introduced to you at university. At least that's when I encountered them first. Anyway, I'm about to explain everything you ever wanted to know about matrices.

1) What is a matrix and how do I solve it?

You know what a function is by now, correct? f(x)=mx+a for example. You can equal it to 0, and we have a mx-f(x)+a=0 where f(x)=y. You usually do this when you want to resolve a system of equations. Well, that's what a matrix is, a system of equations:

               2x+3y+z=1                           2  3  1 : 1 
               x+12y+4z=0  gets transformed into   1 12  4 : 0 
               -x+2y-z=0                          -1  2 -1 : 0 

So normally when you want to know the (x,y,z) you would need to solve it "manually" in order of one of the unknown numbers, in order of the two others and substitute the variables in the other two equations with what you calculated on and on until you finally know one of the three and substitute back until you get all three values.

There is a much faster way of solving such systems using two rules you learned back on the 8th grade...

1) You can multiply both sides of an equation with the same value.
2) You can add two equations.

So let's use it.

First and for higher convenience always get a 1 on the top-left side of your matrix! You'll see why in a second.

So we have:

               1 12  4 : 0  Swapped order of equations! So what?
              -1  2 -1 : 0
               2  3  1 : 1

Let's find one of our unknowns. If we get a line like "0 0 a : b" we know that z=b/a, correct? So let's do it! How? By using the rules I mentioned a bit earlier...

    1 12  4 : 0    If we add  the first and second equations  we get a 0!  We
   -1  2 -1 : 0    can do the same with  the 1st and 3rd lines by multiplying
    2  3  1 : 1    the first with -2!!!

    1 12  4 : 0    See? Let's multiply the first line with -2 and do the same
    0 14  3 : 0    with the 3rd line!
    2  3  1 : 1

    1 12  4 : 0    Notice I also multiplied the 0 with -2 and added to 1!  It
    0 14  3 : 0    is not my fault if I have a 0 in there!!!
    0 -21 -7: 1

    1 12  4 : 0    I divided the last line by -7 on both sides. Let's do what
    0 14  3 : 0    we did with 1-3 with 2-3 this time.
    0  3  1 : -1/7

    1 12  4 : 0
    0 14  3 : 0    Have you got lost? Let's go through it more slowly...
    0  0  5 : -2

           14 3 : 0     *-3  <=>  -42 -9 : 0    and add...   0 5 : -2
            3 1 : -1/7  *14  <=>   42 14 : -2

Have you got it now? It's a little confusing in the beginning but with practise you will do all these calculations on your head straight away! This one multiplying by the negative of the other, etc... Anyway, now we have the value of z! Wheeeee!!!!! z=-2/5. Now what? We can replace it automatically:

                          z=-2/5                  so
                          y=(-3*-2/5)/14          and
                          x=-12y-4z

If you are wondering where all the negative signs came from: it's easy, recall you have ax+by+cz=d at the beginning, so you get x=(-by-cz+d)/a. It's highly frustrating to get this little process wrong after all those mindcalc all correct! Believe me... It sucks.

So now you know what a matrix is and how to solve it. "Tell me more!" OK, next step:

2) Adding and multiplying matrices.

Before I proceed I must notify that sometimes you will only get a matrix without the ":". That means the d is equal to 0 in all equations.

                          ax+by+cz=d        a b c : d

So if you have a matrix A and a matrix B as follows:

        1 4 7      6 3     What? Only two columns? Don't get scared, it
     A= 2 5 8   B= 5 2     means there is no unknown z!
        3 6 9      4 1 

We can add A to B if we fill in the vacancies, but we will do it in a different way. C=A+A is easy... C=(i,j)=A(i,j)+A(i,j), so we get:

         2  8 14 
      C= 4 10 16 
         6 12 18 

D=A*A is a little more complex... by multiplying, we get:

             D(i,j)=(A(1,j)*A(i,1)+A(2,j)*A(i,2)+...+A(n,j)*A(i,n))

This sounds complicated, but it isn't.

                      1*1+2*4+3*7 4*1+5*4+6*7 7*1+8*4+9*7 
                   D= 1*2+2*5+3*8 4*2+5*5+6*8 7*2+8*5+9*8 
                      1*3+2*6+3*9 4*3+5*6+6*9 7*3+8*6+9*9 

Got it? Down side, down side... Always look at things down side! So that you don't forget... down side! Is A multipliable by B? And the other way around? A*B is different from B*A!!! This is very important! How do we check if they are multipliable?

A is 3*3, B is 3*2 (downside, remember? It's not 2*3!!! Beware!). So if we want them to be multipliable, the two middle numbers must be the same, and the resulting matrix is of the two outer numbers' type:

   A*B= 3*3 with 3*2   3,3,3,2, the  two  middle  numbers are 3,  so it's ok!
                       The result of the multiplication is a 3*2 matrix.
                       
   B*A= 3*2 with 3*3   3,2,3,3, the two middle numbers  are not the same, so
                       so we cannot multiply them!!!
                       
   A*A= 3*3 with 3*3   results in a 3*3.
                       
   B*B= 3*2 with 3*2   is  not  multipliable (sounds like it should, doesn't
                       it? But it's not!).

3) What is a determinant?

A determinant of a matrix is a value we calculate. It is specific of each matrix and is unique! We can only calculate the determinants of n*n matrices.

You can thank LaPlace for this piece of information!

"Yeah so? Why would I want to get the determinant?" A determinant can give us lots of information about the matrix, if there is an inverse matrix (I'll show you how to calculate it later on), how to calculate the eigen-values of a matrix and other stuff...

"So how do I do it?" For 3*3 matrices it's easy, just apply the Sarrus rule:

                 a b c 
                 d e f  = a*e*i+d*h*c+g*b*f-c*e*g-f*h*a-i*b*d
                 g h i 

For bigger n*n matrices you have to use the multiplying property to get to a 3*3 matrix of which you can easily calculate the determinant using the LaPlace method (he came up with determinants). It is not that hard. Example:

                                   a b c d 
                                   e f g h 
                                   i j k l 
                                   m n o p 

If we can get a line (vertical or horizontal) where everything but a number is 0, then we can simplify it like this:

               a 0 c d                                  a c d 
               e 0 g h   so the calculus get easy:  -j* e g h 
               i j k l                                  m o p 
               m 0 o p 

Where does the negative sign come from? The signal table:

                                   + - + - 
                                   - + - + 
                                   + - + - 
                                   - + - + 

The main diagonal is always positive. You can deduct the others from there.

You can even get the determinants of a 12*12 matrix! But how do you get that vertical or horizontal line with one number and rest 0's? It isn't easy but if you multiply with negative numbers and add (vertically or horizontally) you can get to it!!

BEWARE:
In matrix solving you can only multiply vertical lines. In determinants it doesn't matter if you multiply and change order of the horizontals as well but only for determinants!

"So now I have a determinant! So what?" Well... If it's different from 0, you can calculate an inverse matrix!!!

4) Inverse matrix

There are two easy ways of calculating inverse matrices. Either way you have to check if the determinant is different from 0 or not...

"I don't recall all those equations..." We have a 3*3 (we can only do inverse matrices of the n*n type of matrices!!!) and we equal it to the basic matrix, which is (1,1,1)*(x,y,z). Confused?

                            1              1 0 0 
                            1 * (x,y,z) =  0 1 0 
                            1              0 0 1 

So we have our matrix A...

                                     1 2 3 
                                  A= 4 5 4 
                                     3 2 1 

First let's check if it's invertable:

         det(A) = 1*5*1+4*2*3+3*2*4-3*5*3-4*2*1-1*2*4 = 5+24+24-45-8-8
                                                      = 53-61
                                                      = -8

Good, it's invertable! So let's get to work:

                 1 2 3 : 1 0 0                 1 0 0 :      
                 4 5 4 : 0 1 0   when we get   0 1 0 : A^-1 
                 3 2 1 : 0 0 1                 0 0 1 :      

A^-1 is our inverted A. The inverted A is unique and if we reinvert it we get A again! There is no other matrix which inverted would get an A other than A^-1.

      1 2 3 : 1 0 0     1  2  3 :  1 0 0     1  2  3 :  1  0   0  
      4 5 4 : 0 1 0  =  0 -3 -8 : -4 1 0  =  0  1  2 : 3/4 0 -1/4  =
      3 2 1 : 0 0 1     0 -4 -8 : -3 0 1     0 -3 -8 : -4  1   0  

                              1 2  3 :   1  0   0  
                           =  0 1  2 :  3/4 0 -1/4 
                              0 0 -2 : -7/4 1 -3/4 

                              1 2 3 :  1    0    0  
                           =  0 1 2 : 3/4   0  -1/4 
                              0 0 1 : 7/8 -1/2  3/8 

                              1 2 0 : -13/8 3/2 -9/8 
                           =  0 1 0 :  -1    1   -1  
                              0 0 1 :  7/8 -1/2  3/8 

                              1 0 0 : 3/8 -1/2 7/8 
                           =  0 1 0 : -1    1  -1  
                              0 0 1 : 7/8 -1/2 3/8 

                 3/8 -1/2 7/8 
 So our A^-1 is  -1    1  -1   if I didn't mess up with the calculus!
                 7/8 -1/2 3/8 

There is another way of doing it though!

"Show me the way! I don't like doing so many counts!" Well, you start off with A, right? You calculate its adjacent. You don't know how? I'll show you...

                    a b c          e*i-f*h f*g-d*i d*h-e*g 
                 A= d e f  adj(A)= h*c-i*b a*i-c*g g*b-h*a 
                    g h i          b*f-c*e c*d-a*f a*e-b*d 

As I said... you calculate its adjacent and transpost it. You don't know how? I'll show you...

                            j k l                j m p 
                    adj(A)= m n o     T(adj(A))= k n q 
                            p q r                l o r 

As I said... you transpost it and divide by the det(A). You don't know how? I'll show you...

                             j m p             j m p    1
                  T(adj(A))= k n q     A^-1  = k n q  ----
                             l o r             l o r  detA

So let's check if it's ok!

                1 2 3             5-8  12-4 8-15   -3  8 -7 
             A= 4 5 4     adj(A)= 6-2  1-9  6-2  =  4 -8  4 
                3 2 1             8-15 12-4 5-8    -7  8 -3 

                            -3  4 -7 
                 T(adj(A))=  8 -8  8     (Recall det(A)=-8.)
                            -7  4 -3 

                                    3/8 -1/2 7/8 
                             A^-1 = -1    1  -1  
                                    7/8 -1/2 3/8 

Hei look! It's exactly like the one previously calculated!

I was thinking about showing you what an engel value is and how to calculate it and its engel vectors but there is no real need to do it! It's not very hard compared to what is above... It's just one extra application.

Well... I hope this article contains no mistakes and is useful for some people who want to know more about math but their school year doesn't include matrix. And for those who had some doubts about matrices I hope everything is clearer now!

Hei! If I added a chapter about geometry applications you would know what linear algebra is all about!!!

- Psychic Symphony / Evolve