
    wgC                       U d Z ddlmZ ddlmZ ddlmZmZmZm	Z	m
Z
mZmZmZ ddlmZmZ ddlmZ ddlmZmZ ddlmZ dd	lZi d
dddddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5	Zd6Z ej6                  d7ej8                        ZdRd8Zd9 Zd: Z d; Z!d< Z"eD  cg c]
  }  e#|        c} Z$ed=d> D cg c]
  } e%|       c}Z&d? Z'd@ Z(dA Z)dB Z*dC Z+dD Z,dE Z-dF Z.dG Z/dH Z0e,Z1e.Z2e0Z3dI Z4 G dJ dK      Z5 G dL dM      Z6dNe7dO<   edPk(  rdd	l8Z8e8jr                  e8jt                  fZ;y	ddQl<m=Z= d	Z8e=fZ;y	c c} w c c}w )Sz6Useful utilities for higher level polynomial classes.     )annotations)GROUND_TYPES)SAddMulPowEqExpr
expand_mulexpand_multinomial)decompose_powerdecompose_power_rat)_illegal)PolynomialErrorGeneratorsError)build_optionsNai-  bi.  ci/  di0  ei1  fi2  gi3  hi4  ii5  ji6  ki7  li8  mi9  ni:  oi;  p   q                     |   }   ~   )	rstuvwxyzi  z^(.*?)(\d*)$c           
     J   t        d | D              st        | D cg c]@  }|j                         D cg c]$  }|j                  d      j                         d   & c}B }}}t	        |       dkD  rt        d |D              rt        d      |D cg c]  \  }}|rdnd||f }}}t        t        ||             }|r;g }g }|D ].  \  \  }}}}|r|j                  |       |j                  |       0 ||fS t        | \  }} t        |       S c c}w c c}}w c c}}w )a  Sort the numerical roots putting the real roots first, then sorting
    according to real and imaginary parts. If ``separated`` is True, then
    the real and imaginary roots will be returned in two lists, respectively.

    This routine tries to avoid issue 6137 by separating the roots into real
    and imaginary parts before evaluation. In addition, the sorting will raise
    an error if any computation cannot be done with precision.
    c              3  4   K   | ]  }|j                     y wN	is_number).0r/   s     Z/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/polys/polyutils.py	<genexpr>z_nsort.<locals>.<genexpr>(   s     *qq{{*      r      c              3  H   K   | ]  }|D ]  }|j                   d k(     yw)rB   N)_prec)r=   r   r   s      r>   r?   z_nsort.<locals>.<genexpr>.   s$     CqCAagglClCs    "z%could not compute root with precision)
allNotImplementedErroras_real_imagr    lenanysortedzipappendlist)roots	separatedr/   r   keyim_r3   s           r>   _nsortrS      s    *E**!! JO
OAann.>?ACCF!!$?
OC
O
5zA~#CCCC!"IJJ-0
1TQA1a 
1C
1
S%
!C  	MJRA		
 !tCyHAu;% @
O
 2s   D)DDDDc                   t        |      }i dc|4i |j                  ct        |j                        D ]  \  }}|dz   |<    fd}	 t	        | |      } t        |       S # t
        $ r Y t        |       S w xY w)z1Sort generators in a reasonably intelligent way. NrB   c                f   t        |       } "	 t               j                  |       z   | dfS t        j                  |       j                         \  }}|rt        |      }nd}	 |   ||fS # t        $ r Y Jw xY w# t        $ r Y nw xY w	 t        |   ||fS # t        $ r Y nw xY wt        ||fS )Nr   )strrH   index
ValueError_re_genmatchgroupsintKeyError_gens_order
_max_order)gennamerW   
gens_orderwrts      r>   	order_keyz_sort_gens.<locals>.order_keyM   s    #h?S	CIIcN2C;; mmC(//1eJEE	%tU33    			%tU33 		 D%((s5    A0 (A? 0	A<;A<?	B
BB 	B'&B'rP   )r   rc   	enumeratesortrJ   	TypeErrortuple)gensargsoptr   r`   rd   rb   rc   s         @@r>   
_sort_gensrm   A   s    

C$OJ
cgg
C) 	$FAs!eJsO	$)8d	* ;  ;s   A( (	A>=A>c                   t        |       } t        |      }| |k(  rt        |       S g g d}}}| D ]  }||v s|j                  |        t        |      D ]  \  }}||v s||   |dz   c||<   } |D ]m  }| j	                  |      }|j                  | d|        | |dz   d } |j	                  |      }|j                  |d|        ||dz   d }|j                  |       o |j                  |        |j                  |       t        |      S )z2Unify generators in a reasonably intelligent way. r   rB   N)rM   ri   rL   rf   rW   extend)f_gensg_gensrj   commonr   r`   r   s          r>   _unify_gensrs   q   s,   &\F&\FV}"a!&D &=MM# F# ,3&=!!9a!eLF1Iq,  LLF2AJALLF2AJAC 	KKKK;    c                n    t        |       dk(  rt        | d   d      rt        | d         S t        |       S )z8Support for passing generators as `*gens` and `[gens]`. rB   r   __iter__)rH   hasattrri   )rj   s    r>   _analyze_gensrx      s2    
4yA~'$q':6T!W~T{rt   c                z    fdfd}fd}|j                  dd      rt        | |      S t        | |      S )z9Sort low-level factors in increasing 'complexity' order. c                    t        | t              rt        |       S t        | t              r| D cg c]
  } |       c}S | S c c}w r:   )
isinstance	_GF_typesr\   rM   )factorr   rd   s     r>   rd   z _sort_factors.<locals>.order_key   s@    fi(v;%*01QIaL11M 2s   Ac                6    | \  }}t        |      | |      fS r:   rH   )r}   r   r    rd   s      r>   order_if_multiple_keyz,_sort_factors.<locals>.order_if_multiple_key   s!    AA9Q<((rt   c                *    t        |        |       fS r:   r   )r   rd   s    r>   order_no_multiple_keyz,_sort_factors.<locals>.order_no_multiple_key   s    A	!%%rt   multipleTre   )getrJ   )factorsrk   r   r   rd   s       @r>   _sort_factorsr      s>    )& xx
D!g#899g#899rt   rB      c                v    t        |       t        v s| t        v ryt        | t              rt	        |       | k7  ryy)zBDo not treat NaN and infinities as valid polynomial coefficients. TN)typeillegal_typesfinfr{   floatexprs    r>   _not_a_coeffr      s2    Dz]"ddl$5;$#6
rt   c                |   t        |j                        i }}t        |j                        D ]
  \  }}|||<    g }| D ]%  }i }|j                  r|j                  |j
                  z
  }t        j                  |      D ]  }	g dg|z  }}
t        j                  |	      D ]  }t        |      s|j                  r|
j                  |       ,	 |j                  du r2t        |      \  }}|dk  r-| t        |t        j                          }}nt#        |      \  }}||||   <    t+        |      }||v r||xx   t        |
 z  cc<   t        |
 ||<    |j                  |       ( ||j                  fS # t$        $ r=  |j&                  |j                   s|
j                  |       nt)        d|z        Y w xY w)z@Transform expressions into a multinomial form given generators. r   Fz0%s contains an element of the set of generators.)rH   rj   rf   is_Equalitylhsrhsr   	make_argsr   r   	is_NumberrL   seriesr   r   r   Oner   r]   has_freer   ri   )exprsrl   r   indicesr   r   polysr   polytermcoeffmonomr}   baseexps                  r>    _parallel_dict_from_expr_if_gensr      s   SXXwA#((# 1
 E %88dhh&DMM$' 	*Ds1u5E--- U#F+0@0@LL(U::.(7(?ID#"Qw-0D#dQUUF2CT(;F(CID#/2gdm,U* %LE}UsE{*!5kU;	*> 	TK%N #((?! $ U.v9!LL0"1 3KMS3T #U U 1Us   AE55AF;	:F;	c                h   j                   fd}n'j                  du rd }nj                  durd }nd }t               g }}| D ].  }g }|j                  r|j
                  |j                  z
  }t        j                  |      D ]  }g i }	}t        j                  |      D ]  }
t        |
      s&|
j                  s ||
      r|j                  |
       4j                  du r2t        |
      \  }}|dk  r-| t        |t         j"                         }}nt%        |
      \  }}|	j'                  |d      |z   |	|<   |j)                  |        |j                  ||	f        |j                  |       1 t+        |      }t-        |      i }}t/        |      D ]
  \  }}|||<    g }|D ]s  }i }|D ]Y  \  }}dg|z  }|j1                         D ]  \  }}||||   <    t3        |      }||v r||xx   t        | z  cc<   Ot        | ||<   [ |j                  |       u |t3        |      fS )	zITransform expressions into a multinomial form and figure out generators. c                     | j                   v S r:   )domain)r}   rl   s    r>   	_is_coeffz3_parallel_dict_from_expr_no_gens.<locals>._is_coeff   s    SZZ''rt   Tc                    | j                   S r:   )is_algebraicr}   s    r>   r   z3_parallel_dict_from_expr_no_gens.<locals>._is_coeff   s    &&&rt   Fc                &    | t         j                  u S r:   )r   ImaginaryUnitr   s    r>   r   z3_parallel_dict_from_expr_no_gens.<locals>._is_coeff  s    Q__,,rt   c                    | j                   S r:   r;   r   s    r>   r   z3_parallel_dict_from_expr_no_gens.<locals>._is_coeff  s    ###rt   r   )rl   )r   	extensiongreedysetr   r   r   r   r   r   r   r   rL   r   r   r   r   r   r   
setdefaultaddrm   rH   rf   itemsri   )r   rl   r   rj   reprsr   termsr   r   elementsr}   r   r   r   r   r   r   r   r   r   s    `                  r>    _parallel_dict_from_expr_no_gensr      sJ   
zz	(	$		'	5	 	-	$ %%D 88dhh&DMM$' 	,D "8E--- ##F+1A1AYvEVLL(zzU*$3F$;	c7),c$.?C$7$?	c%-%8%8q%AC%GHTNHHTN# LL%*+%	,( 	U58 d$DTBwA$ 1
 E   	*KE4CEE!ZZ\ +	c'*gdm$+ %LE}UsE{*!5kU	* 	T!$ %+rt   c                .    t        | f|      \  \  }}||fS )zBTransform an expression into a multinomial form given generators. )r   r   rl   r   rj   s       r>   _dict_from_expr_if_gensr   C       4dWcBMGTT:rt   c                .    t        | f|      \  \  }}||fS )zKTransform an expression into a multinomial form and figure out generators. )r   r   s       r>   _dict_from_expr_no_gensr   I  r   rt   c                N    t        | t        |            \  }}||j                  fS )/Transform expressions into a multinomial form. )_parallel_dict_from_exprr   rj   )r   rk   repsrl   s       r>   parallel_dict_from_exprr   O  s%    (d0CDID#>rt   c                   |j                   dur| D cg c]  }|j                          } }t        d | D              rt        d      |j                  rt	        | |      \  }}nt        | |      \  }}||j                  d|i      fS c c}w )r   Fc              3  8   K   | ]  }|j                   d u   yw)FN)is_commutative)r=   r   s     r>   r?   z+_parallel_dict_from_expr.<locals>.<genexpr>Z  s     
:D4%'
:s   -non-commutative expressions are not supportedrj   )expandrI   r   rj   r   r   clone)r   rl   r   r   rj   s        r>   r   r   U  s    
zz,13D$++-33

:E
::MNN
xx5eSA
d5eSA
dFD>*** 4s   B	c                N    t        | t        |            \  }}||j                  fS )1Transform an expression into a multinomial form. )_dict_from_exprr   rj   )r   rk   reprl   s       r>   dict_from_exprr   e  s%    t]4%89HC=rt   c                   | j                   du rt        d      d |j                  durt        | t        t
        f      st        d      | j                         } t        fdt        j                  |       D              r3t        |       } t        fdt        j                  |       D              r3t        d t        j                  |       D              r1t        |       } t        d t        j                  |       D              r1|j                  rt        | |      \  }}nt        | |      \  }}||j                  d|i      fS )r   Fr   c                    | j                   xrF | j                  j                  xr. | j                  j                  xr | j                  j
                  S r:   )is_Powr   is_positive
is_Integerr   is_Addr   s    r>   _is_expandable_powz+_dict_from_expr.<locals>._is_expandable_powp  sB     % 4 4 %9L9L %II$$	&rt   zexpression must be of type Exprc              3     K   | ]:  } |      xs, |j                   xr t        fd |j                  D               < yw)c              3  .   K   | ]  } |        y wr:    )r=   r   r   s     r>   r?   z,_dict_from_expr.<locals>.<genexpr>.<genexpr>z  s     6!"1%6s   Nis_MulrI   rk   )r=   r   r   s     r>   r?   z"_dict_from_expr.<locals>.<genexpr>y  sE      %;< %Q' 7188 ,76qvv667 %s   A Ac              3  p   K   | ].  }|j                   xr t        d  |j                  D               0 yw)c              3  4   K   | ]  }|j                     y wr:   )r   )r=   r   s     r>   r?   z,_dict_from_expr.<locals>.<genexpr>.<genexpr>~  s     "<188"<r@   Nr   )r=   r   s     r>   r?   z"_dict_from_expr.<locals>.<genexpr>~  s+     Z!((<s"<QVV"<<<Zs   46rj   )r   r   r   r{   r
   r	   rI   r   r   r   r   rj   r   r   r   )r   rl   r   rj   r   s       @r>   r   r   k  s   e#MNN& zz$r
+!"CDD{{} %d#% % &d+D	  %d#% %
 ZcmmTXFYZZd#D ZcmmTXFYZZ xx+D#6	T+D#6	T		64.)))rt   c                    g }| j                         D ]Q  \  }}|g}t        ||      D ]#  \  }}|s	|j                  t        ||             % |j                  t	        |        S t        | S )z/Convert a multinomial form into an expression. )r   rK   rL   r   r   r   )r   rj   resultr   r   r   r   r   s           r>   expr_from_dictr     st    F		 "uwe$ 	'DAqC1I&	' 	c4j!" <rt   c                ,   t        |      }| j                         }| j                         }t        t	        |             D cg c]  }g  }}t               }|D ]M  }	 |j                  |      }	|j                  |	       t        ||      D ]  \  }
}|j                  |
|	           O t        |      D ]!  \  }}||vs|D ]  }||   s	t        d       # t        t        |      |fS c c}w # t        $ r |D ]  }|j                  d        Y w xY w)z*Reorder levels using dict representation. r   zunable to drop generators)rM   keysvaluesrangerH   r   rW   r   rK   rL   rX   rf   r   mapri   )r   rj   new_gensmonomscoeffsrR   
new_monomsused_indicesr`   r   Mnew_Mr   r   s                 r>   _dict_reorderr     s'   :DXXZFZZ\F$SX0!20J05L 	 	 

3AQ
3 #5QqT"#	  $ G1L  G8)*EFFGG uj!6))) 1  	 #  Q 	 s   	C*A
C//!DDc                  "    e Zd ZdZdZddZd Zy)PicklableWithSlotsa  
    Mixin class that allows to pickle objects with ``__slots__``.

    Examples
    ========

    First define a class that mixes :class:`PicklableWithSlots` in::

        >>> from sympy.polys.polyutils import PicklableWithSlots
        >>> class Some(PicklableWithSlots):
        ...     __slots__ = ('foo', 'bar')
        ...
        ...     def __init__(self, foo, bar):
        ...         self.foo = foo
        ...         self.bar = bar

    To make :mod:`pickle` happy in doctest we have to use these hacks::

        >>> import builtins
        >>> builtins.Some = Some
        >>> from sympy.polys import polyutils
        >>> polyutils.Some = Some

    Next lets see if we can create an instance, pickle it and unpickle::

        >>> some = Some('abc', 10)
        >>> some.foo, some.bar
        ('abc', 10)

        >>> from pickle import dumps, loads
        >>> some2 = loads(dumps(some))

        >>> some2.foo, some2.bar
        ('abc', 10)

    r   Nc                   || j                   }i }|j                  D ]@  }t        |dd       }t        t        dd       }|$||us)|j	                   || |             B |j
                  D ]  }t        | |      st        | |      ||<     |S )N__getstate__)	__class__	__bases__getattrobjectupdate	__slots__rw   )selfclsr   r   getstateobjstatera   s          r>   r   zPicklableWithSlots.__getstate__  s    ;..C  		,A q.$7Hv~t<H#(@$*+		, MM 	.DtT"!$-$	. rt   c                N    |j                         D ]  \  }}t        | ||        y r:   )r   setattr)r   r   ra   values       r>   __setstate__zPicklableWithSlots.__setstate__  s'    779 	'KD%D$&	'rt   r:   )__name__
__module____qualname____doc__r   r   r   r   rt   r>   r   r     s    #J I4'rt   r   c                  ,    e Zd ZdZddZddZd Zd Zy)IntegerPowerablea  
    Mixin class for classes that define a `__mul__` method, and want to be
    raised to integer powers in the natural way that follows. Implements
    powering via binary expansion, for efficiency.

    By default, only integer powers $\geq 2$ are supported. To support the
    first, zeroth, or negative powers, override the corresponding methods,
    `_first_power`, `_zeroth_power`, `_negative_power`, below.
    Nc                   |dk  r>	 |dk(  r| j                         S |dk(  r| j                         S | j                  ||      S t        t        |      dd        D cg c]  }t        |       }}t        |      }| }d}t        |      D ]0  }||   r|r|}	d}n	|z  }	||	|z  }	||dz
  k  s$||z  }|,||z  }2 	S # t        $ r	 t        cY S w xY wc c}w )NrA   rB   r   )moduloTF)
_first_power_zeroth_power_negative_powerrF   NotImplementedreversedbinr\   rH   r   )
r   r   r  r   bitsr    r"   firstr   r/   s
             r>   __pow__zIntegerPowerable.__pow__  s   q5&6,,..!V--////&/AA %-SVABZ$89qCF9D9D	AAE1X $7 %Q!-KAq1u9FA)V$ H) ' &%%& :s!   C  C  C  C CCc                    t         )z
        Compute inverse of self, then raise that to the abs(e) power.
        For example, if the class has an `inv()` method,
            return self.inv() ** abs(e) % modulo
        rF   )r   r   r  s      r>   r  z IntegerPowerable._negative_power,  s
     "!rt   c                    t         )z?Return unity element of algebraic struct to which self belongs.r  r   s    r>   r  zIntegerPowerable._zeroth_power4      !!rt   c                    t         )zReturn a copy of self.r  r  s    r>   r  zIntegerPowerable._first_power8  r  rt   r:   )r   r   r   r   r  r  r  r  r   rt   r>   r  r    s    >"""rt   r  ztuple[type, ...]r|   flint)ModularInteger)F)>r   
__future__r   sympy.external.gmpyr   
sympy.corer   r   r   r   r	   r
   r   r   sympy.core.exprtoolsr   r   sympy.core.numbersr   sympy.polys.polyerrorsr   r   sympy.polys.polyoptionsr   rer^   r_   compile	MULTILINErY   rS   rm   rs   rx   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   parallel_dict_from_basicdict_from_basicbasic_from_dictr   r   r  __annotations__r  nmodfmpz_modr|   "sympy.polys.domains.modularintegerr  )objr   s   00r>   <module>r(     s   < " ,$ $ $ E ' C 1 	3S"%s3S"%s  3 S #&s 	 3	 S	 #&s	
 
 Ss	3Ss	3 

"**_bll
3D-`"J:6 '//sc/"1Q-(Qa(0fGT+ *< 3   *<E' E'P8" 8"v   7U^^,IAE!I[ 0(s   8EE