# 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](/files/-Lx1ngneR8Lxlu0C5cuF)

{% 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](/files/-Lx1nsdXPvJ8wfkEMhsN)

{% 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)](/files/-Lx6G8HCC4mT7y3cxYV4)

![Output element (1, 2)](/files/-Lx6GYaoUpj8Bmq9cr48)

![Output element (1, 3)](/files/-Lx6HGrIaOzLwHsLQvg7)

![Output element (2, 1)](/files/-Lx6Hr2PpRaOpgVoBKR2)

![Output element (2, 2)](/files/-Lx6IV0J7fgE0WgU2TmZ)

![Output element (2, 3)](/files/-Lx6I_zhEHXvHEegBdpR)

## 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](/files/-Lx4pQ-xlqJirXwH8dQx)

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](/files/-Lx4vy5cs4SlcPN4ETTd)

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)](/files/-Lx4wSJfsQlCBxOJQVwT)

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)](/files/-Lx4wnRzoYr7ellNH--s)

### 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](/files/-Lx6U_x2ARCuVQDQxNhm)

### 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](/files/-Lx6XLE0yZrpE6jw_TOG)

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://tony-ng-1.gitbook.io/matrices-and-linear-algebra-fundamentals/matrix-multiplication.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
