#NEWLINE# def lstsq( a , b , cond = None , overwrite_a = False , overwrite_b = False , check_finite = True , lapack_driver = None ) : #NEWLINE# #INDENT# "\n    Compute least-squares solution to equation Ax = b.\n\n    Compute a vector x such that the 2-norm ``|b - A x|`` is minimized.\n\n    Parameters\n    ----------\n    a : (M, N) array_like\n        Left hand side matrix (2-D array).\n    b : (M,) or (M, K) array_like\n        Right hand side matrix or vector (1-D or 2-D array).\n    cond : float, optional\n        Cutoff for 'small' singular values; used to determine effective\n        rank of a. Singular values smaller than\n        ``rcond * largest_singular_value`` are considered zero.\n    overwrite_a : bool, optional\n        Discard data in `a` (may enhance performance). Default is False.\n    overwrite_b : bool, optional\n        Discard data in `b` (may enhance performance). Default is False.\n    check_finite : bool, optional\n        Whether to check that the input matrices contain only finite numbers.\n        Disabling may give a performance gain, but may result in problems\n        (crashes, non-termination) if the inputs do contain infinities or NaNs.\n    lapack_driver: str, optional\n        Which LAPACK driver is used to solve the least-squares problem.\n        Options are ``'gelsd'``, ``'gelsy'``, ``'gelss'``. Default\n        (``'gelsd'``) is a good choice.  However, ``'gelsy'`` can be slightly\n        faster on many problems.  ``'gelss'`` was used historically.  It is\n        generally slow but uses less memory.\n\n        .. versionadded:: 0.17.0\n\n    Returns\n    -------\n    x : (N,) or (N, K) ndarray\n        Least-squares solution.  Return shape matches shape of `b`.\n    residues : () or (1,) or (K,) ndarray\n        Sums of residues, squared 2-norm for each column in ``b - a x``.\n        If rank of matrix a is ``< N`` or ``> M``, or ``'gelsy'`` is used,\n        this is an empty array. If b was 1-D, this is an (1,) shape array,\n        otherwise the shape is (K,).\n    rank : int\n        Effective rank of matrix `a`.\n    s : (min(M,N),) ndarray or None\n        Singular values of `a`. The condition number of a is\n        ``abs(s[0] / s[-1])``. None is returned when ``'gelsy'`` is used.\n\n    Raises\n    ------\n    LinAlgError :\n        If computation does not converge.\n\n    ValueError :\n        When parameters are wrong.\n\n    See Also\n    --------\n    optimize.nnls : linear least squares with non-negativity constraint\n\n    " #NEWLINE# a1 = _asarray_validated ( a , check_finite = check_finite ) #NEWLINE# b1 = _asarray_validated ( b , check_finite = check_finite ) #NEWLINE# if ( len ( a1 . shape ) != 2 ) : #NEWLINE# #INDENT# raise ValueError ( 'expected matrix' ) #NEWLINE# #UNINDENT# ( m , n ) = a1 . shape #NEWLINE# if ( len ( b1 . shape ) == 2 ) : #NEWLINE# #INDENT# nrhs = b1 . shape [ 1 ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# nrhs = 1 #NEWLINE# #UNINDENT# if ( m != b1 . shape [ 0 ] ) : #NEWLINE# #INDENT# raise ValueError ( 'incompatible dimensions' ) #NEWLINE# #UNINDENT# driver = lapack_driver #NEWLINE# if ( driver is None ) : #NEWLINE# #INDENT# driver = lstsq . default_lapack_driver #NEWLINE# #UNINDENT# if ( driver not in ( 'gelsd' , 'gelsy' , 'gelss' ) ) : #NEWLINE# #INDENT# raise ValueError ( ( 'LAPACK driver "%s" is not found' % driver ) ) #NEWLINE# #UNINDENT# ( lapack_func , lapack_lwork ) = get_lapack_funcs ( ( driver , ( '%s_lwork' % driver ) ) , ( a1 , b1 ) ) #NEWLINE# real_data = ( True if ( lapack_func . dtype . kind == 'f' ) else False ) #NEWLINE# if ( m < n ) : #NEWLINE# #INDENT# if ( len ( b1 . shape ) == 2 ) : #NEWLINE# #INDENT# b2 = np . zeros ( ( n , nrhs ) , dtype = lapack_func . dtype ) #NEWLINE# b2 [ : m , : ] = b1 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# b2 = np . zeros ( n , dtype = lapack_func . dtype ) #NEWLINE# b2 [ : m ] = b1 #NEWLINE# #UNINDENT# b1 = b2 #NEWLINE# #UNINDENT# overwrite_a = ( overwrite_a or _datacopied ( a1 , a ) ) #NEWLINE# overwrite_b = ( overwrite_b or _datacopied ( b1 , b ) ) #NEWLINE# if ( cond is None ) : #NEWLINE# #INDENT# cond = np . finfo ( lapack_func . dtype ) . eps #NEWLINE# #UNINDENT# if ( driver in ( 'gelss' , 'gelsd' ) ) : #NEWLINE# #INDENT# if ( driver == 'gelss' ) : #NEWLINE# #INDENT# lwork = _compute_lwork ( lapack_lwork , m , n , nrhs , cond ) #NEWLINE# ( v , x , s , rank , work , info ) = lapack_func ( a1 , b1 , cond , lwork , overwrite_a = overwrite_a , overwrite_b = overwrite_b ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( driver == 'gelsd' ) : #NEWLINE# #INDENT# if real_data : #NEWLINE# #INDENT# ( lwork , iwork ) = _compute_lwork ( lapack_lwork , m , n , nrhs , cond ) #NEWLINE# if ( iwork == 0 ) : #NEWLINE# #INDENT# mesg = 'internal gelsd driver lwork query error, required iwork dimension not returned. This is likely the result of LAPACK bug 0038, fixed in LAPACK 3.2.2 (released July 21, 2010). ' #NEWLINE# if ( lapack_driver is None ) : #NEWLINE# #INDENT# lstsq . default_lapack_driver = 'gelss' #NEWLINE# mesg += "Falling back to 'gelss' driver." #NEWLINE# warnings . warn ( mesg , RuntimeWarning ) #NEWLINE# return lstsq ( a , b , cond , overwrite_a , overwrite_b , check_finite , lapack_driver = 'gelss' ) #NEWLINE# #UNINDENT# mesg += 'Use a different lapack_driver when calling lstsq or upgrade LAPACK.' #NEWLINE# raise LstsqLapackError ( mesg ) #NEWLINE# #UNINDENT# ( x , s , rank , info ) = lapack_func ( a1 , b1 , lwork , iwork , cond , False , False ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# ( lwork , rwork , iwork ) = _compute_lwork ( lapack_lwork , m , n , nrhs , cond ) #NEWLINE# ( x , s , rank , info ) = lapack_func ( a1 , b1 , lwork , rwork , iwork , cond , False , False ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( info > 0 ) : #NEWLINE# #INDENT# raise LinAlgError ( 'SVD did not converge in Linear Least Squares' ) #NEWLINE# #UNINDENT# if ( info < 0 ) : #NEWLINE# #INDENT# raise ValueError ( ( 'illegal value in %d-th argument of internal %s' % ( ( - info ) , lapack_driver ) ) ) #NEWLINE# #UNINDENT# resids = np . asarray ( [ ] , dtype = x . dtype ) #NEWLINE# if ( m > n ) : #NEWLINE# #INDENT# x1 = lapack_driver [ : n ] #NEWLINE# if ( rank == n ) : #NEWLINE# #INDENT# resids = np . sum ( ( np . abs ( x [ n : ] ) ** 2 ) , axis = 0 ) #NEWLINE# #UNINDENT# x = x1 #NEWLINE# #UNINDENT# return ( x , resids , rank , s ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( driver == 'gelsy' ) : #NEWLINE# #INDENT# lwork = _compute_lwork ( lapack_lwork , m , n , nrhs , cond ) #NEWLINE# jptv = np . zeros ( ( a1 . shape [ 1 ] , 1 ) , dtype = np . int32 ) #NEWLINE# ( v , x , j , rank , info ) = lapack_func ( a1 , b1 , jptv , cond , lwork , False , False ) #NEWLINE# if ( info < 0 ) : #NEWLINE# #INDENT# raise ValueError ( ( 'illegal value in %d-th argument of internal gelsy' % ( - info ) ) ) #NEWLINE# #UNINDENT# if ( m > n ) : #NEWLINE# #INDENT# x1 = x [ : n ] #NEWLINE# x = x1 #NEWLINE# #UNINDENT# return ( x , np . array ( [ ] , x . dtype ) , rank , None )