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
Examples
id(8 kg)
= 8 kg [Mass]
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
Examples
abs(-22.2 m)
= 22.2 m [Length]
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
Examples
sqrt(4 are) -> m
= 20 m [Length]
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
Examples
cbrt(8 L) -> cm
= 20.0 cm [Length]
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
Examples
sqr(7)
= 49
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
Examples
round(5.5)
= 6
round(-5.5)
= -6
round_in
(Rounding)
Round to the nearest multiple of base
.
fn round_in<D: Dim>(base: D, value: D) -> D
Examples
Round in meters.
round_in(m, 5.3 m)
= 5 m [Length]
Round in centimeters.
round_in(cm, 5.3 m)
= 530 cm [Length]
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
Examples
floor(5.5)
= 5
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
Examples
Floor in meters.
floor_in(m, 5.7 m)
= 5 m [Length]
Floor in centimeters.
floor_in(cm, 5.7 m)
= 570 cm [Length]
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
Examples
ceil(5.5)
= 6
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
Examples
Ceil in meters.
ceil_in(m, 5.3 m)
= 6 m [Length]
Ceil in centimeters.
ceil_in(cm, 5.3 m)
= 530 cm [Length]
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
Examples
trunc(5.5)
= 5
trunc(-5.5)
= -5
trunc_in
(Truncation)
Truncates to an integer multiple of base
(towards zero).
fn trunc_in<D: Dim>(base: D, value: D) -> D
Examples
Truncate in meters.
trunc_in(m, 5.7 m)
= 5 m [Length]
Truncate in centimeters.
trunc_in(cm, 5.7 m)
= 570 cm [Length]
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
Examples
fract(0.0)
= 0
fract(5.5)
= 0.5
fract(-5.5)
= -0.5
mod
(Modulo)
Calculates the least nonnegative remainder of \( a (\mod b) \). More information here.
fn mod<T: Dim>(a: T, b: T) -> T
Examples
mod(27, 5)
= 2
Transcendental functions
Defined in: math::transcendental
exp
(Exponential function)
The exponential function, \( e^x \). More information here.
fn exp(x: Scalar) -> Scalar
Examples
exp(4)
= 54.5982
ln
(Natural logarithm)
The natural logarithm with base \( e \). More information here.
fn ln(x: Scalar) -> Scalar
Examples
ln(20)
= 2.99573
log
(Natural logarithm)
The natural logarithm with base \( e \). More information here.
fn log(x: Scalar) -> Scalar
Examples
log(20)
= 2.99573
log10
(Common logarithm)
The common logarithm with base \( 10 \). More information here.
fn log10(x: Scalar) -> Scalar
Examples
log10(100)
= 2
log2
(Binary logarithm)
The binary logarithm with base \( 2 \). More information here.
fn log2(x: Scalar) -> Scalar
Examples
log2(256)
= 8
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
Examples
maximum([30 cm, 2 m])
= 2 m [Length]
minimum
(Minimum)
Get the smallest element of a list.
fn minimum<D: Dim>(xs: List<D>) -> D
Examples
minimum([30 cm, 2 m])
= 30 cm [Length]
mean
(Arithmetic mean)
Calculate the arithmetic mean of a list of quantities. More information here.
fn mean<D: Dim>(xs: List<D>) -> D
Examples
mean([1 m, 2 m, 300 cm])
= 2 m [Length]
variance
(Variance)
Calculate the population variance of a list of quantities. More information here.
fn variance<D: Dim>(xs: List<D>) -> D^2
Examples
variance([1 m, 2 m, 300 cm])
= 0.666667 m² [Area]
stdev
(Standard deviation)
Calculate the population standard deviation of a list of quantities. More information here.
fn stdev<D: Dim>(xs: List<D>) -> D
Examples
stdev([1 m, 2 m, 300 cm])
= 0.816497 m [Length]
median
(Median)
Calculate the median of a list of quantities. More information here.
fn median<D: Dim>(xs: List<D>) -> D
Examples
median([1 m, 2 m, 400 cm])
= 2 m [Length]
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
Examples
factorial(4)
= 24
4!
= 24
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
Examples
falling_factorial(4, 2)
= 12
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
Examples
binom(5, 2)
= 10
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
Examples
gcd(60, 42)
= 6
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
Examples
lcm(14, 4)
= 28
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) -> Y / X
Examples
Compute the derivative of \( f(x) = x² -x -1 \) at \( x=1 \).
use numerics::diff
fn polynomial(x) = x² - x - 1
diff(polynomial, 1)
= 1.0
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)
velocity(2 s)
= 19.6133 m/s [Velocity]
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
Examples
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
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
Examples
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
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
Examples
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
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
Examples
72 € |> increase_by(15%)
= 82.8 € [Money]
decrease_by
Decrease a quantity by the given percentage. More information here.
fn decrease_by<D: Dim>(percentage: Scalar, quantity: D) -> D
Examples
210 cm |> decrease_by(10%)
= 189 cm [Length]
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
Examples
percentage_change(35 kg, 42 kg)
= 20 %
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
Examples
hypot2(3 m, 4 m)
= 5 m [Length]
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
Examples
hypot3(8, 9, 12)
= 17
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>
Examples
Solve the equation \( 2x² -x -1 = 0 \)
use extra::algebra
quadratic_equation(2, -1, -1)
= [1, -0.5] [List]
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