-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunderwood.F90
1365 lines (1340 loc) · 42.4 KB
/
underwood.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
!--------------------------------------------------
!
! underwood.F90
! module: underwood_mod
! requirements: rand_mod
!
! created by Kun Fang
!
! This is the core module for block-lanczos method which can
! solve the eigenproblem of a real symmetric or complex
! hermitian matrix. The subroutines in this module is revised
! based on the famous block lanczos subroutines written by
! Richard Underwood. His program can only deal with real
! symmetric matrix. I made neccessary changes so that it can
! be used on complex hermitian matrices.
!
! The only interface provided in the module is the first
! subroutine: MINVAL. Please refer to the introductions below
! for usage of the interface.
!
!------------------------------------------------------
module underwood_mod
integer,private,parameter::INMAX=1000000
real(8),private,save::ERRC
contains
!*****************************************************************
!*****************************************************************
! AN ITERATIVE BLOCK LANCZOS METHOD FOR THE
! SOLUTION OF LARGE SPARSE SYMMETRIC
! EIGENPROBLEMS
! by
! Richard Ray Underwood
!
! Revised to a Fortran 90 version
! by
! Kun Fang
!
!
! if further details concerning the content of the code are
! required, you are advised to refer to R. R. Underwood's
! PhD thesis - "An Iterative Block Lanczos Method for the
! Solution of Large Sparse Symmetric Eigenproblems", 1975.
!*****************************************************************
!*****************************************************************
!
!
!
!
SUBROUTINE MINVAL(N,Q,PINIT,R,MMAX,EPS,OP,M,D,X,IECODE)
!=================================================================
! this subroutine is the main subroutine implementing the
! iterative block lanczos method for computing the
! eigenvalues and eigenvectors of symmetric matrices.
!=================================================================
IMPLICIT NONE
INTEGER N , Q , PINIT , R , MMAX , M,I,J
REAL(8) EPS
EXTERNAL OP
REAL(8) D(Q)
COMPLEX(8) X(N,Q) , C(Q,Q),TEST
INTEGER IECODE
REAL(8) E(Q)
COMPLEX(8) U(INMAX) , V(INMAX)
INTEGER P , S , PS , K , ITER , IMM , NCONV
!
!------
! description of parameters:
! N: integer variable. The order of the symmetric
! matrix A whose eigenvalues and eigenvectors are
! being computed. The value of N should be less than
! or equal to 1296 and greater than or equal to 2.
!
! Q: integer variable. The number of vectors of length
! N contained in the array X. The value of Q should
! be less than or equal to 25, at least one greater
! than the value of R and less than or equal to N.
!
! PINIT: integer variable. The initial block size to be used
! in the block lanczos method. If PINIT is negative,
! then -PINIT is used for the block size and columns
! M+L,...,M+(-PINIT) of the array X are assumed to be
! initialized to the initial matrix used to start the
! block lanczos method. If the subroutine terminates
! with a value of M less than R, then PINIT is assigned
! a value -P where P is the final block size chosen.
! In this circumstance, columns M+1,...M+P will contain
! the most recent set of eigenvector approximations
! which can be used to restart the subroutine if desired.
!
! R: integer variable. The number of eigenvalues and
! eigenvectors being computed. That is, MINVAL attempts
! to compute accurate approximations to the R least
! eigenvalues and eigenvectors of the matrix A. The value
! of R should be greater than zero and less than Q.
!
! MMAX: integer variable. The maximum number of matrix-vector
! products A*X (where X is a vector) that are allowed
! during one call of this subroutine to complete its task
! of computing R eigenvalues and eigenvectors. Unless the
! problem indicates otherwise, MMAX should be given a very
! large value.
!
! EPS: REAL(8) variable. Initially, EPS should contain a value
! indicating the relative precision to which MINVAL will
! attempt to compute the eigenvalues and eigenvectors of A.
! For eigenvalues less in modulus than 1, EPS will be an
! absolute tolerance. Because of the way this method works,
! it may happen that the later eigenvalues cannot be
! computed to the same relative precision as those less in
! value.
!
! OP: subroutine name. The actual argument corresponding to OP
! should be the name of a subroutine used to define the
! matrix A. This subroutine should have three arguments
! N, U, and V, say, where N is an integer variable giving
! the order of A, and U and V are two one-dimensional
! arrays of length N. If W denotes the vector of order N
! stored in U, then the statement
! CALL OP(N,U,V)
! should result in the vector A*W being computed and stored
! in V. The contents of U can be modified by this call.
!
! M: integer variable. M gives the number of eigenvalues and
! eigenvectors already computed. Thus, initially, M should
! be zero. If M is greater than zero, then columns one
! through M of the array X are assumed to contain the
! computed approximations to the M least eigenvalues and
! eigenvectors of A. On exit, M contains a value equal to
! the total number of eigenvalues and eigenvectors
! computed including any already computed when MINVAL was
! entered. Thus, on exit, the first M elements of D and the
! first M columns of X will contain approximations to the
! M least eigenvalues of A.
!
! D: REAL(8) array. D contains the computed eigenvalues. D
! should be a one-dimensional array with at least Q
! elements.
!
! X: REAL(8) array. X contains the computed eigenvectors. X
! should be an array containing at least N*Q elements. X
! is used not only to store the eigenvectors computed by
! MINVAL, but also as working storage for the block lanczos
! method. On exit, the first N*M elements of X contain the
! eigenvector approximations - the first vector in the
! first N elements, the second in the second N elements,
! etc...
!
!
! IECODE:integer variable. The value of IECODE indicates whether
! MINVAL terminated successfully, and if not, the reason
! why.
!
! IECODE=0 : successful termination.
! IECODE=1 : the value of N is less than 2.
! IECODE=2 : the value of N exceeds MAX.
! IECODE=3 : the value of R is less than 1.
! IECODE=4 : the value of Q is less than or equal to R.
! IECODE=5 : the value of Q is greater than 25.
! IECODE=6 : the value of Q exceeds N.
! IECODE=7 : the value of MMAX was exceeded before R
! eigenvalues and eigenvectors were
! computed.
!
! Note that the subroutine has been designed to allow initial
! approximations to the eigenvectors corresponding to the least
! eigenvalues to be utilised if they are known (by storing them
! in X and assigning PINIT minus the value of their number).
! Furthermore, it has also been designed to allow restarting if
! it stops with IECODE=7. Thus, the user of this program can
! restart it after examining any partial results without loss of
! previous work.
!------
!
!------
! check that the initial values of the subroutine parameters
! are in range.
!------
IF ( N<2 ) THEN
IECODE = 1
RETURN
ELSEIF ( N>INMAX ) THEN
IECODE = 2
RETURN
ELSEIF ( R<1 ) THEN
IECODE = 3
RETURN
ELSEIF ( Q<=R ) THEN
IECODE = 4
RETURN
! ELSEIF ( Q>25 ) THEN
! IECODE = 5
! RETURN
ELSEIF ( Q>N ) THEN
IECODE = 6
RETURN
ELSE
!
!------
! choose initial values for the block size P, the number of
! steps that the block lanczos method is carried out, and
! choose an initial N-by-P orthonormal matrix X1 used to start
! the block lanczos method.
!------
P = PINIT
C(1:Q,1:Q) = (0.D0,0.D0)
IF ( P<0 ) P = -P
S = (Q-M)/P
IF ( S<=2 ) THEN
S = 2
P = (Q-M)/2
ENDIF
IF ( PINIT>=0 ) THEN
DO K = M+1 , M+P
CALL RANDOM(N,Q,K,X)
ENDDO
ENDIF
IF ( M<=0 ) THEN
CALL ORTHG(N,Q,M,P,C,X)
!
!------
! rotate the initial N-by-P matrix X1 so that
! X1'*A*X1=diag(D1,D2,...,DP)
! where DI is stored in D(I), I=1,...,P.
!------
CALL SECTN(N,Q,M,P,OP,X,C,D,U,V)
ERRC = 0.D0
ENDIF
ITER = 0
IMM = 0
!
!------
! the main body of the subroutine starts here. IMM
! counts the number of matrix-vector products computed,
! which is the number of times the subroutine named by
! OP is called. ERRC measures the accumulated error in
! the eigenvalues and eigenvectors.
!------
!
20 IF ( M>=R ) THEN
!
!------
! this is the end of the main body of the subroutine. Now set
! the value of IECODE and EXIT.
!------
IECODE = 0
RETURN
ELSEIF ( IMM>MMAX ) THEN
IECODE = 7
PINIT = -P
ELSE
ITER = ITER + 1
PS = P*S
!
!------
! BKLANC carries out the block lanczos method and stores
! the resulting block tridiagonal matrix MS in C and the
! N-by-PS orthonormal matrix XS in X. The initial N-by-P
! orthonormal matrix is assumed to be stored in columns
! M+1 through M+PS of X. The residuals for these vectors
! and the eigenvalue approximations in D are computed and
! stored in E.
!------
CALL BKLANC(N,Q,M,P,S,OP,D,C,X,E,U,V)
!
!------
! EIGEN solves the eigenproblem for MS, storing the eigenvalues
! in elements M+1 through M+PS of D and the eigenvectors in the
! first P*S rows and columns of C (overwriting MS, possibly).
!------
CALL EIGEN(Q,M,P,PS,C,D)
!
!------
! CNVTST determines how many of the eigenvalues and eigenvectors
! have converged using the error estimates stored in E. The number
! that have converged is stored in NCONV. If NCONV=0, then none
! have converged.
!------
CALL CNVTST(N,Q,M,P,EPS,D,E,NCONV)
!
!------
! PCH chooses new values for P and S, the block size and the
! number of steps for the block lanczos subprogram, respectively.
!------
CALL PCH(N,Q,M,R,NCONV,P,S)
!
!------
! ROTATE computes the eigenvectors of the restricted matrix
! using XS stored in X and the eigenvectors of MS stored in C.
! These vectors serve both as eigenvector approximations and
! to form the matrix used to start the block lanczos method in
! the next iteration.
!------
CALL ROTATE(N,Q,M,PS,NCONV+P,C,X)
!
M = M + NCONV
IMM = IMM + P*S
!WRITE (*,"(' =>ITER,IMM,P,PS =',4I5)") ITER , IMM , P , PS
GOTO 20
ENDIF
ENDIF
!
END SUBROUTINE
!
!
!
!
SUBROUTINE BKLANC(N,Q,M,P,S,OP,D,C,X,E,U,V)
!====================================================================
! this subroutine implements the block lanczos method
! with reorthogonalization. BKLANC computes a block
! tridiagonal matrix MS which it stores in rows and
! columns M+1 through M+P*S of the array C, and an
! orthonormal matrix XS which it stores in columns M+1
! through M+P*S of the N-by-Q array X. MS is a symmetric
! matrix with P-by-P symmetric matrices M(1),...,M(S) on
! its diagonal and P-by-P upper triangular matrices
! R(2),...,R(S) along its lower diagonal. Since MS is
! symmetric and banded, only its lower triangle (P+1
! diagonals) is stored in C. XS is composed of S N-by-P
! orthonormal matrices X(1),...,X(S) where X(1) is given
! and should be stored in columns M+1 through M+P of X.
! Furthermore, X(1) is assumed to satisfy X(1)*A*X(1) =
! diag(D(M+1),D(M+2),...,D(M+P)), and if M>0, then X(1) is
! assumed to be orthogonal to the vectors stored in columns
! 1 through M of X. OP is the name of an external subroutine
! used to define the matrix A. During the first step, the
! subroutine ERR is called and the quantities EJ are computed
! where EJ=||A*X1J-D(M+J)*X1J||, X1J is the J-th column of x(1),
! and ||*|| denotes the Euclidean norm. EJ is stored in E(M+J),
! J=1,2,...,P. U and V are auxilliary vectors used by OP.
!====================================================================
IMPLICIT NONE
INTEGER N,Q,M,P,S
EXTERNAL OP
REAL(8) D(Q)
REAL(8) E(Q)
COMPLEX(8) C(Q,Q),X(N,Q),U(N),V(N)
COMPLEX(8) T
INTEGER MP1,MPPS,L,LL,LU,K,I,J,IT,KP1,IL,ii
!
MP1 = M+1
MPPS = M+P*S
DO L = 1 , S
LL = M + (L-1)*P + 1
LU = M + L*P
DO K = LL , LU
U(1:N) = X(1:N,K)
CALL OP(N,U,V)
IF ( L>1 ) THEN
DO I = K , LU
T = 0
DO J = 1 , N
T = T + V(J)*CONJG(X(J,I))
ENDDO
C(I,K) = T
ENDDO
IT = K - P
DO I = 1 , N
T = 0
DO J = IT , K
T = T + X(I,J)*CONJG(C(K,J))
ENDDO
IF ( K==LU ) THEN
V(I) = V(I) - T
ELSE
KP1 = K + 1
DO J = KP1 , LU
T = T + X(I,J)*C(J,K)
ENDDO
ENDIF
ENDDO
ELSE
C(K:LU,K) = 0.D0
C(K,K) = D(K)
V = V - D(K)*X(1:N,K)
ENDIF
IF ( L==S ) CYCLE
X(1:N,K+P) = V(1:N)
ENDDO
IF ( L==1 ) CALL ERR(N,Q,M,P,X,E)
IF ( L==S ) CYCLE
CALL ORTHG(N,Q,LU,P,C,X)
IL = LU + 1
IT = LU
DO J = 1 , P
IT = IT + 1
C(IL:IT,IT-P) = C(IL:IT,IT)
ENDDO
ENDDO
!
END SUBROUTINE
!
!
!
!
SUBROUTINE PCH(N,Q,M,R,NCONV,P,S)
!====================================================================
! based on the values of N, Q, M, R and NCONV, PCH chooses new
! values for P and S, the block size and number of steps for the
! block lanczos method. The strategy used here is to choose P to
! be the smaller of the two following values:
! 1) the previous block size
! and, 2) the number of values left to be computed. S is chosen
! as large as possible subject to the constraints imposed by the
! limits of storage. In any event, S is greater than or equal to
! 2. N is the order of the problem and Q is the number of vectors
! available for storing eigenvectors and applying the block
! lanczos method. M is the number of eigenvalues and eigenvectors
! that have already been computed and R is the required number.
! Finally, NCONV is the number of eigenvalues and eigenvectors
! that have converged in the current iteration.
!====================================================================
IMPLICIT NONE
INTEGER N , Q , M , R , NCONV , P , S
INTEGER PT , ST , MT
!
MT = M + NCONV
PT = R - MT
IF ( PT>P ) PT = P
IF ( PT>0 ) THEN
ST = (Q-MT)/PT
ELSE
P = 0
RETURN
ENDIF
!
IF ( ST>2 ) THEN
P = PT
S = ST
ELSE
ST = 2
PT = (Q-MT)/2
P = PT
S = ST
ENDIF
!
END SUBROUTINE
!
!
!
!
SUBROUTINE ERR(N,Q,M,P,X,E)
!================================================
! ERR COMPUTES THE EUCLIDEAN LENGTHS OF THE
! VECTORS STORED IN THE COLUMNS M+P+1 THROUGH
! M+P+P OF THE N-BY-Q ARRAY X AND STORES THEM
! IN ELEMENTS M+1 THROUGH M+P OF E.
!================================================
IMPLICIT NONE
!
INTEGER N , Q , M , P
REAL(8) E(Q) , T , DSQRT
COMPLEX(8) X(N,Q)
INTEGER MP1 , MPP , K , I
!
MP1 = M + P + 1
MPP = M + P + P
DO K = MP1 , MPP
T = ABS(DOT_PRODUCT(X(1:N,K),X(1:N,K)))
E(K-P) = DSQRT(T)
ENDDO
!
END SUBROUTINE
!
!
!
!
SUBROUTINE CNVTST(N,Q,M,P,EPS,D,E,NCONV)
!===========================================================
! CNVTST determines which of the P eigenvalues stored
! in elements M+1 through M+P of D have converged. ERRC
! is a measure of the accumulated error in the M
! previously computed eigenvalues and eigenvectors. ERRC
! is updated if more approximations have converged. The
! norms of the residual vectors are stored in elements
! M+1 through M+P of E. EPS is the precision to which we
! are computing the approximations. Finally, NCONV is the
! number that have converged. If NCONV=0, then none have
! converged.
!============================================================
IMPLICIT NONE
INTEGER N , Q , M , P
REAL(8) EPS
REAL(8) D(Q) , E(Q)
REAL(8) T , DSQRT
INTEGER NCONV , K , I
REAL(8) , PARAMETER :: CHEPS = 2.22D-16
!
K = 0
DO I = 1 , P
T = DABS(D(M+I))
IF ( T<1.D0 ) T = 1.D0
IF ( E(M+I)>T*(EPS+10.D0*N*CHEPS)+ERRC ) EXIT
K = I
ENDDO
NCONV = K
IF ( K==0 ) RETURN
T = ABS(DOT_PRODUCT(E(M+1:M+K),E(M+1:M+K)))
ERRC = DSQRT(ERRC**2+T)
!
END SUBROUTINE
!
!
!
!
SUBROUTINE EIGEN(Q,M,P,PS,A,D)
!=======================================================
! EIGEN solves the eigenproblem for the symmetric
! matrix MS of order PS stored in rows and columns
! M+1 through M+PS of C. The eigenvalues of MS are
! stored in elements M+1 through M+PS of D and the
! eigenvactors are stored in rows and columns 1
! through PS of C possibly overwriting MS. EIGEN
! simply re-stores MS in a manner acceptable to the
! subroutines TRED2 and TQL2. These two routines are
! available through eispack.
!=======================================================
IMPLICIT NONE
INTEGER M , P , Q , PS , PP1
COMPLEX(8) A(Q,Q)
REAL(8) C(Q,Q) , D(Q) , CR(Q,Q) , CI(Q,Q)
REAL(8) DD(Q) , V(Q) , D2(Q), S
INTEGER I , LIM , LM1 , J , IERR
!
DO I = 1 , PS
LIM = I - P
IF ( I<=P ) LIM = 1
IF ( LIM>1 ) THEN
LM1 = LIM - 1
DO J = 1 , LM1
C(I,J) = 0.D0
C(J,I) = 0.D0
ENDDO
ENDIF
DO J = LIM , I
C(I,J) = REAL(A(I+M,J+M))
IF(I/=J) C(J,I) = IMAG(A(I+M,J+M))
ENDDO
ENDDO
!
!------
! CALL EISPACK ROUTINES HERE
!------
!
CALL DIAGM(Q,PS,C,DD,CR,CI,IERR)
!
!WRITE (*,"(' => ORDER =',I4,/,(' EIGENVALUES =',10D10.4))") PS , (DD(I),I=1,PS)
!PP1 = P + 1
!DO J = 1 , PP1
!WRITE (*,"(' => J =',I4,/,(' EIGENVECTORS =',10D10.4))") J , (C(I,J),I=1,PS)
!ENDDO
A(1:PS,1:PS)=CR(1:PS,1:PS)+(0.D0,1.D0)*CI(1:PS,1:PS)
DO I = 1 , PS
D(M+I) = DD(I)
ENDDO
!
END SUBROUTINE
!
!
!
!
SUBROUTINE SECTN(N,Q,M,P,OP,X,C,D,U,V)
!==============================================================
! SECTN transforms the N-by-P orthonormal matrix X1,
! say, stored in columns M+1 through M+P of the N-by-Q
! array X so that X1'*A*X1 = diag(D1,D2,...,DP), where
! ' denotes transpose and A is a symmetric matrix of
! order N defined by the subroutine OP. The values D1,...
! ,DP are stored in elements M+1 through M+P of D. SECTN
! forms the matrix X1'*A*X1 = CP, storing CP in the array
! C. The values D1,D2,...,DP and the eigenvectors QP of CP
! are computed by EIGEN and stored in D and C respectively.
! ROTATE then carries out the transformation X1<=X1*QP.
!==============================================================
IMPLICIT NONE
INTEGER N , Q , M , P
EXTERNAL OP
REAL(8) D(Q)
COMPLEX(8) U(N) , V(N) , X(N,Q) , C(Q,Q) , T
INTEGER ICOL1 , ICOL2 , I , J , K
!
ICOL1 = M
DO J = 1 , P
ICOL1 = ICOL1 + 1
U(1:N) = X(1:N,ICOL1)
CALL OP(N,U,V)
ICOL2 = M
DO I = 1 , J
ICOL2 = ICOL2 + 1
T = 0.D0
DO K = 1 , N
T = T + CONJG(V(K))*X(K,ICOL2)
ENDDO
C(ICOL1,ICOL2) = T
ENDDO
ENDDO
CALL EIGEN(Q,M,P,P,C,D)
CALL ROTATE(N,Q,M,P,P,C,X)
END SUBROUTINE
!
!
!
!
SUBROUTINE ROTATE(N,Q,M,PS,L,C,X)
!======================================================
! ROTATE computes the first L columns of the matrix
! XS*QS where XS is an N-by-PS orthonormal matrix
! stored in columns M+1 through M+PS of the N-by-Q
! array X and QS is a PS-by-PS orthonormal matrix
! stored in rows and columns 1 through Ps of the
! array C. The result is stored in columns M+1
! through M+L of X overwriting part of XS.
!======================================================
IMPLICIT NONE
INTEGER N , Q , M , PS , L
COMPLEX(8) C(Q,Q) , X(N,Q) , V(Q) , T
INTEGER I , J , K
!
DO I = 1 , N
DO K = 1 , L
T = 0.D0
DO J = 1 , PS
T = T + X(I,M+J)*C(J,K)
ENDDO
V(K) = T
ENDDO
DO K = 1 , L
X(I,M+K) = V(K)
ENDDO
ENDDO
!
END SUBROUTINE
!
!
!
!
SUBROUTINE ORTHG(N,Q,F,P,B,X)
!=========================================================
! ORTHG reorthogonalizes the N-by-P matrix Z stored
! in columns F+1 through F+P of the N-by-Q array X
! with respect to the vectors stored in the first F
! columns of X and then decomposes the resulting
! matrix into the product of an N-by-P orthonormal
! matrix XORTH, say, and a P-by-P upper triangular
! matrix R. XORTH is stored over Z and the upper
! triangle of R is stored in rows and columns F+1
! through F+P of the Q-by-Q array B. A stable variant
! of the Gram-Schmidt orthogonalization method is
! utilised.
!=========================================================
IMPLICIT NONE
INTEGER N , Q , F , P
REAL(8) T , DSQRT , SS, ORT(F+P,F+P)
COMPLEX(8) S, QQ
COMPLEX(8) B(Q,Q) , X(N,Q)
INTEGER FP1 , FPP , K , KM1 , I , J
LOGICAL ORIG
!
IF ( P==0 ) RETURN
FP1 = F + 1
FPP = F + P
DO K = FP1 , FPP
ORIG = .TRUE.
KM1 = K - 1
50 T = 0.D0
IF ( KM1>=1 ) THEN
DO I = 1 , KM1
S = DOT_PRODUCT(X(1:N,I),X(1:N,K))
IF ( ORIG .AND. I>F ) B(I,K) = S
T = T + ABS(S)
X(1:N,K) = X(1:N,K) - S*X(1:N,I)
ENDDO
ENDIF
S = DOT_PRODUCT(X(1:N,K),X(1:N,K))
T = T + ABS(S)
IF ( ABS(S)>T/10.D0 ) THEN
SS = SQRT(ABS(S))
B(K,K) = SS
IF ( SS/=0 ) SS = 1.D0/SS
DO J = 1 , N
X(J,K) = SS*X(J,K)
ENDDO
ELSE
ORIG = .FALSE.
GOTO 50
ENDIF
ENDDO
END SUBROUTINE
!
!
!
!
SUBROUTINE RANDOM(N,Q,L,X)
use rand_mod
!======================================================
! RANDOM computes and stores a sequence of N
! pseudo-random numbers in the L-th column of the
! N-by-Q array X. RANDOM generates two sequences of
! pseudo-random numbers, filling an array with one
! sequence and using the second to access the array
! in a random fashion.
!======================================================
IMPLICIT NONE
INTEGER N , Q , L
INTEGER I
COMPLEX(8) X(N,Q)
REAL(8) RR , RI
!
DO I = 1 , N
RI = random_n(0.D0,1.D0)
RR = random_n(0.D0,1.D0)
X(I,L) = RR + (0.D0,1.D0)*RI
ENDDO
!
!
END SUBROUTINE
!
!
!
SUBROUTINE DIAGM(NM,N,H,E,ZR,ZI,IERR)
!**********************************************************************
!
! SOLVES THE COMPLEX HERMITIAN EIGENVALUE PROBLEM (H-E*S)Z=0
! FOR MATRICES H AND S OF ORDER N. ONLY THE LOWER TRIANGLE
! OF THE HERMITIAN MATRICES NEED BE SUPPLIED. IF A(I,J) IS A
! REAL MATRIX AND B IS A HERMITIAN MATRIX, THEN B IS STORED
! IN A IN THE FOLLOWING WAY (I.GE.J):
! A(I,J) = REAL ( B(I,J) )
! A(J,I) = IMAG ( B(I,J) )
! SINCE THE DIAGONAL ELEMENTS OF B ARE REAL, THERE IS NO NEED
! TO STORE THE ZERO IMAGINARY PARTS.
!
! M. WEINERT JULY 1983
!
! NM FIRST DIMENSION OF ARRAYS
! N ORDER OF MATRICES
! H HAMILTONIAN MATRIX (OVERWRITTEN ON OUTPUT)
! E EIGENVALUES
! ZR,ZI EIGENVECTORS (REAL, IMAGINARY PARTS)
! E1,E2,TAU WORK ARRAYS
! TIME TIME REQUIRED FOR DIAGONALIZATION
!
! MODIFIED FOR USE IN BEST FALL 1993 - SPRING 1994
!**********************************************************************
!
IMPLICIT NONE
INTEGER I,IERR,J,N,NM
REAL(8) T1,T2,TIME
!---> HAMILTONIAN MATRIX
REAL(8) H(NM,N)
!---> EIGENVALUES AND EIGENVECTORS
REAL(8) E(N),ZR(NM,N),ZI(NM,N)
!---> WORK ARRAYS
REAL(8) E1(N),E2(N),TAU(2,N)
!
!---> REDUCE THE STANDARD PROBLEM TO REAL TRIDIAGONAL FORM
CALL TREDC(NM,N,H,E,E1,E2,TAU)
!---> FIND EIGENVALUES AND EIGENVECTORS OF REAL TRIADIAGONAL MATRIX
ZI=0.D0
DO I=1,N
DO J=1,N
ZR(J,I)=0.0D0
ENDDO
ZR(I,I)=1.0D0
ENDDO
CALL TQL2(NM,N,E,E1,ZR,IERR)
IF(IERR.NE.0) RETURN
!---> BACK-TRANSFORM THE EIGENVECTORS TO THE STANDARD PROBLEM
CALL TRBAKC(NM,N,H,TAU,N,ZR,ZI)
END SUBROUTINE
SUBROUTINE TRED2(NM,N,A,D,E,Z)
!
IMPLICIT NONE
INTEGER I,J,K,L,N,II,NM,JP1
DOUBLE PRECISION A(NM,N),D(N),E(N),Z(NM,N)
DOUBLE PRECISION F,G,H,HH,SCALE
! ======================================================
! THIS SUBROUTINE IS A TRANSLATION OF THE ALGOL PROCEDURE TRED2,
! NUM. MATH. 11, 181-195(1968) BY MARTIN, REINSCH, AND WILKINSON.
! HANDBOOK FOR AUTO. COMP., VOL.II-LINEAR ALGEBRA, 212-226(1971).
!
! THIS SUBROUTINE REDUCES A REAL SYMMETRIC MATRIX TO A
! SYMMETRIC TRIDIAGONAL MATRIX USING AND ACCUMULATING
! ORTHOGONAL SIMILARITY TRANSFORMATIONS.
!
! ON INPUT
!
! NM MUST BE SET TO THE ROW DIMENSION OF TWO-DIMENSIONAL
! ARRAY PARAMETERS AS DECLARED IN THE CALLING PROGRAM
! DIMENSION STATEMENT.
!
! N IS THE ORDER OF THE MATRIX.
!
! A CONTAINS THE REAL SYMMETRIC INPUT MATRIX. ONLY THE
! LOWER TRIANGLE OF THE MATRIX NEED BE SUPPLIED.
!
! ON OUTPUT
!
! D CONTAINS THE DIAGONAL ELEMENTS OF THE TRIDIAGONAL MATRIX.
!
! E CONTAINS THE SUBDIAGONAL ELEMENTS OF THE TRIDIAGONAL
! MATRIX IN ITS LAST N-1 POSITIONS. E(1) IS SET TO ZERO.
!
! Z CONTAINS THE ORTHOGONAL TRANSFORMATION MATRIX
! PRODUCED IN THE REDUCTION.
!
! A AND Z MAY COINCIDE. IF DISTINCT, A IS UNALTERED.
!
! QUESTIONS AND COMMENTS SHOULD BE DIRECTED TO BURTON S. GARBOW,
! MATHEMATICS AND COMPUTER SCIENCE DIV, ARGONNE NATIONAL LABORATORY
!
! THIS VERSION DATED AUGUST 1983.
!
! ======================================================
!
DO 100 I = 1, N
!
DO 80 J = I, N
80 Z(J,I) = A(J,I)
!
D(I) = A(N,I)
100 CONTINUE
!
IF (N == 1) GO TO 510
! .......... FOR I=N STEP -1 UNTIL 2 DO -- ..........
DO 300 II = 2, N
I = N + 2 - II
L = I - 1
H = 0.0D0
SCALE = 0.0D0
IF (L < 2) GO TO 130
! .......... SCALE ROW (ALGOL TOL THEN NOT NEEDED) ..........
DO 120 K = 1, L
120 SCALE = SCALE + DABS(D(K))
!
IF (SCALE /= 0.0D0) GO TO 140
130 E(I) = D(L)
!
DO 135 J = 1, L
D(J) = Z(L,J)
Z(I,J) = 0.0D0
Z(J,I) = 0.0D0
135 CONTINUE
!
GO TO 290
!
140 DO 150 K = 1, L
D(K) = D(K) / SCALE
H = H + D(K) * D(K)
150 CONTINUE
!
F = D(L)
G = -DSIGN(DSQRT(H),F)
E(I) = SCALE * G
H = H - F * G
D(L) = F - G
! .......... FORM A*U ..........
DO 170 J = 1, L
170 E(J) = 0.0D0
!
DO 240 J = 1, L
F = D(J)
Z(J,I) = F
G = E(J) + Z(J,J) * F
JP1 = J + 1
IF (L < JP1) GO TO 220
!
DO 200 K = JP1, L
G = G + Z(K,J) * D(K)
E(K) = E(K) + Z(K,J) * F
200 CONTINUE
!
220 E(J) = G
240 CONTINUE
! .......... FORM P ..........
F = 0.0D0
!
DO 245 J = 1, L
E(J) = E(J) / H
F = F + E(J) * D(J)
245 CONTINUE
!
HH = F / (H + H)
! .......... FORM Q ..........
DO 250 J = 1, L
250 E(J) = E(J) - HH * D(J)
! .......... FORM REDUCED A ..........
DO 280 J = 1, L
F = D(J)
G = E(J)
!
DO 260 K = J, L
260 Z(K,J) = Z(K,J) - F * E(K) - G * D(K)
!
D(J) = Z(L,J)
Z(I,J) = 0.0D0
280 CONTINUE
!
290 D(I) = H
300 CONTINUE
! .......... ACCUMULATION OF TRANSFORMATION MATRICES ..........
DO 500 I = 2, N
L = I - 1
Z(N,L) = Z(L,L)
Z(L,L) = 1.0D0
H = D(I)
IF (ABS(H) < 1.0D-6) GO TO 380
!
DO 330 K = 1, L
330 D(K) = Z(K,I) / H
!
DO 360 J = 1, L
G = 0.0D0
!
DO 340 K = 1, L
340 G = G + Z(K,I) * Z(K,J)
!
DO 360 K = 1, L
Z(K,J) = Z(K,J) - G * D(K)
360 CONTINUE
!
380 DO 400 K = 1, L
400 Z(K,I) = 0.0D0
!
500 CONTINUE
!
510 DO 520 I = 1, N
D(I) = Z(N,I)
Z(N,I) = 0.0D0
520 CONTINUE
!
Z(N,N) = 1.0D0
E(1) = 0.0D0
END SUBROUTINE
!
!
SUBROUTINE TREDC(NM,N,A,D,E,E2,TAU)
!*********************************************************************
!
! COMPLEX VERSION OF THE ALGOL PROCEDURE TRED3, LINEAR
! ALGEBRA, VOL. II, WILKINSON AND REINSCH, 1971.
! REDUCES A COMPLEX HERMITIAN MATRIX, STORED AS A SINGLE
! SQUARE ARRAY, TO A REAL SYMMETRIC TRIDIAGONAL MATRIX
! USING UNITARY SIMILARITY TRANSFORMATIONS.
!
! ON INPUT
! NM MUST BE SET TO THE ROW DIMENSION OF TWO-DIMENSIONAL
! ARRAY PARAMETERS AS DECLARED IN THE CALLING PROGRAM
! DIMENSION STATEMENT.
! N IS THE ORDER OF THE MATRIX.
! A CONTAINS THE LOWER TRIANGLE OF THE COMPLEX HERMITIAN INPUT
! MATRIX. THE REAL PARTS OF THE MATRIX ELEMENTS ARE STORED
! IN THE FULL LOWER TRIANGLE OF A, AND THE IMAGINARY PARTS
! ARE STORED IN THE TRANSPOSED POSITIONS OF THE STRICT UPPER
! TRIANGLE OF A. NO STORAGE IS REQUIRED FOR THE ZERO
! IMAGINARY PARTS OF THE DIAGONAL ELEMENTS.
!
! ON OUTPUT
! A CONTAINS INFORMATION ABOUT THE UNITARY TRANSFORMATIONS
! USED IN THE REDUCTION.
! D CONTAINS THE DIAGONAL ELEMENTS OF THE THE TRIDIAGONAL
! MATRIX.
! E CONTAINS THE SUBDIAGONAL ELEMENTS OF THE TRIDIAGONAL
! MATRIX IN ITS LAST N-1 POSITIONS. E(1) IS SET TO ZERO.
! E2 CONTAINS THE SQUARES OF THE CORRESPONDING ELEMENTS OF E.
! E2 AND E MUST BE SEPARATE LOCATIONS.
! TAU CONTAINS FURTHER INFORMATION ABOUT THE TRANSFORMATIONS.
!
! M. WEINERT JUNE 1990
!*********************************************************************
IMPLICIT NONE
INTEGER I,J,K,L,N,II,NM,JM1,JP1
REAL*8 A(NM,N),D(N),E(N),E2(N),TAU(2,N)
REAL*8 F,G,H,FI,GI,HH,SI,SCALE
!
TAU(1,N) = 1.0D0
TAU(2,N) = 0.0D0
!
DO 300 I=N,2,-1
!---> USE D AND E2 HAS TEMPORARY STORAGE
DO K=1,I-1
D(K) =A(I,K)
E2(K)=A(K,I)
ENDDO
!---> SCALE ROWS
SCALE=0.0D0
DO K=1,I-1
SCALE = SCALE + ABS(D(K)) + ABS(E2(K))