a
    <b*                     @   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   j/Users/vegardjervell/Documents/master/model/venv/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   s8   t |tr*g }|D ]}|||   q|S | | gS d S N)r   r.   append)r$   listofotherr)   r5   r   r   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.   Zto_sympysubsgensr   ZOner8   r1   )r$   r)   r5   r6   r	   r   r   
_mul_Sni_b   s    
$z.RecurrenceOperator.__mul__.<locals>._mul_Sni_br,   )r3   r-   r	   r   r   r2   r1   r   ranger'   r*   )r   r   Z
listofselfr9   r:   r)   r>   r5   r   r=   r   __mul__   s    
	zRecurrenceOperator.__mul__c                 C   sh   t |tsdt |trt|}t || jjjs:| jj|}g }| jD ]}|	||  qDt|| jS d S r7   )
r   r   r0   r   r-   r	   r2   r1   r3   r8   )r   r   r)   r6   r   r   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}g }|
|d |d   ||dd  7 }t|| jS d S )Nr   r,   )r   r   r*   r3   r-   r0   r   r	   r2   r1   r8   )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 rC   r   r   r   r   r   __rsub__   s    zRecurrenceOperator.__rsub__c                 C   s   |dkr| S |dkr(t | jjjg| jS | j| jjjkrxg }td|D ]}|| jjj qF|| jjj t || jS |d dkr| |d  }||  S |d dkr| |d  }|| S d S )Nr,   r      )	r   r-   r	   r   r3   r   r?   r8   r   )r   nr)   r5   Z	powreducer   r   r   __pow__   s     zRecurrenceOperator.__pow__c                 C   s   | j }d}t|D ]\}}|| jjjkr*q|dkrH|dt| d 7 }q|rT|d7 }|dkrr|dt| d 7 }q|dt| d d t| 7 }q|S )	N r   ()z + r,   z)SnzSn**)r3   r/   r-   r	   r   r   )r   r3   Z	print_strr5   r6   r   r   r   r     s    "zRecurrenceOperator.__str__c                 C   sn   t |tr,| j|jkr&| j|jkr&dS dS n>| jd |krf| jdd  D ]}|| jjjurH dS qHdS dS d S )NTFr   r,   )r   r   r3   r-   r	   r   )r   r   r5   r   r   r   r   ,  s    
zRecurrenceOperator.__eq__N)r   r   r   r   Z_op_priorityr   r@   rA   rB   __radd__rE   rF   rI   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r6d| _nd| _|jjjd | _	d S )Nr   FT)

recurrencer   r.   u0r'   _have_init_condr-   r	   r<   rH   )r   rO   rP   r   r   r   r   C  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)rJ   r   z, u(%s) = %sr,   )rO   r    r   rH   rQ   rP   )r   Zstr_solZcond_strZseq_strr5   r)   r   r   r   r    P  s    

zHolonomicSequence.__repr__c                 C   sN   | j |j krF| j|jkr@| jr:|jr:| j|jkr4dS dS qDdS qJdS ndS d S r   )rO   rH   rQ   rP   r   r   r   r   r   `  s    zHolonomicSequence.__eq__N)r   r   r   r   r   r    r   r   r   r   r   r   rN   <  s
   rN   N)r   Zsympy.core.singletonr   Zsympy.core.symbolr   r   Zsympy.printingr   Zsympy.core.sympifyr   r   r   r*   r   rN   r   r   r   r   <module>   s   ; [