<s> """<STR_LIT>""" <EOL> from __future__ import division , print_function , absolute_import <EOL> __all__ = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> ] <EOL> import warnings <EOL> import numpy as np <EOL> from scipy . _lib . _version import NumpyVersion <EOL> supported_dtypes = [ '<STR_LIT:bool>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> supported_dtypes = [ np . typeDict [ x ] for x in supported_dtypes ] <EOL> _upcast_memo = { } <EOL> def upcast ( * args ) : <EOL> """<STR_LIT>""" <EOL> t = _upcast_memo . get ( hash ( args ) ) <EOL> if t is not None : <EOL> return t <EOL> upcast = np . find_common_type ( args , [ ] ) <EOL> for t in supported_dtypes : <EOL> if np . can_cast ( upcast , t ) : <EOL> _upcast_memo [ hash ( args ) ] = t <EOL> return t <EOL> raise TypeError ( '<STR_LIT>' % ( args , ) ) <EOL> def upcast_char ( * args ) : <EOL> """<STR_LIT>""" <EOL> t = _upcast_memo . get ( args ) <EOL> if t is not None : <EOL> return t <EOL> t = upcast ( * map ( np . dtype , args ) ) <EOL> _upcast_memo [ args ] = t <EOL> return t <EOL> def upcast_scalar ( dtype , scalar ) : <EOL> """<STR_LIT>""" <EOL> return ( np . array ( [ <NUM_LIT:0> ] , dtype = dtype ) * scalar ) . dtype <EOL> def downcast_intp_index ( arr ) : <EOL> """<STR_LIT>""" <EOL> if arr . dtype . itemsize > np . dtype ( np . intp ) . itemsize : <EOL> if arr . size == <NUM_LIT:0> : <EOL> return arr . astype ( np . intp ) <EOL> maxval = arr . max ( ) <EOL> minval = arr . min ( ) <EOL> if maxval > np . iinfo ( np . intp ) . max or minval < np . iinfo ( np . intp ) . min : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return arr . astype ( np . intp ) <EOL> return arr <EOL> def to_native ( A ) : <EOL> return np . asarray ( A , dtype = A . dtype . newbyteorder ( '<STR_LIT>' ) ) <EOL> def getdtype ( dtype , a = None , default = None ) : <EOL> """<STR_LIT>""" <EOL> if dtype is None : <EOL> try : <EOL> newdtype = a . dtype <EOL> except AttributeError : <EOL> if default is not None : <EOL> newdtype = np . dtype ( default ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> else : <EOL> newdtype = np . dtype ( dtype ) <EOL> if newdtype == np . object_ : <EOL> warnings . warn ( "<STR_LIT>" ) <EOL> return newdtype <EOL> def get_index_dtype ( arrays = ( ) , maxval = None , check_contents = False ) : <EOL> """<STR_LIT>""" <EOL> int32max = np . iinfo ( np . int32 ) . max <EOL> dtype = np . intc <EOL> if maxval is not None : <EOL> if maxval > int32max : <EOL> dtype = np . int64 <EOL> if isinstance ( arrays , np . ndarray ) : <EOL> arrays = ( arrays , ) <EOL> for arr in arrays : <EOL> arr = np . asarray ( arr ) <EOL> if arr . dtype > np . int32 : <EOL> if check_contents : <EOL> if arr . size == <NUM_LIT:0> : <EOL> continue <EOL> elif np . issubdtype ( arr . dtype , np . integer ) : <EOL> maxval = arr . max ( ) <EOL> minval = arr . min ( ) <EOL> if minval >= np . iinfo ( np . int32 ) . min and maxval <= np . iinfo ( np . int32 ) . max : <EOL> continue <EOL> dtype = np . int64 <EOL> break <EOL> return dtype <EOL> def get_sum_dtype ( dtype ) : <EOL> """<STR_LIT>""" <EOL> if np . issubdtype ( dtype , np . float_ ) : <EOL> return np . float_ <EOL> if dtype . kind == '<STR_LIT:u>' and np . can_cast ( dtype , np . uint ) : <EOL> return np . uint <EOL> if np . can_cast ( dtype , np . int_ ) : <EOL> return np . int_ <EOL> return dtype <EOL> def isscalarlike ( x ) : <EOL> """<STR_LIT>""" <EOL> return np . isscalar ( x ) or ( isdense ( x ) and x . ndim == <NUM_LIT:0> ) <EOL> def isintlike ( x ) : <EOL> """<STR_LIT>""" <EOL> if issequence ( x ) : <EOL> return False <EOL> else : <EOL> try : <EOL> if int ( x ) == x : <EOL> return True <EOL> else : <EOL> return False <EOL> except TypeError : <EOL> return False <EOL> def isshape ( x ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> ( M , N ) = x <EOL> except : <EOL> return False <EOL> else : <EOL> if isintlike ( M ) and isintlike ( N ) : <EOL> if np . ndim ( M ) == <NUM_LIT:0> and np . ndim ( N ) == <NUM_LIT:0> : <EOL> return True <EOL> return False <EOL> def issequence ( t ) : <EOL> return ( isinstance ( t , ( list , tuple ) ) and ( len ( t ) == <NUM_LIT:0> or np . isscalar ( t [ <NUM_LIT:0> ] ) ) ) or ( isinstance ( t , np . ndarray ) and ( t . ndim == <NUM_LIT:1> ) ) <EOL> def ismatrix ( t ) : <EOL> return ( ( isinstance ( t , ( list , tuple ) ) and len ( t ) > <NUM_LIT:0> and issequence ( t [ <NUM_LIT:0> ] ) ) <EOL> or ( isinstance ( t , np . ndarray ) and t . ndim == <NUM_LIT:2> ) ) <EOL> def isdense ( x ) : <EOL> return isinstance ( x , np . ndarray ) <EOL> class IndexMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def _slicetoarange ( self , j , shape ) : <EOL> """<STR_LIT>""" <EOL> start , stop , step = j . indices ( shape ) <EOL> return np . arange ( start , stop , step ) <EOL> def _unpack_index ( self , index ) : <EOL> """<STR_LIT>""" <EOL> from . base import spmatrix <EOL> if ( isinstance ( index , ( spmatrix , np . ndarray ) ) and <EOL> ( index . ndim == <NUM_LIT:2> ) and index . dtype . kind == '<STR_LIT:b>' ) : <EOL> return index . nonzero ( ) <EOL> index = self . _check_ellipsis ( index ) <EOL> if isinstance ( index , tuple ) : <EOL> if len ( index ) == <NUM_LIT:2> : <EOL> row , col = index <EOL> elif len ( index ) == <NUM_LIT:1> : <EOL> row , col = index [ <NUM_LIT:0> ] , slice ( None ) <EOL> else : <EOL> raise IndexError ( '<STR_LIT>' ) <EOL> else : <EOL> row , col = index , slice ( None ) <EOL> row , col = self . _check_boolean ( row , col ) <EOL> return row , col <EOL> def _check_ellipsis ( self , index ) : <EOL> """<STR_LIT>""" <EOL> if index is Ellipsis : <EOL> return ( slice ( None ) , slice ( None ) ) <EOL> elif isinstance ( index , tuple ) : <EOL> for j , v in enumerate ( index ) : <EOL> if v is Ellipsis : <EOL> first_ellipsis = j <EOL> break <EOL> else : <EOL> first_ellipsis = None <EOL> if first_ellipsis is not None : <EOL> if len ( index ) == <NUM_LIT:1> : <EOL> return ( slice ( None ) , slice ( None ) ) <EOL> elif len ( index ) == <NUM_LIT:2> : <EOL> if first_ellipsis == <NUM_LIT:0> : <EOL> if index [ <NUM_LIT:1> ] is Ellipsis : <EOL> return ( slice ( None ) , slice ( None ) ) <EOL> else : <EOL> return ( slice ( None ) , index [ <NUM_LIT:1> ] ) <EOL> else : <EOL> return ( index [ <NUM_LIT:0> ] , slice ( None ) ) <EOL> tail = ( ) <EOL> for v in index [ first_ellipsis + <NUM_LIT:1> : ] : <EOL> if v is not Ellipsis : <EOL> tail = tail + ( v , ) <EOL> nd = first_ellipsis + len ( tail ) <EOL> nslice = max ( <NUM_LIT:0> , <NUM_LIT:2> - nd ) <EOL> return index [ : first_ellipsis ] + ( slice ( None ) , ) * nslice + tail <EOL> return index <EOL> def _check_boolean ( self , row , col ) : <EOL> from . base import isspmatrix <EOL> if isspmatrix ( row ) or isspmatrix ( col ) : <EOL> raise IndexError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if isinstance ( row , np . ndarray ) and row . dtype . kind == '<STR_LIT:b>' : <EOL> row = self . _boolean_index_to_array ( row ) <EOL> if isinstance ( col , np . ndarray ) and col . dtype . kind == '<STR_LIT:b>' : <EOL> col = self . _boolean_index_to_array ( col ) <EOL> return row , col <EOL> def _boolean_index_to_array ( self , i ) : <EOL> if i . ndim > <NUM_LIT:1> : <EOL> raise IndexError ( '<STR_LIT>' ) <EOL> return i . nonzero ( ) [ <NUM_LIT:0> ] <EOL> def _index_to_arrays ( self , i , j ) : <EOL> i , j = self . _check_boolean ( i , j ) <EOL> i_slice = isinstance ( i , slice ) <EOL> if i_slice : <EOL> i = self . _slicetoarange ( i , self . shape [ <NUM_LIT:0> ] ) [ : , None ] <EOL> else : <EOL> i = np . atleast_1d ( i ) <EOL> if isinstance ( j , slice ) : <EOL> j = self . _slicetoarange ( j , self . shape [ <NUM_LIT:1> ] ) [ None , : ] <EOL> if i . ndim == <NUM_LIT:1> : <EOL> i = i [ : , None ] <EOL> elif not i_slice : <EOL> raise IndexError ( '<STR_LIT>' ) <EOL> elif isscalarlike ( j ) : <EOL> j = np . atleast_1d ( j ) <EOL> if i . ndim == <NUM_LIT:1> : <EOL> i , j = np . broadcast_arrays ( i , j ) <EOL> i = i [ : , None ] <EOL> j = j [ : , None ] <EOL> return i , j <EOL> else : <EOL> j = np . atleast_1d ( j ) <EOL> if i_slice and j . ndim > <NUM_LIT:1> : <EOL> raise IndexError ( '<STR_LIT>' ) <EOL> i , j = np . broadcast_arrays ( i , j ) <EOL> if i . ndim == <NUM_LIT:1> : <EOL> i = i [ None , : ] <EOL> j = j [ None , : ] <EOL> elif i . ndim > <NUM_LIT:2> : <EOL> raise IndexError ( "<STR_LIT>" ) <EOL> return i , j </s>
