-
Notifications
You must be signed in to change notification settings - Fork 9
Broadcasting
Jake Bolewski edited this page Jun 18, 2021
·
4 revisions
Interface: https://docs.julialang.org/en/v1/manual/interfaces/#man-interfaces-broadcasting
- each
arg
inargs
callsarg = broadcastable(arg)
- call
S = combine_styles(args...)
- this calls
s = BroadcastStyle(typeof(arg))
on eacharg
- combines them with
S = result_style(s1, s2, ...)
- this calls
- call
broadcasted(style, f, args...)
- default returns
Broadcasted{S}(f, args)
- certain cases here are special cased: e.g. adding a
Real
+Range
- default returns
- regular assignment (
a = f.(args...)
) will callmaterialize
on the result of 1.
- For non-
Broadcasted
objects, this simply returns the object - For
bc = Broadcasted{S}(f, args)
, this calls-
bc = instantiate(bc)
: constructs or checks the axes:- if
bc.axes
isnothing
, callscombine_axes(bc.args...)
- this calls
axes(arg)
on eacharg
- combines them with
broadcast_shape(ax1, ax2, ...)
- currently only defined for
Tuple
arguments - handles the array broadcasting semantics
- length 1 axes can be extended
- pad out the shape if required
- currently only defined for
- this calls
- if
bc.axes
is notnothing
calls `check_broadcast_axes(bc.axes, bc.args...)- this is mainly used for broadcasted assignment (see below)
- calls
check_broadcast_axes(bc.axes, arg)
for eacharg
- calls
check_broadcast_shape(bc.axes, axes(arg))
- again, only defined for
Tuple
arguments - checks that
axes(arg)
is compatible with `bc.axes
- again, only defined for
- calls
- if
-
copy(bc)
this first attempts to infer the concrete return type elementElType
:- if successful, it calls
-
dest = similar(bc, ElType)
to construct the output object -
copyto!(dest, bc)
- if
bc
is broadcasting a scalar, it handles it specially - otherwise
- it converts
bc = Broadcasted{Nothing}(bc.f, bc.args, bc.axes)
- calls
copyto!(dest, bc)
- if
identity.(arg)
, callscopyto!(dest, arg)
-
bc = preprocess(destm bc)
- checks if each
arg
aliases withdest
- if they are exactly equal, it leaves them (since they won't overlap)
- otherwise calls
unalias(dest, arg)
(recursively)
-
bc = extrude(bc)
- ???
- checks if each
- iterates `I in eachindex(bc)
dest[I] = bc′[I]
- if
- it converts
- if
-
- if not, it uses widening:
- it evaluates the first item, and creates an output object with the same element type
- it calls
copyto_nonleaf!
to construct the rest and widen as necessary
- if successful, it calls
-
- For broadcasted assignment (
dest .= f.(args...)
), it callsmaterialize!(dest, bc)
:- For non-
Broadcasted
objects, it wraps it in anidentity
Broadcasted
object. - For
bc = Broadcasted{S}(f, args)
-
DS = combine_styles(dest, bc)
(see above) -
broadcasted!(DS, dest, bc)
- this constructs a new object
dbc = Broadcasted{DS}(bc.f, bc.args, axes(dest))
- calls
dbc = instantiate(dbc)
(which checks the axes are compatible) copyto!(dest, dbc)
- this constructs a new object
-
- For non-