OpenMS
Loading...
Searching...
No Matches
NonNegativeLeastSquaresSolver Class Reference

Wrapper around the Lawson-Hanson non-negative least squares (NNLS) Fortran routine. More...

#include <OpenMS/ML/NNLS/NonNegativeLeastSquaresSolver.h>

Public Types

enum  RETURN_STATUS { SOLVED , ITERATION_EXCEEDED }
 Return status of solve. More...
 

Static Public Member Functions

static Int solve (const Matrix< double > &A, const Matrix< double > &b, Matrix< double > &x)
 Solve argmin_{x >= 0} ||Ax - b||_2 without modifying the inputs.
 
static Int solve (double *A, int A_rows, int A_cols, std::vector< double > &b, std::vector< double > &x)
 In-place pointer overload of solve.
 
static Int solve (Matrix< double > &A, std::vector< double > &b, std::vector< double > &x)
 In-place Matrix / std::vector overload of solve.
 

Detailed Description

Wrapper around the Lawson-Hanson non-negative least squares (NNLS) Fortran routine.

Solves the constrained least-squares problem argmin_{x >= 0} ||Ax - b||_2 with all entries of x required to be non-negative.

Three overloads are provided:

  • a non-destructive Matrix-based one that copies its inputs;
  • an in-place pointer-based one for callers that already own a column-major buffer;
  • an in-place Matrix / std::vector convenience wrapper around the pointer overload.

Member Enumeration Documentation

◆ RETURN_STATUS

Return status of solve.

Enumerator
SOLVED 

NNLS converged within the iteration limit; x holds the solution.

ITERATION_EXCEEDED 

NNLS hit the iteration limit; x holds the best result so far.

Member Function Documentation

◆ solve() [1/3]

static Int solve ( const Matrix< double > &  A,
const Matrix< double > &  b,
Matrix< double > &  x 
)
static

Solve argmin_{x >= 0} ||Ax - b||_2 without modifying the inputs.

Copies A and b internally, then runs NNLS on the copies and writes the result into x (resized to A.cols() rows, 1 column).

Parameters
[in]AInput matrix of size m x n.
[in]bInput vector as a column matrix of size m x 1.
[out]xReceives the non-negative solution as a column matrix of size n x 1.
Returns
SOLVED on convergence, ITERATION_EXCEEDED when the iteration limit was reached.
Exceptions
Exception::InvalidParameterwhen A.rows() does not match b.rows(), or when the underlying NNLS routine reports an invalid dimension (should not happen in practice).

◆ solve() [2/3]

static Int solve ( double *  A,
int  A_rows,
int  A_cols,
std::vector< double > &  b,
std::vector< double > &  x 
)
static

In-place pointer overload of solve.

Operates directly on the caller-owned buffer for A and the b vector, so neither needs to be copied. After the call the contents of A and b are clobbered by the NNLS workspace and should not be read again.

Parameters
[in,out]APointer to a column-major double buffer of length A_rows * A_cols. The caller retains ownership, must keep the buffer valid for the duration of the call, and must not pass nullptr or a buffer that is shorter than required. The contents are unspecified on return.
[in]A_rowsNumber of rows of A (> 0).
[in]A_colsNumber of columns of A (> 0).
[in,out]bRight-hand side of size A_rows; contents are unspecified on return.
[out]xReceives the non-negative solution of size A_cols (resized internally).
Returns
SOLVED on convergence, ITERATION_EXCEEDED when the iteration limit was reached.
Exceptions
Exception::InvalidParameterwhen A_rows does not match b.size(), or when the underlying NNLS routine reports an invalid dimension.
Note
Prefer the Matrix overload for additional bounds checking; a future revision may adopt std::span.

◆ solve() [3/3]

static Int solve ( Matrix< double > &  A,
std::vector< double > &  b,
std::vector< double > &  x 
)
static

In-place Matrix / std::vector overload of solve.

Delegates to the pointer overload using A.data(), so A and b are clobbered by the NNLS workspace and should not be read after the call.

Parameters
[in,out]AInput matrix of size m x n; contents are unspecified on return.
[in,out]bRight-hand side of size m; contents are unspecified on return.
[out]xReceives the non-negative solution of size n (resized internally).
Returns
SOLVED on convergence, ITERATION_EXCEEDED when the iteration limit was reached.
Exceptions
Exception::InvalidParameterwhen A.rows() does not match b.size(), or when the underlying NNLS routine reports an invalid dimension.