Repository navigation
Expand file tree
/
Copy path_utility_functions.py
More file actions
146 lines (118 loc) · 4.88 KB
/
Copy path_utility_functions.py
File metadata and controls
146 lines (118 loc) · 4.88 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
"""Sorting 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._types import AxisT
__all__ = ["all", "any"]
def all(
x: Array,
/,
*,
axis: int | tuple[int, ...] | None = None,
keepdims: bool = False,
) -> Array:
"""
Tests whether all input array elements evaluate to ``True`` along a
specified axis.
.. note::
Positive infinity, negative infinity, and NaN must evaluate to ``True``.
.. note::
If ``x`` is an empty array or the size of the axis (dimension) along
which to evaluate elements is zero, the test result must be ``True``.
Parameters
----------
x: array
input array.
axis: Optional[int | tuple[int, ...]]
axis or axes along which to perform a logical AND reduction. By default,
a logical AND reduction must be performed over the entire array. If a
tuple of integers, logical AND reductions must be performed over
multiple axes. A valid ``axis`` must be an integer on the interval
``[-N, N)``, where ``N`` is the rank (number of dimensions) of ``x``. If
an ``axis`` is specified as a negative integer, the function must
determine the axis along which to perform a reduction by counting
backward from the last dimension (where ``-1`` refers to the last
dimension). If provided an invalid ``axis``, the function must raise an
exception. Default: ``None``.
keepdims: bool
If ``True``, the reduced axes (dimensions) must be included in the
result as singleton dimensions, and, accordingly, the result must be
compatible with the input array (see :ref:`broadcasting`). Otherwise, if
``False``, the reduced axes (dimensions) must not be included in the
result. Default: ``False``.
Returns
-------
out: array
if a logical AND reduction was performed over the entire array, the
returned array must be a zero-dimensional array containing the test
result; otherwise, the returned array must be a non-zero-dimensional
array containing the test results. The returned array must have a data
type of ``bool``.
"""
return get_namespace(x).all(x, axis=axis, keepdims=keepdims)
def any(
x: Array,
/,
*,
axis: int | tuple[int, ...] | None = None,
keepdims: bool = False,
) -> Array:
"""
Tests whether any input array element evaluates to ``True`` along a
specified axis.
.. note::
Positive infinity, negative infinity, and NaN must evaluate to ``True``.
.. note::
If ``x`` is an empty array or the size of the axis (dimension) along
which to evaluate elements is zero, the test result must be ``False``.
Parameters
----------
x: array
input array.
axis: Optional[int | tuple[int, ...]]
axis or axes along which to perform a logical OR reduction. By default,
a logical OR reduction must be performed over the entire array. If a
tuple of integers, logical OR reductions must be performed over multiple
axes. A valid ``axis`` must be an integer on the interval ``[-N, N)``,
where ``N`` is the rank (number of dimensions) of ``x``. If an ``axis``
is specified as a negative integer, the function must determine the axis
along which to perform a reduction by counting backward from the last
dimension (where ``-1`` refers to the last dimension). If provided an
invalid ``axis``, the function must raise an exception. Default:
``None``.
keepdims: bool
If ``True``, the reduced axes (dimensions) must be included in the
result as singleton dimensions, and, accordingly, the result must be
compatible with the input array (see :ref:`broadcasting`). Otherwise, if
``False``, the reduced axes (dimensions) must not be included in the
result. Default: ``False``.
Returns
-------
out: array
if a logical OR reduction was performed over the entire array, the
returned array must be a zero-dimensional array containing the test
result; otherwise, the returned array must be a non-zero-dimensional
array containing the test results. The returned array must have a data
type of ``bool``.
"""
return get_namespace(x).any(x, axis=axis, keepdims=keepdims)
####################################################################################################
class HasUtilityFunctions(Protocol):
@staticmethod
def all(
x: Array,
/,
*,
axis: AxisT = None,
keepdims: bool = False,
) -> Array: ...
@staticmethod
def any(
x: Array,
/,
*,
axis: AxisT = None,
keepdims: bool = False,
) -> Array: ...