User:Saul/matrices

From Organic Design

Matrices are a way of representing complex numbers, usually consisting of a multi-dimensional array of numbers.
Below is an example of the matrix 'A' which is a 3x3 matrix consisting of the numbers a-i.

Matrix A
a b c
d e f
g h i

Addition and Subtraction

To add or subtract two matrices you must first ensure they are both the same size.
For example to calculate the following matrices: ANxM + BQxR you must ensure that N == Q and M == R.

Matrix A
a b c
d e f
g h i
+
Matrix B
j k l
m n o
p q r
=
Matrix A+B
a+j b+k c+l
d+m e+n f+o
g+p h+q i+r


Matrix A
a b c
d e f
g h i
-
Matrix B
j k l
m n o
p q r
=
Matrix A-B
a-j b-k c-l
d-m e-n f-o
g-p h-q i-r

Example

Matrix A
1 0 5
1 3 7
5 9 2
+
Matrix B
5 5 5
1 8 1
9 1 7
=
Matrix A+B
6 5 10
2 11 8
14 10 9


Matrix A
1 -2 3
3 -2 1
-7 5 -6
-
Matrix B
7 5 6
-3 -9 -7
0 0 9
=
Matrix A-B
-6 -7 -3
6 7 8
-7 5 -15

Multiplication

Matrices can by multiplied scaly quantities or other matrices. Matrices multiplication is not communal - AN x M x BQ x R != BQ x R x AN x M

Scalar

Multiplying a matrix by a scalar value multiplies all values within the matrix by that value.

Z x
Matrix A
a b c
d e f
g h i
=
Z x Matrix A
Za Zb Zc
Zd Ze Zf
Zg Zh Zi

Example

4 x
Matrix A
1 4 8
2 5 0
9 -5 3
=
4 x Matrix A
4 16 32
8 20 0
36 -20 12

Other Matrices

Multiplying a matrix by another matrix requires you to multiply each value in the row of the first matrix by the corresponding value in the second value's column and sum the total and put it in the position of that row and column, rinse and repeat for the other rows of the first matrix and the columns of the second.

To multiply two matrices together the rows in the first one must be the same amount of the columns in the second matrix.
AN x M x BQ x R can only be done if M and Q are equal.
BQ x R x AN x M can only be done if R and N are equal.

The size of the resulting matrix will have the amount of rows the first matrix has and the columns of the second.
AN x M x BQ x R = CN x R
BQ x R x AN x M = CQ x M.

Matrix A
a b c
d e f
g h i
x
Matrix B
j k l
m n o
p q r
=
Matrix AB
aj + bm + cp ak + bn + cq al + bo + cr
dj + em + fp dk + en + fq dl + eo + fr
gj + hm + ip gk + hn + iq gl + ho + ir


Example

Matrix A
1 7
4 0
-5 3
x
Matrix B
7 -3 5 6
-1 6 1 6
=
Matrix AB
1x7 + 7x-1 1x-3 + 7x6 1x5 + 7x1 1x6 + 7x6
4x7 + 0x-1 4x-3 + 0x6 4x5 + 0x1 4x6 + 0x6
-5x7 + 3x-1 -5x-3 + 3x6 -5x5 + 3x1 -5x6 + 3x6
=
Matrix AB
0 39 12 48
28 -12 20 24
-38 33 -22 -30

Identity

Identity matrix in complex numbers is the similar to '1' in real numbers.
For example in real numbers: Y x 1 = Y
In matrices: An x n x In x n = An x n - where In x n is an identity matrix.
Identity matrices are communal - An x n x In x n = In x n x An x n

Examples

I1x1
1
I2x2
1 0
0 1
I3x3
1 0 0
0 1 0
0 0 1

And so on.

Inverse

An inverse matrix is a matrix to the power of -1, the same way that the inverse of a scalar number is 1 divided by it - which is same as multiplying by that number to the power of -1, for example: (1/6 = 6-1)
So to switch a matrix to the other side of an equation multiply the other side by the inverse and the first side will cancel, for example:
Ax = y
(Where A is a matrix)
A-1Ax = yA-1 x = yA-1

Finding the Inverse

n x n (Gauss–Jordan Elimination)

To find the inverse of a square matrix of any size (n x n) you can use Gauss–Jordan Elimination.
Start by augmenting the matrix with the identity matrix of the same size then apply row operations till the matrix looks like identity, then the other one will become the inverse.

Augment Matrix with Identity

Create an augmented matrix with the matrix (A) and identity (I).
[A | I]

Example
Matrix A
1 4 8
2 5 0
9 -5 3
Identity
1 0 0
0 1 0
0 0 1
Augmented
1 4 8 1 0 0
2 5 0 0 1 0
9 -5 3 0 0 1
Apply Row Operations

Now you need to apply row operations until the matrix (A) becomes Identity (I).
This is best done by:

  • Create a lower triangular matrix.
  • Create an upper triangular matrix.
  • Multiply or divide the rows so the number becomes 1.

[A | I] → [I | A-1]

Example - Lower Triangle
0 -3 -2 1 0 0
1 -4 -2 0 1 0
-3 4 1 0 0 1
R1 ←→ R3
-3 4 1 0 0 1
1 -4 -2 0 1 0
0 -3 -2 1 0 0
3R2 → R2
-3 4 1 0 0 1
3 -12 -6 0 3 0
0 -3 -2 1 0 0
R1 + R2 → R2
-3 4 1 0 0 1
0 -8 -5 0 3 1
0 -3 -2 1 0 0
-8R3 → R3
-3 4 1 0 0 1
0 -8 -5 0 3 1
0 24 16 -8 0 0
3R2 + R3 → R3
-3 4 1 0 0 1
0 -8 -5 0 3 1
0 0 1 -8 9 3
Example - Upper Triangle
-3 4 1 0 0 1
0 -8 -5 0 3 1
0 0 1 -8 9 3
5R1 → R1
-15 20 5 0 0 5
0 -8 -5 0 3 1
0 0 1 -8 9 3
R1 + R2 → R1
-15 12 0 0 3 6
0 -8 -5 0 3 1
0 0 1 -8 9 3
5R3 + R2 → R2
-15 12 0 0 3 6
0 -8 0 -40 48 16
0 0 1 -8 9 3
(2/3)R1 → R1
-10 8 0 0 2 4
0 -8 0 -40 48 16
0 0 1 -8 9 3
R1 + R2 → R1
-10 0 0 -40 50 20
0 -8 0 -40 48 16
0 0 1 -8 9 3
Example - Simplify
-10 0 0 -40 50 20
0 -8 0 -40 48 16
0 0 1 -8 9 3
(-1/10)R1 → R1
1 0 0 4 -5 -2
0 -8 0 -40 48 16
0 0 1 -8 9 3
(-1/8)R2 → R2
1 0 0 4 -5 -2
0 1 0 5 -6 -2
0 0 1 -8 9 3
Extract the Inverse

The matrix on the right will become the inverse - so just extract it.

Example
Augmented
1 0 0 4 -5 -2
0 1 0 5 -6 -2
0 0 1 -8 9 3
Identity
1 0 0
0 1 0
0 0 1
Inverse
4 -5 -2
5 -6 -2
-8 9 3

2x2

The formulae for finding the inverse of a 2x2 matrix is:

Matrix A-1
a b
c d
= (1 / (ad - bc)) *
Matrix As
d -c
-b a
Further Explanation:

The inverse of a 2x2 matrix can be found via:
A-1 = (1 / Det(A)) * AS
(Where A is a 2x2 matrix and Det(A) is the Determinant of A and AS is the swapped matrix)


3 x 3 (Determinant Method)

Matrix A
a b c
d e f
g h i
Determinants

Take the determinants of each value and input them in place of that value, making every second one the negative value of this.

Matrix A
det(e,f,h,i) -det(d,f,g,i) det(d,e,g,h)
-det(b,c,h,i) det(a,c,g,i) -det(a,b,g,h)
det(b,c,e,f) -det(a,d,c,f) det(a,b,d,e)

Expand det:

Matrix A
ei - fh -(di - fg) dh - eg
-(bi - ch) ai - cg -(ah - bg)
bf - ce -(af - dc) ae - bd

Expand brackets:

Matrix A
ei - fh -di + fg dh - eg
-bi + ch ai - cg -ah + bg
bf - ce -af + dc ae - bd

Rearrange:

Matrix A
ei - fh fg - di dh - eg
ch - bi ai - cg bg - ah
bf - ce dc - af ae - bd
Reflection

Then the matrix must be reflected along the y=-x axis:

Matrix A
ei - fh ch - bi bf - ce
fg - di ai - cg dc - af
dh - eg bg - ah ae - bd

Checking

To check if a matrix is an inverse multiply it by the normal matrix and the result should be Identity matrix.
For example: A*A-1=I

Matrix A-1
4 7
2 6
X
Matrix A-1
0.6 -0.7
-0.2 0.4
=
Identity Matrix
1 0
0 1


Determinant

Determinant can be notated det A, det(A) and |A|

2x2

The determinant can be calculated by multiplying the top right element by the bottom left subtracted from the product of the top left and bottom right elements.
If the determinant is 0 then the matrix has no inverse.
For example:

Matrix A
a b
c d

Det(A) = (a * d) - (b * c)

3x3

Finding the determinate of a 3x3 is a little harder you need to create and find the determinate of 3 2x2 matrices embedded in it.
For example take the following matrix:

Matrix A
a b c
d e f
g h i

You need to select the first number (a) and block out all the numbers to the left, right, top and bottom of it (like where a rook could move in chess) like so:

Matrix A
a
e f
h i

Multiply the selected number with the determinate of the other 4 sitting as they are:
a * det(e, f, i, h) = a(ei - fh)
Do the same for the entire top row and add them together, but take the negative value of every second one like so:
det(A) = a(ei - fh) + (-b(di - fg)) + c(dh - eg)

Singular

A singular matrix is a matrix that has no inverse, the same way in scalar numbers 0 has no inverse as you cannot divide by 0.
If the determinant of a matrix is 0 then that matrix has no inverse.

Transformations

A matrix can be transformed in many ways, most useful to alter a shape that is defined by a set of points.
A point defined as a coordinate can be defined as a matrix:

(x, y) =
x
y

Once you have your point as a matrix it can be multiplied by another to perform certain transformations on it.

Translation

Rotation

A object can be rotated by multiplying the points (matrices) that form that object by another matrix.
The points will be rotated around the origin as its pivot point.
The matrix you need to multiply by is:

cos(A) -sin(A)
sin(A) cos(A)

Where A is the angle to rotate by.

Example

To rotate a point (x, y) by 36 degrees:

cos(36) -sin(36)
sin(36) cos(36)
=
0.81 -0.59
0.59 0.81


0.81 -0.59
0.59 0.81
x
y
=
0.81x + -0.59y
0.59x + 0.81y

The point (x, y) rotated by 36 degrees would be (0.81x + -0.59y, 0.59x + 0.81y):

Reflection

An object can be reflected along a mirror line. The mirror line is usually drawn at an angle to the X axis. To reflect an object by a mirror line that has an angle of 36deg.

cos(2 * 36) sin(2 * 36)
sin(2 * 36) -cos(2 * 36)
=
0.31 0.95
0.95 -0.31

Scale

Multi Transformations

You can apply a transformation to a point (P) represented by a matrix (A) by multiplying it.

Point (P)
X
Y

P * A

You can combine transformations by multiplying them together and apply it to the point (P) like so:
For transtformations T1, T2 and T3
PT1T2T3 = PTransformed by 1, 2 and 3

Systems of Linear Equations

Matrices are often used to solve more complicated systems of linear equations.
There are two main methods:Gaussian elimination (similarly there is also Gauss-Jordan elimination) and using inverse matrices.

Using Gaussian

Take the following series of equations:

2x + y -  z = 1
 x + y + 3z = 0
     y +  z = 1

This can be represented as a augmented matrix like so:

Augmented
2 1 -1 1
1 1 3 0
0 1 1 1

Then use row operations to create a the lower triangle like so:

2 1 -1 1
1 1 3 0
0 1 1 1
2R2 → R2
2 1 -1 1
2 2 6 0
0 1 1 1
R2 - R1 → R2
2 1 -1 1
0 1 7 -1
0 1 1 1
R3 - R2 → R3
2 1 -1 1
0 1 7 -1
0 0 -6 2

Then take the data back out of the matrix and put it back into equation form:

2x + y -   z =  1
     y +  7z = -1
         -6z =  2

Then solve starting at the bottom working upwards:

z = 2 / -6 = -1/3

y + 7(-1/3) = -1
y - (7/3) = -1
y = 4/3

2x + (4/3) - (-1/3) = 1
2x + (5/3) = 1
2x = -2/3
x = -2/6 = -1/3

Therefore the solutions are: x=-1/3, y=4/3, z=-1/3

Using Inverse Matrices

Take the following series of equations:

2x + y -  z = 1
 x + y + 3z = 0
     y +  z = 1

This can be represented by the following matrices:

Matrix A
2 1 -1
1 1 3
0 1 1
X
x
y
z
=
C
1
0
1

This can be shown as the following equation:
AX=C
To solve find the inverse of Matrix A and add it to the BEGINNING of both sides of the equation:
A-1AX=A-1C
Simplify:
X=A-1C
And convert back to matrix form:

X
x
y
z
=
A-1
1/3 1/3 -2/3
1/6 -1/3 7/6
-1/6 1/3 -1/6
C
1
0
1

Multiply out the last two matrices:

X
x
y
z
=
A-1
-1/3
4/3
-1/3

Therefore the solutions are: x=-1/3, y=4/3, z=-1/3