mathlib: Miscellaneous math utilities

Miscellaneous math utilities

  • base conversion

  • derivatives

  • dimension reduction

  • range alternatives

  • etc

Functions

intersection(u1, u2, v1, v2)

return the intersection of (u1, u2) and (v1, v2) or None if no intersection

frange(start[, stop, step])

Like xrange(), but returns list of floats instead

fraction_range(start[, stop, step])

Like range, but yielding Fractions

linspace(start, stop, numitems)

Similar to numpy.linspace, returns a python list

clip(x, minvalue, maxvalue)

clip the value of x between minvalue and maxvalue

prod(numbers)

rtype:

Union[Rational, float]

lcm(*numbers)

Least common multiplier between a seq.

min_common_denominator(floats[, limit])

find the min common denominator to express floats as fractions

convert_any_base_to_base_10(s, base)

Converts given number s, from base b to base 10

convert_base_10_to_any_base(x, base)

Converts given number x, from base 10 to base b

convert_base(s, frombase, tobase)

Converts s from base a to base b

euclidian_distance(values[, weights])

Reduces distances in multiple dimensions to 1 dimension.

geometric_mean(numbers)

The geometric mean is often used to find the mean of data measured in different units.

harmonic_mean(numbers)

The harmonic mean is used to calculate F1 score.

split_interval_at_values(start, end, offsets)

Split interval (start, end) at the given offsets

derivative(func[, h])

Return a function which is the derivative of the given func

logrange(start, stop[, num, base])

create an array [start, ..., stop] with a logarithmic scale

randspace(begin, end, numsteps[, include_end])

go from begin to end in numsteps at randomly spaced steps

fib(n)

calculate the fibonacci number n (accepts fractions)

interpfib(x, x0, y0, x1, y1)

Fibonacci interpolation

roundrnd(x)

Round x to its nearest integer, taking the fractional part as the probability

roundres(x[, resolution])

Round x with given resolution

next_in_grid(x, step[, offset])

The next value in the grid defined by step/offset which is >= x

modulo_shortest_distance(x, origin, mod)

Return the shortest distance to x from origin around a circle of modulo mod.

rotate2d(point, degrees[, origin])

A rotation function that rotates a point around an origin

optimize_parameter(func, val, paraminit[, ...])

Optimize one parameter to arrive to a desired value.

ispowerof2(x)

Is x a power of two?

nextpowerof2(x)

Return the lowest power of two >= x