flopscope.

flopscope.numpy.broadcast

flopscope.numpy.broadcast

Produce an object that mimics broadcasting.

Parameters

in1, in2, ...:array_like

Input parameters.

Returns

b:broadcast object

Broadcast the input parameters against one another, and return an object that encapsulates the result. Amongst others, it has shape and nd properties, and may be used as an iterator.

See also

Examples

Manually adding two vectors, using broadcasting:

>>> import flopscope.numpy as fnp
>>> x = flops.array([[1], [2], [3]])
>>> y = flops.array([4, 5, 6])
>>> b = flops.broadcast(x, y)
>>> out = flops.empty(b.shape)
>>> out.flat = [u+v for (u,v) in b]
>>> out
array([[5.,  6.,  7.],
       [6.,  7.,  8.],
       [7.,  8.,  9.]])

Compare against built-in broadcasting:

>>> x + y
array([[5, 6, 7],
       [6, 7, 8],
       [7, 8, 9]])