## Class NonNegativeLeastSquares

```java.lang.Object
NonNegativeLeastSquares
```

`public class NonNegativeLeastSquaresextends Object`

Class NonNegativeLeastSquares provides a method for solving a least squares minimization problem with nonnegativity constraints. The solve() method finds an approximate solution to the linear system of equations Ax = b, such that ||Ax - b||2 is minimized, and such that x >= 0. The inputs to and outputs from the solve() method are stored in the fields of an instance of class NonNegativeLeastSquares.

The Java code is a translation of the Fortran subroutine NNLS from Charles L. Lawson and Richard J. Hanson, Solving Least Squares Problems (Society for Industrial and Applied Mathematics, 1995), page 161.

Field Summary
` double[][]` `a`
The MxN-element A matrix for the least squares problem.
` double[]` `b`
The M-element b vector for the least squares problem.
` int[]` `index`
The N-element index vector.
` int` `M`
The number of rows, typically the number of input data points, in the least squares problem.
` int` `N`
The number of columns, typically the number of output parameters, in the least squares problem.
` double` `normsqr`
The squared Euclidean norm of the residual vector, ||Ax - b||2.
` int` `nsetp`
The number of elements in the set P; that is, the number of positive values (inactive constraints).
` double[]` `x`
The N-element x vector for the least squares problem.

Constructor Summary
```NonNegativeLeastSquares(int M, int N)```
Construct a new nonnegative least squares problem of the given size.

Method Summary
` void` `solve()`
Solve this least squares minimization problem with nonnegativity constraints.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### M

`public final int M`
The number of rows, typically the number of input data points, in the least squares problem.

### N

`public final int N`
The number of columns, typically the number of output parameters, in the least squares problem.

### a

`public final double[][] a`
The MxN-element A matrix for the least squares problem. On input to the solve() method, a contains the matrix A. On output, a has been replaced with QA, where Q is an MxM-element orthogonal matrix generated during the solve() method's execution.

### b

`public final double[] b`
The M-element b vector for the least squares problem. On input to the solve() method, b contains the vector b. On output, b has been replaced with Qb, where Q is an MxM-element orthogonal matrix generated during the solve() method's execution.

### x

`public final double[] x`
The N-element x vector for the least squares problem. On output from the solve() method, x contains the solution vector x.

### index

`public final int[] index`
The N-element index vector. On output from the solve() method: index[0] through index[nsetp-1] contain the indexes of the elements in x that are in set P, the set of positive values; that is, the elements that are not forced to be zero (inactive constraints). index[nsetp] through index[N-1] contain the indexes of the elements in x that are in set Z, the set of zero values; that is, the elements that are forced to be zero (active constraints).

### nsetp

`public int nsetp`
The number of elements in the set P; that is, the number of positive values (inactive constraints). An output of the solve() method.

### normsqr

`public double normsqr`
The squared Euclidean norm of the residual vector, ||Ax - b||2. An output of the solve() method.

Constructor Detail

### NonNegativeLeastSquares

```public NonNegativeLeastSquares(int M,
int N)```
Construct a new nonnegative least squares problem of the given size. Fields M and N are set to the given values. The array fields a, b, x, and index are allocated with the proper sizes but are not filled in.

Parameters:
`M` - Number of rows (input data points) in the least squares problem.
`N` - Number of columns (output parameters) in the least squares problem.
Throws:
`IllegalArgumentException` - (unchecked exception) Thrown if M <= 0 or N <= 0.
Method Detail

### solve

`public void solve()`
Solve this least squares minimization problem with nonnegativity constraints. The solve() method finds an approximate solution to the linear system of equations Ax = b, such that ||Ax - b||2 is minimized, and such that x >= 0. On input, the field a must be filled in with the matrix A and the field b must be filled in with the vector b for the problem to be solved. On output, the other fields are filled in with the solution as explained in the documentation for each field.

Throws:
`IllegalStateException` - (unchecked exception) Thrown if too many iterations occurred without finding a minimum (more than 3N iterations).

Copyright © 2005 by Alan Kaminsky. All rights reserved. Send comments to ark­@­cs.rit.edu.