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

About Wrapping Eigen

Tags: None
(comma "," separated)
zhaozhang1985
Registered Member
Posts
3
Karma
0

About Wrapping Eigen

Fri Jul 23, 2010 4:52 pm
I am doing a project using matrices. Eigen is the best C++ matrix library I've found. However, I am required to write my own API for matrix class. So I decide to write a wrapper matrix class based on Eigen.

I wanna to keep all the benefits of Eigen, and therefore I decide to make my wrapper class inherit from Eigen. Here, I found some tutorial,
http://eigen.tuxfamily.org/dox-devel/Cu ... Eigen.html

However, I wanna to prevent my wrapper class calling the Eigen functions directly. So, I wanna to make the inheritance private. Could anybody tell me if the private inheritance is a good thing to do?

I met some problem even for simple multiplication operation. I guess it is because of the expression type. But I can't find the right type. Here is my sample codes

Code: Select all
class MyMatrix : private Eigen::MatrixXd
{
public:
   typedef Eigen::MatrixXd Base;
public:
   MyMatrix() : Eigen::MatrixXd() { }
   MyMatrix(const Eigen::MatrixXd& eMtr) : Eigen::MatrixXd(eMtr) { }

   template<typename OtherDerived>
   MyMatrix& operator= (const Eigen::MatrixBase<OtherDerived>& other)
   {
      this->Base::operator=(other);
      return *this;
   }

   template<typename OtherDerived>
   MyMatrix& operator=(const Eigen::ReturnByValue<OtherDerived>& other)
        {
      this->Base::operator=(other);
      return *this;
        }

   const Eigen::ProductReturnType<Eigen::MatrixXd,Eigen::MatrixXd>::Type
   operator* (const MyMatrix& other) const
   {
      return this->Base::operator*(other);
   }

   template<typename Derived> friend
   const typename Eigen::ProductReturnType<typename     Derived,Eigen::MatrixXd>::Type&
   operator* (const Eigen::MatrixBase<Derived>& m1, const MyMatrix& m2);
};

template<typename Derived>
const typename Eigen::ProductReturnType<typename Derived,Eigen::MatrixXd>::Type
operator* (const Eigen::MatrixBase<Derived>& m1, const MyMatrix& m2)
{
   return m1 * (Eigen::MatrixXd)m2;
};


Code: Select all
int main()
{      
   unsigned N = 100;
   MyMatrix cMtr1 = Eigen::MatrixXd::Random(N,N);
   MyMatrix cMtr2 = Eigen::MatrixXd::Random(N,N);
   MyMatrix cMtr3 = Eigen::MatrixXd::Random(N,N);
   MyMatrix r1, r2;

        r1 = cMtr1 * cMtr2 // works well
   r2 = cMtr1 * cMtr2 * cMtr3; // doesn't work
}

The above does not work for the product of three matrices. Could anybody give me some thought or a solution?

Thank you so much!
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: About Wrapping Eigen

Fri Jul 23, 2010 5:35 pm
there were many mistakes (typename, reference, wrong declaration):

Code: Select all
class MyMatrix : private Eigen::MatrixXd
{
public:
   typedef Eigen::MatrixXd Base;
public:
   MyMatrix() : Eigen::MatrixXd() { }
   MyMatrix(const Eigen::MatrixXd& eMtr) : Eigen::MatrixXd(eMtr) { }

   template<typename OtherDerived>
   MyMatrix& operator= (const Eigen::MatrixBase<OtherDerived>& other)
   {
      this->Base::operator=(other);
      return *this;
   }

   template<typename OtherDerived>
   MyMatrix& operator=(const Eigen::ReturnByValue<OtherDerived>& other)
        {
      this->Base::operator=(other);
      return *this;
        }

   const Eigen::ProductReturnType<Eigen::MatrixXd,Eigen::MatrixXd>::Type
   operator* (const MyMatrix& other) const
   {
      return this->Base::operator*(other);
   }

   template<typename Derived> friend
   const typename ProductReturnType<Derived,Eigen::MatrixXd>::Type
   operator* (const Eigen::MatrixBase<Derived>& m1, const MyMatrix& m2)
   {
     return m1 * static_cast<const Eigen::MatrixXd&>(m2);
   }
};
zhaozhang1985
Registered Member
Posts
3
Karma
0

Re: About Wrapping Eigen

Fri Jul 23, 2010 5:41 pm
Thank you very much for your reply. I am new to C++ and doing my first internship. Could you please give me some specific suggestions if I could use private inheritance? I really don't have too much experience with C++. Could you please give me a right direction for wrapping Eigen? I really appreciate your help.

ggael wrote:there were many mistakes (typename, reference, wrong declaration):

Code: Select all
class MyMatrix : private Eigen::MatrixXd
{
public:
   typedef Eigen::MatrixXd Base;
public:
   MyMatrix() : Eigen::MatrixXd() { }
   MyMatrix(const Eigen::MatrixXd& eMtr) : Eigen::MatrixXd(eMtr) { }

   template<typename OtherDerived>
   MyMatrix& operator= (const Eigen::MatrixBase<OtherDerived>& other)
   {
      this->Base::operator=(other);
      return *this;
   }

   template<typename OtherDerived>
   MyMatrix& operator=(const Eigen::ReturnByValue<OtherDerived>& other)
        {
      this->Base::operator=(other);
      return *this;
        }

   const Eigen::ProductReturnType<Eigen::MatrixXd,Eigen::MatrixXd>::Type
   operator* (const MyMatrix& other) const
   {
      return this->Base::operator*(other);
   }

   template<typename Derived> friend
   const typename ProductReturnType<Derived,Eigen::MatrixXd>::Type
   operator* (const Eigen::MatrixBase<Derived>& m1, const MyMatrix& m2)
   {
     return m1 * static_cast<const Eigen::MatrixXd&>(m2);
   }
};
zhaozhang1985
Registered Member
Posts
3
Karma
0

Re: About Wrapping Eigen

Fri Jul 23, 2010 5:53 pm
A million thanks with your help! Now, it works well. I've spend one day on this problem. Thanks again for your professional help!
:)


ggael wrote:there were many mistakes (typename, reference, wrong declaration):

Code: Select all
   template<typename Derived> friend
   const typename ProductReturnType<Derived,Eigen::MatrixXd>::Type
   operator* (const Eigen::MatrixBase<Derived>& m1, const MyMatrix& m2)
   {
     return m1 * static_cast<const Eigen::MatrixXd&>(m2);
   }
};


Bookmarks



Who is online

Registered users: Bing [Bot], blue_bullet, Google [Bot], rockscient, Yahoo [Bot]