https://github.com/mrcdr/pylanczos
Python wrapper for Lambda Lanczos
https://github.com/mrcdr/pylanczos
eigenvector-calculation lanczos-algorithm matrix numpy python
Last synced: 3 months ago
JSON representation
Python wrapper for Lambda Lanczos
- Host: GitHub
- URL: https://github.com/mrcdr/pylanczos
- Owner: mrcdr
- License: mit
- Created: 2020-03-09T19:07:15.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2023-12-02T08:15:15.000Z (about 2 years ago)
- Last Synced: 2025-01-15T22:23:27.866Z (about 1 year ago)
- Topics: eigenvector-calculation, lanczos-algorithm, matrix, numpy, python
- Language: Python
- Homepage:
- Size: 64.5 KB
- Stars: 17
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

[]()
[]()
# PyLanczos
## Overview
**PyLanczos** is a Lanczos diagonalization library.
Its core part is written in C++ as [LambdaLanczos](https://github.com/mrcdr/lambda-lanczos).
## Usage
All samples are available [here](https://github.com/mrcdr/pylanczos/tree/master/sample).
### NumPy and SciPy matrix
``` python
matrix = np.array([[2.0, 1.0, 1.0],
[1.0, 2.0, 1.0],
[1.0, 1.0, 2.0]])
engine = PyLanczos(matrix, True, 2) # Find 2 maximum eigenpairs
eigenvalues, eigenvectors = engine.run()
print("Eigenvalue: {}".format(eigenvalues))
print("Eigenvector:")
print(eigenvectors)
```
Note: Use of SciPy sparse matrix is recommended to take full advantage of Lanczos algorithm.
### Customized operation
You can also attach your customized function:
```python
tensor = np.zeros((2, 2, 2, 2), dtype='float64')
tensor[0, 0, 0, 0] = 1
# and so on...
## Matrix-vector (or tensor-matrix) multiplication function
def mv_mul(v_in, v_out):
v_in.shape = (2, 2)
v_out.shape = (2, 2)
np.einsum("ijkl, kl -> ij", tensor, v_in, out=v_out, optimize="optimal")
## Calculate an "eigenmatrix" for the 4th-order tensor.
engine = PyLanczos.create_custom(mv_mul, n, 'float64', False, 1) # Find 1 minimum eigenpair
eigenvalues, eigenmatrices = engine.run()
eigenmatrices.shape = (2, 2)
print("Eigenvalue: {}".format(eigenvalues))
print("Eigenmatrix:")
print(eigenmatrices)
```
There is [a full sample](https://github.com/mrcdr/pylanczos/tree/master/sample/sample3_custom.py) with detailed description.
## Installation
```sh
pip install pylanczos
```
## Requirements
C++11 compatible environment
## License
[MIT](https://github.com/mrcdr/lambda-lanczos/blob/master/LICENSE)
## Author
[mrcdr](https://github.com/mrcdr)
## PyPI repository
[pylanczos](https://pypi.org/project/pylanczos/)