我试图在 C++ LAPACKE 中反转一个实矩阵。对于复杂的矩阵,我有相同的功能并且它有效。但真实案例给出了错误的答案。这是我的功能:
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;
}
我尝试反转一个 24 x 24 的双精度矩阵。虽然程序似乎几乎就在那里,但逆还没有完全出现,它与 python linalg inverse 给我的有很大不同(python 就在这里,因为我将矩阵乘以逆,结果非常接近缩进)。在 LAPACKE 输出中,我将矩阵乘以它的逆矩阵,我得到对角线为 1,但非对角线的值高达 0.17,与 0 相比,这是巨大的。有没有办法让 LAPACKE 程序提供更好的结果?谢谢!