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:
- 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?
- 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). 
Thanks
 
     
    