Computer Science Canada

Need help with the fibonacci Q-Matrix

Author:  wensi [ Thu Aug 30, 2007 9:18 pm ]
Post subject:  Need help with the fibonacci Q-Matrix

I was doing the online judge problems and accidentally found that there is an algorithm which finds the n'th fibonacci in O(log n). It uses the Q-Matrix property and the code is as following:

code:

Divide_Conquer_Fib(n) {
  i = h = 1;
  j = k = 0;
  while (n > 0) {
    if (n%2 == 1) { // if n is odd
      t = j*h;
      j = i*h + j*k + t;
      i = i*k + t;
    }
    t = h*h;
    h = 2*k*h + t;
    k = k*k + t;
    n = (int) n/2;
  }
  return j;
}


I spent hours trying to figure out how it works but still havent find any clue. I know how the matrix works but I dont understand the variables and the arithmetic in the code since its so badly documented. Can anyone please explain to me? or can we discuss it a bit?

Author:  zylum [ Thu Aug 30, 2007 11:32 pm ]
Post subject:  RE:Need help with the fibonacci Q-Matrix

have you ever seen this formula? http://mathworld.wolfram.com/BinetsFibonacciNumberFormula.html I haven't read the problem yet but the formula may be a better solution.

Author:  Catalyst [ Fri Aug 31, 2007 1:09 am ]
Post subject:  Re: Need help with the fibonacci Q-Matrix

the code seems to be just a streamlined version of the Q matrix method using a binary decomposition for the powers.
So for Q^{19} = (Q^16)*(Q^2)*(Q^1) then it would square the Q matrix in each loop then if the binary decomp of n
had the particular bit in it, it would multiply the matrix (to hold Q^n) by the one its been squaring. That might be a little
vague, so heres a non-streamlined version of the above code:
code:

void mult(int& a1,int& b1,int& c1,int& d1,int a,int b,int c,int d) {
        int w,x,y,z;
        w=a1;x=b1;y=c1;z=d1;
        a1=a*w+b*y;     
        b1=a*x+b*z;
        c1=c*w+d*y;
        d1=c*x+d*z;               
}
int fib_Q(int n) {
        int a,b,c,d;
        int w,x,y,z;
       
        a=b=c=1;
        d=0;
       
        w=x=y=1;
        z=0;

        while (n>0) {
                if (n%2 == 1) {
                        mult(a,b,c,d,w,x,y,z);       
                }
                mult(w,x,y,z,w,x,y,z);
                n=(int)(n/2);      
        }

        return b;              
}

the code you posted also seems to take advantage of the fact that Q is symmetric, and rolls some calculations into one

Author:  wensi [ Fri Aug 31, 2007 10:32 am ]
Post subject:  Re: RE:Need help with the fibonacci Q-Matrix

zylum @ Thu Aug 30, 2007 11:32 pm wrote:
have you ever seen this formula? http://mathworld.wolfram.com/BinetsFibonacciNumberFormula.html I haven't read the problem yet but the formula may be a better solution.


that formula isnt accurate for large numbers since u cant get a very precise sqrt(5) using the cmath header.

Author:  wensi [ Fri Aug 31, 2007 11:03 am ]
Post subject:  Re: Need help with the fibonacci Q-Matrix

Catalyst @ Fri Aug 31, 2007 1:09 am wrote:

the code seems to be just a streamlined version of the Q matrix method using a binary decomposition for the powers.
So for Q^{19} = (Q^16)*(Q^2)*(Q^1) then it would square the Q matrix in each loop then if the binary decomp of n
had the particular bit in it, it would multiply the matrix (to hold Q^n) by the one its been squaring. That might be a little
vague, so heres a non-streamlined version of the above code:


thx for ur explaination and ur code, i will be very glad if u could elaborate how the streamlining and combination took place?

Author:  Catalyst [ Fri Aug 31, 2007 2:56 pm ]
Post subject:  Re: Need help with the fibonacci Q-Matrix

what they did is get rid of the mult function my code has and just put those operations right into the main loop. They also dont keep track of the lower
left entry in the Q matrix since it will be equal to the upper right (since the matrix is symmetric). And as a final trick they also dont store the upper left entry of the
Q-matrix but use the fact the a=b+d ( Qrow1=[a,b] Qrow2=[c,d]) (since the entries are fibonacci numbers F_n+1=Fn+F_n-1) to eliminate it from the calculations. After that they avoid doing repeated calculations by storing a few calculations common to more that one line of code in a dummy variable (t in their case).

Author:  wensi [ Sat Sep 01, 2007 9:57 am ]
Post subject:  RE:Need help with the fibonacci Q-Matrix

thank you so much, now i finally understand the code Razz


: