
    \hl                     0   S r SSKJr  SSKJr  SSKJr  SSKJr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  SS
KJr  SSKJr  SSKJr  SSKJrJr  SSKJ r J!r!J"r"  SSK#J$r$  SSK%J&r&  SSK'J(r(J)r)J*r*  SSK+J,r,  SSK-J.r.J/r/  SSK0J1r1J2r2J3r3J4r4J5r5J6r6J7r7J8r8J9r9J:r:  SSK;J<r<J=r=  SSK>J?r?  SSK@JArA  SSKBJCrC  SSKDJErE  SSKFJGrGJHrHJIrI  \)SS4S jrJS rKS rLS  rMS1S" jrNS# rOS$ rPS2S% jrQS& rRS' rSS( rTS) rUS* rVS+ rWS, rXS- rY\HS3S. j5       rZS/ r[\HS3S0 j5       r\g!)4z*Minimal polynomials for algebraic numbers.    )reduce)Add)Factors)
expand_mulexpand_multinomial_mexpand)Mul)IRationalpi_illegal)S)Dummy)sympify)preorder_traversal)exp)sqrtcbrt)cossintan)divisors)subsets)ZZQQFractionField)dup_chebyshevt)NotAlgebraicGeneratorsError)
PolyPurePolyinvertfactor_listgroebner	resultantdegreepoly_from_exprparallel_poly_from_exprlcm)dict_from_exprexpr_from_dict)rs_compose_add)ring)CRootOf)cyclotomic_poly)numbered_symbolspublicsift      c           
         [        U S   [        5      (       a  U  Vs/ s H  ofS   PM	     n n[        U 5      S:X  a  U S   $ Sn0 n[        US5      (       a  UR                  O/ n	Xt::  Ga3  U  Vs/ s H"  ofR                  5       R                  X05      PM$     n
nUR                  (       a   U
 Vs/ s H  ofR                  U5      PM     n
n[        [        U5      [        U	5      SS9 H  n[        X5       H	  u  pXU'   M     [        U
5       VVs/ s H0  u  p[        UR                  U5      R                  U5      5      U4PM2     nnn[        S U 5       5      (       a  M|  [!        U5      nUSS	 u  u  nnu  nnUUS
-  :  d  M  U U   s  $    US	-  nXt::  a  GM3  [#        SU-  5      es  snf s  snf s  snf s  snnf )zY
Return a factor having root ``v``
It is assumed that one of the factors has root ``v``.
r      
   symbolsT)k
repetitionc              3   8   #    U  H  u  pU[         ;   v   M     g 7fN)r   ).0i_s      X/var/www/auris/envauris/lib/python3.13/site-packages/sympy/polys/numberfields/minpoly.py	<genexpr>!_choose_factor.<locals>.<genexpr>H   s     8ZTQ1=Zs   N   i@B z4multiple candidates for the minimal polynomial of %s)
isinstancetuplelenhasattrr8   as_exprxreplace	is_numbernr   rangezip	enumerateabssubsanysortedNotImplementedError)factorsxvdomprecboundfprec1pointsr8   ferK   sr>   
candidatescanaixbr?   s                       r@   _choose_factorrd   (   s    '!*e$$!()AQ4)
7|qqzEF$S)44ckk"G
- 4;;7aiik""A5)7;;;%'(R##d)RB( uW$GAGq	 (
 %R=*(CA qvvf~//67;(  *
 8Z888
 $C!"1gOGQVa1u9}r{"' H* 	
; -> TWXX
YYM * <(*s   F+.)F0.F57F:c                 N    [        S [        R                  " U 5       5       5      $ )Nc              3   <  #    U  H  n[         R                  " U5        Ht  nUR                  =(       d\    UR                  =(       aI    UR                  R                  =(       a,    S UR
                  -  R                  =(       a    UR                  v   Mv     M     g7f)rC   N)r	   	make_argsis_Rationalis_Powbaser   
is_Integeris_extended_real)r=   trZ   s      r@   rA    _is_sum_surds.<locals>.<genexpr>Y   s|      =!A3==+;a }} K !K	!K !!%%33!K898J8JK+;K!s   BB)allr   rg   )ps    r@   _is_sum_surdsrq   X   s%     =q!= = =    c                 V   S n/ nU R                    GH
  nUR                  (       d  U" U5      (       a&  UR                  [        R                  US-  45        MH  UR
                  (       a#  UR                  U[        R                  45        M|  UR                  (       a>  UR                  R                  (       a#  UR                  U[        R                  45        M  [        e[        UR                   USS9u  pEUR                  [        U6 [        U6 S-  45        GM     UR                  S S9  US   S   [        R                  L a  U $ U VVs/ s H  u  p6UPM	     nnn[        [        U5      5       H  nXx   S:w  d  M    O   S	S
KJn	  U	" UWS 6 u  pn/ n/ nU HT  u  p6Xk;   a&  UR                  X6[        R"                  -  -  5        M0  UR                  X6[        R"                  -  -  5        MV     [%        U6 n[%        U6 n['        US-  5      ['        US-  5      -
  n U $ s  snnf )a  
helper function for ``_minimal_polynomial_sq``

It selects a rational ``g`` such that the polynomial ``p``
consists of a sum of terms whose surds squared have gcd equal to ``g``
and a sum of terms with surds squared prime with ``g``;
then it takes the field norm to eliminate ``sqrt(g)``

See simplify.simplify.split_surds and polytools.sqf_norm.

Examples
========

>>> from sympy import sqrt
>>> from sympy.abc import x
>>> from sympy.polys.numberfields.minpoly import _separate_sq
>>> p= -x + sqrt(2) + sqrt(3) + sqrt(7)
>>> p = _separate_sq(p); p
-x**2 + 2*sqrt(3)*x + 2*sqrt(7)*x - 2*sqrt(21) - 8
>>> p = _separate_sq(p); p
-x**4 + 4*sqrt(7)*x**3 - 32*x**2 + 8*sqrt(7)*x + 20
>>> p = _separate_sq(p); p
-x**8 + 48*x**6 - 536*x**4 + 1728*x**2 - 400

c                 `    U R                   =(       a    U R                  [        R                  L $ r<   )ri   r   r   Half)exprs    r@   is_sqrt_separate_sq.<locals>.is_sqrtx   s    {{1txx16611rr   rC   T)binaryc                     U S   $ )Nr6    )zs    r@   <lambda>_separate_sq.<locals>.<lambda>   s    1rr   )keyr6   r   )
_split_gcdN)argsis_Mulappendr   Oneis_Atomri   r   
is_integerrS   r2   r	   sortrL   rF   sympy.simplify.radsimpr   ru   r   r   )rp   rw   ra   yTFr|   surdsr>   r   gb1b2a1a2p1p2s                    r@   _separate_sqr   ^   s   42 	AVVxxqzz!%%A'!QUU$aee..!QUU$))5DAHHc1gsAwz*+  FF~FuQx155141Q1E3u:8q=  2E!"I&IA2	B	B7IIa166	k"IIa166	k"	 
 
bB	bBQ(2q5/)AH! s   H%c                    [        U 5      n [        U5      nUR                  (       a  US:  a  [        U 5      (       d  gU [        SU5      -  nX-  n  [	        U 5      nX@L a  UR                  X"U-  05      n OUn M)  US:X  aI  [        U 5      nU R                  X$R                  U5      -  5      S:  a  U * n U R                  5       S   n U $ [        U 5      S   n[        XRU5      nU$ )a  
Returns the minimal polynomial for the ``nth-root`` of a sum of surds
or ``None`` if it fails.

Parameters
==========

p : sum of surds
n : positive integer
x : variable of the returned polynomial

Examples
========

>>> from sympy.polys.numberfields.minpoly import _minimal_polynomial_sq
>>> from sympy import sqrt
>>> from sympy.abc import x
>>> q = 1 + sqrt(2) + sqrt(3)
>>> _minimal_polynomial_sq(q, 3, x)
x**12 - 4*x**9 - 4*x**6 + 16*x**3 - 8

r   Nr6   )r   rk   rq   r   r   rP   r    coeffr&   	primitiver#   rd   )rp   rK   rU   pnr   rT   results          r@   _minimal_polynomial_sqr      s    . 	
A
A<<q1uM!,<,<	
HQN	BFA
!_7a4!AA  	Av!W771iil?#a'AKKM! !nQGG+FMrr   Nc                 >   [        [        U5      5      nUc  [        XU5      nUc  [        X'U5      nOUR                  X705      nU [        L a|  U[
        :X  a;  [        S[
        5      u  pU" [        U5      S   5      n
U" [        U5      S   5      nOX[        XSU-
  4X75      u  u  pnU
R                  U5      nUR                  5       nO!U [        L a  [        XSU5      nO[        S5      eU [        L d
  U[
        :w  a  [        WXgU/S9nO&[        W
W5      n[!        UR#                  5       U5      n[%        XS5      n[%        Xg5      nU [        L a  US:X  d  US:X  a  U$ ['        XUS9nUR)                  5       u  nn[+        UX0" X5      U5      nUR                  5       $ )ap  
return the minimal polynomial for ``op(ex1, ex2)``

Parameters
==========

op : operation ``Add`` or ``Mul``
ex1, ex2 : expressions for the algebraic elements
x : indeterminate of the polynomials
dom: ground domain
mp1, mp2 : minimal polynomials for ``ex1`` and ``ex2`` or None

Examples
========

>>> from sympy import sqrt, Add, Mul, QQ
>>> from sympy.polys.numberfields.minpoly import _minpoly_op_algebraic_element
>>> from sympy.abc import x, y
>>> p1 = sqrt(sqrt(2) + 1)
>>> p2 = sqrt(sqrt(2) - 1)
>>> _minpoly_op_algebraic_element(Mul, p1, p2, x, QQ)
x - 1
>>> q1 = sqrt(y)
>>> q2 = 1 / y
>>> _minpoly_op_algebraic_element(Add, q1, q2, x, QQ.frac_field(y))
x**2*y**2 - 2*x*y - y**3 + 1

References
==========

.. [1] https://en.wikipedia.org/wiki/Resultant
.. [2] I.M. Isaacs, Proc. Amer. Math. Soc. 25 (1970), 638
       "Degrees of sums in a separable field extension".

Xr   zoption not availablegensr6   domain)r   str_minpoly_composerP   r   r   r-   r*   r(   composerH   r	   _mulyrS   r%   r,   r+   as_expr_dictr&   r    r#   rd   )opex1ex2rU   rW   mp1mp2r   Rr   r   r   r?   rmp1adeg1deg2rT   ress                      r@   _minpoly_op_algebraic_elementr      sy   H 	c!fA
{ss+
{ss+hhv	Sy"9R=DA>#&q)*B>#&q)*B13A,EKHRa

2A99;D	sSQ!"899	SyC2IdC!f-2r"1>>+Q/#>D#>D	SyTQY$!) Q#AJAw
!R\3
7C;;=rr   c                     [        X5      S   n[        U5      nUR                  5        VVs/ s H  u  u  pEXQX4-
  -  -  PM     nnn[        U6 $ s  snnf )z8
Returns ``expand_mul(x**degree(p, x)*p.subs(x, 1/x))``
r   r'   r&   termsr   )rp   rU   r   rK   r>   cra   s          r@   _invertxr   $  sP     
	a	 Br
A')xxz2zGDQZzA27N 	3s   Ac                     [        X5      S   n[        U5      nUR                  5        VVs/ s H  u  u  pVXaU-  -  X$U-
  -  -  PM     nnn[        U6 $ s  snnf )z0
Returns ``_mexpand(y**deg*p.subs({x:x / y}))``
r   r   )rp   rU   r   r   rK   r>   r   ra   s           r@   r   r   /  sY     
	a	 Br
A.0hhj9j74ATAAJ	jA97N 	:s   Ac                    [        U5      nU(       d  [        XU5      nUR                  (       d  [        SU -  5      eUS:  a.  XB:X  a  [	        SU -  5      e[        XB5      nUS:X  a  U$ U* nSU -  n [        [        U5      5      nUR                  X%05      nUR                  5       u  pg[        [        XBU-  XV-  -
  U/S9X#S9nUR                  5       u  p[        XX-  U5      nUR                  5       $ )a8  
Returns ``minpoly(ex**pw, x)``

Parameters
==========

ex : algebraic element
pw : rational number
x : indeterminate of the polynomial
dom: ground domain
mp : minimal polynomial of ``p``

Examples
========

>>> from sympy import sqrt, QQ, Rational
>>> from sympy.polys.numberfields.minpoly import _minpoly_pow, minpoly
>>> from sympy.abc import x, y
>>> p = sqrt(1 + sqrt(2))
>>> _minpoly_pow(p, 2, x, QQ)
x**2 - 2*x - 1
>>> minpoly(p**2, x)
x**2 - 2*x - 1
>>> _minpoly_pow(y, Rational(1, 3), x, QQ.frac_field(y))
x**3 - y
>>> minpoly(y**Rational(1, 3), x)
x**3 - y

+%s does not seem to be an algebraic elementr   z
%s is zeror   r6   r   r   )r   r   is_rationalr   ZeroDivisionErrorr   r   r   rP   as_numer_denomr    r%   r#   rd   rH   )expwrU   rW   mpr   rK   dr   r?   rT   s              r@   _minpoly_powr   :  s    < 
BbS)>>H2MNN	Av7#L2$566b_8ISrTc!fA	!BDA
yTAD[s3Q
CC"JA
RVS
1C;;=rr   c           
          [        [        US   US   X5      nUS   US   -   nUSS  H  n[        [        XEXUS9nXE-   nM     U$ )z&
returns ``minpoly(Add(*a), dom, x)``
r   r6   rC   Nr   )r   r   rU   rW   ra   r   rp   pxs         r@   _minpoly_addr   o  [     
'sAaD!A$	?B	!qtAe*3q2FF  Irr   c           
          [        [        US   US   X5      nUS   US   -  nUSS  H  n[        [        XEXUS9nXE-  nM     U$ )z&
returns ``minpoly(Mul(*a), dom, x)``
r   r6   rC   Nr   )r   r	   r   s         r@   _minpoly_mulr   {  r   rr   c                    U R                   S   R                  5       u  p#U[        L GaL  UR                  (       Ga:  UR                  n[        U5      nUR                  (       a>  [        U[        5      n[        [        U5       Vs/ s H  oaXF-
  S-
  -  X6   -  PM     sn6 $ UR                  S:X  a#  US:X  a  SUS-  -  SUS-  -  -
  SUS	-  -  -   S
-
  $ US	-  S:X  aZ  [        U[        5      n[        US-   5       Vs/ s H  oaXF-
  -  X6   -  PM     nn[        U6 n[        U5      u  p[        XU 5      n
U
$ S[        S	U-  [        -  5      -
  S	-  [        R                   -  n[#        X[$        5      n
U
$ ['        SU -  5      es  snf s  snf )zi
Returns the minimal polynomial of ``sin(ex)``
see https://mathworld.wolfram.com/TrigonometryAngles.html
r   r6   	   @      `      $   rC      r   )r   as_coeff_Mulr   r   qr   is_primer   r   r   rL   rp   r#   rd   r   r   ru   r   r   r   )r   rU   r   ra   rK   r   r>   r   r?   rT   r   rv   s               r@   _minpoly_sinr     ss   
 771:""$DABw===A
Azz #1b)%(C(Q^AD0(CDDssax6ad7R1W,r!Q$w6::1uz #1b).3AEl;lZ_l;G(^
$W4
QqSV_a'!&&0D"4B/CJ
DrI
JJ) D <s   F>F	c           	      ,   U R                   S   R                  5       u  p#U[        L GaY  UR                  (       GaG  UR                  S:X  aL  UR
                  S:X  a  SUS-  -  SUS-  -  -
  SU-  -
  S-   $ UR
                  S:X  a  SUS-  -  S	U-  -
  S-
  $ OlUR                  S:X  a\  [        UR
                  5      nUR                  (       a6  [        X5      n[        UR                  U[        SU-
  S-  5      05      5      $ [        UR
                  5      n[        U[        5      n[        US-   5       Vs/ s H  oqXg-
  -  X7   -  PM     nn[!        U6 S
UR                  -  -
  n[#        U5      u  p[%        XU 5      nU$ ['        SU -  5      es  snf )zi
Returns the minimal polynomial of ``cos(ex)``
see https://mathworld.wolfram.com/TrigonometryAngles.html
r   r6         r   r   rC   r   r   r   r   )r   r   r   r   rp   r   r   r   r   r   rP   r   intr   r   rL   r   r#   rd   r   )r   rU   r   ra   r   r^   rK   r>   r   r?   rT   r   s               r@   _minpoly_cosr     sl   
 771:""$DABw===ssax33!8QT6AadF?QqS014433!8QT6AaC<!++ ACCL::$R+A#AFFAdAE19o+>$?@@ ACCAq"%A*/A,7,QQUAD,A7Q2)#A$QJA R0CJ
DrI
JJ 8s   9Fc                    U R                   S   R                  5       u  p#U[        L a  UR                  (       a  US-  n[	        UR
                  5      nUR                  S-  S:X  a  UOSn/ n[        UR                  S-   S-  US-   S5       H5  nUR                  X1U-  -  5        X4U-
  S-
  -  XF-
  -  * US-   US-   -  -  nM7     [        U6 n[        U5      u  p[        XU 5      n
U
$ [        SU -  5      e)z_
Returns the minimal polynomial of ``tan(ex)``
see https://github.com/sympy/sympy/issues/21430
r   rC   r6   r   )r   r   r   r   r   r   rp   rL   r   r   r#   rd   r   )r   rU   r   ra   rK   r   r9   r   r?   rT   r   s              r@   _minpoly_tanr     s    
 771:""$DABw==AAACCASS1W\qAEACCE19ac1-Q!tV$1Qio&AaC!A#;7 . UA$QJA R0CJ
DrI
JJrr   c                    U R                   S   R                  5       u  p#U[        [        -  :X  Ga.  UR                  (       Ga  [        UR                  5      nUR                  S:X  d  UR                  S:X  a  US:X  a  US-  U-
  S-   $ US:X  a  US-  S-   $ US:X  a  US-  US-  -
  S-   $ US:X  a  US-  S-   $ US	:X  a  US-  US-  -
  S-   $ US
:X  a  US-  US-  -
  US-  -   US-  -
  S-   $ UR                  (       a  Sn[        U5       H  nXQ* U-  -  nM     U$ [        SU-  5       Vs/ s H  n[        Xa5      PM     nn[        XqU 5      nU$ [        SU -  5      e[        SU -  5      es  snf )z/
Returns the minimal polynomial of ``exp(ex)``
r   r6   r   r   rC   r   r   r   r   r7   r   )r   r   r
   r   r   r   r   rp   r   rL   r   r/   rd   r   )	r   rU   r   ra   r   r^   r>   rT   r   s	            r@   _minpoly_expr     s    771:""$DAAbDy===Assax133"96a4!8a<'6a4!8O6a4!Q$;?*6a4!8O6a4!Q$;?*7a4!Q$;A-14q88::A"1Xb1W &H 7?qsmDmq,mGDB/BILrQRR
DrI
JJ Es   -E.c                     U R                   nUR                  U R                  R                  S   U05      n[	        X!5      u  p4[        XAU 5      nU$ )z9
Returns the minimal polynomial of a ``CRootOf`` object.
r   )rv   rP   polyr   r#   rd   )r   rU   rp   r?   rT   r   s         r@   _minpoly_rootofr     sI     	A	Q"#AQ"JAG+FMrr   c           
      B	   U R                   (       a  U R                  U-  U R                  -
  $ U [        L a2  [	        US-  S-   XS9u  p4[        U5      S:X  a  US-  S-   $ U[        -
  $ U [        R                  L aI  [	        US-  U-
  S-
  XS9u  p4[        U5      S:X  a  US-  U-
  S-
  $ [        XAS[        S5      -   S-  US9$ U [        R                  L a  [	        US-  US-  -
  U-
  S-
  XS9u  p4[        U5      S:X  a  US-  US-  -
  U-
  S-
  $ S[        SS[        S5      -  -
  5      -   [        SS[        S5      -  -   5      -   S-  n[        XAXRS9$ [        US	5      (       a  XR                  ;   a  X-
  $ UR                  (       aB  [        U 5      (       a2  U nX-  n  [!        U 5      nXpL a  [        [	        U 5      S   X5      $ Un M+  U R"                  (       a  [%        X/U R&                  Q76 nU$ U R(                  (       Ga  [+        U 5      R,                  n	[/        U	R1                  5       S
 5      n
U
S   (       GaP  U[2        :X  GaE  [5        U
S   U
S   -    VV s/ s H	  u  pX-  PM     sn n6 n[7        U
S   5      nUR9                  5        Vs/ s H  oR                  PM     nn[;        [<        US5      n[        R>                  nURA                  U[        RB                  5      nUR1                  5        VVs/ s H%  u  nnUUR                  U-  UR                  -  -  PM'     nnn[5        U6 n[E        Xq5      nUR                  X-  -  UR                  UUU-  -  -  -
  nUU-  U[G        SU5      -  -  n[I        [4        UUXUUS9nU$ [K        X/U R&                  Q76 n U$ U RL                  (       a#  [O        U RP                  U RR                  X5      nU$ U RT                  [V        L a  [Y        X5      nU$ U RT                  [Z        L a  []        X5      nU$ U RT                  [^        L a  [a        X5      nU$ U RT                  [R        L a  [c        X5      nU$ U RT                  [d        L a  [g        X5      nU$ [i        SU -  5      es  sn nf s  snf s  snnf )au  
Computes the minimal polynomial of an algebraic element
using operations on minimal polynomials

Examples
========

>>> from sympy import minimal_polynomial, sqrt, Rational
>>> from sympy.abc import x, y
>>> minimal_polynomial(sqrt(2) + 3*Rational(1, 3), x, compose=True)
x**2 - 2*x - 1
>>> minimal_polynomial(sqrt(y) + 1/y, x, compose=True)
x**2*y**2 - 2*x*y - y**3 + 1

rC   r6   r   r4   )rW   r      !   r8   c                 L    U S   R                   =(       a    U S   R                   $ )Nr   r6   )rh   )itxs    r@   r}   "_minpoly_compose.<locals>.<lambda>J  s     A(:(:(Qs1v?Q?Q(Qrr   TFN)r   r   r   )5rh   r   rp   r
   r#   rF   r   GoldenRatiord   r   TribonacciConstantr   rG   r8   is_QQrq   r   is_Addr   r   r   r   rT   r2   itemsr   r	   dictvaluesr   r)   NegativeOnepopZerominimal_polynomialr   r   r   ri   r   rj   r   	__class__r   r   r   r   r   r   r   r.   r   r   )r   rU   rW   r?   rT   facrV   r   r   rZ   r   bxr1r   denslcmdensneg1expn1rj   numsr   r   r   s                          r@   r   r     sM     
~~ttAv}	Qw A19
w<1,q!tax7!a%7	Q]] AAq=
w<1a4!8a<!'q47{Ao3GG	Q!!! A1q1!4aD
w<1a4!Q$;?Q&&tB48O,,tB48O/DDIC!'c;;sI2#4v
yy]2&&
r"Cy%k"oa&8!??  
yy1,BGG,L JK 
BKQRT77sbyQuX$-?@-?62-?@ACagB!#-ACCD-S$*G==DFF4(E>@hhjIj74D133w;!##-.jDIt*C$S,C %%
"SUU4%-+@%@@C+Xa%9 99C/S#q3TWXC" J q00C J 
277BFFA3 J 
	2! J 
	2! J 
	2! J 
	2!
 J	 
	 b$ J H2MNNA A- Js   #R
R,Rc                 Z   [        U 5      n U R                  (       a
  [        U SS9n [        U 5       H  nUR                  (       d  M  Sn  O   Ub  [        U5      [
        paO[        S5      [        paU(       d;  U R                  (       a$  [        [        [        U R                  5      5      nO[        n[        US5      (       a"  XR                  ;   a  [        SU< SU< 35      eU(       ax  [        XU5      nUR!                  5       S   nUR#                  U[%        Xq5      -  5      nUR&                  (       a  [)        U* 5      nU(       a  U" XqSS	9$ UR+                  U5      $ UR,                  (       d  [/        S
5      e[1        XU5      nU(       a  U" XqSS	9$ UR+                  U5      $ )a  
Computes the minimal polynomial of an algebraic element.

Parameters
==========

ex : Expr
    Element or expression whose minimal polynomial is to be calculated.

x : Symbol, optional
    Independent variable of the minimal polynomial

compose : boolean, optional (default=True)
    Method to use for computing minimal polynomial. If ``compose=True``
    (default) then ``_minpoly_compose`` is used, if ``compose=False`` then
    groebner bases are used.

polys : boolean, optional (default=False)
    If ``True`` returns a ``Poly`` object else an ``Expr`` object.

domain : Domain, optional
    Ground domain

Notes
=====

By default ``compose=True``, the minimal polynomial of the subexpressions of ``ex``
are computed, then the arithmetic operations on them are performed using the resultant
and factorization.
If ``compose=False``, a bottom-up algorithm is used with ``groebner``.
The default algorithm stalls less frequently.

If no ground domain is given, it will be generated automatically from the expression.

Examples
========

>>> from sympy import minimal_polynomial, sqrt, solve, QQ
>>> from sympy.abc import x, y

>>> minimal_polynomial(sqrt(2), x)
x**2 - 2
>>> minimal_polynomial(sqrt(2), x, domain=QQ.algebraic_field(sqrt(2)))
x - sqrt(2)
>>> minimal_polynomial(sqrt(2) + sqrt(3), x)
x**4 - 10*x**2 + 1
>>> minimal_polynomial(solve(x**3 + x + 3)[0], x)
x**3 + x + 3
>>> minimal_polynomial(sqrt(y), x)
x**2 - y

T)	recursiveFrU   r8   zthe variable z$ is an element of the ground domain r6   )fieldz!groebner method only works for QQ)r   rJ   r   r   is_AlgebraicNumberr    r   r!   free_symbolsr   r   listrG   r8   r   r   r   r   r&   is_negativer   collectr   rS   _minpoly_groebner)	r   rU   r   polysr   rv   clsr   r   s	            r@   r   r   p  sZ   n 
B	||bD)"2&"""G '
 	}T3sX3??"2tBOO'<=FFvy!!a>>&9-.8 9 	9 !"0!!#A&LLF6--.==(F-2s6D)Iq8II<<!"EFFrc*F).3v%EFNN14EErr   c                   ^^^^^^^ [        S[        S9m0 0 smmSUUU4S jjmUUUUUU4S jmS nSn[        U 5      n U R                  (       a  U R	                  5       R                  T5      $ U R                  (       a  U R                  T-  U R                  -
  nGO
U" U 5      nU(       a  U S-  n SnU R                  (       aE  S	U R                  -  R                  (       a'  S	U R                  -  n[        U R                  UT5      nO+[        U 5      (       a  [        U [        R                   T5      nUb  UnUck  T" U 5      nTU-
  /[#        TR%                  5       5      -   n	['        U	[#        TR%                  5       5      T/-   S
S9n
[)        U
S   5      u  p[+        UTU 5      nU(       a:  [-        WT5      nUR/                  T[1        UT5      -  5      S:  a  [3        U* 5      nW$ )a  
Computes the minimal polynomial of an algebraic number
using Groebner bases

Examples
========

>>> from sympy import minimal_polynomial, sqrt, Rational
>>> from sympy.abc import x
>>> minimal_polynomial(sqrt(2) + 3*Rational(1, 3), x, compose=False)
x**2 - 2*x - 1

ra   )r  Nc                 p   > [        T5      nUTU '   Ub  X1-  U-   TU '   U$ UR                  " U5      TU '   U$ r<   )nextrH   )r   r   rj   ra   	generatormappingr8   s       r@   update_mapping)_minpoly_groebner.<locals>.update_mapping  sI    O&4-GBK  ++a.GBKrr   c                 0  > U R                   (       a=  U [        R                  L a  U T
;  a
  T" U SS5      $ TU    $ U R                  (       a  U $ GO0U R                  (       a)  [        U R                   Vs/ s H  nT" U5      PM     sn6 $ U R                  (       a)  [        U R                   Vs/ s H  nT" U5      PM     sn6 $ U R                  (       Gax  U R                  R                  (       Ga[  U R                  S:  a  [        U R                  TT	5      n[        TU5      R                  5       nUR                  TU R                  5      R!                  5       nU R                  S:X  a  T" U5      $ X@R                  * -  n U R                  R"                  (       dQ  U R                  U R                  R$                  -  R!                  5       ['        SU R                  R(                  5      peOU R                  U R                  peT" U5      nXV-  nUT
;  a/  UR"                  (       a  UR!                  5       $ T" USU-  U* 5      $ TU   $ O2U R*                  (       a!  U T
;  a  T" X R-                  5       5      $ TU    $ [/        SU -  5      es  snf s  snf )a3  
Transform a given algebraic expression *ex* into a multivariate
polynomial, by introducing fresh variables with defining equations.

Explanation
===========

The critical elements of the algebraic expression *ex* are root
extractions, instances of :py:class:`~.AlgebraicNumber`, and negative
powers.

When we encounter a root extraction or an :py:class:`~.AlgebraicNumber`
we replace this expression with a fresh variable ``a_i``, and record
the defining polynomial for ``a_i``. For example, if ``a_0**(1/3)``
occurs, we will replace it with ``a_1``, and record the new defining
polynomial ``a_1**3 - a_0``.

When we encounter a negative power we transform it into a positive
power by algebraically inverting the base. This means computing the
minimal polynomial in ``x`` for the base, inverting ``x`` modulo this
poly (which generates a new polynomial) and then substituting the
original base expression for ``x`` in this last polynomial.

We return the transformed expression, and we record the defining
equations for new symbols using the ``update_mapping()`` function.

rC   r6   r   r   z*%s does not seem to be an algebraic number)r   r   ImaginaryUnitrh   r   r   r   r   r	   ri   r   r  rj   r"   rH   rP   expandrk   rp   r   r   r   minpoly_of_elementr   )r   r   minpoly_baseinversebase_invrj   r   rv   bottom_up_scanr  r  r8   r  rU   s           r@   r  )_minpoly_groebner.<locals>.bottom_up_scan  s   8 ::Q__$W$)"a33"2;&	  YYRWW>W.+W>??YYRWW>W.+W>??YYYvv!!!66A:#4RWWa#EL$Q5==?G&||Arww7>>@Hvv|-h77%0vv(()668Xa5J  !##%d+yw&~~#{{},-dAGdUCC"4=(1 "2 "" %b*?*?*ABBr{"G"LMMG ?>s   4J.Jc                    U R                   (       aJ  SU R                  -  R                  (       a,  U R                  S:  a  U R                  R                  (       a  gU R
                  (       ar  SnU R                   HX  nUR                  (       a    gUR                   (       d  M)  UR                  R                  (       d  MF  UR                  S:  d  MX    g   U(       a  gg)zg
Returns True if it is more likely that the minimal polynomial
algorithm works better with the inverse
r6   r   TF)ri   r   r   rj   r   r   r   )r   hitrp   s      r@   simpler_inverse*_minpoly_groebner.<locals>.simpler_inverse4  s    
 99"&&$$!77>>99CWW88 888vv}}}$  rr   Fr   r6   lex)orderr   r<   )r0   r   r   r   r  rH   rh   r   rp   ri   r   rk   r   rj   rq   r   r   r  r   r$   r#   rd   r   r   r&   r   )r   rU   r  r  invertedr   r   rK   busr   Gr?   rT   r  r  r  r8   r  s    ``          @@@@@r@   r  r    s    !%0I2GW	 	HN HNT, H	B	B	$$&..q11	a"$$"2&RB99!BFF(.."&&A(!Q7C2(QUUA6C?F; $CS	D!122AD!12aS8FA$QrU+JA#GQ3F&!$<<6&!,,-1(FMrr   c                     [        XX#US9$ )z6This is a synonym for :py:func:`~.minimal_polynomial`.)rU   r   r  r   )r   )r   rU   r   r  r   s        r@   minpolyr"  o  s     bwFSSrr   )NNr<   )NTFN)]__doc__	functoolsr   sympy.core.addr   sympy.core.exprtoolsr   sympy.core.functionr   r   r   sympy.core.mulr	   sympy.core.numbersr
   r   r   r   sympy.core.singletonr   sympy.core.symbolr   sympy.core.sympifyr   sympy.core.traversalr   &sympy.functions.elementary.exponentialr   (sympy.functions.elementary.miscellaneousr   r   (sympy.functions.elementary.trigonometricr   r   r   sympy.ntheory.factor_r   sympy.utilities.iterablesr   sympy.polys.domainsr   r   r   sympy.polys.orthopolysr   sympy.polys.polyerrorsr   r   sympy.polys.polytoolsr    r!   r"   r#   r$   r%   r&   r'   r(   r)   sympy.polys.polyutilsr*   r+   sympy.polys.ring_seriesr,   sympy.polys.ringsr-   sympy.polys.rootoftoolsr.   sympy.polys.specialpolysr/   sympy.utilitiesr0   r1   r2   rd   rq   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r"  r{   rr   r@   <module>r=     s   0   ( H H  : : " # & 3 6 ? B B * - 5 5 1   A 2 " + 4 
 ')s! -Z`=?B4lL^2j		#KLK>K0!KHZz YF YFx_D T Trr   