本征函数指针 [英] function pointer with Eigen

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

问题描述

我对Python很熟练,但是对C ++和指针之类的东西还很陌生。我试图用Eigen软件包为线性代数编写一些用于求解ODE的代码(我以后将需要处理许多矩阵,因此我打算从中开始)。我对RK4使用以下代码,它们可以正常工作:

I am pretty competent with Python, but I'm pretty new to C++ and things like pointers. I try to write some codes for solving ODE with the Eigen package for linear algebra (I will need to deal with lots of matrices later, so I plan to start with it). I have the following code for RK4 and they work:

#include "../eigen-eigen-b3f3d4950030/Eigen/Dense"
using namespace Eigen;

VectorXd Func(const VectorXd& a)
{ // equations for solving simple harmonic oscillator
    Vector2d ans;
    ans(0) = a(1);    //  dy/dt
    ans(1) = -a(0);   //  d2y/dt2
    return ans;
}

MatrixXd RK4(VectorXd Func(const VectorXd& y), const Ref<const VectorXd>& y0, double h, int step_num)
{
    MatrixXd y(step_num, y0.rows());
    y.row(0) = y0;

    for (int i=1; i<step_num; i++){
        VectorXd y_old = y.row(i-1).transpose();
        VectorXd k1 = h*Func(y_old);
        VectorXd k2 = h*Func(y_old+k1/2);
        VectorXd k3 = h*Func(y_old+k2/2);
        VectorXd k4 = h*Func(y_old+k3);
        VectorXd dy = (k1 + 2*k2 + 2*k3 + k4)/6;
        y.row(i) = y.row(i-1) + dy.transpose();
    }
    return y;
}

int main()
{
    Vector2d v1;
    v1(0) = 1.4;    v1(1) = -0.1;
    double h = 0.1;
    int step_num = 50;

    MatrixXd sol = RK4(Func,v1,h,step_num);
    return 0;
}

我有以下问题:


  1. 在函数参数中& 是什么意思?通过参考?我只是从官方文档复制了代码,但是我不太确定是否我了解RK4的函数参数中的所有内容,例如 VectorXd Func(const VectorXd& y)。是否有其他方法可以接受Eigen :: MatrixXd和接受Eigen :: MatrixXd作为函数参数的函数?

  1. What's the meaning of & in the function argument? Pass by reference? I just copied the code from the official documentation, but I'm not too sure if I understand every bit in the function arguments of RK4 such as VectorXd Func(const VectorXd& y). Are there alternative ways of accepting Eigen::MatrixXd and functions which accept Eigen::MatrixXd as function arguments?

据我所知,我们无法返回整数函数的2D数组,我们返回的只是数组的第一个元素(如果我写错了,请纠正我)。那么 Eigen :: MatrixX 呢?我们实际上通过/返回的是什么?矩阵的第一个元素还是由Eigen库定义的全新对象?

From what I understand, we cannot return a whole 2D array from a function, and what we are returning is just the first element of the array (correct me if I'm wrong). What about the Eigen::MatrixX? What are we actually passing/returning? The first element of the matrix, or a completely new object defined by the Eigen library?

我不确定这些代码是否有效编写。我可以做些什么来优化这部分吗? (只是想知道我是否做过任何可能显着降低速度的操作。)

I'm not sure if these codes are written efficiently. Are there anything I can do to optimize this part? (Just wondering if I have done anything that may significantly slow down the speed).

谢谢

推荐答案


  1. 是的,& 是通过-引用;后一种是用于传递函数的语法,该函数按引用获取向量并返回向量。 Eigen :: Matrix 应该始终通过引用传递。有很多方法可以将一个函数传递给另一个函数,C ++中最惯用的方法可能是模板参数和 std :: function

  1. Yes, & is pass-by-reference; The latter one is syntax for passing a function, that takes a vector by reference and returns a vector. An Eigen::Matrix should always be passed by reference. There are tons of ways to pass one function to another, the most idiomatic ones in C++ are probably template arguments and std::function.

您不能有多个返回参数,但是可以返回元组 Matrix 对象。 RK4 返回整个矩阵。

You can't have multiple return arguments, but you can return a pair or a tuple or a Matrix object. RK4 returns a whole matrix.

代码相当高效。如果它确实对性能至关重要,则可能需要优化一些内容,但我暂时不担心。

The code is fairly efficient. If it was really performance-critical there might be a few things that could be optimized, but I would not worry for now.

最大的一点是 RK4 非常通用,并且可以处理动态大小的类型,这些类型比其静态大小的计数器部分要昂贵得多( VectorXf Vector2d )。但这需要您为感兴趣的所有维度创建一个专门的版本,或者让编译器通过使用模板来为您完成。

The biggest point is that RK4 is very general and works with dynamically sized types, which are a lot more expensive than their statically sized counter parts (VectorXf vs Vector2d). But this would require you to create a specialized version for all dimensions you are interested in or to get the compiler to do it for you by using templates.

通常:读一本好书可以帮助您入门。

Generally: Read a good book to get you started.

这篇关于本征函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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