【发布时间】:2019-12-16 23:32:15
【问题描述】:
我正在努力将稀疏矩阵支持添加到开源数学库中,并且不希望 Dense 和 Sparse 矩阵类型有重复的函数。
以下示例显示了一个add 函数。一个具有两个功能的工作示例,然后两次尝试失败。下面提供了代码示例的神螺栓链接。
我查看了有关编写采用 Eigen 类型的函数的 Eigen 文档,但他们使用 Eigen::EigenBase 的答案不起作用,因为 MatrixBase 和 SparseMatrixBase 都有特定的可用方法,这些方法在 EigenBase 中不存在
https://eigen.tuxfamily.org/dox/TopicFunctionTakingEigenTypes.html
我们使用 C++14,非常感谢您的帮助和您的宝贵时间!!
#include <Eigen/Core>
#include <Eigen/Sparse>
#include <iostream>
// Sparse matrix helper
using triplet_d = Eigen::Triplet<double>;
using sparse_mat_d = Eigen::SparseMatrix<double>;
std::vector<triplet_d> tripletList;
// Returns plain object
template <typename Derived>
using eigen_return_t = typename Derived::PlainObject;
// Below two are the generics that work
template <class Derived>
eigen_return_t<Derived> add(const Eigen::MatrixBase<Derived>& A) {
return A + A;
}
template <class Derived>
eigen_return_t<Derived> add(const Eigen::SparseMatrixBase<Derived>& A) {
return A + A;
}
int main()
{
// Fill up the sparse and dense matrices
tripletList.reserve(4);
tripletList.push_back(triplet_d(0, 0, 1));
tripletList.push_back(triplet_d(0, 1, 2));
tripletList.push_back(triplet_d(1, 0, 3));
tripletList.push_back(triplet_d(1, 1, 4));
sparse_mat_d mat(2, 2);
mat.setFromTriplets(tripletList.begin(), tripletList.end());
Eigen::Matrix<double, -1, -1> v(2, 2);
v << 1, 2, 3, 4;
// Works fine
sparse_mat_d output = add(mat * mat);
std::cout << output;
// Works fine
Eigen::Matrix<double, -1, -1> output2 = add(v * v);
std::cout << output2;
}
我想要一个同时接受稀疏矩阵和稠密矩阵的函数,而不是两个 add 函数,但是下面的尝试没有成功。
模板模板类型
我的尝试显然很糟糕,但是用模板模板类型替换上面的两个 add 函数会导致模棱两可的基类错误。
template <template <class> class Container, class Derived>
Container<Derived> add(const Container<Derived>& A) {
return A + A;
}
错误:
<source>: In function 'int main()':
<source>:35:38: error: no matching function for call to 'add(const Eigen::Product<Eigen::SparseMatrix<double, 0, int>, Eigen::SparseMatrix<double, 0, int>, 2>)'
35 | sparse_mat_d output = add(mat * mat);
| ^
<source>:20:20: note: candidate: 'template<template<class> class Container, class Derived> Container<Derived> add(const Container<Derived>&)'
20 | Container<Derived> add(const Container<Derived>& A) {
| ^~~
<source>:20:20: note: template argument deduction/substitution failed:
<source>:35:38: note: 'const Container<Derived>' is an ambiguous base class of 'const Eigen::Product<Eigen::SparseMatrix<double, 0, int>, Eigen::SparseMatrix<double, 0, int>, 2>'
35 | sparse_mat_d output = add(mat * mat);
| ^
<source>:40:52: error: no matching function for call to 'add(const Eigen::Product<Eigen::Matrix<double, -1, -1>, Eigen::Matrix<double, -1, -1>, 0>)'
40 | Eigen::Matrix<double, -1, -1> output2 = add(v * v);
| ^
<source>:20:20: note: candidate: 'template<template<class> class Container, class Derived> Container<Derived> add(const Container<Derived>&)'
20 | Container<Derived> add(const Container<Derived>& A) {
| ^~~
<source>:20:20: note: template argument deduction/substitution failed:
<source>:40:52: note: 'const Container<Derived>' is an ambiguous base class of 'const Eigen::Product<Eigen::Matrix<double, -1, -1>, Eigen::Matrix<double, -1, -1>, 0>'
40 | Eigen::Matrix<double, -1, -1> output2 = add(v * v);
| ^
我相信这是同一个钻石继承问题:
https://www.fluentcpp.com/2017/05/19/crtp-helper/
使用 std::conditional_t
下面尝试使用conditional_t来推断正确的输入类型
#include <Eigen/Core>
#include <Eigen/Sparse>
#include <iostream>
// Sparse matrix helper
using triplet_d = Eigen::Triplet<double>;
using sparse_mat_d = Eigen::SparseMatrix<double>;
std::vector<triplet_d> tripletList;
// Returns plain object
template <typename Derived>
using eigen_return_t = typename Derived::PlainObject;
// Check it Object inherits from DenseBase
template<typename Derived>
using is_dense_matrix_expression = std::is_base_of<Eigen::DenseBase<std::decay_t<Derived>>, std::decay_t<Derived>>;
// Check it Object inherits from EigenBase
template<typename Derived>
using is_eigen_expression = std::is_base_of<Eigen::EigenBase<std::decay_t<Derived>>, std::decay_t<Derived>>;
// Alias to deduce if input should be Dense or Sparse matrix
template <typename Derived>
using eigen_matrix = typename std::conditional_t<is_dense_matrix_expression<Derived>::value,
typename Eigen::MatrixBase<Derived>, typename Eigen::SparseMatrixBase<Derived>>;
template <typename Derived>
eigen_return_t<Derived> add(const eigen_matrix<Derived>& A) {
return A + A;
}
int main()
{
tripletList.reserve(4);
tripletList.push_back(triplet_d(0, 0, 1));
tripletList.push_back(triplet_d(0, 1, 2));
tripletList.push_back(triplet_d(1, 0, 3));
tripletList.push_back(triplet_d(1, 1, 4));
sparse_mat_d mat(2, 2);
mat.setFromTriplets(tripletList.begin(), tripletList.end());
sparse_mat_d output = add(mat * mat);
std::cout << output;
Eigen::Matrix<double, -1, -1> v(2, 2);
v << 1, 2, 3, 4;
Eigen::Matrix<double, -1, -1> output2 = add(v * v);
std::cout << output2;
}
这会引发错误
<source>: In function 'int main()':
<source>:94:38: error: no matching function for call to 'add(const Eigen::Product<Eigen::SparseMatrix<double, 0, int>, Eigen::SparseMatrix<double, 0, int>, 2>)'
94 | sparse_mat_d output = add(mat * mat);
| ^
<source>:79:25: note: candidate: 'template<class Derived> eigen_return_t<Derived> add(eigen_matrix<Derived>&)'
79 | eigen_return_t<Derived> add(const eigen_matrix<Derived>& A) {
| ^~~
<source>:79:25: note: template argument deduction/substitution failed:
<source>:94:38: note: couldn't deduce template parameter 'Derived'
94 | sparse_mat_d output = add(mat * mat);
| ^
<source>:99:52: error: no matching function for call to 'add(const Eigen::Product<Eigen::Matrix<double, -1, -1>, Eigen::Matrix<double, -1, -1>, 0>)'
99 | Eigen::Matrix<double, -1, -1> output2 = add(v * v);
| ^
<source>:79:25: note: candidate: 'template<class Derived> eigen_return_t<Derived> add(eigen_matrix<Derived>&)'
79 | eigen_return_t<Derived> add(const eigen_matrix<Derived>& A) {
| ^~~
<source>:79:25: note: template argument deduction/substitution failed:
<source>:99:52: note: couldn't deduce template parameter 'Derived'
99 | Eigen::Matrix<double, -1, -1> output2 = add(v * v);
这似乎是因为无法像此链接那样推断出依赖类型的依赖参数。
Godbolt 示例
下面的神螺栓可以玩上面的所有实例
有没有办法只拥有一个功能而不是两个?我们有很多函数可以同时支持稀疏矩阵和稠密矩阵,因此避免代码重复会很好。
编辑:可能的答案
@Max Langhof 建议使用
template <class Mat>
auto add(const Mat& A) {
return A + A;
}
auto 关键字对 Eigen 来说有点危险
https://eigen.tuxfamily.org/dox/TopicPitfalls.html
但是
template <class Mat>
typename Mat::PlainObject add(const Mat& A) {
return A + A;
}
有效,但我不完全确定为什么在这种情况下返回普通对象有效
编辑编辑
有几个人提到了auto 关键字的使用。遗憾的是,Eigen 不能很好地与 auto 配合使用,正如 C++11 的第二个中所引用的和下面链接中的 auto 一样
https://eigen.tuxfamily.org/dox/TopicPitfalls.html
在某些情况下可以使用 auto ,但我想看看是否有一个通用的 auto'ish 方式来抱怨 Eigen 的模板返回类型
对于带有自动的段错误示例,您可以尝试将添加替换为
template <typename T1>
auto add(const T1& A)
{
return ((A+A).eval()).transpose();
}
【问题讨论】:
-
你使用的是什么 C++ 版本?
-
C++14,谢谢,我会在帖子里补充的!
-
你不能只做
template <class Mat> auto add(const Mat& A) { return A + A; }的任何特殊原因(可能有一些 SFINAE 将其限制为特征矩阵)?或者这是否会以某种方式遇到表达式模板的问题? -
...大声笑。让我试试这个!我唯一担心的是
auto关键字,因为 Eigen 说不要在他们的文档中使用它。 eigen.tuxfamily.org/dox/TopicPitfalls.html -
啊!因此,如果我返回的 Mat::PlainObject 确实有效!虽然我需要检查是否有任何问题,因为这对我来说并不完全有意义。让我探索一下这是否会导致任何奇怪的错误,如果不是,我会接受这个作为答案!
标签: c++ templates eigen multiple-inheritance