-
Notifications
You must be signed in to change notification settings - Fork 1
/
Determinant_Value.f95
63 lines (57 loc) · 1.76 KB
/
Determinant_Value.f95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
! 求解矩阵的行列式
! 将矩阵(方阵)转换为上三角或下三角形式
! 主对角线元素的积即为行列式的值
module LinearAlgebra
implicit none
public ShowMat
public Upper
public GetDet
contains
! 打印矩阵
subroutine ShowMat(mat)
implicit none
real, intent(in) :: mat(:, :)
integer :: i
do i=1, size(mat, 2)
write(*, "(3F8.2)") mat(:, i)
end do
return
end subroutine
! 转换为上三角矩阵
subroutine Upper(mat)
implicit none
real, intent(inout) :: mat(:, :)
integer :: i, j
do i=1, size(mat, 2)-1
do j=i+1, size(mat, 2)
mat(:, j) = mat(:, j) - mat(i, j) * mat(:, i) / mat(i, i)
end do
end do
return
end subroutine
! 对上三角矩阵进行对角线乘积
subroutine GetDet(mat, det)
implicit none
real, intent(inout) :: mat(:, :)
real, intent(out) :: det
integer :: i
call Upper(mat)
det = 1.0
do i=1, size(mat, 2)
det = det * mat(i, i)
end do
return
end subroutine
end module
program main
use LinearAlgebra
implicit none
real :: mat(3, 3) = (/ 1, 2, 1, 3, 2, 3, 2, 3, 4 /)
real :: det = 0.0
call ShowMat(mat)
call Upper(mat)
write(*, *) "-------------------"
call ShowMat(mat)
call GetDet(mat, det)
write(*, *) det
end program