Naïve Matrix Exponentiation

The simplest approach is simply to do M×M×M×M \times M \times M \times \dots as many times as we need. Note that the standard matrix multiplication approach I outlined above requires two multiplications and an addition per cell of the output when you're multiplying a 2x2 matrix by another 2x2 matrix. This means that it requires eight multiplications and four additions to do what we were doing earlier with a single addition!

I'll put the code here, but this is a significant downgrade over our previous approaches.

I'll also mention that one of the nice things about using linear algebra tools is that you can use someone else's code. I'm only implementing multiplication myself for illustrative purposes: you should definitely be using a library like numpy in Python to do linear algebra operations—it'll be faster than anything you write, and less work for you to boot!

Code
Output

Naïve Matrix Exponentiation

The simplest approach is simply to do M×M×M×M \times M \times M \times \dots as many times as we need. Note that the standard matrix multiplication approach I outlined above requires two multiplications and an addition per cell of the output when you're multiplying a 2x2 matrix by another 2x2 matrix. This means that it requires eight multiplications and four additions to do what we were doing earlier with a single addition!

I'll put the code here, but this is a significant downgrade over our previous approaches.

I'll also mention that one of the nice things about using linear algebra tools is that you can use someone else's code. I'm only implementing multiplication myself for illustrative purposes: you should definitely be using a library like numpy in Python to do linear algebra operations—it'll be faster than anything you write, and less work for you to boot!

Code
Output