function pointer with Eigen

為{幸葍}努か 提交于 2020-06-29 03:38:17

问题


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

I have the following questions:

  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?

  2. 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?

  3. 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).

Thanks


回答1:


  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.

  2. 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.

  3. 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.

    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.



来源:https://stackoverflow.com/questions/51840543/function-pointer-with-eigen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!