4
votes

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?

1
If you just need to solve a system of equations, try Octave or any other numerical system.Robert Dodier
Thanks for the suggestion. This is part of a almost - finished program that also does other things. It would make little sense to change environment now. Also, I would like to figure out why does this not work.Jan Malec

1 Answers

1
votes

I have figured out what is wrong. The routine does not work if I use the type complex. This is really strange, because this type worked in all other LAPACK routines that I have tried so far, even complex matrix inverse, that is in a way similar.