<s> """<STR_LIT>""" <EOL> import sys <EOL> from . backend import MPZ , MPZ_ZERO , MPZ_ONE , MPZ_TWO , BACKEND <EOL> from . libmpf import ( round_floor , round_ceiling , round_down , round_up , <EOL> round_nearest , round_fast , bitcount , <EOL> bctable , normalize , normalize1 , reciprocal_rnd , rshift , lshift , giant_steps , <EOL> negative_rnd , <EOL> to_str , to_fixed , from_man_exp , from_float , to_float , from_int , to_int , <EOL> fzero , fone , ftwo , fhalf , finf , fninf , fnan , fnone , <EOL> mpf_abs , mpf_pos , mpf_neg , mpf_add , mpf_sub , mpf_mul , <EOL> mpf_div , mpf_mul_int , mpf_shift , mpf_sqrt , mpf_hypot , <EOL> mpf_rdiv_int , mpf_floor , mpf_ceil , mpf_nint , mpf_frac , <EOL> mpf_sign , mpf_hash , <EOL> ComplexResult <EOL> ) <EOL> from . libelefun import ( mpf_pi , mpf_exp , mpf_log , mpf_cos_sin , mpf_cosh_sinh , mpf_tan , mpf_pow_int , <EOL> mpf_log_hypot , <EOL> mpf_cos_sin_pi , mpf_phi , <EOL> mpf_cos , mpf_sin , mpf_cos_pi , mpf_sin_pi , <EOL> mpf_atan , mpf_atan2 , mpf_cosh , mpf_sinh , mpf_tanh , <EOL> mpf_asin , mpf_acos , mpf_acosh , mpf_nthroot , mpf_fibonacci <EOL> ) <EOL> mpc_one = fone , fzero <EOL> mpc_zero = fzero , fzero <EOL> mpc_two = ftwo , fzero <EOL> mpc_half = ( fhalf , fzero ) <EOL> _infs = ( finf , fninf ) <EOL> _infs_nan = ( finf , fninf , fnan ) <EOL> def mpc_is_inf ( z ) : <EOL> """<STR_LIT>""" <EOL> re , im = z <EOL> if re in _infs : return True <EOL> if im in _infs : return True <EOL> return False <EOL> def mpc_is_infnan ( z ) : <EOL> """<STR_LIT>""" <EOL> re , im = z <EOL> if re in _infs_nan : return True <EOL> if im in _infs_nan : return True <EOL> return False <EOL> def mpc_to_str ( z , dps , ** kwargs ) : <EOL> re , im = z <EOL> rs = to_str ( re , dps ) <EOL> if im [ <NUM_LIT:0> ] : <EOL> return rs + "<STR_LIT>" + to_str ( mpf_neg ( im ) , dps , ** kwargs ) + "<STR_LIT>" <EOL> else : <EOL> return rs + "<STR_LIT>" + to_str ( im , dps , ** kwargs ) + "<STR_LIT>" <EOL> def mpc_to_complex ( z , strict = False , rnd = round_fast ) : <EOL> re , im = z <EOL> return complex ( to_float ( re , strict , rnd ) , to_float ( im , strict , rnd ) ) <EOL> def mpc_hash ( z ) : <EOL> if sys . version >= "<STR_LIT>" : <EOL> re , im = z <EOL> h = mpf_hash ( re ) + sys . hash_info . imag * mpf_hash ( im ) <EOL> h = h % ( <NUM_LIT:2> ** sys . hash_info . width ) <EOL> return int ( h ) <EOL> else : <EOL> try : <EOL> return hash ( mpc_to_complex ( z , strict = True ) ) <EOL> except OverflowError : <EOL> return hash ( z ) <EOL> def mpc_conjugate ( z , prec , rnd = round_fast ) : <EOL> re , im = z <EOL> return re , mpf_neg ( im , prec , rnd ) <EOL> def mpc_is_nonzero ( z ) : <EOL> return z != mpc_zero <EOL> def mpc_add ( z , w , prec , rnd = round_fast ) : <EOL> a , b = z <EOL> c , d = w <EOL> return mpf_add ( a , c , prec , rnd ) , mpf_add ( b , d , prec , rnd ) <EOL> def mpc_add_mpf ( z , x , prec , rnd = round_fast ) : <EOL> a , b = z <EOL> return mpf_add ( a , x , prec , rnd ) , b <EOL> def mpc_sub ( z , w , prec = <NUM_LIT:0> , rnd = round_fast ) : <EOL> a , b = z <EOL> c , d = w <EOL> return mpf_sub ( a , c , prec , rnd ) , mpf_sub ( b , d , prec , rnd ) <EOL> def mpc_sub_mpf ( z , p , prec = <NUM_LIT:0> , rnd = round_fast ) : <EOL> a , b = z <EOL> return mpf_sub ( a , p , prec , rnd ) , b <EOL> def mpc_pos ( z , prec , rnd = round_fast ) : <EOL> a , b = z <EOL> return mpf_pos ( a , prec , rnd ) , mpf_pos ( b , prec , rnd ) <EOL> def mpc_neg ( z , prec = None , rnd = round_fast ) : <EOL> a , b = z <EOL> return mpf_neg ( a , prec , rnd ) , mpf_neg ( b , prec , rnd ) <EOL> def mpc_shift ( z , n ) : <EOL> a , b = z <EOL> return mpf_shift ( a , n ) , mpf_shift ( b , n ) <EOL> def mpc_abs ( z , prec , rnd = round_fast ) : <EOL> """<STR_LIT>""" <EOL> a , b = z <EOL> return mpf_hypot ( a , b , prec , rnd ) <EOL> def mpc_arg ( z , prec , rnd = round_fast ) : <EOL> """<STR_LIT>""" <EOL> a , b = z <EOL> return mpf_atan2 ( b , a , prec , rnd ) <EOL> def mpc_floor ( z , prec , rnd = round_fast ) : <EOL> a , b = z <EOL> return mpf_floor ( a , prec , rnd ) , mpf_floor ( b , prec , rnd ) <EOL> def mpc_ceil ( z , prec , rnd = round_fast ) : <EOL> a , b = z <EOL> return mpf_ceil ( a , prec , rnd ) , mpf_ceil ( b , prec , rnd ) <EOL> def mpc_nint ( z , prec , rnd = round_fast ) : <EOL> a , b = z <EOL> return mpf_nint ( a , prec , rnd ) , mpf_nint ( b , prec , rnd ) <EOL> def mpc_frac ( z , prec , rnd = round_fast ) : <EOL> a , b = z <EOL> return mpf_frac ( a , prec , rnd ) , mpf_frac ( b , prec , rnd ) <EOL> def mpc_mul ( z , w , prec , rnd = round_fast ) : <EOL> """<STR_LIT>""" <EOL> a , b = z <EOL> c , d = w <EOL> p = mpf_mul ( a , c ) <EOL> q = mpf_mul ( b , d ) <EOL> r = mpf_mul ( a , d ) <EOL> s = mpf_mul ( b , c ) <EOL> re = mpf_sub ( p , q , prec , rnd ) <EOL> im = mpf_add ( r , s , prec , rnd ) <EOL> return re , im <EOL> def mpc_square ( z , prec , rnd = round_fast ) : <EOL> a , b = z <EOL> p = mpf_mul ( a , a ) <EOL> q = mpf_mul ( b , b ) <EOL> r = mpf_mul ( a , b , prec , rnd ) <EOL> re = mpf_sub ( p , q , prec , rnd ) <EOL> im = mpf_shift ( r , <NUM_LIT:1> ) <EOL> return re , im <EOL> def mpc_mul_mpf ( z , p , prec , rnd = round_fast ) : <EOL> a , b = z <EOL> re = mpf_mul ( a , p , prec , rnd ) <EOL> im = mpf_mul ( b , p , prec , rnd ) <EOL> return re , im <EOL> def mpc_mul_imag_mpf ( z , x , prec , rnd = round_fast ) : <EOL> """<STR_LIT>""" <EOL> a , b = z <EOL> re = mpf_neg ( mpf_mul ( b , x , prec , rnd ) ) <EOL> im = mpf_mul ( a , x , prec , rnd ) <EOL> return re , im <EOL> def mpc_mul_int ( z , n , prec , rnd = round_fast ) : <EOL> a , b = z <EOL> re = mpf_mul_int ( a , n , prec , rnd ) <EOL> im = mpf_mul_int ( b , n , prec , rnd ) <EOL> return re , im <EOL> def mpc_div ( z , w , prec , rnd = round_fast ) : <EOL> a , b = z <EOL> c , d = w <EOL> wp = prec + <NUM_LIT:10> <EOL> mag = mpf_add ( mpf_mul ( c , c ) , mpf_mul ( d , d ) , wp ) <EOL> t = mpf_add ( mpf_mul ( a , c ) , mpf_mul ( b , d ) , wp ) <EOL> u = mpf_sub ( mpf_mul ( b , c ) , mpf_mul ( a , d ) , wp ) <EOL> return mpf_div ( t , mag , prec , rnd ) , mpf_div ( u , mag , prec , rnd ) <EOL> def mpc_div_mpf ( z , p , prec , rnd = round_fast ) : <EOL> """<STR_LIT>""" <EOL> a , b = z <EOL> re = mpf_div ( a , p , prec , rnd ) <EOL> im = mpf_div ( b , p , prec , rnd ) <EOL> return re , im <EOL> def mpc_reciprocal ( z , prec , rnd = round_fast ) : <EOL> """<STR_LIT>""" <EOL> a , b = z <EOL> m = mpf_add ( mpf_mul ( a , a ) , mpf_mul ( b , b ) , prec + <NUM_LIT:10> ) <EOL> re = mpf_div ( a , m , prec , rnd ) <EOL> im = mpf_neg ( mpf_div ( b , m , prec , rnd ) ) <EOL> return re , im <EOL> def mpc_mpf_div ( p , z , prec , rnd = round_fast ) : <EOL> """<STR_LIT>""" <EOL> a , b = z <EOL> m = mpf_add ( mpf_mul ( a , a ) , mpf_mul ( b , b ) , prec + <NUM_LIT:10> ) <EOL> re = mpf_div ( mpf_mul ( a , p ) , m , prec , rnd ) <EOL> im = mpf_div ( mpf_neg ( mpf_mul ( b , p ) ) , m , prec , rnd ) <EOL> return re , im <EOL> def complex_int_pow ( a , b , n ) : <EOL> """<STR_LIT>""" <EOL> wre = <NUM_LIT:1> <EOL> wim = <NUM_LIT:0> <EOL> while n : <EOL> if n & <NUM_LIT:1> : <EOL> wre , wim = wre * a - wim * b , wim * a + wre * b <EOL> n -= <NUM_LIT:1> <EOL> a , b = a * a - b * b , <NUM_LIT:2> * a * b <EOL> n //= <NUM_LIT:2> <EOL> return wre , wim <EOL> def mpc_pow ( z , w , prec , rnd = round_fast ) : <EOL> if w [ <NUM_LIT:1> ] == fzero : <EOL> return mpc_pow_mpf ( z , w [ <NUM_LIT:0> ] , prec , rnd ) <EOL> return mpc_exp ( mpc_mul ( mpc_log ( z , prec + <NUM_LIT:10> ) , w , prec + <NUM_LIT:10> ) , prec , rnd ) <EOL> def mpc_pow_mpf ( z , p , prec , rnd = round_fast ) : <EOL> psign , pman , pexp , pbc = p <EOL> if pexp >= <NUM_LIT:0> : <EOL> return mpc_pow_int ( z , ( - <NUM_LIT:1> ) ** psign * ( pman << pexp ) , prec , rnd ) <EOL> if pexp == - <NUM_LIT:1> : <EOL> sqrtz = mpc_sqrt ( z , prec + <NUM_LIT:10> ) <EOL> return mpc_pow_int ( sqrtz , ( - <NUM_LIT:1> ) ** psign * pman , prec , rnd ) <EOL> return mpc_exp ( mpc_mul_mpf ( mpc_log ( z , prec + <NUM_LIT:10> ) , p , prec + <NUM_LIT:10> ) , prec , rnd ) <EOL> def mpc_pow_int ( z , n , prec , rnd = round_fast ) : <EOL> a , b = z <EOL> if b == fzero : <EOL> return mpf_pow_int ( a , n , prec , rnd ) , fzero <EOL> if a == fzero : <EOL> v = mpf_pow_int ( b , n , prec , rnd ) <EOL> n %= <NUM_LIT:4> <EOL> if n == <NUM_LIT:0> : <EOL> return v , fzero <EOL> elif n == <NUM_LIT:1> : <EOL> return fzero , v <EOL> elif n == <NUM_LIT:2> : <EOL> return mpf_neg ( v ) , fzero <EOL> elif n == <NUM_LIT:3> : <EOL> return fzero , mpf_neg ( v ) <EOL> if n == <NUM_LIT:0> : return mpc_one <EOL> if n == <NUM_LIT:1> : return mpc_pos ( z , prec , rnd ) <EOL> if n == <NUM_LIT:2> : return mpc_square ( z , prec , rnd ) <EOL> if n == - <NUM_LIT:1> : return mpc_reciprocal ( z , prec , rnd ) <EOL> if n < <NUM_LIT:0> : return mpc_reciprocal ( mpc_pow_int ( z , - n , prec + <NUM_LIT:4> ) , prec , rnd ) <EOL> asign , aman , aexp , abc = a <EOL> bsign , bman , bexp , bbc = b <EOL> if asign : aman = - aman <EOL> if bsign : bman = - bman <EOL> de = aexp - bexp <EOL> abs_de = abs ( de ) <EOL> exact_size = n * ( abs_de + max ( abc , bbc ) ) <EOL> if exact_size < <NUM_LIT> : <EOL> if de > <NUM_LIT:0> : <EOL> aman <<= de <EOL> aexp = bexp <EOL> else : <EOL> bman <<= ( - de ) <EOL> bexp = aexp <EOL> re , im = complex_int_pow ( aman , bman , n ) <EOL> re = from_man_exp ( re , int ( n * aexp ) , prec , rnd ) <EOL> im = from_man_exp ( im , int ( n * bexp ) , prec , rnd ) <EOL> return re , im <EOL> return mpc_exp ( mpc_mul_int ( mpc_log ( z , prec + <NUM_LIT:10> ) , n , prec + <NUM_LIT:10> ) , prec , rnd ) <EOL> def mpc_sqrt ( z , prec , rnd = round_fast ) : <EOL> """<STR_LIT>""" <EOL> a , b = z <EOL> if b == fzero : <EOL> if a == fzero : <EOL> return ( a , b ) <EOL> if a [ <NUM_LIT:0> ] : <EOL> im = mpf_sqrt ( mpf_neg ( a ) , prec , rnd ) <EOL> return ( fzero , im ) <EOL> else : <EOL> re = mpf_sqrt ( a , prec , rnd ) <EOL> return ( re , fzero ) <EOL> wp = prec + <NUM_LIT:20> <EOL> if not a [ <NUM_LIT:0> ] : <EOL> t = mpf_add ( mpc_abs ( ( a , b ) , wp ) , a , wp ) <EOL> u = mpf_shift ( t , - <NUM_LIT:1> ) <EOL> re = mpf_sqrt ( u , prec , rnd ) <EOL> v = mpf_shift ( t , <NUM_LIT:1> ) <EOL> w = mpf_sqrt ( v , wp ) <EOL> im = mpf_div ( b , w , prec , rnd ) <EOL> else : <EOL> t = mpf_sub ( mpc_abs ( ( a , b ) , wp ) , a , wp ) <EOL> u = mpf_shift ( t , - <NUM_LIT:1> ) <EOL> im = mpf_sqrt ( u , prec , rnd ) <EOL> v = mpf_shift ( t , <NUM_LIT:1> ) <EOL> w = mpf_sqrt ( v , wp ) <EOL> re = mpf_div ( b , w , prec , rnd ) <EOL> if b [ <NUM_LIT:0> ] : <EOL> re = mpf_neg ( re ) <EOL> im = mpf_neg ( im ) <EOL> return re , im <EOL> def mpc_nthroot_fixed ( a , b , n , prec ) : <EOL> start = <NUM_LIT:50> <EOL> a1 = int ( rshift ( a , prec - n * start ) ) <EOL> b1 = int ( rshift ( b , prec - n * start ) ) <EOL> try : <EOL> r = ( a1 + <NUM_LIT> * b1 ) ** ( <NUM_LIT:1.0> / n ) <EOL> re = r . real <EOL> im = r . imag <EOL> re = MPZ ( int ( re ) ) <EOL> im = MPZ ( int ( im ) ) <EOL> except OverflowError : <EOL> a1 = from_int ( a1 , start ) <EOL> b1 = from_int ( b1 , start ) <EOL> fn = from_int ( n ) <EOL> nth = mpf_rdiv_int ( <NUM_LIT:1> , fn , start ) <EOL> re , im = mpc_pow ( ( a1 , b1 ) , ( nth , fzero ) , start ) <EOL> re = to_int ( re ) <EOL> im = to_int ( im ) <EOL> extra = <NUM_LIT:10> <EOL> prevp = start <EOL> extra1 = n <EOL> for p in giant_steps ( start , prec + extra ) : <EOL> re2 , im2 = complex_int_pow ( re , im , n - <NUM_LIT:1> ) <EOL> re2 = rshift ( re2 , ( n - <NUM_LIT:1> ) * prevp - p - extra1 ) <EOL> im2 = rshift ( im2 , ( n - <NUM_LIT:1> ) * prevp - p - extra1 ) <EOL> r4 = ( re2 * re2 + im2 * im2 ) >> ( p + extra1 ) <EOL> ap = rshift ( a , prec - p ) <EOL> bp = rshift ( b , prec - p ) <EOL> rec = ( ap * re2 + bp * im2 ) >> p <EOL> imc = ( - ap * im2 + bp * re2 ) >> p <EOL> reb = ( rec << p ) // r4 <EOL> imb = ( imc << p ) // r4 <EOL> re = ( reb + ( n - <NUM_LIT:1> ) * lshift ( re , p - prevp ) ) // n <EOL> im = ( imb + ( n - <NUM_LIT:1> ) * lshift ( im , p - prevp ) ) // n <EOL> prevp = p <EOL> return re , im <EOL> def mpc_nthroot ( z , n , prec , rnd = round_fast ) : <EOL> """<STR_LIT>""" <EOL> a , b = z <EOL> if a [ <NUM_LIT:0> ] == <NUM_LIT:0> and b == fzero : <EOL> re = mpf_nthroot ( a , n , prec , rnd ) <EOL> return ( re , fzero ) <EOL> if n < <NUM_LIT:2> : <EOL> if n == <NUM_LIT:0> : <EOL> return mpc_one <EOL> if n == <NUM_LIT:1> : <EOL> return mpc_pos ( ( a , b ) , prec , rnd ) <EOL> if n == - <NUM_LIT:1> : <EOL> return mpc_div ( mpc_one , ( a , b ) , prec , rnd ) <EOL> inverse = mpc_nthroot ( ( a , b ) , - n , prec + <NUM_LIT:5> , reciprocal_rnd [ rnd ] ) <EOL> return mpc_div ( mpc_one , inverse , prec , rnd ) <EOL> if n <= <NUM_LIT:20> : <EOL> prec2 = int ( <NUM_LIT> * ( prec + <NUM_LIT:10> ) ) <EOL> asign , aman , aexp , abc = a <EOL> bsign , bman , bexp , bbc = b <EOL> pf = mpc_abs ( ( a , b ) , prec ) <EOL> if pf [ - <NUM_LIT:2> ] + pf [ - <NUM_LIT:1> ] > - <NUM_LIT:10> and pf [ - <NUM_LIT:2> ] + pf [ - <NUM_LIT:1> ] < prec : <EOL> af = to_fixed ( a , prec2 ) <EOL> bf = to_fixed ( b , prec2 ) <EOL> re , im = mpc_nthroot_fixed ( af , bf , n , prec2 ) <EOL> extra = <NUM_LIT:10> <EOL> re = from_man_exp ( re , - prec2 - extra , prec2 , rnd ) <EOL> im = from_man_exp ( im , - prec2 - extra , prec2 , rnd ) <EOL> return re , im <EOL> fn = from_int ( n ) <EOL> prec2 = prec + <NUM_LIT:10> + <NUM_LIT:10> <EOL> nth = mpf_rdiv_int ( <NUM_LIT:1> , fn , prec2 ) <EOL> re , im = mpc_pow ( ( a , b ) , ( nth , fzero ) , prec2 , rnd ) <EOL> re = normalize ( re [ <NUM_LIT:0> ] , re [ <NUM_LIT:1> ] , re [ <NUM_LIT:2> ] , re [ <NUM_LIT:3> ] , prec , rnd ) <EOL> im = normalize ( im [ <NUM_LIT:0> ] , im [ <NUM_LIT:1> ] , im [ <NUM_LIT:2> ] , im [ <NUM_LIT:3> ] , prec , rnd ) <EOL> return re , im <EOL> def mpc_cbrt ( z , prec , rnd = round_fast ) : <EOL> """<STR_LIT>""" <EOL> return mpc_nthroot ( z , <NUM_LIT:3> , prec , rnd ) <EOL> def mpc_exp ( z , prec , rnd = round_fast ) : <EOL> """<STR_LIT>""" <EOL> a , b = z <EOL> if a == fzero : <EOL> return mpf_cos_sin ( b , prec , rnd ) <EOL> if b == fzero : <EOL> return mpf_exp ( a , prec , rnd ) , fzero <EOL> mag = mpf_exp ( a , prec + <NUM_LIT:4> , rnd ) <EOL> c , s = mpf_cos_sin ( b , prec + <NUM_LIT:4> , rnd ) <EOL> re = mpf_mul ( mag , c , prec , rnd ) <EOL> im = mpf_mul ( mag , s , prec , rnd ) <EOL> return re , im <EOL> def mpc_log ( z , prec , rnd = round_fast ) : <EOL> re = mpf_log_hypot ( z [ <NUM_LIT:0> ] , z [ <NUM_LIT:1> ] , prec , rnd ) <EOL> im = mpc_arg ( z , prec , rnd ) <EOL> return re , im <EOL> def mpc_cos ( z , prec , rnd = round_fast ) : <EOL> """<STR_LIT>""" <EOL> a , b = z <EOL> if b == fzero : <EOL> return mpf_cos ( a , prec , rnd ) , fzero <EOL> if a == fzero : <EOL> return mpf_cosh ( b , prec , rnd ) , fzero <EOL> wp = prec + <NUM_LIT:6> <EOL> c , s = mpf_cos_sin ( a , wp ) <EOL> ch , sh = mpf_cosh_sinh ( b , wp ) <EOL> re = mpf_mul ( c , ch , prec , rnd ) <EOL> im = mpf_mul ( s , sh , prec , rnd ) <EOL> return re , mpf_neg ( im ) <EOL> def mpc_sin ( z , prec , rnd = round_fast ) : <EOL> """<STR_LIT>""" <EOL> a , b = z <EOL> if b == fzero : <EOL> return mpf_sin ( a , prec , rnd ) , fzero <EOL> if a == fzero : <EOL> return fzero , mpf_sinh ( b , prec , rnd ) <EOL> wp = prec + <NUM_LIT:6> <EOL> c , s = mpf_cos_sin ( a , wp ) <EOL> ch , sh = mpf_cosh_sinh ( b , wp ) <EOL> re = mpf_mul ( s , ch , prec , rnd ) <EOL> im = mpf_mul ( c , sh , prec , rnd ) <EOL> return re , im <EOL> def mpc_tan ( z , prec , rnd = round_fast ) : <EOL> """<STR_LIT>""" <EOL> a , b = z <EOL> asign , aman , aexp , abc = a <EOL> bsign , bman , bexp , bbc = b <EOL> if b == fzero : return mpf_tan ( a , prec , rnd ) , fzero <EOL> if a == fzero : return fzero , mpf_tanh ( b , prec , rnd ) <EOL> wp = prec + <NUM_LIT:15> <EOL> a = mpf_shift ( a , <NUM_LIT:1> ) <EOL> b = mpf_shift ( b , <NUM_LIT:1> ) <EOL> c , s = mpf_cos_sin ( a , wp ) <EOL> ch , sh = mpf_cosh_sinh ( b , wp ) <EOL> mag = mpf_add ( c , ch , wp ) <EOL> re = mpf_div ( s , mag , prec , rnd ) <EOL> im = mpf_div ( sh , mag , prec , rnd ) <EOL> return re , im <EOL> def mpc_cos_pi ( z , prec , rnd = round_fast ) : <EOL> a , b = z <EOL> if b == fzero : <EOL> return mpf_cos_pi ( a , prec , rnd ) , fzero <EOL> b = mpf_mul ( b , mpf_pi ( prec + <NUM_LIT:5> ) , prec + <NUM_LIT:5> ) <EOL> if a == fzero : <EOL> return mpf_cosh ( b , prec , rnd ) , fzero <EOL> wp = prec + <NUM_LIT:6> <EOL> c , s = mpf_cos_sin_pi ( a , wp ) <EOL> ch , sh = mpf_cosh_sinh ( b , wp ) <EOL> re = mpf_mul ( c , ch , prec , rnd ) <EOL> im = mpf_mul ( s , sh , prec , rnd ) <EOL> return re , mpf_neg ( im ) <EOL> def mpc_sin_pi ( z , prec , rnd = round_fast ) : <EOL> a , b = z <EOL> if b == fzero : <EOL> return mpf_sin_pi ( a , prec , rnd ) , fzero <EOL> b = mpf_mul ( b , mpf_pi ( prec + <NUM_LIT:5> ) , prec + <NUM_LIT:5> ) <EOL> if a == fzero : <EOL> return fzero , mpf_sinh ( b , prec , rnd ) <EOL> wp = prec + <NUM_LIT:6> <EOL> c , s = mpf_cos_sin_pi ( a , wp ) <EOL> ch , sh = mpf_cosh_sinh ( b , wp ) <EOL> re = mpf_mul ( s , ch , prec , rnd ) <EOL> im = mpf_mul ( c , sh , prec , rnd ) <EOL> return re , im <EOL> def mpc_cos_sin ( z , prec , rnd = round_fast ) : <EOL> a , b = z <EOL> if a == fzero : <EOL> ch , sh = mpf_cosh_sinh ( b , prec , rnd ) <EOL> return ( ch , fzero ) , ( fzero , sh ) <EOL> if b == fzero : <EOL> c , s = mpf_cos_sin ( a , prec , rnd ) <EOL> return ( c , fzero ) , ( s , fzero ) <EOL> wp = prec + <NUM_LIT:6> <EOL> c , s = mpf_cos_sin ( a , wp ) <EOL> ch , sh = mpf_cosh_sinh ( b , wp ) <EOL> cre = mpf_mul ( c , ch , prec , rnd ) <EOL> cim = mpf_mul ( s , sh , prec , rnd ) <EOL> sre = mpf_mul ( s , ch , prec , rnd ) <EOL> sim = mpf_mul ( c , sh , prec , rnd ) <EOL> return ( cre , mpf_neg ( cim ) ) , ( sre , sim ) <EOL> def mpc_cos_sin_pi ( z , prec , rnd = round_fast ) : <EOL> a , b = z <EOL> if b == fzero : <EOL> c , s = mpf_cos_sin_pi ( a , prec , rnd ) <EOL> return ( c , fzero ) , ( s , fzero ) <EOL> b = mpf_mul ( b , mpf_pi ( prec + <NUM_LIT:5> ) , prec + <NUM_LIT:5> ) <EOL> if a == fzero : <EOL> ch , sh = mpf_cosh_sinh ( b , prec , rnd ) <EOL> return ( ch , fzero ) , ( fzero , sh ) <EOL> wp = prec + <NUM_LIT:6> <EOL> c , s = mpf_cos_sin_pi ( a , wp ) <EOL> ch , sh = mpf_cosh_sinh ( b , wp ) <EOL> cre = mpf_mul ( c , ch , prec , rnd ) <EOL> cim = mpf_mul ( s , sh , prec , rnd ) <EOL> sre = mpf_mul ( s , ch , prec , rnd ) <EOL> sim = mpf_mul ( c , sh , prec , rnd ) <EOL> return ( cre , mpf_neg ( cim ) ) , ( sre , sim ) <EOL> def mpc_cosh ( z , prec , rnd = round_fast ) : <EOL> """<STR_LIT>""" <EOL> a , b = z <EOL> return mpc_cos ( ( b , mpf_neg ( a ) ) , prec , rnd ) <EOL> def mpc_sinh ( z , prec , rnd = round_fast ) : <EOL> """<STR_LIT>""" <EOL> a , b = z <EOL> b , a = mpc_sin ( ( b , a ) , prec , rnd ) <EOL> return a , b <EOL> def mpc_tanh ( z , prec , rnd = round_fast ) : <EOL> """<STR_LIT>""" <EOL> a , b = z <EOL> b , a = mpc_tan ( ( b , a ) , prec , rnd ) <EOL> return a , b <EOL> def mpc_atan ( z , prec , rnd = round_fast ) : <EOL> a , b = z <EOL> wp = prec + <NUM_LIT:15> <EOL> x = mpf_add ( fone , b , wp ) , mpf_neg ( a ) <EOL> y = mpf_sub ( fone , b , wp ) , a <EOL> l1 = mpc_log ( x , wp ) <EOL> l2 = mpc_log ( y , wp ) <EOL> a , b = mpc_sub ( l1 , l2 , prec , rnd ) <EOL> v = mpf_neg ( mpf_shift ( b , - <NUM_LIT:1> ) ) , mpf_shift ( a , - <NUM_LIT:1> ) <EOL> if v [ <NUM_LIT:1> ] == fnan and mpc_is_inf ( z ) : <EOL> v = ( v [ <NUM_LIT:0> ] , fzero ) <EOL> return v <EOL> beta_crossover = from_float ( <NUM_LIT> ) <EOL> alpha_crossover = from_float ( <NUM_LIT> ) <EOL> def acos_asin ( z , prec , rnd , n ) : <EOL> """<STR_LIT>""" <EOL> a , b = z <EOL> wp = prec + <NUM_LIT:10> <EOL> if b == fzero : <EOL> am = mpf_sub ( fone , mpf_abs ( a ) , wp ) <EOL> if not am [ <NUM_LIT:0> ] : <EOL> if n == <NUM_LIT:0> : <EOL> return mpf_acos ( a , prec , rnd ) , fzero <EOL> else : <EOL> return mpf_asin ( a , prec , rnd ) , fzero <EOL> else : <EOL> if a [ <NUM_LIT:0> ] : <EOL> pi = mpf_pi ( prec , rnd ) <EOL> c = mpf_acosh ( mpf_neg ( a ) , prec , rnd ) <EOL> if n == <NUM_LIT:0> : <EOL> return pi , mpf_neg ( c ) <EOL> else : <EOL> return mpf_neg ( mpf_shift ( pi , - <NUM_LIT:1> ) ) , c <EOL> else : <EOL> c = mpf_acosh ( a , prec , rnd ) <EOL> if n == <NUM_LIT:0> : <EOL> return fzero , c <EOL> else : <EOL> pi = mpf_pi ( prec , rnd ) <EOL> return mpf_shift ( pi , - <NUM_LIT:1> ) , mpf_neg ( c ) <EOL> asign = bsign = <NUM_LIT:0> <EOL> if a [ <NUM_LIT:0> ] : <EOL> a = mpf_neg ( a ) <EOL> asign = <NUM_LIT:1> <EOL> if b [ <NUM_LIT:0> ] : <EOL> b = mpf_neg ( b ) <EOL> bsign = <NUM_LIT:1> <EOL> am = mpf_sub ( fone , a , wp ) <EOL> ap = mpf_add ( fone , a , wp ) <EOL> r = mpf_hypot ( ap , b , wp ) <EOL> s = mpf_hypot ( am , b , wp ) <EOL> alpha = mpf_shift ( mpf_add ( r , s , wp ) , - <NUM_LIT:1> ) <EOL> beta = mpf_div ( a , alpha , wp ) <EOL> b2 = mpf_mul ( b , b , wp ) <EOL> if not mpf_sub ( beta_crossover , beta , wp ) [ <NUM_LIT:0> ] : <EOL> if n == <NUM_LIT:0> : <EOL> re = mpf_acos ( beta , wp ) <EOL> else : <EOL> re = mpf_asin ( beta , wp ) <EOL> else : <EOL> Ax = mpf_add ( alpha , a , wp ) <EOL> if not am [ <NUM_LIT:0> ] : <EOL> c = mpf_div ( b2 , mpf_add ( r , ap , wp ) , wp ) <EOL> d = mpf_add ( s , am , wp ) <EOL> re = mpf_shift ( mpf_mul ( Ax , mpf_add ( c , d , wp ) , wp ) , - <NUM_LIT:1> ) <EOL> if n == <NUM_LIT:0> : <EOL> re = mpf_atan ( mpf_div ( mpf_sqrt ( re , wp ) , a , wp ) , wp ) <EOL> else : <EOL> re = mpf_atan ( mpf_div ( a , mpf_sqrt ( re , wp ) , wp ) , wp ) <EOL> else : <EOL> c = mpf_div ( Ax , mpf_add ( r , ap , wp ) , wp ) <EOL> d = mpf_div ( Ax , mpf_sub ( s , am , wp ) , wp ) <EOL> re = mpf_shift ( mpf_add ( c , d , wp ) , - <NUM_LIT:1> ) <EOL> re = mpf_mul ( b , mpf_sqrt ( re , wp ) , wp ) <EOL> if n == <NUM_LIT:0> : <EOL> re = mpf_atan ( mpf_div ( re , a , wp ) , wp ) <EOL> else : <EOL> re = mpf_atan ( mpf_div ( a , re , wp ) , wp ) <EOL> if not mpf_sub ( alpha_crossover , alpha , wp ) [ <NUM_LIT:0> ] : <EOL> c1 = mpf_div ( b2 , mpf_add ( r , ap , wp ) , wp ) <EOL> if mpf_neg ( am ) [ <NUM_LIT:0> ] : <EOL> c2 = mpf_add ( s , am , wp ) <EOL> c2 = mpf_div ( b2 , c2 , wp ) <EOL> Am1 = mpf_shift ( mpf_add ( c1 , c2 , wp ) , - <NUM_LIT:1> ) <EOL> else : <EOL> c2 = mpf_sub ( s , am , wp ) <EOL> Am1 = mpf_shift ( mpf_add ( c1 , c2 , wp ) , - <NUM_LIT:1> ) <EOL> im = mpf_mul ( Am1 , mpf_add ( alpha , fone , wp ) , wp ) <EOL> im = mpf_log ( mpf_add ( fone , mpf_add ( Am1 , mpf_sqrt ( im , wp ) , wp ) , wp ) , wp ) <EOL> else : <EOL> im = mpf_sqrt ( mpf_sub ( mpf_mul ( alpha , alpha , wp ) , fone , wp ) , wp ) <EOL> im = mpf_log ( mpf_add ( alpha , im , wp ) , wp ) <EOL> if asign : <EOL> if n == <NUM_LIT:0> : <EOL> re = mpf_sub ( mpf_pi ( wp ) , re , wp ) <EOL> else : <EOL> re = mpf_neg ( re ) <EOL> if not bsign and n == <NUM_LIT:0> : <EOL> im = mpf_neg ( im ) <EOL> if bsign and n == <NUM_LIT:1> : <EOL> im = mpf_neg ( im ) <EOL> re = normalize ( re [ <NUM_LIT:0> ] , re [ <NUM_LIT:1> ] , re [ <NUM_LIT:2> ] , re [ <NUM_LIT:3> ] , prec , rnd ) <EOL> im = normalize ( im [ <NUM_LIT:0> ] , im [ <NUM_LIT:1> ] , im [ <NUM_LIT:2> ] , im [ <NUM_LIT:3> ] , prec , rnd ) <EOL> return re , im <EOL> def mpc_acos ( z , prec , rnd = round_fast ) : <EOL> return acos_asin ( z , prec , rnd , <NUM_LIT:0> ) <EOL> def mpc_asin ( z , prec , rnd = round_fast ) : <EOL> return acos_asin ( z , prec , rnd , <NUM_LIT:1> ) <EOL> def mpc_asinh ( z , prec , rnd = round_fast ) : <EOL> a , b = z <EOL> a , b = mpc_asin ( ( b , mpf_neg ( a ) ) , prec , rnd ) <EOL> return mpf_neg ( b ) , a <EOL> def mpc_acosh ( z , prec , rnd = round_fast ) : <EOL> a , b = mpc_acos ( z , prec , rnd ) <EOL> if b [ <NUM_LIT:0> ] or b == fzero : <EOL> return mpf_neg ( b ) , a <EOL> else : <EOL> return b , mpf_neg ( a ) <EOL> def mpc_atanh ( z , prec , rnd = round_fast ) : <EOL> wp = prec + <NUM_LIT:15> <EOL> a = mpc_add ( z , mpc_one , wp ) <EOL> b = mpc_sub ( mpc_one , z , wp ) <EOL> a = mpc_log ( a , wp ) <EOL> b = mpc_log ( b , wp ) <EOL> v = mpc_shift ( mpc_sub ( a , b , wp ) , - <NUM_LIT:1> ) <EOL> if v [ <NUM_LIT:0> ] == fnan and mpc_is_inf ( z ) : <EOL> v = ( fzero , v [ <NUM_LIT:1> ] ) <EOL> return v <EOL> def mpc_fibonacci ( z , prec , rnd = round_fast ) : <EOL> re , im = z <EOL> if im == fzero : <EOL> return ( mpf_fibonacci ( re , prec , rnd ) , fzero ) <EOL> size = max ( abs ( re [ <NUM_LIT:2> ] + re [ <NUM_LIT:3> ] ) , abs ( re [ <NUM_LIT:2> ] + re [ <NUM_LIT:3> ] ) ) <EOL> wp = prec + size + <NUM_LIT:20> <EOL> a = mpf_phi ( wp ) <EOL> b = mpf_add ( mpf_shift ( a , <NUM_LIT:1> ) , fnone , wp ) <EOL> u = mpc_pow ( ( a , fzero ) , z , wp ) <EOL> v = mpc_cos_pi ( z , wp ) <EOL> v = mpc_div ( v , u , wp ) <EOL> u = mpc_sub ( u , v , wp ) <EOL> u = mpc_div_mpf ( u , b , prec , rnd ) <EOL> return u <EOL> def mpf_expj ( x , prec , rnd = '<STR_LIT:f>' ) : <EOL> raise ComplexResult <EOL> def mpc_expj ( z , prec , rnd = '<STR_LIT:f>' ) : <EOL> re , im = z <EOL> if im == fzero : <EOL> return mpf_cos_sin ( re , prec , rnd ) <EOL> if re == fzero : <EOL> return mpf_exp ( mpf_neg ( im ) , prec , rnd ) , fzero <EOL> ey = mpf_exp ( mpf_neg ( im ) , prec + <NUM_LIT:10> ) <EOL> c , s = mpf_cos_sin ( re , prec + <NUM_LIT:10> ) <EOL> re = mpf_mul ( ey , c , prec , rnd ) <EOL> im = mpf_mul ( ey , s , prec , rnd ) <EOL> return re , im <EOL> def mpf_expjpi ( x , prec , rnd = '<STR_LIT:f>' ) : <EOL> raise ComplexResult <EOL> def mpc_expjpi ( z , prec , rnd = '<STR_LIT:f>' ) : <EOL> re , im = z <EOL> if im == fzero : <EOL> return mpf_cos_sin_pi ( re , prec , rnd ) <EOL> sign , man , exp , bc = im <EOL> wp = prec + <NUM_LIT:10> <EOL> if man : <EOL> wp += max ( <NUM_LIT:0> , exp + bc ) <EOL> im = mpf_neg ( mpf_mul ( mpf_pi ( wp ) , im , wp ) ) <EOL> if re == fzero : <EOL> return mpf_exp ( im , prec , rnd ) , fzero <EOL> ey = mpf_exp ( im , prec + <NUM_LIT:10> ) <EOL> c , s = mpf_cos_sin_pi ( re , prec + <NUM_LIT:10> ) <EOL> re = mpf_mul ( ey , c , prec , rnd ) <EOL> im = mpf_mul ( ey , s , prec , rnd ) <EOL> return re , im <EOL> if BACKEND == '<STR_LIT>' : <EOL> try : <EOL> import sage . libs . mpmath . ext_libmp as _lbmp <EOL> mpc_exp = _lbmp . mpc_exp <EOL> mpc_sqrt = _lbmp . mpc_sqrt <EOL> except ( ImportError , AttributeError ) : <EOL> print ( "<STR_LIT>" ) </s>
