Skip to content

Commit e2abca0

Browse files
committed
Merge branch 'main' of https://github.com/pydata-apis/array-api into add-real
2 parents d51bdcd + eba54b3 commit e2abca0

38 files changed

+610
-330
lines changed

.github/workflows/pages.yml

+1-5
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ on:
3131
branches:
3232
- main
3333

34-
pull_request:
35-
branches:
36-
- "**"
37-
3834
# Workflow jobs:
3935
jobs:
4036

@@ -128,5 +124,5 @@ jobs:
128124
- name: 'Push changes'
129125
if: success()
130126
run: |
131-
git push "https://$GITHUB_ACTOR:[email protected]/${{ github.repository }}.git"
127+
git push origin gh-pages
132128
timeout-minutes: 10

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ node_modules/
3030
__pycache__/
3131
*.pyc
3232
spec/**/generated
33+
tmp/

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ myst-parser
44
sphinx_markdown_tables
55
sphinx_copybutton
66
docutils<0.18
7+
sphinx-math-dollar
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from .array_object import *
2+
from .constants import *
3+
from .creation_functions import *
4+
from .data_type_functions import *
5+
import array_api.data_types as dtype
6+
from .elementwise_functions import *
7+
from .linear_algebra_functions import *
8+
from .manipulation_functions import *
9+
from .searching_functions import *
10+
from .set_functions import *
11+
from .sorting_functions import *
12+
from .statistical_functions import *
13+
from .utility_functions import *
14+
from . import linalg

spec/API_specification/signatures/array_object.py renamed to spec/API_specification/array_api/array_object.py

+68-63
Large diffs are not rendered by default.

spec/API_specification/signatures/creation_functions.py renamed to spec/API_specification/array_api/creation_functions.py

+36-21
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def arange(start: Union[int, float], /, stop: Optional[Union[int, float]] = None
1414
step: Union[int, float]
1515
the distance between two adjacent elements (``out[i+1] - out[i]``). Must not be ``0``; may be negative, this results in an empty array if ``stop >= start``. Default: ``1``.
1616
dtype: Optional[dtype]
17-
output array data type. If ``dtype`` is ``None``, the output array data type must be inferred from ``start``, ``stop`` and ``step``. If those are all integers, the output array dtype must be the default integer dtype; if one or more have type ``float``, then the output array dtype must be the default floating-point data type. Default: ``None``.
17+
output array data type. If ``dtype`` is ``None``, the output array data type must be inferred from ``start``, ``stop`` and ``step``. If those are all integers, the output array dtype must be the default integer dtype; if one or more have type ``float``, then the output array dtype must be the default real-valued floating-point data type. Default: ``None``.
1818
device: Optional[device]
1919
device on which to place the created array. Default: ``None``.
2020
@@ -28,13 +28,13 @@ def arange(start: Union[int, float], /, stop: Optional[Union[int, float]] = None
2828
a one-dimensional array containing evenly spaced values. The length of the output array must be ``ceil((stop-start)/step)`` if ``stop - start`` and ``step`` have the same sign, and length ``0`` otherwise.
2929
"""
3030

31-
def asarray(obj: Union[array, bool, int, float, NestedSequence, SupportsBufferProtocol], /, *, dtype: Optional[dtype] = None, device: Optional[device] = None, copy: Optional[bool] = None) -> array:
32-
"""
31+
def asarray(obj: Union[array, bool, int, float, complex, NestedSequence, SupportsBufferProtocol], /, *, dtype: Optional[dtype] = None, device: Optional[device] = None, copy: Optional[bool] = None) -> array:
32+
r"""
3333
Convert the input to an array.
3434
3535
Parameters
3636
----------
37-
obj: Union[array, bool, int, float, NestedSequence[bool | int | float], SupportsBufferProtocol]
37+
obj: Union[array, bool, int, float, complex, NestedSequence[bool | int | float | complex], SupportsBufferProtocol]
3838
object to be converted to an array. May be a Python scalar, a (possibly nested) sequence of Python scalars, or an object supporting the Python buffer protocol.
3939
4040
.. admonition:: Tip
@@ -43,18 +43,19 @@ def asarray(obj: Union[array, bool, int, float, NestedSequence, SupportsBufferPr
4343
An object supporting the buffer protocol can be turned into a memoryview through ``memoryview(obj)``.
4444
4545
dtype: Optional[dtype]
46-
output array data type. If ``dtype`` is ``None``, the output array data type must be inferred from the data type(s) in ``obj``. If all input values are Python scalars, then
46+
output array data type. If ``dtype`` is ``None``, the output array data type must be inferred from the data type(s) in ``obj``. If all input values are Python scalars, then, in order of precedence,
4747
4848
- if all values are of type ``bool``, the output data type must be ``bool``.
49-
- if the values are a mixture of ``bool``\s and ``int``, the output data type must be the default integer data type.
50-
- if one or more values are ``float``\s, the output data type must be the default floating-point data type.
49+
- if all values are of type ``int`` or are a mixture of ``bool`` and ``int``, the output data type must be the default integer data type.
50+
- if one or more values are ``complex`` numbers, the output data type must be the default complex floating-point data type.
51+
- if one or more values are ``float``\s, the output data type must be the default real-valued floating-point data type.
5152
5253
Default: ``None``.
5354
5455
.. admonition:: Note
5556
:class: note
5657
57-
If ``dtype`` is not ``None``, then array conversions should obey :ref:`type-promotion` rules. Conversions not specified according to :ref:`type-promotion` rules may or may not be permitted by a conforming array library. To perform an explicit cast, use :func:`signatures.data_type_functions.astype`.
58+
If ``dtype`` is not ``None``, then array conversions should obey :ref:`type-promotion` rules. Conversions not specified according to :ref:`type-promotion` rules may or may not be permitted by a conforming array library. To perform an explicit cast, use :func:`array_api.astype`.
5859
5960
.. note::
6061
If an input value exceeds the precision of the resolved output array data type, behavior is left unspecified and, thus, implementation-defined.
@@ -79,7 +80,7 @@ def empty(shape: Union[int, Tuple[int, ...]], *, dtype: Optional[dtype] = None,
7980
shape: Union[int, Tuple[int, ...]]
8081
output array shape.
8182
dtype: Optional[dtype]
82-
output array data type. If ``dtype`` is ``None``, the output array data type must be the default floating-point data type. Default: ``None``.
83+
output array data type. If ``dtype`` is ``None``, the output array data type must be the default real-valued floating-point data type. Default: ``None``.
8384
device: Optional[device]
8485
device on which to place the created array. Default: ``None``.
8586
@@ -112,6 +113,9 @@ def eye(n_rows: int, n_cols: Optional[int] = None, /, *, k: int = 0, dtype: Opti
112113
"""
113114
Returns a two-dimensional array with ones on the ``k``\th diagonal and zeros elsewhere.
114115
116+
.. note::
117+
An output array having a complex floating-point data type must have the value ``1 + 0j`` along the ``k``\th diagonal and ``0 + 0j`` elsewhere.
118+
115119
Parameters
116120
----------
117121
n_rows: int
@@ -121,7 +125,7 @@ def eye(n_rows: int, n_cols: Optional[int] = None, /, *, k: int = 0, dtype: Opti
121125
k: int
122126
index of the diagonal. A positive value refers to an upper diagonal, a negative value to a lower diagonal, and ``0`` to the main diagonal. Default: ``0``.
123127
dtype: Optional[dtype]
124-
output array data type. If ``dtype`` is ``None``, the output array data type must be the default floating-point data type. Default: ``None``.
128+
output array data type. If ``dtype`` is ``None``, the output array data type must be the default real-valued floating-point data type. Default: ``None``.
125129
device: Optional[device]
126130
device on which to place the created array. Default: ``None``.
127131
@@ -151,18 +155,23 @@ def from_dlpack(x: object, /) -> array:
151155
The returned array may be either a copy or a view. See :ref:`data-interchange` for details.
152156
"""
153157

154-
def full(shape: Union[int, Tuple[int, ...]], fill_value: Union[int, float], *, dtype: Optional[dtype] = None, device: Optional[device] = None) -> array:
158+
def full(shape: Union[int, Tuple[int, ...]], fill_value: Union[bool, int, float, complex], *, dtype: Optional[dtype] = None, device: Optional[device] = None) -> array:
155159
"""
156160
Returns a new array having a specified ``shape`` and filled with ``fill_value``.
157161
158162
Parameters
159163
----------
160164
shape: Union[int, Tuple[int, ...]]
161165
output array shape.
162-
fill_value: Union[int, float]
166+
fill_value: Union[bool, int, float, complex]
163167
fill value.
164168
dtype: Optional[dtype]
165-
output array data type. If ``dtype`` is ``None``, the output array data type must be inferred from ``fill_value``. If the fill value is an ``int``, the output array data type must be the default integer data type. If the fill value is a ``float``, the output array data type must be the default floating-point data type. If the fill value is a ``bool``, the output array must have boolean data type. Default: ``None``.
169+
output array data type. If ``dtype`` is ``None``, the output array data type must be inferred from ``fill_value`` according to the following rules:
170+
171+
- If the fill value is an ``int``, the output array data type must be the default integer data type.
172+
- If the fill value is a ``float``, the output array data type must be the default real-valued floating-point data type.
173+
- If the fill value is a ``complex`` number, the output array data type must be the default complex floating-point data type.
174+
- If the fill value is a ``bool``, the output array must have a boolean data type. Default: ``None``.
166175
167176
.. note::
168177
If the ``fill_value`` exceeds the precision of the resolved default output array data type, behavior is left unspecified and, thus, implementation-defined.
@@ -176,15 +185,15 @@ def full(shape: Union[int, Tuple[int, ...]], fill_value: Union[int, float], *, d
176185
an array where every element is equal to ``fill_value``.
177186
"""
178187

179-
def full_like(x: array, /, fill_value: Union[int, float], *, dtype: Optional[dtype] = None, device: Optional[device] = None) -> array:
188+
def full_like(x: array, /, fill_value: Union[bool, int, float, complex], *, dtype: Optional[dtype] = None, device: Optional[device] = None) -> array:
180189
"""
181190
Returns a new array filled with ``fill_value`` and having the same ``shape`` as an input array ``x``.
182191
183192
Parameters
184193
----------
185194
x: array
186195
input array from which to derive the output array shape.
187-
fill_value: Union[int, float]
196+
fill_value: Union[bool, int, float, complex]
188197
fill value.
189198
dtype: Optional[dtype]
190199
output array data type. If ``dtype`` is ``None``, the output array data type must be inferred from ``x``. Default: ``None``.
@@ -193,7 +202,7 @@ def full_like(x: array, /, fill_value: Union[int, float], *, dtype: Optional[dty
193202
If the ``fill_value`` exceeds the precision of the resolved output array data type, behavior is unspecified and, thus, implementation-defined.
194203
195204
.. note::
196-
If the ``fill_value`` has a data type (``int`` or ``float``) which is not of the same data type kind as the resolved output array data type (see :ref:`type-promotion`), behavior is unspecified and, thus, implementation-defined.
205+
If the ``fill_value`` has a data type which is not of the same data type kind (boolean, integer, or floating-point) as the resolved output array data type (see :ref:`type-promotion`), behavior is unspecified and, thus, implementation-defined.
197206
198207
device: Optional[device]
199208
device on which to place the created array. If ``device`` is ``None``, the output array device must be inferred from ``x``. Default: ``None``.
@@ -221,7 +230,7 @@ def linspace(start: Union[int, float], stop: Union[int, float], /, num: int, *,
221230
num: int
222231
number of samples. Must be a non-negative integer value; otherwise, the function must raise an exception.
223232
dtype: Optional[dtype]
224-
output array data type. Should be a floating-point data type. If ``dtype`` is ``None``, the output array data type must be the default floating-point data type. Default: ``None``.
233+
output array data type. Should be a real-valued floating-point data type. If ``dtype`` is ``None``, the output array data type must be the default real-valued floating-point data type. Default: ``None``.
225234
device: Optional[device]
226235
device on which to place the created array. Default: ``None``.
227236
endpoint: bool
@@ -234,10 +243,10 @@ def linspace(start: Union[int, float], stop: Union[int, float], /, num: int, *,
234243
235244
236245
.. note::
237-
While this specification recommends that this function only return arrays having a floating-point data type, specification-compliant array libraries may choose to support output arrays having an integer data type (e.g., due to backward compatibility concerns). However, function behavior when generating integer output arrays is unspecified and, thus, is implementation-defined. Accordingly, using this function to generate integer output arrays is not portable.
246+
While this specification recommends that this function only return arrays having a real-valued floating-point data type, specification-compliant array libraries may choose to support output arrays having an integer data type (e.g., due to backward compatibility concerns). However, function behavior when generating integer output arrays is unspecified and, thus, is implementation-defined. Accordingly, using this function to generate integer output arrays is not portable.
238247
239248
.. note::
240-
As mixed data type promotion is implementation-defined, behavior when ``start`` or ``stop`` exceeds the maximum safe integer of an output floating-point data type is implementation-defined. An implementation may choose to overflow or raise an exception.
249+
As mixed data type promotion is implementation-defined, behavior when ``start`` or ``stop`` exceeds the maximum safe integer of an output real-valued floating-point data type is implementation-defined. An implementation may choose to overflow or raise an exception.
241250
"""
242251

243252
def meshgrid(*arrays: array, indexing: str = 'xy') -> List[array]:
@@ -270,12 +279,15 @@ def ones(shape: Union[int, Tuple[int, ...]], *, dtype: Optional[dtype] = None, d
270279
"""
271280
Returns a new array having a specified ``shape`` and filled with ones.
272281
282+
.. note::
283+
An output array having a complex floating-point data type must contain complex numbers having a real component equal to one and an imaginary component equal to zero (i.e., ``1 + 0j``).
284+
273285
Parameters
274286
----------
275287
shape: Union[int, Tuple[int, ...]]
276288
output array shape.
277289
dtype: Optional[dtype]
278-
output array data type. If ``dtype`` is ``None``, the output array data type must be the default floating-point data type. Default: ``None``.
290+
output array data type. If ``dtype`` is ``None``, the output array data type must be the default real-valued floating-point data type. Default: ``None``.
279291
device: Optional[device]
280292
device on which to place the created array. Default: ``None``.
281293
@@ -289,6 +301,9 @@ def ones_like(x: array, /, *, dtype: Optional[dtype] = None, device: Optional[de
289301
"""
290302
Returns a new array filled with ones and having the same ``shape`` as an input array ``x``.
291303
304+
.. note::
305+
An output array having a complex floating-point data type must contain complex numbers having a real component equal to one and an imaginary component equal to zero (i.e., ``1 + 0j``).
306+
292307
Parameters
293308
----------
294309
x: array
@@ -359,7 +374,7 @@ def zeros(shape: Union[int, Tuple[int, ...]], *, dtype: Optional[dtype] = None,
359374
shape: Union[int, Tuple[int, ...]]
360375
output array shape.
361376
dtype: Optional[dtype]
362-
output array data type. If ``dtype`` is ``None``, the output array data type must be the default floating-point data type. Default: ``None``.
377+
output array data type. If ``dtype`` is ``None``, the output array data type must be the default real-valued floating-point data type. Default: ``None``.
363378
device: Optional[device]
364379
device on which to place the created array. Default: ``None``.
365380

spec/API_specification/signatures/data_type_functions.py renamed to spec/API_specification/array_api/data_type_functions.py

+8-40
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from ._types import List, Tuple, Union, array, dtype, finfo_object, iinfo_object
1+
from ._types import Union, array, dtype, finfo_object, iinfo_object
22

33
def astype(x: array, dtype: dtype, /, *, copy: bool = True) -> array:
44
"""
@@ -8,9 +8,9 @@ def astype(x: array, dtype: dtype, /, *, copy: bool = True) -> array:
88
Casting floating-point ``NaN`` and ``infinity`` values to integral data types is not specified and is implementation-dependent.
99
1010
.. note::
11-
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``.
11+
When casting a boolean input array to a real-valued 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``.
1212
13-
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``.
13+
When casting a real-valued input array to ``bool``, a value of ``0`` must cast to ``False``, and a non-zero value must cast to ``True``.
1414
1515
Parameters
1616
----------
@@ -27,38 +27,6 @@ def astype(x: array, dtype: dtype, /, *, copy: bool = True) -> array:
2727
an array having the specified data type. The returned array must have the same shape as ``x``.
2828
"""
2929

30-
def broadcast_arrays(*arrays: array) -> List[array]:
31-
"""
32-
Broadcasts one or more arrays against one another.
33-
34-
Parameters
35-
----------
36-
arrays: array
37-
an arbitrary number of to-be broadcasted arrays.
38-
39-
Returns
40-
-------
41-
out: List[array]
42-
a list of broadcasted arrays. Each array must have the same shape. Each array must have the same dtype as its corresponding input array.
43-
"""
44-
45-
def broadcast_to(x: array, /, shape: Tuple[int, ...]) -> array:
46-
"""
47-
Broadcasts an array to a specified shape.
48-
49-
Parameters
50-
----------
51-
x: array
52-
array to broadcast.
53-
shape: Tuple[int, ...]
54-
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.
55-
56-
Returns
57-
-------
58-
out: array
59-
an array having a specified shape. Must have the same data type as ``x``.
60-
"""
61-
6230
def can_cast(from_: Union[dtype, array], to: dtype, /) -> bool:
6331
"""
6432
Determines if one data type can be cast to another data type according :ref:`type-promotion` rules.
@@ -78,17 +46,17 @@ def can_cast(from_: Union[dtype, array], to: dtype, /) -> bool:
7846

7947
def finfo(type: Union[dtype, array], /) -> finfo_object:
8048
"""
81-
Machine limits for floating-point data types.
49+
Machine limits for real-valued floating-point data types.
8250
8351
Parameters
8452
----------
8553
type: Union[dtype, array]
86-
the kind of floating-point data-type about which to get information.
54+
the kind of real-valued floating-point data-type about which to get information.
8755
8856
Returns
8957
-------
9058
out: finfo object
91-
an object having the followng attributes:
59+
an object having the following attributes:
9260
9361
- **bits**: *int*
9462
@@ -123,7 +91,7 @@ def iinfo(type: Union[dtype, array], /) -> iinfo_object:
12391
Returns
12492
-------
12593
out: iinfo object
126-
a class with that encapsules the following attributes:
94+
an object having the following attributes:
12795
12896
- **bits**: *int*
12997
@@ -156,4 +124,4 @@ def result_type(*arrays_and_dtypes: Union[array, dtype]) -> dtype:
156124
the dtype resulting from an operation involving the input arrays and dtypes.
157125
"""
158126

159-
__all__ = ['astype', 'broadcast_arrays', 'broadcast_to', 'can_cast', 'finfo', 'iinfo', 'result_type']
127+
__all__ = ['astype', 'can_cast', 'finfo', 'iinfo', 'result_type']

spec/API_specification/signatures/data_types.py renamed to spec/API_specification/array_api/data_types.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ def __eq__(self: dtype, other: dtype, /) -> bool:
1717
a boolean indicating whether the data type objects are equal.
1818
"""
1919

20-
all = [__eq__]
20+
__all__ = ['__eq__']

0 commit comments

Comments
 (0)