I want to replicate the behaviour of this nested for-loop using ndarray slices:
use ndarray::prelude::*;
// [ ... ]
// self.hx and self.ex are Array3<f32> of the same shape
let [xs, ys, zs] = self.hx.shape();
for x in 0..*xs {
for y in 1..*ys {
for z in 0..*zs {
self.hx[[x, y, z]] -= self.ez[[x, y, z]] - self.ez[[x, y - 1, z]]
}
}
}
First, you can't have any expression on the left side of assignments, so you have to create the temporary slice. Then, you can't subtract one immutable view from another immutable view because there is nowhere to store the result. And finally, you need to add & because ndarray has only implemented the assignments for references.
I need this to be as fast as possible. This means iterating through both matrices once, using vector instructions where possible. Is there a library in the rust ecosystem that would help me with this?
That’s not correct. I don't know ndarray, but there are no restrictions on the expression used on the left of an assignment operator other than that it is a place expression (or a few other things, for destructuring assignment). There might be other reasons like borrow checking errors to organize the code differently, but there is no restriction specifically on the complexity of the left side.
It's not a place expression. In other cases you can get a place expression out of a value expression by dereferencing, but that's not how ndarray implemented assignments.
Edit: I forgot you can make it a place expression but it's kind of ugly:
Ah, right, I see what is going on (the returned value implements AddAssign but you can't just then start mutating it as an owned value). Sorry for the confusion. I wouldn’t have put it that way but my reply was at least equally confusing.