固定大小特征值作为参数 [英] Fixed Sized Eigen types as parameters

查看:104
本文介绍了固定大小特征值作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个采用固定大小本征类型的函数(但以标量类型为模板,例如float / double)。我已阅读 http://eigen.tuxfamily.org/dox/TopicFunctionTakingEigenTypes.html 但我无法使其完美运行。



这是函数定义:

  template< typename T> ; 
内联矩阵< T,3,3> makeSkewSymmetric(const Matrix< T,3,1& v))
{
Matrix< T,3,3>出
out<< 0,-v [2],v [1],
v [2],0,-v [0],
-v [1],v [0],0;

退回;
}

现在我正在使用以下方式:

  Vector3d a(1,2,3); 
Matrix3d ass = makeSkewSymmetric(a); //编译
Matrix3d ass = makeSkewSymmetric(a + a); //不编译

我想,我需要使用某种 MatrixBase< Derived> ,但是如何限制大小,因为该函数仅对长度为3的向量有意义。




编辑:我重新定义了以下函数。可以,但是有更好的方法吗?

  template< typename Derived> 
内联矩阵< typename Derived :: Scalar,3,3> makeSkewSymmetric(const MatrixBase< Derived& v)
{
BOOST_STATIC_ASSERT(Derived :: RowsAtCompileTime == 3&& Derived :: ColsAtCompileTime == 1);
Matrix< typename Derived :: Scalar,3,3>出
out<< 0,-v [2],v [1],
v [2],0,-v [0],
-v [1],v [0],0;

退回;
}


解决方案

我只是想过一个好检查Eigen开发人员希望您解决此问题的方式的方法。 Eigen在 MatrixBase 上带有 cross 函数,但是此函数(如您的函数)仅对3D矢量有意义-因此我从Eigen3来源中挖了相关部分:(cf Eigen / src / Geometry / OrthoMethods.h

  ... 
内联类型名称MatrixBase< Derived> :: template cross_product_return_type< OtherDerived> :: type
MatrixBase< Derived> :: cross(const MatrixBase< OtherDerived> & other)const
{
EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Derived,3)
EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(OtherDerived,3)
...


实际上,Eigen本身使用asserts(尽管具有自己的风格)来检查广义函数中的维。


I am trying to write a function that takes fixed size Eigen Types (but templated on Scalar type e.g. float/double). I have read http://eigen.tuxfamily.org/dox/TopicFunctionTakingEigenTypes.html but I am not able to make it work perfectly.

Here is the function definition:

template <typename T>
inline Matrix<T, 3, 3> makeSkewSymmetric(const Matrix<T, 3, 1>& v)
{
  Matrix<T, 3, 3> out;
  out <<     0, -v[2],  v[1],
          v[2],     0, -v[0],
         -v[1],  v[0],     0;

  return out;
}

Now I am using this as following:

Vector3d a(1,2,3);
Matrix3d ass = makeSkewSymmetric(a); // Compiles
Matrix3d ass = makeSkewSymmetric(a + a); // does NOT compile

I guess, I need to use some sort of MatrixBase<Derived>, but then how do I restrict the size, as the function only makes sense for vectors of length 3.

Edit: I redefined the function as following. It works, but is there a better way?

template <typename Derived>
inline Matrix<typename Derived::Scalar, 3, 3> makeSkewSymmetric(const MatrixBase<Derived>& v)
{
    BOOST_STATIC_ASSERT(Derived::RowsAtCompileTime == 3 && Derived::ColsAtCompileTime == 1);
  Matrix<typename Derived::Scalar, 3, 3> out;
  out <<     0, -v[2],  v[1],
          v[2],     0, -v[0],
         -v[1],  v[0],     0;

  return out;
}

解决方案

I just thought of a good way of checking the way the Eigen developers would want you to solve this problem. Eigen comes with a cross function on MatrixBase, but this function, like yours, is only sensible for 3D vectors - so I dug up the relevant part from the Eigen3 source: (cf Eigen/src/Geometry/OrthoMethods.h)

...
inline typename MatrixBase<Derived>::template cross_product_return_type<OtherDerived>::type
MatrixBase<Derived>::cross(const MatrixBase<OtherDerived>& other) const
{
  EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Derived,3)
  EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(OtherDerived,3)
  ...

and indeed, Eigen itself uses asserts (albeit its own flavor) to check for dimensions in generalized functions.

这篇关于固定大小特征值作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆