Skip to content

Commit 22456c4

Browse files
authored
Implements sign and remainder (#1308)
* Implements sign and remainder * Address review by @oleksandr-pavlyk - Sign now uses appropriate templates from elementwise_common namespace - Sign standardizes returned NaN value - Added an annotation to `test_sign_negative` clarifying that it is testing for all signed, real data types * Remainder now handles 0 divisors correctly for unsigned integers - Added a test for this behavior for signed and unsigned integers - Implemented l_xor private method to improve remainder readability
1 parent 2c4f3c8 commit 22456c4

File tree

7 files changed

+1061
-9
lines changed

7 files changed

+1061
-9
lines changed

dpctl/tensor/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@
135135
pow,
136136
proj,
137137
real,
138+
remainder,
138139
round,
140+
sign,
139141
sin,
140142
sinh,
141143
sqrt,
@@ -265,7 +267,9 @@
265267
"logaddexp",
266268
"proj",
267269
"real",
270+
"remainder",
268271
"round",
272+
"sign",
269273
"sin",
270274
"sinh",
271275
"sqrt",

dpctl/tensor/_elementwise_funcs.py

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -838,9 +838,9 @@
838838
839839
Args:
840840
x1 (usm_ndarray):
841-
First input array, expected to have numeric data type.
841+
First input array, expected to have a real-valued data type.
842842
x2 (usm_ndarray):
843-
Second input array, also expected to have numeric data type.
843+
Second input array, also expected to have real-valued data type.
844844
out ({None, usm_ndarray}, optional):
845845
Output array to populate.
846846
Array have the correct shape and the expected data type.
@@ -1149,7 +1149,27 @@
11491149
)
11501150

11511151
# B22: ==== REMAINDER (x1, x2)
1152-
# FIXME: implement B22
1152+
_remainder_docstring_ = """
1153+
remainder(x1, x2, out=None, order='K')
1154+
1155+
Calculates the remainder of division for each element `x1_i` of the input array
1156+
`x1` with the respective element `x2_i` of the input array `x2`.
1157+
1158+
This function is equivalent to the Python modulus operator.
1159+
1160+
Args:
1161+
x1 (usm_ndarray):
1162+
First input array, expected to have a real-valued data type.
1163+
x2 (usm_ndarray):
1164+
Second input array, also expected to have a real-valued data type.
1165+
Returns:
1166+
usm_ndarray:
1167+
an array containing the element-wise remainders. The data type of
1168+
the returned array is determined by the Type Promotion Rules.
1169+
"""
1170+
remainder = BinaryElementwiseFunc(
1171+
"remainder", ti._remainder_result_type, ti._remainder, _remainder_docstring_
1172+
)
11531173

11541174
# U28: ==== ROUND (x)
11551175
_round_docstring = """
@@ -1178,7 +1198,33 @@
11781198
)
11791199

11801200
# U29: ==== SIGN (x)
1181-
# FIXME: implement U29
1201+
_sign_docstring = """
1202+
sign(x, out=None, order='K')
1203+
1204+
Computes an indication of the sign of each element `x_i` of input array `x`
1205+
using the signum function.
1206+
1207+
The signum function returns `-1` if `x_i` is less than `0`,
1208+
`0` if `x_i` is equal to `0`, and `1` if `x_i` is greater than `0`.
1209+
1210+
Args:
1211+
x (usm_ndarray):
1212+
Input array, expected to have a numeric data type.
1213+
out ({None, usm_ndarray}, optional):
1214+
Output array to populate.
1215+
Array have the correct shape and the expected data type.
1216+
order ("C","F","A","K", optional):
1217+
Memory layout of the newly output array, if parameter `out` is `None`.
1218+
Default: "K".
1219+
Returns:
1220+
usm_narray:
1221+
An array containing the element-wise results. The data type of the
1222+
returned array is determined by the Type Promotion Rules.
1223+
"""
1224+
1225+
sign = UnaryElementwiseFunc(
1226+
"sign", ti._sign_result_type, ti._sign, _sign_docstring
1227+
)
11821228

11831229
# U30: ==== SIN (x)
11841230
_sin_docstring = """

0 commit comments

Comments
 (0)