<s> """<STR_LIT>""" <EOL> from __future__ import print_function , division <EOL> from sympy . polys . domains . domainelement import DomainElement <EOL> from mpmath . ctx_mp_python import PythonMPContext , _mpf , _mpc , _constant <EOL> from mpmath . libmp import ( MPZ_ONE , fzero , fone , finf , fninf , fnan , <EOL> round_nearest , mpf_mul , mpf_abs , mpf_lt , mpc_abs , repr_dps , int_types , <EOL> from_int , from_float , from_str , to_rational ) <EOL> from mpmath . rational import mpq <EOL> from sympy . utilities import public <EOL> @ public <EOL> class RealElement ( _mpf , DomainElement ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ '<STR_LIT>' ] <EOL> def _set_mpf ( self , val ) : <EOL> self . __mpf__ = val <EOL> _mpf_ = property ( lambda self : self . __mpf__ , _set_mpf ) <EOL> def parent ( self ) : <EOL> return self . context . _parent <EOL> @ public <EOL> class ComplexElement ( _mpc , DomainElement ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ '<STR_LIT>' ] <EOL> def _set_mpc ( self , val ) : <EOL> self . __mpc__ = val <EOL> _mpc_ = property ( lambda self : self . __mpc__ , _set_mpc ) <EOL> def parent ( self ) : <EOL> return self . context . _parent <EOL> new = object . __new__ <EOL> @ public <EOL> class MPContext ( PythonMPContext ) : <EOL> def __init__ ( ctx , prec = <NUM_LIT> , dps = None , tol = None ) : <EOL> ctx . _prec_rounding = [ prec , round_nearest ] <EOL> if dps is None : <EOL> ctx . _set_prec ( prec ) <EOL> else : <EOL> ctx . _set_dps ( dps ) <EOL> ctx . mpf = type ( '<STR_LIT>' , ( RealElement , ) , { } ) <EOL> ctx . mpc = type ( '<STR_LIT>' , ( ComplexElement , ) , { } ) <EOL> ctx . mpf . _ctxdata = [ ctx . mpf , new , ctx . _prec_rounding ] <EOL> ctx . mpc . _ctxdata = [ ctx . mpc , new , ctx . _prec_rounding ] <EOL> ctx . mpf . context = ctx <EOL> ctx . mpc . context = ctx <EOL> ctx . constant = type ( '<STR_LIT>' , ( _constant , ) , { } ) <EOL> ctx . constant . _ctxdata = [ ctx . mpf , new , ctx . _prec_rounding ] <EOL> ctx . constant . context = ctx <EOL> ctx . types = [ ctx . mpf , ctx . mpc , ctx . constant ] <EOL> ctx . trap_complex = True <EOL> ctx . pretty = True <EOL> if tol is None : <EOL> ctx . tol = ctx . _make_tol ( ) <EOL> elif tol is False : <EOL> ctx . tol = fzero <EOL> else : <EOL> ctx . tol = ctx . _convert_tol ( tol ) <EOL> ctx . tolerance = ctx . make_mpf ( ctx . tol ) <EOL> if not ctx . tolerance : <EOL> ctx . max_denom = <NUM_LIT> <EOL> else : <EOL> ctx . max_denom = int ( <NUM_LIT:1> / ctx . tolerance ) <EOL> ctx . zero = ctx . make_mpf ( fzero ) <EOL> ctx . one = ctx . make_mpf ( fone ) <EOL> ctx . j = ctx . make_mpc ( ( fzero , fone ) ) <EOL> ctx . inf = ctx . make_mpf ( finf ) <EOL> ctx . ninf = ctx . make_mpf ( fninf ) <EOL> ctx . nan = ctx . make_mpf ( fnan ) <EOL> def _make_tol ( ctx ) : <EOL> hundred = ( <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT:2> , <NUM_LIT:5> ) <EOL> eps = ( <NUM_LIT:0> , MPZ_ONE , <NUM_LIT:1> - ctx . prec , <NUM_LIT:1> ) <EOL> return mpf_mul ( hundred , eps ) <EOL> def make_tol ( ctx ) : <EOL> return ctx . make_mpf ( ctx . _make_tol ( ) ) <EOL> def _convert_tol ( ctx , tol ) : <EOL> if isinstance ( tol , int_types ) : <EOL> return from_int ( tol ) <EOL> if isinstance ( tol , float ) : <EOL> return from_float ( tol ) <EOL> if hasattr ( tol , "<STR_LIT>" ) : <EOL> return tol . _mpf_ <EOL> prec , rounding = ctx . _prec_rounding <EOL> if isinstance ( tol , basestring ) : <EOL> return from_str ( tol , prec , rounding ) <EOL> raise ValueError ( "<STR_LIT>" % tol ) <EOL> def _convert_fallback ( ctx , x , strings ) : <EOL> raise TypeError ( "<STR_LIT>" + repr ( x ) ) <EOL> @ property <EOL> def _repr_digits ( ctx ) : <EOL> return repr_dps ( ctx . _prec ) <EOL> @ property <EOL> def _str_digits ( ctx ) : <EOL> return ctx . _dps <EOL> def to_rational ( ctx , s , limit = True ) : <EOL> p , q = to_rational ( s . _mpf_ ) <EOL> if not limit or q <= ctx . max_denom : <EOL> return p , q <EOL> p0 , q0 , p1 , q1 = <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> <EOL> n , d = p , q <EOL> while True : <EOL> a = n // d <EOL> q2 = q0 + a * q1 <EOL> if q2 > ctx . max_denom : <EOL> break <EOL> p0 , q0 , p1 , q1 = p1 , q1 , p0 + a * p1 , q2 <EOL> n , d = d , n - a * d <EOL> k = ( ctx . max_denom - q0 ) // q1 <EOL> number = mpq ( p , q ) <EOL> bound1 = mpq ( p0 + k * p1 , q0 + k * q1 ) <EOL> bound2 = mpq ( p1 , q1 ) <EOL> if not bound2 or not bound1 : <EOL> return p , q <EOL> elif abs ( bound2 - number ) <= abs ( bound1 - number ) : <EOL> return bound2 . _mpq_ <EOL> else : <EOL> return bound1 . _mpq_ <EOL> def almosteq ( ctx , s , t , rel_eps = None , abs_eps = None ) : <EOL> t = ctx . convert ( t ) <EOL> if abs_eps is None and rel_eps is None : <EOL> rel_eps = abs_eps = ctx . tolerance or ctx . make_tol ( ) <EOL> if abs_eps is None : <EOL> abs_eps = ctx . convert ( rel_eps ) <EOL> elif rel_eps is None : <EOL> rel_eps = ctx . convert ( abs_eps ) <EOL> diff = abs ( s - t ) <EOL> if diff <= abs_eps : <EOL> return True <EOL> abss = abs ( s ) <EOL> abst = abs ( t ) <EOL> if abss < abst : <EOL> err = diff / abst <EOL> else : <EOL> err = diff / abss <EOL> return err <= rel_eps </s>
