Eigen3 和内置类型互兼容
大家好。我遇到了编写可以同时处理 Eigen3 类型(矩阵和数组)和内置类型的例程的问题。我可以用一个例子来最好地解释这一点:假设我有一个Meter<Type>
模板类,它能够在运行时收集统计信息。
Type 类应支持以下运算符:
operator=(Scalar)
operator=(Type)
operator+(Type)
operator-(Type)
operator*(Type)
operator/(Type)
operator*(Scalar)
operator/(Scalar)
Eigen3
types 为所有这些运算符提供了两个例外:首先,如果是 的某个子类,则operator*(Type)
表示点推导;如果是Type
的某个子类,则Eigen::MatrixBase
表示系数乘积。我可以很容易地解决这个问题;其次,两者都没有实现确保正确初始化为零的要求。Type
Eigen::ArrayBase
operator=(Scalar)
我尝试实现以下仿函数类来帮助我处理区别,但我无法让它们工作:
一些结构来处理内置类型和Eigen3
类型之间的区别:
template < class _Type > struct is_scalar : true_type {
using Scalar = _Type;
using Type = _Type;
static constexpr bool value = true;
};
template < class _Matrix >
struct is_scalar<Eigen::MatrixBase<_Matrix>> : false_type {
using Scalar = typename Matrix::Scalar;
static constexpr bool value = false;
};
template < class _Array >
struct is_scalar<Eigen::ArrayBase<_Array>> : false_type {
using Scalar = typename Array::Scalar;
static constexpr bool value = false;
};
函数实现本身
template < class Scalar, bool is_scalar = Math::is_scalar<Scalar>::value >
struct set_const_impl;
template < class Scalar >
struct set_const_impl< Scalar, true > {
static const void run(Scalar &_x, Scalar _y) noexcept { _x = _y; }
};
template < class EigenType >
struct set_const_impl<EigenType, false> {
template < class Scalar >
static const void run(Eigen::EigenBase<EigenType> &_x, Scalar _y) noexcept {
_x.derived().setConstant(_y);
}
};
template < class Type, class Scalar > void set_const(Type &_x, Scalar _y) noexcept {
set_const_impl<Type>::run(_x, _y);
}
template < class Type > void set_zero(Type &_x) noexcept {
set_const_impl<Type>::run(_x, 0);
}
专用版本set_const_impl<EigenType>
永远不会被实例化。例如,如果我打电话
Eigen::Matrix<double, 3, 1> m1;
set_zero(m1);
我让编译器0
在线投诉
set_const_impl<Type>::run(_x, 0);
说它0
不能隐式转换为Eigen::Matrix<double, 3, 1>
,这意味着它选择了set_const_impl<Scalar, true>
仿函数的版本(其中两个参数共享一个公共类型Scalar
)。这也意味着我的is_scalar
构造在这种情况下不起作用,即使我已经使用它并在其他类上测试它没有问题。
我在其他几个类中需要这种行为,我不想明确地专门化它们中的每一个!任何人都知道我应该怎么做才能解决这个问题?
提前感谢您的帮助!