;ō
HFc           @   sM   d  Z  d   Z d k l Z d   Z d Z d   Z d d d d	 d
  Z d S(   sŠ  Generate pythagorean triangles.

For each naturals i, j with i > j, the triangle with sides
        2*(i*(i+1) +(j+1)*j) +1
        2*(i*(i+1) -(j+1)*j)
        (2*i+1)*(2*j+1)
is a right-angle triangle, as may be verified by summing the squares of
the later two and comparing to the square of the first
(see http://www.chaos.org.uk/~eddy/math/pythagoras.html#Whole).

$Id: pythagorean.py,v 1.4 2007/03/24 15:49:46 eddy Exp $
c         C   s   d |  |  d | d | d d |  |  d | d | d |  d d | d f } | d d | d d | d d j p t  | Sd  S(   Ni   i   i    (   s   is   js   anss   AssertionError(   s   is   js   ans(    (    s-   /home/eddy/.sys/py/study/maths/pythagorean.pys   whole   s    W0(   s   hcfc         C   sG   t  |  |  \ } } } t | | |  } | | | | | | f Sd  S(   N(   s   wholes   is   js   hs   as   cs   hcfs   f(   s   is   js   as   cs   fs   h(    (    s-   /home/eddy/.sys/py/study/maths/pythagorean.pys   coprime   s    sŅ  
For the pythagorean animation, I need the size-lists of the two small squares,
and a list of times (expressed as fractions of a half turn) for which linear
interpolation among those sizes equates to smooth variation of angle.  At any
given time-tick, the two sizes are cos and sin of an angle; we need each to be
an exact decimal in not many digits, and we need the angle, as fraction of the
half turn, to also be an exact decimal in not many digits.  We only really need
to cover a quarter turn, since the half turn can be completed by reversal and
shuffling.

So what we really need is three lists n, s, c, of equal length N
 * their entries must all be short decimal fractions,
 * n must be sorted in increasing order, start at 0 and end at .5
 * successive entries in n must be close together
 * for each i in range(N),
   * s must be a good approximation to sin(n),
   * c must be a good approximation to cos(n) and
   * s*s + c*c must be a very good approximation to 1.
c         C   sß   d h  f \ } } xÂ | |  j  oī | } x | d j o | d } t | |  \ } } } | | j  o | | f \ } } n y | | | | f Wq+ t j
 o" | d | | | | | f <q+ Xq+ Wd | } q W| Sd S(   sģ   Returns a mapping from pythagorean rays to their slopes.

    Required argument, n, limits the search: all distinct yields from
    coprime(i,j) with 0 <= j < i < n are returned.
i   i    f1.0N(	   s   is   boks   ns   js   coprimes   hs   as   cs   KeyError(   s   ns   as   cs   is   hs   js   bok(    (    s-   /home/eddy/.sys/py/study/maths/pythagorean.pys   shapes-   s"       
   %i   i
   i   i   c         C   sī  |  i   d g  f \ }	 } } |	 i d    xr|	 oj| g  f \ } } x|	 D]	\ \ } } } } | | } | | | j  o  | i | | | f | f  qQ | | | | | f \ } }
 x0 | | d j o | | |
 d f \ } }
 qÂ W| d | |
 } | i d |
 | | |
 | | |
 | | d |
 | | | | | |
 | f  | | } qQ W| i d  t |	  t |  j o
 | }	 q1 d t |	  GHPq1 Wd i |  Sd	 S(
   sd  Return SVG depicting the rays coprime(i,j) for j < i < n.

    Required first argument is a mapping from pythagorean rays to their slopes
    (see shapes(), above).  Optional arguments:

      height -- largest y-co-ordinate to use for depicted rays (default: 1024),
      base -- only numerator to be used when rescaling (default: 10),
      maxp -- limit on number of factors of base to use,
      font -- height of font being used by labels.

    Attempts to arrange for text and rays to avoid collisions, but only scaling
    up by naturals and down by powers of the base, never going beyond
    base**maxp.
i    c   
      C   sg   |  \ \ } } } } | \ \ } } } }	 t |	 |  p- t | |  p t | |  p t | |  S(   N(	   s   hs   as   cs   ss   gs   bs   ds   ts   cmp(
   s   .0s   .2s   hs   as   cs   ss   gs   bs   ds   t(    (    s-   /home/eddy/.sys/py/study/maths/pythagorean.pys   <lambda>P   s    i   f1.0s`      <line x1="0" y1="0" x2="%.*f" y2="%.*f" /><text x="%.*f" y="%.*f"> %d: [%d, %d] *%.*f </text>s    s   Giving up on last %d itemss   
N(   s   boks   itemss   slops   tops   texts   sorts   heights   skips   hs   as   cs   ss   fonts   appends   bases   maxps   cuts   ds   lens   join(   s   boks   heights   bases   maxps   fonts   hs   as   cs   cuts   slops   ds   texts   ss   skips   top(    (    s-   /home/eddy/.sys/py/study/maths/pythagorean.pys   raysvg@   s0       
    L 
N(   s   __doc__s   wholes   naturals   hcfs   coprimes   pythanims   shapess   raysvg(   s   raysvgs   pythanims   shapess   coprimes   hcfs   whole(    (    s-   /home/eddy/.sys/py/study/maths/pythagorean.pys   ?   s   			