Error at lapack cgesv when matrix is not singular
- by Jan Malec
This is my first post. I usually ask classmates for help, but they have a lot of work now and I'm too desperate to figure this out on my own :). I am working on a project for school and I have come to a point where I need to solve a system of linear equations with complex numbers. I have decided to call lapack routine "cgesv" from c++. I use the c++ complex library to work with complex numbers.
Problem is, when I call the routine, I get error code "2".
From lapack documentation:
INFO is INTEGER
= 0: successful exit
< 0: if INFO = -i, the i-th argument had an illegal value
> 0: if INFO = i, U(i,i) is exactly zero. The factorization
has been completed, but the factor U is exactly
singular, so the solution could not be computed.
Therefore, the element U(2, 2) should be zero, but it is not.
This is how I declare the function:
void cgesv_( int* N, int* NRHS, std::complex* A, int* lda, int* ipiv, std::complex* B, int* ldb, int* INFO );
This is how I use it:
int *IPIV = new int[NA];
int INFO, NRHS = 1;
std::complex<double> *aMatrix = new std::complex<double>[NA*NA];
for(int i=0; i<NA; i++){
for(int j=0; j<NA; j++){
aMatrix[j*NA+i] = A[i][j];
}
}
cgesv_( &NA, &NRHS, aMatrix, &NA, IPIV, B, &NB, &INFO );
And this is how the matrix looks like:
(1,-160.85) (0,0.000306796) (0,-0) (0,-0) (0,-0)
(0,0.000306796) (1,-40.213) (0,0.000306796) (0,-0) (0,-0)
(0,-0) (0,0.000306796) (1,-0.000613592) (0,0.000306796) (0,-0)
(0,-0) (0,-0) (0,0.000306796) (1,-40.213) (0,0.000306796)
(0,-0) (0,-0) (0,-0) (0,0.000306796) (1,-160.85)
I had to split the matrix colums, otherwise it did not format correctly.
My first suspicion was that complex is not parsed correctly, but I have used lapack functions with complex numbers before this way.
Any ideas?