From 59561402d6de5227e4c362c624d76662febda9cc Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Wed, 30 Nov 2022 22:39:21 -0800 Subject: [PATCH 1/2] Add complex number support to `log2` --- .../array_api/elementwise_functions.py | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/spec/API_specification/array_api/elementwise_functions.py b/spec/API_specification/array_api/elementwise_functions.py index e76be8d65..b6f6e3bd2 100644 --- a/spec/API_specification/array_api/elementwise_functions.py +++ b/spec/API_specification/array_api/elementwise_functions.py @@ -958,12 +958,12 @@ def log1p(x: array, /) -> array: """ def log2(x: array, /) -> array: - """ - Calculates an implementation-dependent approximation to the base ``2`` logarithm, having domain ``[0, +infinity]`` and codomain ``[-infinity, +infinity]``, for each element ``x_i`` of the input array ``x``. + r""" + Calculates an implementation-dependent approximation to the base ``2`` logarithm for each element ``x_i`` of the input array ``x``. **Special cases** - For floating-point operands, + For real-valued floating-point operands, - If ``x_i`` is ``NaN``, the result is ``NaN``. - If ``x_i`` is less than ``0``, the result is ``NaN``. @@ -971,15 +971,25 @@ def log2(x: array, /) -> array: - If ``x_i`` is ``1``, the result is ``+0``. - If ``x_i`` is ``+infinity``, the result is ``+infinity``. + For complex floating-point operands, special cases must be handled as if the operation is implemented using the standard change of base formula + + .. math:: + \log_{2} x = \frac{\log_{e} x}{\log_{e} 2} + + where :math:`\log_{e}` is the natural logarithm, as implemented by :func:`~array_api.log`. + + .. note:: + For complex floating-point operands, ``log2(conj(x))`` must equal ``conj(log2(x))``. + Parameters ---------- x: array - input array. Should have a real-valued floating-point data type. + input array. Should have a numeric floating-point data type. Returns ------- out: array - an array containing the evaluated base ``2`` logarithm for each element in ``x``. The returned array must have a real-valued floating-point data type determined by :ref:`type-promotion`. + an array containing the evaluated base ``2`` logarithm for each element in ``x``. The returned array must have a floating-point data type determined by :ref:`type-promotion`. """ def log10(x: array, /) -> array: From 707d812e1f9525962121dca245052287f7dbf758 Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Wed, 30 Nov 2022 22:44:39 -0800 Subject: [PATCH 2/2] Fix dtypes --- spec/API_specification/array_api/elementwise_functions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/API_specification/array_api/elementwise_functions.py b/spec/API_specification/array_api/elementwise_functions.py index b6f6e3bd2..1a7221185 100644 --- a/spec/API_specification/array_api/elementwise_functions.py +++ b/spec/API_specification/array_api/elementwise_functions.py @@ -984,7 +984,7 @@ def log2(x: array, /) -> array: Parameters ---------- x: array - input array. Should have a numeric floating-point data type. + input array. Should have a floating-point data type. Returns -------