# Matrix Multiplication#

## References#

```
[2]:
```

```
import numpy as np
```

## How it happens#

1st row for matrix a is multiplied by 1st column of matrix b and resulted values are added, and this will go on for all the rows of matrix a multiplying with all the columns values of matrix b.

```
n k k
+-------------+ +-------------+ +-------------+
| | | | | |
m | | n | | m | |
| a | X | b | = | |
| | | | | |
| | | | | |
+-------------+ +-------------+ +-------------+
(m, n) (n, k) (m, k)
```

\begin{align*} \begin{bmatrix} a_{1,1} & a_{1,2} & \dots & a_{1,n}\\ a_{2,1} & a_{2,2} & \dots & a_{2,n}\\ & & .\\ & & .\\ a_{m,1} & a_{m,2} & \dots & a_{m,n} \end{bmatrix} \times \begin{bmatrix} b_{1,1} & b_{1,2} & \dots & b_{1,k}\\ b_{2,1} & b_{2,2} & \dots & b_{2,k}\\ & & .\\ & & .\\ b_{n,1} & b_{n,2} & \dots & b_{n,k}š¯‘¸ \end{bmatrix} = \\ \\ \begin{bmatrix} (a_{1,1} \times b_{1,1}) + (a_{1,2} \times b_{2,1}) + \dots + (a_{1,n} \times b_{n,1}) & \dots & \\ . & \\ . & \\ \end{bmatrix} \end{align*}

## Why it happens#

consider that we have a customer who buys certain items

customer |
apple |
banana |
egg |
---|---|---|---|

customer1 |
3 |
4 |
2 |

these items have different rates on different days of the week.

item |
Mon |
Tue |
Wed |
Thu |
Fri |
Sat |
Sun |
---|---|---|---|---|---|---|---|

apple |
13 |
9 |
10 |
12 |
11 |
10 |
8 |

banana |
8 |
7 |
5 |
6 |
8 |
4 |
7 |

egg |
6 |
4 |
2 |
3 |
5 |
6 |
7 |

now actually how much money the customer had spent on each day can be calculated by multiplying, for example on Monday 3 apples of 13 Rs., 4 bananas of 8 Rs., 2 eggs of 6 Rs. In total (3 * 13) + (4 * 8) + (2 * 6) = 83

```
[4]:
```

```
A = np.array([
[ 3, 4, 2 ]
])
B = np.array([
[ 13, 9, 10, 12, 11, 10, 8 ],
[ 8 , 7, 5 , 6 , 8 , 4 , 7 ],
[ 6 , 4, 2 , 3 , 5 , 6 , 7 ]
])
A @ B
```

```
[4]:
```

```
array([[83, 63, 54, 66, 75, 58, 66]])
```

It shows that the customer with same buying pattern for all days in week then the first day he spent 83 Rs. Second day 63 Rs. and so on.

now lets say we have mulitple customers, with different buying patterns

```
[5]:
```

```
A = np.array([
[ 3, 4, 2 ],
[ 1, 1, 1 ],
[ 2, 4, 5 ]
])
B = np.array([
[ 13, 9, 10, 12, 11, 10, 8 ],
[ 8 , 7, 5 , 6 , 8 , 4 , 7 ],
[ 6 , 4, 2 , 3 , 5 , 6 , 7 ]
])
A @ B
```

```
[5]:
```

```
array([[83, 63, 54, 66, 75, 58, 66],
[27, 20, 17, 21, 24, 20, 22],
[88, 66, 50, 63, 79, 66, 79]])
```

this shows total money spent on week days, of all three customers.