Re: [eigen] Passing result of block() as non-const reference-to-matrix? |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/eigen Archives
]
- To: eigen@xxxxxxxxxxxxxxxxxxx
- Subject: Re: [eigen] Passing result of block() as non-const reference-to-matrix?
- From: Hauke Heibel <hauke.heibel@xxxxxxxxxxxxxx>
- Date: Fri, 9 Jul 2010 11:52:52 +0200
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=gamma; h=domainkey-signature:mime-version:received:received:in-reply-to :references:date:message-id:subject:from:to:content-type :content-transfer-encoding; bh=HJskgHDU/Z+Ni1nmo2DkvTGhS9Del9wtgqfopIGRLrY=; b=odOh8Dy3Q0d5z1gyegxVa+5ESdF6xtH8X2T5D7Zv8sTz6n+SFyOvjUXaiybGLbV2vY SYx1zG+rvb8WVh0HDiygoSRg4H4rsMdeqqOK1HF4Hmc09dqP5OIjhAwXRi7LQQyRuJRS 29gm2YKTR7HHyk5Y67O9Wn/QBTx506841numY=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=googlemail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=uafW90ekvZahhIIjc6GwJDVpSJgtS7zeQsHwHEjz2X/vVGQH0KlMTZwH2R1QC1swin OBMkfua34LIQB8vFOG/GGFEZzqXcedybP3wshKKAzFAMIJLJh5ncawWgrcptNywgOO1r cUDthW/Km7oLerTNyc3XsS1scTV4QguSeYMmw=
Hi Sidney,
that is expected since a matrix block is not a matrix. It is a special
matrix expression which shares operations defined in MatrixBase and
DenseBase with matrices. So you need to declare your function as a
template function like this
template <typename Derived>
void incrementBottomRight(Eigen::MatrixBase<Derived> & m)
{
m(m.rows() - 1, m.cols() - 1) += 1.0;
}
In case this is not an option you could probably do
void incrementBottomRight(Eigen::Block<MatrixXd> & m)
{
m(m.rows() - 1, m.cols() - 1) += 1.0;
}
but then the function will be limited to blocks only.
Regards,
Hauke
On Fri, Jul 9, 2010 at 11:28 AM, Sidney Cadot <sidney.cadot@xxxxxxxxx> wrote:
> Hello,
>
> Today I found, to my surprise, that passing the result of a Matrix::block() method to a function that takes a reference to an Eigen::Matrix doesn't seem to work. The program (which doesn't compile) demonstrates the problem.
>
> I know that it /is/ possible to pass the result of a Matrix::block() const call to a const reference to an Eigen::Matrix. Is it intentional that the non-const version of this does not work?
>
> Any help would be greatly appreciated.
>
> Regards, Sidney
>
> /////////////////////// code fragment starts here
>
> #include <Eigen/Core>
>
> void incrementBottomRight(Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> & m)
> {
> m(m.rows() - 1, m.cols() - 1) += 1.0;
> }
>
> int main()
> {
> Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> testMatrix(3, 3);
>
> // This works fine
> incrementBottomRight(testMatrix);
>
> // This gives a compiler error
> incrementBottomRight(testMatrix.block(0, 0, 2, 2));
>
> std::cout << testMatrix << std::endl;
>
> return 0;
> }
>
>
>
>