
    Цi*                         S r SSKJr  SSKJrJr  SSKJr  SSKJ	r	  S r
 " S S5      rS	 r " S
 S5      r " S S5      rg)zRecurrence Operators    )S)Symbolsymbols)sstr)sympifyc                 2    [        X5      nX"R                  4$ )a  
Returns an Algebra of Recurrence Operators and the operator for
shifting i.e. the `Sn` operator.
The first argument needs to be the base polynomial ring for the algebra
and the second argument must be a generator which can be either a
noncommutative Symbol or a string.

Examples
========

>>> from sympy import ZZ
>>> from sympy import symbols
>>> from sympy.holonomic.recurrence import RecurrenceOperators
>>> n = symbols('n', integer=True)
>>> R, Sn = RecurrenceOperators(ZZ.old_poly_ring(n), 'Sn')
)RecurrenceOperatorAlgebrashift_operator)base	generatorrings      Y/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/sympy/holonomic/recurrence.pyRecurrenceOperatorsr   	   s    $ %T5D%%&&    c                   .    \ rS rSrSrS rS r\rS rSr	g)r	      a  
A Recurrence Operator Algebra is a set of noncommutative polynomials
in intermediate `Sn` and coefficients in a base ring A. It follows the
commutation rule:
Sn * a(n) = a(n + 1) * Sn

This class represents a Recurrence Operator Algebra and serves as the parent ring
for Recurrence Operators.

Examples
========

>>> from sympy import ZZ
>>> from sympy import symbols
>>> from sympy.holonomic.recurrence import RecurrenceOperators
>>> n = symbols('n', integer=True)
>>> R, Sn = RecurrenceOperators(ZZ.old_poly_ring(n), 'Sn')
>>> R
Univariate Recurrence Operator Algebra in intermediate Sn over the base ring
ZZ[n]

See Also
========

RecurrenceOperator
c                    Xl         [        UR                  UR                  /U 5      U l        Uc  [        SSS9U l        g [        U[        5      (       a  [        USS9U l        g [        U[        5      (       a  X l        g g )NSnF)commutative)
r   RecurrenceOperatorzerooner
   r   
gen_symbol
isinstancestrr   )selfr   r   s      r   __init__"RecurrenceOperatorAlgebra.__init__;   sn    	0YY!4) %d>DO)S))"))"GIv.."+ /r   c                 r    S[        U R                  5      -   S-   U R                  R                  5       -   nU$ )Nz7Univariate Recurrence Operator Algebra in intermediate z over the base ring )r   r   r   __str__)r   strings     r   r    !RecurrenceOperatorAlgebra.__str__J   s<    J4??#$&<=YY!" r   c                 n    U R                   UR                   :X  a  U R                  UR                  :X  a  ggNTF)r   r   r   others     r   __eq__ RecurrenceOperatorAlgebra.__eq__S   s)    99

"t%:J:J'Jr   )r   r   r
   N)
__name__
__module____qualname____firstlineno____doc__r   r    __repr__r'   __static_attributes__ r   r   r	   r	      s    6, Hr   r	   c                    [        U 5      [        U5      ::  a3  [        X5       VVs/ s H	  u  p#X#-   PM     snnU[        U 5      S  -   nU$ [        X5       VVs/ s H	  u  p#X#-   PM     snnU [        U5      S  -   nU$ s  snnf s  snnf N)lenzip)list1list2absols        r   
_add_listsr:   Z   s    
5zSZ!$U!23!2qu!23eCJK6HH J "%U!23!2qu!23eCJK6HHJ 43s   A?Bc                   Z    \ rS rSrSrSrS rS rS rS r	\	r
S rS	 rS
 rS r\rS rSrg)r   b   a  
The Recurrence Operators are defined by a list of polynomials
in the base ring and the parent ring of the Operator.

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

Takes a list of polynomials for each power of Sn and the
parent ring which must be an instance of RecurrenceOperatorAlgebra.

A Recurrence Operator can be created easily using
the operator `Sn`. See examples below.

Examples
========

>>> from sympy.holonomic.recurrence import RecurrenceOperator, RecurrenceOperators
>>> from sympy import ZZ
>>> from sympy import symbols
>>> n = symbols('n', integer=True)
>>> R, Sn = RecurrenceOperators(ZZ.old_poly_ring(n),'Sn')

>>> RecurrenceOperator([0, 1, n**2], R)
(1)Sn + (n**2)Sn**2

>>> Sn*n
(n + 1)Sn

>>> n*Sn*n + 1 - Sn**2*n
(1) + (n**2 + n)Sn + (-n - 2)Sn**2

See Also
========

DifferentialOperatorAlgebra
   c                    X l         [        U[        5      (       a  [        U5       H  u  p4[        U[        5      (       a2  U R                   R
                  R                  [        U5      5      X'   ML  [        X@R                   R
                  R                  5      (       a  M|  U R                   R
                  R                  U5      X'   M     Xl	        [        U R                  5      S-
  U l        g )N   )parentr   list	enumerateintr   
from_sympyr   dtype
listofpolyr3   order)r   list_of_polyr@   ijs        r   r   RecurrenceOperator.__init__   s      lD))!,/a%%&*kk&6&6&A&A!A$&GLO#A{{'7'7'='=>>&*kk&6&6&A&A!&DLO	 0 +O)A-
r   c                   ^ U R                   nU R                  R                  m[        U[        5      (       db  [        XR                  R                  R
                  5      (       d0  U R                  R                  R                  [        U5      5      /nOU/nOUR                   nS nU" US   U5      nU4S jn[        S[        U5      5       H  nU" U5      n[        XT" X'   U5      5      nM!     [	        XPR                  5      $ )z
Multiplies two Operators and returns another
RecurrenceOperator instance using the commutation rule
Sn * a(n) = a(n + 1) * Sn
c                 v    [        U[        5      (       a   / nU H  nUR                  X0-  5        M     U$ X-  /$ r2   )r   rA   append)r8   listofotherr9   rI   s       r   _mul_dmp_diffop3RecurrenceOperator.__mul__.<locals>._mul_dmp_diffop   s<    +t,,$AJJqu% %
((r   r   c                   > TR                   /n[        U [        5      (       aw  U  Ho  nTR                  U5      R	                  TR
                  S   TR
                  S   [        R                  -   5      nUR                  TR                  U5      5        Mq     U$ U R	                  TR
                  S   TR
                  S   [        R                  -   5      nUR                  TR                  U5      5        U$ )Nr   )
r   r   rA   to_sympysubsgensr   OnerN   rD   )r8   r9   rI   rJ   r   s       r   
_mul_Sni_b.RecurrenceOperator.__mul__.<locals>._mul_Sni_b   s    99+C!T""Aa(--diilDIIaL155<PQAJJtq12  J FF499Q<1)=>

4??1-.Jr   r?   )rF   r@   r   r   r   rE   rD   r   ranger3   r:   )	r   r&   
listofselfrO   rP   r9   rW   rI   r   s	           @r   __mul__RecurrenceOperator.__mul__   s     __
{{%!344e[[%5%5%;%;<<#{{//::75>JK  %g**K	) jm[9	 q#j/*A$[1KS/*-"MNC	 + "#{{33r   c                    [        U[        5      (       d  [        U[        5      (       a  [        U5      n[        XR                  R
                  R                  5      (       d%  U R                  R
                  R                  U5      n/ nU R                   H  nUR                  X-  5        M     [        X R                  5      $ g r2   )
r   r   rC   r   r@   r   rE   rD   rF   rN   )r   r&   r9   rJ   s       r   __rmul__RecurrenceOperator.__rmul__   s    %!344%%%%e[[%5%5%;%;<<))55e<C__

59% % &c;;77 5r   c                    [        U[        5      (       a5  [        U R                  UR                  5      n[        X R                  5      $ [        U[
        5      (       a  [        U5      nU R                  n[        XR                  R                  R                  5      (       d'  U R                  R                  R                  U5      /nOU/n/ nUR                  US   US   -   5        X#SS  -  n[        X R                  5      $ )Nr   r?   )r   r   r:   rF   r@   rC   r   r   rE   rD   rN   )r   r&   r9   	list_self
list_others        r   __add__RecurrenceOperator.__add__   s    e/00T__e.>.>?C%c;;77 %%%%Ie[[%5%5%;%;<< $11==eDE
#W
CJJy|jm34QR= C%c;;77r   c                     U SU-  -   $ Nr0   r%   s     r   __sub__RecurrenceOperator.__sub__   s    rUl""r   c                     SU -  U-   $ rf   r0   r%   s     r   __rsub__RecurrenceOperator.__rsub__   s    d{U""r   c                    US:X  a  U $ [        U R                  R                  R                  /U R                  5      nUS:X  a  U$ U R                  U R                  R
                  R                  :X  a[  U R                  R                  R                  /U-  U R                  R                  R                  /-   n[        X0R                  5      $ U n US-  (       a  X$-  nUS-  nU(       d   U$ XD-  nM#  )Nr?   r      )r   r@   r   r   rF   r
   r   )r   nresultr9   xs        r   __pow__RecurrenceOperator.__pow__   s    6K#T[[%5%5%9%9$:DKKH6M??dkk88CCC;;##(()A-1A1A1E1E0FFC%c;;771u!GA FA r   c                    U R                   nSn[        U5       H  u  p4X@R                  R                  R                  :X  a  M*  U R                  R                  R                  U5      nUS:X  a  US[        U5      -   S-   -  nMk  U(       a  US-  nUS:X  a  US[        U5      -   S-   -  nM  US[        U5      -   S-   S-   [        U5      -   -  nM     U$ )	N r   ()z + r?   z)SnzSn**)rF   rB   r@   r   r   rS   r   )r   rF   	print_strrI   rJ   s        r   r    RecurrenceOperator.__str__  s    __
	j)DAKK$$)))  ))!,AAvS47]S00	U"	AvS47]U22	tAw,v5Q??I# *& r   c                 6   [        U[        5      (       a6  U R                  UR                  :X  a  U R                  UR                  :X  a  ggU R                  S   U:X  a;  U R                  SS   H'  nX R                  R                  R
                  Ld  M'    g   gg)NTFr   r?   )r   r   rF   r@   r   r   )r   r&   rI   s      r   r'   RecurrenceOperator.__eq__,  s~    e/00%"2"22t{{ell7Rq!U*,A 0 0 5 55$ - r   )rF   rG   r@   N)r)   r*   r+   r,   r-   _op_priorityr   r[   r^   rc   __radd__rh   rk   rr   r    r.   r'   r/   r0   r   r   r   r   b   sK    #J L."44l88* H##(2 Hr   r   c                   4    \ rS rSrSr/ 4S jrS r\rS rSr	g)HolonomicSequencei<  z
A Holonomic Sequence is a type of sequence satisfying a linear homogeneous
recurrence relation with Polynomial coefficients. Alternatively, A sequence
is Holonomic if and only if its generating function is a Holonomic Function.
c                     Xl         [        U[        5      (       d	  U/U l        OX l        [	        U R                  5      S:X  a  SU l        OSU l        UR                  R                  R                  S   U l	        g )Nr   FT)

recurrencer   rA   u0r3   _have_init_condr@   r   rU   ro   )r   r   r   s      r   r   HolonomicSequence.__init__C  s`    $"d##dDGGtww<1#(D #'D ""'',,Q/r   c                    SU R                   R                  5       < S[        U R                  5      < S3nU R                  (       d  U$ SnSnU R
                   H'  nUS[        U5      < S[        U5      < 3-  nUS-  nM)     X-   nU$ )	NzHolonomicSequence(z, rw   ru   r   z, u(z) = r?   )r   r.   r   ro   r   r   )r   str_solcond_strseq_strrI   r9   s         r   r.   HolonomicSequence.__repr__P  sy    26//1K1K1MtTXTZTZ|\##NHGWWd7mT!WEE1  $CJr   c                     U R                   UR                   :X  aZ  U R                  UR                  :X  a?  U R                  (       a-  UR                  (       a  U R                  UR                  :X  a  gggggr$   )r   ro   r   r   r%   s     r   r'   HolonomicSequence.__eq__`  sT    ??e...vv ''E,A,Aww%((*#$r   )r   ro   r   r   N)
r)   r*   r+   r,   r-   r   r.   r    r'   r/   r0   r   r   r   r   <  s"     ') 0 Gr   r   N)r-   sympy.core.singletonr   sympy.core.symbolr   r   sympy.printingr   sympy.core.sympifyr   r   r	   r:   r   r   r0   r   r   <module>r      s@     " /  &',8 8vW Wt1 1r   