我想使用 Eigen 3.3 乘以 bool 稀疏矩阵。
以下代码在 GCC 8.2 和 Clang 6 上编译:
#include <Eigen/Sparse>
#include <iostream>
int main()
{
Eigen::SparseMatrix<bool> A { 3, 3 };
Eigen::SparseMatrix<bool> B { 3, 1 };
A(0, 1) = true;
A(1, 0) = true;
A(2, 2) = true;
B(0, 0) = true;
B(0, 2) = true;
std::cout << A * B << std::endl;
return 0;
}
它打印出我期待的结果,[1 0 1]
,它在 Clang 6 上编译时没有任何警告。但是,在 GCC 8.2 上,我收到了以下烦人的警告:
/usr/include/eigen3/Eigen/src/SparseCore/ConservativeSparseSparseProduct.h:65:25: warning: ‘*’ in boolean context, suggest ‘&&’ instead [-Wint-in-bool-context]
values[i] = x * y;
有没有办法将 bool 乘法覆盖为逻辑或,&&
, 仅当乘以稀疏矩阵时?或者还有其他解决方法吗?我无法切换到密集矩阵/数组,因为我在实际代码中使用非常大的矩阵。即使有此警告,此操作的性能也非常好。
我已经试过了:
- 使用来自其 mercurial 存储库的 Eigen 开发版本:它会引发相同的警告;
-
(A * B).template cast<bool>
如本 stackoverflow question 中所述: 它提出了同样的警告; -
A && B
:它引发运行时错误(似乎它试图执行标量/系数明智的产品):Assertion aLhs.rows() == aRhs.rows() && aLhs.cols() == aRhs.cols()' failed.
.
最佳答案
我假设您使用 -Wall
作为 GCC 编译器标志。
您可以通过 -Wall -Wno-int-in-bool-context
关闭该特定警告
关于c++ - Eigen 3 SpaseMatrix<bool> - GCC 8.2 上的 SparseMatrix<bool> 乘法警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53345496/