o
    oh*                     @   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   n/var/www/html/construction_image-detection-poc/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
   $$r0   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_polyr3   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   s4   t |trg }|D ]	}|||   q	|S | | gS N)r   r4   append)r(   listofotherr/   r<   r   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   r4   to_sympysubsgensr   Oner?   r7   )r(   r/   r<   r=   r	   r   r   
_mul_Sni_b   s   
$z.RecurrenceOperator.__mul__.<locals>._mul_Sni_br2   )r9   r3   r	   r   r   r8   r7   r   ranger+   r0   )r   r   
listofselfr@   rA   r/   rG   r<   r   rF   r   __mul__   s   
	zRecurrenceOperator.__mul__c                 C   sh   t |ts2t |trt|}t || jjjs| jj|}g }| jD ]	}|	||  q"t|| jS d S r>   )
r   r   r6   r   r3   r	   r8   r7   r9   r?   )r   r   r/   r=   r   r   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}g }|
|d |d   ||dd  7 }t|| jS )Nr   r2   )r   r   r0   r9   r3   r6   r   r	   r8   r7   r?   )r   r   r/   	list_self
list_otherr   r   r   __add__   s   

zRecurrenceOperator.__add__c                 C   s   | d|  S Nr   r   r   r   r   __sub__      zRecurrenceOperator.__sub__c                 C   s   d|  | S rO   r   r   r   r   r   __rsub__   rR   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)Nr2   r   T   )r   r3   r	   r   r9   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 + r2   z)SnzSn**)r9   r5   r3   r	   r   rB   r   )r   r9   	print_strr<   r=   r   r   r   r     s    "zRecurrenceOperator.__str__c                 C   sh   t |tr| j|jkr| j|jkrdS dS | jd |kr2| jdd  D ]}|| jjjur/ dS q#dS dS )NTFr   r2   )r   r   r9   r3   r	   r   )r   r   r<   r   r   r   r   ,  s   
zRecurrenceOperator.__eq__N)r   r    r!   r"   _op_priorityr   rJ   rK   rN   __radd__rQ   rS   rX   r   r#   r   r   r   r   r   r   b   s    %6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   r4   u0r+   _have_init_condr3   r	   rD   rU   )r   r`   ra   r   r   r   r   C  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)rY   r   z, u(%s) = %sr2   )r`   r#   r   rU   rb   ra   )r   str_solcond_strseq_strr<   r/   r   r   r   r#   P  s   

zHolonomicSequence.__repr__c                 C   sD   | j |j kr | j|jkr| jr|jr| j|jkrdS dS dS dS dS r   )r`   rU   rb   ra   r   r   r   r   r   `  s   zHolonomicSequence.__eq__N)r   r    r!   r"   r   r#   r   r   r   r   r   r   r_   <  s    r_   N)r"   sympy.core.singletonr   sympy.core.symbolr   r   sympy.printingr   sympy.core.sympifyr   r   r   r0   r   r_   r   r   r   r   <module>   s    ; [