program eigenvalue_demo
implicit none
! 定义矩阵维度和工作数组
integer, parameter :: nm = 3, n = 3
real*16 :: a(nm, n), w(n), z(nm, n), fv1(n), fv2(n)
integer :: matz, ierr, i, j
! 定义一个 3x3 实对称矩阵
! 矩阵数据:
! [ 4.0, 2.0, 1.0 ]
! [ 2.0, 3.0, 0.5 ]
! [ 1.0, 0.5, 2.0 ]
a(1,1) = 4.0_16
a(1,2) = 2.0_16
a(1,3) = 1.0_16
a(2,1) = 2.0_16 ! 对称位置
a(2,2) = 3.0_16
a(2,3) = 0.5_16
a(3,1) = 1.0_16 ! 对称位置
a(3,2) = 0.5_16 ! 对称位置
a(3,3) = 2.0_16
! 设置 matz = 1 表示计算特征值和特征向量
matz = 1
! 打印原始矩阵
print *, "实对称矩阵 A:"
do i = 1, n
print '(3F10.6)', (a(i,j), j = 1, n)
end do
print *, ""
! 调用特征值求解子程序
call rs(nm, n, a, w, matz, z, fv1, fv2, ierr)
! 检查错误代码
if (ierr /= 0) then
print *, "计算失败! 错误代码 ierr =", ierr
if (ierr == 10*n) print *, "输入错误: n > nm, 矩阵维度不匹配"
stop
end if
! 打印特征值
print *, "特征值 (升序排列):"
print '(3F10.6)', w
print *, ""
! 打印特征向量
print *, "对应的特征向量 (列向量):"
do i = 1, n
print '(A, I1, A, 3F10.6)', "特征向量 ", i, ": ", (z(i,j), j = 1, n)
end do
end program eigenvalue_demo
!*********************************************************************
subroutine rs(nm,n,a,w,matz,z,fv1,fv2,ierr)
!
implicit none
integer n,nm,ierr,matz
real*16 a(nm,n),w(n),z(nm,n),fv1(n),fv2(n)
!
! this subroutine calls the recommended sequence of
! subroutines from the eigensystem subroutine package (eispack)
! to find the eigenvalues and eigenvectors (if desired)
! of a real symmetric matrix.
!
! on input
!
! nm must be set to the row dimension of the two-dimensional
! array parameters as declared in the calling program
! dimension statement.
!
! n is the order of the matrix a.
!
! a contains the real symmetric matrix.
!
! matz is an integer variable set equal to zero if
! only eigenvalues are desired. otherwise it is set to
! any non-zero integer for both eigenvalues and eigenvectors.
!
! on output
!
! w contains the eigenvalues in ascending order.
!
! z contains the eigenvectors if matz is not zero.
!
! ierr is an integer output variable set equal to an error
! completion code described in the documentation for tqlrat
! and tql2. the normal completion code is zero.
!
! fv1 and fv2 are temporary storage arrays.
!
! questions and comments should be directed to burton s. garbow,
! mathematics and computer science div, argonne national laboratory
!
! this version dated august 1983.
!
! ------------------------------------------------------------------
!
if (n .le. nm) go to 10
ierr = 10 * n
go to 50
!
10 if (matz .ne. 0) go to 20
! .......... find eigenvalues only ..........
call tred1(nm,n,a,w,fv1,fv2)
! tqlrat encounters catastrophic underflow on the Vax
! call tqlrat(n,w,fv2,ierr)
call tql1(n,w,fv1,ierr)
go to 50
! .......... find both eigenvalues and eigenvectors ..........
20 call tred2(nm,n,a,w,fv1,z)
call tql2(nm,n,w,fv1,z,ierr)
50 return
end subroutine rs
报错:PS D:\win_fortran> cd "d:\win_fortran\" ; if ($?) { gfortran ceshiyi.f90 -o ceshiyi } ; if ($?) { .\ceshiyi }
D:/programs/mingw/bin/../lib/gcc/x86_64-w64-mingw32/15.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\13204\AppData\Local\Temp\ccbq7jZo.o:ceshiyi.f90:(.text+0xc9): undefined reference to `tred1_'
D:/programs/mingw/bin/../lib/gcc/x86_64-w64-mingw32/15.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\13204\AppData\Local\Temp\ccbq7jZo.o:ceshiyi.f90:(.text+0xe7): undefined reference to `tql1_'
D:/programs/mingw/bin/../lib/gcc/x86_64-w64-mingw32/15.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\13204\AppData\Local\Temp\ccbq7jZo.o:ceshiyi.f90:(.text+0x114): undefined reference to `tred2_'
D:/programs/mingw/bin/../lib/gcc/x86_64-w64-mingw32/15.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\13204\AppData\Local\Temp\ccbq7jZo.o:ceshiyi.f90:(.text+0x13e): undefined reference to `tql2_'
collect2.exe: error: ld returned 1 exit status