
    \h(                         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      R/var/www/auris/envauris/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_listsr9   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
listofpolyr2   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                 j    [        U[        5      (       a  U Vs/ s H  o"U -  PM	     sn$ X-  /$ s  snf r1   )r   r@   )r7   listofotherrH   s      r   _mul_dmp_diffop3RecurrenceOperator.__mul__.<locals>._mul_dmp_diffop   s6    +t,,'23{!A{33O$$ 4s   0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   r@   to_sympysubsgensr   OneappendrC   )r7   r8   rH   rI   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>   )rE   r?   r   r   r   rD   rC   r   ranger2   r9   )	r   r%   
listofselfrM   rN   r8   rV   rH   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U R                   Vs/ s H  o!U-  PM	     nn[        X0R                  5      $ g s  snf r1   )	r   r   rB   r   r?   r   rD   rC   rE   )r   r%   rI   r8   s       r   __rmul__RecurrenceOperator.__rmul__   s    %!344%%%%e[[%5%5%;%;<<))55e<&*oo6o19oC6%c;;77 5 7s   B=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US   US   -   /USS  -   n[        X R                  5      $ )Nr   r>   )
r   r   r9   rE   r?   rB   r   r   rD   rC   )r   r%   r8   	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
Q<*Q-/09QR=@C%c;;77r   c                     U SU-  -   $ Nr/   r$   s     r   __sub__RecurrenceOperator.__sub__   s    rUl""r   c                     SU -  U-   $ re   r/   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   rE   r
   r   )r   nresultr8   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**)rE   rA   r?   r   r   rQ   r   )r   rE   	print_strrH   rI   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                   ^  [        U[        5      (       a6  T R                  UR                  :X  a  T R                  UR                  :X  a  ggT R                  S   U:H  =(       a"    [	        U 4S jT R                  SS   5       5      $ )NTFr   c              3   f   >#    U  H&  oTR                   R                  R                  L v   M(     g 7fr1   )r?   r   r   ).0rH   r   s     r   	<genexpr>,RecurrenceOperator.__eq__.<locals>.<genexpr>*  s&     H4GqT[[%%***4Gs   .1r>   )r   r   rE   r?   allr$   s   ` r   r&   RecurrenceOperator.__eq__#  sn    e/00%"2"22t{{ell7Rq!U* IHDOOAB4GHH	Ir   )rE   rF   r?   N)r(   r)   r*   r+   r,   _op_priorityr   rZ   r]   rb   __radd__rg   rj   rq   r    r-   r&   r.   r/   r   r   r   r   b   sL    #J L."04d
88& H##(2 HI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   r@   u0r2   _have_init_condr?   r   rS   rn   )r   r   r   s      r   r   HolonomicSequence.__init__4  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, rv   rt   r   z, u(z) = r>   )r   r-   r   rn   r   r   )r   str_solcond_strseq_strrH   r8   s         r   r-   HolonomicSequence.__repr__A  sy    26//1K1K1MtTXTZTZ|\##NHGWWd7mT!WEE1  $CJr   c                     U R                   UR                   :w  d  U R                  UR                  :w  a  gU R                  (       a*  UR                  (       a  U R                  UR                  :H  $ g)NFT)r   rn   r   r   r$   s     r   r&   HolonomicSequence.__eq__Q  sM    ??e...$&&EGG2CE$9$977ehh&&r   )r   rn   r   r   N)
r(   r)   r*   r+   r,   r   r-   r    r&   r.   r/   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	   r9   r   r   r/   r   r   <module>r      sB     " /  &',8 8vHI HIV) )r   