xp_broadcast_promote

xp_broadcast_promote(*args, ensure_writeable=False, force_floating=False, xp: ModuleType | None = None)[source][source]

Broadcast arrays and promote to a common data type.

Parameters:
  • *args (sequence of array_like) – The arrays to broadcast and promote.

  • ensure_writeable (bool, optional) –

    If True, ensure that the returned arrays are writeable by making a copy

    if necessary.

  • force_floating (bool, optional) –

    If True, ensure that the returned arrays have a floating-point data

    type.

  • xp (module, optional) –

    The array API namespace to use. If not provided, the namespace is

    inferred from the arrays.

Returns:

The broadcasted and promoted arrays. The order of the arrays in the

output list corresponds to the order of the input arrays.

Return type:

list of Arrays

Notes

This function performs two operations: 1. Broadcasts all arrays to a common shape. 2. Promotes all arrays to a common data type.

If force_floating is True, the common data type will be a floating-point

type, even if all input arrays have integer types.

Examples

>>> import numpy as np
>>> a = np.array([1, 2, 3])
>>> b = np.array([[4], [5], [6]])
>>> c, d = xp_broadcast_promote(a, b, xp=np)
>>> c.shape  # Broadcasted to shape (3, 3)
(3, 3)
>>> d.shape  # Broadcasted to shape (3, 3)
(3, 3)
>>> c
array([[1, 2, 3],
       [1, 2, 3],
       [1, 2, 3]])
>>> d
array([[4, 4, 4],
       [5, 5, 5],
       [6, 6, 6]])
>>> # With force_floating=True
>>> e, f = xp_broadcast_promote(a, b, force_floating=True, xp=np)
>>> e.dtype  # Promoted to float
dtype('float64')