
    Ed                       d Z ddlmZmZ ddlmZ ddlmZ ddlm	Z	m
Z
mZ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mZmZmZmZ dd
lmZ ddlmZmZ ddl m!Z!m"Z"m#Z# ddl$m%Z%m&Z& ddl'm(Z( ddl)m*Z*m+Z+ ddl,m-Z-m.Z. ddl/m0Z0m1Z1 ddl2m3Z3 ddl4m5Z6 ddl7m8Z8 ddl9m:Z:m;Z;m<Z< ddl=m>Z> ddl?m@Z@ ddlAmBZC ddlDmEZE ddlFmGZG ddlHmIZImJZJmKZK ddlLmMZMmNZNmOZOmPZPmQZQmRZRmSZSmTZTmUZUmVZVmWZW ddlXmYZYmZZZm[Z[m\Z\m]Z]m^Z^ ddl_m`Z` ddlambZb dd lcmdZdmeZemfZf dd!lgmhZh dd"limjZjmkZk dd#l4Zldd#lmZmdd$lnmoZo d% Zpee G d& d'e                      Zqee G d( d)eq                      Zreed*             Zsd+ Zteed,             Zud- Zvd. Zweedud/            Zxeed0             Zyeed1             Zzeed2             Z{eed3             Z|eed4             Z}eed5             Z~eed6             Zeed7             Zeed8             Zeed9             Zeed:             Zeed;             Zeed<             Zeed=             Zeed>             Zeed?             Zeed@             ZeedAdBdC            ZeedD             ZeedE             ZeedF             ZeedvdG            ZeedH             ZeedvdI            ZeedJ             ZeedK             ZeedL             ZeedM             ZeedN             ZeedO             ZeedP             ZeedQ             ZeedR             ZeedS             ZeedT             ZeedU             ZdV ZdW ZdX ZdY ZdZ Zd[ Zd\ Zd] Zeed^             Zeed_             Zeed`             ZeedAdadb            Zeedwdc            Zeedxdd            Zeedyde            Zeedzdg            Zeed{dj            Zeedk             Zeedl             Zeedfdmdn            Zeedo             Zeedp             ZBeedq             Zee G dr dse                      Zeedt             Zd#S )|z8User-friendly public interface to polynomial functions.     )wrapsreducemul)Optional)SExprAddTuple)Basic)
_sympifyit)Factors	factor_ncfactor_terms)pure_complexevalffastlog_evalf_with_bounded_errorquad_to_mpmath)
Derivative)Mul_keep_coeff)ilcmIInteger)
RelationalEquality)ordered)DummySymbol)sympify_sympify)preorder_traversal	bottom_up)BooleanAtom)polyoptions)construct_domain)FFQQZZ)DomainElement)matrix_fglm)groebner)Monomial)monomial_key)DMPDMFANP)OperationNotSupportedDomainErrorCoercionFailedUnificationFailedGeneratorsNeededPolynomialErrorMultivariatePolynomialErrorExactQuotientFailedPolificationFailedComputationFailedGeneratorsError)basic_from_dict
_sort_gens_unify_gens_dict_reorder_dict_from_expr_parallel_dict_from_expr)together)dup_isolate_real_roots_list)grouppublic
filldedent)sympy_deprecation_warning)iterablesiftN)NoConvergencec                 <     t                      fd            }|S )Nc                    t          |          }t          |t                    r | |          S t          |t                    r	  | j        |g| j        R  } | |          S # t          $ rb |j        r	t          cY S t          | 
                                j                  } ||          }|t          urt          ddd           |cY S w xY wt          S )Na@  
                        Mixing Poly with non-polynomial expressions in binary
                        operations is deprecated. Either explicitly convert
                        the non-Poly operand to a Poly with as_poly() or
                        convert the Poly to an Expr with as_expr().
                        z1.6z)deprecated-poly-nonpoly-binary-operations)deprecated_since_versionactive_deprecations_target)r"   
isinstancePolyr	   	from_exprgensr8   	is_MatrixNotImplementedgetattras_expr__name__rI   )fgexpr_methodresultfuncs       5lib/python3.11/site-packages/sympy/polys/polytools.pywrapperz_polifyit.<locals>.wrapperD   s   QKKa 	"41::4   	""AK+AF+++& tAqzz!% #   ; *))))%aiikk4=AA$Q/ 
- 273^	 	 	 	 !( "!s   A' 'CACC)r   )r^   r`   s   ` r_   	_polifyitra   C   s3    
4[[" " " " ["8 N    c                   >    e Zd ZdZdZdZdZdZd Ze	d             Z
ed             Zed             Zd	 Ze	d
             Ze	d             Ze	d             Ze	d             Ze	d             Ze	d             Ze	d             Ze	d             Ze	d             Z fdZed             Zed             Zed             Zed             Zed             Zed             Zed             Zd Z d Z!ddZ"d Z#d  Z$d! Z%d" Z&d# Z'd$ Z(dd%Z)d& Z*d' Z+d( Z,d) Z-d* Z.d+ Z/d, Z0dd-Z1dd.Z2dd/Z3dd0Z4dd1Z5d2 Z6d3 Z7d4 Z8d5 Z9d6 Z:dd8Z;dd9Z<d: Z=d; Z>d< Z?d= Z@dd>ZAd? ZBd@ ZCdA ZDdB ZEdC ZFdD ZGdE ZHdF ZIdG ZJdH ZKdI ZLdJ ZMdK ZNdL ZOdM ZPdN ZQdO ZRdP ZSddQZTddRZUddSZVddTZWdU ZXddWZYdX ZZdY Z[dZ Z\d[ Z]dd\Z^d] Z_dd^Z`d_ Zad` ZbddbZcddcZddddZeddeZfddfZgdg Zhdh ZiddiZjdj Zkdk Zldl ZmemZnddmZodn ZpddoZqddpZrddqZsdr Ztds ZuddtZvdu ZwddvZxddwZydx Zzdy Z{dz Z|d{ Z}dd|Z~d} Zd~ Zd Zd Zd Zd ZddZd Zd Zd Zd ZddZddZd Zd ZddZddZddZddZddZddZddZd Zd Zd ZddZed             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zd Zd Zed             Zed             Zed             Zed             Zed             Zed             Z ede          d             Zed             Zed             Zed             Zed             Zed             Zed             Z ede          d             Z ede          d             Z ede          d             Z ede          d             Zd ZddZddZd Z xZS )rR   aP  
    Generic class for representing and operating on polynomial expressions.

    See :ref:`polys-docs` for general documentation.

    Poly is a subclass of Basic rather than Expr but instances can be
    converted to Expr with the :py:meth:`~.Poly.as_expr` method.

    .. deprecated:: 1.6

       Combining Poly with non-Poly objects in binary operations is
       deprecated. Explicitly convert both objects to either Poly or Expr
       first. See :ref:`deprecated-poly-nonpoly-binary-operations`.

    Examples
    ========

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

    Create a univariate polynomial:

    >>> Poly(x*(x**2 + x - 1)**2)
    Poly(x**5 + 2*x**4 - x**3 - 2*x**2 + x, x, domain='ZZ')

    Create a univariate polynomial with specific domain:

    >>> from sympy import sqrt
    >>> Poly(x**2 + 2*x + sqrt(3), domain='R')
    Poly(1.0*x**2 + 2.0*x + 1.73205080756888, x, domain='RR')

    Create a multivariate polynomial:

    >>> Poly(y*x**2 + x*y + 1)
    Poly(x**2*y + x*y + 1, x, y, domain='ZZ')

    Create a univariate polynomial, where y is a constant:

    >>> Poly(y*x**2 + x*y + 1,x)
    Poly(y*x**2 + y*x + 1, x, domain='ZZ[y]')

    You can evaluate the above polynomial as a function of y:

    >>> Poly(y*x**2 + x*y + 1,x).eval(2)
    6*y + 1

    See Also
    ========

    sympy.core.expr.Expr

    reprT   Tgn $@c                    t          j        ||          }d|v rt          d          t          |t          t
          t          t          f          r|                     ||          S t          |t                    rNt          |t                    r|                     ||          S |                     t          |          |          S t          |          }|j        r|                     ||          S |                     ||          S )z:Create a new polynomial instance out of something useful. orderz&'order' keyword is not implemented yet)exclude)optionsbuild_optionsNotImplementedErrorrQ   r0   r1   r2   r+   _from_domain_elementrJ   strdict
_from_dict
_from_listlistr!   is_Poly
_from_poly
_from_exprclsre   rT   argsopts        r_   __new__zPoly.__new__   s    #D$//c> 	P%&NOOOcCc=9:: 	0++C555c3''' 	0#t$$ 6~~c3///~~d3ii555#,,C{ 0~~c3///~~c3///rb   c                     t          |t                    st          d|z            |j        t	          |          dz
  k    rt          d|d|          t          j        |           }||_        ||_        |S )z:Construct :class:`Poly` instance from raw representation. z%invalid polynomial representation: %s   zinvalid arguments: z, )	rQ   r0   r8   levlenr   ry   re   rT   )rv   re   rT   objs       r_   newzPoly.new   s     #s## 	M!7#=? ? ?WD		A% 	M!/dd"KLLLmC  
rb   c                 T    t          | j                                        g| j        R  S N)r>   re   to_sympy_dictrT   selfs    r_   exprz	Poly.expr   s(    tx5577D$)DDDDrb   c                 "    | j         f| j        z   S r   )r   rT   r   s    r_   rw   z	Poly.args   s    	|di''rb   c                 "    | j         f| j        z   S r   rd   r   s    r_   _hashable_contentzPoly._hashable_content   s    {TY&&rb   c                 X    t          j        ||          }|                     ||          S )(Construct a polynomial from a ``dict``. )ri   rj   ro   ru   s        r_   	from_dictzPoly.from_dict   *     #D$//~~c3'''rb   c                 X    t          j        ||          }|                     ||          S )(Construct a polynomial from a ``list``. )ri   rj   rp   ru   s        r_   	from_listzPoly.from_list   r   rb   c                 X    t          j        ||          }|                     ||          S )*Construct a polynomial from a polynomial. )ri   rj   rs   ru   s        r_   	from_polyzPoly.from_poly   r   rb   c                 X    t          j        ||          }|                     ||          S +Construct a polynomial from an expression. )ri   rj   rt   ru   s        r_   rS   zPoly.from_expr   r   rb   c                 :   |j         }|st          d          t          |          dz
  }|j        }|t	          ||          \  }}n2|                                D ]\  }}|                    |          ||<    | j        t          j	        |||          g|R  S )r   z0Cannot initialize from 'dict' without generatorsr{   Nrx   )
rT   r7   r}   domainr'   itemsconvertr   r0   r   )rv   re   rx   rT   levelr   monomcoeffs           r_   ro   zPoly._from_dict   s     x 	D"BD D D D		A 	3*3C888KFCC #		 3 3u#^^E22E

sws}S%88@4@@@@rb   c                 ^   |j         }|st          d          t          |          dk    rt          d          t          |          dz
  }|j        }|t          ||          \  }}n"t          t          |j        |                    } | j	        t          j        |||          g|R  S )r   z0Cannot initialize from 'list' without generatorsr{   z#'list' representation not supportedNr   )rT   r7   r}   r9   r   r'   rq   mapr   r   r0   r   )rv   re   rx   rT   r   r   s         r_   rp   zPoly._from_list  s     x 	7"BD D DYY!^ 	7-57 7 7 D		A 	1*3C888KFCCs6>3//00Csws}S%88@4@@@@rb   c                    | |j         k    r | j        |j        g|j        R  }|j        }|j        }|j        }|rb|j        |k    rWt          |j                  t          |          k    r(|                     |                                |          S  |j	        | }d|v r|r|
                    |          }n|du r|                                }|S )r   r   T)	__class__r   re   rT   fieldr   setrt   rX   reorder
set_domainto_field)rv   re   rx   rT   r   r   s         r_   rs   zPoly._from_poly  s     #- 	.#'#'-CH---Cx	 	)CH$ 	)38}}D		) )~~ckkmmS999!ck4(s? 	!v 	!..((CCd] 	!,,..C
rb   c                 T    t          ||          \  }}|                     ||          S r   )rB   ro   )rv   re   rx   s      r_   rt   zPoly._from_expr4  s+     #3,,S~~c3'''rb   c                     |j         }|j        }t          |          dz
  }|                    |          g} | j        t          j        |||          g|R  S Nr{   )rT   r   r}   r   r   r0   r   )rv   re   rx   rT   r   r   s         r_   rl   zPoly._from_domain_element:  s[    xD		A~~c""#sws}S%88@4@@@@rb   c                 D    t                                                      S r   super__hash__r   r   s    r_   r   zPoly.__hash__D      ww!!!rb   c                     t                      }| j        }t          t          |                    D ]3}|                                 D ]}||         r|||         j        z  } n4|| j        z  S )a  
        Free symbols of a polynomial expression.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x, y, z

        >>> Poly(x**2 + 1).free_symbols
        {x}
        >>> Poly(x**2 + y).free_symbols
        {x, y}
        >>> Poly(x**2 + y, x).free_symbols
        {x, y}
        >>> Poly(x**2 + y, x, z).free_symbols
        {x, y}

        )r   rT   ranger}   monomsfree_symbolsfree_symbols_in_domain)r   symbolsrT   ir   s        r_   r   zPoly.free_symbolsG  s    * %%ys4yy!! 	 	A  8 tAw33GE 444rb   c                     | j         j        t                      }}|j        r|j        D ]}||j        z  }n(|j        r!|                                 D ]}||j        z  }|S )aj  
        Free symbols of the domain of ``self``.

        Examples
        ========

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

        >>> Poly(x**2 + 1).free_symbols_in_domain
        set()
        >>> Poly(x**2 + y).free_symbols_in_domain
        set()
        >>> Poly(x**2 + y, x).free_symbols_in_domain
        {y}

        )re   domr   is_Compositer   r   is_EXcoeffs)r   r   r   genr   s        r_   r   zPoly.free_symbols_in_domainf  sz    & (, 	.~ , ,3++,\ 	. . .5--rb   c                     | j         d         S )z
        Return the principal generator.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**2 + 1, x).gen
        x

        r   rT   r   s    r_   r   zPoly.gen  s     y|rb   c                 *    |                                  S )a  Get the ground domain of a :py:class:`~.Poly`

        Returns
        =======

        :py:class:`~.Domain`:
            Ground domain of the :py:class:`~.Poly`.

        Examples
        ========

        >>> from sympy import Poly, Symbol
        >>> x = Symbol('x')
        >>> p = Poly(x**2 + x)
        >>> p
        Poly(x**2 + x, x, domain='ZZ')
        >>> p.domain
        ZZ
        )
get_domainr   s    r_   r   zPoly.domain  s    *    rb   c                      | j         | j                            | j        j        | j        j                  g| j        R  S )z3Return zero polynomial with ``self``'s properties. )r   re   zeror|   r   rT   r   s    r_   r   z	Poly.zero  8     txdhlDHLAANDINNNNrb   c                      | j         | j                            | j        j        | j        j                  g| j        R  S )z2Return one polynomial with ``self``'s properties. )r   re   oner|   r   rT   r   s    r_   r   zPoly.one  s8     txTX\48<@@M49MMMMrb   c                      | j         | j                            | j        j        | j        j                  g| j        R  S )z3Return unit polynomial with ``self``'s properties. )r   re   unitr|   r   rT   r   s    r_   r   z	Poly.unit  r   rb   c                 b    |                      |          \  }}}} ||           ||          fS )a  
        Make ``f`` and ``g`` belong to the same domain.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> f, g = Poly(x/2 + 1), Poly(2*x + 1)

        >>> f
        Poly(1/2*x + 1, x, domain='QQ')
        >>> g
        Poly(2*x + 1, x, domain='ZZ')

        >>> F, G = f.unify(g)

        >>> F
        Poly(1/2*x + 1, x, domain='QQ')
        >>> G
        Poly(2*x + 1, x, domain='QQ')

        )_unify)rZ   r[   _perFGs         r_   unifyz
Poly.unify  s5    2 xx{{31s1vvss1vv~rb   c           
      F    t                    j        ss	  j        j         j         j         j                             j        j                                                fS # t          $ r t          d d          w xY wt           j        t                    rt          j        t                    rt           j        j                  } j        j                            j        j        |          t          |          dz
  c} j        |k    rt           j                                         j        |          \  }} j        j        k    r fd|D             }t          t!          t#          t%          ||                              |          }n j                                      }j        |k    rt          j                                        j        |          \  }}j        j        k    rfd|D             }t          t!          t#          t%          ||                              |          }	n0j                                      }	nt          d d           j        |d ffd	}
|
||	fS )NCannot unify  with r{   c                 P    g | ]"}                     |j        j                  #S  r   re   r   ).0cr   rZ   s     r_   
<listcomp>zPoly._unify.<locals>.<listcomp>  +    LLLaAquy 9 9LLLrb   c                 P    g | ]"}                     |j        j                  #S r   r   )r   r   r   r[   s     r_   r   zPoly._unify.<locals>.<listcomp>  r   rb   c                     |/|d |         ||dz   d          z   }|s|                     |           S  j        | g|R  S r   to_sympyr   re   r   rT   removerv   s       r_   r   zPoly._unify.<locals>.per  ^     -GVG}tFQJKK'88 -<<,,,373&&&&&rb   )r!   rr   re   r   r   
from_sympyr5   r6   rQ   r0   r@   rT   r   r}   rA   to_dictrn   rq   zipr   r   )rZ   r[   rT   r|   f_monomsf_coeffsr   g_monomsg_coeffsr   r   rv   r   s   ``         @@r_   r   zPoly._unify  s   AJJy 	LLuy!%		!%):N:Nq:Q:Q0R0RRR! L L L''QQQ(JKKKL aeS!! 	Hj&<&< 	Hqvqv..Duyquy$77TQHCv~ 	'%2EMMOOQVT&3 &3"( 59# MLLLLL8LLLHT#h"9"9::;;S#FFEMM#&&v~ 	'%2EMMOOQVT&3 &3"( 59# MLLLLL8LLLHT#h"9"9::;;S#FFEMM#&&##AA$FGGGk4 	' 	' 	' 	' 	' 	' CA~s   AA+ + BNc                     || j         }|9|d|         ||dz   d         z   }|s| j        j                            |          S  | j        j        |g|R  S )ab  
        Create a Poly out of the given representation.

        Examples
        ========

        >>> from sympy import Poly, ZZ
        >>> from sympy.abc import x, y

        >>> from sympy.polys.polyclasses import DMP

        >>> a = Poly(x**2 + 1)

        >>> a.per(DMP([ZZ(1), ZZ(1)], ZZ), gens=[y])
        Poly(y + 1, y, domain='ZZ')

        Nr{   )rT   re   r   r   r   r   )rZ   re   rT   r   s       r_   r   zPoly.per
  sv    $  	6D 	/=4
#44D /uy))#...q{s*T****rb   c                     t          j        | j        d|i          }|                     | j                            |j                            S )z Set the ground domain of ``f``. r   )ri   rj   rT   r   re   r   r   )rZ   r   rx   s      r_   r   zPoly.set_domain'  s=    #AFXv,>??uuQU]]3:..///rb   c                     | j         j        S )z Get the ground domain of ``f``. )re   r   rZ   s    r_   r   zPoly.get_domain,  s    uyrb   c                     t           j                            |          }|                     t	          |                    S )z
        Set the modulus of ``f``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(5*x**2 + 2*x - 1, x).set_modulus(2)
        Poly(x**2 + 1, x, modulus=2)

        )ri   Modulus
preprocessr   r(   )rZ   moduluss     r_   set_moduluszPoly.set_modulus0  s1     /,,W55||BwKK(((rb   c                     |                                  }|j        r!t          |                                          S t	          d          )z
        Get the modulus of ``f``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**2 + 1, modulus=2).get_modulus()
        2

        z$not a polynomial over a Galois field)r   is_FiniteFieldr   characteristicr8   )rZ   r   s     r_   get_moduluszPoly.get_modulusA  sF       	J60022333!"HIIIrb   c                     || j         v rD|j        r|                     ||          S 	 |                     ||          S # t          $ r Y nw xY w|                                                     ||          S )z)Internal implementation of :func:`subs`. )rT   	is_numberevalreplacer8   rX   subs)rZ   oldr   s      r_   
_eval_subszPoly._eval_subsV  s    !&= 	} vvc3'''99S#...&   D yy{{S)))s   > 
A
Ac                     | j                                         \  }fdt          | j                  D             }|                     ||          S )a  
        Remove unnecessary generators from ``f``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import a, b, c, d, x

        >>> Poly(a + x, a, b, c, d, x).exclude()
        Poly(a + x, a, x, domain='ZZ')

        c                 "    g | ]\  }}|v	|S r   r   )r   jr   Js      r_   r   z Poly.exclude.<locals>.<listcomp>r  s&    BBB3qzBBBBrb   r   )re   rh   	enumeraterT   r   )rZ   r   rT   r   s      @r_   rh   zPoly.excludec  sQ     3BBBB)AF"3"3BBBuuStu$$$rb   c                    | | j         r
| j        |}}nt          d          ||k    s	|| j        vr| S || j        v ru|| j        vrl|                                 }|j        r	||j        vrHt          | j                  }|||                    |          <   | 	                    | j
        |          S t          d|d|d|           )a  
        Replace ``x`` with ``y`` in generators list.

        Examples
        ========

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

        >>> Poly(x**2 + 1, x).replace(x, y)
        Poly(y**2 + 1, y, domain='ZZ')

        Nz(syntax supported only in univariate caser   zCannot replace r    in )is_univariater   r8   rT   r   r   r   rq   indexr   re   )rZ   xy_ignorer   rT   s         r_   r   zPoly.replacev  s      	@ @ua1%>@ @ @ 6 	Qaf_ 	H; 	/1AF? 	/,,..C# /q'; /AF||&'TZZ]]#uuQUu...o111aaaKLLLrb   c                 @     |                                  j        |i |S )z-Match expression from Poly. See Basic.match())rX   match)rZ   rw   kwargss      r_   r  z
Poly.match  s"     qyy{{ $1&111rb   c                    t          j        d|          }|st          | j        |          }n4t	          | j                  t	          |          k    rt          d          t          t          t          t          | j
                                        | j        |                               }|                     t          || j
        j        t          |          dz
            |          S )a  
        Efficiently apply new order of generators.

        Examples
        ========

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

        >>> Poly(x**2 + x*y**2, x, y).reorder(y, x)
        Poly(y**2*x + x**2, y, x, domain='ZZ')

        r   r   z7generators list can differ only up to order of elementsr{   r   )ri   Optionsr?   rT   r   r8   rn   rq   r   rA   re   r   r   r0   r   r}   )rZ   rT   rw   rx   re   s        r_   r   zPoly.reorder  s     ob$'' 	Kaf#...DD[[CII% 	K!IK K K 4]15==??AFDIIJKKLLuuSaeiTQ77duCCCrb   c                    |                      d          }|                     |          }i }|                                D ];\  }}t          |d|                   rt	          d| z            ||||d         <   <| j        |d         } | j        t          j        |t          |          dz
  | j
        j                  g|R  S )a(  
        Remove dummy generators from ``f`` that are to the left of
        specified ``gen`` in the generators as ordered. When ``gen``
        is an integer, it refers to the generator located at that
        position within the tuple of generators of ``f``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x, y, z

        >>> Poly(y**2 + y*z**2, x, y, z).ltrim(y)
        Poly(y**2 + y*z**2, y, z, domain='ZZ')
        >>> Poly(z, x, y, z).ltrim(-1)
        Poly(z, z, domain='ZZ')

        T)nativeNzCannot left trim %sr{   )as_dict_gen_to_levelr   anyr8   rT   r   r0   r   r}   re   r   )rZ   r   re   r   termsr   r   rT   s           r_   ltrimz
Poly.ltrim  s    & iiti$$OOC  IIKK 	% 	%LE55!9~~ A%&;a&?@@@$E%)vabbzquS]5#d))a-CCKdKKKKrb   c                 >   t                      }|D ]U}	 | j                            |          }|                    |           3# t          $ r t          | d|d          w xY w|                                 D ]!}t          |          D ]\  }}||vr|r  dS "dS )aJ  
        Return ``True`` if ``Poly(f, *gens)`` retains ground domain.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x, y, z

        >>> Poly(x*y + 1, x, y, z).has_only_gens(x, y)
        True
        >>> Poly(x*y + z, x, y, z).has_only_gens(x, y)
        False

         doesn't have  as generatorFT)r   rT   r  add
ValueErrorr=   r   r   )rZ   rT   indicesr   r  r   r   elts           r_   has_only_genszPoly.has_only_gens  s      %% 	# 	#C#S))
 E""""	  B B B%9:CCC@B B BB XXZZ 	! 	!E#E** ! !3G# ! ! 555! ts   A A$c                     t          | j        d          r| j                                        }nt          | d          |                     |          S )z
        Make the ground domain a ring.

        Examples
        ========

        >>> from sympy import Poly, QQ
        >>> from sympy.abc import x

        >>> Poly(x**2 + 1, domain=QQ).to_ring()
        Poly(x**2 + 1, x, domain='ZZ')

        to_ring)hasattrre   r  r3   r   rZ   r]   s     r_   r  zPoly.to_ring  sI     15)$$ 	6U]]__FF'9555uuV}}rb   c                     t          | j        d          r| j                                        }nt          | d          |                     |          S )z
        Make the ground domain a field.

        Examples
        ========

        >>> from sympy import Poly, ZZ
        >>> from sympy.abc import x

        >>> Poly(x**2 + 1, x, domain=ZZ).to_field()
        Poly(x**2 + 1, x, domain='QQ')

        r   )r  re   r   r3   r   r  s     r_   r   zPoly.to_field  K     15*%% 	7U^^%%FF':666uuV}}rb   c                     t          | j        d          r| j                                        }nt          | d          |                     |          S )z
        Make the ground domain exact.

        Examples
        ========

        >>> from sympy import Poly, RR
        >>> from sympy.abc import x

        >>> Poly(x**2 + 1.0, x, domain=RR).to_exact()
        Poly(x**2 + 1, x, domain='QQ')

        to_exact)r  re   r"  r3   r   r  s     r_   r"  zPoly.to_exact'  r   rb   c                     t          |                     d          || j        j        pd          \  }}|                     || j        |          S )a  
        Recalculate the ground domain of a polynomial.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> f = Poly(x**2 + 1, x, domain='QQ[y]')
        >>> f
        Poly(x**2 + 1, x, domain='QQ[y]')

        >>> f.retract()
        Poly(x**2 + 1, x, domain='ZZ')
        >>> f.retract(field=True)
        Poly(x**2 + 1, x, domain='QQ')

        Tr   N)r   	compositer   )r'   r  r   r   r   rT   )rZ   r   r   re   s       r_   retractzPoly.retract<  sX    ( $AII4I$8$818#8#@DB B BS{{3s{333rb   c                 (   |d||}}}n|                      |          }t          |          t          |          }}t          | j        d          r| j                            |||          }nt          | d          |                     |          S )z1Take a continuous subsequence of terms of ``f``. Nr   slice)r  intr  re   r)  r3   r   )rZ   r  mnr   r]   s         r_   r)  z
Poly.sliceT  s     	#A!qAA""A1vvs1vv115'"" 	4U[[Aq))FF'7333uuV}}rb   c                 R      fd j                             |          D             S )aQ  
        Returns all non-zero coefficients from ``f`` in lex order.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**3 + 2*x + 3, x).coeffs()
        [1, 2, 3]

        See Also
        ========
        all_coeffs
        coeff_monomial
        nth

        c                 N    g | ]!}j         j                            |          "S r   re   r   r   r   r   rZ   s     r_   r   zPoly.coeffs.<locals>.<listcomp>x  s+    III!	""1%%IIIrb   rg   )re   r   rZ   rg   s   ` r_   r   zPoly.coeffsd  s0    ( JIIIqu||%|/H/HIIIIrb   c                 8    | j                             |          S )aU  
        Returns all non-zero monomials from ``f`` in lex order.

        Examples
        ========

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

        >>> Poly(x**2 + 2*x*y**2 + x*y + 3*y, x, y).monoms()
        [(2, 0), (1, 2), (1, 1), (0, 1)]

        See Also
        ========
        all_monoms

        r1  )re   r   r2  s     r_   r   zPoly.monomsz  s    $ u||%|(((rb   c                 R      fd j                             |          D             S )ac  
        Returns all non-zero terms from ``f`` in lex order.

        Examples
        ========

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

        >>> Poly(x**2 + 2*x*y**2 + x*y + 3*y, x, y).terms()
        [((2, 0), 1), ((1, 2), 2), ((1, 1), 1), ((0, 1), 3)]

        See Also
        ========
        all_terms

        c                 X    g | ]&\  }}|j         j                            |          f'S r   r/  r   r+  r   rZ   s      r_   r   zPoly.terms.<locals>.<listcomp>  s4    PPPtq!AEI&&q))*PPPrb   r1  )re   r  r2  s   ` r_   r  z
Poly.terms  s0    $ QPPPqu{{{7O7OPPPPrb   c                 N      fd j                                         D             S )a  
        Returns all coefficients from a univariate polynomial ``f``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**3 + 2*x - 1, x).all_coeffs()
        [1, 0, 2, -1]

        c                 N    g | ]!}j         j                            |          "S r   r/  r0  s     r_   r   z#Poly.all_coeffs.<locals>.<listcomp>  s+    BBB!	""1%%BBBrb   )re   
all_coeffsr   s   `r_   r9  zPoly.all_coeffs  s.     CBBBqu/?/?/A/ABBBBrb   c                 4    | j                                         S )a?  
        Returns all monomials from a univariate polynomial ``f``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**3 + 2*x - 1, x).all_monoms()
        [(3,), (2,), (1,), (0,)]

        See Also
        ========
        all_terms

        )re   
all_monomsr   s    r_   r;  zPoly.all_monoms  s    $ u!!!rb   c                 N      fd j                                         D             S )a  
        Returns all terms from a univariate polynomial ``f``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**3 + 2*x - 1, x).all_terms()
        [((3,), 1), ((2,), 0), ((1,), 2), ((0,), -1)]

        c                 X    g | ]&\  }}|j         j                            |          f'S r   r/  r6  s      r_   r   z"Poly.all_terms.<locals>.<listcomp>  s4    IIItq!AEI&&q))*IIIrb   )re   	all_termsr   s   `r_   r>  zPoly.all_terms  s,     JIIIqu7H7HIIIIrb   c                     i }|                                  D ]L\  }} |||          }t          |t                    r|\  }}n|}|r||vr|||<   :t          d|z            M | j        |g|p| j        R i |S )ah  
        Apply a function to all terms of ``f``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> def func(k, coeff):
        ...     k = k[0]
        ...     return coeff//10**(2-k)

        >>> Poly(x**2 + 20*x + 400).termwise(func)
        Poly(x**2 + 2*x + 4, x, domain='ZZ')

        z%s monomial was generated twice)r  rQ   tupler8   r   rT   )rZ   r^   rT   rw   r  r   r   r]   s           r_   termwisezPoly.termwise  s    $ GGII 	C 	CLE5T%''F&%(( %uu C% C#(E%LL)9EAC C C	C q{5<DNAF<<<t<<<rb   c                 D    t          |                                           S )z
        Returns the number of non-zero terms in ``f``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**2 + 2*x - 1).length()
        3

        )r}   r  r   s    r_   lengthzPoly.length  s     199;;rb   Fc                 r    |r| j                             |          S | j                             |          S )a  
        Switch to a ``dict`` representation.

        Examples
        ========

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

        >>> Poly(x**2 + 2*x*y**2 - y, x, y).as_dict()
        {(0, 1): -1, (1, 2): 2, (2, 0): 1}

        r$  )re   r   r   )rZ   r  r   s      r_   r  zPoly.as_dict  s;      	25==d=+++5&&D&111rb   c                 j    |r| j                                         S | j                                         S )z%Switch to a ``list`` representation. )re   to_listto_sympy_list)rZ   r  s     r_   as_listzPoly.as_list  s.     	)5==??"5&&(((rb   c                    |s| j         S t          |          dk    rt          |d         t                    rt|d         }t	          | j                  }|                                D ]C\  }}	 |                    |          }|||<   !# t          $ r t          | d|d          w xY wt          | j                                        g|R  S )ar  
        Convert a Poly instance to an Expr instance.

        Examples
        ========

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

        >>> f = Poly(x**2 + 2*x*y**2 - y, x, y)

        >>> f.as_expr()
        x**2 + 2*x*y**2 - y
        >>> f.as_expr({x: 5})
        10*y**2 - y + 25
        >>> f.as_expr(5, 6)
        379

        r{   r   r  r  )r   r}   rQ   rn   rq   rT   r   r  r  r=   r>   re   r   )rZ   rT   mappingr   valuer  s         r_   rX   zPoly.as_expr%  s    (  	6Mt99> 	(ja$77 	(1gG<<D%mmoo ( (
U( JJsOOE
 #(DKK	 " F F F)=>QQDF F FF qu2244<t<<<<s   .B		 B)c                 \    	 t          | g|R i |}|j        sdS |S # t          $ r Y dS w xY w)a{  Converts ``self`` to a polynomial or returns ``None``.

        >>> from sympy import sin
        >>> from sympy.abc import x, y

        >>> print((x**2 + x*y).as_poly())
        Poly(x**2 + x*y, x, y, domain='ZZ')

        >>> print((x**2 + x*y).as_poly(x, y))
        Poly(x**2 + x*y, x, y, domain='ZZ')

        >>> print((x**2 + sin(y)).as_poly(x, y))
        None

        N)rR   rr   r8   )r   rT   rw   polys       r_   as_polyzPoly.as_polyK  sZ     	,t,,,t,,D< t 	 	 	44	s     
++c                     t          | j        d          r| j                                        }nt          | d          |                     |          S )a  
        Convert algebraic coefficients to rationals.

        Examples
        ========

        >>> from sympy import Poly, I
        >>> from sympy.abc import x

        >>> Poly(x**2 + I*x + 1, x, extension=I).lift()
        Poly(x**4 + 3*x**2 + 1, x, domain='QQ')

        lift)r  re   rP  r3   r   r  s     r_   rP  z	Poly.lifte  sI     15&!! 	3UZZ\\FF'6222uuV}}rb   c                     t          | j        d          r| j                                        \  }}nt          | d          ||                     |          fS )a+  
        Reduce degree of ``f`` by mapping ``x_i**m`` to ``y_i``.

        Examples
        ========

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

        >>> Poly(x**6*y**2 + x**3 + 1, x, y).deflate()
        ((3, 2), Poly(x**2*y + x + 1, x, y, domain='ZZ'))

        deflate)r  re   rR  r3   r   rZ   r   r]   s      r_   rR  zPoly.deflatez  sR     15)$$ 	6IAvv'9555!%%--rb   c                 >   | j         j        }|j        r| S |j        st	          d|z            t          | j         d          r| j                             |          }nt          | d          |r|j        | j	        z   }n| j	        |j        z   } | j
        |g|R  S )a  
        Inject ground domain generators into ``f``.

        Examples
        ========

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

        >>> f = Poly(x**2*y + x*y**3 + x*y + 1, x)

        >>> f.inject()
        Poly(x**2*y + x*y**3 + x*y + 1, x, y, domain='ZZ')
        >>> f.inject(front=True)
        Poly(y**3*x + y*x**2 + y*x + 1, y, x, domain='ZZ')

        z Cannot inject generators over %sinjectfront)re   r   is_Numericalrr   r4   r  rU  r3   r   rT   r   )rZ   rW  r   r]   rT   s        r_   rU  zPoly.inject  s    $ ei 	HH 	H@3FGGG15(## 	5U\\\..FF'8444 	(;'DD6CK'DquV#d####rb   c                    | j         j        }|j        st          d|z            t	          |          }| j        d|         |k    r| j        |d         d}}n6| j        | d         |k    r| j        d|          d}}nt          d           |j        | }t          | j         d          r| j         	                    ||          }nt          | d           | j        |g|R  S )a  
        Eject selected generators into the ground domain.

        Examples
        ========

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

        >>> f = Poly(x**2*y + x*y**3 + x*y + 1, x, y)

        >>> f.eject(x)
        Poly(x*y**3 + (x**2 + x)*y + 1, y, domain='ZZ[x]')
        >>> f.eject(y)
        Poly(y*x**2 + (y**3 + y)*x + 1, x, domain='ZZ[y]')

        zCannot eject generators over %sNTFz'can only eject front or back generatorsejectrV  )re   r   rX  r4   r}   rT   rk   rU  r  rZ  r3   r   )rZ   rT   r   k_gensrW  r]   s          r_   rZ  z
Poly.eject  s   $ ei 	G?#EFFFII6"1": 	;6!"":t5EEVQBCC[D  	;6#A2#;5EE%9; ; ; cj$15'"" 	4U[[E[22FF'7333quV$e$$$$rb   c                     t          | j        d          r| j                                        \  }}nt          | d          ||                     |          fS )a  
        Remove GCD of terms from the polynomial ``f``.

        Examples
        ========

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

        >>> Poly(x**6*y**2 + x**3*y, x, y).terms_gcd()
        ((3, 1), Poly(x**3*y + 1, x, y, domain='ZZ'))

        	terms_gcd)r  re   r^  r3   r   rS  s      r_   r^  zPoly.terms_gcd  sT     15+&& 	8))IAvv';777!%%--rb   c                     t          | j        d          r| j                            |          }nt          | d          |                     |          S )z
        Add an element of the ground domain to ``f``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x + 1).add_ground(2)
        Poly(x + 3, x, domain='ZZ')

        
add_ground)r  re   r`  r3   r   rZ   r   r]   s      r_   r`  zPoly.add_ground  O     15,'' 	9U%%e,,FF'<888uuV}}rb   c                     t          | j        d          r| j                            |          }nt          | d          |                     |          S )z
        Subtract an element of the ground domain from ``f``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x + 1).sub_ground(2)
        Poly(x - 1, x, domain='ZZ')

        
sub_ground)r  re   rd  r3   r   ra  s      r_   rd  zPoly.sub_ground  rb  rb   c                     t          | j        d          r| j                            |          }nt          | d          |                     |          S )z
        Multiply ``f`` by a an element of the ground domain.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x + 1).mul_ground(2)
        Poly(2*x + 2, x, domain='ZZ')

        
mul_ground)r  re   rf  r3   r   ra  s      r_   rf  zPoly.mul_ground  rb  rb   c                     t          | j        d          r| j                            |          }nt          | d          |                     |          S )aO  
        Quotient of ``f`` by a an element of the ground domain.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(2*x + 4).quo_ground(2)
        Poly(x + 2, x, domain='ZZ')

        >>> Poly(2*x + 3).quo_ground(2)
        Poly(x + 1, x, domain='ZZ')

        
quo_ground)r  re   rh  r3   r   ra  s      r_   rh  zPoly.quo_ground2  sO    " 15,'' 	9U%%e,,FF'<888uuV}}rb   c                     t          | j        d          r| j                            |          }nt          | d          |                     |          S )a  
        Exact quotient of ``f`` by a an element of the ground domain.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(2*x + 4).exquo_ground(2)
        Poly(x + 2, x, domain='ZZ')

        >>> Poly(2*x + 3).exquo_ground(2)
        Traceback (most recent call last):
        ...
        ExactQuotientFailed: 2 does not divide 3 in ZZ

        exquo_ground)r  re   rj  r3   r   ra  s      r_   rj  zPoly.exquo_groundJ  sO    & 15.)) 	;U''..FF'>:::uuV}}rb   c                     t          | j        d          r| j                                        }nt          | d          |                     |          S )z
        Make all coefficients in ``f`` positive.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**2 - 1, x).abs()
        Poly(x**2 + 1, x, domain='ZZ')

        abs)r  re   rl  r3   r   r  s     r_   rl  zPoly.absd  sI     15%   	2UYY[[FF'5111uuV}}rb   c                     t          | j        d          r| j                                        }nt          | d          |                     |          S )a4  
        Negate all coefficients in ``f``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**2 - 1, x).neg()
        Poly(-x**2 + 1, x, domain='ZZ')

        >>> -Poly(x**2 - 1, x)
        Poly(-x**2 + 1, x, domain='ZZ')

        neg)r  re   rn  r3   r   r  s     r_   rn  zPoly.negy  I    " 15%   	2UYY[[FF'5111uuV}}rb   c                    t          |          }|j        s|                     |          S |                     |          \  }}}}t	          | j        d          r|                    |          }nt          | d           ||          S )a[  
        Add two polynomials ``f`` and ``g``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**2 + 1, x).add(Poly(x - 2, x))
        Poly(x**2 + x - 1, x, domain='ZZ')

        >>> Poly(x**2 + 1, x) + Poly(x - 2, x)
        Poly(x**2 + x - 1, x, domain='ZZ')

        r  )r!   rr   r`  r   r  re   r  r3   rZ   r[   r   r   r   r   r]   s          r_   r  zPoly.add      " AJJy 	#<<??"xx{{3115%   	2UU1XXFF'5111s6{{rb   c                    t          |          }|j        s|                     |          S |                     |          \  }}}}t	          | j        d          r|                    |          }nt          | d           ||          S )a`  
        Subtract two polynomials ``f`` and ``g``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**2 + 1, x).sub(Poly(x - 2, x))
        Poly(x**2 - x + 3, x, domain='ZZ')

        >>> Poly(x**2 + 1, x) - Poly(x - 2, x)
        Poly(x**2 - x + 3, x, domain='ZZ')

        sub)r!   rr   rd  r   r  re   rt  r3   rq  s          r_   rt  zPoly.sub  rr  rb   c                    t          |          }|j        s|                     |          S |                     |          \  }}}}t	          | j        d          r|                    |          }nt          | d           ||          S )ap  
        Multiply two polynomials ``f`` and ``g``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**2 + 1, x).mul(Poly(x - 2, x))
        Poly(x**3 - 2*x**2 + x - 2, x, domain='ZZ')

        >>> Poly(x**2 + 1, x)*Poly(x - 2, x)
        Poly(x**3 - 2*x**2 + x - 2, x, domain='ZZ')

        r   )r!   rr   rf  r   r  re   r   r3   rq  s          r_   r   zPoly.mul  rr  rb   c                     t          | j        d          r| j                                        }nt          | d          |                     |          S )a3  
        Square a polynomial ``f``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x - 2, x).sqr()
        Poly(x**2 - 4*x + 4, x, domain='ZZ')

        >>> Poly(x - 2, x)**2
        Poly(x**2 - 4*x + 4, x, domain='ZZ')

        sqr)r  re   rw  r3   r   r  s     r_   rw  zPoly.sqr  ro  rb   c                     t          |          }t          | j        d          r| j                            |          }nt	          | d          |                     |          S )aX  
        Raise ``f`` to a non-negative power ``n``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x - 2, x).pow(3)
        Poly(x**3 - 6*x**2 + 12*x - 8, x, domain='ZZ')

        >>> Poly(x - 2, x)**3
        Poly(x**3 - 6*x**2 + 12*x - 8, x, domain='ZZ')

        pow)r*  r  re   ry  r3   r   rZ   r,  r]   s      r_   ry  zPoly.pow  sV    " FF15%   	2UYYq\\FF'5111uuV}}rb   c                     |                      |          \  }}}}t          | j        d          r|                    |          \  }}nt	          | d           ||           ||          fS )a#  
        Polynomial pseudo-division of ``f`` by ``g``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**2 + 1, x).pdiv(Poly(2*x - 4, x))
        (Poly(2*x + 4, x, domain='ZZ'), Poly(20, x, domain='ZZ'))

        pdiv)r   r  re   r|  r3   )rZ   r[   r   r   r   r   qrs           r_   r|  z	Poly.pdiv   sn     xx{{3115&!! 	366!99DAqq'6222s1vvss1vv~rb   c                     |                      |          \  }}}}t          | j        d          r|                    |          }nt	          | d           ||          S )aN  
        Polynomial pseudo-remainder of ``f`` by ``g``.

        Caveat: The function prem(f, g, x) can be safely used to compute
          in Z[x] _only_ subresultant polynomial remainder sequences (prs's).

          To safely compute Euclidean and Sturmian prs's in Z[x]
          employ anyone of the corresponding functions found in
          the module sympy.polys.subresultants_qq_zz. The functions
          in the module with suffix _pg compute prs's in Z[x] employing
          rem(f, g, x), whereas the functions with suffix _amv
          compute prs's in Z[x] employing rem_z(f, g, x).

          The function rem_z(f, g, x) differs from prem(f, g, x) in that
          to compute the remainder polynomials in Z[x] it premultiplies
          the divident times the absolute value of the leading coefficient
          of the divisor raised to the power degree(f, x) - degree(g, x) + 1.


        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**2 + 1, x).prem(Poly(2*x - 4, x))
        Poly(20, x, domain='ZZ')

        prem)r   r  re   r  r3   rq  s          r_   r  z	Poly.prem7  s^    < xx{{3115&!! 	3VVAYYFF'6222s6{{rb   c                     |                      |          \  }}}}t          | j        d          r|                    |          }nt	          | d           ||          S )a  
        Polynomial pseudo-quotient of ``f`` by ``g``.

        See the Caveat note in the function prem(f, g).

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**2 + 1, x).pquo(Poly(2*x - 4, x))
        Poly(2*x + 4, x, domain='ZZ')

        >>> Poly(x**2 - 1, x).pquo(Poly(2*x - 2, x))
        Poly(2*x + 2, x, domain='ZZ')

        pquo)r   r  re   r  r3   rq  s          r_   r  z	Poly.pquo^  s^    & xx{{3115&!! 	3VVAYYFF'6222s6{{rb   c                 \   |                      |          \  }}}}t          | j        d          rc	 |                    |          }n\# t          $ r?}|                    |                                 |                                          d}~ww xY wt          | d           ||          S )a  
        Polynomial exact pseudo-quotient of ``f`` by ``g``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**2 - 1, x).pexquo(Poly(2*x - 2, x))
        Poly(2*x + 2, x, domain='ZZ')

        >>> Poly(x**2 + 1, x).pexquo(Poly(2*x - 4, x))
        Traceback (most recent call last):
        ...
        ExactQuotientFailed: 2*x - 4 does not divide x**2 + 1

        pexquoN)r   r  re   r  r:   r   rX   r3   )rZ   r[   r   r   r   r   r]   excs           r_   r  zPoly.pexquoz  s    & xx{{3115(## 	58!& 8 8 8ggaiikk199;;7778 (8444s6{{s   A 
B:BBc                    |                      |          \  }}}}d}|r8|j        r1|j        s*|                                |                                }}d}t	          | j        d          r|                    |          \  }}	nt          | d          |r>	 |                                |	                                }}
|
|}	}n# t          $ r Y nw xY w ||           ||	          fS )a  
        Polynomial division with remainder of ``f`` by ``g``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**2 + 1, x).div(Poly(2*x - 4, x))
        (Poly(1/2*x + 1, x, domain='QQ'), Poly(5, x, domain='QQ'))

        >>> Poly(x**2 + 1, x).div(Poly(2*x - 4, x), auto=False)
        (Poly(0, x, domain='ZZ'), Poly(x**2 + 1, x, domain='ZZ'))

        FTdiv)
r   is_Ringis_Fieldr   r  re   r  r3   r  r5   )rZ   r[   autor   r   r   r   r'  r}  r~  QRs               r_   r  zPoly.div  s   " !S!Q 	CK 	 	::<<qAG15%   	25588DAqq'5111 	yy{{AIIKK1 !1 "   
 s1vvss1vv~s   (C 
CCc                    |                      |          \  }}}}d}|r8|j        r1|j        s*|                                |                                }}d}t	          | j        d          r|                    |          }nt          | d          |r&	 |                                }n# t          $ r Y nw xY w ||          S )ao  
        Computes the polynomial remainder of ``f`` by ``g``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**2 + 1, x).rem(Poly(2*x - 4, x))
        Poly(5, x, domain='ZZ')

        >>> Poly(x**2 + 1, x).rem(Poly(2*x - 4, x), auto=False)
        Poly(x**2 + 1, x, domain='ZZ')

        FTrem)
r   r  r  r   r  re   r  r3   r  r5   )	rZ   r[   r  r   r   r   r   r'  r~  s	            r_   r  zPoly.rem      " !S!Q 	CK 	 	::<<qAG15%   	2aAA'5111 	IIKK!    s1vv   B* *
B76B7c                    |                      |          \  }}}}d}|r8|j        r1|j        s*|                                |                                }}d}t	          | j        d          r|                    |          }nt          | d          |r&	 |                                }n# t          $ r Y nw xY w ||          S )aa  
        Computes polynomial quotient of ``f`` by ``g``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**2 + 1, x).quo(Poly(2*x - 4, x))
        Poly(1/2*x + 1, x, domain='QQ')

        >>> Poly(x**2 - 1, x).quo(Poly(x - 1, x))
        Poly(x + 1, x, domain='ZZ')

        FTquo)
r   r  r  r   r  re   r  r3   r  r5   )	rZ   r[   r  r   r   r   r   r'  r}  s	            r_   r  zPoly.quo  r  r  c                 $   |                      |          \  }}}}d}|r8|j        r1|j        s*|                                |                                }}d}t	          | j        d          rc	 |                    |          }n\# t          $ r?}	|	                    | 	                                |	                                          d}	~	ww xY wt          | d          |r&	 |                                }n# t          $ r Y nw xY w ||          S )a  
        Computes polynomial exact quotient of ``f`` by ``g``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**2 - 1, x).exquo(Poly(x - 1, x))
        Poly(x + 1, x, domain='ZZ')

        >>> Poly(x**2 + 1, x).exquo(Poly(2*x - 4, x))
        Traceback (most recent call last):
        ...
        ExactQuotientFailed: 2*x - 4 does not divide x**2 + 1

        FTexquoN)r   r  r  r   r  re   r  r:   r   rX   r3   r  r5   )
rZ   r[   r  r   r   r   r   r'  r}  r  s
             r_   r  z
Poly.exquo
  s"   & !S!Q 	CK 	 	::<<qAG15'"" 	48GGAJJ& 8 8 8ggaiikk199;;7778 (7333 	IIKK!    s1vvs*   -B 
C:CC"C7 7
DDc                 P   t          |t                    rJt          | j                  }| |cxk    r|k     rn n|dk     r||z   S |S t	          d|d|d|          	 | j                            t          |                    S # t          $ r t	          d|z            w xY w)z3Returns level associated with the given generator. r   -z
 <= gen < z expected, got z"a valid generator expected, got %s)rQ   r*  r}   rT   r8   r  r!   r  )rZ   r   rC  s      r_   r  zPoly._gen_to_level4  s    c3 	@[[Fw# = = = = = = = = =7 !C<'J%o'-vvvvvss'< = = =@v||GCLL111 @ @ @%83>@ @ @@s   !&B B%r   c                     |                      |          }t          | j        d          r| j                            |          S t	          | d          )au  
        Returns degree of ``f`` in ``x_j``.

        The degree of 0 is negative infinity.

        Examples
        ========

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

        >>> Poly(x**2 + y*x + 1, x, y).degree()
        2
        >>> Poly(x**2 + y*x + y, x, y).degree(y)
        1
        >>> Poly(0, x).degree()
        -oo

        degree)r  r  re   r  r3   )rZ   r   r   s      r_   r  zPoly.degreeH  sK    ( OOC  15(## 	55<<??"'8444rb   c                 ~    t          | j        d          r| j                                        S t          | d          )z
        Returns a list of degrees of ``f``.

        Examples
        ========

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

        >>> Poly(x**2 + y*x + 1, x, y).degree_list()
        (2, 1)

        degree_list)r  re   r  r3   r   s    r_   r  zPoly.degree_listc  s<     15-(( 	:5$$&&&'=999rb   c                 ~    t          | j        d          r| j                                        S t          | d          )a  
        Returns the total degree of ``f``.

        Examples
        ========

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

        >>> Poly(x**2 + y*x + 1, x, y).total_degree()
        2
        >>> Poly(x + y**5, x, y).total_degree()
        5

        total_degree)r  re   r  r3   r   s    r_   r  zPoly.total_degreev  s<      15.)) 	;5%%''''>:::rb   c                    t          |t                    st          dt          |          z            || j        v r"| j                            |          }| j        }nt          | j                  }| j        |fz   }t          | j        d          r/| 	                    | j        
                    |          |          S t          | d          )a  
        Returns the homogeneous polynomial of ``f``.

        A homogeneous polynomial is a polynomial whose all monomials with
        non-zero coefficients have the same total degree. If you only
        want to check if a polynomial is homogeneous, then use
        :func:`Poly.is_homogeneous`. If you want not only to check if a
        polynomial is homogeneous but also compute its homogeneous order,
        then use :func:`Poly.homogeneous_order`.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x, y, z

        >>> f = Poly(x**5 + 2*x**2*y**2 + 9*x*y**3)
        >>> f.homogenize(z)
        Poly(x**5 + 2*x**2*y**2*z + 9*x*y**3*z, x, y, z, domain='ZZ')

        z``Symbol`` expected, got %s
homogenizer   homogeneous_order)rQ   r    	TypeErrortyperT   r  r}   r  re   r   r  r3   )rZ   sr   rT   s       r_   r  zPoly.homogenize  s    , !V$$ 	E9DGGCDDD; 	!QA6DDAFA6QD=D15,'' 	955))!,,45888#A':;;;rb   c                 ~    t          | j        d          r| j                                        S t          | d          )a-  
        Returns the homogeneous order of ``f``.

        A homogeneous polynomial is a polynomial whose all monomials with
        non-zero coefficients have the same total degree. This degree is
        the homogeneous order of ``f``. If you only want to check if a
        polynomial is homogeneous, then use :func:`Poly.is_homogeneous`.

        Examples
        ========

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

        >>> f = Poly(x**5 + 2*x**3*y**2 + 9*x*y**4)
        >>> f.homogeneous_order()
        5

        r  )r  re   r  r3   r   s    r_   r  zPoly.homogeneous_order  s?    ( 15-.. 	@5**,,,'+>???rb   c                     ||                      |          d         S t          | j        d          r| j                                        }nt	          | d          | j        j                            |          S )z
        Returns the leading coefficient of ``f``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(4*x**3 + 2*x**2 + 3*x, x).LC()
        4

        Nr   LC)r   r  re   r  r3   r   r   )rZ   rg   r]   s      r_   r  zPoly.LC  sn      	&88E??1%%15$ 	1UXXZZFF'4000uy!!&)))rb   c                     t          | j        d          r| j                                        }nt          | d          | j        j                            |          S )z
        Returns the trailing coefficient of ``f``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**3 + 2*x**2 + 3*x, x).TC()
        0

        TC)r  re   r  r3   r   r   r  s     r_   r  zPoly.TC  sQ     15$ 	1UXXZZFF'4000uy!!&)))rb   c                     t          | j        d          r|                     |          d         S t          | d          )z
        Returns the last non-zero coefficient of ``f``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**3 + 2*x**2 + 3*x, x).EC()
        3

        r   EC)r  re   r   r3   r2  s     r_   r  zPoly.EC  s=     15(## 	188E??2&&'4000rb   c                 F     | j         t          || j                  j         S )aE  
        Returns the coefficient of ``monom`` in ``f`` if there, else None.

        Examples
        ========

        >>> from sympy import Poly, exp
        >>> from sympy.abc import x, y

        >>> p = Poly(24*x*y*exp(8) + 23*x, x, y)

        >>> p.coeff_monomial(x)
        23
        >>> p.coeff_monomial(y)
        0
        >>> p.coeff_monomial(x*y)
        24*exp(8)

        Note that ``Expr.coeff()`` behaves differently, collecting terms
        if possible; the Poly must be converted to an Expr to use that
        method, however:

        >>> p.as_expr().coeff(x)
        24*y*exp(8) + 23
        >>> p.as_expr().coeff(y)
        24*x*exp(8)
        >>> p.as_expr().coeff(x*y)
        24*exp(8)

        See Also
        ========
        nth: more efficient query using exponents of the monomial's generators

        )nthr.   rT   	exponents)rZ   r   s     r_   coeff_monomialzPoly.coeff_monomial  s#    F quhuaf--788rb   c                 R   t          | j        d          rdt          |          t          | j                  k    rt	          d           | j        j        t          t          t          |                     }nt          | d          | j        j
                            |          S )a.  
        Returns the ``n``-th coefficient of ``f`` where ``N`` are the
        exponents of the generators in the term of interest.

        Examples
        ========

        >>> from sympy import Poly, sqrt
        >>> from sympy.abc import x, y

        >>> Poly(x**3 + 2*x**2 + 3*x, x).nth(2)
        2
        >>> Poly(x**3 + 2*x*y**2 + y**2, x, y).nth(1, 2)
        2
        >>> Poly(4*sqrt(x)*y)
        Poly(4*y*(sqrt(x)), y, sqrt(x), domain='ZZ')
        >>> _.nth(1, 1)
        4

        See Also
        ========
        coeff_monomial

        r  z,exponent of each generator must be specified)r  re   r}   rT   r  r  rq   r   r*  r3   r   r   )rZ   Nr]   s      r_   r  zPoly.nth+  s    2 15%   	21vvQV$ Q !OPPPQUYSa[[ 1 12FF'5111uy!!&)))rb   r{   c                      t          d          )NzyEither convert to Expr with `as_expr` method to use Expr's coeff method or else use the `coeff_monomial` method of Polys.rk   )rZ   r  r,  rights       r_   r   z
Poly.coeffM  s     "01 1 	1rb   c                 ^    t          |                     |          d         | j                  S )a  
        Returns the leading monomial of ``f``.

        The Leading monomial signifies the monomial having
        the highest power of the principal generator in the
        expression f.

        Examples
        ========

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

        >>> Poly(4*x**2 + 2*x*y**2 + x*y + 3*y, x, y).LM()
        x**2*y**0

        r   r.   r   rT   r2  s     r_   LMzPoly.LMY  s%    $ *AF333rb   c                 ^    t          |                     |          d         | j                  S )z
        Returns the last non-zero monomial of ``f``.

        Examples
        ========

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

        >>> Poly(4*x**2 + 2*x*y**2 + x*y + 3*y, x, y).EM()
        x**0*y**1

        r  r  r2  s     r_   EMzPoly.EMm  s%     +QV444rb   c                 l    |                      |          d         \  }}t          || j                  |fS )a  
        Returns the leading term of ``f``.

        The Leading term signifies the term having
        the highest power of the principal generator in the
        expression f along with its coefficient.

        Examples
        ========

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

        >>> Poly(4*x**2 + 2*x*y**2 + x*y + 3*y, x, y).LT()
        (x**2*y**0, 4)

        r   r  r.   rT   rZ   rg   r   r   s       r_   LTzPoly.LT}  s3    $ wwu~~a(uqv&&--rb   c                 l    |                      |          d         \  }}t          || j                  |fS )z
        Returns the last non-zero term of ``f``.

        Examples
        ========

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

        >>> Poly(4*x**2 + 2*x*y**2 + x*y + 3*y, x, y).ET()
        (x**0*y**1, 3)

        r  r  r  s       r_   ETzPoly.ET  s3     wwu~~b)uqv&&--rb   c                     t          | j        d          r| j                                        }nt          | d          | j        j                            |          S )z
        Returns maximum norm of ``f``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(-x**2 + 2*x - 3, x).max_norm()
        3

        max_norm)r  re   r  r3   r   r   r  s     r_   r  zPoly.max_norm  sS     15*%% 	7U^^%%FF':666uy!!&)))rb   c                     t          | j        d          r| j                                        }nt          | d          | j        j                            |          S )z
        Returns l1 norm of ``f``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(-x**2 + 2*x - 3, x).l1_norm()
        6

        l1_norm)r  re   r  r3   r   r   r  s     r_   r  zPoly.l1_norm  Q     15)$$ 	6U]]__FF'9555uy!!&)))rb   c                    | }|j         j        j        st          j        |fS |                                }|j        r|j         j                                        }t          |j         d          r|j         	                                \  }}nt          |d          |                    |          |                    |          }}|r|j        s||fS ||                                fS )a  
        Clear denominators, but keep the ground domain.

        Examples
        ========

        >>> from sympy import Poly, S, QQ
        >>> from sympy.abc import x

        >>> f = Poly(x/2 + S(1)/3, x, domain=QQ)

        >>> f.clear_denoms()
        (6, Poly(3*x + 2, x, domain='QQ'))
        >>> f.clear_denoms(convert=True)
        (6, Poly(3*x + 2, x, domain='ZZ'))

        clear_denoms)re   r   r  r   Oner   has_assoc_Ringget_ringr  r  r3   r   r   r  )r   r   rZ   r   r   r]   s         r_   r  zPoly.clear_denoms  s    $ uy! 	5!8Ollnn 	'%)$$&&C15.)) 	;E..00ME66'>:::<<&&fq 	&c0 	&!8O!))++%%rb   c                 J   | }|                     |          \  }}}} ||          } ||          }|j        r|j        s||fS |                    d          \  }}|                    d          \  }}|                    |          }|                    |          }||fS )a  
        Clear denominators in a rational function ``f/g``.

        Examples
        ========

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

        >>> f = Poly(x**2/y + 1, x)
        >>> g = Poly(x**3 + y, x)

        >>> p, q = f.rat_clear_denoms(g)

        >>> p
        Poly(x**2 + y, x, domain='ZZ[y]')
        >>> q
        Poly(y*x**3 + y**2, x, domain='ZZ[y]')

        Tr   )r   r  r  r  rf  )r   r[   rZ   r   r   abs          r_   rat_clear_denomszPoly.rat_clear_denoms  s    * !S!QCFFCFF 	!3 	a4K~~d~++1~~d~++1LLOOLLOO!trb   c                    | }|                     dd          r%|j        j        j        r|                                }t          |j        d          r|s.|                    |j                            d                    S |j        }|D ]W}t          |t                    r|\  }}n|d}}|                    t          |          |                    |                    }X|                    |          S t          |d          )a  
        Computes indefinite integral of ``f``.

        Examples
        ========

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

        >>> Poly(x**2 + 2*x + 1, x).integrate()
        Poly(1/3*x**3 + x**2 + x, x, domain='QQ')

        >>> Poly(x*y**2 + x, x, y).integrate((0, 1), (1, 0))
        Poly(1/2*x**2*y**2 + 1/2*x**2, x, y, domain='QQ')

        r  T	integrater{   r+  )getre   r   r  r   r  r   r  rQ   r@  r*  r  r3   )r   specsrw   rZ   re   specr   r+  s           r_   r  zPoly.integrate	  s    " 88FD!! 	aei&7 	

A15+&& 	8 3uuQU__q_11222%C B BdE** %!FC!1CmmCFFAOOC,@,@AA55::';777rb   c                    |                     dd          st          | g|R i |S t          | j        d          r|s.|                     | j                            d                    S | j        }|D ]W}t          |t                    r|\  }}n|d}}|                    t          |          | 	                    |                    }X|                     |          S t          | d          )aX  
        Computes partial derivative of ``f``.

        Examples
        ========

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

        >>> Poly(x**2 + 2*x + 1, x).diff()
        Poly(2*x + 2, x, domain='ZZ')

        >>> Poly(x*y**2 + x, x, y).diff((0, 0), (1, 1))
        Poly(2*x*y, x, y, domain='ZZ')

        evaluateTdiffr{   r  )r  r   r  re   r   r  rQ   r@  r*  r  r3   )rZ   r  r	  re   r  r   r+  s          r_   r  z	Poly.diffC	  s    " zz*d++ 	3a2%222622215&!! 	3 .uuQUZZ!Z__---%C = =dE** %!FC!1Chhs1vvqs';';<<55::'6222rb   c                    | }|t          |t                    r4|}|                                D ]\  }}|                    ||          }|S t          |t          t
          f          ri|}t          |          t          |j                  k    rt          d          t          |j        |          D ]\  }}|                    ||          }|S d|}}	n|
                    |          }	t          |j        d          st          |d          	 |j                            ||	          }
n# t          $ r |st          d|d|j        j                  t#          |g          \  }\  }|                                                    ||j                  }|                    |          }|                    ||          }|j                            ||	          }
Y nw xY w|                    |
|	          S )a  
        Evaluate ``f`` at ``a`` in the given variable.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x, y, z

        >>> Poly(x**2 + 2*x + 3, x).eval(2)
        11

        >>> Poly(2*x*y + 3*x + y + 2, x, y).eval(x, 2)
        Poly(5*y + 8, y, domain='ZZ')

        >>> f = Poly(2*x*y + 3*x + y + 2*z, x, y, z)

        >>> f.eval({x: 2})
        Poly(5*y + 2*z + 6, y, z, domain='ZZ')
        >>> f.eval({x: 2, y: 5})
        Poly(2*z + 31, z, domain='ZZ')
        >>> f.eval({x: 2, y: 5, z: 7})
        45

        >>> f.eval((2, 5))
        Poly(2*z + 31, z, domain='ZZ')
        >>> f(2, 5)
        Poly(2*z + 31, z, domain='ZZ')

        Nztoo many values providedr   r   zCannot evaluate at r  r   )rQ   rn   r   r   r@  rq   r}   rT   r  r   r  r  re   r3   r5   r4   r   r'   r   unify_with_symbolsr   r   r   )r   r  r  r  rZ   rJ  r   rK  valuesr   r]   a_domain
new_domains                r_   r   z	Poly.evalk	  s   >  	#!T"" ")--// + +JCsE**AAAt}-- v;;QV, A$%?@@@"%aff"5"5 + +JCsE**AA!1""Aquf%% 	3'6222	*UZZ1%%FF 
	* 
	* 
	* 	*!k111aeii"PQQQ 0! 5 5#1\\^^>>xPP
LL,,&&q(33Aq))
	* uuVAu&&&s   D/ /B3G%$G%c                 ,    |                      |          S )az  
        Evaluate ``f`` at the give values.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x, y, z

        >>> f = Poly(2*x*y + 3*x + y + 2*z, x, y, z)

        >>> f(2)
        Poly(5*y + 2*z + 6, y, z, domain='ZZ')
        >>> f(2, 5)
        Poly(2*z + 31, z, domain='ZZ')
        >>> f(2, 5, 7)
        45

        )r   )rZ   r  s     r_   __call__zPoly.__call__	  s    ( vvf~~rb   c                 @   |                      |          \  }}}}|r/|j        r(|                                |                                }}t          | j        d          r|                    |          \  }}nt          | d           ||           ||          fS )a  
        Half extended Euclidean algorithm of ``f`` and ``g``.

        Returns ``(s, h)`` such that ``h = gcd(f, g)`` and ``s*f = h (mod g)``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> f = x**4 - 2*x**3 - 6*x**2 + 12*x + 15
        >>> g = x**3 + x**2 - 4*x - 4

        >>> Poly(f).half_gcdex(Poly(g))
        (Poly(-1/5*x + 3/5, x, domain='QQ'), Poly(x + 1, x, domain='QQ'))

        
half_gcdex)r   r  r   r  re   r  r3   )	rZ   r[   r  r   r   r   r   r  hs	            r_   r  zPoly.half_gcdex	  s    & !S!Q 	.CK 	.::<<qA15,'' 	9<<??DAqq'<888s1vvss1vv~rb   c                 V   |                      |          \  }}}}|r/|j        r(|                                |                                }}t          | j        d          r|                    |          \  }}}	nt          | d           ||           ||           ||	          fS )a  
        Extended Euclidean algorithm of ``f`` and ``g``.

        Returns ``(s, t, h)`` such that ``h = gcd(f, g)`` and ``s*f + t*g = h``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> f = x**4 - 2*x**3 - 6*x**2 + 12*x + 15
        >>> g = x**3 + x**2 - 4*x - 4

        >>> Poly(f).gcdex(Poly(g))
        (Poly(-1/5*x + 3/5, x, domain='QQ'),
         Poly(1/5*x**2 - 6/5*x + 2, x, domain='QQ'),
         Poly(x + 1, x, domain='QQ'))

        gcdex)r   r  r   r  re   r  r3   )
rZ   r[   r  r   r   r   r   r  tr  s
             r_   r  z
Poly.gcdex	  s    * !S!Q 	.CK 	.::<<qA15'"" 	4ggajjGAq!!'7333s1vvss1vvss1vv%%rb   c                 $   |                      |          \  }}}}|r/|j        r(|                                |                                }}t          | j        d          r|                    |          }nt          | d           ||          S )a  
        Invert ``f`` modulo ``g`` when possible.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**2 - 1, x).invert(Poly(2*x - 1, x))
        Poly(-4/3, x, domain='QQ')

        >>> Poly(x**2 - 1, x).invert(Poly(x - 1, x))
        Traceback (most recent call last):
        ...
        NotInvertible: zero divisor

        invert)r   r  r   r  re   r  r3   )rZ   r[   r  r   r   r   r   r]   s           r_   r  zPoly.invert
  s    & !S!Q 	.CK 	.::<<qA15(## 	5XXa[[FF'8444s6{{rb   c                     t          | j        d          r(| j                            t          |                    }nt	          | d          |                     |          S )ad  
        Compute ``f**(-1)`` mod ``x**n``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(1, x).revert(2)
        Poly(1, x, domain='ZZ')

        >>> Poly(1 + x, x).revert(1)
        Poly(1, x, domain='ZZ')

        >>> Poly(x**2 - 2, x).revert(2)
        Traceback (most recent call last):
        ...
        NotReversible: only units are reversible in a ring

        >>> Poly(1/x, x).revert(1)
        Traceback (most recent call last):
        ...
        PolynomialError: 1/x contains an element of the generators set

        revert)r  re   r  r*  r3   r   rz  s      r_   r  zPoly.revert+
  sS    6 15(## 	5U\\#a&&))FF'8444uuV}}rb   c                     |                      |          \  }}}}t          | j        d          r|                    |          }nt	          | d          t          t          ||                    S )ad  
        Computes the subresultant PRS of ``f`` and ``g``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**2 + 1, x).subresultants(Poly(x**2 - 1, x))
        [Poly(x**2 + 1, x, domain='ZZ'),
         Poly(x**2 - 1, x, domain='ZZ'),
         Poly(-2, x, domain='ZZ')]

        subresultants)r   r  re   r  r3   rq   r   rq  s          r_   r  zPoly.subresultantsM
  sj      xx{{3115/** 	<__Q''FF'?;;;CV$$%%%rb   c                 X   |                      |          \  }}}}t          | j        d          r3|r|                    ||          \  }}n&|                    |          }nt	          | d          |r* ||d          t          t          ||                    fS  ||d          S )a  
        Computes the resultant of ``f`` and ``g`` via PRS.

        If includePRS=True, it includes the subresultant PRS in the result.
        Because the PRS is used to calculate the resultant, this is more
        efficient than calling :func:`subresultants` separately.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> f = Poly(x**2 + 1, x)

        >>> f.resultant(Poly(x**2 - 1, x))
        4
        >>> f.resultant(Poly(x**2 - 1, x), includePRS=True)
        (4, [Poly(x**2 + 1, x, domain='ZZ'), Poly(x**2 - 1, x, domain='ZZ'),
             Poly(-2, x, domain='ZZ')])

        	resultant
includePRSr   r  )r   r  re   r  r3   rq   r   )	rZ   r[   r  r   r   r   r   r]   r  s	            r_   r  zPoly.resultantf
  s    . xx{{3115+&& 	8 (KKjKAA	Q';777 	>Cq)))4C+<+<==s6!$$$$rb   c                     t          | j        d          r| j                                        }nt          | d          |                     |d          S )z
        Computes the discriminant of ``f``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**2 + 2*x + 3, x).discriminant()
        -8

        discriminantr   r  )r  re   r  r3   r   r  s     r_   r  zPoly.discriminant
  sS     15.)) 	;U''))FF'>:::uuVAu&&&rb   c                 &    ddl m}  || |          S )a  Compute the *dispersion set* of two polynomials.

        For two polynomials `f(x)` and `g(x)` with `\deg f > 0`
        and `\deg g > 0` the dispersion set `\operatorname{J}(f, g)` is defined as:

        .. math::
            \operatorname{J}(f, g)
            & := \{a \in \mathbb{N}_0 | \gcd(f(x), g(x+a)) \neq 1\} \\
            &  = \{a \in \mathbb{N}_0 | \deg \gcd(f(x), g(x+a)) \geq 1\}

        For a single polynomial one defines `\operatorname{J}(f) := \operatorname{J}(f, f)`.

        Examples
        ========

        >>> from sympy import poly
        >>> from sympy.polys.dispersion import dispersion, dispersionset
        >>> from sympy.abc import x

        Dispersion set and dispersion of a simple polynomial:

        >>> fp = poly((x - 3)*(x + 3), x)
        >>> sorted(dispersionset(fp))
        [0, 6]
        >>> dispersion(fp)
        6

        Note that the definition of the dispersion is not symmetric:

        >>> fp = poly(x**4 - 3*x**2 + 1, x)
        >>> gp = fp.shift(-3)
        >>> sorted(dispersionset(fp, gp))
        [2, 3, 4]
        >>> dispersion(fp, gp)
        4
        >>> sorted(dispersionset(gp, fp))
        []
        >>> dispersion(gp, fp)
        -oo

        Computing the dispersion also works over field extensions:

        >>> from sympy import sqrt
        >>> fp = poly(x**2 + sqrt(5)*x - 1, x, domain='QQ<sqrt(5)>')
        >>> gp = poly(x**2 + (2 + sqrt(5))*x + sqrt(5), x, domain='QQ<sqrt(5)>')
        >>> sorted(dispersionset(fp, gp))
        [2]
        >>> sorted(dispersionset(gp, fp))
        [1, 4]

        We can even perform the computations for polynomials
        having symbolic coefficients:

        >>> from sympy.abc import a
        >>> fp = poly(4*x**4 + (4*a + 8)*x**3 + (a**2 + 6*a + 4)*x**2 + (a**2 + 2*a)*x, x)
        >>> sorted(dispersionset(fp))
        [0, 1]

        See Also
        ========

        dispersion

        References
        ==========

        1. [ManWright94]_
        2. [Koepf98]_
        3. [Abramov71]_
        4. [Man93]_
        r   )dispersionset)sympy.polys.dispersionr  )rZ   r[   r  s      r_   r  zPoly.dispersionset
  s)    P 	988888}Q"""rb   c                 &    ddl m}  || |          S )a  Compute the *dispersion* of polynomials.

        For two polynomials `f(x)` and `g(x)` with `\deg f > 0`
        and `\deg g > 0` the dispersion `\operatorname{dis}(f, g)` is defined as:

        .. math::
            \operatorname{dis}(f, g)
            & := \max\{ J(f,g) \cup \{0\} \} \\
            &  = \max\{ \{a \in \mathbb{N} | \gcd(f(x), g(x+a)) \neq 1\} \cup \{0\} \}

        and for a single polynomial `\operatorname{dis}(f) := \operatorname{dis}(f, f)`.

        Examples
        ========

        >>> from sympy import poly
        >>> from sympy.polys.dispersion import dispersion, dispersionset
        >>> from sympy.abc import x

        Dispersion set and dispersion of a simple polynomial:

        >>> fp = poly((x - 3)*(x + 3), x)
        >>> sorted(dispersionset(fp))
        [0, 6]
        >>> dispersion(fp)
        6

        Note that the definition of the dispersion is not symmetric:

        >>> fp = poly(x**4 - 3*x**2 + 1, x)
        >>> gp = fp.shift(-3)
        >>> sorted(dispersionset(fp, gp))
        [2, 3, 4]
        >>> dispersion(fp, gp)
        4
        >>> sorted(dispersionset(gp, fp))
        []
        >>> dispersion(gp, fp)
        -oo

        Computing the dispersion also works over field extensions:

        >>> from sympy import sqrt
        >>> fp = poly(x**2 + sqrt(5)*x - 1, x, domain='QQ<sqrt(5)>')
        >>> gp = poly(x**2 + (2 + sqrt(5))*x + sqrt(5), x, domain='QQ<sqrt(5)>')
        >>> sorted(dispersionset(fp, gp))
        [2]
        >>> sorted(dispersionset(gp, fp))
        [1, 4]

        We can even perform the computations for polynomials
        having symbolic coefficients:

        >>> from sympy.abc import a
        >>> fp = poly(4*x**4 + (4*a + 8)*x**3 + (a**2 + 6*a + 4)*x**2 + (a**2 + 2*a)*x, x)
        >>> sorted(dispersionset(fp))
        [0, 1]

        See Also
        ========

        dispersionset

        References
        ==========

        1. [ManWright94]_
        2. [Koepf98]_
        3. [Abramov71]_
        4. [Man93]_
        r   )
dispersion)r  r  )rZ   r[   r  s      r_   r  zPoly.dispersion
  s)    P 	655555z!Qrb   c                     |                      |          \  }}}}t          | j        d          r|                    |          \  }}}nt	          | d           ||           ||           ||          fS )a#  
        Returns the GCD of ``f`` and ``g`` and their cofactors.

        Returns polynomials ``(h, cff, cfg)`` such that ``h = gcd(f, g)``, and
        ``cff = quo(f, h)`` and ``cfg = quo(g, h)`` are, so called, cofactors
        of ``f`` and ``g``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**2 - 1, x).cofactors(Poly(x**2 - 3*x + 2, x))
        (Poly(x - 1, x, domain='ZZ'),
         Poly(x + 1, x, domain='ZZ'),
         Poly(x - 2, x, domain='ZZ'))

        	cofactors)r   r  re   r  r3   )	rZ   r[   r   r   r   r   r  cffcfgs	            r_   r  zPoly.cofactors6  s{    ( xx{{3115+&& 	8++a..KAsCC';777s1vvss3xxS))rb   c                     |                      |          \  }}}}t          | j        d          r|                    |          }nt	          | d           ||          S )a  
        Returns the polynomial GCD of ``f`` and ``g``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**2 - 1, x).gcd(Poly(x**2 - 3*x + 2, x))
        Poly(x - 1, x, domain='ZZ')

        gcd)r   r  re   r  r3   rq  s          r_   r  zPoly.gcdS  ^     xx{{3115%   	2UU1XXFF'5111s6{{rb   c                     |                      |          \  }}}}t          | j        d          r|                    |          }nt	          | d           ||          S )a  
        Returns polynomial LCM of ``f`` and ``g``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**2 - 1, x).lcm(Poly(x**2 - 3*x + 2, x))
        Poly(x**3 - 2*x**2 - x + 2, x, domain='ZZ')

        lcm)r   r  re   r  r3   rq  s          r_   r  zPoly.lcmj  r  rb   c                     | j         j                            |          }t          | j         d          r| j                             |          }nt          | d          |                     |          S )a  
        Reduce ``f`` modulo a constant ``p``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(2*x**3 + 3*x**2 + 5*x + 7, x).trunc(3)
        Poly(-x**3 - x + 1, x, domain='ZZ')

        trunc)re   r   r   r  r   r3   r   )rZ   pr]   s      r_   r   z
Poly.trunc  sb     EIa  15'"" 	4U[[^^FF'7333uuV}}rb   c                     | }|r%|j         j        j        r|                                }t	          |j         d          r|j                                         }nt          |d          |                    |          S )az  
        Divides all coefficients by ``LC(f)``.

        Examples
        ========

        >>> from sympy import Poly, ZZ
        >>> from sympy.abc import x

        >>> Poly(3*x**2 + 6*x + 9, x, domain=ZZ).monic()
        Poly(x**2 + 2*x + 3, x, domain='QQ')

        >>> Poly(3*x**2 + 4*x + 2, x, domain=ZZ).monic()
        Poly(x**2 + 4/3*x + 2/3, x, domain='QQ')

        monic)re   r   r  r   r  r  r3   r   r   r  rZ   r]   s       r_   r  z
Poly.monic  sq    "  	AEI% 	

A15'"" 	4U[[]]FF'7333uuV}}rb   c                     t          | j        d          r| j                                        }nt          | d          | j        j                            |          S )z
        Returns the GCD of polynomial coefficients.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(6*x**2 + 8*x + 12, x).content()
        2

        content)r  re   r  r3   r   r   r  s     r_   r  zPoly.content  r  rb   c                     t          | j        d          r| j                                        \  }}nt          | d          | j        j                            |          |                     |          fS )a  
        Returns the content and a primitive form of ``f``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(2*x**2 + 8*x + 12, x).primitive()
        (2, Poly(x**2 + 4*x + 6, x, domain='ZZ'))

        	primitive)r  re   r  r3   r   r   r   )rZ   contr]   s      r_   r  zPoly.primitive  sf     15+&& 	85??,,LD&&';777uy!!$''v66rb   c                     |                      |          \  }}}}t          | j        d          r|                    |          }nt	          | d           ||          S )a  
        Computes the functional composition of ``f`` and ``g``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**2 + x, x).compose(Poly(x - 1, x))
        Poly(x**2 - x, x, domain='ZZ')

        compose)r   r  re   r  r3   rq  s          r_   r  zPoly.compose  s^     xx{{3115)$$ 	6YYq\\FF'9555s6{{rb   c                     t          | j        d          r| j                                        }nt          | d          t	          t          | j        |                    S )a=  
        Computes a functional decomposition of ``f``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**4 + 2*x**3 - x - 1, x, domain='ZZ').decompose()
        [Poly(x**2 - x - 1, x, domain='ZZ'), Poly(x**2 + x, x, domain='ZZ')]

        	decompose)r  re   r  r3   rq   r   r   r  s     r_   r  zPoly.decompose  sU     15+&& 	8U__&&FF';777Cv&&'''rb   c                     t          | j        d          r| j                            |          }nt          | d          |                     |          S )a  
        Efficiently compute Taylor shift ``f(x + a)``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**2 - 2*x + 1, x).shift(2)
        Poly(x**2 + 2*x + 1, x, domain='ZZ')

        shift)r  re   r  r3   r   )rZ   r  r]   s      r_   r  z
Poly.shift  sK     15'"" 	4U[[^^FF'7333uuV}}rb   c                 R   |                     |          \  }}|                      |          \  }}|                     |          \  }}t          |j        d          r&|j                            |j        |j                  }nt	          |d          |                    |          S )a3  
        Efficiently evaluate the functional transformation ``q**n * f(p/q)``.


        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**2 - 2*x + 1, x).transform(Poly(x + 1, x), Poly(x - 1, x))
        Poly(4, x, domain='ZZ')

        	transform)r   r  re   r  r3   r   )rZ   r  r}  Pr  r   r]   s          r_   r  zPoly.transform   s     wwqzz1wwqzz1wwqzz115+&& 	8U__QUAE22FF';777uuV}}rb   c                    | }|r%|j         j        j        r|                                }t	          |j         d          r|j                                         }nt          |d          t          t          |j	        |                    S )a  
        Computes the Sturm sequence of ``f``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**3 - 2*x**2 + x - 3, x).sturm()
        [Poly(x**3 - 2*x**2 + x - 3, x, domain='QQ'),
         Poly(3*x**2 - 4*x + 1, x, domain='QQ'),
         Poly(2/9*x + 25/9, x, domain='QQ'),
         Poly(-2079/4, x, domain='QQ')]

        sturm)
re   r   r  r   r  r  r3   rq   r   r   r  s       r_   r  z
Poly.sturm:  s{    "  	AEI% 	

A15'"" 	4U[[]]FF'7333Cv&&'''rb   c                      t           j        d          r j                                        }nt           d           fd|D             S )aI  
        Computes greatest factorial factorization of ``f``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> f = x**5 + 2*x**4 - x**3 - 2*x**2

        >>> Poly(f).gff_list()
        [(Poly(x, x, domain='ZZ'), 1), (Poly(x + 2, x, domain='ZZ'), 4)]

        gff_listc                 D    g | ]\  }}                     |          |fS r   r   r   r[   r[  rZ   s      r_   r   z!Poly.gff_list.<locals>.<listcomp>l  s+    111$!Qq1111rb   )r  re   r  r3   r  s   ` r_   r  zPoly.gff_listW  sV      15*%% 	7U^^%%FF':6661111&1111rb   c                     t          | j        d          r| j                                        }nt          | d          |                     |          S )a  
        Computes the product, ``Norm(f)``, of the conjugates of
        a polynomial ``f`` defined over a number field ``K``.

        Examples
        ========

        >>> from sympy import Poly, sqrt
        >>> from sympy.abc import x

        >>> a, b = sqrt(2), sqrt(3)

        A polynomial over a quadratic extension.
        Two conjugates x - a and x + a.

        >>> f = Poly(x - a, x, extension=a)
        >>> f.norm()
        Poly(x**2 - 2, x, domain='QQ')

        A polynomial over a quartic extension.
        Four conjugates x - a, x - a, x + a and x + a.

        >>> f = Poly(x - a, x, extension=(a, b))
        >>> f.norm()
        Poly(x**4 - 4*x**2 + 4, x, domain='QQ')

        norm)r  re   r  r3   r   )rZ   r~  s     r_   r  z	Poly.normn  sH    8 15&!! 	3

AA'6222uuQxxrb   c                     t          | j        d          r| j                                        \  }}}nt          | d          ||                     |          |                     |          fS )af  
        Computes square-free norm of ``f``.

        Returns ``s``, ``f``, ``r``, such that ``g(x) = f(x-sa)`` and
        ``r(x) = Norm(g(x))`` is a square-free polynomial over ``K``,
        where ``a`` is the algebraic extension of the ground domain.

        Examples
        ========

        >>> from sympy import Poly, sqrt
        >>> from sympy.abc import x

        >>> s, f, r = Poly(x**2 + 1, x, extension=[sqrt(3)]).sqf_norm()

        >>> s
        1
        >>> f
        Poly(x**2 - 2*sqrt(3)*x + 4, x, domain='QQ<sqrt(3)>')
        >>> r
        Poly(x**4 - 4*x**2 + 16, x, domain='QQ')

        sqf_norm)r  re   r  r3   r   )rZ   r  r[   r~  s       r_   r  zPoly.sqf_norm  sb    0 15*%% 	7enn&&GAq!!':666!%%((AEE!HH$$rb   c                     t          | j        d          r| j                                        }nt          | d          |                     |          S )z
        Computes square-free part of ``f``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**3 - 3*x - 2, x).sqf_part()
        Poly(x**2 - x - 2, x, domain='ZZ')

        sqf_part)r  re   r  r3   r   r  s     r_   r  zPoly.sqf_part  r   rb   c                      t           j        d          r j                            |          \  }}nt           d           j        j                            |           fd|D             fS )a   
        Returns a list of square-free factors of ``f``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> f = 2*x**5 + 16*x**4 + 50*x**3 + 76*x**2 + 56*x + 16

        >>> Poly(f).sqf_list()
        (2, [(Poly(x + 1, x, domain='ZZ'), 2),
             (Poly(x + 2, x, domain='ZZ'), 3)])

        >>> Poly(f).sqf_list(all=True)
        (2, [(Poly(1, x, domain='ZZ'), 1),
             (Poly(x + 1, x, domain='ZZ'), 2),
             (Poly(x + 2, x, domain='ZZ'), 3)])

        sqf_listc                 D    g | ]\  }}                     |          |fS r   r  r  s      r_   r   z!Poly.sqf_list.<locals>.<listcomp>  +    *M*M*MTQAEE!HHa=*M*M*Mrb   )r  re   r!  r3   r   r   )rZ   allr   factorss   `   r_   r!  zPoly.sqf_list  st    , 15*%% 	7U^^C00NE77':666uy!!%((*M*M*M*MW*M*M*MMMrb   c                      t           j        d          r j                            |          }nt           d           fd|D             S )a  
        Returns a list of square-free factors of ``f``.

        Examples
        ========

        >>> from sympy import Poly, expand
        >>> from sympy.abc import x

        >>> f = expand(2*(x + 1)**3*x**4)
        >>> f
        2*x**7 + 6*x**6 + 6*x**5 + 2*x**4

        >>> Poly(f).sqf_list_include()
        [(Poly(2, x, domain='ZZ'), 1),
         (Poly(x + 1, x, domain='ZZ'), 3),
         (Poly(x, x, domain='ZZ'), 4)]

        >>> Poly(f).sqf_list_include(all=True)
        [(Poly(2, x, domain='ZZ'), 1),
         (Poly(1, x, domain='ZZ'), 2),
         (Poly(x + 1, x, domain='ZZ'), 3),
         (Poly(x, x, domain='ZZ'), 4)]

        sqf_list_includec                 D    g | ]\  }}                     |          |fS r   r  r  s      r_   r   z)Poly.sqf_list_include.<locals>.<listcomp>  +    222$!Qq1222rb   )r  re   r'  r3   )rZ   r$  r%  s   `  r_   r'  zPoly.sqf_list_include  s\    4 15,-- 	?e,,S11GG'+=>>>2222'2222rb   c                 &    t           j        d          r?	  j                                        \  }}n1# t          $ r t          j         dfgfcY S w xY wt           d           j        j                            |           fd|D             fS )a~  
        Returns a list of irreducible factors of ``f``.

        Examples
        ========

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

        >>> f = 2*x**5 + 2*x**4*y + 4*x**3 + 4*x**2*y + 2*x + 2*y

        >>> Poly(f).factor_list()
        (2, [(Poly(x + y, x, y, domain='ZZ'), 1),
             (Poly(x**2 + 1, x, y, domain='ZZ'), 2)])

        factor_listr{   c                 D    g | ]\  }}                     |          |fS r   r  r  s      r_   r   z$Poly.factor_list.<locals>.<listcomp>  r#  rb   )	r  re   r+  r4   r   r  r3   r   r   )rZ   r   r%  s   `  r_   r+  zPoly.factor_list  s    " 15-(( 	:'!"!2!2!4!4ww ' ' 'u1vh&&&' (=999uy!!%((*M*M*M*MW*M*M*MMMs   5 AAc                      t           j        d          r0	  j                                        }n%# t          $ r  dfgcY S w xY wt	           d           fd|D             S )a  
        Returns a list of irreducible factors of ``f``.

        Examples
        ========

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

        >>> f = 2*x**5 + 2*x**4*y + 4*x**3 + 4*x**2*y + 2*x + 2*y

        >>> Poly(f).factor_list_include()
        [(Poly(2*x + 2*y, x, y, domain='ZZ'), 1),
         (Poly(x**2 + 1, x, y, domain='ZZ'), 2)]

        factor_list_includer{   c                 D    g | ]\  }}                     |          |fS r   r  r  s      r_   r   z,Poly.factor_list_include.<locals>.<listcomp>7  r)  rb   )r  re   r.  r4   r3   )rZ   r%  s   ` r_   r.  zPoly.factor_list_include  s    " 15/00 	B %3355      Ax  (+@AAA2222'2222s   2 AAc                    |)t          j        |          }|dk    rt          d          |t          j        |          }|t          j        |          }t          | j        d          r!| j                            ||||||          }nt          | d          |rdd }|st          t          ||                    S d }	|\  }
}t          t          ||
                    t          t          |	|                    fS d }|st          t          ||                    S d	 }	|\  }
}t          t          ||
                    t          t          |	|                    fS )
a  
        Compute isolating intervals for roots of ``f``.

        For real roots the Vincent-Akritas-Strzebonski (VAS) continued fractions method is used.

        References
        ==========
        .. [#] Alkiviadis G. Akritas and Adam W. Strzebonski: A Comparative Study of Two Real Root
            Isolation Methods . Nonlinear Analysis: Modelling and Control, Vol. 10, No. 4, 297-304, 2005.
        .. [#] Alkiviadis G. Akritas, Adam W. Strzebonski and Panagiotis S. Vigklas: Improving the
            Performance of the Continued Fractions Method Using new Bounds of Positive Roots. Nonlinear
            Analysis: Modelling and Control, Vol. 13, No. 3, 265-279, 2008.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**2 - 3, x).intervals()
        [((-2, -1), 1), ((1, 2), 1)]
        >>> Poly(x**2 - 3, x).intervals(eps=1e-2)
        [((-26/15, -19/11), 1), ((19/11, 26/15), 1)]

        Nr   !'eps' must be a positive rational	intervalsr$  epsinfsupfastsqfc                 \    | \  }}t          j        |          t          j        |          fS r   r)   r   )intervalr  r  s      r_   _realzPoly.intervals.<locals>._reale  s&    1AA77rb   c                     | \  \  }}\  }}t          j        |          t          t          j        |          z  z   t          j        |          t          t          j        |          z  z   fS r   r)   r   r   )	rectangleuvr  r  s        r_   _complexz Poly.intervals.<locals>._complexl  sW    !*AAA2;q>>)99A2;q>>)99; ;rb   c                 f    | \  \  }}}t          j        |          t          j        |          f|fS r   r:  )r;  r  r  r[  s       r_   r<  zPoly.intervals.<locals>._realu  s/    $	AQQ8!<<rb   c                     | \  \  \  }}\  }}}t          j        |          t          t          j        |          z  z   t          j        |          t          t          j        |          z  z   f|fS r   r>  )r?  r@  rA  r  r  r[  s         r_   rB  z Poly.intervals.<locals>._complex|  sg    &/# !Q!Q!Q!BKNN*::Q!BKNN*::<=>@ @rb   )	r)   r   r  r  re   r2  r3   rq   r   )rZ   r$  r4  r5  r6  r7  r8  r]   r<  rB  	real_partcomplex_parts               r_   r2  zPoly.intervals9  s   4  	F*S//Cax F !DEEE 	"*S//C 	"*S//C15+&& 	8U__Scs3 % H HFF (;777 	R8 8 8  0Cv..///; ; ;
 '-#I|E9--..S<5P5P0Q0QQQ= = =  0Cv..///@ @ @
 '-#I|E9--..S<5P5P0Q0QQQrb   c                    |r| j         st          d          t          j        |          t          j        |          }}|)t          j        |          }|dk    rt	          d          |t          |          }n|d}t          | j        d          r#| j                            |||||          \  }}nt          | d          t          j
        |          t          j
        |          fS )a  
        Refine an isolating interval of a root to the given precision.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**2 - 3, x).refine_root(1, 2, eps=1e-2)
        (19/11, 26/15)

        z&only square-free polynomials supportedNr   r1  r{   refine_root)r4  stepsr7  )is_sqfr8   r)   r   r  r*  r  re   rH  r3   r   )	rZ   r  r  r4  rI  r7  	check_sqfr   Ts	            r_   rH  zPoly.refine_root  s      	LQX 	L!"JKKKz!}}bjmm1 	F*S//Cax F !DEEE 	JJEE 	E15-(( 	:5$$Qs%d$KKDAqq'=999{1~~r{1~~--rb   c                 n   d\  }}|yt          |          }|t          j        u rd}nY|                                \  }}|st	          j        |          }n+t          t          t          j        ||f                    d}}|yt          |          }|t          j        u rd}nY|                                \  }}|st	          j        |          }n+t          t          t          j        ||f                    d}}|rD|rBt          | j
        d          r| j
                            ||          }nvt          | d          |r||t          j        f}|r||t          j        f}t          | j
        d          r| j
                            ||          }nt          | d          t          |          S )a<  
        Return the number of roots of ``f`` in ``[inf, sup]`` interval.

        Examples
        ========

        >>> from sympy import Poly, I
        >>> from sympy.abc import x

        >>> Poly(x**4 - 4, x).count_roots(-3, 3)
        2
        >>> Poly(x**4 - 4, x).count_roots(0, 1 + 3*I)
        1

        TTNFcount_real_rootsr5  r6  count_complex_roots)r!   r   NegativeInfinityas_real_imagr)   r   rq   r   Infinityr  re   rO  r3   r   rQ  r   )rZ   r5  r6  inf_realsup_realreimcounts           r_   count_rootszPoly.count_roots  s     (( 	K#,,Ca(( K))++B K*S//CC$(RZ"b)B)B$C$CUC 	K#,,Caj  K))++B K*S//CC$(RZ"b)B)B$C$CUC 	F 	Fqu011 C..3C.@@+A/ABBB %C %BGn %C %BGnqu344 F11cs1CC+A/DEEEu~~rb   c                 P    t           j        j                            | ||          S )a  
        Get an indexed root of a polynomial.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> f = Poly(2*x**3 - 7*x**2 + 4*x + 4)

        >>> f.root(0)
        -1/2
        >>> f.root(1)
        2
        >>> f.root(2)
        2
        >>> f.root(3)
        Traceback (most recent call last):
        ...
        IndexError: root index out of [-3, 2] range, got 3

        >>> Poly(x**5 + x + 1).root(0)
        CRootOf(x**3 - x**2 + 1, 0)

        radicals)sympypolysrootoftoolsrootof)rZ   r  r]  s      r_   rootz	Poly.root  s$    6 {&--a-JJJrb   c                     t           j        j        j                            | |          }|r|S t          |d          S )aL  
        Return a list of real roots with multiplicities.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(2*x**3 - 7*x**2 + 4*x + 4).real_roots()
        [-1/2, 2, 2]
        >>> Poly(x**3 + x + 1).real_roots()
        [CRootOf(x**3 + x + 1, 0)]

        r\  Fmultiple)r^  r_  r`  CRootOf
real_rootsrF   )rZ   re  r]  realss       r_   rg  zPoly.real_roots  sD      '/::1x:PP 	0L////rb   c                     t           j        j        j                            | |          }|r|S t          |d          S )a  
        Return a list of real and complex roots with multiplicities.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(2*x**3 - 7*x**2 + 4*x + 4).all_roots()
        [-1/2, 2, 2]
        >>> Poly(x**3 + x + 1).all_roots()
        [CRootOf(x**3 + x + 1, 0),
         CRootOf(x**3 + x + 1, 1),
         CRootOf(x**3 + x + 1, 2)]

        r\  Frd  )r^  r_  r`  rf  	all_rootsrF   )rZ   re  r]  rootss       r_   rj  zPoly.all_roots  sD    $ '/99!h9OO 	0L////rb      2   c                   	 | j         rt          d| z            |                                 dk    rg S | j        j        t
          u rd |                                 D             }n| j        j        t          u rHd |                                 D             }t          | fd|                                 D             }nXfd|                                 D             }	 d |D             }n*# t          $ r t          d| j        j        z            w xY wt          j        j        }t          j        _        dd	lm	 	 t          j        |||d
|                                 dz            }t#          t%          t&          t)          |	fd                              }n# t*          $ r 	 t          j        |||d
|                                 dz            }t#          t%          t&          t)          |	fd                              }n## t*          $ r t+          dd|          w xY wY nw xY w|t          j        _        n# |t          j        _        w xY w|S )a  
        Compute numerical approximations of roots of ``f``.

        Parameters
        ==========

        n ... the number of digits to calculate
        maxsteps ... the maximum number of iterations to do

        If the accuracy `n` cannot be reached in `maxsteps`, it will raise an
        exception. You need to rerun with higher maxsteps.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**2 - 3).nroots(n=15)
        [-1.73205080756888, 1.73205080756888]
        >>> Poly(x**2 - 3).nroots(n=30)
        [-1.73205080756887729352744634151, 1.73205080756887729352744634151]

        z$Cannot compute numerical roots of %sr   c                 ,    g | ]}t          |          S r   r*  r   r   s     r_   r   zPoly.nroots.<locals>.<listcomp>Z  s    ===Uc%jj===rb   c                     g | ]	}|j         
S r   )r}  rq  s     r_   r   zPoly.nroots.<locals>.<listcomp>\  s    :::%eg:::rb   c                 4    g | ]}t          |z            S r   rp  )r   r   facs     r_   r   zPoly.nroots.<locals>.<listcomp>^  s#    AAAc%)nnAAArb   c                 `    g | ]*}|                                                                +S ))r,  )r   rS  )r   r   r,  s     r_   r   zPoly.nroots.<locals>.<listcomp>`  sC     1 1 1 kkAk&&3355 1 1 1rb   c                 *    g | ]}t          j        | S r   )mpmathmpcrq  s     r_   r   zPoly.nroots.<locals>.<listcomp>c  s    AAA&*e,AAArb   z!Numerical domain expected, got %s)signF
   )maxstepscleanuperror	extraprecc                 l    | j         rdnd| j        t          | j                    | j                   fS Nr{   r   imagrealrl  r~  ry  s    r_   <lambda>zPoly.nroots.<locals>.<lambda>u  s5    !&-?QQaQVVZVZ[\[aVbVb,c rb   keyrl  c                 l    | j         rdnd| j        t          | j                    | j                   fS r  r  r  s    r_   r  zPoly.nroots.<locals>.<lambda>|  s7    af1C!QVSQRQW[[Z^Z^_`_eZfZf0g rb   z$convergence to root failed; try n < z or maxsteps > )is_multivariater9   r  re   r   r*   r9  r)   r   r  r4   rw  mpdps$sympy.functions.elementary.complexesry  	polyrootsrq   r   r!   sortedrL   )
rZ   r,  r{  r|  r   denomsr  rk  rt  ry  s
    `      @@r_   nrootszPoly.nroots6  s   2  	<-6:< < < 88::? 	I
 59? 	#==allnn===FFUY"_ 	#::1<<>>:::F-CAAAA!,,..AAAFF1 1 1 1!"1 1 1F#AA&AAA # # #!"E	#" # # ## im	======	  $Vh#5AHHJJrMK K KE
 Wu"c"c"c"cdddf f g gEE 
	" 
	" 
	"	"((#5AHHJJrMK K KS5&g&g&g&ghhhj j k k  " " "#mAAxx!" " "" 
	"  FIMMCFIMsP   %C2 2'DA"F( 'I (
H<3A"HH< H66H<9I ;H<<I I$c                     | j         rt          d| z            i }|                                 d         D ],\  }}|j        r |                                \  }}||| |z  <   -|S )a  
        Compute roots of ``f`` by factorization in the ground domain.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**6 - 4*x**4 + 4*x**3 - x**2).ground_roots()
        {0: 2, 1: 2}

        z!Cannot compute ground roots of %sr{   )r  r9   r+  	is_linearr9  )rZ   rk  factorr[  r  r  s         r_   ground_rootszPoly.ground_roots  s      	9-3a79 9 9 + 	  	 IFA  ((**1qbdrb   c                 l   | j         rt          d          t          |          }|j        r|dk    rt	          |          }nt          d|z            | j        }t          d          }|                     | j	        
                    ||z  |z
  ||                    }|                    ||          S )af  
        Construct a polynomial with n-th powers of roots of ``f``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> f = Poly(x**4 - x**2 + 1)

        >>> f.nth_power_roots_poly(2)
        Poly(x**4 - 2*x**3 + 3*x**2 - 2*x + 1, x, domain='ZZ')
        >>> f.nth_power_roots_poly(3)
        Poly(x**4 + 2*x**2 + 1, x, domain='ZZ')
        >>> f.nth_power_roots_poly(4)
        Poly(x**4 + 2*x**3 + 3*x**2 + 2*x + 1, x, domain='ZZ')
        >>> f.nth_power_roots_poly(12)
        Poly(x**4 - 4*x**3 + 6*x**2 - 4*x + 1, x, domain='ZZ')

        zmust be a univariate polynomialr{   z&'n' must an integer and n >= 1, got %sr  )r  r9   r!   
is_Integerr*  r  r   r   r  r   rS   r   )rZ   r,  r  r  r  r~  s         r_   nth_power_roots_polyzPoly.nth_power_roots_poly  s    ,  	3-13 3 3 AJJ< 	KAF 	KAAAEIJJJE#JJKK--adQh1==>>yyArb   c                    | j         rt          d          | j                                        }| j                                                            |          }|dz  }t          d|z  di           \  }}}}t          |          }|dz  |dz  z   fd}	 |	|           |	|          }}
|
j	        |j	        z
  dz  |
j
        |j
        z
  dz  z   |k     S )a  
        Decide whether two roots of this polynomial are equal.

        Examples
        ========

        >>> from sympy import Poly, cyclotomic_poly, exp, I, pi
        >>> f = Poly(cyclotomic_poly(5))
        >>> r0 = exp(2*I*pi/5)
        >>> indices = [i for i, r in enumerate(f.all_roots()) if f.same_root(r, r0)]
        >>> print(indices)
        [3]

        Raises
        ======

        DomainError
            If the domain of the polynomial is not :ref:`ZZ`, :ref:`QQ`,
            :ref:`RR`, or :ref:`CC`.
        MultivariatePolynomialError
            If the polynomial is not univariate.
        PolynomialError
            If the polynomial is of degree < 2.

        zMust be a univariate polynomial	   r{      c                 @    t          t          |                     S )Nr  )r   r   )r  r+  s    r_   r  z Poly.same_root.<locals>.<lambda>  s    ~&?Q&G&G&GHH rb   )r  r9   re   mignotte_sep_bound_squaredr   	get_fieldr   r   r   r  r  )rZ   r  r  dom_delta_sqdelta_sqeps_sqr~  r   r,  evABr+  s               @r_   	same_rootzPoly.same_root  s    4  	3-13 3 3 u77998%%''00>> A1V8Q++
1aAJJ!VAHHHH r!uubbee1!#qv&::VCCrb   c                    |                      |          \  }}}}t          |d          r|                    ||          }nt          | d          |sf|j        r|                                }|\  }}	}
}|                    |          }|                    |	          }	||	z   ||
           ||          fS t          t          ||                    S )a  
        Cancel common factors in a rational function ``f/g``.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(2*x**2 - 2, x).cancel(Poly(x**2 - 2*x + 1, x))
        (1, Poly(2*x + 2, x, domain='ZZ'), Poly(x - 1, x, domain='ZZ'))

        >>> Poly(2*x**2 - 2, x).cancel(Poly(x**2 - 2*x + 1, x), include=True)
        (Poly(2*x + 2, x, domain='ZZ'), Poly(x - 1, x, domain='ZZ'))

        cancel)include)	r   r  r  r3   r  r  r   r@  r   )rZ   r[   r  r   r   r   r   r]   cpcqr  r}  s               r_   r  zPoly.cancel  s    " !S!Q1h 	5XXaX11FF'8444 	+! %llnn!LBAqb!!Bb!!Bb5##a&&##a&&((S&))***rb   c                     | j         j        S )a  
        Returns ``True`` if ``f`` is a zero polynomial.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(0, x).is_zero
        True
        >>> Poly(1, x).is_zero
        False

        )re   is_zeror   s    r_   r  zPoly.is_zero!  s    " u}rb   c                     | j         j        S )a  
        Returns ``True`` if ``f`` is a unit polynomial.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(0, x).is_one
        False
        >>> Poly(1, x).is_one
        True

        )re   is_oner   s    r_   r  zPoly.is_one4      " u|rb   c                     | j         j        S )a   
        Returns ``True`` if ``f`` is a square-free polynomial.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**2 - 2*x + 1, x).is_sqf
        False
        >>> Poly(x**2 - 1, x).is_sqf
        True

        )re   rJ  r   s    r_   rJ  zPoly.is_sqfG  r  rb   c                     | j         j        S )a   
        Returns ``True`` if the leading coefficient of ``f`` is one.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x + 2, x).is_monic
        True
        >>> Poly(2*x + 2, x).is_monic
        False

        )re   is_monicr   s    r_   r  zPoly.is_monicZ  s    " u~rb   c                     | j         j        S )a;  
        Returns ``True`` if GCD of the coefficients of ``f`` is one.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(2*x**2 + 6*x + 12, x).is_primitive
        False
        >>> Poly(x**2 + 3*x + 6, x).is_primitive
        True

        )re   is_primitiver   s    r_   r  zPoly.is_primitivem      " u!!rb   c                     | j         j        S )aJ  
        Returns ``True`` if ``f`` is an element of the ground domain.

        Examples
        ========

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

        >>> Poly(x, x).is_ground
        False
        >>> Poly(2, x).is_ground
        True
        >>> Poly(y, x).is_ground
        True

        )re   	is_groundr   s    r_   r  zPoly.is_ground  s    & urb   c                     | j         j        S )a,  
        Returns ``True`` if ``f`` is linear in all its variables.

        Examples
        ========

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

        >>> Poly(x + y + 2, x, y).is_linear
        True
        >>> Poly(x*y + 2, x, y).is_linear
        False

        )re   r  r   s    r_   r  zPoly.is_linear  s    " urb   c                     | j         j        S )a6  
        Returns ``True`` if ``f`` is quadratic in all its variables.

        Examples
        ========

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

        >>> Poly(x*y + 2, x, y).is_quadratic
        True
        >>> Poly(x*y**2 + 2, x, y).is_quadratic
        False

        )re   is_quadraticr   s    r_   r  zPoly.is_quadratic  r  rb   c                     | j         j        S )a%  
        Returns ``True`` if ``f`` is zero or has only one term.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(3*x**2, x).is_monomial
        True
        >>> Poly(3*x**2 + 1, x).is_monomial
        False

        )re   is_monomialr   s    r_   r  zPoly.is_monomial  s    " u  rb   c                     | j         j        S )aZ  
        Returns ``True`` if ``f`` is a homogeneous polynomial.

        A homogeneous polynomial is a polynomial whose all monomials with
        non-zero coefficients have the same total degree. If you want not
        only to check if a polynomial is homogeneous but also compute its
        homogeneous order, then use :func:`Poly.homogeneous_order`.

        Examples
        ========

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

        >>> Poly(x**2 + x*y, x, y).is_homogeneous
        True
        >>> Poly(x**3 + x*y, x, y).is_homogeneous
        False

        )re   is_homogeneousr   s    r_   r  zPoly.is_homogeneous  s    , u##rb   c                     | j         j        S )aG  
        Returns ``True`` if ``f`` has no factors over its domain.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> Poly(x**2 + x + 1, x, modulus=2).is_irreducible
        True
        >>> Poly(x**2 + 1, x, modulus=2).is_irreducible
        False

        )re   is_irreducibler   s    r_   r  zPoly.is_irreducible  s    " u##rb   c                 2    t          | j                  dk    S )a  
        Returns ``True`` if ``f`` is a univariate polynomial.

        Examples
        ========

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

        >>> Poly(x**2 + x + 1, x).is_univariate
        True
        >>> Poly(x*y**2 + x*y + 1, x, y).is_univariate
        False
        >>> Poly(x*y**2 + x*y + 1, x).is_univariate
        True
        >>> Poly(x**2 + x + 1, x, y).is_univariate
        False

        r{   r}   rT   r   s    r_   r  zPoly.is_univariate      * 16{{arb   c                 2    t          | j                  dk    S )a  
        Returns ``True`` if ``f`` is a multivariate polynomial.

        Examples
        ========

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

        >>> Poly(x**2 + x + 1, x).is_multivariate
        False
        >>> Poly(x*y**2 + x*y + 1, x, y).is_multivariate
        True
        >>> Poly(x*y**2 + x*y + 1, x).is_multivariate
        False
        >>> Poly(x**2 + x + 1, x, y).is_multivariate
        True

        r{   r  r   s    r_   r  zPoly.is_multivariate  r  rb   c                     | j         j        S )a  
        Returns ``True`` if ``f`` is a cyclotomic polnomial.

        Examples
        ========

        >>> from sympy import Poly
        >>> from sympy.abc import x

        >>> f = x**16 + x**14 - x**10 + x**8 - x**6 + x**2 + 1

        >>> Poly(f).is_cyclotomic
        False

        >>> g = x**16 + x**14 - x**10 - x**8 - x**6 + x**2 + 1

        >>> Poly(g).is_cyclotomic
        True

        )re   is_cyclotomicr   s    r_   r  zPoly.is_cyclotomic'  s    , u""rb   c                 *    |                                  S r   )rl  r   s    r_   __abs__zPoly.__abs__?      uuwwrb   c                 *    |                                  S r   )rn  r   s    r_   __neg__zPoly.__neg__B  r  rb   c                 ,    |                      |          S r   r  rZ   r[   s     r_   __add__zPoly.__add__E      uuQxxrb   c                 ,    |                     |           S r   r  r  s     r_   __radd__zPoly.__radd__I  r  rb   c                 ,    |                      |          S r   rt  r  s     r_   __sub__zPoly.__sub__M  r  rb   c                 ,    |                     |           S r   r  r  s     r_   __rsub__zPoly.__rsub__Q  r  rb   c                 ,    |                      |          S r   r   r  s     r_   __mul__zPoly.__mul__U  r  rb   c                 ,    |                     |           S r   r   r  s     r_   __rmul__zPoly.__rmul__Y  r  rb   r,  c                 T    |j         r|dk    r|                     |          S t          S )Nr   )r  ry  rV   )rZ   r,  s     r_   __pow__zPoly.__pow__]  s,    < 	"AF 	"5588O!!rb   c                 ,    |                      |          S r   r  r  s     r_   
__divmod__zPoly.__divmod__d  r  rb   c                 ,    |                     |           S r   r  r  s     r_   __rdivmod__zPoly.__rdivmod__h  r  rb   c                 ,    |                      |          S r   r  r  s     r_   __mod__zPoly.__mod__l  r  rb   c                 ,    |                     |           S r   r  r  s     r_   __rmod__zPoly.__rmod__p  r  rb   c                 ,    |                      |          S r   r  r  s     r_   __floordiv__zPoly.__floordiv__t  r  rb   c                 ,    |                     |           S r   r  r  s     r_   __rfloordiv__zPoly.__rfloordiv__x  r  rb   r[   c                 T    |                                  |                                 z  S r   rX   r  s     r_   __truediv__zPoly.__truediv__|      yy{{199;;&&rb   c                 T    |                                 |                                  z  S r   r  r  s     r_   __rtruediv__zPoly.__rtruediv__  r  rb   otherc                 2   | |}}|j         sO	 |                    ||j        |                                          }n# t          t
          t          f$ r Y dS w xY w|j        |j        k    rdS |j        j        |j        j        k    rdS |j        |j        k    S Nr&  F)	rr   r   rT   r   r8   r4   r5   re   r   )r   r  rZ   r[   s       r_   __eq__zPoly.__eq__  s    U1y 	KK16!,,..KAA#[.A   uu 6QV 	559	! 	5u~s   /= AAc                     | |k     S r   r   r  s     r_   __ne__zPoly.__ne__  s    6zrb   c                     | j          S r   )r  r   s    r_   __bool__zPoly.__bool__  s    9}rb   c                 V    |s| |k    S |                      t          |                    S r   )
_strict_eqr!   rZ   r[   stricts      r_   eqzPoly.eq  s+     	,6M<<

+++rb   c                 2    |                      ||           S )Nr  )r  r  s      r_   nezPoly.ne  s    44&4))))rb   c                     t          || j                  o0| j        |j        k    o | j                            |j        d          S NTr   )rQ   r   rT   re   r  r  s     r_   r  zPoly._strict_eq  s<    !Q[))_af.>_1588AEZ^8C_C__rb   NNr   )FF)FTr   )r{   FNT)FNNNFFNNFFrN  rl  rm  T)rY   
__module____qualname____doc__	__slots__is_commutativerr   _op_priorityry   classmethodr   propertyr   rw   r   r   r   r   rS   ro   rp   rs   rt   rl   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rh   r   r  r   r  r  r  r   r"  r'  r)  r   r   r  r9  r;  r>  rA  rC  r  rH  rX   rN  rP  rR  rU  rZ  r^  r`  rd  rf  rh  rj  rl  rn  r  rt  r   rw  ry  r|  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r  r  r  r  r  r  r  r  _eval_derivativer   r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r  r  r  r  r  r  r  r  r  r  r!  r'  r+  r.  r2  rH  rZ  rb  rg  rj  r  r  r  r  r  r  r  rJ  r  r  r  r  r  r  r  r  r  r  r  r  r  ra   r  r  r  r  r  r  r   rV   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  __classcell__r   s   @r_   rR   rR   e   s       3 3j  INGL0 0 0>   [ E E XE ( ( X(' ' ' ( ( [(
 ( ( [(
 ( ( [(
 ( ( [(
 A A [A& A A [A*   [, ( ( [(
 A A [A" " " " " 5 5 X5<   X:   X  ! ! X!, O O XO N N XN O O XO  81 1 1f+ + + +:0 0 0
  ) ) )"J J J** * *% % %&!M !M !M !MF2 2 2D D D4"L "L "LH     D  *  *  *4 4 4 40    J J J J,) ) ) )(Q Q Q Q(C C C " " "(J J J #= #= #=J      2 2 2 2&) ) ) )$= $= $=L  4  *     *#$ #$ #$ #$J(% (% (%T     *  *  *  *  0  4  *  0  >  >  >  0  4  .% % %N  8  >% % % %N# # # #J# # # #J( ( ( (T@ @ @(5 5 5 56: : :&; ; ;* <  <  <D@ @ @2* * * *0* * **1 1 1 1&#9 #9 #9J *  *  *D
1 
1 
1 
14 4 4 4(5 5 5 5 . . . .*. . . ."* * *** * **%& %& %& %&N% % %N&8 &8 &8P$3 $3 $3L I' I' I' I'V  ,   >& & & &B   >     D& & &2#% #% #% #%J' ' '*I# I# I# I#VI  I  I  I V* * *:  .  .  .   :* * **7 7 7*  .( ( (*  *  4( ( ( (:2 2 2.! ! !F% % %>  *N N N N:3 3 3 3BN N N63 3 36JR JR JR JRX#. #. #. #.J= = = =~K K K K:0 0 0 0.0 0 0 02N N N N`  6& & &P1D 1D 1Df#+ #+ #+ #+J   X$   X$   X$   X$ " " X"$   X(   X$ " " X"$ ! ! X!$ $ $ X$. $ $ X$$     X ,     X , # # X#.       Y   Y   Y   Y   Y   Y Z^$$" " %$"   Y   Y   Y   Y   Y   Y Z^$$' ' %$' Z^$$' ' %$' Z((  )(" Z^$$  %$  , , , ,* * * *` ` ` ` ` ` `rb   rR   c                   t     e Zd ZdZd Z fdZed             Z ede	          d             Z
d Zd Z xZS )	PurePolyz)Class for representing pure polynomials. c                     | j         fS )z$Allow SymPy to hash Poly instances. )re   r   s    r_   r   zPurePoly._hashable_content  s    {rb   c                 D    t                                                      S r   r   r   s    r_   r   zPurePoly.__hash__  r   rb   c                     | j         S )aR  
        Free symbols of a polynomial.

        Examples
        ========

        >>> from sympy import PurePoly
        >>> from sympy.abc import x, y

        >>> PurePoly(x**2 + 1).free_symbols
        set()
        >>> PurePoly(x**2 + y).free_symbols
        set()
        >>> PurePoly(x**2 + y, x).free_symbols
        {y}

        )r   r   s    r_   r   zPurePoly.free_symbols  s    & **rb   r  c                 :   | |}}|j         sO	 |                    ||j        |                                          }n# t          t
          t          f$ r Y dS w xY wt          |j                  t          |j                  k    rdS |j        j	        |j        j	        k    rl	 |j        j	        
                    |j        j	        |j                  }n# t          $ r Y dS w xY w|                    |          }|                    |          }|j        |j        k    S r  )rr   r   rT   r   r8   r4   r5   r}   re   r   r   r6   r   )r   r  rZ   r[   r   s        r_   r  zPurePoly.__eq__  s   U1y 	KK16!,,..KAA#[.A   uu qv;;#af++% 	559	! 	"eiooaei88$   uu S!!AS!!Au~s!   /= AA"/C 
C C c                 n    t          || j                  o | j                            |j        d          S r  )rQ   r   re   r  r  s     r_   r  zPurePoly._strict_eq  s-    !Q[))JaehhquTh.J.JJrb   c                    t          |          }|j        ss	 | j        j        | j        | j        | j                            | j        j                            |                    fS # t          $ r t          d| d|          w xY wt          | j	                  t          |j	                  k    rt          d| d|          t          | j        t                    rt          |j        t                    st          d| d|          | j        | j	        }| j        j                            |j        j        |          }| j                            |          }|j                            |          }||d ffd	}||||fS )Nr   r   c                     |/|d |         ||dz   d          z   }|s|                     |           S  j        | g|R  S r   r   r   s       r_   r   zPurePoly._unify.<locals>.per  r   rb   )r!   rr   re   r   r   r   r5   r6   r}   rT   rQ   r0   r   r   r   )rZ   r[   rT   r   r   r   r   rv   s          @r_   r   zPurePoly._unify  s   AJJy 	LLuy!%		!%):N:Nq:Q:Q0R0RRR! L L L''QQQ(JKKKL qv;;#af++% 	H##AA$FGGG15#&& 	H:aeS+A+A 	H##AA$FGGGkveiooaei..EMM#EMM#4 	' 	' 	' 	' 	' 	' CA~s   AA( ( B)rY   r
  r  r  r   r   r  r   r   rV   r  r  r   r  r  s   @r_   r  r    s        33  " " " " " + + X+( Z((  )(.K K K             rb   r  c                 L    t          j        ||          }t          | |          S r   )ri   rj   _poly_from_expr)r   rT   rw   rx   s       r_   poly_from_exprr     s&     
d
+
+C4%%%rb   c                 <   | t          |           } }t          | t                    st          |||           | j        rE| j                            | |          }|j        |_        |j        |_        |j	        d|_	        ||fS |j
        r| 
                                } t          | |          \  }}|j        st          |||           t          t          t          |                                                     \  }}|j        }|t          ||          \  |_        }n"t          t!          |j        |                    }t%          t          t          ||                              }t&                              ||          }|j	        d|_	        ||fS )r   NTr   F)r!   rQ   r   r;   rr   r   rs   rT   r   r_  expandrB   rq   r   r   r'   r   r   rn   rR   ro   )r   rx   origrM  re   r   r   r   s           r_   r  r    s   wt}}$DdE""  dD111	 ~((s339[
9 	CISy	 {{}}tS))HC8 2 dD111#tCIIKK00122NFFZF 6-f#>>>
FFc&+V4455
tC''((
)
)C??3$$D
y 	9rb   c                 L    t          j        ||          }t          | |          S )(Construct polynomials from expressions. )ri   rj   _parallel_poly_from_expr)exprsrT   rw   rx   s       r_   parallel_poly_from_exprr(  7  s&     
d
+
+C#E3///rb   c           	      0   t          |           dk    r| \  }}t          |t                    rt          |t                    rz|j                            ||          }|j                            ||          }|                    |          \  }}|j        |_        |j        |_        |j        d|_        ||g|fS t          |           g } }g g }}d}t          |          D ]\  }}	t          |	          }	t          |	t                    rN|	j        r|                    |           n3|                    |           |j        r|	                                }	nd}|                     |	           |rt!          ||| d          |r"|D ]}| |                                         | |<    t%          | |          \  }
}|j        st!          ||| d          ddlm} |j        D ]!}t          ||          rt+          d          "g g }}g }g }|
D ]}t          t-          t          |                                                     \  }}|                    |           |                    |           |                    t          |                     |j        }|t3          ||          \  |_        }n"t          t5          |j        |                    }|D ])}|                    |d|                    ||d         }*g }t-          ||          D ]_\  }}t9          t          t-          ||                              }t                              ||          }|                    |           `|j        t=          |          |_        ||fS )	r%  r  NTFr   	Piecewisez&Piecewise generators do not make senser   )r}   rQ   rR   r   rs   r   rT   r   r_  rq   r   r!   r   rr   appendr"  r;   rX   rC   $sympy.functions.elementary.piecewiser+  r8   r   r   extendr'   r   r   rn   ro   bool)r'  rx   rZ   r[   origs_exprs_polysfailedr   r   repsr+  r[  coeffs_listlengthsr;  r9  re   r   r   r   r_  rM  s                          r_   r&  r&  >  s   
5zzQ 1a 	:a#6#6 	&&q#..A&&q#..A771::DAqvCHCJy ! 	q63;;;5EFFFU##  4t}}dE"" 		| )a    a   : );;==DFT : eUD999 * 	* 	*AQx''))E!HH(44ID#8 : eUD999>>>>>>X L La## 	L!"JKKK	L rKJJ $ $c4		#4#45666"""&!!!s6{{####ZF @"2;C"H"H"H
KK3v0+>>?? & &+bqb/***!!""oEj*55  4FF++,,--sC((T
y !LL	#:rb   c                 6    t          |           } || vr|| |<   | S )z7Add a new ``(key, value)`` pair to arguments ``dict``. )rn   )rw   r  rK  s      r_   _update_argsr8    s'    ::D
$ S	Krb   c                    t          | d          } t          |d          j        }| j        r| }|                                j        }n1| j        }|s(|rt	          |           \  }}nt	          | |          \  }}|r| rt
          j        nt
          j        S |sJ| j        r-||j        vr$t	          |                                           \  }}||j        vrt
          j        S nd| j        s]t          | j
                  dk    rEt          t          d| dt          t          | j
                            d|d                    |                    |          }t!          |t"                    rt%          |          nt
          j        S )a  
    Return the degree of ``f`` in the given variable.

    The degree of 0 is negative infinity.

    Examples
    ========

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

    >>> degree(x**2 + y*x + 1, gen=x)
    2
    >>> degree(x**2 + y*x + 1, gen=y)
    1
    >>> degree(0, x)
    -oo

    See also
    ========

    sympy.polys.polytools.Poly.total_degree
    degree_list
    Tr   r{   zj
         A symbolic generator of interest is required for a multivariate
         expression like func = z, e.g. degree(func, gen = z)) instead of
         degree(func, gen = z).
        )r!   	is_Numberrr   rX   r   r   ZerorR  rT   r}   r   r  rH   nextr   r  rQ   r*  r   )rZ   r   
gen_is_Numr  isNumr   r]   s          r_   r  r    s   6 	$AT***4Jy 	.		% 	. .%a((11%a--1 32qvv 22 89 	/AF* 	/!!))++..DAqaf 	6M	Y 83q~..2 8

 qq$wq~..////	$6 7 7 8 8 	8
 XXc]]F(55M76???1;MMrb   c                     t          |           }|j        r|                                }|j        rd}n2| j        r	|p| j        }t          ||                                          }t          |          S )a  
    Return the total_degree of ``f`` in the given variables.

    Examples
    ========
    >>> from sympy import total_degree, Poly
    >>> from sympy.abc import x, y

    >>> total_degree(1)
    0
    >>> total_degree(x + x*y)
    2
    >>> total_degree(x + x*y, x)
    1

    If the expression is a Poly and no variables are given
    then the generators of the Poly will be used:

    >>> p = Poly(x + x*y, y)
    >>> total_degree(p)
    1

    To deal with the underlying expression of the Poly, convert
    it to an Expr:

    >>> total_degree(p.as_expr())
    2

    This is done automatically if any variables are given:

    >>> total_degree(p, x)
    1

    See also
    ========
    degree
    r   )r!   rr   rX   r:  rT   rR   r  r   )rZ   rT   r  rvs       r_   r  r    sr    P 	

Ay IIKK{ *9 	">16D!T]]''))2;;rb   c                 
   t          j        |dg           	 t          | g|R i |\  }}n## t          $ r}t	          dd|          d}~ww xY w|                                }t          t          t          |                    S )z
    Return a list of degrees of ``f`` in all variables.

    Examples
    ========

    >>> from sympy import degree_list
    >>> from sympy.abc import x, y

    >>> degree_list(x**2 + y*x + 1)
    (2, 1)

    r_  r  r{   N)	ri   allowed_flagsr   r;   r<   r  r@  r   r   )rZ   rT   rw   r   rx   r  degreess          r_   r  r    s     $	***71D111D1133 7 7 7q#6667 mmooGWg&&'''   , 
AAAc                     t          j        |dg           	 t          | g|R i |\  }}n## t          $ r}t	          dd|          d}~ww xY w|                    |j                  S )z
    Return the leading coefficient of ``f``.

    Examples
    ========

    >>> from sympy import LC
    >>> from sympy.abc import x, y

    >>> LC(4*x**2 + 2*x*y**2 + x*y + 3*y)
    4

    r_  r  r{   Nr1  )ri   rB  r   r;   r<   r  rg   rZ   rT   rw   r   rx   r  s         r_   r  r  1  s     $	***.1D111D1133 . . .a---. 44ci4   rD  c                     t          j        |dg           	 t          | g|R i |\  }}n## t          $ r}t	          dd|          d}~ww xY w|                    |j                  }|                                S )z
    Return the leading monomial of ``f``.

    Examples
    ========

    >>> from sympy import LM
    >>> from sympy.abc import x, y

    >>> LM(4*x**2 + 2*x*y**2 + x*y + 3*y)
    x**2

    r_  r  r{   Nr1  )ri   rB  r   r;   r<   r  rg   rX   )rZ   rT   rw   r   rx   r  r   s          r_   r  r  J  s     $	***.1D111D1133 . . .a---. DDsyD!!E==??rD  c                    t          j        |dg           	 t          | g|R i |\  }}n## t          $ r}t	          dd|          d}~ww xY w|                    |j                  \  }}||                                z  S )z
    Return the leading term of ``f``.

    Examples
    ========

    >>> from sympy import LT
    >>> from sympy.abc import x, y

    >>> LT(4*x**2 + 2*x*y**2 + x*y + 3*y)
    4*x**2

    r_  r  r{   Nr1  )ri   rB  r   r;   r<   r  rg   rX   )rZ   rT   rw   r   rx   r  r   r   s           r_   r  r  d  s     $	***.1D111D1133 . . .a---. 44ci4((LE5  rD  c                 >   t          j        |dg           	 t          | |fg|R i |\  \  }}}n## t          $ r}t	          dd|          d}~ww xY w|                    |          \  }}	|j        s(|                                |	                                fS ||	fS )z
    Compute polynomial pseudo-division of ``f`` and ``g``.

    Examples
    ========

    >>> from sympy import pdiv
    >>> from sympy.abc import x

    >>> pdiv(x**2 + 1, 2*x - 4)
    (2*x + 4, 20)

    r_  r|  r  N)ri   rB  r(  r;   r<   r|  r_  rX   
rZ   r[   rT   rw   r   r   rx   r  r}  r~  s
             r_   r|  r|  ~  s     $	***0-q!fDtDDDtDDA 0 0 03///0 66!99DAq9 yy{{AIIKK''!t   1 
AAAc                    t          j        |dg           	 t          | |fg|R i |\  \  }}}n## t          $ r}t	          dd|          d}~ww xY w|                    |          }|j        s|                                S |S )z
    Compute polynomial pseudo-remainder of ``f`` and ``g``.

    Examples
    ========

    >>> from sympy import prem
    >>> from sympy.abc import x

    >>> prem(x**2 + 1, 2*x - 4)
    20

    r_  r  r  N)ri   rB  r(  r;   r<   r  r_  rX   	rZ   r[   rT   rw   r   r   rx   r  r~  s	            r_   r  r    s     $	***0-q!fDtDDDtDDA 0 0 03///0 	
q		A9 yy{{rK  c                 L   t          j        |dg           	 t          | |fg|R i |\  \  }}}n## t          $ r}t	          dd|          d}~ww xY w	 |                    |          }n# t          $ r t          | |          w xY w|j        s|                                S |S )z
    Compute polynomial pseudo-quotient of ``f`` and ``g``.

    Examples
    ========

    >>> from sympy import pquo
    >>> from sympy.abc import x

    >>> pquo(x**2 + 1, 2*x - 4)
    2*x + 4
    >>> pquo(x**2 - 1, 2*x - 1)
    2*x + 1

    r_  r  r  N)	ri   rB  r(  r;   r<   r  r:   r_  rX   	rZ   r[   rT   rw   r   r   rx   r  r}  s	            r_   r  r    s    " $	***0-q!fDtDDDtDDA 0 0 03///0(FF1II ( ( (!!Q'''( 9 yy{{s    1 
AAAA+ +Bc                    t          j        |dg           	 t          | |fg|R i |\  \  }}}n## t          $ r}t	          dd|          d}~ww xY w|                    |          }|j        s|                                S |S )a_  
    Compute polynomial exact pseudo-quotient of ``f`` and ``g``.

    Examples
    ========

    >>> from sympy import pexquo
    >>> from sympy.abc import x

    >>> pexquo(x**2 - 1, 2*x - 2)
    2*x + 2

    >>> pexquo(x**2 + 1, 2*x - 4)
    Traceback (most recent call last):
    ...
    ExactQuotientFailed: 2*x - 4 does not divide x**2 + 1

    r_  r  r  N)ri   rB  r(  r;   r<   r  r_  rX   rO  s	            r_   r  r    s    ( $	***2-q!fDtDDDtDDA 2 2 2!S1112 	
A9 yy{{rK  c                 N   t          j        |ddg           	 t          | |fg|R i |\  \  }}}n## t          $ r}t	          dd|          d}~ww xY w|                    ||j                  \  }}	|j        s(|                                |	                                fS ||	fS )a  
    Compute polynomial division of ``f`` and ``g``.

    Examples
    ========

    >>> from sympy import div, ZZ, QQ
    >>> from sympy.abc import x

    >>> div(x**2 + 1, 2*x - 4, domain=ZZ)
    (0, x**2 + 1)
    >>> div(x**2 + 1, 2*x - 4, domain=QQ)
    (x/2 + 1, 5)

    r  r_  r  r  Nr  )	ri   rB  r(  r;   r<   r  r  r_  rX   rJ  s
             r_   r  r     s    " $ 1222/-q!fDtDDDtDDA / / /q#.../ 555""DAq9 yy{{AIIKK''!t   2 
AAAc                    t          j        |ddg           	 t          | |fg|R i |\  \  }}}n## t          $ r}t	          dd|          d}~ww xY w|                    ||j                  }|j        s|                                S |S )a  
    Compute polynomial remainder of ``f`` and ``g``.

    Examples
    ========

    >>> from sympy import rem, ZZ, QQ
    >>> from sympy.abc import x

    >>> rem(x**2 + 1, 2*x - 4, domain=ZZ)
    x**2 + 1
    >>> rem(x**2 + 1, 2*x - 4, domain=QQ)
    5

    r  r_  r  r  NrR  )	ri   rB  r(  r;   r<   r  r  r_  rX   rM  s	            r_   r  r         " $ 1222/-q!fDtDDDtDDA / / /q#.../ 	
achA9 yy{{rS  c                    t          j        |ddg           	 t          | |fg|R i |\  \  }}}n## t          $ r}t	          dd|          d}~ww xY w|                    ||j                  }|j        s|                                S |S )z
    Compute polynomial quotient of ``f`` and ``g``.

    Examples
    ========

    >>> from sympy import quo
    >>> from sympy.abc import x

    >>> quo(x**2 + 1, 2*x - 4)
    x/2 + 1
    >>> quo(x**2 - 1, x - 1)
    x + 1

    r  r_  r  r  NrR  )	ri   rB  r(  r;   r<   r  r  r_  rX   rO  s	            r_   r  r  @  rU  rS  c                    t          j        |ddg           	 t          | |fg|R i |\  \  }}}n## t          $ r}t	          dd|          d}~ww xY w|                    ||j                  }|j        s|                                S |S )aQ  
    Compute polynomial exact quotient of ``f`` and ``g``.

    Examples
    ========

    >>> from sympy import exquo
    >>> from sympy.abc import x

    >>> exquo(x**2 - 1, x - 1)
    x + 1

    >>> exquo(x**2 + 1, 2*x - 4)
    Traceback (most recent call last):
    ...
    ExactQuotientFailed: 2*x - 4 does not divide x**2 + 1

    r  r_  r  r  NrR  )	ri   rB  r(  r;   r<   r  r  r_  rX   rO  s	            r_   r  r  `  s    ( $ 12221-q!fDtDDDtDDA 1 1 1C0001 	
!!A9 yy{{rS  c                 0   t          j        |ddg           	 t          | |fg|R i |\  \  }}}n# t          $ r}t	          |j                  \  }\  }	}
	 |                    |	|
          \  }}|                    |          |                    |          fcY d}~S # t          $ r t          dd|          w xY wd}~ww xY w|                    ||j
                  \  }}|j        s(|                                |                                fS ||fS )aT  
    Half extended Euclidean algorithm of ``f`` and ``g``.

    Returns ``(s, h)`` such that ``h = gcd(f, g)`` and ``s*f = h (mod g)``.

    Examples
    ========

    >>> from sympy import half_gcdex
    >>> from sympy.abc import x

    >>> half_gcdex(x**4 - 2*x**3 - 6*x**2 + 12*x + 15, x**3 + x**2 - 4*x - 4)
    (3/5 - x/5, x + 1)

    r  r_  Nr  r  rR  )ri   rB  r(  r;   r'   r'  r  r   rk   r<   r  r_  rX   )rZ   r[   rT   rw   r   r   rx   r  r   r  r  r  r  s                r_   r  r    sN   " $ 1222
:-q!fDtDDDtDDA : : :)#)44A	:$$Q**DAq ??1%%vq'9'99999999 # 	: 	: 	:#L!S999	:: <<<))DAq9 yy{{AIIKK''!ts2   2 
CB>B0)B>CB;;B>>Cc                    t          j        |ddg           	 t          | |fg|R i |\  \  }}}n# t          $ r}t	          |j                  \  }\  }	}
	 |                    |	|
          \  }}}|                    |          |                    |          |                    |          fcY d}~S # t          $ r t          dd|          w xY wd}~ww xY w|                    ||j
                  \  }}}|j        s;|                                |                                |                                fS |||fS )aZ  
    Extended Euclidean algorithm of ``f`` and ``g``.

    Returns ``(s, t, h)`` such that ``h = gcd(f, g)`` and ``s*f + t*g = h``.

    Examples
    ========

    >>> from sympy import gcdex
    >>> from sympy.abc import x

    >>> gcdex(x**4 - 2*x**3 - 6*x**2 + 12*x + 15, x**3 + x**2 - 4*x - 4)
    (3/5 - x/5, x**2/5 - 6*x/5 + 2, x + 1)

    r  r_  Nr  r  rR  )ri   rB  r(  r;   r'   r'  r  r   rk   r<   r  r_  rX   )rZ   r[   rT   rw   r   r   rx   r  r   r  r  r  r  r  s                 r_   r  r    sp   " $ 1222
N-q!fDtDDDtDDA N N N)#)44A	Nll1a((GAq! ??1%%vq'9'96??1;M;MMMMMMMM # 	5 	5 	5#GQ444	5N ggachg''GAq!9 yy{{AIIKK44!Qws2   2 
CCB41=C.C4CCCc                    t          j        |ddg           	 t          | |fg|R i |\  \  }}}nz# t          $ rm}t	          |j                  \  }\  }	}
	 |                    |                    |	|
                    cY d}~S # t          $ r t          dd|          w xY wd}~ww xY w|                    ||j
                  }|j        s|                                S |S )a  
    Invert ``f`` modulo ``g`` when possible.

    Examples
    ========

    >>> from sympy import invert, S, mod_inverse
    >>> from sympy.abc import x

    >>> invert(x**2 - 1, 2*x - 1)
    -4/3

    >>> invert(x**2 - 1, x - 1)
    Traceback (most recent call last):
    ...
    NotInvertible: zero divisor

    For more efficient inversion of Rationals,
    use the :obj:`~.mod_inverse` function:

    >>> mod_inverse(3, 5)
    2
    >>> (S(2)/5).invert(S(7)/3)
    5/2

    See Also
    ========

    sympy.core.numbers.mod_inverse

    r  r_  Nr  r  rR  )ri   rB  r(  r;   r'   r'  r   r  rk   r<   r  r_  rX   )rZ   r[   rT   rw   r   r   rx   r  r   r  r  r  s               r_   r  r    s   B $ 12226-q!fDtDDDtDDA 6 6 6)#)44A	6??6==A#6#677777777" 	6 	6 	6#Ha555	66 	
""A9 yy{{s,   2 
B)B$(B?B)B!!B$$B)c                     t          j        |dg           	 t          | |fg|R i |\  \  }}}n## t          $ r}t	          dd|          d}~ww xY w|                    |          }|j        sd |D             S |S )z
    Compute subresultant PRS of ``f`` and ``g``.

    Examples
    ========

    >>> from sympy import subresultants
    >>> from sympy.abc import x

    >>> subresultants(x**2 + 1, x**2 - 1)
    [x**2 + 1, x**2 - 1, -2]

    r_  r  r  Nc                 6    g | ]}|                                 S r   r  r   r~  s     r_   r   z!subresultants.<locals>.<listcomp>       ,,,		,,,rb   )ri   rB  r(  r;   r<   r  r_  	rZ   r[   rT   rw   r   r   rx   r  r]   s	            r_   r  r    s     $	***9-q!fDtDDDtDDA 9 9 9C8889 __QF9 ,,V,,,,rK  Fr  c                   t          j        |dg           	 t          | |fg|R i |\  \  }}}n## t          $ r}t	          dd|          d}~ww xY w|r|                    ||          \  }	}
n|                    |          }	|j        s6|r |	                                d |
D             fS |	                                S |r|	|
fS |	S )z
    Compute resultant of ``f`` and ``g``.

    Examples
    ========

    >>> from sympy import resultant
    >>> from sympy.abc import x

    >>> resultant(x**2 + 1, x**2 - 1)
    4

    r_  r  r  Nr  c                 6    g | ]}|                                 S r   r  r]  s     r_   r   zresultant.<locals>.<listcomp>A  s     %=%=%=aaiikk%=%=%=rb   )ri   rB  r(  r;   r<   r  r_  rX   )rZ   r[   r  rT   rw   r   r   rx   r  r]   r  s              r_   r  r  $  s    $	***5-q!fDtDDDtDDA 5 5 5Q4445   KKjK99	Q9  	>>>##%=%=1%=%=%===~~ 	19rK  c                     t          j        |dg           	 t          | g|R i |\  }}n## t          $ r}t	          dd|          d}~ww xY w|                                }|j        s|                                S |S )z
    Compute discriminant of ``f``.

    Examples
    ========

    >>> from sympy import discriminant
    >>> from sympy.abc import x

    >>> discriminant(x**2 + 2*x + 3)
    -8

    r_  r  r{   N)ri   rB  r   r;   r<   r  r_  rX   rZ   rT   rw   r   rx   r  r]   s          r_   r  r  I  s     $	***81D111D1133 8 8 837778 ^^F9 ~~rD  c                 t   t          j        |dg           	 t          | |fg|R i |\  \  }}}n# t          $ r}t	          |j                  \  }\  }	}
	 |                    |	|
          \  }}}|                    |          |                    |          |                    |          fcY d}~S # t          $ r t          dd|          w xY wd}~ww xY w|                    |          \  }}}|j
        s;|                                |                                |                                fS |||fS )a  
    Compute GCD and cofactors of ``f`` and ``g``.

    Returns polynomials ``(h, cff, cfg)`` such that ``h = gcd(f, g)``, and
    ``cff = quo(f, h)`` and ``cfg = quo(g, h)`` are, so called, cofactors
    of ``f`` and ``g``.

    Examples
    ========

    >>> from sympy import cofactors
    >>> from sympy.abc import x

    >>> cofactors(x**2 - 1, x**2 - 3*x + 2)
    (x - 1, x + 1, x - 2)

    r_  Nr  r  )ri   rB  r(  r;   r'   r'  r  r   rk   r<   r_  rX   )rZ   r[   rT   rw   r   r   rx   r  r   r  r  r  r  r  s                 r_   r  r  g  sh   & $	***
R-q!fDtDDDtDDA R R R)#)44A	R **1a00KAsC ??1%%vs';';V__S=Q=QQQQQQQQ # 	9 	9 	9#KC888	9R ++a..KAsC9 yy{{CKKMM3;;==88#s{s2   1 
CCB30=C-C3CCCc                 p   t          |           } fd} ||           }||S t          j        dg           	 t          | gR i \  }}t	          |           dk    rt          d | D                       rx| d         fd| dd         D             }t          d |D                       rAd}|D ]*}	t          ||	                                d	                   }+t          |z            S nI# t          $ r<}
 ||
j
                  }||cY d}
~
S t          d
t	          |           |
          d}
~
ww xY w|s$|j        st          j        S t          d	|          S |d	         |dd         }}|D ] }|                    |          }|j        r n!|j        s|                                S |S )z
    Compute GCD of a list of polynomials.

    Examples
    ========

    >>> from sympy import gcd_list
    >>> from sympy.abc import x

    >>> gcd_list([x**3 - 1, x**2 - 1, x**2 - 3*x + 2])
    x - 1

    c                    s}s{t          |           \  }}|s|j        S |j        rY|d         |dd          }}|D ]/}|                    ||          }|                    |          r n0|                    |          S d S Nr   r{   )r'   r   rX  r  r  r   seqr   numbersr]   numberrw   rT   s        r_   try_non_polynomial_gcdz(gcd_list.<locals>.try_non_polynomial_gcd  s     	/D 	/.s33OFG /{"$ 	/")!*gabbk%  F#ZZ77F}}V,,  v...trb   Nr_  r{   c              3   2   K   | ]}|j         o|j        V  d S r   is_algebraicis_irrationalr   r  s     r_   	<genexpr>zgcd_list.<locals>.<genexpr>  -      VV3 0 FS5FVVVVVVrb   r  c                 >    g | ]}|z                                   S r   ratsimpr   r  r  s     r_   r   zgcd_list.<locals>.<listcomp>  '    ;;;#QsUOO%%;;;rb   c              3   $   K   | ]}|j         V  d S r   is_rationalr   frcs     r_   rr  zgcd_list.<locals>.<genexpr>  $      22s3?222222rb   r   gcd_listr   )r!   ri   rB  r(  r}   r$  r  as_numer_denomrl  r;   r'  r<   r_  r   r;  rR   r  r  rX   )ri  rT   rw   rl  r]   r_  rx   lstlcr}  r  rM  r  s    ``         @r_   r  r    sE    #,,C     & $#C((F $	***?,S@4@@@4@@
s s88a< 	!CVVRUVVVVV 	!BA;;;;SbS;;;C22c22222 ! : :CR!3!3!5!5a!899BB1R4yy  ? ? ?''	22 	?MMMMMM#JC#>>>?  $y 	$6Ms####!HeABBiEF  D!!= 	E	 9 ~~s$   B6C7 7
D=D8D=D88D=c                 (   t          | d          r||f|z   }t          | g|R i |S |t          d          t          j        |dg           	 t          | |fg|R i |\  \  }}}t          t          | |f          \  }}|j        r]|j	        rV|j        rO|j	        rH||z  
                                }	|	j        r*t          ||	                                d         z            S nz# t          $ rm}
t          |
j                  \  }\  }}	 |                    |                    ||                    cY d}
~
S # t&          $ r t)          dd|
          w xY wd}
~
ww xY w|                    |          }|j        s|                                S |S )z
    Compute GCD of ``f`` and ``g``.

    Examples
    ========

    >>> from sympy import gcd
    >>> from sympy.abc import x

    >>> gcd(x**2 - 1, x**2 - 3*x + 2)
    x - 1

    __iter__Nz2gcd() takes 2 arguments or a sequence of argumentsr_  r   r  r  )r  r  r  ri   rB  r(  r   r!   ro  rp  rv  r{  rl  r  r;   r'   r'  r   r  rk   r<   r_  rX   rZ   r[   rT   rw   r   r   rx   r  r  r}  r  r   r]   s                r_   r  r    s    q* N 	4$;D)D)))D)))	
 NLMMM$	***3-q!fDtDDDtDDA 7QF##1> 	6ao 	6!. 	6Q_ 	6Q3--//C 61S//11!44555 3 3 3)#)44A	3??6::a#3#344444444" 	3 	3 	3#E1c222	33 UU1XXF9 ~~s1   BC( (
E2E(D;5E;EEEc                 j   t          |           } dt          t                   ffd} ||           }||S t          j        dg           	 t          | gR i \  }}t          |           dk    rt          d | D                       rk| d         fd| dd         D             }t          d	 |D                       r4d}|D ]*}	t          ||		                                d                   }+|z  S nI# t          $ r<}
 ||
j                  }||cY d}
~
S t          d
t          |           |
          d}
~
ww xY w|s$|j        st          j        S t!          d|          S |d         |dd         }}|D ]}|                    |          }|j        s|                                S |S )z
    Compute LCM of a list of polynomials.

    Examples
    ========

    >>> from sympy import lcm_list
    >>> from sympy.abc import x

    >>> lcm_list([x**3 - 1, x**2 - 1, x**2 - 3*x + 2])
    x**5 - x**4 - 2*x**3 - x**2 + x + 2

    returnc                     syswt          |           \  }}|s|                    |j                  S |j        rB|d         |dd          }}|D ]}|                    ||          }|                    |          S d S rg  )r'   r   r   rX  r  rh  s        r_   try_non_polynomial_lcmz(lcm_list.<locals>.try_non_polynomial_lcm*  s     	/D 	/.s33OFG /vz222$ /")!*gabbk% 8 8F#ZZ77FFv...trb   Nr_  r{   c              3   2   K   | ]}|j         o|j        V  d S r   rn  rq  s     r_   rr  zlcm_list.<locals>.<genexpr>E  rs  rb   r  c                 >    g | ]}|z                                   S r   ru  rw  s     r_   r   zlcm_list.<locals>.<listcomp>G  rx  rb   c              3   $   K   | ]}|j         V  d S r   rz  r|  s     r_   rr  zlcm_list.<locals>.<genexpr>H  r~  rb   lcm_listr   r   )r!   r   r	   ri   rB  r(  r}   r$  r  r  r;   r'  r<   r_  r   r  rR   rX   )ri  rT   rw   r  r]   r_  rx   r  r  r}  r  rM  r  s    ``         @r_   r  r    s:    #,,Cx~         $#C((F $	***?,S@4@@@4@@
s s88a< 	CVVRUVVVVV 	BA;;;;SbS;;;C22c22222  : :CR!3!3!5!5a!899BBt ? ? ?''	22 	?MMMMMM#JC#>>>?  $y 	$5Ls####!HeABBiEF " "D!!9 ~~s%   B)C= =
ED>E D>>Ec                    t          | d          r||f|z   }t          | g|R i |S |t          d          t          j        |dg           	 t          | |fg|R i |\  \  }}}t          t          | |f          \  }}|j        rP|j	        rI|j        rB|j	        r;||z  
                                }	|	j        r||	                                d         z  S nz# t          $ rm}
t          |
j                  \  }\  }}	 |                    |                    ||                    cY d}
~
S # t$          $ r t'          dd|
          w xY wd}
~
ww xY w|                    |          }|j        s|                                S |S )z
    Compute LCM of ``f`` and ``g``.

    Examples
    ========

    >>> from sympy import lcm
    >>> from sympy.abc import x

    >>> lcm(x**2 - 1, x**2 - 3*x + 2)
    x**3 - 2*x**2 - x + 2

    r  Nz2lcm() takes 2 arguments or a sequence of argumentsr_  r{   r  r  )r  r  r  ri   rB  r(  r   r!   ro  rp  rv  r{  r  r;   r'   r'  r   r  rk   r<   r_  rX   r  s                r_   r  r  g  s    q* N 	4$;D)D)))D)))	
 NLMMM$	***3-q!fDtDDDtDDA 7QF##1> 	1ao 	1!. 	1Q_ 	1Q3--//C 1++--a000 3 3 3)#)44A	3??6::a#3#344444444" 	3 	3 	3#E1c222	33 UU1XXF9 ~~s1   BC 
E%E (D.(E.E

EEc                    t          |           }t          | t                    r"t          fd| j        | j        fD              S t          | t
                    rt          d|           t          | t                    r| j        r|S 	                    dd          rF | j
        fd| j        D              }                    d           dd<   t          |gR i S                     dd          }t          j        d	g           	 t!          | gR i \  }}n# t"          $ r}|j        cY d
}~S d
}~ww xY w|                                \  }	} |j        j        rN|j        j        r|                     d          \  }
} |                                 \  }} |j        j        r||
z  }nt0          j        }t5          d t7          | j        |	          D              }|dk    rt0          j        }|dk    r|S |r%t;          |||                                 z            S t;          ||                                 d                                          \  }} t;          ||| z  d          S )az  
    Remove GCD of terms from ``f``.

    If the ``deep`` flag is True, then the arguments of ``f`` will have
    terms_gcd applied to them.

    If a fraction is factored out of ``f`` and ``f`` is an Add, then
    an unevaluated Mul will be returned so that automatic simplification
    does not redistribute it. The hint ``clear``, when set to False, can be
    used to prevent such factoring when all coefficients are not fractions.

    Examples
    ========

    >>> from sympy import terms_gcd, cos
    >>> from sympy.abc import x, y
    >>> terms_gcd(x**6*y**2 + x**3*y, x, y)
    x**3*y*(x**3*y + 1)

    The default action of polys routines is to expand the expression
    given to them. terms_gcd follows this behavior:

    >>> terms_gcd((3+3*x)*(x+x*y))
    3*x*(x*y + x + y + 1)

    If this is not desired then the hint ``expand`` can be set to False.
    In this case the expression will be treated as though it were comprised
    of one or more terms:

    >>> terms_gcd((3+3*x)*(x+x*y), expand=False)
    (3*x + 3)*(x*y + x)

    In order to traverse factors of a Mul or the arguments of other
    functions, the ``deep`` hint can be used:

    >>> terms_gcd((3 + 3*x)*(x + x*y), expand=False, deep=True)
    3*x*(x + 1)*(y + 1)
    >>> terms_gcd(cos(x + x*y), deep=True)
    cos(x*(y + 1))

    Rationals are factored out by default:

    >>> terms_gcd(x + y/2)
    (2*x + y)/2

    Only the y-term had a coefficient that was a fraction; if one
    does not want to factor out the 1/2 in cases like this, the
    flag ``clear`` can be set to False:

    >>> terms_gcd(x + y/2, clear=False)
    x + y/2
    >>> terms_gcd(x*y/2 + y**2, clear=False)
    y*(x/2 + y)

    The ``clear`` flag is ignored if all coefficients are fractions:

    >>> terms_gcd(x/3 + y/2, clear=False)
    (2*x + 3*y)/6

    See Also
    ========
    sympy.core.exprtools.gcd_terms, sympy.core.exprtools.factor_terms

    c              3   8   K   | ]}t          |gR i V  d S r   r^  )r   r  rw   rT   s     r_   rr  zterms_gcd.<locals>.<genexpr>  s;      NN!)A555555NNNNNNrb   z3Inequalities cannot be used with terms_gcd. Found: deepFc                 0    g | ]}t          |gR i S r   r  )r   r  rw   rT   s     r_   r   zterms_gcd.<locals>.<listcomp>  s1    CCCqy2T222T22CCCrb   r"  clearTr_  Nr  c                     g | ]
\  }}||z  S r   r   )r   r  r   s      r_   r   zterms_gcd.<locals>.<listcomp>  s     111$!QA111rb   r{   )r  ) r!   rQ   r   lhsrhsr   r  r	   is_Atomr  r^   rw   popr^  ri   rB  r   r;   r   r   r  r  r  r  r   r  r   r   rT   r   rX   as_coeff_Mul)rZ   rT   rw   r#  r   r  r   rx   r  r   denomr   terms    ``          r_   r^  r^    s   F 1::D!X WNNNNNqu~NNNOO	Az	"	" WiRSRSUVVVa !) xx -afCCCCCAFCCCDX,t,,,t,,,HHWd##E$	***1D111D1133   x ;;==DAq
z 	: 	4~~d~33HE1;;==q: 	UNE11#afa..1112Dz 19 	K 45$qyy{{"2333 5!))++U;;;HHJJHE1ud1fE2222s   D+ +
E5E;EEc                    t          j        |ddg           	 t          | g|R i |\  }}n## t          $ r}t	          dd|          d}~ww xY w|                    t          |                    }|j        s|                                S |S )z
    Reduce ``f`` modulo a constant ``p``.

    Examples
    ========

    >>> from sympy import trunc
    >>> from sympy.abc import x

    >>> trunc(2*x**3 + 3*x**2 + 5*x + 7, 3)
    -x**3 - x + 1

    r  r_  r   r{   N)	ri   rB  r   r;   r<   r   r!   r_  rX   )rZ   r  rT   rw   r   rx   r  r]   s           r_   r   r     s     $ 122211D111D1133 1 1 1C0001 WWWQZZ  F9 ~~   - 
AAAc                    t          j        |ddg           	 t          | g|R i |\  }}n## t          $ r}t	          dd|          d}~ww xY w|                    |j                  }|j        s|                                S |S )z
    Divide all coefficients of ``f`` by ``LC(f)``.

    Examples
    ========

    >>> from sympy import monic
    >>> from sympy.abc import x

    >>> monic(3*x**2 + 4*x + 2)
    x**2 + 4*x/3 + 2/3

    r  r_  r  r{   NrR  )	ri   rB  r   r;   r<   r  r  r_  rX   rc  s          r_   r  r  .  s     $ 122211D111D1133 1 1 1C0001 WW#(W##F9 ~~r  c                     t          j        |dg           	 t          | g|R i |\  }}n## t          $ r}t	          dd|          d}~ww xY w|                                S )z
    Compute GCD of coefficients of ``f``.

    Examples
    ========

    >>> from sympy import content
    >>> from sympy.abc import x

    >>> content(6*x**2 + 8*x + 12)
    2

    r_  r  r{   N)ri   rB  r   r;   r<   r  rF  s         r_   r  r  L  s     $	***31D111D1133 3 3 3	1c2223 99;;rD  c                    t          j        |dg           	 t          | g|R i |\  }}n## t          $ r}t	          dd|          d}~ww xY w|                                \  }}|j        s||                                fS ||fS )a  
    Compute content and the primitive form of ``f``.

    Examples
    ========

    >>> from sympy.polys.polytools import primitive
    >>> from sympy.abc import x

    >>> primitive(6*x**2 + 8*x + 12)
    (2, 3*x**2 + 4*x + 6)

    >>> eq = (2 + 2*x)*x + 2

    Expansion is performed by default:

    >>> primitive(eq)
    (2, x**2 + x + 1)

    Set ``expand`` to False to shut this off. Note that the
    extraction will not be recursive; use the as_content_primitive method
    for recursive, non-destructive Rational extraction.

    >>> primitive(eq, expand=False)
    (1, x*(2*x + 2) + 2)

    >>> eq.as_content_primitive()
    (2, x*(x + 1) + 1)

    r_  r  r{   N)ri   rB  r   r;   r<   r  r_  rX   )rZ   rT   rw   r   rx   r  r	  r]   s           r_   r  r  e  s    @ $	***51D111D1133 5 5 5Q4445 ;;==LD&9 V^^%%%%V|rD  c                    t          j        |dg           	 t          | |fg|R i |\  \  }}}n## t          $ r}t	          dd|          d}~ww xY w|                    |          }|j        s|                                S |S )z
    Compute functional composition ``f(g)``.

    Examples
    ========

    >>> from sympy import compose
    >>> from sympy.abc import x

    >>> compose(x**2 + x, x - 1)
    x**2 - x

    r_  r  r  N)ri   rB  r(  r;   r<   r  r_  rX   r_  s	            r_   r  r    s     $	***3-q!fDtDDDtDDA 3 3 3	1c2223 YYq\\F9 ~~rK  c                     t          j        |dg           	 t          | g|R i |\  }}n## t          $ r}t	          dd|          d}~ww xY w|                                }|j        sd |D             S |S )z
    Compute functional decomposition of ``f``.

    Examples
    ========

    >>> from sympy import decompose
    >>> from sympy.abc import x

    >>> decompose(x**4 + 2*x**3 - x - 1)
    [x**2 - x - 1, x**2 + x]

    r_  r  r{   Nc                 6    g | ]}|                                 S r   r  r]  s     r_   r   zdecompose.<locals>.<listcomp>  r^  rb   )ri   rB  r   r;   r<   r  r_  rc  s          r_   r  r    s     $	***51D111D1133 5 5 5Q4445 [[]]F9 ,,V,,,,rD  c                     t          j        |ddg           	 t          | g|R i |\  }}n## t          $ r}t	          dd|          d}~ww xY w|                    |j                  }|j        sd |D             S |S )z
    Compute Sturm sequence of ``f``.

    Examples
    ========

    >>> from sympy import sturm
    >>> from sympy.abc import x

    >>> sturm(x**3 - 2*x**2 + x - 3)
    [x**3 - 2*x**2 + x - 3, 3*x**2 - 4*x + 1, 2*x/9 + 25/9, -2079/4]

    r  r_  r  r{   NrR  c                 6    g | ]}|                                 S r   r  r]  s     r_   r   zsturm.<locals>.<listcomp>  r^  rb   )ri   rB  r   r;   r<   r  r  r_  rc  s          r_   r  r    s     $ 122211D111D1133 1 1 1C0001 WW#(W##F9 ,,V,,,,r  c                     t          j        |dg           	 t          | g|R i |\  }}n## t          $ r}t	          dd|          d}~ww xY w|                                }|j        sd |D             S |S )a&  
    Compute a list of greatest factorial factors of ``f``.

    Note that the input to ff() and rf() should be Poly instances to use the
    definitions here.

    Examples
    ========

    >>> from sympy import gff_list, ff, Poly
    >>> from sympy.abc import x

    >>> f = Poly(x**5 + 2*x**4 - x**3 - 2*x**2, x)

    >>> gff_list(f)
    [(Poly(x, x, domain='ZZ'), 1), (Poly(x + 2, x, domain='ZZ'), 4)]

    >>> (ff(Poly(x), 1)*ff(Poly(x + 2), 4)) == f
    True

    >>> f = Poly(x**12 + 6*x**11 - 11*x**10 - 56*x**9 + 220*x**8 + 208*x**7 -         1401*x**6 + 1090*x**5 + 2715*x**4 - 6720*x**3 - 1092*x**2 + 5040*x, x)

    >>> gff_list(f)
    [(Poly(x**3 + 7, x, domain='ZZ'), 2), (Poly(x**2 + 5*x, x, domain='ZZ'), 3)]

    >>> ff(Poly(x**3 + 7, x), 2)*ff(Poly(x**2 + 5*x, x), 3) == f
    True

    r_  r  r{   Nc                 @    g | ]\  }}|                                 |fS r   r  )r   r[   r[  s      r_   r   zgff_list.<locals>.<listcomp>  s)    555TQa 555rb   )ri   rB  r   r;   r<   r  r_  )rZ   rT   rw   r   rx   r  r%  s          r_   r  r    s    @ $	***41D111D1133 4 4 4
As3334 jjllG9 55W5555rD  c                      t          d          )z3Compute greatest factorial factorization of ``f``. zsymbolic falling factorialr  rZ   rT   rw   s      r_   gffr    s     :
;
;;rb   c                 l   t          j        |dg           	 t          | g|R i |\  }}n## t          $ r}t	          dd|          d}~ww xY w|                                \  }}}|j        s6t          |          |                                |                                fS t          |          ||fS )a  
    Compute square-free norm of ``f``.

    Returns ``s``, ``f``, ``r``, such that ``g(x) = f(x-sa)`` and
    ``r(x) = Norm(g(x))`` is a square-free polynomial over ``K``,
    where ``a`` is the algebraic extension of the ground domain.

    Examples
    ========

    >>> from sympy import sqf_norm, sqrt
    >>> from sympy.abc import x

    >>> sqf_norm(x**2 + 1, extension=[sqrt(3)])
    (1, x**2 - 2*sqrt(3)*x + 4, x**4 - 4*x**2 + 16)

    r_  r  r{   N)	ri   rB  r   r;   r<   r  r_  r   rX   )	rZ   rT   rw   r   rx   r  r  r[   r~  s	            r_   r  r  "  s    & $	***41D111D1133 4 4 4
As3334 jjllGAq!9  qzz199;;		33qzz1arD  c                     t          j        |dg           	 t          | g|R i |\  }}n## t          $ r}t	          dd|          d}~ww xY w|                                }|j        s|                                S |S )z
    Compute square-free part of ``f``.

    Examples
    ========

    >>> from sympy import sqf_part
    >>> from sympy.abc import x

    >>> sqf_part(x**3 - 3*x - 2)
    x**2 - x - 2

    r_  r  r{   N)ri   rB  r   r;   r<   r  r_  rX   rc  s          r_   r  r  D  s     $	***41D111D1133 4 4 4
As3334 ZZ\\F9 ~~rD  c                 >    |dk    rd }nd }t          | |          S )z&Sort a list of ``(expr, exp)`` pairs. r8  c                     | \  }}|j         j         }|t          |          t          |j                  t          |j                  |fS r   re   r}   rT   rm   r   r~   rM  expre   s       r_   r  z_sorted_factors.<locals>.keye  s=    ID#(,CS3ty>>3t{3C3CSIIrb   c                     | \  }}|j         j         }t          |          t          |j                  |t          |j                  |fS r   r  r  s       r_   r  z_sorted_factors.<locals>.keyj  s=    ID#(,CHHc$)nnc3t{3C3CSIIrb   r  )r  )r%  methodr  s      r_   _sorted_factorsr  b  sN     	J	J 	J 	J 	J
	J 	J 	J
 's####rb   c                 (    t          d | D              S )z*Multiply a list of ``(expr, exp)`` pairs. c                 B    g | ]\  }}|                                 |z  S r   r  r   rZ   r[  s      r_   r   z$_factors_product.<locals>.<listcomp>t  s(    444DAqa444rb   )r   )r%  s    r_   _factors_productr  r  s    44G44455rb   c                    t           j        g c}d t          j        |           D             }|D ]+}|j        s$t          |t                    rt          |          r||z  }4|j        rS|j	        t           j
        k    r>|j        \  }|j        rj        r||z  }n|j        r                    |f           n|t           j        c}	 t          ||          \  }}t          ||dz             }	 |	            \  }
}|
t           j        urPj        r	||
z  z  }n@|
j        r                    |
f           n!|                    |
t           j        f           t           j        u r                    |           Tj        r#                    fd|D                        ~g }|D ]P\  }}|                                j        r                    ||z  f           9|                    ||f           Q                    t)          |          f           # t*          $ r'}                    |j        f           Y d}~%d}~ww xY w|dk    rfdd D             D             |fS )z.Helper function for :func:`_symbolic_factor`. c                 Z    g | ](}t          |d           r|                                n|)S )_eval_factor)r  r  r   r   s     r_   r   z)_symbolic_factor_list.<locals>.<listcomp>{  sF     & & & !(> : :AANN & & &rb   _listc                 $    g | ]\  }}||z  fS r   r   )r   rZ   r[  r  s      r_   r   z)_symbolic_factor_list.<locals>.<listcomp>  s%    @@@tq!AcE
@@@rb   Nr8  c                 X    g | ]%t          t          fd D                       f&S )c              3   .   K   | ]\  }}|k    |V  d S r   r   )r   rZ   r   r[  s      r_   rr  z3_symbolic_factor_list.<locals>.<listcomp>.<genexpr>  s0       A Atq!!q& A A A A A A Arb   )r   r   )r   r[  r%  s    @r_   r   z)_symbolic_factor_list.<locals>.<listcomp>  sN     5 5 5 3 A A A Aw A A ABBAF 5 5 5rb   c                     h | ]\  }}|S r   r   )r   r   r   s      r_   	<setcomp>z(_symbolic_factor_list.<locals>.<setcomp>  s    33341aQ333rb   )r   r  r   	make_argsr:  rQ   r	   r   is_PowbaseExp1rw   r,  r  rW   r  is_positiver.  
is_integerrX   r  r;   r   )r   rx   r  r   rw   argr  rM  r   r^   _coeff_factorsr  rZ   r[  r  r  r%  s                   @@r_   _symbolic_factor_listr  w  s   UBNE7& &t$$& & &D ,? ,?= 	#ZT22 	#|C7H7H 	#SLEZ 		#CH. 		#ID#~ #- ~ c{+++ QUID#	?%dC00GD! 4'!122D#tvvFHQU" 5> 5VS[(EE' 5NNFC=1111OOVQUO444ae| ?x(((( ?@@@@x@@@AAAA$ - -DAqyy{{. -1S5z2222aV,,,, 0 7 7=>>>>7 " 	, 	, 	,NNCHc?++++++++	,8  55 5 5 53373335 5 5 '>s   H++
I5IIc                    t          | t                    rjt          | d          r|                                 S t	          t          | d                             \  }}t          |t          |                    S t          | d          r | j        fd| j	        D              S t          | d          r"| 
                    fd| D                       S | S )z%Helper function for :func:`_factor`. r  fraction)r  rw   c                 2    g | ]}t          |          S r   _symbolic_factorr   r  r  rx   s     r_   r   z$_symbolic_factor.<locals>.<listcomp>  s&    SSS#+Cf==SSSrb   r  c                 2    g | ]}t          |          S r   r  r  s     r_   r   z$_symbolic_factor.<locals>.<listcomp>  s&    RRRc/S&AARRRrb   )rQ   r	   r  r  r  rD   r   r  r^   rw   r   )r   rx   r  r   r%  s    ``  r_   r  r    s    $ 
4'' 	'$$&&&.xs:/W/W/WY\^deew5"27";";<<<	v		 tySSSSSSSSTT	z	"	" ~~RRRRRTRRRSSSrb   c                 F   t          j        |ddg           t          j        ||          }t          |           } t	          | t
          t          f          r8t	          | t                    r| d}}n$t          |                                           \  }}t          |||          \  }}t          |||          \  }	}
|
r|j
        st          d| z            |                    t          d                    }||
fD ];}t          |          D ])\  }\  }}|j        st!          ||          \  }}||f||<   *<t#          ||          }t#          |
|          }
|j        sd |D             }d |
D             }
||	z  }|j
        s||fS |||
fS t          d| z            )	z>Helper function for :func:`sqf_list` and :func:`factor_list`. fracr_  r{   za polynomial expected, got %sT)r"  c                 @    g | ]\  }}|                                 |fS r   r  r  s      r_   r   z(_generic_factor_list.<locals>.<listcomp>  )    222tq!199;;"222rb   c                 @    g | ]\  }}|                                 |fS r   r  r  s      r_   r   z(_generic_factor_list.<locals>.<listcomp>  r  rb   )ri   rB  rj   r!   rQ   r	   rR   rD   r  r  r  r8   clonern   r   rr   r  r  r_  )r   rT   rw   r  rx   numerr  r  fpr  fq_optr%  r   rZ   r[  r   r   s                     r_   _generic_factor_listr    s   $ 1222

d
+
+C4==D$t%% "FdD!! 	;5EE#D>>88::LE5&uc6::B&uc6::B 	Jch 	J!"AD"HIIIyyT***++Bx 	( 	(G&w// ( (	6Aqy (*1d33DAq"#QGAJ(
 R((R((y 	322r222B22r222B2x 	!"9"b= =DEEErb   c                     |                     dd          }t          j        |g            t          j        ||          }||d<   t	          t          |           ||          S )z4Helper function for :func:`sqf` and :func:`factor`. r  T)r  ri   rB  rj   r  r!   )r   rT   rw   r  r  rx   s         r_   _generic_factorr    s[    xx
D))H$###

d
+
+CC
OGDMM3777rb   c                 .   ddl m d	fd	}d	fd	}d }|                                 j        re ||           rZ|                                 } || |          }|r|d         |d         d|d         fS  || |          }|rdd|d         |d         fS dS )
a!  
    try to transform a polynomial to have rational coefficients

    try to find a transformation ``x = alpha*y``

    ``f(x) = lc*alpha**n * g(y)`` where ``g`` is a polynomial with
    rational coefficients, ``lc`` the leading coefficient.

    If this fails, try ``x = y + beta``
    ``f(x) = g(y)``

    Returns ``None`` if ``g`` not found;
    ``(lc, alpha, None, g)`` in case of rescaling
    ``(None, None, beta, g)`` in case of translation

    Notes
    =====

    Currently it transforms only polynomials without roots larger than 2.

    Examples
    ========

    >>> from sympy import sqrt, Poly, simplify
    >>> from sympy.polys.polytools import to_rational_coeffs
    >>> from sympy.abc import x
    >>> p = Poly(((x**2-1)*(x-2)).subs({x:x*(1 + sqrt(2))}), x, domain='EX')
    >>> lc, r, _, g = to_rational_coeffs(p)
    >>> lc, r
    (7 + 5*sqrt(2), 2 - 2*sqrt(2))
    >>> g
    Poly(x**3 + x**2 - 1/4*x - 1/4, x, domain='QQ')
    >>> r1 = simplify(1/r)
    >>> Poly(lc*r**3*(g.as_expr()).subs({x:x*r1}), x, domain='EX') == p
    True

    r   simplifyNc                 F   t          | j                  dk    r| j        d         j        sd| fS |                                 }|                                 }|p|                                }|                                dd         }fd|D             }t          |          dk    r|d         r |d         |d         z            }g }t          t          |                    D ]:} ||         ||dz   z  z            }|j        s n|	                    |           ; d|z            }	| j        d         }
|
|z  g}t          d|dz             D ])}|	                    ||dz
           |
||z
  z  z             *t          | } t          |           } ||	| fS dS )a$  
        try rescaling ``x -> alpha*x`` to convert f to a polynomial
        with rational coefficients.
        Returns ``alpha, f``; if the rescaling is successful,
        ``alpha`` is the rescaling factor, and ``f`` is the rescaled
        polynomial; else ``alpha`` is ``None``.
        r{   r   Nc                 &    g | ]} |          S r   r   )r   coeffxr  s     r_   r   z<to_rational_coeffs.<locals>._try_rescale.<locals>.<listcomp>+  s#    888v((6""888rb   r  )r}   rT   r  r  r  r  r9  r   r{  r,  r
   rR   )rZ   f1r,  r  r   
rescale1_xcoeffs1r   r  	rescale_xr  rA  r  s               r_   _try_rescalez(to_rational_coeffs.<locals>._try_rescale  s    16{{a 	q	': 	7NHHJJTTVV288::$8888888v;;? 	(vbz 	(!&*VBZ"788JG3v;;'' ( (!&)JQ,?"?@@) Ev&&&&$HQz\22	F1ITFq!a% 8 8AHHWQU^AAJ67777GGG9a''trb   c                    t          | j                  dk    r| j        d         j        sd| fS |                                 }|p|                                }|                                dd         } 	|d                   }|j        rI|j        sBt          |j	        d d          \  }} |j
        |  |z  }|                    |          }||fS dS )a+  
        try translating ``x -> x + alpha`` to convert f to a polynomial
        with rational coefficients.
        Returns ``alpha, f``; if the translating is successful,
        ``alpha`` is the translating factor, and ``f`` is the shifted
        polynomial; else ``alpha`` is ``None``.
        r{   r   Nc                     | j         du S r  rz  )zs    r_   r  z<to_rational_coeffs.<locals>._try_translate.<locals>.<lambda>O  s    !-4/ rb   Tbinary)r}   rT   r  r  r  r9  is_Addr{  rK   rw   r^   r  )
rZ   r  r,  r   r   ratnonratalphaf2r  s
            r_   _try_translatez*to_rational_coeffs.<locals>._try_translate?  s     16{{a 	q	': 	7NHHJJ288::$HVAY8 	AM 	qv//> > >KCQVV_$Q&E%B"9trb   c                 ,   |                                  }d}|D ]z}t          j        |          D ]c}t          |          j        }d |                                D             }|s7t          |          dk    rd}t          |          dk    r  dS d{|S )zS
        Return True if ``f`` is a sum with square roots but no other root
        Fc                 T    g | ]%\  }}|j         |j        |j        d k    |j        &S )r  )r   is_Rationalr}  )r   r  wxs      r_   r   zAto_rational_coeffs.<locals>._has_square_roots.<locals>.<listcomp>^  sU     B B BeaKB$&NB79tqyBRT B B Brb   r  T)r   r
   r  r   r%  r   minmax)r  r   has_sqr  r  rZ   r~  s          r_   _has_square_rootsz-to_rational_coeffs.<locals>._has_square_rootsU  s      
	! 
	!A]1%% 	! 	!AJJ&B Bqwwyy B B B q66Q; "!Fq66A: ! 555!	! rb   r{   r  r   )sympy.simplify.simplifyr  r   r   r  )rZ   r  r  r   r  r~  r  s         @r_   to_rational_coeffsr    s   L 100000           D     ,  & 	||~~ . 1 1! 4 4 .WWYYLB 	.Q41tQqT))q"%%A .T1Q41--4rb   c           
         ddl m} t          | |d          }|                                }t	          |          }|sdS |\  }}}}	t          |	                                          }
|r ||
d         |z  ||z  z            } |d|z            }g }|
dd         d         D ]F}|                     ||d                             |||z  i                    |d         f           GnX|
d         }g }|
dd         d         D ]=}|                    |d                             |||z
  i          |d         f           >||fS )a  
    helper function to factor polynomial using to_rational_coeffs

    Examples
    ========

    >>> from sympy.polys.polytools import _torational_factor_list
    >>> from sympy.abc import x
    >>> from sympy import sqrt, expand, Mul
    >>> p = expand(((x**2-1)*(x-2)).subs({x:x*(1 + sqrt(2))}))
    >>> factors = _torational_factor_list(p, x); factors
    (-2, [(-x*(1 + sqrt(2))/2 + 1, 1), (-x*(1 + sqrt(2)) - 1, 1), (-x*(1 + sqrt(2)) + 1, 1)])
    >>> expand(factors[0]*Mul(*[z[0] for z in factors[1]])) == p
    True
    >>> p = expand(((x**2-1)*(x-2)).subs({x:x + sqrt(2)}))
    >>> factors = _torational_factor_list(p, x); factors
    (1, [(x - 2 + sqrt(2), 1), (x - 1 + sqrt(2), 1), (x + 1 + sqrt(2), 1)])
    >>> expand(factors[0]*Mul(*[z[0] for z in factors[1]])) == p
    True

    r   r  EXr&  Nr{   )	r  r  rR   r  r  r+  rX   r,  r   )r  r  r  p1r,  resr  r~  r  r[   r%  r   r1r  r  s                  r_   _torational_factor_listr  t  s{   , 100000	a4	 	 	 B
		A
R
 
 C tKB1a!))++&&G	 
4HWQZ]1a4'((Xac]]Q 	= 	=AHHhhqtyy!QrT3344ad;<<<<	= AJQ 	4 	4AHHadiiAE
++QqT23333q6Mrb   c                 (    t          | ||d          S )z
    Compute a list of square-free factors of ``f``.

    Examples
    ========

    >>> from sympy import sqf_list
    >>> from sympy.abc import x

    >>> sqf_list(2*x**5 + 16*x**4 + 50*x**3 + 76*x**2 + 56*x + 16)
    (2, [(x + 1, 2), (x + 2, 3)])

    r8  r  r  r  s      r_   r!  r!    s      4e<<<<rb   c                 (    t          | ||d          S )z
    Compute square-free factorization of ``f``.

    Examples
    ========

    >>> from sympy import sqf
    >>> from sympy.abc import x

    >>> sqf(2*x**5 + 16*x**4 + 50*x**3 + 76*x**2 + 56*x + 16)
    2*(x + 1)**2*(x + 2)**3

    r8  r
  )r  r  s      r_   r8  r8    s     1dD7777rb   c                 (    t          | ||d          S )a  
    Compute a list of irreducible factors of ``f``.

    Examples
    ========

    >>> from sympy import factor_list
    >>> from sympy.abc import x, y

    >>> factor_list(2*x**5 + 2*x**4*y + 4*x**3 + 4*x**2*y + 2*x + 2*y)
    (2, [(x + y, 1), (x**2 + 1, 2)])

    r  r
  r  r  s      r_   r+  r+    s      4h????rb   )r  c                   t          |           } |r{fd}t          | |          } i }|                     t          t                    }|D ]+}t          |gR i }|j        s|j        r||k    r|||<   ,|                     |          S 	 t          | d          S # t          $ r/}	| j        st          |           cY d}	~	S t          |	          d}	~	ww xY w)a  
    Compute the factorization of expression, ``f``, into irreducibles. (To
    factor an integer into primes, use ``factorint``.)

    There two modes implemented: symbolic and formal. If ``f`` is not an
    instance of :class:`Poly` and generators are not specified, then the
    former mode is used. Otherwise, the formal mode is used.

    In symbolic mode, :func:`factor` will traverse the expression tree and
    factor its components without any prior expansion, unless an instance
    of :class:`~.Add` is encountered (in this case formal factorization is
    used). This way :func:`factor` can handle large or symbolic exponents.

    By default, the factorization is computed over the rationals. To factor
    over other domain, e.g. an algebraic or finite field, use appropriate
    options: ``extension``, ``modulus`` or ``domain``.

    Examples
    ========

    >>> from sympy import factor, sqrt, exp
    >>> from sympy.abc import x, y

    >>> factor(2*x**5 + 2*x**4*y + 4*x**3 + 4*x**2*y + 2*x + 2*y)
    2*(x + y)*(x**2 + 1)**2

    >>> factor(x**2 + 1)
    x**2 + 1
    >>> factor(x**2 + 1, modulus=2)
    (x + 1)**2
    >>> factor(x**2 + 1, gaussian=True)
    (x - I)*(x + I)

    >>> factor(x**2 - 2, extension=sqrt(2))
    (x - sqrt(2))*(x + sqrt(2))

    >>> factor((x**2 - 1)/(x**2 + 4*x + 4))
    (x - 1)*(x + 1)/(x + 2)**2
    >>> factor((x**2 + 4*x + 4)**10000000*(x**2 + 1))
    (x + 2)**20000000*(x**2 + 1)

    By default, factor deals with an expression as a whole:

    >>> eq = 2**(x**2 + 2*x + 1)
    >>> factor(eq)
    2**(x**2 + 2*x + 1)

    If the ``deep`` flag is True then subexpressions will
    be factored:

    >>> factor(eq, deep=True)
    2**((x + 1)**2)

    If the ``fraction`` flag is False then rational expressions
    will not be combined. By default it is True.

    >>> factor(5*x + 3*exp(2 - 7*x), deep=True)
    (5*x*exp(7*x) + 3*exp(2))*exp(-7*x)
    >>> factor(5*x + 3*exp(2 - 7*x), deep=True, fraction=False)
    5*x + 3*exp(2)*exp(-7*x)

    See Also
    ========
    sympy.ntheory.factor_.factorint

    c                 H    t          | gR i }|j        s|j        r|S | S )zS
            Factor, but avoid changing the expression when unable to.
            )r  is_Mulr  )r   rt  rw   rT   s     r_   _try_factorzfactor.<locals>._try_factor  s?     ------Cz SZ 
Krb   r  r
  N)r!   r$   atomsr   r
   r  r  r  xreplacer  r8   r  r   )
rZ   r  rT   rw   r  partialsmuladdr  rt  msgs
     ``      r_   r  r    s8   H 	

A $	 	 	 	 	 	 a%% c"" 	" 	"A*T***T**C
 "cj "cQh "!zz(###'q$X>>>> ' ' ' 	'Q<<!#&&&	's$   B# #
C-CCCCc           	         t          | d          s>	 t          |           } n# t          $ r g cY S w xY w|                     ||||||          S t	          | d          \  }}	t          |	j                  dk    rt          t          |          D ]\  }
}|j	        j	        ||
<   |/|	j
                            |          }|dk    rt          d          ||	j
                            |          }||	j
                            |          }t          ||	j
        |||||	          }g }|D ]U\  \  }}}|	j
                            |          |	j
                            |          }}|                    ||f|f           V|S )
a/  
    Compute isolating intervals for roots of ``f``.

    Examples
    ========

    >>> from sympy import intervals
    >>> from sympy.abc import x

    >>> intervals(x**2 - 3)
    [((-2, -1), 1), ((1, 2), 1)]
    >>> intervals(x**2 - 3, eps=1e-2)
    [((-26/15, -19/11), 1), ((19/11, 26/15), 1)]

    r  r3  r)   r&  r{   Nr   r1  )r4  r5  r6  r  r7  )r  rR   r7   r2  r(  r}   rT   r9   r   re   r   r   r  rE   r   r,  )r   r$  r4  r5  r6  r  r7  r8  r_  rx   r   rM  r2  r]   r  r  r  s                    r_   r2  r2  9  s   " 1j!! $	QAA 	 	 	III	 {{s#Dc{RRR,Qt<<<
ssx==1 	.-- '' 	$ 	$GAtx|E!HH 	F*$$S))Cax F !DEEE 	**$$S))C 	**$$S))C/sz#f4A A A	 ( 	- 	-OFQG:&&q))3:+>+>q+A+AqAMMAq67+,,,,s   " 11c                     	 t          |           }t          | t                     s|j        j        st	          d          n # t
          $ r t	          d| z            w xY w|                    ||||||          S )z
    Refine an isolating interval of a root to the given precision.

    Examples
    ========

    >>> from sympy import refine_root
    >>> from sympy.abc import x

    >>> refine_root(x**2 - 3, 1, 2, eps=1e-2)
    (19/11, 26/15)

    generator must be a Symbolz,Cannot refine a root of %s, not a polynomial)r4  rI  r7  rK  )rR   rQ   r   	is_Symbolr8   r7   rH  )rZ   r  r  r4  rI  r7  rK  r   s           r_   rH  rH  q  s    	@GG!T"" 	@15? 	@ "">??? @ @ @:Q>@ @ 	@@ ==A3e$)=TTTs   ?A Ac                     	 t          | d          }t          | t                     s|j        j        st	          d          n # t
          $ r t	          d| z            w xY w|                    ||          S )a  
    Return the number of roots of ``f`` in ``[inf, sup]`` interval.

    If one of ``inf`` or ``sup`` is complex, it will return the number of roots
    in the complex rectangle with corners at ``inf`` and ``sup``.

    Examples
    ========

    >>> from sympy import count_roots, I
    >>> from sympy.abc import x

    >>> count_roots(x**4 - 4, -3, 3)
    2
    >>> count_roots(x**4 - 4, 0, 1 + 3*I)
    1

    Fgreedyr  z*Cannot count roots of %s, not a polynomialrP  )rR   rQ   r   r  r8   r7   rZ  )rZ   r5  r6  r   s       r_   rZ  rZ    s    (P5!!!!T"" 	@15? 	@ "">??? P P PJQNOOOP ==Sc=***   AA A!Tc                     	 t          | d          }t          | t                     s|j        j        st	          d          n # t
          $ r t	          d| z            w xY w|                    |          S )z
    Return a list of real roots with multiplicities of ``f``.

    Examples
    ========

    >>> from sympy import real_roots
    >>> from sympy.abc import x

    >>> real_roots(2*x**3 - 7*x**2 + 4*x + 4)
    [-1/2, 2, 2]
    Fr  r  z1Cannot compute real roots of %s, not a polynomialrd  )rR   rQ   r   r  r8   r7   rg  )rZ   re  r   s      r_   rg  rg    s    	E5!!!!T"" 	@15? 	@ "">??? E E E?!CE E 	EE <<<***r  rl  rm  c                     	 t          | d          }t          | t                     s|j        j        st	          d          n # t
          $ r t	          d| z            w xY w|                    |||          S )aL  
    Compute numerical approximations of roots of ``f``.

    Examples
    ========

    >>> from sympy import nroots
    >>> from sympy.abc import x

    >>> nroots(x**2 - 3, n=15)
    [-1.73205080756888, 1.73205080756888]
    >>> nroots(x**2 - 3, n=30)
    [-1.73205080756887729352744634151, 1.73205080756887729352744634151]

    Fr  r  z6Cannot compute numerical roots of %s, not a polynomial)r,  r{  r|  )rR   rQ   r   r  r8   r7   r  )rZ   r,  r{  r|  r   s        r_   r  r    s    "	J5!!!!T"" 	@15? 	@ "">??? J J JDqHJ J 	JJ 88a(G8<<<r  c                 $   t          j        |g            	 t          | g|R i |\  }}t          | t                    s|j        j        st          d          n## t          $ r}t          dd|          d}~ww xY w|
                                S )z
    Compute roots of ``f`` by factorization in the ground domain.

    Examples
    ========

    >>> from sympy import ground_roots
    >>> from sympy.abc import x

    >>> ground_roots(x**6 - 4*x**4 + 4*x**3 - x**2)
    {0: 2, 1: 2}

    r  r  r{   N)ri   rB  r   rQ   rR   r   r  r8   r;   r<   r  rF  s         r_   r  r    s     $###81D111D113!T"" 	@15? 	@ "">??? 8 8 837778 >>   AA 
A;%A66A;c                 `   t          j        |g            	 t          | g|R i |\  }}t          | t                    s|j        j        st          d          n## t          $ r}t          dd|          d}~ww xY w|
                    |          }|j        s|                                S |S )a  
    Construct a polynomial with n-th powers of roots of ``f``.

    Examples
    ========

    >>> from sympy import nth_power_roots_poly, factor, roots
    >>> from sympy.abc import x

    >>> f = x**4 - x**2 + 1
    >>> g = factor(nth_power_roots_poly(f, 2))

    >>> g
    (x**2 - x + 1)**2

    >>> R_f = [ (r**2).expand() for r in roots(f) ]
    >>> R_g = roots(g).keys()

    >>> set(R_f) == set(R_g)
    True

    r  r  r{   N)ri   rB  r   rQ   rR   r   r  r8   r;   r<   r  r_  rX   )rZ   r,  rT   rw   r   rx   r  r]   s           r_   r  r    s    0 $###@1D111D113!T"" 	@15? 	@ "">??? @ @ @ 63???@ ##A&&F9 ~~r"  )	_signsimpc                   ddl m} ddlm} t	          j        |dg           t          |           } |r ||           } i }d|v r|d         |d<   t          | t          t          f          s\| j
        s*t          | t                    st          | t                    s| S t          | d          } |                                 \  }}nt          |           dk    r| \  }}t          |t                     rBt          |t                     r-|j        |d<   |j        |d	<   |                    dd          |d<   |                                |                                }}n6t          | t                    rt          |           S t+          d
| z            ddlm 	 |                               rt3                       |||fg|R i |\  }	\  }
}|	j        s?t          | t          t          f          s|                                 S t8          j        ||fS nU# t2          $ rG}| j        r$|                               st3          |          | j        s| j         rQtC          | j"        fdd          \  }}d |D             } | j#        tI           | j#        |           g|R  cY d}~S g }tK          |           }tM          |           |D ]n}t          |t          t          tN          f          r%	 |(                    |tI          |          f           |)                                 _# tT          $ r Y kw xY w| +                    tY          |                    cY d}~S d}~ww xY wd|
$                    |          c}\  }}|                    dd          rd|vr
|	j-        |d<   t          | t          t          f          s,||                                |                                z  z  S |                                |                                }}|                    dd          s|||fS |t!          |g|R i |t!          |g|R i |fS )a[  
    Cancel common factors in a rational function ``f``.

    Examples
    ========

    >>> from sympy import cancel, sqrt, Symbol, together
    >>> from sympy.abc import x
    >>> A = Symbol('A', commutative=False)

    >>> cancel((2*x**2 - 2)/(x**2 - 2*x + 1))
    (2*x + 2)/(x - 1)
    >>> cancel((sqrt(3) + sqrt(15)*A)/(sqrt(2) + sqrt(10)*A))
    sqrt(6)/2

    Note: due to automatic distribution of Rationals, a sum divided by an integer
    will appear as a sum. To recover a rational form use `together` on the result:

    >>> cancel(x/2 + 1)
    x/2 + 1
    >>> together(_)
    (x + 2)/2
    r   )signsimp)sringr_  T)radicalr  rT   r   zunexpected argument: %sr*  c                 B    | j         du o|                                S r  )r  has)r  r+  s    r_   r  zcancel.<locals>.<lambda>y  s'     D(Ay1A1A-A rb   r  c                 ,    g | ]}t          |          S r   )r  r  s     r_   r   zcancel.<locals>.<listcomp>|  s    (((&))(((rb   Nr{   F).r  r&  sympy.polys.ringsr'  ri   rB  r!   rQ   r@  r   r:  r   r	   r   r  r}   rR   rT   r   r  rX   r  r-  r+  r*  r8   ngensr"  r   r  r  r  r  rK   rw   r^   r  r#   r<  r%   r,  skiprk   r  rn   r   )rZ   r$  rT   rw   r&  r'  rx   r  r}  r  r   r   r  r   ncr4  poter  r  r+  s                       @r_   r  r  4  s   2 100000''''''$	***

A HQKK
C$ %G}Ga%(( 8; 	*Q
33 	:a;N;N 	HD)))!!11	Q1 
81a 	2:a#6#6 	2&CKHCM777D11CLyy{{AIIKK1	Au		 8A2Q6777>>>>>> *55 	$!###E1a&04000400	6Aqw 	#a%00 #xxzz!ua{"		#
  * * * 	'AEE)$4$4 	'!#&&&8 	*qx 	* "B "B "B "B  EAr )(R(((B16&,,2r222222222D$Q''CIII  a%!<== KKF1II///HHJJJJ*   D::d4jj))))))))/*2 188A;;IAv1
www  6#4  iFa%(( C!))++aiikk)**yy{{AIIKK1www&& 	Ca7Nd1+t+++s++T!-Bd-B-B-Bc-B-BBBsW   A-H H M/(BM*,M/2AM*:8L32M*3
M =M*?M  $M*$M/*M/c                    t          j        |ddg           	 t          | gt          |          z   g|R i |\  }n## t          $ r}t          dd|          d}~ww xY wj        }d}j        rE|j        r>|j	        s7
                    t          |                                                    d}dd	lm}  |j        j        j                  \  }	}
t#          |          D ]N\  }}|                    j                  j                                        }|	                    |          ||<   O|d                             |d
d                   \  }}fd|D             }t.                              t          |                    }|r6	 d |D             |                                }}||}}n# t4          $ r Y nw xY wj        s d |D             |                                fS ||fS )a<  
    Reduces a polynomial ``f`` modulo a set of polynomials ``G``.

    Given a polynomial ``f`` and a set of polynomials ``G = (g_1, ..., g_n)``,
    computes a set of quotients ``q = (q_1, ..., q_n)`` and the remainder ``r``
    such that ``f = q_1*g_1 + ... + q_n*g_n + r``, where ``r`` vanishes or ``r``
    is a completely reduced polynomial with respect to ``G``.

    Examples
    ========

    >>> from sympy import reduced
    >>> from sympy.abc import x, y

    >>> reduced(2*x**4 + y**2 - x**2 + y**3, [x**3 - x, y**3 - y])
    ([2*x, 1], x**2 + y**2 + y)

    r_  r  reducedr   NFr&  Txringr{   c                 `    g | ]*}t                               t          |                    +S r   rR   ro   rn   r   r}  rx   s     r_   r   zreduced.<locals>.<listcomp>  s-    2221a#	&	&222rb   c                 6    g | ]}|                                 S r   r  r   r}  s     r_   r   zreduced.<locals>.<listcomp>  s     ---aaiikk---rb   c                 6    g | ]}|                                 S r   r  r;  s     r_   r   zreduced.<locals>.<listcomp>  s     '''		'''rb   )ri   rB  r(  rq   r;   r<   r   r  r  r  r  rn   r  r,  r5  rT   rg   r   r   re   r   r   r  rR   ro   r  r5   r_  rX   )rZ   r   rT   rw   r_  r  r   r'  r5  _ringr   r   rM  r  r~  _Q_rrx   s                    @r_   r3  r3    s@   ( $& 12223,aS477]JTJJJTJJ
ss 3 3 3	1c2223 ZFG
x FN 6? iiF$4$4$6$677788''''''uSXsz3955HE1U## ) )4sz**.6688??4((a8<<abb	""DAq2222222AQ%%A 	--1---qyy{{B rqAA  	 	 	D	
 9 ''Q'''44!ts'   $? 
A	AA F> >
G
Gc                 "    t          | g|R i |S )a  
    Computes the reduced Groebner basis for a set of polynomials.

    Use the ``order`` argument to set the monomial ordering that will be
    used to compute the basis. Allowed orders are ``lex``, ``grlex`` and
    ``grevlex``. If no order is specified, it defaults to ``lex``.

    For more information on Groebner bases, see the references and the docstring
    of :func:`~.solve_poly_system`.

    Examples
    ========

    Example taken from [1].

    >>> from sympy import groebner
    >>> from sympy.abc import x, y

    >>> F = [x*y - 2*y, 2*y**2 - x**2]

    >>> groebner(F, x, y, order='lex')
    GroebnerBasis([x**2 - 2*y**2, x*y - 2*y, y**3 - 2*y], x, y,
                  domain='ZZ', order='lex')
    >>> groebner(F, x, y, order='grlex')
    GroebnerBasis([y**3 - 2*y, x**2 - 2*y**2, x*y - 2*y], x, y,
                  domain='ZZ', order='grlex')
    >>> groebner(F, x, y, order='grevlex')
    GroebnerBasis([y**3 - 2*y, x**2 - 2*y**2, x*y - 2*y], x, y,
                  domain='ZZ', order='grevlex')

    By default, an improved implementation of the Buchberger algorithm is
    used. Optionally, an implementation of the F5B algorithm can be used. The
    algorithm can be set using the ``method`` flag or with the
    :func:`sympy.polys.polyconfig.setup` function.

    >>> F = [x**2 - x - 1, (2*x - 1) * y - (x**10 - (1 - x)**10)]

    >>> groebner(F, x, y, method='buchberger')
    GroebnerBasis([x**2 - x - 1, y - 55], x, y, domain='ZZ', order='lex')
    >>> groebner(F, x, y, method='f5b')
    GroebnerBasis([x**2 - x - 1, y - 55], x, y, domain='ZZ', order='lex')

    References
    ==========

    1. [Buchberger01]_
    2. [Cox97]_

    )GroebnerBasisr   rT   rw   s      r_   r-   r-     s#    f *T***T***rb   c                 ,    t          | g|R i |j        S )a[  
    Checks if the ideal generated by a Groebner basis is zero-dimensional.

    The algorithm checks if the set of monomials not divisible by the
    leading monomial of any element of ``F`` is bounded.

    References
    ==========

    David A. Cox, John B. Little, Donal O'Shea. Ideals, Varieties and
    Algorithms, 3rd edition, p. 230

    )rA  is_zero_dimensionalrB  s      r_   rD  rD    s%     *T***T**>>rb   c                       e Zd ZdZd Zed             Zed             Zed             Z	ed             Z
ed             Zed             Zed	             Zd
 Zd Zd Zd Zd Zd Zed             Zd ZddZd ZdS )rA  z%Represents a reduced Groebner basis. c                   	 t          j        |ddg           	 t          |g|R i |\  }n0# t          $ r#}t	          dt          |          |          d}~ww xY wddlm}  |j        j	        j
                  		fd|D             }t          |	j                  }fd	|D             }|                     |          S )
z>Compute a reduced Groebner basis for a system of polynomials. r_  r  r-   Nr   )PolyRingc                 l    g | ]0}|                     |j                                                  1S r   )r   re   r   )r   rM  rings     r_   r   z)GroebnerBasis.__new__.<locals>.<listcomp>/  s8    NNNN 0 0 2 233NNNrb   r
  c                 F    g | ]}t                               |          S r   )rR   ro   r   r[   rx   s     r_   r   z)GroebnerBasis.__new__.<locals>.<listcomp>2  s'    000T__Q$$000rb   )ri   rB  r(  r;   r<   r}   r,  rG  rT   r   rg   	_groebnerr  _new)
rv   r   rT   rw   r_  r  rG  r   rx   rI  s
           @@r_   ry   zGroebnerBasis.__new__#  s   dWh$7888	=0BTBBBTBBJE33! 	= 	= 	=#JA<<<	= 	/.....x#*ci88NNNNNNNeT#*5550000a000xx3s   / 
AAAc                 d    t          j        |           }t          |          |_        ||_        |S r   )r   ry   r@  _basis_options)rv   basisri   r~   s       r_   rM  zGroebnerBasis._new6  s*    mC  5\\

rb   c                 \    d | j         D             }t          | t          | j        j         fS )Nc              3   >   K   | ]}|                                 V  d S r   r  )r   r  s     r_   rr  z%GroebnerBasis.args.<locals>.<genexpr>A  s*      22222222rb   )rO  r   rP  rT   )r   rQ  s     r_   rw   zGroebnerBasis.args?  s0    22dk222uudm&89::rb   c                 $    d | j         D             S )Nc                 6    g | ]}|                                 S r   r  )r   rM  s     r_   r   z'GroebnerBasis.exprs.<locals>.<listcomp>F  s     7774777rb   )rO  r   s    r_   r'  zGroebnerBasis.exprsD  s    774;7777rb   c                 *    t          | j                  S r   )rq   rO  r   s    r_   r_  zGroebnerBasis.polysH  s    DK   rb   c                     | j         j        S r   )rP  rT   r   s    r_   rT   zGroebnerBasis.gensL  s    }!!rb   c                     | j         j        S r   )rP  r   r   s    r_   r   zGroebnerBasis.domainP  s    }##rb   c                     | j         j        S r   )rP  rg   r   s    r_   rg   zGroebnerBasis.orderT  s    }""rb   c                 *    t          | j                  S r   )r}   rO  r   s    r_   __len__zGroebnerBasis.__len__X  s    4;rb   c                 j    | j         j        rt          | j                  S t          | j                  S r   )rP  r_  iterr'  r   s    r_   r  zGroebnerBasis.__iter__[  s/    = 	$
###
###rb   c                 H    | j         j        r| j        }n| j        }||         S r   )rP  r_  r'  )r   itemrQ  s      r_   __getitem__zGroebnerBasis.__getitem__a  s)    = 	JEEJET{rb   c                 v    t          | j        t          | j                                                  f          S r   )hashrO  r@  rP  r   r   s    r_   r   zGroebnerBasis.__hash__i  s-    T[%(;(;(=(=">">?@@@rb   c                     t          || j                  r | j        |j        k    o| j        |j        k    S t	          |          r0| j        t          |          k    p| j        t          |          k    S dS )NF)rQ   r   rO  rP  rJ   r_  rq   r'  r   r  s     r_   r  zGroebnerBasis.__eq__l  sk    eT^,, 	;%,.R4=EN3RRe__ 	:e,I
d5kk0II5rb   c                     | |k     S r   r   rd  s     r_   r  zGroebnerBasis.__ne__t  s    5=  rb   c                     d }t          dgt          | j                  z            }| j        j        }| j        D ](}|                    |          } ||          r||z  })t          |          S )a{  
        Checks if the ideal generated by a Groebner basis is zero-dimensional.

        The algorithm checks if the set of monomials not divisible by the
        leading monomial of any element of ``F`` is bounded.

        References
        ==========

        David A. Cox, John B. Little, Donal O'Shea. Ideals, Varieties and
        Algorithms, 3rd edition, p. 230

        c                 N    t          t          t          |                     dk    S r   )sumr   r/  )monomials    r_   
single_varz5GroebnerBasis.is_zero_dimensional.<locals>.single_var  s    s4**++q00rb   r   r1  )r.   r}   rT   rP  rg   r_  r  r$  )r   rj  r  rg   rM  ri  s         r_   rD  z!GroebnerBasis.is_zero_dimensionalw  s    	1 	1 	1 aSTY/00	#J 	& 	&DwwUw++Hz(## &X%	
 9~~rb   c                    | j         j        }t          |          }||k    r| S | j        st	          d          t          | j                  }j        }                    t          |
                                |                    ddlm}  |j        j        |          \  }}t          |          D ]N\  }	}
|
                    j                  j                                        }
|                    |
          ||	<   Ot'          |||          }fd|D             }|j        sd |D             }|_        |                     |          S )a  
        Convert a Groebner basis from one ordering to another.

        The FGLM algorithm converts reduced Groebner bases of zero-dimensional
        ideals from one ordering to another. This method is often used when it
        is infeasible to compute a Groebner basis with respect to a particular
        ordering directly.

        Examples
        ========

        >>> from sympy.abc import x, y
        >>> from sympy import groebner

        >>> F = [x**2 - 3*y - x + 1, y**2 - 2*x + y - 1]
        >>> G = groebner(F, x, y, order='grlex')

        >>> list(G.fglm('lex'))
        [2*x - y**2 - y + 1, y**4 + 2*y**3 - 3*y**2 - 16*y + 7]
        >>> list(groebner(F, x, y, order='lex'))
        [2*x - y**2 - y + 1, y**4 + 2*y**3 - 3*y**2 - 16*y + 7]

        References
        ==========

        .. [1] J.C. Faugere, P. Gianni, D. Lazard, T. Mora (1994). Efficient
               Computation of Zero-dimensional Groebner Bases by Change of
               Ordering

        z?Cannot convert Groebner bases of ideals with positive dimension)r   rg   r   r4  c                 `    g | ]*}t                               t          |                    +S r   r7  rK  s     r_   r   z&GroebnerBasis.fglm.<locals>.<listcomp>  -    666qT__T!WWc**666rb   c                 F    g | ]}|                     d           d         S )Tr  r{   )r  )r   r[   s     r_   r   z&GroebnerBasis.fglm.<locals>.<listcomp>  s+    <<<Q--a0<<<rb   )rP  rg   r/   rD  rk   rq   rO  r   r  rn   r  r,  r5  rT   r   r   re   r   r   r,   r  rM  )r   rg   	src_order	dst_orderr_  r   r5  r=  r   r   rM  r   rx   s               @r_   fglmzGroebnerBasis.fglm  s~   > mI	 ''		! 	K' 	i%&ghhhT[!!ii##%%
 
 
  
 	,+++++53:y99q '' 	- 	-GAt??3:..2::<<Dt,,E!HHui006666A666 	 <<!<<<ACJyyC   rb   Tc                    t                               || j                  }|gt          | j                  z   }| j        j        }d}|rE|j        r>|j        s7                    t          |
                                                    d}ddlm}  |j        j        j                  \  }}	t          |          D ]N\  }
}|                    j                  j                                        }|                    |          ||
<   O|d                             |dd                   \  }}fd|D             }t                               t          |                    }|r6	 d	 |D             |                                }}||}}n# t.          $ r Y nw xY wj        s d
 |D             |                                fS ||fS )a#  
        Reduces a polynomial modulo a Groebner basis.

        Given a polynomial ``f`` and a set of polynomials ``G = (g_1, ..., g_n)``,
        computes a set of quotients ``q = (q_1, ..., q_n)`` and the remainder ``r``
        such that ``f = q_1*f_1 + ... + q_n*f_n + r``, where ``r`` vanishes or ``r``
        is a completely reduced polynomial with respect to ``G``.

        Examples
        ========

        >>> from sympy import groebner, expand
        >>> from sympy.abc import x, y

        >>> f = 2*x**4 - x**2 + y**3 + y**2
        >>> G = groebner([x**3 - x, y**3 - y])

        >>> G.reduce(f)
        ([2*x, 1], x**2 + y**2 + y)
        >>> Q, r = _

        >>> expand(sum(q*g for q, g in zip(Q, G)) + r)
        2*x**4 - x**2 + y**3 + y**2
        >>> _ == f
        True

        Fr&  Tr   r4  r{   Nc                 `    g | ]*}t                               t          |                    +S r   r7  r8  s     r_   r   z(GroebnerBasis.reduce.<locals>.<listcomp>
  rm  rb   c                 6    g | ]}|                                 S r   r:  r;  s     r_   r   z(GroebnerBasis.reduce.<locals>.<listcomp>  s     111!!))++111rb   c                 6    g | ]}|                                 S r   r  r;  s     r_   r   z(GroebnerBasis.reduce.<locals>.<listcomp>  s     +++AAIIKK+++rb   )rR   rt   rP  rq   rO  r   r  r  r  rn   r  r,  r5  rT   rg   r   r   re   r   r   r  ro   r  r5   r_  rX   )r   r   r  rM  r_  r   r'  r5  r=  r   r   r  r~  r>  r?  rx   s                  @r_   r   zGroebnerBasis.reduce  s   8 tT]33dk***m 	FN 	6? 	))D(8(8(:(:;;;<<CG++++++53:sy99q '' 	- 	-GAt??3:..2::<<Dt,,E!HHQx||E!""I&&16666A666OODGGS)) 	11q111199;;B 21 "   
 y 	+++++QYY[[88a4Ks   3 F 
F%$F%c                 @    |                      |          d         dk    S )am  
        Check if ``poly`` belongs the ideal generated by ``self``.

        Examples
        ========

        >>> from sympy import groebner
        >>> from sympy.abc import x, y

        >>> f = 2*x**3 + y**3 + 3*y
        >>> G = groebner([x**2 + y**2 - 1, x*y - 2])

        >>> G.contains(f)
        True
        >>> G.contains(f + 1)
        False

        r{   r   )r   )r   rM  s     r_   containszGroebnerBasis.contains  s     & {{4  #q((rb   Nr  )rY   r
  r  r  ry   r  rM  r  rw   r'  r_  rT   r   rg   r[  r  r`  r   r  r  rD  rq  r   rw  r   rb   r_   rA  rA    s       //     &   [ ; ; X; 8 8 X8 ! ! X! " " X" $ $ X$ # # X#     $ $ $  A A A  ! ! !   X>@! @! @!D? ? ? ?B) ) ) ) )rb   rA  c                     t          j        g            fdt          |           } | j        rt	          | g|R i S dvrdd<   t          j        |          } | |          S )z
    Efficiently transform an expression into a polynomial.

    Examples
    ========

    >>> from sympy import poly
    >>> from sympy.abc import x

    >>> poly(x*(x**2 + x - 1)**2)
    Poly(x**5 + 2*x**4 - x**3 - 2*x**2 + x, x, domain='ZZ')

    c                    g g }}t          j        |           D ]n}g g }}t          j        |          D ]}|j        r |                     
||                     )|j        r`|j        j        rT|j        j        rH|j        dk    r=|                     
|j        |          	                    |j                             |                    |           |s|                    |           |d         }|dd          D ]}|
                    |          }|rTt          | }|j        r|
                    |          }n.|
                    t                              ||                    }|                    |           p|st                              | |          }	n|d         }	|dd          D ]}|	                    |          }	|rTt          | }|j        r|	                    |          }	n.|	                    t                              ||                    }	 |	j        |                    dd          i S )Nr   r{   rT   r   )r
   r  r   r  r,  r  r  r  r  ry  r   r:  rR   rt   r  r   r  )r   rx   r  
poly_termsr  r%  poly_factorsr  productr]   _polyrw   s             r_   r}  zpoly.<locals>._polyA  s{   zM$'' 	+ 	+D$&\G--- + += + ''fc(:(:;;;;] +v{'9 +
-+28*/+ ''fk3//33FJ??A A A A NN6**** +T""""&q/*122. 2 2F%kk&11GG L ']F' L")++f"5"5")++doofc.J.J"K"K!!'**** 	D__T3//FF]F"122 * *D)) DE{> D#ZZ--FF#ZZc(B(BCCFv~swwvr22;d;;;rb   r"  F)ri   rB  r!   rr   rR   rj   )r   rT   rw   rx   r}  s     ` @r_   rM  rM  0  s     $###2< 2< 2< 2< 2< 2<h 4==D| )D(4(((4(((t X

d
+
+C5srb   r  r   )FNNNFFFr  r  r  r	  )r  	functoolsr   r   operatorr   typingr   
sympy.corer   r	   r
   r   sympy.core.basicr   sympy.core.decoratorsr   sympy.core.exprtoolsr   r   r   sympy.core.evalfr   r   r   r   r   sympy.core.functionr   sympy.core.mulr   r   sympy.core.numbersr   r   r   sympy.core.relationalr   r   sympy.core.sortingr   sympy.core.symbolr   r    sympy.core.sympifyr!   r"   sympy.core.traversalr#   r$   sympy.logic.boolalgr%   sympy.polysr&   ri   sympy.polys.constructorr'   sympy.polys.domainsr(   r)   r*   !sympy.polys.domains.domainelementr+   sympy.polys.fglmtoolsr,   sympy.polys.groebnertoolsr-   rL  sympy.polys.monomialsr.   sympy.polys.orderingsr/   sympy.polys.polyclassesr0   r1   r2   sympy.polys.polyerrorsr3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   sympy.polys.polyutilsr>   r?   r@   rA   rB   rC   sympy.polys.rationaltoolsrD   sympy.polys.rootisolationrE   sympy.utilitiesrF   rG   rH   sympy.utilities.exceptionsrI   sympy.utilities.iterablesrJ   rK   r^  rw  mpmath.libmp.libhyperrL   ra   rR   r  r   r  r(  r&  r8  r  r  r  r  r  r  r|  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r^  r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r!  r8  r+  r  r2  rH  rZ  rg  r  r  r  r  r3  rD  rA  rM  r   rb   r_   <module>r     s   > > $ # # # # # # #                        # " " " " " , , , , , , A A A A A A A A A AM M M M M M M M M M M M M M * * * * * * + + + + + + + + / / / / / / / / / / 6 6 6 6 6 6 6 6 & & & & & & + + + + + + + + 0 0 0 0 0 0 0 0 > > > > > > > > + + + + + + . . . . . . 4 4 4 4 4 4 * * * * * * * * * * ; ; ; ; ; ; - - - - - - ; ; ; ; ; ; * * * * * * . . . . . . 1 1 1 1 1 1 1 1 1 1	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	                / . . . . . A A A A A A 5 5 5 5 5 5 5 5 5 5 @ @ @ @ @ @ 4 4 4 4 4 4 4 4      / / / / / /  D AA` AA` AA` AA` AA`5 AA` AA` AA`HB Z Z Z Z Zt Z Z Zz & & &% % %P 0 0 0[ [ [|   7N 7N 7N 7Nt 1 1 1h ( ( (4 ! ! !0   2 ! ! !2   :   :   D   D   >   >   >   D # # #L # # #L 1 1 1h   : &+ ! ! ! ! !H   : % % %P Q Q Qh 0 0 0 0f J J JZ / / / /d r3 r3 r3j   :   :   0 * * *Z   :   :   : + + +\ < < <
      B   :$ $ $ 6 6 6
7 7 7t  )F )F )FX8 8 8| | |~) ) )X = = =" 8 8 8" @ @ @"  _' _' _' _' _'D 4 4 4 4n U U U U8 + + + +@ + + + +6 = = = =<   : ( ( (V # cC cC cC cC cCL 8 8 8v 2+ 2+ 2+j ? ? ?" M) M) M) M) M)E M) M) M)` N N N N Nrb   