§
    ]²Åj±*  ã                   ó˜   — 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      úKC:\Projects\Historical\venv\Lib\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   r1   z_add_lists.<locals>.<listcomp>^   r2   r   )ÚlenÚzip)Úlist1Úlist2Úsols      r   Ú
_add_listsr9   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Ú
listofpolyr4   Ú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                 óz   — t          |t          ¦  «        r!g }|D ]}|                     || z  ¦  «         Œ|S | |z  gS ©N)r   r?   Úappend)r0   Úlistofotherr8   rG   s       r   Ú_mul_dmp_diffopz3RecurrenceOperator.__mul__.<locals>._mul_dmp_diffop¯   sS   € Ý˜+¥tÑ,Ô,ð )Ø�Ø$ð &ð &�AØ—J’J˜q 1™uÑ%Ô%Ð%Ð%Ø�
à˜K™Ð(Ð(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   ÚOnerL   rB   )r0   r8   rG   rH   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=   )rD   r>   r   r   r   rC   rB   r   Úranger4   r9   )	r   r#   Ú
listofselfrM   rN   r8   rT   rG   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                             |¦  «        }g }| j        D ]}| 	                    ||z  ¦  «         Œt          || j        ¦  «        S d S rK   )
r   r   rA   r   r>   r   rC   rB   rD   rL   )r   r#   r8   rH   s       r   Ú__rmul__zRecurrenceOperator.__rmul__Ñ   s¯   € Ý˜%Õ!3Ñ4Ô4ð 	8å˜%¥Ñ%Ô%ð !Ý˜%™œ�å˜e T¤[Ô%5Ô%;Ñ<Ô<ð =ØœÔ)×5Ò5°eÑ<Ô<�àˆCØ”_ð &ð &�Ø—
’
˜5 1™9Ñ%Ô%Ð%Ð%å% 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}g }| 
                    |d         |d         z   ¦  «         ||dd …         z  }t          || j        ¦  «        S )Nr   r=   )r   r   r9   rD   r>   rA   r   r   rC   rB   rL   )r   r#   r8   Ú	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�
ØˆCØ�JŠJ�y ”| j°¤mÑ3Ñ4Ô4Ð4Ø�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   rD   r
   r   )r   ÚnÚresultr8   Ú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**)rD   r@   r>   r   r   rP   r   )r   rD   Ú	print_strrG   rH   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    r*| j        dd …         D ]}|| j        j        j        ur dS ŒdS dS )NTFr   r=   )r   r   rD   r>   r   r   )r   r#   rG   s      r   r$   zRecurrenceOperator.__eq__,  s’   € Ý�eÕ/Ñ0Ô0ð 	ØŒ %Ô"2Ò2Ð2°t´{ÀeÄlÒ7RÐ7RØ�tà�uàŒ˜qÔ! UÒ*Ð*Øœ¨¨¨Ô,ð %ð %�AØ ¤Ô 0Ô 5Ð5Ð5Ø$˜u˜uð 6à�tà�ur   N)r%   r&   r'   r(   Ú_op_priorityr   rW   rY   r]   Ú__radd__ra   rc   ri   r   r)   r$   r*   r   r   r   r   b   s¼   € € € € € ð#ð #ðJ €Lð.ð .ð .ð"44ð 44ð 44ðl8ð 8ð 8ð8ð 8ð 8ð* €Hð#ð #ð #ð#ð #ð #ðð ð ð(ð ð ð2 €Hðð ð ð ð 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?   Úu0r4   Ú_have_init_condr>   r   rR   rf   )r   ru   rv   s      r   r   zHolonomicSequence.__init__C  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=   )ru   r)   r   rf   rw   rv   )r   Ústr_solÚcond_strÚseq_strrG   r8   s         r   r)   zHolonomicSequence.__repr__P  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    r6| j        |j        k    r$| j        r|j        r| j        |j        k    rdS dS dS dS dS r!   )ru   rf   rw   rv   r"   s     r   r$   zHolonomicSequence.__eq__`  s^   € ØŒ?˜eÔ.Ò.Ð.ØŒv˜œÒ Ð ØÔ'ð  ¨EÔ,Að  Ø”w %¤(Ò*Ð*Ø#˜tà$˜uà˜4à�uà�5r   N)r%   r&   r'   r(   r   r)   r   r$   r*   r   r   rs   rs   <  s\   € € € € € ðð ð ')ð 0ð 0ð 0ð 0ðð ð ð €Gðð ð ð ð r   rs   N)r(   Úsympy.core.singletonr   Úsympy.core.symbolr   r   Úsympy.printingr   Úsympy.core.sympifyr   r   r	   r9   r   rs   r*   r   r   ú<module>r�      s  ðØ Ð à "Ð "Ð "Ð "Ð "Ð "Ø /Ð /Ð /Ð /Ð /Ð /Ð /Ð /Ø Ð Ð Ð Ð Ð Ø &Ð &Ð &Ð &Ð &Ð &ð'ð 'ð 'ð,8ð 8ð 8ð 8ð 8ñ 8ô 8ð 8ðvð ð ðWð Wð Wð Wð Wñ Wô Wð Wðt1ð 1ð 1ð 1ð 1ñ 1ô 1ð 1ð 1ð 1r   