
    gC                    t   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j        d7ej                  ZdUd9Zd: Zd; Z d< Z!d= Z"d> eD             Z#d? ed@dA         D             Z$dB Z%dC Z&dD Z'dE Z(dF Z)dG Z*dH Z+dI Z,dJ Z-dK Z.e*Z/e,Z0e.Z1dL Z2 G dM dN          Z3 G dO dP          Z4dQe5dR<   edSk    rdd	l6Z6e6j7        e6j8        fZ9d	S ddTl:m;Z; d	Z6e;fZ9d	S )Vz6Useful 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*)$Fc                   t          d | D                       st          d | D             }t          |           dk    r(t          d |D                       rt          d          d |D             }t	          t          ||                     }|rAg }g }|D ]6\  \  }}}}|r|                    |           !|                    |           7||fS t          | \  }} t          |           S )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  $   K   | ]}|j         V  d S N	is_number.0r/   s     Q/var/www/html/ai-engine/env/lib/python3.11/site-packages/sympy/polys/polyutils.py	<genexpr>z_nsort.<locals>.<genexpr>(   s$      **qq{******    c                J    g | ] }d  |                                 D             !S )c                h    g | ]/}|                     d                                           d         0S )   r   )r    as_real_imagr>   r   s     r?   
<listcomp>z%_nsort.<locals>.<listcomp>.<listcomp>,   s3    ???ACCFF!!!$???rA   )rE   r=   s     r?   rG   z_nsort.<locals>.<listcomp>,   s1    
O
O
OA??ann.>.>???
O
O
OrA      c              3  6   K   | ]}|D ]}|j         d k    V  dS )rH   N)_prec)r>   r   r   s      r?   r@   z_nsort.<locals>.<genexpr>.   s5      CCqCCAaglCCCCCCCrA   z%could not compute root with precisionc                &    g | ]\  }}|rd nd||fS )rH   r    )r>   r/   r   s      r?   rG   z_nsort.<locals>.<listcomp>1   s+    
1
1
1TQMAA1a 
1
1
1rA   )allNotImplementedErrorlenanysortedzipappendlist)roots	separatedkeyr/   r   im_r3   s           r?   _nsortrZ      s    **E***** "!! P
O
O
O
OC
5zzA~~#CCCCCCCC~!"IJJJ
1
1S
1
1
1C
S%
!
!C   	 	MJRA !tCyHAu;;rA   c                    t          |          }i dc|,i |j        ct          |j                  D ]\  }}|dz   |<   fd}	 t	          | |          } n# t
          $ r Y nw xY wt          |           S )z1Sort generators in a reasonably intelligent way. NrH   c                   t          |           } :	 t                                         |           z   | dfS # t          $ r Y nw xY wt                              |                                           \  }}|rt          |          }nd}	 |         ||fS # t          $ r Y nw xY w	 t          |         ||fS # t          $ r Y nw xY wt          ||fS )Nr   )strrO   index
ValueError_re_genmatchgroupsintKeyError_gens_order
_max_order)gennamer^   
gens_orderwrts      r?   	order_keyz_sort_gens.<locals>.order_keyM   s   #hh?S	CIIcNN2C;;    mmC((//11e 	JJEEE	%tU33 	 	 	D		%tU33 	 	 	D	 D%((s3   (= 
A
	A

B 
B)(B)-B= =
C
	C
rW   )r   rj   	enumeratesortrQ   	TypeErrortuple)gensargsoptr   rg   rk   ri   rj   s         @@r?   
_sort_gensrt   A   s    


C$OJ
cg
C)) 	$ 	$FAs!eJsOO) ) ) ) ) )8d	***    ;;s   A 
A+*A+c                   t          |           } t          |          }| |k    rt          |           S g g d}}}| D ]}||v r|                    |           t          |          D ]\  }}||v r||         |dz   c||<   }|D ]}|                     |          }|                    | d|                    | |dz   d         } |                    |          }|                    |d|                    ||dz   d         }|                    |           |                    |            |                    |           t          |          S )z2Unify generators in a reasonably intelligent way. r   rH   N)rT   rp   rS   rm   r^   extend)f_gensg_gensrq   commonr   rg   r   s          r?   _unify_gensrz   q   so   &\\F&\\FV}}"a!&D  &==MM#F## , ,3&==!!9a!eLF1Iq  LLF2A2JALLF2A2JACKKKK;;rA   c                    t          |           dk    r+t          | d         d          rt          | d                   S t          |           S )z8Support for passing generators as `*gens` and `[gens]`. rH   r   __iter__)rO   hasattrrp   )rq   s    r?   _analyze_gensr~      s?    
4yyA~~'$q':66~T!W~~T{{rA   c                    fdfd}fd}|                     dd          rt          | |          S t          | |          S )z9Sort low-level factors in increasing 'complexity' order. c                    t          | t                    rt          |           S t          | t                    rfd| D             S | S )Nc                &    g | ]} |          S rL   rL   )r>   r   rk   s     r?   rG   z4_sort_factors.<locals>.order_key.<locals>.<listcomp>   s!    111QIIaLL111rA   )
isinstance	_GF_typesrc   rT   )factorrk   s    r?   rk   z _sort_factors.<locals>.order_key   sR    fi(( 	v;;%% 	1111&1111MrA   c                D    | \  }}t          |          | |          fS r:   rO   )r   r   r    rk   s      r?   order_if_multiple_keyz,_sort_factors.<locals>.order_if_multiple_key   s'    AA99Q<<((rA   c                8    t          |            |           fS r:   r   )r   rk   s    r?   order_no_multiple_keyz,_sort_factors.<locals>.order_no_multiple_key   s    A		!%%rA   multipleTrl   )getrQ   )factorsrr   r   r   rk   s       @r?   _sort_factorsr      s        ) ) ) ) )& & & & & xx
D!! :g#89999g#89999rA   c                ,    g | ]}t          |          S rL   )type)r>   objs     r?   rG   rG      s    ///sc///rA   c                ,    g | ]}t          |          S rL   )floatrF   s     r?   rG   rG      s    (((Qa(((rA   rH      c                    t          |           t          v s	| t          v rdS t          | t                    rt	          |           | k    rdS dS )zBDo not treat NaN and infinities as valid polynomial coefficients. TN)r   illegal_typesfinfr   r   exprs    r?   _not_a_coeffr      sL    Dzz]""ddllt$ 5;;$#6#6t
FrA   c                l   t          |j                  i }}t          |j                  D ]
\  }}|||<   g }| D ]q}i }|j        r|j        |j        z
  }t          j        |          D ]+}	g dg|z  }}
t          j        |	          D ]}t          |          s|j
        r|
                    |           .	 |j        du r7t          |          \  }}|dk     r| t          |t          j                   }}nt#          |          \  }}||||         <   # t$          $ r:  |j        |j         s|
                    |           nt)          d|z            Y w xY wt+          |          }||v r||xx         t          |
 z  cc<   t          |
 ||<   -|                    |           s||j        fS )z@Transform expressions into a multinomial form given generators. r   Fz0%s contains an element of the set of generators.)rO   rq   rm   is_Equalitylhsrhsr   	make_argsr   r   	is_NumberrS   seriesr   r   r   Oner   rd   has_freer   rp   )exprsrs   r   indicesr   r   polysr   polytermcoeffmonomr   baseexps                  r?    _parallel_dict_from_expr_if_gensr      s    SXwA#(##  1

E % % 	'8dh&DM$'' 	* 	*Ds1u5E--- U U#F++ U0@ ULL((((U:..(7(?(?ID#"Qww-0D#dQUF2C2CT(;F(C(CID#/2gdm,,# U U U.v9 U!LL0000"1 3KMS3T #U #U U 10U %LLE}}UsE{*!5kUT#(?s   7ADAE	E	c                   j         fd}nj        du rd }nj        durd }nd }t                      g }}| D ]3}g }|j        r|j        |j        z
  }t          j        |          D ]}g i }	}t          j        |          D ]}
t          |
          s(|
j        s ||
          r|                    |
           9j        du r7t          |
          \  }}|dk     r| t          |t           j                   }}nt%          |
          \  }}|	                    |d          |z   |	|<   |                    |           |                    ||	f           |                    |           5t+          |	          }t-          |          i }}t/          |          D ]
\  }}|||<   g }|D ]}i }|D ]g\  }}dg|z  }|                                D ]\  }}||||         <   t3          |          }||v r||xx         t          | z  cc<   [t          | ||<   h|                    |           |t3          |          fS )
zITransform expressions into a multinomial form and figure out generators. Nc                    | j         v S r:   )domain)r   rs   s    r?   	_is_coeffz3_parallel_dict_from_expr_no_gens.<locals>._is_coeff   s    SZ''rA   Tc                    | j         S r:   )is_algebraicr   s    r?   r   z3_parallel_dict_from_expr_no_gens.<locals>._is_coeff   s    &&rA   Fc                    | t           j        u S r:   )r   ImaginaryUnitr   s    r?   r   z3_parallel_dict_from_expr_no_gens.<locals>._is_coeff  s    Q_,,rA   c                    | j         S r:   r;   r   s    r?   r   z3_parallel_dict_from_expr_no_gens.<locals>._is_coeff  s    ##rA   r   )rs   )r   	extensiongreedysetr   r   r   r   r   r   r   r   rS   r   r   r   r   r   r   
setdefaultaddrt   rO   rm   itemsrp   )r   rs   r   rq   reprsr   termsr   r   elementsr   r   r   r   r   r   r   r   r   r   s    `                  r?    _parallel_dict_from_expr_no_gensr      s   
z	( 	( 	( 	( 	( 	(	$			' 	' 	' 	'	5	 	 	- 	- 	- 	-	$ 	$ 	$ %%%D   	'8dh&DM$'' 	, 	,D "8E--- # ##F++ #1A #YYvEVEV #LL((((zU**$3F$;$;	c77),c$.?.?C$7$?$?	c%-%8%8q%A%AC%GHTNHHTNNNNLL%*++++Ud$$$DTBwA$  1

E    	* 	*KE4CEE!ZZ\\ + +	c'*gdm$$%LLE}}UsE{*!5kUT%++rA   c                6    t          | f|          \  \  }}||fS )zBTransform an expression into a multinomial form given generators. )r   r   rs   r   rq   s       r?   _dict_from_expr_if_gensr   C  #    4dWcBBMGTT:rA   c                6    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   rA   c                T    t          | t          |                    \  }}||j        fS )/Transform expressions into a multinomial form. )_parallel_dict_from_exprr   rq   )r   rr   repsrs   s       r?   parallel_dict_from_exprr   O  s)    (d0C0CDDID#>rA   c                
   |j         durd | D             } t          d | D                       rt          d          |j        rt	          | |          \  }}nt          | |          \  }}||                    d|i          fS )r   Fc                6    g | ]}|                                 S rL   )expandr>   r   s     r?   rG   z,_parallel_dict_from_expr.<locals>.<listcomp>X  s     333D$++--333rA   c              3  (   K   | ]}|j         d u V  dS )FN)is_commutativer   s     r?   r@   z+_parallel_dict_from_expr.<locals>.<genexpr>Z  s*      
:
:D4%'
:
:
:
:
:
:rA   -non-commutative expressions are not supportedrq   )r   rP   r   rq   r   r   clone)r   rs   r   rq   s       r?   r   r   U  s    
z33E333

:
:E
:
:
::: OMNNN
x B5eSAA
dd5eSAA
dFD>****rA   c                T    t          | t          |                    \  }}||j        fS )1Transform an expression into a multinomial form. )_dict_from_exprr   rq   )r   rr   reprs   s       r?   dict_from_exprr   e  s)    t]4%8%899HC=rA   c                   | j         du rt          d          d |j        durt          | t          t
          f          st          d          |                                 } t          fdt          j        |           D                       r<t          |           } t          fdt          j        |           D                       <t          d t          j        |           D                       r:t          |           } t          d t          j        |           D                       :|j        rt          | |          \  }}nt          | |          \  }}||                    d|i          fS )r   Fr   c                X    | j         o#| j        j        o| j        j        o| j        j        S r:   )is_Powr   is_positive
is_Integerr   is_Addr   s    r?   _is_expandable_powz+_dict_from_expr.<locals>._is_expandable_powp  s1     % 4 %9L %I$	&rA   zexpression must be of type Exprc              3  |   K   | ]6} |          p&|j         ot          fd |j        D                       V  7dS )c              3  .   K   | ]} |          V  d S r:   rL   )r>   r   r   s     r?   r@   z,_dict_from_expr.<locals>.<genexpr>.<genexpr>z  s/      66!""1%%666666rA   Nis_MulrP   rr   )r>   r   r   s     r?   r@   z"_dict_from_expr.<locals>.<genexpr>y  sr       % %;< %$Q'' 718 ,76666qv66666% % % % % %rA   c              3  `   K   | ])}|j         ot          d  |j        D                       V  *dS )c              3  $   K   | ]}|j         V  d S r:   )r   )r>   r   s     r?   r@   z,_dict_from_expr.<locals>.<genexpr>.<genexpr>~  s$      "<"<18"<"<"<"<"<"<rA   Nr   rF   s     r?   r@   z"_dict_from_expr.<locals>.<genexpr>~  sB      ZZ!(<s"<"<QV"<"<"<<<ZZZZZZrA   rq   )r   r   r   r   r
   r	   rP   r   r   r   r   rq   r   r   r   )r   rs   r   rq   r   s       @r?   r   r   k  s   e##MNNN& & & z$r
++ 	E!"CDDD{{}} % % % %d##% % % % % 	, &d++D	  % % % %d##% % % % % 	,
 ZZcmTXFYFYZZZZZ 	$d##D ZZcmTXFYFYZZZZZ 	$ x 7+D#66	TT+D#66	T		64.))))rA   c                    g }|                                  D ]_\  }}|g}t          ||          D ]*\  }}|r#|                    t          ||                     +|                    t	          |            `t          | S )z/Convert a multinomial form into an expression. )r   rR   rS   r   r   r   )r   rq   resultr   r   r   r   r   s           r?   expr_from_dictr     s    F		 " "uwe$$ 	' 	'DAq 'C1II&&&c4j!!!!<rA   c                   t          |          }|                                 }|                                 }d t          t	          |                     D             }t                      }|D ]}	 |                    |          }|                    |           t          ||          D ] \  }	}
|
	                    |	|                    !_# t          $ r |D ]}
|
	                    d           Y w xY wt          |          D ]%\  }}||vr|D ]}||         rt          d          &t          t          |          |fS )z*Reorder levels using dict representation. c                    g | ]}g S rL   rL   )r>   rY   s     r?   rG   z!_dict_reorder.<locals>.<listcomp>  s    000!2000rA   r   zunable to drop generators)rT   keysvaluesrangerO   r   r^   r   rR   rS   r_   rm   r   maprp   )r   rq   new_gensmonomscoeffs
new_monomsused_indicesrg   r   Mnew_Mr   rY   r   s                 r?   _dict_reorderr     sy   ::DXXZZFZZ\\F00uSXX000J55L 	  	 	 

3AQ
33 # #5QqT""""# 	  	  	 #    Q   	  $ G G1L   G G8 G)*EFFFG uj!!6))s   1AC$C43C4c                  $    e Zd ZdZdZddZd ZdS )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)

    rL   Nc                "   || j         }i }|j        D ]N}t          |dd           }t          t          dd           }|#||ur|                     || |                     O|j        D ]%}t          | |          rt          | |          ||<   &|S )N__getstate__)	__class__	__bases__getattrobjectupdate	__slots__r}   )selfclsr   r   getstateobjstaterh   s          r?   r   zPicklableWithSlots.__getstate__  s    ;.C  		, 		,A q.$77Hv~t<<H#(@(@$**+++ M 	. 	.DtT"" .!$--$rA   c                \    |                                 D ]\  }}t          | ||           d S r:   )r   setattr)r  r   rh   values       r?   __setstate__zPicklableWithSlots.__setstate__  s<    7799 	' 	'KD%D$&&&&	' 	'rA   r:   )__name__
__module____qualname____doc__r   r   r  rL   rA   r?   r   r     sI        # #J I   4' ' ' ' 'rA   r   c                  .    e Zd ZdZddZddZd Zd ZdS )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     rc	 |dk    r|                                  S |dk    r|                                 S |                     ||          S # t          $ r
 t          cY S w xY wd t          t          |          dd                    D             }t          |          }| }d}t          |          D ]2}||         r|r|}d}n||z  }|||z  }||dz
  k     r||z  }|||z  }3|S )NrD   rH   r   )moduloc                ,    g | ]}t          |          S rL   )rc   )r>   r   s     r?   rG   z,IntegerPowerable.__pow__.<locals>.<listcomp>  s    999qCFF999rA   TF)	_first_power_zeroth_power_negative_powerrN   NotImplementedreversedbinrO   r   )	r  r   r  bitsr    r"   firstr   r/   s	            r?   __pow__zIntegerPowerable.__pow__  s>   q55&66,,...!VV--/////&/AAA& & & &%%%%& :9HSVVABBZ$8$8999DD		AAE1XX $ $7 ( ( %Q!-KAq1u99FA)VHs   A A A A'&A'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
        rN   )r  r   r  s      r?   r  z IntegerPowerable._negative_power,  s
     "!rA   c                    t           )z?Return unity element of algebraic struct to which self belongs.r  r  s    r?   r  zIntegerPowerable._zeroth_power4      !!rA   c                    t           )zReturn a copy of self.r  r  s    r?   r  zIntegerPowerable._first_power8  r  rA   r:   )r	  r
  r  r  r  r  r  r  rL   rA   r?   r  r    sd            >" " " "" " "" " " " "rA   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   rere   rf   compile	MULTILINEr`   rZ   rt   rz   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"  rL   rA   r?   <module>r4     s   < < < " " " " " " , , , , , ,$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ E E E E E E E E ' ' ' ' ' ' C C C C C C C C 1 1 1 1 1 1 				3S"%s3S"%s  3 S #&s 	 3	 S	 #&s	
 
 Ss	3Ss	3   

"*_bl
3
3   D- - -`" " "J  : : :6 0/h///(((1Q3-(((  0 0 0fG G GT      + + +   * * *<   3   * * *<E' E' E' E' E' E' E' E'P8" 8" 8" 8" 8" 8" 8" 8"v     7LLLU^,IIIAAAAAAE!IIIrA   