如何集成使用表达式模板的库? [英] How to integrate a library that uses expression templates?

查看:177
本文介绍了如何集成使用表达式模板的库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的程序中使用Eigen矩阵库作为线性代数引擎。 Eigen使用表达式模板实现延迟评估,并简化循环和计算。

I would like to use the Eigen matrix library as the linear algebra engine in my program. Eigen uses expression templates to implement lazy evaluation and to simplify loops and calculations.

例如:

#include<Eigen/Core>

int main()
{
  int size = 40;
  // VectorXf is a vector of floats, with dynamic size.
  Eigen::VectorXf u(size), v(size), w(size), z(size);
  u = 2*v + w + 0.2*z;
}

由于Eigen使用表达式模板,因此

Since Eigen uses expression templates, code like

u = 2*v + w + 0.2*z;

在上面提到的示例中,简化为长度为10的单循环(不是40,进入regiser由大块4),而不创建一个临时。这是多么酷?

In the above mentioned sample reduces to a single loop of length 10 (not 40, the floats are put into regiser by chunks of 4) without creating a temporary. How cool is that?

但是如果我像这样集成库:

But if I integrate the library like this:

class UsingEigen
{
    public:  
        UsingEigen(const Eigen::VectorXf& data):
            data_(data)
        {}

        UsingEigen operator + (const UsingEigen& adee)const
        {
            return UsingEigen(data_ + adee.data_);
        }

        ...
    private:
        Eigen::VectorXf data_;
}

然后输入以下表达式:

UsingEigen a, b, c, d;
a = b + c + d;

无法利用Eigen的实现方式。这不是最后一个。还有许多其他示例,其中在Eigen中使用表达式模板。

cannot take advantage of the way Eigen is implemented. And this is not the last of it. There are many other examples, where expression templates are used in Eigen.

简单的解决方案不是自己定义运算符,make data _ public,

The easy solution would be not to define the operators by myself, make data_ public and just write expressions like:

UsingEigen a, b, c, d;
a.data_ = b.data_ + c.data_ + d.data_;

这会打断封装,但会保留Eigen的效率。

This breaks encapsulation, but it preserves the efficiency of Eigen.

其他方法可以是我自己的操作符,但让他们返回表达式模板。但由于我是C ++的初学者,我不知道这是否是正确的方式。

Other way could be to make my own operators, but let them return expression templates. But since I am a beginner in C++, I do not know if this is the right way to go.

对不起,如果问题本质上太笼统。我是一个初学者,没有人问。直到现在我使用 std :: vector< float> 无处不在,但现在我需要使用矩阵。在整个项目中从 std :: vector< float> 切换到Eigen是一个很大的步骤,我害怕在开始时做错了调用。欢迎任何建议!

I am sorry if the question is too general in nature. I am a beginner and have noone to ask. Up until now I was using std::vector<float> everywhere, but now I need to use matrices also. To switch from std::vector<float> to Eigen in my entire project is a big step and I am afraid of making a wrong call right in the start. Any advice is welcomed!

推荐答案

为什么会暴露 data _ 封装意味着隐藏实现细节,只暴露接口。如果你的包装类 UsingEigen 没有添加任何行为或状态到本地 Eigen 库,接口不会改变。在这种情况下,您应该完全删除此封装,并使用 Eigen 数据结构编写程序。

Why would exposing data_ break encapsulation? Encapsulation means hiding the implementation details and only exposing the interface. If your wrapper class UsingEigen does not add any behavior or state to the native Eigen library, the interface does not change. In this case, you should drop this wrapper altogether and write your program using the Eigen data structures.

矩阵或向量不破坏封装:只有暴露矩阵或向量的实现才会这样做。 Eigen 库公开算术运算符,但不公开它们的实现。

Exposing a matrix or a vector is not breaking encapsulation: only exposing the implementation of the matrix or vector would do that. The Eigen library exposes the arithmetic operators but not their implementation.

使用表达式模板库,用户扩展库功能的最常见方法是添加行为,而不是通过添加状态添加。为了添加行为,你不需要编写包装类:你也可以添加非成员函数,这些函数按照 Eigen 类成员函数实现。请参阅此列非会员功能如何改进封装,由Scott Meyers。

With expression template libraries, the most common way for users to extend the library functionality is by adding behavior, not adding by adding state. And for adding behavior you do not need to write wrapper classes: you can also add non-member functions that are implemented in terms of the Eigen class member functions. See this column "How Non-Member Functions Improve Encapsulation" by Scott Meyers.

对于你将当前程序转换为明确使用 Eigen 功能的版本,您可以执行更改 - 步骤,每次改变程序的小部分,确保你的单元测试(你有单元测试,不是吗?)不要断开,因为你一起去。

As for your concern that the transformation of your current program to a version that explicitly uses the Eigen functionality: you can perform the change step-by-step, changing small parts of your program each time, making sure your unit tests (you do have unit tests, don't you?) do not break as you go along.

这篇关于如何集成使用表达式模板的库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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