From 6e57d829cb6628610e163524f203245b247a2839 Mon Sep 17 00:00:00 2001 From: Aaron Meurer Date: Wed, 4 Aug 2021 16:47:05 -0600 Subject: Rename numpy._array_api to numpy.array_api Instead of the leading underscore, the experimentalness of the module will be indicated by omitting a warning on import. That we, we do not have to change the API from underscore to no underscore when the module is no longer experimental. --- numpy/array_api/_elementwise_functions.py | 659 ++++++++++++++++++++++++++++++ 1 file changed, 659 insertions(+) create mode 100644 numpy/array_api/_elementwise_functions.py (limited to 'numpy/array_api/_elementwise_functions.py') diff --git a/numpy/array_api/_elementwise_functions.py b/numpy/array_api/_elementwise_functions.py new file mode 100644 index 000000000..7833ebe54 --- /dev/null +++ b/numpy/array_api/_elementwise_functions.py @@ -0,0 +1,659 @@ +from __future__ import annotations + +from ._dtypes import (_boolean_dtypes, _floating_dtypes, + _integer_dtypes, _integer_or_boolean_dtypes, + _numeric_dtypes, _result_type) +from ._array_object import Array + +import numpy as np + +def abs(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.abs `. + + See its docstring for more information. + """ + if x.dtype not in _numeric_dtypes: + raise TypeError('Only numeric dtypes are allowed in abs') + return Array._new(np.abs(x._array)) + +# Note: the function name is different here +def acos(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.arccos `. + + See its docstring for more information. + """ + if x.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed in acos') + return Array._new(np.arccos(x._array)) + +# Note: the function name is different here +def acosh(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.arccosh `. + + See its docstring for more information. + """ + if x.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed in acosh') + return Array._new(np.arccosh(x._array)) + +def add(x1: Array, x2: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.add `. + + See its docstring for more information. + """ + if x1.dtype not in _numeric_dtypes or x2.dtype not in _numeric_dtypes: + raise TypeError('Only numeric dtypes are allowed in add') + # Call result type here just to raise on disallowed type combinations + _result_type(x1.dtype, x2.dtype) + x1, x2 = Array._normalize_two_args(x1, x2) + return Array._new(np.add(x1._array, x2._array)) + +# Note: the function name is different here +def asin(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.arcsin `. + + See its docstring for more information. + """ + if x.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed in asin') + return Array._new(np.arcsin(x._array)) + +# Note: the function name is different here +def asinh(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.arcsinh `. + + See its docstring for more information. + """ + if x.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed in asinh') + return Array._new(np.arcsinh(x._array)) + +# Note: the function name is different here +def atan(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.arctan `. + + See its docstring for more information. + """ + if x.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed in atan') + return Array._new(np.arctan(x._array)) + +# Note: the function name is different here +def atan2(x1: Array, x2: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.arctan2 `. + + See its docstring for more information. + """ + if x1.dtype not in _floating_dtypes or x2.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed in atan2') + # Call result type here just to raise on disallowed type combinations + _result_type(x1.dtype, x2.dtype) + x1, x2 = Array._normalize_two_args(x1, x2) + return Array._new(np.arctan2(x1._array, x2._array)) + +# Note: the function name is different here +def atanh(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.arctanh `. + + See its docstring for more information. + """ + if x.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed in atanh') + return Array._new(np.arctanh(x._array)) + +def bitwise_and(x1: Array, x2: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.bitwise_and `. + + See its docstring for more information. + """ + if x1.dtype not in _integer_or_boolean_dtypes or x2.dtype not in _integer_or_boolean_dtypes: + raise TypeError('Only integer or boolean dtypes are allowed in bitwise_and') + # Call result type here just to raise on disallowed type combinations + _result_type(x1.dtype, x2.dtype) + x1, x2 = Array._normalize_two_args(x1, x2) + return Array._new(np.bitwise_and(x1._array, x2._array)) + +# Note: the function name is different here +def bitwise_left_shift(x1: Array, x2: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.left_shift `. + + See its docstring for more information. + """ + if x1.dtype not in _integer_dtypes or x2.dtype not in _integer_dtypes: + raise TypeError('Only integer dtypes are allowed in bitwise_left_shift') + # Call result type here just to raise on disallowed type combinations + _result_type(x1.dtype, x2.dtype) + x1, x2 = Array._normalize_two_args(x1, x2) + # Note: bitwise_left_shift is only defined for x2 nonnegative. + if np.any(x2._array < 0): + raise ValueError('bitwise_left_shift(x1, x2) is only defined for x2 >= 0') + return Array._new(np.left_shift(x1._array, x2._array)) + +# Note: the function name is different here +def bitwise_invert(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.invert `. + + See its docstring for more information. + """ + if x.dtype not in _integer_or_boolean_dtypes: + raise TypeError('Only integer or boolean dtypes are allowed in bitwise_invert') + return Array._new(np.invert(x._array)) + +def bitwise_or(x1: Array, x2: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.bitwise_or `. + + See its docstring for more information. + """ + if x1.dtype not in _integer_or_boolean_dtypes or x2.dtype not in _integer_or_boolean_dtypes: + raise TypeError('Only integer or boolean dtypes are allowed in bitwise_or') + # Call result type here just to raise on disallowed type combinations + _result_type(x1.dtype, x2.dtype) + x1, x2 = Array._normalize_two_args(x1, x2) + return Array._new(np.bitwise_or(x1._array, x2._array)) + +# Note: the function name is different here +def bitwise_right_shift(x1: Array, x2: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.right_shift `. + + See its docstring for more information. + """ + if x1.dtype not in _integer_dtypes or x2.dtype not in _integer_dtypes: + raise TypeError('Only integer dtypes are allowed in bitwise_right_shift') + # Call result type here just to raise on disallowed type combinations + _result_type(x1.dtype, x2.dtype) + x1, x2 = Array._normalize_two_args(x1, x2) + # Note: bitwise_right_shift is only defined for x2 nonnegative. + if np.any(x2._array < 0): + raise ValueError('bitwise_right_shift(x1, x2) is only defined for x2 >= 0') + return Array._new(np.right_shift(x1._array, x2._array)) + +def bitwise_xor(x1: Array, x2: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.bitwise_xor `. + + See its docstring for more information. + """ + if x1.dtype not in _integer_or_boolean_dtypes or x2.dtype not in _integer_or_boolean_dtypes: + raise TypeError('Only integer or boolean dtypes are allowed in bitwise_xor') + # Call result type here just to raise on disallowed type combinations + _result_type(x1.dtype, x2.dtype) + x1, x2 = Array._normalize_two_args(x1, x2) + return Array._new(np.bitwise_xor(x1._array, x2._array)) + +def ceil(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.ceil `. + + See its docstring for more information. + """ + if x.dtype not in _numeric_dtypes: + raise TypeError('Only numeric dtypes are allowed in ceil') + if x.dtype in _integer_dtypes: + # Note: The return dtype of ceil is the same as the input + return x + return Array._new(np.ceil(x._array)) + +def cos(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.cos `. + + See its docstring for more information. + """ + if x.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed in cos') + return Array._new(np.cos(x._array)) + +def cosh(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.cosh `. + + See its docstring for more information. + """ + if x.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed in cosh') + return Array._new(np.cosh(x._array)) + +def divide(x1: Array, x2: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.divide `. + + See its docstring for more information. + """ + if x1.dtype not in _floating_dtypes or x2.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed in divide') + # Call result type here just to raise on disallowed type combinations + _result_type(x1.dtype, x2.dtype) + x1, x2 = Array._normalize_two_args(x1, x2) + return Array._new(np.divide(x1._array, x2._array)) + +def equal(x1: Array, x2: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.equal `. + + See its docstring for more information. + """ + # Call result type here just to raise on disallowed type combinations + _result_type(x1.dtype, x2.dtype) + x1, x2 = Array._normalize_two_args(x1, x2) + return Array._new(np.equal(x1._array, x2._array)) + +def exp(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.exp `. + + See its docstring for more information. + """ + if x.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed in exp') + return Array._new(np.exp(x._array)) + +def expm1(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.expm1 `. + + See its docstring for more information. + """ + if x.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed in expm1') + return Array._new(np.expm1(x._array)) + +def floor(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.floor `. + + See its docstring for more information. + """ + if x.dtype not in _numeric_dtypes: + raise TypeError('Only numeric dtypes are allowed in floor') + if x.dtype in _integer_dtypes: + # Note: The return dtype of floor is the same as the input + return x + return Array._new(np.floor(x._array)) + +def floor_divide(x1: Array, x2: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.floor_divide `. + + See its docstring for more information. + """ + if x1.dtype not in _numeric_dtypes or x2.dtype not in _numeric_dtypes: + raise TypeError('Only numeric dtypes are allowed in floor_divide') + # Call result type here just to raise on disallowed type combinations + _result_type(x1.dtype, x2.dtype) + x1, x2 = Array._normalize_two_args(x1, x2) + return Array._new(np.floor_divide(x1._array, x2._array)) + +def greater(x1: Array, x2: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.greater `. + + See its docstring for more information. + """ + if x1.dtype not in _numeric_dtypes or x2.dtype not in _numeric_dtypes: + raise TypeError('Only numeric dtypes are allowed in greater') + # Call result type here just to raise on disallowed type combinations + _result_type(x1.dtype, x2.dtype) + x1, x2 = Array._normalize_two_args(x1, x2) + return Array._new(np.greater(x1._array, x2._array)) + +def greater_equal(x1: Array, x2: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.greater_equal `. + + See its docstring for more information. + """ + if x1.dtype not in _numeric_dtypes or x2.dtype not in _numeric_dtypes: + raise TypeError('Only numeric dtypes are allowed in greater_equal') + # Call result type here just to raise on disallowed type combinations + _result_type(x1.dtype, x2.dtype) + x1, x2 = Array._normalize_two_args(x1, x2) + return Array._new(np.greater_equal(x1._array, x2._array)) + +def isfinite(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.isfinite `. + + See its docstring for more information. + """ + if x.dtype not in _numeric_dtypes: + raise TypeError('Only numeric dtypes are allowed in isfinite') + return Array._new(np.isfinite(x._array)) + +def isinf(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.isinf `. + + See its docstring for more information. + """ + if x.dtype not in _numeric_dtypes: + raise TypeError('Only numeric dtypes are allowed in isinf') + return Array._new(np.isinf(x._array)) + +def isnan(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.isnan `. + + See its docstring for more information. + """ + if x.dtype not in _numeric_dtypes: + raise TypeError('Only numeric dtypes are allowed in isnan') + return Array._new(np.isnan(x._array)) + +def less(x1: Array, x2: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.less `. + + See its docstring for more information. + """ + if x1.dtype not in _numeric_dtypes or x2.dtype not in _numeric_dtypes: + raise TypeError('Only numeric dtypes are allowed in less') + # Call result type here just to raise on disallowed type combinations + _result_type(x1.dtype, x2.dtype) + x1, x2 = Array._normalize_two_args(x1, x2) + return Array._new(np.less(x1._array, x2._array)) + +def less_equal(x1: Array, x2: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.less_equal `. + + See its docstring for more information. + """ + if x1.dtype not in _numeric_dtypes or x2.dtype not in _numeric_dtypes: + raise TypeError('Only numeric dtypes are allowed in less_equal') + # Call result type here just to raise on disallowed type combinations + _result_type(x1.dtype, x2.dtype) + x1, x2 = Array._normalize_two_args(x1, x2) + return Array._new(np.less_equal(x1._array, x2._array)) + +def log(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.log `. + + See its docstring for more information. + """ + if x.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed in log') + return Array._new(np.log(x._array)) + +def log1p(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.log1p `. + + See its docstring for more information. + """ + if x.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed in log1p') + return Array._new(np.log1p(x._array)) + +def log2(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.log2 `. + + See its docstring for more information. + """ + if x.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed in log2') + return Array._new(np.log2(x._array)) + +def log10(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.log10 `. + + See its docstring for more information. + """ + if x.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed in log10') + return Array._new(np.log10(x._array)) + +def logaddexp(x1: Array, x2: Array) -> Array: + """ + Array API compatible wrapper for :py:func:`np.logaddexp `. + + See its docstring for more information. + """ + if x1.dtype not in _floating_dtypes or x2.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed in logaddexp') + # Call result type here just to raise on disallowed type combinations + _result_type(x1.dtype, x2.dtype) + x1, x2 = Array._normalize_two_args(x1, x2) + return Array._new(np.logaddexp(x1._array, x2._array)) + +def logical_and(x1: Array, x2: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.logical_and `. + + See its docstring for more information. + """ + if x1.dtype not in _boolean_dtypes or x2.dtype not in _boolean_dtypes: + raise TypeError('Only boolean dtypes are allowed in logical_and') + # Call result type here just to raise on disallowed type combinations + _result_type(x1.dtype, x2.dtype) + x1, x2 = Array._normalize_two_args(x1, x2) + return Array._new(np.logical_and(x1._array, x2._array)) + +def logical_not(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.logical_not `. + + See its docstring for more information. + """ + if x.dtype not in _boolean_dtypes: + raise TypeError('Only boolean dtypes are allowed in logical_not') + return Array._new(np.logical_not(x._array)) + +def logical_or(x1: Array, x2: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.logical_or `. + + See its docstring for more information. + """ + if x1.dtype not in _boolean_dtypes or x2.dtype not in _boolean_dtypes: + raise TypeError('Only boolean dtypes are allowed in logical_or') + # Call result type here just to raise on disallowed type combinations + _result_type(x1.dtype, x2.dtype) + x1, x2 = Array._normalize_two_args(x1, x2) + return Array._new(np.logical_or(x1._array, x2._array)) + +def logical_xor(x1: Array, x2: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.logical_xor `. + + See its docstring for more information. + """ + if x1.dtype not in _boolean_dtypes or x2.dtype not in _boolean_dtypes: + raise TypeError('Only boolean dtypes are allowed in logical_xor') + # Call result type here just to raise on disallowed type combinations + _result_type(x1.dtype, x2.dtype) + x1, x2 = Array._normalize_two_args(x1, x2) + return Array._new(np.logical_xor(x1._array, x2._array)) + +def multiply(x1: Array, x2: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.multiply `. + + See its docstring for more information. + """ + if x1.dtype not in _numeric_dtypes or x2.dtype not in _numeric_dtypes: + raise TypeError('Only numeric dtypes are allowed in multiply') + # Call result type here just to raise on disallowed type combinations + _result_type(x1.dtype, x2.dtype) + x1, x2 = Array._normalize_two_args(x1, x2) + return Array._new(np.multiply(x1._array, x2._array)) + +def negative(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.negative `. + + See its docstring for more information. + """ + if x.dtype not in _numeric_dtypes: + raise TypeError('Only numeric dtypes are allowed in negative') + return Array._new(np.negative(x._array)) + +def not_equal(x1: Array, x2: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.not_equal `. + + See its docstring for more information. + """ + # Call result type here just to raise on disallowed type combinations + _result_type(x1.dtype, x2.dtype) + x1, x2 = Array._normalize_two_args(x1, x2) + return Array._new(np.not_equal(x1._array, x2._array)) + +def positive(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.positive `. + + See its docstring for more information. + """ + if x.dtype not in _numeric_dtypes: + raise TypeError('Only numeric dtypes are allowed in positive') + return Array._new(np.positive(x._array)) + +# Note: the function name is different here +def pow(x1: Array, x2: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.power `. + + See its docstring for more information. + """ + if x1.dtype not in _floating_dtypes or x2.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed in pow') + # Call result type here just to raise on disallowed type combinations + _result_type(x1.dtype, x2.dtype) + x1, x2 = Array._normalize_two_args(x1, x2) + return Array._new(np.power(x1._array, x2._array)) + +def remainder(x1: Array, x2: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.remainder `. + + See its docstring for more information. + """ + if x1.dtype not in _numeric_dtypes or x2.dtype not in _numeric_dtypes: + raise TypeError('Only numeric dtypes are allowed in remainder') + # Call result type here just to raise on disallowed type combinations + _result_type(x1.dtype, x2.dtype) + x1, x2 = Array._normalize_two_args(x1, x2) + return Array._new(np.remainder(x1._array, x2._array)) + +def round(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.round `. + + See its docstring for more information. + """ + if x.dtype not in _numeric_dtypes: + raise TypeError('Only numeric dtypes are allowed in round') + return Array._new(np.round(x._array)) + +def sign(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.sign `. + + See its docstring for more information. + """ + if x.dtype not in _numeric_dtypes: + raise TypeError('Only numeric dtypes are allowed in sign') + return Array._new(np.sign(x._array)) + +def sin(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.sin `. + + See its docstring for more information. + """ + if x.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed in sin') + return Array._new(np.sin(x._array)) + +def sinh(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.sinh `. + + See its docstring for more information. + """ + if x.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed in sinh') + return Array._new(np.sinh(x._array)) + +def square(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.square `. + + See its docstring for more information. + """ + if x.dtype not in _numeric_dtypes: + raise TypeError('Only numeric dtypes are allowed in square') + return Array._new(np.square(x._array)) + +def sqrt(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.sqrt `. + + See its docstring for more information. + """ + if x.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed in sqrt') + return Array._new(np.sqrt(x._array)) + +def subtract(x1: Array, x2: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.subtract `. + + See its docstring for more information. + """ + if x1.dtype not in _numeric_dtypes or x2.dtype not in _numeric_dtypes: + raise TypeError('Only numeric dtypes are allowed in subtract') + # Call result type here just to raise on disallowed type combinations + _result_type(x1.dtype, x2.dtype) + x1, x2 = Array._normalize_two_args(x1, x2) + return Array._new(np.subtract(x1._array, x2._array)) + +def tan(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.tan `. + + See its docstring for more information. + """ + if x.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed in tan') + return Array._new(np.tan(x._array)) + +def tanh(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.tanh `. + + See its docstring for more information. + """ + if x.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed in tanh') + return Array._new(np.tanh(x._array)) + +def trunc(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.trunc `. + + See its docstring for more information. + """ + if x.dtype not in _numeric_dtypes: + raise TypeError('Only numeric dtypes are allowed in trunc') + if x.dtype in _integer_dtypes: + # Note: The return dtype of trunc is the same as the input + return x + return Array._new(np.trunc(x._array)) -- cgit v1.2.1 From 8f7d00ed447174d9398af3365709222b529c1cad Mon Sep 17 00:00:00 2001 From: Aaron Meurer Date: Fri, 6 Aug 2021 18:22:00 -0600 Subject: Run (selective) black on the array_api submodule I've omitted a few changes from black that messed up the readability of some complicated if statements that were organized logically line-by-line, and some changes that use unnecessary operator spacing. --- numpy/array_api/_elementwise_functions.py | 194 ++++++++++++++++++++---------- 1 file changed, 132 insertions(+), 62 deletions(-) (limited to 'numpy/array_api/_elementwise_functions.py') diff --git a/numpy/array_api/_elementwise_functions.py b/numpy/array_api/_elementwise_functions.py index 7833ebe54..4408fe833 100644 --- a/numpy/array_api/_elementwise_functions.py +++ b/numpy/array_api/_elementwise_functions.py @@ -1,12 +1,18 @@ from __future__ import annotations -from ._dtypes import (_boolean_dtypes, _floating_dtypes, - _integer_dtypes, _integer_or_boolean_dtypes, - _numeric_dtypes, _result_type) +from ._dtypes import ( + _boolean_dtypes, + _floating_dtypes, + _integer_dtypes, + _integer_or_boolean_dtypes, + _numeric_dtypes, + _result_type, +) from ._array_object import Array import numpy as np + def abs(x: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.abs `. @@ -14,9 +20,10 @@ def abs(x: Array, /) -> Array: See its docstring for more information. """ if x.dtype not in _numeric_dtypes: - raise TypeError('Only numeric dtypes are allowed in abs') + raise TypeError("Only numeric dtypes are allowed in abs") return Array._new(np.abs(x._array)) + # Note: the function name is different here def acos(x: Array, /) -> Array: """ @@ -25,9 +32,10 @@ def acos(x: Array, /) -> Array: See its docstring for more information. """ if x.dtype not in _floating_dtypes: - raise TypeError('Only floating-point dtypes are allowed in acos') + raise TypeError("Only floating-point dtypes are allowed in acos") return Array._new(np.arccos(x._array)) + # Note: the function name is different here def acosh(x: Array, /) -> Array: """ @@ -36,9 +44,10 @@ def acosh(x: Array, /) -> Array: See its docstring for more information. """ if x.dtype not in _floating_dtypes: - raise TypeError('Only floating-point dtypes are allowed in acosh') + raise TypeError("Only floating-point dtypes are allowed in acosh") return Array._new(np.arccosh(x._array)) + def add(x1: Array, x2: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.add `. @@ -46,12 +55,13 @@ def add(x1: Array, x2: Array, /) -> Array: See its docstring for more information. """ if x1.dtype not in _numeric_dtypes or x2.dtype not in _numeric_dtypes: - raise TypeError('Only numeric dtypes are allowed in add') + raise TypeError("Only numeric dtypes are allowed in add") # Call result type here just to raise on disallowed type combinations _result_type(x1.dtype, x2.dtype) x1, x2 = Array._normalize_two_args(x1, x2) return Array._new(np.add(x1._array, x2._array)) + # Note: the function name is different here def asin(x: Array, /) -> Array: """ @@ -60,9 +70,10 @@ def asin(x: Array, /) -> Array: See its docstring for more information. """ if x.dtype not in _floating_dtypes: - raise TypeError('Only floating-point dtypes are allowed in asin') + raise TypeError("Only floating-point dtypes are allowed in asin") return Array._new(np.arcsin(x._array)) + # Note: the function name is different here def asinh(x: Array, /) -> Array: """ @@ -71,9 +82,10 @@ def asinh(x: Array, /) -> Array: See its docstring for more information. """ if x.dtype not in _floating_dtypes: - raise TypeError('Only floating-point dtypes are allowed in asinh') + raise TypeError("Only floating-point dtypes are allowed in asinh") return Array._new(np.arcsinh(x._array)) + # Note: the function name is different here def atan(x: Array, /) -> Array: """ @@ -82,9 +94,10 @@ def atan(x: Array, /) -> Array: See its docstring for more information. """ if x.dtype not in _floating_dtypes: - raise TypeError('Only floating-point dtypes are allowed in atan') + raise TypeError("Only floating-point dtypes are allowed in atan") return Array._new(np.arctan(x._array)) + # Note: the function name is different here def atan2(x1: Array, x2: Array, /) -> Array: """ @@ -93,12 +106,13 @@ def atan2(x1: Array, x2: Array, /) -> Array: See its docstring for more information. """ if x1.dtype not in _floating_dtypes or x2.dtype not in _floating_dtypes: - raise TypeError('Only floating-point dtypes are allowed in atan2') + raise TypeError("Only floating-point dtypes are allowed in atan2") # Call result type here just to raise on disallowed type combinations _result_type(x1.dtype, x2.dtype) x1, x2 = Array._normalize_two_args(x1, x2) return Array._new(np.arctan2(x1._array, x2._array)) + # Note: the function name is different here def atanh(x: Array, /) -> Array: """ @@ -107,22 +121,27 @@ def atanh(x: Array, /) -> Array: See its docstring for more information. """ if x.dtype not in _floating_dtypes: - raise TypeError('Only floating-point dtypes are allowed in atanh') + raise TypeError("Only floating-point dtypes are allowed in atanh") return Array._new(np.arctanh(x._array)) + def bitwise_and(x1: Array, x2: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.bitwise_and `. See its docstring for more information. """ - if x1.dtype not in _integer_or_boolean_dtypes or x2.dtype not in _integer_or_boolean_dtypes: - raise TypeError('Only integer or boolean dtypes are allowed in bitwise_and') + if ( + x1.dtype not in _integer_or_boolean_dtypes + or x2.dtype not in _integer_or_boolean_dtypes + ): + raise TypeError("Only integer or boolean dtypes are allowed in bitwise_and") # Call result type here just to raise on disallowed type combinations _result_type(x1.dtype, x2.dtype) x1, x2 = Array._normalize_two_args(x1, x2) return Array._new(np.bitwise_and(x1._array, x2._array)) + # Note: the function name is different here def bitwise_left_shift(x1: Array, x2: Array, /) -> Array: """ @@ -131,15 +150,16 @@ def bitwise_left_shift(x1: Array, x2: Array, /) -> Array: See its docstring for more information. """ if x1.dtype not in _integer_dtypes or x2.dtype not in _integer_dtypes: - raise TypeError('Only integer dtypes are allowed in bitwise_left_shift') + raise TypeError("Only integer dtypes are allowed in bitwise_left_shift") # Call result type here just to raise on disallowed type combinations _result_type(x1.dtype, x2.dtype) x1, x2 = Array._normalize_two_args(x1, x2) # Note: bitwise_left_shift is only defined for x2 nonnegative. if np.any(x2._array < 0): - raise ValueError('bitwise_left_shift(x1, x2) is only defined for x2 >= 0') + raise ValueError("bitwise_left_shift(x1, x2) is only defined for x2 >= 0") return Array._new(np.left_shift(x1._array, x2._array)) + # Note: the function name is different here def bitwise_invert(x: Array, /) -> Array: """ @@ -148,22 +168,27 @@ def bitwise_invert(x: Array, /) -> Array: See its docstring for more information. """ if x.dtype not in _integer_or_boolean_dtypes: - raise TypeError('Only integer or boolean dtypes are allowed in bitwise_invert') + raise TypeError("Only integer or boolean dtypes are allowed in bitwise_invert") return Array._new(np.invert(x._array)) + def bitwise_or(x1: Array, x2: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.bitwise_or `. See its docstring for more information. """ - if x1.dtype not in _integer_or_boolean_dtypes or x2.dtype not in _integer_or_boolean_dtypes: - raise TypeError('Only integer or boolean dtypes are allowed in bitwise_or') + if ( + x1.dtype not in _integer_or_boolean_dtypes + or x2.dtype not in _integer_or_boolean_dtypes + ): + raise TypeError("Only integer or boolean dtypes are allowed in bitwise_or") # Call result type here just to raise on disallowed type combinations _result_type(x1.dtype, x2.dtype) x1, x2 = Array._normalize_two_args(x1, x2) return Array._new(np.bitwise_or(x1._array, x2._array)) + # Note: the function name is different here def bitwise_right_shift(x1: Array, x2: Array, /) -> Array: """ @@ -172,28 +197,33 @@ def bitwise_right_shift(x1: Array, x2: Array, /) -> Array: See its docstring for more information. """ if x1.dtype not in _integer_dtypes or x2.dtype not in _integer_dtypes: - raise TypeError('Only integer dtypes are allowed in bitwise_right_shift') + raise TypeError("Only integer dtypes are allowed in bitwise_right_shift") # Call result type here just to raise on disallowed type combinations _result_type(x1.dtype, x2.dtype) x1, x2 = Array._normalize_two_args(x1, x2) # Note: bitwise_right_shift is only defined for x2 nonnegative. if np.any(x2._array < 0): - raise ValueError('bitwise_right_shift(x1, x2) is only defined for x2 >= 0') + raise ValueError("bitwise_right_shift(x1, x2) is only defined for x2 >= 0") return Array._new(np.right_shift(x1._array, x2._array)) + def bitwise_xor(x1: Array, x2: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.bitwise_xor `. See its docstring for more information. """ - if x1.dtype not in _integer_or_boolean_dtypes or x2.dtype not in _integer_or_boolean_dtypes: - raise TypeError('Only integer or boolean dtypes are allowed in bitwise_xor') + if ( + x1.dtype not in _integer_or_boolean_dtypes + or x2.dtype not in _integer_or_boolean_dtypes + ): + raise TypeError("Only integer or boolean dtypes are allowed in bitwise_xor") # Call result type here just to raise on disallowed type combinations _result_type(x1.dtype, x2.dtype) x1, x2 = Array._normalize_two_args(x1, x2) return Array._new(np.bitwise_xor(x1._array, x2._array)) + def ceil(x: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.ceil `. @@ -201,12 +231,13 @@ def ceil(x: Array, /) -> Array: See its docstring for more information. """ if x.dtype not in _numeric_dtypes: - raise TypeError('Only numeric dtypes are allowed in ceil') + raise TypeError("Only numeric dtypes are allowed in ceil") if x.dtype in _integer_dtypes: # Note: The return dtype of ceil is the same as the input return x return Array._new(np.ceil(x._array)) + def cos(x: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.cos `. @@ -214,9 +245,10 @@ def cos(x: Array, /) -> Array: See its docstring for more information. """ if x.dtype not in _floating_dtypes: - raise TypeError('Only floating-point dtypes are allowed in cos') + raise TypeError("Only floating-point dtypes are allowed in cos") return Array._new(np.cos(x._array)) + def cosh(x: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.cosh `. @@ -224,9 +256,10 @@ def cosh(x: Array, /) -> Array: See its docstring for more information. """ if x.dtype not in _floating_dtypes: - raise TypeError('Only floating-point dtypes are allowed in cosh') + raise TypeError("Only floating-point dtypes are allowed in cosh") return Array._new(np.cosh(x._array)) + def divide(x1: Array, x2: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.divide `. @@ -234,12 +267,13 @@ def divide(x1: Array, x2: Array, /) -> Array: See its docstring for more information. """ if x1.dtype not in _floating_dtypes or x2.dtype not in _floating_dtypes: - raise TypeError('Only floating-point dtypes are allowed in divide') + raise TypeError("Only floating-point dtypes are allowed in divide") # Call result type here just to raise on disallowed type combinations _result_type(x1.dtype, x2.dtype) x1, x2 = Array._normalize_two_args(x1, x2) return Array._new(np.divide(x1._array, x2._array)) + def equal(x1: Array, x2: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.equal `. @@ -251,6 +285,7 @@ def equal(x1: Array, x2: Array, /) -> Array: x1, x2 = Array._normalize_two_args(x1, x2) return Array._new(np.equal(x1._array, x2._array)) + def exp(x: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.exp `. @@ -258,9 +293,10 @@ def exp(x: Array, /) -> Array: See its docstring for more information. """ if x.dtype not in _floating_dtypes: - raise TypeError('Only floating-point dtypes are allowed in exp') + raise TypeError("Only floating-point dtypes are allowed in exp") return Array._new(np.exp(x._array)) + def expm1(x: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.expm1 `. @@ -268,9 +304,10 @@ def expm1(x: Array, /) -> Array: See its docstring for more information. """ if x.dtype not in _floating_dtypes: - raise TypeError('Only floating-point dtypes are allowed in expm1') + raise TypeError("Only floating-point dtypes are allowed in expm1") return Array._new(np.expm1(x._array)) + def floor(x: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.floor `. @@ -278,12 +315,13 @@ def floor(x: Array, /) -> Array: See its docstring for more information. """ if x.dtype not in _numeric_dtypes: - raise TypeError('Only numeric dtypes are allowed in floor') + raise TypeError("Only numeric dtypes are allowed in floor") if x.dtype in _integer_dtypes: # Note: The return dtype of floor is the same as the input return x return Array._new(np.floor(x._array)) + def floor_divide(x1: Array, x2: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.floor_divide `. @@ -291,12 +329,13 @@ def floor_divide(x1: Array, x2: Array, /) -> Array: See its docstring for more information. """ if x1.dtype not in _numeric_dtypes or x2.dtype not in _numeric_dtypes: - raise TypeError('Only numeric dtypes are allowed in floor_divide') + raise TypeError("Only numeric dtypes are allowed in floor_divide") # Call result type here just to raise on disallowed type combinations _result_type(x1.dtype, x2.dtype) x1, x2 = Array._normalize_two_args(x1, x2) return Array._new(np.floor_divide(x1._array, x2._array)) + def greater(x1: Array, x2: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.greater `. @@ -304,12 +343,13 @@ def greater(x1: Array, x2: Array, /) -> Array: See its docstring for more information. """ if x1.dtype not in _numeric_dtypes or x2.dtype not in _numeric_dtypes: - raise TypeError('Only numeric dtypes are allowed in greater') + raise TypeError("Only numeric dtypes are allowed in greater") # Call result type here just to raise on disallowed type combinations _result_type(x1.dtype, x2.dtype) x1, x2 = Array._normalize_two_args(x1, x2) return Array._new(np.greater(x1._array, x2._array)) + def greater_equal(x1: Array, x2: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.greater_equal `. @@ -317,12 +357,13 @@ def greater_equal(x1: Array, x2: Array, /) -> Array: See its docstring for more information. """ if x1.dtype not in _numeric_dtypes or x2.dtype not in _numeric_dtypes: - raise TypeError('Only numeric dtypes are allowed in greater_equal') + raise TypeError("Only numeric dtypes are allowed in greater_equal") # Call result type here just to raise on disallowed type combinations _result_type(x1.dtype, x2.dtype) x1, x2 = Array._normalize_two_args(x1, x2) return Array._new(np.greater_equal(x1._array, x2._array)) + def isfinite(x: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.isfinite `. @@ -330,9 +371,10 @@ def isfinite(x: Array, /) -> Array: See its docstring for more information. """ if x.dtype not in _numeric_dtypes: - raise TypeError('Only numeric dtypes are allowed in isfinite') + raise TypeError("Only numeric dtypes are allowed in isfinite") return Array._new(np.isfinite(x._array)) + def isinf(x: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.isinf `. @@ -340,9 +382,10 @@ def isinf(x: Array, /) -> Array: See its docstring for more information. """ if x.dtype not in _numeric_dtypes: - raise TypeError('Only numeric dtypes are allowed in isinf') + raise TypeError("Only numeric dtypes are allowed in isinf") return Array._new(np.isinf(x._array)) + def isnan(x: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.isnan `. @@ -350,9 +393,10 @@ def isnan(x: Array, /) -> Array: See its docstring for more information. """ if x.dtype not in _numeric_dtypes: - raise TypeError('Only numeric dtypes are allowed in isnan') + raise TypeError("Only numeric dtypes are allowed in isnan") return Array._new(np.isnan(x._array)) + def less(x1: Array, x2: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.less `. @@ -360,12 +404,13 @@ def less(x1: Array, x2: Array, /) -> Array: See its docstring for more information. """ if x1.dtype not in _numeric_dtypes or x2.dtype not in _numeric_dtypes: - raise TypeError('Only numeric dtypes are allowed in less') + raise TypeError("Only numeric dtypes are allowed in less") # Call result type here just to raise on disallowed type combinations _result_type(x1.dtype, x2.dtype) x1, x2 = Array._normalize_two_args(x1, x2) return Array._new(np.less(x1._array, x2._array)) + def less_equal(x1: Array, x2: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.less_equal `. @@ -373,12 +418,13 @@ def less_equal(x1: Array, x2: Array, /) -> Array: See its docstring for more information. """ if x1.dtype not in _numeric_dtypes or x2.dtype not in _numeric_dtypes: - raise TypeError('Only numeric dtypes are allowed in less_equal') + raise TypeError("Only numeric dtypes are allowed in less_equal") # Call result type here just to raise on disallowed type combinations _result_type(x1.dtype, x2.dtype) x1, x2 = Array._normalize_two_args(x1, x2) return Array._new(np.less_equal(x1._array, x2._array)) + def log(x: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.log `. @@ -386,9 +432,10 @@ def log(x: Array, /) -> Array: See its docstring for more information. """ if x.dtype not in _floating_dtypes: - raise TypeError('Only floating-point dtypes are allowed in log') + raise TypeError("Only floating-point dtypes are allowed in log") return Array._new(np.log(x._array)) + def log1p(x: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.log1p `. @@ -396,9 +443,10 @@ def log1p(x: Array, /) -> Array: See its docstring for more information. """ if x.dtype not in _floating_dtypes: - raise TypeError('Only floating-point dtypes are allowed in log1p') + raise TypeError("Only floating-point dtypes are allowed in log1p") return Array._new(np.log1p(x._array)) + def log2(x: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.log2 `. @@ -406,9 +454,10 @@ def log2(x: Array, /) -> Array: See its docstring for more information. """ if x.dtype not in _floating_dtypes: - raise TypeError('Only floating-point dtypes are allowed in log2') + raise TypeError("Only floating-point dtypes are allowed in log2") return Array._new(np.log2(x._array)) + def log10(x: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.log10 `. @@ -416,9 +465,10 @@ def log10(x: Array, /) -> Array: See its docstring for more information. """ if x.dtype not in _floating_dtypes: - raise TypeError('Only floating-point dtypes are allowed in log10') + raise TypeError("Only floating-point dtypes are allowed in log10") return Array._new(np.log10(x._array)) + def logaddexp(x1: Array, x2: Array) -> Array: """ Array API compatible wrapper for :py:func:`np.logaddexp `. @@ -426,12 +476,13 @@ def logaddexp(x1: Array, x2: Array) -> Array: See its docstring for more information. """ if x1.dtype not in _floating_dtypes or x2.dtype not in _floating_dtypes: - raise TypeError('Only floating-point dtypes are allowed in logaddexp') + raise TypeError("Only floating-point dtypes are allowed in logaddexp") # Call result type here just to raise on disallowed type combinations _result_type(x1.dtype, x2.dtype) x1, x2 = Array._normalize_two_args(x1, x2) return Array._new(np.logaddexp(x1._array, x2._array)) + def logical_and(x1: Array, x2: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.logical_and `. @@ -439,12 +490,13 @@ def logical_and(x1: Array, x2: Array, /) -> Array: See its docstring for more information. """ if x1.dtype not in _boolean_dtypes or x2.dtype not in _boolean_dtypes: - raise TypeError('Only boolean dtypes are allowed in logical_and') + raise TypeError("Only boolean dtypes are allowed in logical_and") # Call result type here just to raise on disallowed type combinations _result_type(x1.dtype, x2.dtype) x1, x2 = Array._normalize_two_args(x1, x2) return Array._new(np.logical_and(x1._array, x2._array)) + def logical_not(x: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.logical_not `. @@ -452,9 +504,10 @@ def logical_not(x: Array, /) -> Array: See its docstring for more information. """ if x.dtype not in _boolean_dtypes: - raise TypeError('Only boolean dtypes are allowed in logical_not') + raise TypeError("Only boolean dtypes are allowed in logical_not") return Array._new(np.logical_not(x._array)) + def logical_or(x1: Array, x2: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.logical_or `. @@ -462,12 +515,13 @@ def logical_or(x1: Array, x2: Array, /) -> Array: See its docstring for more information. """ if x1.dtype not in _boolean_dtypes or x2.dtype not in _boolean_dtypes: - raise TypeError('Only boolean dtypes are allowed in logical_or') + raise TypeError("Only boolean dtypes are allowed in logical_or") # Call result type here just to raise on disallowed type combinations _result_type(x1.dtype, x2.dtype) x1, x2 = Array._normalize_two_args(x1, x2) return Array._new(np.logical_or(x1._array, x2._array)) + def logical_xor(x1: Array, x2: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.logical_xor `. @@ -475,12 +529,13 @@ def logical_xor(x1: Array, x2: Array, /) -> Array: See its docstring for more information. """ if x1.dtype not in _boolean_dtypes or x2.dtype not in _boolean_dtypes: - raise TypeError('Only boolean dtypes are allowed in logical_xor') + raise TypeError("Only boolean dtypes are allowed in logical_xor") # Call result type here just to raise on disallowed type combinations _result_type(x1.dtype, x2.dtype) x1, x2 = Array._normalize_two_args(x1, x2) return Array._new(np.logical_xor(x1._array, x2._array)) + def multiply(x1: Array, x2: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.multiply `. @@ -488,12 +543,13 @@ def multiply(x1: Array, x2: Array, /) -> Array: See its docstring for more information. """ if x1.dtype not in _numeric_dtypes or x2.dtype not in _numeric_dtypes: - raise TypeError('Only numeric dtypes are allowed in multiply') + raise TypeError("Only numeric dtypes are allowed in multiply") # Call result type here just to raise on disallowed type combinations _result_type(x1.dtype, x2.dtype) x1, x2 = Array._normalize_two_args(x1, x2) return Array._new(np.multiply(x1._array, x2._array)) + def negative(x: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.negative `. @@ -501,9 +557,10 @@ def negative(x: Array, /) -> Array: See its docstring for more information. """ if x.dtype not in _numeric_dtypes: - raise TypeError('Only numeric dtypes are allowed in negative') + raise TypeError("Only numeric dtypes are allowed in negative") return Array._new(np.negative(x._array)) + def not_equal(x1: Array, x2: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.not_equal `. @@ -515,6 +572,7 @@ def not_equal(x1: Array, x2: Array, /) -> Array: x1, x2 = Array._normalize_two_args(x1, x2) return Array._new(np.not_equal(x1._array, x2._array)) + def positive(x: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.positive `. @@ -522,9 +580,10 @@ def positive(x: Array, /) -> Array: See its docstring for more information. """ if x.dtype not in _numeric_dtypes: - raise TypeError('Only numeric dtypes are allowed in positive') + raise TypeError("Only numeric dtypes are allowed in positive") return Array._new(np.positive(x._array)) + # Note: the function name is different here def pow(x1: Array, x2: Array, /) -> Array: """ @@ -533,12 +592,13 @@ def pow(x1: Array, x2: Array, /) -> Array: See its docstring for more information. """ if x1.dtype not in _floating_dtypes or x2.dtype not in _floating_dtypes: - raise TypeError('Only floating-point dtypes are allowed in pow') + raise TypeError("Only floating-point dtypes are allowed in pow") # Call result type here just to raise on disallowed type combinations _result_type(x1.dtype, x2.dtype) x1, x2 = Array._normalize_two_args(x1, x2) return Array._new(np.power(x1._array, x2._array)) + def remainder(x1: Array, x2: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.remainder `. @@ -546,12 +606,13 @@ def remainder(x1: Array, x2: Array, /) -> Array: See its docstring for more information. """ if x1.dtype not in _numeric_dtypes or x2.dtype not in _numeric_dtypes: - raise TypeError('Only numeric dtypes are allowed in remainder') + raise TypeError("Only numeric dtypes are allowed in remainder") # Call result type here just to raise on disallowed type combinations _result_type(x1.dtype, x2.dtype) x1, x2 = Array._normalize_two_args(x1, x2) return Array._new(np.remainder(x1._array, x2._array)) + def round(x: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.round `. @@ -559,9 +620,10 @@ def round(x: Array, /) -> Array: See its docstring for more information. """ if x.dtype not in _numeric_dtypes: - raise TypeError('Only numeric dtypes are allowed in round') + raise TypeError("Only numeric dtypes are allowed in round") return Array._new(np.round(x._array)) + def sign(x: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.sign `. @@ -569,9 +631,10 @@ def sign(x: Array, /) -> Array: See its docstring for more information. """ if x.dtype not in _numeric_dtypes: - raise TypeError('Only numeric dtypes are allowed in sign') + raise TypeError("Only numeric dtypes are allowed in sign") return Array._new(np.sign(x._array)) + def sin(x: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.sin `. @@ -579,9 +642,10 @@ def sin(x: Array, /) -> Array: See its docstring for more information. """ if x.dtype not in _floating_dtypes: - raise TypeError('Only floating-point dtypes are allowed in sin') + raise TypeError("Only floating-point dtypes are allowed in sin") return Array._new(np.sin(x._array)) + def sinh(x: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.sinh `. @@ -589,9 +653,10 @@ def sinh(x: Array, /) -> Array: See its docstring for more information. """ if x.dtype not in _floating_dtypes: - raise TypeError('Only floating-point dtypes are allowed in sinh') + raise TypeError("Only floating-point dtypes are allowed in sinh") return Array._new(np.sinh(x._array)) + def square(x: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.square `. @@ -599,9 +664,10 @@ def square(x: Array, /) -> Array: See its docstring for more information. """ if x.dtype not in _numeric_dtypes: - raise TypeError('Only numeric dtypes are allowed in square') + raise TypeError("Only numeric dtypes are allowed in square") return Array._new(np.square(x._array)) + def sqrt(x: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.sqrt `. @@ -609,9 +675,10 @@ def sqrt(x: Array, /) -> Array: See its docstring for more information. """ if x.dtype not in _floating_dtypes: - raise TypeError('Only floating-point dtypes are allowed in sqrt') + raise TypeError("Only floating-point dtypes are allowed in sqrt") return Array._new(np.sqrt(x._array)) + def subtract(x1: Array, x2: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.subtract `. @@ -619,12 +686,13 @@ def subtract(x1: Array, x2: Array, /) -> Array: See its docstring for more information. """ if x1.dtype not in _numeric_dtypes or x2.dtype not in _numeric_dtypes: - raise TypeError('Only numeric dtypes are allowed in subtract') + raise TypeError("Only numeric dtypes are allowed in subtract") # Call result type here just to raise on disallowed type combinations _result_type(x1.dtype, x2.dtype) x1, x2 = Array._normalize_two_args(x1, x2) return Array._new(np.subtract(x1._array, x2._array)) + def tan(x: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.tan `. @@ -632,9 +700,10 @@ def tan(x: Array, /) -> Array: See its docstring for more information. """ if x.dtype not in _floating_dtypes: - raise TypeError('Only floating-point dtypes are allowed in tan') + raise TypeError("Only floating-point dtypes are allowed in tan") return Array._new(np.tan(x._array)) + def tanh(x: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.tanh `. @@ -642,9 +711,10 @@ def tanh(x: Array, /) -> Array: See its docstring for more information. """ if x.dtype not in _floating_dtypes: - raise TypeError('Only floating-point dtypes are allowed in tanh') + raise TypeError("Only floating-point dtypes are allowed in tanh") return Array._new(np.tanh(x._array)) + def trunc(x: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.trunc `. @@ -652,7 +722,7 @@ def trunc(x: Array, /) -> Array: See its docstring for more information. """ if x.dtype not in _numeric_dtypes: - raise TypeError('Only numeric dtypes are allowed in trunc') + raise TypeError("Only numeric dtypes are allowed in trunc") if x.dtype in _integer_dtypes: # Note: The return dtype of trunc is the same as the input return x -- cgit v1.2.1