Within the first story [1] of this collection, we’ve got:
- Addressed multiplication of a matrix by a vector,
- Launched the idea of X-diagram for a given matrix,
- Noticed conduct of a number of particular matrices, when being multiplied by a vector.
Within the present 2nd story, we’ll grasp the bodily which means of matrix-matrix multiplication, perceive why multiplication just isn’t a symmetrical operation (i.e., why “A*B ≠ B*A“), and eventually, we’ll see how a number of particular matrices behave when being multiplied over one another.
So let’s begin, and we’ll do it by recalling the definitions that I take advantage of all through this collection:
- Matrices are denoted with uppercase (like ‘A‘ and ‘B‘), whereas vectors and scalars are denoted with lowercase (like ‘x‘, ‘y‘ or ‘m‘, ‘n‘).
- |x| – is the size of vector ‘x‘,
- rows(A) – variety of rows of matrix ‘A‘,
- columns(A) – variety of columns of matrix ‘A‘.
The idea of multiplying matrices
Multiplication of two matrices “A” and “B” might be the commonest operation in matrix evaluation. A identified reality is that “A” and “B” will be multiplied provided that “columns(A) = rows(B)”. On the similar time, “A” can have any variety of rows, and “B” can have any variety of columns. Cells of the product matrix “C = A*B” are calculated by the next components:
[
begin{equation*}
c_{i,j} = sum_{k=1}^{p} a_{i,k}*b_{k,j}
end{equation*}
]
the place “p = columns(A) = rows(B)”. The end result matrix “C” can have the size:
rows(C) = rows(A),
columns(C) = columns(B).
Performing upon the multiplication components, when calculating “A*B” we should always scan i-th row of “A” in parallel to scanning j-th column of “B“, and after summing up all of the merchandise “ai,okay*bokay,j” we can have the worth of “ci,j“.
One other well-known reality is that matrix multiplication just isn’t a symmetrical operation, i.e., “A*B ≠ B*A“. With out going into particulars, we are able to already see that when multiplying 2 rectangular matrices:

For newbies, the truth that matrix multiplication just isn’t a symmetrical operation typically appears unusual, as multiplication outlined for nearly every other object is a symmetrical operation. One other reality that’s typically unclear is why matrix multiplication is carried out by such a wierd components.
On this story, I’m going to offer my solutions to each of those questions, and never solely to them…
Derivation of the matrices multiplication components
Multiplying “A*B” ought to produce such a matrix ‘C‘, that:
y = C*x = (A*B)*x = A*(B*x).
In different phrases, multiplying any vector ‘x‘ by the product matrix “C=A*B” ought to lead to the identical vector ‘y‘, which we’ll obtain if at first multiplying ‘B‘ by ‘x‘, after which multiplying ‘A‘ by that intermediate end result.
This already explains why in “C=A*B“, the situation that “columns(A) = rows(B)” needs to be saved. That’s due to the size of the intermediate vector. Let’s denote it as ‘t‘:
t = B*x,
y = C*x = (A*B)*x = A*(B*x) = A*t.
Clearly, as “t = B*x“, we’ll obtain a vector ‘t‘ of size “|t| = rows(B)”. However later, matrix ‘A‘ goes to be multiplied by ‘t‘, which requires ‘t‘ to have the size “|t| = columns(A)”. From these 2 info, we are able to already determine that:
rows(B) = |t| = columns(A), or
rows(B) = columns(A).
Within the first story [1] of this collection, we’ve got realized the “X-way interpretation” of matrix-vector multiplication “A*x“. Contemplating that for “y = (A*B)x“, vector ‘x‘ goes at first via the transformation of matrix ‘B‘, after which it continues via the transformation of matrix ‘A‘, we are able to broaden the idea of “X-way interpretation” and current matrix-matrix multiplication “A*B” as 2 adjoining X-diagrams:

Now, what ought to a sure cell “ci,j” of matrix ‘C‘ be equal to? From half 1 – “matrix-vector multiplication” [1], we keep in mind that the bodily which means of “ci,j” is – how a lot the enter worth ‘xj‘ impacts the output worth ‘yi‘. Contemplating the image above, let’s see how some enter worth ‘xj‘ can have an effect on another output worth ‘yi‘. It might probably have an effect on via the intermediate worth ‘t1‘, i.e., via arrows “ai,1” and “b1,j“. Additionally, the love can happen via the intermediate worth ‘t2‘, i.e., via arrows “ai,2” and “b2,j“. Typically, the love of ‘xj‘ on ‘yi‘ can happen via any worth ‘tokay‘ of the intermediate vector ‘t‘, i.e., via arrows “ai,okay” and “bokay,j“.

So there are ‘p‘ potential methods wherein the worth ‘xj‘ influences ‘yi‘, the place ‘p‘ is the size of the intermediate vector: “p = |t| = |B*x|”. The influences are:
[begin{equation*}
begin{matrix}
a_{i,1}*b_{1,j},
a_{i,2}*b_{2,j},
a_{i,3}*b_{3,j},
dots
a_{i,p}*b_{p,j}
end{matrix}
end{equation*}]
All these ‘p‘ influences are impartial of one another, which is why within the components of matrices multiplication they take part as a sum:
[begin{equation*}
c_{i,j} =
a_{i,1}*b_{1,j} + a_{i,2}*b_{2,j} + dots + a_{i,p}*b_{p,j} =
sum_{k=1}^{p} a_{i,k}*b_{k,j}
end{equation*}]
That is my visible rationalization of the matrix-matrix multiplication components. By the way in which, deciphering “A*B” as a concatenation of X-diagrams of “A” and “B” explicitly exhibits why the situation “columns(A) = rows(B)” needs to be held. That’s easy, as a result of in any other case it is not going to be potential to concatenate the 2 X-diagrams:

Why is it that “A*B ≠ B*A”
Decoding matrix multiplication “A*B” as a concatenation of X-diagrams of “A” and “B” additionally explains why multiplication just isn’t symmetrical for matrices, i.e., why “A*B ≠ B*A“. Let me present that on two sure matrices:
[begin{equation*}
A =
begin{bmatrix}
0 & 0 & 0 & 0
0 & 0 & 0 & 0
a_{3,1} & a_{3,2} & a_{3,3} & a_{3,4}
a_{4,1} & a_{4,2} & a_{4,3} & a_{4,4}
end{bmatrix}
, B =
begin{bmatrix}
b_{1,1} & b_{1,2} & 0 & 0
b_{2,1} & b_{2,2} & 0 & 0
b_{3,1} & b_{3,2} & 0 & 0
b_{4,1} & b_{4,2} & 0 & 0
end{bmatrix}
end{equation*}]
Right here, matrix ‘A‘ has its higher half full of zeroes, whereas ‘B‘ has zeroes on its proper half. Corresponding X-diagrams are:

The truth that ‘A’ has zeroes on its higher rows ends in the higher objects of its left stack being disconnected.
The truth that ‘B’ has zeroes on its proper columns ends in the decrease objects of its proper stack being disconnected.
What’s going to occur if attempting to multiply “A*B“? Then A’s X-diagram needs to be positioned to the left of B’s X-diagram.

Having such a placement, we see that enter values ‘x1‘ and ‘x2‘ can have an effect on each output values ‘y3‘ and ‘y4‘. Notably, because of this the product matrix “A*B” is non-zero.
[
begin{equation*}
A*B =
begin{bmatrix}
0 & 0 & 0 & 0
0 & 0 & 0 & 0
c_{3,1} & c_{3,2} & 0 & 0
c_{4,1} & c_{4,2} & 0 & 0
end{bmatrix}
end{equation*}
]
Now, what’s going to occur if we attempt to multiply these two matrices within the reverse order? For presenting the product “B*A“, B’s X-diagram needs to be drawn to the left of A’s diagram:

We see that now there isn’t any related path, by which any enter worth “xj” can have an effect on any output worth “yi“. In different phrases, within the product matrix “B*A” there isn’t any affection in any respect, and it’s really a zero-matrix.
[begin{equation*}
B*A =
begin{bmatrix}
0 & 0 & 0 & 0
0 & 0 & 0 & 0
0 & 0 & 0 & 0
0 & 0 & 0 & 0
end{bmatrix}
end{equation*}]
This instance clearly illustrates why order is essential for matrix-matrix multiplication. In fact, many different examples will also be found out.
Multiplying chain of matrices
X-diagrams will also be concatenated once we multiply 3 or extra matrices. For example, for the case of:
G = A*B*C,
we are able to draw the concatenation within the following approach:

Right here we now have 2 intermediate vectors:
t = C*x, and
s = (B*C)*x = B*(C*x) = B*t
whereas the end result vector is:
y = (A*B*C)*x = A*(B*(C*x)) = A*(B*t) = A*s.
The variety of potential methods wherein some enter worth “xj” can have an effect on some output worth “yi” grows right here by an order of magnitude.

Extra exactly, the affect of sure “xj” over “yi” can come via any merchandise of the primary intermediate stack “t“, and any merchandise of the second intermediate stack “s“. So the variety of methods of affect turns into “|t|*|s|”, and the components for “gi,j” turns into:
[begin{equation*}
g_{i,j} = sum_{v=1}^ sum_{u=1}^t a_{i,v}*b_{v,u}*c_{u,j}
end{equation*}]
Multiplying matrices of particular varieties
We will already visually interpret matrix-matrix multiplication. Within the first story of this collection [1], we additionally realized about a number of particular kinds of matrices – the dimensions matrix, shift matrix, permutation matrix, and others. So let’s check out how multiplication works for these kinds of matrices.
Multiplication of scale matrices
A scale matrix has non-zero values solely on its diagonal:

From concept, we all know that multiplying two scale matrices ends in one other scale matrix. Why is it that approach? Let’s concatenate X-diagrams of two scale matrices:

The concatenation X-diagram clearly exhibits that any enter merchandise “xi” can nonetheless have an effect on solely the corresponding output merchandise “yi“. It has no approach of influencing every other output merchandise. Subsequently, the end result construction behaves the identical approach as another scale matrix.
Multiplication of shift matrices
A shift matrix is one which, when multiplied over some enter vector ‘x‘, shifts upwards or downwards values of ‘x‘ by some ‘okay‘ positions, filling the emptied slots with zeroes. To attain that, a shift matrix ‘V‘ should have 1(s) on a line parallel to its primary diagonal, and 0(s) in any respect different cells.

The speculation says that multiplying 2 shift matrices ‘V1‘ and ‘V2‘ ends in one other shift matrix. Interpretation with X-diagrams offers a transparent rationalization of that. Multiplying the shift matrices ‘V1‘ and ‘V2‘ corresponds to concatenating their X-diagrams:

We see that if shift matrix ‘V1‘ shifts values of its enter vector by ‘k1‘ positions upwards, and shift matrix ‘V2‘ shifts values of the enter vector by ‘k2‘ positions upwards, then the outcomes matrix “V3 = V1*V2” will shift values of the enter vector by ‘k1+k2‘ positions upwards, which implies that “V3” can also be a shift matrix.
Multiplication of permutation matrices
A permutation matrix is one which, when multiplied by an enter vector ‘x‘, rearranges the order of values in ‘x‘. To behave like that, the NxN-sized permutation matrix ‘P‘ should fulfill the next standards:
- it ought to have N 1(s),
- no two 1(s) needs to be on the identical row or the identical column,
- all remaining cells needs to be 0(s).

Upon concept, multiplying 2 permutation matrices ‘P1‘ and ‘P2‘ ends in one other permutation matrix ‘P3‘. Whereas the explanation for this may not be clear sufficient if matrix multiplication within the abnormal approach (as scanning rows of ‘P1‘ and columns of ‘P2‘), it turns into a lot clearer if it via the interpretation of X-diagrams. Multiplying “P1*P2” is identical as concatenating X-diagrams of ‘P1‘ and ‘P2‘.

We see that each enter worth ‘xj‘ of the best stack nonetheless has just one path for reaching another place ‘yi‘ on the left stack. So “P1*P2” nonetheless acts as a rearrangement of all values of the enter vector ‘x‘, in different phrases, “P1*P2” can also be a permutation matrix.
Multiplication of triangular matrices
A triangular matrix has all zeroes both above or under its primary diagonal. Right here, let’s focus on upper-triangular matrices, the place zeroes are under the primary diagonal. The case of lower-triangular matrices is analogous.

The truth that non-zero values of ‘B‘ are both on its primary diagonal or above, makes all of the arrows of its X-diagram both horizontal or directed upwards. This, in flip, implies that any enter worth ‘xj‘ of the best stack can have an effect on solely these output values ‘yi‘ of the left stack, which have a lesser or equal index (i.e., “i ≤ j“). That is without doubt one of the properties of an upper-triangular matrix.
In response to concept, multiplying two upper-triangular matrices ends in one other upper-triangular matrix. And right here too, interpretation with X-diagrams offers a transparent rationalization of that reality. Multiplying two upper-triangular matrices ‘A‘ and ‘B‘ is identical as concatenating their X-diagrams:

We see that placing two X-diagrams of triangular matrices ‘A‘ and ‘B‘ close to one another ends in such a diagram, the place each enter worth ‘xj‘ of the best stack nonetheless can have an effect on solely these output values ‘yi‘ of the left stack, that are both on its degree or above it (in different phrases, “i ≤ j“). Which means that the product “A*B” additionally behaves like an upper-triangular matrix; thus, it should have zeroes under its primary diagonal.
Conclusion
Within the present 2nd story of this collection, we noticed how matrix-matrix multiplication will be offered visually, with the assistance of so-called “X-diagrams”. We have now realized that doing multiplication “C = A*B” is identical as concatenating X-diagrams of these two matrices. This methodology clearly illustrates varied properties of matrix multiplications, like why it’s not a symmetrical operation (“A*B ≠ B*A“), in addition to explains the components:
[begin{equation*}
c_{i,j} = sum_{k=1}^{p} a_{i,k}*b_{k,j}
end{equation*}]
We have now additionally noticed why multiplication behaves in sure methods when operands are matrices of particular varieties (scale, shift, permutation, and triangular matrices).
I hope you loved studying this story!
Within the coming story, we’ll tackle how matrix transposition “AT” will be interpreted with X-diagrams, and what we are able to achieve from such interpretation, so subscribe to my web page to not miss the updates!
My gratitude to:
– Roza Galstyan, for cautious overview of the draft ( https://www.linkedin.com/in/roza-galstyan-a54a8b352 )
– Asya Papyan, for the exact design of all of the used illustrations ( https://www.behance.net/asyapapyan ).If you happen to loved studying this story, be at liberty to observe me on LinkedIn, the place, amongst different issues, I may even publish updates ( https://www.linkedin.com/in/tigran-hayrapetyan-cs/ ).
All used photos, until in any other case famous, are designed by request of the writer.
References
[1] – Understanding matrices | Half 1: matrix-vector multiplication : https://towardsdatascience.com/understanding-matrices-part-1-matrix-vector-multiplication/

