在Eigen中创建重塑函数 [英] Making a reshape function in Eigen

查看:137
本文介绍了在Eigen中创建重塑函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是Eigen中的重塑函数的代码。它工作。

Here's the code for a reshape function in Eigen. It works.

typedef MatrixXd mat;
typedef VectorXd vec;

Map<mat> reshape (vec b, const uint n, const uint m) {
    return Map<mat>(b.data(), n, m);
}

我试图为第一个参数决定正确的类型。

I was trying to decide the right type for the first argument.

vec& b 仍然可以工作,但是我遇到了奇怪的错误
const vec& b const vec b

vec & b still works, but I get strange errors with const vec & b or const vec b:

error: invalid conversion from 'const Scalar* {aka const double*}' to 'Eigen::Map<Eigen::Matrix<double, -1, -1> >::PointerArgType {aka double*}' [-fpermissive]
     return Map<mat>(b.data(), n, m);
                 ~~~~~~^~

我怀疑这是由于可变性和事实,重塑功能不分配新的内存,但我想学习一个更详细的解释。

I suspect this is due to mutability and the fact that the reshape function doesn't allocate new memory, but I would like to learn a more detailed explanation.

嗯,看起来 vec &&&

Hmm, it seems that vec && b is the right type for this, but I'm out of my depth.

推荐答案

是正确的,这是一个可变性问题。如果你使用 const vec& ,你只能将它映射到 const mat 作为

You are right that it is a mutability issue. If you use const vec&, you can only map it to a const mat as

Map<const mat> reshape (const vec& b, const uint n, const uint m) {
    return Map<const mat>(b.data(), n, m);
}

否则可以通过映射矩阵修改const vec的组件。

otherwise you can modify the components of a const vec via the mapped matrix. It has nothing to do with allocating memory.

实际上 vec const vec 都错了。两者都意味着映射到原始向量的临时副本,其生命在函数调用之后结束,如Avi Ginsburg所说,导致未定义的行为。

Actually both vec and const vec are wrong. Both of them mean mapping to a temp copy of the original vector, whose life ends after the function call, as Avi Ginsburg said, resulting in an undefined behavior.

请注意,上述代码只有在输入为 Eigen :: VectorXd 时才正确。

Please note the above code is only correct when the input is an Eigen::VectorXd.

事实上,如果你的输入 b,那么 vec& const vec& 是一种表达式。使用 vec& 将无法编译,因为表达式的组件不可变。在函数调用期间,使用 const vec& 将强制将表达式计算到temp常量向量中,并再次映射到temp向量。

In fact both vec& and const vec& can be wrong, if your input b is an expression. Using vec& will fail compiling as the components of an expression are not mutable. Using const vec& will force the expression to be evaluated into a temp const vector during the function call and you are mapping to a temp vector again.

Map<mat> reshape (vec& b, const uint n, const uint m) {
    return Map<const mat>(b.data(), n, m);
}

Map<const mat> reshape_const (const vec& b, const uint n, const uint m) {
    return Map<const mat>(b.data(), n, m);
}

vec a(100), b(100);
Map<mat> c1 = reshap(a, 10, 10);                  // ok
Map<mat> c2 = reshap(a + b, 10, 10);              // error, expression not mutable
Map<const mat> c3 = reshap_const(a, 10, 10);      // ok
Map<const mat> c4 = reshap_const(a + b, 10, 10);  // error, mapping to temp vec

在这种情况下,您需要使用表达式类型作为参数类型,尽管你可能不打算对表达式使用 reshape 。您可以在这里找到更多详情。

In this case you need to use the expression type as the parameter type, although you probably do not plan to use your reshape with expressions. You could find more details here.

http://eigen.tuxfamily.org/dox-devel/TopicFunctionTakingEigenTypes.html

这篇关于在Eigen中创建重塑函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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