
    g׊                        d dl mZ d dlmZ d dlmZ d dlm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	lmZ d d
lmZmZ d dlmZ d dlmZmZmZ d dlmZ d dlmZ d dl m!Z!m"Z" d dl#m$Z$m%Z% d dl&m'Z' d dl(m)Z)m*Z*m+Z+  G d de          Z, G d de,e          Z- G d de,          Z. G d de.          Z/ G d de.          Z0 G d de,          Z1d(d!Z2 G d" d#e,          Z3 G d$ d%e3          Z4 G d& d'e3          Z5d S ))    )Basic)cacheit)Tuple)call_highest_priority)global_parameters)AppliedUndefexpandMul)Integer)Eq)S	Singleton)ordered)DummySymbolWildsympify)Matrix)lcmfactor)IntervalIntersection)Idx)flattenis_sequenceiterablec                      e Zd ZdZdZdZed             Zd Ze	d             Z
e	d             Ze	d             Ze	d	             Ze	d
             Ze	d             Ze	d             Zed             Zd Zd Zd Zd Zd Zd Z ed          d             Zd Z ed          d             Zd Zd Z ed          d             Zd Z d Z!d!d Z"dS )"SeqBasezBase class for sequencesT   c                 P    	 | j         }n# t          $ r t          j        }Y nw xY w|S )z[Return start (if possible) else S.Infinity.

        adapted from Set._infimum_key
        )startNotImplementedErrorr   Infinity)exprr#   s     R/var/www/html/ai-engine/env/lib/python3.11/site-packages/sympy/series/sequences.py
_start_keyzSeqBase._start_key    s;    	JEE" 	 	 	JEEE	s   
 ##c                 R    t          | j        |j                  }|j        |j        fS )zTReturns start and stop.

        Takes intersection over the two intervals.
        )r   intervalinfsup)selfotherr*   s      r'   _intersect_intervalzSeqBase._intersect_interval,   s&    
  u~>>|X\))    c                 &    t          d| z            )z&Returns the generator for the sequencez(%s).genr$   r-   s    r'   genzSeqBase.gen4   s     "*t"3444r0   c                 &    t          d| z            )z-The interval on which the sequence is definedz(%s).intervalr2   r3   s    r'   r*   zSeqBase.interval9   s     "/D"8999r0   c                 &    t          d| z            ):The starting point of the sequence. This point is includedz
(%s).startr2   r3   s    r'   r#   zSeqBase.start>   s     ","5666r0   c                 &    t          d| z            )z8The ending point of the sequence. This point is includedz	(%s).stopr2   r3   s    r'   stopzSeqBase.stopC   s     "+"4555r0   c                 &    t          d| z            )zLength of the sequencez(%s).lengthr2   r3   s    r'   lengthzSeqBase.lengthH   s     "-$"6777r0   c                     dS )z-Returns a tuple of variables that are bounded r=   r3   s    r'   	variableszSeqBase.variablesM   s	     rr0   c                 *      fd j         D             S )aG  
        This method returns the symbols in the object, excluding those
        that take on a specific value (i.e. the dummy symbols).

        Examples
        ========

        >>> from sympy import SeqFormula
        >>> from sympy.abc import n, m
        >>> SeqFormula(m*n**2, (n, 0, 5)).free_symbols
        {m}
        c                 X    h | ]&}|j                             j                  D ]}|'S r=   )free_symbols
differencer>   ).0ijr-   s      r'   	<setcomp>z'SeqBase.free_symbols.<locals>.<setcomp>`   sI     0 0 0qq~Jt~..0 0! 0 0 0 0r0   argsr3   s   `r'   rA   zSeqBase.free_symbolsR   s/    0 0 0 0DI 0 0 0 	1r0   c                     || j         k     s|| j        k    rt          d|d| j                  |                     |          S )z#Returns the coefficient at point ptzIndex z out of bounds )r#   r9   
IndexErrorr*   _eval_coeffr-   pts     r'   coeffzSeqBase.coeffc   sJ     
??b49nn*BBBNOOO###r0   c                 0    t          d| j        z            )NzhThe _eval_coeff method should be added to%s to return coefficient so it is availablewhen coeff calls it.)r$   funcrL   s     r'   rK   zSeqBase._eval_coeffj   s%    ! #9 %)I#. / / 	/r0   c                     | j         t          j        u r| j        }n| j         }| j         t          j        u rd}nd}|||z  z   S )a  Returns the i'th point of a sequence.

        Explanation
        ===========

        If start point is negative infinity, point is returned from the end.
        Assumes the first point to be indexed zero.

        Examples
        =========

        >>> from sympy import oo
        >>> from sympy.series.sequences import SeqPer

        bounded

        >>> SeqPer((1, 2, 3), (-10, 10))._ith_point(0)
        -10
        >>> SeqPer((1, 2, 3), (-10, 10))._ith_point(5)
        -5

        End is at infinity

        >>> SeqPer((1, 2, 3), (0, oo))._ith_point(5)
        5

        Starts at negative infinity

        >>> SeqPer((1, 2, 3), (-oo, 0))._ith_point(5)
        -5
           )r#   r   NegativeInfinityr9   )r-   rD   initialsteps       r'   
_ith_pointzSeqBase._ith_pointp   sP    @ :+++iGGjG:+++DDD4r0   c                     dS )aI  
        Should only be used internally.

        Explanation
        ===========

        self._add(other) returns a new, term-wise added sequence if self
        knows how to add with other, otherwise it returns ``None``.

        ``other`` should only be a sequence object.

        Used within :class:`SeqAdd` class.
        Nr=   r-   r.   s     r'   _addzSeqBase._add   	     tr0   c                     dS )aS  
        Should only be used internally.

        Explanation
        ===========

        self._mul(other) returns a new, term-wise multiplied sequence if self
        knows how to multiply with other, otherwise it returns ``None``.

        ``other`` should only be a sequence object.

        Used within :class:`SeqMul` class.
        Nr=   rY   s     r'   _mulzSeqBase._mul   r[   r0   c                 "    t          | |          S )a  
        Should be used when ``other`` is not a sequence. Should be
        defined to define custom behaviour.

        Examples
        ========

        >>> from sympy import SeqFormula
        >>> from sympy.abc import n
        >>> SeqFormula(n**2).coeff_mul(2)
        SeqFormula(2*n**2, (n, 0, oo))

        Notes
        =====

        '*' defines multiplication of sequences with sequences only.
        r
   rY   s     r'   	coeff_mulzSeqBase.coeff_mul   s    $ 4r0   c                     t          |t                    st          dt          |          z            t	          | |          S )a4  Returns the term-wise addition of 'self' and 'other'.

        ``other`` should be a sequence.

        Examples
        ========

        >>> from sympy import SeqFormula
        >>> from sympy.abc import n
        >>> SeqFormula(n**2) + SeqFormula(n**3)
        SeqFormula(n**3 + n**2, (n, 0, oo))
        zcannot add sequence and %s
isinstancer    	TypeErrortypeSeqAddrY   s     r'   __add__zSeqBase.__add__   sA     %)) 	H84;;FGGGdE"""r0   rf   c                     | |z   S Nr=   rY   s     r'   __radd__zSeqBase.__radd__       e|r0   c                     t          |t                    st          dt          |          z            t	          | |           S )a7  Returns the term-wise subtraction of ``self`` and ``other``.

        ``other`` should be a sequence.

        Examples
        ========

        >>> from sympy import SeqFormula
        >>> from sympy.abc import n
        >>> SeqFormula(n**2) - (SeqFormula(n))
        SeqFormula(n**2 - n, (n, 0, oo))
        zcannot subtract sequence and %sra   rY   s     r'   __sub__zSeqBase.__sub__   sC     %)) 	M=UKLLLdUF###r0   rl   c                     |  |z   S rh   r=   rY   s     r'   __rsub__zSeqBase.__rsub__   s    r0   c                 ,    |                      d          S )zNegates the sequence.

        Examples
        ========

        >>> from sympy import SeqFormula
        >>> from sympy.abc import n
        >>> -SeqFormula(n**2)
        SeqFormula(-n**2, (n, 0, oo))
        rR   )r_   r3   s    r'   __neg__zSeqBase.__neg__   s     ~~b!!!r0   c                     t          |t                    st          dt          |          z            t	          | |          S )a{  Returns the term-wise multiplication of 'self' and 'other'.

        ``other`` should be a sequence. For ``other`` not being a
        sequence see :func:`coeff_mul` method.

        Examples
        ========

        >>> from sympy import SeqFormula
        >>> from sympy.abc import n
        >>> SeqFormula(n**2) * (SeqFormula(n))
        SeqFormula(n**3, (n, 0, oo))
        zcannot multiply sequence and %s)rb   r    rc   rd   SeqMulrY   s     r'   __mul__zSeqBase.__mul__  sA     %)) 	M=UKLLLdE"""r0   rs   c                     | |z  S rh   r=   rY   s     r'   __rmul__zSeqBase.__rmul__  rj   r0   c              #      K   t          | j                  D ].}|                     |          }|                     |          V  /d S rh   )ranger;   rW   rN   )r-   rD   rM   s      r'   __iter__zSeqBase.__iter__  sS      t{## 	! 	!A##B**R..    	! 	!r0   c                 .    t          |t                    r*                     |          }                     |          S t          |t                    r?|j        |j        }}|d}| j        } fdt          |||j	        pd          D             S d S )Nr   c                 `    g | ]*}                                         |                    +S r=   )rN   rW   )rC   rD   r-   s     r'   
<listcomp>z'SeqBase.__getitem__.<locals>.<listcomp>,  s=     9 9 9qDJJtq1122 9 9 9r0   rS   )
rb   intrW   rN   slicer#   r9   r;   rw   rV   )r-   indexr#   r9   s   `   r'   __getitem__zSeqBase.__getitem__"  s    eS!! 
	9OOE**E::e$$$u%% 	9+uz4E}|{9 9 9 9%uzQ779 9 9 9	9 	9r0   Nc           
         ddl m fd| d|         D             }t          |          }||dz  }nt          ||dz            }g }t	          d|dz             D ]5}d|z  }	g }
t	          |          D ]"}|
                    ||||z                       #t          |
          }|                                dk    r |                    t          |||	                                       }||	k    rt          |ddd                   } ng }
t	          |||z
            D ]"}|
                    ||||z                       #t          |
          }||z  t          ||	d                   k    rt          |ddd                   } n7||S t          |          }|dk    rg dfS ||dz
           ||dz
  z  z  d||dz
           ||z  z  z
  }}t	          |dz
            D ]_}|||         ||z  z  z  }t	          ||z
  dz
            D ]"}|||         ||         z  |||z   dz   z  z  z  }#|||         ||dz   z  z  z  }`| t          |          t          |          z            fS )a  
        Finds the shortest linear recurrence that satisfies the first n
        terms of sequence of order `\leq` ``n/2`` if possible.
        If ``d`` is specified, find shortest linear recurrence of order
        `\leq` min(d, n/2) if possible.
        Returns list of coefficients ``[b(1), b(2), ...]`` corresponding to the
        recurrence relation ``x(n) = b(1)*x(n-1) + b(2)*x(n-2) + ...``
        Returns ``[]`` if no recurrence is found.
        If gfvar is specified, also returns ordinary generating function as a
        function of gfvar.

        Examples
        ========

        >>> from sympy import sequence, sqrt, oo, lucas
        >>> from sympy.abc import n, x, y
        >>> sequence(n**2).find_linear_recurrence(10, 2)
        []
        >>> sequence(n**2).find_linear_recurrence(10)
        [3, -3, 1]
        >>> sequence(2**n).find_linear_recurrence(10)
        [2]
        >>> sequence(23*n**4+91*n**2).find_linear_recurrence(10)
        [5, -10, 10, -5, 1]
        >>> sequence(sqrt(5)*(((1 + sqrt(5))/2)**n - (-(1 + sqrt(5))/2)**(-n))/5).find_linear_recurrence(10)
        [1, 1]
        >>> sequence(x+y*(-2)**(-n), (n, 0, oo)).find_linear_recurrence(30)
        [1/2, 1/2]
        >>> sequence(3*5**n + 12).find_linear_recurrence(20,gfvar=x)
        ([6, -5], 3*(5 - 21*x)/((x - 1)*(5*x - 1)))
        >>> sequence(lucas(n)).find_linear_recurrence(15,gfvar=x)
        ([1, 1], (x - 2)/(x**2 + x - 1))
        r   )simplifyc                 @    g | ]} t          |                    S r=   )r	   )rC   tr   s     r'   r{   z2SeqBase.find_linear_recurrence.<locals>.<listcomp>R  s)    333QXXfQii  333r0   N   rS   rR   )sympy.simplifyr   lenminrw   appendr   detLUsolver   r   )r-   ndgfvarxlxrcoeffsll2mlistkmyrD   rE   r   s                   @r'   find_linear_recurrencezSeqBase.find_linear_recurrence/  s   D 	,+++++3333$rr(333VV9AAAAb!eAq!A# 	 	A1BE1XX ' 'Qq1uX&&&&uAuuww!||HQYYva"g778888$QtttW--FEqA + +ALL1QqS5****5MMQ3&233..(($QtttW--FE=MFAAvv4x1veacl*AqsE1H0D,D1qs 0 0A1eQh&A"1Q3q5\\ ; ;VAYqt^EAaCEN::51Q3<//AAxxq		&))(;<<<<r0   )NN)#__name__
__module____qualname____doc__is_commutative_op_prioritystaticmethodr(   r/   propertyr4   r*   r#   r9   r;   r>   rA   r   rN   rK   rW   rZ   r]   r_   rf   r   ri   rl   rn   rp   rs   ru   rx   r   r   r=   r0   r'   r    r       sY       ""NL	 	 \	* * * 5 5 X5 : : X: 7 7 X7 6 6 X6 8 8 X8   X 1 1 X1  $ $ W$/ / /*  *  * X           (# # #" 9%%  &%$ $ $" 9%%  &%" " "# # #$ 9%%  &%! ! !
9 9 9I= I= I= I= I= I=r0   r    c                   J    e Zd ZdZed             Zed             Zd Zd ZdS )EmptySequencea  Represents an empty sequence.

    The empty sequence is also available as a singleton as
    ``S.EmptySequence``.

    Examples
    ========

    >>> from sympy import EmptySequence, SeqPer
    >>> from sympy.abc import x
    >>> EmptySequence
    EmptySequence
    >>> SeqPer((1, 2), (x, 0, 10)) + EmptySequence
    SeqPer((1, 2), (x, 0, 10))
    >>> SeqPer((1, 2)) * EmptySequence
    EmptySequence
    >>> EmptySequence.coeff_mul(-1)
    EmptySequence
    c                     t           j        S rh   )r   EmptySetr3   s    r'   r*   zEmptySequence.interval  s
    zr0   c                     t           j        S rh   )r   Zeror3   s    r'   r;   zEmptySequence.length  s	    vr0   c                     | S )"See docstring of SeqBase.coeff_mulr=   )r-   rN   s     r'   r_   zEmptySequence.coeff_mul  s    r0   c                      t          g           S rh   )iterr3   s    r'   rx   zEmptySequence.__iter__  s    Bxxr0   N)	r   r   r   r   r   r*   r;   r_   rx   r=   r0   r'   r   r   z  sr         (   X   X      r0   r   )	metaclassc                       e Zd ZdZed             Zed             Zed             Zed             Zed             Z	ed             Z
dS )	SeqExpra  Sequence expression class.

    Various sequences should inherit from this class.

    Examples
    ========

    >>> from sympy.series.sequences import SeqExpr
    >>> from sympy.abc import x
    >>> from sympy import Tuple
    >>> s = SeqExpr(Tuple(1, 2, 3), Tuple(x, 0, 10))
    >>> s.gen
    (1, 2, 3)
    >>> s.interval
    Interval(0, 10)
    >>> s.length
    11

    See Also
    ========

    sympy.series.sequences.SeqPer
    sympy.series.sequences.SeqFormula
    c                     | j         d         S Nr   rG   r3   s    r'   r4   zSeqExpr.gen  s    y|r0   c                 f    t          | j        d         d         | j        d         d                   S )NrS   r   )r   rH   r3   s    r'   r*   zSeqExpr.interval  s&    	!Q1a999r0   c                     | j         j        S rh   r*   r+   r3   s    r'   r#   zSeqExpr.start      }  r0   c                     | j         j        S rh   r*   r,   r3   s    r'   r9   zSeqExpr.stop  r   r0   c                 &    | j         | j        z
  dz   S NrS   r9   r#   r3   s    r'   r;   zSeqExpr.length      y4:%))r0   c                 *    | j         d         d         fS )NrS   r   rG   r3   s    r'   r>   zSeqExpr.variables  s    	!Q!!r0   N)r   r   r   r   r   r4   r*   r#   r9   r;   r>   r=   r0   r'   r   r     s         2   X : : X: ! ! X! ! ! X! * * X* " " X" " "r0   r   c                   ^    e Zd ZdZd
dZed             Zed             Zd Zd Z	d Z
d	 ZdS )SeqPera  
    Represents a periodic sequence.

    The elements are repeated after a given period.

    Examples
    ========

    >>> from sympy import SeqPer, oo
    >>> from sympy.abc import k

    >>> s = SeqPer((1, 2, 3), (0, 5))
    >>> s.periodical
    (1, 2, 3)
    >>> s.period
    3

    For value at a particular point

    >>> s.coeff(3)
    1

    supports slicing

    >>> s[:]
    [1, 2, 3, 1, 2, 3]

    iterable

    >>> list(s)
    [1, 2, 3, 1, 2, 3]

    sequence starts from negative infinity

    >>> SeqPer((1, 2, 3), (-oo, 0))[0:6]
    [1, 2, 3, 1, 2, 3]

    Periodic formulas

    >>> SeqPer((k, k**2, k**3), (k, 0, oo))[0:6]
    [0, 1, 8, 3, 16, 125]

    See Also
    ========

    sympy.series.sequences.SeqFormula
    Nc                 :   t          |          }d }d\  }}}| ||          dt          j        }}}t          |t                    r=t          |          dk    r|\  }}}n#t          |          dk    r ||          }|\  }}t          |t          t          f          r||t          dt          |          z            |t          j        u r|t          j        u rt          d          t          |||f          }t          |t                    r*t          t          t          |                              }nt          d|z            t          |d	         |d                   t          j        u rt          j        S t#          j        | ||          S )
Nc                     | j         }t          | j                   dk    r|                                S t          d          S )NrS   r   )rA   r   popr   )
periodicalfrees     r'   _find_xzSeqPer.__new__.<locals>._find_x  s:    *D:*++q00xxzz!Szz!r0   NNNr      r   Invalid limits given: %sz/Both the start and end valuecannot be unboundedz6invalid period %s should be something like e.g (1, 2) rS   )r   r   r%   r   r   r   rb   r   r   
ValueErrorstrrT   tupler   r   r   r   r   __new__)clsr   limitsr   r   r#   r9   s          r'   r   zSeqPer.__new__  s   Z((
	" 	" 	" *5$>$WZ00!QZduAvu%% 	%6{{a!'5$$V!!GJ''$t!fc]++ 	Gu}7#f++EFFFA&&&41:+=+=  "7 8 8 8 !UD)**z5)) 	> wz':':!;!;<<JJ 02<= > > > F1Ivay))QZ77?"}S*f555r0   c                 *    t          | j                  S rh   )r   r4   r3   s    r'   periodzSeqPer.period+  s    48}}r0   c                     | j         S rh   r4   r3   s    r'   r   zSeqPer.periodical/  	    xr0   c                     | j         t          j        u r| j        |z
  | j        z  }n|| j         z
  | j        z  }| j        |                             | j        d         |          S r   )r#   r   rT   r9   r   r   subsr>   )r-   rM   idxs      r'   rK   zSeqPer._eval_coeff3  s\    :+++9r>T[0CC
?dk1Cs#(():B???r0   c                 x   t          |t                    r| j        | j        }}|j        |j        }}t	          ||          }g }t          |          D ]0}|||z           }	|||z           }
|                    |	|
z              1|                     |          \  }}t          || j        d         ||f          S dS zSee docstring of SeqBase._addr   N	rb   r   r   r   r   rw   r   r/   r>   r-   r.   per1lper1per2lper2
per_lengthnew_perr   ele1ele2r#   r9   s                r'   rZ   zSeqPer._add:      eV$$ 	E/4;%D*EL%DUE**JG:&& , ,AIAItd{++++22599KE4'DN1$5ud#CDDD	E 	Er0   c                 x   t          |t                    r| j        | j        }}|j        |j        }}t	          ||          }g }t          |          D ]0}|||z           }	|||z           }
|                    |	|
z             1|                     |          \  }}t          || j        d         ||f          S dS zSee docstring of SeqBase._mulr   Nr   r   s                r'   r]   zSeqPer._mulK  r   r0   c                 ~    t                    fd| j        D             }t          || j        d                   S )r   c                     g | ]}|z  S r=   r=   )rC   r   rN   s     r'   r{   z$SeqPer.coeff_mul.<locals>.<listcomp>_  s    222Qq5y222r0   rS   )r   r   r   rH   )r-   rN   pers    ` r'   r_   zSeqPer.coeff_mul\  s?    2222$/222c49Q<(((r0   rh   )r   r   r   r   r   r   r   r   rK   rZ   r]   r_   r=   r0   r'   r   r     s        . .`&6 &6 &6 &6P   X   X@ @ @E E E"E E E") ) ) ) )r0   r   c                   N    e Zd ZdZd
dZed             Zd Zd Zd Z	d Z
d	 ZdS )
SeqFormulaaf  
    Represents sequence based on a formula.

    Elements are generated using a formula.

    Examples
    ========

    >>> from sympy import SeqFormula, oo, Symbol
    >>> n = Symbol('n')
    >>> s = SeqFormula(n**2, (n, 0, 5))
    >>> s.formula
    n**2

    For value at a particular point

    >>> s.coeff(3)
    9

    supports slicing

    >>> s[:]
    [0, 1, 4, 9, 16, 25]

    iterable

    >>> list(s)
    [0, 1, 4, 9, 16, 25]

    sequence starts from negative infinity

    >>> SeqFormula(n**2, (-oo, 0))[0:6]
    [0, 1, 4, 9, 16, 25]

    See Also
    ========

    sympy.series.sequences.SeqPer
    Nc                    t          |          }d }d\  }}}| ||          dt          j        }}}t          |t                    r=t          |          dk    r|\  }}}n#t          |          dk    r ||          }|\  }}t          |t          t          f          r||t          dt          |          z            |t          j        u r|t          j        u rt          d          t          |||f          }t          |d         |d                   t          j        u rt          j        S t          j        | ||          S )	Nc                     | j         }t          |          dk    r|                                S |st          d          S t	          d| z            )NrS   r   z specify dummy variables for %s. If the formula contains more than one free symbol, a dummy variable should be supplied explicitly e.g., SeqFormula(m*n**2, (n, 0, 5)))rA   r   r   r   r   )formular   s     r'   r   z#SeqFormula.__new__.<locals>._find_x  s]    'D4yyA~~xxzz! Szz! O   r0   r   r   r   r   r   z0Both the start and end value cannot be unboundedrS   )r   r   r%   r   r   r   rb   r   r   r   r   rT   r   r   r   r   r   )r   r   r   r   r   r#   r9   s          r'   r   zSeqFormula.__new__  s[   '""	 	 	 *5$>$WW--q!*duAvu%% 	%6{{a!'5$$V!!GG$$$t!fc]++ 	Gu}7#f++EFFFA&&&41:+=+=  "7 8 8 8!UD)**F1Ivay))QZ77?"}S'6222r0   c                     | j         S rh   r   r3   s    r'   r   zSeqFormula.formula  r   r0   c                 R    | j         d         }| j                            ||          S r   )r>   r   r   )r-   rM   r   s      r'   rK   zSeqFormula._eval_coeff  s&    N1|  B'''r0   c                    t          |t                    rl| j        | j        d         }}|j        |j        d         }}||                    ||          z   }|                     |          \  }}t          ||||f          S dS r   rb   r   r   r>   r   r/   	r-   r.   form1v1form2v2r   r#   r9   s	            r'   rZ   zSeqFormula._add      eZ(( 	:dnQ&72Euq'92EejjR000G22599KE4gE4'8999	: 	:r0   c                    t          |t                    rl| j        | j        d         }}|j        |j        d         }}||                    ||          z  }|                     |          \  }}t          ||||f          S dS r   r   r   s	            r'   r]   zSeqFormula._mul  r   r0   c                 j    t          |          }| j        |z  }t          || j        d                   S )r   rS   )r   r   r   rH   )r-   rN   r   s      r'   r_   zSeqFormula.coeff_mul  s/    ,&'49Q<000r0   c                 ^    t          t          | j        g|R i || j        d                   S r   )r   r	   r   rH   )r-   rH   kwargss      r'   r	   zSeqFormula.expand  s2    &??????1NNNr0   rh   )r   r   r   r   r   r   r   rK   rZ   r]   r_   r	   r=   r0   r'   r   r   c  s        & &P%3 %3 %3 %3N   X( ( (: : :: : :1 1 1O O O O Or0   r   c                       e Zd ZdZddZed             Zed             Zed             Zed             Z	ed	             Z
ed
             Zed             Zed             Zed             Zd Zd ZdS )RecursiveSeqa  
    A finite degree recursive sequence.

    Explanation
    ===========

    That is, a sequence a(n) that depends on a fixed, finite number of its
    previous values. The general form is

        a(n) = f(a(n - 1), a(n - 2), ..., a(n - d))

    for some fixed, positive integer d, where f is some function defined by a
    SymPy expression.

    Parameters
    ==========

    recurrence : SymPy expression defining recurrence
        This is *not* an equality, only the expression that the nth term is
        equal to. For example, if :code:`a(n) = f(a(n - 1), ..., a(n - d))`,
        then the expression should be :code:`f(a(n - 1), ..., a(n - d))`.

    yn : applied undefined function
        Represents the nth term of the sequence as e.g. :code:`y(n)` where
        :code:`y` is an undefined function and `n` is the sequence index.

    n : symbolic argument
        The name of the variable that the recurrence is in, e.g., :code:`n` if
        the recurrence function is :code:`y(n)`.

    initial : iterable with length equal to the degree of the recurrence
        The initial values of the recurrence.

    start : start value of sequence (inclusive)

    Examples
    ========

    >>> from sympy import Function, symbols
    >>> from sympy.series.sequences import RecursiveSeq
    >>> y = Function("y")
    >>> n = symbols("n")
    >>> fib = RecursiveSeq(y(n - 1) + y(n - 2), y(n), n, [0, 1])

    >>> fib.coeff(3) # Value at a particular point
    2

    >>> fib[:6] # supports slicing
    [0, 1, 1, 2, 3, 5]

    >>> fib.recurrence # inspect recurrence
    Eq(y(n), y(n - 2) + y(n - 1))

    >>> fib.degree # automatically determine degree
    2

    >>> for x in zip(range(10), fib): # supports iteration
    ...     print(x)
    (0, 0)
    (1, 1)
    (2, 1)
    (3, 2)
    (4, 3)
    (5, 5)
    (6, 8)
    (7, 13)
    (8, 21)
    (9, 34)

    See Also
    ========

    sympy.series.sequences.SeqFormula

    Nr   c                 ,   t          |t                    s"t          d                    |                    t          |t                    r|j        s"t          d                    |                    |j        |fk    rt          d          |j        t          d|f          }d}|	                              }|D ]}	t          |	j                  dk    rt          d          |	j        d                             ||z             |         }
|
                                r|
j        r|
dk     s"t          d	                    |	                    |
 |k    r|
 }|sd
 t          |          D             }t          |          |k    rt          d          t!          |          }t#                    t%          d |D              }t	          j        | ||||          }fdt)          |          D             |_        ||_        |S )NzErecurrence sequence must be an applied undefined function, found `{}`z0recurrence variable must be a symbol, found `{}`z)recurrence sequence does not match symbolr   )excluder   rS   z)Recurrence should be in a single variablezDRecurrence should have constant, negative, integer shifts (found {})c                 R    g | ]$}t          d                     |                    %S )zc_{})r   format)rC   r   s     r'   r{   z(RecursiveSeq.__new__.<locals>.<listcomp>G  s,    FFF1uV]]1--..FFFr0   z)Number of initial terms must equal degreec              3   4   K   | ]}t          |          V  d S rh   r   )rC   r   s     r'   	<genexpr>z'RecursiveSeq.__new__.<locals>.<genexpr>O  s(      66'!**666666r0   c                 4    i | ]\  }} |z             |S r=   r=   )rC   r   initr#   r   s      r'   
<dictcomp>z(RecursiveSeq.__new__.<locals>.<dictcomp>S  s+    JJJGAtQQuqy\\4JJJr0   )rb   r   rc   r   r   	is_symbolrH   rP   r   findr   matchis_constant
is_integerrw   r   r   r   r   r   	enumeratecachedegree)r   
recurrenceynr   rU   r#   r   r  prev_ysprev_yshiftseqr   s        `      @r'   r   zRecursiveSeq.__new__#  s6   "l++ 	7 ++16"::7 7 7 !U## 	61; 	6 ++16!996 6 6 7qd??GHHHGqd### //!$$ 	  	 F6;1$$ KLLLKN((Q//2E%%'' >E,< > !..4fVnn> > > v 	GFFfFFFGw<<6!!HIII66g6667mCRGUCCJJJJJy7I7IJJJ	

r0   c                     | j         d         S zEquation defining recurrence.r   rG   r3   s    r'   _recurrencezRecursiveSeq._recurrenceX       y|r0   c                 B    t          | j        | j        d                   S r  )r   r  rH   r3   s    r'   r  zRecursiveSeq.recurrence]  s     $'49Q<(((r0   c                     | j         d         S )z*Applied function representing the nth termrS   rG   r3   s    r'   r  zRecursiveSeq.ynb  r  r0   c                     | j         j        S )z3Undefined function for the nth term of the sequence)r  rP   r3   s    r'   r   zRecursiveSeq.yg  s     w|r0   c                     | j         d         S )zSequence index symbolr   rG   r3   s    r'   r   zRecursiveSeq.nl  r  r0   c                     | j         d         S )z"The initial values of the sequencer   rG   r3   s    r'   rU   zRecursiveSeq.initialq  r  r0   c                     | j         d         S )r7      rG   r3   s    r'   r#   zRecursiveSeq.startv  r  r0   c                     t           j        S )z&The ending point of the sequence. (oo))r   r%   r3   s    r'   r9   zRecursiveSeq.stop{  s     zr0   c                 (    | j         t          j        fS )z&Interval on which sequence is defined.)r#   r   r%   r3   s    r'   r*   zRecursiveSeq.interval  s     
AJ''r0   c                    || j         z
  t          | j                  k     r | j        |                     |                   S t	          t          | j                  |dz             D ]d}| j         |z   }| j                            | j        |i          }|                    | j                  }|| j        |                     |          <   e| j        |                     | j         |z                      S r   )r#   r   r
  r   rw   r  xreplacer   )r-   r~   current	seq_indexcurrent_recurrencenew_terms         r'   rK   zRecursiveSeq._eval_coeff  s    4:DJ//:dffUmm,,S__eai88 	5 	5G 
W,I!%!1!:!:DFI;N!O!O)224:>>H,4DJtvvi(())z$&&g!56677r0   c              #   P   K   | j         }	 |                     |          V  |dz  })NTrS   )r#   rK   )r-   r~   s     r'   rx   zRecursiveSeq.__iter__  s:      
	""5)))))QJE	r0   r   )r   r   r   r   r   r   r  r  r  r   r   rU   r#   r9   r*   rK   rx   r=   r0   r'   r   r     sG       J JX3 3 3 3j   X ) ) X)   X   X   X   X   X   X ( ( X(8 8 8    r0   r   Nc                     t          |           } t          | t                    rt          | |          S t	          | |          S )a  
    Returns appropriate sequence object.

    Explanation
    ===========

    If ``seq`` is a SymPy sequence, returns :class:`SeqPer` object
    otherwise returns :class:`SeqFormula` object.

    Examples
    ========

    >>> from sympy import sequence
    >>> from sympy.abc import n
    >>> sequence(n**2, (n, 0, 5))
    SeqFormula(n**2, (n, 0, 5))
    >>> sequence((1, 2, 3), (n, 0, 5))
    SeqPer((1, 2, 3), (n, 0, 5))

    See Also
    ========

    sympy.series.sequences.SeqPer
    sympy.series.sequences.SeqFormula
    )r   r   r   r   r   )r  r   s     r'   sequencer'    sA    4 #,,C3 'c6"""#v&&&r0   c                       e Zd ZdZed             Zed             Zed             Zed             Zed             Z	ed             Z
dS )		SeqExprOpa  
    Base class for operations on sequences.

    Examples
    ========

    >>> from sympy.series.sequences import SeqExprOp, sequence
    >>> from sympy.abc import n
    >>> s1 = sequence(n**2, (n, 0, 10))
    >>> s2 = sequence((1, 2, 3), (n, 5, 10))
    >>> s = SeqExprOp(s1, s2)
    >>> s.gen
    (n**2, (1, 2, 3))
    >>> s.interval
    Interval(5, 10)
    >>> s.length
    6

    See Also
    ========

    sympy.series.sequences.SeqAdd
    sympy.series.sequences.SeqMul
    c                 >    t          d | j        D                       S )zjGenerator for the sequence.

        returns a tuple of generators of all the argument sequences.
        c              3   $   K   | ]}|j         V  d S rh   r   rC   as     r'   r   z SeqExprOp.gen.<locals>.<genexpr>  s$      ..qQU......r0   )r   rH   r3   s    r'   r4   zSeqExprOp.gen  s#     ..DI......r0   c                 2    t          d | j        D              S )zeSequence is defined on the intersection
        of all the intervals of respective sequences
        c              3   $   K   | ]}|j         V  d S rh   r*   r,  s     r'   r   z%SeqExprOp.interval.<locals>.<genexpr>  s$      <<Qaj<<<<<<r0   )r   rH   r3   s    r'   r*   zSeqExprOp.interval  s     
 <<$)<<<==r0   c                     | j         j        S rh   r   r3   s    r'   r#   zSeqExprOp.start  r   r0   c                     | j         j        S rh   r   r3   s    r'   r9   zSeqExprOp.stop  r   r0   c                 X    t          t          d | j        D                                 S )z%Cumulative of all the bound variablesc                     g | ]	}|j         
S r=   )r>   r,  s     r'   r{   z'SeqExprOp.variables.<locals>.<listcomp>  s    ===aak===r0   )r   r   rH   r3   s    r'   r>   zSeqExprOp.variables  s+     W==49===>>???r0   c                 &    | j         | j        z
  dz   S r   r   r3   s    r'   r;   zSeqExprOp.length  r   r0   N)r   r   r   r   r   r4   r*   r#   r9   r>   r;   r=   r0   r'   r)  r)    s         0 / / X/ > > X> ! ! X! ! ! X! @ @ X@ * * X* * *r0   r)  c                   4    e Zd ZdZd Zed             Zd ZdS )re   a  Represents term-wise addition of sequences.

    Rules:
        * The interval on which sequence is defined is the intersection
          of respective intervals of sequences.
        * Anything + :class:`EmptySequence` remains unchanged.
        * Other rules are defined in ``_add`` methods of sequence classes.

    Examples
    ========

    >>> from sympy import EmptySequence, oo, SeqAdd, SeqPer, SeqFormula
    >>> from sympy.abc import n
    >>> SeqAdd(SeqPer((1, 2), (n, 0, oo)), EmptySequence)
    SeqPer((1, 2), (n, 0, oo))
    >>> SeqAdd(SeqPer((1, 2), (n, 0, 5)), SeqPer((1, 2), (n, 6, 10)))
    EmptySequence
    >>> SeqAdd(SeqPer((1, 2), (n, 0, oo)), SeqFormula(n**2, (n, 0, oo)))
    SeqAdd(SeqFormula(n**2, (n, 0, oo)), SeqPer((1, 2), (n, 0, oo)))
    >>> SeqAdd(SeqFormula(n**3), SeqFormula(n**2))
    SeqFormula(n**3 + n**2, (n, 0, oo))

    See Also
    ========

    sympy.series.sequences.SeqMul
    c                    |                     dt          j                  }t          |          }fd |          }d |D             }|st          j        S t          d |D              t          j        u rt          j        S |rt          	                    |          S t          t          |t          j                            }t          j        | g|R  S )Nevaluatec                    t          | t                    r;t          | t                    r#t          t	          | j                  g           S | gS t          |           rt          t	          |           g           S t          d          Nz2Input must be Sequences or  iterables of Sequences)rb   r    re   summaprH   r   rc   arg_flattens    r'   r?  z SeqAdd.__new__.<locals>._flatten   s    #w'' !c6** !s8SX66;;;5L}} 33x--r222 6 7 7 7r0   c                 .    g | ]}|t           j        u|S r=   )r   r   r,  s     r'   r{   z"SeqAdd.__new__.<locals>.<listcomp>,  s$    <<<a1AO#;#;#;#;#;r0   c              3   $   K   | ]}|j         V  d S rh   r0  r,  s     r'   r   z!SeqAdd.__new__.<locals>.<genexpr>2  $      33!*333333r0   )getr   r8  listr   r   r   r   re   reducer   r    r(   r   r   r   rH   r   r8  r?  s       @r'   r   zSeqAdd.__new__  s    ::j*;*DEE Dzz		7 		7 		7 		7 		7 x~~<<4<<<  	#?"33d3334
BB?"  	'==&&&GD'"45566}S(4((((r0   c                 n   d}|rxt          |           D ]f\  }d}t          |           D ]I\  }||k    r                              }|&fd| D             }|                    |            nJ|r|}  ng|xt          |           dk    r|                                 S t          | d          S )a  Simplify :class:`SeqAdd` using known rules.

        Iterates through all pairs and ask the constituent
        sequences if they can simplify themselves with any other constituent.

        Notes
        =====

        adapted from ``Union.reduce``

        TFNc                      g | ]
}|fv|S r=   r=   rC   r-  sr   s     r'   r{   z!SeqAdd.reduce.<locals>.<listcomp>U  "    #G#G#G!qAAr0   rS   r8  )r	  rZ   r   r   r   re   rH   new_argsid1id2new_seqrJ  r   s        @@r'   rE  zSeqAdd.reduce=  s      	#D//  Q 'oo 	 	FCczz ffQiiG *#G#G#G#G#Gt#G#G#G 000 +  #DE  	" t99>>88::$////r0   c                 D    t          fd| j        D                       S )z9adds up the coefficients of all the sequences at point ptc              3   B   K   | ]}|                               V  d S rh   )rN   )rC   r-  rM   s     r'   r   z%SeqAdd._eval_coeff.<locals>.<genexpr>c  s-      2211772;;222222r0   )r;  rH   rL   s    `r'   rK   zSeqAdd._eval_coeffa  s(    2222	222222r0   Nr   r   r   r   r   r   rE  rK   r=   r0   r'   re   re     sY         8") ") ")H !0 !0 \!0F3 3 3 3 3r0   re   c                   4    e Zd ZdZd Zed             Zd ZdS )rr   a'  Represents term-wise multiplication of sequences.

    Explanation
    ===========

    Handles multiplication of sequences only. For multiplication
    with other objects see :func:`SeqBase.coeff_mul`.

    Rules:
        * The interval on which sequence is defined is the intersection
          of respective intervals of sequences.
        * Anything \* :class:`EmptySequence` returns :class:`EmptySequence`.
        * Other rules are defined in ``_mul`` methods of sequence classes.

    Examples
    ========

    >>> from sympy import EmptySequence, oo, SeqMul, SeqPer, SeqFormula
    >>> from sympy.abc import n
    >>> SeqMul(SeqPer((1, 2), (n, 0, oo)), EmptySequence)
    EmptySequence
    >>> SeqMul(SeqPer((1, 2), (n, 0, 5)), SeqPer((1, 2), (n, 6, 10)))
    EmptySequence
    >>> SeqMul(SeqPer((1, 2), (n, 0, oo)), SeqFormula(n**2))
    SeqMul(SeqFormula(n**2, (n, 0, oo)), SeqPer((1, 2), (n, 0, oo)))
    >>> SeqMul(SeqFormula(n**3), SeqFormula(n**2))
    SeqFormula(n**5, (n, 0, oo))

    See Also
    ========

    sympy.series.sequences.SeqAdd
    c                    |                     dt          j                  }t          |          }fd |          }|st          j        S t          d |D              t          j        u rt          j        S |rt          	                    |          S t          t          |t          j                            }t          j        | g|R  S )Nr8  c                    t          | t                    r;t          | t                    r#t          t	          | j                  g           S | gS t          |           rt          t	          |           g           S t          d          r:  )rb   r    rr   r;  r<  rH   r   rc   r=  s    r'   r?  z SeqMul.__new__.<locals>._flatten  s    #w'' 3c6** !s8SX66;;;5L# 33x--r222 6 7 7 7r0   c              3   $   K   | ]}|j         V  d S rh   r0  r,  s     r'   r   z!SeqMul.__new__.<locals>.<genexpr>  rB  r0   )rC  r   r8  rD  r   r   r   r   rr   rE  r   r    r(   r   r   rF  s       @r'   r   zSeqMul.__new__  s    ::j*;*DEE Dzz		7 		7 		7 		7 		7 x~~  	#?"33d3334
BB?"  	'==&&&GD'"45566}S(4((((r0   c                 n   d}|rxt          |           D ]f\  }d}t          |           D ]I\  }||k    r                              }|&fd| D             }|                    |            nJ|r|}  ng|xt          |           dk    r|                                 S t          | d          S )a.  Simplify a :class:`SeqMul` using known rules.

        Explanation
        ===========

        Iterates through all pairs and ask the constituent
        sequences if they can simplify themselves with any other constituent.

        Notes
        =====

        adapted from ``Union.reduce``

        TFNc                      g | ]
}|fv|S r=   r=   rI  s     r'   r{   z!SeqMul.reduce.<locals>.<listcomp>  rK  r0   rS   rL  )r	  r]   r   r   r   rr   rM  s        @@r'   rE  zSeqMul.reduce  s       	#D//  Q 'oo 	 	FCczz ffQiiG *#G#G#G#G#Gt#G#G#G 000 +  #DE  	" t99>>88::$////r0   c                 N    d}| j         D ]}||                    |          z  }|S )z<multiplies the coefficients of all the sequences at point ptrS   )rH   rN   )r-   rM   valr-  s       r'   rK   zSeqMul._eval_coeff  s3     	 	A1772;;CC
r0   NrT  r=   r0   r'   rr   rr   f  sZ           D )  )  )D $0 $0 \$0L    r0   rr   rh   )6sympy.core.basicr   sympy.core.cacher   sympy.core.containersr   sympy.core.decoratorsr   sympy.core.parametersr   sympy.core.functionr   r	   sympy.core.mulr   sympy.core.numbersr   sympy.core.relationalr   sympy.core.singletonr   r   sympy.core.sortingr   sympy.core.symbolr   r   r   sympy.core.sympifyr   sympy.matricesr   sympy.polysr   r   sympy.sets.setsr   r   sympy.tensor.indexedr   sympy.utilities.iterablesr   r   r   r    r   r   r   r   r   r'  r)  re   rr   r=   r0   r'   <module>ro     s[   " " " " " " $ $ $ $ $ $ ' ' ' ' ' ' 7 7 7 7 7 7 3 3 3 3 3 3 4 4 4 4 4 4 4 4       & & & & & & $ $ $ $ $ $ - - - - - - - - & & & & & & 1 1 1 1 1 1 1 1 1 1 & & & & & & ! ! ! ! ! ! # # # # # # # # 2 2 2 2 2 2 2 2 $ $ $ $ $ $ D D D D D D D D D D^= ^= ^= ^= ^=e ^= ^= ^=@" " " " "Gy " " " "J0" 0" 0" 0" 0"g 0" 0" 0"fN) N) N) N) N)W N) N) N)bqO qO qO qO qO qO qO qOfB B B B B7 B B BJ' ' ' 'N7* 7* 7* 7* 7* 7* 7* 7*tg3 g3 g3 g3 g3Y g3 g3 g3Tq q q q qY q q q q qr0   