我试图使用 LAPACKE 包反转C++中的复杂矩阵。我的代码如下:
void inv(std::vector<std::vector<std::complex<double>>> &ans, std::vector<std::vector<std::complex<double>>> MAT){
int N = MAT.size();
int *IPIV = new int[N];
std::complex<double> ** arr = new std::complex<double>*[N];
for (int i = 0; i<N; i++){
arr[i] = new std::complex<double>[N];
for (int j = 0; j<N; j++){
arr[i][j] = MAT[i][j];
}
}
LAPACKE_cgetrf(LAPACK_ROW_MAJOR, N, N, arr[0], N, IPIV);
LAPACKE_cgetri(LAPACK_ROW_MAJOR, N, arr[0], N, IPIV);
for (int i = 0; i<N; i++){
for (int j = 0; j<N; j++){
ans[i][j] = arr[i][j];
}
}
delete IPIV;
delete arr;
}
这给出了一个错误:
utils.cc:253:59: error: cannot convert 'std::complex<double>*' to '__complex__
float*' for argument '4' to 'int LAPACKE_cgetrf(int, int, int, __complex__ float*,
int, int* '
LAPACKE_cgetrf(LAPACK_ROW_MAJOR, N, N, arr[0], N, IPIV); ^
utils.cc:254:56: error: cannot convert 'std::complex<double>*' to '__complex__
float*' for argument '3' to 'int LAPACKE_cgetri(int, int, __complex__ float*, int,
const int*)'
LAPACKE_cgetri(LAPACK_ROW_MAJOR, N, arr[0], N, IPIV);
我尝试做:
__complex__ float ** arr = new __complex__ float *[N];
for (int i = 0; i<N; i++){
arr[i] = new __complex__ float [N];
for (int j = 0; j<N; j++){
arr[i][j] = reinterpret_cast<float(&)[2]>(MAT[i][j]);
}
}
对于函数中的第一个循环,但它给出:
error: cannot convert 'float [2]' to '__complex__ float' in assignment
arr[i][j] = reinterpret_cast<float(&)[2]>(MAT[i][j]);
我做错了什么?我还有另一个 inv 函数,可以很好地处理双打。这是我遇到麻烦的复杂类型。
谢谢
LAPACK 可能需要连续的数据块。台词
void inv(std::vector<std::vector<std::complex<double>>> &ans, std::vector<std::vector<std::complex<double>>> MAT){
using cfloat = __complex__ float;
int N = MAT.size();
cfloat* arr = new cfloat [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]).real() + _Complex_I*(MAT[i][j]).imag();
}
}
int* IPIV = new int[N];
LAPACKE_cgetrf(LAPACK_ROW_MAJOR, N, N, arr, N, IPIV);
LAPACKE_cgetri(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;
}