package com.imsl.test.example.math; import com.imsl.math.*; /** *
* Solves a nonnegative least squares problem. *
* Consider the following problem: $$ \begin{bmatrix}1 & -3 & 2\\ -3 & 10 & -5\\ * 2 & -5 & 6\end{bmatrix} \begin{bmatrix}x_1\\ x_2\\ x_3\end{bmatrix} = * \begin{bmatrix}27\\ -78\\ 64\end{bmatrix}$$ ** Subject to the constraint \(x \ge 0\). The NonNegativeLeastSquares class is * used to compute a solution, which is compared to the exact solution of \([1, -4, 7]\). *
* * @see Code * @see Output */ public class NonNegativeLeastSquaresEx1 { public static void main(String args[]) throws Exception { double a[][] = { {1, -3, 2}, {-3, 10, -5}, {2, -5, 6} }; double b[] = {27, -78, 64}; NonNegativeLeastSquares nnls = new NonNegativeLeastSquares(a, b); nnls.solve(); double[] x = nnls.getSolution(); new PrintMatrix("Solution").print(x); // compare solution with exact answer double[][] compare = new double[2][]; compare[0] = Matrix.multiply(a, x); compare[1] = Matrix.multiply(a, new double[]{1, -4, 7}); PrintMatrixFormat pmf = new PrintMatrixFormat(); pmf.setColumnLabels(new String[]{"x >= 0", "exact"}); PrintMatrix pm = new PrintMatrix("Comparison of 'b'"); pm.print(pmf, Matrix.transpose(compare)); } }