
    wg<                     n   d 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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  ddl!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- ddl.m/Z/ ddl0m1Z1 ddl2m3Z3 ddl4m5Z5 ddl6m7Z7 ddl8m9Z9 d>dZ:d Z;d?dZ<d Z=d Z>d Z?d Z@d  ZAd! ZBd" ZCd# ZDd$ ZEd% ZFd& ZGd' ZHd( ZId) ZJd* ZKd+ ZLd, ZMd?d-ZNd. ZO	 	 d@d/ZP G d0 d1e      ZQ G d2 d3e7      ZR G d4 d5eR      ZS G d6 d7eS      ZT G d8 d9eS      ZU G d: d;eS      ZVdAd=ZWy<)BzFormal Power Series    )defaultdict)nanoozoo)Add)Expr)
DerivativeFunctionexpand)Mul)Rational)Eq)Interval)S)WildDummysymbolsSymbol)sympify)convolution)binomial	factorialrf)bell)floorfracceiling)MinMax)	Piecewise)Limit)Order)sequence)
SeriesBase)iterablec                    ddl m}m} ddlm} | }g }	t        |dz         D ]H  }
|
r|j                  |      }|j                  |      rt        j                  t        j                  }} ||||      }|j                  |      r|j                         }t        j                  |      D ]  }|j                         \  }}|j                  |      s||z  }-t        |t               r|j#                  |      }|d   }||d   z  }|j%                         \  }}|j'                  |      \  }}|s||z  }|d   j)                  |      }|| z  }|||z  z  }d|z  |z  t+        ||z   dz
  |      j-                  t.              z  |||z   z  z  }||z  } |j0                  r y|j                  |      s?|j                  t2              s*|j                  t4              s|j                  t6              r yt        |
      D ]<  }|||z   dz   z  } |||      }||	j9                         |z
  j;                  |d      z  }> |j=                  |||
z
        ||
fc S |	j?                  |       K y)a  
    Rational algorithm for computing
    formula of coefficients of Formal Power Series
    of a function.

    Explanation
    ===========

    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.

    Parameters
    ==========

    x : Symbol
    order : int, optional
        Order of the derivative of ``f``, Default is 4.
    full : bool

    Returns
    =======

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

    Examples
    ========

    >>> from sympy import log, atan
    >>> 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/((-1)**k*k), 0, 1)

    >>> ra(atan(x), x, k, full=True)
    ((-I/(2*(-I)**k) + I/(2*I**k))/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 :func:`~.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

    r   )RootSumapart	integrate   )fullN) sympy.polysr'   r(   sympy.integralsr*   rangediffis_rational_functionr   Zerohasdoitr   	make_argsas_numer_denom
isinstancer   as_independentas_base_expas_coeff_addcoeffr   rewriter   is_zeror   r   r   poplimitsubsappend)fxkorderr,   r'   r(   r*   r1   dsir<   septermstnumdenindjaxtermxcaks                          X/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/series/formal.pyrational_algorithmrU      sX   R +)D	B519 699Q<D$$Q'3E$-Eyy!

]]5)  ++-Swwqz1HC!#s+!003!!fs1v !__.FC"//2HAu q q*B"HA2q5LC'C-"1q519a088CDa!e*%B RKE7 < }}		!		#%))B-IIcN1X 4!a%!),Q'3--a334 JJq!a%(#q11 IIdOm6p     c                 *   | sg S | dd }| dd D ]  }|j                  |      d   }t        |      D ]L  \  }}|j                  |      d   }||z  j                         }|j                  |      s?||xx   |z  cc<    p |j	                  |        |S )a  
    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)]
    r   r+   N)r9   	enumeratecancelr2   rB   )	rJ   rD   rN   rK   nrH   termdqs	            rT   rational_independentr^      s      	
!*C12Y 	Q" ~ 	GAt##A&q)AQ A%%a(A!	 JJqM	 JrV   c              #   >   K   ddl m} t        d|z         fd}d}t        d|dz         D ]  } ||      \  }}	|j	                         }|j                         }
t        |
      }|st        |      |k(  sKt        t        d  ||d|       D                    }|rd	}|	j                  |      }	|	j                         d   }	|	j                         j                  t              d   d   }	|	j                  t                           |f  yw)
a  
    Generates simple DE.

    Explanation
    ===========

    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).
    r   linsolveza:%dc                 \   j                  |       t        t        d|       D cg c]  }|   j                  |      z   c} z   }       j                  |       t        t        d|       D cg c]   }|          j                  |      z  " c} z   }||fS c c}w c c}w Nr   )r1   r   r0   )rE   rH   eqDErP   rC   grD   s       rT   _makeDEzsimpleDE.<locals>._makeDE   s    VVAq\CU1a[!I!A$qvva|"3!IJJqTYYq!_s5A;$OaQqT!A$))Aq/%9$OPP2v "J$Os   B$
5%B)
Fr+   c              3   .   K   | ]  }|D ]  }|   y wN .0srH   s      rT   	<genexpr>zsimpleDE.<locals>.<genexpr>   s     JQJ1qJqJ   NT)sympy.solvers.solvesetra   r   r0   r   as_ordered_termsr^   lendictziprA   r7   factoras_coeff_mulr	   collect)rC   rD   rf   rF   ra   rg   foundrE   rd   re   rJ   rN   solrP   s   ```          @rT   simpleDErz      s	    ( 0% !A
 E1eai  2BYY[##%"5!,CHMs1J(3"1*>JKLCWWS\""$Q'B))*5a8;B**Z!-.112s   A8D?BDc                 l   t         j                  }| j                  t              j	                         }d}t        j                  |       D ]O  }|j                  |      \  }}t        |t              r|j                  }	nd}	||	|k  r|	}|| |||	z         z  z  }Q |r|j                  |||z
        }|S )a  Converts a DE with constant coefficients (explike) into a RE.

    Explanation
    ===========

    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
    Nr   )r   r3   atomsr
   r?   r   r6   r9   r8   r	   derivative_countrA   )
re   rrE   RErf   minirK   r<   r\   rO   s
             rT   exp_rer      s    > 
B
 AD]]2 ##A&qa$""AA<1t8D
eaAh WWQD!IrV   c                    t         j                  }| j                  t              j	                         }|j                  t
              j	                         }d}t        j                  | j                               D ]  }|j                  |      \  }}	|j                  |      \  }
}|j                  |      d   }t        |	t              r|	j                  }nd}||
t        |dz   |z
  |      z   |||z   |z
        z  z  }|	||z
  |k  s||z
  } |j                  |||z
        }t!        d      }|j#                   |||z               S )a  
    Converts a DE into a RE.

    Explanation
    ===========

    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
    Nr+   r   m)r   r3   r|   r
   r?   r   r   r6   r   r9   as_coeff_exponentr8   r	   r}   r   rA   r   rw   )re   r~   rE   r   rf   rD   r   rK   r<   r\   cvlrO   r   s                  rT   hyper_rer   )  s2   @ 
B
 A	AD]]299;' 
##A&q##A&1"1%a$""AA
a"QUQY""Qq1uqy\11<1q54<q5D
 
AH	BS	A::aAhrV   c                 t    | || z  z  } |j                  |||z         }|j                  |||z         }| |||fS ri   rA   )rC   rD   PQrE   r   shifts          rT   _transformation_ar   a  sH    eVA	q!e)A	q!e)AaA:rV   c                     | j                  |||z        } |j                  |||z        }|j                  |||z        }||z  }| |||fS ri   r   )rC   rD   r   r   rE   r   scales          rT   _transformation_cr   h  sU    	q!U(A	q!e)A	q!e)AJAaA:rV   c                     | j                  |      } |j                  ||dz         ||z   dz   z  }|j                  ||dz         |dz   z  }| |||fS Nr+   )r1   rA   )rC   rD   r   r   rE   r   s         rT   _transformation_er   p  s[    	q	A	q!a%AEAI&A	q!a%AE"AaA:rV   c                 B    | D cg c]  \  }}|||z   f c}}S c c}}w ri   rj   )ry   r   resconds       rT   _apply_shiftr   w  #    145ICS$,555   c                 B    | D cg c]  \  }}|||z  f c}}S c c}}w ri   rj   )ry   r   r   r   s       rT   _apply_scaler   {  r   r   c           	          | D cg c]5  \  }}||dz   |j                         d   j                  |      z  z  |dz   f7 c}}S c c}}w r   )as_coeff_Addr<   )ry   rD   rE   r   r   s        rT   _apply_integrater     sV     "T TAX 1 1 3A 6 < <Q ?@A4!8L " " "s   :Ac                 (   ddl m} g }t        |dz   ||z   dz         D ]d  }	|	dk  dk(  r| j                  ||	      j	                  |d      t        |	      z  }
|
j                  rH||z  |	z   }|
}|j                  ||      }|j                  ||      }|j                  |d|z        j                  |      d   }|j                  |d|z        j                  |      d   }|| |z  |z  z  }|t         |||      j                         D 
cg c]  \  }
}t        |
 |      |z   c}}
 z  }|t         |||      j                         D 
cg c]  \  }
}t        |
 |      |z   c}}
 z  }|j                  ||f       g |S c c}}
w c c}}
w )zComputes the formula for f.r   )rootsr+   T)r.   r   r0   r1   r@   r   r>   rA   leadtermr   itemsr   rB   )rC   rD   r   r   rE   r   k_maxr   ry   rH   r~   ktermr   pr]   c1c2muls                     rT   _compute_formular     s   !
C519eai!m, !Ed?FF1aLq!$y|399!aFF1eFF1eVVAqs^$$Q'*VVAqs^$$Q'*b1}s%1+2C2C2EF3RAY^FGGs%1+2C2C2EF3RAY^FGG

C< '!* J GFs   FFc           
      N   ddl m}m} ddlm}  |||       |||      }
}	t        |	      }|j                  |
        ||j                         D cg c]$  \  }}|j                  r|j                         d   & c}}      }t        | ||||||      \  } }}} |||      }
|
rt        |
j                          }nt        j                  }||z   }t        | ||||||      \  } }}}|| z  j!                  |d      }t#        |t$              s|dk7  ry |||      }
|
rt'        |
j                          }nt        j                  }t        j                  t(         }}t+        ||z   dz         D ]  }| j-                  ||      j!                  |d      t/        |      z  }|j0                  du r| }t        | ||||||      \  } }}}t3        | |||||      \  } }}}t5        | |||||      \  }}}t7        |||      }t9        ||      } |||      }|||z
  j!                  |d      z  }|dz  }|||fc S |s|||||z   z  z  z  }t;        ||z   |      }||kD  s|} |j=                  ||d|z  z        }t?        | ||||||      }t9        ||      }tA        ||      }|||fS c c}}w )a  
    Recursive wrapper to rsolve_hypergeometric.

    Explanation
    ===========

    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.
    r   )lcmr   r)   r+   NF)!r.   r   r   r/   r*   rs   updater   is_rationalr7   r   r   keysr   r3   r   r@   r8   r!   r   r   r0   r1   r   	is_finiter   _rsolve_hypergeometricr   r   r   rA   r   r   )rC   rD   r   r   rE   r   r   r   r*   prootsqroots	all_rootsr~   rK   r   k_minr   r   r   rN   mprH   old_fry   pow_xs                            rT   r   r     s    ') 1a[%1+FFVIV9??3D #41aMM !!#A& # $E"1aAq!U;JAq!Q 1a[FV[[]#AIE"1aAq!U;JAq!Q	
1AqAaAF1a[FV[[]#ffrcC519q=! FF1aLq!$y|3;;%E*1aAq!Q?JAq!Q*1aAq!<JAq!Q1!Q1aCLCb"31-CsA&CC#CECK&&q!,,C!GBR<1QU^##Ca%i%0Erz#$ ((1a!E'l
#C
1aAq!U
3C
sE
"C
sE
"CR<a#s   )J!
c           	         t        | |||||      }|y|\  }}}	t        d       }
|D ]  \  }}|j                         \  }}|j                  |      }|j                  du r||t        |      z  z  }t        |      }|j                  |||z
  |z        }t        ||z  ||z        }|
|xx   |z  cc<    g }|
j                         D ]  \  }}|j                  ||f        |j                  t        j                  df       t        | }|	t         u rt        j                  }n|	j                  du rt        |	      }n|	dz   }|dk  r1|t!        t#        |||z  z  ||df            z  }t        j                  }|||fS )a  
    Solves RE of hypergeometric type.

    Explanation
    ===========

    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
    Nc                  "    t         j                  S ri   r   r3   rj   rV   rT   <lambda>z'rsolve_hypergeometric.<locals>.<lambda>   s
    166 rV   FTr+   r   r-   )r   r   r   r<   
is_integerr   r   rA   r   r   rB   r   r3   r    r   r   sumr#   )rC   rD   r   r   rE   r   resultsol_listrN   r   sol_dictr   r   rO   mkr   ry   rm   s                     rT   rsolve_hypergeometricr     s   b $Aq!Q15F~Hc2>*H 
	T!!#2HHQK<<5 1d1g:CaAhhq1q5A+&!a%Q#
 C^^%  	c

C; JJ~
S/C	bSyFF	%	BKF 	1us8C!Q$JAr
344FFa=rV   c                 J   t        j                  |      }t        |      dk(  rt        |j	                  t
                    }t        |j                  |      \  }}|d   j                  d   |d   j                  d   z
  }	|	dk  r||}}t        |	      }	t        | |||||	      S y)z;See docstring of :func:`rsolve_hypergeometric` for details.   r+   r   N)r   r6   rr   listr|   r
   mapr<   argsabsr   )
rC   rD   r   rf   rE   rJ   gsr   r   r   s
             rT   _solve_hyper_REr   C  s    MM"E
5zQ"((8$%288R 1qEJJqMBqEJJqM)q5aqAAA$Q1aA66 rV   c                    ddl m} t        j                  |      D ]$  }|j	                  |      \  }}|j
                  s$ y t        |||      }	i }
t        t        t        j                  |	                  D ]@  }|r| j                  |      } | j                  |d      |
 ||      j                  ||      <   B  ||	 ||      |
      }|r-|t        |      z  t        j                  t        j                  fS y)z%Solves DE with constant coefficients.r   rsolveN)sympy.solversr   r   r6   r9   free_symbolsr   r0   rr   r1   r@   rA   r   r   r3   )rC   rD   re   rf   rE   r   rK   r<   r\   r   initrH   ry   s                rT   _solve_explike_DEr   Q  s    $]]2 ##A&q
 
Aq	BD3s}}R()* .q	A !1QqTYYq!_.
 QqT4
 C
il"AFFAFF33 rV   c                    ddl m} t        |||      }i }t        t	        t        j                  |                  D ]L  }|r| j                  |      } | j                  |d      t        |      z  | ||      j                  ||      <   N  || ||      |      }	|	r!|	t        j                  t        j                  fS y)z4Converts DE into RE and solves using :func:`rsolve`.r   r   N)r   r   r   r0   rr   r   r6   r1   r@   r   rA   r   r3   )
rC   rD   re   rf   rE   r   r   r   rH   ry   s
             rT   _solve_simpler   h  s    $	"a	BD3s}}R()* =q	A !1	! <QqTYYq!_=
 QqT4
 C
QVVQVV$$ rV   c                    ddl m} g }| j                  t         ||      ||            }t	        |      D ]q  }| j                  t         ||      ||            }	|	|z  j                         j                  |      }	t        j                  |	      D ]  }
|j                  |
        s g }|D ]<  }|j                  |      r n+|j                  t              s,|j                  |       > |}|rt        t        |d  ||t        |            D                    }|rZ| j                  |      } | j!                         j#                  t              d   d   } | j                  t         ||                  } | S )zDConverts DE with free parameters into DE with constant coefficients.r   r`   c              3   .   K   | ]  }|D ]  }|   y wri   rj   rk   s      rT   rn   z(_transform_explike_DE.<locals>.<genexpr>  s     MA1MaaMaMro   r+   )rp   ra   r<   r	   r0   r   rw   r   r6   rB   r4   r   rs   rt   r   rA   ru   rv   )re   rf   rD   rF   symsra   rd   highest_coeffrH   r<   rK   tempery   s                 rT   _transform_explike_DEr   z  sF   /	BHHZ!a78M5\ AaD!Q/0&..088;u% 	AIIaL	
 D 558UU6]KKN	 	3tM"d4j)AMNOB))*5a8;BJqt,-BIrV   c                 f   ddl m} t        | ||      }g }t        d|      D ]-  }|j	                   |||z               }	|j                  |	       / t        t        |d  ||t        |            D                    }
|
rt        d      }|j                  |
      }|j                         j                         d   j                   |||z               }|j                  |      d   d   }t        |      D ]7  }|j	                   |||z               s|s!|j                  |||z
        } |S  |S )z@Converts DE with free parameters into RE of hypergeometric type.r   r`   r+   c              3   .   K   | ]  }|D ]  }|   y wri   rj   rk   s      rT   rn   z#_transform_DE_RE.<locals>.<genexpr>  s     IqI!!I!Iro   r   )rp   ra   r   r0   r<   rB   rs   rt   r   r   rA   ru   r7   rw   rv   )re   rf   rE   rF   r   ra   r   rd   rH   r<   ry   r   s               rT   _transform_DE_REr     s   /	"a	B	B1e_ 1q5"
		% s4IXb$t*%=IJ
KC
IWWS\YY['')!,44Qq1uX>__Q"1%u 	Axx!a%!aWWQA&I		 IrV   c                 ^   d}|j                   j                  ||h      }|rt        |||||      }nt        |||      }|j                   j                  |h      st	        | ||||      }|r|S |rt        |||||      }|j                   j                  |h      st        | ||||      }|r|S y)aA  
    Solves the DE.

    Explanation
    ===========

    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)r   
differencer   r   r   r   r   )	rC   rD   re   rF   rf   rE   ry   r   r   s	            rT   solve_der     s    D C??%%q!f-Db!Qt4b!Q??%%qc*aB1-

"2q!UD9??%%qc*1b!Q/

 rV   c           	         t        d      }g }d}t        | |||      D ]K  \  }}|t        | |||||      }|r|c S |j                  j	                  |h      r;|j                  |       M |D ]  }t        | ||||      }|s|c S  y)a  
    Hypergeometric algorithm for computing Formal Power Series.

    Explanation
    ===========

    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
    rf   N)r
   rz   r   r   r   rB   r   )	rC   rD   rE   rF   rf   desry   re   rH   s	            rT   hyper_algorithmr     s    B 	A
C
C!Q5) A>1aQ1-CJ))1#.JJrN  Aq"a+JrV   c                    |t         j                  t         j                  fv r|t         j                  u rt         j                  nt         j                   }| j	                  |d|z        }t        ||d|||||      }	|	y|	d   |	d   j	                  |d|z        |	d   j	                  |d|z        fS |s|t         j                   k(  r|t         j                   k(  r| |z   }
| }|}n
||z   }
|}| }| j	                  ||
      }t        ||dt         j                  ||||      }	|	y|	d   |	d   j	                  |||z         |	d   j	                  |||z         fS | j                  |      rVt        d      }t        t        | ||      |dt        f      }t        ||z  |dt        f      }| j                  |d      }|||fS t        | t              r&d}	t        t         j                  dt        f      }t         j                  d}}t        j                  |       D ]  }t        ||dt         j                  ||||      }|r|	sd}	|d   }|d   j                   |j                   kD  r|}|j                   |d   j                   } }n |d   }|d   j                   |j                   } }t        t#        |d| |z
   |||        D cg c]  }|d   |d   z   c} }||d   z  }||d   |z   z  }||z  } |	r|||fS y| j$                  j'                  |h      } t)        |       j*                  | \  } }d}	t        d      }|rt-        | ||||      }	|	|rt/        | |||      }	|	yddlm} |j4                  rt         j                  }n ||      }t        |	d   ||	d   t        f      } |||z  |z        }t        ||dt        f      } ||	d   |z        }|||fS c c}w )	zPRecursive wrapper to compute fps.

    See :func:`compute_fps` for details.
    r+   r   Nr   rE   FT)powsimp)r   InfinityNegativeInfinityOnerA   _compute_fpsis_polynomialr   r#   Coeffr   r<   r8   r   r3   r6   startrt   r   r   r   r9   rU   r   sympy.simplify.powsimpr   r>   )rC   rD   x0dirhyperrF   rationalr,   r   r   reprep2rep2brE   rS   xkrN   rK   r   seqrm   zsaver   symbr   
xk_formulas                              rT   r   r     s   
 
ajj!,,--QZZ'aeeaeeVvva1~dAq#ueXtL>q	6!9>>!QqS16!9>>!QqS3IJJ	squuf}155&="r'C2DEb&CDCEvva~dAq!%%xN>q	6!9>>!TE\:q	q$,/1 	1 	q#JeAq!nq!Rj1adQ2J'ggam2s{ !Saffq"g&&&$Rq! 	Aq!QueXtLC!FQBq6<<"((*C88SV\\qAa&Cq6<<qAC1q5NBqG0LM1QqT!A$YMNc!fs1v}$q!	" r3; >>$$aS)D(q	(($/IQF 	c
A#Aq!UD9~% Aq%0~.||uut}	&)aB/	0BA%J	*q!Rj	)B
&)d"
#Cr3;K Ns   O
c           
      X   t        |       } t        |      }| j                  |      syt        |      }|dk(  rt        j                  }nP|dk(  rt        j                   }n9|t        j                  t        j                   fvrt	        d      t        |      }t        | |||||||      S )a  
    Computes the formula for Formal Power Series of a function.

    Explanation
    ===========

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

    * rational_algorithm
    * Hypergeometric 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
    N+-zDir must be '+' or '-')r   r4   r   r   
ValueErrorr   )rC   rD   r   r   r   rF   r   r,   s           rT   compute_fpsr   y  s    n 	
A
A558	B
czee	uuf	QUUQUUFO	#122cl1b#ueXtDDrV   c                        e Zd ZdZed        Zy)r   zP
    Coeff(p, x, n) represents the nth coefficient of the polynomial p in x
    c                 d    |j                  |      r|j                  r|j                  ||      S y y ri   )r   r   r<   )clsr   rD   rZ   s       rT   evalz
Coeff.eval  s,    ??1!,,771a=  #/rV   N)__name__
__module____qualname____doc__classmethodr  rj   rV   rT   r   r     s     ! !rV   r   c                   b   e Zd Zd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&dZd Zd'dZd(dZd Zd(dZd(dZd Z d Z!d  Z"d! Z#d" Z$d# Z%d$ Z&y))FormalPowerSeriesa5  
    Represents Formal Power Series of a function.

    Explanation
    ===========

    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                 N    t        t        |      }t        j                  | g| S ri   )r   r   r   __new__)r  r   s     rT   r  zFormalPowerSeries.__new__  s"    7D!||C'$''rV   c                 $   |d   d   }|j                   d   }t        |j                  |dt        f      | _        t        t        |      |dt        f      | _        | j                  | j                  z  | _        t        d|dt        f      | _        y )N   r   r+   )r-   r+   )		variablesr#   formular   ak_seqr   fact_seqbell_coeff_seqsign_seq)selfr   rS   rE   s       rT   __init__zFormalPowerSeries.__init__  su    !WQZLLOrzzAq":6 11bz:"kkDMM9 1a*5rV   c                      | j                   d   S rc   r   r  s    rT   functionzFormalPowerSeries.function      yy|rV   c                      | j                   d   S r   r  r  s    rT   rD   zFormalPowerSeries.x  r  rV   c                      | j                   d   S )Nr   r  r  s    rT   r   zFormalPowerSeries.x0  r  rV   c                      | j                   d   S )N   r  r  s    rT   r   zFormalPowerSeries.dir  r  rV   c                 &    | j                   d   d   S )Nr  r   r  r  s    rT   rS   zFormalPowerSeries.ak      yy|ArV   c                 &    | j                   d   d   S )Nr  r+   r  r  s    rT   r   zFormalPowerSeries.xk  r"  rV   c                 &    | j                   d   d   S )Nr  r   r  r  s    rT   rN   zFormalPowerSeries.ind  r"  rV   c                 "    t        dt              S rc   )r   r   r  s    rT   intervalzFormalPowerSeries.interval  s    2rV   c                 .    | j                   j                  S ri   )r&  infr  s    rT   r   zFormalPowerSeries.start      }}   rV   c                 .    | j                   j                  S ri   )r&  supr  s    rT   stopzFormalPowerSeries.stop  r)  rV   c                     t         S ri   )r   r  s    rT   lengthzFormalPowerSeries.length  s    	rV   c                     ddl m} | j                  | j                  }}|j                  d   } ||j
                  |j
                  z  ||j                  |j                  f      }| j                  |z   S )z0Returns an infinite representation of the seriesr   )Sum)	sympy.concreter0  rS   r   r  r  r   r,  rN   )r  r0  rS   r   rE   inf_sums         rT   infinitezFormalPowerSeries.infinite  s\     	'$''BLLObjj2::-288RWW/EFxx'!!rV   c                     |j                  | j                        d   j                         \  }}|j                  | j                        st        j
                  S |S )z!Returns the power of x in a term.r+   )r9   rD   r:   r4   r   r3   )r  r[   rQ   r   s       rT   
_get_pow_xzFormalPowerSeries._get_pow_x!  sF    **466215AACuyy 66MrV   c                 Z   g }| j                   }t        |       D ]  \  }}| j                  |      } |j                  | r |j                  | d   }||k\  r	 t        | S |j
                  du r||dz   k(  r	 t        | S |t        j                  usx|j                  |        t        | S )z
        Truncated series as polynomial.

        Explanation
        ===========

        Returns series expansion of ``f`` upto order ``O(x**n)``
        as a polynomial(without ``O`` term).
        r   Tr+   )
r   rX   r5  r4   r;   r   r   r3   rB   r   )r  rZ   rJ   symrH   rK   xps          rT   
polynomialzFormalPowerSeries.polynomial(  s     dO 		 DAq#Brvvs|$R__c*1-Qw E{ $&1A: E{ !&&Q		  E{rV   c                    |t        |       S | j                  | j                  }}| j                  j	                  |      }|t
        j                  u rt
        j                  }| j                  |      t        |||f      z   S )z
        Truncated series.

        Explanation
        ===========

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

        If n is ``None``, returns an infinite iterator.
        )
iterrD   r   r   r<   r   r   r   r9  r"   )r  rZ   rD   r   pt_xks        rT   truncatezFormalPowerSeries.truncateA  sl     9:2a ###Bq!E%!R$999rV   c                 $    | j                  d      S rc   )
_eval_termr  s    rT   
zero_coeffzFormalPowerSeries.zero_coeffW  s    q!!rV   c                 X   	 | j                   j                  |      }| j                  j                  |      j                         }||z  }| j                  rt
        j                  }| j                  }t        j                  | j                        D ]X  }| j                  |      } |j                  | r |j                  | d   }|dk(  r|dk  r||z  }E||k\  sK||dz   k  sT||z  }Z ||z  }|j                  | j                        S # t        $ r t
        j                  }Y w xY wNr   r+   )r   r<   rS   simplify
IndexErrorr   r3   rN   r   r   r6   r5  r4   r;   rw   rD   )	r  ptr<  pt_akr[   rN   r7  rK   r   s	            rT   r?  zFormalPowerSeries._eval_termZ  s   	#GGMM"%EGGMM"%..0E EMD88&&C##C]]488, *599c?.E..4Q7E7uqy1HCb[UR!V^1HC CKD||DFF##%  	66D	s   AD D)(D)c                 B    | j                   }|j                  |      r| S y ri   )rD   r4   )r  oldnewrD   s       rT   
_eval_subszFormalPowerSeries._eval_subsr  s     FF771:K rV   Nc                 >    | D ]  }|t         j                  us|c S  y ri   r   )r  rD   logxcdirrK   s        rT   _eval_as_leading_termz'FormalPowerSeries._eval_as_leading_termw  s!     	A	rV   c           	         | j                   j                  |      }| j                  j                  |      }| j                  | j                  j
                        }| j                  }|j                  d   }|j
                  j                  |      rg }|j
                  j                  D ]^  \  }}	t        j                  }
t        j                  |      D ]  }| j                  |      }|
|||z   z  z  }
  |j                  |
|	f       ` t        | }t!        |j#                  ||dz         ||j$                  dz
  |j&                  f      }nFt!        |j
                  |z  j#                  ||dz         ||j$                  dz
  |j&                  f      }| j)                  || j*                  | j,                  | j.                  || j                  |f      S rB  )r  r1   rN   r5  r   r  rS   r  r4   r   r   r3   r   r6   rB   r    r#   rA   r   r,  funcrD   r   r   )r  rD   rC   rN   pow_xkrS   rE   formr   r   r   rK   r   s                rT   _eval_derivativez"FormalPowerSeries._eval_derivative|  s~   MMq!hhmmA1WWLLO::>>!D

 '1vvq) 1A OOA.EA%00D1 T1I&' d#D$))Aq1u-288a</IJB2::.44QA>bhhlBGG46B yyDFFDGGTXXDGGS7IJJrV   c           	      (   ddl m} || j                  }nt        |      r || j                  |      S  || j                  |      } || j
                  |      }|||z
  j                  |d      z  }| j                  | j                  j                        }| j                  }|j                  d   }|j                  j                  |      rg }	|j                  j                  D ]a  \  }
}t        j                  }t!        j"                  |
      D ]!  }| j                  |      }||||z   dz   z  z  }# |	j%                  ||f       c t'        |	 }	t)        |	j+                  ||dz
        ||j,                  dz   |j.                  f      }nIt)        |j                  |dz   z  j+                  ||dz
        ||j,                  dz   |j.                  f      }| j1                  || j                  | j2                  | j4                  || j                  |f      S )aK  
        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)
        r   r)   r+   )r/   r*   rD   r%   r  rN   r@   r5  r   r  rS   r  r4   r   r   r3   r   r6   rB   r    r#   rA   r   r,  rP  r   r   )r  rD   kwargsr*   rC   rN   rQ  rS   rE   rR  r   r   r   rK   r   s                  rT   r*   zFormalPowerSeries.integrate  s    	.9Aa[T]]A..dmmQ'!$Cq!$$1WWLLO::>>!D

 '1vvq) 5A OOA.EA%!!344D5 T1I&' d#D$))Aq1u-288a</IJB2::!4::1a!eDbhhlBGG46B yyDFFDGGTXXDGGS7IJJrV   c                 X   |t        |       S t        |      }t        |t              st	        d      | j
                  |j
                  k7  rt	        d      | j                  |j                  k7  rt	        d      | j                  |j                  k7  rt	        d      t        | |      S )a  
        Multiplies two Formal Power Series, using discrete convolution and
        return the truncated terms upto specified order.

        Parameters
        ==========

        n : Number, optional
            Specifies the order of the term up to which the polynomial should
            be truncated.

        Examples
        ========

        >>> from sympy import fps, sin, exp
        >>> from sympy.abc import x
        >>> f1 = fps(sin(x))
        >>> f2 = fps(exp(x))

        >>> f1.product(f2, x).truncate(4)
        x + x**2 + x**3/3 + O(x**4)

        See Also
        ========

        sympy.discrete.convolutions
        sympy.series.formal.FormalPowerSeriesProduct

        =Both series should be an instance of FormalPowerSeries class.9Both series should be calculated from the same direction.6Both series should be calculated about the same point.(Both series should have the same symbol.)	r;  r   r8   r  r   r   r   rD   FormalPowerSeriesProductr  otherrD   rZ   s       rT   productzFormalPowerSeries.product  s    > 9:%!23 ' ( ( 88uyy  0 1 1WW  , - - VVuwwGHH'e44rV   c                     t        d|dz         D cg c]+  }t        ||t        | j                  d||z
  dz                - }}t	        d      }t        t        |      |dt        f      S c c}w )au  
        self.coeff_bell(n) returns a sequence of Bell polynomials of the second kind.
        Note that ``n`` should be a integer.

        The second kind of Bell polynomials (are sometimes called "partial" Bell
        polynomials or incomplete Bell polynomials) are defined as

        .. math::
            B_{n,k}(x_1, x_2,\dotsc x_{n-k+1}) =
                \sum_{j_1+j_2+j_2+\dotsb=k \atop j_1+2j_2+3j_2+\dotsb=n}
                \frac{n!}{j_1!j_2!\dotsb j_{n-k+1}!}
                \left(\frac{x_1}{1!} \right)^{j_1}
                \left(\frac{x_2}{2!} \right)^{j_2} \dotsb
                \left(\frac{x_{n-k+1}}{(n-k+1)!} \right) ^{j_{n-k+1}}.

        * ``bell(n, k, (x1, x2, ...))`` gives Bell polynomials of the second kind,
          `B_{n,k}(x_1, x_2, \dotsc, x_{n-k+1})`.

        See Also
        ========

        sympy.functions.combinatorial.numbers.bell

        r+   NrE   )r0   r   tupler  r   r#   r   )r  rZ   rO   inner_coeffsrE   s        rT   
coeff_bellzFormalPowerSeries.coeff_bell  sr    4 QVVWYZ[\Y\P]^1Q5)<)<Vac!e)D#EF^^#Jl+aBZ88 _s   0A+c                    |t        |       S t        |      }t        |t              st	        d      | j
                  |j
                  k7  rt	        d      | j                  |j                  k7  rt	        d      | j                  |j                  k7  rt	        d      |j                  d      j                  |j                        d   t        j                  urt	        d      t        | |      S )a  
        Returns the truncated terms of the formal power series of the composed function,
        up to specified ``n``.

        Explanation
        ===========

        If ``f`` and ``g`` are two formal power series of two different functions,
        then the coefficient sequence ``ak`` of the composed formal power series `fp`
        will be as follows.

        .. math::
            \sum\limits_{k=0}^{n} b_k B_{n,k}(x_1, x_2, \dotsc, x_{n-k+1})

        Parameters
        ==========

        n : Number, optional
            Specifies the order of the term up to which the polynomial should
            be truncated.

        Examples
        ========

        >>> from sympy import fps, sin, exp
        >>> from sympy.abc import x
        >>> f1 = fps(exp(x))
        >>> f2 = fps(sin(x))

        >>> f1.compose(f2, x).truncate()
        1 + x + x**2/2 - x**4/8 - x**5/15 + O(x**6)

        >>> f1.compose(f2, x).truncate(8)
        1 + x + x**2/2 - x**4/8 - x**5/15 - x**6/240 + x**7/90 + O(x**8)

        See Also
        ========

        sympy.functions.combinatorial.numbers.bell
        sympy.series.formal.FormalPowerSeriesCompose

        References
        ==========

        .. [1] Comtet, Louis: Advanced combinatorics; the art of finite and infinite expansions. Reidel, 1974.

        rW  rX  rY  rZ  r   z\The formal power series of the inner function should not have any constant coefficient term.)r;  r   r8   r  r   r   r   rD   r?  rv   r   r3   FormalPowerSeriesComposer\  s       rT   composezFormalPowerSeries.compose  s    b 9:%!23 ' ( ( 88uyy  0 1 1WW  , - - VVuwwGHHA++EGG4Q7qvvE - . . (e44rV   c                 ~    |t        |       S | j                  d      j                  rt        d      t	        |       S )a  
        Returns the truncated terms of the inverse of the formal power series,
        up to specified ``n``.

        Explanation
        ===========

        If ``f`` and ``g`` are two formal power series of two different functions,
        then the coefficient sequence ``ak`` of the composed formal power series ``fp``
        will be as follows.

        .. math::
            \sum\limits_{k=0}^{n} (-1)^{k} x_0^{-k-1} B_{n,k}(x_1, x_2, \dotsc, x_{n-k+1})

        Parameters
        ==========

        n : Number, optional
            Specifies the order of the term up to which the polynomial should
            be truncated.

        Examples
        ========

        >>> from sympy import fps, exp, cos
        >>> from sympy.abc import x
        >>> f1 = fps(exp(x))
        >>> f2 = fps(cos(x))

        >>> f1.inverse(x).truncate()
        1 - x + x**2/2 - x**3/6 + x**4/24 - x**5/120 + O(x**6)

        >>> f2.inverse(x).truncate(8)
        1 + x**2/2 + 5*x**4/24 + 61*x**6/720 + O(x**8)

        See Also
        ========

        sympy.functions.combinatorial.numbers.bell
        sympy.series.formal.FormalPowerSeriesInverse

        References
        ==========

        .. [1] Comtet, Louis: Advanced combinatorics; the art of finite and infinite expansions. Reidel, 1974.

        r   zSConstant coefficient should exist for an inverse of a formal power series to exist.)r;  r?  r>   r   FormalPowerSeriesInverse)r  rD   rZ   s      rT   inversezFormalPowerSeries.inverse]  sE    b 9:??1%% * + + (--rV   c           	      2   t        |      }t        |t              r| j                  |j                  k7  rt	        d      | j
                  |j
                  k7  rt	        d      | j                  |j                  }}| j                  |j                  j                  ||      z   }| j                  |j                  vr|S | j                  |j                  z   }| j                  j                  |j                  j                  kD  r9|j                  }|j                  j                  | j                  j                  }}n8| j                  }| j                  j                  |j                  j                  }}t        t        |d||z
   | j                  ||       D 	cg c]  }	|	d   |	d   z   c}	 }
| j                  |j                  z   |
z   }| j!                  ||| j
                  | j                  || j                  |f      S |j#                  | j                        sh| j                  |z   }| j                  |z   }| j!                  || j                  | j
                  | j                  | j                  | j                  |f      S t        | |      S c c}	w )NrX  rY  r   r+   )r   r8   r  r   r   r   rD   r  rA   r   rS   r   r   rt   r   rN   rP  r4   )r  r]  rD   yrC   rS   r   rm   r   r   r   rN   s               rT   __add__zFormalPowerSeries.__add__  s   e./xx599$  "4 5 5EHH$  "0 1 1 66577qA 3 3Aq 99AvvQ^^+588#Bww}}uxx~~-hhxx~~tww}}1ggww}}ehhnn1CAq1u!,MNq1adNOD((UYY&-C99Q477DHHr477C6HII466"%A((U"C99Q"ggtww46 6 4 Os   Jc                 $    | j                  |      S ri   rk  r  r]  s     rT   __radd__zFormalPowerSeries.__radd__      ||E""rV   c           	          | j                  | j                   | j                  | j                  | j                  | j
                   | j                  | j                   f      S ri   )rP  r  rD   r   r   rS   r   rN   r  s    rT   __neg__zFormalPowerSeries.__neg__  sG    yy$--$((77(DGGdhhY79 	9rV   c                 &    | j                  |       S ri   rm  rn  s     rT   __sub__zFormalPowerSeries.__sub__  s    ||UF##rV   c                 &    |  j                  |      S ri   rm  rn  s     rT   __rsub__zFormalPowerSeries.__rsub__  s    u%%rV   c           	      X   t        |      }|j                  | j                        rt        | |      S | j                  |z  }| j
                  j                  |      }| j                  |z  }| j                  || j                  | j                  | j                  || j                  |f      S ri   )r   r4   rD   r   r  rS   	coeff_mulrN   rP  r   r   r   )r  r]  rC   rS   rN   s        rT   __mul__zFormalPowerSeries.__mul__  s    99TVVtU##MME!WWu%hhyyDFFDGGTXXDGGS7IJJrV   c                 $    | j                  |      S ri   )ry  rn  s     rT   __rmul__zFormalPowerSeries.__rmul__  rp  rV      rc   ri   )Nr}  )'r  r  r  r  r  r  propertyr  rD   r   r   rS   r   rN   r&  r   r,  r.  r3  r5  r9  r=  r@  r?  rJ  rN  rS  r*   r^  rb  re  rh  rk  ro  rr  rt  rv  ry  r{  rj   rV   rT   r  r    sn    (6                 ! ! ! !   " "2:,"$0

K.+KZ25h9>H5T8.t$ L#9$&
K#rV   r  c                       e Zd ZdZd Zed        Zed        Zed        Zed        Z	ed        Z
d Zd	 Zd
 ZddZd Zd Zy)FiniteFormalPowerSeriesz3Base Class for Product, Compose and Inverse classesc                      y ri   rj   )r  r   s     rT   r  z FiniteFormalPowerSeries.__init__  s    rV   c                      | j                   d   S rc   r  r  s    rT   ffpszFiniteFormalPowerSeries.ffps  r  rV   c                      | j                   d   S r   r  r  s    rT   gfpszFiniteFormalPowerSeries.gfps  r  rV   c                 .    | j                   j                  S ri   )r  r  r  s    rT   rC   zFiniteFormalPowerSeries.f      yy!!!rV   c                 .    | j                   j                  S ri   )r  r  r  s    rT   rf   zFiniteFormalPowerSeries.g  r  rV   c                     t        d      )NzCNo infinite version for an object of FiniteFormalPowerSeries class.NotImplementedErrorr  s    rT   r3  z FiniteFormalPowerSeries.infinite  s    ! #7 8 	8rV   c                     t        d| z        )Nz(%s)._eval_terms()r  r  rZ   s     rT   _eval_termsz#FiniteFormalPowerSeries._eval_terms  s    !"6"=>>rV   c                     t        d      )Nz]By the current logic, one can get termsupto a certain order, instead of getting term by term.r  )r  rE  s     rT   r?  z"FiniteFormalPowerSeries._eval_term  s    ! #\ ] 	]rV   c                 $    | j                  |      S ri   )r  r  s     rT   r9  z"FiniteFormalPowerSeries.polynomial  s    ""rV   c                     | j                   }|j                  j                  |      }|j                  |j                  }}| j                  |      t        |||f      z   S ri   )r  r   r<   rD   r   r9  r"   )r  rZ   r  r<  rD   r   s         rT   r=  z FiniteFormalPowerSeries.truncate  sM    yya 2q!E%!R$999rV   c                     t         ri   r  r  rD   s     rT   rS  z(FiniteFormalPowerSeries._eval_derivative      !!rV   c                     t         ri   r  r  s     rT   r*   z!FiniteFormalPowerSeries.integrate	  r  rV   Nr|  )r  r  r  r  r  r~  r  r  rC   rf   r3  r  r?  r9  r=  rS  r*   rj   rV   rT   r  r    s    =     " " " " 8 8?]#:""rV   r  c                   ,    e Zd ZdZd Zed        Zd Zy)r[  a  Represents the product of two formal power series of two functions.

    Explanation
    ===========

    No computation is performed. Terms are calculated using a term by term logic,
    instead of a point by point logic.

    There are two differences between a :obj:`FormalPowerSeries` object and a
    :obj:`FormalPowerSeriesProduct` object. The first argument contains the two
    functions involved in the product. Also, the coefficient sequence contains
    both the coefficient sequence of the formal power series of the involved functions.

    See Also
    ========

    sympy.series.formal.FormalPowerSeries
    sympy.series.formal.FiniteFormalPowerSeries

    c                 H   | j                   | j                  }}|j                  j                  d   }t	        |j                  j
                  |dt        f      | _        |j                  j                  d   }t	        |j                  j
                  |dt        f      | _        y rc   )	r  r  rS   r  r#   r  r   coeff1coeff2)r  r   r  r  rE   s        rT   r  z!FormalPowerSeriesProduct.__init__#  ss    YY		dGGa twwAr
;GGa twwAr
;rV   c                 4    | j                   | j                  z  S )z3Function of the product of two formal power series.)rC   rf   r  s    rT   r  z!FormalPowerSeriesProduct.function,  s     vvrV   c                     | j                   | j                  }}t        |d| |d|       }g }t        d|      D ]<  }|j	                  ||   | j
                  j                  j                  |      z         > t        | S )a  
        Returns the first ``n`` terms of the product formal power series.
        Term by term logic is implemented here.

        Examples
        ========

        >>> from sympy import fps, sin, exp
        >>> from sympy.abc import x
        >>> f1 = fps(sin(x))
        >>> f2 = fps(exp(x))
        >>> fprod = f1.product(f2, x)

        >>> fprod._eval_terms(4)
        x**3/3 + x**2 + x

        See Also
        ========

        sympy.series.formal.FormalPowerSeries.product

        Nr   )	r  r  r   r0   rB   r  r   r<   r   )r  rZ   r  r  aksrJ   rH   s          rT   r  z$FormalPowerSeriesProduct._eval_terms1  s{    . dkk&!*fRaj1q! 	9ALLQ$)),,"4"4Q"778	9 E{rV   N)r  r  r  r  r  r~  r  r  rj   rV   rT   r[  r[    s%    *<  rV   r[  c                   &    e Zd ZdZed        Zd Zy)rd  a  
    Represents the composed formal power series of two functions.

    Explanation
    ===========

    No computation is performed. Terms are calculated using a term by term logic,
    instead of a point by point logic.

    There are two differences between a :obj:`FormalPowerSeries` object and a
    :obj:`FormalPowerSeriesCompose` object. The first argument contains the outer
    function and the inner function involved in the omposition. Also, the
    coefficient sequence contains the generic sequence which is to be multiplied
    by a custom ``bell_seq`` finite sequence. The finite terms will then be added up to
    get the final terms.

    See Also
    ========

    sympy.series.formal.FormalPowerSeries
    sympy.series.formal.FiniteFormalPowerSeries

    c                     | j                   | j                  | j                  j                  }}}|j	                  ||      S )z.Function for the composed formal power series.)rC   rf   r  rD   rA   )r  rC   rf   rD   s       rT   r  z!FormalPowerSeriesCompose.functionl  s1     &&$&&$))++a1vva|rV   c                 X   | j                   | j                  }}|j                         g}t        d|      D ]k  }|j	                  |      }|j
                  |z  }|j                  t        |d|  |j                  |dz
     z  |j                  j                  |      z         m t        | S )a  
        Returns the first `n` terms of the composed formal power series.
        Term by term logic is implemented here.

        Explanation
        ===========

        The coefficient sequence of the :obj:`FormalPowerSeriesCompose` object is the generic sequence.
        It is multiplied by ``bell_seq`` to get a sequence, whose terms are added up to get
        the final terms for the polynomial.

        Examples
        ========

        >>> from sympy import fps, sin, exp
        >>> from sympy.abc import x
        >>> f1 = fps(exp(x))
        >>> f2 = fps(sin(x))
        >>> fcomp = f1.compose(f2, x)

        >>> fcomp._eval_terms(6)
        -x**5/15 - x**4/8 + x**2/2 + x + 1

        >>> fcomp._eval_terms(8)
        x**7/90 - x**6/240 - x**5/15 - x**4/8 + x**2/2 + x + 1

        See Also
        ========

        sympy.series.formal.FormalPowerSeries.compose
        sympy.series.formal.FormalPowerSeries.coeff_bell

        r+   N)r  r  r@  r0   rb  r  rB   r   r  r   r<   )r  rZ   r  r  rJ   rH   bell_seqr   s           rT   r  z$FormalPowerSeriesCompose._eval_termsr  s    F YY		d"#q! 	RAq)H&&1CLLs2Aw4==1+==a@PPQ	R
 E{rV   N)r  r  r  r  r~  r  r  rj   rV   rT   rd  rd  S  s     0  
+rV   rd  c                   L    e Zd ZdZd Zed        Zed        Zed        Zd Z	y)rg  a  
    Represents the Inverse of a formal power series.

    Explanation
    ===========

    No computation is performed. Terms are calculated using a term by term logic,
    instead of a point by point logic.

    There is a single difference between a :obj:`FormalPowerSeries` object and a
    :obj:`FormalPowerSeriesInverse` object. The coefficient sequence contains the
    generic sequence which is to be multiplied by a custom ``bell_seq`` finite sequence.
    The finite terms will then be added up to get the final terms.

    See Also
    ========

    sympy.series.formal.FormalPowerSeries
    sympy.series.formal.FiniteFormalPowerSeries

    c                     | j                   }|j                  j                  d   }|j                         }t	        ||dz    z  |dt
        f      }|j                  |j                  z  |z  | _        y rB  )	r  r   r  r@  r#   r   r  r  aux_seq)r  r   r  rE   invinv_seqs         rT   r  z!FormalPowerSeriesInverse.__init__  sa    yyGGa oo3QU8,q!Rj9}}t}}4w>rV   c                 $    | j                   }d|z  S )z2Function for the inverse of a formal power series.r+   )rC   )r  rC   s     rT   r  z!FormalPowerSeriesInverse.function  s     FF1urV   c                     t        d      NzQOnly one function is considered while performinginverse of a formal power series.r   r  s    rT   rf   zFormalPowerSeriesInverse.g       < = 	=rV   c                     t        d      r  r  r  s    rT   r  zFormalPowerSeriesInverse.gfps  r  rV   c                 @   | j                   }|j                         g}t        d|      D ]k  }|j                  |      }| j                  |z  }|j                  t        |d|  |j                  |dz
     z  |j                  j                  |      z         m t        | S )a  
        Returns the first ``n`` terms of the composed formal power series.
        Term by term logic is implemented here.

        Explanation
        ===========

        The coefficient sequence of the `FormalPowerSeriesInverse` object is the generic sequence.
        It is multiplied by ``bell_seq`` to get a sequence, whose terms are added up to get
        the final terms for the polynomial.

        Examples
        ========

        >>> from sympy import fps, exp, cos
        >>> from sympy.abc import x
        >>> f1 = fps(exp(x))
        >>> f2 = fps(cos(x))
        >>> finv1, finv2 = f1.inverse(), f2.inverse()

        >>> finv1._eval_terms(6)
        -x**5/120 + x**4/24 - x**3/6 + x**2/2 - x + 1

        >>> finv2._eval_terms(8)
        61*x**6/720 + 5*x**4/24 + x**2/2 + 1

        See Also
        ========

        sympy.series.formal.FormalPowerSeries.inverse
        sympy.series.formal.FormalPowerSeries.coeff_bell

        r+   N)
r  r@  r0   rb  r  rB   r   r  r   r<   )r  rZ   r  rJ   rH   r  r   s          rT   r  z$FormalPowerSeriesInverse._eval_terms  s    D yy"#q! 	RAq)H<<(*CLLs2Aw4==1+==a@PPQ	R
 E{rV   N)
r  r  r  r  r  r~  r  rf   r  r  rj   rV   rT   rg  rg    sM    *?  
 = = = =*rV   rg  Nc           
          t        |       } |:| j                  }t        |      dk(  r|j                         }n|s| S t	        d      t        | |||||||      }	|	| S t        | ||||	      S )a  
    Generates Formal Power Series of ``f``.

    Explanation
    ===========

    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, ln, atan, sin
    >>> from sympy.abc import x, n

    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)

    Symbolic Functions

    >>> fps(x**n*sin(x**2), x).truncate(8)
    -x**(n + 6)/6 + x**(n + 2) + O(x**(n + 8))

    See Also
    ========

    sympy.series.formal.FormalPowerSeries
    sympy.series.formal.compute_fps
    r+   z multivariate formal power series)r   r   rr   r?   r  r   r  )
rC   rD   r   r   r   rF   r   r,   freer   s
             rT   fpsr    s{    D 	
Ay~~t9>
AH%&HIIAr3uhEF~Q2sF33rV   )r  F)r  )r   r+   Tr  TF)Nr   r+   Tr  TF)Xr  collectionsr   sympy.core.numbersr   r   r   sympy.core.addr   sympy.core.exprr   sympy.core.functionr	   r
   r   sympy.core.mulr   r   sympy.core.relationalr   sympy.sets.setsr   sympy.core.singletonr   sympy.core.symbolr   r   r   r   sympy.core.sympifyr   sympy.discrete.convolutionsr   (sympy.functions.combinatorial.factorialsr   r   r   %sympy.functions.combinatorial.numbersr   #sympy.functions.elementary.integersr   r   r   (sympy.functions.elementary.miscellaneousr   r   $sympy.functions.elementary.piecewiser    sympy.series.limitsr!   sympy.series.orderr"   sympy.series.sequencesr#   sympy.series.series_classr$   sympy.utilities.iterablesr%   rU   r^   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r[  rd  rg  r  rj   rV   rT   <module>r     sI    # - -    < <  ' $ $ " : : & 3 L L 6 D D = : % $ + 0 .GTD*2Z/d5 p66"
:DNXv74.%$8.5p2j\~ BFHEV!H !I#
 I#X0"/ 0"fC6 CLJ6 JZX6 XvR4rV   