Repository navigation
Expand file tree
/
Copy path_sorting_functions.py
More file actions
119 lines (99 loc) · 3.29 KB
/
Copy path_sorting_functions.py
File metadata and controls
119 lines (99 loc) · 3.29 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
"""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
__all__ = ["sort", "argsort"]
def argsort(
x: Array,
/,
*,
axis: int = -1,
descending: bool = False,
stable: bool = True,
) -> Array:
"""
Returns the indices that sort an array ``x`` along a specified axis.
Parameters
----------
x : array
input array.
axis: int
axis along which to sort. If set to ``-1``, the function must sort along
the last axis. Default: ``-1``.
descending: bool
sort order. If ``True``, the returned indices sort ``x`` in descending
order (by value). If ``False``, the returned indices sort ``x`` in
ascending order (by value). Default: ``False``.
stable: bool
sort stability. If ``True``, the returned indices must maintain the
relative order of ``x`` values which compare as equal. If ``False``, the
returned indices may or may not maintain the relative order of ``x``
values which compare as equal (i.e., the relative order of ``x`` values
which compare as equal is implementation-dependent). Default: ``True``.
Returns
-------
out : array
an array of indices. The returned array must have the same shape as
``x``. The returned array must have the default array index data type.
"""
return get_namespace(x).argsort(
x, axis=axis, descending=descending, stable=stable
)
def sort(
x: Array,
/,
*,
axis: int = -1,
descending: bool = False,
stable: bool = True,
) -> Array:
"""
Returns a sorted copy of an input array ``x``.
Parameters
----------
x: array
input array.
axis: int
axis along which to sort. If set to ``-1``, the function must sort along
the last axis. Default: ``-1``.
descending: bool
sort order. If ``True``, the array must be sorted in descending order
(by value). If ``False``, the array must be sorted in ascending order
(by value). Default: ``False``.
stable: bool
sort stability. If ``True``, the returned array must maintain the
relative order of ``x`` values which compare as equal. If ``False``, the
returned array may or may not maintain the relative order of ``x``
values which compare as equal (i.e., the relative order of ``x`` values
which compare as equal is implementation-dependent). Default: ``True``.
Returns
-------
out : array
a sorted array. The returned array must have the same data type and
shape as ``x``.
"""
return get_namespace(x).sort(
x, axis=axis, descending=descending, stable=stable
)
####################################################################################################
class HasSortingFunctions(Protocol):
@staticmethod
def argsort(
x: Array,
/,
*,
axis: int = -1,
descending: bool = False,
stable: bool = True,
) -> Array: ...
@staticmethod
def sort(
x: Array,
/,
*,
axis: int = -1,
descending: bool = False,
stable: bool = True,
) -> Array: ...