import _nnls from numpy import asarray_chkfinite, zeros, double, require def nnls(A,b): """ Solve || Ax - b ||_2 -> min with x>=0 Inputs: A -- matrix as above b -- vector as above Outputs: x -- solution vector rnorm -- residual || Ax-b ||_2 """ A,b = map(asarray_chkfinite, (A,b)) if A.ndim != 2: raise ValueError, "expected matrix" if b.ndim != 1: raise ValueError, "expected vector" m,n = A.shape if m != b.shape[0]: raise ValueError, "incompatible dimensions" A = require(A, double, [ "F" ]) b = require(b, double) x,rnorm,mode = _nnls.nnls(A,b) if mode != 1: raise RuntimeError, "too many iterations" return x, rnorm