围绕矩阵/线性代数库的多态包装器-C ++,以本征开头 [英] Polymorphic wrapper around matrix/linear algebra libraries - C++, starting with Eigen

查看:108
本文介绍了围绕矩阵/线性代数库的多态包装器-C ++,以本征开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个高度依赖线性代数例程的自定义C ++数值库.我还使用Eigen来满足实际的矩阵运算.我想将我的库与Eigen实现解耦,以使它不了解Eigen.这样一来,我就可以将Eigen引用保留在一个地方,并且可以轻松地在不久的将来将线性代数库更改为另一种实现.

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.

在Java中,这将相对简单.但是,由于Eigen使用模板,因此我遇到了困难.特别是我正在使用MatrixXd和VectorXd类型.有人对围绕这些类构造包装器有什么建议吗?这些包装器将为Eigen和我的图书馆之间提供牢固的界限?

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?

我的第一次尝试是使用组合实现的,因此对MyBaseMatrix的调用被定向为包含类型(例如MatrixXd)的调用,如下所示:

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?

在此建议其他两种解决方案: http://eigen.tuxfamily.org /dox-devel/TopicCustomizingEigen.html#ExtendingMatrixBase (扩展MatrixBase或继承Matrix).但是,它们似乎不允许我在特征类型和数值库之间建立严格的界限.扩展MatrixBase似乎也不允许运算符重载吗?

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?

我考虑过继承Matrix和MyBaseMatrix(多重继承),但是在尝试保留干净的边界时,模板使我头疼.

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

有人对这个特定问题有任何经验,或者对C ++中类似问题的解决方案有经验吗?

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

推荐答案

从代码设计的角度来看,我不建议这样做,因为您不太可能会替换线性代数库.因此,封装它很可能不会带来任何好处,并使您的代码更加复杂.但是,如果您确实要执行此操作,则可以使用模板专门化.遵循以下原则:

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);
   }
};

编辑:添加了有关typedef的信息以阐明用法.基于以下来自穆迪的评论.

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

在整个库中,您可以键入定义模板类的内容.这样您就可以使用cMatrixMatrix<InternalMatrixType>之类的东西.

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;

这篇关于围绕矩阵/线性代数库的多态包装器-C ++,以本征开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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