;ň
žBFc           @   sM   d  Z  d d d  Z d   Z d d d  Z d   Z d f  d     YZ d	 S(
   s	  Fibonacci's sequence.

This sequence grows assymptotically as a power of the golden ratio,
(1+5.**.5)/2; there is even a geometrical justification for this.  The usual
algebraic reasoning takes the recurrence relation f(n+1) = f(n) +f(n-1) and
guesses f(n) = k * x**n for some k and x, yielding x = 1 + 1/x whence x*x = 1 +
x, the quadratic whose roots (satisfying (x -1/2)**2 = x*x -x +1/4 = 5/4) are
the golden ratio and its negated inverse (which is equally the result of
subtracting it from one).  This establishes that any solution to Fibonacci's
relation is indeed of form f(n) = k * x**n +h * (-x)**(-n) for some k and h,
with x the golden ratio.

The geometric argument starts from the golden ratio's geometric definition: it
is the ratio of longer to shorter side in a rectangle which is similar to the
rectangle obtained by cutting off from it an internal square on one of its
shorter sides.  If the long side is b and the short side is a, the trimmed
rectangle has sides b-a and a, which must be in the same ratio as a to b, one
way round or the other.  Since b/a cannot equal (b-a)/a for finite b and
non-zero a, the ratio must actually be the other way round: b/a = a/(b-a), which
can be re-arranged as 1 +a/b = b/a, the same equation we solved above with b/a
in place of x.

If we take an arbitrary rectangle and *add* an exterior square on its longer
side, we certainly obtain a rectangle with what was the longer side as its
shorter side.  If we start with a rectangle whose longer side is x+e times its
shorter side, we obtain one whose longer side is (1+x+e)/(x+e); which differs
from (1+x)/x = x by e times the derivative, at x, of (: (1+y)/y &larr;y :),
i.e. by -e/x/x, which is smaller than e since x &gt; 1.  Thus, at each
iteration, adding an external square brings the ratio of sides closer to the
golden ratio (by a factor of about 2.6).  If we start with unit sides we are
effectively computing the Fibonacci sequence, so we may infer that the ratio
between successive terms of this sequence steadilly approaches the golden ratio.
i    i   c         C   s  d |  j  o
 d j  n o | Sn xh |  d j oZ y | | | f \ } } Wn/ t j
 o# | t |  | f \ } } n X|  d }  q) Wxh |  d j  oZ y | | | f \ } } Wn/ t j
 o# | t |  | f \ } } n X|  d }  q W| Sd S(   sI  Computes Fibonacci's sequence.

        Required first argument is the index into Fibonacci's sequence: it
        should be an integer and may be negative.  (If it is not an integer, it
        is effectively rounded towards zero.)  Optional second and third
        arguments are the sequence's entries with indices 0 and 1 respectively;
        their defaults are 0 and 1, respectively.

	Defined here by: for all integers n, f(n+1) = f(n) + f(n-1).  For the
	default initial entries, this gives f(-n) = f(n) for odd n, f(-n) =
	-f(n) for even n; in particular, both f(-1) and f(1) are 1.  Note that
	conventional definitions tend to index from 1 rather than zero and take
	the first two entries to be 1: this helpfully matches what we get here,
	since f(2) = f(1) + f(0) = 1 + 0 = 1.

	Performs its calculations assuming its data to be integers, so handles
	overflow by coercing values to indefinite-precision integers (Python's
	long integer type).  If actual initial values are floats, this handling
	of overflow is probably wrong; you may be better off using
	basEddy.bigfloat.BigFloat()s.
i˙˙˙˙i   N(   s   whats   wass   results   OverflowErrors   long(   s   whats   wass   result(    (    s+   /home/eddy/.sys/py/study/maths/Fibonacci.pys	   fibonacci$   s$        !  !c         C   sB   |  \ } } | \ } } | | | | | | | | | f Sd S(   sŐ  A multiplication with which to speed Fibonacci computation.

	See: http://www.inwap.com/pdp10/hbaker/hakmem/hakmem.html

	Define multiplication on ordered pairs

	(A,B) (C,D) = (A C + A D + B C, A C + B D).

	This is just (A X + B) * (C X + D) mod X^2 - X - 1, and so is
	associative, etc. We note (A,B) (1,0) = (A + B, A), which is the
	Fibonacci iteration. Thus, (1,0)^N = (FIB(N), FIB(N-1)), which can be
	computed in log N steps by repeated squaring, for instance.
N(   s   as   bs   cs   d(   s   .0s   .2s   as   bs   cs   d(    (    s+   /home/eddy/.sys/py/study/maths/Fibonacci.pys   fibtimesM   s    c         C   s   d d f \ } } xt |  d j of t |  d  \ }  } | o% t | | f | | f  \ } } n t | | f | | f  \ } } q W| | f Sd  S(   Ni    i   i   (   s   cs   ds   ns   divmods   is   fibtimess   as   b(   s   ns   as   bs   cs   ds   i(    (    s+   /home/eddy/.sys/py/study/maths/Fibonacci.pys   fibpow]   s      %&c         C   s!   t  | | f t |    d Sd S(   s8   As for fibonacci, but computed in logarithmic time :-)
	i    N(   s   fibtimess   zeros   ones   fibpows   n(   s   ns   zeros   one(    (    s+   /home/eddy/.sys/py/study/maths/Fibonacci.pys
   fastonaccie   s     s	   Fibonaccic           B   s2   t  Z d d d  Z d   Z d   Z d   Z RS(   Ni    i   c         C   s"   | | g g  f \ |  _ |  _ d  S(   N(   s   zeros   ones   selfs   _Fibonacci__naturals   _Fibonacci__negative(   s   selfs   zeros   one(    (    s+   /home/eddy/.sys/py/study/maths/Fibonacci.pys   __init__m   s    c         C   sx   | d j p t  |  i } | d t |  } x5 | d j o' | i | d | d  | d } q4 W|  i | Sd  S(   Ni    i   i˙˙˙˙iţ˙˙˙(   s   ns   AssertionErrors   selfs   _Fibonacci__naturals   rows   lens   lefts   append(   s   selfs   ns   rows   left(    (    s+   /home/eddy/.sys/py/study/maths/Fibonacci.pys   __upp   s    	 c         C   su   | d j p t  |  i } | t |  } x5 | d j o' | i | d | d  | d } q0 W| | d Sd  S(   Ni    iţ˙˙˙i˙˙˙˙i   (   s   ns   AssertionErrors   selfs   _Fibonacci__negatives   rows   lens   lefts   append(   s   selfs   ns   rows   left(    (    s+   /home/eddy/.sys/py/study/maths/Fibonacci.pys   __downz   s    	 c         C   s0   | d j  o |  i |  Sn |  i |  Sd  S(   Ni    (   s   ns   selfs   _Fibonacci__downs   _Fibonacci__up(   s   selfs   n(    (    s+   /home/eddy/.sys/py/study/maths/Fibonacci.pys   __getitem__   s     (   s   __name__s
   __module__s   __init__s   _Fibonacci__ups   _Fibonacci__downs   __getitem__(    (    (    s+   /home/eddy/.sys/py/study/maths/Fibonacci.pys	   Fibonaccil   s   	
	
N(   s   __doc__s	   fibonaccis   fibtimess   fibpows
   fastonaccis	   Fibonacci(   s	   Fibonaccis   fibtimess
   fastonaccis	   fibonaccis   fibpow(    (    s+   /home/eddy/.sys/py/study/maths/Fibonacci.pys   ?!   s
   )		