common#

A module with JIT-compiled code for common functionalities.

Most of the functions listed below are available in base Python or NumPy. However, a common underlying reason to re-implement this in Numba is that those functions cannot be called or called with the required arguments within a JIT-compiled function.

minterpy.jit_compiled.common.n_choose_r(n, r)[source]#

Compute the binominal coefficient (n choose r) iteratively.

Parameters:
  • n (int) – The total number of elements to choose from.

  • r (int) – The number of elements to form the combinations.

Returns:

The number of ways to choose r elements from n elements (i.e., the binomial coefficient).

Return type:

int

Raises:

OverflowError – If any of the arguments is signed (which does not make any sense anyway).

Notes

  • This iterative implementation is an alternative to math.comb() (from the math module) such that it can be used inside a Numba JIT-ted function.

minterpy.jit_compiled.common.combinations_iter(xx, r)[source]#

Return successive r-length combinations of elements as an array.

Parameters:
  • xx (numpy.ndarray) – A one-dimensional array of integers to select from.

  • r (int) – The number of elements to select from the array of integers.

Returns:

A two-dimensional array of integers; each rows is the selected combination. The total number of rows is \(n \choose r\) where \(n\) is the length of the input array. The r-length combinations are sorted lexicographically.

Return type:

numpy.ndarray

Notes

  • This is iterative implementation is suitable for Numba as an alternative to itertools.combinations() (from the itertools module).

  • This is not a generator function; all possible combinations of length r will be returned at once. Unlike the function itertools.combinations(), the output of this function is a two-dimensional array instead of a tuple generator.

  • This function is created to avoid using a generator function inside a Numba JIT-ted function; generator functions causes memory leaks.

  • In the way this function is used by the relevant part in Minterpy, r is always close to the total length of xx so there is no explosions in the number of elements.

minterpy.jit_compiled.common.dot(a, bb)[source]#

Compute vector-matrix dot product with contiguous arrays.

Parameters:
Returns:

A one-dimensional array of length m.

Return type:

numpy.ndarray

minterpy.jit_compiled.common.get_max_columnwise(xx)[source]#

Get the maximum from each column of a two-dimensional integers array.

Parameters:

xx (numpy.ndarray) – A two-dimensional integer array.

Returns:

A one-dimensional integer array, each element contains the maximum of each column of the input array.

Return type:

numpy.ndarray

Notes

  • This is to imitate a particular way of calling numpy.max(), that is, np.max(arr, axis=0). Such a call cannot be used inside a Numba JIT-ted function as Numba does not support passing the second argument.

  • The function is NJIT-ted with integers input/output; it can’t be used with other types.