gpt4 book ai didi

具有 RowMajor 稀疏矩阵的 C++ Spectra

转载 作者:行者123 更新时间:2023-11-30 04:57:44 25 4
gpt4 key购买 nike

我正在尝试在我的 Linux 机器上使用 Spectra 3.5 库,而用于矩阵 vector 乘法的 SparseGenMatProd 包装器似乎仅在稀疏矩阵为 ColMajor 格式时才起作用。这是正常行为吗?如果是这样,我该如何修复它以采用 RowMajor 格式?我提供了一个基本示例,其中输出为“段错误(核心已转储)”。我浏览了其他几篇文章和文档,但似乎找不到答案。

#include <Eigen/Core>
#include <Eigen/SparseCore>
#include <GenEigsSolver.h>
#include <MatOp/SparseGenMatProd.h>
#include <iostream>

using namespace Spectra;

int main()
{
// A band matrix with 1 on the main diagonal, 2 on the below-main subdiagonal,
// and 3 on the above-main subdiagonal
const int n = 10;
Eigen::SparseMatrix<double, Eigen::RowMajor> M(n, n);
M.reserve(Eigen::VectorXi::Constant(n, 3));
for(int i = 0; i < n; i++)
{
M.insert(i, i) = 1.0;
if(i > 0)
M.insert(i - 1, i) = 3.0;
if(i < n - 1)
M.insert(i + 1, i) = 2.0;
}

// Construct matrix operation object using the wrapper class SparseGenMatProd
SparseGenMatProd<double> op(M);

// Construct eigen solver object, requesting the largest three eigenvalues
GenEigsSolver< double, LARGEST_MAGN, SparseGenMatProd<double> > eigs(&op, 3, 6);

// Initialize and compute
eigs.init();
int nconv = eigs.compute();

// Retrieve results
Eigen::VectorXcd evalues;
if(eigs.info() == SUCCESSFUL)
evalues = eigs.eigenvalues();

std::cout << *emphasized text*"Eigenvalues found:\n" << evalues << std::endl;

return 0;
}

如果将第 15 行更改为:

Eigen::SparseMatrix<double, Eigen::ColMajor> M(n, n);

它将按预期工作。

目前我正在解决这个问题并将我的矩阵转换为 ColMajor,但我想了解发生了什么。任何帮助深表感谢。

最佳答案

SparseGenMatProd 的 API 似乎具有误导性。看起来您必须通过第二个模板参数指定您正在处理行优先矩阵:

SparseGenMatProd<double,RowMajor> op(M);

否则 M 被隐式转换为一个临时的列主矩阵,然后由 op 的 const 引用存储,但是这个临时的就在那行代码之后就死了。

关于具有 RowMajor 稀疏矩阵的 C++ Spectra,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51972117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com