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.
abs (Absolute value)
Return the absolute value \( |x| \) of the input. This works for quantities, too: abs(-5 m) = 5 m.
More information here.
sqrt (Square root)
Return the square root \( \sqrt{x} \) of the input: sqrt(121 m^2) = 11 m.
More information here.
cbrt (Cube root)
Return the cube root \( \sqrt[3]{x} \) of the input: cbrt(8 m^3) = 2 m.
More information here.
sqr (Square function)
Return the square of the input, \( x^2 \): sqr(5 m) = 25 m^2.
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.
round_in (Rounding)
Round to the nearest multiple of base.
floor (Floor function)
Returns the largest integer less than or equal to \( x \). See also: floor_in.
More information here.
floor_in (Floor function)
Returns the largest integer multiple of base less than or equal to value.
ceil (Ceil function)
Returns the smallest integer greater than or equal to \( x \). See also: ceil_in.
More information here.
ceil_in (Ceil function)
Returns the smallest integer multiple of base greater than or equal to value.
trunc (Truncation)
Returns the integer part of \( x \). Non-integer numbers are always truncated towards zero. See also: trunc_in.
More information here.
trunc_in (Truncation)
Truncates to an integer multiple of base (towards zero).
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.
mod (Modulo)
Calculates the least nonnegative remainder of \( a (\mod b) \). More information here.
Transcendental functions
Defined in: math::transcendental
exp (Exponential function)
The exponential function, \( e^x \). More information here.
ln (Natural logarithm)
The natural logarithm with base \( e \). More information here.
log (Natural logarithm)
The natural logarithm with base \( e \). More information here.
log10 (Common logarithm)
The common logarithm with base \( 10 \). More information here.
log2 (Binary logarithm)
The binary logarithm with base \( 2 \). More information here.
gamma (Gamma function)
The gamma function, \( \Gamma(x) \). More information here.
Trigonometry
Defined in: math::trigonometry
sin (Sine)
More information here.
cos (Cosine)
More information here.
tan (Tangent)
More information here.
asin (Arc sine)
More information here.
acos (Arc cosine)
More information here.
atan (Arc tangent)
More information here.
atan2
More information here.
sinh (Hyperbolic sine)
More information here.
cosh (Hyperbolic cosine)
More information here.
tanh (Hyperbolic tangent)
More information here.
asinh (Area hyperbolic sine)
More information here.
acosh (Area hyperbolic cosine)
More information here.
atanh (Area hyperbolic tangent )
More information here.
Statistics
Defined in: math::statistics
maximum (Maxmimum)
Get the largest element of a list.
minimum (Minimum)
Get the smallest element of a list.
mean (Arithmetic mean)
Calculate the arithmetic mean of a list of quantities. More information here.
variance (Variance)
Calculate the population variance of a list of quantities. More information here.
stdev (Standard deviation)
Calculate the population standard deviation of a list of quantities. More information here.
median (Median)
Calculate the median of a list of quantities. More information here.
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.
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.
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.
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.
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.
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.
Random sampling, distributions
Defined in: core::random, math::distributions
random (Standard uniform distribution sampling)
Uniformly samples the interval \( [0,1) \).
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.
rand_int (Discrete uniform distribution sampling)
Uniformly samples integers from the interval \( [a, b] \). More information here.
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.
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.
rand_norm (Normal distribution sampling)
Samples a normal distribution with mean \( \mu \) and standard deviation \( \sigma \) using the Box-Muller transform. More information here.
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.
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.
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.
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.
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.
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.
lcm (Least common multiple)
The smallest positive integer that is divisible by both \( a \) and \( b \). More information here.
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.
Compute the derivative of \( f(x) = x² -x -1 \) at \( x=1 \).
Run this exampleCompute 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]
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.
Find the root of \( f(x) = x² +x -2 \) in the interval \( [0, 100] \).
Run this exampleroot_newton (Newton's method)
Find the root of the function \( f(x) \) and its derivative \( f'(x) \) using Newton's method. More information here.
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.
Compute the fixed poin of \( f(x) = x/2 -1 \).
Run this examplePercentage calculations
Defined in: math::percentage_calculations
increase_by
Increase a quantity by the given percentage. More information here.
decrease_by
Decrease a quantity by the given percentage. More information here.
percentage_change
By how many percent has a given quantity increased or decreased?. More information here.
Geometry
Defined in: math::geometry
hypot2
The length of the hypotenuse of a right-angled triangle \( \sqrt{x^2+y^2} \).
hypot3
The Euclidean norm of a 3D vector \( \sqrt{x^2+y^2+z^2} \).
circle_area
The area of a circle, \( \pi r^2 \).
circle_circumference
The circumference of a circle, \( 2\pi r \).
sphere_area
The surface area of a sphere, \( 4\pi r^2 \).
sphere_volume
The volume of a sphere, \( \frac{4}{3}\pi r^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.
Solve the equation \( 2x² -x -1 = 0 \)
Run this examplecubic_equation (Solve cubic equations)
Returns the solutions of the equation a x³ + b x² + c x + e = 0. More information here.
Solve the equation \( x³ - 6x² + 11x - 6 = 0 \)
Run this exampleTrigonometry (extra)
Defined in: math::trigonometry_extra