Eigen SparseMatrix 上三角到全矩阵

Eigen SparseMatrix upper triangular to full matrix

我有一个上三角SparseMatrix<double>。将其转换为完整稀疏矩阵的最有效方法是什么?

我目前已将此实施为 mat.transpose() + mat - diagonal(mat)

我想我可以使用

mat.selfadjointView<Eigen::Lower>() = mat.selfadjointView<Eigen::Upper>();

出于我不完全理解的原因,这会清除矩阵。

根据 documentation for Eigen::MatrixBase::selfadjointview,该函数已经从上三角部分或下三角部分创建了对称视图。

Matrix3i m = Matrix3i::Random();
cout << "Here is the matrix m:" << endl << m << endl;
cout << "Here is the symmetric matrix extracted from the upper part of m:" << endl
     << Matrix3i(m.selfadjointView<Upper>()) << endl;
cout << "Here is the symmetric matrix extracted from the lower part of m:" << endl
     << Matrix3i(m.selfadjointView<Lower>()) << endl;

输出:

Here is the matrix m:
 7  6 -3
-2  9  6
 6 -6 -5
Here is the symmetric matrix extracted from the upper part of m:
 7  6 -3
 6  9  6
-3  6 -5
Here is the symmetric matrix extracted from the lower part of m:
 7 -2  6
-2  9 -6
 6 -6 -5

假设你的矩阵是上三角矩阵,下面应该回答你的问题。

Matrix3i m = [] {
   Matrix3i tmp;
   tmp << 1, 2, 3, 0, 4, 5, 0, 0, 6;
   return tmp;
}();
cout << "Here is the matrix m:" << endl << m << endl;
cout << "Here is the symmetric matrix extracted from the upper part of m:" << endl
     << Matrix3i(m.selfadjointView<Upper>()) << endl;

输出:

Here is the matrix m:
 1  2  3
 0  4  5
 0  0  6
Here is the symmetric matrix extracted from the upper part of m:
 1  2  3
 2  4  5
 3  5  6