
~9\c           @  s  d  Z  d d l m Z m Z d d l m Z d d l m Z m Z m	 Z	 d d l
 m Z d d l m Z d d l m Z d d l m Z m Z d d	 l m Z d d
 l m Z d d l m Z d d l m Z d d l m Z d d l m Z m Z m  Z  m! Z! d d l" m# Z# d d l$ m% Z% m& Z& m' Z' d d l( m) Z) m* Z* m+ Z+ d d l, m- Z- m. Z. d d l/ m0 Z0 d d l1 m2 Z2 d d l3 m4 Z4 d d l5 m6 Z6 d d l7 m8 Z8 d e9 d  Z: d   Z; d d  Z< d   Z= d   Z> d   Z? d   Z@ d    ZA d!   ZB d"   ZC d#   ZD d$   ZE d%   ZF d&   ZG d'   ZH d(   ZI d)   ZJ d*   ZK d+   ZL d,   ZM d d-  ZN d.   ZO d/ d0 eP d eP e9 d1  ZQ d2 e8 f d3     YZR eS d/ d0 eP d eP e9 d4  ZT d5 S(6   s   Formal Power Seriesi(   t   print_functiont   division(   t   defaultdict(   t   oot   zoot   nan(   t   Add(   t   iterable(   t   Expr(   t
   Derivativet   Function(   t   Mul(   t   Rational(   t   Eq(   t   Interval(   t   S(   t   Wildt   Dummyt   symbolst   Symbol(   t   sympify(   t   binomialt	   factorialt   rf(   t   floort   fract   ceiling(   t   Mint   Max(   t	   Piecewise(   t   Limit(   t   Order(   t   sequence(   t
   SeriesBasei   c         C  s  d d l  m } m } d d l m } |  } g  }	 xt | d  D]t}
 |
 ra | j |  } n  | j |  rt j	 t j	 } } | | | d | } | j
 |  r | j   } n  x)t j |  D]} | j   \ } } | j
 |  s | | 7} q t | t  r3| j |  } | d } | | d } n  | j   \ } } | j |  \ } } | sp| | 7} q n  | d j |  } | | } | | | } d | | t | | d |  j t  | | | } | | 7} q W| t j	 k rd S| j
 |  s1| j
 t  s1| j
 t  s1| j
 t  r5d SxU t |
  D]G } | | | d } | | |  } | |	 j   | j | d  7} qBW| j | | |
  | |
 f S|	 j |  qC Wd S(   s?  Rational algorithm for computing
    formula of coefficients of Formal Power Series
    of a function.

    Applicable when f(x) or some derivative of f(x)
    is a rational function in x.

    :func:`rational_algorithm` uses :func:`apart` function for partial fraction
    decomposition. :func:`apart` by default uses 'undetermined coefficients
    method'. By setting ``full=True``, 'Bronstein's algorithm' can be used
    instead.

    Looks for derivative of a function up to 4'th order (by default).
    This can be overridden using order option.

    Returns
    =======

    formula : Expr
    ind : Expr
        Independent terms.
    order : int

    Examples
    ========

    >>> from sympy import log, atan, I
    >>> from sympy.series.formal import rational_algorithm as ra
    >>> from sympy.abc import x, k

    >>> ra(1 / (1 - x), x, k)
    (1, 0, 0)
    >>> ra(log(1 + x), x, k)
    (-(-1)**(-k)/k, 0, 1)

    >>> ra(atan(x), x, k, full=True)
    ((-I*(-I)**(-k)/2 + I*I**(-k)/2)/k, 0, 1)

    Notes
    =====

    By setting ``full=True``, range of admissible functions to be solved using
    ``rational_algorithm`` can be increased. This option should be used
    carefully as it can significantly slow down the computation as ``doit`` is
    performed on the :class:`RootSum` object returned by the ``apart`` function.
    Use ``full=False`` whenever possible.

    See Also
    ========

    sympy.polys.partfrac.apart

    References
    ==========

    .. [1] Formal Power Series - Dominik Gruntz, Wolfram Koepf
    .. [2] Power Series in Computer Algebra - Wolfram Koepf
    i(   t   RootSumt   apart(   t	   integratei   t   fulli    N(    t   sympy.polysR"   R#   t   sympy.integralsR$   t   ranget   difft   is_rational_functionR   t   Zerot   hast   doitR   t	   make_argst   as_numer_denomt
   isinstanceR   t   as_independentt   as_base_expt   as_coeff_addt   coeffR   t   rewriteR   t   NoneR   R   R   t   popt   limitt   subst   append(   t   ft   xt   kt   orderR%   R"   R#   R$   R)   t   dst   iR4   t   sept   termst   tt   numt   dent   indt   jt   at   xtermt   xct   ak(    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   rational_algorithm   sV    ;

)-$c   	      C  s   |  s
 g  S|  d d !} x |  d D] } | j  |  d } xm t |  D]R \ } } | j  |  d } | | j   } | j |  rH | | c | 7<PqH qH W| j |  q" W| S(   s  Returns a list of all the rationally independent terms.

    Examples
    ========

    >>> from sympy import sin, cos
    >>> from sympy.series.formal import rational_independent
    >>> from sympy.abc import x

    >>> rational_independent([cos(x), sin(x)], x)
    [cos(x), sin(x)]
    >>> rational_independent([x**2, sin(x), x*sin(x), x**3], x)
    [x**3 + x**2, x*sin(x) + sin(x)]
    i    i   (   R1   t	   enumeratet   cancelR*   R:   (	   RB   R<   RF   RC   t   nR@   t   termt   dt   q(    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   rational_independent   s    c      	   #  s]  d d l  m } t d |         f d   } | |  \ } } t } xt d | d  D] }	 | |	  \ } } | j   } | j   }
 t |
   } | s t |  |	 k rd t	 t
   d   | |   |	   D   } | rt } | j |  } n  | j   d } | j   j t  d d } | j t      |	 f Vqd qd Wd S(	   s^  Generates simple DE.

    DE is of the form

    .. math::
        f^k(x) + \sum\limits_{j=0}^{k-1} A_j f^j(x) = 0

    where :math:`A_j` should be rational function in x.

    Generates DE's upto order 4 (default). DE's can also have free parameters.

    By increasing order, higher order DE's can be found.

    Yields a tuple of (DE, order).
    i(   t   linsolves   a:%dc           s    j   |   t g  t d |   D]  }   |  j   |  ^ q"   }    j   |   t g  t d |   D]& }   |    j   |  ^ qt   } | | f S(   Ni    (   R)   R   R(   (   R=   R@   t   eqt   DE(   RH   R;   t   gR<   (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   _makeDE   s    LXi   c         s  s"   |  ] } | D] } | Vq q d  S(   N(    (   t   .0t   sR@   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pys	   <genexpr>   s    i    N(   t   sympy.solvers.solvesetRT   R   t   FalseR(   t   expandt   as_ordered_termsRS   t   lent   dictt   zipt   TrueR9   R/   t   factort   as_coeff_mulR	   t   collect(   R;   R<   RW   R>   RT   RX   RU   RV   t   foundR=   RB   RF   t   sol(    (   RH   R;   RW   R<   s2   lib/python2.7/site-packages/sympy/series/formal.pyt   simpleDE   s$    ,c   
      C  s   t  j } |  j t  j   } d } x t j |   D]u } | j |  \ } } t	 | t
  rj | j }	 n d }	 | d k s |	 | k  r |	 } n  | | | | |	  7} q4 W| r | j | | |  } n  | S(   sz  Converts a DE with constant coefficients (explike) into a RE.

    Performs the substitution:

    .. math::
        f^j(x) \to r(k + j)

    Normalises the terms so that lowest order of a term is always r(k).

    Examples
    ========

    >>> from sympy import Function, Derivative
    >>> from sympy.series.formal import exp_re
    >>> from sympy.abc import x, k
    >>> f, r = Function('f'), Function('r')

    >>> exp_re(-f(x) + Derivative(f(x)), r, k)
    -r(k) + r(k + 1)
    >>> exp_re(Derivative(f(x), x) + Derivative(f(x), (x, 2)), r, k)
    r(k) + r(k + 1)

    See Also
    ========

    sympy.series.formal.hyper_re
    i    N(   R   R+   t   atomsR
   R7   R6   R   R.   R1   R0   R	   t   derivative_countR9   (
   RV   t   rR=   t   RERW   t   miniRC   R4   RQ   RG   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   exp_re   s    		c         C  sJ  t  j } |  j t  j   } | j t  j   } d } x t j |  j	    D] } | j
 |  \ } }	 | j
 |  \ }
 } | j |  d } t |	 t  r |	 j } n d } | |
 t | d | |  | | | |  7} | d k s | | | k  rO | | } qO qO W| j | | |  } t d  } | j | | |   S(   s}  Converts a DE into a RE.

    Performs the substitution:

    .. math::
        x^l f^j(x) \to (k + 1 - l)_j . a_{k + j - l}

    Normalises the terms so that lowest order of a term is always r(k).

    Examples
    ========

    >>> from sympy import Function, Derivative
    >>> from sympy.series.formal import hyper_re
    >>> from sympy.abc import x, k
    >>> f, r = Function('f'), Function('r')

    >>> hyper_re(-f(x) + Derivative(f(x)), r, k)
    (k + 1)*r(k + 1) - r(k)
    >>> hyper_re(-x*f(x) + Derivative(f(x), (x, 2)), r, k)
    (k + 2)*(k + 3)*r(k + 3) - r(k)

    See Also
    ========

    sympy.series.formal.exp_re
    i   i    t   mN(   R   R+   Ri   R
   R7   R   R6   R   R.   R]   R1   t   as_coeff_exponentR0   R	   Rj   R   R9   R   Re   (   RV   Rk   R=   Rl   RW   R<   Rm   RC   R4   RQ   t   ct   vt   lRG   Ro   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   hyper_re  s"    	1c         C  sK   |  | | 9}  | j  | | |  } | j  | | |  } |  | | | f S(   N(   R9   (   R;   R<   t   Pt   QR=   Ro   t   shift(    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   _transformation_aH  s    c         C  s\   |  j  | | |  }  | j  | | |  } | j  | | |  } | | 9} |  | | | f S(   N(   R9   (   R;   R<   Ru   Rv   R=   Ro   t   scale(    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   _transformation_cO  s
    
c         C  s_   |  j  |  }  | j | | d  | | d } | j | | d  | d } |  | | | f S(   Ni   (   R)   R9   (   R;   R<   Ru   Rv   R=   Ro   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   _transformation_eW  s    "c         C  s'   g  |  D] \ } } | | | f ^ q S(   N(    (   Rg   Rw   t   rest   cond(    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   _apply_shift^  s    c         C  s'   g  |  D] \ } } | | | f ^ q S(   N(    (   Rg   Ry   R|   R}   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   _apply_scaleb  s    c         C  sF   g  |  D]; \ } } | | d | j    d j |  | d f ^ q S(   Ni   (   t   as_coeff_AddR4   (   Rg   R<   R=   R|   R}   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   _apply_integratef  s    c         C  s  d d l  m } g  } xt | d | | d  D]{}	 |	 d k  t k rP q2 n  |  j | |	  j | d  t |	  }
 |
 t j k r q2 n  | | |	 } |
 } | j	 | |  } | j	 | |  } | j	 | d |  j
 |  d } | j	 | d |  j
 |  d } | | | | 9} x; | | |  j   D]$ \ }
 } | t |
 |  | 9} q4Wx; | | |  j   D]$ \ }
 } | t |
 |  | } qrW| j | | f  q2 W| S(   s   Computes the formula for f.i(   t   rootsi   i    (   R&   R   R(   Rb   R)   R8   R   R   R+   R9   t   leadtermt   itemsR   R:   (   R;   R<   Ru   Rv   R=   Ro   t   k_maxR   Rg   R@   Rk   t   ktermR|   t   pRR   t   c1t   c2t   mul(    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   _compute_formulak  s*    "(##""c      	   C  s  d d l  m } m } d d l m } | | |  | | |  }	 }
 t |	  } | j |
  | g  | j   D]% \ } } | j rn | j	   d ^ qn  } t
 |  | | | | | |  \ }  } } } | | |  }
 |
 r t |
 j     } n	 t j } | | } t |  | | | | | |  \ }  } } } | |  j | d  } t | t  rc| d k rcd S| | |  }
 |
 rt |
 j     } n	 t j } t j t } } xvt | | d  D]`} |  j | |  j | d  t |  } | j t k r|  } t |  | | | | | |  \ }  } } } t |  | | | | |  \ }  } } } t |  | | | | |  \ } } } t | | |  } t | |  } | | |  } | | | j | d  7} | d 7} | | | f S| r| | | | | 7} t | | |  } | | k r| } qqqW| j  | | d |  } t! |  | | | | | |  } t | |  } t" | |  } | | | f S(   s   Recursive wrapper to rsolve_hypergeometric.

    Returns a Tuple of (formula, series independent terms,
    maximum power of x in independent terms) if successful
    otherwise ``None``.

    See :func:`rsolve_hypergeometric` for details.
    i(   t   lcmR   (   R$   i   i    N(#   R&   R   R   R'   R$   R`   t   updateR   t   is_rationalR/   Rz   R   t   keysR   R+   Rx   R8   R0   R   R6   R   R   R(   R)   R   t	   is_finiteR\   R{   t   _rsolve_hypergeometricR   R~   R   R9   R   R   (   R;   R<   Ru   Rv   R=   Ro   R   R   R$   t   prootst   qrootst	   all_rootsRk   RC   Ry   t   k_minRw   Rs   R   RF   t   mpR@   t   old_fRg   t   pow_x(    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyR     sZ    	"*	
*	(*'$
c         C  s  t  |  | | | | |  } | d k r+ d S| \ } } }	 t d    }
 x | D] \ } } | j   \ } } | j |  } | j t k r | | t |  9} t |  } n  | j	 | | | |  } t
 | | | |  } |
 | c | 7<qP Wg  } x- |
 j   D] \ } } | j | | f  qW| j t j t f  t |   } |	 t k ret j } n( |	 j t k rt |	  } n
 |	 d } | d k  r| t t | | | | | d f   7} t j } n  | | | f S(   s  Solves RE of hypergeometric type.

    Attempts to solve RE of the form

    Q(k)*a(k + m) - P(k)*a(k)

    Transformations that preserve Hypergeometric type:

        a. x**n*f(x): b(k + m) = R(k - n)*b(k)
        b. f(A*x): b(k + m) = A**m*R(k)*b(k)
        c. f(x**n): b(k + n*m) = R(k/n)*b(k)
        d. f(x**(1/m)): b(k + 1) = R(k*m)*b(k)
        e. f'(x): b(k + m) = ((k + m + 1)/(k + 1))*R(k + 1)*b(k)

    Some of these transformations have been used to solve the RE.

    Returns
    =======

    formula : Expr
    ind : Expr
        Independent terms.
    order : int

    Examples
    ========

    >>> from sympy import exp, ln, S
    >>> from sympy.series.formal import rsolve_hypergeometric as rh
    >>> from sympy.abc import x, k

    >>> rh(exp(x), x, -S.One, (k + 1), k, 1)
    (Piecewise((1/factorial(k), Eq(Mod(k, 1), 0)), (0, True)), 1, 1)

    >>> rh(ln(1 + x), x, k**2, k*(k + 1), k, 1)
    (Piecewise(((-1)**(k - 1)*factorial(k - 1)/RisingFactorial(2, k - 1),
     Eq(Mod(k, 1), 0)), (0, True)), x, 2)

    References
    ==========

    .. [1] Formal Power Series - Dominik Gruntz, Wolfram Koepf
    .. [2] Power Series in Computer Algebra - Wolfram Koepf
    c           S  s   t  j S(   N(   R   R+   (    (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   <lambda>  t    i   i    iN(   R   R6   R   R   R4   t
   is_integerR\   R   R   R9   R   R   R:   R   R+   Rb   R   R   R   t   sumR    (   R;   R<   Ru   Rv   R=   Ro   t   resultt   sol_listRF   R   t   sol_dictR|   R}   RG   t   mkRq   Rg   RZ   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   rsolve_hypergeometric  s8    -
*c   
      C  s   t  j |  } t |  d k r t | j t   } t | j |  \ } } | d j d | d j d }	 |	 d k  r | | } } t	 |	  }	 n  t
 |  | | | | |	  Sd S(   s;   See docstring of :func:`rsolve_hypergeometric` for details.i   i   i    N(   R   R.   R_   t   listRi   R
   t   mapR4   t   argst   absR   (
   R;   R<   Rl   RW   R=   RB   t   gsRu   Rv   Ro   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   _solve_hyper_RE$  s     c         C  s	  d d l  m } x9 t j |  D]( } | j |  \ } } | j r  d Sq  Wt | | |  }	 i  }
 xc t t t j |	    D]F } | r |  j	 |  }  n  |  j
 | d  |
 | |  j | |  <q W| |	 | |  |
  } | r| t |  t j t j f Sd S(   s%   Solves DE with constant coefficients.i(   t   rsolveNi    (   t   sympy.solversR   R   R.   R1   t   free_symbolsRn   R(   R_   R)   R8   R9   R   R   R+   (   R;   R<   RV   RW   R=   R   RC   R4   RQ   Rl   t   initR@   Rg   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   _solve_explike_DE2  s    	",c   
      C  s   d d l  m } t | | |  } i  } xm t t t j |    D]P } | rb |  j |  }  n  |  j | d  t	 |  | | |  j
 | |  <qD W| | | |  |  }	 |	 r |	 t j t j f Sd S(   s4   Converts DE into RE and solves using :func:`rsolve`.i(   R   i    N(   R   R   Rt   R(   R_   R   R.   R)   R8   R   R9   R   R+   (
   R;   R<   RV   RW   R=   R   Rl   R   R@   Rg   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   _solve_simpleI  s    "6c         C  s  d d l  m } g  } |  j t | |  | |   } xu t |  D]g } |  j t | |  | |   }	 |	 | j   j |  }	 x$ t j |	  D] }
 | j	 |
  q WqD Wg  } xF | D]8 } | j
 |  r Pq | j
 t  r | j	 |  q q W| } | rt t | d   | | t |   D   } | r|  j |  }  |  j   j t  d d }  |  j t | |    }  qn  |  S(   sD   Converts DE with free parameters into DE with constant coefficients.i(   RT   c         s  s"   |  ] } | D] } | Vq q d  S(   N(    (   RY   RZ   R@   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pys	   <genexpr>o  s    i   i    (   R[   RT   R4   R	   R(   R]   Re   R   R.   R:   R,   R   R`   Ra   R   R9   Rc   Rd   (   RV   RW   R<   R>   t   symsRT   RU   t   highest_coeffR@   R4   RC   t   tempt   eRg   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   _transform_explike_DE[  s,    !!.!c         C  sN  d d l  m } t |  | |  } g  } x= t d |  D], } | j | | |   }	 | j |	  q8 Wt t | d   | | t |   D   }
 |
 rJt	 d  } | j
 |
  } | j   j   d j | | |   } | j |  d d } xP t |  D]? } | j | | |   r| r| j
 | | |  } PqqWn  | S(   s@   Converts DE with free parameters into RE of hypergeometric type.i(   RT   i   c         s  s"   |  ] } | D] } | Vq q d  S(   N(    (   RY   RZ   R@   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pys	   <genexpr>  s    Ro   i    (   R[   RT   Rt   R(   R4   R:   R`   Ra   R   R   R9   Rc   R/   Re   Rd   (   RV   RW   R=   R>   R   RT   Rl   RU   R@   R4   Rg   Ro   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   _transform_DE_REw  s"    .)c   	      C  s   d } | j j | | h  } | r? t | | | | |  } n t | | |  } | j j | h  s t |  | | | |  } n  | r | S| r t | | | | |  } n  | j j | h  s t |  | | | |  } n  | r | Sd S(   s  Solves the DE.

    Tries to solve DE by either converting into a RE containing two terms or
    converting into a DE having constant coefficients.

    Returns
    =======

    formula : Expr
    ind : Expr
        Independent terms.
    order : int

    Examples
    ========

    >>> from sympy import Derivative as D, Function
    >>> from sympy import exp, ln
    >>> from sympy.series.formal import solve_de
    >>> from sympy.abc import x, k
    >>> f = Function('f')

    >>> solve_de(exp(x), x, D(f(x), x) - f(x), 1, f, k)
    (Piecewise((1/factorial(k), Eq(Mod(k, 1), 0)), (0, True)), 1, 1)

    >>> solve_de(ln(1 + x), x, (x + 1)*D(f(x), x, 2) + D(f(x)), 2, f, k)
    (Piecewise(((-1)**(k - 1)*factorial(k - 1)/RisingFactorial(2, k - 1),
     Eq(Mod(k, 1), 0)), (0, True)), x, 2)
    N(   R6   R   t
   differenceR   Rt   R   R   R   (	   R;   R<   RV   R>   RW   R=   Rg   R   Rl   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   solve_de  s    c   	      C  s   t  d  } g  } d } x| t |  | | |  D]e \ } } | d k	 rd t |  | | | | |  } n  | rn | S| j j | h  s. | j |  q. q. Wx0 | D]( } t |  | | | |  } | r | Sq Wd S(   s  Hypergeometric algorithm for computing Formal Power Series.

    Steps:
        * Generates DE
        * Convert the DE into RE
        * Solves the RE

    Examples
    ========

    >>> from sympy import exp, ln
    >>> from sympy.series.formal import hyper_algorithm

    >>> from sympy.abc import x, k

    >>> hyper_algorithm(exp(x), x, k)
    (Piecewise((1/factorial(k), Eq(Mod(k, 1), 0)), (0, True)), 1, 1)

    >>> hyper_algorithm(ln(1 + x), x, k)
    (Piecewise(((-1)**(k - 1)*factorial(k - 1)/RisingFactorial(2, k - 1),
     Eq(Mod(k, 1), 0)), (0, True)), x, 2)

    See Also
    ========

    sympy.series.formal.simpleDE
    sympy.series.formal.solve_de
    RW   N(   R
   R6   Rh   R   R   R   R:   R   (	   R;   R<   R=   R>   RW   t   desRg   RV   R@   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   hyper_algorithm  s    "c      
   C  s  | t  j t  j g k r | t  j k r1 t  j n t  j } |  j | d |  } t | | d | | | | |  }	 |	 d k r d S|	 d |	 d j | d |  |	 d j | d |  f S| s | t  j k r| t  j k r | | }
 | } | } n | | }
 | } | } |  j | |
  } t | | d t  j | | | |  }	 |	 d k rYd S|	 d |	 d j | | |  |	 d j | | |  f S|  j |  rd St |  t  r t	 }	 t
 t  j d t f  } t  j d } } x$t j |   D]} t | | d t  j | | | |  } | r|	 s;t }	 | d } n  | d j | j k rq| } | j | d j } }  n! | d } | d j | j } }  t g  t | d |  | !| | |  ! D] } | d | d ^ q  } | | d 7} | | d | 7} q| | 7} qW|	 r| | | f Sd Sd }	 t d  } | rSt |  | | | |  }	 n  |	 d k r}| r}t |  | | |  }	 n  |	 d k rd St
 |	 d | |	 d t f  } t
 | | | d t f  } |	 d } | | | f S(   sP   Recursive wrapper to compute fps.

    See :func:`compute_fps` for details.
    i   i    i   R=   N(   R   t   Infinityt   OneR9   t   _compute_fpsR6   t   is_polynomialR0   R   R\   R    R+   R   R.   Rb   t   startRa   R   RL   R   (   R;   R<   t   x0t   dirt   hyperR>   t   rationalR%   R   R   t   rept   rep2t   rep2bRK   RF   t   xkRC   R|   t   seqRZ   t   zt   saveR=   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyR     sr    "!9	
$$
F 
i    i   c      	   C  s   t  |   }  t  |  } |  j |  s+ d St  |  } | d k rO t j } nM | d k rh t j } n4 | t j t j g k r t d   n t  |  } t |  | | | | | | |  S(   s  Computes the formula for Formal Power Series of a function.

    Tries to compute the formula by applying the following techniques
    (in order):

    * rational_algorithm
    * Hypergeomitric algorithm

    Parameters
    ==========

    x : Symbol
    x0 : number, optional
        Point to perform series expansion about. Default is 0.
    dir : {1, -1, '+', '-'}, optional
        If dir is 1 or '+' the series is calculated from the right and
        for -1 or '-' the series is calculated from the left. For smooth
        functions this flag will not alter the results. Default is 1.
    hyper : {True, False}, optional
        Set hyper to False to skip the hypergeometric algorithm.
        By default it is set to False.
    order : int, optional
        Order of the derivative of ``f``, Default is 4.
    rational : {True, False}, optional
        Set rational to False to skip rational algorithm. By default it is set
        to True.
    full : {True, False}, optional
        Set full to True to increase the range of rational algorithm.
        See :func:`rational_algorithm` for details. By default it is set to
        False.

    Returns
    =======

    ak : sequence
        Sequence of coefficients.
    xk : sequence
        Sequence of powers of x.
    ind : Expr
        Independent terms.
    mul : Pow
        Common terms.

    See Also
    ========

    sympy.series.formal.rational_algorithm
    sympy.series.formal.hyper_algorithm
    t   +t   -s   Dir must be '+' or '-'N(   R   R,   R6   R   R   t
   ValueErrorR   (   R;   R<   R   R   R   R>   R   R%   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   compute_fpsC  s    3t   FormalPowerSeriesc           B  s[  e  Z d  Z d   Z e d    Z e d    Z e d    Z e d    Z e d    Z	 e d    Z
 e d    Z e d	    Z e d
    Z e d    Z e d    Z e d    Z d   Z d d  Z d d  Z d   Z d   Z d   Z d   Z d d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z  RS(   s  Represents Formal Power Series of a function.

    No computation is performed. This class should only to be used to represent
    a series. No checks are performed.

    For computing a series use :func:`fps`.

    See Also
    ========

    sympy.series.formal.fps
    c         G  s   t  t |  } t j |  |  S(   N(   R   R   R   t   __new__(   t   clsR   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyR     s    c         C  s   |  j  d S(   Ni    (   R   (   t   self(    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   function  s    c         C  s   |  j  d S(   Ni   (   R   (   R   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyR<     s    c         C  s   |  j  d S(   Ni   (   R   (   R   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyR     s    c         C  s   |  j  d S(   Ni   (   R   (   R   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyR     s    c         C  s   |  j  d d S(   Ni   i    (   R   (   R   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyRK     s    c         C  s   |  j  d d S(   Ni   i   (   R   (   R   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyR     s    c         C  s   |  j  d d S(   Ni   i   (   R   (   R   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyRF     s    c         C  s   t  d t  S(   Ni    (   R   R   (   R   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   interval  s    c         C  s
   |  j  j S(   N(   R   t   inf(   R   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyR     s    c         C  s
   |  j  j S(   N(   R   t   sup(   R   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   stop  s    c         C  s   t  S(   N(   R   (   R   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   length  s    c         C  sc   d d l  m } |  j |  j } } | j d } | | j | j | | j | j f  } |  j | S(   s0   Returns an infinite representation of the seriesi(   t   Sumi    (	   t   sympy.concreteR   RK   R   t	   variablest   formulaR   R   RF   (   R   R   RK   R   R=   t   inf_sum(    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   infinite  s
    (c         C  s?   | j  |  j  d j   \ } } | j |  j  s; t j S| S(   s!   Returns the power of x in a term.i   (   R1   R<   R2   R,   R   R+   (   R   RP   RI   R   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt
   _get_pow_x  s    "i   c         C  s   g  } x{ t  |   D]m \ } } |  j |  } | | k r> Pq | j t k ra | | d k ra Pq | t j k	 r | j |  q q Wt |   S(   s   Truncated series as polynomial.

        Returns series sexpansion of ``f`` upto order ``O(x**n)``
        as a polynomial(without ``O`` term).
        i   (   RM   R   R   Rb   R   R+   R:   R   (   R   RO   RB   R@   RC   t   xp(    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt
   polynomial  s    c         C  sv   | d k r t |   S|  j |  j } } |  j j |  } | t j k rV t j } n  |  j	 |  t
 | | | f  S(   s   Truncated series.

        Returns truncated series expansion of f upto
        order ``O(x**n)``.

        If n is ``None``, returns an infinite iterator.
        N(   R6   t   iterR<   R   R   R4   R   t   NegativeInfinityR   R   R   (   R   RO   R<   R   t   pt_xk(    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   truncate  s    
c         C  s   y. |  j  j |  } |  j j |  j   } Wn t k
 rJ t j } n X| | } |  j r t j } xw t j	 |  j  D]c } |  j
 |  } | d k r | d k  r | | 7} qz | | k rz | | d k  rz | | 7} qz qz W| | 7} n  | j |  j  S(   Ni    i   (   R   R4   RK   t   simplifyt
   IndexErrorR   R+   RF   R   R.   R   Re   R<   (   R   t   ptR   t   pt_akRP   RF   RC   R   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt
   _eval_term  s     
		c         C  s    |  j  } | j |  r |  Sd  S(   N(   R<   R,   (   R   t   oldt   newR<   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt
   _eval_subs  s    	c         C  s(   x! |  D] } | t  j k	 r | Sq Wd  S(   N(   R   R+   (   R   R<   RC   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   _eval_as_leading_term  s    c         C  s  |  j  j |  } |  j j |  } |  j |  j j  } |  j } | j d } | j j |  rg  } xq | j j	 D]c \ } }	 t
 j }
 x8 t j |  D]' } |  j |  } |
 | | | 7}
 q W| j |
 |	 f  qt Wt |   } t | j | | d  | | j d | j f  } n9 t | j | j | | d  | | j d | j f  } |  j | |  j |  j |  j | |  j | f  S(   Ni    i   (   R   R)   RF   R   R   R   RK   R   R,   R   R   R+   R   R.   R:   R   R    R9   R   R   t   funcR<   R   R   (   R   R<   R;   RF   t   pow_xkRK   R=   t   formR   Rq   R   RC   R   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   _eval_derivative  s$    		5c         K  s  d d l  m } | d k r( |  j } n t |  rD | |  j |  S| |  j |  } | |  j |  } | | | j | d  7} |  j |  j	 j
  } |  j } | j d } | j
 j |  r~g  }	 xu | j
 j D]g \ }
 } t j } x< t j |
  D]+ } |  j |  } | | | | d 7} q W|	 j | | f  q Wt |	   }	 t |	 j | | d  | | j d | j f  } n= t | j
 | d j | | d  | | j d | j f  } |  j | |  j |  j |  j | |  j	 | f  S(   sB  Integrate Formal Power Series.

        Examples
        ========

        >>> from sympy import fps, sin, integrate
        >>> from sympy.abc import x
        >>> f = fps(sin(x))
        >>> f.integrate(x).truncate()
        -1 + x**2/2 - x**4/24 + O(x**6)
        >>> integrate(f, (x, 0, 1))
        1 - cos(1)
        i(   R$   i    i   N(   R'   R$   R6   R<   R   R   RF   R8   R   R   R   RK   R   R,   R   R   R+   R   R.   R:   R   R    R9   R   R   R   R   R   (   R   R<   t   kwargsR$   R;   RF   R   RK   R=   R   R   Rq   R   RC   R   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyR$   2  s0    		5!c         C  s  t  |  } t | t  r|  j | j k r< t d   n! |  j | j k r] t d   n  |  j | j } } |  j | j j | |  } |  j | j	 k r | S|  j
 | j
 } |  j
 j | j
 j k r | j
 } | j
 j |  j
 j } } n" |  j
 } |  j
 j | j
 j } } t g  t | d | | !|  j | | ! D] }	 |	 d |	 d ^ q9  }
 |  j | j |
 } |  j | | |  j |  j | |  j | f  S| j |  j  s|  j | } |  j | } |  j | |  j |  j |  j |  j
 |  j | f  St |  |  S(   Ns9   Both series should be calculated from the same direction.s6   Both series should be calculated about the same point.i    i   (   R   R0   R   R   R   R   R<   R   R9   R   RK   R   R   Ra   R   RF   R   R,   (   R   t   otherR<   t   yR;   RK   R   RZ   R   R   R   RF   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   __add__^  s2    		I+c         C  s   |  j  |  S(   N(   R   (   R   R   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   __radd__  s    c         C  s:   |  j  |  j |  j |  j |  j |  j |  j |  j f  S(   N(   R   R   R<   R   R   RK   R   RF   (   R   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   __neg__  s    c         C  s   |  j  |  S(   N(   R   (   R   R   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   __sub__  s    c         C  s   |  j  |  S(   N(   R   (   R   R   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   __rsub__  s    c         C  s   t  |  } | j |  j  r+ t |  |  S|  j | } |  j j |  } |  j | } |  j | |  j |  j	 |  j
 | |  j | f  S(   N(   R   R,   R<   R   R   RK   t	   coeff_mulRF   R   R   R   R   (   R   R   R;   RK   RF   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   __mul__  s    c         C  s   |  j  |  S(   N(   R   (   R   R   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   __rmul__  s    N(!   t   __name__t
   __module__t   __doc__R   t   propertyR   R<   R   R   RK   R   RF   R   R   R   R   R   R   R   R   R   R   R   R   R6   R$   R   R   R   R   R   R   R   (    (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyR     s:   	
					,	&					c   
   	   C  s   t  |   }  | d k r[ |  j } t |  d k rB | j   } q[ | sL |  St d   n  t |  | | | | | | |  }	 |	 d k r |  St |  | | | |	  S(   sC  Generates Formal Power Series of f.

    Returns the formal series expansion of ``f`` around ``x = x0``
    with respect to ``x`` in the form of a ``FormalPowerSeries`` object.

    Formal Power Series is represented using an explicit formula
    computed using different algorithms.

    See :func:`compute_fps` for the more details regarding the computation
    of formula.

    Parameters
    ==========

    x : Symbol, optional
        If x is None and ``f`` is univariate, the univariate symbols will be
        supplied, otherwise an error will be raised.
    x0 : number, optional
        Point to perform series expansion about. Default is 0.
    dir : {1, -1, '+', '-'}, optional
        If dir is 1 or '+' the series is calculated from the right and
        for -1 or '-' the series is calculated from the left. For smooth
        functions this flag will not alter the results. Default is 1.
    hyper : {True, False}, optional
        Set hyper to False to skip the hypergeometric algorithm.
        By default it is set to False.
    order : int, optional
        Order of the derivative of ``f``, Default is 4.
    rational : {True, False}, optional
        Set rational to False to skip rational algorithm. By default it is set
        to True.
    full : {True, False}, optional
        Set full to True to increase the range of rational algorithm.
        See :func:`rational_algorithm` for details. By default it is set to
        False.

    Examples
    ========

    >>> from sympy import fps, O, ln, atan
    >>> from sympy.abc import x

    Rational Functions

    >>> fps(ln(1 + x)).truncate()
    x - x**2/2 + x**3/3 - x**4/4 + x**5/5 + O(x**6)

    >>> fps(atan(x), full=True).truncate()
    x - x**3/3 + x**5/5 + O(x**6)

    See Also
    ========

    sympy.series.formal.FormalPowerSeries
    sympy.series.formal.compute_fps
    i   s    multivariate formal power seriesN(   R   R6   R   R_   R7   t   NotImplementedErrorR   R   (
   R;   R<   R   R   R   R>   R   R%   t   freeR   (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   fps  s    9	!N(U   R   t
   __future__R    R   t   collectionsR   t   sympyR   R   R   t   sympy.core.addR   t   sympy.core.compatibilityR   t   sympy.core.exprR   t   sympy.core.functionR	   R
   t   sympy.core.mulR   t   sympy.core.numbersR   t   sympy.core.relationalR   t   sympy.sets.setsR   t   sympy.core.singletonR   t   sympy.core.symbolR   R   R   R   t   sympy.core.sympifyR   t(   sympy.functions.combinatorial.factorialsR   R   R   t#   sympy.functions.elementary.integersR   R   R   t(   sympy.functions.elementary.miscellaneousR   R   t$   sympy.functions.elementary.piecewiseR   t   sympy.series.limitsR   t   sympy.series.orderR   t   sympy.series.sequencesR    t   sympy.series.series_classR!   R\   RL   RS   Rh   Rn   Rt   Rx   Rz   R{   R~   R   R   R   R   R   R   R   R   R   R   R   R   R   Rb   R   R   R6   R  (    (    (    s2   lib/python2.7/site-packages/sympy/series/formal.pyt   <module>   sb   "|	!+	/	4								C	W						41	PF 