Le 27/04/2011 17:23, Luc Maisonobe a écrit :
> Le 27/04/2011 15:52, Cedric Martin a écrit :
>> Hi all,
>
> Hi Cédric,
>
>>
>> I have a simplistic implementation of least squares using Jama Matrix which
>> I would like to migrate to Math Commons if possible. I'm running into
>> issues where the LUDecomposition requires a square matrix, and what I want
>> to offer is a rectangular matrix. The following code will hopefully clarify
>> (note that the following code does produce the expected results, using Jama
>> Matrix)):
>>
>>
>> Matrix matrixX = new Matrix(*length, order*);
>>
>> Matrix matrixY = new Matrix(length, 1);
>>
>> for (int j = 0; j < this.order; j++) {
>>
>> for (int i = 0; i < length; i++) {
>>
>> matrixX.set(i, j, Math.pow(i + offset, j));
>>
>> }
>>
>> }
>>
>>
>> double[] values = super.getValues();
>>
>>
>> for (int i = 0; i < length; i++) {
>>
>> matrixY.set(i, 0, values[i]);
>>
>> }
>>
>>
>> double[] result = { 0.0, 0.0 };
>>
>>
>> if (length >= this.order) {
>>
>> result = matrixX.solve(matrixY).getRowPackedCopy();
>>
>> }
>>
>>
>> Would anyone be able to help me with this? Am I simply be using the wrong
>> objects?
>
> Yes, you are using the wrong classes.
> LU decomposition only works with square matrices and it only solves
> problemns when an exact solution does exist.
>
> As you need a least square approximation when the number of free
> variables and the number of equations do not match, you should use
> another type of decomposition. The most effective one in this case is QR
> decomposition. You can use it just as LU decomposition, the API is
> really similar.
>
> In fact, you can even use QR decomposition for square matrices too. In
> this case it would involve twice as much operations as LU decomposition,
> but would be numerically more stable. This numericall stability explains
> that in many cases, QR decomposition is used even if LU decomposition
> could theoretically also be used.
I forgot to say that he decomposition algorithms classes are separate
from the matrix classes (but they are in the same package). So you first
build your matrix, and then you build a QRDecomposition instance from
this matrix. The QRDecomposition then provides you with a solver.
See the user guide here for an explanation:
<http://commons.apache.org/math/userguide/linear.html#a3.4_Solving_linear_systems>
The solve methods directly in the matrix classes have been deprecated
since 2.0 and removed in the current development version in the
subversion repository.
Luc
>
>>
>> I also have to build a simple Recursive least squares implementation using
>> similar objects (RealMatrix I'm assuming), would anyone be able to point me
>> in the right direction for this as well, while using the Commons Math
>> library?
>
> We don't have a recursive least square implementation now.
>
> best regards,
> Luc
>
>>
>>
>> Thank you very much!
>>
>
>
> 
> To unsubscribe, email: userunsubscribe@commons.apache.org
> For additional commands, email: userhelp@commons.apache.org
>

To unsubscribe, email: userunsubscribe@commons.apache.org
For additional commands, email: userhelp@commons.apache.org
