[Fwd: full size Q output from a QR factorization]
- Subject: [Fwd: full size Q output from a QR factorization]
- From: Ron Boisvert <boisvert@nist.gov>
- Date: Wed, 12 Dec 2007 17:40:39 -0500
- Content-Transfer-Encoding: 7bit
- Content-Type: text/plain; charset=ISO-8859-1; format=flowed
- User-Agent: Thunderbird 2.0.0.9 (Windows/20071031)
Sender: Andreas Kyrmegalos <andreask1@vivodinet.gr>
Subject: full size Q output from a QR factorization
Hi all,
if you happen to need a full size Q matrix (as I have) from a QR
factorization replace the getQ() method in QRfactorization.java,
with this code:
/** Generate and return the (economy-sized) orthogonal factor
@return Q
*/
public Matrix getQ() {
return getQ(true);
}
/** Generate and return the orthogonal factor
@param economy If true the factor is economy sized
@return Q
*/
public Matrix getQ(boolean economy) {
Matrix X = new Matrix(m,economy?n:m);
double[][] Q = X.getArray();
if (!economy) {
if (QR[n-1][n-1] != 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];
}
}
}
}
for (int k = n-1-(economy?0:1); k >= 0; k--) {
Q[k][k] = 1.0;
for (int j = (economy?n:m)-1; j >=k; j--) {
if (QR[k][k] != 0) {
double s = 0.0;
for (int i = k; i < m; i++) {
s += QR[i][k]*Q[i][j];
}
s = -s/QR[k][k];
for (int i = k; i < m; i++) {
Q[i][j] += s*QR[i][k];
}
}
}
}
return X;
}
I have tested it with various sized matrices and the result look ok. But
don't take my word for it, try it yourself :)
Cheers,
Andreas
Date Index |
Thread Index |
Problems or questions? Contact list-master@nist.gov