|
18 | 18 | "get_sparse_eye", |
19 | 19 | "get_lstsq", |
20 | 20 | "get_sp_fft", |
| 21 | + "get_eigvals", |
| 22 | + "get_eigsh", |
| 23 | + "get_lobpcg", |
21 | 24 | "get_complex_dtype", |
22 | 25 | "get_real_dtype", |
23 | 26 | "to_cupy", |
|
36 | 39 |
|
37 | 40 | import numpy as np |
38 | 41 | import scipy.fft as sp_fft |
39 | | -from scipy.linalg import block_diag, lstsq, toeplitz |
| 42 | +from scipy.linalg import block_diag, eigvals, lstsq, toeplitz |
40 | 43 | from scipy.ndimage import gaussian_filter |
41 | 44 | from scipy.signal import convolve, correlate, fftconvolve, oaconvolve |
42 | 45 | from scipy.sparse import csc_matrix, csr_matrix, dia_matrix, eye |
| 46 | +from scipy.sparse.linalg import eigsh, lobpcg |
43 | 47 |
|
44 | 48 | from pylops.utils import deps |
45 | 49 | from pylops.utils.typing import ArrayLike, DTypeLike, NDArray, Tfftengine_ncj |
|
48 | 52 | import cupy as cp |
49 | 53 | import cupyx |
50 | 54 | import cupyx.scipy.fft as cp_fft |
| 55 | + from cupy.linalg import eigvals as cp_eigvals |
51 | 56 | from cupyx.scipy.linalg import block_diag as cp_block_diag |
52 | 57 | from cupyx.scipy.linalg import toeplitz as cp_toeplitz |
53 | 58 | from cupyx.scipy.ndimage import gaussian_filter as cp_gaussian_filter |
|
59 | 64 | from cupyx.scipy.sparse import csr_matrix as cp_csr_matrix |
60 | 65 | from cupyx.scipy.sparse import dia_matrix as cp_dia_matrix |
61 | 66 | from cupyx.scipy.sparse import eye as cp_eye |
| 67 | + from cupyx.scipy.sparse.linalg import eigsh as cp_eigsh |
| 68 | + from cupyx.scipy.sparse.linalg import lobpcg as cp_lobpcg |
62 | 69 |
|
63 | 70 | if deps.jax_enabled: |
64 | 71 | import jax |
@@ -527,6 +534,75 @@ def get_sp_fft(x: ArrayLike) -> Callable: |
527 | 534 | return cp_fft |
528 | 535 |
|
529 | 536 |
|
| 537 | +def get_eigvals(x: ArrayLike) -> Callable: |
| 538 | + """Returns correct eigvals module based on input |
| 539 | +
|
| 540 | + Parameters |
| 541 | + ---------- |
| 542 | + x : :obj:`numpy.ndarray` |
| 543 | + Array |
| 544 | +
|
| 545 | + Returns |
| 546 | + ------- |
| 547 | + f : :obj:`callable` |
| 548 | + Function to be used to process array |
| 549 | +
|
| 550 | + """ |
| 551 | + if not deps.cupy_enabled: |
| 552 | + return eigvals |
| 553 | + |
| 554 | + if cp.get_array_module(x) == np: |
| 555 | + return eigvals |
| 556 | + else: |
| 557 | + return cp_eigvals |
| 558 | + |
| 559 | + |
| 560 | +def get_eigsh(x: ArrayLike) -> Callable: |
| 561 | + """Returns correct eigsh module based on input |
| 562 | +
|
| 563 | + Parameters |
| 564 | + ---------- |
| 565 | + x : :obj:`numpy.ndarray` |
| 566 | + Array |
| 567 | +
|
| 568 | + Returns |
| 569 | + ------- |
| 570 | + f : :obj:`callable` |
| 571 | + Function to be used to process array |
| 572 | +
|
| 573 | + """ |
| 574 | + if not deps.cupy_enabled: |
| 575 | + return eigsh |
| 576 | + |
| 577 | + if cp.get_array_module(x) == np: |
| 578 | + return eigsh |
| 579 | + else: |
| 580 | + return cp_eigsh |
| 581 | + |
| 582 | + |
| 583 | +def get_lobpcg(x: ArrayLike) -> Callable: |
| 584 | + """Returns correct lobpcg module based on input |
| 585 | +
|
| 586 | + Parameters |
| 587 | + ---------- |
| 588 | + x : :obj:`numpy.ndarray` |
| 589 | + Array |
| 590 | +
|
| 591 | + Returns |
| 592 | + ------- |
| 593 | + f : :obj:`callable` |
| 594 | + Function to be used to process array |
| 595 | +
|
| 596 | + """ |
| 597 | + if not deps.cupy_enabled: |
| 598 | + return lobpcg |
| 599 | + |
| 600 | + if cp.get_array_module(x) == np: |
| 601 | + return lobpcg |
| 602 | + else: |
| 603 | + return cp_lobpcg |
| 604 | + |
| 605 | + |
530 | 606 | def get_complex_dtype(dtype: DTypeLike) -> DTypeLike: |
531 | 607 | """Returns a complex type in the precision of the input type. |
532 | 608 |
|
|
0 commit comments