Repository navigation
Expand file tree
/
Copy path_creation_functions.py
More file actions
394 lines (319 loc) · 11.4 KB
/
Copy path_creation_functions.py
File metadata and controls
394 lines (319 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
"""Creation functions."""
from __future__ import annotations
from typing import TYPE_CHECKING, Protocol
from array_api._namespace import get_namespace
if TYPE_CHECKING:
from array_api._array import Array
from array_api._device import Device
from array_api._dtype import DType
from array_api._types import NestedSequence, SupportsBufferProtocol
__all__ = [
"empty_like",
"full_like",
"meshgrid",
"ones_like",
"tril",
"triu",
"zeros_like",
]
def empty_like(
x: Array, /, *, dtype: DType | None = None, device: Device | None = None
) -> Array:
"""
Returns an uninitialized array with the same ``shape`` as an input array
``x``.
Parameters
----------
x: array
input array from which to derive the output array shape.
dtype: dype or None
output array data type. If ``dtype`` is ``None``, the output array data
type must be inferred from ``x``. Default: ``None``.
device: device or None
device on which to place the created array. If ``device`` is ``None``,
the output array device must be inferred from ``x``. Default: ``None``.
Returns
-------
out: array
an array having the same shape as ``x`` and containing uninitialized
data.
"""
return get_namespace(x).empty_like(x, dtype=dtype, device=device)
def full_like(
x: Array,
/,
fill_value: float,
*,
dtype: DType | None = None,
device: Device | None = None,
) -> Array:
"""
Returns a new array filled with ``fill_value`` and having the same ``shape``
as an input array ``x``.
Parameters
----------
x: array
input array from which to derive the output array shape.
fill_value: int or float
fill value.
dtype: dype or None
output array data type. If ``dtype`` is ``None``, the output array data
type must be inferred from ``x``. Default: ``None``.
.. note::
If the ``fill_value`` exceeds the precision of the resolved output
array data type, behavior is unspecified and, thus,
implementation-defined.
.. note::
If the ``fill_value`` has a data type (``int`` or ``float``) which
is not of the same data type kind as the resolved output array data
type (see :ref:`type-promotion`), behavior is unspecified and, thus,
implementation-defined.
device: device or None
device on which to place the created array. If ``device`` is ``None``,
the output array device must be inferred from ``x``. Default: ``None``.
Returns
-------
out: array
an array having the same shape as ``x`` and where every element is equal
to ``fill_value``.
"""
return get_namespace(x).full_like(
x, fill_value=fill_value, dtype=dtype, device=device
)
def meshgrid(*arrays: Array, indexing: str = "xy") -> list[Array]:
"""
Returns coordinate matrices from coordinate vectors.
Parameters
----------
arrays: array
an arbitrary number of one-dimensional arrays representing grid
coordinates. Each array should have the same numeric data type.
indexing: str
Cartesian ``'xy'`` or matrix ``'ij'`` indexing of output. If provided
zero or one one-dimensional vector(s) (i.e., the zero- and
one-dimensional cases, respectively), the ``indexing`` keyword has no
effect and should be ignored. Default: ``'xy'``.
Returns
-------
out: List[array]
list of N arrays, where ``N`` is the number of provided one-dimensional
input arrays. Each returned array must have rank ``N``. For ``N``
one-dimensional arrays having lengths ``Ni = len(xi)``,
- if matrix indexing ``ij``, then each returned array must have the
shape ``(N1, N2, N3, ..., Nn)``. - if Cartesian indexing ``xy``, then
each returned array must have shape ``(N2, N1, N3, ..., Nn)``.
Accordingly, for the two-dimensional case with input one-dimensional
arrays of length ``M`` and ``N``, if matrix indexing ``ij``, then each
returned array must have shape ``(M, N)``, and, if Cartesian indexing
``xy``, then each returned array must have shape ``(N, M)``.
Similarly, for the three-dimensional case with input one-dimensional
arrays of length ``M``, ``N``, and ``P``, if matrix indexing ``ij``,
then each returned array must have shape ``(M, N, P)``, and, if
Cartesian indexing ``xy``, then each returned array must have shape
``(N, M, P)``.
Each returned array should have the same data type as the input arrays.
"""
return get_namespace(*arrays).meshgrid(*arrays, indexing=indexing)
def ones_like(
x: Array, /, *, dtype: DType | None = None, device: Device | None = None
) -> Array:
"""
Returns a new array filled with ones and having the same ``shape`` as an
input array ``x``.
Parameters
----------
x: array
input array from which to derive the output array shape.
dtype: dype or None
output array data type. If ``dtype`` is ``None``, the output array data
type must be inferred from ``x``. Default: ``None``.
device: device or None
device on which to place the created array. If ``device`` is ``None``,
the output array device must be inferred from ``x``. Default: ``None``.
Returns
-------
out: array
an array having the same shape as ``x`` and filled with ones.
"""
return get_namespace(x).ones_like(x, dtype=dtype, device=device)
def tril(x: Array, /, *, k: int = 0) -> Array:
"""
Returns the lower triangular part of a matrix (or a stack of matrices)
``x``.
.. note::
The lower triangular part of the matrix is defined as the elements on
and below the specified diagonal ``k``.
Parameters
----------
x: array
input array having shape ``(..., M, N)`` and whose innermost two
dimensions form ``MxN`` matrices.
k: int
diagonal above which to zero elements. If ``k = 0``, the diagonal is the
main diagonal. If ``k < 0``, the diagonal is below the main diagonal. If
``k > 0``, the diagonal is above the main diagonal. Default: ``0``.
.. note::
The main diagonal is defined as the set of indices ``{(i, i)}`` for
``i`` on the interval ``[0, min(M, N) - 1]``.
Returns
-------
out: array
an array containing the lower triangular part(s). The returned array
must have the same shape and data type as ``x``. All elements above the
specified diagonal ``k`` must be zeroed. The returned array should be
allocated on the same device as ``x``.
"""
return get_namespace(x).tril(x, k=k)
def triu(x: Array, /, *, k: int = 0) -> Array:
"""
Returns the upper triangular part of a matrix (or a stack of matrices)
``x``.
.. note::
The upper triangular part of the matrix is defined as the elements on
and above the specified diagonal ``k``.
Parameters
----------
x: array
input array having shape ``(..., M, N)`` and whose innermost two
dimensions form ``MxN`` matrices.
k: int
diagonal below which to zero elements. If ``k = 0``, the diagonal is the
main diagonal. If ``k < 0``, the diagonal is below the main diagonal. If
``k > 0``, the diagonal is above the main diagonal. Default: ``0``.
.. note::
The main diagonal is defined as the set of indices ``{(i, i)}`` for
``i`` on the interval ``[0, min(M, N) - 1]``.
Returns
-------
out: array
an array containing the upper triangular part(s). The returned array
must have the same shape and data type as ``x``. All elements below the
specified diagonal ``k`` must be zeroed. The returned array should be
allocated on the same device as ``x``.
"""
return get_namespace(x).triu(x, k=k)
def zeros_like(
x: Array, /, *, dtype: DType | None = None, device: Device | None = None
) -> Array:
"""
Returns a new array filled with zeros and having the same ``shape`` as an
input array ``x``.
Parameters
----------
x: array
input array from which to derive the output array shape.
dtype: dype or None
output array data type. If ``dtype`` is ``None``, the output array data
type must be inferred from ``x``. Default: ``None``.
device: device or None
device on which to place the created array. If ``device`` is ``None``,
the output array device must be inferred from ``x``. Default: ``None``.
Returns
-------
out: array
an array having the same shape as ``x`` and filled with zeros.
"""
return get_namespace(x).zeros_like(x, dtype=dtype, device=device)
###############################################################################
class HasCreationFunctions(Protocol):
@staticmethod
def arange(
start: float,
/,
stop: float | None = None,
step: float = 1,
*,
dtype: DType | None = None,
device: Device | None = None,
) -> Array: ...
@staticmethod
def asarray(
obj: (
Array
| bool
| float
| NestedSequence[bool | float]
| SupportsBufferProtocol
),
/,
*,
dtype: DType | None = None,
device: Device | None = None,
copy: bool | None = None,
) -> Array: ...
@staticmethod
def empty_like(
x: Array, /, *, dtype: DType | None = None, device: Device | None = None
) -> Array: ...
@staticmethod
def eye(
n_rows: int,
n_cols: int | None = None,
/,
*,
k: int = 0,
dtype: DType | None = None,
device: Device | None = None,
) -> Array: ...
@staticmethod
def from_dlpack(x: object, /) -> Array: ...
@staticmethod
def full(
shape: int | tuple[int, ...],
fill_value: float,
*,
dtype: DType | None = None,
device: Device | None = None,
) -> Array: ...
@staticmethod
def full_like(
x: Array,
/,
fill_value: float,
*,
dtype: DType | None = None,
device: Device | None = None,
) -> Array: ...
@staticmethod
def linspace(
start: float,
stop: float,
/,
num: int,
*,
dtype: DType | None = None,
device: Device | None = None,
endpoint: bool = True,
) -> Array: ...
@staticmethod
def meshgrid(*arrays: Array, indexing: str = "xy") -> list[Array]: ...
@staticmethod
def ones(
shape: int | tuple[int, ...],
*,
dtype: DType | None = None,
device: Device | None = None,
) -> Array: ...
@staticmethod
def ones_like(
x: Array,
/,
*,
dtype: DType | None = None,
device: Device | None = None,
) -> Array: ...
@staticmethod
def tril(x: Array, /, *, k: int = 0) -> Array: ...
@staticmethod
def triu(x: Array, /, *, k: int = 0) -> Array: ...
@staticmethod
def zeros(
shape: int | tuple[int, ...],
*,
dtype: DType | None = None,
device: Device | None = None,
) -> Array: ...
@staticmethod
def zeros_like(
x: Array, /, *, dtype: DType | None = None, device: Device | None = None
) -> Array: ...