76691

Fixed size Eigen matrix as parameters in template function

Question:

I am trying to write a function that takes fixed size matrix using template on the matrix size. I have read <a href="http://eigen.tuxfamily.org/dox/TopicFunctionTakingEigenTypes.html" rel="nofollow">http://eigen.tuxfamily.org/dox/TopicFunctionTakingEigenTypes.html</a> but I am not able to make it works perfectly. I could not use fixed size matrix block operations on the fixed size matrix inside my function. (TutorialBlockOperations.html">http://eigen.tuxfamily.org/dox/group_TutorialBlockOperations.html)

I tried to do it in two ways but both of them did not work.

Here is the function definition A:

<pre class="lang-cpp prettyprint-override">template <int N> Matrix<double, 3, N> foo(const Matrix<double, 3, N>& v) { Matrix<double, 3, N> ret; Vector3d a = v.leftCols<1>(); // error: expected primary-expression before ')' token return ret; }

Here is the function definition B:

<pre class="lang-cpp prettyprint-override">template<typename Derived> Eigen::MatrixBase<Derived> bar(const Eigen::MatrixBase<Derived>& v) { EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived); EIGEN_STATIC_ASSERT(Derived::RowsAtCompileTime == 3, THIS_METHOD_IS_ONLY_FOR_MATRICES_OF_A_SPECIFIC_SIZE); Eigen::Matrix<double, Derived::RowsAtCompileTime, Derived::ColsAtCompileTime> ret; Vector3d a = v.leftCols<1>(); // error: expected primary-expression before ')' token return ret; }

Any ideas?

Answer1:

The argument in version B is correct, b!t not the return type which should be Derived::PlainObject. You also need the template disambiguate keyword to access template member within templated code:

template<typename Derived> typename Derived::PlainObject bar(const Eigen::MatrixBase<Derived>& v) { EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived); EIGEN_STATIC_ASSERT(Derived::RowsAtCompileTime == 3, THIS_METHOD_IS_ONLY_FOR_MATRICES_OF_A_SPECIFIC_SIZE); typename Derived::PlainObject ret; Vector3d a = v.template leftCols<1>(); return ret; }

Recommend

  • Unable to use Environment.GetResourceString static method
  • Is there a way of avoiding so many list(chain(*list_of_list))?
  • Creating a grid of images with jQuery
  • How can Haskell read S-expressions from files?
  • BASH - Split file into several files based on conditions
  • MS Access 2010 SQL Top N query by group performance issue (continued)
  • Implicit conversion confusion between signed and unsigned when reading K&R book
  • Gnuplot histogram 3d
  • Splitting string into groups of specific length
  • Plot ROC curve and calculate AUC in R at specific cutoff info
  • constexpr vs const vs constexpr const
  • Prevent Emacs from modifying the OS X clipboard?
  • CFNetwork SSLHandshake failed (-9806) & (-9800) & (-9830)
  • Problem in Loading xml from specified url using javascript in FF & Google Chrome
  • C++11 variable size POD struct
  • How to sort a same column both in asc order and desc order
  • findObjectsInBackgroundWithBlock block signature not correct
  • Generic/Unknown HTTP Error with response code 0 using UnityWebRequest
  • How do I refer to a client_deploy.wsdd file that's in WEB-INF?
  • How gzip file gets stored in HDFS
  • Responsive left sidebar open close
  • Prevent page break in text block with iText, XMLWorker
  • mave 3.2 not able to access local nexus instance return 502 code
  • Javascript/Jquery runs fast in desktop browsers, but slow in mobile/smartphone browsers…should I spl
  • C++ friend class std::vector
  • SyntaxError: (irb):26: both block arg and actual block given
  • Find group of records that match multiple values
  • Query to find the duplicates between the name and number in table
  • Tamper-proof configuration files in .NET?
  • Eloquent update method change created_at timestamp
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • Javascript + PHP Encryption with pidCrypt
  • WOWZA + RTMP + HTML5 Playback?
  • How does Linux kernel interrupt the application?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?