-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDeterminant_matrix.f90
84 lines (73 loc) · 2.2 KB
/
Determinant_matrix.f90
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
! Determinant_matrix.f90
! From http://rosettacode.org/wiki/Rosetta_Code
!****************************************************************************
!
! PROGRAM: Determinant_matrix
!
! PURPOSE: just see the name of the source code.
!
! REFERENCE: https://rosettacode.org/wiki/Matrix_arithmetic#Fortran
!
!****************************************************************************
!-*- mode: compilation; default-directory: "/tmp/" -*-
!Compilation started at Sat May 18 23:25:42
!
!a=./F && make $a && $a < unixdict.txt
!f95 -Wall -ffree-form F.F -o F
! j example, determinant: 7.00000000
! j example, permanent: 5.00000000
! maxima, determinant: -360.000000
! maxima, permanent: 900.000000
!
!Compilation finished at Sat May 18 23:25:43
! NB. example computed by J
! NB. fixed seed random matrix
! _2+3 3?.@$5
! 2 _1 1
!_1 _2 1
!_1 _1 _1
!
! (-/ .*)_2+3 3?.@$5 NB. determinant
!7
! (+/ .*)_2+3 3?.@$5 NB. permanent
!5
!maxima example
!a: matrix([2, 9, 4], [7, 5, 3], [6, 1, 8])$
!determinant(a);
!-360
!
!permanent(a);
!900
! compute permanent or determinant
program f
implicit none
real, dimension(3,3) :: j, m
data j/ 2,-1, 1,-1,-2, 1,-1,-1,-1/
data m/2, 9, 4, 7, 5, 3, 6, 1, 8/
write(6,*) 'j example, determinant: ',det(j,3,-1)
write(6,*) 'j example, permanent: ',det(j,3,1)
write(6,*) 'maxima, determinant: ',det(m,3,-1)
write(6,*) 'maxima, permanent: ',det(m,3,1)
contains
recursive function det(a,n,permanent) result(accumulation)
! setting permanent to 1 computes the permanent.
! setting permanent to -1 computes the determinant.
real, dimension(n,n), intent(in) :: a
integer, intent(in) :: n, permanent
real, dimension(n-1, n-1) :: b
real :: accumulation
integer :: i, sgn
if (n .eq. 1) then
accumulation = a(1,1)
else
accumulation = 0
sgn = 1
do i=1, n
b(:, :(i-1)) = a(2:, :i-1)
b(:, i:) = a(2:, i+1:)
accumulation = accumulation + sgn * a(1, i) * det(b, n-1, permanent)
sgn = sgn * permanent
enddo
endif
end function det
end program f