I create 2d array where elements depend one ones with lower indexes. I.e. elements can be build up in natural order bit access to previous already created elements is necessary. My code has following structure (simplified):
fn create2d(n1: usize, n2: usize) -> Array2<MyItem> {
// create array with default values which will be all overwritten later
let mut arr = Array2::<MyItem>::default((n1, n2));
// (0,0)
arr[[0, 0]] = f0();
// 1st col
for i in 1..n1 {
arr[[i, 0]] = f1(arr[[i - 1, 0]]);
}
// 1st row
for j in 1..n2 {
arr[[0, j]] = f1(arr[[0, j - 1]]);
}
// inner 2d area
for i in 1..n1 {
for j in 1..n2 {
arr[[i, j]] = f2(arr[[i, j - 1]], arr[[i - 1, j]], arr[[i - 1, j - 1]]);
}
}
arr
}
Is there any more Rust and ndarray way how to write it? I can not find anything better than this C-like code.
Especially when extending this current way to 3d it would be too annoying.