【问题标题】:Eigen C++: mapping a raw buffer for a Sparse MatrixEigen C++:为稀疏矩阵映射原始缓冲区
【发布时间】:2021-05-20 10:01:26
【问题描述】:

我在为 SparseMatrix 映射原始缓冲区时遇到了一些麻烦。

1 - 使用以下代码的非稀疏矩阵情况没有问题:

Eigen::MatrixXd DIFFXX;
new (&DIFFXX) Eigen::Map<Eigen::MatrixXd> (&coeff_diffxx(0,0), coeff_diffxx.xsize(), coeff_diffxx.ysize());

2- 当我想对 SparseMatrix 执行相同类型的操作时失败了

i) 这样:生成的 MAA 完全错误

Eigen::SparseMatrix<double, Eigen::RowMajor> MAA;
new (&MAA) Eigen::Map< Eigen::SparseMatrix<double, Eigen::RowMajor>> (xsize*ysize, xsize*ysize, 3*xsize*ysize, tridiagonalMatrix.outerIndexPtr(), tridiagonalMatrix.innerIndexPtr(), &m_a.m_data[1]);

ii) 或者这样:MAA 不是引用&amp;m_a.m_data[1],而是复制它!

Eigen::SparseMatrix<double, Eigen::RowMajor> MAA = Eigen::Map< Eigen::SparseMatrix<double, Eigen::RowMajor>> (xsize*ysize, xsize*ysize, 3*xsize*ysize, tridiagonalMatrix.outerIndexPtr(), tridiagonalMatrix.innerIndexPtr(), &m_a.m_data[1]);

我的目标是在 C++ 原始缓冲区上使用稀疏(实际上是三对角)矩阵执行操作。

感谢您的任何提示。 卢克

【问题讨论】:

  • 您可以尝试像这样创建地图吗? Eigen::Map&lt; Eigen::SparseMatrix&lt;double, Eigen::RowMajor&gt;&gt; MAA(xsize*ysize, xsize*ysize, 3*xsize*ysize, tridiagonalMatrix.outerIndexPtr(), tridiagonalMatrix.innerIndexPtr(), &amp;m_a.m_data[1]); 另外,你是故意指向m_a.m_data[1],还是应该指向m_a.m_data[0]
  • 嗨。我故意指向[1]。关于你的建议,我已经测试过了。确实,它运作良好。但之后,我无法执行一些矩阵运算,例如向 MAA 添加另一个 SparseMatrix,而仅使用 DenseMatrix 很容易做到这一点。
  • 你不应该对不同的类型使用placement-new。 Eigen::Map&lt;Eigen::MatrixXd&gt; 仅“有效”,因为这些类型共享一个共同的内存布局(但在重新分配存储时它可能会失败)。将两个稀疏矩阵相加的结果应存储在SparseMatrix 中,将结果存储在Map&lt;SparseMatrix&gt; 中几乎是不可能的。

标签: c++ buffer sparse-matrix eigen


【解决方案1】:

我终于用类MappedSparseMatrix做到了,这样:

int outerPtr[] = {0,2};
int innerPtr[] = {1,2};
double valsPtr[] = {2.5,3.6};
Eigen::MappedSparseMatrix<double, Eigen::ColMajor, int> mat(5,1,2,outerPtr,innerPtr,valsPtr);

谢谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-23
    • 1970-01-01
    • 2016-10-03
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多