
    ЦiI                        S r SSKJrJr  SSKJr  SSKJrJrJ	r	J
r
  SSKJr  SSKJrJr  SSKJr  SSKJrJr  \SS
 j5       rS rS rS rS rS rS rS rS rS rS rS r S r! " S S5      r"\ " S S\5      5       r#g	)z@Tools and arithmetics for monomials of distributed polynomials.     )combinations_with_replacementproduct)dedent)MulSTuplesympify)ExactQuotientFailed)PicklableWithSlotsdict_from_expr)public)is_sequenceiterableNc              #     ^^#    [        U 5      n[        T5      (       a~  [        T5      U:w  a  [        S5      eTc  S/U-  mOW[        T5      (       d  [        S5      e[        T5      U:w  a  [        S5      e[        S T 5       5      (       a  [        S5      eSnO.TnUS:  a  [        S5      eTc  SnOTS:  a  [        S	5      eTnS
nU(       Ga{  WW:  a  gU (       a  US:X  a  [        R
                  v   g[        U 5      [        R
                  /-   n [        S U  5       5      (       a  / n[        X5       Hl  n[        R                  U S5      n	U H  n
U
S:w  d  M  X==   S-  ss'   M     [        U	R                  5       5      U:  d  MU  UR                  [        U6 5        Mn     [        U5       Sh  vN   g/ n[!        XS9 Hl  n[        R                  U S5      n	U H  n
U
S:w  d  M  X==   S-  ss'   M     [        U	R                  5       5      U:  d  MU  UR                  [        U6 5        Mn     [        U5       Sh  vN   g[        UU4S j[#        U5       5       5      (       a  [        S5      e/ n[%        U TT5       H7  u  pnUR                  [#        XS-   5       Vs/ s H  nUU-  PM
     sn5        M9     [!        U6  H  n	[        U	6 v   M     g GN( Ns  snf 7f)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              3   *   #    U  H	  oS :  v   M     g7fr   N ).0is     T/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/sympy/polys/monomials.py	<genexpr> itermonomials.<locals>.<genexpr>b   s     .+Qq5+   z+min_degrees cannot contain negative numbersFzmax_degrees cannot be negativezmin_degrees cannot be negativeTc              3   8   #    U  H  oR                   v   M     g 7fN)is_commutative)r   variables     r   r   r   x   s     Ay8&&ys      )repeatc              3   :   >#    U  H  nTU   TU   :  v   M     g 7fr   r   )r   r   max_degreesmin_degreess     r   r   r      s     A1{1~A.s   z2min_degrees[i] must be <= max_degrees[i] for all i)lenr   
ValueErroranyr   Onelistallr   dictfromkeyssumvaluesappendr   setr   rangezip)	variablesr!   r"   ntotal_degree
max_degree
min_degreemonomials_list_commitempowersr   monomials_list_non_commpower_listsvarmin_dmax_dr   s    ``              r   itermonomialsr>      s    T 	IA;{q :;;#a%K[))899;1$ !>??.+... !NOO 
>=>>JQ !ABB$J
"J!O%%KOquug-	AyAAA"$5iLy!4 $H1}(A-( !% v}}':5'..sDz: M .///&(#	=y!4 $H1}(A-( !% v}}':5+223:> > 2333AaAAAQRR!$Y[!ICeQY0GH0G1Q0GHI "J{+Fv, ,# 0 4  IsO   EK,"+K,)K,:K";2K,1+K, )K,	K%
A$K,.K'
=&K,%K,'K,c                 F    SSK Jn  U" X-   5      U" U 5      -  U" U5      -  $ )a  
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.factorialsr@   )VNr@   s      r   monomial_countrD      s'    < CQUil*Yq\99    c                 d    [        [        X5       VVs/ s H	  u  p#X#-   PM     snn5      $ s  snnf )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`.

tupler0   ABabs       r   monomial_mulrN      s)    " SY0YTQ15Y0110   ,
c                 ^    [        X5      n[        S U 5       5      (       a  [        U5      $ g)al  
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              3   *   #    U  H	  oS :  v   M     g7fr   r   )r   cs     r   r   monomial_div.<locals>.<genexpr>   s     
1a61r   N)monomial_ldivr(   rH   )rJ   rK   Cs      r   monomial_divrV      s+    , 	aA

1
QxrE   c                 d    [        [        X5       VVs/ s H	  u  p#X#-
  PM     snn5      $ s  snnf )aU  
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`.

rG   rI   s       r   rT   rT      s)    , SY0YTQ15Y0110rO   c                 H    [        U  Vs/ s H  o"U-  PM	     sn5      $ s  snf )z%Return the n-th pow of the monomial. )rH   )rJ   r2   rL   s      r   monomial_powrY      s!    #1Q3#$$#s   c           
      r    [        [        X5       VVs/ s H  u  p#[        X#5      PM     snn5      $ s  snnf )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`.

)rH   r0   minrI   s       r   monomial_gcdr\     +    " Q43q94554   3
c           
      r    [        [        X5       VVs/ s H  u  p#[        X#5      PM     snn5      $ s  snnf )a  
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`.

)rH   r0   maxrI   s       r   monomial_lcmra     r]   r^   c                 8    [        S [        X5       5       5      $ )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              3   .   #    U  H  u  pX:*  v   M     g 7fr   r   )r   rL   rM   s      r   r   #monomial_divides.<locals>.<genexpr>5  s     ,)$!qv)s   )r(   r0   )rJ   rK   s     r   monomial_dividesre   (  s     ,#a),,,rE   c                      [        U S   5      nU SS  H'  n[        U5       H  u  p4[        X   U5      X'   M     M)     [        U5      $ )ai  
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'   	enumerater`   rH   monomsMrC   r   r2   s        r   monomial_maxrk   7  K    " 	VAYAABZaLDAqtQ<AD !  8OrE   c                      [        U S   5      nU SS  H'  n[        U5       H  u  p4[        X   U5      X'   M     M)     [        U5      $ )ai  
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'   rg   r[   rH   rh   s        r   monomial_minrn   P  rl   rE   c                     [        U 5      $ )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
)r+   )rj   s    r   monomial_degrp   i  s     q6MrE   c                     U u  p4Uu  pV[        X55      nUR                  (       a  Ub  XrR                  XF5      4$ gUb  XF-  (       d  XrR                  XF5      4$ g)z,Division of two terms in over a ring/field. N)rV   is_Fieldquo)rL   rM   domaina_lma_lcb_lmb_lcmonoms           r   term_divrz   x  sZ    JDJD$E**T000**T000rE   c                   T    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS rS rSrg)MonomialOpsi  z6Code generator of fast monomial arithmetic functions. c                     Xl         g r   ngens)selfr   s     r   __init__MonomialOps.__init__  s    
rE   c                 $    0 n[        X5        X2   $ r   )exec)r   codenamenss       r   _buildMonomialOps._build  s    TxrE   c                 b    [        U R                  5       Vs/ s H
  o!< U< 3PM     sn$ s  snf r   )r/   r   )r   r   r   s      r   _varsMonomialOps._vars  s*    -24::->@->4#->@@@s   ,c                 F   Sn[        S5      nU R                  S5      nU R                  S5      n[        X45       VVs/ s H  u  pVU< SU< 3PM     nnnX!SR                  U5      SR                  U5      SR                  U5      S.-  nU R	                  X5      $ s  snnf )NrN   s        def %(name)s(A, B):
            (%(A)s,) = A
            (%(B)s,) = B
            return (%(AB)s,)
        rL   rM    + , r   rJ   rK   ABr   r   r0   joinr   	r   r   templaterJ   rK   rL   rM   r   r   s	            r   mulMonomialOps.mul  s       JJsOJJsO.1!i9idaAq!i9diil1UYU^U^_aUbcc{{4&& :   Bc                     Sn[        S5      nU R                  S5      nU Vs/ s H  nSU-  PM
     nnX!SR                  U5      SR                  U5      S.-  nU R                  Xa5      $ s  snf )NrY   zZ        def %(name)s(A, k):
            (%(A)s,) = A
            return (%(Ak)s,)
        rL   z%s*kr   )r   rJ   Ak)r   r   r   r   )r   r   r   rJ   rL   r   r   s          r   powMonomialOps.pow  sp      
 JJsO#$&1avz1&diil$))B-PP{{4&& 's   A+c                 H   Sn[        S5      nU R                  S5      nU R                  S5      n[        X45       VVs/ s H  u  pVU< SU< S3PM     nnnX!SR                  U5      SR                  U5      SR                  U5      S.-  nU R	                  X5      $ s  snnf )	Nmonomial_mulpowzw        def %(name)s(A, B, k):
            (%(A)s,) = A
            (%(B)s,) = B
            return (%(ABk)s,)
        rL   rM   r   z*kr   )r   rJ   rK   ABkr   )	r   r   r   rJ   rK   rL   rM   r   r   s	            r   mulpowMonomialOps.mulpow  s        JJsOJJsO14Q<q!$<diil1VZV_V_`cVdee{{4&& =s   Bc                 F   Sn[        S5      nU R                  S5      nU R                  S5      n[        X45       VVs/ s H  u  pVU< SU< 3PM     nnnX!SR                  U5      SR                  U5      SR                  U5      S.-  nU R	                  X5      $ s  snnf )NrT   r   rL   rM   z - r   r   r   r   s	            r   ldivMonomialOps.ldiv  s       JJsOJJsO.1!i9idaAq!i9diil1UYU^U^_aUbcc{{4&& :r   c                    Sn[        S5      nU R                  S5      nU R                  S5      n[        U R                  5       Vs/ s H
  nSSU0-  PM     nnU R                  S5      nX!SR	                  U5      SR	                  U5      S	R	                  U5      SR	                  U5      S
.-  nU R                  X5      $ s  snf )NrV   z        def %(name)s(A, B):
            (%(A)s,) = A
            (%(B)s,) = B
            %(RAB)s
            return (%(R)s,)
        rL   rM   z7r%(i)s = a%(i)s - b%(i)s
    if r%(i)s < 0: return Noner   rr   z
    )r   rJ   rK   RABR)r   r   r/   r   r   r   )	r   r   r   rJ   rK   r   r   r   r   s	            r   divMonomialOps.div  s       JJsOJJsO_deieoeo_pr_pZ[JcSTXU_prJJsOdiil1V^VcVcdgVhosoxoxyzo{||{{4&& ss   Cc                 V   Sn[        S5      nU R                  S5      nU R                  S5      n[        X45       VVs/ s H  u  pVU< SU< SU< SU< 3PM     nnnX!SR                  U5      SR                  U5      SR                  U5      S	.-  nU R	                  X5      $ s  snnf )
Nra   r   rL   rM    if z >=  else r   r   r   r   s	            r   lcmMonomialOps.lcm         JJsOJJsOCFq9N9411aA69Ndiil1UYU^U^_aUbcc{{4&& O   B%c                 V   Sn[        S5      nU R                  S5      nU R                  S5      n[        X45       VVs/ s H  u  pVU< SU< SU< SU< 3PM     nnnX!SR                  U5      SR                  U5      SR                  U5      S	.-  nU R	                  X5      $ s  snnf )
Nr\   r   rL   rM   r   z <= r   r   r   r   r   s	            r   gcdMonomialOps.gcd  r   r   r~   N)__name__
__module____qualname____firstlineno____doc__r   r   r   r   r   r   r   r   r   r   __static_attributes__r   rE   r   r|   r|     s8    @
A'
'''' ''rE   r|   c                       \ rS rSrSrSrSS jrSS jrS rS r	S	 r
S
 rS rS rS rS rS rS r\rS rS rS rSrg)Monomiali  z9Class representing a monomial, i.e. a product of powers. )	exponentsgensNc                 b   [        U5      (       d{  [        [        U5      US9u  p2[        U5      S:X  a=  [	        UR                  5       5      S   S:X  a  [	        UR                  5       5      S   nO[        SR                  U5      5      e[        [        [        U5      5      U l        X l        g )N)r   r   r   zExpected a monomial got {})r   r   r	   r#   r'   r,   keysr$   formatrH   mapintr   r   )r   ry   r   reps       r   r   Monomial.__init__   s    &wu~DAIC3x1}cjjl!3A!6!!;SXXZ(+ !=!D!DU!KLLs3/	rE   c                 J    U R                  X=(       d    U R                  5      $ r   )	__class__r   )r   r   r   s      r   rebuildMonomial.rebuild  s    ~~i):;;rE   c                 ,    [        U R                  5      $ r   )r#   r   r   s    r   __len__Monomial.__len__  s    4>>""rE   c                 ,    [        U R                  5      $ r   )iterr   r   s    r   __iter__Monomial.__iter__  s    DNN##rE   c                      U R                   U   $ r   )r   )r   r7   s     r   __getitem__Monomial.__getitem__  s    ~~d##rE   c                 n    [        U R                  R                  U R                  U R                  45      $ r   )hashr   r   r   r   r   s    r   __hash__Monomial.__hash__  s&    T^^,,dnndiiHIIrE   c           	         U R                   (       aK  SR                  [        U R                   U R                  5       VVs/ s H  u  pU< SU< 3PM     snn5      $ U R                  R
                  < SU R                  < S3$ s  snnf )N*z**())r   r   r0   r   r   r   )r   genexps      r   __str__Monomial.__str__  sa    9988C		SWSaSaDbdDb#s3Dbdee#~~66GG es    B
c                     U=(       d    U R                   nU(       d  [        SU -  5      e[        [        XR                  5       VVs/ s H	  u  p#X#-  PM     snn6 $ s  snnf )z3Convert a monomial instance to a SymPy expression. z5Cannot convert %s to an expression without generators)r   r$   r   r0   r   )r   r   r   r   s       r   as_exprMonomial.as_expr   sX     tyyG$NP P s4/HJ/H83ch/HJKKJs   A
c                     [        U[        5      (       a  UR                  nO[        U[        [        45      (       a  UnOgU R                  U:H  $ )NF)
isinstancer   r   rH   r   r   otherr   s      r   __eq__Monomial.__eq__*  s@    eX&&Iu~..I~~**rE   c                     X:X  + $ r   r   )r   r   s     r   __ne__Monomial.__ne__4  s      rE   c                     [        U[        5      (       a  UR                  nO$[        U[        [        45      (       a  UnO[
        eU R                  [        U R                  U5      5      $ r   )r   r   r   rH   r   NotImplementedErrorr   rN   r   s      r   __mul__Monomial.__mul__7  sN    eX&&Iu~..I%%||LCDDrE   c                    [        U[        5      (       a  UR                  nO$[        U[        [        45      (       a  UnO[
        e[        U R                  U5      nUb  U R                  U5      $ [        U [        U5      5      er   )	r   r   r   rH   r   r   rV   r   r
   )r   r   r   results       r   __truediv__Monomial.__truediv__A  sj    eX&&Iu~..I%%dnni8<<''%dHUO<<rE   c                     [        U5      nUS:  a  [        SU-  5      eU R                  [        U R                  U5      5      $ )Nr   z'a non-negative integer expected, got %s)r   r$   r   rY   r   )r   r   r2   s      r   __pow__Monomial.__pow__R  s=    Jq5FNOO||L;<<rE   c                     [        U[        5      (       a  UR                  nO,[        U[        [        45      (       a  UnO[        SU-  5      eU R                  [        U R                  U5      5      $ )z&Greatest common divisor of monomials. .an instance of Monomial class expected, got %s)r   r   r   rH   r   	TypeErrorr   r\   r   s      r   r   Monomial.gcdX  a    eX&&Iu~..I@5HJ J ||LCDDrE   c                     [        U[        5      (       a  UR                  nO,[        U[        [        45      (       a  UnO[        SU-  5      eU R                  [        U R                  U5      5      $ )z$Least common multiple of monomials. r   )r   r   r   rH   r   r   r   ra   r   s      r   r   Monomial.lcmd  r   rE   r   )r   r   r   r   r   	__slots__r   r   r   r   r   r   r   r   r   r   r   r   __floordiv__r   r   r   r   r   rE   r   r   r     sb    C%I	<#$$JHL+!E= L=
E
ErE   r   r   )$r   	itertoolsr   r   textwrapr   
sympy.corer   r   r   r	   sympy.polys.polyerrorsr
   sympy.polys.polyutilsr   r   sympy.utilitiesr   sympy.utilities.iterablesr   r   r>   rD   rN   rV   rT   rY   r\   ra   re   rk   rn   rp   rz   r|   r   r   rE   r   <module>r	     s    F =  - - 6 D " ;E EN:B2&:20%6&6&-22$n' n'` sE! sE sErE   