找到稀疏矩阵(特征)最大值的有效方法

Efficient way to find maximum value of Sparse matrix (Eigen)

本文关键字:最大值 有效 方法 特征      更新时间:2023-10-16

我想知道是否有可能以有效的方式在稀疏矩阵中找到最大/最小系数。

似乎 minCoeff()/maxCoeff() 函数没有为稀疏矩阵实现,这有点奇怪。

我在这里找到了这个答案,但我无法弄清楚。

using Tmp = typename remove_cv_ref<decltype(matrix)>::type;
if constexpr(std::is_base_of<Eigen::SparseMatrixBase<Tmp>, Tmp>::value)
max = Eigen::Map<const Vector>(matrix.valuePtr(), matrix.size()).maxCoeff();
else
max = matrix.maxCoeff();

编辑:这是我的尝试,我不确定效率。

typedef Eigen::SparseMatrix<int, Eigen::RowMajor> SRI;
int maxCoeff(const SparseMatrix<int, RowMajor> &A)
{
size_t row = A.rows();
size_t col = A.cols();
int max_value = -10000000;
for (size_t k = 0; k < row; k++)
{
for (SRI::InnerIterator it(A, k); it; ++it)
{
if (it.value() > max_value)
max_value = it.value();
}
}
return max_value;
}

这些函数并不容易获得,因为是否应考虑隐式零可能不明确。例如,如果所有非零都是负数,maxCoeff是否应返回0

如果您只想考虑显式存储的元素,并且您的稀疏矩阵处于压缩模式,那么您可以编写:

auto max = matrix.coeffs().maxCoeff();

coeff方法等同于RHertel的答案。

尝试使用此模板化函数来获取Eigen::SparseMatrix的最大非零值:

template <class T>
T sparseMax (SparseMatrix<T>& mat) { 
return Map<Matrix<T, Dynamic, 1> >(mat.valuePtr(), mat.nonZeros()).maxCoeff();
}

这里的原理在于将稀疏矩阵的非零元素映射到一个向量(一维Eigen::Matrix),然后用.maxCoeff()从中提取最大值。这应该是确定矩阵中最大值的非常有效的方法。 请注意,需要压缩矩阵才能使此方法正常工作。

在代码中,函数sparseMax()可以像这样调用:

if (!mat.isCompressed()) mat.makeCompressed();
auto max = sparseMax(mat);

其中matEigen::SparseMatrix.

因此,链接中的答案几乎是正确的,但.size()应该替换为.nonZeros(),并且它无法确保矩阵被压缩。如果您知道您正在处理稀疏矩阵,则不需要链接答案中提到的if/else构造。


编辑

上述解决方案适用于具有默认列主存储顺序的稀疏矩阵。在RowMajor存储的情况下,可以将模板更改为

template <class T>
T sparseMax_RM (SparseMatrix<T,RowMajor>& mat) { 
return Map<Matrix<T, Dynamic, 1> >(mat.valuePtr(), mat.nonZeros()).maxCoeff();
}

更健壮的方法是改用成员函数.coeffs(),如@ggael所指出的。.coeffs()函数适用于两种类型的压缩 SparseMatrix 类,即 RowMajor 和 ColMajor 存储顺序。