Files
2025-09-28 16:00:09 +08:00

25 lines
746 B
Fortran
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
program invert_simple
implicit none
integer, parameter :: n = 2
double precision :: a(n,n)
integer :: ipiv(n)
double precision :: work(n) ! 最小工作空间LWORK = N
integer :: info
! 初始化一个简单的可逆矩阵: [[2, 1], [1, 2]]
a(1,1) = 2.0d0; a(1,2) = 1.0d0
a(2,1) = 1.0d0; a(2,2) = 2.0d0
! LU 分解
call DGETRF(n, n, a, n, ipiv, info)
if (info /= 0) stop 'DGETRF failed'
! 求逆(使用最小工作空间 LWORK = N
call DGETRI(n, a, n, ipiv, work, n, info)
if (info /= 0) stop 'DGETRI failed'
! 输出结果(应为 [[0.6667, -0.3333], [-0.3333, 0.6667]]
print *, 'Inverse:'
print *, a(1,1), a(1,2)
print *, a(2,1), a(2,2)
end program