jasonfan commited on
Commit
2d00d09
·
verified ·
1 Parent(s): 853baad

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/numpy/_info.py +346 -0
  2. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/numpy/_typing.py +46 -0
  3. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/numpy/fft.py +29 -0
  4. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/numpy/linalg.py +90 -0
  5. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/torch/__init__.py +24 -0
  6. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/torch/_aliases.py +752 -0
  7. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/torch/_info.py +358 -0
  8. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/torch/fft.py +86 -0
  9. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/torch/linalg.py +121 -0
  10. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_extra/__init__.py +15 -0
  11. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_extra/_funcs.py +484 -0
  12. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_extra/_typing.py +8 -0
  13. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/__init__.py +20 -0
  14. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/framework.py +1240 -0
  15. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/main.py +1506 -0
  16. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/models.py +1529 -0
  17. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/problem.py +1296 -0
  18. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/settings.py +132 -0
  19. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/subsolvers/__init__.py +14 -0
  20. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/subsolvers/geometry.py +387 -0
  21. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/subsolvers/optim.py +1203 -0
  22. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/utils/__init__.py +18 -0
  23. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/utils/exceptions.py +22 -0
  24. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/utils/math.py +77 -0
  25. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/utils/versions.py +67 -0
  26. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/__init__.py +0 -0
  27. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test__gcutils.py +110 -0
  28. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test__pep440.py +67 -0
  29. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test__testutils.py +32 -0
  30. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test__threadsafety.py +51 -0
  31. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test__util.py +657 -0
  32. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_array_api.py +191 -0
  33. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_bunch.py +162 -0
  34. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_ccallback.py +204 -0
  35. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_config.py +45 -0
  36. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_deprecation.py +10 -0
  37. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_doccer.py +143 -0
  38. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_import_cycles.py +18 -0
  39. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_public_api.py +469 -0
  40. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_scipy_version.py +28 -0
  41. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_tmpdirs.py +48 -0
  42. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_warnings.py +137 -0
  43. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/__init__.py +31 -0
  44. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/hierarchy.py +0 -0
  45. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/tests/__init__.py +0 -0
  46. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/tests/hierarchy_test_data.py +145 -0
  47. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/tests/test_disjoint_set.py +202 -0
  48. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/tests/test_hierarchy.py +1300 -0
  49. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/tests/test_vq.py +450 -0
  50. miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/vq.py +828 -0
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/numpy/_info.py ADDED
@@ -0,0 +1,346 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Array API Inspection namespace
3
+
4
+ This is the namespace for inspection functions as defined by the array API
5
+ standard. See
6
+ https://data-apis.org/array-api/latest/API_specification/inspection.html for
7
+ more details.
8
+
9
+ """
10
+ from numpy import (
11
+ dtype,
12
+ bool_ as bool,
13
+ intp,
14
+ int8,
15
+ int16,
16
+ int32,
17
+ int64,
18
+ uint8,
19
+ uint16,
20
+ uint32,
21
+ uint64,
22
+ float32,
23
+ float64,
24
+ complex64,
25
+ complex128,
26
+ )
27
+
28
+
29
+ class __array_namespace_info__:
30
+ """
31
+ Get the array API inspection namespace for NumPy.
32
+
33
+ The array API inspection namespace defines the following functions:
34
+
35
+ - capabilities()
36
+ - default_device()
37
+ - default_dtypes()
38
+ - dtypes()
39
+ - devices()
40
+
41
+ See
42
+ https://data-apis.org/array-api/latest/API_specification/inspection.html
43
+ for more details.
44
+
45
+ Returns
46
+ -------
47
+ info : ModuleType
48
+ The array API inspection namespace for NumPy.
49
+
50
+ Examples
51
+ --------
52
+ >>> info = np.__array_namespace_info__()
53
+ >>> info.default_dtypes()
54
+ {'real floating': numpy.float64,
55
+ 'complex floating': numpy.complex128,
56
+ 'integral': numpy.int64,
57
+ 'indexing': numpy.int64}
58
+
59
+ """
60
+
61
+ __module__ = 'numpy'
62
+
63
+ def capabilities(self):
64
+ """
65
+ Return a dictionary of array API library capabilities.
66
+
67
+ The resulting dictionary has the following keys:
68
+
69
+ - **"boolean indexing"**: boolean indicating whether an array library
70
+ supports boolean indexing. Always ``True`` for NumPy.
71
+
72
+ - **"data-dependent shapes"**: boolean indicating whether an array
73
+ library supports data-dependent output shapes. Always ``True`` for
74
+ NumPy.
75
+
76
+ See
77
+ https://data-apis.org/array-api/latest/API_specification/generated/array_api.info.capabilities.html
78
+ for more details.
79
+
80
+ See Also
81
+ --------
82
+ __array_namespace_info__.default_device,
83
+ __array_namespace_info__.default_dtypes,
84
+ __array_namespace_info__.dtypes,
85
+ __array_namespace_info__.devices
86
+
87
+ Returns
88
+ -------
89
+ capabilities : dict
90
+ A dictionary of array API library capabilities.
91
+
92
+ Examples
93
+ --------
94
+ >>> info = np.__array_namespace_info__()
95
+ >>> info.capabilities()
96
+ {'boolean indexing': True,
97
+ 'data-dependent shapes': True}
98
+
99
+ """
100
+ return {
101
+ "boolean indexing": True,
102
+ "data-dependent shapes": True,
103
+ # 'max rank' will be part of the 2024.12 standard
104
+ # "max rank": 64,
105
+ }
106
+
107
+ def default_device(self):
108
+ """
109
+ The default device used for new NumPy arrays.
110
+
111
+ For NumPy, this always returns ``'cpu'``.
112
+
113
+ See Also
114
+ --------
115
+ __array_namespace_info__.capabilities,
116
+ __array_namespace_info__.default_dtypes,
117
+ __array_namespace_info__.dtypes,
118
+ __array_namespace_info__.devices
119
+
120
+ Returns
121
+ -------
122
+ device : str
123
+ The default device used for new NumPy arrays.
124
+
125
+ Examples
126
+ --------
127
+ >>> info = np.__array_namespace_info__()
128
+ >>> info.default_device()
129
+ 'cpu'
130
+
131
+ """
132
+ return "cpu"
133
+
134
+ def default_dtypes(self, *, device=None):
135
+ """
136
+ The default data types used for new NumPy arrays.
137
+
138
+ For NumPy, this always returns the following dictionary:
139
+
140
+ - **"real floating"**: ``numpy.float64``
141
+ - **"complex floating"**: ``numpy.complex128``
142
+ - **"integral"**: ``numpy.intp``
143
+ - **"indexing"**: ``numpy.intp``
144
+
145
+ Parameters
146
+ ----------
147
+ device : str, optional
148
+ The device to get the default data types for. For NumPy, only
149
+ ``'cpu'`` is allowed.
150
+
151
+ Returns
152
+ -------
153
+ dtypes : dict
154
+ A dictionary describing the default data types used for new NumPy
155
+ arrays.
156
+
157
+ See Also
158
+ --------
159
+ __array_namespace_info__.capabilities,
160
+ __array_namespace_info__.default_device,
161
+ __array_namespace_info__.dtypes,
162
+ __array_namespace_info__.devices
163
+
164
+ Examples
165
+ --------
166
+ >>> info = np.__array_namespace_info__()
167
+ >>> info.default_dtypes()
168
+ {'real floating': numpy.float64,
169
+ 'complex floating': numpy.complex128,
170
+ 'integral': numpy.int64,
171
+ 'indexing': numpy.int64}
172
+
173
+ """
174
+ if device not in ["cpu", None]:
175
+ raise ValueError(
176
+ 'Device not understood. Only "cpu" is allowed, but received:'
177
+ f' {device}'
178
+ )
179
+ return {
180
+ "real floating": dtype(float64),
181
+ "complex floating": dtype(complex128),
182
+ "integral": dtype(intp),
183
+ "indexing": dtype(intp),
184
+ }
185
+
186
+ def dtypes(self, *, device=None, kind=None):
187
+ """
188
+ The array API data types supported by NumPy.
189
+
190
+ Note that this function only returns data types that are defined by
191
+ the array API.
192
+
193
+ Parameters
194
+ ----------
195
+ device : str, optional
196
+ The device to get the data types for. For NumPy, only ``'cpu'`` is
197
+ allowed.
198
+ kind : str or tuple of str, optional
199
+ The kind of data types to return. If ``None``, all data types are
200
+ returned. If a string, only data types of that kind are returned.
201
+ If a tuple, a dictionary containing the union of the given kinds
202
+ is returned. The following kinds are supported:
203
+
204
+ - ``'bool'``: boolean data types (i.e., ``bool``).
205
+ - ``'signed integer'``: signed integer data types (i.e., ``int8``,
206
+ ``int16``, ``int32``, ``int64``).
207
+ - ``'unsigned integer'``: unsigned integer data types (i.e.,
208
+ ``uint8``, ``uint16``, ``uint32``, ``uint64``).
209
+ - ``'integral'``: integer data types. Shorthand for ``('signed
210
+ integer', 'unsigned integer')``.
211
+ - ``'real floating'``: real-valued floating-point data types
212
+ (i.e., ``float32``, ``float64``).
213
+ - ``'complex floating'``: complex floating-point data types (i.e.,
214
+ ``complex64``, ``complex128``).
215
+ - ``'numeric'``: numeric data types. Shorthand for ``('integral',
216
+ 'real floating', 'complex floating')``.
217
+
218
+ Returns
219
+ -------
220
+ dtypes : dict
221
+ A dictionary mapping the names of data types to the corresponding
222
+ NumPy data types.
223
+
224
+ See Also
225
+ --------
226
+ __array_namespace_info__.capabilities,
227
+ __array_namespace_info__.default_device,
228
+ __array_namespace_info__.default_dtypes,
229
+ __array_namespace_info__.devices
230
+
231
+ Examples
232
+ --------
233
+ >>> info = np.__array_namespace_info__()
234
+ >>> info.dtypes(kind='signed integer')
235
+ {'int8': numpy.int8,
236
+ 'int16': numpy.int16,
237
+ 'int32': numpy.int32,
238
+ 'int64': numpy.int64}
239
+
240
+ """
241
+ if device not in ["cpu", None]:
242
+ raise ValueError(
243
+ 'Device not understood. Only "cpu" is allowed, but received:'
244
+ f' {device}'
245
+ )
246
+ if kind is None:
247
+ return {
248
+ "bool": dtype(bool),
249
+ "int8": dtype(int8),
250
+ "int16": dtype(int16),
251
+ "int32": dtype(int32),
252
+ "int64": dtype(int64),
253
+ "uint8": dtype(uint8),
254
+ "uint16": dtype(uint16),
255
+ "uint32": dtype(uint32),
256
+ "uint64": dtype(uint64),
257
+ "float32": dtype(float32),
258
+ "float64": dtype(float64),
259
+ "complex64": dtype(complex64),
260
+ "complex128": dtype(complex128),
261
+ }
262
+ if kind == "bool":
263
+ return {"bool": bool}
264
+ if kind == "signed integer":
265
+ return {
266
+ "int8": dtype(int8),
267
+ "int16": dtype(int16),
268
+ "int32": dtype(int32),
269
+ "int64": dtype(int64),
270
+ }
271
+ if kind == "unsigned integer":
272
+ return {
273
+ "uint8": dtype(uint8),
274
+ "uint16": dtype(uint16),
275
+ "uint32": dtype(uint32),
276
+ "uint64": dtype(uint64),
277
+ }
278
+ if kind == "integral":
279
+ return {
280
+ "int8": dtype(int8),
281
+ "int16": dtype(int16),
282
+ "int32": dtype(int32),
283
+ "int64": dtype(int64),
284
+ "uint8": dtype(uint8),
285
+ "uint16": dtype(uint16),
286
+ "uint32": dtype(uint32),
287
+ "uint64": dtype(uint64),
288
+ }
289
+ if kind == "real floating":
290
+ return {
291
+ "float32": dtype(float32),
292
+ "float64": dtype(float64),
293
+ }
294
+ if kind == "complex floating":
295
+ return {
296
+ "complex64": dtype(complex64),
297
+ "complex128": dtype(complex128),
298
+ }
299
+ if kind == "numeric":
300
+ return {
301
+ "int8": dtype(int8),
302
+ "int16": dtype(int16),
303
+ "int32": dtype(int32),
304
+ "int64": dtype(int64),
305
+ "uint8": dtype(uint8),
306
+ "uint16": dtype(uint16),
307
+ "uint32": dtype(uint32),
308
+ "uint64": dtype(uint64),
309
+ "float32": dtype(float32),
310
+ "float64": dtype(float64),
311
+ "complex64": dtype(complex64),
312
+ "complex128": dtype(complex128),
313
+ }
314
+ if isinstance(kind, tuple):
315
+ res = {}
316
+ for k in kind:
317
+ res.update(self.dtypes(kind=k))
318
+ return res
319
+ raise ValueError(f"unsupported kind: {kind!r}")
320
+
321
+ def devices(self):
322
+ """
323
+ The devices supported by NumPy.
324
+
325
+ For NumPy, this always returns ``['cpu']``.
326
+
327
+ Returns
328
+ -------
329
+ devices : list of str
330
+ The devices supported by NumPy.
331
+
332
+ See Also
333
+ --------
334
+ __array_namespace_info__.capabilities,
335
+ __array_namespace_info__.default_device,
336
+ __array_namespace_info__.default_dtypes,
337
+ __array_namespace_info__.dtypes
338
+
339
+ Examples
340
+ --------
341
+ >>> info = np.__array_namespace_info__()
342
+ >>> info.devices()
343
+ ['cpu']
344
+
345
+ """
346
+ return ["cpu"]
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/numpy/_typing.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ __all__ = [
4
+ "ndarray",
5
+ "Device",
6
+ "Dtype",
7
+ ]
8
+
9
+ import sys
10
+ from typing import (
11
+ Literal,
12
+ Union,
13
+ TYPE_CHECKING,
14
+ )
15
+
16
+ from numpy import (
17
+ ndarray,
18
+ dtype,
19
+ int8,
20
+ int16,
21
+ int32,
22
+ int64,
23
+ uint8,
24
+ uint16,
25
+ uint32,
26
+ uint64,
27
+ float32,
28
+ float64,
29
+ )
30
+
31
+ Device = Literal["cpu"]
32
+ if TYPE_CHECKING or sys.version_info >= (3, 9):
33
+ Dtype = dtype[Union[
34
+ int8,
35
+ int16,
36
+ int32,
37
+ int64,
38
+ uint8,
39
+ uint16,
40
+ uint32,
41
+ uint64,
42
+ float32,
43
+ float64,
44
+ ]]
45
+ else:
46
+ Dtype = dtype
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/numpy/fft.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from numpy.fft import * # noqa: F403
2
+ from numpy.fft import __all__ as fft_all
3
+
4
+ from ..common import _fft
5
+ from .._internal import get_xp
6
+
7
+ import numpy as np
8
+
9
+ fft = get_xp(np)(_fft.fft)
10
+ ifft = get_xp(np)(_fft.ifft)
11
+ fftn = get_xp(np)(_fft.fftn)
12
+ ifftn = get_xp(np)(_fft.ifftn)
13
+ rfft = get_xp(np)(_fft.rfft)
14
+ irfft = get_xp(np)(_fft.irfft)
15
+ rfftn = get_xp(np)(_fft.rfftn)
16
+ irfftn = get_xp(np)(_fft.irfftn)
17
+ hfft = get_xp(np)(_fft.hfft)
18
+ ihfft = get_xp(np)(_fft.ihfft)
19
+ fftfreq = get_xp(np)(_fft.fftfreq)
20
+ rfftfreq = get_xp(np)(_fft.rfftfreq)
21
+ fftshift = get_xp(np)(_fft.fftshift)
22
+ ifftshift = get_xp(np)(_fft.ifftshift)
23
+
24
+ __all__ = fft_all + _fft.__all__
25
+
26
+ del get_xp
27
+ del np
28
+ del fft_all
29
+ del _fft
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/numpy/linalg.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from numpy.linalg import * # noqa: F403
2
+ from numpy.linalg import __all__ as linalg_all
3
+ import numpy as _np
4
+
5
+ from ..common import _linalg
6
+ from .._internal import get_xp
7
+
8
+ # These functions are in both the main and linalg namespaces
9
+ from ._aliases import matmul, matrix_transpose, tensordot, vecdot # noqa: F401
10
+
11
+ import numpy as np
12
+
13
+ cross = get_xp(np)(_linalg.cross)
14
+ outer = get_xp(np)(_linalg.outer)
15
+ EighResult = _linalg.EighResult
16
+ QRResult = _linalg.QRResult
17
+ SlogdetResult = _linalg.SlogdetResult
18
+ SVDResult = _linalg.SVDResult
19
+ eigh = get_xp(np)(_linalg.eigh)
20
+ qr = get_xp(np)(_linalg.qr)
21
+ slogdet = get_xp(np)(_linalg.slogdet)
22
+ svd = get_xp(np)(_linalg.svd)
23
+ cholesky = get_xp(np)(_linalg.cholesky)
24
+ matrix_rank = get_xp(np)(_linalg.matrix_rank)
25
+ pinv = get_xp(np)(_linalg.pinv)
26
+ matrix_norm = get_xp(np)(_linalg.matrix_norm)
27
+ svdvals = get_xp(np)(_linalg.svdvals)
28
+ diagonal = get_xp(np)(_linalg.diagonal)
29
+ trace = get_xp(np)(_linalg.trace)
30
+
31
+ # Note: unlike np.linalg.solve, the array API solve() only accepts x2 as a
32
+ # vector when it is exactly 1-dimensional. All other cases treat x2 as a stack
33
+ # of matrices. The np.linalg.solve behavior of allowing stacks of both
34
+ # matrices and vectors is ambiguous c.f.
35
+ # https://github.com/numpy/numpy/issues/15349 and
36
+ # https://github.com/data-apis/array-api/issues/285.
37
+
38
+ # To workaround this, the below is the code from np.linalg.solve except
39
+ # only calling solve1 in the exactly 1D case.
40
+
41
+ # This code is here instead of in common because it is numpy specific. Also
42
+ # note that CuPy's solve() does not currently support broadcasting (see
43
+ # https://github.com/cupy/cupy/blob/main/cupy/cublas.py#L43).
44
+ def solve(x1: _np.ndarray, x2: _np.ndarray, /) -> _np.ndarray:
45
+ try:
46
+ from numpy.linalg._linalg import (
47
+ _makearray, _assert_stacked_2d, _assert_stacked_square,
48
+ _commonType, isComplexType, _raise_linalgerror_singular
49
+ )
50
+ except ImportError:
51
+ from numpy.linalg.linalg import (
52
+ _makearray, _assert_stacked_2d, _assert_stacked_square,
53
+ _commonType, isComplexType, _raise_linalgerror_singular
54
+ )
55
+ from numpy.linalg import _umath_linalg
56
+
57
+ x1, _ = _makearray(x1)
58
+ _assert_stacked_2d(x1)
59
+ _assert_stacked_square(x1)
60
+ x2, wrap = _makearray(x2)
61
+ t, result_t = _commonType(x1, x2)
62
+
63
+ # This part is different from np.linalg.solve
64
+ if x2.ndim == 1:
65
+ gufunc = _umath_linalg.solve1
66
+ else:
67
+ gufunc = _umath_linalg.solve
68
+
69
+ # This does nothing currently but is left in because it will be relevant
70
+ # when complex dtype support is added to the spec in 2022.
71
+ signature = 'DD->D' if isComplexType(t) else 'dd->d'
72
+ with _np.errstate(call=_raise_linalgerror_singular, invalid='call',
73
+ over='ignore', divide='ignore', under='ignore'):
74
+ r = gufunc(x1, x2, signature=signature)
75
+
76
+ return wrap(r.astype(result_t, copy=False))
77
+
78
+ # These functions are completely new here. If the library already has them
79
+ # (i.e., numpy 2.0), use the library version instead of our wrapper.
80
+ if hasattr(np.linalg, 'vector_norm'):
81
+ vector_norm = np.linalg.vector_norm
82
+ else:
83
+ vector_norm = get_xp(np)(_linalg.vector_norm)
84
+
85
+ __all__ = linalg_all + _linalg.__all__ + ['solve']
86
+
87
+ del get_xp
88
+ del np
89
+ del linalg_all
90
+ del _linalg
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/torch/__init__.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from torch import * # noqa: F403
2
+
3
+ # Several names are not included in the above import *
4
+ import torch
5
+ for n in dir(torch):
6
+ if (n.startswith('_')
7
+ or n.endswith('_')
8
+ or 'cuda' in n
9
+ or 'cpu' in n
10
+ or 'backward' in n):
11
+ continue
12
+ exec(n + ' = torch.' + n)
13
+
14
+ # These imports may overwrite names from the import * above.
15
+ from ._aliases import * # noqa: F403
16
+
17
+ # See the comment in the numpy __init__.py
18
+ __import__(__package__ + '.linalg')
19
+
20
+ __import__(__package__ + '.fft')
21
+
22
+ from ..common._helpers import * # noqa: F403
23
+
24
+ __array_api_version__ = '2023.12'
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/torch/_aliases.py ADDED
@@ -0,0 +1,752 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from functools import wraps as _wraps
4
+ from builtins import all as _builtin_all, any as _builtin_any
5
+
6
+ from ..common._aliases import (matrix_transpose as _aliases_matrix_transpose,
7
+ vecdot as _aliases_vecdot,
8
+ clip as _aliases_clip,
9
+ unstack as _aliases_unstack,
10
+ cumulative_sum as _aliases_cumulative_sum,
11
+ )
12
+ from .._internal import get_xp
13
+
14
+ from ._info import __array_namespace_info__
15
+
16
+ import torch
17
+
18
+ from typing import TYPE_CHECKING
19
+ if TYPE_CHECKING:
20
+ from typing import List, Optional, Sequence, Tuple, Union
21
+ from ..common._typing import Device
22
+ from torch import dtype as Dtype
23
+
24
+ array = torch.Tensor
25
+
26
+ _int_dtypes = {
27
+ torch.uint8,
28
+ torch.int8,
29
+ torch.int16,
30
+ torch.int32,
31
+ torch.int64,
32
+ }
33
+
34
+ _array_api_dtypes = {
35
+ torch.bool,
36
+ *_int_dtypes,
37
+ torch.float32,
38
+ torch.float64,
39
+ torch.complex64,
40
+ torch.complex128,
41
+ }
42
+
43
+ _promotion_table = {
44
+ # bool
45
+ (torch.bool, torch.bool): torch.bool,
46
+ # ints
47
+ (torch.int8, torch.int8): torch.int8,
48
+ (torch.int8, torch.int16): torch.int16,
49
+ (torch.int8, torch.int32): torch.int32,
50
+ (torch.int8, torch.int64): torch.int64,
51
+ (torch.int16, torch.int8): torch.int16,
52
+ (torch.int16, torch.int16): torch.int16,
53
+ (torch.int16, torch.int32): torch.int32,
54
+ (torch.int16, torch.int64): torch.int64,
55
+ (torch.int32, torch.int8): torch.int32,
56
+ (torch.int32, torch.int16): torch.int32,
57
+ (torch.int32, torch.int32): torch.int32,
58
+ (torch.int32, torch.int64): torch.int64,
59
+ (torch.int64, torch.int8): torch.int64,
60
+ (torch.int64, torch.int16): torch.int64,
61
+ (torch.int64, torch.int32): torch.int64,
62
+ (torch.int64, torch.int64): torch.int64,
63
+ # uints
64
+ (torch.uint8, torch.uint8): torch.uint8,
65
+ # ints and uints (mixed sign)
66
+ (torch.int8, torch.uint8): torch.int16,
67
+ (torch.int16, torch.uint8): torch.int16,
68
+ (torch.int32, torch.uint8): torch.int32,
69
+ (torch.int64, torch.uint8): torch.int64,
70
+ (torch.uint8, torch.int8): torch.int16,
71
+ (torch.uint8, torch.int16): torch.int16,
72
+ (torch.uint8, torch.int32): torch.int32,
73
+ (torch.uint8, torch.int64): torch.int64,
74
+ # floats
75
+ (torch.float32, torch.float32): torch.float32,
76
+ (torch.float32, torch.float64): torch.float64,
77
+ (torch.float64, torch.float32): torch.float64,
78
+ (torch.float64, torch.float64): torch.float64,
79
+ # complexes
80
+ (torch.complex64, torch.complex64): torch.complex64,
81
+ (torch.complex64, torch.complex128): torch.complex128,
82
+ (torch.complex128, torch.complex64): torch.complex128,
83
+ (torch.complex128, torch.complex128): torch.complex128,
84
+ # Mixed float and complex
85
+ (torch.float32, torch.complex64): torch.complex64,
86
+ (torch.float32, torch.complex128): torch.complex128,
87
+ (torch.float64, torch.complex64): torch.complex128,
88
+ (torch.float64, torch.complex128): torch.complex128,
89
+ }
90
+
91
+
92
+ def _two_arg(f):
93
+ @_wraps(f)
94
+ def _f(x1, x2, /, **kwargs):
95
+ x1, x2 = _fix_promotion(x1, x2)
96
+ return f(x1, x2, **kwargs)
97
+ if _f.__doc__ is None:
98
+ _f.__doc__ = f"""\
99
+ Array API compatibility wrapper for torch.{f.__name__}.
100
+
101
+ See the corresponding PyTorch documentation and/or the array API specification
102
+ for more details.
103
+
104
+ """
105
+ return _f
106
+
107
+ def _fix_promotion(x1, x2, only_scalar=True):
108
+ if not isinstance(x1, torch.Tensor) or not isinstance(x2, torch.Tensor):
109
+ return x1, x2
110
+ if x1.dtype not in _array_api_dtypes or x2.dtype not in _array_api_dtypes:
111
+ return x1, x2
112
+ # If an argument is 0-D pytorch downcasts the other argument
113
+ if not only_scalar or x1.shape == ():
114
+ dtype = result_type(x1, x2)
115
+ x2 = x2.to(dtype)
116
+ if not only_scalar or x2.shape == ():
117
+ dtype = result_type(x1, x2)
118
+ x1 = x1.to(dtype)
119
+ return x1, x2
120
+
121
+ def result_type(*arrays_and_dtypes: Union[array, Dtype]) -> Dtype:
122
+ if len(arrays_and_dtypes) == 0:
123
+ raise TypeError("At least one array or dtype must be provided")
124
+ if len(arrays_and_dtypes) == 1:
125
+ x = arrays_and_dtypes[0]
126
+ if isinstance(x, torch.dtype):
127
+ return x
128
+ return x.dtype
129
+ if len(arrays_and_dtypes) > 2:
130
+ return result_type(arrays_and_dtypes[0], result_type(*arrays_and_dtypes[1:]))
131
+
132
+ x, y = arrays_and_dtypes
133
+ xdt = x.dtype if not isinstance(x, torch.dtype) else x
134
+ ydt = y.dtype if not isinstance(y, torch.dtype) else y
135
+
136
+ if (xdt, ydt) in _promotion_table:
137
+ return _promotion_table[xdt, ydt]
138
+
139
+ # This doesn't result_type(dtype, dtype) for non-array API dtypes
140
+ # because torch.result_type only accepts tensors. This does however, allow
141
+ # cross-kind promotion.
142
+ x = torch.tensor([], dtype=x) if isinstance(x, torch.dtype) else x
143
+ y = torch.tensor([], dtype=y) if isinstance(y, torch.dtype) else y
144
+ return torch.result_type(x, y)
145
+
146
+ def can_cast(from_: Union[Dtype, array], to: Dtype, /) -> bool:
147
+ if not isinstance(from_, torch.dtype):
148
+ from_ = from_.dtype
149
+ return torch.can_cast(from_, to)
150
+
151
+ # Basic renames
152
+ bitwise_invert = torch.bitwise_not
153
+ newaxis = None
154
+ # torch.conj sets the conjugation bit, which breaks conversion to other
155
+ # libraries. See https://github.com/data-apis/array-api-compat/issues/173
156
+ conj = torch.conj_physical
157
+
158
+ # Two-arg elementwise functions
159
+ # These require a wrapper to do the correct type promotion on 0-D tensors
160
+ add = _two_arg(torch.add)
161
+ atan2 = _two_arg(torch.atan2)
162
+ bitwise_and = _two_arg(torch.bitwise_and)
163
+ bitwise_left_shift = _two_arg(torch.bitwise_left_shift)
164
+ bitwise_or = _two_arg(torch.bitwise_or)
165
+ bitwise_right_shift = _two_arg(torch.bitwise_right_shift)
166
+ bitwise_xor = _two_arg(torch.bitwise_xor)
167
+ copysign = _two_arg(torch.copysign)
168
+ divide = _two_arg(torch.divide)
169
+ # Also a rename. torch.equal does not broadcast
170
+ equal = _two_arg(torch.eq)
171
+ floor_divide = _two_arg(torch.floor_divide)
172
+ greater = _two_arg(torch.greater)
173
+ greater_equal = _two_arg(torch.greater_equal)
174
+ hypot = _two_arg(torch.hypot)
175
+ less = _two_arg(torch.less)
176
+ less_equal = _two_arg(torch.less_equal)
177
+ logaddexp = _two_arg(torch.logaddexp)
178
+ # logical functions are not included here because they only accept bool in the
179
+ # spec, so type promotion is irrelevant.
180
+ maximum = _two_arg(torch.maximum)
181
+ minimum = _two_arg(torch.minimum)
182
+ multiply = _two_arg(torch.multiply)
183
+ not_equal = _two_arg(torch.not_equal)
184
+ pow = _two_arg(torch.pow)
185
+ remainder = _two_arg(torch.remainder)
186
+ subtract = _two_arg(torch.subtract)
187
+
188
+ # These wrappers are mostly based on the fact that pytorch uses 'dim' instead
189
+ # of 'axis'.
190
+
191
+ # torch.min and torch.max return a tuple and don't support multiple axes https://github.com/pytorch/pytorch/issues/58745
192
+ def max(x: array, /, *, axis: Optional[Union[int, Tuple[int, ...]]] = None, keepdims: bool = False) -> array:
193
+ # https://github.com/pytorch/pytorch/issues/29137
194
+ if axis == ():
195
+ return torch.clone(x)
196
+ return torch.amax(x, axis, keepdims=keepdims)
197
+
198
+ def min(x: array, /, *, axis: Optional[Union[int, Tuple[int, ...]]] = None, keepdims: bool = False) -> array:
199
+ # https://github.com/pytorch/pytorch/issues/29137
200
+ if axis == ():
201
+ return torch.clone(x)
202
+ return torch.amin(x, axis, keepdims=keepdims)
203
+
204
+ clip = get_xp(torch)(_aliases_clip)
205
+ unstack = get_xp(torch)(_aliases_unstack)
206
+ cumulative_sum = get_xp(torch)(_aliases_cumulative_sum)
207
+
208
+ # torch.sort also returns a tuple
209
+ # https://github.com/pytorch/pytorch/issues/70921
210
+ def sort(x: array, /, *, axis: int = -1, descending: bool = False, stable: bool = True, **kwargs) -> array:
211
+ return torch.sort(x, dim=axis, descending=descending, stable=stable, **kwargs).values
212
+
213
+ def _normalize_axes(axis, ndim):
214
+ axes = []
215
+ if ndim == 0 and axis:
216
+ # Better error message in this case
217
+ raise IndexError(f"Dimension out of range: {axis[0]}")
218
+ lower, upper = -ndim, ndim - 1
219
+ for a in axis:
220
+ if a < lower or a > upper:
221
+ # Match torch error message (e.g., from sum())
222
+ raise IndexError(f"Dimension out of range (expected to be in range of [{lower}, {upper}], but got {a}")
223
+ if a < 0:
224
+ a = a + ndim
225
+ if a in axes:
226
+ # Use IndexError instead of RuntimeError, and "axis" instead of "dim"
227
+ raise IndexError(f"Axis {a} appears multiple times in the list of axes")
228
+ axes.append(a)
229
+ return sorted(axes)
230
+
231
+ def _axis_none_keepdims(x, ndim, keepdims):
232
+ # Apply keepdims when axis=None
233
+ # (https://github.com/pytorch/pytorch/issues/71209)
234
+ # Note that this is only valid for the axis=None case.
235
+ if keepdims:
236
+ for i in range(ndim):
237
+ x = torch.unsqueeze(x, 0)
238
+ return x
239
+
240
+ def _reduce_multiple_axes(f, x, axis, keepdims=False, **kwargs):
241
+ # Some reductions don't support multiple axes
242
+ # (https://github.com/pytorch/pytorch/issues/56586).
243
+ axes = _normalize_axes(axis, x.ndim)
244
+ for a in reversed(axes):
245
+ x = torch.movedim(x, a, -1)
246
+ x = torch.flatten(x, -len(axes))
247
+
248
+ out = f(x, -1, **kwargs)
249
+
250
+ if keepdims:
251
+ for a in axes:
252
+ out = torch.unsqueeze(out, a)
253
+ return out
254
+
255
+ def prod(x: array,
256
+ /,
257
+ *,
258
+ axis: Optional[Union[int, Tuple[int, ...]]] = None,
259
+ dtype: Optional[Dtype] = None,
260
+ keepdims: bool = False,
261
+ **kwargs) -> array:
262
+ x = torch.asarray(x)
263
+ ndim = x.ndim
264
+
265
+ # https://github.com/pytorch/pytorch/issues/29137. Separate from the logic
266
+ # below because it still needs to upcast.
267
+ if axis == ():
268
+ if dtype is None:
269
+ # We can't upcast uint8 according to the spec because there is no
270
+ # torch.uint64, so at least upcast to int64 which is what sum does
271
+ # when axis=None.
272
+ if x.dtype in [torch.int8, torch.int16, torch.int32, torch.uint8]:
273
+ return x.to(torch.int64)
274
+ return x.clone()
275
+ return x.to(dtype)
276
+
277
+ # torch.prod doesn't support multiple axes
278
+ # (https://github.com/pytorch/pytorch/issues/56586).
279
+ if isinstance(axis, tuple):
280
+ return _reduce_multiple_axes(torch.prod, x, axis, keepdims=keepdims, dtype=dtype, **kwargs)
281
+ if axis is None:
282
+ # torch doesn't support keepdims with axis=None
283
+ # (https://github.com/pytorch/pytorch/issues/71209)
284
+ res = torch.prod(x, dtype=dtype, **kwargs)
285
+ res = _axis_none_keepdims(res, ndim, keepdims)
286
+ return res
287
+
288
+ return torch.prod(x, axis, dtype=dtype, keepdims=keepdims, **kwargs)
289
+
290
+
291
+ def sum(x: array,
292
+ /,
293
+ *,
294
+ axis: Optional[Union[int, Tuple[int, ...]]] = None,
295
+ dtype: Optional[Dtype] = None,
296
+ keepdims: bool = False,
297
+ **kwargs) -> array:
298
+ x = torch.asarray(x)
299
+ ndim = x.ndim
300
+
301
+ # https://github.com/pytorch/pytorch/issues/29137.
302
+ # Make sure it upcasts.
303
+ if axis == ():
304
+ if dtype is None:
305
+ # We can't upcast uint8 according to the spec because there is no
306
+ # torch.uint64, so at least upcast to int64 which is what sum does
307
+ # when axis=None.
308
+ if x.dtype in [torch.int8, torch.int16, torch.int32, torch.uint8]:
309
+ return x.to(torch.int64)
310
+ return x.clone()
311
+ return x.to(dtype)
312
+
313
+ if axis is None:
314
+ # torch doesn't support keepdims with axis=None
315
+ # (https://github.com/pytorch/pytorch/issues/71209)
316
+ res = torch.sum(x, dtype=dtype, **kwargs)
317
+ res = _axis_none_keepdims(res, ndim, keepdims)
318
+ return res
319
+
320
+ return torch.sum(x, axis, dtype=dtype, keepdims=keepdims, **kwargs)
321
+
322
+ def any(x: array,
323
+ /,
324
+ *,
325
+ axis: Optional[Union[int, Tuple[int, ...]]] = None,
326
+ keepdims: bool = False,
327
+ **kwargs) -> array:
328
+ x = torch.asarray(x)
329
+ ndim = x.ndim
330
+ if axis == ():
331
+ return x.to(torch.bool)
332
+ # torch.any doesn't support multiple axes
333
+ # (https://github.com/pytorch/pytorch/issues/56586).
334
+ if isinstance(axis, tuple):
335
+ res = _reduce_multiple_axes(torch.any, x, axis, keepdims=keepdims, **kwargs)
336
+ return res.to(torch.bool)
337
+ if axis is None:
338
+ # torch doesn't support keepdims with axis=None
339
+ # (https://github.com/pytorch/pytorch/issues/71209)
340
+ res = torch.any(x, **kwargs)
341
+ res = _axis_none_keepdims(res, ndim, keepdims)
342
+ return res.to(torch.bool)
343
+
344
+ # torch.any doesn't return bool for uint8
345
+ return torch.any(x, axis, keepdims=keepdims).to(torch.bool)
346
+
347
+ def all(x: array,
348
+ /,
349
+ *,
350
+ axis: Optional[Union[int, Tuple[int, ...]]] = None,
351
+ keepdims: bool = False,
352
+ **kwargs) -> array:
353
+ x = torch.asarray(x)
354
+ ndim = x.ndim
355
+ if axis == ():
356
+ return x.to(torch.bool)
357
+ # torch.all doesn't support multiple axes
358
+ # (https://github.com/pytorch/pytorch/issues/56586).
359
+ if isinstance(axis, tuple):
360
+ res = _reduce_multiple_axes(torch.all, x, axis, keepdims=keepdims, **kwargs)
361
+ return res.to(torch.bool)
362
+ if axis is None:
363
+ # torch doesn't support keepdims with axis=None
364
+ # (https://github.com/pytorch/pytorch/issues/71209)
365
+ res = torch.all(x, **kwargs)
366
+ res = _axis_none_keepdims(res, ndim, keepdims)
367
+ return res.to(torch.bool)
368
+
369
+ # torch.all doesn't return bool for uint8
370
+ return torch.all(x, axis, keepdims=keepdims).to(torch.bool)
371
+
372
+ def mean(x: array,
373
+ /,
374
+ *,
375
+ axis: Optional[Union[int, Tuple[int, ...]]] = None,
376
+ keepdims: bool = False,
377
+ **kwargs) -> array:
378
+ # https://github.com/pytorch/pytorch/issues/29137
379
+ if axis == ():
380
+ return torch.clone(x)
381
+ if axis is None:
382
+ # torch doesn't support keepdims with axis=None
383
+ # (https://github.com/pytorch/pytorch/issues/71209)
384
+ res = torch.mean(x, **kwargs)
385
+ res = _axis_none_keepdims(res, x.ndim, keepdims)
386
+ return res
387
+ return torch.mean(x, axis, keepdims=keepdims, **kwargs)
388
+
389
+ def std(x: array,
390
+ /,
391
+ *,
392
+ axis: Optional[Union[int, Tuple[int, ...]]] = None,
393
+ correction: Union[int, float] = 0.0,
394
+ keepdims: bool = False,
395
+ **kwargs) -> array:
396
+ # Note, float correction is not supported
397
+ # https://github.com/pytorch/pytorch/issues/61492. We don't try to
398
+ # implement it here for now.
399
+
400
+ if isinstance(correction, float):
401
+ _correction = int(correction)
402
+ if correction != _correction:
403
+ raise NotImplementedError("float correction in torch std() is not yet supported")
404
+ else:
405
+ _correction = correction
406
+
407
+ # https://github.com/pytorch/pytorch/issues/29137
408
+ if axis == ():
409
+ return torch.zeros_like(x)
410
+ if isinstance(axis, int):
411
+ axis = (axis,)
412
+ if axis is None:
413
+ # torch doesn't support keepdims with axis=None
414
+ # (https://github.com/pytorch/pytorch/issues/71209)
415
+ res = torch.std(x, tuple(range(x.ndim)), correction=_correction, **kwargs)
416
+ res = _axis_none_keepdims(res, x.ndim, keepdims)
417
+ return res
418
+ return torch.std(x, axis, correction=_correction, keepdims=keepdims, **kwargs)
419
+
420
+ def var(x: array,
421
+ /,
422
+ *,
423
+ axis: Optional[Union[int, Tuple[int, ...]]] = None,
424
+ correction: Union[int, float] = 0.0,
425
+ keepdims: bool = False,
426
+ **kwargs) -> array:
427
+ # Note, float correction is not supported
428
+ # https://github.com/pytorch/pytorch/issues/61492. We don't try to
429
+ # implement it here for now.
430
+
431
+ # if isinstance(correction, float):
432
+ # correction = int(correction)
433
+
434
+ # https://github.com/pytorch/pytorch/issues/29137
435
+ if axis == ():
436
+ return torch.zeros_like(x)
437
+ if isinstance(axis, int):
438
+ axis = (axis,)
439
+ if axis is None:
440
+ # torch doesn't support keepdims with axis=None
441
+ # (https://github.com/pytorch/pytorch/issues/71209)
442
+ res = torch.var(x, tuple(range(x.ndim)), correction=correction, **kwargs)
443
+ res = _axis_none_keepdims(res, x.ndim, keepdims)
444
+ return res
445
+ return torch.var(x, axis, correction=correction, keepdims=keepdims, **kwargs)
446
+
447
+ # torch.concat doesn't support dim=None
448
+ # https://github.com/pytorch/pytorch/issues/70925
449
+ def concat(arrays: Union[Tuple[array, ...], List[array]],
450
+ /,
451
+ *,
452
+ axis: Optional[int] = 0,
453
+ **kwargs) -> array:
454
+ if axis is None:
455
+ arrays = tuple(ar.flatten() for ar in arrays)
456
+ axis = 0
457
+ return torch.concat(arrays, axis, **kwargs)
458
+
459
+ # torch.squeeze only accepts int dim and doesn't require it
460
+ # https://github.com/pytorch/pytorch/issues/70924. Support for tuple dim was
461
+ # added at https://github.com/pytorch/pytorch/pull/89017.
462
+ def squeeze(x: array, /, axis: Union[int, Tuple[int, ...]]) -> array:
463
+ if isinstance(axis, int):
464
+ axis = (axis,)
465
+ for a in axis:
466
+ if x.shape[a] != 1:
467
+ raise ValueError("squeezed dimensions must be equal to 1")
468
+ axes = _normalize_axes(axis, x.ndim)
469
+ # Remove this once pytorch 1.14 is released with the above PR #89017.
470
+ sequence = [a - i for i, a in enumerate(axes)]
471
+ for a in sequence:
472
+ x = torch.squeeze(x, a)
473
+ return x
474
+
475
+ # torch.broadcast_to uses size instead of shape
476
+ def broadcast_to(x: array, /, shape: Tuple[int, ...], **kwargs) -> array:
477
+ return torch.broadcast_to(x, shape, **kwargs)
478
+
479
+ # torch.permute uses dims instead of axes
480
+ def permute_dims(x: array, /, axes: Tuple[int, ...]) -> array:
481
+ return torch.permute(x, axes)
482
+
483
+ # The axis parameter doesn't work for flip() and roll()
484
+ # https://github.com/pytorch/pytorch/issues/71210. Also torch.flip() doesn't
485
+ # accept axis=None
486
+ def flip(x: array, /, *, axis: Optional[Union[int, Tuple[int, ...]]] = None, **kwargs) -> array:
487
+ if axis is None:
488
+ axis = tuple(range(x.ndim))
489
+ # torch.flip doesn't accept dim as an int but the method does
490
+ # https://github.com/pytorch/pytorch/issues/18095
491
+ return x.flip(axis, **kwargs)
492
+
493
+ def roll(x: array, /, shift: Union[int, Tuple[int, ...]], *, axis: Optional[Union[int, Tuple[int, ...]]] = None, **kwargs) -> array:
494
+ return torch.roll(x, shift, axis, **kwargs)
495
+
496
+ def nonzero(x: array, /, **kwargs) -> Tuple[array, ...]:
497
+ if x.ndim == 0:
498
+ raise ValueError("nonzero() does not support zero-dimensional arrays")
499
+ return torch.nonzero(x, as_tuple=True, **kwargs)
500
+
501
+ def where(condition: array, x1: array, x2: array, /) -> array:
502
+ x1, x2 = _fix_promotion(x1, x2)
503
+ return torch.where(condition, x1, x2)
504
+
505
+ # torch.reshape doesn't have the copy keyword
506
+ def reshape(x: array,
507
+ /,
508
+ shape: Tuple[int, ...],
509
+ copy: Optional[bool] = None,
510
+ **kwargs) -> array:
511
+ if copy is not None:
512
+ raise NotImplementedError("torch.reshape doesn't yet support the copy keyword")
513
+ return torch.reshape(x, shape, **kwargs)
514
+
515
+ # torch.arange doesn't support returning empty arrays
516
+ # (https://github.com/pytorch/pytorch/issues/70915), and doesn't support some
517
+ # keyword argument combinations
518
+ # (https://github.com/pytorch/pytorch/issues/70914)
519
+ def arange(start: Union[int, float],
520
+ /,
521
+ stop: Optional[Union[int, float]] = None,
522
+ step: Union[int, float] = 1,
523
+ *,
524
+ dtype: Optional[Dtype] = None,
525
+ device: Optional[Device] = None,
526
+ **kwargs) -> array:
527
+ if stop is None:
528
+ start, stop = 0, start
529
+ if step > 0 and stop <= start or step < 0 and stop >= start:
530
+ if dtype is None:
531
+ if _builtin_all(isinstance(i, int) for i in [start, stop, step]):
532
+ dtype = torch.int64
533
+ else:
534
+ dtype = torch.float32
535
+ return torch.empty(0, dtype=dtype, device=device, **kwargs)
536
+ return torch.arange(start, stop, step, dtype=dtype, device=device, **kwargs)
537
+
538
+ # torch.eye does not accept None as a default for the second argument and
539
+ # doesn't support off-diagonals (https://github.com/pytorch/pytorch/issues/70910)
540
+ def eye(n_rows: int,
541
+ n_cols: Optional[int] = None,
542
+ /,
543
+ *,
544
+ k: int = 0,
545
+ dtype: Optional[Dtype] = None,
546
+ device: Optional[Device] = None,
547
+ **kwargs) -> array:
548
+ if n_cols is None:
549
+ n_cols = n_rows
550
+ z = torch.zeros(n_rows, n_cols, dtype=dtype, device=device, **kwargs)
551
+ if abs(k) <= n_rows + n_cols:
552
+ z.diagonal(k).fill_(1)
553
+ return z
554
+
555
+ # torch.linspace doesn't have the endpoint parameter
556
+ def linspace(start: Union[int, float],
557
+ stop: Union[int, float],
558
+ /,
559
+ num: int,
560
+ *,
561
+ dtype: Optional[Dtype] = None,
562
+ device: Optional[Device] = None,
563
+ endpoint: bool = True,
564
+ **kwargs) -> array:
565
+ if not endpoint:
566
+ return torch.linspace(start, stop, num+1, dtype=dtype, device=device, **kwargs)[:-1]
567
+ return torch.linspace(start, stop, num, dtype=dtype, device=device, **kwargs)
568
+
569
+ # torch.full does not accept an int size
570
+ # https://github.com/pytorch/pytorch/issues/70906
571
+ def full(shape: Union[int, Tuple[int, ...]],
572
+ fill_value: Union[bool, int, float, complex],
573
+ *,
574
+ dtype: Optional[Dtype] = None,
575
+ device: Optional[Device] = None,
576
+ **kwargs) -> array:
577
+ if isinstance(shape, int):
578
+ shape = (shape,)
579
+
580
+ return torch.full(shape, fill_value, dtype=dtype, device=device, **kwargs)
581
+
582
+ # ones, zeros, and empty do not accept shape as a keyword argument
583
+ def ones(shape: Union[int, Tuple[int, ...]],
584
+ *,
585
+ dtype: Optional[Dtype] = None,
586
+ device: Optional[Device] = None,
587
+ **kwargs) -> array:
588
+ return torch.ones(shape, dtype=dtype, device=device, **kwargs)
589
+
590
+ def zeros(shape: Union[int, Tuple[int, ...]],
591
+ *,
592
+ dtype: Optional[Dtype] = None,
593
+ device: Optional[Device] = None,
594
+ **kwargs) -> array:
595
+ return torch.zeros(shape, dtype=dtype, device=device, **kwargs)
596
+
597
+ def empty(shape: Union[int, Tuple[int, ...]],
598
+ *,
599
+ dtype: Optional[Dtype] = None,
600
+ device: Optional[Device] = None,
601
+ **kwargs) -> array:
602
+ return torch.empty(shape, dtype=dtype, device=device, **kwargs)
603
+
604
+ # tril and triu do not call the keyword argument k
605
+
606
+ def tril(x: array, /, *, k: int = 0) -> array:
607
+ return torch.tril(x, k)
608
+
609
+ def triu(x: array, /, *, k: int = 0) -> array:
610
+ return torch.triu(x, k)
611
+
612
+ # Functions that aren't in torch https://github.com/pytorch/pytorch/issues/58742
613
+ def expand_dims(x: array, /, *, axis: int = 0) -> array:
614
+ return torch.unsqueeze(x, axis)
615
+
616
+ def astype(x: array, dtype: Dtype, /, *, copy: bool = True) -> array:
617
+ return x.to(dtype, copy=copy)
618
+
619
+ def broadcast_arrays(*arrays: array) -> List[array]:
620
+ shape = torch.broadcast_shapes(*[a.shape for a in arrays])
621
+ return [torch.broadcast_to(a, shape) for a in arrays]
622
+
623
+ # Note that these named tuples aren't actually part of the standard namespace,
624
+ # but I don't see any issue with exporting the names here regardless.
625
+ from ..common._aliases import (UniqueAllResult, UniqueCountsResult,
626
+ UniqueInverseResult)
627
+
628
+ # https://github.com/pytorch/pytorch/issues/70920
629
+ def unique_all(x: array) -> UniqueAllResult:
630
+ # torch.unique doesn't support returning indices.
631
+ # https://github.com/pytorch/pytorch/issues/36748. The workaround
632
+ # suggested in that issue doesn't actually function correctly (it relies
633
+ # on non-deterministic behavior of scatter()).
634
+ raise NotImplementedError("unique_all() not yet implemented for pytorch (see https://github.com/pytorch/pytorch/issues/36748)")
635
+
636
+ # values, inverse_indices, counts = torch.unique(x, return_counts=True, return_inverse=True)
637
+ # # torch.unique incorrectly gives a 0 count for nan values.
638
+ # # https://github.com/pytorch/pytorch/issues/94106
639
+ # counts[torch.isnan(values)] = 1
640
+ # return UniqueAllResult(values, indices, inverse_indices, counts)
641
+
642
+ def unique_counts(x: array) -> UniqueCountsResult:
643
+ values, counts = torch.unique(x, return_counts=True)
644
+
645
+ # torch.unique incorrectly gives a 0 count for nan values.
646
+ # https://github.com/pytorch/pytorch/issues/94106
647
+ counts[torch.isnan(values)] = 1
648
+ return UniqueCountsResult(values, counts)
649
+
650
+ def unique_inverse(x: array) -> UniqueInverseResult:
651
+ values, inverse = torch.unique(x, return_inverse=True)
652
+ return UniqueInverseResult(values, inverse)
653
+
654
+ def unique_values(x: array) -> array:
655
+ return torch.unique(x)
656
+
657
+ def matmul(x1: array, x2: array, /, **kwargs) -> array:
658
+ # torch.matmul doesn't type promote (but differently from _fix_promotion)
659
+ x1, x2 = _fix_promotion(x1, x2, only_scalar=False)
660
+ return torch.matmul(x1, x2, **kwargs)
661
+
662
+ matrix_transpose = get_xp(torch)(_aliases_matrix_transpose)
663
+ _vecdot = get_xp(torch)(_aliases_vecdot)
664
+
665
+ def vecdot(x1: array, x2: array, /, *, axis: int = -1) -> array:
666
+ x1, x2 = _fix_promotion(x1, x2, only_scalar=False)
667
+ return _vecdot(x1, x2, axis=axis)
668
+
669
+ # torch.tensordot uses dims instead of axes
670
+ def tensordot(x1: array, x2: array, /, *, axes: Union[int, Tuple[Sequence[int], Sequence[int]]] = 2, **kwargs) -> array:
671
+ # Note: torch.tensordot fails with integer dtypes when there is only 1
672
+ # element in the axis (https://github.com/pytorch/pytorch/issues/84530).
673
+ x1, x2 = _fix_promotion(x1, x2, only_scalar=False)
674
+ return torch.tensordot(x1, x2, dims=axes, **kwargs)
675
+
676
+
677
+ def isdtype(
678
+ dtype: Dtype, kind: Union[Dtype, str, Tuple[Union[Dtype, str], ...]],
679
+ *, _tuple=True, # Disallow nested tuples
680
+ ) -> bool:
681
+ """
682
+ Returns a boolean indicating whether a provided dtype is of a specified data type ``kind``.
683
+
684
+ Note that outside of this function, this compat library does not yet fully
685
+ support complex numbers.
686
+
687
+ See
688
+ https://data-apis.org/array-api/latest/API_specification/generated/array_api.isdtype.html
689
+ for more details
690
+ """
691
+ if isinstance(kind, tuple) and _tuple:
692
+ return _builtin_any(isdtype(dtype, k, _tuple=False) for k in kind)
693
+ elif isinstance(kind, str):
694
+ if kind == 'bool':
695
+ return dtype == torch.bool
696
+ elif kind == 'signed integer':
697
+ return dtype in _int_dtypes and dtype.is_signed
698
+ elif kind == 'unsigned integer':
699
+ return dtype in _int_dtypes and not dtype.is_signed
700
+ elif kind == 'integral':
701
+ return dtype in _int_dtypes
702
+ elif kind == 'real floating':
703
+ return dtype.is_floating_point
704
+ elif kind == 'complex floating':
705
+ return dtype.is_complex
706
+ elif kind == 'numeric':
707
+ return isdtype(dtype, ('integral', 'real floating', 'complex floating'))
708
+ else:
709
+ raise ValueError(f"Unrecognized data type kind: {kind!r}")
710
+ else:
711
+ return dtype == kind
712
+
713
+ def take(x: array, indices: array, /, *, axis: Optional[int] = None, **kwargs) -> array:
714
+ if axis is None:
715
+ if x.ndim != 1:
716
+ raise ValueError("axis must be specified when ndim > 1")
717
+ axis = 0
718
+ return torch.index_select(x, axis, indices, **kwargs)
719
+
720
+ def sign(x: array, /) -> array:
721
+ # torch sign() does not support complex numbers and does not propagate
722
+ # nans. See https://github.com/data-apis/array-api-compat/issues/136
723
+ if x.dtype.is_complex:
724
+ out = x/torch.abs(x)
725
+ # sign(0) = 0 but the above formula would give nan
726
+ out[x == 0+0j] = 0+0j
727
+ return out
728
+ else:
729
+ out = torch.sign(x)
730
+ if x.dtype.is_floating_point:
731
+ out[torch.isnan(x)] = torch.nan
732
+ return out
733
+
734
+
735
+ __all__ = ['__array_namespace_info__', 'result_type', 'can_cast',
736
+ 'permute_dims', 'bitwise_invert', 'newaxis', 'conj', 'add',
737
+ 'atan2', 'bitwise_and', 'bitwise_left_shift', 'bitwise_or',
738
+ 'bitwise_right_shift', 'bitwise_xor', 'copysign', 'divide',
739
+ 'equal', 'floor_divide', 'greater', 'greater_equal', 'hypot',
740
+ 'less', 'less_equal', 'logaddexp', 'maximum', 'minimum',
741
+ 'multiply', 'not_equal', 'pow', 'remainder', 'subtract', 'max',
742
+ 'min', 'clip', 'unstack', 'cumulative_sum', 'sort', 'prod', 'sum',
743
+ 'any', 'all', 'mean', 'std', 'var', 'concat', 'squeeze',
744
+ 'broadcast_to', 'flip', 'roll', 'nonzero', 'where', 'reshape',
745
+ 'arange', 'eye', 'linspace', 'full', 'ones', 'zeros', 'empty',
746
+ 'tril', 'triu', 'expand_dims', 'astype', 'broadcast_arrays',
747
+ 'UniqueAllResult', 'UniqueCountsResult', 'UniqueInverseResult',
748
+ 'unique_all', 'unique_counts', 'unique_inverse', 'unique_values',
749
+ 'matmul', 'matrix_transpose', 'vecdot', 'tensordot', 'isdtype',
750
+ 'take', 'sign']
751
+
752
+ _all_ignore = ['torch', 'get_xp']
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/torch/_info.py ADDED
@@ -0,0 +1,358 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Array API Inspection namespace
3
+
4
+ This is the namespace for inspection functions as defined by the array API
5
+ standard. See
6
+ https://data-apis.org/array-api/latest/API_specification/inspection.html for
7
+ more details.
8
+
9
+ """
10
+ import torch
11
+
12
+ from functools import cache
13
+
14
+ class __array_namespace_info__:
15
+ """
16
+ Get the array API inspection namespace for PyTorch.
17
+
18
+ The array API inspection namespace defines the following functions:
19
+
20
+ - capabilities()
21
+ - default_device()
22
+ - default_dtypes()
23
+ - dtypes()
24
+ - devices()
25
+
26
+ See
27
+ https://data-apis.org/array-api/latest/API_specification/inspection.html
28
+ for more details.
29
+
30
+ Returns
31
+ -------
32
+ info : ModuleType
33
+ The array API inspection namespace for PyTorch.
34
+
35
+ Examples
36
+ --------
37
+ >>> info = np.__array_namespace_info__()
38
+ >>> info.default_dtypes()
39
+ {'real floating': numpy.float64,
40
+ 'complex floating': numpy.complex128,
41
+ 'integral': numpy.int64,
42
+ 'indexing': numpy.int64}
43
+
44
+ """
45
+
46
+ __module__ = 'torch'
47
+
48
+ def capabilities(self):
49
+ """
50
+ Return a dictionary of array API library capabilities.
51
+
52
+ The resulting dictionary has the following keys:
53
+
54
+ - **"boolean indexing"**: boolean indicating whether an array library
55
+ supports boolean indexing. Always ``True`` for PyTorch.
56
+
57
+ - **"data-dependent shapes"**: boolean indicating whether an array
58
+ library supports data-dependent output shapes. Always ``True`` for
59
+ PyTorch.
60
+
61
+ See
62
+ https://data-apis.org/array-api/latest/API_specification/generated/array_api.info.capabilities.html
63
+ for more details.
64
+
65
+ See Also
66
+ --------
67
+ __array_namespace_info__.default_device,
68
+ __array_namespace_info__.default_dtypes,
69
+ __array_namespace_info__.dtypes,
70
+ __array_namespace_info__.devices
71
+
72
+ Returns
73
+ -------
74
+ capabilities : dict
75
+ A dictionary of array API library capabilities.
76
+
77
+ Examples
78
+ --------
79
+ >>> info = np.__array_namespace_info__()
80
+ >>> info.capabilities()
81
+ {'boolean indexing': True,
82
+ 'data-dependent shapes': True}
83
+
84
+ """
85
+ return {
86
+ "boolean indexing": True,
87
+ "data-dependent shapes": True,
88
+ # 'max rank' will be part of the 2024.12 standard
89
+ # "max rank": 64,
90
+ }
91
+
92
+ def default_device(self):
93
+ """
94
+ The default device used for new PyTorch arrays.
95
+
96
+ See Also
97
+ --------
98
+ __array_namespace_info__.capabilities,
99
+ __array_namespace_info__.default_dtypes,
100
+ __array_namespace_info__.dtypes,
101
+ __array_namespace_info__.devices
102
+
103
+ Returns
104
+ -------
105
+ device : str
106
+ The default device used for new PyTorch arrays.
107
+
108
+ Examples
109
+ --------
110
+ >>> info = np.__array_namespace_info__()
111
+ >>> info.default_device()
112
+ 'cpu'
113
+
114
+ """
115
+ return torch.device("cpu")
116
+
117
+ def default_dtypes(self, *, device=None):
118
+ """
119
+ The default data types used for new PyTorch arrays.
120
+
121
+ Parameters
122
+ ----------
123
+ device : str, optional
124
+ The device to get the default data types for. For PyTorch, only
125
+ ``'cpu'`` is allowed.
126
+
127
+ Returns
128
+ -------
129
+ dtypes : dict
130
+ A dictionary describing the default data types used for new PyTorch
131
+ arrays.
132
+
133
+ See Also
134
+ --------
135
+ __array_namespace_info__.capabilities,
136
+ __array_namespace_info__.default_device,
137
+ __array_namespace_info__.dtypes,
138
+ __array_namespace_info__.devices
139
+
140
+ Examples
141
+ --------
142
+ >>> info = np.__array_namespace_info__()
143
+ >>> info.default_dtypes()
144
+ {'real floating': torch.float32,
145
+ 'complex floating': torch.complex64,
146
+ 'integral': torch.int64,
147
+ 'indexing': torch.int64}
148
+
149
+ """
150
+ # Note: if the default is set to float64, the devices like MPS that
151
+ # don't support float64 will error. We still return the default_dtype
152
+ # value here because this error doesn't represent a different default
153
+ # per-device.
154
+ default_floating = torch.get_default_dtype()
155
+ default_complex = torch.complex64 if default_floating == torch.float32 else torch.complex128
156
+ default_integral = torch.int64
157
+ return {
158
+ "real floating": default_floating,
159
+ "complex floating": default_complex,
160
+ "integral": default_integral,
161
+ "indexing": default_integral,
162
+ }
163
+
164
+
165
+ def _dtypes(self, kind):
166
+ bool = torch.bool
167
+ int8 = torch.int8
168
+ int16 = torch.int16
169
+ int32 = torch.int32
170
+ int64 = torch.int64
171
+ uint8 = torch.uint8
172
+ # uint16, uint32, and uint64 are present in newer versions of pytorch,
173
+ # but they aren't generally supported by the array API functions, so
174
+ # we omit them from this function.
175
+ float32 = torch.float32
176
+ float64 = torch.float64
177
+ complex64 = torch.complex64
178
+ complex128 = torch.complex128
179
+
180
+ if kind is None:
181
+ return {
182
+ "bool": bool,
183
+ "int8": int8,
184
+ "int16": int16,
185
+ "int32": int32,
186
+ "int64": int64,
187
+ "uint8": uint8,
188
+ "float32": float32,
189
+ "float64": float64,
190
+ "complex64": complex64,
191
+ "complex128": complex128,
192
+ }
193
+ if kind == "bool":
194
+ return {"bool": bool}
195
+ if kind == "signed integer":
196
+ return {
197
+ "int8": int8,
198
+ "int16": int16,
199
+ "int32": int32,
200
+ "int64": int64,
201
+ }
202
+ if kind == "unsigned integer":
203
+ return {
204
+ "uint8": uint8,
205
+ }
206
+ if kind == "integral":
207
+ return {
208
+ "int8": int8,
209
+ "int16": int16,
210
+ "int32": int32,
211
+ "int64": int64,
212
+ "uint8": uint8,
213
+ }
214
+ if kind == "real floating":
215
+ return {
216
+ "float32": float32,
217
+ "float64": float64,
218
+ }
219
+ if kind == "complex floating":
220
+ return {
221
+ "complex64": complex64,
222
+ "complex128": complex128,
223
+ }
224
+ if kind == "numeric":
225
+ return {
226
+ "int8": int8,
227
+ "int16": int16,
228
+ "int32": int32,
229
+ "int64": int64,
230
+ "uint8": uint8,
231
+ "float32": float32,
232
+ "float64": float64,
233
+ "complex64": complex64,
234
+ "complex128": complex128,
235
+ }
236
+ if isinstance(kind, tuple):
237
+ res = {}
238
+ for k in kind:
239
+ res.update(self.dtypes(kind=k))
240
+ return res
241
+ raise ValueError(f"unsupported kind: {kind!r}")
242
+
243
+ @cache
244
+ def dtypes(self, *, device=None, kind=None):
245
+ """
246
+ The array API data types supported by PyTorch.
247
+
248
+ Note that this function only returns data types that are defined by
249
+ the array API.
250
+
251
+ Parameters
252
+ ----------
253
+ device : str, optional
254
+ The device to get the data types for.
255
+ kind : str or tuple of str, optional
256
+ The kind of data types to return. If ``None``, all data types are
257
+ returned. If a string, only data types of that kind are returned.
258
+ If a tuple, a dictionary containing the union of the given kinds
259
+ is returned. The following kinds are supported:
260
+
261
+ - ``'bool'``: boolean data types (i.e., ``bool``).
262
+ - ``'signed integer'``: signed integer data types (i.e., ``int8``,
263
+ ``int16``, ``int32``, ``int64``).
264
+ - ``'unsigned integer'``: unsigned integer data types (i.e.,
265
+ ``uint8``, ``uint16``, ``uint32``, ``uint64``).
266
+ - ``'integral'``: integer data types. Shorthand for ``('signed
267
+ integer', 'unsigned integer')``.
268
+ - ``'real floating'``: real-valued floating-point data types
269
+ (i.e., ``float32``, ``float64``).
270
+ - ``'complex floating'``: complex floating-point data types (i.e.,
271
+ ``complex64``, ``complex128``).
272
+ - ``'numeric'``: numeric data types. Shorthand for ``('integral',
273
+ 'real floating', 'complex floating')``.
274
+
275
+ Returns
276
+ -------
277
+ dtypes : dict
278
+ A dictionary mapping the names of data types to the corresponding
279
+ PyTorch data types.
280
+
281
+ See Also
282
+ --------
283
+ __array_namespace_info__.capabilities,
284
+ __array_namespace_info__.default_device,
285
+ __array_namespace_info__.default_dtypes,
286
+ __array_namespace_info__.devices
287
+
288
+ Examples
289
+ --------
290
+ >>> info = np.__array_namespace_info__()
291
+ >>> info.dtypes(kind='signed integer')
292
+ {'int8': numpy.int8,
293
+ 'int16': numpy.int16,
294
+ 'int32': numpy.int32,
295
+ 'int64': numpy.int64}
296
+
297
+ """
298
+ res = self._dtypes(kind)
299
+ for k, v in res.copy().items():
300
+ try:
301
+ torch.empty((0,), dtype=v, device=device)
302
+ except:
303
+ del res[k]
304
+ return res
305
+
306
+ @cache
307
+ def devices(self):
308
+ """
309
+ The devices supported by PyTorch.
310
+
311
+ Returns
312
+ -------
313
+ devices : list of str
314
+ The devices supported by PyTorch.
315
+
316
+ See Also
317
+ --------
318
+ __array_namespace_info__.capabilities,
319
+ __array_namespace_info__.default_device,
320
+ __array_namespace_info__.default_dtypes,
321
+ __array_namespace_info__.dtypes
322
+
323
+ Examples
324
+ --------
325
+ >>> info = np.__array_namespace_info__()
326
+ >>> info.devices()
327
+ [device(type='cpu'), device(type='mps', index=0), device(type='meta')]
328
+
329
+ """
330
+ # Torch doesn't have a straightforward way to get the list of all
331
+ # currently supported devices. To do this, we first parse the error
332
+ # message of torch.device to get the list of all possible types of
333
+ # device:
334
+ try:
335
+ torch.device('notadevice')
336
+ except RuntimeError as e:
337
+ # The error message is something like:
338
+ # "Expected one of cpu, cuda, ipu, xpu, mkldnn, opengl, opencl, ideep, hip, ve, fpga, ort, xla, lazy, vulkan, mps, meta, hpu, mtia, privateuseone device type at start of device string: notadevice"
339
+ devices_names = e.args[0].split('Expected one of ')[1].split(' device type')[0].split(', ')
340
+
341
+ # Next we need to check for different indices for different devices.
342
+ # device(device_name, index=index) doesn't actually check if the
343
+ # device name or index is valid. We have to try to create a tensor
344
+ # with it (which is why this function is cached).
345
+ devices = []
346
+ for device_name in devices_names:
347
+ i = 0
348
+ while True:
349
+ try:
350
+ a = torch.empty((0,), device=torch.device(device_name, index=i))
351
+ if a.device in devices:
352
+ break
353
+ devices.append(a.device)
354
+ except:
355
+ break
356
+ i += 1
357
+
358
+ return devices
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/torch/fft.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING
4
+ if TYPE_CHECKING:
5
+ import torch
6
+ array = torch.Tensor
7
+ from typing import Union, Sequence, Literal
8
+
9
+ from torch.fft import * # noqa: F403
10
+ import torch.fft
11
+
12
+ # Several torch fft functions do not map axes to dim
13
+
14
+ def fftn(
15
+ x: array,
16
+ /,
17
+ *,
18
+ s: Sequence[int] = None,
19
+ axes: Sequence[int] = None,
20
+ norm: Literal["backward", "ortho", "forward"] = "backward",
21
+ **kwargs,
22
+ ) -> array:
23
+ return torch.fft.fftn(x, s=s, dim=axes, norm=norm, **kwargs)
24
+
25
+ def ifftn(
26
+ x: array,
27
+ /,
28
+ *,
29
+ s: Sequence[int] = None,
30
+ axes: Sequence[int] = None,
31
+ norm: Literal["backward", "ortho", "forward"] = "backward",
32
+ **kwargs,
33
+ ) -> array:
34
+ return torch.fft.ifftn(x, s=s, dim=axes, norm=norm, **kwargs)
35
+
36
+ def rfftn(
37
+ x: array,
38
+ /,
39
+ *,
40
+ s: Sequence[int] = None,
41
+ axes: Sequence[int] = None,
42
+ norm: Literal["backward", "ortho", "forward"] = "backward",
43
+ **kwargs,
44
+ ) -> array:
45
+ return torch.fft.rfftn(x, s=s, dim=axes, norm=norm, **kwargs)
46
+
47
+ def irfftn(
48
+ x: array,
49
+ /,
50
+ *,
51
+ s: Sequence[int] = None,
52
+ axes: Sequence[int] = None,
53
+ norm: Literal["backward", "ortho", "forward"] = "backward",
54
+ **kwargs,
55
+ ) -> array:
56
+ return torch.fft.irfftn(x, s=s, dim=axes, norm=norm, **kwargs)
57
+
58
+ def fftshift(
59
+ x: array,
60
+ /,
61
+ *,
62
+ axes: Union[int, Sequence[int]] = None,
63
+ **kwargs,
64
+ ) -> array:
65
+ return torch.fft.fftshift(x, dim=axes, **kwargs)
66
+
67
+ def ifftshift(
68
+ x: array,
69
+ /,
70
+ *,
71
+ axes: Union[int, Sequence[int]] = None,
72
+ **kwargs,
73
+ ) -> array:
74
+ return torch.fft.ifftshift(x, dim=axes, **kwargs)
75
+
76
+
77
+ __all__ = torch.fft.__all__ + [
78
+ "fftn",
79
+ "ifftn",
80
+ "rfftn",
81
+ "irfftn",
82
+ "fftshift",
83
+ "ifftshift",
84
+ ]
85
+
86
+ _all_ignore = ['torch']
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_compat/torch/linalg.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING
4
+ if TYPE_CHECKING:
5
+ import torch
6
+ array = torch.Tensor
7
+ from torch import dtype as Dtype
8
+ from typing import Optional, Union, Tuple, Literal
9
+ inf = float('inf')
10
+
11
+ from ._aliases import _fix_promotion, sum
12
+
13
+ from torch.linalg import * # noqa: F403
14
+
15
+ # torch.linalg doesn't define __all__
16
+ # from torch.linalg import __all__ as linalg_all
17
+ from torch import linalg as torch_linalg
18
+ linalg_all = [i for i in dir(torch_linalg) if not i.startswith('_')]
19
+
20
+ # outer is implemented in torch but aren't in the linalg namespace
21
+ from torch import outer
22
+ # These functions are in both the main and linalg namespaces
23
+ from ._aliases import matmul, matrix_transpose, tensordot
24
+
25
+ # Note: torch.linalg.cross does not default to axis=-1 (it defaults to the
26
+ # first axis with size 3), see https://github.com/pytorch/pytorch/issues/58743
27
+
28
+ # torch.cross also does not support broadcasting when it would add new
29
+ # dimensions https://github.com/pytorch/pytorch/issues/39656
30
+ def cross(x1: array, x2: array, /, *, axis: int = -1) -> array:
31
+ x1, x2 = _fix_promotion(x1, x2, only_scalar=False)
32
+ if not (-min(x1.ndim, x2.ndim) <= axis < max(x1.ndim, x2.ndim)):
33
+ raise ValueError(f"axis {axis} out of bounds for cross product of arrays with shapes {x1.shape} and {x2.shape}")
34
+ if not (x1.shape[axis] == x2.shape[axis] == 3):
35
+ raise ValueError(f"cross product axis must have size 3, got {x1.shape[axis]} and {x2.shape[axis]}")
36
+ x1, x2 = torch.broadcast_tensors(x1, x2)
37
+ return torch_linalg.cross(x1, x2, dim=axis)
38
+
39
+ def vecdot(x1: array, x2: array, /, *, axis: int = -1, **kwargs) -> array:
40
+ from ._aliases import isdtype
41
+
42
+ x1, x2 = _fix_promotion(x1, x2, only_scalar=False)
43
+
44
+ # torch.linalg.vecdot incorrectly allows broadcasting along the contracted dimension
45
+ if x1.shape[axis] != x2.shape[axis]:
46
+ raise ValueError("x1 and x2 must have the same size along the given axis")
47
+
48
+ # torch.linalg.vecdot doesn't support integer dtypes
49
+ if isdtype(x1.dtype, 'integral') or isdtype(x2.dtype, 'integral'):
50
+ if kwargs:
51
+ raise RuntimeError("vecdot kwargs not supported for integral dtypes")
52
+
53
+ x1_ = torch.moveaxis(x1, axis, -1)
54
+ x2_ = torch.moveaxis(x2, axis, -1)
55
+ x1_, x2_ = torch.broadcast_tensors(x1_, x2_)
56
+
57
+ res = x1_[..., None, :] @ x2_[..., None]
58
+ return res[..., 0, 0]
59
+ return torch.linalg.vecdot(x1, x2, dim=axis, **kwargs)
60
+
61
+ def solve(x1: array, x2: array, /, **kwargs) -> array:
62
+ x1, x2 = _fix_promotion(x1, x2, only_scalar=False)
63
+ # Torch tries to emulate NumPy 1 solve behavior by using batched 1-D solve
64
+ # whenever
65
+ # 1. x1.ndim - 1 == x2.ndim
66
+ # 2. x1.shape[:-1] == x2.shape
67
+ #
68
+ # See linalg_solve_is_vector_rhs in
69
+ # aten/src/ATen/native/LinearAlgebraUtils.h and
70
+ # TORCH_META_FUNC(_linalg_solve_ex) in
71
+ # aten/src/ATen/native/BatchLinearAlgebra.cpp in the PyTorch source code.
72
+ #
73
+ # The easiest way to work around this is to prepend a size 1 dimension to
74
+ # x2, since x2 is already one dimension less than x1.
75
+ #
76
+ # See https://github.com/pytorch/pytorch/issues/52915
77
+ if x2.ndim != 1 and x1.ndim - 1 == x2.ndim and x1.shape[:-1] == x2.shape:
78
+ x2 = x2[None]
79
+ return torch.linalg.solve(x1, x2, **kwargs)
80
+
81
+ # torch.trace doesn't support the offset argument and doesn't support stacking
82
+ def trace(x: array, /, *, offset: int = 0, dtype: Optional[Dtype] = None) -> array:
83
+ # Use our wrapped sum to make sure it does upcasting correctly
84
+ return sum(torch.diagonal(x, offset=offset, dim1=-2, dim2=-1), axis=-1, dtype=dtype)
85
+
86
+ def vector_norm(
87
+ x: array,
88
+ /,
89
+ *,
90
+ axis: Optional[Union[int, Tuple[int, ...]]] = None,
91
+ keepdims: bool = False,
92
+ ord: Union[int, float, Literal[inf, -inf]] = 2,
93
+ **kwargs,
94
+ ) -> array:
95
+ # torch.vector_norm incorrectly treats axis=() the same as axis=None
96
+ if axis == ():
97
+ out = kwargs.get('out')
98
+ if out is None:
99
+ dtype = None
100
+ if x.dtype == torch.complex64:
101
+ dtype = torch.float32
102
+ elif x.dtype == torch.complex128:
103
+ dtype = torch.float64
104
+
105
+ out = torch.zeros_like(x, dtype=dtype)
106
+
107
+ # The norm of a single scalar works out to abs(x) in every case except
108
+ # for ord=0, which is x != 0.
109
+ if ord == 0:
110
+ out[:] = (x != 0)
111
+ else:
112
+ out[:] = torch.abs(x)
113
+ return out
114
+ return torch.linalg.vector_norm(x, ord=ord, axis=axis, keepdim=keepdims, **kwargs)
115
+
116
+ __all__ = linalg_all + ['outer', 'matmul', 'matrix_transpose', 'tensordot',
117
+ 'cross', 'vecdot', 'solve', 'trace', 'vector_norm']
118
+
119
+ _all_ignore = ['torch_linalg', 'sum']
120
+
121
+ del linalg_all
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_extra/__init__.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from ._funcs import atleast_nd, cov, create_diagonal, expand_dims, kron, sinc
4
+
5
+ __version__ = "0.2.0"
6
+
7
+ __all__ = [
8
+ "__version__",
9
+ "atleast_nd",
10
+ "cov",
11
+ "create_diagonal",
12
+ "expand_dims",
13
+ "kron",
14
+ "sinc",
15
+ ]
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_extra/_funcs.py ADDED
@@ -0,0 +1,484 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import warnings
4
+ from typing import TYPE_CHECKING
5
+
6
+ if TYPE_CHECKING:
7
+ from ._typing import Array, ModuleType
8
+
9
+ __all__ = ["atleast_nd", "cov", "create_diagonal", "expand_dims", "kron", "sinc"]
10
+
11
+
12
+ def atleast_nd(x: Array, /, *, ndim: int, xp: ModuleType) -> Array:
13
+ """
14
+ Recursively expand the dimension of an array to at least `ndim`.
15
+
16
+ Parameters
17
+ ----------
18
+ x : array
19
+ ndim : int
20
+ The minimum number of dimensions for the result.
21
+ xp : array_namespace
22
+ The standard-compatible namespace for `x`.
23
+
24
+ Returns
25
+ -------
26
+ res : array
27
+ An array with ``res.ndim`` >= `ndim`.
28
+ If ``x.ndim`` >= `ndim`, `x` is returned.
29
+ If ``x.ndim`` < `ndim`, `x` is expanded by prepending new axes
30
+ until ``res.ndim`` equals `ndim`.
31
+
32
+ Examples
33
+ --------
34
+ >>> import array_api_strict as xp
35
+ >>> import array_api_extra as xpx
36
+ >>> x = xp.asarray([1])
37
+ >>> xpx.atleast_nd(x, ndim=3, xp=xp)
38
+ Array([[[1]]], dtype=array_api_strict.int64)
39
+
40
+ >>> x = xp.asarray([[[1, 2],
41
+ ... [3, 4]]])
42
+ >>> xpx.atleast_nd(x, ndim=1, xp=xp) is x
43
+ True
44
+
45
+ """
46
+ if x.ndim < ndim:
47
+ x = xp.expand_dims(x, axis=0)
48
+ x = atleast_nd(x, ndim=ndim, xp=xp)
49
+ return x
50
+
51
+
52
+ def cov(m: Array, /, *, xp: ModuleType) -> Array:
53
+ """
54
+ Estimate a covariance matrix.
55
+
56
+ Covariance indicates the level to which two variables vary together.
57
+ If we examine N-dimensional samples, :math:`X = [x_1, x_2, ... x_N]^T`,
58
+ then the covariance matrix element :math:`C_{ij}` is the covariance of
59
+ :math:`x_i` and :math:`x_j`. The element :math:`C_{ii}` is the variance
60
+ of :math:`x_i`.
61
+
62
+ This provides a subset of the functionality of ``numpy.cov``.
63
+
64
+ Parameters
65
+ ----------
66
+ m : array
67
+ A 1-D or 2-D array containing multiple variables and observations.
68
+ Each row of `m` represents a variable, and each column a single
69
+ observation of all those variables.
70
+ xp : array_namespace
71
+ The standard-compatible namespace for `m`.
72
+
73
+ Returns
74
+ -------
75
+ res : array
76
+ The covariance matrix of the variables.
77
+
78
+ Examples
79
+ --------
80
+ >>> import array_api_strict as xp
81
+ >>> import array_api_extra as xpx
82
+
83
+ Consider two variables, :math:`x_0` and :math:`x_1`, which
84
+ correlate perfectly, but in opposite directions:
85
+
86
+ >>> x = xp.asarray([[0, 2], [1, 1], [2, 0]]).T
87
+ >>> x
88
+ Array([[0, 1, 2],
89
+ [2, 1, 0]], dtype=array_api_strict.int64)
90
+
91
+ Note how :math:`x_0` increases while :math:`x_1` decreases. The covariance
92
+ matrix shows this clearly:
93
+
94
+ >>> xpx.cov(x, xp=xp)
95
+ Array([[ 1., -1.],
96
+ [-1., 1.]], dtype=array_api_strict.float64)
97
+
98
+
99
+ Note that element :math:`C_{0,1}`, which shows the correlation between
100
+ :math:`x_0` and :math:`x_1`, is negative.
101
+
102
+ Further, note how `x` and `y` are combined:
103
+
104
+ >>> x = xp.asarray([-2.1, -1, 4.3])
105
+ >>> y = xp.asarray([3, 1.1, 0.12])
106
+ >>> X = xp.stack((x, y), axis=0)
107
+ >>> xpx.cov(X, xp=xp)
108
+ Array([[11.71 , -4.286 ],
109
+ [-4.286 , 2.14413333]], dtype=array_api_strict.float64)
110
+
111
+ >>> xpx.cov(x, xp=xp)
112
+ Array(11.71, dtype=array_api_strict.float64)
113
+
114
+ >>> xpx.cov(y, xp=xp)
115
+ Array(2.14413333, dtype=array_api_strict.float64)
116
+
117
+ """
118
+ m = xp.asarray(m, copy=True)
119
+ dtype = (
120
+ xp.float64 if xp.isdtype(m.dtype, "integral") else xp.result_type(m, xp.float64)
121
+ )
122
+
123
+ m = atleast_nd(m, ndim=2, xp=xp)
124
+ m = xp.astype(m, dtype)
125
+
126
+ avg = _mean(m, axis=1, xp=xp)
127
+ fact = m.shape[1] - 1
128
+
129
+ if fact <= 0:
130
+ warnings.warn("Degrees of freedom <= 0 for slice", RuntimeWarning, stacklevel=2)
131
+ fact = 0.0
132
+
133
+ m -= avg[:, None]
134
+ m_transpose = m.T
135
+ if xp.isdtype(m_transpose.dtype, "complex floating"):
136
+ m_transpose = xp.conj(m_transpose)
137
+ c = m @ m_transpose
138
+ c /= fact
139
+ axes = tuple(axis for axis, length in enumerate(c.shape) if length == 1)
140
+ return xp.squeeze(c, axis=axes)
141
+
142
+
143
+ def create_diagonal(x: Array, /, *, offset: int = 0, xp: ModuleType) -> Array:
144
+ """
145
+ Construct a diagonal array.
146
+
147
+ Parameters
148
+ ----------
149
+ x : array
150
+ A 1-D array
151
+ offset : int, optional
152
+ Offset from the leading diagonal (default is ``0``).
153
+ Use positive ints for diagonals above the leading diagonal,
154
+ and negative ints for diagonals below the leading diagonal.
155
+ xp : array_namespace
156
+ The standard-compatible namespace for `x`.
157
+
158
+ Returns
159
+ -------
160
+ res : array
161
+ A 2-D array with `x` on the diagonal (offset by `offset`).
162
+
163
+ Examples
164
+ --------
165
+ >>> import array_api_strict as xp
166
+ >>> import array_api_extra as xpx
167
+ >>> x = xp.asarray([2, 4, 8])
168
+
169
+ >>> xpx.create_diagonal(x, xp=xp)
170
+ Array([[2, 0, 0],
171
+ [0, 4, 0],
172
+ [0, 0, 8]], dtype=array_api_strict.int64)
173
+
174
+ >>> xpx.create_diagonal(x, offset=-2, xp=xp)
175
+ Array([[0, 0, 0, 0, 0],
176
+ [0, 0, 0, 0, 0],
177
+ [2, 0, 0, 0, 0],
178
+ [0, 4, 0, 0, 0],
179
+ [0, 0, 8, 0, 0]], dtype=array_api_strict.int64)
180
+
181
+ """
182
+ if x.ndim != 1:
183
+ err_msg = "`x` must be 1-dimensional."
184
+ raise ValueError(err_msg)
185
+ n = x.shape[0] + abs(offset)
186
+ diag = xp.zeros(n**2, dtype=x.dtype)
187
+ i = offset if offset >= 0 else abs(offset) * n
188
+ diag[i : min(n * (n - offset), diag.shape[0]) : n + 1] = x
189
+ return xp.reshape(diag, (n, n))
190
+
191
+
192
+ def _mean(
193
+ x: Array,
194
+ /,
195
+ *,
196
+ axis: int | tuple[int, ...] | None = None,
197
+ keepdims: bool = False,
198
+ xp: ModuleType,
199
+ ) -> Array:
200
+ """
201
+ Complex mean, https://github.com/data-apis/array-api/issues/846.
202
+ """
203
+ if xp.isdtype(x.dtype, "complex floating"):
204
+ x_real = xp.real(x)
205
+ x_imag = xp.imag(x)
206
+ mean_real = xp.mean(x_real, axis=axis, keepdims=keepdims)
207
+ mean_imag = xp.mean(x_imag, axis=axis, keepdims=keepdims)
208
+ return mean_real + (mean_imag * xp.asarray(1j))
209
+ return xp.mean(x, axis=axis, keepdims=keepdims)
210
+
211
+
212
+ def expand_dims(
213
+ a: Array, /, *, axis: int | tuple[int, ...] = (0,), xp: ModuleType
214
+ ) -> Array:
215
+ """
216
+ Expand the shape of an array.
217
+
218
+ Insert (a) new axis/axes that will appear at the position(s) specified by
219
+ `axis` in the expanded array shape.
220
+
221
+ This is ``xp.expand_dims`` for `axis` an int *or a tuple of ints*.
222
+ Roughly equivalent to ``numpy.expand_dims`` for NumPy arrays.
223
+
224
+ Parameters
225
+ ----------
226
+ a : array
227
+ axis : int or tuple of ints, optional
228
+ Position(s) in the expanded axes where the new axis (or axes) is/are placed.
229
+ If multiple positions are provided, they should be unique (note that a position
230
+ given by a positive index could also be referred to by a negative index -
231
+ that will also result in an error).
232
+ Default: ``(0,)``.
233
+ xp : array_namespace
234
+ The standard-compatible namespace for `a`.
235
+
236
+ Returns
237
+ -------
238
+ res : array
239
+ `a` with an expanded shape.
240
+
241
+ Examples
242
+ --------
243
+ >>> import array_api_strict as xp
244
+ >>> import array_api_extra as xpx
245
+ >>> x = xp.asarray([1, 2])
246
+ >>> x.shape
247
+ (2,)
248
+
249
+ The following is equivalent to ``x[xp.newaxis, :]`` or ``x[xp.newaxis]``:
250
+
251
+ >>> y = xpx.expand_dims(x, axis=0, xp=xp)
252
+ >>> y
253
+ Array([[1, 2]], dtype=array_api_strict.int64)
254
+ >>> y.shape
255
+ (1, 2)
256
+
257
+ The following is equivalent to ``x[:, xp.newaxis]``:
258
+
259
+ >>> y = xpx.expand_dims(x, axis=1, xp=xp)
260
+ >>> y
261
+ Array([[1],
262
+ [2]], dtype=array_api_strict.int64)
263
+ >>> y.shape
264
+ (2, 1)
265
+
266
+ ``axis`` may also be a tuple:
267
+
268
+ >>> y = xpx.expand_dims(x, axis=(0, 1), xp=xp)
269
+ >>> y
270
+ Array([[[1, 2]]], dtype=array_api_strict.int64)
271
+
272
+ >>> y = xpx.expand_dims(x, axis=(2, 0), xp=xp)
273
+ >>> y
274
+ Array([[[1],
275
+ [2]]], dtype=array_api_strict.int64)
276
+
277
+ """
278
+ if not isinstance(axis, tuple):
279
+ axis = (axis,)
280
+ ndim = a.ndim + len(axis)
281
+ if axis != () and (min(axis) < -ndim or max(axis) >= ndim):
282
+ err_msg = (
283
+ f"a provided axis position is out of bounds for array of dimension {a.ndim}"
284
+ )
285
+ raise IndexError(err_msg)
286
+ axis = tuple(dim % ndim for dim in axis)
287
+ if len(set(axis)) != len(axis):
288
+ err_msg = "Duplicate dimensions specified in `axis`."
289
+ raise ValueError(err_msg)
290
+ for i in sorted(axis):
291
+ a = xp.expand_dims(a, axis=i)
292
+ return a
293
+
294
+
295
+ def kron(a: Array, b: Array, /, *, xp: ModuleType) -> Array:
296
+ """
297
+ Kronecker product of two arrays.
298
+
299
+ Computes the Kronecker product, a composite array made of blocks of the
300
+ second array scaled by the first.
301
+
302
+ Equivalent to ``numpy.kron`` for NumPy arrays.
303
+
304
+ Parameters
305
+ ----------
306
+ a, b : array
307
+ xp : array_namespace
308
+ The standard-compatible namespace for `a` and `b`.
309
+
310
+ Returns
311
+ -------
312
+ res : array
313
+ The Kronecker product of `a` and `b`.
314
+
315
+ Notes
316
+ -----
317
+ The function assumes that the number of dimensions of `a` and `b`
318
+ are the same, if necessary prepending the smallest with ones.
319
+ If ``a.shape = (r0,r1,..,rN)`` and ``b.shape = (s0,s1,...,sN)``,
320
+ the Kronecker product has shape ``(r0*s0, r1*s1, ..., rN*SN)``.
321
+ The elements are products of elements from `a` and `b`, organized
322
+ explicitly by::
323
+
324
+ kron(a,b)[k0,k1,...,kN] = a[i0,i1,...,iN] * b[j0,j1,...,jN]
325
+
326
+ where::
327
+
328
+ kt = it * st + jt, t = 0,...,N
329
+
330
+ In the common 2-D case (N=1), the block structure can be visualized::
331
+
332
+ [[ a[0,0]*b, a[0,1]*b, ... , a[0,-1]*b ],
333
+ [ ... ... ],
334
+ [ a[-1,0]*b, a[-1,1]*b, ... , a[-1,-1]*b ]]
335
+
336
+
337
+ Examples
338
+ --------
339
+ >>> import array_api_strict as xp
340
+ >>> import array_api_extra as xpx
341
+ >>> xpx.kron(xp.asarray([1, 10, 100]), xp.asarray([5, 6, 7]), xp=xp)
342
+ Array([ 5, 6, 7, 50, 60, 70, 500,
343
+ 600, 700], dtype=array_api_strict.int64)
344
+
345
+ >>> xpx.kron(xp.asarray([5, 6, 7]), xp.asarray([1, 10, 100]), xp=xp)
346
+ Array([ 5, 50, 500, 6, 60, 600, 7,
347
+ 70, 700], dtype=array_api_strict.int64)
348
+
349
+ >>> xpx.kron(xp.eye(2), xp.ones((2, 2)), xp=xp)
350
+ Array([[1., 1., 0., 0.],
351
+ [1., 1., 0., 0.],
352
+ [0., 0., 1., 1.],
353
+ [0., 0., 1., 1.]], dtype=array_api_strict.float64)
354
+
355
+
356
+ >>> a = xp.reshape(xp.arange(100), (2, 5, 2, 5))
357
+ >>> b = xp.reshape(xp.arange(24), (2, 3, 4))
358
+ >>> c = xpx.kron(a, b, xp=xp)
359
+ >>> c.shape
360
+ (2, 10, 6, 20)
361
+ >>> I = (1, 3, 0, 2)
362
+ >>> J = (0, 2, 1)
363
+ >>> J1 = (0,) + J # extend to ndim=4
364
+ >>> S1 = (1,) + b.shape
365
+ >>> K = tuple(xp.asarray(I) * xp.asarray(S1) + xp.asarray(J1))
366
+ >>> c[K] == a[I]*b[J]
367
+ Array(True, dtype=array_api_strict.bool)
368
+
369
+ """
370
+
371
+ b = xp.asarray(b)
372
+ singletons = (1,) * (b.ndim - a.ndim)
373
+ a = xp.broadcast_to(xp.asarray(a), singletons + a.shape)
374
+
375
+ nd_b, nd_a = b.ndim, a.ndim
376
+ nd_max = max(nd_b, nd_a)
377
+ if nd_a == 0 or nd_b == 0:
378
+ return xp.multiply(a, b)
379
+
380
+ a_shape = a.shape
381
+ b_shape = b.shape
382
+
383
+ # Equalise the shapes by prepending smaller one with 1s
384
+ a_shape = (1,) * max(0, nd_b - nd_a) + a_shape
385
+ b_shape = (1,) * max(0, nd_a - nd_b) + b_shape
386
+
387
+ # Insert empty dimensions
388
+ a_arr = expand_dims(a, axis=tuple(range(nd_b - nd_a)), xp=xp)
389
+ b_arr = expand_dims(b, axis=tuple(range(nd_a - nd_b)), xp=xp)
390
+
391
+ # Compute the product
392
+ a_arr = expand_dims(a_arr, axis=tuple(range(1, nd_max * 2, 2)), xp=xp)
393
+ b_arr = expand_dims(b_arr, axis=tuple(range(0, nd_max * 2, 2)), xp=xp)
394
+ result = xp.multiply(a_arr, b_arr)
395
+
396
+ # Reshape back and return
397
+ a_shape = xp.asarray(a_shape)
398
+ b_shape = xp.asarray(b_shape)
399
+ return xp.reshape(result, tuple(xp.multiply(a_shape, b_shape)))
400
+
401
+
402
+ def sinc(x: Array, /, *, xp: ModuleType) -> Array:
403
+ r"""
404
+ Return the normalized sinc function.
405
+
406
+ The sinc function is equal to :math:`\sin(\pi x)/(\pi x)` for any argument
407
+ :math:`x\ne 0`. ``sinc(0)`` takes the limit value 1, making ``sinc`` not
408
+ only everywhere continuous but also infinitely differentiable.
409
+
410
+ .. note::
411
+
412
+ Note the normalization factor of ``pi`` used in the definition.
413
+ This is the most commonly used definition in signal processing.
414
+ Use ``sinc(x / xp.pi)`` to obtain the unnormalized sinc function
415
+ :math:`\sin(x)/x` that is more common in mathematics.
416
+
417
+ Parameters
418
+ ----------
419
+ x : array
420
+ Array (possibly multi-dimensional) of values for which to calculate
421
+ ``sinc(x)``. Must have a real floating point dtype.
422
+ xp : array_namespace
423
+ The standard-compatible namespace for `x`.
424
+
425
+ Returns
426
+ -------
427
+ res : array
428
+ ``sinc(x)`` calculated elementwise, which has the same shape as the input.
429
+
430
+ Notes
431
+ -----
432
+ The name sinc is short for "sine cardinal" or "sinus cardinalis".
433
+
434
+ The sinc function is used in various signal processing applications,
435
+ including in anti-aliasing, in the construction of a Lanczos resampling
436
+ filter, and in interpolation.
437
+
438
+ For bandlimited interpolation of discrete-time signals, the ideal
439
+ interpolation kernel is proportional to the sinc function.
440
+
441
+ References
442
+ ----------
443
+ .. [1] Weisstein, Eric W. "Sinc Function." From MathWorld--A Wolfram Web
444
+ Resource. https://mathworld.wolfram.com/SincFunction.html
445
+ .. [2] Wikipedia, "Sinc function",
446
+ https://en.wikipedia.org/wiki/Sinc_function
447
+
448
+ Examples
449
+ --------
450
+ >>> import array_api_strict as xp
451
+ >>> import array_api_extra as xpx
452
+ >>> x = xp.linspace(-4, 4, 41)
453
+ >>> xpx.sinc(x, xp=xp)
454
+ Array([-3.89817183e-17, -4.92362781e-02,
455
+ -8.40918587e-02, -8.90384387e-02,
456
+ -5.84680802e-02, 3.89817183e-17,
457
+ 6.68206631e-02, 1.16434881e-01,
458
+ 1.26137788e-01, 8.50444803e-02,
459
+ -3.89817183e-17, -1.03943254e-01,
460
+ -1.89206682e-01, -2.16236208e-01,
461
+ -1.55914881e-01, 3.89817183e-17,
462
+ 2.33872321e-01, 5.04551152e-01,
463
+ 7.56826729e-01, 9.35489284e-01,
464
+ 1.00000000e+00, 9.35489284e-01,
465
+ 7.56826729e-01, 5.04551152e-01,
466
+ 2.33872321e-01, 3.89817183e-17,
467
+ -1.55914881e-01, -2.16236208e-01,
468
+ -1.89206682e-01, -1.03943254e-01,
469
+ -3.89817183e-17, 8.50444803e-02,
470
+ 1.26137788e-01, 1.16434881e-01,
471
+ 6.68206631e-02, 3.89817183e-17,
472
+ -5.84680802e-02, -8.90384387e-02,
473
+ -8.40918587e-02, -4.92362781e-02,
474
+ -3.89817183e-17], dtype=array_api_strict.float64)
475
+
476
+ """
477
+ if not xp.isdtype(x.dtype, "real floating"):
478
+ err_msg = "`x` must have a real floating data type."
479
+ raise ValueError(err_msg)
480
+ # no scalars in `where` - array-api#807
481
+ y = xp.pi * xp.where(
482
+ x, x, xp.asarray(xp.finfo(x.dtype).smallest_normal, dtype=x.dtype)
483
+ )
484
+ return xp.sin(y) / y
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/array_api_extra/_typing.py ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from types import ModuleType
4
+ from typing import Any
5
+
6
+ Array = Any # To be changed to a Protocol later (see array-api#589)
7
+
8
+ __all__ = ["Array", "ModuleType"]
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/__init__.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from .main import minimize
2
+ from .utils import show_versions
3
+
4
+ # PEP0440 compatible formatted version, see:
5
+ # https://www.python.org/dev/peps/pep-0440/
6
+ #
7
+ # Final release markers:
8
+ # X.Y.0 # For first release after an increment in Y
9
+ # X.Y.Z # For bugfix releases
10
+ #
11
+ # Admissible pre-release markers:
12
+ # X.YaN # Alpha release
13
+ # X.YbN # Beta release
14
+ # X.YrcN # Release Candidate
15
+ #
16
+ # Dev branch marker is: 'X.Y.dev' or 'X.Y.devN' where N is an integer.
17
+ # 'X.Y.dev0' is the canonical version of 'X.Y.dev'.
18
+ __version__ = "1.1.2"
19
+
20
+ __all__ = ["minimize", "show_versions"]
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/framework.py ADDED
@@ -0,0 +1,1240 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import warnings
2
+
3
+ import numpy as np
4
+ from scipy.optimize import lsq_linear
5
+
6
+ from .models import Models, Quadratic
7
+ from .settings import Options, Constants
8
+ from .subsolvers import (
9
+ cauchy_geometry,
10
+ spider_geometry,
11
+ normal_byrd_omojokun,
12
+ tangential_byrd_omojokun,
13
+ constrained_tangential_byrd_omojokun,
14
+ )
15
+ from .subsolvers.optim import qr_tangential_byrd_omojokun
16
+ from .utils import get_arrays_tol
17
+
18
+
19
+ TINY = np.finfo(float).tiny
20
+ EPS = np.finfo(float).eps
21
+
22
+
23
+ class TrustRegion:
24
+ """
25
+ Trust-region framework.
26
+ """
27
+
28
+ def __init__(self, pb, options, constants):
29
+ """
30
+ Initialize the trust-region framework.
31
+
32
+ Parameters
33
+ ----------
34
+ pb : `cobyqa.problem.Problem`
35
+ Problem to solve.
36
+ options : dict
37
+ Options of the solver.
38
+ constants : dict
39
+ Constants of the solver.
40
+
41
+ Raises
42
+ ------
43
+ `cobyqa.utils.MaxEvalError`
44
+ If the maximum number of evaluations is reached.
45
+ `cobyqa.utils.TargetSuccess`
46
+ If a nearly feasible point has been found with an objective
47
+ function value below the target.
48
+ `cobyqa.utils.FeasibleSuccess`
49
+ If a feasible point has been found for a feasibility problem.
50
+ `numpy.linalg.LinAlgError`
51
+ If the initial interpolation system is ill-defined.
52
+ """
53
+ # Set the initial penalty parameter.
54
+ self._penalty = 0.0
55
+
56
+ # Initialize the models.
57
+ self._pb = pb
58
+ self._models = Models(self._pb, options, self.penalty)
59
+ self._constants = constants
60
+
61
+ # Set the index of the best interpolation point.
62
+ self._best_index = 0
63
+ self.set_best_index()
64
+
65
+ # Set the initial Lagrange multipliers.
66
+ self._lm_linear_ub = np.zeros(self.m_linear_ub)
67
+ self._lm_linear_eq = np.zeros(self.m_linear_eq)
68
+ self._lm_nonlinear_ub = np.zeros(self.m_nonlinear_ub)
69
+ self._lm_nonlinear_eq = np.zeros(self.m_nonlinear_eq)
70
+ self.set_multipliers(self.x_best)
71
+
72
+ # Set the initial trust-region radius and the resolution.
73
+ self._resolution = options[Options.RHOBEG]
74
+ self._radius = self.resolution
75
+
76
+ @property
77
+ def n(self):
78
+ """
79
+ Number of variables.
80
+
81
+ Returns
82
+ -------
83
+ int
84
+ Number of variables.
85
+ """
86
+ return self._pb.n
87
+
88
+ @property
89
+ def m_linear_ub(self):
90
+ """
91
+ Number of linear inequality constraints.
92
+
93
+ Returns
94
+ -------
95
+ int
96
+ Number of linear inequality constraints.
97
+ """
98
+ return self._pb.m_linear_ub
99
+
100
+ @property
101
+ def m_linear_eq(self):
102
+ """
103
+ Number of linear equality constraints.
104
+
105
+ Returns
106
+ -------
107
+ int
108
+ Number of linear equality constraints.
109
+ """
110
+ return self._pb.m_linear_eq
111
+
112
+ @property
113
+ def m_nonlinear_ub(self):
114
+ """
115
+ Number of nonlinear inequality constraints.
116
+
117
+ Returns
118
+ -------
119
+ int
120
+ Number of nonlinear inequality constraints.
121
+ """
122
+ return self._pb.m_nonlinear_ub
123
+
124
+ @property
125
+ def m_nonlinear_eq(self):
126
+ """
127
+ Number of nonlinear equality constraints.
128
+
129
+ Returns
130
+ -------
131
+ int
132
+ Number of nonlinear equality constraints.
133
+ """
134
+ return self._pb.m_nonlinear_eq
135
+
136
+ @property
137
+ def radius(self):
138
+ """
139
+ Trust-region radius.
140
+
141
+ Returns
142
+ -------
143
+ float
144
+ Trust-region radius.
145
+ """
146
+ return self._radius
147
+
148
+ @radius.setter
149
+ def radius(self, radius):
150
+ """
151
+ Set the trust-region radius.
152
+
153
+ Parameters
154
+ ----------
155
+ radius : float
156
+ New trust-region radius.
157
+ """
158
+ self._radius = radius
159
+ if (
160
+ self.radius
161
+ <= self._constants[Constants.DECREASE_RADIUS_THRESHOLD]
162
+ * self.resolution
163
+ ):
164
+ self._radius = self.resolution
165
+
166
+ @property
167
+ def resolution(self):
168
+ """
169
+ Resolution of the trust-region framework.
170
+
171
+ The resolution is a lower bound on the trust-region radius.
172
+
173
+ Returns
174
+ -------
175
+ float
176
+ Resolution of the trust-region framework.
177
+ """
178
+ return self._resolution
179
+
180
+ @resolution.setter
181
+ def resolution(self, resolution):
182
+ """
183
+ Set the resolution of the trust-region framework.
184
+
185
+ Parameters
186
+ ----------
187
+ resolution : float
188
+ New resolution of the trust-region framework.
189
+ """
190
+ self._resolution = resolution
191
+
192
+ @property
193
+ def penalty(self):
194
+ """
195
+ Penalty parameter.
196
+
197
+ Returns
198
+ -------
199
+ float
200
+ Penalty parameter.
201
+ """
202
+ return self._penalty
203
+
204
+ @property
205
+ def models(self):
206
+ """
207
+ Models of the objective function and constraints.
208
+
209
+ Returns
210
+ -------
211
+ `cobyqa.models.Models`
212
+ Models of the objective function and constraints.
213
+ """
214
+ return self._models
215
+
216
+ @property
217
+ def best_index(self):
218
+ """
219
+ Index of the best interpolation point.
220
+
221
+ Returns
222
+ -------
223
+ int
224
+ Index of the best interpolation point.
225
+ """
226
+ return self._best_index
227
+
228
+ @property
229
+ def x_best(self):
230
+ """
231
+ Best interpolation point.
232
+
233
+ Its value is interpreted as relative to the origin, not the base point.
234
+
235
+ Returns
236
+ -------
237
+ `numpy.ndarray`
238
+ Best interpolation point.
239
+ """
240
+ return self.models.interpolation.point(self.best_index)
241
+
242
+ @property
243
+ def fun_best(self):
244
+ """
245
+ Value of the objective function at `x_best`.
246
+
247
+ Returns
248
+ -------
249
+ float
250
+ Value of the objective function at `x_best`.
251
+ """
252
+ return self.models.fun_val[self.best_index]
253
+
254
+ @property
255
+ def cub_best(self):
256
+ """
257
+ Values of the nonlinear inequality constraints at `x_best`.
258
+
259
+ Returns
260
+ -------
261
+ `numpy.ndarray`, shape (m_nonlinear_ub,)
262
+ Values of the nonlinear inequality constraints at `x_best`.
263
+ """
264
+ return self.models.cub_val[self.best_index, :]
265
+
266
+ @property
267
+ def ceq_best(self):
268
+ """
269
+ Values of the nonlinear equality constraints at `x_best`.
270
+
271
+ Returns
272
+ -------
273
+ `numpy.ndarray`, shape (m_nonlinear_eq,)
274
+ Values of the nonlinear equality constraints at `x_best`.
275
+ """
276
+ return self.models.ceq_val[self.best_index, :]
277
+
278
+ def lag_model(self, x):
279
+ """
280
+ Evaluate the Lagrangian model at a given point.
281
+
282
+ Parameters
283
+ ----------
284
+ x : `numpy.ndarray`, shape (n,)
285
+ Point at which the Lagrangian model is evaluated.
286
+
287
+ Returns
288
+ -------
289
+ float
290
+ Value of the Lagrangian model at `x`.
291
+ """
292
+ return (
293
+ self.models.fun(x)
294
+ + self._lm_linear_ub
295
+ @ (self._pb.linear.a_ub @ x - self._pb.linear.b_ub)
296
+ + self._lm_linear_eq
297
+ @ (self._pb.linear.a_eq @ x - self._pb.linear.b_eq)
298
+ + self._lm_nonlinear_ub @ self.models.cub(x)
299
+ + self._lm_nonlinear_eq @ self.models.ceq(x)
300
+ )
301
+
302
+ def lag_model_grad(self, x):
303
+ """
304
+ Evaluate the gradient of the Lagrangian model at a given point.
305
+
306
+ Parameters
307
+ ----------
308
+ x : `numpy.ndarray`, shape (n,)
309
+ Point at which the gradient of the Lagrangian model is evaluated.
310
+
311
+ Returns
312
+ -------
313
+ `numpy.ndarray`, shape (n,)
314
+ Gradient of the Lagrangian model at `x`.
315
+ """
316
+ return (
317
+ self.models.fun_grad(x)
318
+ + self._lm_linear_ub @ self._pb.linear.a_ub
319
+ + self._lm_linear_eq @ self._pb.linear.a_eq
320
+ + self._lm_nonlinear_ub @ self.models.cub_grad(x)
321
+ + self._lm_nonlinear_eq @ self.models.ceq_grad(x)
322
+ )
323
+
324
+ def lag_model_hess(self):
325
+ """
326
+ Evaluate the Hessian matrix of the Lagrangian model at a given point.
327
+
328
+ Returns
329
+ -------
330
+ `numpy.ndarray`, shape (n, n)
331
+ Hessian matrix of the Lagrangian model at `x`.
332
+ """
333
+ hess = self.models.fun_hess()
334
+ if self.m_nonlinear_ub > 0:
335
+ hess += self._lm_nonlinear_ub @ self.models.cub_hess()
336
+ if self.m_nonlinear_eq > 0:
337
+ hess += self._lm_nonlinear_eq @ self.models.ceq_hess()
338
+ return hess
339
+
340
+ def lag_model_hess_prod(self, v):
341
+ """
342
+ Evaluate the right product of the Hessian matrix of the Lagrangian
343
+ model with a given vector.
344
+
345
+ Parameters
346
+ ----------
347
+ v : `numpy.ndarray`, shape (n,)
348
+ Vector with which the Hessian matrix of the Lagrangian model is
349
+ multiplied from the right.
350
+
351
+ Returns
352
+ -------
353
+ `numpy.ndarray`, shape (n,)
354
+ Right product of the Hessian matrix of the Lagrangian model with
355
+ `v`.
356
+ """
357
+ return (
358
+ self.models.fun_hess_prod(v)
359
+ + self._lm_nonlinear_ub @ self.models.cub_hess_prod(v)
360
+ + self._lm_nonlinear_eq @ self.models.ceq_hess_prod(v)
361
+ )
362
+
363
+ def lag_model_curv(self, v):
364
+ """
365
+ Evaluate the curvature of the Lagrangian model along a given direction.
366
+
367
+ Parameters
368
+ ----------
369
+ v : `numpy.ndarray`, shape (n,)
370
+ Direction along which the curvature of the Lagrangian model is
371
+ evaluated.
372
+
373
+ Returns
374
+ -------
375
+ float
376
+ Curvature of the Lagrangian model along `v`.
377
+ """
378
+ return (
379
+ self.models.fun_curv(v)
380
+ + self._lm_nonlinear_ub @ self.models.cub_curv(v)
381
+ + self._lm_nonlinear_eq @ self.models.ceq_curv(v)
382
+ )
383
+
384
+ def sqp_fun(self, step):
385
+ """
386
+ Evaluate the objective function of the SQP subproblem.
387
+
388
+ Parameters
389
+ ----------
390
+ step : `numpy.ndarray`, shape (n,)
391
+ Step along which the objective function of the SQP subproblem is
392
+ evaluated.
393
+
394
+ Returns
395
+ -------
396
+ float
397
+ Value of the objective function of the SQP subproblem along `step`.
398
+ """
399
+ return step @ (
400
+ self.models.fun_grad(self.x_best)
401
+ + 0.5 * self.lag_model_hess_prod(step)
402
+ )
403
+
404
+ def sqp_cub(self, step):
405
+ """
406
+ Evaluate the linearization of the nonlinear inequality constraints.
407
+
408
+ Parameters
409
+ ----------
410
+ step : `numpy.ndarray`, shape (n,)
411
+ Step along which the linearization of the nonlinear inequality
412
+ constraints is evaluated.
413
+
414
+ Returns
415
+ -------
416
+ `numpy.ndarray`, shape (m_nonlinear_ub,)
417
+ Value of the linearization of the nonlinear inequality constraints
418
+ along `step`.
419
+ """
420
+ return (
421
+ self.models.cub(self.x_best)
422
+ + self.models.cub_grad(self.x_best) @ step
423
+ )
424
+
425
+ def sqp_ceq(self, step):
426
+ """
427
+ Evaluate the linearization of the nonlinear equality constraints.
428
+
429
+ Parameters
430
+ ----------
431
+ step : `numpy.ndarray`, shape (n,)
432
+ Step along which the linearization of the nonlinear equality
433
+ constraints is evaluated.
434
+
435
+ Returns
436
+ -------
437
+ `numpy.ndarray`, shape (m_nonlinear_ub,)
438
+ Value of the linearization of the nonlinear equality constraints
439
+ along `step`.
440
+ """
441
+ return (
442
+ self.models.ceq(self.x_best)
443
+ + self.models.ceq_grad(self.x_best) @ step
444
+ )
445
+
446
+ def merit(self, x, fun_val=None, cub_val=None, ceq_val=None):
447
+ """
448
+ Evaluate the merit function at a given point.
449
+
450
+ Parameters
451
+ ----------
452
+ x : `numpy.ndarray`, shape (n,)
453
+ Point at which the merit function is evaluated.
454
+ fun_val : float, optional
455
+ Value of the objective function at `x`. If not provided, the
456
+ objective function is evaluated at `x`.
457
+ cub_val : `numpy.ndarray`, shape (m_nonlinear_ub,), optional
458
+ Values of the nonlinear inequality constraints. If not provided,
459
+ the nonlinear inequality constraints are evaluated at `x`.
460
+ ceq_val : `numpy.ndarray`, shape (m_nonlinear_eq,), optional
461
+ Values of the nonlinear equality constraints. If not provided,
462
+ the nonlinear equality constraints are evaluated at `x`.
463
+
464
+ Returns
465
+ -------
466
+ float
467
+ Value of the merit function at `x`.
468
+ """
469
+ if fun_val is None or cub_val is None or ceq_val is None:
470
+ fun_val, cub_val, ceq_val = self._pb(x, self.penalty)
471
+ m_val = fun_val
472
+ if self._penalty > 0.0:
473
+ c_val = self._pb.violation(x, cub_val=cub_val, ceq_val=ceq_val)
474
+ if np.count_nonzero(c_val):
475
+ m_val += self._penalty * np.linalg.norm(c_val)
476
+ return m_val
477
+
478
+ def get_constraint_linearizations(self, x):
479
+ """
480
+ Get the linearizations of the constraints at a given point.
481
+
482
+ Parameters
483
+ ----------
484
+ x : `numpy.ndarray`, shape (n,)
485
+ Point at which the linearizations of the constraints are evaluated.
486
+
487
+ Returns
488
+ -------
489
+ `numpy.ndarray`, shape (m_linear_ub + m_nonlinear_ub, n)
490
+ Left-hand side matrix of the linearized inequality constraints.
491
+ `numpy.ndarray`, shape (m_linear_ub + m_nonlinear_ub,)
492
+ Right-hand side vector of the linearized inequality constraints.
493
+ `numpy.ndarray`, shape (m_linear_eq + m_nonlinear_eq, n)
494
+ Left-hand side matrix of the linearized equality constraints.
495
+ `numpy.ndarray`, shape (m_linear_eq + m_nonlinear_eq,)
496
+ Right-hand side vector of the linearized equality constraints.
497
+ """
498
+ aub = np.block(
499
+ [
500
+ [self._pb.linear.a_ub],
501
+ [self.models.cub_grad(x)],
502
+ ]
503
+ )
504
+ bub = np.block(
505
+ [
506
+ self._pb.linear.b_ub - self._pb.linear.a_ub @ x,
507
+ -self.models.cub(x),
508
+ ]
509
+ )
510
+ aeq = np.block(
511
+ [
512
+ [self._pb.linear.a_eq],
513
+ [self.models.ceq_grad(x)],
514
+ ]
515
+ )
516
+ beq = np.block(
517
+ [
518
+ self._pb.linear.b_eq - self._pb.linear.a_eq @ x,
519
+ -self.models.ceq(x),
520
+ ]
521
+ )
522
+ return aub, bub, aeq, beq
523
+
524
+ def get_trust_region_step(self, options):
525
+ """
526
+ Get the trust-region step.
527
+
528
+ The trust-region step is computed by solving the derivative-free
529
+ trust-region SQP subproblem using a Byrd-Omojokun composite-step
530
+ approach. For more details, see Section 5.2.3 of [1]_.
531
+
532
+ Parameters
533
+ ----------
534
+ options : dict
535
+ Options of the solver.
536
+
537
+ Returns
538
+ -------
539
+ `numpy.ndarray`, shape (n,)
540
+ Normal step.
541
+ `numpy.ndarray`, shape (n,)
542
+ Tangential step.
543
+
544
+ References
545
+ ----------
546
+ .. [1] T. M. Ragonneau. *Model-Based Derivative-Free Optimization
547
+ Methods and Software*. PhD thesis, Department of Applied
548
+ Mathematics, The Hong Kong Polytechnic University, Hong Kong, China,
549
+ 2022. URL: https://theses.lib.polyu.edu.hk/handle/200/12294.
550
+ """
551
+ # Evaluate the linearizations of the constraints.
552
+ aub, bub, aeq, beq = self.get_constraint_linearizations(self.x_best)
553
+ xl = self._pb.bounds.xl - self.x_best
554
+ xu = self._pb.bounds.xu - self.x_best
555
+
556
+ # Evaluate the normal step.
557
+ radius = self._constants[Constants.BYRD_OMOJOKUN_FACTOR] * self.radius
558
+ normal_step = normal_byrd_omojokun(
559
+ aub,
560
+ bub,
561
+ aeq,
562
+ beq,
563
+ xl,
564
+ xu,
565
+ radius,
566
+ options[Options.DEBUG],
567
+ **self._constants,
568
+ )
569
+ if options[Options.DEBUG]:
570
+ tol = get_arrays_tol(xl, xu)
571
+ if (np.any(normal_step + tol < xl)
572
+ or np.any(xu < normal_step - tol)):
573
+ warnings.warn(
574
+ "the normal step does not respect the bound constraint.",
575
+ RuntimeWarning,
576
+ 2,
577
+ )
578
+ if np.linalg.norm(normal_step) > 1.1 * radius:
579
+ warnings.warn(
580
+ "the normal step does not respect the trust-region "
581
+ "constraint.",
582
+ RuntimeWarning,
583
+ 2,
584
+ )
585
+
586
+ # Evaluate the tangential step.
587
+ radius = np.sqrt(self.radius**2.0 - normal_step @ normal_step)
588
+ xl -= normal_step
589
+ xu -= normal_step
590
+ bub = np.maximum(bub - aub @ normal_step, 0.0)
591
+ g_best = self.models.fun_grad(self.x_best) + self.lag_model_hess_prod(
592
+ normal_step
593
+ )
594
+ if self._pb.type in ["unconstrained", "bound-constrained"]:
595
+ tangential_step = tangential_byrd_omojokun(
596
+ g_best,
597
+ self.lag_model_hess_prod,
598
+ xl,
599
+ xu,
600
+ radius,
601
+ options[Options.DEBUG],
602
+ **self._constants,
603
+ )
604
+ else:
605
+ tangential_step = constrained_tangential_byrd_omojokun(
606
+ g_best,
607
+ self.lag_model_hess_prod,
608
+ xl,
609
+ xu,
610
+ aub,
611
+ bub,
612
+ aeq,
613
+ radius,
614
+ options["debug"],
615
+ **self._constants,
616
+ )
617
+ if options[Options.DEBUG]:
618
+ tol = get_arrays_tol(xl, xu)
619
+ if np.any(tangential_step + tol < xl) or np.any(
620
+ xu < tangential_step - tol
621
+ ):
622
+ warnings.warn(
623
+ "The tangential step does not respect the bound "
624
+ "constraints.",
625
+ RuntimeWarning,
626
+ 2,
627
+ )
628
+ if (
629
+ np.linalg.norm(normal_step + tangential_step)
630
+ > 1.1 * np.sqrt(2.0) * self.radius
631
+ ):
632
+ warnings.warn(
633
+ "The trial step does not respect the trust-region "
634
+ "constraint.",
635
+ RuntimeWarning,
636
+ 2,
637
+ )
638
+ return normal_step, tangential_step
639
+
640
+ def get_geometry_step(self, k_new, options):
641
+ """
642
+ Get the geometry-improving step.
643
+
644
+ Three different geometry-improving steps are computed and the best one
645
+ is returned. For more details, see Section 5.2.7 of [1]_.
646
+
647
+ Parameters
648
+ ----------
649
+ k_new : int
650
+ Index of the interpolation point to be modified.
651
+ options : dict
652
+ Options of the solver.
653
+
654
+ Returns
655
+ -------
656
+ `numpy.ndarray`, shape (n,)
657
+ Geometry-improving step.
658
+
659
+ Raises
660
+ ------
661
+ `numpy.linalg.LinAlgError`
662
+ If the computation of a determinant fails.
663
+
664
+ References
665
+ ----------
666
+ .. [1] T. M. Ragonneau. *Model-Based Derivative-Free Optimization
667
+ Methods and Software*. PhD thesis, Department of Applied
668
+ Mathematics, The Hong Kong Polytechnic University, Hong Kong, China,
669
+ 2022. URL: https://theses.lib.polyu.edu.hk/handle/200/12294.
670
+ """
671
+ if options[Options.DEBUG]:
672
+ assert (
673
+ k_new != self.best_index
674
+ ), "The index `k_new` must be different from the best index."
675
+
676
+ # Build the k_new-th Lagrange polynomial.
677
+ coord_vec = np.squeeze(np.eye(1, self.models.npt, k_new))
678
+ lag = Quadratic(
679
+ self.models.interpolation,
680
+ coord_vec,
681
+ options[Options.DEBUG],
682
+ )
683
+ g_lag = lag.grad(self.x_best, self.models.interpolation)
684
+
685
+ # Compute a simple constrained Cauchy step.
686
+ xl = self._pb.bounds.xl - self.x_best
687
+ xu = self._pb.bounds.xu - self.x_best
688
+ step = cauchy_geometry(
689
+ 0.0,
690
+ g_lag,
691
+ lambda v: lag.curv(v, self.models.interpolation),
692
+ xl,
693
+ xu,
694
+ self.radius,
695
+ options[Options.DEBUG],
696
+ )
697
+ sigma = self.models.determinants(self.x_best + step, k_new)
698
+
699
+ # Compute the solution on the straight lines joining the interpolation
700
+ # points to the k-th one, and choose it if it provides a larger value
701
+ # of the determinant of the interpolation system in absolute value.
702
+ xpt = (
703
+ self.models.interpolation.xpt
704
+ - self.models.interpolation.xpt[:, self.best_index, np.newaxis]
705
+ )
706
+ xpt[:, [0, self.best_index]] = xpt[:, [self.best_index, 0]]
707
+ step_alt = spider_geometry(
708
+ 0.0,
709
+ g_lag,
710
+ lambda v: lag.curv(v, self.models.interpolation),
711
+ xpt[:, 1:],
712
+ xl,
713
+ xu,
714
+ self.radius,
715
+ options[Options.DEBUG],
716
+ )
717
+ sigma_alt = self.models.determinants(self.x_best + step_alt, k_new)
718
+ if abs(sigma_alt) > abs(sigma):
719
+ step = step_alt
720
+ sigma = sigma_alt
721
+
722
+ # Compute a Cauchy step on the tangent space of the active constraints.
723
+ if self._pb.type in [
724
+ "linearly constrained",
725
+ "nonlinearly constrained",
726
+ ]:
727
+ aub, bub, aeq, beq = (
728
+ self.get_constraint_linearizations(self.x_best))
729
+ tol_bd = get_arrays_tol(xl, xu)
730
+ tol_ub = get_arrays_tol(bub)
731
+ free_xl = xl <= -tol_bd
732
+ free_xu = xu >= tol_bd
733
+ free_ub = bub >= tol_ub
734
+
735
+ # Compute the Cauchy step.
736
+ n_act, q = qr_tangential_byrd_omojokun(
737
+ aub,
738
+ aeq,
739
+ free_xl,
740
+ free_xu,
741
+ free_ub,
742
+ )
743
+ g_lag_proj = q[:, n_act:] @ (q[:, n_act:].T @ g_lag)
744
+ norm_g_lag_proj = np.linalg.norm(g_lag_proj)
745
+ if 0 < n_act < self._pb.n and norm_g_lag_proj > TINY * self.radius:
746
+ step_alt = (self.radius / norm_g_lag_proj) * g_lag_proj
747
+ if lag.curv(step_alt, self.models.interpolation) < 0.0:
748
+ step_alt = -step_alt
749
+
750
+ # Evaluate the constraint violation at the Cauchy step.
751
+ cbd = np.block([xl - step_alt, step_alt - xu])
752
+ cub = aub @ step_alt - bub
753
+ ceq = aeq @ step_alt - beq
754
+ maxcv_val = max(
755
+ np.max(array, initial=0.0)
756
+ for array in [cbd, cub, np.abs(ceq)]
757
+ )
758
+
759
+ # Accept the new step if it is nearly feasible and do not
760
+ # drastically worsen the determinant of the interpolation
761
+ # system in absolute value.
762
+ tol = np.max(np.abs(step_alt[~free_xl]), initial=0.0)
763
+ tol = np.max(np.abs(step_alt[~free_xu]), initial=tol)
764
+ tol = np.max(np.abs(aub[~free_ub, :] @ step_alt), initial=tol)
765
+ tol = min(10.0 * tol, 1e-2 * np.linalg.norm(step_alt))
766
+ if maxcv_val <= tol:
767
+ sigma_alt = self.models.determinants(
768
+ self.x_best + step_alt, k_new
769
+ )
770
+ if abs(sigma_alt) >= 0.1 * abs(sigma):
771
+ step = np.clip(step_alt, xl, xu)
772
+
773
+ if options[Options.DEBUG]:
774
+ tol = get_arrays_tol(xl, xu)
775
+ if np.any(step + tol < xl) or np.any(xu < step - tol):
776
+ warnings.warn(
777
+ "The geometry step does not respect the bound "
778
+ "constraints.",
779
+ RuntimeWarning,
780
+ 2,
781
+ )
782
+ if np.linalg.norm(step) > 1.1 * self.radius:
783
+ warnings.warn(
784
+ "The geometry step does not respect the "
785
+ "trust-region constraint.",
786
+ RuntimeWarning,
787
+ 2,
788
+ )
789
+ return step
790
+
791
+ def get_second_order_correction_step(self, step, options):
792
+ """
793
+ Get the second-order correction step.
794
+
795
+ Parameters
796
+ ----------
797
+ step : `numpy.ndarray`, shape (n,)
798
+ Trust-region step.
799
+ options : dict
800
+ Options of the solver.
801
+
802
+ Returns
803
+ -------
804
+ `numpy.ndarray`, shape (n,)
805
+ Second-order correction step.
806
+ """
807
+ # Evaluate the linearizations of the constraints.
808
+ aub, bub, aeq, beq = self.get_constraint_linearizations(self.x_best)
809
+ xl = self._pb.bounds.xl - self.x_best
810
+ xu = self._pb.bounds.xu - self.x_best
811
+ radius = np.linalg.norm(step)
812
+ soc_step = normal_byrd_omojokun(
813
+ aub,
814
+ bub,
815
+ aeq,
816
+ beq,
817
+ xl,
818
+ xu,
819
+ radius,
820
+ options[Options.DEBUG],
821
+ **self._constants,
822
+ )
823
+ if options[Options.DEBUG]:
824
+ tol = get_arrays_tol(xl, xu)
825
+ if np.any(soc_step + tol < xl) or np.any(xu < soc_step - tol):
826
+ warnings.warn(
827
+ "The second-order correction step does not "
828
+ "respect the bound constraints.",
829
+ RuntimeWarning,
830
+ 2,
831
+ )
832
+ if np.linalg.norm(soc_step) > 1.1 * radius:
833
+ warnings.warn(
834
+ "The second-order correction step does not "
835
+ "respect the trust-region constraint.",
836
+ RuntimeWarning,
837
+ 2,
838
+ )
839
+ return soc_step
840
+
841
+ def get_reduction_ratio(self, step, fun_val, cub_val, ceq_val):
842
+ """
843
+ Get the reduction ratio.
844
+
845
+ Parameters
846
+ ----------
847
+ step : `numpy.ndarray`, shape (n,)
848
+ Trust-region step.
849
+ fun_val : float
850
+ Objective function value at the trial point.
851
+ cub_val : `numpy.ndarray`, shape (m_nonlinear_ub,)
852
+ Nonlinear inequality constraint values at the trial point.
853
+ ceq_val : `numpy.ndarray`, shape (m_nonlinear_eq,)
854
+ Nonlinear equality constraint values at the trial point.
855
+
856
+ Returns
857
+ -------
858
+ float
859
+ Reduction ratio.
860
+ """
861
+ merit_old = self.merit(
862
+ self.x_best,
863
+ self.fun_best,
864
+ self.cub_best,
865
+ self.ceq_best,
866
+ )
867
+ merit_new = self.merit(self.x_best + step, fun_val, cub_val, ceq_val)
868
+ merit_model_old = self.merit(
869
+ self.x_best,
870
+ 0.0,
871
+ self.models.cub(self.x_best),
872
+ self.models.ceq(self.x_best),
873
+ )
874
+ merit_model_new = self.merit(
875
+ self.x_best + step,
876
+ self.sqp_fun(step),
877
+ self.sqp_cub(step),
878
+ self.sqp_ceq(step),
879
+ )
880
+ if abs(merit_model_old - merit_model_new) > TINY * abs(
881
+ merit_old - merit_new
882
+ ):
883
+ return (merit_old - merit_new) / abs(
884
+ merit_model_old - merit_model_new
885
+ )
886
+ else:
887
+ return -1.0
888
+
889
+ def increase_penalty(self, step):
890
+ """
891
+ Increase the penalty parameter.
892
+
893
+ Parameters
894
+ ----------
895
+ step : `numpy.ndarray`, shape (n,)
896
+ Trust-region step.
897
+ """
898
+ aub, bub, aeq, beq = self.get_constraint_linearizations(self.x_best)
899
+ viol_diff = max(
900
+ np.linalg.norm(
901
+ np.block(
902
+ [
903
+ np.maximum(0.0, -bub),
904
+ beq,
905
+ ]
906
+ )
907
+ )
908
+ - np.linalg.norm(
909
+ np.block(
910
+ [
911
+ np.maximum(0.0, aub @ step - bub),
912
+ aeq @ step - beq,
913
+ ]
914
+ )
915
+ ),
916
+ 0.0,
917
+ )
918
+ sqp_val = self.sqp_fun(step)
919
+
920
+ threshold = np.linalg.norm(
921
+ np.block(
922
+ [
923
+ self._lm_linear_ub,
924
+ self._lm_linear_eq,
925
+ self._lm_nonlinear_ub,
926
+ self._lm_nonlinear_eq,
927
+ ]
928
+ )
929
+ )
930
+ if abs(viol_diff) > TINY * abs(sqp_val):
931
+ threshold = max(threshold, sqp_val / viol_diff)
932
+ best_index_save = self.best_index
933
+ if (
934
+ self._penalty
935
+ <= self._constants[Constants.PENALTY_INCREASE_THRESHOLD]
936
+ * threshold
937
+ ):
938
+ self._penalty = max(
939
+ self._constants[Constants.PENALTY_INCREASE_FACTOR] * threshold,
940
+ 1.0,
941
+ )
942
+ self.set_best_index()
943
+ return best_index_save == self.best_index
944
+
945
+ def decrease_penalty(self):
946
+ """
947
+ Decrease the penalty parameter.
948
+ """
949
+ self._penalty = min(self._penalty, self._get_low_penalty())
950
+ self.set_best_index()
951
+
952
+ def set_best_index(self):
953
+ """
954
+ Set the index of the best point.
955
+ """
956
+ best_index = self.best_index
957
+ m_best = self.merit(
958
+ self.x_best,
959
+ self.models.fun_val[best_index],
960
+ self.models.cub_val[best_index, :],
961
+ self.models.ceq_val[best_index, :],
962
+ )
963
+ r_best = self._pb.maxcv(
964
+ self.x_best,
965
+ self.models.cub_val[best_index, :],
966
+ self.models.ceq_val[best_index, :],
967
+ )
968
+ tol = (
969
+ 10.0
970
+ * EPS
971
+ * max(self.models.n, self.models.npt)
972
+ * max(abs(m_best), 1.0)
973
+ )
974
+ for k in range(self.models.npt):
975
+ if k != self.best_index:
976
+ x_val = self.models.interpolation.point(k)
977
+ m_val = self.merit(
978
+ x_val,
979
+ self.models.fun_val[k],
980
+ self.models.cub_val[k, :],
981
+ self.models.ceq_val[k, :],
982
+ )
983
+ r_val = self._pb.maxcv(
984
+ x_val,
985
+ self.models.cub_val[k, :],
986
+ self.models.ceq_val[k, :],
987
+ )
988
+ if m_val < m_best or (m_val < m_best + tol and r_val < r_best):
989
+ best_index = k
990
+ m_best = m_val
991
+ r_best = r_val
992
+ self._best_index = best_index
993
+
994
+ def get_index_to_remove(self, x_new=None):
995
+ """
996
+ Get the index of the interpolation point to remove.
997
+
998
+ If `x_new` is not provided, the index returned should be used during
999
+ the geometry-improvement phase. Otherwise, the index returned is the
1000
+ best index for included `x_new` in the interpolation set.
1001
+
1002
+ Parameters
1003
+ ----------
1004
+ x_new : `numpy.ndarray`, shape (n,), optional
1005
+ New point to be included in the interpolation set.
1006
+
1007
+ Returns
1008
+ -------
1009
+ int
1010
+ Index of the interpolation point to remove.
1011
+ float
1012
+ Distance between `x_best` and the removed point.
1013
+
1014
+ Raises
1015
+ ------
1016
+ `numpy.linalg.LinAlgError`
1017
+ If the computation of a determinant fails.
1018
+ """
1019
+ dist_sq = np.sum(
1020
+ (
1021
+ self.models.interpolation.xpt
1022
+ - self.models.interpolation.xpt[:, self.best_index, np.newaxis]
1023
+ )
1024
+ ** 2.0,
1025
+ axis=0,
1026
+ )
1027
+ if x_new is None:
1028
+ sigma = 1.0
1029
+ weights = dist_sq
1030
+ else:
1031
+ sigma = self.models.determinants(x_new)
1032
+ weights = (
1033
+ np.maximum(
1034
+ 1.0,
1035
+ dist_sq
1036
+ / max(
1037
+ self._constants[Constants.LOW_RADIUS_FACTOR]
1038
+ * self.radius,
1039
+ self.resolution,
1040
+ )
1041
+ ** 2.0,
1042
+ )
1043
+ ** 3.0
1044
+ )
1045
+ weights[self.best_index] = -1.0 # do not remove the best point
1046
+ k_max = np.argmax(weights * np.abs(sigma))
1047
+ return k_max, np.sqrt(dist_sq[k_max])
1048
+
1049
+ def update_radius(self, step, ratio):
1050
+ """
1051
+ Update the trust-region radius.
1052
+
1053
+ Parameters
1054
+ ----------
1055
+ step : `numpy.ndarray`, shape (n,)
1056
+ Trust-region step.
1057
+ ratio : float
1058
+ Reduction ratio.
1059
+ """
1060
+ s_norm = np.linalg.norm(step)
1061
+ if ratio <= self._constants[Constants.LOW_RATIO]:
1062
+ self.radius *= self._constants[Constants.DECREASE_RADIUS_FACTOR]
1063
+ elif ratio <= self._constants[Constants.HIGH_RATIO]:
1064
+ self.radius = max(
1065
+ self._constants[Constants.DECREASE_RADIUS_FACTOR]
1066
+ * self.radius,
1067
+ s_norm,
1068
+ )
1069
+ else:
1070
+ self.radius = min(
1071
+ self._constants[Constants.INCREASE_RADIUS_FACTOR]
1072
+ * self.radius,
1073
+ max(
1074
+ self._constants[Constants.DECREASE_RADIUS_FACTOR]
1075
+ * self.radius,
1076
+ self._constants[Constants.INCREASE_RADIUS_THRESHOLD]
1077
+ * s_norm,
1078
+ ),
1079
+ )
1080
+
1081
+ def enhance_resolution(self, options):
1082
+ """
1083
+ Enhance the resolution of the trust-region framework.
1084
+
1085
+ Parameters
1086
+ ----------
1087
+ options : dict
1088
+ Options of the solver.
1089
+ """
1090
+ if (
1091
+ self._constants[Constants.LARGE_RESOLUTION_THRESHOLD]
1092
+ * options[Options.RHOEND]
1093
+ < self.resolution
1094
+ ):
1095
+ self.resolution *= self._constants[
1096
+ Constants.DECREASE_RESOLUTION_FACTOR
1097
+ ]
1098
+ elif (
1099
+ self._constants[Constants.MODERATE_RESOLUTION_THRESHOLD]
1100
+ * options[Options.RHOEND]
1101
+ < self.resolution
1102
+ ):
1103
+ self.resolution = np.sqrt(self.resolution
1104
+ * options[Options.RHOEND])
1105
+ else:
1106
+ self.resolution = options[Options.RHOEND]
1107
+
1108
+ # Reduce the trust-region radius.
1109
+ self._radius = max(
1110
+ self._constants[Constants.DECREASE_RADIUS_FACTOR] * self._radius,
1111
+ self.resolution,
1112
+ )
1113
+
1114
+ def shift_x_base(self, options):
1115
+ """
1116
+ Shift the base point to `x_best`.
1117
+
1118
+ Parameters
1119
+ ----------
1120
+ options : dict
1121
+ Options of the solver.
1122
+ """
1123
+ self.models.shift_x_base(np.copy(self.x_best), options)
1124
+
1125
+ def set_multipliers(self, x):
1126
+ """
1127
+ Set the Lagrange multipliers.
1128
+
1129
+ This method computes and set the Lagrange multipliers of the linear and
1130
+ nonlinear constraints to be the QP multipliers.
1131
+
1132
+ Parameters
1133
+ ----------
1134
+ x : `numpy.ndarray`, shape (n,)
1135
+ Point at which the Lagrange multipliers are computed.
1136
+ """
1137
+ # Build the constraints of the least-squares problem.
1138
+ incl_linear_ub = self._pb.linear.a_ub @ x >= self._pb.linear.b_ub
1139
+ incl_nonlinear_ub = self.cub_best >= 0.0
1140
+ incl_xl = self._pb.bounds.xl >= x
1141
+ incl_xu = self._pb.bounds.xu <= x
1142
+ m_linear_ub = np.count_nonzero(incl_linear_ub)
1143
+ m_nonlinear_ub = np.count_nonzero(incl_nonlinear_ub)
1144
+ m_xl = np.count_nonzero(incl_xl)
1145
+ m_xu = np.count_nonzero(incl_xu)
1146
+
1147
+ if (
1148
+ m_linear_ub + m_nonlinear_ub + self.m_linear_eq
1149
+ + self.m_nonlinear_eq > 0
1150
+ ):
1151
+ identity = np.eye(self._pb.n)
1152
+ c_jac = np.r_[
1153
+ -identity[incl_xl, :],
1154
+ identity[incl_xu, :],
1155
+ self._pb.linear.a_ub[incl_linear_ub, :],
1156
+ self.models.cub_grad(x, incl_nonlinear_ub),
1157
+ self._pb.linear.a_eq,
1158
+ self.models.ceq_grad(x),
1159
+ ]
1160
+
1161
+ # Solve the least-squares problem.
1162
+ g_best = self.models.fun_grad(x)
1163
+ xl_lm = np.full(c_jac.shape[0], -np.inf)
1164
+ xl_lm[: m_xl + m_xu + m_linear_ub + m_nonlinear_ub] = 0.0
1165
+ res = lsq_linear(
1166
+ c_jac.T,
1167
+ -g_best,
1168
+ bounds=(xl_lm, np.inf),
1169
+ method="bvls",
1170
+ )
1171
+
1172
+ # Extract the Lagrange multipliers.
1173
+ self._lm_linear_ub[incl_linear_ub] = res.x[
1174
+ m_xl + m_xu:m_xl + m_xu + m_linear_ub
1175
+ ]
1176
+ self._lm_linear_ub[~incl_linear_ub] = 0.0
1177
+ self._lm_nonlinear_ub[incl_nonlinear_ub] = res.x[
1178
+ m_xl
1179
+ + m_xu
1180
+ + m_linear_ub:m_xl
1181
+ + m_xu
1182
+ + m_linear_ub
1183
+ + m_nonlinear_ub
1184
+ ]
1185
+ self._lm_nonlinear_ub[~incl_nonlinear_ub] = 0.0
1186
+ self._lm_linear_eq[:] = res.x[
1187
+ m_xl
1188
+ + m_xu
1189
+ + m_linear_ub
1190
+ + m_nonlinear_ub:m_xl
1191
+ + m_xu
1192
+ + m_linear_ub
1193
+ + m_nonlinear_ub
1194
+ + self.m_linear_eq
1195
+ ]
1196
+ self._lm_nonlinear_eq[:] = res.x[
1197
+ m_xl + m_xu + m_linear_ub + m_nonlinear_ub + self.m_linear_eq:
1198
+ ]
1199
+
1200
+ def _get_low_penalty(self):
1201
+ r_val_ub = np.c_[
1202
+ (
1203
+ self.models.interpolation.x_base[np.newaxis, :]
1204
+ + self.models.interpolation.xpt.T
1205
+ )
1206
+ @ self._pb.linear.a_ub.T
1207
+ - self._pb.linear.b_ub[np.newaxis, :],
1208
+ self.models.cub_val,
1209
+ ]
1210
+ r_val_eq = (
1211
+ self.models.interpolation.x_base[np.newaxis, :]
1212
+ + self.models.interpolation.xpt.T
1213
+ ) @ self._pb.linear.a_eq.T - self._pb.linear.b_eq[np.newaxis, :]
1214
+ r_val_eq = np.block(
1215
+ [
1216
+ r_val_eq,
1217
+ -r_val_eq,
1218
+ self.models.ceq_val,
1219
+ -self.models.ceq_val,
1220
+ ]
1221
+ )
1222
+ r_val = np.block([r_val_ub, r_val_eq])
1223
+ c_min = np.nanmin(r_val, axis=0)
1224
+ c_max = np.nanmax(r_val, axis=0)
1225
+ indices = (
1226
+ c_min
1227
+ < self._constants[Constants.THRESHOLD_RATIO_CONSTRAINTS] * c_max
1228
+ )
1229
+ if np.any(indices):
1230
+ f_min = np.nanmin(self.models.fun_val)
1231
+ f_max = np.nanmax(self.models.fun_val)
1232
+ c_min_neg = np.minimum(0.0, c_min[indices])
1233
+ c_diff = np.min(c_max[indices] - c_min_neg)
1234
+ if c_diff > TINY * (f_max - f_min):
1235
+ penalty = (f_max - f_min) / c_diff
1236
+ else:
1237
+ penalty = np.inf
1238
+ else:
1239
+ penalty = 0.0
1240
+ return penalty
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/main.py ADDED
@@ -0,0 +1,1506 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import warnings
2
+
3
+ import numpy as np
4
+ from scipy.optimize import (
5
+ Bounds,
6
+ LinearConstraint,
7
+ NonlinearConstraint,
8
+ OptimizeResult,
9
+ )
10
+
11
+ from .framework import TrustRegion
12
+ from .problem import (
13
+ ObjectiveFunction,
14
+ BoundConstraints,
15
+ LinearConstraints,
16
+ NonlinearConstraints,
17
+ Problem,
18
+ )
19
+ from .utils import (
20
+ MaxEvalError,
21
+ TargetSuccess,
22
+ CallbackSuccess,
23
+ FeasibleSuccess,
24
+ exact_1d_array,
25
+ )
26
+ from .settings import (
27
+ ExitStatus,
28
+ Options,
29
+ Constants,
30
+ DEFAULT_OPTIONS,
31
+ DEFAULT_CONSTANTS,
32
+ PRINT_OPTIONS,
33
+ )
34
+
35
+
36
+ def minimize(
37
+ fun,
38
+ x0,
39
+ args=(),
40
+ bounds=None,
41
+ constraints=(),
42
+ callback=None,
43
+ options=None,
44
+ **kwargs,
45
+ ):
46
+ r"""
47
+ Minimize a scalar function using the COBYQA method.
48
+
49
+ The Constrained Optimization BY Quadratic Approximations (COBYQA) method is
50
+ a derivative-free optimization method designed to solve general nonlinear
51
+ optimization problems. A complete description of COBYQA is given in [3]_.
52
+
53
+ Parameters
54
+ ----------
55
+ fun : {callable, None}
56
+ Objective function to be minimized.
57
+
58
+ ``fun(x, *args) -> float``
59
+
60
+ where ``x`` is an array with shape (n,) and `args` is a tuple. If `fun`
61
+ is ``None``, the objective function is assumed to be the zero function,
62
+ resulting in a feasibility problem.
63
+ x0 : array_like, shape (n,)
64
+ Initial guess.
65
+ args : tuple, optional
66
+ Extra arguments passed to the objective function.
67
+ bounds : {`scipy.optimize.Bounds`, array_like, shape (n, 2)}, optional
68
+ Bound constraints of the problem. It can be one of the cases below.
69
+
70
+ #. An instance of `scipy.optimize.Bounds`. For the time being, the
71
+ argument ``keep_feasible`` is disregarded, and all the constraints
72
+ are considered unrelaxable and will be enforced.
73
+ #. An array with shape (n, 2). The bound constraints for ``x[i]`` are
74
+ ``bounds[i][0] <= x[i] <= bounds[i][1]``. Set ``bounds[i][0]`` to
75
+ :math:`-\infty` if there is no lower bound, and set ``bounds[i][1]``
76
+ to :math:`\infty` if there is no upper bound.
77
+
78
+ The COBYQA method always respect the bound constraints.
79
+ constraints : {Constraint, list}, optional
80
+ General constraints of the problem. It can be one of the cases below.
81
+
82
+ #. An instance of `scipy.optimize.LinearConstraint`. The argument
83
+ ``keep_feasible`` is disregarded.
84
+ #. An instance of `scipy.optimize.NonlinearConstraint`. The arguments
85
+ ``jac``, ``hess``, ``keep_feasible``, ``finite_diff_rel_step``, and
86
+ ``finite_diff_jac_sparsity`` are disregarded.
87
+
88
+ #. A list, each of whose elements are described in the cases above.
89
+
90
+ callback : callable, optional
91
+ A callback executed at each objective function evaluation. The method
92
+ terminates if a ``StopIteration`` exception is raised by the callback
93
+ function. Its signature can be one of the following:
94
+
95
+ ``callback(intermediate_result)``
96
+
97
+ where ``intermediate_result`` is a keyword parameter that contains an
98
+ instance of `scipy.optimize.OptimizeResult`, with attributes ``x``
99
+ and ``fun``, being the point at which the objective function is
100
+ evaluated and the value of the objective function, respectively. The
101
+ name of the parameter must be ``intermediate_result`` for the callback
102
+ to be passed an instance of `scipy.optimize.OptimizeResult`.
103
+
104
+ Alternatively, the callback function can have the signature:
105
+
106
+ ``callback(xk)``
107
+
108
+ where ``xk`` is the point at which the objective function is evaluated.
109
+ Introspection is used to determine which of the signatures to invoke.
110
+ options : dict, optional
111
+ Options passed to the solver. Accepted keys are:
112
+
113
+ disp : bool, optional
114
+ Whether to print information about the optimization procedure.
115
+ Default is ``False``.
116
+ maxfev : int, optional
117
+ Maximum number of function evaluations. Default is ``500 * n``.
118
+ maxiter : int, optional
119
+ Maximum number of iterations. Default is ``1000 * n``.
120
+ target : float, optional
121
+ Target on the objective function value. The optimization
122
+ procedure is terminated when the objective function value of a
123
+ feasible point is less than or equal to this target. Default is
124
+ ``-numpy.inf``.
125
+ feasibility_tol : float, optional
126
+ Tolerance on the constraint violation. If the maximum
127
+ constraint violation at a point is less than or equal to this
128
+ tolerance, the point is considered feasible. Default is
129
+ ``numpy.sqrt(numpy.finfo(float).eps)``.
130
+ radius_init : float, optional
131
+ Initial trust-region radius. Typically, this value should be in
132
+ the order of one tenth of the greatest expected change to `x0`.
133
+ Default is ``1.0``.
134
+ radius_final : float, optional
135
+ Final trust-region radius. It should indicate the accuracy
136
+ required in the final values of the variables. Default is
137
+ ``1e-6``.
138
+ nb_points : int, optional
139
+ Number of interpolation points used to build the quadratic
140
+ models of the objective and constraint functions. Default is
141
+ ``2 * n + 1``.
142
+ scale : bool, optional
143
+ Whether to scale the variables according to the bounds. Default
144
+ is ``False``.
145
+ filter_size : int, optional
146
+ Maximum number of points in the filter. The filter is used to
147
+ select the best point returned by the optimization procedure.
148
+ Default is ``sys.maxsize``.
149
+ store_history : bool, optional
150
+ Whether to store the history of the function evaluations.
151
+ Default is ``False``.
152
+ history_size : int, optional
153
+ Maximum number of function evaluations to store in the history.
154
+ Default is ``sys.maxsize``.
155
+ debug : bool, optional
156
+ Whether to perform additional checks during the optimization
157
+ procedure. This option should be used only for debugging
158
+ purposes and is highly discouraged to general users. Default is
159
+ ``False``.
160
+
161
+ Other constants (from the keyword arguments) are described below. They
162
+ are not intended to be changed by general users. They should only be
163
+ changed by users with a deep understanding of the algorithm, who want
164
+ to experiment with different settings.
165
+
166
+ Returns
167
+ -------
168
+ `scipy.optimize.OptimizeResult`
169
+ Result of the optimization procedure, with the following fields:
170
+
171
+ message : str
172
+ Description of the cause of the termination.
173
+ success : bool
174
+ Whether the optimization procedure terminated successfully.
175
+ status : int
176
+ Termination status of the optimization procedure.
177
+ x : `numpy.ndarray`, shape (n,)
178
+ Solution point.
179
+ fun : float
180
+ Objective function value at the solution point.
181
+ maxcv : float
182
+ Maximum constraint violation at the solution point.
183
+ nfev : int
184
+ Number of function evaluations.
185
+ nit : int
186
+ Number of iterations.
187
+
188
+ If ``store_history`` is True, the result also has the following fields:
189
+
190
+ fun_history : `numpy.ndarray`, shape (nfev,)
191
+ History of the objective function values.
192
+ maxcv_history : `numpy.ndarray`, shape (nfev,)
193
+ History of the maximum constraint violations.
194
+
195
+ A description of the termination statuses is given below.
196
+
197
+ .. list-table::
198
+ :widths: 25 75
199
+ :header-rows: 1
200
+
201
+ * - Exit status
202
+ - Description
203
+ * - 0
204
+ - The lower bound for the trust-region radius has been reached.
205
+ * - 1
206
+ - The target objective function value has been reached.
207
+ * - 2
208
+ - All variables are fixed by the bound constraints.
209
+ * - 3
210
+ - The callback requested to stop the optimization procedure.
211
+ * - 4
212
+ - The feasibility problem received has been solved successfully.
213
+ * - 5
214
+ - The maximum number of function evaluations has been exceeded.
215
+ * - 6
216
+ - The maximum number of iterations has been exceeded.
217
+ * - -1
218
+ - The bound constraints are infeasible.
219
+ * - -2
220
+ - A linear algebra error occurred.
221
+
222
+ Other Parameters
223
+ ----------------
224
+ decrease_radius_factor : float, optional
225
+ Factor by which the trust-region radius is reduced when the reduction
226
+ ratio is low or negative. Default is ``0.5``.
227
+ increase_radius_factor : float, optional
228
+ Factor by which the trust-region radius is increased when the reduction
229
+ ratio is large. Default is ``numpy.sqrt(2.0)``.
230
+ increase_radius_threshold : float, optional
231
+ Threshold that controls the increase of the trust-region radius when
232
+ the reduction ratio is large. Default is ``2.0``.
233
+ decrease_radius_threshold : float, optional
234
+ Threshold used to determine whether the trust-region radius should be
235
+ reduced to the resolution. Default is ``1.4``.
236
+ decrease_resolution_factor : float, optional
237
+ Factor by which the resolution is reduced when the current value is far
238
+ from its final value. Default is ``0.1``.
239
+ large_resolution_threshold : float, optional
240
+ Threshold used to determine whether the resolution is far from its
241
+ final value. Default is ``250.0``.
242
+ moderate_resolution_threshold : float, optional
243
+ Threshold used to determine whether the resolution is close to its
244
+ final value. Default is ``16.0``.
245
+ low_ratio : float, optional
246
+ Threshold used to determine whether the reduction ratio is low. Default
247
+ is ``0.1``.
248
+ high_ratio : float, optional
249
+ Threshold used to determine whether the reduction ratio is high.
250
+ Default is ``0.7``.
251
+ very_low_ratio : float, optional
252
+ Threshold used to determine whether the reduction ratio is very low.
253
+ This is used to determine whether the models should be reset. Default
254
+ is ``0.01``.
255
+ penalty_increase_threshold : float, optional
256
+ Threshold used to determine whether the penalty parameter should be
257
+ increased. Default is ``1.5``.
258
+ penalty_increase_factor : float, optional
259
+ Factor by which the penalty parameter is increased. Default is ``2.0``.
260
+ short_step_threshold : float, optional
261
+ Factor used to determine whether the trial step is too short. Default
262
+ is ``0.5``.
263
+ low_radius_factor : float, optional
264
+ Factor used to determine which interpolation point should be removed
265
+ from the interpolation set at each iteration. Default is ``0.1``.
266
+ byrd_omojokun_factor : float, optional
267
+ Factor by which the trust-region radius is reduced for the computations
268
+ of the normal step in the Byrd-Omojokun composite-step approach.
269
+ Default is ``0.8``.
270
+ threshold_ratio_constraints : float, optional
271
+ Threshold used to determine which constraints should be taken into
272
+ account when decreasing the penalty parameter. Default is ``2.0``.
273
+ large_shift_factor : float, optional
274
+ Factor used to determine whether the point around which the quadratic
275
+ models are built should be updated. Default is ``10.0``.
276
+ large_gradient_factor : float, optional
277
+ Factor used to determine whether the models should be reset. Default is
278
+ ``10.0``.
279
+ resolution_factor : float, optional
280
+ Factor by which the resolution is decreased. Default is ``2.0``.
281
+ improve_tcg : bool, optional
282
+ Whether to improve the steps computed by the truncated conjugate
283
+ gradient method when the trust-region boundary is reached. Default is
284
+ ``True``.
285
+
286
+ References
287
+ ----------
288
+ .. [1] J. Nocedal and S. J. Wright. *Numerical Optimization*. Springer Ser.
289
+ Oper. Res. Financ. Eng. Springer, New York, NY, USA, second edition,
290
+ 2006. `doi:10.1007/978-0-387-40065-5
291
+ <https://doi.org/10.1007/978-0-387-40065-5>`_.
292
+ .. [2] M. J. D. Powell. A direct search optimization method that models the
293
+ objective and constraint functions by linear interpolation. In S. Gomez
294
+ and J.-P. Hennart, editors, *Advances in Optimization and Numerical
295
+ Analysis*, volume 275 of Math. Appl., pages 51--67. Springer, Dordrecht,
296
+ Netherlands, 1994. `doi:10.1007/978-94-015-8330-5_4
297
+ <https://doi.org/10.1007/978-94-015-8330-5_4>`_.
298
+ .. [3] T. M. Ragonneau. *Model-Based Derivative-Free Optimization Methods
299
+ and Software*. PhD thesis, Department of Applied Mathematics, The Hong
300
+ Kong Polytechnic University, Hong Kong, China, 2022. URL:
301
+ https://theses.lib.polyu.edu.hk/handle/200/12294.
302
+
303
+ Examples
304
+ --------
305
+ To demonstrate how to use `minimize`, we first minimize the Rosenbrock
306
+ function implemented in `scipy.optimize` in an unconstrained setting.
307
+
308
+ .. testsetup::
309
+
310
+ import numpy as np
311
+ np.set_printoptions(precision=3, suppress=True)
312
+
313
+ >>> from cobyqa import minimize
314
+ >>> from scipy.optimize import rosen
315
+
316
+ To solve the problem using COBYQA, run:
317
+
318
+ >>> x0 = [1.3, 0.7, 0.8, 1.9, 1.2]
319
+ >>> res = minimize(rosen, x0)
320
+ >>> res.x
321
+ array([1., 1., 1., 1., 1.])
322
+
323
+ To see how bound and constraints are handled using `minimize`, we solve
324
+ Example 16.4 of [1]_, defined as
325
+
326
+ .. math::
327
+
328
+ \begin{aligned}
329
+ \min_{x \in \mathbb{R}^2} & \quad (x_1 - 1)^2 + (x_2 - 2.5)^2\\
330
+ \text{s.t.} & \quad -x_1 + 2x_2 \le 2,\\
331
+ & \quad x_1 + 2x_2 \le 6,\\
332
+ & \quad x_1 - 2x_2 \le 2,\\
333
+ & \quad x_1 \ge 0,\\
334
+ & \quad x_2 \ge 0.
335
+ \end{aligned}
336
+
337
+ >>> import numpy as np
338
+ >>> from scipy.optimize import Bounds, LinearConstraint
339
+
340
+ Its objective function can be implemented as:
341
+
342
+ >>> def fun(x):
343
+ ... return (x[0] - 1.0)**2 + (x[1] - 2.5)**2
344
+
345
+ This problem can be solved using `minimize` as:
346
+
347
+ >>> x0 = [2.0, 0.0]
348
+ >>> bounds = Bounds([0.0, 0.0], np.inf)
349
+ >>> constraints = LinearConstraint([
350
+ ... [-1.0, 2.0],
351
+ ... [1.0, 2.0],
352
+ ... [1.0, -2.0],
353
+ ... ], -np.inf, [2.0, 6.0, 2.0])
354
+ >>> res = minimize(fun, x0, bounds=bounds, constraints=constraints)
355
+ >>> res.x
356
+ array([1.4, 1.7])
357
+
358
+ To see how nonlinear constraints are handled, we solve Problem (F) of [2]_,
359
+ defined as
360
+
361
+ .. math::
362
+
363
+ \begin{aligned}
364
+ \min_{x \in \mathbb{R}^2} & \quad -x_1 - x_2\\
365
+ \text{s.t.} & \quad x_1^2 - x_2 \le 0,\\
366
+ & \quad x_1^2 + x_2^2 \le 1.
367
+ \end{aligned}
368
+
369
+ >>> from scipy.optimize import NonlinearConstraint
370
+
371
+ Its objective and constraint functions can be implemented as:
372
+
373
+ >>> def fun(x):
374
+ ... return -x[0] - x[1]
375
+ >>>
376
+ >>> def cub(x):
377
+ ... return [x[0]**2 - x[1], x[0]**2 + x[1]**2]
378
+
379
+ This problem can be solved using `minimize` as:
380
+
381
+ >>> x0 = [1.0, 1.0]
382
+ >>> constraints = NonlinearConstraint(cub, -np.inf, [0.0, 1.0])
383
+ >>> res = minimize(fun, x0, constraints=constraints)
384
+ >>> res.x
385
+ array([0.707, 0.707])
386
+
387
+ Finally, to see how to supply linear and nonlinear constraints
388
+ simultaneously, we solve Problem (G) of [2]_, defined as
389
+
390
+ .. math::
391
+
392
+ \begin{aligned}
393
+ \min_{x \in \mathbb{R}^3} & \quad x_3\\
394
+ \text{s.t.} & \quad 5x_1 - x_2 + x_3 \ge 0,\\
395
+ & \quad -5x_1 - x_2 + x_3 \ge 0,\\
396
+ & \quad x_1^2 + x_2^2 + 4x_2 \le x_3.
397
+ \end{aligned}
398
+
399
+ Its objective and nonlinear constraint functions can be implemented as:
400
+
401
+ >>> def fun(x):
402
+ ... return x[2]
403
+ >>>
404
+ >>> def cub(x):
405
+ ... return x[0]**2 + x[1]**2 + 4.0*x[1] - x[2]
406
+
407
+ This problem can be solved using `minimize` as:
408
+
409
+ >>> x0 = [1.0, 1.0, 1.0]
410
+ >>> constraints = [
411
+ ... LinearConstraint(
412
+ ... [[5.0, -1.0, 1.0], [-5.0, -1.0, 1.0]],
413
+ ... [0.0, 0.0],
414
+ ... np.inf,
415
+ ... ),
416
+ ... NonlinearConstraint(cub, -np.inf, 0.0),
417
+ ... ]
418
+ >>> res = minimize(fun, x0, constraints=constraints)
419
+ >>> res.x
420
+ array([ 0., -3., -3.])
421
+ """
422
+ # Get basic options that are needed for the initialization.
423
+ if options is None:
424
+ options = {}
425
+ else:
426
+ options = dict(options)
427
+ verbose = options.get(Options.VERBOSE, DEFAULT_OPTIONS[Options.VERBOSE])
428
+ verbose = bool(verbose)
429
+ feasibility_tol = options.get(
430
+ Options.FEASIBILITY_TOL,
431
+ DEFAULT_OPTIONS[Options.FEASIBILITY_TOL],
432
+ )
433
+ feasibility_tol = float(feasibility_tol)
434
+ scale = options.get(Options.SCALE, DEFAULT_OPTIONS[Options.SCALE])
435
+ scale = bool(scale)
436
+ store_history = options.get(
437
+ Options.STORE_HISTORY,
438
+ DEFAULT_OPTIONS[Options.STORE_HISTORY],
439
+ )
440
+ store_history = bool(store_history)
441
+ if Options.HISTORY_SIZE in options and options[Options.HISTORY_SIZE] <= 0:
442
+ raise ValueError("The size of the history must be positive.")
443
+ history_size = options.get(
444
+ Options.HISTORY_SIZE,
445
+ DEFAULT_OPTIONS[Options.HISTORY_SIZE],
446
+ )
447
+ history_size = int(history_size)
448
+ if Options.FILTER_SIZE in options and options[Options.FILTER_SIZE] <= 0:
449
+ raise ValueError("The size of the filter must be positive.")
450
+ filter_size = options.get(
451
+ Options.FILTER_SIZE,
452
+ DEFAULT_OPTIONS[Options.FILTER_SIZE],
453
+ )
454
+ filter_size = int(filter_size)
455
+ debug = options.get(Options.DEBUG, DEFAULT_OPTIONS[Options.DEBUG])
456
+ debug = bool(debug)
457
+
458
+ # Initialize the objective function.
459
+ if not isinstance(args, tuple):
460
+ args = (args,)
461
+ obj = ObjectiveFunction(fun, verbose, debug, *args)
462
+
463
+ # Initialize the bound constraints.
464
+ if not hasattr(x0, "__len__"):
465
+ x0 = [x0]
466
+ n_orig = len(x0)
467
+ bounds = BoundConstraints(_get_bounds(bounds, n_orig))
468
+
469
+ # Initialize the constraints.
470
+ linear_constraints, nonlinear_constraints = _get_constraints(constraints)
471
+ linear = LinearConstraints(linear_constraints, n_orig, debug)
472
+ nonlinear = NonlinearConstraints(nonlinear_constraints, verbose, debug)
473
+
474
+ # Initialize the problem (and remove the fixed variables).
475
+ pb = Problem(
476
+ obj,
477
+ x0,
478
+ bounds,
479
+ linear,
480
+ nonlinear,
481
+ callback,
482
+ feasibility_tol,
483
+ scale,
484
+ store_history,
485
+ history_size,
486
+ filter_size,
487
+ debug,
488
+ )
489
+
490
+ # Set the default options.
491
+ _set_default_options(options, pb.n)
492
+ constants = _set_default_constants(**kwargs)
493
+
494
+ # Initialize the models and skip the computations whenever possible.
495
+ if not pb.bounds.is_feasible:
496
+ # The bound constraints are infeasible.
497
+ return _build_result(
498
+ pb,
499
+ 0.0,
500
+ False,
501
+ ExitStatus.INFEASIBLE_ERROR,
502
+ 0,
503
+ options,
504
+ )
505
+ elif pb.n == 0:
506
+ # All variables are fixed by the bound constraints.
507
+ return _build_result(
508
+ pb,
509
+ 0.0,
510
+ True,
511
+ ExitStatus.FIXED_SUCCESS,
512
+ 0,
513
+ options,
514
+ )
515
+ if verbose:
516
+ print("Starting the optimization procedure.")
517
+ print(f"Initial trust-region radius: {options[Options.RHOBEG]}.")
518
+ print(f"Final trust-region radius: {options[Options.RHOEND]}.")
519
+ print(
520
+ f"Maximum number of function evaluations: "
521
+ f"{options[Options.MAX_EVAL]}."
522
+ )
523
+ print(f"Maximum number of iterations: {options[Options.MAX_ITER]}.")
524
+ print()
525
+ try:
526
+ framework = TrustRegion(pb, options, constants)
527
+ except TargetSuccess:
528
+ # The target on the objective function value has been reached
529
+ return _build_result(
530
+ pb,
531
+ 0.0,
532
+ True,
533
+ ExitStatus.TARGET_SUCCESS,
534
+ 0,
535
+ options,
536
+ )
537
+ except CallbackSuccess:
538
+ # The callback raised a StopIteration exception.
539
+ return _build_result(
540
+ pb,
541
+ 0.0,
542
+ True,
543
+ ExitStatus.CALLBACK_SUCCESS,
544
+ 0,
545
+ options,
546
+ )
547
+ except FeasibleSuccess:
548
+ # The feasibility problem has been solved successfully.
549
+ return _build_result(
550
+ pb,
551
+ 0.0,
552
+ True,
553
+ ExitStatus.FEASIBLE_SUCCESS,
554
+ 0,
555
+ options,
556
+ )
557
+ except MaxEvalError:
558
+ # The maximum number of function evaluations has been exceeded.
559
+ return _build_result(
560
+ pb,
561
+ 0.0,
562
+ False,
563
+ ExitStatus.MAX_ITER_WARNING,
564
+ 0,
565
+ options,
566
+ )
567
+ except np.linalg.LinAlgError:
568
+ # The construction of the initial interpolation set failed.
569
+ return _build_result(
570
+ pb,
571
+ 0.0,
572
+ False,
573
+ ExitStatus.LINALG_ERROR,
574
+ 0,
575
+ options,
576
+ )
577
+
578
+ # Start the optimization procedure.
579
+ success = False
580
+ n_iter = 0
581
+ k_new = None
582
+ n_short_steps = 0
583
+ n_very_short_steps = 0
584
+ n_alt_models = 0
585
+ while True:
586
+ # Stop the optimization procedure if the maximum number of iterations
587
+ # has been exceeded. We do not write the main loop as a for loop
588
+ # because we want to access the number of iterations outside the loop.
589
+ if n_iter >= options[Options.MAX_ITER]:
590
+ status = ExitStatus.MAX_ITER_WARNING
591
+ break
592
+ n_iter += 1
593
+
594
+ # Update the point around which the quadratic models are built.
595
+ if (
596
+ np.linalg.norm(
597
+ framework.x_best - framework.models.interpolation.x_base
598
+ )
599
+ >= constants[Constants.LARGE_SHIFT_FACTOR] * framework.radius
600
+ ):
601
+ framework.shift_x_base(options)
602
+
603
+ # Evaluate the trial step.
604
+ radius_save = framework.radius
605
+ normal_step, tangential_step = framework.get_trust_region_step(options)
606
+ step = normal_step + tangential_step
607
+ s_norm = np.linalg.norm(step)
608
+
609
+ # If the trial step is too short, we do not attempt to evaluate the
610
+ # objective and constraint functions. Instead, we reduce the
611
+ # trust-region radius and check whether the resolution should be
612
+ # enhanced and whether the geometry of the interpolation set should be
613
+ # improved. Otherwise, we entertain a classical iteration. The
614
+ # criterion for performing an exceptional jump is taken from NEWUOA.
615
+ if (
616
+ s_norm
617
+ <= constants[Constants.SHORT_STEP_THRESHOLD] * framework.resolution
618
+ ):
619
+ framework.radius *= constants[Constants.DECREASE_RESOLUTION_FACTOR]
620
+ if radius_save > framework.resolution:
621
+ n_short_steps = 0
622
+ n_very_short_steps = 0
623
+ else:
624
+ n_short_steps += 1
625
+ n_very_short_steps += 1
626
+ if s_norm > 0.1 * framework.resolution:
627
+ n_very_short_steps = 0
628
+ enhance_resolution = n_short_steps >= 5 or n_very_short_steps >= 3
629
+ if enhance_resolution:
630
+ n_short_steps = 0
631
+ n_very_short_steps = 0
632
+ improve_geometry = False
633
+ else:
634
+ try:
635
+ k_new, dist_new = framework.get_index_to_remove()
636
+ except np.linalg.LinAlgError:
637
+ status = ExitStatus.LINALG_ERROR
638
+ break
639
+ improve_geometry = dist_new > max(
640
+ framework.radius,
641
+ constants[Constants.RESOLUTION_FACTOR]
642
+ * framework.resolution,
643
+ )
644
+ else:
645
+ # Increase the penalty parameter if necessary.
646
+ same_best_point = framework.increase_penalty(step)
647
+ if same_best_point:
648
+ # Evaluate the objective and constraint functions.
649
+ try:
650
+ fun_val, cub_val, ceq_val = _eval(
651
+ pb,
652
+ framework,
653
+ step,
654
+ options,
655
+ )
656
+ except TargetSuccess:
657
+ status = ExitStatus.TARGET_SUCCESS
658
+ success = True
659
+ break
660
+ except FeasibleSuccess:
661
+ status = ExitStatus.FEASIBLE_SUCCESS
662
+ success = True
663
+ break
664
+ except CallbackSuccess:
665
+ status = ExitStatus.CALLBACK_SUCCESS
666
+ success = True
667
+ break
668
+ except MaxEvalError:
669
+ status = ExitStatus.MAX_EVAL_WARNING
670
+ break
671
+
672
+ # Perform a second-order correction step if necessary.
673
+ merit_old = framework.merit(
674
+ framework.x_best,
675
+ framework.fun_best,
676
+ framework.cub_best,
677
+ framework.ceq_best,
678
+ )
679
+ merit_new = framework.merit(
680
+ framework.x_best + step, fun_val, cub_val, ceq_val
681
+ )
682
+ if (
683
+ pb.type == "nonlinearly constrained"
684
+ and merit_new > merit_old
685
+ and np.linalg.norm(normal_step)
686
+ > constants[Constants.BYRD_OMOJOKUN_FACTOR] ** 2.0
687
+ * framework.radius
688
+ ):
689
+ soc_step = framework.get_second_order_correction_step(
690
+ step, options
691
+ )
692
+ if np.linalg.norm(soc_step) > 0.0:
693
+ step += soc_step
694
+
695
+ # Evaluate the objective and constraint functions.
696
+ try:
697
+ fun_val, cub_val, ceq_val = _eval(
698
+ pb,
699
+ framework,
700
+ step,
701
+ options,
702
+ )
703
+ except TargetSuccess:
704
+ status = ExitStatus.TARGET_SUCCESS
705
+ success = True
706
+ break
707
+ except FeasibleSuccess:
708
+ status = ExitStatus.FEASIBLE_SUCCESS
709
+ success = True
710
+ break
711
+ except CallbackSuccess:
712
+ status = ExitStatus.CALLBACK_SUCCESS
713
+ success = True
714
+ break
715
+ except MaxEvalError:
716
+ status = ExitStatus.MAX_EVAL_WARNING
717
+ break
718
+
719
+ # Calculate the reduction ratio.
720
+ ratio = framework.get_reduction_ratio(
721
+ step,
722
+ fun_val,
723
+ cub_val,
724
+ ceq_val,
725
+ )
726
+
727
+ # Choose an interpolation point to remove.
728
+ try:
729
+ k_new = framework.get_index_to_remove(
730
+ framework.x_best + step
731
+ )[0]
732
+ except np.linalg.LinAlgError:
733
+ status = ExitStatus.LINALG_ERROR
734
+ break
735
+
736
+ # Update the interpolation set.
737
+ try:
738
+ ill_conditioned = framework.models.update_interpolation(
739
+ k_new, framework.x_best + step, fun_val, cub_val,
740
+ ceq_val
741
+ )
742
+ except np.linalg.LinAlgError:
743
+ status = ExitStatus.LINALG_ERROR
744
+ break
745
+ framework.set_best_index()
746
+
747
+ # Update the trust-region radius.
748
+ framework.update_radius(step, ratio)
749
+
750
+ # Attempt to replace the models by the alternative ones.
751
+ if framework.radius <= framework.resolution:
752
+ if ratio >= constants[Constants.VERY_LOW_RATIO]:
753
+ n_alt_models = 0
754
+ else:
755
+ n_alt_models += 1
756
+ grad = framework.models.fun_grad(framework.x_best)
757
+ try:
758
+ grad_alt = framework.models.fun_alt_grad(
759
+ framework.x_best
760
+ )
761
+ except np.linalg.LinAlgError:
762
+ status = ExitStatus.LINALG_ERROR
763
+ break
764
+ if np.linalg.norm(grad) < constants[
765
+ Constants.LARGE_GRADIENT_FACTOR
766
+ ] * np.linalg.norm(grad_alt):
767
+ n_alt_models = 0
768
+ if n_alt_models >= 3:
769
+ try:
770
+ framework.models.reset_models()
771
+ except np.linalg.LinAlgError:
772
+ status = ExitStatus.LINALG_ERROR
773
+ break
774
+ n_alt_models = 0
775
+
776
+ # Update the Lagrange multipliers.
777
+ framework.set_multipliers(framework.x_best + step)
778
+
779
+ # Check whether the resolution should be enhanced.
780
+ try:
781
+ k_new, dist_new = framework.get_index_to_remove()
782
+ except np.linalg.LinAlgError:
783
+ status = ExitStatus.LINALG_ERROR
784
+ break
785
+ improve_geometry = (
786
+ ill_conditioned
787
+ or ratio <= constants[Constants.LOW_RATIO]
788
+ and dist_new
789
+ > max(
790
+ framework.radius,
791
+ constants[Constants.RESOLUTION_FACTOR]
792
+ * framework.resolution,
793
+ )
794
+ )
795
+ enhance_resolution = (
796
+ radius_save <= framework.resolution
797
+ and ratio <= constants[Constants.LOW_RATIO]
798
+ and not improve_geometry
799
+ )
800
+ else:
801
+ # When increasing the penalty parameter, the best point so far
802
+ # may change. In this case, we restart the iteration.
803
+ enhance_resolution = False
804
+ improve_geometry = False
805
+
806
+ # Reduce the resolution if necessary.
807
+ if enhance_resolution:
808
+ if framework.resolution <= options[Options.RHOEND]:
809
+ success = True
810
+ status = ExitStatus.RADIUS_SUCCESS
811
+ break
812
+ framework.enhance_resolution(options)
813
+ framework.decrease_penalty()
814
+
815
+ if verbose:
816
+ maxcv_val = pb.maxcv(
817
+ framework.x_best, framework.cub_best, framework.ceq_best
818
+ )
819
+ _print_step(
820
+ f"New trust-region radius: {framework.resolution}",
821
+ pb,
822
+ pb.build_x(framework.x_best),
823
+ framework.fun_best,
824
+ maxcv_val,
825
+ pb.n_eval,
826
+ n_iter,
827
+ )
828
+ print()
829
+
830
+ # Improve the geometry of the interpolation set if necessary.
831
+ if improve_geometry:
832
+ try:
833
+ step = framework.get_geometry_step(k_new, options)
834
+ except np.linalg.LinAlgError:
835
+ status = ExitStatus.LINALG_ERROR
836
+ break
837
+
838
+ # Evaluate the objective and constraint functions.
839
+ try:
840
+ fun_val, cub_val, ceq_val = _eval(pb, framework, step, options)
841
+ except TargetSuccess:
842
+ status = ExitStatus.TARGET_SUCCESS
843
+ success = True
844
+ break
845
+ except FeasibleSuccess:
846
+ status = ExitStatus.FEASIBLE_SUCCESS
847
+ success = True
848
+ break
849
+ except CallbackSuccess:
850
+ status = ExitStatus.CALLBACK_SUCCESS
851
+ success = True
852
+ break
853
+ except MaxEvalError:
854
+ status = ExitStatus.MAX_EVAL_WARNING
855
+ break
856
+
857
+ # Update the interpolation set.
858
+ try:
859
+ framework.models.update_interpolation(
860
+ k_new,
861
+ framework.x_best + step,
862
+ fun_val,
863
+ cub_val,
864
+ ceq_val,
865
+ )
866
+ except np.linalg.LinAlgError:
867
+ status = ExitStatus.LINALG_ERROR
868
+ break
869
+ framework.set_best_index()
870
+
871
+ return _build_result(
872
+ pb,
873
+ framework.penalty,
874
+ success,
875
+ status,
876
+ n_iter,
877
+ options,
878
+ )
879
+
880
+
881
+ def _get_bounds(bounds, n):
882
+ """
883
+ Uniformize the bounds.
884
+ """
885
+ if bounds is None:
886
+ return Bounds(np.full(n, -np.inf), np.full(n, np.inf))
887
+ elif isinstance(bounds, Bounds):
888
+ if bounds.lb.shape != (n,) or bounds.ub.shape != (n,):
889
+ raise ValueError(f"The bounds must have {n} elements.")
890
+ return Bounds(bounds.lb, bounds.ub)
891
+ elif hasattr(bounds, "__len__"):
892
+ bounds = np.asarray(bounds)
893
+ if bounds.shape != (n, 2):
894
+ raise ValueError(
895
+ "The shape of the bounds is not compatible with "
896
+ "the number of variables."
897
+ )
898
+ return Bounds(bounds[:, 0], bounds[:, 1])
899
+ else:
900
+ raise TypeError(
901
+ "The bounds must be an instance of "
902
+ "scipy.optimize.Bounds or an array-like object."
903
+ )
904
+
905
+
906
+ def _get_constraints(constraints):
907
+ """
908
+ Extract the linear and nonlinear constraints.
909
+ """
910
+ if isinstance(constraints, dict) or not hasattr(constraints, "__len__"):
911
+ constraints = (constraints,)
912
+
913
+ # Extract the linear and nonlinear constraints.
914
+ linear_constraints = []
915
+ nonlinear_constraints = []
916
+ for constraint in constraints:
917
+ if isinstance(constraint, LinearConstraint):
918
+ lb = exact_1d_array(
919
+ constraint.lb,
920
+ "The lower bound of the linear constraints must be a vector.",
921
+ )
922
+ ub = exact_1d_array(
923
+ constraint.ub,
924
+ "The upper bound of the linear constraints must be a vector.",
925
+ )
926
+ linear_constraints.append(
927
+ LinearConstraint(
928
+ constraint.A,
929
+ *np.broadcast_arrays(lb, ub),
930
+ )
931
+ )
932
+ elif isinstance(constraint, NonlinearConstraint):
933
+ lb = exact_1d_array(
934
+ constraint.lb,
935
+ "The lower bound of the "
936
+ "nonlinear constraints must be a "
937
+ "vector.",
938
+ )
939
+ ub = exact_1d_array(
940
+ constraint.ub,
941
+ "The upper bound of the "
942
+ "nonlinear constraints must be a "
943
+ "vector.",
944
+ )
945
+ nonlinear_constraints.append(
946
+ NonlinearConstraint(
947
+ constraint.fun,
948
+ *np.broadcast_arrays(lb, ub),
949
+ )
950
+ )
951
+ elif isinstance(constraint, dict):
952
+ if "type" not in constraint or constraint["type"] not in (
953
+ "eq",
954
+ "ineq",
955
+ ):
956
+ raise ValueError('The constraint type must be "eq" or "ineq".')
957
+ if "fun" not in constraint or not callable(constraint["fun"]):
958
+ raise ValueError("The constraint function must be callable.")
959
+ nonlinear_constraints.append(
960
+ {
961
+ "fun": constraint["fun"],
962
+ "type": constraint["type"],
963
+ "args": constraint.get("args", ()),
964
+ }
965
+ )
966
+ else:
967
+ raise TypeError(
968
+ "The constraints must be instances of "
969
+ "scipy.optimize.LinearConstraint, "
970
+ "scipy.optimize.NonlinearConstraint, or dict."
971
+ )
972
+ return linear_constraints, nonlinear_constraints
973
+
974
+
975
+ def _set_default_options(options, n):
976
+ """
977
+ Set the default options.
978
+ """
979
+ if Options.RHOBEG in options and options[Options.RHOBEG] <= 0.0:
980
+ raise ValueError("The initial trust-region radius must be positive.")
981
+ if Options.RHOEND in options and options[Options.RHOEND] < 0.0:
982
+ raise ValueError("The final trust-region radius must be nonnegative.")
983
+ if Options.RHOBEG in options and Options.RHOEND in options:
984
+ if options[Options.RHOBEG] < options[Options.RHOEND]:
985
+ raise ValueError(
986
+ "The initial trust-region radius must be greater "
987
+ "than or equal to the final trust-region radius."
988
+ )
989
+ elif Options.RHOBEG in options:
990
+ options[Options.RHOEND.value] = np.min(
991
+ [
992
+ DEFAULT_OPTIONS[Options.RHOEND],
993
+ options[Options.RHOBEG],
994
+ ]
995
+ )
996
+ elif Options.RHOEND in options:
997
+ options[Options.RHOBEG.value] = np.max(
998
+ [
999
+ DEFAULT_OPTIONS[Options.RHOBEG],
1000
+ options[Options.RHOEND],
1001
+ ]
1002
+ )
1003
+ else:
1004
+ options[Options.RHOBEG.value] = DEFAULT_OPTIONS[Options.RHOBEG]
1005
+ options[Options.RHOEND.value] = DEFAULT_OPTIONS[Options.RHOEND]
1006
+ options[Options.RHOBEG.value] = float(options[Options.RHOBEG])
1007
+ options[Options.RHOEND.value] = float(options[Options.RHOEND])
1008
+ if Options.NPT in options and options[Options.NPT] <= 0:
1009
+ raise ValueError("The number of interpolation points must be "
1010
+ "positive.")
1011
+ if (
1012
+ Options.NPT in options
1013
+ and options[Options.NPT] > ((n + 1) * (n + 2)) // 2
1014
+ ):
1015
+ raise ValueError(
1016
+ f"The number of interpolation points must be at most "
1017
+ f"{((n + 1) * (n + 2)) // 2}."
1018
+ )
1019
+ options.setdefault(Options.NPT.value, DEFAULT_OPTIONS[Options.NPT](n))
1020
+ options[Options.NPT.value] = int(options[Options.NPT])
1021
+ if Options.MAX_EVAL in options and options[Options.MAX_EVAL] <= 0:
1022
+ raise ValueError(
1023
+ "The maximum number of function evaluations must be positive."
1024
+ )
1025
+ options.setdefault(
1026
+ Options.MAX_EVAL.value,
1027
+ np.max(
1028
+ [
1029
+ DEFAULT_OPTIONS[Options.MAX_EVAL](n),
1030
+ options[Options.NPT] + 1,
1031
+ ]
1032
+ ),
1033
+ )
1034
+ options[Options.MAX_EVAL.value] = int(options[Options.MAX_EVAL])
1035
+ if Options.MAX_ITER in options and options[Options.MAX_ITER] <= 0:
1036
+ raise ValueError("The maximum number of iterations must be positive.")
1037
+ options.setdefault(
1038
+ Options.MAX_ITER.value,
1039
+ DEFAULT_OPTIONS[Options.MAX_ITER](n),
1040
+ )
1041
+ options[Options.MAX_ITER.value] = int(options[Options.MAX_ITER])
1042
+ options.setdefault(Options.TARGET.value, DEFAULT_OPTIONS[Options.TARGET])
1043
+ options[Options.TARGET.value] = float(options[Options.TARGET])
1044
+ options.setdefault(
1045
+ Options.FEASIBILITY_TOL.value,
1046
+ DEFAULT_OPTIONS[Options.FEASIBILITY_TOL],
1047
+ )
1048
+ options[Options.FEASIBILITY_TOL.value] = float(
1049
+ options[Options.FEASIBILITY_TOL]
1050
+ )
1051
+ options.setdefault(Options.VERBOSE.value, DEFAULT_OPTIONS[Options.VERBOSE])
1052
+ options[Options.VERBOSE.value] = bool(options[Options.VERBOSE])
1053
+ options.setdefault(Options.SCALE.value, DEFAULT_OPTIONS[Options.SCALE])
1054
+ options[Options.SCALE.value] = bool(options[Options.SCALE])
1055
+ options.setdefault(
1056
+ Options.FILTER_SIZE.value,
1057
+ DEFAULT_OPTIONS[Options.FILTER_SIZE],
1058
+ )
1059
+ options[Options.FILTER_SIZE.value] = int(options[Options.FILTER_SIZE])
1060
+ options.setdefault(
1061
+ Options.STORE_HISTORY.value,
1062
+ DEFAULT_OPTIONS[Options.STORE_HISTORY],
1063
+ )
1064
+ options[Options.STORE_HISTORY.value] = bool(options[Options.STORE_HISTORY])
1065
+ options.setdefault(
1066
+ Options.HISTORY_SIZE.value,
1067
+ DEFAULT_OPTIONS[Options.HISTORY_SIZE],
1068
+ )
1069
+ options[Options.HISTORY_SIZE.value] = int(options[Options.HISTORY_SIZE])
1070
+ options.setdefault(Options.DEBUG.value, DEFAULT_OPTIONS[Options.DEBUG])
1071
+ options[Options.DEBUG.value] = bool(options[Options.DEBUG])
1072
+
1073
+ # Check whether they are any unknown options.
1074
+ for key in options:
1075
+ if key not in Options.__members__.values():
1076
+ warnings.warn(f"Unknown option: {key}.", RuntimeWarning, 3)
1077
+
1078
+
1079
+ def _set_default_constants(**kwargs):
1080
+ """
1081
+ Set the default constants.
1082
+ """
1083
+ constants = dict(kwargs)
1084
+ constants.setdefault(
1085
+ Constants.DECREASE_RADIUS_FACTOR.value,
1086
+ DEFAULT_CONSTANTS[Constants.DECREASE_RADIUS_FACTOR],
1087
+ )
1088
+ constants[Constants.DECREASE_RADIUS_FACTOR.value] = float(
1089
+ constants[Constants.DECREASE_RADIUS_FACTOR]
1090
+ )
1091
+ if (
1092
+ constants[Constants.DECREASE_RADIUS_FACTOR] <= 0.0
1093
+ or constants[Constants.DECREASE_RADIUS_FACTOR] >= 1.0
1094
+ ):
1095
+ raise ValueError(
1096
+ "The constant decrease_radius_factor must be in the interval "
1097
+ "(0, 1)."
1098
+ )
1099
+ constants.setdefault(
1100
+ Constants.INCREASE_RADIUS_THRESHOLD.value,
1101
+ DEFAULT_CONSTANTS[Constants.INCREASE_RADIUS_THRESHOLD],
1102
+ )
1103
+ constants[Constants.INCREASE_RADIUS_THRESHOLD.value] = float(
1104
+ constants[Constants.INCREASE_RADIUS_THRESHOLD]
1105
+ )
1106
+ if constants[Constants.INCREASE_RADIUS_THRESHOLD] <= 1.0:
1107
+ raise ValueError(
1108
+ "The constant increase_radius_threshold must be greater than 1."
1109
+ )
1110
+ if (
1111
+ Constants.INCREASE_RADIUS_FACTOR in constants
1112
+ and constants[Constants.INCREASE_RADIUS_FACTOR] <= 1.0
1113
+ ):
1114
+ raise ValueError(
1115
+ "The constant increase_radius_factor must be greater than 1."
1116
+ )
1117
+ if (
1118
+ Constants.DECREASE_RADIUS_THRESHOLD in constants
1119
+ and constants[Constants.DECREASE_RADIUS_THRESHOLD] <= 1.0
1120
+ ):
1121
+ raise ValueError(
1122
+ "The constant decrease_radius_threshold must be greater than 1."
1123
+ )
1124
+ if (
1125
+ Constants.INCREASE_RADIUS_FACTOR in constants
1126
+ and Constants.DECREASE_RADIUS_THRESHOLD in constants
1127
+ ):
1128
+ if (
1129
+ constants[Constants.DECREASE_RADIUS_THRESHOLD]
1130
+ >= constants[Constants.INCREASE_RADIUS_FACTOR]
1131
+ ):
1132
+ raise ValueError(
1133
+ "The constant decrease_radius_threshold must be "
1134
+ "less than increase_radius_factor."
1135
+ )
1136
+ elif Constants.INCREASE_RADIUS_FACTOR in constants:
1137
+ constants[Constants.DECREASE_RADIUS_THRESHOLD.value] = np.min(
1138
+ [
1139
+ DEFAULT_CONSTANTS[Constants.DECREASE_RADIUS_THRESHOLD],
1140
+ 0.5 * (1.0 + constants[Constants.INCREASE_RADIUS_FACTOR]),
1141
+ ]
1142
+ )
1143
+ elif Constants.DECREASE_RADIUS_THRESHOLD in constants:
1144
+ constants[Constants.INCREASE_RADIUS_FACTOR.value] = np.max(
1145
+ [
1146
+ DEFAULT_CONSTANTS[Constants.INCREASE_RADIUS_FACTOR],
1147
+ 2.0 * constants[Constants.DECREASE_RADIUS_THRESHOLD],
1148
+ ]
1149
+ )
1150
+ else:
1151
+ constants[Constants.INCREASE_RADIUS_FACTOR.value] = DEFAULT_CONSTANTS[
1152
+ Constants.INCREASE_RADIUS_FACTOR
1153
+ ]
1154
+ constants[Constants.DECREASE_RADIUS_THRESHOLD.value] = (
1155
+ DEFAULT_CONSTANTS[Constants.DECREASE_RADIUS_THRESHOLD])
1156
+ constants.setdefault(
1157
+ Constants.DECREASE_RESOLUTION_FACTOR.value,
1158
+ DEFAULT_CONSTANTS[Constants.DECREASE_RESOLUTION_FACTOR],
1159
+ )
1160
+ constants[Constants.DECREASE_RESOLUTION_FACTOR.value] = float(
1161
+ constants[Constants.DECREASE_RESOLUTION_FACTOR]
1162
+ )
1163
+ if (
1164
+ constants[Constants.DECREASE_RESOLUTION_FACTOR] <= 0.0
1165
+ or constants[Constants.DECREASE_RESOLUTION_FACTOR] >= 1.0
1166
+ ):
1167
+ raise ValueError(
1168
+ "The constant decrease_resolution_factor must be in the interval "
1169
+ "(0, 1)."
1170
+ )
1171
+ if (
1172
+ Constants.LARGE_RESOLUTION_THRESHOLD in constants
1173
+ and constants[Constants.LARGE_RESOLUTION_THRESHOLD] <= 1.0
1174
+ ):
1175
+ raise ValueError(
1176
+ "The constant large_resolution_threshold must be greater than 1."
1177
+ )
1178
+ if (
1179
+ Constants.MODERATE_RESOLUTION_THRESHOLD in constants
1180
+ and constants[Constants.MODERATE_RESOLUTION_THRESHOLD] <= 1.0
1181
+ ):
1182
+ raise ValueError(
1183
+ "The constant moderate_resolution_threshold must be greater than "
1184
+ "1."
1185
+ )
1186
+ if (
1187
+ Constants.LARGE_RESOLUTION_THRESHOLD in constants
1188
+ and Constants.MODERATE_RESOLUTION_THRESHOLD in constants
1189
+ ):
1190
+ if (
1191
+ constants[Constants.MODERATE_RESOLUTION_THRESHOLD]
1192
+ > constants[Constants.LARGE_RESOLUTION_THRESHOLD]
1193
+ ):
1194
+ raise ValueError(
1195
+ "The constant moderate_resolution_threshold "
1196
+ "must be at most large_resolution_threshold."
1197
+ )
1198
+ elif Constants.LARGE_RESOLUTION_THRESHOLD in constants:
1199
+ constants[Constants.MODERATE_RESOLUTION_THRESHOLD.value] = np.min(
1200
+ [
1201
+ DEFAULT_CONSTANTS[Constants.MODERATE_RESOLUTION_THRESHOLD],
1202
+ constants[Constants.LARGE_RESOLUTION_THRESHOLD],
1203
+ ]
1204
+ )
1205
+ elif Constants.MODERATE_RESOLUTION_THRESHOLD in constants:
1206
+ constants[Constants.LARGE_RESOLUTION_THRESHOLD.value] = np.max(
1207
+ [
1208
+ DEFAULT_CONSTANTS[Constants.LARGE_RESOLUTION_THRESHOLD],
1209
+ constants[Constants.MODERATE_RESOLUTION_THRESHOLD],
1210
+ ]
1211
+ )
1212
+ else:
1213
+ constants[Constants.LARGE_RESOLUTION_THRESHOLD.value] = (
1214
+ DEFAULT_CONSTANTS[Constants.LARGE_RESOLUTION_THRESHOLD]
1215
+ )
1216
+ constants[Constants.MODERATE_RESOLUTION_THRESHOLD.value] = (
1217
+ DEFAULT_CONSTANTS[Constants.MODERATE_RESOLUTION_THRESHOLD]
1218
+ )
1219
+ if Constants.LOW_RATIO in constants and (
1220
+ constants[Constants.LOW_RATIO] <= 0.0
1221
+ or constants[Constants.LOW_RATIO] >= 1.0
1222
+ ):
1223
+ raise ValueError(
1224
+ "The constant low_ratio must be in the interval (0, 1)."
1225
+ )
1226
+ if Constants.HIGH_RATIO in constants and (
1227
+ constants[Constants.HIGH_RATIO] <= 0.0
1228
+ or constants[Constants.HIGH_RATIO] >= 1.0
1229
+ ):
1230
+ raise ValueError(
1231
+ "The constant high_ratio must be in the interval (0, 1)."
1232
+ )
1233
+ if Constants.LOW_RATIO in constants and Constants.HIGH_RATIO in constants:
1234
+ if constants[Constants.LOW_RATIO] > constants[Constants.HIGH_RATIO]:
1235
+ raise ValueError(
1236
+ "The constant low_ratio must be at most high_ratio."
1237
+ )
1238
+ elif Constants.LOW_RATIO in constants:
1239
+ constants[Constants.HIGH_RATIO.value] = np.max(
1240
+ [
1241
+ DEFAULT_CONSTANTS[Constants.HIGH_RATIO],
1242
+ constants[Constants.LOW_RATIO],
1243
+ ]
1244
+ )
1245
+ elif Constants.HIGH_RATIO in constants:
1246
+ constants[Constants.LOW_RATIO.value] = np.min(
1247
+ [
1248
+ DEFAULT_CONSTANTS[Constants.LOW_RATIO],
1249
+ constants[Constants.HIGH_RATIO],
1250
+ ]
1251
+ )
1252
+ else:
1253
+ constants[Constants.LOW_RATIO.value] = DEFAULT_CONSTANTS[
1254
+ Constants.LOW_RATIO
1255
+ ]
1256
+ constants[Constants.HIGH_RATIO.value] = DEFAULT_CONSTANTS[
1257
+ Constants.HIGH_RATIO
1258
+ ]
1259
+ constants.setdefault(
1260
+ Constants.VERY_LOW_RATIO.value,
1261
+ DEFAULT_CONSTANTS[Constants.VERY_LOW_RATIO],
1262
+ )
1263
+ constants[Constants.VERY_LOW_RATIO.value] = float(
1264
+ constants[Constants.VERY_LOW_RATIO]
1265
+ )
1266
+ if (
1267
+ constants[Constants.VERY_LOW_RATIO] <= 0.0
1268
+ or constants[Constants.VERY_LOW_RATIO] >= 1.0
1269
+ ):
1270
+ raise ValueError(
1271
+ "The constant very_low_ratio must be in the interval (0, 1)."
1272
+ )
1273
+ if (
1274
+ Constants.PENALTY_INCREASE_THRESHOLD in constants
1275
+ and constants[Constants.PENALTY_INCREASE_THRESHOLD] < 1.0
1276
+ ):
1277
+ raise ValueError(
1278
+ "The constant penalty_increase_threshold must be "
1279
+ "greater than or equal to 1."
1280
+ )
1281
+ if (
1282
+ Constants.PENALTY_INCREASE_FACTOR in constants
1283
+ and constants[Constants.PENALTY_INCREASE_FACTOR] <= 1.0
1284
+ ):
1285
+ raise ValueError(
1286
+ "The constant penalty_increase_factor must be greater than 1."
1287
+ )
1288
+ if (
1289
+ Constants.PENALTY_INCREASE_THRESHOLD in constants
1290
+ and Constants.PENALTY_INCREASE_FACTOR in constants
1291
+ ):
1292
+ if (
1293
+ constants[Constants.PENALTY_INCREASE_FACTOR]
1294
+ < constants[Constants.PENALTY_INCREASE_THRESHOLD]
1295
+ ):
1296
+ raise ValueError(
1297
+ "The constant penalty_increase_factor must be "
1298
+ "greater than or equal to "
1299
+ "penalty_increase_threshold."
1300
+ )
1301
+ elif Constants.PENALTY_INCREASE_THRESHOLD in constants:
1302
+ constants[Constants.PENALTY_INCREASE_FACTOR.value] = np.max(
1303
+ [
1304
+ DEFAULT_CONSTANTS[Constants.PENALTY_INCREASE_FACTOR],
1305
+ constants[Constants.PENALTY_INCREASE_THRESHOLD],
1306
+ ]
1307
+ )
1308
+ elif Constants.PENALTY_INCREASE_FACTOR in constants:
1309
+ constants[Constants.PENALTY_INCREASE_THRESHOLD.value] = np.min(
1310
+ [
1311
+ DEFAULT_CONSTANTS[Constants.PENALTY_INCREASE_THRESHOLD],
1312
+ constants[Constants.PENALTY_INCREASE_FACTOR],
1313
+ ]
1314
+ )
1315
+ else:
1316
+ constants[Constants.PENALTY_INCREASE_THRESHOLD.value] = (
1317
+ DEFAULT_CONSTANTS[Constants.PENALTY_INCREASE_THRESHOLD]
1318
+ )
1319
+ constants[Constants.PENALTY_INCREASE_FACTOR.value] = DEFAULT_CONSTANTS[
1320
+ Constants.PENALTY_INCREASE_FACTOR
1321
+ ]
1322
+ constants.setdefault(
1323
+ Constants.SHORT_STEP_THRESHOLD.value,
1324
+ DEFAULT_CONSTANTS[Constants.SHORT_STEP_THRESHOLD],
1325
+ )
1326
+ constants[Constants.SHORT_STEP_THRESHOLD.value] = float(
1327
+ constants[Constants.SHORT_STEP_THRESHOLD]
1328
+ )
1329
+ if (
1330
+ constants[Constants.SHORT_STEP_THRESHOLD] <= 0.0
1331
+ or constants[Constants.SHORT_STEP_THRESHOLD] >= 1.0
1332
+ ):
1333
+ raise ValueError(
1334
+ "The constant short_step_threshold must be in the interval (0, 1)."
1335
+ )
1336
+ constants.setdefault(
1337
+ Constants.LOW_RADIUS_FACTOR.value,
1338
+ DEFAULT_CONSTANTS[Constants.LOW_RADIUS_FACTOR],
1339
+ )
1340
+ constants[Constants.LOW_RADIUS_FACTOR.value] = float(
1341
+ constants[Constants.LOW_RADIUS_FACTOR]
1342
+ )
1343
+ if (
1344
+ constants[Constants.LOW_RADIUS_FACTOR] <= 0.0
1345
+ or constants[Constants.LOW_RADIUS_FACTOR] >= 1.0
1346
+ ):
1347
+ raise ValueError(
1348
+ "The constant low_radius_factor must be in the interval (0, 1)."
1349
+ )
1350
+ constants.setdefault(
1351
+ Constants.BYRD_OMOJOKUN_FACTOR.value,
1352
+ DEFAULT_CONSTANTS[Constants.BYRD_OMOJOKUN_FACTOR],
1353
+ )
1354
+ constants[Constants.BYRD_OMOJOKUN_FACTOR.value] = float(
1355
+ constants[Constants.BYRD_OMOJOKUN_FACTOR]
1356
+ )
1357
+ if (
1358
+ constants[Constants.BYRD_OMOJOKUN_FACTOR] <= 0.0
1359
+ or constants[Constants.BYRD_OMOJOKUN_FACTOR] >= 1.0
1360
+ ):
1361
+ raise ValueError(
1362
+ "The constant byrd_omojokun_factor must be in the interval (0, 1)."
1363
+ )
1364
+ constants.setdefault(
1365
+ Constants.THRESHOLD_RATIO_CONSTRAINTS.value,
1366
+ DEFAULT_CONSTANTS[Constants.THRESHOLD_RATIO_CONSTRAINTS],
1367
+ )
1368
+ constants[Constants.THRESHOLD_RATIO_CONSTRAINTS.value] = float(
1369
+ constants[Constants.THRESHOLD_RATIO_CONSTRAINTS]
1370
+ )
1371
+ if constants[Constants.THRESHOLD_RATIO_CONSTRAINTS] <= 1.0:
1372
+ raise ValueError(
1373
+ "The constant threshold_ratio_constraints must be greater than 1."
1374
+ )
1375
+ constants.setdefault(
1376
+ Constants.LARGE_SHIFT_FACTOR.value,
1377
+ DEFAULT_CONSTANTS[Constants.LARGE_SHIFT_FACTOR],
1378
+ )
1379
+ constants[Constants.LARGE_SHIFT_FACTOR.value] = float(
1380
+ constants[Constants.LARGE_SHIFT_FACTOR]
1381
+ )
1382
+ if constants[Constants.LARGE_SHIFT_FACTOR] < 0.0:
1383
+ raise ValueError("The constant large_shift_factor must be "
1384
+ "nonnegative.")
1385
+ constants.setdefault(
1386
+ Constants.LARGE_GRADIENT_FACTOR.value,
1387
+ DEFAULT_CONSTANTS[Constants.LARGE_GRADIENT_FACTOR],
1388
+ )
1389
+ constants[Constants.LARGE_GRADIENT_FACTOR.value] = float(
1390
+ constants[Constants.LARGE_GRADIENT_FACTOR]
1391
+ )
1392
+ if constants[Constants.LARGE_GRADIENT_FACTOR] <= 1.0:
1393
+ raise ValueError(
1394
+ "The constant large_gradient_factor must be greater than 1."
1395
+ )
1396
+ constants.setdefault(
1397
+ Constants.RESOLUTION_FACTOR.value,
1398
+ DEFAULT_CONSTANTS[Constants.RESOLUTION_FACTOR],
1399
+ )
1400
+ constants[Constants.RESOLUTION_FACTOR.value] = float(
1401
+ constants[Constants.RESOLUTION_FACTOR]
1402
+ )
1403
+ if constants[Constants.RESOLUTION_FACTOR] <= 1.0:
1404
+ raise ValueError(
1405
+ "The constant resolution_factor must be greater than 1."
1406
+ )
1407
+ constants.setdefault(
1408
+ Constants.IMPROVE_TCG.value,
1409
+ DEFAULT_CONSTANTS[Constants.IMPROVE_TCG],
1410
+ )
1411
+ constants[Constants.IMPROVE_TCG.value] = bool(
1412
+ constants[Constants.IMPROVE_TCG]
1413
+ )
1414
+
1415
+ # Check whether they are any unknown options.
1416
+ for key in kwargs:
1417
+ if key not in Constants.__members__.values():
1418
+ warnings.warn(f"Unknown constant: {key}.", RuntimeWarning, 3)
1419
+ return constants
1420
+
1421
+
1422
+ def _eval(pb, framework, step, options):
1423
+ """
1424
+ Evaluate the objective and constraint functions.
1425
+ """
1426
+ if pb.n_eval >= options[Options.MAX_EVAL]:
1427
+ raise MaxEvalError
1428
+ x_eval = framework.x_best + step
1429
+ fun_val, cub_val, ceq_val = pb(x_eval, framework.penalty)
1430
+ r_val = pb.maxcv(x_eval, cub_val, ceq_val)
1431
+ if (
1432
+ fun_val <= options[Options.TARGET]
1433
+ and r_val <= options[Options.FEASIBILITY_TOL]
1434
+ ):
1435
+ raise TargetSuccess
1436
+ if pb.is_feasibility and r_val <= options[Options.FEASIBILITY_TOL]:
1437
+ raise FeasibleSuccess
1438
+ return fun_val, cub_val, ceq_val
1439
+
1440
+
1441
+ def _build_result(pb, penalty, success, status, n_iter, options):
1442
+ """
1443
+ Build the result of the optimization process.
1444
+ """
1445
+ # Build the result.
1446
+ x, fun, maxcv = pb.best_eval(penalty)
1447
+ success = success and np.isfinite(fun) and np.isfinite(maxcv)
1448
+ if status not in [ExitStatus.TARGET_SUCCESS, ExitStatus.FEASIBLE_SUCCESS]:
1449
+ success = success and maxcv <= options[Options.FEASIBILITY_TOL]
1450
+ result = OptimizeResult()
1451
+ result.message = {
1452
+ ExitStatus.RADIUS_SUCCESS: "The lower bound for the trust-region "
1453
+ "radius has been reached",
1454
+ ExitStatus.TARGET_SUCCESS: "The target objective function value has "
1455
+ "been reached",
1456
+ ExitStatus.FIXED_SUCCESS: "All variables are fixed by the bound "
1457
+ "constraints",
1458
+ ExitStatus.CALLBACK_SUCCESS: "The callback requested to stop the "
1459
+ "optimization procedure",
1460
+ ExitStatus.FEASIBLE_SUCCESS: "The feasibility problem received has "
1461
+ "been solved successfully",
1462
+ ExitStatus.MAX_EVAL_WARNING: "The maximum number of function "
1463
+ "evaluations has been exceeded",
1464
+ ExitStatus.MAX_ITER_WARNING: "The maximum number of iterations has "
1465
+ "been exceeded",
1466
+ ExitStatus.INFEASIBLE_ERROR: "The bound constraints are infeasible",
1467
+ ExitStatus.LINALG_ERROR: "A linear algebra error occurred",
1468
+ }.get(status, "Unknown exit status")
1469
+ result.success = success
1470
+ result.status = status.value
1471
+ result.x = pb.build_x(x)
1472
+ result.fun = fun
1473
+ result.maxcv = maxcv
1474
+ result.nfev = pb.n_eval
1475
+ result.nit = n_iter
1476
+ if options[Options.STORE_HISTORY]:
1477
+ result.fun_history = pb.fun_history
1478
+ result.maxcv_history = pb.maxcv_history
1479
+
1480
+ # Print the result if requested.
1481
+ if options[Options.VERBOSE]:
1482
+ _print_step(
1483
+ result.message,
1484
+ pb,
1485
+ result.x,
1486
+ result.fun,
1487
+ result.maxcv,
1488
+ result.nfev,
1489
+ result.nit,
1490
+ )
1491
+ return result
1492
+
1493
+
1494
+ def _print_step(message, pb, x, fun_val, r_val, n_eval, n_iter):
1495
+ """
1496
+ Print information about the current state of the optimization process.
1497
+ """
1498
+ print()
1499
+ print(f"{message}.")
1500
+ print(f"Number of function evaluations: {n_eval}.")
1501
+ print(f"Number of iterations: {n_iter}.")
1502
+ if not pb.is_feasibility:
1503
+ print(f"Least value of {pb.fun_name}: {fun_val}.")
1504
+ print(f"Maximum constraint violation: {r_val}.")
1505
+ with np.printoptions(**PRINT_OPTIONS):
1506
+ print(f"Corresponding point: {x}.")
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/models.py ADDED
@@ -0,0 +1,1529 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import warnings
2
+
3
+ import numpy as np
4
+ from scipy.linalg import eigh
5
+
6
+ from .settings import Options
7
+ from .utils import MaxEvalError, TargetSuccess, FeasibleSuccess
8
+
9
+
10
+ EPS = np.finfo(float).eps
11
+
12
+
13
+ class Interpolation:
14
+ """
15
+ Interpolation set.
16
+
17
+ This class stores a base point around which the models are expanded and the
18
+ interpolation points. The coordinates of the interpolation points are
19
+ relative to the base point.
20
+ """
21
+
22
+ def __init__(self, pb, options):
23
+ """
24
+ Initialize the interpolation set.
25
+
26
+ Parameters
27
+ ----------
28
+ pb : `cobyqa.problem.Problem`
29
+ Problem to be solved.
30
+ options : dict
31
+ Options of the solver.
32
+ """
33
+ # Reduce the initial trust-region radius if necessary.
34
+ self._debug = options[Options.DEBUG]
35
+ max_radius = 0.5 * np.min(pb.bounds.xu - pb.bounds.xl)
36
+ if options[Options.RHOBEG] > max_radius:
37
+ options[Options.RHOBEG.value] = max_radius
38
+ options[Options.RHOEND.value] = np.min(
39
+ [
40
+ options[Options.RHOEND],
41
+ max_radius,
42
+ ]
43
+ )
44
+
45
+ # Set the initial point around which the models are expanded.
46
+ self._x_base = np.copy(pb.x0)
47
+ very_close_xl_idx = (
48
+ self.x_base <= pb.bounds.xl + 0.5 * options[Options.RHOBEG]
49
+ )
50
+ self.x_base[very_close_xl_idx] = pb.bounds.xl[very_close_xl_idx]
51
+ close_xl_idx = (
52
+ pb.bounds.xl + 0.5 * options[Options.RHOBEG] < self.x_base
53
+ ) & (self.x_base <= pb.bounds.xl + options[Options.RHOBEG])
54
+ self.x_base[close_xl_idx] = np.minimum(
55
+ pb.bounds.xl[close_xl_idx] + options[Options.RHOBEG],
56
+ pb.bounds.xu[close_xl_idx],
57
+ )
58
+ very_close_xu_idx = (
59
+ self.x_base >= pb.bounds.xu - 0.5 * options[Options.RHOBEG]
60
+ )
61
+ self.x_base[very_close_xu_idx] = pb.bounds.xu[very_close_xu_idx]
62
+ close_xu_idx = (
63
+ self.x_base < pb.bounds.xu - 0.5 * options[Options.RHOBEG]
64
+ ) & (pb.bounds.xu - options[Options.RHOBEG] <= self.x_base)
65
+ self.x_base[close_xu_idx] = np.maximum(
66
+ pb.bounds.xu[close_xu_idx] - options[Options.RHOBEG],
67
+ pb.bounds.xl[close_xu_idx],
68
+ )
69
+
70
+ # Set the initial interpolation set.
71
+ self._xpt = np.zeros((pb.n, options[Options.NPT]))
72
+ for k in range(1, options[Options.NPT]):
73
+ if k <= pb.n:
74
+ if very_close_xu_idx[k - 1]:
75
+ self.xpt[k - 1, k] = -options[Options.RHOBEG]
76
+ else:
77
+ self.xpt[k - 1, k] = options[Options.RHOBEG]
78
+ elif k <= 2 * pb.n:
79
+ if very_close_xl_idx[k - pb.n - 1]:
80
+ self.xpt[k - pb.n - 1, k] = 2.0 * options[Options.RHOBEG]
81
+ elif very_close_xu_idx[k - pb.n - 1]:
82
+ self.xpt[k - pb.n - 1, k] = -2.0 * options[Options.RHOBEG]
83
+ else:
84
+ self.xpt[k - pb.n - 1, k] = -options[Options.RHOBEG]
85
+ else:
86
+ spread = (k - pb.n - 1) // pb.n
87
+ k1 = k - (1 + spread) * pb.n - 1
88
+ k2 = (k1 + spread) % pb.n
89
+ self.xpt[k1, k] = self.xpt[k1, k1 + 1]
90
+ self.xpt[k2, k] = self.xpt[k2, k2 + 1]
91
+
92
+ @property
93
+ def n(self):
94
+ """
95
+ Number of variables.
96
+
97
+ Returns
98
+ -------
99
+ int
100
+ Number of variables.
101
+ """
102
+ return self.xpt.shape[0]
103
+
104
+ @property
105
+ def npt(self):
106
+ """
107
+ Number of interpolation points.
108
+
109
+ Returns
110
+ -------
111
+ int
112
+ Number of interpolation points.
113
+ """
114
+ return self.xpt.shape[1]
115
+
116
+ @property
117
+ def xpt(self):
118
+ """
119
+ Interpolation points.
120
+
121
+ Returns
122
+ -------
123
+ `numpy.ndarray`, shape (n, npt)
124
+ Interpolation points.
125
+ """
126
+ return self._xpt
127
+
128
+ @xpt.setter
129
+ def xpt(self, xpt):
130
+ """
131
+ Set the interpolation points.
132
+
133
+ Parameters
134
+ ----------
135
+ xpt : `numpy.ndarray`, shape (n, npt)
136
+ New interpolation points.
137
+ """
138
+ if self._debug:
139
+ assert xpt.shape == (
140
+ self.n,
141
+ self.npt,
142
+ ), "The shape of `xpt` is not valid."
143
+ self._xpt = xpt
144
+
145
+ @property
146
+ def x_base(self):
147
+ """
148
+ Base point around which the models are expanded.
149
+
150
+ Returns
151
+ -------
152
+ `numpy.ndarray`, shape (n,)
153
+ Base point around which the models are expanded.
154
+ """
155
+ return self._x_base
156
+
157
+ @x_base.setter
158
+ def x_base(self, x_base):
159
+ """
160
+ Set the base point around which the models are expanded.
161
+
162
+ Parameters
163
+ ----------
164
+ x_base : `numpy.ndarray`, shape (n,)
165
+ New base point around which the models are expanded.
166
+ """
167
+ if self._debug:
168
+ assert x_base.shape == (
169
+ self.n,
170
+ ), "The shape of `x_base` is not valid."
171
+ self._x_base = x_base
172
+
173
+ def point(self, k):
174
+ """
175
+ Get the `k`-th interpolation point.
176
+
177
+ The return point is relative to the origin.
178
+
179
+ Parameters
180
+ ----------
181
+ k : int
182
+ Index of the interpolation point.
183
+
184
+ Returns
185
+ -------
186
+ `numpy.ndarray`, shape (n,)
187
+ `k`-th interpolation point.
188
+ """
189
+ if self._debug:
190
+ assert 0 <= k < self.npt, "The index `k` is not valid."
191
+ return self.x_base + self.xpt[:, k]
192
+
193
+
194
+ _cache = {"xpt": None, "a": None, "right_scaling": None, "eigh": None}
195
+
196
+
197
+ def build_system(interpolation):
198
+ """
199
+ Build the left-hand side matrix of the interpolation system. The
200
+ matrix below stores W * diag(right_scaling),
201
+ where W is the theoretical matrix of the interpolation system. The
202
+ right scaling matrices is chosen to keep the elements in
203
+ the matrix well-balanced.
204
+
205
+ Parameters
206
+ ----------
207
+ interpolation : `cobyqa.models.Interpolation`
208
+ Interpolation set.
209
+ """
210
+
211
+ # Compute the scaled directions from the base point to the
212
+ # interpolation points. We scale the directions to avoid numerical
213
+ # difficulties.
214
+ if _cache["xpt"] is not None and np.array_equal(
215
+ interpolation.xpt, _cache["xpt"]
216
+ ):
217
+ return _cache["a"], _cache["right_scaling"], _cache["eigh"]
218
+
219
+ scale = np.max(np.linalg.norm(interpolation.xpt, axis=0), initial=EPS)
220
+ xpt_scale = interpolation.xpt / scale
221
+
222
+ n, npt = xpt_scale.shape
223
+ a = np.zeros((npt + n + 1, npt + n + 1))
224
+ a[:npt, :npt] = 0.5 * (xpt_scale.T @ xpt_scale) ** 2.0
225
+ a[:npt, npt] = 1.0
226
+ a[:npt, npt + 1:] = xpt_scale.T
227
+ a[npt, :npt] = 1.0
228
+ a[npt + 1:, :npt] = xpt_scale
229
+
230
+ # Build the left and right scaling diagonal matrices.
231
+ right_scaling = np.empty(npt + n + 1)
232
+ right_scaling[:npt] = 1.0 / scale**2.0
233
+ right_scaling[npt] = scale**2.0
234
+ right_scaling[npt + 1:] = scale
235
+
236
+ eig_values, eig_vectors = eigh(a, check_finite=False)
237
+
238
+ _cache["xpt"] = np.copy(interpolation.xpt)
239
+ _cache["a"] = np.copy(a)
240
+ _cache["right_scaling"] = np.copy(right_scaling)
241
+ _cache["eigh"] = (eig_values, eig_vectors)
242
+
243
+ return a, right_scaling, (eig_values, eig_vectors)
244
+
245
+
246
+ class Quadratic:
247
+ """
248
+ Quadratic model.
249
+
250
+ This class stores the Hessian matrix of the quadratic model using the
251
+ implicit/explicit representation designed by Powell for NEWUOA [1]_.
252
+
253
+ References
254
+ ----------
255
+ .. [1] M. J. D. Powell. The NEWUOA software for unconstrained optimization
256
+ without derivatives. In G. Di Pillo and M. Roma, editors, *Large-Scale
257
+ Nonlinear Optimization*, volume 83 of Nonconvex Optim. Appl., pages
258
+ 255--297. Springer, Boston, MA, USA, 2006. `doi:10.1007/0-387-30065-1_16
259
+ <https://doi.org/10.1007/0-387-30065-1_16>`_.
260
+ """
261
+
262
+ def __init__(self, interpolation, values, debug):
263
+ """
264
+ Initialize the quadratic model.
265
+
266
+ Parameters
267
+ ----------
268
+ interpolation : `cobyqa.models.Interpolation`
269
+ Interpolation set.
270
+ values : `numpy.ndarray`, shape (npt,)
271
+ Values of the interpolated function at the interpolation points.
272
+ debug : bool
273
+ Whether to make debugging tests during the execution.
274
+
275
+ Raises
276
+ ------
277
+ `numpy.linalg.LinAlgError`
278
+ If the interpolation system is ill-defined.
279
+ """
280
+ self._debug = debug
281
+ if self._debug:
282
+ assert values.shape == (
283
+ interpolation.npt,
284
+ ), "The shape of `values` is not valid."
285
+ if interpolation.npt < interpolation.n + 1:
286
+ raise ValueError(
287
+ f"The number of interpolation points must be at least "
288
+ f"{interpolation.n + 1}."
289
+ )
290
+ self._const, self._grad, self._i_hess, _ = self._get_model(
291
+ interpolation,
292
+ values,
293
+ )
294
+ self._e_hess = np.zeros((self.n, self.n))
295
+
296
+ def __call__(self, x, interpolation):
297
+ """
298
+ Evaluate the quadratic model at a given point.
299
+
300
+ Parameters
301
+ ----------
302
+ x : `numpy.ndarray`, shape (n,)
303
+ Point at which the quadratic model is evaluated.
304
+ interpolation : `cobyqa.models.Interpolation`
305
+ Interpolation set.
306
+
307
+ Returns
308
+ -------
309
+ float
310
+ Value of the quadratic model at `x`.
311
+ """
312
+ if self._debug:
313
+ assert x.shape == (self.n,), "The shape of `x` is not valid."
314
+ x_diff = x - interpolation.x_base
315
+ return (
316
+ self._const
317
+ + self._grad @ x_diff
318
+ + 0.5
319
+ * (
320
+ self._i_hess @ (interpolation.xpt.T @ x_diff) ** 2.0
321
+ + x_diff @ self._e_hess @ x_diff
322
+ )
323
+ )
324
+
325
+ @property
326
+ def n(self):
327
+ """
328
+ Number of variables.
329
+
330
+ Returns
331
+ -------
332
+ int
333
+ Number of variables.
334
+ """
335
+ return self._grad.size
336
+
337
+ @property
338
+ def npt(self):
339
+ """
340
+ Number of interpolation points used to define the quadratic model.
341
+
342
+ Returns
343
+ -------
344
+ int
345
+ Number of interpolation points used to define the quadratic model.
346
+ """
347
+ return self._i_hess.size
348
+
349
+ def grad(self, x, interpolation):
350
+ """
351
+ Evaluate the gradient of the quadratic model at a given point.
352
+
353
+ Parameters
354
+ ----------
355
+ x : `numpy.ndarray`, shape (n,)
356
+ Point at which the gradient of the quadratic model is evaluated.
357
+ interpolation : `cobyqa.models.Interpolation`
358
+ Interpolation set.
359
+
360
+ Returns
361
+ -------
362
+ `numpy.ndarray`, shape (n,)
363
+ Gradient of the quadratic model at `x`.
364
+ """
365
+ if self._debug:
366
+ assert x.shape == (self.n,), "The shape of `x` is not valid."
367
+ x_diff = x - interpolation.x_base
368
+ return self._grad + self.hess_prod(x_diff, interpolation)
369
+
370
+ def hess(self, interpolation):
371
+ """
372
+ Evaluate the Hessian matrix of the quadratic model.
373
+
374
+ Parameters
375
+ ----------
376
+ interpolation : `cobyqa.models.Interpolation`
377
+ Interpolation set.
378
+
379
+ Returns
380
+ -------
381
+ `numpy.ndarray`, shape (n, n)
382
+ Hessian matrix of the quadratic model.
383
+ """
384
+ return self._e_hess + interpolation.xpt @ (
385
+ self._i_hess[:, np.newaxis] * interpolation.xpt.T
386
+ )
387
+
388
+ def hess_prod(self, v, interpolation):
389
+ """
390
+ Evaluate the right product of the Hessian matrix of the quadratic model
391
+ with a given vector.
392
+
393
+ Parameters
394
+ ----------
395
+ v : `numpy.ndarray`, shape (n,)
396
+ Vector with which the Hessian matrix of the quadratic model is
397
+ multiplied from the right.
398
+ interpolation : `cobyqa.models.Interpolation`
399
+ Interpolation set.
400
+
401
+ Returns
402
+ -------
403
+ `numpy.ndarray`, shape (n,)
404
+ Right product of the Hessian matrix of the quadratic model with
405
+ `v`.
406
+ """
407
+ if self._debug:
408
+ assert v.shape == (self.n,), "The shape of `v` is not valid."
409
+ return self._e_hess @ v + interpolation.xpt @ (
410
+ self._i_hess * (interpolation.xpt.T @ v)
411
+ )
412
+
413
+ def curv(self, v, interpolation):
414
+ """
415
+ Evaluate the curvature of the quadratic model along a given direction.
416
+
417
+ Parameters
418
+ ----------
419
+ v : `numpy.ndarray`, shape (n,)
420
+ Direction along which the curvature of the quadratic model is
421
+ evaluated.
422
+ interpolation : `cobyqa.models.Interpolation`
423
+ Interpolation set.
424
+
425
+ Returns
426
+ -------
427
+ float
428
+ Curvature of the quadratic model along `v`.
429
+ """
430
+ if self._debug:
431
+ assert v.shape == (self.n,), "The shape of `v` is not valid."
432
+ return (
433
+ v @ self._e_hess @ v
434
+ + self._i_hess @ (interpolation.xpt.T @ v) ** 2.0
435
+ )
436
+
437
+ def update(self, interpolation, k_new, dir_old, values_diff):
438
+ """
439
+ Update the quadratic model.
440
+
441
+ This method applies the derivative-free symmetric Broyden update to the
442
+ quadratic model. The `knew`-th interpolation point must be updated
443
+ before calling this method.
444
+
445
+ Parameters
446
+ ----------
447
+ interpolation : `cobyqa.models.Interpolation`
448
+ Updated interpolation set.
449
+ k_new : int
450
+ Index of the updated interpolation point.
451
+ dir_old : `numpy.ndarray`, shape (n,)
452
+ Value of ``interpolation.xpt[:, k_new]`` before the update.
453
+ values_diff : `numpy.ndarray`, shape (npt,)
454
+ Differences between the values of the interpolated nonlinear
455
+ function and the previous quadratic model at the updated
456
+ interpolation points.
457
+
458
+ Raises
459
+ ------
460
+ `numpy.linalg.LinAlgError`
461
+ If the interpolation system is ill-defined.
462
+ """
463
+ if self._debug:
464
+ assert 0 <= k_new < self.npt, "The index `k_new` is not valid."
465
+ assert dir_old.shape == (
466
+ self.n,
467
+ ), "The shape of `dir_old` is not valid."
468
+ assert values_diff.shape == (
469
+ self.npt,
470
+ ), "The shape of `values_diff` is not valid."
471
+
472
+ # Forward the k_new-th element of the implicit Hessian matrix to the
473
+ # explicit Hessian matrix. This must be done because the implicit
474
+ # Hessian matrix is related to the interpolation points, and the
475
+ # k_new-th interpolation point is modified.
476
+ self._e_hess += self._i_hess[k_new] * np.outer(dir_old, dir_old)
477
+ self._i_hess[k_new] = 0.0
478
+
479
+ # Update the quadratic model.
480
+ const, grad, i_hess, ill_conditioned = self._get_model(
481
+ interpolation,
482
+ values_diff,
483
+ )
484
+ self._const += const
485
+ self._grad += grad
486
+ self._i_hess += i_hess
487
+ return ill_conditioned
488
+
489
+ def shift_x_base(self, interpolation, new_x_base):
490
+ """
491
+ Shift the point around which the quadratic model is defined.
492
+
493
+ Parameters
494
+ ----------
495
+ interpolation : `cobyqa.models.Interpolation`
496
+ Previous interpolation set.
497
+ new_x_base : `numpy.ndarray`, shape (n,)
498
+ Point that will replace ``interpolation.x_base``.
499
+ """
500
+ if self._debug:
501
+ assert new_x_base.shape == (
502
+ self.n,
503
+ ), "The shape of `new_x_base` is not valid."
504
+ self._const = self(new_x_base, interpolation)
505
+ self._grad = self.grad(new_x_base, interpolation)
506
+ shift = new_x_base - interpolation.x_base
507
+ update = np.outer(
508
+ shift,
509
+ (interpolation.xpt - 0.5 * shift[:, np.newaxis]) @ self._i_hess,
510
+ )
511
+ self._e_hess += update + update.T
512
+
513
+ @staticmethod
514
+ def solve_systems(interpolation, rhs):
515
+ """
516
+ Solve the interpolation systems.
517
+
518
+ Parameters
519
+ ----------
520
+ interpolation : `cobyqa.models.Interpolation`
521
+ Interpolation set.
522
+ rhs : `numpy.ndarray`, shape (npt + n + 1, m)
523
+ Right-hand side vectors of the ``m`` interpolation systems.
524
+
525
+ Returns
526
+ -------
527
+ `numpy.ndarray`, shape (npt + n + 1, m)
528
+ Solutions of the interpolation systems.
529
+ `numpy.ndarray`, shape (m, )
530
+ Whether the interpolation systems are ill-conditioned.
531
+
532
+ Raises
533
+ ------
534
+ `numpy.linalg.LinAlgError`
535
+ If the interpolation systems are ill-defined.
536
+ """
537
+ n, npt = interpolation.xpt.shape
538
+ assert (
539
+ rhs.ndim == 2 and rhs.shape[0] == npt + n + 1
540
+ ), "The shape of `rhs` is not valid."
541
+
542
+ # Build the left-hand side matrix of the interpolation system. The
543
+ # matrix below stores diag(left_scaling) * W * diag(right_scaling),
544
+ # where W is the theoretical matrix of the interpolation system. The
545
+ # left and right scaling matrices are chosen to keep the elements in
546
+ # the matrix well-balanced.
547
+ a, right_scaling, eig = build_system(interpolation)
548
+
549
+ # Build the solution. After a discussion with Mike Saunders and Alexis
550
+ # Montoison during their visit to the Hong Kong Polytechnic University
551
+ # in 2024, we decided to use the eigendecomposition of the symmetric
552
+ # matrix a. This is more stable than the previously employed LBL
553
+ # decomposition, and allows us to directly detect ill-conditioning of
554
+ # the system and to build the least-squares solution if necessary.
555
+ # Numerical experiments have shown that this strategy improves the
556
+ # performance of the solver.
557
+ rhs_scaled = rhs * right_scaling[:, np.newaxis]
558
+ if not (np.all(np.isfinite(a)) and np.all(np.isfinite(rhs_scaled))):
559
+ raise np.linalg.LinAlgError(
560
+ "The interpolation system is ill-defined."
561
+ )
562
+
563
+ # calculated in build_system
564
+ eig_values, eig_vectors = eig
565
+
566
+ large_eig_values = np.abs(eig_values) > EPS
567
+ eig_vectors = eig_vectors[:, large_eig_values]
568
+ inv_eig_values = 1.0 / eig_values[large_eig_values]
569
+ ill_conditioned = ~np.all(large_eig_values, 0)
570
+ left_scaled_solutions = eig_vectors @ (
571
+ (eig_vectors.T @ rhs_scaled) * inv_eig_values[:, np.newaxis]
572
+ )
573
+ return (
574
+ left_scaled_solutions * right_scaling[:, np.newaxis],
575
+ ill_conditioned,
576
+ )
577
+
578
+ @staticmethod
579
+ def _get_model(interpolation, values):
580
+ """
581
+ Solve the interpolation system.
582
+
583
+ Parameters
584
+ ----------
585
+ interpolation : `cobyqa.models.Interpolation`
586
+ Interpolation set.
587
+ values : `numpy.ndarray`, shape (npt,)
588
+ Values of the interpolated function at the interpolation points.
589
+
590
+ Returns
591
+ -------
592
+ float
593
+ Constant term of the quadratic model.
594
+ `numpy.ndarray`, shape (n,)
595
+ Gradient of the quadratic model at ``interpolation.x_base``.
596
+ `numpy.ndarray`, shape (npt,)
597
+ Implicit Hessian matrix of the quadratic model.
598
+
599
+ Raises
600
+ ------
601
+ `numpy.linalg.LinAlgError`
602
+ If the interpolation system is ill-defined.
603
+ """
604
+ assert values.shape == (
605
+ interpolation.npt,
606
+ ), "The shape of `values` is not valid."
607
+ n, npt = interpolation.xpt.shape
608
+ x, ill_conditioned = Quadratic.solve_systems(
609
+ interpolation,
610
+ np.block(
611
+ [
612
+ [
613
+ values,
614
+ np.zeros(n + 1),
615
+ ]
616
+ ]
617
+ ).T,
618
+ )
619
+ return x[npt, 0], x[npt + 1:, 0], x[:npt, 0], ill_conditioned
620
+
621
+
622
+ class Models:
623
+ """
624
+ Models for a nonlinear optimization problem.
625
+ """
626
+
627
+ def __init__(self, pb, options, penalty):
628
+ """
629
+ Initialize the models.
630
+
631
+ Parameters
632
+ ----------
633
+ pb : `cobyqa.problem.Problem`
634
+ Problem to be solved.
635
+ options : dict
636
+ Options of the solver.
637
+ penalty : float
638
+ Penalty parameter used to select the point in the filter to forward
639
+ to the callback function.
640
+
641
+ Raises
642
+ ------
643
+ `cobyqa.utils.MaxEvalError`
644
+ If the maximum number of evaluations is reached.
645
+ `cobyqa.utils.TargetSuccess`
646
+ If a nearly feasible point has been found with an objective
647
+ function value below the target.
648
+ `cobyqa.utils.FeasibleSuccess`
649
+ If a feasible point has been found for a feasibility problem.
650
+ `numpy.linalg.LinAlgError`
651
+ If the interpolation system is ill-defined.
652
+ """
653
+ # Set the initial interpolation set.
654
+ self._debug = options[Options.DEBUG]
655
+ self._interpolation = Interpolation(pb, options)
656
+
657
+ # Evaluate the nonlinear functions at the initial interpolation points.
658
+ x_eval = self.interpolation.point(0)
659
+ fun_init, cub_init, ceq_init = pb(x_eval, penalty)
660
+ self._fun_val = np.full(options[Options.NPT], np.nan)
661
+ self._cub_val = np.full((options[Options.NPT], cub_init.size), np.nan)
662
+ self._ceq_val = np.full((options[Options.NPT], ceq_init.size), np.nan)
663
+ for k in range(options[Options.NPT]):
664
+ if k >= options[Options.MAX_EVAL]:
665
+ raise MaxEvalError
666
+ if k == 0:
667
+ self.fun_val[k] = fun_init
668
+ self.cub_val[k, :] = cub_init
669
+ self.ceq_val[k, :] = ceq_init
670
+ else:
671
+ x_eval = self.interpolation.point(k)
672
+ self.fun_val[k], self.cub_val[k, :], self.ceq_val[k, :] = pb(
673
+ x_eval,
674
+ penalty,
675
+ )
676
+
677
+ # Stop the iterations if the problem is a feasibility problem and
678
+ # the current interpolation point is feasible.
679
+ if (
680
+ pb.is_feasibility
681
+ and pb.maxcv(
682
+ self.interpolation.point(k),
683
+ self.cub_val[k, :],
684
+ self.ceq_val[k, :],
685
+ )
686
+ <= options[Options.FEASIBILITY_TOL]
687
+ ):
688
+ raise FeasibleSuccess
689
+
690
+ # Stop the iterations if the current interpolation point is nearly
691
+ # feasible and has an objective function value below the target.
692
+ if (
693
+ self._fun_val[k] <= options[Options.TARGET]
694
+ and pb.maxcv(
695
+ self.interpolation.point(k),
696
+ self.cub_val[k, :],
697
+ self.ceq_val[k, :],
698
+ )
699
+ <= options[Options.FEASIBILITY_TOL]
700
+ ):
701
+ raise TargetSuccess
702
+
703
+ # Build the initial quadratic models.
704
+ self._fun = Quadratic(
705
+ self.interpolation,
706
+ self._fun_val,
707
+ options[Options.DEBUG],
708
+ )
709
+ self._cub = np.empty(self.m_nonlinear_ub, dtype=Quadratic)
710
+ self._ceq = np.empty(self.m_nonlinear_eq, dtype=Quadratic)
711
+ for i in range(self.m_nonlinear_ub):
712
+ self._cub[i] = Quadratic(
713
+ self.interpolation,
714
+ self.cub_val[:, i],
715
+ options[Options.DEBUG],
716
+ )
717
+ for i in range(self.m_nonlinear_eq):
718
+ self._ceq[i] = Quadratic(
719
+ self.interpolation,
720
+ self.ceq_val[:, i],
721
+ options[Options.DEBUG],
722
+ )
723
+ if self._debug:
724
+ self._check_interpolation_conditions()
725
+
726
+ @property
727
+ def n(self):
728
+ """
729
+ Dimension of the problem.
730
+
731
+ Returns
732
+ -------
733
+ int
734
+ Dimension of the problem.
735
+ """
736
+ return self.interpolation.n
737
+
738
+ @property
739
+ def npt(self):
740
+ """
741
+ Number of interpolation points.
742
+
743
+ Returns
744
+ -------
745
+ int
746
+ Number of interpolation points.
747
+ """
748
+ return self.interpolation.npt
749
+
750
+ @property
751
+ def m_nonlinear_ub(self):
752
+ """
753
+ Number of nonlinear inequality constraints.
754
+
755
+ Returns
756
+ -------
757
+ int
758
+ Number of nonlinear inequality constraints.
759
+ """
760
+ return self.cub_val.shape[1]
761
+
762
+ @property
763
+ def m_nonlinear_eq(self):
764
+ """
765
+ Number of nonlinear equality constraints.
766
+
767
+ Returns
768
+ -------
769
+ int
770
+ Number of nonlinear equality constraints.
771
+ """
772
+ return self.ceq_val.shape[1]
773
+
774
+ @property
775
+ def interpolation(self):
776
+ """
777
+ Interpolation set.
778
+
779
+ Returns
780
+ -------
781
+ `cobyqa.models.Interpolation`
782
+ Interpolation set.
783
+ """
784
+ return self._interpolation
785
+
786
+ @property
787
+ def fun_val(self):
788
+ """
789
+ Values of the objective function at the interpolation points.
790
+
791
+ Returns
792
+ -------
793
+ `numpy.ndarray`, shape (npt,)
794
+ Values of the objective function at the interpolation points.
795
+ """
796
+ return self._fun_val
797
+
798
+ @property
799
+ def cub_val(self):
800
+ """
801
+ Values of the nonlinear inequality constraint functions at the
802
+ interpolation points.
803
+
804
+ Returns
805
+ -------
806
+ `numpy.ndarray`, shape (npt, m_nonlinear_ub)
807
+ Values of the nonlinear inequality constraint functions at the
808
+ interpolation points.
809
+ """
810
+ return self._cub_val
811
+
812
+ @property
813
+ def ceq_val(self):
814
+ """
815
+ Values of the nonlinear equality constraint functions at the
816
+ interpolation points.
817
+
818
+ Returns
819
+ -------
820
+ `numpy.ndarray`, shape (npt, m_nonlinear_eq)
821
+ Values of the nonlinear equality constraint functions at the
822
+ interpolation points.
823
+ """
824
+ return self._ceq_val
825
+
826
+ def fun(self, x):
827
+ """
828
+ Evaluate the quadratic model of the objective function at a given
829
+ point.
830
+
831
+ Parameters
832
+ ----------
833
+ x : `numpy.ndarray`, shape (n,)
834
+ Point at which to evaluate the quadratic model of the objective
835
+ function.
836
+
837
+ Returns
838
+ -------
839
+ float
840
+ Value of the quadratic model of the objective function at `x`.
841
+ """
842
+ if self._debug:
843
+ assert x.shape == (self.n,), "The shape of `x` is not valid."
844
+ return self._fun(x, self.interpolation)
845
+
846
+ def fun_grad(self, x):
847
+ """
848
+ Evaluate the gradient of the quadratic model of the objective function
849
+ at a given point.
850
+
851
+ Parameters
852
+ ----------
853
+ x : `numpy.ndarray`, shape (n,)
854
+ Point at which to evaluate the gradient of the quadratic model of
855
+ the objective function.
856
+
857
+ Returns
858
+ -------
859
+ `numpy.ndarray`, shape (n,)
860
+ Gradient of the quadratic model of the objective function at `x`.
861
+ """
862
+ if self._debug:
863
+ assert x.shape == (self.n,), "The shape of `x` is not valid."
864
+ return self._fun.grad(x, self.interpolation)
865
+
866
+ def fun_hess(self):
867
+ """
868
+ Evaluate the Hessian matrix of the quadratic model of the objective
869
+ function.
870
+
871
+ Returns
872
+ -------
873
+ `numpy.ndarray`, shape (n, n)
874
+ Hessian matrix of the quadratic model of the objective function.
875
+ """
876
+ return self._fun.hess(self.interpolation)
877
+
878
+ def fun_hess_prod(self, v):
879
+ """
880
+ Evaluate the right product of the Hessian matrix of the quadratic model
881
+ of the objective function with a given vector.
882
+
883
+ Parameters
884
+ ----------
885
+ v : `numpy.ndarray`, shape (n,)
886
+ Vector with which the Hessian matrix of the quadratic model of the
887
+ objective function is multiplied from the right.
888
+
889
+ Returns
890
+ -------
891
+ `numpy.ndarray`, shape (n,)
892
+ Right product of the Hessian matrix of the quadratic model of the
893
+ objective function with `v`.
894
+ """
895
+ if self._debug:
896
+ assert v.shape == (self.n,), "The shape of `v` is not valid."
897
+ return self._fun.hess_prod(v, self.interpolation)
898
+
899
+ def fun_curv(self, v):
900
+ """
901
+ Evaluate the curvature of the quadratic model of the objective function
902
+ along a given direction.
903
+
904
+ Parameters
905
+ ----------
906
+ v : `numpy.ndarray`, shape (n,)
907
+ Direction along which the curvature of the quadratic model of the
908
+ objective function is evaluated.
909
+
910
+ Returns
911
+ -------
912
+ float
913
+ Curvature of the quadratic model of the objective function along
914
+ `v`.
915
+ """
916
+ if self._debug:
917
+ assert v.shape == (self.n,), "The shape of `v` is not valid."
918
+ return self._fun.curv(v, self.interpolation)
919
+
920
+ def fun_alt_grad(self, x):
921
+ """
922
+ Evaluate the gradient of the alternative quadratic model of the
923
+ objective function at a given point.
924
+
925
+ Parameters
926
+ ----------
927
+ x : `numpy.ndarray`, shape (n,)
928
+ Point at which to evaluate the gradient of the alternative
929
+ quadratic model of the objective function.
930
+
931
+ Returns
932
+ -------
933
+ `numpy.ndarray`, shape (n,)
934
+ Gradient of the alternative quadratic model of the objective
935
+ function at `x`.
936
+
937
+ Raises
938
+ ------
939
+ `numpy.linalg.LinAlgError`
940
+ If the interpolation system is ill-defined.
941
+ """
942
+ if self._debug:
943
+ assert x.shape == (self.n,), "The shape of `x` is not valid."
944
+ model = Quadratic(self.interpolation, self.fun_val, self._debug)
945
+ return model.grad(x, self.interpolation)
946
+
947
+ def cub(self, x, mask=None):
948
+ """
949
+ Evaluate the quadratic models of the nonlinear inequality functions at
950
+ a given point.
951
+
952
+ Parameters
953
+ ----------
954
+ x : `numpy.ndarray`, shape (n,)
955
+ Point at which to evaluate the quadratic models of the nonlinear
956
+ inequality functions.
957
+ mask : `numpy.ndarray`, shape (m_nonlinear_ub,), optional
958
+ Mask of the quadratic models to consider.
959
+
960
+ Returns
961
+ -------
962
+ `numpy.ndarray`
963
+ Values of the quadratic model of the nonlinear inequality
964
+ functions.
965
+ """
966
+ if self._debug:
967
+ assert x.shape == (self.n,), "The shape of `x` is not valid."
968
+ assert mask is None or mask.shape == (
969
+ self.m_nonlinear_ub,
970
+ ), "The shape of `mask` is not valid."
971
+ return np.array(
972
+ [model(x, self.interpolation) for model in self._get_cub(mask)]
973
+ )
974
+
975
+ def cub_grad(self, x, mask=None):
976
+ """
977
+ Evaluate the gradients of the quadratic models of the nonlinear
978
+ inequality functions at a given point.
979
+
980
+ Parameters
981
+ ----------
982
+ x : `numpy.ndarray`, shape (n,)
983
+ Point at which to evaluate the gradients of the quadratic models of
984
+ the nonlinear inequality functions.
985
+ mask : `numpy.ndarray`, shape (m_nonlinear_eq,), optional
986
+ Mask of the quadratic models to consider.
987
+
988
+ Returns
989
+ -------
990
+ `numpy.ndarray`
991
+ Gradients of the quadratic model of the nonlinear inequality
992
+ functions.
993
+ """
994
+ if self._debug:
995
+ assert x.shape == (self.n,), "The shape of `x` is not valid."
996
+ assert mask is None or mask.shape == (
997
+ self.m_nonlinear_ub,
998
+ ), "The shape of `mask` is not valid."
999
+ return np.reshape(
1000
+ [model.grad(x, self.interpolation)
1001
+ for model in self._get_cub(mask)],
1002
+ (-1, self.n),
1003
+ )
1004
+
1005
+ def cub_hess(self, mask=None):
1006
+ """
1007
+ Evaluate the Hessian matrices of the quadratic models of the nonlinear
1008
+ inequality functions.
1009
+
1010
+ Parameters
1011
+ ----------
1012
+ mask : `numpy.ndarray`, shape (m_nonlinear_ub,), optional
1013
+ Mask of the quadratic models to consider.
1014
+
1015
+ Returns
1016
+ -------
1017
+ `numpy.ndarray`
1018
+ Hessian matrices of the quadratic models of the nonlinear
1019
+ inequality functions.
1020
+ """
1021
+ if self._debug:
1022
+ assert mask is None or mask.shape == (
1023
+ self.m_nonlinear_ub,
1024
+ ), "The shape of `mask` is not valid."
1025
+ return np.reshape(
1026
+ [model.hess(self.interpolation) for model in self._get_cub(mask)],
1027
+ (-1, self.n, self.n),
1028
+ )
1029
+
1030
+ def cub_hess_prod(self, v, mask=None):
1031
+ """
1032
+ Evaluate the right product of the Hessian matrices of the quadratic
1033
+ models of the nonlinear inequality functions with a given vector.
1034
+
1035
+ Parameters
1036
+ ----------
1037
+ v : `numpy.ndarray`, shape (n,)
1038
+ Vector with which the Hessian matrices of the quadratic models of
1039
+ the nonlinear inequality functions are multiplied from the right.
1040
+ mask : `numpy.ndarray`, shape (m_nonlinear_ub,), optional
1041
+ Mask of the quadratic models to consider.
1042
+
1043
+ Returns
1044
+ -------
1045
+ `numpy.ndarray`
1046
+ Right products of the Hessian matrices of the quadratic models of
1047
+ the nonlinear inequality functions with `v`.
1048
+ """
1049
+ if self._debug:
1050
+ assert v.shape == (self.n,), "The shape of `v` is not valid."
1051
+ assert mask is None or mask.shape == (
1052
+ self.m_nonlinear_ub,
1053
+ ), "The shape of `mask` is not valid."
1054
+ return np.reshape(
1055
+ [
1056
+ model.hess_prod(v, self.interpolation)
1057
+ for model in self._get_cub(mask)
1058
+ ],
1059
+ (-1, self.n),
1060
+ )
1061
+
1062
+ def cub_curv(self, v, mask=None):
1063
+ """
1064
+ Evaluate the curvature of the quadratic models of the nonlinear
1065
+ inequality functions along a given direction.
1066
+
1067
+ Parameters
1068
+ ----------
1069
+ v : `numpy.ndarray`, shape (n,)
1070
+ Direction along which the curvature of the quadratic models of the
1071
+ nonlinear inequality functions is evaluated.
1072
+ mask : `numpy.ndarray`, shape (m_nonlinear_ub,), optional
1073
+ Mask of the quadratic models to consider.
1074
+
1075
+ Returns
1076
+ -------
1077
+ `numpy.ndarray`
1078
+ Curvature of the quadratic models of the nonlinear inequality
1079
+ functions along `v`.
1080
+ """
1081
+ if self._debug:
1082
+ assert v.shape == (self.n,), "The shape of `v` is not valid."
1083
+ assert mask is None or mask.shape == (
1084
+ self.m_nonlinear_ub,
1085
+ ), "The shape of `mask` is not valid."
1086
+ return np.array(
1087
+ [model.curv(v, self.interpolation)
1088
+ for model in self._get_cub(mask)]
1089
+ )
1090
+
1091
+ def ceq(self, x, mask=None):
1092
+ """
1093
+ Evaluate the quadratic models of the nonlinear equality functions at a
1094
+ given point.
1095
+
1096
+ Parameters
1097
+ ----------
1098
+ x : `numpy.ndarray`, shape (n,)
1099
+ Point at which to evaluate the quadratic models of the nonlinear
1100
+ equality functions.
1101
+ mask : `numpy.ndarray`, shape (m_nonlinear_eq,), optional
1102
+ Mask of the quadratic models to consider.
1103
+
1104
+ Returns
1105
+ -------
1106
+ `numpy.ndarray`
1107
+ Values of the quadratic model of the nonlinear equality functions.
1108
+ """
1109
+ if self._debug:
1110
+ assert x.shape == (self.n,), "The shape of `x` is not valid."
1111
+ assert mask is None or mask.shape == (
1112
+ self.m_nonlinear_eq,
1113
+ ), "The shape of `mask` is not valid."
1114
+ return np.array(
1115
+ [model(x, self.interpolation) for model in self._get_ceq(mask)]
1116
+ )
1117
+
1118
+ def ceq_grad(self, x, mask=None):
1119
+ """
1120
+ Evaluate the gradients of the quadratic models of the nonlinear
1121
+ equality functions at a given point.
1122
+
1123
+ Parameters
1124
+ ----------
1125
+ x : `numpy.ndarray`, shape (n,)
1126
+ Point at which to evaluate the gradients of the quadratic models of
1127
+ the nonlinear equality functions.
1128
+ mask : `numpy.ndarray`, shape (m_nonlinear_eq,), optional
1129
+ Mask of the quadratic models to consider.
1130
+
1131
+ Returns
1132
+ -------
1133
+ `numpy.ndarray`
1134
+ Gradients of the quadratic model of the nonlinear equality
1135
+ functions.
1136
+ """
1137
+ if self._debug:
1138
+ assert x.shape == (self.n,), "The shape of `x` is not valid."
1139
+ assert mask is None or mask.shape == (
1140
+ self.m_nonlinear_eq,
1141
+ ), "The shape of `mask` is not valid."
1142
+ return np.reshape(
1143
+ [model.grad(x, self.interpolation)
1144
+ for model in self._get_ceq(mask)],
1145
+ (-1, self.n),
1146
+ )
1147
+
1148
+ def ceq_hess(self, mask=None):
1149
+ """
1150
+ Evaluate the Hessian matrices of the quadratic models of the nonlinear
1151
+ equality functions.
1152
+
1153
+ Parameters
1154
+ ----------
1155
+ mask : `numpy.ndarray`, shape (m_nonlinear_eq,), optional
1156
+ Mask of the quadratic models to consider.
1157
+
1158
+ Returns
1159
+ -------
1160
+ `numpy.ndarray`
1161
+ Hessian matrices of the quadratic models of the nonlinear equality
1162
+ functions.
1163
+ """
1164
+ if self._debug:
1165
+ assert mask is None or mask.shape == (
1166
+ self.m_nonlinear_eq,
1167
+ ), "The shape of `mask` is not valid."
1168
+ return np.reshape(
1169
+ [model.hess(self.interpolation) for model in self._get_ceq(mask)],
1170
+ (-1, self.n, self.n),
1171
+ )
1172
+
1173
+ def ceq_hess_prod(self, v, mask=None):
1174
+ """
1175
+ Evaluate the right product of the Hessian matrices of the quadratic
1176
+ models of the nonlinear equality functions with a given vector.
1177
+
1178
+ Parameters
1179
+ ----------
1180
+ v : `numpy.ndarray`, shape (n,)
1181
+ Vector with which the Hessian matrices of the quadratic models of
1182
+ the nonlinear equality functions are multiplied from the right.
1183
+ mask : `numpy.ndarray`, shape (m_nonlinear_eq,), optional
1184
+ Mask of the quadratic models to consider.
1185
+
1186
+ Returns
1187
+ -------
1188
+ `numpy.ndarray`
1189
+ Right products of the Hessian matrices of the quadratic models of
1190
+ the nonlinear equality functions with `v`.
1191
+ """
1192
+ if self._debug:
1193
+ assert v.shape == (self.n,), "The shape of `v` is not valid."
1194
+ assert mask is None or mask.shape == (
1195
+ self.m_nonlinear_eq,
1196
+ ), "The shape of `mask` is not valid."
1197
+ return np.reshape(
1198
+ [
1199
+ model.hess_prod(v, self.interpolation)
1200
+ for model in self._get_ceq(mask)
1201
+ ],
1202
+ (-1, self.n),
1203
+ )
1204
+
1205
+ def ceq_curv(self, v, mask=None):
1206
+ """
1207
+ Evaluate the curvature of the quadratic models of the nonlinear
1208
+ equality functions along a given direction.
1209
+
1210
+ Parameters
1211
+ ----------
1212
+ v : `numpy.ndarray`, shape (n,)
1213
+ Direction along which the curvature of the quadratic models of the
1214
+ nonlinear equality functions is evaluated.
1215
+ mask : `numpy.ndarray`, shape (m_nonlinear_eq,), optional
1216
+ Mask of the quadratic models to consider.
1217
+
1218
+ Returns
1219
+ -------
1220
+ `numpy.ndarray`
1221
+ Curvature of the quadratic models of the nonlinear equality
1222
+ functions along `v`.
1223
+ """
1224
+ if self._debug:
1225
+ assert v.shape == (self.n,), "The shape of `v` is not valid."
1226
+ assert mask is None or mask.shape == (
1227
+ self.m_nonlinear_eq,
1228
+ ), "The shape of `mask` is not valid."
1229
+ return np.array(
1230
+ [model.curv(v, self.interpolation)
1231
+ for model in self._get_ceq(mask)]
1232
+ )
1233
+
1234
+ def reset_models(self):
1235
+ """
1236
+ Set the quadratic models of the objective function, nonlinear
1237
+ inequality constraints, and nonlinear equality constraints to the
1238
+ alternative quadratic models.
1239
+
1240
+ Raises
1241
+ ------
1242
+ `numpy.linalg.LinAlgError`
1243
+ If the interpolation system is ill-defined.
1244
+ """
1245
+ self._fun = Quadratic(self.interpolation, self.fun_val, self._debug)
1246
+ for i in range(self.m_nonlinear_ub):
1247
+ self._cub[i] = Quadratic(
1248
+ self.interpolation,
1249
+ self.cub_val[:, i],
1250
+ self._debug,
1251
+ )
1252
+ for i in range(self.m_nonlinear_eq):
1253
+ self._ceq[i] = Quadratic(
1254
+ self.interpolation,
1255
+ self.ceq_val[:, i],
1256
+ self._debug,
1257
+ )
1258
+ if self._debug:
1259
+ self._check_interpolation_conditions()
1260
+
1261
+ def update_interpolation(self, k_new, x_new, fun_val, cub_val, ceq_val):
1262
+ """
1263
+ Update the interpolation set.
1264
+
1265
+ This method updates the interpolation set by replacing the `knew`-th
1266
+ interpolation point with `xnew`. It also updates the function values
1267
+ and the quadratic models.
1268
+
1269
+ Parameters
1270
+ ----------
1271
+ k_new : int
1272
+ Index of the updated interpolation point.
1273
+ x_new : `numpy.ndarray`, shape (n,)
1274
+ New interpolation point. Its value is interpreted as relative to
1275
+ the origin, not the base point.
1276
+ fun_val : float
1277
+ Value of the objective function at `x_new`.
1278
+ Objective function value at `x_new`.
1279
+ cub_val : `numpy.ndarray`, shape (m_nonlinear_ub,)
1280
+ Values of the nonlinear inequality constraints at `x_new`.
1281
+ ceq_val : `numpy.ndarray`, shape (m_nonlinear_eq,)
1282
+ Values of the nonlinear equality constraints at `x_new`.
1283
+
1284
+ Raises
1285
+ ------
1286
+ `numpy.linalg.LinAlgError`
1287
+ If the interpolation system is ill-defined.
1288
+ """
1289
+ if self._debug:
1290
+ assert 0 <= k_new < self.npt, "The index `k_new` is not valid."
1291
+ assert x_new.shape == (self.n,), \
1292
+ "The shape of `x_new` is not valid."
1293
+ assert isinstance(fun_val, float), \
1294
+ "The function value is not valid."
1295
+ assert cub_val.shape == (
1296
+ self.m_nonlinear_ub,
1297
+ ), "The shape of `cub_val` is not valid."
1298
+ assert ceq_val.shape == (
1299
+ self.m_nonlinear_eq,
1300
+ ), "The shape of `ceq_val` is not valid."
1301
+
1302
+ # Compute the updates in the interpolation conditions.
1303
+ fun_diff = np.zeros(self.npt)
1304
+ cub_diff = np.zeros(self.cub_val.shape)
1305
+ ceq_diff = np.zeros(self.ceq_val.shape)
1306
+ fun_diff[k_new] = fun_val - self.fun(x_new)
1307
+ cub_diff[k_new, :] = cub_val - self.cub(x_new)
1308
+ ceq_diff[k_new, :] = ceq_val - self.ceq(x_new)
1309
+
1310
+ # Update the function values.
1311
+ self.fun_val[k_new] = fun_val
1312
+ self.cub_val[k_new, :] = cub_val
1313
+ self.ceq_val[k_new, :] = ceq_val
1314
+
1315
+ # Update the interpolation set.
1316
+ dir_old = np.copy(self.interpolation.xpt[:, k_new])
1317
+ self.interpolation.xpt[:, k_new] = x_new - self.interpolation.x_base
1318
+
1319
+ # Update the quadratic models.
1320
+ ill_conditioned = self._fun.update(
1321
+ self.interpolation,
1322
+ k_new,
1323
+ dir_old,
1324
+ fun_diff,
1325
+ )
1326
+ for i in range(self.m_nonlinear_ub):
1327
+ ill_conditioned = ill_conditioned or self._cub[i].update(
1328
+ self.interpolation,
1329
+ k_new,
1330
+ dir_old,
1331
+ cub_diff[:, i],
1332
+ )
1333
+ for i in range(self.m_nonlinear_eq):
1334
+ ill_conditioned = ill_conditioned or self._ceq[i].update(
1335
+ self.interpolation,
1336
+ k_new,
1337
+ dir_old,
1338
+ ceq_diff[:, i],
1339
+ )
1340
+ if self._debug:
1341
+ self._check_interpolation_conditions()
1342
+ return ill_conditioned
1343
+
1344
+ def determinants(self, x_new, k_new=None):
1345
+ """
1346
+ Compute the normalized determinants of the new interpolation systems.
1347
+
1348
+ Parameters
1349
+ ----------
1350
+ x_new : `numpy.ndarray`, shape (n,)
1351
+ New interpolation point. Its value is interpreted as relative to
1352
+ the origin, not the base point.
1353
+ k_new : int, optional
1354
+ Index of the updated interpolation point. If `k_new` is not
1355
+ specified, all the possible determinants are computed.
1356
+
1357
+ Returns
1358
+ -------
1359
+ {float, `numpy.ndarray`, shape (npt,)}
1360
+ Determinant(s) of the new interpolation system.
1361
+
1362
+ Raises
1363
+ ------
1364
+ `numpy.linalg.LinAlgError`
1365
+ If the interpolation system is ill-defined.
1366
+
1367
+ Notes
1368
+ -----
1369
+ The determinants are normalized by the determinant of the current
1370
+ interpolation system. For stability reasons, the calculations are done
1371
+ using the formula (2.12) in [1]_.
1372
+
1373
+ References
1374
+ ----------
1375
+ .. [1] M. J. D. Powell. On updating the inverse of a KKT matrix.
1376
+ Technical Report DAMTP 2004/NA01, Department of Applied Mathematics
1377
+ and Theoretical Physics, University of Cambridge, Cambridge, UK,
1378
+ 2004.
1379
+ """
1380
+ if self._debug:
1381
+ assert x_new.shape == (self.n,), \
1382
+ "The shape of `x_new` is not valid."
1383
+ assert (
1384
+ k_new is None or 0 <= k_new < self.npt
1385
+ ), "The index `k_new` is not valid."
1386
+
1387
+ # Compute the values independent of k_new.
1388
+ shift = x_new - self.interpolation.x_base
1389
+ new_col = np.empty((self.npt + self.n + 1, 1))
1390
+ new_col[: self.npt, 0] = (
1391
+ 0.5 * (self.interpolation.xpt.T @ shift) ** 2.0)
1392
+ new_col[self.npt, 0] = 1.0
1393
+ new_col[self.npt + 1:, 0] = shift
1394
+ inv_new_col = Quadratic.solve_systems(self.interpolation, new_col)[0]
1395
+ beta = 0.5 * (shift @ shift) ** 2.0 - new_col[:, 0] @ inv_new_col[:, 0]
1396
+
1397
+ # Compute the values that depend on k.
1398
+ if k_new is None:
1399
+ coord_vec = np.eye(self.npt + self.n + 1, self.npt)
1400
+ alpha = np.diag(
1401
+ Quadratic.solve_systems(
1402
+ self.interpolation,
1403
+ coord_vec,
1404
+ )[0]
1405
+ )
1406
+ tau = inv_new_col[: self.npt, 0]
1407
+ else:
1408
+ coord_vec = np.eye(self.npt + self.n + 1, 1, -k_new)
1409
+ alpha = Quadratic.solve_systems(
1410
+ self.interpolation,
1411
+ coord_vec,
1412
+ )[
1413
+ 0
1414
+ ][k_new, 0]
1415
+ tau = inv_new_col[k_new, 0]
1416
+ return alpha * beta + tau**2.0
1417
+
1418
+ def shift_x_base(self, new_x_base, options):
1419
+ """
1420
+ Shift the base point without changing the interpolation set.
1421
+
1422
+ Parameters
1423
+ ----------
1424
+ new_x_base : `numpy.ndarray`, shape (n,)
1425
+ New base point.
1426
+ options : dict
1427
+ Options of the solver.
1428
+ """
1429
+ if self._debug:
1430
+ assert new_x_base.shape == (
1431
+ self.n,
1432
+ ), "The shape of `new_x_base` is not valid."
1433
+
1434
+ # Update the models.
1435
+ self._fun.shift_x_base(self.interpolation, new_x_base)
1436
+ for model in self._cub:
1437
+ model.shift_x_base(self.interpolation, new_x_base)
1438
+ for model in self._ceq:
1439
+ model.shift_x_base(self.interpolation, new_x_base)
1440
+
1441
+ # Update the base point and the interpolation points.
1442
+ shift = new_x_base - self.interpolation.x_base
1443
+ self.interpolation.x_base += shift
1444
+ self.interpolation.xpt -= shift[:, np.newaxis]
1445
+ if options[Options.DEBUG]:
1446
+ self._check_interpolation_conditions()
1447
+
1448
+ def _get_cub(self, mask=None):
1449
+ """
1450
+ Get the quadratic models of the nonlinear inequality constraints.
1451
+
1452
+ Parameters
1453
+ ----------
1454
+ mask : `numpy.ndarray`, shape (m_nonlinear_ub,), optional
1455
+ Mask of the quadratic models to return.
1456
+
1457
+ Returns
1458
+ -------
1459
+ `numpy.ndarray`
1460
+ Quadratic models of the nonlinear inequality constraints.
1461
+ """
1462
+ return self._cub if mask is None else self._cub[mask]
1463
+
1464
+ def _get_ceq(self, mask=None):
1465
+ """
1466
+ Get the quadratic models of the nonlinear equality constraints.
1467
+
1468
+ Parameters
1469
+ ----------
1470
+ mask : `numpy.ndarray`, shape (m_nonlinear_eq,), optional
1471
+ Mask of the quadratic models to return.
1472
+
1473
+ Returns
1474
+ -------
1475
+ `numpy.ndarray`
1476
+ Quadratic models of the nonlinear equality constraints.
1477
+ """
1478
+ return self._ceq if mask is None else self._ceq[mask]
1479
+
1480
+ def _check_interpolation_conditions(self):
1481
+ """
1482
+ Check the interpolation conditions of all quadratic models.
1483
+ """
1484
+ error_fun = 0.0
1485
+ error_cub = 0.0
1486
+ error_ceq = 0.0
1487
+ for k in range(self.npt):
1488
+ error_fun = np.max(
1489
+ [
1490
+ error_fun,
1491
+ np.abs(
1492
+ self.fun(self.interpolation.point(k)) - self.fun_val[k]
1493
+ ),
1494
+ ]
1495
+ )
1496
+ error_cub = np.max(
1497
+ np.abs(
1498
+ self.cub(self.interpolation.point(k)) - self.cub_val[k, :]
1499
+ ),
1500
+ initial=error_cub,
1501
+ )
1502
+ error_ceq = np.max(
1503
+ np.abs(
1504
+ self.ceq(self.interpolation.point(k)) - self.ceq_val[k, :]
1505
+ ),
1506
+ initial=error_ceq,
1507
+ )
1508
+ tol = 10.0 * np.sqrt(EPS) * max(self.n, self.npt)
1509
+ if error_fun > tol * np.max(np.abs(self.fun_val), initial=1.0):
1510
+ warnings.warn(
1511
+ "The interpolation conditions for the objective function are "
1512
+ "not satisfied.",
1513
+ RuntimeWarning,
1514
+ 2,
1515
+ )
1516
+ if error_cub > tol * np.max(np.abs(self.cub_val), initial=1.0):
1517
+ warnings.warn(
1518
+ "The interpolation conditions for the inequality constraint "
1519
+ "function are not satisfied.",
1520
+ RuntimeWarning,
1521
+ 2,
1522
+ )
1523
+ if error_ceq > tol * np.max(np.abs(self.ceq_val), initial=1.0):
1524
+ warnings.warn(
1525
+ "The interpolation conditions for the equality constraint "
1526
+ "function are not satisfied.",
1527
+ RuntimeWarning,
1528
+ 2,
1529
+ )
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/problem.py ADDED
@@ -0,0 +1,1296 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from contextlib import suppress
2
+ from inspect import signature
3
+ import copy
4
+
5
+ import numpy as np
6
+ from scipy.optimize import (
7
+ Bounds,
8
+ LinearConstraint,
9
+ NonlinearConstraint,
10
+ OptimizeResult,
11
+ )
12
+ from scipy.optimize._constraints import PreparedConstraint
13
+
14
+
15
+ from .settings import PRINT_OPTIONS, BARRIER
16
+ from .utils import CallbackSuccess, get_arrays_tol
17
+ from .utils import exact_1d_array
18
+
19
+
20
+ class ObjectiveFunction:
21
+ """
22
+ Real-valued objective function.
23
+ """
24
+
25
+ def __init__(self, fun, verbose, debug, *args):
26
+ """
27
+ Initialize the objective function.
28
+
29
+ Parameters
30
+ ----------
31
+ fun : {callable, None}
32
+ Function to evaluate, or None.
33
+
34
+ ``fun(x, *args) -> float``
35
+
36
+ where ``x`` is an array with shape (n,) and `args` is a tuple.
37
+ verbose : bool
38
+ Whether to print the function evaluations.
39
+ debug : bool
40
+ Whether to make debugging tests during the execution.
41
+ *args : tuple
42
+ Additional arguments to be passed to the function.
43
+ """
44
+ if debug:
45
+ assert fun is None or callable(fun)
46
+ assert isinstance(verbose, bool)
47
+ assert isinstance(debug, bool)
48
+
49
+ self._fun = fun
50
+ self._verbose = verbose
51
+ self._args = args
52
+ self._n_eval = 0
53
+
54
+ def __call__(self, x):
55
+ """
56
+ Evaluate the objective function.
57
+
58
+ Parameters
59
+ ----------
60
+ x : array_like, shape (n,)
61
+ Point at which the objective function is evaluated.
62
+
63
+ Returns
64
+ -------
65
+ float
66
+ Function value at `x`.
67
+ """
68
+ x = np.array(x, dtype=float)
69
+ if self._fun is None:
70
+ f = 0.0
71
+ else:
72
+ f = float(np.squeeze(self._fun(x, *self._args)))
73
+ self._n_eval += 1
74
+ if self._verbose:
75
+ with np.printoptions(**PRINT_OPTIONS):
76
+ print(f"{self.name}({x}) = {f}")
77
+ return f
78
+
79
+ @property
80
+ def n_eval(self):
81
+ """
82
+ Number of function evaluations.
83
+
84
+ Returns
85
+ -------
86
+ int
87
+ Number of function evaluations.
88
+ """
89
+ return self._n_eval
90
+
91
+ @property
92
+ def name(self):
93
+ """
94
+ Name of the objective function.
95
+
96
+ Returns
97
+ -------
98
+ str
99
+ Name of the objective function.
100
+ """
101
+ name = ""
102
+ if self._fun is not None:
103
+ try:
104
+ name = self._fun.__name__
105
+ except AttributeError:
106
+ name = "fun"
107
+ return name
108
+
109
+
110
+ class BoundConstraints:
111
+ """
112
+ Bound constraints ``xl <= x <= xu``.
113
+ """
114
+
115
+ def __init__(self, bounds):
116
+ """
117
+ Initialize the bound constraints.
118
+
119
+ Parameters
120
+ ----------
121
+ bounds : scipy.optimize.Bounds
122
+ Bound constraints.
123
+ """
124
+ self._xl = np.array(bounds.lb, float)
125
+ self._xu = np.array(bounds.ub, float)
126
+
127
+ # Remove the ill-defined bounds.
128
+ self.xl[np.isnan(self.xl)] = -np.inf
129
+ self.xu[np.isnan(self.xu)] = np.inf
130
+
131
+ self.is_feasible = (
132
+ np.all(self.xl <= self.xu)
133
+ and np.all(self.xl < np.inf)
134
+ and np.all(self.xu > -np.inf)
135
+ )
136
+ self.m = np.count_nonzero(self.xl > -np.inf) + np.count_nonzero(
137
+ self.xu < np.inf
138
+ )
139
+ self.pcs = PreparedConstraint(bounds, np.ones(bounds.lb.size))
140
+
141
+ @property
142
+ def xl(self):
143
+ """
144
+ Lower bound.
145
+
146
+ Returns
147
+ -------
148
+ `numpy.ndarray`, shape (n,)
149
+ Lower bound.
150
+ """
151
+ return self._xl
152
+
153
+ @property
154
+ def xu(self):
155
+ """
156
+ Upper bound.
157
+
158
+ Returns
159
+ -------
160
+ `numpy.ndarray`, shape (n,)
161
+ Upper bound.
162
+ """
163
+ return self._xu
164
+
165
+ def maxcv(self, x):
166
+ """
167
+ Evaluate the maximum constraint violation.
168
+
169
+ Parameters
170
+ ----------
171
+ x : array_like, shape (n,)
172
+ Point at which the maximum constraint violation is evaluated.
173
+
174
+ Returns
175
+ -------
176
+ float
177
+ Maximum constraint violation at `x`.
178
+ """
179
+ x = np.asarray(x, dtype=float)
180
+ return self.violation(x)
181
+
182
+ def violation(self, x):
183
+ # shortcut for no bounds
184
+ if self.is_feasible:
185
+ return np.array([0])
186
+ else:
187
+ return self.pcs.violation(x)
188
+
189
+ def project(self, x):
190
+ """
191
+ Project a point onto the feasible set.
192
+
193
+ Parameters
194
+ ----------
195
+ x : array_like, shape (n,)
196
+ Point to be projected.
197
+
198
+ Returns
199
+ -------
200
+ `numpy.ndarray`, shape (n,)
201
+ Projection of `x` onto the feasible set.
202
+ """
203
+ return np.clip(x, self.xl, self.xu) if self.is_feasible else x
204
+
205
+
206
+ class LinearConstraints:
207
+ """
208
+ Linear constraints ``a_ub @ x <= b_ub`` and ``a_eq @ x == b_eq``.
209
+ """
210
+
211
+ def __init__(self, constraints, n, debug):
212
+ """
213
+ Initialize the linear constraints.
214
+
215
+ Parameters
216
+ ----------
217
+ constraints : list of LinearConstraint
218
+ Linear constraints.
219
+ n : int
220
+ Number of variables.
221
+ debug : bool
222
+ Whether to make debugging tests during the execution.
223
+ """
224
+ if debug:
225
+ assert isinstance(constraints, list)
226
+ for constraint in constraints:
227
+ assert isinstance(constraint, LinearConstraint)
228
+ assert isinstance(debug, bool)
229
+
230
+ self._a_ub = np.empty((0, n))
231
+ self._b_ub = np.empty(0)
232
+ self._a_eq = np.empty((0, n))
233
+ self._b_eq = np.empty(0)
234
+ for constraint in constraints:
235
+ is_equality = np.abs(
236
+ constraint.ub - constraint.lb
237
+ ) <= get_arrays_tol(constraint.lb, constraint.ub)
238
+ if np.any(is_equality):
239
+ self._a_eq = np.vstack((self.a_eq, constraint.A[is_equality]))
240
+ self._b_eq = np.concatenate(
241
+ (
242
+ self.b_eq,
243
+ 0.5
244
+ * (
245
+ constraint.lb[is_equality]
246
+ + constraint.ub[is_equality]
247
+ ),
248
+ )
249
+ )
250
+ if not np.all(is_equality):
251
+ self._a_ub = np.vstack(
252
+ (
253
+ self.a_ub,
254
+ constraint.A[~is_equality],
255
+ -constraint.A[~is_equality],
256
+ )
257
+ )
258
+ self._b_ub = np.concatenate(
259
+ (
260
+ self.b_ub,
261
+ constraint.ub[~is_equality],
262
+ -constraint.lb[~is_equality],
263
+ )
264
+ )
265
+
266
+ # Remove the ill-defined constraints.
267
+ self.a_ub[np.isnan(self.a_ub)] = 0.0
268
+ self.a_eq[np.isnan(self.a_eq)] = 0.0
269
+ undef_ub = np.isnan(self.b_ub) | np.isinf(self.b_ub)
270
+ undef_eq = np.isnan(self.b_eq)
271
+ self._a_ub = self.a_ub[~undef_ub, :]
272
+ self._b_ub = self.b_ub[~undef_ub]
273
+ self._a_eq = self.a_eq[~undef_eq, :]
274
+ self._b_eq = self.b_eq[~undef_eq]
275
+ self.pcs = [
276
+ PreparedConstraint(c, np.ones(n)) for c in constraints if c.A.size
277
+ ]
278
+
279
+ @property
280
+ def a_ub(self):
281
+ """
282
+ Left-hand side matrix of the linear inequality constraints.
283
+
284
+ Returns
285
+ -------
286
+ `numpy.ndarray`, shape (m, n)
287
+ Left-hand side matrix of the linear inequality constraints.
288
+ """
289
+ return self._a_ub
290
+
291
+ @property
292
+ def b_ub(self):
293
+ """
294
+ Right-hand side vector of the linear inequality constraints.
295
+
296
+ Returns
297
+ -------
298
+ `numpy.ndarray`, shape (m, n)
299
+ Right-hand side vector of the linear inequality constraints.
300
+ """
301
+ return self._b_ub
302
+
303
+ @property
304
+ def a_eq(self):
305
+ """
306
+ Left-hand side matrix of the linear equality constraints.
307
+
308
+ Returns
309
+ -------
310
+ `numpy.ndarray`, shape (m, n)
311
+ Left-hand side matrix of the linear equality constraints.
312
+ """
313
+ return self._a_eq
314
+
315
+ @property
316
+ def b_eq(self):
317
+ """
318
+ Right-hand side vector of the linear equality constraints.
319
+
320
+ Returns
321
+ -------
322
+ `numpy.ndarray`, shape (m, n)
323
+ Right-hand side vector of the linear equality constraints.
324
+ """
325
+ return self._b_eq
326
+
327
+ @property
328
+ def m_ub(self):
329
+ """
330
+ Number of linear inequality constraints.
331
+
332
+ Returns
333
+ -------
334
+ int
335
+ Number of linear inequality constraints.
336
+ """
337
+ return self.b_ub.size
338
+
339
+ @property
340
+ def m_eq(self):
341
+ """
342
+ Number of linear equality constraints.
343
+
344
+ Returns
345
+ -------
346
+ int
347
+ Number of linear equality constraints.
348
+ """
349
+ return self.b_eq.size
350
+
351
+ def maxcv(self, x):
352
+ """
353
+ Evaluate the maximum constraint violation.
354
+
355
+ Parameters
356
+ ----------
357
+ x : array_like, shape (n,)
358
+ Point at which the maximum constraint violation is evaluated.
359
+
360
+ Returns
361
+ -------
362
+ float
363
+ Maximum constraint violation at `x`.
364
+ """
365
+ return np.max(self.violation(x), initial=0.0)
366
+
367
+ def violation(self, x):
368
+ if len(self.pcs):
369
+ return np.concatenate([pc.violation(x) for pc in self.pcs])
370
+ return np.array([])
371
+
372
+
373
+ class NonlinearConstraints:
374
+ """
375
+ Nonlinear constraints ``c_ub(x) <= 0`` and ``c_eq(x) == b_eq``.
376
+ """
377
+
378
+ def __init__(self, constraints, verbose, debug):
379
+ """
380
+ Initialize the nonlinear constraints.
381
+
382
+ Parameters
383
+ ----------
384
+ constraints : list
385
+ Nonlinear constraints.
386
+ verbose : bool
387
+ Whether to print the function evaluations.
388
+ debug : bool
389
+ Whether to make debugging tests during the execution.
390
+ """
391
+ if debug:
392
+ assert isinstance(constraints, list)
393
+ for constraint in constraints:
394
+ assert isinstance(constraint, NonlinearConstraint)
395
+ assert isinstance(verbose, bool)
396
+ assert isinstance(debug, bool)
397
+
398
+ self._constraints = constraints
399
+ self.pcs = []
400
+ self._verbose = verbose
401
+
402
+ # map of indexes for equality and inequality constraints
403
+ self._map_ub = None
404
+ self._map_eq = None
405
+ self._m_ub = self._m_eq = None
406
+
407
+ def __call__(self, x):
408
+ """
409
+ Calculates the residual (slack) for the constraints.
410
+
411
+ Parameters
412
+ ----------
413
+ x : array_like, shape (n,)
414
+ Point at which the constraints are evaluated.
415
+
416
+ Returns
417
+ -------
418
+ `numpy.ndarray`, shape (m_nonlinear_ub,)
419
+ Nonlinear inequality constraint slack values.
420
+ `numpy.ndarray`, shape (m_nonlinear_eq,)
421
+ Nonlinear equality constraint slack values.
422
+ """
423
+ if not len(self._constraints):
424
+ self._m_eq = self._m_ub = 0
425
+ return np.array([]), np.array([])
426
+
427
+ x = np.array(x, dtype=float)
428
+ # first time around the constraints haven't been prepared
429
+ if not len(self.pcs):
430
+ self._map_ub = []
431
+ self._map_eq = []
432
+ self._m_eq = 0
433
+ self._m_ub = 0
434
+
435
+ for constraint in self._constraints:
436
+ if not callable(constraint.jac):
437
+ # having a callable constraint function prevents
438
+ # constraint.fun from being evaluated when preparing
439
+ # constraint
440
+ c = copy.copy(constraint)
441
+ c.jac = lambda x0: x0
442
+ c.hess = lambda x0, v: 0.0
443
+ pc = PreparedConstraint(c, x)
444
+ else:
445
+ pc = PreparedConstraint(constraint, x)
446
+ # we're going to be using the same x value again immediately
447
+ # after this initialisation
448
+ pc.fun.f_updated = True
449
+
450
+ self.pcs.append(pc)
451
+ idx = np.arange(pc.fun.m)
452
+
453
+ # figure out equality and inequality maps
454
+ lb, ub = pc.bounds[0], pc.bounds[1]
455
+ arr_tol = get_arrays_tol(lb, ub)
456
+ is_equality = np.abs(ub - lb) <= arr_tol
457
+ self._map_eq.append(idx[is_equality])
458
+ self._map_ub.append(idx[~is_equality])
459
+
460
+ # these values will be corrected to their proper values later
461
+ self._m_eq += np.count_nonzero(is_equality)
462
+ self._m_ub += np.count_nonzero(~is_equality)
463
+
464
+ c_ub = []
465
+ c_eq = []
466
+ for i, pc in enumerate(self.pcs):
467
+ val = pc.fun.fun(x)
468
+ if self._verbose:
469
+ with np.printoptions(**PRINT_OPTIONS):
470
+ with suppress(AttributeError):
471
+ fun_name = self._constraints[i].fun.__name__
472
+ print(f"{fun_name}({x}) = {val}")
473
+
474
+ # separate violations into c_eq and c_ub
475
+ eq_idx = self._map_eq[i]
476
+ ub_idx = self._map_ub[i]
477
+
478
+ ub_val = val[ub_idx]
479
+ if len(ub_idx):
480
+ xl = pc.bounds[0][ub_idx]
481
+ xu = pc.bounds[1][ub_idx]
482
+
483
+ # calculate slack within lower bound
484
+ finite_xl = xl > -np.inf
485
+ _v = xl[finite_xl] - ub_val[finite_xl]
486
+ c_ub.append(_v)
487
+
488
+ # calculate slack within lower bound
489
+ finite_xu = xu < np.inf
490
+ _v = ub_val[finite_xu] - xu[finite_xu]
491
+ c_ub.append(_v)
492
+
493
+ # equality constraints taken from midpoint between lb and ub
494
+ eq_val = val[eq_idx]
495
+ if len(eq_idx):
496
+ midpoint = 0.5 * (pc.bounds[1][eq_idx] + pc.bounds[0][eq_idx])
497
+ eq_val -= midpoint
498
+ c_eq.append(eq_val)
499
+
500
+ if self._m_eq:
501
+ c_eq = np.concatenate(c_eq)
502
+ else:
503
+ c_eq = np.array([])
504
+
505
+ if self._m_ub:
506
+ c_ub = np.concatenate(c_ub)
507
+ else:
508
+ c_ub = np.array([])
509
+
510
+ self._m_ub = c_ub.size
511
+ self._m_eq = c_eq.size
512
+
513
+ return c_ub, c_eq
514
+
515
+ @property
516
+ def m_ub(self):
517
+ """
518
+ Number of nonlinear inequality constraints.
519
+
520
+ Returns
521
+ -------
522
+ int
523
+ Number of nonlinear inequality constraints.
524
+
525
+ Raises
526
+ ------
527
+ ValueError
528
+ If the number of nonlinear inequality constraints is unknown.
529
+ """
530
+ if self._m_ub is None:
531
+ raise ValueError(
532
+ "The number of nonlinear inequality constraints is unknown."
533
+ )
534
+ else:
535
+ return self._m_ub
536
+
537
+ @property
538
+ def m_eq(self):
539
+ """
540
+ Number of nonlinear equality constraints.
541
+
542
+ Returns
543
+ -------
544
+ int
545
+ Number of nonlinear equality constraints.
546
+
547
+ Raises
548
+ ------
549
+ ValueError
550
+ If the number of nonlinear equality constraints is unknown.
551
+ """
552
+ if self._m_eq is None:
553
+ raise ValueError(
554
+ "The number of nonlinear equality constraints is unknown."
555
+ )
556
+ else:
557
+ return self._m_eq
558
+
559
+ @property
560
+ def n_eval(self):
561
+ """
562
+ Number of function evaluations.
563
+
564
+ Returns
565
+ -------
566
+ int
567
+ Number of function evaluations.
568
+ """
569
+ if len(self.pcs):
570
+ return self.pcs[0].fun.nfev
571
+ else:
572
+ return 0
573
+
574
+ def maxcv(self, x, cub_val=None, ceq_val=None):
575
+ """
576
+ Evaluate the maximum constraint violation.
577
+
578
+ Parameters
579
+ ----------
580
+ x : array_like, shape (n,)
581
+ Point at which the maximum constraint violation is evaluated.
582
+ cub_val : array_like, shape (m_nonlinear_ub,), optional
583
+ Values of the nonlinear inequality constraints. If not provided,
584
+ the nonlinear inequality constraints are evaluated at `x`.
585
+ ceq_val : array_like, shape (m_nonlinear_eq,), optional
586
+ Values of the nonlinear equality constraints. If not provided,
587
+ the nonlinear equality constraints are evaluated at `x`.
588
+
589
+ Returns
590
+ -------
591
+ float
592
+ Maximum constraint violation at `x`.
593
+ """
594
+ return np.max(
595
+ self.violation(x, cub_val=cub_val, ceq_val=ceq_val), initial=0.0
596
+ )
597
+
598
+ def violation(self, x, cub_val=None, ceq_val=None):
599
+ return np.concatenate([pc.violation(x) for pc in self.pcs])
600
+
601
+
602
+ class Problem:
603
+ """
604
+ Optimization problem.
605
+ """
606
+
607
+ def __init__(
608
+ self,
609
+ obj,
610
+ x0,
611
+ bounds,
612
+ linear,
613
+ nonlinear,
614
+ callback,
615
+ feasibility_tol,
616
+ scale,
617
+ store_history,
618
+ history_size,
619
+ filter_size,
620
+ debug,
621
+ ):
622
+ """
623
+ Initialize the nonlinear problem.
624
+
625
+ The problem is preprocessed to remove all the variables that are fixed
626
+ by the bound constraints.
627
+
628
+ Parameters
629
+ ----------
630
+ obj : ObjectiveFunction
631
+ Objective function.
632
+ x0 : array_like, shape (n,)
633
+ Initial guess.
634
+ bounds : BoundConstraints
635
+ Bound constraints.
636
+ linear : LinearConstraints
637
+ Linear constraints.
638
+ nonlinear : NonlinearConstraints
639
+ Nonlinear constraints.
640
+ callback : {callable, None}
641
+ Callback function.
642
+ feasibility_tol : float
643
+ Tolerance on the constraint violation.
644
+ scale : bool
645
+ Whether to scale the problem according to the bounds.
646
+ store_history : bool
647
+ Whether to store the function evaluations.
648
+ history_size : int
649
+ Maximum number of function evaluations to store.
650
+ filter_size : int
651
+ Maximum number of points in the filter.
652
+ debug : bool
653
+ Whether to make debugging tests during the execution.
654
+ """
655
+ if debug:
656
+ assert isinstance(obj, ObjectiveFunction)
657
+ assert isinstance(bounds, BoundConstraints)
658
+ assert isinstance(linear, LinearConstraints)
659
+ assert isinstance(nonlinear, NonlinearConstraints)
660
+ assert isinstance(feasibility_tol, float)
661
+ assert isinstance(scale, bool)
662
+ assert isinstance(store_history, bool)
663
+ assert isinstance(history_size, int)
664
+ if store_history:
665
+ assert history_size > 0
666
+ assert isinstance(filter_size, int)
667
+ assert filter_size > 0
668
+ assert isinstance(debug, bool)
669
+
670
+ self._obj = obj
671
+ self._linear = linear
672
+ self._nonlinear = nonlinear
673
+ if callback is not None:
674
+ if not callable(callback):
675
+ raise TypeError("The callback must be a callable function.")
676
+ self._callback = callback
677
+
678
+ # Check the consistency of the problem.
679
+ x0 = exact_1d_array(x0, "The initial guess must be a vector.")
680
+ n = x0.size
681
+ if bounds.xl.size != n:
682
+ raise ValueError(f"The bounds must have {n} elements.")
683
+ if linear.a_ub.shape[1] != n:
684
+ raise ValueError(
685
+ f"The left-hand side matrices of the linear constraints must "
686
+ f"have {n} columns."
687
+ )
688
+
689
+ # Check which variables are fixed.
690
+ tol = get_arrays_tol(bounds.xl, bounds.xu)
691
+ self._fixed_idx = (bounds.xl <= bounds.xu) & (
692
+ np.abs(bounds.xl - bounds.xu) < tol
693
+ )
694
+ self._fixed_val = 0.5 * (
695
+ bounds.xl[self._fixed_idx] + bounds.xu[self._fixed_idx]
696
+ )
697
+ self._fixed_val = np.clip(
698
+ self._fixed_val,
699
+ bounds.xl[self._fixed_idx],
700
+ bounds.xu[self._fixed_idx],
701
+ )
702
+
703
+ # Set the bound constraints.
704
+ self._orig_bounds = bounds
705
+ self._bounds = BoundConstraints(
706
+ Bounds(bounds.xl[~self._fixed_idx], bounds.xu[~self._fixed_idx])
707
+ )
708
+
709
+ # Set the initial guess.
710
+ self._x0 = self._bounds.project(x0[~self._fixed_idx])
711
+
712
+ # Set the linear constraints.
713
+ b_eq = linear.b_eq - linear.a_eq[:, self._fixed_idx] @ self._fixed_val
714
+ self._linear = LinearConstraints(
715
+ [
716
+ LinearConstraint(
717
+ linear.a_ub[:, ~self._fixed_idx],
718
+ -np.inf,
719
+ linear.b_ub
720
+ - linear.a_ub[:, self._fixed_idx] @ self._fixed_val,
721
+ ),
722
+ LinearConstraint(linear.a_eq[:, ~self._fixed_idx], b_eq, b_eq),
723
+ ],
724
+ self.n,
725
+ debug,
726
+ )
727
+
728
+ # Scale the problem if necessary.
729
+ scale = (
730
+ scale
731
+ and self._bounds.is_feasible
732
+ and np.all(np.isfinite(self._bounds.xl))
733
+ and np.all(np.isfinite(self._bounds.xu))
734
+ )
735
+ if scale:
736
+ self._scaling_factor = 0.5 * (self._bounds.xu - self._bounds.xl)
737
+ self._scaling_shift = 0.5 * (self._bounds.xu + self._bounds.xl)
738
+ self._bounds = BoundConstraints(
739
+ Bounds(-np.ones(self.n), np.ones(self.n))
740
+ )
741
+ b_eq = self._linear.b_eq - self._linear.a_eq @ self._scaling_shift
742
+ self._linear = LinearConstraints(
743
+ [
744
+ LinearConstraint(
745
+ self._linear.a_ub @ np.diag(self._scaling_factor),
746
+ -np.inf,
747
+ self._linear.b_ub
748
+ - self._linear.a_ub @ self._scaling_shift,
749
+ ),
750
+ LinearConstraint(
751
+ self._linear.a_eq @ np.diag(self._scaling_factor),
752
+ b_eq,
753
+ b_eq,
754
+ ),
755
+ ],
756
+ self.n,
757
+ debug,
758
+ )
759
+ self._x0 = (self._x0 - self._scaling_shift) / self._scaling_factor
760
+ else:
761
+ self._scaling_factor = np.ones(self.n)
762
+ self._scaling_shift = np.zeros(self.n)
763
+
764
+ # Set the initial filter.
765
+ self._feasibility_tol = feasibility_tol
766
+ self._filter_size = filter_size
767
+ self._fun_filter = []
768
+ self._maxcv_filter = []
769
+ self._x_filter = []
770
+
771
+ # Set the initial history.
772
+ self._store_history = store_history
773
+ self._history_size = history_size
774
+ self._fun_history = []
775
+ self._maxcv_history = []
776
+ self._x_history = []
777
+
778
+ def __call__(self, x, penalty=0.0):
779
+ """
780
+ Evaluate the objective and nonlinear constraint functions.
781
+
782
+ Parameters
783
+ ----------
784
+ x : array_like, shape (n,)
785
+ Point at which the functions are evaluated.
786
+ penalty : float, optional
787
+ Penalty parameter used to select the point in the filter to forward
788
+ to the callback function.
789
+
790
+ Returns
791
+ -------
792
+ float
793
+ Objective function value.
794
+ `numpy.ndarray`, shape (m_nonlinear_ub,)
795
+ Nonlinear inequality constraint function values.
796
+ `numpy.ndarray`, shape (m_nonlinear_eq,)
797
+ Nonlinear equality constraint function values.
798
+
799
+ Raises
800
+ ------
801
+ `cobyqa.utils.CallbackSuccess`
802
+ If the callback function raises a ``StopIteration``.
803
+ """
804
+ # Evaluate the objective and nonlinear constraint functions.
805
+ x = np.asarray(x, dtype=float)
806
+ x_full = self.build_x(x)
807
+ fun_val = self._obj(x_full)
808
+ cub_val, ceq_val = self._nonlinear(x_full)
809
+ maxcv_val = self.maxcv(x, cub_val, ceq_val)
810
+ if self._store_history:
811
+ self._fun_history.append(fun_val)
812
+ self._maxcv_history.append(maxcv_val)
813
+ self._x_history.append(x)
814
+ if len(self._fun_history) > self._history_size:
815
+ self._fun_history.pop(0)
816
+ self._maxcv_history.pop(0)
817
+ self._x_history.pop(0)
818
+
819
+ # Add the point to the filter if it is not dominated by any point.
820
+ if np.isnan(fun_val) and np.isnan(maxcv_val):
821
+ include_point = len(self._fun_filter) == 0
822
+ elif np.isnan(fun_val):
823
+ include_point = all(
824
+ np.isnan(fun_filter)
825
+ and maxcv_val < maxcv_filter
826
+ or np.isnan(maxcv_filter)
827
+ for fun_filter, maxcv_filter in zip(
828
+ self._fun_filter,
829
+ self._maxcv_filter,
830
+ )
831
+ )
832
+ elif np.isnan(maxcv_val):
833
+ include_point = all(
834
+ np.isnan(maxcv_filter)
835
+ and fun_val < fun_filter
836
+ or np.isnan(fun_filter)
837
+ for fun_filter, maxcv_filter in zip(
838
+ self._fun_filter,
839
+ self._maxcv_filter,
840
+ )
841
+ )
842
+ else:
843
+ include_point = all(
844
+ fun_val < fun_filter or maxcv_val < maxcv_filter
845
+ for fun_filter, maxcv_filter in zip(
846
+ self._fun_filter,
847
+ self._maxcv_filter,
848
+ )
849
+ )
850
+ if include_point:
851
+ self._fun_filter.append(fun_val)
852
+ self._maxcv_filter.append(maxcv_val)
853
+ self._x_filter.append(x)
854
+
855
+ # Remove the points in the filter that are dominated by the new
856
+ # point. We must iterate in reverse order to avoid problems when
857
+ # removing elements from the list.
858
+ for k in range(len(self._fun_filter) - 2, -1, -1):
859
+ if np.isnan(fun_val):
860
+ remove_point = np.isnan(self._fun_filter[k])
861
+ elif np.isnan(maxcv_val):
862
+ remove_point = np.isnan(self._maxcv_filter[k])
863
+ else:
864
+ remove_point = (
865
+ np.isnan(self._fun_filter[k])
866
+ or np.isnan(self._maxcv_filter[k])
867
+ or fun_val <= self._fun_filter[k]
868
+ and maxcv_val <= self._maxcv_filter[k]
869
+ )
870
+ if remove_point:
871
+ self._fun_filter.pop(k)
872
+ self._maxcv_filter.pop(k)
873
+ self._x_filter.pop(k)
874
+
875
+ # Keep only the most recent points in the filter.
876
+ if len(self._fun_filter) > self._filter_size:
877
+ self._fun_filter.pop(0)
878
+ self._maxcv_filter.pop(0)
879
+ self._x_filter.pop(0)
880
+
881
+ # Evaluate the callback function after updating the filter to ensure
882
+ # that the current point can be returned by the method.
883
+ if self._callback is not None:
884
+ sig = signature(self._callback)
885
+ try:
886
+ x_best, fun_best, _ = self.best_eval(penalty)
887
+ x_best = self.build_x(x_best)
888
+ if set(sig.parameters) == {"intermediate_result"}:
889
+ intermediate_result = OptimizeResult(
890
+ x=x_best,
891
+ fun=fun_best,
892
+ # maxcv=maxcv_best,
893
+ )
894
+ self._callback(intermediate_result=intermediate_result)
895
+ else:
896
+ self._callback(x_best)
897
+ except StopIteration as exc:
898
+ raise CallbackSuccess from exc
899
+
900
+ # Apply the extreme barriers and return.
901
+ if np.isnan(fun_val):
902
+ fun_val = BARRIER
903
+ cub_val[np.isnan(cub_val)] = BARRIER
904
+ ceq_val[np.isnan(ceq_val)] = BARRIER
905
+ fun_val = max(min(fun_val, BARRIER), -BARRIER)
906
+ cub_val = np.maximum(np.minimum(cub_val, BARRIER), -BARRIER)
907
+ ceq_val = np.maximum(np.minimum(ceq_val, BARRIER), -BARRIER)
908
+ return fun_val, cub_val, ceq_val
909
+
910
+ @property
911
+ def n(self):
912
+ """
913
+ Number of variables.
914
+
915
+ Returns
916
+ -------
917
+ int
918
+ Number of variables.
919
+ """
920
+ return self.x0.size
921
+
922
+ @property
923
+ def n_orig(self):
924
+ """
925
+ Number of variables in the original problem (with fixed variables).
926
+
927
+ Returns
928
+ -------
929
+ int
930
+ Number of variables in the original problem (with fixed variables).
931
+ """
932
+ return self._fixed_idx.size
933
+
934
+ @property
935
+ def x0(self):
936
+ """
937
+ Initial guess.
938
+
939
+ Returns
940
+ -------
941
+ `numpy.ndarray`, shape (n,)
942
+ Initial guess.
943
+ """
944
+ return self._x0
945
+
946
+ @property
947
+ def n_eval(self):
948
+ """
949
+ Number of function evaluations.
950
+
951
+ Returns
952
+ -------
953
+ int
954
+ Number of function evaluations.
955
+ """
956
+ return self._obj.n_eval
957
+
958
+ @property
959
+ def fun_name(self):
960
+ """
961
+ Name of the objective function.
962
+
963
+ Returns
964
+ -------
965
+ str
966
+ Name of the objective function.
967
+ """
968
+ return self._obj.name
969
+
970
+ @property
971
+ def bounds(self):
972
+ """
973
+ Bound constraints.
974
+
975
+ Returns
976
+ -------
977
+ BoundConstraints
978
+ Bound constraints.
979
+ """
980
+ return self._bounds
981
+
982
+ @property
983
+ def linear(self):
984
+ """
985
+ Linear constraints.
986
+
987
+ Returns
988
+ -------
989
+ LinearConstraints
990
+ Linear constraints.
991
+ """
992
+ return self._linear
993
+
994
+ @property
995
+ def m_bounds(self):
996
+ """
997
+ Number of bound constraints.
998
+
999
+ Returns
1000
+ -------
1001
+ int
1002
+ Number of bound constraints.
1003
+ """
1004
+ return self.bounds.m
1005
+
1006
+ @property
1007
+ def m_linear_ub(self):
1008
+ """
1009
+ Number of linear inequality constraints.
1010
+
1011
+ Returns
1012
+ -------
1013
+ int
1014
+ Number of linear inequality constraints.
1015
+ """
1016
+ return self.linear.m_ub
1017
+
1018
+ @property
1019
+ def m_linear_eq(self):
1020
+ """
1021
+ Number of linear equality constraints.
1022
+
1023
+ Returns
1024
+ -------
1025
+ int
1026
+ Number of linear equality constraints.
1027
+ """
1028
+ return self.linear.m_eq
1029
+
1030
+ @property
1031
+ def m_nonlinear_ub(self):
1032
+ """
1033
+ Number of nonlinear inequality constraints.
1034
+
1035
+ Returns
1036
+ -------
1037
+ int
1038
+ Number of nonlinear inequality constraints.
1039
+
1040
+ Raises
1041
+ ------
1042
+ ValueError
1043
+ If the number of nonlinear inequality constraints is not known.
1044
+ """
1045
+ return self._nonlinear.m_ub
1046
+
1047
+ @property
1048
+ def m_nonlinear_eq(self):
1049
+ """
1050
+ Number of nonlinear equality constraints.
1051
+
1052
+ Returns
1053
+ -------
1054
+ int
1055
+ Number of nonlinear equality constraints.
1056
+
1057
+ Raises
1058
+ ------
1059
+ ValueError
1060
+ If the number of nonlinear equality constraints is not known.
1061
+ """
1062
+ return self._nonlinear.m_eq
1063
+
1064
+ @property
1065
+ def fun_history(self):
1066
+ """
1067
+ History of objective function evaluations.
1068
+
1069
+ Returns
1070
+ -------
1071
+ `numpy.ndarray`, shape (n_eval,)
1072
+ History of objective function evaluations.
1073
+ """
1074
+ return np.array(self._fun_history, dtype=float)
1075
+
1076
+ @property
1077
+ def maxcv_history(self):
1078
+ """
1079
+ History of maximum constraint violations.
1080
+
1081
+ Returns
1082
+ -------
1083
+ `numpy.ndarray`, shape (n_eval,)
1084
+ History of maximum constraint violations.
1085
+ """
1086
+ return np.array(self._maxcv_history, dtype=float)
1087
+
1088
+ @property
1089
+ def type(self):
1090
+ """
1091
+ Type of the problem.
1092
+
1093
+ The problem can be either 'unconstrained', 'bound-constrained',
1094
+ 'linearly constrained', or 'nonlinearly constrained'.
1095
+
1096
+ Returns
1097
+ -------
1098
+ str
1099
+ Type of the problem.
1100
+ """
1101
+ try:
1102
+ if self.m_nonlinear_ub > 0 or self.m_nonlinear_eq > 0:
1103
+ return "nonlinearly constrained"
1104
+ elif self.m_linear_ub > 0 or self.m_linear_eq > 0:
1105
+ return "linearly constrained"
1106
+ elif self.m_bounds > 0:
1107
+ return "bound-constrained"
1108
+ else:
1109
+ return "unconstrained"
1110
+ except ValueError:
1111
+ # The number of nonlinear constraints is not known. It may be zero
1112
+ # if the user provided a nonlinear inequality and/or equality
1113
+ # constraint function that returns an empty array. However, as this
1114
+ # is not known before the first call to the function, we assume
1115
+ # that the problem is nonlinearly constrained.
1116
+ return "nonlinearly constrained"
1117
+
1118
+ @property
1119
+ def is_feasibility(self):
1120
+ """
1121
+ Whether the problem is a feasibility problem.
1122
+
1123
+ Returns
1124
+ -------
1125
+ bool
1126
+ Whether the problem is a feasibility problem.
1127
+ """
1128
+ return self.fun_name == ""
1129
+
1130
+ def build_x(self, x):
1131
+ """
1132
+ Build the full vector of variables from the reduced vector.
1133
+
1134
+ Parameters
1135
+ ----------
1136
+ x : array_like, shape (n,)
1137
+ Reduced vector of variables.
1138
+
1139
+ Returns
1140
+ -------
1141
+ `numpy.ndarray`, shape (n_orig,)
1142
+ Full vector of variables.
1143
+ """
1144
+ x_full = np.empty(self.n_orig)
1145
+ x_full[self._fixed_idx] = self._fixed_val
1146
+ x_full[~self._fixed_idx] = (x * self._scaling_factor
1147
+ + self._scaling_shift)
1148
+ return self._orig_bounds.project(x_full)
1149
+
1150
+ def maxcv(self, x, cub_val=None, ceq_val=None):
1151
+ """
1152
+ Evaluate the maximum constraint violation.
1153
+
1154
+ Parameters
1155
+ ----------
1156
+ x : array_like, shape (n,)
1157
+ Point at which the maximum constraint violation is evaluated.
1158
+ cub_val : array_like, shape (m_nonlinear_ub,), optional
1159
+ Values of the nonlinear inequality constraints. If not provided,
1160
+ the nonlinear inequality constraints are evaluated at `x`.
1161
+ ceq_val : array_like, shape (m_nonlinear_eq,), optional
1162
+ Values of the nonlinear equality constraints. If not provided,
1163
+ the nonlinear equality constraints are evaluated at `x`.
1164
+
1165
+ Returns
1166
+ -------
1167
+ float
1168
+ Maximum constraint violation at `x`.
1169
+ """
1170
+ violation = self.violation(x, cub_val=cub_val, ceq_val=ceq_val)
1171
+ if np.count_nonzero(violation):
1172
+ return np.max(violation, initial=0.0)
1173
+ else:
1174
+ return 0.0
1175
+
1176
+ def violation(self, x, cub_val=None, ceq_val=None):
1177
+ violation = []
1178
+ if not self.bounds.is_feasible:
1179
+ b = self.bounds.violation(x)
1180
+ violation.append(b)
1181
+
1182
+ if len(self.linear.pcs):
1183
+ lc = self.linear.violation(x)
1184
+ violation.append(lc)
1185
+ if len(self._nonlinear.pcs):
1186
+ nlc = self._nonlinear.violation(x, cub_val, ceq_val)
1187
+ violation.append(nlc)
1188
+
1189
+ if len(violation):
1190
+ return np.concatenate(violation)
1191
+
1192
+ def best_eval(self, penalty):
1193
+ """
1194
+ Return the best point in the filter and the corresponding objective and
1195
+ nonlinear constraint function evaluations.
1196
+
1197
+ Parameters
1198
+ ----------
1199
+ penalty : float
1200
+ Penalty parameter
1201
+
1202
+ Returns
1203
+ -------
1204
+ `numpy.ndarray`, shape (n,)
1205
+ Best point.
1206
+ float
1207
+ Corresponding objective function value.
1208
+ float
1209
+ Corresponding maximum constraint violation.
1210
+ """
1211
+ # If the filter is empty, i.e., if no function evaluation has been
1212
+ # performed, we evaluate the objective and nonlinear constraint
1213
+ # functions at the initial guess.
1214
+ if len(self._fun_filter) == 0:
1215
+ self(self.x0)
1216
+
1217
+ # Find the best point in the filter.
1218
+ fun_filter = np.array(self._fun_filter)
1219
+ maxcv_filter = np.array(self._maxcv_filter)
1220
+ x_filter = np.array(self._x_filter)
1221
+ finite_idx = np.isfinite(maxcv_filter)
1222
+ if np.any(finite_idx):
1223
+ # At least one point has a finite maximum constraint violation.
1224
+ feasible_idx = maxcv_filter <= self._feasibility_tol
1225
+ if np.any(feasible_idx) and not np.all(
1226
+ np.isnan(fun_filter[feasible_idx])
1227
+ ):
1228
+ # At least one point is feasible and has a well-defined
1229
+ # objective function value. We select the point with the least
1230
+ # objective function value. If there is a tie, we select the
1231
+ # point with the least maximum constraint violation. If there
1232
+ # is still a tie, we select the most recent point.
1233
+ fun_min_idx = feasible_idx & (
1234
+ fun_filter <= np.nanmin(fun_filter[feasible_idx])
1235
+ )
1236
+ if np.count_nonzero(fun_min_idx) > 1:
1237
+ fun_min_idx &= maxcv_filter <= np.min(
1238
+ maxcv_filter[fun_min_idx]
1239
+ )
1240
+ i = np.flatnonzero(fun_min_idx)[-1]
1241
+ elif np.any(feasible_idx):
1242
+ # At least one point is feasible but no feasible point has a
1243
+ # well-defined objective function value. We select the most
1244
+ # recent feasible point.
1245
+ i = np.flatnonzero(feasible_idx)[-1]
1246
+ else:
1247
+ # No point is feasible. We first compute the merit function
1248
+ # value for each point.
1249
+ merit_filter = np.full_like(fun_filter, np.nan)
1250
+ merit_filter[finite_idx] = (
1251
+ fun_filter[finite_idx] + penalty * maxcv_filter[finite_idx]
1252
+ )
1253
+ if np.all(np.isnan(merit_filter)):
1254
+ # No point has a well-defined merit function value. In
1255
+ # other words, among the points with a well-defined maximum
1256
+ # constraint violation, none has a well-defined objective
1257
+ # function value. We select the point with the least
1258
+ # maximum constraint violation. If there is a tie, we
1259
+ # select the most recent point.
1260
+ min_maxcv_idx = maxcv_filter <= np.nanmin(maxcv_filter)
1261
+ i = np.flatnonzero(min_maxcv_idx)[-1]
1262
+ else:
1263
+ # At least one point has a well-defined merit function
1264
+ # value. We select the point with the least merit function
1265
+ # value. If there is a tie, we select the point with the
1266
+ # least maximum constraint violation. If there is still a
1267
+ # tie, we select the point with the least objective
1268
+ # function value. If there is still a tie, we select the
1269
+ # most recent point.
1270
+ merit_min_idx = merit_filter <= np.nanmin(merit_filter)
1271
+ if np.count_nonzero(merit_min_idx) > 1:
1272
+ merit_min_idx &= maxcv_filter <= np.min(
1273
+ maxcv_filter[merit_min_idx]
1274
+ )
1275
+
1276
+ if np.count_nonzero(merit_min_idx) > 1:
1277
+ merit_min_idx &= fun_filter <= np.min(
1278
+ fun_filter[merit_min_idx]
1279
+ )
1280
+ i = np.flatnonzero(merit_min_idx)[-1]
1281
+ elif not np.all(np.isnan(fun_filter)):
1282
+ # No maximum constraint violation is well-defined but at least one
1283
+ # point has a well-defined objective function value. We select the
1284
+ # point with the least objective function value. If there is a tie,
1285
+ # we select the most recent point.
1286
+ fun_min_idx = fun_filter <= np.nanmin(fun_filter)
1287
+ i = np.flatnonzero(fun_min_idx)[-1]
1288
+ else:
1289
+ # No point has a well-defined maximum constraint violation or
1290
+ # objective function value. We select the most recent point.
1291
+ i = len(fun_filter) - 1
1292
+ return (
1293
+ self.bounds.project(x_filter[i, :]),
1294
+ fun_filter[i],
1295
+ maxcv_filter[i],
1296
+ )
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/settings.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ from enum import Enum
3
+
4
+ import numpy as np
5
+
6
+
7
+ # Exit status.
8
+ class ExitStatus(Enum):
9
+ """
10
+ Exit statuses.
11
+ """
12
+
13
+ RADIUS_SUCCESS = 0
14
+ TARGET_SUCCESS = 1
15
+ FIXED_SUCCESS = 2
16
+ CALLBACK_SUCCESS = 3
17
+ FEASIBLE_SUCCESS = 4
18
+ MAX_EVAL_WARNING = 5
19
+ MAX_ITER_WARNING = 6
20
+ INFEASIBLE_ERROR = -1
21
+ LINALG_ERROR = -2
22
+
23
+
24
+ class Options(str, Enum):
25
+ """
26
+ Options.
27
+ """
28
+
29
+ DEBUG = "debug"
30
+ FEASIBILITY_TOL = "feasibility_tol"
31
+ FILTER_SIZE = "filter_size"
32
+ HISTORY_SIZE = "history_size"
33
+ MAX_EVAL = "maxfev"
34
+ MAX_ITER = "maxiter"
35
+ NPT = "nb_points"
36
+ RHOBEG = "radius_init"
37
+ RHOEND = "radius_final"
38
+ SCALE = "scale"
39
+ STORE_HISTORY = "store_history"
40
+ TARGET = "target"
41
+ VERBOSE = "disp"
42
+
43
+
44
+ class Constants(str, Enum):
45
+ """
46
+ Constants.
47
+ """
48
+
49
+ DECREASE_RADIUS_FACTOR = "decrease_radius_factor"
50
+ INCREASE_RADIUS_FACTOR = "increase_radius_factor"
51
+ INCREASE_RADIUS_THRESHOLD = "increase_radius_threshold"
52
+ DECREASE_RADIUS_THRESHOLD = "decrease_radius_threshold"
53
+ DECREASE_RESOLUTION_FACTOR = "decrease_resolution_factor"
54
+ LARGE_RESOLUTION_THRESHOLD = "large_resolution_threshold"
55
+ MODERATE_RESOLUTION_THRESHOLD = "moderate_resolution_threshold"
56
+ LOW_RATIO = "low_ratio"
57
+ HIGH_RATIO = "high_ratio"
58
+ VERY_LOW_RATIO = "very_low_ratio"
59
+ PENALTY_INCREASE_THRESHOLD = "penalty_increase_threshold"
60
+ PENALTY_INCREASE_FACTOR = "penalty_increase_factor"
61
+ SHORT_STEP_THRESHOLD = "short_step_threshold"
62
+ LOW_RADIUS_FACTOR = "low_radius_factor"
63
+ BYRD_OMOJOKUN_FACTOR = "byrd_omojokun_factor"
64
+ THRESHOLD_RATIO_CONSTRAINTS = "threshold_ratio_constraints"
65
+ LARGE_SHIFT_FACTOR = "large_shift_factor"
66
+ LARGE_GRADIENT_FACTOR = "large_gradient_factor"
67
+ RESOLUTION_FACTOR = "resolution_factor"
68
+ IMPROVE_TCG = "improve_tcg"
69
+
70
+
71
+ # Default options.
72
+ DEFAULT_OPTIONS = {
73
+ Options.DEBUG.value: False,
74
+ Options.FEASIBILITY_TOL.value: np.sqrt(np.finfo(float).eps),
75
+ Options.FILTER_SIZE.value: sys.maxsize,
76
+ Options.HISTORY_SIZE.value: sys.maxsize,
77
+ Options.MAX_EVAL.value: lambda n: 500 * n,
78
+ Options.MAX_ITER.value: lambda n: 1000 * n,
79
+ Options.NPT.value: lambda n: 2 * n + 1,
80
+ Options.RHOBEG.value: 1.0,
81
+ Options.RHOEND.value: 1e-6,
82
+ Options.SCALE.value: False,
83
+ Options.STORE_HISTORY.value: False,
84
+ Options.TARGET.value: -np.inf,
85
+ Options.VERBOSE.value: False,
86
+ }
87
+
88
+ # Default constants.
89
+ DEFAULT_CONSTANTS = {
90
+ Constants.DECREASE_RADIUS_FACTOR.value: 0.5,
91
+ Constants.INCREASE_RADIUS_FACTOR.value: np.sqrt(2.0),
92
+ Constants.INCREASE_RADIUS_THRESHOLD.value: 2.0,
93
+ Constants.DECREASE_RADIUS_THRESHOLD.value: 1.4,
94
+ Constants.DECREASE_RESOLUTION_FACTOR.value: 0.1,
95
+ Constants.LARGE_RESOLUTION_THRESHOLD.value: 250.0,
96
+ Constants.MODERATE_RESOLUTION_THRESHOLD.value: 16.0,
97
+ Constants.LOW_RATIO.value: 0.1,
98
+ Constants.HIGH_RATIO.value: 0.7,
99
+ Constants.VERY_LOW_RATIO.value: 0.01,
100
+ Constants.PENALTY_INCREASE_THRESHOLD.value: 1.5,
101
+ Constants.PENALTY_INCREASE_FACTOR.value: 2.0,
102
+ Constants.SHORT_STEP_THRESHOLD.value: 0.5,
103
+ Constants.LOW_RADIUS_FACTOR.value: 0.1,
104
+ Constants.BYRD_OMOJOKUN_FACTOR.value: 0.8,
105
+ Constants.THRESHOLD_RATIO_CONSTRAINTS.value: 2.0,
106
+ Constants.LARGE_SHIFT_FACTOR.value: 10.0,
107
+ Constants.LARGE_GRADIENT_FACTOR.value: 10.0,
108
+ Constants.RESOLUTION_FACTOR.value: 2.0,
109
+ Constants.IMPROVE_TCG.value: True,
110
+ }
111
+
112
+ # Printing options.
113
+ PRINT_OPTIONS = {
114
+ "threshold": 6,
115
+ "edgeitems": 2,
116
+ "linewidth": sys.maxsize,
117
+ "formatter": {
118
+ "float_kind": lambda x: np.format_float_scientific(
119
+ x,
120
+ precision=3,
121
+ unique=False,
122
+ pad_left=2,
123
+ )
124
+ },
125
+ }
126
+
127
+ # Constants.
128
+ BARRIER = 2.0 ** min(
129
+ 100,
130
+ np.finfo(float).maxexp // 2,
131
+ -np.finfo(float).minexp // 2,
132
+ )
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/subsolvers/__init__.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from .geometry import cauchy_geometry, spider_geometry
2
+ from .optim import (
3
+ tangential_byrd_omojokun,
4
+ constrained_tangential_byrd_omojokun,
5
+ normal_byrd_omojokun,
6
+ )
7
+
8
+ __all__ = [
9
+ "cauchy_geometry",
10
+ "spider_geometry",
11
+ "tangential_byrd_omojokun",
12
+ "constrained_tangential_byrd_omojokun",
13
+ "normal_byrd_omojokun",
14
+ ]
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/subsolvers/geometry.py ADDED
@@ -0,0 +1,387 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import inspect
2
+
3
+ import numpy as np
4
+
5
+ from ..utils import get_arrays_tol
6
+
7
+
8
+ TINY = np.finfo(float).tiny
9
+
10
+
11
+ def cauchy_geometry(const, grad, curv, xl, xu, delta, debug):
12
+ r"""
13
+ Maximize approximately the absolute value of a quadratic function subject
14
+ to bound constraints in a trust region.
15
+
16
+ This function solves approximately
17
+
18
+ .. math::
19
+
20
+ \max_{s \in \mathbb{R}^n} \quad \bigg\lvert c + g^{\mathsf{T}} s +
21
+ \frac{1}{2} s^{\mathsf{T}} H s \bigg\rvert \quad \text{s.t.} \quad
22
+ \left\{ \begin{array}{l}
23
+ l \le s \le u,\\
24
+ \lVert s \rVert \le \Delta,
25
+ \end{array} \right.
26
+
27
+ by maximizing the objective function along the constrained Cauchy
28
+ direction.
29
+
30
+ Parameters
31
+ ----------
32
+ const : float
33
+ Constant :math:`c` as shown above.
34
+ grad : `numpy.ndarray`, shape (n,)
35
+ Gradient :math:`g` as shown above.
36
+ curv : callable
37
+ Curvature of :math:`H` along any vector.
38
+
39
+ ``curv(s) -> float``
40
+
41
+ returns :math:`s^{\mathsf{T}} H s`.
42
+ xl : `numpy.ndarray`, shape (n,)
43
+ Lower bounds :math:`l` as shown above.
44
+ xu : `numpy.ndarray`, shape (n,)
45
+ Upper bounds :math:`u` as shown above.
46
+ delta : float
47
+ Trust-region radius :math:`\Delta` as shown above.
48
+ debug : bool
49
+ Whether to make debugging tests during the execution.
50
+
51
+ Returns
52
+ -------
53
+ `numpy.ndarray`, shape (n,)
54
+ Approximate solution :math:`s`.
55
+
56
+ Notes
57
+ -----
58
+ This function is described as the first alternative in Section 6.5 of [1]_.
59
+ It is assumed that the origin is feasible with respect to the bound
60
+ constraints and that `delta` is finite and positive.
61
+
62
+ References
63
+ ----------
64
+ .. [1] T. M. Ragonneau. *Model-Based Derivative-Free Optimization Methods
65
+ and Software*. PhD thesis, Department of Applied Mathematics, The Hong
66
+ Kong Polytechnic University, Hong Kong, China, 2022. URL:
67
+ https://theses.lib.polyu.edu.hk/handle/200/12294.
68
+ """
69
+ if debug:
70
+ assert isinstance(const, float)
71
+ assert isinstance(grad, np.ndarray) and grad.ndim == 1
72
+ assert inspect.signature(curv).bind(grad)
73
+ assert isinstance(xl, np.ndarray) and xl.shape == grad.shape
74
+ assert isinstance(xu, np.ndarray) and xu.shape == grad.shape
75
+ assert isinstance(delta, float)
76
+ assert isinstance(debug, bool)
77
+ tol = get_arrays_tol(xl, xu)
78
+ assert np.all(xl <= tol)
79
+ assert np.all(xu >= -tol)
80
+ assert np.isfinite(delta) and delta > 0.0
81
+ xl = np.minimum(xl, 0.0)
82
+ xu = np.maximum(xu, 0.0)
83
+
84
+ # To maximize the absolute value of a quadratic function, we maximize the
85
+ # function itself or its negative, and we choose the solution that provides
86
+ # the largest function value.
87
+ step1, q_val1 = _cauchy_geom(const, grad, curv, xl, xu, delta, debug)
88
+ step2, q_val2 = _cauchy_geom(
89
+ -const,
90
+ -grad,
91
+ lambda x: -curv(x),
92
+ xl,
93
+ xu,
94
+ delta,
95
+ debug,
96
+ )
97
+ step = step1 if abs(q_val1) >= abs(q_val2) else step2
98
+
99
+ if debug:
100
+ assert np.all(xl <= step)
101
+ assert np.all(step <= xu)
102
+ assert np.linalg.norm(step) < 1.1 * delta
103
+ return step
104
+
105
+
106
+ def spider_geometry(const, grad, curv, xpt, xl, xu, delta, debug):
107
+ r"""
108
+ Maximize approximately the absolute value of a quadratic function subject
109
+ to bound constraints in a trust region.
110
+
111
+ This function solves approximately
112
+
113
+ .. math::
114
+
115
+ \max_{s \in \mathbb{R}^n} \quad \bigg\lvert c + g^{\mathsf{T}} s +
116
+ \frac{1}{2} s^{\mathsf{T}} H s \bigg\rvert \quad \text{s.t.} \quad
117
+ \left\{ \begin{array}{l}
118
+ l \le s \le u,\\
119
+ \lVert s \rVert \le \Delta,
120
+ \end{array} \right.
121
+
122
+ by maximizing the objective function along given straight lines.
123
+
124
+ Parameters
125
+ ----------
126
+ const : float
127
+ Constant :math:`c` as shown above.
128
+ grad : `numpy.ndarray`, shape (n,)
129
+ Gradient :math:`g` as shown above.
130
+ curv : callable
131
+ Curvature of :math:`H` along any vector.
132
+
133
+ ``curv(s) -> float``
134
+
135
+ returns :math:`s^{\mathsf{T}} H s`.
136
+ xpt : `numpy.ndarray`, shape (n, npt)
137
+ Points defining the straight lines. The straight lines considered are
138
+ the ones passing through the origin and the points in `xpt`.
139
+ xl : `numpy.ndarray`, shape (n,)
140
+ Lower bounds :math:`l` as shown above.
141
+ xu : `numpy.ndarray`, shape (n,)
142
+ Upper bounds :math:`u` as shown above.
143
+ delta : float
144
+ Trust-region radius :math:`\Delta` as shown above.
145
+ debug : bool
146
+ Whether to make debugging tests during the execution.
147
+
148
+ Returns
149
+ -------
150
+ `numpy.ndarray`, shape (n,)
151
+ Approximate solution :math:`s`.
152
+
153
+ Notes
154
+ -----
155
+ This function is described as the second alternative in Section 6.5 of
156
+ [1]_. It is assumed that the origin is feasible with respect to the bound
157
+ constraints and that `delta` is finite and positive.
158
+
159
+ References
160
+ ----------
161
+ .. [1] T. M. Ragonneau. *Model-Based Derivative-Free Optimization Methods
162
+ and Software*. PhD thesis, Department of Applied Mathematics, The Hong
163
+ Kong Polytechnic University, Hong Kong, China, 2022. URL:
164
+ https://theses.lib.polyu.edu.hk/handle/200/12294.
165
+ """
166
+ if debug:
167
+ assert isinstance(const, float)
168
+ assert isinstance(grad, np.ndarray) and grad.ndim == 1
169
+ assert inspect.signature(curv).bind(grad)
170
+ assert (
171
+ isinstance(xpt, np.ndarray)
172
+ and xpt.ndim == 2
173
+ and xpt.shape[0] == grad.size
174
+ )
175
+ assert isinstance(xl, np.ndarray) and xl.shape == grad.shape
176
+ assert isinstance(xu, np.ndarray) and xu.shape == grad.shape
177
+ assert isinstance(delta, float)
178
+ assert isinstance(debug, bool)
179
+ tol = get_arrays_tol(xl, xu)
180
+ assert np.all(xl <= tol)
181
+ assert np.all(xu >= -tol)
182
+ assert np.isfinite(delta) and delta > 0.0
183
+ xl = np.minimum(xl, 0.0)
184
+ xu = np.maximum(xu, 0.0)
185
+
186
+ # Iterate through the straight lines.
187
+ step = np.zeros_like(grad)
188
+ q_val = const
189
+ s_norm = np.linalg.norm(xpt, axis=0)
190
+
191
+ # Set alpha_xl to the step size for the lower-bound constraint and
192
+ # alpha_xu to the step size for the upper-bound constraint.
193
+
194
+ # xl.shape = (N,)
195
+ # xpt.shape = (N, M)
196
+ # i_xl_pos.shape = (M, N)
197
+ i_xl_pos = (xl > -np.inf) & (xpt.T > -TINY * xl)
198
+ i_xl_neg = (xl > -np.inf) & (xpt.T < TINY * xl)
199
+ i_xu_pos = (xu < np.inf) & (xpt.T > TINY * xu)
200
+ i_xu_neg = (xu < np.inf) & (xpt.T < -TINY * xu)
201
+
202
+ # (M, N)
203
+ alpha_xl_pos = np.atleast_2d(
204
+ np.broadcast_to(xl, i_xl_pos.shape)[i_xl_pos] / xpt.T[i_xl_pos]
205
+ )
206
+ # (M,)
207
+ alpha_xl_pos = np.max(alpha_xl_pos, axis=1, initial=-np.inf)
208
+ # make sure it's (M,)
209
+ alpha_xl_pos = np.broadcast_to(np.atleast_1d(alpha_xl_pos), xpt.shape[1])
210
+
211
+ alpha_xl_neg = np.atleast_2d(
212
+ np.broadcast_to(xl, i_xl_neg.shape)[i_xl_neg] / xpt.T[i_xl_neg]
213
+ )
214
+ alpha_xl_neg = np.max(alpha_xl_neg, axis=1, initial=np.inf)
215
+ alpha_xl_neg = np.broadcast_to(np.atleast_1d(alpha_xl_neg), xpt.shape[1])
216
+
217
+ alpha_xu_neg = np.atleast_2d(
218
+ np.broadcast_to(xu, i_xu_neg.shape)[i_xu_neg] / xpt.T[i_xu_neg]
219
+ )
220
+ alpha_xu_neg = np.max(alpha_xu_neg, axis=1, initial=-np.inf)
221
+ alpha_xu_neg = np.broadcast_to(np.atleast_1d(alpha_xu_neg), xpt.shape[1])
222
+
223
+ alpha_xu_pos = np.atleast_2d(
224
+ np.broadcast_to(xu, i_xu_pos.shape)[i_xu_pos] / xpt.T[i_xu_pos]
225
+ )
226
+ alpha_xu_pos = np.max(alpha_xu_pos, axis=1, initial=np.inf)
227
+ alpha_xu_pos = np.broadcast_to(np.atleast_1d(alpha_xu_pos), xpt.shape[1])
228
+
229
+ for k in range(xpt.shape[1]):
230
+ # Set alpha_tr to the step size for the trust-region constraint.
231
+ if s_norm[k] > TINY * delta:
232
+ alpha_tr = max(delta / s_norm[k], 0.0)
233
+ else:
234
+ # The current straight line is basically zero.
235
+ continue
236
+
237
+ alpha_bd_pos = max(min(alpha_xu_pos[k], alpha_xl_neg[k]), 0.0)
238
+ alpha_bd_neg = min(max(alpha_xl_pos[k], alpha_xu_neg[k]), 0.0)
239
+
240
+ # Set alpha_quad_pos and alpha_quad_neg to the step size to the extrema
241
+ # of the quadratic function along the positive and negative directions.
242
+ grad_step = grad @ xpt[:, k]
243
+ curv_step = curv(xpt[:, k])
244
+ if (
245
+ grad_step >= 0.0
246
+ and curv_step < -TINY * grad_step
247
+ or grad_step <= 0.0
248
+ and curv_step > -TINY * grad_step
249
+ ):
250
+ alpha_quad_pos = max(-grad_step / curv_step, 0.0)
251
+ else:
252
+ alpha_quad_pos = np.inf
253
+ if (
254
+ grad_step >= 0.0
255
+ and curv_step > TINY * grad_step
256
+ or grad_step <= 0.0
257
+ and curv_step < TINY * grad_step
258
+ ):
259
+ alpha_quad_neg = min(-grad_step / curv_step, 0.0)
260
+ else:
261
+ alpha_quad_neg = -np.inf
262
+
263
+ # Select the step that provides the largest value of the objective
264
+ # function if it improves the current best. The best positive step is
265
+ # either the one that reaches the constraints or the one that reaches
266
+ # the extremum of the objective function along the current direction
267
+ # (only possible if the resulting step is feasible). We test both, and
268
+ # we perform similar calculations along the negative step.
269
+ # N.B.: we select the largest possible step among all the ones that
270
+ # maximize the objective function. This is to avoid returning the zero
271
+ # step in some extreme cases.
272
+ alpha_pos = min(alpha_tr, alpha_bd_pos)
273
+ alpha_neg = max(-alpha_tr, alpha_bd_neg)
274
+ q_val_pos = (
275
+ const + alpha_pos * grad_step + 0.5 * alpha_pos**2.0 * curv_step
276
+ )
277
+ q_val_neg = (
278
+ const + alpha_neg * grad_step + 0.5 * alpha_neg**2.0 * curv_step
279
+ )
280
+ if alpha_quad_pos < alpha_pos:
281
+ q_val_quad_pos = (
282
+ const
283
+ + alpha_quad_pos * grad_step
284
+ + 0.5 * alpha_quad_pos**2.0 * curv_step
285
+ )
286
+ if abs(q_val_quad_pos) > abs(q_val_pos):
287
+ alpha_pos = alpha_quad_pos
288
+ q_val_pos = q_val_quad_pos
289
+ if alpha_quad_neg > alpha_neg:
290
+ q_val_quad_neg = (
291
+ const
292
+ + alpha_quad_neg * grad_step
293
+ + 0.5 * alpha_quad_neg**2.0 * curv_step
294
+ )
295
+ if abs(q_val_quad_neg) > abs(q_val_neg):
296
+ alpha_neg = alpha_quad_neg
297
+ q_val_neg = q_val_quad_neg
298
+ if abs(q_val_pos) >= abs(q_val_neg) and abs(q_val_pos) > abs(q_val):
299
+ step = np.clip(alpha_pos * xpt[:, k], xl, xu)
300
+ q_val = q_val_pos
301
+ elif abs(q_val_neg) > abs(q_val_pos) and abs(q_val_neg) > abs(q_val):
302
+ step = np.clip(alpha_neg * xpt[:, k], xl, xu)
303
+ q_val = q_val_neg
304
+
305
+ if debug:
306
+ assert np.all(xl <= step)
307
+ assert np.all(step <= xu)
308
+ assert np.linalg.norm(step) < 1.1 * delta
309
+ return step
310
+
311
+
312
+ def _cauchy_geom(const, grad, curv, xl, xu, delta, debug):
313
+ """
314
+ Same as `bound_constrained_cauchy_step` without the absolute value.
315
+ """
316
+ # Calculate the initial active set.
317
+ fixed_xl = (xl < 0.0) & (grad > 0.0)
318
+ fixed_xu = (xu > 0.0) & (grad < 0.0)
319
+
320
+ # Calculate the Cauchy step.
321
+ cauchy_step = np.zeros_like(grad)
322
+ cauchy_step[fixed_xl] = xl[fixed_xl]
323
+ cauchy_step[fixed_xu] = xu[fixed_xu]
324
+ if np.linalg.norm(cauchy_step) > delta:
325
+ working = fixed_xl | fixed_xu
326
+ while True:
327
+ # Calculate the Cauchy step for the directions in the working set.
328
+ g_norm = np.linalg.norm(grad[working])
329
+ delta_reduced = np.sqrt(
330
+ delta**2.0 - cauchy_step[~working] @ cauchy_step[~working]
331
+ )
332
+ if g_norm > TINY * abs(delta_reduced):
333
+ mu = max(delta_reduced / g_norm, 0.0)
334
+ else:
335
+ break
336
+ cauchy_step[working] = mu * grad[working]
337
+
338
+ # Update the working set.
339
+ fixed_xl = working & (cauchy_step < xl)
340
+ fixed_xu = working & (cauchy_step > xu)
341
+ if not np.any(fixed_xl) and not np.any(fixed_xu):
342
+ # Stop the calculations as the Cauchy step is now feasible.
343
+ break
344
+ cauchy_step[fixed_xl] = xl[fixed_xl]
345
+ cauchy_step[fixed_xu] = xu[fixed_xu]
346
+ working = working & ~(fixed_xl | fixed_xu)
347
+
348
+ # Calculate the step that maximizes the quadratic along the Cauchy step.
349
+ grad_step = grad @ cauchy_step
350
+ if grad_step >= 0.0:
351
+ # Set alpha_tr to the step size for the trust-region constraint.
352
+ s_norm = np.linalg.norm(cauchy_step)
353
+ if s_norm > TINY * delta:
354
+ alpha_tr = max(delta / s_norm, 0.0)
355
+ else:
356
+ # The Cauchy step is basically zero.
357
+ alpha_tr = 0.0
358
+
359
+ # Set alpha_quad to the step size for the maximization problem.
360
+ curv_step = curv(cauchy_step)
361
+ if curv_step < -TINY * grad_step:
362
+ alpha_quad = max(-grad_step / curv_step, 0.0)
363
+ else:
364
+ alpha_quad = np.inf
365
+
366
+ # Set alpha_bd to the step size for the bound constraints.
367
+ i_xl = (xl > -np.inf) & (cauchy_step < TINY * xl)
368
+ i_xu = (xu < np.inf) & (cauchy_step > TINY * xu)
369
+ alpha_xl = np.min(xl[i_xl] / cauchy_step[i_xl], initial=np.inf)
370
+ alpha_xu = np.min(xu[i_xu] / cauchy_step[i_xu], initial=np.inf)
371
+ alpha_bd = min(alpha_xl, alpha_xu)
372
+
373
+ # Calculate the solution and the corresponding function value.
374
+ alpha = min(alpha_tr, alpha_quad, alpha_bd)
375
+ step = np.clip(alpha * cauchy_step, xl, xu)
376
+ q_val = const + alpha * grad_step + 0.5 * alpha**2.0 * curv_step
377
+ else:
378
+ # This case is never reached in exact arithmetic. It prevents this
379
+ # function to return a step that decreases the objective function.
380
+ step = np.zeros_like(grad)
381
+ q_val = const
382
+
383
+ if debug:
384
+ assert np.all(xl <= step)
385
+ assert np.all(step <= xu)
386
+ assert np.linalg.norm(step) < 1.1 * delta
387
+ return step, q_val
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/subsolvers/optim.py ADDED
@@ -0,0 +1,1203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import inspect
2
+
3
+ import numpy as np
4
+ from scipy.linalg import qr
5
+
6
+ from ..utils import get_arrays_tol
7
+
8
+
9
+ TINY = np.finfo(float).tiny
10
+ EPS = np.finfo(float).eps
11
+
12
+
13
+ def tangential_byrd_omojokun(grad, hess_prod, xl, xu, delta, debug, **kwargs):
14
+ r"""
15
+ Minimize approximately a quadratic function subject to bound constraints in
16
+ a trust region.
17
+
18
+ This function solves approximately
19
+
20
+ .. math::
21
+
22
+ \min_{s \in \mathbb{R}^n} \quad g^{\mathsf{T}} s + \frac{1}{2}
23
+ s^{\mathsf{T}} H s \quad \text{s.t.} \quad
24
+ \left\{ \begin{array}{l}
25
+ l \le s \le u\\
26
+ \lVert s \rVert \le \Delta,
27
+ \end{array} \right.
28
+
29
+ using an active-set variation of the truncated conjugate gradient method.
30
+
31
+ Parameters
32
+ ----------
33
+ grad : `numpy.ndarray`, shape (n,)
34
+ Gradient :math:`g` as shown above.
35
+ hess_prod : callable
36
+ Product of the Hessian matrix :math:`H` with any vector.
37
+
38
+ ``hess_prod(s) -> `numpy.ndarray`, shape (n,)``
39
+
40
+ returns the product :math:`H s`.
41
+ xl : `numpy.ndarray`, shape (n,)
42
+ Lower bounds :math:`l` as shown above.
43
+ xu : `numpy.ndarray`, shape (n,)
44
+ Upper bounds :math:`u` as shown above.
45
+ delta : float
46
+ Trust-region radius :math:`\Delta` as shown above.
47
+ debug : bool
48
+ Whether to make debugging tests during the execution.
49
+
50
+ Returns
51
+ -------
52
+ `numpy.ndarray`, shape (n,)
53
+ Approximate solution :math:`s`.
54
+
55
+ Other Parameters
56
+ ----------------
57
+ improve_tcg : bool, optional
58
+ If True, a solution generated by the truncated conjugate gradient
59
+ method that is on the boundary of the trust region is improved by
60
+ moving around the trust-region boundary on the two-dimensional space
61
+ spanned by the solution and the gradient of the quadratic function at
62
+ the solution (default is True).
63
+
64
+ Notes
65
+ -----
66
+ This function implements Algorithm 6.2 of [1]_. It is assumed that the
67
+ origin is feasible with respect to the bound constraints and that `delta`
68
+ is finite and positive.
69
+
70
+ References
71
+ ----------
72
+ .. [1] T. M. Ragonneau. *Model-Based Derivative-Free Optimization Methods
73
+ and Software*. PhD thesis, Department of Applied Mathematics, The Hong
74
+ Kong Polytechnic University, Hong Kong, China, 2022. URL:
75
+ https://theses.lib.polyu.edu.hk/handle/200/12294.
76
+ """
77
+ if debug:
78
+ assert isinstance(grad, np.ndarray) and grad.ndim == 1
79
+ assert inspect.signature(hess_prod).bind(grad)
80
+ assert isinstance(xl, np.ndarray) and xl.shape == grad.shape
81
+ assert isinstance(xu, np.ndarray) and xu.shape == grad.shape
82
+ assert isinstance(delta, float)
83
+ assert isinstance(debug, bool)
84
+ tol = get_arrays_tol(xl, xu)
85
+ assert np.all(xl <= tol)
86
+ assert np.all(xu >= -tol)
87
+ assert np.isfinite(delta) and delta > 0.0
88
+ xl = np.minimum(xl, 0.0)
89
+ xu = np.maximum(xu, 0.0)
90
+
91
+ # Copy the arrays that may be modified by the code below.
92
+ n = grad.size
93
+ grad = np.copy(grad)
94
+ grad_orig = np.copy(grad)
95
+
96
+ # Calculate the initial active set.
97
+ free_bd = ((xl < 0.0) | (grad < 0.0)) & ((xu > 0.0) | (grad > 0.0))
98
+
99
+ # Set the initial iterate and the initial search direction.
100
+ step = np.zeros_like(grad)
101
+ sd = np.zeros_like(step)
102
+ sd[free_bd] = -grad[free_bd]
103
+
104
+ k = 0
105
+ reduct = 0.0
106
+ boundary_reached = False
107
+ while k < np.count_nonzero(free_bd):
108
+ # Stop the computations if sd is not a descent direction.
109
+ grad_sd = grad @ sd
110
+ if grad_sd >= -10.0 * EPS * n * max(1.0, np.linalg.norm(grad)):
111
+ break
112
+
113
+ # Set alpha_tr to the step size for the trust-region constraint.
114
+ try:
115
+ alpha_tr = _alpha_tr(step, sd, delta)
116
+ except ZeroDivisionError:
117
+ break
118
+
119
+ # Stop the computations if a step along sd is expected to give a
120
+ # relatively small reduction in the objective function.
121
+ if -alpha_tr * grad_sd <= 1e-8 * reduct:
122
+ break
123
+
124
+ # Set alpha_quad to the step size for the minimization problem.
125
+ hess_sd = hess_prod(sd)
126
+ curv_sd = sd @ hess_sd
127
+ if curv_sd > TINY * abs(grad_sd):
128
+ alpha_quad = max(-grad_sd / curv_sd, 0.0)
129
+ else:
130
+ alpha_quad = np.inf
131
+
132
+ # Stop the computations if the reduction in the objective function
133
+ # provided by an unconstrained step is small.
134
+ alpha = min(alpha_tr, alpha_quad)
135
+ if -alpha * (grad_sd + 0.5 * alpha * curv_sd) <= 1e-8 * reduct:
136
+ break
137
+
138
+ # Set alpha_bd to the step size for the bound constraints.
139
+ i_xl = (xl > -np.inf) & (sd < -TINY * np.abs(xl - step))
140
+ i_xu = (xu < np.inf) & (sd > TINY * np.abs(xu - step))
141
+ all_alpha_xl = np.full_like(step, np.inf)
142
+ all_alpha_xu = np.full_like(step, np.inf)
143
+ all_alpha_xl[i_xl] = np.maximum(
144
+ (xl[i_xl] - step[i_xl]) / sd[i_xl],
145
+ 0.0,
146
+ )
147
+ all_alpha_xu[i_xu] = np.maximum(
148
+ (xu[i_xu] - step[i_xu]) / sd[i_xu],
149
+ 0.0,
150
+ )
151
+ alpha_xl = np.min(all_alpha_xl)
152
+ alpha_xu = np.min(all_alpha_xu)
153
+ alpha_bd = min(alpha_xl, alpha_xu)
154
+
155
+ # Update the iterate.
156
+ alpha = min(alpha, alpha_bd)
157
+ if alpha > 0.0:
158
+ step[free_bd] = np.clip(
159
+ step[free_bd] + alpha * sd[free_bd],
160
+ xl[free_bd],
161
+ xu[free_bd],
162
+ )
163
+ grad += alpha * hess_sd
164
+ reduct -= alpha * (grad_sd + 0.5 * alpha * curv_sd)
165
+
166
+ if alpha < min(alpha_tr, alpha_bd):
167
+ # The current iteration is a conjugate gradient iteration. Update
168
+ # the search direction so that it is conjugate (with respect to H)
169
+ # to all the previous search directions.
170
+ beta = (grad[free_bd] @ hess_sd[free_bd]) / curv_sd
171
+ sd[free_bd] = beta * sd[free_bd] - grad[free_bd]
172
+ sd[~free_bd] = 0.0
173
+ k += 1
174
+ elif alpha < alpha_tr:
175
+ # The iterate is restricted by a bound constraint. Add this bound
176
+ # constraint to the active set, and restart the calculations.
177
+ if alpha_xl <= alpha:
178
+ i_new = np.argmin(all_alpha_xl)
179
+ step[i_new] = xl[i_new]
180
+ else:
181
+ i_new = np.argmin(all_alpha_xu)
182
+ step[i_new] = xu[i_new]
183
+ free_bd[i_new] = False
184
+ sd[free_bd] = -grad[free_bd]
185
+ sd[~free_bd] = 0.0
186
+ k = 0
187
+ else:
188
+ # The current iterate is on the trust-region boundary. Add all the
189
+ # active bounds to the working set to prepare for the improvement
190
+ # of the solution, and stop the iterations.
191
+ if alpha_xl <= alpha:
192
+ i_new = _argmin(all_alpha_xl)
193
+ step[i_new] = xl[i_new]
194
+ free_bd[i_new] = False
195
+ if alpha_xu <= alpha:
196
+ i_new = _argmin(all_alpha_xu)
197
+ step[i_new] = xu[i_new]
198
+ free_bd[i_new] = False
199
+ boundary_reached = True
200
+ break
201
+
202
+ # Attempt to improve the solution on the trust-region boundary.
203
+ if kwargs.get("improve_tcg", True) and boundary_reached:
204
+ step_base = np.copy(step)
205
+ step_comparator = grad_orig @ step_base + 0.5 * step_base @ hess_prod(
206
+ step_base
207
+ )
208
+
209
+ while np.count_nonzero(free_bd) > 0:
210
+ # Check whether a substantial reduction in the objective function
211
+ # is possible, and set the search direction.
212
+ step_sq = step[free_bd] @ step[free_bd]
213
+ grad_sq = grad[free_bd] @ grad[free_bd]
214
+ grad_step = grad[free_bd] @ step[free_bd]
215
+ grad_sd = -np.sqrt(max(step_sq * grad_sq - grad_step**2.0, 0.0))
216
+ sd[free_bd] = grad_step * step[free_bd] - step_sq * grad[free_bd]
217
+ sd[~free_bd] = 0.0
218
+ if grad_sd >= -1e-8 * reduct or np.any(
219
+ grad_sd >= -TINY * np.abs(sd[free_bd])
220
+ ):
221
+ break
222
+ sd[free_bd] /= -grad_sd
223
+
224
+ # Calculate an upper bound for the tangent of half the angle theta
225
+ # of this alternative iteration. The step will be updated as:
226
+ # step = cos(theta) * step + sin(theta) * sd.
227
+ temp_xl = np.zeros(n)
228
+ temp_xu = np.zeros(n)
229
+ temp_xl[free_bd] = (
230
+ step[free_bd] ** 2.0 + sd[free_bd] ** 2.0 - xl[free_bd] ** 2.0
231
+ )
232
+ temp_xu[free_bd] = (
233
+ step[free_bd] ** 2.0 + sd[free_bd] ** 2.0 - xu[free_bd] ** 2.0
234
+ )
235
+ temp_xl[temp_xl > 0.0] = (
236
+ np.sqrt(temp_xl[temp_xl > 0.0]) - sd[temp_xl > 0.0]
237
+ )
238
+ temp_xu[temp_xu > 0.0] = (
239
+ np.sqrt(temp_xu[temp_xu > 0.0]) + sd[temp_xu > 0.0]
240
+ )
241
+ dist_xl = np.maximum(step - xl, 0.0)
242
+ dist_xu = np.maximum(xu - step, 0.0)
243
+ i_xl = temp_xl > TINY * dist_xl
244
+ i_xu = temp_xu > TINY * dist_xu
245
+ all_t_xl = np.ones(n)
246
+ all_t_xu = np.ones(n)
247
+ all_t_xl[i_xl] = np.minimum(
248
+ all_t_xl[i_xl],
249
+ dist_xl[i_xl] / temp_xl[i_xl],
250
+ )
251
+ all_t_xu[i_xu] = np.minimum(
252
+ all_t_xu[i_xu],
253
+ dist_xu[i_xu] / temp_xu[i_xu],
254
+ )
255
+ t_xl = np.min(all_t_xl)
256
+ t_xu = np.min(all_t_xu)
257
+ t_bd = min(t_xl, t_xu)
258
+
259
+ # Calculate some curvature information.
260
+ hess_step = hess_prod(step)
261
+ hess_sd = hess_prod(sd)
262
+ curv_step = step @ hess_step
263
+ curv_sd = sd @ hess_sd
264
+ curv_step_sd = step @ hess_sd
265
+
266
+ # For a range of equally spaced values of tan(0.5 * theta),
267
+ # calculate the reduction in the objective function that would be
268
+ # obtained by accepting the corresponding angle.
269
+ n_samples = 20
270
+ n_samples = int((n_samples - 3) * t_bd + 3)
271
+ t_samples = np.linspace(t_bd / n_samples, t_bd, n_samples)
272
+ sin_values = 2.0 * t_samples / (1.0 + t_samples**2.0)
273
+ all_reduct = sin_values * (
274
+ grad_step * t_samples
275
+ - grad_sd
276
+ - t_samples * curv_step
277
+ + sin_values
278
+ * (t_samples * curv_step_sd - 0.5 * (curv_sd - curv_step))
279
+ )
280
+ if np.all(all_reduct <= 0.0):
281
+ # No reduction in the objective function is obtained.
282
+ break
283
+
284
+ # Accept the angle that provides the largest reduction in the
285
+ # objective function, and update the iterate.
286
+ i_max = np.argmax(all_reduct)
287
+ cos_value = (1.0 - t_samples[i_max] ** 2.0) / (
288
+ 1.0 + t_samples[i_max] ** 2.0
289
+ )
290
+ step[free_bd] = (
291
+ cos_value * step[free_bd] + sin_values[i_max] * sd[free_bd]
292
+ )
293
+ grad += (cos_value - 1.0) * hess_step + sin_values[i_max] * hess_sd
294
+ reduct += all_reduct[i_max]
295
+
296
+ # If the above angle is restricted by bound constraints, add them
297
+ # to the working set, and restart the alternative iteration.
298
+ # Otherwise, the calculations are terminated.
299
+ if t_bd < 1.0 and i_max == n_samples - 1:
300
+ if t_xl <= t_bd:
301
+ i_new = _argmin(all_t_xl)
302
+ step[i_new] = xl[i_new]
303
+ free_bd[i_new] = False
304
+ if t_xu <= t_bd:
305
+ i_new = _argmin(all_t_xu)
306
+ step[i_new] = xu[i_new]
307
+ free_bd[i_new] = False
308
+ else:
309
+ break
310
+
311
+ # Ensure that the alternative iteration improves the objective
312
+ # function.
313
+ if grad_orig @ step + 0.5 * step @ hess_prod(step) > step_comparator:
314
+ step = step_base
315
+
316
+ if debug:
317
+ assert np.all(xl <= step)
318
+ assert np.all(step <= xu)
319
+ assert np.linalg.norm(step) < 1.1 * delta
320
+ return step
321
+
322
+
323
+ def constrained_tangential_byrd_omojokun(
324
+ grad,
325
+ hess_prod,
326
+ xl,
327
+ xu,
328
+ aub,
329
+ bub,
330
+ aeq,
331
+ delta,
332
+ debug,
333
+ **kwargs,
334
+ ):
335
+ r"""
336
+ Minimize approximately a quadratic function subject to bound and linear
337
+ constraints in a trust region.
338
+
339
+ This function solves approximately
340
+
341
+ .. math::
342
+
343
+ \min_{s \in \mathbb{R}^n} \quad g^{\mathsf{T}} s + \frac{1}{2}
344
+ s^{\mathsf{T}} H s \quad \text{s.t.} \quad
345
+ \left\{ \begin{array}{l}
346
+ l \le s \le u,\\
347
+ A_{\scriptscriptstyle I} s \le b_{\scriptscriptstyle I},\\
348
+ A_{\scriptscriptstyle E} s = 0,\\
349
+ \lVert s \rVert \le \Delta,
350
+ \end{array} \right.
351
+
352
+ using an active-set variation of the truncated conjugate gradient method.
353
+
354
+ Parameters
355
+ ----------
356
+ grad : `numpy.ndarray`, shape (n,)
357
+ Gradient :math:`g` as shown above.
358
+ hess_prod : callable
359
+ Product of the Hessian matrix :math:`H` with any vector.
360
+
361
+ ``hess_prod(s) -> `numpy.ndarray`, shape (n,)``
362
+
363
+ returns the product :math:`H s`.
364
+ xl : `numpy.ndarray`, shape (n,)
365
+ Lower bounds :math:`l` as shown above.
366
+ xu : `numpy.ndarray`, shape (n,)
367
+ Upper bounds :math:`u` as shown above.
368
+ aub : `numpy.ndarray`, shape (m_linear_ub, n)
369
+ Coefficient matrix :math:`A_{\scriptscriptstyle I}` as shown above.
370
+ bub : `numpy.ndarray`, shape (m_linear_ub,)
371
+ Right-hand side :math:`b_{\scriptscriptstyle I}` as shown above.
372
+ aeq : `numpy.ndarray`, shape (m_linear_eq, n)
373
+ Coefficient matrix :math:`A_{\scriptscriptstyle E}` as shown above.
374
+ delta : float
375
+ Trust-region radius :math:`\Delta` as shown above.
376
+ debug : bool
377
+ Whether to make debugging tests during the execution.
378
+
379
+ Returns
380
+ -------
381
+ `numpy.ndarray`, shape (n,)
382
+ Approximate solution :math:`s`.
383
+
384
+ Other Parameters
385
+ ----------------
386
+ improve_tcg : bool, optional
387
+ If True, a solution generated by the truncated conjugate gradient
388
+ method that is on the boundary of the trust region is improved by
389
+ moving around the trust-region boundary on the two-dimensional space
390
+ spanned by the solution and the gradient of the quadratic function at
391
+ the solution (default is True).
392
+
393
+ Notes
394
+ -----
395
+ This function implements Algorithm 6.3 of [1]_. It is assumed that the
396
+ origin is feasible with respect to the bound and linear constraints, and
397
+ that `delta` is finite and positive.
398
+
399
+ References
400
+ ----------
401
+ .. [1] T. M. Ragonneau. *Model-Based Derivative-Free Optimization Methods
402
+ and Software*. PhD thesis, Department of Applied Mathematics, The Hong
403
+ Kong Polytechnic University, Hong Kong, China, 2022. URL:
404
+ https://theses.lib.polyu.edu.hk/handle/200/12294.
405
+ """
406
+ if debug:
407
+ assert isinstance(grad, np.ndarray) and grad.ndim == 1
408
+ assert inspect.signature(hess_prod).bind(grad)
409
+ assert isinstance(xl, np.ndarray) and xl.shape == grad.shape
410
+ assert isinstance(xu, np.ndarray) and xu.shape == grad.shape
411
+ assert (
412
+ isinstance(aub, np.ndarray)
413
+ and aub.ndim == 2
414
+ and aub.shape[1] == grad.size
415
+ )
416
+ assert (
417
+ isinstance(bub, np.ndarray)
418
+ and bub.ndim == 1
419
+ and bub.size == aub.shape[0]
420
+ )
421
+ assert (
422
+ isinstance(aeq, np.ndarray)
423
+ and aeq.ndim == 2
424
+ and aeq.shape[1] == grad.size
425
+ )
426
+ assert isinstance(delta, float)
427
+ assert isinstance(debug, bool)
428
+ tol = get_arrays_tol(xl, xu)
429
+ assert np.all(xl <= tol)
430
+ assert np.all(xu >= -tol)
431
+ assert np.all(bub >= -tol)
432
+ assert np.isfinite(delta) and delta > 0.0
433
+ xl = np.minimum(xl, 0.0)
434
+ xu = np.maximum(xu, 0.0)
435
+ bub = np.maximum(bub, 0.0)
436
+
437
+ # Copy the arrays that may be modified by the code below.
438
+ n = grad.size
439
+ grad = np.copy(grad)
440
+ grad_orig = np.copy(grad)
441
+
442
+ # Calculate the initial active set.
443
+ free_xl = (xl < 0.0) | (grad < 0.0)
444
+ free_xu = (xu > 0.0) | (grad > 0.0)
445
+ free_ub = (bub > 0.0) | (aub @ grad > 0.0)
446
+ n_act, q = qr_tangential_byrd_omojokun(aub, aeq, free_xl, free_xu, free_ub)
447
+
448
+ # Set the initial iterate and the initial search direction.
449
+ step = np.zeros_like(grad)
450
+ sd = -q[:, n_act:] @ (q[:, n_act:].T @ grad)
451
+ resid = np.copy(bub)
452
+
453
+ k = 0
454
+ reduct = 0.0
455
+ boundary_reached = False
456
+ while k < n - n_act:
457
+ # Stop the computations if sd is not a descent direction.
458
+ grad_sd = grad @ sd
459
+ if grad_sd >= -10.0 * EPS * n * max(1.0, np.linalg.norm(grad)):
460
+ break
461
+
462
+ # Set alpha_tr to the step size for the trust-region constraint.
463
+ try:
464
+ alpha_tr = _alpha_tr(step, sd, delta)
465
+ except ZeroDivisionError:
466
+ break
467
+
468
+ # Stop the computations if a step along sd is expected to give a
469
+ # relatively small reduction in the objective function.
470
+ if -alpha_tr * grad_sd <= 1e-8 * reduct:
471
+ break
472
+
473
+ # Set alpha_quad to the step size for the minimization problem.
474
+ hess_sd = hess_prod(sd)
475
+ curv_sd = sd @ hess_sd
476
+ if curv_sd > TINY * abs(grad_sd):
477
+ alpha_quad = max(-grad_sd / curv_sd, 0.0)
478
+ else:
479
+ alpha_quad = np.inf
480
+
481
+ # Stop the computations if the reduction in the objective function
482
+ # provided by an unconstrained step is small.
483
+ alpha = min(alpha_tr, alpha_quad)
484
+ if -alpha * (grad_sd + 0.5 * alpha * curv_sd) <= 1e-8 * reduct:
485
+ break
486
+
487
+ # Set alpha_bd to the step size for the bound constraints.
488
+ i_xl = free_xl & (xl > -np.inf) & (sd < -TINY * np.abs(xl - step))
489
+ i_xu = free_xu & (xu < np.inf) & (sd > TINY * np.abs(xu - step))
490
+ all_alpha_xl = np.full_like(step, np.inf)
491
+ all_alpha_xu = np.full_like(step, np.inf)
492
+ all_alpha_xl[i_xl] = np.maximum(
493
+ (xl[i_xl] - step[i_xl]) / sd[i_xl],
494
+ 0.0,
495
+ )
496
+ all_alpha_xu[i_xu] = np.maximum(
497
+ (xu[i_xu] - step[i_xu]) / sd[i_xu],
498
+ 0.0,
499
+ )
500
+ alpha_xl = np.min(all_alpha_xl)
501
+ alpha_xu = np.min(all_alpha_xu)
502
+ alpha_bd = min(alpha_xl, alpha_xu)
503
+
504
+ # Set alpha_ub to the step size for the linear constraints.
505
+ aub_sd = aub @ sd
506
+ i_ub = free_ub & (aub_sd > TINY * np.abs(resid))
507
+ all_alpha_ub = np.full_like(bub, np.inf)
508
+ all_alpha_ub[i_ub] = resid[i_ub] / aub_sd[i_ub]
509
+ alpha_ub = np.min(all_alpha_ub, initial=np.inf)
510
+
511
+ # Update the iterate.
512
+ alpha = min(alpha, alpha_bd, alpha_ub)
513
+ if alpha > 0.0:
514
+ step = np.clip(step + alpha * sd, xl, xu)
515
+ grad += alpha * hess_sd
516
+ resid = np.maximum(0.0, resid - alpha * aub_sd)
517
+ reduct -= alpha * (grad_sd + 0.5 * alpha * curv_sd)
518
+
519
+ if alpha < min(alpha_tr, alpha_bd, alpha_ub):
520
+ # The current iteration is a conjugate gradient iteration. Update
521
+ # the search direction so that it is conjugate (with respect to H)
522
+ # to all the previous search directions.
523
+ grad_proj = q[:, n_act:] @ (q[:, n_act:].T @ grad)
524
+ beta = (grad_proj @ hess_sd) / curv_sd
525
+ sd = beta * sd - grad_proj
526
+ k += 1
527
+ elif alpha < alpha_tr:
528
+ # The iterate is restricted by a bound/linear constraint. Add this
529
+ # constraint to the active set, and restart the calculations.
530
+ if alpha_xl <= alpha:
531
+ i_new = np.argmin(all_alpha_xl)
532
+ step[i_new] = xl[i_new]
533
+ free_xl[i_new] = False
534
+ elif alpha_xu <= alpha:
535
+ i_new = np.argmin(all_alpha_xu)
536
+ step[i_new] = xu[i_new]
537
+ free_xu[i_new] = False
538
+ else:
539
+ i_new = np.argmin(all_alpha_ub)
540
+ free_ub[i_new] = False
541
+ n_act, q = qr_tangential_byrd_omojokun(
542
+ aub,
543
+ aeq,
544
+ free_xl,
545
+ free_xu,
546
+ free_ub,
547
+ )
548
+ sd = -q[:, n_act:] @ (q[:, n_act:].T @ grad)
549
+ k = 0
550
+ else:
551
+ # The current iterate is on the trust-region boundary. Add all the
552
+ # active bound/linear constraints to the working set to prepare for
553
+ # the improvement of the solution, and stop the iterations.
554
+ if alpha_xl <= alpha:
555
+ i_new = _argmin(all_alpha_xl)
556
+ step[i_new] = xl[i_new]
557
+ free_xl[i_new] = False
558
+ if alpha_xu <= alpha:
559
+ i_new = _argmin(all_alpha_xu)
560
+ step[i_new] = xu[i_new]
561
+ free_xu[i_new] = False
562
+ if alpha_ub <= alpha:
563
+ i_new = _argmin(all_alpha_ub)
564
+ free_ub[i_new] = False
565
+ n_act, q = qr_tangential_byrd_omojokun(
566
+ aub,
567
+ aeq,
568
+ free_xl,
569
+ free_xu,
570
+ free_ub,
571
+ )
572
+ boundary_reached = True
573
+ break
574
+
575
+ # Attempt to improve the solution on the trust-region boundary.
576
+ if kwargs.get("improve_tcg", True) and boundary_reached and n_act < n:
577
+ step_base = np.copy(step)
578
+ while n_act < n:
579
+ # Check whether a substantial reduction in the objective function
580
+ # is possible, and set the search direction.
581
+ step_proj = q[:, n_act:] @ (q[:, n_act:].T @ step)
582
+ grad_proj = q[:, n_act:] @ (q[:, n_act:].T @ grad)
583
+ step_sq = step_proj @ step_proj
584
+ grad_sq = grad_proj @ grad_proj
585
+ grad_step = grad_proj @ step_proj
586
+ grad_sd = -np.sqrt(max(step_sq * grad_sq - grad_step**2.0, 0.0))
587
+ sd = q[:, n_act:] @ (
588
+ q[:, n_act:].T @ (grad_step * step - step_sq * grad)
589
+ )
590
+ if grad_sd >= -1e-8 * reduct or np.any(
591
+ grad_sd >= -TINY * np.abs(sd)
592
+ ):
593
+ break
594
+ sd /= -grad_sd
595
+
596
+ # Calculate an upper bound for the tangent of half the angle theta
597
+ # of this alternative iteration for the bound constraints. The step
598
+ # will be updated as:
599
+ # step += (cos(theta) - 1) * step_proj + sin(theta) * sd.
600
+ temp_xl = np.zeros(n)
601
+ temp_xu = np.zeros(n)
602
+ dist_xl = np.maximum(step - xl, 0.0)
603
+ dist_xu = np.maximum(xu - step, 0.0)
604
+ temp_xl[free_xl] = sd[free_xl] ** 2.0 - dist_xl[free_xl] * (
605
+ dist_xl[free_xl] - 2.0 * step_proj[free_xl]
606
+ )
607
+ temp_xu[free_xu] = sd[free_xu] ** 2.0 - dist_xu[free_xu] * (
608
+ dist_xu[free_xu] + 2.0 * step_proj[free_xu]
609
+ )
610
+ temp_xl[temp_xl > 0.0] = (
611
+ np.sqrt(temp_xl[temp_xl > 0.0]) - sd[temp_xl > 0.0]
612
+ )
613
+ temp_xu[temp_xu > 0.0] = (
614
+ np.sqrt(temp_xu[temp_xu > 0.0]) + sd[temp_xu > 0.0]
615
+ )
616
+ i_xl = temp_xl > TINY * dist_xl
617
+ i_xu = temp_xu > TINY * dist_xu
618
+ all_t_xl = np.ones(n)
619
+ all_t_xu = np.ones(n)
620
+ all_t_xl[i_xl] = np.minimum(
621
+ all_t_xl[i_xl],
622
+ dist_xl[i_xl] / temp_xl[i_xl],
623
+ )
624
+ all_t_xu[i_xu] = np.minimum(
625
+ all_t_xu[i_xu],
626
+ dist_xu[i_xu] / temp_xu[i_xu],
627
+ )
628
+ t_xl = np.min(all_t_xl)
629
+ t_xu = np.min(all_t_xu)
630
+ t_bd = min(t_xl, t_xu)
631
+
632
+ # Calculate an upper bound for the tangent of half the angle theta
633
+ # of this alternative iteration for the linear constraints.
634
+ temp_ub = np.zeros_like(resid)
635
+ aub_step = aub @ step_proj
636
+ aub_sd = aub @ sd
637
+ temp_ub[free_ub] = aub_sd[free_ub] ** 2.0 - resid[free_ub] * (
638
+ resid[free_ub] + 2.0 * aub_step[free_ub]
639
+ )
640
+ temp_ub[temp_ub > 0.0] = (
641
+ np.sqrt(temp_ub[temp_ub > 0.0]) + aub_sd[temp_ub > 0.0]
642
+ )
643
+ i_ub = temp_ub > TINY * resid
644
+ all_t_ub = np.ones_like(resid)
645
+ all_t_ub[i_ub] = np.minimum(
646
+ all_t_ub[i_ub],
647
+ resid[i_ub] / temp_ub[i_ub],
648
+ )
649
+ t_ub = np.min(all_t_ub, initial=1.0)
650
+ t_min = min(t_bd, t_ub)
651
+
652
+ # Calculate some curvature information.
653
+ hess_step = hess_prod(step_proj)
654
+ hess_sd = hess_prod(sd)
655
+ curv_step = step_proj @ hess_step
656
+ curv_sd = sd @ hess_sd
657
+ curv_step_sd = step_proj @ hess_sd
658
+
659
+ # For a range of equally spaced values of tan(0.5 * theta),
660
+ # calculate the reduction in the objective function that would be
661
+ # obtained by accepting the corresponding angle.
662
+ n_samples = 20
663
+ n_samples = int((n_samples - 3) * t_min + 3)
664
+ t_samples = np.linspace(t_min / n_samples, t_min, n_samples)
665
+ sin_values = 2.0 * t_samples / (1.0 + t_samples**2.0)
666
+ all_reduct = sin_values * (
667
+ grad_step * t_samples
668
+ - grad_sd
669
+ - sin_values
670
+ * (
671
+ 0.5 * t_samples**2.0 * curv_step
672
+ - 2.0 * t_samples * curv_step_sd
673
+ + 0.5 * curv_sd
674
+ )
675
+ )
676
+ if np.all(all_reduct <= 0.0):
677
+ # No reduction in the objective function is obtained.
678
+ break
679
+
680
+ # Accept the angle that provides the largest reduction in the
681
+ # objective function, and update the iterate.
682
+ i_max = np.argmax(all_reduct)
683
+ cos_value = (1.0 - t_samples[i_max] ** 2.0) / (
684
+ 1.0 + t_samples[i_max] ** 2.0
685
+ )
686
+ step = np.clip(
687
+ step + (cos_value - 1.0) * step_proj + sin_values[i_max] * sd,
688
+ xl,
689
+ xu,
690
+ )
691
+ grad += (cos_value - 1.0) * hess_step + sin_values[i_max] * hess_sd
692
+ resid = np.maximum(
693
+ 0.0,
694
+ resid
695
+ - (cos_value - 1.0) * aub_step
696
+ - sin_values[i_max] * aub_sd,
697
+ )
698
+ reduct += all_reduct[i_max]
699
+
700
+ # If the above angle is restricted by bound constraints, add them
701
+ # to the working set, and restart the alternative iteration.
702
+ # Otherwise, the calculations are terminated.
703
+ if t_min < 1.0 and i_max == n_samples - 1:
704
+ if t_xl <= t_min:
705
+ i_new = _argmin(all_t_xl)
706
+ step[i_new] = xl[i_new]
707
+ free_xl[i_new] = False
708
+ if t_xu <= t_min:
709
+ i_new = _argmin(all_t_xu)
710
+ step[i_new] = xu[i_new]
711
+ free_xl[i_new] = False
712
+ if t_ub <= t_min:
713
+ i_new = _argmin(all_t_ub)
714
+ free_ub[i_new] = False
715
+ n_act, q = qr_tangential_byrd_omojokun(
716
+ aub,
717
+ aeq,
718
+ free_xl,
719
+ free_xu,
720
+ free_ub,
721
+ )
722
+ else:
723
+ break
724
+
725
+ # Ensure that the alternative iteration improves the objective
726
+ # function.
727
+ if grad_orig @ step + 0.5 * step @ hess_prod(
728
+ step
729
+ ) > grad_orig @ step_base + 0.5 * step_base @ hess_prod(step_base):
730
+ step = step_base
731
+
732
+ if debug:
733
+ tol = get_arrays_tol(xl, xu)
734
+ assert np.all(xl <= step)
735
+ assert np.all(step <= xu)
736
+ assert np.all(aub @ step <= bub + tol)
737
+ assert np.all(np.abs(aeq @ step) <= tol)
738
+ assert np.linalg.norm(step) < 1.1 * delta
739
+ return step
740
+
741
+
742
+ def normal_byrd_omojokun(aub, bub, aeq, beq, xl, xu, delta, debug, **kwargs):
743
+ r"""
744
+ Minimize approximately a linear constraint violation subject to bound
745
+ constraints in a trust region.
746
+
747
+ This function solves approximately
748
+
749
+ .. math::
750
+
751
+ \min_{s \in \mathbb{R}^n} \quad \frac{1}{2} \big( \lVert \max \{
752
+ A_{\scriptscriptstyle I} s - b_{\scriptscriptstyle I}, 0 \} \rVert^2 +
753
+ \lVert A_{\scriptscriptstyle E} s - b_{\scriptscriptstyle E} \rVert^2
754
+ \big) \quad \text{s.t.}
755
+ \quad
756
+ \left\{ \begin{array}{l}
757
+ l \le s \le u,\\
758
+ \lVert s \rVert \le \Delta,
759
+ \end{array} \right.
760
+
761
+ using a variation of the truncated conjugate gradient method.
762
+
763
+ Parameters
764
+ ----------
765
+ aub : `numpy.ndarray`, shape (m_linear_ub, n)
766
+ Matrix :math:`A_{\scriptscriptstyle I}` as shown above.
767
+ bub : `numpy.ndarray`, shape (m_linear_ub,)
768
+ Vector :math:`b_{\scriptscriptstyle I}` as shown above.
769
+ aeq : `numpy.ndarray`, shape (m_linear_eq, n)
770
+ Matrix :math:`A_{\scriptscriptstyle E}` as shown above.
771
+ beq : `numpy.ndarray`, shape (m_linear_eq,)
772
+ Vector :math:`b_{\scriptscriptstyle E}` as shown above.
773
+ xl : `numpy.ndarray`, shape (n,)
774
+ Lower bounds :math:`l` as shown above.
775
+ xu : `numpy.ndarray`, shape (n,)
776
+ Upper bounds :math:`u` as shown above.
777
+ delta : float
778
+ Trust-region radius :math:`\Delta` as shown above.
779
+ debug : bool
780
+ Whether to make debugging tests during the execution.
781
+
782
+ Returns
783
+ -------
784
+ `numpy.ndarray`, shape (n,)
785
+ Approximate solution :math:`s`.
786
+
787
+ Other Parameters
788
+ ----------------
789
+ improve_tcg : bool, optional
790
+ If True, a solution generated by the truncated conjugate gradient
791
+ method that is on the boundary of the trust region is improved by
792
+ moving around the trust-region boundary on the two-dimensional space
793
+ spanned by the solution and the gradient of the quadratic function at
794
+ the solution (default is True).
795
+
796
+ Notes
797
+ -----
798
+ This function implements Algorithm 6.4 of [1]_. It is assumed that the
799
+ origin is feasible with respect to the bound constraints and that `delta`
800
+ is finite and positive.
801
+
802
+ References
803
+ ----------
804
+ .. [1] T. M. Ragonneau. *Model-Based Derivative-Free Optimization Methods
805
+ and Software*. PhD thesis, Department of Applied Mathematics, The Hong
806
+ Kong Polytechnic University, Hong Kong, China, 2022. URL:
807
+ https://theses.lib.polyu.edu.hk/handle/200/12294.
808
+ """
809
+ if debug:
810
+ assert isinstance(aub, np.ndarray) and aub.ndim == 2
811
+ assert (
812
+ isinstance(bub, np.ndarray)
813
+ and bub.ndim == 1
814
+ and bub.size == aub.shape[0]
815
+ )
816
+ assert (
817
+ isinstance(aeq, np.ndarray)
818
+ and aeq.ndim == 2
819
+ and aeq.shape[1] == aub.shape[1]
820
+ )
821
+ assert (
822
+ isinstance(beq, np.ndarray)
823
+ and beq.ndim == 1
824
+ and beq.size == aeq.shape[0]
825
+ )
826
+ assert isinstance(xl, np.ndarray) and xl.shape == (aub.shape[1],)
827
+ assert isinstance(xu, np.ndarray) and xu.shape == (aub.shape[1],)
828
+ assert isinstance(delta, float)
829
+ assert isinstance(debug, bool)
830
+ tol = get_arrays_tol(xl, xu)
831
+ assert np.all(xl <= tol)
832
+ assert np.all(xu >= -tol)
833
+ assert np.isfinite(delta) and delta > 0.0
834
+ xl = np.minimum(xl, 0.0)
835
+ xu = np.maximum(xu, 0.0)
836
+
837
+ # Calculate the initial active set.
838
+ m_linear_ub, n = aub.shape
839
+ grad = np.r_[aeq.T @ -beq, np.maximum(0.0, -bub)]
840
+ free_xl = (xl < 0.0) | (grad[:n] < 0.0)
841
+ free_xu = (xu > 0.0) | (grad[:n] > 0.0)
842
+ free_slack = bub < 0.0
843
+ free_ub = (bub > 0.0) | (aub @ grad[:n] - grad[n:] > 0.0)
844
+ n_act, q = qr_normal_byrd_omojokun(
845
+ aub,
846
+ free_xl,
847
+ free_xu,
848
+ free_slack,
849
+ free_ub,
850
+ )
851
+
852
+ # Calculate an upper bound on the norm of the slack variables. It is not
853
+ # used in the original algorithm, but it may prevent undesired behaviors
854
+ # engendered by computer rounding errors.
855
+ delta_slack = np.sqrt(beq @ beq + grad[n:] @ grad[n:])
856
+
857
+ # Set the initial iterate and the initial search direction.
858
+ step = np.zeros(n)
859
+ sd = -q[:, n_act:] @ (q[:, n_act:].T @ grad)
860
+ resid = bub + grad[n:]
861
+
862
+ k = 0
863
+ reduct = 0.0
864
+ boundary_reached = False
865
+ while k < n + m_linear_ub - n_act:
866
+ # Stop the computations if sd is not a descent direction.
867
+ grad_sd = grad @ sd
868
+ if grad_sd >= -10.0 * EPS * n * max(1.0, np.linalg.norm(grad)):
869
+ break
870
+
871
+ # Set alpha_tr to the step size for the trust-region constraint.
872
+ try:
873
+ alpha_tr = _alpha_tr(step, sd[:n], delta)
874
+ except ZeroDivisionError:
875
+ alpha_tr = np.inf
876
+
877
+ # Prevent undesired behaviors engendered by computer rounding errors by
878
+ # considering the trust-region constraint on the slack variables.
879
+ try:
880
+ alpha_tr = min(alpha_tr, _alpha_tr(grad[n:], sd[n:], delta_slack))
881
+ except ZeroDivisionError:
882
+ pass
883
+
884
+ # Stop the computations if a step along sd is expected to give a
885
+ # relatively small reduction in the objective function.
886
+ if -alpha_tr * grad_sd <= 1e-8 * reduct:
887
+ break
888
+
889
+ # Set alpha_quad to the step size for the minimization problem.
890
+ hess_sd = np.r_[aeq.T @ (aeq @ sd[:n]), sd[n:]]
891
+ curv_sd = sd @ hess_sd
892
+ if curv_sd > TINY * abs(grad_sd):
893
+ alpha_quad = max(-grad_sd / curv_sd, 0.0)
894
+ else:
895
+ alpha_quad = np.inf
896
+
897
+ # Stop the computations if the reduction in the objective function
898
+ # provided by an unconstrained step is small.
899
+ alpha = min(alpha_tr, alpha_quad)
900
+ if -alpha * (grad_sd + 0.5 * alpha * curv_sd) <= 1e-8 * reduct:
901
+ break
902
+
903
+ # Set alpha_bd to the step size for the bound constraints.
904
+ i_xl = free_xl & (xl > -np.inf) & (sd[:n] < -TINY * np.abs(xl - step))
905
+ i_xu = free_xu & (xu < np.inf) & (sd[:n] > TINY * np.abs(xu - step))
906
+ i_slack = free_slack & (sd[n:] < -TINY * np.abs(grad[n:]))
907
+ all_alpha_xl = np.full_like(step, np.inf)
908
+ all_alpha_xu = np.full_like(step, np.inf)
909
+ all_alpha_slack = np.full_like(bub, np.inf)
910
+ all_alpha_xl[i_xl] = np.maximum(
911
+ (xl[i_xl] - step[i_xl]) / sd[:n][i_xl],
912
+ 0.0,
913
+ )
914
+ all_alpha_xu[i_xu] = np.maximum(
915
+ (xu[i_xu] - step[i_xu]) / sd[:n][i_xu],
916
+ 0.0,
917
+ )
918
+ all_alpha_slack[i_slack] = np.maximum(
919
+ -grad[n:][i_slack] / sd[n:][i_slack],
920
+ 0.0,
921
+ )
922
+ alpha_xl = np.min(all_alpha_xl)
923
+ alpha_xu = np.min(all_alpha_xu)
924
+ alpha_slack = np.min(all_alpha_slack, initial=np.inf)
925
+ alpha_bd = min(alpha_xl, alpha_xu, alpha_slack)
926
+
927
+ # Set alpha_ub to the step size for the linear constraints.
928
+ aub_sd = aub @ sd[:n] - sd[n:]
929
+ i_ub = free_ub & (aub_sd > TINY * np.abs(resid))
930
+ all_alpha_ub = np.full_like(bub, np.inf)
931
+ all_alpha_ub[i_ub] = resid[i_ub] / aub_sd[i_ub]
932
+ alpha_ub = np.min(all_alpha_ub, initial=np.inf)
933
+
934
+ # Update the iterate.
935
+ alpha = min(alpha, alpha_bd, alpha_ub)
936
+ if alpha > 0.0:
937
+ step = np.clip(step + alpha * sd[:n], xl, xu)
938
+ grad += alpha * hess_sd
939
+ resid = np.maximum(0.0, resid - alpha * aub_sd)
940
+ reduct -= alpha * (grad_sd + 0.5 * alpha * curv_sd)
941
+
942
+ if alpha < min(alpha_tr, alpha_bd, alpha_ub):
943
+ # The current iteration is a conjugate gradient iteration. Update
944
+ # the search direction so that it is conjugate (with respect to H)
945
+ # to all the previous search directions.
946
+ grad_proj = q[:, n_act:] @ (q[:, n_act:].T @ grad)
947
+ beta = (grad_proj @ hess_sd) / curv_sd
948
+ sd = beta * sd - grad_proj
949
+ k += 1
950
+ elif alpha < alpha_tr:
951
+ # The iterate is restricted by a bound/linear constraint. Add this
952
+ # constraint to the active set, and restart the calculations.
953
+ if alpha_xl <= alpha:
954
+ i_new = np.argmin(all_alpha_xl)
955
+ step[i_new] = xl[i_new]
956
+ free_xl[i_new] = False
957
+ elif alpha_xu <= alpha:
958
+ i_new = np.argmin(all_alpha_xu)
959
+ step[i_new] = xu[i_new]
960
+ free_xu[i_new] = False
961
+ elif alpha_slack <= alpha:
962
+ i_new = np.argmin(all_alpha_slack)
963
+ free_slack[i_new] = False
964
+ else:
965
+ i_new = np.argmin(all_alpha_ub)
966
+ free_ub[i_new] = False
967
+ n_act, q = qr_normal_byrd_omojokun(
968
+ aub, free_xl, free_xu, free_slack, free_ub
969
+ )
970
+ sd = -q[:, n_act:] @ (q[:, n_act:].T @ grad)
971
+ k = 0
972
+ else:
973
+ # The current iterate is on the trust-region boundary. Add all the
974
+ # active bound constraints to the working set to prepare for the
975
+ # improvement of the solution, and stop the iterations.
976
+ if alpha_xl <= alpha:
977
+ i_new = _argmin(all_alpha_xl)
978
+ step[i_new] = xl[i_new]
979
+ free_xl[i_new] = False
980
+ if alpha_xu <= alpha:
981
+ i_new = _argmin(all_alpha_xu)
982
+ step[i_new] = xu[i_new]
983
+ free_xu[i_new] = False
984
+ boundary_reached = True
985
+ break
986
+
987
+ # Attempt to improve the solution on the trust-region boundary.
988
+ if kwargs.get("improve_tcg", True) and boundary_reached:
989
+ step_base = np.copy(step)
990
+ free_bd = free_xl & free_xu
991
+ grad = aub.T @ np.maximum(aub @ step - bub, 0.0) + aeq.T @ (
992
+ aeq @ step - beq
993
+ )
994
+ sd = np.zeros(n)
995
+ while np.count_nonzero(free_bd) > 0:
996
+ # Check whether a substantial reduction in the objective function
997
+ # is possible, and set the search direction.
998
+ step_sq = step[free_bd] @ step[free_bd]
999
+ grad_sq = grad[free_bd] @ grad[free_bd]
1000
+ grad_step = grad[free_bd] @ step[free_bd]
1001
+ grad_sd = -np.sqrt(max(step_sq * grad_sq - grad_step**2.0, 0.0))
1002
+ sd[free_bd] = grad_step * step[free_bd] - step_sq * grad[free_bd]
1003
+ sd[~free_bd] = 0.0
1004
+ if grad_sd >= -1e-8 * reduct or np.any(
1005
+ grad_sd >= -TINY * np.abs(sd[free_bd])
1006
+ ):
1007
+ break
1008
+ sd[free_bd] /= -grad_sd
1009
+
1010
+ # Calculate an upper bound for the tangent of half the angle theta
1011
+ # of this alternative iteration. The step will be updated as:
1012
+ # step = cos(theta) * step + sin(theta) * sd.
1013
+ temp_xl = np.zeros(n)
1014
+ temp_xu = np.zeros(n)
1015
+ temp_xl[free_bd] = (
1016
+ step[free_bd] ** 2.0 + sd[free_bd] ** 2.0 - xl[free_bd] ** 2.0
1017
+ )
1018
+ temp_xu[free_bd] = (
1019
+ step[free_bd] ** 2.0 + sd[free_bd] ** 2.0 - xu[free_bd] ** 2.0
1020
+ )
1021
+ temp_xl[temp_xl > 0.0] = (
1022
+ np.sqrt(temp_xl[temp_xl > 0.0]) - sd[temp_xl > 0.0]
1023
+ )
1024
+ temp_xu[temp_xu > 0.0] = (
1025
+ np.sqrt(temp_xu[temp_xu > 0.0]) + sd[temp_xu > 0.0]
1026
+ )
1027
+ dist_xl = np.maximum(step - xl, 0.0)
1028
+ dist_xu = np.maximum(xu - step, 0.0)
1029
+ i_xl = temp_xl > TINY * dist_xl
1030
+ i_xu = temp_xu > TINY * dist_xu
1031
+ all_t_xl = np.ones(n)
1032
+ all_t_xu = np.ones(n)
1033
+ all_t_xl[i_xl] = np.minimum(
1034
+ all_t_xl[i_xl],
1035
+ dist_xl[i_xl] / temp_xl[i_xl],
1036
+ )
1037
+ all_t_xu[i_xu] = np.minimum(
1038
+ all_t_xu[i_xu],
1039
+ dist_xu[i_xu] / temp_xu[i_xu],
1040
+ )
1041
+ t_xl = np.min(all_t_xl)
1042
+ t_xu = np.min(all_t_xu)
1043
+ t_bd = min(t_xl, t_xu)
1044
+
1045
+ # For a range of equally spaced values of tan(0.5 * theta),
1046
+ # calculate the reduction in the objective function that would be
1047
+ # obtained by accepting the corresponding angle.
1048
+ n_samples = 20
1049
+ n_samples = int((n_samples - 3) * t_bd + 3)
1050
+ t_samples = np.linspace(t_bd / n_samples, t_bd, n_samples)
1051
+ resid_ub = np.maximum(aub @ step - bub, 0.0)
1052
+ resid_eq = aeq @ step - beq
1053
+ step_proj = np.copy(step)
1054
+ step_proj[~free_bd] = 0.0
1055
+ all_reduct = np.empty(n_samples)
1056
+ for i in range(n_samples):
1057
+ sin_value = 2.0 * t_samples[i] / (1.0 + t_samples[i] ** 2.0)
1058
+ step_alt = np.clip(
1059
+ step + sin_value * (sd - t_samples[i] * step_proj),
1060
+ xl,
1061
+ xu,
1062
+ )
1063
+ resid_ub_alt = np.maximum(aub @ step_alt - bub, 0.0)
1064
+ resid_eq_alt = aeq @ step_alt - beq
1065
+ all_reduct[i] = 0.5 * (
1066
+ resid_ub @ resid_ub
1067
+ + resid_eq @ resid_eq
1068
+ - resid_ub_alt @ resid_ub_alt
1069
+ - resid_eq_alt @ resid_eq_alt
1070
+ )
1071
+ if np.all(all_reduct <= 0.0):
1072
+ # No reduction in the objective function is obtained.
1073
+ break
1074
+
1075
+ # Accept the angle that provides the largest reduction in the
1076
+ # objective function, and update the iterate.
1077
+ i_max = np.argmax(all_reduct)
1078
+ cos_value = (1.0 - t_samples[i_max] ** 2.0) / (
1079
+ 1.0 + t_samples[i_max] ** 2.0
1080
+ )
1081
+ sin_value = (2.0 * t_samples[i_max]
1082
+ / (1.0 + t_samples[i_max] ** 2.0))
1083
+ step[free_bd] = cos_value * step[free_bd] + sin_value * sd[free_bd]
1084
+ grad = aub.T @ np.maximum(aub @ step - bub, 0.0) + aeq.T @ (
1085
+ aeq @ step - beq
1086
+ )
1087
+ reduct += all_reduct[i_max]
1088
+
1089
+ # If the above angle is restricted by bound constraints, add them
1090
+ # to the working set, and restart the alternative iteration.
1091
+ # Otherwise, the calculations are terminated.
1092
+ if t_bd < 1.0 and i_max == n_samples - 1:
1093
+ if t_xl <= t_bd:
1094
+ i_new = _argmin(all_t_xl)
1095
+ step[i_new] = xl[i_new]
1096
+ free_bd[i_new] = False
1097
+ if t_xu <= t_bd:
1098
+ i_new = _argmin(all_t_xu)
1099
+ step[i_new] = xu[i_new]
1100
+ free_bd[i_new] = False
1101
+ else:
1102
+ break
1103
+
1104
+ # Ensure that the alternative iteration improves the objective
1105
+ # function.
1106
+ resid_ub = np.maximum(aub @ step - bub, 0.0)
1107
+ resid_ub_base = np.maximum(aub @ step_base - bub, 0.0)
1108
+ resid_eq = aeq @ step - beq
1109
+ resid_eq_base = aeq @ step_base - beq
1110
+ if (
1111
+ resid_ub @ resid_ub + resid_eq @ resid_eq
1112
+ > resid_ub_base @ resid_ub_base + resid_eq_base @ resid_eq_base
1113
+ ):
1114
+ step = step_base
1115
+
1116
+ if debug:
1117
+ assert np.all(xl <= step)
1118
+ assert np.all(step <= xu)
1119
+ assert np.linalg.norm(step) < 1.1 * delta
1120
+ return step
1121
+
1122
+
1123
+ def qr_tangential_byrd_omojokun(aub, aeq, free_xl, free_xu, free_ub):
1124
+ n = free_xl.size
1125
+ identity = np.eye(n)
1126
+ q, r, _ = qr(
1127
+ np.block(
1128
+ [
1129
+ [aeq],
1130
+ [aub[~free_ub, :]],
1131
+ [-identity[~free_xl, :]],
1132
+ [identity[~free_xu, :]],
1133
+ ]
1134
+ ).T,
1135
+ pivoting=True,
1136
+ )
1137
+ n_act = np.count_nonzero(
1138
+ np.abs(np.diag(r))
1139
+ >= 10.0
1140
+ * EPS
1141
+ * n
1142
+ * np.linalg.norm(r[: np.min(r.shape), : np.min(r.shape)], axis=0)
1143
+ )
1144
+ return n_act, q
1145
+
1146
+
1147
+ def qr_normal_byrd_omojokun(aub, free_xl, free_xu, free_slack, free_ub):
1148
+ m_linear_ub, n = aub.shape
1149
+ identity_n = np.eye(n)
1150
+ identity_m = np.eye(m_linear_ub)
1151
+ q, r, _ = qr(
1152
+ np.block(
1153
+ [
1154
+ [
1155
+ aub[~free_ub, :],
1156
+ -identity_m[~free_ub, :],
1157
+ ],
1158
+ [
1159
+ np.zeros((m_linear_ub - np.count_nonzero(free_slack), n)),
1160
+ -identity_m[~free_slack, :],
1161
+ ],
1162
+ [
1163
+ -identity_n[~free_xl, :],
1164
+ np.zeros((n - np.count_nonzero(free_xl), m_linear_ub)),
1165
+ ],
1166
+ [
1167
+ identity_n[~free_xu, :],
1168
+ np.zeros((n - np.count_nonzero(free_xu), m_linear_ub)),
1169
+ ],
1170
+ ]
1171
+ ).T,
1172
+ pivoting=True,
1173
+ )
1174
+ n_act = np.count_nonzero(
1175
+ np.abs(np.diag(r))
1176
+ >= 10.0
1177
+ * EPS
1178
+ * (n + m_linear_ub)
1179
+ * np.linalg.norm(r[: np.min(r.shape), : np.min(r.shape)], axis=0)
1180
+ )
1181
+ return n_act, q
1182
+
1183
+
1184
+ def _alpha_tr(step, sd, delta):
1185
+ step_sd = step @ sd
1186
+ sd_sq = sd @ sd
1187
+ dist_tr_sq = delta**2.0 - step @ step
1188
+ temp = np.sqrt(max(step_sd**2.0 + sd_sq * dist_tr_sq, 0.0))
1189
+ if step_sd <= 0.0 and sd_sq > TINY * abs(temp - step_sd):
1190
+ alpha_tr = max((temp - step_sd) / sd_sq, 0.0)
1191
+ elif abs(temp + step_sd) > TINY * dist_tr_sq:
1192
+ alpha_tr = max(dist_tr_sq / (temp + step_sd), 0.0)
1193
+ else:
1194
+ raise ZeroDivisionError
1195
+ return alpha_tr
1196
+
1197
+
1198
+ def _argmax(x):
1199
+ return np.flatnonzero(x >= np.max(x))
1200
+
1201
+
1202
+ def _argmin(x):
1203
+ return np.flatnonzero(x <= np.min(x))
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/utils/__init__.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from .exceptions import (
2
+ MaxEvalError,
3
+ TargetSuccess,
4
+ CallbackSuccess,
5
+ FeasibleSuccess,
6
+ )
7
+ from .math import get_arrays_tol, exact_1d_array
8
+ from .versions import show_versions
9
+
10
+ __all__ = [
11
+ "MaxEvalError",
12
+ "TargetSuccess",
13
+ "CallbackSuccess",
14
+ "FeasibleSuccess",
15
+ "get_arrays_tol",
16
+ "exact_1d_array",
17
+ "show_versions",
18
+ ]
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/utils/exceptions.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class MaxEvalError(Exception):
2
+ """
3
+ Exception raised when the maximum number of evaluations is reached.
4
+ """
5
+
6
+
7
+ class TargetSuccess(Exception):
8
+ """
9
+ Exception raised when the target value is reached.
10
+ """
11
+
12
+
13
+ class CallbackSuccess(StopIteration):
14
+ """
15
+ Exception raised when the callback function raises a ``StopIteration``.
16
+ """
17
+
18
+
19
+ class FeasibleSuccess(Exception):
20
+ """
21
+ Exception raised when a feasible point of a feasible problem is found.
22
+ """
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/utils/math.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+
3
+
4
+ EPS = np.finfo(float).eps
5
+
6
+
7
+ def get_arrays_tol(*arrays):
8
+ """
9
+ Get a relative tolerance for a set of arrays.
10
+
11
+ Parameters
12
+ ----------
13
+ *arrays: tuple
14
+ Set of `numpy.ndarray` to get the tolerance for.
15
+
16
+ Returns
17
+ -------
18
+ float
19
+ Relative tolerance for the set of arrays.
20
+
21
+ Raises
22
+ ------
23
+ ValueError
24
+ If no array is provided.
25
+ """
26
+ if len(arrays) == 0:
27
+ raise ValueError("At least one array must be provided.")
28
+ size = max(array.size for array in arrays)
29
+ weight = max(
30
+ np.max(np.abs(array[np.isfinite(array)]), initial=1.0)
31
+ for array in arrays
32
+ )
33
+ return 10.0 * EPS * max(size, 1.0) * weight
34
+
35
+
36
+ def exact_1d_array(x, message):
37
+ """
38
+ Preprocess a 1-dimensional array.
39
+
40
+ Parameters
41
+ ----------
42
+ x : array_like
43
+ Array to be preprocessed.
44
+ message : str
45
+ Error message if `x` cannot be interpreter as a 1-dimensional array.
46
+
47
+ Returns
48
+ -------
49
+ `numpy.ndarray`
50
+ Preprocessed array.
51
+ """
52
+ x = np.atleast_1d(np.squeeze(x)).astype(float)
53
+ if x.ndim != 1:
54
+ raise ValueError(message)
55
+ return x
56
+
57
+
58
+ def exact_2d_array(x, message):
59
+ """
60
+ Preprocess a 2-dimensional array.
61
+
62
+ Parameters
63
+ ----------
64
+ x : array_like
65
+ Array to be preprocessed.
66
+ message : str
67
+ Error message if `x` cannot be interpreter as a 2-dimensional array.
68
+
69
+ Returns
70
+ -------
71
+ `numpy.ndarray`
72
+ Preprocessed array.
73
+ """
74
+ x = np.atleast_2d(x).astype(float)
75
+ if x.ndim != 2:
76
+ raise ValueError(message)
77
+ return x
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/cobyqa/utils/versions.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import platform
3
+ import sys
4
+ from importlib.metadata import PackageNotFoundError, version
5
+
6
+
7
+ def _get_sys_info():
8
+ """
9
+ Get useful system information.
10
+
11
+ Returns
12
+ -------
13
+ dict
14
+ Useful system information.
15
+ """
16
+ return {
17
+ "python": sys.version.replace(os.linesep, " "),
18
+ "executable": sys.executable,
19
+ "machine": platform.platform(),
20
+ }
21
+
22
+
23
+ def _get_deps_info():
24
+ """
25
+ Get the versions of the dependencies.
26
+
27
+ Returns
28
+ -------
29
+ dict
30
+ Versions of the dependencies.
31
+ """
32
+ deps = ["cobyqa", "numpy", "scipy", "setuptools", "pip"]
33
+ deps_info = {}
34
+ for module in deps:
35
+ try:
36
+ deps_info[module] = version(module)
37
+ except PackageNotFoundError:
38
+ deps_info[module] = None
39
+ return deps_info
40
+
41
+
42
+ def show_versions():
43
+ """
44
+ Display useful system and dependencies information.
45
+
46
+ When reporting issues, please include this information.
47
+ """
48
+ print("System settings")
49
+ print("---------------")
50
+ sys_info = _get_sys_info()
51
+ print(
52
+ "\n".join(
53
+ f"{k:>{max(map(len, sys_info.keys())) + 1}}: {v}"
54
+ for k, v in sys_info.items()
55
+ )
56
+ )
57
+
58
+ print()
59
+ print("Python dependencies")
60
+ print("-------------------")
61
+ deps_info = _get_deps_info()
62
+ print(
63
+ "\n".join(
64
+ f"{k:>{max(map(len, deps_info.keys())) + 1}}: {v}"
65
+ for k, v in deps_info.items()
66
+ )
67
+ )
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/__init__.py ADDED
File without changes
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test__gcutils.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """ Test for assert_deallocated context manager and gc utilities
2
+ """
3
+ import gc
4
+ from threading import Lock
5
+
6
+ from scipy._lib._gcutils import (set_gc_state, gc_state, assert_deallocated,
7
+ ReferenceError, IS_PYPY)
8
+
9
+ from numpy.testing import assert_equal
10
+
11
+ import pytest
12
+
13
+
14
+ @pytest.fixture
15
+ def gc_lock():
16
+ return Lock()
17
+
18
+
19
+ def test_set_gc_state(gc_lock):
20
+ with gc_lock:
21
+ gc_status = gc.isenabled()
22
+ try:
23
+ for state in (True, False):
24
+ gc.enable()
25
+ set_gc_state(state)
26
+ assert_equal(gc.isenabled(), state)
27
+ gc.disable()
28
+ set_gc_state(state)
29
+ assert_equal(gc.isenabled(), state)
30
+ finally:
31
+ if gc_status:
32
+ gc.enable()
33
+
34
+
35
+ def test_gc_state(gc_lock):
36
+ # Test gc_state context manager
37
+ with gc_lock:
38
+ gc_status = gc.isenabled()
39
+ try:
40
+ for pre_state in (True, False):
41
+ set_gc_state(pre_state)
42
+ for with_state in (True, False):
43
+ # Check the gc state is with_state in with block
44
+ with gc_state(with_state):
45
+ assert_equal(gc.isenabled(), with_state)
46
+ # And returns to previous state outside block
47
+ assert_equal(gc.isenabled(), pre_state)
48
+ # Even if the gc state is set explicitly within the block
49
+ with gc_state(with_state):
50
+ assert_equal(gc.isenabled(), with_state)
51
+ set_gc_state(not with_state)
52
+ assert_equal(gc.isenabled(), pre_state)
53
+ finally:
54
+ if gc_status:
55
+ gc.enable()
56
+
57
+
58
+ @pytest.mark.skipif(IS_PYPY, reason="Test not meaningful on PyPy")
59
+ def test_assert_deallocated(gc_lock):
60
+ # Ordinary use
61
+ class C:
62
+ def __init__(self, arg0, arg1, name='myname'):
63
+ self.name = name
64
+ with gc_lock:
65
+ for gc_current in (True, False):
66
+ with gc_state(gc_current):
67
+ # We are deleting from with-block context, so that's OK
68
+ with assert_deallocated(C, 0, 2, 'another name') as c:
69
+ assert_equal(c.name, 'another name')
70
+ del c
71
+ # Or not using the thing in with-block context, also OK
72
+ with assert_deallocated(C, 0, 2, name='third name'):
73
+ pass
74
+ assert_equal(gc.isenabled(), gc_current)
75
+
76
+
77
+ @pytest.mark.skipif(IS_PYPY, reason="Test not meaningful on PyPy")
78
+ def test_assert_deallocated_nodel():
79
+ class C:
80
+ pass
81
+ with pytest.raises(ReferenceError):
82
+ # Need to delete after using if in with-block context
83
+ # Note: assert_deallocated(C) needs to be assigned for the test
84
+ # to function correctly. It is assigned to _, but _ itself is
85
+ # not referenced in the body of the with, it is only there for
86
+ # the refcount.
87
+ with assert_deallocated(C) as _:
88
+ pass
89
+
90
+
91
+ @pytest.mark.skipif(IS_PYPY, reason="Test not meaningful on PyPy")
92
+ def test_assert_deallocated_circular():
93
+ class C:
94
+ def __init__(self):
95
+ self._circular = self
96
+ with pytest.raises(ReferenceError):
97
+ # Circular reference, no automatic garbage collection
98
+ with assert_deallocated(C) as c:
99
+ del c
100
+
101
+
102
+ @pytest.mark.skipif(IS_PYPY, reason="Test not meaningful on PyPy")
103
+ def test_assert_deallocated_circular2():
104
+ class C:
105
+ def __init__(self):
106
+ self._circular = self
107
+ with pytest.raises(ReferenceError):
108
+ # Still circular reference, no automatic garbage collection
109
+ with assert_deallocated(C):
110
+ pass
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test__pep440.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pytest import raises as assert_raises
2
+ from scipy._lib._pep440 import Version, parse
3
+
4
+
5
+ def test_main_versions():
6
+ assert Version('1.8.0') == Version('1.8.0')
7
+ for ver in ['1.9.0', '2.0.0', '1.8.1']:
8
+ assert Version('1.8.0') < Version(ver)
9
+
10
+ for ver in ['1.7.0', '1.7.1', '0.9.9']:
11
+ assert Version('1.8.0') > Version(ver)
12
+
13
+
14
+ def test_version_1_point_10():
15
+ # regression test for gh-2998.
16
+ assert Version('1.9.0') < Version('1.10.0')
17
+ assert Version('1.11.0') < Version('1.11.1')
18
+ assert Version('1.11.0') == Version('1.11.0')
19
+ assert Version('1.99.11') < Version('1.99.12')
20
+
21
+
22
+ def test_alpha_beta_rc():
23
+ assert Version('1.8.0rc1') == Version('1.8.0rc1')
24
+ for ver in ['1.8.0', '1.8.0rc2']:
25
+ assert Version('1.8.0rc1') < Version(ver)
26
+
27
+ for ver in ['1.8.0a2', '1.8.0b3', '1.7.2rc4']:
28
+ assert Version('1.8.0rc1') > Version(ver)
29
+
30
+ assert Version('1.8.0b1') > Version('1.8.0a2')
31
+
32
+
33
+ def test_dev_version():
34
+ assert Version('1.9.0.dev+Unknown') < Version('1.9.0')
35
+ for ver in ['1.9.0', '1.9.0a1', '1.9.0b2', '1.9.0b2.dev+ffffffff', '1.9.0.dev1']:
36
+ assert Version('1.9.0.dev+f16acvda') < Version(ver)
37
+
38
+ assert Version('1.9.0.dev+f16acvda') == Version('1.9.0.dev+f16acvda')
39
+
40
+
41
+ def test_dev_a_b_rc_mixed():
42
+ assert Version('1.9.0a2.dev+f16acvda') == Version('1.9.0a2.dev+f16acvda')
43
+ assert Version('1.9.0a2.dev+6acvda54') < Version('1.9.0a2')
44
+
45
+
46
+ def test_dev0_version():
47
+ assert Version('1.9.0.dev0+Unknown') < Version('1.9.0')
48
+ for ver in ['1.9.0', '1.9.0a1', '1.9.0b2', '1.9.0b2.dev0+ffffffff']:
49
+ assert Version('1.9.0.dev0+f16acvda') < Version(ver)
50
+
51
+ assert Version('1.9.0.dev0+f16acvda') == Version('1.9.0.dev0+f16acvda')
52
+
53
+
54
+ def test_dev0_a_b_rc_mixed():
55
+ assert Version('1.9.0a2.dev0+f16acvda') == Version('1.9.0a2.dev0+f16acvda')
56
+ assert Version('1.9.0a2.dev0+6acvda54') < Version('1.9.0a2')
57
+
58
+
59
+ def test_raises():
60
+ for ver in ['1,9.0', '1.7.x']:
61
+ assert_raises(ValueError, Version, ver)
62
+
63
+ def test_legacy_version():
64
+ # Non-PEP-440 version identifiers always compare less. For NumPy this only
65
+ # occurs on dev builds prior to 1.10.0 which are unsupported anyway.
66
+ assert parse('invalid') < Version('0.0.0')
67
+ assert parse('1.9.0-f16acvda') < Version('1.0.0')
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test__testutils.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ from scipy._lib._testutils import _parse_size, _get_mem_available
3
+ import pytest
4
+
5
+
6
+ def test__parse_size():
7
+ expected = {
8
+ '12': 12e6,
9
+ '12 b': 12,
10
+ '12k': 12e3,
11
+ ' 12 M ': 12e6,
12
+ ' 12 G ': 12e9,
13
+ ' 12Tb ': 12e12,
14
+ '12 Mib ': 12 * 1024.0**2,
15
+ '12Tib': 12 * 1024.0**4,
16
+ }
17
+
18
+ for inp, outp in sorted(expected.items()):
19
+ if outp is None:
20
+ with pytest.raises(ValueError):
21
+ _parse_size(inp)
22
+ else:
23
+ assert _parse_size(inp) == outp
24
+
25
+
26
+ def test__mem_available():
27
+ # May return None on non-Linux platforms
28
+ available = _get_mem_available()
29
+ if sys.platform.startswith('linux'):
30
+ assert available >= 0
31
+ else:
32
+ assert available is None or available >= 0
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test__threadsafety.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import threading
2
+ import time
3
+ import traceback
4
+
5
+ from numpy.testing import assert_
6
+ from pytest import raises as assert_raises
7
+
8
+ from scipy._lib._threadsafety import ReentrancyLock, non_reentrant, ReentrancyError
9
+
10
+
11
+ def test_parallel_threads():
12
+ # Check that ReentrancyLock serializes work in parallel threads.
13
+ #
14
+ # The test is not fully deterministic, and may succeed falsely if
15
+ # the timings go wrong.
16
+
17
+ lock = ReentrancyLock("failure")
18
+
19
+ failflag = [False]
20
+ exceptions_raised = []
21
+
22
+ def worker(k):
23
+ try:
24
+ with lock:
25
+ assert_(not failflag[0])
26
+ failflag[0] = True
27
+ time.sleep(0.1 * k)
28
+ assert_(failflag[0])
29
+ failflag[0] = False
30
+ except Exception:
31
+ exceptions_raised.append(traceback.format_exc(2))
32
+
33
+ threads = [threading.Thread(target=lambda k=k: worker(k))
34
+ for k in range(3)]
35
+ for t in threads:
36
+ t.start()
37
+ for t in threads:
38
+ t.join()
39
+
40
+ exceptions_raised = "\n".join(exceptions_raised)
41
+ assert_(not exceptions_raised, exceptions_raised)
42
+
43
+
44
+ def test_reentering():
45
+ # Check that ReentrancyLock prevents re-entering from the same thread.
46
+
47
+ @non_reentrant()
48
+ def func(x):
49
+ return func(x)
50
+
51
+ assert_raises(ReentrancyError, func, 0)
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test__util.py ADDED
@@ -0,0 +1,657 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from multiprocessing import Pool
2
+ from multiprocessing.pool import Pool as PWL
3
+ import re
4
+ import math
5
+ from fractions import Fraction
6
+
7
+ import numpy as np
8
+ from numpy.testing import assert_equal, assert_
9
+ import pytest
10
+ from pytest import raises as assert_raises
11
+ import hypothesis.extra.numpy as npst
12
+ from hypothesis import given, strategies, reproduce_failure # noqa: F401
13
+ from scipy.conftest import array_api_compatible, skip_xp_invalid_arg
14
+
15
+ from scipy._lib._array_api import (xp_assert_equal, xp_assert_close, is_numpy,
16
+ xp_copy, is_array_api_strict)
17
+ from scipy._lib._util import (_aligned_zeros, check_random_state, MapWrapper,
18
+ getfullargspec_no_self, FullArgSpec,
19
+ rng_integers, _validate_int, _rename_parameter,
20
+ _contains_nan, _rng_html_rewrite, _lazywhere)
21
+ from scipy import cluster, interpolate, linalg, optimize, sparse, spatial, stats
22
+
23
+ skip_xp_backends = pytest.mark.skip_xp_backends
24
+
25
+
26
+ @pytest.mark.slow
27
+ def test__aligned_zeros():
28
+ niter = 10
29
+
30
+ def check(shape, dtype, order, align):
31
+ err_msg = repr((shape, dtype, order, align))
32
+ x = _aligned_zeros(shape, dtype, order, align=align)
33
+ if align is None:
34
+ align = np.dtype(dtype).alignment
35
+ assert_equal(x.__array_interface__['data'][0] % align, 0)
36
+ if hasattr(shape, '__len__'):
37
+ assert_equal(x.shape, shape, err_msg)
38
+ else:
39
+ assert_equal(x.shape, (shape,), err_msg)
40
+ assert_equal(x.dtype, dtype)
41
+ if order == "C":
42
+ assert_(x.flags.c_contiguous, err_msg)
43
+ elif order == "F":
44
+ if x.size > 0:
45
+ # Size-0 arrays get invalid flags on NumPy 1.5
46
+ assert_(x.flags.f_contiguous, err_msg)
47
+ elif order is None:
48
+ assert_(x.flags.c_contiguous, err_msg)
49
+ else:
50
+ raise ValueError()
51
+
52
+ # try various alignments
53
+ for align in [1, 2, 3, 4, 8, 16, 32, 64, None]:
54
+ for n in [0, 1, 3, 11]:
55
+ for order in ["C", "F", None]:
56
+ for dtype in [np.uint8, np.float64]:
57
+ for shape in [n, (1, 2, 3, n)]:
58
+ for j in range(niter):
59
+ check(shape, dtype, order, align)
60
+
61
+
62
+ def test_check_random_state():
63
+ # If seed is None, return the RandomState singleton used by np.random.
64
+ # If seed is an int, return a new RandomState instance seeded with seed.
65
+ # If seed is already a RandomState instance, return it.
66
+ # Otherwise raise ValueError.
67
+ rsi = check_random_state(1)
68
+ assert_equal(type(rsi), np.random.RandomState)
69
+ rsi = check_random_state(rsi)
70
+ assert_equal(type(rsi), np.random.RandomState)
71
+ rsi = check_random_state(None)
72
+ assert_equal(type(rsi), np.random.RandomState)
73
+ assert_raises(ValueError, check_random_state, 'a')
74
+ rg = np.random.Generator(np.random.PCG64())
75
+ rsi = check_random_state(rg)
76
+ assert_equal(type(rsi), np.random.Generator)
77
+
78
+
79
+ def test_getfullargspec_no_self():
80
+ p = MapWrapper(1)
81
+ argspec = getfullargspec_no_self(p.__init__)
82
+ assert_equal(argspec, FullArgSpec(['pool'], None, None, (1,), [],
83
+ None, {}))
84
+ argspec = getfullargspec_no_self(p.__call__)
85
+ assert_equal(argspec, FullArgSpec(['func', 'iterable'], None, None, None,
86
+ [], None, {}))
87
+
88
+ class _rv_generic:
89
+ def _rvs(self, a, b=2, c=3, *args, size=None, **kwargs):
90
+ return None
91
+
92
+ rv_obj = _rv_generic()
93
+ argspec = getfullargspec_no_self(rv_obj._rvs)
94
+ assert_equal(argspec, FullArgSpec(['a', 'b', 'c'], 'args', 'kwargs',
95
+ (2, 3), ['size'], {'size': None}, {}))
96
+
97
+
98
+ def test_mapwrapper_serial():
99
+ in_arg = np.arange(10.)
100
+ out_arg = np.sin(in_arg)
101
+
102
+ p = MapWrapper(1)
103
+ assert_(p._mapfunc is map)
104
+ assert_(p.pool is None)
105
+ assert_(p._own_pool is False)
106
+ out = list(p(np.sin, in_arg))
107
+ assert_equal(out, out_arg)
108
+
109
+ with assert_raises(RuntimeError):
110
+ p = MapWrapper(0)
111
+
112
+
113
+ def test_pool():
114
+ with Pool(2) as p:
115
+ p.map(math.sin, [1, 2, 3, 4])
116
+
117
+
118
+ def test_mapwrapper_parallel():
119
+ in_arg = np.arange(10.)
120
+ out_arg = np.sin(in_arg)
121
+
122
+ with MapWrapper(2) as p:
123
+ out = p(np.sin, in_arg)
124
+ assert_equal(list(out), out_arg)
125
+
126
+ assert_(p._own_pool is True)
127
+ assert_(isinstance(p.pool, PWL))
128
+ assert_(p._mapfunc is not None)
129
+
130
+ # the context manager should've closed the internal pool
131
+ # check that it has by asking it to calculate again.
132
+ with assert_raises(Exception) as excinfo:
133
+ p(np.sin, in_arg)
134
+
135
+ assert_(excinfo.type is ValueError)
136
+
137
+ # can also set a PoolWrapper up with a map-like callable instance
138
+ with Pool(2) as p:
139
+ q = MapWrapper(p.map)
140
+
141
+ assert_(q._own_pool is False)
142
+ q.close()
143
+
144
+ # closing the PoolWrapper shouldn't close the internal pool
145
+ # because it didn't create it
146
+ out = p.map(np.sin, in_arg)
147
+ assert_equal(list(out), out_arg)
148
+
149
+
150
+ def test_rng_integers():
151
+ rng = np.random.RandomState()
152
+
153
+ # test that numbers are inclusive of high point
154
+ arr = rng_integers(rng, low=2, high=5, size=100, endpoint=True)
155
+ assert np.max(arr) == 5
156
+ assert np.min(arr) == 2
157
+ assert arr.shape == (100, )
158
+
159
+ # test that numbers are inclusive of high point
160
+ arr = rng_integers(rng, low=5, size=100, endpoint=True)
161
+ assert np.max(arr) == 5
162
+ assert np.min(arr) == 0
163
+ assert arr.shape == (100, )
164
+
165
+ # test that numbers are exclusive of high point
166
+ arr = rng_integers(rng, low=2, high=5, size=100, endpoint=False)
167
+ assert np.max(arr) == 4
168
+ assert np.min(arr) == 2
169
+ assert arr.shape == (100, )
170
+
171
+ # test that numbers are exclusive of high point
172
+ arr = rng_integers(rng, low=5, size=100, endpoint=False)
173
+ assert np.max(arr) == 4
174
+ assert np.min(arr) == 0
175
+ assert arr.shape == (100, )
176
+
177
+ # now try with np.random.Generator
178
+ try:
179
+ rng = np.random.default_rng()
180
+ except AttributeError:
181
+ return
182
+
183
+ # test that numbers are inclusive of high point
184
+ arr = rng_integers(rng, low=2, high=5, size=100, endpoint=True)
185
+ assert np.max(arr) == 5
186
+ assert np.min(arr) == 2
187
+ assert arr.shape == (100, )
188
+
189
+ # test that numbers are inclusive of high point
190
+ arr = rng_integers(rng, low=5, size=100, endpoint=True)
191
+ assert np.max(arr) == 5
192
+ assert np.min(arr) == 0
193
+ assert arr.shape == (100, )
194
+
195
+ # test that numbers are exclusive of high point
196
+ arr = rng_integers(rng, low=2, high=5, size=100, endpoint=False)
197
+ assert np.max(arr) == 4
198
+ assert np.min(arr) == 2
199
+ assert arr.shape == (100, )
200
+
201
+ # test that numbers are exclusive of high point
202
+ arr = rng_integers(rng, low=5, size=100, endpoint=False)
203
+ assert np.max(arr) == 4
204
+ assert np.min(arr) == 0
205
+ assert arr.shape == (100, )
206
+
207
+
208
+ class TestValidateInt:
209
+
210
+ @pytest.mark.parametrize('n', [4, np.uint8(4), np.int16(4), np.array(4)])
211
+ def test_validate_int(self, n):
212
+ n = _validate_int(n, 'n')
213
+ assert n == 4
214
+
215
+ @pytest.mark.parametrize('n', [4.0, np.array([4]), Fraction(4, 1)])
216
+ def test_validate_int_bad(self, n):
217
+ with pytest.raises(TypeError, match='n must be an integer'):
218
+ _validate_int(n, 'n')
219
+
220
+ def test_validate_int_below_min(self):
221
+ with pytest.raises(ValueError, match='n must be an integer not '
222
+ 'less than 0'):
223
+ _validate_int(-1, 'n', 0)
224
+
225
+
226
+ class TestRenameParameter:
227
+ # check that wrapper `_rename_parameter` for backward-compatible
228
+ # keyword renaming works correctly
229
+
230
+ # Example method/function that still accepts keyword `old`
231
+ @_rename_parameter("old", "new")
232
+ def old_keyword_still_accepted(self, new):
233
+ return new
234
+
235
+ # Example method/function for which keyword `old` is deprecated
236
+ @_rename_parameter("old", "new", dep_version="1.9.0")
237
+ def old_keyword_deprecated(self, new):
238
+ return new
239
+
240
+ def test_old_keyword_still_accepted(self):
241
+ # positional argument and both keyword work identically
242
+ res1 = self.old_keyword_still_accepted(10)
243
+ res2 = self.old_keyword_still_accepted(new=10)
244
+ res3 = self.old_keyword_still_accepted(old=10)
245
+ assert res1 == res2 == res3 == 10
246
+
247
+ # unexpected keyword raises an error
248
+ message = re.escape("old_keyword_still_accepted() got an unexpected")
249
+ with pytest.raises(TypeError, match=message):
250
+ self.old_keyword_still_accepted(unexpected=10)
251
+
252
+ # multiple values for the same parameter raises an error
253
+ message = re.escape("old_keyword_still_accepted() got multiple")
254
+ with pytest.raises(TypeError, match=message):
255
+ self.old_keyword_still_accepted(10, new=10)
256
+ with pytest.raises(TypeError, match=message):
257
+ self.old_keyword_still_accepted(10, old=10)
258
+ with pytest.raises(TypeError, match=message):
259
+ self.old_keyword_still_accepted(new=10, old=10)
260
+
261
+ @pytest.fixture
262
+ def kwarg_lock(self):
263
+ from threading import Lock
264
+ return Lock()
265
+
266
+ def test_old_keyword_deprecated(self, kwarg_lock):
267
+ # positional argument and both keyword work identically,
268
+ # but use of old keyword results in DeprecationWarning
269
+ dep_msg = "Use of keyword argument `old` is deprecated"
270
+ res1 = self.old_keyword_deprecated(10)
271
+ res2 = self.old_keyword_deprecated(new=10)
272
+ # pytest warning filter is not thread-safe, enforce serialization
273
+ with kwarg_lock:
274
+ with pytest.warns(DeprecationWarning, match=dep_msg):
275
+ res3 = self.old_keyword_deprecated(old=10)
276
+ assert res1 == res2 == res3 == 10
277
+
278
+ # unexpected keyword raises an error
279
+ message = re.escape("old_keyword_deprecated() got an unexpected")
280
+ with pytest.raises(TypeError, match=message):
281
+ self.old_keyword_deprecated(unexpected=10)
282
+
283
+ # multiple values for the same parameter raises an error and,
284
+ # if old keyword is used, results in DeprecationWarning
285
+ message = re.escape("old_keyword_deprecated() got multiple")
286
+ with pytest.raises(TypeError, match=message):
287
+ self.old_keyword_deprecated(10, new=10)
288
+ with kwarg_lock:
289
+ with pytest.raises(TypeError, match=message), \
290
+ pytest.warns(DeprecationWarning, match=dep_msg):
291
+ # breakpoint()
292
+ self.old_keyword_deprecated(10, old=10)
293
+ with kwarg_lock:
294
+ with pytest.raises(TypeError, match=message), \
295
+ pytest.warns(DeprecationWarning, match=dep_msg):
296
+ self.old_keyword_deprecated(new=10, old=10)
297
+
298
+
299
+ class TestContainsNaNTest:
300
+
301
+ def test_policy(self):
302
+ data = np.array([1, 2, 3, np.nan])
303
+
304
+ contains_nan, nan_policy = _contains_nan(data, nan_policy="propagate")
305
+ assert contains_nan
306
+ assert nan_policy == "propagate"
307
+
308
+ contains_nan, nan_policy = _contains_nan(data, nan_policy="omit")
309
+ assert contains_nan
310
+ assert nan_policy == "omit"
311
+
312
+ msg = "The input contains nan values"
313
+ with pytest.raises(ValueError, match=msg):
314
+ _contains_nan(data, nan_policy="raise")
315
+
316
+ msg = "nan_policy must be one of"
317
+ with pytest.raises(ValueError, match=msg):
318
+ _contains_nan(data, nan_policy="nan")
319
+
320
+ def test_contains_nan(self):
321
+ data1 = np.array([1, 2, 3])
322
+ assert not _contains_nan(data1)[0]
323
+
324
+ data2 = np.array([1, 2, 3, np.nan])
325
+ assert _contains_nan(data2)[0]
326
+
327
+ data3 = np.array([np.nan, 2, 3, np.nan])
328
+ assert _contains_nan(data3)[0]
329
+
330
+ data4 = np.array([[1, 2], [3, 4]])
331
+ assert not _contains_nan(data4)[0]
332
+
333
+ data5 = np.array([[1, 2], [3, np.nan]])
334
+ assert _contains_nan(data5)[0]
335
+
336
+ @skip_xp_invalid_arg
337
+ def test_contains_nan_with_strings(self):
338
+ data1 = np.array([1, 2, "3", np.nan]) # converted to string "nan"
339
+ assert not _contains_nan(data1)[0]
340
+
341
+ data2 = np.array([1, 2, "3", np.nan], dtype='object')
342
+ assert _contains_nan(data2)[0]
343
+
344
+ data3 = np.array([["1", 2], [3, np.nan]]) # converted to string "nan"
345
+ assert not _contains_nan(data3)[0]
346
+
347
+ data4 = np.array([["1", 2], [3, np.nan]], dtype='object')
348
+ assert _contains_nan(data4)[0]
349
+
350
+ @skip_xp_backends('jax.numpy',
351
+ reason="JAX arrays do not support item assignment")
352
+ @pytest.mark.usefixtures("skip_xp_backends")
353
+ @array_api_compatible
354
+ @pytest.mark.parametrize("nan_policy", ['propagate', 'omit', 'raise'])
355
+ def test_array_api(self, xp, nan_policy):
356
+ rng = np.random.default_rng(932347235892482)
357
+ x0 = rng.random(size=(2, 3, 4))
358
+ x = xp.asarray(x0)
359
+ x_nan = xp_copy(x, xp=xp)
360
+ x_nan[1, 2, 1] = np.nan
361
+
362
+ contains_nan, nan_policy_out = _contains_nan(x, nan_policy=nan_policy)
363
+ assert not contains_nan
364
+ assert nan_policy_out == nan_policy
365
+
366
+ if nan_policy == 'raise':
367
+ message = 'The input contains...'
368
+ with pytest.raises(ValueError, match=message):
369
+ _contains_nan(x_nan, nan_policy=nan_policy)
370
+ elif nan_policy == 'omit' and not is_numpy(xp):
371
+ message = "`nan_policy='omit' is incompatible..."
372
+ with pytest.raises(ValueError, match=message):
373
+ _contains_nan(x_nan, nan_policy=nan_policy)
374
+ elif nan_policy == 'propagate':
375
+ contains_nan, nan_policy_out = _contains_nan(
376
+ x_nan, nan_policy=nan_policy)
377
+ assert contains_nan
378
+ assert nan_policy_out == nan_policy
379
+
380
+
381
+ def test__rng_html_rewrite():
382
+ def mock_str():
383
+ lines = [
384
+ 'np.random.default_rng(8989843)',
385
+ 'np.random.default_rng(seed)',
386
+ 'np.random.default_rng(0x9a71b21474694f919882289dc1559ca)',
387
+ ' bob ',
388
+ ]
389
+ return lines
390
+
391
+ res = _rng_html_rewrite(mock_str)()
392
+ ref = [
393
+ 'np.random.default_rng()',
394
+ 'np.random.default_rng(seed)',
395
+ 'np.random.default_rng()',
396
+ ' bob ',
397
+ ]
398
+
399
+ assert res == ref
400
+
401
+
402
+ class TestTransitionToRNG:
403
+ def kmeans(self, **kwargs):
404
+ rng = np.random.default_rng(3458934594269824562)
405
+ return cluster.vq.kmeans2(rng.random(size=(20, 3)), 3, **kwargs)
406
+
407
+ def kmeans2(self, **kwargs):
408
+ rng = np.random.default_rng(3458934594269824562)
409
+ return cluster.vq.kmeans2(rng.random(size=(20, 3)), 3, **kwargs)
410
+
411
+ def barycentric(self, **kwargs):
412
+ rng = np.random.default_rng(3458934594269824562)
413
+ x1, x2, y1 = rng.random((3, 10))
414
+ f = interpolate.BarycentricInterpolator(x1, y1, **kwargs)
415
+ return f(x2)
416
+
417
+ def clarkson_woodruff_transform(self, **kwargs):
418
+ rng = np.random.default_rng(3458934594269824562)
419
+ return linalg.clarkson_woodruff_transform(rng.random((10, 10)), 3, **kwargs)
420
+
421
+ def basinhopping(self, **kwargs):
422
+ rng = np.random.default_rng(3458934594269824562)
423
+ return optimize.basinhopping(optimize.rosen, rng.random(3), **kwargs).x
424
+
425
+ def opt(self, fun, **kwargs):
426
+ rng = np.random.default_rng(3458934594269824562)
427
+ bounds = optimize.Bounds(-rng.random(3) * 10, rng.random(3) * 10)
428
+ return fun(optimize.rosen, bounds, **kwargs).x
429
+
430
+ def differential_evolution(self, **kwargs):
431
+ return self.opt(optimize.differential_evolution, **kwargs)
432
+
433
+ def dual_annealing(self, **kwargs):
434
+ return self.opt(optimize.dual_annealing, **kwargs)
435
+
436
+ def check_grad(self, **kwargs):
437
+ rng = np.random.default_rng(3458934594269824562)
438
+ x = rng.random(3)
439
+ return optimize.check_grad(optimize.rosen, optimize.rosen_der, x,
440
+ direction='random', **kwargs)
441
+
442
+ def random_array(self, **kwargs):
443
+ return sparse.random_array((10, 10), density=1.0, **kwargs).toarray()
444
+
445
+ def random(self, **kwargs):
446
+ return sparse.random(10, 10, density=1.0, **kwargs).toarray()
447
+
448
+ def rand(self, **kwargs):
449
+ return sparse.rand(10, 10, density=1.0, **kwargs).toarray()
450
+
451
+ def svds(self, **kwargs):
452
+ rng = np.random.default_rng(3458934594269824562)
453
+ A = rng.random((10, 10))
454
+ return sparse.linalg.svds(A, **kwargs)
455
+
456
+ def random_rotation(self, **kwargs):
457
+ return spatial.transform.Rotation.random(3, **kwargs).as_matrix()
458
+
459
+ def goodness_of_fit(self, **kwargs):
460
+ rng = np.random.default_rng(3458934594269824562)
461
+ data = rng.random(100)
462
+ return stats.goodness_of_fit(stats.laplace, data, **kwargs).pvalue
463
+
464
+ def permutation_test(self, **kwargs):
465
+ rng = np.random.default_rng(3458934594269824562)
466
+ data = tuple(rng.random((2, 100)))
467
+ def statistic(x, y, axis): return np.mean(x, axis=axis) - np.mean(y, axis=axis)
468
+ return stats.permutation_test(data, statistic, **kwargs).pvalue
469
+
470
+ def bootstrap(self, **kwargs):
471
+ rng = np.random.default_rng(3458934594269824562)
472
+ data = (rng.random(100),)
473
+ return stats.bootstrap(data, np.mean, **kwargs).confidence_interval
474
+
475
+ def dunnett(self, **kwargs):
476
+ rng = np.random.default_rng(3458934594269824562)
477
+ x, y, control = rng.random((3, 100))
478
+ return stats.dunnett(x, y, control=control, **kwargs).pvalue
479
+
480
+ def sobol_indices(self, **kwargs):
481
+ def f_ishigami(x): return (np.sin(x[0]) + 7 * np.sin(x[1]) ** 2
482
+ + 0.1 * (x[2] ** 4) * np.sin(x[0]))
483
+ dists = [stats.uniform(loc=-np.pi, scale=2 * np.pi),
484
+ stats.uniform(loc=-np.pi, scale=2 * np.pi),
485
+ stats.uniform(loc=-np.pi, scale=2 * np.pi)]
486
+ res = stats.sobol_indices(func=f_ishigami, n=1024, dists=dists, **kwargs)
487
+ return res.first_order
488
+
489
+ def qmc_engine(self, engine, **kwargs):
490
+ qrng = engine(d=1, **kwargs)
491
+ return qrng.random(4)
492
+
493
+ def halton(self, **kwargs):
494
+ return self.qmc_engine(stats.qmc.Halton, **kwargs)
495
+
496
+ def sobol(self, **kwargs):
497
+ return self.qmc_engine(stats.qmc.Sobol, **kwargs)
498
+
499
+ def latin_hypercube(self, **kwargs):
500
+ return self.qmc_engine(stats.qmc.LatinHypercube, **kwargs)
501
+
502
+ def poisson_disk(self, **kwargs):
503
+ return self.qmc_engine(stats.qmc.PoissonDisk, **kwargs)
504
+
505
+ def multivariate_normal_qmc(self, **kwargs):
506
+ X = stats.qmc.MultivariateNormalQMC([0], **kwargs)
507
+ return X.random(4)
508
+
509
+ def multinomial_qmc(self, **kwargs):
510
+ X = stats.qmc.MultinomialQMC([0.5, 0.5], 4, **kwargs)
511
+ return X.random(4)
512
+
513
+ def permutation_method(self, **kwargs):
514
+ rng = np.random.default_rng(3458934594269824562)
515
+ data = tuple(rng.random((2, 100)))
516
+ method = stats.PermutationMethod(**kwargs)
517
+ return stats.pearsonr(*data, method=method).pvalue
518
+
519
+ def bootstrap_method(self, **kwargs):
520
+ rng = np.random.default_rng(3458934594269824562)
521
+ data = tuple(rng.random((2, 100)))
522
+ res = stats.pearsonr(*data)
523
+ method = stats.BootstrapMethod(**kwargs)
524
+ return res.confidence_interval(method=method)
525
+
526
+ @pytest.mark.fail_slow(10)
527
+ @pytest.mark.slow
528
+ @pytest.mark.parametrize("method, arg_name", [
529
+ (kmeans, "seed"),
530
+ (kmeans2, "seed"),
531
+ (barycentric, "random_state"),
532
+ (clarkson_woodruff_transform, "seed"),
533
+ (basinhopping, "seed"),
534
+ (differential_evolution, "seed"),
535
+ (dual_annealing, "seed"),
536
+ (check_grad, "seed"),
537
+ (random_array, 'random_state'),
538
+ (random, 'random_state'),
539
+ (rand, 'random_state'),
540
+ (svds, "random_state"),
541
+ (random_rotation, "random_state"),
542
+ (goodness_of_fit, "random_state"),
543
+ (permutation_test, "random_state"),
544
+ (bootstrap, "random_state"),
545
+ (permutation_method, "random_state"),
546
+ (bootstrap_method, "random_state"),
547
+ (dunnett, "random_state"),
548
+ (sobol_indices, "random_state"),
549
+ (halton, "seed"),
550
+ (sobol, "seed"),
551
+ (latin_hypercube, "seed"),
552
+ (poisson_disk, "seed"),
553
+ (multivariate_normal_qmc, "seed"),
554
+ (multinomial_qmc, "seed"),
555
+ ])
556
+ def test_rng_deterministic(self, method, arg_name):
557
+ np.random.seed(None)
558
+ seed = 2949672964
559
+
560
+ rng = np.random.default_rng(seed)
561
+ message = "got multiple values for argument now known as `rng`"
562
+ with pytest.raises(TypeError, match=message):
563
+ method(self, **{'rng': rng, arg_name: seed})
564
+
565
+ rng = np.random.default_rng(seed)
566
+ res1 = method(self, rng=rng)
567
+ res2 = method(self, rng=seed)
568
+ assert_equal(res2, res1)
569
+
570
+ if method.__name__ in {"dunnett", "sobol_indices"}:
571
+ # the two kwargs have essentially the same behavior for these functions
572
+ res3 = method(self, **{arg_name: seed})
573
+ assert_equal(res3, res1)
574
+ return
575
+
576
+ rng = np.random.RandomState(seed)
577
+ res1 = method(self, **{arg_name: rng})
578
+ res2 = method(self, **{arg_name: seed})
579
+
580
+ if method.__name__ in {"halton", "sobol", "latin_hypercube", "poisson_disk",
581
+ "multivariate_normal_qmc", "multinomial_qmc"}:
582
+ # For these, passing `random_state=RandomState(seed)` is not the same as
583
+ # passing integer `seed`.
584
+ res1b = method(self, **{arg_name: np.random.RandomState(seed)})
585
+ assert_equal(res1b, res1)
586
+ res2b = method(self, **{arg_name: seed})
587
+ assert_equal(res2b, res2)
588
+ return
589
+
590
+ np.random.seed(seed)
591
+ res3 = method(self, **{arg_name: None})
592
+ assert_equal(res2, res1)
593
+ assert_equal(res3, res1)
594
+
595
+
596
+ class TestLazywhere:
597
+ n_arrays = strategies.integers(min_value=1, max_value=3)
598
+ rng_seed = strategies.integers(min_value=1000000000, max_value=9999999999)
599
+ dtype = strategies.sampled_from((np.float32, np.float64))
600
+ p = strategies.floats(min_value=0, max_value=1)
601
+ data = strategies.data()
602
+
603
+ @pytest.mark.fail_slow(10)
604
+ @pytest.mark.filterwarnings('ignore::RuntimeWarning') # overflows, etc.
605
+ @skip_xp_backends('jax.numpy',
606
+ reason="JAX arrays do not support item assignment")
607
+ @pytest.mark.usefixtures("skip_xp_backends")
608
+ @array_api_compatible
609
+ @given(n_arrays=n_arrays, rng_seed=rng_seed, dtype=dtype, p=p, data=data)
610
+ @pytest.mark.thread_unsafe
611
+ def test_basic(self, n_arrays, rng_seed, dtype, p, data, xp):
612
+ mbs = npst.mutually_broadcastable_shapes(num_shapes=n_arrays+1,
613
+ min_side=0)
614
+ input_shapes, result_shape = data.draw(mbs)
615
+ cond_shape, *shapes = input_shapes
616
+ elements = {'allow_subnormal': False} # cupy/cupy#8382
617
+ fillvalue = xp.asarray(data.draw(npst.arrays(dtype=dtype, shape=tuple(),
618
+ elements=elements)))
619
+ float_fillvalue = float(fillvalue)
620
+ arrays = [xp.asarray(data.draw(npst.arrays(dtype=dtype, shape=shape)))
621
+ for shape in shapes]
622
+
623
+ def f(*args):
624
+ return sum(arg for arg in args)
625
+
626
+ def f2(*args):
627
+ return sum(arg for arg in args) / 2
628
+
629
+ rng = np.random.default_rng(rng_seed)
630
+ cond = xp.asarray(rng.random(size=cond_shape) > p)
631
+
632
+ res1 = _lazywhere(cond, arrays, f, fillvalue)
633
+ res2 = _lazywhere(cond, arrays, f, f2=f2)
634
+ if not is_array_api_strict(xp):
635
+ res3 = _lazywhere(cond, arrays, f, float_fillvalue)
636
+
637
+ # Ensure arrays are at least 1d to follow sane type promotion rules.
638
+ # This can be removed when minimum supported NumPy is 2.0
639
+ if xp == np:
640
+ cond, fillvalue, *arrays = np.atleast_1d(cond, fillvalue, *arrays)
641
+
642
+ ref1 = xp.where(cond, f(*arrays), fillvalue)
643
+ ref2 = xp.where(cond, f(*arrays), f2(*arrays))
644
+ if not is_array_api_strict(xp):
645
+ # Array API standard doesn't currently define behavior when fillvalue is a
646
+ # Python scalar. When it does, test can be run with array_api_strict, too.
647
+ ref3 = xp.where(cond, f(*arrays), float_fillvalue)
648
+
649
+ if xp == np: # because we ensured arrays are at least 1d
650
+ ref1 = ref1.reshape(result_shape)
651
+ ref2 = ref2.reshape(result_shape)
652
+ ref3 = ref3.reshape(result_shape)
653
+
654
+ xp_assert_close(res1, ref1, rtol=2e-16)
655
+ xp_assert_equal(res2, ref2)
656
+ if not is_array_api_strict(xp):
657
+ xp_assert_equal(res3, ref3)
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_array_api.py ADDED
@@ -0,0 +1,191 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import pytest
3
+
4
+ from scipy.conftest import array_api_compatible
5
+ from scipy._lib._array_api import (
6
+ _GLOBAL_CONFIG, array_namespace, _asarray, xp_copy, xp_assert_equal, is_numpy,
7
+ np_compat, xp_default_dtype
8
+ )
9
+ from scipy._lib._array_api_no_0d import xp_assert_equal as xp_assert_equal_no_0d
10
+
11
+ skip_xp_backends = pytest.mark.skip_xp_backends
12
+
13
+
14
+ @pytest.mark.skipif(not _GLOBAL_CONFIG["SCIPY_ARRAY_API"],
15
+ reason="Array API test; set environment variable SCIPY_ARRAY_API=1 to run it")
16
+ class TestArrayAPI:
17
+
18
+ def test_array_namespace(self):
19
+ x, y = np.array([0, 1, 2]), np.array([0, 1, 2])
20
+ xp = array_namespace(x, y)
21
+ assert 'array_api_compat.numpy' in xp.__name__
22
+
23
+ _GLOBAL_CONFIG["SCIPY_ARRAY_API"] = False
24
+ xp = array_namespace(x, y)
25
+ assert 'array_api_compat.numpy' in xp.__name__
26
+ _GLOBAL_CONFIG["SCIPY_ARRAY_API"] = True
27
+
28
+ @array_api_compatible
29
+ def test_asarray(self, xp):
30
+ x, y = _asarray([0, 1, 2], xp=xp), _asarray(np.arange(3), xp=xp)
31
+ ref = xp.asarray([0, 1, 2])
32
+ xp_assert_equal(x, ref)
33
+ xp_assert_equal(y, ref)
34
+
35
+ @pytest.mark.filterwarnings("ignore: the matrix subclass")
36
+ def test_raises(self):
37
+ msg = "of type `numpy.ma.MaskedArray` are not supported"
38
+ with pytest.raises(TypeError, match=msg):
39
+ array_namespace(np.ma.array(1), np.array(1))
40
+
41
+ msg = "of type `numpy.matrix` are not supported"
42
+ with pytest.raises(TypeError, match=msg):
43
+ array_namespace(np.array(1), np.matrix(1))
44
+
45
+ msg = "only boolean and numerical dtypes are supported"
46
+ with pytest.raises(TypeError, match=msg):
47
+ array_namespace([object()])
48
+ with pytest.raises(TypeError, match=msg):
49
+ array_namespace('abc')
50
+
51
+ def test_array_likes(self):
52
+ # should be no exceptions
53
+ array_namespace([0, 1, 2])
54
+ array_namespace(1, 2, 3)
55
+ array_namespace(1)
56
+
57
+ @skip_xp_backends('jax.numpy',
58
+ reason="JAX arrays do not support item assignment")
59
+ @pytest.mark.usefixtures("skip_xp_backends")
60
+ @array_api_compatible
61
+ def test_copy(self, xp):
62
+ for _xp in [xp, None]:
63
+ x = xp.asarray([1, 2, 3])
64
+ y = xp_copy(x, xp=_xp)
65
+ # with numpy we'd want to use np.shared_memory, but that's not specified
66
+ # in the array-api
67
+ x[0] = 10
68
+ x[1] = 11
69
+ x[2] = 12
70
+
71
+ assert x[0] != y[0]
72
+ assert x[1] != y[1]
73
+ assert x[2] != y[2]
74
+ assert id(x) != id(y)
75
+
76
+ @array_api_compatible
77
+ @pytest.mark.parametrize('dtype', ['int32', 'int64', 'float32', 'float64'])
78
+ @pytest.mark.parametrize('shape', [(), (3,)])
79
+ def test_strict_checks(self, xp, dtype, shape):
80
+ # Check that `_strict_check` behaves as expected
81
+ dtype = getattr(xp, dtype)
82
+ x = xp.broadcast_to(xp.asarray(1, dtype=dtype), shape)
83
+ x = x if shape else x[()]
84
+ y = np_compat.asarray(1)[()]
85
+
86
+ kwarg_names = ["check_namespace", "check_dtype", "check_shape", "check_0d"]
87
+ options = dict(zip(kwarg_names, [True, False, False, False]))
88
+ if xp == np:
89
+ xp_assert_equal(x, y, **options)
90
+ else:
91
+ with pytest.raises(AssertionError, match="Namespaces do not match."):
92
+ xp_assert_equal(x, y, **options)
93
+
94
+ options = dict(zip(kwarg_names, [False, True, False, False]))
95
+ if y.dtype.name in str(x.dtype):
96
+ xp_assert_equal(x, y, **options)
97
+ else:
98
+ with pytest.raises(AssertionError, match="dtypes do not match."):
99
+ xp_assert_equal(x, y, **options)
100
+
101
+ options = dict(zip(kwarg_names, [False, False, True, False]))
102
+ if x.shape == y.shape:
103
+ xp_assert_equal(x, y, **options)
104
+ else:
105
+ with pytest.raises(AssertionError, match="Shapes do not match."):
106
+ xp_assert_equal(x, xp.asarray(y), **options)
107
+
108
+ options = dict(zip(kwarg_names, [False, False, False, True]))
109
+ if is_numpy(xp) and x.shape == y.shape:
110
+ xp_assert_equal(x, y, **options)
111
+ elif is_numpy(xp):
112
+ with pytest.raises(AssertionError, match="Array-ness does not match."):
113
+ xp_assert_equal(x, y, **options)
114
+
115
+
116
+ @array_api_compatible
117
+ def test_check_scalar(self, xp):
118
+ if not is_numpy(xp):
119
+ pytest.skip("Scalars only exist in NumPy")
120
+
121
+ # identity always passes
122
+ xp_assert_equal(xp.float64(0), xp.float64(0))
123
+ xp_assert_equal(xp.asarray(0.), xp.asarray(0.))
124
+ xp_assert_equal(xp.float64(0), xp.float64(0), check_0d=False)
125
+ xp_assert_equal(xp.asarray(0.), xp.asarray(0.), check_0d=False)
126
+
127
+ # Check default convention: 0d-arrays are distinguished from scalars
128
+ message = "Array-ness does not match:.*"
129
+ with pytest.raises(AssertionError, match=message):
130
+ xp_assert_equal(xp.asarray(0.), xp.float64(0))
131
+ with pytest.raises(AssertionError, match=message):
132
+ xp_assert_equal(xp.float64(0), xp.asarray(0.))
133
+ with pytest.raises(AssertionError, match=message):
134
+ xp_assert_equal(xp.asarray(42), xp.int64(42))
135
+ with pytest.raises(AssertionError, match=message):
136
+ xp_assert_equal(xp.int64(42), xp.asarray(42))
137
+
138
+ # with `check_0d=False`, scalars-vs-0d passes (if values match)
139
+ xp_assert_equal(xp.asarray(0.), xp.float64(0), check_0d=False)
140
+ xp_assert_equal(xp.float64(0), xp.asarray(0.), check_0d=False)
141
+ # also with regular python objects
142
+ xp_assert_equal(xp.asarray(0.), 0., check_0d=False)
143
+ xp_assert_equal(0., xp.asarray(0.), check_0d=False)
144
+ xp_assert_equal(xp.asarray(42), 42, check_0d=False)
145
+ xp_assert_equal(42, xp.asarray(42), check_0d=False)
146
+
147
+ # as an alternative to `check_0d=False`, explicitly expect scalar
148
+ xp_assert_equal(xp.float64(0), xp.asarray(0.)[()])
149
+
150
+
151
+ @array_api_compatible
152
+ def test_check_scalar_no_0d(self, xp):
153
+ if not is_numpy(xp):
154
+ pytest.skip("Scalars only exist in NumPy")
155
+
156
+ # identity passes, if first argument is not 0d (or check_0d=True)
157
+ xp_assert_equal_no_0d(xp.float64(0), xp.float64(0))
158
+ xp_assert_equal_no_0d(xp.float64(0), xp.float64(0), check_0d=True)
159
+ xp_assert_equal_no_0d(xp.asarray(0.), xp.asarray(0.), check_0d=True)
160
+
161
+ # by default, 0d values are forbidden as the first argument
162
+ message = "Result is a NumPy 0d-array.*"
163
+ with pytest.raises(AssertionError, match=message):
164
+ xp_assert_equal_no_0d(xp.asarray(0.), xp.asarray(0.))
165
+ with pytest.raises(AssertionError, match=message):
166
+ xp_assert_equal_no_0d(xp.asarray(0.), xp.float64(0))
167
+ with pytest.raises(AssertionError, match=message):
168
+ xp_assert_equal_no_0d(xp.asarray(42), xp.int64(42))
169
+
170
+ # Check default convention: 0d-arrays are NOT distinguished from scalars
171
+ xp_assert_equal_no_0d(xp.float64(0), xp.asarray(0.))
172
+ xp_assert_equal_no_0d(xp.int64(42), xp.asarray(42))
173
+
174
+ # opt in to 0d-check remains possible
175
+ message = "Array-ness does not match:.*"
176
+ with pytest.raises(AssertionError, match=message):
177
+ xp_assert_equal_no_0d(xp.asarray(0.), xp.float64(0), check_0d=True)
178
+ with pytest.raises(AssertionError, match=message):
179
+ xp_assert_equal_no_0d(xp.float64(0), xp.asarray(0.), check_0d=True)
180
+ with pytest.raises(AssertionError, match=message):
181
+ xp_assert_equal_no_0d(xp.asarray(42), xp.int64(0), check_0d=True)
182
+ with pytest.raises(AssertionError, match=message):
183
+ xp_assert_equal_no_0d(xp.int64(0), xp.asarray(42), check_0d=True)
184
+
185
+ # scalars-vs-0d passes (if values match) also with regular python objects
186
+ xp_assert_equal_no_0d(0., xp.asarray(0.))
187
+ xp_assert_equal_no_0d(42, xp.asarray(42))
188
+
189
+ @array_api_compatible
190
+ def test_default_dtype(self, xp):
191
+ assert xp_default_dtype(xp) == xp.asarray(1.).dtype
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_bunch.py ADDED
@@ -0,0 +1,162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+ import pickle
3
+ from numpy.testing import assert_equal
4
+ from scipy._lib._bunch import _make_tuple_bunch
5
+
6
+
7
+ # `Result` is defined at the top level of the module so it can be
8
+ # used to test pickling.
9
+ Result = _make_tuple_bunch('Result', ['x', 'y', 'z'], ['w', 'beta'])
10
+
11
+
12
+ class TestMakeTupleBunch:
13
+
14
+ # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
15
+ # Tests with Result
16
+ # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
17
+
18
+ def setup_method(self):
19
+ # Set up an instance of Result.
20
+ self.result = Result(x=1, y=2, z=3, w=99, beta=0.5)
21
+
22
+ def test_attribute_access(self):
23
+ assert_equal(self.result.x, 1)
24
+ assert_equal(self.result.y, 2)
25
+ assert_equal(self.result.z, 3)
26
+ assert_equal(self.result.w, 99)
27
+ assert_equal(self.result.beta, 0.5)
28
+
29
+ def test_indexing(self):
30
+ assert_equal(self.result[0], 1)
31
+ assert_equal(self.result[1], 2)
32
+ assert_equal(self.result[2], 3)
33
+ assert_equal(self.result[-1], 3)
34
+ with pytest.raises(IndexError, match='index out of range'):
35
+ self.result[3]
36
+
37
+ def test_unpacking(self):
38
+ x0, y0, z0 = self.result
39
+ assert_equal((x0, y0, z0), (1, 2, 3))
40
+ assert_equal(self.result, (1, 2, 3))
41
+
42
+ def test_slice(self):
43
+ assert_equal(self.result[1:], (2, 3))
44
+ assert_equal(self.result[::2], (1, 3))
45
+ assert_equal(self.result[::-1], (3, 2, 1))
46
+
47
+ def test_len(self):
48
+ assert_equal(len(self.result), 3)
49
+
50
+ def test_repr(self):
51
+ s = repr(self.result)
52
+ assert_equal(s, 'Result(x=1, y=2, z=3, w=99, beta=0.5)')
53
+
54
+ def test_hash(self):
55
+ assert_equal(hash(self.result), hash((1, 2, 3)))
56
+
57
+ def test_pickle(self):
58
+ s = pickle.dumps(self.result)
59
+ obj = pickle.loads(s)
60
+ assert isinstance(obj, Result)
61
+ assert_equal(obj.x, self.result.x)
62
+ assert_equal(obj.y, self.result.y)
63
+ assert_equal(obj.z, self.result.z)
64
+ assert_equal(obj.w, self.result.w)
65
+ assert_equal(obj.beta, self.result.beta)
66
+
67
+ def test_read_only_existing(self):
68
+ with pytest.raises(AttributeError, match="can't set attribute"):
69
+ self.result.x = -1
70
+
71
+ def test_read_only_new(self):
72
+ self.result.plate_of_shrimp = "lattice of coincidence"
73
+ assert self.result.plate_of_shrimp == "lattice of coincidence"
74
+
75
+ def test_constructor_missing_parameter(self):
76
+ with pytest.raises(TypeError, match='missing'):
77
+ # `w` is missing.
78
+ Result(x=1, y=2, z=3, beta=0.75)
79
+
80
+ def test_constructor_incorrect_parameter(self):
81
+ with pytest.raises(TypeError, match='unexpected'):
82
+ # `foo` is not an existing field.
83
+ Result(x=1, y=2, z=3, w=123, beta=0.75, foo=999)
84
+
85
+ def test_module(self):
86
+ m = 'scipy._lib.tests.test_bunch'
87
+ assert_equal(Result.__module__, m)
88
+ assert_equal(self.result.__module__, m)
89
+
90
+ def test_extra_fields_per_instance(self):
91
+ # This test exists to ensure that instances of the same class
92
+ # store their own values for the extra fields. That is, the values
93
+ # are stored per instance and not in the class.
94
+ result1 = Result(x=1, y=2, z=3, w=-1, beta=0.0)
95
+ result2 = Result(x=4, y=5, z=6, w=99, beta=1.0)
96
+ assert_equal(result1.w, -1)
97
+ assert_equal(result1.beta, 0.0)
98
+ # The rest of these checks aren't essential, but let's check
99
+ # them anyway.
100
+ assert_equal(result1[:], (1, 2, 3))
101
+ assert_equal(result2.w, 99)
102
+ assert_equal(result2.beta, 1.0)
103
+ assert_equal(result2[:], (4, 5, 6))
104
+
105
+ # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
106
+ # Other tests
107
+ # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
108
+
109
+ def test_extra_field_names_is_optional(self):
110
+ Square = _make_tuple_bunch('Square', ['width', 'height'])
111
+ sq = Square(width=1, height=2)
112
+ assert_equal(sq.width, 1)
113
+ assert_equal(sq.height, 2)
114
+ s = repr(sq)
115
+ assert_equal(s, 'Square(width=1, height=2)')
116
+
117
+ def test_tuple_like(self):
118
+ Tup = _make_tuple_bunch('Tup', ['a', 'b'])
119
+ tu = Tup(a=1, b=2)
120
+ assert isinstance(tu, tuple)
121
+ assert isinstance(tu + (1,), tuple)
122
+
123
+ def test_explicit_module(self):
124
+ m = 'some.module.name'
125
+ Foo = _make_tuple_bunch('Foo', ['x'], ['a', 'b'], module=m)
126
+ foo = Foo(x=1, a=355, b=113)
127
+ assert_equal(Foo.__module__, m)
128
+ assert_equal(foo.__module__, m)
129
+
130
+ # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
131
+ # Argument validation
132
+ # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
133
+
134
+ @pytest.mark.parametrize('args', [('123', ['a'], ['b']),
135
+ ('Foo', ['-3'], ['x']),
136
+ ('Foo', ['a'], ['+-*/'])])
137
+ def test_identifiers_not_allowed(self, args):
138
+ with pytest.raises(ValueError, match='identifiers'):
139
+ _make_tuple_bunch(*args)
140
+
141
+ @pytest.mark.parametrize('args', [('Foo', ['a', 'b', 'a'], ['x']),
142
+ ('Foo', ['a', 'b'], ['b', 'x'])])
143
+ def test_repeated_field_names(self, args):
144
+ with pytest.raises(ValueError, match='Duplicate'):
145
+ _make_tuple_bunch(*args)
146
+
147
+ @pytest.mark.parametrize('args', [('Foo', ['_a'], ['x']),
148
+ ('Foo', ['a'], ['_x'])])
149
+ def test_leading_underscore_not_allowed(self, args):
150
+ with pytest.raises(ValueError, match='underscore'):
151
+ _make_tuple_bunch(*args)
152
+
153
+ @pytest.mark.parametrize('args', [('Foo', ['def'], ['x']),
154
+ ('Foo', ['a'], ['or']),
155
+ ('and', ['a'], ['x'])])
156
+ def test_keyword_not_allowed_in_fields(self, args):
157
+ with pytest.raises(ValueError, match='keyword'):
158
+ _make_tuple_bunch(*args)
159
+
160
+ def test_at_least_one_field_name_required(self):
161
+ with pytest.raises(ValueError, match='at least one name'):
162
+ _make_tuple_bunch('Qwerty', [], ['a', 'b'])
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_ccallback.py ADDED
@@ -0,0 +1,204 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from numpy.testing import assert_equal, assert_
2
+ from pytest import raises as assert_raises
3
+
4
+ import time
5
+ import pytest
6
+ import ctypes
7
+ import threading
8
+ from scipy._lib import _ccallback_c as _test_ccallback_cython
9
+ from scipy._lib import _test_ccallback
10
+ from scipy._lib._ccallback import LowLevelCallable
11
+
12
+ try:
13
+ import cffi
14
+ HAVE_CFFI = True
15
+ except ImportError:
16
+ HAVE_CFFI = False
17
+
18
+
19
+ ERROR_VALUE = 2.0
20
+
21
+
22
+ def callback_python(a, user_data=None):
23
+ if a == ERROR_VALUE:
24
+ raise ValueError("bad value")
25
+
26
+ if user_data is None:
27
+ return a + 1
28
+ else:
29
+ return a + user_data
30
+
31
+ def _get_cffi_func(base, signature):
32
+ if not HAVE_CFFI:
33
+ pytest.skip("cffi not installed")
34
+
35
+ # Get function address
36
+ voidp = ctypes.cast(base, ctypes.c_void_p)
37
+ address = voidp.value
38
+
39
+ # Create corresponding cffi handle
40
+ ffi = cffi.FFI()
41
+ func = ffi.cast(signature, address)
42
+ return func
43
+
44
+
45
+ def _get_ctypes_data():
46
+ value = ctypes.c_double(2.0)
47
+ return ctypes.cast(ctypes.pointer(value), ctypes.c_voidp)
48
+
49
+
50
+ def _get_cffi_data():
51
+ if not HAVE_CFFI:
52
+ pytest.skip("cffi not installed")
53
+ ffi = cffi.FFI()
54
+ return ffi.new('double *', 2.0)
55
+
56
+
57
+ CALLERS = {
58
+ 'simple': _test_ccallback.test_call_simple,
59
+ 'nodata': _test_ccallback.test_call_nodata,
60
+ 'nonlocal': _test_ccallback.test_call_nonlocal,
61
+ 'cython': _test_ccallback_cython.test_call_cython,
62
+ }
63
+
64
+ # These functions have signatures known to the callers
65
+ FUNCS = {
66
+ 'python': lambda: callback_python,
67
+ 'capsule': lambda: _test_ccallback.test_get_plus1_capsule(),
68
+ 'cython': lambda: LowLevelCallable.from_cython(_test_ccallback_cython,
69
+ "plus1_cython"),
70
+ 'ctypes': lambda: _test_ccallback_cython.plus1_ctypes,
71
+ 'cffi': lambda: _get_cffi_func(_test_ccallback_cython.plus1_ctypes,
72
+ 'double (*)(double, int *, void *)'),
73
+ 'capsule_b': lambda: _test_ccallback.test_get_plus1b_capsule(),
74
+ 'cython_b': lambda: LowLevelCallable.from_cython(_test_ccallback_cython,
75
+ "plus1b_cython"),
76
+ 'ctypes_b': lambda: _test_ccallback_cython.plus1b_ctypes,
77
+ 'cffi_b': lambda: _get_cffi_func(_test_ccallback_cython.plus1b_ctypes,
78
+ 'double (*)(double, double, int *, void *)'),
79
+ }
80
+
81
+ # These functions have signatures the callers don't know
82
+ BAD_FUNCS = {
83
+ 'capsule_bc': lambda: _test_ccallback.test_get_plus1bc_capsule(),
84
+ 'cython_bc': lambda: LowLevelCallable.from_cython(_test_ccallback_cython,
85
+ "plus1bc_cython"),
86
+ 'ctypes_bc': lambda: _test_ccallback_cython.plus1bc_ctypes,
87
+ 'cffi_bc': lambda: _get_cffi_func(
88
+ _test_ccallback_cython.plus1bc_ctypes,
89
+ 'double (*)(double, double, double, int *, void *)'
90
+ ),
91
+ }
92
+
93
+ USER_DATAS = {
94
+ 'ctypes': _get_ctypes_data,
95
+ 'cffi': _get_cffi_data,
96
+ 'capsule': _test_ccallback.test_get_data_capsule,
97
+ }
98
+
99
+
100
+ def test_callbacks():
101
+ def check(caller, func, user_data):
102
+ caller = CALLERS[caller]
103
+ func = FUNCS[func]()
104
+ user_data = USER_DATAS[user_data]()
105
+
106
+ if func is callback_python:
107
+ def func2(x):
108
+ return func(x, 2.0)
109
+ else:
110
+ func2 = LowLevelCallable(func, user_data)
111
+ func = LowLevelCallable(func)
112
+
113
+ # Test basic call
114
+ assert_equal(caller(func, 1.0), 2.0)
115
+
116
+ # Test 'bad' value resulting to an error
117
+ assert_raises(ValueError, caller, func, ERROR_VALUE)
118
+
119
+ # Test passing in user_data
120
+ assert_equal(caller(func2, 1.0), 3.0)
121
+
122
+ for caller in sorted(CALLERS.keys()):
123
+ for func in sorted(FUNCS.keys()):
124
+ for user_data in sorted(USER_DATAS.keys()):
125
+ check(caller, func, user_data)
126
+
127
+
128
+ def test_bad_callbacks():
129
+ def check(caller, func, user_data):
130
+ caller = CALLERS[caller]
131
+ user_data = USER_DATAS[user_data]()
132
+ func = BAD_FUNCS[func]()
133
+
134
+ if func is callback_python:
135
+ def func2(x):
136
+ return func(x, 2.0)
137
+ else:
138
+ func2 = LowLevelCallable(func, user_data)
139
+ func = LowLevelCallable(func)
140
+
141
+ # Test that basic call fails
142
+ assert_raises(ValueError, caller, LowLevelCallable(func), 1.0)
143
+
144
+ # Test that passing in user_data also fails
145
+ assert_raises(ValueError, caller, func2, 1.0)
146
+
147
+ # Test error message
148
+ llfunc = LowLevelCallable(func)
149
+ try:
150
+ caller(llfunc, 1.0)
151
+ except ValueError as err:
152
+ msg = str(err)
153
+ assert_(llfunc.signature in msg, msg)
154
+ assert_('double (double, double, int *, void *)' in msg, msg)
155
+
156
+ for caller in sorted(CALLERS.keys()):
157
+ for func in sorted(BAD_FUNCS.keys()):
158
+ for user_data in sorted(USER_DATAS.keys()):
159
+ check(caller, func, user_data)
160
+
161
+
162
+ def test_signature_override():
163
+ caller = _test_ccallback.test_call_simple
164
+ func = _test_ccallback.test_get_plus1_capsule()
165
+
166
+ llcallable = LowLevelCallable(func, signature="bad signature")
167
+ assert_equal(llcallable.signature, "bad signature")
168
+ assert_raises(ValueError, caller, llcallable, 3)
169
+
170
+ llcallable = LowLevelCallable(func, signature="double (double, int *, void *)")
171
+ assert_equal(llcallable.signature, "double (double, int *, void *)")
172
+ assert_equal(caller(llcallable, 3), 4)
173
+
174
+
175
+ def test_threadsafety():
176
+ def callback(a, caller):
177
+ if a <= 0:
178
+ return 1
179
+ else:
180
+ res = caller(lambda x: callback(x, caller), a - 1)
181
+ return 2*res
182
+
183
+ def check(caller):
184
+ caller = CALLERS[caller]
185
+
186
+ results = []
187
+
188
+ count = 10
189
+
190
+ def run():
191
+ time.sleep(0.01)
192
+ r = caller(lambda x: callback(x, caller), count)
193
+ results.append(r)
194
+
195
+ threads = [threading.Thread(target=run) for j in range(20)]
196
+ for thread in threads:
197
+ thread.start()
198
+ for thread in threads:
199
+ thread.join()
200
+
201
+ assert_equal(results, [2.0**count]*len(threads))
202
+
203
+ for caller in CALLERS.keys():
204
+ check(caller)
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_config.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Check the SciPy config is valid.
3
+ """
4
+ import scipy
5
+ import pytest
6
+ from unittest.mock import patch
7
+
8
+ pytestmark = pytest.mark.skipif(
9
+ not hasattr(scipy.__config__, "_built_with_meson"),
10
+ reason="Requires Meson builds",
11
+ )
12
+
13
+
14
+ class TestSciPyConfigs:
15
+ REQUIRED_CONFIG_KEYS = [
16
+ "Compilers",
17
+ "Machine Information",
18
+ "Python Information",
19
+ ]
20
+
21
+ @pytest.mark.thread_unsafe
22
+ @patch("scipy.__config__._check_pyyaml")
23
+ def test_pyyaml_not_found(self, mock_yaml_importer):
24
+ mock_yaml_importer.side_effect = ModuleNotFoundError()
25
+ with pytest.warns(UserWarning):
26
+ scipy.show_config()
27
+
28
+ def test_dict_mode(self):
29
+ config = scipy.show_config(mode="dicts")
30
+
31
+ assert isinstance(config, dict)
32
+ assert all([key in config for key in self.REQUIRED_CONFIG_KEYS]), (
33
+ "Required key missing,"
34
+ " see index of `False` with `REQUIRED_CONFIG_KEYS`"
35
+ )
36
+
37
+ def test_invalid_mode(self):
38
+ with pytest.raises(AttributeError):
39
+ scipy.show_config(mode="foo")
40
+
41
+ def test_warn_to_add_tests(self):
42
+ assert len(scipy.__config__.DisplayModes) == 2, (
43
+ "New mode detected,"
44
+ " please add UT if applicable and increment this count"
45
+ )
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_deprecation.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+
3
+ @pytest.mark.thread_unsafe
4
+ def test_cython_api_deprecation():
5
+ match = ("`scipy._lib._test_deprecation_def.foo_deprecated` "
6
+ "is deprecated, use `foo` instead!\n"
7
+ "Deprecated in Scipy 42.0.0")
8
+ with pytest.warns(DeprecationWarning, match=match):
9
+ from .. import _test_deprecation_call
10
+ assert _test_deprecation_call.call() == (1, 1)
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_doccer.py ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ''' Some tests for the documenting decorator and support functions '''
2
+
3
+ import sys
4
+ import pytest
5
+ from numpy.testing import assert_equal, suppress_warnings
6
+
7
+ from scipy._lib import doccer
8
+
9
+ # python -OO strips docstrings
10
+ DOCSTRINGS_STRIPPED = sys.flags.optimize > 1
11
+
12
+ docstring = \
13
+ """Docstring
14
+ %(strtest1)s
15
+ %(strtest2)s
16
+ %(strtest3)s
17
+ """
18
+ param_doc1 = \
19
+ """Another test
20
+ with some indent"""
21
+
22
+ param_doc2 = \
23
+ """Another test, one line"""
24
+
25
+ param_doc3 = \
26
+ """ Another test
27
+ with some indent"""
28
+
29
+ doc_dict = {'strtest1':param_doc1,
30
+ 'strtest2':param_doc2,
31
+ 'strtest3':param_doc3}
32
+
33
+ filled_docstring = \
34
+ """Docstring
35
+ Another test
36
+ with some indent
37
+ Another test, one line
38
+ Another test
39
+ with some indent
40
+ """
41
+
42
+
43
+ def test_unindent():
44
+ with suppress_warnings() as sup:
45
+ sup.filter(category=DeprecationWarning)
46
+ assert_equal(doccer.unindent_string(param_doc1), param_doc1)
47
+ assert_equal(doccer.unindent_string(param_doc2), param_doc2)
48
+ assert_equal(doccer.unindent_string(param_doc3), param_doc1)
49
+
50
+
51
+ def test_unindent_dict():
52
+ with suppress_warnings() as sup:
53
+ sup.filter(category=DeprecationWarning)
54
+ d2 = doccer.unindent_dict(doc_dict)
55
+ assert_equal(d2['strtest1'], doc_dict['strtest1'])
56
+ assert_equal(d2['strtest2'], doc_dict['strtest2'])
57
+ assert_equal(d2['strtest3'], doc_dict['strtest1'])
58
+
59
+
60
+ def test_docformat():
61
+ with suppress_warnings() as sup:
62
+ sup.filter(category=DeprecationWarning)
63
+ udd = doccer.unindent_dict(doc_dict)
64
+ formatted = doccer.docformat(docstring, udd)
65
+ assert_equal(formatted, filled_docstring)
66
+ single_doc = 'Single line doc %(strtest1)s'
67
+ formatted = doccer.docformat(single_doc, doc_dict)
68
+ # Note - initial indent of format string does not
69
+ # affect subsequent indent of inserted parameter
70
+ assert_equal(formatted, """Single line doc Another test
71
+ with some indent""")
72
+
73
+
74
+ @pytest.mark.skipif(DOCSTRINGS_STRIPPED, reason="docstrings stripped")
75
+ def test_decorator():
76
+ with suppress_warnings() as sup:
77
+ sup.filter(category=DeprecationWarning)
78
+ # with unindentation of parameters
79
+ decorator = doccer.filldoc(doc_dict, True)
80
+
81
+ @decorator
82
+ def func():
83
+ """ Docstring
84
+ %(strtest3)s
85
+ """
86
+
87
+ def expected():
88
+ """ Docstring
89
+ Another test
90
+ with some indent
91
+ """
92
+ assert_equal(func.__doc__, expected.__doc__)
93
+
94
+ # without unindentation of parameters
95
+
96
+ # The docstring should be unindented for Python 3.13+
97
+ # because of https://github.com/python/cpython/issues/81283
98
+ decorator = doccer.filldoc(doc_dict, False if \
99
+ sys.version_info < (3, 13) else True)
100
+
101
+ @decorator
102
+ def func():
103
+ """ Docstring
104
+ %(strtest3)s
105
+ """
106
+ def expected():
107
+ """ Docstring
108
+ Another test
109
+ with some indent
110
+ """
111
+ assert_equal(func.__doc__, expected.__doc__)
112
+
113
+
114
+ @pytest.mark.skipif(DOCSTRINGS_STRIPPED, reason="docstrings stripped")
115
+ def test_inherit_docstring_from():
116
+
117
+ with suppress_warnings() as sup:
118
+ sup.filter(category=DeprecationWarning)
119
+
120
+ class Foo:
121
+ def func(self):
122
+ '''Do something useful.'''
123
+ return
124
+
125
+ def func2(self):
126
+ '''Something else.'''
127
+
128
+ class Bar(Foo):
129
+ @doccer.inherit_docstring_from(Foo)
130
+ def func(self):
131
+ '''%(super)sABC'''
132
+ return
133
+
134
+ @doccer.inherit_docstring_from(Foo)
135
+ def func2(self):
136
+ # No docstring.
137
+ return
138
+
139
+ assert_equal(Bar.func.__doc__, Foo.func.__doc__ + 'ABC')
140
+ assert_equal(Bar.func2.__doc__, Foo.func2.__doc__)
141
+ bar = Bar()
142
+ assert_equal(bar.func.__doc__, Foo.func.__doc__ + 'ABC')
143
+ assert_equal(bar.func2.__doc__, Foo.func2.__doc__)
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_import_cycles.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+ import sys
3
+ import subprocess
4
+
5
+ from .test_public_api import PUBLIC_MODULES
6
+
7
+ # Regression tests for gh-6793.
8
+ # Check that all modules are importable in a new Python process.
9
+ # This is not necessarily true if there are import cycles present.
10
+
11
+ @pytest.mark.fail_slow(40)
12
+ @pytest.mark.slow
13
+ @pytest.mark.thread_unsafe
14
+ def test_public_modules_importable():
15
+ pids = [subprocess.Popen([sys.executable, '-c', f'import {module}'])
16
+ for module in PUBLIC_MODULES]
17
+ for i, pid in enumerate(pids):
18
+ assert pid.wait() == 0, f'Failed to import {PUBLIC_MODULES[i]}'
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_public_api.py ADDED
@@ -0,0 +1,469 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ This test script is adopted from:
3
+ https://github.com/numpy/numpy/blob/main/numpy/tests/test_public_api.py
4
+ """
5
+
6
+ import pkgutil
7
+ import types
8
+ import importlib
9
+ import warnings
10
+ from importlib import import_module
11
+
12
+ import pytest
13
+
14
+ import numpy as np
15
+ import scipy
16
+
17
+ from scipy.conftest import xp_available_backends
18
+
19
+
20
+ def test_dir_testing():
21
+ """Assert that output of dir has only one "testing/tester"
22
+ attribute without duplicate"""
23
+ assert len(dir(scipy)) == len(set(dir(scipy)))
24
+
25
+
26
+ # Historically SciPy has not used leading underscores for private submodules
27
+ # much. This has resulted in lots of things that look like public modules
28
+ # (i.e. things that can be imported as `import scipy.somesubmodule.somefile`),
29
+ # but were never intended to be public. The PUBLIC_MODULES list contains
30
+ # modules that are either public because they were meant to be, or because they
31
+ # contain public functions/objects that aren't present in any other namespace
32
+ # for whatever reason and therefore should be treated as public.
33
+ PUBLIC_MODULES = ["scipy." + s for s in [
34
+ "cluster",
35
+ "cluster.vq",
36
+ "cluster.hierarchy",
37
+ "constants",
38
+ "datasets",
39
+ "differentiate",
40
+ "fft",
41
+ "fftpack",
42
+ "integrate",
43
+ "interpolate",
44
+ "io",
45
+ "io.arff",
46
+ "io.matlab",
47
+ "io.wavfile",
48
+ "linalg",
49
+ "linalg.blas",
50
+ "linalg.cython_blas",
51
+ "linalg.lapack",
52
+ "linalg.cython_lapack",
53
+ "linalg.interpolative",
54
+ "ndimage",
55
+ "odr",
56
+ "optimize",
57
+ "optimize.elementwise",
58
+ "signal",
59
+ "signal.windows",
60
+ "sparse",
61
+ "sparse.linalg",
62
+ "sparse.csgraph",
63
+ "spatial",
64
+ "spatial.distance",
65
+ "spatial.transform",
66
+ "special",
67
+ "stats",
68
+ "stats.contingency",
69
+ "stats.distributions",
70
+ "stats.mstats",
71
+ "stats.qmc",
72
+ "stats.sampling"
73
+ ]]
74
+
75
+ # The PRIVATE_BUT_PRESENT_MODULES list contains modules that lacked underscores
76
+ # in their name and hence looked public, but weren't meant to be. All these
77
+ # namespace were deprecated in the 1.8.0 release - see "clear split between
78
+ # public and private API" in the 1.8.0 release notes.
79
+ # These private modules support will be removed in SciPy v2.0.0, as the
80
+ # deprecation messages emitted by each of these modules say.
81
+ PRIVATE_BUT_PRESENT_MODULES = [
82
+ 'scipy.constants.codata',
83
+ 'scipy.constants.constants',
84
+ 'scipy.fftpack.basic',
85
+ 'scipy.fftpack.convolve',
86
+ 'scipy.fftpack.helper',
87
+ 'scipy.fftpack.pseudo_diffs',
88
+ 'scipy.fftpack.realtransforms',
89
+ 'scipy.integrate.dop',
90
+ 'scipy.integrate.lsoda',
91
+ 'scipy.integrate.odepack',
92
+ 'scipy.integrate.quadpack',
93
+ 'scipy.integrate.vode',
94
+ 'scipy.interpolate.dfitpack',
95
+ 'scipy.interpolate.fitpack',
96
+ 'scipy.interpolate.fitpack2',
97
+ 'scipy.interpolate.interpnd',
98
+ 'scipy.interpolate.interpolate',
99
+ 'scipy.interpolate.ndgriddata',
100
+ 'scipy.interpolate.polyint',
101
+ 'scipy.interpolate.rbf',
102
+ 'scipy.io.arff.arffread',
103
+ 'scipy.io.harwell_boeing',
104
+ 'scipy.io.idl',
105
+ 'scipy.io.matlab.byteordercodes',
106
+ 'scipy.io.matlab.mio',
107
+ 'scipy.io.matlab.mio4',
108
+ 'scipy.io.matlab.mio5',
109
+ 'scipy.io.matlab.mio5_params',
110
+ 'scipy.io.matlab.mio5_utils',
111
+ 'scipy.io.matlab.mio_utils',
112
+ 'scipy.io.matlab.miobase',
113
+ 'scipy.io.matlab.streams',
114
+ 'scipy.io.mmio',
115
+ 'scipy.io.netcdf',
116
+ 'scipy.linalg.basic',
117
+ 'scipy.linalg.decomp',
118
+ 'scipy.linalg.decomp_cholesky',
119
+ 'scipy.linalg.decomp_lu',
120
+ 'scipy.linalg.decomp_qr',
121
+ 'scipy.linalg.decomp_schur',
122
+ 'scipy.linalg.decomp_svd',
123
+ 'scipy.linalg.matfuncs',
124
+ 'scipy.linalg.misc',
125
+ 'scipy.linalg.special_matrices',
126
+ 'scipy.misc',
127
+ 'scipy.misc.common',
128
+ 'scipy.misc.doccer',
129
+ 'scipy.ndimage.filters',
130
+ 'scipy.ndimage.fourier',
131
+ 'scipy.ndimage.interpolation',
132
+ 'scipy.ndimage.measurements',
133
+ 'scipy.ndimage.morphology',
134
+ 'scipy.odr.models',
135
+ 'scipy.odr.odrpack',
136
+ 'scipy.optimize.cobyla',
137
+ 'scipy.optimize.cython_optimize',
138
+ 'scipy.optimize.lbfgsb',
139
+ 'scipy.optimize.linesearch',
140
+ 'scipy.optimize.minpack',
141
+ 'scipy.optimize.minpack2',
142
+ 'scipy.optimize.moduleTNC',
143
+ 'scipy.optimize.nonlin',
144
+ 'scipy.optimize.optimize',
145
+ 'scipy.optimize.slsqp',
146
+ 'scipy.optimize.tnc',
147
+ 'scipy.optimize.zeros',
148
+ 'scipy.signal.bsplines',
149
+ 'scipy.signal.filter_design',
150
+ 'scipy.signal.fir_filter_design',
151
+ 'scipy.signal.lti_conversion',
152
+ 'scipy.signal.ltisys',
153
+ 'scipy.signal.signaltools',
154
+ 'scipy.signal.spectral',
155
+ 'scipy.signal.spline',
156
+ 'scipy.signal.waveforms',
157
+ 'scipy.signal.wavelets',
158
+ 'scipy.signal.windows.windows',
159
+ 'scipy.sparse.base',
160
+ 'scipy.sparse.bsr',
161
+ 'scipy.sparse.compressed',
162
+ 'scipy.sparse.construct',
163
+ 'scipy.sparse.coo',
164
+ 'scipy.sparse.csc',
165
+ 'scipy.sparse.csr',
166
+ 'scipy.sparse.data',
167
+ 'scipy.sparse.dia',
168
+ 'scipy.sparse.dok',
169
+ 'scipy.sparse.extract',
170
+ 'scipy.sparse.lil',
171
+ 'scipy.sparse.linalg.dsolve',
172
+ 'scipy.sparse.linalg.eigen',
173
+ 'scipy.sparse.linalg.interface',
174
+ 'scipy.sparse.linalg.isolve',
175
+ 'scipy.sparse.linalg.matfuncs',
176
+ 'scipy.sparse.sparsetools',
177
+ 'scipy.sparse.spfuncs',
178
+ 'scipy.sparse.sputils',
179
+ 'scipy.spatial.ckdtree',
180
+ 'scipy.spatial.kdtree',
181
+ 'scipy.spatial.qhull',
182
+ 'scipy.spatial.transform.rotation',
183
+ 'scipy.special.add_newdocs',
184
+ 'scipy.special.basic',
185
+ 'scipy.special.cython_special',
186
+ 'scipy.special.orthogonal',
187
+ 'scipy.special.sf_error',
188
+ 'scipy.special.specfun',
189
+ 'scipy.special.spfun_stats',
190
+ 'scipy.stats.biasedurn',
191
+ 'scipy.stats.kde',
192
+ 'scipy.stats.morestats',
193
+ 'scipy.stats.mstats_basic',
194
+ 'scipy.stats.mstats_extras',
195
+ 'scipy.stats.mvn',
196
+ 'scipy.stats.stats',
197
+ ]
198
+
199
+
200
+ def is_unexpected(name):
201
+ """Check if this needs to be considered."""
202
+ if '._' in name or '.tests' in name or '.setup' in name:
203
+ return False
204
+
205
+ if name in PUBLIC_MODULES:
206
+ return False
207
+
208
+ if name in PRIVATE_BUT_PRESENT_MODULES:
209
+ return False
210
+
211
+ return True
212
+
213
+
214
+ SKIP_LIST = [
215
+ 'scipy.conftest',
216
+ 'scipy.version',
217
+ 'scipy.special.libsf_error_state'
218
+ ]
219
+
220
+
221
+ # XXX: this test does more than it says on the tin - in using `pkgutil.walk_packages`,
222
+ # it will raise if it encounters any exceptions which are not handled by `ignore_errors`
223
+ # while attempting to import each discovered package.
224
+ # For now, `ignore_errors` only ignores what is necessary, but this could be expanded -
225
+ # for example, to all errors from private modules or git subpackages - if desired.
226
+ @pytest.mark.thread_unsafe
227
+ def test_all_modules_are_expected():
228
+ """
229
+ Test that we don't add anything that looks like a new public module by
230
+ accident. Check is based on filenames.
231
+ """
232
+
233
+ def ignore_errors(name):
234
+ # if versions of other array libraries are installed which are incompatible
235
+ # with the installed NumPy version, there can be errors on importing
236
+ # `array_api_compat`. This should only raise if SciPy is configured with
237
+ # that library as an available backend.
238
+ backends = {'cupy', 'torch', 'dask.array'}
239
+ for backend in backends:
240
+ path = f'array_api_compat.{backend}'
241
+ if path in name and backend not in xp_available_backends:
242
+ return
243
+ raise
244
+
245
+ modnames = []
246
+
247
+ with np.testing.suppress_warnings() as sup:
248
+ sup.filter(DeprecationWarning,"scipy.misc")
249
+ for _, modname, _ in pkgutil.walk_packages(path=scipy.__path__,
250
+ prefix=scipy.__name__ + '.',
251
+ onerror=ignore_errors):
252
+ if is_unexpected(modname) and modname not in SKIP_LIST:
253
+ # We have a name that is new. If that's on purpose, add it to
254
+ # PUBLIC_MODULES. We don't expect to have to add anything to
255
+ # PRIVATE_BUT_PRESENT_MODULES. Use an underscore in the name!
256
+ modnames.append(modname)
257
+
258
+ if modnames:
259
+ raise AssertionError(f'Found unexpected modules: {modnames}')
260
+
261
+
262
+ # Stuff that clearly shouldn't be in the API and is detected by the next test
263
+ # below
264
+ SKIP_LIST_2 = [
265
+ 'scipy.char',
266
+ 'scipy.rec',
267
+ 'scipy.emath',
268
+ 'scipy.math',
269
+ 'scipy.random',
270
+ 'scipy.ctypeslib',
271
+ 'scipy.ma'
272
+ ]
273
+
274
+
275
+ def test_all_modules_are_expected_2():
276
+ """
277
+ Method checking all objects. The pkgutil-based method in
278
+ `test_all_modules_are_expected` does not catch imports into a namespace,
279
+ only filenames.
280
+ """
281
+
282
+ def find_unexpected_members(mod_name):
283
+ members = []
284
+ module = importlib.import_module(mod_name)
285
+ if hasattr(module, '__all__'):
286
+ objnames = module.__all__
287
+ else:
288
+ objnames = dir(module)
289
+
290
+ for objname in objnames:
291
+ if not objname.startswith('_'):
292
+ fullobjname = mod_name + '.' + objname
293
+ if isinstance(getattr(module, objname), types.ModuleType):
294
+ if is_unexpected(fullobjname) and fullobjname not in SKIP_LIST_2:
295
+ members.append(fullobjname)
296
+
297
+ return members
298
+ with np.testing.suppress_warnings() as sup:
299
+ sup.filter(DeprecationWarning, "scipy.misc")
300
+ unexpected_members = find_unexpected_members("scipy")
301
+
302
+ for modname in PUBLIC_MODULES:
303
+ unexpected_members.extend(find_unexpected_members(modname))
304
+
305
+ if unexpected_members:
306
+ raise AssertionError("Found unexpected object(s) that look like "
307
+ f"modules: {unexpected_members}")
308
+
309
+
310
+ def test_api_importable():
311
+ """
312
+ Check that all submodules listed higher up in this file can be imported
313
+ Note that if a PRIVATE_BUT_PRESENT_MODULES entry goes missing, it may
314
+ simply need to be removed from the list (deprecation may or may not be
315
+ needed - apply common sense).
316
+ """
317
+ def check_importable(module_name):
318
+ try:
319
+ importlib.import_module(module_name)
320
+ except (ImportError, AttributeError):
321
+ return False
322
+
323
+ return True
324
+
325
+ module_names = []
326
+ for module_name in PUBLIC_MODULES:
327
+ if not check_importable(module_name):
328
+ module_names.append(module_name)
329
+
330
+ if module_names:
331
+ raise AssertionError("Modules in the public API that cannot be "
332
+ f"imported: {module_names}")
333
+
334
+ with warnings.catch_warnings(record=True):
335
+ warnings.filterwarnings('always', category=DeprecationWarning)
336
+ warnings.filterwarnings('always', category=ImportWarning)
337
+ for module_name in PRIVATE_BUT_PRESENT_MODULES:
338
+ if not check_importable(module_name):
339
+ module_names.append(module_name)
340
+
341
+ if module_names:
342
+ raise AssertionError("Modules that are not really public but looked "
343
+ "public and can not be imported: "
344
+ f"{module_names}")
345
+
346
+
347
+ @pytest.mark.thread_unsafe
348
+ @pytest.mark.parametrize(("module_name", "correct_module"),
349
+ [('scipy.constants.codata', None),
350
+ ('scipy.constants.constants', None),
351
+ ('scipy.fftpack.basic', None),
352
+ ('scipy.fftpack.helper', None),
353
+ ('scipy.fftpack.pseudo_diffs', None),
354
+ ('scipy.fftpack.realtransforms', None),
355
+ ('scipy.integrate.dop', None),
356
+ ('scipy.integrate.lsoda', None),
357
+ ('scipy.integrate.odepack', None),
358
+ ('scipy.integrate.quadpack', None),
359
+ ('scipy.integrate.vode', None),
360
+ ('scipy.interpolate.fitpack', None),
361
+ ('scipy.interpolate.fitpack2', None),
362
+ ('scipy.interpolate.interpolate', None),
363
+ ('scipy.interpolate.ndgriddata', None),
364
+ ('scipy.interpolate.polyint', None),
365
+ ('scipy.interpolate.rbf', None),
366
+ ('scipy.io.harwell_boeing', None),
367
+ ('scipy.io.idl', None),
368
+ ('scipy.io.mmio', None),
369
+ ('scipy.io.netcdf', None),
370
+ ('scipy.io.arff.arffread', 'arff'),
371
+ ('scipy.io.matlab.byteordercodes', 'matlab'),
372
+ ('scipy.io.matlab.mio_utils', 'matlab'),
373
+ ('scipy.io.matlab.mio', 'matlab'),
374
+ ('scipy.io.matlab.mio4', 'matlab'),
375
+ ('scipy.io.matlab.mio5_params', 'matlab'),
376
+ ('scipy.io.matlab.mio5_utils', 'matlab'),
377
+ ('scipy.io.matlab.mio5', 'matlab'),
378
+ ('scipy.io.matlab.miobase', 'matlab'),
379
+ ('scipy.io.matlab.streams', 'matlab'),
380
+ ('scipy.linalg.basic', None),
381
+ ('scipy.linalg.decomp', None),
382
+ ('scipy.linalg.decomp_cholesky', None),
383
+ ('scipy.linalg.decomp_lu', None),
384
+ ('scipy.linalg.decomp_qr', None),
385
+ ('scipy.linalg.decomp_schur', None),
386
+ ('scipy.linalg.decomp_svd', None),
387
+ ('scipy.linalg.matfuncs', None),
388
+ ('scipy.linalg.misc', None),
389
+ ('scipy.linalg.special_matrices', None),
390
+ ('scipy.ndimage.filters', None),
391
+ ('scipy.ndimage.fourier', None),
392
+ ('scipy.ndimage.interpolation', None),
393
+ ('scipy.ndimage.measurements', None),
394
+ ('scipy.ndimage.morphology', None),
395
+ ('scipy.odr.models', None),
396
+ ('scipy.odr.odrpack', None),
397
+ ('scipy.optimize.cobyla', None),
398
+ ('scipy.optimize.lbfgsb', None),
399
+ ('scipy.optimize.linesearch', None),
400
+ ('scipy.optimize.minpack', None),
401
+ ('scipy.optimize.minpack2', None),
402
+ ('scipy.optimize.moduleTNC', None),
403
+ ('scipy.optimize.nonlin', None),
404
+ ('scipy.optimize.optimize', None),
405
+ ('scipy.optimize.slsqp', None),
406
+ ('scipy.optimize.tnc', None),
407
+ ('scipy.optimize.zeros', None),
408
+ ('scipy.signal.bsplines', None),
409
+ ('scipy.signal.filter_design', None),
410
+ ('scipy.signal.fir_filter_design', None),
411
+ ('scipy.signal.lti_conversion', None),
412
+ ('scipy.signal.ltisys', None),
413
+ ('scipy.signal.signaltools', None),
414
+ ('scipy.signal.spectral', None),
415
+ ('scipy.signal.waveforms', None),
416
+ ('scipy.signal.wavelets', None),
417
+ ('scipy.signal.windows.windows', 'windows'),
418
+ ('scipy.sparse.lil', None),
419
+ ('scipy.sparse.linalg.dsolve', 'linalg'),
420
+ ('scipy.sparse.linalg.eigen', 'linalg'),
421
+ ('scipy.sparse.linalg.interface', 'linalg'),
422
+ ('scipy.sparse.linalg.isolve', 'linalg'),
423
+ ('scipy.sparse.linalg.matfuncs', 'linalg'),
424
+ ('scipy.sparse.sparsetools', None),
425
+ ('scipy.sparse.spfuncs', None),
426
+ ('scipy.sparse.sputils', None),
427
+ ('scipy.spatial.ckdtree', None),
428
+ ('scipy.spatial.kdtree', None),
429
+ ('scipy.spatial.qhull', None),
430
+ ('scipy.spatial.transform.rotation', 'transform'),
431
+ ('scipy.special.add_newdocs', None),
432
+ ('scipy.special.basic', None),
433
+ ('scipy.special.orthogonal', None),
434
+ ('scipy.special.sf_error', None),
435
+ ('scipy.special.specfun', None),
436
+ ('scipy.special.spfun_stats', None),
437
+ ('scipy.stats.biasedurn', None),
438
+ ('scipy.stats.kde', None),
439
+ ('scipy.stats.morestats', None),
440
+ ('scipy.stats.mstats_basic', 'mstats'),
441
+ ('scipy.stats.mstats_extras', 'mstats'),
442
+ ('scipy.stats.mvn', None),
443
+ ('scipy.stats.stats', None)])
444
+ def test_private_but_present_deprecation(module_name, correct_module):
445
+ # gh-18279, gh-17572, gh-17771 noted that deprecation warnings
446
+ # for imports from private modules
447
+ # were misleading. Check that this is resolved.
448
+ module = import_module(module_name)
449
+ if correct_module is None:
450
+ import_name = f'scipy.{module_name.split(".")[1]}'
451
+ else:
452
+ import_name = f'scipy.{module_name.split(".")[1]}.{correct_module}'
453
+
454
+ correct_import = import_module(import_name)
455
+
456
+ # Attributes that were formerly in `module_name` can still be imported from
457
+ # `module_name`, albeit with a deprecation warning.
458
+ for attr_name in module.__all__:
459
+ # ensure attribute is present where the warning is pointing
460
+ assert getattr(correct_import, attr_name, None) is not None
461
+ message = f"Please import `{attr_name}` from the `{import_name}`..."
462
+ with pytest.deprecated_call(match=message):
463
+ getattr(module, attr_name)
464
+
465
+ # Attributes that were not in `module_name` get an error notifying the user
466
+ # that the attribute is not in `module_name` and that `module_name` is deprecated.
467
+ message = f"`{module_name}` is deprecated..."
468
+ with pytest.raises(AttributeError, match=message):
469
+ getattr(module, "ekki")
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_scipy_version.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+
3
+ import scipy
4
+ import scipy.version
5
+
6
+
7
+ def test_valid_scipy_version():
8
+ # Verify that the SciPy version is a valid one (no .post suffix or other
9
+ # nonsense). See NumPy issue gh-6431 for an issue caused by an invalid
10
+ # version.
11
+ version_pattern = r"^[0-9]+\.[0-9]+\.[0-9]+(|a[0-9]|b[0-9]|rc[0-9])"
12
+ dev_suffix = r"((.dev0)|(\.dev0+\+git[0-9]{8}.[0-9a-f]{7}))"
13
+ if scipy.version.release:
14
+ res = re.match(version_pattern, scipy.__version__)
15
+ else:
16
+ res = re.match(version_pattern + dev_suffix, scipy.__version__)
17
+
18
+ assert res is not None
19
+ assert scipy.__version__
20
+
21
+
22
+ def test_version_submodule_members():
23
+ """`scipy.version` may not be quite public, but we install it.
24
+
25
+ So check that we don't silently change its contents.
26
+ """
27
+ for attr in ('version', 'full_version', 'short_version', 'git_revision', 'release'):
28
+ assert hasattr(scipy.version, attr)
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_tmpdirs.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """ Test tmpdirs module """
2
+ from os import getcwd
3
+ from os.path import realpath, abspath, dirname, isfile, join as pjoin, exists
4
+
5
+ from scipy._lib._tmpdirs import tempdir, in_tempdir, in_dir
6
+
7
+ from numpy.testing import assert_, assert_equal
8
+
9
+ import pytest
10
+
11
+
12
+ MY_PATH = abspath(__file__)
13
+ MY_DIR = dirname(MY_PATH)
14
+
15
+
16
+ @pytest.mark.thread_unsafe
17
+ def test_tempdir():
18
+ with tempdir() as tmpdir:
19
+ fname = pjoin(tmpdir, 'example_file.txt')
20
+ with open(fname, "w") as fobj:
21
+ fobj.write('a string\\n')
22
+ assert_(not exists(tmpdir))
23
+
24
+
25
+ @pytest.mark.thread_unsafe
26
+ def test_in_tempdir():
27
+ my_cwd = getcwd()
28
+ with in_tempdir() as tmpdir:
29
+ with open('test.txt', "w") as f:
30
+ f.write('some text')
31
+ assert_(isfile('test.txt'))
32
+ assert_(isfile(pjoin(tmpdir, 'test.txt')))
33
+ assert_(not exists(tmpdir))
34
+ assert_equal(getcwd(), my_cwd)
35
+
36
+
37
+ @pytest.mark.thread_unsafe
38
+ def test_given_directory():
39
+ # Test InGivenDirectory
40
+ cwd = getcwd()
41
+ with in_dir() as tmpdir:
42
+ assert_equal(tmpdir, abspath(cwd))
43
+ assert_equal(tmpdir, abspath(getcwd()))
44
+ with in_dir(MY_DIR) as tmpdir:
45
+ assert_equal(tmpdir, MY_DIR)
46
+ assert_equal(realpath(MY_DIR), realpath(abspath(getcwd())))
47
+ # We were deleting the given directory! Check not so now.
48
+ assert_(isfile(MY_PATH))
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/_lib/tests/test_warnings.py ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Tests which scan for certain occurrences in the code, they may not find
3
+ all of these occurrences but should catch almost all. This file was adapted
4
+ from NumPy.
5
+ """
6
+
7
+
8
+ import os
9
+ from pathlib import Path
10
+ import ast
11
+ import tokenize
12
+
13
+ import scipy
14
+
15
+ import pytest
16
+
17
+
18
+ class ParseCall(ast.NodeVisitor):
19
+ def __init__(self):
20
+ self.ls = []
21
+
22
+ def visit_Attribute(self, node):
23
+ ast.NodeVisitor.generic_visit(self, node)
24
+ self.ls.append(node.attr)
25
+
26
+ def visit_Name(self, node):
27
+ self.ls.append(node.id)
28
+
29
+
30
+ class FindFuncs(ast.NodeVisitor):
31
+ def __init__(self, filename):
32
+ super().__init__()
33
+ self.__filename = filename
34
+ self.bad_filters = []
35
+ self.bad_stacklevels = []
36
+
37
+ def visit_Call(self, node):
38
+ p = ParseCall()
39
+ p.visit(node.func)
40
+ ast.NodeVisitor.generic_visit(self, node)
41
+
42
+ if p.ls[-1] == 'simplefilter' or p.ls[-1] == 'filterwarnings':
43
+ # get first argument of the `args` node of the filter call
44
+ match node.args[0]:
45
+ case ast.Constant() as c:
46
+ argtext = c.value
47
+ case ast.JoinedStr() as js:
48
+ # if we get an f-string, discard the templated pieces, which
49
+ # are likely the type or specific message; we're interested
50
+ # in the action, which is less likely to use a template
51
+ argtext = "".join(
52
+ x.value for x in js.values if isinstance(x, ast.Constant)
53
+ )
54
+ case _:
55
+ raise ValueError("unknown ast node type")
56
+ # check if filter is set to ignore
57
+ if argtext == "ignore":
58
+ self.bad_filters.append(
59
+ f"{self.__filename}:{node.lineno}")
60
+
61
+ if p.ls[-1] == 'warn' and (
62
+ len(p.ls) == 1 or p.ls[-2] == 'warnings'):
63
+
64
+ if self.__filename == "_lib/tests/test_warnings.py":
65
+ # This file
66
+ return
67
+
68
+ # See if stacklevel exists:
69
+ if len(node.args) == 3:
70
+ return
71
+ args = {kw.arg for kw in node.keywords}
72
+ if "stacklevel" not in args:
73
+ self.bad_stacklevels.append(
74
+ f"{self.__filename}:{node.lineno}")
75
+
76
+
77
+ @pytest.fixture(scope="session")
78
+ def warning_calls():
79
+ # combined "ignore" and stacklevel error
80
+ base = Path(scipy.__file__).parent
81
+
82
+ bad_filters = []
83
+ bad_stacklevels = []
84
+
85
+ for path in base.rglob("*.py"):
86
+ # use tokenize to auto-detect encoding on systems where no
87
+ # default encoding is defined (e.g., LANG='C')
88
+ with tokenize.open(str(path)) as file:
89
+ tree = ast.parse(file.read(), filename=str(path))
90
+ finder = FindFuncs(path.relative_to(base))
91
+ finder.visit(tree)
92
+ bad_filters.extend(finder.bad_filters)
93
+ bad_stacklevels.extend(finder.bad_stacklevels)
94
+
95
+ return bad_filters, bad_stacklevels
96
+
97
+
98
+ @pytest.mark.fail_slow(40)
99
+ @pytest.mark.slow
100
+ def test_warning_calls_filters(warning_calls):
101
+ bad_filters, bad_stacklevels = warning_calls
102
+
103
+ # We try not to add filters in the code base, because those filters aren't
104
+ # thread-safe. We aim to only filter in tests with
105
+ # np.testing.suppress_warnings. However, in some cases it may prove
106
+ # necessary to filter out warnings, because we can't (easily) fix the root
107
+ # cause for them and we don't want users to see some warnings when they use
108
+ # SciPy correctly. So we list exceptions here. Add new entries only if
109
+ # there's a good reason.
110
+ allowed_filters = (
111
+ os.path.join('datasets', '_fetchers.py'),
112
+ os.path.join('datasets', '__init__.py'),
113
+ os.path.join('optimize', '_optimize.py'),
114
+ os.path.join('optimize', '_constraints.py'),
115
+ os.path.join('optimize', '_nnls.py'),
116
+ os.path.join('signal', '_ltisys.py'),
117
+ os.path.join('sparse', '__init__.py'), # np.matrix pending-deprecation
118
+ os.path.join('special', '_basic.py'), # gh-21801
119
+ os.path.join('stats', '_discrete_distns.py'), # gh-14901
120
+ os.path.join('stats', '_continuous_distns.py'),
121
+ os.path.join('stats', '_binned_statistic.py'), # gh-19345
122
+ os.path.join('stats', '_stats_py.py'), # gh-20743
123
+ os.path.join('stats', 'tests', 'test_axis_nan_policy.py'), # gh-20694
124
+ os.path.join('_lib', '_util.py'), # gh-19341
125
+ os.path.join('sparse', 'linalg', '_dsolve', 'linsolve.py'), # gh-17924
126
+ "conftest.py",
127
+ )
128
+ bad_filters = [item for item in bad_filters if item.split(':')[0] not in
129
+ allowed_filters]
130
+
131
+ if bad_filters:
132
+ raise AssertionError(
133
+ "warning ignore filter should not be used, instead, use\n"
134
+ "numpy.testing.suppress_warnings (in tests only);\n"
135
+ "found in:\n {}".format(
136
+ "\n ".join(bad_filters)))
137
+
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/__init__.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ =========================================
3
+ Clustering package (:mod:`scipy.cluster`)
4
+ =========================================
5
+
6
+ .. currentmodule:: scipy.cluster
7
+
8
+ Clustering algorithms are useful in information theory, target detection,
9
+ communications, compression, and other areas. The `vq` module only
10
+ supports vector quantization and the k-means algorithms.
11
+
12
+ The `hierarchy` module provides functions for hierarchical and
13
+ agglomerative clustering. Its features include generating hierarchical
14
+ clusters from distance matrices,
15
+ calculating statistics on clusters, cutting linkages
16
+ to generate flat clusters, and visualizing clusters with dendrograms.
17
+
18
+ .. toctree::
19
+ :maxdepth: 1
20
+
21
+ cluster.vq
22
+ cluster.hierarchy
23
+
24
+ """
25
+ __all__ = ['vq', 'hierarchy']
26
+
27
+ from . import vq, hierarchy
28
+
29
+ from scipy._lib._testutils import PytestTester
30
+ test = PytestTester(__name__)
31
+ del PytestTester
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/hierarchy.py ADDED
The diff for this file is too large to render. See raw diff
 
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/tests/__init__.py ADDED
File without changes
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/tests/hierarchy_test_data.py ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from numpy import array
2
+
3
+
4
+ Q_X = array([[5.26563660e-01, 3.14160190e-01, 8.00656370e-02],
5
+ [7.50205180e-01, 4.60299830e-01, 8.98696460e-01],
6
+ [6.65461230e-01, 6.94011420e-01, 9.10465700e-01],
7
+ [9.64047590e-01, 1.43082200e-03, 7.39874220e-01],
8
+ [1.08159060e-01, 5.53028790e-01, 6.63804780e-02],
9
+ [9.31359130e-01, 8.25424910e-01, 9.52315440e-01],
10
+ [6.78086960e-01, 3.41903970e-01, 5.61481950e-01],
11
+ [9.82730940e-01, 7.04605210e-01, 8.70978630e-02],
12
+ [6.14691610e-01, 4.69989230e-02, 6.02406450e-01],
13
+ [5.80161260e-01, 9.17354970e-01, 5.88163850e-01],
14
+ [1.38246310e+00, 1.96358160e+00, 1.94437880e+00],
15
+ [2.10675860e+00, 1.67148730e+00, 1.34854480e+00],
16
+ [1.39880070e+00, 1.66142050e+00, 1.32224550e+00],
17
+ [1.71410460e+00, 1.49176380e+00, 1.45432170e+00],
18
+ [1.54102340e+00, 1.84374950e+00, 1.64658950e+00],
19
+ [2.08512480e+00, 1.84524350e+00, 2.17340850e+00],
20
+ [1.30748740e+00, 1.53801650e+00, 2.16007740e+00],
21
+ [1.41447700e+00, 1.99329070e+00, 1.99107420e+00],
22
+ [1.61943490e+00, 1.47703280e+00, 1.89788160e+00],
23
+ [1.59880600e+00, 1.54988980e+00, 1.57563350e+00],
24
+ [3.37247380e+00, 2.69635310e+00, 3.39981700e+00],
25
+ [3.13705120e+00, 3.36528090e+00, 3.06089070e+00],
26
+ [3.29413250e+00, 3.19619500e+00, 2.90700170e+00],
27
+ [2.65510510e+00, 3.06785900e+00, 2.97198540e+00],
28
+ [3.30941040e+00, 2.59283970e+00, 2.57714110e+00],
29
+ [2.59557220e+00, 3.33477370e+00, 3.08793190e+00],
30
+ [2.58206180e+00, 3.41615670e+00, 3.26441990e+00],
31
+ [2.71127000e+00, 2.77032450e+00, 2.63466500e+00],
32
+ [2.79617850e+00, 3.25473720e+00, 3.41801560e+00],
33
+ [2.64741750e+00, 2.54538040e+00, 3.25354110e+00]])
34
+
35
+ ytdist = array([662., 877., 255., 412., 996., 295., 468., 268., 400., 754.,
36
+ 564., 138., 219., 869., 669.])
37
+
38
+ linkage_ytdist_single = array([[2., 5., 138., 2.],
39
+ [3., 4., 219., 2.],
40
+ [0., 7., 255., 3.],
41
+ [1., 8., 268., 4.],
42
+ [6., 9., 295., 6.]])
43
+
44
+ linkage_ytdist_complete = array([[2., 5., 138., 2.],
45
+ [3., 4., 219., 2.],
46
+ [1., 6., 400., 3.],
47
+ [0., 7., 412., 3.],
48
+ [8., 9., 996., 6.]])
49
+
50
+ linkage_ytdist_average = array([[2., 5., 138., 2.],
51
+ [3., 4., 219., 2.],
52
+ [0., 7., 333.5, 3.],
53
+ [1., 6., 347.5, 3.],
54
+ [8., 9., 680.77777778, 6.]])
55
+
56
+ linkage_ytdist_weighted = array([[2., 5., 138., 2.],
57
+ [3., 4., 219., 2.],
58
+ [0., 7., 333.5, 3.],
59
+ [1., 6., 347.5, 3.],
60
+ [8., 9., 670.125, 6.]])
61
+
62
+ # the optimal leaf ordering of linkage_ytdist_single
63
+ linkage_ytdist_single_olo = array([[5., 2., 138., 2.],
64
+ [4., 3., 219., 2.],
65
+ [7., 0., 255., 3.],
66
+ [1., 8., 268., 4.],
67
+ [6., 9., 295., 6.]])
68
+
69
+ X = array([[1.43054825, -7.5693489],
70
+ [6.95887839, 6.82293382],
71
+ [2.87137846, -9.68248579],
72
+ [7.87974764, -6.05485803],
73
+ [8.24018364, -6.09495602],
74
+ [7.39020262, 8.54004355]])
75
+
76
+ linkage_X_centroid = array([[3., 4., 0.36265956, 2.],
77
+ [1., 5., 1.77045373, 2.],
78
+ [0., 2., 2.55760419, 2.],
79
+ [6., 8., 6.43614494, 4.],
80
+ [7., 9., 15.17363237, 6.]])
81
+
82
+ linkage_X_median = array([[3., 4., 0.36265956, 2.],
83
+ [1., 5., 1.77045373, 2.],
84
+ [0., 2., 2.55760419, 2.],
85
+ [6., 8., 6.43614494, 4.],
86
+ [7., 9., 15.17363237, 6.]])
87
+
88
+ linkage_X_ward = array([[3., 4., 0.36265956, 2.],
89
+ [1., 5., 1.77045373, 2.],
90
+ [0., 2., 2.55760419, 2.],
91
+ [6., 8., 9.10208346, 4.],
92
+ [7., 9., 24.7784379, 6.]])
93
+
94
+ # the optimal leaf ordering of linkage_X_ward
95
+ linkage_X_ward_olo = array([[4., 3., 0.36265956, 2.],
96
+ [5., 1., 1.77045373, 2.],
97
+ [2., 0., 2.55760419, 2.],
98
+ [6., 8., 9.10208346, 4.],
99
+ [7., 9., 24.7784379, 6.]])
100
+
101
+ inconsistent_ytdist = {
102
+ 1: array([[138., 0., 1., 0.],
103
+ [219., 0., 1., 0.],
104
+ [255., 0., 1., 0.],
105
+ [268., 0., 1., 0.],
106
+ [295., 0., 1., 0.]]),
107
+ 2: array([[138., 0., 1., 0.],
108
+ [219., 0., 1., 0.],
109
+ [237., 25.45584412, 2., 0.70710678],
110
+ [261.5, 9.19238816, 2., 0.70710678],
111
+ [233.66666667, 83.9424406, 3., 0.7306594]]),
112
+ 3: array([[138., 0., 1., 0.],
113
+ [219., 0., 1., 0.],
114
+ [237., 25.45584412, 2., 0.70710678],
115
+ [247.33333333, 25.38372182, 3., 0.81417007],
116
+ [239., 69.36377537, 4., 0.80733783]]),
117
+ 4: array([[138., 0., 1., 0.],
118
+ [219., 0., 1., 0.],
119
+ [237., 25.45584412, 2., 0.70710678],
120
+ [247.33333333, 25.38372182, 3., 0.81417007],
121
+ [235., 60.73302232, 5., 0.98793042]])}
122
+
123
+ fcluster_inconsistent = {
124
+ 0.8: array([6, 2, 2, 4, 6, 2, 3, 7, 3, 5, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 1,
125
+ 1, 1, 1, 1, 1, 1, 1, 1, 1]),
126
+ 1.0: array([6, 2, 2, 4, 6, 2, 3, 7, 3, 5, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 1,
127
+ 1, 1, 1, 1, 1, 1, 1, 1, 1]),
128
+ 2.0: array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
129
+ 1, 1, 1, 1, 1, 1, 1, 1, 1])}
130
+
131
+ fcluster_distance = {
132
+ 0.6: array([4, 4, 4, 4, 4, 4, 4, 5, 4, 4, 6, 6, 6, 6, 6, 7, 6, 6, 6, 6, 3,
133
+ 1, 1, 1, 2, 1, 1, 1, 1, 1]),
134
+ 1.0: array([2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1,
135
+ 1, 1, 1, 1, 1, 1, 1, 1, 1]),
136
+ 2.0: array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
137
+ 1, 1, 1, 1, 1, 1, 1, 1, 1])}
138
+
139
+ fcluster_maxclust = {
140
+ 8.0: array([5, 5, 5, 5, 5, 5, 5, 6, 5, 5, 7, 7, 7, 7, 7, 8, 7, 7, 7, 7, 4,
141
+ 1, 1, 1, 3, 1, 1, 1, 1, 2]),
142
+ 4.0: array([3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2,
143
+ 1, 1, 1, 1, 1, 1, 1, 1, 1]),
144
+ 1.0: array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
145
+ 1, 1, 1, 1, 1, 1, 1, 1, 1])}
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/tests/test_disjoint_set.py ADDED
@@ -0,0 +1,202 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+ from pytest import raises as assert_raises
3
+ import numpy as np
4
+ from scipy.cluster.hierarchy import DisjointSet
5
+ import string
6
+
7
+
8
+ def generate_random_token():
9
+ k = len(string.ascii_letters)
10
+ tokens = list(np.arange(k, dtype=int))
11
+ tokens += list(np.arange(k, dtype=float))
12
+ tokens += list(string.ascii_letters)
13
+ tokens += [None for i in range(k)]
14
+ tokens = np.array(tokens, dtype=object)
15
+ rng = np.random.RandomState(seed=0)
16
+
17
+ while 1:
18
+ size = rng.randint(1, 3)
19
+ element = rng.choice(tokens, size)
20
+ if size == 1:
21
+ yield element[0]
22
+ else:
23
+ yield tuple(element)
24
+
25
+
26
+ def get_elements(n):
27
+ # dict is deterministic without difficulty of comparing numpy ints
28
+ elements = {}
29
+ for element in generate_random_token():
30
+ if element not in elements:
31
+ elements[element] = len(elements)
32
+ if len(elements) >= n:
33
+ break
34
+ return list(elements.keys())
35
+
36
+
37
+ def test_init():
38
+ n = 10
39
+ elements = get_elements(n)
40
+ dis = DisjointSet(elements)
41
+ assert dis.n_subsets == n
42
+ assert list(dis) == elements
43
+
44
+
45
+ def test_len():
46
+ n = 10
47
+ elements = get_elements(n)
48
+ dis = DisjointSet(elements)
49
+ assert len(dis) == n
50
+
51
+ dis.add("dummy")
52
+ assert len(dis) == n + 1
53
+
54
+
55
+ @pytest.mark.parametrize("n", [10, 100])
56
+ def test_contains(n):
57
+ elements = get_elements(n)
58
+ dis = DisjointSet(elements)
59
+ for x in elements:
60
+ assert x in dis
61
+
62
+ assert "dummy" not in dis
63
+
64
+
65
+ @pytest.mark.parametrize("n", [10, 100])
66
+ def test_add(n):
67
+ elements = get_elements(n)
68
+ dis1 = DisjointSet(elements)
69
+
70
+ dis2 = DisjointSet()
71
+ for i, x in enumerate(elements):
72
+ dis2.add(x)
73
+ assert len(dis2) == i + 1
74
+
75
+ # test idempotency by adding element again
76
+ dis2.add(x)
77
+ assert len(dis2) == i + 1
78
+
79
+ assert list(dis1) == list(dis2)
80
+
81
+
82
+ def test_element_not_present():
83
+ elements = get_elements(n=10)
84
+ dis = DisjointSet(elements)
85
+
86
+ with assert_raises(KeyError):
87
+ dis["dummy"]
88
+
89
+ with assert_raises(KeyError):
90
+ dis.merge(elements[0], "dummy")
91
+
92
+ with assert_raises(KeyError):
93
+ dis.connected(elements[0], "dummy")
94
+
95
+
96
+ @pytest.mark.parametrize("direction", ["forwards", "backwards"])
97
+ @pytest.mark.parametrize("n", [10, 100])
98
+ def test_linear_union_sequence(n, direction):
99
+ elements = get_elements(n)
100
+ dis = DisjointSet(elements)
101
+ assert elements == list(dis)
102
+
103
+ indices = list(range(n - 1))
104
+ if direction == "backwards":
105
+ indices = indices[::-1]
106
+
107
+ for it, i in enumerate(indices):
108
+ assert not dis.connected(elements[i], elements[i + 1])
109
+ assert dis.merge(elements[i], elements[i + 1])
110
+ assert dis.connected(elements[i], elements[i + 1])
111
+ assert dis.n_subsets == n - 1 - it
112
+
113
+ roots = [dis[i] for i in elements]
114
+ if direction == "forwards":
115
+ assert all(elements[0] == r for r in roots)
116
+ else:
117
+ assert all(elements[-2] == r for r in roots)
118
+ assert not dis.merge(elements[0], elements[-1])
119
+
120
+
121
+ @pytest.mark.parametrize("n", [10, 100])
122
+ def test_self_unions(n):
123
+ elements = get_elements(n)
124
+ dis = DisjointSet(elements)
125
+
126
+ for x in elements:
127
+ assert dis.connected(x, x)
128
+ assert not dis.merge(x, x)
129
+ assert dis.connected(x, x)
130
+ assert dis.n_subsets == len(elements)
131
+
132
+ assert elements == list(dis)
133
+ roots = [dis[x] for x in elements]
134
+ assert elements == roots
135
+
136
+
137
+ @pytest.mark.parametrize("order", ["ab", "ba"])
138
+ @pytest.mark.parametrize("n", [10, 100])
139
+ def test_equal_size_ordering(n, order):
140
+ elements = get_elements(n)
141
+ dis = DisjointSet(elements)
142
+
143
+ rng = np.random.RandomState(seed=0)
144
+ indices = np.arange(n)
145
+ rng.shuffle(indices)
146
+
147
+ for i in range(0, len(indices), 2):
148
+ a, b = elements[indices[i]], elements[indices[i + 1]]
149
+ if order == "ab":
150
+ assert dis.merge(a, b)
151
+ else:
152
+ assert dis.merge(b, a)
153
+
154
+ expected = elements[min(indices[i], indices[i + 1])]
155
+ assert dis[a] == expected
156
+ assert dis[b] == expected
157
+
158
+
159
+ @pytest.mark.parametrize("kmax", [5, 10])
160
+ def test_binary_tree(kmax):
161
+ n = 2**kmax
162
+ elements = get_elements(n)
163
+ dis = DisjointSet(elements)
164
+ rng = np.random.RandomState(seed=0)
165
+
166
+ for k in 2**np.arange(kmax):
167
+ for i in range(0, n, 2 * k):
168
+ r1, r2 = rng.randint(0, k, size=2)
169
+ a, b = elements[i + r1], elements[i + k + r2]
170
+ assert not dis.connected(a, b)
171
+ assert dis.merge(a, b)
172
+ assert dis.connected(a, b)
173
+
174
+ assert elements == list(dis)
175
+ roots = [dis[i] for i in elements]
176
+ expected_indices = np.arange(n) - np.arange(n) % (2 * k)
177
+ expected = [elements[i] for i in expected_indices]
178
+ assert roots == expected
179
+
180
+
181
+ @pytest.mark.parametrize("n", [10, 100])
182
+ def test_subsets(n):
183
+ elements = get_elements(n)
184
+ dis = DisjointSet(elements)
185
+
186
+ rng = np.random.RandomState(seed=0)
187
+ for i, j in rng.randint(0, n, (n, 2)):
188
+ x = elements[i]
189
+ y = elements[j]
190
+
191
+ expected = {element for element in dis if {dis[element]} == {dis[x]}}
192
+ assert dis.subset_size(x) == len(dis.subset(x))
193
+ assert expected == dis.subset(x)
194
+
195
+ expected = {dis[element]: set() for element in dis}
196
+ for element in dis:
197
+ expected[dis[element]].add(element)
198
+ expected = list(expected.values())
199
+ assert expected == dis.subsets()
200
+
201
+ dis.merge(x, y)
202
+ assert dis.subset(x) == dis.subset(y)
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/tests/test_hierarchy.py ADDED
@@ -0,0 +1,1300 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Author: Damian Eads
3
+ # Date: April 17, 2008
4
+ #
5
+ # Copyright (C) 2008 Damian Eads
6
+ #
7
+ # Redistribution and use in source and binary forms, with or without
8
+ # modification, are permitted provided that the following conditions
9
+ # are met:
10
+ #
11
+ # 1. Redistributions of source code must retain the above copyright
12
+ # notice, this list of conditions and the following disclaimer.
13
+ #
14
+ # 2. Redistributions in binary form must reproduce the above
15
+ # copyright notice, this list of conditions and the following
16
+ # disclaimer in the documentation and/or other materials provided
17
+ # with the distribution.
18
+ #
19
+ # 3. The name of the author may not be used to endorse or promote
20
+ # products derived from this software without specific prior
21
+ # written permission.
22
+ #
23
+ # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
24
+ # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25
+ # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
27
+ # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29
+ # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31
+ # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32
+ # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33
+ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
+ import numpy as np
35
+ from numpy.testing import (assert_allclose, assert_equal, assert_array_equal, assert_,
36
+ assert_warns)
37
+ import pytest
38
+ from pytest import raises as assert_raises
39
+
40
+ import scipy.cluster.hierarchy
41
+ from scipy.cluster.hierarchy import (
42
+ ClusterWarning, linkage, from_mlab_linkage, to_mlab_linkage,
43
+ num_obs_linkage, inconsistent, cophenet, fclusterdata, fcluster,
44
+ is_isomorphic, single, leaders,
45
+ correspond, is_monotonic, maxdists, maxinconsts, maxRstat,
46
+ is_valid_linkage, is_valid_im, to_tree, leaves_list, dendrogram,
47
+ set_link_color_palette, cut_tree, optimal_leaf_ordering,
48
+ _order_cluster_tree, _hierarchy, _LINKAGE_METHODS)
49
+ from scipy.spatial.distance import pdist
50
+ from scipy.cluster._hierarchy import Heap
51
+ from scipy.conftest import array_api_compatible
52
+ from scipy._lib._array_api import xp_assert_close, xp_assert_equal
53
+
54
+ from threading import Lock
55
+
56
+ from . import hierarchy_test_data
57
+
58
+
59
+ # Matplotlib is not a scipy dependency but is optionally used in dendrogram, so
60
+ # check if it's available
61
+ try:
62
+ import matplotlib
63
+ # and set the backend to be Agg (no gui)
64
+ matplotlib.use('Agg')
65
+ # before importing pyplot
66
+ import matplotlib.pyplot as plt
67
+ have_matplotlib = True
68
+ except Exception:
69
+ have_matplotlib = False
70
+
71
+
72
+ pytestmark = [array_api_compatible, pytest.mark.usefixtures("skip_xp_backends")]
73
+ skip_xp_backends = pytest.mark.skip_xp_backends
74
+
75
+
76
+ class TestLinkage:
77
+
78
+ @skip_xp_backends(cpu_only=True)
79
+ def test_linkage_non_finite_elements_in_distance_matrix(self, xp):
80
+ # Tests linkage(Y) where Y contains a non-finite element (e.g. NaN or Inf).
81
+ # Exception expected.
82
+ y = xp.asarray([xp.nan] + [0.0]*5)
83
+ assert_raises(ValueError, linkage, y)
84
+
85
+ @skip_xp_backends(cpu_only=True)
86
+ def test_linkage_empty_distance_matrix(self, xp):
87
+ # Tests linkage(Y) where Y is a 0x4 linkage matrix. Exception expected.
88
+ y = xp.zeros((0,))
89
+ assert_raises(ValueError, linkage, y)
90
+
91
+ @skip_xp_backends(cpu_only=True)
92
+ def test_linkage_tdist(self, xp):
93
+ for method in ['single', 'complete', 'average', 'weighted']:
94
+ self.check_linkage_tdist(method, xp)
95
+
96
+ def check_linkage_tdist(self, method, xp):
97
+ # Tests linkage(Y, method) on the tdist data set.
98
+ Z = linkage(xp.asarray(hierarchy_test_data.ytdist), method)
99
+ expectedZ = getattr(hierarchy_test_data, 'linkage_ytdist_' + method)
100
+ xp_assert_close(Z, xp.asarray(expectedZ), atol=1e-10)
101
+
102
+ @skip_xp_backends(cpu_only=True)
103
+ def test_linkage_X(self, xp):
104
+ for method in ['centroid', 'median', 'ward']:
105
+ self.check_linkage_q(method, xp)
106
+
107
+ def check_linkage_q(self, method, xp):
108
+ # Tests linkage(Y, method) on the Q data set.
109
+ Z = linkage(xp.asarray(hierarchy_test_data.X), method)
110
+ expectedZ = getattr(hierarchy_test_data, 'linkage_X_' + method)
111
+ xp_assert_close(Z, xp.asarray(expectedZ), atol=1e-06)
112
+
113
+ y = scipy.spatial.distance.pdist(hierarchy_test_data.X,
114
+ metric="euclidean")
115
+ Z = linkage(xp.asarray(y), method)
116
+ xp_assert_close(Z, xp.asarray(expectedZ), atol=1e-06)
117
+
118
+ @skip_xp_backends(cpu_only=True)
119
+ def test_compare_with_trivial(self, xp):
120
+ rng = np.random.RandomState(0)
121
+ n = 20
122
+ X = rng.rand(n, 2)
123
+ d = pdist(X)
124
+
125
+ for method, code in _LINKAGE_METHODS.items():
126
+ Z_trivial = _hierarchy.linkage(d, n, code)
127
+ Z = linkage(xp.asarray(d), method)
128
+ xp_assert_close(Z, xp.asarray(Z_trivial), rtol=1e-14, atol=1e-15)
129
+
130
+ @skip_xp_backends(cpu_only=True)
131
+ def test_optimal_leaf_ordering(self, xp):
132
+ Z = linkage(xp.asarray(hierarchy_test_data.ytdist), optimal_ordering=True)
133
+ expectedZ = getattr(hierarchy_test_data, 'linkage_ytdist_single_olo')
134
+ xp_assert_close(Z, xp.asarray(expectedZ), atol=1e-10)
135
+
136
+
137
+ @skip_xp_backends(cpu_only=True)
138
+ class TestLinkageTies:
139
+
140
+ _expectations = {
141
+ 'single': np.array([[0, 1, 1.41421356, 2],
142
+ [2, 3, 1.41421356, 3]]),
143
+ 'complete': np.array([[0, 1, 1.41421356, 2],
144
+ [2, 3, 2.82842712, 3]]),
145
+ 'average': np.array([[0, 1, 1.41421356, 2],
146
+ [2, 3, 2.12132034, 3]]),
147
+ 'weighted': np.array([[0, 1, 1.41421356, 2],
148
+ [2, 3, 2.12132034, 3]]),
149
+ 'centroid': np.array([[0, 1, 1.41421356, 2],
150
+ [2, 3, 2.12132034, 3]]),
151
+ 'median': np.array([[0, 1, 1.41421356, 2],
152
+ [2, 3, 2.12132034, 3]]),
153
+ 'ward': np.array([[0, 1, 1.41421356, 2],
154
+ [2, 3, 2.44948974, 3]]),
155
+ }
156
+
157
+ def test_linkage_ties(self, xp):
158
+ for method in ['single', 'complete', 'average', 'weighted',
159
+ 'centroid', 'median', 'ward']:
160
+ self.check_linkage_ties(method, xp)
161
+
162
+ def check_linkage_ties(self, method, xp):
163
+ X = xp.asarray([[-1, -1], [0, 0], [1, 1]])
164
+ Z = linkage(X, method=method)
165
+ expectedZ = self._expectations[method]
166
+ xp_assert_close(Z, xp.asarray(expectedZ), atol=1e-06)
167
+
168
+
169
+ @skip_xp_backends(cpu_only=True)
170
+ class TestInconsistent:
171
+
172
+ def test_inconsistent_tdist(self, xp):
173
+ for depth in hierarchy_test_data.inconsistent_ytdist:
174
+ self.check_inconsistent_tdist(depth, xp)
175
+
176
+ def check_inconsistent_tdist(self, depth, xp):
177
+ Z = xp.asarray(hierarchy_test_data.linkage_ytdist_single)
178
+ xp_assert_close(inconsistent(Z, depth),
179
+ xp.asarray(hierarchy_test_data.inconsistent_ytdist[depth]))
180
+
181
+
182
+ @skip_xp_backends(cpu_only=True)
183
+ class TestCopheneticDistance:
184
+
185
+ def test_linkage_cophenet_tdist_Z(self, xp):
186
+ # Tests cophenet(Z) on tdist data set.
187
+ expectedM = xp.asarray([268, 295, 255, 255, 295, 295, 268, 268, 295, 295,
188
+ 295, 138, 219, 295, 295])
189
+ Z = xp.asarray(hierarchy_test_data.linkage_ytdist_single)
190
+ M = cophenet(Z)
191
+ xp_assert_close(M, xp.asarray(expectedM, dtype=xp.float64), atol=1e-10)
192
+
193
+ def test_linkage_cophenet_tdist_Z_Y(self, xp):
194
+ # Tests cophenet(Z, Y) on tdist data set.
195
+ Z = xp.asarray(hierarchy_test_data.linkage_ytdist_single)
196
+ (c, M) = cophenet(Z, xp.asarray(hierarchy_test_data.ytdist))
197
+ expectedM = xp.asarray([268, 295, 255, 255, 295, 295, 268, 268, 295, 295,
198
+ 295, 138, 219, 295, 295], dtype=xp.float64)
199
+ expectedc = xp.asarray(0.639931296433393415057366837573, dtype=xp.float64)[()]
200
+ xp_assert_close(c, expectedc, atol=1e-10)
201
+ xp_assert_close(M, expectedM, atol=1e-10)
202
+
203
+ def test_gh_22183(self, xp):
204
+ # check for lack of segfault
205
+ # (out of bounds memory access)
206
+ # and correct interception of
207
+ # invalid linkage matrix
208
+ arr=[[0.0, 1.0, 1.0, 2.0],
209
+ [2.0, 12.0, 1.0, 3.0],
210
+ [3.0, 4.0, 1.0, 2.0],
211
+ [5.0, 14.0, 1.0, 3.0],
212
+ [6.0, 7.0, 1.0, 2.0],
213
+ [8.0, 16.0, 1.0, 3.0],
214
+ [9.0, 10.0, 1.0, 2.0],
215
+ [11.0, 18.0, 1.0, 3.0],
216
+ [13.0, 15.0, 2.0, 6.0],
217
+ [17.0, 20.0, 2.0, 32.0],
218
+ [19.0, 21.0, 2.0, 12.0]]
219
+ with pytest.raises(ValueError, match="excessive observations"):
220
+ cophenet(xp.asarray(arr))
221
+
222
+
223
+ class TestMLabLinkageConversion:
224
+
225
+ def test_mlab_linkage_conversion_empty(self, xp):
226
+ # Tests from/to_mlab_linkage on empty linkage array.
227
+ X = xp.asarray([], dtype=xp.float64)
228
+ xp_assert_equal(from_mlab_linkage(X), X)
229
+ xp_assert_equal(to_mlab_linkage(X), X)
230
+
231
+ @skip_xp_backends(cpu_only=True)
232
+ def test_mlab_linkage_conversion_single_row(self, xp):
233
+ # Tests from/to_mlab_linkage on linkage array with single row.
234
+ Z = xp.asarray([[0., 1., 3., 2.]])
235
+ Zm = xp.asarray([[1, 2, 3]])
236
+ xp_assert_close(from_mlab_linkage(Zm), xp.asarray(Z, dtype=xp.float64),
237
+ rtol=1e-15)
238
+ xp_assert_close(to_mlab_linkage(Z), xp.asarray(Zm, dtype=xp.float64),
239
+ rtol=1e-15)
240
+
241
+ @skip_xp_backends(cpu_only=True)
242
+ def test_mlab_linkage_conversion_multiple_rows(self, xp):
243
+ # Tests from/to_mlab_linkage on linkage array with multiple rows.
244
+ Zm = xp.asarray([[3, 6, 138], [4, 5, 219],
245
+ [1, 8, 255], [2, 9, 268], [7, 10, 295]])
246
+ Z = xp.asarray([[2., 5., 138., 2.],
247
+ [3., 4., 219., 2.],
248
+ [0., 7., 255., 3.],
249
+ [1., 8., 268., 4.],
250
+ [6., 9., 295., 6.]],
251
+ dtype=xp.float64)
252
+ xp_assert_close(from_mlab_linkage(Zm), Z, rtol=1e-15)
253
+ xp_assert_close(to_mlab_linkage(Z), xp.asarray(Zm, dtype=xp.float64),
254
+ rtol=1e-15)
255
+
256
+
257
+ @skip_xp_backends(cpu_only=True)
258
+ class TestFcluster:
259
+
260
+ def test_fclusterdata(self, xp):
261
+ for t in hierarchy_test_data.fcluster_inconsistent:
262
+ self.check_fclusterdata(t, 'inconsistent', xp)
263
+ for t in hierarchy_test_data.fcluster_distance:
264
+ self.check_fclusterdata(t, 'distance', xp)
265
+ for t in hierarchy_test_data.fcluster_maxclust:
266
+ self.check_fclusterdata(t, 'maxclust', xp)
267
+
268
+ def check_fclusterdata(self, t, criterion, xp):
269
+ # Tests fclusterdata(X, criterion=criterion, t=t) on a random 3-cluster data set
270
+ expectedT = xp.asarray(getattr(hierarchy_test_data, 'fcluster_' + criterion)[t])
271
+ X = xp.asarray(hierarchy_test_data.Q_X)
272
+ T = fclusterdata(X, criterion=criterion, t=t)
273
+ assert_(is_isomorphic(T, expectedT))
274
+
275
+ def test_fcluster(self, xp):
276
+ for t in hierarchy_test_data.fcluster_inconsistent:
277
+ self.check_fcluster(t, 'inconsistent', xp)
278
+ for t in hierarchy_test_data.fcluster_distance:
279
+ self.check_fcluster(t, 'distance', xp)
280
+ for t in hierarchy_test_data.fcluster_maxclust:
281
+ self.check_fcluster(t, 'maxclust', xp)
282
+
283
+ def check_fcluster(self, t, criterion, xp):
284
+ # Tests fcluster(Z, criterion=criterion, t=t) on a random 3-cluster data set.
285
+ expectedT = xp.asarray(getattr(hierarchy_test_data, 'fcluster_' + criterion)[t])
286
+ Z = single(xp.asarray(hierarchy_test_data.Q_X))
287
+ T = fcluster(Z, criterion=criterion, t=t)
288
+ assert_(is_isomorphic(T, expectedT))
289
+
290
+ def test_fcluster_monocrit(self, xp):
291
+ for t in hierarchy_test_data.fcluster_distance:
292
+ self.check_fcluster_monocrit(t, xp)
293
+ for t in hierarchy_test_data.fcluster_maxclust:
294
+ self.check_fcluster_maxclust_monocrit(t, xp)
295
+
296
+ def check_fcluster_monocrit(self, t, xp):
297
+ expectedT = xp.asarray(hierarchy_test_data.fcluster_distance[t])
298
+ Z = single(xp.asarray(hierarchy_test_data.Q_X))
299
+ T = fcluster(Z, t, criterion='monocrit', monocrit=maxdists(Z))
300
+ assert_(is_isomorphic(T, expectedT))
301
+
302
+ def check_fcluster_maxclust_monocrit(self, t, xp):
303
+ expectedT = xp.asarray(hierarchy_test_data.fcluster_maxclust[t])
304
+ Z = single(xp.asarray(hierarchy_test_data.Q_X))
305
+ T = fcluster(Z, t, criterion='maxclust_monocrit', monocrit=maxdists(Z))
306
+ assert_(is_isomorphic(T, expectedT))
307
+
308
+ def test_fcluster_maxclust_gh_12651(self, xp):
309
+ y = xp.asarray([[1], [4], [5]])
310
+ Z = single(y)
311
+ assert_array_equal(fcluster(Z, t=1, criterion="maxclust"),
312
+ xp.asarray([1, 1, 1]))
313
+ assert_array_equal(fcluster(Z, t=2, criterion="maxclust"),
314
+ xp.asarray([2, 1, 1]))
315
+ assert_array_equal(fcluster(Z, t=3, criterion="maxclust"),
316
+ xp.asarray([1, 2, 3]))
317
+ assert_array_equal(fcluster(Z, t=5, criterion="maxclust"),
318
+ xp.asarray([1, 2, 3]))
319
+
320
+
321
+ @skip_xp_backends(cpu_only=True)
322
+ class TestLeaders:
323
+
324
+ def test_leaders_single(self, xp):
325
+ # Tests leaders using a flat clustering generated by single linkage.
326
+ X = hierarchy_test_data.Q_X
327
+ Y = pdist(X)
328
+ Y = xp.asarray(Y)
329
+ Z = linkage(Y)
330
+ T = fcluster(Z, criterion='maxclust', t=3)
331
+ Lright = (xp.asarray([53, 55, 56]), xp.asarray([2, 3, 1]))
332
+ T = xp.asarray(T, dtype=xp.int32)
333
+ L = leaders(Z, T)
334
+ assert_allclose(np.concatenate(L), np.concatenate(Lright), rtol=1e-15)
335
+
336
+
337
+ @skip_xp_backends(np_only=True,
338
+ reason='`is_isomorphic` only supports NumPy backend')
339
+ class TestIsIsomorphic:
340
+
341
+ @skip_xp_backends(np_only=True,
342
+ reason='array-likes only supported for NumPy backend')
343
+ def test_array_like(self, xp):
344
+ assert is_isomorphic([1, 1, 1], [2, 2, 2])
345
+ assert is_isomorphic([], [])
346
+
347
+ def test_is_isomorphic_1(self, xp):
348
+ # Tests is_isomorphic on test case #1 (one flat cluster, different labellings)
349
+ a = xp.asarray([1, 1, 1])
350
+ b = xp.asarray([2, 2, 2])
351
+ assert is_isomorphic(a, b)
352
+ assert is_isomorphic(b, a)
353
+
354
+ def test_is_isomorphic_2(self, xp):
355
+ # Tests is_isomorphic on test case #2 (two flat clusters, different labelings)
356
+ a = xp.asarray([1, 7, 1])
357
+ b = xp.asarray([2, 3, 2])
358
+ assert is_isomorphic(a, b)
359
+ assert is_isomorphic(b, a)
360
+
361
+ def test_is_isomorphic_3(self, xp):
362
+ # Tests is_isomorphic on test case #3 (no flat clusters)
363
+ a = xp.asarray([])
364
+ b = xp.asarray([])
365
+ assert is_isomorphic(a, b)
366
+
367
+ def test_is_isomorphic_4A(self, xp):
368
+ # Tests is_isomorphic on test case #4A
369
+ # (3 flat clusters, different labelings, isomorphic)
370
+ a = xp.asarray([1, 2, 3])
371
+ b = xp.asarray([1, 3, 2])
372
+ assert is_isomorphic(a, b)
373
+ assert is_isomorphic(b, a)
374
+
375
+ def test_is_isomorphic_4B(self, xp):
376
+ # Tests is_isomorphic on test case #4B
377
+ # (3 flat clusters, different labelings, nonisomorphic)
378
+ a = xp.asarray([1, 2, 3, 3])
379
+ b = xp.asarray([1, 3, 2, 3])
380
+ assert is_isomorphic(a, b) is False
381
+ assert is_isomorphic(b, a) is False
382
+
383
+ def test_is_isomorphic_4C(self, xp):
384
+ # Tests is_isomorphic on test case #4C
385
+ # (3 flat clusters, different labelings, isomorphic)
386
+ a = xp.asarray([7, 2, 3])
387
+ b = xp.asarray([6, 3, 2])
388
+ assert is_isomorphic(a, b)
389
+ assert is_isomorphic(b, a)
390
+
391
+ def test_is_isomorphic_5(self, xp):
392
+ # Tests is_isomorphic on test case #5 (1000 observations, 2/3/5 random
393
+ # clusters, random permutation of the labeling).
394
+ for nc in [2, 3, 5]:
395
+ self.help_is_isomorphic_randperm(1000, nc, xp=xp)
396
+
397
+ def test_is_isomorphic_6(self, xp):
398
+ # Tests is_isomorphic on test case #5A (1000 observations, 2/3/5 random
399
+ # clusters, random permutation of the labeling, slightly
400
+ # nonisomorphic.)
401
+ for nc in [2, 3, 5]:
402
+ self.help_is_isomorphic_randperm(1000, nc, True, 5, xp=xp)
403
+
404
+ def test_is_isomorphic_7(self, xp):
405
+ # Regression test for gh-6271
406
+ a = xp.asarray([1, 2, 3])
407
+ b = xp.asarray([1, 1, 1])
408
+ assert not is_isomorphic(a, b)
409
+
410
+ def help_is_isomorphic_randperm(self, nobs, nclusters, noniso=False, nerrors=0,
411
+ *, xp):
412
+ for k in range(3):
413
+ a = (np.random.rand(nobs) * nclusters).astype(int)
414
+ b = np.zeros(a.size, dtype=int)
415
+ P = np.random.permutation(nclusters)
416
+ for i in range(0, a.shape[0]):
417
+ b[i] = P[a[i]]
418
+ if noniso:
419
+ Q = np.random.permutation(nobs)
420
+ b[Q[0:nerrors]] += 1
421
+ b[Q[0:nerrors]] %= nclusters
422
+ a = xp.asarray(a)
423
+ b = xp.asarray(b)
424
+ assert is_isomorphic(a, b) == (not noniso)
425
+ assert is_isomorphic(b, a) == (not noniso)
426
+
427
+
428
+ @skip_xp_backends(cpu_only=True)
429
+ class TestIsValidLinkage:
430
+
431
+ def test_is_valid_linkage_various_size(self, xp):
432
+ for nrow, ncol, valid in [(2, 5, False), (2, 3, False),
433
+ (1, 4, True), (2, 4, True)]:
434
+ self.check_is_valid_linkage_various_size(nrow, ncol, valid, xp)
435
+
436
+ def check_is_valid_linkage_various_size(self, nrow, ncol, valid, xp):
437
+ # Tests is_valid_linkage(Z) with linkage matrices of various sizes
438
+ Z = xp.asarray([[0, 1, 3.0, 2, 5],
439
+ [3, 2, 4.0, 3, 3]], dtype=xp.float64)
440
+ Z = Z[:nrow, :ncol]
441
+ assert_(is_valid_linkage(Z) == valid)
442
+ if not valid:
443
+ assert_raises(ValueError, is_valid_linkage, Z, throw=True)
444
+
445
+ def test_is_valid_linkage_int_type(self, xp):
446
+ # Tests is_valid_linkage(Z) with integer type.
447
+ Z = xp.asarray([[0, 1, 3.0, 2],
448
+ [3, 2, 4.0, 3]], dtype=xp.int64)
449
+ assert_(is_valid_linkage(Z) is False)
450
+ assert_raises(TypeError, is_valid_linkage, Z, throw=True)
451
+
452
+ def test_is_valid_linkage_empty(self, xp):
453
+ # Tests is_valid_linkage(Z) with empty linkage.
454
+ Z = xp.zeros((0, 4), dtype=xp.float64)
455
+ assert_(is_valid_linkage(Z) is False)
456
+ assert_raises(ValueError, is_valid_linkage, Z, throw=True)
457
+
458
+ def test_is_valid_linkage_4_and_up(self, xp):
459
+ # Tests is_valid_linkage(Z) on linkage on observation sets between
460
+ # sizes 4 and 15 (step size 3).
461
+ for i in range(4, 15, 3):
462
+ y = np.random.rand(i*(i-1)//2)
463
+ y = xp.asarray(y)
464
+ Z = linkage(y)
465
+ assert_(is_valid_linkage(Z) is True)
466
+
467
+ @skip_xp_backends('jax.numpy',
468
+ reason='jax arrays do not support item assignment')
469
+ def test_is_valid_linkage_4_and_up_neg_index_left(self, xp):
470
+ # Tests is_valid_linkage(Z) on linkage on observation sets between
471
+ # sizes 4 and 15 (step size 3) with negative indices (left).
472
+ for i in range(4, 15, 3):
473
+ y = np.random.rand(i*(i-1)//2)
474
+ y = xp.asarray(y)
475
+ Z = linkage(y)
476
+ Z[i//2,0] = -2
477
+ assert_(is_valid_linkage(Z) is False)
478
+ assert_raises(ValueError, is_valid_linkage, Z, throw=True)
479
+
480
+ @skip_xp_backends('jax.numpy',
481
+ reason='jax arrays do not support item assignment')
482
+ def test_is_valid_linkage_4_and_up_neg_index_right(self, xp):
483
+ # Tests is_valid_linkage(Z) on linkage on observation sets between
484
+ # sizes 4 and 15 (step size 3) with negative indices (right).
485
+ for i in range(4, 15, 3):
486
+ y = np.random.rand(i*(i-1)//2)
487
+ y = xp.asarray(y)
488
+ Z = linkage(y)
489
+ Z[i//2,1] = -2
490
+ assert_(is_valid_linkage(Z) is False)
491
+ assert_raises(ValueError, is_valid_linkage, Z, throw=True)
492
+
493
+ @skip_xp_backends('jax.numpy',
494
+ reason='jax arrays do not support item assignment')
495
+ def test_is_valid_linkage_4_and_up_neg_dist(self, xp):
496
+ # Tests is_valid_linkage(Z) on linkage on observation sets between
497
+ # sizes 4 and 15 (step size 3) with negative distances.
498
+ for i in range(4, 15, 3):
499
+ y = np.random.rand(i*(i-1)//2)
500
+ y = xp.asarray(y)
501
+ Z = linkage(y)
502
+ Z[i//2,2] = -0.5
503
+ assert_(is_valid_linkage(Z) is False)
504
+ assert_raises(ValueError, is_valid_linkage, Z, throw=True)
505
+
506
+ @skip_xp_backends('jax.numpy',
507
+ reason='jax arrays do not support item assignment')
508
+ def test_is_valid_linkage_4_and_up_neg_counts(self, xp):
509
+ # Tests is_valid_linkage(Z) on linkage on observation sets between
510
+ # sizes 4 and 15 (step size 3) with negative counts.
511
+ for i in range(4, 15, 3):
512
+ y = np.random.rand(i*(i-1)//2)
513
+ y = xp.asarray(y)
514
+ Z = linkage(y)
515
+ Z[i//2,3] = -2
516
+ assert_(is_valid_linkage(Z) is False)
517
+ assert_raises(ValueError, is_valid_linkage, Z, throw=True)
518
+
519
+
520
+ @skip_xp_backends(cpu_only=True)
521
+ class TestIsValidInconsistent:
522
+
523
+ def test_is_valid_im_int_type(self, xp):
524
+ # Tests is_valid_im(R) with integer type.
525
+ R = xp.asarray([[0, 1, 3.0, 2],
526
+ [3, 2, 4.0, 3]], dtype=xp.int64)
527
+ assert_(is_valid_im(R) is False)
528
+ assert_raises(TypeError, is_valid_im, R, throw=True)
529
+
530
+ def test_is_valid_im_various_size(self, xp):
531
+ for nrow, ncol, valid in [(2, 5, False), (2, 3, False),
532
+ (1, 4, True), (2, 4, True)]:
533
+ self.check_is_valid_im_various_size(nrow, ncol, valid, xp)
534
+
535
+ def check_is_valid_im_various_size(self, nrow, ncol, valid, xp):
536
+ # Tests is_valid_im(R) with linkage matrices of various sizes
537
+ R = xp.asarray([[0, 1, 3.0, 2, 5],
538
+ [3, 2, 4.0, 3, 3]], dtype=xp.float64)
539
+ R = R[:nrow, :ncol]
540
+ assert_(is_valid_im(R) == valid)
541
+ if not valid:
542
+ assert_raises(ValueError, is_valid_im, R, throw=True)
543
+
544
+ def test_is_valid_im_empty(self, xp):
545
+ # Tests is_valid_im(R) with empty inconsistency matrix.
546
+ R = xp.zeros((0, 4), dtype=xp.float64)
547
+ assert_(is_valid_im(R) is False)
548
+ assert_raises(ValueError, is_valid_im, R, throw=True)
549
+
550
+ def test_is_valid_im_4_and_up(self, xp):
551
+ # Tests is_valid_im(R) on im on observation sets between sizes 4 and 15
552
+ # (step size 3).
553
+ for i in range(4, 15, 3):
554
+ y = np.random.rand(i*(i-1)//2)
555
+ y = xp.asarray(y)
556
+ Z = linkage(y)
557
+ R = inconsistent(Z)
558
+ assert_(is_valid_im(R) is True)
559
+
560
+ @skip_xp_backends('jax.numpy', reason='jax arrays do not support item assignment')
561
+ def test_is_valid_im_4_and_up_neg_index_left(self, xp):
562
+ # Tests is_valid_im(R) on im on observation sets between sizes 4 and 15
563
+ # (step size 3) with negative link height means.
564
+ for i in range(4, 15, 3):
565
+ y = np.random.rand(i*(i-1)//2)
566
+ y = xp.asarray(y)
567
+ Z = linkage(y)
568
+ R = inconsistent(Z)
569
+ R[i//2,0] = -2.0
570
+ assert_(is_valid_im(R) is False)
571
+ assert_raises(ValueError, is_valid_im, R, throw=True)
572
+
573
+ @skip_xp_backends('jax.numpy', reason='jax arrays do not support item assignment')
574
+ def test_is_valid_im_4_and_up_neg_index_right(self, xp):
575
+ # Tests is_valid_im(R) on im on observation sets between sizes 4 and 15
576
+ # (step size 3) with negative link height standard deviations.
577
+ for i in range(4, 15, 3):
578
+ y = np.random.rand(i*(i-1)//2)
579
+ y = xp.asarray(y)
580
+ Z = linkage(y)
581
+ R = inconsistent(Z)
582
+ R[i//2,1] = -2.0
583
+ assert_(is_valid_im(R) is False)
584
+ assert_raises(ValueError, is_valid_im, R, throw=True)
585
+
586
+ @skip_xp_backends('jax.numpy', reason='jax arrays do not support item assignment')
587
+ def test_is_valid_im_4_and_up_neg_dist(self, xp):
588
+ # Tests is_valid_im(R) on im on observation sets between sizes 4 and 15
589
+ # (step size 3) with negative link counts.
590
+ for i in range(4, 15, 3):
591
+ y = np.random.rand(i*(i-1)//2)
592
+ y = xp.asarray(y)
593
+ Z = linkage(y)
594
+ R = inconsistent(Z)
595
+ R[i//2,2] = -0.5
596
+ assert_(is_valid_im(R) is False)
597
+ assert_raises(ValueError, is_valid_im, R, throw=True)
598
+
599
+
600
+ class TestNumObsLinkage:
601
+
602
+ @skip_xp_backends(cpu_only=True)
603
+ def test_num_obs_linkage_empty(self, xp):
604
+ # Tests num_obs_linkage(Z) with empty linkage.
605
+ Z = xp.zeros((0, 4), dtype=xp.float64)
606
+ assert_raises(ValueError, num_obs_linkage, Z)
607
+
608
+ def test_num_obs_linkage_1x4(self, xp):
609
+ # Tests num_obs_linkage(Z) on linkage over 2 observations.
610
+ Z = xp.asarray([[0, 1, 3.0, 2]], dtype=xp.float64)
611
+ assert_equal(num_obs_linkage(Z), 2)
612
+
613
+ def test_num_obs_linkage_2x4(self, xp):
614
+ # Tests num_obs_linkage(Z) on linkage over 3 observations.
615
+ Z = xp.asarray([[0, 1, 3.0, 2],
616
+ [3, 2, 4.0, 3]], dtype=xp.float64)
617
+ assert_equal(num_obs_linkage(Z), 3)
618
+
619
+ @skip_xp_backends(cpu_only=True)
620
+ def test_num_obs_linkage_4_and_up(self, xp):
621
+ # Tests num_obs_linkage(Z) on linkage on observation sets between sizes
622
+ # 4 and 15 (step size 3).
623
+ for i in range(4, 15, 3):
624
+ y = np.random.rand(i*(i-1)//2)
625
+ y = xp.asarray(y)
626
+ Z = linkage(y)
627
+ assert_equal(num_obs_linkage(Z), i)
628
+
629
+
630
+ @skip_xp_backends(cpu_only=True)
631
+ class TestLeavesList:
632
+
633
+ def test_leaves_list_1x4(self, xp):
634
+ # Tests leaves_list(Z) on a 1x4 linkage.
635
+ Z = xp.asarray([[0, 1, 3.0, 2]], dtype=xp.float64)
636
+ to_tree(Z)
637
+ assert_allclose(leaves_list(Z), [0, 1], rtol=1e-15)
638
+
639
+ def test_leaves_list_2x4(self, xp):
640
+ # Tests leaves_list(Z) on a 2x4 linkage.
641
+ Z = xp.asarray([[0, 1, 3.0, 2],
642
+ [3, 2, 4.0, 3]], dtype=xp.float64)
643
+ to_tree(Z)
644
+ assert_allclose(leaves_list(Z), [0, 1, 2], rtol=1e-15)
645
+
646
+ def test_leaves_list_Q(self, xp):
647
+ for method in ['single', 'complete', 'average', 'weighted', 'centroid',
648
+ 'median', 'ward']:
649
+ self.check_leaves_list_Q(method, xp)
650
+
651
+ def check_leaves_list_Q(self, method, xp):
652
+ # Tests leaves_list(Z) on the Q data set
653
+ X = xp.asarray(hierarchy_test_data.Q_X)
654
+ Z = linkage(X, method)
655
+ node = to_tree(Z)
656
+ assert_allclose(node.pre_order(), leaves_list(Z), rtol=1e-15)
657
+
658
+ def test_Q_subtree_pre_order(self, xp):
659
+ # Tests that pre_order() works when called on sub-trees.
660
+ X = xp.asarray(hierarchy_test_data.Q_X)
661
+ Z = linkage(X, 'single')
662
+ node = to_tree(Z)
663
+ assert_allclose(node.pre_order(), (node.get_left().pre_order()
664
+ + node.get_right().pre_order()),
665
+ rtol=1e-15)
666
+
667
+
668
+ @skip_xp_backends(cpu_only=True)
669
+ class TestCorrespond:
670
+
671
+ def test_correspond_empty(self, xp):
672
+ # Tests correspond(Z, y) with empty linkage and condensed distance matrix.
673
+ y = xp.zeros((0,), dtype=xp.float64)
674
+ Z = xp.zeros((0,4), dtype=xp.float64)
675
+ assert_raises(ValueError, correspond, Z, y)
676
+
677
+ def test_correspond_2_and_up(self, xp):
678
+ # Tests correspond(Z, y) on linkage and CDMs over observation sets of
679
+ # different sizes.
680
+ for i in range(2, 4):
681
+ y = np.random.rand(i*(i-1)//2)
682
+ y = xp.asarray(y)
683
+ Z = linkage(y)
684
+ assert_(correspond(Z, y))
685
+ for i in range(4, 15, 3):
686
+ y = np.random.rand(i*(i-1)//2)
687
+ y = xp.asarray(y)
688
+ Z = linkage(y)
689
+ assert_(correspond(Z, y))
690
+
691
+ def test_correspond_4_and_up(self, xp):
692
+ # Tests correspond(Z, y) on linkage and CDMs over observation sets of
693
+ # different sizes. Correspondence should be false.
694
+ for (i, j) in (list(zip(list(range(2, 4)), list(range(3, 5)))) +
695
+ list(zip(list(range(3, 5)), list(range(2, 4))))):
696
+ y = np.random.rand(i*(i-1)//2)
697
+ y2 = np.random.rand(j*(j-1)//2)
698
+ y = xp.asarray(y)
699
+ y2 = xp.asarray(y2)
700
+ Z = linkage(y)
701
+ Z2 = linkage(y2)
702
+ assert not correspond(Z, y2)
703
+ assert not correspond(Z2, y)
704
+
705
+ def test_correspond_4_and_up_2(self, xp):
706
+ # Tests correspond(Z, y) on linkage and CDMs over observation sets of
707
+ # different sizes. Correspondence should be false.
708
+ for (i, j) in (list(zip(list(range(2, 7)), list(range(16, 21)))) +
709
+ list(zip(list(range(2, 7)), list(range(16, 21))))):
710
+ y = np.random.rand(i*(i-1)//2)
711
+ y2 = np.random.rand(j*(j-1)//2)
712
+ y = xp.asarray(y)
713
+ y2 = xp.asarray(y2)
714
+ Z = linkage(y)
715
+ Z2 = linkage(y2)
716
+ assert not correspond(Z, y2)
717
+ assert not correspond(Z2, y)
718
+
719
+ def test_num_obs_linkage_multi_matrix(self, xp):
720
+ # Tests num_obs_linkage with observation matrices of multiple sizes.
721
+ for n in range(2, 10):
722
+ X = np.random.rand(n, 4)
723
+ Y = pdist(X)
724
+ Y = xp.asarray(Y)
725
+ Z = linkage(Y)
726
+ assert_equal(num_obs_linkage(Z), n)
727
+
728
+
729
+ @skip_xp_backends(cpu_only=True)
730
+ class TestIsMonotonic:
731
+
732
+ def test_is_monotonic_empty(self, xp):
733
+ # Tests is_monotonic(Z) on an empty linkage.
734
+ Z = xp.zeros((0, 4), dtype=xp.float64)
735
+ assert_raises(ValueError, is_monotonic, Z)
736
+
737
+ def test_is_monotonic_1x4(self, xp):
738
+ # Tests is_monotonic(Z) on 1x4 linkage. Expecting True.
739
+ Z = xp.asarray([[0, 1, 0.3, 2]], dtype=xp.float64)
740
+ assert is_monotonic(Z)
741
+
742
+ def test_is_monotonic_2x4_T(self, xp):
743
+ # Tests is_monotonic(Z) on 2x4 linkage. Expecting True.
744
+ Z = xp.asarray([[0, 1, 0.3, 2],
745
+ [2, 3, 0.4, 3]], dtype=xp.float64)
746
+ assert is_monotonic(Z)
747
+
748
+ def test_is_monotonic_2x4_F(self, xp):
749
+ # Tests is_monotonic(Z) on 2x4 linkage. Expecting False.
750
+ Z = xp.asarray([[0, 1, 0.4, 2],
751
+ [2, 3, 0.3, 3]], dtype=xp.float64)
752
+ assert not is_monotonic(Z)
753
+
754
+ def test_is_monotonic_3x4_T(self, xp):
755
+ # Tests is_monotonic(Z) on 3x4 linkage. Expecting True.
756
+ Z = xp.asarray([[0, 1, 0.3, 2],
757
+ [2, 3, 0.4, 2],
758
+ [4, 5, 0.6, 4]], dtype=xp.float64)
759
+ assert is_monotonic(Z)
760
+
761
+ def test_is_monotonic_3x4_F1(self, xp):
762
+ # Tests is_monotonic(Z) on 3x4 linkage (case 1). Expecting False.
763
+ Z = xp.asarray([[0, 1, 0.3, 2],
764
+ [2, 3, 0.2, 2],
765
+ [4, 5, 0.6, 4]], dtype=xp.float64)
766
+ assert not is_monotonic(Z)
767
+
768
+ def test_is_monotonic_3x4_F2(self, xp):
769
+ # Tests is_monotonic(Z) on 3x4 linkage (case 2). Expecting False.
770
+ Z = xp.asarray([[0, 1, 0.8, 2],
771
+ [2, 3, 0.4, 2],
772
+ [4, 5, 0.6, 4]], dtype=xp.float64)
773
+ assert not is_monotonic(Z)
774
+
775
+ def test_is_monotonic_3x4_F3(self, xp):
776
+ # Tests is_monotonic(Z) on 3x4 linkage (case 3). Expecting False
777
+ Z = xp.asarray([[0, 1, 0.3, 2],
778
+ [2, 3, 0.4, 2],
779
+ [4, 5, 0.2, 4]], dtype=xp.float64)
780
+ assert not is_monotonic(Z)
781
+
782
+ def test_is_monotonic_tdist_linkage1(self, xp):
783
+ # Tests is_monotonic(Z) on clustering generated by single linkage on
784
+ # tdist data set. Expecting True.
785
+ Z = linkage(xp.asarray(hierarchy_test_data.ytdist), 'single')
786
+ assert is_monotonic(Z)
787
+
788
+ @skip_xp_backends('jax.numpy', reason='jax arrays do not support item assignment')
789
+ def test_is_monotonic_tdist_linkage2(self, xp):
790
+ # Tests is_monotonic(Z) on clustering generated by single linkage on
791
+ # tdist data set. Perturbing. Expecting False.
792
+ Z = linkage(xp.asarray(hierarchy_test_data.ytdist), 'single')
793
+ Z[2,2] = 0.0
794
+ assert not is_monotonic(Z)
795
+
796
+ def test_is_monotonic_Q_linkage(self, xp):
797
+ # Tests is_monotonic(Z) on clustering generated by single linkage on
798
+ # Q data set. Expecting True.
799
+ X = xp.asarray(hierarchy_test_data.Q_X)
800
+ Z = linkage(X, 'single')
801
+ assert is_monotonic(Z)
802
+
803
+
804
+ @skip_xp_backends(cpu_only=True)
805
+ class TestMaxDists:
806
+
807
+ def test_maxdists_empty_linkage(self, xp):
808
+ # Tests maxdists(Z) on empty linkage. Expecting exception.
809
+ Z = xp.zeros((0, 4), dtype=xp.float64)
810
+ assert_raises(ValueError, maxdists, Z)
811
+
812
+ @skip_xp_backends('jax.numpy', reason='jax arrays do not support item assignment')
813
+ def test_maxdists_one_cluster_linkage(self, xp):
814
+ # Tests maxdists(Z) on linkage with one cluster.
815
+ Z = xp.asarray([[0, 1, 0.3, 4]], dtype=xp.float64)
816
+ MD = maxdists(Z)
817
+ expectedMD = calculate_maximum_distances(Z, xp)
818
+ xp_assert_close(MD, expectedMD, atol=1e-15)
819
+
820
+ @skip_xp_backends('jax.numpy', reason='jax arrays do not support item assignment')
821
+ def test_maxdists_Q_linkage(self, xp):
822
+ for method in ['single', 'complete', 'ward', 'centroid', 'median']:
823
+ self.check_maxdists_Q_linkage(method, xp)
824
+
825
+ def check_maxdists_Q_linkage(self, method, xp):
826
+ # Tests maxdists(Z) on the Q data set
827
+ X = xp.asarray(hierarchy_test_data.Q_X)
828
+ Z = linkage(X, method)
829
+ MD = maxdists(Z)
830
+ expectedMD = calculate_maximum_distances(Z, xp)
831
+ xp_assert_close(MD, expectedMD, atol=1e-15)
832
+
833
+
834
+ class TestMaxInconsts:
835
+
836
+ @skip_xp_backends(cpu_only=True)
837
+ def test_maxinconsts_empty_linkage(self, xp):
838
+ # Tests maxinconsts(Z, R) on empty linkage. Expecting exception.
839
+ Z = xp.zeros((0, 4), dtype=xp.float64)
840
+ R = xp.zeros((0, 4), dtype=xp.float64)
841
+ assert_raises(ValueError, maxinconsts, Z, R)
842
+
843
+ def test_maxinconsts_difrow_linkage(self, xp):
844
+ # Tests maxinconsts(Z, R) on linkage and inconsistency matrices with
845
+ # different numbers of clusters. Expecting exception.
846
+ Z = xp.asarray([[0, 1, 0.3, 4]], dtype=xp.float64)
847
+ R = np.random.rand(2, 4)
848
+ R = xp.asarray(R)
849
+ assert_raises(ValueError, maxinconsts, Z, R)
850
+
851
+ @skip_xp_backends('jax.numpy', reason='jax arrays do not support item assignment',
852
+ cpu_only=True)
853
+ def test_maxinconsts_one_cluster_linkage(self, xp):
854
+ # Tests maxinconsts(Z, R) on linkage with one cluster.
855
+ Z = xp.asarray([[0, 1, 0.3, 4]], dtype=xp.float64)
856
+ R = xp.asarray([[0, 0, 0, 0.3]], dtype=xp.float64)
857
+ MD = maxinconsts(Z, R)
858
+ expectedMD = calculate_maximum_inconsistencies(Z, R, xp=xp)
859
+ xp_assert_close(MD, expectedMD, atol=1e-15)
860
+
861
+ @skip_xp_backends('jax.numpy', reason='jax arrays do not support item assignment',
862
+ cpu_only=True)
863
+ def test_maxinconsts_Q_linkage(self, xp):
864
+ for method in ['single', 'complete', 'ward', 'centroid', 'median']:
865
+ self.check_maxinconsts_Q_linkage(method, xp)
866
+
867
+ def check_maxinconsts_Q_linkage(self, method, xp):
868
+ # Tests maxinconsts(Z, R) on the Q data set
869
+ X = xp.asarray(hierarchy_test_data.Q_X)
870
+ Z = linkage(X, method)
871
+ R = inconsistent(Z)
872
+ MD = maxinconsts(Z, R)
873
+ expectedMD = calculate_maximum_inconsistencies(Z, R, xp=xp)
874
+ xp_assert_close(MD, expectedMD, atol=1e-15)
875
+
876
+
877
+ class TestMaxRStat:
878
+
879
+ def test_maxRstat_invalid_index(self, xp):
880
+ for i in [3.3, -1, 4]:
881
+ self.check_maxRstat_invalid_index(i, xp)
882
+
883
+ def check_maxRstat_invalid_index(self, i, xp):
884
+ # Tests maxRstat(Z, R, i). Expecting exception.
885
+ Z = xp.asarray([[0, 1, 0.3, 4]], dtype=xp.float64)
886
+ R = xp.asarray([[0, 0, 0, 0.3]], dtype=xp.float64)
887
+ if isinstance(i, int):
888
+ assert_raises(ValueError, maxRstat, Z, R, i)
889
+ else:
890
+ assert_raises(TypeError, maxRstat, Z, R, i)
891
+
892
+ @skip_xp_backends(cpu_only=True)
893
+ def test_maxRstat_empty_linkage(self, xp):
894
+ for i in range(4):
895
+ self.check_maxRstat_empty_linkage(i, xp)
896
+
897
+ def check_maxRstat_empty_linkage(self, i, xp):
898
+ # Tests maxRstat(Z, R, i) on empty linkage. Expecting exception.
899
+ Z = xp.zeros((0, 4), dtype=xp.float64)
900
+ R = xp.zeros((0, 4), dtype=xp.float64)
901
+ assert_raises(ValueError, maxRstat, Z, R, i)
902
+
903
+ def test_maxRstat_difrow_linkage(self, xp):
904
+ for i in range(4):
905
+ self.check_maxRstat_difrow_linkage(i, xp)
906
+
907
+ def check_maxRstat_difrow_linkage(self, i, xp):
908
+ # Tests maxRstat(Z, R, i) on linkage and inconsistency matrices with
909
+ # different numbers of clusters. Expecting exception.
910
+ Z = xp.asarray([[0, 1, 0.3, 4]], dtype=xp.float64)
911
+ R = np.random.rand(2, 4)
912
+ R = xp.asarray(R)
913
+ assert_raises(ValueError, maxRstat, Z, R, i)
914
+
915
+ @skip_xp_backends('jax.numpy', reason='jax arrays do not support item assignment',
916
+ cpu_only=True)
917
+ def test_maxRstat_one_cluster_linkage(self, xp):
918
+ for i in range(4):
919
+ self.check_maxRstat_one_cluster_linkage(i, xp)
920
+
921
+ def check_maxRstat_one_cluster_linkage(self, i, xp):
922
+ # Tests maxRstat(Z, R, i) on linkage with one cluster.
923
+ Z = xp.asarray([[0, 1, 0.3, 4]], dtype=xp.float64)
924
+ R = xp.asarray([[0, 0, 0, 0.3]], dtype=xp.float64)
925
+ MD = maxRstat(Z, R, 1)
926
+ expectedMD = calculate_maximum_inconsistencies(Z, R, 1, xp)
927
+ xp_assert_close(MD, expectedMD, atol=1e-15)
928
+
929
+ @skip_xp_backends('jax.numpy', reason='jax arrays do not support item assignment',
930
+ cpu_only=True)
931
+ def test_maxRstat_Q_linkage(self, xp):
932
+ for method in ['single', 'complete', 'ward', 'centroid', 'median']:
933
+ for i in range(4):
934
+ self.check_maxRstat_Q_linkage(method, i, xp)
935
+
936
+ def check_maxRstat_Q_linkage(self, method, i, xp):
937
+ # Tests maxRstat(Z, R, i) on the Q data set
938
+ X = xp.asarray(hierarchy_test_data.Q_X)
939
+ Z = linkage(X, method)
940
+ R = inconsistent(Z)
941
+ MD = maxRstat(Z, R, 1)
942
+ expectedMD = calculate_maximum_inconsistencies(Z, R, 1, xp)
943
+ xp_assert_close(MD, expectedMD, atol=1e-15)
944
+
945
+
946
+ @skip_xp_backends(cpu_only=True)
947
+ class TestDendrogram:
948
+
949
+ def test_dendrogram_single_linkage_tdist(self, xp):
950
+ # Tests dendrogram calculation on single linkage of the tdist data set.
951
+ Z = linkage(xp.asarray(hierarchy_test_data.ytdist), 'single')
952
+ R = dendrogram(Z, no_plot=True)
953
+ leaves = R["leaves"]
954
+ assert_equal(leaves, [2, 5, 1, 0, 3, 4])
955
+
956
+ def test_valid_orientation(self, xp):
957
+ Z = linkage(xp.asarray(hierarchy_test_data.ytdist), 'single')
958
+ assert_raises(ValueError, dendrogram, Z, orientation="foo")
959
+
960
+ def test_labels_as_array_or_list(self, xp):
961
+ # test for gh-12418
962
+ Z = linkage(xp.asarray(hierarchy_test_data.ytdist), 'single')
963
+ labels = [1, 3, 2, 6, 4, 5]
964
+ result1 = dendrogram(Z, labels=xp.asarray(labels), no_plot=True)
965
+ result2 = dendrogram(Z, labels=labels, no_plot=True)
966
+ assert result1 == result2
967
+
968
+ @pytest.mark.skipif(not have_matplotlib, reason="no matplotlib")
969
+ def test_valid_label_size(self, xp):
970
+ link = xp.asarray([
971
+ [0, 1, 1.0, 4],
972
+ [2, 3, 1.0, 5],
973
+ [4, 5, 2.0, 6],
974
+ ])
975
+ plt.figure()
976
+ with pytest.raises(ValueError) as exc_info:
977
+ dendrogram(link, labels=list(range(100)))
978
+ assert "Dimensions of Z and labels must be consistent."\
979
+ in str(exc_info.value)
980
+
981
+ with pytest.raises(
982
+ ValueError,
983
+ match="Dimensions of Z and labels must be consistent."):
984
+ dendrogram(link, labels=[])
985
+
986
+ plt.close()
987
+
988
+ @skip_xp_backends('torch',
989
+ reason='MPL 3.9.2 & torch DeprecationWarning from __array_wrap__'
990
+ ' and NumPy 2.0'
991
+ )
992
+ @pytest.mark.skipif(not have_matplotlib, reason="no matplotlib")
993
+ def test_dendrogram_plot(self, xp):
994
+ for orientation in ['top', 'bottom', 'left', 'right']:
995
+ self.check_dendrogram_plot(orientation, xp)
996
+
997
+ def check_dendrogram_plot(self, orientation, xp):
998
+ # Tests dendrogram plotting.
999
+ Z = linkage(xp.asarray(hierarchy_test_data.ytdist), 'single')
1000
+ expected = {'color_list': ['C1', 'C0', 'C0', 'C0', 'C0'],
1001
+ 'dcoord': [[0.0, 138.0, 138.0, 0.0],
1002
+ [0.0, 219.0, 219.0, 0.0],
1003
+ [0.0, 255.0, 255.0, 219.0],
1004
+ [0.0, 268.0, 268.0, 255.0],
1005
+ [138.0, 295.0, 295.0, 268.0]],
1006
+ 'icoord': [[5.0, 5.0, 15.0, 15.0],
1007
+ [45.0, 45.0, 55.0, 55.0],
1008
+ [35.0, 35.0, 50.0, 50.0],
1009
+ [25.0, 25.0, 42.5, 42.5],
1010
+ [10.0, 10.0, 33.75, 33.75]],
1011
+ 'ivl': ['2', '5', '1', '0', '3', '4'],
1012
+ 'leaves': [2, 5, 1, 0, 3, 4],
1013
+ 'leaves_color_list': ['C1', 'C1', 'C0', 'C0', 'C0', 'C0'],
1014
+ }
1015
+
1016
+ fig = plt.figure()
1017
+ ax = fig.add_subplot(221)
1018
+
1019
+ # test that dendrogram accepts ax keyword
1020
+ R1 = dendrogram(Z, ax=ax, orientation=orientation)
1021
+ R1['dcoord'] = np.asarray(R1['dcoord'])
1022
+ assert_equal(R1, expected)
1023
+
1024
+ # test that dendrogram accepts and handle the leaf_font_size and
1025
+ # leaf_rotation keywords
1026
+ dendrogram(Z, ax=ax, orientation=orientation,
1027
+ leaf_font_size=20, leaf_rotation=90)
1028
+ testlabel = (
1029
+ ax.get_xticklabels()[0]
1030
+ if orientation in ['top', 'bottom']
1031
+ else ax.get_yticklabels()[0]
1032
+ )
1033
+ assert_equal(testlabel.get_rotation(), 90)
1034
+ assert_equal(testlabel.get_size(), 20)
1035
+ dendrogram(Z, ax=ax, orientation=orientation,
1036
+ leaf_rotation=90)
1037
+ testlabel = (
1038
+ ax.get_xticklabels()[0]
1039
+ if orientation in ['top', 'bottom']
1040
+ else ax.get_yticklabels()[0]
1041
+ )
1042
+ assert_equal(testlabel.get_rotation(), 90)
1043
+ dendrogram(Z, ax=ax, orientation=orientation,
1044
+ leaf_font_size=20)
1045
+ testlabel = (
1046
+ ax.get_xticklabels()[0]
1047
+ if orientation in ['top', 'bottom']
1048
+ else ax.get_yticklabels()[0]
1049
+ )
1050
+ assert_equal(testlabel.get_size(), 20)
1051
+ plt.close()
1052
+
1053
+ # test plotting to gca (will import pylab)
1054
+ R2 = dendrogram(Z, orientation=orientation)
1055
+ plt.close()
1056
+ R2['dcoord'] = np.asarray(R2['dcoord'])
1057
+ assert_equal(R2, expected)
1058
+
1059
+ @skip_xp_backends('torch',
1060
+ reason='MPL 3.9.2 & torch DeprecationWarning from __array_wrap__'
1061
+ ' and NumPy 2.0'
1062
+ )
1063
+ @pytest.mark.skipif(not have_matplotlib, reason="no matplotlib")
1064
+ def test_dendrogram_truncate_mode(self, xp):
1065
+ Z = linkage(xp.asarray(hierarchy_test_data.ytdist), 'single')
1066
+
1067
+ R = dendrogram(Z, 2, 'lastp', show_contracted=True)
1068
+ plt.close()
1069
+ R['dcoord'] = np.asarray(R['dcoord'])
1070
+ assert_equal(R, {'color_list': ['C0'],
1071
+ 'dcoord': [[0.0, 295.0, 295.0, 0.0]],
1072
+ 'icoord': [[5.0, 5.0, 15.0, 15.0]],
1073
+ 'ivl': ['(2)', '(4)'],
1074
+ 'leaves': [6, 9],
1075
+ 'leaves_color_list': ['C0', 'C0'],
1076
+ })
1077
+
1078
+ R = dendrogram(Z, 2, 'mtica', show_contracted=True)
1079
+ plt.close()
1080
+ R['dcoord'] = np.asarray(R['dcoord'])
1081
+ assert_equal(R, {'color_list': ['C1', 'C0', 'C0', 'C0'],
1082
+ 'dcoord': [[0.0, 138.0, 138.0, 0.0],
1083
+ [0.0, 255.0, 255.0, 0.0],
1084
+ [0.0, 268.0, 268.0, 255.0],
1085
+ [138.0, 295.0, 295.0, 268.0]],
1086
+ 'icoord': [[5.0, 5.0, 15.0, 15.0],
1087
+ [35.0, 35.0, 45.0, 45.0],
1088
+ [25.0, 25.0, 40.0, 40.0],
1089
+ [10.0, 10.0, 32.5, 32.5]],
1090
+ 'ivl': ['2', '5', '1', '0', '(2)'],
1091
+ 'leaves': [2, 5, 1, 0, 7],
1092
+ 'leaves_color_list': ['C1', 'C1', 'C0', 'C0', 'C0'],
1093
+ })
1094
+
1095
+ @pytest.fixture
1096
+ def dendrogram_lock(self):
1097
+ return Lock()
1098
+
1099
+ def test_dendrogram_colors(self, xp, dendrogram_lock):
1100
+ # Tests dendrogram plots with alternate colors
1101
+ Z = linkage(xp.asarray(hierarchy_test_data.ytdist), 'single')
1102
+
1103
+ with dendrogram_lock:
1104
+ # Global color palette might be changed concurrently
1105
+ set_link_color_palette(['c', 'm', 'y', 'k'])
1106
+ R = dendrogram(Z, no_plot=True,
1107
+ above_threshold_color='g', color_threshold=250)
1108
+ set_link_color_palette(['g', 'r', 'c', 'm', 'y', 'k'])
1109
+
1110
+ color_list = R['color_list']
1111
+ assert_equal(color_list, ['c', 'm', 'g', 'g', 'g'])
1112
+
1113
+ # reset color palette (global list)
1114
+ set_link_color_palette(None)
1115
+
1116
+ def test_dendrogram_leaf_colors_zero_dist(self, xp):
1117
+ # tests that the colors of leafs are correct for tree
1118
+ # with two identical points
1119
+ x = xp.asarray([[1, 0, 0],
1120
+ [0, 0, 1],
1121
+ [0, 2, 0],
1122
+ [0, 0, 1],
1123
+ [0, 1, 0],
1124
+ [0, 1, 0]])
1125
+ z = linkage(x, "single")
1126
+ d = dendrogram(z, no_plot=True)
1127
+ exp_colors = ['C0', 'C1', 'C1', 'C0', 'C2', 'C2']
1128
+ colors = d["leaves_color_list"]
1129
+ assert_equal(colors, exp_colors)
1130
+
1131
+ def test_dendrogram_leaf_colors(self, xp):
1132
+ # tests that the colors are correct for a tree
1133
+ # with two near points ((0, 0, 1.1) and (0, 0, 1))
1134
+ x = xp.asarray([[1, 0, 0],
1135
+ [0, 0, 1.1],
1136
+ [0, 2, 0],
1137
+ [0, 0, 1],
1138
+ [0, 1, 0],
1139
+ [0, 1, 0]])
1140
+ z = linkage(x, "single")
1141
+ d = dendrogram(z, no_plot=True)
1142
+ exp_colors = ['C0', 'C1', 'C1', 'C0', 'C2', 'C2']
1143
+ colors = d["leaves_color_list"]
1144
+ assert_equal(colors, exp_colors)
1145
+
1146
+
1147
+ def calculate_maximum_distances(Z, xp):
1148
+ # Used for testing correctness of maxdists.
1149
+ n = Z.shape[0] + 1
1150
+ B = xp.zeros((n-1,), dtype=Z.dtype)
1151
+ q = xp.zeros((3,))
1152
+ for i in range(0, n - 1):
1153
+ q[:] = 0.0
1154
+ left = Z[i, 0]
1155
+ right = Z[i, 1]
1156
+ if left >= n:
1157
+ q[0] = B[xp.asarray(left, dtype=xp.int64) - n]
1158
+ if right >= n:
1159
+ q[1] = B[xp.asarray(right, dtype=xp.int64) - n]
1160
+ q[2] = Z[i, 2]
1161
+ B[i] = xp.max(q)
1162
+ return B
1163
+
1164
+
1165
+ def calculate_maximum_inconsistencies(Z, R, k=3, xp=np):
1166
+ # Used for testing correctness of maxinconsts.
1167
+ n = Z.shape[0] + 1
1168
+ dtype = xp.result_type(Z, R)
1169
+ B = xp.zeros((n-1,), dtype=dtype)
1170
+ q = xp.zeros((3,))
1171
+ for i in range(0, n - 1):
1172
+ q[:] = 0.0
1173
+ left = Z[i, 0]
1174
+ right = Z[i, 1]
1175
+ if left >= n:
1176
+ q[0] = B[xp.asarray(left, dtype=xp.int64) - n]
1177
+ if right >= n:
1178
+ q[1] = B[xp.asarray(right, dtype=xp.int64) - n]
1179
+ q[2] = R[i, k]
1180
+ B[i] = xp.max(q)
1181
+ return B
1182
+
1183
+
1184
+ @pytest.mark.thread_unsafe
1185
+ @skip_xp_backends(cpu_only=True)
1186
+ def test_unsupported_uncondensed_distance_matrix_linkage_warning(xp):
1187
+ assert_warns(ClusterWarning, linkage, xp.asarray([[0, 1], [1, 0]]))
1188
+
1189
+
1190
+ def test_euclidean_linkage_value_error(xp):
1191
+ for method in scipy.cluster.hierarchy._EUCLIDEAN_METHODS:
1192
+ assert_raises(ValueError, linkage, xp.asarray([[1, 1], [1, 1]]),
1193
+ method=method, metric='cityblock')
1194
+
1195
+
1196
+ @skip_xp_backends(cpu_only=True)
1197
+ def test_2x2_linkage(xp):
1198
+ Z1 = linkage(xp.asarray([1]), method='single', metric='euclidean')
1199
+ Z2 = linkage(xp.asarray([[0, 1], [0, 0]]), method='single', metric='euclidean')
1200
+ xp_assert_close(Z1, Z2, rtol=1e-15)
1201
+
1202
+
1203
+ @skip_xp_backends(cpu_only=True)
1204
+ def test_node_compare(xp):
1205
+ np.random.seed(23)
1206
+ nobs = 50
1207
+ X = np.random.randn(nobs, 4)
1208
+ X = xp.asarray(X)
1209
+ Z = scipy.cluster.hierarchy.ward(X)
1210
+ tree = to_tree(Z)
1211
+ assert_(tree > tree.get_left())
1212
+ assert_(tree.get_right() > tree.get_left())
1213
+ assert_(tree.get_right() == tree.get_right())
1214
+ assert_(tree.get_right() != tree.get_left())
1215
+
1216
+
1217
+ @skip_xp_backends(np_only=True, reason='`cut_tree` uses non-standard indexing')
1218
+ def test_cut_tree(xp):
1219
+ np.random.seed(23)
1220
+ nobs = 50
1221
+ X = np.random.randn(nobs, 4)
1222
+ X = xp.asarray(X)
1223
+ Z = scipy.cluster.hierarchy.ward(X)
1224
+ cutree = cut_tree(Z)
1225
+
1226
+ # cutree.dtype varies between int32 and int64 over platforms
1227
+ xp_assert_close(cutree[:, 0], xp.arange(nobs), rtol=1e-15, check_dtype=False)
1228
+ xp_assert_close(cutree[:, -1], xp.zeros(nobs), rtol=1e-15, check_dtype=False)
1229
+ assert_equal(np.asarray(cutree).max(0), np.arange(nobs - 1, -1, -1))
1230
+
1231
+ xp_assert_close(cutree[:, [-5]], cut_tree(Z, n_clusters=5), rtol=1e-15)
1232
+ xp_assert_close(cutree[:, [-5, -10]], cut_tree(Z, n_clusters=[5, 10]), rtol=1e-15)
1233
+ xp_assert_close(cutree[:, [-10, -5]], cut_tree(Z, n_clusters=[10, 5]), rtol=1e-15)
1234
+
1235
+ nodes = _order_cluster_tree(Z)
1236
+ heights = xp.asarray([node.dist for node in nodes])
1237
+
1238
+ xp_assert_close(cutree[:, np.searchsorted(heights, [5])],
1239
+ cut_tree(Z, height=5), rtol=1e-15)
1240
+ xp_assert_close(cutree[:, np.searchsorted(heights, [5, 10])],
1241
+ cut_tree(Z, height=[5, 10]), rtol=1e-15)
1242
+ xp_assert_close(cutree[:, np.searchsorted(heights, [10, 5])],
1243
+ cut_tree(Z, height=[10, 5]), rtol=1e-15)
1244
+
1245
+
1246
+ @skip_xp_backends(cpu_only=True)
1247
+ def test_optimal_leaf_ordering(xp):
1248
+ # test with the distance vector y
1249
+ Z = optimal_leaf_ordering(linkage(xp.asarray(hierarchy_test_data.ytdist)),
1250
+ xp.asarray(hierarchy_test_data.ytdist))
1251
+ expectedZ = hierarchy_test_data.linkage_ytdist_single_olo
1252
+ xp_assert_close(Z, xp.asarray(expectedZ), atol=1e-10)
1253
+
1254
+ # test with the observation matrix X
1255
+ Z = optimal_leaf_ordering(linkage(xp.asarray(hierarchy_test_data.X), 'ward'),
1256
+ xp.asarray(hierarchy_test_data.X))
1257
+ expectedZ = hierarchy_test_data.linkage_X_ward_olo
1258
+ xp_assert_close(Z, xp.asarray(expectedZ), atol=1e-06)
1259
+
1260
+
1261
+ @skip_xp_backends(np_only=True, reason='`Heap` only supports NumPy backend')
1262
+ def test_Heap(xp):
1263
+ values = xp.asarray([2, -1, 0, -1.5, 3])
1264
+ heap = Heap(values)
1265
+
1266
+ pair = heap.get_min()
1267
+ assert_equal(pair['key'], 3)
1268
+ assert_equal(pair['value'], -1.5)
1269
+
1270
+ heap.remove_min()
1271
+ pair = heap.get_min()
1272
+ assert_equal(pair['key'], 1)
1273
+ assert_equal(pair['value'], -1)
1274
+
1275
+ heap.change_value(1, 2.5)
1276
+ pair = heap.get_min()
1277
+ assert_equal(pair['key'], 2)
1278
+ assert_equal(pair['value'], 0)
1279
+
1280
+ heap.remove_min()
1281
+ heap.remove_min()
1282
+
1283
+ heap.change_value(1, 10)
1284
+ pair = heap.get_min()
1285
+ assert_equal(pair['key'], 4)
1286
+ assert_equal(pair['value'], 3)
1287
+
1288
+ heap.remove_min()
1289
+ pair = heap.get_min()
1290
+ assert_equal(pair['key'], 1)
1291
+ assert_equal(pair['value'], 10)
1292
+
1293
+
1294
+ @skip_xp_backends(cpu_only=True)
1295
+ def test_centroid_neg_distance(xp):
1296
+ # gh-21011
1297
+ values = xp.asarray([0, 0, -1])
1298
+ with pytest.raises(ValueError):
1299
+ # This is just checking that this doesn't crash
1300
+ linkage(values, method='centroid')
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/tests/test_vq.py ADDED
@@ -0,0 +1,450 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import warnings
2
+ import sys
3
+ from copy import deepcopy
4
+ from threading import Lock
5
+
6
+ import numpy as np
7
+ from numpy.testing import (
8
+ assert_array_equal, assert_equal, assert_, suppress_warnings
9
+ )
10
+ import pytest
11
+ from pytest import raises as assert_raises
12
+
13
+ from scipy.cluster.vq import (kmeans, kmeans2, py_vq, vq, whiten,
14
+ ClusterError, _krandinit)
15
+ from scipy.cluster import _vq
16
+ from scipy.conftest import array_api_compatible
17
+ from scipy.sparse._sputils import matrix
18
+
19
+ from scipy._lib import array_api_extra as xpx
20
+ from scipy._lib._array_api import (
21
+ SCIPY_ARRAY_API, array_namespace, xp_copy, xp_assert_close, xp_assert_equal
22
+ )
23
+
24
+ pytestmark = [array_api_compatible, pytest.mark.usefixtures("skip_xp_backends")]
25
+ skip_xp_backends = pytest.mark.skip_xp_backends
26
+
27
+ TESTDATA_2D = np.array([
28
+ -2.2, 1.17, -1.63, 1.69, -2.04, 4.38, -3.09, 0.95, -1.7, 4.79, -1.68, 0.68,
29
+ -2.26, 3.34, -2.29, 2.55, -1.72, -0.72, -1.99, 2.34, -2.75, 3.43, -2.45,
30
+ 2.41, -4.26, 3.65, -1.57, 1.87, -1.96, 4.03, -3.01, 3.86, -2.53, 1.28,
31
+ -4.0, 3.95, -1.62, 1.25, -3.42, 3.17, -1.17, 0.12, -3.03, -0.27, -2.07,
32
+ -0.55, -1.17, 1.34, -2.82, 3.08, -2.44, 0.24, -1.71, 2.48, -5.23, 4.29,
33
+ -2.08, 3.69, -1.89, 3.62, -2.09, 0.26, -0.92, 1.07, -2.25, 0.88, -2.25,
34
+ 2.02, -4.31, 3.86, -2.03, 3.42, -2.76, 0.3, -2.48, -0.29, -3.42, 3.21,
35
+ -2.3, 1.73, -2.84, 0.69, -1.81, 2.48, -5.24, 4.52, -2.8, 1.31, -1.67,
36
+ -2.34, -1.18, 2.17, -2.17, 2.82, -1.85, 2.25, -2.45, 1.86, -6.79, 3.94,
37
+ -2.33, 1.89, -1.55, 2.08, -1.36, 0.93, -2.51, 2.74, -2.39, 3.92, -3.33,
38
+ 2.99, -2.06, -0.9, -2.83, 3.35, -2.59, 3.05, -2.36, 1.85, -1.69, 1.8,
39
+ -1.39, 0.66, -2.06, 0.38, -1.47, 0.44, -4.68, 3.77, -5.58, 3.44, -2.29,
40
+ 2.24, -1.04, -0.38, -1.85, 4.23, -2.88, 0.73, -2.59, 1.39, -1.34, 1.75,
41
+ -1.95, 1.3, -2.45, 3.09, -1.99, 3.41, -5.55, 5.21, -1.73, 2.52, -2.17,
42
+ 0.85, -2.06, 0.49, -2.54, 2.07, -2.03, 1.3, -3.23, 3.09, -1.55, 1.44,
43
+ -0.81, 1.1, -2.99, 2.92, -1.59, 2.18, -2.45, -0.73, -3.12, -1.3, -2.83,
44
+ 0.2, -2.77, 3.24, -1.98, 1.6, -4.59, 3.39, -4.85, 3.75, -2.25, 1.71, -3.28,
45
+ 3.38, -1.74, 0.88, -2.41, 1.92, -2.24, 1.19, -2.48, 1.06, -1.68, -0.62,
46
+ -1.3, 0.39, -1.78, 2.35, -3.54, 2.44, -1.32, 0.66, -2.38, 2.76, -2.35,
47
+ 3.95, -1.86, 4.32, -2.01, -1.23, -1.79, 2.76, -2.13, -0.13, -5.25, 3.84,
48
+ -2.24, 1.59, -4.85, 2.96, -2.41, 0.01, -0.43, 0.13, -3.92, 2.91, -1.75,
49
+ -0.53, -1.69, 1.69, -1.09, 0.15, -2.11, 2.17, -1.53, 1.22, -2.1, -0.86,
50
+ -2.56, 2.28, -3.02, 3.33, -1.12, 3.86, -2.18, -1.19, -3.03, 0.79, -0.83,
51
+ 0.97, -3.19, 1.45, -1.34, 1.28, -2.52, 4.22, -4.53, 3.22, -1.97, 1.75,
52
+ -2.36, 3.19, -0.83, 1.53, -1.59, 1.86, -2.17, 2.3, -1.63, 2.71, -2.03,
53
+ 3.75, -2.57, -0.6, -1.47, 1.33, -1.95, 0.7, -1.65, 1.27, -1.42, 1.09, -3.0,
54
+ 3.87, -2.51, 3.06, -2.6, 0.74, -1.08, -0.03, -2.44, 1.31, -2.65, 2.99,
55
+ -1.84, 1.65, -4.76, 3.75, -2.07, 3.98, -2.4, 2.67, -2.21, 1.49, -1.21,
56
+ 1.22, -5.29, 2.38, -2.85, 2.28, -5.6, 3.78, -2.7, 0.8, -1.81, 3.5, -3.75,
57
+ 4.17, -1.29, 2.99, -5.92, 3.43, -1.83, 1.23, -1.24, -1.04, -2.56, 2.37,
58
+ -3.26, 0.39, -4.63, 2.51, -4.52, 3.04, -1.7, 0.36, -1.41, 0.04, -2.1, 1.0,
59
+ -1.87, 3.78, -4.32, 3.59, -2.24, 1.38, -1.99, -0.22, -1.87, 1.95, -0.84,
60
+ 2.17, -5.38, 3.56, -1.27, 2.9, -1.79, 3.31, -5.47, 3.85, -1.44, 3.69,
61
+ -2.02, 0.37, -1.29, 0.33, -2.34, 2.56, -1.74, -1.27, -1.97, 1.22, -2.51,
62
+ -0.16, -1.64, -0.96, -2.99, 1.4, -1.53, 3.31, -2.24, 0.45, -2.46, 1.71,
63
+ -2.88, 1.56, -1.63, 1.46, -1.41, 0.68, -1.96, 2.76, -1.61,
64
+ 2.11]).reshape((200, 2))
65
+
66
+
67
+ # Global data
68
+ X = np.array([[3.0, 3], [4, 3], [4, 2],
69
+ [9, 2], [5, 1], [6, 2], [9, 4],
70
+ [5, 2], [5, 4], [7, 4], [6, 5]])
71
+
72
+ CODET1 = np.array([[3.0000, 3.0000],
73
+ [6.2000, 4.0000],
74
+ [5.8000, 1.8000]])
75
+
76
+ CODET2 = np.array([[11.0/3, 8.0/3],
77
+ [6.7500, 4.2500],
78
+ [6.2500, 1.7500]])
79
+
80
+ LABEL1 = np.array([0, 1, 2, 2, 2, 2, 1, 2, 1, 1, 1])
81
+
82
+
83
+ class TestWhiten:
84
+
85
+ def test_whiten(self, xp):
86
+ desired = xp.asarray([[5.08738849, 2.97091878],
87
+ [3.19909255, 0.69660580],
88
+ [4.51041982, 0.02640918],
89
+ [4.38567074, 0.95120889],
90
+ [2.32191480, 1.63195503]])
91
+
92
+ obs = xp.asarray([[0.98744510, 0.82766775],
93
+ [0.62093317, 0.19406729],
94
+ [0.87545741, 0.00735733],
95
+ [0.85124403, 0.26499712],
96
+ [0.45067590, 0.45464607]])
97
+ xp_assert_close(whiten(obs), desired, rtol=1e-5)
98
+
99
+ @pytest.fixture
100
+ def whiten_lock(self):
101
+ return Lock()
102
+
103
+ @skip_xp_backends('jax.numpy',
104
+ reason='jax arrays do not support item assignment')
105
+ def test_whiten_zero_std(self, xp, whiten_lock):
106
+ desired = xp.asarray([[0., 1.0, 2.86666544],
107
+ [0., 1.0, 1.32460034],
108
+ [0., 1.0, 3.74382172]])
109
+
110
+ obs = xp.asarray([[0., 1., 0.74109533],
111
+ [0., 1., 0.34243798],
112
+ [0., 1., 0.96785929]])
113
+
114
+ with whiten_lock:
115
+ with warnings.catch_warnings(record=True) as w:
116
+ warnings.simplefilter('always')
117
+
118
+ xp_assert_close(whiten(obs), desired, rtol=1e-5)
119
+
120
+ assert_equal(len(w), 1)
121
+ assert_(issubclass(w[-1].category, RuntimeWarning))
122
+
123
+ def test_whiten_not_finite(self, xp):
124
+ for bad_value in xp.nan, xp.inf, -xp.inf:
125
+ obs = xp.asarray([[0.98744510, bad_value],
126
+ [0.62093317, 0.19406729],
127
+ [0.87545741, 0.00735733],
128
+ [0.85124403, 0.26499712],
129
+ [0.45067590, 0.45464607]])
130
+ assert_raises(ValueError, whiten, obs)
131
+
132
+ @pytest.mark.skipif(SCIPY_ARRAY_API,
133
+ reason='`np.matrix` unsupported in array API mode')
134
+ def test_whiten_not_finite_matrix(self, xp):
135
+ for bad_value in np.nan, np.inf, -np.inf:
136
+ obs = matrix([[0.98744510, bad_value],
137
+ [0.62093317, 0.19406729],
138
+ [0.87545741, 0.00735733],
139
+ [0.85124403, 0.26499712],
140
+ [0.45067590, 0.45464607]])
141
+ assert_raises(ValueError, whiten, obs)
142
+
143
+
144
+ class TestVq:
145
+
146
+ @skip_xp_backends(cpu_only=True)
147
+ def test_py_vq(self, xp):
148
+ initc = np.concatenate([[X[0]], [X[1]], [X[2]]])
149
+ # label1.dtype varies between int32 and int64 over platforms
150
+ label1 = py_vq(xp.asarray(X), xp.asarray(initc))[0]
151
+ xp_assert_equal(label1, xp.asarray(LABEL1, dtype=xp.int64),
152
+ check_dtype=False)
153
+
154
+ @pytest.mark.skipif(SCIPY_ARRAY_API,
155
+ reason='`np.matrix` unsupported in array API mode')
156
+ def test_py_vq_matrix(self, xp):
157
+ initc = np.concatenate([[X[0]], [X[1]], [X[2]]])
158
+ # label1.dtype varies between int32 and int64 over platforms
159
+ label1 = py_vq(matrix(X), matrix(initc))[0]
160
+ assert_array_equal(label1, LABEL1)
161
+
162
+ @skip_xp_backends(np_only=True, reason='`_vq` only supports NumPy backend')
163
+ def test_vq(self, xp):
164
+ initc = np.concatenate([[X[0]], [X[1]], [X[2]]])
165
+ label1, _ = _vq.vq(xp.asarray(X), xp.asarray(initc))
166
+ assert_array_equal(label1, LABEL1)
167
+ _, _ = vq(xp.asarray(X), xp.asarray(initc))
168
+
169
+ @pytest.mark.skipif(SCIPY_ARRAY_API,
170
+ reason='`np.matrix` unsupported in array API mode')
171
+ def test_vq_matrix(self, xp):
172
+ initc = np.concatenate([[X[0]], [X[1]], [X[2]]])
173
+ label1, _ = _vq.vq(matrix(X), matrix(initc))
174
+ assert_array_equal(label1, LABEL1)
175
+ _, _ = vq(matrix(X), matrix(initc))
176
+
177
+ @skip_xp_backends(cpu_only=True)
178
+ def test_vq_1d(self, xp):
179
+ # Test special rank 1 vq algo, python implementation.
180
+ data = X[:, 0]
181
+ initc = data[:3]
182
+ a, b = _vq.vq(data, initc)
183
+ data = xp.asarray(data)
184
+ initc = xp.asarray(initc)
185
+ ta, tb = py_vq(data[:, np.newaxis], initc[:, np.newaxis])
186
+ # ta.dtype varies between int32 and int64 over platforms
187
+ xp_assert_equal(ta, xp.asarray(a, dtype=xp.int64), check_dtype=False)
188
+ xp_assert_equal(tb, xp.asarray(b))
189
+
190
+ @skip_xp_backends(np_only=True, reason='`_vq` only supports NumPy backend')
191
+ def test__vq_sametype(self, xp):
192
+ a = xp.asarray([1.0, 2.0], dtype=xp.float64)
193
+ b = a.astype(xp.float32)
194
+ assert_raises(TypeError, _vq.vq, a, b)
195
+
196
+ @skip_xp_backends(np_only=True, reason='`_vq` only supports NumPy backend')
197
+ def test__vq_invalid_type(self, xp):
198
+ a = xp.asarray([1, 2], dtype=int)
199
+ assert_raises(TypeError, _vq.vq, a, a)
200
+
201
+ @skip_xp_backends(cpu_only=True)
202
+ def test_vq_large_nfeat(self, xp):
203
+ X = np.random.rand(20, 20)
204
+ code_book = np.random.rand(3, 20)
205
+
206
+ codes0, dis0 = _vq.vq(X, code_book)
207
+ codes1, dis1 = py_vq(
208
+ xp.asarray(X), xp.asarray(code_book)
209
+ )
210
+ xp_assert_close(dis1, xp.asarray(dis0), rtol=1e-5)
211
+ # codes1.dtype varies between int32 and int64 over platforms
212
+ xp_assert_equal(codes1, xp.asarray(codes0, dtype=xp.int64), check_dtype=False)
213
+
214
+ X = X.astype(np.float32)
215
+ code_book = code_book.astype(np.float32)
216
+
217
+ codes0, dis0 = _vq.vq(X, code_book)
218
+ codes1, dis1 = py_vq(
219
+ xp.asarray(X), xp.asarray(code_book)
220
+ )
221
+ xp_assert_close(dis1, xp.asarray(dis0, dtype=xp.float64), rtol=1e-5)
222
+ # codes1.dtype varies between int32 and int64 over platforms
223
+ xp_assert_equal(codes1, xp.asarray(codes0, dtype=xp.int64), check_dtype=False)
224
+
225
+ @skip_xp_backends(cpu_only=True)
226
+ def test_vq_large_features(self, xp):
227
+ X = np.random.rand(10, 5) * 1000000
228
+ code_book = np.random.rand(2, 5) * 1000000
229
+
230
+ codes0, dis0 = _vq.vq(X, code_book)
231
+ codes1, dis1 = py_vq(
232
+ xp.asarray(X), xp.asarray(code_book)
233
+ )
234
+ xp_assert_close(dis1, xp.asarray(dis0), rtol=1e-5)
235
+ # codes1.dtype varies between int32 and int64 over platforms
236
+ xp_assert_equal(codes1, xp.asarray(codes0, dtype=xp.int64), check_dtype=False)
237
+
238
+
239
+ # Whole class skipped on GPU for now;
240
+ # once pdist/cdist are hooked up for CuPy, more tests will work
241
+ @skip_xp_backends(cpu_only=True)
242
+ class TestKMean:
243
+
244
+ def test_large_features(self, xp):
245
+ # Generate a data set with large values, and run kmeans on it to
246
+ # (regression for 1077).
247
+ d = 300
248
+ n = 100
249
+
250
+ m1 = np.random.randn(d)
251
+ m2 = np.random.randn(d)
252
+ x = 10000 * np.random.randn(n, d) - 20000 * m1
253
+ y = 10000 * np.random.randn(n, d) + 20000 * m2
254
+
255
+ data = np.empty((x.shape[0] + y.shape[0], d), np.float64)
256
+ data[:x.shape[0]] = x
257
+ data[x.shape[0]:] = y
258
+
259
+ # use `seed` to ensure backwards compatibility after SPEC7
260
+ kmeans(xp.asarray(data), 2, seed=1)
261
+
262
+ def test_kmeans_simple(self, xp):
263
+ rng = np.random.default_rng(54321)
264
+ initc = np.concatenate([[X[0]], [X[1]], [X[2]]])
265
+ code1 = kmeans(xp.asarray(X), xp.asarray(initc), iter=1, rng=rng)[0]
266
+ xp_assert_close(code1, xp.asarray(CODET2))
267
+
268
+ @pytest.mark.skipif(SCIPY_ARRAY_API,
269
+ reason='`np.matrix` unsupported in array API mode')
270
+ def test_kmeans_simple_matrix(self, xp):
271
+ rng = np.random.default_rng(54321)
272
+ initc = np.concatenate([[X[0]], [X[1]], [X[2]]])
273
+ code1 = kmeans(matrix(X), matrix(initc), iter=1, rng=rng)[0]
274
+ xp_assert_close(code1, CODET2)
275
+
276
+ def test_kmeans_lost_cluster(self, xp):
277
+ # This will cause kmeans to have a cluster with no points.
278
+ data = xp.asarray(TESTDATA_2D)
279
+ initk = xp.asarray([[-1.8127404, -0.67128041],
280
+ [2.04621601, 0.07401111],
281
+ [-2.31149087, -0.05160469]])
282
+
283
+ kmeans(data, initk)
284
+ with suppress_warnings() as sup:
285
+ sup.filter(UserWarning,
286
+ "One of the clusters is empty. Re-run kmeans with a "
287
+ "different initialization")
288
+ kmeans2(data, initk, missing='warn')
289
+
290
+ assert_raises(ClusterError, kmeans2, data, initk, missing='raise')
291
+
292
+ def test_kmeans2_simple(self, xp):
293
+ rng = np.random.default_rng(12345678)
294
+ initc = xp.asarray(np.concatenate([[X[0]], [X[1]], [X[2]]]))
295
+ arrays = [xp.asarray] if SCIPY_ARRAY_API else [np.asarray, matrix]
296
+ for tp in arrays:
297
+ code1 = kmeans2(tp(X), tp(initc), iter=1, rng=rng)[0]
298
+ code2 = kmeans2(tp(X), tp(initc), iter=2, rng=rng)[0]
299
+
300
+ xp_assert_close(code1, xp.asarray(CODET1))
301
+ xp_assert_close(code2, xp.asarray(CODET2))
302
+
303
+ @pytest.mark.skipif(SCIPY_ARRAY_API,
304
+ reason='`np.matrix` unsupported in array API mode')
305
+ def test_kmeans2_simple_matrix(self, xp):
306
+ rng = np.random.default_rng(12345678)
307
+ initc = xp.asarray(np.concatenate([[X[0]], [X[1]], [X[2]]]))
308
+ code1 = kmeans2(matrix(X), matrix(initc), iter=1, rng=rng)[0]
309
+ code2 = kmeans2(matrix(X), matrix(initc), iter=2, rng=rng)[0]
310
+
311
+ xp_assert_close(code1, CODET1)
312
+ xp_assert_close(code2, CODET2)
313
+
314
+ def test_kmeans2_rank1(self, xp):
315
+ data = xp.asarray(TESTDATA_2D)
316
+ data1 = data[:, 0]
317
+
318
+ initc = data1[:3]
319
+ code = xp_copy(initc, xp=xp)
320
+
321
+ # use `seed` to ensure backwards compatibility after SPEC7
322
+ kmeans2(data1, code, iter=1, seed=1)[0]
323
+ kmeans2(data1, code, iter=2)[0]
324
+
325
+ def test_kmeans2_rank1_2(self, xp):
326
+ data = xp.asarray(TESTDATA_2D)
327
+ data1 = data[:, 0]
328
+ kmeans2(data1, 2, iter=1)
329
+
330
+ def test_kmeans2_high_dim(self, xp):
331
+ # test kmeans2 when the number of dimensions exceeds the number
332
+ # of input points
333
+ data = xp.asarray(TESTDATA_2D)
334
+ data = xp.reshape(data, (20, 20))[:10, :]
335
+ kmeans2(data, 2)
336
+
337
+ @skip_xp_backends('jax.numpy',
338
+ reason='jax arrays do not support item assignment')
339
+ def test_kmeans2_init(self, xp):
340
+ rng = np.random.default_rng(12345678)
341
+ data = xp.asarray(TESTDATA_2D)
342
+ k = 3
343
+
344
+ kmeans2(data, k, minit='points', rng=rng)
345
+ kmeans2(data[:, 1], k, minit='points', rng=rng) # special case (1-D)
346
+
347
+ kmeans2(data, k, minit='++', rng=rng)
348
+ kmeans2(data[:, 1], k, minit='++', rng=rng) # special case (1-D)
349
+
350
+ # minit='random' can give warnings, filter those
351
+ with suppress_warnings() as sup:
352
+ sup.filter(message="One of the clusters is empty. Re-run.")
353
+ kmeans2(data, k, minit='random', rng=rng)
354
+ kmeans2(data[:, 1], k, minit='random', rng=rng) # special case (1-D)
355
+
356
+ @pytest.fixture
357
+ def krand_lock(self):
358
+ return Lock()
359
+
360
+ @pytest.mark.skipif(sys.platform == 'win32',
361
+ reason='Fails with MemoryError in Wine.')
362
+ def test_krandinit(self, xp, krand_lock):
363
+ data = xp.asarray(TESTDATA_2D)
364
+ datas = [xp.reshape(data, (200, 2)),
365
+ xp.reshape(data, (20, 20))[:10, :]]
366
+ k = int(1e6)
367
+ xp_test = array_namespace(data)
368
+ with krand_lock:
369
+ for data in datas:
370
+ rng = np.random.default_rng(1234)
371
+ init = _krandinit(data, k, rng, xp_test)
372
+ orig_cov = xpx.cov(data.T, xp=xp_test)
373
+ init_cov = xpx.cov(init.T, xp=xp_test)
374
+ xp_assert_close(orig_cov, init_cov, atol=1.1e-2)
375
+
376
+ def test_kmeans2_empty(self, xp):
377
+ # Regression test for gh-1032.
378
+ assert_raises(ValueError, kmeans2, xp.asarray([]), 2)
379
+
380
+ def test_kmeans_0k(self, xp):
381
+ # Regression test for gh-1073: fail when k arg is 0.
382
+ assert_raises(ValueError, kmeans, xp.asarray(X), 0)
383
+ assert_raises(ValueError, kmeans2, xp.asarray(X), 0)
384
+ assert_raises(ValueError, kmeans2, xp.asarray(X), xp.asarray([]))
385
+
386
+ def test_kmeans_large_thres(self, xp):
387
+ # Regression test for gh-1774
388
+ x = xp.asarray([1, 2, 3, 4, 10], dtype=xp.float64)
389
+ res = kmeans(x, 1, thresh=1e16)
390
+ xp_assert_close(res[0], xp.asarray([4.], dtype=xp.float64))
391
+ xp_assert_close(res[1], xp.asarray(2.3999999999999999, dtype=xp.float64)[()])
392
+
393
+ @skip_xp_backends('jax.numpy',
394
+ reason='jax arrays do not support item assignment')
395
+ def test_kmeans2_kpp_low_dim(self, xp):
396
+ # Regression test for gh-11462
397
+ rng = np.random.default_rng(2358792345678234568)
398
+ prev_res = xp.asarray([[-1.95266667, 0.898],
399
+ [-3.153375, 3.3945]], dtype=xp.float64)
400
+ res, _ = kmeans2(xp.asarray(TESTDATA_2D), 2, minit='++', rng=rng)
401
+ xp_assert_close(res, prev_res)
402
+
403
+ @pytest.mark.thread_unsafe
404
+ @skip_xp_backends('jax.numpy',
405
+ reason='jax arrays do not support item assignment')
406
+ def test_kmeans2_kpp_high_dim(self, xp):
407
+ # Regression test for gh-11462
408
+ rng = np.random.default_rng(23587923456834568)
409
+ n_dim = 100
410
+ size = 10
411
+ centers = np.vstack([5 * np.ones(n_dim),
412
+ -5 * np.ones(n_dim)])
413
+
414
+ data = np.vstack([
415
+ rng.multivariate_normal(centers[0], np.eye(n_dim), size=size),
416
+ rng.multivariate_normal(centers[1], np.eye(n_dim), size=size)
417
+ ])
418
+
419
+ data = xp.asarray(data)
420
+ res, _ = kmeans2(data, 2, minit='++', rng=rng)
421
+ xp_assert_equal(xp.sign(res), xp.sign(xp.asarray(centers)))
422
+
423
+ def test_kmeans_diff_convergence(self, xp):
424
+ # Regression test for gh-8727
425
+ obs = xp.asarray([-3, -1, 0, 1, 1, 8], dtype=xp.float64)
426
+ res = kmeans(obs, xp.asarray([-3., 0.99]))
427
+ xp_assert_close(res[0], xp.asarray([-0.4, 8.], dtype=xp.float64))
428
+ xp_assert_close(res[1], xp.asarray(1.0666666666666667, dtype=xp.float64)[()])
429
+
430
+ @skip_xp_backends('jax.numpy',
431
+ reason='jax arrays do not support item assignment')
432
+ def test_kmeans_and_kmeans2_random_seed(self, xp):
433
+
434
+ seed_list = [
435
+ 1234, np.random.RandomState(1234), np.random.default_rng(1234)
436
+ ]
437
+
438
+ for seed in seed_list:
439
+ seed1 = deepcopy(seed)
440
+ seed2 = deepcopy(seed)
441
+ data = xp.asarray(TESTDATA_2D)
442
+ # test for kmeans
443
+ res1, _ = kmeans(data, 2, seed=seed1)
444
+ res2, _ = kmeans(data, 2, seed=seed2)
445
+ xp_assert_close(res1, res2) # should be same results
446
+ # test for kmeans2
447
+ for minit in ["random", "points", "++"]:
448
+ res1, _ = kmeans2(data, 2, minit=minit, seed=seed1)
449
+ res2, _ = kmeans2(data, 2, minit=minit, seed=seed2)
450
+ xp_assert_close(res1, res2) # should be same results
miniconda3/envs/ladir/lib/python3.10/site-packages/scipy/cluster/vq.py ADDED
@@ -0,0 +1,828 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ K-means clustering and vector quantization (:mod:`scipy.cluster.vq`)
3
+ ====================================================================
4
+
5
+ Provides routines for k-means clustering, generating code books
6
+ from k-means models and quantizing vectors by comparing them with
7
+ centroids in a code book.
8
+
9
+ .. autosummary::
10
+ :toctree: generated/
11
+
12
+ whiten -- Normalize a group of observations so each feature has unit variance
13
+ vq -- Calculate code book membership of a set of observation vectors
14
+ kmeans -- Perform k-means on a set of observation vectors forming k clusters
15
+ kmeans2 -- A different implementation of k-means with more methods
16
+ -- for initializing centroids
17
+
18
+ Background information
19
+ ----------------------
20
+ The k-means algorithm takes as input the number of clusters to
21
+ generate, k, and a set of observation vectors to cluster. It
22
+ returns a set of centroids, one for each of the k clusters. An
23
+ observation vector is classified with the cluster number or
24
+ centroid index of the centroid closest to it.
25
+
26
+ A vector v belongs to cluster i if it is closer to centroid i than
27
+ any other centroid. If v belongs to i, we say centroid i is the
28
+ dominating centroid of v. The k-means algorithm tries to
29
+ minimize distortion, which is defined as the sum of the squared distances
30
+ between each observation vector and its dominating centroid.
31
+ The minimization is achieved by iteratively reclassifying
32
+ the observations into clusters and recalculating the centroids until
33
+ a configuration is reached in which the centroids are stable. One can
34
+ also define a maximum number of iterations.
35
+
36
+ Since vector quantization is a natural application for k-means,
37
+ information theory terminology is often used. The centroid index
38
+ or cluster index is also referred to as a "code" and the table
39
+ mapping codes to centroids and, vice versa, is often referred to as a
40
+ "code book". The result of k-means, a set of centroids, can be
41
+ used to quantize vectors. Quantization aims to find an encoding of
42
+ vectors that reduces the expected distortion.
43
+
44
+ All routines expect obs to be an M by N array, where the rows are
45
+ the observation vectors. The codebook is a k by N array, where the
46
+ ith row is the centroid of code word i. The observation vectors
47
+ and centroids have the same feature dimension.
48
+
49
+ As an example, suppose we wish to compress a 24-bit color image
50
+ (each pixel is represented by one byte for red, one for blue, and
51
+ one for green) before sending it over the web. By using a smaller
52
+ 8-bit encoding, we can reduce the amount of data by two
53
+ thirds. Ideally, the colors for each of the 256 possible 8-bit
54
+ encoding values should be chosen to minimize distortion of the
55
+ color. Running k-means with k=256 generates a code book of 256
56
+ codes, which fills up all possible 8-bit sequences. Instead of
57
+ sending a 3-byte value for each pixel, the 8-bit centroid index
58
+ (or code word) of the dominating centroid is transmitted. The code
59
+ book is also sent over the wire so each 8-bit code can be
60
+ translated back to a 24-bit pixel value representation. If the
61
+ image of interest was of an ocean, we would expect many 24-bit
62
+ blues to be represented by 8-bit codes. If it was an image of a
63
+ human face, more flesh-tone colors would be represented in the
64
+ code book.
65
+
66
+ """
67
+ import warnings
68
+ import numpy as np
69
+ from collections import deque
70
+ from scipy._lib._array_api import (
71
+ _asarray, array_namespace, xp_size, xp_copy
72
+ )
73
+ from scipy._lib._util import (check_random_state, rng_integers,
74
+ _transition_to_rng)
75
+ from scipy._lib import array_api_extra as xpx
76
+ from scipy.spatial.distance import cdist
77
+
78
+ from . import _vq
79
+
80
+ __docformat__ = 'restructuredtext'
81
+
82
+ __all__ = ['whiten', 'vq', 'kmeans', 'kmeans2']
83
+
84
+
85
+ class ClusterError(Exception):
86
+ pass
87
+
88
+
89
+ def whiten(obs, check_finite=True):
90
+ """
91
+ Normalize a group of observations on a per feature basis.
92
+
93
+ Before running k-means, it is beneficial to rescale each feature
94
+ dimension of the observation set by its standard deviation (i.e. "whiten"
95
+ it - as in "white noise" where each frequency has equal power).
96
+ Each feature is divided by its standard deviation across all observations
97
+ to give it unit variance.
98
+
99
+ Parameters
100
+ ----------
101
+ obs : ndarray
102
+ Each row of the array is an observation. The
103
+ columns are the features seen during each observation.
104
+
105
+ >>> # f0 f1 f2
106
+ >>> obs = [[ 1., 1., 1.], #o0
107
+ ... [ 2., 2., 2.], #o1
108
+ ... [ 3., 3., 3.], #o2
109
+ ... [ 4., 4., 4.]] #o3
110
+
111
+ check_finite : bool, optional
112
+ Whether to check that the input matrices contain only finite numbers.
113
+ Disabling may give a performance gain, but may result in problems
114
+ (crashes, non-termination) if the inputs do contain infinities or NaNs.
115
+ Default: True
116
+
117
+ Returns
118
+ -------
119
+ result : ndarray
120
+ Contains the values in `obs` scaled by the standard deviation
121
+ of each column.
122
+
123
+ Examples
124
+ --------
125
+ >>> import numpy as np
126
+ >>> from scipy.cluster.vq import whiten
127
+ >>> features = np.array([[1.9, 2.3, 1.7],
128
+ ... [1.5, 2.5, 2.2],
129
+ ... [0.8, 0.6, 1.7,]])
130
+ >>> whiten(features)
131
+ array([[ 4.17944278, 2.69811351, 7.21248917],
132
+ [ 3.29956009, 2.93273208, 9.33380951],
133
+ [ 1.75976538, 0.7038557 , 7.21248917]])
134
+
135
+ """
136
+ xp = array_namespace(obs)
137
+ obs = _asarray(obs, check_finite=check_finite, xp=xp)
138
+ std_dev = xp.std(obs, axis=0)
139
+ zero_std_mask = std_dev == 0
140
+ if xp.any(zero_std_mask):
141
+ std_dev[zero_std_mask] = 1.0
142
+ warnings.warn("Some columns have standard deviation zero. "
143
+ "The values of these columns will not change.",
144
+ RuntimeWarning, stacklevel=2)
145
+ return obs / std_dev
146
+
147
+
148
+ def vq(obs, code_book, check_finite=True):
149
+ """
150
+ Assign codes from a code book to observations.
151
+
152
+ Assigns a code from a code book to each observation. Each
153
+ observation vector in the 'M' by 'N' `obs` array is compared with the
154
+ centroids in the code book and assigned the code of the closest
155
+ centroid.
156
+
157
+ The features in `obs` should have unit variance, which can be
158
+ achieved by passing them through the whiten function. The code
159
+ book can be created with the k-means algorithm or a different
160
+ encoding algorithm.
161
+
162
+ Parameters
163
+ ----------
164
+ obs : ndarray
165
+ Each row of the 'M' x 'N' array is an observation. The columns are
166
+ the "features" seen during each observation. The features must be
167
+ whitened first using the whiten function or something equivalent.
168
+ code_book : ndarray
169
+ The code book is usually generated using the k-means algorithm.
170
+ Each row of the array holds a different code, and the columns are
171
+ the features of the code.
172
+
173
+ >>> # f0 f1 f2 f3
174
+ >>> code_book = [
175
+ ... [ 1., 2., 3., 4.], #c0
176
+ ... [ 1., 2., 3., 4.], #c1
177
+ ... [ 1., 2., 3., 4.]] #c2
178
+
179
+ check_finite : bool, optional
180
+ Whether to check that the input matrices contain only finite numbers.
181
+ Disabling may give a performance gain, but may result in problems
182
+ (crashes, non-termination) if the inputs do contain infinities or NaNs.
183
+ Default: True
184
+
185
+ Returns
186
+ -------
187
+ code : ndarray
188
+ A length M array holding the code book index for each observation.
189
+ dist : ndarray
190
+ The distortion (distance) between the observation and its nearest
191
+ code.
192
+
193
+ Examples
194
+ --------
195
+ >>> import numpy as np
196
+ >>> from scipy.cluster.vq import vq
197
+ >>> code_book = np.array([[1., 1., 1.],
198
+ ... [2., 2., 2.]])
199
+ >>> features = np.array([[1.9, 2.3, 1.7],
200
+ ... [1.5, 2.5, 2.2],
201
+ ... [0.8, 0.6, 1.7]])
202
+ >>> vq(features, code_book)
203
+ (array([1, 1, 0], dtype=int32), array([0.43588989, 0.73484692, 0.83066239]))
204
+
205
+ """
206
+ xp = array_namespace(obs, code_book)
207
+ obs = _asarray(obs, xp=xp, check_finite=check_finite)
208
+ code_book = _asarray(code_book, xp=xp, check_finite=check_finite)
209
+ ct = xp.result_type(obs, code_book)
210
+
211
+ c_obs = xp.astype(obs, ct, copy=False)
212
+ c_code_book = xp.astype(code_book, ct, copy=False)
213
+
214
+ if xp.isdtype(ct, kind='real floating'):
215
+ c_obs = np.asarray(c_obs)
216
+ c_code_book = np.asarray(c_code_book)
217
+ result = _vq.vq(c_obs, c_code_book)
218
+ return xp.asarray(result[0]), xp.asarray(result[1])
219
+ return py_vq(obs, code_book, check_finite=False)
220
+
221
+
222
+ def py_vq(obs, code_book, check_finite=True):
223
+ """ Python version of vq algorithm.
224
+
225
+ The algorithm computes the Euclidean distance between each
226
+ observation and every frame in the code_book.
227
+
228
+ Parameters
229
+ ----------
230
+ obs : ndarray
231
+ Expects a rank 2 array. Each row is one observation.
232
+ code_book : ndarray
233
+ Code book to use. Same format than obs. Should have same number of
234
+ features (e.g., columns) than obs.
235
+ check_finite : bool, optional
236
+ Whether to check that the input matrices contain only finite numbers.
237
+ Disabling may give a performance gain, but may result in problems
238
+ (crashes, non-termination) if the inputs do contain infinities or NaNs.
239
+ Default: True
240
+
241
+ Returns
242
+ -------
243
+ code : ndarray
244
+ code[i] gives the label of the ith obversation; its code is
245
+ code_book[code[i]].
246
+ mind_dist : ndarray
247
+ min_dist[i] gives the distance between the ith observation and its
248
+ corresponding code.
249
+
250
+ Notes
251
+ -----
252
+ This function is slower than the C version but works for
253
+ all input types. If the inputs have the wrong types for the
254
+ C versions of the function, this one is called as a last resort.
255
+
256
+ It is about 20 times slower than the C version.
257
+
258
+ """
259
+ xp = array_namespace(obs, code_book)
260
+ obs = _asarray(obs, xp=xp, check_finite=check_finite)
261
+ code_book = _asarray(code_book, xp=xp, check_finite=check_finite)
262
+
263
+ if obs.ndim != code_book.ndim:
264
+ raise ValueError("Observation and code_book should have the same rank")
265
+
266
+ if obs.ndim == 1:
267
+ obs = obs[:, xp.newaxis]
268
+ code_book = code_book[:, xp.newaxis]
269
+
270
+ # Once `cdist` has array API support, this `xp.asarray` call can be removed
271
+ dist = xp.asarray(cdist(obs, code_book))
272
+ code = xp.argmin(dist, axis=1)
273
+ min_dist = xp.min(dist, axis=1)
274
+ return code, min_dist
275
+
276
+
277
+ def _kmeans(obs, guess, thresh=1e-5, xp=None):
278
+ """ "raw" version of k-means.
279
+
280
+ Returns
281
+ -------
282
+ code_book
283
+ The lowest distortion codebook found.
284
+ avg_dist
285
+ The average distance a observation is from a code in the book.
286
+ Lower means the code_book matches the data better.
287
+
288
+ See Also
289
+ --------
290
+ kmeans : wrapper around k-means
291
+
292
+ Examples
293
+ --------
294
+ Note: not whitened in this example.
295
+
296
+ >>> import numpy as np
297
+ >>> from scipy.cluster.vq import _kmeans
298
+ >>> features = np.array([[ 1.9,2.3],
299
+ ... [ 1.5,2.5],
300
+ ... [ 0.8,0.6],
301
+ ... [ 0.4,1.8],
302
+ ... [ 1.0,1.0]])
303
+ >>> book = np.array((features[0],features[2]))
304
+ >>> _kmeans(features,book)
305
+ (array([[ 1.7 , 2.4 ],
306
+ [ 0.73333333, 1.13333333]]), 0.40563916697728591)
307
+
308
+ """
309
+ xp = np if xp is None else xp
310
+ code_book = guess
311
+ diff = xp.inf
312
+ prev_avg_dists = deque([diff], maxlen=2)
313
+ while diff > thresh:
314
+ # compute membership and distances between obs and code_book
315
+ obs_code, distort = vq(obs, code_book, check_finite=False)
316
+ prev_avg_dists.append(xp.mean(distort, axis=-1))
317
+ # recalc code_book as centroids of associated obs
318
+ obs = np.asarray(obs)
319
+ obs_code = np.asarray(obs_code)
320
+ code_book, has_members = _vq.update_cluster_means(obs, obs_code,
321
+ code_book.shape[0])
322
+ obs = xp.asarray(obs)
323
+ obs_code = xp.asarray(obs_code)
324
+ code_book = xp.asarray(code_book)
325
+ has_members = xp.asarray(has_members)
326
+ code_book = code_book[has_members]
327
+ diff = xp.abs(prev_avg_dists[0] - prev_avg_dists[1])
328
+
329
+ return code_book, prev_avg_dists[1]
330
+
331
+
332
+ @_transition_to_rng("seed")
333
+ def kmeans(obs, k_or_guess, iter=20, thresh=1e-5, check_finite=True,
334
+ *, rng=None):
335
+ """
336
+ Performs k-means on a set of observation vectors forming k clusters.
337
+
338
+ The k-means algorithm adjusts the classification of the observations
339
+ into clusters and updates the cluster centroids until the position of
340
+ the centroids is stable over successive iterations. In this
341
+ implementation of the algorithm, the stability of the centroids is
342
+ determined by comparing the absolute value of the change in the average
343
+ Euclidean distance between the observations and their corresponding
344
+ centroids against a threshold. This yields
345
+ a code book mapping centroids to codes and vice versa.
346
+
347
+ Parameters
348
+ ----------
349
+ obs : ndarray
350
+ Each row of the M by N array is an observation vector. The
351
+ columns are the features seen during each observation.
352
+ The features must be whitened first with the `whiten` function.
353
+
354
+ k_or_guess : int or ndarray
355
+ The number of centroids to generate. A code is assigned to
356
+ each centroid, which is also the row index of the centroid
357
+ in the code_book matrix generated.
358
+
359
+ The initial k centroids are chosen by randomly selecting
360
+ observations from the observation matrix. Alternatively,
361
+ passing a k by N array specifies the initial k centroids.
362
+
363
+ iter : int, optional
364
+ The number of times to run k-means, returning the codebook
365
+ with the lowest distortion. This argument is ignored if
366
+ initial centroids are specified with an array for the
367
+ ``k_or_guess`` parameter. This parameter does not represent the
368
+ number of iterations of the k-means algorithm.
369
+
370
+ thresh : float, optional
371
+ Terminates the k-means algorithm if the change in
372
+ distortion since the last k-means iteration is less than
373
+ or equal to threshold.
374
+
375
+ check_finite : bool, optional
376
+ Whether to check that the input matrices contain only finite numbers.
377
+ Disabling may give a performance gain, but may result in problems
378
+ (crashes, non-termination) if the inputs do contain infinities or NaNs.
379
+ Default: True
380
+ rng : `numpy.random.Generator`, optional
381
+ Pseudorandom number generator state. When `rng` is None, a new
382
+ `numpy.random.Generator` is created using entropy from the
383
+ operating system. Types other than `numpy.random.Generator` are
384
+ passed to `numpy.random.default_rng` to instantiate a ``Generator``.
385
+
386
+ Returns
387
+ -------
388
+ codebook : ndarray
389
+ A k by N array of k centroids. The ith centroid
390
+ codebook[i] is represented with the code i. The centroids
391
+ and codes generated represent the lowest distortion seen,
392
+ not necessarily the globally minimal distortion.
393
+ Note that the number of centroids is not necessarily the same as the
394
+ ``k_or_guess`` parameter, because centroids assigned to no observations
395
+ are removed during iterations.
396
+
397
+ distortion : float
398
+ The mean (non-squared) Euclidean distance between the observations
399
+ passed and the centroids generated. Note the difference to the standard
400
+ definition of distortion in the context of the k-means algorithm, which
401
+ is the sum of the squared distances.
402
+
403
+ See Also
404
+ --------
405
+ kmeans2 : a different implementation of k-means clustering
406
+ with more methods for generating initial centroids but without
407
+ using a distortion change threshold as a stopping criterion.
408
+
409
+ whiten : must be called prior to passing an observation matrix
410
+ to kmeans.
411
+
412
+ Notes
413
+ -----
414
+ For more functionalities or optimal performance, you can use
415
+ `sklearn.cluster.KMeans <https://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html>`_.
416
+ `This <https://hdbscan.readthedocs.io/en/latest/performance_and_scalability.html#comparison-of-high-performance-implementations>`_
417
+ is a benchmark result of several implementations.
418
+
419
+ Examples
420
+ --------
421
+ >>> import numpy as np
422
+ >>> from scipy.cluster.vq import vq, kmeans, whiten
423
+ >>> import matplotlib.pyplot as plt
424
+ >>> features = np.array([[ 1.9,2.3],
425
+ ... [ 1.5,2.5],
426
+ ... [ 0.8,0.6],
427
+ ... [ 0.4,1.8],
428
+ ... [ 0.1,0.1],
429
+ ... [ 0.2,1.8],
430
+ ... [ 2.0,0.5],
431
+ ... [ 0.3,1.5],
432
+ ... [ 1.0,1.0]])
433
+ >>> whitened = whiten(features)
434
+ >>> book = np.array((whitened[0],whitened[2]))
435
+ >>> kmeans(whitened,book)
436
+ (array([[ 2.3110306 , 2.86287398], # random
437
+ [ 0.93218041, 1.24398691]]), 0.85684700941625547)
438
+
439
+ >>> codes = 3
440
+ >>> kmeans(whitened,codes)
441
+ (array([[ 2.3110306 , 2.86287398], # random
442
+ [ 1.32544402, 0.65607529],
443
+ [ 0.40782893, 2.02786907]]), 0.5196582527686241)
444
+
445
+ >>> # Create 50 datapoints in two clusters a and b
446
+ >>> pts = 50
447
+ >>> rng = np.random.default_rng()
448
+ >>> a = rng.multivariate_normal([0, 0], [[4, 1], [1, 4]], size=pts)
449
+ >>> b = rng.multivariate_normal([30, 10],
450
+ ... [[10, 2], [2, 1]],
451
+ ... size=pts)
452
+ >>> features = np.concatenate((a, b))
453
+ >>> # Whiten data
454
+ >>> whitened = whiten(features)
455
+ >>> # Find 2 clusters in the data
456
+ >>> codebook, distortion = kmeans(whitened, 2)
457
+ >>> # Plot whitened data and cluster centers in red
458
+ >>> plt.scatter(whitened[:, 0], whitened[:, 1])
459
+ >>> plt.scatter(codebook[:, 0], codebook[:, 1], c='r')
460
+ >>> plt.show()
461
+
462
+ """
463
+ if isinstance(k_or_guess, int):
464
+ xp = array_namespace(obs)
465
+ else:
466
+ xp = array_namespace(obs, k_or_guess)
467
+ obs = _asarray(obs, xp=xp, check_finite=check_finite)
468
+ guess = _asarray(k_or_guess, xp=xp, check_finite=check_finite)
469
+ if iter < 1:
470
+ raise ValueError(f"iter must be at least 1, got {iter}")
471
+
472
+ # Determine whether a count (scalar) or an initial guess (array) was passed.
473
+ if xp_size(guess) != 1:
474
+ if xp_size(guess) < 1:
475
+ raise ValueError(f"Asked for 0 clusters. Initial book was {guess}")
476
+ return _kmeans(obs, guess, thresh=thresh, xp=xp)
477
+
478
+ # k_or_guess is a scalar, now verify that it's an integer
479
+ k = int(guess)
480
+ if k != guess:
481
+ raise ValueError("If k_or_guess is a scalar, it must be an integer.")
482
+ if k < 1:
483
+ raise ValueError("Asked for %d clusters." % k)
484
+
485
+ rng = check_random_state(rng)
486
+
487
+ # initialize best distance value to a large value
488
+ best_dist = xp.inf
489
+ for i in range(iter):
490
+ # the initial code book is randomly selected from observations
491
+ guess = _kpoints(obs, k, rng, xp)
492
+ book, dist = _kmeans(obs, guess, thresh=thresh, xp=xp)
493
+ if dist < best_dist:
494
+ best_book = book
495
+ best_dist = dist
496
+ return best_book, best_dist
497
+
498
+
499
+ def _kpoints(data, k, rng, xp):
500
+ """Pick k points at random in data (one row = one observation).
501
+
502
+ Parameters
503
+ ----------
504
+ data : ndarray
505
+ Expect a rank 1 or 2 array. Rank 1 are assumed to describe one
506
+ dimensional data, rank 2 multidimensional data, in which case one
507
+ row is one observation.
508
+ k : int
509
+ Number of samples to generate.
510
+ rng : `numpy.random.Generator` or `numpy.random.RandomState`
511
+ Random number generator.
512
+
513
+ Returns
514
+ -------
515
+ x : ndarray
516
+ A 'k' by 'N' containing the initial centroids
517
+
518
+ """
519
+ idx = rng.choice(data.shape[0], size=int(k), replace=False)
520
+ # convert to array with default integer dtype (avoids numpy#25607)
521
+ idx = xp.asarray(idx, dtype=xp.asarray([1]).dtype)
522
+ return xp.take(data, idx, axis=0)
523
+
524
+
525
+ def _krandinit(data, k, rng, xp):
526
+ """Returns k samples of a random variable whose parameters depend on data.
527
+
528
+ More precisely, it returns k observations sampled from a Gaussian random
529
+ variable whose mean and covariances are the ones estimated from the data.
530
+
531
+ Parameters
532
+ ----------
533
+ data : ndarray
534
+ Expect a rank 1 or 2 array. Rank 1 is assumed to describe 1-D
535
+ data, rank 2 multidimensional data, in which case one
536
+ row is one observation.
537
+ k : int
538
+ Number of samples to generate.
539
+ rng : `numpy.random.Generator` or `numpy.random.RandomState`
540
+ Random number generator.
541
+
542
+ Returns
543
+ -------
544
+ x : ndarray
545
+ A 'k' by 'N' containing the initial centroids
546
+
547
+ """
548
+ mu = xp.mean(data, axis=0)
549
+ k = np.asarray(k)
550
+
551
+ if data.ndim == 1:
552
+ _cov = xpx.cov(data, xp=xp)
553
+ x = rng.standard_normal(size=k)
554
+ x = xp.asarray(x)
555
+ x *= xp.sqrt(_cov)
556
+ elif data.shape[1] > data.shape[0]:
557
+ # initialize when the covariance matrix is rank deficient
558
+ _, s, vh = xp.linalg.svd(data - mu, full_matrices=False)
559
+ x = rng.standard_normal(size=(k, xp_size(s)))
560
+ x = xp.asarray(x)
561
+ sVh = s[:, None] * vh / xp.sqrt(data.shape[0] - xp.asarray(1.))
562
+ x = x @ sVh
563
+ else:
564
+ _cov = xpx.atleast_nd(xpx.cov(data.T, xp=xp), ndim=2, xp=xp)
565
+
566
+ # k rows, d cols (one row = one obs)
567
+ # Generate k sample of a random variable ~ Gaussian(mu, cov)
568
+ x = rng.standard_normal(size=(k, xp_size(mu)))
569
+ x = xp.asarray(x)
570
+ x = x @ xp.linalg.cholesky(_cov).T
571
+
572
+ x += mu
573
+ return x
574
+
575
+
576
+ def _kpp(data, k, rng, xp):
577
+ """ Picks k points in the data based on the kmeans++ method.
578
+
579
+ Parameters
580
+ ----------
581
+ data : ndarray
582
+ Expect a rank 1 or 2 array. Rank 1 is assumed to describe 1-D
583
+ data, rank 2 multidimensional data, in which case one
584
+ row is one observation.
585
+ k : int
586
+ Number of samples to generate.
587
+ rng : `numpy.random.Generator` or `numpy.random.RandomState`
588
+ Random number generator.
589
+
590
+ Returns
591
+ -------
592
+ init : ndarray
593
+ A 'k' by 'N' containing the initial centroids.
594
+
595
+ References
596
+ ----------
597
+ .. [1] D. Arthur and S. Vassilvitskii, "k-means++: the advantages of
598
+ careful seeding", Proceedings of the Eighteenth Annual ACM-SIAM Symposium
599
+ on Discrete Algorithms, 2007.
600
+ """
601
+
602
+ ndim = len(data.shape)
603
+ if ndim == 1:
604
+ data = data[:, None]
605
+
606
+ dims = data.shape[1]
607
+
608
+ init = xp.empty((int(k), dims))
609
+
610
+ for i in range(k):
611
+ if i == 0:
612
+ init[i, :] = data[rng_integers(rng, data.shape[0]), :]
613
+
614
+ else:
615
+ D2 = cdist(init[:i,:], data, metric='sqeuclidean').min(axis=0)
616
+ probs = D2/D2.sum()
617
+ cumprobs = probs.cumsum()
618
+ r = rng.uniform()
619
+ cumprobs = np.asarray(cumprobs)
620
+ init[i, :] = data[np.searchsorted(cumprobs, r), :]
621
+
622
+ if ndim == 1:
623
+ init = init[:, 0]
624
+ return init
625
+
626
+
627
+ _valid_init_meth = {'random': _krandinit, 'points': _kpoints, '++': _kpp}
628
+
629
+
630
+ def _missing_warn():
631
+ """Print a warning when called."""
632
+ warnings.warn("One of the clusters is empty. "
633
+ "Re-run kmeans with a different initialization.",
634
+ stacklevel=3)
635
+
636
+
637
+ def _missing_raise():
638
+ """Raise a ClusterError when called."""
639
+ raise ClusterError("One of the clusters is empty. "
640
+ "Re-run kmeans with a different initialization.")
641
+
642
+
643
+ _valid_miss_meth = {'warn': _missing_warn, 'raise': _missing_raise}
644
+
645
+
646
+ @_transition_to_rng("seed")
647
+ def kmeans2(data, k, iter=10, thresh=1e-5, minit='random',
648
+ missing='warn', check_finite=True, *, rng=None):
649
+ """
650
+ Classify a set of observations into k clusters using the k-means algorithm.
651
+
652
+ The algorithm attempts to minimize the Euclidean distance between
653
+ observations and centroids. Several initialization methods are
654
+ included.
655
+
656
+ Parameters
657
+ ----------
658
+ data : ndarray
659
+ A 'M' by 'N' array of 'M' observations in 'N' dimensions or a length
660
+ 'M' array of 'M' 1-D observations.
661
+ k : int or ndarray
662
+ The number of clusters to form as well as the number of
663
+ centroids to generate. If `minit` initialization string is
664
+ 'matrix', or if a ndarray is given instead, it is
665
+ interpreted as initial cluster to use instead.
666
+ iter : int, optional
667
+ Number of iterations of the k-means algorithm to run. Note
668
+ that this differs in meaning from the iters parameter to
669
+ the kmeans function.
670
+ thresh : float, optional
671
+ (not used yet)
672
+ minit : str, optional
673
+ Method for initialization. Available methods are 'random',
674
+ 'points', '++' and 'matrix':
675
+
676
+ 'random': generate k centroids from a Gaussian with mean and
677
+ variance estimated from the data.
678
+
679
+ 'points': choose k observations (rows) at random from data for
680
+ the initial centroids.
681
+
682
+ '++': choose k observations accordingly to the kmeans++ method
683
+ (careful seeding)
684
+
685
+ 'matrix': interpret the k parameter as a k by M (or length k
686
+ array for 1-D data) array of initial centroids.
687
+ missing : str, optional
688
+ Method to deal with empty clusters. Available methods are
689
+ 'warn' and 'raise':
690
+
691
+ 'warn': give a warning and continue.
692
+
693
+ 'raise': raise an ClusterError and terminate the algorithm.
694
+ check_finite : bool, optional
695
+ Whether to check that the input matrices contain only finite numbers.
696
+ Disabling may give a performance gain, but may result in problems
697
+ (crashes, non-termination) if the inputs do contain infinities or NaNs.
698
+ Default: True
699
+ rng : `numpy.random.Generator`, optional
700
+ Pseudorandom number generator state. When `rng` is None, a new
701
+ `numpy.random.Generator` is created using entropy from the
702
+ operating system. Types other than `numpy.random.Generator` are
703
+ passed to `numpy.random.default_rng` to instantiate a ``Generator``.
704
+
705
+ Returns
706
+ -------
707
+ centroid : ndarray
708
+ A 'k' by 'N' array of centroids found at the last iteration of
709
+ k-means.
710
+ label : ndarray
711
+ label[i] is the code or index of the centroid the
712
+ ith observation is closest to.
713
+
714
+ See Also
715
+ --------
716
+ kmeans
717
+
718
+ References
719
+ ----------
720
+ .. [1] D. Arthur and S. Vassilvitskii, "k-means++: the advantages of
721
+ careful seeding", Proceedings of the Eighteenth Annual ACM-SIAM Symposium
722
+ on Discrete Algorithms, 2007.
723
+
724
+ Examples
725
+ --------
726
+ >>> from scipy.cluster.vq import kmeans2
727
+ >>> import matplotlib.pyplot as plt
728
+ >>> import numpy as np
729
+
730
+ Create z, an array with shape (100, 2) containing a mixture of samples
731
+ from three multivariate normal distributions.
732
+
733
+ >>> rng = np.random.default_rng()
734
+ >>> a = rng.multivariate_normal([0, 6], [[2, 1], [1, 1.5]], size=45)
735
+ >>> b = rng.multivariate_normal([2, 0], [[1, -1], [-1, 3]], size=30)
736
+ >>> c = rng.multivariate_normal([6, 4], [[5, 0], [0, 1.2]], size=25)
737
+ >>> z = np.concatenate((a, b, c))
738
+ >>> rng.shuffle(z)
739
+
740
+ Compute three clusters.
741
+
742
+ >>> centroid, label = kmeans2(z, 3, minit='points')
743
+ >>> centroid
744
+ array([[ 2.22274463, -0.61666946], # may vary
745
+ [ 0.54069047, 5.86541444],
746
+ [ 6.73846769, 4.01991898]])
747
+
748
+ How many points are in each cluster?
749
+
750
+ >>> counts = np.bincount(label)
751
+ >>> counts
752
+ array([29, 51, 20]) # may vary
753
+
754
+ Plot the clusters.
755
+
756
+ >>> w0 = z[label == 0]
757
+ >>> w1 = z[label == 1]
758
+ >>> w2 = z[label == 2]
759
+ >>> plt.plot(w0[:, 0], w0[:, 1], 'o', alpha=0.5, label='cluster 0')
760
+ >>> plt.plot(w1[:, 0], w1[:, 1], 'd', alpha=0.5, label='cluster 1')
761
+ >>> plt.plot(w2[:, 0], w2[:, 1], 's', alpha=0.5, label='cluster 2')
762
+ >>> plt.plot(centroid[:, 0], centroid[:, 1], 'k*', label='centroids')
763
+ >>> plt.axis('equal')
764
+ >>> plt.legend(shadow=True)
765
+ >>> plt.show()
766
+
767
+ """
768
+ if int(iter) < 1:
769
+ raise ValueError(f"Invalid iter ({iter}), must be a positive integer.")
770
+ try:
771
+ miss_meth = _valid_miss_meth[missing]
772
+ except KeyError as e:
773
+ raise ValueError(f"Unknown missing method {missing!r}") from e
774
+
775
+ if isinstance(k, int):
776
+ xp = array_namespace(data)
777
+ else:
778
+ xp = array_namespace(data, k)
779
+ data = _asarray(data, xp=xp, check_finite=check_finite)
780
+ code_book = xp_copy(k, xp=xp)
781
+ if data.ndim == 1:
782
+ d = 1
783
+ elif data.ndim == 2:
784
+ d = data.shape[1]
785
+ else:
786
+ raise ValueError("Input of rank > 2 is not supported.")
787
+
788
+ if xp_size(data) < 1 or xp_size(code_book) < 1:
789
+ raise ValueError("Empty input is not supported.")
790
+
791
+ # If k is not a single value, it should be compatible with data's shape
792
+ if minit == 'matrix' or xp_size(code_book) > 1:
793
+ if data.ndim != code_book.ndim:
794
+ raise ValueError("k array doesn't match data rank")
795
+ nc = code_book.shape[0]
796
+ if data.ndim > 1 and code_book.shape[1] != d:
797
+ raise ValueError("k array doesn't match data dimension")
798
+ else:
799
+ nc = int(code_book)
800
+
801
+ if nc < 1:
802
+ raise ValueError("Cannot ask kmeans2 for %d clusters"
803
+ " (k was %s)" % (nc, code_book))
804
+ elif nc != code_book:
805
+ warnings.warn("k was not an integer, was converted.", stacklevel=2)
806
+
807
+ try:
808
+ init_meth = _valid_init_meth[minit]
809
+ except KeyError as e:
810
+ raise ValueError(f"Unknown init method {minit!r}") from e
811
+ else:
812
+ rng = check_random_state(rng)
813
+ code_book = init_meth(data, code_book, rng, xp)
814
+
815
+ data = np.asarray(data)
816
+ code_book = np.asarray(code_book)
817
+ for i in range(iter):
818
+ # Compute the nearest neighbor for each obs using the current code book
819
+ label = vq(data, code_book, check_finite=check_finite)[0]
820
+ # Update the code book by computing centroids
821
+ new_code_book, has_members = _vq.update_cluster_means(data, label, nc)
822
+ if not has_members.all():
823
+ miss_meth()
824
+ # Set the empty clusters to their previous positions
825
+ new_code_book[~has_members] = code_book[~has_members]
826
+ code_book = new_code_book
827
+
828
+ return xp.asarray(code_book), xp.asarray(label)