-
Notifications
You must be signed in to change notification settings - Fork 0
/
linalg.f90
395 lines (383 loc) · 9.98 KB
/
linalg.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
!> \brief Performs matrix multiplication
!> This function is a custom version of a LAPACK routine
!> which multiplies two matrices. To summarize,
!> \c C := \c alpha \c * \c A*B + \c beta \c * \c C
!> \param tA Transposition of \c A
!> \param tB Transposition of \c B
!> \param n Number rows in A
!> \param k Number of columns in \c A and rows in \c B
!> \param m Number of columns in \c B
!> \param alpha Multiplier on product \c AB
!> \param A Pointer to the column major matrix \c A
!> \param lda The leading dimension of \c A
!> \param B Pointer to the column major matrix \c B
!> \param ldb The leading dimension of \c B
!> \param beta Multiplier on additive \c C
!> \param C Pointer to the column major matrix \c C
!> \param ldc The leading dimension of \c C
subroutine dgemm(tA,tB,n,k,m,alpha,A,lda,B,ldb,beta,C,ldc)
implicit none
integer :: n,k,m,lda,ldb,ldc,i,j,p
real*8, dimension(lda,n) :: A
real*8, dimension(ldb,k) :: B
real*8, dimension(ldc,n) :: C
real*8 alpha,beta
character :: tA,tB
if ( tA == 't' .or. tA == 'T' ) then
if( tB == 't' .or. tB == 'T' ) then
do i = 1,n
do j = 1,m
C(i,j) = beta * C(i,j)
do p = 1,k
C(i,j) = C(i,j) + alpha * A(p,i) * B(j,p)
enddo
enddo
enddo
else
do i = 1,n
do j = 1,m
C(i,j) = beta * C(i,j)
do p = 1,k
C(i,j) = C(i,j) + alpha * A(p,i) * B(p,j)
enddo
enddo
enddo
endif
else
if( tB == 't' .or. tB == 'T' ) then
do i = 1,n
do j = 1,m
C(i,j) = beta * C(i,j)
do p = 1,k
C(i,j) = C(i,j) + alpha * A(i,p) * B(j,p)
enddo
enddo
enddo
else
do i = 1,n
do j = 1,m
C(i,j) = beta * C(i,j)
do p = 1,k
C(i,j) = C(i,j) + alpha * A(i,p) * B(p,j)
enddo
enddo
enddo
endif
endif
end subroutine
subroutine dgemv(tA,n,m,alpha,A,lda,x,ldx,beta,y,ldy)
implicit none
integer :: i,k,n,m,lda,ldx,ldy
real*8, dimension(lda,m) :: A
real*8, dimension(ldx,m) :: x
real*8, dimension(ldy,n) :: y
real*8 :: alpha,beta
character :: tA
if( tA == 't' .or. tA == 'T' ) then
do i = 1,n
do k = 1,m
y(1,i) = y(1,i) + A(i,k) * x(1,k)
enddo
enddo
else
do i = 1,n
do k = 1,m
y(1,i) = y(1,i) + A(k,i) * x(1,k)
enddo
enddo
endif
end subroutine
subroutine daxpy(n,x,ldx,y,ldy,ac)
implicit none
integer :: n,ldx,ldy,i
real*8, dimension(ldx,n) :: x ! Ensure that each entry of x is ldx quadwords apart
real*8, dimension(ldy,n) :: y ! Ensure that each entry of y is ldy quadwords apart
real*8 :: ac
ac = 0.0
do i = 1,n
ac = ac + x(1,i) * y(1,i)
enddo
end subroutine
subroutine dlacpy(uplo,n,m,A,lda,B,ldb)
integer :: i,j,n,m,lda,ldb
real*8, dimension(lda,m) :: A
real*8, dimension(ldb,m) :: B
character :: uplo
if( uplo == 'u' .or. uplo == 'U' ) then
do i = 1,m
do j = 1,min(i,n)
B(j,i) = A(j,i)
enddo
enddo
else
if ( uplo == 'l' .or. uplo == 'L' ) then
do i = 1,n
do j = 1,min(i,m)
B(i,j) = A(i,j)
enddo
enddo
else
do i = 1,n
do j = 1,m
B(i,j) = A(i,j)
enddo
enddo
endif
endif
end subroutine
!> \brief Determines the permutation of rows/columns required for all nonzero
!> diagonals
!> This function determines the permutation matrix, \c P , such
!> that \c PA has no zeroes on the diagonal. The variable \c ipiv
!> is a one-dimensional vector mapping row/column numbers to their
!> new locations. This is primarily used internally to deal with
!> the LU factorization which requires that all diagonal elements
!> be nonzero.
!> \param n The dimension of the matrix \c A
!> \param A The square matrix of interest
!> \param lda The leading dimension of \c A
!> \param ipiv The pointer to the length \c n vector to store the permutatiion
!> \param res The result of the operation; returns 0 if all is well
subroutine dgepiv(n,A,lda,ipiv,res)
implicit none
integer :: n,lda,res,i,j,found,tmp
integer, dimension(n) :: ipiv
real*8, dimension(lda,n) :: A
real*8, parameter :: zlim = 1.0e-6
res = 0
do i = 1,n ! Initialize all rows to their current locations
ipiv(i) = i
enddo
do i = 1,n
if( abs(A(ipiv(i),i)) < zlim ) then
found = 0
do j = 1,n
if( ipiv(j) == ipiv(i) ) then
continue
else
if( abs(A(ipiv(i),j)) > zlim .and. abs(A(ipiv(j),i)) > zlim ) then
tmp = ipiv(i)
ipiv(i) = ipiv(j)
ipiv(j) = tmp
found = 1
exit
endif
endif
enddo
if( found == 0 ) then ! If there is even one zero which cannot be eliminated then matrix is rank deficient
res = -1
exit
else
res = res + 1 ! Count the number of zeros originally on the diagonal
endif
endif
enddo
end subroutine
subroutine dgelu(perm,n,m,A,lda,ipiv,res)
implicit none
integer :: n,m,lda,res,i,j,k
integer, dimension(n) :: ipiv
real*8, dimension(lda,n) :: A
real*8 :: tau
character :: perm
if( perm == 'n' .or. perm == 'N' ) then ! perm = 'n' or 'N' indicates DO NOT calculate the row permutation
do i = 1,min(n,m) ! matrix, P, and also do not use the input ipiv; ipiv can just be NULL
do j = i+1,n
tau = A(j,i) / A(i,i)
do k = i+1,m
A(j,k) = A(j,k) - tau * A(i,k)
enddo
A(j,i) = tau
enddo
enddo
else
if( perm == 'f' .or. perm == 'F' ) then ! Call dgepiv to calculate the pivots and put it in ipiv as output
call dgepiv(n,A,lda,ipiv,res) ! This function deals with square matrices, but it works for rectangular as well (I hope)
if( res < 0 ) then ! This would indicate that the pivot matrix calculation failed
return
endif
do i = 1,min(n,m)
do j = i+1,n
tau = A(ipiv(j),i) / A(ipiv(i),i)
do k = i+1,m
A(ipiv(j),k) = A(ipiv(j),k) - tau * A(ipiv(i),k)
enddo
A(ipiv(j),i) = tau
enddo
enddo
else
if( perm == 'p' .or. perm == 'P' ) then ! Use the ipiv pivots given as input
do i = 1,min(n,m)
do j = i+1,n
tau = A(ipiv(j),i) / A(ipiv(i),i)
do k = i+1,m
A(ipiv(j),k) = A(ipiv(j),k) - tau * A(ipiv(i),k)
enddo
A(ipiv(j),i) = tau
enddo
enddo
endif
endif
endif
end subroutine
subroutine dgefb(perm,n,nrhs,A,lda,ipiv,B,ldb,res)
implicit none
integer :: n,nrhs,lda,ldb,res,i,j,k
integer, dimension(n) :: ipiv
real*8, dimension(lda,n) :: A
real*8, dimension(ldb,nrhs) :: B
character :: perm
if( perm == 'n' .or. perm == 'N' ) then
do k = 1,nrhs
do i = 2,n ! Forward substitution
do j = 1,i-1
B(i,k) = B(i,k) - A(i,j) * B(j,k)
enddo
enddo
do i = n,1,-1 ! Backward substitution
do j = n,i+1,-1
B(i,k) = B(i,k) - A(i,j) * B(j,k)
enddo
B(i,k) = B(i,k) / A(i,i)
enddo
enddo
res = 0
else
if( perm == 'f' .or. perm == 'F' ) then
call dgepiv(n,A,lda,ipiv,res)
if( res >= 0 ) then
do k = 1,nrhs
do i = 2,n ! Forward substitution
do j = 1,i-1
B(ipiv(i),k) = B(ipiv(i),k) - A(ipiv(i),j) * B(ipiv(j),k)
enddo
enddo
do i = n,1,-1 ! Backward substitution
do j = n,i+1,-1
B(ipiv(i),k) = B(ipiv(i),k) - A(ipiv(i),j) * B(ipiv(j),k)
enddo
B(ipiv(i),k) = B(ipiv(i),k) / A(ipiv(i),i)
enddo
enddo
endif
res = 0
else
if( perm == 'p' .or. perm == 'P' ) then
do k = 1,nrhs
do i = 2,n ! Forward substitution
do j = 1,i-1
B(ipiv(i),k) = B(ipiv(i),k) - A(ipiv(i),j) * B(ipiv(j),k)
enddo
enddo
do i = n,1,-1 ! Backward substitution
do j = n,i+1,-1
B(ipiv(i),k) = B(ipiv(i),k) - A(ipiv(i),j) * B(ipiv(j),k)
enddo
B(ipiv(i),k) = B(ipiv(i),k) / A(ipiv(i),i)
enddo
enddo
res = 0
endif
endif
endif
end subroutine
subroutine dgedet(perm,n,A,lda,ipiv,det,res)
implicit none
integer :: n,lda,res,i,j,tmp
integer, dimension(n) :: ipiv
real*8, dimension(lda,n) :: A
real*8 :: det
character :: perm
if( n > 0 ) then
if( n == 1 ) then
det = A(1,1)
res = 0
else
res = 0
if( perm == 'f' .or. perm == 'F' ) then
call dgepiv(n,A,lda,ipiv,res)
endif
if( res >= 0 ) then
call dgelu(perm,n,n,A,lda,ipiv,res)
if( res >= 0 ) then
det = 1.0
if( perm .ne. 'n' .and. perm .ne. 'N' ) then
do i = 1,n
det = det * A(ipiv(i),i)
enddo
tmp = 0
do i = 1,n
do j = i+1,n
if( ipiv(i) > ipiv(j) ) then
tmp = tmp + 1
endif
enddo
enddo
tmp = mod(tmp,2)
if( tmp == 1 ) then
det = -1.0 * det
endif
else
do i = 1,n
det = det * A(i,i)
enddo
endif
endif
endif
endif
else
res = -1
endif
end subroutine
!> \brief Solves the linear system \c AX = \c B, overwriting \c B
!> This function is analogous to the LAPACK routine
!> by the same name. It solves the system \c AX \c = \c B
!> using LU factorization. The variable \c ipiv is space
!> required to calculate the necessary permutation of rows
!> and columns to assure that no diagonal entries are zero.
!> The solution is stored in \c B, overwriting the original
!> input.
!> \param n The number of rows (and columns) in \c A
!> \param nrhs The number of columns in \c B
!> \param A The matrix to invert
!> \param lda The leading dimension of \c A
!> \param ipiv The permutation vector
!> \param B The righthand side of the system
!> \param ldb The leading dimension of \c B
!> \param res The result code; returns 0 for all okay
subroutine dgesv(n,nrhs,A,lda,ipiv,B,ldb,res)
implicit none
integer :: n,nrhs,lda,ldb,ldx,res,k,i
integer, dimension(n) :: ipiv
real*8, dimension(n) :: temp
real*8, dimension(lda,n) :: A
real*8, dimension(ldb,nrhs) :: B
call dgelu('f',n,n,A,lda,ipiv,res)
if( res >= 0 ) then
call dgefb('p',n,nrhs,A,lda,ipiv,B,ldb,res)
if( res >= 0 ) then
do k = 1,nrhs ! Putting the values into the right places in each row
do i = 1,n ! Row i is physically located at row ipiv(i) in memory
temp(i) = B(i,k)
enddo
do i = 1,n
B(i,k) = temp(ipiv(i))
enddo
enddo
endif
endif
end subroutine
subroutine matrix_print(n,m,A)
implicit none
real*8, dimension(n,m) :: A
integer :: n,m
real*8 x
integer i,j
do i = 1,n
do j = 1,m
x = A(i,j)
write (*,"(2f11.7)",advance='no') x
enddo
print *
enddo
end subroutine