Whenever there is a NaN value inside an array, exp() will return an Inf instead of NaN. Example bellow: #include <iostream> #include <limits> #include <Eigen/Dense> int main(){ double nan = std::numeric_limits<double>::quiet_NaN(); Eigen::MatrixXd m1(3,3); m1 << 10, 1.5, 3.2, nan, 10, 4.1, 0.1, 4.1, 10; Eigen::MatrixXd m2(3,3); Eigen::ArrayXXd a1(3,3); //Has bug m2 = m1.array().exp().matrix(); std::cout << m2 << std::endl << std::endl; //Has bug a1 = m1.array().exp(); std::cout << a1 << std::endl << std::endl; //Output: //22026.5 4.48169 24.5325 //inf 22026.5 60.3403 //1.10517 60.3403 22026.5 // //22026.5 4.48169 24.5325 //inf 22026.5 60.3403 //1.10517 60.3403 22026.5 //Now, some other calls, just for comparison: a1 = m1.array(); std::cout << a1 << std::endl << std::endl; for (int i; i < m1.size(); i++) std::cout << std::exp(m1(i)) << std::endl; std::cout << std::endl; m2 = m1.array().log().matrix(); std::cout << m2 << std::endl << std::endl; a1 = m1.array().log(); std::cout << a1 << std::endl << std::endl; }
It seems the problem is that pexp in Eigen/src/Core/arch/SSE/MathFunctions.h does not check for NaNs. We could simply return pmax(x, pexp(x)); because exp(x) is always bigger than x, and max(NaN, x) is NaN (beware that (max(x, NaN)==x) !) Or we document that NaNs result in undefined values.
Good idea to return return pmax(x, pexp(x));, the overhead is negligible.
Fixed: https://bitbucket.org/eigen/eigen/commits/f20b295aa91e/ Changeset: f20b295aa91e User: ggael Date: 2014-10-20 09:38:51+00:00 Summary: Fix bug 859: pexp(NaN) returned Inf instead of NaN 3.2 backport: https://bitbucket.org/eigen/eigen/commits/ff389140fde7/
-- GitLab Migration Automatic Message -- This bug has been migrated to gitlab.com's GitLab instance and has been closed from further activity. You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.com/libeigen/eigen/issues/859.