-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinAlgebra_module.f95
221 lines (183 loc) · 8.2 KB
/
linAlgebra_module.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
MODULE linAlgebra
!--------1!--------2!--------3!--------4!--------5!--------6!--------7!--------8
USE basics
IMPLICIT NONE
! This module contains the REAL(dp) version of the code posted at:
! http://fortranwiki.org/fortran/show/Matrix+inversion
CONTAINS
PURE FUNCTION matinv2(A) RESULT(B)
!! Performs a direct calculation of the inverse of a 2×2 matrix.
REAL(dp), INTENT(IN) :: A(2,2) !! Matrix
REAL(dp) :: B(2,2) !! Inverse matrix
REAL(dp) :: detinv
! Calculate the inverse determinant of the matrix
detinv = 1.0D0/(A(1,1)*A(2,2) - A(1,2)*A(2,1))
! Calculate the inverse of the matrix
B(1,1) = +detinv * A(2,2)
B(2,1) = -detinv * A(2,1)
B(1,2) = -detinv * A(1,2)
B(2,2) = +detinv * A(1,1)
END FUNCTION
PURE FUNCTION matinv3(A) RESULT(B)
!! Performs a direct calculation of the inverse of a 3×3 matrix.
REAL(dp), INTENT(IN) :: A(3,3) !! Matrix
REAL(dp) :: B(3,3) !! Inverse matrix
REAL(dp) :: detinv
! Calculate the inverse determinant of the matrix
detinv = 1.0D0/(A(1,1)*A(2,2)*A(3,3) - A(1,1)*A(2,3)*A(3,2)&
- A(1,2)*A(2,1)*A(3,3) + A(1,2)*A(2,3)*A(3,1)&
+ A(1,3)*A(2,1)*A(3,2) - A(1,3)*A(2,2)*A(3,1))
! Calculate the inverse of the matrix
B(1,1) = +detinv * (A(2,2)*A(3,3) - A(2,3)*A(3,2))
B(2,1) = -detinv * (A(2,1)*A(3,3) - A(2,3)*A(3,1))
B(3,1) = +detinv * (A(2,1)*A(3,2) - A(2,2)*A(3,1))
B(1,2) = -detinv * (A(1,2)*A(3,3) - A(1,3)*A(3,2))
B(2,2) = +detinv * (A(1,1)*A(3,3) - A(1,3)*A(3,1))
B(3,2) = -detinv * (A(1,1)*A(3,2) - A(1,2)*A(3,1))
B(1,3) = +detinv * (A(1,2)*A(2,3) - A(1,3)*A(2,2))
B(2,3) = -detinv * (A(1,1)*A(2,3) - A(1,3)*A(2,1))
B(3,3) = +detinv * (A(1,1)*A(2,2) - A(1,2)*A(2,1))
END FUNCTION
PURE FUNCTION matinv4(A) RESULT(B)
!! Performs a direct calculation of the inverse of a 4×4 matrix.
REAL(dp), INTENT(IN) :: A(4,4) !! Matrix
REAL(dp) :: B(4,4) !! Inverse matrix
REAL(dp) :: detinv
! Calculate the inverse determinant of the matrix
detinv = &
1.0D0/(A(1,1)*(A(2,2)*(A(3,3)*A(4,4)-A(3,4)*A(4,3))+A(2,3)*(A(3,4)*A(4,2)-A(3,2)*A(4,4))+A(2,4)*(A(3,2)*A(4,3)-A(3,3)*A(4,2)))&
- A(1,2)*(A(2,1)*(A(3,3)*A(4,4)-A(3,4)*A(4,3))+A(2,3)*(A(3,4)*A(4,1)-A(3,1)*A(4,4))+A(2,4)*(A(3,1)*A(4,3)-A(3,3)*A(4,1)))&
+ A(1,3)*(A(2,1)*(A(3,2)*A(4,4)-A(3,4)*A(4,2))+A(2,2)*(A(3,4)*A(4,1)-A(3,1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)*(A(2,3)*A(3,1)-A(2,1)*A(3,3))+A(1,3)*(A(2,1)*A(3,2)-A(2,2)*A(3,1)))
END FUNCTION
!--------1!--------2!--------3!--------4!--------5!--------6!--------7!--------8
SUBROUTINE matinv(n,matIn,matOut)
INTEGER(regInt), INTENT(IN) :: n !matrix dimension
REAL(dp), INTENT(IN) :: matIN(n,n) !input matrix to be inverted
REAL(dp), INTENT(OUT) :: matOut(n,n) !the inverse of the above
SELECT CASE(n)
CASE (1)
matOut=1.0D0/matIn
CASE (2)
matOut=matinv2(matIn)
CASE (3)
matOut=matinv3(matIn)
CASE (4)
matOut=matinv4(matIn)
CASE DEFAULT
CALL matinvL(n,matIn,matOut)
END SELECT
END SUBROUTINE matinv
!--------1!--------2!--------3!--------4!--------5!--------6!--------7!--------8
SUBROUTINE matinvL(n,a,minverse)
!matrix inverse, used if n>=5
INTEGER(regInt), INTENT(IN) :: n
REAL(dp),INTENT(IN),DIMENSION(n,n) :: a
REAL(dp),INTENT(OUT),DIMENSION(n,n) :: minverse
!more variables
REAL(dp),DIMENSION(n,n) :: y,b
INTEGER(regInt),DIMENSION(n) :: indx
REAL(dp) :: d
INTEGER(regInt) :: i
y=0.0_dp
DO i=1,n
y(i,i)=1.0_dp
END DO
b=a
CALL ludcmp(b,indx,d)
do i=1,n
CALL lubksb(b,indx,y(:,i))
END DO
minverse=y
END SUBROUTINE matinvL
!--------1!--------2!--------3!--------4!--------5!--------6!--------7!--------8
SUBROUTINE ludcmp(a,indx,d)
! LU decomposition of matrix a
REAL(dp),DIMENSION(:,:),INTENT(INOUT) :: a
INTEGER(regInt),DIMENSION(:),INTENT(OUT):: indx
REAL(dp),INTENT(OUT) :: d
!additional variables
REAL(dp),DIMENSION(SIZE(a,1)) :: vv
REAL(dp),PARAMETER :: TINY=1.0e-20_dp
INTEGER(regInt):: j,n,imax
n=SIZE(indx)
d=1.0_dp
vv=MAXVAL(ABS(a),DIM=2)
IF (ANY(vv.LT.TINY)) THEN
d=-99.0_dp !error
RETURN
END IF
vv=1.0_dp/vv
DO j=1,n
imax=(j-1)+MAXLOC(vv(j:n)*ABS(a(j:n,j)), dim=1)
IF (j /=imax) THEN
CALL swap_rv(a(imax,:),a(j,:))
d=-d
vv(imax)=vv(j)
END IF
indx(j)=imax
if (a(j,j).EQ.0.0_dp) a(j,j)=TINY
a(j+1:n,j)=a(j+1:n,j)/a(j,j)
a(j+1:n,j+1:n)=a(j+1:n,j+1:n)-outerprod(a(j+1:n,j),a(j,j+1:n))
END DO
END SUBROUTINE ludcmp
!--------1!--------2!--------3!--------4!--------5!--------6!--------7!--------8
SUBROUTINE lubksb(a,indx,b)
!solving linear system A*x=b when A is a matrix after a LU decomposition
!the solution is returned into b
REAL(dp),DIMENSION(:,:),INTENT(IN) :: a
INTEGER(regInt),DIMENSION(:),INTENT(IN) :: indx
REAL(dp),DIMENSION(:),INTENT(INOUT) :: b
!additional variables
INTEGER(regInt):: i,n,ii,ll
REAL(dp) :: summ
n=SIZE(indx)
ii=0
do i=1,n
ll=indx(i)
summ=b(ll)
b(ll)=b(i)
if (ii /= 0)then
summ=summ-DOT_PRODUCT(a(i,ii:i-1),b(ii:i-1))
else if (summ /= 0.0_dp) THEN
ii=i
endif
b(i)=summ
END DO
DO i=n,1,-1
b(i)=(b(i)-DOT_PRODUCT(a(i,i+1:n),b(i+1:n)))/a(i,i)
END DO
END SUBROUTINE lubksb
!--------1!--------2!--------3!--------4!--------5!--------6!--------7!--------8
FUNCTION outerprod(a,b)
REAL(dp),DIMENSION(:),INTENT(IN) ::a,b
REAL(dp),DIMENSION(SIZE(a),SIZE(b)) ::outerprod
outerprod=SPREAD(a,DIM=2,ncopies=SIZE(b))*&
SPREAD(b,DIM=1,ncopies=SIZE(a))
END function outerprod
!--------1!--------2!--------3!--------4!--------5!--------6!--------7!--------8
SUBROUTINE swap_rv(a,b)
REAL(dp),DIMENSION(:),INTENT(INOUT) ::a,b
REAL(dp),DIMENSION(SIZE(a)) ::dum
dum=a
a=b
b=dum
END SUBROUTINE swap_rv
END MODULE linAlgebra