colour.algebra.sdiv#

colour.algebra.sdiv(a: ArrayLike, b: ArrayLike) NDArrayFloat[source]#

Perform safe division of array \(a\) by array \(b\) while handling zero-division cases.

Avoid NaN and +/- inf generation when array \(b\) contains zero values. The zero-division handling behaviour is controlled by the colour.algebra.set_sdiv_mode() definition or the sdiv_mode() context manager. The following modes are available:

  • Numpy: The current Numpy zero-division handling occurs.

  • Ignore: Zero-division occurs silently.

  • Warning: Zero-division occurs with a warning.

  • Ignore Zero Conversion: Zero-division occurs silently and NaNs or +/- infs values are converted to zeros. See numpy.nan_to_num() definition for more details.

  • Warning Zero Conversion: Zero-division occurs with a warning and NaNs or +/- infs values are converted to zeros. See numpy.nan_to_num() definition for more details.

  • Ignore Limit Conversion: Zero-division occurs silently and NaNs or +/- infs values are converted to zeros or the largest +/- finite floating point values representable by the division result numpy.dtype. See numpy.nan_to_num() definition for more details.

  • Warning Limit Conversion: Zero-division occurs with a warning and NaNs or +/- infs values are converted to zeros or the largest +/- finite floating point values representable by the division result numpy.dtype.

  • Replace With Epsilon: Zero-division is avoided by replacing zero denominators with the machine epsilon value from colour.constants.EPSILON.

  • Warning Replace With Epsilon: Zero-division is avoided by replacing zero denominators with the machine epsilon value from colour.constants.EPSILON with a warning.

Parameters:
Returns:

Array \(a\) safely divided by \(b\).

Return type:

np.float or numpy.ndarray

Examples

>>> a = np.array([0, 1, 2])
>>> b = np.array([2, 1, 0])
>>> sdiv(a, b)
array([0., 1., 0.])
>>> try:
...     with sdiv_mode("Raise"):
...         sdiv(a, b)
... except Exception as error:
...     error
FloatingPointError('divide by zero encountered in...divide')
>>> with sdiv_mode("Ignore Zero Conversion"):
...     sdiv(a, b)
array([0., 1., 0.])
>>> with sdiv_mode("Warning Zero Conversion"):
...     sdiv(a, b)
array([0., 1., 0.])
>>> with sdiv_mode("Ignore Limit Conversion"):
...     sdiv(a, b)
array([0.00000000e+000, 1.00000000e+000, 1.79769313e+308])
>>> with sdiv_mode("Warning Limit Conversion"):
...     sdiv(a, b)
array([0.00000000e+000, 1.00000000e+000, 1.79769313e+308])
>>> with sdiv_mode("Replace With Epsilon"):
...     sdiv(a, b)
array([0.00000000e+00, 1.00000000e+00, ...])
>>> with sdiv_mode("Warning Replace With Epsilon"):
...     sdiv(a, b)
array([0.00000000e+00, 1.00000000e+00, ...])