-
Notifications
You must be signed in to change notification settings - Fork 17
Broadcasting
Jake Bolewski edited this page Jun 18, 2021
·
4 revisions
Interface: https://docs.julialang.org/en/v1/manual/interfaces/#man-interfaces-broadcasting
- each
arginargscallsarg = 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 callmaterializeon the result of 1.
- For non-
Broadcastedobjects, this simply returns the object - For
bc = Broadcasted{S}(f, args), this calls-
bc = instantiate(bc): constructs or checks the axes:- if
bc.axesisnothing, callscombine_axes(bc.args...)- this calls
axes(arg)on eacharg - combines them with
broadcast_shape(ax1, ax2, ...)- currently only defined for
Tuplearguments - 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.axesis notnothingcalls `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
Tuplearguments - 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
bcis 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
argaliases 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-
Broadcastedobjects, it wraps it in anidentityBroadcastedobject. - 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-