-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.F90
433 lines (406 loc) · 9.73 KB
/
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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
!--------------------------------------------------
!
! matrix.F90
! module: matrix_mod
! requirement: none
!
! created by Kun Fang
!
! This module contains routines for sparse matrix
! creation and calculation. There are two versions
! of sparse matrix: real version and complex version.
! The other part of this program is setup for complex
! version, so it is recommended to always use the
! complex version.
!
! The sparse matrix is stored by rows and only non-zero
! terms are stored. Each non-zero term is stored in a
! node including its value and column index. Each row is
! stored as a linked-list of these nodes. The matrix is
! represented as an array of these linked-lists.
!
! types:
! real_matrix
! |- real_spot
!
! complex_matrix
! |- complex_spot
!
!------------------------------------------------------
module matrix_mod
implicit none
complex(8),private,parameter::Zero=(0.0,0.0),One=(1.0,0.0),Xi=(0.0,1.0)
! real matrix element node
type real_spot
real(8)::val=0.00 ! element value
integer::index=0 ! column index
type(real_spot),pointer::next=>NULL()
end type
! complex matrix element node
type complex_spot
complex(8)::val=0.00 ! element value
integer::index=0 ! column index
type(complex_spot),pointer::next=>NULL()
end type
! real matrix
type real_matrix
integer::dim=0
type(real_spot),pointer::head(:)=>NULL()
end type real_matrix
! comple matrix
type complex_matrix
integer::dim=0
type(complex_spot),pointer::head(:)=>NULL()
end type complex_matrix
public::real_matrix_init,real_matrix_del,real_matrix_show,real_matrix_set,real_matrix_product
public::complex_matrix_init,complex_matrix_del,complex_matrix_show,complex_matrix_set,complex_matrix_product
contains
!--------------- Sparse Matrix real version ---------------
!
! It is stored by rows
!
!----------------------------------------------------------
! initialize the matrix
function real_matrix_init(n) result(M)
type(real_matrix),pointer::M
integer::n,i
allocate(M)
M%dim=n
allocate(M%head(n))
do i=1,n
M%head(i)%val=0.0
M%head(i)%index=i
M%head(i)%next=>NULL()
end do
end function
! delete the matrix from the memory
subroutine real_matrix_del(M)
type(real_matrix),pointer::M
type(real_spot),pointer::a,b
integer::i
do i=1,M%dim
a=>M%head(i)%next
do
if(.not.associated(a)) exit
b=>a
a=>a%next
deallocate(b)
end do
end do
deallocate(M%head)
M%head=>NULL()
M%dim=0
deallocate(M);
end subroutine
! print all the non-zero terms of the matrix
subroutine real_matrix_print(M)
type(real_matrix),pointer::M
integer::n,s,i,j
type(real_spot),pointer::a
n=M%dim
do i=1,n
print *,M%head(i)%index,M%head(i)%index,M%head(i)%val
a=>M%head(i)%next
do
if(.not.associated(a)) exit
print *,i,a%index,a%val
a=>a%next
end do
end do
end subroutine
! return the value of matrix element M(i,j)
function real_matrix_show(M,i,j) result(x)
type(real_matrix),pointer::M
type(real_spot),pointer::a
integer::i,j,k
real(8)::x
x=0.0d0
if(i>M%dim.or.j>M%dim) return
if(i==j) then
x=M%head(i)%val
return
end if
a=>M%head(i)%next
do
if(.not.associated(a)) exit
if(a%index==j) then
x=a%val
return
end if
if(a%index>j) return
a=>a%next
end do
end function
! calculate y=M*x, where M is a matrix, x and y both are
! column vector
!
! Note: the subroutine assume that the row dimension of the
! matrix is the same as the length of the two vectors.
! The subroutine doesn't check the validity of the
! matrix production
subroutine real_matrix_product(M,x,y)
type(real_matrix),pointer::M
type(real_spot),pointer::a
real(8)::x(:),y(:)
integer::i,j,k
do i=1,M%dim
y(i)=M%head(i)%val*x(i)
a=>M%head(i)%next
do
if(.not.associated(a)) exit
y(i)=y(i)+a%val*x(a%index)
a=>a%next
end do
end do
end subroutine
! assign a value to matrix element M(i,j)
subroutine real_matrix_set(M,i,j,x)
type(real_matrix),pointer::M
integer::i,j
real(8)::x
type(real_spot),pointer::a,b,new
if(i==j) then
M%head(i)%val=x
return
end if
b=>M%head(i)
a=>M%head(i)%next
do
if(.not.associated(a)) exit
if(a%index==j) then
if(abs(x)<1.d-8) then
b%next=>a%next
deallocate(a)
else
a%val=x
end if
return
end if
if(a%index>j) exit
b=>a
a=>a%next
end do
if(abs(x)<1.d-8) return
allocate(new)
new%index=j
new%val=x
if(associated(a)) then
b%next=>new
new%next=>a
else
b%next=>new
end if
end subroutine
! convert a sparse matrix to an 2D array
subroutine real_matrix_convert(M,A)
type(real_matrix),pointer::M
type(real_spot),pointer::p
real(8)::A(:,:)
integer::i,j,k,n
n=size(A(1,:))
A(1:n,1:n)=0.d0
do i=1,n
p=>M%head(i)
A(i,i)=p%val
do
p=>p%next
if(.not.associated(p)) exit
j=p%index
A(i,j)=p%val
end do
end do
end subroutine
! convert an 2D array to a sparse matrix
subroutine real_matrix_reverse(A,M)
type(real_matrix),pointer::M
type(real_spot),pointer::p,new
real(8)::A(:,:)
integer::i,j,k,n
n=size(A(1,:))
M=>real_matrix_init(n)
do i=1,n
p=>M%head(i)
p%val=A(i,i)
do j=1,n
if(abs(A(i,j))<1.d-6) cycle
allocate(new)
new%index=j
new%val=A(i,j)
end do
end do
end subroutine
!--------------- Sparse Matrix complex version ---------------
!
! It is stored in rows
!
!-------------------------------------------------------------
! initialize the matrix
function complex_matrix_init(n) result(M)
type(complex_matrix),pointer::M
integer::n,i
allocate(M)
M%dim=n
allocate(M%head(n))
do i=1,n
M%head(i)%val=Zero
M%head(i)%index=i
M%head(i)%next=>NULL()
end do
end function
! delete the matrix from the memory
subroutine complex_matrix_del(M)
type(complex_matrix),pointer::M
type(complex_spot),pointer::a,b
integer::i
do i=1,M%dim
a=>M%head(i)%next
do
if(.not.associated(a)) exit
b=>a
a=>a%next
deallocate(b)
end do
end do
deallocate(M%head)
M%head=>NULL()
M%dim=0
deallocate(M);
end subroutine
! print all the non-zero terms of the matrix
subroutine complex_matrix_print(M)
type(complex_matrix),pointer::M
integer::n,s,i,j
type(complex_spot),pointer::a
n=M%dim
do i=1,n
print *,M%head(i)%index,M%head(i)%index,M%head(i)%val
a=>M%head(i)%next
do
if(.not.associated(a)) exit
print *,i,a%index,a%val
a=>a%next
end do
end do
end subroutine
! return the value of matrix element M(i,j)
function complex_matrix_show(M,i,j) result(x)
type(complex_matrix),pointer::M
type(complex_spot),pointer::a
integer::i,j,k
complex(8)::x
x=Zero
if(i>M%dim.or.j>M%dim) return
if(i==j) then
x=M%head(i)%val
return
end if
a=>M%head(i)%next
do
if(.not.associated(a)) exit
if(a%index==j) then
x=a%val
return
end if
if(a%index>j) return
a=>a%next
end do
end function
! calculate y=M*x, where M is a matrix, x and y both are
! column vector
!
! Note: the subroutine assume that the row dimension of the
! matrix is the same as the length of the two vectors.
! The subroutine doesn't check the validity of the
! matrix production
subroutine complex_matrix_product(M,x,y)
type(complex_matrix),pointer::M
type(complex_spot),pointer::a
complex(8)::x(:),y(:)
integer::i,j,k
do i=1,M%dim
y(i)=M%head(i)%val*x(i)
a=>M%head(i)%next
do
if(.not.associated(a)) exit
y(i)=y(i)+a%val*x(a%index)
a=>a%next
end do
end do
end subroutine
! assign a value to matrix element M(i,j)
subroutine complex_matrix_set(M,i,j,x)
type(complex_matrix),pointer::M
integer::i,j
complex(8)::x
type(complex_spot),pointer::a,b,new
if(i==j) then
M%head(i)%val=x
return
end if
b=>M%head(i)
a=>M%head(i)%next
do
if(.not.associated(a)) exit
if(a%index==j) then
if(abs(x)<1.d-8) then
b%next=>a%next
deallocate(a)
else
a%val=x
end if
return
end if
if(a%index>j) exit
b=>a
a=>a%next
end do
if(abs(x)<1.d-8) return
allocate(new)
new%index=j
new%val=x
if(associated(a)) then
b%next=>new
new%next=>a
else
b%next=>new
end if
end subroutine
! convert a sparse matrix to an 2D array
subroutine complex_matrix_convert(M,A)
type(complex_matrix),pointer::M
type(complex_spot),pointer::p
complex(8)::A(:,:)
integer::i,j,k,n
n=size(A(1,:))
A(1:n,1:n)=Zero
do i=1,n
p=>M%head(i)
A(i,i)=p%val
do
p=>p%next
if(.not.associated(p)) exit
j=p%index
A(i,j)=p%val
end do
end do
end subroutine
! convert an 2D array to a sparse matrix
subroutine complex_matrix_reverse(A,M)
type(complex_matrix),pointer::M
type(complex_spot),pointer::p,new
complex(8)::A(:,:)
integer::i,j,k,n
n=size(A(1,:))
M=>complex_matrix_init(n)
do i=1,n
p=>M%head(i)
p%val=A(i,i)
do j=1,n
if(abs(A(i,j))<1.d-6) cycle
allocate(new)
new%index=j
new%val=A(i,j)
end do
end do
end subroutine
end module matrix_mod