Skip to content

Commit 6a35bc1

Browse files
authored
Add complex number support to full and full_like (#435)
* Add complex number support to `full` and `full_like` * Use a list when enumerating rules * Fix missing article * Fix type annotations which omit `bool`
1 parent 41b1b39 commit 6a35bc1

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

spec/API_specification/array_api/creation_functions.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -155,18 +155,23 @@ def from_dlpack(x: object, /) -> array:
155155
The returned array may be either a copy or a view. See :ref:`data-interchange` for details.
156156
"""
157157

158-
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:
159159
"""
160160
Returns a new array having a specified ``shape`` and filled with ``fill_value``.
161161
162162
Parameters
163163
----------
164164
shape: Union[int, Tuple[int, ...]]
165165
output array shape.
166-
fill_value: Union[int, float]
166+
fill_value: Union[bool, int, float, complex]
167167
fill value.
168168
dtype: Optional[dtype]
169-
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 real-valued 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``.
170175
171176
.. note::
172177
If the ``fill_value`` exceeds the precision of the resolved default output array data type, behavior is left unspecified and, thus, implementation-defined.
@@ -180,15 +185,15 @@ def full(shape: Union[int, Tuple[int, ...]], fill_value: Union[int, float], *, d
180185
an array where every element is equal to ``fill_value``.
181186
"""
182187

183-
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:
184189
"""
185190
Returns a new array filled with ``fill_value`` and having the same ``shape`` as an input array ``x``.
186191
187192
Parameters
188193
----------
189194
x: array
190195
input array from which to derive the output array shape.
191-
fill_value: Union[int, float]
196+
fill_value: Union[bool, int, float, complex]
192197
fill value.
193198
dtype: Optional[dtype]
194199
output array data type. If ``dtype`` is ``None``, the output array data type must be inferred from ``x``. Default: ``None``.
@@ -197,7 +202,7 @@ def full_like(x: array, /, fill_value: Union[int, float], *, dtype: Optional[dty
197202
If the ``fill_value`` exceeds the precision of the resolved output array data type, behavior is unspecified and, thus, implementation-defined.
198203
199204
.. note::
200-
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.
201206
202207
device: Optional[device]
203208
device on which to place the created array. If ``device`` is ``None``, the output array device must be inferred from ``x``. Default: ``None``.

0 commit comments

Comments
 (0)