Matrix-free numerical linear algebra including trace-estimation.
Project description
matfree: Matrix-free linear algebra in JAX
Randomised and deterministic matrix-free methods for trace estimation, matrix functions, and/or matrix factorisations. Builds on JAX.
Installation
To install the package, run
pip install matfree
Important: This assumes you already have a working installation of JAX.
To install JAX, follow these instructions.
To combine matfree with a CPU version of JAX, run
pip install matfree[cpu]
which is equivalent to combining pip install jax[cpu] with pip install matfree.
Minimal example
Imports:
>>> import jax
>>> import jax.numpy as jnp
>>> from matfree import hutchinson, montecarlo, slq
>>> a = jnp.reshape(jnp.arange(12.0), (6, 2))
>>> key = jax.random.PRNGKey(1)
Estimate traces as such:
>>> normal = montecarlo.normal(shape=(2,))
>>> matvec = lambda x: a.T @ (a @ x)
>>> trace = hutchinson.trace(matvec, key=key, sample_fun=normal)
>>> print(jnp.round(trace))
514.0
>>> # for comparison:
>>> print(jnp.round(jnp.trace(a.T @ a)))
506.0
The number of keys determines the number of sequential batches. Many small batches reduces memory. Few large batches increases memory and runtime.
Determine the number of samples per batch as follows.
>>> trace = hutchinson.trace(matvec, key=key, sample_fun=normal, num_batches=10)
>>> print(jnp.round(trace))
508.0
>>> # for comparison:
>>> print(jnp.round(jnp.trace(a.T @ a)))
506.0
More tutorials
- Control variates: Use control variates and multilevel schemes to reduce variances. (LINK)
- Log-determinants: Use stochastic Lanczos quadrature to compute matrix functions. (LINK)
- Higher moments and UQ: Compute means, variances, and other moments simultaneously. (LINK)
- Vector calculus: Use matrix-free linear algebra to implement vector calculus. (LINK)
Contributing
Contributions are absolutely welcome! Most contributions start with an issue. Please don't hesitate to create issues in which you ask for features, give feedback on performances, or simply want to reach out.
To make a pull request, proceed as follows:
Fork the repository.
Install all dependencies with pip install .[full] or pip install -e .[full].
Make your changes.
From the root of the project, run the tests via make test, and check out make format and make lint as well.
Use the pre-commit hook if you like.
When making a pull request, keep in mind the following (rough) guidelines:
- Most PRs resolve an issue and change ~10 lines of code (or less)
- Most PRs contain a single commit. Here is how we can write better commit messages.
- Almost every enhancement (e.g. a new feature) is covered by a test.
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 Distribution
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 matfree-0.0.7.tar.gz.
File metadata
- Download URL: matfree-0.0.7.tar.gz
- Upload date:
- Size: 30.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.1 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a0f141b131281ee2a51a631f1f550cc1eca9944338da76f95abbd58d126cdd16
|
|
| MD5 |
ddba4266145a0ec2bbca06432d971ed2
|
|
| BLAKE2b-256 |
fd32af1d87c32f21fb633a4db87ff3acfc56d2478695bd1066048b7957347f89
|
File details
Details for the file matfree-0.0.7-py3-none-any.whl.
File metadata
- Download URL: matfree-0.0.7-py3-none-any.whl
- Upload date:
- Size: 31.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.1 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1853e25197bbeaa668da5ba9d110804b611d2c1ca966dc1c57adb74b95a21947
|
|
| MD5 |
96e827a0b4d6d4ee8180e620061ac7d3
|
|
| BLAKE2b-256 |
1a75534dde2f1b233451453fbd79ab392c13417e7e8a7b983fdb52ddbf1d1718
|