<s> """<STR_LIT>""" <EOL> from numbers import Integral <EOL> from platform import processor , machine <EOL> import numpy as np <EOL> class CastingError ( Exception ) : <EOL> pass <EOL> _test_val = <NUM_LIT:2> ** <NUM_LIT> + <NUM_LIT:2> ** <NUM_LIT:11> <EOL> TRUNC_UINT64 = np . float64 ( _test_val ) . astype ( np . uint64 ) != _test_val <EOL> def float_to_int ( arr , int_type , nan2zero = True , infmax = False ) : <EOL> """<STR_LIT>""" <EOL> arr = np . asarray ( arr ) <EOL> flt_type = arr . dtype . type <EOL> int_type = np . dtype ( int_type ) . type <EOL> shape = arr . shape <EOL> arr = np . atleast_1d ( arr ) <EOL> mn , mx = shared_range ( flt_type , int_type ) <EOL> if nan2zero is None : <EOL> seen_nans = False <EOL> else : <EOL> nans = np . isnan ( arr ) <EOL> seen_nans = np . any ( nans ) <EOL> if not nan2zero and seen_nans : <EOL> raise CastingError ( '<STR_LIT>' ) <EOL> iarr = np . clip ( np . rint ( arr ) , mn , mx ) . astype ( int_type ) <EOL> if seen_nans : <EOL> iarr [ nans ] = <NUM_LIT:0> <EOL> if not infmax : <EOL> return iarr . reshape ( shape ) <EOL> ii = np . iinfo ( int_type ) <EOL> iarr [ arr == np . inf ] = ii . max <EOL> if ii . min != int ( mn ) : <EOL> iarr [ arr == - np . inf ] = ii . min <EOL> return iarr . reshape ( shape ) <EOL> _SHARED_RANGES = { } <EOL> def shared_range ( flt_type , int_type ) : <EOL> """<STR_LIT>""" <EOL> flt_type = np . dtype ( flt_type ) . type <EOL> int_type = np . dtype ( int_type ) . type <EOL> key = ( flt_type , int_type ) <EOL> try : <EOL> return _SHARED_RANGES [ key ] <EOL> except KeyError : <EOL> pass <EOL> ii = np . iinfo ( int_type ) <EOL> fi = np . finfo ( flt_type ) <EOL> mn = ceil_exact ( ii . min , flt_type ) <EOL> if mn == - np . inf : <EOL> mn = fi . min <EOL> mx = floor_exact ( ii . max , flt_type ) <EOL> if mx == np . inf : <EOL> mx = fi . max <EOL> elif TRUNC_UINT64 and int_type == np . uint64 : <EOL> mx = min ( mx , flt_type ( <NUM_LIT:2> ** <NUM_LIT> ) ) <EOL> _SHARED_RANGES [ key ] = ( mn , mx ) <EOL> return mn , mx <EOL> try : <EOL> _float16 = np . float16 <EOL> except AttributeError : <EOL> _float16 = None <EOL> class FloatingError ( Exception ) : <EOL> pass <EOL> def on_powerpc ( ) : <EOL> """<STR_LIT>""" <EOL> return processor ( ) == '<STR_LIT>' or machine ( ) . startswith ( '<STR_LIT>' ) <EOL> def type_info ( np_type ) : <EOL> """<STR_LIT>""" <EOL> dt = np . dtype ( np_type ) <EOL> np_type = dt . type <EOL> width = dt . itemsize <EOL> try : <EOL> info = np . iinfo ( dt ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> return dict ( min = np_type ( info . min ) , max = np_type ( info . max ) , minexp = None , <EOL> maxexp = None , nmant = None , nexp = None , width = width ) <EOL> info = np . finfo ( dt ) <EOL> nmant , nexp = info . nmant , info . nexp <EOL> ret = dict ( min = np_type ( info . min ) , <EOL> max = np_type ( info . max ) , <EOL> nmant = nmant , <EOL> nexp = nexp , <EOL> minexp = info . minexp , <EOL> maxexp = info . maxexp , <EOL> width = width ) <EOL> if np_type in ( _float16 , np . float32 , np . float64 , <EOL> np . complex64 , np . complex128 ) : <EOL> return ret <EOL> info_64 = np . finfo ( np . float64 ) <EOL> if dt . kind == '<STR_LIT:c>' : <EOL> assert np_type is np . longcomplex <EOL> vals = ( nmant , nexp , width / <NUM_LIT:2> ) <EOL> else : <EOL> assert np_type is np . longdouble <EOL> vals = ( nmant , nexp , width ) <EOL> if vals in ( ( <NUM_LIT> , <NUM_LIT:15> , <NUM_LIT:16> ) , <EOL> ( info_64 . nmant , info_64 . nexp , <NUM_LIT:8> ) , <EOL> ( <NUM_LIT> , <NUM_LIT:15> , <NUM_LIT:12> ) , ( <NUM_LIT> , <NUM_LIT:15> , <NUM_LIT:16> ) ) : <EOL> return ret <EOL> ret = type_info ( np . float64 ) <EOL> if vals in ( ( <NUM_LIT> , <NUM_LIT:15> , <NUM_LIT:12> ) , <EOL> ( <NUM_LIT> , <NUM_LIT:15> , <NUM_LIT:16> ) ) : <EOL> ret . update ( dict ( width = width ) ) <EOL> return ret <EOL> if np_type not in ( np . longdouble , np . longcomplex ) or width not in ( <NUM_LIT:16> , <NUM_LIT:32> ) : <EOL> raise FloatingError ( '<STR_LIT>' % np_type ) <EOL> if ( vals == ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:16> ) and on_powerpc ( ) and <EOL> _check_maxexp ( np . longdouble , <NUM_LIT> ) ) : <EOL> ret . update ( dict ( nmant = <NUM_LIT> , width = width ) ) <EOL> elif ( _check_nmant ( np . longdouble , <NUM_LIT> ) and <EOL> _check_maxexp ( np . longdouble , <NUM_LIT:11> ) ) : <EOL> pass <EOL> elif ( _check_nmant ( np . longdouble , <NUM_LIT> ) and <EOL> _check_maxexp ( np . longdouble , <NUM_LIT> ) ) : <EOL> two = np . longdouble ( <NUM_LIT:2> ) <EOL> max_val = ( two ** <NUM_LIT> - <NUM_LIT:1> ) / ( two ** <NUM_LIT> ) * two ** <NUM_LIT> <EOL> if np_type is np . longcomplex : <EOL> max_val += <NUM_LIT> <EOL> ret = dict ( min = - max_val , <EOL> max = max_val , <EOL> nmant = <NUM_LIT> , <EOL> nexp = <NUM_LIT:15> , <EOL> minexp = - <NUM_LIT> , <EOL> maxexp = <NUM_LIT> , <EOL> width = width ) <EOL> else : <EOL> raise FloatingError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( np_type , info ) ) <EOL> return ret <EOL> def _check_nmant ( np_type , nmant ) : <EOL> """<STR_LIT>""" <EOL> np_type = np . dtype ( np_type ) . type <EOL> max_contig = np_type ( <NUM_LIT:2> ** ( nmant + <NUM_LIT:1> ) ) <EOL> tests = max_contig + np . array ( [ - <NUM_LIT:2> , - <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] , dtype = np_type ) <EOL> return np . all ( tests - max_contig == [ - <NUM_LIT:2> , - <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:2> ] ) <EOL> def _check_maxexp ( np_type , maxexp ) : <EOL> """<STR_LIT>""" <EOL> dt = np . dtype ( np_type ) <EOL> np_type = dt . type <EOL> two = np_type ( <NUM_LIT:2> ) . reshape ( ( <NUM_LIT:1> , ) ) <EOL> return ( np . isfinite ( two ** ( maxexp - <NUM_LIT:1> ) ) and <EOL> not np . isfinite ( two ** maxexp ) ) <EOL> def as_int ( x , check = True ) : <EOL> """<STR_LIT>""" <EOL> x = np . array ( x ) <EOL> if x . dtype . kind in '<STR_LIT>' : <EOL> return int ( str ( x ) ) <EOL> ix = int ( x ) <EOL> if ix == x : <EOL> return ix <EOL> fx = np . floor ( x ) <EOL> if check and fx != x : <EOL> raise FloatingError ( '<STR_LIT>' % x ) <EOL> if not fx . dtype . type == np . longdouble : <EOL> return int ( x ) <EOL> ret = <NUM_LIT:0> <EOL> while fx != <NUM_LIT:0> : <EOL> f64 = np . float64 ( fx ) <EOL> fx -= f64 <EOL> ret += int ( f64 ) <EOL> return ret <EOL> def int_to_float ( val , flt_type ) : <EOL> """<STR_LIT>""" <EOL> if flt_type is not np . longdouble : <EOL> return flt_type ( val ) <EOL> if not isinstance ( val , Integral ) : <EOL> val = int ( str ( val ) ) <EOL> faval = np . longdouble ( <NUM_LIT:0> ) <EOL> while val != <NUM_LIT:0> : <EOL> f64 = np . float64 ( val ) <EOL> faval += f64 <EOL> val -= int ( f64 ) <EOL> return faval <EOL> def floor_exact ( val , flt_type ) : <EOL> """<STR_LIT>""" <EOL> val = int ( val ) <EOL> flt_type = np . dtype ( flt_type ) . type <EOL> sign = <NUM_LIT:1> if val > <NUM_LIT:0> else - <NUM_LIT:1> <EOL> try : <EOL> fval = int_to_float ( val , flt_type ) <EOL> except OverflowError : <EOL> return sign * np . inf <EOL> if not np . isfinite ( fval ) : <EOL> return fval <EOL> info = type_info ( flt_type ) <EOL> diff = val - as_int ( fval ) <EOL> if diff >= <NUM_LIT:0> : <EOL> return fval <EOL> biggest_gap = <NUM_LIT:2> ** ( floor_log2 ( val ) - info [ '<STR_LIT>' ] ) <EOL> assert biggest_gap > <NUM_LIT:1> <EOL> fval -= flt_type ( biggest_gap ) <EOL> return fval <EOL> def ceil_exact ( val , flt_type ) : <EOL> """<STR_LIT>""" <EOL> return - floor_exact ( - val , flt_type ) <EOL> def int_abs ( arr ) : <EOL> """<STR_LIT>""" <EOL> arr = np . array ( arr , copy = False ) <EOL> dt = arr . dtype <EOL> if dt . kind == '<STR_LIT:u>' : <EOL> return arr <EOL> if dt . kind != '<STR_LIT:i>' : <EOL> return np . absolute ( arr ) <EOL> out = arr . astype ( np . dtype ( dt . str . replace ( '<STR_LIT:i>' , '<STR_LIT:u>' ) ) ) <EOL> return np . choose ( arr < <NUM_LIT:0> , ( arr , arr * - <NUM_LIT:1> ) , out = out ) <EOL> def floor_log2 ( x ) : <EOL> """<STR_LIT>""" <EOL> ip = <NUM_LIT:0> <EOL> rem = abs ( x ) <EOL> if rem > <NUM_LIT:1> : <EOL> while rem >= <NUM_LIT:2> : <EOL> ip += <NUM_LIT:1> <EOL> rem //= <NUM_LIT:2> <EOL> return ip <EOL> elif rem == <NUM_LIT:0> : <EOL> return None <EOL> while rem < <NUM_LIT:1> : <EOL> ip -= <NUM_LIT:1> <EOL> rem *= <NUM_LIT:2> <EOL> return ip <EOL> def best_float ( ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> long_info = type_info ( np . longdouble ) <EOL> except FloatingError : <EOL> return np . float64 <EOL> if ( long_info [ '<STR_LIT>' ] > type_info ( np . float64 ) [ '<STR_LIT>' ] and <EOL> machine ( ) != '<STR_LIT>' ) : <EOL> return np . longdouble <EOL> return np . float64 <EOL> def longdouble_lte_float64 ( ) : <EOL> """<STR_LIT>""" <EOL> return np . longdouble ( <NUM_LIT:2> ** <NUM_LIT> ) == np . longdouble ( <NUM_LIT:2> ** <NUM_LIT> ) + <NUM_LIT:1> <EOL> _LD_LTE_FLOAT64 = longdouble_lte_float64 ( ) <EOL> def longdouble_precision_improved ( ) : <EOL> """<STR_LIT>""" <EOL> return not longdouble_lte_float64 ( ) and _LD_LTE_FLOAT64 <EOL> def have_binary128 ( ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> ti = type_info ( np . longdouble ) <EOL> except FloatingError : <EOL> return False <EOL> return ( ti [ '<STR_LIT>' ] , ti [ '<STR_LIT>' ] ) == ( <NUM_LIT> , <NUM_LIT> ) <EOL> def ok_floats ( ) : <EOL> """<STR_LIT>""" <EOL> floats = np . sctypes [ '<STR_LIT:float>' ] [ : ] <EOL> if best_float ( ) != np . longdouble and np . longdouble in floats : <EOL> floats . remove ( np . longdouble ) <EOL> return sorted ( floats , key = lambda f : type_info ( f ) [ '<STR_LIT>' ] ) <EOL> OK_FLOATS = ok_floats ( ) <EOL> def able_int_type ( values ) : <EOL> """<STR_LIT>""" <EOL> if any ( [ v % <NUM_LIT:1> for v in values ] ) : <EOL> return None <EOL> mn = min ( values ) <EOL> mx = max ( values ) <EOL> if mn >= <NUM_LIT:0> : <EOL> for ityp in np . sctypes [ '<STR_LIT>' ] : <EOL> if mx <= np . iinfo ( ityp ) . max : <EOL> return ityp <EOL> for ityp in np . sctypes [ '<STR_LIT:int>' ] : <EOL> info = np . iinfo ( ityp ) <EOL> if mn >= info . min and mx <= info . max : <EOL> return ityp <EOL> return None <EOL> def ulp ( val = np . float64 ( <NUM_LIT:1.0> ) ) : <EOL> """<STR_LIT>""" <EOL> val = np . array ( val ) <EOL> if not np . isfinite ( val ) : <EOL> return np . nan <EOL> if val . dtype . kind in '<STR_LIT>' : <EOL> return <NUM_LIT:1> <EOL> aval = np . abs ( val ) <EOL> info = type_info ( val . dtype ) <EOL> fl2 = floor_log2 ( aval ) <EOL> if fl2 is None or fl2 < info [ '<STR_LIT>' ] : <EOL> fl2 = info [ '<STR_LIT>' ] <EOL> return <NUM_LIT:2> ** ( fl2 - info [ '<STR_LIT>' ] ) </s>
