2

I am having a strange issue with using Eigen (Tuxfamily) in my software (in c++).

I am analyzing a 3D volume image by calculating for each pixel an Hessian matrix. The volume (approx 800x800x600) is divided in subvolumes and for each subvolume i sum up all the obtained matrices and then divide them by the amount to obtain the average (and then i do the same summing up all the averages and dividing by the number of subvolumes to obtain the average for the full volume).

The matrices are of type Matrix3d. The problem is, that for most of the sums (and obviously for the averages as well) i obtain something like :

Elements analyzed : 28215

Elements summed : 28215

Subvolume sum :

5143.76 | nan | -2778.05

5402.07 | 16011.9 | -inf

-2778.05 | -8716.86 | 7059.32

I sum them this way :

    for(int i = 0;i<(int)OuterVector.size();i++){
    AverageProduct+=OuterVector[i];
}

Due to the nature of the matrices i know that they should be symmetrical on the diagonal, so the correct value is calculated for some of them. Any idea on why the others might be failing? (and consider that it's always the same two position of the matrix giving me nan and -inf)

drHogan
  • 101
  • 1
  • 11
  • 3
    I don't have an answer, but there are C++ functions for determining if something is a `nan` or `inf`. You can call [`std::isnan()`](http://en.cppreference.com/w/cpp/numeric/math/isnan) and [`std::isinf()`](http://en.cppreference.com/w/cpp/numeric/math/isinf) in your debug code to figure out where they're coming in. Once you know that, you can probably work backwards from there to figure out which calculation is producing them for that element. Once you get one, they tend to cascade and cause all subsequent calculations to produce `nan` or `inf` results. – user1118321 Jan 10 '14 at 14:51
  • You can probably use your debugger to break on generation of NaN. See this answer for UNIX-ish platforms: http://stackoverflow.com/a/5394095/1401351 and this one for MSVC: http://stackoverflow.com/q/4454582/1401351 – Peter Jan 10 '14 at 15:21
  • It is hard to tell without knowing all the other stuff. Can you create a [Short, Self Contained, Compilable, Example](http://sscce.org/)? Detect the first broken matrix immediately after its creation (probably using the `isnan` and `isinf` from a previous comment) then print its input with sufficient precision and see whether that can be used to reproduce the issue. If so, also see whether you now already understand the source of the problem, in which case you might also answer your own question. Otherwise post the SSCCE and we'll have a closer look. – MvG Jan 10 '14 at 15:35
  • That's unrelated to your issue, but if OuterVector is an Eigen object, then you can do OuterVector.sum() instead of a manual for loop. – ggael Jan 10 '14 at 16:48

1 Answers1

3

Ok, using a mix of the suggestions you guys gave me in the comments, i tried a couple of random fixes and i solved the problem.

When i was creating the Eigen::Matrix3d object, i was not initializing the values, so somehow as soon as i was adding the first OuterVector[i] those two values were going wild (the (0,1) was going to nan and the (1,2) was going to inf). Strange that it was only happening only for those two specific values and in the same identical way every time.

So doing (at initialization time)

Matrix3d AverageProduct << 0,0,0,0,0,0,0,0,0;

was enough to fix it.

drHogan
  • 101
  • 1
  • 11