IffGit has a new shared runner for building Docker images in GitLab CI. Visit https://iffgit.fz-juelich.de/examples/ci-docker-in-docker for more details.

wrapper.F 79.7 KB
Newer Older
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
# define test

      module m_wrapper

      interface blockmat
      module procedure  blockmat_d,blockmat_z
      end interface

      interface packmat
      module procedure  packmat_d,packmat_z
      end interface

      interface packmatcoul
      module procedure  packmatcoul_d,packmatcoul_z
      end interface

      interface unpackmat
      module procedure  unpackmat_d,unpackmat_z
      end interface

      interface dotprod
      module procedure  dotprod_dd, dotprod_dz, dotprod_zd, dotprod_zz
      end interface

      interface matvec
      module procedure  matvec_dpd, matvec_dpz, matvec_zpd, matvec_zpz
      end interface

      interface matmat
      module procedure  
     &     matmat_dpdp, matmat_dpzp, matmat_zpdp, matmat_zpzp,
     &                  matmat_dmzp, matmat_dpzm, matmat_dmzm,
     &                  matmat_zmdp, matmat_zpdm, matmat_zmdm,
     &                  matmat_zmzp, matmat_zpzm, matmat_zmzm
      end interface

      interface matmatmatd
      module procedure  matmatmatd_ddd, matmatmatd_dzd
      end interface

      interface diagonalize
!       module procedure  diagonalize_de,  diagonalize_dv,  diagonalize_dpe,  diagonalize_dpv,
!      &                  diagonalize_ze,  diagonalize_zv,  diagonalize_zpe,  diagonalize_zpv,
!      &                  diagonalize_deo, diagonalize_dvo, diagonalize_dpeo, diagonalize_dpvo,
!      &                  diagonalize_zeo, diagonalize_zvo, diagonalize_zpeo, diagonalize_zpvo
      module procedure  
     +     diagonalize_de,    diagonalize_dv,    diagonalize_dpe,
     +     diagonalize_dpv,   diagonalize_ze,    diagonalize_zv,
     +     diagonalize_zpe,   diagonalize_zpv,   diagonalize_deo,
     +     diagonalize_dvo,   diagonalize_dpeo,  diagonalize_dpvo,
     +     diagonalize_zeo,   diagonalize_zvo,   diagonalize_zpeo, 
     +     diagonalize_zpvo,  diagonalize_dvs,   diagonalize_dvos,
     +     diagonalize_dpvs,  diagonalize_dpvos, diagonalize_zvs,
     +     diagonalize_zvos,  diagonalize_zpvs,  diagonalize_zpvos,
     +     diagonalize_dvx,   diagonalize_dvox,  diagonalize_dpvx,
     +     diagonalize_dpvox, diagonalize_zvx,   diagonalize_zvox,
     +     diagonalize_zpvx,  diagonalize_zpvox
      end interface

      interface geteigen
      module procedure  geteigen_zpvo
      end interface

      interface inverse
      module procedure  inverse_d,  inverse_dp,  inverse_z,  inverse_zp,
     &                  inverse_d1, inverse_dp1, inverse_z1, inverse_zp1
      end interface

      interface sqrtmat
      module procedure  sqrtmat_d,  sqrtmat_dp,  sqrtmat_z,  sqrtmat_zp,
     &                  sqrtmat_d1, sqrtmat_dp1, sqrtmat_z1, sqrtmat_zp1
      end interface
      
      contains

c     --------

      function identity(n)
      implicit none
      integer, intent(in)  :: n
      integer              :: identity(n,n)
      integer              :: i,j
      identity = 0
      do i = 1,n
        identity(i,i) = 1
      enddo
      end function identity

c     --------

      function blockmat_d(a,b)
      implicit none
93
94
      real, intent(in) :: a(:,:),b(:,:)
      real             :: blockmat_d(size(a,1)+size(b,1),
95
96
97
98
99
100
101
102
103
104
105
106
107
108
     +                                  size(a,1)+size(b,1))
      integer             :: na,nb
      na = size(a,1) ; nb = size(b,1)
      if(size(a,2).ne.na) 
     +  stop 'blockmat_d: dimensions of first array differ.'
      if(size(b,2).ne.nb)
     +  stop 'blockmat_d: dimensions of second array differ.'
      blockmat_d              = 0d0
      blockmat_d(  :na,  :na) = a
      blockmat_d(na+1:,na+1:) = b
      end function blockmat_d

      function blockmat_z(a,b)
      implicit none
109
110
      complex, intent(in) :: a(:,:),b(:,:)
      complex             :: blockmat_z(size(a,1)+size(b,1),
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
     +                                     size(a,1)+size(b,1))
      integer                :: na,nb
      na = size(a,1) ; nb = size(b,1)
      if(size(a,2).ne.na)
     +  stop 'blockmat_z: dimensions of first array differ.'
      if(size(b,2).ne.nb)
     +  stop 'blockmat_z: dimensions of second array differ.'
      blockmat_z              = 0d0
      blockmat_z(  :na,  :na) = a
      blockmat_z(na+1:,na+1:) = b
      end function blockmat_z

c     --------

      function packmat_d(mat)
      implicit none
127
128
      real, intent(in)  :: mat(:,:)
      real              :: packmat_d(size(mat,1)*(size(mat,1)+1)/2)
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
      integer              :: n,nn,i,j,k
      n = size(mat,1) ; nn = n*(n+1)/2
      if(size(mat,2).ne.n) stop 'packmat_d: array dimensions differ.'
      k = 0
      do j = 1,n
        do i = 1,j
          k            = k + 1
          packmat_d(k) = mat(i,j)
# ifdef test
          if(abs(mat(j,i)-mat(i,j)).gt.1d-8)
     +       STOP 'packmat_d: input matrix not symmetric'
# endif
        enddo
      enddo
      end function packmat_d
      
      function packmatcoul_d(mat)
      implicit none
147
148
      real, intent(in)  :: mat(:,:)
      real              :: packmatcoul_d(
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
     +                                    size(mat,1)*(size(mat,1)+1)/2)
      integer              :: n,nn,i,j,k
      n = size(mat,1) ; nn = n*(n+1)/2
      if(size(mat,2).ne.n) stop 'packmat_d: array dimensions differ.'
      k = 0
      do j = 1,n
        do i = 1,j
          k            = k + 1

          packmatcoul_d(k) = (mat(i,j)+mat(j,i))/2.

!           if(abs(mat(j,i)-mat(i,j)).gt.1d-6) then
!             write(*,*) 'packmatcoul_d: input matrix not symmetric; deviation .gt. 1E-06'         
!           endif
        enddo
      enddo
      end function packmatcoul_d

      function unpackmat_d(mat)
      implicit none
169
170
      real, intent(in)  :: mat(:)
      real              :: unpackmat_d(
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
     +                             nint(sqrt(0.25d0+2*size(mat))-0.5d0),
     &                             nint(sqrt(0.25d0+2*size(mat))-0.5d0))
      integer              :: n,nn,i,j,k
      nn = size(mat) ; n = nint(sqrt(0.25d0+2*nn)-0.5d0)
      k  = 0
      do j = 1,n
        do i = 1,j
          k                = k + 1
          unpackmat_d(i,j) = mat(k)
          unpackmat_d(j,i) = mat(k)
        enddo
      enddo
      end function unpackmat_d

      

      function packmat_z(mat)
      implicit none
189
190
      complex, intent(in) :: mat(:,:)
      complex             :: packmat_z(size(mat,1)*(size(mat,1)+1)/2)
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
      integer                :: n,nn,i,j,k
      n = size(mat,1) ; nn = n*(n+1)/2
      if(size(mat,2).ne.n) stop 'packmat_z: array dimensions differ.'
      k = 0
      do j = 1,n
        do i = 1,j
          k            = k + 1
          packmat_z(k) = mat(i,j)
# ifdef test
          if(abs(conjg(mat(j,i))-mat(i,j)).gt.1d-8) 
     +                    stop 'packmat_z: input matrix not Hermitian.'
# endif
        enddo
      enddo
      end function packmat_z

      function packmatcoul_z(mat)
      implicit none
209
210
      complex, intent(in)  :: mat(:,:)
      complex              :: packmatcoul_z(
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
     +                                    size(mat,1)*(size(mat,1)+1)/2)
      integer                 :: n,nn,i,j,k
      n = size(mat,1) ; nn = n*(n+1)/2
      if(size(mat,2).ne.n) stop 'packmat_z: array dimensions differ.'
      k = 0
      do j = 1,n
        do i = 1,j
          k            = k + 1
          packmatcoul_z(k) = (mat(i,j) + conjg(mat(j,i)))/2.

          if(abs(conjg(mat(j,i))-mat(i,j)).gt.1d-4) then
            stop 'packmatcoul_z: input matrix not Hermitian; &
     & deviation .gt. 1E-04.'
          endif
        enddo
      enddo
      end function packmatcoul_z
  
      function unpackmat_z(mat)
      implicit none
231
232
      complex, intent(in)  :: mat(:)
      complex              :: unpackmat_z(
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
     +                             nint(sqrt(0.25d0+2*size(mat))-0.5d0),
     &                             nint(sqrt(0.25d0+2*size(mat))-0.5d0))
      integer                 :: n,nn,i,j,k
      nn = size(mat) ; n = nint(sqrt(0.25d0+2*nn)-0.5d0)
      k  = 0
      do j = 1,n
        do i = 1,j
          k                = k + 1
          unpackmat_z(i,j) = mat(k)
          unpackmat_z(j,i) = conjg(mat(k))
        enddo
      enddo
      end function unpackmat_z

c     --------

      function dotprod_dd(vec1,vec2)
      implicit none
251
252
      real, intent(in) :: vec1(:),vec2(:)
      real             :: dotprod_dd
253
      integer             :: n
254
      real             :: ddot
255
256
257
258
259
260
261
262
      n = size(vec1) 
      if(size(vec2).ne.n) 
     +  stop 'dotprod_dd: sizes of input vectors differ.'
      dotprod_dd = ddot(n,vec1,1,vec2,1)
      end function dotprod_dd

      function dotprod_dz(vec1,vec2)
      implicit none
263
264
265
      real,    intent(in) :: vec1(:)
      complex, intent(in) :: vec2(:)
      complex             :: dotprod_dz
266
      integer                :: n
267
      real                :: ddot
268
269
270
271
272
273
274
275
276
      n = size(vec1) 
      if(size(vec2).ne.n) 
     +  stop 'dotprod_dz: sizes of input vectors differ.'
      dotprod_dz = ddot(n,vec1,1,real(vec2),1)
     +             +(0d0,1d0)*ddot(n,vec1,1,aimag(vec2),1)
      end function dotprod_dz

      function dotprod_zd(vec1,vec2)
      implicit none
277
278
279
      complex, intent(in) :: vec1(:)
      real,    intent(in) :: vec2(:)
      complex             :: dotprod_zd
280
      integer                :: n
281
      real                :: ddot
282
283
284
285
286
287
288
289
290
      n = size(vec1) 
      if(size(vec2).ne.n)
     +  stop 'dotprod_zd: sizes of input vectors differ.'
      dotprod_zd = ddot(n,real(vec1),1,vec2,1)
     +             -(0d0,1d0)*ddot(n,aimag(vec1),1,vec2,1)
      end function dotprod_zd

      function dotprod_zz(vec1,vec2)
      implicit none
291
292
      complex, intent(in) :: vec1(:),vec2(:)
      complex             :: dotprod_zz
293
      integer                :: n
294
      complex             :: zdotc
295
296
297
298
299
300
301
302
303
304
      n = size(vec1)
      if(size(vec2).ne.n)
     +  stop 'dotprod_zz: sizes of input vectors differ.'
      dotprod_zz = zdotc(n,vec1,1,vec2,1)
      end function dotprod_zz

c     --------

      function matvec_dpd(mat,vec)
      implicit none
305
306
      real, intent(in)  :: mat(:),vec(:)
      real              :: matvec_dpd(size(vec))
307
308
309
310
311
312
313
314
315
      integer              :: nn,n
      n  = size(vec)
      nn = n*(n+1)/2 
      if(size(mat).ne.nn) stop 'matvec_dpd: input array has wrong size.'
      call dspmv('U',n,1d0,mat,vec,1,0d0,matvec_dpd,1)
      end function matvec_dpd

      function matvec_dpz(mat,vec)
      implicit none
316
317
318
319
      real,    intent(in) :: mat(:)
      complex, intent(in) :: vec(:)
      complex             :: matvec_dpz(size(vec))
      real,   allocatable :: vecr(:),veci(:)
320
321
322
323
324
325
326
327
328
329
330
331
      integer                :: nn,n
      n  = size(vec) ; allocate ( vecr(n),veci(n) )
      nn = n*(n+1)/2 
      if(size(mat).ne.nn) stop 'matvec_dpz: input array has wrong size.'
      call dspmv('U',n,1d0,mat,real(vec),1,0d0,vecr,1)
      call dspmv('U',n,1d0,mat,aimag(vec),1,0d0,veci,1)
      matvec_dpz = vecr + (0d0,1d0) * veci
      deallocate ( vecr,veci )
      end function matvec_dpz

      function matvec_zpd(mat,vec)
      implicit none
332
333
334
335
      complex, intent(in) :: mat(:)
      real,    intent(in) :: vec(:)
      complex             :: matvec_zpd(size(vec))
      real,   allocatable :: vecr(:),veci(:)
336
337
338
339
340
341
342
343
344
345
346
347
      integer                :: nn,n
      n  = size(vec) ; allocate ( vecr(n),veci(n) )
      nn = n*(n+1)/2 
      if(size(mat).ne.nn) stop 'matvec_zpd: input array has wrong size.'
      call dspmv('U',n,1d0,real(mat),vec,1,0d0,vecr,1)
      call dspmv('U',n,1d0,aimag(mat),vec,1,0d0,veci,1)
      matvec_zpd = vecr + (0d0,1d0) * veci
      deallocate ( vecr,veci )
      end function matvec_zpd
      
      function matvec_zpz(mat,vec)
      implicit none
348
349
      complex, intent(in)  :: mat(:),vec(:)
      complex              :: matvec_zpz(size(vec))
350
351
352
353
354
355
356
357
358
359
360
      integer                 :: nn,n
      n  = size(vec)
      nn = n*(n+1)/2 
      if(size(mat).ne.nn) stop 'matvec_zpz: input array has wrong size.'
      call zhpmv('U',n,(1d0,0d0),mat,vec,1,(0d0,0d0),matvec_zpz,1)
      end function matvec_zpz

c     --------

      function matmat_dpdp(mat1,mat2)
      implicit none
361
362
      real, intent(in)  :: mat1(:),mat2(:)
      real              :: matmat_dpdp(
363
364
     +                            nint(sqrt(0.25d0+2*size(mat1))-0.5d0),
     &                            nint(sqrt(0.25d0+2*size(mat1))-0.5d0))
365
      real, allocatable :: vec(:),vec2(:)
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
      integer              :: nn,n,k1,i,j,k
      nn = size(mat1)
      n  = nint(sqrt(0.25d0+2*nn)-0.5d0) ; allocate ( vec(n),vec2(n) )
      if(size(mat2).ne.nn)
     +  stop 'matmat_dpdp: second input array has wrong size.'
      k  = 0
      do i = 1,n
        vec2(:i) = mat2(k+1:k+i)
        k1       = k+2*i
        do j = i+1,n
          vec2(j) = mat2(k1)
          k1      = k1 + j
        enddo
        call dspmv('U',n,1d0,mat1,vec2,1,0d0,vec,1)
        matmat_dpdp(:,i) = vec
        k = k + i
      enddo
      deallocate ( vec,vec2 )
      end function matmat_dpdp

      function matmat_dpzp(mat1,mat2)
      implicit none
388
389
390
      real,    intent(in)  :: mat1(:)
      complex, intent(in)  :: mat2(:)
      complex              :: matmat_dpzp(
391
392
     +                            nint(sqrt(0.25d0+2*size(mat1))-0.5d0),
     &                            nint(sqrt(0.25d0+2*size(mat1))-0.5d0))
393
394
      real,    allocatable :: vecr(:),veci(:)
      complex, allocatable :: vec2(:)
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
      integer                 :: nn,n,k1,i,j,k
      nn = size(mat1)
      n  = nint(sqrt(0.25d0+2*nn)-0.5d0) 
      allocate ( vecr(n),veci(n),vec2(n) )
      if(size(mat2).ne.nn)
     +  stop 'matmat_dpzp: second input array has wrong size.'
      k  = 0
      do i = 1,n
        vec2(:i) = mat2(k+1:k+i)
        k1       = k+2*i
        do j = i+1,n
          vec2(j) = conjg(mat2(k1))
          k1      = k1 + j
        enddo
        call dspmv('U',n,1d0,mat1,real(vec2),1,0d0,vecr,1)
        call dspmv('U',n,1d0,mat1,aimag(vec2),1,0d0,veci,1)
        matmat_dpzp(:,i) = vecr + (0d0,1d0) * veci
        k = k + i
      enddo
      deallocate ( vecr,veci,vec2 )
      end function matmat_dpzp

      function matmat_zpdp(mat1,mat2)
      implicit none
419
420
421
      complex, intent(in)  :: mat1(:)
      real,    intent(in)  :: mat2(:)
      complex              :: matmat_zpdp(
422
423
     +                            nint(sqrt(0.25d0+2*size(mat1))-0.5d0),
     &                            nint(sqrt(0.25d0+2*size(mat1))-0.5d0))
424
425
      real,    allocatable :: vecr(:),veci(:)
      complex, allocatable :: vec1(:)
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
      integer                 :: nn,n,k1,i,j,k
      nn = size(mat1)
      n  = nint(sqrt(0.25d0+2*nn)-0.5d0) 
      allocate ( vecr(n),veci(n),vec1(n) )
      if(size(mat2).ne.nn)
     +  stop 'matmat_zpdp: second input array has wrong size.'
      k  = 0
      do i = 1,n
        vec1(:i) = conjg(mat1(k+1:k+i))
        k1       = k+2*i
        do j = i+1,n
          vec1(j) = mat1(k1)
          k1      = k1 + j
        enddo
        call dspmv('U',n,1d0,mat2,real(vec1),1,0d0,vecr,1)
        call dspmv('U',n,1d0,mat2,aimag(vec1),1,0d0,veci,1)
        matmat_zpdp(i,:) = vecr + (0d0,1d0) * veci
        k = k + i
      enddo
      deallocate ( vecr,veci,vec1 )
      end function matmat_zpdp

      function matmat_zpzp(mat1,mat2)
      implicit none
450
451
      complex, intent(in)  :: mat1(:),mat2(:)
      complex              :: matmat_zpzp(
452
453
     +                            nint(sqrt(0.25d0+2*size(mat1))-0.5d0),
     &                            nint(sqrt(0.25d0+2*size(mat1))-0.5d0))
454
      complex, allocatable :: vec(:),vec2(:)
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
      integer                 :: nn,n,k1,i,j,k
      nn = size(mat1)
      n  = nint(sqrt(0.25d0+2*nn)-0.5d0) ; allocate ( vec(n),vec2(n) )
      if(size(mat2).ne.nn) 
     +  stop 'matmat_zpzp: second input array has wrong size.'
      k  = 0
      do i = 1,n
        vec2(:i) = mat2(k+1:k+i)
        k1       = k+2*i
        do j = i+1,n
          vec2(j) = conjg(mat2(k1))
          k1      = k1 + j
        enddo
        call zhpmv('U',n,(1d0,0d0),mat1,vec2,1,(0d0,0d0),vec,1)
        matmat_zpzp(:,i) = vec
        k = k + i
      enddo
      deallocate ( vec,vec2 )
      end function matmat_zpzp

      function matmat_dpdm(mat1,mat2)
      implicit none
477
478
479
      real, intent(in)  :: mat1(:),mat2(:,:)
      real              :: matmat_dpdm(size(mat2,1),size(mat2,1))
      real, allocatable :: vec(:),vec2(:)
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
      integer              :: nn,n,k1,i
      n = size(mat2,1) ; nn = n*(n+1)/2 ; allocate ( vec(n),vec2(n) )
      if(size(mat2,2).ne.n)  
     +  stop 'matmat_dpdm: dimensions of second input array differ.'
      if(size(mat1)  .ne.nn) 
     +  stop 'matmat_dpdm: first input array has wrong size.'
      do i = 1,n
        vec2 = mat2(:,i)
        call dspmv('U',n,1d0,mat1,vec2,1,0d0,vec,1)
        matmat_dpdm(:,i) = vec
      enddo
      deallocate ( vec,vec2 )
      end function matmat_dpdm

      function matmat_dmdp(mat1,mat2)
      implicit none
496
497
498
      real, intent(in)  :: mat1(:,:),mat2(:)
      real              :: matmat_dmdp(size(mat1,1),size(mat1,1))
      real, allocatable :: vec(:),vec2(:)
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
      integer              :: nn,n,k1,i
      n = size(mat1,1) ; nn = n*(n+1)/2 ; allocate ( vec(n),vec2(n) )
      if(size(mat1,2).ne.n)  
     +  stop 'matmat_dmdp: dimensions of first input array differ.'
      if(size(mat2)  .ne.nn) 
     +  stop 'matmat_dmdp: second input array has wrong size.'
      do i = 1,n
        vec2 = mat1(i,:)
        call dspmv('U',n,1d0,mat2,vec2,1,0d0,vec,1)
        matmat_dmdp(i,:) = vec
      enddo
      deallocate ( vec,vec2 )
      end function matmat_dmdp

      function matmat_dmdm(mat1,mat2)
      implicit none
515
516
      real, intent(in) :: mat1(:,:),mat2(:,:)
      real             :: matmat_dmdm(size(mat1,1),size(mat1,1))
517
518
519
520
521
522
523
524
525
526
527
528
529
      integer             :: n
      n = size(mat1,1)
      if(size(mat1,2).ne.n)
     +  stop 'matmat_dmdm: dimensions of first input array differ.'
      if(size(mat2,1).ne.n)
     +  stop 'matmat_dmdm: second input array has wrong dimensions.'
      if(size(mat2,2).ne.n)
     +  stop 'matmat_dmdm: dimensions of second input array differ.'
      call dgemm('N','N',n,n,n,1d0,mat1,n,mat2,n,0d0,matmat_dmdm,n)
      end function matmat_dmdm

      function matmat_dpzm(mat1,mat2)
      implicit none
530
531
532
533
534
      real,    intent(in)  :: mat1(:)
      complex, intent(in)  :: mat2(:,:)
      complex              :: matmat_dpzm(size(mat2,1),size(mat2,1))
      real,    allocatable :: vecr(:),veci(:)
      complex, allocatable :: vec2(:)
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
      integer                 :: nn,n,k1,i
      n = size(mat2,1) 
      nn = n*(n+1)/2 ; allocate ( vecr(n),veci(n),vec2(n) )
      if(size(mat2,2).ne.n)
     +  stop 'matmat_dpzm: dimensions of second input array differ.'
      if(size(mat1)  .ne.nn)
     +  stop 'matmat_dpzm: first input array has wrong size.'
      do i = 1,n
        vec2 = mat2(:,i)
        call dspmv('U',n,1d0,mat1,real(vec2),1,0d0,vecr,1)
        call dspmv('U',n,1d0,mat1,aimag(vec2),1,0d0,veci,1)
        matmat_dpzm(:,i) = vecr + (0d0,1d0) * veci
      enddo
      deallocate ( vecr,veci,vec2 )
      end function matmat_dpzm

      function matmat_dmzp(mat1,mat2)
      implicit none
553
554
555
556
      real,    intent(in)  :: mat1(:,:)
      complex, intent(in)  :: mat2(:)
      complex              :: matmat_dmzp(size(mat1,1),size(mat1,1))
      complex, allocatable :: vec1(:),vec(:)
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
      integer                 :: nn,n,k1,i
      n = size(mat1,1) ; nn = n*(n+1)/2 ; allocate ( vec(n),vec1(n) )
      if(size(mat1,2).ne.n)
     +  stop 'matmat_dmzp: dimensions of first input array differ.'
      if(size(mat2)  .ne.nn)
     +  stop 'matmat_dmzp: second input array has wrong size.'
      do i = 1,n
        vec1 = mat1(i,:)
        call zhpmv('U',n,(1d0,0d0),mat2,vec1,1,(0d0,0d0),vec,1)
        matmat_dmzp(i,:) = conjg(vec)
      enddo
      deallocate ( vec,vec1 )
      end function matmat_dmzp

      function matmat_dmzm(mat1,mat2)
      implicit none
573
574
575
576
      real,    intent(in) :: mat1(:,:)
      complex, intent(in) :: mat2(:,:)
      complex             :: matmat_dmzm(size(mat1,1),size(mat2,2))
      real                :: matr(size(mat1,1),size(mat2,2)),
577
578
579
580
581
582
583
584
585
586
587
588
589
590
     +                          mati(size(mat1,1),size(mat2,2))
      integer                :: n,n1,n2
      n1 = size(mat1,1)
      n  = size(mat1,2)
      n2 = size(mat2,2)
      if(size(mat2,1).ne.n)
     +  stop 'matmat_dmzm: dimensions of matrices are inconsistent.'
      call dgemm('N','N',n1,n2,n,1d0,mat1,n1,real(mat2),n,0d0,matr,n1)
      call dgemm('N','N',n1,n2,n,1d0,mat1,n1,aimag(mat2),n,0d0,mati,n1)
      matmat_dmzm = matr + (0d0,1d0) * mati
      end function matmat_dmzm

      function matmat_zpdm(mat1,mat2)
      implicit none
591
592
593
594
      complex, intent(in)  :: mat1(:)
      real,    intent(in)  :: mat2(:,:)
      complex              :: matmat_zpdm(size(mat2,1),size(mat2,1))
      complex, allocatable :: vec(:),vec2(:)
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
      integer                 :: nn,n,k1,i
      n = size(mat2,1) ; nn = n*(n+1)/2 ; allocate ( vec(n),vec2(n) )
      if(size(mat2,2).ne.n)
     +  stop 'matmat_zpdm: dimensions of second input array differ.'
      if(size(mat1)  .ne.nn)
     +  stop 'matmat_zpdm: first input array has wrong size.'
      do i = 1,n
        vec2 = mat2(:,i)
        call zhpmv('U',n,(1d0,0d0),mat1,vec2,1,(0d0,0d0),vec,1)
        matmat_zpdm(:,i) = vec
      enddo
      deallocate ( vec,vec2 )
      end function matmat_zpdm

      function matmat_zmdp(mat1,mat2)
      implicit none
611
612
613
614
615
      complex, intent(in)  :: mat1(:,:)
      real,    intent(in)  :: mat2(:)
      complex              :: matmat_zmdp(size(mat1,1),size(mat1,1))
      complex, allocatable :: vec1(:)
      real,    allocatable :: vecr(:),veci(:)
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
      integer                 :: nn,n,k1,i
      n = size(mat1,1) ; nn = n*(n+1)/2 
      allocate ( vecr(n),veci(n),vec1(n) )
      if(size(mat1,2).ne.n)
     +  stop 'matmat_zmdp: dimensions of first input array differ.'
      if(size(mat2)  .ne.nn)
     +  stop 'matmat_zmdp: second input array has wrong size.'
      do i = 1,n
        vec1 = conjg(mat1(i,:))
        call dspmv('U',n,1d0,mat2,real(vec1),1,0d0,vecr,1)
        call dspmv('U',n,1d0,mat2,aimag(vec1),1,0d0,veci,1)
        matmat_zmdp(i,:) = vecr - (0d0,1d0) * veci
      enddo
      deallocate ( vecr,veci,vec1 )
      end function matmat_zmdp

      function matmat_zmdm(mat1,mat2)
      implicit none
634
635
636
637
      complex, intent(in) :: mat1(:,:)
      real,    intent(in) :: mat2(:,:)
      complex             :: matmat_zmdm(size(mat1,1),size(mat2,2))
      real                :: matr(size(mat1,1),size(mat2,2)),
638
639
640
641
642
643
644
645
646
647
648
649
650
651
     +                          mati(size(mat1,1),size(mat2,2)) 
      integer                :: n,n1,n2
      n1 = size(mat1,1)
      n  = size(mat1,2)
      n2 = size(mat2,2)
      if(size(mat2,1).ne.n)
     +  stop 'matmat_zmdm: dimensions of matrices are inconsistent.'
      call dgemm('N','N',n1,n2,n,1d0,real(mat1),n1,mat2,n,0d0,matr,n1)
      call dgemm('N','N',n1,n2,n,1d0,aimag(mat1),n1,mat2,n,0d0,mati,n1)
      matmat_zmdm = matr + (0d0,1d0) * mati
      end function matmat_zmdm

      function matmat_zpzm(mat1,mat2)
      implicit none
652
653
654
      complex, intent(in)  :: mat1(:),mat2(:,:)
      complex              :: matmat_zpzm(size(mat2,1),size(mat2,2))
      complex, allocatable :: vec(:),vec2(:)
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
      integer                 :: nn,n,k1,i,n2
      n  = size(mat2,1) ; nn = n*(n+1)/2 ; allocate ( vec(n),vec2(n) )
      n2 = size(mat2,2)
      if(size(mat1).ne.nn)
     +  stop 'matmat_zpzm: first input array has wrong size.'
      do i = 1,n2
        vec2 = mat2(:,i)
        call zhpmv('U',n,(1d0,0d0),mat1,vec2,1,(0d0,0d0),vec,1)
        matmat_zpzm(:,i) = vec
      enddo
      deallocate ( vec,vec2 )
      end function matmat_zpzm

      function matmat_zmzp(mat1,mat2)
      implicit none
670
671
672
      complex, intent(in)  :: mat1(:,:),mat2(:)
      complex              :: matmat_zmzp(size(mat1,1),size(mat1,1))
      complex, allocatable :: vec(:),vec2(:)
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
      integer                 :: nn,n,k1,i
      n = size(mat1,1) ; nn = n*(n+1)/2 ; allocate ( vec(n),vec2(n) )
      if(size(mat1,2).ne.n)
     +  stop 'matmat_zmzp: dimensions of first input array differ.'
      if(size(mat2)  .ne.nn)
     +  stop 'matmat_zmzp: second input array has wrong size.'
      do i = 1,n
        vec2 = conjg(mat1(i,:))
        call zhpmv('U',n,(1d0,0d0),mat2,vec2,1,(0d0,0d0),vec,1)
        matmat_zmzp(i,:) = conjg(vec)
      enddo
      deallocate ( vec,vec2 )
      end function matmat_zmzp

      function matmat_zmzm(mat1,mat2)
      implicit none
689
690
      complex, intent(in) :: mat1(:,:),mat2(:,:)
      complex             :: matmat_zmzm(size(mat1,1),size(mat2,2))
691
692
693
694
695
696
697
698
699
700
701
702
703
704
      integer                :: n1,n,n2
      complex, parameter     :: one = (1,0), zero = 0
      n1 = size(mat1,1)
      n  = size(mat1,2)
      n2 = size(mat2,2)
      if(size(mat2,1).ne.n)
     +  stop 'matmat_zmzm: dimensions of matrices are inconsistent.'
      call zgemm('N','N',n1,n2,n,one,mat1,n1,mat2,n,zero,matmat_zmzm,n1)
      end function matmat_zmzm

c     --------

      function matmatmatd_ddd(diag1,mat,diag2)
      implicit none
705
706
      real, intent(in) :: diag1(:),mat(:,:),diag2(:)
      real             :: matmatmatd_ddd(size(diag1),size(diag1))
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
      integer             :: n,i
      n = size(diag1)
      if(size(mat,1).ne.n) 
     +  stop 'matmatmatd_ddd: input matrix has wrong size.'
      if(size(mat,2).ne.n)
     +  stop 'matmatmatd_ddd: dimensions of input matrix differ.'
      if(size(diag2).ne.n)
     +  stop 'matmatmatd_ddd: second diagonal matrix has wrong size.'
      do i = 1,n
        matmatmatd_ddd(:,i) = diag2(i) * mat(:,i)
      enddo
      do i = 1,n
        matmatmatd_ddd(i,:) = diag1(i) * matmatmatd_ddd(i,:)
      enddo
      end function matmatmatd_ddd

      function matmatmatd_dzd(diag1,mat,diag2)
      implicit none
725
726
727
      real,    intent(in) :: diag1(:),diag2(:)
      complex, intent(in) :: mat(:,:)
      complex             :: matmatmatd_dzd(size(diag1),size(diag1))
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
      integer                :: n,i
      n = size(diag1)
      if(size(mat,1).ne.n)
     +  stop 'matmatmatd_ddd: input matrix has wrong size.'
      if(size(mat,2).ne.n)
     +  stop 'matmatmatd_ddd: dimensions of input matrix differ.'
      if(size(diag2).ne.n)
     +  stop 'matmatmatd_ddd: second diagonal matrix has wrong size.'
      do i = 1,n
        matmatmatd_dzd(:,i) = diag2(i) * mat(:,i)
      enddo
      do i = 1,n
        matmatmatd_dzd(i,:) = diag1(i) * matmatmatd_dzd(i,:)
      enddo
      end function matmatmatd_dzd

c     --------
      
      subroutine diagonalize_de(eval,mat)
      implicit none
748
749
750
      real, intent(out) :: eval(:)
      real, intent(in)  :: mat(:,:)
      real, allocatable :: mat1(:,:),work(:)
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
      integer              :: n,info
      n = size(mat,1)
      if(n.eq.0)
     +  stop 'diagonalize_de: zero dimension in eigenvalue problem.'
      if(size(mat,2).ne.n)
     +  stop 'diagonalize_de: dimensions of input matrix differ.'
      if(size(eval) .ne.n)
     +  stop 'diagonalize_de: eigenvalue array has wrong size.'
      allocate ( mat1(n,n),work(3*n) ) ; mat1 = mat
      call dsyev('N','U',n,mat1,n,eval,work,3*n,info) 
      if(info.ne.0) stop 'diagonalize_de: dsyev failed.'
      deallocate ( mat1,work )
      end subroutine diagonalize_de

      subroutine diagonalize_dv(evec,eval,mat)
      implicit none
767
768
769
      real, intent(out) :: eval(:),evec(:,:)
      real, intent(in)  :: mat(:,:)
      real, allocatable :: work(:)
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
      integer              :: n,info
      n = size(mat,1)
      if(n.eq.0)
     +  stop 'diagonalize_dv: zero dimension in eigenvalue problem.'
      if(size(mat,2) .ne.n)
     +  stop 'diagonalize_dv: dimensions of input matrix differ.'
      if(size(eval)  .ne.n)
     +  stop 'diagonalize_dv: eigenvalue array has wrong size.'
      if(size(evec,1).ne.n)
     +  stop 'diagonalize_dv: eigenvector array has wrong dimensions.'
      if(size(evec,2).ne.n)
     +  stop 'diagonalize_dv: dimensions of eigenvector array differ.'
      allocate ( work(3*n) ) ; evec = mat
      call dsyev('V','U',n,evec,n,eval,work,3*n,info) 
      if(info.ne.0) stop 'diagonalize_dv: dsyev failed.'
      deallocate ( work )
      end subroutine diagonalize_dv

      subroutine diagonalize_dpe(eval,mat)
      implicit none
790
791
792
      real, intent(out) :: eval(:)
      real, intent(in)  :: mat(:)
      real, allocatable :: mat1(:),work(:)
793
794
795
796
797
798
799
800
801
802
803
804
805
806
      integer              :: n,nn,info
      n = size(eval,1) ; nn = n*(n+1)/2
      if(n.eq.0)
     +  stop 'diagonalize_dpe: zero dimension in eigenvalue problem.'
      if(size(mat).ne.nn)
     +  stop 'diagonalize_dpe: input matrix has wrong size.'
      allocate ( mat1(nn),work(3*n) ) ; mat1 = mat
      call dspev('N','U',n,mat1,eval,work,n,work,info) 
      if(info.ne.0) stop 'diagonalize_dpe: dspev failed.'
      deallocate ( mat1,work )
      end subroutine diagonalize_dpe

      subroutine diagonalize_dpv(evec,eval,mat)
      implicit none
807
808
809
      real, intent(out) :: eval(:),evec(:,:)
      real, intent(in)  :: mat(:)
      real, allocatable :: mat1(:),work(:)
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
      integer              :: n,nn,info
      n = size(eval,1) ; nn = n*(n+1)/2
      if(n.eq.0)
     +  stop 'diagonalize_dpv: zero dimension in eigenvalue problem.'
      if(size(mat)   .ne.nn)
     +  stop 'diagonalize_dpv: input matrix has wrong size.'
      if(size(evec,1).ne.n)
     +  stop 'diagonalize_dpv: eigenvector array has wrong dimensions.'
      if(size(evec,2).ne.n)
     +  stop 'diagonalize_dpv: dimensions of eigenvector array differ.'
      allocate ( mat1(nn),work(3*n) ) ; mat1 = mat
      call dspev('V','U',n,mat1,eval,evec,n,work,info) 
      if(info.ne.0) stop 'diagonalize_dpv: dspev failed.'
      deallocate ( mat1,work )
      end subroutine diagonalize_dpv

      subroutine diagonalize_ze(eval,mat)
      implicit none
828
829
830
831
      real,    intent(out) :: eval(:)
      complex, intent(in)  :: mat(:,:)
      complex, allocatable :: mat1(:,:),work(:)
      real,    allocatable :: rwork(:)
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
      integer                 :: n,info
      n = size(mat,1)
      if(n.eq.0)
     +  stop 'diagonalize_ze: zero dimension in eigenvalue problem.'
      if(size(mat,2).ne.n)
     +  stop 'diagonalize_ze: dimensions of input matrix differ.'
      if(size(eval) .ne.n)
     +  stop 'diagonalize_ze: eigenvalue array has wrong size.'
      allocate ( mat1(n,n),work(3*n),rwork(3*n) ) ; mat1 = mat
      call zheev('N','U',n,mat1,n,eval,work,3*n,rwork,info) 
      if(info.ne.0) stop 'diagonalize_ze: zheev failed.'
      deallocate ( mat1,work,rwork )
      end subroutine diagonalize_ze

      subroutine diagonalize_zv(evec,eval,mat)
      implicit none
848
849
850
851
852
      real,    intent(out) :: eval(:)
      complex, intent(out) :: evec(:,:)
      complex, intent(in)  :: mat(:,:)
      complex, allocatable :: work(:)
      real,    allocatable :: rwork(:)
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
      integer                 :: n,info
      n = size(mat,1)
      if(n.eq.0)
     +  stop 'diagonalize_zv: zero dimension in eigenvalue problem.'
      if(size(mat,2) .ne.n)
     +  stop 'diagonalize_zv: dimensions of input matrix differ.'
      if(size(eval)  .ne.n)
     +  stop 'diagonalize_zv: eigenvalue array has wrong size.'
      if(size(evec,1).ne.n)
     +  stop 'diagonalize_zv: eigenvector array has wrong dimensions.'
      if(size(evec,2).ne.n)
     +  stop 'diagonalize_zv: dimensions of eigenvector array differ.'
      allocate ( work(3*n),rwork(3*n) ) ; evec = mat
      call zheev('V','U',n,evec,n,eval,work,3*n,rwork,info) 
      if(info.ne.0) stop 'diagonalize_zv: zheev failed.'
      deallocate ( work,rwork )
      end subroutine diagonalize_zv

      subroutine diagonalize_zpe(eval,mat)
      implicit none
873
874
875
876
      real,    intent(out) :: eval(:)
      complex, intent(in)  :: mat(:)
      complex, allocatable :: mat1(:),work(:)
      real,    allocatable :: rwork(:)
877
878
879
880
881
882
883
884
885
886
887
888
889
890
      integer                 :: n,nn,info
      n = size(eval,1) ; nn = n*(n+1)/2
      if(n.eq.0)
     +  stop 'diagonalize_zpe: zero dimension in eigenvalue problem.'
      if(size(mat).ne.nn)
     +  stop 'diagonalize_zpe: input matrix has wrong size.'
      allocate ( mat1(nn),work(3*n),rwork(3*n) ) ; mat1 = mat
      call zhpev('N','U',n,mat1,eval,work,n,work,rwork,info) 
      if(info.ne.0) stop 'diagonalize_zpe: zhpev failed.'
      deallocate ( mat1,work,rwork )
      end subroutine diagonalize_zpe

      subroutine diagonalize_zpv(evec,eval,mat)
      implicit none
891
892
893
894
895
      real,    intent(out) :: eval(:)
      complex, intent(out) :: evec(:,:)
      complex, intent(in)  :: mat(:)
      complex, allocatable :: mat1(:),work(:)
      real,    allocatable :: rwork(:)
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
      integer                 :: n,nn,info
      n = size(eval,1) ; nn = n*(n+1)/2
      if(n.eq.0)
     +  stop 'diagonalize_zpv: zero dimension in eigenvalue problem.'
      if(size(mat)   .ne.nn)
     +  stop 'diagonalize_zpv: input matrix has wrong size.'
      if(size(evec,1).ne.n)
     +  stop 'diagonalize_zpv: eigenvector array has wrong dimensions.'
      if(size(evec,2).ne.n)
     +  stop 'diagonalize_zpv: dimensions of eigenvector array differ.'
      allocate ( mat1(nn),work(3*n),rwork(3*n) ) ; mat1 = mat 
      call zhpev('V','U',n,mat1,eval,evec,n,work,rwork,info) 
      if(info.ne.0) stop 'diagonalize_zpv: zhpev failed.'
      deallocate ( mat1,work,rwork )
      end subroutine diagonalize_zpv

      subroutine diagonalize_deo(eval,mat,olap)
      implicit none
914
915
916
      real, intent(out) :: eval(:)
      real, intent(in)  :: mat(:,:),olap(:,:)
      real, allocatable :: mat1(:,:),olap1(:,:),work(:)
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
      integer              :: n,info
      n = size(mat,1)
      if(n.eq.0)
     +  stop 'diagonalize_deo: zero dimension in eigenvalue problem.'
      if(size(mat,2) .ne.n)
     +  stop 'diagonalize_deo: dimensions of input matrix differ.'
      if(size(eval)  .ne.n)
     +  stop 'diagonalize_deo: eigenvalue array has wrong size.'
      if(size(olap,1).ne.n)
     +  stop 'diagonalize_deo: overlap matrix has wrong size.'
      if(size(olap,2).ne.n)
     +  stop 'diagonalize_deo: dimensions of overlap matrix differ.'
      allocate ( mat1(n,n),olap1(n,n),work(3*n) ) 
      mat1 = mat ; olap1 = olap
      call dsygv(1,'N','U',n,mat1,n,olap1,n,eval,work,3*n,info)
      if(info.ne.0) stop 'diagonalize_deo: dsygv failed.'
      deallocate ( mat1,olap1,work )
      end subroutine diagonalize_deo

      subroutine diagonalize_dvo(evec,eval,mat,olap)
      implicit none
938
939
940
      real, intent(out) :: eval(:),evec(:,:)
      real, intent(in)  :: mat(:,:),olap(:,:)
      real, allocatable :: olap1(:,:),work(:)
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
      integer              :: n,info
      n = size(mat,1)
      if(n.eq.0)
     +  stop 'diagonalize_dvo: zero dimension in eigenvalue problem.'
      if(size(mat,2) .ne.n)
     +  stop 'diagonalize_dvo: dimensions of input matrix differ.'
      if(size(eval)  .ne.n)
     +  stop 'diagonalize_dvo: eigenvalue array has wrong size.'
      if(size(evec,1).ne.n)
     +  stop 'diagonalize_dvo: eigenvector array has wrong dimensions.'
      if(size(evec,2).ne.n)
     +  stop 'diagonalize_dvo: dimensions of eigenvector array differ.'
      if(size(olap,1).ne.n)
     +  stop 'diagonalize_dvo: overlap matrix has wrong dimensions.'
      if(size(olap,2).ne.n)
     +  stop 'diagonalize_dvo: dimensions of overlap matrix differ.'
      allocate ( olap1(n,n),work(3*n) ) ; evec = mat ; olap1 = olap
      call dsygv(1,'V','U',n,evec,n,olap1,n,eval,work,3*n,info) 
      if(info.ne.0) stop 'diagonalize_dvo: dsygv failed.'
      deallocate ( olap1,work )
      end subroutine diagonalize_dvo

      subroutine diagonalize_dpeo(eval,mat,olap)
      implicit none
965
966
967
      real, intent(out) :: eval(:)
      real, intent(in)  :: mat(:),olap(:)
      real, allocatable :: mat1(:),olap1(:),work(:)
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
      integer              :: n,nn,info
      n = size(eval,1) ; nn = n*(n+1)/2
      if(n.eq.0)
     +  stop 'diagonalize_dpeo: zero dimension in eigenvalue problem.'
      if(size(mat) .ne.nn)
     +  stop 'diagonalize_dpeo: input matrix has wrong size.'
      if(size(olap).ne.nn)
     +  stop 'diagonalize_dpeo: overlap matrix has wrong size.'
      allocate ( mat1(nn),olap1(nn),work(3*n) ) 
      mat1 = mat ; olap1 = olap
      call dspgv(1,'N','U',n,mat1,olap1,eval,work,n,work,info) 
      if(info.ne.0) stop 'diagonalize_dpeo: dspgv failed.'
      deallocate ( mat1,olap1,work )
      end subroutine diagonalize_dpeo

      subroutine diagonalize_dpvo(evec,eval,mat,olap)
      implicit none
985
986
987
      real, intent(out) :: eval(:),evec(:,:)
      real, intent(in)  :: mat(:),olap(:)
      real, allocatable :: mat1(:),olap1(:),work(:)
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
      integer              :: n,nn,info
      n = size(eval,1) ; nn = n*(n+1)/2
      if(n.eq.0)
     +  stop 'diagonalize_dpvo: zero dimension in eigenvalue problem.'
      if(size(mat)   .ne.nn)
     +  stop 'diagonalize_dpvo: input matrix has wrong size.'
      if(size(olap)  .ne.nn)
     +  stop 'diagonalize_dpvo: overlap matrix has wrong size.'
      if(size(evec,1).ne.n)
     +  stop 'diagonalize_dpvo: eigenvector array has wrong dimensions.'
      if(size(evec,2).ne.n)
     +  stop 'diagonalize_dpvo: dimensions of eigenvector array differ.'
      allocate ( mat1(nn),olap1(nn),work(3*n) ) 
      mat1 = mat ; olap1 = olap
      call dspgv(1,'V','U',n,mat1,olap1,eval,evec,n,work,info) 
      if(info.ne.0) stop 'diagonalize_dpvo: dspgv failed.'
      deallocate ( mat1,olap1,work )
      end subroutine diagonalize_dpvo

      subroutine diagonalize_zeo(eval,mat,olap)
      implicit none
1009
1010
1011
1012
      real,    intent(out) :: eval(:)
      complex, intent(in)  :: mat(:,:),olap(:,:)
      complex, allocatable :: mat1(:,:),olap1(:,:),work(:)
      real,    allocatable :: rwork(:)
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
      integer                 :: n,info
      n = size(mat,1)
      if(n.eq.0)
     +  stop 'diagonalize_zeo: zero dimension in eigenvalue problem.'
      if(size(mat,2) .ne.n)
     +  stop 'diagonalize_zeo: dimensions of input matrix differ.'
      if(size(eval)  .ne.n)
     +  stop 'diagonalize_zeo: eigenvalue array has wrong size.'
      if(size(olap,1).ne.n)
     +  stop 'diagonalize_zeo: overlap matrix has wrong size.'
      if(size(olap,2).ne.n)
     +  stop 'diagonalize_zeo: dimensions of overlap matrix differ.'
      allocate ( mat1(n,n),olap1(n,n),work(3*n),rwork(3*n) ) 
      mat1 = mat ; olap1 = olap
      call zhegv(1,'N','U',n,mat1,n,olap1,n,eval,work,3*n,rwork,info) 
      if(info.ne.0) stop 'diagonalize_zeo: zhegv failed.'
      deallocate ( mat1,olap1,work,rwork )
      end subroutine diagonalize_zeo

      subroutine diagonalize_zvo(evec,eval,mat,olap)
      implicit none
1034
1035
1036
1037
1038
      real,    intent(out) :: eval(:)
      complex, intent(out) :: evec(:,:)
      complex, intent(in)  :: mat(:,:),olap(:,:)
      complex, allocatable :: olap1(:,:),work(:)
      real,    allocatable :: rwork(:)
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
      integer                 :: n,info
      n = size(mat,1)
      if(n.eq.0)
     +  stop 'diagonalize_zvo: zero dimension in eigenvalue problem.'
      if(size(mat,2) .ne.n)
     +  stop 'diagonalize_zvo: dimensions of input matrix differ.'
      if(size(eval)  .ne.n)
     +  stop 'diagonalize_zvo: eigenvalue array has wrong size.'
      if(size(evec,1).ne.n)
     +  stop 'diagonalize_zvo: eigenvector array has wrong dimensions.'
      if(size(evec,2).ne.n)
     +  stop 'diagonalize_zvo: dimensions of eigenvector array differ.'
      if(size(olap,1).ne.n)
     +  stop 'diagonalize_zvo: overlap matrix has wrong dimensions.'
      if(size(olap,2).ne.n)
     +  stop 'diagonalize_zvo: dimensions of overlap matrix differ.'
      allocate ( olap1(n,n),work(3*n),rwork(3*n) ) 
      evec = mat ; olap1 = olap
      call zhegv(1,'V','U',n,evec,n,olap1,n,eval,work,3*n,rwork,info) 
      if(info.ne.0) stop 'diagonalize_zvo: zhegv failed.'
      deallocate ( olap1,work,rwork )
      end subroutine diagonalize_zvo

      subroutine diagonalize_zpeo(eval,mat,olap)
      implicit none
1064
1065
1066
1067
      real,    intent(out) :: eval(:)
      complex, intent(in)  :: mat(:),olap(:)
      complex, allocatable :: mat1(:),olap1(:),work(:)
      real,    allocatable :: rwork(:)
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
      integer                 :: n,nn,info
      n = size(eval,1) ; nn = n*(n+1)/2
      if(n.eq.0)
     +  stop 'diagonalize_zpeo: zero dimension in eigenvalue problem.'
      if(size(mat) .ne.nn)
     +  stop 'diagonalize_zpeo: input matrix has wrong size.'
      if(size(olap).ne.nn)
     +  stop 'diagonalize_zpeo: overlap matrix has wrong size.'
      allocate ( mat1(nn),olap1(nn),work(3*n),rwork(3*n) ) 
      mat1 = mat ; olap1 = olap
      call zhpgv(1,'N','U',n,mat1,olap1,eval,work,n,work,rwork,info) 
      if(info.ne.0) stop 'diagonalize_zpeo: zhpev failed.'
      deallocate ( mat1,olap1,work,rwork )
      end subroutine diagonalize_zpeo

      subroutine diagonalize_zpvo(evec,eval,mat,olap)
      implicit none
1085
1086
1087
1088
1089
      real,    intent(out) :: eval(:)
      complex, intent(out) :: evec(:,:)
      complex, intent(in)  :: mat(:),olap(:)
      complex, allocatable :: mat1(:),olap1(:),work(:)
      real,    allocatable :: rwork(:)
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
      integer                 :: n,nn,info
      n = size(eval,1) ; nn = n*(n+1)/2
      if(n.eq.0)
     +  stop 'diagonalize_zpvo: zero dimension in eigenvalue problem.'
      if(size(mat)   .ne.nn)
     +  stop 'diagonalize_zpvo: input matrix has wrong size.'
      if(size(olap)  .ne.nn)
     +  stop 'diagonalize_zpvo: overlap matrix has wrong size.'
      if(size(evec,1).ne.n)
     +  stop 'diagonalize_zpvo: eigenvector array has wrong dimensions.'
      if(size(evec,2).ne.n)
     +  stop 'diagonalize_zpvo: dimensions of eigenvector array differ.'
      allocate ( mat1(nn),olap1(nn),work(3*n),rwork(3*n) ) 
      mat1 = mat ; olap1 = olap
      call zhpgv(1,'V','U',n,mat1,olap1,eval,evec,n,work,rwork,info) 
      if(info.ne.0) stop 'diagonalize_zpvo: zhpgv failed.'
      deallocate ( mat1,olap1,work,rwork )
      end subroutine diagonalize_zpvo

      subroutine diagonalize_dvs(evec,eval,mat,m)
      implicit none
1111
1112
1113
1114
      real, intent(out) :: eval(:),evec(:,:)
      real, intent(in)  :: mat(:,:)
      real, allocatable :: work(:),mat1(:,:)
      real              :: abstol,dlamch
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
      integer, intent(in)  :: m
      integer, allocatable :: iwork(:),ifail(:)
      integer              :: n,ma,idum,info
      ma = abs(m)
      n  = size(mat,1)
      if(n.eq.0)
     +  stop 'diagonalize_dvs: zero dimension in eigenvalue problem.'
      if(ma.gt.n) stop 'diagonalize_dvs: number of selected eigenvalues&
     & exceeds maximal number.'
      if(size(mat,2) .ne.n)
     +  stop 'diagonalize_dvs: dimensions of input matrix differ.'
      if(size(eval)  .ne.n)
     +  stop 'diagonalize_dvs: eigenvalue array has wrong size.'
      if(size(evec,1).ne.n)
     +  stop 'diagonalize_dvs: first dimension of eigenvector is wrong.'
      if(size(evec,2).ne.n)
     + stop 'diagonalize_dvs: second dimension of eigenvector is wrong.'
      allocate ( work(8*n),iwork(5*n),mat1(n,n),ifail(n) ) ; mat1 = mat
      abstol = 2 * dlamch('S')
      if(m.gt.0) then
        call dsyevx('V','I','U',n,mat1,n,0d0,0d0,n-ma+1,n,abstol,idum,
     +              eval,evec,n,work,8*n,iwork,ifail,info)
      else
        call dsyevx('V','I','U',n,mat1,n,0d0,0d0,    1,ma,abstol,idum,
     +              eval,evec,n,work,8*n,iwork,ifail,info)
      endif
      if(info.ne.0) stop 'diagonalize_dvs: dsyevx failed.'
      deallocate ( work,iwork,mat1,ifail )
      end subroutine diagonalize_dvs

      subroutine diagonalize_dvos(evec,eval,mat,olap,m)
      implicit none
1147
1148
1149
1150
      real, intent(out) :: eval(:),evec(:,:)
      real, intent(in)  :: mat(:,:),olap(:,:)
      real, allocatable :: work(:),mat1(:,:),olap1(:,:)
      real              :: abstol,dlamch
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
      integer, intent(in)  :: m
      integer, allocatable :: iwork(:),ifail(:)
      integer              :: n,ma,idum,info
      ma = abs(m)
      n  = size(mat,1)
      if(n.eq.0) 
     +  stop 'diagonalize_dvos: zero dimension in eigenvalue problem.'
      if(ma.gt.n) stop 'diagonalize_dvos: number of selected&
     & eigenvalues exceeds maximal number.'
      if(size(mat,2) .ne.n) stop 'diagonalize_dvos: dimensions of&
     & input matrix differ.'
      if(size(eval)  .ne.n)
     +  stop 'diagonalize_dvos: eigenvalue array has wrong size.'
      if(size(evec,1).ne.n)
     + stop 'diagonalize_dvos: first dimension of eigenvector is wrong.'
      if(size(evec,2).ne.n)
     +stop 'diagonalize_dvos: second dimension of eigenvector is wrong.'
      if(size(olap,1).ne.n) stop 'diagonalize_dvos: first dimension &
     &of overlap matrix is wrong.'
      if(size(olap,2).ne.n) stop 'diagonalize_dvos: second dimension of&
     & overlap matrix is wrong.'
      allocate ( work(8*n),iwork(5*n),mat1(n,n),olap1(n,n),ifail(n) ) 
      mat1 = mat ; olap1 = olap
      abstol = 2 * dlamch('S')
      if(m.gt.0) then
        call dsygvx(1,'V','I','U',n,mat1,n,olap1,n,0d0,0d0,n-ma+1,n,
     +              abstol,idum,eval,evec,n,work,8*n,iwork,ifail,info)
      else
        call dsygvx(1,'V','I','U',n,mat1,n,olap1,n,0d0,0d0,    1,ma,
     +              abstol,idum,eval,evec,n,work,8*n,iwork,ifail,info)
      endif
      if(info.ne.0) stop 'diagonalize_dvos: dsygvx failed.'
      deallocate ( work,iwork,mat1,olap1,ifail )
      end subroutine diagonalize_dvos

      subroutine diagonalize_dpvs(evec,eval,mat,m)
      implicit none
1188
1189
1190
1191
      real, intent(out) :: eval(:),evec(:,:)
      real, intent(in)  :: mat(:)
      real, allocatable :: work(:),mat1(:)
      real              :: abstol,dlamch
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
      integer, intent(in)  :: m
      integer, allocatable :: iwork(:),ifail(:)
      integer              :: n,nn,ma,idum,info
      ma = abs(m)
      n  = size(eval)
      nn = n*(n+1)/2
      if(n.eq.0)
     +  stop 'diagonalize_dpvs: zero dimension in eigenvalue problem.'
      if(ma.gt.n) stop 'diagonalize_dpvs: number of selected &
     &eigenvalues exceeds maximal number.'
      if(size(mat)   .ne.nn)
     +  stop 'diagonalize_dpvs: input matrix has wrong size.'
      if(size(evec,1).ne.n)  stop 'diagonalize_dpvs: first dimension &
     &of eigenvector is wrong.'
      if(size(evec,2).ne.n) 
     +stop 'diagonalize_dpvs: second dimension of eigenvector is wrong.'
      allocate ( work(8*n),iwork(5*n),mat1(nn),ifail(n) ) ; mat1 = mat
      abstol = 2 * dlamch('S')
      if(m.gt.0) then
        call dspevx('V','I','U',n,mat1,0d0,0d0,n-ma+1,n,abstol,idum,
     +              eval,evec,n,work,iwork,ifail,info)
      else
        call dspevx('V','I','U',n,mat1,0d0,0d0,    1,ma,abstol,idum,
     +              eval,evec,n,work,iwork,ifail,info)
      endif
      if(info.ne.0) stop 'diagonalize_dpvs: dspevx failed.'
      deallocate ( work,iwork,mat1,ifail )
      end subroutine diagonalize_dpvs

      subroutine diagonalize_dpvos(evec,eval,mat,olap,m)
      implicit none
1223
1224
1225
1226
      real, intent(out) :: eval(:),evec(:,:)
      real, intent(in)  :: mat(:),olap(:)
      real, allocatable :: work(:),mat1(:),olap1(:)
      real              :: abstol,dlamch
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
      integer, intent(in)  :: m
      integer, allocatable :: iwork(:),ifail(:)
      integer              :: n,nn,ma,idum,info
      ma = abs(m)
      n  = size(eval)
      nn = n*(n+1)/2
      if(n.eq.0)
     +  stop 'diagonalize_dpvos: zero dimension in eigenvalue problem.'
      if(ma.gt.n) stop 'diagonalize_dpvos: number of selected &
     &eigenvalues exceeds maximal number.'
      if(size(mat)   .ne.nn)
     + stop 'diagonalize_dpvos: input matrix has wrong size.'
      if(size(olap)  .ne.nn)
     +  stop 'diagonalize_dpvos: overlap matrix has wrong size.'
      if(size(evec,1).ne.n)  stop 'diagonalize_dpvos: first dimension&
     & of eigenvector is wrong.'
      if(size(evec,2).ne.n)  stop 'diagonalize_dpvos: second dimension&
     & of eigenvector is wrong.'
      allocate ( work(8*n),iwork(5*n),mat1(nn),olap1(nn),ifail(n) ) 
      mat1 = mat ; olap1 = olap
      abstol = 2 * dlamch('S')
      if(m.gt.0) then
        call dspgvx(1,'V','I','U',n,mat1,olap1,0d0,0d0,n-ma+1,n,abstol,
     +              idum,eval,evec,n,work,iwork,ifail,info)
      else
        call dspgvx(1,'V','I','U',n,mat1,olap1,0d0,0d0,    1,ma,abstol,
     +              idum,eval,evec,n,work,iwork,ifail,info)
      endif
      if(info.ne.0) stop 'diagonalize_dpvos: dspgvx failed.'
      deallocate ( work,iwork,mat1,olap1,ifail )
      end subroutine diagonalize_dpvos

      subroutine diagonalize_zvs(evec,eval,mat,m)
      implicit none
1261
1262
1263
1264
1265
1266
      real,    intent(out) :: eval(:)
      complex, intent(out) :: evec(:,:)
      complex, intent(in)  :: mat(:,:)
      complex, allocatable :: work(:),mat1(:,:)
      real,    allocatable :: rwork(:)
      real                 :: abstol,dlamch
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
      integer,    intent(in)  :: m
      integer,    allocatable :: iwork(:),ifail(:)
      integer                 :: n,ma,idum,info
      ma = abs(m)
      n  = size(mat,1)
      if(n.eq.0)
     +  stop 'diagonalize_zvs: zero dimension in eigenvalue problem.'
      if(ma.gt.n) stop 'diagonalize_zvs: number of selected eigenvalues&
     & exceeds maximal number.'
      if(size(mat,2) .ne.n)
     +  stop 'diagonalize_zvs: dimensions of input matrix differ.'
      if(size(eval)  .ne.n)
     +  stop 'diagonalize_zvs: eigenvalue array has wrong size.'
      if(size(evec,1).ne.n)
     +  stop 'diagonalize_zvs: first dimension of eigenvector is wrong.'
      if(size(evec,2).ne.n)
     + stop 'diagonalize_zvs: second dimension of eigenvector is wrong.'
      allocate ( work(2*n),rwork(7*n),iwork(5*n),mat1(n,n),ifail(n) ) 
      mat1 = mat
      abstol = 2 * dlamch('S')
      if(m.gt.0) then
        call zheevx('V','I','U',n,mat1,n,0d0,0d0,n-ma+1,n,abstol,idum,
     +              eval,evec,n,work,2*n,rwork,iwork,ifail,info)
      else
        call zheevx('V','I','U',n,mat1,n,0d0,0d0,    1,ma,abstol,idum,
     +              eval,evec,n,work,2*n,rwork,iwork,ifail,info)
      endif
      if(info.ne.0) stop 'diagonalize_zvs: zheevx failed.'
      deallocate ( work,rwork,iwork,mat1,ifail )
      end subroutine diagonalize_zvs

      subroutine diagonalize_zvos(evec,eval,mat,olap,m)
      implicit none
1300
1301
1302
1303
1304
1305
      real,    intent(out) :: eval(:)
      complex, intent(out) :: evec(:,:)
      complex, intent(in)  :: mat(:,:),olap(:,:)
      complex, allocatable :: work(:),mat1(:,:),olap1(:,:)
      real,    allocatable :: rwork(:)
      real                 :: abstol,dlamch
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
      integer,    intent(in)  :: m
      integer,    allocatable :: iwork(:),ifail(:)
      integer                 :: n,ma,idum,info
      ma = abs(m)
      n  = size(mat,1)
      if(n.eq.0)
     +  stop 'diagonalize_zvos: zero dimension in eigenvalue problem.'
      if(ma.gt.n) stop 'diagonalize_zvos: number of selected &
     &eigenvalues exceeds maximal number.'
      if(size(mat,2) .ne.n) stop 'diagonalize_zvos: dimensions of input&
     & matrix differ.'
      if(size(eval)  .ne.n) stop 'diagonalize_zvos: eigenvalue array&
     & has wrong size.'
      if(size(evec,1).ne.n)
     + stop 'diagonalize_zvos: first dimension of eigenvector is wrong.'
      if(size(evec,2).ne.n)
     +stop 'diagonalize_zvos: second dimension of eigenvector is wrong.'
      if(size(olap,1).ne.n) stop 'diagonalize_zvos: first dimension of&
     & overlap matrix is wrong.'
      if(size(olap,2).ne.n) stop 'diagonalize_zvos: second dimension of&
     & overlap matrix is wrong.'
      allocate ( work(2*n),rwork(7*n),iwork(5*n),mat1(n,n),olap1(n,n),
     +           ifail(n) ) ; mat1 = mat ; olap1 = olap
      abstol = 2 * dlamch('S')
      if(m.gt.0) then
        call zhegvx(1,'V','I','U',n,mat1,n,olap1,n,0d0,0d0,n-ma+1,n,
     +          abstol,idum,eval,evec,n,work,2*n,rwork,iwork,ifail,info)
      else
        call zhegvx(1,'V','I','U',n,mat1,n,olap1,n,0d0,0d0,    1,ma,
     +          abstol,idum,eval,evec,n,work,2*n,rwork,iwork,ifail,info)
      endif
      if(info.ne.0) stop 'diagonalize_zvos: zhegvx failed.'
      deallocate ( work,rwork,iwork,mat1,olap1,ifail )
      end subroutine diagonalize_zvos

      subroutine diagonalize_zpvs(evec,eval,mat,m)
      implicit none
1343
1344
1345
1346
1347
1348
      real,    intent(out) :: eval(:)
      complex, intent(out) :: evec(:,:)
      complex, intent(in)  :: mat(:)
      complex, allocatable :: work(:),mat1(:)
      real,    allocatable :: rwork(:)
      real                 :: abstol,dlamch
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
      integer,    intent(in)  :: m
      integer,    allocatable :: iwork(:),ifail(:)
      integer                 :: n,nn,ma,idum,info
      ma = abs(m)
      n  = size(eval)
      nn = n*(n+1)/2
      if(n.eq.0)
     +  stop 'diagonalize_zpvs: zero dimension in eigenvalue problem.'
      if(ma.gt.n)            stop 'diagonalize_zpvs: number of selected&
     & eigenvalues exceeds maximal number.'
      if(size(mat)   .ne.nn)
     +  stop 'diagonalize_zpvs: input matrix has wrong size.'
      if(size(evec,1).ne.n)
     + stop 'diagonalize_zpvs: first dimension of eigenvector is wrong.'
      if(size(evec,2).ne.n)
     +stop 'diagonalize_zpvs: second dimension of eigenvector is wrong.'
      allocate ( work(2*n),rwork(7*n),iwork(5*n),mat1(nn),ifail(n) ) 
      mat1 = mat
      abstol = 2 * dlamch('S')
      if(m.gt.0) then
        call zhpevx('V','I','U',n,mat1,0d0,0d0,n-ma+1,n,abstol,idum,
     +              eval,evec,n,work,iwork,ifail,info)
      else
        call zhpevx('V','I','U',n,mat1,0d0,0d0,    1,ma,abstol,idum,
     +              eval,evec,n,work,iwork,ifail,info)
      endif
      if(info.ne.0) stop 'diagonalize_zpvs: zhpevx failed.'
      deallocate ( work,rwork,iwork,mat1,ifail )
      end subroutine diagonalize_zpvs

      subroutine diagonalize_zpvos(evec,eval,mat,olap,m)
      implicit none
1381
1382
1383
1384
1385
1386
      real,    intent(out) :: eval(:)
      complex, intent(out) :: evec(:,:)
      complex, intent(in)  :: mat(:),olap(:)
      complex, allocatable :: work(:),mat1(:),olap1(:)
      real,    allocatable :: rwork(:)
      real                 :: abstol,dlamch
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
      integer,    intent(in)  :: m
      integer,    allocatable :: iwork(:),ifail(:)
      integer                 :: n,nn,ma,idum,info
      ma = abs(m)
      n  = size(eval)
      nn = n*(n+1)/2
      if(n.eq.0)
     +  stop 'diagonalize_zpvos: zero dimension in eigenvalue problem.'
      if(ma.gt.n)            stop 'diagonalize_zpvos: number of&
     & selected eigenvalues exceeds maximal number.'
      if(size(mat)   .ne.nn) 
     + stop 'diagonalize_zpvos: input matrix has wrong size.'
      if(size(olap)  .ne.nn) stop 'diagonalize_zpvos: overlap matrix&
     + has wrong size.'
      if(size(evec,1).ne.n)  stop 'diagonalize_zpvos: first dimension&
     + of eigenvector is wrong.'
      if(size(evec,2).ne.n)  stop 'diagonalize_zpvos: second dimension&
     + of eigenvector is wrong.'
      allocate ( work(2*n),rwork(7*n),iwork(5*n),mat1(nn),olap1(nn),
     +           ifail(n) ) ; mat1 = mat ; olap1 = olap
      abstol = 2 * dlamch('S')
      if(m.gt.0) then
        call zhpgvx(1,'V','I','U',n,mat1,olap1,0d0,0d0,n-ma+1,n,abstol,
     +              idum,eval,evec,n,work,rwork,iwork,ifail,info)
      else
        call zhpgvx(1,'V','I','U',n,mat1,olap1,0d0,0d0,    1,ma,abstol,
     +              idum,eval,evec,n,work,rwork,iwork,ifail,info)
      endif
      if(info.ne.0) stop 'diagonalize_zpvos: zhpgvx failed.'
      deallocate ( work,rwork,iwork,mat1,olap1,ifail )
      end subroutine diagonalize_zpvos

      ! routines for diagonalization: eigenvalue range [r1,r2) or index range [ir1,ir2].
      ! the number of actually found eigenvectors is returned in ir2.

      subroutine diagonalize_dvx(evec,eval,mat,ir1,ir2,r1,r2)
      implicit none
1424
1425
1426
1427
1428
      real, intent(out)   :: eval(:),evec(:,:)
      real, intent(in)    :: mat(:,:)
      real, allocatable   :: work(:),mat1(:,:)
      real                :: abstol,dlamch
      real, intent(in)    :: r1,r2
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
      integer, intent(in)    :: ir1
      integer, intent(inout) :: ir2
      integer, allocatable   :: iwork(:),ifail(:)
      integer                :: n,m,idum,info
      n = size(mat,1)
      m = ir2 - ir1 + 1
      if(n.eq.0) 
     +  stop 'diagonalize_dvx: zero dimension in eigenvalue problem.'
      if(m.lt.0)
     +  stop 'diagonalize_dvx: negative index range.'
      if(m.gt.n)            stop 'diagonalize_dvx: number of selected&
     & eigenvalues exceeds maximal number.'
      if(size(mat,2) .ne.n)
     +  stop 'diagonalize_dvx: dimensions of input matrix differ.'
      if(size(eval)  .lt.m)
     +  stop 'diagonalize_dvx: eigenvalue array too small.'
      if(size(evec,1).ne.n) stop 'diagonalize_dvx: first dimension of&
     & eigenvector is wrong.'
      if(size(evec,2).lt.m) stop 'diagonalize_dvx: second dimension of&
     & eigenvector too small.'
      allocate ( work(8*n),iwork(5*n),mat1(n,n),ifail(n) ) ; mat1 = mat
      abstol = 2 * dlamch('S')
      if(r1.lt.r2) then
        call dsyevx('V','V','U',n,mat1,n,r1,r2,0,0,      abstol,idum,
     +              eval,evec,n,work,8*n,iwork,ifail,info)
      else
        call dsyevx('V','I','U',n,mat1,n,0d0,0d0,ir1,ir2,abstol,idum,
     +              eval,evec,n,work,8*n,iwork,ifail,info)
      endif
      ir2 = idum
      if(info.ne.0) stop 'diagonalize_dvx: dsyevx failed.'
      deallocate ( work,iwork,mat1,ifail )
      end subroutine diagonalize_dvx

      subroutine diagonalize_dvox(evec,eval,mat,olap,ir1,ir2,r1,r2)
      implicit none
1465
1466
1467
1468
1469
      real, intent(out)   :: eval(:),evec(:,:)
      real, intent(in)    :: mat(:,:),olap(:,:)
      real, allocatable   :: work(:),mat1(:,:),olap1(:,:)
      real                :: abstol,dlamch
      real, intent(in)    :: r1,r2
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
      integer, intent(in)    :: ir1
      integer, intent(inout) :: ir2
      integer, allocatable   :: iwork(:),ifail(:)
      integer                :: n,m,ma,idum,info
      n = size(mat,1)
      m = ir2 - ir1 + 1 
      if(n.eq.0) 
     +  stop 'diagonalize_dvox: zero dimension in eigenvalue problem.'
      if(m.lt.0)
     +  stop 'diagonalize_dvox: negative index range.'
      if(m.gt.n) stop 'diagonalize_dvox: number of selected eigenvalues&
     & exceeds maximal number.'
      if(size(mat,2) .ne.n) stop 'diagonalize_dvox: dimensions of input&
     & matrix differ.'
      if(size(eval)  .lt.m)
     +  stop 'diagonalize_dvox: eigenvalue array too small.'
      if(size(evec,1).ne.n)
     + stop 'diagonalize_dvox: first dimension of eigenvector is wrong.'
      if(size(evec,2).lt.m) stop 'diagonalize_dvox: second dimension of&
     & eigenvector too small.'
      if(size(olap,1).ne.n) stop 'diagonalize_dvox: first dimension of&
     & overlap matrix is wrong.'
      if(size(olap,2).ne.n) stop 'diagonalize_dvox: second dimension of&
     & overlap matrix is wrong.'
      allocate ( work(8*n),iwork(5*n),mat1(n,n),olap1(n,n),ifail(n) ) 
      mat1 = mat ; olap1 = olap
      abstol = 2 * dlamch('S')
      if(r1.lt.r2) then
        call dsygvx(1,'V','V','U',n,mat1,n,olap1,n,r1,r2,0,0,
     +              abstol,idum,eval,evec,n,work,8*n,iwork,ifail,info)
      else
        call dsygvx(1,'V','I','U',n,mat1,n,olap1,n,0d0,0d0,ir1,ir2,
     +              abstol,idum,eval,evec,n,work,8*n,iwork,ifail,info)
      endif
      ir2 = idum
      if(info.ne.0) stop 'diagonalize_dvos: dsygvx failed.'
      deallocate ( work,iwork,mat1,olap1,ifail )
      end subroutine diagonalize_dvox

      subroutine diagonalize_dpvx(evec,eval,mat,ir1,ir2,r1,r2)
      implicit none
1511
1512
1513
1514
1515
      real, intent(out)   :: eval(:),evec(:,:)
      real, intent(in)    :: mat(:)
      real, allocatable   :: work(:),mat1(:)
      real                :: abstol,dlamch
      real, intent(in)    :: r1,r2
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541