³ò
ÜˆøFc           @   sä   d  d k  Z  d  d k  Td  d k Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d	 „  Z d
 „  Z	 d „  Z
 d „  Z d d d „  ƒ  YZ d „  Z d „  Z d „  Z d d „ Z d d „ Z d  d k Z d „  Z d „  Z d S(   iÿÿÿÿN(   t   *c         C   s/   |  d j o |  d d Sn |  d d Sd  S(   Ng        g      ð?g      @(    (   t   x(    (    s   polynomial.pyt   cbrt   s    c         C   s8   |  d } | i  } | i } t | | ƒ t | | ƒ f S(   Ny                (   t   realt   imagt   hypott   atan2(   R   t   yt   at   b(    (    s   polynomial.pyt
   cart2polar   s    
		c         C   s:   t  i |  d ƒ } t  i |  d ƒ } |  d | | d S(   Ni   i    y              ð?(   t   matht   cost   sin(   R   R   R	   (    (    s   polynomial.pyt
   polar2cart   s    c   
      C   s—   t  |  ƒ \ } } | d d } | d } | d t i d } | d t i d } t | | g ƒ } t | | g ƒ } t | | g ƒ }	 | | |	 g S(   Ni   g      @g       @g      @(   R
   R   t   piR   (
   R   t   magt   thetat   xmagt   xanglet   xangle2t   xangle3t   r1t   r2t   r3(    (    s   polynomial.pyt   cuberoot   s    
c         C   s¯   t  |  ƒ }  t  | ƒ } t  | ƒ } | d d |  | } | d j o2 | d } | | d |  | | d |  g Sn3 | d d } | | d |  | | d |  g Sd  S(   Ni   i   g        g      à?y                (   t   float(   R   R	   t   ct   dsq(    (    s   polynomial.pyt	   quadratic!   s    
(c         C   sq  t  |  ƒ d j  p
 t d ‚ t  |  ƒ d j p
 t d ‚ t  |  ƒ d j o( |  \ } } t | ƒ t | ƒ g Sn t  |  ƒ d j o# |  \ } } } t | | | ƒ SnÃ|  d d j o |  d d j oG |  d d j p
 t d ‚ |  d \ } } t | ƒ t | ƒ g Sq;|  d \ } } } t | | | ƒ Sn |  \ } } } } t | ƒ } | | } | | } | | } | d } | d | d } | | | d | d } t d	 | | d d ƒ \ }	 }
 t |	 ƒ t d
 ƒ j o7 t |	 ƒ \ } } d t | ƒ t i | d ƒ } n t |	 ƒ t |
 ƒ } t d	 | | | d ƒ \ } } | | | | | | f Sd  S(   Ni   s    poly must for quadratic or cubici   s!   No roots of a constant polynomiali   i   i    g      @g      ð?y                g       @(	   t   lent   AssertionErrorR   R   t   typeR
   R   R   R   (   t   polyR   R	   R   t   dt   aot   tt   pt   qt   ut   vt   rut   wut   y1t   y2t   y3(    (    s   polynomial.pyt   roots-   s<    



!% c         C   sP   |  d d } x; t  d t |  ƒ ƒ D]$ } | | 9} | t |  | ƒ 7} q$ W| S(   Ni    y                i   (   t   rangeR   R   (   R!   R   t   valt   i(    (    s   polynomial.pyt   polyEvalW   s     
c           C   s   d t  i  ƒ  d S(   Ng      $@g      à?(   t   random(    (    (    s   polynomial.pyt   myrand^   s    c         C   sˆ   t  g  } | D] } | t | ƒ q ~ d g ƒ } xM t d t | ƒ ƒ D]6 } t t |  | | ƒ ƒ d | j o t SqJ t SqJ Wd  S(   Ng      ð?i    g-Cëâ6?(   t   maxt   absR/   R   R2   t   Falset   True(   t   tPolyt   tRootst   _[1]R   t   maxRt   j(    (    s   polynomial.pyt	   testRootsa   s    4 $c         C   sG  t  } xd t d |  ƒ D]S } t ƒ  t ƒ  t ƒ  t ƒ  g } t | ƒ } t | | ƒ t  j o | GH| Sq q WxÓ t d d ƒ D]Â } x¹ t d d ƒ D]¨ } xŸ t d d ƒ D]Ž } x… t d d ƒ D]t } t t | ƒ t | ƒ t | ƒ g ƒ d j o@ | | | | g } t | ƒ } t | | ƒ t  j o | Sq3q¿ q¿ Wq© Wq“ Wq} Wg  S(   Ni    iûÿÿÿi   (   R7   R/   R4   R.   R>   R5   R6   (   t   nt   failedR1   R9   R:   R=   t   kt   l(    (    s   polynomial.pyt	   testCubici   s,         .t
   Polynomialc           B   s\   e  Z d  „  Z d „  Z d „  Z d „  Z d d „ Z d „  Z d „  Z d „  Z	 d	 „  Z
 RS(
   c         C   s7   g  } | D] } | | q ~ |  _  t | ƒ |  _ d  S(   N(   t   coeffsR   t   order(   t   selfRE   R;   R   (    (    s   polynomial.pyt   __init__„   s    $c         C   s@   t  |  i | i ƒ } t t t |  i | ƒ t | i | ƒ ƒ ƒ S(   N(   R5   RF   RD   t	   vectorAddt   extendRE   (   t   p1t   p2t   resultOrder(    (    s   polynomial.pyt   __add__ˆ   s    c         C   s   d g |  i  | i  d } xW t |  i  ƒ D]F } x= t | i  ƒ D], } | | | c |  i | | i | 7<qA Wq+ Wt | ƒ S(   Ng        i   (   RF   R/   RE   RD   (   RK   RL   t   resultR1   R=   (    (    s   polynomial.pyt   __mul__   s      .c            s   t  t ‡  f d †  |  i ƒ ƒ S(   Nc            s   |  ˆ  S(    (    (   R   (   t   s(    s   polynomial.pyt   <lambda>–   s    (   RD   t   mapRE   (   RG   RQ   (    (   RQ   s   polynomial.pyt
   scalarMult•   s    t   zc         C   s   t  |  i |  i | ƒ S(   N(   t
   polyStringRF   RE   (   RG   t   var(    (    s   polynomial.pyt   string™   s    c         C   s   t  |  i |  i ƒ S(   N(   RV   RF   RE   (   RG   (    (    s   polynomial.pyt   __str__   s    c         C   s,   d } x |  i  D] } | | | } q W| S(   Ng        (   RE   (   RG   R   R(   R   (    (    s   polynomial.pyR0       s
    
 c         C   s   |  i  S(   N(   RE   (   RG   (    (    s   polynomial.pyt   coeff¦   s    c         C   s*   |  i  d j o t |  i ƒ Sn d GHd  S(   Ni   s/   Order too high to solve for roots.  Try Newton.(   RF   R.   RE   (   RG   (    (    s   polynomial.pyR.   ©   s    (   t   __name__t
   __module__RH   RN   RP   RT   RX   RY   R0   RZ   R.   (    (    (    s   polynomial.pyRD   ‚   s   							c         C   s;   t  |  ƒ t  | ƒ j p t d t |  ƒ t | ƒ ‚ d  S(   Ns"   Error: lists must have same length(   R   R   t   str(   R   R	   (    (    s   polynomial.pyt   assertSameLength³   s    c         C   s?   t  |  | ƒ g  } t |  | ƒ D] \ } } | | | q! ~ S(   N(   R^   t   zip(   R   R	   R;   t   ait   bi(    (    s   polynomial.pyRI   ¹   s    c         C   s   d g | t  |  ƒ |  S(   Ng        (   R   (   RE   RB   (    (    s   polynomial.pyRJ   ¿   s    RU   c         C   s[   d i  g  } t |  ƒ D]: } | | d j o# | t | | |  | d | ƒ q q ~ ƒ S(   Ns    + i    i   (   t   joinR/   t   strh(   RF   RE   RW   R;   R1   (    (    s   polynomial.pyRV   Ã   s    c         C   sP   | d j o d |  Sn | d j o d | d |  Sn d | d |  | f S(   Ni    s   %.2fi   s   %.2f(t   )s   ^%d)(    (   t   coeft   powerRW   (    (    s   polynomial.pyRc   È   s
    c      	   C   s  t  i |  ƒ t t  i d d ƒ ƒ } t t  i d d ƒ ƒ } t t  i d d ƒ ƒ } t g  } t | ƒ D] } | t  i  ƒ  qi ~ ƒ } t g  } t | ƒ D] } | t  i  ƒ  qœ ~ ƒ } t g  }	 t | ƒ D] } |	 t  i  ƒ  qÏ ~	 ƒ }
 t  i  ƒ  } | | |
 i | ƒ GHd  S(   Ni   i   (   R3   t   seedt   intt   uniformRD   R/   R0   (   Rg   t   order1t   order2t   order3R;   R1   RK   t   _[2]RL   t   _[3]t   p3R(   (    (    s   polynomial.pyt   testPolyArithmeticÖ   s    333c         C   s   t  i |  ƒ t g  } t d ƒ D] } | t  i  ƒ  q! ~ ƒ } | i ƒ  \ } } d G| i G| i Gd G| i G| i Gd GHd  S(   Ni   s   ((s   )(s   ))(   R3   Rg   RD   R/   R.   R   R   (   Rg   R;   R1   R%   R   R   (    (    s   polynomial.pyt   testPolyRootsá   s    3(    (   R   R3   R   R
   R   R   R   R.   R2   R4   R>   RC   RD   R^   RI   RJ   RV   Rc   Rp   Rq   (    (    (    s   polynomial.pys   <module>   s*   
						*				1				