There is a mind-blowing application of matrix multiplication: doing recursion (almost) at the speed of light!
By the end of this thread, you'll learn precisely how.
Trust me, if you are into programming and math, you want to know this trick.
↓ ↓ ↓
Let's start with the simplest example for recursion: Fibonacci numbers.
Each Fibonacci number is the sum of the previous one and the one before.
The recursion starts with 0 and 1.
In Python, the implementation is rather straightforward.
Can you guess the issue?
The execution is extremely slow, as each function call involves two more calls!
Thus, the `fibonacci(n)` calls itself many times!
Now, let's talk about matrix multiplication.
What do you get when multiplying a row and a column vector? Their inner product.
How does this relate to the Fibonacci numbers?
Simple: we can express the Fibonacci recursion in terms of vectors.
With one small trick, we can turn this into an iterative process.
By adding a second column to the right side, we can copy the n-th Fibonacci number over.
Thus, we have a recursive relation.
We can express the above without recursion!
Applying our matrix recursion n times, we obtain an explicit formula to calculate the Fibonacci numbers.
This is much faster to compute.
Just one more step.
By noticing that the Fibonacci numbers start with 0, 1, 1, we can stack two shifted vectors on top of itself and obtain the n+1-th, n-th, n-1-th Fibonacci numbers purely by raising the right matrix to the n-th power.
By clearing everything up, we obtain an extremely elegant formula.
Tell me it's not beautiful. I dare you.
We can quickly implement the formula in NumPy.
Let's see how it performs!
As you can see, it is much faster than the vanilla version.
Moreover, while the vanilla implementation has exponential time complexity, this has linear. Quite the difference!
A small caveat, though. Due to integer overflow, NumPy is not suitable for this task.
Homework for you: write a plain Python implementation that takes advantage of the arbitrarily large ints!
Feel free to share your solution! (I made the snippets with carbon.sh.)
Having a deep understanding of math will make you a better engineer. I want to help you with this, so I am writing a comprehensive book about the subject.
If you are interested in the details and beauties of mathematics, check out the early access!
There is more than one way to think about matrix multiplication.
By definition, it is not easy to understand. However, there are multiple ways of looking at it, each one revealing invaluable insights.
Let's take a look at them!
↓ A thread. ↓
First, let's unravel the definition and visualize what happens.
For instance, the element in the 2nd row and 1st column of the product matrix is created from the 2nd row of the left and 1st column of the right matrices by summing their elementwise product.
To move beyond the definition, let's introduce some notations.
A matrix is built from rows and vectors. These can be viewed as individual vectors.
You can think of them as a horizontal stack of column vectors or a vertical stack of row vectors.
One common wisdom about gambling is that the house always wins.
This is not just a catchphrase; there is mathematical evidence behind it. If you play against an opponent with much deeper pockets, your chances of winning approach zero.
Read on to see why.
↓ A thread. ↓
To illustrate the problem above, consider a simple example: betting on coin tosses.
The dealer tosses a fair coin. If it lands on heads, you win $1. If tails, you lose $1.
You have 𝑛 dollars, while the casino has 𝑚. In total, there are 𝑁 = 𝑛 + 𝑚 dollars on the table.
You win when you reach 𝑁 dollars. However, if you get to zero, you lose.
The question is simple: what is the probability of winning?
There are 25 people in a room. What is the probability that two of them share the same birthday?
If you think it is low, you'll be surprised to find out that the actual probability is more than 50%.
Here is why!
↓ A thread. ↓
The usual thinking is if there are 25 people and 365 days in a year, then chances should be roughly 24/365 ≈ 6.5%, and if we have 366 people, then it is guaranteed that two of them share birthdays.
However, this is not how probability works.
First, it is much easier to talk about the probability of having no shared birthdays.
This is a common trick, often making the calculations much more manageable.