I am trying to compute the inverse of a complex matrix with ZGETRI, but even if it executes without error (info = 0), it does not give me the correct inverse matrix and I have absolutely no idea where the error comes from.
PROGRAM solvelinear
implicit none
INTEGER :: i,j,info,lwork
INTEGER,dimension(3) :: ipiv
COMPLEX(16), dimension(3,3) :: C,Cinv,M,LU
COMPLEX(16),allocatable :: work(:)
info=0
lwork=100
allocate(work(lwork))
ipiv=0
work=(0.d0,0.d0)
C(1,1)=(0.d0,-1.d0)
C(1,2)=(1.d0,5.d0)
C(1,3)=(2.d0,-2.d0)
C(2,1)=(4.d0,-1.d0)
C(2,2)=(2.d0,-3.d0)
C(2,3)=(-1.d0,2.d0)
C(3,1)=(1.d0,0.d0)
C(3,2)=(3.d0,-2.d0)
C(3,3)=(0.d0,1.d0)
write(*,*)"C = "
do i=1,3
write(*,10)(C(i,j),j=1,3)
end do
!-- LU factorisation
LU=C
CALL ZGETRF(3,3,LU,3,ipiv,info)
write(*,*)'info = ',info
write(*,*)"LU = "
do i=1,3
write(*,10)(LU(i,j),j=1,3)
end do
!-- Inversion of matrix C using the LU
Cinv=LU
CALL ZGETRI(3,Cinv,3,ipiv,work,lwork,info)
write(*,*)'info = ',info
write(*,*)"Cinv = "
do i=1,3
write(*,10)(Cinv(i,j),j=1,3)
end do
!-- computation of C^-1 * C to check the inverse
M = matmul(Cinv,C)
write(*,*)"M = "
do i=1,3
write(*,10)(M(i,j),j=1,3)
end do
10 FORMAT(3('(',F20.10,',',F20.10,') '))
END PROGRAM solvelinear
I compile with ifort
(and my LAPACK
librairies version 3.7.1 are also compiled with ifort). Makefile:
#$Id: Makefile $
.SUFFIXES: .f90 .f .c .o
FC = ifort
FFLAGS = -g -check all -zmuldefs -i8
LIBS = -L/path/to/lapack-3.7.1 -llapack -ltmglib -lrefblas
MAIN = prog.o
EXEC = xx
all: ${MAIN} Makefile
${FC} ${FFLAGS} -o ${EXEC} ${MAIN} ${LIBS}
.f.o: ${MODS} Makefile
${FC} ${FFLAGS} -c $<
.f90.o: ${MODS} Makefile
${FC} ${FFLAGS} -c $<
I have no errors when compiling. Here is my output:
C =
( 0.00000, -1.00000) ( 1.00000, 5.00000) ( 2.00000, -2.00000)
( 4.00000, -1.00000) ( 2.00000, -3.00000) ( -1.00000, 2.00000)
( 1.00000, 0.00000) ( 3.00000, -2.00000) ( 0.00000, 1.00000)
info = 0
LU =
( 4.00000, 0.00000) ( 2.00000, 120470.58824) ( 2.00000, -2.00000)
( 0.00000, 0.00000) (28003147.29412, -3.00000) ( -1.00000, 2.00000)
( 1.00000, 0.00000) ( 3.00000, -2.00000) ( 0.00000, 1.00000)
info = 0
Cinv =
( 0.00000, 0.00000) ( -0.00000, -0.00000) ( 2.00000, -2.00000)
( -0.00000, 0.00000) ( -0.00000, -3.00000) ( -1.00000, 2.00000)
( -0.00000, -0.00000) ( 3.00000, -2.00000) ( 0.00000, 1.00000)
M =
( 2.00000, -2.00000) ( 2.00000, -10.00000) ( 2.00000, 2.00000)
( -4.00000, -10.00000) ( -8.00000, 2.00000) ( 4.00000, 2.00000)
( 10.00000, -10.00000) ( 2.00000, -10.00000) ( -0.00000, 8.00000)
And M should be the identity if I'm not wrong.