Skip to main content

Python interface to the C++ Spectra library

Project description

pfPySpectra

pfpyspectra based on pyspectra, Python interface to the C++ Spectra library

Eigensolvers

pfPySpecta offers two general interfaces to Spectra: eigensolver and eigensolverh. For general(dense&sparse) and symmetric(dense&sparse) matrices respectively.These two functions would invoke the most suitable method based on the information provided by the user.

Usage

import numpy as np
import scipy.sparse as sp
from pfpyspectra import eigensolver, eigensolverh

# matrix size
size = 100

# number of eigenpairs to compute
nvalues = 2

# Create random matrix
xs = np.random.normal(size=size ** 2).reshape(size, size)
new_xs=sp.rand(size, size, density=0.1, format='csc')

# Create symmetric matrix
mat = xs + xs.T
new_mat = new_xs + new_xs.T

# Compute two eigenpairs selecting the eigenvalues with
# largest magnitude (default).
eigenvalues, eigenvectors = eigensolver(xs, nvalues)
sprse_eigenvalues, sprse_eigenvectors = eigensolver(new_xs, nvalues)
# Compute two eigenpairs selecting the eigenvalues with
# largest algebraic value
selection_rule = "LargestAlge"
symm_eigenvalues, symm_eigenvectors = eigensolverh(
  mat, nvalues, selection_rule)
sprse_symm_eigenvalues, sprse_symm_eigenvectors = eigensolverh(
  mat, nvalues, selection_rule)

Note: The available selection_rules to compute a portion of the spectrum are:

  • LargestMagn
  • LargestReal
  • LargestImag
  • LargestAlge
  • SmallestMagn
  • SmallestReal
  • SmallestImag
  • SmallestAlge
  • BothEnds

Eigensolvers Dense Interface

You can also call directly the dense interface. You would need to import the following module:

import numpy as np
from pfpyspectra import spectra_dense_interface

The following functions are available in the spectra_dense_interface:

  • general_eigensolver(
     mat: np.ndarray, eigenpairs: int, basis_size: int, selection_rule: str)
     -> (np.ndarray, np.ndarray)
    
  • general_real_shift_eigensolver(
    mat: np.ndarray, eigenpairs: int, basis_size: int, shift: float, selection_rule: str)
    -> (np.ndarray, np.ndarray)
    
  • general_complex_shift_eigensolver(
      mat: np.ndarray, eigenpairs: int, basis_size: int,
      shift_real: float, shift_imag: float, selection_rule: str)
      -> (np.ndarray, np.ndarray)
    
  • symmetric_eigensolver(
      mat: np.ndarray, eigenpairs: int, basis_size: int, selection_rule: str)
      -> (np.ndarray, np.ndarray)
    
  • symmetric_shift_eigensolver(
      mat: np.ndarray, eigenpairs: int, basis_size: int, shift: float, selection_rule: str)
      -> (np.ndarray, np.ndarray)
    
  • symmetric_generalized_shift_eigensolver(
      mat_A: np.ndarray, mat_B: np.ndarray, eigenpairs: int, basis_size: int, shift: float,
      selection_rule: str)
      -> (np.ndarray, np.ndarray)
    

Eigensolvers Sparse Interface

You can also call directly the sparse interface. You would need to import the following module:

import scipy as sp
from pfpyspectra import spectra_sparse_interface

The following functions are available in the spectra_sparse_interface:

  • sparse_general_eigensolver(
     mat: sp.spmatrix, eigenpairs: int, basis_size: int, selection_rule: str)
     -> (np.ndarray, np.ndarray)
    
  • sparse_general_real_shift_eigensolver(
    mat: sp.spmatrix, eigenpairs: int, basis_size: int, shift: float, selection_rule: str)
    -> (np.ndarray, np.ndarray)
    
  • sparse_general_complex_shift_eigensolver(
      mat: sp.spmatrix, eigenpairs: int, basis_size: int,
      shift_real: float, shift_imag: float, selection_rule: str)
      -> (np.ndarray, np.ndarray)
    
  • sparse_symmetric_eigensolver(
      mat: sp.spmatrix, eigenpairs: int, basis_size: int, selection_rule: str)
      -> (np.ndarray, np.ndarray)
    
  • sparse_symmetric_shift_eigensolver(
      mat: sp.spmatrix, eigenpairs: int, basis_size: int, shift: float, selection_rule: str)
      -> (np.ndarray, np.ndarray)
    
  • sparse_symmetric_generalized_shift_eigensolver(
      mat_A: sp.spmatrix, mat_B: sp.spmatrix, eigenpairs: int, basis_size: int, shift: float,
      selection_rule: str)
      -> (np.ndarray, np.ndarray)
    

Example

import numpy as np
from pfpyspectra import spectra_dense_interface

size = 100
nvalues = 2 # eigenpairs to compute
search_space = nvalues * 2 # size of the search space
shift = 1.0

# Create random matrix
xs = np.random.normal(size=size ** 2).reshape(size, size)

# Create symmetric matrix
mat = xs + xs.T

# Compute two eigenpairs selecting the eigenvalues with
# largest algebraic value
selection_rule = "LargestAlge"
symm_eigenvalues, symm_eigenvectors = \
  spectra_dense_interface.symmetric_eigensolver(
  mat, nvalues, search_space, selection_rule)

Note: All functions return a tuple whith the resulting eigenvalues and eigenvectors. For more examples, please see the directory: pfpyspectra/tests/

Installation

To install pyspectra, do:

  git clone git@gitee.com:PerfXLab/spectra4py.git
  cd pyspectra
  bash ./install.sh

Test

Run tests (including coverage) with:

  pytest tests/test_dense_pyspectra.py
  pytest tests/test_sparse_pyspectra.py
  pytest tests/test_pyspectra.py
  # also you can just `pytest tests`

Help: If you don't pass them all, don't worry, try a few more times.
I think that's because of the random parameter problem, It will not affect the use, can you help me?

License

No. Just for fun!
Thanks :
pyspectra,
C++ Spectra library

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pfpyspectra-0.1.0.tar.gz (9.7 kB view details)

Uploaded Source

File details

Details for the file pfpyspectra-0.1.0.tar.gz.

File metadata

  • Download URL: pfpyspectra-0.1.0.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.4

File hashes

Hashes for pfpyspectra-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7083f7287cff13a8ef6ebe4321254f2e0321e6959faf9cc512f02b9f0d51249d
MD5 749a3227278a8826f9fd10b184a5afc4
BLAKE2b-256 035d2ff8155e17e9350e8fd654eada3eda9b321c74ec3abf24709327551e53e3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page