[Fortran] 纯文本查看 复制代码
PROGRAM Generalizedeigenvalue
use lapack95
IMPLICIT NONE
INTEGER,PARAMETER::N=3!矩阵维数
INTEGER,PARAMETER::LDA=N
INTEGER,PARAMETER::LDB=N
INTEGER,PARAMETER::LDVL=N
INTEGER,PARAMETER::LDVR=N
INTEGER,PARAMETER::LWORK=8*n+16
INTEGER::INFO,J
REAL*8::A(LDA,N),B(LDA,N)!系数矩阵
REAL*8::ALPHAR(N),ALPHAI(N),BETA(N)!特征值和对应的广义特征向量
REAL*8::VL(LDVL,N),VR(LDVR,N)!左右特征向量
REAL*8::WORK(LWORK,LWORK)
!初始化系数矩阵
A=RESHAPE((/1.D0,2.D0,3.D0,4.D0,5.D0,6.D0,7.D0,8.D0,9.D0/),SHAPE(A))
B=RESHAPE((/2.D0,2.D0,2.D0,2.D0,2.D0,2.D0,2.D0,2.D0,2.D0/),SHAPE(B))
A = transpose(A)
B = transpose(B)
!调用DGGEV函数
CALL DGGEV('N','V',N,A,LDA,B,LDB,ALPHAR,ALPHAI, BETA, VL, LDVL, VR, LDVR, WORK, LWORK, INFO)
!输出结果
IF(INFO==0.d0)then
WRITE(*,*)'广义特征值问题的解:'
DO J=1,N
IF(ALPHAI(J)==0.D0)THEN
WRITE(*,'(2(A,1X),F8.4)')'特征值',J,':',ALPHAR(J)
ELSE
WRITE(*,'(4(A,1X),2(F8.4,1X))')'特征值',J,':',ALPHAR(J),'+',ALPHAI(J),'i'
END IF
WRITE(*,*) '左特征向量:'
WRITE(*,'(A)') ' '
WRITE(*,'(3(F8.4,1X))') VL(:,J)
WRITE(*,*) '右特征向量:'
WRITE(*,'(A)') ' '
WRITE(*,'(3(F8.4,1X))') VR(:,J)
END DO
ELSE
WRITE(*,*)'求解广义特征值问题时出错,错误代码为',INFO
END IF
END PROGRAM GeneralizedEigenvalue