lrux 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- lrux-0.1.0/LICENSE +21 -0
- lrux-0.1.0/PKG-INFO +9 -0
- lrux-0.1.0/README.md +121 -0
- lrux-0.1.0/lrux/__init__.py +3 -0
- lrux-0.1.0/lrux/det_lru.py +563 -0
- lrux-0.1.0/lrux/pf_lru.py +429 -0
- lrux-0.1.0/lrux/pfaffian.py +300 -0
- lrux-0.1.0/lrux.egg-info/PKG-INFO +9 -0
- lrux-0.1.0/lrux.egg-info/SOURCES.txt +16 -0
- lrux-0.1.0/lrux.egg-info/dependency_links.txt +1 -0
- lrux-0.1.0/lrux.egg-info/requires.txt +1 -0
- lrux-0.1.0/lrux.egg-info/top_level.txt +1 -0
- lrux-0.1.0/pyproject.toml +20 -0
- lrux-0.1.0/setup.cfg +4 -0
- lrux-0.1.0/tests/__init__.py +0 -0
- lrux-0.1.0/tests/test_det_lru.py +131 -0
- lrux-0.1.0/tests/test_pf_lru.py +129 -0
- lrux-0.1.0/tests/test_pfaffian.py +93 -0
lrux-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Chen Ao
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
lrux-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: lrux
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Fast low-rank updates (LRU) of matrix determinants and pfaffians in JAX
|
|
5
|
+
Author-email: Ao Chen <chenao.phys@gmail.com>, Christopher Roth <christopher_roth@utexas.edu>
|
|
6
|
+
Requires-Python: >=3.8
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Requires-Dist: jax>=0.4.4
|
|
9
|
+
Dynamic: license-file
|
lrux-0.1.0/README.md
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
<h1 align='center'>lrux</h1>
|
|
2
|
+
|
|
3
|
+
Fast low-rank update (LRU) of matrix determinants and pfaffians in [JAX](https://github.com/jax-ml/jax)
|
|
4
|
+
|
|
5
|
+
[đź“– Documentation](https://chenao-phys.github.io/lrux/)
|
|
6
|
+
|
|
7
|
+
## What's low-rank update?
|
|
8
|
+
|
|
9
|
+
In quantum physics and many other fields, it often happens that we have computed $\det(\mathbf{A}_0)$ and need to compute $\det(\mathbf{A}_1)$, where $\mathbf{A}_0$ and $\mathbf{A}_1$ are nearly identical. Due to the similarity of $\mathbf{A}_0$ and $\mathbf{A}_1$, we can well expect that in many cases $\det(\mathbf{A}_1)$ doesn't have to be recomputed from scratch.
|
|
10
|
+
|
|
11
|
+
Consider a special case that $\mathbf{A}_0$ and $\mathbf{A}_1$ are only different by one row. We can express their difference as
|
|
12
|
+
<!-- $$
|
|
13
|
+
\mathbf{A}_1 - \mathbf{A}_0 = \begin{pmatrix}
|
|
14
|
+
0 & ... & 0 \\
|
|
15
|
+
\vdots && \vdots \\
|
|
16
|
+
0 & ... & 0 \\
|
|
17
|
+
u_1 & ... & u_n \\
|
|
18
|
+
0 & ... & 0 \\
|
|
19
|
+
\vdots && \vdots \\
|
|
20
|
+
0 & ... & 0
|
|
21
|
+
\end{pmatrix}
|
|
22
|
+
= \begin{pmatrix}
|
|
23
|
+
0 \\ \vdots \\ 0 \\ 1 \\ 0 \\ \vdots \\ 0
|
|
24
|
+
\end{pmatrix}
|
|
25
|
+
(u_1, ..., u_n)
|
|
26
|
+
= \mathbf{vu}^T.
|
|
27
|
+
$$ -->
|
|
28
|
+
<div align="center">
|
|
29
|
+
<img src="./images/A1_A0.svg"/>
|
|
30
|
+
</div>
|
|
31
|
+
|
|
32
|
+
Then $\mathbf{A}_1$ can be viewed as a low-rank update to $\mathbf{A}_0$, and the [matrix determinant lemma](https://en.wikipedia.org/wiki/Matrix_determinant_lemma) tells us
|
|
33
|
+
<!-- $$
|
|
34
|
+
\det(\mathbf{A}_1) = \det (\mathbf{A}_0 + \mathbf{vu}^T)
|
|
35
|
+
= \det (\mathbf{A}_0) (1 + \mathbf{u}^T \mathbf{A}_0^{-1} \mathbf{v}).
|
|
36
|
+
$$ -->
|
|
37
|
+
<div align="center">
|
|
38
|
+
<img src="./images/detA1.svg"/>
|
|
39
|
+
</div>
|
|
40
|
+
|
|
41
|
+
If $\mathbf{A}_0^{-1}$ and $\det(\mathbf{A}_0)$ has been computed and stored earlier, one can immediately obtain $\det(\mathbf{A}_1)$ with $\mathcal{O}(n^2)$ complexity for any general $\mathbf{u}$ and $\mathbf{v}$, instead of the original determinant complexity $\mathcal{O}(n^3)$. The following code shows how this is done with lrux, where `lrux.det_lru` returns the ratio
|
|
42
|
+
<!-- $$
|
|
43
|
+
r = \frac{\det(\mathbf{A}_1)}{\det (\mathbf{A}_0)} = 1 + \mathbf{u}^T \mathbf{A}_0^{-1} \mathbf{v}.
|
|
44
|
+
$$ -->
|
|
45
|
+
<div align="center">
|
|
46
|
+
<img src="./images/ratio.svg"/>
|
|
47
|
+
</div>
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
import jax
|
|
51
|
+
|
|
52
|
+
# 64-bit recommended for numerical precision
|
|
53
|
+
jax.config.update("jax_enable_x64", True)
|
|
54
|
+
|
|
55
|
+
import jax.numpy as jnp
|
|
56
|
+
import jax.random as jr
|
|
57
|
+
from lrux import det_lru
|
|
58
|
+
|
|
59
|
+
n = 10
|
|
60
|
+
A0 = jr.normal(jr.key(0), (n, n))
|
|
61
|
+
u = jr.normal(jr.key(1), (n,))
|
|
62
|
+
v = 5 # one-hot vector index
|
|
63
|
+
detA0 = jnp.linalg.det(A0)
|
|
64
|
+
Ainv = jnp.linalg.inv(A0)
|
|
65
|
+
|
|
66
|
+
ratio = det_lru(Ainv, u, v)
|
|
67
|
+
detA1_lru = detA0 * ratio
|
|
68
|
+
|
|
69
|
+
A1 = A0.at[v, :].add(u)
|
|
70
|
+
assert jnp.isclose(detA1_lru, jnp.linalg.det(A1))
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
## Consecutive updates
|
|
75
|
+
|
|
76
|
+
Sometimes we need to keep computing $\det(\mathbf{A}_2)$ by a low-rank update of $\mathbf{A}_1$, in which case $\mathbf{A}_1^{-1}$ is required. Utilizing the [Sherman–Morrison formula](https://en.wikipedia.org/wiki/Sherman%E2%80%93Morrison_formula), one can also obtain the low-rank update of matrix inverse as
|
|
77
|
+
<!-- $$
|
|
78
|
+
\mathbf{A}_1^{-1} = (\mathbf{A}_0 + \mathbf{vu}^T)^{-1} = \mathbf{A}_0^{-1} - \mathbf{A}_0^{-1} \mathbf{v} r^{-1} \mathbf{u}^T \mathbf{A}_0^{-1},
|
|
79
|
+
$$ -->
|
|
80
|
+
<div align="center">
|
|
81
|
+
<img src="./images/A1inv.svg"/>
|
|
82
|
+
</div>
|
|
83
|
+
|
|
84
|
+
where the complexity is again $\mathcal{O}(n^2)$ instead of $\mathcal{O}(n^3)$. Following the previous example code, one can add a few lines below to perform consecutive low-rank updates.
|
|
85
|
+
|
|
86
|
+
```python
|
|
87
|
+
ratio, Ainv = det_lru(Ainv, u, v, return_update=True)
|
|
88
|
+
assert jnp.allclose(Ainv, jnp.linalg.inv(A1))
|
|
89
|
+
|
|
90
|
+
u_new = jr.normal(jr.key(2), (n,))
|
|
91
|
+
v_new = 6
|
|
92
|
+
|
|
93
|
+
ratio, Ainv = det_lru(Ainv, u_new, v_new, return_update=True)
|
|
94
|
+
detA2_lru = detA1_lru * ratio
|
|
95
|
+
|
|
96
|
+
A2 = A1.at[v_new, :].add(u_new)
|
|
97
|
+
assert jnp.isclose(detA2_lru, jnp.linalg.det(A2))
|
|
98
|
+
assert jnp.allclose(Ainv, jnp.linalg.inv(A2))
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
## What does lrux provide?
|
|
103
|
+
|
|
104
|
+
The main functions of lrux include `det_lru`, `det_lru_delayed`, `pf_lru`, and `pf_lru_delayed`. They provide:
|
|
105
|
+
|
|
106
|
+
- Row and column updates
|
|
107
|
+
- General rank-k updates
|
|
108
|
+
- Delayed updates
|
|
109
|
+
- `jit` and `vmap` compatibility
|
|
110
|
+
|
|
111
|
+
As the [pfaffian](https://en.wikipedia.org/wiki/Pfaffian) is not directly supported in JAX, we also provide backward-compatible functions `pf` and `slogpf` for pfaffian computations.
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
## Installation
|
|
115
|
+
|
|
116
|
+
Requires Python 3.8+ and JAX 0.4.4+
|
|
117
|
+
|
|
118
|
+
```
|
|
119
|
+
git clone https://github.com/ChenAo-Phys/lrux.git
|
|
120
|
+
pip install ./lrux
|
|
121
|
+
```
|