ripples-sci 0.1.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.
ripples/__init__.py ADDED
@@ -0,0 +1,267 @@
1
+ """
2
+ Ripples
3
+ =======
4
+
5
+ A scientific computation library that aims to provide a unified, numerically
6
+ accurate, and readable framework for multi-discipline numerical work.
7
+
8
+ The public API is exposed at the top level, so the intended workflow is::
9
+
10
+ >>> import ripples
11
+ >>> ripples.<public_function>(...)
12
+
13
+ enough to reach any feature of the library.
14
+
15
+
16
+ Submodules
17
+ ----------
18
+ Each submodule groups one discipline:
19
+
20
+ ripples.differentiation
21
+ Numerical differentiation for scalar functions of any variable number.
22
+ Computes gradients, Hessians, and full mixed-partials tensors of any order
23
+ through plain central finite differences, Richardson-extrapolated central
24
+ differences, or the complex-step method. Also exposes a Hessian-vector
25
+ product that never forms the full Hessian.
26
+
27
+ Public functions / classes
28
+ - ***nth_numerical_derivative***
29
+ - ***numerical_hessian_vector_product***
30
+ - ***DifferentiationResult***
31
+
32
+ ripples.optimization
33
+ Numerical optimization for scalar functions of any variable number. A
34
+ single unified entry point (***minimizer***) dispatches to local
35
+ first-order methods (Nesterov, Adam), nonlinear conjugate gradient,
36
+ quasi-Newton methods (BFGS, L-BFGS), Newton-CG, trust-region methods
37
+ (trust-ncg, trust-lanczos), and global optimizers (DIRECT, dual annealing).
38
+ Supports bounds and constraints through an Augmented Lagrangian wrapper.
39
+
40
+ Public functions / classes
41
+ - ***minimizer***
42
+ - ***OptimizationResult***
43
+
44
+ The recommended reading order, file by file, is documented in the `__init__.py`
45
+ file of each submodule.
46
+
47
+
48
+ Examples
49
+ --------
50
+ The following snippets shown for each function are minimal, for the full
51
+ parameter list and complete usage guide read their own docstring.
52
+
53
+
54
+ ### Differentiation
55
+
56
+
57
+ ***nth_numerical_derivative***
58
+ Builds a callable that evaluates the `derivative_order`-th partial
59
+ derivative tensor of a user function at one or many points.
60
+
61
+ >>> import numpy as np
62
+ >>> import ripples
63
+ >>>
64
+ >>> # Example 1 - first derivative of a 1-D function at a single point.
65
+ >>> derivative_of_sin = ripples.nth_numerical_derivative(
66
+ ... np.sin, derivative_order=1,
67
+ ... )
68
+ >>> derivative_of_sin(0.0) # ~ cos(0) = 1.0
69
+ >>>
70
+ >>> # Example 2 - full Hessian of a 2-D function at one point.
71
+ >>> def rosenbrock(x):
72
+ ... return (1.0 - x[0]) ** 2 + 100.0 * (x[1] - x[0] ** 2) ** 2
73
+ >>>
74
+ >>> hessian_of_rosenbrock = ripples.nth_numerical_derivative(
75
+ ... rosenbrock, derivative_order=2,
76
+ ... )
77
+ >>> hessian_of_rosenbrock(np.array([1.0, 1.0])) # 2x2 ndarray
78
+
79
+
80
+ ***numerical_hessian_vector_product***
81
+ Computes `H(point) @ vector` directly, without ever forming
82
+ `H`, from the gradient of the user function.
83
+
84
+ >>> import numpy as np
85
+ >>> import ripples
86
+ >>>
87
+ >>> # Example 1 - HVP of the quadratic 0.5 * x^T A x at any point.
88
+ >>> A = np.array([[4.0, 1.0], [1.0, 3.0]])
89
+ >>> def gradient_of_quadratic(x):
90
+ ... return A @ x
91
+ >>>
92
+ >>> ripples.numerical_hessian_vector_product(
93
+ ... gradient_of_quadratic,
94
+ ... point=np.array([1.0, -2.0]),
95
+ ... vector=np.array([1.0, 1.0]),
96
+ ... ) # ~ A @ [1, 1] = [5, 4]
97
+ >>>
98
+ >>> # Example 2 - HVP of a nonlinear function at a generic point.
99
+ >>> def gradient_of_nonlinear(x):
100
+ ... return np.array([
101
+ ... np.cos(x[0]) * np.exp(x[1]) + 2.0 * x[0] * x[1],
102
+ ... np.sin(x[0]) * np.exp(x[1]) + x[0] ** 2,
103
+ ... ])
104
+ >>>
105
+ >>> ripples.numerical_hessian_vector_product(
106
+ ... gradient_of_nonlinear,
107
+ ... point=np.array([0.7, 0.5]),
108
+ ... vector=np.array([0.3, -0.6]),
109
+ ... ) # 1-D ndarray of length 2
110
+
111
+
112
+ ### Optimization
113
+
114
+
115
+ ***minimizer***
116
+ Minimizes a scalar function of any variable number under a unified
117
+ interface. The same call shape works for every supported method;
118
+ `method='trust_ncg'` is the robust default for smooth problems.
119
+
120
+ >>> import numpy as np
121
+ >>> import ripples
122
+ >>>
123
+ >>> # Example 1 - minimize a 2-D quadratic with the default method.
124
+ >>> def quadratic(x):
125
+ ... return (x[0] - 3.0) ** 2 + (x[1] + 1.0) ** 2
126
+ >>>
127
+ >>> result = ripples.minimizer(
128
+ ... function=quadratic,
129
+ ... initial_params=[0.0, 0.0],
130
+ ... )
131
+ >>> result.final_params # ~ [3., -1.]
132
+ >>> result.final_cost # ~ 0.0
133
+ >>>
134
+ >>> # Example 2 - global optimization on a multi-modal function.
135
+ >>> def rastrigin(x):
136
+ ... return 10.0 * len(x) + np.sum(
137
+ ... x ** 2 - 10.0 * np.cos(2.0 * np.pi * x)
138
+ ... )
139
+ >>>
140
+ >>> result = ripples.minimizer(
141
+ ... function=rastrigin,
142
+ ... method='annealing',
143
+ ... bounds=([-5.12, -5.12], [5.12, 5.12]),
144
+ ... )
145
+ >>> result.final_params # ~ [0., 0.]
146
+
147
+
148
+ ***OptimizationResult***
149
+ Output class returned by ***minimizer***. Behaves like a read-only
150
+ `numpy.ndarray` of the final parameters and additionally carries the full
151
+ record of the run (cost, evaluation counts, elapsed time, termination
152
+ reason) and every configuration setting.
153
+
154
+ >>> import ripples
155
+ >>>
156
+ >>> # Example 1 - inspect the most common attributes.
157
+ >>> result = ripples.minimizer(
158
+ ... function=lambda x: x[0] ** 2 + x[1] ** 2,
159
+ ... initial_params=[1.0, 1.0],
160
+ ... )
161
+ >>> result.success
162
+ >>> result.final_params # read-only ndarray
163
+ >>> result.final_cost
164
+ >>> result.iteration_number
165
+ >>>
166
+ >>> # Example 2 - summary and conversion helpers.
167
+ >>> print(result) # compact summary
168
+ >>> print(result.summary('full')) # full configuration
169
+ >>> result.to_dict() # JSON-friendly dict
170
+
171
+
172
+ Author
173
+ ------
174
+ Álvaro Cátedra Sánchez <alvaro.catedra.sanchez@gmail.com>, unique
175
+ author, maintainer, and responsible for overall design, numerical
176
+ methods implementation, and API consistency.
177
+ """
178
+
179
+ # Copyright (c) Álvaro Cátedra Sánchez <alvaro.catedra.sanchez@gmail.com>,
180
+ # unique author, maintainer, and responsible for overall design, numerical
181
+ # methods implementation, and API consistency.
182
+ #
183
+ # Licensed under the Apache License, Version 2.0 (the "License");
184
+ # you may not use this file except in compliance with the License.
185
+ # You may obtain a copy of the License at:
186
+ #
187
+ # http://www.apache.org/licenses/LICENSE-2.0
188
+ #
189
+ # Unless required by applicable law or agreed to in writing, software
190
+ # distributed under the License is distributed on an "AS IS" BASIS,
191
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
192
+ # See the License for the specific language governing permissions and
193
+ # limitations under the License.
194
+ #
195
+ # Proper attribution to the original author must be preserved in
196
+ # all copies or substantial portions of this software.
197
+
198
+ __version__ = '0.1.0'
199
+ __author__ = 'Álvaro Cátedra Sánchez'
200
+ __email__ = 'alvaro.catedra.sanchez@gmail.com'
201
+ __license__ = 'Apache-2.0'
202
+ __copyright__ = 'Copyright (c) Álvaro Cátedra Sánchez'
203
+
204
+
205
+
206
+ # Differentiation
207
+ from .differentiation import (
208
+ nth_numerical_derivative,
209
+ numerical_hessian_vector_product,
210
+ DifferentiationResult
211
+ )
212
+
213
+ # Optimization
214
+ from .optimization import (
215
+ minimizer,
216
+ OptimizationResult
217
+ )
218
+
219
+ # Test
220
+ from ._test import test
221
+
222
+
223
+
224
+
225
+ _submodules = (
226
+ 'differentiation',
227
+ 'optimization',
228
+ )
229
+
230
+ _public_functions = (
231
+ 'nth_numerical_derivative',
232
+ 'numerical_hessian_vector_product',
233
+ 'DifferentiationResult',
234
+ 'minimizer',
235
+ 'OptimizationResult',
236
+ )
237
+
238
+ _dispatcher = (
239
+ 'test',
240
+ )
241
+
242
+ _metadata = (
243
+ '__version__',
244
+ '__author__',
245
+ '__email__',
246
+ '__license__',
247
+ '__copyright__',
248
+ )
249
+
250
+ __all__ = list(_submodules + _public_functions + _dispatcher + _metadata)
251
+
252
+
253
+
254
+ def __dir__() -> list[str]:
255
+ """
256
+ Custom directory listing for `ripples`.
257
+
258
+ Restricts tab-completion and `dir(ripples)` to the public surface
259
+ declared in `__all__`.
260
+
261
+ Returns
262
+ -------
263
+ list of str
264
+ Sorted copy of `__all__`.
265
+ """
266
+
267
+ return sorted(__all__)