News for hackers. Stuff that matters.
Re:A Linear Algebra Trick for Computing Fibonacci Numbers Fast by kragen on Monday November 06, 2023 @02:41PM (#38167781)
what if you use the closed-form answer, but without any floating-point or other approximations

i mean it all comes out to an integer in the end, all the radicals cancel

Re:A Linear Algebra Trick for Computing Fibonacci Numbers Fast by abhi9u on Monday November 06, 2023 @02:54PM (#38167970)
But that seems to be computationally same amount of work as the matrix form, so we get similar performance?
Re:A Linear Algebra Trick for Computing Fibonacci Numbers Fast by kmill on Monday November 06, 2023 @03:31PM (#38168549)
Yes, I believe so, up to some multiplication factor.

You can carry out the exponentiations in the field Q[sqrt(5)], which is two-dimentional over Q. The interesting thing here is that diagonalization is trading one 2d vector space for another -- one with a very well-behaved multiplication operation (it's distributive, commutative, associative, and invertible).

There's no need to do this to quickly compute fibonacci numbers, but it's pretty neat.

Re:A Linear Algebra Trick for Computing Fibonacci Numbers Fast by kragen on Monday November 06, 2023 @03:00PM (#38168053)
hmm, maybe? i haven't tried it so i can't be sure, but it's plausible
Re:A Linear Algebra Trick for Computing Fibonacci Numbers Fast by jfarmer on Monday November 06, 2023 @11:52PM (#38173384)
I did this years ago to demonstrate to my students that the "exact solution" can still be written in code. There are implementations in Ruby and Python, with some benchmarking code: https://github.com/jfarmer/fib-bench/

Code winds up looking like:

  def fib_phi(n)
    ((PhiRational(0,1)**n - PhiRational(1,-1)**n)/PhiRational(-1, 2)).a.to_i
  end
The exponentiation operation uses basic exponentiation by squaring for performance: https://en.wikipedia.org/wiki/Exponentiation_by_squaring

(Technically implemented arithmetic over Q(ϕ) not Q(√5) because it simplifies the code a bit.)

Re:A Linear Algebra Trick for Computing Fibonacci Numbers Fast by abhi9u on Tuesday November 07, 2023 @03:18AM (#38174426)
Nice! Thank you for sharing.
Re:A Linear Algebra Trick for Computing Fibonacci Numbers Fast by dietrichepp on Monday November 06, 2023 @06:12PM (#38170554)
> what if you use the closed-form answer, but without any floating-point or other approximations

You can use a field extension here, ℚ(√5), to represent your numbers. The numbers would be represented as pairs, (a,b), which represents the number a+b√5. You can see for yourself that this set is closed under addition and multiplication.

But when you try to implement code that computes exponents with this representation, you’re probably going to fall back to the old “repeated squaring” trick. But that’s how you calculate exponents with your matrix representation anyway—and you can note that the matrix powers (for this matrix) always take the form

  | a b |
  | 0 a |
So you can represent these matrixes as (a,b) pairs anyway. We are right back where we started—our state is represented as a pair of numbers, and we use repeated squaring to calculate an exponent.

Sometimes these different ideas for representing the problem give us new insight or new algorithms, and sometimes it turns out to be a new perspective on the same piece of code.