xbarray 0.0.1a1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of xbarray might be problematic. Click here for more details.
- array_api_typing/__init__.py +9 -0
- array_api_typing/typing_2024_12/__init__.py +12 -0
- array_api_typing/typing_2024_12/_api_constant.py +32 -0
- array_api_typing/typing_2024_12/_api_fft_typing.py +717 -0
- array_api_typing/typing_2024_12/_api_linalg_typing.py +897 -0
- array_api_typing/typing_2024_12/_api_return_typing.py +103 -0
- array_api_typing/typing_2024_12/_api_typing.py +5855 -0
- array_api_typing/typing_2024_12/_array_typing.py +1265 -0
- array_api_typing/typing_compat/__init__.py +12 -0
- array_api_typing/typing_compat/_api_typing.py +27 -0
- array_api_typing/typing_compat/_array_typing.py +36 -0
- array_api_typing/typing_extra/__init__.py +12 -0
- array_api_typing/typing_extra/_api_typing.py +651 -0
- array_api_typing/typing_extra/_at.py +87 -0
- xbarray/__init__.py +1 -0
- xbarray/base/__init__.py +1 -0
- xbarray/base/base.py +199 -0
- xbarray/common/implementations.py +61 -0
- xbarray/jax/__init__.py +25 -0
- xbarray/jax/_extra.py +98 -0
- xbarray/jax/_typing.py +15 -0
- xbarray/jax/random.py +116 -0
- xbarray/numpy/__init__.py +19 -0
- xbarray/numpy/_extra.py +83 -0
- xbarray/numpy/_typing.py +14 -0
- xbarray/numpy/random.py +106 -0
- xbarray/pytorch/__init__.py +20 -0
- xbarray/pytorch/_extra.py +109 -0
- xbarray/pytorch/_typing.py +13 -0
- xbarray/pytorch/random.py +102 -0
- xbarray-0.0.1a1.dist-info/METADATA +14 -0
- xbarray-0.0.1a1.dist-info/RECORD +35 -0
- xbarray-0.0.1a1.dist-info/WHEEL +5 -0
- xbarray-0.0.1a1.dist-info/licenses/LICENSE +21 -0
- xbarray-0.0.1a1.dist-info/top_level.txt +2 -0
|
@@ -0,0 +1,651 @@
|
|
|
1
|
+
from typing import Protocol, TypeVar, Optional, Any, Tuple, Union, Type, TypedDict, Literal, Sequence, overload, Callable
|
|
2
|
+
from abc import abstractmethod
|
|
3
|
+
from array_api_typing.typing_compat._api_typing import ArrayAPINamespace as ArrayAPICompatNamespace, _NAMESPACE_ARRAY, _NAMESPACE_DEVICE, _NAMESPACE_DTYPE
|
|
4
|
+
from array_api_typing.typing_compat._array_typing import SetIndex, GetIndex
|
|
5
|
+
from ._at import AtResult
|
|
6
|
+
|
|
7
|
+
try:
|
|
8
|
+
from array_api_extra._lib._at import Undef
|
|
9
|
+
except ImportError:
|
|
10
|
+
from enum import Enum
|
|
11
|
+
class Undef(Enum):
|
|
12
|
+
UNDEF = 0
|
|
13
|
+
|
|
14
|
+
class ArrayAPINamespace(ArrayAPICompatNamespace[_NAMESPACE_ARRAY, _NAMESPACE_DEVICE, _NAMESPACE_DTYPE], Protocol[_NAMESPACE_ARRAY, _NAMESPACE_DEVICE, _NAMESPACE_DTYPE]):
|
|
15
|
+
"""
|
|
16
|
+
From the array_api_extra
|
|
17
|
+
https://github.com/data-apis/array-api-extra/blob/main/src/array_api_extra/_lib/_at.py
|
|
18
|
+
"""
|
|
19
|
+
@abstractmethod
|
|
20
|
+
def at(
|
|
21
|
+
self,
|
|
22
|
+
x: _NAMESPACE_ARRAY,
|
|
23
|
+
idx : Union[SetIndex, Undef] = Undef.UNDEF,
|
|
24
|
+
/
|
|
25
|
+
) -> "AtResult[_NAMESPACE_ARRAY]":
|
|
26
|
+
"""
|
|
27
|
+
Returns an object that provides methods for elementwise operations
|
|
28
|
+
on the array `x` using the `at` API.
|
|
29
|
+
"""
|
|
30
|
+
pass
|
|
31
|
+
|
|
32
|
+
"""
|
|
33
|
+
Utility Functions
|
|
34
|
+
https://github.com/data-apis/array-api-extra/blob/main/src/array_api_extra/_lib/_funcs.py
|
|
35
|
+
"""
|
|
36
|
+
@overload
|
|
37
|
+
def apply_where(
|
|
38
|
+
self,
|
|
39
|
+
cond: _NAMESPACE_ARRAY,
|
|
40
|
+
args: Union[_NAMESPACE_ARRAY, Tuple[_NAMESPACE_ARRAY, ...]],
|
|
41
|
+
f1: Callable[..., _NAMESPACE_ARRAY],
|
|
42
|
+
f2: Callable[..., _NAMESPACE_ARRAY],
|
|
43
|
+
/,
|
|
44
|
+
) -> _NAMESPACE_ARRAY:
|
|
45
|
+
"""
|
|
46
|
+
Run one of two elementwise functions depending on a condition.
|
|
47
|
+
|
|
48
|
+
Equivalent to ``f1(*args) if cond else fill_value`` performed elementwise
|
|
49
|
+
when `fill_value` is defined, otherwise to ``f1(*args) if cond else f2(*args)``.
|
|
50
|
+
|
|
51
|
+
Parameters
|
|
52
|
+
----------
|
|
53
|
+
cond : array
|
|
54
|
+
The condition, expressed as a boolean array.
|
|
55
|
+
args : Array or tuple of Arrays
|
|
56
|
+
Argument(s) to `f1` (and `f2`). Must be broadcastable with `cond`.
|
|
57
|
+
f1 : callable
|
|
58
|
+
Elementwise function of `args`, returning a single array.
|
|
59
|
+
Where `cond` is True, output will be ``f1(arg0[cond], arg1[cond], ...)``.
|
|
60
|
+
f2 : callable, optional
|
|
61
|
+
Elementwise function of `args`, returning a single array.
|
|
62
|
+
Where `cond` is False, output will be ``f2(arg0[cond], arg1[cond], ...)``.
|
|
63
|
+
Mutually exclusive with `fill_value`.
|
|
64
|
+
fill_value : Array or scalar, optional
|
|
65
|
+
If provided, value with which to fill output array where `cond` is False.
|
|
66
|
+
It does not need to be scalar; it needs however to be broadcastable with
|
|
67
|
+
`cond` and `args`.
|
|
68
|
+
Mutually exclusive with `f2`. You must provide one or the other.
|
|
69
|
+
|
|
70
|
+
Returns
|
|
71
|
+
-------
|
|
72
|
+
Array
|
|
73
|
+
An array with elements from the output of `f1` where `cond` is True and either
|
|
74
|
+
the output of `f2` or `fill_value` where `cond` is False. The returned array has
|
|
75
|
+
data type determined by type promotion rules between the output of `f1` and
|
|
76
|
+
either `fill_value` or the output of `f2`.
|
|
77
|
+
|
|
78
|
+
Notes
|
|
79
|
+
-----
|
|
80
|
+
``xp.where(cond, f1(*args), f2(*args))`` requires explicitly evaluating `f1` even
|
|
81
|
+
when `cond` is False, and `f2` when cond is True. This function evaluates each
|
|
82
|
+
function only for their matching condition, if the backend allows for it.
|
|
83
|
+
|
|
84
|
+
On Dask, `f1` and `f2` are applied to the individual chunks and should use functions
|
|
85
|
+
from the namespace of the chunks.
|
|
86
|
+
|
|
87
|
+
Examples
|
|
88
|
+
--------
|
|
89
|
+
>>> import array_api_strict as xp
|
|
90
|
+
>>> import array_api_extra as xpx
|
|
91
|
+
>>> a = xp.asarray([5, 4, 3])
|
|
92
|
+
>>> b = xp.asarray([0, 2, 2])
|
|
93
|
+
>>> def f(a, b):
|
|
94
|
+
... return a // b
|
|
95
|
+
>>> xpx.apply_where(b != 0, (a, b), f, fill_value=xp.nan)
|
|
96
|
+
array([ nan, 2., 1.])
|
|
97
|
+
"""
|
|
98
|
+
|
|
99
|
+
@overload
|
|
100
|
+
def apply_where(
|
|
101
|
+
self,
|
|
102
|
+
cond: _NAMESPACE_ARRAY,
|
|
103
|
+
args: Union[_NAMESPACE_ARRAY, Tuple[_NAMESPACE_ARRAY, ...]],
|
|
104
|
+
f1: Callable[..., _NAMESPACE_ARRAY],
|
|
105
|
+
f2: Callable[..., _NAMESPACE_ARRAY],
|
|
106
|
+
/,
|
|
107
|
+
*,
|
|
108
|
+
fill_value: Optional[Union[_NAMESPACE_ARRAY, float, int, complex]] = None,
|
|
109
|
+
) -> _NAMESPACE_ARRAY:
|
|
110
|
+
pass
|
|
111
|
+
|
|
112
|
+
@abstractmethod
|
|
113
|
+
def atleast_nd(
|
|
114
|
+
self,
|
|
115
|
+
x: _NAMESPACE_ARRAY,
|
|
116
|
+
/,
|
|
117
|
+
*,
|
|
118
|
+
ndim: int,
|
|
119
|
+
) -> _NAMESPACE_ARRAY:
|
|
120
|
+
"""
|
|
121
|
+
Recursively expand the dimension of an array to at least `ndim`.
|
|
122
|
+
|
|
123
|
+
Parameters
|
|
124
|
+
----------
|
|
125
|
+
x : array
|
|
126
|
+
Input array.
|
|
127
|
+
ndim : int
|
|
128
|
+
The minimum number of dimensions for the result.
|
|
129
|
+
|
|
130
|
+
Returns
|
|
131
|
+
-------
|
|
132
|
+
array
|
|
133
|
+
An array with ``res.ndim`` >= `ndim`.
|
|
134
|
+
If ``x.ndim`` >= `ndim`, `x` is returned.
|
|
135
|
+
If ``x.ndim`` < `ndim`, `x` is expanded by prepending new axes
|
|
136
|
+
until ``res.ndim`` equals `ndim`.
|
|
137
|
+
|
|
138
|
+
Examples
|
|
139
|
+
--------
|
|
140
|
+
>>> import array_api_strict as xp
|
|
141
|
+
>>> import array_api_extra as xpx
|
|
142
|
+
>>> x = xp.asarray([1])
|
|
143
|
+
>>> xpx.atleast_nd(x, ndim=3, xp=xp)
|
|
144
|
+
Array([[[1]]], dtype=array_api_strict.int64)
|
|
145
|
+
|
|
146
|
+
>>> x = xp.asarray([[[1, 2],
|
|
147
|
+
... [3, 4]]])
|
|
148
|
+
>>> xpx.atleast_nd(x, ndim=1, xp=xp) is x
|
|
149
|
+
True
|
|
150
|
+
"""
|
|
151
|
+
pass
|
|
152
|
+
|
|
153
|
+
@abstractmethod
|
|
154
|
+
def broadcast_shapes(
|
|
155
|
+
self,
|
|
156
|
+
*shapes : Tuple[Optional[float], ...]
|
|
157
|
+
) -> Tuple[Optional[float], ...]:
|
|
158
|
+
"""
|
|
159
|
+
Compute the shape of the broadcasted arrays.
|
|
160
|
+
|
|
161
|
+
Duplicates :func:`numpy.broadcast_shapes`, with additional support for
|
|
162
|
+
None and NaN sizes.
|
|
163
|
+
|
|
164
|
+
This is equivalent to ``xp.broadcast_arrays(arr1, arr2, ...)[0].shape``
|
|
165
|
+
without needing to worry about the backend potentially deep copying
|
|
166
|
+
the arrays.
|
|
167
|
+
|
|
168
|
+
Parameters
|
|
169
|
+
----------
|
|
170
|
+
*shapes : tuple[int | None, ...]
|
|
171
|
+
Shapes of the arrays to broadcast.
|
|
172
|
+
|
|
173
|
+
Returns
|
|
174
|
+
-------
|
|
175
|
+
tuple[int | None, ...]
|
|
176
|
+
The shape of the broadcasted arrays.
|
|
177
|
+
|
|
178
|
+
See Also
|
|
179
|
+
--------
|
|
180
|
+
numpy.broadcast_shapes : Equivalent NumPy function.
|
|
181
|
+
array_api.broadcast_arrays : Function to broadcast actual arrays.
|
|
182
|
+
|
|
183
|
+
Notes
|
|
184
|
+
-----
|
|
185
|
+
This function accepts the Array API's ``None`` for unknown sizes,
|
|
186
|
+
as well as Dask's non-standard ``math.nan``.
|
|
187
|
+
Regardless of input, the output always contains ``None`` for unknown sizes.
|
|
188
|
+
|
|
189
|
+
Examples
|
|
190
|
+
--------
|
|
191
|
+
>>> import array_api_extra as xpx
|
|
192
|
+
>>> xpx.broadcast_shapes((2, 3), (2, 1))
|
|
193
|
+
(2, 3)
|
|
194
|
+
>>> xpx.broadcast_shapes((4, 2, 3), (2, 1), (1, 3))
|
|
195
|
+
(4, 2, 3)
|
|
196
|
+
"""
|
|
197
|
+
|
|
198
|
+
def cov(
|
|
199
|
+
self,
|
|
200
|
+
m: _NAMESPACE_ARRAY,
|
|
201
|
+
/,
|
|
202
|
+
) -> _NAMESPACE_ARRAY:
|
|
203
|
+
"""
|
|
204
|
+
Estimate a covariance matrix.
|
|
205
|
+
|
|
206
|
+
Covariance indicates the level to which two variables vary together.
|
|
207
|
+
If we examine N-dimensional samples, :math:`X = [x_1, x_2, ... x_N]^T`,
|
|
208
|
+
then the covariance matrix element :math:`C_{ij}` is the covariance of
|
|
209
|
+
:math:`x_i` and :math:`x_j`. The element :math:`C_{ii}` is the variance
|
|
210
|
+
of :math:`x_i`.
|
|
211
|
+
|
|
212
|
+
This provides a subset of the functionality of ``numpy.cov``.
|
|
213
|
+
|
|
214
|
+
Parameters
|
|
215
|
+
----------
|
|
216
|
+
m : array
|
|
217
|
+
A 1-D or 2-D array containing multiple variables and observations.
|
|
218
|
+
Each row of `m` represents a variable, and each column a single
|
|
219
|
+
observation of all those variables.
|
|
220
|
+
xp : array_namespace, optional
|
|
221
|
+
The standard-compatible namespace for `m`. Default: infer.
|
|
222
|
+
|
|
223
|
+
Returns
|
|
224
|
+
-------
|
|
225
|
+
array
|
|
226
|
+
The covariance matrix of the variables.
|
|
227
|
+
|
|
228
|
+
Examples
|
|
229
|
+
--------
|
|
230
|
+
>>> import array_api_strict as xp
|
|
231
|
+
>>> import array_api_extra as xpx
|
|
232
|
+
|
|
233
|
+
Consider two variables, :math:`x_0` and :math:`x_1`, which
|
|
234
|
+
correlate perfectly, but in opposite directions:
|
|
235
|
+
|
|
236
|
+
>>> x = xp.asarray([[0, 2], [1, 1], [2, 0]]).T
|
|
237
|
+
>>> x
|
|
238
|
+
Array([[0, 1, 2],
|
|
239
|
+
[2, 1, 0]], dtype=array_api_strict.int64)
|
|
240
|
+
|
|
241
|
+
Note how :math:`x_0` increases while :math:`x_1` decreases. The covariance
|
|
242
|
+
matrix shows this clearly:
|
|
243
|
+
|
|
244
|
+
>>> xpx.cov(x, xp=xp)
|
|
245
|
+
Array([[ 1., -1.],
|
|
246
|
+
[-1., 1.]], dtype=array_api_strict.float64)
|
|
247
|
+
|
|
248
|
+
Note that element :math:`C_{0,1}`, which shows the correlation between
|
|
249
|
+
:math:`x_0` and :math:`x_1`, is negative.
|
|
250
|
+
|
|
251
|
+
Further, note how `x` and `y` are combined:
|
|
252
|
+
|
|
253
|
+
>>> x = xp.asarray([-2.1, -1, 4.3])
|
|
254
|
+
>>> y = xp.asarray([3, 1.1, 0.12])
|
|
255
|
+
>>> X = xp.stack((x, y), axis=0)
|
|
256
|
+
>>> xpx.cov(X, xp=xp)
|
|
257
|
+
Array([[11.71 , -4.286 ],
|
|
258
|
+
[-4.286 , 2.14413333]], dtype=array_api_strict.float64)
|
|
259
|
+
|
|
260
|
+
>>> xpx.cov(x, xp=xp)
|
|
261
|
+
Array(11.71, dtype=array_api_strict.float64)
|
|
262
|
+
|
|
263
|
+
>>> xpx.cov(y, xp=xp)
|
|
264
|
+
Array(2.14413333, dtype=array_api_strict.float64)
|
|
265
|
+
"""
|
|
266
|
+
|
|
267
|
+
def create_diagonal(
|
|
268
|
+
self,
|
|
269
|
+
x: _NAMESPACE_ARRAY,
|
|
270
|
+
/,
|
|
271
|
+
*,
|
|
272
|
+
offset: int = 0,
|
|
273
|
+
) -> _NAMESPACE_ARRAY:
|
|
274
|
+
"""
|
|
275
|
+
Construct a diagonal array.
|
|
276
|
+
|
|
277
|
+
Parameters
|
|
278
|
+
----------
|
|
279
|
+
x : array
|
|
280
|
+
An array having shape ``(*batch_dims, k)``.
|
|
281
|
+
offset : int, optional
|
|
282
|
+
Offset from the leading diagonal (default is ``0``).
|
|
283
|
+
Use positive ints for diagonals above the leading diagonal,
|
|
284
|
+
and negative ints for diagonals below the leading diagonal.
|
|
285
|
+
xp : array_namespace, optional
|
|
286
|
+
The standard-compatible namespace for `x`. Default: infer.
|
|
287
|
+
|
|
288
|
+
Returns
|
|
289
|
+
-------
|
|
290
|
+
array
|
|
291
|
+
An array having shape ``(*batch_dims, k+abs(offset), k+abs(offset))`` with `x`
|
|
292
|
+
on the diagonal (offset by `offset`).
|
|
293
|
+
|
|
294
|
+
Examples
|
|
295
|
+
--------
|
|
296
|
+
>>> import array_api_strict as xp
|
|
297
|
+
>>> import array_api_extra as xpx
|
|
298
|
+
>>> x = xp.asarray([2, 4, 8])
|
|
299
|
+
|
|
300
|
+
>>> xpx.create_diagonal(x, xp=xp)
|
|
301
|
+
Array([[2, 0, 0],
|
|
302
|
+
[0, 4, 0],
|
|
303
|
+
[0, 0, 8]], dtype=array_api_strict.int64)
|
|
304
|
+
|
|
305
|
+
>>> xpx.create_diagonal(x, offset=-2, xp=xp)
|
|
306
|
+
Array([[0, 0, 0, 0, 0],
|
|
307
|
+
[0, 0, 0, 0, 0],
|
|
308
|
+
[2, 0, 0, 0, 0],
|
|
309
|
+
[0, 4, 0, 0, 0],
|
|
310
|
+
[0, 0, 8, 0, 0]], dtype=array_api_strict.int64)
|
|
311
|
+
"""
|
|
312
|
+
|
|
313
|
+
def kron(
|
|
314
|
+
self,
|
|
315
|
+
a: _NAMESPACE_ARRAY,
|
|
316
|
+
b: _NAMESPACE_ARRAY,
|
|
317
|
+
/,
|
|
318
|
+
) -> _NAMESPACE_ARRAY:
|
|
319
|
+
"""
|
|
320
|
+
Kronecker product of two arrays.
|
|
321
|
+
|
|
322
|
+
Computes the Kronecker product, a composite array made of blocks of the
|
|
323
|
+
second array scaled by the first.
|
|
324
|
+
|
|
325
|
+
Equivalent to ``numpy.kron`` for NumPy arrays.
|
|
326
|
+
|
|
327
|
+
Parameters
|
|
328
|
+
----------
|
|
329
|
+
a, b : Array | int | float | complex
|
|
330
|
+
Input arrays or scalars. At least one must be an array.
|
|
331
|
+
xp : array_namespace, optional
|
|
332
|
+
The standard-compatible namespace for `a` and `b`. Default: infer.
|
|
333
|
+
|
|
334
|
+
Returns
|
|
335
|
+
-------
|
|
336
|
+
array
|
|
337
|
+
The Kronecker product of `a` and `b`.
|
|
338
|
+
|
|
339
|
+
Notes
|
|
340
|
+
-----
|
|
341
|
+
The function assumes that the number of dimensions of `a` and `b`
|
|
342
|
+
are the same, if necessary prepending the smallest with ones.
|
|
343
|
+
If ``a.shape = (r0,r1,..,rN)`` and ``b.shape = (s0,s1,...,sN)``,
|
|
344
|
+
the Kronecker product has shape ``(r0*s0, r1*s1, ..., rN*SN)``.
|
|
345
|
+
The elements are products of elements from `a` and `b`, organized
|
|
346
|
+
explicitly by::
|
|
347
|
+
|
|
348
|
+
kron(a,b)[k0,k1,...,kN] = a[i0,i1,...,iN] * b[j0,j1,...,jN]
|
|
349
|
+
|
|
350
|
+
where::
|
|
351
|
+
|
|
352
|
+
kt = it * st + jt, t = 0,...,N
|
|
353
|
+
|
|
354
|
+
In the common 2-D case (N=1), the block structure can be visualized::
|
|
355
|
+
|
|
356
|
+
[[ a[0,0]*b, a[0,1]*b, ... , a[0,-1]*b ],
|
|
357
|
+
[ ... ... ],
|
|
358
|
+
[ a[-1,0]*b, a[-1,1]*b, ... , a[-1,-1]*b ]]
|
|
359
|
+
|
|
360
|
+
Examples
|
|
361
|
+
--------
|
|
362
|
+
>>> import array_api_strict as xp
|
|
363
|
+
>>> import array_api_extra as xpx
|
|
364
|
+
>>> xpx.kron(xp.asarray([1, 10, 100]), xp.asarray([5, 6, 7]), xp=xp)
|
|
365
|
+
Array([ 5, 6, 7, 50, 60, 70, 500,
|
|
366
|
+
600, 700], dtype=array_api_strict.int64)
|
|
367
|
+
|
|
368
|
+
>>> xpx.kron(xp.asarray([5, 6, 7]), xp.asarray([1, 10, 100]), xp=xp)
|
|
369
|
+
Array([ 5, 50, 500, 6, 60, 600, 7,
|
|
370
|
+
70, 700], dtype=array_api_strict.int64)
|
|
371
|
+
|
|
372
|
+
>>> xpx.kron(xp.eye(2), xp.ones((2, 2)), xp=xp)
|
|
373
|
+
Array([[1., 1., 0., 0.],
|
|
374
|
+
[1., 1., 0., 0.],
|
|
375
|
+
[0., 0., 1., 1.],
|
|
376
|
+
[0., 0., 1., 1.]], dtype=array_api_strict.float64)
|
|
377
|
+
|
|
378
|
+
>>> a = xp.reshape(xp.arange(100), (2, 5, 2, 5))
|
|
379
|
+
>>> b = xp.reshape(xp.arange(24), (2, 3, 4))
|
|
380
|
+
>>> c = xpx.kron(a, b, xp=xp)
|
|
381
|
+
>>> c.shape
|
|
382
|
+
(2, 10, 6, 20)
|
|
383
|
+
>>> I = (1, 3, 0, 2)
|
|
384
|
+
>>> J = (0, 2, 1)
|
|
385
|
+
>>> J1 = (0,) + J # extend to ndim=4
|
|
386
|
+
>>> S1 = (1,) + b.shape
|
|
387
|
+
>>> K = tuple(xp.asarray(I) * xp.asarray(S1) + xp.asarray(J1))
|
|
388
|
+
>>> c[K] == a[I]*b[J]
|
|
389
|
+
Array(True, dtype=array_api_strict.bool)
|
|
390
|
+
"""
|
|
391
|
+
|
|
392
|
+
def nunique(
|
|
393
|
+
self,
|
|
394
|
+
x: _NAMESPACE_ARRAY,
|
|
395
|
+
/,
|
|
396
|
+
) -> _NAMESPACE_ARRAY:
|
|
397
|
+
"""
|
|
398
|
+
Count the number of unique elements in an array.
|
|
399
|
+
|
|
400
|
+
Compatible with JAX and Dask, whose laziness would be otherwise
|
|
401
|
+
problematic.
|
|
402
|
+
|
|
403
|
+
Parameters
|
|
404
|
+
----------
|
|
405
|
+
x : Array
|
|
406
|
+
Input array.
|
|
407
|
+
xp : array_namespace, optional
|
|
408
|
+
The standard-compatible namespace for `x`. Default: infer.
|
|
409
|
+
|
|
410
|
+
Returns
|
|
411
|
+
-------
|
|
412
|
+
array: 0-dimensional integer array
|
|
413
|
+
The number of unique elements in `x`. It can be lazy.
|
|
414
|
+
"""
|
|
415
|
+
|
|
416
|
+
def setdiff1d(
|
|
417
|
+
self,
|
|
418
|
+
x1: Union[_NAMESPACE_ARRAY, int, float, complex],
|
|
419
|
+
x2: Union[_NAMESPACE_ARRAY, int, float, complex],
|
|
420
|
+
/,
|
|
421
|
+
*,
|
|
422
|
+
assume_unique: bool = False,
|
|
423
|
+
) -> _NAMESPACE_ARRAY:
|
|
424
|
+
"""
|
|
425
|
+
Find the set difference of two arrays.
|
|
426
|
+
|
|
427
|
+
Return the unique values in `x1` that are not in `x2`.
|
|
428
|
+
|
|
429
|
+
Parameters
|
|
430
|
+
----------
|
|
431
|
+
x1 : array | int | float | complex | bool
|
|
432
|
+
Input array.
|
|
433
|
+
x2 : array
|
|
434
|
+
Input comparison array.
|
|
435
|
+
assume_unique : bool
|
|
436
|
+
If ``True``, the input arrays are both assumed to be unique, which
|
|
437
|
+
can speed up the calculation. Default is ``False``.
|
|
438
|
+
xp : array_namespace, optional
|
|
439
|
+
The standard-compatible namespace for `x1` and `x2`. Default: infer.
|
|
440
|
+
|
|
441
|
+
Returns
|
|
442
|
+
-------
|
|
443
|
+
array
|
|
444
|
+
1D array of values in `x1` that are not in `x2`. The result
|
|
445
|
+
is sorted when `assume_unique` is ``False``, but otherwise only sorted
|
|
446
|
+
if the input is sorted.
|
|
447
|
+
|
|
448
|
+
Examples
|
|
449
|
+
--------
|
|
450
|
+
>>> import array_api_strict as xp
|
|
451
|
+
>>> import array_api_extra as xpx
|
|
452
|
+
|
|
453
|
+
>>> x1 = xp.asarray([1, 2, 3, 2, 4, 1])
|
|
454
|
+
>>> x2 = xp.asarray([3, 4, 5, 6])
|
|
455
|
+
>>> xpx.setdiff1d(x1, x2, xp=xp)
|
|
456
|
+
Array([1, 2], dtype=array_api_strict.int64)
|
|
457
|
+
"""
|
|
458
|
+
|
|
459
|
+
def sinc(
|
|
460
|
+
self,
|
|
461
|
+
x: _NAMESPACE_ARRAY,
|
|
462
|
+
/,
|
|
463
|
+
) -> _NAMESPACE_ARRAY:
|
|
464
|
+
r"""
|
|
465
|
+
Return the normalized sinc function.
|
|
466
|
+
|
|
467
|
+
The sinc function is equal to :math:`\sin(\pi x)/(\pi x)` for any argument
|
|
468
|
+
:math:`x\ne 0`. ``sinc(0)`` takes the limit value 1, making ``sinc`` not
|
|
469
|
+
only everywhere continuous but also infinitely differentiable.
|
|
470
|
+
|
|
471
|
+
.. note::
|
|
472
|
+
|
|
473
|
+
Note the normalization factor of ``pi`` used in the definition.
|
|
474
|
+
This is the most commonly used definition in signal processing.
|
|
475
|
+
Use ``sinc(x / xp.pi)`` to obtain the unnormalized sinc function
|
|
476
|
+
:math:`\sin(x)/x` that is more common in mathematics.
|
|
477
|
+
|
|
478
|
+
Parameters
|
|
479
|
+
----------
|
|
480
|
+
x : array
|
|
481
|
+
Array (possibly multi-dimensional) of values for which to calculate
|
|
482
|
+
``sinc(x)``. Must have a real floating point dtype.
|
|
483
|
+
xp : array_namespace, optional
|
|
484
|
+
The standard-compatible namespace for `x`. Default: infer.
|
|
485
|
+
|
|
486
|
+
Returns
|
|
487
|
+
-------
|
|
488
|
+
array
|
|
489
|
+
``sinc(x)`` calculated elementwise, which has the same shape as the input.
|
|
490
|
+
|
|
491
|
+
Notes
|
|
492
|
+
-----
|
|
493
|
+
The name sinc is short for "sine cardinal" or "sinus cardinalis".
|
|
494
|
+
|
|
495
|
+
The sinc function is used in various signal processing applications,
|
|
496
|
+
including in anti-aliasing, in the construction of a Lanczos resampling
|
|
497
|
+
filter, and in interpolation.
|
|
498
|
+
|
|
499
|
+
For bandlimited interpolation of discrete-time signals, the ideal
|
|
500
|
+
interpolation kernel is proportional to the sinc function.
|
|
501
|
+
|
|
502
|
+
References
|
|
503
|
+
----------
|
|
504
|
+
#. Weisstein, Eric W. "Sinc Function." From MathWorld--A Wolfram Web
|
|
505
|
+
Resource. https://mathworld.wolfram.com/SincFunction.html
|
|
506
|
+
#. Wikipedia, "Sinc function",
|
|
507
|
+
https://en.wikipedia.org/wiki/Sinc_function
|
|
508
|
+
|
|
509
|
+
Examples
|
|
510
|
+
--------
|
|
511
|
+
>>> import array_api_strict as xp
|
|
512
|
+
>>> import array_api_extra as xpx
|
|
513
|
+
>>> x = xp.linspace(-4, 4, 41)
|
|
514
|
+
>>> xpx.sinc(x, xp=xp)
|
|
515
|
+
Array([-3.89817183e-17, -4.92362781e-02,
|
|
516
|
+
-8.40918587e-02, -8.90384387e-02,
|
|
517
|
+
-5.84680802e-02, 3.89817183e-17,
|
|
518
|
+
6.68206631e-02, 1.16434881e-01,
|
|
519
|
+
1.26137788e-01, 8.50444803e-02,
|
|
520
|
+
-3.89817183e-17, -1.03943254e-01,
|
|
521
|
+
-1.89206682e-01, -2.16236208e-01,
|
|
522
|
+
-1.55914881e-01, 3.89817183e-17,
|
|
523
|
+
2.33872321e-01, 5.04551152e-01,
|
|
524
|
+
7.56826729e-01, 9.35489284e-01,
|
|
525
|
+
1.00000000e+00, 9.35489284e-01,
|
|
526
|
+
7.56826729e-01, 5.04551152e-01,
|
|
527
|
+
2.33872321e-01, 3.89817183e-17,
|
|
528
|
+
-1.55914881e-01, -2.16236208e-01,
|
|
529
|
+
-1.89206682e-01, -1.03943254e-01,
|
|
530
|
+
-3.89817183e-17, 8.50444803e-02,
|
|
531
|
+
1.26137788e-01, 1.16434881e-01,
|
|
532
|
+
6.68206631e-02, 3.89817183e-17,
|
|
533
|
+
-5.84680802e-02, -8.90384387e-02,
|
|
534
|
+
-8.40918587e-02, -4.92362781e-02,
|
|
535
|
+
-3.89817183e-17], dtype=array_api_strict.float64)
|
|
536
|
+
"""
|
|
537
|
+
|
|
538
|
+
"""
|
|
539
|
+
Delegation functions
|
|
540
|
+
https://github.com/data-apis/array-api-extra/blob/main/src/array_api_extra/_delegation.py
|
|
541
|
+
"""
|
|
542
|
+
def isclose(
|
|
543
|
+
a: Union[_NAMESPACE_ARRAY, int, float, complex],
|
|
544
|
+
b: Union[_NAMESPACE_ARRAY, int, float, complex],
|
|
545
|
+
*,
|
|
546
|
+
rtol: float = 1e-05,
|
|
547
|
+
atol: float = 1e-08,
|
|
548
|
+
equal_nan: bool = False,
|
|
549
|
+
) -> _NAMESPACE_ARRAY:
|
|
550
|
+
"""
|
|
551
|
+
Return a boolean array where two arrays are element-wise equal within a tolerance.
|
|
552
|
+
|
|
553
|
+
The tolerance values are positive, typically very small numbers. The relative
|
|
554
|
+
difference ``(rtol * abs(b))`` and the absolute difference `atol` are added together
|
|
555
|
+
to compare against the absolute difference between `a` and `b`.
|
|
556
|
+
|
|
557
|
+
NaNs are treated as equal if they are in the same place and if ``equal_nan=True``.
|
|
558
|
+
Infs are treated as equal if they are in the same place and of the same sign in both
|
|
559
|
+
arrays.
|
|
560
|
+
|
|
561
|
+
Parameters
|
|
562
|
+
----------
|
|
563
|
+
a, b : Array | int | float | complex | bool
|
|
564
|
+
Input objects to compare. At least one must be an array.
|
|
565
|
+
rtol : array_like, optional
|
|
566
|
+
The relative tolerance parameter (see Notes).
|
|
567
|
+
atol : array_like, optional
|
|
568
|
+
The absolute tolerance parameter (see Notes).
|
|
569
|
+
equal_nan : bool, optional
|
|
570
|
+
Whether to compare NaN's as equal. If True, NaN's in `a` will be considered
|
|
571
|
+
equal to NaN's in `b` in the output array.
|
|
572
|
+
xp : array_namespace, optional
|
|
573
|
+
The standard-compatible namespace for `a` and `b`. Default: infer.
|
|
574
|
+
|
|
575
|
+
Returns
|
|
576
|
+
-------
|
|
577
|
+
Array
|
|
578
|
+
A boolean array of shape broadcasted from `a` and `b`, containing ``True`` where
|
|
579
|
+
`a` is close to `b`, and ``False`` otherwise.
|
|
580
|
+
|
|
581
|
+
Warnings
|
|
582
|
+
--------
|
|
583
|
+
The default `atol` is not appropriate for comparing numbers with magnitudes much
|
|
584
|
+
smaller than one (see notes).
|
|
585
|
+
|
|
586
|
+
See Also
|
|
587
|
+
--------
|
|
588
|
+
math.isclose : Similar function in stdlib for Python scalars.
|
|
589
|
+
|
|
590
|
+
Notes
|
|
591
|
+
-----
|
|
592
|
+
For finite values, `isclose` uses the following equation to test whether two
|
|
593
|
+
floating point values are equivalent::
|
|
594
|
+
|
|
595
|
+
absolute(a - b) <= (atol + rtol * absolute(b))
|
|
596
|
+
|
|
597
|
+
Unlike the built-in `math.isclose`,
|
|
598
|
+
the above equation is not symmetric in `a` and `b`,
|
|
599
|
+
so that ``isclose(a, b)`` might be different from ``isclose(b, a)`` in some rare
|
|
600
|
+
cases.
|
|
601
|
+
|
|
602
|
+
The default value of `atol` is not appropriate when the reference value `b` has
|
|
603
|
+
magnitude smaller than one. For example, it is unlikely that ``a = 1e-9`` and
|
|
604
|
+
``b = 2e-9`` should be considered "close", yet ``isclose(1e-9, 2e-9)`` is ``True``
|
|
605
|
+
with default settings. Be sure to select `atol` for the use case at hand, especially
|
|
606
|
+
for defining the threshold below which a non-zero value in `a` will be considered
|
|
607
|
+
"close" to a very small or zero value in `b`.
|
|
608
|
+
|
|
609
|
+
The comparison of `a` and `b` uses standard broadcasting, which means that `a` and
|
|
610
|
+
`b` need not have the same shape in order for ``isclose(a, b)`` to evaluate to
|
|
611
|
+
``True``.
|
|
612
|
+
|
|
613
|
+
`isclose` is not defined for non-numeric data types.
|
|
614
|
+
``bool`` is considered a numeric data-type for this purpose.
|
|
615
|
+
"""
|
|
616
|
+
|
|
617
|
+
def pad(
|
|
618
|
+
self,
|
|
619
|
+
x: _NAMESPACE_ARRAY,
|
|
620
|
+
pad_width: Union[int, Tuple[int, int], Sequence[Tuple[int, int]]],
|
|
621
|
+
mode: Literal["constant"] = "constant",
|
|
622
|
+
*,
|
|
623
|
+
constant_values: Union[int, float, complex] = 0,
|
|
624
|
+
) -> _NAMESPACE_ARRAY:
|
|
625
|
+
"""
|
|
626
|
+
Pad the input array.
|
|
627
|
+
|
|
628
|
+
Parameters
|
|
629
|
+
----------
|
|
630
|
+
x : array
|
|
631
|
+
Input array.
|
|
632
|
+
pad_width : int or tuple of ints or sequence of pairs of ints
|
|
633
|
+
Pad the input array with this many elements from each side.
|
|
634
|
+
If a sequence of tuples, ``[(before_0, after_0), ... (before_N, after_N)]``,
|
|
635
|
+
each pair applies to the corresponding axis of ``x``.
|
|
636
|
+
A single tuple, ``(before, after)``, is equivalent to a list of ``x.ndim``
|
|
637
|
+
copies of this tuple.
|
|
638
|
+
mode : str, optional
|
|
639
|
+
Only "constant" mode is currently supported, which pads with
|
|
640
|
+
the value passed to `constant_values`.
|
|
641
|
+
constant_values : python scalar, optional
|
|
642
|
+
Use this value to pad the input. Default is zero.
|
|
643
|
+
xp : array_namespace, optional
|
|
644
|
+
The standard-compatible namespace for `x`. Default: infer.
|
|
645
|
+
|
|
646
|
+
Returns
|
|
647
|
+
-------
|
|
648
|
+
array
|
|
649
|
+
The input array,
|
|
650
|
+
padded with ``pad_width`` elements equal to ``constant_values``.
|
|
651
|
+
"""
|