| 1 |
! This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
! License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
! file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 |
module mbd_lapack |
| 5 |
|
| 6 |
use mbd_constants |
| 7 |
use mbd_utils, only: exception_t, tostr |
| 8 |
|
| 9 |
implicit none |
| 10 |
private |
| 11 |
public :: mmul, inv, invh, inverse, eig, eigh, eigvals, eigvalsh, det, mode |
| 12 |
|
| 13 |
interface mmul |
| 14 |
module procedure mmul_real |
| 15 |
module procedure mmul_complex |
| 16 |
end interface |
| 17 |
|
| 18 |
interface inv |
| 19 |
module procedure inv_real |
| 20 |
end interface |
| 21 |
|
| 22 |
interface invh |
| 23 |
module procedure invh_real |
| 24 |
end interface |
| 25 |
|
| 26 |
interface eig |
| 27 |
module procedure eig_real |
| 28 |
end interface |
| 29 |
|
| 30 |
interface eigh |
| 31 |
module procedure eigh_real |
| 32 |
module procedure eigh_complex |
| 33 |
end interface |
| 34 |
|
| 35 |
interface eigvals |
| 36 |
module procedure eigvals_real |
| 37 |
end interface |
| 38 |
|
| 39 |
interface eigvalsh |
| 40 |
module procedure eigvalsh_real |
| 41 |
module procedure eigvalsh_complex |
| 42 |
end interface |
| 43 |
|
| 44 |
interface |
| 45 |
! The followinbg interfaces were taken straight from the LAPACK codebase, |
| 46 |
! replacing COMPLEX*16 for COMPLEX(dp) |
| 47 |
SUBROUTINE ZHEEV(JOBZ, UPLO, N, A, LDA, W, WORK, LWORK, RWORK, INFO) |
| 48 |
import :: dp |
| 49 |
CHARACTER JOBZ, UPLO |
| 50 |
INTEGER INFO, LDA, LWORK, N |
| 51 |
DOUBLE PRECISION RWORK(*), W(*) |
| 52 |
COMPLEX(dp) A(LDA, *), WORK(*) |
| 53 |
END |
| 54 |
SUBROUTINE DGEEV(JOBVL, JOBVR, N, A, LDA, WR, WI, VL, LDVL, VR, LDVR, WORK, LWORK, INFO) |
| 55 |
CHARACTER JOBVL, JOBVR |
| 56 |
INTEGER INFO, LDA, LDVL, LDVR, LWORK, N |
| 57 |
DOUBLE PRECISION A(LDA, *), VL(LDVL, *), VR(LDVR, *), WI(*), WORK(*), WR(*) |
| 58 |
END |
| 59 |
SUBROUTINE DSYEV(JOBZ, UPLO, N, A, LDA, W, WORK, LWORK, INFO) |
| 60 |
CHARACTER JOBZ, UPLO |
| 61 |
INTEGER INFO, LDA, LWORK, N |
| 62 |
DOUBLE PRECISION A(LDA, *), W(*), WORK(*) |
| 63 |
END |
| 64 |
SUBROUTINE DGETRF(M, N, A, LDA, IPIV, INFO) |
| 65 |
INTEGER INFO, LDA, M, N |
| 66 |
INTEGER IPIV(*) |
| 67 |
DOUBLE PRECISION A(LDA, *) |
| 68 |
END |
| 69 |
SUBROUTINE DGETRI(N, A, LDA, IPIV, WORK, LWORK, INFO) |
| 70 |
INTEGER INFO, LDA, LWORK, N |
| 71 |
INTEGER IPIV(*) |
| 72 |
DOUBLE PRECISION A(LDA, *), WORK(*) |
| 73 |
END |
| 74 |
SUBROUTINE DGESV(N, NRHS, A, LDA, IPIV, B, LDB, INFO) |
| 75 |
INTEGER INFO, LDA, LDB, N, NRHS |
| 76 |
INTEGER IPIV(*) |
| 77 |
DOUBLE PRECISION A(LDA, *), B(LDB, *) |
| 78 |
END |
| 79 |
SUBROUTINE ZGETRF(M, N, A, LDA, IPIV, INFO) |
| 80 |
import :: dp |
| 81 |
INTEGER INFO, LDA, M, N |
| 82 |
INTEGER IPIV(*) |
| 83 |
COMPLEX(dp) A(LDA, *) |
| 84 |
END |
| 85 |
SUBROUTINE ZGETRI(N, A, LDA, IPIV, WORK, LWORK, INFO) |
| 86 |
import :: dp |
| 87 |
INTEGER INFO, LDA, LWORK, N |
| 88 |
INTEGER IPIV(*) |
| 89 |
COMPLEX(dp) A(LDA, *), WORK(*) |
| 90 |
END |
| 91 |
SUBROUTINE ZGEEV(JOBVL, JOBVR, N, A, LDA, W, VL, LDVL, VR, LDVR, WORK, LWORK, RWORK, INFO) |
| 92 |
import :: dp |
| 93 |
CHARACTER JOBVL, JOBVR |
| 94 |
INTEGER INFO, LDA, LDVL, LDVR, LWORK, N |
| 95 |
DOUBLE PRECISION RWORK(*) |
| 96 |
COMPLEX(dp) A(LDA, *), VL(LDVL, *), VR(LDVR, *), W(*), WORK(*) |
| 97 |
END |
| 98 |
SUBROUTINE DSYTRI(UPLO, N, A, LDA, IPIV, WORK, INFO) |
| 99 |
CHARACTER UPLO |
| 100 |
INTEGER INFO, LDA, N |
| 101 |
INTEGER IPIV(*) |
| 102 |
DOUBLE PRECISION A(LDA, *), WORK(*) |
| 103 |
END |
| 104 |
SUBROUTINE DSYTRF(UPLO, N, A, LDA, IPIV, WORK, LWORK, INFO) |
| 105 |
CHARACTER UPLO |
| 106 |
INTEGER INFO, LDA, LWORK, N |
| 107 |
INTEGER IPIV(*) |
| 108 |
DOUBLE PRECISION A(LDA, *), WORK(*) |
| 109 |
END |
| 110 |
SUBROUTINE DGEMM(TRANSA, TRANSB, M, N, K, ALPHA, A, LDA, B, LDB, BETA, C, LDC) |
| 111 |
DOUBLE PRECISION ALPHA, BETA |
| 112 |
INTEGER K, LDA, LDB, LDC, M, N |
| 113 |
CHARACTER TRANSA, TRANSB |
| 114 |
DOUBLE PRECISION A(LDA, *), B(LDB, *), C(LDC, *) |
| 115 |
END |
| 116 |
SUBROUTINE ZGEMM(TRANSA, TRANSB, M, N, K, ALPHA, A, LDA, B, LDB, BETA, C, LDC) |
| 117 |
import :: dp |
| 118 |
COMPLEX(dp) ALPHA, BETA |
| 119 |
INTEGER K, LDA, LDB, LDC, M, N |
| 120 |
CHARACTER TRANSA, TRANSB |
| 121 |
COMPLEX(dp) A(LDA, *), B(LDB, *), C(LDC, *) |
| 122 |
END |
| 123 |
end interface |
| 124 |
|
| 125 |
contains |
| 126 |
|
| 127 |
function inverse(A, exc) |
| 128 |
real(dp), intent(in) :: A(:, :) |
| 129 |
type(exception_t), intent(out), optional :: exc |
| 130 |
real(dp) :: inverse(size(A, 1), size(A, 2)) |
| 131 |
|
| 132 |
call inv_real(inverse, exc, src=A) |
| 133 |
end function |
| 134 |
|
| 135 |
function eigvalsh_real(A, exc, destroy) result(eigvals) |
| 136 |
real(dp), target, intent(in) :: A(:, :) |
| 137 |
type(exception_t), intent(out), optional :: exc |
| 138 |
logical, intent(in), optional :: destroy |
| 139 |
real(dp) :: eigvals(size(A, 1)) |
| 140 |
|
| 141 |
real(dp), allocatable, target :: A_work(:, :) |
| 142 |
real(dp), pointer :: A_p(:, :) |
| 143 |
|
| 144 |
nullify (A_p) |
| 145 |
if (present(destroy)) then |
| 146 |
if (destroy) then |
| 147 |
A_p => A |
| 148 |
end if |
| 149 |
end if |
| 150 |
if (.not. associated(A_p)) then |
| 151 |
allocate (A_work(size(A, 1), size(A, 1)), source=A) |
| 152 |
A_p => A_work |
| 153 |
end if |
| 154 |
call eigh_real(A_p, eigvals, exc, vals_only=.true.) |
| 155 |
end function |
| 156 |
|
| 157 |
function eigvalsh_complex(A, exc, destroy) result(eigvals) |
| 158 |
complex(dp), target, intent(in) :: A(:, :) |
| 159 |
type(exception_t), intent(out), optional :: exc |
| 160 |
logical, intent(in), optional :: destroy |
| 161 |
real(dp) :: eigvals(size(A, 1)) |
| 162 |
|
| 163 |
complex(dp), allocatable, target :: A_work(:, :) |
| 164 |
complex(dp), pointer :: A_p(:, :) |
| 165 |
|
| 166 |
nullify (A_p) |
| 167 |
if (present(destroy)) then |
| 168 |
if (destroy) then |
| 169 |
A_p => A |
| 170 |
end if |
| 171 |
end if |
| 172 |
if (.not. associated(A_p)) then |
| 173 |
allocate (A_work(size(A, 1), size(A, 1)), source=A) |
| 174 |
A_p => A_work |
| 175 |
end if |
| 176 |
call eigh_complex(A_p, eigvals, exc, vals_only=.true.) |
| 177 |
end function |
| 178 |
|
| 179 |
function eigvals_real(A, exc, destroy) result(eigvals) |
| 180 |
real(dp), target, intent(in) :: A(:, :) |
| 181 |
type(exception_t), intent(out), optional :: exc |
| 182 |
logical, intent(in), optional :: destroy |
| 183 |
complex(dp) :: eigvals(size(A, 1)) |
| 184 |
|
| 185 |
real(dp), allocatable, target :: A_work(:, :) |
| 186 |
real(dp), pointer :: A_p(:, :) |
| 187 |
|
| 188 |
nullify (A_p) |
| 189 |
if (present(destroy)) then |
| 190 |
if (destroy) then |
| 191 |
A_p => A |
| 192 |
end if |
| 193 |
end if |
| 194 |
if (.not. associated(A_p)) then |
| 195 |
allocate (A_work(size(A, 1), size(A, 1)), source=A) |
| 196 |
A_p => A_work |
| 197 |
end if |
| 198 |
call eig_real(A_p, eigvals, exc, vals_only=.true.) |
| 199 |
end function |
| 200 |
|
| 201 |
function mmul_real(A, B, transA, transB) result(C) |
| 202 |
real(dp), intent(in) :: A(:, :), B(:, :) |
| 203 |
character, intent(in), optional :: transA, transB |
| 204 |
real(dp) :: C(size(A, 1), size(B, 2)) |
| 205 |
|
| 206 |
character :: transA_, transB_ |
| 207 |
integer :: n |
| 208 |
|
| 209 |
transA_ = 'N' |
| 210 |
transB_ = 'N' |
| 211 |
if (present(transA)) transA_ = transA |
| 212 |
if (present(transB)) transB_ = transB |
| 213 |
n = size(A, 1) |
| 214 |
call DGEMM(transA_, transB_, n, n, n, 1d0, A, n, B, n, 0d0, C, n) |
| 215 |
end function |
| 216 |
|
| 217 |
function mmul_complex(A, B, transA, transB) result(C) |
| 218 |
complex(dp), intent(in) :: A(:, :), B(:, :) |
| 219 |
character, intent(in), optional :: transA, transB |
| 220 |
complex(dp) :: C(size(A, 1), size(B, 2)) |
| 221 |
|
| 222 |
character :: transA_, transB_ |
| 223 |
integer :: n |
| 224 |
|
| 225 |
transA_ = 'N' |
| 226 |
transB_ = 'N' |
| 227 |
if (present(transA)) transA_ = transA |
| 228 |
if (present(transB)) transB_ = transB |
| 229 |
n = size(A, 1) |
| 230 |
call ZGEMM(transA_, transB_, n, n, n, (1d0, 0d0), A, n, B, n, (0d0, 0d0), C, n) |
| 231 |
end function |
| 232 |
|
| 233 |
subroutine inv_real(A, exc, src) |
| 234 |
real(dp), intent(inout) :: A(:, :) |
| 235 |
type(exception_t), intent(out), optional :: exc |
| 236 |
real(dp), intent(in), optional :: src(:, :) |
| 237 |
|
| 238 |
real(dp), allocatable :: work_arr(:) |
| 239 |
integer, allocatable :: i_pivot(:) |
| 240 |
integer :: n, n_work_arr, error_flag |
| 241 |
real(dp) :: n_work_arr_optim(1) |
| 242 |
|
| 243 |
n = size(A, 1) |
| 244 |
if (n == 0) return |
| 245 |
if (present(src)) A = src |
| 246 |
allocate (i_pivot(n)) |
| 247 |
call DGETRF(n, n, A, n, i_pivot, error_flag) |
| 248 |
if (error_flag /= 0) then |
| 249 |
if (present(exc)) then |
| 250 |
exc%code = MBD_EXC_LINALG |
| 251 |
exc%origin = 'DGETRF' |
| 252 |
exc%msg = 'Failed with code '//trim(tostr(error_flag)) |
| 253 |
end if |
| 254 |
return |
| 255 |
end if |
| 256 |
call DGETRI(n, A, n, i_pivot, n_work_arr_optim, -1, error_flag) |
| 257 |
n_work_arr = nint(n_work_arr_optim(1)) |
| 258 |
allocate (work_arr(n_work_arr)) |
| 259 |
call DGETRI(n, A, n, i_pivot, work_arr(1), n_work_arr, error_flag) |
| 260 |
if (error_flag /= 0) then |
| 261 |
if (present(exc)) then |
| 262 |
exc%code = MBD_EXC_LINALG |
| 263 |
exc%origin = 'DGETRI' |
| 264 |
exc%msg = 'Failed with code '//trim(tostr(error_flag)) |
| 265 |
end if |
| 266 |
return |
| 267 |
end if |
| 268 |
end subroutine |
| 269 |
|
| 270 |
subroutine invh_real(A, exc, src) |
| 271 |
real(dp), intent(inout) :: A(:, :) |
| 272 |
type(exception_t), intent(out), optional :: exc |
| 273 |
real(dp), intent(in), optional :: src(:, :) |
| 274 |
|
| 275 |
integer, allocatable :: i_pivot(:) |
| 276 |
real(dp), allocatable :: work_arr(:) |
| 277 |
integer :: n, n_work_arr, error_flag |
| 278 |
real(dp) :: n_work_arr_optim(1) |
| 279 |
|
| 280 |
n = size(A, 1) |
| 281 |
if (n == 0) return |
| 282 |
if (present(src)) A = src |
| 283 |
allocate (i_pivot(n)) |
| 284 |
call DSYTRF('U', n, A, n, i_pivot, n_work_arr_optim, -1, error_flag) |
| 285 |
n_work_arr = nint(n_work_arr_optim(1)) |
| 286 |
allocate (work_arr(n_work_arr)) |
| 287 |
call DSYTRF('U', n, A, n, i_pivot, work_arr(1), n_work_arr, error_flag) |
| 288 |
if (error_flag /= 0) then |
| 289 |
if (present(exc)) then |
| 290 |
exc%code = MBD_EXC_LINALG |
| 291 |
exc%origin = 'DSYTRF' |
| 292 |
exc%msg = 'Failed with code '//trim(tostr(error_flag)) |
| 293 |
end if |
| 294 |
return |
| 295 |
end if |
| 296 |
deallocate (work_arr) |
| 297 |
allocate (work_arr(n)) |
| 298 |
call DSYTRI('U', n, A, n, i_pivot, work_arr, error_flag) |
| 299 |
if (error_flag /= 0) then |
| 300 |
if (present(exc)) then |
| 301 |
exc%code = MBD_EXC_LINALG |
| 302 |
exc%origin = 'DSYTRI' |
| 303 |
exc%msg = 'Failed with code '//trim(tostr(error_flag)) |
| 304 |
end if |
| 305 |
return |
| 306 |
end if |
| 307 |
call fill_tril(A) |
| 308 |
end subroutine |
| 309 |
|
| 310 |
subroutine eigh_real(A, eigs, exc, src, vals_only) |
| 311 |
real(dp), intent(inout) :: A(:, :) |
| 312 |
real(dp), intent(out) :: eigs(:) |
| 313 |
type(exception_t), intent(out), optional :: exc |
| 314 |
real(dp), intent(in), optional :: src(:, :) |
| 315 |
logical, intent(in), optional :: vals_only |
| 316 |
|
| 317 |
real(dp), allocatable :: work_arr(:) |
| 318 |
real(dp) :: n_work_arr(1) |
| 319 |
integer :: error_flag, n |
| 320 |
|
| 321 |
n = size(A, 1) |
| 322 |
if (present(src)) A = src |
| 323 |
call DSYEV(mode(vals_only), 'U', n, A, n, eigs, n_work_arr, -1, error_flag) |
| 324 |
allocate (work_arr(nint(n_work_arr(1)))) |
| 325 |
call DSYEV(mode(vals_only), 'U', n, A, n, eigs, work_arr(1), size(work_arr), error_flag) |
| 326 |
if (error_flag /= 0) then |
| 327 |
if (present(exc)) then |
| 328 |
exc%code = MBD_EXC_LINALG |
| 329 |
exc%origin = 'DSYEV' |
| 330 |
exc%msg = 'Failed with code '//trim(tostr(error_flag)) |
| 331 |
end if |
| 332 |
return |
| 333 |
end if |
| 334 |
end subroutine |
| 335 |
|
| 336 |
subroutine eig_real(A, eigs, exc, src, vals_only) |
| 337 |
real(dp), intent(inout) :: A(:, :) |
| 338 |
complex(dp), intent(out) :: eigs(:) |
| 339 |
type(exception_t), intent(out), optional :: exc |
| 340 |
real(dp), intent(in), optional :: src(:, :) |
| 341 |
logical, intent(in), optional :: vals_only |
| 342 |
|
| 343 |
real(dp) :: n_work_arr(1), dummy(1) |
| 344 |
integer :: error_flag, n |
| 345 |
real(dp), allocatable :: eigs_r(:), eigs_i(:), vectors(:, :), work_arr(:) |
| 346 |
|
| 347 |
n = size(A, 1) |
| 348 |
if (present(src)) A = src |
| 349 |
allocate (eigs_r(n), eigs_i(n)) |
| 350 |
if (mode(vals_only) == 'V') then |
| 351 |
allocate (vectors(n, n)) |
| 352 |
else |
| 353 |
allocate (vectors(1, 1)) |
| 354 |
end if |
| 355 |
call DGEEV( & |
| 356 |
'N', mode(vals_only), n, A, n, eigs_r, eigs_i, dummy, 1, & |
| 357 |
vectors, n, n_work_arr, -1, error_flag & |
| 358 |
) |
| 359 |
allocate (work_arr(nint(n_work_arr(1)))) |
| 360 |
call DGEEV( & |
| 361 |
'N', mode(vals_only), n, A, n, eigs_r, eigs_i, dummy, 1, & |
| 362 |
vectors, n, work_arr(1), size(work_arr), error_flag & |
| 363 |
) |
| 364 |
if (error_flag /= 0) then |
| 365 |
if (present(exc)) then |
| 366 |
exc%code = MBD_EXC_LINALG |
| 367 |
exc%origin = 'DGEEV' |
| 368 |
exc%msg = 'Failed with code '//trim(tostr(error_flag)) |
| 369 |
end if |
| 370 |
return |
| 371 |
end if |
| 372 |
eigs = cmplx(eigs_r, eigs_i, dp) |
| 373 |
if (mode(vals_only) == 'V') A = vectors |
| 374 |
end subroutine |
| 375 |
|
| 376 |
subroutine eigh_complex(A, eigs, exc, src, vals_only) |
| 377 |
complex(dp), intent(inout) :: A(:, :) |
| 378 |
real(dp), intent(out) :: eigs(:) |
| 379 |
type(exception_t), intent(out), optional :: exc |
| 380 |
complex(dp), intent(in), optional :: src(:, :) |
| 381 |
logical, intent(in), optional :: vals_only |
| 382 |
|
| 383 |
complex(dp), allocatable :: work(:) |
| 384 |
complex(dp) :: lwork_cmplx(1) |
| 385 |
real(dp), allocatable :: rwork(:) |
| 386 |
integer :: n, lwork, error_flag |
| 387 |
|
| 388 |
n = size(A, 1) |
| 389 |
if (present(src)) A = src |
| 390 |
allocate (rwork(max(1, 3 * n - 2))) |
| 391 |
call ZHEEV(mode(vals_only), 'U', n, A, n, eigs, lwork_cmplx, -1, rwork, error_flag) |
| 392 |
lwork = nint(dble(lwork_cmplx(1))) |
| 393 |
allocate (work(lwork)) |
| 394 |
call ZHEEV(mode(vals_only), 'U', n, A, n, eigs, work(1), lwork, rwork, error_flag) |
| 395 |
if (error_flag /= 0) then |
| 396 |
if (present(exc)) then |
| 397 |
exc%code = MBD_EXC_LINALG |
| 398 |
exc%origin = 'ZHEEV' |
| 399 |
exc%msg = 'Failed with code '//trim(tostr(error_flag)) |
| 400 |
end if |
| 401 |
return |
| 402 |
end if |
| 403 |
end subroutine |
| 404 |
|
| 405 |
real(dp) function det(A) result(D) |
| 406 |
real(dp), intent(in) :: A(:, :) |
| 407 |
|
| 408 |
integer :: n, i, info |
| 409 |
real(dp), allocatable :: LU(:, :) |
| 410 |
integer, allocatable :: ipiv(:) |
| 411 |
|
| 412 |
n = size(A, 1) |
| 413 |
allocate (ipiv(n)) |
| 414 |
LU = A |
| 415 |
call DGETRF(n, n, LU, n, ipiv, info) |
| 416 |
D = product([(LU(i, i), i=1, n)]) |
| 417 |
end function |
| 418 |
|
| 419 |
subroutine fill_tril(A) |
| 420 |
real(dp), intent(inout) :: A(:, :) |
| 421 |
|
| 422 |
integer :: i, j |
| 423 |
|
| 424 |
do i = 1, size(A, 1) |
| 425 |
do j = i + 1, size(A, 1) |
| 426 |
A(j, i) = A(i, j) |
| 427 |
end do |
| 428 |
end do |
| 429 |
end subroutine |
| 430 |
|
| 431 |
character(len=1) function mode(vals_only) |
| 432 |
logical, intent(in), optional :: vals_only |
| 433 |
|
| 434 |
mode = 'V' |
| 435 |
if (present(vals_only)) then |
| 436 |
if (vals_only) mode = 'N' |
| 437 |
end if |
| 438 |
end function |
| 439 |
|
| 440 |
end module |