I'm trying to compute the variance of data vectors contained in the rows of an Eigen3 matrix, i.e.:
MatrixXd Y = MatrixXd::Random(2,3); VectorXd mean = Y.colwise().mean(); MatrixXd variance = (Y.colwise().squaredNorm()/Y.rows()).array() - mean.array()*mean.array();
Strangely, with this code the variance vector contains only the variance over the first column. However, changing the "VectorXd mean" declaration to "MatrixXd mean" solves this problem and the code yields the correct result.
Is this behaviour expected? Could the compiler provide the user with a hint here, or does the user need to remember to use equivalent types in expressions containing reduction operations?