Two quick observations:
This allocates for root[i,:]
and dest[j,:]
because slices copy in Julia. The former is also constant for the inner loop. So try instead
@views for i in axes(roots, 1)
tmp = roots[i, :]
for j in min(i, b):-1:1
dest[j+1, :] .+= temp .* dest[j, :]
end
end
You order of indices should be suboptimal since Julia Array
s are column-major, which means you should iterate the left-most index in the inner most loop. In your code you have written a function that takes Matrix
inputs but conceptually it is a operation on vectors. You kinda baked in a form of vectorization. I would consider this an antipattern in Julia. And note that this antipattern is responsible for the issue with the index order in this case.
So I would recommend you to either switch the indices on your matrices, i.e. transpose them effectively, or rewrite this function to take in Vectors instead and perform a broadcast over the different instances at a higher level in the code.
If you come from Matlab or similar: Note that in Julia you don’t need to vectorize computations for performances. A for loop has the same speed as broadcasting.
Also another comment on style: You don’t need to type annotate the function arguments. You can write this function completely generically, which makes it a lot easier to test difference number types you are also almost there already since you use
zero
and one
. Just drop the type annotations from the signature and use eltype(dest)
where you hardcoded the types in the body.