Wednesday, January 23, 2019

Matrices Pt 2

If you haven't read part one, it might be a good idea as it goes over the basics of matrices.

Multiplication
You can think of calculating the matrices multiplication by thinking in terms of vectors and dot products, For example, you have matrix A and matrix B, both are a 2x2 matrix, you want to multiply the two together to get matrix C. The top left value of matrix C is equal to the dot product of the first row of matrix A and the first column of matrix B. Then to get the top right value of matrix C, you do the same but using the second column of matrix B. The bottom two values of matrix C can be calculated in the same before but using the second row of matrix A.


Of course this scales upwards so if you have two 4x4 matrices, the working out is still the same as before. Multiplication doesn't require both matrices to have the same dimensions, although it does require the number of columns in matrix A to be the same as the number of rows in matrix B.


It should be noted that matrix multiplication is note commutative, however, it is both associative and distributive over addition. Also if a matrix is multiplied by the identity matrix then the matrix stays the same. You can also calculate the identity matrix by multiplying a matrix by its inverse.

Matrix Multiplication Properties
  • A * B ≠ B * A - This means that it isn't communative
  • A * (B + C) = A * B + A * C  - This means that it is distributive
  • A * (B * C) = C * (A * B) - This means that it is associative
  • A = A * I 
  • I = A * A^-1
Matrix Multiplication Dimension
The dimensions of the end matrix of a matrix multiplication are the number of rows in the first matrix and the number of columns in the second matrix. So if we had a 3x2 matrix and a 2x5 matrix, the dimension of the final matrix is 3x5.

Transpose
When transposing a matrix, you convert each row of the matrix to its corresponding column, so the first row becomes the first column,etc. Transposing is denoted by a T.


Transforming 3D Vectors by Matrices
So you need to transform a vector by a specific matrix, well the first thing is to convert the vector into a matrix. There are two ways to do this, row major and column major.

For example, Vector A = {1, 2, 3} can either look like:

Row Major Version

Column Major Version
Depending on the kind of matrix you are multiplying by it makes sense to convert to that type of major. So if a matrix is intended to be multiplied by a row major matrix then convert it to that type of major. However, if that is not possible you can transpose the multiplaction matrix to multiply with the opposite major type.


x^1 = x * a + y * b + z * c
y^1 = x * d + y * e + z * f
z^1 = x * g + y * h + z * i


x^1 = a * x + b * y + c *z
y^1 = d * x + e * y + f * z
z^1 = g * x + h * y + i * z

So it actually doesn't matter which major is used but it is best to be consistent with which one is used throughout a game.

All images were created using https://www.codecogs.com/latex/eqneditor.php

No comments:

Post a Comment