4

I do not use any matrix library, but instead plain std::vector for my matrix data.

To fill it with 2D data I use this code:

data[iy + dataPointsY * ix] = value;

I would like to know is this is correct or if it must be the other way (ix first). To my understanding fftw needs 'Row-major Format'. Since I use it the formula should be according to row-major format.

8
  • 4
    The key point is, they way you store the data should be exactly the way you retrieve. Commented Nov 4, 2016 at 15:26
  • sure, but I know that other libs rely on row.major format. Since I use it I should order the data according to it. Commented Nov 4, 2016 at 15:27
  • If you are trying to encode the 2d indexing in a linear format which some other library expects, then you have to follow exactly the format specified by that library. Like MPI functions would require all the data elements of a matrix should be adjacent in memory. Commented Nov 4, 2016 at 15:29
  • Also, if you traverse the matrix column wise, (that is top to bottom for one single column), then you should consider storing it in a column-major format, as in that case the elements would be adjacent, allowing large datasets being traversed in lesser page faults. Commented Nov 4, 2016 at 15:30
  • The question is not whether this format is "correct". The question is what format does the fftw library expect. Commented Nov 4, 2016 at 15:31

1 Answer 1

3

Assuming you want row major format for fftw, what you want is:

data[ix + iy*dataPointsY]

The point of row-major is, when the combined index increased by 1, the corresponding row index would be same (assuming not overflowing to the next row).

double m[4][4];
mp = (double*)m;
mp[1+2*3] == m[2][1]; //true
mp[2+2*3] == m[2][2]; //true
mp[2+2*3] == m[3][1]; //false

In general, there's no "right" way to store a matrix. Row major format is also called "C-style" matrix, while column major is called "fortran-style" matrix. The naming is due to different multidimensional array indexing scheme between the two language.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.