common.js
A small library of useful functions
Usage
Node:
const { vec, mat } = require('@basementuniverse/commonjs');
Browser:
<script src="common.js"></script>
Typescript:
import { vec, mat } from '@basementuniverse/commonjs';
Contents
Classes
Functions
-
vec([x], [y]) ⇒
vec
-
Create a new vector
-
mat([m], [n], [entries]) ⇒
mat
-
Create a new matrix
Typedefs
-
interpolationCallback ⇒
number
-
An interpolation function
-
timesCallback ⇒
*
-
A function for generating array values
-
vec :
Object
-
A 2d vector
-
vectorMapCallback ⇒
number
-
A function to call on each component of a vector
-
mat :
Object
-
A matrix
-
matrixMapCallback ⇒
number
-
A function to call on each entry of a matrix
Math
Kind: global class
-
Math
-
.floatEquals(a, b, [p]) ⇒
boolean
-
.clamp(a, [min], [max]) ⇒
number
-
.frac(a) ⇒
number
-
.lerp(a, b, i) ⇒
number
-
.unlerp(a, b, i) ⇒
number
-
.blerp(c00, c10, c01, c11, ix, iy) ⇒
number
-
.remap(i, a1, a2, b1, b2) ⇒
number
-
.smoothstep(a, b, i) ⇒
number
-
.radians(degrees) ⇒
number
-
.degrees(radians) ⇒
number
-
.randomBetween(min, max) ⇒
number
-
.randomIntBetween(min, max) ⇒
number
-
.cltRandom([mu], [sigma], [samples]) ⇒
number
-
.cltRandomInt(min, max) ⇒
number
-
.weightedRandom(w) ⇒
number
-
.lerpArray(a, i, [f]) ⇒
number
-
.dot(a, b) ⇒
number
-
.factorial(a) ⇒
number
-
.permutation(n, r) ⇒
number
-
.combination(n, r) ⇒
number
-
.floatEquals(a, b, [p]) ⇒
boolean
Math.floatEquals(a, b, [p]) ⇒ Check if two numbers are approximately equal
Kind: static method of Math
Returns: boolean
- True if numbers a and b are approximately equal
Param | Type | Default | Description |
---|---|---|---|
a | number |
Number a | |
b | number |
Number b | |
[p] | number |
Number.EPSILON |
The precision value |
number
Math.clamp(a, [min], [max]) ⇒ Clamp a number between min and max
Kind: static method of Math
Returns: number
- A clamped number
Param | Type | Default | Description |
---|---|---|---|
a | number |
The number to clamp | |
[min] | number |
0 |
The minimum value |
[max] | number |
1 |
The maximum value |
number
Math.frac(a) ⇒ Get the fractional part of a number
Kind: static method of Math
Returns: number
- The fractional part of the number
Param | Type | Description |
---|---|---|
a | number |
The number from which to get the fractional part |
number
Math.lerp(a, b, i) ⇒ Do a linear interpolation between a and b
Kind: static method of Math
Returns: number
- An interpolated value in the interval [a, b]
Param | Type | Description |
---|---|---|
a | number |
The minimum number |
b | number |
The maximum number |
i | number |
The interpolation value, should be in the interval [0, 1] |
number
Math.unlerp(a, b, i) ⇒ Get the position of i between a and b
Kind: static method of Math
Returns: number
- The position of i between a and b
Param | Type | Description |
---|---|---|
a | number |
The minimum number |
b | number |
The maximum number |
i | number |
The interpolated value in the interval [a, b] |
number
Math.blerp(c00, c10, c01, c11, ix, iy) ⇒ Do a bilinear interpolation
Kind: static method of Math
Returns: number
- A bilinear interpolated value
Param | Type | Description |
---|---|---|
c00 | number |
Top-left value |
c10 | number |
Top-right value |
c01 | number |
Bottom-left value |
c11 | number |
Bottom-right value |
ix | number |
Interpolation value along x |
iy | number |
Interpolation value along y |
number
Math.remap(i, a1, a2, b1, b2) ⇒ Re-map a number i from range a1...a2 to b1...b2
Kind: static method of Math
Param | Type | Description |
---|---|---|
i | number |
The number to re-map |
a1 | number |
|
a2 | number |
|
b1 | number |
|
b2 | number |
number
Math.smoothstep(a, b, i) ⇒ Do a smooth interpolation between a and b
Kind: static method of Math
Returns: number
- An interpolated value in the interval [a, b]
Param | Type | Description |
---|---|---|
a | number |
The minimum number |
b | number |
The maximum number |
i | number |
The interpolation value |
number
Math.radians(degrees) ⇒ Get an angle in radians
Kind: static method of Math
Returns: number
- The angle in radians
Param | Type | Description |
---|---|---|
degrees | number |
The angle in degrees |
number
Math.degrees(radians) ⇒ Get an angle in degrees
Kind: static method of Math
Returns: number
- The angle in degrees
Param | Type | Description |
---|---|---|
radians | number |
The angle in radians |
number
Math.randomBetween(min, max) ⇒ Get a random float in the interval [min, max)
Kind: static method of Math
Returns: number
- A random float in the interval [min, max)
Param | Type | Description |
---|---|---|
min | number |
Inclusive min |
max | number |
Exclusive max |
number
Math.randomIntBetween(min, max) ⇒ Get a random integer in the interval [min, max]
Kind: static method of Math
Returns: number
- A random integer in the interval [min, max]
Param | Type | Description |
---|---|---|
min | number |
Inclusive min |
max | number |
Inclusive max |
number
Math.cltRandom([mu], [sigma], [samples]) ⇒ Get a normally-distributed random number
Kind: static method of Math
Returns: number
- A normally-distributed random number
Param | Type | Default | Description |
---|---|---|---|
[mu] | number |
0.5 |
The mean value |
[sigma] | number |
0.5 |
The standard deviation |
[samples] | number |
2 |
The number of samples |
number
Math.cltRandomInt(min, max) ⇒ Get a normally-distributed random integer in the interval [min, max]
Kind: static method of Math
Returns: number
- A normally-distributed random integer
Param | Type | Description |
---|---|---|
min | number |
Inclusive min |
max | number |
Inclusive max |
number
Math.weightedRandom(w) ⇒ Return a weighted random integer
Kind: static method of Math
Returns: number
- An index from w
Param | Type | Description |
---|---|---|
w | Array.<number> |
An array of weights |
number
Math.lerpArray(a, i, [f]) ⇒ Return an interpolated value from an array
Kind: static method of Math
Returns: number
- An interpolated value in the interval [min(a), max(a)]
Param | Type | Default | Description |
---|---|---|---|
a | Array.<number> |
An array of values interpolate | |
i | number |
A number in the interval [0, 1] | |
[f] | interpolationCallback |
Math.lerp |
The interpolation function to use |
number
Math.dot(a, b) ⇒ Get the dot product of two vectors
Kind: static method of Math
Returns: number
- a ∙ b
Param | Type | Description |
---|---|---|
a | Array.<number> |
Vector a |
b | Array.<number> |
Vector b |
number
Math.factorial(a) ⇒ Get the factorial of a number
Kind: static method of Math
Returns: number
- a!
Param | Type |
---|---|
a | number |
number
Math.permutation(n, r) ⇒ Get the number of permutations of r elements from a set of n elements
Kind: static method of Math
Returns: number
- nPr
Param | Type |
---|---|
n | number |
r | number |
number
Math.combination(n, r) ⇒ Get the number of combinations of r elements from a set of n elements
Kind: static method of Math
Returns: number
- nCr
Param | Type |
---|---|
n | number |
r | number |
Array
Kind: global class
-
Array
-
instance
-
.at(i) ⇒
*
-
.chunk(n) ⇒
Array.<Array.<*>>
-
.shuffle() ⇒
Array.<*>
-
.at(i) ⇒
-
static
-
.times(f, n) ⇒
Array.<*>
-
.range(n) ⇒
Array.<number>
-
.zip(a, b) ⇒
Array.<Array.<*>>
-
.times(f, n) ⇒
-
instance
*
array.at(i) ⇒ Return array[i] with positive and negative wrapping
Kind: instance method of Array
Returns: *
- An element from the array
Param | Type | Description |
---|---|---|
i | number |
The positively/negatively wrapped array index |
Array.<Array.<*>>
array.chunk(n) ⇒ Chop an array into chunks of size n
Kind: instance method of Array
Returns: Array.<Array.<*>>
- An array of array chunks
Param | Type | Description |
---|---|---|
n | number |
The chunk size |
Array.<*>
array.shuffle() ⇒ Randomly shuffle an array in-place
Kind: instance method of Array
Returns: Array.<*>
- The shuffled array
Array.<*>
Array.times(f, n) ⇒ Return a new array with length n by calling function f(i) on each element
Kind: static method of Array
Param | Type | Description |
---|---|---|
f | timesCallback |
|
n | number |
The size of the array |
Array.<number>
Array.range(n) ⇒ Return an array containing numbers 0->(n - 1)
Kind: static method of Array
Returns: Array.<number>
- An array of integers 0->(n - 1)
Param | Type | Description |
---|---|---|
n | number |
The size of the array |
Array.<Array.<*>>
Array.zip(a, b) ⇒ Zip 2 arrays together, i.e. ([1, 2, 3], [a, b, c]) => [[1, a], [2, b], [3, c]]
Kind: static method of Array
Param | Type |
---|---|
a | Array.<*> |
b | Array.<*> |
vec
vec([x], [y]) ⇒ Create a new vector
Kind: global function
Returns: vec
- A new vector
Param | Type | Description |
---|---|---|
[x] |
number | vec
|
The x component of the vector, or a vector to copy |
[y] | number |
The y component of the vector |
Example (Various ways to initialise a vector)
let a = vec(3, 2); // (3, 2)
let b = vec(4); // (4, 4)
let c = vec(a); // (3, 2)
let d = vec(); // (0, 0)
-
vec([x], [y]) ⇒
vec
-
.components(a) ⇒
Array.<number>
-
.ux() ⇒
vec
-
.uy() ⇒
vec
-
.add(a, b) ⇒
vec
-
.mul(a, b) ⇒
vec
-
.sub(a, b) ⇒
vec
-
.len(a) ⇒
number
-
.manhattan(a) ⇒
number
-
.nor(a) ⇒
vec
-
.dot(a, b) ⇒
number
-
.rot(a, r) ⇒
vec
-
.eq(a, b) ⇒
boolean
-
.rad(a) ⇒
number
-
.cpy(a) ⇒
vec
-
.map(a, f) ⇒
vec
-
.str(a, [s]) ⇒
string
-
.components(a) ⇒
Array.<number>
vec.components(a) ⇒ Get the components of a vector as an array
Kind: static method of vec
Returns: Array.<number>
- The vector components as an array
Param | Type | Description |
---|---|---|
a | vec |
The vector to get components from |
vec
vec.ux() ⇒ Return a unit vector (1, 0)
Kind: static method of vec
Returns: vec
- A unit vector (1, 0)
vec
vec.uy() ⇒ Return a unit vector (0, 1)
Kind: static method of vec
Returns: vec
- A unit vector (0, 1)
vec
vec.add(a, b) ⇒ Add vectors
Kind: static method of vec
Returns: vec
- a + b
Param | Type | Description |
---|---|---|
a | vec |
Vector a |
b | vec |
Vector b |
vec
vec.mul(a, b) ⇒ Scale a vector
Kind: static method of vec
Returns: vec
- a * b
Param | Type | Description |
---|---|---|
a | vec |
Vector a |
b | number |
Scalar b |
vec
vec.sub(a, b) ⇒ Subtract vectors
Kind: static method of vec
Returns: vec
- a - b
Param | Type | Description |
---|---|---|
a | vec |
Vector a |
b | vec |
Vector b |
number
vec.len(a) ⇒ Get the length of a vector
Kind: static method of vec
Returns: number
- |a|
Param | Type | Description |
---|---|---|
a | vec |
Vector a |
number
vec.manhattan(a) ⇒ Get the length of a vector using taxicab geometry
Kind: static method of vec
Returns: number
- |a|
Param | Type | Description |
---|---|---|
a | vec |
Vector a |
vec
vec.nor(a) ⇒ Normalise a vector
Kind: static method of vec
Returns: vec
- ^a
Param | Type | Description |
---|---|---|
a | vec |
The vector to normalise |
number
vec.dot(a, b) ⇒ Get a dot product of vectors
Kind: static method of vec
Returns: number
- a ∙ b
Param | Type | Description |
---|---|---|
a | vec |
Vector a |
b | vec |
Vector b |
vec
vec.rot(a, r) ⇒ Rotate a vector by r radians
Kind: static method of vec
Returns: vec
- A rotated vector
Param | Type | Description |
---|---|---|
a | vec |
The vector to rotate |
r | number |
The angle to rotate by, measured in radians |
boolean
vec.eq(a, b) ⇒ Check if two vectors are equal
Kind: static method of vec
Returns: boolean
- True if vectors a and b are equal, false otherwise
Param | Type | Description |
---|---|---|
a | vec |
Vector a |
b | vec |
Vector b |
number
vec.rad(a) ⇒ Get the angle of a vector
Kind: static method of vec
Returns: number
- The angle of vector a in radians
Param | Type | Description |
---|---|---|
a | vec |
Vector a |
vec
vec.cpy(a) ⇒ Copy a vector
Kind: static method of vec
Returns: vec
- A copy of vector a
Param | Type | Description |
---|---|---|
a | vec |
The vector to copy |
vec
vec.map(a, f) ⇒ Call a function on each component of a vector and build a new vector from the results
Kind: static method of vec
Returns: vec
- Vector a mapped through f
Param | Type | Description |
---|---|---|
a | vec |
Vector a |
f | vectorMapCallback |
The function to call on each component of the vector |
string
vec.str(a, [s]) ⇒ Convert a vector into a string
Kind: static method of vec
Returns: string
- A string representation of the vector
Param | Type | Default | Description |
---|---|---|---|
a | vec |
The vector to convert | |
[s] | string |
"', '" |
The separator string |
mat
mat([m], [n], [entries]) ⇒ Create a new matrix
Kind: global function
Returns: mat
- A new matrix
Param | Type | Default | Description |
---|---|---|---|
[m] | number |
4 |
The number of rows |
[n] | number |
4 |
The number of columns |
[entries] | Array.<number> |
[] |
Matrix values in reading order |
-
mat([m], [n], [entries]) ⇒
mat
-
.identity(n) ⇒
mat
-
.get(a, i, j) ⇒
number
- .set(a, i, j, v)
-
.row(a, m) ⇒
Array.<number>
-
.col(a, n) ⇒
Array.<number>
-
.add(a, b) ⇒
mat
-
.sub(a, b) ⇒
mat
-
.mul(a, b) ⇒
mat
|boolean
-
.scale(a, b) ⇒
mat
-
.trans(a) ⇒
mat
-
.minor(a, i, j) ⇒
mat
|boolean
-
.det(a) ⇒
number
|boolean
-
.nor(a) ⇒
mat
|boolean
-
.adj(a) ⇒
mat
-
.inv(a) ⇒
mat
|boolean
-
.eq(a, b) ⇒
boolean
-
.cpy(a) ⇒
mat
-
.map(a, f) ⇒
mat
-
.str(a, [ms], [ns]) ⇒
string
-
.identity(n) ⇒
mat
mat.identity(n) ⇒ Get an identity matrix of size n
Kind: static method of mat
Returns: mat
- An identity matrix
Param | Type | Description |
---|---|---|
n | number |
The size of the matrix |
number
mat.get(a, i, j) ⇒ Get an entry from a matrix
Kind: static method of mat
Returns: number
- The value at position (i, j) in matrix a
Param | Type | Description |
---|---|---|
a | mat |
Matrix a |
i | number |
The row offset |
j | number |
The column offset |
mat.set(a, i, j, v)
Set an entry of a matrix
Kind: static method of mat
Param | Type | Description |
---|---|---|
a | mat |
Matrix a |
i | number |
The row offset |
j | number |
The column offset |
v | number |
The value to set in matrix a |
Array.<number>
mat.row(a, m) ⇒ Get a row from a matrix as an array
Kind: static method of mat
Returns: Array.<number>
- Row m from matrix a
Param | Type | Description |
---|---|---|
a | mat |
Matrix a |
m | number |
The row offset |
Array.<number>
mat.col(a, n) ⇒ Get a column from a matrix as an array
Kind: static method of mat
Returns: Array.<number>
- Column n from matrix a
Param | Type | Description |
---|---|---|
a | mat |
Matrix a |
n | number |
The column offset |
mat
mat.add(a, b) ⇒ Add matrices
Kind: static method of mat
Returns: mat
- a + b
Param | Type | Description |
---|---|---|
a | mat |
Matrix a |
b | mat |
Matrix b |
mat
mat.sub(a, b) ⇒ Subtract matrices
Kind: static method of mat
Returns: mat
- a - b
Param | Type | Description |
---|---|---|
a | mat |
Matrix a |
b | mat |
Matrix b |
mat
| boolean
mat.mul(a, b) ⇒ Multiply matrices
Kind: static method of mat
Returns: mat
| boolean
- ab or false if the matrices cannot be multiplied
Param | Type | Description |
---|---|---|
a | mat |
Matrix a |
b | mat |
Matrix b |
mat
mat.scale(a, b) ⇒ Scale a matrix
Kind: static method of mat
Returns: mat
- a * b
Param | Type | Description |
---|---|---|
a | mat |
Matrix a |
b | number |
Scalar b |
mat
mat.trans(a) ⇒ Transpose a matrix
Kind: static method of mat
Returns: mat
- A transposed matrix
Param | Type | Description |
---|---|---|
a | mat |
The matrix to transpose |
mat
| boolean
mat.minor(a, i, j) ⇒ Get the minor of a matrix
Kind: static method of mat
Returns: mat
| boolean
- The (i, j) minor of matrix a or false if the matrix is not square
Param | Type | Description |
---|---|---|
a | mat |
Matrix a |
i | number |
The row offset |
j | number |
The column offset |
number
| boolean
mat.det(a) ⇒ Get the determinant of a matrix
Kind: static method of mat
Returns: number
| boolean
- |a| or false if the matrix is not square
Param | Type | Description |
---|---|---|
a | mat |
Matrix a |
mat
| boolean
mat.nor(a) ⇒ Normalise a matrix
Kind: static method of mat
Returns: mat
| boolean
- ^a or false if the matrix is not square
Param | Type | Description |
---|---|---|
a | mat |
The matrix to normalise |
mat
mat.adj(a) ⇒ Get the adjugate of a matrix
Kind: static method of mat
Returns: mat
- The adjugate of a
Param | Type | Description |
---|---|---|
a | mat |
The matrix from which to get the adjugate |
mat
| boolean
mat.inv(a) ⇒ Get the inverse of a matrix
Kind: static method of mat
Returns: mat
| boolean
- a^-1 or false if the matrix has no inverse
Param | Type | Description |
---|---|---|
a | mat |
The matrix to invert |
boolean
mat.eq(a, b) ⇒ Check if two matrices are equal
Kind: static method of mat
Returns: boolean
- True if matrices a and b are identical, false otherwise
Param | Type | Description |
---|---|---|
a | mat |
Matrix a |
b | mat |
Matrix b |
mat
mat.cpy(a) ⇒ Copy a matrix
Kind: static method of mat
Returns: mat
- A copy of matrix a
Param | Type | Description |
---|---|---|
a | mat |
The matrix to copy |
mat
mat.map(a, f) ⇒ Call a function on each entry of a matrix and build a new matrix from the results
Kind: static method of mat
Returns: mat
- Matrix a mapped through f
Param | Type | Description |
---|---|---|
a | mat |
Matrix a |
f | matrixMapCallback |
The function to call on each entry of the matrix |
string
mat.str(a, [ms], [ns]) ⇒ Convert a matrix into a string
Kind: static method of mat
Returns: string
- A string representation of the matrix
Param | Type | Default | Description |
---|---|---|---|
a | mat |
The matrix to convert | |
[ms] | string |
"', '" |
The separator string for columns |
[ns] | string |
"'\n'" |
The separator string for rows |
number
interpolationCallback ⇒ An interpolation function
Kind: global typedef
Returns: number
- The interpolated value in the interval [a, b]
Param | Type | Description |
---|---|---|
a | number |
The minimum number |
b | number |
The maximum number |
i | number |
The interpolation value, should be in the interval [0, 1] |
*
timesCallback ⇒ A function for generating array values
Kind: global typedef
Returns: *
- The array value
Param | Type | Description |
---|---|---|
i | number |
The array index |
Object
vec : A 2d vector
Kind: global typedef
Properties
Name | Type | Description |
---|---|---|
x | number |
The x component of the vector |
y | number |
The y component of the vector |
-
vec :
Object
-
.components(a) ⇒
Array.<number>
-
.ux() ⇒
vec
-
.uy() ⇒
vec
-
.add(a, b) ⇒
vec
-
.mul(a, b) ⇒
vec
-
.sub(a, b) ⇒
vec
-
.len(a) ⇒
number
-
.manhattan(a) ⇒
number
-
.nor(a) ⇒
vec
-
.dot(a, b) ⇒
number
-
.rot(a, r) ⇒
vec
-
.eq(a, b) ⇒
boolean
-
.rad(a) ⇒
number
-
.cpy(a) ⇒
vec
-
.map(a, f) ⇒
vec
-
.str(a, [s]) ⇒
string
-
.components(a) ⇒
Array.<number>
vec.components(a) ⇒ Get the components of a vector as an array
Kind: static method of vec
Returns: Array.<number>
- The vector components as an array
Param | Type | Description |
---|---|---|
a | vec |
The vector to get components from |
vec
vec.ux() ⇒ Return a unit vector (1, 0)
Kind: static method of vec
Returns: vec
- A unit vector (1, 0)
vec
vec.uy() ⇒ Return a unit vector (0, 1)
Kind: static method of vec
Returns: vec
- A unit vector (0, 1)
vec
vec.add(a, b) ⇒ Add vectors
Kind: static method of vec
Returns: vec
- a + b
Param | Type | Description |
---|---|---|
a | vec |
Vector a |
b | vec |
Vector b |
vec
vec.mul(a, b) ⇒ Scale a vector
Kind: static method of vec
Returns: vec
- a * b
Param | Type | Description |
---|---|---|
a | vec |
Vector a |
b | number |
Scalar b |
vec
vec.sub(a, b) ⇒ Subtract vectors
Kind: static method of vec
Returns: vec
- a - b
Param | Type | Description |
---|---|---|
a | vec |
Vector a |
b | vec |
Vector b |
number
vec.len(a) ⇒ Get the length of a vector
Kind: static method of vec
Returns: number
- |a|
Param | Type | Description |
---|---|---|
a | vec |
Vector a |
number
vec.manhattan(a) ⇒ Get the length of a vector using taxicab geometry
Kind: static method of vec
Returns: number
- |a|
Param | Type | Description |
---|---|---|
a | vec |
Vector a |
vec
vec.nor(a) ⇒ Normalise a vector
Kind: static method of vec
Returns: vec
- ^a
Param | Type | Description |
---|---|---|
a | vec |
The vector to normalise |
number
vec.dot(a, b) ⇒ Get a dot product of vectors
Kind: static method of vec
Returns: number
- a ∙ b
Param | Type | Description |
---|---|---|
a | vec |
Vector a |
b | vec |
Vector b |
vec
vec.rot(a, r) ⇒ Rotate a vector by r radians
Kind: static method of vec
Returns: vec
- A rotated vector
Param | Type | Description |
---|---|---|
a | vec |
The vector to rotate |
r | number |
The angle to rotate by, measured in radians |
boolean
vec.eq(a, b) ⇒ Check if two vectors are equal
Kind: static method of vec
Returns: boolean
- True if vectors a and b are equal, false otherwise
Param | Type | Description |
---|---|---|
a | vec |
Vector a |
b | vec |
Vector b |
number
vec.rad(a) ⇒ Get the angle of a vector
Kind: static method of vec
Returns: number
- The angle of vector a in radians
Param | Type | Description |
---|---|---|
a | vec |
Vector a |
vec
vec.cpy(a) ⇒ Copy a vector
Kind: static method of vec
Returns: vec
- A copy of vector a
Param | Type | Description |
---|---|---|
a | vec |
The vector to copy |
vec
vec.map(a, f) ⇒ Call a function on each component of a vector and build a new vector from the results
Kind: static method of vec
Returns: vec
- Vector a mapped through f
Param | Type | Description |
---|---|---|
a | vec |
Vector a |
f | vectorMapCallback |
The function to call on each component of the vector |
string
vec.str(a, [s]) ⇒ Convert a vector into a string
Kind: static method of vec
Returns: string
- A string representation of the vector
Param | Type | Default | Description |
---|---|---|---|
a | vec |
The vector to convert | |
[s] | string |
"', '" |
The separator string |
number
vectorMapCallback ⇒ A function to call on each component of a vector
Kind: global typedef
Returns: number
- The mapped component
Param | Type | Description |
---|---|---|
value | number |
The component value |
label |
'x' | 'y'
|
The component label (x or y) |
Object
mat : A matrix
Kind: global typedef
Properties
Name | Type | Description |
---|---|---|
m | number |
The number of rows in the matrix |
n | number |
The number of columns in the matrix |
entries | Array.<number> |
The matrix values |
-
mat :
Object
-
.identity(n) ⇒
mat
-
.get(a, i, j) ⇒
number
- .set(a, i, j, v)
-
.row(a, m) ⇒
Array.<number>
-
.col(a, n) ⇒
Array.<number>
-
.add(a, b) ⇒
mat
-
.sub(a, b) ⇒
mat
-
.mul(a, b) ⇒
mat
|boolean
-
.scale(a, b) ⇒
mat
-
.trans(a) ⇒
mat
-
.minor(a, i, j) ⇒
mat
|boolean
-
.det(a) ⇒
number
|boolean
-
.nor(a) ⇒
mat
|boolean
-
.adj(a) ⇒
mat
-
.inv(a) ⇒
mat
|boolean
-
.eq(a, b) ⇒
boolean
-
.cpy(a) ⇒
mat
-
.map(a, f) ⇒
mat
-
.str(a, [ms], [ns]) ⇒
string
-
.identity(n) ⇒
mat
mat.identity(n) ⇒ Get an identity matrix of size n
Kind: static method of mat
Returns: mat
- An identity matrix
Param | Type | Description |
---|---|---|
n | number |
The size of the matrix |
number
mat.get(a, i, j) ⇒ Get an entry from a matrix
Kind: static method of mat
Returns: number
- The value at position (i, j) in matrix a
Param | Type | Description |
---|---|---|
a | mat |
Matrix a |
i | number |
The row offset |
j | number |
The column offset |
mat.set(a, i, j, v)
Set an entry of a matrix
Kind: static method of mat
Param | Type | Description |
---|---|---|
a | mat |
Matrix a |
i | number |
The row offset |
j | number |
The column offset |
v | number |
The value to set in matrix a |
Array.<number>
mat.row(a, m) ⇒ Get a row from a matrix as an array
Kind: static method of mat
Returns: Array.<number>
- Row m from matrix a
Param | Type | Description |
---|---|---|
a | mat |
Matrix a |
m | number |
The row offset |
Array.<number>
mat.col(a, n) ⇒ Get a column from a matrix as an array
Kind: static method of mat
Returns: Array.<number>
- Column n from matrix a
Param | Type | Description |
---|---|---|
a | mat |
Matrix a |
n | number |
The column offset |
mat
mat.add(a, b) ⇒ Add matrices
Kind: static method of mat
Returns: mat
- a + b
Param | Type | Description |
---|---|---|
a | mat |
Matrix a |
b | mat |
Matrix b |
mat
mat.sub(a, b) ⇒ Subtract matrices
Kind: static method of mat
Returns: mat
- a - b
Param | Type | Description |
---|---|---|
a | mat |
Matrix a |
b | mat |
Matrix b |
mat
| boolean
mat.mul(a, b) ⇒ Multiply matrices
Kind: static method of mat
Returns: mat
| boolean
- ab or false if the matrices cannot be multiplied
Param | Type | Description |
---|---|---|
a | mat |
Matrix a |
b | mat |
Matrix b |
mat
mat.scale(a, b) ⇒ Scale a matrix
Kind: static method of mat
Returns: mat
- a * b
Param | Type | Description |
---|---|---|
a | mat |
Matrix a |
b | number |
Scalar b |
mat
mat.trans(a) ⇒ Transpose a matrix
Kind: static method of mat
Returns: mat
- A transposed matrix
Param | Type | Description |
---|---|---|
a | mat |
The matrix to transpose |
mat
| boolean
mat.minor(a, i, j) ⇒ Get the minor of a matrix
Kind: static method of mat
Returns: mat
| boolean
- The (i, j) minor of matrix a or false if the matrix is not square
Param | Type | Description |
---|---|---|
a | mat |
Matrix a |
i | number |
The row offset |
j | number |
The column offset |
number
| boolean
mat.det(a) ⇒ Get the determinant of a matrix
Kind: static method of mat
Returns: number
| boolean
- |a| or false if the matrix is not square
Param | Type | Description |
---|---|---|
a | mat |
Matrix a |
mat
| boolean
mat.nor(a) ⇒ Normalise a matrix
Kind: static method of mat
Returns: mat
| boolean
- ^a or false if the matrix is not square
Param | Type | Description |
---|---|---|
a | mat |
The matrix to normalise |
mat
mat.adj(a) ⇒ Get the adjugate of a matrix
Kind: static method of mat
Returns: mat
- The adjugate of a
Param | Type | Description |
---|---|---|
a | mat |
The matrix from which to get the adjugate |
mat
| boolean
mat.inv(a) ⇒ Get the inverse of a matrix
Kind: static method of mat
Returns: mat
| boolean
- a^-1 or false if the matrix has no inverse
Param | Type | Description |
---|---|---|
a | mat |
The matrix to invert |
boolean
mat.eq(a, b) ⇒ Check if two matrices are equal
Kind: static method of mat
Returns: boolean
- True if matrices a and b are identical, false otherwise
Param | Type | Description |
---|---|---|
a | mat |
Matrix a |
b | mat |
Matrix b |
mat
mat.cpy(a) ⇒ Copy a matrix
Kind: static method of mat
Returns: mat
- A copy of matrix a
Param | Type | Description |
---|---|---|
a | mat |
The matrix to copy |
mat
mat.map(a, f) ⇒ Call a function on each entry of a matrix and build a new matrix from the results
Kind: static method of mat
Returns: mat
- Matrix a mapped through f
Param | Type | Description |
---|---|---|
a | mat |
Matrix a |
f | matrixMapCallback |
The function to call on each entry of the matrix |
string
mat.str(a, [ms], [ns]) ⇒ Convert a matrix into a string
Kind: static method of mat
Returns: string
- A string representation of the matrix
Param | Type | Default | Description |
---|---|---|---|
a | mat |
The matrix to convert | |
[ms] | string |
"', '" |
The separator string for columns |
[ns] | string |
"'\n'" |
The separator string for rows |
number
matrixMapCallback ⇒ A function to call on each entry of a matrix
Kind: global typedef
Returns: number
- The mapped entry
Param | Type | Description |
---|---|---|
value | number |
The entry value |
index | number |
The entry index |
entries | Array.<number> |
The array of matrix entries |