特征库:返回一个函数中的矩阵块作为左值 [英] Eigen library: return a matrix block in a function as lvalue

查看:167
本文介绍了特征库:返回一个函数中的矩阵块作为左值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图返回一个矩阵块作为函数的左值。假设我的函数如下所示:

  Block< Derived> getBlock(MatrixXd& m,int i,int j,int row,int column)
{
return m.block(i,j,row,column);



$ b

事实证明,似乎C ++编译器理解block()运算符只给出临时值,因此编译器禁止将其作为左值返回。然而,在Eigen文档中有一些例子,我们可以使用Eigen作为左值(http://eigen.tuxfamily.org/dox/TutorialBlockOperations.html#TutorialBlockOperationsUsing),所以我想知道我们怎么做不到返回。

  a.block(0,0,2,3)= a.block(2,1,2,3) ); 

谢谢!

解决方案



我的基本解决方案是知道你想要的派生类型是什么该块是。在这种情况下:

 块< MatrixXd> getBlock(MatrixXd& m,int i,int j,int row,int column)
{
return m.block(i,j,row,column);
}

有趣的是,我注意到这个方法会返回引用矩阵m的内容默认。因此,如果我们这样做:

  MatrixXd m = MatrixXd :: Zero(10,10); 
Block< MatrixXd> myBlock = getBlock(m,1,1,3,3);
myBlock<< 1,0,0,
0,1,0,
0,0,1;

矩阵m中的内容也会被修改。但是,请注意,

  MatrixXd m = MatrixXd :: Zero(10,10); 
MatrixXd myBlock = getBlock(m,1,1,3,3);
myBlock<< 1,0,0,
0,1,0,
0,0,1;

将不起作用。我的理解是,一旦我们将该块转换为另一种类型,Eigen会在转换之前制作一份数据副本。


I am trying to return a block of a matrix as an lvalue of a function. Let's say my function looks like this:

Block<Derived> getBlock(MatrixXd & m, int i, int j, int row, int column)
{
    return m.block(i,j,row,column);
}

As it turns out, it seems that C++ compiler understands that block() operator gives only temporary value and so returning it as an lvalue is prohibited by the compiler. However, in Eigen documentation there is some example that we can use Eigen as an lvalue (http://eigen.tuxfamily.org/dox/TutorialBlockOperations.html#TutorialBlockOperationsUsing) so I am wondering how we couldn't do the same with function return.

a.block(0,0,2,3) = a.block(2,1,2,3);

Thank you!

解决方案

I want to put what I found myself so it might be helpful to someone else:

My basic solution is to know what derived type you want the block to be. In this case:

Block<MatrixXd> getBlock(MatrixXd & m, int i, int j, int row, int column)
{
    return m.block(i,j,row,column);
}

It is interesting to me to notice that this method will return the reference to the content of matrix m by default. So if we do:

MatrixXd m = MatrixXd::Zero(10,10);
Block<MatrixXd> myBlock = getBlock(m, 1, 1, 3, 3);
myBlock << 1, 0, 0, 
           0, 1, 0, 
           0, 0, 1;

The content in matrix m will be modified as well. Note that, however,

MatrixXd m = MatrixXd::Zero(10,10);
MatrixXd myBlock = getBlock(m, 1, 1, 3, 3);
myBlock << 1, 0, 0, 
           0, 1, 0, 
           0, 0, 1;

will not work. My understanding is that once we convert the block to another type Eigen makes a copy of the data before conversion.

这篇关于特征库:返回一个函数中的矩阵块作为左值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆