Constructor
new Matrix()
The Matrix class should not be instantiated with the new keyword. Instead use the Matrix.of syntax to create a new Matrix. Unfortunatly jsdocs does not allow for the constructor to be hidden.
Example
const m = Matrix.of([[1,2],[2,3],[4,5]])
Members
precision :Number
Properties:
Name | Type | Description |
---|---|---|
precision |
Number
|
Floating point precision is set to 4 by default |
Type:
-
Number
Example
const m = Matrix.of([[1,2],[2,3],[4,5]])
m.precision === 4
type :String
Properties:
Name | Type | Description |
---|---|---|
type |
String
|
Returns the string 'Matrix' for all Matrix objects |
Type:
-
String
Example
const m = Matrix.of([[1,2],[2,3],[4,5]])
m.type === 'Matrix'
Methods
(static) ap(f, M) → {Matrix}
Curried function that applies a function to a Matrix
Parameters:
Name | Type | Description |
---|---|---|
f |
function
|
Function that accepts a Matrix as input |
M |
Matrix
|
Array
|
Matrix or Array to apply a function |
Returns:
- Type:
-
Matrix
Example
const f = x => x.reduce((prev, next) => prev + next)
Matrix.ap(f, [[1, 2, 3], [4, 5, 6], [7, 8, 9]])
// [[6], [15], [24]
(static) combine(A, A) → {Matrix}
Curried fucntion that combines 2 Matrices
- Source:
- See:
Parameters:
Name | Type | Description |
---|---|---|
A |
Matrix
|
Left side of the combine operator |
A |
Matrix
|
Right side of the combine operator |
Returns:
- Type:
-
Matrix
(static) concat(A, B, fopt) → {Matrix}
A curried function that concatenates 2 Matrices using a function as operator
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
A |
Matrix
|
Left side Matrix of the concatenation | ||
B |
Matrix
|
Right side Matrix of the concatenation | ||
f |
function
|
<optional> |
concat | A curried function accepting 2 matrices as input |
Returns:
- Type:
-
Matrix
Example
const a = [[0, 1, 1], [2, 3, 4]]
const b = [[2, 2, 2], [3, 3, 3]]
const A = Matrix.of(a)
const B = Matrix.of(b)
const M = Matrix.concat(A, B)
// [[0, 1, 1, 2, 2, 2], [2, 3, 4, 3, 3, 3]]
(static) determinant(A) → {Number}
Calculates the determinant of a square Matrix using Sarrus' rule or LU decomposition
Parameters:
Name | Type | Description |
---|---|---|
A |
Matrix
|
Array
|
Matrix as input to calculate the determinant |
Returns:
- Type:
-
Number
(static) diag(M) → {Array}
Returns an array containing the values on the diagonal
Parameters:
Name | Type | Description |
---|---|---|
M |
Matrix
|
Array
|
Matrix from which to return the diagonal |
Returns:
- Type:
-
Array
Example
const diag1 = Matrix.diag([[2, 1], [1, 5]])
// [2, 5]
(static) diagproduct(M) → {Number}
Returns the product of the values on the diagonal
Parameters:
Name | Type | Description |
---|---|---|
M |
Matrix
|
Array
|
Matrix from which to return the diagonal |
Returns:
- Type:
-
Number
Example
const diag1 = Matrix.diagproduct([[2, 1], [1, 5]])
// 10
(static) dot(A, B) → {Matrix}
Curried fucntion that returns the dot product of 2 matrices
Parameters:
Name | Type | Description |
---|---|---|
A |
Matrix
|
Array
|
Left side of the dot product |
B |
Matrix
|
Array
|
Right side of the dot product |
Returns:
- Type:
-
Matrix
Example
const a = [[1, 2, 3], [4, 5, 6]]
const b = [[7, 8], [9, 10], [11, 12]]
const A = Matrix.of(a)
const B = Matrix.of(b)
Matrix.dot(A, B) // [[58, 64], [139, 154]]
(static) empty(rowsopt, colsopt) → {Matrix}
Returns an empty Matrix from an existing Matrix
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
rows |
Number
|
<optional> |
0 | Rows to generate |
cols |
Number
|
<optional> |
0 | Cols to generate |
Returns:
- Type:
-
Matrix
(static) flatMap(fn, M) → {*}
Runs flatMap on the value of hte Matrix
Parameters:
Name | Type | Description |
---|---|---|
fn |
function
|
Flatten function |
M |
Matrix
|
Array
|
Returns:
- Type:
-
*
Example
const a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
const flattenedArray = Matrix.flatMap(x => x, a) // [1, 2, 3, 4, 5, 6, 7, 8, 9]
(static) flatten() → {Array|*}
Flattens the value of a Matrix into an one dimensional array
Returns:
-
- Type:
-
Array
-
- Type:
-
*
Example
const a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
const flattenedArray = Matrix.flatten(a) // [1, 2, 3, 4, 5, 6, 7, 8, 9]
(static) fold(f, M) → {Matrix}
Static function to reduce the matrix rows using a reduce function
Parameters:
Name | Type | Description |
---|---|---|
f |
function
|
A reduce/fold function |
M |
Matrix
|
Array
|
The Matrix to reduce |
Returns:
- Type:
-
Matrix
Example
// Sum of all matrix values
const reducer = (prev, next) => Number(prev) + next.reduce((acc, x) => acc + x, 0)
const A = Matrix.of([[1, 1], [1, 1]]
Matrix.fold(reducer, A)
// 4
(static) getColumn(index, M) → {Array}
Returns the values of a Matrix column
Parameters:
Name | Type | Description |
---|---|---|
index |
Number
|
Index of the column |
M |
Matrix
|
Array
|
Returns:
- Type:
-
Array
(static) getRow(index, M) → {Array}
Returns the values of a Matrix row
Parameters:
Name | Type | Description |
---|---|---|
index |
Number
|
Index of the row |
M |
Matrix
|
Array
|
Returns:
- Type:
-
Array
(static) kronecker(A, B) → {Matrix}
The Kronecker product is an operation on two matrices of arbitrary size resulting in a block matrix.
Parameters:
Name | Type | Description |
---|---|---|
A |
Matrix
|
The left side Matrix of the product (A ⊗ B) |
B |
Matrix
|
The right side Matrix of the product (A ⊗ B) |
Returns:
- Type:
-
Matrix
(static) map(f, M) → {Matrix}
Curried function that maps over the rows of the matrix using a map function
Parameters:
Name | Type | Description |
---|---|---|
f |
function
|
An iterator function |
M |
Matrix
|
Array
|
Matrix or array to map |
Returns:
- Type:
-
Matrix
Example
const m = Matrix.map(x= > x.map(y => y+ 1), [[1, 1], [1, 1]])
// [[2, 2], [2, 2]]
(static) matrixIdentity() → {Matrix}
curried fucntion that returns an matrixIdentity matrix
Returns:
- Type:
-
Matrix
Example
const A = Matrix.matrixIdentity(3, 2)
// [[1, 0, 0], [0, 1, 0]]
(static) of(val) → {Matrix}
Creates a Matrix object and flattens the Matrix
Parameters:
Name | Type | Description |
---|---|---|
val |
Array
|
function
|
An array of arrays |
Returns:
- Type:
-
Matrix
Example
const m = Matrix.of([[1,2],[2,3],[4,5]])
(static) ones(rows, cols) → {Matrix}
Fill up an empty matrix with ones
Parameters:
Name | Type | Description |
---|---|---|
rows |
Number
|
Defines the rows of the matrix |
cols |
Number
|
Defines the columns of the matrix |
Returns:
- Type:
-
Matrix
Example
const A = Matrix.ones(1, 1)
// [[1,1,1], [1,1,1], [1,1,1]]
(static) random(f, rows, cols) → {Matrix}
Fill up an empty matrix with random numbers
Parameters:
Name | Type | Description |
---|---|---|
f |
function
|
Function which returns random values. Default random values are between -1 and 1 |
rows |
Number
|
Defines the rows of the matrix |
cols |
Number
|
Defines the columns of the matrix |
Returns:
- Type:
-
Matrix
(static) sum(M) → {Number}
Returns the sum of the values in the Matrix
Parameters:
Name | Type | Description |
---|---|---|
M |
Matrix
|
Array
|
Matrix from which to return the diagonal |
Returns:
- Type:
-
Number
Example
const diag1 = Matrix.sum([[2, 1], [1, 5]])
// 9
(static) transpose(M) → {Matrix}
Returns a transposed Matrix
Parameters:
Name | Type | Description |
---|---|---|
M |
Matrix
|
Array
|
A Matrix or a matrix array |
Returns:
- Type:
-
Matrix
Example
const a = [-1, 2], [3, 4], [-8, 2]
const b = Matrix.transpose(a).toArray()
// returns [[-1, 3,-8], [2, 4, 2]]
(static) zeros(rows, cols) → {Matrix}
Fill up an empty matrix with zeros
Parameters:
Name | Type | Description |
---|---|---|
rows |
Number
|
Defines the rows of the matrix |
cols |
Number
|
Defines the columns of the matrix |
Returns:
- Type:
-
Matrix
Example
const A = Matrix.zeros(3, 3)
// [[0,0,0], [0,0,0], [0,0,0]]
add(M) → {Matrix}
Adds a number or a Matrix to this
Parameters:
Name | Type | Description |
---|---|---|
M |
Matrix
|
Number
|
Add a Matrix or a number |
Returns:
- Type:
-
Matrix
Example
const A = Matrix.of([[5, 4]])
A.add(1) // [[6, 5]]
const B = Matrix.of([[5, 5]])
B.add(B) // [[10, 10]]
additiveinverse() → {Matrix}
Function that returns the matrix obtained by changing the sign of every matrix element. The additive inverse of matrix A is written –A.
Returns:
- Type:
-
Matrix
Example
const A = Matrix.of([[5,-5], [-4, 4]])
const minusA = A.additiveinverse()
// [[-5, 5], [4, -4]]
ap(M) → {Matrix}
Function that applies a function to a Matrix
Parameters:
Name | Type | Description |
---|---|---|
M |
Matrix
|
Array
|
Matrix or Array to apply a function |
Returns:
- Type:
-
Matrix
Example
const f = x => x.reduce((prev, next) => prev + next)
const A = Matrix.of([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
Matrix.of(f).ap(M)
// [[6], [15], [24]
combine(M) → {Matrix}
Concatenates 2 Matrices together.
- Source:
- See:
Parameters:
Name | Type | Description |
---|---|---|
M |
Matrix
|
Right side Matrix of the combine operation |
Returns:
- Type:
-
Matrix
concat(M, fopt) → {Matrix}
Concatenates 2 Matrices using a function as operator
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
M |
Matrix
|
The right side of the concatenation/product | ||
f |
function
|
<optional> |
concat | A curried function accepting 2 matrices as input |
Returns:
- Type:
-
Matrix
Example
const a = [[0, 1, 1], [2, 3, 4]]
const b = [[2, 2, 2], [3, 3, 3]]
const A = Matrix.of(a)
const B = Matrix.of(b)
const M = A.concat(B)
// [[0, 1, 1, 2, 2, 2], [2, 3, 4, 3, 3, 3]]
determinant() → {Number}
Calculates the determinant of a square Matrix using Sarrus' rule or LU decomposition
Returns:
- Type:
-
Number
diag() → {Array}
Returns an array containing the values on the diagonal
Returns:
- Type:
-
Array
Example
const diag1 = Matrix.ones(3, 3).diag()
// [1, 1, 1]
const diag0 = Matrix.zeros(5, 5).diag()
// [0, 0, 0, 0, 0]
diagproduct() → {Number}
Returns the product of the values on the diagonal
Returns:
- Type:
-
Number
Example
const diag1 = Matrix.ones(3, 3).diagproduct()
// 1
const diag0 = Matrix.zeros(5, 5).diagproduct()
// 0
dimension() → {Number}
Number indicating the maximum number of linearly independent columns.
Returns:
- Type:
-
Number
divide(M) → {Matrix}
Divide a scalar or a matrix by a matrix. Throws an error if the division is not possible.
Parameters:
Name | Type | Description |
---|---|---|
M |
Matrix
|
Number
|
A Matrix M or a Number to divide a Matrix |
Returns:
- Type:
-
Matrix
Example
const A = Matrix.of([[5, 4]])
A.divide(2) // [[10, 8]]
const B = Matrix.of([[1, 1], [2, 4]])
B.divide(B) // [[1, 0], [0, 1]]
dot(M) → {Matrix}
Returns the dot product between 2 matrices
Parameters:
Name | Type | Description |
---|---|---|
M |
Matrix
|
Array
|
Right side of the dot product |
Returns:
- Type:
-
Matrix
Example
// Create matrix
const m = Matrix.of([[1, 2], [3, 4]])
// Generate matrixIdentity matrix
const I = m.matrixIdentity() // [[1, 0], [0, 1]]
if(m.dot(I).equals(m)) {
console.log('Dot product with matrixIdentity matrix returns the same matrix')
}
equals(M) → {Boolean}
Function returning a boolean testing for equality of the values of a Matrix or Array
Parameters:
Name | Type | Description |
---|---|---|
M |
Matrix
|
Array
|
Matrix or Array to compare for equality |
Returns:
- Type:
-
Boolean
Example
var a = [[1, 1], [1, 1]]
var A = Matrix.of(a)
var B = Matrix.of(a)
true === A.equals(B)
fill(f) → {Matrix}
Fill up an empty matrix with the provided map function
Parameters:
Name | Type | Description |
---|---|---|
f |
function
|
Function that generates a value |
Returns:
- Type:
-
Matrix
Example
const A = Matrix.of([[1,2,3], [3,2,1], [4,5,6]]).fill(x => 42)
// [[42,42,42], [42,42,42], [42,42,42]]
flatMap(fn) → {*}
Runs flatMap on the value of hte Matrix
Parameters:
Name | Type | Description |
---|---|---|
fn |
function
|
Flatten function |
Returns:
- Type:
-
*
Example
const a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
const A = Matrix.of(a)
const flattenedArray = A.flatMap(x => x) // [1, 2, 3, 4, 5, 6, 7, 8, 9]
flatten() → {Array}
Flattens the value of a Matrix into an one dimensional array
Returns:
- Type:
-
Array
Example
const a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
const A = Matrix.of(a)
const flattenedArray = A.flatten() // [1, 2, 3, 4, 5, 6, 7, 8, 9]
fold(f) → {Matrix}
Reduce the matrix rows using a reduce function
Parameters:
Name | Type | Description |
---|---|---|
f |
function
|
A reduce/fold function |
Returns:
- Type:
-
Matrix
Example
// Flatten Matrix
Matrix.of([[1, 1], [1, 1]]).fold((prev, next) => prev.concat(next))
// [1, 1, 1, 1]
fromArray() → {Array}
Returns a Matrix from an array
Returns:
- Type:
-
Array
getCols() → {Number}
Number indicating the number of columns in the Matrix
Returns:
- Type:
-
Number
Example
const A = Matrix.of([[1, 1], [1, 1]])
A.getCols() === 2
getColumn(index) → {Array}
Returns the values of a Matrix column
Parameters:
Name | Type | Description |
---|---|---|
index |
Number
|
Index of the column |
Returns:
- Type:
-
Array
getRow(index) → {Array}
Returns the values of a Matrix row
Parameters:
Name | Type | Description |
---|---|---|
index |
Number
|
Index of the row |
Returns:
- Type:
-
Array
getRows() → {Number}
Number indicating the number of rows in the Matrix
Returns:
- Type:
-
Number
Example
const A = Matrix.of([[1, 1], [1, 1]])
A.getRows() // 2
getShape() → {Array}
Returns:
- Type:
-
Array
Example
const A = Matrix.of([[1, 1], [1, 1]])
A.getShape() // [2, 2]
hadamard(M) → {Matrix}
Hadamar is an alias of the multiply function
Parameters:
Name | Type | Description |
---|---|---|
M |
Matrix
|
Number
|
A Matrix M or a Number to multiply a Matrix |
Returns:
- Type:
-
Matrix
Example
const A = Matrix.of([[5, 4]])
A.hadamard(2) // [[10, 8]]
const B = Matrix.of([[5, 5]])
B.hadamard(B) // [[25, 25]]
inverse() → {Matrix}
Returns the inverse of a Matrix
Returns:
- Type:
-
Matrix
Example
const A = Matrix.of([[1, 1], [2, 4]]).inverse()
// [ [ 2, -0.5 ], [ -1, 0.5 ] ]
isOrthogonal() → {Boolean}
Boolean indicating whether the Matrix is orthogonal by testing for equality between Identity Matrix and the dot product of the Matrix and its transpose.
Returns:
- Type:
-
Boolean
Example
const result = [[-0.3092, -0.9510], [-0.9510, 0.3092]]
const A = Matrix.fromArray(result)
true === A.isOrthogonal()
isSquare() → {Boolean}
Boolean indicating whether the Matrix object is square.
Returns:
- Type:
-
Boolean
Example
const A = Matrix.of([[1, 1], [1, 1]])
true === A.isSquare()
isSymmetric() → {Boolean}
Boolean indicating whether the Matrix is symmetric by testing for equality of the transposed Matrix.
Returns:
- Type:
-
Boolean
Example
const A = Matrix.of([[1, 1], [1, 1]])
true === A.isSymmetric()
kronecker(M) → {Matrix}
The Kronecker product is an operation on two matrices of arbitrary size resulting in a block matrix.
Parameters:
Name | Type | Description |
---|---|---|
M |
Matrix
|
The right side Matrix of the product (this ⊗ M) |
Returns:
- Type:
-
Matrix
lu() → {Array.<Matrix>}
Calculates LU decomposition of the Matrix
Returns:
- Type:
-
Array.<Matrix>
Example
const result = [[3, -7, -2, 2], [-3, 5, 1, 0], [6, -4, 0, -5], [-9, 5, -5, 12]]
const A = Matrix.fromArray(result)
const lu = A.lu()
// L.__value = [ [ 1, 0, 0, 0 ], [ -1, 1, 0, 0 ], [ 2, -5, 1, 0 ], [ -3, 8, 3, 1 ] ]
// U.__value = [ [ 3, -7, -2, 2 ], [ 0, -2, -1, 2 ], [ 0, 0, -1, 1 ], [ 0, 0, 0, -1 ] ]
Matrix.dot(lu[0], lu[1]) // returns clone of A
map(f) → {Matrix}
Maps over the rows of the matrix using a map function
Parameters:
Name | Type | Description |
---|---|---|
f |
function
|
An iterator function |
Returns:
- Type:
-
Matrix
Example
const m = Matrix.of([[1, 1], [1, 1]])
m.map(x => x.map(y => y+ 1))
// [[2, 2], [2, 2]]
matrixIdentity() → {Matrix}
Returns an matrixIdentity matrix
Returns:
- Type:
-
Matrix
Example
const a = [[1, 2, 3], [4, 5, 6]]
const A = Matrix.of(a)
const Aidentity = A.matrixIdentity()
// [[1, 0, 0], [0, 1, 0]]
multiply(M) → {Matrix}
Mutliply a scalar or a matrix with a matrix. Throws an error if the multiplication is not possible.
Parameters:
Name | Type | Description |
---|---|---|
M |
Matrix
|
Number
|
A Matrix M or a Number to multiply a Matrix |
Returns:
- Type:
-
Matrix
Example
const A = Matrix.of([[5, 4]])
A.multiply(2) // [[10, 8]]
const B = Matrix.of([[5, 5]])
B.multiply(B) // [[25, 25]]
ones() → {Matrix}
Fill up an empty matrix with ones
Returns:
- Type:
-
Matrix
Example
const A = Matrix.of([[1,2,3], [3,2,1], [4,5,6]]).ones()
// [[1,1,1], [1,1,1], [1,1,1]]
random(fopt) → {Matrix}
Fill up an empty matrix with random values
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
f |
function
|
<optional> |
e => Math.random() * 2 - 1 | Function producing random values, can be any type of value |
Returns:
- Type:
-
Matrix
rank() → {Number}
Number indicating the maximum number of linearly independent columns.
Returns:
- Type:
-
Number
rref() → {Matrix}
Returns a Matrix containing the row reduced echelon form
Returns:
- Type:
-
Matrix
Example
var A = Matrix.of([[-1, 1], [-1, 0], [0, -1], [-1, -2]])
A.rref() // [ [ 1, 0 ], [ -0, 1 ], [ 0, 0 ], [ 0, 0 ] ]
setPrecision(precisionopt)
The precision is used in floating point calculations for the dot product
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
precision |
Number
|
<optional> |
4 | Set the number of decimals for rounding |
Example
const m = Matrix.of([[1,2],[2,3],[4,5]])
m.setPrecision(10)
m.precision === 10
solve(b) → {Array}
Returns the solution for a system of linear equations
Parameters:
Name | Type | Description |
---|---|---|
b |
Array
|
The numbers for which to solve the system of linear equations |
Returns:
- Type:
-
Array
Example
// Solve xA = b
// 5x + y = 7
// 3x - 4y = 18
// Solution for x and y:
// x = 2
// y = -3
const A = Matrix.of([[5, 1], [3, -4]])
const solveA = A.solve([7, 18]) // [2, -3]
subtract(M) → {Matrix}
Subtracts a number or a Matrix from this
Parameters:
Name | Type | Description |
---|---|---|
M |
Matrix
|
Number
|
Subtract a Matrix or a number |
Returns:
- Type:
-
Matrix
Example
const A = Matrix.of([[5, 4]])
A.subtract(1) // [[4, 2]]
const B = Matrix.of([[5, 5]])
B.subtract(B) // [[0, 0]]
sum() → {Number}
Returns the sum of the values in the Matrix
Returns:
- Type:
-
Number
Example
const diag1 = Matrix.ones(3, 3).sum()
// 9
const diag0 = Matrix.zeros(5, 5).sum()
// 0
toArray() → {Array}
Returns the array from the matrix
Returns:
- Type:
-
Array