#NEWLINE# def regularized_lsq_with_qr( m , n , R , QTb , perm , diag , copy_R = True ) : #NEWLINE# #INDENT# 'Solve regularized least squares using information from QR-decomposition.\n\n    The initial problem is to solve the following system in a least-squares\n    sense:\n    ::\n\n        A x = b\n        D x = 0\n\n    Where D is diagonal matrix. The method is based on QR decomposition\n    of the form A P = Q R, where P is a column permutation matrix, Q is an\n    orthogonal matrix and R is an upper triangular matrix.\n\n    Parameters\n    ----------\n    m, n : int\n        Initial shape of A.\n    R : ndarray, shape (n, n)\n        Upper triangular matrix from QR decomposition of A.\n    QTb : ndarray, shape (n,)\n        First n components of Q^T b.\n    perm : ndarray, shape (n,)\n        Array defining column permutation of A, such that i-th column of\n        P is perm[i]-th column of identity matrix.\n    diag : ndarray, shape (n,)\n        Array containing diagonal elements of D.\n\n    Returns\n    -------\n    x : ndarray, shape (n,)\n        Found least-squares solution.\n    ' #NEWLINE# if copy_R : #NEWLINE# #INDENT# R = R . copy ( ) #NEWLINE# #UNINDENT# v = QTb . copy ( ) #NEWLINE# givens_elimination ( R , v , diag [ perm ] ) #NEWLINE# abs_diag_R = np . abs ( np . diag ( R ) ) #NEWLINE# threshold = ( ( EPS * max ( m , n ) ) * np . max ( abs_diag_R ) ) #NEWLINE# ( nns , ) = np . nonzero ( ( abs_diag_R > threshold ) ) #NEWLINE# R = R [ np . ix_ ( nns , nns ) ] #NEWLINE# v = v [ nns ] #NEWLINE# x = np . zeros ( n ) #NEWLINE# x [ perm [ nns ] ] = solve_triangular ( R , v ) #NEWLINE# return x