pynumerics 2.3.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.
- pynumerics/__init__.py +73 -0
- pynumerics/_bin/Microsoft.CodeAnalysis.CSharp.dll +0 -0
- pynumerics/_bin/Microsoft.CodeAnalysis.LICENSE.txt +27 -0
- pynumerics/_bin/Microsoft.CodeAnalysis.dll +0 -0
- pynumerics/_bin/Numerics.dll +0 -0
- pynumerics/_bin/pynumerics.runtimeconfig.json +15 -0
- pynumerics/_coverage.py +3825 -0
- pynumerics/_util.py +237 -0
- pynumerics/_version.py +40 -0
- pynumerics/bootstrap.py +593 -0
- pynumerics/callbacks.py +791 -0
- pynumerics/copulas.py +910 -0
- pynumerics/data.py +2233 -0
- pynumerics/differentiation.py +348 -0
- pynumerics/distributions.py +4043 -0
- pynumerics/download.py +623 -0
- pynumerics/enums.py +119 -0
- pynumerics/errors.py +245 -0
- pynumerics/functions.py +1142 -0
- pynumerics/goodness_of_fit.py +699 -0
- pynumerics/graphs.py +556 -0
- pynumerics/hypothesis_tests.py +445 -0
- pynumerics/identity.py +144 -0
- pynumerics/integration.py +677 -0
- pynumerics/linalg.py +1408 -0
- pynumerics/machine_learning.py +1335 -0
- pynumerics/marshal.py +733 -0
- pynumerics/mathematics.py +1053 -0
- pynumerics/mcmc.py +1406 -0
- pynumerics/multivariate.py +1539 -0
- pynumerics/ode.py +156 -0
- pynumerics/optimization.py +1736 -0
- pynumerics/probability.py +966 -0
- pynumerics/py.typed +0 -0
- pynumerics/regional.py +522 -0
- pynumerics/roots.py +274 -0
- pynumerics/runtime.py +384 -0
- pynumerics/sampling.py +1758 -0
- pynumerics/special.py +1079 -0
- pynumerics/statistics.py +1958 -0
- pynumerics/timeseries.py +1326 -0
- pynumerics-2.3.0.dist-info/METADATA +142 -0
- pynumerics-2.3.0.dist-info/RECORD +46 -0
- pynumerics-2.3.0.dist-info/WHEEL +5 -0
- pynumerics-2.3.0.dist-info/licenses/LICENSE +10 -0
- pynumerics-2.3.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,677 @@
|
|
|
1
|
+
"""Numerical integration from ``Numerics.Mathematics.Integration``.
|
|
2
|
+
|
|
3
|
+
The fixed rules of the static ``Integration`` class (:func:`gauss_legendre`, :func:`trapezoidal_rule`,
|
|
4
|
+
...) return a float. Every ``Integrator`` subclass has an ``integrate_*`` function that builds the
|
|
5
|
+
Numerics integrator, applies the settings given, calls ``Integrate`` and returns an
|
|
6
|
+
:class:`IntegrationResult` (a :class:`VegasResult` for Vegas). Settings left as None keep the
|
|
7
|
+
Numerics defaults.
|
|
8
|
+
|
|
9
|
+
Integrands are Python callables, C# source compiled by :func:`pynumerics.callbacks.compile_csharp`
|
|
10
|
+
or .NET delegates (see :mod:`pynumerics.callbacks`). A compiled integrand runs without crossing into
|
|
11
|
+
Python per evaluation.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
from collections.abc import Callable, Mapping, Sequence
|
|
17
|
+
from dataclasses import dataclass
|
|
18
|
+
from types import MappingProxyType
|
|
19
|
+
from typing import Any
|
|
20
|
+
|
|
21
|
+
import numpy as np
|
|
22
|
+
|
|
23
|
+
from pynumerics import callbacks
|
|
24
|
+
from pynumerics._util import namespace, net_enum
|
|
25
|
+
from pynumerics.enums import NetEnum
|
|
26
|
+
from pynumerics.errors import translate_public
|
|
27
|
+
from pynumerics.marshal import to_net_doubles, to_numpy_matrix
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class IntegrationStatus(NetEnum):
|
|
31
|
+
"""Integration outcomes (``Numerics.Mathematics.IntegrationStatus``)."""
|
|
32
|
+
|
|
33
|
+
NONE = "None"
|
|
34
|
+
SUCCESS = "Success"
|
|
35
|
+
MAXIMUM_ITERATIONS_REACHED = "MaximumIterationsReached"
|
|
36
|
+
MAXIMUM_FUNCTION_EVALUATIONS_REACHED = "MaximumFunctionEvaluationsReached"
|
|
37
|
+
FAILURE = "Failure"
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class AdaptiveGaussKronrodMethod(NetEnum):
|
|
41
|
+
"""Adaptive strategies of ``AdaptiveGaussKronrod``.
|
|
42
|
+
|
|
43
|
+
Wraps ``Numerics.Mathematics.Integration.AdaptiveGaussKronrodMethod``.
|
|
44
|
+
|
|
45
|
+
``LOCAL_BISECTION`` is the default recursive local bisection. ``QAGS`` is the QUADPACK QAGS algorithm
|
|
46
|
+
behind ``scipy.integrate.quad``: global bisection of the largest error with epsilon-algorithm
|
|
47
|
+
extrapolation, suited to endpoint singularities.
|
|
48
|
+
"""
|
|
49
|
+
|
|
50
|
+
LOCAL_BISECTION = "LocalBisection"
|
|
51
|
+
QAGS = "Qags"
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
INTEGRATOR_SETTINGS: Mapping[str, str] = MappingProxyType({
|
|
55
|
+
"min_iterations": "MinIterations",
|
|
56
|
+
"max_iterations": "MaxIterations",
|
|
57
|
+
"min_function_evaluations": "MinFunctionEvaluations",
|
|
58
|
+
"max_function_evaluations": "MaxFunctionEvaluations",
|
|
59
|
+
"absolute_tolerance": "AbsoluteTolerance",
|
|
60
|
+
"relative_tolerance": "RelativeTolerance",
|
|
61
|
+
"report_failure": "ReportFailure",
|
|
62
|
+
})
|
|
63
|
+
"""The ``**settings`` keywords every ``integrate_*`` function takes, and the ``Integrator`` property each sets."""
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
@dataclass(frozen=True, slots=True)
|
|
67
|
+
class IntegrationResult:
|
|
68
|
+
"""The outcome of a Numerics integrator.
|
|
69
|
+
|
|
70
|
+
Parameters
|
|
71
|
+
----------
|
|
72
|
+
value : float
|
|
73
|
+
The integral estimate (``Result``).
|
|
74
|
+
standard_error : float
|
|
75
|
+
The error estimate (``StandardError``); NaN for integrators without one.
|
|
76
|
+
iterations : int
|
|
77
|
+
Iterations used (``Iterations``).
|
|
78
|
+
function_evaluations : int
|
|
79
|
+
Integrand evaluations used (``FunctionEvaluations``).
|
|
80
|
+
status : IntegrationStatus
|
|
81
|
+
How the integration ended (``Status``).
|
|
82
|
+
"""
|
|
83
|
+
|
|
84
|
+
value: float
|
|
85
|
+
standard_error: float
|
|
86
|
+
iterations: int
|
|
87
|
+
function_evaluations: int
|
|
88
|
+
status: IntegrationStatus
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
@dataclass(frozen=True, slots=True)
|
|
92
|
+
class VegasResult(IntegrationResult):
|
|
93
|
+
"""The outcome of a Vegas integration.
|
|
94
|
+
|
|
95
|
+
Parameters
|
|
96
|
+
----------
|
|
97
|
+
value : float
|
|
98
|
+
The integral estimate (``Result``).
|
|
99
|
+
standard_error : float
|
|
100
|
+
The error estimate (``StandardError``).
|
|
101
|
+
iterations : int
|
|
102
|
+
Iterations used (``Iterations``).
|
|
103
|
+
function_evaluations : int
|
|
104
|
+
Integrand evaluations used (``FunctionEvaluations``).
|
|
105
|
+
status : IntegrationStatus
|
|
106
|
+
How the integration ended (``Status``).
|
|
107
|
+
chi_squared : float
|
|
108
|
+
The chi-squared per degree of freedom of the iteration estimates (``ChiSquared``).
|
|
109
|
+
grid : numpy.ndarray
|
|
110
|
+
The adapted importance-sampling grid, one row per dimension (``Grid``).
|
|
111
|
+
"""
|
|
112
|
+
|
|
113
|
+
chi_squared: float
|
|
114
|
+
grid: np.ndarray
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def _integration() -> Any:
|
|
118
|
+
"""Return the ``Numerics.Mathematics.Integration`` namespace after starting the runtime."""
|
|
119
|
+
return namespace("Numerics.Mathematics.Integration")
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _set(net: Any, **values: Any) -> None:
|
|
123
|
+
"""Set each .NET property named in ``values`` whose value is not None."""
|
|
124
|
+
for name, value in values.items():
|
|
125
|
+
if value is not None:
|
|
126
|
+
setattr(net, name, value)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def _optional(kind: Callable[[Any], Any], value: Any) -> Any:
|
|
130
|
+
"""Return ``kind(value)``, or None when ``value`` is None."""
|
|
131
|
+
return None if value is None else kind(value)
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def _run(net: Any, settings: Mapping[str, Any], *functions: Any, bins: Any = None,
|
|
135
|
+
standard_error: bool = True) -> IntegrationResult:
|
|
136
|
+
"""Apply the ``Integrator`` settings and the parallel policy, integrate and read the result.
|
|
137
|
+
|
|
138
|
+
``bins`` (a ``StratificationBins`` record or a .NET ``List<StratificationBin>``) selects the
|
|
139
|
+
``Integrate(List<StratificationBin>)`` overload.
|
|
140
|
+
"""
|
|
141
|
+
unknown = sorted(set(settings) - set(INTEGRATOR_SETTINGS))
|
|
142
|
+
if unknown:
|
|
143
|
+
raise TypeError(f"Unknown integrator settings: {', '.join(unknown)}; known: {', '.join(INTEGRATOR_SETTINGS)}.")
|
|
144
|
+
for key, value in settings.items():
|
|
145
|
+
kind = bool if key == "report_failure" else float if key.endswith("tolerance") else int
|
|
146
|
+
_set(net, **{INTEGRATOR_SETTINGS[key]: _optional(kind, value)})
|
|
147
|
+
callbacks.apply_parallel_policy(net, *functions)
|
|
148
|
+
if bins is None:
|
|
149
|
+
net.Integrate()
|
|
150
|
+
else:
|
|
151
|
+
net.Integrate(getattr(bins, "net", bins))
|
|
152
|
+
return IntegrationResult(float(net.Result), float(net.StandardError) if standard_error else float("nan"),
|
|
153
|
+
int(net.Iterations), int(net.FunctionEvaluations), IntegrationStatus(str(net.Status)))
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def _random(seed: int | None) -> Any:
|
|
157
|
+
"""Return a seeded ``MersenneTwister``, or None to keep the integrator's own generator."""
|
|
158
|
+
return None if seed is None else namespace("Numerics.Sampling").MersenneTwister(int(seed))
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def _bounds(lower: Sequence[float], upper: Sequence[float]) -> tuple[int, Any, Any]:
|
|
162
|
+
"""Return the dimension count and the bounds as .NET arrays."""
|
|
163
|
+
low, high = to_net_doubles(lower), to_net_doubles(upper)
|
|
164
|
+
return int(low.Length), low, high
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def gauss_legendre(f: Any, a: float, b: float) -> float:
|
|
168
|
+
"""Integrate ``f`` on [a, b] with the 10-point Gauss-Legendre rule (``Integration.GaussLegendre``).
|
|
169
|
+
|
|
170
|
+
Parameters
|
|
171
|
+
----------
|
|
172
|
+
f : callable, str or System.Delegate
|
|
173
|
+
Integrand of one float (see :mod:`pynumerics.callbacks`).
|
|
174
|
+
a, b : float
|
|
175
|
+
The limits.
|
|
176
|
+
|
|
177
|
+
Returns
|
|
178
|
+
-------
|
|
179
|
+
float
|
|
180
|
+
The estimate, exact for polynomials up to degree 19.
|
|
181
|
+
"""
|
|
182
|
+
return float(_integration().Integration.GaussLegendre(callbacks.scalar_function(f), float(a), float(b)))
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def gauss_legendre_20(f: Any, a: float, b: float) -> float:
|
|
186
|
+
"""Integrate ``f`` on [a, b] with the 20-point Gauss-Legendre rule (``Integration.GaussLegendre20``).
|
|
187
|
+
|
|
188
|
+
Parameters
|
|
189
|
+
----------
|
|
190
|
+
f : callable, str or System.Delegate
|
|
191
|
+
Integrand of one float.
|
|
192
|
+
a, b : float
|
|
193
|
+
The limits.
|
|
194
|
+
|
|
195
|
+
Returns
|
|
196
|
+
-------
|
|
197
|
+
float
|
|
198
|
+
The estimate, exact for polynomials up to degree 39.
|
|
199
|
+
"""
|
|
200
|
+
return float(_integration().Integration.GaussLegendre20(callbacks.scalar_function(f), float(a), float(b)))
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def trapezoidal_rule(f: Any, a: float, b: float, steps: int = 2) -> float:
|
|
204
|
+
"""Integrate ``f`` on [a, b] with the composite trapezoidal rule (``Integration.TrapezoidalRule``).
|
|
205
|
+
|
|
206
|
+
Parameters
|
|
207
|
+
----------
|
|
208
|
+
f : callable, str or System.Delegate
|
|
209
|
+
Integrand of one float.
|
|
210
|
+
a, b : float
|
|
211
|
+
The limits.
|
|
212
|
+
steps : int, optional
|
|
213
|
+
Number of equal panels; default 2.
|
|
214
|
+
|
|
215
|
+
Returns
|
|
216
|
+
-------
|
|
217
|
+
float
|
|
218
|
+
The estimate.
|
|
219
|
+
"""
|
|
220
|
+
return float(_integration().Integration.TrapezoidalRule(callbacks.scalar_function(f), float(a), float(b),
|
|
221
|
+
int(steps)))
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def simpsons_rule(f: Any, a: float, b: float, steps: int = 2) -> float:
|
|
225
|
+
"""Integrate ``f`` on [a, b] with the composite Simpson's rule (``Integration.SimpsonsRule``).
|
|
226
|
+
|
|
227
|
+
Parameters
|
|
228
|
+
----------
|
|
229
|
+
f : callable, str or System.Delegate
|
|
230
|
+
Integrand of one float.
|
|
231
|
+
a, b : float
|
|
232
|
+
The limits.
|
|
233
|
+
steps : int, optional
|
|
234
|
+
Number of equal panels, each evaluated at its ends and midpoint; default 2.
|
|
235
|
+
|
|
236
|
+
Returns
|
|
237
|
+
-------
|
|
238
|
+
float
|
|
239
|
+
The estimate.
|
|
240
|
+
"""
|
|
241
|
+
return float(_integration().Integration.SimpsonsRule(callbacks.scalar_function(f), float(a), float(b),
|
|
242
|
+
int(steps)))
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def midpoint_rule(f: Any, a: float, b: float, steps: int = 2) -> float:
|
|
246
|
+
"""Integrate ``f`` on [a, b] with the composite midpoint rule (``Integration.Midpoint``).
|
|
247
|
+
|
|
248
|
+
Parameters
|
|
249
|
+
----------
|
|
250
|
+
f : callable, str or System.Delegate
|
|
251
|
+
Integrand of one float.
|
|
252
|
+
a, b : float
|
|
253
|
+
The limits.
|
|
254
|
+
steps : int, optional
|
|
255
|
+
Number of equal panels; default 2.
|
|
256
|
+
|
|
257
|
+
Returns
|
|
258
|
+
-------
|
|
259
|
+
float
|
|
260
|
+
The estimate.
|
|
261
|
+
"""
|
|
262
|
+
return float(_integration().Integration.Midpoint(callbacks.scalar_function(f), float(a), float(b), int(steps)))
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def integrate_trapezoidal(f: Any, a: float, b: float, **settings: Any) -> IntegrationResult:
|
|
266
|
+
"""Integrate ``f`` on [a, b] by trapezoidal refinement to tolerance (``TrapezoidalRule``).
|
|
267
|
+
|
|
268
|
+
Each iteration doubles the panels until successive estimates agree within the tolerances.
|
|
269
|
+
|
|
270
|
+
Parameters
|
|
271
|
+
----------
|
|
272
|
+
f : callable, str or System.Delegate
|
|
273
|
+
Integrand of one float.
|
|
274
|
+
a, b : float
|
|
275
|
+
Finite limits with ``a < b``.
|
|
276
|
+
**settings
|
|
277
|
+
``Integrator`` settings, the keys of :data:`INTEGRATOR_SETTINGS`.
|
|
278
|
+
|
|
279
|
+
Returns
|
|
280
|
+
-------
|
|
281
|
+
IntegrationResult
|
|
282
|
+
The estimate, counts and status; ``standard_error`` is NaN.
|
|
283
|
+
|
|
284
|
+
Raises
|
|
285
|
+
------
|
|
286
|
+
ValueError
|
|
287
|
+
If a limit or setting is out of range (raised by Numerics).
|
|
288
|
+
TypeError
|
|
289
|
+
If a setting name is unknown.
|
|
290
|
+
"""
|
|
291
|
+
fn = callbacks.scalar_function(f)
|
|
292
|
+
return _run(_integration().TrapezoidalRule(fn, float(a), float(b)), settings, fn, standard_error=False)
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
def integrate_simpson(f: Any, a: float, b: float, **settings: Any) -> IntegrationResult:
|
|
296
|
+
"""Integrate ``f`` on [a, b] by Simpson refinement to tolerance (``SimpsonsRule``).
|
|
297
|
+
|
|
298
|
+
Parameters
|
|
299
|
+
----------
|
|
300
|
+
f : callable, str or System.Delegate
|
|
301
|
+
Integrand of one float.
|
|
302
|
+
a, b : float
|
|
303
|
+
Finite limits with ``a < b``.
|
|
304
|
+
**settings
|
|
305
|
+
``Integrator`` settings, the keys of :data:`INTEGRATOR_SETTINGS`.
|
|
306
|
+
|
|
307
|
+
Returns
|
|
308
|
+
-------
|
|
309
|
+
IntegrationResult
|
|
310
|
+
The estimate, counts and status; ``standard_error`` is NaN.
|
|
311
|
+
|
|
312
|
+
Raises
|
|
313
|
+
------
|
|
314
|
+
ValueError
|
|
315
|
+
If a limit or setting is out of range (raised by Numerics).
|
|
316
|
+
TypeError
|
|
317
|
+
If a setting name is unknown.
|
|
318
|
+
"""
|
|
319
|
+
fn = callbacks.scalar_function(f)
|
|
320
|
+
return _run(_integration().SimpsonsRule(fn, float(a), float(b)), settings, fn, standard_error=False)
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
def integrate_adaptive_simpson(f: Any, a: float, b: float, *, min_depth: int | None = None,
|
|
324
|
+
max_depth: int | None = None, bins: Any = None,
|
|
325
|
+
**settings: Any) -> IntegrationResult:
|
|
326
|
+
"""Integrate ``f`` on [a, b] with the adaptive Simpson's rule (``AdaptiveSimpsonsRule``).
|
|
327
|
+
|
|
328
|
+
Parameters
|
|
329
|
+
----------
|
|
330
|
+
f : callable, str or System.Delegate
|
|
331
|
+
Integrand of one float.
|
|
332
|
+
a, b : float
|
|
333
|
+
Finite limits with ``a < b``.
|
|
334
|
+
min_depth, max_depth : int, optional
|
|
335
|
+
Recursion depth limits (``MinDepth``, ``MaxDepth``).
|
|
336
|
+
bins : pynumerics.sampling.StratificationBins, optional
|
|
337
|
+
Integrate over these bins instead of [a, b] (``Integrate(List<StratificationBin>)``).
|
|
338
|
+
**settings
|
|
339
|
+
``Integrator`` settings, the keys of :data:`INTEGRATOR_SETTINGS`.
|
|
340
|
+
|
|
341
|
+
Returns
|
|
342
|
+
-------
|
|
343
|
+
IntegrationResult
|
|
344
|
+
The estimate, standard error, counts and status.
|
|
345
|
+
|
|
346
|
+
Raises
|
|
347
|
+
------
|
|
348
|
+
ValueError
|
|
349
|
+
If a limit or setting is out of range (raised by Numerics).
|
|
350
|
+
TypeError
|
|
351
|
+
If a setting name is unknown.
|
|
352
|
+
"""
|
|
353
|
+
fn = callbacks.scalar_function(f)
|
|
354
|
+
net = _integration().AdaptiveSimpsonsRule(fn, float(a), float(b))
|
|
355
|
+
_set(net, MinDepth=_optional(int, min_depth), MaxDepth=_optional(int, max_depth))
|
|
356
|
+
return _run(net, settings, fn, bins=bins)
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
def integrate_adaptive_gauss_kronrod(f: Any, a: float, b: float, *, min_depth: int | None = None,
|
|
360
|
+
max_depth: int | None = None, recorder: Any = None,
|
|
361
|
+
bins: Any = None, method: AdaptiveGaussKronrodMethod | str | None = None,
|
|
362
|
+
**settings: Any) -> IntegrationResult:
|
|
363
|
+
"""Integrate ``f`` on [a, b] with the adaptive Gauss-Kronrod G10K21 rule (``AdaptiveGaussKronrod``).
|
|
364
|
+
|
|
365
|
+
Parameters
|
|
366
|
+
----------
|
|
367
|
+
f : callable, str or System.Delegate
|
|
368
|
+
Integrand of one float.
|
|
369
|
+
a, b : float
|
|
370
|
+
Finite limits with ``a < b``.
|
|
371
|
+
min_depth, max_depth : int, optional
|
|
372
|
+
Recursion depth limits (``MinDepth``, ``MaxDepth``).
|
|
373
|
+
recorder : callable, str or System.Delegate, optional
|
|
374
|
+
Called as ``recorder(x, weight, fx)`` for each node of the accepted intervals (``Recorder``);
|
|
375
|
+
the weights sum to the domain width and ``sum(weight * fx)`` reproduces the result.
|
|
376
|
+
bins : pynumerics.sampling.StratificationBins, optional
|
|
377
|
+
Integrate over these bins instead of [a, b] (``Integrate(List<StratificationBin>)``).
|
|
378
|
+
method : AdaptiveGaussKronrodMethod or str, optional
|
|
379
|
+
The adaptive strategy (``Method``); left at the Numerics default, local bisection, when None.
|
|
380
|
+
``"qags"`` selects QUADPACK QAGS, which suits endpoint singularities; there ``recorder``
|
|
381
|
+
receives the final partition and ``relative_tolerance=0`` is legal with a positive
|
|
382
|
+
``absolute_tolerance``.
|
|
383
|
+
**settings
|
|
384
|
+
``Integrator`` settings, the keys of :data:`INTEGRATOR_SETTINGS`.
|
|
385
|
+
|
|
386
|
+
Returns
|
|
387
|
+
-------
|
|
388
|
+
IntegrationResult
|
|
389
|
+
The estimate, standard error, counts and status.
|
|
390
|
+
|
|
391
|
+
Raises
|
|
392
|
+
------
|
|
393
|
+
ValueError
|
|
394
|
+
If a limit or setting is out of range (raised by Numerics), or ``method`` is not a method name.
|
|
395
|
+
ArithmeticError
|
|
396
|
+
If an accepted interval is not finite and ``report_failure`` is on (raised by Numerics).
|
|
397
|
+
TypeError
|
|
398
|
+
If a setting name is unknown.
|
|
399
|
+
|
|
400
|
+
Examples
|
|
401
|
+
--------
|
|
402
|
+
>>> from pynumerics.integration import integrate_adaptive_gauss_kronrod
|
|
403
|
+
>>> integrate_adaptive_gauss_kronrod("x => Math.Exp(-x * x)", -3.0, 3.0).value # doctest: +SKIP
|
|
404
|
+
1.772414696519...
|
|
405
|
+
"""
|
|
406
|
+
fn = callbacks.scalar_function(f)
|
|
407
|
+
record = None if recorder is None else callbacks.to_delegate(recorder, "Action<double,double,double>")
|
|
408
|
+
integration = _integration()
|
|
409
|
+
net = integration.AdaptiveGaussKronrod(fn, float(a), float(b))
|
|
410
|
+
if method is not None:
|
|
411
|
+
net.Method = net_enum(integration.AdaptiveGaussKronrodMethod, AdaptiveGaussKronrodMethod(method))
|
|
412
|
+
_set(net, MinDepth=_optional(int, min_depth), MaxDepth=_optional(int, max_depth), Recorder=record)
|
|
413
|
+
return _run(net, settings, fn, record, bins=bins)
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
def integrate_adaptive_gauss_lobatto(f: Any, a: float, b: float, **settings: Any) -> IntegrationResult:
|
|
417
|
+
"""Integrate ``f`` on [a, b] with the adaptive Gauss-Lobatto rule (``AdaptiveGaussLobatto``).
|
|
418
|
+
|
|
419
|
+
Parameters
|
|
420
|
+
----------
|
|
421
|
+
f : callable, str or System.Delegate
|
|
422
|
+
Integrand of one float.
|
|
423
|
+
a, b : float
|
|
424
|
+
Finite limits with ``a < b``.
|
|
425
|
+
**settings
|
|
426
|
+
``Integrator`` settings, the keys of :data:`INTEGRATOR_SETTINGS`.
|
|
427
|
+
|
|
428
|
+
Returns
|
|
429
|
+
-------
|
|
430
|
+
IntegrationResult
|
|
431
|
+
The estimate, counts and status; ``standard_error`` is NaN.
|
|
432
|
+
|
|
433
|
+
Raises
|
|
434
|
+
------
|
|
435
|
+
ValueError
|
|
436
|
+
If a limit or setting is out of range (raised by Numerics).
|
|
437
|
+
TypeError
|
|
438
|
+
If a setting name is unknown.
|
|
439
|
+
"""
|
|
440
|
+
fn = callbacks.scalar_function(f)
|
|
441
|
+
return _run(_integration().AdaptiveGaussLobatto(fn, float(a), float(b)), settings, fn, standard_error=False)
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
def integrate_adaptive_simpson_2d(f: Any, x_min: float, x_max: float, y_min: float, y_max: float, *,
|
|
445
|
+
min_depth: int | None = None, max_depth: int | None = None,
|
|
446
|
+
**settings: Any) -> IntegrationResult:
|
|
447
|
+
"""Integrate ``f(x, y)`` over a rectangle with the adaptive 2-D Simpson's rule (``AdaptiveSimpsonsRule2D``).
|
|
448
|
+
|
|
449
|
+
Parameters
|
|
450
|
+
----------
|
|
451
|
+
f : callable, str or System.Delegate
|
|
452
|
+
Integrand of two floats.
|
|
453
|
+
x_min, x_max, y_min, y_max : float
|
|
454
|
+
The rectangle.
|
|
455
|
+
min_depth, max_depth : int, optional
|
|
456
|
+
Recursion depth limits (``MinDepth``, ``MaxDepth``).
|
|
457
|
+
**settings
|
|
458
|
+
``Integrator`` settings, the keys of :data:`INTEGRATOR_SETTINGS`.
|
|
459
|
+
|
|
460
|
+
Returns
|
|
461
|
+
-------
|
|
462
|
+
IntegrationResult
|
|
463
|
+
The estimate, standard error, counts and status.
|
|
464
|
+
|
|
465
|
+
Raises
|
|
466
|
+
------
|
|
467
|
+
ValueError
|
|
468
|
+
If a limit or setting is out of range (raised by Numerics).
|
|
469
|
+
TypeError
|
|
470
|
+
If a setting name is unknown.
|
|
471
|
+
"""
|
|
472
|
+
fn = callbacks.bivariate_function(f)
|
|
473
|
+
net = _integration().AdaptiveSimpsonsRule2D(fn, float(x_min), float(x_max), float(y_min), float(y_max))
|
|
474
|
+
_set(net, MinDepth=_optional(int, min_depth), MaxDepth=_optional(int, max_depth))
|
|
475
|
+
return _run(net, settings, fn)
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
def integrate_adaptive_gauss_kronrod_2d(f: Any, x_min: float, x_max: float, y_min: float, y_max: float, *,
|
|
479
|
+
min_depth: int | None = None, max_depth: int | None = None,
|
|
480
|
+
recorder: Any = None, **settings: Any) -> IntegrationResult:
|
|
481
|
+
"""Integrate ``f(x, y)`` over a rectangle with the adaptive 2-D Gauss-Kronrod rule (``AdaptiveGaussKronrod2D``).
|
|
482
|
+
|
|
483
|
+
Parameters
|
|
484
|
+
----------
|
|
485
|
+
f : callable, str or System.Delegate
|
|
486
|
+
Integrand of two floats.
|
|
487
|
+
x_min, x_max, y_min, y_max : float
|
|
488
|
+
The rectangle.
|
|
489
|
+
min_depth, max_depth : int, optional
|
|
490
|
+
Recursion depth limits (``MinDepth``, ``MaxDepth``).
|
|
491
|
+
recorder : callable, str or System.Delegate, optional
|
|
492
|
+
Called as ``recorder(x, y, weight, fxy)`` for each node of the accepted cells (``Recorder``).
|
|
493
|
+
**settings
|
|
494
|
+
``Integrator`` settings, the keys of :data:`INTEGRATOR_SETTINGS`.
|
|
495
|
+
|
|
496
|
+
Returns
|
|
497
|
+
-------
|
|
498
|
+
IntegrationResult
|
|
499
|
+
The estimate, standard error, counts and status.
|
|
500
|
+
|
|
501
|
+
Raises
|
|
502
|
+
------
|
|
503
|
+
ValueError
|
|
504
|
+
If a limit or setting is out of range (raised by Numerics).
|
|
505
|
+
TypeError
|
|
506
|
+
If a setting name is unknown.
|
|
507
|
+
"""
|
|
508
|
+
fn = callbacks.bivariate_function(f)
|
|
509
|
+
record = None if recorder is None else callbacks.to_delegate(recorder, "Action<double,double,double,double>")
|
|
510
|
+
net = _integration().AdaptiveGaussKronrod2D(fn, float(x_min), float(x_max), float(y_min), float(y_max))
|
|
511
|
+
_set(net, MinDepth=_optional(int, min_depth), MaxDepth=_optional(int, max_depth), Recorder=record)
|
|
512
|
+
return _run(net, settings, fn, record)
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
def integrate_monte_carlo(f: Any, lower: Sequence[float], upper: Sequence[float], *, seed: int | None = None,
|
|
516
|
+
use_sobol_sequence: bool | None = None, **settings: Any) -> IntegrationResult:
|
|
517
|
+
"""Integrate ``f(x)`` over a box by plain Monte Carlo sampling (``MonteCarloIntegration``).
|
|
518
|
+
|
|
519
|
+
Parameters
|
|
520
|
+
----------
|
|
521
|
+
f : callable, str or System.Delegate
|
|
522
|
+
Integrand of a float64 array of length ``len(lower)``.
|
|
523
|
+
lower, upper : array_like
|
|
524
|
+
The box bounds, one per dimension.
|
|
525
|
+
seed : int, optional
|
|
526
|
+
Seed of the ``MersenneTwister`` set as ``Random``; None keeps the Numerics generator.
|
|
527
|
+
use_sobol_sequence : bool, optional
|
|
528
|
+
Sample a Sobol sequence instead of ``Random`` (``UseSobolSequence``; Numerics default False).
|
|
529
|
+
**settings
|
|
530
|
+
``Integrator`` settings, the keys of :data:`INTEGRATOR_SETTINGS`.
|
|
531
|
+
|
|
532
|
+
Returns
|
|
533
|
+
-------
|
|
534
|
+
IntegrationResult
|
|
535
|
+
The estimate, standard error, counts and status.
|
|
536
|
+
|
|
537
|
+
Raises
|
|
538
|
+
------
|
|
539
|
+
ValueError
|
|
540
|
+
If the bounds or a setting are out of range (raised by Numerics).
|
|
541
|
+
TypeError
|
|
542
|
+
If a setting name is unknown.
|
|
543
|
+
"""
|
|
544
|
+
fn = callbacks.vector_function(f)
|
|
545
|
+
dimensions, low, high = _bounds(lower, upper)
|
|
546
|
+
net = _integration().MonteCarloIntegration(fn, dimensions, low, high)
|
|
547
|
+
_set(net, Random=_random(seed), UseSobolSequence=_optional(bool, use_sobol_sequence))
|
|
548
|
+
return _run(net, settings, fn)
|
|
549
|
+
|
|
550
|
+
|
|
551
|
+
def integrate_miser(f: Any, lower: Sequence[float], upper: Sequence[float], *, seed: int | None = None,
|
|
552
|
+
use_sobol_sequence: bool | None = None, fraction: float | None = None,
|
|
553
|
+
minimum_number_of_subregion_points: int | None = None,
|
|
554
|
+
minimum_number_of_bisections: int | None = None, dither: float | None = None,
|
|
555
|
+
**settings: Any) -> IntegrationResult:
|
|
556
|
+
"""Integrate ``f(x)`` over a box by recursive stratified sampling (``Miser``).
|
|
557
|
+
|
|
558
|
+
Parameters
|
|
559
|
+
----------
|
|
560
|
+
f : callable, str or System.Delegate
|
|
561
|
+
Integrand of a float64 array of length ``len(lower)``.
|
|
562
|
+
lower, upper : array_like
|
|
563
|
+
The box bounds, one per dimension.
|
|
564
|
+
seed : int, optional
|
|
565
|
+
Seed of the ``MersenneTwister`` set as ``Random``; None keeps the Numerics generator.
|
|
566
|
+
use_sobol_sequence : bool, optional
|
|
567
|
+
Sample a Sobol sequence instead of ``Random`` (``UseSobolSequence``; Numerics default True).
|
|
568
|
+
fraction : float, optional
|
|
569
|
+
Fraction of the points used to explore variance (``Fraction``).
|
|
570
|
+
minimum_number_of_subregion_points : int, optional
|
|
571
|
+
Points below which a region is sampled directly (``MinimumNumberOfSubregionPoints``).
|
|
572
|
+
minimum_number_of_bisections : int, optional
|
|
573
|
+
Bisection threshold (``MinimumNumberOfBisections``).
|
|
574
|
+
dither : float, optional
|
|
575
|
+
Bisection dither (``Dither``).
|
|
576
|
+
**settings
|
|
577
|
+
``Integrator`` settings, the keys of :data:`INTEGRATOR_SETTINGS`.
|
|
578
|
+
|
|
579
|
+
Returns
|
|
580
|
+
-------
|
|
581
|
+
IntegrationResult
|
|
582
|
+
The estimate, standard error, counts and status.
|
|
583
|
+
|
|
584
|
+
Raises
|
|
585
|
+
------
|
|
586
|
+
ValueError
|
|
587
|
+
If the bounds or a setting are out of range (raised by Numerics).
|
|
588
|
+
TypeError
|
|
589
|
+
If a setting name is unknown.
|
|
590
|
+
"""
|
|
591
|
+
fn = callbacks.vector_function(f)
|
|
592
|
+
dimensions, low, high = _bounds(lower, upper)
|
|
593
|
+
net = _integration().Miser(fn, dimensions, low, high)
|
|
594
|
+
_set(net, Random=_random(seed), UseSobolSequence=_optional(bool, use_sobol_sequence),
|
|
595
|
+
Fraction=_optional(float, fraction),
|
|
596
|
+
MinimumNumberOfSubregionPoints=_optional(int, minimum_number_of_subregion_points),
|
|
597
|
+
MinimumNumberOfBisections=_optional(int, minimum_number_of_bisections), Dither=_optional(float, dither))
|
|
598
|
+
return _run(net, settings, fn)
|
|
599
|
+
|
|
600
|
+
|
|
601
|
+
def integrate_vegas(f: Any, lower: Sequence[float], upper: Sequence[float], *, seed: int | None = None,
|
|
602
|
+
use_sobol_sequence: bool | None = None, sobol_seed: int | None = None,
|
|
603
|
+
check_convergence: bool | None = None, independent_evaluations: int | None = None,
|
|
604
|
+
function_calls: int | None = None, alpha: float | None = None,
|
|
605
|
+
number_of_bins: int | None = None, tail_focus_parameter: float | None = None,
|
|
606
|
+
rare_event_probability: float | None = None, **settings: Any) -> VegasResult:
|
|
607
|
+
"""Integrate ``f(x, weight)`` over a box by adaptive importance sampling (``Vegas``).
|
|
608
|
+
|
|
609
|
+
Parameters
|
|
610
|
+
----------
|
|
611
|
+
f : callable, str or System.Delegate
|
|
612
|
+
Integrand of a float64 array of length ``len(lower)`` and the sample weight
|
|
613
|
+
(``Func<double[], double, double>``).
|
|
614
|
+
lower, upper : array_like
|
|
615
|
+
The box bounds, one per dimension.
|
|
616
|
+
seed : int, optional
|
|
617
|
+
Seed of the ``MersenneTwister`` set as ``Random``; None keeps the Numerics generator.
|
|
618
|
+
use_sobol_sequence : bool, optional
|
|
619
|
+
Sample a Sobol sequence instead of ``Random`` (``UseSobolSequence``; Numerics default True).
|
|
620
|
+
sobol_seed : int, optional
|
|
621
|
+
Seed of the Sobol sequence (``SobolSeed``).
|
|
622
|
+
check_convergence : bool, optional
|
|
623
|
+
Stop once the tolerances are met (``CheckConvergence``).
|
|
624
|
+
independent_evaluations : int, optional
|
|
625
|
+
Evaluations of the final independent estimate (``IndependentEvaluations``).
|
|
626
|
+
function_calls : int, optional
|
|
627
|
+
Evaluations per iteration (``FunctionCalls``).
|
|
628
|
+
alpha : float, optional
|
|
629
|
+
Grid adaptation rate (``Alpha``).
|
|
630
|
+
number_of_bins : int, optional
|
|
631
|
+
Grid bins per dimension (``NumberOfBins``).
|
|
632
|
+
tail_focus_parameter : float, optional
|
|
633
|
+
Power-transform exponent that concentrates samples near the upper bounds (``TailFocusParameter``).
|
|
634
|
+
rare_event_probability : float, optional
|
|
635
|
+
Tune the tail focus, bins and alpha for an event of this probability, applied after the
|
|
636
|
+
other options (``ConfigureForRareEvents``).
|
|
637
|
+
**settings
|
|
638
|
+
``Integrator`` settings, the keys of :data:`INTEGRATOR_SETTINGS` (Vegas sets
|
|
639
|
+
``RelativeTolerance`` to 1e-3 by default).
|
|
640
|
+
|
|
641
|
+
Returns
|
|
642
|
+
-------
|
|
643
|
+
VegasResult
|
|
644
|
+
The estimate, standard error, counts, status, chi-squared and adapted grid.
|
|
645
|
+
|
|
646
|
+
Raises
|
|
647
|
+
------
|
|
648
|
+
ValueError
|
|
649
|
+
If the bounds or a setting are out of range (raised by Numerics).
|
|
650
|
+
TypeError
|
|
651
|
+
If a setting name is unknown.
|
|
652
|
+
"""
|
|
653
|
+
fn = callbacks.to_delegate(f, "Func<double[],double,double>")
|
|
654
|
+
dimensions, low, high = _bounds(lower, upper)
|
|
655
|
+
net = _integration().Vegas(fn, dimensions, low, high)
|
|
656
|
+
_set(net, Random=_random(seed), UseSobolSequence=_optional(bool, use_sobol_sequence),
|
|
657
|
+
SobolSeed=_optional(int, sobol_seed), CheckConvergence=_optional(bool, check_convergence),
|
|
658
|
+
IndependentEvaluations=_optional(int, independent_evaluations),
|
|
659
|
+
FunctionCalls=_optional(int, function_calls), Alpha=_optional(float, alpha),
|
|
660
|
+
NumberOfBins=_optional(int, number_of_bins), TailFocusParameter=_optional(float, tail_focus_parameter))
|
|
661
|
+
if rare_event_probability is not None:
|
|
662
|
+
net.ConfigureForRareEvents(float(rare_event_probability))
|
|
663
|
+
base = _run(net, settings, fn)
|
|
664
|
+
return VegasResult(base.value, base.standard_error, base.iterations, base.function_evaluations, base.status,
|
|
665
|
+
float(net.ChiSquared), to_numpy_matrix(net.Grid))
|
|
666
|
+
|
|
667
|
+
|
|
668
|
+
__all__ = [
|
|
669
|
+
"INTEGRATOR_SETTINGS", "AdaptiveGaussKronrodMethod", "IntegrationResult", "IntegrationStatus", "VegasResult",
|
|
670
|
+
"gauss_legendre",
|
|
671
|
+
"gauss_legendre_20", "integrate_adaptive_gauss_kronrod", "integrate_adaptive_gauss_kronrod_2d",
|
|
672
|
+
"integrate_adaptive_gauss_lobatto", "integrate_adaptive_simpson", "integrate_adaptive_simpson_2d",
|
|
673
|
+
"integrate_miser", "integrate_monte_carlo", "integrate_simpson", "integrate_trapezoidal", "integrate_vegas",
|
|
674
|
+
"midpoint_rule", "simpsons_rule", "trapezoidal_rule",
|
|
675
|
+
]
|
|
676
|
+
|
|
677
|
+
translate_public(globals())
|