首页 > 解决方案 > 使用求解器 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++ 的新手,所以我不完全确定问题出在哪里。

标签: c++c++11eigen

解决方案


简短的回答,写:

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


推荐阅读