From bfadb2952a0d3689d7ddd4c48acb90baadf78e47 Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Sun, 22 May 2022 17:46:05 -0700 Subject: [PATCH 1/4] Add complex number support to `full` and `full_like` --- .../array_api/creation_functions.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/spec/API_specification/array_api/creation_functions.py b/spec/API_specification/array_api/creation_functions.py index 65244cf97..4fce362bb 100644 --- a/spec/API_specification/array_api/creation_functions.py +++ b/spec/API_specification/array_api/creation_functions.py @@ -151,7 +151,7 @@ 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[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``. @@ -159,10 +159,10 @@ def full(shape: Union[int, Tuple[int, ...]], fill_value: Union[int, float], *, d ---------- shape: Union[int, Tuple[int, ...]] output array shape. - fill_value: Union[int, float] + fill_value: Union[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``. 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 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. @@ -176,7 +176,7 @@ 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[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``. @@ -184,7 +184,7 @@ def full_like(x: array, /, fill_value: Union[int, float], *, dtype: Optional[dty ---------- x: array input array from which to derive the output array shape. - fill_value: Union[int, float] + fill_value: Union[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``. @@ -193,7 +193,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 (``int``, ``float``, or ``complex``) which is not of the same data type kind (integer versus 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``. From bb993c8f0a6c04ebc8e4da59b4c6d37a26aaa88e Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Sun, 22 May 2022 17:49:15 -0700 Subject: [PATCH 2/4] Use a list when enumerating rules --- spec/API_specification/array_api/creation_functions.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/spec/API_specification/array_api/creation_functions.py b/spec/API_specification/array_api/creation_functions.py index 4fce362bb..94c52f720 100644 --- a/spec/API_specification/array_api/creation_functions.py +++ b/spec/API_specification/array_api/creation_functions.py @@ -162,7 +162,12 @@ def full(shape: Union[int, Tuple[int, ...]], fill_value: Union[int, float, compl fill_value: Union[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 ``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 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 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. From 94ebddda6b7f9da955c14ff5ce8ce711c680a14a Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Sun, 22 May 2022 17:50:50 -0700 Subject: [PATCH 3/4] Fix missing article --- spec/API_specification/array_api/creation_functions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/API_specification/array_api/creation_functions.py b/spec/API_specification/array_api/creation_functions.py index 94c52f720..fe250eb60 100644 --- a/spec/API_specification/array_api/creation_functions.py +++ b/spec/API_specification/array_api/creation_functions.py @@ -167,7 +167,7 @@ def full(shape: Union[int, Tuple[int, ...]], fill_value: Union[int, float, compl - 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 boolean data type. Default: ``None``. + - 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. From 4db51967d198957a0340eb60210cd47931dd645c Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Sun, 22 May 2022 17:55:23 -0700 Subject: [PATCH 4/4] Fix type annotations which omit `bool` --- spec/API_specification/array_api/creation_functions.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/spec/API_specification/array_api/creation_functions.py b/spec/API_specification/array_api/creation_functions.py index fe250eb60..85621e144 100644 --- a/spec/API_specification/array_api/creation_functions.py +++ b/spec/API_specification/array_api/creation_functions.py @@ -151,7 +151,7 @@ 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, complex], *, 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``. @@ -159,7 +159,7 @@ def full(shape: Union[int, Tuple[int, ...]], fill_value: Union[int, float, compl ---------- shape: Union[int, Tuple[int, ...]] output array shape. - fill_value: Union[int, float, complex] + 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`` according to the following rules: @@ -181,7 +181,7 @@ def full(shape: Union[int, Tuple[int, ...]], fill_value: Union[int, float, compl an array where every element is equal to ``fill_value``. """ -def full_like(x: array, /, fill_value: Union[int, float, complex], *, 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``. @@ -189,7 +189,7 @@ def full_like(x: array, /, fill_value: Union[int, float, complex], *, dtype: Opt ---------- x: array input array from which to derive the output array shape. - fill_value: Union[int, float, complex] + 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``. @@ -198,7 +198,7 @@ def full_like(x: array, /, fill_value: Union[int, float, complex], *, dtype: Opt 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``, ``float``, or ``complex``) which is not of the same data type kind (integer versus floating-point) 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``.