https://github.com/technologicat/pylu
Small nogil-compatible linear equation system solver
https://github.com/technologicat/pylu
cython linear-equations numerical numpy python python2 python27 python3 python34 solver
Last synced: 29 days ago
JSON representation
Small nogil-compatible linear equation system solver
- Host: GitHub
- URL: https://github.com/technologicat/pylu
- Owner: Technologicat
- License: bsd-2-clause
- Created: 2017-04-07T12:47:33.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2026-04-11T09:24:07.000Z (about 2 months ago)
- Last Synced: 2026-04-11T11:23:32.944Z (about 2 months ago)
- Topics: cython, linear-equations, numerical, numpy, python, python2, python27, python3, python34, solver
- Language: Cython
- Homepage:
- Size: 191 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
- Authors: AUTHORS.md
Awesome Lists containing this project
README
# PyLU









[](http://makeapullrequest.com/)
Small nogil-compatible Cython-based solver for linear equation systems `A x = b`.
We use [semantic versioning](https://semver.org/).
For my stance on AI contributions, see the [collaboration guidelines](https://github.com/Technologicat/substrate-independent/blob/main/collaboration.md).
## Introduction
The algorithm is LU decomposition with partial pivoting (row swaps). The code requires only NumPy and Cython.
The main use case for PyLU (over `numpy.linalg.solve`) is solving many small systems inside a `nogil` block in Cython code, without requiring SciPy (for its `cython_lapack` module).
Python and Cython interfaces are provided. The API is designed to be as simple to use as possible.
The arrays are stored using the C memory layout.
A rudimentary banded solver is also provided, based on detecting the band structure (if any) from the initial full LU decomposition. For cases where `L` and `U` have small bandwidth, this makes the `O(n**2)` solve step run faster. The LU decomposition still costs `O(n**3)`, so this is useful only if the system is small, and the same matrix is needed for a large number of different RHS vectors. (This can be the case e.g. in integration of ODE systems with a constant-in-time mass matrix.)
## Examples
Basic usage:
```python
import numpy as np
import pylu
A = np.random.random( (5,5) )
b = np.random.random( 5 )
x = pylu.solve( A, b )
```
For a complete tour, see the [test suite](tests/test_dgesv.py).
The main item of interest, however, is the Cython API in [`dgesv.pxd`](pylu/dgesv.pxd). The main differences to the Python API are:
- Function names end with `_c`.
- Explicit sizes must be provided, since the arrays are accessed via raw pointers.
- The result array `x` must be allocated by the caller, and passed in as an argument. See [`dgesv.pyx`](pylu/dgesv.pyx) for examples on how to do this in NumPy.
## Installation
### From PyPI
```bash
pip install pylu
```
### From source
```bash
git clone https://github.com/Technologicat/pylu.git
cd pylu
pip install .
```
For maximum performance on your machine, build with architecture-specific optimizations:
```bash
CFLAGS="-march=native" pip install --no-build-isolation .
```
Pre-built wheels from PyPI use generic `-O2` because `-march=native` bakes in the instruction set of the build machine — a wheel built with AVX-512 would crash on a CPU without it. Building from source avoids this and lets the compiler target your specific hardware.
### Development setup
PyLU uses [meson-python](https://meson-python.readthedocs.io/) as its build backend and [PDM](https://pdm-project.org/) for dependency management.
```bash
git clone https://github.com/Technologicat/pylu.git
cd pylu
pdm install # creates venv, installs dev deps
pip install --no-build-isolation -e . # editable install (needs venv activated)
```
The `--no-build-isolation` flag is required for editable installs with meson-python — the on-import rebuild mechanism needs build dependencies (Cython, NumPy, meson, ninja) to remain available in the environment, not just in a throwaway build-time overlay.
**PATH note:** The meson-python editable loader needs `meson` and `ninja` on `PATH`. If you get rebuild errors, ensure the venv's `bin/` is on the path:
```bash
export PATH="$(pwd)/.venv/bin:$PATH"
```
**Note on editable installs:** After modifying `.pyx` or `.pxd` files, the next import auto-rebuilds the Cython extension — no manual reinstall step needed. For a clean non-editable build, use `pip install .` and reinstall after changes.
## Dependencies
- [NumPy](http://www.numpy.org) ≥ 1.25
- [Cython](http://www.cython.org) ≥ 3.0 (build-time only)
Requires Python ≥ 3.11.
## License
[BSD](LICENSE.md). Copyright 2016–2026 Juha Jeronen, University of Jyväskylä, and JAMK University of Applied Sciences.
#### Acknowledgement
This work was financially supported by the Jenny and Antti Wihuri Foundation.