问题
I have a problem with the following piece of code, after some research I have singled out the problem in a separate line, but not sure now how to solve it.
typedef double ComplexType;
typedef std::complex<ComplexType> Complex;
typedef Eigen::SparseMatrix<Complex, Eigen::ColMajor, long long> SparseMatrixT;
typedef Eigen::SparseVector<Complex, Eigen::ColMajor, long long> SparseVectorC;
typedef Eigen::SparseLU<SparseMatrixT, Eigen::COLAMDOrdering< long long>> SolverT;
SparseVectorC Solve(const Eigen::Ref<const SparseVectorC>& Rhs)
{
auto _Result = m_LU.solve(Rhs); //SolverT m_LU; defined and "prepared" elsewhere
SparseVectorC Result = _Result; // cause error C2512
return Result;
}
the error shows
\eigen\src\core\solve.h(125): error C2512: 'Eigen::internal::evaluator< Eigen::SparseVector< Complex,0, long long > >': no appropriate default constructor available
How can I get the Result in either Sparse of Dense vector (since it is not supposed to be sparse unlike the Rhs). The matrix size is (could be) huge, so extra copy would be unpleasant.
The variable _Result is apparently sparse, however neither assignment (with or without casting) to Sparse or Dense vector (using available method toDense() which is probably make a copy) doesn't work.
回答1:
Even though SparseLU::solve
accepts sparse matrices as the rhs, there is no special path for them because, as you also noticed, in most cases the result is dene anyway. So internally, if the rhs is a sparse matrix, it is internally converted to dense vectors prior to the actual solving. Numerical zeros are then dropped to output a sparse matrix. So in your case, better copy Rhs
to a VectorXcd
and let Solve
returns a dense VectorXcd
too.
To answer the compilation error, that's an issue in Eigen (Edit: fixed in changeset 80c2b4346260). If you still want to stick with sparse rhs and result, then you can workaround by replacing Ref<const SparseVectorC>
with Ref<const SparseMatrixT>
. There won't be any overhead compared to using SparseVectorC
.
来源:https://stackoverflow.com/questions/40434976/eigen-sparse-lu-solver-return-value