o
     ôjH  ã                   @   s  d Z ddlmZmZ ddlmZ ddlmZ ddlm	Z	m
Z
mZ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 ed)dd„ƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Z dd „ Z!d!d"„ Z"d#d$„ Z#G d%d&„ d&ƒZ$eG d'd(„ d(eƒƒZ%d
S )*z@Tools and arithmetics for monomials of distributed polynomials. é    )Úcombinations_with_replacementÚproduct)Údedent)Úcacheit)ÚMulÚSÚTupleÚsympify)ÚExactQuotientFailed)ÚPicklableWithSlotsÚdict_from_expr)Úpublic)Úis_sequenceÚiterableNc                 #   sè  � t ˆ ƒr{t| ƒ}tˆ ƒ|krtdƒ‚ˆdu rdg| ‰nt ˆƒs%tdƒ‚tˆƒ|kr/tdƒ‚tdd„ ˆD ƒƒr<tdƒ‚t‡ ‡fdd„t|ƒD ƒƒrNtd	ƒ‚g }t| ˆˆ ƒD ]\‰}}| ‡fd
d„t||d ƒD ƒ¡ qVt|Ž D ]}t|Ž V  qqdS ˆ }|dk r…tdƒ‚ˆdu rŒd}	n
ˆdk r”tdƒ‚ˆ}	|	|krœdS | r¢|dkr¨t	j
V  dS t| ƒt	j
g } tdd„ | D ƒƒr¿t| |ƒ}
nt| |d�}
tƒ }||	 }|
D ]}d}|D ]}|dkrä|d7 }||k rä nqÔ| t|Ž ¡ qÎ|E dH  dS )a‚  
    ``max_degrees`` and ``min_degrees`` are either both integers or both lists.
    Unless otherwise specified, ``min_degrees`` is either ``0`` or
    ``[0, ..., 0]``.

    A generator of all monomials ``monom`` is returned, such that
    either
    ``min_degree <= total_degree(monom) <= max_degree``,
    or
    ``min_degrees[i] <= degree_list(monom)[i] <= max_degrees[i]``,
    for all ``i``.

    Case I. ``max_degrees`` and ``min_degrees`` are both integers
    =============================================================

    Given a set of variables $V$ and a min_degree $N$ and a max_degree $M$
    generate a set of monomials of degree less than or equal to $N$ and greater
    than or equal to $M$. The total number of monomials in commutative
    variables is huge and is given by the following formula if $M = 0$:

        .. math::
            \frac{(\#V + N)!}{\#V! N!}

    For example if we would like to generate a dense polynomial of
    a total degree $N = 50$ and $M = 0$, which is the worst case, in 5
    variables, assuming that exponents and all of coefficients are 32-bit long
    and stored in an array we would need almost 80 GiB of memory! Fortunately
    most polynomials, that we will encounter, are sparse.

    Consider monomials in commutative variables $x$ and $y$
    and non-commutative variables $a$ and $b$::

        >>> from sympy import symbols
        >>> from sympy.polys.monomials import itermonomials
        >>> from sympy.polys.orderings import monomial_key
        >>> from sympy.abc import x, y

        >>> sorted(itermonomials([x, y], 2), key=monomial_key('grlex', [y, x]))
        [1, x, y, x**2, x*y, y**2]

        >>> sorted(itermonomials([x, y], 3), key=monomial_key('grlex', [y, x]))
        [1, x, y, x**2, x*y, y**2, x**3, x**2*y, x*y**2, y**3]

        >>> a, b = symbols('a, b', commutative=False)
        >>> set(itermonomials([a, b, x], 2))
        {1, a, a**2, b, b**2, x, x**2, a*b, b*a, x*a, x*b}

        >>> sorted(itermonomials([x, y], 2, 1), key=monomial_key('grlex', [y, x]))
        [x, y, x**2, x*y, y**2]

    Case II. ``max_degrees`` and ``min_degrees`` are both lists
    ===========================================================

    If ``max_degrees = [d_1, ..., d_n]`` and
    ``min_degrees = [e_1, ..., e_n]``, the number of monomials generated
    is:

    .. math::
        (d_1 - e_1 + 1) (d_2 - e_2 + 1) \cdots (d_n - e_n + 1)

    Let us generate all monomials ``monom`` in variables $x$ and $y$
    such that ``[1, 2][i] <= degree_list(monom)[i] <= [2, 4][i]``,
    ``i = 0, 1`` ::

        >>> from sympy import symbols
        >>> from sympy.polys.monomials import itermonomials
        >>> from sympy.polys.orderings import monomial_key
        >>> from sympy.abc import x, y

        >>> sorted(itermonomials([x, y], [2, 4], [1, 2]), reverse=True, key=monomial_key('lex', [x, y]))
        [x**2*y**4, x**2*y**3, x**2*y**2, x*y**4, x*y**3, x*y**2]
    zArgument sizes do not matchNr   zmin_degrees is not a listc                 s   s   � | ]}|d k V  qdS ©r   N© ©Ú.0Úir   r   úh/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/sympy/polys/monomials.pyÚ	<genexpr>c   ó   € z itermonomials.<locals>.<genexpr>z+min_degrees cannot contain negative numbersc                 3   s    � | ]}ˆ| ˆ | kV  qd S ©Nr   r   )Úmax_degreesÚmin_degreesr   r   r   e   s   € z2min_degrees[i] must be <= max_degrees[i] for all ic                    s   g | ]}ˆ | ‘qS r   r   r   )Úvarr   r   Ú
<listcomp>i   ó    z!itermonomials.<locals>.<listcomp>é   zmax_degrees cannot be negativezmin_degrees cannot be negativec                 s   s   � | ]}|j V  qd S r   )Úis_commutative)r   Úvariabler   r   r   r   }   s   € )Úrepeat)r   ÚlenÚ
ValueErrorÚanyÚrangeÚzipÚappendr   r   r   ÚOneÚlistÚallr   ÚsetÚadd)Ú	variablesr   r   ÚnZpower_listsZmin_dZmax_dÚpowersÚ
max_degreeZ
min_degreeÚitZmonomials_setÚdÚitemÚcountr    r   )r   r   r   r   Úitermonomials   sf   €J$ÿ€€r5   c                 C   s(   ddl m} || | ƒ|| ƒ ||ƒ S )aW  
    Computes the number of monomials.

    The number of monomials is given by the following formula:

    .. math::

        \frac{(\#V + N)!}{\#V! N!}

    where `N` is a total degree and `V` is a set of variables.

    Examples
    ========

    >>> from sympy.polys.monomials import itermonomials, monomial_count
    >>> from sympy.polys.orderings import monomial_key
    >>> from sympy.abc import x, y

    >>> monomial_count(2, 2)
    6

    >>> M = list(itermonomials([x, y], 2))

    >>> sorted(M, key=monomial_key('grlex', [y, x]))
    [1, x, y, x**2, x*y, y**2]
    >>> len(M)
    6

    r   )Ú	factorial)Ú(sympy.functions.combinatorial.factorialsr6   )ÚVÚNr6   r   r   r   Úmonomial_countŽ   s   r:   c                 C   ó   t dd„ t| |ƒD ƒƒS )a%  
    Multiplication of tuples representing monomials.

    Examples
    ========

    Lets multiply `x**3*y**4*z` with `x*y**2`::

        >>> from sympy.polys.monomials import monomial_mul

        >>> monomial_mul((3, 4, 1), (1, 2, 0))
        (4, 6, 1)

    which gives `x**4*y**5*z`.

    c                 S   s   g | ]\}}|| ‘qS r   r   ©r   ÚaÚbr   r   r   r   À   ó    z monomial_mul.<locals>.<listcomp>©Útupler&   ©ÚAÚBr   r   r   Úmonomial_mul¯   ó   rE   c                 C   s(   t | |ƒ}tdd„ |D ƒƒrt|ƒS dS )aœ  
    Division of tuples representing monomials.

    Examples
    ========

    Lets divide `x**3*y**4*z` by `x*y**2`::

        >>> from sympy.polys.monomials import monomial_div

        >>> monomial_div((3, 4, 1), (1, 2, 0))
        (2, 2, 1)

    which gives `x**2*y**2*z`. However::

        >>> monomial_div((3, 4, 1), (1, 2, 2)) is None
        True

    `x*y**2*z**2` does not divide `x**3*y**4*z`.

    c                 s   s   � | ]}|d kV  qdS r   r   )r   Úcr   r   r   r   Ú   r   zmonomial_div.<locals>.<genexpr>N)Úmonomial_ldivr*   rA   )rC   rD   ÚCr   r   r   Úmonomial_divÂ   s   
rJ   c                 C   r;   )a…  
    Division of tuples representing monomials.

    Examples
    ========

    Lets divide `x**3*y**4*z` by `x*y**2`::

        >>> from sympy.polys.monomials import monomial_ldiv

        >>> monomial_ldiv((3, 4, 1), (1, 2, 0))
        (2, 2, 1)

    which gives `x**2*y**2*z`.

        >>> monomial_ldiv((3, 4, 1), (1, 2, 2))
        (2, 2, -1)

    which gives `x**2*y**2*z**-1`.

    c                 S   s   g | ]\}}|| ‘qS r   r   r<   r   r   r   r   õ   r?   z!monomial_ldiv.<locals>.<listcomp>r@   rB   r   r   r   rH   ß   s   rH   c                    s   t ‡ fdd„| D ƒƒS )z%Return the n-th pow of the monomial. c                    s   g | ]}|ˆ  ‘qS r   r   ©r   r=   ©r.   r   r   r   ù   r   z monomial_pow.<locals>.<listcomp>)rA   )rC   r.   r   rL   r   Úmonomial_pow÷   s   rM   c                 C   r;   )a.  
    Greatest common divisor of tuples representing monomials.

    Examples
    ========

    Lets compute GCD of `x*y**4*z` and `x**3*y**2`::

        >>> from sympy.polys.monomials import monomial_gcd

        >>> monomial_gcd((1, 4, 1), (3, 2, 0))
        (1, 2, 0)

    which gives `x*y**2`.

    c                 S   ó   g | ]	\}}t ||ƒ‘qS r   )Úminr<   r   r   r   r     ó    z monomial_gcd.<locals>.<listcomp>r@   rB   r   r   r   Úmonomial_gcdû   rF   rQ   c                 C   r;   )a1  
    Least common multiple of tuples representing monomials.

    Examples
    ========

    Lets compute LCM of `x*y**4*z` and `x**3*y**2`::

        >>> from sympy.polys.monomials import monomial_lcm

        >>> monomial_lcm((1, 4, 1), (3, 2, 0))
        (3, 4, 1)

    which gives `x**3*y**4*z`.

    c                 S   rN   r   )Úmaxr<   r   r   r   r     rP   z monomial_lcm.<locals>.<listcomp>r@   rB   r   r   r   Úmonomial_lcm  rF   rS   c                 C   r;   )zö
    Does there exist a monomial X such that XA == B?

    Examples
    ========

    >>> from sympy.polys.monomials import monomial_divides
    >>> monomial_divides((1, 2), (3, 4))
    True
    >>> monomial_divides((1, 2), (0, 2))
    False
    c                 s   s   � | ]	\}}||kV  qd S r   r   r<   r   r   r   r   .  s   € z#monomial_divides.<locals>.<genexpr>)r*   r&   rB   r   r   r   Úmonomial_divides!  s   rT   c                  G   óJ   t | d ƒ}| dd… D ]}t|ƒD ]\}}t|| |ƒ||< qqt|ƒS )a‘  
    Returns maximal degree for each variable in a set of monomials.

    Examples
    ========

    Consider monomials `x**3*y**4*z**5`, `y**5*z` and `x**6*y**3*z**9`.
    We wish to find out what is the maximal degree for each of `x`, `y`
    and `z` variables::

        >>> from sympy.polys.monomials import monomial_max

        >>> monomial_max((3,4,5), (0,5,1), (6,3,9))
        (6, 5, 9)

    r   r   N)r)   Ú	enumeraterR   rA   ©ÚmonomsÚMr9   r   r.   r   r   r   Úmonomial_max0  ó   ÿrZ   c                  G   rU   )a‘  
    Returns minimal degree for each variable in a set of monomials.

    Examples
    ========

    Consider monomials `x**3*y**4*z**5`, `y**5*z` and `x**6*y**3*z**9`.
    We wish to find out what is the minimal degree for each of `x`, `y`
    and `z` variables::

        >>> from sympy.polys.monomials import monomial_min

        >>> monomial_min((3,4,5), (0,5,1), (6,3,9))
        (0, 3, 1)

    r   r   N)r)   rV   rO   rA   rW   r   r   r   Úmonomial_minI  r[   r\   c                 C   s   t | ƒS )zÍ
    Returns the total degree of a monomial.

    Examples
    ========

    The total degree of `xy^2` is 3:

    >>> from sympy.polys.monomials import monomial_deg
    >>> monomial_deg((1, 2))
    3
    )Úsum)rY   r   r   r   Úmonomial_degb  s   r^   c                 C   s`   | \}}|\}}t ||ƒ}|jr|dur|| ||¡fS dS |du s.|| s.|| ||¡fS dS )z,Division of two terms in over a ring/field. N)rJ   Úis_FieldÚquo)r=   r>   ÚdomainZa_lmZa_lcZb_lmZb_lcÚmonomr   r   r   Úterm_divq  s   
rc   c                       s�   e Zd ZdZe‡ fdd„ƒZdd„ Zdd„ Zdd	„ Zed
d„ ƒZ	edd„ ƒZ
edd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZ‡  ZS )ÚMonomialOpsz6Code generator of fast monomial arithmetic functions. c                    s   t ƒ  | ¡}||_|S r   )ÚsuperÚ__new__Úngens)Úclsrg   Úobj©Ú	__class__r   r   rf   †  s   zMonomialOps.__new__c                 C   s   | j fS r   )rg   ©Úselfr   r   r   Ú__getnewargs__Œ  s   zMonomialOps.__getnewargs__c                 C   s   i }t ||ƒ || S r   )Úexec)rm   ÚcodeÚnameÚnsr   r   r   Ú_build�  s   
zMonomialOps._buildc                    s   ‡ fdd„t | jƒD ƒS )Nc                    s   g | ]}d ˆ |f ‘qS )z%s%sr   r   ©rq   r   r   r   •  r?   z%MonomialOps._vars.<locals>.<listcomp>)r%   rg   )rm   rq   r   rt   r   Ú_vars”  s   zMonomialOps._varsc                 C   ód   d}t dƒ}|  d¡}|  d¡}dd„ t||ƒD ƒ}||d |¡d |¡d |¡dœ }|  ||¡S )	NrE   ús        def %(name)s(A, B):
            (%(A)s,) = A
            (%(B)s,) = B
            return (%(AB)s,)
        r=   r>   c                 S   ó   g | ]
\}}d ||f ‘qS )z%s + %sr   r<   r   r   r   r   ¢  ó    z#MonomialOps.mul.<locals>.<listcomp>ú, ©rq   rC   rD   ÚAB©r   ru   r&   Újoinrs   ©rm   rq   ÚtemplaterC   rD   r|   rp   r   r   r   Úmul—  ó   

$zMonomialOps.mulc                 C   sL   d}t dƒ}|  d¡}dd„ |D ƒ}||d |¡d |¡dœ }|  ||¡S )NrM   zZ        def %(name)s(A, k):
            (%(A)s,) = A
            return (%(Ak)s,)
        r=   c                 S   s   g | ]}d | ‘qS )z%s*kr   rK   r   r   r   r   ¯  r   z#MonomialOps.pow.<locals>.<listcomp>rz   )rq   rC   ÚAk)r   ru   r~   rs   )rm   rq   r€   rC   rƒ   rp   r   r   r   Úpow¦  s   
zMonomialOps.powc                 C   rv   )	NZmonomial_mulpowzw        def %(name)s(A, B, k):
            (%(A)s,) = A
            (%(B)s,) = B
            return (%(ABk)s,)
        r=   r>   c                 S   rx   )z	%s + %s*kr   r<   r   r   r   r   ¾  ry   z&MonomialOps.mulpow.<locals>.<listcomp>rz   )rq   rC   rD   ÚABkr}   )rm   rq   r€   rC   rD   r…   rp   r   r   r   Úmulpow³  r‚   zMonomialOps.mulpowc                 C   rv   )	NrH   rw   r=   r>   c                 S   rx   )z%s - %sr   r<   r   r   r   r   Í  ry   z$MonomialOps.ldiv.<locals>.<listcomp>rz   r{   r}   r   r   r   r   ÚldivÂ  r‚   zMonomialOps.ldivc                 C   sv   d}t dƒ}|  d¡}|  d¡}dd„ t| jƒD ƒ}|  d¡}||d |¡d |¡d	 |¡d |¡d
œ }|  ||¡S )NrJ   z†        def %(name)s(A, B):
            (%(A)s,) = A
            (%(B)s,) = B
            %(RAB)s
            return (%(R)s,)
        r=   r>   c                 S   s   g | ]}d d|i ‘qS )z7r%(i)s = a%(i)s - b%(i)s
    if r%(i)s < 0: return Noner   r   r   r   r   r   r   Ý  r?   z#MonomialOps.div.<locals>.<listcomp>Úrrz   z
    )rq   rC   rD   ÚRABÚR)r   ru   r%   rg   r~   rs   )rm   rq   r€   rC   rD   r‰   rŠ   rp   r   r   r   ÚdivÑ  s   


,zMonomialOps.divc                 C   rv   )	NrS   rw   r=   r>   c                 S   ó    g | ]\}}d ||||f ‘qS )z%s if %s >= %s else %sr   r<   r   r   r   r   í  ó     z#MonomialOps.lcm.<locals>.<listcomp>rz   r{   r}   r   r   r   r   Úlcmâ  r‚   zMonomialOps.lcmc                 C   rv   )	NrQ   rw   r=   r>   c                 S   rŒ   )z%s if %s <= %s else %sr   r<   r   r   r   r   ü  r�   z#MonomialOps.gcd.<locals>.<listcomp>rz   r{   r}   r   r   r   r   Úgcdñ  r‚   zMonomialOps.gcd)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   rf   rn   rs   ru   r�   r„   r†   r‡   r‹   rŽ   r�   Ú__classcell__r   r   rj   r   rd   ƒ  s*    





rd   c                   @   s”   e Zd ZdZdZd"dd„Zd"dd„Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zd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S )#ÚMonomialz9Class representing a monomial, i.e. a product of powers. )Ú	exponentsÚgensNc                 C   sv   t |ƒs.tt|ƒ|d�\}}t|ƒdkr't| ¡ ƒd dkr't| ¡ ƒd }ntd |¡ƒ‚t	t
t|ƒƒ| _|| _d S )N)r—   r   r   zExpected a monomial got {})r   r   r	   r"   r)   ÚvaluesÚkeysr#   ÚformatrA   ÚmapÚintr–   r—   )rm   rb   r—   Úrepr   r   r   Ú__init__  s    
zMonomial.__init__c                 C   s   |   ||p| j¡S r   )rk   r—   )rm   r–   r—   r   r   r   Úrebuild  s   zMonomial.rebuildc                 C   ó
   t | jƒS r   )r"   r–   rl   r   r   r   Ú__len__  ó   
zMonomial.__len__c                 C   r    r   )Úiterr–   rl   r   r   r   Ú__iter__  r¢   zMonomial.__iter__c                 C   s
   | j | S r   )r–   )rm   r3   r   r   r   Ú__getitem__  r¢   zMonomial.__getitem__c                 C   s   t | jj| j| jfƒS r   )Úhashrk   r�   r–   r—   rl   r   r   r   Ú__hash__  s   zMonomial.__hash__c                 C   s6   | j rd dd„ t| j | jƒD ƒ¡S d| jj| jf S )NÚ*c                 S   rx   )z%s**%sr   ©r   ÚgenÚexpr   r   r   r   "  ry   z$Monomial.__str__.<locals>.<listcomp>z%s(%s))r—   r~   r&   r–   rk   r�   rl   r   r   r   Ú__str__   s   zMonomial.__str__c                 G   s4   |p| j }|std|  ƒ‚tdd„ t|| jƒD ƒŽ S )z3Convert a monomial instance to a SymPy expression. z5Cannot convert %s to an expression without generatorsc                 S   s   g | ]\}}|| ‘qS r   r   r©   r   r   r   r   .  r?   z$Monomial.as_expr.<locals>.<listcomp>)r—   r#   r   r&   r–   )rm   r—   r   r   r   Úas_expr&  s   
ÿzMonomial.as_exprc                 C   s4   t |tƒr	|j}nt |ttfƒr|}ndS | j|kS )NF)Ú
isinstancer•   r–   rA   r   ©rm   Úotherr–   r   r   r   Ú__eq__0  s   

zMonomial.__eq__c                 C   s
   | |k S r   r   )rm   r°   r   r   r   Ú__ne__:  r¢   zMonomial.__ne__c                 C   s<   t |tƒr	|j}nt |ttfƒr|}nt‚|  t| j|ƒ¡S r   )r®   r•   r–   rA   r   ÚNotImplementedErrorrŸ   rE   r¯   r   r   r   Ú__mul__=  s   
zMonomial.__mul__c                 C   sV   t |tƒr	|j}nt |ttfƒr|}nt‚t| j|ƒ}|d ur$|  |¡S t| t|ƒƒ‚r   )	r®   r•   r–   rA   r   r³   rJ   rŸ   r
   )rm   r°   r–   Úresultr   r   r   Ú__truediv__G  s   

zMonomial.__truediv__c                 C   s.   t |ƒ}|dk rtd| ƒ‚|  t| j|ƒ¡S )Nr   z'a non-negative integer expected, got %s)rœ   r#   rŸ   rM   r–   )rm   r°   r.   r   r   r   Ú__pow__X  s   zMonomial.__pow__c                 C   óD   t |tƒr	|j}nt |ttfƒr|}ntd| ƒ‚|  t| j|ƒ¡S )z&Greatest common divisor of monomials. ú.an instance of Monomial class expected, got %s)r®   r•   r–   rA   r   Ú	TypeErrorrŸ   rQ   r¯   r   r   r   r�   ^  ó   
ÿzMonomial.gcdc                 C   r¸   )z$Least common multiple of monomials. r¹   )r®   r•   r–   rA   r   rº   rŸ   rS   r¯   r   r   r   rŽ   j  r»   zMonomial.lcmr   )r�   r‘   r’   r“   Ú	__slots__rž   rŸ   r¡   r¤   r¥   r§   r¬   r­   r±   r²   r´   r¶   Ú__floordiv__r·   r�   rŽ   r   r   r   r   r•      s&    




r•   r   )&r“   Ú	itertoolsr   r   Útextwrapr   Úsympy.core.cacher   Ú
sympy.corer   r   r   r	   Úsympy.polys.polyerrorsr
   Úsympy.polys.polyutilsr   r   Úsympy.utilitiesr   Úsympy.utilities.iterablesr   r   r5   r:   rE   rJ   rH   rM   rQ   rS   rT   rZ   r\   r^   rc   rd   r•   r   r   r   r   Ú<module>   s4    !}