From ab5164549113390dfc34feaa3fb635eb646b168d Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Mon, 13 Jun 2022 03:00:56 -0700 Subject: [PATCH 1/4] Add complex number support to `cos` --- .gitignore | 1 + .../array_api/elementwise_functions.py | 25 +++++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 73e203a64..86bab2717 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,4 @@ node_modules/ __pycache__/ *.pyc spec/**/generated +tmp/ \ No newline at end of file diff --git a/spec/API_specification/array_api/elementwise_functions.py b/spec/API_specification/array_api/elementwise_functions.py index 7acefaf52..06e4c7cf2 100644 --- a/spec/API_specification/array_api/elementwise_functions.py +++ b/spec/API_specification/array_api/elementwise_functions.py @@ -402,12 +402,14 @@ def ceil(x: array, /) -> array: """ def cos(x: array, /) -> array: - """ - Calculates an implementation-dependent approximation to the cosine, having domain ``(-infinity, +infinity)`` and codomain ``[-1, +1]``, for each element ``x_i`` of the input array ``x``. Each element ``x_i`` is assumed to be expressed in radians. + r""" + Calculates an implementation-dependent approximation to the cosine for each element ``x_i`` of the input array ``x``. + + Each element ``x_i`` is assumed to be expressed in radians. **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 ``+0``, the result is ``1``. @@ -415,15 +417,28 @@ def cos(x: array, /) -> array: - If ``x_i`` is ``+infinity``, the result is ``NaN``. - If ``x_i`` is ``-infinity``, the result is ``NaN``. + For complex floating-point operands, special cases must be handled as if the operation is implemented as ``cosh(x*1j)``. + + .. note:: + The cosine is an entire function on the complex plane and has no branch cuts. + + .. note:: + For complex arguments, the mathematical definition of cosine is + + .. math:: + \begin{align} \operatorname{cos}(x) &= \sum_{n=0}^\inf \frac{(-1)^n}{(2n)!} z^{2n} \\ &= \frac{e^{jz} + e^{-jz}}{2} \\ &= \operatorname{cosh}(jx) \end{align} + + where :math:`\operatorname{cosh}` is the hyperbolic cosine. + Parameters ---------- x: array - input array whose elements are each expressed in radians. Should have a real-valued floating-point data type. + input array whose elements are each expressed in radians. Should have a floating-point data type. Returns ------- out: array - an array containing the cosine of 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 cosine of each element in ``x``. The returned array must have a floating-point data type determined by :ref:`type-promotion`. """ def cosh(x: array, /) -> array: From 6d5f4f6662649b0751198fc4962f4d0201d34c47 Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Mon, 13 Jun 2022 03:03:26 -0700 Subject: [PATCH 2/4] Fix equation --- 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 06e4c7cf2..ef7fd40d9 100644 --- a/spec/API_specification/array_api/elementwise_functions.py +++ b/spec/API_specification/array_api/elementwise_functions.py @@ -426,7 +426,7 @@ def cos(x: array, /) -> array: For complex arguments, the mathematical definition of cosine is .. math:: - \begin{align} \operatorname{cos}(x) &= \sum_{n=0}^\inf \frac{(-1)^n}{(2n)!} z^{2n} \\ &= \frac{e^{jz} + e^{-jz}}{2} \\ &= \operatorname{cosh}(jx) \end{align} + \begin{align} \operatorname{cos}(x) &= \sum_{n=0}^\infty \frac{(-1)^n}{(2n)!} z^{2n} \\ &= \frac{e^{jz} + e^{-jz}}{2} \\ &= \operatorname{cosh}(jx) \end{align} where :math:`\operatorname{cosh}` is the hyperbolic cosine. From 45d8435f6b3fdb79e3cb77edd4fb80c5c996f48d Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Mon, 13 Jun 2022 03:04:19 -0700 Subject: [PATCH 3/4] Fix equation variable --- 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 ef7fd40d9..f89d804a2 100644 --- a/spec/API_specification/array_api/elementwise_functions.py +++ b/spec/API_specification/array_api/elementwise_functions.py @@ -426,7 +426,7 @@ def cos(x: array, /) -> array: For complex arguments, the mathematical definition of cosine is .. math:: - \begin{align} \operatorname{cos}(x) &= \sum_{n=0}^\infty \frac{(-1)^n}{(2n)!} z^{2n} \\ &= \frac{e^{jz} + e^{-jz}}{2} \\ &= \operatorname{cosh}(jx) \end{align} + \begin{align} \operatorname{cos}(x) &= \sum_{n=0}^\infty \frac{(-1)^n}{(2n)!} z^{2n} \\ &= \frac{e^{jx} + e^{-jx}}{2} \\ &= \operatorname{cosh}(jx) \end{align} where :math:`\operatorname{cosh}` is the hyperbolic cosine. From fcf4b47d562470d33e840c011d6a502b1c9e20d7 Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Mon, 13 Jun 2022 03:06:52 -0700 Subject: [PATCH 4/4] Fix variable name --- 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 f89d804a2..86623cc5c 100644 --- a/spec/API_specification/array_api/elementwise_functions.py +++ b/spec/API_specification/array_api/elementwise_functions.py @@ -426,7 +426,7 @@ def cos(x: array, /) -> array: For complex arguments, the mathematical definition of cosine is .. math:: - \begin{align} \operatorname{cos}(x) &= \sum_{n=0}^\infty \frac{(-1)^n}{(2n)!} z^{2n} \\ &= \frac{e^{jx} + e^{-jx}}{2} \\ &= \operatorname{cosh}(jx) \end{align} + \begin{align} \operatorname{cos}(x) &= \sum_{n=0}^\infty \frac{(-1)^n}{(2n)!} x^{2n} \\ &= \frac{e^{jx} + e^{-jx}}{2} \\ &= \operatorname{cosh}(jx) \end{align} where :math:`\operatorname{cosh}` is the hyperbolic cosine.