2013-10-29 122 views
4

我想計算一個複雜矩陣的反演。它發生在我身上 lapack包含許多與代數計算相關的例程,所以我找到了子程序ZGETRI。不料,編譯 下面的代碼後「ifort -o出-heap陣列test.f90 -mkl」和 運行文件「走出去」,我得到了一個錯誤使用lapack計算逆矩陣

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 

回答

2

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 
+0

到文檔的鏈接斷開,我建議http://www.netlib.org/ lapack/lug/node38.html – Ross

+0

@Ross謝謝,我更新了鏈接。 –