§
    OŠtj‰(  ã                   ó˜   — d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	 d„ Z
 G d„ d¦  «        Zd	„ Z G d
„ d¦  «        Z G d„ d¦  «        ZdS )zRecurrence Operatorsé    )ÚS)ÚSymbolÚsymbols)Ússtr)Úsympifyc                 ó4   — t          | |¦  «        }||j        fS )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      úX/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/sympy/holonomic/recurrence.pyÚRecurrenceOperatorsr   	   s!   € õ$ % T¨9Ñ5Ô5€DØ�$Ô%Ð&Ð&ó    c                   ó(   — e Zd ZdZd„ Zd„ ZeZd„ ZdS )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        |j        g| ¦  «        | _        |€t          dd¬¦  «        | _        d S t          |t          ¦  «        rt          |d¬¦  «        | _        d S t          |t          ¦  «        r	|| _        d S d S )NÚSnF)Úcommutative)
r   ÚRecurrenceOperatorÚzeroÚoner
   r   Ú
gen_symbolÚ
isinstanceÚstrr   )Úselfr   r   s      r   Ú__init__z"RecurrenceOperatorAlgebra.__init__;   sš   € àˆŒ	å0ØŒY˜œÐ! 4ñ)ô )ˆÔð ÐÝ% d¸Ð>Ñ>Ô>ˆDŒOˆOˆOå˜)¥SÑ)Ô)ð ,Ý")¨)ÀÐ"GÑ"GÔ"G�”��Ý˜I¥vÑ.Ô.ð ,Ø"+�”��ð,ð ,r   c                 ón   — dt          | j        ¦  «        z   dz   | j                             ¦   «         z   }|S )Nz7Univariate Recurrence Operator Algebra in intermediate z over the base ring )r   r   r   Ú__str__)r   Ústrings     r   r   z!RecurrenceOperatorAlgebra.__str__J   s>   € ØJÝ�4”?Ñ#Ô#ñ$Ø&<ñ=àŒY×ÒÑ!Ô!ñ"ˆð ˆr   c                 óJ   — | j         |j         k    r| j        |j        k    rdS dS )NTF)r   r   ©r   Úothers     r   Ú__eq__z RecurrenceOperatorAlgebra.__eq__S   s*   € ØŒ9˜œ
Ò"Ð" t¤¸%Ô:JÒ'JÐ'JØ�4à�5r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Ú__repr__r#   © r   r   r	   r	      sR   € € € € € ðð ð6,ð ,ð ,ðð ð ð €Hðð ð ð ð r   r	   c                 ó  — t          | ¦  «        t          |¦  «        k    r3d„ t          | |¦  «        D ¦   «         |t          | ¦  «        d …         z   }n2d„ t          | |¦  «        D ¦   «         | t          |¦  «        d …         z   }|S )Nc                 ó   — g | ]
\  }}||z   ‘ŒS r)   r)   ©Ú.0ÚaÚbs      r   ú
<listcomp>z_add_lists.<locals>.<listcomp>\   ó    € Ð3Ð3Ð3™˜˜Aˆq�1‰uÐ3Ð3Ð3r   c                 ó   — g | ]
\  }}||z   ‘ŒS r)   r)   r,   s      r   r0   z_add_lists.<locals>.<listcomp>^   r1   r   )ÚlenÚzip)Úlist1Úlist2Úsols      r   Ú
_add_listsr8   Z   s‚   € Ý
ˆ5�z„z•S˜‘Z”ZÒÐØ3Ð3¥ U¨EÑ!2Ô!2Ð3Ñ3Ô3°e½CÀ¹J¼J¸K¸KÔ6HÑHˆˆà3Ð3¥ U¨EÑ!2Ô!2Ð3Ñ3Ô3°e½CÀ¹J¼J¸K¸KÔ6HÑHˆØ€Jr   c                   óT   — e Zd ZdZdZd„ Zd„ Zd„ Zd„ ZeZ	d„ Z
d„ Zd	„ Zd
„ ZeZd„ ZdS )r   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                 óº  — || _         t          |t          ¦  «        r¢t          |¦  «        D ]‹\  }}t          |t          ¦  «        r0| j         j                             t          |¦  «        ¦  «        ||<   ŒJt          || j         j        j        ¦  «        s"| j         j                             |¦  «        ||<   ŒŒ|| _	        t          | j	        ¦  «        dz
  | _        d S )Né   )Úparentr   ÚlistÚ	enumerateÚintr   Ú
from_sympyr   ÚdtypeÚ
listofpolyr3   Úorder)r   Úlist_of_polyr=   ÚiÚjs        r   r   zRecurrenceOperator.__init__Š   sÌ   € ð ˆŒõ �l¥DÑ)Ô)ð 	+Ý! ,Ñ/Ô/ð Eð E‘��1Ý˜a¥Ñ%Ô%ð EØ&*¤kÔ&6×&AÒ&AÅ!ÀAÁ$Ä$Ñ&GÔ&G�L ‘O�OÝ# A t¤{Ô'7Ô'=Ñ>Ô>ð EØ&*¤kÔ&6×&AÒ&AÀ!Ñ&DÔ&D�L ‘Oøà*ˆDŒOÝ˜œÑ)Ô)¨AÑ-ˆŒ
ˆ
ˆ
r   c                 óø  ‡— | j         }| j        j        Št          |t          ¦  «        sQt          || j        j        j        ¦  «        s.| j        j                             t          |¦  «        ¦  «        g}n|g}n|j         }d„ } ||d         |¦  «        }ˆfd„}t          dt          |¦  «        ¦  «        D ]-} ||¦  «        }t          | |||         |¦  «        ¦  «        }Œ.t	          || j        ¦  «        S )zŸ
        Multiplies two Operators and returns another
        RecurrenceOperator instance using the commutation rule
        Sn * a(n) = a(n + 1) * Sn
        c                 óV   ‡ — t          |t          ¦  «        rˆ fd„|D ¦   «         S ‰ |z  gS )Nc                 ó   •— g | ]}|‰z  ‘ŒS r)   r)   )r-   rF   r/   s     €r   r0   zGRecurrenceOperator.__mul__.<locals>._mul_dmp_diffop.<locals>.<listcomp>±   s   ø€ Ð3Ð3Ð3 !˜˜A™Ð3Ð3Ð3r   )r   r>   )r/   Úlistofothers   ` r   Ú_mul_dmp_diffopz3RecurrenceOperator.__mul__.<locals>._mul_dmp_diffop¯   s<   ø€ Ý˜+¥tÑ,Ô,ð 4Ø3Ð3Ð3Ð3 {Ð3Ñ3Ô3Ð3Ø˜‘OÐ$Ð$r   r   c                 óø  •— ‰j         g}t          | t          ¦  «        rz| D ]v}‰                     |¦  «                             ‰j        d         ‰j        d         t          j        z   ¦  «        }|                     ‰ 	                    |¦  «        ¦  «         Œwna|                      ‰j        d         ‰j        d         t          j        z   ¦  «        }|                     ‰ 	                    |¦  «        ¦  «         |S )Nr   )
r   r   r>   Úto_sympyÚsubsÚgensr   ÚOneÚappendrA   )r/   r7   rF   rG   r   s       €r   Ú
_mul_Sni_bz.RecurrenceOperator.__mul__.<locals>._mul_Sni_b·   sÒ   ø€ Ø”9�+ˆCå˜!�TÑ"Ô"ð /Øð 3ð 3�AØŸš aÑ(Ô(×-Ò-¨d¬i¸¬l¸D¼IÀa¼LÍ1Ì5Ñ<PÑQÔQ�AØ—J’J˜tŸš¨qÑ1Ô1Ñ2Ô2Ð2Ð2ð3ð
 —F’F˜4œ9 Qœ<¨¬°1¬½¼Ñ)=Ñ>Ô>�Ø—
’
˜4Ÿ?š?¨1Ñ-Ô-Ñ.Ô.Ð.àˆJr   r<   )rC   r=   r   r   r   rB   rA   r   Úranger3   r8   )	r   r"   Ú
listofselfrK   rL   r7   rS   rF   r   s	           @r   Ú__mul__zRecurrenceOperator.__mul__›   s  ø€ ð ”_ˆ
ØŒ{Ôˆå˜%Õ!3Ñ4Ô4ð 	+Ý˜e T¤[Ô%5Ô%;Ñ<Ô<ð &Ø#œ{Ô/×:Ò:½7À5¹>¼>ÑJÔJÐK��ð  %˜g��àÔ*ˆKð	%ð 	%ð 	%ð
 ˆo˜j¨œm¨[Ñ9Ô9ˆð	ð 	ð 	ð 	ð 	õ �q�#˜j™/œ/Ñ*Ô*ð 	Oð 	OˆAà$˜* [Ñ1Ô1ˆKå˜S / /°*¸Q´-ÀÑ"MÔ"MÑNÔNˆCˆCå! # t¤{Ñ3Ô3Ð3r   c                 óF  ‡— t          ‰t          ¦  «        sŠt          ‰t          ¦  «        rt          ‰¦  «        Št          ‰| j        j        j        ¦  «        s| j        j                             ‰¦  «        Šˆfd„| j        D ¦   «         }t          || j        ¦  «        S d S )Nc                 ó   •— g | ]}‰|z  ‘ŒS r)   r)   )r-   rG   r"   s     €r   r0   z/RecurrenceOperator.__rmul__.<locals>.<listcomp>Ö   s   ø€ Ð6Ð6Ð6 �5˜1‘9Ð6Ð6Ð6r   )	r   r   r@   r   r=   r   rB   rA   rC   )r   r"   r7   s    ` r   Ú__rmul__zRecurrenceOperator.__rmul__Í   sœ   ø€ Ý˜%Õ!3Ñ4Ô4ð 		8å˜%¥Ñ%Ô%ð !Ý˜%™œ�å˜e T¤[Ô%5Ô%;Ñ<Ô<ð =ØœÔ)×5Ò5°eÑ<Ô<�à6Ð6Ð6Ð6 d¤oÐ6Ñ6Ô6ˆCÝ% c¨4¬;Ñ7Ô7Ð7ð		8ð 		8r   c                 óÊ  — t          |t          ¦  «        r/t          | j        |j        ¦  «        }t          || j        ¦  «        S t          |t
          ¦  «        rt          |¦  «        }| j        }t          || j        j        j        ¦  «        s!| j        j         	                    |¦  «        g}n|g}|d         |d         z   g|dd …         z   }t          || j        ¦  «        S )Nr   r<   )
r   r   r8   rC   r=   r@   r   r   rB   rA   )r   r"   r7   Ú	list_selfÚ
list_others        r   Ú__add__zRecurrenceOperator.__add__Ù   sÓ   € Ý�eÕ/Ñ0Ô0ð 	8å˜Tœ_¨eÔ.>Ñ?Ô?ˆCÝ% c¨4¬;Ñ7Ô7Ð7õ ˜%¥Ñ%Ô%ð !Ý˜%™œ�ØœˆIÝ˜e T¤[Ô%5Ô%;Ñ<Ô<ð %Ø $¤Ô1×=Ò=¸eÑDÔDÐE�
�
à#˜W�
Ø˜Q”< *¨Q¤-Ñ/Ð0°9¸Q¸R¸R´=Ñ@ˆCå% c¨4¬;Ñ7Ô7Ð7r   c                 ó   — | d|z  z   S ©Néÿÿÿÿr)   r!   s     r   Ú__sub__zRecurrenceOperator.__sub__î   s   € Ø�r˜U‘lÑ"Ð"r   c                 ó   — d| z  |z   S r_   r)   r!   s     r   Ú__rsub__zRecurrenceOperator.__rsub__ñ   s   € Ø�d‰{˜UÑ"Ð"r   c                 óT  — |dk    r| S t          | j        j        j        g| j        ¦  «        }|dk    r|S | j        | j        j        j        k    r=| j        j        j        g|z  | j        j        j        gz   }t          || j        ¦  «        S | }	 |dz  r||z  }|dz  }|sn||z  }Œ|S )Nr<   r   Té   )r   r=   r   r   rC   r
   r   )r   ÚnÚresultr7   Úxs        r   Ú__pow__zRecurrenceOperator.__pow__ô   sÎ   € Ø�Š6ˆ6ØˆKÝ# T¤[Ô%5Ô%9Ð$:¸D¼KÑHÔHˆØ�Š6ˆ6ØˆMàŒ?˜dœkÔ8ÔCÒCÐCØ”;Ô#Ô(Ð)¨AÑ-°´Ô1AÔ1EÐ0FÑFˆCÝ% c¨4¬;Ñ7Ô7Ð7Øˆð	Ø�1‰uð Ø˜!‘�Ø�!‰GˆAØð ØØ�‰FˆAð	ð ˆr   c                 óŒ  — | j         }d}t          |¦  «        D ]ª\  }}|| j        j        j        k    rŒ| j        j                             |¦  «        }|dk    r|dt          |¦  «        z   dz   z  }ŒY|r|dz  }|dk    r|dt          |¦  «        z   dz   z  }Œ|dt          |¦  «        z   dz   dz   t          |¦  «        z   z  }Œ«|S )	NÚ r   ú(ú)z + r<   z)SnzSn**)rC   r?   r=   r   r   rN   r   )r   rC   Ú	print_strrF   rG   s        r   r   zRecurrenceOperator.__str__  sæ   € Ø”_ˆ
Øˆ	å˜jÑ)Ô)ð 	@ð 	@‰DˆAˆqØ�D”KÔ$Ô)Ò)Ð)Øà”Ô ×)Ò)¨!Ñ,Ô,ˆAà�AŠvˆvØ˜S¥4¨¡7¤7™]¨SÑ0Ñ0�	Øàð #Ø˜UÑ"�	à�AŠvˆvØ˜S¥4¨¡7¤7™]¨UÑ2Ñ2�	Øà˜�t A™wœw™¨Ñ,¨vÑ5½¸Q¹¼Ñ?Ñ?ˆIˆIàÐr   c                 óè   ‡ — t          |t          ¦  «        r$‰ j        |j        k    r‰ j        |j        k    rdS dS ‰ j        d         |k    o't	          ˆ fd„‰ j        dd …         D ¦   «         ¦  «        S )NTFr   c              3   ó>   •K  — | ]}|‰j         j        j        u V — Œd S )N)r=   r   r   )r-   rF   r   s     €r   ú	<genexpr>z,RecurrenceOperator.__eq__.<locals>.<genexpr>*  s0   øè è € ÐHÐH¨q��T”[Ô%Ô*Ð*ÐHÐHÐHÐHÐHÐHr   r<   )r   r   rC   r=   Úallr!   s   ` r   r#   zRecurrenceOperator.__eq__#  s„   ø€ Ý�eÕ/Ñ0Ô0ð 	ØŒ %Ô"2Ò2Ð2°t´{ÀeÄlÒ7RÐ7RØ�tà�uØŒ˜qÔ! UÒ*ð IÝÐHÐHÐHÐH°D´OÀAÀBÀBÔ4GÐHÑHÔHÑHÔHð	Ir   N)r$   r%   r&   r'   Ú_op_priorityr   rV   rY   r]   Ú__radd__ra   rc   ri   r   r(   r#   r)   r   r   r   r   b   sÁ   € € € € € ð#ð #ðJ €Lð.ð .ð .ð"04ð 04ð 04ðd
8ð 
8ð 
8ð8ð 8ð 8ð& €Hð#ð #ð #ð#ð #ð #ðð ð ð(ð ð ð2 €HðIð Ið Ið Ið Ir   r   c                   ó,   — e Zd ZdZg fd„Zd„ ZeZd„ ZdS )Ú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          |t          ¦  «        s	|g| _        n|| _        t	          | j        ¦  «        dk    rd| _        nd| _        |j        j        j        d         | _	        d S )Nr   FT)
Ú
recurrencer   r>   Úu0r3   Ú_have_init_condr=   r   rP   rf   )r   rx   ry   s      r   r   zHolonomicSequence.__init__4  sl   € Ø$ˆŒÝ˜"�dÑ#Ô#ð 	Ø�dˆDŒGˆGàˆDŒGåˆtŒw‰<Œ<˜1ÒÐØ#(ˆDÔ Ð à#'ˆDÔ ØÔ"Ô'Ô,¨QÔ/ˆŒˆˆr   c                 óö   — d| j                              ¦   «         ›dt          | j        ¦  «        ›d�}| j        s|S d}d}| j        D ],}|dt          |¦  «        ›dt          |¦  «        ›�z  }|dz  }Œ-||z   }|S )	NzHolonomicSequence(z, rm   rk   r   z, u(z) = r<   )rx   r(   r   rf   rz   ry   )r   Ústr_solÚcond_strÚseq_strrF   r7   s         r   r(   zHolonomicSequence.__repr__A  s˜   € € Ø26´/×1KÒ1KÑ1MÔ1MÐ1MÐ1MÍtÐTXÔTZÉ|Ì|È|È|Ð\ˆØÔ#ð 
	ØˆNàˆHØˆGØ”Wð ð �Ø�­d°7©m¬m¨m¨m½TÀ!¹W¼W¸WÐEÑE�Ø˜1‘��à˜HÑ$ˆCØˆJr   c                 ó†   — | j         |j         k    s| j        |j        k    rdS | j        r|j        r| j        |j        k    S dS )NFT)rx   rf   rz   ry   r!   s     r   r#   zHolonomicSequence.__eq__Q  sN   € ØŒ?˜eÔ.Ò.Ð.°$´&¸E¼GÒ2CÐ2CØ�5ØÔð 	' EÔ$9ð 	'Ø”7˜eœhÒ&Ð&Øˆtr   N)r$   r%   r&   r'   r   r(   r   r#   r)   r   r   rv   rv   -  s\   € € € € € ðð ð ')ð 0ð 0ð 0ð 0ðð ð ð €Gðð ð ð ð r   rv   N)r'   Úsympy.core.singletonr   Úsympy.core.symbolr   r   Úsympy.printingr   Úsympy.core.sympifyr   r   r	   r8   r   rv   r)   r   r   ú<module>r„      s  ðØ Ð à "Ð "Ð "Ð "Ð "Ð "Ø /Ð /Ð /Ð /Ð /Ð /Ð /Ð /Ø Ð Ð Ð Ð Ð Ø &Ð &Ð &Ð &Ð &Ð &ð'ð 'ð 'ð,8ð 8ð 8ð 8ð 8ñ 8ô 8ð 8ðvð ð ðHIð HIð HIð HIð HIñ HIô HIð HIðV)ð )ð )ð )ð )ñ )ô )ð )ð )ð )r   