I am trying to invert a real matrix in C++ LAPACKE. I have the same function for complex matrices and it works. But the real case gives the wrong answer. Here's my function:
void inv(std::vector<std::vector<double>> &ans, std::vector<std::vector<double>> MAT){
int N = MAT.size();
int *IPIV = new int[N];
double * arr = new double[N*N];
for (int i = 0; i<N; i++){
for (int j = 0; j<N; j++){
int idx = i*N + j;
arr[idx] = MAT[i][j];
}
}
LAPACKE_dgetrf(LAPACK_ROW_MAJOR, N, N, arr, N, IPIV);
LAPACKE_dgetri(LAPACK_ROW_MAJOR, N, arr, N, IPIV);
for (int i = 0; i<N; i++){
for (int j = 0; j<N; j++){
int idx = i*N + j;
ans[i][j] = arr[idx];
}
}
delete[] IPIV;
delete[] arr;
}
I tried inverting a 24 by 24 matrix of doubles. While the program seems to be almost there, the inverse is not quite there yet and it differs a lot from what python linalg inverse gives me (python is right here because I multiplied the matrix with inverse and result is very close to indentity). In the LAPACKE output, I multiply the matrix by its inverse and I get the diagonals being 1 but the non diagonals go to values up to 0.17, which is huge compared to 0. Is there a way to make the LAPACKE program to give a better result? Thanks!
linalg.inv(), in particular line 1555 - 1693 -1727 of umath_linalg.c, it solves A.X=Id, using dgesv() instead of calling dgetrf() and dgetri(). Scipy makes use of dgetrf() / dgetri(). Could you try to use scipy'sscipy.linalg.inv()in python and see if it also result in a correct output? - francis