Skip to content

Commit ccd9cbd

Browse files
committed
Return scalar, if both inputs are scalars
1 parent 8497870 commit ccd9cbd

File tree

2 files changed

+31
-23
lines changed

2 files changed

+31
-23
lines changed

dpnp/dpnp_iface_mathematical.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,12 +1091,13 @@ def multiply(x1,
10911091
10921092
Returns
10931093
-------
1094-
y : dpnp.ndarray
1094+
y : {dpnp.ndarray, scalar}
10951095
The product of `x1` and `x2`, element-wise.
1096+
The result is a scalar if both x1 and x2 are scalars.
10961097
10971098
Limitations
10981099
-----------
1099-
Parameters ``x1`` and ``x2`` are supported as either :class:`dpnp.ndarray` or scalar.
1100+
Parameters `x1` and `x2` are supported as either :class:`dpnp.ndarray` or scalar.
11001101
Parameters ``out``, ``where``, ``dtype`` and ``subok`` are supported with their default values.
11011102
Keyword arguments ``kwargs`` are currently unsupported.
11021103
Otherwise the functions will be executed sequentially on CPU.
@@ -1120,6 +1121,9 @@ def multiply(x1,
11201121
pass
11211122
elif subok is not True:
11221123
pass
1124+
elif dpnp.isscalar(x1) and dpnp.isscalar(x2):
1125+
# keep the result in host memory, if both inputs are scalars
1126+
return x1 * x2
11231127
else:
11241128
# get a common queue to copy data from the host into a device if any input is scalar
11251129
queue = get_common_allocation_queue([x1, x2]) if dpnp.isscalar(x1) or dpnp.isscalar(x2) else None

tests/third_party/cupy/math_tests/test_arithmetic.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -145,27 +145,31 @@ def check_binary(self, xp):
145145
y = y.astype(numpy.complex64)
146146

147147
# NumPy returns an output array of another type than DPNP when input ones have diffrent types.
148-
if self.name == 'multiply' and xp is cupy and dtype1 != dtype2:
149-
is_array_arg1 = not xp.isscalar(arg1)
150-
is_array_arg2 = not xp.isscalar(arg2)
151-
152-
is_int_float = lambda _x, _y: numpy.issubdtype(_x, numpy.integer) and numpy.issubdtype(_y, numpy.floating)
153-
is_same_type = lambda _x, _y, _type: numpy.issubdtype(_x, _type) and numpy.issubdtype(_y, _type)
154-
155-
if is_array_arg1 and is_array_arg2:
156-
# If both inputs are arrays where one is of floating type and another - integer,
157-
# NumPy will return an output array of always "float64" type,
158-
# while DPNP will return the array of a wider type from the input arrays.
159-
if is_int_float(dtype1, dtype2) or is_int_float(dtype2, dtype1):
160-
y = y.astype(numpy.float64)
161-
elif is_same_type(dtype1, dtype2, numpy.floating) or is_same_type(dtype1, dtype2, numpy.integer):
162-
# If one input is an array and another - scalar,
163-
# NumPy will return an output array of the same type as the inpupt array has,
164-
# while DPNP will return the array of a wider type from the inputs (considering both array and scalar).
165-
if is_array_arg1 and not is_array_arg2:
166-
y = y.astype(dtype1)
167-
elif is_array_arg2 and not is_array_arg1:
168-
y = y.astype(dtype2)
148+
if self.name == 'multiply' and xp is cupy:
149+
if xp.isscalar(arg1) and xp.isscalar(arg2):
150+
# If both are scalars, the result will be a scalar, so needs to convert into numpy-scalar.
151+
y = numpy.asarray(y)
152+
elif dtype1 != dtype2:
153+
is_array_arg1 = not xp.isscalar(arg1)
154+
is_array_arg2 = not xp.isscalar(arg2)
155+
156+
is_int_float = lambda _x, _y: numpy.issubdtype(_x, numpy.integer) and numpy.issubdtype(_y, numpy.floating)
157+
is_same_type = lambda _x, _y, _type: numpy.issubdtype(_x, _type) and numpy.issubdtype(_y, _type)
158+
159+
if is_array_arg1 and is_array_arg2:
160+
# If both inputs are arrays where one is of floating type and another - integer,
161+
# NumPy will return an output array of always "float64" type,
162+
# while DPNP will return the array of a wider type from the input arrays.
163+
if is_int_float(dtype1, dtype2) or is_int_float(dtype2, dtype1):
164+
y = y.astype(numpy.float64)
165+
elif is_same_type(dtype1, dtype2, numpy.floating) or is_same_type(dtype1, dtype2, numpy.integer):
166+
# If one input is an array and another - scalar,
167+
# NumPy will return an output array of the same type as the inpupt array has,
168+
# while DPNP will return the array of a wider type from the inputs (considering both array and scalar).
169+
if is_array_arg1 and not is_array_arg2:
170+
y = y.astype(dtype1)
171+
elif is_array_arg2 and not is_array_arg1:
172+
y = y.astype(dtype2)
169173

170174
# NumPy returns different values (nan/inf) on division by zero
171175
# depending on the architecture.

0 commit comments

Comments
 (0)