Skip to content

Instantly share code, notes, and snippets.

@preetum
Created February 20, 2014 06:16
Show Gist options
  • Save preetum/9107973 to your computer and use it in GitHub Desktop.
Save preetum/9107973 to your computer and use it in GitHub Desktop.
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