[docs]defas_int_arrays(input_:typing.Sequence[int|typing.Iterable[int]],)->tuple[numpy.typing.NDArray[numpy.int_],...]:"""Convert integer sequences into arrays, and checks for matching lenghts. Uses :py:func:`numpy.asarray`, which only converts arguments if they are not already integer arrays. We then use :py:func:`numpy.atleast_1d` to ensure all output arrays have at least 1 dimension. Parameters ---------- input_ Integer sequences to be converted into numpy arrays of integers. Returns ------- A tuple with input arrays converted. All input arrays contain are at least one dimensional. Raises ------ TypeError If the dimensions of the various arrays do not match. """retval=tuple(numpy.atleast_1d(numpy.asarray(k,dtype=numpy.int_))forkininput_)shapes=tuple(k.shapeforkinretval)iflen(set(shapes))!=1:shape_str=", ".join([f"input_[{i}].shape = {k}"fori,kinenumerate(shapes)])raiseTypeError(f"The number of dimensions on input arrays is different: {shape_str}")returnretval
[docs]defsafe_divide(n:int|float,d:int|float)->float:r"""Divide n by d. Returns 0.0 in case of a division by zero. Parameters ---------- n Numerator. d Denominator. Returns ------- :math:`\frac{n}{d}`` if :math:`d \ne 0`, else 0. """returnn/(d+(d==0))