Question

I am writing a custom C++ numerical library that relies heavily on linear algebra routines. I am also using Eigen to cater for the actual matrix operations. I want to decouple my library from the Eigen implementation so that it is unaware of Eigen. This will allow me to keep Eigen references in one place and make it easy to change the linear algebra library to another implementation in the near future.

In java, this would be relatively simple. However I am running into difficulties with Eigen as it uses templates. In particular I am using the types MatrixXd and VectorXd. Does anyone have any suggestions about constructing a wrapper around these classes that will provide a solid boundary between Eigen and my library?

My first attempt was implemented using composition so that calls to MyBaseMatrix were directed to calls in the contained type (e.g. MatrixXd) as suggested here: https://forum.kde.org/viewtopic.php?f=74&t=87072&p=154014&hilit=wrap+eigen#p154014. However I am sceptical that I will retain Eigen under-the-hood optimisations?

Two other solutions are suggested here: http://eigen.tuxfamily.org/dox-devel/TopicCustomizingEigen.html#ExtendingMatrixBase, (extending MatrixBase or inheriting Matrix). However they don't seem to allow me the strict boundary between Eigen types and my numerical library. Also extending MatrixBase doesn't seem to allow operator overloading?

I considered inheriting Matrix and MyBaseMatrix (multiple inheritance), but the templating has caused me headaches when trying to retain a clean boundary.

Does anyone have any experience with this particular problem, or solutions to similar problems in C++?

Was it helpful?

Solution

I would not recommend doing this from a code design standpoint, as a linear algebra library is not something you are likely to replace. So encapsulating it will most likely not be beneficial and will make your code more complicated. However if you really want to do this, you would use template specialization. Something along the lines of the following:

template< typename InternalMatrixType>
class Matrix
{
private:
    InternalMatrixType _matrix;
public:
   // Example function
   float operator[](unsigned index)
   {
      return _matrix[index];
   }
};

For a particular linear algebra library:

template<>
class Matrix<EigenMatrixType>
{
private:
    EigenMatrixType _matrix;
public:
   // Example function
   float operator[](unsigned index)
   {
      return _matrix.get(index);
   }
};

Edit: Added information on typedefs to clarify usage. Based on below comment from moodle.

Throughout the library you could then typedef the template class. This will allow you to use something like cMatrix vs Matrix<InternalMatrixType>.

typedef Matrix<InternalMatrixType> cMatrix;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top