SL | Matrix Multiplication

Matrix Multiplication - Column Major

AB=C

In column-major multiplication, matrix B transforms matrix A by taking the linear combinations of the columns of A to produce the columns 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 column major form and full form:

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

B=B0 B1 B2 B3= ω0 ω1 ω2 δ0 δ1 δ2 η0 η1 η2 ζ0 ζ1 ζ2

C=C0 C1 C2 C3= u0 u1 u2 v0 v1 v2 w0 w1 w2 x0 x1 x2

Matrix B Transforms Columns of A

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

A0 A1 A2B0 B1 B2 B3=C0 C1 C2 C3

Each column of B produces the corresponding column of C. That is, the first column of B produces the first column of C; the second column of B produces the second column of C, and so on. Therefore, C will have the same number of columns as B has.

A0 A1 A2 ω0 ω1 ω2 δ0 δ1 δ2 η0 η1 η2 ζ0 ζ1 ζ2 =C0 C1 C2 C3

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

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

Algebraically, here is how the columns of C are computed as the linear combinations of the columns of A:

C0= u0 u1 u2 =ω0A0+ω1A1+ω2A2

C1= v0 v1 v2 =δ0A0+δ1A1+δ2A2

C2= w0 w1 w2 =η0A0+η1A1+η2A2

C3= x0 x1 x2 =ζ0A0+ζ1A1+ζ2A2

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 columns of B plays a very important role in the formation of the linear combinations of the columns of A. By carefully choosing those elements, we can control how much each column of A contributes to each column of C.