K i(dZddlmZddlmZmZddlmZddlm Z dZ GddZ d Z Gd d Z Gd d Zy)zRecurrence Operators)S)Symbolsymbols)sstr)sympifyc6t||}||jfS)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') )RecurrenceOperatorAlgebrashift_operator)base generatorrings `/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/holonomic/recurrence.pyRecurrenceOperatorsr s!$ %T9 5D $%% &&c&eZdZdZdZdZeZdZy)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||_t|j|jg||_|t dd|_yt|trt |d|_yt|tr||_yy)NSnF) commutative) r RecurrenceOperatorzerooner r gen_symbol isinstancestrr)selfr r s r__init__z"RecurrenceOperatorAlgebra.__init__;sl 0 YY !4)  %d>DO)S)"))"GIv."+/rcrdt|jzdz|jjz}|S)Nz7Univariate Recurrence Operator Algebra in intermediate z over the base ring )rrr __str__)rstrings rrz!RecurrenceOperatorAlgebra.__str__Js<J4??#$&<= YY   !" rcj|j|jk(r|j|jk(ryy)NTF)r rrothers r__eq__z RecurrenceOperatorAlgebra.__eq__Ss) 99 "t%:J:J'JrN)__name__ __module__ __qualname____doc__rr__repr__r#rrr r s6 ,Hrr ct|t|kr3t||Dcgc] \}}||z c}}|t|dz}|St||Dcgc] \}}||z c}}|t|dz}|Scc}}wcc}}wN)lenzip)list1list2absols r _add_listsr3Zs 5zSZ!$UE!23Aq1u3eCJK6HH J"%UE!23Aq1u3eCJK6HH J43s A>BcReZdZdZdZdZdZdZdZeZ dZ dZ d Z d Z e Zd Zy ) ra 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||_t|trt|D]\}}t|tr2|jj j t|||<Ht||jj jrs|jj j |||<||_ t|jdz |_ y)N) parentrlist enumerateintr from_sympyrdtype listofpolyr,order)r list_of_polyr8ijs rrzRecurrenceOperator.__init__s  lD )!,/ E1a%&*kk&6&6&A&A!A$&GLO#At{{'7'7'='=>&*kk&6&6&A&A!&DLO  E +DO)A- rc|j}|jjt|ts^t||jjj s0|jjj t|g}n|g}n |j}d}||d|}fd}tdt|D] }||}t|||||}"t ||jS)z Multiplies two Operators and returns another RecurrenceOperator instance using the commutation rule Sn * a(n) = a(n + 1) * Sn c^t|tr|Dcgc]}||z c}S||zgScc}wr+)rr9)r1 listofotherrAs r_mul_dmp_diffopz3RecurrenceOperator.__mul__.._mul_dmp_diffops3+t,'23!A33 O$ $4s *rcjg}t|tru|D]n}j|j j dj dt jz}|jj|p|S|j j dj dt jz}|jj||S)Nr) rrr9to_sympysubsgensrOneappendr<)r1r2rArBr s r _mul_Sni_bz.RecurrenceOperator.__mul__.._mul_Sni_bs99+C!T"3A a(--diilDIIaL155 4??1-.Jrr7) r>r8r rrr=r<rranger,r3) rr" listofselfrErFr2rMrAr s @r__mul__zRecurrenceOperator.__mul__s__ {{%!34eT[[%5%5%;%;<#{{//::75>JK  %g **K % jm[9 q#j/* OA$[1KS/*Q-"MNC  O "#t{{33rcft|tst|tr t|}t||jj j s%|jj j|}|jDcgc]}||z }}t||jSycc}wr+) rrr;rr8r r=r<r>)rr"rBr2s r__rmul__zRecurrenceOperator.__rmul__s%!34%%%eT[[%5%5%;%;<))55e<&*oo65196C6%c4;;7 757s B.ct|tr6t|j|j}t||jSt|t r t |}|j}t||jjjs'|jjj|g}n|g}|d|dzg|ddz}t||jS)Nrr7) rrr3r>r8r;rr r=r<)rr"r2 list_self list_others r__add__zRecurrenceOperator.__add__s e/ 0T__e.>.>?C%c4;;7 7%%%IeT[[%5%5%;%;< $ 11==eDE #W Q<*Q-/09QR=@C%c4;;7 7rc|d|zzSNr)r!s r__sub__zRecurrenceOperator.__sub__srUl""rcd|z|zSrXr)r!s r__rsub__zRecurrenceOperator.__rsub__sd{U""rc|dk(r|St|jjjg|j}|dk(r|S|j|jj jk(r\|jjj g|z|jjjgz}t||jS|} |dzr||z}|dz}|s |S||z})Nr7r)rr8r rr>r r)rnresultr2xs r__pow__zRecurrenceOperator.__pow__s 6K#T[[%5%5%9%9$:DKKH 6M ??dkk88CC C;;##(()A-1A1A1E1E0FFC%c4;;7 7 1u!  !GA  FA rc|j}d}t|D]\}}||jjjk(r*|jjj |}|dk(r|dt |zdzz }i|r|dz }|dk(r|dt |zdzz }|dt |zdzdzt |zz }|S) Nr()z + r7z)SnzSn**)r>r:r8r rrHr)rr> print_strrArBs rrzRecurrenceOperator.__str__s__  j) @DAqDKK$$)))   ))!,AAvS47]S00 U" AvS47]U22  tAw,v5Q? ?I# @&rct|tr4j|jk(rj|jk(ryyjd|k(xr!t fdjddDS)NTFrc3bK|]&}|jjju(ywr+)r8r r).0rArs r z,RecurrenceOperator.__eq__..*s&HqT[[%%***Hs,/r7)rrr>r8allr!s` rr#zRecurrenceOperator.__eq__#sk e/ 0%"2"22t{{ell7Rq!U*I HDOOAB4GH H IrN)r$r%r&r' _op_priorityrrPrRrV__radd__rZr\rbrr(r#r)rrrrbsL#JL."04d 88&H##(2HIrrc*eZdZdZgfdZdZeZdZy)HolonomicSequencez 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||_t|ts |g|_n||_t |jdk(rd|_nd|_|j jjd|_ y)NrFT) recurrencerr9u0r,_have_init_condr8r rJr_)rrrrss rrzHolonomicSequence.__init__4sa$"d#dDGDG tww<1 #(D #'D ""'',,Q/rcd|jjdt|jd}|js|Sd}d}|j D]$}|dt|dt|z }|dz }&||z}|S) NzHolonomicSequence(z, rfrdrz, u(z) = r7)rrr(rr_rtrs)rstr_solcond_strseq_strrAr2s rr(zHolonomicSequence.__repr__As26//1K1K1MtTXTZTZ|\##NHGWW d7mT!WEE1  H$CJrc|j|jk7s|j|jk7ry|jr%|jr|j|jk(Sy)NFT)rrr_rtrsr!s rr#zHolonomicSequence.__eq__QsM ??e.. .$&&EGG2C   E$9$977ehh& &rN)r$r%r&r'rr(rr#r)rrrprp-s" ') 0 GrrpN)r'sympy.core.singletonrsympy.core.symbolrrsympy.printingrsympy.core.sympifyrrr r3rrpr)rrr~sB"/&',88vHIHIV))r