使用特征库执行sparseLU并显示L& ü?

时间:2014-08-22 17:34:28

标签: c++ matrix eigen eigen3

我是 Eigen 的新手,我正在使用稀疏LU问题。 我发现如果我创建一个向量b(n), Eigen 可以计算出Ax = b方程的x(n)。

问题:

  1. 如何展示L& U,这是原始矩阵A?

  2. 的分解结果
  3. 如何在 Eigen 中插入非零值?现在我只是测试一些小的稀疏矩阵,所以我逐个插入非零,但如果我有一个大型矩阵,我怎么能在我的程序中输入矩阵?

2 个答案:

答案 0 :(得分:1)

我意识到很久以前就问过这个问题了。显然,请参考Eigen documentation

  

矩阵L的表达式,内部存储为超级节点此表达式可用的唯一操作是三角形求解

因此无法将其实际转换为实际的稀疏矩阵来显示它。 Eigen::FullPivLU执行密集分解,对我们没用。在大型稀疏矩阵上使用它时,我们会在尝试将其转换为密集时快速耗尽内存,计算分解所需的时间将增加几个数量级。

另一种解决方案是使用Suite Sparse中的CSparse库:

extern "C" { // we are in C++ now, since you are using Eigen
#include <csparse/cs.h>
}

const cs *p_matrix = ...; // perhaps possible to use Eigen::internal::viewAsCholmod()

css *p_symbolic_decomposition;
csn *p_factor;

p_symbolic_decomposition = cs_sqr(2, p_matrix, 0); // 1 = ordering A + AT, 2 = ATA
p_factor = cs_lu(p_matrix, m_p_symbolic_decomposition, 1.0); // tol = 1.0 for ATA ordering, or use A + AT with a small tol if the matrix has amostly symmetric nonzero pattern and large enough entries on its diagonal
// calculate ordering, symbolic decomposition and numerical decomposition

cs *L = p_factor->L, *U = p_factor->U;
// there they are (perhaps can use Eigen::internal::viewAsEigen())

cs_sfree(p_symbolic_decomposition); cs_nfree(p_factor);
// clean up (deletes the L and U matrices)

请注意,虽然这不像某些特征函数那样使用expliit向量化,但它仍然相当快。 CSparse也非常紧凑,它只是一个标题和大约30个.c文件,没有外部依赖。易于合并到任何C ++项目中。没有必要实际包含所有Suite Sparse。

答案 1 :(得分:-1)

如果您将Eigen::FullPivLU::matrixLU()用于原始矩阵,您将收到LU分解矩阵。要分别显示L和U,您可以使用方法triangularView< mode >。在Eigen wiki中,您可以找到good example。将非零值插入矩阵取决于数字,这是你不想放的。 Eigen具有方便的语法,因此您可以轻松地在循环中插入值:

for(int i=0;i<size;i++)
  {
  for(int j=size;j>someNumber;j--)
    {
    matrix(i,j)=yourClass.getNextNumber();
    }
  }