Math.js Functions

9 min read

Arithmetic Functions

You can check out the available functions and required parameters in the table below. Please use the links provided to review the original documentation on mathjs.org.

You can check out this diagram for Useful Functions & Syntax used in Machinations:
https://my.machinations.io/d/framework-useful-functions–syntax/a51907aeb67b4ca8b03add11829841c6

FunctionWhat it does
abs(x)Calculate the absolute value of a number abs(-4.2) = 4.2
add(x, y)Add two or more values add(2,3,4) = 9
cbrt(x , allRoots)Calculates the cubic root of a value cbrt(27) = 3
ceil(x)Round a value towards plus infinity ceil(3.2) = 4
cube(x)Compute the cube of a value cube(2) = 8
divide(x, y)Divide two values, x / y divide(2,3) = 0.6666...
dotPow(x, y)Calculates the power of x to y element-wise dotPow(2,3) = 8
exp(x)Calculate the exponent of a value exp(2) = 7.38905..
expm1(x)Calculate the value of subtracting one from the exponential value expm1(2) = 6.38905..
fix(x)Round a value towards zero fix(3.2) = 3
floor(x)Round a value towards minus infinity floor(-3.2) = -4
gcd(a, b, …)Calculate the greatest common divisor for two or more values or arrays gcd(8,12) = 4
hypot(a, b, …)Calculate the hypotenuse of a list with values hypot(3,4) = 5
lcm(a, b, …)Calculate the least common multiple for two or more values or arrays lcm(6,21,5) = 210
log(x, base)Calculate the logarithm of a value log(1024, 2) = 10
log10(x)Calculate the 10-base logarithm of a value log10(10000) = 4
log1p(x)Calculate the logarithm of a value+1 log1p(2.5) = 1.25276...
log2(x)Calculate the 2-base of a value log2(16) = 4
mod(x, y)Calculates the modulus, the remainder of an integer division mod(8, 3) = 2
multiply(x, y)Multiply two or more values multiply(2.5*10) = 25
norm(x, p)Calculate the norm of a number, vector or matrix log(1024, 2) = 10
nthRoot(a, root)Calculate the nth root of a value. The default value of root is 2 if left empty nthRoot(9, 2) = 3
nthRoots(x, root)Calculate the nth roots of a value. Root is optional and has a value of 2 by default. The output is a complex value nthRoots(1) = {re: 1, im: 0}, {re: -1, im: 0}
pow(x, y)Calculates the power of x to y pow(4, 2) = 16
round(x, n)Round a value towards the nearest integer. n represents the number of rounded decimals  round(3.333, 2) = 3.3
sign(x)Compute the sign of a value sign(-10) = -1
sqrt(x)Calculate the square root of a value sqrt(25) = 5
square(x)Compute the square of a value square(5) = 25
subtract(x, y)Subtract two values subtract(5.3, 2) = 3.3
unaryMinus(x)Inverse the sign of a value, apply a unary minus operation unaryMinus(3.3) = -3.3
unaryPlus(x)Unary plus operation unaryPlus(3.5) = 3.5
xgcd(a, b)Calculate the extended greatest common divisor for two values xgcd(8, 12) = [4, -1, 1]

Logical Functions

FunctionWhat it does
and(x, y)Logical and and(2, 5) = true
not(x)Logical not not(2) = false
or(x, y)Logical or or(5, 0) = true
xor(x, y)Logical xor xor(5, 0) = true

Set & Array Functions

FunctionWhat it does
setCartesian(X, Y)Create the cartesian product of two arrays.
setCartesian([1, 2], [3, 4]) = [[1, 3], [1, 4], [2, 3], [2, 4]]
setDifference(X, Y)Create the difference between two arrays: every element of X, that is not the element of Y.
setDifference([1, 2, 3, 4], [3, 4, 5, 6]) = [1, 2]
setDistinct(X)Collect the distinct elements of an array.
setDistinct([1, 1, 1, 2, 2, 3]) = [1, 2, 3]
setIntersect(X, Y)Create the intersection of two arrays.
setIntersect([1, 2, 3, 4], [3, 4, 5, 6]) = [3, 4]
setIsSubset(X, Y)Check whether array X is a subset of array Y.
setIsSubset([3, 4], [3, 4, 5, 6]) = true
setMultiplicity(element, X)Count the number of occurrences of an element in an array.
setMultiplicity(2, [1, 2, 2, 4]) = 2
setPowerset(X)Create the powerset of an array.
setPowerset([1, 2]) = [[], [1], [2], [1, 2]]
setSize(X)Count the number of elements of an array.
setSize([1, 2, 2, 4]) = 4
setSymDifference(X, Y)Create the symmetric difference of two arrays.
setSymDifference([1, 2, 3, 4], [3, 4, 5, 6]) = [1, 2, 5, 6]
setUnion(X, Y)Create the union of two arrays.
setUnion([1, 2, 3, 4], [4, 5]) = [1, 2, 3, 4, 5]

Matrix Functions

FunctionWhat it does
row(value, index)Return a row from a Matrix. The returned value will be an array row([[1, 2], [3, 4]], 1) = [[1, 2]]
column(value, index)Return a column from a Matrix. The returned value will be an array column([[1, 2], [3, 4]], 1) = [[1, 3]]
concat(a, b, c, … )Concatenate two or more matrices.
concat([[1, 2], [5, 6]], [[3, 4], [7, 8]]) = [[1, 2, 3, 4], [5, 6, 7, 8]]
count(x)Count the number of elements of a matrix, array, or string.
count([[1, 2, 3], [4, 5, 6]]) = 6
filter(x, test)Filter the items in an array or one-dimensional matrix.
filter([6, -2, -1, 4, 3], isPositive) = [6, 4, 3]
resize(x, size)Resize a matrix based on a given size
resize([1, 2, 3, 4, 5], [3]) = [1, 2, 3]
subset(value, index)Get or set a subset of a matrix or string subset([[1, 2], [3, 4]], index(1, 0)) = 3
size(x)Calculate the size of a matrix or scalar size([[1, 2, 3], [4, 5, 6]]) = [2, 3]
sort(x)Sort the items in a matrix sort([5, 10, 1]) = [1, 5, 10]
squeeze(x)Remove inner and outer singleton dimensions from a matrix squeeze([[0, 0, 0]]) = [0, 0, 0]

Probability Functions

FunctionWhat it does
combinations(n, k)Compute the number of ways of picking k unordered outcomes from n possibilities.
combinations(7, 5) = 21
factorial(n)Compute the factorial of a value Factorial only supports an integer value as an argument factorial(5) = 120
gamma(n)Compute the gamma function of a value using Lanczos approximation for small values, and an extended Stirling approximation for large values gamma(5) = 24
permutations(n k)Compute the number of ways of obtaining an ordered subset of k elements from a set of n elements.
permutations(5, 3) = 60
pickRandom(array)Random pick one or more values from a one-dimensional array pickRandom([3, 6, 12, 2]) = one random value in the array
random(min, max)Return a random number larger or equal to min and smaller than max using a uniform distribution random(30, 40) = returns a random number between 30 and 39
randomInt(min, max)Return a random integer number larger or equal to min and smaller than max using a uniform distribution randomInt(30, 40) = returns a random integer between 30 and 39

Relational Functions

FunctionWhat it does
compare(x, y)Compare two values compare(2, [1, 2, 3]) = [1, 0, -1]
compareNatural(x, y)Compare two values of any type in a deterministic, natural way compareNatural(6, 1) = 1
equal(x, y)Test whether two values are equal equal(5, 5) = true
larger(x, y)Test whether value x is larger than y larger(2, 5) = false
largerEq(x, y)Test whether value x is larger or equal to y largerEq(5, 5) = true
smaller(x, y)Test whether value x is smaller than y smaller(5, 0) = false
smallerEq(x, y)Test whether value x is smaller or equal to y smallerEq(5, 5) = true
unequal(x, y)Test whether two values are unequal unequal(5, 0) = true

Statistics Functions

FunctionWhat it does
corr(A, B)Compute the correlation coefficient of two lists with values, For matrices, the matrix correlation coefficient is calculated corr([1, 2, 3, 4, 5], [4, 5, 6, 7, 8]) = 1
cumsum(a, b, c, …)Compute the cumulative sum of a matrix or a list with values. cumsum(2, 1, 4, 3) = [2, 3, 7, 10]
mad(a, b, c, …)Compute the median absolute deviation of a matrix or a list with values mad([[1, 2, 3], [4, 5, 6]]) = 1.5
max(a, b, c, …)Compute the maximum value of a matrix or a list with values max(2, 1, 4, 3) = 4
mean(a, b, c, …)Compute the mean value of matrix or a list with values mean(2, 1, 4, 3) = 2.5
median(a, b, c, …)Compute the median of a matrix or a list with values median(5, 2, 7) = 5
min(a, b, c, …)Compute the minimum value of a matrix or a list of values min(2, 1, 4, 3) = 1
mode(a, b, c, …)Computes the mode of a set of numbers or a list with values(numbers or characters) mode(2, 1, 4, 3, 1) = [1]
prod(a, b, c, …)Compute the product of a matrix or a list with values prod(2, 3, 4) = 24
std(a, b, c, …)Compute the standard deviation of a matrix or a list with values std([2, 4, 6, 8]) = 2.5819...
sum(a, b, c, …)Compute the sum of a matrix or a list with values sum([2, 1, 4, 3]) = 10
variance(a, b, c, …)Compute the variance of a matrix or a list with values variance([2, 4, 6, 8]) = 6.666...

Utils Functions

FunctionWhat it does
hasNumericValue(x)Test whether a value is a numeric value hasNumericValue([2.3, ' a ', false]) = [true, false, true]
isInteger(x)Test whether a value is an integer number isInteger(5) = true
isNaN(x)Test whether a value is NaN (not a number) isNaN(NaN) = true
isNegative(x)Test whether a value is negative: smaller than zero isNegative(5) = false
isNumeric(x)Test whether a value is an numeric value isNumeric([2.3, ' a ', false]) = [true, false, true]
isPositive(x)Test whether a value is positive: larger than zero isPositive(-5) = false
isPrime(x)Test whether a value is prime: has no divisors other than itself and one isPrime(23) = true
isZero(x)Test whether a value is zero isZero(0) = true

Trigonometry Functions

FunctionWhat it does
acos(x)Calculate the inverse cosine of a value acos(0.5) = 1.0471...
acot(x)Calculate the inverse cotangent of a value, defined as acot(x) = atan(1/x) acot(0.5) = 1.1071...
asin(x)Calculate the inverse sine of a value asin(0.5) = 0.5235...
atan(x)Calculate the inverse tangent of a value atan(0.5) = 0.4636...
cos(x)Calculate the cosine of a value cos(pi / 4) = 0.7071...
cosh(x)Calculate the hyperbolic cosine of a value, defined as cosh(x) = 1/2 * (exp(x) + exp(-x)) cosh(0.5) = 1.1276...
cot(x)Calculate the cotangent of a value cot(2) = -0.4576...
coth(x)Calculate the hyperbolic cotangent of a value, defined as coth(x) = 1 / tanh(x) coth(2) = 1.03731...
sec(x)Calculate the secant of a value, defined as sec(x) = 1/cos(x) sec(2) = -2.4029...
sin(x)Calculate the sine of a value sin(pi / 4) = 0.7071...
sinh(x)Calculate the hyperbolic sine of a value, defined as sinh(x) = 1/2 * (exp(x) – exp(-x)) sinh(0.5) = 0.52109...
tan(x)Calculate the tangent of a value tan(0.5) = 0.5463...

Bitwise Functions

FunctionsWhat it does
bitAnd(x, y)Bitwise AND two values, bitAnd(6,3) = 2 // and(110, 011) = 010
bitNot(x)Bitwise NOT value, bitNot(5) = -6 // not(1 0101) = 0 1010
bitOr(x, y)Bitwise OR two values, bitOr(6,3) = 7 // or(110, 011) = 111
bitXor(x, y)Bitwise XOR two values, bitXor(6,3) = 5 // xor(110, 011) = 101
leftShift(x, y)Bitwise left logical shift of a value x by y number of bits leftShift(9, 1) = 18
rightArithShift(x, y)Bitwise right arithmetic shift of a value x by y number of bits rightShift(9,1) = 4
rightLogShift(x, y)Bitwise right logical shift of value x by y number of bits rightLogShift(16,1) = 8

Operators

The following table contains useful operators for working with Registers and/or parameters (called custom variables in older guides). You can find the full list in the Operators section at Mathjs.org.

NameSyntaxExampleResult
Grouping(x)2 * (3 + 4)14
Matrix, Index[...][[1,2],[3,4]][[1,2],[3,4]]
Parameter separatorx, ymax(2, 1, 5)5
Addx + y4 + 59
Subtractx - y7 - 34
Multiplyx * y2 * 36
Dividex / y6 / 23
Powerx ^ y2 ^ 38
Factorialy!5!120
Logical andx and ytrue and falsefalse
Logical notnot ynot truefalse
Logical orx or ytrue or falsetrue
Logical xorx xor ytrue xor truefalse
Assignmentx = ya = 55
Conditional expressionx ? y : z15 > 100 ? 1 : -1-1
Equalx == y2 == 4 - 2true
Unequalx != y2 != 3true
Smallerx < y2 < 3true
Largerx > y2 > 3false
Smallereqx <= y4 <= 3false
Largereqx >= y2 + 4 >= 6true