matrix - LAPACK 给我错误的特征值

标签 matrix fortran lapack eigenvalue

我正在使用 LAPACK 库中的 DSYEV 和 DSYEVD 来查找特征值和特征向量(编译语法: gfortran -llapack )。但是,我发现特定矩阵的错误特征值(-0.44,0.35,0.88)。出了什么问题?

我们很容易看出矩阵的行列式为零,因此至少有一个特征值必须为零。

这是我的代码(希望它不会太大):

    Program Real_Eigenvec
    implicit none

    integer, parameter:: n=3
    integer:: i,j, flag
    real*8:: A(n,n),X(n,n) 
    real*8:: lambda(n)
    real*8, parameter:: p=0.5d0/dsqrt(2.d0), q=1.d0-1.d0/dsqrt(2.d0)


    Print*,'Enter flag: 0 for DSYEV, 1 for DSYEVD'
    Read*, flag


    A= transpose(reshape((/ 0.d0, 1.d0, 0.d0, p, q, p, 0.5d0, 0.0d0, 0.5d0 /), shape(A)))


    print*,'Dimension of the matrix, n=',int(sqrt(float(size(A))))

    Print*,'A matrix in full form:'
    Do i=1,n
      print 100, (A(i,j),j=1,n)
    End Do

    call Eigen(A,lambda,X,n,flag)

    !    Print the eigenvalues and eigenvectors.

    PRINT 200
    DO i  = 1, n
      PRINT 201, lambda(i), (X(i,j), j=1,n)
    END DO

100 FORMAT (1X,10(:2X,F10.2))
200 FORMAT (/1X, 'Eigenvalue', 16X, 'Eigenvector^T')
201 FORMAT (1X, F10.2,4X,6(:f10.2))

    End Program Real_Eigenvec



!!!      SUBROUTINES -----------------------------------------


    Subroutine Eigen(A,lambda,X,n,flag)
    implicit none

    integer:: i,n,flag
    real*8:: A(n,n),Ap(n,n),X(n,n)
    real*8:: lambda(n)
    real*8, allocatable  :: work(:)
        integer, allocatable :: iwork(:)
    integer:: lwork,liwork,info

    print*,'n in Eiegen routine=',n

    lwork=3*n-1      ! DSYEV for flag=0
    if (flag==1) then ! DSYEVD for flag=1
      lwork=1+6*n+2*n**2
    end if 

    liwork=3+5*n


    allocate(work(lwork))
    allocate(iwork(liwork))

    Ap=A

    if (flag==0) then
      CALL DSYEV ('v', 'l', n, Ap, n, lambda, work, lwork, info)
    else
      CALL  DSYEVD ('V', 'U', n, Ap, n, lambda, work, &
                &   lwork, iwork, liwork, info)
      ! For doumentation visit: http://www.netlib.org/lapack/explore-html/d1/da2/dsyevd_8f.html
    end if

    X=Ap

    print*,'info=',info

    deallocate(work)
    deallocate(iwork)

    End Subroutine Eigen

最佳答案

如 lapack documentation 中所述DSYEV 可用于对称矩阵。

DSYEV computes all eigenvalues and, optionally, eigenvectors of a real symmetric matrix A.

在示例中,矩阵A不是对称的

Dimension of the matrix, n=           3
A matrix in full form:
     0.00        1.00        0.00
     0.35        0.29        0.35
     0.50        0.00        0.50

在这种情况下,您应该使用用于非对称 matricesDGEEV

DGEEV computes for an N-by-N real nonsymmetric matrix A, the eigenvalues and, optionally, the left and/or right eigenvectors.

一般情况下,特征值是复数,因此您必须提供 WRWL。此外,您还需要定义是否需要左 VL 或右 VR 特征向量。

A * v(j) = lambda(j) * v(j)
u(j)**H * A = lambda(j) * u(j)**H

函数的定义是:

DGEEV(JOBVL, JOBVR, N, A, LDA, WR, WI, VL, LDVL, VR, LDVR, WORK, LWORK, INFO)

我建议像这样使用它

LWORK = 4*N
CALL DGEEV( 'N', 'V', n, A, n, wr, wl, Ap, n, Ap, n, work, lwork, info )

为了获得左右特征向量,请使用

real*8:: A(n,n),VL(n,n),VR(n,n)
real*8:: wr(n),wl(n)
lwork = 4*N
allocate(work(lwork))
CALL DGEEV( 'V', 'V', n, A, n, wr, wl, VL, n, VR, n, work, lwork, info )

对于您的矩阵,所有特征值的虚部为零。因此特征值为(1.00, -0.21, 0.00)

关于matrix - LAPACK 给我错误的特征值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29698600/

相关文章:

fortran - 如何将输出写入 Fortran 中的字符串?

matrix - Intel Extended Eigensolver(用于稀疏矩阵)极慢

python - Cython调用lapack,报错: `Cannot take address of Python variable'

c - Scalapack中处理器子集的不相交网格及其通信

excel - 将矩阵转换为 3 列表 ('reverse pivot' 、 'unpivot' 、 'flatten' 、 'normalize' )

c++ - 计算行列式的最快方法是什么?

matrix - 奇异值分解

c# - 在 C++ 代码中更改 Dll 的 .NET 4 运行时激活策略

c# - Visual C# 在调用包装的 C++\CLI dll 中的 Lapack 时抛出 System.AccessViolationException

c - 使用矩阵算法和常量进行嵌套 for 循环调试。