§
    OŠtj  ã                   ó>   — d Z ddlmZmZ ddlmZ ddlmZ d„ Zd„ Z	dS )z
Recurrences
é    )ÚSÚsympify)Úiterable)Úas_intc                 ó^  — | st           j        S t          | ¦  «        st          d¦  «        ‚t          |¦  «        st          d¦  «        ‚t	          |¦  «        }|dk     rt          d¦  «        ‚d„ | D ¦   «         }d„ |D ¦   «         }t          |¦  «        }t          |¦  «        |k    rt          d¦  «        ‚|t           j        g|t          |¦  «        z
  z  z  }||k     r||         S d„ t          t          ||¦  «        |¦  «        D ¦   «         }t          |d	d
…         |d
         ¦  «        S )aŽ	  
    Evaluation of univariate linear recurrences of homogeneous type
    having coefficients independent of the recurrence variable.

    Parameters
    ==========

    coeffs : iterable
        Coefficients of the recurrence
    init : iterable
        Initial values of the recurrence
    n : Integer
        Point of evaluation for the recurrence

    Notes
    =====

    Let `y(n)` be the recurrence of given type, ``c`` be the sequence
    of coefficients, ``b`` be the sequence of initial/base values of the
    recurrence and ``k`` (equal to ``len(c)``) be the order of recurrence.
    Then,

    .. math :: y(n) = \begin{cases} b_n & 0 \le n < k \\
        c_0 y(n-1) + c_1 y(n-2) + \cdots + c_{k-1} y(n-k) & n \ge k
        \end{cases}

    Let `x_0, x_1, \ldots, x_n` be a sequence and consider the transformation
    that maps each polynomial `f(x)` to `T(f(x))` where each power `x^i` is
    replaced by the corresponding value `x_i`. The sequence is then a solution
    of the recurrence if and only if `T(x^i p(x)) = 0` for each `i \ge 0` where
    `p(x) = x^k - c_0 x^(k-1) - \cdots - c_{k-1}` is the characteristic
    polynomial.

    Then `T(f(x)p(x)) = 0` for each polynomial `f(x)` (as it is a linear
    combination of powers `x^i`). Now, if `x^n` is congruent to
    `g(x) = a_0 x^0 + a_1 x^1 + \cdots + a_{k-1} x^{k-1}` modulo `p(x)`, then
    `T(x^n) = x_n` is equal to
    `T(g(x)) = a_0 x_0 + a_1 x_1 + \cdots + a_{k-1} x_{k-1}`.

    Computation of `x^n`,
    given `x^k = c_0 x^{k-1} + c_1 x^{k-2} + \cdots + c_{k-1}`
    is performed using exponentiation by squaring (refer to [1_]) with
    an additional reduction step performed to retain only first `k` powers
    of `x` in the representation of `x^n`.

    Examples
    ========

    >>> from sympy.discrete.recurrences import linrec
    >>> from sympy.abc import x, y, z

    >>> linrec(coeffs=[1, 1], init=[0, 1], n=10)
    55

    >>> linrec(coeffs=[1, 1], init=[x, y], n=10)
    34*x + 55*y

    >>> linrec(coeffs=[x, y], init=[0, 1], n=5)
    x**2*y + x*(x**3 + 2*x*y) + y**2

    >>> linrec(coeffs=[1, 2, 3, 0, 0, 4], init=[x, y, z], n=16)
    13576*x + 5676*y + 2356*z

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Exponentiation_by_squaring
    .. [2] https://en.wikipedia.org/w/index.php?title=Modular_exponentiation&section=6#Matrices

    See Also
    ========

    sympy.polys.agca.extensions.ExtensionElement.__pow__

    z6Expected a sequence of coefficients for the recurrencezFExpected a sequence of values for the initialization of the recurrencer   z@Point of evaluation of recurrence must be a non-negative integerc                 ó,   — g | ]}t          |¦  «        ‘ŒS © ©r   ©Ú.0Úargs     úX/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/sympy/discrete/recurrences.pyú
<listcomp>zlinrec.<locals>.<listcomp>g   s   € Ð(Ð(Ð(˜#��‰ŒÐ(Ð(Ð(ó    c                 ó,   — g | ]}t          |¦  «        ‘ŒS r	   r
   r   s     r   r   zlinrec.<locals>.<listcomp>h   s   € Ð&Ð&Ð&˜#��‰ŒÐ&Ð&Ð&r   zECount of initial values should not exceed the order of the recurrencec                 ó   — g | ]
\  }}||z  ‘ŒS r	   r	   )r   ÚuÚvs      r   r   zlinrec.<locals>.<listcomp>s   s    € Ð9Ð9Ð9‘T�Q˜ˆQˆq‰SÐ9Ð9Ð9r   Néÿÿÿÿ)
r   ÚZeror   Ú	TypeErrorr   Ú
ValueErrorÚlenÚzipÚlinrec_coeffsÚsum)ÚcoeffsÚinitÚnÚcÚbÚkÚtermss          r   Úlinrecr$   
   s\  € ðZ ð ÝŒvˆå�FÑÔð +Ýð *ñ +ô +ð 	+õ �D‰>Œ>ð .Ýð -ñ .ô .ð 	.õ 	ˆq‰	Œ	€AØˆ1‚u€uÝð /ñ 0ô 0ð 	0ð 	)Ð( Ð(Ñ(Ô(€AØ&Ð& Ð&Ñ&Ô&€AÝˆA‰Œ€Aå
ˆ1�v„v�‚z€zÝð 2ñ 3ô 3ð 	3ð 	
�aŒfˆX�q�3˜q™6œ6‘zÑ"Ñ"ˆàˆ1‚u€uØ�ŒtˆØ9Ð9�S¥¨q°!Ñ!4Ô!4°aÑ8Ô8Ð9Ñ9Ô9€EÝˆu�S�b�SŒz˜5 œ9Ñ%Ô%Ð%r   c                 óX   ‡ ‡‡‡— t          ‰ ¦  «        Šˆ ˆfd„Šˆˆˆfd„Š ‰|¦  «        S )a–  
    Compute the coefficients of n'th term in linear recursion
    sequence defined by c.

    `x^k = c_0 x^{k-1} + c_1 x^{k-2} + \cdots + c_{k-1}`.

    It computes the coefficients by using binary exponentiation.
    This function is used by `linrec` and `_eval_pow_by_cayley`.

    Parameters
    ==========

    c = coefficients of the divisor polynomial
    n = exponent of x, so dividend is x^n

    c                 ó¦  •— t           j        gdt          | ¦  «        z  dz
  |z   z  }t          | ¦  «        D ]3\  }}t          | ¦  «        D ]\  }}|||z   |z   xx         ||z  z  cc<   ŒŒ4t	          t          |¦  «        dz
  ‰dz
  d¦  «        D ]9}t	          ‰¦  «        D ]'}|||z
  dz
  xx         ||         ‰|         z  z  cc<   Œ(Œ:|d ‰…         S )Né   é   r   )r   r   r   Ú	enumerateÚrange)	r   ÚoffsetÚwÚiÚpÚjÚqr    r"   s	          €€r   Ú_square_and_reducez)linrec_coeffs.<locals>._square_and_reduce‹   s  ø€ õ ŒVˆH�a�˜A™œ‘h ‘l VÑ+Ñ,ˆÝ˜a‘L”Lð 	)ð 	)‰DˆAˆqÝ! !™œð )ð )‘��1Ø�&˜1‘*˜q‘.Ð!Ð!Ô! Q q¡SÑ(Ð!Ð!Ñ!Ð!ð)õ •s˜1‘v”v ‘z 1 q¡5¨"Ñ-Ô-ð 	*ð 	*ˆAÝ˜1‘X”Xð *ð *�Ø�!�a‘%˜!‘)��”  !¤ Q q¤T¡	Ñ)��‘�ð*ð ��!�Œuˆr   c                 ó°   •— | ‰k     r5t           j        g| z  t           j        gz   t           j        g‰| z
  dz
  z  z   S  ‰ ‰| dz  ¦  «        | dz  ¦  «        S )Nr(   r'   )r   r   ÚOne)r   Ú_final_coeffsr1   r"   s    €€€r   r4   z$linrec_coeffs.<locals>._final_coeffsœ   s`   ø€ ð
 ˆqŠ5ˆ5Ý”F�8˜A‘:¥¤ Ñ'­1¬6¨(°A¸±E¸A±IÑ*>Ñ>Ð>à%Ð% m m°A¸±FÑ&;Ô&;¸QÀ¹UÑCÔCÐCr   )r   )r    r   r4   r1   r"   s   ` @@@r   r   r   w   sm   øøøø€ õ$ 	ˆA‰Œ€Aðð ð ð ð ð ð"Dð Dð Dð Dð Dð Dð Dð ˆ=˜ÑÔÐr   N)
Ú__doc__Ú
sympy.corer   r   Úsympy.utilities.iterablesr   Úsympy.utilities.miscr   r$   r   r	   r   r   ú<module>r9      sy   ððð ð "Ð !Ð !Ð !Ð !Ð !Ð !Ð !Ø .Ð .Ð .Ð .Ð .Ð .Ø 'Ð 'Ð 'Ð 'Ð 'Ð 'ðj&ð j&ð j&ðZ/ð /ð /ð /ð /r   