Qu

nnls

Builtin index · Core maths

result = nnls(A, b, max_iter=)

Input: A, b, max_iter · Output: result

What it is

Non-negative least squares by Lawson-Hanson, for when a negative coefficient would be physically meaningless — a concentration, a mass, a relaxation strength. A is an m×n Mat, b a length-m Vec; max_iter is an optional scalar iteration cap (unset by default, meaning the solver's own internal limit applies).

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = nnls(A, b, max_iter=)
A Mat or VecA is an m×n Mat, b a length-m Vec.
b Mat or VecA is an m×n Mat, b a length-m Vec.
max_iter scalarmax_iter is an optional scalar iteration cap (unset by default, meaning the solver's own internal limit applies).
x Veclength-n Vec, every entry >= 0
residual scalar or Numa scalar Num, ‖A·x - b‖
Returns a Record with fields x (length-n Vec, every entry >= 0) and residual (a scalar Num, ‖A·x - b‖).

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

A = [4.0, 1.0; 1.0, 3.0]
print("diag={diag(A)}  trace={trace(A)}  cond={cond(A)}")

b = [1.0, 2.0]
r = nnls(A, b)
print("nnls x={r.x}  residual={r.residual}")
output
diag=[4, 3]  trace=7  cond=1.938749
nnls x=[0.090909, 0.636364]  residual=4.965068e-16

from core-math