A linear algebra trick for computing Fibonacci numbers fast
codeconfessions.substack.com
A linear algebra trick for computing Fibonacci numbers fast
1–10 of 76 posts
Re: A linear algebra trick for computing Fibonacci numbers fast
#2Re: A linear algebra trick for computing Fibonacci numbers fast
#3Computing the matrix power by squaring means the sizes of the integers are small until the final step, so that final step dominates the run time.
Re: A linear algebra trick for computing Fibonacci numbers fast
#4 M = P Δ P^-1
Here, P is some invertible matrix and Δ is a diagonal matrix. The powers of M reveal how useful this is: M^N = (P Δ P^-1) * … * (P Δ P^-1)
If you adjust the parentheses, you’ll see N-1 terms of (P^-1 P), which can be removed, giving: M^N = P Δ^N P^-1
The powers of a diagonal matrix is done by taking powers of the entries on the diagonal.You see the φ values (1±√5)/2 in the matrix Δ.
Diagonalization of a 2x2 matrix is simple to do on paper. The diagonal of Δ contains the eigenvalues of M, which can be found using the quadratic formula. Proving that this is a correct way to diagonalize any diagonalizable matrix is more of a chore, but for this specific 2x2 matrix, you can just show that that you’ve found the values for P and Δ.
This is very elegant mathematically, but I would not use the closed-form solution if I wanted the exact answer, because you’d need a lot of precision, and that’s inconvenient.
Re: A linear algebra trick for computing Fibonacci numbers fast
#5The closed form solution as implemented will use floats with some fixed number of bits, right? So it cannot possible compute the numbers precisely except in a finite number of initial cases. Computing the matrix power by squaring means the sizes of the integers are small until the final step, so that final step dominates the run time.
Re: A linear algebra trick for computing Fibonacci numbers fast
#6The closed form solution as implemented will use floats with some fixed number of bits, right? So it cannot possible compute the numbers precisely except in a finite number of initial cases. Computing the matrix power by squaring means the sizes of the integers are small until the final step, so that final step dominates the run time.
Re: A linear algebra trick for computing Fibonacci numbers fast
#7Computing powers of the golden ratio should not be done as scalars, since as other posters noted that requires a lot of precision and makes for a messy computation. It's more simply done on two dimensional numbers of the form a + sqr(5)*b with a and b integers, analogous to complex numbers. Then the computational effort can be seen to equal that of the matrix powers.
Re: A linear algebra trick for computing Fibonacci numbers fast
#8The closed form solution as implemented will use floats with some fixed number of bits, right? So it cannot possible compute the numbers precisely except in a finite number of initial cases. Computing the matrix power by squaring means the sizes of the integers are small until the final step, so that final step dominates the run time.
I think you're right, and the article says "in some rare cases the method may produce incorrect result due to approximation errors".
Re: A linear algebra trick for computing Fibonacci numbers fast
#9This is also a nice way to show that the growth of fib numbers is exponentially bounded.
Re: A linear algebra trick for computing Fibonacci numbers fast
#10I think the proof for the closed-form version is accessible to people with a background in linear algebra. The matrix is diagonalizable (not all matrixes are diagonalizable, but this one is): M = P Δ P^-1 Here, P is some invertible matrix and Δ is a diagonal matrix. The powers of M reveal how useful this is: M^N = (P Δ P^-1) * … * (P Δ P^-1) If you adjust the parentheses, you’ll see N-1 terms of (P^-1 P), which can b…
The thirty three miniatures book showed a more abstract proof, which would have been accessible to people deeply familiar with the concept of basis vectors.