# pfPySpectra
pfpyspectra based on [pyspectra](https://github.com/NLESC-JCER/pyspectra.git), Python interface to the [C++ Spectra library](https://github.com/yixuan/spectra)
## Eigensolvers
**pfPySpecta** offers two general interfaces to [Spectra](https://github.com/yixuan/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
```python
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:
```python
import numpy as np
from pfpyspectra import spectra_dense_interface
```
The following functions are available in the spectra_dense_interface:
* ```py
general_eigensolver(
mat: np.ndarray, eigenpairs: int, basis_size: int, selection_rule: str)
-> (np.ndarray, np.ndarray)
```
* ```py
general_real_shift_eigensolver(
mat: np.ndarray, eigenpairs: int, basis_size: int, shift: float, selection_rule: str)
-> (np.ndarray, np.ndarray)
```
* ```py
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)
```
* ```py
symmetric_eigensolver(
mat: np.ndarray, eigenpairs: int, basis_size: int, selection_rule: str)
-> (np.ndarray, np.ndarray)
```
* ```py
symmetric_shift_eigensolver(
mat: np.ndarray, eigenpairs: int, basis_size: int, shift: float, selection_rule: str)
-> (np.ndarray, np.ndarray)
```
* ```py
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:
```python
import scipy as sp
from pfpyspectra import spectra_sparse_interface
```
The following functions are available in the spectra_sparse_interface:
* ```py
sparse_general_eigensolver(
mat: sp.spmatrix, eigenpairs: int, basis_size: int, selection_rule: str)
-> (np.ndarray, np.ndarray)
```
* ```py
sparse_general_real_shift_eigensolver(
mat: sp.spmatrix, eigenpairs: int, basis_size: int, shift: float, selection_rule: str)
-> (np.ndarray, np.ndarray)
```
* ```py
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)
```
* ```py
sparse_symmetric_eigensolver(
mat: sp.spmatrix, eigenpairs: int, basis_size: int, selection_rule: str)
-> (np.ndarray, np.ndarray)
```
* ```py
sparse_symmetric_shift_eigensolver(
mat: sp.spmatrix, eigenpairs: int, basis_size: int, shift: float, selection_rule: str)
-> (np.ndarray, np.ndarray)
```
* ```py
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
```python
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:
```bash
git clone git@gitee.com:PerfXLab/spectra4py.git
cd pyspectra
bash ./install.sh
```
## Test
Run tests (including coverage) with:
```bash
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](https://github.com/NLESC-JCER/pyspectra.git),
[C++ Spectra library](https://github.com/yixuan/spectra)
Raw data
{
"_id": null,
"home_page": "",
"name": "pfpyspectra",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "pfpyspectra",
"author": "pf_test",
"author_email": "",
"download_url": "https://files.pythonhosted.org/packages/03/5d/2ff8155e17e9350e8fd654eada3eda9b321c74ec3abf24709327551e53e3/pfpyspectra-0.1.0.tar.gz",
"platform": null,
"description": "# pfPySpectra\n\npfpyspectra based on [pyspectra](https://github.com/NLESC-JCER/pyspectra.git), Python interface to the [C++ Spectra library](https://github.com/yixuan/spectra)\n\n## Eigensolvers\n**pfPySpecta** offers two general interfaces to [Spectra](https://github.com/yixuan/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.\n\n## Usage\n```python\nimport numpy as np\nimport scipy.sparse as sp\nfrom pfpyspectra import eigensolver, eigensolverh\n\n# matrix size\nsize = 100\n\n# number of eigenpairs to compute\nnvalues = 2\n\n# Create random matrix\nxs = np.random.normal(size=size ** 2).reshape(size, size)\nnew_xs=sp.rand(size, size, density=0.1, format='csc')\n\n# Create symmetric matrix\nmat = xs + xs.T\nnew_mat = new_xs + new_xs.T\n\n# Compute two eigenpairs selecting the eigenvalues with\n# largest magnitude (default).\neigenvalues, eigenvectors = eigensolver(xs, nvalues)\nsprse_eigenvalues, sprse_eigenvectors = eigensolver(new_xs, nvalues)\n# Compute two eigenpairs selecting the eigenvalues with\n# largest algebraic value\nselection_rule = \"LargestAlge\"\nsymm_eigenvalues, symm_eigenvectors = eigensolverh(\n mat, nvalues, selection_rule)\nsprse_symm_eigenvalues, sprse_symm_eigenvectors = eigensolverh(\n mat, nvalues, selection_rule)\n```\n\n**Note**:\n The available selection_rules to compute a portion of the spectrum are:\n * LargestMagn\n * LargestReal\n * LargestImag\n * LargestAlge\n * SmallestMagn\n * SmallestReal\n * SmallestImag\n * SmallestAlge\n * BothEnds\n\n## Eigensolvers Dense Interface\nYou can also call directly the dense interface. You would need to import the following module:\n```python\nimport numpy as np\nfrom pfpyspectra import spectra_dense_interface\n```\nThe following functions are available in the spectra_dense_interface:\n* ```py\n general_eigensolver(\n mat: np.ndarray, eigenpairs: int, basis_size: int, selection_rule: str)\n -> (np.ndarray, np.ndarray)\n ```\n* ```py\n general_real_shift_eigensolver(\n mat: np.ndarray, eigenpairs: int, basis_size: int, shift: float, selection_rule: str)\n -> (np.ndarray, np.ndarray)\n ```\n* ```py\n general_complex_shift_eigensolver(\n mat: np.ndarray, eigenpairs: int, basis_size: int,\n shift_real: float, shift_imag: float, selection_rule: str)\n -> (np.ndarray, np.ndarray)\n ```\n* ```py\n symmetric_eigensolver(\n mat: np.ndarray, eigenpairs: int, basis_size: int, selection_rule: str)\n -> (np.ndarray, np.ndarray)\n ```\n* ```py\n symmetric_shift_eigensolver(\n mat: np.ndarray, eigenpairs: int, basis_size: int, shift: float, selection_rule: str)\n -> (np.ndarray, np.ndarray)\n ```\n* ```py\n symmetric_generalized_shift_eigensolver(\n mat_A: np.ndarray, mat_B: np.ndarray, eigenpairs: int, basis_size: int, shift: float,\n selection_rule: str)\n -> (np.ndarray, np.ndarray)\n ```\n\n## Eigensolvers Sparse Interface\nYou can also call directly the sparse interface. You would need to import the following module:\n```python\nimport scipy as sp\nfrom pfpyspectra import spectra_sparse_interface\n```\nThe following functions are available in the spectra_sparse_interface:\n* ```py\n sparse_general_eigensolver(\n mat: sp.spmatrix, eigenpairs: int, basis_size: int, selection_rule: str)\n -> (np.ndarray, np.ndarray)\n ```\n* ```py\n sparse_general_real_shift_eigensolver(\n mat: sp.spmatrix, eigenpairs: int, basis_size: int, shift: float, selection_rule: str)\n -> (np.ndarray, np.ndarray)\n ```\n* ```py\n sparse_general_complex_shift_eigensolver(\n mat: sp.spmatrix, eigenpairs: int, basis_size: int,\n shift_real: float, shift_imag: float, selection_rule: str)\n -> (np.ndarray, np.ndarray)\n ```\n* ```py\n sparse_symmetric_eigensolver(\n mat: sp.spmatrix, eigenpairs: int, basis_size: int, selection_rule: str)\n -> (np.ndarray, np.ndarray)\n ```\n* ```py\n sparse_symmetric_shift_eigensolver(\n mat: sp.spmatrix, eigenpairs: int, basis_size: int, shift: float, selection_rule: str)\n -> (np.ndarray, np.ndarray)\n ```\n* ```py\n sparse_symmetric_generalized_shift_eigensolver(\n mat_A: sp.spmatrix, mat_B: sp.spmatrix, eigenpairs: int, basis_size: int, shift: float,\n selection_rule: str)\n -> (np.ndarray, np.ndarray)\n ```\n## Example\n```python\nimport numpy as np\nfrom pfpyspectra import spectra_dense_interface\n\nsize = 100\nnvalues = 2 # eigenpairs to compute\nsearch_space = nvalues * 2 # size of the search space\nshift = 1.0\n\n# Create random matrix\nxs = np.random.normal(size=size ** 2).reshape(size, size)\n\n# Create symmetric matrix\nmat = xs + xs.T\n\n# Compute two eigenpairs selecting the eigenvalues with\n# largest algebraic value\nselection_rule = \"LargestAlge\"\nsymm_eigenvalues, symm_eigenvectors = \\\n spectra_dense_interface.symmetric_eigensolver(\n mat, nvalues, search_space, selection_rule)\n\n```\n> Note: **All functions return a tuple whith the resulting eigenvalues and eigenvectors.**\n> For more examples, please see the directory: `pfpyspectra/tests/`\n\n\n## Installation\nTo install pyspectra, do:\n```bash\n git clone git@gitee.com:PerfXLab/spectra4py.git\n cd pyspectra\n bash ./install.sh\n```\n## Test\nRun tests (including coverage) with:\n\n```bash\n pytest tests/test_dense_pyspectra.py\n pytest tests/test_sparse_pyspectra.py\n pytest tests/test_pyspectra.py\n # also you can just `pytest tests`\n```\n> **Help:** If you don't pass them all, don't worry, try a few more times. \n> I think that's because of the random parameter problem, It will not affect the use, can you help me?\n\n## License\nNo. Just for fun! \nThanks : \n [pyspectra](https://github.com/NLESC-JCER/pyspectra.git), \n [C++ Spectra library](https://github.com/yixuan/spectra)\n\n",
"bugtrack_url": null,
"license": "Apache Software License 2.0",
"summary": "Python interface to the C++ Spectra library",
"version": "0.1.0",
"project_urls": null,
"split_keywords": [
"pfpyspectra"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "035d2ff8155e17e9350e8fd654eada3eda9b321c74ec3abf24709327551e53e3",
"md5": "749a3227278a8826f9fd10b184a5afc4",
"sha256": "7083f7287cff13a8ef6ebe4321254f2e0321e6959faf9cc512f02b9f0d51249d"
},
"downloads": -1,
"filename": "pfpyspectra-0.1.0.tar.gz",
"has_sig": false,
"md5_digest": "749a3227278a8826f9fd10b184a5afc4",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 9726,
"upload_time": "2023-08-08T07:53:29",
"upload_time_iso_8601": "2023-08-08T07:53:29.704181Z",
"url": "https://files.pythonhosted.org/packages/03/5d/2ff8155e17e9350e8fd654eada3eda9b321c74ec3abf24709327551e53e3/pfpyspectra-0.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-08-08 07:53:29",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "pfpyspectra"
}