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