
    wg                        d Z ddlZddlmZ ddlmZmZmZ ddlm	Z	 ddl
mZ ddl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mZ ddlmZ ddlmZmZmZ ddl m!Z! ddl"m#Z#m$Z$m%Z%m&Z&m'Z' ddl(m)Z)m*Z* ddl+m,Z,m-Z-m.Z. ddl/m0Z0 ddl1m2Z2m3Z3m4Z4m5Z5 ddl6m7Z7 ddl8m9Z9m:Z:m;Z;m<Z<m=Z= ddl>m?Z? ddl@mAZA ddlBmCZC ddlDmEZE  ed      ZFd ZGd ZHd.dZId  ZJd! ZKd" ZLd# ZMd.d$ZNd% ZOd& ZPd' ZQd( ZReCd)d)dd)dddddd*	d+       ZSdd,d-ZTy)/z8Algorithms for computing symbolic roots of polynomials.     N)reduce)SIpi)factor_terms)_mexpand)	fuzzy_not)expand_2argMul)igcd)Rationalcomp)Pow)Eq)ordered)DummySymbolsymbols)sympify)expimcosacos	Piecewise)rootsqrt)divisorsisprime	nextprime)EX)PolynomialErrorGeneratorsNeededDomainErrorUnsolvableFactorError)PolyQuintic)Polycancelfactorgcd_listdiscriminant)together)cyclotomic_poly)public)
filldedentzc                     | j                  d       | j                  d      z  }| j                         }|j                  s(|j                  rt	        |      }|gS ddlm}  ||      }|gS )z/Returns a list of roots of a linear polynomial.r      simplify)nth
get_domainis_Numericalis_Compositer(   sympy.simplify.simplifyr3   )frdomr3   s       Z/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/polys/polyroots.pyroots_linearr=   %   sb    	
q	!%%(A
,,.Cq	A
 3J 9A3J    c                    | j                         \  }}}| j                         d }fd}|t        j                  u rBt        j                  | |z  }}j                  s ||      }||gS |j
                  r||}}||gS |t        j                  u r+| |z  }j                  s ||      } ||      }	|	 }|	}||gS |dz  d|z  |z  z
  }
d|z  }| |z  }j                  s ||
      }
 ||      }t         ||
      |z        }||z
  }||z   }|j
                  r||}}||gS j                  s||fD cg c]  }t        |       c}\  }}||gS c c}w )aP  Returns a list of roots of a quadratic polynomial. If the domain is ZZ
    then the roots will be sorted with negatives coming before positives.
    The ordering will be the same for any numerical coefficients as long as
    the assumptions tested are correct, otherwise the ordering will not be
    sorted (but will be canonical).
    c                    g }g }t        j                  |       D ]z  }|j                  r[|j                  j                  rE|j                  dz  dk(  r3|j                  t        |j                  |j                  dz               j|j                  |       | |rt        | } t        | }|t        |       z  S t        |       S )N   r   )	r   	make_argsis_Powr   
is_Integerappendr   baser   )dcootherdis       r<   _sqrtzroots_quadratic.<locals>._sqrt?   s     --" 	!ByyRVV..266A:?		#bggrvvqy12R 		!
 UAbBd1g:Awr>   c                 N    j                   rt        |       S ddlm}  ||       S )Nr   r2   )r7   r(   r8   r3   )exprr3   r;   s     r<   	_simplifyz"roots_quadratic.<locals>._simplifyP   s#    $<8D>!r>   rA      )
all_coeffsr5   r   Zeror6   is_negativer   r
   )r9   abcrK   rN   r0r1r:   RrG   ABDir;   s                  @r<   roots_quadraticr]   4   s    llnGAq!
,,.C"" 	AFF{!AB2B8 8O7 ^^B4 8O3 
affBqD!A!HR$ 8O! qD1Q3q5LaCBqD!A!Aq!$UU==B 8O !!/12h7k!n7FB8O 8s   8EFc                    |r8| j                         \  }}}}d|z  |z  |dz  z
  d|dz  z  z  }d|dz  z  d|z  |z  |z  z
  d|dz  z  |z  z   d|dz  z  z  }d|z  |z  |z  |z  d|dz  z  |z  z
  |dz  |dz  z  z   d|z  |dz  z  z
  d|dz  z  |dz  z  z
  }|dkD  dk(  rg }	t        d      D ]q  }
|	j                  dt        | dz        z  t	        t        ||z  t        d	|z        z  t        dd      z        dz  |
t        z  t        dd      z  z
        z         s |	D cg c]  }||dz  |z  z
   c}S | j                         j                         \  }}}}|t        j                  u r&t        d
||gd      \  }}|t        j                  |gS ||dz  dz  z
  }|||z  dz  z
  d|dz  z  dz  z   }|dz  }|dz  }d}|t        j                  u r@|t        j                  u r| gdz  S |j                  rt        |d       nt        | d      }n|t        j                  u r7t        d
d|gd      \  }}|t        j                  |fD cg c]  }||z
  	 c}S |j                  r5|j                  r)t        | dz  t        |dz  dz  |dz  z         z   d       }t         t        d      z  dz  }|t        j"                  }t        dd      |z   }t        dd      |z
  }|||}}}|dz  d|z  z
  }d|dz  z  d|z  |z  z
  d|z  z   }t        |t        |dz  d|dz  z  z
        z   dz  d      }|||fD cg c]  }|||z  z   ||z  |z  z    dz   c}S |t        dd      |z   z  }|t        dd      |z
  z  }|t        j                  u r||z
  ||z
  ||z
  gS | ||z  z   |z
  | ||z  z   |z
  | ||z  z   |z
  g}|S c c}w c c}w c c}w )zReturns a list of roots of a cubic polynomial.

    References
    ==========
    [1] https://en.wikipedia.org/wiki/Cubic_function, General formula for roots,
    (accessed November 17, 2014).
       rA   	         rO   r   Tr1   multipleN)rP   rangerE   r   r   r   r   r   monicr   rQ   rootsis_positiver   is_realrR   r   One)r9   trigrS   rT   rU   rG   pqr[   rvkr\   _x1x2pon3aon3u1y1y2tmpcoeffu2u3D0D1Cuksolns                                r<   roots_cubicr   z   sY    \\^
1aqSUQT\AadF#q!tVac!eAg1a4	)Bq!tG4qDF1HQJ1a4!AqDAI-!AqD82ad71a4<GEd?B1X i		!D!AJ,s4!DAJx1~0M+Nq+PSTUWSWX`abdeXfSf+f'gghi')*!A!AI** %%'JAq!QAFF{1ay40BAFFB 	
AqDF
A	AaCE	AadF2IAQ3DQ3D	BAFF{;E719MMd1aj[tQB{	
aff1ay40B')1662&67sd
77	
q}}A2a4$q!tAva/00!44d1gIaKE	zUUb!_u$b!_u$Qa1TAaCZq!tVac!e^bd""tBEAb!eGO,,a/335r2,?B!bd(RT"W$%a'??	Xb!_u$	%B	Xb!_u$	%BAFF{T	29b4i00 
d2g	d2g	d2gD Ke +. 8 @s   'OO	<Oc                    t        d      }d|dz  z  d| z  |dz  z  z   d| dz  z  d|z  z
  |z  z   |dz  z
  }t        t        t        ||      d	      j	                               }|D cg c]  }|j
                  s|j                  s|  }}|sy
t        |      }t        |      }	| |	z  d|z  z  }
| | dz  z
  }t        ||
z         }t        ||
z
        }|	|z
  |z
  |	 |z
  |z
  |	 |z   |z
  |	|z   |z
  gS c c}w )al  
    Descartes-Euler solution of the quartic equation

    Parameters
    ==========

    p, q, r: coefficients of ``x**4 + p*x**2 + q*x + r``
    a: shift of the roots

    Notes
    =====

    This is a helper function for ``roots_quartic``.

    Look for solutions of the form ::

      ``x1 = sqrt(R) - sqrt(A + B*sqrt(R))``
      ``x2 = -sqrt(R) - sqrt(A - B*sqrt(R))``
      ``x3 = -sqrt(R) + sqrt(A - B*sqrt(R))``
      ``x4 = sqrt(R) + sqrt(A + B*sqrt(R))``

    To satisfy the quartic equation one must have
    ``p = -2*(R + A); q = -4*B*R; r = (R - A)**2 - B**2*R``
    so that ``R`` must satisfy the Descartes-Euler resolvent equation
    ``64*R**3 + 32*p*R**2 + (4*p**2 - 16*r)*R - q**2 = 0``

    If the resolvent does not have a rational solution, return None;
    in that case it is likely that the Ferrari method gives a simpler
    solution.

    Examples
    ========

    >>> from sympy import S
    >>> from sympy.polys.polyroots import _roots_quartic_euler
    >>> p, q, r = -S(64)/5, -S(512)/125, -S(1024)/3125
    >>> _roots_quartic_euler(p, q, r, S(0))[0]
    -sqrt(32*sqrt(5)/125 + 16/5) + 4*sqrt(5)/5
    x@   r_       rA   rO      F)cubicsN)	r   listri   r&   keysis_rational
is_nonzeromaxr   )rn   ro   r:   rS   r   eqxsolssolrX   c1rZ   rY   c2c3s                 r<   _roots_quartic_eulerr      s   R 	c
A	AqD2a419	!Q$Aq0	01a4	7BtB{516689E!HSS__SHEHE
A	aB	
2qsA	
QqSA	a!eB	a!eBGaK"rAsRx!|R"Wq[AA Is   *C9<C9	C9c                      j                         j                         \  }}}}}|s#t        j                  gt	        d|||gd      z   S ||z  dz  |k(  r j
                  ||z  }}t        |dz  ||z  z   |z   d|z  z
  |      }t        |      \  }	}
t        |dz  |	|z  z
  |z   |      }t        |dz  |
|z  z
  |z   |      }t        |      }t        |      }||z   S |dz  }|d|z  dz  z
  t        |||dz  |dz  z
  z  z          |dz  t        ||d|z  dz  |dz  z
  z  |z   z  z
        } j                  rCt	        d|gd      D cg c]  }t        |       c}\  }}| | ||fD cg c]  }|z
  	 c}S |j                  r6t        j                  gt	        dd	 gd      z   }|D cg c]  }|z
  	 c}S t         |      }|r|S dz   d
z  |z
  }dz   dz  |z  dz  z    dz  dz  z
  }t        dd      } fd}t        |      }t        dd      z  ||z  z
  }|j                  r ||      S t        |dz  dz  |dz  dz  z         }| dz  |z   }||z  }t        dd      z  |z   ||z  dz  z
  }t        |j                        r ||      S t         ||       ||            D cg c]  \  }}t        |t!        |d	      f|df      ! c}}S c c}w c c}w c c}w c c}}w )a  
    Returns a list of roots of a quartic polynomial.

    There are many references for solving quartic expressions available [1-5].
    This reviewer has found that many of them require one to select from among
    2 or more possible sets of solutions and that some solutions work when one
    is searching for real roots but do not work when searching for complex roots
    (though this is not always stated clearly). The following routine has been
    tested and found to be correct for 0, 2 or 4 complex roots.

    The quasisymmetric case solution [6] looks for quartics that have the form
    `x**4 + A*x**3 + B*x**2 + C*x + D = 0` where `(C/A)**2 = D`.

    Although no general solution that is always applicable for all
    coefficients is known to this reviewer, certain conditions are tested
    to determine the simplest 4 expressions that can be returned:

      1) `f = c + a*(a**2/8 - b/2) == 0`
      2) `g = d - a*(a*(3*a**2/256 - b/16) + c/4) = 0`
      3) if `f != 0` and `g != 0` and `p = -d + a*c/4 - b**2/12` then
        a) `p == 0`
        b) `p != 0`

    Examples
    ========

        >>> from sympy import Poly
        >>> from sympy.polys.polyroots import roots_quartic

        >>> r = roots_quartic(Poly('x**4-6*x**3+17*x**2-26*x+20'))

        >>> # 4 complex roots: 1+-I*sqrt(3), 2+-I
        >>> sorted(str(tmp.evalf(n=2)) for tmp in r)
        ['1.0 + 1.7*I', '1.0 - 1.7*I', '2.0 + 1.0*I', '2.0 - 1.0*I']

    References
    ==========

    1. http://mathforum.org/dr.math/faq/faq.cubic.equations.html
    2. https://en.wikipedia.org/wiki/Quartic_function#Summary_of_Ferrari.27s_method
    3. https://planetmath.org/encyclopedia/GaloisTheoreticDerivationOfTheQuarticFormula.html
    4. https://people.bath.ac.uk/masjhd/JHD-CA.pdf
    5. http://www.albmath.org/files/Math_5713.pdf
    6. https://web.archive.org/web/20171002081448/http://www.statemaster.com/encyclopedia/Quartic-equation
    7. https://eqworld.ipmnet.ru/en/solutions/ae/ae0108.pdf
    r1   Trd   rA   r_      rO   r   r      l   c                     t        	d| z  z         }d	z  d| z  z   }d
z  |z  }g }dD ];  }t        |||z  z          }dD ]"  }|j                  ||z  ||z  z
  dz  z
         $ = |S )NrA   r_   rf   r1   )r   rE   )ywarg1arg2anssr   taon4er9   s           r<   _anszroots_quartic.<locals>._ansP  s    QqSMsQqSys1u  <A$4- 01D$ <

AaC!D&L!#3d#:;<< 
r>      ra   )rh   rP   r   rQ   ri   genr&   r]   r   is_zeror   r   r   r	   zipr   r   )r9   rr   rS   rT   rU   rG   r   mgz1z2h1h2rW   r2a2rz   rx   ry   r   solsrn   ro   THr   r   r:   ua1r   r   s   `                            @@r<   roots_quarticr      sQ   ^ GGI((*MAq!Qx%Aq!t<<<
A#Quuac1A!a!A#%q) #B!Q$A+/1%!Q$A+/1%R R BwT"QJQBqD1Q3J'(sQq!B$r'AaC-01455699Q1I57Cd3i 7FB,.3R*<=3C$J==995!Q1==A*+,3C$J,, (1a6DAb1AAc	AaCE!AqDF*A!QB	 A 8B?"QU*ByyBx 1QAb()D1tA2A8B?"Q&1Q.B#Bx #&d2hR"9;B r2a8nr4j9 ; ;_7= -T;s   K&2K+4K0>$K5c                 l   | j                         }| j                  |      | j                  d      }}t        ||z         }t        ||      }|j                  r|j                  d      }|j                  }|dz  dk(  }|r|dk(  r|dz   j                  rd}nd}g }	|dz  }
|r|	j                  |
       |
dz  }
|s|	j                  d       t        |
dd      D ]-  }|r|	j                  || g       |	j                  | |g       / |rJ|	j                  d       r7t        dt        |	      d      D ]  }|	||dz    }t        t        |            }  g dt        z  t        z  |z  }}|	D ]D  }t!        ||z        j                  d      }|j                  ||z  j                  d             F |S )	aO  Returns a list of roots of a binomial polynomial. If the domain is ZZ
    then the roots will be sorted with negatives coming before positives.
    The ordering will be the same for any numerical coefficients as long as
    the assumptions tested are correct, otherwise the ordering will not be
    sorted (but will be canonical).
    r   TcomplexrA   r1   Frf   )
power_base)degreer4   r'   r   	is_numberexpandrR   rj   rE   rg   extendlenr   reversedr   r   r   )r9   nrS   rT   rF   alphanegevenbigksimaxr\   pairri   rG   rq   zetas                    r<   roots_binomialr   s  s    	

A558QUU1XqA1Q3K<DqMET* 

Cq5A:D
4<TAX22CC 
Ba4D
		$	
		!4B IIq1"gIIr1g	
 
		!1c"gq) ,!QU|HTN+,
 1Q3r6!81E <1Q3xt,eDj((E(:;< Lr>   c                    t        |       D cg c]  }t        |dz         s|dz    }}d\  }}|D ]  }||z  }||dz
  z  } | }t        t        j                  | t        |      |z  z              }dx}}g }||k  r't        |      }|j                  |       ||z  }||k  r'||z  }d}|dd D ]
  }||dz
  z  } t        t        j                  | t        |      |z  z              }||fS c c}w )z
    Find ``(L, U)`` such that ``L <= phi^-1(m) <= U``.

    Examples
    ========

    >>> from sympy.polys.polyroots import _inv_totient_estimate

    >>> _inv_totient_estimate(192)
    (192, 840)
    >>> _inv_totient_estimate(400)
    (400, 1750)

    r1   )r1   r1   rA   Nrf   )r   r   intmathceilfloatr   rE   )	r   rG   primesrS   rT   rn   LUPs	            r<   _inv_totient_estimater     s    'qk=WQU^q1u=F=DAq 	Q	QU
 	
ADIIaq!n%&AIAF
q&aLa	Q q&
 !GA	ACR[ 	QU
 	DIIaq!n%&Aa4K7 >s
   C/C/c                    t        | j                               \  }}t        ||dz         D ]5  t        | j                  d      }| j
                  |j
                  k(  s5 n t        d      g }|sdz  t        ddz         D cg c]  }t        |      dk(  s| }}|j                  fd       dt        z  t        z  z  }t        |      D ]/  }	|j                  t        |	|z        j                  d             1 |S t        | t!        d	      
      }t#        |j%                         d         D ]%  \  }
|j                  j'                                 ' |S c c}w )z)Compute roots of cyclotomic polynomials. r1   T)polysz/failed to find index of a cyclotomic polynomialrA   c                 6    | k  r| dfS t        | z
        dfS )Nrf   r1   )abs)r   hr   s    r<   <lambda>z"roots_cyclotomic.<locals>.<lambda>  s!    aq"g c!a%j!_ r>   )keyr   rf   )	extension)r   r   rg   r,   r   rM   RuntimeErrorr   sortr   r   r   rE   r   r   r&   r   r   factor_listTC)r9   r(   r   r   r   ri   r\   r   rG   rq   rr   r   r   s              @@r<   roots_cyclotomicr     sQ    ,DAq1a!e_ NAquuD166QVV	N LMME qDq!a%<ADAJ!Oa<<
DEaCF1H" 	8ALLQqS67	8 L d2qk*AMMOA./ 	"DAqLL!$$&!	" L =s   E+ E+c           
      f   g }| j                         \  }}}}}}t        d ||||||fD              s|S |dk7  r%t        | |z        } | j                         \  }}}}}}|r|d|z  |z  dz  z
  }	|d|z  |z  dz  z
  d|dz  z  dz  z   }
|d|z  |z  dz  z
  d|dz  z  |z  dz  z   d|dz  z  dz  z
  }|||z  dz  z
  |dz  |z  dz  z   |dz  |z  dz  z
  d|dz  z  d	z  z   }| j                  }t        |dz  |	|dz  z  z   |
|dz  z  z   ||z  z   |z         } n||||f\  }	}
}}t	        |       }| j
                  s|S |j                  }|j
                  r|S |j                         d   D ]'  }|d
   j                  s|d
   j                  d
      } n t        |       }t        |      }|j                  \  }}}}|j                  |      }t        d      }|d   |d   |z  z   }|d   |d   |z  z
  }|d   |d   |z  z   }|d   |d   |z  z
  }|dz  d|z  z
  }|dz  d|z  z
  }|j                  |      } t        d      }!t!        | t        |      z   |!z        }"t!        | t        |      z
  |!z        }#t!        | t        |      z   |!z        }$t!        | t        |      z
  |!z        }%|j#                  ||      }&|&|j%                         z  |"j%                         |#j%                         z
  |$j%                         |%j%                         z
  z  z
  }'t'        |'d
|      s|%|$}%}$| |"|z  z   |$|z  z   |%|z  z   |#|z  z   }(| |%|z  z   |"|z  z   |#|z  z   |$|z  z   })| |$|z  z   |#|z  z   |"|z  z   |%|z  z   }*| |#|z  z   |%|z  z   |$|z  z   |"|z  z   }+ddgdz  dgdz  dgdz  dgdz  g},ddgdz  dgdz  dgdz  dgdz  g}-t!        |(      }(t!        |)      })t!        |*      }*t!        |+      }+t(        t        d      dz  z  }.t        d      }/t        d      }0t        d|0z
        }1t*        |0z  }2|2t*        z   }3t*        |.z  dz  }4|/t        |0dz         z  }5|.|4 |/|1z  |3z
  z  |4|/|1z  |3z   z  |4 |2|5z   t*        z
  z  |4|2 |5z   t*        z   z  g}6|(j-                         }(|)j-                         })|*j-                         }*|+j-                         }+t/        |6      D ]  \  }7}t!        |j1                  t(        |(d
   t*        |(d   z  z   i            |,d   |7<   t!        |j1                  t(        |)d
   t*        |)d   z  z   i            |,d   |7<   t!        |j1                  t(        |*d
   t*        |*d   z  z   i            |,d   |7<   t!        |j1                  t(        |+d
   t*        |+d   z  z   i            |,d   |7<    t3        dd      D ]E  }7t3        d      D ]5  }8|,|7   |8   j%                         |-|7   |8<   t!        |,|7   |8         |,|7   |8<   7 G |,d   d
   }9|-d   d
   }:t3        d      D ]+  }7t'        t5        |:|-d   |7   z        d
|      s#|,d   |7   }; n |j7                  ||      \  }<}=|<|=|z  t        d      z  z   j%                         }>|<|=|z  t        d      z  z
  j%                         }?;j%                         }@dx}A}Bt3        d      D ]  }7|-d   |7   }Ct3        d      D ]w  }8|-d   |8   }Dt'        |:Cdz  z  @|Ddz  z  z   |>z
  j%                         d
|      s9t'        D|:dz  z  C@dz  z  z   |?z
  j%                         d
|      sg|,d   |7   }A|,d   |8   }B n A n g S |9Az   Bz   |;z   dz  }/|9|z  |A|z  z   |B|z  z   |;|z  z   dz  }0|9|z  |A|z  z   |B|z  z   |;|z  z   dz  }1|9|z  |A|z  z   |B|z  z   |;|z  z   dz  }2|9|z  |A|z  z   |B|z  z   |;|z  z   dz  }3|/|0|1|2|3g}t9               }E|D ],  }|j%                  d      }|Ev rg c S Ej;                  |       . |r|D cg c]
  }||dz  z
   }}|S c c}w )z
    Calculate exact roots of a solvable irreducible quintic with rational coefficients.
    Return an empty list if the quintic is reducible or not solvable.
    c              3   4   K   | ]  }|j                     y wNis_Rational).0r{   s     r<   	<genexpr>z roots_quintic.<locals>.<genexpr>  s     QUu  Q   r1   rA      r_   rO      }   i5  r   g|=N)rP   allr&   r   r%   is_irreduciblef20r   	is_linearr   r*   r   r   Tr   l0_quintic_simplifyorderr   r   r/   r   as_real_imag	enumeratexreplacerg   r   uvsetadd)Fr9   resultcoeff_5coeff_4p_q_r_s_rr   rn   ro   r:   r   r   quinticr   _factorthetarG   deltazeta1zeta2zeta3zeta4r   tolr   	alpha_barbetabeta_bardiscdisc_barr   Stwol1l4l2l3r   testR1R2R3R4ResRes_nx0rs   rt   x3x4x5x6x7r   r\   jrW   r1_nr4r   vtestplus	testminusr4_nr   r3r2temp_nr3temp_nsawsF                                                                         r<   roots_quinticr.    s	   
 F'(||~$GWb"b"QwRR.PQQ!|W%&\\^"7BB 77"1$$72a!GQJ,r/172a!GQJ,r/""44q!|C7GGA
2b 007A:b=3DDqRS|TXGXXEEA!Q$1a4'!A#-12R^
1a!nG 
++C
??$Q' 1:AJOOA&E 	QAGE!(E5%		%A
E(CaD1Q4:E!qtEz!IQ4!A$u*Dtad5j H!8afD!|aj(H	E	BQ4D	UFT$Z/47	8B	UFT$Z/47	8B	YJh74?	@B	YJh74?	@BMM%#E%'')O"$$&2446/BDDFRTTVO!DFDaRB 
bhE	!BuH	,r%x	7B	bhE	!BuH	,r%x	7B	bhE	!BuH	,r%x	7B	bhE	!BuH	,r%x	7B$D6!8dVAXvax
8CD6!8dVAXvax$:E 
2	B	2	B	2	B	2	B 
QqT!VB	aB	aB	a"fB	
2B	aB	
2aB	DaLBsBrEBJRURZ2#rBw{2CR"rTUEV
WC		B		B		B		B# H1%ajj!RUQr!uW_1E&FGAq	%ajj!RUQr!uW_1E&FGAq	%ajj!RUQr!uW_1E&FGAq	%ajj!RUQr!uW_1E&FGAq		H 1a[ 5q 	5Aa&)++-E!HQK)#a&)4CF1I	55 
QB8A;D1X 4a#$a-QB ::eQDAqAeGDGO#&&(HQuWT!W_$'')I 446DNB1X 8A;q 	A Qx{Hd8Q;&hk)99HDGGI1cRhtQw&$')99IEHHJAsSVAYVAY	 > 	 r'B,
Q	B
U(RX
5
(2e8
3Q	6B
U(RX
5
(2e8
3Q	6B
U(RX
5
(2e8
3Q	6B
U(RX
5
(2e8
3Q	6B"b"b!F %C CCF8 I
 +12a!gk/22M 3s   `.c                 J    ddl m}  ||       } t        |       } t        |       S )Nr   )powsimp)r8   r0  r'   r+   )rM   r0  s     r<   r   r     s!    /4=D$<DD>r>   c                    t        t        | j                                \  }}t        t        |       \  }t        t        t        |            }|d   |d   k  r5t        t        |            }|d   }t        |      D cg c]  }||z
  	 }}ny|dd }|dd }t        |      dk(  r;t        |d   t        j                  |d   z        }|j                  rt        |      S yt        t        t        |            dd       }	 t        |      }	 t        ||      D ]  \  }}	|	||z  z  dk7  s	 t        |      } n |S 0c c}w # t        $ r Y yw xY w# t        $ r Y  yw xY w)a~  Compute coefficient basis for a polynomial over integers.

    Returns the integer ``div`` such that substituting ``x = div*y``
    ``p(x) = m*q(y)`` where the coefficients of ``q`` are smaller
    than those of ``p``.

    For example ``x**5 + 512*x + 1024 = 0``
    with ``div = 4`` becomes ``y**5 + 2*y + 1 = 0``

    Returns the integer ``div`` or ``None`` if there is no possible scaling.

    Examples
    ========

    >>> from sympy.polys import Poly
    >>> from sympy.abc import x
    >>> from sympy.polys.polyroots import _integer_basis
    >>> p = Poly(x**5 + 512*x + 1024, x, domain='ZZ')
    >>> _integer_basis(p)
    4
    r   rf   Nr1   )r   r   termsmapr   r   r   r   r   rl   rD   r   r   r)   nextStopIteration)
polymonomscoeffsr   r\   r:   divsdivmonomr{   s
             r<   _integer_basisr<    s|   , #tzz|,-NFF3< GF#c6"#Fay6":hv&'1I!)&!12A!a%22CR[FCR[F 6{aq	155?+<<q6MHXf-.qr23D4j / 		LE5sEz!Q&t*C 		 J - 3&   %   s*    E E /E	EE	E! E!c                 \   t         j                  }| j                  }	 | j                  d      \  }} | j                         d   } | j                         } | j                         j                  r1t        d | j                  j                         D              r| j                         } t        t        | j                                }t        | j                   dd       }|d   |dd }}t        t        |      |      D ]  \  }}d}	|d   |d   k  rt#        |      }d}	d}
t        ||      D ]+  \  }}|s|s|r|s ?||z  dk7  r I||z  }|
|}
%|
|k7  s+ [ |	r|
 }
| j%                  |d      } |||
 z  z  }|j'                  |        |r | j(                  | } | j*                  rS| j                         j,                  r9t/        |       ,| j1                         fd	}| j3                  |      } |z  }t5        | |      s ||       } || fS # t        $ r || fcY S w xY w)
z7Try to get rid of symbolic coefficients from ``poly``. T)convertr1   c              3   4   K   | ]  }|j                     y wr   )is_term)r   rU   s     r<   r   z#preprocess_roots.<locals>.<genexpr>  s     (Nq(Nr   Nr   Frf   c                      || d   z
  z  z  S )Nr    )rq   r{   basisr   s     r<   funczpreprocess_roots.<locals>.func*  s    ea!A$h///r>   )r   rl   rD  clear_denomsr#   	primitiveretractr5   is_Polyr   repr8  injectr   r   r7  gensr   evalremoveejectis_univariateis_ZZr<  r   termwise
isinstance)r6  r{   	poly_funcrr   stripsrK  rF   r   stripreverseratiorS   rT   _ratiorD  rC  r   s                  @@r<   preprocess_rootsrY    s2   EEE		I##D#14 >>AD<<>D   S(NDHHOO<M(N%N{{}c4;;=)*DIIabM"ay&*fd4j&1 	!JCGQx%)# ED%( !1!UaZ!VF} &&! "FEyya(v&C ;	!> 4::t$Ddoo/55t$A0 ==&DUNEdI&$;  d{s   H H+*H+T)	autor   rm   quarticsquinticsre   filter	predicatestrictc       	         L  0 ddl m} t        |      }t        | t              rS|
rt        d      t        d      }i t        |       dz
  }}| D ]  }t        |      |dz
  c||<   } t        ||d      } n	 t        | g|
i |}t        | t              s!|j                  j                  st        d      |} | j                         }| j                         d	k(  r`|d	kD  rZ | j                         j                   | j"                   \  }}| j%                          }||k7  r|}g }t'        j(                  |      D ]Y  }|j*                  s|j-                         \  }}|j.                  s0|j0                  s=|j3                  |t        d
      f       [ |rt5        t        ||z   j7                  t        |            g| j"                   g|j"                  ||||d|}|j9                         D ci c]4  \  }}t;        |j7                  |D ci c]  \  }}||
 c}}            6 c}}S | j>                  rt        d      d }0fd}fd0tA        dt        | j"                        z  t              }| jC                  | jD                  |      } | jG                         \  \  }} |si }ntH        jJ                  |i}tM        |       \  }} |r*| jO                         jP                  r| jS                         } | jO                         jT                  r.| jC                  | jD                  jW                  tX                    } d} d}!i }"| jZ                  s| jO                         }#|#j\                  s.|#j^                  r"| ja                         D ]  }$ ||"||$d        n| j                         dk(  r ||"|tc        |       d   d       n| j                         d	k(  r9| j                         d	k(  rtd        ntf        }% |%|       D ]  }$ ||"||$d        nLt        | j                               ji                         \  }&}'t        |'      dk(  r/| j                         d	k(  rte        |       D ]  }$ ||"||$d        nt        |'      dk(  r|'d   d   dk(  r| jO                         jj                  ra ||       }(|(r>|(d   	|(d	d \  }!} n
|(d   |(d   } } t5        |       }"|"s ||       D ]  }) ||"||)d        nj 0|       D ]  }$ ||"||$d        nQ ||       D ]  }) ||"||)d        n8|'D ]3  \  }*} 0t        |*| j                  d            D ]  }$ ||"||$|        5 |tH        jl                  ur$|"i }"}+|+j9                         D ]  \  })}||"||)z  <    |dvr;d d d d d},	 |,|   }-t        |"      jq                         D ]  }. |-|.      r|"|.=  |*t        |"      jq                         D ]  }. ||.      r|"|.=  | r$i }/|"j9                         D ]  \  }}||/|| z  <    |/}"|!r$i }/|"j9                         D ]  \  }}||/||!z   <    |/}"|"js                  |       |	r>tu        |"jw                               | j                         k  rty        t{        d            |s|"S g }t}        |"      D ]  }.|j                  |.g|"|.   z          |S c c}}w c c}}w # t<        $ r |rg cY S i cY S w xY w# tn        $ r t        d|z        w xY w)a  
    Computes symbolic roots of a univariate polynomial.

    Given a univariate polynomial f with symbolic coefficients (or
    a list of the polynomial's coefficients), returns a dictionary
    with its roots and their multiplicities.

    Only roots expressible via radicals will be returned.  To get
    a complete set of roots use RootOf class or numerical methods
    instead. By default cubic and quartic formulas are used in
    the algorithm. To disable them because of unreadable output
    set ``cubics=False`` or ``quartics=False`` respectively. If cubic
    roots are real but are expressed in terms of complex numbers
    (casus irreducibilis [1]) the ``trig`` flag can be set to True to
    have the solutions returned in terms of cosine and inverse cosine
    functions.

    To get roots from a specific domain set the ``filter`` flag with
    one of the following specifiers: Z, Q, R, I, C. By default all
    roots are returned (this is equivalent to setting ``filter='C'``).

    By default a dictionary is returned giving a compact result in
    case of multiple roots.  However to get a list containing all
    those roots set the ``multiple`` flag to True; the list will
    have identical roots appearing next to each other in the result.
    (For a given Poly, the all_roots method will give the roots in
    sorted numerical order.)

    If the ``strict`` flag is True, ``UnsolvableFactorError`` will be
    raised if the roots found are known to be incomplete (because
    some roots are not expressible in radicals).

    Examples
    ========

    >>> from sympy import Poly, roots, degree
    >>> from sympy.abc import x, y

    >>> roots(x**2 - 1, x)
    {-1: 1, 1: 1}

    >>> p = Poly(x**2-1, x)
    >>> roots(p)
    {-1: 1, 1: 1}

    >>> p = Poly(x**2-y, x, y)

    >>> roots(Poly(p, x))
    {-sqrt(y): 1, sqrt(y): 1}

    >>> roots(x**2 - y, x)
    {-sqrt(y): 1, sqrt(y): 1}

    >>> roots([1, 0, -1])
    {-1: 1, 1: 1}

    ``roots`` will only return roots expressible in radicals. If
    the given polynomial has some or all of its roots inexpressible in
    radicals, the result of ``roots`` will be incomplete or empty
    respectively.

    Example where result is incomplete:

    >>> roots((x-1)*(x**5-x+1), x)
    {1: 1}

    In this case, the polynomial has an unsolvable quintic factor
    whose roots cannot be expressed by radicals. The polynomial has a
    rational root (due to the factor `(x-1)`), which is returned since
    ``roots`` always finds all rational roots.

    Example where result is empty:

    >>> roots(x**7-3*x**2+1, x)
    {}

    Here, the polynomial has no roots expressible in radicals, so
    ``roots`` returns an empty dictionary.

    The result produced by ``roots`` is complete if and only if the
    sum of the multiplicity of each root is equal to the degree of
    the polynomial. If strict=True, UnsolvableFactorError will be
    raised if the result is incomplete.

    The result can be be checked for completeness as follows:

    >>> f = x**3-2*x**2+1
    >>> sum(roots(f, x).values()) == degree(f, x)
    True
    >>> f = (x-1)*(x**5-x+1)
    >>> sum(roots(f, x).values()) == degree(f, x)
    False


    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Cubic_equation#Trigonometric_and_hyperbolic_solutions

    r   )to_rational_coeffszredundant generators givenr   r1   T)fieldzgenerator must be a SymbolrA   )positive)rZ  r   rm   r[  r\  re   r]  r^  *multivariate polynomials are not supportedc                     |t         j                  k(  rAt         j                  |v r|t         j                  xx   |z  cc<   n||t         j                  <   || v r| |xx   |z  cc<   y || |<   y r   )r   rQ   )r   zeroscurrentrootrq   s       r<   _update_dictzroots.<locals>._update_dict  sZ    !&& vvaff" !aff& ;1$"#F;r>   c                    | j                         g }} |d         D ]  }|j                  |        |dd D ]M  }t        |      g }}|D ]9  }|t        || j                        z
  } |      D ]  }|j                  |        ; O |S )z+Find roots using functional decomposition. r   r1   N)	decomposerE   r   r&   r   )r9   factorsri   rg  currentfactorpreviousr   _try_heuristicss          r<   _try_decomposezroots.<locals>._try_decompose  s    *71:6 	&KLL%	& %QR[ 	.M"5k2eH' .!Daee$<<#21#5 .KLL-..	. r>   c           	      v   | j                   rg S | j                  r"t        j                  g| j	                         z  S | j                         dk(  r@| j	                         dk(  r"t        t        t        t        |                   S t        |       S g }dD ]X  }| j                  |      r| j                  t        | j                  |z
  | j                              } |j                  |        n | j	                         }|dk(  r'|t        t        t        t        |                   z  }|S |dk(  r'|t        t        t        t!        |                   z  }|S | j"                  r|t%        |       z  }|S |dk(  rr|t'        |       z  }|S |dk(  rr|t)        |       z  }|S |dk(  rr|t+        |       z  }|S )z+Find roots using formulas and some tricks. rA   r1   r   r_   )rm   rO   r   )	is_groundis_monomialr   rQ   r   lengthr   r3  r'   r=   r   rL  quor&   r   rE   r]   is_cyclotomicr   r   r   r.  )r9   r   r\   r   r   r[  r\  rm   s       r<   rn  zroots.<locals>._try_heuristics  s   ;;I==FF8AHHJ&&88:?xxzQCQ899%a(( 	A66!9EE$quuqy!%%01a 		 HHJ6d3v|A788F  !Vd3vq'9:;;F  __&q))F  !Vk!$//F  !VmA&&F  !VmA&&Fr>   zx:%d)clsNrf   )Nr   c                     | j                   S r   )rD   r:   s    r<   r   zroots.<locals>.<lambda>l  s
    1<< r>   c                     | j                   S r   r   rx  s    r<   r   zroots.<locals>.<lambda>m  s
    1== r>   c                 B    t        d | j                         D              S )Nc              3   4   K   | ]  }|j                     y wr   )rk   )r   rS   s     r<   r   z*roots.<locals>.<lambda>.<locals>.<genexpr>n  s     EQqyyEr   )r   as_numer_denomrx  s    r<   r   zroots.<locals>.<lambda>n  s    3E!2B2B2DEE r>   c                     | j                   S r   )is_imaginaryrx  s    r<   r   zroots.<locals>.<lambda>o  s
    1>> r>   )ZQrX   r   zInvalid filter: %sa  
            Strict mode: some factors cannot be solved in radicals, so
            a complete list of solutions cannot be returned. Call
            roots with strict=False to get solutions expressible in
            radicals (if there are any).
            )@sympy.polys.polytoolsra  dictrR  r   
ValueErrorr   r   r   r&   r   	is_Symbolr!   r   rs  as_expras_independentrK  r(   r   rB   rC   as_base_exprD   is_AddrE   ri   r   itemsr   r"   is_multivariater   perrI  	terms_gcdr   rQ   rY  r5   is_Ringto_fieldis_QQ_Ir>  r    rq  is_Exactr6   nrootsr=   r]   r   r   is_EXrl   KeyErrorr   updatesumvaluesr$   r.   r   r   )1r9   rZ  r   rm   r[  r\  re   r]  r^  r_  rK  flagsra  r   r6  r\   r{   Fr   condepfconbasesrT   r   rp   rq   r&  rh  ro  dumgensrf  	rescale_xtranslate_xr   r;   r:   	roots_funrr   rk  resrg  rl  _resulthandlersqueryzeroresult1rn  s1     ````                                          @r<   ri   ri   5  s3   ` 9KE!T9::#Jc!fqja 	/E QJDGQ	/ q%&	5Q'''Aa&quu%&BCCA 
AxxzQ1q55199;55qvv>S'3;CE ]]3/ H88#$==?DAq || %a1E-F G	H
 "4s(<(<T%[(I $%VV$% 
%'(vv
%!%#)!%%-%-%-#)&/
% $
% *, 5!%A !-QZZ.34daQT4.6 !!  5 5 !"NOO	$$$N fs166{*6G	aeeWAkkmGDQ!"HE1&&JJL 	||~EE!%%--#$IKF;;lln|| 0 0XXZ 2VUAq12XXZ1_Q(:A>XXZ1_+,88:?Iq\ 2VUAq12 aiik*668JAw7|q QXXZ1_(+ 6A 156 w<1$A!);||~++03"1v~14QRQ/21vs2w1	%*1XF#)3A!3D !PK$0Q$O!P &5Q%7 B ,VUAq AB ,:!+< HK(QGH -4 >(q!0mQUURV1W!X >A(1=>> AEE!2%mmo 	*NK()F5$%	* [ '(E)	
	<V$E L%%' 	!D;4L	! L%%' 	!DT?4L	! LLN 	%DAq#$GAiK 	%LLN 	)DAq'(GAO$	) MM%#fmmo&3#J 0 %  	 FO 	.DLL$t,-	. G 5 5?   					t  	<1F:;;	<s=   ;A[4 [.![(.[. \ ([.4\\\\#r]  c                .   t        |      }t        | g|i |}|j                  s| gS |j                  rt	        d      |j
                  d   }t        ||      }|s|g}ng d}}t        |j                               D ]   \  }	}
|t        ||	z
  |      g|
z  z   ||
z   }}" ||j                         k  r-t        d |      }|j                  |j                  |             t        | t              s|D  cg c]  } | j                          }} |S c c} w )z
    Returns all factors of a univariate polynomial.

    Examples
    ========

    >>> from sympy.abc import x, y
    >>> from sympy.polys.polyroots import root_factors

    >>> root_factors(x**2 - y, x)
    [x - sqrt(y), x + sqrt(y)]

    rd  r   r  c                     | |z  S r   rB  )rn   ro   s     r<   r   zroot_factors.<locals>.<lambda>  s
    AaC r>   )r  r&   rH  r  r  rK  ri   r   r  r   r   rE   rt  rR  r  )r9   r]  rK  argsr  r   rf  rk  Nr:   r   Gs               r<   root_factorsr    s    :DQA99s
EFF	q	A!F#E#EKKM* 	=DAq DQN#3A#55q1uQG	= qxxz>'1ANN1558$a)02AAIIK22N 3s   7D)F)U__doc__r   	functoolsr   
sympy.corer   r   r   sympy.core.exprtoolsr   sympy.core.functionr   sympy.core.logicr	   sympy.core.mulr
   r   sympy.core.intfuncr   sympy.core.numbersr   r   sympy.core.powerr   sympy.core.relationalr   sympy.core.sortingr   sympy.core.symbolr   r   r   sympy.core.sympifyr   sympy.functionsr   r   r   r   r   (sympy.functions.elementary.miscellaneousr   r   sympy.ntheoryr   r   r   sympy.polys.domainsr    sympy.polys.polyerrorsr!   r"   r#   r$   sympy.polys.polyquinticconstr%   r  r&   r'   r(   r)   r*   sympy.polys.rationaltoolsr+   sympy.polys.specialpolysr,   sympy.utilitiesr-   sympy.utilities.miscr.   r/   r=   r]   r   r   r   r   r   r   r.  r   r<  rY  ri   r  rB  r>   r<   <module>r     s    >     - ( & + # -   $ & 4 4 & 9 9 ? 6 6 "( ( 4 N N . 4 " + 3KCLCJ5Bpy;x7t*Z@gT>BFR g gT #' +r>   