0

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?

  • 1
    Please provide a minimal reproducible example. You're pretty close, but we need to know the value of `U` in order to help. – PitaJ Oct 12 '22 at 17:04
  • Run it in a debugger, break when it hangs and see where it stopped? – Max Klein Oct 12 '22 at 20:38
  • I found the bug. Using the transpose of Q instead of Q itself had as result an infinite loop in a numerical method in which the exit condition of the loop was never reached. – Massimiliano Martinelli Oct 13 '22 at 06:55

0 Answers0