
    \h-c                     $   S r SSKJr  SSKJr  SSKJr  SSKJrJ	r	  SSK
JrJrJr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJrJrJr  SSKJrJr  SSKJ r J!r!J"r"J#r#J$r$J%r%  SSK&J'r'J(r(J)r)J*r*  SSK+J,r,J-r-  SSK.J/r/  SS jr0S r1S r2SS jr3g)a1  
This module is intended for solving recurrences or, in other words,
difference equations. Currently supported are linear, inhomogeneous
equations with polynomial or rational coefficients.

The solutions are obtained among polynomials, rational functions,
hypergeometric terms, or combinations of hypergeometric term which
are pairwise dissimilar.

``rsolve_X`` functions were meant as a low level interface
for ``rsolve`` which would use Mathematica's syntax.

Given a recurrence relation:

    .. math:: a_{k}(n) y(n+k) + a_{k-1}(n) y(n+k-1) +
              ... + a_{0}(n) y(n) = f(n)

where `k > 0` and `a_{i}(n)` are polynomials in `n`. To use
``rsolve_X`` we need to put all coefficients in to a list ``L`` of
`k+1` elements the following way:

    ``L = [a_{0}(n), ..., a_{k-1}(n), a_{k}(n)]``

where ``L[i]``, for `i=0, \ldots, k`, maps to
`a_{i}(n) y(n+i)` (`y(n+i)` is implicit).

For example if we would like to compute `m`-th Bernoulli polynomial
up to a constant (example was taken from rsolve_poly docstring),
then we would use `b(n+1) - b(n) = m n^{m-1}` recurrence, which
has solution `b(n) = B_m + C`.

Then ``L = [-1, 1]`` and `f(n) = m n^(m-1)` and finally for `m=4`:

>>> from sympy import Symbol, bernoulli, rsolve_poly
>>> n = Symbol('n', integer=True)

>>> rsolve_poly([-1, 1], 4*n**3, n)
C0 + n**4 - 2*n**3 + n**2

>>> bernoulli(4, n)
n**4 - 2*n**3 + n**2 - 1/30

For the sake of completeness, `f(n)` can be:

    [1] a polynomial               -> rsolve_poly
    [2] a rational function        -> rsolve_ratio
    [3] a hypergeometric function  -> rsolve_hyper
    )defaultdict)product)S)RationalI)SymbolWildDummy)Equality)Add)Mul)default_sort_key)sympify)simplify	hypersimphypersimilar)solvesolve_undetermined_coeffs)Polyquogcdlcmroots	resultant)binomial	factorialFallingFactorialRisingFactorial)Matrix
casoratian)numbered_symbolsc           
        ^^,^-^.^/ [        U5      nUR                  T5      (       d  gUR                  n[        U 5      S-
  nU  Vs/ s H  n[	        UT5      PM     n n[	        ST5      /US-   -  n[
        R                  [
        R                  4/US-   -  n	[        US-   5       Hq  n
[        XS-   5       H,  nX==   X   [        X5      R                  T5      -  -  ss'   M.     X   R                  (       a  MV  X   R                  5       u  u  pX|4X'   Ms     U	S   S   =p[        SUS-   5       H/  n
X   S   U:  a  X   S   nX   S   U
-
  U:  d  M%  X   S   U
-
  nM1     [        U5      [        U5      p[        S5      n[
        R                  n[        US-   5       H)  n
X   S   U
-
  U:X  d  M  UX   S   [        X5      -  -  nM+     [        [!        UUSS S9R#                  5       5      nU(       a  [%        U5      /nO/ nU(       a  UU* S-
  /-  nO+UUR                  T5      R'                  5       U-
  U* S-
  /-  n[        [%        U5      5      nUS:  aA  U(       a9  UR)                  SS	5      (       a  [
        R                  / 4$ [
        R                  $ gUU::  a  / m,[
        R                  =nn[        US-   5       H9  n
T,R+                  [-        S
[/        X-   5      -   5      5        UT,U
   TU
-  -  -  nM;     [        US-   5       H.  n
UX
   R1                  5       UR3                  TTU
-   5      -  -  nM0     [5        UU-
  T,T5      nUb0  T,nT, Vs/ s H  nUU;  d  M  UPM     snm,UR3                  U5      nGO;gUnUU-   U-   S-   n[        [!        X   SS S9R#                  5       5      nU/ :w  a  [%        U5      S-   m/O[
        R                  m/S nS nU/U4S jm.0 n[        U* US-   5       H  n
U" US-   5      n[        SUS-   5       H  nUUS-
     X-   U-
  S-   -  U-  UU'   M     [
        R                  UU
'   [        US-   5       HU  n[        US-   5       H@  n[        UX-   5      n T." X   R1                  5       U5      n!UU
==   UU   U -  U!-  -  ss'   MB     MW     M     [7        UUS 5      n"U(       a  [        UU5       H  n
U" U5      n#[        SUU-   S-   5       HR  nU
U-
  S:  a    OHUUU-
     R3                  XU-
  5      n [        U5       H  nU#U==   U U"U
U-
  U4   -  -  ss'   M     MT     UU*    R3                  X5      n$[        U5       H  nU#U   * U$-  U"X4'   M     M     OU" U5      n%[        UU5       H  n
U" U5      n#[
        R                  n&[        SUU-   S-   5       H`  nU
U-
  S:  a    OVUUU-
     R3                  XU-
  5      n [        U5       H  nU#U==   U U"U
U-
  U4   -  -  ss'   M     U&U U%U
U-
     -  -  n&Mb     UU*    R3                  X5      n$[        U5       H  nU#U   * U$-  U"X4'   M     T." XU-
  5      U&-
  U$-  U%U
'   M     U" U5      U" U5      sn'm-[        SU5       H+  n
U'U
S-
     TT/-
  U
-
  S-   -  U
-  R9                  5       U'U
'   M-     [        U5       HF  n
[;        [=        U"SS2U
4   U'5       V#V(s/ s H  u  n#n(U#U(-  R9                  5       PM     sn(n#6 T-U
'   MH     U(       d9  [;        [=        W%U'5       V&V(s/ s H  u  n&n(U&U(-  R9                  5       PM     sn(n&6 n)[        U5       V
s/ s H  n
[-        S
[/        X-   5      -   5      PM     sn
m,U,U-U.4S jn&U(       a&  [        US-   U5       V
s/ s H  n
U&" U
5      PM     nn
O/[        US-   U5       V
s/ s H  n
U&" U
5      T." W)U
5      -   PM     nn
U/ :w  aU  [?        U/T,Q76 nU(       dA  U(       a9  UR)                  SS	5      (       a  [
        R                  / 4$ [
        R                  $ gO0 nU(       a  [
        R                  nOW)nT,SS n[        [=        T,T-5      5       H>  u  nn*UU;   a  UU   U*-  n+T,RA                  U5        OUU*-  n+UU+R9                  5       -  nM@     T,U:w  a2  URC                  [E        [=        T,U5      5      5      nUS[        T,5       m,UR)                  SS	5      (       a  UT,4$ U$ s  snf s  snf s  sn(n#f s  sn(n&f s  sn
f s  sn
f s  sn
f )a  
Given linear recurrence operator `\operatorname{L}` of order
`k` with polynomial coefficients and inhomogeneous equation
`\operatorname{L} y = f`, where `f` is a polynomial, we seek for
all polynomial solutions over field `K` of characteristic zero.

The algorithm performs two basic steps:

    (1) Compute degree `N` of the general polynomial solution.
    (2) Find all polynomials of degree `N` or less
        of `\operatorname{L} y = f`.

There are two methods for computing the polynomial solutions.
If the degree bound is relatively small, i.e. it's smaller than
or equal to the order of the recurrence, then naive method of
undetermined coefficients is being used. This gives a system
of algebraic equations with `N+1` unknowns.

In the other case, the algorithm performs transformation of the
initial equation to an equivalent one for which the system of
algebraic equations has only `r` indeterminates. This method is
quite sophisticated (in comparison with the naive one) and was
invented together by Abramov, Bronstein and Petkovsek.

It is possible to generalize the algorithm implemented here to
the case of linear q-difference and differential equations.

Lets say that we would like to compute `m`-th Bernoulli polynomial
up to a constant. For this we can use `b(n+1) - b(n) = m n^{m-1}`
recurrence, which has solution `b(n) = B_m + C`. For example:

>>> from sympy import Symbol, rsolve_poly
>>> n = Symbol('n', integer=True)

>>> rsolve_poly([-1, 1], 4*n**3, n)
C0 + n**4 - 2*n**3 + n**2

References
==========

.. [1] S. A. Abramov, M. Bronstein and M. Petkovsek, On polynomial
       solutions of linear operator equations, in: T. Levelt, ed.,
       Proc. ISSAC '95, ACM Press, New York, 1995, 290-296.

.. [2] M. Petkovsek, Hypergeometric solutions of linear recurrences
       with polynomial coefficients, J. Symbolic Computation,
       14 (1992), 243-264.

.. [3] M. Petkovsek, H. S. Wilf, D. Zeilberger, A = B, 1996.

N   r   xZc                     U S:  $ Nr    rs    L/var/www/auris/envauris/lib/python3.13/site-packages/sympy/solvers/recurr.py<lambda>rsolve_poly.<locals>.<lambda>       AF    filter	predicatesymbolsFCc                     U S:  $ r'   r(   r)   s    r+   r,   r-      s    Qr/   c                 *    [         R                  /U -  $ Nr   Zeroks    r+   _zero_vector!rsolve_poly.<locals>._zero_vector   s    FF8a<r/   c                 *    [         R                  /U -  $ r7   )r   Oner:   s    r+   _one_vector rsolve_poly.<locals>._one_vector   s    EE7Q;r/   c                    > [         R                  nU R                  TTU-   5      n[        SUS-   5       H4  nU[	        XA-
  S-
  U5      -  nX2U R                  TTU-   U-
  5      -  -  nM6     U$ )Nr#   )r   r?   subsranger   )pr;   BDians        r+   _deltarsolve_poly.<locals>._delta   sr    Aq!a% A1a!e_Xaeai++1q519--- % Hr/   c                     [        X:H  5      $ r7   )int)rH   js     r+   r,   r-      s
    c!&kr/   c                 p   > [        [        TT5       VVs/ s H  u  pUT" X 5      -  PM     snn6 $ s  snnf r7   )r   zip)rH   cqr4   QrK   s      r+   r,   r-   3  s,    cs1ayAytqAfQlNyABAs   2
)#r   is_polynomialis_zerolenr   r   r9   NegativeInfinityrD   r   as_polyLTrN   r
   r   listr   keysmaxdegreegetappendr   stras_exprrC   r   r   expandr   rQ   r   removexreplacedict)0coeffsfrJ   shifthintshomogeneousr*   coeffpolystermsrH   rO   expdbr$   degree_poly	nni_rootsNyE	solutions_CrR   resultAUr<   r@   alphar   r;   rF   rG   VvdenomGgPrE   hrS   sr4   rT   rK   rI   s0     `                                         @@@@r+   rsolve_polyr   E   s   h 	
A??1))KFaA*01&d5!n&F1!QZL!a% Effa(()*AE2E1q5\qa%AH	8A>#9#9!#<==H ! x!HKKMMFS|EH  !HQKA1a!e_8A;?A8A;?QaA  q63q6qc
A&&K1q5\8A;?a58A;'7'===K  U;#"$$(DF,I ^	qb1fX	aiil!!#a'!a00CFA1uyyE**|#vvAvAq1uAHHVC#ai.01211A  q1uA""$QVVAq1u%555A  .a!eQ:	 B6Aq!9"4A6AVVI&FEAIMux&((,0	 ?I"AA	 		 r1q5!AAE"A1a!e_Qx1519q=1!3! % vvE!H1q5\q1uA AE*Aux//115A!H!Qq(H	 & " " 1a121a[ Oq!a%!),A1uqya!e))!U3A"1X!Aa!eQhK/ & - qb	q,qA teemAadG " !" QA1a[ OFFq!a%!),A1uqya!e))!U3A"1X!Aa!eQhK/ & Qq1uX%A - qb	q,qA teemAadG " qa%(1,5!) !, 1~|A1q!Aa!eHA	A.q088:AaD  qAC!Q$ODODAq!A#ODEAaD  Q;Aqslln;<A388<8aVC#ai.()8<B$QUA/!1A/A.3AE1o>o1q!$oA>7a!IyyE22 !|+ vv  IVVFFqTQODAqI~aLNaCahhj F $ 	Bwc!Rj!12wAKyyE""{{ 2N 7F E <<
 0>s0   d!
d /d 0 d%7 d+
)#d1/d6d;c                    [        U5      nUR                  U5      (       d  g[        [        [         U 5      5      n [	        U 5      S-
  nX   U S   peUR                  X"U-
  5      R                  5       n[        S5      n[        XVR                  X"U-   5      U5      nUR                  U5      (       d  UR                  5       u  p[        XU5      n[        [        XSS S9R                  5       5      nU(       d  [        XU40 UD6$ [        R                  [        R                   /US-   -  p[#        [%        ['        U5      5      SS5       H  n[)        XVR                  X"U-   5      U5      n[        X_U5      n[        XoR                  X"U-
  5      U5      nU[+        [#        US-   5       Vs/ s H  nUR                  X"U-
  5      PM     sn6 -  nM     [#        US-   5       Vs/ s H  oR                  X"U-   5      PM     nn[#        US-   5       H9  n[)        X   UU   U5      n[        X   UU5      X'   [        UU   UU5      UU'   M;     [#        US-   5       H!  nX==   [+        USU UUS-   S -   6 -  ss'   M#     [        X[+        U6 -  U40 UD6nUb;  UR-                  S	S
5      (       a  [/        US   U-  5      US   4$ [/        UU-  5      $ gs  snf s  snf )aC  
Given linear recurrence operator `\operatorname{L}` of order `k`
with polynomial coefficients and inhomogeneous equation
`\operatorname{L} y = f`, where `f` is a polynomial, we seek
for all rational solutions over field `K` of characteristic zero.

This procedure accepts only polynomials, however if you are
interested in solving recurrence with rational coefficients
then use ``rsolve`` which will pre-process the given equation
and run this procedure with polynomial arguments.

The algorithm performs two basic steps:

    (1) Compute polynomial `v(n)` which can be used as universal
        denominator of any rational solution of equation
        `\operatorname{L} y = f`.

    (2) Construct new linear difference equation by substitution
        `y(n) = u(n)/v(n)` and solve it for `u(n)` finding all its
        polynomial solutions. Return ``None`` if none were found.

The algorithm implemented here is a revised version of the original
Abramov's algorithm, developed in 1989. The new approach is much
simpler to implement and has better overall efficiency. This
method can be easily adapted to the q-difference equations case.

Besides finding rational solutions alone, this functions is
an important part of Hyper algorithm where it is used to find
a particular solution for the inhomogeneous part of a recurrence.

Examples
========

>>> from sympy.abc import x
>>> from sympy.solvers.recurr import rsolve_ratio
>>> rsolve_ratio([-2*x**3 + x**2 + 2*x - 1, 2*x**3 + x**2 - 6*x,
... - 2*x**3 - 11*x**2 - 18*x - 9, 2*x**3 + 13*x**2 + 22*x + 8], 0, x)
C0*(2*x - 3)/(2*(x**2 - 1))

References
==========

.. [1] S. A. Abramov, Rational solutions of linear difference
       and q-difference equations with polynomial coefficients,
       in: T. Levelt, ed., Proc. ISSAC '95, ACM Press, New York,
       1995, 285-289

See Also
========

rsolve_hyper
Nr#   r   r   r%   c                     U S:  $ r'   r(   r)   s    r+   r,   rsolve_ratio.<locals>.<lambda>  r.   r/   r0   r3   F)r   rU   r[   maprW   rC   rc   r
   r   as_numer_denomr   r   r\   r   r   r?   r9   rD   rN   r]   r   r   r_   r   )rg   rh   rJ   rj   r*   rz   rF   r   resrE   rS   rs   r4   numersrH   rp   rO   denomsr   ry   s                       r+   rsolve_ratior   b  s   j 	
A??1#gv&'FFaA9fQiq	qa%!Ac
A
AvvaQ'
+CQ!!#!lU3#"$$(DF,I 6a1511EEAFF8QU+6s3y>*B3AAvvaQ'+AA!AAvvaQ'+Aq1u>AqvvaQ'>??A 4 -2!a%L9Lq&&E"L9q1uAFIvay!,AFIq!,FIF1Iq!,F1I	  q1uAIvbqzF1q56N:<<I  Vf%5qBEByyE** Q/;;
+++ ?9s   .K&Kc                    [        [        [        U 5      5      n [        U5      n[        U 5      S-
  / [	        5       penUR
                  (       Gd2  UR                  (       a  0 nUR                  5       R                   Hd  nUR                  U5      (       d    gUR                  5        H#  n	[        XU5      (       d  M  Xy==   U-  ss'     MP     [        R                  Xx'   Mf     UR                  5        VV	s/ s H	  u  pX-   PM     n
nn	OUR                  U5      (       a  U/n
Og[        U
5       GHJ  u  p[        R                   U SS p[        R                   /US-   -  n[#        X5      n[%        SUS-   5       HA  nXR'                  X"U-   S-
  5      -  nUR)                  5       u  nnUU==   U-  ss'   UUU'   MC     [%        US-   5       H"  nUU==   [+        USU UUS-   S -   6 -  ss'   M$     [-        U[+        U6 USS9nUb5  Uu  nnU(       a(  UR'                  [/        US/[        U5      -  5      5      nO[1        U[+        U6 U5      nU(       a  X==   U-  ss'   O  g[3        U
6 n[5        U5      nGMM     O[        R                  n[7        S5      nU S   X   R'                  X"U-
  S-   5      nn[        [9        UU5      R                  5       5      n[        [9        UU5      R                  5       5      n[        R                   [        R                   4/nU HD  nU H;  nUR:                  (       a  UR:                  (       a  UU::  a  M-  UUU-
  UU-
  4/-  nM=     MF     U Vs/ s H  nUU-
  [        R                   4PM     snnU Vs/ s H  n[        R                   UU-
  4PM     snnUU-   U-   nU GH  u  nn/ / nnUUR'                  X"U-   S-
  5      -  n[%        US-   5       H  n[+        [%        U5       Vs/ s H  nUR'                  X"U-   5      PM     sn6 n[+        [%        X5       Vs/ s H  nUR'                  X"U-   5      PM     sn6 n[=        X   U-  U-  UU5      n UR?                  U RA                  U5      5        U R
                  (       a  M  UR?                  X   RC                  5       5        M     U(       a  [E        U5      [        R                  n n!O  g[%        US-   5       H6  nX   RG                  U!5      nU[        R                  Ld  M+  U UUU-  -  -  n M8     [9        U U5      R                  5        GH  n"U"R
                  (       a  M  [%        US-   5       Vs/ s H  oU   RI                  5       U"U-  -  PM     n#nU!S:X  aR  S[3        [%        SUS-   5       Vs/ s H  nU#U   U-  PM     sn6 :w  a"  [K        S[M        [        U5      5      -   5      /n$OK[1        U#SU[        U5      SS9u  n$nU$RO                  U5      n$U Vs/ s H  nU$RQ                  U5      PM     n$nU$ H  n%U"U-  U%R'                  X"S-   5      -  U-  U%-  n&[5        U&5      n&Sn'[9        U&R)                  5       S   U5      R                  5        H5  n(U(RS                  [T        5      (       a          gU'U(S-   :  S:X  d  M0  U(S-   n'M7     [W        U&UU'US-
  45      n)U)RS                  [X        [Z        [\        5      (       a  [5        U)5      n)[_        UU)/-   USS	9S:w  d  M  UR?                  U)5        M     GM     GM     URa                  [b        S
9  [        [/        [e        S5      U5      5      n*U* H  u  n%n+WU%U+-  -  nM     URg                  SS5      (       a(  UU* VV,s1 s H  u  nn,UiM
     sn,n-  nW[        U5      4$ W$ s  sn	nf s  snf s  snf s  snf s  snf s  snf s  snf s  snf s  sn,nf )aZ  
Given linear recurrence operator `\operatorname{L}` of order `k`
with polynomial coefficients and inhomogeneous equation
`\operatorname{L} y = f` we seek for all hypergeometric solutions
over field `K` of characteristic zero.

The inhomogeneous part can be either hypergeometric or a sum
of a fixed number of pairwise dissimilar hypergeometric terms.

The algorithm performs three basic steps:

    (1) Group together similar hypergeometric terms in the
        inhomogeneous part of `\operatorname{L} y = f`, and find
        particular solution using Abramov's algorithm.

    (2) Compute generating set of `\operatorname{L}` and find basis
        in it, so that all solutions are linearly independent.

    (3) Form final solution with the number of arbitrary
        constants equal to dimension of basis of `\operatorname{L}`.

Term `a(n)` is hypergeometric if it is annihilated by first order
linear difference equations with polynomial coefficients or, in
simpler words, if consecutive term ratio is a rational function.

The output of this procedure is a linear combination of fixed
number of hypergeometric terms. However the underlying method
can generate larger class of solutions - D'Alembertian terms.

Note also that this method not only computes the kernel of the
inhomogeneous equation, but also reduces in to a basis so that
solutions generated by this procedure are linearly independent

Examples
========

>>> from sympy.solvers import rsolve_hyper
>>> from sympy.abc import x

>>> rsolve_hyper([-1, -1, 1], 0, x)
C0*(1/2 - sqrt(5)/2)**x + C1*(1/2 + sqrt(5)/2)**x

>>> rsolve_hyper([-1, 1], 1 + x, x)
C0 + x*(x + 1)/2

References
==========

.. [1] M. Petkovsek, Hypergeometric solutions of linear recurrences
       with polynomial coefficients, J. Symbolic Computation,
       14 (1992), 243-264.

.. [2] M. Petkovsek, H. S. Wilf, D. Zeilberger, A = B, 1996.
r#   NTr3   r   r%   r4   F)zero)keyr3   )4r[   r   r   rW   setrV   is_Addrc   argsis_hypergeometricr\   r   r   r9   items	enumerater?   r   rD   rC   r   r   r   rQ   r   r   r   r
   r   
is_integerr   r`   rY   r^   r]   nthrb   r   ra   collectrl   hasr   r   r   r   r   r    sortr   r!   r_   )-rg   rh   rJ   rj   r*   kernelr3   similarr   r   inhomogeneousrH   rl   rm   r   r   rO   rE   rS   Rsymsry   r%   	p_factors	q_factorsfactorsrz   rF   degreesrG   rI   rq   polyrp   zrecurr_coeffssolr4   ration0n_rootKskkerr;   s-                                                r+   rsolve_hyperr     s	   n #gv&'F
AVq"cewA99988GXXZ__**1-- A#A!,,
a
 (
 "#GJ % 07}}?tqQUM?M  ##CMm,DA55&)5eeWa!e_F!A1a!e_q519--++-1aAq	 % 1q5\aC&!*va!ef~"=?? " UCL!TBA}4s4!SY78AsF|Q7 A% -(Ff%FE -H c
A!9finnQA	2qAU1a[%%'(IU1a[%%'(Iquu~GA||aQUAEN++	   "++A!a%+A!*+A!%%Q+A'kAoG1RwaffQA	""q1uAa91affQA&9:Aa<1affQA&<=Avy{1}a+DLLa)<<<ux01  'lAFFtAtq1uAHLLOEAFF"1$	  tQ$$&Ayy>CAElKl1X--/14lMKAv!sq!a%$QA]1%5a%7$QRR cCG$5567'q!S\SWX	Tkk$'-12Tsyy|T2Aqa% 0014q8  #E$8$8$:1$=qAFFHFzz!}}#
+4#aZ	 I
 EAr1q5>255$4oFF AfslAE:a?MM!$#  '3 t KK$K%	c"3'0	1B3!c'  yyE"""%"$!QA"%%W&&M @x 	,+ :<. L$Q 3: &s6   8_0__
_#>_(;"_-<_2%_7/_<Nc                   ^ [        U [        5      (       a  U R                  U R                  -
  n UR                  S   m[        ST4S9nU R                  5       R                  UR                  [        SSS95      5      n [        [        5      n/ n[        R                  " U 5       H  nUR                  UR                  5      u  pxU(       d  UR                  U5        M:  U H  n	U	R                  (       a_  U	R                  UR                  :X  aE  U	R                  S   R!                  TU-   5      n
U
b!  U[#        X   5         R                  U5        Ms  [%        SUR                  < S	T< S
U	< S35      e   M     U H  n[        XC   6 XC'   M     S Ul        [        U6 nUR)                  5        H  u  p7[+        U5      XC'   M     [,        R.                  nUR0                  (       dg  UR3                  T5      (       dQ  UR4                  (       a2  [7        U4S jUR                  5       R                   5       5      (       d  [%        SU-  5      eUR9                  5        H\  nUR;                  T5      (       a7  UR=                  T5      (       d  [?        XRA                  5       S   T5      nMN  MP  [%        SU-  5      e   URA                  5       u  pUR=                  T5      (       a  [?        XT5      nU[,        R.                  LaK  UR)                  5        H(  u  p7URA                  5       u  pU[C        XT5      -  XC'   M*     U[C        XT5      -  n[E        URG                  5       5      nUS:  a  [I        U5      n[        S 5      nURK                  TTU-   5      R                  5       nURK                  TTU-   5      R                  5       nUR)                  5        H.  u  p7URK                  TTU-   5      R                  5       UUU-   '   M0     OUn[M        URG                  5       5      n[O        US-   5       Vs/ s H  nUU   PM
     nn[Q        UU* TSS9n
U
c  gU
u  nnU0 / 4;   a  SnU(       a  Ub  [        U[        5      (       a(  [O        [S        U5      5       Vs0 s H	  nUUU   _M     nn/ nUR)                  5        Hq  u  nn [#        U5      nURK                  TU5      U-
  nURW                  [,        RX                  5      (       a  UR[                  TU5      U-
  nUR                  U5        Ms     []        U/UQ76 n
U
(       d  gURK                  U
5      nU$ s  snf s  snf ! [T         aT    UR                  (       a4  UR                  UR                  :X  a  [#        UR                  S   5      n N[%        SU-  5      ef = f)a  
Solve univariate recurrence with rational coefficients.

Given `k`-th order linear recurrence `\operatorname{L} y = f`,
or equivalently:

.. math:: a_{k}(n) y(n+k) + a_{k-1}(n) y(n+k-1) +
          \cdots + a_{0}(n) y(n) = f(n)

where `a_{i}(n)`, for `i=0, \ldots, k`, are polynomials or rational
functions in `n`, and `f` is a hypergeometric function or a sum
of a fixed number of pairwise dissimilar hypergeometric terms in
`n`, finds all solutions or returns ``None``, if none were found.

Initial conditions can be given as a dictionary in two forms:

    (1) ``{  n_0  : v_0,   n_1  : v_1, ...,   n_m  : v_m}``
    (2) ``{y(n_0) : v_0, y(n_1) : v_1, ..., y(n_m) : v_m}``

or as a list ``L`` of values:

    ``L = [v_0, v_1, ..., v_m]``

where ``L[i] = v_i``, for `i=0, \ldots, m`, maps to `y(n_i)`.

Examples
========

Lets consider the following recurrence:

.. math:: (n - 1) y(n + 2) - (n^2 + 3 n - 2) y(n + 1) +
          2 n (n + 1) y(n) = 0

>>> from sympy import Function, rsolve
>>> from sympy.abc import n
>>> y = Function('y')

>>> f = (n - 1)*y(n + 2) - (n**2 + 3*n - 2)*y(n + 1) + 2*n*(n + 1)*y(n)

>>> rsolve(f, y(n))
2**n*C0 + C1*factorial(n)

>>> rsolve(f, y(n), {y(0):0, y(1):3})
3*2**n - 3*factorial(n)

See Also
========

rsolve_poly, rsolve_ratio, rsolve_hyper

r   r;   )excludemT)integerN'(z + k)' expected, got 'c                      gr'   r(   r(   r/   r+   r,   rsolve.<locals>.<lambda>  s    Qr/   c              3   D   >#    U  H  oR                  T5      v   M     g 7fr7   )r   ).0r$   rJ   s     r+   	<genexpr>rsolve.<locals>.<genexpr>  s     "XCWa#6#6q#9#9CWs    zJThe independent term should be a sum of hypergeometric functions, got '%s'r#   z2Polynomial or rational function expected, got '%s'c                  "    [         R                  $ r7   r8   r(   r/   r+   r,   r     s    QVVr/   r   z"Integer or term expected, got '%s')/
isinstancer   lhsrhsr   r	   rc   r   funcr   r[   r   	make_argsas_coeff_mulr`   is_FunctionmatchrN   
ValueErrordefault_factoryr   r   r   r?   rV   r   r   allvaluesis_rational_functionrU   r   r   r   minr\   absrC   r]   rD   r   rW   	TypeErrorr   NaNlimitr   )rh   ru   initr;   h_parti_partr   rl   depr   ry   commoni_numeri_denomnumerr   K_minr   H_partK_maxrH   rg   solutionr3   	equationsr~   eqrJ   s                              @r+   rsolver     s   h !XEEAEEM	q	AS1$A 	

166$sD"9:;AFF]]1^^AFF+
MM% A}}166!1Q/%3vy>*11%856VVQBD D   O	 &F&\FLLNUO	 # UUF>>&":":1"="=MMc"X6==?CWCW"XYYehnnoo%%a((&&q))V%9%9%;A%>B * DuLN N ! ,,.GQVa(QUUHA //1LEc&33FI ' Va00EqyJ^,QA&--/QA&--/HA!JJq!a%0779F1q5M ' E!&uqy!12!1AfQi!1F2&6'1d;F~HgBx4#dD!!(-c$i(89(81AtAwJ(8D9	JJLDAqOF q!$q(Bvvaee}}^^Aq)A-R  ! y+7+}}V,HOO 3 :  O==QVVqvv%5AFF1IA$%IA%MNN	Os   =U##U(U--AW<W)r   r7   )4__doc__collectionsr   sympy.concreter   sympy.core.singletonr   sympy.core.numbersr   r   sympy.core.symbolr   r	   r
   sympy.core.relationalr   sympy.core.addr   sympy.core.mulr   sympy.core.sortingr   sympy.core.sympifyr   sympy.simplifyr   r   r   sympy.solversr   r   sympy.polysr   r   r   r   r   r   sympy.functionsr   r   r   r   sympy.matricesr   r    sympy.utilities.iterablesr!   r   r   r   r   r(   r/   r+   <module>r      sd   /` $ " " * 1 1 *   / & < < : = = R R - 6Zzl^Rjer/   