Basic matrix calculations

When it comes to matrix calculations, most of my colleagues say: “Uuuh. Ugly! Terrible” I say that’s not fair (o.k. you might take me as a fool now :-) ). Matrix calculations are quite cool. Have a look in (see Rotation Matrix or even Householder transformation. A Householder transformation: That’s fine art of matrix calculations :-))



Basic matrix definition

A matrix is a field of numbers containing n rows and m columns. The number of rows can differ from the number of columns. If they are equal, the matrix is a square matrix (for instance a 4*4 matrix like here).



Matrix

Main diagonal

Each matrix has a so called main diagonal consisting of the elements aii. If the matrix is not a square matrix this main diagonal does not lead from edge to edge. But that should not bother :-)

A 4 * 4 matrix with the main diagonal elements marked red looks like this:



Matrix

A square matrix can be symmetrical. That means the values symmetrical to the main diagonal are equal.

aij = aji


That’s quite an interesting point for various manipulations and occurs quite often.

A 4 * 4 matrix with the main diagonal elements marked red looks like this:



Diagonal matrix

A matrix that has only in the main diagonal elements that are not 0 is a diagonal matrix:


Matrix

Tridiagonal matrix

A matrix that has in its main diagonal and each element to the left and right of the diagonal element values not 0 is a tridiagonal matrix.


Matrix


Transposed matrix

A matrix can be mirrored at its main diagonal. Such a matrix is called transposed matrix AT (see Transposed Matrix)

AT would look like:


Matrix


Transposing a not square matrix switches the number of rows and columns.

Transposed matrixes have some importance in some algorithms (see Method of the least squares) and are therefore quite interesting.


As matrixes are not simple numbers, there are special regulations for matrix calculations:



Addition of two matrixes

The addition of two matrixes A+B is quite simple: Each element of A is added to the same element of B:


Matrix


=


Matrix


The matrixes of the addition must have the same dimension. They must have the same number of rows and the same number of columns. The matrixes of the addition may be switched without affecting the result:

A + B = B + A



Multiplication of a matrix by a number

The multiplication of a matrix by a simple number is quite easy to. Each element of the matrix is multiplied by the number.

A * x

=


Matrix



Multiplication of two matrixes

For the multiplication of two matrixes A * B there is the rule to lay the rows of A on the columns of B. That means we take a row of A and a column of B. Multiply the first elements of these, the second elements, the third and so on and finally add results of these multiplications together. That is the resulting element of the same row and column in the result matrix. If we multiply two 4*4 matrixes

C = A * B

The elements of the first row of C will be:

Matrix


The second row:

Matrix



And so on.

And the entire multiplication is:

Matrix



=

Matrix



That means there is the limitation that the left side matrix of the multiplication must have the same number of columns as the right side matrix has rows. Otherwise the multiplication is not possible. We can multiply a n*n matrix by a n dimensional vector. But the vector must be on the right side.

Matrix



=

Matrix



If the B vector would be on the left side, it would not work.

That means The matrixes or vectors of a multiplication cannot be switched.

Even if the multiplication would be possible, the result would not be the same if the matrixes would be switched. That’s an important fact.

A * B ≠ B * A



Orthogonal and orthonormal matrixes

Orthogonal and orthonormal matrixes have the special characteristic that their column vectors are orthogonal one to the other. That means the dot product of two such vectors is 0. For example the dot product of the first 2 vectors of a 4*4 matrix

Matrix


The orthonormal matrix additionally has the characteristic that the length of its column vectors is 1. That means the column vectors are normalized. These characteristics bring some additional topics.

The multiplication of an orthogonal and orthonormal matrix with its transposed matrix yields a diagonal matrix. If the matrix is orthonormal, the resulting diagonal matrix is the identity matrix.

With my favorited 4*4 matrix :-)

A * AT =

Matrix


=

Matrix


All the sums that are not in the main diagonal are dot products of two different vectors and because of the orthogonality 0. Only the diagonal elements become not 0. Only the diagonal elements are not 0. With the orthogonal matrix they contain the square of the length of the corresponding column vector and with the orthonormal matrix they are all 1. That means the multiplication of a orthonormal matrix with its transposed yields the identity matrix.

A funny detail is that we get the same result if we change the order of the multipliers:

 A * AT = AT * A


Now the fact that for an orthonormal matrix A * AT = I is quite an interesting thing. As the multiplication of the identity matrix by another matrix B does not change anything on B.


I * B = B


An orthonormal matrix Q can be eliminated on one side of an equation like:


Q * B = C


This can be extended by QT.


QT * Q * B = QT *C


and that is


I * B = QT *C


or


B = QT *C


That’s quite helpful in some cases. I used that in me algorithm for the method of the least squares (see The method of the least squares).

Buth don’t forget: That’s valid only for orthonormal matrixes :-)