m
vbFc           @   sh   d  Z  d k Z d k l Z d e f d     YZ d e f d     YZ d e f d     YZ [ [ d S(	   s   Polynomials.  Coefficients are assumed numeric.  Only natural powers are considered.

$Id: polynomial.py,v 1.19 2007/06/03 16:42:30 eddy Exp $
N(   s   Lazyt   invalidCoefficientc           B   s   t  Z d  Z RS(   s"   Invalid coefficient for polynomial(   t   __name__t
   __module__t   __doc__(    (    (    t,   /home/eddy/.sys/py/study/maths/polynomial.pyR       s    t   unNaturalPowerc           B   s   t  Z d  Z RS(   s7   Power of variable in polynomial is not a natural number(   R   R   R   (    (    (    R   R   	   s    t
   Polynomialc           B   s  t  Z d  Z d   Z e i e i e i e i f d  Z	 e	 d  Z
 [	 d   Z e i e i f d  d  Z d   Z d   Z d	   Z d
   Z d   Z e Z d Z d   Z e d  Z [ d   Z d   Z e d  Z [ d   Z e d  Z [ d   Z e d  Z [ d   Z d   Z e Z d   Z  d   Z! d   Z" e" Z# d   Z$ e$ Z% d   Z& d   Z' e' d  Z( d   Z) e' e) d   Z* [' [) d!   Z+ e+ d"  Z, e+ e i e i f d#  d$  Z- [+ d%   Z. d&   Z/ d'   Z0 d( d( d)  Z1 d*   Z2 d+   Z3 d,   Z4 d-   Z5 d.   Z6 e i7 d/  Z8 d0   Z9 d1   Z: d2   Z; d3   Z< d4   Z= d5   Z> d6   Z? d7   Z@ d8   ZA d9 kB lC ZC eC d:  ZD [C d( d; d<  ZE eF d=  ZG d>   ZH d? d( d@  ZI dA   ZJ RS(B   s:  Model of the mathematical ring of polynomials (in one free variable).

    Supports arithmetic (+, -, *, /, %, divmod, power, negation), comparison
    (but cmp() will yield zero in some cases where == will say no),
    representation (as a python lambda expression), evaluation (i.e. calling as
    a function), repeated integration (as <<) and differentiation (as >>), use
    as boolean (only the zero polynomial is false) and (lazy) hashing.

    Lazy attributes:
    ===============

      rank -- highest power of the free variable with non-zero coefficient
      normalised -- same polynomial scaled so rank's coefficient is 1
      derivative -- result of differentiating the polynomial
      assquares -- decompose as sum of scaled squares and a remainder
      sign -- like cmp(self, 0) but None when ill-defined or variable
      isreal -- are all coefficients real ?
      factors -- tuple of normalised irreducible factors and an optional scalar

    Note that a poly with true isreal considers positive-definite quadratic
    factors to be irreducible; assigning isreal = None will persuade a poly to
    believe quadratic factors are reducible.  The value of p.assquares is of
    form (bok, rem) with rem either zero or of odd degree and p-rem equal to
    reduce(lambda y, (x, v): y + x*x*v, bok.items(), 0).

    The value of sign may presently sometimes be None when it needn't be; I've
    yet to find an example, but the computation in use only deals with `easy
    enough' cases.  Note that cmp() yields zero when the difference's sign is
    None or zero; but == is true only when sign is zero (actual equality).

    Methods:
    ========

      coefficient(n) -- yields coefficient of (: x**n &larr;x :) in polynomial
      hcf([poly, ...]) -- yields highest common factor of arbitrarily many
      integral([start=0, base=0]) -- integration
      unafter(poly) -- input to poly yielding self as output
      seek_root([guess=0, tol=1e-6]) -- find an input mapped to zero
      seek_factor([guess=None]) -- find a factor, if possible

    See individual methods' docs for details.
c         G   s   h  |  _ y | \ } | |  i |  Wn t j
 o |  i |  nm t t t f j
 oW y | i	 | i
 d d  f Wn# t j
 o |  i d |  q X|  i |  n Xd S(   s   Constructor.

        If exactly one argument is passed and it is a mapping, its keys are used
        as powers and values as coefficients of each power's term in the
        polynomial; if exactly one argument is passed and it is a sequence, it
        is interpreted as a mapping whose keys are indices into the list and
        values are entries in the list.  Otherwise, each argument must be
        numeric and the arguments are interpreted as if they'd been passed as a
        sequence.

        Thus:
          Polynomial({ power: coeff ... }) == (: sum(: coeff * x**power :) <- x :)

          Polynomial([ co_0_, co_1_ ... ]) ==
          Polynomial(( co_0_, co_1_ ... )) ==
          Polynomial( co_0_, co_1_ ... )   == (: sum(: co_i_ * x**i <- i :) <- x :)

        As special cases of the last, Polynomial( value ) generates the constant
        polynomial value <- x and Polynomial() generates the zero polynomial -
        but note that this is the scalar zero polynomial; if a vector zero or
        dimensioned zero is desired, supply it as the value for a constant
        polynomial; but if your vector type supports slicing, you'd better pass
        it as Polynomial({0: zero}) instead.

        The polynomial 1 + 2.x + 3.x**2 <- x may thus be generated by any of:
        Polynomial(1,2,3), Polynomial((1,2,3)), Polynomial([1,2,3]) or
        Polynomial({0:1,1:2,2:3}).  The last, dictionary, form is generally
        preferrable for sparse polynomials (ie, rank >> the number of non-zero
        coefficients): tuple form is better for short and sweet forms like that
        illustrated.

        Note that setting z = Polynomial(0, 1) provides a `free variable' that
        can then be used to generate polynomials the way many folk prefer; with
        this, Polynomial(1,2,3) can simply be written 3*z*z +2*z +1.  If you
        want a polynomial's representation to use the name z (rather than x),
        simply set the polynomial's .variablename to 'z' (but note that this
        only applies to that polynomial, not to ones computed from it).
i    N(   t   selft   _Polynomial__coefst   argst   argt   _Polynomial__fromseqt
   ValueErrort   AttributeErrort	   TypeErrort   KeyErrort   itemst   gett   Nonet   _Polynomial__storet   _Polynomial__frombok(   R   R	   R
   (    (    R   t   __init__6   s    % 		   c         C   sn   t  |   | j o) |  t |   j o t |   Sn |  Sn/ t |  d  o t |  d  o |  Sn t  d  S(   Nt   __add__t   __mul__(   t   typet   valt   oktypest   longt   hasattrR    (   R   R   (    (    R   t
   _get_coeffk   s      c         C   s1   | o | |  |  i | <n | |  |  _ d  S(   N(   t   coefft   gR   R   t   powert   _zero(   R   R    R   R   (    (    R   t   __storeu   s     c         C   s5   d } x( | D]  } |  i | |  d | } q Wd  S(   Ni    i   (   t   it   seqt   vR   R   (   R   R$   R#   R%   (    (    R   t	   __fromseqz   s
     c         C   s   t  |   | j S(   N(   R   t   kR#   (   R'   R#   (    (    R   t   <lambda>   s    c         C   sV   xO | i   D]A \ } } | |  p | d j  o
 t  q |  i | |  q Wd  S(   Ni    (   t   bokR   t   keyR   t   okR   R   R   (   R   R)   R+   R   R*   (    (    R   t	   __frombok   s
      
c         C   sq   x9 |  i i   D]( } |  i | d j o |  i | =q q Wy t |  i i    SWn t j
 o d Sn Xd  S(   Nf0.0i(   R   R   t   keysR*   t   maxR   (   R   t   ignoredR*   (    (    R   t   _lazy_get_rank_   s       c         G   s5   x. | D]& } x | o | |  | }  } q Wq W|  S(   N(   t   otherst   otherR   (   R   R1   R2   (    (    R   t   hcf   s       c         G   s   |  i |  i d j S(   Ni    (   R   R3   R1   t   rank(   R   R1   (    (    R   t   coprime   s    c         C   sJ   |  i d j  o t d  n |  i |  i } | d j o |  Sn |  | S(   Ni    s   Can't normalise zeroi   (   R   R4   R   R   t   scale(   R   R/   R6   (    (    R   t   _lazy_get_normalised_   s      c         C   s~   y |  i SWn t j
 o n X|  i g } |  i | d  } d d i |  } | p | d } n | | } | |  _ | S(   Ni    s   lambda %s: s   , t   0(
   R   t   _Polynomial__reprR   t   variablenamet   namest   _Polynomial__representt   textt   joint   lambt   ans(   R   R?   R=   R;   R@   (    (    R   t   __repr__   s       
	t   zc         C   ss   y! |  i d j o |  i }  n Wn t j
 o n Xy |  i | d |  SWn t j
 o t |   Sn Xd  S(   Ni    i   (   t   numt   imagt   realR   R<   R;   t   deptht   str(   RC   R;   RF   (    (    R   t   format   s        c   	      C   s  xc | t |  j oO | d d d j o | i d  q | i t t | d d  d   q Wd | | } } x|  i	 D] } |  i |  } | o | d j o
 d } nS | d j o
 d } n< | | | |  } d | j o d	 | d
 } n | d 7} | d j o | | 7} qK| d | | f 7} n | | | |  } | d  d j o | d | 7} q | d | 7} q W| d  d j o | d Sn | d  d j o | d Sn | S(   Nii    t   at   Zi   t    t   -t    t   (s   )*t   *s   %s**%ds    +i   (   RF   t   lenR;   t   appendt   chrt   ordt   resultt   nameR   t   _powersR*   t   coefficientR   t   fragt   fmt(	   R   R;   RF   RY   RX   RU   R   R*   RT   (    (    R   t   __represent   s<      *
  
 
 
    c         C   s@   h  } x- |  i i   D] \ } } | |  | | <q Wt |  S(   N(   R)   R   R   R   R'   R%   t   eachR   (   R   R[   R)   R%   R'   (    (    R   t
   __eachattr   s
      c         C   s)   y |  i SWn t j
 o |  Sn Xd  S(   N(   R   RE   R   (   R   (    (    R   t   toreal   s      c         C   s   |  i |  S(   N(   R   t   _Polynomial__eachattrt   as(   R   R/   R_   (    (    R   t   _lazy_get_real_   s    c         C   s)   y |  i SWn t j
 o d Sn Xd  S(   Ni    (   R   RD   R   (   R   (    (    R   t   toimag   s      c         C   s   |  i |  S(   N(   R   R^   R_   (   R   R/   R_   (    (    R   t   _lazy_get_imag_   s    c         C   s)   y |  i SWn t j
 o |  Sn Xd  S(   N(   R   t	   conjugateR   (   R   (    (    R   t   conjug8   s      c         C   s   |  i |  S(   N(   R   R^   R_   (   R   R/   R_   (    (    R   t   _lazy_get_conjugate_   s    c         C   s-   |  i i   } | i   | i   t |  S(   N(   R   R   R-   t   sortt   reverset   tuple(   R   t   igR-   (    (    R   t   _lazy_get__powers_   s    

c         C   s   y | i i   } Wn" t j
 o h  d | <} n X|  i } x7 |  i i   D]& \ } } | i | |  | | | <qQ Wt |  S(   Ni    (   R2   R   t   copyt   sumR   R   R!   t   zeroR   R'   R%   R   R   (   R   R2   R'   R%   Rm   Rl   (    (    R   R      s      	 c         C   s   |  i i   |  i } } y | i } Wn- t j
 o! | i	 d |  | | d <n9 Xx4 | i
   D]& \ } } | i	 | |  | | | <qc Wt |  S(   Ni    (   R   R   Rk   R!   Rl   Rm   R2   R)   R   R   R   R*   R   R   (   R   R2   R   R)   Rm   R*   Rl   (    (    R   t   __sub__  s       c         C   s   y | i i   } Wn" t j
 o h  d | <} n X|  i } x7 |  i i   D]& \ } } | i | |  | | | <qQ Wt |  S(   Ni    (   R2   R   Rk   Rl   R   R   R!   Rm   R   R'   R%   R   R   (   R   R2   R'   R%   Rm   Rl   (    (    R   t   __rsub__  s      	 c   
      C   s   h  } y | i } WnA t j
 o5 x |  i i   D] \ } } | | | | <q4 Wnw X|  i	 | i	 } xb |  i i   D]Q \ } } xB | i   D]4 \ } } | | }	 | i |	 |  | | | |	 <q Wqw Wt |  S(   N(   t   termR2   R   R)   R   R   R   R*   R   R!   Rm   t   clet   lueRl   R   R   (
   R   R2   Rp   R   R)   Rq   Rm   Rr   R*   Rl   (    (    R   R     s        
&c         C   sM   |  i |  \ } } | o- | i |  i  o t |  d | |   n | S(   Ns   not a multiple of(   R   t
   __divmod__R2   t   qt   rt   _Polynomial__istinyt   _bigcoefR   (   R   R2   Rt   Ru   (    (    R   t   __div__'  s    c         C   s   |  i |  d S(   Ni   (   R   Rs   R2   (   R   R2   (    (    R   t   __mod__.  s    c         C   s/   |  | } | | |  j o | Sn |  d | S(   Nf1.0(   RC   t   divt   rat(   RC   Rz   R{   (    (    R   t   divide0  s    
 c         C   s  y | i } Wn/ t j
 o# d t h  d | < } } n X| d j  o
 t  n | i |  } | d j oN h  } x+ |  i
 i   D] \ } }
 |
 | | | <q Wt |  t d  f Sn t d  |  }	 } |  i } x | | j o | i |  } x | o t h  | | | | |  < } |	 | | | | }	 } | i |  } t |  d t |  j o% d | i
 | | f GH| i
 | =Pn | } q W| i | j  p t  | i } q W|	 | f S(   sz   Solves self = q.other + r for r of rank < other.rank: returns (q, r)

	This depends on our coefficients forming a field.
	i    f0.0i
   s(   Wiping %g x**%d in Polynomial.__divmod__N(   R2   R4   t   topR   R   t   ZeroDivisionErrorRW   t   oR)   R   R   R   R'   R%   Rt   Ru   t   gott   mt   ratioR6   t   nt   abst   AssertionError(   R   R2   R   R6   R)   R   R   R   R   Rt   R%   R'   Ru   R}   (    (    R   Rs   5  s@       ! 
 	  "
c         C   s`   |  p t  y4 |  d j o n  | o |  | Sn |  d }  Wn t j
 o n X|  | S(   Ni    x0.00.0(   R   R   t   oddt   expR   (   R   R   R   (    (    R   t
   scalarroot\  s       c         C   s6  | d j o |  i | j n p t  |  i | } t h  | | |  i |  i  d | | d  < } |  | | } x | d j o | o | i | d | i } | | j  o
 | } n t  | t h  | | | i | i  | | i | i  | d  < } |  | | } q W| o
 t  n | S(   Ni    f1.0i   i   (   RC   R   R4   R   R}   R   t   rootRW   R@   t   rest   nextR   R   (   R   RC   R   R   R   R   R@   R}   (    (    R   t   __rootf  s     ,5  
' 
c         C   s2   y t  |   SWn t j
 o t |   Sn Xd  S(   N(   t   intR   t   OverflowErrorR   (   R   (    (    R   t   whole|  s      c         C   s]   |  i i | |  i  } y% | |  } | | j o | Sn Wn t t	 f j
 o n X| S(   N(
   R   R   R   R*   R!   R   R   R#   R   R   (   R   R*   R   R   R#   (    (    R   RW     s      c         C   s   t  |   | j S(   N(   R   R#   t   t(   R#   R   (    (    R   R(     s    c         C   s  d |  } } y' | i d j  o | i d  } n Wn t j
 o n Xy | d j  o
 t  n |  i d j  o |  Sn d } | |  pW | |  } | | j o
 | } q | |  i } | | |  j o
 t  n d } n Wn% t t t f j
 o t |  n X| ok x1 d | } | | } | | |  j o PqqW| |  i j p t  | |  |  i |  } } n x@ | d j o2 | d o | | } n | | | d } } qW| S(   Ni   i    l    i   (   R   RT   t   werR2   R4   RW   R   R   R   R+   R   R#   R   R   R   t   _Polynomial__root(   R   R2   R   R+   R#   R   R   RT   (    (    R   t   __pow__  sJ       
  
 
  

 
   c         C   s8   d | >x) | d j o |  i   | d }  } q W|  S(   s   f << n -> nth integral of fl    i    i   N(   R2   R   t   integral(   R   R2   (    (    R   t
   __lshift__  s      c         C   s5   d | ?x& | d j o |  i | d }  } q W|  S(   s   f >> n -> nth derivative of fl    i    i   N(   R2   R   t
   derivative(   R   R2   (    (    R   t
   __rshift__  s      c         C   sM   h  } x: |  i i   D]) \ } } | o | | | | d <q q Wt |  S(   s   Differentiate a polynomial. i   N(   R)   R   R   R   R'   R%   R   (   R   R/   R)   R%   R'   (    (    R   t   _lazy_get_derivative_  s       i    c         C   s|   h  } x3 |  i i   D]" \ } } | d | | | d <q Wt |  } | o | | |  } n | o | | } n | S(   sx  Integrate a polynomial.

        Optional arguments, start and base, specify the constant of integration;
        the integral's value at start (whose default is 0) will be base (whose
        default is also zero).  Note that self.integral(base=h) is equivalent to
        self.integral()+h; and self.integral(start, 0)(end) is the integral of
        self from start to end.f1.0i   N(
   R)   R   R   R   R'   R%   R   R@   t   startt   base(   R   R   R   R)   R@   R%   R'   (    (    R   R     s        c         C   s   |  i d j S(   Ni    (   R   R4   (   R   (    (    R   t   __nonzero__  s    c         C   s   |  | i p d S(   Ni    (   R   R2   t   sign(   R   R2   (    (    R   t   __cmp__  s    c         C   s   |  | i d j  S(   Ni    (   R   R2   R4   (   R   R2   (    (    R   t   __eq__  s    c         C   s   |  S(   N(   R   (   R   (    (    R   t   __pos__  s    c         C   s   d |  S(   Ni    (   R   (   R   (    (    R   t   __neg__  s    c         C   sv   y( t  | i  | j o |  | f Sn Wn t j
 o n Xy |  t |  f SWn t t f j
 o d  Sn Xd  S(   N(
   R   R2   R   t   boktypR   R   R   R   R    R   (   R   R2   R   (    (    R   t
   __coerce__  s        c         C   sD   d } x7 |  i i   D]& \ } } | t |  At |  A} q W| S(   Ni    (   RT   R   R   R   R*   R   t   hash(   R   R/   R   RT   R*   (    (    R   t   _lazy_get__lazy_hash_  s
     c         C   s   |  i } y | d | d } } Wn t j
 o |  i Sn X|  i |  } xK | D]C } x' | | j o | | | d } } q^ W| |  i |  } qU Wx' | d j o | | | d } } q W| S(   s   Evaluate a polynomial as a function

	For a polynomial, p, with coefficients in some domain V, and a value t
	in some domain T supporting *: T-> V-> V and +: V-> V-> V, we can
	evaluate p(t) by substituting t in as the value of p's formal
	parameter.
i    i   N(
   R   RV   R-   R}   t
   IndexErrorR!   RW   RT   R*   R
   (   R   R
   R-   RT   R*   R}   (    (    R   t   __call__  s"     	     c         C   s   |  h  } } x | o | i | i o t d | d |   n | i | i } t | | |  \ } } | i d j p t	  | i | | i j  p t	  | i
 d  | | <q Wt |  S(   s  Returns a polynomial which, if fed other, will yield self.

        The nature of polynomial arithmetic is such that, if f and g are
        polynomials, their composite (: g(f(x)) &larr;x :) is simply g(f).  This
        method seeks to express self as g(other) for some g.  Requires rank of
        self to be a multiple of rank of other, among other things; raises
        ValueError if the goal can't be met.
s   Cannot factoriset   viai    N(   R   t   residueRT   R4   R2   R   t   pt   divmodRt   R   RW   R   (   R   R2   R   Rt   R   RT   (    (    R   t   unafter  s      c         C   sY   |  i } |  i |  } x: | d j o, | | | d } } | |  i |  } q W| S(   sO  Integrates self * (: exp(-t) &larr;t :{positives}).

        When self is power(n) the result is Gamma(n+1) = n!, so the result is
        just the sum of self's coefficients, each multiplied by the factorial of
        its order.  The name is slighly misguided but not unreasonable.  See
        atomic.py for a sub-class using this.
i    i   N(   R   R4   R   RW   R@   (   R   R/   R@   R   (    (    R   t   _lazy_get_Gamma_1  s     	 c   	      C   sJ  h  |  } } x| i d d j o | i } | i |  | d } } | d j p t  t h  | d < } x~ | d j op | d } t
 | | | | d | |  \ } } | i | j p t  | t h  | | i |  < } qs W| | | | | | | <} q W|  | t d   | i   d  j p t  | | f S(   s^  Decompose self as a sum of scaled squares, as far as possible.

        Returns a twople, (bok, poly) in which: poly is zero or a polynomial of
        odd rank; bok is a mapping from polynomials to scalars; if each key of
        bok is squared and multiplied by the corresponding value, summing the
        results and adding poly will yield self. i   i    i   c         C   s   | \ } } |  | | | S(   N(   t   xR'   t   y(   R   t   .2R   R'   (    (    R   R(   Y  s    N(   R   R)   t   remR4   R#   RW   R'   R   R   RB   R   Rt   t   ignt   reduceR   (	   R   R/   R#   Rt   R)   R   R   RB   R'   (    (    R   t   _lazy_get_assquares_A  s"      	 
)'"-c         C   sJ   xC |  i i   D]2 } y | i o d  Sn Wq t j
 o q Xq Wd S(   Ni   (   R   R   t   valuesR%   RD   R   R   (   R   R%   (    (    R   t   __pure_real\  s     
  	c         C   s7   y |  i i   d d SWn t j
 o d Sn Xd  S(   Ni    (   R   R   R   R   (   R   Ri   (    (    R   t   _lazy_get__zero_d  s      c         C   s
   |  i   S(   N(   R   t   _Polynomial__pure_real(   R   R/   (    (    R   t   _lazy_get_isreal_h  s    c         C   s  |  i d j  o d Sn |  i d p |  i o d  Sn |  i d j o t |  i d  d  Sn |  i \ } } | i d j o d  Sn | i	   } | i d j o | i | i d   n t |  t |  } } | d j o d Sn | d j  o d Sn d  S(   Ni    i   i(   R   R4   t   isrealR   t   cmpRW   t	   assquarest   bRu   R   t   rowRQ   t   minR.   t   lot   hi(   R   R/   R   R   Ru   R   R   (    (    R   t   _lazy_get_sign_j  s$           (   s   cubicc         C   s   t  | t |  i d   } t d   |  } | o- | } t d   |  } | o
 | } qa n | d | d } } x1 | D]) } t
 |  t
 |  j o
 | } q} q} W| S(   Ni   i   i   i    c         C   s   |  d i d j S(   Nx0.00.0i    (   R   RD   (   R   (    (    R   R(   ~  s    c         C   s   |  t |   j S(   N(   R   R   (   R   (    (    R   R(     s    (   i   i   i   i    (   t   applyt   cubt   mapR   RW   R@   t   filtert   niceR   R   R   (   R   R   R   R   R@   R   (    (    R   t   __cubic_rooty  s       f9.9999999999999995e-07c         C   sv  |  i d j  o t d  n |  i d j  p |  i d j o |  i   o |  i   Sn |  i d  d j o d Sn |  i i o" | d i	 d j o | d } n |  i
 |  |  } } x t |  | j o | |  } | o | | d | } nh y | | } WnL t j
 o@ |  i p
 |  i o d d } n d } | } | | } n X| | } |  |  } q W| S(
   s  Find an input which self maps to zero.

        Optional arguments:

          guess -- where to start searching [default: 0]
          tol -- tolerance [default: 1e-6]; if abs(self(x)) < tol, x is a root

        These are ignored if self is a quadratic or a real cubic; cardan.py's
        tools then provide for exact solution.  Otherwise, Newton-Raphson is
        deployed.  If self is everywhere or nowhere zero, ValueError is raised;
        this can only happen for constant polynomials. i   s6   constant function is either nowhere or everywhere zeroi   i    x0.00.0x0.01.0f1.0iN(   R   R4   R   R   t   _Polynomial__cubic_rootRW   t
   normalisedR   t   guessRD   R   t   gradet   offR   t   tolt   dt   wobblet	   NameErrorR   t   spin(   R   R   R   R   R   R   R   R   (    (    R   t	   seek_root  s4     - !    
c         C   sh  |  i d j  o |  i Sn y | i d j  o
 d  } n Wn t j
 o d  } n X| d  j o y |  i   } Wn; t j
 o/ |  i d j o
 |  i	 p t
  |  i Sn X|  i	 ol yR | i oD t h  d d <d d | i <d | i d | i d < } n Wq&t j
 o q&Xn | d  j o# t h  d d <d | < } qZn d |  i } t | i  } x |  | }	 |	 i d j  o | Sn |	 i | j  o | Sn d t |	 i | i  } g  } xs | D]k } | t h  | | < } |	 |  | | } g  }
 x$ | D] } |
 i | i |   qW| i |
  qWt d  | |	 | } qyWd  S(   Ni   i   ii    f9.9999999999999995e-07f0.001s   Need to divide vector by matrix(   R   R4   R   R   R   R   R   R   R   R   R   RD   R   RE   Rw   t   tinyt   ranget   indexRu   R   t   smallt   matrixR#   Rt   R   R   t   jRQ   RW   t   NotImplementedError(   R   R   R   R   R#   R   R   Rt   R   Ru   R   R   R   (    (    R   t   seek_factor  sV        !

H 	'  
    	c         C   s   t  t t |  i i     S(   N(   R.   R   R   R   R   R   (   R   R/   (    (    R   t   _lazy_get__bigcoef_  s    i   c         C   sH   |  i | j o d  Sn |  i d j  p |  i | d j  o d Sn d  S(   Ni    f9.9999999999999995e-07i   (   R   R4   t   maxrankR   Rw   R6   (   R   R6   R   (    (    R   t   __istiny  s
     $ c         C   s  g  } xT|  i d j oC|  i \ } } | oe | oZ t | i | i    } | i d j o. | i
 } | t | i  |  | } }  q	 q qnq| i   } t | d i | d  } | i d j o6 | i
 } | t d | i  |  | | } }  q	 n t |  d j o | i   \ \ } } \ }	 } | d j  o
 | j  n o" |	 | | | f \ } } }	 } n | d j  o
 | j  n oq | d | d } } | |	 | | i
 | |	 | | i
 } }	 | t | i |	 i  |  | |	 } }  q	 qn |  i   } xE t |  |  \ }
 } | i |  i  p Pn | i |  |
 }  qWq	 W|  d j o | i |  i d   n t |  S(   Ni    i   i   f0.5(   R@   R   R4   R   R)   R   R   R3   R-   t   fR   t   listt   factorsR   RP   R   t   XRI   t   YR   R   R   t   newRv   Rw   RQ   RW   Rh   (   R   R/   RI   R   R   R@   R)   R   R   R   R   R   (    (    R   t   _lazy_get_factors_  sJ     		& "+)    (K   R   R   R   R   t   typest   IntTypet   ComplexTypet	   FloatTypet   LongTypeR   R   R   R   R0   R3   R5   R7   RA   t   __str__R:   RH   R<   R^   R]   R`   Ra   Rb   Rd   Re   Rj   R   t   __radd__Rn   Ro   R   t   __rmul__Rx   t   __floordiv__Ry   R|   Rs   R   R   R   RW   R   R   R   R   R   R   R   R   R   R   t   DictionaryTypeR   R   R   R   R   R   R   R   R   R   t   cardant   cubicR   R   R   R   R   Rv   R   (    (    (    R   R      s   ) 	5$
													
		
				'	
	!+																		-.	(   R   R   t   study.snake.lazyt   LazyR   R    R   R   (   R   R   R   R   R    (    (    R   t   ?   s   	   