I am still newish to Rust and currently trying to do some (hopefully) fast operations with the ndarray crate. Specifically I want to do an inplace addition using the output of a matrix-multiplication (element-wise) of the general form a += b * c.
I know that the AddAssign of the form a += &b should work. But will a += &(&b * &c) generate a new temporary array before addition? Do I have to resort to
Either way, the multiplication b * c (or &b * &c) will create a temporary array as its output.
If you find that the allocation/deallocation overhead of this is expensive, you can try something like this to allocate just a single temporary array outside of the loop:
I assumed that when using Zip and for_each the values will be scalars already and therfore the additional product variable to avoid allocations is not necessary anymore. Or am I misunderstanding something here?
Their alternative reuses the same variable to hold the product for each multiplication, instead of allocating and dropping a new variable each loop. It may have to reallocate in order to hold a larger value some times, but then that larger reallocation is also reused subsequent times through the loop.
Oh, sorry, I wasn’t familiar with ndarray::Zip and was thinking it was iterating over a collection of arrays.
&b * &c will definitely allocate a new array.
b * &c might not allocate, but it consumes b, so it might not be an option.
If you care about avoiding temporary allocations, your Zip version is good. Or if you are doing this calculation repeatedly, maybe you can do some version where you re-use the same temporary array.