<s> """<STR_LIT>""" <EOL> from __future__ import print_function , division <EOL> from sympy . core import ( <EOL> S , Basic , Expr , I , Integer , Add , Mul , Dummy , Tuple <EOL> ) <EOL> from sympy . core . mul import _keep_coeff <EOL> from sympy . core . symbol import Symbol <EOL> from sympy . core . basic import preorder_traversal <EOL> from sympy . core . relational import Relational <EOL> from sympy . core . sympify import sympify <EOL> from sympy . core . decorators import _sympifyit <EOL> from sympy . core . function import Derivative <EOL> from sympy . logic . boolalg import BooleanAtom <EOL> from sympy . polys . polyclasses import DMP <EOL> from sympy . polys . polyutils import ( <EOL> basic_from_dict , <EOL> _sort_gens , <EOL> _unify_gens , <EOL> _dict_reorder , <EOL> _dict_from_expr , <EOL> _parallel_dict_from_expr , <EOL> ) <EOL> from sympy . polys . rationaltools import together <EOL> from sympy . polys . rootisolation import dup_isolate_real_roots_list <EOL> from sympy . polys . groebnertools import groebner as _groebner <EOL> from sympy . polys . fglmtools import matrix_fglm <EOL> from sympy . polys . monomials import Monomial <EOL> from sympy . polys . orderings import monomial_key <EOL> from sympy . polys . polyerrors import ( <EOL> OperationNotSupported , DomainError , <EOL> CoercionFailed , UnificationFailed , <EOL> GeneratorsNeeded , PolynomialError , <EOL> MultivariatePolynomialError , <EOL> ExactQuotientFailed , <EOL> PolificationFailed , <EOL> ComputationFailed , <EOL> GeneratorsError , <EOL> ) <EOL> from sympy . utilities import group , sift , public <EOL> import sympy . polys <EOL> import mpmath <EOL> from mpmath . libmp . libhyper import NoConvergence <EOL> from sympy . polys . domains import FF , QQ , ZZ <EOL> from sympy . polys . constructor import construct_domain <EOL> from sympy . polys import polyoptions as options <EOL> from sympy . core . compatibility import iterable , range <EOL> @ public <EOL> class Poly ( Expr ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> is_commutative = True <EOL> is_Poly = True <EOL> def __new__ ( cls , rep , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> opt = options . build_options ( gens , args ) <EOL> if '<STR_LIT>' in opt : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> if iterable ( rep , exclude = str ) : <EOL> if isinstance ( rep , dict ) : <EOL> return cls . _from_dict ( rep , opt ) <EOL> else : <EOL> return cls . _from_list ( list ( rep ) , opt ) <EOL> else : <EOL> rep = sympify ( rep ) <EOL> if rep . is_Poly : <EOL> return cls . _from_poly ( rep , opt ) <EOL> else : <EOL> return cls . _from_expr ( rep , opt ) <EOL> @ classmethod <EOL> def new ( cls , rep , * gens ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( rep , DMP ) : <EOL> raise PolynomialError ( <EOL> "<STR_LIT>" % rep ) <EOL> elif rep . lev != len ( gens ) - <NUM_LIT:1> : <EOL> raise PolynomialError ( "<STR_LIT>" % ( rep , gens ) ) <EOL> obj = Basic . __new__ ( cls ) <EOL> obj . rep = rep <EOL> obj . gens = gens <EOL> return obj <EOL> @ classmethod <EOL> def from_dict ( cls , rep , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> opt = options . build_options ( gens , args ) <EOL> return cls . _from_dict ( rep , opt ) <EOL> @ classmethod <EOL> def from_list ( cls , rep , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> opt = options . build_options ( gens , args ) <EOL> return cls . _from_list ( rep , opt ) <EOL> @ classmethod <EOL> def from_poly ( cls , rep , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> opt = options . build_options ( gens , args ) <EOL> return cls . _from_poly ( rep , opt ) <EOL> @ classmethod <EOL> def from_expr ( cls , rep , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> opt = options . build_options ( gens , args ) <EOL> return cls . _from_expr ( rep , opt ) <EOL> @ classmethod <EOL> def _from_dict ( cls , rep , opt ) : <EOL> """<STR_LIT>""" <EOL> gens = opt . gens <EOL> if not gens : <EOL> raise GeneratorsNeeded ( <EOL> "<STR_LIT>" ) <EOL> level = len ( gens ) - <NUM_LIT:1> <EOL> domain = opt . domain <EOL> if domain is None : <EOL> domain , rep = construct_domain ( rep , opt = opt ) <EOL> else : <EOL> for monom , coeff in rep . items ( ) : <EOL> rep [ monom ] = domain . convert ( coeff ) <EOL> return cls . new ( DMP . from_dict ( rep , level , domain ) , * gens ) <EOL> @ classmethod <EOL> def _from_list ( cls , rep , opt ) : <EOL> """<STR_LIT>""" <EOL> gens = opt . gens <EOL> if not gens : <EOL> raise GeneratorsNeeded ( <EOL> "<STR_LIT>" ) <EOL> elif len ( gens ) != <NUM_LIT:1> : <EOL> raise MultivariatePolynomialError ( <EOL> "<STR_LIT>" ) <EOL> level = len ( gens ) - <NUM_LIT:1> <EOL> domain = opt . domain <EOL> if domain is None : <EOL> domain , rep = construct_domain ( rep , opt = opt ) <EOL> else : <EOL> rep = list ( map ( domain . convert , rep ) ) <EOL> return cls . new ( DMP . from_list ( rep , level , domain ) , * gens ) <EOL> @ classmethod <EOL> def _from_poly ( cls , rep , opt ) : <EOL> """<STR_LIT>""" <EOL> if cls != rep . __class__ : <EOL> rep = cls . new ( rep . rep , * rep . gens ) <EOL> gens = opt . gens <EOL> field = opt . field <EOL> domain = opt . domain <EOL> if gens and rep . gens != gens : <EOL> if set ( rep . gens ) != set ( gens ) : <EOL> return cls . _from_expr ( rep . as_expr ( ) , opt ) <EOL> else : <EOL> rep = rep . reorder ( * gens ) <EOL> if '<STR_LIT>' in opt and domain : <EOL> rep = rep . set_domain ( domain ) <EOL> elif field is True : <EOL> rep = rep . to_field ( ) <EOL> return rep <EOL> @ classmethod <EOL> def _from_expr ( cls , rep , opt ) : <EOL> """<STR_LIT>""" <EOL> rep , opt = _dict_from_expr ( rep , opt ) <EOL> return cls . _from_dict ( rep , opt ) <EOL> def _hashable_content ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( self . rep , self . gens ) <EOL> def __hash__ ( self ) : <EOL> return super ( Poly , self ) . __hash__ ( ) <EOL> @ property <EOL> def free_symbols ( self ) : <EOL> """<STR_LIT>""" <EOL> symbols = set ( [ ] ) <EOL> for gen in self . gens : <EOL> symbols |= gen . free_symbols <EOL> return symbols | self . free_symbols_in_domain <EOL> @ property <EOL> def free_symbols_in_domain ( self ) : <EOL> """<STR_LIT>""" <EOL> domain , symbols = self . rep . dom , set ( ) <EOL> if domain . is_Composite : <EOL> for gen in domain . symbols : <EOL> symbols |= gen . free_symbols <EOL> elif domain . is_EX : <EOL> for coeff in self . coeffs ( ) : <EOL> symbols |= coeff . free_symbols <EOL> return symbols <EOL> @ property <EOL> def args ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( self . as_expr ( ) , ) <EOL> @ property <EOL> def gen ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . gens [ <NUM_LIT:0> ] <EOL> @ property <EOL> def domain ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . get_domain ( ) <EOL> @ property <EOL> def zero ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . new ( self . rep . zero ( self . rep . lev , self . rep . dom ) , * self . gens ) <EOL> @ property <EOL> def one ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . new ( self . rep . one ( self . rep . lev , self . rep . dom ) , * self . gens ) <EOL> @ property <EOL> def unit ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . new ( self . rep . unit ( self . rep . lev , self . rep . dom ) , * self . gens ) <EOL> def unify ( f , g ) : <EOL> """<STR_LIT>""" <EOL> _ , per , F , G = f . _unify ( g ) <EOL> return per ( F ) , per ( G ) <EOL> def _unify ( f , g ) : <EOL> g = sympify ( g ) <EOL> if not g . is_Poly : <EOL> try : <EOL> return f . rep . dom , f . per , f . rep , f . rep . per ( f . rep . dom . from_sympy ( g ) ) <EOL> except CoercionFailed : <EOL> raise UnificationFailed ( "<STR_LIT>" % ( f , g ) ) <EOL> if isinstance ( f . rep , DMP ) and isinstance ( g . rep , DMP ) : <EOL> gens = _unify_gens ( f . gens , g . gens ) <EOL> dom , lev = f . rep . dom . unify ( g . rep . dom , gens ) , len ( gens ) - <NUM_LIT:1> <EOL> if f . gens != gens : <EOL> f_monoms , f_coeffs = _dict_reorder ( <EOL> f . rep . to_dict ( ) , f . gens , gens ) <EOL> if f . rep . dom != dom : <EOL> f_coeffs = [ dom . convert ( c , f . rep . dom ) for c in f_coeffs ] <EOL> F = DMP ( dict ( list ( zip ( f_monoms , f_coeffs ) ) ) , dom , lev ) <EOL> else : <EOL> F = f . rep . convert ( dom ) <EOL> if g . gens != gens : <EOL> g_monoms , g_coeffs = _dict_reorder ( <EOL> g . rep . to_dict ( ) , g . gens , gens ) <EOL> if g . rep . dom != dom : <EOL> g_coeffs = [ dom . convert ( c , g . rep . dom ) for c in g_coeffs ] <EOL> G = DMP ( dict ( list ( zip ( g_monoms , g_coeffs ) ) ) , dom , lev ) <EOL> else : <EOL> G = g . rep . convert ( dom ) <EOL> else : <EOL> raise UnificationFailed ( "<STR_LIT>" % ( f , g ) ) <EOL> cls = f . __class__ <EOL> def per ( rep , dom = dom , gens = gens , remove = None ) : <EOL> if remove is not None : <EOL> gens = gens [ : remove ] + gens [ remove + <NUM_LIT:1> : ] <EOL> if not gens : <EOL> return dom . to_sympy ( rep ) <EOL> return cls . new ( rep , * gens ) <EOL> return dom , per , F , G <EOL> def per ( f , rep , gens = None , remove = None ) : <EOL> """<STR_LIT>""" <EOL> if gens is None : <EOL> gens = f . gens <EOL> if remove is not None : <EOL> gens = gens [ : remove ] + gens [ remove + <NUM_LIT:1> : ] <EOL> if not gens : <EOL> return f . rep . dom . to_sympy ( rep ) <EOL> return f . __class__ . new ( rep , * gens ) <EOL> def set_domain ( f , domain ) : <EOL> """<STR_LIT>""" <EOL> opt = options . build_options ( f . gens , { '<STR_LIT>' : domain } ) <EOL> return f . per ( f . rep . convert ( opt . domain ) ) <EOL> def get_domain ( f ) : <EOL> """<STR_LIT>""" <EOL> return f . rep . dom <EOL> def set_modulus ( f , modulus ) : <EOL> """<STR_LIT>""" <EOL> modulus = options . Modulus . preprocess ( modulus ) <EOL> return f . set_domain ( FF ( modulus ) ) <EOL> def get_modulus ( f ) : <EOL> """<STR_LIT>""" <EOL> domain = f . get_domain ( ) <EOL> if domain . is_FiniteField : <EOL> return Integer ( domain . characteristic ( ) ) <EOL> else : <EOL> raise PolynomialError ( "<STR_LIT>" ) <EOL> def _eval_subs ( f , old , new ) : <EOL> """<STR_LIT>""" <EOL> if old in f . gens : <EOL> if new . is_number : <EOL> return f . eval ( old , new ) <EOL> else : <EOL> try : <EOL> return f . replace ( old , new ) <EOL> except PolynomialError : <EOL> pass <EOL> return f . as_expr ( ) . subs ( old , new ) <EOL> def exclude ( f ) : <EOL> """<STR_LIT>""" <EOL> J , new = f . rep . exclude ( ) <EOL> gens = [ ] <EOL> for j in range ( len ( f . gens ) ) : <EOL> if j not in J : <EOL> gens . append ( f . gens [ j ] ) <EOL> return f . per ( new , gens = gens ) <EOL> def replace ( f , x , y = None ) : <EOL> """<STR_LIT>""" <EOL> if y is None : <EOL> if f . is_univariate : <EOL> x , y = f . gen , x <EOL> else : <EOL> raise PolynomialError ( <EOL> "<STR_LIT>" ) <EOL> if x == y : <EOL> return f <EOL> if x in f . gens and y not in f . gens : <EOL> dom = f . get_domain ( ) <EOL> if not dom . is_Composite or y not in dom . symbols : <EOL> gens = list ( f . gens ) <EOL> gens [ gens . index ( x ) ] = y <EOL> return f . per ( f . rep , gens = gens ) <EOL> raise PolynomialError ( "<STR_LIT>" % ( x , y , f ) ) <EOL> def reorder ( f , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> opt = options . Options ( ( ) , args ) <EOL> if not gens : <EOL> gens = _sort_gens ( f . gens , opt = opt ) <EOL> elif set ( f . gens ) != set ( gens ) : <EOL> raise PolynomialError ( <EOL> "<STR_LIT>" ) <EOL> rep = dict ( list ( zip ( * _dict_reorder ( f . rep . to_dict ( ) , f . gens , gens ) ) ) ) <EOL> return f . per ( DMP ( rep , f . rep . dom , len ( gens ) - <NUM_LIT:1> ) , gens = gens ) <EOL> def ltrim ( f , gen ) : <EOL> """<STR_LIT>""" <EOL> rep = f . as_dict ( native = True ) <EOL> j = f . _gen_to_level ( gen ) <EOL> terms = { } <EOL> for monom , coeff in rep . items ( ) : <EOL> monom = monom [ j : ] <EOL> if monom not in terms : <EOL> terms [ monom ] = coeff <EOL> else : <EOL> raise PolynomialError ( "<STR_LIT>" % f ) <EOL> gens = f . gens [ j : ] <EOL> return f . new ( DMP . from_dict ( terms , len ( gens ) - <NUM_LIT:1> , f . rep . dom ) , * gens ) <EOL> def has_only_gens ( f , * gens ) : <EOL> """<STR_LIT>""" <EOL> indices = set ( [ ] ) <EOL> for gen in gens : <EOL> try : <EOL> index = f . gens . index ( gen ) <EOL> except ValueError : <EOL> raise GeneratorsError ( <EOL> "<STR_LIT>" % ( f , gen ) ) <EOL> else : <EOL> indices . add ( index ) <EOL> for monom in f . monoms ( ) : <EOL> for i , elt in enumerate ( monom ) : <EOL> if i not in indices and elt : <EOL> return False <EOL> return True <EOL> def to_ring ( f ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = f . rep . to_ring ( ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return f . per ( result ) <EOL> def to_field ( f ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = f . rep . to_field ( ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return f . per ( result ) <EOL> def to_exact ( f ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = f . rep . to_exact ( ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return f . per ( result ) <EOL> def retract ( f , field = None ) : <EOL> """<STR_LIT>""" <EOL> dom , rep = construct_domain ( f . as_dict ( zero = True ) , <EOL> field = field , composite = f . domain . is_Composite or None ) <EOL> return f . from_dict ( rep , f . gens , domain = dom ) <EOL> def slice ( f , x , m , n = None ) : <EOL> """<STR_LIT>""" <EOL> if n is None : <EOL> j , m , n = <NUM_LIT:0> , x , m <EOL> else : <EOL> j = f . _gen_to_level ( x ) <EOL> m , n = int ( m ) , int ( n ) <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = f . rep . slice ( m , n , j ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return f . per ( result ) <EOL> def coeffs ( f , order = None ) : <EOL> """<STR_LIT>""" <EOL> return [ f . rep . dom . to_sympy ( c ) for c in f . rep . coeffs ( order = order ) ] <EOL> def monoms ( f , order = None ) : <EOL> """<STR_LIT>""" <EOL> return f . rep . monoms ( order = order ) <EOL> def terms ( f , order = None ) : <EOL> """<STR_LIT>""" <EOL> return [ ( m , f . rep . dom . to_sympy ( c ) ) for m , c in f . rep . terms ( order = order ) ] <EOL> def all_coeffs ( f ) : <EOL> """<STR_LIT>""" <EOL> return [ f . rep . dom . to_sympy ( c ) for c in f . rep . all_coeffs ( ) ] <EOL> def all_monoms ( f ) : <EOL> """<STR_LIT>""" <EOL> return f . rep . all_monoms ( ) <EOL> def all_terms ( f ) : <EOL> """<STR_LIT>""" <EOL> return [ ( m , f . rep . dom . to_sympy ( c ) ) for m , c in f . rep . all_terms ( ) ] <EOL> def termwise ( f , func , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> terms = { } <EOL> for monom , coeff in f . terms ( ) : <EOL> result = func ( monom , coeff ) <EOL> if isinstance ( result , tuple ) : <EOL> monom , coeff = result <EOL> else : <EOL> coeff = result <EOL> if coeff : <EOL> if monom not in terms : <EOL> terms [ monom ] = coeff <EOL> else : <EOL> raise PolynomialError ( <EOL> "<STR_LIT>" % monom ) <EOL> return f . from_dict ( terms , * ( gens or f . gens ) , ** args ) <EOL> def length ( f ) : <EOL> """<STR_LIT>""" <EOL> return len ( f . as_dict ( ) ) <EOL> def as_dict ( f , native = False , zero = False ) : <EOL> """<STR_LIT>""" <EOL> if native : <EOL> return f . rep . to_dict ( zero = zero ) <EOL> else : <EOL> return f . rep . to_sympy_dict ( zero = zero ) <EOL> def as_list ( f , native = False ) : <EOL> """<STR_LIT>""" <EOL> if native : <EOL> return f . rep . to_list ( ) <EOL> else : <EOL> return f . rep . to_sympy_list ( ) <EOL> def as_expr ( f , * gens ) : <EOL> """<STR_LIT>""" <EOL> if not gens : <EOL> gens = f . gens <EOL> elif len ( gens ) == <NUM_LIT:1> and isinstance ( gens [ <NUM_LIT:0> ] , dict ) : <EOL> mapping = gens [ <NUM_LIT:0> ] <EOL> gens = list ( f . gens ) <EOL> for gen , value in mapping . items ( ) : <EOL> try : <EOL> index = gens . index ( gen ) <EOL> except ValueError : <EOL> raise GeneratorsError ( <EOL> "<STR_LIT>" % ( f , gen ) ) <EOL> else : <EOL> gens [ index ] = value <EOL> return basic_from_dict ( f . rep . to_sympy_dict ( ) , * gens ) <EOL> def lift ( f ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = f . rep . lift ( ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return f . per ( result ) <EOL> def deflate ( f ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> J , result = f . rep . deflate ( ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return J , f . per ( result ) <EOL> def inject ( f , front = False ) : <EOL> """<STR_LIT>""" <EOL> dom = f . rep . dom <EOL> if dom . is_Numerical : <EOL> return f <EOL> elif not dom . is_Poly : <EOL> raise DomainError ( "<STR_LIT>" % dom ) <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = f . rep . inject ( front = front ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> if front : <EOL> gens = dom . symbols + f . gens <EOL> else : <EOL> gens = f . gens + dom . symbols <EOL> return f . new ( result , * gens ) <EOL> def eject ( f , * gens ) : <EOL> """<STR_LIT>""" <EOL> dom = f . rep . dom <EOL> if not dom . is_Numerical : <EOL> raise DomainError ( "<STR_LIT>" % dom ) <EOL> n , k = len ( f . gens ) , len ( gens ) <EOL> if f . gens [ : k ] == gens : <EOL> _gens , front = f . gens [ k : ] , True <EOL> elif f . gens [ - k : ] == gens : <EOL> _gens , front = f . gens [ : - k ] , False <EOL> else : <EOL> raise NotImplementedError ( <EOL> "<STR_LIT>" ) <EOL> dom = dom . inject ( * gens ) <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = f . rep . eject ( dom , front = front ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return f . new ( result , * _gens ) <EOL> def terms_gcd ( f ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> J , result = f . rep . terms_gcd ( ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return J , f . per ( result ) <EOL> def add_ground ( f , coeff ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = f . rep . add_ground ( coeff ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return f . per ( result ) <EOL> def sub_ground ( f , coeff ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = f . rep . sub_ground ( coeff ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return f . per ( result ) <EOL> def mul_ground ( f , coeff ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = f . rep . mul_ground ( coeff ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return f . per ( result ) <EOL> def quo_ground ( f , coeff ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = f . rep . quo_ground ( coeff ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return f . per ( result ) <EOL> def exquo_ground ( f , coeff ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = f . rep . exquo_ground ( coeff ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return f . per ( result ) <EOL> def abs ( f ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = f . rep . abs ( ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return f . per ( result ) <EOL> def neg ( f ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = f . rep . neg ( ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return f . per ( result ) <EOL> def add ( f , g ) : <EOL> """<STR_LIT>""" <EOL> g = sympify ( g ) <EOL> if not g . is_Poly : <EOL> return f . add_ground ( g ) <EOL> _ , per , F , G = f . _unify ( g ) <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = F . add ( G ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return per ( result ) <EOL> def sub ( f , g ) : <EOL> """<STR_LIT>""" <EOL> g = sympify ( g ) <EOL> if not g . is_Poly : <EOL> return f . sub_ground ( g ) <EOL> _ , per , F , G = f . _unify ( g ) <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = F . sub ( G ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return per ( result ) <EOL> def mul ( f , g ) : <EOL> """<STR_LIT>""" <EOL> g = sympify ( g ) <EOL> if not g . is_Poly : <EOL> return f . mul_ground ( g ) <EOL> _ , per , F , G = f . _unify ( g ) <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = F . mul ( G ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return per ( result ) <EOL> def sqr ( f ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = f . rep . sqr ( ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return f . per ( result ) <EOL> def pow ( f , n ) : <EOL> """<STR_LIT>""" <EOL> n = int ( n ) <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = f . rep . pow ( n ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return f . per ( result ) <EOL> def pdiv ( f , g ) : <EOL> """<STR_LIT>""" <EOL> _ , per , F , G = f . _unify ( g ) <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> q , r = F . pdiv ( G ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return per ( q ) , per ( r ) <EOL> def prem ( f , g ) : <EOL> """<STR_LIT>""" <EOL> _ , per , F , G = f . _unify ( g ) <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = F . prem ( G ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return per ( result ) <EOL> def pquo ( f , g ) : <EOL> """<STR_LIT>""" <EOL> _ , per , F , G = f . _unify ( g ) <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = F . pquo ( G ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return per ( result ) <EOL> def pexquo ( f , g ) : <EOL> """<STR_LIT>""" <EOL> _ , per , F , G = f . _unify ( g ) <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> try : <EOL> result = F . pexquo ( G ) <EOL> except ExactQuotientFailed as exc : <EOL> raise exc . new ( f . as_expr ( ) , g . as_expr ( ) ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return per ( result ) <EOL> def div ( f , g , auto = True ) : <EOL> """<STR_LIT>""" <EOL> dom , per , F , G = f . _unify ( g ) <EOL> retract = False <EOL> if auto and dom . has_Ring and not dom . has_Field : <EOL> F , G = F . to_field ( ) , G . to_field ( ) <EOL> retract = True <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> q , r = F . div ( G ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> if retract : <EOL> try : <EOL> Q , R = q . to_ring ( ) , r . to_ring ( ) <EOL> except CoercionFailed : <EOL> pass <EOL> else : <EOL> q , r = Q , R <EOL> return per ( q ) , per ( r ) <EOL> def rem ( f , g , auto = True ) : <EOL> """<STR_LIT>""" <EOL> dom , per , F , G = f . _unify ( g ) <EOL> retract = False <EOL> if auto and dom . has_Ring and not dom . has_Field : <EOL> F , G = F . to_field ( ) , G . to_field ( ) <EOL> retract = True <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> r = F . rem ( G ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> if retract : <EOL> try : <EOL> r = r . to_ring ( ) <EOL> except CoercionFailed : <EOL> pass <EOL> return per ( r ) <EOL> def quo ( f , g , auto = True ) : <EOL> """<STR_LIT>""" <EOL> dom , per , F , G = f . _unify ( g ) <EOL> retract = False <EOL> if auto and dom . has_Ring and not dom . has_Field : <EOL> F , G = F . to_field ( ) , G . to_field ( ) <EOL> retract = True <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> q = F . quo ( G ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> if retract : <EOL> try : <EOL> q = q . to_ring ( ) <EOL> except CoercionFailed : <EOL> pass <EOL> return per ( q ) <EOL> def exquo ( f , g , auto = True ) : <EOL> """<STR_LIT>""" <EOL> dom , per , F , G = f . _unify ( g ) <EOL> retract = False <EOL> if auto and dom . has_Ring and not dom . has_Field : <EOL> F , G = F . to_field ( ) , G . to_field ( ) <EOL> retract = True <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> try : <EOL> q = F . exquo ( G ) <EOL> except ExactQuotientFailed as exc : <EOL> raise exc . new ( f . as_expr ( ) , g . as_expr ( ) ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> if retract : <EOL> try : <EOL> q = q . to_ring ( ) <EOL> except CoercionFailed : <EOL> pass <EOL> return per ( q ) <EOL> def _gen_to_level ( f , gen ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( gen , int ) : <EOL> length = len ( f . gens ) <EOL> if - length <= gen < length : <EOL> if gen < <NUM_LIT:0> : <EOL> return length + gen <EOL> else : <EOL> return gen <EOL> else : <EOL> raise PolynomialError ( "<STR_LIT>" % <EOL> ( length , length , gen ) ) <EOL> else : <EOL> try : <EOL> return f . gens . index ( sympify ( gen ) ) <EOL> except ValueError : <EOL> raise PolynomialError ( <EOL> "<STR_LIT>" % gen ) <EOL> def degree ( f , gen = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> j = f . _gen_to_level ( gen ) <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> return f . rep . degree ( j ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> def degree_list ( f ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> return f . rep . degree_list ( ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> def total_degree ( f ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> return f . rep . total_degree ( ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> def homogenize ( f , s ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( s , Symbol ) : <EOL> raise TypeError ( "<STR_LIT>" % type ( s ) ) <EOL> if s in f . gens : <EOL> i = f . gens . index ( s ) <EOL> gens = f . gens <EOL> else : <EOL> i = len ( f . gens ) <EOL> gens = f . gens + ( s , ) <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> return f . per ( f . rep . homogenize ( i ) , gens = gens ) <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> def homogeneous_order ( f ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> return f . rep . homogeneous_order ( ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> def LC ( f , order = None ) : <EOL> """<STR_LIT>""" <EOL> if order is not None : <EOL> return f . coeffs ( order ) [ <NUM_LIT:0> ] <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = f . rep . LC ( ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return f . rep . dom . to_sympy ( result ) <EOL> def TC ( f ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = f . rep . TC ( ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return f . rep . dom . to_sympy ( result ) <EOL> def EC ( f , order = None ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> return f . coeffs ( order ) [ - <NUM_LIT:1> ] <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> def coeff_monomial ( f , monom ) : <EOL> """<STR_LIT>""" <EOL> return f . nth ( * Monomial ( monom , f . gens ) . exponents ) <EOL> def nth ( f , * N ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> if len ( N ) != len ( f . gens ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> result = f . rep . nth ( * list ( map ( int , N ) ) ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return f . rep . dom . to_sympy ( result ) <EOL> def coeff ( f , x , n = <NUM_LIT:1> , right = False ) : <EOL> raise NotImplementedError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def LM ( f , order = None ) : <EOL> """<STR_LIT>""" <EOL> return Monomial ( f . monoms ( order ) [ <NUM_LIT:0> ] , f . gens ) <EOL> def EM ( f , order = None ) : <EOL> """<STR_LIT>""" <EOL> return Monomial ( f . monoms ( order ) [ - <NUM_LIT:1> ] , f . gens ) <EOL> def LT ( f , order = None ) : <EOL> """<STR_LIT>""" <EOL> monom , coeff = f . terms ( order ) [ <NUM_LIT:0> ] <EOL> return Monomial ( monom , f . gens ) , coeff <EOL> def ET ( f , order = None ) : <EOL> """<STR_LIT>""" <EOL> monom , coeff = f . terms ( order ) [ - <NUM_LIT:1> ] <EOL> return Monomial ( monom , f . gens ) , coeff <EOL> def max_norm ( f ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = f . rep . max_norm ( ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return f . rep . dom . to_sympy ( result ) <EOL> def l1_norm ( f ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = f . rep . l1_norm ( ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return f . rep . dom . to_sympy ( result ) <EOL> def clear_denoms ( self , convert = False ) : <EOL> """<STR_LIT>""" <EOL> f = self <EOL> if not f . rep . dom . has_Field : <EOL> return S . One , f <EOL> dom = f . get_domain ( ) <EOL> if dom . has_assoc_Ring : <EOL> dom = f . rep . dom . get_ring ( ) <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> coeff , result = f . rep . clear_denoms ( ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> coeff , f = dom . to_sympy ( coeff ) , f . per ( result ) <EOL> if not convert or not dom . has_assoc_Ring : <EOL> return coeff , f <EOL> else : <EOL> return coeff , f . to_ring ( ) <EOL> def rat_clear_denoms ( self , g ) : <EOL> """<STR_LIT>""" <EOL> f = self <EOL> dom , per , f , g = f . _unify ( g ) <EOL> f = per ( f ) <EOL> g = per ( g ) <EOL> if not ( dom . has_Field and dom . has_assoc_Ring ) : <EOL> return f , g <EOL> a , f = f . clear_denoms ( convert = True ) <EOL> b , g = g . clear_denoms ( convert = True ) <EOL> f = f . mul_ground ( b ) <EOL> g = g . mul_ground ( a ) <EOL> return f , g <EOL> def integrate ( self , * specs , ** args ) : <EOL> """<STR_LIT>""" <EOL> f = self <EOL> if args . get ( '<STR_LIT>' , True ) and f . rep . dom . has_Ring : <EOL> f = f . to_field ( ) <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> if not specs : <EOL> return f . per ( f . rep . integrate ( m = <NUM_LIT:1> ) ) <EOL> rep = f . rep <EOL> for spec in specs : <EOL> if type ( spec ) is tuple : <EOL> gen , m = spec <EOL> else : <EOL> gen , m = spec , <NUM_LIT:1> <EOL> rep = rep . integrate ( int ( m ) , f . _gen_to_level ( gen ) ) <EOL> return f . per ( rep ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> def diff ( f , * specs , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not kwargs . get ( '<STR_LIT>' , True ) : <EOL> return Derivative ( f , * specs , ** kwargs ) <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> if not specs : <EOL> return f . per ( f . rep . diff ( m = <NUM_LIT:1> ) ) <EOL> rep = f . rep <EOL> for spec in specs : <EOL> if type ( spec ) is tuple : <EOL> gen , m = spec <EOL> else : <EOL> gen , m = spec , <NUM_LIT:1> <EOL> rep = rep . diff ( int ( m ) , f . _gen_to_level ( gen ) ) <EOL> return f . per ( rep ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> _eval_derivative = diff <EOL> _eval_diff = diff <EOL> def eval ( self , x , a = None , auto = True ) : <EOL> """<STR_LIT>""" <EOL> f = self <EOL> if a is None : <EOL> if isinstance ( x , dict ) : <EOL> mapping = x <EOL> for gen , value in mapping . items ( ) : <EOL> f = f . eval ( gen , value ) <EOL> return f <EOL> elif isinstance ( x , ( tuple , list ) ) : <EOL> values = x <EOL> if len ( values ) > len ( f . gens ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> for gen , value in zip ( f . gens , values ) : <EOL> f = f . eval ( gen , value ) <EOL> return f <EOL> else : <EOL> j , a = <NUM_LIT:0> , x <EOL> else : <EOL> j = f . _gen_to_level ( x ) <EOL> if not hasattr ( f . rep , '<STR_LIT>' ) : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> try : <EOL> result = f . rep . eval ( a , j ) <EOL> except CoercionFailed : <EOL> if not auto : <EOL> raise DomainError ( "<STR_LIT>" % ( a , f . rep . dom ) ) <EOL> else : <EOL> a_domain , [ a ] = construct_domain ( [ a ] ) <EOL> new_domain = f . get_domain ( ) . unify_with_symbols ( a_domain , f . gens ) <EOL> f = f . set_domain ( new_domain ) <EOL> a = new_domain . convert ( a , a_domain ) <EOL> result = f . rep . eval ( a , j ) <EOL> return f . per ( result , remove = j ) <EOL> def __call__ ( f , * values ) : <EOL> """<STR_LIT>""" <EOL> return f . eval ( values ) <EOL> def half_gcdex ( f , g , auto = True ) : <EOL> """<STR_LIT>""" <EOL> dom , per , F , G = f . _unify ( g ) <EOL> if auto and dom . has_Ring : <EOL> F , G = F . to_field ( ) , G . to_field ( ) <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> s , h = F . half_gcdex ( G ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return per ( s ) , per ( h ) <EOL> def gcdex ( f , g , auto = True ) : <EOL> """<STR_LIT>""" <EOL> dom , per , F , G = f . _unify ( g ) <EOL> if auto and dom . has_Ring : <EOL> F , G = F . to_field ( ) , G . to_field ( ) <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> s , t , h = F . gcdex ( G ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return per ( s ) , per ( t ) , per ( h ) <EOL> def invert ( f , g , auto = True ) : <EOL> """<STR_LIT>""" <EOL> dom , per , F , G = f . _unify ( g ) <EOL> if auto and dom . has_Ring : <EOL> F , G = F . to_field ( ) , G . to_field ( ) <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = F . invert ( G ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return per ( result ) <EOL> def revert ( f , n ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = f . rep . revert ( int ( n ) ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return f . per ( result ) <EOL> def subresultants ( f , g ) : <EOL> """<STR_LIT>""" <EOL> _ , per , F , G = f . _unify ( g ) <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = F . subresultants ( G ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return list ( map ( per , result ) ) <EOL> def resultant ( f , g , includePRS = False ) : <EOL> """<STR_LIT>""" <EOL> _ , per , F , G = f . _unify ( g ) <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> if includePRS : <EOL> result , R = F . resultant ( G , includePRS = includePRS ) <EOL> else : <EOL> result = F . resultant ( G ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> if includePRS : <EOL> return ( per ( result , remove = <NUM_LIT:0> ) , list ( map ( per , R ) ) ) <EOL> return per ( result , remove = <NUM_LIT:0> ) <EOL> def discriminant ( f ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = f . rep . discriminant ( ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return f . per ( result , remove = <NUM_LIT:0> ) <EOL> def dispersionset ( f , g = None ) : <EOL> r"""<STR_LIT>""" <EOL> from sympy . polys . dispersion import dispersionset <EOL> return dispersionset ( f , g ) <EOL> def dispersion ( f , g = None ) : <EOL> r"""<STR_LIT>""" <EOL> from sympy . polys . dispersion import dispersion <EOL> return dispersion ( f , g ) <EOL> def cofactors ( f , g ) : <EOL> """<STR_LIT>""" <EOL> _ , per , F , G = f . _unify ( g ) <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> h , cff , cfg = F . cofactors ( G ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return per ( h ) , per ( cff ) , per ( cfg ) <EOL> def gcd ( f , g ) : <EOL> """<STR_LIT>""" <EOL> _ , per , F , G = f . _unify ( g ) <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = F . gcd ( G ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return per ( result ) <EOL> def lcm ( f , g ) : <EOL> """<STR_LIT>""" <EOL> _ , per , F , G = f . _unify ( g ) <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = F . lcm ( G ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return per ( result ) <EOL> def trunc ( f , p ) : <EOL> """<STR_LIT>""" <EOL> p = f . rep . dom . convert ( p ) <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = f . rep . trunc ( p ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return f . per ( result ) <EOL> def monic ( self , auto = True ) : <EOL> """<STR_LIT>""" <EOL> f = self <EOL> if auto and f . rep . dom . has_Ring : <EOL> f = f . to_field ( ) <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = f . rep . monic ( ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return f . per ( result ) <EOL> def content ( f ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f . rep , '<STR_LIT:content>' ) : <EOL> result = f . rep . content ( ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT:content>' ) <EOL> return f . rep . dom . to_sympy ( result ) <EOL> def primitive ( f ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> cont , result = f . rep . primitive ( ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return f . rep . dom . to_sympy ( cont ) , f . per ( result ) <EOL> def compose ( f , g ) : <EOL> """<STR_LIT>""" <EOL> _ , per , F , G = f . _unify ( g ) <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = F . compose ( G ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return per ( result ) <EOL> def decompose ( f ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = f . rep . decompose ( ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return list ( map ( f . per , result ) ) <EOL> def shift ( f , a ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = f . rep . shift ( a ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return f . per ( result ) <EOL> def sturm ( self , auto = True ) : <EOL> """<STR_LIT>""" <EOL> f = self <EOL> if auto and f . rep . dom . has_Ring : <EOL> f = f . to_field ( ) <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = f . rep . sturm ( ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return list ( map ( f . per , result ) ) <EOL> def gff_list ( f ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = f . rep . gff_list ( ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return [ ( f . per ( g ) , k ) for g , k in result ] <EOL> def sqf_norm ( f ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> s , g , r = f . rep . sqf_norm ( ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return s , f . per ( g ) , f . per ( r ) <EOL> def sqf_part ( f ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = f . rep . sqf_part ( ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return f . per ( result ) <EOL> def sqf_list ( f , all = False ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> coeff , factors = f . rep . sqf_list ( all ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return f . rep . dom . to_sympy ( coeff ) , [ ( f . per ( g ) , k ) for g , k in factors ] <EOL> def sqf_list_include ( f , all = False ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> factors = f . rep . sqf_list_include ( all ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return [ ( f . per ( g ) , k ) for g , k in factors ] <EOL> def factor_list ( f ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> try : <EOL> coeff , factors = f . rep . factor_list ( ) <EOL> except DomainError : <EOL> return S . One , [ ( f , <NUM_LIT:1> ) ] <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return f . rep . dom . to_sympy ( coeff ) , [ ( f . per ( g ) , k ) for g , k in factors ] <EOL> def factor_list_include ( f ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> try : <EOL> factors = f . rep . factor_list_include ( ) <EOL> except DomainError : <EOL> return [ ( f , <NUM_LIT:1> ) ] <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return [ ( f . per ( g ) , k ) for g , k in factors ] <EOL> def intervals ( f , all = False , eps = None , inf = None , sup = None , fast = False , sqf = False ) : <EOL> """<STR_LIT>""" <EOL> if eps is not None : <EOL> eps = QQ . convert ( eps ) <EOL> if eps <= <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if inf is not None : <EOL> inf = QQ . convert ( inf ) <EOL> if sup is not None : <EOL> sup = QQ . convert ( sup ) <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> result = f . rep . intervals ( <EOL> all = all , eps = eps , inf = inf , sup = sup , fast = fast , sqf = sqf ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> if sqf : <EOL> def _real ( interval ) : <EOL> s , t = interval <EOL> return ( QQ . to_sympy ( s ) , QQ . to_sympy ( t ) ) <EOL> if not all : <EOL> return list ( map ( _real , result ) ) <EOL> def _complex ( rectangle ) : <EOL> ( u , v ) , ( s , t ) = rectangle <EOL> return ( QQ . to_sympy ( u ) + I * QQ . to_sympy ( v ) , <EOL> QQ . to_sympy ( s ) + I * QQ . to_sympy ( t ) ) <EOL> real_part , complex_part = result <EOL> return list ( map ( _real , real_part ) ) , list ( map ( _complex , complex_part ) ) <EOL> else : <EOL> def _real ( interval ) : <EOL> ( s , t ) , k = interval <EOL> return ( ( QQ . to_sympy ( s ) , QQ . to_sympy ( t ) ) , k ) <EOL> if not all : <EOL> return list ( map ( _real , result ) ) <EOL> def _complex ( rectangle ) : <EOL> ( ( u , v ) , ( s , t ) ) , k = rectangle <EOL> return ( ( QQ . to_sympy ( u ) + I * QQ . to_sympy ( v ) , <EOL> QQ . to_sympy ( s ) + I * QQ . to_sympy ( t ) ) , k ) <EOL> real_part , complex_part = result <EOL> return list ( map ( _real , real_part ) ) , list ( map ( _complex , complex_part ) ) <EOL> def refine_root ( f , s , t , eps = None , steps = None , fast = False , check_sqf = False ) : <EOL> """<STR_LIT>""" <EOL> if check_sqf and not f . is_sqf : <EOL> raise PolynomialError ( "<STR_LIT>" ) <EOL> s , t = QQ . convert ( s ) , QQ . convert ( t ) <EOL> if eps is not None : <EOL> eps = QQ . convert ( eps ) <EOL> if eps <= <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if steps is not None : <EOL> steps = int ( steps ) <EOL> elif eps is None : <EOL> steps = <NUM_LIT:1> <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> S , T = f . rep . refine_root ( s , t , eps = eps , steps = steps , fast = fast ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return QQ . to_sympy ( S ) , QQ . to_sympy ( T ) <EOL> def count_roots ( f , inf = None , sup = None ) : <EOL> """<STR_LIT>""" <EOL> inf_real , sup_real = True , True <EOL> if inf is not None : <EOL> inf = sympify ( inf ) <EOL> if inf is S . NegativeInfinity : <EOL> inf = None <EOL> else : <EOL> re , im = inf . as_real_imag ( ) <EOL> if not im : <EOL> inf = QQ . convert ( inf ) <EOL> else : <EOL> inf , inf_real = list ( map ( QQ . convert , ( re , im ) ) ) , False <EOL> if sup is not None : <EOL> sup = sympify ( sup ) <EOL> if sup is S . Infinity : <EOL> sup = None <EOL> else : <EOL> re , im = sup . as_real_imag ( ) <EOL> if not im : <EOL> sup = QQ . convert ( sup ) <EOL> else : <EOL> sup , sup_real = list ( map ( QQ . convert , ( re , im ) ) ) , False <EOL> if inf_real and sup_real : <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> count = f . rep . count_real_roots ( inf = inf , sup = sup ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> else : <EOL> if inf_real and inf is not None : <EOL> inf = ( inf , QQ . zero ) <EOL> if sup_real and sup is not None : <EOL> sup = ( sup , QQ . zero ) <EOL> if hasattr ( f . rep , '<STR_LIT>' ) : <EOL> count = f . rep . count_complex_roots ( inf = inf , sup = sup ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> return Integer ( count ) <EOL> def root ( f , index , radicals = True ) : <EOL> """<STR_LIT>""" <EOL> return sympy . polys . rootoftools . rootof ( f , index , radicals = radicals ) <EOL> def real_roots ( f , multiple = True , radicals = True ) : <EOL> """<STR_LIT>""" <EOL> reals = sympy . polys . rootoftools . CRootOf . real_roots ( f , radicals = radicals ) <EOL> if multiple : <EOL> return reals <EOL> else : <EOL> return group ( reals , multiple = False ) <EOL> def all_roots ( f , multiple = True , radicals = True ) : <EOL> """<STR_LIT>""" <EOL> roots = sympy . polys . rootoftools . CRootOf . all_roots ( f , radicals = radicals ) <EOL> if multiple : <EOL> return roots <EOL> else : <EOL> return group ( roots , multiple = False ) <EOL> def nroots ( f , n = <NUM_LIT:15> , maxsteps = <NUM_LIT:50> , cleanup = True ) : <EOL> """<STR_LIT>""" <EOL> if f . is_multivariate : <EOL> raise MultivariatePolynomialError ( <EOL> "<STR_LIT>" % f ) <EOL> if f . degree ( ) <= <NUM_LIT:0> : <EOL> return [ ] <EOL> if f . rep . dom is ZZ : <EOL> coeffs = [ int ( coeff ) for coeff in f . all_coeffs ( ) ] <EOL> elif f . rep . dom is QQ : <EOL> denoms = [ coeff . q for coeff in f . all_coeffs ( ) ] <EOL> from sympy . core . numbers import ilcm <EOL> fac = ilcm ( * denoms ) <EOL> coeffs = [ int ( coeff * fac ) for coeff in f . all_coeffs ( ) ] <EOL> else : <EOL> coeffs = [ coeff . evalf ( n = n ) . as_real_imag ( ) <EOL> for coeff in f . all_coeffs ( ) ] <EOL> try : <EOL> coeffs = [ mpmath . mpc ( * coeff ) for coeff in coeffs ] <EOL> except TypeError : <EOL> raise DomainError ( "<STR_LIT>" % f . rep . dom ) <EOL> dps = mpmath . mp . dps <EOL> mpmath . mp . dps = n <EOL> try : <EOL> roots = mpmath . polyroots ( coeffs , maxsteps = maxsteps , <EOL> cleanup = cleanup , error = False , extraprec = f . degree ( ) * <NUM_LIT:10> ) <EOL> roots = list ( map ( sympify , <EOL> sorted ( roots , key = lambda r : ( <NUM_LIT:1> if r . imag else <NUM_LIT:0> , r . real , r . imag ) ) ) ) <EOL> except NoConvergence : <EOL> raise NoConvergence ( <EOL> '<STR_LIT>' % ( <EOL> n , maxsteps ) ) <EOL> finally : <EOL> mpmath . mp . dps = dps <EOL> return roots <EOL> def ground_roots ( f ) : <EOL> """<STR_LIT>""" <EOL> if f . is_multivariate : <EOL> raise MultivariatePolynomialError ( <EOL> "<STR_LIT>" % f ) <EOL> roots = { } <EOL> for factor , k in f . factor_list ( ) [ <NUM_LIT:1> ] : <EOL> if factor . is_linear : <EOL> a , b = factor . all_coeffs ( ) <EOL> roots [ - b / a ] = k <EOL> return roots <EOL> def nth_power_roots_poly ( f , n ) : <EOL> """<STR_LIT>""" <EOL> if f . is_multivariate : <EOL> raise MultivariatePolynomialError ( <EOL> "<STR_LIT>" ) <EOL> N = sympify ( n ) <EOL> if N . is_Integer and N >= <NUM_LIT:1> : <EOL> n = int ( N ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % n ) <EOL> x = f . gen <EOL> t = Dummy ( '<STR_LIT:t>' ) <EOL> r = f . resultant ( f . __class__ . from_expr ( x ** n - t , x , t ) ) <EOL> return r . replace ( t , x ) <EOL> def cancel ( f , g , include = False ) : <EOL> """<STR_LIT>""" <EOL> dom , per , F , G = f . _unify ( g ) <EOL> if hasattr ( F , '<STR_LIT>' ) : <EOL> result = F . cancel ( G , include = include ) <EOL> else : <EOL> raise OperationNotSupported ( f , '<STR_LIT>' ) <EOL> if not include : <EOL> if dom . has_assoc_Ring : <EOL> dom = dom . get_ring ( ) <EOL> cp , cq , p , q = result <EOL> cp = dom . to_sympy ( cp ) <EOL> cq = dom . to_sympy ( cq ) <EOL> return cp / cq , per ( p ) , per ( q ) <EOL> else : <EOL> return tuple ( map ( per , result ) ) <EOL> @ property <EOL> def is_zero ( f ) : <EOL> """<STR_LIT>""" <EOL> return f . rep . is_zero <EOL> @ property <EOL> def is_one ( f ) : <EOL> """<STR_LIT>""" <EOL> return f . rep . is_one <EOL> @ property <EOL> def is_sqf ( f ) : <EOL> """<STR_LIT>""" <EOL> return f . rep . is_sqf <EOL> @ property <EOL> def is_monic ( f ) : <EOL> """<STR_LIT>""" <EOL> return f . rep . is_monic <EOL> @ property <EOL> def is_primitive ( f ) : <EOL> """<STR_LIT>""" <EOL> return f . rep . is_primitive <EOL> @ property <EOL> def is_ground ( f ) : <EOL> """<STR_LIT>""" <EOL> return f . rep . is_ground <EOL> @ property <EOL> def is_linear ( f ) : <EOL> """<STR_LIT>""" <EOL> return f . rep . is_linear <EOL> @ property <EOL> def is_quadratic ( f ) : <EOL> """<STR_LIT>""" <EOL> return f . rep . is_quadratic <EOL> @ property <EOL> def is_monomial ( f ) : <EOL> """<STR_LIT>""" <EOL> return f . rep . is_monomial <EOL> @ property <EOL> def is_homogeneous ( f ) : <EOL> """<STR_LIT>""" <EOL> return f . rep . is_homogeneous <EOL> @ property <EOL> def is_irreducible ( f ) : <EOL> """<STR_LIT>""" <EOL> return f . rep . is_irreducible <EOL> @ property <EOL> def is_univariate ( f ) : <EOL> """<STR_LIT>""" <EOL> return len ( f . gens ) == <NUM_LIT:1> <EOL> @ property <EOL> def is_multivariate ( f ) : <EOL> """<STR_LIT>""" <EOL> return len ( f . gens ) != <NUM_LIT:1> <EOL> @ property <EOL> def is_cyclotomic ( f ) : <EOL> """<STR_LIT>""" <EOL> return f . rep . is_cyclotomic <EOL> def __abs__ ( f ) : <EOL> return f . abs ( ) <EOL> def __neg__ ( f ) : <EOL> return f . neg ( ) <EOL> @ _sympifyit ( '<STR_LIT:g>' , NotImplemented ) <EOL> def __add__ ( f , g ) : <EOL> if not g . is_Poly : <EOL> try : <EOL> g = f . __class__ ( g , * f . gens ) <EOL> except PolynomialError : <EOL> return f . as_expr ( ) + g <EOL> return f . add ( g ) <EOL> @ _sympifyit ( '<STR_LIT:g>' , NotImplemented ) <EOL> def __radd__ ( f , g ) : <EOL> if not g . is_Poly : <EOL> try : <EOL> g = f . __class__ ( g , * f . gens ) <EOL> except PolynomialError : <EOL> return g + f . as_expr ( ) <EOL> return g . add ( f ) <EOL> @ _sympifyit ( '<STR_LIT:g>' , NotImplemented ) <EOL> def __sub__ ( f , g ) : <EOL> if not g . is_Poly : <EOL> try : <EOL> g = f . __class__ ( g , * f . gens ) <EOL> except PolynomialError : <EOL> return f . as_expr ( ) - g <EOL> return f . sub ( g ) <EOL> @ _sympifyit ( '<STR_LIT:g>' , NotImplemented ) <EOL> def __rsub__ ( f , g ) : <EOL> if not g . is_Poly : <EOL> try : <EOL> g = f . __class__ ( g , * f . gens ) <EOL> except PolynomialError : <EOL> return g - f . as_expr ( ) <EOL> return g . sub ( f ) <EOL> @ _sympifyit ( '<STR_LIT:g>' , NotImplemented ) <EOL> def __mul__ ( f , g ) : <EOL> if not g . is_Poly : <EOL> try : <EOL> g = f . __class__ ( g , * f . gens ) <EOL> except PolynomialError : <EOL> return f . as_expr ( ) * g <EOL> return f . mul ( g ) <EOL> @ _sympifyit ( '<STR_LIT:g>' , NotImplemented ) <EOL> def __rmul__ ( f , g ) : <EOL> if not g . is_Poly : <EOL> try : <EOL> g = f . __class__ ( g , * f . gens ) <EOL> except PolynomialError : <EOL> return g * f . as_expr ( ) <EOL> return g . mul ( f ) <EOL> @ _sympifyit ( '<STR_LIT:n>' , NotImplemented ) <EOL> def __pow__ ( f , n ) : <EOL> if n . is_Integer and n >= <NUM_LIT:0> : <EOL> return f . pow ( n ) <EOL> else : <EOL> return f . as_expr ( ) ** n <EOL> @ _sympifyit ( '<STR_LIT:g>' , NotImplemented ) <EOL> def __divmod__ ( f , g ) : <EOL> if not g . is_Poly : <EOL> g = f . __class__ ( g , * f . gens ) <EOL> return f . div ( g ) <EOL> @ _sympifyit ( '<STR_LIT:g>' , NotImplemented ) <EOL> def __rdivmod__ ( f , g ) : <EOL> if not g . is_Poly : <EOL> g = f . __class__ ( g , * f . gens ) <EOL> return g . div ( f ) <EOL> @ _sympifyit ( '<STR_LIT:g>' , NotImplemented ) <EOL> def __mod__ ( f , g ) : <EOL> if not g . is_Poly : <EOL> g = f . __class__ ( g , * f . gens ) <EOL> return f . rem ( g ) <EOL> @ _sympifyit ( '<STR_LIT:g>' , NotImplemented ) <EOL> def __rmod__ ( f , g ) : <EOL> if not g . is_Poly : <EOL> g = f . __class__ ( g , * f . gens ) <EOL> return g . rem ( f ) <EOL> @ _sympifyit ( '<STR_LIT:g>' , NotImplemented ) <EOL> def __floordiv__ ( f , g ) : <EOL> if not g . is_Poly : <EOL> g = f . __class__ ( g , * f . gens ) <EOL> return f . quo ( g ) <EOL> @ _sympifyit ( '<STR_LIT:g>' , NotImplemented ) <EOL> def __rfloordiv__ ( f , g ) : <EOL> if not g . is_Poly : <EOL> g = f . __class__ ( g , * f . gens ) <EOL> return g . quo ( f ) <EOL> @ _sympifyit ( '<STR_LIT:g>' , NotImplemented ) <EOL> def __div__ ( f , g ) : <EOL> return f . as_expr ( ) / g . as_expr ( ) <EOL> @ _sympifyit ( '<STR_LIT:g>' , NotImplemented ) <EOL> def __rdiv__ ( f , g ) : <EOL> return g . as_expr ( ) / f . as_expr ( ) <EOL> __truediv__ = __div__ <EOL> __rtruediv__ = __rdiv__ <EOL> @ _sympifyit ( '<STR_LIT>' , NotImplemented ) <EOL> def __eq__ ( self , other ) : <EOL> f , g = self , other <EOL> if not g . is_Poly : <EOL> try : <EOL> g = f . __class__ ( g , f . gens , domain = f . get_domain ( ) ) <EOL> except ( PolynomialError , DomainError , CoercionFailed ) : <EOL> return False <EOL> if f . gens != g . gens : <EOL> return False <EOL> if f . rep . dom != g . rep . dom : <EOL> try : <EOL> dom = f . rep . dom . unify ( g . rep . dom , f . gens ) <EOL> except UnificationFailed : <EOL> return False <EOL> f = f . set_domain ( dom ) <EOL> g = g . set_domain ( dom ) <EOL> return f . rep == g . rep <EOL> @ _sympifyit ( '<STR_LIT:g>' , NotImplemented ) <EOL> def __ne__ ( f , g ) : <EOL> return not f . __eq__ ( g ) <EOL> def __nonzero__ ( f ) : <EOL> return not f . is_zero <EOL> __bool__ = __nonzero__ <EOL> def eq ( f , g , strict = False ) : <EOL> if not strict : <EOL> return f . __eq__ ( g ) <EOL> else : <EOL> return f . _strict_eq ( sympify ( g ) ) <EOL> def ne ( f , g , strict = False ) : <EOL> return not f . eq ( g , strict = strict ) <EOL> def _strict_eq ( f , g ) : <EOL> return isinstance ( g , f . __class__ ) and f . gens == g . gens and f . rep . eq ( g . rep , strict = True ) <EOL> @ public <EOL> class PurePoly ( Poly ) : <EOL> """<STR_LIT>""" <EOL> def _hashable_content ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( self . rep , ) <EOL> def __hash__ ( self ) : <EOL> return super ( PurePoly , self ) . __hash__ ( ) <EOL> @ property <EOL> def free_symbols ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . free_symbols_in_domain <EOL> @ _sympifyit ( '<STR_LIT>' , NotImplemented ) <EOL> def __eq__ ( self , other ) : <EOL> f , g = self , other <EOL> if not g . is_Poly : <EOL> try : <EOL> g = f . __class__ ( g , f . gens , domain = f . get_domain ( ) ) <EOL> except ( PolynomialError , DomainError , CoercionFailed ) : <EOL> return False <EOL> if len ( f . gens ) != len ( g . gens ) : <EOL> return False <EOL> if f . rep . dom != g . rep . dom : <EOL> try : <EOL> dom = f . rep . dom . unify ( g . rep . dom , f . gens ) <EOL> except UnificationFailed : <EOL> return False <EOL> f = f . set_domain ( dom ) <EOL> g = g . set_domain ( dom ) <EOL> return f . rep == g . rep <EOL> def _strict_eq ( f , g ) : <EOL> return isinstance ( g , f . __class__ ) and f . rep . eq ( g . rep , strict = True ) <EOL> def _unify ( f , g ) : <EOL> g = sympify ( g ) <EOL> if not g . is_Poly : <EOL> try : <EOL> return f . rep . dom , f . per , f . rep , f . rep . per ( f . rep . dom . from_sympy ( g ) ) <EOL> except CoercionFailed : <EOL> raise UnificationFailed ( "<STR_LIT>" % ( f , g ) ) <EOL> if len ( f . gens ) != len ( g . gens ) : <EOL> raise UnificationFailed ( "<STR_LIT>" % ( f , g ) ) <EOL> if not ( isinstance ( f . rep , DMP ) and isinstance ( g . rep , DMP ) ) : <EOL> raise UnificationFailed ( "<STR_LIT>" % ( f , g ) ) <EOL> cls = f . __class__ <EOL> gens = f . gens <EOL> dom = f . rep . dom . unify ( g . rep . dom , gens ) <EOL> F = f . rep . convert ( dom ) <EOL> G = g . rep . convert ( dom ) <EOL> def per ( rep , dom = dom , gens = gens , remove = None ) : <EOL> if remove is not None : <EOL> gens = gens [ : remove ] + gens [ remove + <NUM_LIT:1> : ] <EOL> if not gens : <EOL> return dom . to_sympy ( rep ) <EOL> return cls . new ( rep , * gens ) <EOL> return dom , per , F , G <EOL> @ public <EOL> def poly_from_expr ( expr , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> opt = options . build_options ( gens , args ) <EOL> return _poly_from_expr ( expr , opt ) <EOL> def _poly_from_expr ( expr , opt ) : <EOL> """<STR_LIT>""" <EOL> orig , expr = expr , sympify ( expr ) <EOL> if not isinstance ( expr , Basic ) : <EOL> raise PolificationFailed ( opt , orig , expr ) <EOL> elif expr . is_Poly : <EOL> poly = expr . __class__ . _from_poly ( expr , opt ) <EOL> opt . gens = poly . gens <EOL> opt . domain = poly . domain <EOL> if opt . polys is None : <EOL> opt . polys = True <EOL> return poly , opt <EOL> elif opt . expand : <EOL> expr = expr . expand ( ) <EOL> try : <EOL> rep , opt = _dict_from_expr ( expr , opt ) <EOL> except GeneratorsNeeded : <EOL> raise PolificationFailed ( opt , orig , expr ) <EOL> monoms , coeffs = list ( zip ( * list ( rep . items ( ) ) ) ) <EOL> domain = opt . domain <EOL> if domain is None : <EOL> opt . domain , coeffs = construct_domain ( coeffs , opt = opt ) <EOL> else : <EOL> coeffs = list ( map ( domain . from_sympy , coeffs ) ) <EOL> rep = dict ( list ( zip ( monoms , coeffs ) ) ) <EOL> poly = Poly . _from_dict ( rep , opt ) <EOL> if opt . polys is None : <EOL> opt . polys = False <EOL> return poly , opt <EOL> @ public <EOL> def parallel_poly_from_expr ( exprs , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> opt = options . build_options ( gens , args ) <EOL> return _parallel_poly_from_expr ( exprs , opt ) <EOL> def _parallel_poly_from_expr ( exprs , opt ) : <EOL> """<STR_LIT>""" <EOL> from sympy . functions . elementary . piecewise import Piecewise <EOL> if len ( exprs ) == <NUM_LIT:2> : <EOL> f , g = exprs <EOL> if isinstance ( f , Poly ) and isinstance ( g , Poly ) : <EOL> f = f . __class__ . _from_poly ( f , opt ) <EOL> g = g . __class__ . _from_poly ( g , opt ) <EOL> f , g = f . unify ( g ) <EOL> opt . gens = f . gens <EOL> opt . domain = f . domain <EOL> if opt . polys is None : <EOL> opt . polys = True <EOL> return [ f , g ] , opt <EOL> origs , exprs = list ( exprs ) , [ ] <EOL> _exprs , _polys = [ ] , [ ] <EOL> failed = False <EOL> for i , expr in enumerate ( origs ) : <EOL> expr = sympify ( expr ) <EOL> if isinstance ( expr , Basic ) : <EOL> if expr . is_Poly : <EOL> _polys . append ( i ) <EOL> else : <EOL> _exprs . append ( i ) <EOL> if opt . expand : <EOL> expr = expr . expand ( ) <EOL> else : <EOL> failed = True <EOL> exprs . append ( expr ) <EOL> if failed : <EOL> raise PolificationFailed ( opt , origs , exprs , True ) <EOL> if _polys : <EOL> for i in _polys : <EOL> exprs [ i ] = exprs [ i ] . as_expr ( ) <EOL> try : <EOL> reps , opt = _parallel_dict_from_expr ( exprs , opt ) <EOL> except GeneratorsNeeded : <EOL> raise PolificationFailed ( opt , origs , exprs , True ) <EOL> for k in opt . gens : <EOL> if isinstance ( k , Piecewise ) : <EOL> raise PolynomialError ( "<STR_LIT>" ) <EOL> coeffs_list , lengths = [ ] , [ ] <EOL> all_monoms = [ ] <EOL> all_coeffs = [ ] <EOL> for rep in reps : <EOL> monoms , coeffs = list ( zip ( * list ( rep . items ( ) ) ) ) <EOL> coeffs_list . extend ( coeffs ) <EOL> all_monoms . append ( monoms ) <EOL> lengths . append ( len ( coeffs ) ) <EOL> domain = opt . domain <EOL> if domain is None : <EOL> opt . domain , coeffs_list = construct_domain ( coeffs_list , opt = opt ) <EOL> else : <EOL> coeffs_list = list ( map ( domain . from_sympy , coeffs_list ) ) <EOL> for k in lengths : <EOL> all_coeffs . append ( coeffs_list [ : k ] ) <EOL> coeffs_list = coeffs_list [ k : ] <EOL> polys = [ ] <EOL> for monoms , coeffs in zip ( all_monoms , all_coeffs ) : <EOL> rep = dict ( list ( zip ( monoms , coeffs ) ) ) <EOL> poly = Poly . _from_dict ( rep , opt ) <EOL> polys . append ( poly ) <EOL> if opt . polys is None : <EOL> opt . polys = bool ( _polys ) <EOL> return polys , opt <EOL> def _update_args ( args , key , value ) : <EOL> """<STR_LIT>""" <EOL> args = dict ( args ) <EOL> if key not in args : <EOL> args [ key ] = value <EOL> return args <EOL> @ public <EOL> def degree ( f , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> try : <EOL> F , opt = poly_from_expr ( f , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> raise ComputationFailed ( '<STR_LIT>' , <NUM_LIT:1> , exc ) <EOL> return sympify ( F . degree ( opt . gen ) ) <EOL> @ public <EOL> def degree_list ( f , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ '<STR_LIT>' ] ) <EOL> try : <EOL> F , opt = poly_from_expr ( f , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> raise ComputationFailed ( '<STR_LIT>' , <NUM_LIT:1> , exc ) <EOL> degrees = F . degree_list ( ) <EOL> return tuple ( map ( Integer , degrees ) ) <EOL> @ public <EOL> def LC ( f , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ '<STR_LIT>' ] ) <EOL> try : <EOL> F , opt = poly_from_expr ( f , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> raise ComputationFailed ( '<STR_LIT>' , <NUM_LIT:1> , exc ) <EOL> return F . LC ( order = opt . order ) <EOL> @ public <EOL> def LM ( f , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ '<STR_LIT>' ] ) <EOL> try : <EOL> F , opt = poly_from_expr ( f , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> raise ComputationFailed ( '<STR_LIT>' , <NUM_LIT:1> , exc ) <EOL> monom = F . LM ( order = opt . order ) <EOL> return monom . as_expr ( ) <EOL> @ public <EOL> def LT ( f , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ '<STR_LIT>' ] ) <EOL> try : <EOL> F , opt = poly_from_expr ( f , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> raise ComputationFailed ( '<STR_LIT>' , <NUM_LIT:1> , exc ) <EOL> monom , coeff = F . LT ( order = opt . order ) <EOL> return coeff * monom . as_expr ( ) <EOL> @ public <EOL> def pdiv ( f , g , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ '<STR_LIT>' ] ) <EOL> try : <EOL> ( F , G ) , opt = parallel_poly_from_expr ( ( f , g ) , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> raise ComputationFailed ( '<STR_LIT>' , <NUM_LIT:2> , exc ) <EOL> q , r = F . pdiv ( G ) <EOL> if not opt . polys : <EOL> return q . as_expr ( ) , r . as_expr ( ) <EOL> else : <EOL> return q , r <EOL> @ public <EOL> def prem ( f , g , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ '<STR_LIT>' ] ) <EOL> try : <EOL> ( F , G ) , opt = parallel_poly_from_expr ( ( f , g ) , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> raise ComputationFailed ( '<STR_LIT>' , <NUM_LIT:2> , exc ) <EOL> r = F . prem ( G ) <EOL> if not opt . polys : <EOL> return r . as_expr ( ) <EOL> else : <EOL> return r <EOL> @ public <EOL> def pquo ( f , g , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ '<STR_LIT>' ] ) <EOL> try : <EOL> ( F , G ) , opt = parallel_poly_from_expr ( ( f , g ) , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> raise ComputationFailed ( '<STR_LIT>' , <NUM_LIT:2> , exc ) <EOL> try : <EOL> q = F . pquo ( G ) <EOL> except ExactQuotientFailed : <EOL> raise ExactQuotientFailed ( f , g ) <EOL> if not opt . polys : <EOL> return q . as_expr ( ) <EOL> else : <EOL> return q <EOL> @ public <EOL> def pexquo ( f , g , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ '<STR_LIT>' ] ) <EOL> try : <EOL> ( F , G ) , opt = parallel_poly_from_expr ( ( f , g ) , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> raise ComputationFailed ( '<STR_LIT>' , <NUM_LIT:2> , exc ) <EOL> q = F . pexquo ( G ) <EOL> if not opt . polys : <EOL> return q . as_expr ( ) <EOL> else : <EOL> return q <EOL> @ public <EOL> def div ( f , g , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> try : <EOL> ( F , G ) , opt = parallel_poly_from_expr ( ( f , g ) , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> raise ComputationFailed ( '<STR_LIT>' , <NUM_LIT:2> , exc ) <EOL> q , r = F . div ( G , auto = opt . auto ) <EOL> if not opt . polys : <EOL> return q . as_expr ( ) , r . as_expr ( ) <EOL> else : <EOL> return q , r <EOL> @ public <EOL> def rem ( f , g , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> try : <EOL> ( F , G ) , opt = parallel_poly_from_expr ( ( f , g ) , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> raise ComputationFailed ( '<STR_LIT>' , <NUM_LIT:2> , exc ) <EOL> r = F . rem ( G , auto = opt . auto ) <EOL> if not opt . polys : <EOL> return r . as_expr ( ) <EOL> else : <EOL> return r <EOL> @ public <EOL> def quo ( f , g , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> try : <EOL> ( F , G ) , opt = parallel_poly_from_expr ( ( f , g ) , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> raise ComputationFailed ( '<STR_LIT>' , <NUM_LIT:2> , exc ) <EOL> q = F . quo ( G , auto = opt . auto ) <EOL> if not opt . polys : <EOL> return q . as_expr ( ) <EOL> else : <EOL> return q <EOL> @ public <EOL> def exquo ( f , g , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> try : <EOL> ( F , G ) , opt = parallel_poly_from_expr ( ( f , g ) , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> raise ComputationFailed ( '<STR_LIT>' , <NUM_LIT:2> , exc ) <EOL> q = F . exquo ( G , auto = opt . auto ) <EOL> if not opt . polys : <EOL> return q . as_expr ( ) <EOL> else : <EOL> return q <EOL> @ public <EOL> def half_gcdex ( f , g , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> try : <EOL> ( F , G ) , opt = parallel_poly_from_expr ( ( f , g ) , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> domain , ( a , b ) = construct_domain ( exc . exprs ) <EOL> try : <EOL> s , h = domain . half_gcdex ( a , b ) <EOL> except NotImplementedError : <EOL> raise ComputationFailed ( '<STR_LIT>' , <NUM_LIT:2> , exc ) <EOL> else : <EOL> return domain . to_sympy ( s ) , domain . to_sympy ( h ) <EOL> s , h = F . half_gcdex ( G , auto = opt . auto ) <EOL> if not opt . polys : <EOL> return s . as_expr ( ) , h . as_expr ( ) <EOL> else : <EOL> return s , h <EOL> @ public <EOL> def gcdex ( f , g , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> try : <EOL> ( F , G ) , opt = parallel_poly_from_expr ( ( f , g ) , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> domain , ( a , b ) = construct_domain ( exc . exprs ) <EOL> try : <EOL> s , t , h = domain . gcdex ( a , b ) <EOL> except NotImplementedError : <EOL> raise ComputationFailed ( '<STR_LIT>' , <NUM_LIT:2> , exc ) <EOL> else : <EOL> return domain . to_sympy ( s ) , domain . to_sympy ( t ) , domain . to_sympy ( h ) <EOL> s , t , h = F . gcdex ( G , auto = opt . auto ) <EOL> if not opt . polys : <EOL> return s . as_expr ( ) , t . as_expr ( ) , h . as_expr ( ) <EOL> else : <EOL> return s , t , h <EOL> @ public <EOL> def invert ( f , g , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> try : <EOL> ( F , G ) , opt = parallel_poly_from_expr ( ( f , g ) , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> domain , ( a , b ) = construct_domain ( exc . exprs ) <EOL> try : <EOL> return domain . to_sympy ( domain . invert ( a , b ) ) <EOL> except NotImplementedError : <EOL> raise ComputationFailed ( '<STR_LIT>' , <NUM_LIT:2> , exc ) <EOL> h = F . invert ( G , auto = opt . auto ) <EOL> if not opt . polys : <EOL> return h . as_expr ( ) <EOL> else : <EOL> return h <EOL> @ public <EOL> def subresultants ( f , g , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ '<STR_LIT>' ] ) <EOL> try : <EOL> ( F , G ) , opt = parallel_poly_from_expr ( ( f , g ) , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> raise ComputationFailed ( '<STR_LIT>' , <NUM_LIT:2> , exc ) <EOL> result = F . subresultants ( G ) <EOL> if not opt . polys : <EOL> return [ r . as_expr ( ) for r in result ] <EOL> else : <EOL> return result <EOL> @ public <EOL> def resultant ( f , g , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> includePRS = args . pop ( '<STR_LIT>' , False ) <EOL> options . allowed_flags ( args , [ '<STR_LIT>' ] ) <EOL> try : <EOL> ( F , G ) , opt = parallel_poly_from_expr ( ( f , g ) , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> raise ComputationFailed ( '<STR_LIT>' , <NUM_LIT:2> , exc ) <EOL> if includePRS : <EOL> result , R = F . resultant ( G , includePRS = includePRS ) <EOL> else : <EOL> result = F . resultant ( G ) <EOL> if not opt . polys : <EOL> if includePRS : <EOL> return result . as_expr ( ) , [ r . as_expr ( ) for r in R ] <EOL> return result . as_expr ( ) <EOL> else : <EOL> if includePRS : <EOL> return result , R <EOL> return result <EOL> @ public <EOL> def discriminant ( f , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ '<STR_LIT>' ] ) <EOL> try : <EOL> F , opt = poly_from_expr ( f , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> raise ComputationFailed ( '<STR_LIT>' , <NUM_LIT:1> , exc ) <EOL> result = F . discriminant ( ) <EOL> if not opt . polys : <EOL> return result . as_expr ( ) <EOL> else : <EOL> return result <EOL> @ public <EOL> def cofactors ( f , g , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ '<STR_LIT>' ] ) <EOL> try : <EOL> ( F , G ) , opt = parallel_poly_from_expr ( ( f , g ) , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> domain , ( a , b ) = construct_domain ( exc . exprs ) <EOL> try : <EOL> h , cff , cfg = domain . cofactors ( a , b ) <EOL> except NotImplementedError : <EOL> raise ComputationFailed ( '<STR_LIT>' , <NUM_LIT:2> , exc ) <EOL> else : <EOL> return domain . to_sympy ( h ) , domain . to_sympy ( cff ) , domain . to_sympy ( cfg ) <EOL> h , cff , cfg = F . cofactors ( G ) <EOL> if not opt . polys : <EOL> return h . as_expr ( ) , cff . as_expr ( ) , cfg . as_expr ( ) <EOL> else : <EOL> return h , cff , cfg <EOL> @ public <EOL> def gcd_list ( seq , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> seq = sympify ( seq ) <EOL> def try_non_polynomial_gcd ( seq ) : <EOL> if not gens and not args : <EOL> domain , numbers = construct_domain ( seq ) <EOL> if not numbers : <EOL> return domain . zero <EOL> elif domain . is_Numerical : <EOL> result , numbers = numbers [ <NUM_LIT:0> ] , numbers [ <NUM_LIT:1> : ] <EOL> for number in numbers : <EOL> result = domain . gcd ( result , number ) <EOL> if domain . is_one ( result ) : <EOL> break <EOL> return domain . to_sympy ( result ) <EOL> return None <EOL> result = try_non_polynomial_gcd ( seq ) <EOL> if result is not None : <EOL> return result <EOL> options . allowed_flags ( args , [ '<STR_LIT>' ] ) <EOL> try : <EOL> polys , opt = parallel_poly_from_expr ( seq , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> result = try_non_polynomial_gcd ( exc . exprs ) <EOL> if result is not None : <EOL> return result <EOL> else : <EOL> raise ComputationFailed ( '<STR_LIT>' , len ( seq ) , exc ) <EOL> if not polys : <EOL> if not opt . polys : <EOL> return S . Zero <EOL> else : <EOL> return Poly ( <NUM_LIT:0> , opt = opt ) <EOL> result , polys = polys [ <NUM_LIT:0> ] , polys [ <NUM_LIT:1> : ] <EOL> for poly in polys : <EOL> result = result . gcd ( poly ) <EOL> if result . is_one : <EOL> break <EOL> if not opt . polys : <EOL> return result . as_expr ( ) <EOL> else : <EOL> return result <EOL> @ public <EOL> def gcd ( f , g = None , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f , '<STR_LIT>' ) : <EOL> if g is not None : <EOL> gens = ( g , ) + gens <EOL> return gcd_list ( f , * gens , ** args ) <EOL> elif g is None : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> options . allowed_flags ( args , [ '<STR_LIT>' ] ) <EOL> try : <EOL> ( F , G ) , opt = parallel_poly_from_expr ( ( f , g ) , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> domain , ( a , b ) = construct_domain ( exc . exprs ) <EOL> try : <EOL> return domain . to_sympy ( domain . gcd ( a , b ) ) <EOL> except NotImplementedError : <EOL> raise ComputationFailed ( '<STR_LIT>' , <NUM_LIT:2> , exc ) <EOL> result = F . gcd ( G ) <EOL> if not opt . polys : <EOL> return result . as_expr ( ) <EOL> else : <EOL> return result <EOL> @ public <EOL> def lcm_list ( seq , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> seq = sympify ( seq ) <EOL> def try_non_polynomial_lcm ( seq ) : <EOL> if not gens and not args : <EOL> domain , numbers = construct_domain ( seq ) <EOL> if not numbers : <EOL> return domain . one <EOL> elif domain . is_Numerical : <EOL> result , numbers = numbers [ <NUM_LIT:0> ] , numbers [ <NUM_LIT:1> : ] <EOL> for number in numbers : <EOL> result = domain . lcm ( result , number ) <EOL> return domain . to_sympy ( result ) <EOL> return None <EOL> result = try_non_polynomial_lcm ( seq ) <EOL> if result is not None : <EOL> return result <EOL> options . allowed_flags ( args , [ '<STR_LIT>' ] ) <EOL> try : <EOL> polys , opt = parallel_poly_from_expr ( seq , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> result = try_non_polynomial_lcm ( exc . exprs ) <EOL> if result is not None : <EOL> return result <EOL> else : <EOL> raise ComputationFailed ( '<STR_LIT>' , len ( seq ) , exc ) <EOL> if not polys : <EOL> if not opt . polys : <EOL> return S . One <EOL> else : <EOL> return Poly ( <NUM_LIT:1> , opt = opt ) <EOL> result , polys = polys [ <NUM_LIT:0> ] , polys [ <NUM_LIT:1> : ] <EOL> for poly in polys : <EOL> result = result . lcm ( poly ) <EOL> if not opt . polys : <EOL> return result . as_expr ( ) <EOL> else : <EOL> return result <EOL> @ public <EOL> def lcm ( f , g = None , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f , '<STR_LIT>' ) : <EOL> if g is not None : <EOL> gens = ( g , ) + gens <EOL> return lcm_list ( f , * gens , ** args ) <EOL> elif g is None : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> options . allowed_flags ( args , [ '<STR_LIT>' ] ) <EOL> try : <EOL> ( F , G ) , opt = parallel_poly_from_expr ( ( f , g ) , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> domain , ( a , b ) = construct_domain ( exc . exprs ) <EOL> try : <EOL> return domain . to_sympy ( domain . lcm ( a , b ) ) <EOL> except NotImplementedError : <EOL> raise ComputationFailed ( '<STR_LIT>' , <NUM_LIT:2> , exc ) <EOL> result = F . lcm ( G ) <EOL> if not opt . polys : <EOL> return result . as_expr ( ) <EOL> else : <EOL> return result <EOL> @ public <EOL> def terms_gcd ( f , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> from sympy . core . relational import Equality <EOL> orig = sympify ( f ) <EOL> if not isinstance ( f , Expr ) or f . is_Atom : <EOL> return orig <EOL> if args . get ( '<STR_LIT>' , False ) : <EOL> new = f . func ( * [ terms_gcd ( a , * gens , ** args ) for a in f . args ] ) <EOL> args . pop ( '<STR_LIT>' ) <EOL> args [ '<STR_LIT>' ] = False <EOL> return terms_gcd ( new , * gens , ** args ) <EOL> if isinstance ( f , Equality ) : <EOL> return f <EOL> clear = args . pop ( '<STR_LIT>' , True ) <EOL> options . allowed_flags ( args , [ '<STR_LIT>' ] ) <EOL> try : <EOL> F , opt = poly_from_expr ( f , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> return exc . expr <EOL> J , f = F . terms_gcd ( ) <EOL> if opt . domain . has_Ring : <EOL> if opt . domain . has_Field : <EOL> denom , f = f . clear_denoms ( convert = True ) <EOL> coeff , f = f . primitive ( ) <EOL> if opt . domain . has_Field : <EOL> coeff /= denom <EOL> else : <EOL> coeff = S . One <EOL> term = Mul ( * [ x ** j for x , j in zip ( f . gens , J ) ] ) <EOL> if coeff == <NUM_LIT:1> : <EOL> coeff = S . One <EOL> if term == <NUM_LIT:1> : <EOL> return orig <EOL> if clear : <EOL> return _keep_coeff ( coeff , term * f . as_expr ( ) ) <EOL> coeff , f = _keep_coeff ( coeff , f . as_expr ( ) , clear = False ) . as_coeff_Mul ( ) <EOL> return _keep_coeff ( coeff , term * f , clear = False ) <EOL> @ public <EOL> def trunc ( f , p , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> try : <EOL> F , opt = poly_from_expr ( f , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> raise ComputationFailed ( '<STR_LIT>' , <NUM_LIT:1> , exc ) <EOL> result = F . trunc ( sympify ( p ) ) <EOL> if not opt . polys : <EOL> return result . as_expr ( ) <EOL> else : <EOL> return result <EOL> @ public <EOL> def monic ( f , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> try : <EOL> F , opt = poly_from_expr ( f , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> raise ComputationFailed ( '<STR_LIT>' , <NUM_LIT:1> , exc ) <EOL> result = F . monic ( auto = opt . auto ) <EOL> if not opt . polys : <EOL> return result . as_expr ( ) <EOL> else : <EOL> return result <EOL> @ public <EOL> def content ( f , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ '<STR_LIT>' ] ) <EOL> try : <EOL> F , opt = poly_from_expr ( f , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> raise ComputationFailed ( '<STR_LIT:content>' , <NUM_LIT:1> , exc ) <EOL> return F . content ( ) <EOL> @ public <EOL> def primitive ( f , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ '<STR_LIT>' ] ) <EOL> try : <EOL> F , opt = poly_from_expr ( f , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> raise ComputationFailed ( '<STR_LIT>' , <NUM_LIT:1> , exc ) <EOL> cont , result = F . primitive ( ) <EOL> if not opt . polys : <EOL> return cont , result . as_expr ( ) <EOL> else : <EOL> return cont , result <EOL> @ public <EOL> def compose ( f , g , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ '<STR_LIT>' ] ) <EOL> try : <EOL> ( F , G ) , opt = parallel_poly_from_expr ( ( f , g ) , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> raise ComputationFailed ( '<STR_LIT>' , <NUM_LIT:2> , exc ) <EOL> result = F . compose ( G ) <EOL> if not opt . polys : <EOL> return result . as_expr ( ) <EOL> else : <EOL> return result <EOL> @ public <EOL> def decompose ( f , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ '<STR_LIT>' ] ) <EOL> try : <EOL> F , opt = poly_from_expr ( f , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> raise ComputationFailed ( '<STR_LIT>' , <NUM_LIT:1> , exc ) <EOL> result = F . decompose ( ) <EOL> if not opt . polys : <EOL> return [ r . as_expr ( ) for r in result ] <EOL> else : <EOL> return result <EOL> @ public <EOL> def sturm ( f , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> try : <EOL> F , opt = poly_from_expr ( f , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> raise ComputationFailed ( '<STR_LIT>' , <NUM_LIT:1> , exc ) <EOL> result = F . sturm ( auto = opt . auto ) <EOL> if not opt . polys : <EOL> return [ r . as_expr ( ) for r in result ] <EOL> else : <EOL> return result <EOL> @ public <EOL> def gff_list ( f , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ '<STR_LIT>' ] ) <EOL> try : <EOL> F , opt = poly_from_expr ( f , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> raise ComputationFailed ( '<STR_LIT>' , <NUM_LIT:1> , exc ) <EOL> factors = F . gff_list ( ) <EOL> if not opt . polys : <EOL> return [ ( g . as_expr ( ) , k ) for g , k in factors ] <EOL> else : <EOL> return factors <EOL> @ public <EOL> def gff ( f , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> @ public <EOL> def sqf_norm ( f , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ '<STR_LIT>' ] ) <EOL> try : <EOL> F , opt = poly_from_expr ( f , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> raise ComputationFailed ( '<STR_LIT>' , <NUM_LIT:1> , exc ) <EOL> s , g , r = F . sqf_norm ( ) <EOL> if not opt . polys : <EOL> return Integer ( s ) , g . as_expr ( ) , r . as_expr ( ) <EOL> else : <EOL> return Integer ( s ) , g , r <EOL> @ public <EOL> def sqf_part ( f , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ '<STR_LIT>' ] ) <EOL> try : <EOL> F , opt = poly_from_expr ( f , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> raise ComputationFailed ( '<STR_LIT>' , <NUM_LIT:1> , exc ) <EOL> result = F . sqf_part ( ) <EOL> if not opt . polys : <EOL> return result . as_expr ( ) <EOL> else : <EOL> return result <EOL> def _sorted_factors ( factors , method ) : <EOL> """<STR_LIT>""" <EOL> if method == '<STR_LIT>' : <EOL> def key ( obj ) : <EOL> poly , exp = obj <EOL> rep = poly . rep . rep <EOL> return ( exp , len ( rep ) , len ( poly . gens ) , rep ) <EOL> else : <EOL> def key ( obj ) : <EOL> poly , exp = obj <EOL> rep = poly . rep . rep <EOL> return ( len ( rep ) , len ( poly . gens ) , exp , rep ) <EOL> return sorted ( factors , key = key ) <EOL> def _factors_product ( factors ) : <EOL> """<STR_LIT>""" <EOL> return Mul ( * [ f . as_expr ( ) ** k for f , k in factors ] ) <EOL> def _symbolic_factor_list ( expr , opt , method ) : <EOL> """<STR_LIT>""" <EOL> coeff , factors = S . One , [ ] <EOL> args = [ i . _eval_factor ( ) if hasattr ( i , '<STR_LIT>' ) else i <EOL> for i in Mul . make_args ( expr ) ] <EOL> for arg in args : <EOL> if arg . is_Number : <EOL> coeff *= arg <EOL> continue <EOL> if arg . is_Mul : <EOL> args . extend ( arg . args ) <EOL> continue <EOL> if arg . is_Pow : <EOL> base , exp = arg . args <EOL> if base . is_Number : <EOL> factors . append ( ( base , exp ) ) <EOL> continue <EOL> else : <EOL> base , exp = arg , S . One <EOL> try : <EOL> poly , _ = _poly_from_expr ( base , opt ) <EOL> except PolificationFailed as exc : <EOL> factors . append ( ( exc . expr , exp ) ) <EOL> else : <EOL> func = getattr ( poly , method + '<STR_LIT>' ) <EOL> _coeff , _factors = func ( ) <EOL> if _coeff is not S . One : <EOL> if exp . is_Integer : <EOL> coeff *= _coeff ** exp <EOL> elif _coeff . is_positive : <EOL> factors . append ( ( _coeff , exp ) ) <EOL> else : <EOL> _factors . append ( ( _coeff , S . One ) ) <EOL> if exp is S . One : <EOL> factors . extend ( _factors ) <EOL> elif exp . is_integer : <EOL> factors . extend ( [ ( f , k * exp ) for f , k in _factors ] ) <EOL> else : <EOL> other = [ ] <EOL> for f , k in _factors : <EOL> if f . as_expr ( ) . is_positive : <EOL> factors . append ( ( f , k * exp ) ) <EOL> else : <EOL> other . append ( ( f , k ) ) <EOL> factors . append ( ( _factors_product ( other ) , exp ) ) <EOL> return coeff , factors <EOL> def _symbolic_factor ( expr , opt , method ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( expr , Expr ) and not expr . is_Relational : <EOL> if hasattr ( expr , '<STR_LIT>' ) : <EOL> return expr . _eval_factor ( ) <EOL> coeff , factors = _symbolic_factor_list ( together ( expr ) , opt , method ) <EOL> return _keep_coeff ( coeff , _factors_product ( factors ) ) <EOL> elif hasattr ( expr , '<STR_LIT:args>' ) : <EOL> return expr . func ( * [ _symbolic_factor ( arg , opt , method ) for arg in expr . args ] ) <EOL> elif hasattr ( expr , '<STR_LIT>' ) : <EOL> return expr . __class__ ( [ _symbolic_factor ( arg , opt , method ) for arg in expr ] ) <EOL> else : <EOL> return expr <EOL> def _generic_factor_list ( expr , gens , args , method ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> opt = options . build_options ( gens , args ) <EOL> expr = sympify ( expr ) <EOL> if isinstance ( expr , Expr ) and not expr . is_Relational : <EOL> numer , denom = together ( expr ) . as_numer_denom ( ) <EOL> cp , fp = _symbolic_factor_list ( numer , opt , method ) <EOL> cq , fq = _symbolic_factor_list ( denom , opt , method ) <EOL> if fq and not opt . frac : <EOL> raise PolynomialError ( "<STR_LIT>" % expr ) <EOL> _opt = opt . clone ( dict ( expand = True ) ) <EOL> for factors in ( fp , fq ) : <EOL> for i , ( f , k ) in enumerate ( factors ) : <EOL> if not f . is_Poly : <EOL> f , _ = _poly_from_expr ( f , _opt ) <EOL> factors [ i ] = ( f , k ) <EOL> fp = _sorted_factors ( fp , method ) <EOL> fq = _sorted_factors ( fq , method ) <EOL> if not opt . polys : <EOL> fp = [ ( f . as_expr ( ) , k ) for f , k in fp ] <EOL> fq = [ ( f . as_expr ( ) , k ) for f , k in fq ] <EOL> coeff = cp / cq <EOL> if not opt . frac : <EOL> return coeff , fp <EOL> else : <EOL> return coeff , fp , fq <EOL> else : <EOL> raise PolynomialError ( "<STR_LIT>" % expr ) <EOL> def _generic_factor ( expr , gens , args , method ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ ] ) <EOL> opt = options . build_options ( gens , args ) <EOL> return _symbolic_factor ( sympify ( expr ) , opt , method ) <EOL> def to_rational_coeffs ( f ) : <EOL> """<STR_LIT>""" <EOL> from sympy . simplify . simplify import simplify <EOL> def _try_rescale ( f , f1 = None ) : <EOL> """<STR_LIT>""" <EOL> from sympy . core . add import Add <EOL> if not len ( f . gens ) == <NUM_LIT:1> or not ( f . gens [ <NUM_LIT:0> ] ) . is_Atom : <EOL> return None , f <EOL> n = f . degree ( ) <EOL> lc = f . LC ( ) <EOL> f1 = f1 or f1 . monic ( ) <EOL> coeffs = f1 . all_coeffs ( ) [ <NUM_LIT:1> : ] <EOL> coeffs = [ simplify ( coeffx ) for coeffx in coeffs ] <EOL> if coeffs [ - <NUM_LIT:2> ] : <EOL> rescale1_x = simplify ( coeffs [ - <NUM_LIT:2> ] / coeffs [ - <NUM_LIT:1> ] ) <EOL> coeffs1 = [ ] <EOL> for i in range ( len ( coeffs ) ) : <EOL> coeffx = simplify ( coeffs [ i ] * rescale1_x ** ( i + <NUM_LIT:1> ) ) <EOL> if not coeffx . is_rational : <EOL> break <EOL> coeffs1 . append ( coeffx ) <EOL> else : <EOL> rescale_x = simplify ( <NUM_LIT:1> / rescale1_x ) <EOL> x = f . gens [ <NUM_LIT:0> ] <EOL> v = [ x ** n ] <EOL> for i in range ( <NUM_LIT:1> , n + <NUM_LIT:1> ) : <EOL> v . append ( coeffs1 [ i - <NUM_LIT:1> ] * x ** ( n - i ) ) <EOL> f = Add ( * v ) <EOL> f = Poly ( f ) <EOL> return lc , rescale_x , f <EOL> return None <EOL> def _try_translate ( f , f1 = None ) : <EOL> """<STR_LIT>""" <EOL> from sympy . core . add import Add <EOL> if not len ( f . gens ) == <NUM_LIT:1> or not ( f . gens [ <NUM_LIT:0> ] ) . is_Atom : <EOL> return None , f <EOL> n = f . degree ( ) <EOL> f1 = f1 or f1 . monic ( ) <EOL> coeffs = f1 . all_coeffs ( ) [ <NUM_LIT:1> : ] <EOL> c = simplify ( coeffs [ <NUM_LIT:0> ] ) <EOL> if c and not c . is_rational : <EOL> func = Add <EOL> if c . is_Add : <EOL> args = c . args <EOL> func = c . func <EOL> else : <EOL> args = [ c ] <EOL> sifted = sift ( args , lambda z : z . is_rational ) <EOL> c1 , c2 = sifted [ True ] , sifted [ False ] <EOL> alpha = - func ( * c2 ) / n <EOL> f2 = f1 . shift ( alpha ) <EOL> return alpha , f2 <EOL> return None <EOL> def _has_square_roots ( p ) : <EOL> """<STR_LIT>""" <EOL> from sympy . core . exprtools import Factors <EOL> coeffs = p . coeffs ( ) <EOL> has_sq = False <EOL> for y in coeffs : <EOL> for x in Add . make_args ( y ) : <EOL> f = Factors ( x ) . factors <EOL> r = [ wx . q for b , wx in f . items ( ) if <EOL> b . is_number and wx . is_Rational and wx . q >= <NUM_LIT:2> ] <EOL> if not r : <EOL> continue <EOL> if min ( r ) == <NUM_LIT:2> : <EOL> has_sq = True <EOL> if max ( r ) > <NUM_LIT:2> : <EOL> return False <EOL> return has_sq <EOL> if f . get_domain ( ) . is_EX and _has_square_roots ( f ) : <EOL> f1 = f . monic ( ) <EOL> r = _try_rescale ( f , f1 ) <EOL> if r : <EOL> return r [ <NUM_LIT:0> ] , r [ <NUM_LIT:1> ] , None , r [ <NUM_LIT:2> ] <EOL> else : <EOL> r = _try_translate ( f , f1 ) <EOL> if r : <EOL> return None , None , r [ <NUM_LIT:0> ] , r [ <NUM_LIT:1> ] <EOL> return None <EOL> def _torational_factor_list ( p , x ) : <EOL> """<STR_LIT>""" <EOL> from sympy . simplify . simplify import simplify <EOL> p1 = Poly ( p , x , domain = '<STR_LIT>' ) <EOL> n = p1 . degree ( ) <EOL> res = to_rational_coeffs ( p1 ) <EOL> if not res : <EOL> return None <EOL> lc , r , t , g = res <EOL> factors = factor_list ( g . as_expr ( ) ) <EOL> if lc : <EOL> c = simplify ( factors [ <NUM_LIT:0> ] * lc * r ** n ) <EOL> r1 = simplify ( <NUM_LIT:1> / r ) <EOL> a = [ ] <EOL> for z in factors [ <NUM_LIT:1> : ] [ <NUM_LIT:0> ] : <EOL> a . append ( ( simplify ( z [ <NUM_LIT:0> ] . subs ( { x : x * r1 } ) ) , z [ <NUM_LIT:1> ] ) ) <EOL> else : <EOL> c = factors [ <NUM_LIT:0> ] <EOL> a = [ ] <EOL> for z in factors [ <NUM_LIT:1> : ] [ <NUM_LIT:0> ] : <EOL> a . append ( ( z [ <NUM_LIT:0> ] . subs ( { x : x - t } ) , z [ <NUM_LIT:1> ] ) ) <EOL> return ( c , a ) <EOL> @ public <EOL> def sqf_list ( f , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> return _generic_factor_list ( f , gens , args , method = '<STR_LIT>' ) <EOL> @ public <EOL> def sqf ( f , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> return _generic_factor ( f , gens , args , method = '<STR_LIT>' ) <EOL> @ public <EOL> def factor_list ( f , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> return _generic_factor_list ( f , gens , args , method = '<STR_LIT>' ) <EOL> @ public <EOL> def factor ( f , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> f = sympify ( f ) <EOL> if args . pop ( '<STR_LIT>' , False ) : <EOL> partials = { } <EOL> muladd = f . atoms ( Mul , Add ) <EOL> for p in muladd : <EOL> fac = factor ( p , * gens , ** args ) <EOL> if ( fac . is_Mul or fac . is_Pow ) and fac != p : <EOL> partials [ p ] = fac <EOL> return f . xreplace ( partials ) <EOL> try : <EOL> return _generic_factor ( f , gens , args , method = '<STR_LIT>' ) <EOL> except PolynomialError as msg : <EOL> if not f . is_commutative : <EOL> from sympy . core . exprtools import factor_nc <EOL> return factor_nc ( f ) <EOL> else : <EOL> raise PolynomialError ( msg ) <EOL> @ public <EOL> def intervals ( F , all = False , eps = None , inf = None , sup = None , strict = False , fast = False , sqf = False ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( F , '<STR_LIT>' ) : <EOL> try : <EOL> F = Poly ( F ) <EOL> except GeneratorsNeeded : <EOL> return [ ] <EOL> return F . intervals ( all = all , eps = eps , inf = inf , sup = sup , fast = fast , sqf = sqf ) <EOL> else : <EOL> polys , opt = parallel_poly_from_expr ( F , domain = '<STR_LIT>' ) <EOL> if len ( opt . gens ) > <NUM_LIT:1> : <EOL> raise MultivariatePolynomialError <EOL> for i , poly in enumerate ( polys ) : <EOL> polys [ i ] = poly . rep . rep <EOL> if eps is not None : <EOL> eps = opt . domain . convert ( eps ) <EOL> if eps <= <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if inf is not None : <EOL> inf = opt . domain . convert ( inf ) <EOL> if sup is not None : <EOL> sup = opt . domain . convert ( sup ) <EOL> intervals = dup_isolate_real_roots_list ( polys , opt . domain , <EOL> eps = eps , inf = inf , sup = sup , strict = strict , fast = fast ) <EOL> result = [ ] <EOL> for ( s , t ) , indices in intervals : <EOL> s , t = opt . domain . to_sympy ( s ) , opt . domain . to_sympy ( t ) <EOL> result . append ( ( ( s , t ) , indices ) ) <EOL> return result <EOL> @ public <EOL> def refine_root ( f , s , t , eps = None , steps = None , fast = False , check_sqf = False ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> F = Poly ( f ) <EOL> except GeneratorsNeeded : <EOL> raise PolynomialError ( <EOL> "<STR_LIT>" % f ) <EOL> return F . refine_root ( s , t , eps = eps , steps = steps , fast = fast , check_sqf = check_sqf ) <EOL> @ public <EOL> def count_roots ( f , inf = None , sup = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> F = Poly ( f , greedy = False ) <EOL> except GeneratorsNeeded : <EOL> raise PolynomialError ( "<STR_LIT>" % f ) <EOL> return F . count_roots ( inf = inf , sup = sup ) <EOL> @ public <EOL> def real_roots ( f , multiple = True ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> F = Poly ( f , greedy = False ) <EOL> except GeneratorsNeeded : <EOL> raise PolynomialError ( <EOL> "<STR_LIT>" % f ) <EOL> return F . real_roots ( multiple = multiple ) <EOL> @ public <EOL> def nroots ( f , n = <NUM_LIT:15> , maxsteps = <NUM_LIT:50> , cleanup = True ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> F = Poly ( f , greedy = False ) <EOL> except GeneratorsNeeded : <EOL> raise PolynomialError ( <EOL> "<STR_LIT>" % f ) <EOL> return F . nroots ( n = n , maxsteps = maxsteps , cleanup = cleanup ) <EOL> @ public <EOL> def ground_roots ( f , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ ] ) <EOL> try : <EOL> F , opt = poly_from_expr ( f , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> raise ComputationFailed ( '<STR_LIT>' , <NUM_LIT:1> , exc ) <EOL> return F . ground_roots ( ) <EOL> @ public <EOL> def nth_power_roots_poly ( f , n , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ ] ) <EOL> try : <EOL> F , opt = poly_from_expr ( f , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> raise ComputationFailed ( '<STR_LIT>' , <NUM_LIT:1> , exc ) <EOL> result = F . nth_power_roots_poly ( n ) <EOL> if not opt . polys : <EOL> return result . as_expr ( ) <EOL> else : <EOL> return result <EOL> @ public <EOL> def cancel ( f , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> from sympy . core . exprtools import factor_terms <EOL> from sympy . functions . elementary . piecewise import Piecewise <EOL> options . allowed_flags ( args , [ '<STR_LIT>' ] ) <EOL> f = sympify ( f ) <EOL> if not isinstance ( f , ( tuple , Tuple ) ) : <EOL> if f . is_Number or isinstance ( f , Relational ) or not isinstance ( f , Expr ) : <EOL> return f <EOL> f = factor_terms ( f , radical = True ) <EOL> p , q = f . as_numer_denom ( ) <EOL> elif len ( f ) == <NUM_LIT:2> : <EOL> p , q = f <EOL> elif isinstance ( f , Tuple ) : <EOL> return factor_terms ( f ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % f ) <EOL> try : <EOL> ( F , G ) , opt = parallel_poly_from_expr ( ( p , q ) , * gens , ** args ) <EOL> except PolificationFailed : <EOL> if not isinstance ( f , ( tuple , Tuple ) ) : <EOL> return f <EOL> else : <EOL> return S . One , p , q <EOL> except PolynomialError as msg : <EOL> if f . is_commutative and not f . has ( Piecewise ) : <EOL> raise PolynomialError ( msg ) <EOL> if f . is_Add or f . is_Mul : <EOL> sifted = sift ( f . args , lambda x : x . is_commutative is True and not x . has ( Piecewise ) ) <EOL> c , nc = sifted [ True ] , sifted [ False ] <EOL> nc = [ cancel ( i ) for i in nc ] <EOL> return f . func ( cancel ( f . func . _from_args ( c ) ) , * nc ) <EOL> else : <EOL> reps = [ ] <EOL> pot = preorder_traversal ( f ) <EOL> next ( pot ) <EOL> for e in pot : <EOL> if isinstance ( e , ( tuple , Tuple , BooleanAtom ) ) : <EOL> continue <EOL> try : <EOL> reps . append ( ( e , cancel ( e ) ) ) <EOL> pot . skip ( ) <EOL> except NotImplementedError : <EOL> pass <EOL> return f . xreplace ( dict ( reps ) ) <EOL> c , P , Q = F . cancel ( G ) <EOL> if not isinstance ( f , ( tuple , Tuple ) ) : <EOL> return c * ( P . as_expr ( ) / Q . as_expr ( ) ) <EOL> else : <EOL> if not opt . polys : <EOL> return c , P . as_expr ( ) , Q . as_expr ( ) <EOL> else : <EOL> return c , P , Q <EOL> @ public <EOL> def reduced ( f , G , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> try : <EOL> polys , opt = parallel_poly_from_expr ( [ f ] + list ( G ) , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> raise ComputationFailed ( '<STR_LIT>' , <NUM_LIT:0> , exc ) <EOL> domain = opt . domain <EOL> retract = False <EOL> if opt . auto and domain . has_Ring and not domain . has_Field : <EOL> opt = opt . clone ( dict ( domain = domain . get_field ( ) ) ) <EOL> retract = True <EOL> from sympy . polys . rings import xring <EOL> _ring , _ = xring ( opt . gens , opt . domain , opt . order ) <EOL> for i , poly in enumerate ( polys ) : <EOL> poly = poly . set_domain ( opt . domain ) . rep . to_dict ( ) <EOL> polys [ i ] = _ring . from_dict ( poly ) <EOL> Q , r = polys [ <NUM_LIT:0> ] . div ( polys [ <NUM_LIT:1> : ] ) <EOL> Q = [ Poly . _from_dict ( dict ( q ) , opt ) for q in Q ] <EOL> r = Poly . _from_dict ( dict ( r ) , opt ) <EOL> if retract : <EOL> try : <EOL> _Q , _r = [ q . to_ring ( ) for q in Q ] , r . to_ring ( ) <EOL> except CoercionFailed : <EOL> pass <EOL> else : <EOL> Q , r = _Q , _r <EOL> if not opt . polys : <EOL> return [ q . as_expr ( ) for q in Q ] , r . as_expr ( ) <EOL> else : <EOL> return Q , r <EOL> @ public <EOL> def groebner ( F , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> return GroebnerBasis ( F , * gens , ** args ) <EOL> @ public <EOL> def is_zero_dimensional ( F , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> return GroebnerBasis ( F , * gens , ** args ) . is_zero_dimensional <EOL> @ public <EOL> class GroebnerBasis ( Basic ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( cls , F , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> try : <EOL> polys , opt = parallel_poly_from_expr ( F , * gens , ** args ) <EOL> except PolificationFailed as exc : <EOL> raise ComputationFailed ( '<STR_LIT>' , len ( F ) , exc ) <EOL> from sympy . polys . rings import PolyRing <EOL> ring = PolyRing ( opt . gens , opt . domain , opt . order ) <EOL> for i , poly in enumerate ( polys ) : <EOL> polys [ i ] = ring . from_dict ( poly . rep . to_dict ( ) ) <EOL> G = _groebner ( polys , ring , method = opt . method ) <EOL> G = [ Poly . _from_dict ( g , opt ) for g in G ] <EOL> return cls . _new ( G , opt ) <EOL> @ classmethod <EOL> def _new ( cls , basis , options ) : <EOL> obj = Basic . __new__ ( cls ) <EOL> obj . _basis = tuple ( basis ) <EOL> obj . _options = options <EOL> return obj <EOL> @ property <EOL> def args ( self ) : <EOL> return ( Tuple ( * self . _basis ) , Tuple ( * self . _options . gens ) ) <EOL> @ property <EOL> def exprs ( self ) : <EOL> return [ poly . as_expr ( ) for poly in self . _basis ] <EOL> @ property <EOL> def polys ( self ) : <EOL> return list ( self . _basis ) <EOL> @ property <EOL> def gens ( self ) : <EOL> return self . _options . gens <EOL> @ property <EOL> def domain ( self ) : <EOL> return self . _options . domain <EOL> @ property <EOL> def order ( self ) : <EOL> return self . _options . order <EOL> def __len__ ( self ) : <EOL> return len ( self . _basis ) <EOL> def __iter__ ( self ) : <EOL> if self . _options . polys : <EOL> return iter ( self . polys ) <EOL> else : <EOL> return iter ( self . exprs ) <EOL> def __getitem__ ( self , item ) : <EOL> if self . _options . polys : <EOL> basis = self . polys <EOL> else : <EOL> basis = self . exprs <EOL> return basis [ item ] <EOL> def __hash__ ( self ) : <EOL> return hash ( ( self . _basis , tuple ( self . _options . items ( ) ) ) ) <EOL> def __eq__ ( self , other ) : <EOL> if isinstance ( other , self . __class__ ) : <EOL> return self . _basis == other . _basis and self . _options == other . _options <EOL> elif iterable ( other ) : <EOL> return self . polys == list ( other ) or self . exprs == list ( other ) <EOL> else : <EOL> return False <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> @ property <EOL> def is_zero_dimensional ( self ) : <EOL> """<STR_LIT>""" <EOL> def single_var ( monomial ) : <EOL> return sum ( map ( bool , monomial ) ) == <NUM_LIT:1> <EOL> exponents = Monomial ( [ <NUM_LIT:0> ] * len ( self . gens ) ) <EOL> order = self . _options . order <EOL> for poly in self . polys : <EOL> monomial = poly . LM ( order = order ) <EOL> if single_var ( monomial ) : <EOL> exponents *= monomial <EOL> return all ( exponents ) <EOL> def fglm ( self , order ) : <EOL> """<STR_LIT>""" <EOL> opt = self . _options <EOL> src_order = opt . order <EOL> dst_order = monomial_key ( order ) <EOL> if src_order == dst_order : <EOL> return self <EOL> if not self . is_zero_dimensional : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> polys = list ( self . _basis ) <EOL> domain = opt . domain <EOL> opt = opt . clone ( dict ( <EOL> domain = domain . get_field ( ) , <EOL> order = dst_order , <EOL> ) ) <EOL> from sympy . polys . rings import xring <EOL> _ring , _ = xring ( opt . gens , opt . domain , src_order ) <EOL> for i , poly in enumerate ( polys ) : <EOL> poly = poly . set_domain ( opt . domain ) . rep . to_dict ( ) <EOL> polys [ i ] = _ring . from_dict ( poly ) <EOL> G = matrix_fglm ( polys , _ring , dst_order ) <EOL> G = [ Poly . _from_dict ( dict ( g ) , opt ) for g in G ] <EOL> if not domain . has_Field : <EOL> G = [ g . clear_denoms ( convert = True ) [ <NUM_LIT:1> ] for g in G ] <EOL> opt . domain = domain <EOL> return self . _new ( G , opt ) <EOL> def reduce ( self , expr , auto = True ) : <EOL> """<STR_LIT>""" <EOL> poly = Poly . _from_expr ( expr , self . _options ) <EOL> polys = [ poly ] + list ( self . _basis ) <EOL> opt = self . _options <EOL> domain = opt . domain <EOL> retract = False <EOL> if auto and domain . has_Ring and not domain . has_Field : <EOL> opt = opt . clone ( dict ( domain = domain . get_field ( ) ) ) <EOL> retract = True <EOL> from sympy . polys . rings import xring <EOL> _ring , _ = xring ( opt . gens , opt . domain , opt . order ) <EOL> for i , poly in enumerate ( polys ) : <EOL> poly = poly . set_domain ( opt . domain ) . rep . to_dict ( ) <EOL> polys [ i ] = _ring . from_dict ( poly ) <EOL> Q , r = polys [ <NUM_LIT:0> ] . div ( polys [ <NUM_LIT:1> : ] ) <EOL> Q = [ Poly . _from_dict ( dict ( q ) , opt ) for q in Q ] <EOL> r = Poly . _from_dict ( dict ( r ) , opt ) <EOL> if retract : <EOL> try : <EOL> _Q , _r = [ q . to_ring ( ) for q in Q ] , r . to_ring ( ) <EOL> except CoercionFailed : <EOL> pass <EOL> else : <EOL> Q , r = _Q , _r <EOL> if not opt . polys : <EOL> return [ q . as_expr ( ) for q in Q ] , r . as_expr ( ) <EOL> else : <EOL> return Q , r <EOL> def contains ( self , poly ) : <EOL> """<STR_LIT>""" <EOL> return self . reduce ( poly ) [ <NUM_LIT:1> ] == <NUM_LIT:0> <EOL> @ public <EOL> def poly ( expr , * gens , ** args ) : <EOL> """<STR_LIT>""" <EOL> options . allowed_flags ( args , [ ] ) <EOL> def _poly ( expr , opt ) : <EOL> terms , poly_terms = [ ] , [ ] <EOL> for term in Add . make_args ( expr ) : <EOL> factors , poly_factors = [ ] , [ ] <EOL> for factor in Mul . make_args ( term ) : <EOL> if factor . is_Add : <EOL> poly_factors . append ( _poly ( factor , opt ) ) <EOL> elif factor . is_Pow and factor . base . is_Add and factor . exp . is_Integer : <EOL> poly_factors . append ( <EOL> _poly ( factor . base , opt ) . pow ( factor . exp ) ) <EOL> else : <EOL> factors . append ( factor ) <EOL> if not poly_factors : <EOL> terms . append ( term ) <EOL> else : <EOL> product = poly_factors [ <NUM_LIT:0> ] <EOL> for factor in poly_factors [ <NUM_LIT:1> : ] : <EOL> product = product . mul ( factor ) <EOL> if factors : <EOL> factor = Mul ( * factors ) <EOL> if factor . is_Number : <EOL> product = product . mul ( factor ) <EOL> else : <EOL> product = product . mul ( Poly . _from_expr ( factor , opt ) ) <EOL> poly_terms . append ( product ) <EOL> if not poly_terms : <EOL> result = Poly . _from_expr ( expr , opt ) <EOL> else : <EOL> result = poly_terms [ <NUM_LIT:0> ] <EOL> for term in poly_terms [ <NUM_LIT:1> : ] : <EOL> result = result . add ( term ) <EOL> if terms : <EOL> term = Add ( * terms ) <EOL> if term . is_Number : <EOL> result = result . add ( term ) <EOL> else : <EOL> result = result . add ( Poly . _from_expr ( term , opt ) ) <EOL> return result . reorder ( * opt . get ( '<STR_LIT>' , ( ) ) , ** args ) <EOL> expr = sympify ( expr ) <EOL> if expr . is_Poly : <EOL> return Poly ( expr , * gens , ** args ) <EOL> if '<STR_LIT>' not in args : <EOL> args [ '<STR_LIT>' ] = False <EOL> opt = options . build_options ( gens , args ) <EOL> return _poly ( expr , opt ) </s>
