Dear LAPACK users,
I try to compute the pseudo inverse of a general MxN matrix using CLAPACK, however the result for a test 3x3 matrix differs from the matrix's inverse and also from what MATLAB's pinv() function returns. I know about the problems of explicitly computing the pseudo inverse, however, I need it.
I attach the code for my function and hope that someone is able to see an error, since I do not...
bool pseudoInverse(float **matrix, int m, int n) {
int info;
int LDWORK = -1;
float *WORK = new float[1];
int NRHS = 0;
int LDA = m;
float *B = new float[m];
for(int i=0; i<m; i++)
B[i] = 0.0;
int LDB = m;
float *SING = new float[(int)(min(m,n))];
float RCOND = -1.0f;
int IRANK = -1;
// 1. compute optimal size of work array
int res = sgelss_( &m, &n, &NRHS, &matrix[0][0], &LDA, B, &LDB, SING, &RCOND, &IRANK, WORK, &LDWORK, &info);
if(info)
return false;
// 2. compute pseudo inverse
LDWORK = WORK[0];
delete [] WORK;
WORK = new float[LDWORK];
res = sgelss_( &m, &n, &NRHS, &matrix[0][0], &LDA, B, &LDB, SING, &RCOND, &IRANK, WORK, &LDWORK, &info);
if(info)
return false;
delete [] B;
delete [] WORK;
delete [] SING;
return true;
}
I appreciate any comment.
Kind regards,
Gordon Wetzstein
Faculty of Media
Bauhaus-University Weimar