Arithmetic Functions (30)

Basic arithmetic operations and scalar math functions

abs
Compute the absolute value.
Syntax
abs(x)
Parameters
NameTypeDescription
xnumber | BigNumber | bigint | Fraction | Complex | Array | Matrix | Unit
Returns
number | BigNumber | bigint | Fraction | Complex | Array | Matrix | Unit —
Examples
abs(3.5)
abs(-4.2)
See Also
sign
addScalar
Add two scalar values, `x + y`. This function is meant for internal use: it is used by the public function `add` This function does not support collections (Array or Matrix).
Type Signatures
Complex, Complex, BigNumber, BigNumber, bigint, bigint, Fraction, Fraction
Parameters
NameTypeDescription
xnumber | BigNumber | bigint | Fraction | Complex | UnitFirst value to add
ynumber | BigNumber | bigint | Fraction | ComplexSecond value to add
Returns
number | BigNumber | bigint | Fraction | Complex | Unit — Sum of `x` and `y`
cbrt
Compute the cubic root value. If x = y * y * y, then y is the cubic root of x. When `x` is a number or complex number, an optional second argument `allRoots` can be provided to return all three cubic roots. If not provided, the principal root is returned
Syntax
cbrt(x) | cbrt(x | allRoots)
Parameters
NameTypeDescription
xnumber | BigNumber | Complex | Unit
allRootsbooleanOptional, false by default. Only applicable
Returns
number | BigNumber | Complex | Unit —
Examples
cbrt(64)
cube(4)
cbrt(-8)
cbrt(2 + 3i)
cbrt(8i)
ceil
Round a value towards plus infinity. If x is complex, both real and imaginary part are rounded towards plus infinity.
Syntax
ceil(x) | ceil(x | n)
Type Signatures
number, number, Complex, number, Complex, BigNumber, BigNumber, BigNumber, Fraction, number, Fraction, BigNumber
Parameters
NameTypeDescription
xnumber | BigNumber | Fraction | Complex | Unit | Array | MatrixValue to be rounded
valuelessUnitUnitA valueless unit
Returns
number | BigNumber | Fraction | Complex | Unit | Array | Matrix — Rounded value
Examples
ceil(3.2)
ceil(3.8)
ceil(-4.2)
ceil(3.241cm
cm)
See Also
floor fix round
cube
Compute the cube of a value. The cube of x is x * x * x.
Syntax
cube(x)
Parameters
NameTypeDescription
xnumber | BigNumber | bigint | Fraction | Complex | UnitNumber for which to calculate the cube
Returns
number | BigNumber | bigint | Fraction | Complex | Unit — Cube of x
Examples
cube(2)
2^3
2 * 2 * 2
See Also
multiply square pow
divideScalar
Divide two scalar values, `x / y`. This function is meant for internal use: it is used by the public functions `divide` and `inv`. This function does not support collections (Array or Matrix).
Type Signatures
number, number, Complex, Complex, BigNumber, BigNumber, bigint, bigint, Fraction, Fraction
Parameters
NameTypeDescription
xnumber | BigNumber | bigint | Fraction | Complex | UnitNumerator
ynumber | BigNumber | bigint | Fraction | ComplexDenominator
Returns
number | BigNumber | bigint | Fraction | Complex | Unit — Quotient, `x / y`
exp
Calculate the exponent of a value.
Syntax
exp(x)
Parameters
NameTypeDescription
xnumber | BigNumber | ComplexA number to exponentiate
Returns
number | BigNumber | Complex — Exponential of `x`
Examples
exp(1.3)
e ^ 1.3
log(exp(1.3))
x = 2.4
(exp(i*x) == cos(x) + i*sin(x)) # Euler\'s formula
See Also
expm expm1 pow log
expm
Compute the matrix exponential, expm(A) = e^A.
Syntax
exp(x)
Parameters
NameTypeDescription
xMatrixA square Matrix
Returns
Matrix — The exponential of x
Examples
expm([[0
2
See Also
exp
expm1
Calculate the value of subtracting 1 from the exponential value.
Syntax
expm1(x)
Parameters
NameTypeDescription
xnumber | BigNumber | ComplexThe number to exponentiate
Returns
number | BigNumber | Complex — Exponential of `x`, minus one
Examples
expm1(2)
pow(e
2) - 1
log(expm1(2) + 1)
See Also
exp pow log
fix
Round a value towards zero. If x is complex, both real and imaginary part are rounded towards zero.
Syntax
fix(x) | fix(x | n)
Type Signatures
number, number, Complex, number, Complex, BigNumber, BigNumber, number | BigNumber, Fraction, number | BigNumber
Parameters
NameTypeDescription
xnumber | BigNumber | Fraction | Complex | Unit | Array | MatrixValue to be rounded
valuelessUnitUnitA valueless unit
Returns
number | BigNumber | Fraction | Complex | Unit | Array | Matrix — Rounded value
Examples
fix(3.2)
fix(3.8)
fix(-4.2)
fix(-4.8)
fix(3.241cm
See Also
ceil floor round
floor
Round a value towards minus infinity.If x is complex, both real and imaginary part are rounded towards minus infinity.
Syntax
floor(x) | floor(x | n)
Type Signatures
number, number, Complex, number, Complex, BigNumber, BigNumber, BigNumber, Fraction, number, Fraction, BigNumber
Parameters
NameTypeDescription
xnumber | BigNumber | Fraction | Complex | Unit | Array | MatrixValue to be rounded
valuelessUnitUnitA valueless unit
Returns
number | BigNumber | Fraction | Complex | Unit | Array | Matrix — Rounded value
Examples
floor(3.2)
floor(3.8)
floor(-4.2)
floor(3.241cm
cm)
See Also
ceil fix round
gcd
Compute the greatest common divisor.
Syntax
gcd(a | b) | gcd(a
Parameters
NameTypeDescription
args... number | BigNumber | Fraction | Array | MatrixTwo or more integer numbers
Returns
number | BigNumber | Fraction | Array | Matrix — The greatest common divisor
Examples
gcd(8
12)
gcd(-4
6)
gcd(25
See Also
lcm xgcd
hypot
Calculate the hypotenuse of a list with values.
Syntax
hypot(a | b | c
Parameters
NameTypeDescription
args... number | BigNumber | Array | MatrixA list with numeric values or an Array or Matrix.
Returns
number | BigNumber — Returns the hypothenusa of the input values.
Examples
hypot(3
4)
sqrt(3^2 + 4^2)
hypot(-2)
hypot([3
See Also
abs norm
invmod
Calculate the (modular) multiplicative inverse of a modulo b. Solution to the equation ax ≣ 1 (mod b)
Syntax
invmod(a | b)
Parameters
NameTypeDescription
anumber | BigNumberAn integer number
bnumber | BigNumberAn integer number
Returns
number | BigNumber — Returns an integer number
Examples
invmod(8
12)
invmod(7
13)
invmod(15151
See Also
gcd xgcd
lcm
Compute the least common multiple.
Syntax
lcm(x | y)
Parameters
NameTypeDescription
args... number | BigNumber | Array | MatrixTwo or more integer numbers
Returns
number | BigNumber | Array | Matrix — The least common multiple
Examples
lcm(4
6)
lcm(6
21)
lcm(6
See Also
gcd
log
Compute the logarithm of a value. If no base is provided, the natural logarithm of x is calculated. If base if provided, the logarithm is calculated for the specified base. log(x, base) is defined as log(x) / log(base).
Syntax
log(x) | log(x | base)
Parameters
NameTypeDescription
xnumber | BigNumber | Fraction | Complex
Returns
number | BigNumber | Fraction | Complex —
Examples
log(3.5)
a = log(2.4)
exp(a)
10 ^ 4
log(10000
See Also
exp log1p log2 log10
log10
Compute the 10-base logarithm of a value.
Syntax
log10(x)
Parameters
NameTypeDescription
xnumber | BigNumber | Complex | Array | Matrix
Returns
number | BigNumber | Complex | Array | Matrix —
Examples
log10(0.00001)
log10(10000)
10 ^ 4
log(10000) / log(10)
log(10000
See Also
exp log
log1p
Calculate the logarithm of a `value+1`
Syntax
log1p(x) | log1p(x | base)
Parameters
NameTypeDescription
xnumber | BigNumber | Complex | Array | Matrix
Returns
number | BigNumber | Complex | Array | Matrix —
Examples
log1p(2.5)
exp(log1p(1.4))
pow(10
4)
log1p(9999
See Also
exp log log2 log10
log2
Calculate the 2-base of a value. This is the same as calculating `log(x, 2)`.
Syntax
log2(x)
Parameters
NameTypeDescription
xnumber | BigNumber | Complex | Array | Matrix
Returns
number | BigNumber | Complex | Array | Matrix —
Examples
log2(0.03125)
log2(16)
log2(16) / log2(2)
pow(2
4)
See Also
exp log1p log log10
multiplyScalar
Multiply two scalar values, `x * y`. This function is meant for internal use: it is used by the public function `multiply` This function does not support collections (Array or Matrix).
Type Signatures
Complex, Complex, BigNumber, BigNumber, bigint, bigint, Fraction, Fraction
Parameters
NameTypeDescription
xnumber | BigNumber | bigint | Fraction | Complex | UnitFirst value to multiply
ynumber | BigNumber | bigint | Fraction | ComplexSecond value to multiply
Returns
number | BigNumber | bigint | Fraction | Complex | Unit — Multiplication of `x` and `y`
norm
Calculate the norm of a number, vector or matrix.
Syntax
norm(x) | norm(x | p)
Type Signatures
Array, number | BigNumber | string, Matrix, number | BigNumber | string
Parameters
NameTypeDescription
xnumber | BigNumber | Complex | Array | Matrix
Returns
number | BigNumber — the p-norm
Examples
abs(-3.5)
norm(-3.5)
norm(3 - 4i)
norm([1
2
See Also
abs hypot
nthRoot
Calculate the nth root of a value.
Syntax
nthRoot(a) | nthRoot(a | root)
Parameters
NameTypeDescription
anumber | BigNumber | Array | Matrix | Complex
Returns
number | Complex | Array | Matrix — Returns the nth root of `a`
Examples
4 ^ 3
nthRoot(64
3)
nthRoot(9
2)
See Also
nthRoots pow sqrt
nthRoots
Syntax
nthRoots(A) | nthRoots(A | root)
Parameters
NameTypeDescription
valnumber
Returns
Complex — val, i*val, -val or -i*val for index 0, 1, 2, 3
Examples
nthRoots(1)
nthRoots(1
3)
See Also
sqrt pow nthRoot
round
round a value towards the nearest integer.If x is complex, both real and imaginary part are rounded towards the nearest integer. When n is specified, the value is rounded to n decimals.
Syntax
round(x) | round(x | n)
Type Signatures
number, number, number, BigNumber, Complex, number, Complex, BigNumber, BigNumber, BigNumber, Fraction, number
Parameters
NameTypeDescription
xnumber | BigNumber | Fraction | Complex | Unit | Array | MatrixValue to be rounded
valuelessUnitUnitA valueless unit
Returns
number | BigNumber | Fraction | Complex | Unit | Array | Matrix — Rounded value
Examples
round(3.2)
round(3.8)
round(-4.2)
round(-4.8)
round(pi
See Also
ceil floor fix
sign
Compute the sign of a value. The sign of a value x is 1 when x>0, -1 when x<0, and 0 when x=0.
Syntax
sign(x)
Parameters
NameTypeDescription
xnumber | BigNumber | bigint | Fraction | Complex | Array | Matrix | Unit
Returns
number | BigNumber | bigint | Fraction | Complex | Array | Matrix | Unit —
Examples
sign(3.5)
sign(-4.2)
sign(0)
See Also
abs
sqrt
Compute the square root value. If x = y * y, then y is the square root of x.
Syntax
sqrt(x)
Parameters
NameTypeDescription
xnumber | BigNumber | Complex | Unit
Returns
number | BigNumber | Complex | Unit —
Examples
sqrt(25)
5 * 5
sqrt(-1)
sqrtm
Calculate the principal square root of a square matrix. The principal square root matrix `X` of another matrix `A` is such that `X * X = A`.
Syntax
sqrtm(x)
Type Signatures
Array | Matrix
Parameters
NameTypeDescription
AArray | MatrixThe square matrix `A`
Returns
Array | Matrix — The principal square root of matrix `A`
Examples
sqrtm([[33
24
See Also
sqrt abs square multiply
square
Compute the square of a value. The square of x is x * x.
Syntax
square(x)
Parameters
NameTypeDescription
xnumber | BigNumber | bigint | Fraction | Complex | Unit
Returns
number | BigNumber | bigint | Fraction | Complex | Unit —
Examples
square(3)
sqrt(9)
3^2
3 * 3
See Also
multiply pow sqrt cube
subtractScalar
Subtract two scalar values, `x - y`. This function is meant for internal use: it is used by the public function `subtract` This function does not support collections (Array or Matrix).
Type Signatures
Complex, Complex, BigNumber, BigNumber, bigint, bigint, Fraction, Fraction
Parameters
NameTypeDescription
xnumber | BigNumber | bigint | Fraction | Complex | UnitFirst value
ynumber | BigNumber | bigint | Fraction | ComplexSecond value to be subtracted from `x`
Returns
number | BigNumber | bigint | Fraction | Complex | Unit — Difference of `x` and `y`
xgcd
Calculate the extended greatest common divisor for two values. The result is an array [d, x, y] with 3 entries, where d is the greatest common divisor, and d = x * a + y * b.
Syntax
xgcd(a | b)
Type Signatures
number, number
Parameters
NameTypeDescription
anumber | BigNumberAn integer number
bnumber | BigNumberAn integer number
Returns
Array — Returns an array containing 3 integers `[div, m, n]`
Examples
xgcd(8
12)
gcd(8
12)
xgcd(36163
See Also
gcd lcm