本文介绍了lapack routin zheev的危险行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用lapack例程zheev()时,我偶然发现了一个奇怪的行为.我不明白有两个问题

I stumbled upon an odd behavior when using the lapack routine zheev(). There are two issues which I do not understand

1)我的一个全局变量似乎被zheev()覆盖.下面的小程序显示了它:

1) One of my global variables seems to be overwritten by zheev(). The following small program shows it:

[由gfortran -o test test.f90 -llapack -lblas编译]

program test    
implicit none

integer, parameter :: dp = 8
integer, parameter :: dim = 3

integer :: l

real(dp), parameter :: kmin = -0.03_dp
real(dp), parameter :: kmax = 0.03_dp
integer, parameter  :: steps = 100
real(dp) :: stepD = (kmax - kmin)/real(steps)
complex(dp) :: matrix(dim,dim)=0.

integer :: info
integer :: rwork=3*dim-2, lwork, lwmax=100
real(dp) :: evals(dim) 
complex(dp) :: work(3*dim-2)

lwork=-1
call zheev('N','U',size(matrix,1), matrix, dim, evals, work, lwork, &
            rwork, info) 
lwork = min( lwmax, int( work(1) ))

do l = 0, 3
    write(*,*) stepD
    call zheev('N', 'U', size(matrix,1), matrix, dim, evals, work, &
    lwork, rwork, info) 
    write(*,*) stepD
end do

end program test

输出为

5.9999999999999995E-004
   0.0000000000000000     
   0.0000000000000000     
   0.0000000000000000     
   0.0000000000000000     
   0.0000000000000000     
   0.0000000000000000     
   0.0000000000000000  

这可以通过将stepD设置为参数来解决.但是我不了解这种行为.越来越奇怪了:

This can be cured by setting stepD to a parameter. But I do not understand this behavior. It is getting even more weird:

2)将定义中的lwork设置为某个值,例如

2) Setting lwork in the definition to some value like

integer :: rwork=3*dim-2, lwork=10, lwmax=100

(只需更改上面的相应行)给出以下结果:

(simply change the corresponding line above)gives the following result:

   5.9999999999999995E-004
   5.9999991208314896E-004
   5.9999991208314896E-004
   5.9999991208314896E-004
   5.9999991208314896E-004
   5.9999991208314896E-004
   5.9999991208314896E-004
   5.9999991208314896E-004

这怎么会发生?将lwork设置为10应该没有任何作用,因为稍后将其设置为-1.一个重要的概念是:如果删除了

How can this happen? Setting lwork to 10 should have no effect since later on it is set to -1. An important notion is: If one removes

lwork=-1
call zheev('N','U',size(matrix,1), matrix, dim, evals, work, lwork, &
            rwork, info) 
lwork = min( lwmax, int( work(1) ))

代码工作正常.

推荐答案

来自zheev的文档( http://www.netlib.org/lapack/explore-html/df/d9a/group__complex16_h_eeigen_gaf23fb5b3ae38072f4890ba43d5cfea2 fca3b

From the documentation of zheev (http://www.netlib.org/lapack/explore-html/df/d9a/group__complex16_h_eeigen_gaf23fb5b3ae38072ef4890ba43d5cfea2.html#gaf23fb5b3ae38072ef4890ba43d5cfea2):

subroutine zheev    (   character   JOBZ,
        character   UPLO,
        integer     N,
        complex*16, dimension( lda, * )     A,
        integer     LDA,
        double precision, dimension( * )    W,
        complex*16, dimension( * )      WORK,
        integer     LWORK,
        double precision, dimension( * )    RWORK,
        integer     INFO 
    )   

在这里我们看到RWORK是双精度类型的数组,但是在提供的代码中rwork是标量整数

here we see that RWORK is an array of type double precision, but in the supplied code rwork is a scalar integer

这篇关于lapack routin zheev的危险行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!