wrapper.F 79.6 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
     +                                  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.'
102
      blockmat_d              = 0.0
103 104 105 106 107 108
      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
     +                                     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.'
118
      blockmat_z              = 0.0
119 120 121 122 123 124 125 126
      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
      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
Matthias Redies's avatar
Matthias Redies committed
138
          if(abs(mat(j,i)-mat(i,j)).gt.1e-8)
139 140 141 142 143 144 145 146
     +       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
     +                                    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.

Matthias Redies's avatar
Matthias Redies committed
160
!           if(abs(mat(j,i)-mat(i,j)).gt.1e-6) then
161 162 163 164 165 166
!             write(*,*) 'packmatcoul_d: input matrix not symmetric; deviation .gt. 1E-06'         
!           endif
        enddo
      enddo
      end function packmatcoul_d

Daniel Wortmann's avatar
Daniel Wortmann committed
167
            
168 169
      function unpackmat_d(mat)
      implicit none
170 171
      real, intent(in)  :: mat(:)
      real              :: unpackmat_d(
172 173
     +                             nint(sqrt(0.25+2*size(mat))-0.5),
     &                             nint(sqrt(0.25+2*size(mat))-0.5))
174
      integer              :: n,nn,i,j,k
175
      nn = size(mat) ; n = nint(sqrt(0.25+2*nn)-0.5)
176 177 178 179 180 181 182 183 184 185 186 187 188 189
      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
190 191
      complex, intent(in) :: mat(:,:)
      complex             :: packmat_z(size(mat,1)*(size(mat,1)+1)/2)
192 193 194 195 196 197 198 199 200
      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
Matthias Redies's avatar
Matthias Redies committed
201
          if(abs(conjg(mat(j,i))-mat(i,j)).gt.1e-8) 
202 203 204 205 206 207 208 209
     +                    stop 'packmat_z: input matrix not Hermitian.'
# endif
        enddo
      enddo
      end function packmat_z

      function packmatcoul_z(mat)
      implicit none
210 211
      complex, intent(in)  :: mat(:,:)
      complex              :: packmatcoul_z(
212 213 214 215 216 217 218 219 220 221
     +                                    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.

Matthias Redies's avatar
Matthias Redies committed
222
          if(abs(conjg(mat(j,i))-mat(i,j)).gt.1e-4) then
223 224 225 226 227 228 229 230 231
            stop 'packmatcoul_z: input matrix not Hermitian; &
     & deviation .gt. 1E-04.'
          endif
        enddo
      enddo
      end function packmatcoul_z
  
      function unpackmat_z(mat)
      implicit none
232 233
      complex, intent(in)  :: mat(:)
      complex              :: unpackmat_z(
234 235
     +                             nint(sqrt(0.25+2*size(mat))-0.5),
     &                             nint(sqrt(0.25+2*size(mat))-0.5))
236
      integer                 :: n,nn,i,j,k
237
      nn = size(mat) ; n = nint(sqrt(0.25+2*nn)-0.5)
238 239 240 241 242 243 244 245 246 247 248 249 250 251
      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
252 253
      real, intent(in) :: vec1(:),vec2(:)
      real             :: dotprod_dd
254
      integer             :: n
255
      real             :: ddot
256 257 258 259 260 261 262 263
      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
264 265 266
      real,    intent(in) :: vec1(:)
      complex, intent(in) :: vec2(:)
      complex             :: dotprod_dz
267
      integer                :: n
268
      real                :: ddot
269 270 271 272
      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)
273
     +             +(0.0,1.0)*ddot(n,vec1,1,aimag(vec2),1)
274 275 276 277
      end function dotprod_dz

      function dotprod_zd(vec1,vec2)
      implicit none
278 279 280
      complex, intent(in) :: vec1(:)
      real,    intent(in) :: vec2(:)
      complex             :: dotprod_zd
281
      integer                :: n
282
      real                :: ddot
283 284 285 286
      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)
287
     +             -(0.0,1.0)*ddot(n,aimag(vec1),1,vec2,1)
288 289 290 291
      end function dotprod_zd

      function dotprod_zz(vec1,vec2)
      implicit none
292 293
      complex, intent(in) :: vec1(:),vec2(:)
      complex             :: dotprod_zz
294
      integer                :: n
295
      complex             :: zdotc
296 297 298 299 300 301 302 303 304 305
      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
306 307
      real, intent(in)  :: mat(:),vec(:)
      real              :: matvec_dpd(size(vec))
308 309 310 311
      integer              :: nn,n
      n  = size(vec)
      nn = n*(n+1)/2 
      if(size(mat).ne.nn) stop 'matvec_dpd: input array has wrong size.'
312
      call dspmv('U',n,1.0,mat,vec,1,0.0,matvec_dpd,1)
313 314 315 316
      end function matvec_dpd

      function matvec_dpz(mat,vec)
      implicit none
317 318 319 320
      real,    intent(in) :: mat(:)
      complex, intent(in) :: vec(:)
      complex             :: matvec_dpz(size(vec))
      real,   allocatable :: vecr(:),veci(:)
321 322 323 324
      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.'
325 326 327
      call dspmv('U',n,1.0,mat,real(vec),1,0.0,vecr,1)
      call dspmv('U',n,1.0,mat,aimag(vec),1,0.0,veci,1)
      matvec_dpz = vecr + (0.0,1.0) * veci
328 329 330 331 332
      deallocate ( vecr,veci )
      end function matvec_dpz

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

c     --------

      function matmat_dpdp(mat1,mat2)
      implicit none
362 363
      real, intent(in)  :: mat1(:),mat2(:)
      real              :: matmat_dpdp(
364 365
     +                            nint(sqrt(0.25+2*size(mat1))-0.5),
     &                            nint(sqrt(0.25+2*size(mat1))-0.5))
366
      real, allocatable :: vec(:),vec2(:)
367 368
      integer              :: nn,n,k1,i,j,k
      nn = size(mat1)
369
      n  = nint(sqrt(0.25+2*nn)-0.5) ; allocate ( vec(n),vec2(n) )
370 371 372 373 374 375 376 377 378 379
      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
380
        call dspmv('U',n,1.0,mat1,vec2,1,0.0,vec,1)
381 382 383 384 385 386 387 388
        matmat_dpdp(:,i) = vec
        k = k + i
      enddo
      deallocate ( vec,vec2 )
      end function matmat_dpdp

      function matmat_dpzp(mat1,mat2)
      implicit none
389 390 391
      real,    intent(in)  :: mat1(:)
      complex, intent(in)  :: mat2(:)
      complex              :: matmat_dpzp(
392 393
     +                            nint(sqrt(0.25+2*size(mat1))-0.5),
     &                            nint(sqrt(0.25+2*size(mat1))-0.5))
394 395
      real,    allocatable :: vecr(:),veci(:)
      complex, allocatable :: vec2(:)
396 397
      integer                 :: nn,n,k1,i,j,k
      nn = size(mat1)
398
      n  = nint(sqrt(0.25+2*nn)-0.5) 
399 400 401 402 403 404 405 406 407 408 409
      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
410 411 412
        call dspmv('U',n,1.0,mat1,real(vec2),1,0.0,vecr,1)
        call dspmv('U',n,1.0,mat1,aimag(vec2),1,0.0,veci,1)
        matmat_dpzp(:,i) = vecr + (0.0,1.0) * veci
413 414 415 416 417 418 419
        k = k + i
      enddo
      deallocate ( vecr,veci,vec2 )
      end function matmat_dpzp

      function matmat_zpdp(mat1,mat2)
      implicit none
420 421 422
      complex, intent(in)  :: mat1(:)
      real,    intent(in)  :: mat2(:)
      complex              :: matmat_zpdp(
423 424
     +                            nint(sqrt(0.25+2*size(mat1))-0.5),
     &                            nint(sqrt(0.25+2*size(mat1))-0.5))
425 426
      real,    allocatable :: vecr(:),veci(:)
      complex, allocatable :: vec1(:)
427 428
      integer                 :: nn,n,k1,i,j,k
      nn = size(mat1)
429
      n  = nint(sqrt(0.25+2*nn)-0.5) 
430 431 432 433 434 435 436 437 438 439 440
      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
441 442 443
        call dspmv('U',n,1.0,mat2,real(vec1),1,0.0,vecr,1)
        call dspmv('U',n,1.0,mat2,aimag(vec1),1,0.0,veci,1)
        matmat_zpdp(i,:) = vecr + (0.0,1.0) * veci
444 445 446 447 448 449 450
        k = k + i
      enddo
      deallocate ( vecr,veci,vec1 )
      end function matmat_zpdp

      function matmat_zpzp(mat1,mat2)
      implicit none
451 452
      complex, intent(in)  :: mat1(:),mat2(:)
      complex              :: matmat_zpzp(
453 454
     +                            nint(sqrt(0.25+2*size(mat1))-0.5),
     &                            nint(sqrt(0.25+2*size(mat1))-0.5))
455
      complex, allocatable :: vec(:),vec2(:)
456 457
      integer                 :: nn,n,k1,i,j,k
      nn = size(mat1)
458
      n  = nint(sqrt(0.25+2*nn)-0.5) ; allocate ( vec(n),vec2(n) )
459 460 461 462 463 464 465 466 467 468
      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
469
        call zhpmv('U',n,(1.0,0.0),mat1,vec2,1,(0.0,0.0),vec,1)
470 471 472 473 474 475 476 477
        matmat_zpzp(:,i) = vec
        k = k + i
      enddo
      deallocate ( vec,vec2 )
      end function matmat_zpzp

      function matmat_dpdm(mat1,mat2)
      implicit none
478 479 480
      real, intent(in)  :: mat1(:),mat2(:,:)
      real              :: matmat_dpdm(size(mat2,1),size(mat2,1))
      real, allocatable :: vec(:),vec2(:)
481 482 483 484 485 486 487 488
      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)
489
        call dspmv('U',n,1.0,mat1,vec2,1,0.0,vec,1)
490 491 492 493 494 495 496
        matmat_dpdm(:,i) = vec
      enddo
      deallocate ( vec,vec2 )
      end function matmat_dpdm

      function matmat_dmdp(mat1,mat2)
      implicit none
497 498 499
      real, intent(in)  :: mat1(:,:),mat2(:)
      real              :: matmat_dmdp(size(mat1,1),size(mat1,1))
      real, allocatable :: vec(:),vec2(:)
500 501 502 503 504 505 506 507
      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,:)
508
        call dspmv('U',n,1.0,mat2,vec2,1,0.0,vec,1)
509 510 511 512 513 514 515
        matmat_dmdp(i,:) = vec
      enddo
      deallocate ( vec,vec2 )
      end function matmat_dmdp

      function matmat_dmdm(mat1,mat2)
      implicit none
516 517
      real, intent(in) :: mat1(:,:),mat2(:,:)
      real             :: matmat_dmdm(size(mat1,1),size(mat1,1))
518 519 520 521 522 523 524 525
      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.'
526
      call dgemm('N','N',n,n,n,1.0,mat1,n,mat2,n,0.0,matmat_dmdm,n)
527 528 529 530
      end function matmat_dmdm

      function matmat_dpzm(mat1,mat2)
      implicit none
531 532 533 534 535
      real,    intent(in)  :: mat1(:)
      complex, intent(in)  :: mat2(:,:)
      complex              :: matmat_dpzm(size(mat2,1),size(mat2,1))
      real,    allocatable :: vecr(:),veci(:)
      complex, allocatable :: vec2(:)
536 537 538 539 540 541 542 543 544
      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)
545 546 547
        call dspmv('U',n,1.0,mat1,real(vec2),1,0.0,vecr,1)
        call dspmv('U',n,1.0,mat1,aimag(vec2),1,0.0,veci,1)
        matmat_dpzm(:,i) = vecr + (0.0,1.0) * veci
548 549 550 551 552 553
      enddo
      deallocate ( vecr,veci,vec2 )
      end function matmat_dpzm

      function matmat_dmzp(mat1,mat2)
      implicit none
554 555 556 557
      real,    intent(in)  :: mat1(:,:)
      complex, intent(in)  :: mat2(:)
      complex              :: matmat_dmzp(size(mat1,1),size(mat1,1))
      complex, allocatable :: vec1(:),vec(:)
558 559 560 561 562 563 564 565
      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,:)
566
        call zhpmv('U',n,(1.0,0.0),mat2,vec1,1,(0.0,0.0),vec,1)
567 568 569 570 571 572 573
        matmat_dmzp(i,:) = conjg(vec)
      enddo
      deallocate ( vec,vec1 )
      end function matmat_dmzp

      function matmat_dmzm(mat1,mat2)
      implicit none
574 575 576 577
      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)),
578 579 580 581 582 583 584
     +                          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.'
585 586 587
      call dgemm('N','N',n1,n2,n,1.0,mat1,n1,real(mat2),n,0.0,matr,n1)
      call dgemm('N','N',n1,n2,n,1.0,mat1,n1,aimag(mat2),n,0.0,mati,n1)
      matmat_dmzm = matr + (0.0,1.0) * mati
588 589 590 591
      end function matmat_dmzm

      function matmat_zpdm(mat1,mat2)
      implicit none
592 593 594 595
      complex, intent(in)  :: mat1(:)
      real,    intent(in)  :: mat2(:,:)
      complex              :: matmat_zpdm(size(mat2,1),size(mat2,1))
      complex, allocatable :: vec(:),vec2(:)
596 597 598 599 600 601 602 603
      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)
604
        call zhpmv('U',n,(1.0,0.0),mat1,vec2,1,(0.0,0.0),vec,1)
605 606 607 608 609 610 611
        matmat_zpdm(:,i) = vec
      enddo
      deallocate ( vec,vec2 )
      end function matmat_zpdm

      function matmat_zmdp(mat1,mat2)
      implicit none
612 613 614 615 616
      complex, intent(in)  :: mat1(:,:)
      real,    intent(in)  :: mat2(:)
      complex              :: matmat_zmdp(size(mat1,1),size(mat1,1))
      complex, allocatable :: vec1(:)
      real,    allocatable :: vecr(:),veci(:)
617 618 619 620 621 622 623 624 625
      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,:))
626 627 628
        call dspmv('U',n,1.0,mat2,real(vec1),1,0.0,vecr,1)
        call dspmv('U',n,1.0,mat2,aimag(vec1),1,0.0,veci,1)
        matmat_zmdp(i,:) = vecr - (0.0,1.0) * veci
629 630 631 632 633 634
      enddo
      deallocate ( vecr,veci,vec1 )
      end function matmat_zmdp

      function matmat_zmdm(mat1,mat2)
      implicit none
635 636 637 638
      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)),
639 640 641 642 643 644 645
     +                          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.'
646 647 648
      call dgemm('N','N',n1,n2,n,1.0,real(mat1),n1,mat2,n,0.0,matr,n1)
      call dgemm('N','N',n1,n2,n,1.0,aimag(mat1),n1,mat2,n,0.0,mati,n1)
      matmat_zmdm = matr + (0.0,1.0) * mati
649 650 651 652
      end function matmat_zmdm

      function matmat_zpzm(mat1,mat2)
      implicit none
653 654 655
      complex, intent(in)  :: mat1(:),mat2(:,:)
      complex              :: matmat_zpzm(size(mat2,1),size(mat2,2))
      complex, allocatable :: vec(:),vec2(:)
656 657 658 659 660 661 662
      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)
663
        call zhpmv('U',n,(1.0,0.0),mat1,vec2,1,(0.0,0.0),vec,1)
664 665 666 667 668 669 670
        matmat_zpzm(:,i) = vec
      enddo
      deallocate ( vec,vec2 )
      end function matmat_zpzm

      function matmat_zmzp(mat1,mat2)
      implicit none
671 672 673
      complex, intent(in)  :: mat1(:,:),mat2(:)
      complex              :: matmat_zmzp(size(mat1,1),size(mat1,1))
      complex, allocatable :: vec(:),vec2(:)
674 675 676 677 678 679 680 681
      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,:))
682
        call zhpmv('U',n,(1.0,0.0),mat2,vec2,1,(0.0,0.0),vec,1)
683 684 685 686 687 688 689
        matmat_zmzp(i,:) = conjg(vec)
      enddo
      deallocate ( vec,vec2 )
      end function matmat_zmzp

      function matmat_zmzm(mat1,mat2)
      implicit none
690 691
      complex, intent(in) :: mat1(:,:),mat2(:,:)
      complex             :: matmat_zmzm(size(mat1,1),size(mat2,2))
692 693 694 695 696 697 698 699 700 701 702 703 704 705
      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
706 707
      real, intent(in) :: diag1(:),mat(:,:),diag2(:)
      real             :: matmatmatd_ddd(size(diag1),size(diag1))
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
      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
726 727 728
      real,    intent(in) :: diag1(:),diag2(:)
      complex, intent(in) :: mat(:,:)
      complex             :: matmatmatd_dzd(size(diag1),size(diag1))
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
      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
749 750 751
      real, intent(out) :: eval(:)
      real, intent(in)  :: mat(:,:)
      real, allocatable :: mat1(:,:),work(:)
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
      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
768 769 770
      real, intent(out) :: eval(:),evec(:,:)
      real, intent(in)  :: mat(:,:)
      real, allocatable :: work(:)
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
      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
791 792 793
      real, intent(out) :: eval(:)
      real, intent(in)  :: mat(:)
      real, allocatable :: mat1(:),work(:)
794 795 796 797 798 799 800 801 802 803 804 805 806 807
      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
808 809 810
      real, intent(out) :: eval(:),evec(:,:)
      real, intent(in)  :: mat(:)
      real, allocatable :: mat1(:),work(:)
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
      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
829 830 831 832
      real,    intent(out) :: eval(:)
      complex, intent(in)  :: mat(:,:)
      complex, allocatable :: mat1(:,:),work(:)
      real,    allocatable :: rwork(:)
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
      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
849 850 851 852 853
      real,    intent(out) :: eval(:)
      complex, intent(out) :: evec(:,:)
      complex, intent(in)  :: mat(:,:)
      complex, allocatable :: work(:)
      real,    allocatable :: rwork(:)
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
      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
874 875 876 877
      real,    intent(out) :: eval(:)
      complex, intent(in)  :: mat(:)
      complex, allocatable :: mat1(:),work(:)
      real,    allocatable :: rwork(:)
878 879 880 881 882 883 884 885 886 887 888 889 890 891
      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
892 893 894 895 896
      real,    intent(out) :: eval(:)
      complex, intent(out) :: evec(:,:)
      complex, intent(in)  :: mat(:)
      complex, allocatable :: mat1(:),work(:)
      real,    allocatable :: rwork(:)
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
      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
915 916 917
      real, intent(out) :: eval(:)
      real, intent(in)  :: mat(:,:),olap(:,:)
      real, allocatable :: mat1(:,:),olap1(:,:),work(:)
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
      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
939 940 941
      real, intent(out) :: eval(:),evec(:,:)
      real, intent(in)  :: mat(:,:),olap(:,:)
      real, allocatable :: olap1(:,:),work(:)
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
      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
966 967 968
      real, intent(out) :: eval(:)
      real, intent(in)  :: mat(:),olap(:)
      real, allocatable :: mat1(:),olap1(:),work(:)
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
      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
986 987 988
      real, intent(out) :: eval(:),evec(:,:)
      real, intent(in)  :: mat(:),olap(:)
      real, allocatable :: mat1(:),olap1(:),work(:)
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
      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
1010 1011 1012 1013
      real,    intent(out) :: eval(:)
      complex, intent(in)  :: mat(:,:),olap(:,:)
      complex, allocatable :: mat1(:,:),olap1(:,:),work(:)
      real,    allocatable :: rwork(:)
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
      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
1035 1036 1037 1038 1039
      real,    intent(out) :: eval(:)
      complex, intent(out) :: evec(:,:)
      complex, intent(in)  :: mat(:,:),olap(:,:)
      complex, allocatable :: olap1(:,:),work(:)
      real,    allocatable :: rwork(:)
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
      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
1065 1066 1067 1068
      real,    intent(out) :: eval(:)
      complex, intent(in)  :: mat(:),olap(:)
      complex, allocatable :: mat1(:),olap1(:),work(:)
      real,    allocatable :: rwork(:)
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
      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
1086 1087 1088 1089 1090
      real,    intent(out) :: eval(:)
      complex, intent(out) :: evec(:,:)
      complex, intent(in)  :: mat(:),olap(:)
      complex, allocatable :: mat1(:),olap1(:),work(:)
      real,    allocatable :: rwork(:)
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
      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
1112 1113 1114 1115
      real, intent(out) :: eval(:),evec(:,:)
      real, intent(in)  :: mat(:,:)
      real, allocatable :: work(:),mat1(:,:)
      real              :: abstol,dlamch
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
      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
1136
        call dsyevx('V','I','U',n,mat1,n,0.0,0.0,n-ma+1,n,abstol,idum,
1137 1138
     +              eval,evec,n,work,8*n,iwork,ifail,info)
      else
1139
        call dsyevx('V','I','U',n,mat1,n,0.0,0.0,    1,ma,abstol,idum,
1140 1141 1142 1143 1144 1145 1146 1147
     +              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
1148 1149 1150 1151
      real, intent(out) :: eval(:),evec(:,:)
      real, intent(in)  :: mat(:,:),olap(:,:)
      real, allocatable :: work(:),mat1(:,:),olap1(:,:)
      real              :: abstol,dlamch
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
      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
1177
        call dsygvx(1,'V','I','U',n,mat1,n,olap1,n,0.0,0.0,n-ma+1,n,
1178 1179
     +              abstol,idum,eval,evec,n,work,8*n,iwork,ifail,info)
      else
1180
        call dsygvx(1,'V','I','U',n,mat1,n,olap1,n,0.0,0.0,    1,ma,
1181 1182 1183 1184 1185 1186 1187 1188
     +              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
1189 1190 1191 1192
      real, intent(out) :: eval(:),evec(:,:)
      real, intent(in)  :: mat(:)
      real, allocatable :: work(:),mat1(:)
      real              :: abstol,dlamch
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
      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
1212
        call dspevx('V','I','U',n,mat1,0.0,0.0,n-ma+1,n,abstol,idum,
1213 1214
     +              eval,evec,n,work,iwork,ifail,info)
      else
1215
        call dspevx('V','I','U',n,mat1,0.0,0.0,    1,ma,abstol,idum,
1216 1217 1218 1219 1220 1221 1222 1223
     +              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
1224 1225 1226 1227
      real, intent(out) :: eval(:),evec(:,:)
      real, intent(in)  :: mat(:),olap(:)
      real, allocatable :: work(:),mat1(:),olap1(:)
      real              :: abstol,dlamch
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
      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
1250
        call dspgvx(1,'V','I','U',n,mat1,olap1,0.0,0.0,n-ma+1,n,abstol,
1251 1252
     +              idum,eval,evec,n,work,iwork,ifail,info)
      else
1253
        call dspgvx(1,'V','I','U',n,mat1,olap1,0.0,0.0,    1,ma,abstol,
1254 1255 1256 1257 1258 1259 1260 1261
     +              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
1262 1263 1264 1265 1266 1267
      real,    intent(out) :: eval(:)
      complex, intent(out) :: evec(:,:)
      complex, intent(in)  :: mat(:,:)
      complex, allocatable :: work(:),mat1(:,:)
      real,    allocatable :: rwork(:)
      real                 :: abstol,dlamch
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
      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
1289
        call zheevx('V','I','U',n,mat1,n,00.,0.0,n-ma+1,n,abstol,idum,
1290 1291
     +              eval,evec,n,work,2*n,rwork,iwork,ifail,info)
      else
1292
        call zheevx('V','I','U',n,mat1,n,0.0,0.0,    1,ma,abstol,idum,
1293 1294 1295 1296 1297 1298 1299 1300
     +              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
1301 1302 1303 1304 1305 1306
      real,    intent(out) :: eval(:)
      complex, intent(out) :: evec(:,:)
      complex, intent(in)  :: mat(:,:),olap(:,:)
      complex, allocatable :: work(:),mat1(:,:),olap1(:,:)
      real,    allocatable :: rwork(:)
      real                 :: abstol,dlamch
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
      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
1332
        call zhegvx(1,'V','I','U',n,mat1,n,olap1,n,0.0,0.0,n-ma+1,n,
1333 1334
     +          abstol,idum,eval,evec,n,work,2*n,rwork,iwork,ifail,info)
      else
1335
        call zhegvx(1,'V','I','U',n,mat1,n,olap1,n,0.0,0.0,    1,ma,
1336 1337 1338 1339 1340 1341 1342 1343
     +          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
1344 1345 1346 1347 1348 1349
      real,    intent(out) :: eval(:)
      complex, intent(out) :: evec(:,:)
      complex, intent(in)  :: mat(:)
      complex, allocatable :: work(:),mat1(:)
      real,    allocatable :: rwork(:)
      real                 :: abstol,dlamch
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
      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
1370
        call zhpevx('V','I','U',n,mat1,0.0,0.0,n-ma+1,n,abstol,idum,
1371 1372
     +              eval,evec,n,work,iwork,ifail,info)
      else
1373
        call zhpevx('V','I','U',n,mat1,0.0,0.0,    1,ma,abstol,idum,
1374 1375 1376 1377 1378 1379 1380 1381
     +              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
1382 1383 1384 1385 1386 1387
      real,    intent(out) :: eval(:)
      complex, intent(out) :: evec(:,:)
      complex, intent(in)  :: mat(:),olap(:)
      complex, allocatable :: work(:),mat1(:),olap1(:)
      real,    allocatable :: rwork(:)
      real                 :: abstol,dlamch
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
      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
1410
        call zhpgvx(1,'V','I','U',n,mat1,olap1,0.0,0.0,n-ma+1,n,abstol,
1411 1412
     +              idum,eval,evec,n,work,rwork,iwork,ifail,info)
      else