How to handle the indices of a 9-dimensional matrix

Viewed 153

I am a physicist currently writing a C++ program dealing with multidimensional integration; in particular, the functions I am considering can have up to D=9 dimensions.

From a mathematical perspective, I need to handle a NxNxN...xN (D times) matrix, but from a programming point of view, I was instructed to use an array of NxNxN...xN elements instead. From what I know, an array is better for the sake of generality and for all the ensuing calculations involving pointers.

However, now I am stuck with a problem I cannot solve.

I need to perform some calculations where a single index of my matrix is fixed and all the other ones take all their different values.

If it were a 3x3x3 matrix, the code would be something similar to the following:

double test[3][3][3];
for(int i=0;i<3;i++) {
    for(int j=0;j<3;j++) {
    test[0][i][j]=i*j;
    }
}

i.e. I could have an index fixed and cycle through the other ones. The same process could be extended to the second and the third index as well.

How can I accomplish the same effect with a double test[3*3*3]? Please keep in mind that the three dimensional matrix is just an example; the real matrices I am dealing with are 9-dimensional, and so I need a general way to keep a single index of my matrix fixed and cycle through all the other ones.

TL;DR: I have an array which represents a NxNxN...xN (9 times) matrix. I need to perform some calculations on the array as if a single index of my matrix were fixed and all the other ones were cycling through all their possible values.

I know there is a simple expression for the case where a 2-D matrix is mapped in a 1-D array; does something similar exist here?

3 Answers
Related