Matrix Operations
- Matrix multiplication. Multiply rows of $A$ with columns of $B$.
Input: $A_{n \times k}$ and $B_{k \times m}$. Note that the number of columns in $A$ must be equal to the number of rows in $B$; otherwise, the multiplication can't be done.
Output: $C_{n \times m}$ where $c_{i,j} = \sum_{t=1}^k a_{i,t} \cdot b_{t,j}$.
Algorithm:
- Create space for the matrix $C_{n \times m}$.
- For each $1 \le i \le n$ and $1 \le j \le n$, compute the inner product of row $i$ and column $j$ as follows: $c_{i,j} = \sum_{t=1}^k a_{i,t} \cdot b_{t,j}$. Then, return matrix $C$.
Runtime analysis: For every value of $i$ and $j$, we perform an inner product computation, which runs from $1$ to $k$, hence takes $\Theta(k)$. We do this inner product $nm$ times (remember the Multiplication Principle from the topic on Counting? That's the same idea!), so the runtime is $\Theta(k)\cdot\Theta(nm) =$$\; \Theta(knm)$. Fun fact: for square $A_n$ and $B_n$, it is $\Theta(n^3)$.