c++ - 使用求解器 SparseLU 并出现错误 C2664
问题描述
我正在尝试使用 Eigen 来解决 C++ 中的稀疏线性系统,我也在使用 Microsoft Visual Studio 2017。
使用 Eigen 的代码行如下:
Eigen::VectorXd x(sizeM), b(sizeM);
Eigen::SparseMatrix<double> A(sizeM, sizeM);
Eigen::SparseLU<Eigen::SparseMatrix<double, Eigen::ColMajor>, Eigen::COLAMDOrdering<Eigen::Index> > solver;
// M is my coefficient array and B is my independent vector.
for (int i = 0; i < sizeM; i++)
{
b(i) = B[i];
}
A.reserve(Eigen::VectorXi::Constant(sizeM, 6));
for (int i = 0; i < sizeM; i++)
{
for (int j = 0; j < sizeM; j++)
{
if (M[i][j] != 0)
{
A.insert(i,j) = M[i][j];
}
}
}
A.makeCompressed();
// Compute the ordering permutation vector from the structural pattern of A.
solver.analyzePattern(A);
// Compute the numerical factorization .
solver.factorize(A);
//Use the factors to solve the linear system .
x = solver.solve(b);
代码错误是这样的:
c:\users\bruno\desktop\c++ apps\eigen\eigen\src\sparselu\sparselu.h(421): error C2664: 'void Eigen::COLAMDOrdering<Eigen::Index>::operator ()<Eigen::SparseMatrix<double,0,int>>(const MatrixType &,Eigen::PermutationMatrix<-1,-1,StorageIndex> &)': cannot convert argument 2 from 'Eigen::PermutationMatrix<-1,-1,int>' to 'Eigen::PermutationMatrix<-1,-1,StorageIndex> &'
1> with
1> [
1> MatrixType=Eigen::SparseMatrix<double,0,int>,
1> StorageIndex=Eigen::Index
1> ]
1> and
1> [
1> StorageIndex=Eigen::Index
1> ]
1>c:\users\bruno\desktop\c++ apps\eigen\eigen\src\sparselu\sparselu.h(412): note: while compiling class template member function 'void Eigen::SparseLU<Eigen::SparseMatrix<double,0,int>,Eigen::COLAMDOrdering<Eigen::Index>>::analyzePattern(const Eigen::SparseMatrix<double,0,int> &)'
1>c:\users\bruno\desktop\c++ apps\project1\project1\main.cpp(386): note: see reference to function template instantiation 'void Eigen::SparseLU<Eigen::SparseMatrix<double,0,int>,Eigen::COLAMDOrdering<Eigen::Index>>::analyzePattern(const Eigen::SparseMatrix<double,0,int> &)' being compiled
1>c:\users\bruno\desktop\c++ apps\project1\project1\main.cpp(367): note: see reference to class template instantiation 'Eigen::SparseLU<Eigen::SparseMatrix<double,0,int>,Eigen::COLAMDOrdering<Eigen::Index>>' being compiled
1>Done building project "Project1.vcxproj" -- FAILED.
我是 Eigen 和 C++ 的新手,所以我不完全确定问题出在哪里。
解决方案
简短的回答,写:
Eigen::SparseLU<Eigen::SparseMatrix<double, Eigen::ColMajor>, Eigen::COLAMDOrdering<int> > solver;
或者
Eigen::SparseLU<Eigen::SparseMatrix<double, Eigen::ColMajor, Eigen::Index>, Eigen::COLAMDOrdering<Eigen::Index> > solver;
whereEigen::ColMajor
在第一种情况下是可选的。
说明: 的最后一个模板参数Eigen::SparseMatrix
定义StorageIndex
,它必须与 相同Eigen::COLAMDOrdering
。默认情况下,这是int
(在大多数架构上为 32 位),而在 64 位架构上是 64 位Eigen::Index
的 typedef 。std::ptrdiff_t
推荐阅读
- android - 如何在 Android Studio WebView 上的 Android 7.0 或更高版本中打开图像选择器?
- c - C - 指向一个字符
- ios - 如何在运行时判断 iOS 应用程序是否正在通过 Crashlytics Beta 安装运行
- javascript - 解析 Promise 返回一个值
- node.js - Gulp、mocha 和 istanbul - 不捕获测试
- dart - 如何获得被测文件的覆盖率报告?
- json - tsconfig.json - 扩展 - 编译器不使用其他 tsconfig.json 文件
- scikit-learn - 高斯过程 RBF 内核使用什么各向异性方法?
- html - 使用 FOR XML PATH 在 SQL Server 中格式化颜色
- node.js - 在节点中使用 postcss 修改 css 字符串