Skip to content

Add complex number support to full and full_like #435

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions spec/API_specification/array_api/creation_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,23 @@ def from_dlpack(x: object, /) -> array:
The returned array may be either a copy or a view. See :ref:`data-interchange` for details.
"""

def full(shape: Union[int, Tuple[int, ...]], fill_value: Union[int, float], *, dtype: Optional[dtype] = None, device: Optional[device] = None) -> array:
def full(shape: Union[int, Tuple[int, ...]], fill_value: Union[bool, int, float, complex], *, dtype: Optional[dtype] = None, device: Optional[device] = None) -> array:
"""
Returns a new array having a specified ``shape`` and filled with ``fill_value``.

Parameters
----------
shape: Union[int, Tuple[int, ...]]
output array shape.
fill_value: Union[int, float]
fill_value: Union[bool, int, float, complex]
fill value.
dtype: Optional[dtype]
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``.
output array data type. If ``dtype`` is ``None``, the output array data type must be inferred from ``fill_value`` according to the following rules:

- 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 ``complex`` number, the output array data type must be the default complex floating-point data type.
- If the fill value is a ``bool``, the output array must have a boolean data type. Default: ``None``.

.. note::
If the ``fill_value`` exceeds the precision of the resolved default output array data type, behavior is left unspecified and, thus, implementation-defined.
Expand All @@ -176,15 +181,15 @@ def full(shape: Union[int, Tuple[int, ...]], fill_value: Union[int, float], *, d
an array where every element is equal to ``fill_value``.
"""

def full_like(x: array, /, fill_value: Union[int, float], *, dtype: Optional[dtype] = None, device: Optional[device] = None) -> array:
def full_like(x: array, /, fill_value: Union[bool, int, float, complex], *, dtype: Optional[dtype] = None, device: Optional[device] = None) -> array:
"""
Returns a new array filled with ``fill_value`` and having the same ``shape`` as an input array ``x``.

Parameters
----------
x: array
input array from which to derive the output array shape.
fill_value: Union[int, float]
fill_value: Union[bool, int, float, complex]
fill value.
dtype: Optional[dtype]
output array data type. If ``dtype`` is ``None``, the output array data type must be inferred from ``x``. Default: ``None``.
Expand All @@ -193,7 +198,7 @@ def full_like(x: array, /, fill_value: Union[int, float], *, dtype: Optional[dty
If the ``fill_value`` exceeds the precision of the resolved output array data type, behavior is unspecified and, thus, implementation-defined.

.. note::
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.
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.

device: Optional[device]
device on which to place the created array. If ``device`` is ``None``, the output array device must be inferred from ``x``. Default: ``None``.
Expand Down