Skip to main content

Maths and statistics

Elementary and special functions, complex numbers, statistical reductions and tolerant comparisons.
import rustymath as rm
import rustymath.mathish as mm

Elementary functions

Functions accept scalars or arrays. Float functions compute integer inputs at float32 up to int16, and at float64 for wider integers. Float arrays retain their width:

rm.exp([0., 1, 2]), rm.log([1., 10, 100]), rm.sin([0., 0.5, 1])
(Array([1.0, 2.7182818, 7.3890561], dtype=float64),
 Array([0.0, 2.3025851, 4.6051702], dtype=float64),
 Array([0.0, 0.47942554, 0.84147098], dtype=float64))

root(n, y) gives the real nth root of y. logb(base, x) takes its base first:

rm.root(3, [8., 27., 1000.]), rm.logb(2, [8., 16.])
(Array([2.0, 3.0, 10.0], dtype=float64), Array([3.0, 4.0], dtype=float64))

expm1(x) and log1p(x) retain small changes that exp(x)-1 and log(1+x) lose to rounding. Recover a small continuously compounded growth rate from its proportional change:

rate = 1e-16
change = rm.expm1(rate)
rm.exp(rate) - 1, change, rm.log1p(change)
(0.0, 1e-16, 1e-16)
Family Functions
Exponentials and logarithms exp, expm1, log, log1p, log2, log10, logb
Powers and roots power, root, sqrt, cbrt
Circular and hyperbolic sin, cos, tan, arcsin, arccos, arctan, arctan2; sinh, cosh, tanh, arcsinh, arccosh, arctanh
Integer bits bitwise_and, bitwise_or, bitwise_xor, bitwise_not, left_shift, right_shift
Boolean operations and predicates logical_and, logical_or, logical_xor, logical_xnor; isfinite, isinf, isnan

hypot(x, y) computes a two-dimensional vector’s magnitude without intermediate overflow or underflow from squaring its components. The specialized roots sqrt1mx2, sqrt1px2 and sqrtx2m1 compute √(1 - x²), √(1 + x²) and √(x² - 1) directly. For real |x| >= 1, sqrtx2m1(x) is sign(x) * sqrt(x*x-1).

round(x, decimals) rounds values to a decimal place, with ties to even. rint rounds to whole values. Neither changes how many digits the array’s repr displays. degrees and radians convert angular units.

rm.round([1.2345, -2.71828], 2), rm.degrees([0., 1.5707963267948966])
(Array([1.23, -2.72], dtype=float64), Array([0.0, 90.0], dtype=float64))

derivative(f, *args) gives a native elementwise function’s derivative at its arguments: an array for a function of one argument, and a tuple of its partial derivatives for a function of two. value_and_derivative(f, *args) also returns f’s result. The derivative formulas reuse that result where needed, so f runs once. Passing a function with no derivative, such as floor, raises ValueError:

value, slope = rm.value_and_derivative(rm.tanh, [-1., 0, 1])
slope, 1 - value**2, rm.derivative(rm.power, 2., 3.)

The Rust names follow Rust’s standard library instead. Python’s log, power, arcsin and arctan2 are Rust’s ln, pow, asin and atan2.

Complex numbers

Complex exp, log, power, sin, cos, sinh and cosh use vectorized real loops. Real inputs to sqrt and log give NaN outside their real domains. Supply complex values to request principal complex values:

rm.sqrt([-4., 4.]), rm.sqrt([-4+0j, 3+4j]), rm.log([-1+0j])
(Array([nan, 2.0], dtype=float64),
 Array([2j, (2+1j)], dtype=complex128),
 Array([3.1415927j], dtype=complex128))

cis(theta) constructs a unit complex number with angle theta in radians. real, imag, absolute and angle return real values even for complex inputs. Predicates and equality give Booleans. conj negates the imaginary part:

z = 2 * rm.cis([0., 0.5, 1.])
z, rm.real(z), rm.imag(z), rm.absolute(z), rm.angle(z), rm.conj(z)
(Array([(2+0j), (1.7551651+0.95885108j), (1.0806046+1.682942j)], dtype=complex128),
 Array([2.0, 1.7551651, 1.0806046], dtype=float64),
 Array([0.0, 0.95885108, 1.682942], dtype=float64),
 Array([2.0, 2.0, 2.0], dtype=float64),
 Array([0.0, 0.5, 1.0], dtype=float64),
 Array([(2-0j), (1.7551651-0.95885108j), (1.0806046-1.682942j)], dtype=complex128))

Special functions

The special functions use SciPy’s names:

Family Functions
Gamma and log gamma gamma(x), gammaln(x), loggamma(x), digamma(x), polygamma(n, x)
Error function and complement erf(x), erfc(x), erfinv(p), erfcinv(q)
Regularized incomplete gamma gammainc(a, x), gammaincc(a, x), gammaincinv(a, p)
Beta and regularized incomplete beta beta(a, b), betainc(a, b, x), betaincinv(a, b, p)
Factorial and binomial factorial(x), binom(n, k), binom_dn(n, k), binom_dk(n, k)
Lambert W lambertw(x)

gammaln(x) gives the real log of the magnitude of gamma(x). loggamma(x) gives the principal branch of log gamma as a complex value, including for real inputs. rm.factorial extends factorial to floating-point and complex arguments. Use number_theory.factorial for exact integers:

rm.gamma([0.5, 5, 10]), rm.erfinv(0.5)
(Array([1.7724539, 24.0, 362880.0], dtype=float64), 0.4769362762044699)

digamma(x) is the derivative of gammaln(x), so its value at 1 is minus the Euler–Mascheroni constant. binom_dn(n, k) and binom_dk(n, k) give the partial derivatives of the binomial coefficient with respect to n and k, respectively. For a non-whole n and a whole k up to 64, binom and binom_dn are within 2k ulps, not the usual 4. C(n, 2) is n(n - 1)/2, whose slope in n at 10 is 9.5:

rm.digamma(1.), rm.binom_dn(10., 2)
(-0.5772156649015329, 9.5)

Invert a regularized incomplete beta probability, then recover the probability from its quantile:

quantile = rm.betaincinv(2, 5, 0.9)
quantile, rm.betainc(2, 5, quantile)
(0.5103163065514917, 0.9)

lambertw supplies the principal branch W₀. It solves w * exp(w) = x:

w = rm.lambertw(10.)
w, w * rm.exp(w)
(1.7455280027406994, 10.0)

Reductions and statistics

With no axis, reductions summarize all items. Pass an axis position, name or tuple of axes to summarize those axes. Here each row holds one series. clip limits values to the given bounds in one pass:

measurements = rm.array([[1., 2, 3], [4, 5, 6]])
rm.mean(measurements, axis=1), rm.std(measurements, axis=1), rm.clip(measurements, 2., 5.)
(Array([2.0, 5.0], dtype=float64),
 Array([0.81649658, 0.81649658], dtype=float64),
 Array([[2.0, 2.0, 3.0], [4.0, 5.0, 5.0]], dtype=float64))

diff takes adjacent differences along an axis; ptp gives maximum minus minimum, and trace sums the diagonal of each matrix in the last two axes. Integer differences wrap where they don’t fit, as NumPy’s do. Boolean differences report a change (XOR).

rm.diff(measurements), rm.ptp(measurements, axis=1), rm.trace(measurements)
(Array([[1.0, 1.0], [1.0, 1.0]], dtype=float64),
 Array([2.0, 2.0], dtype=float64),
 6.0)

var and std divide by n - ddof, with ddof=0 by default. average broadcasts weights by aligning leading axes, as other functions do. A single row of observation weights applies to every series:

weights = [[1., 1, 2]]
rm.average(measurements, axis=1, weights=weights), rm.std(measurements, axis=1, ddof=1)
(Array([2.25, 5.25], dtype=float64), Array([1.0, 1.0], dtype=float64))

cov and corrcoef treat rows as variables unless rowvar=False. Summaries of float16 data accumulate in float32 and return float32. Complex variance and norms return real values:

rm.cov(measurements), rm.corrcoef(measurements)
(Array([[1.0, 1.0], [1.0, 1.0]], dtype=float64),
 Array([[1.0, 1.0], [1.0, 1.0]], dtype=float64))

norm computes an elementwise p-norm over the selected axes. ord=1 sums magnitudes, ord=0 counts nonzeros, and ord=float('inf') selects the greatest magnitude. For a two-dimensional input, norm is still elementwise, not a matrix-induced norm:

rm.norm([3., 4.]), rm.isfinite([1., float('inf'), float('nan')])
(5.0, Array([True, False, False], dtype=bool))

histogram returns counts and bin edges. Give a bin count and range, or explicit increasing edges. The final bin includes its upper edge. Values outside the edges and NaNs are ignored:

rm.histogram(measurements, bins=3, range=(0, 6)), rm.histogram(measurements, bins=[0., 2, 5, 6])
((Array([1, 2, 3], dtype=int64), Array([0.0, 2.0, 4.0, 6.0], dtype=float64)),
 (Array([1, 3, 2], dtype=int64), Array([0.0, 2.0, 5.0, 6.0], dtype=float64)))

Scans keep a running result. accumulate(f, x) applies a native binary function from left to right over the flattened input, or along one axis specified by position or name. A scalar start is the initial value for each run along the axis, and adds no output item. Here subtraction starts at 10 and subtracts each item in order. logical_scan accepts any binary Boolean function as a 2 by 2 truth table; this table computes running exclusive-or:

rm.accumulate(rm.subtract, [3., 2, 1], start=10), rm.logical_scan([False, True, True, False], [[False, True], [True, False]])
(Array([7.0, 5.0, 4.0], dtype=float64),
 Array([False, True, False, False], dtype=bool))

sliding(f, x, width, axis=-1) reduces each window of width neighbouring items using a native associative binary function: add, multiply, maximum, minimum or a logical function. An axis of n items gives n - width + 1 results. Windows of more than 8 items use van Herk-Gil-Werman’s method, whose computation cost per item does not grow with the window width. sliding_argmax and sliding_argmin return the position along the axis of each window’s first maximum or minimum. Here sliding_argmax gives 5, the position of the 9, for each window that holds it. Reducing a view returned by windows along an axis runs the corresponding sliding reduction directly on the view’s storage, without building the windows: rm.sum(rm.windows(x, 3), axis=1) gives the same result as sliding(add, x, 3).

v = [3., 1, 4, 1, 5, 9, 2, 6]
rm.sliding(rm.add, [1., 2, 3, 4, 5], 3), rm.sum(rm.windows([1., 2, 3, 4, 5], 3), axis=1), rm.sliding(rm.maximum, v, 4), rm.sliding_argmax(v, 4)
(Array([6.0, 9.0, 12.0], dtype=float64),
 Array([4.0, 5.0, 9.0, 9.0, 9.0], dtype=float64),
 Array([2, 4, 5, 5, 5], dtype=int64))

Correlation and convolution

correlate(x, kernel, mode='valid') slides kernel along x and gives the sum of products of the window and the kernel at each position. convolve reverses the kernel first. The modes are SciPy’s: 'valid' keeps windows inside x, 'full' keeps every overlap, and 'same' returns one output for each position in x, centred as NumPy centres them. The kernel isn’t conjugated.

rm.correlate([1., 2, 3, 4], [1., 10]), rm.convolve([1., 2, 3, 4], [1., 10], 'full')
(Array([21.0, 32.0, 43.0], dtype=float64),
 Array([1.0, 12.0, 23.0, 34.0, 40.0], dtype=float64))

A kernel can have more axes than the axes it slides along. slide sets how many leading axes of x the kernel slides along. The remaining axes of x are contracted with the kernel, as a convolution layer contracts an image’s channels. Each position has one output for each item along the kernel’s remaining axes. Here a 3 by 3 kernel moves 2 positions at a time over a 9 by 8 image with 3 channels, producing 4 outputs at each position in a 4 by 3 grid:

rm.correlate(rm.ones((9, 8, 3)), rm.ones((3, 3, 3, 4)), slide=2, stride=2).shape
(4, 3, 4)

f and g replace the sum and the product, as in linalg.inner. Using maximum for f and add for g computes a dilation:

rm.correlate([0., 5, 0, 0, 3], [0., 1, 0], f=rm.maximum, g=rm.add)
Array([6.0, 5.0, 3.0], dtype=float64)

overlap_add(frames, step=1, axis=-2) is the transpose of taking windows. It adds each frame back where its window was read. Items that several frames cover add up. An inverse short-time Fourier transform uses it:

rm.overlap_add([[1., 1, 1], [1., 1, 1], [1., 1, 1]], 2)
Array([1.0, 1.0, 2.0, 1.0, 2.0, 1.0, 1.0], dtype=float64)

Mathish mode

rustymath.mathish provides J-inspired conventions through the same function names as rustymath. For float64 and complex128, comparisons use relative tolerance 1e-14, zero divided by zero gives zero, and logb(1., 1.) gives zero. Narrower floats retain exact comparisons and raw division. Integer true division uses the conventions of the float type it computes in. Machine-integer floor division still raises an error for a zero divisor.

Importing rustymath.mathish does not change rm functions or array operators. Functions that take a function, such as rm.accumulate, use that function’s conventions, as in rm.accumulate(mm.divide, values). The namespace’s equal, not_equal, less, less_equal, greater and greater_equal select tolerant comparison explicitly:

mathish = mm.equal(0.1 + 0.2, 0.3), mm.divide(0., 0.), mm.logb(1., 1.)
ordinary = rm.equal(0.1 + 0.2, 0.3), rm.divide(0., 0.), rm.logb(1., 1.)
ordinary, mathish
((False, nan, nan), (True, 0.0, 0.0))

mm.floor, mm.ceil, mm.floor_divide, mm.remainder, mm.gcd and mm.lcm use the same tolerance. A value tolerantly equal to a whole number rounds to that number. mm.floor_divide(0., 0.) gives zero. Division of a nonzero real by zero still gives infinity. These conventions don’t change the stopping tolerances of iterative algorithms. Ordinary rm.floor, which has no tolerance, rounds 2.9999999999999996 down:

rm.floor([2.9999999999999996, 2.5])
Array([2.0, 2.0], dtype=float64)

floor_divide and remainder agree with each other on near-integer quotients, in rm and in mm. Here the exact float quotient of 0.3 by 0.1 is just below 3. remainder(x, 0) gives x. remainder uses NumPy’s argument order, opposite to Rust’s residue(y, x):

exact = rm.floor_divide(0.3, 0.1), rm.remainder(0.3, 0.1)
tolerant = mm.floor_divide(0.3, 0.1), mm.remainder(0.3, 0.1)
exact, tolerant
((2.0, 0.09999999999999998), (3.0, 0.0))

For complex inputs, floor rounds onto the Gaussian-integer lattice using APL’s rule. remainder uses this complex floor of the quotient. gcd uses Euclid’s algorithm on Gaussian integers:

rm.floor(1.7 + 2.8j), rm.remainder(5+0j, 2+1j), rm.gcd(5+0j, 2+1j)
((1+3j), 0j, (2+1j))

Checking accuracy

import numpy as np

To check the accuracy yourself, compare with NumPy. np.spacing gives the size of one ulp at each value. Dividing the absolute difference between the two results by this ulp size gives their distance in ulps:

xs = np.linspace(-10, 10, 100_001)
y = np.exp(xs)
(np.abs(rm.exp(xs) - y) / np.spacing(y)).max()
np.float64(2.0)