A matrix is set of Numbers .
A matrix is an Rectangular Array .
A matrix is arranged in Rows and Columns .
Matrix Dimensions
This Matrix has 1 row and 3 columns:
| C = | 2 5 3 | |
|---|---|---|
| 2 | 5 | 3 |
| 2 | 5 | 3 |
|---|
The Dimension of the matrix is ( 1 x 3 ).
This matrix has 2 rows and 3 columns:
| C = | 2 5 3 4 7 1 | |
|---|---|---|
| 2 | 5 | 3 |
| 4 | 7 | 1 |
| 2 | 5 | 3 |
|---|---|---|
| 4 | 7 | 1 |
The dimension of the matrix is ( 2 x 3 ).
Square Matrices
A Square Matrix is a matrix with the same number of rows and columns.
An n-by-n matrix is known as a square matrix of order n.
A 2-by-2 matrix (Square matrix of order 2):
| C = | 1 2 3 4 |
|---|---|
| 1 | 2 |
| 3 | 4 |
| 1 | 2 |
|---|---|
| 3 | 4 |
A 4-by-4 matrix (Square matrix of order 4):
| C = | 1 -2 3 4 5 6 -7 8 4 3 2 -1 8 7 6 -5 | ||
|---|---|---|---|
| 1 | -2 | 3 | 4 |
| 5 | 6 | -7 | 8 |
| 4 | 3 | 2 | -1 |
| 8 | 7 | 6 | -5 |
| 1 | -2 | 3 | 4 |
|---|---|---|---|
| 5 | 6 | -7 | 8 |
| 4 | 3 | 2 | -1 |
| 8 | 7 | 6 | -5 |
Diagonal Matrices
| C = | 2 0 0 0 5 0 0 0 3 | |
|---|---|---|
| 2 | 0 | 0 |
| 0 | 5 | 0 |
| 0 | 0 | 3 |
| 2 | 0 | 0 |
|---|---|---|
| 0 | 5 | 0 |
| 0 | 0 | 3 |
Scalar Matrices
| C = | 3 0 0 0 0 3 0 0 0 0 3 0 0 0 0 3 | ||
|---|---|---|---|
| 3 | 0 | 0 | 0 |
| 0 | 3 | 0 | 0 |
| 0 | 0 | 3 | 0 |
| 0 | 0 | 0 | 3 |
| 3 | 0 | 0 | 0 |
|---|---|---|---|
| 0 | 3 | 0 | 0 |
| 0 | 0 | 3 | 0 |
| 0 | 0 | 0 | 3 |
The Identity Matrix
The Identity Matrix has 1 on the diagonal and 0 on the rest.
This is the matrix equivalent of 1. The symbol is I .
| I = | 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 | ||
|---|---|---|---|
| 1 | 0 | 0 | 0 |
| 0 | 1 | 0 | 0 |
| 0 | 0 | 1 | 0 |
| 0 | 0 | 0 | 1 |
| 1 | 0 | 0 | 0 |
|---|---|---|---|
| 0 | 1 | 0 | 0 |
| 0 | 0 | 1 | 0 |
| 0 | 0 | 0 | 1 |
If you multiply any matrix with the identity matrix, the result equals the original.
The Zero Matrix
The Zero Matrix (Null Matrix) has only zeros.
| C = | 0 0 0 0 0 0 | |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 0 | 0 |
| 0 | 0 | 0 |
|---|---|---|
| 0 | 0 | 0 |
Equal Matrices
Matrices are Equal if each element correspond:
| 2 5 3 4 7 1 | = | 2 5 3 4 7 1 |
|---|---|---|
| 2 | 5 | 3 |
| 4 | 7 | 1 |
| 2 | 5 | 3 |
| 4 | 7 | 1 |
| 2 | 5 | 3 |
|---|---|---|
| 4 | 7 | 1 |
| 2 | 5 | 3 |
|---|---|---|
| 4 | 7 | 1 |
Negative Matrices
The Negative of a matrix is easy to understand:
| - | -2 5 3 -4 7 1 | = | 2 -5 -3 4 -7 -1 |
|---|---|---|---|
| -2 | 5 | 3 | |
| -4 | 7 | 1 | |
| 2 | -5 | -3 | |
| 4 | -7 | -1 |
| -2 | 5 | 3 |
|---|---|---|
| -4 | 7 | 1 |
| 2 | -5 | -3 |
|---|---|---|
| 4 | -7 | -1 |
Linear Algebra in JavaScript
In linear algebra, the most simple math object is the Scalar :
const scalar = 1;Another simple math object is the Array :
const array = [ 1, 2, 3 ];Matrices are 2-dimensional Arrays
const matrix = [ [1,2],[3,4],[5,6] ];Vectors can be written as Matrices with only one column:
const vector = [ [1],[2],[3] ];Vectors can also be written as Arrays :
const vector = [ 1, 2, 3 ];JavaScript Matrix Operations
Programming matrix operations in JavaScript, can easily become a spaghetti of loops.
Using a JavaScript library will save you a lot of headache.
One of the most common libraries to use for matrix operations is called math.js .
It can be added to your web page with one line of code:
Using math.js
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/9.3.2/math.js"></script>Adding Matrices
If two matrices have the same dimension, we can add them:
| 2 5 3 4 7 1 | + | 4 7 1 2 5 3 | = | 6 12 4 6 12 4 |
|---|---|---|---|---|
| 2 | 5 | 3 | ||
| 4 | 7 | 1 | ||
| 4 | 7 | 1 | ||
| 2 | 5 | 3 | ||
| 6 | 12 | 4 | ||
| 6 | 12 | 4 |
| 2 | 5 | 3 |
|---|---|---|
| 4 | 7 | 1 |
| 4 | 7 | 1 |
|---|---|---|
| 2 | 5 | 3 |
| 6 | 12 | 4 |
|---|---|---|
| 6 | 12 | 4 |
Example
const mA = math.matrix([[1, 2], [3, 4], [5, 6]]);
const mB = math.matrix([[1,-1], [2,-2], [3,-3]]);
// Matrix Addition
const matrixAdd = math.add(mA, mB);
// Result [ [2, 1], [5, 2], [8, 3] ]Subtracting Matrices
If two matrices have the same dimension, we can subtract them:
| 2 5 3 4 7 1 | - | 4 7 1 2 5 3 | = | -2 -2 2 2 2 -2 |
|---|---|---|---|---|
| 2 | 5 | 3 | ||
| 4 | 7 | 1 | ||
| 4 | 7 | 1 | ||
| 2 | 5 | 3 | ||
| -2 | -2 | 2 | ||
| 2 | 2 | -2 |
| 2 | 5 | 3 |
|---|---|---|
| 4 | 7 | 1 |
| 4 | 7 | 1 |
|---|---|---|
| 2 | 5 | 3 |
| -2 | -2 | 2 |
|---|---|---|
| 2 | 2 | -2 |
Example
const mA = math.matrix([[1, 2], [3, 4], [5, 6]]);
const mB = math.matrix([[1,-1], [2,-2], [3,-3]]);
// Matrix Subtraction
const matrixSub = math.subtract(mA, mB);
// Result [ [0, 3], [1, 6], [2, 9] ]To add or subtract matrices, they must have the same dimension.
Scalar Multiplication
While numbers in rows and columns are called Matrices , single numbers are called Scalars .
It is easy to multiply a matrix with a scalar. Just multiply each number in the matrix with the scalar:
| 2 5 3 4 7 1 | x 2 = | 4 10 6 8 14 2 |
|---|---|---|
| 2 | 5 | 3 |
| 4 | 7 | 1 |
| 4 | 10 | 6 |
| 8 | 14 | 2 |
| 2 | 5 | 3 |
|---|---|---|
| 4 | 7 | 1 |
| 4 | 10 | 6 |
|---|---|---|
| 8 | 14 | 2 |
Example
const mA = math.matrix([[1, 2], [3, 4], [5, 6]]);
// Matrix Multiplication
const matrixMult = math.multiply(2, mA);
// Result [ [2, 4], [6, 8], [10, 12] ]Example
const mA = math.matrix([[0, 2], [4, 6], [8, 10]]);
// Matrix Division
const matrixDiv = math.divide(mA, 2);
// Result [ [0, 1], [2, 3], [4, 5] ]Transpose a Matrix
To transpose a matrix, means to replace rows with columns.
When you swap rows and columns, you rotate the matrix around it's diagonal.
| A = | 1 2 3 4 | A T = | 1 3 2 4 |
|---|---|---|---|
| 1 | 2 | ||
| 3 | 4 | ||
| 1 | 3 | ||
| 2 | 4 |
| 1 | 2 |
|---|---|
| 3 | 4 |
| 1 | 3 |
|---|---|
| 2 | 4 |
Multiplying Matrices
Multiplying matrices is more difficult.
We can only multiply two matrices if the number of colums in matrix A is the same as the number of rows in matrix B.
Then, we need to compile a "dot product":
We need to multiply the numbers in each column of A with the numbers in each row of B , and then add the products:
Example
const mA = math.matrix([1, 2, 3]);
const mB = math.matrix([[1, 4, 7], [2, 5, 8], [3, 6, 9]]);
// Matrix Multiplication
const matrixMult = math.multiply(mA, mB);
// Result [14, 32, 50]Explained
| A | B | C | ||
|---|---|---|---|---|
| 1 2 3 | x | 1 4 7 2 5 8 3 6 9 | = | 14 32 50 |
| 1 | 2 | 3 | ||
| 1 | 4 | 7 | ||
| 2 | 5 | 8 | ||
| 3 | 6 | 9 | ||
| 14 | 32 | 50 |
| 1 | 2 | 3 |
|---|
| 1 | 4 | 7 |
|---|---|---|
| 2 | 5 | 8 |
| 3 | 6 | 9 |
| 14 | 32 | 50 |
|---|
| (1,2,3) * (1,2,3) = 1x1 + 2x2 + 3x3 = | 14 |
|---|---|
| (1,2,3) * (4,5,6) = 1x4 + 2x5 + 3x6 = | 32 |
| (1,2,3) * (7,8,9) = 1x7 + 2x8 + 3x9 = | 50 |
If you know how to multiply matrices, you can solve many complex equations.
Example
You sell roses.
- Monday you sold 260 roses
- Tuesday you sold 200 roses
- Wednesday you sold 120 roses
What was the value of all the sales?
| Mon | 120 | 80 | 60 |
|---|---|---|---|
| Tue | 90 | 70 | 40 |
| Wed | 60 | 40 | 20 |
Example
const mA = math.matrix([3, 4, 2]);
const mB = math.matrix([[120, 90, 60], [80, 70, 40], [60, 40, 20]);
// Matrix Multiplication
const matrixMult = math.multiply(mA, mB);
// Result [800, 630, 380]Explained
| A | B | |
|---|---|---|
| 120 | 90 | 60 |
| 80 | 70 | 40 |
| 60 | 40 | 20 |
| 120 | 90 | 60 |
|---|---|---|
| 80 | 70 | 40 |
| 60 | 40 | 20 |
| (3,4,2) * (120,80,60) | = 3x120 + 4x80 + 2x60 | = 800 |
|---|---|---|
| (3,4,2) * (90,70,40) | = 3x90 + 4x70 + 2x40 | = 630 |
| (3,4,2) * (60,40,20) | = 3x60 + 4x40 + 2x20 | = 380 |
Matrix Factorization
With AI, you need to know how to factorize a matrix.
Matrix factorization is a key tool in linear algebra, especially in Linear Least Squares.