What is interaction between F.() broadcasting and keyword args?

How does F.(a, b; c=d) work with respect to broadcasting? I understand that the arguments a, b will be broadcast. What about the keyword argument?

3 Likes

F.(a, b; c=d) is equivalent to broadcast((a,b) -> F(a, b; c=d), a, b). That is, it does not broadcast over the keyword arguments, but instead passes them inside the closure.

7 Likes

Any workaround to broadcast the keyword arguments? Should I lower these to a _func?

1 Like

Just call broadcast explicitly and you can use whatever arguments you want.

1 Like

This is suppose to be a friendly end-user API… I ended up lowering the function and checking whether any of the arguments where AbstractVector to decide on whether to broadcast or not. Poor man type dispatch since there are too many combinations for a simple type dispatch.

I came across this thread after encountering a similar situation. The code below seems to offer a feasible solution. Note that there might be a small performance penalty for very simple functions, such as the example below, but it seems to be negligible in most cases.

f(a,b) = a + b
f(;a,b) = f.(a,b)

Output:

julia> f(;a=1,b=2)
3

julia> f(;a=1,b=[1,2])
2-element Array{Int64,1}:
 2
 3

A simple solution is to define a wrapper function:

wrap(a, b, c) = f(a, b; c=c)
wrap.(A, B, C) # broadcasts on three args.
1 Like

Is this documented somewhere? In the sense one can rely on this behavior and it won’t change except in 2.0.

I can’t find mention of keyword args in Multi-dimensional Arrays · The Julia Language.

It should be documented; I’ve made a PR: https://github.com/JuliaLang/julia/pull/44033

1 Like

Great, thanks.