c++特征复制
c++ Eigen making copy?
设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
充当写入掩码。
相关文章:
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将元素从opencv矩阵复制到特征矩阵
- 避免复制重复使用的特征块
- 将特征向量复制到 C 数组
- 如何将数据从数组复制到C++特征矩阵或向量
- 将数组复制C++作为特征矩阵的向量
- 我可以通过向量附加特征矩阵而不复制任何数据
- 在特征中复制模板化函数参数
- c++特征复制
- 来自密集向量的 c++ 特征块对角矩阵,无需复制
- 特征在不复制的情况下对行重新排序
- 特征:沿着一个维度复制项目,而无用分配
- 特征库:为什么我的函数在不应该复制我的参数时复制我的参数?
- 在访问特征矩阵的行向量时复制或引用
- 从指针进行特征3类型的强制转换/复制(溢出uint8_t)
- 将上矩阵Xd复制到下矩阵Xd(特征3)C++库
- 存储指向特征向量"段"的指针而不复制?
- 为什么memcpy无法复制特征矩阵数据,但std::复制成功
- 如何复制特征矩阵
- 有没有可能在不复制的情况下使特征矩阵变平