写新的特征表达式

loading...


5

我正在尝试按照最新文档https://eigen.tuxfamily.org/dox-devel/TopicNewExpressionType.html编写新的Eigen表达式 . 基本上,我想要的是重塑功能的一部分,在Eigen中仍然不存在 . 所以chop_expr(这里的特征向量表达式)应该将输入向量重塑为n次矩阵 .

不幸的是,我实现的不适用于在堆上分配的表达式,例如下面的代码不起作用,但是在将MAXV更改为10之后,一切都变得完美 .

另一个问题是

enum {Flags = EvalBeforeNestingBit}

我发现我需要它,否则,当我切割矩阵乘法时,Eigen不会创建临时值,但我猜这种方式我强迫chop_expr为任何其他表达式创建临时值 . 所以问题是我该如何正确地做到这一点?

namespace Eigen {

template <int chunk, typename Derived> struct ChoppedExpression;

namespace internal {

template <int chunk, typename Derived>
struct traits<ChoppedExpression<chunk, Derived>> : traits<Derived> {
  enum {Flags = EvalBeforeNestingBit};
  enum {IsRowMajor = 0};

  enum {   RowsAtCompileTime = chunk};
  enum {MaxRowsAtCompileTime = chunk};

  enum {ColsAtCompileTime = (Derived::RowsAtCompileTime == Eigen::Dynamic 
      ? Eigen::Dynamic : Derived::RowsAtCompileTime / chunk)};

  enum {MaxColsAtCompileTime = (Derived::MaxRowsAtCompileTime == Eigen::Dynamic
      ? Eigen::Dynamic : (Derived::MaxRowsAtCompileTime + chunk - 1) / chunk)};
};

}  // namespace internal

template <int chunk, class Derived> struct ChoppedExpression
  : public MatrixBase<ChoppedExpression<chunk, Derived>> {

   ChoppedExpression(const Derived& arg) : m_arg(arg) {
   EIGEN_STATIC_ASSERT(Derived::ColsAtCompileTime == 1,
       YOU_TRIED_CALLING_A_VECTOR_METHOD_ON_A_MATRIX);

   EIGEN_STATIC_ASSERT(Derived::RowsAtCompileTime % chunk == 0
        || Derived::RowsAtCompileTime == Eigen::Dynamic,
       VECTOR_SHOULD_HAVE_INTEGER_NUMBER_OF_CHUNKS_FOR_CHOPPING);
  }

  typedef Index Index;

  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rows() const { return chunk; }
  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index cols() const { return m_arg.size() / chunk; }

  typedef typename internal::ref_selector<ChoppedExpression>::type Nested;
  typedef typename internal::ref_selector<Derived>::type DerivedTypeNested;

  DerivedTypeNested m_arg;
};

namespace internal {

template<int chunk, typename Derived>
struct evaluator<ChoppedExpression<chunk, Derived>>
  : public evaluator_base<ChoppedExpression<chunk, Derived>> {

  typedef ChoppedExpression<chunk, Derived> XprType;
  typedef typename nested_eval<Derived, XprType::ColsAtCompileTime>::type DerivedNested;
  typedef typename remove_all<DerivedNested>::type DerivedNestedCleaned;
  typedef typename XprType::CoeffReturnType CoeffReturnType;

  enum {
    CoeffReadCost = evaluator<DerivedNestedCleaned>::CoeffReadCost,
    Flags = traits<XprType>::Flags, IsRowMajor = 0
  };

  evaluator(const XprType& xpr) : m_argImpl(xpr.m_arg) {}

  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index row, Index col) const
    { return m_argImpl.coeff(col * chunk + row); }

  evaluator<DerivedNestedCleaned> m_argImpl;
};

}  // namespace internal
}  // namespace Eigen

template<int chunk, typename Derived> EIGEN_ALWAYS_INLINE
EIGEN_DEVICE_FUNC static Eigen::ChoppedExpression<chunk, Derived>
chop_expr(const Eigen::MatrixBase<Derived> &expr)
  { return Eigen::ChoppedExpression<chunk, Derived>(expr.derived()); }


#define MAXV -1

Eigen::Matrix<double, -1, 1, 0, std::max(3*MAXV, -1)> _blendshapes(2, 1);

int main() {
  for (int i = 0; i < 2; ++i) _blendshapes[i] = double(i + 10);

  std::cout << chop_expr<2>(_blendshapes + Eigen::Matrix<double, 2, 1>(1, 1)) << std::endl;
}

Update

最后,我找到了让它工作的方法 . 解决方案是删除DerivedNested和DerivedNestedCleaned typedef(我显然不需要它们在我的表达式中,因为它只是重塑,但是,我无法解释为什么它会导致错误的结果) . 因此,唯一的问题是我应该怎么做EvalBeforeNestingBit?

1回答

  • 2

    您不需要 EvalBeforeNestingBit ,但在评估器中传播标志时必须小心 . 为了安全起见,请写下:

    Flags = traits<XprType>::Flags&HereditaryBits
    

loading...

评论

暂时没有评论!