c++特征复制

c++ Eigen making copy?

本文关键字:复制 特征 c++      更新时间:2023-10-16

设A为对称矩阵,设v为向量。我从A中提取一个从j开始的n列的块,并使用将其乘以v

VectorXd a;
a = A.middleCols(j,n).selfadjointView<Lower>() * v // does not compile

由于这不编译,而这个

a = MatrixXd(A.middleCols(j,n).selfadjointView<Lower>()) * v

是的,我想知道第二个版本是否复制了

A.middleCols(j,n).selfadjointView<Lower>()  

或者直接执行计算?

谢谢你的提示。

编辑:我怀疑问题与参数类型有关,因为我得到了错误:

invalid argument type 'typename ConstSelfAdjointViewReturnType.... to unary expression' 

事实上,A是通过使用的const引用传递的函数的自变量

const MatrixXd& A 
const Ref<const MatrixXd>& A

这里有一个例子:

// this version doesn't compile
MatrixXd returnSymmetricMatrix(const MatrixXd& A, const VectorXd& v, const MatrixXd& B){
// B is a symmetric matrix
VectorXd a;
a = A.middleCols(3, 4).selfadjointView<Lower>() * v;
MatrixXd M(code_fill(){...}); 
// code_fill is the function filling the lower triangular part of a symmetric matrix
M.block(1, 2, 3, 4).triangularView<Lower>() += B.selfadjointView<Lower>();
return M;
}
// this version compiles
MatrixXd returnSymmetricMatrix(const MatrixXd& A, const VectorXd& v, const MatrixXd& B){
// B is a symmetric matrix
VectorXd a;
a = MatrixXd(A.middleCols(3, 4).selfadjointView<Lower>()) * v;
MatrixXd M(code_fill(){...}); 
// code_fill is the function filling the lower triangular part of a symmetric matrix
Matrix(M.block(1, 2, 3, 4).triangularView<Lower>()) += B.selfadjointView<Lower>();
return M;
}

EDIT2关于我最初的问题和我在编辑部分添加的示例,我对复制有点困惑。正如我所理解的工作版本和非工作版本之间的区别,线

Matrix(M.block(1, 2, 3, 4).triangularView<Lower>()) += B.selfadjointView<Lower>();  

之所以有效,是因为它的lhs告诉Eigen M.block(1,2,3,4).trigularView()实际上是一个矩阵,而不是对矩阵的引用。否则,运算符+=会出现一个错误,即该运算符没有为.block()重载。所以我最初的问题是,矩阵(…)是否只告诉它是一个矩阵来启用计算,还是复制。。。变成矩阵?谢谢

以下表达式:

A.middleCols(j,n).selfadjointView<Lower>() 

不创建任何副本。

另一方面,为了避免产品的临时结果,您可以添加.noalias():

a.noalias() = M.middleCols(j,n).selfadjointView<Lower>() * v;

这只需要立即分配密集产品,以允许类似以下代码:

a = M * a;

按照预期行事。

编辑:

关于您的编译问题,以下编译良好:

#include <Eigen/Dense>
using namespace Eigen;
int main()
{
int n = 10;
MatrixXd M = MatrixXd::Random(n,2*n);
VectorXd v = VectorXd::Random(n);
VectorXd a;
a.noalias() = M.middleCols(2,n).selfadjointView<Upper>() * v;
const Ref<const MatrixXd>& A = M;
a.noalias() = A.middleCols(2,n).selfadjointView<Upper>() * v;
}

EDIT2

以下行:

MatrixXd(M.block(1, 2, 3, 4).triangularView<Lower>()) += B.selfadjointView<Lower>();

毫无意义,因为您正在创建一个要分配给的临时副本。回想一下,这里MatrixXd(whatever)调用Eigen::Matrix构造函数。同样,将自伴视图指定给三角形视图也没有意义。我甚至想不出什么是合理的行为。如果你只想更新下三角部分,那么做:

M.block(1, 2, 3, 4).triangularView<Lower>() += B;

在这种情况下,triangularView充当写入掩码。