Matrix are rectangular array of numbers, for example matrix 3x5 might look like this:
You can access individual elements by row and column index for example mi,j.
Usually "i" (first index) is row index and "j"(second index) is for columns, each index start from 1, for our example matrix this mean :
* m1, 1 = 9
* m3, 5 = 7
* m2, 4 = -5
Matrix are widely used in computer science and game programming because it allow to simply represent mathematical formulas that is easy and natural to use on pc.
Lets assume we have following two formulas :
3x + 5y = 6
4x - 3y = -9
We can represent this as matrix :
Now we can simplify our two formulas into one :
Matrix * VectorXY = VectorConstTo calculate VectorXY we only have to divide both side by Matrix:
Lot of matrix operations are noncommutative, this mean A * B is not equal to B * A
In linear algebra,the identity matrix of size NxN is the square matrix with ones on the main diagonal and zeros elsewhere. Usually we name it "I" example :
When two given matrix (A and B) multiplied by each other return as a result I-matrix then we know that matrix B is inverse of matrix A.
This operation are commutative, A + B equal B + A. This operation require matrix to have the same dimensions. Addition or subtraction is accomplished by adding or subtracting corresponding elements.
Each element of destination matrix is calculated by adding element from matrix a and b that have the same row and col index : ci, j = ai, j + bi, j
Destination matrix element for subtract : ci, j = ai, j - bi, j
Example for 2x2 add operation :
Matrix can be multiply by scalar value, this is accomplished by multiplying each matrix element by scalar value
(c * A)i, j = c * Ai, j
Example multiply 2x3 matrix by 5
Matrix multiplication is noncommutative, A * B is NOT EQUAL to B * A.
Each element of destination matrix C is computed by cross product of matrix A row, and Matrix B column.
Mathematical formula for that is :
Ci, j = Ai, 1 * B1, j + Ai, 2 * B2, j + ... + Ai, N * BN, j
This formula show requirement for matrix A to have exactly the same amount of row as Matrix B have columns.
A(MxN) can be multiply with matrix B(NxP) result is matrix C(NxP)
Example of multiplication two arrays
Matrix transpose is operation that flips matrix over its diagonal. This can be represented by formula :
Bi, j = Aj, i
Example for 2x3 matrix transposition
This is scalar value and it is computed from all matrix elements. Determinant is used to calculate some of Matrix operation like matrix inversion. This operation require square matrix (NxN)
Wiki definitions :
In linear algebra, the determinant is a value that can be computed from the elements of a square matrix and encodes certain properties of the linear transformation described by the matrix. The determinant of a matrix A is denoted det(A), det A, or |A|. Geometrically, it can be viewed as the volume scaling factor of the linear transformation described by the matrix. This is also the signed volume of the n-dimensional parallelepiped spanned by the column or row vectors of the matrix. The determinant is positive or negative according to whether the linear mapping preserves or reverses the orientation of n-space.
Computing determinant for matrix 2x2 and 3x3 is straightforward :
Leibniz formula for PC computation is not required but it helps reduce operation in Laplace expansion. You can take any row of matrix multiply it by scalar value and add to another row, the same operation can by done on columns, lets have a look at simple example:
We will use Leibniz formula to reduce second row.
( 9, 3, -3, 15)To do that we will select second column and add it permutations to other columns. First we need to define scalar values for each column, to do that we will use row values (without second column):
(9, -3, 15)and divide it by inverted second column value (-3), result is :
(-3, 1, -5)
Now we can add second column, multiplied by scalar values:
Derivation of both matrix(before and after Leibniz algorithm) should be equal.
Laplace expansion is used to compute matrix derivation by changing matrix representation to weighted sum of the determinants of m sub-matrix, each of size (n−1) × (n−1).
Determinant of each sub matrix is called Minor.
This may sound intimidating but actually is very simple. We will split it into few steps:
a) Create all sub-matrix
We will have "m" sub-matrix, each matrix will be created from crossing out one row(i-th), and one column(j-th) of source matrix lets call it Sij This matrix. Example of calculating sub matrix :
Sub matrix S2,3 will be created from removing second row and third column :
Repeat this operation to create other sub-matrix
b) Calculate Minor matrix
Minor matrix element Mi,j is calculated as determinant of previous matrix Si,j
Mi,j = det(Si,j);
From our sample S2,3 this is calculated as :
c) Calculate Cofactor matrix
This matrix is created from Minor matrix by multiplying each value by (-1)i + j.
Ci,j = (-1)i + jMi,j
Again for our sample (-1)i + j we will calculate it :
C2,3 = (-1)2 + 3M2,3 = (-1)5348 = -348
This operation reduce matrix size by one on each dimension, we can reduce matrix size until we reach size 2x2 or 3x3 and then we can use already known formulas.
Now we can go back to Laplace expansion
First we pick either row or column that we want to reduce, best option is column/row that have lot of zero values, because each value will be use as weight. When weigh is equal 0 we can remove all sub-matrix.
For removing ith row we will use: - weight ai,1, ai,2 ... ai,N - each weight will be multiply by cofactor matrix for created by removing i-th row and j-th column
For removing jth column we wil use: - weight a1,j, a2,j ... aN,j - each weight will be multiply by cofactor matrix for created by removing i-th row and j-th column
This is example for our previous matrix (before leibniz):
We will remove second row (9, 3, -3, 15), we need four Cofactor matrix C2,1, C2,2, C2,3, C2,4
At this point we have 4 matrix 3x3 lets compute det for each one :
Now we need to go back to our formula and insert det values :
det A = -9 * 159 + 3 * (-1020) + 3 * (-198) + 15 * ( -54) = -5895
That's it. Lets try to do the same for matrix after leibniz :
We will remove the same row, this time it has only one non zero value this mean we have only one sub-matrix because other will be multiply by 0
det for this matrix is calculated by saurrs formula and is equal to : -1965, result for our formula:
det A = 3 * (-1965) = -5895
We have proven that both method leads to the same result.
There is no division by matrix, we can multiply result by inverted matrix. For ordinary number a / b means the solution to the equation xb = a.
This is the same as bx = a, but not for matrix, since matrix multiplication is non commutative.
For matrix we we have to solve XB = A for A * B-1 or BX = A for B-1A
To calculate A-1 of a matrix A we can use formula:
"C" is cofactor matrix, that we already know form "7. Matrix Determinant.". Transpose of this matrix is called the adjugate matrix.
We will try to calculate inverse matrix for :
This is our cofactor matrix:
Transposition of this matrix (adjugate) :
Last component is determinant of our initial matrix(Sarrus algorithm) :
Our inverse matrix is computed by dividing each element of adjugate matrix by det A, result
To validate result we can multiply source matrix with our result matrix, the result should be identity matrix
C1,1 = -2 * 3.4285 + 1 * 1.8571 -3 * (-2) = 1.0001
C1,2 = -2 * 0.1428 + 1 * 0.2857 -3 * 0 = 0.0001
C1,3 = -2 * (-2) + 1 * (-1) -3 * 1 = 0.0
C2,1 = -1 * 3.4285 + 4 * 1.8571 +2 * (-2) = -0.0001
C2,2 = -1 * 0.1428 + 4 * 0.2857 +2 * 0 = 1
C2,3 = -1 * (-2) + 4 * (-1) +2 * 1 = 0.0
C3,1 = -4 * 3.4285 + 2 * 1.8571 -5 * (-2) = 0.0002
C3,2 = -4 * 0.1428 + 2 * 0.2857 -5 * 0 = 0.0002
C3,3 = -4 * (-2) + 2 * (-1) -5 * 1 = 1
This is identity matrix with rounding "error".