0
\$\begingroup\$

Can someone provide some type of example of multiplying a 4x4 matrix without using loops?

typedef struct matrix4
{
    data[16];
} m4;

can someone provide a sample of how you you'd multiply two of these in the opengl column major way?

\$\endgroup\$
1
  • \$\begingroup\$ Multiply a 4x4 matrix with what? With another 4x4 matrix? Of course, you dont have to use loops if the matrix dimensions are specified in advance! \$\endgroup\$
    – vanguard2k
    Jul 31, 2015 at 12:39

1 Answer 1

2
\$\begingroup\$

In matrix multiplication, each index in the resulting matrix is the dot product of the ith row in matrix A and the jth column in matrix B:

Cij = A.row( i ) . B.col( j );

Say we have this matrix, which represents indices in row-major representation,

Indices in row-major representation

 0  1  2  3
 4  5  6  7
 8  9 10 11
12 13 14 15

In row-major it will be stored in an array and elements belonging in the same row will be stored sequentially.

0, 1, 2, 3, 4, 5, 6, 7 ... 15

In column-major it's the opposite, elements in the same column will be stored sequentially.

0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15

This corresponds to transposing the row-major matrix. Multiplying two transposed matrices is the same as transposing after multiplying them. So in the end you can multiply column-major matrices in the exact same way as you multiply row-major matrices.

Still I'll show you how to multiply in the "column-major way". If we transpose the matrix above, you can easily see which linear indices correspond to which matrix index in the column-major representation,

Indices in column-major representation

0  4  8 12  // Indices 0,4,8,12 are the first row
1  5  9 13  // Indices 0,1,2,3 the first column, etc
2  6 10 14
3  7 11 15

For example, the value in (0,0) and (2,2) can be calculated like this

C00 = C[0] = A[0] * B[0] + A[4] * B[1] + A[8] * B[2] + A[12] * B[3]
C22 = C[10]= A[2] * B[8] + A[6] * B[9] + A[10] * B[10] + A[14] * B[11]

And I'll leave the remaining indices for you.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .