Fast selected inversion of sparse symmetric matrices (Python wrapper for SelInv)
Project description
selinv
[!WARNING] The Python bindings and build infrastructure of this package have been largely generated with the help of AI (LLM). While the code is tested, it may contain inaccuracies or rough edges. Review and contributions are welcome.
selinv is a Python package for fast selected inversion of sparse symmetric matrices. It wraps the Fortran SelInv library by Lin Lin, Chao Yang, Juan Meza, Jianfeng Lu, Lexing Ying, and Weinan E.
Selected inversion computes only the entries of A⁻¹ that correspond to the non-zero pattern of the sparse LDL' factor of A (or a superset thereof), making it far cheaper than a full inversion for large sparse systems.
Features
- Diagonal of A⁻¹ via
selinv(), or full selected inverse viaselinvfull() - Convenience wrapper
fullselinvfor sub-block extraction and diagonal access - Input via any
scipy.sparseformat (CSC, CSR, COO, …) - Fortran core with BLAS/LAPACK acceleration
- Optional METIS fill-reducing reordering
Installation
The selinv package works on python>=3.11 for Linux and macos. It should work on Windows too, but it has not been tested.
Install from Pypi
If using pixi, add selinv to your pixi.toml file:
pixi add --pypi selinv
Or with pip (similarly with pipx ou uv):
pip install selinv
Install from source
Using pixi (recommanded)
# As of pixi-0.66 you must add `preview = ["pixi-build"]`
# to the `workspace` or `project` table of your pixi.toml.
pixi add --git https://gitlab.in2p3.fr/lemaitre/selinv.git selinv --branch main
Using pip
You need a C, C++, and Fortran compiler, CMake ≥ 3.18, and BLAS/LAPACK development libraries installed on your system.
System dependencies (Linux):
# Debian / Ubuntu
sudo apt-get install gfortran g++ cmake liblapack-dev libblas-dev
# RHEL / Fedora
sudo dnf install gcc-gfortran gcc-c++ cmake lapack-devel blas-devel
System dependencies (macOS):
xcode-select --install
brew install gcc cmake # provides gfortran via the gcc formula
On macOS the Accelerate framework is used automatically for BLAS/LAPACK.
Install the package:
git clone https://gitlab.in2p3.fr/lemaitre/selinv.git
cd selinv
pip install .
Quick Start
import numpy as np
import scipy.sparse as sp
from selinv import selinv, selinvfull, fullselinv
# Build a small sparse SPD tridiagonal matrix (CSC format)
diag = np.array([4.0, 4.0, 4.0, 4.0])
off = np.array([-1.0, -1.0, -1.0])
A = sp.diags([off, diag, off], [-1, 0, 1], format="csc")
# 1. Compute the diagonal of A^{-1}
d = selinv(A)
print("Diagonal of A^{-1}:", d)
# 2. Compute the full selected inverse with an explicit permutation
P = np.arange(A.shape[0], dtype=np.int32) # identity permutation
L, Ainv, perm = selinvfull(A, P)
# 3. Use the fullselinv wrapper for convenient access
result = fullselinv(L, Ainv, perm)
print("Diagonal:", result.diag()) # same as selinv(A)
print("Sub-block [[0,1]]:\n", result[[0, 1]]) # 2x2 dense block of A^{-1}
API Overview
selinv(mat, P=None) -> numpy.ndarray
Compute the diagonal of A⁻¹ via the SelInv algorithm.
| Parameter | Type | Description |
|---|---|---|
mat |
scipy.sparse matrix |
Sparse symmetric matrix (lower triangle stored) |
P |
numpy.ndarray or None |
Optional 0-based permutation vector. When None, Multiple Minimum Degree (MMD) reordering is used. |
Returns a 1-D numpy.ndarray of shape (n,) with dtype float64
containing the diagonal elements of A⁻¹ in the original ordering.
selinvfull(mat, P) -> tuple[csc_array, csc_array, ndarray]
Compute the full selected inverse: all entries of A⁻¹ at positions where the LDL' factor L has a non-zero.
| Parameter | Type | Description |
|---|---|---|
mat |
scipy.sparse matrix |
Sparse symmetric matrix (lower triangle stored) |
P |
numpy.ndarray |
0-based permutation vector (required) |
Returns a 3-tuple:
| Element | Type | Description |
|---|---|---|
L |
scipy.sparse.csc_array |
Lower-triangular factor (diagonal zeroed) |
Ainv |
scipy.sparse.csc_array |
Selected inverse in the permuted basis |
perm |
numpy.ndarray (int32) |
Inverse permutation vector: perm[i] gives the permuted position of original index i. May differ from the simple inverse of P due to elimination-tree postordering. |
fullselinv(L, Ainv, perm)
Convenience class wrapping the output of selinvfull().
| Method / Accessor | Description |
|---|---|
result.diag() |
Diagonal of A⁻¹ in the original ordering |
result[[i, j, ...]] |
Dense sub-block of A⁻¹ for the given row/column indices |
result.L |
The lower-triangular factor |
result.Ai |
The selected inverse (sparse) |
result.perm |
The inverse permutation |
Note:
result[[i, j]]only returns accurate values for index pairs that fall within the fill pattern of L. Entries outside the fill pattern will be zero, not the true inverse value. See the docstring for details.
Mathematical Background
Given a sparse symmetric matrix A, selected inversion computes the entries of A⁻¹ that correspond to the non-zero pattern of the LDL' factor of A. This is significantly cheaper than computing the full inverse and is exact (not approximate).
The algorithm exploits a recurrence first described by Takahashi, Fagan, and Chin (1973) and extended to a supernodal setting by Lin et al. (2011). The computational cost is O(|L|), the same as the factorization itself.
Applications include:
- Green's function calculations in computational physics
- Density matrix evaluation in linear-scaling DFT
- Marginal variances in Gaussian Markov Random Fields (Bayesian inference)
- Uncertainty quantification in large-scale inverse problems
See docs/math.md for a detailed derivation.
Building the Documentation
The project includes Sphinx documentation under docs/. To build it
locally:
pixi run -e dev docs
Then open docs/_build/html/index.html in your browser.
To clean the build:
pixi run -e dev docs-clean
Running the Tests
pixi run test
Or for a specific Python version:
pixi run -e py314 test
Releasing a New Version
This section is intended for maintainers.
Releases are driven by git tags: pushing a tag matching vX.Y.Z triggers
the CI/CD pipeline which builds wheels for all supported Python versions and
publishes them to PyPI automatically.
Version bumping is automated via bump-my-version,
available in the dev environment.
Step-by-step:
-
Update
CHANGELOG.md— move the items under[Unreleased]to a new section named after the upcoming version (e.g.[0.2.0] - 2025-01-31). Commit this change manually if you want it separate from the version bump. -
Bump the version — choose the appropriate increment:
pixi run -e dev bump-patch # 0.1.0 → 0.1.1 (bug fixes) pixi run -e dev bump-minor # 0.1.0 → 0.2.0 (new features) pixi run -e dev bump-major # 0.1.0 → 1.0.0 (breaking changes)
This updates both
versionfields inpyproject.toml, creates a commit (chore: release X.Y.Z), and tags itvX.Y.Z. -
Push the commit and the tag:
git push --follow-tags
The CI/CD pipeline then takes over: it builds the source distribution and
binary wheels, and uploads everything to PyPI via twine.
License
This Python wrapper is released under the BSD 3-Clause License. Copyright © 2025 Mathieu Bernard.
The original Fortran SelInv library (extern/selinv/) is distributed under
its own BSD-style license.
Copyright © 2011 The Regents of the University of California.
See LICENSE for the full text of both licenses.
Citation
If you use selinv in academic work, please cite the original SelInv paper:
Lin Lin, Chao Yang, Juan C. Meza, Jianfeng Lu, Lexing Ying, and Weinan E. SelInv — An Algorithm for Selected Inversion of a Sparse Symmetric Matrix. ACM Transactions on Mathematical Software, 37(4), Article 40, 2011. https://doi.org/10.1145/1916461.1916464
BibTeX
@article{lin2011selinv,
author = {Lin, Lin and Yang, Chao and Meza, Juan C. and Lu, Jianfeng
and Ying, Lexing and E, Weinan},
title = {{SelInv} --- An Algorithm for Selected Inversion of a
Sparse Symmetric Matrix},
journal = {ACM Transactions on Mathematical Software},
volume = {37},
number = {4},
pages = {40:1--40:19},
year = {2011},
doi = {10.1145/1916461.1916464},
}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file selinv-1.0.0.tar.gz.
File metadata
- Download URL: selinv-1.0.0.tar.gz
- Upload date:
- Size: 171.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
12beb0d7383787075d76f44dc2cb4b3b46498d3caf9abf375abd1d704b7123a2
|
|
| MD5 |
83bec1b28724e88882a642661c435ae4
|
|
| BLAKE2b-256 |
e8b77131246332b172a5a1006e91a43fbf32d2b4d48b0623b70b6dde5aa16e4a
|
File details
Details for the file selinv-1.0.0-cp314-cp314-manylinux_2_39_x86_64.whl.
File metadata
- Download URL: selinv-1.0.0-cp314-cp314-manylinux_2_39_x86_64.whl
- Upload date:
- Size: 47.1 MB
- Tags: CPython 3.14, manylinux: glibc 2.39+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5557871d87c330ad785857f879ee1dde19bc749930c97e6a6babd96641a6fd8e
|
|
| MD5 |
7ae68c3344eb2c41690ea7607aa6334a
|
|
| BLAKE2b-256 |
419f64f7ce0d672094d69d6315adf4662444ad4872eb692aafc0fb63c52bf709
|
File details
Details for the file selinv-1.0.0-cp313-cp313-manylinux_2_39_x86_64.whl.
File metadata
- Download URL: selinv-1.0.0-cp313-cp313-manylinux_2_39_x86_64.whl
- Upload date:
- Size: 47.1 MB
- Tags: CPython 3.13, manylinux: glibc 2.39+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
70d13d91b730eb3abc527933b12cd459674e417c6cf34e2c09fcebffdb35bea1
|
|
| MD5 |
984390756b70076913be6f17b23e3bda
|
|
| BLAKE2b-256 |
97d76705390c9c84c2c66805494ecb4668ddaa4c2c23ead661b3bc128968485a
|
File details
Details for the file selinv-1.0.0-cp312-cp312-manylinux_2_39_x86_64.whl.
File metadata
- Download URL: selinv-1.0.0-cp312-cp312-manylinux_2_39_x86_64.whl
- Upload date:
- Size: 47.1 MB
- Tags: CPython 3.12, manylinux: glibc 2.39+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4bb817d6ded614fa00c0456a5ad34cfaf6d39c7cdec01d9e089d5e73280222f9
|
|
| MD5 |
d4aedb098a206c01bae5b65756861c8e
|
|
| BLAKE2b-256 |
ed6959d457098f08cbc4c254d56dd0d55cda7b71c410fcc41d42f12445b15487
|
File details
Details for the file selinv-1.0.0-cp311-cp311-manylinux_2_39_x86_64.whl.
File metadata
- Download URL: selinv-1.0.0-cp311-cp311-manylinux_2_39_x86_64.whl
- Upload date:
- Size: 47.1 MB
- Tags: CPython 3.11, manylinux: glibc 2.39+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
504a861bc2e86a9501b9e8739ad949fac0d49222bff60f41eb69d80648e507d1
|
|
| MD5 |
6f4711249995d6ddeebce9ffb99ef6fd
|
|
| BLAKE2b-256 |
a96ced4ab9a8bff8b5a4b6072feb86d1882ab71368c9e508f9e89368b60119de
|
File details
Details for the file selinv-1.0.0-cp310-cp310-manylinux_2_39_x86_64.whl.
File metadata
- Download URL: selinv-1.0.0-cp310-cp310-manylinux_2_39_x86_64.whl
- Upload date:
- Size: 47.1 MB
- Tags: CPython 3.10, manylinux: glibc 2.39+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f4cd3c418720e325cccae33b8369a06e19a55b8cbb819f8a39d3dbd06f8ba603
|
|
| MD5 |
24a153d47807a4f4619a144f4a5c9539
|
|
| BLAKE2b-256 |
ea61c90f23c1b2a63ed1e1e9110bfb69ed06ada57d6d26b7e06fc11508ae7607
|