本征稀疏LU解算器返回值 [英] Eigen Sparse LU solver return value

查看:110
本文介绍了本征稀疏LU解算器返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对以下代码有问题,经过一些研究,我在单独的一行中指出了问题,但现在不确定如何解决。

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;
}

错误显示

特征\src\core\solve.h(125):错误C2512:
'Eigen :: internal :: evaluator< Eigen :: SparseVector< Complex,0,long long>>':没有可用的
合适的默认构造函数

\eigen\src\core\solve.h(125): error C2512: 'Eigen::internal::evaluator< Eigen::SparseVector< Complex,0, long long > >': no appropriate default constructor available

如何在任一方法中获取结果稀疏向量(因为它不像Rhs那样稀疏)。

变量_Result显然是稀疏的,但是既没有赋值(有或没有强制转换)给稀疏或密集向量(使用可用的方法toDense)。 ()可能是副本)无效。

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.

推荐答案

即使 SparseLU :: solve 接受稀疏矩阵作为rhs ,因此它们没有特殊的路径,因为正如您还注意到的,无论如何,大多数情况下结果都是dene。因此在内部,如果rhs是稀疏矩阵,则在实际求解之前会在内部将其转换为密集向量。然后将数字零丢弃以输出稀疏矩阵。因此,根据您的情况,最好将 Rhs 复制到 VectorXcd 并让 Solve 也返回密集的 VectorXcd

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.

要回答编译错误,这是Eigen(修改:已在变更集80c2b4346260中修复)。如果您仍然希望保留稀疏的rhs和结果,那么可以通过将 Ref< const SparseVectorC> 替换为 Ref< const SparseMatrixT> 。与使用 SparseVectorC 相比,不会有任何开销。

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.

这篇关于本征稀疏LU解算器返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆