使用lapack计算逆矩阵

时间:2013-10-29 10:08:34

标签: matrix fortran lapack

我想计算复杂矩阵的反演。它发生在我身上 lapack包含许多与代数计算相关的例程, 所以我找到了子程序ZGETRI。没想到,编译之后 以下代码使用“ifort -o out -heap-arrays test.f90 -mkl”和 运行文件“out”,我收到错误

检测到glibc ./ out:free():指针无效:0x00007fee68f76010 ***”

后跟内存映射,最后“中止(核心转储)”。 这对我来说很奇怪,我不知道错误在哪里。 顺便说一句,当一些错误出现在编译过程中时,但是 正在运行的进程,有没有办法检测这个错误的位置 从?

program test
Implicit none
integer,parameter::M=300   
complex*16,allocatable,dimension(:,:)::A
complex*16,allocatable,dimension(:)::WORK
integer,allocatable,dimension(:)::IPIV
integer i,j,info,error

allocate(A(M,M),WORK(M),IPIV(M),stat=error)
if (error.ne.0)then
  print *,"error:not enough memory"
  stop
end if

!definition of the test matrix A
 do i=1,M
   do j=1,M
      if(j.eq.i)then
         A(i,j)=(1,0)
      else 
         A(i,j)=0
      end if
   end do
 end do  

call ZGETRI(M,A,M,IPIV,WORK,M,info)
if(info .eq. 0) then
  write(*,*)"succeded"
else
 write(*,*)"failed"
end if
deallocate(A,IPIV,WORK,stat=error)
if (error.ne.0)then
  print *,"error:fail to release"
  stop
end if      
end 

1 个答案:

答案 0 :(得分:3)

来自documentation

ZGETRI computes the inverse of a matrix using the LU factorization
computed by ZGETRF.

您需要先运行ZGETRF

program test
  Implicit none
  integer,parameter::M=300   
  complex*16,allocatable,dimension(:,:)::A
  complex*16,allocatable,dimension(:)::WORK
  integer,allocatable,dimension(:)::IPIV
  integer i,j,info,error

  allocate(A(M,M),WORK(M),IPIV(M),stat=error)
  if (error.ne.0)then
    print *,"error:not enough memory"
    stop
  end if

  !definition of the test matrix A
   do i=1,M
     do j=1,M
        if(j.eq.i)then
           A(i,j)=(1,0)
        else 
           A(i,j)=0
        end if
     end do
   end do  

  call ZGETRF(M,M,A,M,IPIV,info)
  if(info .eq. 0) then
    write(*,*)"succeded"
  else
   write(*,*)"failed"
  end if

  call ZGETRI(M,A,M,IPIV,WORK,M,info)
  if(info .eq. 0) then
    write(*,*)"succeded"
  else
   write(*,*)"failed"
  end if
  deallocate(A,IPIV,WORK,stat=error)
  if (error.ne.0)then
    print *,"error:fail to release"
    stop
  end if      
end