spacecore-optimize 0.0.1__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.
Files changed (43) hide show
  1. spacecore_optimize-0.0.1/CHANGELOG.md +14 -0
  2. spacecore_optimize-0.0.1/LICENSE +201 -0
  3. spacecore_optimize-0.0.1/MANIFEST.in +8 -0
  4. spacecore_optimize-0.0.1/PKG-INFO +101 -0
  5. spacecore_optimize-0.0.1/README.md +68 -0
  6. spacecore_optimize-0.0.1/docs/api.rst +32 -0
  7. spacecore_optimize-0.0.1/docs/changelog.rst +4 -0
  8. spacecore_optimize-0.0.1/docs/conf.py +16 -0
  9. spacecore_optimize-0.0.1/docs/index.rst +35 -0
  10. spacecore_optimize-0.0.1/pyproject.toml +50 -0
  11. spacecore_optimize-0.0.1/setup.cfg +4 -0
  12. spacecore_optimize-0.0.1/spacecore_optimize/__init__.py +53 -0
  13. spacecore_optimize-0.0.1/spacecore_optimize/_common.py +69 -0
  14. spacecore_optimize-0.0.1/spacecore_optimize/_optax.py +461 -0
  15. spacecore_optimize-0.0.1/spacecore_optimize/_scipy.py +216 -0
  16. spacecore_optimize-0.0.1/spacecore_optimize/functionals/__init__.py +36 -0
  17. spacecore_optimize-0.0.1/spacecore_optimize/functionals/_entropy.py +134 -0
  18. spacecore_optimize-0.0.1/spacecore_optimize/functionals/_huber.py +77 -0
  19. spacecore_optimize-0.0.1/spacecore_optimize/functionals/_least_squares.py +106 -0
  20. spacecore_optimize-0.0.1/spacecore_optimize/functionals/_proximal.py +193 -0
  21. spacecore_optimize-0.0.1/spacecore_optimize/functionals/_realified.py +166 -0
  22. spacecore_optimize-0.0.1/spacecore_optimize/functionals/_spectral.py +226 -0
  23. spacecore_optimize-0.0.1/spacecore_optimize/py.typed +0 -0
  24. spacecore_optimize-0.0.1/spacecore_optimize.egg-info/PKG-INFO +101 -0
  25. spacecore_optimize-0.0.1/spacecore_optimize.egg-info/SOURCES.txt +41 -0
  26. spacecore_optimize-0.0.1/spacecore_optimize.egg-info/dependency_links.txt +1 -0
  27. spacecore_optimize-0.0.1/spacecore_optimize.egg-info/requires.txt +14 -0
  28. spacecore_optimize-0.0.1/spacecore_optimize.egg-info/top_level.txt +1 -0
  29. spacecore_optimize-0.0.1/tests/__init__.py +0 -0
  30. spacecore_optimize-0.0.1/tests/_helpers.py +93 -0
  31. spacecore_optimize-0.0.1/tests/conftest.py +30 -0
  32. spacecore_optimize-0.0.1/tests/test_battery_functionals.py +200 -0
  33. spacecore_optimize-0.0.1/tests/test_composed_functional.py +30 -0
  34. spacecore_optimize-0.0.1/tests/test_contracts.py +127 -0
  35. spacecore_optimize-0.0.1/tests/test_least_squares.py +182 -0
  36. spacecore_optimize-0.0.1/tests/test_line_search_scipy.py +89 -0
  37. spacecore_optimize-0.0.1/tests/test_metric_handoff.py +63 -0
  38. spacecore_optimize-0.0.1/tests/test_minimize_optax.py +336 -0
  39. spacecore_optimize-0.0.1/tests/test_minimize_scipy.py +134 -0
  40. spacecore_optimize-0.0.1/tests/test_proximal.py +434 -0
  41. spacecore_optimize-0.0.1/tests/test_readme.py +14 -0
  42. spacecore_optimize-0.0.1/tests/test_realified.py +66 -0
  43. spacecore_optimize-0.0.1/tests/test_spectral.py +173 -0
@@ -0,0 +1,14 @@
1
+ # Changelog
2
+
3
+ ## [0.0.1] — 2026-10-18
4
+
5
+ First release, carved out of SpaceCore 0.5.0, which keeps no optimization loops.
6
+
7
+ - `minimize_scipy`, `line_search_scipy`: SciPy adapters handing over the covector
8
+ `X.riesz(F.grad(x))`.
9
+ - `minimize_optax`, `OptaxResult`: a compiled optax loop on a JAX-backed domain, stopping
10
+ on the dual gradient norm.
11
+ - Functionals: `least_squares`, `NegativeEntropyFunctional`, `KLDivergenceFunctional`,
12
+ `HuberFunctional`, `SpectralFunctional`, `spectralize`, `eigenvalue_space`,
13
+ `NuclearNormFunctional`, `RealifiedFunctional`, `realify`.
14
+ - Proximal steps: `generalized_shrinkage`, `prox_l1`, `prox_l2sq`, `project_nonneg`.
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,8 @@
1
+ include CHANGELOG.md
2
+
3
+ recursive-include docs *.rst *.py
4
+ recursive-include tests *.py
5
+
6
+ global-exclude *.py[cod]
7
+ global-exclude __pycache__
8
+ global-exclude .DS_Store
@@ -0,0 +1,101 @@
1
+ Metadata-Version: 2.4
2
+ Name: spacecore-optimize
3
+ Version: 0.0.1
4
+ Summary: Optimization on top of SpaceCore: SciPy and optax adapters, and a catalogue of functionals and proximal steps.
5
+ Author: Pavlo Pelikh
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Documentation, https://pavlo3p.github.io/spacecore-optimize/
8
+ Project-URL: Source, https://github.com/Pavlo3P/spacecore-optimize
9
+ Project-URL: SpaceCore, https://github.com/Pavlo3P/SpaceCore
10
+ Project-URL: Changelog, https://github.com/Pavlo3P/spacecore-optimize/blob/main/CHANGELOG.md
11
+ Keywords: optimization,proximal,scipy,optax,spacecore
12
+ Classifier: Development Status :: 3 - Alpha
13
+ Classifier: Intended Audience :: Science/Research
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Topic :: Scientific/Engineering :: Mathematics
18
+ Requires-Python: >=3.11
19
+ Description-Content-Type: text/markdown
20
+ License-File: LICENSE
21
+ Requires-Dist: spacecore>=0.5.0
22
+ Requires-Dist: numpy>=2.0.0
23
+ Requires-Dist: scipy>=1.17
24
+ Provides-Extra: optax
25
+ Requires-Dist: jax>=0.9.1; extra == "optax"
26
+ Requires-Dist: optax>=0.2; extra == "optax"
27
+ Provides-Extra: dev
28
+ Requires-Dist: pytest>=8.0; extra == "dev"
29
+ Provides-Extra: docs
30
+ Requires-Dist: sphinx>=8.0; extra == "docs"
31
+ Requires-Dist: pydata-sphinx-theme>=0.16; extra == "docs"
32
+ Dynamic: license-file
33
+
34
+ # spacecore-optimize
35
+
36
+ Optimization on top of [SpaceCore](https://github.com/Pavlo3P/SpaceCore). SpaceCore supplies
37
+ spaces, geometry, operators, functionals and proximal steps; this package hands them to
38
+ external optimizers and adds a catalogue of common objectives.
39
+
40
+ The one rule it enforces: an optimizer that works on flat coordinates gets the gradient as
41
+ the covector `X.riesz(F.grad(x))`, not the Riesz gradient `F.grad(x)`. On a Euclidean space
42
+ the two coincide; on a weighted or operator geometry only the covector is the derivative
43
+ with respect to the coordinates SciPy and optax update.
44
+
45
+ ## Install
46
+
47
+ ```bash
48
+ pip install spacecore-optimize # SciPy adapters and functionals
49
+ pip install "spacecore-optimize[optax]" # plus minimize_optax (JAX backend)
50
+ ```
51
+
52
+ Requires Python 3.11+ and SpaceCore 0.5.0+.
53
+
54
+ ## Example
55
+
56
+ Least squares on a space with a weighted inner product:
57
+
58
+ ```python
59
+ import numpy as np
60
+ import spacecore as sc
61
+ import spacecore_optimize as spo
62
+
63
+ ctx = sc.Context(sc.NumpyOps(), dtype=np.float64)
64
+ X = sc.DenseCoordinateSpace((3,), ctx, geometry=sc.WeightedInnerProduct(ctx.asarray([1.0, 4.0, 9.0])))
65
+ Y = sc.DenseCoordinateSpace((5,), ctx)
66
+ rng = np.random.default_rng(0)
67
+ A = sc.DenseLinOp(ctx.asarray(rng.standard_normal((5, 3))), X, Y, ctx)
68
+ b = ctx.asarray(rng.standard_normal(5))
69
+
70
+ F = spo.least_squares(A, b) # 1/2 ||A x - b||^2
71
+ result = spo.minimize_scipy(F, X.zeros()) # scipy.optimize.minimize, L-BFGS-B
72
+ x = result.x_element # the minimizer, an element of X
73
+ ```
74
+
75
+ ## What is in it
76
+
77
+ | Name | What it does |
78
+ |---|---|
79
+ | `minimize_scipy`, `line_search_scipy` | Run `scipy.optimize.minimize` / `line_search` on a SpaceCore `Functional`. |
80
+ | `minimize_optax`, `OptaxResult` | A compiled optax loop on a JAX-backed domain, stopping on the dual gradient norm. |
81
+ | `least_squares` | `scale * ‖A x − b‖²` as a quadratic form, optionally weighted. |
82
+ | `NegativeEntropyFunctional`, `KLDivergenceFunctional`, `HuberFunctional` | Coordinate objectives whose gradients follow the domain geometry. |
83
+ | `SpectralFunctional`, `spectralize`, `eigenvalue_space`, `NuclearNormFunctional` | Spectral functions on Jordan-algebra spaces such as Hermitian matrices. |
84
+ | `RealifiedFunctional`, `realify` | A complex-domain functional seen on stacked real coordinates. |
85
+ | `generalized_shrinkage`, `prox_l1`, `prox_l2sq`, `project_nonneg` | Closed-form proximal steps in diagonal geometry. |
86
+
87
+ The norms covered by SpaceCore's own prox rules (`LpNormFunctional`,
88
+ `SquaredL2NormFunctional`, `L1NormFunctional`) live in SpaceCore.
89
+
90
+ ## Documentation
91
+
92
+ Published at https://pavlo3p.github.io/spacecore-optimize/. To build locally:
93
+
94
+ ```bash
95
+ pip install -e ".[docs]"
96
+ sphinx-build -W docs docs/_build/html
97
+ ```
98
+
99
+ ## License
100
+
101
+ Apache-2.0.
@@ -0,0 +1,68 @@
1
+ # spacecore-optimize
2
+
3
+ Optimization on top of [SpaceCore](https://github.com/Pavlo3P/SpaceCore). SpaceCore supplies
4
+ spaces, geometry, operators, functionals and proximal steps; this package hands them to
5
+ external optimizers and adds a catalogue of common objectives.
6
+
7
+ The one rule it enforces: an optimizer that works on flat coordinates gets the gradient as
8
+ the covector `X.riesz(F.grad(x))`, not the Riesz gradient `F.grad(x)`. On a Euclidean space
9
+ the two coincide; on a weighted or operator geometry only the covector is the derivative
10
+ with respect to the coordinates SciPy and optax update.
11
+
12
+ ## Install
13
+
14
+ ```bash
15
+ pip install spacecore-optimize # SciPy adapters and functionals
16
+ pip install "spacecore-optimize[optax]" # plus minimize_optax (JAX backend)
17
+ ```
18
+
19
+ Requires Python 3.11+ and SpaceCore 0.5.0+.
20
+
21
+ ## Example
22
+
23
+ Least squares on a space with a weighted inner product:
24
+
25
+ ```python
26
+ import numpy as np
27
+ import spacecore as sc
28
+ import spacecore_optimize as spo
29
+
30
+ ctx = sc.Context(sc.NumpyOps(), dtype=np.float64)
31
+ X = sc.DenseCoordinateSpace((3,), ctx, geometry=sc.WeightedInnerProduct(ctx.asarray([1.0, 4.0, 9.0])))
32
+ Y = sc.DenseCoordinateSpace((5,), ctx)
33
+ rng = np.random.default_rng(0)
34
+ A = sc.DenseLinOp(ctx.asarray(rng.standard_normal((5, 3))), X, Y, ctx)
35
+ b = ctx.asarray(rng.standard_normal(5))
36
+
37
+ F = spo.least_squares(A, b) # 1/2 ||A x - b||^2
38
+ result = spo.minimize_scipy(F, X.zeros()) # scipy.optimize.minimize, L-BFGS-B
39
+ x = result.x_element # the minimizer, an element of X
40
+ ```
41
+
42
+ ## What is in it
43
+
44
+ | Name | What it does |
45
+ |---|---|
46
+ | `minimize_scipy`, `line_search_scipy` | Run `scipy.optimize.minimize` / `line_search` on a SpaceCore `Functional`. |
47
+ | `minimize_optax`, `OptaxResult` | A compiled optax loop on a JAX-backed domain, stopping on the dual gradient norm. |
48
+ | `least_squares` | `scale * ‖A x − b‖²` as a quadratic form, optionally weighted. |
49
+ | `NegativeEntropyFunctional`, `KLDivergenceFunctional`, `HuberFunctional` | Coordinate objectives whose gradients follow the domain geometry. |
50
+ | `SpectralFunctional`, `spectralize`, `eigenvalue_space`, `NuclearNormFunctional` | Spectral functions on Jordan-algebra spaces such as Hermitian matrices. |
51
+ | `RealifiedFunctional`, `realify` | A complex-domain functional seen on stacked real coordinates. |
52
+ | `generalized_shrinkage`, `prox_l1`, `prox_l2sq`, `project_nonneg` | Closed-form proximal steps in diagonal geometry. |
53
+
54
+ The norms covered by SpaceCore's own prox rules (`LpNormFunctional`,
55
+ `SquaredL2NormFunctional`, `L1NormFunctional`) live in SpaceCore.
56
+
57
+ ## Documentation
58
+
59
+ Published at https://pavlo3p.github.io/spacecore-optimize/. To build locally:
60
+
61
+ ```bash
62
+ pip install -e ".[docs]"
63
+ sphinx-build -W docs docs/_build/html
64
+ ```
65
+
66
+ ## License
67
+
68
+ Apache-2.0.
@@ -0,0 +1,32 @@
1
+ API
2
+ ===
3
+
4
+ Optimizer adapters
5
+ ------------------
6
+
7
+ .. autofunction:: spacecore_optimize.minimize_scipy
8
+ .. autofunction:: spacecore_optimize.line_search_scipy
9
+ .. autofunction:: spacecore_optimize.minimize_optax
10
+ .. autoclass:: spacecore_optimize.OptaxResult
11
+
12
+ Functionals
13
+ -----------
14
+
15
+ .. autofunction:: spacecore_optimize.least_squares
16
+ .. autoclass:: spacecore_optimize.NegativeEntropyFunctional
17
+ .. autoclass:: spacecore_optimize.KLDivergenceFunctional
18
+ .. autoclass:: spacecore_optimize.HuberFunctional
19
+ .. autoclass:: spacecore_optimize.SpectralFunctional
20
+ .. autofunction:: spacecore_optimize.spectralize
21
+ .. autofunction:: spacecore_optimize.eigenvalue_space
22
+ .. autoclass:: spacecore_optimize.NuclearNormFunctional
23
+ .. autoclass:: spacecore_optimize.RealifiedFunctional
24
+ .. autofunction:: spacecore_optimize.realify
25
+
26
+ Proximal steps
27
+ --------------
28
+
29
+ .. autofunction:: spacecore_optimize.generalized_shrinkage
30
+ .. autofunction:: spacecore_optimize.prox_l1
31
+ .. autofunction:: spacecore_optimize.prox_l2sq
32
+ .. autofunction:: spacecore_optimize.project_nonneg
@@ -0,0 +1,4 @@
1
+ Changelog
2
+ =========
3
+
4
+ See `CHANGELOG.md <https://github.com/Pavlo3P/spacecore-optimize/blob/master/CHANGELOG.md>`_.
@@ -0,0 +1,16 @@
1
+ """Sphinx configuration for spacecore-optimize."""
2
+
3
+ import importlib.metadata
4
+
5
+ project = "spacecore-optimize"
6
+ author = "Pavlo Pelikh"
7
+ release = importlib.metadata.version("spacecore-optimize")
8
+
9
+ extensions = ["sphinx.ext.autodoc", "sphinx.ext.napoleon"]
10
+ autodoc_mock_imports = ["jax", "optax"]
11
+ autodoc_member_order = "bysource"
12
+ autodoc_typehints = "description"
13
+ napoleon_google_docstring = False
14
+
15
+ html_theme = "pydata_sphinx_theme"
16
+ exclude_patterns = ["_build"]
@@ -0,0 +1,35 @@
1
+ spacecore-optimize
2
+ ==================
3
+
4
+ Optimization on top of `SpaceCore <https://github.com/Pavlo3P/SpaceCore>`_: adapters that
5
+ hand a SpaceCore :class:`~spacecore.Functional` to SciPy or optax, and a catalogue of
6
+ objectives and closed-form proximal steps.
7
+
8
+ An optimizer that updates flat coordinates receives the covector ``X.riesz(F.grad(x))``;
9
+ on a non-Euclidean geometry the Riesz gradient ``F.grad(x)`` is not the coordinate
10
+ derivative.
11
+
12
+ Install with ``pip install spacecore-optimize`` (add ``[optax]`` for
13
+ :func:`~spacecore_optimize.minimize_optax`). Requires SpaceCore 0.5.0 or later.
14
+
15
+ .. code-block:: python
16
+
17
+ import numpy as np
18
+ import spacecore as sc
19
+ import spacecore_optimize as spo
20
+
21
+ ctx = sc.Context(sc.NumpyOps(), dtype=np.float64)
22
+ X = sc.DenseCoordinateSpace((3,), ctx, geometry=sc.WeightedInnerProduct(ctx.asarray([1.0, 4.0, 9.0])))
23
+ Y = sc.DenseCoordinateSpace((5,), ctx)
24
+ rng = np.random.default_rng(0)
25
+ A = sc.DenseLinOp(ctx.asarray(rng.standard_normal((5, 3))), X, Y, ctx)
26
+ b = ctx.asarray(rng.standard_normal(5))
27
+
28
+ F = spo.least_squares(A, b)
29
+ x = spo.minimize_scipy(F, X.zeros()).x_element
30
+
31
+ .. toctree::
32
+ :maxdepth: 2
33
+
34
+ api
35
+ changelog
@@ -0,0 +1,50 @@
1
+ [build-system]
2
+ requires = ["setuptools>=69", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "spacecore-optimize"
7
+ version = "0.0.1"
8
+ description = "Optimization on top of SpaceCore: SciPy and optax adapters, and a catalogue of functionals and proximal steps."
9
+ readme = "README.md"
10
+ requires-python = ">=3.11"
11
+ license = "Apache-2.0"
12
+ license-files = ["LICENSE"]
13
+ authors = [{ name = "Pavlo Pelikh" }]
14
+ keywords = ["optimization", "proximal", "scipy", "optax", "spacecore"]
15
+ classifiers = [
16
+ "Development Status :: 3 - Alpha",
17
+ "Intended Audience :: Science/Research",
18
+ "Programming Language :: Python :: 3",
19
+ "Programming Language :: Python :: 3.11",
20
+ "Programming Language :: Python :: 3.12",
21
+ "Topic :: Scientific/Engineering :: Mathematics",
22
+ ]
23
+ dependencies = [
24
+ "spacecore>=0.5.0",
25
+ "numpy>=2.0.0",
26
+ "scipy>=1.17",
27
+ ]
28
+
29
+ [project.urls]
30
+ Documentation = "https://pavlo3p.github.io/spacecore-optimize/"
31
+ Source = "https://github.com/Pavlo3P/spacecore-optimize"
32
+ SpaceCore = "https://github.com/Pavlo3P/SpaceCore"
33
+ Changelog = "https://github.com/Pavlo3P/spacecore-optimize/blob/main/CHANGELOG.md"
34
+
35
+ [project.optional-dependencies]
36
+ # optax adapter (``minimize_optax``); needs a JAX backend.
37
+ optax = ["jax>=0.9.1", "optax>=0.2"]
38
+ dev = ["pytest>=8.0"]
39
+ docs = ["sphinx>=8.0", "pydata-sphinx-theme>=0.16"]
40
+
41
+ [tool.setuptools.packages.find]
42
+ include = ["spacecore_optimize*"]
43
+
44
+ [tool.setuptools.package-data]
45
+ spacecore_optimize = ["py.typed"]
46
+
47
+ [tool.pytest.ini_options]
48
+ addopts = ["--doctest-modules"]
49
+ testpaths = ["spacecore_optimize", "tests"]
50
+ doctest_optionflags = ["NORMALIZE_WHITESPACE", "ELLIPSIS", "NUMBER"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,53 @@
1
+ """Optimization on top of SpaceCore.
2
+
3
+ External optimizer adapters, plus the functional catalogue in
4
+ :mod:`spacecore_optimize.functionals` (re-exported here). The adapters are thin
5
+ functions that drive a mature external optimizer from a SpaceCore
6
+ :class:`~spacecore.Functional`. Each adapter evaluates the objective through
7
+ ``F.value``, converts the metric (Riesz) gradient ``F.grad`` to a coordinate
8
+ gradient with ``X.riesz`` -- the central, geometry-aware handoff --
9
+ and marshals between SpaceCore elements and the optimizer's representation. The
10
+ external optimizer owns the loop, line search, and convergence.
11
+ """
12
+
13
+ from __future__ import annotations
14
+
15
+ from ._optax import OptaxResult, minimize_optax
16
+ from ._scipy import line_search_scipy, minimize_scipy
17
+ from .functionals import (
18
+ HuberFunctional,
19
+ KLDivergenceFunctional,
20
+ NegativeEntropyFunctional,
21
+ NuclearNormFunctional,
22
+ RealifiedFunctional,
23
+ SpectralFunctional,
24
+ eigenvalue_space,
25
+ generalized_shrinkage,
26
+ least_squares,
27
+ project_nonneg,
28
+ prox_l1,
29
+ prox_l2sq,
30
+ realify,
31
+ spectralize,
32
+ )
33
+
34
+ __all__ = [
35
+ "HuberFunctional",
36
+ "KLDivergenceFunctional",
37
+ "NegativeEntropyFunctional",
38
+ "NuclearNormFunctional",
39
+ "OptaxResult",
40
+ "RealifiedFunctional",
41
+ "SpectralFunctional",
42
+ "eigenvalue_space",
43
+ "generalized_shrinkage",
44
+ "least_squares",
45
+ "line_search_scipy",
46
+ "minimize_optax",
47
+ "minimize_scipy",
48
+ "project_nonneg",
49
+ "prox_l1",
50
+ "prox_l2sq",
51
+ "realify",
52
+ "spectralize",
53
+ ]
@@ -0,0 +1,69 @@
1
+ """Shared marshalling helpers for the external-optimizer adapters.
2
+
3
+ The adapters in this subpackage drive a mature external optimizer (SciPy, optax)
4
+ from a SpaceCore :class:`~spacecore.Functional`. They share one central
5
+ responsibility, stated here: a SpaceCore gradient ``F.grad(x)`` is a
6
+ *metric* (Riesz) gradient, but external NumPy/JAX optimizers consume
7
+ a *coordinate* gradient. The correct, geometry-aware handoff is
8
+ ``X.riesz(F.grad(x))`` -- the identity on a Euclidean space, and mandatory on a
9
+ weighted/non-Euclidean one. :func:`coordinate_gradient` performs this conversion
10
+ once, centrally. Beck, *First-Order Methods in Optimization*, Remark 3.32.
11
+ """
12
+ from __future__ import annotations
13
+
14
+ from typing import Any
15
+
16
+ from spacecore import Functional
17
+ from spacecore import InnerProductSpace
18
+
19
+
20
+ def require_functional(F: Any, fname: str) -> Functional:
21
+ """Return ``F`` as a :class:`~spacecore.Functional` or raise a clear type error."""
22
+ if not isinstance(F, Functional):
23
+ raise TypeError(
24
+ f"{fname} requires a spacecore Functional, got {type(F).__name__}."
25
+ )
26
+ return F
27
+
28
+
29
+ def domain_with_geometry(F: Functional, fname: str) -> InnerProductSpace:
30
+ """Return ``F.domain`` after checking it carries an inner-product geometry.
31
+
32
+ The metric-to-coordinate gradient handoff needs ``X.riesz``; a domain without
33
+ inner-product geometry has no Riesz map and no well-defined metric gradient
34
+ (the ``Functional`` contract), so the adapter refuses it rather than silently assuming Euclidean.
35
+ """
36
+ X = F.domain
37
+ if not isinstance(X, InnerProductSpace):
38
+ raise TypeError(
39
+ f"{fname} requires F.domain to be an InnerProductSpace so the metric "
40
+ f"gradient can be converted to a coordinate gradient with X.riesz; "
41
+ f"got {type(X).__name__}."
42
+ )
43
+ return X
44
+
45
+
46
+ def coordinate_gradient(F: Functional, X: InnerProductSpace, x: Any) -> Any:
47
+ """Return the coordinate gradient ``F.differential(x)`` at ``x``.
48
+
49
+ This is the central conversion: an external coordinate-gradient consumer wants
50
+ the differential covector, which equals ``X.riesz(F.grad(x))``. Coordinate
51
+ functionals return it directly, without a Riesz-map round trip.
52
+ """
53
+ return F.differential(x)
54
+
55
+
56
+ def require_real_field(X: InnerProductSpace, fname: str) -> None:
57
+ """Raise when ``X`` is over the complex field.
58
+
59
+ The SciPy adapters drive real-valued optimizers (``scipy.optimize`` works on
60
+ real coordinate vectors). A complex domain must be split into real and
61
+ imaginary coordinates by the caller; refusing here is more honest than
62
+ silently dropping the imaginary part during ``asarray``.
63
+ """
64
+ if X.field != "real":
65
+ raise ValueError(
66
+ f"{fname} drives a real-valued SciPy optimizer and cannot consume a "
67
+ f"complex domain ({type(X).__name__}, field={X.field!r}); split the "
68
+ f"problem into real and imaginary coordinates first."
69
+ )