def lstsq(a, b, cond=None, overwrite_a=False, overwrite_b=False,<EOL>check_finite=True):
if check_finite:<EOL><INDENT>a1,b1 = map(np.asarray_chkfinite, (a,b))<EOL><DEDENT>else:<EOL><INDENT>a1,b1 = map(np.asarray, (a,b))<EOL><DEDENT>if len(a1.shape) != <NUM_LIT:2>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>m, n = a1.shape<EOL>if len(b1.shape) == <NUM_LIT:2>:<EOL><INDENT>nrhs = b1.shape[<NUM_LIT:1>]<EOL><DEDENT>else:<EOL><INDENT>nrhs = <NUM_LIT:1><EOL><DEDENT>if m != b1.shape[<NUM_LIT:0>]:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>gelss, = get_lapack_funcs(('<STR_LIT>',), (a1, b1))<EOL>if n > m:<EOL><INDENT>if len(b1.shape) == <NUM_LIT:2>:<EOL><INDENT>b2 = np.zeros((n, nrhs), dtype=gelss.dtype)<EOL>b2[:m,:] = b1<EOL><DEDENT>else:<EOL><INDENT>b2 = np.zeros(n, dtype=gelss.dtype)<EOL>b2[:m] = b1<EOL><DEDENT>b1 = b2<EOL><DEDENT>overwrite_a = overwrite_a or _datacopied(a1, a)<EOL>overwrite_b = overwrite_b or _datacopied(b1, b)<EOL>work = gelss(a1, b1, lwork=-<NUM_LIT:1>)[<NUM_LIT:4>]<EOL>lwork = work[<NUM_LIT:0>].real.astype(np.int)<EOL>v, x, s, rank, work, info = gelss(<EOL>a1, b1, cond=cond, lwork=lwork, overwrite_a=overwrite_a,<EOL>overwrite_b=overwrite_b)<EOL>if info > <NUM_LIT:0>:<EOL><INDENT>raise LinAlgError("<STR_LIT>")<EOL><DEDENT>if info < <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>% -info)<EOL><DEDENT>resids = np.asarray([], dtype=x.dtype)<EOL>if n < m:<EOL><INDENT>x1 = x[:n]<EOL>if rank == n:<EOL><INDENT>resids = np.sum(np.abs(x[n:])**<NUM_LIT:2>, axis=<NUM_LIT:0>)<EOL><DEDENT>x = x1<EOL><DEDENT>return x, resids, rank, s<EOL>
Compute least-squares solution to equation Ax = b.

Compute a vector x such that the 2-norm ``|b - A x|`` is minimized.

Parameters
----------
a : (M, N) array_like
    Left hand side matrix (2-D array).
b : (M,) or (M, K) array_like
    Right hand side matrix or vector (1-D or 2-D array).
cond : float, optional
    Cutoff for 'small' singular values; used to determine effective
    rank of a. Singular values smaller than
    ``rcond * largest_singular_value`` are considered zero.
overwrite_a : bool, optional
    Discard data in `a` (may enhance performance). Default is False.
overwrite_b : bool, optional
    Discard data in `b` (may enhance performance). Default is False.
check_finite : boolean, optional
    Whether to check that the input matrices contain only finite numbers.
    Disabling may give a performance gain, but may result in problems
    (crashes, non-termination) if the inputs do contain infinities or NaNs.

Returns
-------
x : (N,) or (N, K) ndarray
    Least-squares solution.  Return shape matches shape of `b`.
residues : () or (1,) or (K,) ndarray
    Sums of residues, squared 2-norm for each column in ``b - a x``.
    If rank of matrix a is < N or > M this is an empty array.
    If b was 1-D, this is an (1,) shape array, otherwise the shape is (K,).
rank : int
    Effective rank of matrix `a`.
s : (min(M,N),) ndarray
    Singular values of `a`. The condition number of a is
    ``abs(s[0]/s[-1])``.

Raises
------
LinAlgError :
    If computation does not converge.


See Also
--------
optimize.nnls : linear least squares with non-negativity constraint