Skip to content

Mathematical functions

Basics · Transcendental functions · Trigonometry · Statistics · Combinatorics · Random sampling, distributions · Number theory · Numerical methods · Percentage calculations · Geometry · Algebra · Trigonometry (extra)

Basics

Defined in: core::functions

id (Identity function)

Return the input value.

fn id<A>(x: A) -> A

Example

id(8 kg)

    = 8 kg    [Mass]
Run this example

abs (Absolute value)

Return the absolute value \( |x| \) of the input. This works for quantities, too: abs(-5 m) = 5 m. More information here.

fn abs<T: Dim>(x: T) -> T

Example

abs(-22.2 m)

    = 22.2 m    [Length]
Run this example

sqrt (Square root)

Return the square root \( \sqrt{x} \) of the input: sqrt(121 m^2) = 11 m. More information here.

fn sqrt<D: Dim>(x: D^2) -> D

Example

sqrt(4 are) -> m

    = 20 m    [Length]
Run this example

cbrt (Cube root)

Return the cube root \( \sqrt[3]{x} \) of the input: cbrt(8 m^3) = 2 m. More information here.

fn cbrt<D: Dim>(x: D^3) -> D

Example

cbrt(8 L) -> cm

    = 20.0 cm    [Length]
Run this example

sqr (Square function)

Return the square of the input, \( x^2 \): sqr(5 m) = 25 m^2.

fn sqr<D: Dim>(x: D) -> D^2

Example

sqr(7)

    = 49
Run this example

round (Rounding)

Round to the nearest integer. If the value is half-way between two integers, round away from \( 0 \). See also: round_in. More information here.

fn round(x: Scalar) -> Scalar

Example

round(5.5)

    = 6
Run this example

Example

round(-5.5)

    = -6
Run this example

round_in (Rounding)

Round to the nearest multiple of base.

fn round_in<D: Dim>(base: D, value: D) -> D

Round in meters.

round_in(m, 5.3 m)

    = 5 m    [Length]
Run this example

Round in centimeters.

round_in(cm, 5.3 m)

    = 530 cm    [Length]
Run this example

floor (Floor function)

Returns the largest integer less than or equal to \( x \). See also: floor_in. More information here.

fn floor(x: Scalar) -> Scalar

Example

floor(5.5)

    = 5
Run this example

floor_in (Floor function)

Returns the largest integer multiple of base less than or equal to value.

fn floor_in<D: Dim>(base: D, value: D) -> D

Floor in meters.

floor_in(m, 5.7 m)

    = 5 m    [Length]
Run this example

Floor in centimeters.

floor_in(cm, 5.7 m)

    = 570 cm    [Length]
Run this example

ceil (Ceil function)

Returns the smallest integer greater than or equal to \( x \). See also: ceil_in. More information here.

fn ceil(x: Scalar) -> Scalar

Example

ceil(5.5)

    = 6
Run this example

ceil_in (Ceil function)

Returns the smallest integer multiple of base greater than or equal to value.

fn ceil_in<D: Dim>(base: D, value: D) -> D

Ceil in meters.

ceil_in(m, 5.3 m)

    = 6 m    [Length]
Run this example

Ceil in centimeters.

ceil_in(cm, 5.3 m)

    = 530 cm    [Length]
Run this example

trunc (Truncation)

Returns the integer part of \( x \). Non-integer numbers are always truncated towards zero. See also: trunc_in. More information here.

fn trunc(x: Scalar) -> Scalar

Example

trunc(5.5)

    = 5
Run this example

Example

trunc(-5.5)

    = -5
Run this example

trunc_in (Truncation)

Truncates to an integer multiple of base (towards zero).

fn trunc_in<D: Dim>(base: D, value: D) -> D

Truncate in meters.

trunc_in(m, 5.7 m)

    = 5 m    [Length]
Run this example

Truncate in centimeters.

trunc_in(cm, 5.7 m)

    = 570 cm    [Length]
Run this example

fract (Fractional part)

Returns the fractional part of \( x \), i.e. the remainder when divided by 1. If \( x < 0 \), then so will be fract(x). Note that due to floating point error, a number’s fractional part can be slightly “off”; for instance, fract(1.2) == 0.1999...996 != 0.2. More information here.

fn fract(x: Scalar) -> Scalar

Example

fract(0.0)

    = 0
Run this example

Example

fract(5.5)

    = 0.5
Run this example

Example

fract(-5.5)

    = -0.5
Run this example

mod (Modulo)

Calculates the least nonnegative remainder of \( a (\mod b) \). More information here.

fn mod<T: Dim>(a: T, b: T) -> T

Example

mod(27, 5)

    = 2
Run this example

Transcendental functions

Defined in: math::transcendental

exp (Exponential function)

The exponential function, \( e^x \). More information here.

fn exp(x: Scalar) -> Scalar

Example

exp(4)

    = 54.5982
Run this example

ln (Natural logarithm)

The natural logarithm with base \( e \). More information here.

fn ln(x: Scalar) -> Scalar

Example

ln(20)

    = 2.99573
Run this example

log (Natural logarithm)

The natural logarithm with base \( e \). More information here.

fn log(x: Scalar) -> Scalar

Example

log(20)

    = 2.99573
Run this example

log10 (Common logarithm)

The common logarithm with base \( 10 \). More information here.

fn log10(x: Scalar) -> Scalar

Example

log10(100)

    = 2
Run this example

log2 (Binary logarithm)

The binary logarithm with base \( 2 \). More information here.

fn log2(x: Scalar) -> Scalar

Example

log2(256)

    = 8
Run this example

gamma (Gamma function)

The gamma function, \( \Gamma(x) \). More information here.

fn gamma(x: Scalar) -> Scalar

Trigonometry

Defined in: math::trigonometry

sin (Sine)

More information here.

fn sin(x: Scalar) -> Scalar

cos (Cosine)

More information here.

fn cos(x: Scalar) -> Scalar

tan (Tangent)

More information here.

fn tan(x: Scalar) -> Scalar

asin (Arc sine)

More information here.

fn asin(x: Scalar) -> Scalar

acos (Arc cosine)

More information here.

fn acos(x: Scalar) -> Scalar

atan (Arc tangent)

More information here.

fn atan(x: Scalar) -> Scalar

atan2

More information here.

fn atan2<T: Dim>(y: T, x: T) -> Scalar

sinh (Hyperbolic sine)

More information here.

fn sinh(x: Scalar) -> Scalar

cosh (Hyperbolic cosine)

More information here.

fn cosh(x: Scalar) -> Scalar

tanh (Hyperbolic tangent)

More information here.

fn tanh(x: Scalar) -> Scalar

asinh (Area hyperbolic sine)

More information here.

fn asinh(x: Scalar) -> Scalar

acosh (Area hyperbolic cosine)

More information here.

fn acosh(x: Scalar) -> Scalar

atanh (Area hyperbolic tangent )

More information here.

fn atanh(x: Scalar) -> Scalar

Statistics

Defined in: math::statistics

maximum (Maxmimum)

Get the largest element of a list.

fn maximum<D: Dim>(xs: List<D>) -> D

Example

maximum([30 cm, 2 m])

    = 2 m    [Length]
Run this example

minimum (Minimum)

Get the smallest element of a list.

fn minimum<D: Dim>(xs: List<D>) -> D

Example

minimum([30 cm, 2 m])

    = 30 cm    [Length]
Run this example

mean (Arithmetic mean)

Calculate the arithmetic mean of a list of quantities. More information here.

fn mean<D: Dim>(xs: List<D>) -> D

Example

mean([1 m, 2 m, 300 cm])

    = 2 m    [Length]
Run this example

variance (Variance)

Calculate the population variance of a list of quantities. More information here.

fn variance<D: Dim>(xs: List<D>) -> D^2

Example

variance([1 m, 2 m, 300 cm])

    = 0.666667 m²    [Area]
Run this example

stdev (Standard deviation)

Calculate the population standard deviation of a list of quantities. More information here.

fn stdev<D: Dim>(xs: List<D>) -> D

Example

stdev([1 m, 2 m, 300 cm])

    = 0.816497 m    [Length]
Run this example

median (Median)

Calculate the median of a list of quantities. More information here.

fn median<D: Dim>(xs: List<D>) -> D

Example

median([1 m, 2 m, 400 cm])

    = 2 m    [Length]
Run this example

Combinatorics

Defined in: math::combinatorics

factorial (Factorial)

The product of the integers 1 through n. Numbat also supports calling this via the postfix operator n!. More information here.

fn factorial(n: Scalar) -> Scalar

Example

factorial(4)

    = 24
Run this example

Example

4!

    = 24
Run this example

falling_factorial (Falling factorial)

Equal to \( n⋅(n-1)⋅…⋅(n-k+2)⋅(n-k+1) \) (k terms total). If n is an integer, this is the number of k-element permutations from a set of size n. k must always be an integer. More information here.

fn falling_factorial(n: Scalar, k: Scalar) -> Scalar

Example

falling_factorial(4, 2)

    = 12
Run this example

binom (Binomial coefficient)

Equal to falling_factorial(n, k)/k!, this is the coefficient of \( x^k \) in the series expansion of \( (1+x)^n \) (see “binomial series”). If n is an integer, then this this is the number of k-element subsets of a set of size n, often read "n choose k". k must always be an integer. More information here.

fn binom(n: Scalar, k: Scalar) -> Scalar

Example

binom(5, 2)

    = 10
Run this example

fibonacci (Fibonacci numbers)

The nth Fibonacci number, where n is a nonnegative integer. The Fibonacci sequence is given by \( F_0=0 \), \( F_1=1 \), and \( F_n=F_{n-1}+F_{n-2} \) for \( n≥2 \). The first several elements, starting with \( n=0 \), are \( 0, 1, 1, 2, 3, 5, 8, 13 \). More information here.

fn fibonacci(n: Scalar) -> Scalar

Example

fibonacci(5)

    = 5
Run this example

lucas (Lucas numbers)

The nth Lucas number, where n is a nonnegative integer. The Lucas sequence is given by \( L_0=2 \), \( L_1=1 \), and \( L_n=L_{n-1}+L_{n-2} \) for \( n≥2 \). The first several elements, starting with \( n=0 \), are \( 2, 1, 3, 4, 7, 11, 18, 29 \). More information here.

fn lucas(n: Scalar) -> Scalar

Example

lucas(5)

    = 11
Run this example

catalan (Catalan numbers)

The nth Catalan number, where n is a nonnegative integer. The Catalan sequence is given by \( C_n=\frac{1}{n+1}\binom{2n}{n}=\binom{2n}{n}-\binom{2n}{n+1} \). The first several elements, starting with \( n=0 \), are \( 1, 1, 2, 5, 14, 42, 132, 429 \). More information here.

fn catalan(n: Scalar) -> Scalar

Example

catalan(5)

    = 42
Run this example

Random sampling, distributions

Defined in: core::random, math::distributions

random (Standard uniform distribution sampling)

Uniformly samples the interval \( [0,1) \).

fn random() -> Scalar

rand_uniform (Continuous uniform distribution sampling)

Uniformly samples the interval \( [a,b) \) if \( a \le b \) or \( [b,a) \) if \( b<a \) using inversion sampling. More information here.

fn rand_uniform<T: Dim>(a: T, b: T) -> T

rand_int (Discrete uniform distribution sampling)

Uniformly samples integers from the interval \( [a, b] \). More information here.

fn rand_int(a: Scalar, b: Scalar) -> Scalar

rand_bernoulli (Bernoulli distribution sampling)

Samples a Bernoulli random variable. That is, \( 1 \) with probability \( p \) and \( 0 \) with probability \( 1-p \). The parameter \( p \) must be a probability (\( 0 \le p \le 1 \)). More information here.

fn rand_bernoulli(p: Scalar) -> Scalar

rand_binom (Binomial distribution sampling)

Samples a binomial distribution by doing \( n \) Bernoulli trials with probability \( p \). The parameter \( n \) must be a positive integer, the parameter \( p \) must be a probability (\( 0 \le p \le 1 \)). More information here.

fn rand_binom(n: Scalar, p: Scalar) -> Scalar

rand_norm (Normal distribution sampling)

Samples a normal distribution with mean \( \mu \) and standard deviation \( \sigma \) using the Box-Muller transform. More information here.

fn rand_norm<T: Dim>(μ: T, σ: T) -> T

rand_geom (Geometric distribution sampling)

Samples a geometric distribution (the distribution of the number of Bernoulli trials with probability \( p \) needed to get one success) by inversion sampling. The parameter \( p \) must be a probability (\( 0 \le p \le 1 \)). More information here.

fn rand_geom(p: Scalar) -> Scalar

rand_poisson (Poisson distribution sampling)

Sampling a poisson distribution with rate \( \lambda \), that is, the distribution of the number of events occurring in a fixed interval if these events occur with mean rate \( \lambda \). The rate parameter \( \lambda \) must be non-negative. More information here.

fn rand_poisson(λ: Scalar) -> Scalar

rand_expon (Exponential distribution sampling)

Sampling an exponential distribution (the distribution of the distance between events in a Poisson process with rate \( \lambda \)) using inversion sampling. The rate parameter \( \lambda \) must be positive. More information here.

fn rand_expon<T: Dim>(λ: T) -> 1 / T

rand_lognorm (Log-normal distribution sampling)

Sampling a log-normal distribution, that is, a distribution whose logarithm is a normal distribution with mean \( \mu \) and standard deviation \( \sigma \). More information here.

fn rand_lognorm(μ: Scalar, σ: Scalar) -> Scalar

rand_pareto (Pareto distribution sampling)

Sampling a Pareto distribution with minimum value min and shape parameter \( \alpha \) using inversion sampling. Both parameters must be positive. More information here.

fn rand_pareto<T: Dim>(α: Scalar, min: T) -> T

Number theory

Defined in: math::number_theory

gcd (Greatest common divisor)

The largest positive integer that divides each of the integers \( a \) and \( b \). More information here.

fn gcd(a: Scalar, b: Scalar) -> Scalar

Example

gcd(60, 42)

    = 6
Run this example

lcm (Least common multiple)

The smallest positive integer that is divisible by both \( a \) and \( b \). More information here.

fn lcm(a: Scalar, b: Scalar) -> Scalar

Example

lcm(14, 4)

    = 28
Run this example

Numerical methods

Defined in: numerics::diff, numerics::solve, numerics::fixed_point

diff (Numerical differentiation)

Compute the numerical derivative of the function \( f \) at point \( x \) using the central difference method. More information here.

fn diff<X: Dim, Y: Dim>(f: Fn[(X) -> Y], x: X, Δx: X) -> Y / X

Compute the derivative of \( f(x) = x² -x -1 \) at \( x=1 \).

use numerics::diff
fn polynomial(x) = x² - x - 1
diff(polynomial, 1, 1e-10)

    = 1.0
Run this example

Compute the free fall velocity after \( t=2 s \).

use numerics::diff
fn distance(t) = 0.5 g0 t²
fn velocity(t) = diff(distance, t, 1e-10 s)
velocity(2 s)

    = 19.6133 m/s    [Velocity]
Run this example

root_bisect (Bisection method)

Find the root of the function \( f \) in the interval \( [x_1, x_2] \) using the bisection method. The function \( f \) must be continuous and \( f(x_1) \cdot f(x_2) < 0 \). More information here.

fn root_bisect<X: Dim, Y: Dim>(f: Fn[(X) -> Y], x1: X, x2: X, x_tol: X, y_tol: Y) -> X

Find the root of \( f(x) = x² +x -2 \) in the interval \( [0, 100] \).

use numerics::solve
fn f(x) = x² +x -2
root_bisect(f, 0, 100, 0.01, 0.01)

    = 1.00098
Run this example

root_newton (Newton's method)

Find the root of the function \( f(x) \) and its derivative \( f'(x) \) using Newton's method. More information here.

fn root_newton<X: Dim, Y: Dim>(f: Fn[(X) -> Y], f_prime: Fn[(X) -> Y / X], x0: X, y_tol: Y) -> X

Find a root of \( f(x) = x² -3x +2 \) using Newton's method.

use numerics::solve
fn f(x) = x² -3x +2
fn f_prime(x) = 2x -3
root_newton(f, f_prime, 0 , 0.01)

    = 0.996078
Run this example

fixed_point (Fixed-point iteration)

Compute the approximate fixed point of a function \( f: X \rightarrow X \) starting from \( x_0 \), until \( |f(x) - x| < ε \). More information here.

fn fixed_point<X: Dim>(f: Fn[(X) -> X], x0: X, ε: X) -> X

Compute the fixed poin of \( f(x) = x/2 -1 \).

use numerics::fixed_point
fn function(x) = x/2 - 1
fixed_point(function, 0, 0.01)

    = -1.99219
Run this example

Percentage calculations

Defined in: math::percentage_calculations

increase_by

Increase a quantity by the given percentage. More information here.

fn increase_by<D: Dim>(percentage: Scalar, quantity: D) -> D

Example

72  |> increase_by(15%)

    = 82.8     [Money]
Run this example

decrease_by

Decrease a quantity by the given percentage. More information here.

fn decrease_by<D: Dim>(percentage: Scalar, quantity: D) -> D

Example

210 cm |> decrease_by(10%)

    = 189 cm    [Length]
Run this example

percentage_change

By how many percent has a given quantity increased or decreased?. More information here.

fn percentage_change<D: Dim>(old: D, new: D) -> Scalar

Example

percentage_change(35 kg, 42 kg)

    = 20 %
Run this example

Geometry

Defined in: math::geometry

hypot2

The length of the hypotenuse of a right-angled triangle \( \sqrt{x^2+y^2} \).

fn hypot2<T: Dim>(x: T, y: T) -> T

Example

hypot2(3 m, 4 m)

    = 5 m    [Length]
Run this example

hypot3

The Euclidean norm of a 3D vector \( \sqrt{x^2+y^2+z^2} \).

fn hypot3<T: Dim>(x: T, y: T, z: T) -> T

Example

hypot3(8, 9, 12)

    = 17
Run this example

circle_area

The area of a circle, \( \pi r^2 \).

fn circle_area<L: Dim>(radius: L) -> L^2

circle_circumference

The circumference of a circle, \( 2\pi r \).

fn circle_circumference<L: Dim>(radius: L) -> L

sphere_area

The surface area of a sphere, \( 4\pi r^2 \).

fn sphere_area<L: Dim>(radius: L) -> L^2

sphere_volume

The volume of a sphere, \( \frac{4}{3}\pi r^3 \).

fn sphere_volume<L: Dim>(radius: L) -> L^3

Algebra

Defined in: extra::algebra

quadratic_equation (Solve quadratic equations)

Returns the solutions of the equation a x² + b x + c = 0. More information here.

fn quadratic_equation<A: Dim, B: Dim>(a: A, b: B, c: B^2 / A) -> List<B / A>

Solve the equation \( 2x² -x -1 = 0 \)

use extra::algebra
quadratic_equation(2, -1, -1)

    = [1, -0.5]    [List<Scalar>]
Run this example

cubic_equation (Solve cubic equations)

Returns the solutions of the equation a x³ + b x² + c x + e = 0. More information here.

fn cubic_equation(a: Scalar, b: Scalar, c: Scalar, e: Scalar) -> List<Scalar>

Solve the equation \( x³ - 6x² + 11x - 6 = 0 \)

use extra::algebra
cubic_equation(1, -6, 11, -6)

    = [1, 2, 3]    [List<Scalar>]
Run this example

Trigonometry (extra)

Defined in: math::trigonometry_extra

cot

fn cot(x: Scalar) -> Scalar

acot

fn acot(x: Scalar) -> Scalar

coth

fn coth(x: Scalar) -> Scalar

acoth

fn acoth(x: Scalar) -> Scalar

secant

fn secant(x: Scalar) -> Scalar

arcsecant

fn arcsecant(x: Scalar) -> Scalar

cosecant

fn cosecant(x: Scalar) -> Scalar

csc

fn csc(x: Scalar) -> Scalar

acsc

fn acsc(x: Scalar) -> Scalar

sech

fn sech(x: Scalar) -> Scalar

asech

fn asech(x: Scalar) -> Scalar

csch

fn csch(x: Scalar) -> Scalar

acsch

fn acsch(x: Scalar) -> Scalar