Skip to content

Commit 2d91b04

Browse files
committed
Change utils. Add docstrings
1 parent 04fbb1b commit 2d91b04

File tree

4 files changed

+227
-11
lines changed

4 files changed

+227
-11
lines changed

arrayfire/array_api/array_object.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from arrayfire.algorithm import count # TODO refactor
1111
from arrayfire.array import _get_indices, _in_display_dims_limit # TODO refactor
1212

13+
from .device import PointerSource
1314
from .dtypes import CShape, Dtype
1415
from .dtypes import bool as af_bool
1516
from .dtypes import c_dim_t
@@ -20,7 +21,6 @@
2021
from .dtypes import int64 as af_int64
2122
from .dtypes import supported_dtypes
2223
from .dtypes import uint64 as af_uint64
23-
from .utils import PointerSource, to_str
2424

2525
ShapeType = tuple[int, ...]
2626
_bcast_var = False # HACK, TODO replace for actual bcast_var after refactoring
@@ -632,7 +632,7 @@ def _array_as_str(array: Array) -> str:
632632
arr_str = ctypes.c_char_p(0)
633633
# FIXME add description to passed arguments
634634
safe_call(backend.get().af_array_to_string(ctypes.pointer(arr_str), "", array.arr, 4, True))
635-
py_str = to_str(arr_str)
635+
py_str = _to_str(arr_str)
636636
safe_call(backend.get().af_free_host(arr_str))
637637
return py_str
638638

@@ -662,6 +662,10 @@ def _c_api_value_to_dtype(value: int) -> Dtype:
662662
raise TypeError("There is no supported dtype that matches passed dtype C API value.")
663663

664664

665+
def _to_str(c_str: ctypes.c_char_p) -> str:
666+
return str(c_str.value.decode("utf-8")) # type: ignore[union-attr]
667+
668+
665669
def _str_to_dtype(value: int) -> Dtype:
666670
for dtype in supported_dtypes:
667671
if value == dtype.typecode or value == dtype.typename:

arrayfire/array_api/device.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import enum
2+
3+
4+
class PointerSource(enum.Enum):
5+
"""
6+
Source of the pointer.
7+
"""
8+
# FIXME
9+
device = 0
10+
host = 1
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
from .array_object import Array
2+
from .dtypes import Dtype
3+
4+
# TODO implement functions
5+
6+
7+
def astype(x: Array, dtype: Dtype, /, *, copy: bool = True) -> Array:
8+
"""
9+
Copies an array to a specified data type irrespective of Type Promotion Rules rules.
10+
11+
Parameters
12+
----------
13+
x : Array
14+
Array to cast.
15+
dtype: Dtype
16+
Desired data type.
17+
copy: bool, optional
18+
Specifies whether to copy an array when the specified dtype matches the data type of the input array x.
19+
If True, a newly allocated array must always be returned. If False and the specified dtype matches the data
20+
type of the input array, the input array must be returned; otherwise, a newly allocated array must be returned.
21+
Default: True.
22+
23+
Returns
24+
-------
25+
out : Array
26+
An array having the specified data type. The returned array must have the same shape as x.
27+
28+
Note
29+
----
30+
- Casting floating-point NaN and infinity values to integral data types is not specified and is
31+
implementation-dependent.
32+
- Casting a complex floating-point array to a real-valued data type should not be permitted.
33+
Historically, when casting a complex floating-point array to a real-valued data type, libraries such as NumPy have
34+
discarded imaginary components such that, for a complex floating-point array x, astype(x) equals astype(real(x))).
35+
This behavior is considered problematic as the choice to discard the imaginary component is arbitrary and
36+
introduces more than one way to achieve the same outcome (i.e., for a complex floating-point array x, astype(x) and
37+
astype(real(x)) versus only astype(imag(x))). Instead, in order to avoid ambiguity and to promote clarity, this
38+
specification requires that array API consumers explicitly express which component should be cast to a specified
39+
real-valued data type.
40+
- When casting a boolean input array to a real-valued data type, a value of True must cast to a real-valued number
41+
equal to 1, and a value of False must cast to a real-valued number equal to 0.
42+
When casting a boolean input array to a complex floating-point data type, a value of True must cast to a complex
43+
number equal to 1 + 0j, and a value of False must cast to a complex number equal to 0 + 0j.
44+
- When casting a real-valued input array to bool, a value of 0 must cast to False, and a non-zero value must cast
45+
to True.
46+
When casting a complex floating-point array to bool, a value of 0 + 0j must cast to False, and all other values
47+
must cast to True.
48+
"""
49+
return NotImplemented
50+
51+
52+
def can_cast(from_: Dtype | Array, to: Dtype, /) -> bool:
53+
"""
54+
Determines if one data type can be cast to another data type according Type Promotion Rules rules.
55+
56+
Parameters
57+
----------
58+
from_ : Dtype | Array
59+
Input data type or array from which to cast.
60+
to : Dtype
61+
Desired data type.
62+
63+
Returns
64+
-------
65+
out : bool
66+
True if the cast can occur according to Type Promotion Rules rules; otherwise, False.
67+
"""
68+
return NotImplemented
69+
70+
71+
def finfo(type: Dtype | Array, /): # type: ignore[no-untyped-def]
72+
# TODO add docstring, implementation and return type -> finfo_object
73+
return NotImplemented
74+
75+
76+
def iinfo(type: Dtype | Array, /): # type: ignore[no-untyped-def]
77+
# TODO add docstring, implementation and return type -> iinfo_object
78+
return NotImplemented
79+
80+
81+
def isdtype(dtype: Dtype, kind: Dtype | str | tuple[Dtype | str, ...]) -> bool:
82+
"""
83+
Returns a boolean indicating whether a provided dtype is of a specified data type “kind”.
84+
85+
Parameters
86+
----------
87+
dtype : Dtype
88+
The input dtype.
89+
kind : Dtype | str | tuple[Dtype | str, ...]
90+
Data type kind.
91+
- If kind is a dtype, the function must return a boolean indicating whether the input dtype is equal to the
92+
dtype specified by kind.
93+
- If kind is a string, the function must return a boolean indicating whether the input dtype is of a specified
94+
data type kind. The following dtype kinds must be supported:
95+
- bool: boolean data types (e.g., bool).
96+
- signed integer: signed integer data types (e.g., int8, int16, int32, int64).
97+
- unsigned integer: unsigned integer data types (e.g., uint8, uint16, uint32, uint64).
98+
- integral: integer data types. Shorthand for ('signed integer', 'unsigned integer').
99+
- real floating: real-valued floating-point data types (e.g., float32, float64).
100+
- complex floating: complex floating-point data types (e.g., complex64, complex128).
101+
- numeric: numeric data types. Shorthand for ('integral', 'real floating', 'complex floating').
102+
- If kind is a tuple, the tuple specifies a union of dtypes and/or kinds, and the function must return a
103+
boolean indicating whether the input dtype is either equal to a specified dtype or belongs to at least one
104+
specified data type kind.
105+
106+
Returns
107+
-------
108+
out : bool
109+
Boolean indicating whether a provided dtype is of a specified data type kind.
110+
111+
Note
112+
----
113+
- A conforming implementation of the array API standard is not limited to only including the dtypes described in
114+
this specification in the required data type kinds. For example, implementations supporting float16 and bfloat16
115+
can include float16 and bfloat16 in the real floating data type kind. Similarly, implementations supporting int128
116+
can include int128 in the signed integer data type kind.
117+
In short, conforming implementations may extend data type kinds; however, data type kinds must remain consistent
118+
(e.g., only integer dtypes may belong to integer data type kinds and only floating-point dtypes may belong to
119+
floating-point data type kinds), and extensions must be clearly documented as such in library documentation.
120+
"""
121+
return NotImplemented
122+
123+
124+
def result_type(*arrays_and_dtypes: Dtype | Array) -> Dtype:
125+
"""
126+
Returns the dtype that results from applying the type promotion rules (see Type Promotion Rules) to the arguments.
127+
128+
Parameters
129+
----------
130+
arrays_and_dtypes: Dtype | Array
131+
An arbitrary number of input arrays and/or dtypes.
132+
133+
Returns
134+
-------
135+
out : Dtype
136+
The dtype resulting from an operation involving the input arrays and dtypes.
137+
"""
138+
return NotImplemented

arrayfire/array_api/utils.py

Lines changed: 73 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,79 @@
1-
import ctypes
2-
import enum
1+
from .array_object import Array
32

3+
# TODO implement functions
44

5-
class PointerSource(enum.Enum):
5+
6+
def all(x: Array, /, *, axis: None | int | tuple[int, ...] = None, keepdims: bool = False) -> Array:
67
"""
7-
Source of the pointer
8+
Tests whether all input array elements evaluate to True along a specified axis.
9+
10+
Parameters
11+
----------
12+
x : Array
13+
Input array.
14+
axis : None | int | tuple[int, ...], optional
15+
Axis or axes along which to perform a logical AND reduction. By default, a logical AND reduction must be
16+
performed over the entire array. If a tuple of integers, logical AND reductions must be performed over
17+
multiple axes. A valid axis must be an integer on the interval [-N, N), where N is the rank (number of
18+
dimensions) of x. If an axis is specified as a negative integer, the function must determine the axis along
19+
which to perform a reduction by counting backward from the last dimension (where -1 refers to the last
20+
dimension). If provided an invalid axis, the function must raise an exception. Default: None.
21+
keepdims : bool, optional
22+
If True, the reduced axes (dimensions) must be included in the result as singleton dimensions, and,
23+
accordingly, the result must be compatible with the input array (see Broadcasting). Otherwise, if False,
24+
the reduced axes (dimensions) must not be included in the result. Default: False.
25+
26+
Returns
27+
-------
28+
out : Array
29+
If a logical AND reduction was performed over the entire array, the returned array must be a zero-dimensional
30+
array containing the test result; otherwise, the returned array must be a non-zero-dimensional array
31+
containing the test results. The returned array must have a data type of bool.
32+
33+
Note
34+
----
35+
- Positive infinity, negative infinity, and NaN must evaluate to True.
36+
- If x has a complex floating-point data type, elements having a non-zero component (real or imaginary) must
37+
evaluate to True.
38+
- If x is an empty array or the size of the axis (dimension) along which to evaluate elements is zero, the test
39+
result must be True.
840
"""
9-
# FIXME
10-
device = 0
11-
host = 1
41+
return NotImplemented
1242

1343

14-
def to_str(c_str: ctypes.c_char_p) -> str:
15-
return str(c_str.value.decode("utf-8")) # type: ignore[union-attr]
44+
def any(x: Array, /, *, axis: None | int | tuple[int, ...] = None, keepdims: bool = False) -> Array:
45+
"""
46+
Tests whether any input array element evaluates to True along a specified axis.
47+
48+
Parameters
49+
----------
50+
x : Array
51+
Input array.
52+
axis : None | int | tuple[int, ...], optional
53+
Axis or axes along which to perform a logical OR reduction. By default, a logical OR reduction must be
54+
performed over the entire array. If a tuple of integers, logical OR reductions must be performed over
55+
multiple axes. A valid axis must be an integer on the interval [-N, N), where N is the rank (number of
56+
dimensions) of x. If an axis is specified as a negative integer, the function must determine the axis along
57+
which to perform a reduction by counting backward from the last dimension (where -1 refers to the last
58+
dimension). If provided an invalid axis, the function must raise an exception. Default: None.
59+
keepdims : bool, optional
60+
If True, the reduced axes (dimensions) must be included in the result as singleton dimensions, and,
61+
accordingly, the result must be compatible with the input array (see Broadcasting). Otherwise, if False,
62+
the reduced axes (dimensions) must not be included in the result. Default: False.
63+
64+
Returns
65+
-------
66+
out : Array
67+
If a logical OR reduction was performed over the entire array, the returned array must be a zero-dimensional
68+
array containing the test result; otherwise, the returned array must be a non-zero-dimensional array
69+
containing the test results. The returned array must have a data type of bool.
70+
71+
Note
72+
----
73+
- Positive infinity, negative infinity, and NaN must evaluate to True.
74+
- If x has a complex floating-point data type, elements having a non-zero component (real or imaginary) must
75+
evaluate to True.
76+
- If x is an empty array or the size of the axis (dimension) along which to evaluate elements is zero, the test
77+
result must be False.
78+
"""
79+
return NotImplemented

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy