vector-structure 1.0.0__py3-none-any.whl
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.
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
from typing import Sequence
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class SimpleSlice:
|
|
5
|
+
"""Represents a contiguous range.
|
|
6
|
+
|
|
7
|
+
Behaves like a `slice` object.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
def __init__(self, start: int, stop: int):
|
|
11
|
+
self.start = start
|
|
12
|
+
self.stop = stop
|
|
13
|
+
|
|
14
|
+
step = 1
|
|
15
|
+
|
|
16
|
+
@staticmethod
|
|
17
|
+
def from_slice(sl: slice) -> "SimpleSlice":
|
|
18
|
+
"""Translate a `slice` into a SimpleSlice."""
|
|
19
|
+
if sl.step not in (1, None):
|
|
20
|
+
raise ValueError()
|
|
21
|
+
return SimpleSlice(sl.start, sl.stop)
|
|
22
|
+
|
|
23
|
+
def __len__(self):
|
|
24
|
+
return self.start - self.stop
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class VectorStructure:
|
|
28
|
+
"""Helper class to describe block vectors and block matrices."""
|
|
29
|
+
|
|
30
|
+
def __init__(self, sizes: Sequence[tuple[str, int]]):
|
|
31
|
+
cuts = {}
|
|
32
|
+
start = 0
|
|
33
|
+
for k, v in sizes:
|
|
34
|
+
cuts[k] = slice(start, start + v)
|
|
35
|
+
start += v
|
|
36
|
+
self.size = start
|
|
37
|
+
self.cuts = cuts
|
|
38
|
+
|
|
39
|
+
def __getitem__(self, idx: str | Sequence[str] | slice) -> SimpleSlice:
|
|
40
|
+
if isinstance(idx, str):
|
|
41
|
+
return self.cuts[idx]
|
|
42
|
+
elif isinstance(idx, slice):
|
|
43
|
+
if idx.step is not None:
|
|
44
|
+
raise ValueError("slice step [start:stop:step] not supported")
|
|
45
|
+
start, stop = None, None
|
|
46
|
+
if idx.start is None:
|
|
47
|
+
start = 0
|
|
48
|
+
if idx.stop is None:
|
|
49
|
+
stop = self.size
|
|
50
|
+
for name, cut in self.cuts.items():
|
|
51
|
+
if name == idx.start:
|
|
52
|
+
start = cut.start
|
|
53
|
+
if name == idx.stop:
|
|
54
|
+
stop = cut.start
|
|
55
|
+
break
|
|
56
|
+
if start is None or stop is None:
|
|
57
|
+
raise ValueError(f"{idx.start} or {idx.stop} not found")
|
|
58
|
+
return SimpleSlice(start, stop)
|
|
59
|
+
else:
|
|
60
|
+
first, *more = idx
|
|
61
|
+
start = self.cuts[first].start
|
|
62
|
+
stop = self.cuts[first].stop
|
|
63
|
+
for idx in more:
|
|
64
|
+
next_cut = self.cuts[idx]
|
|
65
|
+
assert next_cut.start == stop
|
|
66
|
+
stop = next_cut.stop
|
|
67
|
+
return SimpleSlice(start, stop)
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: vector-structure
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Project-URL: Homepage, https://github.com/warggr/vector-structure
|
|
5
|
+
Project-URL: Issues, https://github.com/warggr/vector-structure/issues
|
|
6
|
+
Author-email: Pierre Ballif <pierre@ballif.eu>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Keywords: block,matrix,structure,vector
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Programming Language :: Python
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Requires-Python: >=3.5
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
|
|
16
|
+
# vector-structure
|
|
17
|
+
|
|
18
|
+
Lightweight utilities for working with _structured flat vectors_ and _block matrices_ in NumPy.
|
|
19
|
+
|
|
20
|
+
# Example usage
|
|
21
|
+
|
|
22
|
+
Consider a Newton algorithm for solving the KKT conditions, as described [here](https://won-j.github.io/M1399_000200-2021fall/lectures/22-newton/newton_constr.html):
|
|
23
|
+
|
|
24
|
+
$$\begin{pmatrix}
|
|
25
|
+
\nabla^2 f(x) & \color{gray}{Df(x)^T} & A^T \\
|
|
26
|
+
\color{gray}{-diag(\lambda) Df(x)} & \color{gray}{-diag(f(x))} & \color{gray}{0} \\
|
|
27
|
+
A & \color{gray}{0} & 0
|
|
28
|
+
\end{pmatrix} \begin{pmatrix} x^* \\ \color{gray}{\lambda^\*} \\ v^\* \end{pmatrix} = \begin{pmatrix} \\ \dots \\
|
|
29
|
+
\end{pmatrix}$$
|
|
30
|
+
|
|
31
|
+
where the grey components are only required when inequality constraints are present.
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
from vector_structure import VectorStructure
|
|
35
|
+
|
|
36
|
+
structure = [("x", n)]
|
|
37
|
+
if ineq_constraints:
|
|
38
|
+
structure.append(("lambda", m))
|
|
39
|
+
structure.append(("mu", p))
|
|
40
|
+
|
|
41
|
+
vs = VectorStructure(structure)
|
|
42
|
+
|
|
43
|
+
M = np.zeros((vs.size, vs.size))
|
|
44
|
+
M[vs["x"], vs["x"]] = nabla2(f)(x)
|
|
45
|
+
if ineq_constraints:
|
|
46
|
+
M[vs["x"], vs["lambda"]] = Df(x).T
|
|
47
|
+
M[vs["x"], vs["mu"]] = A
|
|
48
|
+
...
|
|
49
|
+
|
|
50
|
+
x_lambda_mu = np.linalg.solve(M, r)
|
|
51
|
+
x = x_lambda_mu[vs["x"]]
|
|
52
|
+
if ineq_constraints:
|
|
53
|
+
lambda = x_lambda_mu[vs["lambda"]]
|
|
54
|
+
```
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
vector_structure/__init__.py,sha256=3PBOBBrSYXwXkrGVUjUPOtR78CWEPyCBUCl8c8U5ztY,77
|
|
2
|
+
vector_structure/structure.py,sha256=absSkXx2tLGKInDujESm5g3H0AcIplpnQHBVCXnYHEM,2052
|
|
3
|
+
vector_structure-1.0.0.dist-info/METADATA,sha256=6Qn2Hw8vldA09BFM_0ksRrkIHbb4Nt0u7EHywhzkZRM,1716
|
|
4
|
+
vector_structure-1.0.0.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
|
|
5
|
+
vector_structure-1.0.0.dist-info/licenses/LICENSE,sha256=9vbyUaxsJ032bytdPkeCRKdQ8e7dWKXgFVKL7sEjYno,1070
|
|
6
|
+
vector_structure-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Pierre Ballif
|
|
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.
|