This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Specialising a template class with a VectorBlock

Tags: None
(comma "," separated)
ofisette
Registered Member
Posts
3
Karma
0
Hi Eigen users,

I am starting to get familiar with Eigen and I now use it in my research for many calculations. It is an awesome tool. Many thanks to all the developers!

I am starting to write template classes (simple functors) that work with Eigen types. By using MatrixBase<Derived> as template parameters, as suggested by the documentation, I was able to successfully specialise the class for plain matrices and the Block type, but I had no luck with the VectorBlock type. I cannot seem to find the right expression! I have no problem with template functions that are implicitly specialised.

Here is a trivial example illustrating this:

Code: Select all
#include <Eigen/Dense>
#include <iostream>

using namespace Eigen;
using namespace std;

template <typename Derived>
typename MatrixBase<Derived>::Scalar f(MatrixBase<Derived> const& v)
{
   return v.sum();
}

template <class Derived>
class g_func
{
private:
   MatrixBase<Derived> const& m_v;
   
public:
   typedef typename MatrixBase<Derived>::Scalar Scalar;
   
   g_func(MatrixBase<Derived> const& v): m_v(v) { }
   
   Scalar operator()(int x)
   {
      return m_v.head(x).sum();
   }
};

int main(int argc, char* argv[])
{
   VectorXd v = VectorXd::Random(10);
   // Specialising template functions is no problem
   cout << f(v) << endl;
   cout << f(v.block(0, 0, 5, 1)) << endl;
   cout << f(v.segment(0, 5)) << endl;

   // Specialising template classes works with VectorXd and
   // Block<VectorXd> but not VectorBlock<VectorXd>
   g_func<VectorXd> g1(v);
   cout << g1(2) << endl;
   g_func< Block<VectorXd> > g2(v.block(0, 0, 5, 1));
   cout << g2(2) << endl;
   g_func< VectorBlock<VectorXd> > g3(v.segment(0, 5)); // BUG!
   cout << g3(0) << endl;
}


I use Eigen 3.0 beta 2 and GCC 4.4.4 on Ubuntu. (I also tried on other Linux variants and with other GCC versions.)

Thanks in advance to anyone who can help!

Olivier
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
the VectorBlock<> template class is just a convenient class to declare and construct vector block expressions, but it is essentially a Block<>, so in your case you should do:

g_func< Block<VectorXd> > g3(v.segment(0, 5));
ofisette
Registered Member
Posts
3
Karma
0
Thanks for the suggestion. I tried it before, but it does not work. I get:

Code: Select all
eigen-test.cpp:41: error: no matching function for call to ‘g_func<Eigen::Block<Eigen::Matrix<double, -0x00000000000000001, 1, 0, -0x00000000000000001, 1>, -0x00000000000000001, -0x00000000000000001, false, true> >::g_func(Eigen::VectorBlock<Eigen::Matrix<double, -0x00000000000000001, 1, 0, -0x00000000000000001, 1>, -0x00000000000000001>)’
eigen-test.cpp:22: note: candidates are: g_func<Derived>::g_func(const Eigen::MatrixBase<Derived>&) [with Derived = Eigen::Block<Eigen::Matrix<double, -0x00000000000000001, 1, 0, -0x00000000000000001, 1>, -0x00000000000000001, -0x00000000000000001, false, true>]
eigen-test.cpp:15: note:                 g_func<Eigen::Block<Eigen::Matrix<double, -0x00000000000000001, 1, 0, -0x00000000000000001, 1>, -0x00000000000000001, -0x00000000000000001, false, true> >::g_func(const g_func<Eigen::Block<Eigen::Matrix<double, -0x00000000000000001, 1, 0, -0x00000000000000001, 1>, -0x00000000000000001, -0x00000000000000001, false, true> >&)
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
oops sorry, it is:

g_func< Block<VectorXd,Dynamic,1> > g3(v.segment(0, 5));

Another solution is to take:

g_func< VectorBlock<VectorXd>::Derived > g3(v.segment(0, 5));

I agree that's not obvious.
ofisette
Registered Member
Posts
3
Karma
0
Thank you!

I will make note of all my related beginner difficulties and then write a patch for the documentation.
User avatar
bjacob
Registered Member
Posts
658
Karma
3
I would note that interfacing directly with our expression templates is NOT a "beginner" issue!


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!


Bookmarks



Who is online

Registered users: bartoloni, Bing [Bot], Evergrowing, Google [Bot], ourcraft