I have a very strange bug in my Rust code using ndarray. The following code runs with no error:
// ...other code before producing a lot of print on screen
//
let rank = 2;
let mut Q = Array2::<f64>::zeros((rank,rank));
// U is an Array2<F> where F is a struct that can call eval(&self,&f64) -> f64
for i in 0..rank {
println!("i = {}",i);
for j in 0..rank {
let v: f64 = U[[0,i]].eval(&1.0);
println!("i,j = {}-{} -- v = {}",i,j,v);
Q[[i,j]] = v;
}
}
but if I change the order of the indices for writing to a specific entry of the matrix Q, like for example
for i in 0..rank {
println!("i = {}",i);
for j in 0..rank {
let v: f64 = U[[0,i]].eval(&1.0);
println!("i,j = {}-{} -- v = {}",i,j,v);
Q[[j,i]] = v;
}
}
the executable runs forever, without printing anything on the screen (neither the output that is supposed to be produced before that point).
I've tried to replace the line
let v: f64 = U[[0,i]].eval(&1.0);
with
let v: f64 = f64::sin(1.0);
and now both version works fine.
Do you have any idea how to debug such kind of situation?