forked from ESCOMP/CLUBB_CESM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_operations.F90
582 lines (443 loc) · 16.5 KB
/
matrix_operations.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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
!-----------------------------------------------------------------------
! $Id$
!===============================================================================
module matrix_operations
implicit none
public :: symm_covar_matrix_2_corr_matrix, Cholesky_factor, &
row_mult_lower_tri_matrix, print_lower_triangular_matrix, &
get_lower_triangular_matrix, set_lower_triangular_matrix, &
mirror_lower_triangular_matrix
private :: Symm_matrix_eigenvalues
private ! Default scope
contains
!-----------------------------------------------------------------------
subroutine symm_covar_matrix_2_corr_matrix( ndim, covar, corr )
! Description:
! Convert a matrix of covariances in to a matrix of correlations.
! This only does the computation the lower triangular portion of the
! matrix.
! References:
! None
!-----------------------------------------------------------------------
use clubb_precision, only: &
core_rknd ! double precision
implicit none
! External
intrinsic :: sqrt
! Input Variables
integer, intent(in) :: ndim
real( kind = core_rknd ), dimension(ndim,ndim), intent(in) :: &
covar ! Covariance Matrix [units vary]
! Output Variables
real( kind = core_rknd ), dimension(ndim,ndim), intent(out) :: &
corr ! Correlation Matrix [-]
! Local Variables
integer :: i, j
! ---- Begin Code ----
corr = 0._core_rknd ! Initialize to 0
do i = 1, ndim
do j = 1, i
corr(i,j) = covar(i,j) / sqrt( covar(i,i) * covar(j,j) )
end do
end do
return
end subroutine symm_covar_matrix_2_corr_matrix
!-----------------------------------------------------------------------
subroutine row_mult_lower_tri_matrix( ndim, xvector, tmatrix_in, tmatrix_out )
! Description:
! Do a row-wise multiply of the elements of a lower triangular matrix.
! References:
! None
!-----------------------------------------------------------------------
use clubb_precision, only: &
core_rknd ! double precision
implicit none
! Input Variables
integer, intent(in) :: ndim
real( kind = core_rknd ), dimension(ndim), intent(in) :: &
xvector ! Factors to be multiplied across a row [units vary]
! Input Variables
real( kind = core_rknd ), dimension(ndim,ndim), intent(in) :: &
tmatrix_in ! nxn matrix (usually a correlation matrix) [units vary]
! Output Variables
real( kind = core_rknd ), dimension(ndim,ndim), intent(inout) :: &
tmatrix_out ! nxn matrix (usually a covariance matrix) [units vary]
! Local Variables
integer :: i, j
! ---- Begin Code ----
do i = 1, ndim
do j = 1, i
tmatrix_out(i,j) = tmatrix_in(i,j) * xvector(i)
end do
end do
return
end subroutine row_mult_lower_tri_matrix
!-------------------------------------------------------------------------------
subroutine Cholesky_factor( ndim, a_input, a_scaling, a_Cholesky, l_scaled )
! Description:
! Create a Cholesky factorization of a_input.
! If the factorization fails we use a modified a_input matrix and attempt
! to factorize again.
!
! References:
! <http://www.netlib.org/lapack/explore-html/a00868.html> dpotrf
! <http://www.netlib.org/lapack/explore-html/a00860.html> dpoequ
! <http://www.netlib.org/lapack/explore-html/a00753.html> dlaqsy
!-------------------------------------------------------------------------------
use error_code, only: &
clubb_at_least_debug_level ! Procedure
use constants_clubb, only: &
fstderr ! Constant
use clubb_precision, only: &
core_rknd
implicit none
! External
external :: dpotrf, dpoequ, dlaqsy, & ! LAPACK subroutines
spotrf, spoequ, slaqsy
! Constant Parameters
integer, parameter :: itermax = 10 ! Max iterations of the modified method
real( kind = core_rknd), parameter :: d_coef = 0.1_core_rknd
! Coefficient applied if the decomposition doesn't work
! Input Variables
integer, intent(in) :: ndim
real( kind = core_rknd ), dimension(ndim,ndim), intent(in) :: a_input
! Output Variables
real( kind = core_rknd ), dimension(ndim), intent(out) :: a_scaling
real( kind = core_rknd ), dimension(ndim,ndim), intent(out) :: a_Cholesky
logical, intent(out) :: l_scaled
! Local Variables
real( kind = core_rknd ), dimension(ndim) :: a_eigenvalues
real( kind = core_rknd ), dimension(ndim,ndim) :: a_corr, a_scaled
real( kind = core_rknd ) :: tau, d_smallest
real( kind = core_rknd ) :: amax, scond
integer :: info
integer :: i, j, iter
character :: equed
logical :: l_dp
! ---- Begin code ----
a_scaled = a_input ! Copy input array into output array
! do i = 1, n
! do j = 1, n
! write(6,'(e10.3)',advance='no') a(i,j)
! end do
! write(6,*) ""
! end do
! pause
equed = 'N'
if ( kind( 0.0_core_rknd ) == kind( 0.0d0 ) ) then
l_dp = .true.
else if ( kind( 0.0_core_rknd ) == kind( 0.0 ) ) then
l_dp = .false.
else
stop "Precision is not single or double precision in Cholesky_factor"
end if
! Compute scaling for a_input
if ( l_dp ) then
call dpoequ( ndim, a_input, ndim, a_scaling, scond, amax, info )
else
call spoequ( ndim, a_input, ndim, a_scaling, scond, amax, info )
end if
if ( info == 0 ) then
! Apply scaling to a_input
if ( l_dp ) then
call dlaqsy( 'Lower', ndim, a_scaled, ndim, a_scaling, scond, amax, equed )
else
call slaqsy( 'Lower', ndim, a_scaled, ndim, a_scaling, scond, amax, equed )
end if
end if
! Determine if scaling was necessary
if ( equed == 'Y' ) then
l_scaled = .true.
a_Cholesky = a_scaled
else
l_scaled = .false.
a_Cholesky = a_input
end if
do iter = 1, itermax
if ( l_dp ) then
call dpotrf( 'Lower', ndim, a_Cholesky, ndim, info )
else
call spotrf( 'Lower', ndim, a_Cholesky, ndim, info )
end if
select case( info )
case( :-1 )
write(fstderr,*) "Cholesky_factor " // &
" illegal value for argument ", -info
stop
case( 0 )
! Success!
if ( clubb_at_least_debug_level( 1 ) .and. iter > 1 ) then
write(fstderr,*) "a_factored (worked)="
do i = 1, ndim
do j = 1, i
write(fstderr,'(g10.3)',advance='no') a_Cholesky(i,j)
end do
write(fstderr,*) ""
end do
end if
exit
case( 1: )
if ( clubb_at_least_debug_level( 1 ) ) then
! This shouldn't happen now that the s and t Mellor(chi/eta) elements have been
! modified to never be perfectly correlated, but it's here just in case.
! -dschanen 10 Sept 2010
write(fstderr,*) "Cholesky_factor: leading minor of order ", &
info, " is not positive definite."
write(fstderr,*) "factorization failed."
write(fstderr,*) "a_input="
do i = 1, ndim
do j = 1, i
write(fstderr,'(g10.3)',advance='no') a_input(i,j)
end do
write(fstderr,*) ""
end do
write(fstderr,*) "a_Cholesky="
do i = 1, ndim
do j = 1, i
write(fstderr,'(g10.3)',advance='no') a_Cholesky(i,j)
end do
write(fstderr,*) ""
end do
end if
if ( clubb_at_least_debug_level( 2 ) ) then
call Symm_matrix_eigenvalues( ndim, a_input, a_eigenvalues )
write(fstderr,*) "a_eigenvalues="
do i = 1, ndim
write(fstderr,'(g10.3)',advance='no') a_eigenvalues(i)
end do
write(fstderr,*) ""
call symm_covar_matrix_2_corr_matrix( ndim, a_input, a_corr )
write(fstderr,*) "a_correlations="
do i = 1, ndim
do j = 1, i
write(fstderr,'(g10.3)',advance='no') a_corr(i,j)
end do
write(fstderr,*) ""
end do
end if
if ( iter == itermax ) then
write(fstderr,*) "iteration =", iter, "itermax =", itermax
write(fstderr,*) "Fatal error in Cholesky_factor"
else if ( clubb_at_least_debug_level( 1 ) ) then
! Adding a STOP statement to prevent this problem from slipping under
! the rug.
write(fstderr,*) "Fatal error in Cholesky_factor"
write(fstderr,*) "Attempting to modify matrix to allow factorization."
end if
if ( l_scaled ) then
a_Cholesky = a_scaled
else
a_Cholesky = a_input
end if
! The number used for tau here is case specific to the Sigma covariance
! matrix in the latin hypercube code and is not at all general.
! Tau should be a number that is small relative to the other diagonal
! elements of the matrix to have keep the error caused by modifying 'a' low.
! -dschanen 30 Aug 2010
d_smallest = a_Cholesky(1,1)
do i = 2, ndim
if ( d_smallest > a_Cholesky(i,i) ) d_smallest = a_Cholesky(i,i)
end do
! Use the smallest element * d_coef * iteration
tau = d_smallest * d_coef * real( iter, kind=core_rknd )
! print *, "tau =", tau, "d_smallest = ", d_smallest
do i = 1, ndim
do j = 1, ndim
if ( i == j ) then
a_Cholesky(i,j) = a_Cholesky(i,j) + tau ! Add tau to the diagonal
else
a_Cholesky(i,j) = a_Cholesky(i,j)
end if
end do
end do
if ( clubb_at_least_debug_level( 2 ) ) then
call Symm_matrix_eigenvalues( ndim, a_Cholesky, a_eigenvalues )
write(fstderr,*) "a_modified eigenvalues="
do i = 1, ndim
write(fstderr,'(e10.3)',advance='no') a_eigenvalues(i)
end do
write(fstderr,*) ""
end if
end select ! info
end do ! 1..itermax
return
end subroutine Cholesky_factor
!----------------------------------------------------------------------
subroutine Symm_matrix_eigenvalues( ndim, a_input, a_eigenvalues )
! Description:
! Computes the eigevalues of a_input
!
! References:
! None
!-----------------------------------------------------------------------
use constants_clubb, only: &
fstderr ! Constant
use clubb_precision, only: &
core_rknd ! double precision
implicit none
! External
external :: dsyev, ssyev ! LAPACK subroutine(s)
! Parameters
integer, parameter :: &
lwork = 180 ! This is the optimal value I obtained for an n of 5 -dschanen 31 Aug 2010
! Input Variables
integer, intent(in) :: ndim
real( kind = core_rknd ), dimension(ndim,ndim), intent(in) :: a_input
! Output Variables
real( kind = core_rknd ), dimension(ndim), intent(out) :: a_eigenvalues
! Local Variables
real( kind = core_rknd ), dimension(ndim,ndim) :: a_scratch
real( kind = core_rknd ), dimension(lwork) :: work
integer :: info
! integer :: i, j
! ---- Begin code ----
a_scratch = a_input
! do i = 1, ndim
! do j = 1, ndim
! write(6,'(e10.3)',advance='no') a(i,j)
! end do
! write(6,*) ""
! end do
! pause
if ( kind( 0.0_core_rknd ) == kind( 0.0d0 ) ) then
call dsyev( 'No eigenvectors', 'Lower', ndim, a_scratch, ndim, &
a_eigenvalues, work, lwork, info )
else if ( kind( 0.0_core_rknd ) == kind( 0.0 ) ) then
call ssyev( 'No eigenvectors', 'Lower', ndim, a_scratch, ndim, &
a_eigenvalues, work, lwork, info )
else
stop "Precision is not single or double in Symm_matrix_eigenvalues"
end if
select case( info )
case( :-1 )
write(fstderr,*) "Symm_matrix_eigenvalues:" // &
" illegal value for argument ", -info
case( 0 )
! Success!
case( 1: )
write(fstderr,*) "Symm_matrix_eigenvalues: Algorithm failed to converge."
end select
return
end subroutine Symm_matrix_eigenvalues
!-------------------------------------------------------------------------------
subroutine set_lower_triangular_matrix( pdf_dim, index1, index2, xpyp, &
matrix )
! Description:
! Set a value for the lower triangular portion of a matrix.
! References:
! None
!-------------------------------------------------------------------------------
use clubb_precision, only: &
core_rknd ! user defined precision
implicit none
! External
intrinsic :: max, min
! Input Variables
integer, intent(in) :: &
pdf_dim, & ! Number of variates
index1, index2 ! Indices for 2 variates (the order doesn't matter)
real( kind = core_rknd ), intent(in) :: &
xpyp ! Value for the matrix (usually a correlation or covariance) [units vary]
! Input/Output Variables
real( kind = core_rknd ), dimension(pdf_dim,pdf_dim), intent(inout) :: &
matrix ! The lower triangular matrix
integer :: i,j
! ---- Begin Code ----
! Reverse these to set the values of upper triangular matrix
i = max( index1, index2 )
j = min( index1, index2 )
if( i > 0 .and. j > 0 ) then
matrix(i,j) = xpyp
end if
return
end subroutine set_lower_triangular_matrix
!-------------------------------------------------------------------------------
!-------------------------------------------------------------------------------
subroutine get_lower_triangular_matrix( pdf_dim, index1, index2, matrix, &
xpyp )
! Description:
! Returns a value from the lower triangular portion of a matrix.
! References:
! None
!-------------------------------------------------------------------------------
use clubb_precision, only: &
core_rknd
implicit none
! External
intrinsic :: max, min
! Input Variables
integer, intent(in) :: &
pdf_dim, & ! Number of variates
index1, index2 ! Indices for 2 variates (the order doesn't matter)
! Input/Output Variables
real( kind = core_rknd ), dimension(pdf_dim,pdf_dim), intent(in) :: &
matrix ! The covariance matrix
real( kind = core_rknd ), intent(out) :: &
xpyp ! Value from the matrix (usually a correlation or covariance) [units vary]
integer :: i,j
! ---- Begin Code ----
! Reverse these to set the values of upper triangular matrix
i = max( index1, index2 )
j = min( index1, index2 )
xpyp = matrix(i,j)
return
end subroutine get_lower_triangular_matrix
!-----------------------------------------------------------------------
subroutine print_lower_triangular_matrix( iunit, ndim, matrix )
! Description:
! Print the values of lower triangular matrix to a file or console.
! References:
! None
!-----------------------------------------------------------------------
use clubb_precision, only: &
core_rknd ! Variable(s)
implicit none
! Input Variables
integer, intent(in) :: &
iunit, & ! File I/O logical unit (usually 6 for stdout and 0 for stderr)
ndim ! Dimension of the matrix
real( kind = core_rknd ), dimension(ndim,ndim), intent(in) :: &
matrix ! Lower triangular matrix [units vary]
! Local Variables
integer :: i, j
! ---- Begin Code ----
do i = 1, ndim
do j = 1, i
write(iunit,fmt='(g15.6)',advance='no') matrix(i,j)
end do
write(iunit,fmt=*) "" ! newline
end do
return
end subroutine print_lower_triangular_matrix
!-----------------------------------------------------------------------
subroutine mirror_lower_triangular_matrix( nvars, matrix )
! Description:
! Mirrors the elements of a lower triangular matrix to the upper
! triangle so that it is symmetric.
! References:
! None
!-----------------------------------------------------------------------
use clubb_precision, only: &
core_rknd ! Constant
implicit none
! Input Variables
integer, intent(in) :: &
nvars ! Number of variables in each dimension of square matrix
! Input/Output Variables
real( kind = core_rknd ), dimension(nvars,nvars), intent(inout) :: &
matrix ! Lower triangluar square matrix
! Local Variables
integer :: row, col
!-----------------------------------------------------------------------
!----- Begin Code -----
if ( nvars > 1 ) then
do col=2, nvars
do row=1, col-1
matrix(row,col) = matrix(col,row)
end do
end do
end if ! nvars > 1
return
end subroutine mirror_lower_triangular_matrix
!-----------------------------------------------------------------------
end module matrix_operations