Multidimensional arrays can be stored in linear memory in two orders: row-major and column-major. What is the difference between these two orders?
Row Major will search through information as:
[0][0],[0][1],...,[0][n],[1][0],...,[1][n],..[m][n]
Column Major will search through info information as:
[0][0],[1][0],...,[m][0],[0][1],...,[m][1],...,[m][n]
In memory it is always stored as:
[0][0],[0][1],...,[0][n],[1][0],...,[1][n],..[m][n]
From https://en.wikipedia.org/wiki/Row-major_order
The difference is simply that in row-major order, consecutive elements of the rows of the array are contiguous in memory; in column-major order, consecutive elements of the columns are contiguous.
There are no multidimensional arrays in C++, so this question is moot.
Related
The argument for Java 2D arrays being neither row major nor column major is that "two-dimensional array in Java is actually an array of references to arrays". C++ also follows the "array of arrays" abstraction. Then why does C++ require row major order?
Quoting the link in the question
What we may sometimes think of as two-dimensional array in Java is actually an array of references to arrays. It's not stored linearly in memory.
The difference is in C++ arrays are stored linearly in memory.
If you represent an m by n 2D array in linear memory, it has to either be layed out as n groups of m items or m groups of n items. In C and C++ an array like ary[M][N] will be stored as M groups of N items. Whether you want to call those groups rows or columns is arbitrary but the point is the language does specify an order whereas Java does not because a 2D array in Java is not stored "flat".
I am new to C++ and wanted to create a class for matrix and found two methods:
allocating one block of memory (which is faster as I read)
allocating multiple blocks (one for each line which is slower)
But what is better?
on the one hand I can use the second like mat[i][j] which doesn't work with the first method.
and I need to write a function that multiplies matrixes and I'm afraid that the first method will make things really hard when trying to access members
Where did you read that? 2-dimensional matrix can be represented both as a 1-dimensional array or 2-dimensional array. It's just a matter of references with 2 index. So for an element of having index row and col you can get its 1-dimensional index like this: row * matrix_width + col. So there's no impact on the speed besides calculating this index formula.
I understand what an array and a matrix is. I want to learn how to create 3D graphics and I want to know if a multi-demionsional array is the same as a matrix.
There are several uses of the term "matrix". Normally however we say that a matrix is a 2-dimensional array of scalar (integer or floating point) values, with known dimensions, an entry for every position (no missing values allowed), and arranged such that the columns represent observations about or operations on the rows of another matrix. So if we have a matrix with four columns, it only makes sense if we have another matrix or vector with four rows to which the four columns apply.
So the obvious way to represent a matrix in C++ is as a 2D array. But 2D arrays aren't identical with matrices. You might have a 2D array that is not a matrix (missing values which are uninitialised or nan), or a matrix that is not a 2D array (we could represent as a 1D array and do the index calculations manually, or as a "sparse matrix" where most values are expected to be zero and we just have a list of non-zero values).
Matrix is an abstract mathematical concept that can be modeled in C++ using a number of ways:
A two-dimensional array,
An array of pointers to arrays with arrays of identical size
A std::vector<std::vector<T>>
An std::array<N,std::array<M,T>>
A library-specific opaque implementation
The actual implementation is always specific to the drawing library that you have in mind.
Suppose I have a two dimensional array in C++ under CUDA, stored in the shared memory,
like so:
__shared__ float arr[4][4]; // C++ has a default row-major ordering
By default C++ will order the elements in arr in a row-major format.
That is it will allocate a continuous block of memory and store the elements like this (0,0), (0,1), (0,2), (0,3), (1,0), (1,1), ... and so on...
Is there a way to tell the C++/CUDA compiler to arrange this in a column-major order?
Why don't you just swap indexes you are using?
Instead of using arr[x][y] use arr[y][x].
Interesting is why you would like to do this. Maybe using cache memory could be helpful but I can't tell for sure without details.
Hope it help.
Transpose the matrix. arr[4][4] means that arr is an array of 4 arrays of size 4. The reason to store the values in "row-major" ordering is that arr[0], for example, must give us the pointer to the first of these four arrays, and elements of a single array should be placed in contiguous memory locations so that they can be individually referenced by adding an index to a unique identifier.
I know only the number of rows r in a matrix.
How do I read it into a multi-dimensional array arr[MAX][MAX]?
I thought of reading all the elements into a single array, count the no. of elements and then adding them to arr in groups of count/r. Is there a simpler way?
You could use the fact that everything may as well go into contiguous memory so just keep pushing it at the end of std::vector<double>. At the end you know its length, and given that you know r, you now also know the number of columns.
If you really have nothing but the number of rows and a list of data values, just read the whole thing into a vector, and then divide the size of the vector by the number of rows to get the number of columns. You should, however, also know whether the data is stored row-wise or column-wise. On this depends how to index the vector (I would keep the data in the vector and access it through index calculation, most probably encapsulated in a nice little class).