Pegah
Pegah

Reputation: 682

Compute least squares using java

I am trying to find a java code to compute the least squares solution (x) in the Ax=b equation. Suppose that

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

x = A\b

returns the

x =

    1.5000
         0
         0

I found Class LeastSquares,

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

but in the input both A and B are one dimensional arrays, however, in 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 description of the class says that it finds an approximate solution to the linear system of equations Ax = b, such that ||Ax - b||2 is minimized, and such that x >= 0. Which means that x must be always positive.

I need a similar class as NonNegativeLeastSquares, however with out the x>=0 constraint. Could someone please help me?
thanks a lot.

Upvotes: 5

Views: 15621

Answers (1)

maerics
maerics

Reputation: 156572

See the Apache Commons Math library, specifically the SimpleRegression class.

Upvotes: 10

Related Questions