Calculate least squares using java

I am trying to find java code to calculate the solution of least squares (x) in the equation Ax = b. Let's pretend that

A = [1 0 0;1 0 0];
b = [1; 2];

x = A\b

returns

x =

    1.5000
         0
         0

I found Class LeastSquares,

public LeastSquares(double[] a, double[] b, int degree)

but at the input, both A and B are one-dimensional arrays, however, in the above example, A is a matrix and B is an array.

In class NonNegativeLeastSquares

public NonNegativeLeastSquares(int M, int N, double a[][],double b[])

A is a matrix, and B is an array, but the class description says that it finds an approximate solution of the linear system of equations Ax = b, which || Ax - b || 2 is minimized, and such that x> = 0. This means that x is always positive.

I need the same class as NonNegativeLeastSquares, but without limitation x> = 0. Can someone please help me?
 Many thanks.

+4
source

All Articles