# Matrices (Pt. 2): MM

## Matrix Multiplication

This is probably where 💩 gets slightly difficult, but it is also where the crux of all data transformation happens 😌. I will firstly walk-through the **mechanical operation**, and then subsequently **a few ways to think** about it in the next page.

### Matrix-Vector Multiplication Operation

*Each row* of matrix evaluates with its corresponding *column vector*

$$
\left\[\begin{array}{cc}
1 & 2\\
3 & 4
\end{array}\right]
\times
\left\[\begin{array}{cc}
5 \\
6
\end{array}\right]
==================

\left\[\begin{array}{cc}
(1\times5) + (2\times 6)\\
(3\times 5) + (4\times 6)
\end{array}\right]  =
\left\[\begin{array}{cc}
17\\
39
\end{array}\right]
$$

We can break-up the above operation to help assist in understanding.

#### Step 1:

![Row 1 of Matrix A, interacts with Col 1 of Matrix B](https://3978845189-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LwfxeT-UMPxSGzkwagB%2F-Lx1mr4bJHLRBgBb_ZYb%2F-Lx1ngneR8Lxlu0C5cuF%2Fimage.png?alt=media\&token=4b2eec08-935a-4384-a381-b3060b8db619)

{% hint style="info" %}

* A(1, 1) multiplies with B(1, 1). &#x20;
* A(1, 2) multiplies with B(2, 1).
* The result of C(1, 1) is the summation of elements involved in the interaction from row 1 of Matrix A and col 1 of Matrix B
  {% endhint %}

#### Step 2:

![Row 2 of Matrix A, interacts with Col 1 of Matrix B](https://3978845189-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LwfxeT-UMPxSGzkwagB%2F-Lx1mr4bJHLRBgBb_ZYb%2F-Lx1nsdXPvJ8wfkEMhsN%2Fimage.png?alt=media\&token=f63c9df9-d2f0-426d-a534-aafaee3b0c22)

{% hint style="info" %}

* A(2, 1) multiplies with B(1, 1). &#x20;
* A(2, 2) multiplies with B(2, 1).
* The result of C(2, 1) is the summation of elements involved in the interaction from row 2 of Matrix A and col 1 of Matrix B
  {% endhint %}

#### Test Yourself!

Now, try to perform the following problem before revealing its answer! 👍

{% tabs %}
{% tab title="Question" %}
$$
\left\[\begin{array}{cc}
3 & 2  & 1\\
4 & 5 & 6 \\
7 & 8 & 9
\end{array}\right]
\times
\left\[\begin{array}{cc}
2 \\
3 \\
4
\end{array}\right]\
\=  ?
$$
{% endtab %}

{% tab title="Answer" %}
$$
\left\[\begin{array}{cc}
3 & 2  & 1\\
4 & 5 & 6 \\
7 & 8 & 9
\end{array}\right]
\times
\left\[\begin{array}{cc}
2 \\
3 \\
4
\end{array}\right]\
\\=
\left\[\begin{array}{cc}
(3\times 2) + (2\times 3) + (1\times 4)\\
(4\times 2) + (5\times 3) + (6\times 4)\\
(7\times 2) + (8 \times 3) + (9\times 4)
\end{array}\right]
==================

\left\[\begin{array}{cc}
16\\
47\\
74
\end{array}\right]
$$
{% endtab %}
{% endtabs %}

### Matrix-Matrix Multiplication Operation

*Each row* of matrix evaluates with each of its *respective* *column vector*.

* Number of columns (n) of left matrix must be equal to number of rows (m) of right matrix

$$
\left\[\begin{array}{cc}
1 & 4  & 7\\
2 & 5 & 8 \\
3 & 6 & 9
\end{array}\right]
\times
\left\[\begin{array}{cc}
2 & 6 \\
1 & 3  \\
6 & 9
\end{array}\right]
==================

\left\[\begin{array}{cc}
48 & 81 \\
77 &  99 \\
66 & 117
\end{array}\right]
$$

Let us break up this problem like we've had previously. 💔

1. We begin with the **first row** of the *left-matrix*, and&#x20;
2. Multiply it with the **first column** of the *right-matrix*, with&#x20;
3. The output element thus occupying the space (1, 1)&#x20;

![Output element (1, 1)](https://3978845189-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LwfxeT-UMPxSGzkwagB%2F-Lx6EosJaKASUqVlXVQ5%2F-Lx6G8HCC4mT7y3cxYV4%2Fimage.png?alt=media\&token=8987c733-7789-4135-aecb-1a82e3e96ecf)

![Output element (1, 2)](https://3978845189-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LwfxeT-UMPxSGzkwagB%2F-Lx6EosJaKASUqVlXVQ5%2F-Lx6GYaoUpj8Bmq9cr48%2Fimage.png?alt=media\&token=22ab0c99-7472-40e6-92ee-35ea2f1613e1)

![Output element (1, 3)](https://3978845189-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LwfxeT-UMPxSGzkwagB%2F-Lx6EosJaKASUqVlXVQ5%2F-Lx6HGrIaOzLwHsLQvg7%2Fimage.png?alt=media\&token=35391db2-5593-4a48-9271-1497d7ace6f7)

![Output element (2, 1)](https://3978845189-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LwfxeT-UMPxSGzkwagB%2F-Lx6EosJaKASUqVlXVQ5%2F-Lx6Hr2PpRaOpgVoBKR2%2Fimage.png?alt=media\&token=434c15e0-30a9-4c84-ab47-170c0686236d)

![Output element (2, 2)](https://3978845189-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LwfxeT-UMPxSGzkwagB%2F-Lx6EosJaKASUqVlXVQ5%2F-Lx6IV0J7fgE0WgU2TmZ%2Fimage.png?alt=media\&token=0a0e6b57-ab98-4b16-aa54-57be27e734e0)

![Output element (2, 3)](https://3978845189-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LwfxeT-UMPxSGzkwagB%2F-Lx6EosJaKASUqVlXVQ5%2F-Lx6I_zhEHXvHEegBdpR%2Fimage.png?alt=media\&token=dffa161c-4dc9-49e6-8e8e-9685ded9b685)

## Useful Tricks

### Size of output

With the previous example laid out, you might question **"What would happen if there are not enough elements in my right-matrix to interact with my left?".**

With that being a valid question, math simply answers back with a "**you can't**" 🥁. Hence, the size of both matrices and the non-commutative property (discussed at next page) are important to ensure valid operations.&#x20;

To recap, we would require&#x20;

* The same amount of *columns* in the *left-matrix*, to &#x20;
* the *rows* of the *right-matrix*

For example, a left-matrix of size **m x n,** has to interact with a right-matrix of size **n x o**, with m and o being any integers greater than or equals to 1.&#x20;

Finally, the following might be a trivial, but yet a useful trick to find the size of the output vector.

![Size of output](https://3978845189-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LwfxeT-UMPxSGzkwagB%2F-Lx4lmrkFHI6O77o9koL%2F-Lx4pQ-xlqJirXwH8dQx%2Fimage.png?alt=media\&token=fdf1c795-5b26-480c-89b1-afa442c305a9)

As it is a requirement for **"n"** (value 2) to be the same,

* With the values of **"m"** (value 2), and
* **"o"** (value 1),
* The size of output variable of **m x o** (2 x 1) can be seen

{% hint style="success" %}
The output matrix size is the rows of the left-matrix, by the columns of the right-matrix **(m x o)**.&#x20;
{% endhint %}

### Standard Method (rows times columns)

To recap from the previous page, an element has the notation of **a(i,j)**,&#x20;

* where **i** is the *row number*,
* and **j** is the *column number*

![Notation of Element](https://3978845189-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LwfxeT-UMPxSGzkwagB%2F-Lx4tQF4xuLbO4xh4oGj%2F-Lx4vy5cs4SlcPN4ETTd%2Fimage.png?alt=media\&token=15bdb516-cd30-49a6-8626-fa3d4e786cf1)

Supposed the fact that the above matrix is the *output matrix X* in the operation (a . b = x)*,*&#x20;

{% hint style="info" %}
What then were the **factors of A and B** used for the computation of a particular element in X?
{% endhint %}

You may then recall that we previously used the count of *row numbers* of the left-matrix, and the count of *column numbers* of the right-matrix to determine the output matrix size.&#x20;

Similarly, for each output element x(i, j), its factors are the resultant interaction between the&#x20;

* **"i-th" row** of the left matrix, and&#x20;
* **the "j-th" column** of the right-matrix.

This would otherwise be also the **standard way** to think about matrix multiplication, with:

$$
x\_{\mathrm{i,j}} =
\sum\_{\mathrm{k=1}}^k
a\_{\mathrm{i,n}}b\_{\mathrm{n,j}}
$$

The following question helps illustrates the discussion.&#x20;

Let's say we arbitrarily wanted to check the value and the method we obtained x(2, 3).&#x20;

![Element of Output Matrix x(2, 3)](https://3978845189-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LwfxeT-UMPxSGzkwagB%2F-Lx4wJqdLHmLEYOn79jl%2F-Lx4wSJfsQlCBxOJQVwT%2Fimage.png?alt=media\&token=fb8e1c59-667d-4447-a807-c9f2a5757d60)

How do we go about doing so? Easy!

We can do so simply via the **2nd row** of the left-matrix, and the **3rd column** of the right-matrix.

![Factors involved in computing x(2, 3)](https://3978845189-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LwfxeT-UMPxSGzkwagB%2F-Lx4wJqdLHmLEYOn79jl%2F-Lx4wnRzoYr7ellNH--s%2Fimage.png?alt=media\&token=aaf9e08c-783d-4010-8cfe-82dfff422b26)

### Columns Method

The *output matrix X* can be alternatively thought of as&#x20;

* A combination of 3 separate columns, from
* Its **corresponding column** in *right-matrix B,*
* Multiplied by *matrix A*

![(A\*Cols of B) represents Cols of X](https://3978845189-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LwfxeT-UMPxSGzkwagB%2F-Lx6MUQfZCpV_srIBJsD%2F-Lx6U_x2ARCuVQDQxNhm%2Fimage.png?alt=media\&token=5e923e63-37ee-440d-89b9-fd47a083472d)

### Row Method

Alternatively, the *output matrix X* can be alternatively thought of as&#x20;

* A combination of 3 separate rows, from
* Its **corresponding row** in *left-matrix* &#x41;*,*
* Multiplied by *matrix B*

![(Rows of A\*B) represents Rows of X](https://3978845189-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LwfxeT-UMPxSGzkwagB%2F-Lx6V6EmuPEGYYrg0Bk0%2F-Lx6XLE0yZrpE6jw_TOG%2Fimage.png?alt=media\&token=abab162b-47fe-4bc9-b622-6b3810a23426)

#### **More on the next page ⏭**
