I don't know if I understood the question correctly so I will state what I understood:
- You have an array of a given size that represents a multidimensional matrix.
- You want to convert a 5 dimensional vector into the real position of the array
I would first create a class to encapsulate it, and I would provide operator() defined with 5 arguments (std::size_t, unsigned int or so). This operator() should first check ranges (maybe throw an exception and convert all the arguments to the final position.
Simplest transformation would be:
size_t position( size_t idx_r, size_t idx_l, size_t idx_rho, size_t idx_alpha, size_t idx_beta )
{
size_t pos = (((((((idx_r * dim_l) + idx_l) * dim_rho) + idx_rho) * dim_alpha) + idx_alpha) * dim_beta) + idx_beta;
return pos;
}
Where *dim_XXX* represent the size of the matrix in dimension XXX.
If you are performing many operations you may want to consider representing internally the data with a different order without changing the interface to get a better cache hit rate.
The general algorithm is converting each index in one dimension to the following dimension element and adding the offset at that dimension. Easiest example is with a 2 dimension system. To access row 3 column 2 on a 10 column array (assuming you store by rows, and counting from 1 on for the sake of argument) you will first calculate the start of the third row, that is 3 * 10 elements per row. Then you add the offset inside the row 2 (column 2).
If you grow it to a 3 dimensional array, first you would have to find the plane you are looking for by multiplying the plane index by the size of the plane and then use the previous algorithm:
size_t position( size_t x, size_t y, size_t z )
{
size_t start_of_plane_z = z * dim_y * dim_x; // z * size_of_plane
size_t start_of_row_y = y * dim_x; // y * size_of_row
size_t offset_inside_row = x;
return start_of_plane_z + start_of_row_y + offset_inside_row;
}
Now, applying some basic algebra you can turn the equation into:
size_t pos = (((z * dim_y) + y) * dim_x) + x;
That will reduce the number of multiplications and be easier to define for a higher number of dimensions.