Skip to content

Posts tagged ‘rows’

Maths 101: Matrix Multiplication

May 25, 2013

Charles

I’ve started so i’ll finish – we’re going to dig into matrix multiplication; once I’ve covered this we’ll dive into determinants and back to matrix inversion. Theres a reason for this which we’ll understand along the way – i’ll throw in the identity matrix and mean which are great for validation and dimensional understanding. Onto matrix multiplication then:

With matrix multiplication, we’re actually on face value doing sort of addition but internally multiplication – your’ll see this more when working with the inverse. Crucially there are two things that matter in matrix multiplication: firstly the multiplication rule, which is dependent of the second thing – A matrix can ONLY be multiplied by another matrix if it has the same amount of columns as the other has rows. Given two matrices:

A B C
D E F

X I
Y K
Z J

We start by taking the first value of the first row of first matrix! (A) and multipling it by the same element of the second matrix, like so (A * X). Now instead of following this pattern E.g. (B * I), we move down the column of the second matrix, so (B * Y). We can see now why the second matrix needs the same amount of rows as the first has columns. Colour coding this with the first row of the first matrix and with the first column of the second we can see the rule in action:

A B C
D E F

X I
Y K
Z J

We take the sum of these multiplications and treat it as the first value of the first row of the new matrix:

(AX) + (BY) + ( CZ)

The entire matrix looking like so:

(AX) + (BY) + (CZ) , (AI) + (BK) + (CJ)

(DX) + (EY) + (FZ) , (DI) + (EK) + (FJ)

Crucially we move along the first matrices rows as we do with the second matrices columns. Pseudo code could look something like this (might be off):

For i in matrix B’s column length:

For j in matrix B’s row length:

Sum (Multiply A[i][j] by B[j][i])

Now if you wanted to multiply a matrix by itself that wasn’t square i.e. it didn’t have the same amount of columns as rows; this would be where you’d create a transposed version of the matrix and multiply it by that E.g. To multiply:

A B C
D E F

by itself, you’d create a new matrix transposing the original like so:

A D
B E
C F

And multiply these two using the rule stated above:

(AA) + (BB) + (CC) , (AD) + (BC) + (CF)

(DA) + (EB) + (FC) , (DD) + (EE) + (FF)

Next we’ll break into determinants; which we’ll try to formulate recursive a function for any n x m square matrix.

Maths 101: Back to basics – Matrices

May 21, 2013

Charles

I’m going to cover broad topics in vectors, matrices and the likes, so i’t may appear that i’m skipping specific’s at time. But hopefully I’ll give a good enough understanding and hopefully be able to add specifics in due course. So onto matrices:

A matrix is an object made up of columns and rows – by association an n x m is row by column matrix of n rows and m columns like so (the …n just denotes the list of values):

    M C O L U M N S
N                            ..n
R                            ..n
O                            ..n
W                            ..n
S                             ..n

Another way to look at it is that each column represents a dimension, if we have say a 3 x 3 matrix we can represent an objects orientation, with each row representing an axis:

X0 Y0 Z0
X1 Y1 Z1
X2 Y2 Z2

In this example each row is a vector (direction) with an x, y and z component and by this three dimensional. This vector is also known as a ‘row vector’, similarly if we got the first components of each row we can call that a ‘column vector. Matrices have no know bounds  – theres no limit to them which makes them important in n-dimensional workflows (stuff that i dig).

There are 4 main big functions of matrices – Transpose, multiplication, determinant and inverse. These 4 are the grease that allows powerful manipulation of matrices. We’ll throw mean (average) in there two because its important in data analysis.

Transpose

We’ll start with something relatively simple but that’ll make a difference when we get to multiplication. All that transpose does is swap rows for columns and vice versa. So a matrix that looked like this:

1 2 3
4 5 6

Becomes this:

1 4
2 5
3 6

Doing this in pseudo code we can do something like this:

for i in column:

for j in rows:

collect rows[i][j]

So we’ve  transposed the matrix, why is this important – we’ll to multiply a matrix with another matrix it needs to have the same amount of columns as the other has rows! We’ll discuss square and identity matrices next…