/
data_type.py
324 lines (269 loc) · 9.62 KB
/
data_type.py
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
# global
import abc
from typing import Tuple, Optional, List, Union
# local
import ivy
Finfo = None
Iinfo = None
class _ArrayWithDataTypes(abc.ABC):
def astype(
self: ivy.Array,
dtype: ivy.Dtype,
/,
*,
copy: bool = True,
out: Optional[ivy.Array] = None,
) -> ivy.Array:
"""Copy 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
----------
self
array to cast.
dtype
desired data type.
copy
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``.
out
optional output array, for writing the result to. It must have a shape
that the inputs broadcast to.
Returns
-------
ret
an array having the specified data type. The returned array must have
the same shape as ``x``.
Examples
--------
Using :class:`ivy.Array` instance method:
>>> x = ivy.array([[-1, -2], [0, 2]])
>>> print(x.astype(ivy.float64))
ivy.array([[-1., -2.], [0., 2.]])
"""
return ivy.astype(self._data, dtype, copy=copy, out=out)
def broadcast_arrays(
self: ivy.Array, *arrays: Union[ivy.Array, ivy.NativeArray]
) -> List[ivy.Array]:
"""`ivy.Array` instance method variant of `ivy.broadcast_arrays`. This
method simply wraps the function, and so the docstring for
`ivy.broadcast_arrays` also applies to this method with minimal
changes.
Parameters
----------
self
An input array to be broadcasted against other input arrays.
arrays
an arbitrary number of arrays to-be broadcasted.
Each array must have the same shape.
Each array must have the same dtype as its
corresponding input array.
Returns
-------
ret
A list containing broadcasted arrays of type `ivy.Array`
Examples
--------
With :class:`ivy.Array` inputs:
>>> x1 = ivy.array([1, 2])
>>> x2 = ivy.array([0.2, 0.])
>>> x3 = ivy.zeros(2)
>>> y = x1.broadcast_arrays(x2, x3)
>>> print(y)
[ivy.array([1, 2]), ivy.array([0.2, 0. ]), ivy.array([0., 0.])]
With mixed :class:`ivy.Array` and :class:`ivy.NativeArray` inputs:
>>> x1 = ivy.array([-1., 3.4])
>>> x2 = ivy.native_array([2.4, 5.1])
>>> y = x1.broadcast_arrays(x2)
>>> print(y)
[ivy.array([-1., 3.4]), ivy.array([2.4, 5.1])]
"""
return ivy.broadcast_arrays(self._data, *arrays)
def broadcast_to(
self: ivy.Array, /, shape: Tuple[int, ...], *, out: Optional[ivy.Array] = None
) -> ivy.Array:
"""`ivy.Array` instance method variant of `ivy.broadcast_to`. This
method simply wraps the function, and so the docstring for
`ivy.broadcast_to` also applies to this method with minimal changes.
Parameters
----------
self
input array to be broadcasted.
shape
desired shape to be broadcasted to.
out
Optional array to store the broadcasted array.
Returns
-------
ret
Returns the broadcasted array of shape 'shape'
Examples
--------
With :class:`ivy.Array` instance method:
>>> x = ivy.array([1, 2, 3])
>>> y = x.broadcast_to((3,3))
>>> print(y)
ivy.array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]])
"""
return ivy.broadcast_to(self._data, shape=shape, out=out)
def can_cast(self: ivy.Array, to: ivy.Dtype) -> bool:
"""`ivy.Array` instance method variant of `ivy.can_cast`. This method
simply wraps the function, and so the docstring for `ivy.can_cast` also
applies to this method with minimal changes.
Parameters
----------
self
input array from which to cast.
to
desired data type.
Returns
-------
ret
``True`` if the cast can occur according to :ref:`type-promotion` rules;
otherwise, ``False``.
Examples
--------
>>> x = ivy.array([1., 2., 3.])
>>> print(x.dtype)
float32
>>> x = ivy.array([4., 5., 6.])
>>> print(x.can_cast(ivy.float64))
True
"""
return ivy.can_cast(self._data, to)
def dtype(
self: ivy.Array, as_native: bool = False
) -> Union[ivy.Dtype, ivy.NativeDtype]:
"""`ivy.Array` instance method variant of `ivy.dtype`. This method
helps to get the data type of the array.
Parameters
----------
self
The input array.
as_native
Whether to return the native data type of the array.
If True, returns the native data type. Default is False.
Returns
-------
ret
The data type of the array. If as_native is True,
returns the native data type.
Examples
--------
>>> x = ivy.array([1, 2, 3])
>>> y = x.dtype()
>>> print(y)
int32
>>> x= ivy.array([1.0, 2.0, 3.0], dtype=ivy.float64)
>>> y = x.dtype(as_native=True)
>>> print(y)
float64
"""
return ivy.dtype(self._data, as_native=as_native)
def finfo(self: ivy.Array, /) -> Finfo:
"""Array instance method variant of `ivy.finfo`.
Parameters
----------
self
input array.
Returns
-------
ret
An instance of the `Finfo` class, containing information
about the floating point data type of the input array.
Example
-------
>>> x = ivy.array([0.7,8.4,3.14], dtype=ivy.float32)
>>> print(x.finfo())
finfo(resolution=1e-06, min=-3.4028235e+38, max=3.4028235e+38, dtype=float32)
"""
return ivy.finfo(self._data)
def iinfo(self: ivy.Array, /) -> Iinfo:
"""`ivy.Array` instance method variant of `ivy.iinfo`. This method
simply wraps the function, and so the docstring for `ivy.iinfo` also
applies to this method with minimal changes.
Parameters
----------
self
input array.
Returns
-------
ret
An instance of the `Iinfo` class, containing information
about the integer data type of the input array.
Examples
--------
>>> x = ivy.array([-119,122,14], dtype=ivy.int8))
>>> x.iinfo()
iinfo(min=-128, max=127, dtype=int8)
>>> x = ivy.array([-12,54,1,9,-1220], dtype=ivy.int16))
>>> x.iinfo()
iinfo(min=-32768, max=32767, dtype=int16)
"""
return ivy.iinfo(self._data)
def is_bool_dtype(self: ivy.Array) -> bool:
return ivy.is_bool_dtype(self._data)
def is_float_dtype(self: ivy.Array) -> bool:
"""`ivy.Array` instance method variant of `ivy.is_float_dtype`. This
method simply checks to see if the array is of type `float`.
Parameters
----------
self
Input array from which to check for float dtype.
Returns
-------
ret
Boolean value of whether the array is of type `float`.
Examples
--------
>>> x = ivy.array([1, 2, 3], dtype=ivy.int8)
>>> print(x.is_float_dtype())
False
>>> x = ivy.array([2.3, 4.5, 6.8], dtype=ivy.float32)
>>> print( x.is_float_dtype())
True
"""
return ivy.is_float_dtype(self._data)
def is_int_dtype(self: ivy.Array) -> bool:
return ivy.is_int_dtype(self._data)
def is_uint_dtype(self: ivy.Array) -> bool:
return ivy.is_uint_dtype(self._data)
def result_type(
self: ivy.Array,
*arrays_and_dtypes: Union[ivy.Array, ivy.NativeArray, ivy.Dtype],
) -> ivy.Dtype:
"""`ivy.Array` instance method variant of `ivy.result_type`. This
method simply wraps the function, and so the docstring for
`ivy.result_type` also applies to this method with minimal changes.
Parameters
----------
self
input array from which to cast.
arrays_and_dtypes
an arbitrary number of input arrays and/or dtypes.
Returns
-------
ret
the dtype resulting from an operation involving the input arrays and dtypes.
Examples
--------
>>> x = ivy.array([0, 1, 2])
>>> print(x.dtype)
int32
>>> x.result_type(ivy.float64)
<dtype:'float64'>
"""
return ivy.result_type(self._data, *arrays_and_dtypes)