
    wg-c                         d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	 ddl
mZ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 ddlmZmZ ddlm Z m!Z!m"Z"m#Z#m$Z$m%Z% ddl&m'Z'm(Z(m)Z)m*Z* ddl+m,Z,m-Z- ddl.m/Z/ ddZ0d Z1d Z2ddZ3y)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           
        ,-./ t        |      }|j                        sy|j                  }t        |       dz
  }| D cg c]  }t	        |       } }t	        d      g|dz   z  }t
        j                  t
        j                  fg|dz   z  }	t        |dz         D ]q  }
t        |
|dz         D ].  }||
xx   | |   t        ||
      j                        z  z  cc<   0 ||
   j                  rS||
   j                         \  \  }}||f|	|
<   s |	d   d   x}}t        d|dz         D ]/  }
|	|
   d   |kD  r|	|
   d   }|	|
   d   |
z
  |kD  s%|	|
   d   |
z
  }1 t        |      t        |      }}t        d      }t
        j                  }t        |dz         D ])  }
|	|
   d   |
z
  |k(  s||	|
   d   t        ||
      z  z  }+ t        t!        ||dd       j#                               }|rt%        |      g}ng }|r|| dz
  gz  }n+||j                        j'                         |z
  | dz
  gz  }t        t%        |            }|dk  r7|r4|j)                  dd	      rt
        j                  g fS t
        j                  S y||k  rg ,t
        j                  x}}t        |dz         D ]9  }
,j+                  t-        d
t/        |
|z         z                |,|
   |
z  z  z  }; t        |dz         D ].  }
|| |
   j1                         |j3                  |
z         z  z  }0 t5        ||z
  ,      }|*,},D cg c]	  }||vs| c},|j3                  |      }ny|}||z   |z   dz   }t        t!        ||   dd       j#                               }|g k7  rt%        |      dz   /nt
        j                  /d }d }/fd.i }t        | |dz         D ]  }
 ||dz         }t        d|dz         D ]  }||dz
     ||
z   |z
  dz   z  |z  ||<    t
        j                  ||
<   t        |dz         D ]T  }t        |dz         D ]A  }t        ||
|z         }  .||   j1                         |      }!||
xx   ||   | z  |!z  z  cc<   C V  t7        ||d       }"|rt        ||      D ]  }
 ||      }#t        d||z   dz         D ]O  }|
|z
  dk  r nE|||z
     j3                  ||
|z
        } t        |      D ]  }|#|xx   | |"|
|z
  |f   z  z  cc<    Q ||    j3                  ||
      }$t        |      D ]  }|#|    |$z  |"|
|f<     n ||      }%t        ||      D ]  }
 ||      }#t
        j                  }&t        d||z   dz         D ]]  }|
|z
  dk  r nS|||z
     j3                  ||
|z
        } t        |      D ]  }|#|xx   | |"|
|z
  |f   z  z  cc<    |&| |%|
|z
     z  z  }&_ ||    j3                  ||
      }$t        |      D ]  }|#|    |$z  |"|
|f<     .||
|z
        |&z
  |$z  |%|
<     ||       ||      c}'-t        d|      D ]*  }
|'|
dz
     /z
  |
z
  dz   z  |
z  j9                         |'|
<   , t        |      D ]B  }
t;        t=        |"dd|
f   |'      D #(cg c]  \  }#}(|#|(z  j9                          c}(}# -|
<   D |s6t;        t=        %|'      D &(cg c]  \  }&}(|&|(z  j9                          c}(}& })t        |      D 
cg c]  }
t-        d
t/        |
|z         z          c}
,,-.fd}&|r#t        |dz   |      D 
cg c]
  }
 |&|
       }}
n,t        |dz   |      D 
cg c]  }
 |&|
       .)|
      z    }}
|g k7  rEt?        |g, }|s9|r4|j)                  dd	      rt
        j                  g fS t
        j                  S yi }|rt
        j                  }n)},dd }t        t=        ,-            D ];  \  }}*||v r||   |*z  }+,jA                  |       n||*z  }+||+j9                         z  }= ,|k7  r2|jC                  tE        t=        ,|                  }|dt        ,       ,|j)                  dd	      r|,fS |S c c}w c c}w c c}(}#w c c}(}&w c c}
w c c}
w c c}
w )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                     | dk\  S Nr    rs    Y/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/solvers/recurr.py<lambda>zrsolve_poly.<locals>.<lambda>   
    AF     filter	predicatesymbolsFCc                     | dk\  S r'   r(   r)   s    r+   r,   zrsolve_poly.<locals>.<lambda>   s
    Q r.   c                 *    t         j                  g| z  S Nr   Zeroks    r+   _zero_vectorz!rsolve_poly.<locals>._zero_vector   s    FF8a<r.   c                 *    t         j                  g| z  S r6   )r   Oner9   s    r+   _one_vectorz rsolve_poly.<locals>._one_vector   s    EE7Q;r.   c                     t         j                  }| j                  |z         }t        d|dz         D ]5  }|t	        ||z
  dz
  |      z  }||| j                  |z   |z
        z  z  }7 |S )Nr#   )r   r=   subsranger   )pr:   BDians        r+   _deltazrsolve_poly.<locals>._delta   s{    Aq!a% A1a!e_ .Xa!eai++Q1q519---. Hr.   c                     t        | |k(        S r6   )int)rE   js     r+   r,   zrsolve_poly.<locals>.<lambda>   s    c!q&k r.   c                 n    t        t              D cg c]  \  }}| ||       z   c}} S c c}}w r6   )r   zip)rE   cqr3   QrH   s      r+   r,   zrsolve_poly.<locals>.<lambda>3  s.    cs1ayAtq!AfQlNAB As   1
)#r   is_polynomialis_zerolenr   r   r8   NegativeInfinityrA   r   as_polyLTrJ   r
   r   listr   keysmaxdegreegetappendr   stras_exprr@   r   r   expandr   rM   r   removexreplacedict)0coeffsfrG   shifthintshomogeneousr*   coeffpolystermsrE   rK   expdbr$   degree_poly	nni_rootsNyE	solutions_CrN   resultAUr;   r>   alphar   r:   rC   rD   VvdenomGgPrB   hrO   sr3   rP   rH   rF   s0     `                                         @@@@r+   rsolve_polyr   E   st	   h 	
A??1))KFaA*01d5!n1F1!QZL!a% Effa(()*AE2E1q5\ $q!a% 	>A!Hq	8Aq>#9#9!#<==H	> Qx!!HKKMMFSEs|E!H$ !HQKA1a!e_  8A;?aA8A;?QaaA  q63q6qAc
A&&K1q5\ >8A;?a58A;'71'===K> U;#"$$(DF,I ^	qb1fX	aiil!!#a'!a00CFA1uyyE*|#vvAvAq1u 	AHHVC#a%i.01211A	 q1u 	6A""$QVVAq1u%555A	6 .a!eQ:	 B6q!9"46AVVI&FEAIMuQx&((,0	 ?I"AA	 		 r1q5! 	)AAE"A1a!e_ 4Qx1q519q=1!3!4 vvE!H1q5\ )q1u )A AE*AuQx//115A!H!Qq(H	))	) 1a121a[ , Oq!a%!), 0A1uqya!e))!QU3A"1X 0!Aa!eQhK/00 qb	q!,q ,A teemAadG,," QA1a[ 6 OFFq!a%!), 	&A1uqya!e))!QU3A"1X 0!Aa!eQhK/0 Qq1uX%A	& qb	q!,q ,A teemAadG, q!a%(1,5!)6, 1~|A1q! 	;Aa!eHA	A.q088:AaD	; q 	FAC!Q$ODDAq!A#DEAaD	F Q;Aqslln;<A388<aVC#a%i.()<B$QUA/!1/A/.3AE1o>1q!$>A>7a!IyyE2 !|+ vvIVVFFqTQO 	!DAqI~aLNaCahhj F	! 	Bwc!Rj!12wAKyyE"{{ 2N 7F E <<
 0>s0   b&	b+b+6b03b6
"!b< ccc                    t        |      }|j                  |      syt        t        t         |             } t	        |       dz
  }| |   | d   }}|j                  |||z
        j                         }t        d      }t        ||j                  |||z         |      }|j                  |      s |j                         \  }	}
t        |	|
|      }t        t        ||dd       j                               }|st        | ||fi |S t        j                  t        j                   g|dz   z  }}t#        t%        t'        |            dd      D ]  }t)        ||j                  |||z         |      }t        |||      }t        ||j                  |||z
        |      }|t+        t#        |dz         D cg c]  }|j                  |||z
         c} z  } t#        |dz         D cg c]  }|j                  |||z          }}t#        |dz         D ];  }t)        | |   ||   |      }t        | |   ||      ||<   t        ||   ||      ||<   = t#        |dz         D ]!  }||xx   t+        |d| ||dz   d z    z  cc<   # t        ||t+        | z  |fi |}|6|j-                  d	d
      rt/        |d   |z        |d   fS t/        ||z        S yc c}w c c}w )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 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                     | dk\  S r'   r(   r)   s    r+   r,   zrsolve_ratio.<locals>.<lambda>  r-   r.   r/   r2   F)r   rQ   rW   maprS   r@   r_   r
   r   as_numer_denomr   r   rX   r   r   r=   r8   rA   rJ   rY   r   r   r[   r   )rc   rd   rG   rf   r*   rv   rC   r   resrB   rO   ro   r3   numersrE   rl   rK   denomsr}   ru   s                       r+   rsolve_ratior   b  s   j 	
A??1#gv&'FFaA!9fQiqA	q!a%!Ac
A
AqvvaQ'
+CQ!!#1!QlU3#"$$(DF,I 61a1511EEAFF8QU+6s3y>*B3 	@AAqvvaQ'+AAq!AAqvvaQ'+Aq1u>AqvvaQ'>??A	@ -2!a%L9q!&&AE"99q1u 	-AF1Ivay!,AF1Iq!,F1IF1Iq!,F1I		- q1u 	=A1IvbqzF1q56N:<<I	= VQf%5qBEByyE* Q/;;
+++ ?9s   1K%Kc                 *   t        t        t        |             } t        |      }t        |       dz
  g t	               }}}|j
                  s|j                  ri }|j                         j                  D ]Y  }|j                  |      s y|j                         D ]  }	t        ||	|      s||	xx   |z  cc<    F t        j                  ||<   [ |j                         D 	cg c]
  \  }}	||	z    }
}}	n|j                  |      r|g}
nyt        |
      D ]C  \  }}t        j                   | dd }}t        j                   g|dz   z  }t#        ||      }t%        d|dz         D ]B  }||j'                  |||z   dz
        z  }|j)                         \  }}||xx   |z  cc<   |||<   D t%        |dz         D ]!  }||xx   t+        |d| ||dz   d z    z  cc<   # t-        |t+        | |d      }|0|\  }}|r<|j'                  t/        |dgt        |      z              }nt1        |t+        | |      }|r|
|xx   |z  cc<   n yt3        |
 }t5        |      }F nt        j                  }t7        d      }| d   | |   j'                  |||z
  dz         }}t        t9        ||      j                               }t        t9        ||      j                               }t        j                   t        j                   fg}|D ]5  }|D ].  }|j:                  r|j:                  r||k  r!|||z
  ||z
  fgz  }0 7 |D cg c]  }||z
  t        j                   f c}}|D cg c]  }t        j                   ||z
  f c}}||z   |z   }|D ]X  \  }}g g }}||j'                  |||z   dz
        z  }t%        |dz         D ]  }t+        t%        |      D cg c]  }|j'                  |||z          c} }t+        t%        ||      D cg c]  }|j'                  |||z          c} }t=        | |   |z  |z  ||      } |j?                  | jA                  |             | j
                  r|j?                  ||   jC                                 |rtE        |      t        j                  } }!n yt%        |dz         D ]4  }||   jG                  |!      }|t        j                  us*| |||z  z  z  } 6 t9        | |      j                         D ]  }"|"j
                  rt%        |dz         D cg c]  }||   jI                         |"|z  z   }#}|!dk(  rNdt3        t%        d|dz         D cg c]
  }|#|   |z   c} k7  r"tK        dtM        t        |            z         g}$nKt1        |#d|t        |      d      \  }$}|$jO                  |      }$|D cg c]  }|$jQ                  |       }$}|$D ]  }%|"|z  |%j'                  ||dz         z  |z  |%z  }&t5        |&      }&d}'t9        |&j)                         d   |      j                         D ]-  }(|(jS                  tT              r    y|'|(dz   k  dk(  s)|(dz   }'/ tW        |&||'|dz
  f      })|)jS                  tX        tZ        t\              rt5        |)      })t_        ||)gz   |d	      dk7  s|j?                  |)         [ |ja                  tb        
       t        t/        te        d      |            }*|*D ]  \  }%}+|%|+z  z  } |jg                  dd      r%||*D ,ch c]  \  }},|	 c},}z  }t        |      fS S c c}	}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c},}w )a  
    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r2   r   r%   r3   F)zero)keyr2   )4rW   r   r   rS   setrR   is_Addr_   argsis_hypergeometricrX   r   r   r8   items	enumerater=   r   rA   r@   r   r   r   rM   r   r   r   r
   r   
is_integerr   r\   rU   rZ   rY   nthr^   r   r]   collectrh   hasr   r   r   r   r   r    sortr   r!   r[   )-rc   rd   rG   rf   r*   kernelr2   similarr}   r   inhomogeneousrE   rh   ri   r   r   rK   rB   rO   Rsymsru   r%   	p_factors	q_factorsfactorsrv   rC   degreesrD   rF   rm   polyrl   zrecurr_coeffssolr3   ration0n_rootKskkerr:   s-                                                r+   rsolve_hyperr     sI   n #gv&'F
AVq"cewvA9988GXXZ__ 	(**1-  (A#Aq!,
a
(
 "#GAJ	( 07}}?tq!QU?M?  #CMm, "	&DAq55&)5EeeWa!e_F!QA1a!e_ 1q519--++-1aAq	 1q5\ @aC&!*va!ef~"=??@ UCL!TBA}4s4!SY78AsF|Q7a A% -(Ff%FE"	&H c
A!9fQinnQA	2qAU1a[%%'(IU1a[%%'(Iquu~G , 	,A||aQUAEN++		,, "++A!a%+A!*+A!%%Q+A'kAoG 8%1RwaffQA	""q1u 	2Aa91affQA&9:Aa<1affQA&<=Avay{1}a+DLLa)<<uQx01	2 'lAFFtAq1u 	%A!HLLOEAFF"1$		% tQ$$& 	%Ayy>CAElKU1X--/14KMKAv!sq!a%$QA]1%5a%7$QRR cCG$5567'q!S\SWX	Tkk$'-12syy|22 %Aq!a% 0014q8  #E$8$8$:1$=qAFFH (Fzz!}#
+4#aZ	(
 EAr1q5>255$4oF AfslAE:a?MM!$#%	%38%t KK$K%	c"3'0	1B 3!c' yyE""%$!QA%%W&&M @x 	,+ :<. L$Q 3: &s6   #]&?],"]1]6];. ^ ,^^
^Nc                 
   t        | t              r| j                  | j                  z
  } |j                  d   t        df      }| j                         j                  |j                  t        dd                  } t        t              }g }t        j                  |       D ]  }|j                  |j                        \  }}|s|j                  |       5|D ]  }	|	j                  r]|	j                  |j                  k(  rD|	j                  d   j!                  |z         }
|
!|t#        |
|            j                  |       lt%        d|j                  d	d
|	d        |D ]  }t        ||    ||<    d |_        t        | }|j)                         D ]  \  }}t+        |      ||<    t,        j.                  }|j0                  sW|j3                        sF|j4                  r,t7        fd|j                         j                  D              st%        d|z        |j9                         D ]Q  }|j;                        r1|j=                        r&t?        ||jA                         d         }Et%        d|z         |jA                         \  }}|j=                        rt?        ||      }|t,        j.                  urN|j)                         D ]+  \  }}|jA                         \  }}|tC        ||      z  ||<   - |tC        ||      z  }tE        |jG                               }|dk  rtI        |      }t        d       }|jK                  |z         j                         }|jK                  |z         j                         }|j)                         D ].  \  }}|jK                  |z         j                         |||z   <   0 n|}tM        |jG                               }tO        |dz         D cg c]  }||   	 }}tQ        || d      }
|
y|
\  }}|i g fv rd}|r|t        |t              r%tO        tS        |            D ci c]  }|||   
 }}g }|j)                         D ]k  \  }}	 t#        |      }|jK                  |      |z
  }|jW                  t,        jX                        r|j[                  |      |z
  }|j                  |       m t]        |g| }
|
sy|jK                  |
      }|S c c}w c c}w # tT        $ rO |j                  r2|j                  |j                  k(  rt#        |j                  d         }nt%        d|z        Y w xY w)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                       yr'   r(   r(   r.   r+   r,   zrsolve.<locals>.<lambda>  s    r.   c              3   @   K   | ]  }|j                          y wr6   )r   ).0r$   rG   s     r+   	<genexpr>zrsolve.<locals>.<genexpr>  s     "Xa1#6#6q#9"Xs   zJThe independent term should be a sum of hypergeometric functions, got '%s'r#   z2Polynomial or rational function expected, got '%s'c                  "    t         j                  S r6   r7   r(   r.   r+   r,   zrsolve.<locals>.<lambda>  s
    QVV r.   r   z"Integer or term expected, got '%s')/
isinstancer   lhsrhsr   r	   r_   r   funcr   rW   r   	make_argsas_coeff_mulr\   is_FunctionmatchrJ   
ValueErrordefault_factoryr   r   r   r=   rR   r   r   allvaluesis_rational_functionrQ   r   r   r   minrX   absr@   rY   rA   r   rS   	TypeErrorr   NaNlimitr   )rd   rq   initr:   h_parti_partr}   rh   depr   ru   commoni_numeri_denomnumerr{   K_minr   H_partK_maxrE   rc   solutionr2   	equationsrz   eqrG   s                              @r+   rsolver     s   h !XEEAEEM	q	AS1$A 	

166$sD"9:;AFF]]1 D^^AFF+
sMM%  	DA}}166!1Q/%3vay>*11%856VVQBD D	DD  $Oq	$&F&\FLLN $5UOq	$ UUF>>&":":1"=MMc"X6==?CWCW"XYehnnoo N%%a(&&q)VU%9%9%;A%>BDuLN NN ,,.GWQVWa(QUU 	4HAu //1LE5c&%33F1I	4 VWa00EqyJ^,QA&--/QA&--/ 	:HAu!JJq!a%0779F1q5M	: E!&uqy!12AfQi2F2&6'1d;F~HgBx4#dD!(-c$i(891AtAwJ9D9	JJL 	!DAq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   TTTAU21U2)r   r6   )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.   