Repository navigation
Expand file tree
/
Copy path_data_type_functions.py
More file actions
129 lines (92 loc) · 3.62 KB
/
Copy path_data_type_functions.py
File metadata and controls
129 lines (92 loc) · 3.62 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
"""Data type functions."""
from __future__ import annotations
from typing import TYPE_CHECKING, Any, Final, Protocol
from array_api._namespace import get_namespace
if TYPE_CHECKING:
from array_api._array import Array
from array_api._dtype import DType
from array_api._types import finfo_object, iinfo_object
__all__ = ["astype", "broadcast_arrays", "broadcast_to"]
_EMPTY_DICT: Final[dict[str, Any]] = {}
def astype(x: Array, dtype: DType, /, *, copy: bool = True) -> Array:
"""
Copies an array to a specified data type irrespective of
:ref:`type-promotion` rules.
.. note::
Casting floating-point ``NaN`` and ``infinity`` values to integral data
types is not specified and is implementation-dependent.
.. note::
When casting a boolean input array to a numeric data type, a value of
``True`` must cast to a numeric value equal to ``1``, and a value of
``False`` must cast to a numeric value equal to ``0``.
When casting a numeric input array to ``bool``, a value of ``0`` must
cast to ``False``, and a non-zero value must cast to ``True``.
Parameters
----------
x: array
array to cast.
dtype: dtype
desired data type.
copy: bool
specifies whether to copy an array when the specified ``dtype`` matches
the data type of the input array ``x``. If ``True``, a newly allocated
array must always be returned. If ``False`` and the specified ``dtype``
matches the data type of the input array, the input array must be
returned; otherwise, a newly allocated must be returned. Default:
``True``.
Returns
-------
out: array
an array having the specified data type. The returned array must have
the same shape as ``x``.
"""
return get_namespace(x).astype(x, dtype, copy=copy)
def broadcast_arrays(*arrays: Array) -> list[Array]:
"""
Broadcasts one or more Arrays against one another.
Parameters
----------
arrays: array
an arbitrary number of to-be broadcasted arrays.
Returns
-------
out: List[array]
a list of broadcasted arrays. Each array must have the same shape.
Each array must have the same dtype as its corresponding input
array.
"""
return get_namespace(*arrays).broadcast_arrays(*arrays)
def broadcast_to(x: Array, /, shape: tuple[int, ...]) -> Array:
"""
Broadcasts an array to a specified shape.
Parameters
----------
x: array
array to broadcast.
shape: Tuple[int, ...]
array shape. Must be compatible with ``x`` (see
:ref:`broadcasting`). If the array is incompatible with the
specified shape, the function should raise an exception.
Returns
-------
out: array
an array having a specified shape. Must have the same data type as
``x``.
"""
return get_namespace(x).broadcast_to(x, shape=shape)
###############################################################################
class HasDataTypeFunctions(Protocol):
@staticmethod
def astype(x: Array, dtype: DType, /, *, copy: bool = True) -> Array: ...
@staticmethod
def broadcast_arrays(*arrays: Array) -> list[Array]: ...
@staticmethod
def broadcast_to(x: Array, /, shape: tuple[int, ...]) -> Array: ...
@staticmethod
def can_cast(from_: DType | Array, to: DType, /) -> bool: ...
@staticmethod
def finfo(type: DType | Array, /) -> finfo_object: ...
@staticmethod
def iinfo(type: DType | Array, /) -> iinfo_object: ...
@staticmethod
def result_type(*arrays_and_dtypes: Array | DType) -> DType: ...