Registered Member
|
Hi I am experimenting the new c++0x function syntax so see if I can return an Eigen expression from a function. On gcc-4.5 I get the error below. Am I doing this right? I assume the function multi() would return the product expression and decltype(C) would become the product expression (dont know if the expression object is moved or not). C(0,0) would then trigger the evaluation of C.
template<typename Derived1, typename Derived2> auto multi(const MatrixBase<Derived1>& A, const MatrixBase<Derived2>& B) -> decltype(A*B*A) { return A*B*A; } BOOST_AUTO_TEST_CASE(EIGEN_TYPE_DECLTYPE){ auto C(multi(MatrixXd::Random(n, n),MatrixXd::Random(n, n))); // MatrixXd C(multi(MatrixXd::Random(n, n),MatrixXd::Random(n, n))); C(0,0); } ------------------------------------------------------------------ unit_test: ../lib/Eigen/src/Core/ProductBase.h:154: typename Eigen::MatrixBase<Derived>::CoeffReturnType Eigen::ProductBase<Derived, Lhs, Rhs>::coeff(typename Eigen::internal::traits<Eigen::ProductBase<Derived, _Lhs, _Rhs> >::Index, typename Eigen::internal::traits<Eigen::ProductBase<Derived, _Lhs, _Rhs> >::Index) const [with Derived = Eigen::GeneralProduct<Eigen::GeneralProduct<Eigen::CwiseNullaryOp<Eigen::internal::scalar_random_op<double>, Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001, 0, -0x00000000000000001, -0x00000000000000001> >, Eigen::CwiseNullaryOp<Eigen::internal::scalar_random_op<double>, Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001, 0, -0x00000000000000001, -0x00000000000000001> >, 5>, Eigen::CwiseNullaryOp<Eigen::internal::scalar_random_op<double>, Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001, 0, -0x00000000000000001, -0x00000000000000001> >, 5>, Lhs = Eigen::GeneralProduct<Eigen::CwiseNullaryOp<Eigen::internal::scalar_random_op<double>, Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001, 0, -0x00000000000000001, -0x00000000000000001> >, Eigen::CwiseNullaryOp<Eigen::internal::scalar_random_op<double>, Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001, 0, -0x00000000000000001, -0x00000000000000001> >, 5>, Rhs = Eigen::CwiseNullaryOp<Eigen::internal::scalar_random_op<double>, Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001, 0, -0x00000000000000001, -0x00000000000000001> >]: Assertion `this->rows() == 1 && this->cols() == 1' failed. unknown location(0): fatal error in "EIGEN_TYPE_DECLTYPE": signal: SIGABRT (application abort requested) |
Moderator
|
Calling operator(i,j) on a matrix product is forbidden because it is inherently extremely slow.
|
Registered Member
|
printing the whole matrix would trigger the error. Is it because the temporaries got destroyed at the time of exiting multi, and C handling dangling reference of 2 random matrices? Another question is, do you foresee any issue to return Eigen expression from this new syntax? |
Moderator
|
oh, indeed in your case the product expression references dead matrices. Beside, I don't see major issue with this auto syntax and Eigen expression, but I'd recommend to use that only for really specific cases.
|
Registered Member
|
Seems like a free function that takes const& and return an expression seems to be a bad idea because of the dangling reference issue. Any way to work around this? Maybe reference counting? I was planning to do this for BFGS like update in the optimisation process so I can reuse the update step in different solver. I fill try this route and let you know how that works out. Thanks again |
Moderator
|
You just have to take care of what you provide as input and how you use the output.
|
Registered users: bartoloni, Bing [Bot], Google [Bot], Yahoo [Bot]