Onyx logo

Previous topic

onyx.util.pi_spigots – Spigot algorithms for digits of pi

Next topic

onyx.util.floatutils – Simple routines to exactly serialize floats as strings in a readable

This Page

onyx.util.safediv – Utility routine for doing safe division of Numpy arrays

onyx.util.safediv.safely_divide_float_array(numerator, denom)

Divide (element-wise) one Numpy array of dtype float by another, suppressing warnings about division by 0.0. Raises a ValueError in case there is any 0.0 in the denominator whose corresponding value in the numerator is not also 0.0. Converts all NaNs resulting from 0.0/0.0 operations to 0.0.

>>> n = np.zeros((2,2), dtype=float)
>>> d = np.zeros((2,2), dtype=float)
>>> r = safely_divide_float_array(n,d)
>>> r
array([[ 0.,  0.],
       [ 0.,  0.]])
>>> n[0,0] = 1.0
>>> r = safely_divide_float_array(n,d)
Traceback (most recent call last):
  ...
ValueError: Unable to safely divide; denominator array has a 0.0 where numerator array has a non-zero value
>>> d[0,0] = 10.0
>>> r = safely_divide_float_array(n,d)
>>> r
array([[ 0.1,  0. ],
       [ 0. ,  0. ]])