a
    kh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  rt| }t |kr$tddu r8dg| n@t sJtdn.t|kr^tdtdd D rxtdt fddt|D rtd	g }t|  D ],\}}|fd
dt||d D  qt| D ]}t| V  qn }|dk rtddu rd}	ndk r.td}	|	|kr@dS | rP|dkr\t	j
V  dS t| t	j
g } tdd | D rt| |}
nt| |d}
t }||	 }|
D ]F}d}|D ](}|dkr|d7 }||k r q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   C/var/www/auris/lib/python3.9/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   r   z2min_degrees[i] must be <= max_degrees[i] for all ic                    s   g | ]} | qS r   r   r   )varr   r   
<listcomp>i   r   z!itermonomials.<locals>.<listcomp>   zmax_degrees cannot be negativezmin_degrees cannot be negativec                 s   s   | ]}|j V  qd S r   )Zis_commutative)r   variabler   r   r   r   }   r   )repeat)r   len
ValueErroranyrangezipappendr   r   r   ZOnelistallr   setadd)	variablesr   r   nZpower_listsZmin_dZmax_dZpowersZ
max_degreeZ
min_degreeitZmonomials_setditemcountr   r   )r   r   r   r   itermonomials   s^    J
$






r0   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)Z(sympy.functions.combinatorial.factorialsr1   )VNr1   r   r   r   monomial_count   s    r4   c                 C   s   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      r   z monomial_mul.<locals>.<listcomp>tupler$   ABr   r   r   monomial_mul   s    r=   c                 C   s,   t | |}tdd |D r$t|S d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'   r9   )r;   r<   Cr   r   r   monomial_div   s    
rA   c                 C   s   t dd t| |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_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   r5   r   r   r   r      r   z!monomial_ldiv.<locals>.<listcomp>r8   r:   r   r   r   r?      s    r?   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   r6   r+   r   r   r      r   z monomial_pow.<locals>.<listcomp>)r9   )r;   r+   r   rC   r   monomial_pow   s    rD   c                 C   s   t dd t| |D S )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   s   g | ]\}}t ||qS r   )minr5   r   r   r   r     r   z monomial_gcd.<locals>.<listcomp>r8   r:   r   r   r   monomial_gcd   s    rF   c                 C   s   t dd t| |D S )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   s   g | ]\}}t ||qS r   )maxr5   r   r   r   r     r   z monomial_lcm.<locals>.<listcomp>r8   r:   r   r   r   monomial_lcm  s    rH   c                 C   s   t dd t| |D S )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   r5   r   r   r   r   .  r   z#monomial_divides.<locals>.<genexpr>)r'   r$   r:   r   r   r   monomial_divides!  s    rI   c                  G   sJ   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&   	enumeraterG   r9   ZmonomsMr3   r   r+   r   r   r   monomial_max0  s
    rM   c                  G   sJ   t | d }| dd D ](}t|D ]\}}t|| |||< q$qt|S )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&   rJ   rE   r9   rK   r   r   r   monomial_minI  s
    rN   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)rL   r   r   r   monomial_degb  s    rP   c                 C   sf   | \}}|\}}t ||}|jr>|dur8||||fS dS n$|du s^|| s^||||fS dS dS )z,Division of two terms in over a ring/field. N)rA   Zis_FieldZquo)r6   r7   domainZa_lmZa_lcZb_lmZb_lcmonomr   r   r   term_divq  s    
rS   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)clsrW   obj	__class__r   r   rV     s    zMonomialOps.__new__c                 C   s   | j fS r   )rW   selfr   r   r   __getnewargs__  s    zMonomialOps.__getnewargs__c                 C   s   i }t || || S r   )exec)r]   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   ra   r   r   r     r   z%MonomialOps._vars.<locals>.<listcomp>)r#   rW   )r]   ra   r   rd   r   _vars  s    zMonomialOps._varsc                 C   sd   d}t d}| d}| d}dd t||D }||d|d|d|d }| ||S )	Nr=   s        def %(name)s(A, B):
            (%(A)s,) = A
            (%(B)s,) = B
            return (%(AB)s,)
        r6   r7   c                 S   s   g | ]\}}d ||f qS )z%s + %sr   r5   r   r   r   r     r   z#MonomialOps.mul.<locals>.<listcomp>, ra   r;   r<   ABr   re   r$   joinrc   r]   ra   templater;   r<   ri   r`   r   r   r   mul  s    

$zMonomialOps.mulc                 C   sL   d}t d}| d}dd |D }||d|d|d }| ||S )NrD   zZ        def %(name)s(A, k):
            (%(A)s,) = A
            return (%(Ak)s,)
        r6   c                 S   s   g | ]}d | qS )z%s*kr   rB   r   r   r   r     r   z#MonomialOps.pow.<locals>.<listcomp>rg   )ra   r;   Ak)r   re   rk   rc   )r]   ra   rm   r;   ro   r`   r   r   r   pow  s    
zMonomialOps.powc                 C   sd   d}t d}| d}| d}dd t||D }||d|d|d|d }| ||S )	NZmonomial_mulpowzw        def %(name)s(A, B, k):
            (%(A)s,) = A
            (%(B)s,) = B
            return (%(ABk)s,)
        r6   r7   c                 S   s   g | ]\}}d ||f qS )z	%s + %s*kr   r5   r   r   r   r     r   z&MonomialOps.mulpow.<locals>.<listcomp>rg   )ra   r;   r<   ABkrj   )r]   ra   rm   r;   r<   rq   r`   r   r   r   mulpow  s    

$zMonomialOps.mulpowc                 C   sd   d}t d}| d}| d}dd t||D }||d|d|d|d }| ||S )	Nr?   rf   r6   r7   c                 S   s   g | ]\}}d ||f qS )z%s - %sr   r5   r   r   r   r     r   z$MonomialOps.ldiv.<locals>.<listcomp>rg   rh   rj   rl   r   r   r   ldiv  s    

$zMonomialOps.ldivc                 C   sv   d}t d}| d}| d}dd t| jD }| d}||d|d|d	|d|d
 }| ||S )NrA   z        def %(name)s(A, B):
            (%(A)s,) = A
            (%(B)s,) = B
            %(RAB)s
            return (%(R)s,)
        r6   r7   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>rrg   z
    )ra   r;   r<   RABR)r   re   r#   rW   rk   rc   )r]   ra   rm   r;   r<   ru   rv   r`   r   r   r   div  s    


,zMonomialOps.divc                 C   sd   d}t d}| d}| d}dd t||D }||d|d|d|d }| ||S )	NrH   rf   r6   r7   c                 S   s    g | ]\}}d ||||f qS )z%s if %s >= %s else %sr   r5   r   r   r   r     r   z#MonomialOps.lcm.<locals>.<listcomp>rg   rh   rj   rl   r   r   r   lcm  s    

$zMonomialOps.lcmc                 C   sd   d}t d}| d}| d}dd t||D }||d|d|d|d }| ||S )	NrF   rf   r6   r7   c                 S   s    g | ]\}}d ||||f qS )z%s if %s <= %s else %sr   r5   r   r   r   r     r   z#MonomialOps.gcd.<locals>.<listcomp>rg   rh   rj   rl   r   r   r   gcd  s    

$zMonomialOps.gcd)__name__
__module____qualname____doc__r   rV   r^   rc   re   rn   rp   rr   rs   rw   rx   ry   __classcell__r   r   rZ   r   rT     s(   





rT   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rNt| d dkrNt| 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!   formatr9   mapintr   r   )r]   rR   r   repr   r   r   __init__  s     zMonomial.__init__c                 C   s   |  ||p| jS r   )r[   r   )r]   r   r   r   r   r   rebuild  s    zMonomial.rebuildc                 C   s
   t | jS r   )r    r   r\   r   r   r   __len__  s    zMonomial.__len__c                 C   s
   t | jS r   )iterr   r\   r   r   r   __iter__  s    zMonomial.__iter__c                 C   s
   | j | S r   )r   )r]   r.   r   r   r   __getitem__  s    zMonomial.__getitem__c                 C   s   t | jj| j| jfS r   )hashr[   rz   r   r   r\   r   r   r   __hash__  s    zMonomial.__hash__c                 C   s:   | j r$ddd t| j | jD S d| jj| jf S d S )N*c                 S   s   g | ]\}}d ||f qS )z%s**%sr   r   genexpr   r   r   r   "  r   z$Monomial.__str__.<locals>.<listcomp>z%s(%s))r   rk   r$   r   r[   rz   r\   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   )r]   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   r9   r   r]   otherr   r   r   r   __eq__0  s    
zMonomial.__eq__c                 C   s
   | |k S r   r   )r]   r   r   r   r   __ne__:  s    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   r9   r   NotImplementedErrorr   r=   r   r   r   r   __mul__=  s    
zMonomial.__mul__c                 C   sZ   t |tr|j}nt |ttfr&|}ntt| j|}|d urH| |S t| t|d S r   )	r   r   r   r9   r   r   rA   r   r
   )r]   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   rD   r   )r]   r   r+   r   r   r   __pow__X  s    zMonomial.__pow__c                 C   sD   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   r9   r   	TypeErrorr   rF   r   r   r   r   ry   ^  s    
zMonomial.gcdc                 C   sD   t |tr|j}n t |ttfr&|}ntd| | t| j|S )z$Least common multiple of monomials. r   )r   r   r   r9   r   r   r   rH   r   r   r   r   rx   j  s    
zMonomial.lcm)N)N)rz   r{   r|   r}   	__slots__r   r   r   r   r   r   r   r   r   r   r   r   __floordiv__r   ry   rx   r   r   r   r   r      s$   




r   )N)&r}   	itertoolsr   r   textwrapr   Zsympy.core.cacher   Z
sympy.corer   r   r   r	   Zsympy.polys.polyerrorsr
   Zsympy.polys.polyutilsr   r   Zsympy.utilitiesr   Zsympy.utilities.iterablesr   r   r0   r4   r=   rA   r?   rD   rF   rH   rI   rM   rN   rP   rS   rT   r   r   r   r   r   <module>   s2   !}