experimental.math_extras

Assorted mathematical and statistical functions that can be re-used across scripts

Intended to augment Python’s standard math library with additional useful functions

experimental.math_extras.geometric_mean(l)

Calculate the geometric mean value of an array

Parameters

l – An iterable collection of numbers

Returns

The geometric mean of the list, or 0 of the list is empty

experimental.math_extras.harmonic_mean(l)

Calculate the harmonic mean value of an array

Parameters

l – An iterable collection of numbers

Returns

The harmonic mean of the list, or 0 of the list is empty

experimental.math_extras.mean(l)

Calculate the arithmetic mean value from an array

Parameters

l – An iterable collection of numbers

Returns

The arithmetic mean of the list, or 0 of the list is empty

experimental.math_extras.median(l)

Calculate the median value from an array

This is a cheater median, as we always choose the lower value if the length is even instead of averaging the middle two values. This is faster, but mathematically incorrect.

Parameters

l – An iterable collection of numbers

Returns

The median value from the list, or 0 if the list is empty

experimental.math_extras.mode(l)

Calculate the mode of an array

The mode is the most-occurring item; if multiple items are tied, the median of the tied items is returned

Parameters

l – An iterable collection of numbers

Returns

The mode of the list, or 0 if the list is empty

experimental.math_extras.prod(l)

Calculate the product of all items in a list

Equivalent the Python3’s math.prod

Parameters

l – An iterable collection of numbers

Returns

The product of all items in the list, or 0 if the list is empty

experimental.math_extras.rescale(x, old_min, old_max, new_min, new_max, clip=True)

Convert x in [old_min, old_max] -> y in [new_min, new_max] using linear interpolation

Parameters
  • x – The value to convert

  • old_min – The old (inclusive) minimum

  • old_max – The old (inclusive) maximum

  • new_min – The new (inclusive) minimum

  • new_max – The new (inclusive) maximum

  • clip – If true, we clip values within the [min, max] range; otherwise we extrapolate based on the ranges

experimental.math_extras.solve_linear_system(m)

Use gaussian elimination to solve a series of linear equations

The provided matrix is the augmented matrix representation:

[
    [k11 k12 k12 ... k1n  a1]
    [k21 k22 k22 ... k2n  a2]
    .
    .
    .
    [kmn kmn kmn ... kmn  am]
]
Parameters

m – The augmented matrix representation of the series of equations. This array is mangled in the process of calculation

Returns

A matrix of the coefficients of the equation