SL | Matrix Multiplication

Matrix Multiplication - Row Major

AB=C

In row-major multiplication, matrix A transforms matrix B by taking the linear combinations of the rows of B to produce the rows of C.

This requires that the column count of the first matrix (the one on the left) be equal to the row count of the second matrix (the one on the right.) The reason behind this restriction will become clear by the end of the next section.

As an example, imagine that Matrix A has 3 rows and 3 columns, and matrix B has 3 rows and 4 columns.

To prepare for what follows, write the matrices in row major form and in full form:

A= A0 A1 A2 = α0 α1 α2 β0 β1 β2 θ0 θ1 θ2

B= B0 B1 B2 = μ0 μ1 μ2 μ3 δ0 δ1 δ2 δ3 η0 η1 η2 η3

C= C0 C1 C2 = u0 u1 u2 u3 v0 v1 v2 v3 x0 x1 x2 x3

Matrix A Transforms Rows of B

Repeating again, in row-major multiplication, matrix A transforms matrix B by taking the linear combinations of the rows of B to produce the rows of C.

A0 A1 A2 B0 B1 B2 = C0 C1 C2

Each row of A produces the corresponding row of C. That is, the first row of A produces the first row of C; the second row of A produces the second row of C, and so on. Therefore, C will have the same number of rows as A has.

α0 α1 α2 β0 β1 β2 θ0 θ1 θ2 B0 B1 B2 = C0 C1 C2

To compute a row of C, pick the corresponding row of A and use its elements to form the linear combination of the rows of B. This implies that the column count of A must equal the row count B simply because each element in a row of A wants to multiply the corresponding row in B.

To compute the first row of C, multiply each row of B by the corresponding element of the first row of A and sum the products; to compute the second row of C, multiply each row of B by the corresponding element of the second row of A and sum the products; to compute the third row of C, multiply each row of B by the corresponding element of the third row of B and sum the products; and so on. Multiplying each row of B by the corresponding element of a row of A means: multiplying the first row of B by the first element of the row of A; multiplying the second row of B by the second element of the row of A; multiplying the third row of B by the third element of the row of A; and son on. Again, this implies that the column count of A must equal the row count B.

Algebraically, here is how the rows of C are computed as the linear combinations of the rows of B:

C0= u0 u1 u2 u3 =α0B0+α1B1+α2B2

C1= v0 v1 v2 v3 =β0B0+β1B1+β2B2

C2= x0 x1 x2 x3 =θ0B0+θ1B1+θ2B2

Here we can see that matrix C's row count equals matrix A's row count, and that its column count equals matrix B's column count.

Finally, it is important to understand that every element of the rows of A plays a very important role in the formation of the linear combinations of the rows of B. By carefully choosing those elements, we can control how much each row of B contributes to each row of C.