# Thread: Question about 2D-array notation and matrices

1. ## Question about 2D-array notation and matrices

I have written a program that does matrix multiplication as in the example below and which gives me correct results (compared to online calculators).

Code:
int A[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int B[3][3] = {
{a, b, c},
{d, e, f},
{g, h, i}
};

// multiplication along the lines of
// (1 * a) + (2 * d) + (3 * g)
C[0][0] = A[0][0] * B[0][0] + A[0][1] * B[1][0] + A[0][2] * B[2][0];
...
However I'm a little unsure whether I can write matrices that I find in math texts in the order in which I see them.

Taking this matrix for right-handed, CCW rotation around the x-axis for example:

Can i write this as
Code:
// c, s are symbolic
double Rx[3][3] = {
{1, 0, 0},
{0, c, s},
{0,-s, c}
};
Or do the C++ rows represent the columns in the picture above, such that the definition becomes:
Code:
double Rx[3][3] = {
{1, 0, 0},
{0, c,-s},
{0, s, c}
};
To summarize, I'm unsure whether matrices, they way they are written in math texts, can simply be written into C++ arrays "as they appear".

2. > To summarize, I'm unsure whether matrices, they way they are written in math texts, can simply be written into C++ arrays "as they appear".
Surely your initial experiment tells you the answer to this.

3. C++ has no concept of matrices. Therefore, there is no "real" way to convert a mathematical matrix to C++.
C++ does, however, have two dimensional arrays where you can use two indexes, say r and c, to index through it. However, because C++ has no concept of matrices, there is nothing saying that the first index shall be the row and the second the column. You can invert them if you want. What matters is how you iterate them. Does that make sense?
There is one thing, though. C++ lays out arrays "row wise" in memory. That means that the array m[0][0...n] comes before m[1][0...n] in memory. Therefore, it is more efficient to iterate the inner dimension first, and the outer last. Although, big matrix multiplication tend to play havoc with caches unless they are optimized.

4. I think I get what you're saying. Whether I should use C++ "rows" or "columns" to represent the columns of a matrix is dependent on the order in which I compute with the array later on.

The reason my question arose is that the mathematical notation, when written with the rows = rows approach, does seem to be in the wrong order (using my multiplication algorithm). So I guess I'll just work with the transpose of the matrix internally (e.g. in a matrix-class constructor) and see if that gives me the desired result while preserving the "look" of textbook matrices in my code.

Thanks.

5. Originally Posted by -Adrian
I think I get what you're saying. Whether I should use C++ rows or columns to represent the columns of a matrix is dependent on the order in which I compute with the array later on.
Exactly.

6. You should follow the normal mathematical conventions, such as the one shown in this wiki article:

Rotation matrix - Wikipedia, the free encyclopedia

The layout of a matrix in C / C++ implies that the indices are [row][col]. The offset to data within a matrix will be ((row x number_of_columns) + col) (times size of each element of the matrix).

When matrices are used for "mapping", the mapping matrix goes on the left, and the data to be mapped goes on the right. This means that mapping of vectors requires you "transpose" them into "vertical" matrices, and end up with a vertical result (1 column, multiple rows), as seen in the wiki article.

Popular pages Recent additions