+
    iH                        R t ^ RIHtHt ^ RIHt ^ RIHt ^ RIH	t	H
t
HtHt ^ RIHt ^ RIHtHt ^ RIHt ^ RIHtHt ]RR
 l4       tR tR tR tR tR tR tR tR tR t R t!R t"R t# ! R R4      t$] ! R R]4      4       t%R	# )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              #    aa"   \        S4      '       Ed~   \        V 4      p\        S4      V8w  d   \        R4      hSf   ^ .V,          oM|\        S4      '       g   \        R4      h\        S4      V8w  d   \        R4      h\        ;QJ d    R S 4       F  '       g   K   RM	  RM! R S 4       4      '       d   \        R4      h\        ;QJ d)    VV3R l\	        V4       4       F  '       g   K   RM	  RM! VV3R l\	        V4       4       4      '       d   \        R	4      h. p\        V SS4       F=  w  rVpTP                  \	        Wg^,           4       Uu. uF  qV,          NK  	  up4       K?  	  \        V!   F  p	\        V	!  x  K  	  R# Sp
V
^ 8  d   \        R
4      hSf   ^ pMS^ 8  d   \        R4      hSpW8  d   R# V '       d   V
^ 8X  d   \        P                  x  R# \        V 4      \        P                  .,           p \        ;QJ d    R V  4       F  '       d   K   RM	  RM! R V  4       4      '       d   \        W
4      pM\        W
R7      p\        4       pW,
          pV FB  p^ pV F  pV^8X  g   K  V^,          pVV8  g   K   K(  	  VP                  \        V!  4       KD  	  T Rj  xL
  R# u upi  L5i)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 matchNzmin_degrees is not a listc              3   *   "   T F	  q^ 8  x  K  	  R# 5i    N ).0is   & u/Users/tonyclaw/.openclaw/workspace/skills/math-calculator/venv/lib/python3.14/site-packages/sympy/polys/monomials.py	<genexpr> itermonomials.<locals>.<genexpr>c   s     .+Qq5+   TFz+min_degrees cannot contain negative numbersc              3   J   <"   T F  pSV,          SV,          8  x  K  	  R # 5iNr   )r   r   max_degreesmin_degreess   & r   r   r   e   s     A1{1~A.s    #z2min_degrees[i] must be <= max_degrees[i] for all izmax_degrees cannot be negativezmin_degrees cannot be negativec              3   8   "   T F  qP                   x  K  	  R # 5ir   )is_commutative)r   variables   & r   r   r   }   s     Ay8&&ys   )repeat)r   len
ValueErroranyrangezipappendr   r   r   Onelistallr   setadd)	variablesr   r   npower_listsvarmin_dmax_dr   powers
max_degree
min_degreeitmonomials_setditemcountr!   s   &ff               r   itermonomialsr<      sN    T ;	N{q :;;#a%K[))899;1$ !>??s.+.sss.+... !NOO3AaA333AaAAAQRR!$Y[!ICeQY0GH0G1Q0GHI "J{+Fv, , !
>=>>JQ !ABB$J"J!O%%KOquug-	3AyA333AyAAA.yEB6B#DE q=QJE5y	 ! !!#t*-  !  G  IF 	!sb   B	KK"K=KK4'K?KK
,A&KAK.K	:KK&K KKc                ^    ^ RI Hp V! W,           4      V! V 4      ,          V! V4      ,          # )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

)	factorial)(sympy.functions.combinatorial.factorialsr>   )VNr>   s   && r   monomial_countrB      s'    < CQUil*Yq\99    c                j    \        \        W4       UUu. uF  w  r#W#,           NK  	  upp4      # u uppi )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`.

tupler'   ABabs   &&  r   monomial_mulrL      s+    " SY0YTQ155Y0110   /
c                    \        W4      p\        ;QJ d    R V 4       F  '       d   K   RM	  RM! R V 4       4      '       d   \        V4      # R# )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   *   "   T F	  q^ 8  x  K  	  R# 5ir   r   )r   cs   & r   r   monomial_div.<locals>.<genexpr>   s     
1a61r   FTN)monomial_ldivr+   rF   )rH   rI   Cs   && r   monomial_divrT      s;    , 	aA
s
1
sss
1
QxrC   c                j    \        \        W4       UUu. uF  w  r#W#,
          NK  	  upp4      # u uppi )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`.

rE   rG   s   &&  r   rR   rR      s+    , SY0YTQ155Y0110rM   c                N    \        V  Uu. uF  q"V,          NK  	  up4      # u upi )z%Return the n-th pow of the monomial. )rF   )rH   r/   rJ   s   && r   monomial_powrW      s#    #1Q33#$$#s   "c           
     p    \        \        W4       UUu. uF  w  r#\        W#4      NK  	  upp4      # u uppi )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`.

)rF   r'   minrG   s   &&  r   monomial_gcdrZ      +    " Q43q94554   2
c           
     p    \        \        W4       UUu. uF  w  r#\        W#4      NK  	  upp4      # u uppi )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`.

)rF   r'   maxrG   s   &&  r   monomial_lcmr_     r[   r\   c                    \         ;QJ d%    R \        W4       4       F  '       d   K   R# 	  R# ! R \        W4       4       4      # )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   .   "   T F  w  rW8*  x  K  	  R # 5ir   r   )r   rJ   rK   s   &  r   r   #monomial_divides.<locals>.<genexpr>.  s     ,)$!qv)s   FT)r+   r'   )rH   rI   s   &&r   monomial_dividesrc   !  s5     3,#a),33,3,3,#a),,,rC   c                     \        V ^ ,          4      pV R,           F+  p\        V4       F  w  r4\        W,          V4      W&   K  	  K-  	  \        V4      # )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)

   NN)r*   	enumerater^   rF   monomsMrA   r   r/   s   *    r   monomial_maxrk   0  K    " 	VAYABZZaLDAqtQ<AD !  8OrC   c                     \        V ^ ,          4      pV R,           F+  p\        V4       F  w  r4\        W,          V4      W&   K  	  K-  	  \        V4      # )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)

re   )r*   rg   rY   rF   rh   s   *    r   monomial_minrn   I  rl   rC   c                    \        V 4      # )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)rj   s   &r   monomial_degrq   b  s     q6MrC   c                    V w  r4Vw  rV\        W54      pVP                  '       d   Ve   WrP                  WF4      3# R# Ve!   WF,          '       g   WrP                  WF4      3# R# )z,Division of two terms in over a ring/field. N)rT   is_Fieldquo)rJ   rK   domaina_lma_lcb_lmb_lcmonoms   &&&     r   term_divr{   q  s^    JDJD$E**T000**T000rC   c                      a a ] tR tRt oRt]V 3R l4       tR tR tR t	]R 4       t
]R 4       t]R	 4       t]R
 4       t]R 4       t]R 4       t]R 4       tRtVtV ;t# )MonomialOpsi  z6Code generator of fast monomial arithmetic functions. c                2   < \         SV `  V 4      pWn        V# r   )super__new__ngens)clsr   obj	__class__s   && r   r   MonomialOps.__new__  s    goc"	
rC   c                    V P                   3# r   )r   selfs   &r   __getnewargs__MonomialOps.__getnewargs__  s    

}rC   c                ,    / p\        W4       W2,          # r   )exec)r   codenamenss   &&& r   _buildMonomialOps._build  s    TxrC   c                `    \        V P                  4       Uu. uF
  q!: V: 2NK  	  up# u upi r   )r&   r   )r   r   r   s   && r   _varsMonomialOps._vars  s*    -24::->@->4#->@@@s   +c                T   R p\        R4      pV P                  R4      pV P                  R4      p\        W44       UUu. uF  w  rVV: RV: 2NK  	  pppVRVRRP                  V4      RRP                  V4      R	RP                  V4      /,          pV P	                  W4      # u uppi )
rL   s        def %(name)s(A, B):
            (%(A)s,) = A
            (%(B)s,) = B
            return (%(AB)s,)
        rJ   rK    + r   rH   , rI   ABr   r   r'   joinr   	r   r   templaterH   rI   rJ   rK   r   r   s	   &        r   mulMonomialOps.mul  s       JJsOJJsO.1!i9idaAq!i964diilC1tUYU^U^_aUbcc{{4&& :   B$c           	         R p\        R4      pV P                  R4      pV Uu. uF  pRV,          NK  	  ppVRVRRP                  V4      RRP                  V4      /,          pV P                  Wa4      # u upi )rW   zZ        def %(name)s(A, k):
            (%(A)s,) = A
            return (%(Ak)s,)
        rJ   z%s*kr   rH   r   Ak)r   r   r   r   )r   r   r   rH   rJ   r   r   s   &      r   powMonomialOps.pow  sz      
 JJsO#$&1avzz1&64diilD$))B-PP{{4&& 's   A5c                V   R p\        R4      pV P                  R4      pV P                  R4      p\        W44       UUu. uF  w  rVV: RV: R2NK  	  pppVRVRRP                  V4      R	RP                  V4      R
RP                  V4      /,          pV P	                  W4      # u uppi )monomial_mulpowzw        def %(name)s(A, B, k):
            (%(A)s,) = A
            (%(B)s,) = B
            return (%(ABk)s,)
        rJ   rK   r   z*kr   rH   r   rI   ABkr   )	r   r   r   rH   rI   rJ   rK   r   r   s	   &        r   mulpowMonomialOps.mulpow  s        JJsOJJsO14Q<q!$<64diilC1uVZV_V_`cVdee{{4&& =s   B%c                T   R p\        R4      pV P                  R4      pV P                  R4      p\        W44       UUu. uF  w  rVV: RV: 2NK  	  pppVRVRRP                  V4      RRP                  V4      R	RP                  V4      /,          pV P	                  W4      # u uppi )
rR   r   rJ   rK   z - r   rH   r   rI   r   r   r   s	   &        r   ldivMonomialOps.ldiv  s       JJsOJJsO.1!i9idaAq!i964diilC1tUYU^U^_aUbcc{{4&& :r   c                   R p\        R4      pV P                  R4      pV P                  R4      p\        V P                  4       Uu. uF  pRRV/,          NK  	  ppV P                  R4      pVRVRR	P	                  V4      R
R	P	                  V4      RRP	                  V4      RR	P	                  V4      /,          pV P                  W4      # u upi )rT   z        def %(name)s(A, B):
            (%(A)s,) = A
            (%(B)s,) = B
            %(RAB)s
            return (%(R)s,)
        rJ   rK   z7r%(i)s = a%(i)s - b%(i)s
    if r%(i)s < 0: return Noner   rr   rH   r   rI   RABz
    R)r   r   r&   r   r   r   )	r   r   r   rH   rI   r   r   r   r   s	   &        r   divMonomialOps.div  s       JJsOJJsO_deieoeo_pr_pZ[JcSTXUU_prJJsO64diilC1uV^VcVcdgVhjmosoxoxyzo{||{{4&& ss   Cc                d   R p\        R4      pV P                  R4      pV P                  R4      p\        W44       UUu. uF  w  rVV: RV: RV: RV: 2NK  	  pppVRVRR	P                  V4      R
R	P                  V4      RR	P                  V4      /,          pV P	                  W4      # u uppi )r_   r   rJ   rK    if z >=  else r   rH   r   rI   r   r   r   s	   &        r   lcmMonomialOps.lcm         JJsOJJsOCFq9N9411aA69N64diilC1tUYU^U^_aUbcc{{4&& O   B,c                d   R p\        R4      pV P                  R4      pV P                  R4      p\        W44       UUu. uF  w  rVV: RV: RV: RV: 2NK  	  pppVRVRR	P                  V4      R
R	P                  V4      RR	P                  V4      /,          pV P	                  W4      # u uppi )rZ   r   rJ   rK   r   z <= r   r   rH   r   rI   r   r   r   s	   &        r   gcdMonomialOps.gcd  r   r   r   )__name__
__module____qualname____firstlineno____doc__r   r   r   r   r   r   r   r   r   r   r   r   __static_attributes____classdictcell____classcell__)r   __classdict__s   @@r   r}   r}     s     @ 

A ' ' 
' 
' ' ' ' ' ' '  ' ' ' ' 'rC   r}   c                      a  ] tR tRt o RtRtRR ltRR ltR tR t	R t
R	 tR
 tR tR tR tR tR t]tR tR tR tRtV tR# )Monomiali   z9Class representing a monomial, i.e. a product of powers. Nc                   \        V4      '       g   \        \        V4      VR 7      w  r2\        V4      ^8X  dG   \	        VP                  4       4      ^ ,          ^8X  d"   \	        VP                  4       4      ^ ,          pM\        RP                  V4      4      h\        \        \        V4      4      V n        W n        R# ))genszExpected a monomial got {}N)r   r   r	   r#   r*   valueskeysr$   formatrF   mapint	exponentsr   )r   rz   r   reps   &&& r   __init__Monomial.__init__  s    &wu~DAIC3x1}cjjl!3A!6!!;SXXZ(+ !=!D!DU!KLLs3/	rC   c                L    T P                  Y;'       g    V P                  4      # r   )r   r   )r   r   r   s   &&&r   rebuildMonomial.rebuild  s    ~~i):):;;rC   c                ,    \        V P                  4      # r   )r#   r   r   s   &r   __len__Monomial.__len__  s    4>>""rC   c                ,    \        V P                  4      # r   )iterr   r   s   &r   __iter__Monomial.__iter__  s    DNN##rC   c                (    V P                   V,          # r   )r   )r   r:   s   &&r   __getitem__Monomial.__getitem__  s    ~~d##rC   c                n    \        V P                  P                  V P                  V P                  34      # r   )hashr   r   r   r   r   s   &r   __hash__Monomial.__hash__  s&    T^^,,dnndiiHIIrC   c           	        V P                   '       dK   R P                  \        V P                   V P                  4       UUu. uF  w  rV: RV: 2NK  	  upp4      # V P                  P
                  : RV P                  : R2# u uppi )*z**())r   r   r'   r   r   r   )r   genexps   &  r   __str__Monomial.__str__   sc    99988C		SWSaSaDbdDb#s3Dbdee#~~66GG es   B
c                    T;'       g    V P                   pV'       g   \        RV ,          4      h\        \        WP                  4       UUu. uF  w  r#W#,          NK  	  upp!  # u uppi )z3Convert a monomial instance to a SymPy expression. z5Cannot convert %s to an expression without generators)r   r$   r   r'   r   )r   r   r   r   s   &*  r   as_exprMonomial.as_expr&  s]      tyyG$NP P s4/HJ/H83chh/HJKKJs   A'
c                    \        V\        4      '       d   VP                  pM!\        V\        \        34      '       d   TpMR # V P                  V8H  # )F)
isinstancer   r   rF   r   r   otherr   s   && r   __eq__Monomial.__eq__0  s@    eX&&Iu~..I~~**rC   c                    W8X  * # r   r   )r   r   s   &&r   __ne__Monomial.__ne__:  s      rC   c                    \        V\        4      '       d   VP                  pM%\        V\        \        34      '       d   TpM\
        hV P                  \        V P                  V4      4      # r   )r   r   r   rF   r   NotImplementedErrorr   rL   r   s   && r   __mul__Monomial.__mul__=  sN    eX&&Iu~..I%%||LCDDrC   c                   \        V\        4      '       d   VP                  pM%\        V\        \        34      '       d   TpM\
        h\        V P                  V4      pVe   V P                  V4      # \        V \        V4      4      hr   )	r   r   r   rF   r   r   rT   r   r
   )r   r   r   results   &&  r   __truediv__Monomial.__truediv__G  sj    eX&&Iu~..I%%dnni8<<''%dHUO<<rC   c                    \        V4      pV^ 8  d   \        RV,          4      hV P                  \        V P                  V4      4      # )r   z'a non-negative integer expected, got %s)r   r$   r   rW   r   )r   r   r/   s   && r   __pow__Monomial.__pow__X  s=    Jq5FNOO||L;<<rC   c                    \        V\        4      '       d   VP                  pM1\        V\        \        34      '       d   TpM\        RV,          4      hV P                  \        V P                  V4      4      # )z&Greatest common divisor of monomials. .an instance of Monomial class expected, got %s)r   r   r   rF   r   	TypeErrorr   rZ   r   s   && r   r   Monomial.gcd^  a    eX&&Iu~..I@5HJ J ||LCDDrC   c                    \        V\        4      '       d   VP                  pM1\        V\        \        34      '       d   TpM\        RV,          4      hV P                  \        V P                  V4      4      # )z$Least common multiple of monomials. r  )r   r   r   rF   r   r  r   r_   r   s   && r   r   Monomial.lcmj  r  rC   )r   r   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   )r   s   @r   r   r      sk     C%I	<#$$JHL+!E= L=
E
E 
ErC   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   r<   rB   rL   rT   rR   rW   rZ   r_   rc   rk   rn   rq   r{   r}   r   r   rC   r   <module>r     s    F =  $ - - 6 D " ;}! }!~:B2&:20%6&6&-22${' {'z sE! sE sErC   