-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix_utils.F90
2342 lines (1927 loc) · 64.5 KB
/
Matrix_utils.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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!Matrix utility routines. Uses BLAS/LAPACK. Mostly wrapper routines.
!Generally (but not always) assumes that all matrix arrays are defined at exactly correct size
!Not complete
!Antony Lewis May 2003-2007
!http://cosmologist.info/utils/
module MatrixUtils
use AMLutils
implicit none
logical, parameter :: Matrix_runmsgs = .false.
#ifdef MATRIX_SINGLE
integer, parameter :: dm = KIND(1.0)
#else
integer, parameter :: dm = KIND(1.d0)
#endif
!Precision of matrix operators
!If changing also need to change prefix on LAPACK routine names
integer, parameter :: Mat_F90=1, Mat_Norm=2, Mat_DC =3 !Normal, basic BLAS/LAPACK or divide and conquer
integer, parameter :: matrix_method = mat_DC
real Matrix_StartTime
Type TMatrixType
real(dm), dimension(:,:), pointer :: M
end Type TMatrixType
complex(dm), parameter :: COne = (1._dm,0._dm), CZero = (0._dm,0._dm)
real(dm), parameter :: ROne = 1._dm, RZero = 0._dm
real, parameter :: SOne = 1., SZero = 0.
contains
function GetMatrixTime()
real GetMatrixTime
real atime
call cpu_time(atime)
GetMatrixTime = atime
end function GetMatrixTime
subroutine Matrix_start(Name)
character(LEN=*), intent(in) :: Name
if (Matrix_runmsgs) then
Matrix_StartTime = GetMatrixTime()
Write(*,*) 'Matrix_'//trim(Name) //' start'
end if
end subroutine Matrix_start
subroutine Matrix_end(Name)
character(LEN=*), intent(in) :: Name
if (Matrix_runmsgs) then
Write(*,*) 'Matrix_'//trim(Name) //' end: ', GetMatrixTime() - Matrix_StartTime
end if
end subroutine Matrix_end
subroutine Matrix_WriteFileRow(aunit, vec,n)
integer, intent(in) :: aunit
integer, intent(in) :: n
real(dm) :: vec(n)
character(LEN=50) fmt
fmt = trim(numcat('(',n))//'E17.7)'
write (aunit, fmt) vec(1:n)
end subroutine Matrix_WriteFileRow
subroutine Matrix_Write(aname, mat, forcetable, commentline)
character(LEN=*), intent(in) :: aname
character(LEN=*), intent(in), optional :: commentline
real(dm), intent(in) :: mat(:,:)
logical, intent(in), optional :: forcetable
integer i,k
character(LEN=50) fmt
integer shp(2)
logical WriteTab
integer file_unit
shp = shape(mat)
WriteTab = shp(2)<=50
if (present(forcetable)) then
if (forcetable) WriteTab = .true.
end if
file_unit = new_file_unit()
call CreateTxtFile(aname, file_unit)
if (present(commentline)) then
write(file_unit,'(a)') '#'//trim(commentline)
end if
fmt = trim(numcat('(',shp(2)))//'E15.5)'
do i=1, shp(1)
if (.not. WriteTab) then
do k=1, shp(2)
write (file_unit, '(1E17.7)') mat(i,k)
end do
else
write (file_unit, fmt) mat(i,1:shp(2))
end if
end do
call CloseFile(file_unit)
end subroutine Matrix_Write
subroutine Matrix_Write_double(aname, mat, forcetable)
character(LEN=*), intent(in) :: aname
double precision, intent(in) :: mat(:,:)
logical, intent(in), optional :: forcetable
integer i,k
character(LEN=50) fmt
integer shp(2)
logical WriteTab
integer file_unit
shp = shape(mat)
WriteTab = shp(2)<=50
if (present(forcetable)) then
if (forcetable) WriteTab = .true.
end if
file_unit = new_file_unit()
call CreateTxtFile(aname, file_unit)
fmt = trim(numcat('(',shp(2)))//'E15.5)'
do i=1, shp(1)
if (.not. WriteTab) then
do k=1, shp(2)
write (file_unit, '(1E17.7)') mat(i,k)
end do
else
write (file_unit, fmt) mat(i,1:shp(2))
end if
end do
call CloseFile(file_unit)
end subroutine Matrix_Write_double
subroutine Matrix_Write_Binary(aname, mat)
character(LEN=*), intent(in) :: aname
real(dm), intent(in) :: mat(:,:)
integer file_unit
file_unit = new_file_unit()
call CreateFile(aname, file_unit,'unformatted')
write (file_unit) mat
call CloseFile(file_unit)
end subroutine Matrix_Write_Binary
subroutine MatrixSym_Write_Binary(aname, mat)
character(LEN=*), intent(in) :: aname
real(dm), intent(in) :: mat(:,:)
integer i
integer shp(2)
integer file_unit
shp = shape(mat)
if (shp(1) /= shp(2)) call MpiStop('MatrixSym_Write_Binary: Not square matrix')
if (shp(1) == 0) return
file_unit = new_file_unit()
call CreateFile(aname, file_unit,'unformatted')
do i=1,shp(1)
write (file_unit) mat(i:shp(2),i)
end do
call CloseFile(file_unit)
end subroutine MatrixSym_Write_Binary
subroutine MatrixSym_Write_Binary_Single(aname, mat)
character(LEN=*), intent(in) :: aname
real(dm), intent(in) :: mat(:,:)
integer i, file_unit
integer shp(2)
shp = shape(mat)
if (shp(1) /= shp(2)) call MpiStop('MatrixSym_Write_Binary_Single: Not square matrix')
if (shp(1) == 0) return
file_unit = new_file_unit()
call CreateFile(aname, file_unit,'unformatted')
do i=1,shp(1)
write (file_unit) real(mat(i:shp(2),i), kind(1.0))
end do
call CloseFile(file_unit)
end subroutine MatrixSym_Write_Binary_Single
subroutine Matrix_WriteVec(aname, vec)
character(LEN=*), intent(in) :: aname
real(dm), intent(in) :: vec(:)
integer i, file_unit
file_unit = new_file_unit()
call CreateTxtFile(aname, file_unit)
do i=1, size(vec)
write (file_unit, '(1E17.7)') vec(i)
end do
call CloseFile(file_unit)
end subroutine Matrix_WriteVec
subroutine Matrix_Read_Binary(aname, mat)
character(LEN=*), intent(in) :: aname
real(dm), intent(out) :: mat(:,:)
integer file_unit
file_unit = new_file_unit()
call OpenFile(aname, file_unit,'unformatted')
read (file_unit) mat
call CloseFile(file_unit)
end subroutine Matrix_Read_Binary
subroutine MatrixSym_Read_Binary(aname, mat)
character(LEN=*), intent(in) :: aname
real(dm), intent(out) :: mat(:,:)
integer i, file_unit
integer shp(2)
shp = shape(mat)
if (shp(1) /= shp(2)) call MpiStop( 'MatrixSym_Read_Binary: Not square matrix')
if (shp(1) == 0) return
file_unit = new_file_unit()
call OpenFile(aname, file_unit,'unformatted')
do i=1,shp(1)
read (file_unit) mat(i:shp(1),i)
mat(i,i:shp(1)) = mat(i:shp(1),i)
end do
call CloseFile(file_unit)
end subroutine MatrixSym_Read_Binary
subroutine MatrixSym_Read_Binary_Single(aname, mat)
character(LEN=*), intent(in) :: aname
real, intent(out) :: mat(:,:)
integer i, file_unit
integer shp(2)
shp = shape(mat)
if (shp(1) /= shp(2)) call MpiStop( 'MatrixSym_Read_Binary: Not square matrix')
if (shp(1) == 0) return
file_unit = new_file_unit()
call OpenFile(aname, file_unit,'unformatted')
do i=1,shp(1)
read (file_unit) mat(i:shp(1),i)
mat(i,i:shp(1)) = mat(i:shp(1),i)
end do
call CloseFile(file_unit)
end subroutine MatrixSym_Read_Binary_Single
subroutine Matrix_Read(aname, mat)
character(LEN=*), intent(IN) :: aname
real(dm), intent(out) :: mat(:,:)
integer j,k, file_unit
integer shp(2)
real(dm) tmp
shp = shape(mat)
file_unit = new_file_unit()
call OpenTxtFile(aname, file_unit)
do j=1,shp(1)
read (file_unit,*, end = 200, err=100) mat(j,1:shp(2))
end do
goto 120
100 rewind(file_unit) !Try other possible format
do j=1,shp(1)
do k=1,shp(2)
read (file_unit,*, end = 200) mat(j,k)
end do
end do
120 read (file_unit,*, err = 150, end =150) tmp
goto 200
150 call CloseFile(file_unit)
return
200 call MpiStop('Matrix_Read: file '//trim(aname)//' is the wrong size')
end subroutine Matrix_Read
subroutine Matrix_ReadSingle(aname, mat)
character(LEN=*), intent(IN) :: aname
real, intent(out) :: mat(:,:)
integer j,k, file_unit
integer shp(2)
real tmp
shp = shape(mat)
file_unit = new_file_unit()
call OpenTxtFile(aname, file_unit)
do j=1,shp(1)
read (file_unit,*, end = 200, err=100) mat(j,1:shp(2))
end do
goto 120
100 rewind(file_unit) !Try other possible format
do j=1,shp(1)
do k=1,shp(2)
read (file_unit,*, end = 200) mat(j,k)
end do
end do
120 read (file_unit,*, err = 150, end =150) tmp
goto 200
150 call CloseFile(file_unit)
return
200 call MpiStop('Matrix_Read:Single file '//trim(aname)//' is the wrong size')
end subroutine Matrix_ReadSingle
function Matrix_Diag(M, n)
integer, intent(in) :: n
real(dm), intent(in) :: M(:,:)
real(dm) Matrix_Diag(n)
integer i
do i=1,n
Matrix_Diag(i) = M(i,i)
end do
end function Matrix_Diag
function ILAENV_wrap(i,S1,S2,a,b,c,d)
integer ILAENV_wrap
integer, intent(in) :: i,a,b,c,d
character(LEN=*), intent(in) :: S1, S2
integer, external :: ILAENV
!If you don't have ILAENV in math library, change routine to return some positive integer
!that is a guess at the blocksize
#ifdef MATRIX_SINGLE
ILAENV_wrap = 16
#else
ILAENV_wrap = ILAENV(i,S1,S2,a,b,c,d)
#endif
!!!IFC
end function ILAENV_wrap
subroutine Matrix_Diagonalize(M, diag, n)
!Does m = U diag U^T, returning U in M
integer, intent(in) :: n
real(dm), intent(inout):: m(n,n)
real(dm), intent(out) :: diag(n)
integer ierr, tmpsize
real(dm), allocatable, dimension(:) :: tmp
call Matrix_Start('Diagonalize')
#ifdef MATRIX_SINGLE
tmpsize = max( (ILAENV_wrap(1,'SSYTRD','U',n,n,n,n)+2)*N,max(1,3*n-1)) !3*n**2
allocate(tmp(tmpsize));
call SSYEV('V','U',n,m,n,diag,tmp,tmpsize,ierr) !evalues and vectors of symmetric matrix
#else
tmpsize = max( (ILAENV_wrap(1,'DSYTRD','U',n,n,n,n)+2)*N,max(1,3*n-1)) !3*n**2
allocate(tmp(tmpsize));
call DSYEV('V','U',n,m,n,diag,tmp,tmpsize,ierr) !evalues and vectors of symmetric matrix
#endif
if (ierr /= 0) call MpiStop('Error in Matrix_Diagonalize')
deallocate(tmp)
call Matrix_End('Diagonalize')
end subroutine Matrix_Diagonalize
subroutine Matrix_Diagonalize_DC(M, diag, n)
!Complex version. Does m = U diag U^dag, returning U in M
integer, intent(in) :: n
real(dm), intent(inout):: m(n,n)
real(dm), intent(out) :: diag(n)
integer ierr, tmpsize ,isize
real(dm), allocatable, dimension(:) :: tmp
integer, allocatable,dimension(:):: iwork
call Matrix_Start('Diagonalize')
if (matrix_method == Mat_DC) then
!Divide and conquer
tmpsize = 1 + 6*N + 2*N**2
isize = 3+5*N
allocate(tmp(tmpsize))
allocate(iwork(isize))
#ifdef MATRIX_SINGLE
call SSYEVD('V','U',n,M,n,diag,tmp,tmpsize,iwork,isize,ierr) !evalues and vectors of hermitian matrix
#else
call DSYEVD('V','U',n,M,n,diag,tmp,tmpsize,iwork,isize,ierr) !evalues and vectors of hermitian matrix
#endif
deallocate(iwork)
deallocate(tmp)
else
call Matrix_Diagonalize(M, diag, n)
end if
if (ierr /= 0) call MpiStop('Error in Matrix_Diagonalize')
call Matrix_End('Diagonalize')
end subroutine Matrix_Diagonalize_DC
subroutine Matrix_Root(M, n, pow)
!Does M**pow for symmetric M using U D**pow U^T
!Not optimized for large matrices
integer, intent(in) :: n
real(dm), intent(inout):: M(n,n)
real(dm) :: Tmp(n,n)
real(dm), intent(in) :: pow
real(dm) :: diag(n)
integer i
call Matrix_Diagonalize(M, diag, n)
Tmp = M
diag = diag**pow
do i = 1, n
M(:,i) = M(:,i)*diag(i)
end do
M = matmul(M,transpose(Tmp))
end subroutine Matrix_Root
subroutine Matrix_Diagonalize_Partial(M, diag, n, emin,emax, nfound)
!Real version. Does m = U diag U^dag, returning U in M
!Assumes up to nfound values will be found. nfound set to true value on exit
integer, intent(in) :: n
real(dm), intent(inout):: m(:,:)
real(dm), intent(out) :: diag(:)
real(dm), intent(in) :: emin,emax
integer, intent(inout) :: nfound
integer ierr, worksize, LIWork
real(dm), allocatable, dimension(:) :: work
real(dm), allocatable, dimension(:,:) :: tmp
integer, allocatable,dimension(:):: supp,iwork
real(dm) wsize(1)
real(dm) atol
integer ISize(1)
atol = 1d-9
call Matrix_Start('Matrix_Diagonalize_Partial')
allocate(tmp(n,nfound))
allocate(Supp(n))
!Query
WorkSize = -1
LIWork = -1
#ifdef MATRIX_SINGLE
call SSYEVR('V','V','U',n,M,Size(M,DIM=1),emin,emax,0,0,atol,nfound,diag,tmp,Size(TMP,DIM=1),&
Supp,WSize,WorkSize,ISize,LIWork,ierr )
#else
call DSYEVR('V','V','U',n,M,Size(M,DIM=1),emin,emax,0,0,atol,nfound,diag,tmp,Size(TMP,DIM=1),&
Supp,WSize,WorkSize,ISize,LIWork,ierr )
#endif
WorkSize = Real(WSize(1))
LIWork = ISize(1)
allocate(Work(WorkSize),IWork(LIWork))
#ifdef MATRIX_SINGLE
call SSYEVR('V','V','U',n,M,Size(M,DIM=1),emin,emax,0,0,atol,nfound,diag,tmp,Size(TMP,DIM=1),&
Supp,Work,WorkSize,IWork,LIWork,ierr )
#else
call DSYEVR('V','V','U',n,M,Size(M,DIM=1),emin,emax,0,0,atol,nfound,diag,tmp,Size(TMP,DIM=1),&
Supp,Work,WorkSize,IWork,LIWork,ierr )
#endif
deallocate(Supp,Work,IWork)
if (ierr /= 0) call MpiStop('Matrix_Diagonalize_Partial: Error')
M(1:n,1:nfound) = tmp(1:n,1:nfound) !nfound now different
deallocate(tmp)
call Matrix_End('Matrix_Diagonalize_Partial')
end subroutine Matrix_Diagonalize_Partial
subroutine Matrix_CDiagonalize_Partial(M, diag, n, emin,emax, nfound)
!Complex version. Does m = U diag U^dag, returning U in M
!Assumes up to nfound values will be found. nfound set to true value on exit
integer, intent(in) :: n
complex(dm), intent(inout):: m(:,:)
real(dm), intent(out) :: diag(:)
real(dm), intent(in) :: emin,emax
integer, intent(inout) :: nfound
integer ierr, worksize, LRWork, LIWork
real(dm), allocatable, dimension(:) :: Rwork
complex(dm), allocatable, dimension(:) :: work
complex(dm), allocatable, dimension(:,:) :: tmp
integer, allocatable,dimension(:):: supp,iwork
complex(dm) wsize(1)
real(dm) Rsize(1), atol
integer ISize(1)
atol = 1d-9
call Matrix_Start('Matrix_CDiagonalize_Partial')
allocate(tmp(n,nfound))
allocate(Supp(n))
!Query
WorkSize = -1
LRWork = -1
LIWork = -1
#ifdef MATRIX_SINGLE
call CHEEVR('V','V','U',n,M,Size(M,DIM=1),emin,emax,0,0,atol,nfound,diag,tmp,Size(TMP,DIM=1),&
Supp,WSize,WorkSize,RSize,LRWork,ISize,LIWork,ierr )
#else
call ZHEEVR('V','V','U',n,M,Size(M,DIM=1),emin,emax,0,0,atol,nfound,diag,tmp,Size(TMP,DIM=1),&
Supp,WSize,WorkSize,RSize,LRWork,ISize,LIWork,ierr )
#endif
WorkSize = Real(WSize(1))
LRWork = RSize(1)
LIWork = ISize(1)
allocate(Work(WorkSize),RWork(LRWork),IWork(LIWork))
#ifdef MATRIX_SINGLE
call CHEEVR('V','V','U',n,M,Size(M,DIM=1),emin,emax,0,0,atol,nfound,diag,tmp,Size(TMP,DIM=1),&
Supp,Work,WorkSize,RWork,LRWork,IWork,LIWork,ierr )
#else
call ZHEEVR('V','V','U',n,M,Size(M,DIM=1),emin,emax,0,0,atol,nfound,diag,tmp,Size(TMP,DIM=1),&
Supp,Work,WorkSize,RWork,LRWork,IWork,LIWork,ierr )
#endif
deallocate(Supp,Work,RWork,IWork)
if (ierr /= 0) call MpiStop('Matrix_CDiagonalize_Partial: Error')
M(1:n,1:nfound) = tmp(1:n,1:nfound) !nfound now different
deallocate(tmp)
call Matrix_End('Matrix_CDiagonalize_Partial')
end subroutine
subroutine Matrix_CDiagonalize(M, diag, n)
!Complex version. Does m = U diag U^dag, returning U in M
integer, intent(in) :: n
complex(dm), intent(inout):: m(n,n)
real(dm), intent(out) :: diag(n)
integer ierr, tmpsize ,isize, rworksize
real(dm), allocatable, dimension(:) :: Rwork
complex(dm), allocatable, dimension(:) :: tmp
integer, allocatable,dimension(:):: iwork
call Matrix_Start('CDiagonalize')
if (matrix_method == Mat_DC) then
!Divide and conquer
tmpsize = 2*N + N**2
rworksize = 1 + 4*N + 2*N*int(log(real(N))/log(2.)+1) + 3*N**2
isize = (2 + 5*N)*4
allocate(tmp(tmpsize),rwork(rworksize))
allocate(iwork(isize))
#ifdef MATRIX_SINGLE
call CHEEVD('V','U',n,M,n,diag,tmp,tmpsize,Rwork,rworksize,iwork,isize,ierr) !evalues and vectors of hermitian matrix
#else
call ZHEEVD('V','U',n,M,n,diag,tmp,tmpsize,Rwork,rworksize,iwork,isize,ierr) !evalues and vectors of hermitian matrix
#endif
deallocate(iwork)
else
rworksize = max(1, 3*n-2)
#ifdef MATRIX_SINGLE
tmpsize = max( (ILAENV_wrap(1,'CHETRD','U',n,n,n,n)+1)*N,max(1,2*n-1)) ! 3*n**2
allocate(tmp(tmpsize),rwork(rworksize));
call CHEEV('V','U',n,m,n,diag,tmp,tmpsize,Rwork,ierr) !evalues and vectors of hermitian matrix
#else
tmpsize = max( (ILAENV_wrap(1,'ZHETRD','U',n,n,n,n)+1)*N,max(1,2*n-1)) ! 3*n**2
allocate(tmp(tmpsize),rwork(rworksize));
call ZHEEV('V','U',n,m,n,diag,tmp,tmpsize,Rwork,ierr) !evalues and vectors of hermitian matrix
#endif
end if
if (ierr /= 0) call MpiStop('Error in Matrix_CDiagonalize')
deallocate(tmp,rwork)
call Matrix_End('CDiagonalize')
end subroutine Matrix_CDiagonalize
function Matrix_CTrace(M)
complex(dm), intent(in) :: M(:,:)
complex(dm) tmp,Matrix_CTrace
integer i
if (size(M,dim=1) /= size(M,dim=2)) call MpiStop('Matrix_CTrace: non-square matrix')
tmp =0
do i=1,size(M,dim=1)
tmp = tmp + M(i,i)
end do
Matrix_CTrace = tmp
end function Matrix_CTrace
function Matrix_Trace(M)
real(dm), intent(in) :: M(:,:)
real(dm) tmp,Matrix_Trace
integer i
if (size(M,dim=1) /= size(M,dim=2)) call mpiStop('Matrix_Trace: non-square matrix')
tmp =0
do i=1,size(M,dim=1)
tmp = tmp + M(i,i)
end do
Matrix_Trace = tmp
end function Matrix_Trace
function MatrixSym_LogDet(mat) result (logDet)
real(dm), intent(in) :: mat(:,:)
real(dm) logDet
real(dm) Tmp(size(mat,dim=1),size(mat,dim=1))
integer i
if (size(mat,dim=1) /= size(mat,dim=2)) call mpiStop('MatrixSym_LogDet: non-square matrix')
Tmp = mat
call Matrix_Cholesky(tmp)
logDet =0
do i=1, size(mat,dim=1)
logDet = logDet + log(tmp(i,i))
end do
logDet = 2._dm*logDet
end function MatrixSym_LogDet
subroutine Matrix_CRotateSymm(Mat,U,m,Out,triangular)
!Gets U^dag Mat U
integer, intent(in) ::m
complex(dm), intent(in) :: Mat(:,:),U(:,:)
complex(dm) Out(:,:)
complex(dm), dimension(:,:), allocatable :: C
integer n
logical, intent(in), optional :: triangular
logical :: triang
call Matrix_Start('CRotateSymm')
if (present(triangular)) then
triang=triangular
else
triang=.false.
end if
n = Size(Mat,DIM=1)
if (n /= Size(Mat,DIM=2)) call mpiStop('Matrix_CRotateSymm: Need square matrix')
if (n /= Size(U,DIM=1)) call MpiStop('Matrix_CRotateSymm: Matrix size mismatch')
if (Size(Out,DIM=1) < m .or. Size(Out,DIM=2) < m) &
call MpiStop('Matrix_CRotateSymm: Wrong output size')
if (matrix_method == Mat_F90) then
Out = matmul(matmul(transpose(conjg(U(1:n,1:m))),Mat),U(1:n,1:m))
else
#ifdef MATRIX_SINGLE
if (triang) then
if (m/=n) call MpiStop('Matrix_CRotateSymm: Matrices must be same size')
call CHEMM('L','U',n,n,COne,Mat,Size(Mat,DIM=1),U,Size(U,DIM=1),CZero,Out,Size(Out,Dim=1))
call CTRMM('Left','Upper','Complex-Transpose','Not-unit',n,n,COne,U,Size(U,DIM=1),Out,Size(Out,Dim=1))
else
allocate(C(n,m))
call CHEMM('L','U',n,m,COne,Mat,Size(Mat,DIM=1),U,Size(U,DIM=1),CZero,C,n)
call CGEMM('C','N',m,m,n,COne,U,Size(U,DIM=1),C,n,CZero,Out,Size(Out,Dim=1))
deallocate(C)
end if
#else
if (triang) then
if (m/=n) call MpiStop('Matrix_CRotateSymm: Matrices must be same size')
call ZHEMM('L','U',n,n,COne,Mat,Size(Mat,DIM=1),U,Size(U,DIM=1),CZero,Out,Size(Out,Dim=1))
call ZTRMM('Left','Upper','Complex-Transpose','Not-unit',n,n,COne,U,Size(U,DIM=1),Out,Size(Out,Dim=1))
else
allocate(C(n,m))
call ZHEMM('L','U',n,m,COne,Mat,Size(Mat,DIM=1),U,Size(U,DIM=1),CZero,C,n)
call ZGEMM('C','N',m,m,n,COne,U,Size(U,DIM=1),C,n,CZero,Out,Size(Out,Dim=1))
deallocate(C)
end if
#endif
end if
call Matrix_End('CRotateSymm')
end subroutine Matrix_CRotateSymm
subroutine Matrix_RotateSymm(Mat,U,m,Out, triangular)
!Gets U^T Mat U
!If triangular U = Upper triangular (U^T lower triangular)
integer, intent(in) ::m
real(dm), intent(in) :: Mat(:,:),U(:,:)
real(dm) Out(:,:)
real(dm), dimension(:,:), allocatable :: C
logical, intent(in), optional :: triangular
logical triang
integer n
call Matrix_Start('RotateSymm')
if (present(triangular)) then
triang=triangular
else
triang=.false.
end if
n = Size(Mat,DIM=1)
if (n /= Size(Mat,DIM=2)) call MpiStop('Matrix_RotateSymm: Need square matrix')
if (n /= Size(U,DIM=1)) call MpiStop('Matrix_RotateSymm: Matrix size mismatch')
if (Size(Out,DIM=1) < m .or. Size(Out,DIM=2) < m) &
call MpiStop('Matrix_RotateSymm: Wrong output size')
if (matrix_method == Mat_F90) then
Out = matmul(matmul(transpose(U(1:n,1:m)),Mat),U(1:n,1:m))
else
#ifdef MATRIX_SINGLE
if (triang) then
if (m/=n) call MpiStop('Matrix_RotateSymm: Matrices must be same size')
call SSYMM('L','U',n,n,ROne,Mat,Size(Mat,DIM=1),U,Size(U,DIM=1),RZero,Out,Size(Out,Dim=1))
call STRMM('Left','Upper','Transpose','Not-unit',n,n,ROne,U,Size(U,DIM=1),Out,Size(Out,Dim=1))
else
allocate(C(n,m))
call SSYMM('L','U',n,m,ROne,Mat,Size(Mat,DIM=1),U,Size(U,DIM=1),RZero,C,n)
call SGEMM('T','N',m,m,n,ROne,U,Size(U,DIM=1),C,n,RZero,Out,Size(Out,Dim=1))
deallocate(C)
end if
#else
if (triang) then
if (m/=n) call MpiStop('Matrix_RotateSymm: Matrices must be same size')
call DSYMM('L','U',n,n,ROne,Mat,Size(Mat,DIM=1),U,Size(U,DIM=1),RZero,Out,Size(Out,Dim=1))
call DTRMM('Left','Upper','Transpose','Not-unit',n,n,ROne,U,Size(U,DIM=1),Out,Size(Out,Dim=1))
else
allocate(C(n,m))
call DSYMM('L','U',n,m,ROne,Mat,Size(Mat,DIM=1),U,Size(U,DIM=1),RZero,C,n)
call DGEMM('T','N',m,m,n,ROne,U,Size(U,DIM=1),C,n,RZero,Out,Size(Out,Dim=1))
deallocate(C)
end if
#endif
end if
call Matrix_End('RotateSymm')
end subroutine Matrix_RotateSymm
subroutine Matrix_RotateAntiSymm(Mat,U,m,Out)
!Gets U^T Mat U
!Where Mat = -Mat^T
integer, intent(in) ::m
real(dm), intent(in) :: Mat(:,:),U(:,:)
real(dm) Out(:,:)
real(dm), dimension(:,:), allocatable :: C
integer i,j,n
call Matrix_Start('RotateAntiSymm')
n = Size(Mat,DIM=1)
if (n /= Size(Mat,DIM=2)) call MpiStop('Matrix_RotateAntiSymm: Need square matrix')
if (n /= Size(U,DIM=1)) call MpiStop('Matrix_RotateAntiSymm: Matrix size mismatch')
if (Size(Out,DIM=1) < m .or. Size(Out,DIM=2) < m) &
call MpiStop('Matrix_RotateAntiSymm: Wrong output size')
if (matrix_method == Mat_F90) then
Out = matmul(matmul(transpose(U(1:n,1:m)),Mat),U(1:n,1:m))
else
allocate(C(n,m))
C = U(1:n,1:m)
#ifdef MATRIX_SINGLE
call STRMM('Left','Lower','Not-Transpose','Not-unit',n,m,ROne,Mat,Size(Mat,DIM=1),C,Size(C,Dim=1))
call SGEMM('T','N',m,m,n,ROne,U,Size(U,DIM=1),C,n,RZero,Out,Size(Out,Dim=1))
#else
call DTRMM('Left','Lower','Not-Transpose','Not-unit',n,m,ROne,Mat,Size(Mat,DIM=1),C,Size(C,Dim=1))
call DGEMM('T','N',m,m,n,ROne,U,Size(U,DIM=1),C,n,RZero,Out,Size(Out,Dim=1))
#endif
deallocate(C)
end if
do i=1, m
do j=1,i
Out(j,i) = Out(j,i) - Out(i,j)
out(i,j) = -Out(j,i)
end do
end do
call Matrix_End('RotateAntiSymm')
end subroutine Matrix_RotateAntiSymm
subroutine Matrix_CMult_SymmRight(Mat,U,Out,a,b)
complex(dm), intent(in) :: Mat(:,:),U(:,:)
complex(dm) Out(:,:)
complex(dm), intent(in), optional :: a,b
complex(dm) mult, beta
integer n,m
call Matrix_Start('CMult_SymmRight')
m = Size(Mat,DIM=1)
n = Size(U,DIM=2)
if (n /= Size(Mat,DIM=2) .or. n/=Size(U,DIM=1)) &
call MpiStop('Matrix_CMult_SymmRight: Size mismatch')
if (present(a)) then
mult = a
else
mult = COne
end if
if (present(b)) then
beta = b
else
beta = CZero
end if
if (matrix_method == Mat_F90) then
if (beta /= CZero) then
out = a*MatMul(Mat,U) + beta*Out
else
out = MatMul(Mat,U)
if (mult /= COne) Out = Out*mult
end if
else
#ifdef MATRIX_SINGLE
call CHEMM('R','U',m,n,mult,U,Size(U,DIM=1),Mat,Size(Mat,DIM=1),beta,Out,Size(Out,DIM=1))
#else
call ZHEMM('R','U',m,n,mult,U,Size(U,DIM=1),Mat,Size(Mat,DIM=1),beta,Out,Size(Out,DIM=1))
#endif
end if
call Matrix_End('CMult_SymmRight')
end subroutine Matrix_CMult_SymmRight
subroutine Matrix_CMult_SymmLeft(Mat,U,Out,a,b)
complex(dm), intent(in) :: Mat(:,:),U(:,:)
complex(dm) Out(:,:)
complex(dm), intent(in), optional :: a,b
complex(dm) mult, beta
integer n,m
call Matrix_Start('CMult_SymmLeft')
m = Size(Mat,DIM=1)
n = Size(U,DIM=2)
if (m /= Size(U,DIM=1) .or. m/=Size(Mat,DIM=2)) &
call MpiStop('Matrix_CMult_SymmLeft: Size mismatch')
if (present(a)) then
mult = a
else
mult = COne
end if
if (present(b)) then
beta = b
else
beta = CZero
end if
if (matrix_method == Mat_F90) then
if (beta /= CZero) then
out = a*MatMul(Mat,U) + beta*Out
else
out = MatMul(Mat,U)
if (mult /= COne) Out = Out*mult
end if
else
#ifdef MATRIX_SINGLE
call CHEMM('L','U',m,n,mult,Mat,Size(Mat,DIM=1),U,Size(U,DIM=1),beta,Out,Size(Out,DIM=1))
#else
call ZHEMM('L','U',m,n,mult,Mat,Size(Mat,DIM=1),U,Size(U,DIM=1),beta,Out,Size(Out,DIM=1))
#endif
end if
call Matrix_End('CMult_SymmLeft')
end subroutine Matrix_CMult_SymmLeft
subroutine Matrix_CMult(Mat,U,Out,a,b)
! Out = a*Mat U + b*out
complex(dm), intent(in) :: Mat(:,:),U(:,:)
complex(dm) Out(:,:)
complex(dm), intent(in), optional :: a,b
complex(dm) mult, beta
integer m,n,k
call Matrix_Start('CMult')
m = Size(Mat,DIM=1)
n = Size(U,DIM=2)
k = Size(Mat,DIM=2)
if (k /= Size(U,DIM=1)) call MpiStop('Matrix_Mult: Matrix size mismatch')
if (present(a)) then
mult = a
else
mult = COne
end if
if (present(b)) then
beta = b
else
beta = CZero
end if
if (matrix_method == Mat_F90) then
if (beta /= CZero) then
out = a*MatMul(Mat,U) + beta*Out
else
out = MatMul(Mat,U)
if (mult /= COne) Out = Out*mult
end if
else
#ifdef MATRIX_SINGLE
call CGEMM('N','N',m,n,k,mult,Mat,m,U,k,beta,Out,Size(Out,Dim=1))
#else
call ZGEMM('N','N',m,n,k,mult,Mat,m,U,k,beta,Out,Size(Out,Dim=1))
#endif
end if
call Matrix_End('CMult')
end subroutine Matrix_CMult
subroutine Matrix_MultSq_RepRight(Mat,U,a)
!U = a*Mat*U
real(dm), intent(in) :: Mat(:,:)
real(dm), intent(inout) ::U(:,:)
real(dm), intent(in), optional :: a
real(dm) aa
integer m,n
real(dm), dimension(:,:), allocatable :: tmp
m = Size(Mat,DIM=1)
n = Size(Mat,DIM=2)
if (m /= n) call MpiStop('Matrix_MultSq: Matrix size mismatch')
m = Size(U,DIM=1)
n = Size(U,DIM=2)
if (m /= n) call MpiStop('Matrix_MultSq: Matrix size mismatch')
allocate(tmp(n,n))
if (present(a)) then
aa=a
else
aa=ROne
end if
call Matrix_Mult(Mat,U,tmp,aa)
U = tmp
deallocate(tmp)
end subroutine Matrix_MultSq_RepRight
subroutine Matrix_MultTri(Mat,L, side)
! Mat -> L Mat or Mat L where L is lower triangular
real(dm), intent(inout) :: Mat(:,:)
real(dm), intent(in) :: L(:,:)
character(LEN=*), intent(in) :: side
integer m,n
call Matrix_Start('Matrix_MultTri')
m = Size(Mat,DIM=1)
n = Size(Mat,DIM=2)
if (side(1:1)=='L') then
if (Size(L,DIM=2) /= m) call MpiStop('Matrix_MultTri: Matrix size mismatch')
else
if (Size(L,DIM=1) /= n) call MpiStop('Matrix_MultTri: Matrix size mismatch')
end if
#ifdef MATRIX_SINGLE
call STRMM(side,'Lower','Not-Transpose','Not-unit',m,n,ROne,L,Size(L,DIM=1),Mat,Size(Mat,Dim=1))
#else
call DTRMM(side,'Lower','Not-Transpose','Not-unit',m,n,ROne,L,Size(L,DIM=1),Mat,Size(Mat,Dim=1))
#endif
call Matrix_End('Matrix_MultTri')
end subroutine Matrix_MultTri
subroutine Matrix_Mult(Mat,U,Out,a,b)
! Out = a*Mat U + b*out
real(dm), intent(in) :: Mat(:,:),U(:,:)
real(dm) :: Out(:,:)
real(dm), intent(in), optional :: a,b