Created
February 20, 2014 06:16
-
-
Save preetum/9107973 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from numpy import linalg | |
def null(A, eps=1e-15): | |
""" Returns the null-space of matrix A (as column-vectors) """ | |
u, s, vT = np.linalg.svd(A, full_matrices=1, compute_uv=1) | |
r = sum(s > eps) | |
null_space = vT[r:,:] | |
return null_space.T | |
def lcon_lsqr(A, b, C, d): | |
""" Returns the solution of: | |
argmin_x ||Ax - b||_2 | |
subject to Cx = d | |
""" | |
x0 = np.linalg.lstsq(C, d)[0] | |
N = null(C) | |
z = np.linalg.lstsq(A.dot(N), b - A.dot(x0))[0] | |
x = x0 + N.dot(z) | |
return x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment