Re: [eigen] why does the result of the col member function behave so different?

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


Hi,

regarding: A.col(1)= A.col(0).square(); this is because there is no
square() method in the matrix world, otherwise it would mean A.col(0)
* A.col(0) with "*" the matrix product operator...

So you have to write:

A.col(1)= A.col(0).array().square();

The other issue,  Inp >> A.col(0);, is a C++ limitation. The problem
is that A.col(0) is a temporary object and it cannot be passed as a
non const reference to a function (or operator). So you have to name
it:

MatrixXd::ColXpr A0(A.col(0));
Inp >> A0;

Another ugly solution is to declare your operator >> with a const ref:

std::istream & operator >>(std::istream & s, const
Eigen::MatrixBase<Derived> & m)

and const_cast m... yes very ugly and not safe.


gael


On Fri, May 6, 2011 at 5:44 PM, Helmut Jarausch
<jarausch@xxxxxxxxxxxxxxxxxxx> wrote:
> #include <iostream>
> #include <Eigen/Core>
> using Eigen::MatrixXd;  using Eigen::VectorXd;
>
> #include <sstream>
>
> template<typename Derived>
> std::istream & operator >>(std::istream & s,
> Eigen::MatrixBase<Derived> & m)
> {
>    for (int i = 0; i < m.rows(); ++i)
>    for (int j = 0; j < m.cols(); j++)
>      s >> m(i,j);
>
>  return s;
> }
>
> int main() {
>  MatrixXd A(2,2);
>  std::istringstream Inp("1 2");
>  A << 1,2,3,4;
>  // Inp >> A.col(0);              // DOES NOT WORK - why?
>  // A.col(1)= A.col(0).square();  // DOES NOT WORK - why?
>  std::cout << A << std::endl;
> }



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