从Eigen :: SparseMatrix提取块/ROI,无需复制 [英] Extracting blocks/ROIs from Eigen::SparseMatrix without copying

查看:322
本文介绍了从Eigen :: SparseMatrix提取块/ROI,无需复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有什么好方法可以从Eigen :: SparseMatrix提取块/ROI? 更准确地说,我要提取的是内部向量.

I wonder is there any good way to extract blocks/ROIs from Eigen::SparseMatrix? More precisely, what I want to extract is inner vectors.

我想做的事情是这样的:

What I want to do is like:

typedef Eigen::SparseMatrix<double,Eigen::RowMajor> SpMat;
// Prepare some sparse matrix
SpMat spmat;
// Extract lines from it
const SpMat& row_i = spmat.innerVector(i);
const SpMat& row_j = spmat.innerVector(j);
// Some calculation with row_i and row_j...

如我所测试的,row_irow_j的数据是从spmat复制(!!)的. 但是,显然,它效率低下. 内部向量的数据(特别是row_i.m_data.m_valuesrow_i.m_data.m_indices)是原始数据(分别是spmat.m_data.m_valuesspmat.m_data.m_indices)的连续部分,因此应该有更聪明的方法.

As I tested, the data of row_i and row_j is copied (!!) from spmat. However, obviously, it is inefficient. The data (esp. row_i.m_data.m_values & row_i.m_data.m_indices) of inner vectors is continuous part of original data (spmat.m_data.m_values & spmat.m_data.m_indices resp.), so there should be smarter way.

我也许可以实现一种新方法来做到这一点,但这需要我艰难地研究源代码.所以我不想.

I may be able to implement new method to do this, but it require me a tough digging into the source code. So I don't want to.

任何帮助都将不胜感激! 预先感谢.

Any help is grateful! Thanks in advance.

推荐答案

您可以使用c ++ 11 auto关键字将row_irow_j声明为真正的读写表达式,也可以使用适当的类型:

You can either use the c++11 auto keyword to declare row_iand row_j as true read-write expressions, or use the proper type:

const auto row_i = spmap.innerVector(i); // C++11 version
const SpMat::InnerVectorReturnType row_i = spmap.innerVector(i); // C++98 version

此外,不是默认情况下SparseMatrix存储在main列中,因此内部向量"是一列.如果要引用行,则必须使用行为主的存储布局:

Moreover, not that by default a SparseMatrix is stored in column major, therefore an "inner-vector" is a column. If you want to reference rows, then you have to use a row-major storage layout:

typedef Eigen::SparseMatrix<double,RowMajor> SpMat;

这篇关于从Eigen :: SparseMatrix提取块/ROI,无需复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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