+
    il                     2   R t ^ RIHt ^ RIHt ^ RIHt ^ RIHtH	t	H
t
 ^ RIHt ^ RIHtHtHtHt ^ RIHt ^ RIHt ^ R	IHt ^ R
IHt ^ RIHt ^ RIHtHt ^ RIH t H!t!H"t" ^ RI#H$t$ ^ RI%H&t& ^ RI'H(t(H)t)H*t* ^ RI+H,t, ^ RI-H.t.H/t/ ^ RI0H1t1H2t2H3t3H4t4H5t5H6t6H7t7H8t8H9t9H:t: ^ RI;H<t<H=t= ^ RI>H?t? ^ RI@HAtA ^ RIBHCtC ^ RIDHEtE ^ RIFHGtGHHtHHItI ])^^3R ltJR tKR tLR tMR.R ltNR  tOR! tPR/R" ltQR# tRR$ tSR% tTR& tUR' tVR( tWR) tXR* tY]HR0R+ l4       tZR, t[]HR0R- l4       t\R# )1z*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siftc           
        \        V ^ ,          \        4      '       d   V  Uu. uF  qf^ ,          NK  	  p p\        V 4      ^8X  d
   V ^ ,          # ^
p/ p\        VR4      '       d   VP                  M. p	Wt8:  Edd   V  Uu. uF"  qfP                  4       P                  W/4      NK$  	  p
pVP                  '       d    V
 Uu. uF  qfP                  V4      NK  	  p
p\        \        V4      \        V	4      RR7       F  p\        W4       F	  w  rWV&   K  	  \        V
4       UUu. uF0  w  r\        VP                  V4      P                  V4      4      V3NK2  	  ppp\        ;QJ d    R V 4       F  '       g   K   RM	  RM! R V 4       4      '       d   K  \!        V4      pVR,          w  w  ppw  ppVVR,          8  g   K  V V,          u # 	  V^,          pEKj  \#        RV,          4      hu upi u upi u upi u uppi )	zY
Return a factor having root ``v``
It is assumed that one of the factors has root ``v``.
symbolsT)k
repetitionc              3   8   "   T F  w  rV\         9   x  K  	  R # 5iN)r   ).0i_s   &  ڀ/Users/tonyclaw/.openclaw/workspace/skills/math-calculator/venv/lib/python3.14/site-packages/sympy/polys/numberfields/minpoly.py	<genexpr>!_choose_factor.<locals>.<genexpr>H   s     8ZTQ1=Zs   F:N   Nz4multiple candidates for the minimal polynomial of %si@B )
isinstancetuplelenhasattrr3   as_exprxreplace	is_numbernr   rangezip	enumerateabssubsanysortedNotImplementedError)factorsxvdomprecboundfprec1pointsr3   ferF   sr9   
candidatescanaixbr:   s   &&&&&&              r;   _choose_factorr_   (   s    '!*e$$!()AQ44)
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;(  *
 s8Z8sss8Z888
 $C!"gOGQVa1u9}r{"' H* 	

TWXX
YYM * <(*s   G.=(G3=G8!6G=c                     \         ;QJ d0    R  \        P                  ! V 4       4       F  '       d   K   R# 	  R# ! R  \        P                  ! V 4       4       4      # )c              3   J  "   T F  p\         P                  ! V4       F|  pVP                  ;'       gd    VP                  ;'       dP    VP                  P                  ;'       d2    ^VP
                  ,          P                  ;'       d    VP                  x  K~  	  K  	  R# 5i)r>   N)r   	make_argsis_Rationalis_Powbaser   
is_Integeris_extended_real)r8   trU   s   &  r;   r<    _is_sum_surds.<locals>.<genexpr>Y   s      =!A3==+;a }} K K !K !K	!K !K !!%%33!K !K898J8JK+;K!s   0B#B#B#%$B#
B#FT)allr   rb   )ps   &r;   _is_sum_surdsrl   X   sO    3 =q!=33 =3 =3 =q!= = =    c                   R p. pV P                    EF  pVP                  '       g   V! V4      '       d+   VP                  \        P                  V^,          34       KN  VP
                  '       d$   VP                  V\        P                  34       K  VP                  '       d@   VP                  P                  '       d$   VP                  V\        P                  34       K  \        h\        VP                   VRR7      w  rEVP                  \        V!  \        V!  ^,          34       EK  	  VP                  R R7       VR,          ^,          \        P                  J d   V # V UUu. uF  w  r6VNK	  	  ppp\        \        V4      4       F  pWx,          ^8w  g   K   M	  ^ RIHp	 V	! VXR !  w  rp. p. pV Fe  w  r6Wk9   d/   VP                  W6\        P"                  ,          ,          4       K9  VP                  W6\        P"                  ,          ,          4       Kg  	  \%        V!  p\%        V!  p\'        V^,          4      \'        V^,          4      ,
          p V # u uppi )	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                 b    V P                   ;'       d    V P                  \        P                  J # r7   )rd   r   r   Half)exprs   &r;   is_sqrt_separate_sq.<locals>.is_sqrtx   s!    {{11txx16611rm   T)binaryc                     V ^,          # )    )zs   &r;   <lambda>_separate_sq.<locals>.<lambda>   s    1rm   )key)
_split_gcdN)argsis_Mulappendr   Oneis_Atomrd   r   
is_integerrN   r1   r   sortrG   rA   sympy.simplify.radsimpr|   rp   r   r   )rk   rr   r\   yTFrx   surdsr9   r|   gb1b2a1a2p1p2s   &                r;   _separate_sqr   ^   s   42 	AVVxxx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   /I'c                   \        V 4      p \        V4      pVP                  '       d   V^ 8  d   \        V 4      '       g   R# V \        ^V4      ,          pW,          p  \	        V 4      pW@J d   VP                  W"V,          /4      p MTp K/  V^8X  dS   \        V 4      pV P                  W$P                  V4      ,          4      ^ 8  d   V ) p V P                  4       ^,          p V # \        V 4      ^,          p\        WRV4      pV# )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

N)r   rf   rl   r
   r   rK   r   coeffr%   	primitiver"   r_   )rk   rF   rP   pnr   rO   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Mrm   Nc                v   \        \        V4      4      pVf   \        WV4      pVf   \        W'V4      pMVP                  W7/4      pV \        J d   V\
        8X  dD   \        R\
        4      w  rV! \        V4      ^ ,          4      p
V! \        V4      ^ ,          4      pM]\        WSV,
          3W74      w  w  rpV
P                  V4      pVP                  4       pM"V \        J d   \        WSV4      pM\        R4      hV \        J g   V\
        8w  d   \        XWgV.R7      pM&\        X
X4      p\!        VP#                  4       V4      p\%        WS4      p\%        Wg4      pV \        J d   V^8X  g   V^8X  d   V# \'        WVR7      pVP)                  4       w  pp\+        VW0! W4      V4      pVP                  4       # )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".

Xzoption not availablegensdomain)r   str_minpoly_composerK   r   r   r,   r)   r'   composerC   r   _mulyrN   r$   r+   r*   as_expr_dictr%   r   r"   r_   )opex1ex2rP   rR   mp1mp2r   Rr   r   r   r:   rmp1adeg1deg2rO   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;;=rm   c                    \        W4      ^ ,          p\        V4      pVP                  4        UUu. uF  w  w  rEWQW4,
          ,          ,          NK  	  ppp\        V!  # u uppi )z8
Returns ``expand_mul(x**degree(p, x)*p.subs(x, 1/x))``
r&   r%   termsr   )rk   rP   r   rF   r9   cr\   s   &&     r;   _invertxr   $  sR     
	a	 Br
A')xxz2zGDQZzA27N 	3s   "Ac                    \        W4      ^ ,          p\        V4      pVP                  4        UUu. uF*  w  w  rVWaV,          ,          W$V,
          ,          ,          NK,  	  ppp\        V!  # u uppi )z0
Returns ``_mexpand(y**deg*p.subs({x:x / y}))``
r   )rk   rP   r   r   rF   r9   r   r\   s   &&&     r;   r   r   /  s\     
	a	 Br
A.0hhj9j74ATAAJ		jA97N 	:s   0A-c                :   \        V4      pV'       g   \        WV4      pVP                  '       g   \        RV ,          4      hV^ 8  d9   WB8X  d   \	        RV ,          4      h\        WB4      pVR8X  d   V# V) p^V ,          p \        \        V4      4      pVP                  W%/4      pVP                  4       w  rg\        \        WBV,          WV,          ,
          V.R7      W#R7      pVP                  4       w  r\        WW,          V4      pVP                  4       # )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 elementz
%s is zeror   r   r}   )r   r   is_rationalr   ZeroDivisionErrorr   r   r   rK   as_numer_denomr   r$   r"   r_   rC   )expwrP   rR   mpr   rF   dr   r:   rO   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;;=rm   c           
         \        \        V^ ,          V^,          W4      pV^ ,          V^,          ,           pVR,           F  p\        \        WEWVR7      pWE,           pK   	  V# )z&
returns ``minpoly(Add(*a), dom, x)``
r>   NNr   )r   r   rP   rR   r\   r   rk   pxs   &&*   r;   _minpoly_addr   o  [     
'sAaD!A$	?B	!qtAee*3q2FF  Irm   c           
         \        \        V^ ,          V^,          W4      pV^ ,          V^,          ,          pVR,           F  p\        \        WEWVR7      pWE,          pK   	  V# )z&
returns ``minpoly(Mul(*a), dom, x)``
r   r   )r   r   r   s   &&*   r;   _minpoly_mulr   {  r   rm   c                    V P                   ^ ,          P                  4       w  r#V\        J Ed   VP                  '       Ed   VP                  p\        V4      pVP                  '       dS   \        V\        4      p\        \        V4       Uu. uF%  qaWF,
          ^,
          ,          W6,          ,          NK'  	  up!  # VP                  ^8X  dI   V^	8X  dB   ^@V^,          ,          ^`V^,          ,          ,
          ^$V^,          ,          ,           ^,
          # V^,          ^8X  do   \        V\        4      p\        V^,           4       Uu. uF  qaWF,
          ,          W6,          ,          NK   	  pp\        V!  p\        V4      w  r\        WV 4      p
V
# ^\        ^V,          \        ,          4      ,
          ^,          \        P                   ,          p\#        W\$        4      p
V
# \'        RV ,          4      hu upi u upi )zi
Returns the minimal polynomial of ``sin(ex)``
see https://mathworld.wolfram.com/TrigonometryAngles.html
r   )r~   as_coeff_Mulr   r   qr   is_primer   r   r   rG   rk   r"   r_   r   r   rp   r   r   r   )r   rP   r   r\   rF   r   r9   r   r:   rO   r   rq   s   &&          r;   _minpoly_sinr     sz   
 771:""$DABw===A
Azzz #1b)%(C(Q^AD0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   +G6$G;c           	        V P                   ^ ,          P                  4       w  r#V\        J Ed   VP                  '       Ed   VP                  ^8X  d   VP
                  ^8X  d;   ^V^,          ,          ^V^,          ,          ,
          ^V,          ,
          ^,           # VP
                  ^	8X  d&   ^V^,          ,          ^V,          ,
          ^,
          # MvVP                  ^8X  df   \        VP
                  4      pVP                  '       d?   \        W4      p\        VP                  V\        ^V,
          ^,          4      /4      4      # \        VP
                  4      p\        V\        4      p\        V^,           4       Uu. uF  qqWg,
          ,          W7,          ,          NK   	  pp\!        V!  RVP                  ,          ,
          p\#        V4      w  r\%        WV 4      pV# \'        RV ,          4      hu upi )zi
Returns the minimal polynomial of ``cos(ex)``
see https://mathworld.wolfram.com/TrigonometryAngles.html
r   r}   )r~   r   r   r   rk   r   r   r   r   r   rK   r   intr   r   rG   r   r"   r_   r   )r   rP   r   r\   r   rY   rF   r9   r   r:   rO   r   s   &&          r;   _minpoly_cosr     sp   
 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   $G8c                   V P                   ^ ,          P                  4       w  r#V\        J Ed   VP                  '       d   V^,          p\	        VP
                  4      pVP                  ^,          ^ 8X  d   TM^p. p\        VP                  ^,           ^,          V^,           ^4       Fa  pVP                  W1V,          ,          4       W4V,
          ^,
          ,          WF,
          ,          ) V^,           V^,           ,          ,          pKc  	  \        V!  p\        V4      w  r\        WV 4      p
V
# \        RV ,          4      h)z_
Returns the minimal polynomial of ``tan(ex)``
see https://github.com/sympy/sympy/issues/21430
r   )r~   r   r   r   r   r   rk   rG   r   r   r"   r_   r   )r   rP   r   r\   rF   r   r4   r   r:   rO   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Jrm   c                   V P                   ^ ,          P                  4       w  r#V\        \        ,          8X  Ed   VP                  '       Ed   \        VP                  4      pVP                  ^8X  g   VP                  R8X  Ed   V^8X  d   V^,          V,
          ^,           # V^8X  d   V^,          ^,           # V^8X  d   V^,          V^,          ,
          ^,           # V^8X  d   V^,          ^,           # V^	8X  d   V^,          V^,          ,
          ^,           # V^
8X  d;   V^,          V^,          ,
          V^,          ,           V^,          ,
          ^,           # VP                  '       d'   ^ p\        V4       F  pWQ) V,          ,          pK  	  V# \        ^V,          4       Uu. uF  p\        Wa4      NK  	  pp\        WqV 4      pV# \        RV ,          4      h\        RV ,          4      hu upi )z/
Returns the minimal polynomial of ``exp(ex)``
r   r}   )r~   r   r	   r   r   r   r   rk   r   rG   r   r.   r_   r   )	r   rP   r   r\   r   rY   r9   rO   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   )G1c                    V P                   pVP                  V P                  P                  ^ ,          V/4      p\	        W!4      w  r4\        WAV 4      pV# )z9
Returns the minimal polynomial of a ``CRootOf`` object.
)rq   rK   polyr   r"   r_   )r   rP   rk   r:   rO   r   s   &&    r;   _minpoly_rootofr     sI     	A	Q"#AQ"JAG+FMrm   c           
     D   V P                   '       d%   V P                  V,          V P                  ,
          # V \        J dK   \	        V^,          ^,           WR7      w  r4\        V4      ^8X  d   V^,          ^,           # V\        ,
          # V \        P                  J dq   \	        V^,          V,
          ^,
          WR7      w  r4\        V4      ^8X  d   V^,          V,
          ^,
          # \        WA^\        ^4      ,           ^,          VR7      # V \        P                  J d   \	        V^,          V^,          ,
          V,
          ^,
          WR7      w  r4\        V4      ^8X  d&   V^,          V^,          ,
          V,
          ^,
          # ^\        ^^\        ^!4      ,          ,
          4      ,           \        ^^\        ^!4      ,          ,           4      ,           ^,          p\        WAWRR7      # \        VR4      '       d   WP                  9   d	   W,
          # VP                  '       dM   \        V 4      '       d<   T pW,          p  \!        V 4      pWpJ d   \        \	        V 4      ^,          W4      # Tp K1  V P"                  '       d   \%        W.V P&                  O5!  pV# V P(                  '       Ed   \+        V 4      P,                  p	\/        V	P1                  4       R 4      p
V
R,          '       Ed   V\2        8X  Ed   \5        V
R,          V
R,          ,            UU u. uF  w  rW,          NK  	  up p!  p\7        V
R,          4      pVP9                  4        Uu. uF  qP                  NK  	  pp\;        \<        V^4      p\        P>                  pVPA                  V\        PB                  4      pVP1                  4        UUu. uF1  w  ppVVP                  V,          VP                  ,          ,          NK3  	  ppp\5        V!  p\E        Wq4      pVP                  W,          ,          VP                  VVV,          ,          ,          ,
          pVV,          V\G        ^V4      ,          ,          p\I        \4        VVWVVR7      pV# \K        W.V P&                  O5!  p V# V PL                  '       d$   \O        V PP                  V PR                  W4      pV# V PT                  \V        J d   \Y        W4      pV# V PT                  \Z        J d   \]        W4      pV# V PT                  \^        J d   \a        W4      pV# V PT                  \R        J d   \c        W4      pV# V PT                  \d        J d   \g        W4      pV# \i        R	V ,          4      hu up pi u upi u uppi )
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

r   )rR   r3   c                 ^    V ^ ,          P                   ;'       d    V ^,          P                   # )    )rc   )itxs   &r;   ry   "_minpoly_compose.<locals>.<lambda>J  s#    A(:(:(Q(Qs1v?Q?Q(Qrm   TFN)r   r   r   )5rc   r   rk   r	   r"   rA   r   GoldenRatior_   r   TribonacciConstantr   rB   r3   is_QQrl   r   is_Addr   r~   r   r   rO   r1   itemsr   r   dictvaluesr   r(   NegativeOnepopZerominimal_polynomialr
   r   r   rd   r   re   r   	__class__r   r   r   r   r   r   r   r-   r   r   )r   rP   rR   r:   rO   facrQ   r   r   rU   r   bxr1r   denslcmdensneg1expn1re   numsr   r   r   s   &&&                    r;   r   r     sS     
~~~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
yyy]2&&
r"Cy%k"oa&8!??	yyy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   V
V7Vc                   \        V 4      p V P                  '       d   \        V RR7      p \        V 4       F  pVP                  '       g   K  Rp M	  Ve   \        V4      \
        raM\        R4      \        raV'       g=   V P                  '       d%   \        \        \        V P                  4      4      pM\        p\        VR4      '       d$   WP                  9   d   \        RV: RV: 24      hV'       d   \        WV4      pVP!                  4       ^,          pVP#                  V\%        Wq4      ,          4      pVP&                  '       d   \)        V) 4      pV'       d   V! WqRR7      # VP+                  V4      # VP,                  '       g   \/        R	4      h\1        WV4      pV'       d   V! WqRR7      # VP+                  V4      # )
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)	recursiveFrP   r3   zthe variable z$ is an element of the ground domain )fieldz!groebner method only works for QQ)r   rE   r   r   is_AlgebraicNumberr   r   r    free_symbolsr   r   listrB   r3   r   r   r   r   r%   is_negativer   collectr   rN   _minpoly_groebner)	r   rP   r   polysr   rq   clsr   r   s	   &&&&&    r;   r   r   p  sb   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Erm   c                H  aaaaaaa \        R\        R7      o/ / uooR
VVV3R lloVVVVVV3R loR pRp\        V 4      p V P                  '       d    V P	                  4       P                  S4      # V P                  '       d'   V P                  S,          V P                  ,
          pEM/V! V 4      pV'       d
   V R,          p RpV P                  '       dO   ^V P                  ,          P                  '       d,   ^V P                  ,          p\        V P                  VS4      pM,\        V 4      '       d   \        V \        P                   S4      pVe   TpVf   S! V 4      pSV,
          .\#        SP%                  4       4      ,           p	\'        V	\#        SP%                  4       4      S.,           RR	7      p
\)        V
R,          4      w  r\+        VSV 4      pV'       d@   \-        XS4      pVP/                  S\1        VS4      ,          4      ^ 8  d   \3        V) 4      pX# )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

r\   )r   Nc                    < \        S4      pVSV &   Ve   W1,          V,           SV &   V# VP                  ! V4      SV &   V# r7   )nextrC   )r   r   re   r\   	generatormappingr3   s   &&& r;   update_mapping)_minpoly_groebner.<locals>.update_mapping  sI    O&4-GBK  ++a.GBKrm   c                  < V P                   '       dE   V \        P                  J d   V S
9  d   S! V ^^4      # SV ,          # V P                  '       d   V # EMSV P                  '       d*   \        V P                   Uu. uF  pS! V4      NK  	  up!  # V P                  '       d*   \        V P                   Uu. uF  pS! V4      NK  	  up!  # V P                  '       Ed   V P                  P                  '       Edu   V P                  ^ 8  d   \        V P                  SS	4      p\        SV4      P                  4       pVP                  SV P                  4      P!                  4       pV P                  R8X  d	   S! V4      # W@P                  ) ,          p V P                  P"                  '       gV   V P                  V P                  P$                  ,          P!                  4       \'        ^V P                  P(                  4      reMV P                  V P                  reS! V4      pWV,          pVS
9  d5   VP"                  '       d   VP!                  4       # S! V^V,          V) 4      # SV,          # M8V P*                  '       d'   V S
9  d   S! W P-                  4       4      # SV ,          # \/        RV ,          4      hu upi u upi )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.

z*%s does not seem to be an algebraic numberr}   )r   r   ImaginaryUnitrc   r   r   r~   r   r   rd   r   r   re   r!   rC   rK   expandrf   rk   r
   r   r   minpoly_of_elementr   )r   r   minpoly_baseinversebase_invre   r   rq   bottom_up_scanr   r   r3   r   rP   s   &       r;   r	  )_minpoly_groebner.<locals>.bottom_up_scan  s#   8 :::Q__$W$)"a33"2;&	  YYYRWW>W.+W>??YYY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   =J=8Kc                   V P                   '       dS   ^V P                  ,          P                  '       d0   V P                  ^ 8  d   V P                  P                  '       d   R# V P
                  '       d{   RpV P                   F^  pVP                  '       d    R# VP                   '       g   K,  VP                  P                  '       g   KJ  VP                  ^ 8  g   K]   R# 	  V'       d   R# R# )zg
Returns True if it is more likely that the minimal polynomial
algorithm works better with the inverse
TF)rd   r   r   re   r   r   r~   )r   hitrk   s   &  r;   simpler_inverse*_minpoly_groebner.<locals>.simpler_inverse4  s    
 999"&&$$$!77>>>999CWW888 888vv}}}$  rm   Flex)orderr7   r}   )r/   r   r   r   r  rC   rc   r   rk   rd   r   rf   r   re   rl   r   r   r   r   r#   r"   r_   r   r   r%   r   )r   rP   r   r  invertedr   r   rF   busr   Gr:   rO   r	  r   r   r3   r   s   &ff          @@@@@r;   r   r     s    !%0I2GW	 	HN HNT, H	B	B	$$&..q11	a"$$"2&RB999!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Mrm   c                    \        WW#VR7      # )z6This is a synonym for :py:func:`~.minimal_polynomial`.)rP   r   r   r   )r   )r   rP   r   r   r   s   &&&&&r;   minpolyr  o  s     bwFSSrm   )NNr7   )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.utilitiesr/   r0   r1   r_   rl   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  rw   rm   r;   <module>r0     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rm   