Showing posts with label Linear Algebra. Show all posts
Showing posts with label Linear Algebra. Show all posts

Friday, February 1, 2019

Matrices Pt 3

Augmented Matrices
An augmented matrix is a matrix made of several linear equations that make up a system of equations. Each row of the matrix represents a equations within the system, whereas each column of the matrix represents a variable wtihin that equation.

We can see this with the above example, where we have two equations within a system and a matrix representation of that system. It also shows how the columns represents each variable or constant within the equation. For the x variables we have both 2 & 3, for y variables we have 5 & 4 and for the constant we have 10 & 24. This means we can write equations without having to write the x or y letters, or any other letters that might be used.

When converting a system of equations to a matrix, we need to make sure that each variable and constant is in line with each other but also if a variable or constant doesn't exist in one equation of the system then it defaults to 0 within the matrix at the location where the value would have gone.


Matrix Row Operators
There are three types of matrix row operators:

  1. Switch any two rows
  2. Multiply a row by a nonzero constant
  3. Add one row to another
You will know to switch two rows of a matrix when given a question like R1 ↔ R2, which means swap row 1 with row 2. It is important to note that you can also subtract if needed.


You will know to multiply a row of a matrix by a nonzero constant when given a question like 3R1 → R1, which means multiply row 1 by 3. It is also important to note that you can essentially divide by a nonzero constant as well by using fractions like 1/2.


You will know to multiply a row of a matrix by a nonzero constant when given a question like R2 + R3 → R2, which means add row 2 to row 3 and enter the answer in row 2.


Each row operator could also be represented in this format:



On the left side we have the original matrix, in the center is the row operator and the direction being to the final matrix, which we can see on the right side.

It's also worth noting that they can be combined.


Notice how the 2R3 part only applies to the addition in row 1 and is not used to modify row 3.

As mentioned before augmented matrices represent a system of equations. By using row operators we can get equivelent systems of equations. For example by using the mutliplaction by a nonzero constant we can eliminate a variable from an equation.


This is our original system of equations and the augmented matrix. If we carried out -2R1 → R1, we get:


This then allows us to use the row addition operator; R1 + R2 → R2, which gives us:


In other words, we get the final equation -1y = -4, which we can use to solve for y. So y = 4, which can then be subsituted in any of the equations to solve for x.

Reduced Row Echelon Form
You can solve a system of equations by converting the augmented matrix into the reduced row echelon form. This form is were the diagonal components of a matrix are valued at 1 and every other component is valued at 0, except for any constants. Think of it being a combination of the identity matrix and an augmented matrix. Below is an example, where a, b & c are the constants.


To get to the reduced row echelon form, can use the row operators from earlier.


Once a augmented matrix has been converted we can solve the system of equations. So using the above example we can see that x = 5, y = -1 & z = -1 and if we subsitute them back into the original equations we can see that everything is correct.

1x + 1y + 1z = 3

1(5) + 1(-1) + 1(-1) = 3

5 - 1 -1 = 3

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

Matrices Pt 1

A matrix is made up of a grid of real numbers, the grid can be r rows and c columns. If a matrix has 3 rows and 2 columns it is referred to as a 3x2 matrix. A matrix is typically expressed by a capital letter. These can be used to make changes or transform a vector or position in video games. There are two ways to declare a matrix

Method 1



Method 2



Identity Matrix
The identity matrix is a matrix with equal number of rows and columns to another matrix, as well as this each component of the identity matrix is 0, except for a diagonal of 1s that start in the top left and works its way down.



Addition & Subtraction
In order to add or subtract two matrices they have to have the same dimensions or in other words, the amount of rows must match and the amount of columns must match. When adding two matrices we simply add each corresponding component together.


The same goes for subtraction as we subtract each corresponding component from one another.


Matrix Equations
Matrix equations are equations where a matrix is being represented by a letter, similarly to how you might have the equation 5x = 15, which when solved gives x = 3.

An example of this is below, where we have to find A.


The first step is to get A on its own.


Then we carry out the matrix subtraction to give us the value of A.




Scalar Multiplication
This is the same as scalar multiplication of vectors as each component of the matrix is multiplied by the scalar value.



Zero Matrices
A zero matrix is a matrix which has ever element as a value of 0. A zero matrix is represented with a 0 like below and the dimensions are subscripted to that 0.


When you add a zero matrix to any matrix you get the original matrix, whereas if you add the opposite of a matrix to the original matrix you get the zero matrix. If you multiply a matrix by zero you get a zero matrix that has the same dimensions as the original.

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

Linear Interpolation

Linear interpolation or lerp is used to calculate a linear value that is between two values, i.e. lerp could return a value of 0.5 as it's between 0 and 1, which were the two original values given to the function. So in this instance the value is 50% between the two original values.

Lerping can be applied to a wide range of values and not just real world numbers, such as colour, vectors and quaternions. These example are made up of multiple dimensions such as colour usually being made up of 3/4 floats that represent R, G, B and A), so no matter how many dimensions make up something, lerping can still be applied using the generic formula:

Lerp(_a,  _b, _f) = (1 - _f) * _a + (_f * _b)

Where _a and _b are the two points that are being interpolated between and _f is within the fractional range of [0, 1] in terms of _a and _b. It is also important to note that this formula is not frame rate independant and so by using deltaTime * _f will achieve that.

An example use of lerping is moving a platform from point A to point B, where each tick the platform's position is now a percentage between point A and point B using lerp. There are other variations on lerp such as slerp which is spherical lerping, that provide similar effects to lerping.