What is the most julian way to broadcast a call to a vector-valued function?
For example, take the following function.
function return_vec(x)
return [x, x^2]
end
I would like to collect outputs for each x
in 1:15
and store the result in a 15x2 Matrix. I can do either of the following quickly:
julia> @btime hcat(return_vec.(1:15)...)';
548.913 ns (22 allocations: 2.11 KiB)
julia> @btime hcat(map(return_vec,1:15)...)';
563.187 ns (22 allocations: 2.11 KiB)
but somewhat inefficiently. Alternatively, I can define:
function manual_broadcast(X)
out = zeros(eltype(X), length(X),2)
for i in eachindex(X)
out[i,:] .= return_vec(X[i])
end
return out
end
And get a 1.74x speedup:
julia> @btime manual_broadcast(1:15);
316.522 ns (16 allocations: 1.47 KiB)
But is there any syntactic sugar that would effectively implement manual_broadcast
? Surely this is common enough that I don’t need to write my own function to do it?