Search code examples
c++arrayseigennumeric

C++ and ode system: how to handle it?


I want to code a class to solve systems of ODEs with Euler method in C++ (I'm a beginner). If the equation is scalar, there's no problem, since I can store the solution in a vector or I can dinamiccaly allocate an array with double* sol = new double[N_points]

Things starts to get weird to me if I have to handle matrices, so my question is: **should I use some library as Eigen? Or should I struggle with pointers?

I'm looking for some good way/reference to be sure which is the correct/best method to handle such a situation.


Solution

  • If you want to work with matrices you can do this with array of arrays, or use a simplified abstraction layer with an one dimensional array (or vector) to store the matrix data, like:

    std::vector<double> matrix(row * columns);
    

    To access an item, you can use simple arithmetic, like:

    int index = rowIndex * totalColumns + columnIndex;
    double item = matrix[index];
    

    You can have a look at my DoubleMatrix library (not use it, just check out) to have an examples of this implementation.