<s> """<STR_LIT>""" <EOL> from __future__ import print_function , absolute_import , division <EOL> import contextlib <EOL> from llvmlite import ir <EOL> import numpy <EOL> from numba import jit , types , cgutils <EOL> from numba . targets . imputils import ( lower_builtin , impl_ret_borrowed , <EOL> impl_ret_new_ref , impl_ret_untracked ) <EOL> from numba . typing import signature <EOL> from numba . extending import overload <EOL> from numba . numpy_support import version as numpy_version <EOL> from numba import types <EOL> from . arrayobj import make_array , _empty_nd_impl , array_copy <EOL> from . . errors import TypingError <EOL> ll_char = ir . IntType ( <NUM_LIT:8> ) <EOL> ll_char_p = ll_char . as_pointer ( ) <EOL> ll_void_p = ll_char_p <EOL> ll_intc = ir . IntType ( <NUM_LIT:32> ) <EOL> ll_intc_p = ll_intc . as_pointer ( ) <EOL> intp_t = cgutils . intp_t <EOL> ll_intp_p = intp_t . as_pointer ( ) <EOL> _blas_kinds = { <EOL> types . float32 : '<STR_LIT:s>' , <EOL> types . float64 : '<STR_LIT:d>' , <EOL> types . complex64 : '<STR_LIT:c>' , <EOL> types . complex128 : '<STR_LIT:z>' , <EOL> } <EOL> def get_blas_kind ( dtype , func_name = "<STR_LIT>" ) : <EOL> kind = _blas_kinds . get ( dtype ) <EOL> if kind is None : <EOL> raise TypeError ( "<STR_LIT>" % ( func_name , ) ) <EOL> return kind <EOL> def ensure_blas ( ) : <EOL> try : <EOL> import scipy . linalg . cython_blas <EOL> except ImportError : <EOL> raise ImportError ( "<STR_LIT>" ) <EOL> def ensure_lapack ( ) : <EOL> try : <EOL> import scipy . linalg . cython_lapack <EOL> except ImportError : <EOL> raise ImportError ( "<STR_LIT>" ) <EOL> def make_constant_slot ( context , builder , ty , val ) : <EOL> const = context . get_constant_generic ( builder , ty , val ) <EOL> return cgutils . alloca_once_value ( builder , const ) <EOL> @ contextlib . contextmanager <EOL> def make_contiguous ( context , builder , sig , args ) : <EOL> """<STR_LIT>""" <EOL> newtys = [ ] <EOL> newargs = [ ] <EOL> copies = [ ] <EOL> for ty , val in zip ( sig . args , args ) : <EOL> if not isinstance ( ty , types . Array ) or ty . layout in '<STR_LIT>' : <EOL> newty , newval = ty , val <EOL> else : <EOL> newty = ty . copy ( layout = '<STR_LIT:C>' ) <EOL> copysig = signature ( newty , ty ) <EOL> newval = array_copy ( context , builder , copysig , ( val , ) ) <EOL> copies . append ( ( newty , newval ) ) <EOL> newtys . append ( newty ) <EOL> newargs . append ( newval ) <EOL> yield signature ( sig . return_type , * newtys ) , tuple ( newargs ) <EOL> for ty , val in copies : <EOL> context . nrt_decref ( builder , ty , val ) <EOL> def check_c_int ( context , builder , n ) : <EOL> """<STR_LIT>""" <EOL> _maxint = <NUM_LIT:2> ** <NUM_LIT> - <NUM_LIT:1> <EOL> def impl ( n ) : <EOL> if n > _maxint : <EOL> raise OverflowError ( "<STR_LIT>" ) <EOL> context . compile_internal ( builder , impl , <EOL> signature ( types . none , types . intp ) , ( n , ) ) <EOL> def check_blas_return ( context , builder , res ) : <EOL> """<STR_LIT>""" <EOL> with builder . if_then ( cgutils . is_not_null ( builder , res ) , likely = False ) : <EOL> pyapi = context . get_python_api ( builder ) <EOL> pyapi . gil_ensure ( ) <EOL> pyapi . fatal_error ( "<STR_LIT>" ) <EOL> def check_lapack_return ( context , builder , res ) : <EOL> """<STR_LIT>""" <EOL> with builder . if_then ( cgutils . is_not_null ( builder , res ) , likely = False ) : <EOL> pyapi = context . get_python_api ( builder ) <EOL> pyapi . gil_ensure ( ) <EOL> pyapi . fatal_error ( "<STR_LIT>" ) <EOL> def call_xxdot ( context , builder , conjugate , dtype , <EOL> n , a_data , b_data , out_data ) : <EOL> """<STR_LIT>""" <EOL> fnty = ir . FunctionType ( ir . IntType ( <NUM_LIT:32> ) , <EOL> [ ll_char , ll_char , intp_t , <EOL> ll_void_p , ll_void_p , ll_void_p , <EOL> ] ) <EOL> fn = builder . module . get_or_insert_function ( fnty , name = "<STR_LIT>" ) <EOL> kind = get_blas_kind ( dtype ) <EOL> kind_val = ir . Constant ( ll_char , ord ( kind ) ) <EOL> conjugate = ir . Constant ( ll_char , int ( conjugate ) ) <EOL> res = builder . call ( fn , ( kind_val , conjugate , n , <EOL> builder . bitcast ( a_data , ll_void_p ) , <EOL> builder . bitcast ( b_data , ll_void_p ) , <EOL> builder . bitcast ( out_data , ll_void_p ) ) ) <EOL> check_blas_return ( context , builder , res ) <EOL> def call_xxgemv ( context , builder , do_trans , <EOL> m_type , m_shapes , m_data , v_data , out_data ) : <EOL> """<STR_LIT>""" <EOL> fnty = ir . FunctionType ( ir . IntType ( <NUM_LIT:32> ) , <EOL> [ ll_char , ll_char_p , <EOL> intp_t , intp_t , <EOL> ll_void_p , ll_void_p , intp_t , <EOL> ll_void_p , ll_void_p , ll_void_p , <EOL> ] ) <EOL> fn = builder . module . get_or_insert_function ( fnty , name = "<STR_LIT>" ) <EOL> dtype = m_type . dtype <EOL> alpha = make_constant_slot ( context , builder , dtype , <NUM_LIT:1.0> ) <EOL> beta = make_constant_slot ( context , builder , dtype , <NUM_LIT:0.0> ) <EOL> if m_type . layout == '<STR_LIT:F>' : <EOL> m , n = m_shapes <EOL> lda = m_shapes [ <NUM_LIT:0> ] <EOL> else : <EOL> n , m = m_shapes <EOL> lda = m_shapes [ <NUM_LIT:1> ] <EOL> kind = get_blas_kind ( dtype ) <EOL> kind_val = ir . Constant ( ll_char , ord ( kind ) ) <EOL> trans = context . insert_const_string ( builder . module , <EOL> "<STR_LIT:t>" if do_trans else "<STR_LIT:n>" ) <EOL> res = builder . call ( fn , ( kind_val , trans , m , n , <EOL> builder . bitcast ( alpha , ll_void_p ) , <EOL> builder . bitcast ( m_data , ll_void_p ) , lda , <EOL> builder . bitcast ( v_data , ll_void_p ) , <EOL> builder . bitcast ( beta , ll_void_p ) , <EOL> builder . bitcast ( out_data , ll_void_p ) ) ) <EOL> check_blas_return ( context , builder , res ) <EOL> def call_xxgemm ( context , builder , <EOL> x_type , x_shapes , x_data , <EOL> y_type , y_shapes , y_data , <EOL> out_type , out_shapes , out_data ) : <EOL> """<STR_LIT>""" <EOL> fnty = ir . FunctionType ( ir . IntType ( <NUM_LIT:32> ) , <EOL> [ ll_char , <EOL> ll_char_p , ll_char_p , <EOL> intp_t , intp_t , intp_t , <EOL> ll_void_p , ll_void_p , intp_t , <EOL> ll_void_p , intp_t , ll_void_p , <EOL> ll_void_p , intp_t , <EOL> ] ) <EOL> fn = builder . module . get_or_insert_function ( fnty , name = "<STR_LIT>" ) <EOL> m , k = x_shapes <EOL> _k , n = y_shapes <EOL> dtype = x_type . dtype <EOL> alpha = make_constant_slot ( context , builder , dtype , <NUM_LIT:1.0> ) <EOL> beta = make_constant_slot ( context , builder , dtype , <NUM_LIT:0.0> ) <EOL> trans = context . insert_const_string ( builder . module , "<STR_LIT:t>" ) <EOL> notrans = context . insert_const_string ( builder . module , "<STR_LIT:n>" ) <EOL> def get_array_param ( ty , shapes , data ) : <EOL> return ( <EOL> notrans if ty . layout == out_type . layout else trans , <EOL> shapes [ <NUM_LIT:1> ] if ty . layout == '<STR_LIT:C>' else shapes [ <NUM_LIT:0> ] , <EOL> builder . bitcast ( data , ll_void_p ) , <EOL> ) <EOL> transa , lda , data_a = get_array_param ( y_type , y_shapes , y_data ) <EOL> transb , ldb , data_b = get_array_param ( x_type , x_shapes , x_data ) <EOL> _ , ldc , data_c = get_array_param ( out_type , out_shapes , out_data ) <EOL> kind = get_blas_kind ( dtype ) <EOL> kind_val = ir . Constant ( ll_char , ord ( kind ) ) <EOL> res = builder . call ( fn , ( kind_val , transa , transb , n , m , k , <EOL> builder . bitcast ( alpha , ll_void_p ) , data_a , lda , <EOL> data_b , ldb , builder . bitcast ( beta , ll_void_p ) , <EOL> data_c , ldc ) ) <EOL> check_blas_return ( context , builder , res ) <EOL> def dot_2_mm ( context , builder , sig , args ) : <EOL> """<STR_LIT>""" <EOL> def dot_impl ( a , b ) : <EOL> m , k = a . shape <EOL> _k , n = b . shape <EOL> out = numpy . empty ( ( m , n ) , a . dtype ) <EOL> return numpy . dot ( a , b , out ) <EOL> res = context . compile_internal ( builder , dot_impl , sig , args ) <EOL> return impl_ret_new_ref ( context , builder , sig . return_type , res ) <EOL> def dot_2_vm ( context , builder , sig , args ) : <EOL> """<STR_LIT>""" <EOL> def dot_impl ( a , b ) : <EOL> m , = a . shape <EOL> _m , n = b . shape <EOL> out = numpy . empty ( ( n , ) , a . dtype ) <EOL> return numpy . dot ( a , b , out ) <EOL> res = context . compile_internal ( builder , dot_impl , sig , args ) <EOL> return impl_ret_new_ref ( context , builder , sig . return_type , res ) <EOL> def dot_2_mv ( context , builder , sig , args ) : <EOL> """<STR_LIT>""" <EOL> def dot_impl ( a , b ) : <EOL> m , n = a . shape <EOL> _n , = b . shape <EOL> out = numpy . empty ( ( m , ) , a . dtype ) <EOL> return numpy . dot ( a , b , out ) <EOL> res = context . compile_internal ( builder , dot_impl , sig , args ) <EOL> return impl_ret_new_ref ( context , builder , sig . return_type , res ) <EOL> def dot_2_vv ( context , builder , sig , args , conjugate = False ) : <EOL> """<STR_LIT>""" <EOL> aty , bty = sig . args <EOL> dtype = sig . return_type <EOL> a = make_array ( aty ) ( context , builder , args [ <NUM_LIT:0> ] ) <EOL> b = make_array ( bty ) ( context , builder , args [ <NUM_LIT:1> ] ) <EOL> n , = cgutils . unpack_tuple ( builder , a . shape ) <EOL> def check_args ( a , b ) : <EOL> m , = a . shape <EOL> n , = b . shape <EOL> if m != n : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> context . compile_internal ( builder , check_args , <EOL> signature ( types . none , * sig . args ) , args ) <EOL> check_c_int ( context , builder , n ) <EOL> out = cgutils . alloca_once ( builder , context . get_value_type ( dtype ) ) <EOL> call_xxdot ( context , builder , conjugate , dtype , n , a . data , b . data , out ) <EOL> return builder . load ( out ) <EOL> @ lower_builtin ( numpy . dot , types . Array , types . Array ) <EOL> @ lower_builtin ( '<STR_LIT:@>' , types . Array , types . Array ) <EOL> def dot_2 ( context , builder , sig , args ) : <EOL> """<STR_LIT>""" <EOL> ensure_blas ( ) <EOL> with make_contiguous ( context , builder , sig , args ) as ( sig , args ) : <EOL> ndims = [ x . ndim for x in sig . args [ : <NUM_LIT:2> ] ] <EOL> if ndims == [ <NUM_LIT:2> , <NUM_LIT:2> ] : <EOL> return dot_2_mm ( context , builder , sig , args ) <EOL> elif ndims == [ <NUM_LIT:2> , <NUM_LIT:1> ] : <EOL> return dot_2_mv ( context , builder , sig , args ) <EOL> elif ndims == [ <NUM_LIT:1> , <NUM_LIT:2> ] : <EOL> return dot_2_vm ( context , builder , sig , args ) <EOL> elif ndims == [ <NUM_LIT:1> , <NUM_LIT:1> ] : <EOL> return dot_2_vv ( context , builder , sig , args ) <EOL> else : <EOL> assert <NUM_LIT:0> <EOL> @ lower_builtin ( numpy . vdot , types . Array , types . Array ) <EOL> def vdot ( context , builder , sig , args ) : <EOL> """<STR_LIT>""" <EOL> ensure_blas ( ) <EOL> with make_contiguous ( context , builder , sig , args ) as ( sig , args ) : <EOL> return dot_2_vv ( context , builder , sig , args , conjugate = True ) <EOL> def dot_3_vm ( context , builder , sig , args ) : <EOL> """<STR_LIT>""" <EOL> xty , yty , outty = sig . args <EOL> assert outty == sig . return_type <EOL> dtype = xty . dtype <EOL> x = make_array ( xty ) ( context , builder , args [ <NUM_LIT:0> ] ) <EOL> y = make_array ( yty ) ( context , builder , args [ <NUM_LIT:1> ] ) <EOL> out = make_array ( outty ) ( context , builder , args [ <NUM_LIT:2> ] ) <EOL> x_shapes = cgutils . unpack_tuple ( builder , x . shape ) <EOL> y_shapes = cgutils . unpack_tuple ( builder , y . shape ) <EOL> out_shapes = cgutils . unpack_tuple ( builder , out . shape ) <EOL> if xty . ndim < yty . ndim : <EOL> mty = yty <EOL> m_shapes = y_shapes <EOL> do_trans = yty . layout == '<STR_LIT:F>' <EOL> m_data , v_data = y . data , x . data <EOL> def check_args ( a , b , out ) : <EOL> m , = a . shape <EOL> _m , n = b . shape <EOL> if m != _m : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if out . shape != ( n , ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> mty = xty <EOL> m_shapes = x_shapes <EOL> do_trans = xty . layout == '<STR_LIT:C>' <EOL> m_data , v_data = x . data , y . data <EOL> def check_args ( a , b , out ) : <EOL> m , _n = a . shape <EOL> n , = b . shape <EOL> if n != _n : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if out . shape != ( m , ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> context . compile_internal ( builder , check_args , <EOL> signature ( types . none , * sig . args ) , args ) <EOL> for val in m_shapes : <EOL> check_c_int ( context , builder , val ) <EOL> call_xxgemv ( context , builder , do_trans , mty , m_shapes , m_data , <EOL> v_data , out . data ) <EOL> return impl_ret_borrowed ( context , builder , sig . return_type , <EOL> out . _getvalue ( ) ) <EOL> def dot_3_mm ( context , builder , sig , args ) : <EOL> """<STR_LIT>""" <EOL> xty , yty , outty = sig . args <EOL> assert outty == sig . return_type <EOL> dtype = xty . dtype <EOL> x = make_array ( xty ) ( context , builder , args [ <NUM_LIT:0> ] ) <EOL> y = make_array ( yty ) ( context , builder , args [ <NUM_LIT:1> ] ) <EOL> out = make_array ( outty ) ( context , builder , args [ <NUM_LIT:2> ] ) <EOL> x_shapes = cgutils . unpack_tuple ( builder , x . shape ) <EOL> y_shapes = cgutils . unpack_tuple ( builder , y . shape ) <EOL> out_shapes = cgutils . unpack_tuple ( builder , out . shape ) <EOL> m , k = x_shapes <EOL> _k , n = y_shapes <EOL> assert outty . layout == '<STR_LIT:C>' <EOL> def check_args ( a , b , out ) : <EOL> m , k = a . shape <EOL> _k , n = b . shape <EOL> if k != _k : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if out . shape != ( m , n ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> context . compile_internal ( builder , check_args , <EOL> signature ( types . none , * sig . args ) , args ) <EOL> check_c_int ( context , builder , m ) <EOL> check_c_int ( context , builder , k ) <EOL> check_c_int ( context , builder , n ) <EOL> x_data = x . data <EOL> y_data = y . data <EOL> out_data = out . data <EOL> one = ir . Constant ( intp_t , <NUM_LIT:1> ) <EOL> is_left_vec = builder . icmp_signed ( '<STR_LIT>' , m , one ) <EOL> is_right_vec = builder . icmp_signed ( '<STR_LIT>' , n , one ) <EOL> with builder . if_else ( is_right_vec ) as ( r_vec , r_mat ) : <EOL> with r_vec : <EOL> with builder . if_else ( is_left_vec ) as ( v_v , m_v ) : <EOL> with v_v : <EOL> call_xxdot ( context , builder , False , dtype , <EOL> k , x_data , y_data , out_data ) <EOL> with m_v : <EOL> do_trans = xty . layout == outty . layout <EOL> call_xxgemv ( context , builder , do_trans , <EOL> xty , x_shapes , x_data , y_data , out_data ) <EOL> with r_mat : <EOL> with builder . if_else ( is_left_vec ) as ( v_m , m_m ) : <EOL> with v_m : <EOL> do_trans = yty . layout != outty . layout <EOL> call_xxgemv ( context , builder , do_trans , <EOL> yty , y_shapes , y_data , x_data , out_data ) <EOL> with m_m : <EOL> call_xxgemm ( context , builder , <EOL> xty , x_shapes , x_data , <EOL> yty , y_shapes , y_data , <EOL> outty , out_shapes , out_data ) <EOL> return impl_ret_borrowed ( context , builder , sig . return_type , <EOL> out . _getvalue ( ) ) <EOL> @ lower_builtin ( numpy . dot , types . Array , types . Array , <EOL> types . Array ) <EOL> def dot_3 ( context , builder , sig , args ) : <EOL> """<STR_LIT>""" <EOL> ensure_blas ( ) <EOL> with make_contiguous ( context , builder , sig , args ) as ( sig , args ) : <EOL> ndims = set ( x . ndim for x in sig . args [ : <NUM_LIT:2> ] ) <EOL> if ndims == set ( [ <NUM_LIT:2> ] ) : <EOL> return dot_3_mm ( context , builder , sig , args ) <EOL> elif ndims == set ( [ <NUM_LIT:1> , <NUM_LIT:2> ] ) : <EOL> return dot_3_vm ( context , builder , sig , args ) <EOL> else : <EOL> assert <NUM_LIT:0> <EOL> def call_xxgetrf ( context , builder , a_type , a_shapes , a_data , ipiv , info ) : <EOL> """<STR_LIT>""" <EOL> fnty = ir . FunctionType ( ll_intc , <EOL> [ ll_char , <EOL> intp_t , intp_t , <EOL> ll_void_p , intp_t , <EOL> ll_intc_p , ll_intp_p , <EOL> ] ) <EOL> fn = builder . module . get_or_insert_function ( fnty , name = "<STR_LIT>" ) <EOL> kind = get_blas_kind ( a_type . dtype ) <EOL> kind_val = ir . Constant ( ll_char , ord ( kind ) ) <EOL> if a_type . layout == '<STR_LIT:F>' : <EOL> m , n = a_shapes <EOL> lda = a_shapes [ <NUM_LIT:0> ] <EOL> else : <EOL> n , m = a_shapes <EOL> lda = a_shapes [ <NUM_LIT:1> ] <EOL> res = builder . call ( fn , ( kind_val , m , n , <EOL> builder . bitcast ( a_data , ll_void_p ) , lda , <EOL> ipiv , info <EOL> ) ) <EOL> check_lapack_return ( context , builder , res ) <EOL> def call_xxgetri ( context , builder , a_type , a_shapes , a_data , ipiv , work , <EOL> lwork , info ) : <EOL> """<STR_LIT>""" <EOL> fnty = ir . FunctionType ( ll_intc , <EOL> [ ll_char , <EOL> intp_t , ll_void_p , intp_t , <EOL> ll_intc_p , ll_void_p , <EOL> ll_intc_p , ll_intp_p , <EOL> ] ) <EOL> fn = builder . module . get_or_insert_function ( fnty , name = "<STR_LIT>" ) <EOL> kind = get_blas_kind ( a_type . dtype ) <EOL> kind_val = ir . Constant ( ll_char , ord ( kind ) ) <EOL> n = lda = a_shapes [ <NUM_LIT:0> ] <EOL> res = builder . call ( fn , ( kind_val , n , <EOL> builder . bitcast ( a_data , ll_void_p ) , lda , <EOL> ipiv , builder . bitcast ( work , ll_void_p ) , <EOL> lwork , info <EOL> ) ) <EOL> check_lapack_return ( context , builder , res ) <EOL> def mat_inv ( context , builder , sig , args ) : <EOL> """<STR_LIT>""" <EOL> xty = sig . args [ <NUM_LIT:0> ] <EOL> dtype = xty . dtype <EOL> x = make_array ( xty ) ( context , builder , args [ <NUM_LIT:0> ] ) <EOL> x_shapes = cgutils . unpack_tuple ( builder , x . shape ) <EOL> m , n = x_shapes <EOL> check_c_int ( context , builder , m ) <EOL> check_c_int ( context , builder , n ) <EOL> def create_out ( a ) : <EOL> m , n = a . shape <EOL> if m != n : <EOL> raise numpy . linalg . LinAlgError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return a . copy ( ) <EOL> out = context . compile_internal ( builder , create_out , <EOL> signature ( sig . return_type , * sig . args ) , args ) <EOL> o = make_array ( xty ) ( context , builder , out ) <EOL> ipiv_t = types . Array ( types . intc , <NUM_LIT:1> , '<STR_LIT:C>' ) <EOL> i = _empty_nd_impl ( context , builder , ipiv_t , ( m , ) ) <EOL> ipiv = i . _getvalue ( ) <EOL> info = cgutils . alloca_once ( builder , intp_t ) <EOL> call_xxgetrf ( context , builder , xty , x_shapes , o . data , i . data , <EOL> info ) <EOL> info_val = builder . load ( info ) <EOL> zero = info_val . type ( <NUM_LIT:0> ) <EOL> lapack_error = builder . icmp_signed ( '<STR_LIT>' , info_val , zero ) <EOL> invalid_arg = builder . icmp_signed ( '<STR_LIT:<>' , info_val , zero ) <EOL> with builder . if_then ( lapack_error , False ) : <EOL> context . nrt_decref ( builder , ipiv_t , ipiv ) <EOL> with builder . if_else ( invalid_arg ) as ( then , otherwise ) : <EOL> raise_err = context . call_conv . return_user_exc <EOL> with then : <EOL> raise_err ( builder , ValueError , <EOL> ( '<STR_LIT>' , ) <EOL> ) <EOL> with otherwise : <EOL> raise_err ( builder , ValueError , <EOL> ( '<STR_LIT>' , ) <EOL> ) <EOL> lwork = make_constant_slot ( context , builder , types . intc , - <NUM_LIT:1> ) <EOL> work = cgutils . alloca_once ( builder , context . get_value_type ( xty . dtype ) ) <EOL> call_xxgetri ( context , builder , xty , x_shapes , o . data , i . data , work , <EOL> lwork , info ) <EOL> info_val = builder . load ( info ) <EOL> lapack_error = builder . icmp_signed ( '<STR_LIT>' , info_val , zero ) <EOL> with builder . if_then ( lapack_error , False ) : <EOL> context . nrt_decref ( builder , ipiv_t , ipiv ) <EOL> raise_err = context . call_conv . return_user_exc <EOL> raise_err ( builder , ValueError , <EOL> ( '<STR_LIT>' , ) <EOL> ) <EOL> def allocate_work ( x , size ) : <EOL> """<STR_LIT>""" <EOL> size = int ( <NUM_LIT> * size . real ) <EOL> return numpy . empty ( ( size , ) , dtype = x . dtype ) <EOL> wty = types . Array ( dtype , <NUM_LIT:1> , '<STR_LIT:C>' ) <EOL> work = context . compile_internal ( builder , allocate_work , <EOL> signature ( wty , xty , dtype ) , <EOL> ( args [ <NUM_LIT:0> ] , builder . load ( work ) ) ) <EOL> w = make_array ( wty ) ( context , builder , work ) <EOL> w_shapes = cgutils . unpack_tuple ( builder , w . shape ) <EOL> lw , = w_shapes <EOL> builder . store ( context . cast ( builder , lw , types . intp , types . intc ) , <EOL> lwork ) <EOL> call_xxgetri ( context , builder , xty , x_shapes , o . data , i . data , w . data , <EOL> lwork , info ) <EOL> info_val = builder . load ( info ) <EOL> lapack_error = builder . icmp_signed ( '<STR_LIT>' , info_val , zero ) <EOL> invalid_arg = builder . icmp_signed ( '<STR_LIT:<>' , info_val , zero ) <EOL> context . nrt_decref ( builder , wty , work ) <EOL> context . nrt_decref ( builder , ipiv_t , ipiv ) <EOL> with builder . if_then ( lapack_error , False ) : <EOL> with builder . if_else ( invalid_arg ) as ( then , otherwise ) : <EOL> raise_err = context . call_conv . return_user_exc <EOL> with then : <EOL> raise_err ( builder , ValueError , <EOL> ( '<STR_LIT>' , ) <EOL> ) <EOL> with otherwise : <EOL> raise_err ( builder , ValueError , <EOL> ( '<STR_LIT>' , ) <EOL> ) <EOL> return impl_ret_new_ref ( context , builder , sig . return_type , out ) <EOL> @ lower_builtin ( numpy . linalg . inv , types . Array ) <EOL> def inv ( context , builder , sig , args ) : <EOL> """<STR_LIT>""" <EOL> ensure_lapack ( ) <EOL> ndims = sig . args [ <NUM_LIT:0> ] . ndim <EOL> if ndims == <NUM_LIT:2> : <EOL> return mat_inv ( context , builder , sig , args ) <EOL> else : <EOL> assert <NUM_LIT:0> <EOL> fatal_error_sig = types . intc ( ) <EOL> fatal_error_func = types . ExternalFunction ( "<STR_LIT>" , fatal_error_sig ) <EOL> if numpy_version >= ( <NUM_LIT:1> , <NUM_LIT:8> ) : <EOL> @ jit ( nopython = True ) <EOL> def _check_finite_matrix ( a ) : <EOL> for v in numpy . nditer ( a ) : <EOL> if not numpy . isfinite ( v . item ( ) ) : <EOL> raise numpy . linalg . LinAlgError ( <EOL> "<STR_LIT>" ) <EOL> def _check_linalg_matrix ( a , func_name ) : <EOL> if not isinstance ( a , types . Array ) : <EOL> raise TypingError ( "<STR_LIT>" <EOL> % func_name ) <EOL> if not a . ndim == <NUM_LIT:2> : <EOL> raise TypingError ( "<STR_LIT>" <EOL> % func_name ) <EOL> if not isinstance ( a . dtype , ( types . Float , types . Complex ) ) : <EOL> raise TypingError ( "<STR_LIT>" <EOL> "<STR_LIT>" % func_name ) <EOL> @ overload ( numpy . linalg . cholesky ) <EOL> def cho_impl ( a ) : <EOL> ensure_lapack ( ) <EOL> _check_linalg_matrix ( a , "<STR_LIT>" ) <EOL> xxpotrf_sig = types . intc ( types . int8 , types . int8 , types . intp , <EOL> types . CPointer ( a . dtype ) , types . intp ) <EOL> xxpotrf = types . ExternalFunction ( "<STR_LIT>" , xxpotrf_sig ) <EOL> kind = ord ( get_blas_kind ( a . dtype , "<STR_LIT>" ) ) <EOL> UP = ord ( '<STR_LIT>' ) <EOL> LO = ord ( '<STR_LIT:L>' ) <EOL> def cho_impl ( a ) : <EOL> n = a . shape [ - <NUM_LIT:1> ] <EOL> if a . shape [ - <NUM_LIT:2> ] != n : <EOL> msg = "<STR_LIT>" <EOL> raise numpy . linalg . LinAlgError ( msg ) <EOL> out = a . copy ( ) <EOL> r = xxpotrf ( kind , UP , n , out . ctypes , n ) <EOL> if r < <NUM_LIT:0> : <EOL> fatal_error_func ( ) <EOL> assert <NUM_LIT:0> <EOL> if r > <NUM_LIT:0> : <EOL> raise numpy . linalg . LinAlgError ( <EOL> "<STR_LIT>" ) <EOL> for col in range ( n ) : <EOL> out [ : col , col ] = <NUM_LIT:0> <EOL> return out <EOL> return cho_impl <EOL> @ overload ( numpy . linalg . eig ) <EOL> def eig_impl ( a ) : <EOL> ensure_lapack ( ) <EOL> _check_linalg_matrix ( a , "<STR_LIT>" ) <EOL> numba_ez_rgeev_sig = types . intc ( types . char , <EOL> types . char , <EOL> types . char , <EOL> types . intp , <EOL> types . CPointer ( a . dtype ) , <EOL> types . intp , <EOL> types . CPointer ( a . dtype ) , <EOL> types . CPointer ( a . dtype ) , <EOL> types . CPointer ( a . dtype ) , <EOL> types . intp , <EOL> types . CPointer ( a . dtype ) , <EOL> types . intp <EOL> ) <EOL> numba_ez_rgeev = types . ExternalFunction ( "<STR_LIT>" , <EOL> numba_ez_rgeev_sig ) <EOL> numba_ez_cgeev_sig = types . intc ( types . char , <EOL> types . char , <EOL> types . char , <EOL> types . intp , <EOL> types . CPointer ( a . dtype ) , <EOL> types . intp , <EOL> types . CPointer ( a . dtype ) , <EOL> types . CPointer ( a . dtype ) , <EOL> types . intp , <EOL> types . CPointer ( a . dtype ) , <EOL> types . intp <EOL> ) <EOL> numba_ez_cgeev = types . ExternalFunction ( "<STR_LIT>" , <EOL> numba_ez_cgeev_sig ) <EOL> kind = ord ( get_blas_kind ( a . dtype , "<STR_LIT>" ) ) <EOL> JOBVL = ord ( '<STR_LIT:N>' ) <EOL> JOBVR = ord ( '<STR_LIT>' ) <EOL> F_layout = a . layout == '<STR_LIT:F>' <EOL> def real_eig_impl ( a ) : <EOL> """<STR_LIT>""" <EOL> n = a . shape [ - <NUM_LIT:1> ] <EOL> if a . shape [ - <NUM_LIT:2> ] != n : <EOL> msg = "<STR_LIT>" <EOL> raise numpy . linalg . LinAlgError ( msg ) <EOL> _check_finite_matrix ( a ) <EOL> if F_layout : <EOL> acpy = numpy . copy ( a ) <EOL> else : <EOL> acpy = numpy . asfortranarray ( a ) <EOL> ldvl = <NUM_LIT:1> <EOL> ldvr = n <EOL> wr = numpy . empty ( n , dtype = a . dtype ) <EOL> wi = numpy . empty ( n , dtype = a . dtype ) <EOL> vl = numpy . empty ( ( ldvl , n ) , dtype = a . dtype ) <EOL> vr = numpy . empty ( ( ldvr , n ) , dtype = a . dtype ) <EOL> r = numba_ez_rgeev ( kind , <EOL> JOBVL , <EOL> JOBVR , <EOL> n , <EOL> acpy . ctypes , <EOL> n , <EOL> wr . ctypes , <EOL> wi . ctypes , <EOL> vl . ctypes , <EOL> ldvl , <EOL> vr . ctypes , <EOL> ldvr ) <EOL> if r < <NUM_LIT:0> : <EOL> fatal_error_func ( ) <EOL> assert <NUM_LIT:0> <EOL> if numpy . any ( wi ) : <EOL> raise ValueError ( <EOL> "<STR_LIT>" ) <EOL> acpy . size <EOL> vl . size <EOL> vr . size <EOL> wr . size <EOL> wi . size <EOL> return ( wr , vr . T ) <EOL> def cmplx_eig_impl ( a ) : <EOL> """<STR_LIT>""" <EOL> n = a . shape [ - <NUM_LIT:1> ] <EOL> if a . shape [ - <NUM_LIT:2> ] != n : <EOL> msg = "<STR_LIT>" <EOL> raise numpy . linalg . LinAlgError ( msg ) <EOL> _check_finite_matrix ( a ) <EOL> if F_layout : <EOL> acpy = numpy . copy ( a ) <EOL> else : <EOL> acpy = numpy . asfortranarray ( a ) <EOL> ldvl = <NUM_LIT:1> <EOL> ldvr = n <EOL> w = numpy . empty ( n , dtype = a . dtype ) <EOL> vl = numpy . empty ( ( ldvl , n ) , dtype = a . dtype ) <EOL> vr = numpy . empty ( ( ldvr , n ) , dtype = a . dtype ) <EOL> r = numba_ez_cgeev ( kind , <EOL> JOBVL , <EOL> JOBVR , <EOL> n , <EOL> acpy . ctypes , <EOL> n , <EOL> w . ctypes , <EOL> vl . ctypes , <EOL> ldvl , <EOL> vr . ctypes , <EOL> ldvr ) <EOL> if r < <NUM_LIT:0> : <EOL> fatal_error_func ( ) <EOL> assert <NUM_LIT:0> <EOL> acpy . size <EOL> vl . size <EOL> vr . size <EOL> w . size <EOL> return ( w , vr . T ) <EOL> if isinstance ( a . dtype , types . scalars . Complex ) : <EOL> return cmplx_eig_impl <EOL> else : <EOL> return real_eig_impl <EOL> @ overload ( numpy . linalg . svd ) <EOL> def svd_impl ( a , full_matrices = <NUM_LIT:1> ) : <EOL> ensure_lapack ( ) <EOL> _check_linalg_matrix ( a , "<STR_LIT>" ) <EOL> F_layout = a . layout == '<STR_LIT:F>' <EOL> s_type = getattr ( a . dtype , "<STR_LIT>" , a . dtype ) <EOL> if s_type . bitwidth == <NUM_LIT:32> : <EOL> s_dtype = numpy . float32 <EOL> else : <EOL> s_dtype = numpy . float64 <EOL> numba_ez_gesdd_sig = types . intc ( <EOL> types . char , <EOL> types . char , <EOL> types . intp , <EOL> types . intp , <EOL> types . CPointer ( a . dtype ) , <EOL> types . intp , <EOL> types . CPointer ( s_type ) , <EOL> types . CPointer ( a . dtype ) , <EOL> types . intp , <EOL> types . CPointer ( a . dtype ) , <EOL> types . intp <EOL> ) <EOL> numba_ez_gesdd = types . ExternalFunction ( "<STR_LIT>" , <EOL> numba_ez_gesdd_sig ) <EOL> kind = ord ( get_blas_kind ( a . dtype , "<STR_LIT>" ) ) <EOL> JOBZ_A = ord ( '<STR_LIT:A>' ) <EOL> JOBZ_S = ord ( '<STR_LIT:S>' ) <EOL> def svd_impl ( a , full_matrices = <NUM_LIT:1> ) : <EOL> n = a . shape [ - <NUM_LIT:1> ] <EOL> m = a . shape [ - <NUM_LIT:2> ] <EOL> _check_finite_matrix ( a ) <EOL> if F_layout : <EOL> acpy = numpy . copy ( a ) <EOL> else : <EOL> acpy = numpy . asfortranarray ( a ) <EOL> ldu = m <EOL> minmn = min ( m , n ) <EOL> if full_matrices : <EOL> JOBZ = JOBZ_A <EOL> ucol = m <EOL> ldvt = n <EOL> else : <EOL> JOBZ = JOBZ_S <EOL> ucol = minmn <EOL> ldvt = minmn <EOL> u = numpy . empty ( ( ucol , ldu ) , dtype = a . dtype ) <EOL> s = numpy . empty ( minmn , dtype = s_dtype ) <EOL> vt = numpy . empty ( ( n , ldvt ) , dtype = a . dtype ) <EOL> r = numba_ez_gesdd ( <EOL> kind , <EOL> JOBZ , <EOL> m , <EOL> n , <EOL> acpy . ctypes , <EOL> m , <EOL> s . ctypes , <EOL> u . ctypes , <EOL> ldu , <EOL> vt . ctypes , <EOL> ldvt <EOL> ) <EOL> if r < <NUM_LIT:0> : <EOL> fatal_error_func ( ) <EOL> assert <NUM_LIT:0> <EOL> acpy . size <EOL> vt . size <EOL> u . size <EOL> s . size <EOL> return ( u . T , s , vt . T ) <EOL> return svd_impl </s>
