[Fwd: Re: correction to full size Q output from a QR factorization]


Sender: Andreas Kyrmegalos <andreask1@vivodinet.gr>
Subject: Re: correction to full size Q output from a QR factorization


Hello Sione,
  my additions to jama's qr algorithm only affected the Q factor output.

If I understand correctly what you ask is for a way to have a choice for 
the row size of the R factor in the same manner as in the case of the Q 
matrix output addition.

One way is to switch the first two lines of code of the getR() method:

  public Matrix getR () {
     Matrix X = new Matrix(n,n);

with:
 
  public Matrix getR(boolean economy) {
     Matrix X = new Matrix(economy?n:m,n);

And in order to keep compatibility with any already compiled code that 
uses the original getR() method, just add this method to the code:

  public Matrix getR() {
     return getR(true);
  }

So, if you apply both changes, calls to getR() will return a nxn R 
matrix. Calls to getR(true) will also return a nxn matrix. But calls to 
getR(false) will return a mxn matrix.

I hope this helps.

Andreas



Sione wrote:
> Hello Andreas,
>
> I  have the following example using your code:
> ==============================
>
> double[][] aa = {{0.5774,    0.5774,    0.5774}};
> Matrix A = new Matrix(aa);
> A = A.transpose();
>   QRDecomposition QRdecomp = new QRDecomposition(A);
> Matrix Q = QRdecomp.getQ(false);
> System.out.println("---------- Q ----------");
> Q.print(8,4);
>   Matrix R = QRdecomp.getR();
> System.out.println("---------- R ----------");
> R.print(8,4);
>
> ===============================
>
>
> The output is show below:
>
> ---------- Q ----------
>
>  -0.5774   -0.5774   -0.5774
>  -0.5774    0.7887   -0.2113
>  -0.5774   -0.2113    0.7887
>
> ---------- R ----------
>
>  -1.0001
>
>
>
> I know that  R is suppose to be a column vector as shown below:
>
> ---------- R ----------
>
> -1.0001
>        0
>        0
>
>
> Is there any way to make or modify the  method call :   "getR( )"  to 
> output the correct column vector as shown above?   I do use your code 
> as it is the same as that of Matlab  "qr"  function.
>
> Cheers,
> Sione.
>
>
>
>
> Andreas Kyrmegalos wrote:
>> Hello again,
>> in the QRDecomposition code I have neglected to include the case when 
>> rank= n-1 and no householder vector has been calculated. I have an 
>> implementation of the QR algorithm that allows pivoting but I m 
>> skeptical about releasing it. This slight omission is a good example 
>> of why I refrain from its release.
>>
>> Anyway here's the corrected code (the only change is the addition of 
>> the else clause):
>>
>>     ....
>>     if (!economy) {
>>        if (QR[n-1][n-1] != 0.0) {
>>           for(int k = m-1; k >= n-1;k--) {
>>              Q[k][k] = 1.0;
>>              double s = 0.0;
>>               for (int i = n-1; i <=k; i++) {
>>                 s += QR[i][n-1]*Q[i][k];
>>              }
>>              s = -s/QR[n-1][n-1];
>>              Q[k][k] += s*QR[k][n-1];
>>              for (int i = m-1; i >k; i--) {
>>                 Q[i][k] += s*QR[i][n-1];
>>                 Q[k][i] = Q[i][k];
>>              }
>>           }
>>        }
>>        else {
>>           for(int k = m-1; k >= n-1;k--)
>>              Q[k][k] = 1.0;
>>        }
>>     }      ....
>>
>>  I apologize to anyone who may have used the code but as I've pointed 
>> out before, please do test the code before using it.
>>
>> Cheers,
>> Andreas
>>
>>
>>
>>
>>
>
>
>
>
>






Date Index | Thread Index | Problems or questions? Contact list-master@nist.gov