How to broadcast over only certain function arguments?

You can turn one of the arguments into a tuple to consider it as a scalar:

For example (if I understood your question correctly):

f(x,y) = sum(x) + y
f([1,2,3], 4) #-> 10
f([1,2,3], 5) #-> 11

f.([1,2,3], [4,5])    # -> error

f.(([1,2,3],), [4,5]) # -> [10, 11]
                      # this works because ([1,2,3],) is now considered
                      # to be scalar and is broadcasted

Note how in the second case, the array argument [1,2,3] has been turned into a 1-element tuple containing the actual array argument: ([1,2,3],)

It would also have been possible to use Ref([1,2,3]) to achieve the same effect:

f.(Ref([1,2,3]), [4,5]) #-> [10, 11]
9 Likes