returnn.util.math

Some mathematical functions, in pure NumPy.

returnn.util.math.ceil_div(a: int, b: int) int[source]

ceil(a / b)

returnn.util.math.next_power_of_two(n: int) int[source]

next power of two, >= n

class returnn.util.math.PiecewiseLinear(values: Dict[int | float, int | float], *, kw_name: str | None = None, ignore_other_kwargs: bool = False)[source]

Piecewise linear function. (Basically wraps numpy.interp.)

Parameters:
  • values – dict x -> y. Everything between the x values is linearly interpolated. Everything outside is repeated from the nearest x value.

  • kw_name – keyword argument name to use in the __call__. Other keyword arguments are ignored.

  • ignore_other_kwargs – if True, ignore other keyword arguments in the __call__.

returnn.util.math.simplify_and_format_number(n: int | float) str[source]

Format a number, removing trailing zeros and the decimal point if it is an integer

returnn.util.math.merge_random_seeds(data_sources: Sequence[int], *, num_bytes: int = 4, signed: bool = False) int[source]
Parameters:
  • data_sources – A list of integers. We expect that they are all representable as 64-bit signed integers.

  • num_bytes – for the output seed.

  • signed – whether the output seed should be signed.

Returns:

A num_bytes*8-bit integer seed, deterministically derived from the input data.