Re: [eigen] why does the result of the col member function behave so different? |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/eigen Archives
]
- To: eigen@xxxxxxxxxxxxxxxxxxx
- Subject: Re: [eigen] why does the result of the col member function behave so different?
- From: Gael Guennebaud <gael.guennebaud@xxxxxxxxx>
- Date: Fri, 6 May 2011 22:14:38 +0200
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type:content-transfer-encoding; bh=jmzUwWwQNmNt87+dsymWh5DaF7JNA6aUGpXqSXvKsC0=; b=JdQQvjvge8BI9J3iiZOXf6Zt+1LCuXLKudEwF0JVuJEUSjpaeiO0pAjLYFoma2tT9L PdoMTdm+AXMFP6CkGWD/ONGQ6C+FcmdALwNSM62LxpudFL9cYPszlIf8BhnffWJ2p0XS G1zyGOz8bTU1mQEyDWcRVpSQEbYsbLVLNnWKQ=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; b=m0T2US3uTblnvA1BJf8+zhKPGRItxAKwnkqAswvYGsRkSyf+bWtDcLqILvlU1COUxq hRGULE/igj4uyY2nrjH/W2gi3Fsv0Kg52NZ/6AGFt+B6xOBEIJ5JfS95sWCwmqDM+8LO X4nnFr8L8MD7xTKolPWthN0t9hspe/60RF5/w=
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;
> }