<s> """<STR_LIT>""" <EOL> from __future__ import print_function , division <EOL> from sympy . polys . domains . domainelement import DomainElement <EOL> from sympy . core import Basic , sympify <EOL> from sympy . core . compatibility import HAS_GMPY , integer_types , is_sequence <EOL> from sympy . polys . polyerrors import UnificationFailed , CoercionFailed , DomainError <EOL> from sympy . polys . orderings import lex <EOL> from sympy . polys . polyutils import _unify_gens <EOL> from sympy . utilities import default_sort_key , public <EOL> @ public <EOL> class Domain ( object ) : <EOL> """<STR_LIT>""" <EOL> dtype = None <EOL> zero = None <EOL> one = None <EOL> has_Ring = False <EOL> has_Field = False <EOL> has_assoc_Ring = False <EOL> has_assoc_Field = False <EOL> is_FiniteField = is_FF = False <EOL> is_IntegerRing = is_ZZ = False <EOL> is_RationalField = is_QQ = False <EOL> is_RealField = is_RR = False <EOL> is_ComplexField = is_CC = False <EOL> is_AlgebraicField = is_Algebraic = False <EOL> is_PolynomialRing = is_Poly = False <EOL> is_FractionField = is_Frac = False <EOL> is_SymbolicDomain = is_EX = False <EOL> is_Exact = True <EOL> is_Numerical = False <EOL> is_Simple = False <EOL> is_Composite = False <EOL> has_CharacteristicZero = False <EOL> rep = None <EOL> alias = None <EOL> def __init__ ( self ) : <EOL> raise NotImplementedError <EOL> def __str__ ( self ) : <EOL> return self . rep <EOL> def __repr__ ( self ) : <EOL> return str ( self ) <EOL> def __hash__ ( self ) : <EOL> return hash ( ( self . __class__ . __name__ , self . dtype ) ) <EOL> def new ( self , * args ) : <EOL> return self . dtype ( * args ) <EOL> @ property <EOL> def tp ( self ) : <EOL> return self . dtype <EOL> def __call__ ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> return self . new ( * args ) <EOL> def normal ( self , * args ) : <EOL> return self . dtype ( * args ) <EOL> def convert_from ( self , element , base ) : <EOL> """<STR_LIT>""" <EOL> if base . alias is not None : <EOL> method = "<STR_LIT>" + base . alias <EOL> else : <EOL> method = "<STR_LIT>" + base . __class__ . __name__ <EOL> _convert = getattr ( self , method ) <EOL> if _convert is not None : <EOL> result = _convert ( element , base ) <EOL> if result is not None : <EOL> return result <EOL> raise CoercionFailed ( "<STR_LIT>" % ( element , type ( element ) , base , self ) ) <EOL> def convert ( self , element , base = None ) : <EOL> """<STR_LIT>""" <EOL> if base is not None : <EOL> return self . convert_from ( element , base ) <EOL> if self . of_type ( element ) : <EOL> return element <EOL> from sympy . polys . domains import PythonIntegerRing , GMPYIntegerRing , GMPYRationalField , RealField , ComplexField <EOL> if isinstance ( element , integer_types ) : <EOL> return self . convert_from ( element , PythonIntegerRing ( ) ) <EOL> if HAS_GMPY : <EOL> integers = GMPYIntegerRing ( ) <EOL> if isinstance ( element , integers . tp ) : <EOL> return self . convert_from ( element , integers ) <EOL> rationals = GMPYRationalField ( ) <EOL> if isinstance ( element , rationals . tp ) : <EOL> return self . convert_from ( element , rationals ) <EOL> if isinstance ( element , float ) : <EOL> parent = RealField ( tol = False ) <EOL> return self . convert_from ( parent ( element ) , parent ) <EOL> if isinstance ( element , complex ) : <EOL> parent = ComplexField ( tol = False ) <EOL> return self . convert_from ( parent ( element ) , parent ) <EOL> if isinstance ( element , DomainElement ) : <EOL> return self . convert_from ( element , element . parent ( ) ) <EOL> if self . is_Numerical and getattr ( element , '<STR_LIT>' , False ) : <EOL> return self . convert ( element . LC ( ) ) <EOL> if isinstance ( element , Basic ) : <EOL> try : <EOL> return self . from_sympy ( element ) <EOL> except ( TypeError , ValueError ) : <EOL> pass <EOL> else : <EOL> if not is_sequence ( element ) : <EOL> try : <EOL> element = sympify ( element ) <EOL> if isinstance ( element , Basic ) : <EOL> return self . from_sympy ( element ) <EOL> except ( TypeError , ValueError ) : <EOL> pass <EOL> raise CoercionFailed ( "<STR_LIT>" % ( element , type ( element ) , self ) ) <EOL> def of_type ( self , element ) : <EOL> """<STR_LIT>""" <EOL> return isinstance ( element , self . tp ) <EOL> def __contains__ ( self , a ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . convert ( a ) <EOL> except CoercionFailed : <EOL> return False <EOL> return True <EOL> def to_sympy ( self , a ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def from_sympy ( self , a ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def from_FF_python ( K1 , a , K0 ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> def from_ZZ_python ( K1 , a , K0 ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> def from_QQ_python ( K1 , a , K0 ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> def from_FF_gmpy ( K1 , a , K0 ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> def from_ZZ_gmpy ( K1 , a , K0 ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> def from_QQ_gmpy ( K1 , a , K0 ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> def from_RealField ( K1 , a , K0 ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> def from_ComplexField ( K1 , a , K0 ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> def from_AlgebraicField ( K1 , a , K0 ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> def from_PolynomialRing ( K1 , a , K0 ) : <EOL> """<STR_LIT>""" <EOL> if a . is_ground : <EOL> return K1 . convert ( a . LC , K0 . dom ) <EOL> def from_FractionField ( K1 , a , K0 ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> def from_ExpressionDomain ( K1 , a , K0 ) : <EOL> """<STR_LIT>""" <EOL> return K1 . from_sympy ( a . ex ) <EOL> def from_GlobalPolynomialRing ( K1 , a , K0 ) : <EOL> """<STR_LIT>""" <EOL> if a . degree ( ) <= <NUM_LIT:0> : <EOL> return K1 . convert ( a . LC ( ) , K0 . dom ) <EOL> def from_GeneralizedPolynomialRing ( K1 , a , K0 ) : <EOL> return K1 . from_FractionField ( a , K0 ) <EOL> def unify_with_symbols ( K0 , K1 , symbols ) : <EOL> if ( K0 . is_Composite and ( set ( K0 . symbols ) & set ( symbols ) ) ) or ( K1 . is_Composite and ( set ( K1 . symbols ) & set ( symbols ) ) ) : <EOL> raise UnificationFailed ( "<STR_LIT>" % ( K0 , K1 , tuple ( symbols ) ) ) <EOL> return K0 . unify ( K1 ) <EOL> def unify ( K0 , K1 , symbols = None ) : <EOL> """<STR_LIT>""" <EOL> if symbols is not None : <EOL> return K0 . unify_with_symbols ( K1 , symbols ) <EOL> if K0 == K1 : <EOL> return K0 <EOL> if K0 . is_EX : <EOL> return K0 <EOL> if K1 . is_EX : <EOL> return K1 <EOL> if K0 . is_Composite or K1 . is_Composite : <EOL> K0_ground = K0 . dom if K0 . is_Composite else K0 <EOL> K1_ground = K1 . dom if K1 . is_Composite else K1 <EOL> K0_symbols = K0 . symbols if K0 . is_Composite else ( ) <EOL> K1_symbols = K1 . symbols if K1 . is_Composite else ( ) <EOL> domain = K0_ground . unify ( K1_ground ) <EOL> symbols = _unify_gens ( K0_symbols , K1_symbols ) <EOL> order = K0 . order if K0 . is_Composite else K1 . order <EOL> if ( ( K0 . is_FractionField and K1 . is_PolynomialRing or <EOL> K1 . is_FractionField and K0 . is_PolynomialRing ) and <EOL> ( not K0_ground . has_Field or not K1_ground . has_Field ) and domain . has_Field ) : <EOL> domain = domain . get_ring ( ) <EOL> if K0 . is_Composite and ( not K1 . is_Composite or K0 . is_FractionField or K1 . is_PolynomialRing ) : <EOL> cls = K0 . __class__ <EOL> else : <EOL> cls = K1 . __class__ <EOL> return cls ( domain , symbols , order ) <EOL> def mkinexact ( cls , K0 , K1 ) : <EOL> prec = max ( K0 . precision , K1 . precision ) <EOL> tol = max ( K0 . tolerance , K1 . tolerance ) <EOL> return cls ( prec = prec , tol = tol ) <EOL> if K0 . is_ComplexField and K1 . is_ComplexField : <EOL> return mkinexact ( K0 . __class__ , K0 , K1 ) <EOL> if K0 . is_ComplexField and K1 . is_RealField : <EOL> return mkinexact ( K0 . __class__ , K0 , K1 ) <EOL> if K0 . is_RealField and K1 . is_ComplexField : <EOL> return mkinexact ( K1 . __class__ , K1 , K0 ) <EOL> if K0 . is_RealField and K1 . is_RealField : <EOL> return mkinexact ( K0 . __class__ , K0 , K1 ) <EOL> if K0 . is_ComplexField or K0 . is_RealField : <EOL> return K0 <EOL> if K1 . is_ComplexField or K1 . is_RealField : <EOL> return K1 <EOL> if K0 . is_AlgebraicField and K1 . is_AlgebraicField : <EOL> return K0 . __class__ ( K0 . dom . unify ( K1 . dom ) , * _unify_gens ( K0 . orig_ext , K1 . orig_ext ) ) <EOL> elif K0 . is_AlgebraicField : <EOL> return K0 <EOL> elif K1 . is_AlgebraicField : <EOL> return K1 <EOL> if K0 . is_RationalField : <EOL> return K0 <EOL> if K1 . is_RationalField : <EOL> return K1 <EOL> if K0 . is_IntegerRing : <EOL> return K0 <EOL> if K1 . is_IntegerRing : <EOL> return K1 <EOL> if K0 . is_FiniteField and K1 . is_FiniteField : <EOL> return K0 . __class__ ( max ( K0 . mod , K1 . mod , key = default_sort_key ) ) <EOL> from sympy . polys . domains import EX <EOL> return EX <EOL> def __eq__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> return isinstance ( other , Domain ) and self . dtype == other . dtype <EOL> def __ne__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> return not self . __eq__ ( other ) <EOL> def map ( self , seq ) : <EOL> """<STR_LIT>""" <EOL> result = [ ] <EOL> for elt in seq : <EOL> if isinstance ( elt , list ) : <EOL> result . append ( self . map ( elt ) ) <EOL> else : <EOL> result . append ( self ( elt ) ) <EOL> return result <EOL> def get_ring ( self ) : <EOL> """<STR_LIT>""" <EOL> raise DomainError ( '<STR_LIT>' % self ) <EOL> def get_field ( self ) : <EOL> """<STR_LIT>""" <EOL> raise DomainError ( '<STR_LIT>' % self ) <EOL> def get_exact ( self ) : <EOL> """<STR_LIT>""" <EOL> return self <EOL> def __getitem__ ( self , symbols ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( symbols , '<STR_LIT>' ) : <EOL> return self . poly_ring ( * symbols ) <EOL> else : <EOL> return self . poly_ring ( symbols ) <EOL> def poly_ring ( self , * symbols , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> from sympy . polys . domains . polynomialring import PolynomialRing <EOL> return PolynomialRing ( self , symbols , kwargs . get ( "<STR_LIT>" , lex ) ) <EOL> def frac_field ( self , * symbols , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> from sympy . polys . domains . fractionfield import FractionField <EOL> return FractionField ( self , symbols , kwargs . get ( "<STR_LIT>" , lex ) ) <EOL> def old_poly_ring ( self , * symbols , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> from sympy . polys . domains . old_polynomialring import PolynomialRing <EOL> return PolynomialRing ( self , * symbols , ** kwargs ) <EOL> def old_frac_field ( self , * symbols , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> from sympy . polys . domains . old_fractionfield import FractionField <EOL> return FractionField ( self , * symbols , ** kwargs ) <EOL> def algebraic_field ( self , * extension ) : <EOL> """<STR_LIT>""" <EOL> raise DomainError ( "<STR_LIT>" % self ) <EOL> def inject ( self , * symbols ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def is_zero ( self , a ) : <EOL> """<STR_LIT>""" <EOL> return not a <EOL> def is_one ( self , a ) : <EOL> """<STR_LIT>""" <EOL> return a == self . one <EOL> def is_positive ( self , a ) : <EOL> """<STR_LIT>""" <EOL> return a > <NUM_LIT:0> <EOL> def is_negative ( self , a ) : <EOL> """<STR_LIT>""" <EOL> return a < <NUM_LIT:0> <EOL> def is_nonpositive ( self , a ) : <EOL> """<STR_LIT>""" <EOL> return a <= <NUM_LIT:0> <EOL> def is_nonnegative ( self , a ) : <EOL> """<STR_LIT>""" <EOL> return a >= <NUM_LIT:0> <EOL> def abs ( self , a ) : <EOL> """<STR_LIT>""" <EOL> return abs ( a ) <EOL> def neg ( self , a ) : <EOL> """<STR_LIT>""" <EOL> return - a <EOL> def pos ( self , a ) : <EOL> """<STR_LIT>""" <EOL> return + a <EOL> def add ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> return a + b <EOL> def sub ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> return a - b <EOL> def mul ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> return a * b <EOL> def pow ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> return a ** b <EOL> def exquo ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def quo ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def rem ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def div ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def invert ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def revert ( self , a ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def numer ( self , a ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def denom ( self , a ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def half_gcdex ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> s , t , h = self . gcdex ( a , b ) <EOL> return s , h <EOL> def gcdex ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def cofactors ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> gcd = self . gcd ( a , b ) <EOL> cfa = self . quo ( a , gcd ) <EOL> cfb = self . quo ( b , gcd ) <EOL> return gcd , cfa , cfb <EOL> def gcd ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def lcm ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def log ( self , a , b ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def sqrt ( self , a ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def evalf ( self , a , prec = None , ** options ) : <EOL> """<STR_LIT>""" <EOL> return self . to_sympy ( a ) . evalf ( prec , ** options ) <EOL> n = evalf <EOL> def real ( self , a ) : <EOL> return a <EOL> def imag ( self , a ) : <EOL> return self . zero <EOL> def almosteq ( self , a , b , tolerance = None ) : <EOL> """<STR_LIT>""" <EOL> return a == b <EOL> def characteristic ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' ) </s>
