<s> """<STR_LIT>""" <EOL> from scipy . sparse . linalg . eigen . arpack import _arpack <EOL> import numpy as np <EOL> from scipy . sparse . linalg . interface import aslinearoperator , LinearOperator <EOL> from scipy . sparse import identity , isspmatrix , isspmatrix_csr <EOL> from scipy . linalg import lu_factor , lu_solve <EOL> from scipy . sparse . sputils import isdense <EOL> from scipy . sparse . linalg import gmres , splu <EOL> import scipy <EOL> import functools <EOL> import operator <EOL> from distutils . version import LooseVersion <EOL> __docformat__ = "<STR_LIT>" <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> _type_conv = { '<STR_LIT:f>' : '<STR_LIT:s>' , '<STR_LIT:d>' : '<STR_LIT:d>' , '<STR_LIT:F>' : '<STR_LIT:c>' , '<STR_LIT:D>' : '<STR_LIT:z>' } <EOL> _ndigits = { '<STR_LIT:f>' : <NUM_LIT:5> , '<STR_LIT:d>' : <NUM_LIT:12> , '<STR_LIT:F>' : <NUM_LIT:5> , '<STR_LIT:D>' : <NUM_LIT:12> } <EOL> DNAUPD_ERRORS = { <EOL> <NUM_LIT:0> : "<STR_LIT>" , <EOL> <NUM_LIT:1> : "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> <NUM_LIT:2> : "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> <NUM_LIT:3> : "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> - <NUM_LIT:1> : "<STR_LIT>" , <EOL> - <NUM_LIT:2> : "<STR_LIT>" , <EOL> - <NUM_LIT:3> : "<STR_LIT>" , <EOL> - <NUM_LIT:4> : "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> - <NUM_LIT:5> : "<STR_LIT>" , <EOL> - <NUM_LIT:6> : "<STR_LIT>" , <EOL> - <NUM_LIT:7> : "<STR_LIT>" , <EOL> - <NUM_LIT:8> : "<STR_LIT>" , <EOL> - <NUM_LIT:9> : "<STR_LIT>" , <EOL> - <NUM_LIT:10> : "<STR_LIT>" , <EOL> - <NUM_LIT:11> : "<STR_LIT>" , <EOL> - <NUM_LIT:12> : "<STR_LIT>" , <EOL> - <NUM_LIT> : "<STR_LIT>" , <EOL> - <NUM_LIT> : "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> } <EOL> SNAUPD_ERRORS = DNAUPD_ERRORS <EOL> ZNAUPD_ERRORS = DNAUPD_ERRORS . copy ( ) <EOL> ZNAUPD_ERRORS [ - <NUM_LIT:10> ] = "<STR_LIT>" <EOL> CNAUPD_ERRORS = ZNAUPD_ERRORS <EOL> DSAUPD_ERRORS = { <EOL> <NUM_LIT:0> : "<STR_LIT>" , <EOL> <NUM_LIT:1> : "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> <NUM_LIT:2> : "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> <NUM_LIT:3> : "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> - <NUM_LIT:1> : "<STR_LIT>" , <EOL> - <NUM_LIT:2> : "<STR_LIT>" , <EOL> - <NUM_LIT:3> : "<STR_LIT>" , <EOL> - <NUM_LIT:4> : "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> - <NUM_LIT:5> : "<STR_LIT>" , <EOL> - <NUM_LIT:6> : "<STR_LIT>" , <EOL> - <NUM_LIT:7> : "<STR_LIT>" , <EOL> - <NUM_LIT:8> : "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> - <NUM_LIT:9> : "<STR_LIT>" , <EOL> - <NUM_LIT:10> : "<STR_LIT>" , <EOL> - <NUM_LIT:11> : "<STR_LIT>" , <EOL> - <NUM_LIT:12> : "<STR_LIT>" , <EOL> - <NUM_LIT> : "<STR_LIT>" , <EOL> - <NUM_LIT> : "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> } <EOL> SSAUPD_ERRORS = DSAUPD_ERRORS <EOL> DNEUPD_ERRORS = { <EOL> <NUM_LIT:0> : "<STR_LIT>" , <EOL> <NUM_LIT:1> : "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> - <NUM_LIT:1> : "<STR_LIT>" , <EOL> - <NUM_LIT:2> : "<STR_LIT>" , <EOL> - <NUM_LIT:3> : "<STR_LIT>" , <EOL> - <NUM_LIT:5> : "<STR_LIT>" , <EOL> - <NUM_LIT:6> : "<STR_LIT>" , <EOL> - <NUM_LIT:7> : "<STR_LIT>" , <EOL> - <NUM_LIT:8> : "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> - <NUM_LIT:9> : "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> - <NUM_LIT:10> : "<STR_LIT>" , <EOL> - <NUM_LIT:11> : "<STR_LIT>" , <EOL> - <NUM_LIT:12> : "<STR_LIT>" , <EOL> - <NUM_LIT> : "<STR_LIT>" , <EOL> - <NUM_LIT> : "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> - <NUM_LIT:15> : "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> } <EOL> SNEUPD_ERRORS = DNEUPD_ERRORS . copy ( ) <EOL> SNEUPD_ERRORS [ <NUM_LIT:1> ] = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> SNEUPD_ERRORS [ - <NUM_LIT> ] = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> SNEUPD_ERRORS [ - <NUM_LIT:15> ] = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> ZNEUPD_ERRORS = { <NUM_LIT:0> : "<STR_LIT>" , <EOL> <NUM_LIT:1> : "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> - <NUM_LIT:1> : "<STR_LIT>" , <EOL> - <NUM_LIT:2> : "<STR_LIT>" , <EOL> - <NUM_LIT:3> : "<STR_LIT>" , <EOL> - <NUM_LIT:5> : "<STR_LIT>" , <EOL> - <NUM_LIT:6> : "<STR_LIT>" , <EOL> - <NUM_LIT:7> : "<STR_LIT>" , <EOL> - <NUM_LIT:8> : "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> - <NUM_LIT:9> : "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> - <NUM_LIT:10> : "<STR_LIT>" , <EOL> - <NUM_LIT:11> : "<STR_LIT>" , <EOL> - <NUM_LIT:12> : "<STR_LIT>" , <EOL> - <NUM_LIT> : "<STR_LIT>" , <EOL> - <NUM_LIT> : "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> - <NUM_LIT:15> : "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> } <EOL> CNEUPD_ERRORS = ZNEUPD_ERRORS . copy ( ) <EOL> CNEUPD_ERRORS [ - <NUM_LIT> ] = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> CNEUPD_ERRORS [ - <NUM_LIT:15> ] = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> DSEUPD_ERRORS = { <EOL> <NUM_LIT:0> : "<STR_LIT>" , <EOL> - <NUM_LIT:1> : "<STR_LIT>" , <EOL> - <NUM_LIT:2> : "<STR_LIT>" , <EOL> - <NUM_LIT:3> : "<STR_LIT>" , <EOL> - <NUM_LIT:5> : "<STR_LIT>" , <EOL> - <NUM_LIT:6> : "<STR_LIT>" , <EOL> - <NUM_LIT:7> : "<STR_LIT>" , <EOL> - <NUM_LIT:8> : ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> - <NUM_LIT:9> : "<STR_LIT>" , <EOL> - <NUM_LIT:10> : "<STR_LIT>" , <EOL> - <NUM_LIT:11> : "<STR_LIT>" , <EOL> - <NUM_LIT:12> : "<STR_LIT>" , <EOL> - <NUM_LIT> : "<STR_LIT>" , <EOL> - <NUM_LIT:15> : "<STR_LIT>" , <EOL> - <NUM_LIT:16> : "<STR_LIT>" , <EOL> - <NUM_LIT> : ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> } <EOL> SSEUPD_ERRORS = DSEUPD_ERRORS . copy ( ) <EOL> SSEUPD_ERRORS [ - <NUM_LIT> ] = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> SSEUPD_ERRORS [ - <NUM_LIT> ] = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> _SAUPD_ERRORS = { '<STR_LIT:d>' : DSAUPD_ERRORS , <EOL> '<STR_LIT:s>' : SSAUPD_ERRORS } <EOL> _NAUPD_ERRORS = { '<STR_LIT:d>' : DNAUPD_ERRORS , <EOL> '<STR_LIT:s>' : SNAUPD_ERRORS , <EOL> '<STR_LIT:z>' : ZNAUPD_ERRORS , <EOL> '<STR_LIT:c>' : CNAUPD_ERRORS } <EOL> _SEUPD_ERRORS = { '<STR_LIT:d>' : DSEUPD_ERRORS , <EOL> '<STR_LIT:s>' : SSEUPD_ERRORS } <EOL> _NEUPD_ERRORS = { '<STR_LIT:d>' : DNEUPD_ERRORS , <EOL> '<STR_LIT:s>' : SNEUPD_ERRORS , <EOL> '<STR_LIT:z>' : ZNEUPD_ERRORS , <EOL> '<STR_LIT:c>' : CNEUPD_ERRORS } <EOL> _SEUPD_WHICH = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> _NEUPD_WHICH = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> if scipy . version . version >= LooseVersion ( '<STR_LIT>' ) : <EOL> BACKPORT_TO = None <EOL> elif scipy . version . version >= LooseVersion ( '<STR_LIT>' ) : <EOL> BACKPORT_TO = '<STR_LIT>' <EOL> else : <EOL> BACKPORT_TO = '<STR_LIT>' <EOL> def _aligned_zeros ( shape , dtype = float , order = "<STR_LIT:C>" , align = None ) : <EOL> """<STR_LIT>""" <EOL> dtype = np . dtype ( dtype ) <EOL> if align is None : <EOL> align = dtype . alignment <EOL> if not hasattr ( shape , '<STR_LIT>' ) : <EOL> shape = ( shape , ) <EOL> size = functools . reduce ( operator . mul , shape ) * dtype . itemsize <EOL> buf = np . empty ( size + align + <NUM_LIT:1> , np . uint8 ) <EOL> offset = buf . __array_interface__ [ '<STR_LIT:data>' ] [ <NUM_LIT:0> ] % align <EOL> if offset != <NUM_LIT:0> : <EOL> offset = align - offset <EOL> buf = buf [ offset : offset + size + <NUM_LIT:1> ] [ : - <NUM_LIT:1> ] <EOL> data = np . ndarray ( shape , dtype , buf , order = order ) <EOL> data . fill ( <NUM_LIT:0> ) <EOL> return data <EOL> class ArpackError ( RuntimeError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , info , infodict = _NAUPD_ERRORS ) : <EOL> msg = infodict . get ( info , "<STR_LIT>" ) <EOL> RuntimeError . __init__ ( self , "<STR_LIT>" % ( info , msg ) ) <EOL> class ArpackNoConvergence ( ArpackError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , msg , eigenvalues , eigenvectors ) : <EOL> ArpackError . __init__ ( self , - <NUM_LIT:1> , { - <NUM_LIT:1> : msg } ) <EOL> self . eigenvalues = eigenvalues <EOL> self . eigenvectors = eigenvectors <EOL> class _ArpackParams ( object ) : <EOL> def __init__ ( self , n , k , tp , mode = <NUM_LIT:1> , sigma = None , <EOL> ncv = None , v0 = None , maxiter = None , which = "<STR_LIT>" , tol = <NUM_LIT:0> ) : <EOL> if k <= <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" % k ) <EOL> if maxiter is None : <EOL> maxiter = n * <NUM_LIT:10> <EOL> if maxiter <= <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" % maxiter ) <EOL> if tp not in '<STR_LIT>' : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if v0 is not None : <EOL> self . resid = np . array ( v0 , copy = True ) <EOL> info = <NUM_LIT:1> <EOL> else : <EOL> self . resid = np . zeros ( n , tp ) <EOL> info = <NUM_LIT:0> <EOL> if sigma is None : <EOL> self . sigma = <NUM_LIT:0> <EOL> else : <EOL> self . sigma = sigma <EOL> if ncv is None : <EOL> ncv = <NUM_LIT:2> * k + <NUM_LIT:1> <EOL> ncv = min ( ncv , n ) <EOL> self . v = np . zeros ( ( n , ncv ) , tp ) <EOL> self . iparam = np . zeros ( <NUM_LIT:11> , "<STR_LIT:int>" ) <EOL> ishfts = <NUM_LIT:1> <EOL> self . mode = mode <EOL> self . iparam [ <NUM_LIT:0> ] = ishfts <EOL> self . iparam [ <NUM_LIT:2> ] = maxiter <EOL> self . iparam [ <NUM_LIT:3> ] = <NUM_LIT:1> <EOL> self . iparam [ <NUM_LIT:6> ] = mode <EOL> self . n = n <EOL> self . tol = tol <EOL> self . k = k <EOL> self . maxiter = maxiter <EOL> self . ncv = ncv <EOL> self . which = which <EOL> self . tp = tp <EOL> self . info = info <EOL> self . converged = False <EOL> self . ido = <NUM_LIT:0> <EOL> def _raise_no_convergence ( self ) : <EOL> msg = "<STR_LIT>" <EOL> k_ok = self . iparam [ <NUM_LIT:4> ] <EOL> num_iter = self . iparam [ <NUM_LIT:2> ] <EOL> try : <EOL> ev , vec = self . extract ( True ) <EOL> except ArpackError as err : <EOL> msg = "<STR_LIT>" % ( msg , err ) <EOL> ev = np . zeros ( ( <NUM_LIT:0> , ) ) <EOL> vec = np . zeros ( ( self . n , <NUM_LIT:0> ) ) <EOL> k_ok = <NUM_LIT:0> <EOL> raise ArpackNoConvergence ( msg % ( num_iter , k_ok , self . k ) , ev , vec ) <EOL> class _SymmetricArpackParams ( _ArpackParams ) : <EOL> def __init__ ( self , n , k , tp , matvec , mode = <NUM_LIT:1> , M_matvec = None , <EOL> Minv_matvec = None , sigma = None , <EOL> ncv = None , v0 = None , maxiter = None , which = "<STR_LIT>" , tol = <NUM_LIT:0> ) : <EOL> if mode == <NUM_LIT:1> : <EOL> if matvec is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if M_matvec is not None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if Minv_matvec is not None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . OP = matvec <EOL> self . B = lambda x : x <EOL> self . bmat = '<STR_LIT:I>' <EOL> elif mode == <NUM_LIT:2> : <EOL> if matvec is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if M_matvec is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if Minv_matvec is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . OP = lambda x : Minv_matvec ( matvec ( x ) ) <EOL> self . OPa = Minv_matvec <EOL> self . OPb = matvec <EOL> self . B = M_matvec <EOL> self . bmat = '<STR_LIT>' <EOL> elif mode == <NUM_LIT:3> : <EOL> if matvec is not None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if Minv_matvec is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if M_matvec is None : <EOL> self . OP = Minv_matvec <EOL> self . OPa = Minv_matvec <EOL> self . B = lambda x : x <EOL> self . bmat = '<STR_LIT:I>' <EOL> else : <EOL> self . OP = lambda x : Minv_matvec ( M_matvec ( x ) ) <EOL> self . OPa = Minv_matvec <EOL> self . B = M_matvec <EOL> self . bmat = '<STR_LIT>' <EOL> elif mode == <NUM_LIT:4> : <EOL> if matvec is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if M_matvec is not None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if Minv_matvec is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . OPa = Minv_matvec <EOL> self . OP = lambda x : self . OPa ( matvec ( x ) ) <EOL> self . B = matvec <EOL> self . bmat = '<STR_LIT>' <EOL> elif mode == <NUM_LIT:5> : <EOL> if matvec is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if Minv_matvec is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . OPa = Minv_matvec <EOL> self . A_matvec = matvec <EOL> if M_matvec is None : <EOL> self . OP = lambda x : Minv_matvec ( matvec ( x ) + sigma * x ) <EOL> self . B = lambda x : x <EOL> self . bmat = '<STR_LIT:I>' <EOL> else : <EOL> self . OP = lambda x : Minv_matvec ( matvec ( x ) + <EOL> sigma * M_matvec ( x ) ) <EOL> self . B = M_matvec <EOL> self . bmat = '<STR_LIT>' <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % mode ) <EOL> if which not in _SEUPD_WHICH : <EOL> raise ValueError ( "<STR_LIT>" <EOL> % '<STR_LIT:U+0020>' . join ( _SEUPD_WHICH ) ) <EOL> if k >= n : <EOL> raise ValueError ( "<STR_LIT>" % k ) <EOL> _ArpackParams . __init__ ( self , n , k , tp , mode , sigma , <EOL> ncv , v0 , maxiter , which , tol ) <EOL> if self . ncv > n or self . ncv <= k : <EOL> raise ValueError ( "<STR_LIT>" % self . ncv ) <EOL> self . workd = _aligned_zeros ( <NUM_LIT:3> * n , self . tp ) <EOL> self . workl = _aligned_zeros ( self . ncv * ( self . ncv + <NUM_LIT:8> ) , self . tp ) <EOL> ltr = _type_conv [ self . tp ] <EOL> if ltr not in [ "<STR_LIT:s>" , "<STR_LIT:d>" ] : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . _arpack_solver = _arpack . __dict__ [ ltr + '<STR_LIT>' ] <EOL> self . _arpack_extract = _arpack . __dict__ [ ltr + '<STR_LIT>' ] <EOL> self . iterate_infodict = _SAUPD_ERRORS [ ltr ] <EOL> self . extract_infodict = _SEUPD_ERRORS [ ltr ] <EOL> self . ipntr = np . zeros ( <NUM_LIT:11> , "<STR_LIT:int>" ) <EOL> def iterate ( self ) : <EOL> if BACKPORT_TO is None : <EOL> return None <EOL> if BACKPORT_TO == '<STR_LIT>' : <EOL> self . ido , self . tol , self . resid , self . v , self . iparam , self . ipntr , self . info = self . _arpack_solver ( self . ido , self . bmat , self . which , self . k , <EOL> self . tol , self . resid , self . v , self . iparam , <EOL> self . ipntr , self . workd , self . workl , self . info ) <EOL> elif BACKPORT_TO == '<STR_LIT>' : <EOL> self . ido , self . resid , self . v , self . iparam , self . ipntr , self . info = self . _arpack_solver ( self . ido , self . bmat , self . which , self . k , <EOL> self . tol , self . resid , self . v , self . iparam , <EOL> self . ipntr , self . workd , self . workl , self . info ) <EOL> xslice = slice ( self . ipntr [ <NUM_LIT:0> ] - <NUM_LIT:1> , self . ipntr [ <NUM_LIT:0> ] - <NUM_LIT:1> + self . n ) <EOL> yslice = slice ( self . ipntr [ <NUM_LIT:1> ] - <NUM_LIT:1> , self . ipntr [ <NUM_LIT:1> ] - <NUM_LIT:1> + self . n ) <EOL> if self . ido == - <NUM_LIT:1> : <EOL> self . workd [ yslice ] = self . OP ( self . workd [ xslice ] ) <EOL> elif self . ido == <NUM_LIT:1> : <EOL> if self . mode == <NUM_LIT:1> : <EOL> self . workd [ yslice ] = self . OP ( self . workd [ xslice ] ) <EOL> elif self . mode == <NUM_LIT:2> : <EOL> self . workd [ xslice ] = self . OPb ( self . workd [ xslice ] ) <EOL> self . workd [ yslice ] = self . OPa ( self . workd [ xslice ] ) <EOL> elif self . mode == <NUM_LIT:5> : <EOL> Bxslice = slice ( self . ipntr [ <NUM_LIT:2> ] - <NUM_LIT:1> , self . ipntr [ <NUM_LIT:2> ] - <NUM_LIT:1> + self . n ) <EOL> Ax = self . A_matvec ( self . workd [ xslice ] ) <EOL> self . workd [ yslice ] = self . OPa ( Ax + ( self . sigma * <EOL> self . workd [ Bxslice ] ) ) <EOL> else : <EOL> Bxslice = slice ( self . ipntr [ <NUM_LIT:2> ] - <NUM_LIT:1> , self . ipntr [ <NUM_LIT:2> ] - <NUM_LIT:1> + self . n ) <EOL> self . workd [ yslice ] = self . OPa ( self . workd [ Bxslice ] ) <EOL> elif self . ido == <NUM_LIT:2> : <EOL> self . workd [ yslice ] = self . B ( self . workd [ xslice ] ) <EOL> elif self . ido == <NUM_LIT:3> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> else : <EOL> self . converged = True <EOL> if self . info == <NUM_LIT:0> : <EOL> pass <EOL> elif self . info == <NUM_LIT:1> : <EOL> self . _raise_no_convergence ( ) <EOL> else : <EOL> raise ArpackError ( self . info , infodict = self . iterate_infodict ) <EOL> def extract ( self , return_eigenvectors ) : <EOL> rvec = return_eigenvectors <EOL> ierr = <NUM_LIT:0> <EOL> howmny = '<STR_LIT:A>' <EOL> sselect = np . zeros ( self . ncv , '<STR_LIT:int>' ) <EOL> d , z , ierr = self . _arpack_extract ( rvec , howmny , sselect , self . sigma , <EOL> self . bmat , self . which , self . k , <EOL> self . tol , self . resid , self . v , <EOL> self . iparam [ <NUM_LIT:0> : <NUM_LIT:7> ] , self . ipntr , <EOL> self . workd [ <NUM_LIT:0> : <NUM_LIT:2> * self . n ] , <EOL> self . workl , ierr ) <EOL> if ierr != <NUM_LIT:0> : <EOL> raise ArpackError ( ierr , infodict = self . extract_infodict ) <EOL> k_ok = self . iparam [ <NUM_LIT:4> ] <EOL> d = d [ : k_ok ] <EOL> z = z [ : , : k_ok ] <EOL> if return_eigenvectors : <EOL> return d , z <EOL> else : <EOL> return d <EOL> class _UnsymmetricArpackParams ( _ArpackParams ) : <EOL> def __init__ ( self , n , k , tp , matvec , mode = <NUM_LIT:1> , M_matvec = None , <EOL> Minv_matvec = None , sigma = None , <EOL> ncv = None , v0 = None , maxiter = None , which = "<STR_LIT>" , tol = <NUM_LIT:0> ) : <EOL> if mode == <NUM_LIT:1> : <EOL> if matvec is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if M_matvec is not None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if Minv_matvec is not None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . OP = matvec <EOL> self . B = lambda x : x <EOL> self . bmat = '<STR_LIT:I>' <EOL> elif mode == <NUM_LIT:2> : <EOL> if matvec is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if M_matvec is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if Minv_matvec is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . OP = lambda x : Minv_matvec ( matvec ( x ) ) <EOL> self . OPa = Minv_matvec <EOL> self . OPb = matvec <EOL> self . B = M_matvec <EOL> self . bmat = '<STR_LIT>' <EOL> elif mode in ( <NUM_LIT:3> , <NUM_LIT:4> ) : <EOL> if matvec is None : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if Minv_matvec is None : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . matvec = matvec <EOL> if tp in '<STR_LIT>' : <EOL> if mode == <NUM_LIT:3> : <EOL> self . OPa = Minv_matvec <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> else : <EOL> if mode == <NUM_LIT:3> : <EOL> self . OPa = lambda x : np . real ( Minv_matvec ( x ) ) <EOL> else : <EOL> self . OPa = lambda x : np . imag ( Minv_matvec ( x ) ) <EOL> if M_matvec is None : <EOL> self . B = lambda x : x <EOL> self . bmat = '<STR_LIT:I>' <EOL> self . OP = self . OPa <EOL> else : <EOL> self . B = M_matvec <EOL> self . bmat = '<STR_LIT>' <EOL> self . OP = lambda x : self . OPa ( M_matvec ( x ) ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % mode ) <EOL> if which not in _NEUPD_WHICH : <EOL> raise ValueError ( "<STR_LIT>" <EOL> % '<STR_LIT:U+0020>' . join ( _NEUPD_WHICH ) ) <EOL> if k >= n - <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" % k ) <EOL> _ArpackParams . __init__ ( self , n , k , tp , mode , sigma , <EOL> ncv , v0 , maxiter , which , tol ) <EOL> if self . ncv > n or self . ncv <= k + <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" % self . ncv ) <EOL> self . workd = _aligned_zeros ( <NUM_LIT:3> * n , self . tp ) <EOL> self . workl = _aligned_zeros ( <NUM_LIT:3> * self . ncv * ( self . ncv + <NUM_LIT:2> ) , self . tp ) <EOL> ltr = _type_conv [ self . tp ] <EOL> self . _arpack_solver = _arpack . __dict__ [ ltr + '<STR_LIT>' ] <EOL> self . _arpack_extract = _arpack . __dict__ [ ltr + '<STR_LIT>' ] <EOL> self . iterate_infodict = _NAUPD_ERRORS [ ltr ] <EOL> self . extract_infodict = _NEUPD_ERRORS [ ltr ] <EOL> self . ipntr = np . zeros ( <NUM_LIT> , "<STR_LIT:int>" ) <EOL> if self . tp in '<STR_LIT>' : <EOL> self . rwork = _aligned_zeros ( self . ncv , self . tp . lower ( ) ) <EOL> else : <EOL> self . rwork = None <EOL> def iterate ( self ) : <EOL> if BACKPORT_TO is None : <EOL> return None <EOL> if BACKPORT_TO == '<STR_LIT>' : <EOL> if self . tp in '<STR_LIT>' : <EOL> self . ido , self . tol , self . resid , self . v , self . iparam , self . ipntr , self . info = self . _arpack_solver ( self . ido , self . bmat , self . which , self . k , <EOL> self . tol , self . resid , self . v , self . iparam , <EOL> self . ipntr , self . workd , self . workl , <EOL> self . info ) <EOL> else : <EOL> self . ido , self . tol , self . resid , self . v , self . iparam , self . ipntr , self . info = self . _arpack_solver ( self . ido , self . bmat , self . which , self . k , <EOL> self . tol , self . resid , self . v , self . iparam , <EOL> self . ipntr , self . workd , self . workl , <EOL> self . rwork , self . info ) <EOL> elif BACKPORT_TO == '<STR_LIT>' : <EOL> if self . tp in '<STR_LIT>' : <EOL> self . ido , self . resid , self . v , self . iparam , self . ipntr , self . info = self . _arpack_solver ( self . ido , self . bmat , self . which , self . k , <EOL> self . tol , self . resid , self . v , self . iparam , <EOL> self . ipntr , self . workd , self . workl , <EOL> self . info ) <EOL> else : <EOL> self . ido , self . resid , self . v , self . iparam , self . ipntr , self . info = self . _arpack_solver ( self . ido , self . bmat , self . which , self . k , <EOL> self . tol , self . resid , self . v , self . iparam , <EOL> self . ipntr , self . workd , self . workl , <EOL> self . rwork , self . info ) <EOL> xslice = slice ( self . ipntr [ <NUM_LIT:0> ] - <NUM_LIT:1> , self . ipntr [ <NUM_LIT:0> ] - <NUM_LIT:1> + self . n ) <EOL> yslice = slice ( self . ipntr [ <NUM_LIT:1> ] - <NUM_LIT:1> , self . ipntr [ <NUM_LIT:1> ] - <NUM_LIT:1> + self . n ) <EOL> if self . ido == - <NUM_LIT:1> : <EOL> self . workd [ yslice ] = self . OP ( self . workd [ xslice ] ) <EOL> elif self . ido == <NUM_LIT:1> : <EOL> if self . mode in ( <NUM_LIT:1> , <NUM_LIT:2> ) : <EOL> self . workd [ yslice ] = self . OP ( self . workd [ xslice ] ) <EOL> else : <EOL> Bxslice = slice ( self . ipntr [ <NUM_LIT:2> ] - <NUM_LIT:1> , self . ipntr [ <NUM_LIT:2> ] - <NUM_LIT:1> + self . n ) <EOL> self . workd [ yslice ] = self . OPa ( self . workd [ Bxslice ] ) <EOL> elif self . ido == <NUM_LIT:2> : <EOL> self . workd [ yslice ] = self . B ( self . workd [ xslice ] ) <EOL> elif self . ido == <NUM_LIT:3> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> else : <EOL> self . converged = True <EOL> if self . info == <NUM_LIT:0> : <EOL> pass <EOL> elif self . info == <NUM_LIT:1> : <EOL> self . _raise_no_convergence ( ) <EOL> else : <EOL> raise ArpackError ( self . info , infodict = self . iterate_infodict ) <EOL> def extract ( self , return_eigenvectors ) : <EOL> k , n = self . k , self . n <EOL> ierr = <NUM_LIT:0> <EOL> howmny = '<STR_LIT:A>' <EOL> sselect = np . zeros ( self . ncv , '<STR_LIT:int>' ) <EOL> sigmar = np . real ( self . sigma ) <EOL> sigmai = np . imag ( self . sigma ) <EOL> workev = np . zeros ( <NUM_LIT:3> * self . ncv , self . tp ) <EOL> if self . tp in '<STR_LIT>' : <EOL> dr = np . zeros ( k + <NUM_LIT:1> , self . tp ) <EOL> di = np . zeros ( k + <NUM_LIT:1> , self . tp ) <EOL> zr = np . zeros ( ( n , k + <NUM_LIT:1> ) , self . tp ) <EOL> dr , di , zr , ierr = self . _arpack_extract ( return_eigenvectors , <EOL> howmny , sselect , sigmar , sigmai , workev , <EOL> self . bmat , self . which , k , self . tol , self . resid , <EOL> self . v , self . iparam , self . ipntr , <EOL> self . workd , self . workl , self . info ) <EOL> if ierr != <NUM_LIT:0> : <EOL> raise ArpackError ( ierr , infodict = self . extract_infodict ) <EOL> nreturned = self . iparam [ <NUM_LIT:4> ] <EOL> d = dr + <NUM_LIT> * di <EOL> z = zr . astype ( self . tp . upper ( ) ) <EOL> if sigmai == <NUM_LIT:0> : <EOL> i = <NUM_LIT:0> <EOL> while i <= k : <EOL> if abs ( d [ i ] . imag ) != <NUM_LIT:0> : <EOL> if i < k : <EOL> z [ : , i ] = zr [ : , i ] + <NUM_LIT> * zr [ : , i + <NUM_LIT:1> ] <EOL> z [ : , i + <NUM_LIT:1> ] = z [ : , i ] . conjugate ( ) <EOL> i += <NUM_LIT:1> <EOL> else : <EOL> nreturned -= <NUM_LIT:1> <EOL> i += <NUM_LIT:1> <EOL> else : <EOL> i = <NUM_LIT:0> <EOL> while i <= k : <EOL> if abs ( d [ i ] . imag ) == <NUM_LIT:0> : <EOL> d [ i ] = np . dot ( zr [ : , i ] , self . matvec ( zr [ : , i ] ) ) <EOL> else : <EOL> if i < k : <EOL> z [ : , i ] = zr [ : , i ] + <NUM_LIT> * zr [ : , i + <NUM_LIT:1> ] <EOL> z [ : , i + <NUM_LIT:1> ] = z [ : , i ] . conjugate ( ) <EOL> d [ i ] = ( ( np . dot ( zr [ : , i ] , <EOL> self . matvec ( zr [ : , i ] ) ) + <EOL> np . dot ( zr [ : , i + <NUM_LIT:1> ] , <EOL> self . matvec ( zr [ : , i + <NUM_LIT:1> ] ) ) ) + <EOL> <NUM_LIT> * ( np . dot ( zr [ : , i ] , <EOL> self . matvec ( zr [ : , i + <NUM_LIT:1> ] ) ) - <EOL> np . dot ( zr [ : , i + <NUM_LIT:1> ] , <EOL> self . matvec ( zr [ : , i ] ) ) ) ) <EOL> d [ i + <NUM_LIT:1> ] = d [ i ] . conj ( ) <EOL> i += <NUM_LIT:1> <EOL> else : <EOL> nreturned -= <NUM_LIT:1> <EOL> i += <NUM_LIT:1> <EOL> if nreturned <= k : <EOL> d = d [ : nreturned ] <EOL> z = z [ : , : nreturned ] <EOL> else : <EOL> rd = np . round ( d , decimals = _ndigits [ self . tp ] ) <EOL> if self . which in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> ind = np . argsort ( rd . real ) <EOL> elif self . which in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> ind = np . argsort ( abs ( rd . imag ) ) <EOL> else : <EOL> ind = np . argsort ( abs ( rd ) ) <EOL> if self . which in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> d = d [ ind [ - k : ] ] <EOL> z = z [ : , ind [ - k : ] ] <EOL> if self . which in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> d = d [ ind [ : k ] ] <EOL> z = z [ : , ind [ : k ] ] <EOL> else : <EOL> d , z , ierr = self . _arpack_extract ( return_eigenvectors , <EOL> howmny , sselect , self . sigma , workev , <EOL> self . bmat , self . which , k , self . tol , self . resid , <EOL> self . v , self . iparam , self . ipntr , <EOL> self . workd , self . workl , self . rwork , ierr ) <EOL> if ierr != <NUM_LIT:0> : <EOL> raise ArpackError ( ierr , infodict = self . extract_infodict ) <EOL> k_ok = self . iparam [ <NUM_LIT:4> ] <EOL> d = d [ : k_ok ] <EOL> z = z [ : , : k_ok ] <EOL> if return_eigenvectors : <EOL> return d , z <EOL> else : <EOL> return d <EOL> def _aslinearoperator_with_dtype ( m ) : <EOL> m = aslinearoperator ( m ) <EOL> if not hasattr ( m , '<STR_LIT>' ) : <EOL> x = np . zeros ( m . shape [ <NUM_LIT:1> ] ) <EOL> m . dtype = ( m * x ) . dtype <EOL> return m <EOL> class SpLuInv ( LinearOperator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , M ) : <EOL> self . M_lu = splu ( M ) <EOL> self . shape = M . shape <EOL> self . dtype = M . dtype <EOL> self . isreal = not np . issubdtype ( self . dtype , np . complexfloating ) <EOL> def _matvec ( self , x ) : <EOL> x = np . asarray ( x ) <EOL> if self . isreal and np . issubdtype ( x . dtype , np . complexfloating ) : <EOL> return ( self . M_lu . solve ( np . real ( x ) . astype ( self . dtype ) ) + <EOL> <NUM_LIT> * self . M_lu . solve ( np . imag ( x ) . astype ( self . dtype ) ) ) <EOL> else : <EOL> return self . M_lu . solve ( x . astype ( self . dtype ) ) <EOL> class LuInv ( LinearOperator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , M ) : <EOL> self . M_lu = lu_factor ( M ) <EOL> self . shape = M . shape <EOL> self . dtype = M . dtype <EOL> def _matvec ( self , x ) : <EOL> return lu_solve ( self . M_lu , x ) <EOL> class IterInv ( LinearOperator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , M , ifunc = gmres , tol = <NUM_LIT:0> ) : <EOL> if tol <= <NUM_LIT:0> : <EOL> tol = <NUM_LIT:2> * np . finfo ( M . dtype ) . eps <EOL> self . M = M <EOL> self . ifunc = ifunc <EOL> self . tol = tol <EOL> if hasattr ( M , '<STR_LIT>' ) : <EOL> self . dtype = M . dtype <EOL> else : <EOL> x = np . zeros ( M . shape [ <NUM_LIT:1> ] ) <EOL> self . dtype = ( M * x ) . dtype <EOL> self . shape = M . shape <EOL> def _matvec ( self , x ) : <EOL> b , info = self . ifunc ( self . M , x , tol = self . tol ) <EOL> if info != <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( self . ifunc . __name__ , info ) ) <EOL> return b <EOL> class IterOpInv ( LinearOperator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , A , M , sigma , ifunc = gmres , tol = <NUM_LIT:0> ) : <EOL> if tol <= <NUM_LIT:0> : <EOL> tol = <NUM_LIT:2> * np . finfo ( A . dtype ) . eps <EOL> self . A = A <EOL> self . M = M <EOL> self . sigma = sigma <EOL> self . ifunc = ifunc <EOL> self . tol = tol <EOL> def mult_func ( x ) : <EOL> return A . matvec ( x ) - sigma * M . matvec ( x ) <EOL> def mult_func_M_None ( x ) : <EOL> return A . matvec ( x ) - sigma * x <EOL> x = np . zeros ( A . shape [ <NUM_LIT:1> ] ) <EOL> if M is None : <EOL> dtype = mult_func_M_None ( x ) . dtype <EOL> self . OP = LinearOperator ( self . A . shape , <EOL> mult_func_M_None , <EOL> dtype = dtype ) <EOL> else : <EOL> dtype = mult_func ( x ) . dtype <EOL> self . OP = LinearOperator ( self . A . shape , <EOL> mult_func , <EOL> dtype = dtype ) <EOL> self . shape = A . shape <EOL> def _matvec ( self , x ) : <EOL> b , info = self . ifunc ( self . OP , x , tol = self . tol ) <EOL> if info != <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( self . ifunc . __name__ , info ) ) <EOL> return b <EOL> @ property <EOL> def dtype ( self ) : <EOL> return self . OP . dtype <EOL> def get_inv_matvec ( M , symmetric = False , tol = <NUM_LIT:0> ) : <EOL> if isdense ( M ) : <EOL> return LuInv ( M ) . matvec <EOL> elif isspmatrix ( M ) : <EOL> if isspmatrix_csr ( M ) and symmetric : <EOL> M = M . T <EOL> return SpLuInv ( M ) . matvec <EOL> else : <EOL> return IterInv ( M , tol = tol ) . matvec <EOL> def get_OPinv_matvec ( A , M , sigma , symmetric = False , tol = <NUM_LIT:0> ) : <EOL> if sigma == <NUM_LIT:0> : <EOL> return get_inv_matvec ( A , symmetric = symmetric , tol = tol ) <EOL> if M is None : <EOL> if isdense ( A ) : <EOL> if ( np . issubdtype ( A . dtype , np . complexfloating ) or <EOL> np . imag ( sigma ) == <NUM_LIT:0> ) : <EOL> A = np . copy ( A ) <EOL> else : <EOL> A = A + <NUM_LIT> <EOL> A . flat [ : : A . shape [ <NUM_LIT:1> ] + <NUM_LIT:1> ] -= sigma <EOL> return LuInv ( A ) . matvec <EOL> elif isspmatrix ( A ) : <EOL> A = A - sigma * identity ( A . shape [ <NUM_LIT:0> ] ) <EOL> if symmetric and isspmatrix_csr ( A ) : <EOL> A = A . T <EOL> return SpLuInv ( A . tocsc ( ) ) . matvec <EOL> else : <EOL> return IterOpInv ( _aslinearoperator_with_dtype ( A ) , <EOL> M , sigma , tol = tol ) . matvec <EOL> else : <EOL> if ( ( not isdense ( A ) and not isspmatrix ( A ) ) or <EOL> ( not isdense ( M ) and not isspmatrix ( M ) ) ) : <EOL> return IterOpInv ( _aslinearoperator_with_dtype ( A ) , <EOL> _aslinearoperator_with_dtype ( M ) , <EOL> sigma , tol = tol ) . matvec <EOL> elif isdense ( A ) or isdense ( M ) : <EOL> return LuInv ( A - sigma * M ) . matvec <EOL> else : <EOL> OP = A - sigma * M <EOL> if symmetric and isspmatrix_csr ( OP ) : <EOL> OP = OP . T <EOL> return SpLuInv ( OP . tocsc ( ) ) . matvec <EOL> def _eigs ( A , k = <NUM_LIT:6> , M = None , sigma = None , which = '<STR_LIT>' , v0 = None , <EOL> ncv = None , maxiter = None , tol = <NUM_LIT:0> , return_eigenvectors = True , <EOL> Minv = None , OPinv = None , OPpart = None ) : <EOL> """<STR_LIT>""" <EOL> if A . shape [ <NUM_LIT:0> ] != A . shape [ <NUM_LIT:1> ] : <EOL> raise ValueError ( '<STR_LIT>' % ( A . shape , ) ) <EOL> if M is not None : <EOL> if M . shape != A . shape : <EOL> raise ValueError ( '<STR_LIT>' <EOL> % ( M . shape , A . shape ) ) <EOL> if np . dtype ( M . dtype ) . char . lower ( ) != np . dtype ( A . dtype ) . char . lower ( ) : <EOL> import warnings <EOL> warnings . warn ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> n = A . shape [ <NUM_LIT:0> ] <EOL> if k <= <NUM_LIT:0> or k >= n : <EOL> raise ValueError ( "<STR_LIT>" <EOL> % ( k , n - <NUM_LIT:1> ) ) <EOL> if sigma is None : <EOL> matvec = _aslinearoperator_with_dtype ( A ) . matvec <EOL> if OPinv is not None : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if OPpart is not None : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if M is None : <EOL> mode = <NUM_LIT:1> <EOL> M_matvec = None <EOL> Minv_matvec = None <EOL> if Minv is not None : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> mode = <NUM_LIT:2> <EOL> if Minv is None : <EOL> Minv_matvec = get_inv_matvec ( M , symmetric = True , tol = tol ) <EOL> else : <EOL> Minv = _aslinearoperator_with_dtype ( Minv ) <EOL> Minv_matvec = Minv . matvec <EOL> M_matvec = _aslinearoperator_with_dtype ( M ) . matvec <EOL> else : <EOL> if np . issubdtype ( A . dtype , np . complexfloating ) : <EOL> if OPpart is not None : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> mode = <NUM_LIT:3> <EOL> elif OPpart is None or OPpart . lower ( ) == '<STR_LIT:r>' : <EOL> mode = <NUM_LIT:3> <EOL> elif OPpart . lower ( ) == '<STR_LIT:i>' : <EOL> if np . imag ( sigma ) == <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> mode = <NUM_LIT:4> <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> matvec = _aslinearoperator_with_dtype ( A ) . matvec <EOL> if Minv is not None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if OPinv is None : <EOL> Minv_matvec = get_OPinv_matvec ( A , M , sigma , <EOL> symmetric = False , tol = tol ) <EOL> else : <EOL> OPinv = _aslinearoperator_with_dtype ( OPinv ) <EOL> Minv_matvec = OPinv . matvec <EOL> if M is None : <EOL> M_matvec = None <EOL> else : <EOL> M_matvec = _aslinearoperator_with_dtype ( M ) . matvec <EOL> params = _UnsymmetricArpackParams ( n , k , A . dtype . char , matvec , mode , <EOL> M_matvec , Minv_matvec , sigma , <EOL> ncv , v0 , maxiter , which , tol ) <EOL> while not params . converged : <EOL> params . iterate ( ) <EOL> return params . extract ( return_eigenvectors ) <EOL> def _eigsh ( A , k = <NUM_LIT:6> , M = None , sigma = None , which = '<STR_LIT>' , v0 = None , <EOL> ncv = None , maxiter = None , tol = <NUM_LIT:0> , return_eigenvectors = True , <EOL> Minv = None , OPinv = None , mode = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if np . issubdtype ( A . dtype , np . complexfloating ) : <EOL> if mode != '<STR_LIT>' : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" % mode ) <EOL> if which == '<STR_LIT>' : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> elif which == '<STR_LIT>' : <EOL> which = '<STR_LIT>' <EOL> elif which == '<STR_LIT>' : <EOL> which = '<STR_LIT>' <EOL> ret = eigs ( A , k , M = M , sigma = sigma , which = which , v0 = v0 , <EOL> ncv = ncv , maxiter = maxiter , tol = tol , <EOL> return_eigenvectors = return_eigenvectors , Minv = Minv , <EOL> OPinv = OPinv ) <EOL> if return_eigenvectors : <EOL> return ret [ <NUM_LIT:0> ] . real , ret [ <NUM_LIT:1> ] <EOL> else : <EOL> return ret . real <EOL> if A . shape [ <NUM_LIT:0> ] != A . shape [ <NUM_LIT:1> ] : <EOL> raise ValueError ( '<STR_LIT>' % ( A . shape , ) ) <EOL> if M is not None : <EOL> if M . shape != A . shape : <EOL> raise ValueError ( '<STR_LIT>' <EOL> % ( M . shape , A . shape ) ) <EOL> if np . dtype ( M . dtype ) . char . lower ( ) != np . dtype ( A . dtype ) . char . lower ( ) : <EOL> import warnings <EOL> warnings . warn ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> n = A . shape [ <NUM_LIT:0> ] <EOL> if k <= <NUM_LIT:0> or k >= n : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if sigma is None : <EOL> A = _aslinearoperator_with_dtype ( A ) <EOL> matvec = A . matvec <EOL> if OPinv is not None : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if M is None : <EOL> mode = <NUM_LIT:1> <EOL> M_matvec = None <EOL> Minv_matvec = None <EOL> if Minv is not None : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> mode = <NUM_LIT:2> <EOL> if Minv is None : <EOL> Minv_matvec = get_inv_matvec ( M , symmetric = True , tol = tol ) <EOL> else : <EOL> Minv = _aslinearoperator_with_dtype ( Minv ) <EOL> Minv_matvec = Minv . matvec <EOL> M_matvec = _aslinearoperator_with_dtype ( M ) . matvec <EOL> else : <EOL> if Minv is not None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if mode == '<STR_LIT>' : <EOL> mode = <NUM_LIT:3> <EOL> matvec = None <EOL> if OPinv is None : <EOL> Minv_matvec = get_OPinv_matvec ( A , M , sigma , <EOL> symmetric = True , tol = tol ) <EOL> else : <EOL> OPinv = _aslinearoperator_with_dtype ( OPinv ) <EOL> Minv_matvec = OPinv . matvec <EOL> if M is None : <EOL> M_matvec = None <EOL> else : <EOL> M = _aslinearoperator_with_dtype ( M ) <EOL> M_matvec = M . matvec <EOL> elif mode == '<STR_LIT>' : <EOL> mode = <NUM_LIT:4> <EOL> if OPinv is None : <EOL> Minv_matvec = get_OPinv_matvec ( A , M , sigma , <EOL> symmetric = True , tol = tol ) <EOL> else : <EOL> Minv_matvec = _aslinearoperator_with_dtype ( OPinv ) . matvec <EOL> matvec = _aslinearoperator_with_dtype ( A ) . matvec <EOL> M_matvec = None <EOL> elif mode == '<STR_LIT>' : <EOL> mode = <NUM_LIT:5> <EOL> matvec = _aslinearoperator_with_dtype ( A ) . matvec <EOL> if OPinv is None : <EOL> Minv_matvec = get_OPinv_matvec ( A , M , sigma , <EOL> symmetric = True , tol = tol ) <EOL> else : <EOL> Minv_matvec = _aslinearoperator_with_dtype ( OPinv ) . matvec <EOL> if M is None : <EOL> M_matvec = None <EOL> else : <EOL> M_matvec = _aslinearoperator_with_dtype ( M ) . matvec <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % mode ) <EOL> params = _SymmetricArpackParams ( n , k , A . dtype . char , matvec , mode , <EOL> M_matvec , Minv_matvec , sigma , <EOL> ncv , v0 , maxiter , which , tol ) <EOL> while not params . converged : <EOL> params . iterate ( ) <EOL> return params . extract ( return_eigenvectors ) <EOL> def _augmented_orthonormal_cols ( x , k ) : <EOL> n , m = x . shape <EOL> y = np . empty ( ( n , m + k ) , dtype = x . dtype ) <EOL> y [ : , : m ] = x <EOL> for i in range ( k ) : <EOL> v = np . random . randn ( n ) <EOL> if np . iscomplexobj ( x ) : <EOL> v = v + <NUM_LIT> * np . random . randn ( n ) <EOL> for j in range ( m + i ) : <EOL> u = y [ : , j ] <EOL> v -= ( np . dot ( v , u . conj ( ) ) / np . dot ( u , u . conj ( ) ) ) * u <EOL> v /= np . sqrt ( np . dot ( v , v . conj ( ) ) ) <EOL> y [ : , m + i ] = v <EOL> return y <EOL> def _augmented_orthonormal_rows ( x , k ) : <EOL> return _augmented_orthonormal_cols ( x . T , k ) . T <EOL> def _herm ( x ) : <EOL> return x . T . conj ( ) <EOL> def _svds ( A , k = <NUM_LIT:6> , ncv = None , tol = <NUM_LIT:0> , which = '<STR_LIT>' , v0 = None , <EOL> maxiter = None , return_singular_vectors = True ) : <EOL> """<STR_LIT>""" <EOL> if not ( isinstance ( A , LinearOperator ) or isspmatrix ( A ) ) : <EOL> A = np . asarray ( A ) <EOL> n , m = A . shape <EOL> if isinstance ( A , LinearOperator ) : <EOL> if n > m : <EOL> X_dot = A . matvec <EOL> X_matmat = A . matmat <EOL> XH_dot = A . rmatvec <EOL> else : <EOL> X_dot = A . rmatvec <EOL> XH_dot = A . matvec <EOL> dtype = getattr ( A , '<STR_LIT>' , None ) <EOL> if dtype is None : <EOL> dtype = A . dot ( np . zeros ( [ m , <NUM_LIT:1> ] ) ) . dtype <EOL> def X_matmat ( V ) : <EOL> out = np . empty ( ( V . shape [ <NUM_LIT:1> ] , m ) , dtype = dtype ) <EOL> for i , col in enumerate ( V . T ) : <EOL> out [ i , : ] = A . rmatvec ( col . reshape ( - <NUM_LIT:1> , <NUM_LIT:1> ) ) . T <EOL> return out . T <EOL> else : <EOL> if n > m : <EOL> X_dot = X_matmat = A . dot <EOL> XH_dot = _herm ( A ) . dot <EOL> else : <EOL> XH_dot = A . dot <EOL> X_dot = X_matmat = _herm ( A ) . dot <EOL> def matvec_XH_X ( x ) : <EOL> return XH_dot ( X_dot ( x ) ) <EOL> XH_X = LinearOperator ( matvec = matvec_XH_X , dtype = A . dtype , <EOL> shape = ( min ( A . shape ) , min ( A . shape ) ) ) <EOL> eigvals , eigvec = eigsh ( XH_X , k = k , tol = tol ** <NUM_LIT:2> , maxiter = maxiter , <EOL> ncv = ncv , which = which , v0 = v0 ) <EOL> if which == '<STR_LIT>' : <EOL> eigvals = np . maximum ( eigvals . real , <NUM_LIT:0> ) <EOL> t = eigvec . dtype . char . lower ( ) <EOL> factor = { '<STR_LIT:f>' : <NUM_LIT> , '<STR_LIT:d>' : <NUM_LIT> } <EOL> cond = factor [ t ] * np . finfo ( t ) . eps <EOL> cutoff = cond * np . max ( eigvals ) <EOL> above_cutoff = ( eigvals > cutoff ) <EOL> nlarge = above_cutoff . sum ( ) <EOL> nsmall = k - nlarge <EOL> slarge = np . sqrt ( eigvals [ above_cutoff ] ) <EOL> s = np . zeros_like ( eigvals ) <EOL> s [ : nlarge ] = slarge <EOL> if not return_singular_vectors : <EOL> return s <EOL> if n > m : <EOL> vlarge = eigvec [ : , above_cutoff ] <EOL> ularge = X_matmat ( vlarge ) / slarge if return_singular_vectors != '<STR_LIT>' else None <EOL> vhlarge = _herm ( vlarge ) <EOL> else : <EOL> ularge = eigvec [ : , above_cutoff ] <EOL> vhlarge = _herm ( X_matmat ( ularge ) / slarge ) if return_singular_vectors != '<STR_LIT:u>' else None <EOL> u = _augmented_orthonormal_cols ( ularge , nsmall ) if ularge is not None else None <EOL> vh = _augmented_orthonormal_rows ( vhlarge , nsmall ) if vhlarge is not None else None <EOL> elif which == '<STR_LIT>' : <EOL> s = np . sqrt ( eigvals ) <EOL> if not return_singular_vectors : <EOL> return s <EOL> if n > m : <EOL> v = eigvec <EOL> u = X_matmat ( v ) / s if return_singular_vectors != '<STR_LIT>' else None <EOL> vh = _herm ( v ) <EOL> else : <EOL> u = eigvec <EOL> vh = _herm ( X_matmat ( u ) / s ) if return_singular_vectors != '<STR_LIT:u>' else None <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return u , s , vh <EOL> if scipy . version . version >= LooseVersion ( '<STR_LIT>' ) : <EOL> from scipy . sparse . linalg import eigs , eigsh , svds <EOL> else : <EOL> eigs , eigsh , svds = _eigs , _eigsh , _svds </s>
