Re: [eigen] Re: mixing Matrix types in expressions containing reductions

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


Thanks! Sorry about that, you're right, I was defining EIGEN_NO_DEBUG.

On Sun, Oct 31, 2010 at 3:55 PM, Benoit Jacob <jacob.benoit.1@xxxxxxxxx> wrote:
Actually the bug is in your program :) And Eigen correctly errors out
with an assertion, so I guess that the only problem is that you
disabled assertions.

Compilable version:

#include <Eigen/Dense>
#include <iostream>


using namespace Eigen;
using namespace std;

int main() {
   MatrixXd Y              = MatrixXd::Random(2,3);
   VectorXd mean        = Y.colwise().mean();
   std::cout << mean << std::endl;
   MatrixXd variance    =
(Y.colwise().squaredNorm()/Y.rows()).array() -
mean.array()*mean.array();
   std::cout << variance << std::endl;
}


The mean vector is fine, but the program errors out with this assertion:

eigen/Eigen/src/Core/CwiseBinaryOp.h:144:
Eigen::CwiseBinaryOp<BinaryOp, Lhs, Rhs>::CwiseBinaryOp(const Lhs&,
const Rhs&, const BinaryOp&) [with BinaryOp =
Eigen::internal::scalar_difference_op<double>, Lhs =
Eigen::ArrayWrapper<Eigen::CwiseUnaryOp<Eigen::internal::scalar_quotient1_op<double>,
Eigen::PartialReduxExpr<Eigen::Matrix<double, -0x00000000000000001,
-0x00000000000000001, 0, -0x00000000000000001, -0x00000000000000001>,
Eigen::internal::member_squaredNorm<double>, 0> > >, Rhs =
Eigen::CwiseBinaryOp<Eigen::internal::scalar_product_op<double,
double>, Eigen::ArrayWrapper<Eigen::Matrix<double,
-0x00000000000000001, 1, 0, -0x00000000000000001, 1> >,
Eigen::ArrayWrapper<Eigen::Matrix<double, -0x00000000000000001, 1, 0,
-0x00000000000000001, 1> > >]: Assertion `lhs.rows() == rhs.rows() &&
lhs.cols() == rhs.cols()' failed.
Aborted (core dumped)


This just means that you are taking the difference of two arrays of
mismatched dimensions. In other words, this array _expression_:

   (Y.colwise().squaredNorm()/Y.rows()).array()

and this one:

   std::cout << variance << std::endl;

don't have the same size, because the first one is a single row, while
the second one is a single column (VectorXd is a column vector).

Benoit



2010/10/31 Patrick Sauer <pat05.mlst@xxxxxxxxx>:
> Using RowVectorXd also solves the problem.
>
> Patrick
>
> On Sun, Oct 31, 2010 at 3:29 PM, Benoit Jacob <jacob.benoit.1@xxxxxxxxx>
> wrote:
>>
>> 2010/10/31 Patrick Sauer <pat05.mlst@xxxxxxxxx>:
>> > Hi,
>> >
>> > 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.
>>
>> Looks like you found a bug.
>>
>> What happens with "RowVectorXd mean" ?
>>
>> Benoit
>>
>>
>> > 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?
>> >
>> > Thanks.
>> >
>> > Kind regards,
>> >
>> > Patrick
>> >
>>
>>
>
>





Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/