a KLU solver for JAX
Project description
KLUJAX
A sparse linear solver for JAX based on the efficient KLU algorithm.
CPU & float64
This library is a wrapper around the SuiteSparse KLU algorithms. This means the algorithm is only implemented for C-arrays and hence is only available for CPU arrays with double precision, i.e. float64 or complex128.
Note that this will be enforced at import of klujax
!
Usage
The klujax
library provides a single function solve(A, b)
, which solves for x
in
the linear system Ax=b
A is a sparse tensor in COO-format with shape mxm
and x and b
have shape mxn
. Note that JAX does not have a native sparse matrix representation and
hence A should be represented as a tuple of two index arrays and a value
array: (Ai, Aj, Ax)
.
import jax.numpy as jnp
from klujax import solve
b = jnp.array([8, 45, -3, 3, 19], dtype=jnp.float64)
A_dense = jnp.array([[2, 3, 0, 0, 0],
[3, 0, 4, 0, 6],
[0, -1, -3, 2, 0],
[0, 0, 1, 0, 0],
[0, 4, 2, 0, 1]], dtype=jnp.float64)
Ai, Aj = jnp.where(jnp.abs(A_dense) > 0)
Ax = A_dense[Ai, Aj]
result_ref = jnp.linalg.inv(A_dense)@b
result = solve(Ai, Aj, Ax, b)
print(jnp.abs(result - result_ref) < 1e-12)
print(result)
[ True True True True True]
[1. 2. 3. 4. 5.]
Installation
The library is dynamically linked to the SuiteSparse C++ library. The easiest way to install is as follows:
conda install pybind11 suitesparse
pip install klujax
There exist pre-built wheels for Linux and Windows (python 3.8+). If no compatible wheel is found, however, pip will attempt to install the library from source... make sure you have the necessary build dependencies installed.
Linux
On linux, having gcc
and g++
available in your path should be sufficient to be able
to build the library from source.
Windows
On Windows, installing from source is a bit more involved as typically the build dependencies are not installed. To install those, download Visual Studio Community 2017 from here. During installation, go to Workloads and select the following workloads:
- Desktop development with C++
- Python development
Then go to Individual Components and select the following additional items:
- C++/CLI support
- VC++ 2015.3 v14.00 (v140) toolset for desktop
Then, download and install Microsoft Visual C++ Redistributable from here.
After these installation steps, run the following commands inside a x64 Native Tools Command Prompt for VS 2017, after activating your conda environment:
set DISTUTILS_USE_SDK=1
conda install pybind11 suitesparse
pip install klujax
License & Credits
© Floris Laporte 2022, LGPL-2.1
This library was partly based on:
- torch_sparse_solve, LGPL-2.1
- SuiteSparse, LGPL-2.1
- kagami-c/PyKLU, LGPL-2.1
- scipy.sparse, BSD-3
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
Hashes for klujax-0.1.3-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 88005708abfbafa3cf8d4d2f128be4c53ec1843a4c049bb978c35467968e0c97 |
|
MD5 | 67071c630fab0fe4ce5d822cff09cad1 |
|
BLAKE2b-256 | 6aa1f98ae81910a3669c47f52aa03bae76037099eaa1fd08a10aa7f2f57e7776 |
Hashes for klujax-0.1.3-cp310-cp310-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 12017607d5fa3da3ed43436d581514e61849b96a56d7bd0cbcca11c760b7bcce |
|
MD5 | 979ae39be1823212873ecf64b356c76b |
|
BLAKE2b-256 | a7a23589571212c37e9a4d45402fdd43f611b639a58a4cd3b3cd488b3e28c768 |
Hashes for klujax-0.1.3-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dd856bbad5c9524b8085a35adfa32efa836a835690ea4881b1ad3956fb69d97d |
|
MD5 | d56a7210434c825a38780d9f8592853e |
|
BLAKE2b-256 | 334725ea14f31174ea669f8ce0df7e5f7d7349b421219ed1844e6ce8a45b8c9e |
Hashes for klujax-0.1.3-cp39-cp39-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 57b8a755a707f3484f04236938e98c4e4b080c773a88403d431aaf2cf2648ce9 |
|
MD5 | 0eb752ed8ae4e7e0deeae54c628320a0 |
|
BLAKE2b-256 | 0b8cfa0d6132475348e6581cabe576f097e4487d3e76cc81c28b8feb2187a7b9 |
Hashes for klujax-0.1.3-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6ae0427b492ef1a7051fb94d544531a2e4becd2e4d3e17cdb3a891edee37a344 |
|
MD5 | e2241646af4f0b7e7e9ec5beb8346287 |
|
BLAKE2b-256 | 20a1ef56bd3acf3b37681ea4caf4028a2676f2433b47d4a0c7a93b4a27d413fc |
Hashes for klujax-0.1.3-cp38-cp38-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c614c5a2edb79c2fe975884d839cbd7d4ef29ea3f5ae258dddda68492875bf0 |
|
MD5 | 395a6dfd9be2929d981f48e64761cd06 |
|
BLAKE2b-256 | f6cce0080c89374f3a3025241f4f0885ad14cf76b48accd37918f82f35e573b9 |