a
    kh(                     @   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	generatorZring r   H/var/www/auris/lib/python3.9/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   s`   || _ t|j|jg| | _|d u r2tddd| _n*t|trLt|dd| _nt|t	r\|| _d S )NZSnF)Z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 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r6dd t| |D |t | d   }n$dd t| |D | t |d   }|S )Nc                 S   s   g | ]\}}|| qS r   r   .0abr   r   r   
<listcomp>\       z_add_lists.<locals>.<listcomp>c                 S   s   g | ]\}}|| qS r   r   r    r   r   r   r$   ^   r%   )lenzip)Zlist1Z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rlt|D ]L\}}t|trB| j jt|||< qt|| j jjs| j j|||< q|| _	t
| j	d | _d S )N   )parentr   list	enumerateintr	   
from_sympyr   dtype
listofpolyr&   order)r   Zlist_of_polyr,   ijr   r   r   r      s    

zRecurrenceOperator.__init__c                    s   | j }| jj t|tsFt|| jjjs>| jjt|g}qL|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!   r4   r#   r   r   r$      r%   zGRecurrenceOperator.__mul__.<locals>._mul_dmp_diffop.<locals>.<listcomp>)r   r-   )r#   listofotherr   r7   r   _mul_dmp_diffop   s    
z3RecurrenceOperator.__mul__.<locals>._mul_dmp_diffopr   c                    s    j g}t| trR| D ]8} | jd  jd tj }| 	| qn.|  jd  jd tj }| 	| |S )Nr   )
r   r   r-   to_sympysubsgensr   ZOneappendr0   )r#   r(   r4   r5   r	   r   r   
_mul_Sni_b   s    
$z.RecurrenceOperator.__mul__.<locals>._mul_Sni_br+   )r2   r,   r	   r   r   r1   r0   r   ranger&   r)   )r   r   Z
listofselfr8   r9   r(   r?   r4   r   r>   r   __mul__   s    
zRecurrenceOperator.__mul__c                    s^   t  tsZt  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!   r5   r   r   r   r$      r%   z/RecurrenceOperator.__rmul__.<locals>.<listcomp>)	r   r   r/   r   r,   r	   r1   r0   r2   )r   r   r(   r   rB   r   __rmul__   s    

zRecurrenceOperator.__rmul__c                 C   s   t |tr$t| j|j}t|| jS t |tr6t|}| j}t || jjjs^| jj	|g}n|g}|d |d  g|dd   }t|| jS d S )Nr   r+   )
r   r   r)   r2   r,   r/   r   r	   r1   r0   )r   r   r(   Z	list_selfZ
list_otherr   r   r   __add__   s    

zRecurrenceOperator.__add__c                 C   s   | d|  S Nr   r   r   r   r   __sub__   s    zRecurrenceOperator.__sub__c                 C   s   d|  | S rE   r   r   r   r   r   __rsub__   s    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rd| jjjg| | jjjg }t || jS | }|d rx||9 }|dL }|sq||9 }qh|S )Nr+   r      )r   r,   r	   r   r2   r   r   )r   nresultr(   xr   r   r   __pow__   s     
zRecurrenceOperator.__pow__c                 C   s   | j }d}t|D ]\}}|| jjjkr*q| jj|}|dkrV|dt| d 7 }q|rb|d7 }|dkr|dt| d 7 }q|dt| d d t| 7 }q|S )	N r   ()z + r+   z)SnzSn**)r2   r.   r,   r	   r   r:   r   )r   r2   Z	print_strr4   r5   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oVt fdd jdd  D S )NTFr   c                 3   s   | ]}| j jju V  qd S )N)r,   r	   r   r6   r   r   r   	<genexpr>*  r%   z,RecurrenceOperator.__eq__.<locals>.<genexpr>r+   )r   r   r2   r,   allr   r   rQ   r   r   #  s    
zRecurrenceOperator.__eq__N)r   r   r   r   Z_op_priorityr   rA   rC   rD   __radd__rG   rH   rM   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r6d| _nd| _|jjjd | _	d S )Nr   FT)

recurrencer   r-   u0r&   _have_init_condr,   r	   r<   rJ   )r   rV   rW   r   r   r   r   4  s    

zHolonomicSequence.__init__c                 C   sf   d| j  t| jf }| js"|S d}d}| jD ]$}|dt|t|f 7 }|d7 }q0|| }|S d S )NzHolonomicSequence(%s, %s)rN   r   z, u(%s) = %sr+   )rV   r   r   rJ   rX   rW   )r   Zstr_solZcond_strZseq_strr4   r(   r   r   r   r   A  s    

zHolonomicSequence.__repr__c                 C   s8   | j |j ks| j|jkrdS | jr4|jr4| j|jkS dS )NFT)rV   rJ   rX   rW   r   r   r   r   r   Q  s
    zHolonomicSequence.__eq__N)r   r   r   r   r   r   r   r   r   r   r   r   rU   -  s
   rU   N)r   Zsympy.core.singletonr   Zsympy.core.symbolr   r   Zsympy.printingr   Zsympy.core.sympifyr   r   r   r)   r   rU   r   r   r   r   <module>   s   ; L