mò
^›ðEc           @   s_   d  Z  d Z d k l Z l Z l Z d „  Z d
 d „ Z d
 d „ Z	 d d d „ Z
 d	 Z d
 S(   sM   Cardan's solution of the cubic.

See also HAKMEM note on cubics in search.py
s5   
$Id: cardan.py,v 1.8 2007/03/08 23:25:18 eddy Exp $
(   s   coss   acoss   pic   
      C   s  |  p
 t d ‚ | d |  } | d | d |  } | | d |  d | d | d |  } d d d „ }	 | pB | d	 j  o | f Sn d | d } | | | | | f Sn | p |	 d | ƒ | f Sn | d | d } | d	 j o1 | d } |	 | | ƒ |	 | | ƒ | f Sn | d	 j p t ‚ | d } | d	 j  ow t
 | | d ƒ d } d | t | ƒ | d | t | d t d ƒ | d | t | d
 t d ƒ | f Sn | d	 j  o  | | } | | | | f Sn | | } | | | | f S(   s…  Solves a cubic polynomial equation.

    Takes four arguments, the coefficients of the cube, square, linear and
    constant terms in the cubic, respectively.  Finds all the inputs at which
    that cubic yields zero as output; returns these as a tuple.  Repeated roots
    are appropriately repeated.  Raises ValueError if there are no roots (or if
    *every* input yields zero output - i.e. all arguments are zero); this can
    only happen if the first argument is zero.

    To be specific: all entries in
        map(lambda x: ((a*x +b)*x +c)*x +d, Cardan(a, b, c, d))
    will be tiny.  See cardan(), which wraps Cardan and asserts this.
s4   Cardan solves cubics, use other tools for quadraticsf3.0i   f6.0i   f0.5f1.0c         C   s#   |  d j  o |  | Sn |  | S(   Ni    (   t   xt   third(   R    R   (    (    t(   /home/eddy/.sys/py/study/maths/cardan.pyt   cuberoot*   s     i    i   N(   t   cubet   AssertionErrort   squaret   offsett   lineart   Et   constantt   FR   t   disct   acost   at   cost   pi(
   R   R   R   R
   R   R   R   R   R	   R   (    (    R   t   Cardan   s6     	* 
'
_
c         C   sÀ   | d d |  | } y | i Wnb t j
 oV | d j  o' | o t d ‚ n d | d } q | d j o | d } q n X| d } | d |  d | |  } } | | | | f S(   Ni   f4.0i    s-   Positive definite quadratic has no real rootsx0.01.0f0.5(
   R   R   R
   R   t   imagt   AttributeErrort   realonlyt
   ValueErrort   midt   gap(   R   R   R
   R   R   R   R   (    (    R   t	   quadraticK   s       
c         C   s1  |  pp | p4 y | d | SWqB t j
 o t d ‚ qB Xn y t | | | | ƒ SWqw t j
 o f  Sqw Xn | pA y t |  | | | ƒ } Wn t j
 o f  } n Xd | Sn t	 |  | | | ƒ } | p t
 | ƒ d j o | Sn | \ } | p
 t d ‚ | t |  | | |  | | ƒ S(   Nf1.0s+   Constant cubic has no roots (or everything)i    i   s.   zero root was meant to be dealt with earlier !(   i    (   R   R   R
   R   t   ZeroDivisionErrorR   R   R   t   ansR   t   lent   rootR   (   R   R   R   R
   R   R   R   (    (    R   t   cubicW   s,          	i   f1e-14c   	      C   s}   t  |  | | | | ƒ } x^ t |  | | | d „ | ƒ D]> \ } } | o+ t
 | ƒ | j  p t d | | f ‚ q7 q7 W| S(   Nc         C   s"   |  | |  | |  | |  | f S(   N(   R    t   ut   st   it   c(   R    R   R   R    R!   (    (    R   t   <lambda>t   s    s   %s -> %s(   R   R   R   R    R!   R   R   t   mapR    t   vt   abst   tolR   (	   R   R   R    R!   R   R&   R$   R   R    (    (    R   t   cardanp   s     /s™  
$Log: cardan.py,v $
Revision 1.8  2007/03/08 23:25:18  eddy
comment on HAKMEM

Revision 1.7  2004/04/18 11:38:41  eddy
added a comment assertion

Revision 1.6  2003/07/26 22:34:05  eddy
Separated out degenerate cases and added support for finding complex roots.

Revision 1.5  2003/07/26 15:20:07  eddy
missed out the factor of 1/2 in the quadratic special-case !

Revision 1.4  2003/07/26 13:19:47  eddy
Halved F, introduced cuberoot() to deal with a gotcha.

Revision 1.3  2003/07/26 12:49:03  eddy
Made assertion's tolerance an optional parameter.

Revision 1.2  2003/07/26 12:46:24  eddy
Refined the assertion.

Initial Revision 1.1  2003/07/26 12:37:25  eddy
N(   t   __doc__t   _rcs_id_t   mathR   R   R   R   t   NoneR   R   R'   t	   _rcs_log_(	   R   R   R)   R   R   R,   R   R   R'   (    (    R   t   ?   s   	@