Row-wise function application (different functions)

Using map!:

function func_array_eachrow3!(W, f)
    @inbounds for i in 1:size(W, 1)
        wi = view(W, i, :)
        map!(f[i], wi, wi)
    end
    W
end

f_array = [sin, cos, sqrt]
W = rand(3, 10)

@btime func_array_eachrow!(V, $f_array) setup=(V=copy(W))
@btime func_array_eachrow3!(V, $f_array) setup=(V=copy(W))

  727.948 ns (15 allocations: 1.08 KiB)
  182.216 ns (6 allocations: 288 bytes)
2 Likes