cmatrix.F90
9.13 KB
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
!
! This file is part of SACAMOS, State of the Art CAble MOdels in Spice.
! It was developed by the University of Nottingham and the Netherlands Aerospace
! Centre (NLR) for ESA under contract number 4000112765/14/NL/HK.
!
! Copyright (C) 2016-2017 University of Nottingham
!
! SACAMOS is free software: you can redistribute it and/or modify it under the
! terms of the GNU General Public License as published by the Free Software
! Foundation, either version 3 of the License, or (at your option) any later
! version.
!
! SACAMOS is distributed in the hope that it will be useful, but
! WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
! or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
! for more details.
!
! A copy of the GNU General Public License version 3 can be found in the
! file GNU_GPL_v3 in the root or at <http://www.gnu.org/licenses/>.
!
! SACAMOS uses the EISPACK library (in /SRC/EISPACK). EISPACK is subject to
! the GNU Lesser General Public License. A copy of the GNU Lesser General Public
! License version can be found in the file GNU_LGPL in the root of EISPACK
! (/SRC/EISPACK ) or at <http://www.gnu.org/licenses/>.
!
! The University of Nottingham can be contacted at: ggiemr@nottingham.ac.uk
!
!
! SUBROUTINE write_cmatrix(Mat,dim,unit)
! SUBROUTINE write_cmatrix_re(Mat,dim,unit)
! SUBROUTINE write_cmatrix_im(Mat,dim,unit)
! SUBROUTINE cinvert_Gauss_Jordan(A,n,AI,dim)
! SUBROUTINE c_condition_number (A,n,condition_number,dim)
!
! NAME
! write_cmatrix
!
! DESCRIPTION
! write complex(dp) matrix to file or screen
!
!
! HISTORY
!
! started 2/12/15 CJS
!
! COMMENTS
!
SUBROUTINE write_cmatrix(Mat,dim,unit)
USE type_specifications
IMPLICIT NONE
! variables passed to subroutine
integer,intent(IN) :: dim ! matrix dimension
integer,intent(IN) :: unit ! unit to write to. Set to zero for screen output.
complex(dp),intent(IN) :: Mat(dim,dim) ! matrix to write
! local variables
integer row,col
! START
do row=1,dim
if (unit.EQ.0) then
write(*,*)(Mat(row,col),col=1,dim)
else
write(unit,*)(Mat(row,col),col=1,dim)
end if
end do
8000 format(1000ES16.6)
END SUBROUTINE write_cmatrix
!
! NAME
! write_cmatrix_re
!
! DESCRIPTION
! write the real part of a complex(dp) matrix to file or screen
!
! HISTORY
!
! started 2/12/15 CJS
!
! COMMENTS
!
SUBROUTINE write_cmatrix_re(Mat,dim,unit)
USE type_specifications
IMPLICIT NONE
! variables passed to subroutine
integer,intent(IN) :: dim ! matrix dimension
integer,intent(IN) :: unit ! unit to write to. Set to zero for screen output.
complex(dp),intent(IN) :: Mat(dim,dim) ! matrix to write
! local variables
integer row,col
! START
do row=1,dim
if (unit.EQ.0) then
write(*,*)(dble(Mat(row,col)),col=1,dim)
else
write(unit,*)(dble(Mat(row,col)),col=1,dim)
end if
end do
8000 format(20ES16.6)
END SUBROUTINE write_cmatrix_re
!
! NAME
! write_cmatrix_im
!
! DESCRIPTION
! write the real part of a complex(dp) matrix to file or screen
!
!
! HISTORY
!
! started 2/12/15 CJS
!
! COMMENTS
!
SUBROUTINE write_cmatrix_im(Mat,dim,unit)
USE type_specifications
IMPLICIT NONE
! variables passed to subroutine
integer,intent(IN) :: dim ! matrix dimension
integer,intent(IN) :: unit ! unit to write to. Set to zero for screen output.
complex(dp),intent(IN) :: Mat(dim,dim) ! matrix to write
! local variables
integer row,col
! START
do row=1,dim
if (unit.EQ.0) then
write(*,*)(AIMAG(Mat(row,col)),col=1,dim)
else
write(unit,*)(AIMAG(Mat(row,col)),col=1,dim)
end if
end do
8000 format(20ES16.6)
END SUBROUTINE write_cmatrix_im
!
! NAME
! cinvert_Gauss_Jordan
!
! DESCRIPTION
!
! Invert the complex matrix A using Gauss Jordan method with pivoting and return the result in AI
! ierr=0 on the successful calculation of the inverse
! if a singular matrix is found then
! if ierr.EQ.0 on input then the program stops
! if ierr.NE.0 on input then the program returns with ierr=1
!
! HISTORY
!
! started 2/12/15 CJS
!
! COMMENTS
!
SUBROUTINE cinvert_Gauss_Jordan(A,n,AI,dim,ierr)
USE type_specifications
USE general_module
IMPLICIT NONE
! variables passed to subroutine
integer,intent(IN) :: dim ! matrix dimension
integer,intent(IN) :: n ! size of matrix to invert
complex(dp),intent(IN) :: A(dim,dim) ! matrix to invert
complex(dp),intent(OUT) :: AI(dim,dim) ! inverse matrix
integer,intent(INOUT) :: ierr ! error code
! local variables
integer :: row,col,reduce_col,i
real(dp) :: max_element
complex(dp) :: pivot_element
integer :: max_row
integer :: pivot_row
integer :: pivot_row_save(dim)
complex(dp) :: row_multiplier
complex(dp) :: swap
! START
! copy A to AI
AI(1:n,1:n)= A(1:n,1:n)
pivot_row_save(1:dim)=0
! loop over columns of the matrix and reduce each column in turn to identity matrix column
do reduce_col=1,n
! find the largest element in this column and use as the pivot element
max_element=0d0
max_row=0
do row=reduce_col,n
if (abs(AI(row,reduce_col)).GT.max_element) then
max_element=abs(AI(row,reduce_col))
max_row=row
end if
end do
if (max_row.eq.0) then
! all elements are zero so singular matrix
if(verbose) write(*,*)'Singular matrix found in cinvert_Gauss_Jordan'
if (ierr.NE.0) then
run_status='ERROR: Singular matrix in cinvert_Gauss_Jordan'
CALL write_program_status()
STOP 1
else
ierr=1
RETURN
end if
end if
pivot_row=max_row
pivot_row_save(reduce_col)=pivot_row
! swap pivot row with the row reduce_col
if (pivot_row.ne.reduce_col) then
do col=1,n
swap=AI(reduce_col,col)
AI(reduce_col,col)=AI(pivot_row,col)
AI(pivot_row,col)=swap
end do
end if
pivot_row=reduce_col
pivot_element=AI(reduce_col,reduce_col)
! operate on pivot row
do col=1,n
if (col.ne.reduce_col) then
AI(pivot_row,col) = AI(pivot_row,col)/pivot_element
else
AI(pivot_row,col) = (1d0,0d0)/pivot_element
end if
end do
! operate on rows other than the pivot row
do row=1,n
if (row.ne.pivot_row) then
row_multiplier=AI(row,reduce_col)
do col=1,n
if (col.ne.reduce_col) then
AI(row,col) = AI(row,col)- AI(pivot_row,col)*row_multiplier
else
AI(row,reduce_col) =-AI(pivot_row,reduce_col)*row_multiplier
end if
end do
end if ! not pivot row
end do ! next row
end do ! next column of the matrix to reduce
do reduce_col=n,1,-1
if (reduce_col.ne.pivot_row_save(reduce_col)) then
! rows were swapped so must swap the corresponding columns
do row=1,n
swap=AI(row,pivot_row_save(reduce_col))
AI(row,pivot_row_save(reduce_col))=AI(row,reduce_col)
AI(row,reduce_col)=swap
end do
end if
end do
ierr=0
RETURN
END SUBROUTINE cinvert_Gauss_Jordan
!
! NAME
!
!
! DESCRIPTION
!
!
! HISTORY
!
! started 2/12/15 CJS
!
! COMMENTS
!
SUBROUTINE c_condition_number(A,n,condition_number,dim)
! Calculate the condition number of the complex matrix A
USE type_specifications
USE eispack
IMPLICIT NONE
! variables passed to subroutine
integer,intent(IN) :: dim ! matrix dimension
integer,intent(IN) :: n ! size of matrix to process
complex(dp),intent(IN) :: A(dim,dim) ! input matrix
real(dp),intent(OUT) :: condition_number ! output condition number
! local variables
complex(dp) :: AH(dim,dim)
complex(dp) :: AHA(dim,dim)
real(dp) :: Real_AHA(dim,dim)
real(dp) :: singular_values(dim)
integer :: row,col
real(dp) :: max_eigenvalue
real(dp) :: min_eigenvalue
logical :: matu,matv
integer :: ierr
! START
! calculate the Hermitian conjugate of A
do row=1,n
do col=1,n
AH(row,col)=conjg(A(col,row))
end do
end do
AHA=matmul(AH,A)
Real_AHA=dble(AHA)
! calculate the Singular Value Decomposition of AHA using Eispack
matu=.FALSE.
matv=.FALSE. ! we don't need the matrices U or V
CALL svd ( n, n, Real_AHA, singular_values, matu, Real_AHA, matv, Real_AHA, ierr )
! find the maximum and minimum magnitude of singular values
max_eigenvalue=sqrt(abs(singular_values(1)))
min_eigenvalue=sqrt(abs(singular_values(1)))
do row=2,n
! Note that the singular values of A are equal to the square root of the singular values of AHA
max_eigenvalue=max( max_eigenvalue,sqrt(abs(singular_values(row))) )
min_eigenvalue=min( min_eigenvalue,sqrt(abs(singular_values(row))) )
end do
! calculate the condition number
if (min_eigenvalue.NE.0D0) then
condition_number=max_eigenvalue/min_eigenvalue
else
! set the condition number to something large ***** SHOULD PROBABLY USE A PARAMETER FROM MODULE constants HERE *****
condition_number=1D100
end if
RETURN
END SUBROUTINE c_condition_number