forked from hornos/lev00
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvers.f90
193 lines (186 loc) · 5.49 KB
/
invers.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
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
!===============================================================
! (1) INVERS of A | <==== this is what is here: TWO routines!!!
! (2) A*X=B |
!===============================================================
subroutine linear(A,B,INDX,VV,N,NP,Err)
!......................................................................
! Linear system of equations A*X=B is solved by the LU-decomposition.
! INPUT:
! A(NP,NP) - real matrix on the left
! B(N) - vector on the right
! NP - the physical dimension of A and B
! N - their actual dimension.
! OUTPUT:
! B(N) - vector of solution
!......................................................................
! The initial matrix A and vector B will be destroyed at the end.
!......................................................................
! Two working arrays are used for the calculation:
! INDX - an integer vector needed for the pivoting;
! VV - a real vector used to store the scaling.
!......................................................................
! Was taken from the book:
! "Numerical recipies. The art of scientific computing (Fortran
! version)." by W.H.Press et al (Cambridge Univ.Press, Cambridge,
! 1989), page 38.
!......................................................................
implicit none
real*8 A(NP,NP),VV(N),B(N),D
integer INDX(N),iErr,N,NP
logical Err
Err=.false.
!________ decompose the matrix A just once
call LUDCMPr(A,N,NP,INDX,D,VV,iErr)
if(iErr.eq.1) then
Err=.true.
return
end if
!________ solve the equation A*X=B.
call LUBKSBr(A,N,NP,INDX,B)
end subroutine linear
subroutine invers(A,Y,INDX,VV,N,NP,Err)
!......................................................................
! Invers Y(NP,NP) of the real matrix A(NP,NP)
!......................................................................
! NP - the physical dimension of the square matrices A and Y,
! N - their actual dimension.
!......................................................................
!..... The initial matrix A will be destroyed at the end...............
! Two working arrays are used for the calculation:
! INDX - an integer vector needed for the pivoting;
! VV - a real vector used to store the scaling.
!......................................................................
! Was taken from the book:
! "Numerical recipies. The art of scientific computing (Fortran
! version)." by W.H.Press et al (Cambridge Univ.Press, Cambridge,
! 1989), page 38.
!......................................................................
implicit none
integer N,NP,INDX(N),i,j,iErr
real*8 Y(NP,NP),A(NP,NP),VV(N),D
logical Err
Err=.false.
!________ set the identity matrix
do i=1,N
do j=1,N
Y(i,j)=0.0
end do
Y(i,i)=1.0
end do
!________ decompose the matrix A just once
call LUDCMPr(A,N,NP,INDX,D,VV,iErr)
if(iErr.eq.1) then
Err=.true.
return
end if
!________ find inverse by columns j of the Y matrix
do j=1,N
call LUBKSBr(A,N,NP,INDX,Y(1,j))
end do
end subroutine invers
subroutine ludcmpR(a,n,np,indx,d,vv,iErr)
implicit none
integer n,np
real*8, parameter :: tiny=1.0e-20
real*8 a(np,np),vv(n),aamax,sum,dum,d
integer indx(n),iErr,i,j,k,imax
iErr=0
d=1.
!
do i=1,n
aamax=0.
do j=1,n
if (abs(a(i,j)).gt.aamax) aamax=abs(a(i,j))
end do
if (aamax.eq.0.) then
write(*,*)' FATAL! Singular matrix!'
iErr=1
return
end if
vv(i)=1./aamax
end do
!
!.......... the principal loop over columns j=1,...,n
do j=1,n
!
if (j.gt.1) then
do i=1,j-1
if (i.gt.1)then
sum=a(i,j)
do k=1,i-1
sum=sum-a(i,k)*a(k,j)
end do
a(i,j)=sum
endif
end do
end if
!
aamax=0.
!
do i=j,n
sum=a(i,j)
if (j.gt.1)then
do k=1,j-1
sum=sum-a(i,k)*a(k,j)
end do
a(i,j)=sum
endif
dum=vv(i)*abs(sum)
if (dum.ge.aamax) then
imax=i
aamax=dum
endif
end do
!______________ interchanging of rows
if (j.ne.imax)then
do k=1,n
dum=a(imax,k)
a(imax,k)=a(j,k)
a(j,k)=dum
end do
d=-d
vv(imax)=vv(j)
endif
indx(j)=imax
!
if(j.ne.n)then
if(a(j,j).eq.0.) a(j,j)=tiny
dum=1./a(j,j)
do i=j+1,n
a(i,j)=a(i,j)*dum
end do
endif
end do
if(a(n,n).eq.0.) a(n,n)=tiny
end subroutine ludcmpR
subroutine lubksbR(a,n,np,indx,b)
implicit none
integer n,np,ii,i,ll,j
real*8 a(np,np),b(n),sum
integer indx(n)
ii=0
!....... forward substitution
do i=1,n
ll=indx(i)
sum=b(ll)
b(ll)=b(i)
if (ii.ne.0)then
do j=ii,i-1
sum=sum-a(i,j)*b(j)
end do
else if (sum.ne.0.) then
ii=i
end if
b(i)=sum
end do
!...... backsubstitution
do i=n,1,-1
sum=b(i)
if(i.lt.n)then
do j=i+1,n
sum=sum-a(i,j)*b(j)
end do
end if
b(i)=sum/a(i,i)
end do
end subroutine lubksbR