qr factorization in LAPACK / Orthonormalization
Dear all,
I need to do an orthonormalization for a set of vectors in 3D space. I.e., I have 3 vectors or a 3x3 matrix as input respectively. Since it may happen that sometimes 3 vectors lay almost on a line, I thought Gram - Schmidt is to unstable ( read in http://www.wikipedia.org/wiki/QR_decomposition). Than I read that QR factorization is a different approach for this orthonormalization. I found in this forum a C++ template (thanks a lot to the author) and modified it. Here it ( lapack_forum_example.cpp) is:
#include <iostream>
using namespace std;
extern "C" void dgeqrf_(int *m, int *n, double da[], int *lda, double dtau[], double dwork[], int *ldwork, int *info);
int main(int argv, char** argc)
{
int n = 3;
int m = 3;
int lda = 3;
int info;
double work[3];
double tau[3];
double A[9];
A[0] = 12.0;
A[1] = 6.0;
A[2] = -4.0;
A[3] = -51.0;
A[4] = 167.0;
A[5] = 24.0;
A[6] = 4.0;
A[7] = -68.0;
A[8] = -41.0;
cout << "input A:" << endl;
for(int i=0; i < m; i++)
{
for(int j=0; j < n; j++)
{
cout << A[i*n +j] << endl;
}
cout << ""<< endl;
}
cout << "------------------------------------------------------------\n"<< endl;
dgeqrf_(&m,&n,A,&lda,tau,work,&n,&info);
if (info == 0)
{
cout << "output A:"<< endl;
for(int i=0; i < m; i++)
{
for(int j=0; j < n; j++)
{
cout << A[i*n+j] << endl;
}
cout << "" << endl;
}
}
else cout << "Problem!!! info = " << info << "\n";
cout << "------------------------------------------------------------\n"<< endl;
return 0;
}
Compilation was done successfully via:
g++ -o lapack_forum_example lapack_forum_example.cpp -llapack
Obtained output is:
input A:
12
6
-4
-51
167
24
4
-68
-41
------------------------------------------------------------
output A:
-14
0.230769
-0.153846
-21
-175
0.0555556
14
70
-35
------------------------------------------------------------
Ifaik that "output A" corresponds to the matrix R of the QR decomposition. I cross-checked this output of R on wikipedia. And the values of the lower triangle are all 0 - fine.
Even taking rounding errors for the non - zero values of the output shown above, the difference might be significant/problematic for further matrix multiplications. I also cross-checked with octave - same output as above.
Why are the values in the lower triangle of R not (closer to) 0?
Is there something to improve in this source code?
Alternatively: Is there another / a better LAPACK - based way to obtain Orthonormalization?
Thanks a lot in advance,
Gerd
I need to do an orthonormalization for a set of vectors in 3D space. I.e., I have 3 vectors or a 3x3 matrix as input respectively. Since it may happen that sometimes 3 vectors lay almost on a line, I thought Gram - Schmidt is to unstable ( read in http://www.wikipedia.org/wiki/QR_decomposition). Than I read that QR factorization is a different approach for this orthonormalization. I found in this forum a C++ template (thanks a lot to the author) and modified it. Here it ( lapack_forum_example.cpp) is:
#include <iostream>
using namespace std;
extern "C" void dgeqrf_(int *m, int *n, double da[], int *lda, double dtau[], double dwork[], int *ldwork, int *info);
int main(int argv, char** argc)
{
int n = 3;
int m = 3;
int lda = 3;
int info;
double work[3];
double tau[3];
double A[9];
A[0] = 12.0;
A[1] = 6.0;
A[2] = -4.0;
A[3] = -51.0;
A[4] = 167.0;
A[5] = 24.0;
A[6] = 4.0;
A[7] = -68.0;
A[8] = -41.0;
cout << "input A:" << endl;
for(int i=0; i < m; i++)
{
for(int j=0; j < n; j++)
{
cout << A[i*n +j] << endl;
}
cout << ""<< endl;
}
cout << "------------------------------------------------------------\n"<< endl;
dgeqrf_(&m,&n,A,&lda,tau,work,&n,&info);
if (info == 0)
{
cout << "output A:"<< endl;
for(int i=0; i < m; i++)
{
for(int j=0; j < n; j++)
{
cout << A[i*n+j] << endl;
}
cout << "" << endl;
}
}
else cout << "Problem!!! info = " << info << "\n";
cout << "------------------------------------------------------------\n"<< endl;
return 0;
}
Compilation was done successfully via:
g++ -o lapack_forum_example lapack_forum_example.cpp -llapack
Obtained output is:
input A:
12
6
-4
-51
167
24
4
-68
-41
------------------------------------------------------------
output A:
-14
0.230769
-0.153846
-21
-175
0.0555556
14
70
-35
------------------------------------------------------------
Ifaik that "output A" corresponds to the matrix R of the QR decomposition. I cross-checked this output of R on wikipedia. And the values of the lower triangle are all 0 - fine.
Even taking rounding errors for the non - zero values of the output shown above, the difference might be significant/problematic for further matrix multiplications. I also cross-checked with octave - same output as above.
Why are the values in the lower triangle of R not (closer to) 0?
Is there something to improve in this source code?
Alternatively: Is there another / a better LAPACK - based way to obtain Orthonormalization?
Thanks a lot in advance,
Gerd