This package implements the non-negative least squares solver from Lawson and Hanson [1]. Given a matrix A and vector b, nnls(A, b)
computes:
min. || Ax - b ||_2
x
s.t. x[i] >= 0 forall i
The code contained here is a direct port of the original Fortran code to Julia.
A = randn(100, 200)
b = randn(100)
x = nnls(A, b)
The NNLS implementation (and the Fortran code on which it is based) have been implemented to allocate as little memory as possible. If you want direct control over the memory usage, you can pre-allocate an NNLSWorkspace
which will hold all data used in the NNLS algorithm:
A = randn(100, 200)
b = randn(100)
work = NNLSWorkspace(A, b)
nnls!(work)
@show work.x
The call to nnls!(work)
should allocate no memory. You can re-use the same workspace multiple times:
A2 = randn(100, 200)
b2 = randn(100)
load!(work, A2, b2)
nnls!(work)
@show work.x
If A2
and b2
match the size of the arrays A
and b
used to create the workspace, then load!(work, A2, b2)
will not allocate. If they do not match, then the workspace will be resized and some memory will be allocated.
[1] Lawson, C.L. and R.J. Hanson, Solving Least-Squares Problems, Prentice-Hall, Chapter 23, p. 161, 1974