o
    §ƒðj‰(  ã                   @   sv   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d„ Z
G dd	„ d	ƒZd
d„ ZG dd„ dƒZG dd„ dƒZdS )zRecurrence Operatorsé    )ÚS)ÚSymbolÚsymbols)Ússtr)Úsympifyc                 C   s   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Úring© r   úœ/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/holonomic/recurrence.pyÚRecurrenceOperators	   s   

r   c                   @   s,   e Zd ZdZdd„ Zdd„ ZeZd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                 C   sh   || _ t|j|jg| ƒ| _|d u rtddd�| _d S t|tƒr(t|dd�| _d S t|t	ƒr2|| _d S d S )NÚSnF)Úcommutative)
r	   ÚRecurrenceOperatorÚzeroÚoner   r   Ú
gen_symbolÚ
isinstanceÚstrr   )Úselfr	   r
   r   r   r   Ú__init__;   s   ÿ


ÿz"RecurrenceOperatorAlgebra.__init__c                 C   s    dt | jƒ d | j ¡  }|S )Nz7Univariate Recurrence Operator Algebra in intermediate z over the base ring )r   r   r	   Ú__str__)r   Ústringr   r   r   r   J   s   ÿÿþz!RecurrenceOperatorAlgebra.__str__c                 C   s    | j |j kr| j|jkrdS dS )NTF)r	   r   ©r   Úotherr   r   r   Ú__eq__S   s   z RecurrenceOperatorAlgebra.__eq__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Ú__repr__r   r   r   r   r   r      s    r   c                 C   s`   t | ƒt |ƒkrdd„ t| |ƒD ƒ|t | ƒd …  }|S dd„ t| |ƒD ƒ| t |ƒd …  }|S )Nc                 S   ó   g | ]\}}|| ‘qS r   r   ©Ú.0ÚaÚbr   r   r   Ú
<listcomp>\   ó    z_add_lists.<locals>.<listcomp>c                 S   r#   r   r   r$   r   r   r   r(   ^   r)   )ÚlenÚzip)Úlist1Úlist2Úsolr   r   r   Ú
_add_listsZ   s
   $$ÿr/   c                   @   sd   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ ZeZ	dd„ Z
dd„ Zdd„ Zdd„ ZeZd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                 C   s€   || _ t|tƒr6t|ƒD ]&\}}t|tƒr!| j j t|ƒ¡||< qt|| j jjƒs2| j j |¡||< q|| _	t
| j	ƒd | _d S )Né   )Úparentr   ÚlistÚ	enumerateÚintr	   Ú
from_sympyr   ÚdtypeÚ
listofpolyr*   Úorder)r   Úlist_of_polyr2   ÚiÚjr   r   r   r   Š   s   

€zRecurrenceOperator.__init__c                    sª   | j }| jj‰ t|tƒs#t|| jjjƒs| jj t|ƒ¡g}n|g}n|j }dd„ }||d |ƒ}‡ fdd„}tdt	|ƒƒD ]}||ƒ}t
|||| |ƒƒ}q>t|| jƒS )zŸ
        Multiplies two Operators and returns another
        RecurrenceOperator instance using the commutation rule
        Sn * a(n) = a(n + 1) * Sn
        c                    s&   t |tƒr‡ fdd„|D ƒS ˆ | gS )Nc                    s   g | ]}|ˆ  ‘qS r   r   ©r%   r;   ©r'   r   r   r(   ±   ó    zGRecurrenceOperator.__mul__.<locals>._mul_dmp_diffop.<locals>.<listcomp>)r   r3   )r'   Úlistofotherr   r>   r   Ú_mul_dmp_diffop¯   s   

z3RecurrenceOperator.__mul__.<locals>._mul_dmp_diffopr   c                    s†   ˆ j g}t| tƒr*| D ]}ˆ  |¡ ˆ jd ˆ jd tj ¡}| ˆ  	|¡¡ q|S |  ˆ jd ˆ jd tj ¡}| ˆ  	|¡¡ |S )Nr   )
r   r   r3   Úto_sympyÚsubsÚgensr   ÚOneÚappendr6   )r'   r.   r;   r<   ©r	   r   r   Ú
_mul_Sni_b·   s   
$ýz.RecurrenceOperator.__mul__.<locals>._mul_Sni_br1   )r8   r2   r	   r   r   r7   r6   r   Úranger*   r/   )r   r   Ú
listofselfr@   rA   r.   rH   r;   r   rG   r   Ú__mul__›   s   
zRecurrenceOperator.__mul__c                    s^   t ˆ tƒs-t ˆ tƒrtˆ ƒ‰ t ˆ | jjjƒs| jj ˆ ¡‰ ‡ fdd„| jD ƒ}t|| jƒS d S )Nc                    s   g | ]}ˆ | ‘qS r   r   )r%   r<   ©r   r   r   r(   Ö   r?   z/RecurrenceOperator.__rmul__.<locals>.<listcomp>)	r   r   r5   r   r2   r	   r7   r6   r8   )r   r   r.   r   rL   r   Ú__rmul__Í   s   

÷zRecurrenceOperator.__rmul__c                 C   sŽ   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  g|dd …  }t|| jƒS )Nr   r1   )
r   r   r/   r8   r2   r5   r   r	   r7   r6   )r   r   r.   Ú	list_selfÚ
list_otherr   r   r   Ú__add__Ù   s   

zRecurrenceOperator.__add__c                 C   s   | d|  S ©Néÿÿÿÿr   r   r   r   r   Ú__sub__î   ó   zRecurrenceOperator.__sub__c                 C   s   d|  | S rQ   r   r   r   r   r   Ú__rsub__ñ   rT   zRecurrenceOperator.__rsub__c                 C   s–   |dkr| S t | jjjg| jƒ}|dkr|S | j| jjjkr2| jjjg| | jjjg }t || jƒS | }	 |d r=||9 }|dL }|sF	 |S ||9 }q5)Nr1   r   Té   )r   r2   r	   r   r8   r   r   )r   ÚnÚresultr.   Úxr   r   r   Ú__pow__ô   s$   ÿúzRecurrenceOperator.__pow__c                 C   s¦   | j }d}t|ƒD ]G\}}|| jjjkrq	| jj |¡}|dkr+|dt|ƒ d 7 }q	|r1|d7 }|dkr@|dt|ƒ d 7 }q	|dt|ƒ d d t|ƒ 7 }q	|S )	NÚ r   ú(ú)z + r1   z)SnzSn**)r8   r4   r2   r	   r   rB   r   )r   r8   Ú	print_strr;   r<   r   r   r   r     s    "zRecurrenceOperator.__str__c                    sX   t |tƒrˆ j|jkrˆ j|jkrdS dS ˆ jd |ko+t‡ fdd„ˆ jdd … D ƒƒS )NTFr   c                 3   s   � | ]
}|ˆ j jju V  qd S )N)r2   r	   r   r=   ©r   r   r   Ú	<genexpr>*  s   € z,RecurrenceOperator.__eq__.<locals>.<genexpr>r1   )r   r   r8   r2   Úallr   r   r_   r   r   #  s   
ÿzRecurrenceOperator.__eq__N)r   r   r    r!   Ú_op_priorityr   rK   rM   rP   Ú__radd__rS   rU   rZ   r   r"   r   r   r   r   r   r   b   s    %2r   c                   @   s0   e Zd ZdZg fdd„Zdd„ ZeZd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                 C   sP   || _ t|tƒs|g| _n|| _t| jƒdkrd| _nd| _|jjjd | _	d S )Nr   FT)
Ú
recurrencer   r3   Úu0r*   Ú_have_init_condr2   r	   rD   rW   )r   re   rf   r   r   r   r   4  s   

zHolonomicSequence.__init__c                 C   sb   d| j  ¡ t| jƒf }| js|S d}d}| jD ]}|dt|ƒt|ƒf 7 }|d7 }q|| }|S )NzHolonomicSequence(%s, %s)r[   r   z, u(%s) = %sr1   )re   r"   r   rW   rg   rf   )r   Ústr_solÚcond_strÚseq_strr;   r.   r   r   r   r"   A  s   

zHolonomicSequence.__repr__c                 C   s8   | j |j ks| j|jkrdS | jr|jr| j|jkS dS )NFT)re   rW   rg   rf   r   r   r   r   r   Q  s
   zHolonomicSequence.__eq__N)r   r   r    r!   r   r"   r   r   r   r   r   r   rd   -  s    rd   N)r!   Úsympy.core.singletonr   Úsympy.core.symbolr   r   Úsympy.printingr   Úsympy.core.sympifyr   r   r   r/   r   rd   r   r   r   r   Ú<module>   s    ; L